bxe.c revision 284651
1255736Sdavidch/*-
2265411Sdavidcs * Copyright (c) 2007-2014 QLogic Corporation. All rights reserved.
3255736Sdavidch *
4255736Sdavidch * Redistribution and use in source and binary forms, with or without
5255736Sdavidch * modification, are permitted provided that the following conditions
6255736Sdavidch * are met:
7255736Sdavidch *
8255736Sdavidch * 1. Redistributions of source code must retain the above copyright
9255736Sdavidch *    notice, this list of conditions and the following disclaimer.
10255736Sdavidch * 2. Redistributions in binary form must reproduce the above copyright
11255736Sdavidch *    notice, this list of conditions and the following disclaimer in the
12255736Sdavidch *    documentation and/or other materials provided with the distribution.
13255736Sdavidch *
14255736Sdavidch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
15255736Sdavidch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16255736Sdavidch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17255736Sdavidch * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
18255736Sdavidch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19255736Sdavidch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20255736Sdavidch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21255736Sdavidch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22255736Sdavidch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23255736Sdavidch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24255736Sdavidch * THE POSSIBILITY OF SUCH DAMAGE.
25255736Sdavidch */
26255736Sdavidch
27255736Sdavidch#include <sys/cdefs.h>
28255736Sdavidch__FBSDID("$FreeBSD: head/sys/dev/bxe/bxe.c 284651 2015-06-20 22:24:44Z davidcs $");
29255736Sdavidch
30283274Sdavidcs#define BXE_DRIVER_VERSION "1.78.79"
31255736Sdavidch
32255736Sdavidch#include "bxe.h"
33255736Sdavidch#include "ecore_sp.h"
34255736Sdavidch#include "ecore_init.h"
35255736Sdavidch#include "ecore_init_ops.h"
36255736Sdavidch
37255736Sdavidch#include "57710_int_offsets.h"
38255736Sdavidch#include "57711_int_offsets.h"
39255736Sdavidch#include "57712_int_offsets.h"
40255736Sdavidch
41255736Sdavidch/*
42255736Sdavidch * CTLTYPE_U64 and sysctl_handle_64 were added in r217616. Define these
43255736Sdavidch * explicitly here for older kernels that don't include this changeset.
44255736Sdavidch */
45255736Sdavidch#ifndef CTLTYPE_U64
46255736Sdavidch#define CTLTYPE_U64      CTLTYPE_QUAD
47255736Sdavidch#define sysctl_handle_64 sysctl_handle_quad
48255736Sdavidch#endif
49255736Sdavidch
50255736Sdavidch/*
51255736Sdavidch * CSUM_TCP_IPV6 and CSUM_UDP_IPV6 were added in r236170. Define these
52255736Sdavidch * here as zero(0) for older kernels that don't include this changeset
53255736Sdavidch * thereby masking the functionality.
54255736Sdavidch */
55255736Sdavidch#ifndef CSUM_TCP_IPV6
56255736Sdavidch#define CSUM_TCP_IPV6 0
57255736Sdavidch#define CSUM_UDP_IPV6 0
58255736Sdavidch#endif
59255736Sdavidch
60255736Sdavidch/*
61255736Sdavidch * pci_find_cap was added in r219865. Re-define this at pci_find_extcap
62255736Sdavidch * for older kernels that don't include this changeset.
63255736Sdavidch */
64255736Sdavidch#if __FreeBSD_version < 900035
65255736Sdavidch#define pci_find_cap pci_find_extcap
66255736Sdavidch#endif
67255736Sdavidch
68255736Sdavidch#define BXE_DEF_SB_ATT_IDX 0x0001
69255736Sdavidch#define BXE_DEF_SB_IDX     0x0002
70255736Sdavidch
71255736Sdavidch/*
72255736Sdavidch * FLR Support - bxe_pf_flr_clnup() is called during nic_load in the per
73255736Sdavidch * function HW initialization.
74255736Sdavidch */
75255736Sdavidch#define FLR_WAIT_USEC     10000 /* 10 msecs */
76255736Sdavidch#define FLR_WAIT_INTERVAL 50    /* usecs */
77255736Sdavidch#define FLR_POLL_CNT      (FLR_WAIT_USEC / FLR_WAIT_INTERVAL) /* 200 */
78255736Sdavidch
79255736Sdavidchstruct pbf_pN_buf_regs {
80255736Sdavidch    int pN;
81255736Sdavidch    uint32_t init_crd;
82255736Sdavidch    uint32_t crd;
83255736Sdavidch    uint32_t crd_freed;
84255736Sdavidch};
85255736Sdavidch
86255736Sdavidchstruct pbf_pN_cmd_regs {
87255736Sdavidch    int pN;
88255736Sdavidch    uint32_t lines_occup;
89255736Sdavidch    uint32_t lines_freed;
90255736Sdavidch};
91255736Sdavidch
92255736Sdavidch/*
93255736Sdavidch * PCI Device ID Table used by bxe_probe().
94255736Sdavidch */
95255736Sdavidch#define BXE_DEVDESC_MAX 64
96255736Sdavidchstatic struct bxe_device_type bxe_devs[] = {
97255736Sdavidch    {
98255736Sdavidch        BRCM_VENDORID,
99255736Sdavidch        CHIP_NUM_57710,
100255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
101265703Sdavidcs        "QLogic NetXtreme II BCM57710 10GbE"
102255736Sdavidch    },
103255736Sdavidch    {
104255736Sdavidch        BRCM_VENDORID,
105255736Sdavidch        CHIP_NUM_57711,
106255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
107265703Sdavidcs        "QLogic NetXtreme II BCM57711 10GbE"
108255736Sdavidch    },
109255736Sdavidch    {
110255736Sdavidch        BRCM_VENDORID,
111255736Sdavidch        CHIP_NUM_57711E,
112255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
113265703Sdavidcs        "QLogic NetXtreme II BCM57711E 10GbE"
114255736Sdavidch    },
115255736Sdavidch    {
116255736Sdavidch        BRCM_VENDORID,
117255736Sdavidch        CHIP_NUM_57712,
118255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
119265703Sdavidcs        "QLogic NetXtreme II BCM57712 10GbE"
120255736Sdavidch    },
121255736Sdavidch    {
122255736Sdavidch        BRCM_VENDORID,
123255736Sdavidch        CHIP_NUM_57712_MF,
124255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
125265703Sdavidcs        "QLogic NetXtreme II BCM57712 MF 10GbE"
126255736Sdavidch    },
127255736Sdavidch#if 0
128255736Sdavidch    {
129255736Sdavidch        BRCM_VENDORID,
130255736Sdavidch        CHIP_NUM_57712_VF,
131255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
132265703Sdavidcs        "QLogic NetXtreme II BCM57712 VF 10GbE"
133255736Sdavidch    },
134255736Sdavidch#endif
135255736Sdavidch    {
136255736Sdavidch        BRCM_VENDORID,
137255736Sdavidch        CHIP_NUM_57800,
138255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
139265703Sdavidcs        "QLogic NetXtreme II BCM57800 10GbE"
140255736Sdavidch    },
141255736Sdavidch    {
142255736Sdavidch        BRCM_VENDORID,
143255736Sdavidch        CHIP_NUM_57800_MF,
144255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
145265703Sdavidcs        "QLogic NetXtreme II BCM57800 MF 10GbE"
146255736Sdavidch    },
147255736Sdavidch#if 0
148255736Sdavidch    {
149255736Sdavidch        BRCM_VENDORID,
150255736Sdavidch        CHIP_NUM_57800_VF,
151255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
152265703Sdavidcs        "QLogic NetXtreme II BCM57800 VF 10GbE"
153255736Sdavidch    },
154255736Sdavidch#endif
155255736Sdavidch    {
156255736Sdavidch        BRCM_VENDORID,
157255736Sdavidch        CHIP_NUM_57810,
158255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
159265703Sdavidcs        "QLogic NetXtreme II BCM57810 10GbE"
160255736Sdavidch    },
161255736Sdavidch    {
162255736Sdavidch        BRCM_VENDORID,
163255736Sdavidch        CHIP_NUM_57810_MF,
164255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
165265703Sdavidcs        "QLogic NetXtreme II BCM57810 MF 10GbE"
166255736Sdavidch    },
167255736Sdavidch#if 0
168255736Sdavidch    {
169255736Sdavidch        BRCM_VENDORID,
170255736Sdavidch        CHIP_NUM_57810_VF,
171255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
172265703Sdavidcs        "QLogic NetXtreme II BCM57810 VF 10GbE"
173255736Sdavidch    },
174255736Sdavidch#endif
175255736Sdavidch    {
176255736Sdavidch        BRCM_VENDORID,
177255736Sdavidch        CHIP_NUM_57811,
178255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
179265703Sdavidcs        "QLogic NetXtreme II BCM57811 10GbE"
180255736Sdavidch    },
181255736Sdavidch    {
182255736Sdavidch        BRCM_VENDORID,
183255736Sdavidch        CHIP_NUM_57811_MF,
184255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
185265703Sdavidcs        "QLogic NetXtreme II BCM57811 MF 10GbE"
186255736Sdavidch    },
187255736Sdavidch#if 0
188255736Sdavidch    {
189255736Sdavidch        BRCM_VENDORID,
190255736Sdavidch        CHIP_NUM_57811_VF,
191255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
192265703Sdavidcs        "QLogic NetXtreme II BCM57811 VF 10GbE"
193255736Sdavidch    },
194255736Sdavidch#endif
195255736Sdavidch    {
196255736Sdavidch        BRCM_VENDORID,
197255736Sdavidch        CHIP_NUM_57840_4_10,
198255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
199265703Sdavidcs        "QLogic NetXtreme II BCM57840 4x10GbE"
200255736Sdavidch    },
201255736Sdavidch#if 0
202255736Sdavidch    {
203255736Sdavidch        BRCM_VENDORID,
204255736Sdavidch        CHIP_NUM_57840_2_20,
205255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
206265703Sdavidcs        "QLogic NetXtreme II BCM57840 2x20GbE"
207255736Sdavidch    },
208255736Sdavidch#endif
209255736Sdavidch    {
210255736Sdavidch        BRCM_VENDORID,
211255736Sdavidch        CHIP_NUM_57840_MF,
212255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
213265703Sdavidcs        "QLogic NetXtreme II BCM57840 MF 10GbE"
214255736Sdavidch    },
215255736Sdavidch#if 0
216255736Sdavidch    {
217255736Sdavidch        BRCM_VENDORID,
218255736Sdavidch        CHIP_NUM_57840_VF,
219255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
220265703Sdavidcs        "QLogic NetXtreme II BCM57840 VF 10GbE"
221255736Sdavidch    },
222255736Sdavidch#endif
223255736Sdavidch    {
224255736Sdavidch        0, 0, 0, 0, NULL
225255736Sdavidch    }
226255736Sdavidch};
227255736Sdavidch
228255736SdavidchMALLOC_DECLARE(M_BXE_ILT);
229255736SdavidchMALLOC_DEFINE(M_BXE_ILT, "bxe_ilt", "bxe ILT pointer");
230255736Sdavidch
231255736Sdavidch/*
232255736Sdavidch * FreeBSD device entry points.
233255736Sdavidch */
234255736Sdavidchstatic int bxe_probe(device_t);
235255736Sdavidchstatic int bxe_attach(device_t);
236255736Sdavidchstatic int bxe_detach(device_t);
237255736Sdavidchstatic int bxe_shutdown(device_t);
238255736Sdavidch
239255736Sdavidch/*
240255736Sdavidch * FreeBSD KLD module/device interface event handler method.
241255736Sdavidch */
242255736Sdavidchstatic device_method_t bxe_methods[] = {
243255736Sdavidch    /* Device interface (device_if.h) */
244255736Sdavidch    DEVMETHOD(device_probe,     bxe_probe),
245255736Sdavidch    DEVMETHOD(device_attach,    bxe_attach),
246255736Sdavidch    DEVMETHOD(device_detach,    bxe_detach),
247255736Sdavidch    DEVMETHOD(device_shutdown,  bxe_shutdown),
248255736Sdavidch#if 0
249255736Sdavidch    DEVMETHOD(device_suspend,   bxe_suspend),
250255736Sdavidch    DEVMETHOD(device_resume,    bxe_resume),
251255736Sdavidch#endif
252255736Sdavidch    /* Bus interface (bus_if.h) */
253255736Sdavidch    DEVMETHOD(bus_print_child,  bus_generic_print_child),
254255736Sdavidch    DEVMETHOD(bus_driver_added, bus_generic_driver_added),
255255736Sdavidch    KOBJMETHOD_END
256255736Sdavidch};
257255736Sdavidch
258255736Sdavidch/*
259255736Sdavidch * FreeBSD KLD Module data declaration
260255736Sdavidch */
261255736Sdavidchstatic driver_t bxe_driver = {
262255736Sdavidch    "bxe",                   /* module name */
263255736Sdavidch    bxe_methods,             /* event handler */
264255736Sdavidch    sizeof(struct bxe_softc) /* extra data */
265255736Sdavidch};
266255736Sdavidch
267255736Sdavidch/*
268255736Sdavidch * FreeBSD dev class is needed to manage dev instances and
269255736Sdavidch * to associate with a bus type
270255736Sdavidch */
271255736Sdavidchstatic devclass_t bxe_devclass;
272255736Sdavidch
273255736SdavidchMODULE_DEPEND(bxe, pci, 1, 1, 1);
274255736SdavidchMODULE_DEPEND(bxe, ether, 1, 1, 1);
275255736SdavidchDRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
276255736Sdavidch
277255736Sdavidch/* resources needed for unloading a previously loaded device */
278255736Sdavidch
279255736Sdavidch#define BXE_PREV_WAIT_NEEDED 1
280255736Sdavidchstruct mtx bxe_prev_mtx;
281255736SdavidchMTX_SYSINIT(bxe_prev_mtx, &bxe_prev_mtx, "bxe_prev_lock", MTX_DEF);
282255736Sdavidchstruct bxe_prev_list_node {
283255736Sdavidch    LIST_ENTRY(bxe_prev_list_node) node;
284255736Sdavidch    uint8_t bus;
285255736Sdavidch    uint8_t slot;
286255736Sdavidch    uint8_t path;
287255736Sdavidch    uint8_t aer; /* XXX automatic error recovery */
288255736Sdavidch    uint8_t undi;
289255736Sdavidch};
290255736Sdavidchstatic LIST_HEAD(, bxe_prev_list_node) bxe_prev_list = LIST_HEAD_INITIALIZER(bxe_prev_list);
291255736Sdavidch
292255736Sdavidchstatic int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
293255736Sdavidch
294255736Sdavidch/* Tunable device values... */
295255736Sdavidch
296255736SdavidchSYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
297255736Sdavidch
298255736Sdavidch/* Debug */
299258187Sedavisunsigned long bxe_debug = 0;
300267992ShselaskySYSCTL_ULONG(_hw_bxe, OID_AUTO, debug, CTLFLAG_RDTUN,
301258187Sedavis             &bxe_debug, 0, "Debug logging mode");
302255736Sdavidch
303255736Sdavidch/* Interrupt Mode: 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
304255736Sdavidchstatic int bxe_interrupt_mode = INTR_MODE_MSIX;
305258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, interrupt_mode, CTLFLAG_RDTUN,
306258187Sedavis           &bxe_interrupt_mode, 0, "Interrupt (MSI-X/MSI/INTx) mode");
307255736Sdavidch
308255736Sdavidch/* Number of Queues: 0 (Auto) or 1 to 16 (fixed queue number) */
309255736Sdavidchstatic int bxe_queue_count = 4;
310258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN,
311258187Sedavis           &bxe_queue_count, 0, "Multi-Queue queue count");
312255736Sdavidch
313255736Sdavidch/* max number of buffers per queue (default RX_BD_USABLE) */
314258187Sedavisstatic int bxe_max_rx_bufs = 0;
315258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_rx_bufs, CTLFLAG_RDTUN,
316258187Sedavis           &bxe_max_rx_bufs, 0, "Maximum Number of Rx Buffers Per Queue");
317255736Sdavidch
318255736Sdavidch/* Host interrupt coalescing RX tick timer (usecs) */
319258187Sedavisstatic int bxe_hc_rx_ticks = 25;
320258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_rx_ticks, CTLFLAG_RDTUN,
321258187Sedavis           &bxe_hc_rx_ticks, 0, "Host Coalescing Rx ticks");
322255736Sdavidch
323255736Sdavidch/* Host interrupt coalescing TX tick timer (usecs) */
324258187Sedavisstatic int bxe_hc_tx_ticks = 50;
325258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_tx_ticks, CTLFLAG_RDTUN,
326258187Sedavis           &bxe_hc_tx_ticks, 0, "Host Coalescing Tx ticks");
327255736Sdavidch
328255736Sdavidch/* Maximum number of Rx packets to process at a time */
329258187Sedavisstatic int bxe_rx_budget = 0xffffffff;
330258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, rx_budget, CTLFLAG_TUN,
331258187Sedavis           &bxe_rx_budget, 0, "Rx processing budget");
332255736Sdavidch
333255736Sdavidch/* Maximum LRO aggregation size */
334258187Sedavisstatic int bxe_max_aggregation_size = 0;
335258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_aggregation_size, CTLFLAG_TUN,
336258187Sedavis           &bxe_max_aggregation_size, 0, "max aggregation size");
337255736Sdavidch
338255736Sdavidch/* PCI MRRS: -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB) */
339255736Sdavidchstatic int bxe_mrrs = -1;
340258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN,
341258187Sedavis           &bxe_mrrs, 0, "PCIe maximum read request size");
342255736Sdavidch
343255736Sdavidch/* AutoGrEEEn: 0 (hardware default), 1 (force on), 2 (force off) */
344255736Sdavidchstatic int bxe_autogreeen = 0;
345258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, autogreeen, CTLFLAG_RDTUN,
346258187Sedavis           &bxe_autogreeen, 0, "AutoGrEEEn support");
347255736Sdavidch
348255736Sdavidch/* 4-tuple RSS support for UDP: 0 (disabled), 1 (enabled) */
349255736Sdavidchstatic int bxe_udp_rss = 0;
350258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, udp_rss, CTLFLAG_RDTUN,
351258187Sedavis           &bxe_udp_rss, 0, "UDP RSS support");
352255736Sdavidch
353255736Sdavidch
354255736Sdavidch#define STAT_NAME_LEN 32 /* no stat names below can be longer than this */
355255736Sdavidch
356255736Sdavidch#define STATS_OFFSET32(stat_name)                   \
357255736Sdavidch    (offsetof(struct bxe_eth_stats, stat_name) / 4)
358255736Sdavidch
359255736Sdavidch#define Q_STATS_OFFSET32(stat_name)                   \
360255736Sdavidch    (offsetof(struct bxe_eth_q_stats, stat_name) / 4)
361255736Sdavidch
362255736Sdavidchstatic const struct {
363255736Sdavidch    uint32_t offset;
364255736Sdavidch    uint32_t size;
365255736Sdavidch    uint32_t flags;
366255736Sdavidch#define STATS_FLAGS_PORT  1
367255736Sdavidch#define STATS_FLAGS_FUNC  2 /* MF only cares about function stats */
368255736Sdavidch#define STATS_FLAGS_BOTH  (STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
369255736Sdavidch    char string[STAT_NAME_LEN];
370255736Sdavidch} bxe_eth_stats_arr[] = {
371255736Sdavidch    { STATS_OFFSET32(total_bytes_received_hi),
372255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bytes" },
373255736Sdavidch    { STATS_OFFSET32(error_bytes_received_hi),
374255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_error_bytes" },
375255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_received_hi),
376255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
377255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_received_hi),
378255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
379255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_received_hi),
380255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
381255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
382255736Sdavidch                8, STATS_FLAGS_PORT, "rx_crc_errors" },
383255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
384255736Sdavidch                8, STATS_FLAGS_PORT, "rx_align_errors" },
385255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
386255736Sdavidch                8, STATS_FLAGS_PORT, "rx_undersize_packets" },
387255736Sdavidch    { STATS_OFFSET32(etherstatsoverrsizepkts_hi),
388255736Sdavidch                8, STATS_FLAGS_PORT, "rx_oversize_packets" },
389255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
390255736Sdavidch                8, STATS_FLAGS_PORT, "rx_fragments" },
391255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
392255736Sdavidch                8, STATS_FLAGS_PORT, "rx_jabbers" },
393255736Sdavidch    { STATS_OFFSET32(no_buff_discard_hi),
394255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_discards" },
395255736Sdavidch    { STATS_OFFSET32(mac_filter_discard),
396255736Sdavidch                4, STATS_FLAGS_PORT, "rx_filtered_packets" },
397255736Sdavidch    { STATS_OFFSET32(mf_tag_discard),
398255736Sdavidch                4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
399255736Sdavidch    { STATS_OFFSET32(pfc_frames_received_hi),
400255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_received" },
401255736Sdavidch    { STATS_OFFSET32(pfc_frames_sent_hi),
402255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_sent" },
403255736Sdavidch    { STATS_OFFSET32(brb_drop_hi),
404255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_discard" },
405255736Sdavidch    { STATS_OFFSET32(brb_truncate_hi),
406255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_truncate" },
407255736Sdavidch    { STATS_OFFSET32(pause_frames_received_hi),
408255736Sdavidch                8, STATS_FLAGS_PORT, "rx_pause_frames" },
409255736Sdavidch    { STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
410255736Sdavidch                8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
411255736Sdavidch    { STATS_OFFSET32(nig_timer_max),
412255736Sdavidch                4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
413255736Sdavidch    { STATS_OFFSET32(total_bytes_transmitted_hi),
414255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bytes" },
415255736Sdavidch    { STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
416255736Sdavidch                8, STATS_FLAGS_PORT, "tx_error_bytes" },
417255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_transmitted_hi),
418255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
419255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_transmitted_hi),
420255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
421255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
422255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
423255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
424255736Sdavidch                8, STATS_FLAGS_PORT, "tx_mac_errors" },
425255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
426255736Sdavidch                8, STATS_FLAGS_PORT, "tx_carrier_errors" },
427255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
428255736Sdavidch                8, STATS_FLAGS_PORT, "tx_single_collisions" },
429255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
430255736Sdavidch                8, STATS_FLAGS_PORT, "tx_multi_collisions" },
431255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
432255736Sdavidch                8, STATS_FLAGS_PORT, "tx_deferred" },
433255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
434255736Sdavidch                8, STATS_FLAGS_PORT, "tx_excess_collisions" },
435255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
436255736Sdavidch                8, STATS_FLAGS_PORT, "tx_late_collisions" },
437255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
438255736Sdavidch                8, STATS_FLAGS_PORT, "tx_total_collisions" },
439255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
440255736Sdavidch                8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
441255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
442255736Sdavidch                8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
443255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
444255736Sdavidch                8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
445255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
446255736Sdavidch                8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
447255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
448255736Sdavidch                8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
449255736Sdavidch    { STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
450255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
451255736Sdavidch    { STATS_OFFSET32(etherstatspktsover1522octets_hi),
452255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
453255736Sdavidch    { STATS_OFFSET32(pause_frames_sent_hi),
454255736Sdavidch                8, STATS_FLAGS_PORT, "tx_pause_frames" },
455255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregations_hi),
456255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregations" },
457255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregated_frames_hi),
458255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
459255736Sdavidch    { STATS_OFFSET32(total_tpa_bytes_hi),
460255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_bytes"},
461255736Sdavidch#if 0
462255736Sdavidch    { STATS_OFFSET32(recoverable_error),
463255736Sdavidch                4, STATS_FLAGS_FUNC, "recoverable_errors" },
464255736Sdavidch    { STATS_OFFSET32(unrecoverable_error),
465255736Sdavidch                4, STATS_FLAGS_FUNC, "unrecoverable_errors" },
466255736Sdavidch#endif
467255736Sdavidch    { STATS_OFFSET32(eee_tx_lpi),
468255736Sdavidch                4, STATS_FLAGS_PORT, "eee_tx_lpi"},
469255736Sdavidch    { STATS_OFFSET32(rx_calls),
470255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_calls"},
471255736Sdavidch    { STATS_OFFSET32(rx_pkts),
472255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_pkts"},
473255736Sdavidch    { STATS_OFFSET32(rx_tpa_pkts),
474255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_tpa_pkts"},
475283274Sdavidcs    { STATS_OFFSET32(rx_jumbo_sge_pkts),
476283274Sdavidcs                4, STATS_FLAGS_FUNC, "rx_jumbo_sge_pkts"},
477255736Sdavidch    { STATS_OFFSET32(rx_soft_errors),
478255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_soft_errors"},
479255736Sdavidch    { STATS_OFFSET32(rx_hw_csum_errors),
480255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_hw_csum_errors"},
481255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_ip),
482255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_ip"},
483255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
484255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_tcp_udp"},
485255736Sdavidch    { STATS_OFFSET32(rx_budget_reached),
486255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_budget_reached"},
487255736Sdavidch    { STATS_OFFSET32(tx_pkts),
488255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_pkts"},
489255736Sdavidch    { STATS_OFFSET32(tx_soft_errors),
490255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_soft_errors"},
491255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_ip),
492255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_ip"},
493255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_tcp),
494255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_tcp"},
495255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_udp),
496255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_udp"},
497255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso),
498255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso"},
499255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
500255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso_hdr_splits"},
501255736Sdavidch    { STATS_OFFSET32(tx_encap_failures),
502255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_encap_failures"},
503255736Sdavidch    { STATS_OFFSET32(tx_hw_queue_full),
504255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_queue_full"},
505255736Sdavidch    { STATS_OFFSET32(tx_hw_max_queue_depth),
506255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_max_queue_depth"},
507255736Sdavidch    { STATS_OFFSET32(tx_dma_mapping_failure),
508255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_dma_mapping_failure"},
509255736Sdavidch    { STATS_OFFSET32(tx_max_drbr_queue_depth),
510255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_max_drbr_queue_depth"},
511255736Sdavidch    { STATS_OFFSET32(tx_window_violation_std),
512255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_std"},
513255736Sdavidch    { STATS_OFFSET32(tx_window_violation_tso),
514255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_tso"},
515255736Sdavidch#if 0
516255736Sdavidch    { STATS_OFFSET32(tx_unsupported_tso_request_ipv6),
517255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_unsupported_tso_request_ipv6"},
518255736Sdavidch    { STATS_OFFSET32(tx_unsupported_tso_request_not_tcp),
519255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_unsupported_tso_request_not_tcp"},
520255736Sdavidch#endif
521255736Sdavidch    { STATS_OFFSET32(tx_chain_lost_mbuf),
522255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_chain_lost_mbuf"},
523255736Sdavidch    { STATS_OFFSET32(tx_frames_deferred),
524255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_frames_deferred"},
525255736Sdavidch    { STATS_OFFSET32(tx_queue_xoff),
526255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_queue_xoff"},
527255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_attempts),
528255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_attempts"},
529255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_failures),
530255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_failures"},
531255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
532255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_alloc_failed"},
533255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
534255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_mapping_failed"},
535255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
536255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_alloc_failed"},
537255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
538255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_mapping_failed"},
539255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
540255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_alloc_failed"},
541255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
542255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_mapping_failed"},
543255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tx),
544255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_tx"},
545255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_rx),
546255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_rx"},
547255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_sge),
548255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_sge"},
549255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tpa),
550255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"}
551255736Sdavidch};
552255736Sdavidch
553255736Sdavidchstatic const struct {
554255736Sdavidch    uint32_t offset;
555255736Sdavidch    uint32_t size;
556255736Sdavidch    char string[STAT_NAME_LEN];
557255736Sdavidch} bxe_eth_q_stats_arr[] = {
558255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_received_hi),
559255736Sdavidch                8, "rx_bytes" },
560255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
561255736Sdavidch                8, "rx_ucast_packets" },
562255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
563255736Sdavidch                8, "rx_mcast_packets" },
564255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
565255736Sdavidch                8, "rx_bcast_packets" },
566255736Sdavidch    { Q_STATS_OFFSET32(no_buff_discard_hi),
567255736Sdavidch                8, "rx_discards" },
568255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
569255736Sdavidch                8, "tx_bytes" },
570255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
571255736Sdavidch                8, "tx_ucast_packets" },
572255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
573255736Sdavidch                8, "tx_mcast_packets" },
574255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
575255736Sdavidch                8, "tx_bcast_packets" },
576255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
577255736Sdavidch                8, "tpa_aggregations" },
578255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
579255736Sdavidch                8, "tpa_aggregated_frames"},
580255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_bytes_hi),
581255736Sdavidch                8, "tpa_bytes"},
582255736Sdavidch    { Q_STATS_OFFSET32(rx_calls),
583255736Sdavidch                4, "rx_calls"},
584255736Sdavidch    { Q_STATS_OFFSET32(rx_pkts),
585255736Sdavidch                4, "rx_pkts"},
586255736Sdavidch    { Q_STATS_OFFSET32(rx_tpa_pkts),
587255736Sdavidch                4, "rx_tpa_pkts"},
588283274Sdavidcs    { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
589283274Sdavidcs                4, "rx_jumbo_sge_pkts"},
590255736Sdavidch    { Q_STATS_OFFSET32(rx_soft_errors),
591255736Sdavidch                4, "rx_soft_errors"},
592255736Sdavidch    { Q_STATS_OFFSET32(rx_hw_csum_errors),
593255736Sdavidch                4, "rx_hw_csum_errors"},
594255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
595255736Sdavidch                4, "rx_ofld_frames_csum_ip"},
596255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
597255736Sdavidch                4, "rx_ofld_frames_csum_tcp_udp"},
598255736Sdavidch    { Q_STATS_OFFSET32(rx_budget_reached),
599255736Sdavidch                4, "rx_budget_reached"},
600255736Sdavidch    { Q_STATS_OFFSET32(tx_pkts),
601255736Sdavidch                4, "tx_pkts"},
602255736Sdavidch    { Q_STATS_OFFSET32(tx_soft_errors),
603255736Sdavidch                4, "tx_soft_errors"},
604255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
605255736Sdavidch                4, "tx_ofld_frames_csum_ip"},
606255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
607255736Sdavidch                4, "tx_ofld_frames_csum_tcp"},
608255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
609255736Sdavidch                4, "tx_ofld_frames_csum_udp"},
610255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso),
611255736Sdavidch                4, "tx_ofld_frames_lso"},
612255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
613255736Sdavidch                4, "tx_ofld_frames_lso_hdr_splits"},
614255736Sdavidch    { Q_STATS_OFFSET32(tx_encap_failures),
615255736Sdavidch                4, "tx_encap_failures"},
616255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_queue_full),
617255736Sdavidch                4, "tx_hw_queue_full"},
618255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
619255736Sdavidch                4, "tx_hw_max_queue_depth"},
620255736Sdavidch    { Q_STATS_OFFSET32(tx_dma_mapping_failure),
621255736Sdavidch                4, "tx_dma_mapping_failure"},
622255736Sdavidch    { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
623255736Sdavidch                4, "tx_max_drbr_queue_depth"},
624255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_std),
625255736Sdavidch                4, "tx_window_violation_std"},
626255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_tso),
627255736Sdavidch                4, "tx_window_violation_tso"},
628255736Sdavidch#if 0
629255736Sdavidch    { Q_STATS_OFFSET32(tx_unsupported_tso_request_ipv6),
630255736Sdavidch                4, "tx_unsupported_tso_request_ipv6"},
631255736Sdavidch    { Q_STATS_OFFSET32(tx_unsupported_tso_request_not_tcp),
632255736Sdavidch                4, "tx_unsupported_tso_request_not_tcp"},
633255736Sdavidch#endif
634255736Sdavidch    { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
635255736Sdavidch                4, "tx_chain_lost_mbuf"},
636255736Sdavidch    { Q_STATS_OFFSET32(tx_frames_deferred),
637255736Sdavidch                4, "tx_frames_deferred"},
638255736Sdavidch    { Q_STATS_OFFSET32(tx_queue_xoff),
639255736Sdavidch                4, "tx_queue_xoff"},
640255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_attempts),
641255736Sdavidch                4, "mbuf_defrag_attempts"},
642255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_failures),
643255736Sdavidch                4, "mbuf_defrag_failures"},
644255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
645255736Sdavidch                4, "mbuf_rx_bd_alloc_failed"},
646255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
647255736Sdavidch                4, "mbuf_rx_bd_mapping_failed"},
648255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
649255736Sdavidch                4, "mbuf_rx_tpa_alloc_failed"},
650255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
651255736Sdavidch                4, "mbuf_rx_tpa_mapping_failed"},
652255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
653255736Sdavidch                4, "mbuf_rx_sge_alloc_failed"},
654255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
655255736Sdavidch                4, "mbuf_rx_sge_mapping_failed"},
656255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tx),
657255736Sdavidch                4, "mbuf_alloc_tx"},
658255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_rx),
659255736Sdavidch                4, "mbuf_alloc_rx"},
660255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_sge),
661255736Sdavidch                4, "mbuf_alloc_sge"},
662255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tpa),
663255736Sdavidch                4, "mbuf_alloc_tpa"}
664255736Sdavidch};
665255736Sdavidch
666255736Sdavidch#define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
667255736Sdavidch#define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
668255736Sdavidch
669255736Sdavidch
670255736Sdavidchstatic void    bxe_cmng_fns_init(struct bxe_softc *sc,
671255736Sdavidch                                 uint8_t          read_cfg,
672255736Sdavidch                                 uint8_t          cmng_type);
673255736Sdavidchstatic int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
674255736Sdavidchstatic void    storm_memset_cmng(struct bxe_softc *sc,
675255736Sdavidch                                 struct cmng_init *cmng,
676255736Sdavidch                                 uint8_t          port);
677255736Sdavidchstatic void    bxe_set_reset_global(struct bxe_softc *sc);
678255736Sdavidchstatic void    bxe_set_reset_in_progress(struct bxe_softc *sc);
679255736Sdavidchstatic uint8_t bxe_reset_is_done(struct bxe_softc *sc,
680255736Sdavidch                                 int              engine);
681255736Sdavidchstatic uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
682255736Sdavidchstatic uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
683255736Sdavidch                                   uint8_t          *global,
684255736Sdavidch                                   uint8_t          print);
685255736Sdavidchstatic void    bxe_int_disable(struct bxe_softc *sc);
686255736Sdavidchstatic int     bxe_release_leader_lock(struct bxe_softc *sc);
687255736Sdavidchstatic void    bxe_pf_disable(struct bxe_softc *sc);
688255736Sdavidchstatic void    bxe_free_fp_buffers(struct bxe_softc *sc);
689255736Sdavidchstatic inline void bxe_update_rx_prod(struct bxe_softc    *sc,
690255736Sdavidch                                      struct bxe_fastpath *fp,
691255736Sdavidch                                      uint16_t            rx_bd_prod,
692255736Sdavidch                                      uint16_t            rx_cq_prod,
693255736Sdavidch                                      uint16_t            rx_sge_prod);
694255736Sdavidchstatic void    bxe_link_report_locked(struct bxe_softc *sc);
695255736Sdavidchstatic void    bxe_link_report(struct bxe_softc *sc);
696255736Sdavidchstatic void    bxe_link_status_update(struct bxe_softc *sc);
697255736Sdavidchstatic void    bxe_periodic_callout_func(void *xsc);
698255736Sdavidchstatic void    bxe_periodic_start(struct bxe_softc *sc);
699255736Sdavidchstatic void    bxe_periodic_stop(struct bxe_softc *sc);
700255736Sdavidchstatic int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
701255736Sdavidch                                    uint16_t prev_index,
702255736Sdavidch                                    uint16_t index);
703255736Sdavidchstatic int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
704255736Sdavidch                                     int                 queue);
705255736Sdavidchstatic int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
706255736Sdavidch                                     uint16_t            index);
707255736Sdavidchstatic uint8_t bxe_txeof(struct bxe_softc *sc,
708255736Sdavidch                         struct bxe_fastpath *fp);
709255736Sdavidchstatic void    bxe_task_fp(struct bxe_fastpath *fp);
710255736Sdavidchstatic __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
711255736Sdavidch                                     struct mbuf      *m,
712255736Sdavidch                                     uint8_t          contents);
713255736Sdavidchstatic int     bxe_alloc_mem(struct bxe_softc *sc);
714255736Sdavidchstatic void    bxe_free_mem(struct bxe_softc *sc);
715255736Sdavidchstatic int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
716255736Sdavidchstatic void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
717255736Sdavidchstatic int     bxe_interrupt_attach(struct bxe_softc *sc);
718255736Sdavidchstatic void    bxe_interrupt_detach(struct bxe_softc *sc);
719255736Sdavidchstatic void    bxe_set_rx_mode(struct bxe_softc *sc);
720255736Sdavidchstatic int     bxe_init_locked(struct bxe_softc *sc);
721255736Sdavidchstatic int     bxe_stop_locked(struct bxe_softc *sc);
722255736Sdavidchstatic __noinline int bxe_nic_load(struct bxe_softc *sc,
723255736Sdavidch                                   int              load_mode);
724255736Sdavidchstatic __noinline int bxe_nic_unload(struct bxe_softc *sc,
725255736Sdavidch                                     uint32_t         unload_mode,
726255736Sdavidch                                     uint8_t          keep_link);
727255736Sdavidch
728255736Sdavidchstatic void bxe_handle_sp_tq(void *context, int pending);
729255736Sdavidchstatic void bxe_handle_fp_tq(void *context, int pending);
730255736Sdavidch
731255736Sdavidch
732255736Sdavidch/* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
733255736Sdavidchuint32_t
734255736Sdavidchcalc_crc32(uint8_t  *crc32_packet,
735255736Sdavidch           uint32_t crc32_length,
736255736Sdavidch           uint32_t crc32_seed,
737255736Sdavidch           uint8_t  complement)
738255736Sdavidch{
739255736Sdavidch   uint32_t byte         = 0;
740255736Sdavidch   uint32_t bit          = 0;
741255736Sdavidch   uint8_t  msb          = 0;
742255736Sdavidch   uint32_t temp         = 0;
743255736Sdavidch   uint32_t shft         = 0;
744255736Sdavidch   uint8_t  current_byte = 0;
745255736Sdavidch   uint32_t crc32_result = crc32_seed;
746255736Sdavidch   const uint32_t CRC32_POLY = 0x1edc6f41;
747255736Sdavidch
748255736Sdavidch   if ((crc32_packet == NULL) ||
749255736Sdavidch       (crc32_length == 0) ||
750255736Sdavidch       ((crc32_length % 8) != 0))
751255736Sdavidch    {
752255736Sdavidch        return (crc32_result);
753255736Sdavidch    }
754255736Sdavidch
755255736Sdavidch    for (byte = 0; byte < crc32_length; byte = byte + 1)
756255736Sdavidch    {
757255736Sdavidch        current_byte = crc32_packet[byte];
758255736Sdavidch        for (bit = 0; bit < 8; bit = bit + 1)
759255736Sdavidch        {
760255736Sdavidch            /* msb = crc32_result[31]; */
761255736Sdavidch            msb = (uint8_t)(crc32_result >> 31);
762255736Sdavidch
763255736Sdavidch            crc32_result = crc32_result << 1;
764255736Sdavidch
765255736Sdavidch            /* it (msb != current_byte[bit]) */
766255736Sdavidch            if (msb != (0x1 & (current_byte >> bit)))
767255736Sdavidch            {
768255736Sdavidch                crc32_result = crc32_result ^ CRC32_POLY;
769255736Sdavidch                /* crc32_result[0] = 1 */
770255736Sdavidch                crc32_result |= 1;
771255736Sdavidch            }
772255736Sdavidch        }
773255736Sdavidch    }
774255736Sdavidch
775255736Sdavidch    /* Last step is to:
776255736Sdavidch     * 1. "mirror" every bit
777255736Sdavidch     * 2. swap the 4 bytes
778255736Sdavidch     * 3. complement each bit
779255736Sdavidch     */
780255736Sdavidch
781255736Sdavidch    /* Mirror */
782255736Sdavidch    temp = crc32_result;
783255736Sdavidch    shft = sizeof(crc32_result) * 8 - 1;
784255736Sdavidch
785255736Sdavidch    for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
786255736Sdavidch    {
787255736Sdavidch        temp <<= 1;
788255736Sdavidch        temp |= crc32_result & 1;
789255736Sdavidch        shft-- ;
790255736Sdavidch    }
791255736Sdavidch
792255736Sdavidch    /* temp[31-bit] = crc32_result[bit] */
793255736Sdavidch    temp <<= shft;
794255736Sdavidch
795255736Sdavidch    /* Swap */
796255736Sdavidch    /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
797255736Sdavidch    {
798255736Sdavidch        uint32_t t0, t1, t2, t3;
799255736Sdavidch        t0 = (0x000000ff & (temp >> 24));
800255736Sdavidch        t1 = (0x0000ff00 & (temp >> 8));
801255736Sdavidch        t2 = (0x00ff0000 & (temp << 8));
802255736Sdavidch        t3 = (0xff000000 & (temp << 24));
803255736Sdavidch        crc32_result = t0 | t1 | t2 | t3;
804255736Sdavidch    }
805255736Sdavidch
806255736Sdavidch    /* Complement */
807255736Sdavidch    if (complement)
808255736Sdavidch    {
809255736Sdavidch        crc32_result = ~crc32_result;
810255736Sdavidch    }
811255736Sdavidch
812255736Sdavidch    return (crc32_result);
813255736Sdavidch}
814255736Sdavidch
815255736Sdavidchint
816255736Sdavidchbxe_test_bit(int                    nr,
817255736Sdavidch             volatile unsigned long *addr)
818255736Sdavidch{
819255736Sdavidch    return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
820255736Sdavidch}
821255736Sdavidch
822255736Sdavidchvoid
823255736Sdavidchbxe_set_bit(unsigned int           nr,
824255736Sdavidch            volatile unsigned long *addr)
825255736Sdavidch{
826255736Sdavidch    atomic_set_acq_long(addr, (1 << nr));
827255736Sdavidch}
828255736Sdavidch
829255736Sdavidchvoid
830255736Sdavidchbxe_clear_bit(int                    nr,
831255736Sdavidch              volatile unsigned long *addr)
832255736Sdavidch{
833255736Sdavidch    atomic_clear_acq_long(addr, (1 << nr));
834255736Sdavidch}
835255736Sdavidch
836255736Sdavidchint
837255736Sdavidchbxe_test_and_set_bit(int                    nr,
838255736Sdavidch                       volatile unsigned long *addr)
839255736Sdavidch{
840255736Sdavidch    unsigned long x;
841255736Sdavidch    nr = (1 << nr);
842255736Sdavidch    do {
843255736Sdavidch        x = *addr;
844255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
845255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
846255736Sdavidch    return (x & nr);
847255736Sdavidch}
848255736Sdavidch
849255736Sdavidchint
850255736Sdavidchbxe_test_and_clear_bit(int                    nr,
851255736Sdavidch                       volatile unsigned long *addr)
852255736Sdavidch{
853255736Sdavidch    unsigned long x;
854255736Sdavidch    nr = (1 << nr);
855255736Sdavidch    do {
856255736Sdavidch        x = *addr;
857255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
858255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
859255736Sdavidch    return (x & nr);
860255736Sdavidch}
861255736Sdavidch
862255736Sdavidchint
863255736Sdavidchbxe_cmpxchg(volatile int *addr,
864255736Sdavidch            int          old,
865255736Sdavidch            int          new)
866255736Sdavidch{
867255736Sdavidch    int x;
868255736Sdavidch    do {
869255736Sdavidch        x = *addr;
870255736Sdavidch    } while (atomic_cmpset_acq_int(addr, old, new) == 0);
871255736Sdavidch    return (x);
872255736Sdavidch}
873255736Sdavidch
874255736Sdavidch/*
875255736Sdavidch * Get DMA memory from the OS.
876255736Sdavidch *
877255736Sdavidch * Validates that the OS has provided DMA buffers in response to a
878255736Sdavidch * bus_dmamap_load call and saves the physical address of those buffers.
879255736Sdavidch * When the callback is used the OS will return 0 for the mapping function
880255736Sdavidch * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
881255736Sdavidch * failures back to the caller.
882255736Sdavidch *
883255736Sdavidch * Returns:
884255736Sdavidch *   Nothing.
885255736Sdavidch */
886255736Sdavidchstatic void
887255736Sdavidchbxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
888255736Sdavidch{
889255736Sdavidch    struct bxe_dma *dma = arg;
890255736Sdavidch
891255736Sdavidch    if (error) {
892255736Sdavidch        dma->paddr = 0;
893255736Sdavidch        dma->nseg  = 0;
894255736Sdavidch        BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
895255736Sdavidch    } else {
896255736Sdavidch        dma->paddr = segs->ds_addr;
897255736Sdavidch        dma->nseg  = nseg;
898255736Sdavidch#if 0
899262999Sedavis        BLOGD(dma->sc, DBG_LOAD,
900255736Sdavidch              "DMA alloc '%s': vaddr=%p paddr=%p nseg=%d size=%lu\n",
901255736Sdavidch              dma->msg, dma->vaddr, (void *)dma->paddr,
902255736Sdavidch              dma->nseg, dma->size);
903255736Sdavidch#endif
904255736Sdavidch    }
905255736Sdavidch}
906255736Sdavidch
907255736Sdavidch/*
908255736Sdavidch * Allocate a block of memory and map it for DMA. No partial completions
909255736Sdavidch * allowed and release any resources acquired if we can't acquire all
910255736Sdavidch * resources.
911255736Sdavidch *
912255736Sdavidch * Returns:
913255736Sdavidch *   0 = Success, !0 = Failure
914255736Sdavidch */
915255736Sdavidchint
916255736Sdavidchbxe_dma_alloc(struct bxe_softc *sc,
917255736Sdavidch              bus_size_t       size,
918255736Sdavidch              struct bxe_dma   *dma,
919255736Sdavidch              const char       *msg)
920255736Sdavidch{
921255736Sdavidch    int rc;
922255736Sdavidch
923255736Sdavidch    if (dma->size > 0) {
924256299Sedavis        BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
925256299Sedavis              (unsigned long)dma->size);
926255736Sdavidch        return (1);
927255736Sdavidch    }
928255736Sdavidch
929255736Sdavidch    memset(dma, 0, sizeof(*dma)); /* sanity */
930255736Sdavidch    dma->sc   = sc;
931255736Sdavidch    dma->size = size;
932255736Sdavidch    snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
933255736Sdavidch
934255736Sdavidch    rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
935255736Sdavidch                            BCM_PAGE_SIZE,      /* alignment */
936255736Sdavidch                            0,                  /* boundary limit */
937255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted low */
938255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted hi */
939255736Sdavidch                            NULL,               /* addr filter() */
940255736Sdavidch                            NULL,               /* addr filter() arg */
941255736Sdavidch                            size,               /* max map size */
942255736Sdavidch                            1,                  /* num discontinuous */
943255736Sdavidch                            size,               /* max seg size */
944255736Sdavidch                            BUS_DMA_ALLOCNOW,   /* flags */
945255736Sdavidch                            NULL,               /* lock() */
946255736Sdavidch                            NULL,               /* lock() arg */
947255736Sdavidch                            &dma->tag);         /* returned dma tag */
948255736Sdavidch    if (rc != 0) {
949255736Sdavidch        BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
950255736Sdavidch        memset(dma, 0, sizeof(*dma));
951255736Sdavidch        return (1);
952255736Sdavidch    }
953255736Sdavidch
954255736Sdavidch    rc = bus_dmamem_alloc(dma->tag,
955255736Sdavidch                          (void **)&dma->vaddr,
956255736Sdavidch                          (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
957255736Sdavidch                          &dma->map);
958255736Sdavidch    if (rc != 0) {
959255736Sdavidch        BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
960255736Sdavidch        bus_dma_tag_destroy(dma->tag);
961255736Sdavidch        memset(dma, 0, sizeof(*dma));
962255736Sdavidch        return (1);
963255736Sdavidch    }
964255736Sdavidch
965255736Sdavidch    rc = bus_dmamap_load(dma->tag,
966255736Sdavidch                         dma->map,
967255736Sdavidch                         dma->vaddr,
968255736Sdavidch                         size,
969255736Sdavidch                         bxe_dma_map_addr, /* BLOGD in here */
970255736Sdavidch                         dma,
971255736Sdavidch                         BUS_DMA_NOWAIT);
972255736Sdavidch    if (rc != 0) {
973255736Sdavidch        BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
974255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
975255736Sdavidch        bus_dma_tag_destroy(dma->tag);
976255736Sdavidch        memset(dma, 0, sizeof(*dma));
977255736Sdavidch        return (1);
978255736Sdavidch    }
979255736Sdavidch
980255736Sdavidch    return (0);
981255736Sdavidch}
982255736Sdavidch
983255736Sdavidchvoid
984255736Sdavidchbxe_dma_free(struct bxe_softc *sc,
985255736Sdavidch             struct bxe_dma   *dma)
986255736Sdavidch{
987255736Sdavidch    if (dma->size > 0) {
988255736Sdavidch#if 0
989255736Sdavidch        BLOGD(sc, DBG_LOAD,
990255736Sdavidch              "DMA free '%s': vaddr=%p paddr=%p nseg=%d size=%lu\n",
991255736Sdavidch              dma->msg, dma->vaddr, (void *)dma->paddr,
992255736Sdavidch              dma->nseg, dma->size);
993255736Sdavidch#endif
994255736Sdavidch
995255736Sdavidch        DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
996255736Sdavidch
997255736Sdavidch        bus_dmamap_sync(dma->tag, dma->map,
998255736Sdavidch                        (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
999255736Sdavidch        bus_dmamap_unload(dma->tag, dma->map);
1000255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
1001255736Sdavidch        bus_dma_tag_destroy(dma->tag);
1002255736Sdavidch    }
1003255736Sdavidch
1004255736Sdavidch    memset(dma, 0, sizeof(*dma));
1005255736Sdavidch}
1006255736Sdavidch
1007255736Sdavidch/*
1008255736Sdavidch * These indirect read and write routines are only during init.
1009255736Sdavidch * The locking is handled by the MCP.
1010255736Sdavidch */
1011255736Sdavidch
1012255736Sdavidchvoid
1013255736Sdavidchbxe_reg_wr_ind(struct bxe_softc *sc,
1014255736Sdavidch               uint32_t         addr,
1015255736Sdavidch               uint32_t         val)
1016255736Sdavidch{
1017255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
1018255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
1019255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
1020255736Sdavidch}
1021255736Sdavidch
1022255736Sdavidchuint32_t
1023255736Sdavidchbxe_reg_rd_ind(struct bxe_softc *sc,
1024255736Sdavidch               uint32_t         addr)
1025255736Sdavidch{
1026255736Sdavidch    uint32_t val;
1027255736Sdavidch
1028255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
1029255736Sdavidch    val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
1030255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
1031255736Sdavidch
1032255736Sdavidch    return (val);
1033255736Sdavidch}
1034255736Sdavidch
1035255736Sdavidch#if 0
1036255736Sdavidchvoid bxe_dp_dmae(struct bxe_softc *sc, struct dmae_command *dmae, int msglvl)
1037255736Sdavidch{
1038255736Sdavidch    uint32_t src_type = dmae->opcode & DMAE_COMMAND_SRC;
1039255736Sdavidch
1040255736Sdavidch    switch (dmae->opcode & DMAE_COMMAND_DST) {
1041255736Sdavidch    case DMAE_CMD_DST_PCI:
1042255736Sdavidch        if (src_type == DMAE_CMD_SRC_PCI)
1043255736Sdavidch            DP(msglvl, "DMAE: opcode 0x%08x\n"
1044255736Sdavidch               "src [%x:%08x], len [%d*4], dst [%x:%08x]\n"
1045255736Sdavidch               "comp_addr [%x:%08x], comp_val 0x%08x\n",
1046255736Sdavidch               dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
1047255736Sdavidch               dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
1048255736Sdavidch               dmae->comp_addr_hi, dmae->comp_addr_lo,
1049255736Sdavidch               dmae->comp_val);
1050255736Sdavidch        else
1051255736Sdavidch            DP(msglvl, "DMAE: opcode 0x%08x\n"
1052255736Sdavidch               "src [%08x], len [%d*4], dst [%x:%08x]\n"
1053255736Sdavidch               "comp_addr [%x:%08x], comp_val 0x%08x\n",
1054255736Sdavidch               dmae->opcode, dmae->src_addr_lo >> 2,
1055255736Sdavidch               dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
1056255736Sdavidch               dmae->comp_addr_hi, dmae->comp_addr_lo,
1057255736Sdavidch               dmae->comp_val);
1058255736Sdavidch        break;
1059255736Sdavidch    case DMAE_CMD_DST_GRC:
1060255736Sdavidch        if (src_type == DMAE_CMD_SRC_PCI)
1061255736Sdavidch            DP(msglvl, "DMAE: opcode 0x%08x\n"
1062255736Sdavidch               "src [%x:%08x], len [%d*4], dst_addr [%08x]\n"
1063255736Sdavidch               "comp_addr [%x:%08x], comp_val 0x%08x\n",
1064255736Sdavidch               dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
1065255736Sdavidch               dmae->len, dmae->dst_addr_lo >> 2,
1066255736Sdavidch               dmae->comp_addr_hi, dmae->comp_addr_lo,
1067255736Sdavidch               dmae->comp_val);
1068255736Sdavidch        else
1069255736Sdavidch            DP(msglvl, "DMAE: opcode 0x%08x\n"
1070255736Sdavidch               "src [%08x], len [%d*4], dst [%08x]\n"
1071255736Sdavidch               "comp_addr [%x:%08x], comp_val 0x%08x\n",
1072255736Sdavidch               dmae->opcode, dmae->src_addr_lo >> 2,
1073255736Sdavidch               dmae->len, dmae->dst_addr_lo >> 2,
1074255736Sdavidch               dmae->comp_addr_hi, dmae->comp_addr_lo,
1075255736Sdavidch               dmae->comp_val);
1076255736Sdavidch        break;
1077255736Sdavidch    default:
1078255736Sdavidch        if (src_type == DMAE_CMD_SRC_PCI)
1079255736Sdavidch            DP(msglvl, "DMAE: opcode 0x%08x\n"
1080255736Sdavidch               "src_addr [%x:%08x]  len [%d * 4]  dst_addr [none]\n"
1081255736Sdavidch               "comp_addr [%x:%08x]  comp_val 0x%08x\n",
1082255736Sdavidch               dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
1083255736Sdavidch               dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
1084255736Sdavidch               dmae->comp_val);
1085255736Sdavidch        else
1086255736Sdavidch            DP(msglvl, "DMAE: opcode 0x%08x\n"
1087255736Sdavidch               "src_addr [%08x]  len [%d * 4]  dst_addr [none]\n"
1088255736Sdavidch               "comp_addr [%x:%08x]  comp_val 0x%08x\n",
1089255736Sdavidch               dmae->opcode, dmae->src_addr_lo >> 2,
1090255736Sdavidch               dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
1091255736Sdavidch               dmae->comp_val);
1092255736Sdavidch        break;
1093255736Sdavidch    }
1094255736Sdavidch
1095255736Sdavidch}
1096255736Sdavidch#endif
1097255736Sdavidch
1098255736Sdavidchstatic int
1099255736Sdavidchbxe_acquire_hw_lock(struct bxe_softc *sc,
1100255736Sdavidch                    uint32_t         resource)
1101255736Sdavidch{
1102255736Sdavidch    uint32_t lock_status;
1103255736Sdavidch    uint32_t resource_bit = (1 << resource);
1104255736Sdavidch    int func = SC_FUNC(sc);
1105255736Sdavidch    uint32_t hw_lock_control_reg;
1106255736Sdavidch    int cnt;
1107255736Sdavidch
1108255736Sdavidch    /* validate the resource is within range */
1109255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1110255736Sdavidch        BLOGE(sc, "resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE\n", resource);
1111255736Sdavidch        return (-1);
1112255736Sdavidch    }
1113255736Sdavidch
1114255736Sdavidch    if (func <= 5) {
1115255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1116255736Sdavidch    } else {
1117255736Sdavidch        hw_lock_control_reg =
1118255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1119255736Sdavidch    }
1120255736Sdavidch
1121255736Sdavidch    /* validate the resource is not already taken */
1122255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1123255736Sdavidch    if (lock_status & resource_bit) {
1124255736Sdavidch        BLOGE(sc, "resource in use (status 0x%x bit 0x%x)\n",
1125255736Sdavidch              lock_status, resource_bit);
1126255736Sdavidch        return (-1);
1127255736Sdavidch    }
1128255736Sdavidch
1129255736Sdavidch    /* try every 5ms for 5 seconds */
1130255736Sdavidch    for (cnt = 0; cnt < 1000; cnt++) {
1131255736Sdavidch        REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1132255736Sdavidch        lock_status = REG_RD(sc, hw_lock_control_reg);
1133255736Sdavidch        if (lock_status & resource_bit) {
1134255736Sdavidch            return (0);
1135255736Sdavidch        }
1136255736Sdavidch        DELAY(5000);
1137255736Sdavidch    }
1138255736Sdavidch
1139255736Sdavidch    BLOGE(sc, "Resource lock timeout!\n");
1140255736Sdavidch    return (-1);
1141255736Sdavidch}
1142255736Sdavidch
1143255736Sdavidchstatic int
1144255736Sdavidchbxe_release_hw_lock(struct bxe_softc *sc,
1145255736Sdavidch                    uint32_t         resource)
1146255736Sdavidch{
1147255736Sdavidch    uint32_t lock_status;
1148255736Sdavidch    uint32_t resource_bit = (1 << resource);
1149255736Sdavidch    int func = SC_FUNC(sc);
1150255736Sdavidch    uint32_t hw_lock_control_reg;
1151255736Sdavidch
1152255736Sdavidch    /* validate the resource is within range */
1153255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1154255736Sdavidch        BLOGE(sc, "resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE\n", resource);
1155255736Sdavidch        return (-1);
1156255736Sdavidch    }
1157255736Sdavidch
1158255736Sdavidch    if (func <= 5) {
1159255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1160255736Sdavidch    } else {
1161255736Sdavidch        hw_lock_control_reg =
1162255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1163255736Sdavidch    }
1164255736Sdavidch
1165255736Sdavidch    /* validate the resource is currently taken */
1166255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1167255736Sdavidch    if (!(lock_status & resource_bit)) {
1168255736Sdavidch        BLOGE(sc, "resource not in use (status 0x%x bit 0x%x)\n",
1169255736Sdavidch              lock_status, resource_bit);
1170255736Sdavidch        return (-1);
1171255736Sdavidch    }
1172255736Sdavidch
1173255736Sdavidch    REG_WR(sc, hw_lock_control_reg, resource_bit);
1174255736Sdavidch    return (0);
1175255736Sdavidch}
1176284335Sdavidcsstatic void bxe_acquire_phy_lock(struct bxe_softc *sc)
1177284335Sdavidcs{
1178284335Sdavidcs	BXE_PHY_LOCK(sc);
1179284335Sdavidcs	bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1180284335Sdavidcs}
1181255736Sdavidch
1182284335Sdavidcsstatic void bxe_release_phy_lock(struct bxe_softc *sc)
1183284335Sdavidcs{
1184284335Sdavidcs	bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1185284335Sdavidcs	BXE_PHY_UNLOCK(sc);
1186284335Sdavidcs}
1187255736Sdavidch/*
1188255736Sdavidch * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1189255736Sdavidch * had we done things the other way around, if two pfs from the same port
1190255736Sdavidch * would attempt to access nvram at the same time, we could run into a
1191255736Sdavidch * scenario such as:
1192255736Sdavidch * pf A takes the port lock.
1193255736Sdavidch * pf B succeeds in taking the same lock since they are from the same port.
1194255736Sdavidch * pf A takes the per pf misc lock. Performs eeprom access.
1195255736Sdavidch * pf A finishes. Unlocks the per pf misc lock.
1196255736Sdavidch * Pf B takes the lock and proceeds to perform it's own access.
1197255736Sdavidch * pf A unlocks the per port lock, while pf B is still working (!).
1198255736Sdavidch * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1199255736Sdavidch * access corrupted by pf B).*
1200255736Sdavidch */
1201255736Sdavidchstatic int
1202255736Sdavidchbxe_acquire_nvram_lock(struct bxe_softc *sc)
1203255736Sdavidch{
1204255736Sdavidch    int port = SC_PORT(sc);
1205255736Sdavidch    int count, i;
1206255736Sdavidch    uint32_t val = 0;
1207255736Sdavidch
1208255736Sdavidch    /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1209255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1210255736Sdavidch
1211255736Sdavidch    /* adjust timeout for emulation/FPGA */
1212255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1213255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1214255736Sdavidch        count *= 100;
1215255736Sdavidch    }
1216255736Sdavidch
1217255736Sdavidch    /* request access to nvram interface */
1218255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1219255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1220255736Sdavidch
1221255736Sdavidch    for (i = 0; i < count*10; i++) {
1222255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1223255736Sdavidch        if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1224255736Sdavidch            break;
1225255736Sdavidch        }
1226255736Sdavidch
1227255736Sdavidch        DELAY(5);
1228255736Sdavidch    }
1229255736Sdavidch
1230255736Sdavidch    if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1231255736Sdavidch        BLOGE(sc, "Cannot get access to nvram interface\n");
1232255736Sdavidch        return (-1);
1233255736Sdavidch    }
1234255736Sdavidch
1235255736Sdavidch    return (0);
1236255736Sdavidch}
1237255736Sdavidch
1238255736Sdavidchstatic int
1239255736Sdavidchbxe_release_nvram_lock(struct bxe_softc *sc)
1240255736Sdavidch{
1241255736Sdavidch    int port = SC_PORT(sc);
1242255736Sdavidch    int count, i;
1243255736Sdavidch    uint32_t val = 0;
1244255736Sdavidch
1245255736Sdavidch    /* adjust timeout for emulation/FPGA */
1246255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1247255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1248255736Sdavidch        count *= 100;
1249255736Sdavidch    }
1250255736Sdavidch
1251255736Sdavidch    /* relinquish nvram interface */
1252255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1253255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1254255736Sdavidch
1255255736Sdavidch    for (i = 0; i < count*10; i++) {
1256255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1257255736Sdavidch        if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1258255736Sdavidch            break;
1259255736Sdavidch        }
1260255736Sdavidch
1261255736Sdavidch        DELAY(5);
1262255736Sdavidch    }
1263255736Sdavidch
1264255736Sdavidch    if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1265255736Sdavidch        BLOGE(sc, "Cannot free access to nvram interface\n");
1266255736Sdavidch        return (-1);
1267255736Sdavidch    }
1268255736Sdavidch
1269255736Sdavidch    /* release HW lock: protect against other PFs in PF Direct Assignment */
1270255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1271255736Sdavidch
1272255736Sdavidch    return (0);
1273255736Sdavidch}
1274255736Sdavidch
1275255736Sdavidchstatic void
1276255736Sdavidchbxe_enable_nvram_access(struct bxe_softc *sc)
1277255736Sdavidch{
1278255736Sdavidch    uint32_t val;
1279255736Sdavidch
1280255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1281255736Sdavidch
1282255736Sdavidch    /* enable both bits, even on read */
1283255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1284255736Sdavidch           (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1285255736Sdavidch}
1286255736Sdavidch
1287255736Sdavidchstatic void
1288255736Sdavidchbxe_disable_nvram_access(struct bxe_softc *sc)
1289255736Sdavidch{
1290255736Sdavidch    uint32_t val;
1291255736Sdavidch
1292255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1293255736Sdavidch
1294255736Sdavidch    /* disable both bits, even after read */
1295255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1296255736Sdavidch           (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1297255736Sdavidch                    MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1298255736Sdavidch}
1299255736Sdavidch
1300255736Sdavidchstatic int
1301255736Sdavidchbxe_nvram_read_dword(struct bxe_softc *sc,
1302255736Sdavidch                     uint32_t         offset,
1303255736Sdavidch                     uint32_t         *ret_val,
1304255736Sdavidch                     uint32_t         cmd_flags)
1305255736Sdavidch{
1306255736Sdavidch    int count, i, rc;
1307255736Sdavidch    uint32_t val;
1308255736Sdavidch
1309255736Sdavidch    /* build the command word */
1310255736Sdavidch    cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1311255736Sdavidch
1312255736Sdavidch    /* need to clear DONE bit separately */
1313255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1314255736Sdavidch
1315255736Sdavidch    /* address of the NVRAM to read from */
1316255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1317255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1318255736Sdavidch
1319255736Sdavidch    /* issue a read command */
1320255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1321255736Sdavidch
1322255736Sdavidch    /* adjust timeout for emulation/FPGA */
1323255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1324255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1325255736Sdavidch        count *= 100;
1326255736Sdavidch    }
1327255736Sdavidch
1328255736Sdavidch    /* wait for completion */
1329255736Sdavidch    *ret_val = 0;
1330255736Sdavidch    rc = -1;
1331255736Sdavidch    for (i = 0; i < count; i++) {
1332255736Sdavidch        DELAY(5);
1333255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1334255736Sdavidch
1335255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1336255736Sdavidch            val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1337255736Sdavidch            /* we read nvram data in cpu order
1338255736Sdavidch             * but ethtool sees it as an array of bytes
1339255736Sdavidch             * converting to big-endian will do the work
1340255736Sdavidch             */
1341255736Sdavidch            *ret_val = htobe32(val);
1342255736Sdavidch            rc = 0;
1343255736Sdavidch            break;
1344255736Sdavidch        }
1345255736Sdavidch    }
1346255736Sdavidch
1347255736Sdavidch    if (rc == -1) {
1348255736Sdavidch        BLOGE(sc, "nvram read timeout expired\n");
1349255736Sdavidch    }
1350255736Sdavidch
1351255736Sdavidch    return (rc);
1352255736Sdavidch}
1353255736Sdavidch
1354255736Sdavidchstatic int
1355255736Sdavidchbxe_nvram_read(struct bxe_softc *sc,
1356255736Sdavidch               uint32_t         offset,
1357255736Sdavidch               uint8_t          *ret_buf,
1358255736Sdavidch               int              buf_size)
1359255736Sdavidch{
1360255736Sdavidch    uint32_t cmd_flags;
1361255736Sdavidch    uint32_t val;
1362255736Sdavidch    int rc;
1363255736Sdavidch
1364255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1365255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1366255736Sdavidch              offset, buf_size);
1367255736Sdavidch        return (-1);
1368255736Sdavidch    }
1369255736Sdavidch
1370255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1371255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1372255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1373255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1374255736Sdavidch        return (-1);
1375255736Sdavidch    }
1376255736Sdavidch
1377255736Sdavidch    /* request access to nvram interface */
1378255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1379255736Sdavidch    if (rc) {
1380255736Sdavidch        return (rc);
1381255736Sdavidch    }
1382255736Sdavidch
1383255736Sdavidch    /* enable access to nvram interface */
1384255736Sdavidch    bxe_enable_nvram_access(sc);
1385255736Sdavidch
1386255736Sdavidch    /* read the first word(s) */
1387255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1388255736Sdavidch    while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1389255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1390255736Sdavidch        memcpy(ret_buf, &val, 4);
1391255736Sdavidch
1392255736Sdavidch        /* advance to the next dword */
1393255736Sdavidch        offset += sizeof(uint32_t);
1394255736Sdavidch        ret_buf += sizeof(uint32_t);
1395255736Sdavidch        buf_size -= sizeof(uint32_t);
1396255736Sdavidch        cmd_flags = 0;
1397255736Sdavidch    }
1398255736Sdavidch
1399255736Sdavidch    if (rc == 0) {
1400255736Sdavidch        cmd_flags |= MCPR_NVM_COMMAND_LAST;
1401255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1402255736Sdavidch        memcpy(ret_buf, &val, 4);
1403255736Sdavidch    }
1404255736Sdavidch
1405255736Sdavidch    /* disable access to nvram interface */
1406255736Sdavidch    bxe_disable_nvram_access(sc);
1407255736Sdavidch    bxe_release_nvram_lock(sc);
1408255736Sdavidch
1409255736Sdavidch    return (rc);
1410255736Sdavidch}
1411255736Sdavidch
1412255736Sdavidchstatic int
1413255736Sdavidchbxe_nvram_write_dword(struct bxe_softc *sc,
1414255736Sdavidch                      uint32_t         offset,
1415255736Sdavidch                      uint32_t         val,
1416255736Sdavidch                      uint32_t         cmd_flags)
1417255736Sdavidch{
1418255736Sdavidch    int count, i, rc;
1419255736Sdavidch
1420255736Sdavidch    /* build the command word */
1421255736Sdavidch    cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1422255736Sdavidch
1423255736Sdavidch    /* need to clear DONE bit separately */
1424255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1425255736Sdavidch
1426255736Sdavidch    /* write the data */
1427255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1428255736Sdavidch
1429255736Sdavidch    /* address of the NVRAM to write to */
1430255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1431255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1432255736Sdavidch
1433255736Sdavidch    /* issue the write command */
1434255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1435255736Sdavidch
1436255736Sdavidch    /* adjust timeout for emulation/FPGA */
1437255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1438255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1439255736Sdavidch        count *= 100;
1440255736Sdavidch    }
1441255736Sdavidch
1442255736Sdavidch    /* wait for completion */
1443255736Sdavidch    rc = -1;
1444255736Sdavidch    for (i = 0; i < count; i++) {
1445255736Sdavidch        DELAY(5);
1446255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1447255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1448255736Sdavidch            rc = 0;
1449255736Sdavidch            break;
1450255736Sdavidch        }
1451255736Sdavidch    }
1452255736Sdavidch
1453255736Sdavidch    if (rc == -1) {
1454255736Sdavidch        BLOGE(sc, "nvram write timeout expired\n");
1455255736Sdavidch    }
1456255736Sdavidch
1457255736Sdavidch    return (rc);
1458255736Sdavidch}
1459255736Sdavidch
1460255736Sdavidch#define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1461255736Sdavidch
1462255736Sdavidchstatic int
1463255736Sdavidchbxe_nvram_write1(struct bxe_softc *sc,
1464255736Sdavidch                 uint32_t         offset,
1465255736Sdavidch                 uint8_t          *data_buf,
1466255736Sdavidch                 int              buf_size)
1467255736Sdavidch{
1468255736Sdavidch    uint32_t cmd_flags;
1469255736Sdavidch    uint32_t align_offset;
1470255736Sdavidch    uint32_t val;
1471255736Sdavidch    int rc;
1472255736Sdavidch
1473255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1474255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1475255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1476255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1477255736Sdavidch        return (-1);
1478255736Sdavidch    }
1479255736Sdavidch
1480255736Sdavidch    /* request access to nvram interface */
1481255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1482255736Sdavidch    if (rc) {
1483255736Sdavidch        return (rc);
1484255736Sdavidch    }
1485255736Sdavidch
1486255736Sdavidch    /* enable access to nvram interface */
1487255736Sdavidch    bxe_enable_nvram_access(sc);
1488255736Sdavidch
1489255736Sdavidch    cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1490255736Sdavidch    align_offset = (offset & ~0x03);
1491255736Sdavidch    rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1492255736Sdavidch
1493255736Sdavidch    if (rc == 0) {
1494255736Sdavidch        val &= ~(0xff << BYTE_OFFSET(offset));
1495255736Sdavidch        val |= (*data_buf << BYTE_OFFSET(offset));
1496255736Sdavidch
1497255736Sdavidch        /* nvram data is returned as an array of bytes
1498255736Sdavidch         * convert it back to cpu order
1499255736Sdavidch         */
1500255736Sdavidch        val = be32toh(val);
1501255736Sdavidch
1502255736Sdavidch        rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1503255736Sdavidch    }
1504255736Sdavidch
1505255736Sdavidch    /* disable access to nvram interface */
1506255736Sdavidch    bxe_disable_nvram_access(sc);
1507255736Sdavidch    bxe_release_nvram_lock(sc);
1508255736Sdavidch
1509255736Sdavidch    return (rc);
1510255736Sdavidch}
1511255736Sdavidch
1512255736Sdavidchstatic int
1513255736Sdavidchbxe_nvram_write(struct bxe_softc *sc,
1514255736Sdavidch                uint32_t         offset,
1515255736Sdavidch                uint8_t          *data_buf,
1516255736Sdavidch                int              buf_size)
1517255736Sdavidch{
1518255736Sdavidch    uint32_t cmd_flags;
1519255736Sdavidch    uint32_t val;
1520255736Sdavidch    uint32_t written_so_far;
1521255736Sdavidch    int rc;
1522255736Sdavidch
1523255736Sdavidch    if (buf_size == 1) {
1524255736Sdavidch        return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1525255736Sdavidch    }
1526255736Sdavidch
1527255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1528255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1529255736Sdavidch              offset, buf_size);
1530255736Sdavidch        return (-1);
1531255736Sdavidch    }
1532255736Sdavidch
1533255736Sdavidch    if (buf_size == 0) {
1534255736Sdavidch        return (0); /* nothing to do */
1535255736Sdavidch    }
1536255736Sdavidch
1537255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1538255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1539255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1540255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1541255736Sdavidch        return (-1);
1542255736Sdavidch    }
1543255736Sdavidch
1544255736Sdavidch    /* request access to nvram interface */
1545255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1546255736Sdavidch    if (rc) {
1547255736Sdavidch        return (rc);
1548255736Sdavidch    }
1549255736Sdavidch
1550255736Sdavidch    /* enable access to nvram interface */
1551255736Sdavidch    bxe_enable_nvram_access(sc);
1552255736Sdavidch
1553255736Sdavidch    written_so_far = 0;
1554255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1555255736Sdavidch    while ((written_so_far < buf_size) && (rc == 0)) {
1556255736Sdavidch        if (written_so_far == (buf_size - sizeof(uint32_t))) {
1557255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1558255736Sdavidch        } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1559255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1560255736Sdavidch        } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1561255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1562255736Sdavidch        }
1563255736Sdavidch
1564255736Sdavidch        memcpy(&val, data_buf, 4);
1565255736Sdavidch
1566255736Sdavidch        rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1567255736Sdavidch
1568255736Sdavidch        /* advance to the next dword */
1569255736Sdavidch        offset += sizeof(uint32_t);
1570255736Sdavidch        data_buf += sizeof(uint32_t);
1571255736Sdavidch        written_so_far += sizeof(uint32_t);
1572255736Sdavidch        cmd_flags = 0;
1573255736Sdavidch    }
1574255736Sdavidch
1575255736Sdavidch    /* disable access to nvram interface */
1576255736Sdavidch    bxe_disable_nvram_access(sc);
1577255736Sdavidch    bxe_release_nvram_lock(sc);
1578255736Sdavidch
1579255736Sdavidch    return (rc);
1580255736Sdavidch}
1581255736Sdavidch
1582255736Sdavidch/* copy command into DMAE command memory and set DMAE command Go */
1583255736Sdavidchvoid
1584255736Sdavidchbxe_post_dmae(struct bxe_softc    *sc,
1585255736Sdavidch              struct dmae_command *dmae,
1586255736Sdavidch              int                 idx)
1587255736Sdavidch{
1588255736Sdavidch    uint32_t cmd_offset;
1589255736Sdavidch    int i;
1590255736Sdavidch
1591255736Sdavidch    cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_command) * idx));
1592255736Sdavidch    for (i = 0; i < ((sizeof(struct dmae_command) / 4)); i++) {
1593255736Sdavidch        REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1594255736Sdavidch    }
1595255736Sdavidch
1596255736Sdavidch    REG_WR(sc, dmae_reg_go_c[idx], 1);
1597255736Sdavidch}
1598255736Sdavidch
1599255736Sdavidchuint32_t
1600255736Sdavidchbxe_dmae_opcode_add_comp(uint32_t opcode,
1601255736Sdavidch                         uint8_t  comp_type)
1602255736Sdavidch{
1603255736Sdavidch    return (opcode | ((comp_type << DMAE_COMMAND_C_DST_SHIFT) |
1604255736Sdavidch                      DMAE_COMMAND_C_TYPE_ENABLE));
1605255736Sdavidch}
1606255736Sdavidch
1607255736Sdavidchuint32_t
1608255736Sdavidchbxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1609255736Sdavidch{
1610255736Sdavidch    return (opcode & ~DMAE_COMMAND_SRC_RESET);
1611255736Sdavidch}
1612255736Sdavidch
1613255736Sdavidchuint32_t
1614255736Sdavidchbxe_dmae_opcode(struct bxe_softc *sc,
1615255736Sdavidch                uint8_t          src_type,
1616255736Sdavidch                uint8_t          dst_type,
1617255736Sdavidch                uint8_t          with_comp,
1618255736Sdavidch                uint8_t          comp_type)
1619255736Sdavidch{
1620255736Sdavidch    uint32_t opcode = 0;
1621255736Sdavidch
1622255736Sdavidch    opcode |= ((src_type << DMAE_COMMAND_SRC_SHIFT) |
1623255736Sdavidch               (dst_type << DMAE_COMMAND_DST_SHIFT));
1624255736Sdavidch
1625255736Sdavidch    opcode |= (DMAE_COMMAND_SRC_RESET | DMAE_COMMAND_DST_RESET);
1626255736Sdavidch
1627255736Sdavidch    opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1628255736Sdavidch
1629255736Sdavidch    opcode |= ((SC_VN(sc) << DMAE_COMMAND_E1HVN_SHIFT) |
1630255736Sdavidch               (SC_VN(sc) << DMAE_COMMAND_DST_VN_SHIFT));
1631255736Sdavidch
1632255736Sdavidch    opcode |= (DMAE_COM_SET_ERR << DMAE_COMMAND_ERR_POLICY_SHIFT);
1633255736Sdavidch
1634255736Sdavidch#ifdef __BIG_ENDIAN
1635255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1636255736Sdavidch#else
1637255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1638255736Sdavidch#endif
1639255736Sdavidch
1640255736Sdavidch    if (with_comp) {
1641255736Sdavidch        opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1642255736Sdavidch    }
1643255736Sdavidch
1644255736Sdavidch    return (opcode);
1645255736Sdavidch}
1646255736Sdavidch
1647255736Sdavidchstatic void
1648255736Sdavidchbxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1649255736Sdavidch                        struct dmae_command *dmae,
1650255736Sdavidch                        uint8_t             src_type,
1651255736Sdavidch                        uint8_t             dst_type)
1652255736Sdavidch{
1653255736Sdavidch    memset(dmae, 0, sizeof(struct dmae_command));
1654255736Sdavidch
1655255736Sdavidch    /* set the opcode */
1656255736Sdavidch    dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1657255736Sdavidch                                   TRUE, DMAE_COMP_PCI);
1658255736Sdavidch
1659255736Sdavidch    /* fill in the completion parameters */
1660255736Sdavidch    dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1661255736Sdavidch    dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1662255736Sdavidch    dmae->comp_val     = DMAE_COMP_VAL;
1663255736Sdavidch}
1664255736Sdavidch
1665255736Sdavidch/* issue a DMAE command over the init channel and wait for completion */
1666255736Sdavidchstatic int
1667255736Sdavidchbxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1668255736Sdavidch                         struct dmae_command *dmae)
1669255736Sdavidch{
1670255736Sdavidch    uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1671255736Sdavidch    int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1672255736Sdavidch
1673255736Sdavidch    BXE_DMAE_LOCK(sc);
1674255736Sdavidch
1675255736Sdavidch    /* reset completion */
1676255736Sdavidch    *wb_comp = 0;
1677255736Sdavidch
1678255736Sdavidch    /* post the command on the channel used for initializations */
1679255736Sdavidch    bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1680255736Sdavidch
1681255736Sdavidch    /* wait for completion */
1682255736Sdavidch    DELAY(5);
1683255736Sdavidch
1684255736Sdavidch    while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1685255736Sdavidch        if (!timeout ||
1686255736Sdavidch            (sc->recovery_state != BXE_RECOVERY_DONE &&
1687255736Sdavidch             sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1688255736Sdavidch            BLOGE(sc, "DMAE timeout!\n");
1689255736Sdavidch            BXE_DMAE_UNLOCK(sc);
1690255736Sdavidch            return (DMAE_TIMEOUT);
1691255736Sdavidch        }
1692255736Sdavidch
1693255736Sdavidch        timeout--;
1694255736Sdavidch        DELAY(50);
1695255736Sdavidch    }
1696255736Sdavidch
1697255736Sdavidch    if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1698255736Sdavidch        BLOGE(sc, "DMAE PCI error!\n");
1699255736Sdavidch        BXE_DMAE_UNLOCK(sc);
1700255736Sdavidch        return (DMAE_PCI_ERROR);
1701255736Sdavidch    }
1702255736Sdavidch
1703255736Sdavidch    BXE_DMAE_UNLOCK(sc);
1704255736Sdavidch    return (0);
1705255736Sdavidch}
1706255736Sdavidch
1707255736Sdavidchvoid
1708255736Sdavidchbxe_read_dmae(struct bxe_softc *sc,
1709255736Sdavidch              uint32_t         src_addr,
1710255736Sdavidch              uint32_t         len32)
1711255736Sdavidch{
1712255736Sdavidch    struct dmae_command dmae;
1713255736Sdavidch    uint32_t *data;
1714255736Sdavidch    int i, rc;
1715255736Sdavidch
1716255736Sdavidch    DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1717255736Sdavidch
1718255736Sdavidch    if (!sc->dmae_ready) {
1719255736Sdavidch        data = BXE_SP(sc, wb_data[0]);
1720255736Sdavidch
1721255736Sdavidch        for (i = 0; i < len32; i++) {
1722255736Sdavidch            data[i] = (CHIP_IS_E1(sc)) ?
1723255736Sdavidch                          bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1724255736Sdavidch                          REG_RD(sc, (src_addr + (i * 4)));
1725255736Sdavidch        }
1726255736Sdavidch
1727255736Sdavidch        return;
1728255736Sdavidch    }
1729255736Sdavidch
1730255736Sdavidch    /* set opcode and fixed command fields */
1731255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1732255736Sdavidch
1733255736Sdavidch    /* fill in addresses and len */
1734255736Sdavidch    dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1735255736Sdavidch    dmae.src_addr_hi = 0;
1736255736Sdavidch    dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1737255736Sdavidch    dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1738255736Sdavidch    dmae.len         = len32;
1739255736Sdavidch
1740255736Sdavidch    /* issue the command and wait for completion */
1741255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1742255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1743255736Sdavidch    };
1744255736Sdavidch}
1745255736Sdavidch
1746255736Sdavidchvoid
1747255736Sdavidchbxe_write_dmae(struct bxe_softc *sc,
1748255736Sdavidch               bus_addr_t       dma_addr,
1749255736Sdavidch               uint32_t         dst_addr,
1750255736Sdavidch               uint32_t         len32)
1751255736Sdavidch{
1752255736Sdavidch    struct dmae_command dmae;
1753255736Sdavidch    int rc;
1754255736Sdavidch
1755255736Sdavidch    if (!sc->dmae_ready) {
1756255736Sdavidch        DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1757255736Sdavidch
1758255736Sdavidch        if (CHIP_IS_E1(sc)) {
1759255736Sdavidch            ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1760255736Sdavidch        } else {
1761255736Sdavidch            ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1762255736Sdavidch        }
1763255736Sdavidch
1764255736Sdavidch        return;
1765255736Sdavidch    }
1766255736Sdavidch
1767255736Sdavidch    /* set opcode and fixed command fields */
1768255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1769255736Sdavidch
1770255736Sdavidch    /* fill in addresses and len */
1771255736Sdavidch    dmae.src_addr_lo = U64_LO(dma_addr);
1772255736Sdavidch    dmae.src_addr_hi = U64_HI(dma_addr);
1773255736Sdavidch    dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1774255736Sdavidch    dmae.dst_addr_hi = 0;
1775255736Sdavidch    dmae.len         = len32;
1776255736Sdavidch
1777255736Sdavidch    /* issue the command and wait for completion */
1778255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1779255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1780255736Sdavidch    }
1781255736Sdavidch}
1782255736Sdavidch
1783255736Sdavidchvoid
1784255736Sdavidchbxe_write_dmae_phys_len(struct bxe_softc *sc,
1785255736Sdavidch                        bus_addr_t       phys_addr,
1786255736Sdavidch                        uint32_t         addr,
1787255736Sdavidch                        uint32_t         len)
1788255736Sdavidch{
1789255736Sdavidch    int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1790255736Sdavidch    int offset = 0;
1791255736Sdavidch
1792255736Sdavidch    while (len > dmae_wr_max) {
1793255736Sdavidch        bxe_write_dmae(sc,
1794255736Sdavidch                       (phys_addr + offset), /* src DMA address */
1795255736Sdavidch                       (addr + offset),      /* dst GRC address */
1796255736Sdavidch                       dmae_wr_max);
1797255736Sdavidch        offset += (dmae_wr_max * 4);
1798255736Sdavidch        len -= dmae_wr_max;
1799255736Sdavidch    }
1800255736Sdavidch
1801255736Sdavidch    bxe_write_dmae(sc,
1802255736Sdavidch                   (phys_addr + offset), /* src DMA address */
1803255736Sdavidch                   (addr + offset),      /* dst GRC address */
1804255736Sdavidch                   len);
1805255736Sdavidch}
1806255736Sdavidch
1807255736Sdavidchvoid
1808255736Sdavidchbxe_set_ctx_validation(struct bxe_softc   *sc,
1809255736Sdavidch                       struct eth_context *cxt,
1810255736Sdavidch                       uint32_t           cid)
1811255736Sdavidch{
1812255736Sdavidch    /* ustorm cxt validation */
1813255736Sdavidch    cxt->ustorm_ag_context.cdu_usage =
1814255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1815255736Sdavidch            CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1816255736Sdavidch    /* xcontext validation */
1817255736Sdavidch    cxt->xstorm_ag_context.cdu_reserved =
1818255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1819255736Sdavidch            CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1820255736Sdavidch}
1821255736Sdavidch
1822255736Sdavidchstatic void
1823255736Sdavidchbxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1824255736Sdavidch                            uint8_t          port,
1825255736Sdavidch                            uint8_t          fw_sb_id,
1826255736Sdavidch                            uint8_t          sb_index,
1827255736Sdavidch                            uint8_t          ticks)
1828255736Sdavidch{
1829255736Sdavidch    uint32_t addr =
1830255736Sdavidch        (BAR_CSTRORM_INTMEM +
1831255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1832255736Sdavidch
1833255736Sdavidch    REG_WR8(sc, addr, ticks);
1834255736Sdavidch
1835255736Sdavidch    BLOGD(sc, DBG_LOAD,
1836255736Sdavidch          "port %d fw_sb_id %d sb_index %d ticks %d\n",
1837255736Sdavidch          port, fw_sb_id, sb_index, ticks);
1838255736Sdavidch}
1839255736Sdavidch
1840255736Sdavidchstatic void
1841255736Sdavidchbxe_storm_memset_hc_disable(struct bxe_softc *sc,
1842255736Sdavidch                            uint8_t          port,
1843255736Sdavidch                            uint16_t         fw_sb_id,
1844255736Sdavidch                            uint8_t          sb_index,
1845255736Sdavidch                            uint8_t          disable)
1846255736Sdavidch{
1847255736Sdavidch    uint32_t enable_flag =
1848255736Sdavidch        (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1849255736Sdavidch    uint32_t addr =
1850255736Sdavidch        (BAR_CSTRORM_INTMEM +
1851255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1852255736Sdavidch    uint8_t flags;
1853255736Sdavidch
1854255736Sdavidch    /* clear and set */
1855255736Sdavidch    flags = REG_RD8(sc, addr);
1856255736Sdavidch    flags &= ~HC_INDEX_DATA_HC_ENABLED;
1857255736Sdavidch    flags |= enable_flag;
1858255736Sdavidch    REG_WR8(sc, addr, flags);
1859255736Sdavidch
1860255736Sdavidch    BLOGD(sc, DBG_LOAD,
1861255736Sdavidch          "port %d fw_sb_id %d sb_index %d disable %d\n",
1862255736Sdavidch          port, fw_sb_id, sb_index, disable);
1863255736Sdavidch}
1864255736Sdavidch
1865255736Sdavidchvoid
1866255736Sdavidchbxe_update_coalesce_sb_index(struct bxe_softc *sc,
1867255736Sdavidch                             uint8_t          fw_sb_id,
1868255736Sdavidch                             uint8_t          sb_index,
1869255736Sdavidch                             uint8_t          disable,
1870255736Sdavidch                             uint16_t         usec)
1871255736Sdavidch{
1872255736Sdavidch    int port = SC_PORT(sc);
1873255736Sdavidch    uint8_t ticks = (usec / 4); /* XXX ??? */
1874255736Sdavidch
1875255736Sdavidch    bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1876255736Sdavidch
1877255736Sdavidch    disable = (disable) ? 1 : ((usec) ? 0 : 1);
1878255736Sdavidch    bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1879255736Sdavidch}
1880255736Sdavidch
1881255736Sdavidchvoid
1882255736Sdavidchelink_cb_udelay(struct bxe_softc *sc,
1883255736Sdavidch                uint32_t         usecs)
1884255736Sdavidch{
1885255736Sdavidch    DELAY(usecs);
1886255736Sdavidch}
1887255736Sdavidch
1888255736Sdavidchuint32_t
1889255736Sdavidchelink_cb_reg_read(struct bxe_softc *sc,
1890255736Sdavidch                  uint32_t         reg_addr)
1891255736Sdavidch{
1892255736Sdavidch    return (REG_RD(sc, reg_addr));
1893255736Sdavidch}
1894255736Sdavidch
1895255736Sdavidchvoid
1896255736Sdavidchelink_cb_reg_write(struct bxe_softc *sc,
1897255736Sdavidch                   uint32_t         reg_addr,
1898255736Sdavidch                   uint32_t         val)
1899255736Sdavidch{
1900255736Sdavidch    REG_WR(sc, reg_addr, val);
1901255736Sdavidch}
1902255736Sdavidch
1903255736Sdavidchvoid
1904255736Sdavidchelink_cb_reg_wb_write(struct bxe_softc *sc,
1905255736Sdavidch                      uint32_t         offset,
1906255736Sdavidch                      uint32_t         *wb_write,
1907255736Sdavidch                      uint16_t         len)
1908255736Sdavidch{
1909255736Sdavidch    REG_WR_DMAE(sc, offset, wb_write, len);
1910255736Sdavidch}
1911255736Sdavidch
1912255736Sdavidchvoid
1913255736Sdavidchelink_cb_reg_wb_read(struct bxe_softc *sc,
1914255736Sdavidch                     uint32_t         offset,
1915255736Sdavidch                     uint32_t         *wb_write,
1916255736Sdavidch                     uint16_t         len)
1917255736Sdavidch{
1918255736Sdavidch    REG_RD_DMAE(sc, offset, wb_write, len);
1919255736Sdavidch}
1920255736Sdavidch
1921255736Sdavidchuint8_t
1922255736Sdavidchelink_cb_path_id(struct bxe_softc *sc)
1923255736Sdavidch{
1924255736Sdavidch    return (SC_PATH(sc));
1925255736Sdavidch}
1926255736Sdavidch
1927255736Sdavidchvoid
1928255736Sdavidchelink_cb_event_log(struct bxe_softc     *sc,
1929255736Sdavidch                   const elink_log_id_t elink_log_id,
1930255736Sdavidch                   ...)
1931255736Sdavidch{
1932255736Sdavidch    /* XXX */
1933255736Sdavidch#if 0
1934255736Sdavidch    //va_list ap;
1935255736Sdavidch    va_start(ap, elink_log_id);
1936255736Sdavidch    _XXX_(sc, lm_log_id, ap);
1937255736Sdavidch    va_end(ap);
1938255736Sdavidch#endif
1939255736Sdavidch    BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1940255736Sdavidch}
1941255736Sdavidch
1942255736Sdavidchstatic int
1943255736Sdavidchbxe_set_spio(struct bxe_softc *sc,
1944255736Sdavidch             int              spio,
1945255736Sdavidch             uint32_t         mode)
1946255736Sdavidch{
1947255736Sdavidch    uint32_t spio_reg;
1948255736Sdavidch
1949255736Sdavidch    /* Only 2 SPIOs are configurable */
1950255736Sdavidch    if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1951255736Sdavidch        BLOGE(sc, "Invalid SPIO 0x%x\n", spio);
1952255736Sdavidch        return (-1);
1953255736Sdavidch    }
1954255736Sdavidch
1955255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1956255736Sdavidch
1957255736Sdavidch    /* read SPIO and mask except the float bits */
1958255736Sdavidch    spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1959255736Sdavidch
1960255736Sdavidch    switch (mode) {
1961255736Sdavidch    case MISC_SPIO_OUTPUT_LOW:
1962255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1963255736Sdavidch        /* clear FLOAT and set CLR */
1964255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1965255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1966255736Sdavidch        break;
1967255736Sdavidch
1968255736Sdavidch    case MISC_SPIO_OUTPUT_HIGH:
1969255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1970255736Sdavidch        /* clear FLOAT and set SET */
1971255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1972255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_SET_POS);
1973255736Sdavidch        break;
1974255736Sdavidch
1975255736Sdavidch    case MISC_SPIO_INPUT_HI_Z:
1976255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1977255736Sdavidch        /* set FLOAT */
1978255736Sdavidch        spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1979255736Sdavidch        break;
1980255736Sdavidch
1981255736Sdavidch    default:
1982255736Sdavidch        break;
1983255736Sdavidch    }
1984255736Sdavidch
1985255736Sdavidch    REG_WR(sc, MISC_REG_SPIO, spio_reg);
1986255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1987255736Sdavidch
1988255736Sdavidch    return (0);
1989255736Sdavidch}
1990255736Sdavidch
1991255736Sdavidchstatic int
1992255736Sdavidchbxe_gpio_read(struct bxe_softc *sc,
1993255736Sdavidch              int              gpio_num,
1994255736Sdavidch              uint8_t          port)
1995255736Sdavidch{
1996255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1997255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1998255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1999255736Sdavidch    int gpio_shift = (gpio_num +
2000255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2001255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
2002255736Sdavidch    uint32_t gpio_reg;
2003255736Sdavidch
2004255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
2005255736Sdavidch        BLOGE(sc, "Invalid GPIO %d\n", gpio_num);
2006255736Sdavidch        return (-1);
2007255736Sdavidch    }
2008255736Sdavidch
2009255736Sdavidch    /* read GPIO value */
2010255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
2011255736Sdavidch
2012255736Sdavidch    /* get the requested pin value */
2013255736Sdavidch    return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
2014255736Sdavidch}
2015255736Sdavidch
2016255736Sdavidchstatic int
2017255736Sdavidchbxe_gpio_write(struct bxe_softc *sc,
2018255736Sdavidch               int              gpio_num,
2019255736Sdavidch               uint32_t         mode,
2020255736Sdavidch               uint8_t          port)
2021255736Sdavidch{
2022255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
2023255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2024255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2025255736Sdavidch    int gpio_shift = (gpio_num +
2026255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2027255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
2028255736Sdavidch    uint32_t gpio_reg;
2029255736Sdavidch
2030255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
2031255736Sdavidch        BLOGE(sc, "Invalid GPIO %d\n", gpio_num);
2032255736Sdavidch        return (-1);
2033255736Sdavidch    }
2034255736Sdavidch
2035255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2036255736Sdavidch
2037255736Sdavidch    /* read GPIO and mask except the float bits */
2038255736Sdavidch    gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
2039255736Sdavidch
2040255736Sdavidch    switch (mode) {
2041255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2042255736Sdavidch        BLOGD(sc, DBG_PHY,
2043255736Sdavidch              "Set GPIO %d (shift %d) -> output low\n",
2044255736Sdavidch              gpio_num, gpio_shift);
2045255736Sdavidch        /* clear FLOAT and set CLR */
2046255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2047255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
2048255736Sdavidch        break;
2049255736Sdavidch
2050255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2051255736Sdavidch        BLOGD(sc, DBG_PHY,
2052255736Sdavidch              "Set GPIO %d (shift %d) -> output high\n",
2053255736Sdavidch              gpio_num, gpio_shift);
2054255736Sdavidch        /* clear FLOAT and set SET */
2055255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2056255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
2057255736Sdavidch        break;
2058255736Sdavidch
2059255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2060255736Sdavidch        BLOGD(sc, DBG_PHY,
2061255736Sdavidch              "Set GPIO %d (shift %d) -> input\n",
2062255736Sdavidch              gpio_num, gpio_shift);
2063255736Sdavidch        /* set FLOAT */
2064255736Sdavidch        gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2065255736Sdavidch        break;
2066255736Sdavidch
2067255736Sdavidch    default:
2068255736Sdavidch        break;
2069255736Sdavidch    }
2070255736Sdavidch
2071255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2072255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2073255736Sdavidch
2074255736Sdavidch    return (0);
2075255736Sdavidch}
2076255736Sdavidch
2077255736Sdavidchstatic int
2078255736Sdavidchbxe_gpio_mult_write(struct bxe_softc *sc,
2079255736Sdavidch                    uint8_t          pins,
2080255736Sdavidch                    uint32_t         mode)
2081255736Sdavidch{
2082255736Sdavidch    uint32_t gpio_reg;
2083255736Sdavidch
2084255736Sdavidch    /* any port swapping should be handled by caller */
2085255736Sdavidch
2086255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2087255736Sdavidch
2088255736Sdavidch    /* read GPIO and mask except the float bits */
2089255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
2090255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2091255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
2092255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
2093255736Sdavidch
2094255736Sdavidch    switch (mode) {
2095255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2096255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
2097255736Sdavidch        /* set CLR */
2098255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
2099255736Sdavidch        break;
2100255736Sdavidch
2101255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2102255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
2103255736Sdavidch        /* set SET */
2104255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
2105255736Sdavidch        break;
2106255736Sdavidch
2107255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2108255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
2109255736Sdavidch        /* set FLOAT */
2110255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2111255736Sdavidch        break;
2112255736Sdavidch
2113255736Sdavidch    default:
2114255736Sdavidch        BLOGE(sc, "Invalid GPIO mode assignment %d\n", mode);
2115255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2116255736Sdavidch        return (-1);
2117255736Sdavidch    }
2118255736Sdavidch
2119255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2120255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2121255736Sdavidch
2122255736Sdavidch    return (0);
2123255736Sdavidch}
2124255736Sdavidch
2125255736Sdavidchstatic int
2126255736Sdavidchbxe_gpio_int_write(struct bxe_softc *sc,
2127255736Sdavidch                   int              gpio_num,
2128255736Sdavidch                   uint32_t         mode,
2129255736Sdavidch                   uint8_t          port)
2130255736Sdavidch{
2131255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
2132255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2133255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2134255736Sdavidch    int gpio_shift = (gpio_num +
2135255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2136255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
2137255736Sdavidch    uint32_t gpio_reg;
2138255736Sdavidch
2139255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
2140255736Sdavidch        BLOGE(sc, "Invalid GPIO %d\n", gpio_num);
2141255736Sdavidch        return (-1);
2142255736Sdavidch    }
2143255736Sdavidch
2144255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2145255736Sdavidch
2146255736Sdavidch    /* read GPIO int */
2147255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2148255736Sdavidch
2149255736Sdavidch    switch (mode) {
2150255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2151255736Sdavidch        BLOGD(sc, DBG_PHY,
2152255736Sdavidch              "Clear GPIO INT %d (shift %d) -> output low\n",
2153255736Sdavidch              gpio_num, gpio_shift);
2154255736Sdavidch        /* clear SET and set CLR */
2155255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2156255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2157255736Sdavidch        break;
2158255736Sdavidch
2159255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2160255736Sdavidch        BLOGD(sc, DBG_PHY,
2161255736Sdavidch              "Set GPIO INT %d (shift %d) -> output high\n",
2162255736Sdavidch              gpio_num, gpio_shift);
2163255736Sdavidch        /* clear CLR and set SET */
2164255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2165255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2166255736Sdavidch        break;
2167255736Sdavidch
2168255736Sdavidch    default:
2169255736Sdavidch        break;
2170255736Sdavidch    }
2171255736Sdavidch
2172255736Sdavidch    REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2173255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2174255736Sdavidch
2175255736Sdavidch    return (0);
2176255736Sdavidch}
2177255736Sdavidch
2178255736Sdavidchuint32_t
2179255736Sdavidchelink_cb_gpio_read(struct bxe_softc *sc,
2180255736Sdavidch                   uint16_t         gpio_num,
2181255736Sdavidch                   uint8_t          port)
2182255736Sdavidch{
2183255736Sdavidch    return (bxe_gpio_read(sc, gpio_num, port));
2184255736Sdavidch}
2185255736Sdavidch
2186255736Sdavidchuint8_t
2187255736Sdavidchelink_cb_gpio_write(struct bxe_softc *sc,
2188255736Sdavidch                    uint16_t         gpio_num,
2189255736Sdavidch                    uint8_t          mode, /* 0=low 1=high */
2190255736Sdavidch                    uint8_t          port)
2191255736Sdavidch{
2192255736Sdavidch    return (bxe_gpio_write(sc, gpio_num, mode, port));
2193255736Sdavidch}
2194255736Sdavidch
2195255736Sdavidchuint8_t
2196255736Sdavidchelink_cb_gpio_mult_write(struct bxe_softc *sc,
2197255736Sdavidch                         uint8_t          pins,
2198255736Sdavidch                         uint8_t          mode) /* 0=low 1=high */
2199255736Sdavidch{
2200255736Sdavidch    return (bxe_gpio_mult_write(sc, pins, mode));
2201255736Sdavidch}
2202255736Sdavidch
2203255736Sdavidchuint8_t
2204255736Sdavidchelink_cb_gpio_int_write(struct bxe_softc *sc,
2205255736Sdavidch                        uint16_t         gpio_num,
2206255736Sdavidch                        uint8_t          mode, /* 0=low 1=high */
2207255736Sdavidch                        uint8_t          port)
2208255736Sdavidch{
2209255736Sdavidch    return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2210255736Sdavidch}
2211255736Sdavidch
2212255736Sdavidchvoid
2213255736Sdavidchelink_cb_notify_link_changed(struct bxe_softc *sc)
2214255736Sdavidch{
2215255736Sdavidch    REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2216255736Sdavidch                (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2217255736Sdavidch}
2218255736Sdavidch
2219255736Sdavidch/* send the MCP a request, block until there is a reply */
2220255736Sdavidchuint32_t
2221255736Sdavidchelink_cb_fw_command(struct bxe_softc *sc,
2222255736Sdavidch                    uint32_t         command,
2223255736Sdavidch                    uint32_t         param)
2224255736Sdavidch{
2225255736Sdavidch    int mb_idx = SC_FW_MB_IDX(sc);
2226255736Sdavidch    uint32_t seq;
2227255736Sdavidch    uint32_t rc = 0;
2228255736Sdavidch    uint32_t cnt = 1;
2229255736Sdavidch    uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2230255736Sdavidch
2231255736Sdavidch    BXE_FWMB_LOCK(sc);
2232255736Sdavidch
2233255736Sdavidch    seq = ++sc->fw_seq;
2234255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2235255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2236255736Sdavidch
2237255736Sdavidch    BLOGD(sc, DBG_PHY,
2238255736Sdavidch          "wrote command 0x%08x to FW MB param 0x%08x\n",
2239255736Sdavidch          (command | seq), param);
2240255736Sdavidch
2241255736Sdavidch    /* Let the FW do it's magic. GIve it up to 5 seconds... */
2242255736Sdavidch    do {
2243255736Sdavidch        DELAY(delay * 1000);
2244255736Sdavidch        rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2245255736Sdavidch    } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2246255736Sdavidch
2247255736Sdavidch    BLOGD(sc, DBG_PHY,
2248255736Sdavidch          "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2249255736Sdavidch          cnt*delay, rc, seq);
2250255736Sdavidch
2251255736Sdavidch    /* is this a reply to our command? */
2252255736Sdavidch    if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2253255736Sdavidch        rc &= FW_MSG_CODE_MASK;
2254255736Sdavidch    } else {
2255255736Sdavidch        /* Ruh-roh! */
2256255736Sdavidch        BLOGE(sc, "FW failed to respond!\n");
2257255736Sdavidch        // XXX bxe_fw_dump(sc);
2258255736Sdavidch        rc = 0;
2259255736Sdavidch    }
2260255736Sdavidch
2261255736Sdavidch    BXE_FWMB_UNLOCK(sc);
2262255736Sdavidch    return (rc);
2263255736Sdavidch}
2264255736Sdavidch
2265255736Sdavidchstatic uint32_t
2266255736Sdavidchbxe_fw_command(struct bxe_softc *sc,
2267255736Sdavidch               uint32_t         command,
2268255736Sdavidch               uint32_t         param)
2269255736Sdavidch{
2270255736Sdavidch    return (elink_cb_fw_command(sc, command, param));
2271255736Sdavidch}
2272255736Sdavidch
2273255736Sdavidchstatic void
2274255736Sdavidch__storm_memset_dma_mapping(struct bxe_softc *sc,
2275255736Sdavidch                           uint32_t         addr,
2276255736Sdavidch                           bus_addr_t       mapping)
2277255736Sdavidch{
2278255736Sdavidch    REG_WR(sc, addr, U64_LO(mapping));
2279255736Sdavidch    REG_WR(sc, (addr + 4), U64_HI(mapping));
2280255736Sdavidch}
2281255736Sdavidch
2282255736Sdavidchstatic void
2283255736Sdavidchstorm_memset_spq_addr(struct bxe_softc *sc,
2284255736Sdavidch                      bus_addr_t       mapping,
2285255736Sdavidch                      uint16_t         abs_fid)
2286255736Sdavidch{
2287255736Sdavidch    uint32_t addr = (XSEM_REG_FAST_MEMORY +
2288255736Sdavidch                     XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2289255736Sdavidch    __storm_memset_dma_mapping(sc, addr, mapping);
2290255736Sdavidch}
2291255736Sdavidch
2292255736Sdavidchstatic void
2293255736Sdavidchstorm_memset_vf_to_pf(struct bxe_softc *sc,
2294255736Sdavidch                      uint16_t         abs_fid,
2295255736Sdavidch                      uint16_t         pf_id)
2296255736Sdavidch{
2297255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2298255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2299255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2300255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2301255736Sdavidch}
2302255736Sdavidch
2303255736Sdavidchstatic void
2304255736Sdavidchstorm_memset_func_en(struct bxe_softc *sc,
2305255736Sdavidch                     uint16_t         abs_fid,
2306255736Sdavidch                     uint8_t          enable)
2307255736Sdavidch{
2308255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2309255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2310255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2311255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2312255736Sdavidch}
2313255736Sdavidch
2314255736Sdavidchstatic void
2315255736Sdavidchstorm_memset_eq_data(struct bxe_softc       *sc,
2316255736Sdavidch                     struct event_ring_data *eq_data,
2317255736Sdavidch                     uint16_t               pfid)
2318255736Sdavidch{
2319255736Sdavidch    uint32_t addr;
2320255736Sdavidch    size_t size;
2321255736Sdavidch
2322255736Sdavidch    addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2323255736Sdavidch    size = sizeof(struct event_ring_data);
2324255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2325255736Sdavidch}
2326255736Sdavidch
2327255736Sdavidchstatic void
2328255736Sdavidchstorm_memset_eq_prod(struct bxe_softc *sc,
2329255736Sdavidch                     uint16_t         eq_prod,
2330255736Sdavidch                     uint16_t         pfid)
2331255736Sdavidch{
2332255736Sdavidch    uint32_t addr = (BAR_CSTRORM_INTMEM +
2333255736Sdavidch                     CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2334255736Sdavidch    REG_WR16(sc, addr, eq_prod);
2335255736Sdavidch}
2336255736Sdavidch
2337255736Sdavidch/*
2338255736Sdavidch * Post a slowpath command.
2339255736Sdavidch *
2340255736Sdavidch * A slowpath command is used to propogate a configuration change through
2341255736Sdavidch * the controller in a controlled manner, allowing each STORM processor and
2342255736Sdavidch * other H/W blocks to phase in the change.  The commands sent on the
2343255736Sdavidch * slowpath are referred to as ramrods.  Depending on the ramrod used the
2344255736Sdavidch * completion of the ramrod will occur in different ways.  Here's a
2345255736Sdavidch * breakdown of ramrods and how they complete:
2346255736Sdavidch *
2347255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_SETUP
2348255736Sdavidch *   Used to setup the leading connection on a port.  Completes on the
2349255736Sdavidch *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2350255736Sdavidch *
2351255736Sdavidch * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2352255736Sdavidch *   Used to setup an additional connection on a port.  Completes on the
2353255736Sdavidch *   RCQ of the multi-queue/RSS connection being initialized.
2354255736Sdavidch *
2355255736Sdavidch * RAMROD_CMD_ID_ETH_STAT_QUERY
2356255736Sdavidch *   Used to force the storm processors to update the statistics database
2357255736Sdavidch *   in host memory.  This ramrod is send on the leading connection CID and
2358255736Sdavidch *   completes as an index increment of the CSTORM on the default status
2359255736Sdavidch *   block.
2360255736Sdavidch *
2361255736Sdavidch * RAMROD_CMD_ID_ETH_UPDATE
2362255736Sdavidch *   Used to update the state of the leading connection, usually to udpate
2363255736Sdavidch *   the RSS indirection table.  Completes on the RCQ of the leading
2364255736Sdavidch *   connection. (Not currently used under FreeBSD until OS support becomes
2365255736Sdavidch *   available.)
2366255736Sdavidch *
2367255736Sdavidch * RAMROD_CMD_ID_ETH_HALT
2368255736Sdavidch *   Used when tearing down a connection prior to driver unload.  Completes
2369255736Sdavidch *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2370255736Sdavidch *   use this on the leading connection.
2371255736Sdavidch *
2372255736Sdavidch * RAMROD_CMD_ID_ETH_SET_MAC
2373255736Sdavidch *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2374255736Sdavidch *   the RCQ of the leading connection.
2375255736Sdavidch *
2376255736Sdavidch * RAMROD_CMD_ID_ETH_CFC_DEL
2377255736Sdavidch *   Used when tearing down a conneciton prior to driver unload.  Completes
2378255736Sdavidch *   on the RCQ of the leading connection (since the current connection
2379255736Sdavidch *   has been completely removed from controller memory).
2380255736Sdavidch *
2381255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_DEL
2382255736Sdavidch *   Used to tear down the leading connection prior to driver unload,
2383255736Sdavidch *   typically fp[0].  Completes as an index increment of the CSTORM on the
2384255736Sdavidch *   default status block.
2385255736Sdavidch *
2386255736Sdavidch * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2387255736Sdavidch *   Used for connection offload.  Completes on the RCQ of the multi-queue
2388255736Sdavidch *   RSS connection that is being offloaded.  (Not currently used under
2389255736Sdavidch *   FreeBSD.)
2390255736Sdavidch *
2391255736Sdavidch * There can only be one command pending per function.
2392255736Sdavidch *
2393255736Sdavidch * Returns:
2394255736Sdavidch *   0 = Success, !0 = Failure.
2395255736Sdavidch */
2396255736Sdavidch
2397255736Sdavidch/* must be called under the spq lock */
2398255736Sdavidchstatic inline
2399255736Sdavidchstruct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2400255736Sdavidch{
2401255736Sdavidch    struct eth_spe *next_spe = sc->spq_prod_bd;
2402255736Sdavidch
2403255736Sdavidch    if (sc->spq_prod_bd == sc->spq_last_bd) {
2404255736Sdavidch        /* wrap back to the first eth_spq */
2405255736Sdavidch        sc->spq_prod_bd = sc->spq;
2406255736Sdavidch        sc->spq_prod_idx = 0;
2407255736Sdavidch    } else {
2408255736Sdavidch        sc->spq_prod_bd++;
2409255736Sdavidch        sc->spq_prod_idx++;
2410255736Sdavidch    }
2411255736Sdavidch
2412255736Sdavidch    return (next_spe);
2413255736Sdavidch}
2414255736Sdavidch
2415255736Sdavidch/* must be called under the spq lock */
2416255736Sdavidchstatic inline
2417255736Sdavidchvoid bxe_sp_prod_update(struct bxe_softc *sc)
2418255736Sdavidch{
2419255736Sdavidch    int func = SC_FUNC(sc);
2420255736Sdavidch
2421255736Sdavidch    /*
2422255736Sdavidch     * Make sure that BD data is updated before writing the producer.
2423255736Sdavidch     * BD data is written to the memory, the producer is read from the
2424255736Sdavidch     * memory, thus we need a full memory barrier to ensure the ordering.
2425255736Sdavidch     */
2426255736Sdavidch    mb();
2427255736Sdavidch
2428255736Sdavidch    REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2429255736Sdavidch             sc->spq_prod_idx);
2430255736Sdavidch
2431255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2432255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
2433255736Sdavidch}
2434255736Sdavidch
2435255736Sdavidch/**
2436255736Sdavidch * bxe_is_contextless_ramrod - check if the current command ends on EQ
2437255736Sdavidch *
2438255736Sdavidch * @cmd:      command to check
2439255736Sdavidch * @cmd_type: command type
2440255736Sdavidch */
2441255736Sdavidchstatic inline
2442255736Sdavidchint bxe_is_contextless_ramrod(int cmd,
2443255736Sdavidch                              int cmd_type)
2444255736Sdavidch{
2445255736Sdavidch    if ((cmd_type == NONE_CONNECTION_TYPE) ||
2446255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2447255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2448255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2449255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2450255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2451255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2452255736Sdavidch        return (TRUE);
2453255736Sdavidch    } else {
2454255736Sdavidch        return (FALSE);
2455255736Sdavidch    }
2456255736Sdavidch}
2457255736Sdavidch
2458255736Sdavidch/**
2459255736Sdavidch * bxe_sp_post - place a single command on an SP ring
2460255736Sdavidch *
2461255736Sdavidch * @sc:         driver handle
2462255736Sdavidch * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2463255736Sdavidch * @cid:        SW CID the command is related to
2464255736Sdavidch * @data_hi:    command private data address (high 32 bits)
2465255736Sdavidch * @data_lo:    command private data address (low 32 bits)
2466255736Sdavidch * @cmd_type:   command type (e.g. NONE, ETH)
2467255736Sdavidch *
2468255736Sdavidch * SP data is handled as if it's always an address pair, thus data fields are
2469255736Sdavidch * not swapped to little endian in upper functions. Instead this function swaps
2470255736Sdavidch * data as if it's two uint32 fields.
2471255736Sdavidch */
2472255736Sdavidchint
2473255736Sdavidchbxe_sp_post(struct bxe_softc *sc,
2474255736Sdavidch            int              command,
2475255736Sdavidch            int              cid,
2476255736Sdavidch            uint32_t         data_hi,
2477255736Sdavidch            uint32_t         data_lo,
2478255736Sdavidch            int              cmd_type)
2479255736Sdavidch{
2480255736Sdavidch    struct eth_spe *spe;
2481255736Sdavidch    uint16_t type;
2482255736Sdavidch    int common;
2483255736Sdavidch
2484255736Sdavidch    common = bxe_is_contextless_ramrod(command, cmd_type);
2485255736Sdavidch
2486255736Sdavidch    BXE_SP_LOCK(sc);
2487255736Sdavidch
2488255736Sdavidch    if (common) {
2489255736Sdavidch        if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2490255736Sdavidch            BLOGE(sc, "EQ ring is full!\n");
2491255736Sdavidch            BXE_SP_UNLOCK(sc);
2492255736Sdavidch            return (-1);
2493255736Sdavidch        }
2494255736Sdavidch    } else {
2495255736Sdavidch        if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2496255736Sdavidch            BLOGE(sc, "SPQ ring is full!\n");
2497255736Sdavidch            BXE_SP_UNLOCK(sc);
2498255736Sdavidch            return (-1);
2499255736Sdavidch        }
2500255736Sdavidch    }
2501255736Sdavidch
2502255736Sdavidch    spe = bxe_sp_get_next(sc);
2503255736Sdavidch
2504255736Sdavidch    /* CID needs port number to be encoded int it */
2505255736Sdavidch    spe->hdr.conn_and_cmd_data =
2506255736Sdavidch        htole32((command << SPE_HDR_CMD_ID_SHIFT) | HW_CID(sc, cid));
2507255736Sdavidch
2508255736Sdavidch    type = (cmd_type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
2509255736Sdavidch
2510255736Sdavidch    /* TBD: Check if it works for VFs */
2511255736Sdavidch    type |= ((SC_FUNC(sc) << SPE_HDR_FUNCTION_ID_SHIFT) &
2512255736Sdavidch             SPE_HDR_FUNCTION_ID);
2513255736Sdavidch
2514255736Sdavidch    spe->hdr.type = htole16(type);
2515255736Sdavidch
2516255736Sdavidch    spe->data.update_data_addr.hi = htole32(data_hi);
2517255736Sdavidch    spe->data.update_data_addr.lo = htole32(data_lo);
2518255736Sdavidch
2519255736Sdavidch    /*
2520255736Sdavidch     * It's ok if the actual decrement is issued towards the memory
2521255736Sdavidch     * somewhere between the lock and unlock. Thus no more explict
2522255736Sdavidch     * memory barrier is needed.
2523255736Sdavidch     */
2524255736Sdavidch    if (common) {
2525255736Sdavidch        atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2526255736Sdavidch    } else {
2527255736Sdavidch        atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2528255736Sdavidch    }
2529255736Sdavidch
2530256341Sdim    BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2531256341Sdim    BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2532256341Sdim          BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2533255736Sdavidch    BLOGD(sc, DBG_SP,
2534255736Sdavidch          "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2535255736Sdavidch          sc->spq_prod_idx,
2536255736Sdavidch          (uint32_t)U64_HI(sc->spq_dma.paddr),
2537255736Sdavidch          (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2538255736Sdavidch          command,
2539255736Sdavidch          common,
2540255736Sdavidch          HW_CID(sc, cid),
2541255736Sdavidch          data_hi,
2542255736Sdavidch          data_lo,
2543255736Sdavidch          type,
2544255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left),
2545255736Sdavidch          atomic_load_acq_long(&sc->eq_spq_left));
2546255736Sdavidch
2547255736Sdavidch    bxe_sp_prod_update(sc);
2548255736Sdavidch
2549255736Sdavidch    BXE_SP_UNLOCK(sc);
2550255736Sdavidch    return (0);
2551255736Sdavidch}
2552255736Sdavidch
2553255736Sdavidch/**
2554255736Sdavidch * bxe_debug_print_ind_table - prints the indirection table configuration.
2555255736Sdavidch *
2556255736Sdavidch * @sc: driver hanlde
2557255736Sdavidch * @p:  pointer to rss configuration
2558255736Sdavidch */
2559255736Sdavidch#if 0
2560255736Sdavidchstatic void
2561255736Sdavidchbxe_debug_print_ind_table(struct bxe_softc               *sc,
2562255736Sdavidch                          struct ecore_config_rss_params *p)
2563255736Sdavidch{
2564255736Sdavidch    int i;
2565255736Sdavidch
2566255736Sdavidch    BLOGD(sc, DBG_LOAD, "Setting indirection table to:\n");
2567255736Sdavidch    BLOGD(sc, DBG_LOAD, "    0x0000: ");
2568255736Sdavidch    for (i = 0; i < T_ETH_INDIRECTION_TABLE_SIZE; i++) {
2569255736Sdavidch        BLOGD(sc, DBG_LOAD, "0x%02x ", p->ind_table[i]);
2570255736Sdavidch
2571255736Sdavidch        /* Print 4 bytes in a line */
2572255736Sdavidch        if ((i + 1 < T_ETH_INDIRECTION_TABLE_SIZE) &&
2573255736Sdavidch            (((i + 1) & 0x3) == 0)) {
2574255736Sdavidch            BLOGD(sc, DBG_LOAD, "\n");
2575255736Sdavidch            BLOGD(sc, DBG_LOAD, "0x%04x: ", i + 1);
2576255736Sdavidch        }
2577255736Sdavidch    }
2578255736Sdavidch
2579255736Sdavidch    BLOGD(sc, DBG_LOAD, "\n");
2580255736Sdavidch}
2581255736Sdavidch#endif
2582255736Sdavidch
2583255736Sdavidch/*
2584255736Sdavidch * FreeBSD Device probe function.
2585255736Sdavidch *
2586255736Sdavidch * Compares the device found to the driver's list of supported devices and
2587255736Sdavidch * reports back to the bsd loader whether this is the right driver for the device.
2588255736Sdavidch * This is the driver entry function called from the "kldload" command.
2589255736Sdavidch *
2590255736Sdavidch * Returns:
2591255736Sdavidch *   BUS_PROBE_DEFAULT on success, positive value on failure.
2592255736Sdavidch */
2593255736Sdavidchstatic int
2594255736Sdavidchbxe_probe(device_t dev)
2595255736Sdavidch{
2596255736Sdavidch    struct bxe_softc *sc;
2597255736Sdavidch    struct bxe_device_type *t;
2598255736Sdavidch    char *descbuf;
2599255736Sdavidch    uint16_t did, sdid, svid, vid;
2600255736Sdavidch
2601255736Sdavidch    /* Find our device structure */
2602255736Sdavidch    sc = device_get_softc(dev);
2603255736Sdavidch    sc->dev = dev;
2604255736Sdavidch    t = bxe_devs;
2605255736Sdavidch
2606255736Sdavidch    /* Get the data for the device to be probed. */
2607255736Sdavidch    vid  = pci_get_vendor(dev);
2608255736Sdavidch    did  = pci_get_device(dev);
2609255736Sdavidch    svid = pci_get_subvendor(dev);
2610255736Sdavidch    sdid = pci_get_subdevice(dev);
2611255736Sdavidch
2612255736Sdavidch    BLOGD(sc, DBG_LOAD,
2613255736Sdavidch          "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
2614255736Sdavidch          "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
2615255736Sdavidch
2616255736Sdavidch    /* Look through the list of known devices for a match. */
2617255736Sdavidch    while (t->bxe_name != NULL) {
2618255736Sdavidch        if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2619255736Sdavidch            ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2620255736Sdavidch            ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2621255736Sdavidch            descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2622255736Sdavidch            if (descbuf == NULL)
2623255736Sdavidch                return (ENOMEM);
2624255736Sdavidch
2625255736Sdavidch            /* Print out the device identity. */
2626255736Sdavidch            snprintf(descbuf, BXE_DEVDESC_MAX,
2627255736Sdavidch                     "%s (%c%d) BXE v:%s\n", t->bxe_name,
2628255736Sdavidch                     (((pci_read_config(dev, PCIR_REVID, 4) &
2629255736Sdavidch                        0xf0) >> 4) + 'A'),
2630255736Sdavidch                     (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2631255736Sdavidch                     BXE_DRIVER_VERSION);
2632255736Sdavidch
2633255736Sdavidch            device_set_desc_copy(dev, descbuf);
2634255736Sdavidch            free(descbuf, M_TEMP);
2635255736Sdavidch            return (BUS_PROBE_DEFAULT);
2636255736Sdavidch        }
2637255736Sdavidch        t++;
2638255736Sdavidch    }
2639255736Sdavidch
2640255736Sdavidch    return (ENXIO);
2641255736Sdavidch}
2642255736Sdavidch
2643255736Sdavidchstatic void
2644255736Sdavidchbxe_init_mutexes(struct bxe_softc *sc)
2645255736Sdavidch{
2646255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2647255736Sdavidch    snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2648255736Sdavidch             "bxe%d_core_lock", sc->unit);
2649255736Sdavidch    sx_init(&sc->core_sx, sc->core_sx_name);
2650255736Sdavidch#else
2651255736Sdavidch    snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2652255736Sdavidch             "bxe%d_core_lock", sc->unit);
2653255736Sdavidch    mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2654255736Sdavidch#endif
2655255736Sdavidch
2656255736Sdavidch    snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2657255736Sdavidch             "bxe%d_sp_lock", sc->unit);
2658255736Sdavidch    mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2659255736Sdavidch
2660255736Sdavidch    snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2661255736Sdavidch             "bxe%d_dmae_lock", sc->unit);
2662255736Sdavidch    mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2663255736Sdavidch
2664255736Sdavidch    snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2665255736Sdavidch             "bxe%d_phy_lock", sc->unit);
2666255736Sdavidch    mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2667255736Sdavidch
2668255736Sdavidch    snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2669255736Sdavidch             "bxe%d_fwmb_lock", sc->unit);
2670255736Sdavidch    mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2671255736Sdavidch
2672255736Sdavidch    snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2673255736Sdavidch             "bxe%d_print_lock", sc->unit);
2674255736Sdavidch    mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2675255736Sdavidch
2676255736Sdavidch    snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2677255736Sdavidch             "bxe%d_stats_lock", sc->unit);
2678255736Sdavidch    mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2679255736Sdavidch
2680255736Sdavidch    snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2681255736Sdavidch             "bxe%d_mcast_lock", sc->unit);
2682255736Sdavidch    mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2683255736Sdavidch}
2684255736Sdavidch
2685255736Sdavidchstatic void
2686255736Sdavidchbxe_release_mutexes(struct bxe_softc *sc)
2687255736Sdavidch{
2688255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2689255736Sdavidch    sx_destroy(&sc->core_sx);
2690255736Sdavidch#else
2691255736Sdavidch    if (mtx_initialized(&sc->core_mtx)) {
2692255736Sdavidch        mtx_destroy(&sc->core_mtx);
2693255736Sdavidch    }
2694255736Sdavidch#endif
2695255736Sdavidch
2696255736Sdavidch    if (mtx_initialized(&sc->sp_mtx)) {
2697255736Sdavidch        mtx_destroy(&sc->sp_mtx);
2698255736Sdavidch    }
2699255736Sdavidch
2700255736Sdavidch    if (mtx_initialized(&sc->dmae_mtx)) {
2701255736Sdavidch        mtx_destroy(&sc->dmae_mtx);
2702255736Sdavidch    }
2703255736Sdavidch
2704255736Sdavidch    if (mtx_initialized(&sc->port.phy_mtx)) {
2705255736Sdavidch        mtx_destroy(&sc->port.phy_mtx);
2706255736Sdavidch    }
2707255736Sdavidch
2708255736Sdavidch    if (mtx_initialized(&sc->fwmb_mtx)) {
2709255736Sdavidch        mtx_destroy(&sc->fwmb_mtx);
2710255736Sdavidch    }
2711255736Sdavidch
2712255736Sdavidch    if (mtx_initialized(&sc->print_mtx)) {
2713255736Sdavidch        mtx_destroy(&sc->print_mtx);
2714255736Sdavidch    }
2715255736Sdavidch
2716255736Sdavidch    if (mtx_initialized(&sc->stats_mtx)) {
2717255736Sdavidch        mtx_destroy(&sc->stats_mtx);
2718255736Sdavidch    }
2719255736Sdavidch
2720255736Sdavidch    if (mtx_initialized(&sc->mcast_mtx)) {
2721255736Sdavidch        mtx_destroy(&sc->mcast_mtx);
2722255736Sdavidch    }
2723255736Sdavidch}
2724255736Sdavidch
2725255736Sdavidchstatic void
2726255736Sdavidchbxe_tx_disable(struct bxe_softc* sc)
2727255736Sdavidch{
2728266979Smarcel    if_t ifp = sc->ifp;
2729255736Sdavidch
2730255736Sdavidch    /* tell the stack the driver is stopped and TX queue is full */
2731266979Smarcel    if (ifp !=  NULL) {
2732266979Smarcel        if_setdrvflags(ifp, 0);
2733255736Sdavidch    }
2734255736Sdavidch}
2735255736Sdavidch
2736255736Sdavidchstatic void
2737255736Sdavidchbxe_drv_pulse(struct bxe_softc *sc)
2738255736Sdavidch{
2739255736Sdavidch    SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2740255736Sdavidch             sc->fw_drv_pulse_wr_seq);
2741255736Sdavidch}
2742255736Sdavidch
2743255736Sdavidchstatic inline uint16_t
2744255736Sdavidchbxe_tx_avail(struct bxe_softc *sc,
2745255736Sdavidch             struct bxe_fastpath *fp)
2746255736Sdavidch{
2747255736Sdavidch    int16_t  used;
2748255736Sdavidch    uint16_t prod;
2749255736Sdavidch    uint16_t cons;
2750255736Sdavidch
2751255736Sdavidch    prod = fp->tx_bd_prod;
2752255736Sdavidch    cons = fp->tx_bd_cons;
2753255736Sdavidch
2754255736Sdavidch    used = SUB_S16(prod, cons);
2755255736Sdavidch
2756255736Sdavidch#if 0
2757255736Sdavidch    KASSERT((used < 0), ("used tx bds < 0"));
2758255736Sdavidch    KASSERT((used > sc->tx_ring_size), ("used tx bds > tx_ring_size"));
2759255736Sdavidch    KASSERT(((sc->tx_ring_size - used) > MAX_TX_AVAIL),
2760255736Sdavidch            ("invalid number of tx bds used"));
2761255736Sdavidch#endif
2762255736Sdavidch
2763255736Sdavidch    return (int16_t)(sc->tx_ring_size) - used;
2764255736Sdavidch}
2765255736Sdavidch
2766255736Sdavidchstatic inline int
2767255736Sdavidchbxe_tx_queue_has_work(struct bxe_fastpath *fp)
2768255736Sdavidch{
2769255736Sdavidch    uint16_t hw_cons;
2770255736Sdavidch
2771255736Sdavidch    mb(); /* status block fields can change */
2772255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
2773255736Sdavidch    return (hw_cons != fp->tx_pkt_cons);
2774255736Sdavidch}
2775255736Sdavidch
2776255736Sdavidchstatic inline uint8_t
2777255736Sdavidchbxe_has_tx_work(struct bxe_fastpath *fp)
2778255736Sdavidch{
2779255736Sdavidch    /* expand this for multi-cos if ever supported */
2780255736Sdavidch    return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2781255736Sdavidch}
2782255736Sdavidch
2783255736Sdavidchstatic inline int
2784255736Sdavidchbxe_has_rx_work(struct bxe_fastpath *fp)
2785255736Sdavidch{
2786255736Sdavidch    uint16_t rx_cq_cons_sb;
2787255736Sdavidch
2788255736Sdavidch    mb(); /* status block fields can change */
2789255736Sdavidch    rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2790255736Sdavidch    if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2791255736Sdavidch        rx_cq_cons_sb++;
2792255736Sdavidch    return (fp->rx_cq_cons != rx_cq_cons_sb);
2793255736Sdavidch}
2794255736Sdavidch
2795255736Sdavidchstatic void
2796255736Sdavidchbxe_sp_event(struct bxe_softc    *sc,
2797255736Sdavidch             struct bxe_fastpath *fp,
2798255736Sdavidch             union eth_rx_cqe    *rr_cqe)
2799255736Sdavidch{
2800255736Sdavidch    int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2801255736Sdavidch    int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2802255736Sdavidch    enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2803255736Sdavidch    struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2804255736Sdavidch
2805255736Sdavidch    BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2806255736Sdavidch          fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2807255736Sdavidch
2808255736Sdavidch#if 0
2809255736Sdavidch    /*
2810255736Sdavidch     * If cid is within VF range, replace the slowpath object with the
2811255736Sdavidch     * one corresponding to this VF
2812255736Sdavidch     */
2813255736Sdavidch    if ((cid >= BXE_FIRST_VF_CID) && (cid < BXE_FIRST_VF_CID + BXE_VF_CIDS)) {
2814255736Sdavidch        bxe_iov_set_queue_sp_obj(sc, cid, &q_obj);
2815255736Sdavidch    }
2816255736Sdavidch#endif
2817255736Sdavidch
2818255736Sdavidch    switch (command) {
2819255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2820255736Sdavidch        BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2821255736Sdavidch        drv_cmd = ECORE_Q_CMD_UPDATE;
2822255736Sdavidch        break;
2823255736Sdavidch
2824255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2825255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2826255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP;
2827255736Sdavidch        break;
2828255736Sdavidch
2829255736Sdavidch    case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2830255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2831255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2832255736Sdavidch        break;
2833255736Sdavidch
2834255736Sdavidch    case (RAMROD_CMD_ID_ETH_HALT):
2835255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2836255736Sdavidch        drv_cmd = ECORE_Q_CMD_HALT;
2837255736Sdavidch        break;
2838255736Sdavidch
2839255736Sdavidch    case (RAMROD_CMD_ID_ETH_TERMINATE):
2840255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2841255736Sdavidch        drv_cmd = ECORE_Q_CMD_TERMINATE;
2842255736Sdavidch        break;
2843255736Sdavidch
2844255736Sdavidch    case (RAMROD_CMD_ID_ETH_EMPTY):
2845255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2846255736Sdavidch        drv_cmd = ECORE_Q_CMD_EMPTY;
2847255736Sdavidch        break;
2848255736Sdavidch
2849255736Sdavidch    default:
2850255736Sdavidch        BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2851255736Sdavidch              command, fp->index);
2852255736Sdavidch        return;
2853255736Sdavidch    }
2854255736Sdavidch
2855255736Sdavidch    if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2856255736Sdavidch        q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2857255736Sdavidch        /*
2858255736Sdavidch         * q_obj->complete_cmd() failure means that this was
2859255736Sdavidch         * an unexpected completion.
2860255736Sdavidch         *
2861255736Sdavidch         * In this case we don't want to increase the sc->spq_left
2862255736Sdavidch         * because apparently we haven't sent this command the first
2863255736Sdavidch         * place.
2864255736Sdavidch         */
2865255736Sdavidch        // bxe_panic(sc, ("Unexpected SP completion\n"));
2866255736Sdavidch        return;
2867255736Sdavidch    }
2868255736Sdavidch
2869255736Sdavidch#if 0
2870255736Sdavidch    /* SRIOV: reschedule any 'in_progress' operations */
2871255736Sdavidch    bxe_iov_sp_event(sc, cid, TRUE);
2872255736Sdavidch#endif
2873255736Sdavidch
2874255736Sdavidch    atomic_add_acq_long(&sc->cq_spq_left, 1);
2875255736Sdavidch
2876255736Sdavidch    BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2877255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left));
2878255736Sdavidch
2879255736Sdavidch#if 0
2880255736Sdavidch    if ((drv_cmd == ECORE_Q_CMD_UPDATE) && (IS_FCOE_FP(fp)) &&
2881255736Sdavidch        (!!bxe_test_bit(ECORE_AFEX_FCOE_Q_UPDATE_PENDING, &sc->sp_state))) {
2882255736Sdavidch        /*
2883255736Sdavidch         * If Queue update ramrod is completed for last Queue in AFEX VIF set
2884255736Sdavidch         * flow, then ACK MCP at the end. Mark pending ACK to MCP bit to
2885255736Sdavidch         * prevent case that both bits are cleared. At the end of load/unload
2886255736Sdavidch         * driver checks that sp_state is cleared and this order prevents
2887255736Sdavidch         * races.
2888255736Sdavidch         */
2889255736Sdavidch        bxe_set_bit(ECORE_AFEX_PENDING_VIFSET_MCP_ACK, &sc->sp_state);
2890255736Sdavidch        wmb();
2891255736Sdavidch        bxe_clear_bit(ECORE_AFEX_FCOE_Q_UPDATE_PENDING, &sc->sp_state);
2892255736Sdavidch
2893255736Sdavidch        /* schedule the sp task as MCP ack is required */
2894255736Sdavidch        bxe_schedule_sp_task(sc);
2895255736Sdavidch    }
2896255736Sdavidch#endif
2897255736Sdavidch}
2898255736Sdavidch
2899255736Sdavidch/*
2900255736Sdavidch * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2901255736Sdavidch * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2902255736Sdavidch * the current aggregation queue as in-progress.
2903255736Sdavidch */
2904255736Sdavidchstatic void
2905255736Sdavidchbxe_tpa_start(struct bxe_softc            *sc,
2906255736Sdavidch              struct bxe_fastpath         *fp,
2907255736Sdavidch              uint16_t                    queue,
2908255736Sdavidch              uint16_t                    cons,
2909255736Sdavidch              uint16_t                    prod,
2910255736Sdavidch              struct eth_fast_path_rx_cqe *cqe)
2911255736Sdavidch{
2912255736Sdavidch    struct bxe_sw_rx_bd tmp_bd;
2913255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
2914255736Sdavidch    struct eth_rx_bd *rx_bd;
2915255736Sdavidch    int max_agg_queues;
2916255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2917255736Sdavidch    uint16_t index;
2918255736Sdavidch
2919255736Sdavidch    BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2920255736Sdavidch                       "cons=%d prod=%d\n",
2921255736Sdavidch          fp->index, queue, cons, prod);
2922255736Sdavidch
2923255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
2924255736Sdavidch
2925255736Sdavidch    KASSERT((queue < max_agg_queues),
2926255736Sdavidch            ("fp[%02d] invalid aggr queue (%d >= %d)!",
2927255736Sdavidch             fp->index, queue, max_agg_queues));
2928255736Sdavidch
2929255736Sdavidch    KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2930255736Sdavidch            ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2931255736Sdavidch             fp->index, queue));
2932255736Sdavidch
2933255736Sdavidch    /* copy the existing mbuf and mapping from the TPA pool */
2934255736Sdavidch    tmp_bd = tpa_info->bd;
2935255736Sdavidch
2936255736Sdavidch    if (tmp_bd.m == NULL) {
2937255736Sdavidch        BLOGE(sc, "fp[%02d].tpa[%02d] mbuf not allocated!\n",
2938255736Sdavidch              fp->index, queue);
2939255736Sdavidch        /* XXX Error handling? */
2940255736Sdavidch        return;
2941255736Sdavidch    }
2942255736Sdavidch
2943255736Sdavidch    /* change the TPA queue to the start state */
2944255736Sdavidch    tpa_info->state            = BXE_TPA_STATE_START;
2945255736Sdavidch    tpa_info->placement_offset = cqe->placement_offset;
2946255736Sdavidch    tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2947255736Sdavidch    tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2948255736Sdavidch    tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2949255736Sdavidch
2950255736Sdavidch    fp->rx_tpa_queue_used |= (1 << queue);
2951255736Sdavidch
2952255736Sdavidch    /*
2953255736Sdavidch     * If all the buffer descriptors are filled with mbufs then fill in
2954255736Sdavidch     * the current consumer index with a new BD. Else if a maximum Rx
2955255736Sdavidch     * buffer limit is imposed then fill in the next producer index.
2956255736Sdavidch     */
2957255736Sdavidch    index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2958255736Sdavidch                prod : cons;
2959255736Sdavidch
2960255736Sdavidch    /* move the received mbuf and mapping to TPA pool */
2961255736Sdavidch    tpa_info->bd = fp->rx_mbuf_chain[cons];
2962255736Sdavidch
2963255736Sdavidch    /* release any existing RX BD mbuf mappings */
2964255736Sdavidch    if (cons != index) {
2965255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[cons];
2966255736Sdavidch
2967255736Sdavidch        if (rx_buf->m_map != NULL) {
2968255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2969255736Sdavidch                            BUS_DMASYNC_POSTREAD);
2970255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2971255736Sdavidch        }
2972255736Sdavidch
2973255736Sdavidch        /*
2974255736Sdavidch         * We get here when the maximum number of rx buffers is less than
2975255736Sdavidch         * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2976255736Sdavidch         * it out here without concern of a memory leak.
2977255736Sdavidch         */
2978255736Sdavidch        fp->rx_mbuf_chain[cons].m = NULL;
2979255736Sdavidch    }
2980255736Sdavidch
2981255736Sdavidch    /* update the Rx SW BD with the mbuf info from the TPA pool */
2982255736Sdavidch    fp->rx_mbuf_chain[index] = tmp_bd;
2983255736Sdavidch
2984255736Sdavidch    /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2985255736Sdavidch    rx_bd = &fp->rx_chain[index];
2986255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2987255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2988255736Sdavidch}
2989255736Sdavidch
2990255736Sdavidch/*
2991255736Sdavidch * When a TPA aggregation is completed, loop through the individual mbufs
2992255736Sdavidch * of the aggregation, combining them into a single mbuf which will be sent
2993255736Sdavidch * up the stack. Refill all freed SGEs with mbufs as we go along.
2994255736Sdavidch */
2995255736Sdavidchstatic int
2996255736Sdavidchbxe_fill_frag_mbuf(struct bxe_softc          *sc,
2997255736Sdavidch                   struct bxe_fastpath       *fp,
2998255736Sdavidch                   struct bxe_sw_tpa_info    *tpa_info,
2999255736Sdavidch                   uint16_t                  queue,
3000255736Sdavidch                   uint16_t                  pages,
3001255736Sdavidch                   struct mbuf               *m,
3002255736Sdavidch			       struct eth_end_agg_rx_cqe *cqe,
3003255736Sdavidch                   uint16_t                  cqe_idx)
3004255736Sdavidch{
3005255736Sdavidch    struct mbuf *m_frag;
3006255736Sdavidch    uint32_t frag_len, frag_size, i;
3007255736Sdavidch    uint16_t sge_idx;
3008255736Sdavidch    int rc = 0;
3009255736Sdavidch    int j;
3010255736Sdavidch
3011255736Sdavidch    frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
3012255736Sdavidch
3013255736Sdavidch    BLOGD(sc, DBG_LRO,
3014255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
3015255736Sdavidch          fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
3016255736Sdavidch
3017255736Sdavidch    /* make sure the aggregated frame is not too big to handle */
3018255736Sdavidch    if (pages > 8 * PAGES_PER_SGE) {
3019255736Sdavidch        BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
3020255736Sdavidch                  "pkt_len=%d len_on_bd=%d frag_size=%d\n",
3021255736Sdavidch              fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
3022255736Sdavidch              tpa_info->len_on_bd, frag_size);
3023255736Sdavidch        bxe_panic(sc, ("sge page count error\n"));
3024255736Sdavidch        return (EINVAL);
3025255736Sdavidch    }
3026255736Sdavidch
3027255736Sdavidch    /*
3028255736Sdavidch     * Scan through the scatter gather list pulling individual mbufs into a
3029255736Sdavidch     * single mbuf for the host stack.
3030255736Sdavidch     */
3031255736Sdavidch    for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
3032255736Sdavidch        sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
3033255736Sdavidch
3034255736Sdavidch        /*
3035255736Sdavidch         * Firmware gives the indices of the SGE as if the ring is an array
3036255736Sdavidch         * (meaning that the "next" element will consume 2 indices).
3037255736Sdavidch         */
3038255736Sdavidch        frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
3039255736Sdavidch
3040255736Sdavidch        BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
3041255736Sdavidch                           "sge_idx=%d frag_size=%d frag_len=%d\n",
3042255736Sdavidch              fp->index, queue, i, j, sge_idx, frag_size, frag_len);
3043255736Sdavidch
3044255736Sdavidch        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3045255736Sdavidch
3046255736Sdavidch        /* allocate a new mbuf for the SGE */
3047255736Sdavidch        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3048255736Sdavidch        if (rc) {
3049255736Sdavidch            /* Leave all remaining SGEs in the ring! */
3050255736Sdavidch            return (rc);
3051255736Sdavidch        }
3052255736Sdavidch
3053255736Sdavidch        /* update the fragment length */
3054255736Sdavidch        m_frag->m_len = frag_len;
3055255736Sdavidch
3056255736Sdavidch        /* concatenate the fragment to the head mbuf */
3057255736Sdavidch        m_cat(m, m_frag);
3058255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
3059255736Sdavidch
3060255736Sdavidch        /* update the TPA mbuf size and remaining fragment size */
3061255736Sdavidch        m->m_pkthdr.len += frag_len;
3062255736Sdavidch        frag_size -= frag_len;
3063255736Sdavidch    }
3064255736Sdavidch
3065255736Sdavidch    BLOGD(sc, DBG_LRO,
3066255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
3067255736Sdavidch          fp->index, queue, frag_size);
3068255736Sdavidch
3069255736Sdavidch    return (rc);
3070255736Sdavidch}
3071255736Sdavidch
3072255736Sdavidchstatic inline void
3073255736Sdavidchbxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
3074255736Sdavidch{
3075255736Sdavidch    int i, j;
3076255736Sdavidch
3077255736Sdavidch    for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
3078255736Sdavidch        int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
3079255736Sdavidch
3080255736Sdavidch        for (j = 0; j < 2; j++) {
3081255736Sdavidch            BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
3082255736Sdavidch            idx--;
3083255736Sdavidch        }
3084255736Sdavidch    }
3085255736Sdavidch}
3086255736Sdavidch
3087255736Sdavidchstatic inline void
3088255736Sdavidchbxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
3089255736Sdavidch{
3090255736Sdavidch    /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
3091255736Sdavidch    memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
3092255736Sdavidch
3093255736Sdavidch    /*
3094255736Sdavidch     * Clear the two last indices in the page to 1. These are the indices that
3095255736Sdavidch     * correspond to the "next" element, hence will never be indicated and
3096255736Sdavidch     * should be removed from the calculations.
3097255736Sdavidch     */
3098255736Sdavidch    bxe_clear_sge_mask_next_elems(fp);
3099255736Sdavidch}
3100255736Sdavidch
3101255736Sdavidchstatic inline void
3102255736Sdavidchbxe_update_last_max_sge(struct bxe_fastpath *fp,
3103255736Sdavidch                        uint16_t            idx)
3104255736Sdavidch{
3105255736Sdavidch    uint16_t last_max = fp->last_max_sge;
3106255736Sdavidch
3107255736Sdavidch    if (SUB_S16(idx, last_max) > 0) {
3108255736Sdavidch        fp->last_max_sge = idx;
3109255736Sdavidch    }
3110255736Sdavidch}
3111255736Sdavidch
3112255736Sdavidchstatic inline void
3113255736Sdavidchbxe_update_sge_prod(struct bxe_softc          *sc,
3114255736Sdavidch                    struct bxe_fastpath       *fp,
3115255736Sdavidch                    uint16_t                  sge_len,
3116283269Sdavidcs                    union eth_sgl_or_raw_data *cqe)
3117255736Sdavidch{
3118255736Sdavidch    uint16_t last_max, last_elem, first_elem;
3119255736Sdavidch    uint16_t delta = 0;
3120255736Sdavidch    uint16_t i;
3121255736Sdavidch
3122255736Sdavidch    if (!sge_len) {
3123255736Sdavidch        return;
3124255736Sdavidch    }
3125255736Sdavidch
3126255736Sdavidch    /* first mark all used pages */
3127255736Sdavidch    for (i = 0; i < sge_len; i++) {
3128255736Sdavidch        BIT_VEC64_CLEAR_BIT(fp->sge_mask,
3129283269Sdavidcs                            RX_SGE(le16toh(cqe->sgl[i])));
3130255736Sdavidch    }
3131255736Sdavidch
3132255736Sdavidch    BLOGD(sc, DBG_LRO,
3133255736Sdavidch          "fp[%02d] fp_cqe->sgl[%d] = %d\n",
3134255736Sdavidch          fp->index, sge_len - 1,
3135283269Sdavidcs          le16toh(cqe->sgl[sge_len - 1]));
3136255736Sdavidch
3137255736Sdavidch    /* assume that the last SGE index is the biggest */
3138255736Sdavidch    bxe_update_last_max_sge(fp,
3139283269Sdavidcs                            le16toh(cqe->sgl[sge_len - 1]));
3140255736Sdavidch
3141255736Sdavidch    last_max = RX_SGE(fp->last_max_sge);
3142255736Sdavidch    last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
3143255736Sdavidch    first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
3144255736Sdavidch
3145255736Sdavidch    /* if ring is not full */
3146255736Sdavidch    if (last_elem + 1 != first_elem) {
3147255736Sdavidch        last_elem++;
3148255736Sdavidch    }
3149255736Sdavidch
3150255736Sdavidch    /* now update the prod */
3151255736Sdavidch    for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
3152255736Sdavidch        if (__predict_true(fp->sge_mask[i])) {
3153255736Sdavidch            break;
3154255736Sdavidch        }
3155255736Sdavidch
3156255736Sdavidch        fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
3157255736Sdavidch        delta += BIT_VEC64_ELEM_SZ;
3158255736Sdavidch    }
3159255736Sdavidch
3160255736Sdavidch    if (delta > 0) {
3161255736Sdavidch        fp->rx_sge_prod += delta;
3162255736Sdavidch        /* clear page-end entries */
3163255736Sdavidch        bxe_clear_sge_mask_next_elems(fp);
3164255736Sdavidch    }
3165255736Sdavidch
3166255736Sdavidch    BLOGD(sc, DBG_LRO,
3167255736Sdavidch          "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
3168255736Sdavidch          fp->index, fp->last_max_sge, fp->rx_sge_prod);
3169255736Sdavidch}
3170255736Sdavidch
3171255736Sdavidch/*
3172255736Sdavidch * The aggregation on the current TPA queue has completed. Pull the individual
3173255736Sdavidch * mbuf fragments together into a single mbuf, perform all necessary checksum
3174255736Sdavidch * calculations, and send the resuting mbuf to the stack.
3175255736Sdavidch */
3176255736Sdavidchstatic void
3177255736Sdavidchbxe_tpa_stop(struct bxe_softc          *sc,
3178255736Sdavidch             struct bxe_fastpath       *fp,
3179255736Sdavidch             struct bxe_sw_tpa_info    *tpa_info,
3180255736Sdavidch             uint16_t                  queue,
3181255736Sdavidch             uint16_t                  pages,
3182255736Sdavidch			 struct eth_end_agg_rx_cqe *cqe,
3183255736Sdavidch             uint16_t                  cqe_idx)
3184255736Sdavidch{
3185266979Smarcel    if_t ifp = sc->ifp;
3186255736Sdavidch    struct mbuf *m;
3187255736Sdavidch    int rc = 0;
3188255736Sdavidch
3189255736Sdavidch    BLOGD(sc, DBG_LRO,
3190255736Sdavidch          "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3191255736Sdavidch          fp->index, queue, tpa_info->placement_offset,
3192255736Sdavidch          le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3193255736Sdavidch
3194255736Sdavidch    m = tpa_info->bd.m;
3195255736Sdavidch
3196255736Sdavidch    /* allocate a replacement before modifying existing mbuf */
3197255736Sdavidch    rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3198255736Sdavidch    if (rc) {
3199255736Sdavidch        /* drop the frame and log an error */
3200255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3201255736Sdavidch        goto bxe_tpa_stop_exit;
3202255736Sdavidch    }
3203255736Sdavidch
3204255736Sdavidch    /* we have a replacement, fixup the current mbuf */
3205255736Sdavidch    m_adj(m, tpa_info->placement_offset);
3206255736Sdavidch    m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3207255736Sdavidch
3208255736Sdavidch    /* mark the checksums valid (taken care of by the firmware) */
3209255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3210255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3211255736Sdavidch    m->m_pkthdr.csum_data = 0xffff;
3212255736Sdavidch    m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3213255736Sdavidch                               CSUM_IP_VALID   |
3214255736Sdavidch                               CSUM_DATA_VALID |
3215255736Sdavidch                               CSUM_PSEUDO_HDR);
3216255736Sdavidch
3217255736Sdavidch    /* aggregate all of the SGEs into a single mbuf */
3218255736Sdavidch    rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3219255736Sdavidch    if (rc) {
3220255736Sdavidch        /* drop the packet and log an error */
3221255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3222255736Sdavidch        m_freem(m);
3223255736Sdavidch    } else {
3224255736Sdavidch        if (tpa_info->parsing_flags & PARSING_FLAGS_VLAN) {
3225255736Sdavidch            m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3226255736Sdavidch            m->m_flags |= M_VLANTAG;
3227255736Sdavidch        }
3228255736Sdavidch
3229255736Sdavidch        /* assign packet to this interface interface */
3230266979Smarcel        if_setrcvif(m, ifp);
3231255736Sdavidch
3232255736Sdavidch#if __FreeBSD_version >= 800000
3233255736Sdavidch        /* specify what RSS queue was used for this flow */
3234255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3235275358Shselasky        M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
3236255736Sdavidch#endif
3237255736Sdavidch
3238271782Sglebius        if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3239255736Sdavidch        fp->eth_q_stats.rx_tpa_pkts++;
3240255736Sdavidch
3241255736Sdavidch        /* pass the frame to the stack */
3242266979Smarcel        if_input(ifp, m);
3243255736Sdavidch    }
3244255736Sdavidch
3245255736Sdavidch    /* we passed an mbuf up the stack or dropped the frame */
3246255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa--;
3247255736Sdavidch
3248255736Sdavidchbxe_tpa_stop_exit:
3249255736Sdavidch
3250255736Sdavidch    fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3251255736Sdavidch    fp->rx_tpa_queue_used &= ~(1 << queue);
3252255736Sdavidch}
3253255736Sdavidch
3254255736Sdavidchstatic uint8_t
3255283269Sdavidcsbxe_service_rxsgl(
3256283269Sdavidcs                 struct bxe_fastpath *fp,
3257283269Sdavidcs                 uint16_t len,
3258283269Sdavidcs                 uint16_t lenonbd,
3259283269Sdavidcs                 struct mbuf *m,
3260283269Sdavidcs                 struct eth_fast_path_rx_cqe *cqe_fp)
3261283269Sdavidcs{
3262283269Sdavidcs    struct mbuf *m_frag;
3263283269Sdavidcs    uint16_t frags, frag_len;
3264283269Sdavidcs    uint16_t sge_idx = 0;
3265283269Sdavidcs    uint16_t j;
3266283269Sdavidcs    uint8_t i, rc = 0;
3267283269Sdavidcs    uint32_t frag_size;
3268283269Sdavidcs
3269283269Sdavidcs    /* adjust the mbuf */
3270283269Sdavidcs    m->m_len = lenonbd;
3271283269Sdavidcs
3272283269Sdavidcs    frag_size =  len - lenonbd;
3273283269Sdavidcs    frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3274283269Sdavidcs
3275283269Sdavidcs    for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3276283269Sdavidcs        sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3277283269Sdavidcs
3278283269Sdavidcs        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3279283269Sdavidcs        frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3280283269Sdavidcs        m_frag->m_len = frag_len;
3281283269Sdavidcs
3282283269Sdavidcs       /* allocate a new mbuf for the SGE */
3283283269Sdavidcs        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3284283269Sdavidcs        if (rc) {
3285283269Sdavidcs            /* Leave all remaining SGEs in the ring! */
3286283269Sdavidcs            return (rc);
3287283269Sdavidcs        }
3288283269Sdavidcs        fp->eth_q_stats.mbuf_alloc_sge--;
3289283269Sdavidcs
3290283269Sdavidcs        /* concatenate the fragment to the head mbuf */
3291283269Sdavidcs        m_cat(m, m_frag);
3292283269Sdavidcs
3293283269Sdavidcs        frag_size -= frag_len;
3294283269Sdavidcs    }
3295283269Sdavidcs
3296283269Sdavidcs    bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3297283269Sdavidcs
3298283269Sdavidcs    return rc;
3299283269Sdavidcs}
3300283269Sdavidcs
3301283269Sdavidcsstatic uint8_t
3302255736Sdavidchbxe_rxeof(struct bxe_softc    *sc,
3303255736Sdavidch          struct bxe_fastpath *fp)
3304255736Sdavidch{
3305266979Smarcel    if_t ifp = sc->ifp;
3306255736Sdavidch    uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3307255736Sdavidch    uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3308255736Sdavidch    int rx_pkts = 0;
3309281006Sdavidcs    int rc = 0;
3310255736Sdavidch
3311255736Sdavidch    BXE_FP_RX_LOCK(fp);
3312255736Sdavidch
3313255736Sdavidch    /* CQ "next element" is of the size of the regular element */
3314255736Sdavidch    hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3315255736Sdavidch    if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3316255736Sdavidch        hw_cq_cons++;
3317255736Sdavidch    }
3318255736Sdavidch
3319255736Sdavidch    bd_cons = fp->rx_bd_cons;
3320255736Sdavidch    bd_prod = fp->rx_bd_prod;
3321255736Sdavidch    bd_prod_fw = bd_prod;
3322255736Sdavidch    sw_cq_cons = fp->rx_cq_cons;
3323255736Sdavidch    sw_cq_prod = fp->rx_cq_prod;
3324255736Sdavidch
3325255736Sdavidch    /*
3326255736Sdavidch     * Memory barrier necessary as speculative reads of the rx
3327255736Sdavidch     * buffer can be ahead of the index in the status block
3328255736Sdavidch     */
3329255736Sdavidch    rmb();
3330255736Sdavidch
3331255736Sdavidch    BLOGD(sc, DBG_RX,
3332255736Sdavidch          "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3333255736Sdavidch          fp->index, hw_cq_cons, sw_cq_cons);
3334255736Sdavidch
3335255736Sdavidch    while (sw_cq_cons != hw_cq_cons) {
3336255736Sdavidch        struct bxe_sw_rx_bd *rx_buf = NULL;
3337255736Sdavidch        union eth_rx_cqe *cqe;
3338255736Sdavidch        struct eth_fast_path_rx_cqe *cqe_fp;
3339255736Sdavidch        uint8_t cqe_fp_flags;
3340255736Sdavidch        enum eth_rx_cqe_type cqe_fp_type;
3341283269Sdavidcs        uint16_t len, lenonbd,  pad;
3342255736Sdavidch        struct mbuf *m = NULL;
3343255736Sdavidch
3344255736Sdavidch        comp_ring_cons = RCQ(sw_cq_cons);
3345255736Sdavidch        bd_prod = RX_BD(bd_prod);
3346255736Sdavidch        bd_cons = RX_BD(bd_cons);
3347255736Sdavidch
3348255736Sdavidch        cqe          = &fp->rcq_chain[comp_ring_cons];
3349255736Sdavidch        cqe_fp       = &cqe->fast_path_cqe;
3350255736Sdavidch        cqe_fp_flags = cqe_fp->type_error_flags;
3351255736Sdavidch        cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3352255736Sdavidch
3353255736Sdavidch        BLOGD(sc, DBG_RX,
3354255736Sdavidch              "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3355255736Sdavidch              "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3356283269Sdavidcs              "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3357255736Sdavidch              fp->index,
3358255736Sdavidch              hw_cq_cons,
3359255736Sdavidch              sw_cq_cons,
3360255736Sdavidch              bd_prod,
3361255736Sdavidch              bd_cons,
3362255736Sdavidch              CQE_TYPE(cqe_fp_flags),
3363255736Sdavidch              cqe_fp_flags,
3364255736Sdavidch              cqe_fp->status_flags,
3365255736Sdavidch              le32toh(cqe_fp->rss_hash_result),
3366255736Sdavidch              le16toh(cqe_fp->vlan_tag),
3367283269Sdavidcs              le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3368283269Sdavidcs              le16toh(cqe_fp->len_on_bd));
3369255736Sdavidch
3370255736Sdavidch        /* is this a slowpath msg? */
3371255736Sdavidch        if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3372255736Sdavidch            bxe_sp_event(sc, fp, cqe);
3373255736Sdavidch            goto next_cqe;
3374255736Sdavidch        }
3375255736Sdavidch
3376255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[bd_cons];
3377255736Sdavidch
3378255736Sdavidch        if (!CQE_TYPE_FAST(cqe_fp_type)) {
3379255736Sdavidch            struct bxe_sw_tpa_info *tpa_info;
3380255736Sdavidch            uint16_t frag_size, pages;
3381255736Sdavidch            uint8_t queue;
3382255736Sdavidch
3383255736Sdavidch#if 0
3384255736Sdavidch            /* sanity check */
3385255736Sdavidch            if (!fp->tpa_enable &&
3386255736Sdavidch                (CQE_TYPE_START(cqe_fp_type) || CQE_TYPE_STOP(cqe_fp_type))) {
3387255736Sdavidch                BLOGE(sc, "START/STOP packet while !tpa_enable type (0x%x)\n",
3388255736Sdavidch                      CQE_TYPE(cqe_fp_type));
3389255736Sdavidch            }
3390255736Sdavidch#endif
3391255736Sdavidch
3392255736Sdavidch            if (CQE_TYPE_START(cqe_fp_type)) {
3393255736Sdavidch                bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3394255736Sdavidch                              bd_cons, bd_prod, cqe_fp);
3395255736Sdavidch                m = NULL; /* packet not ready yet */
3396255736Sdavidch                goto next_rx;
3397255736Sdavidch            }
3398255736Sdavidch
3399255736Sdavidch            KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3400255736Sdavidch                    ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3401255736Sdavidch
3402255736Sdavidch            queue = cqe->end_agg_cqe.queue_index;
3403255736Sdavidch            tpa_info = &fp->rx_tpa_info[queue];
3404255736Sdavidch
3405255736Sdavidch            BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3406255736Sdavidch                  fp->index, queue);
3407255736Sdavidch
3408255736Sdavidch            frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3409255736Sdavidch                         tpa_info->len_on_bd);
3410255736Sdavidch            pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3411255736Sdavidch
3412255736Sdavidch            bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3413255736Sdavidch                         &cqe->end_agg_cqe, comp_ring_cons);
3414255736Sdavidch
3415283269Sdavidcs            bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3416255736Sdavidch
3417255736Sdavidch            goto next_cqe;
3418255736Sdavidch        }
3419255736Sdavidch
3420255736Sdavidch        /* non TPA */
3421255736Sdavidch
3422255736Sdavidch        /* is this an error packet? */
3423255736Sdavidch        if (__predict_false(cqe_fp_flags &
3424255736Sdavidch                            ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3425255736Sdavidch            BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3426255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3427255736Sdavidch            goto next_rx;
3428255736Sdavidch        }
3429255736Sdavidch
3430255736Sdavidch        len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3431283269Sdavidcs        lenonbd = le16toh(cqe_fp->len_on_bd);
3432255736Sdavidch        pad = cqe_fp->placement_offset;
3433255736Sdavidch
3434255736Sdavidch        m = rx_buf->m;
3435255736Sdavidch
3436255736Sdavidch        if (__predict_false(m == NULL)) {
3437255736Sdavidch            BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3438255736Sdavidch                  bd_cons, fp->index);
3439255736Sdavidch            goto next_rx;
3440255736Sdavidch        }
3441255736Sdavidch
3442255736Sdavidch        /* XXX double copy if packet length under a threshold */
3443255736Sdavidch
3444255736Sdavidch        /*
3445255736Sdavidch         * If all the buffer descriptors are filled with mbufs then fill in
3446255736Sdavidch         * the current consumer index with a new BD. Else if a maximum Rx
3447255736Sdavidch         * buffer limit is imposed then fill in the next producer index.
3448255736Sdavidch         */
3449255736Sdavidch        rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3450255736Sdavidch                                  (sc->max_rx_bufs != RX_BD_USABLE) ?
3451255736Sdavidch                                      bd_prod : bd_cons);
3452255736Sdavidch        if (rc != 0) {
3453281006Sdavidcs
3454281006Sdavidcs            /* we simply reuse the received mbuf and don't post it to the stack */
3455281006Sdavidcs            m = NULL;
3456281006Sdavidcs
3457255736Sdavidch            BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3458255736Sdavidch                  fp->index, rc);
3459255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3460255736Sdavidch
3461255736Sdavidch            if (sc->max_rx_bufs != RX_BD_USABLE) {
3462255736Sdavidch                /* copy this consumer index to the producer index */
3463255736Sdavidch                memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3464255736Sdavidch                       sizeof(struct bxe_sw_rx_bd));
3465255736Sdavidch                memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3466255736Sdavidch            }
3467255736Sdavidch
3468255736Sdavidch            goto next_rx;
3469255736Sdavidch        }
3470255736Sdavidch
3471255736Sdavidch        /* current mbuf was detached from the bd */
3472255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
3473255736Sdavidch
3474255736Sdavidch        /* we allocated a replacement mbuf, fixup the current one */
3475255736Sdavidch        m_adj(m, pad);
3476255736Sdavidch        m->m_pkthdr.len = m->m_len = len;
3477255736Sdavidch
3478283269Sdavidcs        if (len != lenonbd){
3479283269Sdavidcs            rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3480283269Sdavidcs            if (rc)
3481283269Sdavidcs                break;
3482283274Sdavidcs            fp->eth_q_stats.rx_jumbo_sge_pkts++;
3483283269Sdavidcs        }
3484283269Sdavidcs
3485255736Sdavidch        /* assign packet to this interface interface */
3486266979Smarcel	if_setrcvif(m, ifp);
3487255736Sdavidch
3488255736Sdavidch        /* assume no hardware checksum has complated */
3489255736Sdavidch        m->m_pkthdr.csum_flags = 0;
3490255736Sdavidch
3491255736Sdavidch        /* validate checksum if offload enabled */
3492266979Smarcel        if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3493255736Sdavidch            /* check for a valid IP frame */
3494255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3495255736Sdavidch                  ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3496255736Sdavidch                m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3497255736Sdavidch                if (__predict_false(cqe_fp_flags &
3498255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3499255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3500255736Sdavidch                } else {
3501255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3502255736Sdavidch                    m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3503255736Sdavidch                }
3504255736Sdavidch            }
3505255736Sdavidch
3506255736Sdavidch            /* check for a valid TCP/UDP frame */
3507255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3508255736Sdavidch                  ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3509255736Sdavidch                if (__predict_false(cqe_fp_flags &
3510255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3511255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3512255736Sdavidch                } else {
3513255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3514255736Sdavidch                    m->m_pkthdr.csum_data = 0xFFFF;
3515255736Sdavidch                    m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3516255736Sdavidch                                               CSUM_PSEUDO_HDR);
3517255736Sdavidch                }
3518255736Sdavidch            }
3519255736Sdavidch        }
3520255736Sdavidch
3521255736Sdavidch        /* if there is a VLAN tag then flag that info */
3522255736Sdavidch        if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_VLAN) {
3523255736Sdavidch            m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3524255736Sdavidch            m->m_flags |= M_VLANTAG;
3525255736Sdavidch        }
3526255736Sdavidch
3527255736Sdavidch#if __FreeBSD_version >= 800000
3528255736Sdavidch        /* specify what RSS queue was used for this flow */
3529255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3530275358Shselasky        M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
3531255736Sdavidch#endif
3532255736Sdavidch
3533255736Sdavidchnext_rx:
3534255736Sdavidch
3535255736Sdavidch        bd_cons    = RX_BD_NEXT(bd_cons);
3536255736Sdavidch        bd_prod    = RX_BD_NEXT(bd_prod);
3537255736Sdavidch        bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3538255736Sdavidch
3539255736Sdavidch        /* pass the frame to the stack */
3540255736Sdavidch        if (__predict_true(m != NULL)) {
3541271782Sglebius            if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3542255736Sdavidch            rx_pkts++;
3543266979Smarcel            if_input(ifp, m);
3544255736Sdavidch        }
3545255736Sdavidch
3546255736Sdavidchnext_cqe:
3547255736Sdavidch
3548255736Sdavidch        sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3549255736Sdavidch        sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3550255736Sdavidch
3551255736Sdavidch        /* limit spinning on the queue */
3552281006Sdavidcs        if (rc != 0)
3553281006Sdavidcs            break;
3554281006Sdavidcs
3555255736Sdavidch        if (rx_pkts == sc->rx_budget) {
3556255736Sdavidch            fp->eth_q_stats.rx_budget_reached++;
3557255736Sdavidch            break;
3558255736Sdavidch        }
3559255736Sdavidch    } /* while work to do */
3560255736Sdavidch
3561255736Sdavidch    fp->rx_bd_cons = bd_cons;
3562255736Sdavidch    fp->rx_bd_prod = bd_prod_fw;
3563255736Sdavidch    fp->rx_cq_cons = sw_cq_cons;
3564255736Sdavidch    fp->rx_cq_prod = sw_cq_prod;
3565255736Sdavidch
3566255736Sdavidch    /* Update producers */
3567255736Sdavidch    bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3568255736Sdavidch
3569255736Sdavidch    fp->eth_q_stats.rx_pkts += rx_pkts;
3570255736Sdavidch    fp->eth_q_stats.rx_calls++;
3571255736Sdavidch
3572255736Sdavidch    BXE_FP_RX_UNLOCK(fp);
3573255736Sdavidch
3574255736Sdavidch    return (sw_cq_cons != hw_cq_cons);
3575255736Sdavidch}
3576255736Sdavidch
3577255736Sdavidchstatic uint16_t
3578255736Sdavidchbxe_free_tx_pkt(struct bxe_softc    *sc,
3579255736Sdavidch                struct bxe_fastpath *fp,
3580255736Sdavidch                uint16_t            idx)
3581255736Sdavidch{
3582255736Sdavidch    struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3583255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
3584255736Sdavidch    uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3585255736Sdavidch    uint16_t new_cons;
3586255736Sdavidch    int nbd;
3587255736Sdavidch
3588255736Sdavidch    /* unmap the mbuf from non-paged memory */
3589255736Sdavidch    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3590255736Sdavidch
3591255736Sdavidch    tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3592255736Sdavidch    nbd = le16toh(tx_start_bd->nbd) - 1;
3593255736Sdavidch
3594255736Sdavidch#if 0
3595255736Sdavidch    if ((nbd - 1) > (MAX_MBUF_FRAGS + 2)) {
3596255736Sdavidch        bxe_panic(sc, ("BAD nbd!\n"));
3597255736Sdavidch    }
3598255736Sdavidch#endif
3599255736Sdavidch
3600255736Sdavidch    new_cons = (tx_buf->first_bd + nbd);
3601255736Sdavidch
3602255736Sdavidch#if 0
3603255736Sdavidch    struct eth_tx_bd *tx_data_bd;
3604255736Sdavidch
3605255736Sdavidch    /*
3606255736Sdavidch     * The following code doesn't do anything but is left here
3607255736Sdavidch     * for clarity on what the new value of new_cons skipped.
3608255736Sdavidch     */
3609255736Sdavidch
3610255736Sdavidch    /* get the next bd */
3611255736Sdavidch    bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3612255736Sdavidch
3613255736Sdavidch    /* skip the parse bd */
3614255736Sdavidch    --nbd;
3615255736Sdavidch    bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3616255736Sdavidch
3617255736Sdavidch    /* skip the TSO split header bd since they have no mapping */
3618255736Sdavidch    if (tx_buf->flags & BXE_TSO_SPLIT_BD) {
3619255736Sdavidch        --nbd;
3620255736Sdavidch        bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3621255736Sdavidch    }
3622255736Sdavidch
3623255736Sdavidch    /* now free frags */
3624255736Sdavidch    while (nbd > 0) {
3625255736Sdavidch        tx_data_bd = &fp->tx_chain[bd_idx].reg_bd;
3626255736Sdavidch        if (--nbd) {
3627255736Sdavidch            bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3628255736Sdavidch        }
3629255736Sdavidch    }
3630255736Sdavidch#endif
3631255736Sdavidch
3632255736Sdavidch    /* free the mbuf */
3633255736Sdavidch    if (__predict_true(tx_buf->m != NULL)) {
3634255736Sdavidch        m_freem(tx_buf->m);
3635255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx--;
3636255736Sdavidch    } else {
3637255736Sdavidch        fp->eth_q_stats.tx_chain_lost_mbuf++;
3638255736Sdavidch    }
3639255736Sdavidch
3640255736Sdavidch    tx_buf->m = NULL;
3641255736Sdavidch    tx_buf->first_bd = 0;
3642255736Sdavidch
3643255736Sdavidch    return (new_cons);
3644255736Sdavidch}
3645255736Sdavidch
3646255736Sdavidch/* transmit timeout watchdog */
3647255736Sdavidchstatic int
3648255736Sdavidchbxe_watchdog(struct bxe_softc    *sc,
3649255736Sdavidch             struct bxe_fastpath *fp)
3650255736Sdavidch{
3651255736Sdavidch    BXE_FP_TX_LOCK(fp);
3652255736Sdavidch
3653255736Sdavidch    if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3654255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
3655255736Sdavidch        return (0);
3656255736Sdavidch    }
3657255736Sdavidch
3658258187Sedavis    BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3659258187Sedavis
3660255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
3661255736Sdavidch
3662255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_REINIT);
3663255736Sdavidch    taskqueue_enqueue(sc->chip_tq, &sc->chip_tq_task);
3664255736Sdavidch
3665255736Sdavidch    return (-1);
3666255736Sdavidch}
3667255736Sdavidch
3668255736Sdavidch/* processes transmit completions */
3669255736Sdavidchstatic uint8_t
3670255736Sdavidchbxe_txeof(struct bxe_softc    *sc,
3671255736Sdavidch          struct bxe_fastpath *fp)
3672255736Sdavidch{
3673266979Smarcel    if_t ifp = sc->ifp;
3674255736Sdavidch    uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3675255736Sdavidch    uint16_t tx_bd_avail;
3676255736Sdavidch
3677255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
3678255736Sdavidch
3679255736Sdavidch    bd_cons = fp->tx_bd_cons;
3680255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
3681255736Sdavidch    sw_cons = fp->tx_pkt_cons;
3682255736Sdavidch
3683255736Sdavidch    while (sw_cons != hw_cons) {
3684255736Sdavidch        pkt_cons = TX_BD(sw_cons);
3685255736Sdavidch
3686255736Sdavidch        BLOGD(sc, DBG_TX,
3687255736Sdavidch              "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3688255736Sdavidch              fp->index, hw_cons, sw_cons, pkt_cons);
3689255736Sdavidch
3690255736Sdavidch        bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3691255736Sdavidch
3692255736Sdavidch        sw_cons++;
3693255736Sdavidch    }
3694255736Sdavidch
3695255736Sdavidch    fp->tx_pkt_cons = sw_cons;
3696255736Sdavidch    fp->tx_bd_cons  = bd_cons;
3697255736Sdavidch
3698255736Sdavidch    BLOGD(sc, DBG_TX,
3699255736Sdavidch          "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3700255736Sdavidch          fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3701255736Sdavidch
3702255736Sdavidch    mb();
3703255736Sdavidch
3704255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
3705255736Sdavidch
3706258187Sedavis    if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3707266979Smarcel        if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
3708258187Sedavis    } else {
3709266979Smarcel        if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3710258187Sedavis    }
3711255736Sdavidch
3712258187Sedavis    if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3713258187Sedavis        /* reset the watchdog timer if there are pending transmits */
3714255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
3715258187Sedavis        return (TRUE);
3716258187Sedavis    } else {
3717258187Sedavis        /* clear watchdog when there are no pending transmits */
3718258187Sedavis        fp->watchdog_timer = 0;
3719258187Sedavis        return (FALSE);
3720255736Sdavidch    }
3721255736Sdavidch}
3722255736Sdavidch
3723255736Sdavidchstatic void
3724255736Sdavidchbxe_drain_tx_queues(struct bxe_softc *sc)
3725255736Sdavidch{
3726255736Sdavidch    struct bxe_fastpath *fp;
3727255736Sdavidch    int i, count;
3728255736Sdavidch
3729255736Sdavidch    /* wait until all TX fastpath tasks have completed */
3730255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
3731255736Sdavidch        fp = &sc->fp[i];
3732255736Sdavidch
3733255736Sdavidch        count = 1000;
3734255736Sdavidch
3735255736Sdavidch        while (bxe_has_tx_work(fp)) {
3736255736Sdavidch
3737255736Sdavidch            BXE_FP_TX_LOCK(fp);
3738255736Sdavidch            bxe_txeof(sc, fp);
3739255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
3740255736Sdavidch
3741255736Sdavidch            if (count == 0) {
3742255736Sdavidch                BLOGE(sc, "Timeout waiting for fp[%d] "
3743255736Sdavidch                          "transmits to complete!\n", i);
3744255736Sdavidch                bxe_panic(sc, ("tx drain failure\n"));
3745255736Sdavidch                return;
3746255736Sdavidch            }
3747255736Sdavidch
3748255736Sdavidch            count--;
3749255736Sdavidch            DELAY(1000);
3750255736Sdavidch            rmb();
3751255736Sdavidch        }
3752255736Sdavidch    }
3753255736Sdavidch
3754255736Sdavidch    return;
3755255736Sdavidch}
3756255736Sdavidch
3757255736Sdavidchstatic int
3758255736Sdavidchbxe_del_all_macs(struct bxe_softc          *sc,
3759255736Sdavidch                 struct ecore_vlan_mac_obj *mac_obj,
3760255736Sdavidch                 int                       mac_type,
3761255736Sdavidch                 uint8_t                   wait_for_comp)
3762255736Sdavidch{
3763255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3764255736Sdavidch    int rc;
3765255736Sdavidch
3766255736Sdavidch    /* wait for completion of requested */
3767255736Sdavidch    if (wait_for_comp) {
3768255736Sdavidch        bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3769255736Sdavidch    }
3770255736Sdavidch
3771255736Sdavidch    /* Set the mac type of addresses we want to clear */
3772255736Sdavidch    bxe_set_bit(mac_type, &vlan_mac_flags);
3773255736Sdavidch
3774255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3775255736Sdavidch    if (rc < 0) {
3776255736Sdavidch        BLOGE(sc, "Failed to delete MACs (%d)\n", rc);
3777255736Sdavidch    }
3778255736Sdavidch
3779255736Sdavidch    return (rc);
3780255736Sdavidch}
3781255736Sdavidch
3782255736Sdavidchstatic int
3783255736Sdavidchbxe_fill_accept_flags(struct bxe_softc *sc,
3784255736Sdavidch                      uint32_t         rx_mode,
3785255736Sdavidch                      unsigned long    *rx_accept_flags,
3786255736Sdavidch                      unsigned long    *tx_accept_flags)
3787255736Sdavidch{
3788255736Sdavidch    /* Clear the flags first */
3789255736Sdavidch    *rx_accept_flags = 0;
3790255736Sdavidch    *tx_accept_flags = 0;
3791255736Sdavidch
3792255736Sdavidch    switch (rx_mode) {
3793255736Sdavidch    case BXE_RX_MODE_NONE:
3794255736Sdavidch        /*
3795255736Sdavidch         * 'drop all' supersedes any accept flags that may have been
3796255736Sdavidch         * passed to the function.
3797255736Sdavidch         */
3798255736Sdavidch        break;
3799255736Sdavidch
3800255736Sdavidch    case BXE_RX_MODE_NORMAL:
3801255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3802255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3803255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3804255736Sdavidch
3805255736Sdavidch        /* internal switching mode */
3806255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3807255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3808255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3809255736Sdavidch
3810255736Sdavidch        break;
3811255736Sdavidch
3812255736Sdavidch    case BXE_RX_MODE_ALLMULTI:
3813255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3814255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3815255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3816255736Sdavidch
3817255736Sdavidch        /* internal switching mode */
3818255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3819255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3820255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3821255736Sdavidch
3822255736Sdavidch        break;
3823255736Sdavidch
3824255736Sdavidch    case BXE_RX_MODE_PROMISC:
3825255736Sdavidch        /*
3826255736Sdavidch         * According to deffinition of SI mode, iface in promisc mode
3827255736Sdavidch         * should receive matched and unmatched (in resolution of port)
3828255736Sdavidch         * unicast packets.
3829255736Sdavidch         */
3830255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3831255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3832255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3833255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3834255736Sdavidch
3835255736Sdavidch        /* internal switching mode */
3836255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3837255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3838255736Sdavidch
3839255736Sdavidch        if (IS_MF_SI(sc)) {
3840255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3841255736Sdavidch        } else {
3842255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3843255736Sdavidch        }
3844255736Sdavidch
3845255736Sdavidch        break;
3846255736Sdavidch
3847255736Sdavidch    default:
3848255736Sdavidch        BLOGE(sc, "Unknown rx_mode (%d)\n", rx_mode);
3849255736Sdavidch        return (-1);
3850255736Sdavidch    }
3851255736Sdavidch
3852255736Sdavidch    /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3853255736Sdavidch    if (rx_mode != BXE_RX_MODE_NONE) {
3854255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3855255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3856255736Sdavidch    }
3857255736Sdavidch
3858255736Sdavidch    return (0);
3859255736Sdavidch}
3860255736Sdavidch
3861255736Sdavidchstatic int
3862255736Sdavidchbxe_set_q_rx_mode(struct bxe_softc *sc,
3863255736Sdavidch                  uint8_t          cl_id,
3864255736Sdavidch                  unsigned long    rx_mode_flags,
3865255736Sdavidch                  unsigned long    rx_accept_flags,
3866255736Sdavidch                  unsigned long    tx_accept_flags,
3867255736Sdavidch                  unsigned long    ramrod_flags)
3868255736Sdavidch{
3869255736Sdavidch    struct ecore_rx_mode_ramrod_params ramrod_param;
3870255736Sdavidch    int rc;
3871255736Sdavidch
3872255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
3873255736Sdavidch
3874255736Sdavidch    /* Prepare ramrod parameters */
3875255736Sdavidch    ramrod_param.cid = 0;
3876255736Sdavidch    ramrod_param.cl_id = cl_id;
3877255736Sdavidch    ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3878255736Sdavidch    ramrod_param.func_id = SC_FUNC(sc);
3879255736Sdavidch
3880255736Sdavidch    ramrod_param.pstate = &sc->sp_state;
3881255736Sdavidch    ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3882255736Sdavidch
3883255736Sdavidch    ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3884255736Sdavidch    ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3885255736Sdavidch
3886255736Sdavidch    bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3887255736Sdavidch
3888255736Sdavidch    ramrod_param.ramrod_flags = ramrod_flags;
3889255736Sdavidch    ramrod_param.rx_mode_flags = rx_mode_flags;
3890255736Sdavidch
3891255736Sdavidch    ramrod_param.rx_accept_flags = rx_accept_flags;
3892255736Sdavidch    ramrod_param.tx_accept_flags = tx_accept_flags;
3893255736Sdavidch
3894255736Sdavidch    rc = ecore_config_rx_mode(sc, &ramrod_param);
3895255736Sdavidch    if (rc < 0) {
3896255736Sdavidch        BLOGE(sc, "Set rx_mode %d failed\n", sc->rx_mode);
3897255736Sdavidch        return (rc);
3898255736Sdavidch    }
3899255736Sdavidch
3900255736Sdavidch    return (0);
3901255736Sdavidch}
3902255736Sdavidch
3903255736Sdavidchstatic int
3904255736Sdavidchbxe_set_storm_rx_mode(struct bxe_softc *sc)
3905255736Sdavidch{
3906255736Sdavidch    unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3907255736Sdavidch    unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3908255736Sdavidch    int rc;
3909255736Sdavidch
3910255736Sdavidch    rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3911255736Sdavidch                               &tx_accept_flags);
3912255736Sdavidch    if (rc) {
3913255736Sdavidch        return (rc);
3914255736Sdavidch    }
3915255736Sdavidch
3916255736Sdavidch    bxe_set_bit(RAMROD_RX, &ramrod_flags);
3917255736Sdavidch    bxe_set_bit(RAMROD_TX, &ramrod_flags);
3918255736Sdavidch
3919255736Sdavidch    /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3920255736Sdavidch    return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3921255736Sdavidch                              rx_accept_flags, tx_accept_flags,
3922255736Sdavidch                              ramrod_flags));
3923255736Sdavidch}
3924255736Sdavidch
3925255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3926255736Sdavidchstatic int
3927255736Sdavidchbxe_nic_load_no_mcp(struct bxe_softc *sc)
3928255736Sdavidch{
3929255736Sdavidch    int path = SC_PATH(sc);
3930255736Sdavidch    int port = SC_PORT(sc);
3931255736Sdavidch
3932255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3933255736Sdavidch          path, load_count[path][0], load_count[path][1],
3934255736Sdavidch          load_count[path][2]);
3935255736Sdavidch    load_count[path][0]++;
3936255736Sdavidch    load_count[path][1 + port]++;
3937255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3938255736Sdavidch          path, load_count[path][0], load_count[path][1],
3939255736Sdavidch          load_count[path][2]);
3940255736Sdavidch    if (load_count[path][0] == 1) {
3941255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_COMMON);
3942255736Sdavidch    } else if (load_count[path][1 + port] == 1) {
3943255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_PORT);
3944255736Sdavidch    } else {
3945255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3946255736Sdavidch    }
3947255736Sdavidch}
3948255736Sdavidch
3949255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3950255736Sdavidchstatic int
3951255736Sdavidchbxe_nic_unload_no_mcp(struct bxe_softc *sc)
3952255736Sdavidch{
3953255736Sdavidch    int port = SC_PORT(sc);
3954255736Sdavidch    int path = SC_PATH(sc);
3955255736Sdavidch
3956255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3957255736Sdavidch          path, load_count[path][0], load_count[path][1],
3958255736Sdavidch          load_count[path][2]);
3959255736Sdavidch    load_count[path][0]--;
3960255736Sdavidch    load_count[path][1 + port]--;
3961255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3962255736Sdavidch          path, load_count[path][0], load_count[path][1],
3963255736Sdavidch          load_count[path][2]);
3964255736Sdavidch    if (load_count[path][0] == 0) {
3965255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3966255736Sdavidch    } else if (load_count[path][1 + port] == 0) {
3967255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3968255736Sdavidch    } else {
3969255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3970255736Sdavidch    }
3971255736Sdavidch}
3972255736Sdavidch
3973255736Sdavidch/* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3974255736Sdavidchstatic uint32_t
3975255736Sdavidchbxe_send_unload_req(struct bxe_softc *sc,
3976255736Sdavidch                    int              unload_mode)
3977255736Sdavidch{
3978255736Sdavidch    uint32_t reset_code = 0;
3979255736Sdavidch#if 0
3980255736Sdavidch    int port = SC_PORT(sc);
3981255736Sdavidch    int path = SC_PATH(sc);
3982255736Sdavidch#endif
3983255736Sdavidch
3984255736Sdavidch    /* Select the UNLOAD request mode */
3985255736Sdavidch    if (unload_mode == UNLOAD_NORMAL) {
3986255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3987255736Sdavidch    }
3988255736Sdavidch#if 0
3989255736Sdavidch    else if (sc->flags & BXE_NO_WOL_FLAG) {
3990255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP;
3991255736Sdavidch    } else if (sc->wol) {
3992255736Sdavidch        uint32_t emac_base = port ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
3993255736Sdavidch        uint8_t *mac_addr = sc->dev->dev_addr;
3994255736Sdavidch        uint32_t val;
3995255736Sdavidch        uint16_t pmc;
3996255736Sdavidch
3997255736Sdavidch        /*
3998255736Sdavidch         * The mac address is written to entries 1-4 to
3999255736Sdavidch         * preserve entry 0 which is used by the PMF
4000255736Sdavidch         */
4001255736Sdavidch        uint8_t entry = (SC_VN(sc) + 1)*8;
4002255736Sdavidch
4003255736Sdavidch        val = (mac_addr[0] << 8) | mac_addr[1];
4004255736Sdavidch        EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry, val);
4005255736Sdavidch
4006255736Sdavidch        val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
4007255736Sdavidch              (mac_addr[4] << 8) | mac_addr[5];
4008255736Sdavidch        EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry + 4, val);
4009255736Sdavidch
4010255736Sdavidch        /* Enable the PME and clear the status */
4011255736Sdavidch        pmc = pci_read_config(sc->dev,
4012255736Sdavidch                              (sc->devinfo.pcie_pm_cap_reg +
4013255736Sdavidch                               PCIR_POWER_STATUS),
4014255736Sdavidch                              2);
4015255736Sdavidch        pmc |= PCIM_PSTAT_PMEENABLE | PCIM_PSTAT_PME;
4016255736Sdavidch        pci_write_config(sc->dev,
4017255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg +
4018255736Sdavidch                          PCIR_POWER_STATUS),
4019255736Sdavidch                         pmc, 4);
4020255736Sdavidch
4021255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_EN;
4022255736Sdavidch    }
4023255736Sdavidch#endif
4024255736Sdavidch    else {
4025255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
4026255736Sdavidch    }
4027255736Sdavidch
4028255736Sdavidch    /* Send the request to the MCP */
4029255736Sdavidch    if (!BXE_NOMCP(sc)) {
4030255736Sdavidch        reset_code = bxe_fw_command(sc, reset_code, 0);
4031255736Sdavidch    } else {
4032255736Sdavidch        reset_code = bxe_nic_unload_no_mcp(sc);
4033255736Sdavidch    }
4034255736Sdavidch
4035255736Sdavidch    return (reset_code);
4036255736Sdavidch}
4037255736Sdavidch
4038255736Sdavidch/* send UNLOAD_DONE command to the MCP */
4039255736Sdavidchstatic void
4040255736Sdavidchbxe_send_unload_done(struct bxe_softc *sc,
4041255736Sdavidch                     uint8_t          keep_link)
4042255736Sdavidch{
4043255736Sdavidch    uint32_t reset_param =
4044255736Sdavidch        keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
4045255736Sdavidch
4046255736Sdavidch    /* Report UNLOAD_DONE to MCP */
4047255736Sdavidch    if (!BXE_NOMCP(sc)) {
4048255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
4049255736Sdavidch    }
4050255736Sdavidch}
4051255736Sdavidch
4052255736Sdavidchstatic int
4053255736Sdavidchbxe_func_wait_started(struct bxe_softc *sc)
4054255736Sdavidch{
4055255736Sdavidch    int tout = 50;
4056255736Sdavidch
4057255736Sdavidch    if (!sc->port.pmf) {
4058255736Sdavidch        return (0);
4059255736Sdavidch    }
4060255736Sdavidch
4061255736Sdavidch    /*
4062255736Sdavidch     * (assumption: No Attention from MCP at this stage)
4063255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
4064255736Sdavidch     * 1. Sync IRS for default SB
4065255736Sdavidch     * 2. Sync SP queue - this guarantees us that attention handling started
4066255736Sdavidch     * 3. Wait, that TX disable/enable transaction completes
4067255736Sdavidch     *
4068255736Sdavidch     * 1+2 guarantee that if DCBX attention was scheduled it already changed
4069255736Sdavidch     * pending bit of transaction from STARTED-->TX_STOPPED, if we already
4070255736Sdavidch     * received completion for the transaction the state is TX_STOPPED.
4071255736Sdavidch     * State will return to STARTED after completion of TX_STOPPED-->STARTED
4072255736Sdavidch     * transaction.
4073255736Sdavidch     */
4074255736Sdavidch
4075255736Sdavidch    /* XXX make sure default SB ISR is done */
4076255736Sdavidch    /* need a way to synchronize an irq (intr_mtx?) */
4077255736Sdavidch
4078255736Sdavidch    /* XXX flush any work queues */
4079255736Sdavidch
4080255736Sdavidch    while (ecore_func_get_state(sc, &sc->func_obj) !=
4081255736Sdavidch           ECORE_F_STATE_STARTED && tout--) {
4082255736Sdavidch        DELAY(20000);
4083255736Sdavidch    }
4084255736Sdavidch
4085255736Sdavidch    if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
4086255736Sdavidch        /*
4087255736Sdavidch         * Failed to complete the transaction in a "good way"
4088255736Sdavidch         * Force both transactions with CLR bit.
4089255736Sdavidch         */
4090255736Sdavidch        struct ecore_func_state_params func_params = { NULL };
4091255736Sdavidch
4092255736Sdavidch        BLOGE(sc, "Unexpected function state! "
4093255736Sdavidch                  "Forcing STARTED-->TX_STOPPED-->STARTED\n");
4094255736Sdavidch
4095255736Sdavidch        func_params.f_obj = &sc->func_obj;
4096255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
4097255736Sdavidch
4098255736Sdavidch        /* STARTED-->TX_STOPPED */
4099255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_STOP;
4100255736Sdavidch        ecore_func_state_change(sc, &func_params);
4101255736Sdavidch
4102255736Sdavidch        /* TX_STOPPED-->STARTED */
4103255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_START;
4104255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
4105255736Sdavidch    }
4106255736Sdavidch
4107255736Sdavidch    return (0);
4108255736Sdavidch}
4109255736Sdavidch
4110255736Sdavidchstatic int
4111255736Sdavidchbxe_stop_queue(struct bxe_softc *sc,
4112255736Sdavidch               int              index)
4113255736Sdavidch{
4114255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[index];
4115255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
4116255736Sdavidch    int rc;
4117255736Sdavidch
4118255736Sdavidch    BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
4119255736Sdavidch
4120255736Sdavidch    q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
4121255736Sdavidch    /* We want to wait for completion in this context */
4122255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
4123255736Sdavidch
4124255736Sdavidch    /* Stop the primary connection: */
4125255736Sdavidch
4126255736Sdavidch    /* ...halt the connection */
4127255736Sdavidch    q_params.cmd = ECORE_Q_CMD_HALT;
4128255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
4129255736Sdavidch    if (rc) {
4130255736Sdavidch        return (rc);
4131255736Sdavidch    }
4132255736Sdavidch
4133255736Sdavidch    /* ...terminate the connection */
4134255736Sdavidch    q_params.cmd = ECORE_Q_CMD_TERMINATE;
4135255736Sdavidch    memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
4136255736Sdavidch    q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
4137255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
4138255736Sdavidch    if (rc) {
4139255736Sdavidch        return (rc);
4140255736Sdavidch    }
4141255736Sdavidch
4142255736Sdavidch    /* ...delete cfc entry */
4143255736Sdavidch    q_params.cmd = ECORE_Q_CMD_CFC_DEL;
4144255736Sdavidch    memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
4145255736Sdavidch    q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
4146255736Sdavidch    return (ecore_queue_state_change(sc, &q_params));
4147255736Sdavidch}
4148255736Sdavidch
4149255736Sdavidch/* wait for the outstanding SP commands */
4150255736Sdavidchstatic inline uint8_t
4151255736Sdavidchbxe_wait_sp_comp(struct bxe_softc *sc,
4152255736Sdavidch                 unsigned long    mask)
4153255736Sdavidch{
4154255736Sdavidch    unsigned long tmp;
4155255736Sdavidch    int tout = 5000; /* wait for 5 secs tops */
4156255736Sdavidch
4157255736Sdavidch    while (tout--) {
4158255736Sdavidch        mb();
4159255736Sdavidch        if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
4160255736Sdavidch            return (TRUE);
4161255736Sdavidch        }
4162255736Sdavidch
4163255736Sdavidch        DELAY(1000);
4164255736Sdavidch    }
4165255736Sdavidch
4166255736Sdavidch    mb();
4167255736Sdavidch
4168255736Sdavidch    tmp = atomic_load_acq_long(&sc->sp_state);
4169255736Sdavidch    if (tmp & mask) {
4170255736Sdavidch        BLOGE(sc, "Filtering completion timed out: "
4171255736Sdavidch                  "sp_state 0x%lx, mask 0x%lx\n",
4172255736Sdavidch              tmp, mask);
4173255736Sdavidch        return (FALSE);
4174255736Sdavidch    }
4175255736Sdavidch
4176255736Sdavidch    return (FALSE);
4177255736Sdavidch}
4178255736Sdavidch
4179255736Sdavidchstatic int
4180255736Sdavidchbxe_func_stop(struct bxe_softc *sc)
4181255736Sdavidch{
4182255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
4183255736Sdavidch    int rc;
4184255736Sdavidch
4185255736Sdavidch    /* prepare parameters for function state transitions */
4186255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
4187255736Sdavidch    func_params.f_obj = &sc->func_obj;
4188255736Sdavidch    func_params.cmd = ECORE_F_CMD_STOP;
4189255736Sdavidch
4190255736Sdavidch    /*
4191255736Sdavidch     * Try to stop the function the 'good way'. If it fails (in case
4192255736Sdavidch     * of a parity error during bxe_chip_cleanup()) and we are
4193255736Sdavidch     * not in a debug mode, perform a state transaction in order to
4194255736Sdavidch     * enable further HW_RESET transaction.
4195255736Sdavidch     */
4196255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
4197255736Sdavidch    if (rc) {
4198255736Sdavidch        BLOGE(sc, "FUNC_STOP ramrod failed. "
4199255736Sdavidch                  "Running a dry transaction\n");
4200255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
4201255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
4202255736Sdavidch    }
4203255736Sdavidch
4204255736Sdavidch    return (0);
4205255736Sdavidch}
4206255736Sdavidch
4207255736Sdavidchstatic int
4208255736Sdavidchbxe_reset_hw(struct bxe_softc *sc,
4209255736Sdavidch             uint32_t         load_code)
4210255736Sdavidch{
4211255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
4212255736Sdavidch
4213255736Sdavidch    /* Prepare parameters for function state transitions */
4214255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
4215255736Sdavidch
4216255736Sdavidch    func_params.f_obj = &sc->func_obj;
4217255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_RESET;
4218255736Sdavidch
4219255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
4220255736Sdavidch
4221255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
4222255736Sdavidch}
4223255736Sdavidch
4224255736Sdavidchstatic void
4225255736Sdavidchbxe_int_disable_sync(struct bxe_softc *sc,
4226255736Sdavidch                     int              disable_hw)
4227255736Sdavidch{
4228255736Sdavidch    if (disable_hw) {
4229255736Sdavidch        /* prevent the HW from sending interrupts */
4230255736Sdavidch        bxe_int_disable(sc);
4231255736Sdavidch    }
4232255736Sdavidch
4233255736Sdavidch    /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
4234255736Sdavidch    /* make sure all ISRs are done */
4235255736Sdavidch
4236255736Sdavidch    /* XXX make sure sp_task is not running */
4237255736Sdavidch    /* cancel and flush work queues */
4238255736Sdavidch}
4239255736Sdavidch
4240255736Sdavidchstatic void
4241255736Sdavidchbxe_chip_cleanup(struct bxe_softc *sc,
4242255736Sdavidch                 uint32_t         unload_mode,
4243255736Sdavidch                 uint8_t          keep_link)
4244255736Sdavidch{
4245255736Sdavidch    int port = SC_PORT(sc);
4246255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
4247255736Sdavidch    uint32_t reset_code;
4248255736Sdavidch    int i, rc = 0;
4249255736Sdavidch
4250255736Sdavidch    bxe_drain_tx_queues(sc);
4251255736Sdavidch
4252255736Sdavidch    /* give HW time to discard old tx messages */
4253255736Sdavidch    DELAY(1000);
4254255736Sdavidch
4255255736Sdavidch    /* Clean all ETH MACs */
4256255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4257255736Sdavidch    if (rc < 0) {
4258255736Sdavidch        BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4259255736Sdavidch    }
4260255736Sdavidch
4261255736Sdavidch    /* Clean up UC list  */
4262255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4263255736Sdavidch    if (rc < 0) {
4264255736Sdavidch        BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4265255736Sdavidch    }
4266255736Sdavidch
4267255736Sdavidch    /* Disable LLH */
4268255736Sdavidch    if (!CHIP_IS_E1(sc)) {
4269255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4270255736Sdavidch    }
4271255736Sdavidch
4272255736Sdavidch    /* Set "drop all" to stop Rx */
4273255736Sdavidch
4274255736Sdavidch    /*
4275255736Sdavidch     * We need to take the BXE_MCAST_LOCK() here in order to prevent
4276255736Sdavidch     * a race between the completion code and this code.
4277255736Sdavidch     */
4278255736Sdavidch    BXE_MCAST_LOCK(sc);
4279255736Sdavidch
4280255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4281255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4282255736Sdavidch    } else {
4283255736Sdavidch        bxe_set_storm_rx_mode(sc);
4284255736Sdavidch    }
4285255736Sdavidch
4286255736Sdavidch    /* Clean up multicast configuration */
4287255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4288255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4289255736Sdavidch    if (rc < 0) {
4290255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4291255736Sdavidch    }
4292255736Sdavidch
4293255736Sdavidch    BXE_MCAST_UNLOCK(sc);
4294255736Sdavidch
4295255736Sdavidch    // XXX bxe_iov_chip_cleanup(sc);
4296255736Sdavidch
4297255736Sdavidch    /*
4298255736Sdavidch     * Send the UNLOAD_REQUEST to the MCP. This will return if
4299255736Sdavidch     * this function should perform FUNCTION, PORT, or COMMON HW
4300255736Sdavidch     * reset.
4301255736Sdavidch     */
4302255736Sdavidch    reset_code = bxe_send_unload_req(sc, unload_mode);
4303255736Sdavidch
4304255736Sdavidch    /*
4305255736Sdavidch     * (assumption: No Attention from MCP at this stage)
4306255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
4307255736Sdavidch     */
4308255736Sdavidch    rc = bxe_func_wait_started(sc);
4309255736Sdavidch    if (rc) {
4310255736Sdavidch        BLOGE(sc, "bxe_func_wait_started failed\n");
4311255736Sdavidch    }
4312255736Sdavidch
4313255736Sdavidch    /*
4314255736Sdavidch     * Close multi and leading connections
4315255736Sdavidch     * Completions for ramrods are collected in a synchronous way
4316255736Sdavidch     */
4317255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
4318255736Sdavidch        if (bxe_stop_queue(sc, i)) {
4319255736Sdavidch            goto unload_error;
4320255736Sdavidch        }
4321255736Sdavidch    }
4322255736Sdavidch
4323255736Sdavidch    /*
4324255736Sdavidch     * If SP settings didn't get completed so far - something
4325255736Sdavidch     * very wrong has happen.
4326255736Sdavidch     */
4327255736Sdavidch    if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4328255736Sdavidch        BLOGE(sc, "Common slow path ramrods got stuck!\n");
4329255736Sdavidch    }
4330255736Sdavidch
4331255736Sdavidchunload_error:
4332255736Sdavidch
4333255736Sdavidch    rc = bxe_func_stop(sc);
4334255736Sdavidch    if (rc) {
4335255736Sdavidch        BLOGE(sc, "Function stop failed!\n");
4336255736Sdavidch    }
4337255736Sdavidch
4338255736Sdavidch    /* disable HW interrupts */
4339255736Sdavidch    bxe_int_disable_sync(sc, TRUE);
4340255736Sdavidch
4341255736Sdavidch    /* detach interrupts */
4342255736Sdavidch    bxe_interrupt_detach(sc);
4343255736Sdavidch
4344255736Sdavidch    /* Reset the chip */
4345255736Sdavidch    rc = bxe_reset_hw(sc, reset_code);
4346255736Sdavidch    if (rc) {
4347255736Sdavidch        BLOGE(sc, "Hardware reset failed\n");
4348255736Sdavidch    }
4349255736Sdavidch
4350255736Sdavidch    /* Report UNLOAD_DONE to MCP */
4351255736Sdavidch    bxe_send_unload_done(sc, keep_link);
4352255736Sdavidch}
4353255736Sdavidch
4354255736Sdavidchstatic void
4355255736Sdavidchbxe_disable_close_the_gate(struct bxe_softc *sc)
4356255736Sdavidch{
4357255736Sdavidch    uint32_t val;
4358255736Sdavidch    int port = SC_PORT(sc);
4359255736Sdavidch
4360255736Sdavidch    BLOGD(sc, DBG_LOAD,
4361255736Sdavidch          "Disabling 'close the gates'\n");
4362255736Sdavidch
4363255736Sdavidch    if (CHIP_IS_E1(sc)) {
4364255736Sdavidch        uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4365255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
4366255736Sdavidch        val = REG_RD(sc, addr);
4367255736Sdavidch        val &= ~(0x300);
4368255736Sdavidch        REG_WR(sc, addr, val);
4369255736Sdavidch    } else {
4370255736Sdavidch        val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4371255736Sdavidch        val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4372255736Sdavidch                 MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4373255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4374255736Sdavidch    }
4375255736Sdavidch}
4376255736Sdavidch
4377255736Sdavidch/*
4378255736Sdavidch * Cleans the object that have internal lists without sending
4379255736Sdavidch * ramrods. Should be run when interrutps are disabled.
4380255736Sdavidch */
4381255736Sdavidchstatic void
4382255736Sdavidchbxe_squeeze_objects(struct bxe_softc *sc)
4383255736Sdavidch{
4384255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4385255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
4386255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4387255736Sdavidch    int rc;
4388255736Sdavidch
4389255736Sdavidch    /* Cleanup MACs' object first... */
4390255736Sdavidch
4391255736Sdavidch    /* Wait for completion of requested */
4392255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4393255736Sdavidch    /* Perform a dry cleanup */
4394255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4395255736Sdavidch
4396255736Sdavidch    /* Clean ETH primary MAC */
4397255736Sdavidch    bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4398255736Sdavidch    rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4399255736Sdavidch                             &ramrod_flags);
4400255736Sdavidch    if (rc != 0) {
4401255736Sdavidch        BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4402255736Sdavidch    }
4403255736Sdavidch
4404255736Sdavidch    /* Cleanup UC list */
4405255736Sdavidch    vlan_mac_flags = 0;
4406255736Sdavidch    bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4407255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4408255736Sdavidch                             &ramrod_flags);
4409255736Sdavidch    if (rc != 0) {
4410255736Sdavidch        BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4411255736Sdavidch    }
4412255736Sdavidch
4413255736Sdavidch    /* Now clean mcast object... */
4414255736Sdavidch
4415255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4416255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4417255736Sdavidch
4418255736Sdavidch    /* Add a DEL command... */
4419255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4420255736Sdavidch    if (rc < 0) {
4421255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4422255736Sdavidch    }
4423255736Sdavidch
4424255736Sdavidch    /* now wait until all pending commands are cleared */
4425255736Sdavidch
4426255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4427255736Sdavidch    while (rc != 0) {
4428255736Sdavidch        if (rc < 0) {
4429255736Sdavidch            BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4430255736Sdavidch            return;
4431255736Sdavidch        }
4432255736Sdavidch
4433255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4434255736Sdavidch    }
4435255736Sdavidch}
4436255736Sdavidch
4437255736Sdavidch/* stop the controller */
4438255736Sdavidchstatic __noinline int
4439255736Sdavidchbxe_nic_unload(struct bxe_softc *sc,
4440255736Sdavidch               uint32_t         unload_mode,
4441255736Sdavidch               uint8_t          keep_link)
4442255736Sdavidch{
4443255736Sdavidch    uint8_t global = FALSE;
4444255736Sdavidch    uint32_t val;
4445255736Sdavidch
4446255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
4447255736Sdavidch
4448255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4449255736Sdavidch
4450255736Sdavidch    /* mark driver as unloaded in shmem2 */
4451255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4452255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4453255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4454255736Sdavidch                  val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4455255736Sdavidch    }
4456255736Sdavidch
4457255736Sdavidch    if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4458255736Sdavidch        (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4459255736Sdavidch        /*
4460255736Sdavidch         * We can get here if the driver has been unloaded
4461255736Sdavidch         * during parity error recovery and is either waiting for a
4462255736Sdavidch         * leader to complete or for other functions to unload and
4463255736Sdavidch         * then ifconfig down has been issued. In this case we want to
4464255736Sdavidch         * unload and let other functions to complete a recovery
4465255736Sdavidch         * process.
4466255736Sdavidch         */
4467255736Sdavidch        sc->recovery_state = BXE_RECOVERY_DONE;
4468255736Sdavidch        sc->is_leader = 0;
4469255736Sdavidch        bxe_release_leader_lock(sc);
4470255736Sdavidch        mb();
4471255736Sdavidch
4472255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4473255736Sdavidch        BLOGE(sc, "Can't unload in closed or error state\n");
4474255736Sdavidch        return (-1);
4475255736Sdavidch    }
4476255736Sdavidch
4477255736Sdavidch    /*
4478255736Sdavidch     * Nothing to do during unload if previous bxe_nic_load()
4479255736Sdavidch     * did not completed succesfully - all resourses are released.
4480255736Sdavidch     */
4481255736Sdavidch    if ((sc->state == BXE_STATE_CLOSED) ||
4482255736Sdavidch        (sc->state == BXE_STATE_ERROR)) {
4483255736Sdavidch        return (0);
4484255736Sdavidch    }
4485255736Sdavidch
4486255736Sdavidch    sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4487255736Sdavidch    mb();
4488255736Sdavidch
4489255736Sdavidch    /* stop tx */
4490255736Sdavidch    bxe_tx_disable(sc);
4491255736Sdavidch
4492255736Sdavidch    sc->rx_mode = BXE_RX_MODE_NONE;
4493255736Sdavidch    /* XXX set rx mode ??? */
4494255736Sdavidch
4495255736Sdavidch    if (IS_PF(sc)) {
4496255736Sdavidch        /* set ALWAYS_ALIVE bit in shmem */
4497255736Sdavidch        sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4498255736Sdavidch
4499255736Sdavidch        bxe_drv_pulse(sc);
4500255736Sdavidch
4501255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_STOP);
4502255736Sdavidch        bxe_save_statistics(sc);
4503255736Sdavidch    }
4504255736Sdavidch
4505255736Sdavidch    /* wait till consumers catch up with producers in all queues */
4506255736Sdavidch    bxe_drain_tx_queues(sc);
4507255736Sdavidch
4508255736Sdavidch    /* if VF indicate to PF this function is going down (PF will delete sp
4509255736Sdavidch     * elements and clear initializations
4510255736Sdavidch     */
4511255736Sdavidch    if (IS_VF(sc)) {
4512255736Sdavidch        ; /* bxe_vfpf_close_vf(sc); */
4513255736Sdavidch    } else if (unload_mode != UNLOAD_RECOVERY) {
4514255736Sdavidch        /* if this is a normal/close unload need to clean up chip */
4515255736Sdavidch        bxe_chip_cleanup(sc, unload_mode, keep_link);
4516255736Sdavidch    } else {
4517255736Sdavidch        /* Send the UNLOAD_REQUEST to the MCP */
4518255736Sdavidch        bxe_send_unload_req(sc, unload_mode);
4519255736Sdavidch
4520255736Sdavidch        /*
4521255736Sdavidch         * Prevent transactions to host from the functions on the
4522255736Sdavidch         * engine that doesn't reset global blocks in case of global
4523255736Sdavidch         * attention once gloabl blocks are reset and gates are opened
4524255736Sdavidch         * (the engine which leader will perform the recovery
4525255736Sdavidch         * last).
4526255736Sdavidch         */
4527255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
4528255736Sdavidch            bxe_pf_disable(sc);
4529255736Sdavidch        }
4530255736Sdavidch
4531255736Sdavidch        /* disable HW interrupts */
4532255736Sdavidch        bxe_int_disable_sync(sc, TRUE);
4533255736Sdavidch
4534255736Sdavidch        /* detach interrupts */
4535255736Sdavidch        bxe_interrupt_detach(sc);
4536255736Sdavidch
4537255736Sdavidch        /* Report UNLOAD_DONE to MCP */
4538255736Sdavidch        bxe_send_unload_done(sc, FALSE);
4539255736Sdavidch    }
4540255736Sdavidch
4541255736Sdavidch    /*
4542255736Sdavidch     * At this stage no more interrupts will arrive so we may safely clean
4543255736Sdavidch     * the queue'able objects here in case they failed to get cleaned so far.
4544255736Sdavidch     */
4545255736Sdavidch    if (IS_PF(sc)) {
4546255736Sdavidch        bxe_squeeze_objects(sc);
4547255736Sdavidch    }
4548255736Sdavidch
4549255736Sdavidch    /* There should be no more pending SP commands at this stage */
4550255736Sdavidch    sc->sp_state = 0;
4551255736Sdavidch
4552255736Sdavidch    sc->port.pmf = 0;
4553255736Sdavidch
4554255736Sdavidch    bxe_free_fp_buffers(sc);
4555255736Sdavidch
4556255736Sdavidch    if (IS_PF(sc)) {
4557255736Sdavidch        bxe_free_mem(sc);
4558255736Sdavidch    }
4559255736Sdavidch
4560255736Sdavidch    bxe_free_fw_stats_mem(sc);
4561255736Sdavidch
4562255736Sdavidch    sc->state = BXE_STATE_CLOSED;
4563255736Sdavidch
4564255736Sdavidch    /*
4565255736Sdavidch     * Check if there are pending parity attentions. If there are - set
4566255736Sdavidch     * RECOVERY_IN_PROGRESS.
4567255736Sdavidch     */
4568255736Sdavidch    if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4569255736Sdavidch        bxe_set_reset_in_progress(sc);
4570255736Sdavidch
4571255736Sdavidch        /* Set RESET_IS_GLOBAL if needed */
4572255736Sdavidch        if (global) {
4573255736Sdavidch            bxe_set_reset_global(sc);
4574255736Sdavidch        }
4575255736Sdavidch    }
4576255736Sdavidch
4577255736Sdavidch    /*
4578255736Sdavidch     * The last driver must disable a "close the gate" if there is no
4579255736Sdavidch     * parity attention or "process kill" pending.
4580255736Sdavidch     */
4581255736Sdavidch    if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4582255736Sdavidch        bxe_reset_is_done(sc, SC_PATH(sc))) {
4583255736Sdavidch        bxe_disable_close_the_gate(sc);
4584255736Sdavidch    }
4585255736Sdavidch
4586255736Sdavidch    BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4587255736Sdavidch
4588255736Sdavidch    return (0);
4589255736Sdavidch}
4590255736Sdavidch
4591255736Sdavidch/*
4592255736Sdavidch * Called by the OS to set various media options (i.e. link, speed, etc.) when
4593255736Sdavidch * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4594255736Sdavidch */
4595255736Sdavidchstatic int
4596266979Smarcelbxe_ifmedia_update(struct ifnet  *ifp)
4597255736Sdavidch{
4598266979Smarcel    struct bxe_softc *sc = (struct bxe_softc *)if_getsoftc(ifp);
4599255736Sdavidch    struct ifmedia *ifm;
4600255736Sdavidch
4601255736Sdavidch    ifm = &sc->ifmedia;
4602255736Sdavidch
4603255736Sdavidch    /* We only support Ethernet media type. */
4604255736Sdavidch    if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4605255736Sdavidch        return (EINVAL);
4606255736Sdavidch    }
4607255736Sdavidch
4608255736Sdavidch    switch (IFM_SUBTYPE(ifm->ifm_media)) {
4609255736Sdavidch    case IFM_AUTO:
4610255736Sdavidch         break;
4611255736Sdavidch    case IFM_10G_CX4:
4612255736Sdavidch    case IFM_10G_SR:
4613255736Sdavidch    case IFM_10G_T:
4614255736Sdavidch    case IFM_10G_TWINAX:
4615255736Sdavidch    default:
4616255736Sdavidch        /* We don't support changing the media type. */
4617255736Sdavidch        BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4618255736Sdavidch              IFM_SUBTYPE(ifm->ifm_media));
4619255736Sdavidch        return (EINVAL);
4620255736Sdavidch    }
4621255736Sdavidch
4622255736Sdavidch    return (0);
4623255736Sdavidch}
4624255736Sdavidch
4625255736Sdavidch/*
4626255736Sdavidch * Called by the OS to get the current media status (i.e. link, speed, etc.).
4627255736Sdavidch */
4628255736Sdavidchstatic void
4629255736Sdavidchbxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4630255736Sdavidch{
4631266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4632255736Sdavidch
4633255736Sdavidch    /* Report link down if the driver isn't running. */
4634266979Smarcel    if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
4635255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4636255736Sdavidch        return;
4637255736Sdavidch    }
4638255736Sdavidch
4639255736Sdavidch    /* Setup the default interface info. */
4640255736Sdavidch    ifmr->ifm_status = IFM_AVALID;
4641255736Sdavidch    ifmr->ifm_active = IFM_ETHER;
4642255736Sdavidch
4643255736Sdavidch    if (sc->link_vars.link_up) {
4644255736Sdavidch        ifmr->ifm_status |= IFM_ACTIVE;
4645255736Sdavidch    } else {
4646255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4647255736Sdavidch        return;
4648255736Sdavidch    }
4649255736Sdavidch
4650255736Sdavidch    ifmr->ifm_active |= sc->media;
4651255736Sdavidch
4652255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
4653255736Sdavidch        ifmr->ifm_active |= IFM_FDX;
4654255736Sdavidch    } else {
4655255736Sdavidch        ifmr->ifm_active |= IFM_HDX;
4656255736Sdavidch    }
4657255736Sdavidch}
4658255736Sdavidch
4659255736Sdavidchstatic int
4660255736Sdavidchbxe_ioctl_nvram(struct bxe_softc *sc,
4661255736Sdavidch                uint32_t         priv_op,
4662255736Sdavidch                struct ifreq     *ifr)
4663255736Sdavidch{
4664255736Sdavidch    struct bxe_nvram_data nvdata_base;
4665255736Sdavidch    struct bxe_nvram_data *nvdata;
4666255736Sdavidch    int len;
4667255736Sdavidch    int error = 0;
4668255736Sdavidch
4669255736Sdavidch    copyin(ifr->ifr_data, &nvdata_base, sizeof(nvdata_base));
4670255736Sdavidch
4671255736Sdavidch    len = (sizeof(struct bxe_nvram_data) +
4672255736Sdavidch           nvdata_base.len -
4673255736Sdavidch           sizeof(uint32_t));
4674255736Sdavidch
4675255736Sdavidch    if (len > sizeof(struct bxe_nvram_data)) {
4676255736Sdavidch        if ((nvdata = (struct bxe_nvram_data *)
4677255736Sdavidch                 malloc(len, M_DEVBUF,
4678255736Sdavidch                        (M_NOWAIT | M_ZERO))) == NULL) {
4679255736Sdavidch            BLOGE(sc, "BXE_IOC_RD_NVRAM malloc failed\n");
4680255736Sdavidch            return (1);
4681255736Sdavidch        }
4682255736Sdavidch        memcpy(nvdata, &nvdata_base, sizeof(struct bxe_nvram_data));
4683255736Sdavidch    } else {
4684255736Sdavidch        nvdata = &nvdata_base;
4685255736Sdavidch    }
4686255736Sdavidch
4687255736Sdavidch    if (priv_op == BXE_IOC_RD_NVRAM) {
4688255736Sdavidch        BLOGD(sc, DBG_IOCTL, "IOC_RD_NVRAM 0x%x %d\n",
4689255736Sdavidch              nvdata->offset, nvdata->len);
4690255736Sdavidch        error = bxe_nvram_read(sc,
4691255736Sdavidch                               nvdata->offset,
4692255736Sdavidch                               (uint8_t *)nvdata->value,
4693255736Sdavidch                               nvdata->len);
4694255736Sdavidch        copyout(nvdata, ifr->ifr_data, len);
4695255736Sdavidch    } else { /* BXE_IOC_WR_NVRAM */
4696255736Sdavidch        BLOGD(sc, DBG_IOCTL, "IOC_WR_NVRAM 0x%x %d\n",
4697255736Sdavidch              nvdata->offset, nvdata->len);
4698255736Sdavidch        copyin(ifr->ifr_data, nvdata, len);
4699255736Sdavidch        error = bxe_nvram_write(sc,
4700255736Sdavidch                                nvdata->offset,
4701255736Sdavidch                                (uint8_t *)nvdata->value,
4702255736Sdavidch                                nvdata->len);
4703255736Sdavidch    }
4704255736Sdavidch
4705255736Sdavidch    if (len > sizeof(struct bxe_nvram_data)) {
4706255736Sdavidch        free(nvdata, M_DEVBUF);
4707255736Sdavidch    }
4708255736Sdavidch
4709255736Sdavidch    return (error);
4710255736Sdavidch}
4711255736Sdavidch
4712255736Sdavidchstatic int
4713255736Sdavidchbxe_ioctl_stats_show(struct bxe_softc *sc,
4714255736Sdavidch                     uint32_t         priv_op,
4715255736Sdavidch                     struct ifreq     *ifr)
4716255736Sdavidch{
4717255736Sdavidch    const size_t str_size   = (BXE_NUM_ETH_STATS * STAT_NAME_LEN);
4718255736Sdavidch    const size_t stats_size = (BXE_NUM_ETH_STATS * sizeof(uint64_t));
4719255736Sdavidch    caddr_t p_tmp;
4720255736Sdavidch    uint32_t *offset;
4721255736Sdavidch    int i;
4722255736Sdavidch
4723255736Sdavidch    switch (priv_op)
4724255736Sdavidch    {
4725255736Sdavidch    case BXE_IOC_STATS_SHOW_NUM:
4726255736Sdavidch        memset(ifr->ifr_data, 0, sizeof(union bxe_stats_show_data));
4727255736Sdavidch        ((union bxe_stats_show_data *)ifr->ifr_data)->desc.num =
4728255736Sdavidch            BXE_NUM_ETH_STATS;
4729255736Sdavidch        ((union bxe_stats_show_data *)ifr->ifr_data)->desc.len =
4730255736Sdavidch            STAT_NAME_LEN;
4731255736Sdavidch        return (0);
4732255736Sdavidch
4733255736Sdavidch    case BXE_IOC_STATS_SHOW_STR:
4734255736Sdavidch        memset(ifr->ifr_data, 0, str_size);
4735255736Sdavidch        p_tmp = ifr->ifr_data;
4736255736Sdavidch        for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4737255736Sdavidch            strcpy(p_tmp, bxe_eth_stats_arr[i].string);
4738255736Sdavidch            p_tmp += STAT_NAME_LEN;
4739255736Sdavidch        }
4740255736Sdavidch        return (0);
4741255736Sdavidch
4742255736Sdavidch    case BXE_IOC_STATS_SHOW_CNT:
4743255736Sdavidch        memset(ifr->ifr_data, 0, stats_size);
4744255736Sdavidch        p_tmp = ifr->ifr_data;
4745255736Sdavidch        for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4746255736Sdavidch            offset = ((uint32_t *)&sc->eth_stats +
4747255736Sdavidch                      bxe_eth_stats_arr[i].offset);
4748255736Sdavidch            switch (bxe_eth_stats_arr[i].size) {
4749255736Sdavidch            case 4:
4750255736Sdavidch                *((uint64_t *)p_tmp) = (uint64_t)*offset;
4751255736Sdavidch                break;
4752255736Sdavidch            case 8:
4753255736Sdavidch                *((uint64_t *)p_tmp) = HILO_U64(*offset, *(offset + 1));
4754255736Sdavidch                break;
4755255736Sdavidch            default:
4756255736Sdavidch                *((uint64_t *)p_tmp) = 0;
4757255736Sdavidch            }
4758255736Sdavidch            p_tmp += sizeof(uint64_t);
4759255736Sdavidch        }
4760255736Sdavidch        return (0);
4761255736Sdavidch
4762255736Sdavidch    default:
4763255736Sdavidch        return (-1);
4764255736Sdavidch    }
4765255736Sdavidch}
4766255736Sdavidch
4767255736Sdavidchstatic void
4768255736Sdavidchbxe_handle_chip_tq(void *context,
4769255736Sdavidch                   int  pending)
4770255736Sdavidch{
4771255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
4772255736Sdavidch    long work = atomic_load_acq_long(&sc->chip_tq_flags);
4773255736Sdavidch
4774255736Sdavidch    switch (work)
4775255736Sdavidch    {
4776255736Sdavidch
4777255736Sdavidch    case CHIP_TQ_REINIT:
4778266979Smarcel        if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4779255736Sdavidch            /* restart the interface */
4780255736Sdavidch            BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4781255736Sdavidch            bxe_periodic_stop(sc);
4782255736Sdavidch            BXE_CORE_LOCK(sc);
4783255736Sdavidch            bxe_stop_locked(sc);
4784255736Sdavidch            bxe_init_locked(sc);
4785255736Sdavidch            BXE_CORE_UNLOCK(sc);
4786255736Sdavidch        }
4787255736Sdavidch        break;
4788255736Sdavidch
4789255736Sdavidch    default:
4790255736Sdavidch        break;
4791255736Sdavidch    }
4792255736Sdavidch}
4793255736Sdavidch
4794255736Sdavidch/*
4795255736Sdavidch * Handles any IOCTL calls from the operating system.
4796255736Sdavidch *
4797255736Sdavidch * Returns:
4798255736Sdavidch *   0 = Success, >0 Failure
4799255736Sdavidch */
4800255736Sdavidchstatic int
4801266979Smarcelbxe_ioctl(if_t ifp,
4802255736Sdavidch          u_long       command,
4803255736Sdavidch          caddr_t      data)
4804255736Sdavidch{
4805266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4806255736Sdavidch    struct ifreq *ifr = (struct ifreq *)data;
4807255736Sdavidch    struct bxe_nvram_data *nvdata;
4808255736Sdavidch    uint32_t priv_op;
4809255736Sdavidch    int mask = 0;
4810255736Sdavidch    int reinit = 0;
4811255736Sdavidch    int error = 0;
4812255736Sdavidch
4813255736Sdavidch    int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4814255736Sdavidch    int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4815255736Sdavidch
4816255736Sdavidch    switch (command)
4817255736Sdavidch    {
4818255736Sdavidch    case SIOCSIFMTU:
4819255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4820255736Sdavidch              ifr->ifr_mtu);
4821255736Sdavidch
4822255736Sdavidch        if (sc->mtu == ifr->ifr_mtu) {
4823255736Sdavidch            /* nothing to change */
4824255736Sdavidch            break;
4825255736Sdavidch        }
4826255736Sdavidch
4827255736Sdavidch        if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4828255736Sdavidch            BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4829255736Sdavidch                  ifr->ifr_mtu, mtu_min, mtu_max);
4830255736Sdavidch            error = EINVAL;
4831255736Sdavidch            break;
4832255736Sdavidch        }
4833255736Sdavidch
4834255736Sdavidch        atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4835255736Sdavidch                             (unsigned long)ifr->ifr_mtu);
4836266979Smarcel	/*
4837266979Smarcel        atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4838255736Sdavidch                              (unsigned long)ifr->ifr_mtu);
4839266979Smarcel	XXX - Not sure why it needs to be atomic
4840266979Smarcel	*/
4841266979Smarcel	if_setmtu(ifp, ifr->ifr_mtu);
4842255736Sdavidch        reinit = 1;
4843255736Sdavidch        break;
4844255736Sdavidch
4845255736Sdavidch    case SIOCSIFFLAGS:
4846255736Sdavidch        /* toggle the interface state up or down */
4847255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4848255736Sdavidch
4849284335Sdavidcs	BXE_CORE_LOCK(sc);
4850255736Sdavidch        /* check if the interface is up */
4851266979Smarcel        if (if_getflags(ifp) & IFF_UP) {
4852266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4853255736Sdavidch                /* set the receive mode flags */
4854255736Sdavidch                bxe_set_rx_mode(sc);
4855255736Sdavidch            } else {
4856284335Sdavidcs		bxe_init_locked(sc);
4857255736Sdavidch            }
4858255736Sdavidch        } else {
4859266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4860284335Sdavidcs		bxe_periodic_stop(sc);
4861284335Sdavidcs		bxe_stop_locked(sc);
4862255736Sdavidch            }
4863255736Sdavidch        }
4864284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4865255736Sdavidch
4866255736Sdavidch        break;
4867255736Sdavidch
4868255736Sdavidch    case SIOCADDMULTI:
4869255736Sdavidch    case SIOCDELMULTI:
4870255736Sdavidch        /* add/delete multicast addresses */
4871255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4872255736Sdavidch
4873255736Sdavidch        /* check if the interface is up */
4874266979Smarcel        if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4875255736Sdavidch            /* set the receive mode flags */
4876284335Sdavidcs	    BXE_CORE_LOCK(sc);
4877255736Sdavidch            bxe_set_rx_mode(sc);
4878284335Sdavidcs	    BXE_CORE_UNLOCK(sc);
4879255736Sdavidch        }
4880255736Sdavidch
4881255736Sdavidch        break;
4882255736Sdavidch
4883255736Sdavidch    case SIOCSIFCAP:
4884255736Sdavidch        /* find out which capabilities have changed */
4885266979Smarcel        mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4886255736Sdavidch
4887255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4888255736Sdavidch              mask);
4889255736Sdavidch
4890255736Sdavidch        /* toggle the LRO capabilites enable flag */
4891255736Sdavidch        if (mask & IFCAP_LRO) {
4892266979Smarcel	    if_togglecapenable(ifp, IFCAP_LRO);
4893255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4894266979Smarcel                  (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4895255736Sdavidch            reinit = 1;
4896255736Sdavidch        }
4897255736Sdavidch
4898255736Sdavidch        /* toggle the TXCSUM checksum capabilites enable flag */
4899255736Sdavidch        if (mask & IFCAP_TXCSUM) {
4900266979Smarcel	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4901255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4902266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4903266979Smarcel            if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4904266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4905255736Sdavidch                                    CSUM_TCP      |
4906255736Sdavidch                                    CSUM_UDP      |
4907255736Sdavidch                                    CSUM_TSO      |
4908255736Sdavidch                                    CSUM_TCP_IPV6 |
4909266979Smarcel                                    CSUM_UDP_IPV6), 0);
4910255736Sdavidch            } else {
4911266979Smarcel		if_clearhwassist(ifp); /* XXX */
4912255736Sdavidch            }
4913255736Sdavidch        }
4914255736Sdavidch
4915255736Sdavidch        /* toggle the RXCSUM checksum capabilities enable flag */
4916255736Sdavidch        if (mask & IFCAP_RXCSUM) {
4917266979Smarcel	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4918255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4919266979Smarcel                  (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4920266979Smarcel            if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4921266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4922255736Sdavidch                                    CSUM_TCP      |
4923255736Sdavidch                                    CSUM_UDP      |
4924255736Sdavidch                                    CSUM_TSO      |
4925255736Sdavidch                                    CSUM_TCP_IPV6 |
4926266979Smarcel                                    CSUM_UDP_IPV6), 0);
4927255736Sdavidch            } else {
4928266979Smarcel		if_clearhwassist(ifp); /* XXX */
4929255736Sdavidch            }
4930255736Sdavidch        }
4931255736Sdavidch
4932255736Sdavidch        /* toggle TSO4 capabilities enabled flag */
4933255736Sdavidch        if (mask & IFCAP_TSO4) {
4934266979Smarcel            if_togglecapenable(ifp, IFCAP_TSO4);
4935255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4936266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4937255736Sdavidch        }
4938255736Sdavidch
4939255736Sdavidch        /* toggle TSO6 capabilities enabled flag */
4940255736Sdavidch        if (mask & IFCAP_TSO6) {
4941266979Smarcel	    if_togglecapenable(ifp, IFCAP_TSO6);
4942255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4943266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4944255736Sdavidch        }
4945255736Sdavidch
4946255736Sdavidch        /* toggle VLAN_HWTSO capabilities enabled flag */
4947255736Sdavidch        if (mask & IFCAP_VLAN_HWTSO) {
4948266979Smarcel
4949266979Smarcel	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4950255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4951266979Smarcel                  (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4952255736Sdavidch        }
4953255736Sdavidch
4954255736Sdavidch        /* toggle VLAN_HWCSUM capabilities enabled flag */
4955255736Sdavidch        if (mask & IFCAP_VLAN_HWCSUM) {
4956255736Sdavidch            /* XXX investigate this... */
4957255736Sdavidch            BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4958255736Sdavidch            error = EINVAL;
4959255736Sdavidch        }
4960255736Sdavidch
4961255736Sdavidch        /* toggle VLAN_MTU capabilities enable flag */
4962255736Sdavidch        if (mask & IFCAP_VLAN_MTU) {
4963255736Sdavidch            /* XXX investigate this... */
4964255736Sdavidch            BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4965255736Sdavidch            error = EINVAL;
4966255736Sdavidch        }
4967255736Sdavidch
4968255736Sdavidch        /* toggle VLAN_HWTAGGING capabilities enabled flag */
4969255736Sdavidch        if (mask & IFCAP_VLAN_HWTAGGING) {
4970255736Sdavidch            /* XXX investigate this... */
4971255736Sdavidch            BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4972255736Sdavidch            error = EINVAL;
4973255736Sdavidch        }
4974255736Sdavidch
4975255736Sdavidch        /* toggle VLAN_HWFILTER capabilities enabled flag */
4976255736Sdavidch        if (mask & IFCAP_VLAN_HWFILTER) {
4977255736Sdavidch            /* XXX investigate this... */
4978255736Sdavidch            BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4979255736Sdavidch            error = EINVAL;
4980255736Sdavidch        }
4981255736Sdavidch
4982255736Sdavidch        /* XXX not yet...
4983255736Sdavidch         * IFCAP_WOL_MAGIC
4984255736Sdavidch         */
4985255736Sdavidch
4986255736Sdavidch        break;
4987255736Sdavidch
4988255736Sdavidch    case SIOCSIFMEDIA:
4989255736Sdavidch    case SIOCGIFMEDIA:
4990255736Sdavidch        /* set/get interface media */
4991255736Sdavidch        BLOGD(sc, DBG_IOCTL,
4992255736Sdavidch              "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4993255736Sdavidch              (command & 0xff));
4994270876Sglebius        error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4995255736Sdavidch        break;
4996255736Sdavidch
4997255736Sdavidch    case SIOCGPRIVATE_0:
4998255736Sdavidch        copyin(ifr->ifr_data, &priv_op, sizeof(priv_op));
4999255736Sdavidch
5000255736Sdavidch        switch (priv_op)
5001255736Sdavidch        {
5002255736Sdavidch        case BXE_IOC_RD_NVRAM:
5003255736Sdavidch        case BXE_IOC_WR_NVRAM:
5004255736Sdavidch            nvdata = (struct bxe_nvram_data *)ifr->ifr_data;
5005255736Sdavidch            BLOGD(sc, DBG_IOCTL,
5006255736Sdavidch                  "Received Private NVRAM ioctl addr=0x%x size=%u\n",
5007255736Sdavidch                  nvdata->offset, nvdata->len);
5008255736Sdavidch            error = bxe_ioctl_nvram(sc, priv_op, ifr);
5009255736Sdavidch            break;
5010255736Sdavidch
5011255736Sdavidch        case BXE_IOC_STATS_SHOW_NUM:
5012255736Sdavidch        case BXE_IOC_STATS_SHOW_STR:
5013255736Sdavidch        case BXE_IOC_STATS_SHOW_CNT:
5014255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Received Private Stats ioctl (%d)\n",
5015255736Sdavidch                  priv_op);
5016255736Sdavidch            error = bxe_ioctl_stats_show(sc, priv_op, ifr);
5017255736Sdavidch            break;
5018255736Sdavidch
5019255736Sdavidch        default:
5020255736Sdavidch            BLOGW(sc, "Received Private Unknown ioctl (%d)\n", priv_op);
5021255736Sdavidch            error = EINVAL;
5022255736Sdavidch            break;
5023255736Sdavidch        }
5024255736Sdavidch
5025255736Sdavidch        break;
5026255736Sdavidch
5027255736Sdavidch    default:
5028255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
5029255736Sdavidch              (command & 0xff));
5030270876Sglebius        error = ether_ioctl(ifp, command, data);
5031255736Sdavidch        break;
5032255736Sdavidch    }
5033255736Sdavidch
5034266979Smarcel    if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
5035255736Sdavidch        BLOGD(sc, DBG_LOAD | DBG_IOCTL,
5036255736Sdavidch              "Re-initializing hardware from IOCTL change\n");
5037284335Sdavidcs	bxe_periodic_stop(sc);
5038284335Sdavidcs	BXE_CORE_LOCK(sc);
5039284335Sdavidcs	bxe_stop_locked(sc);
5040284335Sdavidcs	bxe_init_locked(sc);
5041284335Sdavidcs	BXE_CORE_UNLOCK(sc);
5042255736Sdavidch    }
5043255736Sdavidch
5044255736Sdavidch    return (error);
5045255736Sdavidch}
5046255736Sdavidch
5047255736Sdavidchstatic __noinline void
5048255736Sdavidchbxe_dump_mbuf(struct bxe_softc *sc,
5049255736Sdavidch              struct mbuf      *m,
5050255736Sdavidch              uint8_t          contents)
5051255736Sdavidch{
5052255736Sdavidch    char * type;
5053260113Sedavis    int i = 0;
5054255736Sdavidch
5055255736Sdavidch    if (!(sc->debug & DBG_MBUF)) {
5056255736Sdavidch        return;
5057255736Sdavidch    }
5058255736Sdavidch
5059255736Sdavidch    if (m == NULL) {
5060255736Sdavidch        BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
5061255736Sdavidch        return;
5062255736Sdavidch    }
5063255736Sdavidch
5064255736Sdavidch    while (m) {
5065255736Sdavidch        BLOGD(sc, DBG_MBUF,
5066260113Sedavis              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
5067260113Sedavis              i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
5068255736Sdavidch
5069255736Sdavidch        if (m->m_flags & M_PKTHDR) {
5070255736Sdavidch             BLOGD(sc, DBG_MBUF,
5071260113Sedavis                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
5072260113Sedavis                   i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
5073260113Sedavis                   (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5074255736Sdavidch        }
5075255736Sdavidch
5076255736Sdavidch        if (m->m_flags & M_EXT) {
5077255736Sdavidch            switch (m->m_ext.ext_type) {
5078255736Sdavidch            case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
5079255736Sdavidch            case EXT_SFBUF:      type = "EXT_SFBUF";      break;
5080260113Sedavis            case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
5081255736Sdavidch            case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
5082255736Sdavidch            case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
5083255736Sdavidch            case EXT_PACKET:     type = "EXT_PACKET";     break;
5084255736Sdavidch            case EXT_MBUF:       type = "EXT_MBUF";       break;
5085255736Sdavidch            case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
5086255736Sdavidch            case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
5087255736Sdavidch            case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
5088255736Sdavidch            case EXT_EXTREF:     type = "EXT_EXTREF";     break;
5089255736Sdavidch            default:             type = "UNKNOWN";        break;
5090255736Sdavidch            }
5091255736Sdavidch
5092255736Sdavidch            BLOGD(sc, DBG_MBUF,
5093260113Sedavis                  "%02d: - m_ext: %p ext_size=%d type=%s\n",
5094260113Sedavis                  i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
5095255736Sdavidch        }
5096255736Sdavidch
5097255736Sdavidch        if (contents) {
5098255736Sdavidch            bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
5099255736Sdavidch        }
5100255736Sdavidch
5101255736Sdavidch        m = m->m_next;
5102260113Sedavis        i++;
5103255736Sdavidch    }
5104255736Sdavidch}
5105255736Sdavidch
5106255736Sdavidch/*
5107255736Sdavidch * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
5108255736Sdavidch * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
5109255736Sdavidch * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
5110255736Sdavidch * The headers comes in a seperate bd in FreeBSD so 13-3=10.
5111255736Sdavidch * Returns: 0 if OK to send, 1 if packet needs further defragmentation
5112255736Sdavidch */
5113255736Sdavidchstatic int
5114255736Sdavidchbxe_chktso_window(struct bxe_softc  *sc,
5115255736Sdavidch                  int               nsegs,
5116255736Sdavidch                  bus_dma_segment_t *segs,
5117255736Sdavidch                  struct mbuf       *m)
5118255736Sdavidch{
5119255736Sdavidch    uint32_t num_wnds, wnd_size, wnd_sum;
5120255736Sdavidch    int32_t frag_idx, wnd_idx;
5121255736Sdavidch    unsigned short lso_mss;
5122255736Sdavidch    int defrag;
5123255736Sdavidch
5124255736Sdavidch    defrag = 0;
5125255736Sdavidch    wnd_sum = 0;
5126255736Sdavidch    wnd_size = 10;
5127255736Sdavidch    num_wnds = nsegs - wnd_size;
5128255736Sdavidch    lso_mss = htole16(m->m_pkthdr.tso_segsz);
5129255736Sdavidch
5130255736Sdavidch    /*
5131255736Sdavidch     * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
5132255736Sdavidch     * first window sum of data while skipping the first assuming it is the
5133255736Sdavidch     * header in FreeBSD.
5134255736Sdavidch     */
5135255736Sdavidch    for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
5136255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
5137255736Sdavidch    }
5138255736Sdavidch
5139255736Sdavidch    /* check the first 10 bd window size */
5140255736Sdavidch    if (wnd_sum < lso_mss) {
5141255736Sdavidch        return (1);
5142255736Sdavidch    }
5143255736Sdavidch
5144255736Sdavidch    /* run through the windows */
5145255736Sdavidch    for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
5146255736Sdavidch        /* subtract the first mbuf->m_len of the last wndw(-header) */
5147255736Sdavidch        wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
5148255736Sdavidch        /* add the next mbuf len to the len of our new window */
5149255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
5150255736Sdavidch        if (wnd_sum < lso_mss) {
5151255736Sdavidch            return (1);
5152255736Sdavidch        }
5153255736Sdavidch    }
5154255736Sdavidch
5155255736Sdavidch    return (0);
5156255736Sdavidch}
5157255736Sdavidch
5158255736Sdavidchstatic uint8_t
5159255736Sdavidchbxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
5160255736Sdavidch                    struct mbuf         *m,
5161255736Sdavidch                    uint32_t            *parsing_data)
5162255736Sdavidch{
5163255736Sdavidch    struct ether_vlan_header *eh = NULL;
5164255736Sdavidch    struct ip *ip4 = NULL;
5165255736Sdavidch    struct ip6_hdr *ip6 = NULL;
5166255736Sdavidch    caddr_t ip = NULL;
5167255736Sdavidch    struct tcphdr *th = NULL;
5168255736Sdavidch    int e_hlen, ip_hlen, l4_off;
5169255736Sdavidch    uint16_t proto;
5170255736Sdavidch
5171255736Sdavidch    if (m->m_pkthdr.csum_flags == CSUM_IP) {
5172255736Sdavidch        /* no L4 checksum offload needed */
5173255736Sdavidch        return (0);
5174255736Sdavidch    }
5175255736Sdavidch
5176255736Sdavidch    /* get the Ethernet header */
5177255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5178255736Sdavidch
5179255736Sdavidch    /* handle VLAN encapsulation if present */
5180255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
5181255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5182255736Sdavidch        proto  = ntohs(eh->evl_proto);
5183255736Sdavidch    } else {
5184255736Sdavidch        e_hlen = ETHER_HDR_LEN;
5185255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
5186255736Sdavidch    }
5187255736Sdavidch
5188255736Sdavidch    switch (proto) {
5189255736Sdavidch    case ETHERTYPE_IP:
5190255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
5191255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
5192255736Sdavidch                  (struct ip *)m->m_next->m_data :
5193255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
5194255736Sdavidch        /* ip_hl is number of 32-bit words */
5195255736Sdavidch        ip_hlen = (ip4->ip_hl << 2);
5196255736Sdavidch        ip = (caddr_t)ip4;
5197255736Sdavidch        break;
5198255736Sdavidch    case ETHERTYPE_IPV6:
5199255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
5200255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
5201255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
5202255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
5203255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
5204255736Sdavidch        ip_hlen = sizeof(struct ip6_hdr);
5205255736Sdavidch        ip = (caddr_t)ip6;
5206255736Sdavidch        break;
5207255736Sdavidch    default:
5208255736Sdavidch        /* We can't offload in this case... */
5209255736Sdavidch        /* XXX error stat ??? */
5210255736Sdavidch        return (0);
5211255736Sdavidch    }
5212255736Sdavidch
5213255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5214255736Sdavidch    l4_off = (e_hlen + ip_hlen);
5215255736Sdavidch
5216255736Sdavidch    *parsing_data |=
5217255736Sdavidch        (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
5218255736Sdavidch         ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
5219255736Sdavidch
5220255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5221255736Sdavidch                                  CSUM_TSO |
5222255736Sdavidch                                  CSUM_TCP_IPV6)) {
5223255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5224255736Sdavidch        th = (struct tcphdr *)(ip + ip_hlen);
5225255736Sdavidch        /* th_off is number of 32-bit words */
5226255736Sdavidch        *parsing_data |= ((th->th_off <<
5227255736Sdavidch                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
5228255736Sdavidch                          ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
5229255736Sdavidch        return (l4_off + (th->th_off << 2)); /* entire header length */
5230255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5231255736Sdavidch                                         CSUM_UDP_IPV6)) {
5232255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5233255736Sdavidch        return (l4_off + sizeof(struct udphdr)); /* entire header length */
5234255736Sdavidch    } else {
5235255736Sdavidch        /* XXX error stat ??? */
5236255736Sdavidch        return (0);
5237255736Sdavidch    }
5238255736Sdavidch}
5239255736Sdavidch
5240255736Sdavidchstatic uint8_t
5241255736Sdavidchbxe_set_pbd_csum(struct bxe_fastpath        *fp,
5242255736Sdavidch                 struct mbuf                *m,
5243255736Sdavidch                 struct eth_tx_parse_bd_e1x *pbd)
5244255736Sdavidch{
5245255736Sdavidch    struct ether_vlan_header *eh = NULL;
5246255736Sdavidch    struct ip *ip4 = NULL;
5247255736Sdavidch    struct ip6_hdr *ip6 = NULL;
5248255736Sdavidch    caddr_t ip = NULL;
5249255736Sdavidch    struct tcphdr *th = NULL;
5250255736Sdavidch    struct udphdr *uh = NULL;
5251255736Sdavidch    int e_hlen, ip_hlen;
5252255736Sdavidch    uint16_t proto;
5253255736Sdavidch    uint8_t hlen;
5254255736Sdavidch    uint16_t tmp_csum;
5255255736Sdavidch    uint32_t *tmp_uh;
5256255736Sdavidch
5257255736Sdavidch    /* get the Ethernet header */
5258255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5259255736Sdavidch
5260255736Sdavidch    /* handle VLAN encapsulation if present */
5261255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
5262255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5263255736Sdavidch        proto  = ntohs(eh->evl_proto);
5264255736Sdavidch    } else {
5265255736Sdavidch        e_hlen = ETHER_HDR_LEN;
5266255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
5267255736Sdavidch    }
5268255736Sdavidch
5269255736Sdavidch    switch (proto) {
5270255736Sdavidch    case ETHERTYPE_IP:
5271255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
5272255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
5273255736Sdavidch                  (struct ip *)m->m_next->m_data :
5274255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
5275255736Sdavidch        /* ip_hl is number of 32-bit words */
5276255736Sdavidch        ip_hlen = (ip4->ip_hl << 1);
5277255736Sdavidch        ip = (caddr_t)ip4;
5278255736Sdavidch        break;
5279255736Sdavidch    case ETHERTYPE_IPV6:
5280255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
5281255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
5282255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
5283255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
5284255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
5285255736Sdavidch        ip_hlen = (sizeof(struct ip6_hdr) >> 1);
5286255736Sdavidch        ip = (caddr_t)ip6;
5287255736Sdavidch        break;
5288255736Sdavidch    default:
5289255736Sdavidch        /* We can't offload in this case... */
5290255736Sdavidch        /* XXX error stat ??? */
5291255736Sdavidch        return (0);
5292255736Sdavidch    }
5293255736Sdavidch
5294255736Sdavidch    hlen = (e_hlen >> 1);
5295255736Sdavidch
5296255736Sdavidch    /* note that rest of global_data is indirectly zeroed here */
5297255736Sdavidch    if (m->m_flags & M_VLANTAG) {
5298255736Sdavidch        pbd->global_data =
5299255736Sdavidch            htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
5300255736Sdavidch    } else {
5301255736Sdavidch        pbd->global_data = htole16(hlen);
5302255736Sdavidch    }
5303255736Sdavidch
5304255736Sdavidch    pbd->ip_hlen_w = ip_hlen;
5305255736Sdavidch
5306255736Sdavidch    hlen += pbd->ip_hlen_w;
5307255736Sdavidch
5308255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5309255736Sdavidch
5310255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5311255736Sdavidch                                  CSUM_TSO |
5312255736Sdavidch                                  CSUM_TCP_IPV6)) {
5313255736Sdavidch        th = (struct tcphdr *)(ip + (ip_hlen << 1));
5314255736Sdavidch        /* th_off is number of 32-bit words */
5315255736Sdavidch        hlen += (uint16_t)(th->th_off << 1);
5316255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5317255736Sdavidch                                         CSUM_UDP_IPV6)) {
5318255736Sdavidch        uh = (struct udphdr *)(ip + (ip_hlen << 1));
5319255736Sdavidch        hlen += (sizeof(struct udphdr) / 2);
5320255736Sdavidch    } else {
5321255736Sdavidch        /* valid case as only CSUM_IP was set */
5322255736Sdavidch        return (0);
5323255736Sdavidch    }
5324255736Sdavidch
5325255736Sdavidch    pbd->total_hlen_w = htole16(hlen);
5326255736Sdavidch
5327255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5328255736Sdavidch                                  CSUM_TSO |
5329255736Sdavidch                                  CSUM_TCP_IPV6)) {
5330255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5331255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5332255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5333255736Sdavidch                                         CSUM_UDP_IPV6)) {
5334255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5335255736Sdavidch
5336255736Sdavidch        /*
5337255736Sdavidch         * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5338255736Sdavidch         * checksums and does not know anything about the UDP header and where
5339255736Sdavidch         * the checksum field is located. It only knows about TCP. Therefore
5340255736Sdavidch         * we "lie" to the hardware for outgoing UDP packets w/ checksum
5341255736Sdavidch         * offload. Since the checksum field offset for TCP is 16 bytes and
5342255736Sdavidch         * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5343255736Sdavidch         * bytes less than the start of the UDP header. This allows the
5344255736Sdavidch         * hardware to write the checksum in the correct spot. But the
5345255736Sdavidch         * hardware will compute a checksum which includes the last 10 bytes
5346255736Sdavidch         * of the IP header. To correct this we tweak the stack computed
5347255736Sdavidch         * pseudo checksum by folding in the calculation of the inverse
5348255736Sdavidch         * checksum for those final 10 bytes of the IP header. This allows
5349255736Sdavidch         * the correct checksum to be computed by the hardware.
5350255736Sdavidch         */
5351255736Sdavidch
5352255736Sdavidch        /* set pointer 10 bytes before UDP header */
5353255736Sdavidch        tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5354255736Sdavidch
5355255736Sdavidch        /* calculate a pseudo header checksum over the first 10 bytes */
5356255736Sdavidch        tmp_csum = in_pseudo(*tmp_uh,
5357255736Sdavidch                             *(tmp_uh + 1),
5358255736Sdavidch                             *(uint16_t *)(tmp_uh + 2));
5359255736Sdavidch
5360255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5361255736Sdavidch    }
5362255736Sdavidch
5363255736Sdavidch    return (hlen * 2); /* entire header length, number of bytes */
5364255736Sdavidch}
5365255736Sdavidch
5366255736Sdavidchstatic void
5367255736Sdavidchbxe_set_pbd_lso_e2(struct mbuf *m,
5368255736Sdavidch                   uint32_t    *parsing_data)
5369255736Sdavidch{
5370255736Sdavidch    *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5371255736Sdavidch                       ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5372255736Sdavidch                      ETH_TX_PARSE_BD_E2_LSO_MSS);
5373255736Sdavidch
5374255736Sdavidch    /* XXX test for IPv6 with extension header... */
5375255736Sdavidch#if 0
5376255736Sdavidch    struct ip6_hdr *ip6;
5377255736Sdavidch    if (ip6 && ip6->ip6_nxt == 'some ipv6 extension header')
5378255736Sdavidch        *parsing_data |= ETH_TX_PARSE_BD_E2_IPV6_WITH_EXT_HDR;
5379255736Sdavidch#endif
5380255736Sdavidch}
5381255736Sdavidch
5382255736Sdavidchstatic void
5383255736Sdavidchbxe_set_pbd_lso(struct mbuf                *m,
5384255736Sdavidch                struct eth_tx_parse_bd_e1x *pbd)
5385255736Sdavidch{
5386255736Sdavidch    struct ether_vlan_header *eh = NULL;
5387255736Sdavidch    struct ip *ip = NULL;
5388255736Sdavidch    struct tcphdr *th = NULL;
5389255736Sdavidch    int e_hlen;
5390255736Sdavidch
5391255736Sdavidch    /* get the Ethernet header */
5392255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5393255736Sdavidch
5394255736Sdavidch    /* handle VLAN encapsulation if present */
5395255736Sdavidch    e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5396255736Sdavidch                 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5397255736Sdavidch
5398255736Sdavidch    /* get the IP and TCP header, with LSO entire header in first mbuf */
5399255736Sdavidch    /* XXX assuming IPv4 */
5400255736Sdavidch    ip = (struct ip *)(m->m_data + e_hlen);
5401255736Sdavidch    th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5402255736Sdavidch
5403255736Sdavidch    pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5404255736Sdavidch    pbd->tcp_send_seq = ntohl(th->th_seq);
5405255736Sdavidch    pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5406255736Sdavidch
5407255736Sdavidch#if 1
5408255736Sdavidch        /* XXX IPv4 */
5409255736Sdavidch        pbd->ip_id = ntohs(ip->ip_id);
5410255736Sdavidch        pbd->tcp_pseudo_csum =
5411255736Sdavidch            ntohs(in_pseudo(ip->ip_src.s_addr,
5412255736Sdavidch                            ip->ip_dst.s_addr,
5413255736Sdavidch                            htons(IPPROTO_TCP)));
5414255736Sdavidch#else
5415255736Sdavidch        /* XXX IPv6 */
5416255736Sdavidch        pbd->tcp_pseudo_csum =
5417255736Sdavidch            ntohs(in_pseudo(&ip6->ip6_src,
5418255736Sdavidch                            &ip6->ip6_dst,
5419255736Sdavidch                            htons(IPPROTO_TCP)));
5420255736Sdavidch#endif
5421255736Sdavidch
5422255736Sdavidch    pbd->global_data |=
5423255736Sdavidch        htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5424255736Sdavidch}
5425255736Sdavidch
5426255736Sdavidch/*
5427255736Sdavidch * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5428255736Sdavidch * visible to the controller.
5429255736Sdavidch *
5430255736Sdavidch * If an mbuf is submitted to this routine and cannot be given to the
5431255736Sdavidch * controller (e.g. it has too many fragments) then the function may free
5432255736Sdavidch * the mbuf and return to the caller.
5433255736Sdavidch *
5434255736Sdavidch * Returns:
5435255736Sdavidch *   0 = Success, !0 = Failure
5436255736Sdavidch *   Note the side effect that an mbuf may be freed if it causes a problem.
5437255736Sdavidch */
5438255736Sdavidchstatic int
5439255736Sdavidchbxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5440255736Sdavidch{
5441255736Sdavidch    bus_dma_segment_t segs[32];
5442255736Sdavidch    struct mbuf *m0;
5443255736Sdavidch    struct bxe_sw_tx_bd *tx_buf;
5444255736Sdavidch    struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5445255736Sdavidch    struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5446255736Sdavidch    /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5447255736Sdavidch    struct eth_tx_bd *tx_data_bd;
5448255736Sdavidch    struct eth_tx_bd *tx_total_pkt_size_bd;
5449255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
5450255736Sdavidch    uint16_t bd_prod, pkt_prod, total_pkt_size;
5451255736Sdavidch    uint8_t mac_type;
5452255736Sdavidch    int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5453255736Sdavidch    struct bxe_softc *sc;
5454255736Sdavidch    uint16_t tx_bd_avail;
5455255736Sdavidch    struct ether_vlan_header *eh;
5456255736Sdavidch    uint32_t pbd_e2_parsing_data = 0;
5457255736Sdavidch    uint8_t hlen = 0;
5458255736Sdavidch    int tmp_bd;
5459255736Sdavidch    int i;
5460255736Sdavidch
5461255736Sdavidch    sc = fp->sc;
5462255736Sdavidch
5463255736Sdavidch    M_ASSERTPKTHDR(*m_head);
5464255736Sdavidch
5465255736Sdavidch    m0 = *m_head;
5466255736Sdavidch    rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5467255736Sdavidch    tx_start_bd = NULL;
5468255736Sdavidch    tx_data_bd = NULL;
5469255736Sdavidch    tx_total_pkt_size_bd = NULL;
5470255736Sdavidch
5471255736Sdavidch    /* get the H/W pointer for packets and BDs */
5472255736Sdavidch    pkt_prod = fp->tx_pkt_prod;
5473255736Sdavidch    bd_prod = fp->tx_bd_prod;
5474255736Sdavidch
5475255736Sdavidch    mac_type = UNICAST_ADDRESS;
5476255736Sdavidch
5477255736Sdavidch    /* map the mbuf into the next open DMAable memory */
5478255736Sdavidch    tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5479255736Sdavidch    error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5480255736Sdavidch                                    tx_buf->m_map, m0,
5481255736Sdavidch                                    segs, &nsegs, BUS_DMA_NOWAIT);
5482255736Sdavidch
5483255736Sdavidch    /* mapping errors */
5484255736Sdavidch    if(__predict_false(error != 0)) {
5485255736Sdavidch        fp->eth_q_stats.tx_dma_mapping_failure++;
5486255736Sdavidch        if (error == ENOMEM) {
5487255736Sdavidch            /* resource issue, try again later */
5488255736Sdavidch            rc = ENOMEM;
5489255736Sdavidch        } else if (error == EFBIG) {
5490255736Sdavidch            /* possibly recoverable with defragmentation */
5491255736Sdavidch            fp->eth_q_stats.mbuf_defrag_attempts++;
5492260718Sglebius            m0 = m_defrag(*m_head, M_NOWAIT);
5493255736Sdavidch            if (m0 == NULL) {
5494255736Sdavidch                fp->eth_q_stats.mbuf_defrag_failures++;
5495255736Sdavidch                rc = ENOBUFS;
5496255736Sdavidch            } else {
5497255736Sdavidch                /* defrag successful, try mapping again */
5498255736Sdavidch                *m_head = m0;
5499255736Sdavidch                error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5500255736Sdavidch                                                tx_buf->m_map, m0,
5501255736Sdavidch                                                segs, &nsegs, BUS_DMA_NOWAIT);
5502255736Sdavidch                if (error) {
5503255736Sdavidch                    fp->eth_q_stats.tx_dma_mapping_failure++;
5504255736Sdavidch                    rc = error;
5505255736Sdavidch                }
5506255736Sdavidch            }
5507255736Sdavidch        } else {
5508255736Sdavidch            /* unknown, unrecoverable mapping error */
5509255736Sdavidch            BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5510255736Sdavidch            bxe_dump_mbuf(sc, m0, FALSE);
5511255736Sdavidch            rc = error;
5512255736Sdavidch        }
5513255736Sdavidch
5514255736Sdavidch        goto bxe_tx_encap_continue;
5515255736Sdavidch    }
5516255736Sdavidch
5517255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
5518255736Sdavidch
5519255736Sdavidch    /* make sure there is enough room in the send queue */
5520255736Sdavidch    if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5521255736Sdavidch        /* Recoverable, try again later. */
5522255736Sdavidch        fp->eth_q_stats.tx_hw_queue_full++;
5523255736Sdavidch        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5524255736Sdavidch        rc = ENOMEM;
5525255736Sdavidch        goto bxe_tx_encap_continue;
5526255736Sdavidch    }
5527255736Sdavidch
5528255736Sdavidch    /* capture the current H/W TX chain high watermark */
5529255736Sdavidch    if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5530255736Sdavidch                        (TX_BD_USABLE - tx_bd_avail))) {
5531255736Sdavidch        fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5532255736Sdavidch    }
5533255736Sdavidch
5534255736Sdavidch    /* make sure it fits in the packet window */
5535262999Sedavis    if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5536255736Sdavidch        /*
5537255736Sdavidch         * The mbuf may be to big for the controller to handle. If the frame
5538255736Sdavidch         * is a TSO frame we'll need to do an additional check.
5539255736Sdavidch         */
5540255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5541255736Sdavidch            if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5542255736Sdavidch                goto bxe_tx_encap_continue; /* OK to send */
5543255736Sdavidch            } else {
5544255736Sdavidch                fp->eth_q_stats.tx_window_violation_tso++;
5545255736Sdavidch            }
5546255736Sdavidch        } else {
5547255736Sdavidch            fp->eth_q_stats.tx_window_violation_std++;
5548255736Sdavidch        }
5549255736Sdavidch
5550262999Sedavis        /* lets try to defragment this mbuf and remap it */
5551260415Sedavis        fp->eth_q_stats.mbuf_defrag_attempts++;
5552262999Sedavis        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5553255736Sdavidch
5554260718Sglebius        m0 = m_defrag(*m_head, M_NOWAIT);
5555260415Sedavis        if (m0 == NULL) {
5556260415Sedavis            fp->eth_q_stats.mbuf_defrag_failures++;
5557260415Sedavis            /* Ugh, just drop the frame... :( */
5558260415Sedavis            rc = ENOBUFS;
5559260415Sedavis        } else {
5560260415Sedavis            /* defrag successful, try mapping again */
5561260415Sedavis            *m_head = m0;
5562260415Sedavis            error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5563260415Sedavis                                            tx_buf->m_map, m0,
5564260415Sedavis                                            segs, &nsegs, BUS_DMA_NOWAIT);
5565260415Sedavis            if (error) {
5566260415Sedavis                fp->eth_q_stats.tx_dma_mapping_failure++;
5567260415Sedavis                /* No sense in trying to defrag/copy chain, drop it. :( */
5568260415Sedavis                rc = error;
5569260415Sedavis            }
5570262999Sedavis            else {
5571262999Sedavis                /* if the chain is still too long then drop it */
5572262999Sedavis                if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5573262999Sedavis                    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5574262999Sedavis                    rc = ENODEV;
5575262999Sedavis                }
5576260415Sedavis            }
5577260415Sedavis        }
5578255736Sdavidch    }
5579255736Sdavidch
5580255736Sdavidchbxe_tx_encap_continue:
5581255736Sdavidch
5582255736Sdavidch    /* Check for errors */
5583255736Sdavidch    if (rc) {
5584255736Sdavidch        if (rc == ENOMEM) {
5585255736Sdavidch            /* recoverable try again later  */
5586255736Sdavidch        } else {
5587255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
5588255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tx--;
5589255736Sdavidch            m_freem(*m_head);
5590255736Sdavidch            *m_head = NULL;
5591255736Sdavidch        }
5592255736Sdavidch
5593255736Sdavidch        return (rc);
5594255736Sdavidch    }
5595255736Sdavidch
5596255736Sdavidch    /* set flag according to packet type (UNICAST_ADDRESS is default) */
5597255736Sdavidch    if (m0->m_flags & M_BCAST) {
5598255736Sdavidch        mac_type = BROADCAST_ADDRESS;
5599255736Sdavidch    } else if (m0->m_flags & M_MCAST) {
5600255736Sdavidch        mac_type = MULTICAST_ADDRESS;
5601255736Sdavidch    }
5602255736Sdavidch
5603255736Sdavidch    /* store the mbuf into the mbuf ring */
5604255736Sdavidch    tx_buf->m        = m0;
5605255736Sdavidch    tx_buf->first_bd = fp->tx_bd_prod;
5606255736Sdavidch    tx_buf->flags    = 0;
5607255736Sdavidch
5608255736Sdavidch    /* prepare the first transmit (start) BD for the mbuf */
5609255736Sdavidch    tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5610255736Sdavidch
5611255736Sdavidch    BLOGD(sc, DBG_TX,
5612255736Sdavidch          "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5613255736Sdavidch          pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5614255736Sdavidch
5615255736Sdavidch    tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5616255736Sdavidch    tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5617255736Sdavidch    tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5618255736Sdavidch    total_pkt_size += tx_start_bd->nbytes;
5619255736Sdavidch    tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5620255736Sdavidch
5621255736Sdavidch    tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5622255736Sdavidch
5623255736Sdavidch    /* all frames have at least Start BD + Parsing BD */
5624255736Sdavidch    nbds = nsegs + 1;
5625255736Sdavidch    tx_start_bd->nbd = htole16(nbds);
5626255736Sdavidch
5627255736Sdavidch    if (m0->m_flags & M_VLANTAG) {
5628255736Sdavidch        tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5629255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |=
5630255736Sdavidch            (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5631255736Sdavidch    } else {
5632255736Sdavidch        /* vf tx, start bd must hold the ethertype for fw to enforce it */
5633255736Sdavidch        if (IS_VF(sc)) {
5634255736Sdavidch            /* map ethernet header to find type and header length */
5635255736Sdavidch            eh = mtod(m0, struct ether_vlan_header *);
5636255736Sdavidch            tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5637255736Sdavidch        } else {
5638255736Sdavidch            /* used by FW for packet accounting */
5639255736Sdavidch            tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5640255736Sdavidch#if 0
5641255736Sdavidch            /*
5642255736Sdavidch             * If NPAR-SD is active then FW should do the tagging regardless
5643255736Sdavidch             * of value of priority. Otherwise, if priority indicates this is
5644255736Sdavidch             * a control packet we need to indicate to FW to avoid tagging.
5645255736Sdavidch             */
5646255736Sdavidch            if (!IS_MF_AFEX(sc) && (mbuf priority == PRIO_CONTROL)) {
5647255736Sdavidch                SET_FLAG(tx_start_bd->general_data,
5648255736Sdavidch                         ETH_TX_START_BD_FORCE_VLAN_MODE, 1);
5649255736Sdavidch            }
5650255736Sdavidch#endif
5651255736Sdavidch        }
5652255736Sdavidch    }
5653255736Sdavidch
5654255736Sdavidch    /*
5655255736Sdavidch     * add a parsing BD from the chain. The parsing BD is always added
5656255736Sdavidch     * though it is only used for TSO and chksum
5657255736Sdavidch     */
5658255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5659255736Sdavidch
5660255736Sdavidch    if (m0->m_pkthdr.csum_flags) {
5661255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5662255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5663255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5664255736Sdavidch        }
5665255736Sdavidch
5666255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5667255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5668255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5669255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5670255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5671255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP |
5672255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5673255736Sdavidch        } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5674255736Sdavidch                   (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5675255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5676255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5677255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5678255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP);
5679255736Sdavidch        }
5680255736Sdavidch    }
5681255736Sdavidch
5682255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
5683255736Sdavidch        pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5684255736Sdavidch        memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5685255736Sdavidch
5686255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5687255736Sdavidch            hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5688255736Sdavidch        }
5689255736Sdavidch
5690255736Sdavidch#if 0
5691255736Sdavidch        /*
5692255736Sdavidch         * Add the MACs to the parsing BD if the module param was
5693255736Sdavidch         * explicitly set, if this is a vf, or in switch independent
5694255736Sdavidch         * mode.
5695255736Sdavidch         */
5696255736Sdavidch        if (sc->flags & BXE_TX_SWITCHING || IS_VF(sc) || IS_MF_SI(sc)) {
5697255736Sdavidch            eh = mtod(m0, struct ether_vlan_header *);
5698255736Sdavidch            bxe_set_fw_mac_addr(&pbd_e2->data.mac_addr.src_hi,
5699255736Sdavidch                                &pbd_e2->data.mac_addr.src_mid,
5700255736Sdavidch                                &pbd_e2->data.mac_addr.src_lo,
5701255736Sdavidch                                eh->evl_shost);
5702255736Sdavidch            bxe_set_fw_mac_addr(&pbd_e2->data.mac_addr.dst_hi,
5703255736Sdavidch                                &pbd_e2->data.mac_addr.dst_mid,
5704255736Sdavidch                                &pbd_e2->data.mac_addr.dst_lo,
5705255736Sdavidch                                eh->evl_dhost);
5706255736Sdavidch        }
5707255736Sdavidch#endif
5708255736Sdavidch
5709255736Sdavidch        SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5710255736Sdavidch                 mac_type);
5711255736Sdavidch    } else {
5712255736Sdavidch        uint16_t global_data = 0;
5713255736Sdavidch
5714255736Sdavidch        pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5715255736Sdavidch        memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5716255736Sdavidch
5717255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5718255736Sdavidch            hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5719255736Sdavidch        }
5720255736Sdavidch
5721255736Sdavidch        SET_FLAG(global_data,
5722255736Sdavidch                 ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5723255736Sdavidch        pbd_e1x->global_data |= htole16(global_data);
5724255736Sdavidch    }
5725255736Sdavidch
5726255736Sdavidch    /* setup the parsing BD with TSO specific info */
5727255736Sdavidch    if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5728255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_lso++;
5729255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5730255736Sdavidch
5731255736Sdavidch        if (__predict_false(tx_start_bd->nbytes > hlen)) {
5732255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5733255736Sdavidch
5734255736Sdavidch            /* split the first BD into header/data making the fw job easy */
5735255736Sdavidch            nbds++;
5736255736Sdavidch            tx_start_bd->nbd = htole16(nbds);
5737260113Sedavis            tx_start_bd->nbytes = htole16(hlen);
5738255736Sdavidch
5739255736Sdavidch            bd_prod = TX_BD_NEXT(bd_prod);
5740255736Sdavidch
5741255736Sdavidch            /* new transmit BD after the tx_parse_bd */
5742255736Sdavidch            tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5743255736Sdavidch            tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5744255736Sdavidch            tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5745255736Sdavidch            tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5746255736Sdavidch            if (tx_total_pkt_size_bd == NULL) {
5747255736Sdavidch                tx_total_pkt_size_bd = tx_data_bd;
5748255736Sdavidch            }
5749255736Sdavidch
5750255736Sdavidch            BLOGD(sc, DBG_TX,
5751255736Sdavidch                  "TSO split header size is %d (%x:%x) nbds %d\n",
5752255736Sdavidch                  le16toh(tx_start_bd->nbytes),
5753255736Sdavidch                  le32toh(tx_start_bd->addr_hi),
5754255736Sdavidch                  le32toh(tx_start_bd->addr_lo),
5755255736Sdavidch                  nbds);
5756255736Sdavidch        }
5757255736Sdavidch
5758255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
5759255736Sdavidch            bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5760255736Sdavidch        } else {
5761255736Sdavidch            bxe_set_pbd_lso(m0, pbd_e1x);
5762255736Sdavidch        }
5763255736Sdavidch    }
5764255736Sdavidch
5765255736Sdavidch    if (pbd_e2_parsing_data) {
5766255736Sdavidch        pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5767255736Sdavidch    }
5768255736Sdavidch
5769255736Sdavidch    /* prepare remaining BDs, start tx bd contains first seg/frag */
5770255736Sdavidch    for (i = 1; i < nsegs ; i++) {
5771255736Sdavidch        bd_prod = TX_BD_NEXT(bd_prod);
5772255736Sdavidch        tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5773255736Sdavidch        tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5774255736Sdavidch        tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5775255736Sdavidch        tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5776255736Sdavidch        if (tx_total_pkt_size_bd == NULL) {
5777255736Sdavidch            tx_total_pkt_size_bd = tx_data_bd;
5778255736Sdavidch        }
5779255736Sdavidch        total_pkt_size += tx_data_bd->nbytes;
5780255736Sdavidch    }
5781255736Sdavidch
5782255736Sdavidch    BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5783255736Sdavidch
5784255736Sdavidch    if (tx_total_pkt_size_bd != NULL) {
5785255736Sdavidch        tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5786255736Sdavidch    }
5787255736Sdavidch
5788255736Sdavidch    if (__predict_false(sc->debug & DBG_TX)) {
5789255736Sdavidch        tmp_bd = tx_buf->first_bd;
5790255736Sdavidch        for (i = 0; i < nbds; i++)
5791255736Sdavidch        {
5792255736Sdavidch            if (i == 0) {
5793255736Sdavidch                BLOGD(sc, DBG_TX,
5794255736Sdavidch                      "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5795255736Sdavidch                      "bd_flags=0x%x hdr_nbds=%d\n",
5796255736Sdavidch                      tx_start_bd,
5797255736Sdavidch                      tmp_bd,
5798255736Sdavidch                      le16toh(tx_start_bd->nbd),
5799255736Sdavidch                      le16toh(tx_start_bd->vlan_or_ethertype),
5800255736Sdavidch                      tx_start_bd->bd_flags.as_bitfield,
5801255736Sdavidch                      (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5802255736Sdavidch            } else if (i == 1) {
5803255736Sdavidch                if (pbd_e1x) {
5804255736Sdavidch                    BLOGD(sc, DBG_TX,
5805255736Sdavidch                          "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5806255736Sdavidch                          "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5807255736Sdavidch                          "tcp_seq=%u total_hlen_w=%u\n",
5808255736Sdavidch                          pbd_e1x,
5809255736Sdavidch                          tmp_bd,
5810255736Sdavidch                          pbd_e1x->global_data,
5811255736Sdavidch                          pbd_e1x->ip_hlen_w,
5812255736Sdavidch                          pbd_e1x->ip_id,
5813255736Sdavidch                          pbd_e1x->lso_mss,
5814255736Sdavidch                          pbd_e1x->tcp_flags,
5815255736Sdavidch                          pbd_e1x->tcp_pseudo_csum,
5816255736Sdavidch                          pbd_e1x->tcp_send_seq,
5817255736Sdavidch                          le16toh(pbd_e1x->total_hlen_w));
5818255736Sdavidch                } else { /* if (pbd_e2) */
5819255736Sdavidch                    BLOGD(sc, DBG_TX,
5820255736Sdavidch                          "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5821255736Sdavidch                          "src=%02x:%02x:%02x parsing_data=0x%x\n",
5822255736Sdavidch                          pbd_e2,
5823255736Sdavidch                          tmp_bd,
5824255736Sdavidch                          pbd_e2->data.mac_addr.dst_hi,
5825255736Sdavidch                          pbd_e2->data.mac_addr.dst_mid,
5826255736Sdavidch                          pbd_e2->data.mac_addr.dst_lo,
5827255736Sdavidch                          pbd_e2->data.mac_addr.src_hi,
5828255736Sdavidch                          pbd_e2->data.mac_addr.src_mid,
5829255736Sdavidch                          pbd_e2->data.mac_addr.src_lo,
5830255736Sdavidch                          pbd_e2->parsing_data);
5831255736Sdavidch                }
5832255736Sdavidch            }
5833255736Sdavidch
5834255736Sdavidch            if (i != 1) { /* skip parse db as it doesn't hold data */
5835255736Sdavidch                tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5836255736Sdavidch                BLOGD(sc, DBG_TX,
5837255736Sdavidch                      "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5838255736Sdavidch                      tx_data_bd,
5839255736Sdavidch                      tmp_bd,
5840255736Sdavidch                      le16toh(tx_data_bd->nbytes),
5841255736Sdavidch                      le32toh(tx_data_bd->addr_hi),
5842255736Sdavidch                      le32toh(tx_data_bd->addr_lo));
5843255736Sdavidch            }
5844255736Sdavidch
5845255736Sdavidch            tmp_bd = TX_BD_NEXT(tmp_bd);
5846255736Sdavidch        }
5847255736Sdavidch    }
5848255736Sdavidch
5849255736Sdavidch    BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5850255736Sdavidch
5851255736Sdavidch    /* update TX BD producer index value for next TX */
5852255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5853255736Sdavidch
5854255736Sdavidch    /*
5855255736Sdavidch     * If the chain of tx_bd's describing this frame is adjacent to or spans
5856255736Sdavidch     * an eth_tx_next_bd element then we need to increment the nbds value.
5857255736Sdavidch     */
5858255736Sdavidch    if (TX_BD_IDX(bd_prod) < nbds) {
5859255736Sdavidch        nbds++;
5860255736Sdavidch    }
5861255736Sdavidch
5862255736Sdavidch    /* don't allow reordering of writes for nbd and packets */
5863255736Sdavidch    mb();
5864255736Sdavidch
5865255736Sdavidch    fp->tx_db.data.prod += nbds;
5866255736Sdavidch
5867255736Sdavidch    /* producer points to the next free tx_bd at this point */
5868255736Sdavidch    fp->tx_pkt_prod++;
5869255736Sdavidch    fp->tx_bd_prod = bd_prod;
5870255736Sdavidch
5871255736Sdavidch    DOORBELL(sc, fp->index, fp->tx_db.raw);
5872255736Sdavidch
5873255736Sdavidch    fp->eth_q_stats.tx_pkts++;
5874255736Sdavidch
5875255736Sdavidch    /* Prevent speculative reads from getting ahead of the status block. */
5876255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5877255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5878255736Sdavidch
5879255736Sdavidch    /* Prevent speculative reads from getting ahead of the doorbell. */
5880255736Sdavidch    bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5881255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5882255736Sdavidch
5883255736Sdavidch    return (0);
5884255736Sdavidch}
5885255736Sdavidch
5886255736Sdavidchstatic void
5887266979Smarcelbxe_tx_start_locked(struct bxe_softc *sc,
5888266979Smarcel                    if_t ifp,
5889255736Sdavidch                    struct bxe_fastpath *fp)
5890255736Sdavidch{
5891255736Sdavidch    struct mbuf *m = NULL;
5892255736Sdavidch    int tx_count = 0;
5893255736Sdavidch    uint16_t tx_bd_avail;
5894255736Sdavidch
5895255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
5896255736Sdavidch
5897255736Sdavidch    /* keep adding entries while there are frames to send */
5898266979Smarcel    while (!if_sendq_empty(ifp)) {
5899255736Sdavidch
5900255736Sdavidch        /*
5901255736Sdavidch         * check for any frames to send
5902255736Sdavidch         * dequeue can still be NULL even if queue is not empty
5903255736Sdavidch         */
5904266979Smarcel        m = if_dequeue(ifp);
5905255736Sdavidch        if (__predict_false(m == NULL)) {
5906255736Sdavidch            break;
5907255736Sdavidch        }
5908255736Sdavidch
5909255736Sdavidch        /* the mbuf now belongs to us */
5910255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5911255736Sdavidch
5912255736Sdavidch        /*
5913255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5914255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5915255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5916255736Sdavidch         */
5917255736Sdavidch        if (__predict_false(bxe_tx_encap(fp, &m))) {
5918255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5919255736Sdavidch            if (m != NULL) {
5920255736Sdavidch                /* mark the TX queue as full and return the frame */
5921266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5922266979Smarcel		if_sendq_prepend(ifp, m);
5923255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5924255736Sdavidch                fp->eth_q_stats.tx_queue_xoff++;
5925255736Sdavidch            }
5926255736Sdavidch
5927255736Sdavidch            /* stop looking for more work */
5928255736Sdavidch            break;
5929255736Sdavidch        }
5930255736Sdavidch
5931255736Sdavidch        /* the frame was enqueued successfully */
5932255736Sdavidch        tx_count++;
5933255736Sdavidch
5934255736Sdavidch        /* send a copy of the frame to any BPF listeners. */
5935266979Smarcel        if_etherbpfmtap(ifp, m);
5936255736Sdavidch
5937255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
5938255736Sdavidch
5939255736Sdavidch        /* handle any completions if we're running low */
5940255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5941258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5942255736Sdavidch            bxe_txeof(sc, fp);
5943266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5944258187Sedavis                break;
5945258187Sedavis            }
5946255736Sdavidch        }
5947255736Sdavidch    }
5948255736Sdavidch
5949255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5950255736Sdavidch    if (tx_count > 0) {
5951255736Sdavidch        /* reset the TX watchdog timeout timer */
5952255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5953255736Sdavidch    }
5954255736Sdavidch}
5955255736Sdavidch
5956255736Sdavidch/* Legacy (non-RSS) dispatch routine */
5957255736Sdavidchstatic void
5958266979Smarcelbxe_tx_start(if_t ifp)
5959255736Sdavidch{
5960255736Sdavidch    struct bxe_softc *sc;
5961255736Sdavidch    struct bxe_fastpath *fp;
5962255736Sdavidch
5963266979Smarcel    sc = if_getsoftc(ifp);
5964255736Sdavidch
5965266979Smarcel    if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5966255736Sdavidch        BLOGW(sc, "Interface not running, ignoring transmit request\n");
5967255736Sdavidch        return;
5968255736Sdavidch    }
5969255736Sdavidch
5970266979Smarcel    if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5971255736Sdavidch        BLOGW(sc, "Interface TX queue is full, ignoring transmit request\n");
5972255736Sdavidch        return;
5973255736Sdavidch    }
5974255736Sdavidch
5975255736Sdavidch    if (!sc->link_vars.link_up) {
5976255736Sdavidch        BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5977255736Sdavidch        return;
5978255736Sdavidch    }
5979255736Sdavidch
5980255736Sdavidch    fp = &sc->fp[0];
5981255736Sdavidch
5982255736Sdavidch    BXE_FP_TX_LOCK(fp);
5983255736Sdavidch    bxe_tx_start_locked(sc, ifp, fp);
5984255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
5985255736Sdavidch}
5986255736Sdavidch
5987255736Sdavidch#if __FreeBSD_version >= 800000
5988255736Sdavidch
5989255736Sdavidchstatic int
5990255736Sdavidchbxe_tx_mq_start_locked(struct bxe_softc    *sc,
5991266979Smarcel                       if_t                ifp,
5992255736Sdavidch                       struct bxe_fastpath *fp,
5993255736Sdavidch                       struct mbuf         *m)
5994255736Sdavidch{
5995255736Sdavidch    struct buf_ring *tx_br = fp->tx_br;
5996255736Sdavidch    struct mbuf *next;
5997255736Sdavidch    int depth, rc, tx_count;
5998255736Sdavidch    uint16_t tx_bd_avail;
5999255736Sdavidch
6000255736Sdavidch    rc = tx_count = 0;
6001255736Sdavidch
6002255736Sdavidch    if (!tx_br) {
6003255736Sdavidch        BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
6004255736Sdavidch        return (EINVAL);
6005255736Sdavidch    }
6006255736Sdavidch
6007255736Sdavidch    /* fetch the depth of the driver queue */
6008266979Smarcel    depth = drbr_inuse_drv(ifp, tx_br);
6009255736Sdavidch    if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
6010255736Sdavidch        fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
6011255736Sdavidch    }
6012255736Sdavidch
6013255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
6014255736Sdavidch
6015255736Sdavidch    if (m == NULL) {
6016255736Sdavidch        /* no new work, check for pending frames */
6017266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
6018266979Smarcel    } else if (drbr_needs_enqueue_drv(ifp, tx_br)) {
6019255736Sdavidch        /* have both new and pending work, maintain packet order */
6020266979Smarcel        rc = drbr_enqueue_drv(ifp, tx_br, m);
6021255736Sdavidch        if (rc != 0) {
6022255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
6023255736Sdavidch            goto bxe_tx_mq_start_locked_exit;
6024255736Sdavidch        }
6025266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
6026255736Sdavidch    } else {
6027255736Sdavidch        /* new work only and nothing pending */
6028255736Sdavidch        next = m;
6029255736Sdavidch    }
6030255736Sdavidch
6031255736Sdavidch    /* keep adding entries while there are frames to send */
6032255736Sdavidch    while (next != NULL) {
6033255736Sdavidch
6034255736Sdavidch        /* the mbuf now belongs to us */
6035255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
6036255736Sdavidch
6037255736Sdavidch        /*
6038255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
6039255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
6040255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
6041255736Sdavidch         */
6042255736Sdavidch        rc = bxe_tx_encap(fp, &next);
6043255736Sdavidch        if (__predict_false(rc != 0)) {
6044255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
6045255736Sdavidch            if (next != NULL) {
6046255736Sdavidch                /* mark the TX queue as full and save the frame */
6047266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
6048255736Sdavidch                /* XXX this may reorder the frame */
6049266979Smarcel                rc = drbr_enqueue_drv(ifp, tx_br, next);
6050255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
6051255736Sdavidch                fp->eth_q_stats.tx_frames_deferred++;
6052255736Sdavidch            }
6053255736Sdavidch
6054255736Sdavidch            /* stop looking for more work */
6055255736Sdavidch            break;
6056255736Sdavidch        }
6057255736Sdavidch
6058255736Sdavidch        /* the transmit frame was enqueued successfully */
6059255736Sdavidch        tx_count++;
6060255736Sdavidch
6061255736Sdavidch        /* send a copy of the frame to any BPF listeners */
6062266979Smarcel	if_etherbpfmtap(ifp, next);
6063255736Sdavidch
6064255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
6065255736Sdavidch
6066255736Sdavidch        /* handle any completions if we're running low */
6067255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
6068258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
6069255736Sdavidch            bxe_txeof(sc, fp);
6070266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
6071258187Sedavis                break;
6072258187Sedavis            }
6073255736Sdavidch        }
6074255736Sdavidch
6075266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
6076255736Sdavidch    }
6077255736Sdavidch
6078255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
6079255736Sdavidch    if (tx_count > 0) {
6080255736Sdavidch        /* reset the TX watchdog timeout timer */
6081255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
6082255736Sdavidch    }
6083255736Sdavidch
6084255736Sdavidchbxe_tx_mq_start_locked_exit:
6085255736Sdavidch
6086255736Sdavidch    return (rc);
6087255736Sdavidch}
6088255736Sdavidch
6089255736Sdavidch/* Multiqueue (TSS) dispatch routine. */
6090255736Sdavidchstatic int
6091255736Sdavidchbxe_tx_mq_start(struct ifnet *ifp,
6092255736Sdavidch                struct mbuf  *m)
6093255736Sdavidch{
6094266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
6095255736Sdavidch    struct bxe_fastpath *fp;
6096255736Sdavidch    int fp_index, rc;
6097255736Sdavidch
6098255736Sdavidch    fp_index = 0; /* default is the first queue */
6099255736Sdavidch
6100275358Shselasky    /* check if flowid is set */
6101275358Shselasky    if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
6102255736Sdavidch        fp_index = (m->m_pkthdr.flowid % sc->num_queues);
6103255736Sdavidch
6104255736Sdavidch    fp = &sc->fp[fp_index];
6105255736Sdavidch
6106266979Smarcel    if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6107255736Sdavidch        BLOGW(sc, "Interface not running, ignoring transmit request\n");
6108255736Sdavidch        return (ENETDOWN);
6109255736Sdavidch    }
6110255736Sdavidch
6111266979Smarcel    if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
6112255736Sdavidch        BLOGW(sc, "Interface TX queue is full, ignoring transmit request\n");
6113255736Sdavidch        return (EBUSY);
6114255736Sdavidch    }
6115255736Sdavidch
6116255736Sdavidch    if (!sc->link_vars.link_up) {
6117255736Sdavidch        BLOGW(sc, "Interface link is down, ignoring transmit request\n");
6118255736Sdavidch        return (ENETDOWN);
6119255736Sdavidch    }
6120255736Sdavidch
6121255736Sdavidch    /* XXX change to TRYLOCK here and if failed then schedule taskqueue */
6122255736Sdavidch
6123255736Sdavidch    BXE_FP_TX_LOCK(fp);
6124255736Sdavidch    rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
6125255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
6126255736Sdavidch
6127255736Sdavidch    return (rc);
6128255736Sdavidch}
6129255736Sdavidch
6130255736Sdavidchstatic void
6131255736Sdavidchbxe_mq_flush(struct ifnet *ifp)
6132255736Sdavidch{
6133266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
6134255736Sdavidch    struct bxe_fastpath *fp;
6135255736Sdavidch    struct mbuf *m;
6136255736Sdavidch    int i;
6137255736Sdavidch
6138255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6139255736Sdavidch        fp = &sc->fp[i];
6140255736Sdavidch
6141255736Sdavidch        if (fp->state != BXE_FP_STATE_OPEN) {
6142255736Sdavidch            BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
6143255736Sdavidch                  fp->index, fp->state);
6144255736Sdavidch            continue;
6145255736Sdavidch        }
6146255736Sdavidch
6147255736Sdavidch        if (fp->tx_br != NULL) {
6148255736Sdavidch            BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
6149255736Sdavidch            BXE_FP_TX_LOCK(fp);
6150255736Sdavidch            while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
6151255736Sdavidch                m_freem(m);
6152255736Sdavidch            }
6153255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
6154255736Sdavidch        }
6155255736Sdavidch    }
6156255736Sdavidch
6157270876Sglebius    if_qflush(ifp);
6158255736Sdavidch}
6159255736Sdavidch
6160255736Sdavidch#endif /* FreeBSD_version >= 800000 */
6161255736Sdavidch
6162255736Sdavidchstatic uint16_t
6163255736Sdavidchbxe_cid_ilt_lines(struct bxe_softc *sc)
6164255736Sdavidch{
6165255736Sdavidch    if (IS_SRIOV(sc)) {
6166255736Sdavidch        return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
6167255736Sdavidch    }
6168255736Sdavidch    return (L2_ILT_LINES(sc));
6169255736Sdavidch}
6170255736Sdavidch
6171255736Sdavidchstatic void
6172255736Sdavidchbxe_ilt_set_info(struct bxe_softc *sc)
6173255736Sdavidch{
6174255736Sdavidch    struct ilt_client_info *ilt_client;
6175255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
6176255736Sdavidch    uint16_t line = 0;
6177255736Sdavidch
6178255736Sdavidch    ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
6179255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
6180255736Sdavidch
6181255736Sdavidch    /* CDU */
6182255736Sdavidch    ilt_client = &ilt->clients[ILT_CLIENT_CDU];
6183255736Sdavidch    ilt_client->client_num = ILT_CLIENT_CDU;
6184255736Sdavidch    ilt_client->page_size = CDU_ILT_PAGE_SZ;
6185255736Sdavidch    ilt_client->flags = ILT_CLIENT_SKIP_MEM;
6186255736Sdavidch    ilt_client->start = line;
6187255736Sdavidch    line += bxe_cid_ilt_lines(sc);
6188255736Sdavidch
6189255736Sdavidch    if (CNIC_SUPPORT(sc)) {
6190255736Sdavidch        line += CNIC_ILT_LINES;
6191255736Sdavidch    }
6192255736Sdavidch
6193255736Sdavidch    ilt_client->end = (line - 1);
6194255736Sdavidch
6195255736Sdavidch    BLOGD(sc, DBG_LOAD,
6196255736Sdavidch          "ilt client[CDU]: start %d, end %d, "
6197255736Sdavidch          "psz 0x%x, flags 0x%x, hw psz %d\n",
6198255736Sdavidch          ilt_client->start, ilt_client->end,
6199255736Sdavidch          ilt_client->page_size,
6200255736Sdavidch          ilt_client->flags,
6201255736Sdavidch          ilog2(ilt_client->page_size >> 12));
6202255736Sdavidch
6203255736Sdavidch    /* QM */
6204255736Sdavidch    if (QM_INIT(sc->qm_cid_count)) {
6205255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_QM];
6206255736Sdavidch        ilt_client->client_num = ILT_CLIENT_QM;
6207255736Sdavidch        ilt_client->page_size = QM_ILT_PAGE_SZ;
6208255736Sdavidch        ilt_client->flags = 0;
6209255736Sdavidch        ilt_client->start = line;
6210255736Sdavidch
6211255736Sdavidch        /* 4 bytes for each cid */
6212255736Sdavidch        line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
6213255736Sdavidch                             QM_ILT_PAGE_SZ);
6214255736Sdavidch
6215255736Sdavidch        ilt_client->end = (line - 1);
6216255736Sdavidch
6217255736Sdavidch        BLOGD(sc, DBG_LOAD,
6218255736Sdavidch              "ilt client[QM]: start %d, end %d, "
6219255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
6220255736Sdavidch              ilt_client->start, ilt_client->end,
6221255736Sdavidch              ilt_client->page_size, ilt_client->flags,
6222255736Sdavidch              ilog2(ilt_client->page_size >> 12));
6223255736Sdavidch    }
6224255736Sdavidch
6225255736Sdavidch    if (CNIC_SUPPORT(sc)) {
6226255736Sdavidch        /* SRC */
6227255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_SRC];
6228255736Sdavidch        ilt_client->client_num = ILT_CLIENT_SRC;
6229255736Sdavidch        ilt_client->page_size = SRC_ILT_PAGE_SZ;
6230255736Sdavidch        ilt_client->flags = 0;
6231255736Sdavidch        ilt_client->start = line;
6232255736Sdavidch        line += SRC_ILT_LINES;
6233255736Sdavidch        ilt_client->end = (line - 1);
6234255736Sdavidch
6235255736Sdavidch        BLOGD(sc, DBG_LOAD,
6236255736Sdavidch              "ilt client[SRC]: start %d, end %d, "
6237255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
6238255736Sdavidch              ilt_client->start, ilt_client->end,
6239255736Sdavidch              ilt_client->page_size, ilt_client->flags,
6240255736Sdavidch              ilog2(ilt_client->page_size >> 12));
6241255736Sdavidch
6242255736Sdavidch        /* TM */
6243255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_TM];
6244255736Sdavidch        ilt_client->client_num = ILT_CLIENT_TM;
6245255736Sdavidch        ilt_client->page_size = TM_ILT_PAGE_SZ;
6246255736Sdavidch        ilt_client->flags = 0;
6247255736Sdavidch        ilt_client->start = line;
6248255736Sdavidch        line += TM_ILT_LINES;
6249255736Sdavidch        ilt_client->end = (line - 1);
6250255736Sdavidch
6251255736Sdavidch        BLOGD(sc, DBG_LOAD,
6252255736Sdavidch              "ilt client[TM]: start %d, end %d, "
6253255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
6254255736Sdavidch              ilt_client->start, ilt_client->end,
6255255736Sdavidch              ilt_client->page_size, ilt_client->flags,
6256255736Sdavidch              ilog2(ilt_client->page_size >> 12));
6257255736Sdavidch    }
6258255736Sdavidch
6259255736Sdavidch    KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
6260255736Sdavidch}
6261255736Sdavidch
6262255736Sdavidchstatic void
6263255736Sdavidchbxe_set_fp_rx_buf_size(struct bxe_softc *sc)
6264255736Sdavidch{
6265255736Sdavidch    int i;
6266283269Sdavidcs    uint32_t rx_buf_size;
6267255736Sdavidch
6268283269Sdavidcs    rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
6269255736Sdavidch
6270255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6271283269Sdavidcs        if(rx_buf_size <= MCLBYTES){
6272283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
6273255736Sdavidch            sc->fp[i].mbuf_alloc_size = MCLBYTES;
6274283269Sdavidcs        }else if (rx_buf_size <= MJUMPAGESIZE){
6275283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
6276283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6277283269Sdavidcs        }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
6278283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
6279283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
6280283269Sdavidcs        }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
6281283269Sdavidcs            sc->fp[i].rx_buf_size = MJUMPAGESIZE;
6282283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6283283269Sdavidcs        }else {
6284283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
6285283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
6286255736Sdavidch        }
6287255736Sdavidch    }
6288255736Sdavidch}
6289255736Sdavidch
6290255736Sdavidchstatic int
6291255736Sdavidchbxe_alloc_ilt_mem(struct bxe_softc *sc)
6292255736Sdavidch{
6293255736Sdavidch    int rc = 0;
6294255736Sdavidch
6295255736Sdavidch    if ((sc->ilt =
6296255736Sdavidch         (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
6297255736Sdavidch                                    M_BXE_ILT,
6298255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
6299255736Sdavidch        rc = 1;
6300255736Sdavidch    }
6301255736Sdavidch
6302255736Sdavidch    return (rc);
6303255736Sdavidch}
6304255736Sdavidch
6305255736Sdavidchstatic int
6306255736Sdavidchbxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
6307255736Sdavidch{
6308255736Sdavidch    int rc = 0;
6309255736Sdavidch
6310255736Sdavidch    if ((sc->ilt->lines =
6311255736Sdavidch         (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
6312255736Sdavidch                                    M_BXE_ILT,
6313255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
6314255736Sdavidch        rc = 1;
6315255736Sdavidch    }
6316255736Sdavidch
6317255736Sdavidch    return (rc);
6318255736Sdavidch}
6319255736Sdavidch
6320255736Sdavidchstatic void
6321255736Sdavidchbxe_free_ilt_mem(struct bxe_softc *sc)
6322255736Sdavidch{
6323255736Sdavidch    if (sc->ilt != NULL) {
6324255736Sdavidch        free(sc->ilt, M_BXE_ILT);
6325255736Sdavidch        sc->ilt = NULL;
6326255736Sdavidch    }
6327255736Sdavidch}
6328255736Sdavidch
6329255736Sdavidchstatic void
6330255736Sdavidchbxe_free_ilt_lines_mem(struct bxe_softc *sc)
6331255736Sdavidch{
6332255736Sdavidch    if (sc->ilt->lines != NULL) {
6333255736Sdavidch        free(sc->ilt->lines, M_BXE_ILT);
6334255736Sdavidch        sc->ilt->lines = NULL;
6335255736Sdavidch    }
6336255736Sdavidch}
6337255736Sdavidch
6338255736Sdavidchstatic void
6339255736Sdavidchbxe_free_mem(struct bxe_softc *sc)
6340255736Sdavidch{
6341255736Sdavidch    int i;
6342255736Sdavidch
6343255736Sdavidch#if 0
6344255736Sdavidch    if (!CONFIGURE_NIC_MODE(sc)) {
6345255736Sdavidch        /* free searcher T2 table */
6346255736Sdavidch        bxe_dma_free(sc, &sc->t2);
6347255736Sdavidch    }
6348255736Sdavidch#endif
6349255736Sdavidch
6350255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
6351255736Sdavidch        bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6352255736Sdavidch        sc->context[i].vcxt = NULL;
6353255736Sdavidch        sc->context[i].size = 0;
6354255736Sdavidch    }
6355255736Sdavidch
6356255736Sdavidch    ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6357255736Sdavidch
6358255736Sdavidch    bxe_free_ilt_lines_mem(sc);
6359255736Sdavidch
6360255736Sdavidch#if 0
6361255736Sdavidch    bxe_iov_free_mem(sc);
6362255736Sdavidch#endif
6363255736Sdavidch}
6364255736Sdavidch
6365255736Sdavidchstatic int
6366255736Sdavidchbxe_alloc_mem(struct bxe_softc *sc)
6367255736Sdavidch{
6368255736Sdavidch    int context_size;
6369255736Sdavidch    int allocated;
6370255736Sdavidch    int i;
6371255736Sdavidch
6372255736Sdavidch#if 0
6373255736Sdavidch    if (!CONFIGURE_NIC_MODE(sc)) {
6374255736Sdavidch        /* allocate searcher T2 table */
6375255736Sdavidch        if (bxe_dma_alloc(sc, SRC_T2_SZ,
6376255736Sdavidch                          &sc->t2, "searcher t2 table") != 0) {
6377255736Sdavidch            return (-1);
6378255736Sdavidch        }
6379255736Sdavidch    }
6380255736Sdavidch#endif
6381255736Sdavidch
6382255736Sdavidch    /*
6383255736Sdavidch     * Allocate memory for CDU context:
6384255736Sdavidch     * This memory is allocated separately and not in the generic ILT
6385255736Sdavidch     * functions because CDU differs in few aspects:
6386255736Sdavidch     * 1. There can be multiple entities allocating memory for context -
6387255736Sdavidch     * regular L2, CNIC, and SRIOV drivers. Each separately controls
6388255736Sdavidch     * its own ILT lines.
6389255736Sdavidch     * 2. Since CDU page-size is not a single 4KB page (which is the case
6390255736Sdavidch     * for the other ILT clients), to be efficient we want to support
6391255736Sdavidch     * allocation of sub-page-size in the last entry.
6392255736Sdavidch     * 3. Context pointers are used by the driver to pass to FW / update
6393255736Sdavidch     * the context (for the other ILT clients the pointers are used just to
6394255736Sdavidch     * free the memory during unload).
6395255736Sdavidch     */
6396255736Sdavidch    context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6397255736Sdavidch    for (i = 0, allocated = 0; allocated < context_size; i++) {
6398255736Sdavidch        sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6399255736Sdavidch                                  (context_size - allocated));
6400255736Sdavidch
6401255736Sdavidch        if (bxe_dma_alloc(sc, sc->context[i].size,
6402255736Sdavidch                          &sc->context[i].vcxt_dma,
6403255736Sdavidch                          "cdu context") != 0) {
6404255736Sdavidch            bxe_free_mem(sc);
6405255736Sdavidch            return (-1);
6406255736Sdavidch        }
6407255736Sdavidch
6408255736Sdavidch        sc->context[i].vcxt =
6409255736Sdavidch            (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6410255736Sdavidch
6411255736Sdavidch        allocated += sc->context[i].size;
6412255736Sdavidch    }
6413255736Sdavidch
6414255736Sdavidch    bxe_alloc_ilt_lines_mem(sc);
6415255736Sdavidch
6416255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6417255736Sdavidch          sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6418255736Sdavidch    {
6419255736Sdavidch        for (i = 0; i < 4; i++) {
6420255736Sdavidch            BLOGD(sc, DBG_LOAD,
6421255736Sdavidch                  "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6422255736Sdavidch                  i,
6423255736Sdavidch                  sc->ilt->clients[i].page_size,
6424255736Sdavidch                  sc->ilt->clients[i].start,
6425255736Sdavidch                  sc->ilt->clients[i].end,
6426255736Sdavidch                  sc->ilt->clients[i].client_num,
6427255736Sdavidch                  sc->ilt->clients[i].flags);
6428255736Sdavidch        }
6429255736Sdavidch    }
6430255736Sdavidch    if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6431255736Sdavidch        BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6432255736Sdavidch        bxe_free_mem(sc);
6433255736Sdavidch        return (-1);
6434255736Sdavidch    }
6435255736Sdavidch
6436255736Sdavidch#if 0
6437255736Sdavidch    if (bxe_iov_alloc_mem(sc)) {
6438255736Sdavidch        BLOGE(sc, "Failed to allocate memory for SRIOV\n");
6439255736Sdavidch        bxe_free_mem(sc);
6440255736Sdavidch        return (-1);
6441255736Sdavidch    }
6442255736Sdavidch#endif
6443255736Sdavidch
6444255736Sdavidch    return (0);
6445255736Sdavidch}
6446255736Sdavidch
6447255736Sdavidchstatic void
6448255736Sdavidchbxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6449255736Sdavidch{
6450255736Sdavidch    struct bxe_softc *sc;
6451255736Sdavidch    int i;
6452255736Sdavidch
6453255736Sdavidch    sc = fp->sc;
6454255736Sdavidch
6455255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6456255736Sdavidch        return;
6457255736Sdavidch    }
6458255736Sdavidch
6459255736Sdavidch    /* free all mbufs and unload all maps */
6460255736Sdavidch    for (i = 0; i < RX_BD_TOTAL; i++) {
6461255736Sdavidch        if (fp->rx_mbuf_chain[i].m_map != NULL) {
6462255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6463255736Sdavidch                            fp->rx_mbuf_chain[i].m_map,
6464255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6465255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6466255736Sdavidch                              fp->rx_mbuf_chain[i].m_map);
6467255736Sdavidch        }
6468255736Sdavidch
6469255736Sdavidch        if (fp->rx_mbuf_chain[i].m != NULL) {
6470255736Sdavidch            m_freem(fp->rx_mbuf_chain[i].m);
6471255736Sdavidch            fp->rx_mbuf_chain[i].m = NULL;
6472255736Sdavidch            fp->eth_q_stats.mbuf_alloc_rx--;
6473255736Sdavidch        }
6474255736Sdavidch    }
6475255736Sdavidch}
6476255736Sdavidch
6477255736Sdavidchstatic void
6478255736Sdavidchbxe_free_tpa_pool(struct bxe_fastpath *fp)
6479255736Sdavidch{
6480255736Sdavidch    struct bxe_softc *sc;
6481255736Sdavidch    int i, max_agg_queues;
6482255736Sdavidch
6483255736Sdavidch    sc = fp->sc;
6484255736Sdavidch
6485255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6486255736Sdavidch        return;
6487255736Sdavidch    }
6488255736Sdavidch
6489255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
6490255736Sdavidch
6491255736Sdavidch    /* release all mbufs and unload all DMA maps in the TPA pool */
6492255736Sdavidch    for (i = 0; i < max_agg_queues; i++) {
6493255736Sdavidch        if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6494255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6495255736Sdavidch                            fp->rx_tpa_info[i].bd.m_map,
6496255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6497255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6498255736Sdavidch                              fp->rx_tpa_info[i].bd.m_map);
6499255736Sdavidch        }
6500255736Sdavidch
6501255736Sdavidch        if (fp->rx_tpa_info[i].bd.m != NULL) {
6502255736Sdavidch            m_freem(fp->rx_tpa_info[i].bd.m);
6503255736Sdavidch            fp->rx_tpa_info[i].bd.m = NULL;
6504255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tpa--;
6505255736Sdavidch        }
6506255736Sdavidch    }
6507255736Sdavidch}
6508255736Sdavidch
6509255736Sdavidchstatic void
6510255736Sdavidchbxe_free_sge_chain(struct bxe_fastpath *fp)
6511255736Sdavidch{
6512255736Sdavidch    struct bxe_softc *sc;
6513255736Sdavidch    int i;
6514255736Sdavidch
6515255736Sdavidch    sc = fp->sc;
6516255736Sdavidch
6517255736Sdavidch    if (fp->rx_sge_mbuf_tag == NULL) {
6518255736Sdavidch        return;
6519255736Sdavidch    }
6520255736Sdavidch
6521255736Sdavidch    /* rree all mbufs and unload all maps */
6522255736Sdavidch    for (i = 0; i < RX_SGE_TOTAL; i++) {
6523255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6524255736Sdavidch            bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6525255736Sdavidch                            fp->rx_sge_mbuf_chain[i].m_map,
6526255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6527255736Sdavidch            bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6528255736Sdavidch                              fp->rx_sge_mbuf_chain[i].m_map);
6529255736Sdavidch        }
6530255736Sdavidch
6531255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6532255736Sdavidch            m_freem(fp->rx_sge_mbuf_chain[i].m);
6533255736Sdavidch            fp->rx_sge_mbuf_chain[i].m = NULL;
6534255736Sdavidch            fp->eth_q_stats.mbuf_alloc_sge--;
6535255736Sdavidch        }
6536255736Sdavidch    }
6537255736Sdavidch}
6538255736Sdavidch
6539255736Sdavidchstatic void
6540255736Sdavidchbxe_free_fp_buffers(struct bxe_softc *sc)
6541255736Sdavidch{
6542255736Sdavidch    struct bxe_fastpath *fp;
6543255736Sdavidch    int i;
6544255736Sdavidch
6545255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6546255736Sdavidch        fp = &sc->fp[i];
6547255736Sdavidch
6548255736Sdavidch#if __FreeBSD_version >= 800000
6549255736Sdavidch        if (fp->tx_br != NULL) {
6550255736Sdavidch            struct mbuf *m;
6551255736Sdavidch            /* just in case bxe_mq_flush() wasn't called */
6552255736Sdavidch            while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
6553255736Sdavidch                m_freem(m);
6554255736Sdavidch            }
6555255736Sdavidch            buf_ring_free(fp->tx_br, M_DEVBUF);
6556255736Sdavidch            fp->tx_br = NULL;
6557255736Sdavidch        }
6558255736Sdavidch#endif
6559255736Sdavidch
6560255736Sdavidch        /* free all RX buffers */
6561255736Sdavidch        bxe_free_rx_bd_chain(fp);
6562255736Sdavidch        bxe_free_tpa_pool(fp);
6563255736Sdavidch        bxe_free_sge_chain(fp);
6564255736Sdavidch
6565255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6566255736Sdavidch            BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6567255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_rx);
6568255736Sdavidch        }
6569255736Sdavidch
6570255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6571255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6572255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_sge);
6573255736Sdavidch        }
6574255736Sdavidch
6575255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6576255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6577255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tpa);
6578255736Sdavidch        }
6579255736Sdavidch
6580255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6581255736Sdavidch            BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6582255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tx);
6583255736Sdavidch        }
6584255736Sdavidch
6585255736Sdavidch        /* XXX verify all mbufs were reclaimed */
6586255736Sdavidch
6587255736Sdavidch        if (mtx_initialized(&fp->tx_mtx)) {
6588255736Sdavidch            mtx_destroy(&fp->tx_mtx);
6589255736Sdavidch        }
6590255736Sdavidch
6591255736Sdavidch        if (mtx_initialized(&fp->rx_mtx)) {
6592255736Sdavidch            mtx_destroy(&fp->rx_mtx);
6593255736Sdavidch        }
6594255736Sdavidch    }
6595255736Sdavidch}
6596255736Sdavidch
6597255736Sdavidchstatic int
6598255736Sdavidchbxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6599255736Sdavidch                     uint16_t            prev_index,
6600255736Sdavidch                     uint16_t            index)
6601255736Sdavidch{
6602255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
6603255736Sdavidch    struct eth_rx_bd *rx_bd;
6604255736Sdavidch    bus_dma_segment_t segs[1];
6605255736Sdavidch    bus_dmamap_t map;
6606255736Sdavidch    struct mbuf *m;
6607255736Sdavidch    int nsegs, rc;
6608255736Sdavidch
6609255736Sdavidch    rc = 0;
6610255736Sdavidch
6611255736Sdavidch    /* allocate the new RX BD mbuf */
6612260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6613255736Sdavidch    if (__predict_false(m == NULL)) {
6614255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6615255736Sdavidch        return (ENOBUFS);
6616255736Sdavidch    }
6617255736Sdavidch
6618255736Sdavidch    fp->eth_q_stats.mbuf_alloc_rx++;
6619255736Sdavidch
6620255736Sdavidch    /* initialize the mbuf buffer length */
6621255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6622255736Sdavidch
6623255736Sdavidch    /* map the mbuf into non-paged pool */
6624255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6625255736Sdavidch                                 fp->rx_mbuf_spare_map,
6626255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6627255736Sdavidch    if (__predict_false(rc != 0)) {
6628255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6629255736Sdavidch        m_freem(m);
6630255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
6631255736Sdavidch        return (rc);
6632255736Sdavidch    }
6633255736Sdavidch
6634255736Sdavidch    /* all mbufs must map to a single segment */
6635255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6636255736Sdavidch
6637255736Sdavidch    /* release any existing RX BD mbuf mappings */
6638255736Sdavidch
6639255736Sdavidch    if (prev_index != index) {
6640255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[prev_index];
6641255736Sdavidch
6642255736Sdavidch        if (rx_buf->m_map != NULL) {
6643255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6644255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6645255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6646255736Sdavidch        }
6647255736Sdavidch
6648255736Sdavidch        /*
6649255736Sdavidch         * We only get here from bxe_rxeof() when the maximum number
6650255736Sdavidch         * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6651255736Sdavidch         * holds the mbuf in the prev_index so it's OK to NULL it out
6652255736Sdavidch         * here without concern of a memory leak.
6653255736Sdavidch         */
6654255736Sdavidch        fp->rx_mbuf_chain[prev_index].m = NULL;
6655255736Sdavidch    }
6656255736Sdavidch
6657255736Sdavidch    rx_buf = &fp->rx_mbuf_chain[index];
6658255736Sdavidch
6659255736Sdavidch    if (rx_buf->m_map != NULL) {
6660255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6661255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6662255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6663255736Sdavidch    }
6664255736Sdavidch
6665255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6666255736Sdavidch    map = (prev_index != index) ?
6667255736Sdavidch              fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6668255736Sdavidch    rx_buf->m_map = fp->rx_mbuf_spare_map;
6669255736Sdavidch    fp->rx_mbuf_spare_map = map;
6670255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6671255736Sdavidch                    BUS_DMASYNC_PREREAD);
6672255736Sdavidch    rx_buf->m = m;
6673255736Sdavidch
6674255736Sdavidch    rx_bd = &fp->rx_chain[index];
6675255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6676255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6677255736Sdavidch
6678255736Sdavidch    return (rc);
6679255736Sdavidch}
6680255736Sdavidch
6681255736Sdavidchstatic int
6682255736Sdavidchbxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6683255736Sdavidch                      int                 queue)
6684255736Sdavidch{
6685255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6686255736Sdavidch    bus_dma_segment_t segs[1];
6687255736Sdavidch    bus_dmamap_t map;
6688255736Sdavidch    struct mbuf *m;
6689255736Sdavidch    int nsegs;
6690255736Sdavidch    int rc = 0;
6691255736Sdavidch
6692255736Sdavidch    /* allocate the new TPA mbuf */
6693260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6694255736Sdavidch    if (__predict_false(m == NULL)) {
6695255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6696255736Sdavidch        return (ENOBUFS);
6697255736Sdavidch    }
6698255736Sdavidch
6699255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa++;
6700255736Sdavidch
6701255736Sdavidch    /* initialize the mbuf buffer length */
6702255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6703255736Sdavidch
6704255736Sdavidch    /* map the mbuf into non-paged pool */
6705255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6706255736Sdavidch                                 fp->rx_tpa_info_mbuf_spare_map,
6707255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6708255736Sdavidch    if (__predict_false(rc != 0)) {
6709255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6710255736Sdavidch        m_free(m);
6711255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tpa--;
6712255736Sdavidch        return (rc);
6713255736Sdavidch    }
6714255736Sdavidch
6715255736Sdavidch    /* all mbufs must map to a single segment */
6716255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6717255736Sdavidch
6718255736Sdavidch    /* release any existing TPA mbuf mapping */
6719255736Sdavidch    if (tpa_info->bd.m_map != NULL) {
6720255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6721255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6722255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6723255736Sdavidch    }
6724255736Sdavidch
6725255736Sdavidch    /* save the mbuf and mapping info for the TPA mbuf */
6726255736Sdavidch    map = tpa_info->bd.m_map;
6727255736Sdavidch    tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6728255736Sdavidch    fp->rx_tpa_info_mbuf_spare_map = map;
6729255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6730255736Sdavidch                    BUS_DMASYNC_PREREAD);
6731255736Sdavidch    tpa_info->bd.m = m;
6732255736Sdavidch    tpa_info->seg = segs[0];
6733255736Sdavidch
6734255736Sdavidch    return (rc);
6735255736Sdavidch}
6736255736Sdavidch
6737255736Sdavidch/*
6738255736Sdavidch * Allocate an mbuf and assign it to the receive scatter gather chain. The
6739255736Sdavidch * caller must take care to save a copy of the existing mbuf in the SG mbuf
6740255736Sdavidch * chain.
6741255736Sdavidch */
6742255736Sdavidchstatic int
6743255736Sdavidchbxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6744255736Sdavidch                      uint16_t            index)
6745255736Sdavidch{
6746255736Sdavidch    struct bxe_sw_rx_bd *sge_buf;
6747255736Sdavidch    struct eth_rx_sge *sge;
6748255736Sdavidch    bus_dma_segment_t segs[1];
6749255736Sdavidch    bus_dmamap_t map;
6750255736Sdavidch    struct mbuf *m;
6751255736Sdavidch    int nsegs;
6752255736Sdavidch    int rc = 0;
6753255736Sdavidch
6754255736Sdavidch    /* allocate a new SGE mbuf */
6755260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6756255736Sdavidch    if (__predict_false(m == NULL)) {
6757255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6758255736Sdavidch        return (ENOMEM);
6759255736Sdavidch    }
6760255736Sdavidch
6761255736Sdavidch    fp->eth_q_stats.mbuf_alloc_sge++;
6762255736Sdavidch
6763255736Sdavidch    /* initialize the mbuf buffer length */
6764255736Sdavidch    m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6765255736Sdavidch
6766255736Sdavidch    /* map the SGE mbuf into non-paged pool */
6767255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6768255736Sdavidch                                 fp->rx_sge_mbuf_spare_map,
6769255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6770255736Sdavidch    if (__predict_false(rc != 0)) {
6771255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6772255736Sdavidch        m_freem(m);
6773255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
6774255736Sdavidch        return (rc);
6775255736Sdavidch    }
6776255736Sdavidch
6777255736Sdavidch    /* all mbufs must map to a single segment */
6778255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6779255736Sdavidch
6780255736Sdavidch    sge_buf = &fp->rx_sge_mbuf_chain[index];
6781255736Sdavidch
6782255736Sdavidch    /* release any existing SGE mbuf mapping */
6783255736Sdavidch    if (sge_buf->m_map != NULL) {
6784255736Sdavidch        bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6785255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6786255736Sdavidch        bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6787255736Sdavidch    }
6788255736Sdavidch
6789255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6790255736Sdavidch    map = sge_buf->m_map;
6791255736Sdavidch    sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6792255736Sdavidch    fp->rx_sge_mbuf_spare_map = map;
6793255736Sdavidch    bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6794255736Sdavidch                    BUS_DMASYNC_PREREAD);
6795255736Sdavidch    sge_buf->m = m;
6796255736Sdavidch
6797255736Sdavidch    sge = &fp->rx_sge_chain[index];
6798255736Sdavidch    sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6799255736Sdavidch    sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6800255736Sdavidch
6801255736Sdavidch    return (rc);
6802255736Sdavidch}
6803255736Sdavidch
6804255736Sdavidchstatic __noinline int
6805255736Sdavidchbxe_alloc_fp_buffers(struct bxe_softc *sc)
6806255736Sdavidch{
6807255736Sdavidch    struct bxe_fastpath *fp;
6808255736Sdavidch    int i, j, rc = 0;
6809255736Sdavidch    int ring_prod, cqe_ring_prod;
6810255736Sdavidch    int max_agg_queues;
6811255736Sdavidch
6812255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6813255736Sdavidch        fp = &sc->fp[i];
6814255736Sdavidch
6815255736Sdavidch#if __FreeBSD_version >= 800000
6816255736Sdavidch        fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
6817260718Sglebius                                   M_NOWAIT, &fp->tx_mtx);
6818255736Sdavidch        if (fp->tx_br == NULL) {
6819255736Sdavidch            BLOGE(sc, "buf_ring alloc fail for fp[%02d]\n", i);
6820255736Sdavidch            goto bxe_alloc_fp_buffers_error;
6821255736Sdavidch        }
6822255736Sdavidch#endif
6823255736Sdavidch
6824255736Sdavidch        ring_prod = cqe_ring_prod = 0;
6825255736Sdavidch        fp->rx_bd_cons = 0;
6826255736Sdavidch        fp->rx_cq_cons = 0;
6827255736Sdavidch
6828255736Sdavidch        /* allocate buffers for the RX BDs in RX BD chain */
6829255736Sdavidch        for (j = 0; j < sc->max_rx_bufs; j++) {
6830255736Sdavidch            rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6831255736Sdavidch            if (rc != 0) {
6832255736Sdavidch                BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6833255736Sdavidch                      i, rc);
6834255736Sdavidch                goto bxe_alloc_fp_buffers_error;
6835255736Sdavidch            }
6836255736Sdavidch
6837255736Sdavidch            ring_prod     = RX_BD_NEXT(ring_prod);
6838255736Sdavidch            cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6839255736Sdavidch        }
6840255736Sdavidch
6841255736Sdavidch        fp->rx_bd_prod = ring_prod;
6842255736Sdavidch        fp->rx_cq_prod = cqe_ring_prod;
6843255736Sdavidch        fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6844255736Sdavidch
6845283274Sdavidcs        max_agg_queues = MAX_AGG_QS(sc);
6846255736Sdavidch
6847283274Sdavidcs        fp->tpa_enable = TRUE;
6848255736Sdavidch
6849283274Sdavidcs        /* fill the TPA pool */
6850283274Sdavidcs        for (j = 0; j < max_agg_queues; j++) {
6851283274Sdavidcs            rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6852283274Sdavidcs            if (rc != 0) {
6853283274Sdavidcs                BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6854283274Sdavidcs                          i, j);
6855283274Sdavidcs                fp->tpa_enable = FALSE;
6856283274Sdavidcs                goto bxe_alloc_fp_buffers_error;
6857283274Sdavidcs            }
6858283274Sdavidcs
6859283274Sdavidcs            fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6860283274Sdavidcs        }
6861283274Sdavidcs
6862283274Sdavidcs        if (fp->tpa_enable) {
6863283274Sdavidcs            /* fill the RX SGE chain */
6864283274Sdavidcs            ring_prod = 0;
6865283274Sdavidcs            for (j = 0; j < RX_SGE_USABLE; j++) {
6866283274Sdavidcs                rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6867255736Sdavidch                if (rc != 0) {
6868283274Sdavidcs                    BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6869283274Sdavidcs                              i, ring_prod);
6870255736Sdavidch                    fp->tpa_enable = FALSE;
6871283274Sdavidcs                    ring_prod = 0;
6872255736Sdavidch                    goto bxe_alloc_fp_buffers_error;
6873255736Sdavidch                }
6874255736Sdavidch
6875283274Sdavidcs                ring_prod = RX_SGE_NEXT(ring_prod);
6876255736Sdavidch            }
6877255736Sdavidch
6878283274Sdavidcs            fp->rx_sge_prod = ring_prod;
6879255736Sdavidch        }
6880255736Sdavidch    }
6881255736Sdavidch
6882255736Sdavidch    return (0);
6883255736Sdavidch
6884255736Sdavidchbxe_alloc_fp_buffers_error:
6885255736Sdavidch
6886255736Sdavidch    /* unwind what was already allocated */
6887255736Sdavidch    bxe_free_rx_bd_chain(fp);
6888255736Sdavidch    bxe_free_tpa_pool(fp);
6889255736Sdavidch    bxe_free_sge_chain(fp);
6890255736Sdavidch
6891255736Sdavidch    return (ENOBUFS);
6892255736Sdavidch}
6893255736Sdavidch
6894255736Sdavidchstatic void
6895255736Sdavidchbxe_free_fw_stats_mem(struct bxe_softc *sc)
6896255736Sdavidch{
6897255736Sdavidch    bxe_dma_free(sc, &sc->fw_stats_dma);
6898255736Sdavidch
6899255736Sdavidch    sc->fw_stats_num = 0;
6900255736Sdavidch
6901255736Sdavidch    sc->fw_stats_req_size = 0;
6902255736Sdavidch    sc->fw_stats_req = NULL;
6903255736Sdavidch    sc->fw_stats_req_mapping = 0;
6904255736Sdavidch
6905255736Sdavidch    sc->fw_stats_data_size = 0;
6906255736Sdavidch    sc->fw_stats_data = NULL;
6907255736Sdavidch    sc->fw_stats_data_mapping = 0;
6908255736Sdavidch}
6909255736Sdavidch
6910255736Sdavidchstatic int
6911255736Sdavidchbxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6912255736Sdavidch{
6913255736Sdavidch    uint8_t num_queue_stats;
6914255736Sdavidch    int num_groups;
6915255736Sdavidch
6916255736Sdavidch    /* number of queues for statistics is number of eth queues */
6917255736Sdavidch    num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6918255736Sdavidch
6919255736Sdavidch    /*
6920255736Sdavidch     * Total number of FW statistics requests =
6921255736Sdavidch     *   1 for port stats + 1 for PF stats + num of queues
6922255736Sdavidch     */
6923255736Sdavidch    sc->fw_stats_num = (2 + num_queue_stats);
6924255736Sdavidch
6925255736Sdavidch    /*
6926255736Sdavidch     * Request is built from stats_query_header and an array of
6927255736Sdavidch     * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6928255736Sdavidch     * rules. The real number or requests is configured in the
6929255736Sdavidch     * stats_query_header.
6930255736Sdavidch     */
6931255736Sdavidch    num_groups =
6932255736Sdavidch        ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6933255736Sdavidch         ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6934255736Sdavidch
6935255736Sdavidch    BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6936255736Sdavidch          sc->fw_stats_num, num_groups);
6937255736Sdavidch
6938255736Sdavidch    sc->fw_stats_req_size =
6939255736Sdavidch        (sizeof(struct stats_query_header) +
6940255736Sdavidch         (num_groups * sizeof(struct stats_query_cmd_group)));
6941255736Sdavidch
6942255736Sdavidch    /*
6943255736Sdavidch     * Data for statistics requests + stats_counter.
6944255736Sdavidch     * stats_counter holds per-STORM counters that are incremented when
6945255736Sdavidch     * STORM has finished with the current request. Memory for FCoE
6946255736Sdavidch     * offloaded statistics are counted anyway, even if they will not be sent.
6947255736Sdavidch     * VF stats are not accounted for here as the data of VF stats is stored
6948255736Sdavidch     * in memory allocated by the VF, not here.
6949255736Sdavidch     */
6950255736Sdavidch    sc->fw_stats_data_size =
6951255736Sdavidch        (sizeof(struct stats_counter) +
6952255736Sdavidch         sizeof(struct per_port_stats) +
6953255736Sdavidch         sizeof(struct per_pf_stats) +
6954255736Sdavidch         /* sizeof(struct fcoe_statistics_params) + */
6955255736Sdavidch         (sizeof(struct per_queue_stats) * num_queue_stats));
6956255736Sdavidch
6957255736Sdavidch    if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6958255736Sdavidch                      &sc->fw_stats_dma, "fw stats") != 0) {
6959255736Sdavidch        bxe_free_fw_stats_mem(sc);
6960255736Sdavidch        return (-1);
6961255736Sdavidch    }
6962255736Sdavidch
6963255736Sdavidch    /* set up the shortcuts */
6964255736Sdavidch
6965255736Sdavidch    sc->fw_stats_req =
6966255736Sdavidch        (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6967255736Sdavidch    sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6968255736Sdavidch
6969255736Sdavidch    sc->fw_stats_data =
6970255736Sdavidch        (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6971255736Sdavidch                                     sc->fw_stats_req_size);
6972255736Sdavidch    sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6973255736Sdavidch                                 sc->fw_stats_req_size);
6974255736Sdavidch
6975256341Sdim    BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6976256341Sdim          (uintmax_t)sc->fw_stats_req_mapping);
6977255736Sdavidch
6978256341Sdim    BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6979256341Sdim          (uintmax_t)sc->fw_stats_data_mapping);
6980255736Sdavidch
6981255736Sdavidch    return (0);
6982255736Sdavidch}
6983255736Sdavidch
6984255736Sdavidch/*
6985255736Sdavidch * Bits map:
6986255736Sdavidch * 0-7  - Engine0 load counter.
6987255736Sdavidch * 8-15 - Engine1 load counter.
6988255736Sdavidch * 16   - Engine0 RESET_IN_PROGRESS bit.
6989255736Sdavidch * 17   - Engine1 RESET_IN_PROGRESS bit.
6990255736Sdavidch * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6991255736Sdavidch *        function on the engine
6992255736Sdavidch * 19   - Engine1 ONE_IS_LOADED.
6993255736Sdavidch * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6994255736Sdavidch *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6995255736Sdavidch *        for just the one belonging to its engine).
6996255736Sdavidch */
6997255736Sdavidch#define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6998255736Sdavidch#define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6999255736Sdavidch#define BXE_PATH0_LOAD_CNT_SHIFT  0
7000255736Sdavidch#define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
7001255736Sdavidch#define BXE_PATH1_LOAD_CNT_SHIFT  8
7002255736Sdavidch#define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
7003255736Sdavidch#define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
7004255736Sdavidch#define BXE_GLOBAL_RESET_BIT      0x00040000
7005255736Sdavidch
7006255736Sdavidch/* set the GLOBAL_RESET bit, should be run under rtnl lock */
7007255736Sdavidchstatic void
7008255736Sdavidchbxe_set_reset_global(struct bxe_softc *sc)
7009255736Sdavidch{
7010255736Sdavidch    uint32_t val;
7011255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7012255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7013255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
7014255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7015255736Sdavidch}
7016255736Sdavidch
7017255736Sdavidch/* clear the GLOBAL_RESET bit, should be run under rtnl lock */
7018255736Sdavidchstatic void
7019255736Sdavidchbxe_clear_reset_global(struct bxe_softc *sc)
7020255736Sdavidch{
7021255736Sdavidch    uint32_t val;
7022255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7023255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7024255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
7025255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7026255736Sdavidch}
7027255736Sdavidch
7028255736Sdavidch/* checks the GLOBAL_RESET bit, should be run under rtnl lock */
7029255736Sdavidchstatic uint8_t
7030255736Sdavidchbxe_reset_is_global(struct bxe_softc *sc)
7031255736Sdavidch{
7032255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7033255736Sdavidch    BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
7034255736Sdavidch    return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
7035255736Sdavidch}
7036255736Sdavidch
7037255736Sdavidch/* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
7038255736Sdavidchstatic void
7039255736Sdavidchbxe_set_reset_done(struct bxe_softc *sc)
7040255736Sdavidch{
7041255736Sdavidch    uint32_t val;
7042255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
7043255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
7044255736Sdavidch
7045255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7046255736Sdavidch
7047255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7048255736Sdavidch    /* Clear the bit */
7049255736Sdavidch    val &= ~bit;
7050255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7051255736Sdavidch
7052255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7053255736Sdavidch}
7054255736Sdavidch
7055255736Sdavidch/* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
7056255736Sdavidchstatic void
7057255736Sdavidchbxe_set_reset_in_progress(struct bxe_softc *sc)
7058255736Sdavidch{
7059255736Sdavidch    uint32_t val;
7060255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
7061255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
7062255736Sdavidch
7063255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7064255736Sdavidch
7065255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7066255736Sdavidch    /* Set the bit */
7067255736Sdavidch    val |= bit;
7068255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7069255736Sdavidch
7070255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7071255736Sdavidch}
7072255736Sdavidch
7073255736Sdavidch/* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
7074255736Sdavidchstatic uint8_t
7075255736Sdavidchbxe_reset_is_done(struct bxe_softc *sc,
7076255736Sdavidch                  int              engine)
7077255736Sdavidch{
7078255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7079255736Sdavidch    uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
7080255736Sdavidch                            BXE_PATH0_RST_IN_PROG_BIT;
7081255736Sdavidch
7082255736Sdavidch    /* return false if bit is set */
7083255736Sdavidch    return (val & bit) ? FALSE : TRUE;
7084255736Sdavidch}
7085255736Sdavidch
7086255736Sdavidch/* get the load status for an engine, should be run under rtnl lock */
7087255736Sdavidchstatic uint8_t
7088255736Sdavidchbxe_get_load_status(struct bxe_softc *sc,
7089255736Sdavidch                    int              engine)
7090255736Sdavidch{
7091255736Sdavidch    uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
7092255736Sdavidch                             BXE_PATH0_LOAD_CNT_MASK;
7093255736Sdavidch    uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
7094255736Sdavidch                              BXE_PATH0_LOAD_CNT_SHIFT;
7095255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7096255736Sdavidch
7097255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
7098255736Sdavidch
7099255736Sdavidch    val = ((val & mask) >> shift);
7100255736Sdavidch
7101255736Sdavidch    BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
7102255736Sdavidch
7103255736Sdavidch    return (val != 0);
7104255736Sdavidch}
7105255736Sdavidch
7106255736Sdavidch/* set pf load mark */
7107255736Sdavidch/* XXX needs to be under rtnl lock */
7108255736Sdavidchstatic void
7109255736Sdavidchbxe_set_pf_load(struct bxe_softc *sc)
7110255736Sdavidch{
7111255736Sdavidch    uint32_t val;
7112255736Sdavidch    uint32_t val1;
7113255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
7114255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
7115255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
7116255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
7117255736Sdavidch
7118255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7119255736Sdavidch
7120255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7121255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
7122255736Sdavidch
7123255736Sdavidch    /* get the current counter value */
7124255736Sdavidch    val1 = ((val & mask) >> shift);
7125255736Sdavidch
7126255736Sdavidch    /* set bit of this PF */
7127255736Sdavidch    val1 |= (1 << SC_ABS_FUNC(sc));
7128255736Sdavidch
7129255736Sdavidch    /* clear the old value */
7130255736Sdavidch    val &= ~mask;
7131255736Sdavidch
7132255736Sdavidch    /* set the new one */
7133255736Sdavidch    val |= ((val1 << shift) & mask);
7134255736Sdavidch
7135255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7136255736Sdavidch
7137255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7138255736Sdavidch}
7139255736Sdavidch
7140255736Sdavidch/* clear pf load mark */
7141255736Sdavidch/* XXX needs to be under rtnl lock */
7142255736Sdavidchstatic uint8_t
7143255736Sdavidchbxe_clear_pf_load(struct bxe_softc *sc)
7144255736Sdavidch{
7145255736Sdavidch    uint32_t val1, val;
7146255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
7147255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
7148255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
7149255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
7150255736Sdavidch
7151255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7152255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7153255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
7154255736Sdavidch
7155255736Sdavidch    /* get the current counter value */
7156255736Sdavidch    val1 = (val & mask) >> shift;
7157255736Sdavidch
7158255736Sdavidch    /* clear bit of that PF */
7159255736Sdavidch    val1 &= ~(1 << SC_ABS_FUNC(sc));
7160255736Sdavidch
7161255736Sdavidch    /* clear the old value */
7162255736Sdavidch    val &= ~mask;
7163255736Sdavidch
7164255736Sdavidch    /* set the new one */
7165255736Sdavidch    val |= ((val1 << shift) & mask);
7166255736Sdavidch
7167255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7168255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7169255736Sdavidch    return (val1 != 0);
7170255736Sdavidch}
7171255736Sdavidch
7172255736Sdavidch/* send load requrest to mcp and analyze response */
7173255736Sdavidchstatic int
7174255736Sdavidchbxe_nic_load_request(struct bxe_softc *sc,
7175255736Sdavidch                     uint32_t         *load_code)
7176255736Sdavidch{
7177255736Sdavidch    /* init fw_seq */
7178255736Sdavidch    sc->fw_seq =
7179255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
7180255736Sdavidch         DRV_MSG_SEQ_NUMBER_MASK);
7181255736Sdavidch
7182255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
7183255736Sdavidch
7184255736Sdavidch    /* get the current FW pulse sequence */
7185255736Sdavidch    sc->fw_drv_pulse_wr_seq =
7186255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
7187255736Sdavidch         DRV_PULSE_SEQ_MASK);
7188255736Sdavidch
7189255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
7190255736Sdavidch          sc->fw_drv_pulse_wr_seq);
7191255736Sdavidch
7192255736Sdavidch    /* load request */
7193255736Sdavidch    (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
7194255736Sdavidch                                  DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
7195255736Sdavidch
7196255736Sdavidch    /* if the MCP fails to respond we must abort */
7197255736Sdavidch    if (!(*load_code)) {
7198255736Sdavidch        BLOGE(sc, "MCP response failure!\n");
7199255736Sdavidch        return (-1);
7200255736Sdavidch    }
7201255736Sdavidch
7202255736Sdavidch    /* if MCP refused then must abort */
7203255736Sdavidch    if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
7204255736Sdavidch        BLOGE(sc, "MCP refused load request\n");
7205255736Sdavidch        return (-1);
7206255736Sdavidch    }
7207255736Sdavidch
7208255736Sdavidch    return (0);
7209255736Sdavidch}
7210255736Sdavidch
7211255736Sdavidch/*
7212255736Sdavidch * Check whether another PF has already loaded FW to chip. In virtualized
7213255736Sdavidch * environments a pf from anoth VM may have already initialized the device
7214255736Sdavidch * including loading FW.
7215255736Sdavidch */
7216255736Sdavidchstatic int
7217255736Sdavidchbxe_nic_load_analyze_req(struct bxe_softc *sc,
7218255736Sdavidch                         uint32_t         load_code)
7219255736Sdavidch{
7220255736Sdavidch    uint32_t my_fw, loaded_fw;
7221255736Sdavidch
7222255736Sdavidch    /* is another pf loaded on this engine? */
7223255736Sdavidch    if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
7224255736Sdavidch        (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
7225255736Sdavidch        /* build my FW version dword */
7226255736Sdavidch        my_fw = (BCM_5710_FW_MAJOR_VERSION +
7227255736Sdavidch                 (BCM_5710_FW_MINOR_VERSION << 8 ) +
7228255736Sdavidch                 (BCM_5710_FW_REVISION_VERSION << 16) +
7229255736Sdavidch                 (BCM_5710_FW_ENGINEERING_VERSION << 24));
7230255736Sdavidch
7231255736Sdavidch        /* read loaded FW from chip */
7232255736Sdavidch        loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
7233255736Sdavidch        BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
7234255736Sdavidch              loaded_fw, my_fw);
7235255736Sdavidch
7236255736Sdavidch        /* abort nic load if version mismatch */
7237255736Sdavidch        if (my_fw != loaded_fw) {
7238255736Sdavidch            BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
7239255736Sdavidch                  loaded_fw, my_fw);
7240255736Sdavidch            return (-1);
7241255736Sdavidch        }
7242255736Sdavidch    }
7243255736Sdavidch
7244255736Sdavidch    return (0);
7245255736Sdavidch}
7246255736Sdavidch
7247255736Sdavidch/* mark PMF if applicable */
7248255736Sdavidchstatic void
7249255736Sdavidchbxe_nic_load_pmf(struct bxe_softc *sc,
7250255736Sdavidch                 uint32_t         load_code)
7251255736Sdavidch{
7252255736Sdavidch    uint32_t ncsi_oem_data_addr;
7253255736Sdavidch
7254255736Sdavidch    if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
7255255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
7256255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
7257255736Sdavidch        /*
7258255736Sdavidch         * Barrier here for ordering between the writing to sc->port.pmf here
7259255736Sdavidch         * and reading it from the periodic task.
7260255736Sdavidch         */
7261255736Sdavidch        sc->port.pmf = 1;
7262255736Sdavidch        mb();
7263255736Sdavidch    } else {
7264255736Sdavidch        sc->port.pmf = 0;
7265255736Sdavidch    }
7266255736Sdavidch
7267255736Sdavidch    BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
7268255736Sdavidch
7269255736Sdavidch    /* XXX needed? */
7270255736Sdavidch    if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
7271255736Sdavidch        if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
7272255736Sdavidch            ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
7273255736Sdavidch            if (ncsi_oem_data_addr) {
7274255736Sdavidch                REG_WR(sc,
7275255736Sdavidch                       (ncsi_oem_data_addr +
7276255736Sdavidch                        offsetof(struct glob_ncsi_oem_data, driver_version)),
7277255736Sdavidch                       0);
7278255736Sdavidch            }
7279255736Sdavidch        }
7280255736Sdavidch    }
7281255736Sdavidch}
7282255736Sdavidch
7283255736Sdavidchstatic void
7284255736Sdavidchbxe_read_mf_cfg(struct bxe_softc *sc)
7285255736Sdavidch{
7286255736Sdavidch    int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
7287255736Sdavidch    int abs_func;
7288255736Sdavidch    int vn;
7289255736Sdavidch
7290255736Sdavidch    if (BXE_NOMCP(sc)) {
7291255736Sdavidch        return; /* what should be the default bvalue in this case */
7292255736Sdavidch    }
7293255736Sdavidch
7294255736Sdavidch    /*
7295255736Sdavidch     * The formula for computing the absolute function number is...
7296255736Sdavidch     * For 2 port configuration (4 functions per port):
7297255736Sdavidch     *   abs_func = 2 * vn + SC_PORT + SC_PATH
7298255736Sdavidch     * For 4 port configuration (2 functions per port):
7299255736Sdavidch     *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
7300255736Sdavidch     */
7301255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
7302255736Sdavidch        abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
7303255736Sdavidch        if (abs_func >= E1H_FUNC_MAX) {
7304255736Sdavidch            break;
7305255736Sdavidch        }
7306255736Sdavidch        sc->devinfo.mf_info.mf_config[vn] =
7307255736Sdavidch            MFCFG_RD(sc, func_mf_config[abs_func].config);
7308255736Sdavidch    }
7309255736Sdavidch
7310255736Sdavidch    if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
7311255736Sdavidch        FUNC_MF_CFG_FUNC_DISABLED) {
7312255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
7313255736Sdavidch        sc->flags |= BXE_MF_FUNC_DIS;
7314255736Sdavidch    } else {
7315255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
7316255736Sdavidch        sc->flags &= ~BXE_MF_FUNC_DIS;
7317255736Sdavidch    }
7318255736Sdavidch}
7319255736Sdavidch
7320255736Sdavidch/* acquire split MCP access lock register */
7321255736Sdavidchstatic int bxe_acquire_alr(struct bxe_softc *sc)
7322255736Sdavidch{
7323255736Sdavidch    uint32_t j, val;
7324255736Sdavidch
7325255736Sdavidch    for (j = 0; j < 1000; j++) {
7326255736Sdavidch        val = (1UL << 31);
7327255736Sdavidch        REG_WR(sc, GRCBASE_MCP + 0x9c, val);
7328255736Sdavidch        val = REG_RD(sc, GRCBASE_MCP + 0x9c);
7329255736Sdavidch        if (val & (1L << 31))
7330255736Sdavidch            break;
7331255736Sdavidch
7332255736Sdavidch        DELAY(5000);
7333255736Sdavidch    }
7334255736Sdavidch
7335255736Sdavidch    if (!(val & (1L << 31))) {
7336255736Sdavidch        BLOGE(sc, "Cannot acquire MCP access lock register\n");
7337255736Sdavidch        return (-1);
7338255736Sdavidch    }
7339255736Sdavidch
7340255736Sdavidch    return (0);
7341255736Sdavidch}
7342255736Sdavidch
7343255736Sdavidch/* release split MCP access lock register */
7344255736Sdavidchstatic void bxe_release_alr(struct bxe_softc *sc)
7345255736Sdavidch{
7346255736Sdavidch    REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
7347255736Sdavidch}
7348255736Sdavidch
7349255736Sdavidchstatic void
7350255736Sdavidchbxe_fan_failure(struct bxe_softc *sc)
7351255736Sdavidch{
7352255736Sdavidch    int port = SC_PORT(sc);
7353255736Sdavidch    uint32_t ext_phy_config;
7354255736Sdavidch
7355255736Sdavidch    /* mark the failure */
7356255736Sdavidch    ext_phy_config =
7357255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7358255736Sdavidch
7359255736Sdavidch    ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7360255736Sdavidch    ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7361255736Sdavidch    SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7362255736Sdavidch             ext_phy_config);
7363255736Sdavidch
7364255736Sdavidch    /* log the failure */
7365255736Sdavidch    BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7366255736Sdavidch              "the card to prevent permanent damage. "
7367255736Sdavidch              "Please contact OEM Support for assistance\n");
7368255736Sdavidch
7369255736Sdavidch    /* XXX */
7370255736Sdavidch#if 1
7371255736Sdavidch    bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7372255736Sdavidch#else
7373255736Sdavidch    /*
7374255736Sdavidch     * Schedule device reset (unload)
7375255736Sdavidch     * This is due to some boards consuming sufficient power when driver is
7376255736Sdavidch     * up to overheat if fan fails.
7377255736Sdavidch     */
7378255736Sdavidch    bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7379255736Sdavidch    schedule_delayed_work(&sc->sp_rtnl_task, 0);
7380255736Sdavidch#endif
7381255736Sdavidch}
7382255736Sdavidch
7383255736Sdavidch/* this function is called upon a link interrupt */
7384255736Sdavidchstatic void
7385255736Sdavidchbxe_link_attn(struct bxe_softc *sc)
7386255736Sdavidch{
7387255736Sdavidch    uint32_t pause_enabled = 0;
7388255736Sdavidch    struct host_port_stats *pstats;
7389255736Sdavidch    int cmng_fns;
7390255736Sdavidch
7391255736Sdavidch    /* Make sure that we are synced with the current statistics */
7392255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_STOP);
7393255736Sdavidch
7394255736Sdavidch    elink_link_update(&sc->link_params, &sc->link_vars);
7395255736Sdavidch
7396255736Sdavidch    if (sc->link_vars.link_up) {
7397255736Sdavidch
7398255736Sdavidch        /* dropless flow control */
7399255736Sdavidch        if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7400255736Sdavidch            pause_enabled = 0;
7401255736Sdavidch
7402255736Sdavidch            if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7403255736Sdavidch                pause_enabled = 1;
7404255736Sdavidch            }
7405255736Sdavidch
7406255736Sdavidch            REG_WR(sc,
7407255736Sdavidch                   (BAR_USTRORM_INTMEM +
7408255736Sdavidch                    USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7409255736Sdavidch                   pause_enabled);
7410255736Sdavidch        }
7411255736Sdavidch
7412255736Sdavidch        if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7413255736Sdavidch            pstats = BXE_SP(sc, port_stats);
7414255736Sdavidch            /* reset old mac stats */
7415255736Sdavidch            memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7416255736Sdavidch        }
7417255736Sdavidch
7418255736Sdavidch        if (sc->state == BXE_STATE_OPEN) {
7419255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7420255736Sdavidch        }
7421255736Sdavidch    }
7422255736Sdavidch
7423255736Sdavidch    if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7424255736Sdavidch        cmng_fns = bxe_get_cmng_fns_mode(sc);
7425255736Sdavidch
7426255736Sdavidch        if (cmng_fns != CMNG_FNS_NONE) {
7427255736Sdavidch            bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7428255736Sdavidch            storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7429255736Sdavidch        } else {
7430255736Sdavidch            /* rate shaping and fairness are disabled */
7431255736Sdavidch            BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7432255736Sdavidch        }
7433255736Sdavidch    }
7434255736Sdavidch
7435255736Sdavidch    bxe_link_report_locked(sc);
7436255736Sdavidch
7437255736Sdavidch    if (IS_MF(sc)) {
7438255736Sdavidch        ; // XXX bxe_link_sync_notify(sc);
7439255736Sdavidch    }
7440255736Sdavidch}
7441255736Sdavidch
7442255736Sdavidchstatic void
7443255736Sdavidchbxe_attn_int_asserted(struct bxe_softc *sc,
7444255736Sdavidch                      uint32_t         asserted)
7445255736Sdavidch{
7446255736Sdavidch    int port = SC_PORT(sc);
7447255736Sdavidch    uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7448255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
7449255736Sdavidch    uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7450255736Sdavidch                                        NIG_REG_MASK_INTERRUPT_PORT0;
7451255736Sdavidch    uint32_t aeu_mask;
7452255736Sdavidch    uint32_t nig_mask = 0;
7453255736Sdavidch    uint32_t reg_addr;
7454255736Sdavidch    uint32_t igu_acked;
7455255736Sdavidch    uint32_t cnt;
7456255736Sdavidch
7457255736Sdavidch    if (sc->attn_state & asserted) {
7458255736Sdavidch        BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7459255736Sdavidch    }
7460255736Sdavidch
7461255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7462255736Sdavidch
7463255736Sdavidch    aeu_mask = REG_RD(sc, aeu_addr);
7464255736Sdavidch
7465255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7466255736Sdavidch          aeu_mask, asserted);
7467255736Sdavidch
7468255736Sdavidch    aeu_mask &= ~(asserted & 0x3ff);
7469255736Sdavidch
7470255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7471255736Sdavidch
7472255736Sdavidch    REG_WR(sc, aeu_addr, aeu_mask);
7473255736Sdavidch
7474255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7475255736Sdavidch
7476255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7477255736Sdavidch    sc->attn_state |= asserted;
7478255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7479255736Sdavidch
7480255736Sdavidch    if (asserted & ATTN_HARD_WIRED_MASK) {
7481255736Sdavidch        if (asserted & ATTN_NIG_FOR_FUNC) {
7482255736Sdavidch
7483284335Sdavidcs	    bxe_acquire_phy_lock(sc);
7484255736Sdavidch            /* save nig interrupt mask */
7485255736Sdavidch            nig_mask = REG_RD(sc, nig_int_mask_addr);
7486255736Sdavidch
7487255736Sdavidch            /* If nig_mask is not set, no need to call the update function */
7488255736Sdavidch            if (nig_mask) {
7489255736Sdavidch                REG_WR(sc, nig_int_mask_addr, 0);
7490255736Sdavidch
7491255736Sdavidch                bxe_link_attn(sc);
7492255736Sdavidch            }
7493255736Sdavidch
7494255736Sdavidch            /* handle unicore attn? */
7495255736Sdavidch        }
7496255736Sdavidch
7497255736Sdavidch        if (asserted & ATTN_SW_TIMER_4_FUNC) {
7498255736Sdavidch            BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7499255736Sdavidch        }
7500255736Sdavidch
7501255736Sdavidch        if (asserted & GPIO_2_FUNC) {
7502255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7503255736Sdavidch        }
7504255736Sdavidch
7505255736Sdavidch        if (asserted & GPIO_3_FUNC) {
7506255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7507255736Sdavidch        }
7508255736Sdavidch
7509255736Sdavidch        if (asserted & GPIO_4_FUNC) {
7510255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7511255736Sdavidch        }
7512255736Sdavidch
7513255736Sdavidch        if (port == 0) {
7514255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_1) {
7515255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7516255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7517255736Sdavidch            }
7518255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_2) {
7519255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7520255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7521255736Sdavidch            }
7522255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_3) {
7523255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7524255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7525255736Sdavidch            }
7526255736Sdavidch        } else {
7527255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_4) {
7528255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7529255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7530255736Sdavidch            }
7531255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_5) {
7532255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7533255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7534255736Sdavidch            }
7535255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_6) {
7536255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7537255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7538255736Sdavidch            }
7539255736Sdavidch        }
7540255736Sdavidch    } /* hardwired */
7541255736Sdavidch
7542255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7543255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7544255736Sdavidch    } else {
7545255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7546255736Sdavidch    }
7547255736Sdavidch
7548255736Sdavidch    BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7549255736Sdavidch          asserted,
7550255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7551255736Sdavidch    REG_WR(sc, reg_addr, asserted);
7552255736Sdavidch
7553255736Sdavidch    /* now set back the mask */
7554255736Sdavidch    if (asserted & ATTN_NIG_FOR_FUNC) {
7555255736Sdavidch        /*
7556255736Sdavidch         * Verify that IGU ack through BAR was written before restoring
7557255736Sdavidch         * NIG mask. This loop should exit after 2-3 iterations max.
7558255736Sdavidch         */
7559255736Sdavidch        if (sc->devinfo.int_block != INT_BLOCK_HC) {
7560255736Sdavidch            cnt = 0;
7561255736Sdavidch
7562255736Sdavidch            do {
7563255736Sdavidch                igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7564255736Sdavidch            } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7565255736Sdavidch                     (++cnt < MAX_IGU_ATTN_ACK_TO));
7566255736Sdavidch
7567255736Sdavidch            if (!igu_acked) {
7568255736Sdavidch                BLOGE(sc, "Failed to verify IGU ack on time\n");
7569255736Sdavidch            }
7570255736Sdavidch
7571255736Sdavidch            mb();
7572255736Sdavidch        }
7573255736Sdavidch
7574255736Sdavidch        REG_WR(sc, nig_int_mask_addr, nig_mask);
7575255736Sdavidch
7576284335Sdavidcs	bxe_release_phy_lock(sc);
7577255736Sdavidch    }
7578255736Sdavidch}
7579255736Sdavidch
7580255736Sdavidchstatic void
7581255736Sdavidchbxe_print_next_block(struct bxe_softc *sc,
7582255736Sdavidch                     int              idx,
7583255736Sdavidch                     const char       *blk)
7584255736Sdavidch{
7585255736Sdavidch    BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7586255736Sdavidch}
7587255736Sdavidch
7588255736Sdavidchstatic int
7589255736Sdavidchbxe_check_blocks_with_parity0(struct bxe_softc *sc,
7590255736Sdavidch                              uint32_t         sig,
7591255736Sdavidch                              int              par_num,
7592255736Sdavidch                              uint8_t          print)
7593255736Sdavidch{
7594255736Sdavidch    uint32_t cur_bit = 0;
7595255736Sdavidch    int i = 0;
7596255736Sdavidch
7597255736Sdavidch    for (i = 0; sig; i++) {
7598255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7599255736Sdavidch        if (sig & cur_bit) {
7600255736Sdavidch            switch (cur_bit) {
7601255736Sdavidch            case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7602255736Sdavidch                if (print)
7603255736Sdavidch                    bxe_print_next_block(sc, par_num++, "BRB");
7604255736Sdavidch                break;
7605255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7606255736Sdavidch                if (print)
7607255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PARSER");
7608255736Sdavidch                break;
7609255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7610255736Sdavidch                if (print)
7611255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSDM");
7612255736Sdavidch                break;
7613255736Sdavidch            case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7614255736Sdavidch                if (print)
7615255736Sdavidch                    bxe_print_next_block(sc, par_num++, "SEARCHER");
7616255736Sdavidch                break;
7617255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7618255736Sdavidch                if (print)
7619255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TCM");
7620255736Sdavidch                break;
7621255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7622255736Sdavidch                if (print)
7623255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSEMI");
7624255736Sdavidch                break;
7625255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7626255736Sdavidch                if (print)
7627255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XPB");
7628255736Sdavidch                break;
7629255736Sdavidch            }
7630255736Sdavidch
7631255736Sdavidch            /* Clear the bit */
7632255736Sdavidch            sig &= ~cur_bit;
7633255736Sdavidch        }
7634255736Sdavidch    }
7635255736Sdavidch
7636255736Sdavidch    return (par_num);
7637255736Sdavidch}
7638255736Sdavidch
7639255736Sdavidchstatic int
7640255736Sdavidchbxe_check_blocks_with_parity1(struct bxe_softc *sc,
7641255736Sdavidch                              uint32_t         sig,
7642255736Sdavidch                              int              par_num,
7643255736Sdavidch                              uint8_t          *global,
7644255736Sdavidch                              uint8_t          print)
7645255736Sdavidch{
7646255736Sdavidch    int i = 0;
7647255736Sdavidch    uint32_t cur_bit = 0;
7648255736Sdavidch    for (i = 0; sig; i++) {
7649255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7650255736Sdavidch        if (sig & cur_bit) {
7651255736Sdavidch            switch (cur_bit) {
7652255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7653255736Sdavidch                if (print)
7654255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PBF");
7655255736Sdavidch                break;
7656255736Sdavidch            case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7657255736Sdavidch                if (print)
7658255736Sdavidch                    bxe_print_next_block(sc, par_num++, "QM");
7659255736Sdavidch                break;
7660255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7661255736Sdavidch                if (print)
7662255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TM");
7663255736Sdavidch                break;
7664255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7665255736Sdavidch                if (print)
7666255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSDM");
7667255736Sdavidch                break;
7668255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7669255736Sdavidch                if (print)
7670255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XCM");
7671255736Sdavidch                break;
7672255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7673255736Sdavidch                if (print)
7674255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSEMI");
7675255736Sdavidch                break;
7676255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7677255736Sdavidch                if (print)
7678255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7679255736Sdavidch                break;
7680255736Sdavidch            case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7681255736Sdavidch                if (print)
7682255736Sdavidch                    bxe_print_next_block(sc, par_num++, "NIG");
7683255736Sdavidch                break;
7684255736Sdavidch            case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7685255736Sdavidch                if (print)
7686255736Sdavidch                    bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7687255736Sdavidch                *global = TRUE;
7688255736Sdavidch                break;
7689255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7690255736Sdavidch                if (print)
7691255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DEBUG");
7692255736Sdavidch                break;
7693255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7694255736Sdavidch                if (print)
7695255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USDM");
7696255736Sdavidch                break;
7697255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7698255736Sdavidch                if (print)
7699255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UCM");
7700255736Sdavidch                break;
7701255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7702255736Sdavidch                if (print)
7703255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USEMI");
7704255736Sdavidch                break;
7705255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7706255736Sdavidch                if (print)
7707255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UPB");
7708255736Sdavidch                break;
7709255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7710255736Sdavidch                if (print)
7711255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSDM");
7712255736Sdavidch                break;
7713255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7714255736Sdavidch                if (print)
7715255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CCM");
7716255736Sdavidch                break;
7717255736Sdavidch            }
7718255736Sdavidch
7719255736Sdavidch            /* Clear the bit */
7720255736Sdavidch            sig &= ~cur_bit;
7721255736Sdavidch        }
7722255736Sdavidch    }
7723255736Sdavidch
7724255736Sdavidch    return (par_num);
7725255736Sdavidch}
7726255736Sdavidch
7727255736Sdavidchstatic int
7728255736Sdavidchbxe_check_blocks_with_parity2(struct bxe_softc *sc,
7729255736Sdavidch                              uint32_t         sig,
7730255736Sdavidch                              int              par_num,
7731255736Sdavidch                              uint8_t          print)
7732255736Sdavidch{
7733255736Sdavidch    uint32_t cur_bit = 0;
7734255736Sdavidch    int i = 0;
7735255736Sdavidch
7736255736Sdavidch    for (i = 0; sig; i++) {
7737255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7738255736Sdavidch        if (sig & cur_bit) {
7739255736Sdavidch            switch (cur_bit) {
7740255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7741255736Sdavidch                if (print)
7742255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSEMI");
7743255736Sdavidch                break;
7744255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7745255736Sdavidch                if (print)
7746255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXP");
7747255736Sdavidch                break;
7748255736Sdavidch            case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7749255736Sdavidch                if (print)
7750255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7751255736Sdavidch                break;
7752255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7753255736Sdavidch                if (print)
7754255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CFC");
7755255736Sdavidch                break;
7756255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7757255736Sdavidch                if (print)
7758255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CDU");
7759255736Sdavidch                break;
7760255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7761255736Sdavidch                if (print)
7762255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DMAE");
7763255736Sdavidch                break;
7764255736Sdavidch            case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7765255736Sdavidch                if (print)
7766255736Sdavidch                    bxe_print_next_block(sc, par_num++, "IGU");
7767255736Sdavidch                break;
7768255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7769255736Sdavidch                if (print)
7770255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MISC");
7771255736Sdavidch                break;
7772255736Sdavidch            }
7773255736Sdavidch
7774255736Sdavidch            /* Clear the bit */
7775255736Sdavidch            sig &= ~cur_bit;
7776255736Sdavidch        }
7777255736Sdavidch    }
7778255736Sdavidch
7779255736Sdavidch    return (par_num);
7780255736Sdavidch}
7781255736Sdavidch
7782255736Sdavidchstatic int
7783255736Sdavidchbxe_check_blocks_with_parity3(struct bxe_softc *sc,
7784255736Sdavidch                              uint32_t         sig,
7785255736Sdavidch                              int              par_num,
7786255736Sdavidch                              uint8_t          *global,
7787255736Sdavidch                              uint8_t          print)
7788255736Sdavidch{
7789255736Sdavidch    uint32_t cur_bit = 0;
7790255736Sdavidch    int i = 0;
7791255736Sdavidch
7792255736Sdavidch    for (i = 0; sig; i++) {
7793255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7794255736Sdavidch        if (sig & cur_bit) {
7795255736Sdavidch            switch (cur_bit) {
7796255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7797255736Sdavidch                if (print)
7798255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MCP ROM");
7799255736Sdavidch                *global = TRUE;
7800255736Sdavidch                break;
7801255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7802255736Sdavidch                if (print)
7803255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7804255736Sdavidch                              "MCP UMP RX");
7805255736Sdavidch                *global = TRUE;
7806255736Sdavidch                break;
7807255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7808255736Sdavidch                if (print)
7809255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7810255736Sdavidch                              "MCP UMP TX");
7811255736Sdavidch                *global = TRUE;
7812255736Sdavidch                break;
7813255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7814255736Sdavidch                if (print)
7815255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7816255736Sdavidch                              "MCP SCPAD");
7817255736Sdavidch                *global = TRUE;
7818255736Sdavidch                break;
7819255736Sdavidch            }
7820255736Sdavidch
7821255736Sdavidch            /* Clear the bit */
7822255736Sdavidch            sig &= ~cur_bit;
7823255736Sdavidch        }
7824255736Sdavidch    }
7825255736Sdavidch
7826255736Sdavidch    return (par_num);
7827255736Sdavidch}
7828255736Sdavidch
7829255736Sdavidchstatic int
7830255736Sdavidchbxe_check_blocks_with_parity4(struct bxe_softc *sc,
7831255736Sdavidch                              uint32_t         sig,
7832255736Sdavidch                              int              par_num,
7833255736Sdavidch                              uint8_t          print)
7834255736Sdavidch{
7835255736Sdavidch    uint32_t cur_bit = 0;
7836255736Sdavidch    int i = 0;
7837255736Sdavidch
7838255736Sdavidch    for (i = 0; sig; i++) {
7839255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7840255736Sdavidch        if (sig & cur_bit) {
7841255736Sdavidch            switch (cur_bit) {
7842255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7843255736Sdavidch                if (print)
7844255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PGLUE_B");
7845255736Sdavidch                break;
7846255736Sdavidch            case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7847255736Sdavidch                if (print)
7848255736Sdavidch                    bxe_print_next_block(sc, par_num++, "ATC");
7849255736Sdavidch                break;
7850255736Sdavidch            }
7851255736Sdavidch
7852255736Sdavidch            /* Clear the bit */
7853255736Sdavidch            sig &= ~cur_bit;
7854255736Sdavidch        }
7855255736Sdavidch    }
7856255736Sdavidch
7857255736Sdavidch    return (par_num);
7858255736Sdavidch}
7859255736Sdavidch
7860255736Sdavidchstatic uint8_t
7861255736Sdavidchbxe_parity_attn(struct bxe_softc *sc,
7862255736Sdavidch                uint8_t          *global,
7863255736Sdavidch                uint8_t          print,
7864255736Sdavidch                uint32_t         *sig)
7865255736Sdavidch{
7866255736Sdavidch    int par_num = 0;
7867255736Sdavidch
7868255736Sdavidch    if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7869255736Sdavidch        (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7870255736Sdavidch        (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7871255736Sdavidch        (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7872255736Sdavidch        (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7873255736Sdavidch        BLOGE(sc, "Parity error: HW block parity attention:\n"
7874255736Sdavidch                  "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7875255736Sdavidch              (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7876255736Sdavidch              (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7877255736Sdavidch              (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7878255736Sdavidch              (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7879255736Sdavidch              (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7880255736Sdavidch
7881255736Sdavidch        if (print)
7882255736Sdavidch            BLOGI(sc, "Parity errors detected in blocks: ");
7883255736Sdavidch
7884255736Sdavidch        par_num =
7885255736Sdavidch            bxe_check_blocks_with_parity0(sc, sig[0] &
7886255736Sdavidch                                          HW_PRTY_ASSERT_SET_0,
7887255736Sdavidch                                          par_num, print);
7888255736Sdavidch        par_num =
7889255736Sdavidch            bxe_check_blocks_with_parity1(sc, sig[1] &
7890255736Sdavidch                                          HW_PRTY_ASSERT_SET_1,
7891255736Sdavidch                                          par_num, global, print);
7892255736Sdavidch        par_num =
7893255736Sdavidch            bxe_check_blocks_with_parity2(sc, sig[2] &
7894255736Sdavidch                                          HW_PRTY_ASSERT_SET_2,
7895255736Sdavidch                                          par_num, print);
7896255736Sdavidch        par_num =
7897255736Sdavidch            bxe_check_blocks_with_parity3(sc, sig[3] &
7898255736Sdavidch                                          HW_PRTY_ASSERT_SET_3,
7899255736Sdavidch                                          par_num, global, print);
7900255736Sdavidch        par_num =
7901255736Sdavidch            bxe_check_blocks_with_parity4(sc, sig[4] &
7902255736Sdavidch                                          HW_PRTY_ASSERT_SET_4,
7903255736Sdavidch                                          par_num, print);
7904255736Sdavidch
7905255736Sdavidch        if (print)
7906255736Sdavidch            BLOGI(sc, "\n");
7907255736Sdavidch
7908255736Sdavidch        return (TRUE);
7909255736Sdavidch    }
7910255736Sdavidch
7911255736Sdavidch    return (FALSE);
7912255736Sdavidch}
7913255736Sdavidch
7914255736Sdavidchstatic uint8_t
7915255736Sdavidchbxe_chk_parity_attn(struct bxe_softc *sc,
7916255736Sdavidch                    uint8_t          *global,
7917255736Sdavidch                    uint8_t          print)
7918255736Sdavidch{
7919255736Sdavidch    struct attn_route attn = { {0} };
7920255736Sdavidch    int port = SC_PORT(sc);
7921255736Sdavidch
7922255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7923255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7924255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7925255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7926255736Sdavidch
7927255736Sdavidch    if (!CHIP_IS_E1x(sc))
7928255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7929255736Sdavidch
7930255736Sdavidch    return (bxe_parity_attn(sc, global, print, attn.sig));
7931255736Sdavidch}
7932255736Sdavidch
7933255736Sdavidchstatic void
7934255736Sdavidchbxe_attn_int_deasserted4(struct bxe_softc *sc,
7935255736Sdavidch                         uint32_t         attn)
7936255736Sdavidch{
7937255736Sdavidch    uint32_t val;
7938255736Sdavidch
7939255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7940255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7941255736Sdavidch        BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7942255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7943255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7944255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7945255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7946255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7947255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7948255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7949255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7950255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7951255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7952255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7953255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7954255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7955255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7956255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7957255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7958255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7959255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7960255736Sdavidch    }
7961255736Sdavidch
7962255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7963255736Sdavidch        val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7964255736Sdavidch        BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7965255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7966255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7967255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7968255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7969255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7970255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7971255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7972255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7973255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7974255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7975255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7976255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7977255736Sdavidch    }
7978255736Sdavidch
7979255736Sdavidch    if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7980255736Sdavidch                AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7981255736Sdavidch        BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7982255736Sdavidch              (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7983255736Sdavidch                                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7984255736Sdavidch    }
7985255736Sdavidch}
7986255736Sdavidch
7987255736Sdavidchstatic void
7988255736Sdavidchbxe_e1h_disable(struct bxe_softc *sc)
7989255736Sdavidch{
7990255736Sdavidch    int port = SC_PORT(sc);
7991255736Sdavidch
7992255736Sdavidch    bxe_tx_disable(sc);
7993255736Sdavidch
7994255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7995255736Sdavidch}
7996255736Sdavidch
7997255736Sdavidchstatic void
7998255736Sdavidchbxe_e1h_enable(struct bxe_softc *sc)
7999255736Sdavidch{
8000255736Sdavidch    int port = SC_PORT(sc);
8001255736Sdavidch
8002255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
8003255736Sdavidch
8004255736Sdavidch    // XXX bxe_tx_enable(sc);
8005255736Sdavidch}
8006255736Sdavidch
8007255736Sdavidch/*
8008255736Sdavidch * called due to MCP event (on pmf):
8009255736Sdavidch *   reread new bandwidth configuration
8010255736Sdavidch *   configure FW
8011255736Sdavidch *   notify others function about the change
8012255736Sdavidch */
8013255736Sdavidchstatic void
8014255736Sdavidchbxe_config_mf_bw(struct bxe_softc *sc)
8015255736Sdavidch{
8016255736Sdavidch    if (sc->link_vars.link_up) {
8017255736Sdavidch        bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
8018255736Sdavidch        // XXX bxe_link_sync_notify(sc);
8019255736Sdavidch    }
8020255736Sdavidch
8021255736Sdavidch    storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
8022255736Sdavidch}
8023255736Sdavidch
8024255736Sdavidchstatic void
8025255736Sdavidchbxe_set_mf_bw(struct bxe_softc *sc)
8026255736Sdavidch{
8027255736Sdavidch    bxe_config_mf_bw(sc);
8028255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
8029255736Sdavidch}
8030255736Sdavidch
8031255736Sdavidchstatic void
8032255736Sdavidchbxe_handle_eee_event(struct bxe_softc *sc)
8033255736Sdavidch{
8034255736Sdavidch    BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
8035255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
8036255736Sdavidch}
8037255736Sdavidch
8038255736Sdavidch#define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
8039255736Sdavidch
8040255736Sdavidchstatic void
8041255736Sdavidchbxe_drv_info_ether_stat(struct bxe_softc *sc)
8042255736Sdavidch{
8043255736Sdavidch    struct eth_stats_info *ether_stat =
8044255736Sdavidch        &sc->sp->drv_info_to_mcp.ether_stat;
8045255736Sdavidch
8046255736Sdavidch    strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
8047255736Sdavidch            ETH_STAT_INFO_VERSION_LEN);
8048255736Sdavidch
8049255736Sdavidch    /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
8050255736Sdavidch    sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
8051255736Sdavidch                                          DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
8052255736Sdavidch                                          ether_stat->mac_local + MAC_PAD,
8053255736Sdavidch                                          MAC_PAD, ETH_ALEN);
8054255736Sdavidch
8055255736Sdavidch    ether_stat->mtu_size = sc->mtu;
8056255736Sdavidch
8057255736Sdavidch    ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
8058266979Smarcel    if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
8059255736Sdavidch        ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
8060255736Sdavidch    }
8061255736Sdavidch
8062255736Sdavidch    // XXX ether_stat->feature_flags |= ???;
8063255736Sdavidch
8064255736Sdavidch    ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
8065255736Sdavidch
8066255736Sdavidch    ether_stat->txq_size = sc->tx_ring_size;
8067255736Sdavidch    ether_stat->rxq_size = sc->rx_ring_size;
8068255736Sdavidch}
8069255736Sdavidch
8070255736Sdavidchstatic void
8071255736Sdavidchbxe_handle_drv_info_req(struct bxe_softc *sc)
8072255736Sdavidch{
8073255736Sdavidch    enum drv_info_opcode op_code;
8074255736Sdavidch    uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
8075255736Sdavidch
8076255736Sdavidch    /* if drv_info version supported by MFW doesn't match - send NACK */
8077255736Sdavidch    if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
8078255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
8079255736Sdavidch        return;
8080255736Sdavidch    }
8081255736Sdavidch
8082255736Sdavidch    op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
8083255736Sdavidch               DRV_INFO_CONTROL_OP_CODE_SHIFT);
8084255736Sdavidch
8085255736Sdavidch    memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
8086255736Sdavidch
8087255736Sdavidch    switch (op_code) {
8088255736Sdavidch    case ETH_STATS_OPCODE:
8089255736Sdavidch        bxe_drv_info_ether_stat(sc);
8090255736Sdavidch        break;
8091255736Sdavidch    case FCOE_STATS_OPCODE:
8092255736Sdavidch    case ISCSI_STATS_OPCODE:
8093255736Sdavidch    default:
8094255736Sdavidch        /* if op code isn't supported - send NACK */
8095255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
8096255736Sdavidch        return;
8097255736Sdavidch    }
8098255736Sdavidch
8099255736Sdavidch    /*
8100255736Sdavidch     * If we got drv_info attn from MFW then these fields are defined in
8101255736Sdavidch     * shmem2 for sure
8102255736Sdavidch     */
8103255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_lo,
8104255736Sdavidch              U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
8105255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_hi,
8106255736Sdavidch              U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
8107255736Sdavidch
8108255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
8109255736Sdavidch}
8110255736Sdavidch
8111255736Sdavidchstatic void
8112255736Sdavidchbxe_dcc_event(struct bxe_softc *sc,
8113255736Sdavidch              uint32_t         dcc_event)
8114255736Sdavidch{
8115255736Sdavidch    BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
8116255736Sdavidch
8117255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
8118255736Sdavidch        /*
8119255736Sdavidch         * This is the only place besides the function initialization
8120255736Sdavidch         * where the sc->flags can change so it is done without any
8121255736Sdavidch         * locks
8122255736Sdavidch         */
8123255736Sdavidch        if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
8124255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
8125255736Sdavidch            sc->flags |= BXE_MF_FUNC_DIS;
8126255736Sdavidch            bxe_e1h_disable(sc);
8127255736Sdavidch        } else {
8128255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
8129255736Sdavidch            sc->flags &= ~BXE_MF_FUNC_DIS;
8130255736Sdavidch            bxe_e1h_enable(sc);
8131255736Sdavidch        }
8132255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
8133255736Sdavidch    }
8134255736Sdavidch
8135255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
8136255736Sdavidch        bxe_config_mf_bw(sc);
8137255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
8138255736Sdavidch    }
8139255736Sdavidch
8140255736Sdavidch    /* Report results to MCP */
8141255736Sdavidch    if (dcc_event)
8142255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
8143255736Sdavidch    else
8144255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
8145255736Sdavidch}
8146255736Sdavidch
8147255736Sdavidchstatic void
8148255736Sdavidchbxe_pmf_update(struct bxe_softc *sc)
8149255736Sdavidch{
8150255736Sdavidch    int port = SC_PORT(sc);
8151255736Sdavidch    uint32_t val;
8152255736Sdavidch
8153255736Sdavidch    sc->port.pmf = 1;
8154255736Sdavidch    BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
8155255736Sdavidch
8156255736Sdavidch    /*
8157255736Sdavidch     * We need the mb() to ensure the ordering between the writing to
8158255736Sdavidch     * sc->port.pmf here and reading it from the bxe_periodic_task().
8159255736Sdavidch     */
8160255736Sdavidch    mb();
8161255736Sdavidch
8162255736Sdavidch    /* queue a periodic task */
8163255736Sdavidch    // XXX schedule task...
8164255736Sdavidch
8165255736Sdavidch    // XXX bxe_dcbx_pmf_update(sc);
8166255736Sdavidch
8167255736Sdavidch    /* enable nig attention */
8168255736Sdavidch    val = (0xff0f | (1 << (SC_VN(sc) + 4)));
8169255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
8170255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
8171255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
8172255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
8173255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
8174255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
8175255736Sdavidch    }
8176255736Sdavidch
8177255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_PMF);
8178255736Sdavidch}
8179255736Sdavidch
8180255736Sdavidchstatic int
8181255736Sdavidchbxe_mc_assert(struct bxe_softc *sc)
8182255736Sdavidch{
8183255736Sdavidch    char last_idx;
8184255736Sdavidch    int i, rc = 0;
8185255736Sdavidch    uint32_t row0, row1, row2, row3;
8186255736Sdavidch
8187255736Sdavidch    /* XSTORM */
8188255736Sdavidch    last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
8189255736Sdavidch    if (last_idx)
8190255736Sdavidch        BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8191255736Sdavidch
8192255736Sdavidch    /* print the asserts */
8193255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8194255736Sdavidch
8195255736Sdavidch        row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
8196255736Sdavidch        row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
8197255736Sdavidch        row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
8198255736Sdavidch        row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
8199255736Sdavidch
8200255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8201255736Sdavidch            BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8202255736Sdavidch                  i, row3, row2, row1, row0);
8203255736Sdavidch            rc++;
8204255736Sdavidch        } else {
8205255736Sdavidch            break;
8206255736Sdavidch        }
8207255736Sdavidch    }
8208255736Sdavidch
8209255736Sdavidch    /* TSTORM */
8210255736Sdavidch    last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
8211255736Sdavidch    if (last_idx) {
8212255736Sdavidch        BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8213255736Sdavidch    }
8214255736Sdavidch
8215255736Sdavidch    /* print the asserts */
8216255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8217255736Sdavidch
8218255736Sdavidch        row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
8219255736Sdavidch        row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
8220255736Sdavidch        row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
8221255736Sdavidch        row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
8222255736Sdavidch
8223255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8224255736Sdavidch            BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8225255736Sdavidch                  i, row3, row2, row1, row0);
8226255736Sdavidch            rc++;
8227255736Sdavidch        } else {
8228255736Sdavidch            break;
8229255736Sdavidch        }
8230255736Sdavidch    }
8231255736Sdavidch
8232255736Sdavidch    /* CSTORM */
8233255736Sdavidch    last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
8234255736Sdavidch    if (last_idx) {
8235255736Sdavidch        BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8236255736Sdavidch    }
8237255736Sdavidch
8238255736Sdavidch    /* print the asserts */
8239255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8240255736Sdavidch
8241255736Sdavidch        row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
8242255736Sdavidch        row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
8243255736Sdavidch        row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
8244255736Sdavidch        row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
8245255736Sdavidch
8246255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8247255736Sdavidch            BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8248255736Sdavidch                  i, row3, row2, row1, row0);
8249255736Sdavidch            rc++;
8250255736Sdavidch        } else {
8251255736Sdavidch            break;
8252255736Sdavidch        }
8253255736Sdavidch    }
8254255736Sdavidch
8255255736Sdavidch    /* USTORM */
8256255736Sdavidch    last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
8257255736Sdavidch    if (last_idx) {
8258255736Sdavidch        BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8259255736Sdavidch    }
8260255736Sdavidch
8261255736Sdavidch    /* print the asserts */
8262255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8263255736Sdavidch
8264255736Sdavidch        row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
8265255736Sdavidch        row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
8266255736Sdavidch        row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
8267255736Sdavidch        row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
8268255736Sdavidch
8269255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8270255736Sdavidch            BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8271255736Sdavidch                  i, row3, row2, row1, row0);
8272255736Sdavidch            rc++;
8273255736Sdavidch        } else {
8274255736Sdavidch            break;
8275255736Sdavidch        }
8276255736Sdavidch    }
8277255736Sdavidch
8278255736Sdavidch    return (rc);
8279255736Sdavidch}
8280255736Sdavidch
8281255736Sdavidchstatic void
8282255736Sdavidchbxe_attn_int_deasserted3(struct bxe_softc *sc,
8283255736Sdavidch                         uint32_t         attn)
8284255736Sdavidch{
8285255736Sdavidch    int func = SC_FUNC(sc);
8286255736Sdavidch    uint32_t val;
8287255736Sdavidch
8288255736Sdavidch    if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
8289255736Sdavidch
8290255736Sdavidch        if (attn & BXE_PMF_LINK_ASSERT(sc)) {
8291255736Sdavidch
8292255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
8293255736Sdavidch            bxe_read_mf_cfg(sc);
8294255736Sdavidch            sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
8295255736Sdavidch                MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
8296255736Sdavidch            val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
8297255736Sdavidch
8298255736Sdavidch            if (val & DRV_STATUS_DCC_EVENT_MASK)
8299255736Sdavidch                bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
8300255736Sdavidch
8301255736Sdavidch            if (val & DRV_STATUS_SET_MF_BW)
8302255736Sdavidch                bxe_set_mf_bw(sc);
8303255736Sdavidch
8304255736Sdavidch            if (val & DRV_STATUS_DRV_INFO_REQ)
8305255736Sdavidch                bxe_handle_drv_info_req(sc);
8306255736Sdavidch
8307255736Sdavidch#if 0
8308255736Sdavidch            if (val & DRV_STATUS_VF_DISABLED)
8309255736Sdavidch                bxe_vf_handle_flr_event(sc);
8310255736Sdavidch#endif
8311255736Sdavidch
8312255736Sdavidch            if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
8313255736Sdavidch                bxe_pmf_update(sc);
8314255736Sdavidch
8315255736Sdavidch#if 0
8316255736Sdavidch            if (sc->port.pmf &&
8317255736Sdavidch                (val & DRV_STATUS_DCBX_NEGOTIATION_RESULTS) &&
8318255736Sdavidch                (sc->dcbx_enabled > 0))
8319255736Sdavidch                /* start dcbx state machine */
8320255736Sdavidch                bxe_dcbx_set_params(sc, BXE_DCBX_STATE_NEG_RECEIVED);
8321255736Sdavidch#endif
8322255736Sdavidch
8323255736Sdavidch#if 0
8324255736Sdavidch            if (val & DRV_STATUS_AFEX_EVENT_MASK)
8325255736Sdavidch                bxe_handle_afex_cmd(sc, val & DRV_STATUS_AFEX_EVENT_MASK);
8326255736Sdavidch#endif
8327255736Sdavidch
8328255736Sdavidch            if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
8329255736Sdavidch                bxe_handle_eee_event(sc);
8330255736Sdavidch
8331255736Sdavidch            if (sc->link_vars.periodic_flags &
8332255736Sdavidch                ELINK_PERIODIC_FLAGS_LINK_EVENT) {
8333255736Sdavidch                /* sync with link */
8334284335Sdavidcs		bxe_acquire_phy_lock(sc);
8335255736Sdavidch                sc->link_vars.periodic_flags &=
8336255736Sdavidch                    ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
8337284335Sdavidcs		bxe_release_phy_lock(sc);
8338255736Sdavidch                if (IS_MF(sc))
8339255736Sdavidch                    ; // XXX bxe_link_sync_notify(sc);
8340255736Sdavidch                bxe_link_report(sc);
8341255736Sdavidch            }
8342255736Sdavidch
8343255736Sdavidch            /*
8344255736Sdavidch             * Always call it here: bxe_link_report() will
8345255736Sdavidch             * prevent the link indication duplication.
8346255736Sdavidch             */
8347255736Sdavidch            bxe_link_status_update(sc);
8348255736Sdavidch
8349255736Sdavidch        } else if (attn & BXE_MC_ASSERT_BITS) {
8350255736Sdavidch
8351255736Sdavidch            BLOGE(sc, "MC assert!\n");
8352255736Sdavidch            bxe_mc_assert(sc);
8353255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
8354255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
8355255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
8356255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
8357255736Sdavidch            bxe_panic(sc, ("MC assert!\n"));
8358255736Sdavidch
8359255736Sdavidch        } else if (attn & BXE_MCP_ASSERT) {
8360255736Sdavidch
8361255736Sdavidch            BLOGE(sc, "MCP assert!\n");
8362255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8363255736Sdavidch            // XXX bxe_fw_dump(sc);
8364255736Sdavidch
8365255736Sdavidch        } else {
8366255736Sdavidch            BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8367255736Sdavidch        }
8368255736Sdavidch    }
8369255736Sdavidch
8370255736Sdavidch    if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8371255736Sdavidch        BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8372255736Sdavidch        if (attn & BXE_GRC_TIMEOUT) {
8373255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8374255736Sdavidch            BLOGE(sc, "GRC time-out 0x%08x\n", val);
8375255736Sdavidch        }
8376255736Sdavidch        if (attn & BXE_GRC_RSV) {
8377255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8378255736Sdavidch            BLOGE(sc, "GRC reserved 0x%08x\n", val);
8379255736Sdavidch        }
8380255736Sdavidch        REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8381255736Sdavidch    }
8382255736Sdavidch}
8383255736Sdavidch
8384255736Sdavidchstatic void
8385255736Sdavidchbxe_attn_int_deasserted2(struct bxe_softc *sc,
8386255736Sdavidch                         uint32_t         attn)
8387255736Sdavidch{
8388255736Sdavidch    int port = SC_PORT(sc);
8389255736Sdavidch    int reg_offset;
8390255736Sdavidch    uint32_t val0, mask0, val1, mask1;
8391255736Sdavidch    uint32_t val;
8392255736Sdavidch
8393255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8394255736Sdavidch        val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8395255736Sdavidch        BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8396255736Sdavidch        /* CFC error attention */
8397255736Sdavidch        if (val & 0x2) {
8398255736Sdavidch            BLOGE(sc, "FATAL error from CFC\n");
8399255736Sdavidch        }
8400255736Sdavidch    }
8401255736Sdavidch
8402255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8403255736Sdavidch        val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8404255736Sdavidch        BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8405255736Sdavidch        /* RQ_USDMDP_FIFO_OVERFLOW */
8406255736Sdavidch        if (val & 0x18000) {
8407255736Sdavidch            BLOGE(sc, "FATAL error from PXP\n");
8408255736Sdavidch        }
8409255736Sdavidch
8410255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8411255736Sdavidch            val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8412255736Sdavidch            BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8413255736Sdavidch        }
8414255736Sdavidch    }
8415255736Sdavidch
8416255736Sdavidch#define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8417255736Sdavidch#define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8418255736Sdavidch
8419255736Sdavidch    if (attn & AEU_PXP2_HW_INT_BIT) {
8420255736Sdavidch        /*  CQ47854 workaround do not panic on
8421255736Sdavidch         *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8422255736Sdavidch         */
8423255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8424255736Sdavidch            mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8425255736Sdavidch            val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8426255736Sdavidch            mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8427255736Sdavidch            val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8428255736Sdavidch            /*
8429255736Sdavidch             * If the olny PXP2_EOP_ERROR_BIT is set in
8430255736Sdavidch             * STS0 and STS1 - clear it
8431255736Sdavidch             *
8432255736Sdavidch             * probably we lose additional attentions between
8433255736Sdavidch             * STS0 and STS_CLR0, in this case user will not
8434255736Sdavidch             * be notified about them
8435255736Sdavidch             */
8436255736Sdavidch            if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8437255736Sdavidch                !(val1 & mask1))
8438255736Sdavidch                val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8439255736Sdavidch
8440255736Sdavidch            /* print the register, since no one can restore it */
8441255736Sdavidch            BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8442255736Sdavidch
8443255736Sdavidch            /*
8444255736Sdavidch             * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8445255736Sdavidch             * then notify
8446255736Sdavidch             */
8447255736Sdavidch            if (val0 & PXP2_EOP_ERROR_BIT) {
8448255736Sdavidch                BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8449255736Sdavidch
8450255736Sdavidch                /*
8451255736Sdavidch                 * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8452255736Sdavidch                 * set then clear attention from PXP2 block without panic
8453255736Sdavidch                 */
8454255736Sdavidch                if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8455255736Sdavidch                    ((val1 & mask1) == 0))
8456255736Sdavidch                    attn &= ~AEU_PXP2_HW_INT_BIT;
8457255736Sdavidch            }
8458255736Sdavidch        }
8459255736Sdavidch    }
8460255736Sdavidch
8461255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_2) {
8462255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8463255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8464255736Sdavidch
8465255736Sdavidch        val = REG_RD(sc, reg_offset);
8466255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8467255736Sdavidch        REG_WR(sc, reg_offset, val);
8468255736Sdavidch
8469255736Sdavidch        BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8470255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8471255736Sdavidch        bxe_panic(sc, ("HW block attention set2\n"));
8472255736Sdavidch    }
8473255736Sdavidch}
8474255736Sdavidch
8475255736Sdavidchstatic void
8476255736Sdavidchbxe_attn_int_deasserted1(struct bxe_softc *sc,
8477255736Sdavidch                         uint32_t         attn)
8478255736Sdavidch{
8479255736Sdavidch    int port = SC_PORT(sc);
8480255736Sdavidch    int reg_offset;
8481255736Sdavidch    uint32_t val;
8482255736Sdavidch
8483255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8484255736Sdavidch        val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8485255736Sdavidch        BLOGE(sc, "DB hw attention 0x%08x\n", val);
8486255736Sdavidch        /* DORQ discard attention */
8487255736Sdavidch        if (val & 0x2) {
8488255736Sdavidch            BLOGE(sc, "FATAL error from DORQ\n");
8489255736Sdavidch        }
8490255736Sdavidch    }
8491255736Sdavidch
8492255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_1) {
8493255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8494255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8495255736Sdavidch
8496255736Sdavidch        val = REG_RD(sc, reg_offset);
8497255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8498255736Sdavidch        REG_WR(sc, reg_offset, val);
8499255736Sdavidch
8500255736Sdavidch        BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8501255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8502255736Sdavidch        bxe_panic(sc, ("HW block attention set1\n"));
8503255736Sdavidch    }
8504255736Sdavidch}
8505255736Sdavidch
8506255736Sdavidchstatic void
8507255736Sdavidchbxe_attn_int_deasserted0(struct bxe_softc *sc,
8508255736Sdavidch                         uint32_t         attn)
8509255736Sdavidch{
8510255736Sdavidch    int port = SC_PORT(sc);
8511255736Sdavidch    int reg_offset;
8512255736Sdavidch    uint32_t val;
8513255736Sdavidch
8514255736Sdavidch    reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8515255736Sdavidch                          MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8516255736Sdavidch
8517255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8518255736Sdavidch        val = REG_RD(sc, reg_offset);
8519255736Sdavidch        val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8520255736Sdavidch        REG_WR(sc, reg_offset, val);
8521255736Sdavidch
8522255736Sdavidch        BLOGW(sc, "SPIO5 hw attention\n");
8523255736Sdavidch
8524255736Sdavidch        /* Fan failure attention */
8525255736Sdavidch        elink_hw_reset_phy(&sc->link_params);
8526255736Sdavidch        bxe_fan_failure(sc);
8527255736Sdavidch    }
8528255736Sdavidch
8529255736Sdavidch    if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8530284335Sdavidcs	bxe_acquire_phy_lock(sc);
8531255736Sdavidch        elink_handle_module_detect_int(&sc->link_params);
8532284335Sdavidcs	bxe_release_phy_lock(sc);
8533255736Sdavidch    }
8534255736Sdavidch
8535255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_0) {
8536255736Sdavidch        val = REG_RD(sc, reg_offset);
8537255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8538255736Sdavidch        REG_WR(sc, reg_offset, val);
8539255736Sdavidch
8540255736Sdavidch        bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8541255736Sdavidch                       (attn & HW_INTERRUT_ASSERT_SET_0)));
8542255736Sdavidch    }
8543255736Sdavidch}
8544255736Sdavidch
8545255736Sdavidchstatic void
8546255736Sdavidchbxe_attn_int_deasserted(struct bxe_softc *sc,
8547255736Sdavidch                        uint32_t         deasserted)
8548255736Sdavidch{
8549255736Sdavidch    struct attn_route attn;
8550255736Sdavidch    struct attn_route *group_mask;
8551255736Sdavidch    int port = SC_PORT(sc);
8552255736Sdavidch    int index;
8553255736Sdavidch    uint32_t reg_addr;
8554255736Sdavidch    uint32_t val;
8555255736Sdavidch    uint32_t aeu_mask;
8556255736Sdavidch    uint8_t global = FALSE;
8557255736Sdavidch
8558255736Sdavidch    /*
8559255736Sdavidch     * Need to take HW lock because MCP or other port might also
8560255736Sdavidch     * try to handle this event.
8561255736Sdavidch     */
8562255736Sdavidch    bxe_acquire_alr(sc);
8563255736Sdavidch
8564255736Sdavidch    if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8565255736Sdavidch        /* XXX
8566255736Sdavidch         * In case of parity errors don't handle attentions so that
8567255736Sdavidch         * other function would "see" parity errors.
8568255736Sdavidch         */
8569255736Sdavidch        sc->recovery_state = BXE_RECOVERY_INIT;
8570255736Sdavidch        // XXX schedule a recovery task...
8571255736Sdavidch        /* disable HW interrupts */
8572255736Sdavidch        bxe_int_disable(sc);
8573255736Sdavidch        bxe_release_alr(sc);
8574255736Sdavidch        return;
8575255736Sdavidch    }
8576255736Sdavidch
8577255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8578255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8579255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8580255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8581255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
8582255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8583255736Sdavidch    } else {
8584255736Sdavidch        attn.sig[4] = 0;
8585255736Sdavidch    }
8586255736Sdavidch
8587255736Sdavidch    BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8588255736Sdavidch          attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8589255736Sdavidch
8590255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8591255736Sdavidch        if (deasserted & (1 << index)) {
8592255736Sdavidch            group_mask = &sc->attn_group[index];
8593255736Sdavidch
8594255736Sdavidch            BLOGD(sc, DBG_INTR,
8595255736Sdavidch                  "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8596255736Sdavidch                  group_mask->sig[0], group_mask->sig[1],
8597255736Sdavidch                  group_mask->sig[2], group_mask->sig[3],
8598255736Sdavidch                  group_mask->sig[4]);
8599255736Sdavidch
8600255736Sdavidch            bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8601255736Sdavidch            bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8602255736Sdavidch            bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8603255736Sdavidch            bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8604255736Sdavidch            bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8605255736Sdavidch        }
8606255736Sdavidch    }
8607255736Sdavidch
8608255736Sdavidch    bxe_release_alr(sc);
8609255736Sdavidch
8610255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
8611255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 +
8612255736Sdavidch                    COMMAND_REG_ATTN_BITS_CLR);
8613255736Sdavidch    } else {
8614255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8615255736Sdavidch    }
8616255736Sdavidch
8617255736Sdavidch    val = ~deasserted;
8618255736Sdavidch    BLOGD(sc, DBG_INTR,
8619255736Sdavidch          "about to mask 0x%08x at %s addr 0x%08x\n", val,
8620255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8621255736Sdavidch    REG_WR(sc, reg_addr, val);
8622255736Sdavidch
8623255736Sdavidch    if (~sc->attn_state & deasserted) {
8624255736Sdavidch        BLOGE(sc, "IGU error\n");
8625255736Sdavidch    }
8626255736Sdavidch
8627255736Sdavidch    reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8628255736Sdavidch                      MISC_REG_AEU_MASK_ATTN_FUNC_0;
8629255736Sdavidch
8630255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8631255736Sdavidch
8632255736Sdavidch    aeu_mask = REG_RD(sc, reg_addr);
8633255736Sdavidch
8634255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8635255736Sdavidch          aeu_mask, deasserted);
8636255736Sdavidch    aeu_mask |= (deasserted & 0x3ff);
8637255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8638255736Sdavidch
8639255736Sdavidch    REG_WR(sc, reg_addr, aeu_mask);
8640255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8641255736Sdavidch
8642255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8643255736Sdavidch    sc->attn_state &= ~deasserted;
8644255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8645255736Sdavidch}
8646255736Sdavidch
8647255736Sdavidchstatic void
8648255736Sdavidchbxe_attn_int(struct bxe_softc *sc)
8649255736Sdavidch{
8650255736Sdavidch    /* read local copy of bits */
8651255736Sdavidch    uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8652255736Sdavidch    uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8653255736Sdavidch    uint32_t attn_state = sc->attn_state;
8654255736Sdavidch
8655255736Sdavidch    /* look for changed bits */
8656255736Sdavidch    uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8657255736Sdavidch    uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8658255736Sdavidch
8659255736Sdavidch    BLOGD(sc, DBG_INTR,
8660255736Sdavidch          "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8661255736Sdavidch          attn_bits, attn_ack, asserted, deasserted);
8662255736Sdavidch
8663255736Sdavidch    if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8664255736Sdavidch        BLOGE(sc, "BAD attention state\n");
8665255736Sdavidch    }
8666255736Sdavidch
8667255736Sdavidch    /* handle bits that were raised */
8668255736Sdavidch    if (asserted) {
8669255736Sdavidch        bxe_attn_int_asserted(sc, asserted);
8670255736Sdavidch    }
8671255736Sdavidch
8672255736Sdavidch    if (deasserted) {
8673255736Sdavidch        bxe_attn_int_deasserted(sc, deasserted);
8674255736Sdavidch    }
8675255736Sdavidch}
8676255736Sdavidch
8677255736Sdavidchstatic uint16_t
8678255736Sdavidchbxe_update_dsb_idx(struct bxe_softc *sc)
8679255736Sdavidch{
8680255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
8681255736Sdavidch    uint16_t rc = 0;
8682255736Sdavidch
8683255736Sdavidch    mb(); /* status block is written to by the chip */
8684255736Sdavidch
8685255736Sdavidch    if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8686255736Sdavidch        sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8687255736Sdavidch        rc |= BXE_DEF_SB_ATT_IDX;
8688255736Sdavidch    }
8689255736Sdavidch
8690255736Sdavidch    if (sc->def_idx != def_sb->sp_sb.running_index) {
8691255736Sdavidch        sc->def_idx = def_sb->sp_sb.running_index;
8692255736Sdavidch        rc |= BXE_DEF_SB_IDX;
8693255736Sdavidch    }
8694255736Sdavidch
8695255736Sdavidch    mb();
8696255736Sdavidch
8697255736Sdavidch    return (rc);
8698255736Sdavidch}
8699255736Sdavidch
8700255736Sdavidchstatic inline struct ecore_queue_sp_obj *
8701255736Sdavidchbxe_cid_to_q_obj(struct bxe_softc *sc,
8702255736Sdavidch                 uint32_t         cid)
8703255736Sdavidch{
8704255736Sdavidch    BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8705255736Sdavidch    return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8706255736Sdavidch}
8707255736Sdavidch
8708255736Sdavidchstatic void
8709255736Sdavidchbxe_handle_mcast_eqe(struct bxe_softc *sc)
8710255736Sdavidch{
8711255736Sdavidch    struct ecore_mcast_ramrod_params rparam;
8712255736Sdavidch    int rc;
8713255736Sdavidch
8714255736Sdavidch    memset(&rparam, 0, sizeof(rparam));
8715255736Sdavidch
8716255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
8717255736Sdavidch
8718255736Sdavidch    BXE_MCAST_LOCK(sc);
8719255736Sdavidch
8720255736Sdavidch    /* clear pending state for the last command */
8721255736Sdavidch    sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8722255736Sdavidch
8723255736Sdavidch    /* if there are pending mcast commands - send them */
8724255736Sdavidch    if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8725255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8726255736Sdavidch        if (rc < 0) {
8727255736Sdavidch            BLOGD(sc, DBG_SP,
8728255736Sdavidch                  "ERROR: Failed to send pending mcast commands (%d)\n",
8729255736Sdavidch                  rc);
8730255736Sdavidch        }
8731255736Sdavidch    }
8732255736Sdavidch
8733255736Sdavidch    BXE_MCAST_UNLOCK(sc);
8734255736Sdavidch}
8735255736Sdavidch
8736255736Sdavidchstatic void
8737255736Sdavidchbxe_handle_classification_eqe(struct bxe_softc      *sc,
8738255736Sdavidch                              union event_ring_elem *elem)
8739255736Sdavidch{
8740255736Sdavidch    unsigned long ramrod_flags = 0;
8741255736Sdavidch    int rc = 0;
8742255736Sdavidch    uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8743255736Sdavidch    struct ecore_vlan_mac_obj *vlan_mac_obj;
8744255736Sdavidch
8745255736Sdavidch    /* always push next commands out, don't wait here */
8746255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
8747255736Sdavidch
8748255736Sdavidch    switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8749255736Sdavidch    case ECORE_FILTER_MAC_PENDING:
8750255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8751255736Sdavidch        vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8752255736Sdavidch        break;
8753255736Sdavidch
8754255736Sdavidch    case ECORE_FILTER_MCAST_PENDING:
8755255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8756255736Sdavidch        /*
8757255736Sdavidch         * This is only relevant for 57710 where multicast MACs are
8758255736Sdavidch         * configured as unicast MACs using the same ramrod.
8759255736Sdavidch         */
8760255736Sdavidch        bxe_handle_mcast_eqe(sc);
8761255736Sdavidch        return;
8762255736Sdavidch
8763255736Sdavidch    default:
8764255736Sdavidch        BLOGE(sc, "Unsupported classification command: %d\n",
8765255736Sdavidch              elem->message.data.eth_event.echo);
8766255736Sdavidch        return;
8767255736Sdavidch    }
8768255736Sdavidch
8769255736Sdavidch    rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8770255736Sdavidch
8771255736Sdavidch    if (rc < 0) {
8772255736Sdavidch        BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8773255736Sdavidch    } else if (rc > 0) {
8774255736Sdavidch        BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8775255736Sdavidch    }
8776255736Sdavidch}
8777255736Sdavidch
8778255736Sdavidchstatic void
8779255736Sdavidchbxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8780255736Sdavidch                       union event_ring_elem *elem)
8781255736Sdavidch{
8782255736Sdavidch    bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8783255736Sdavidch
8784255736Sdavidch    /* send rx_mode command again if was requested */
8785255736Sdavidch    if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8786255736Sdavidch                               &sc->sp_state)) {
8787255736Sdavidch        bxe_set_storm_rx_mode(sc);
8788255736Sdavidch    }
8789255736Sdavidch#if 0
8790255736Sdavidch    else if (bxe_test_and_clear_bit(ECORE_FILTER_ISCSI_ETH_START_SCHED,
8791255736Sdavidch                                    &sc->sp_state)) {
8792255736Sdavidch        bxe_set_iscsi_eth_rx_mode(sc, TRUE);
8793255736Sdavidch    }
8794255736Sdavidch    else if (bxe_test_and_clear_bit(ECORE_FILTER_ISCSI_ETH_STOP_SCHED,
8795255736Sdavidch                                    &sc->sp_state)) {
8796255736Sdavidch        bxe_set_iscsi_eth_rx_mode(sc, FALSE);
8797255736Sdavidch    }
8798255736Sdavidch#endif
8799255736Sdavidch}
8800255736Sdavidch
8801255736Sdavidchstatic void
8802255736Sdavidchbxe_update_eq_prod(struct bxe_softc *sc,
8803255736Sdavidch                   uint16_t         prod)
8804255736Sdavidch{
8805255736Sdavidch    storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8806255736Sdavidch    wmb(); /* keep prod updates ordered */
8807255736Sdavidch}
8808255736Sdavidch
8809255736Sdavidchstatic void
8810255736Sdavidchbxe_eq_int(struct bxe_softc *sc)
8811255736Sdavidch{
8812255736Sdavidch    uint16_t hw_cons, sw_cons, sw_prod;
8813255736Sdavidch    union event_ring_elem *elem;
8814255736Sdavidch    uint8_t echo;
8815255736Sdavidch    uint32_t cid;
8816255736Sdavidch    uint8_t opcode;
8817255736Sdavidch    int spqe_cnt = 0;
8818255736Sdavidch    struct ecore_queue_sp_obj *q_obj;
8819255736Sdavidch    struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8820255736Sdavidch    struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8821255736Sdavidch
8822255736Sdavidch    hw_cons = le16toh(*sc->eq_cons_sb);
8823255736Sdavidch
8824255736Sdavidch    /*
8825255736Sdavidch     * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8826255736Sdavidch     * when we get to the next-page we need to adjust so the loop
8827255736Sdavidch     * condition below will be met. The next element is the size of a
8828255736Sdavidch     * regular element and hence incrementing by 1
8829255736Sdavidch     */
8830255736Sdavidch    if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8831255736Sdavidch        hw_cons++;
8832255736Sdavidch    }
8833255736Sdavidch
8834255736Sdavidch    /*
8835255736Sdavidch     * This function may never run in parallel with itself for a
8836255736Sdavidch     * specific sc and no need for a read memory barrier here.
8837255736Sdavidch     */
8838255736Sdavidch    sw_cons = sc->eq_cons;
8839255736Sdavidch    sw_prod = sc->eq_prod;
8840255736Sdavidch
8841255736Sdavidch    BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8842255736Sdavidch          hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8843255736Sdavidch
8844255736Sdavidch    for (;
8845255736Sdavidch         sw_cons != hw_cons;
8846255736Sdavidch         sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8847255736Sdavidch
8848255736Sdavidch        elem = &sc->eq[EQ_DESC(sw_cons)];
8849255736Sdavidch
8850255736Sdavidch#if 0
8851255736Sdavidch        int rc;
8852255736Sdavidch        rc = bxe_iov_eq_sp_event(sc, elem);
8853255736Sdavidch        if (!rc) {
8854255736Sdavidch            BLOGE(sc, "bxe_iov_eq_sp_event returned %d\n", rc);
8855255736Sdavidch            goto next_spqe;
8856255736Sdavidch        }
8857255736Sdavidch#endif
8858255736Sdavidch
8859255736Sdavidch        /* elem CID originates from FW, actually LE */
8860255736Sdavidch        cid = SW_CID(elem->message.data.cfc_del_event.cid);
8861255736Sdavidch        opcode = elem->message.opcode;
8862255736Sdavidch
8863255736Sdavidch        /* handle eq element */
8864255736Sdavidch        switch (opcode) {
8865255736Sdavidch#if 0
8866255736Sdavidch        case EVENT_RING_OPCODE_VF_PF_CHANNEL:
8867255736Sdavidch            BLOGD(sc, DBG_SP, "vf/pf channel element on eq\n");
8868255736Sdavidch            bxe_vf_mbx(sc, &elem->message.data.vf_pf_event);
8869255736Sdavidch            continue;
8870255736Sdavidch#endif
8871255736Sdavidch
8872255736Sdavidch        case EVENT_RING_OPCODE_STAT_QUERY:
8873255736Sdavidch            BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8874255736Sdavidch                  sc->stats_comp++);
8875255736Sdavidch            /* nothing to do with stats comp */
8876255736Sdavidch            goto next_spqe;
8877255736Sdavidch
8878255736Sdavidch        case EVENT_RING_OPCODE_CFC_DEL:
8879255736Sdavidch            /* handle according to cid range */
8880255736Sdavidch            /* we may want to verify here that the sc state is HALTING */
8881255736Sdavidch            BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8882255736Sdavidch            q_obj = bxe_cid_to_q_obj(sc, cid);
8883255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8884255736Sdavidch                break;
8885255736Sdavidch            }
8886255736Sdavidch            goto next_spqe;
8887255736Sdavidch
8888255736Sdavidch        case EVENT_RING_OPCODE_STOP_TRAFFIC:
8889255736Sdavidch            BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8890255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8891255736Sdavidch                break;
8892255736Sdavidch            }
8893255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8894255736Sdavidch            goto next_spqe;
8895255736Sdavidch
8896255736Sdavidch        case EVENT_RING_OPCODE_START_TRAFFIC:
8897255736Sdavidch            BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8898255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8899255736Sdavidch                break;
8900255736Sdavidch            }
8901255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8902255736Sdavidch            goto next_spqe;
8903255736Sdavidch
8904255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8905255736Sdavidch            echo = elem->message.data.function_update_event.echo;
8906255736Sdavidch            if (echo == SWITCH_UPDATE) {
8907255736Sdavidch                BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8908255736Sdavidch                if (f_obj->complete_cmd(sc, f_obj,
8909255736Sdavidch                                        ECORE_F_CMD_SWITCH_UPDATE)) {
8910255736Sdavidch                    break;
8911255736Sdavidch                }
8912255736Sdavidch            }
8913255736Sdavidch            else {
8914255736Sdavidch                BLOGD(sc, DBG_SP,
8915255736Sdavidch                      "AFEX: ramrod completed FUNCTION_UPDATE\n");
8916255736Sdavidch#if 0
8917255736Sdavidch                f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_AFEX_UPDATE);
8918255736Sdavidch                /*
8919255736Sdavidch                 * We will perform the queues update from the sp_core_task as
8920255736Sdavidch                 * all queue SP operations should run with CORE_LOCK.
8921255736Sdavidch                 */
8922255736Sdavidch                bxe_set_bit(BXE_SP_CORE_AFEX_F_UPDATE, &sc->sp_core_state);
8923255736Sdavidch                taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8924255736Sdavidch#endif
8925255736Sdavidch            }
8926255736Sdavidch            goto next_spqe;
8927255736Sdavidch
8928255736Sdavidch#if 0
8929255736Sdavidch        case EVENT_RING_OPCODE_AFEX_VIF_LISTS:
8930255736Sdavidch            f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_AFEX_VIFLISTS);
8931255736Sdavidch            bxe_after_afex_vif_lists(sc, elem);
8932255736Sdavidch            goto next_spqe;
8933255736Sdavidch#endif
8934255736Sdavidch
8935255736Sdavidch        case EVENT_RING_OPCODE_FORWARD_SETUP:
8936255736Sdavidch            q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8937255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj,
8938255736Sdavidch                                    ECORE_Q_CMD_SETUP_TX_ONLY)) {
8939255736Sdavidch                break;
8940255736Sdavidch            }
8941255736Sdavidch            goto next_spqe;
8942255736Sdavidch
8943255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_START:
8944255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8945255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8946255736Sdavidch                break;
8947255736Sdavidch            }
8948255736Sdavidch            goto next_spqe;
8949255736Sdavidch
8950255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_STOP:
8951255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8952255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8953255736Sdavidch                break;
8954255736Sdavidch            }
8955255736Sdavidch            goto next_spqe;
8956255736Sdavidch        }
8957255736Sdavidch
8958255736Sdavidch        switch (opcode | sc->state) {
8959255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8960255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8961255736Sdavidch            cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8962255736Sdavidch            BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8963255736Sdavidch            rss_raw->clear_pending(rss_raw);
8964255736Sdavidch            break;
8965255736Sdavidch
8966255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8967255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8968255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8969255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8970255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8971255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8972255736Sdavidch            BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8973255736Sdavidch            bxe_handle_classification_eqe(sc, elem);
8974255736Sdavidch            break;
8975255736Sdavidch
8976255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8977255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8978255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8979255736Sdavidch            BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8980255736Sdavidch            bxe_handle_mcast_eqe(sc);
8981255736Sdavidch            break;
8982255736Sdavidch
8983255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8984255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8985255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8986255736Sdavidch            BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8987255736Sdavidch            bxe_handle_rx_mode_eqe(sc, elem);
8988255736Sdavidch            break;
8989255736Sdavidch
8990255736Sdavidch        default:
8991255736Sdavidch            /* unknown event log error and continue */
8992255736Sdavidch            BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8993255736Sdavidch                  elem->message.opcode, sc->state);
8994255736Sdavidch        }
8995255736Sdavidch
8996255736Sdavidchnext_spqe:
8997255736Sdavidch        spqe_cnt++;
8998255736Sdavidch    } /* for */
8999255736Sdavidch
9000255736Sdavidch    mb();
9001255736Sdavidch    atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
9002255736Sdavidch
9003255736Sdavidch    sc->eq_cons = sw_cons;
9004255736Sdavidch    sc->eq_prod = sw_prod;
9005255736Sdavidch
9006255736Sdavidch    /* make sure that above mem writes were issued towards the memory */
9007255736Sdavidch    wmb();
9008255736Sdavidch
9009255736Sdavidch    /* update producer */
9010255736Sdavidch    bxe_update_eq_prod(sc, sc->eq_prod);
9011255736Sdavidch}
9012255736Sdavidch
9013255736Sdavidchstatic void
9014255736Sdavidchbxe_handle_sp_tq(void *context,
9015255736Sdavidch                 int  pending)
9016255736Sdavidch{
9017255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
9018255736Sdavidch    uint16_t status;
9019255736Sdavidch
9020255736Sdavidch    BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
9021255736Sdavidch
9022255736Sdavidch    /* what work needs to be performed? */
9023255736Sdavidch    status = bxe_update_dsb_idx(sc);
9024255736Sdavidch
9025255736Sdavidch    BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
9026255736Sdavidch
9027255736Sdavidch    /* HW attentions */
9028255736Sdavidch    if (status & BXE_DEF_SB_ATT_IDX) {
9029255736Sdavidch        BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
9030255736Sdavidch        bxe_attn_int(sc);
9031255736Sdavidch        status &= ~BXE_DEF_SB_ATT_IDX;
9032255736Sdavidch    }
9033255736Sdavidch
9034255736Sdavidch    /* SP events: STAT_QUERY and others */
9035255736Sdavidch    if (status & BXE_DEF_SB_IDX) {
9036255736Sdavidch        /* handle EQ completions */
9037255736Sdavidch        BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
9038255736Sdavidch        bxe_eq_int(sc);
9039255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
9040255736Sdavidch                   le16toh(sc->def_idx), IGU_INT_NOP, 1);
9041255736Sdavidch        status &= ~BXE_DEF_SB_IDX;
9042255736Sdavidch    }
9043255736Sdavidch
9044255736Sdavidch    /* if status is non zero then something went wrong */
9045255736Sdavidch    if (__predict_false(status)) {
9046255736Sdavidch        BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
9047255736Sdavidch    }
9048255736Sdavidch
9049255736Sdavidch    /* ack status block only if something was actually handled */
9050255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
9051255736Sdavidch               le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
9052255736Sdavidch
9053255736Sdavidch    /*
9054255736Sdavidch     * Must be called after the EQ processing (since eq leads to sriov
9055255736Sdavidch     * ramrod completion flows).
9056255736Sdavidch     * This flow may have been scheduled by the arrival of a ramrod
9057255736Sdavidch     * completion, or by the sriov code rescheduling itself.
9058255736Sdavidch     */
9059255736Sdavidch    // XXX bxe_iov_sp_task(sc);
9060255736Sdavidch
9061255736Sdavidch#if 0
9062255736Sdavidch    /* AFEX - poll to check if VIFSET_ACK should be sent to MFW */
9063255736Sdavidch    if (bxe_test_and_clear_bit(ECORE_AFEX_PENDING_VIFSET_MCP_ACK,
9064255736Sdavidch                               &sc->sp_state)) {
9065255736Sdavidch        bxe_link_report(sc);
9066255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_AFEX_VIFSET_ACK, 0);
9067255736Sdavidch    }
9068255736Sdavidch#endif
9069255736Sdavidch}
9070255736Sdavidch
9071255736Sdavidchstatic void
9072255736Sdavidchbxe_handle_fp_tq(void *context,
9073255736Sdavidch                 int  pending)
9074255736Sdavidch{
9075255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
9076255736Sdavidch    struct bxe_softc *sc = fp->sc;
9077255736Sdavidch    uint8_t more_tx = FALSE;
9078255736Sdavidch    uint8_t more_rx = FALSE;
9079255736Sdavidch
9080255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
9081255736Sdavidch
9082255736Sdavidch    /* XXX
9083255736Sdavidch     * IFF_DRV_RUNNING state can't be checked here since we process
9084255736Sdavidch     * slowpath events on a client queue during setup. Instead
9085255736Sdavidch     * we need to add a "process/continue" flag here that the driver
9086255736Sdavidch     * can use to tell the task here not to do anything.
9087255736Sdavidch     */
9088255736Sdavidch#if 0
9089266979Smarcel    if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
9090255736Sdavidch        return;
9091255736Sdavidch    }
9092255736Sdavidch#endif
9093255736Sdavidch
9094255736Sdavidch    /* update the fastpath index */
9095255736Sdavidch    bxe_update_fp_sb_idx(fp);
9096255736Sdavidch
9097255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
9098255736Sdavidch    /* fp->txdata[cos] */
9099255736Sdavidch    if (bxe_has_tx_work(fp)) {
9100255736Sdavidch        BXE_FP_TX_LOCK(fp);
9101255736Sdavidch        more_tx = bxe_txeof(sc, fp);
9102255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
9103255736Sdavidch    }
9104255736Sdavidch
9105255736Sdavidch    if (bxe_has_rx_work(fp)) {
9106255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
9107255736Sdavidch    }
9108255736Sdavidch
9109255736Sdavidch    if (more_rx /*|| more_tx*/) {
9110255736Sdavidch        /* still more work to do */
9111255736Sdavidch        taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
9112255736Sdavidch        return;
9113255736Sdavidch    }
9114255736Sdavidch
9115255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
9116255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
9117255736Sdavidch}
9118255736Sdavidch
9119255736Sdavidchstatic void
9120255736Sdavidchbxe_task_fp(struct bxe_fastpath *fp)
9121255736Sdavidch{
9122255736Sdavidch    struct bxe_softc *sc = fp->sc;
9123255736Sdavidch    uint8_t more_tx = FALSE;
9124255736Sdavidch    uint8_t more_rx = FALSE;
9125255736Sdavidch
9126255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
9127255736Sdavidch
9128255736Sdavidch    /* update the fastpath index */
9129255736Sdavidch    bxe_update_fp_sb_idx(fp);
9130255736Sdavidch
9131255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
9132255736Sdavidch    /* fp->txdata[cos] */
9133255736Sdavidch    if (bxe_has_tx_work(fp)) {
9134255736Sdavidch        BXE_FP_TX_LOCK(fp);
9135255736Sdavidch        more_tx = bxe_txeof(sc, fp);
9136255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
9137255736Sdavidch    }
9138255736Sdavidch
9139255736Sdavidch    if (bxe_has_rx_work(fp)) {
9140255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
9141255736Sdavidch    }
9142255736Sdavidch
9143255736Sdavidch    if (more_rx /*|| more_tx*/) {
9144255736Sdavidch        /* still more work to do, bail out if this ISR and process later */
9145255736Sdavidch        taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
9146255736Sdavidch        return;
9147255736Sdavidch    }
9148255736Sdavidch
9149255736Sdavidch    /*
9150255736Sdavidch     * Here we write the fastpath index taken before doing any tx or rx work.
9151255736Sdavidch     * It is very well possible other hw events occurred up to this point and
9152255736Sdavidch     * they were actually processed accordingly above. Since we're going to
9153255736Sdavidch     * write an older fastpath index, an interrupt is coming which we might
9154255736Sdavidch     * not do any work in.
9155255736Sdavidch     */
9156255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
9157255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
9158255736Sdavidch}
9159255736Sdavidch
9160255736Sdavidch/*
9161255736Sdavidch * Legacy interrupt entry point.
9162255736Sdavidch *
9163255736Sdavidch * Verifies that the controller generated the interrupt and
9164255736Sdavidch * then calls a separate routine to handle the various
9165255736Sdavidch * interrupt causes: link, RX, and TX.
9166255736Sdavidch */
9167255736Sdavidchstatic void
9168255736Sdavidchbxe_intr_legacy(void *xsc)
9169255736Sdavidch{
9170255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
9171255736Sdavidch    struct bxe_fastpath *fp;
9172255736Sdavidch    uint16_t status, mask;
9173255736Sdavidch    int i;
9174255736Sdavidch
9175255736Sdavidch    BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
9176255736Sdavidch
9177255736Sdavidch#if 0
9178255736Sdavidch    /* Don't handle any interrupts if we're not ready. */
9179255736Sdavidch    if (__predict_false(sc->intr_sem != 0)) {
9180255736Sdavidch        return;
9181255736Sdavidch    }
9182255736Sdavidch#endif
9183255736Sdavidch
9184255736Sdavidch    /*
9185255736Sdavidch     * 0 for ustorm, 1 for cstorm
9186255736Sdavidch     * the bits returned from ack_int() are 0-15
9187255736Sdavidch     * bit 0 = attention status block
9188255736Sdavidch     * bit 1 = fast path status block
9189255736Sdavidch     * a mask of 0x2 or more = tx/rx event
9190255736Sdavidch     * a mask of 1 = slow path event
9191255736Sdavidch     */
9192255736Sdavidch
9193255736Sdavidch    status = bxe_ack_int(sc);
9194255736Sdavidch
9195255736Sdavidch    /* the interrupt is not for us */
9196255736Sdavidch    if (__predict_false(status == 0)) {
9197255736Sdavidch        BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
9198255736Sdavidch        return;
9199255736Sdavidch    }
9200255736Sdavidch
9201255736Sdavidch    BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
9202255736Sdavidch
9203255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
9204255736Sdavidch        fp = &sc->fp[i];
9205255736Sdavidch        mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
9206255736Sdavidch        if (status & mask) {
9207255736Sdavidch            /* acknowledge and disable further fastpath interrupts */
9208255736Sdavidch            bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9209255736Sdavidch            bxe_task_fp(fp);
9210255736Sdavidch            status &= ~mask;
9211255736Sdavidch        }
9212255736Sdavidch    }
9213255736Sdavidch
9214255736Sdavidch#if 0
9215255736Sdavidch    if (CNIC_SUPPORT(sc)) {
9216255736Sdavidch        mask = 0x2;
9217255736Sdavidch        if (status & (mask | 0x1)) {
9218255736Sdavidch            ...
9219255736Sdavidch            status &= ~mask;
9220255736Sdavidch        }
9221255736Sdavidch    }
9222255736Sdavidch#endif
9223255736Sdavidch
9224255736Sdavidch    if (__predict_false(status & 0x1)) {
9225255736Sdavidch        /* acknowledge and disable further slowpath interrupts */
9226255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9227255736Sdavidch
9228255736Sdavidch        /* schedule slowpath handler */
9229255736Sdavidch        taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
9230255736Sdavidch
9231255736Sdavidch        status &= ~0x1;
9232255736Sdavidch    }
9233255736Sdavidch
9234255736Sdavidch    if (__predict_false(status)) {
9235255736Sdavidch        BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
9236255736Sdavidch    }
9237255736Sdavidch}
9238255736Sdavidch
9239255736Sdavidch/* slowpath interrupt entry point */
9240255736Sdavidchstatic void
9241255736Sdavidchbxe_intr_sp(void *xsc)
9242255736Sdavidch{
9243255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
9244255736Sdavidch
9245255736Sdavidch    BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
9246255736Sdavidch
9247255736Sdavidch    /* acknowledge and disable further slowpath interrupts */
9248255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9249255736Sdavidch
9250255736Sdavidch    /* schedule slowpath handler */
9251255736Sdavidch    taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
9252255736Sdavidch}
9253255736Sdavidch
9254255736Sdavidch/* fastpath interrupt entry point */
9255255736Sdavidchstatic void
9256255736Sdavidchbxe_intr_fp(void *xfp)
9257255736Sdavidch{
9258255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
9259255736Sdavidch    struct bxe_softc *sc = fp->sc;
9260255736Sdavidch
9261255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
9262255736Sdavidch
9263255736Sdavidch    BLOGD(sc, DBG_INTR,
9264255736Sdavidch          "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
9265255736Sdavidch          curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
9266255736Sdavidch
9267255736Sdavidch#if 0
9268255736Sdavidch    /* Don't handle any interrupts if we're not ready. */
9269255736Sdavidch    if (__predict_false(sc->intr_sem != 0)) {
9270255736Sdavidch        return;
9271255736Sdavidch    }
9272255736Sdavidch#endif
9273255736Sdavidch
9274255736Sdavidch    /* acknowledge and disable further fastpath interrupts */
9275255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9276255736Sdavidch
9277255736Sdavidch    bxe_task_fp(fp);
9278255736Sdavidch}
9279255736Sdavidch
9280255736Sdavidch/* Release all interrupts allocated by the driver. */
9281255736Sdavidchstatic void
9282255736Sdavidchbxe_interrupt_free(struct bxe_softc *sc)
9283255736Sdavidch{
9284255736Sdavidch    int i;
9285255736Sdavidch
9286255736Sdavidch    switch (sc->interrupt_mode) {
9287255736Sdavidch    case INTR_MODE_INTX:
9288255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
9289255736Sdavidch        if (sc->intr[0].resource != NULL) {
9290255736Sdavidch            bus_release_resource(sc->dev,
9291255736Sdavidch                                 SYS_RES_IRQ,
9292255736Sdavidch                                 sc->intr[0].rid,
9293255736Sdavidch                                 sc->intr[0].resource);
9294255736Sdavidch        }
9295255736Sdavidch        break;
9296255736Sdavidch    case INTR_MODE_MSI:
9297255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
9298255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
9299255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
9300255736Sdavidch                bus_release_resource(sc->dev,
9301255736Sdavidch                                     SYS_RES_IRQ,
9302255736Sdavidch                                     sc->intr[i].rid,
9303255736Sdavidch                                     sc->intr[i].resource);
9304255736Sdavidch            }
9305255736Sdavidch        }
9306255736Sdavidch        pci_release_msi(sc->dev);
9307255736Sdavidch        break;
9308255736Sdavidch    case INTR_MODE_MSIX:
9309255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
9310255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
9311255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
9312255736Sdavidch                bus_release_resource(sc->dev,
9313255736Sdavidch                                     SYS_RES_IRQ,
9314255736Sdavidch                                     sc->intr[i].rid,
9315255736Sdavidch                                     sc->intr[i].resource);
9316255736Sdavidch            }
9317255736Sdavidch        }
9318255736Sdavidch        pci_release_msi(sc->dev);
9319255736Sdavidch        break;
9320255736Sdavidch    default:
9321255736Sdavidch        /* nothing to do as initial allocation failed */
9322255736Sdavidch        break;
9323255736Sdavidch    }
9324255736Sdavidch}
9325255736Sdavidch
9326255736Sdavidch/*
9327255736Sdavidch * This function determines and allocates the appropriate
9328255736Sdavidch * interrupt based on system capabilites and user request.
9329255736Sdavidch *
9330255736Sdavidch * The user may force a particular interrupt mode, specify
9331255736Sdavidch * the number of receive queues, specify the method for
9332255736Sdavidch * distribuitng received frames to receive queues, or use
9333255736Sdavidch * the default settings which will automatically select the
9334255736Sdavidch * best supported combination.  In addition, the OS may or
9335255736Sdavidch * may not support certain combinations of these settings.
9336255736Sdavidch * This routine attempts to reconcile the settings requested
9337255736Sdavidch * by the user with the capabilites available from the system
9338255736Sdavidch * to select the optimal combination of features.
9339255736Sdavidch *
9340255736Sdavidch * Returns:
9341255736Sdavidch *   0 = Success, !0 = Failure.
9342255736Sdavidch */
9343255736Sdavidchstatic int
9344255736Sdavidchbxe_interrupt_alloc(struct bxe_softc *sc)
9345255736Sdavidch{
9346255736Sdavidch    int msix_count = 0;
9347255736Sdavidch    int msi_count = 0;
9348255736Sdavidch    int num_requested = 0;
9349255736Sdavidch    int num_allocated = 0;
9350255736Sdavidch    int rid, i, j;
9351255736Sdavidch    int rc;
9352255736Sdavidch
9353255736Sdavidch    /* get the number of available MSI/MSI-X interrupts from the OS */
9354255736Sdavidch    if (sc->interrupt_mode > 0) {
9355255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
9356255736Sdavidch            msix_count = pci_msix_count(sc->dev);
9357255736Sdavidch        }
9358255736Sdavidch
9359255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
9360255736Sdavidch            msi_count = pci_msi_count(sc->dev);
9361255736Sdavidch        }
9362255736Sdavidch
9363255736Sdavidch        BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
9364255736Sdavidch              msi_count, msix_count);
9365255736Sdavidch    }
9366255736Sdavidch
9367255736Sdavidch    do { /* try allocating MSI-X interrupt resources (at least 2) */
9368255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
9369255736Sdavidch            break;
9370255736Sdavidch        }
9371255736Sdavidch
9372255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
9373255736Sdavidch            (msix_count < 2)) {
9374255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9375255736Sdavidch            break;
9376255736Sdavidch        }
9377255736Sdavidch
9378255736Sdavidch        /* ask for the necessary number of MSI-X vectors */
9379255736Sdavidch        num_requested = min((sc->num_queues + 1), msix_count);
9380255736Sdavidch
9381255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
9382255736Sdavidch
9383255736Sdavidch        num_allocated = num_requested;
9384255736Sdavidch        if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
9385255736Sdavidch            BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
9386255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9387255736Sdavidch            break;
9388255736Sdavidch        }
9389255736Sdavidch
9390255736Sdavidch        if (num_allocated < 2) { /* possible? */
9391255736Sdavidch            BLOGE(sc, "MSI-X allocation less than 2!\n");
9392255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9393255736Sdavidch            pci_release_msi(sc->dev);
9394255736Sdavidch            break;
9395255736Sdavidch        }
9396255736Sdavidch
9397255736Sdavidch        BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
9398255736Sdavidch              num_requested, num_allocated);
9399255736Sdavidch
9400255736Sdavidch        /* best effort so use the number of vectors allocated to us */
9401255736Sdavidch        sc->intr_count = num_allocated;
9402255736Sdavidch        sc->num_queues = num_allocated - 1;
9403255736Sdavidch
9404255736Sdavidch        rid = 1; /* initial resource identifier */
9405255736Sdavidch
9406255736Sdavidch        /* allocate the MSI-X vectors */
9407255736Sdavidch        for (i = 0; i < num_allocated; i++) {
9408255736Sdavidch            sc->intr[i].rid = (rid + i);
9409255736Sdavidch
9410255736Sdavidch            if ((sc->intr[i].resource =
9411255736Sdavidch                 bus_alloc_resource_any(sc->dev,
9412255736Sdavidch                                        SYS_RES_IRQ,
9413255736Sdavidch                                        &sc->intr[i].rid,
9414255736Sdavidch                                        RF_ACTIVE)) == NULL) {
9415255736Sdavidch                BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
9416255736Sdavidch                      i, (rid + i));
9417255736Sdavidch
9418255736Sdavidch                for (j = (i - 1); j >= 0; j--) {
9419255736Sdavidch                    bus_release_resource(sc->dev,
9420255736Sdavidch                                         SYS_RES_IRQ,
9421255736Sdavidch                                         sc->intr[j].rid,
9422255736Sdavidch                                         sc->intr[j].resource);
9423255736Sdavidch                }
9424255736Sdavidch
9425255736Sdavidch                sc->intr_count = 0;
9426255736Sdavidch                sc->num_queues = 0;
9427255736Sdavidch                sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9428255736Sdavidch                pci_release_msi(sc->dev);
9429255736Sdavidch                break;
9430255736Sdavidch            }
9431255736Sdavidch
9432255736Sdavidch            BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9433255736Sdavidch        }
9434255736Sdavidch    } while (0);
9435255736Sdavidch
9436255736Sdavidch    do { /* try allocating MSI vector resources (at least 2) */
9437255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSI) {
9438255736Sdavidch            break;
9439255736Sdavidch        }
9440255736Sdavidch
9441255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9442262999Sedavis            (msi_count < 1)) {
9443255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9444255736Sdavidch            break;
9445255736Sdavidch        }
9446255736Sdavidch
9447262999Sedavis        /* ask for a single MSI vector */
9448262999Sedavis        num_requested = 1;
9449255736Sdavidch
9450255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9451255736Sdavidch
9452255736Sdavidch        num_allocated = num_requested;
9453255736Sdavidch        if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9454255736Sdavidch            BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9455255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9456255736Sdavidch            break;
9457255736Sdavidch        }
9458255736Sdavidch
9459262999Sedavis        if (num_allocated != 1) { /* possible? */
9460262999Sedavis            BLOGE(sc, "MSI allocation is not 1!\n");
9461255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9462255736Sdavidch            pci_release_msi(sc->dev);
9463255736Sdavidch            break;
9464255736Sdavidch        }
9465255736Sdavidch
9466255736Sdavidch        BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9467255736Sdavidch              num_requested, num_allocated);
9468255736Sdavidch
9469255736Sdavidch        /* best effort so use the number of vectors allocated to us */
9470255736Sdavidch        sc->intr_count = num_allocated;
9471262999Sedavis        sc->num_queues = num_allocated;
9472255736Sdavidch
9473255736Sdavidch        rid = 1; /* initial resource identifier */
9474255736Sdavidch
9475262999Sedavis        sc->intr[0].rid = rid;
9476255736Sdavidch
9477262999Sedavis        if ((sc->intr[0].resource =
9478262999Sedavis             bus_alloc_resource_any(sc->dev,
9479262999Sedavis                                    SYS_RES_IRQ,
9480262999Sedavis                                    &sc->intr[0].rid,
9481262999Sedavis                                    RF_ACTIVE)) == NULL) {
9482262999Sedavis            BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9483262999Sedavis            sc->intr_count = 0;
9484262999Sedavis            sc->num_queues = 0;
9485262999Sedavis            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9486262999Sedavis            pci_release_msi(sc->dev);
9487262999Sedavis            break;
9488262999Sedavis        }
9489255736Sdavidch
9490262999Sedavis        BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9491255736Sdavidch    } while (0);
9492255736Sdavidch
9493255736Sdavidch    do { /* try allocating INTx vector resources */
9494255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_INTX) {
9495255736Sdavidch            break;
9496255736Sdavidch        }
9497255736Sdavidch
9498255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9499255736Sdavidch
9500255736Sdavidch        /* only one vector for INTx */
9501255736Sdavidch        sc->intr_count = 1;
9502255736Sdavidch        sc->num_queues = 1;
9503255736Sdavidch
9504255736Sdavidch        rid = 0; /* initial resource identifier */
9505255736Sdavidch
9506255736Sdavidch        sc->intr[0].rid = rid;
9507255736Sdavidch
9508255736Sdavidch        if ((sc->intr[0].resource =
9509255736Sdavidch             bus_alloc_resource_any(sc->dev,
9510255736Sdavidch                                    SYS_RES_IRQ,
9511255736Sdavidch                                    &sc->intr[0].rid,
9512255736Sdavidch                                    (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9513255736Sdavidch            BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9514255736Sdavidch            sc->intr_count = 0;
9515255736Sdavidch            sc->num_queues = 0;
9516255736Sdavidch            sc->interrupt_mode = -1; /* Failed! */
9517255736Sdavidch            break;
9518255736Sdavidch        }
9519255736Sdavidch
9520255736Sdavidch        BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9521255736Sdavidch    } while (0);
9522255736Sdavidch
9523255736Sdavidch    if (sc->interrupt_mode == -1) {
9524255736Sdavidch        BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9525255736Sdavidch        rc = 1;
9526255736Sdavidch    } else {
9527255736Sdavidch        BLOGD(sc, DBG_LOAD,
9528255736Sdavidch              "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9529255736Sdavidch              sc->interrupt_mode, sc->num_queues);
9530255736Sdavidch        rc = 0;
9531255736Sdavidch    }
9532255736Sdavidch
9533255736Sdavidch    return (rc);
9534255736Sdavidch}
9535255736Sdavidch
9536255736Sdavidchstatic void
9537255736Sdavidchbxe_interrupt_detach(struct bxe_softc *sc)
9538255736Sdavidch{
9539255736Sdavidch    struct bxe_fastpath *fp;
9540255736Sdavidch    int i;
9541255736Sdavidch
9542255736Sdavidch    /* release interrupt resources */
9543255736Sdavidch    for (i = 0; i < sc->intr_count; i++) {
9544255736Sdavidch        if (sc->intr[i].resource && sc->intr[i].tag) {
9545255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9546255736Sdavidch            bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9547255736Sdavidch        }
9548255736Sdavidch    }
9549255736Sdavidch
9550255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9551255736Sdavidch        fp = &sc->fp[i];
9552255736Sdavidch        if (fp->tq) {
9553255736Sdavidch            taskqueue_drain(fp->tq, &fp->tq_task);
9554255736Sdavidch            taskqueue_free(fp->tq);
9555255736Sdavidch            fp->tq = NULL;
9556255736Sdavidch        }
9557255736Sdavidch    }
9558255736Sdavidch
9559255736Sdavidch
9560255736Sdavidch    if (sc->sp_tq) {
9561255736Sdavidch        taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9562255736Sdavidch        taskqueue_free(sc->sp_tq);
9563255736Sdavidch        sc->sp_tq = NULL;
9564255736Sdavidch    }
9565255736Sdavidch}
9566255736Sdavidch
9567255736Sdavidch/*
9568255736Sdavidch * Enables interrupts and attach to the ISR.
9569255736Sdavidch *
9570255736Sdavidch * When using multiple MSI/MSI-X vectors the first vector
9571255736Sdavidch * is used for slowpath operations while all remaining
9572255736Sdavidch * vectors are used for fastpath operations.  If only a
9573255736Sdavidch * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9574255736Sdavidch * ISR must look for both slowpath and fastpath completions.
9575255736Sdavidch */
9576255736Sdavidchstatic int
9577255736Sdavidchbxe_interrupt_attach(struct bxe_softc *sc)
9578255736Sdavidch{
9579255736Sdavidch    struct bxe_fastpath *fp;
9580255736Sdavidch    int rc = 0;
9581255736Sdavidch    int i;
9582255736Sdavidch
9583255736Sdavidch    snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9584255736Sdavidch             "bxe%d_sp_tq", sc->unit);
9585255736Sdavidch    TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9586255736Sdavidch    sc->sp_tq = taskqueue_create_fast(sc->sp_tq_name, M_NOWAIT,
9587255736Sdavidch                                      taskqueue_thread_enqueue,
9588255736Sdavidch                                      &sc->sp_tq);
9589255736Sdavidch    taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9590255736Sdavidch                            "%s", sc->sp_tq_name);
9591255736Sdavidch
9592255736Sdavidch
9593255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9594255736Sdavidch        fp = &sc->fp[i];
9595255736Sdavidch        snprintf(fp->tq_name, sizeof(fp->tq_name),
9596255736Sdavidch                 "bxe%d_fp%d_tq", sc->unit, i);
9597255736Sdavidch        TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9598255736Sdavidch        fp->tq = taskqueue_create_fast(fp->tq_name, M_NOWAIT,
9599255736Sdavidch                                       taskqueue_thread_enqueue,
9600255736Sdavidch                                       &fp->tq);
9601255736Sdavidch        taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9602255736Sdavidch                                "%s", fp->tq_name);
9603255736Sdavidch    }
9604255736Sdavidch
9605255736Sdavidch    /* setup interrupt handlers */
9606255736Sdavidch    if (sc->interrupt_mode == INTR_MODE_MSIX) {
9607255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9608255736Sdavidch
9609255736Sdavidch        /*
9610255736Sdavidch         * Setup the interrupt handler. Note that we pass the driver instance
9611255736Sdavidch         * to the interrupt handler for the slowpath.
9612255736Sdavidch         */
9613255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9614255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9615255736Sdavidch                                 NULL, bxe_intr_sp, sc,
9616255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9617255736Sdavidch            BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9618255736Sdavidch            goto bxe_interrupt_attach_exit;
9619255736Sdavidch        }
9620255736Sdavidch
9621255736Sdavidch        bus_describe_intr(sc->dev, sc->intr[0].resource,
9622255736Sdavidch                          sc->intr[0].tag, "sp");
9623255736Sdavidch
9624255736Sdavidch        /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9625255736Sdavidch
9626255736Sdavidch        /* initialize the fastpath vectors (note the first was used for sp) */
9627255736Sdavidch        for (i = 0; i < sc->num_queues; i++) {
9628255736Sdavidch            fp = &sc->fp[i];
9629255736Sdavidch            BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9630255736Sdavidch
9631255736Sdavidch            /*
9632255736Sdavidch             * Setup the interrupt handler. Note that we pass the
9633255736Sdavidch             * fastpath context to the interrupt handler in this
9634255736Sdavidch             * case.
9635255736Sdavidch             */
9636255736Sdavidch            if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9637255736Sdavidch                                     (INTR_TYPE_NET | INTR_MPSAFE),
9638255736Sdavidch                                     NULL, bxe_intr_fp, fp,
9639255736Sdavidch                                     &sc->intr[i + 1].tag)) != 0) {
9640255736Sdavidch                BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9641255736Sdavidch                      (i + 1), rc);
9642255736Sdavidch                goto bxe_interrupt_attach_exit;
9643255736Sdavidch            }
9644255736Sdavidch
9645255736Sdavidch            bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9646255736Sdavidch                              sc->intr[i + 1].tag, "fp%02d", i);
9647255736Sdavidch
9648255736Sdavidch            /* bind the fastpath instance to a cpu */
9649255736Sdavidch            if (sc->num_queues > 1) {
9650255736Sdavidch                bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9651255736Sdavidch            }
9652255736Sdavidch
9653255736Sdavidch            fp->state = BXE_FP_STATE_IRQ;
9654255736Sdavidch        }
9655255736Sdavidch    } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9656262999Sedavis        BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9657255736Sdavidch
9658255736Sdavidch        /*
9659262999Sedavis         * Setup the interrupt handler. Note that we pass the
9660262999Sedavis         * driver instance to the interrupt handler which
9661262999Sedavis         * will handle both the slowpath and fastpath.
9662255736Sdavidch         */
9663255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9664255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9665262999Sedavis                                 NULL, bxe_intr_legacy, sc,
9666255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9667255736Sdavidch            BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9668255736Sdavidch            goto bxe_interrupt_attach_exit;
9669255736Sdavidch        }
9670255736Sdavidch
9671255736Sdavidch    } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9672255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9673255736Sdavidch
9674255736Sdavidch        /*
9675255736Sdavidch         * Setup the interrupt handler. Note that we pass the
9676255736Sdavidch         * driver instance to the interrupt handler which
9677255736Sdavidch         * will handle both the slowpath and fastpath.
9678255736Sdavidch         */
9679255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9680255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9681255736Sdavidch                                 NULL, bxe_intr_legacy, sc,
9682255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9683255736Sdavidch            BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9684255736Sdavidch            goto bxe_interrupt_attach_exit;
9685255736Sdavidch        }
9686255736Sdavidch    }
9687255736Sdavidch
9688255736Sdavidchbxe_interrupt_attach_exit:
9689255736Sdavidch
9690255736Sdavidch    return (rc);
9691255736Sdavidch}
9692255736Sdavidch
9693255736Sdavidchstatic int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9694255736Sdavidchstatic int  bxe_init_hw_common(struct bxe_softc *sc);
9695255736Sdavidchstatic int  bxe_init_hw_port(struct bxe_softc *sc);
9696255736Sdavidchstatic int  bxe_init_hw_func(struct bxe_softc *sc);
9697255736Sdavidchstatic void bxe_reset_common(struct bxe_softc *sc);
9698255736Sdavidchstatic void bxe_reset_port(struct bxe_softc *sc);
9699255736Sdavidchstatic void bxe_reset_func(struct bxe_softc *sc);
9700255736Sdavidchstatic int  bxe_gunzip_init(struct bxe_softc *sc);
9701255736Sdavidchstatic void bxe_gunzip_end(struct bxe_softc *sc);
9702255736Sdavidchstatic int  bxe_init_firmware(struct bxe_softc *sc);
9703255736Sdavidchstatic void bxe_release_firmware(struct bxe_softc *sc);
9704255736Sdavidch
9705255736Sdavidchstatic struct
9706255736Sdavidchecore_func_sp_drv_ops bxe_func_sp_drv = {
9707255736Sdavidch    .init_hw_cmn_chip = bxe_init_hw_common_chip,
9708255736Sdavidch    .init_hw_cmn      = bxe_init_hw_common,
9709255736Sdavidch    .init_hw_port     = bxe_init_hw_port,
9710255736Sdavidch    .init_hw_func     = bxe_init_hw_func,
9711255736Sdavidch
9712255736Sdavidch    .reset_hw_cmn     = bxe_reset_common,
9713255736Sdavidch    .reset_hw_port    = bxe_reset_port,
9714255736Sdavidch    .reset_hw_func    = bxe_reset_func,
9715255736Sdavidch
9716255736Sdavidch    .gunzip_init      = bxe_gunzip_init,
9717255736Sdavidch    .gunzip_end       = bxe_gunzip_end,
9718255736Sdavidch
9719255736Sdavidch    .init_fw          = bxe_init_firmware,
9720255736Sdavidch    .release_fw       = bxe_release_firmware,
9721255736Sdavidch};
9722255736Sdavidch
9723255736Sdavidchstatic void
9724255736Sdavidchbxe_init_func_obj(struct bxe_softc *sc)
9725255736Sdavidch{
9726255736Sdavidch    sc->dmae_ready = 0;
9727255736Sdavidch
9728255736Sdavidch    ecore_init_func_obj(sc,
9729255736Sdavidch                        &sc->func_obj,
9730255736Sdavidch                        BXE_SP(sc, func_rdata),
9731255736Sdavidch                        BXE_SP_MAPPING(sc, func_rdata),
9732255736Sdavidch                        BXE_SP(sc, func_afex_rdata),
9733255736Sdavidch                        BXE_SP_MAPPING(sc, func_afex_rdata),
9734255736Sdavidch                        &bxe_func_sp_drv);
9735255736Sdavidch}
9736255736Sdavidch
9737255736Sdavidchstatic int
9738255736Sdavidchbxe_init_hw(struct bxe_softc *sc,
9739255736Sdavidch            uint32_t         load_code)
9740255736Sdavidch{
9741255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
9742255736Sdavidch    int rc;
9743255736Sdavidch
9744255736Sdavidch    /* prepare the parameters for function state transitions */
9745255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9746255736Sdavidch
9747255736Sdavidch    func_params.f_obj = &sc->func_obj;
9748255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_INIT;
9749255736Sdavidch
9750255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
9751255736Sdavidch
9752255736Sdavidch    /*
9753255736Sdavidch     * Via a plethora of function pointers, we will eventually reach
9754255736Sdavidch     * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9755255736Sdavidch     */
9756255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
9757255736Sdavidch
9758255736Sdavidch    return (rc);
9759255736Sdavidch}
9760255736Sdavidch
9761255736Sdavidchstatic void
9762255736Sdavidchbxe_fill(struct bxe_softc *sc,
9763255736Sdavidch         uint32_t         addr,
9764255736Sdavidch         int              fill,
9765255736Sdavidch         uint32_t         len)
9766255736Sdavidch{
9767255736Sdavidch    uint32_t i;
9768255736Sdavidch
9769255736Sdavidch    if (!(len % 4) && !(addr % 4)) {
9770255736Sdavidch        for (i = 0; i < len; i += 4) {
9771255736Sdavidch            REG_WR(sc, (addr + i), fill);
9772255736Sdavidch        }
9773255736Sdavidch    } else {
9774255736Sdavidch        for (i = 0; i < len; i++) {
9775255736Sdavidch            REG_WR8(sc, (addr + i), fill);
9776255736Sdavidch        }
9777255736Sdavidch    }
9778255736Sdavidch}
9779255736Sdavidch
9780255736Sdavidch/* writes FP SP data to FW - data_size in dwords */
9781255736Sdavidchstatic void
9782255736Sdavidchbxe_wr_fp_sb_data(struct bxe_softc *sc,
9783255736Sdavidch                  int              fw_sb_id,
9784255736Sdavidch                  uint32_t         *sb_data_p,
9785255736Sdavidch                  uint32_t         data_size)
9786255736Sdavidch{
9787255736Sdavidch    int index;
9788255736Sdavidch
9789255736Sdavidch    for (index = 0; index < data_size; index++) {
9790255736Sdavidch        REG_WR(sc,
9791255736Sdavidch               (BAR_CSTRORM_INTMEM +
9792255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9793255736Sdavidch                (sizeof(uint32_t) * index)),
9794255736Sdavidch               *(sb_data_p + index));
9795255736Sdavidch    }
9796255736Sdavidch}
9797255736Sdavidch
9798255736Sdavidchstatic void
9799255736Sdavidchbxe_zero_fp_sb(struct bxe_softc *sc,
9800255736Sdavidch               int              fw_sb_id)
9801255736Sdavidch{
9802255736Sdavidch    struct hc_status_block_data_e2 sb_data_e2;
9803255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9804255736Sdavidch    uint32_t *sb_data_p;
9805255736Sdavidch    uint32_t data_size = 0;
9806255736Sdavidch
9807255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9808255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9809255736Sdavidch        sb_data_e2.common.state = SB_DISABLED;
9810255736Sdavidch        sb_data_e2.common.p_func.vf_valid = FALSE;
9811255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9812255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9813255736Sdavidch                     sizeof(uint32_t));
9814255736Sdavidch    } else {
9815255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9816255736Sdavidch        sb_data_e1x.common.state = SB_DISABLED;
9817255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9818255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9819255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9820255736Sdavidch                     sizeof(uint32_t));
9821255736Sdavidch    }
9822255736Sdavidch
9823255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9824255736Sdavidch
9825255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9826255736Sdavidch             0, CSTORM_STATUS_BLOCK_SIZE);
9827255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9828255736Sdavidch             0, CSTORM_SYNC_BLOCK_SIZE);
9829255736Sdavidch}
9830255736Sdavidch
9831255736Sdavidchstatic void
9832255736Sdavidchbxe_wr_sp_sb_data(struct bxe_softc               *sc,
9833255736Sdavidch                  struct hc_sp_status_block_data *sp_sb_data)
9834255736Sdavidch{
9835255736Sdavidch    int i;
9836255736Sdavidch
9837255736Sdavidch    for (i = 0;
9838255736Sdavidch         i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9839255736Sdavidch         i++) {
9840255736Sdavidch        REG_WR(sc,
9841255736Sdavidch               (BAR_CSTRORM_INTMEM +
9842255736Sdavidch                CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9843255736Sdavidch                (i * sizeof(uint32_t))),
9844255736Sdavidch               *((uint32_t *)sp_sb_data + i));
9845255736Sdavidch    }
9846255736Sdavidch}
9847255736Sdavidch
9848255736Sdavidchstatic void
9849255736Sdavidchbxe_zero_sp_sb(struct bxe_softc *sc)
9850255736Sdavidch{
9851255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9852255736Sdavidch
9853255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9854255736Sdavidch
9855255736Sdavidch    sp_sb_data.state           = SB_DISABLED;
9856255736Sdavidch    sp_sb_data.p_func.vf_valid = FALSE;
9857255736Sdavidch
9858255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9859255736Sdavidch
9860255736Sdavidch    bxe_fill(sc,
9861255736Sdavidch             (BAR_CSTRORM_INTMEM +
9862255736Sdavidch              CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9863255736Sdavidch              0, CSTORM_SP_STATUS_BLOCK_SIZE);
9864255736Sdavidch    bxe_fill(sc,
9865255736Sdavidch             (BAR_CSTRORM_INTMEM +
9866255736Sdavidch              CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9867255736Sdavidch              0, CSTORM_SP_SYNC_BLOCK_SIZE);
9868255736Sdavidch}
9869255736Sdavidch
9870255736Sdavidchstatic void
9871255736Sdavidchbxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9872255736Sdavidch                             int                       igu_sb_id,
9873255736Sdavidch                             int                       igu_seg_id)
9874255736Sdavidch{
9875255736Sdavidch    hc_sm->igu_sb_id      = igu_sb_id;
9876255736Sdavidch    hc_sm->igu_seg_id     = igu_seg_id;
9877255736Sdavidch    hc_sm->timer_value    = 0xFF;
9878255736Sdavidch    hc_sm->time_to_expire = 0xFFFFFFFF;
9879255736Sdavidch}
9880255736Sdavidch
9881255736Sdavidchstatic void
9882255736Sdavidchbxe_map_sb_state_machines(struct hc_index_data *index_data)
9883255736Sdavidch{
9884255736Sdavidch    /* zero out state machine indices */
9885255736Sdavidch
9886255736Sdavidch    /* rx indices */
9887255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9888255736Sdavidch
9889255736Sdavidch    /* tx indices */
9890255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9891255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9892255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9893255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9894255736Sdavidch
9895255736Sdavidch    /* map indices */
9896255736Sdavidch
9897255736Sdavidch    /* rx indices */
9898255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9899255736Sdavidch        (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9900255736Sdavidch
9901255736Sdavidch    /* tx indices */
9902255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9903255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9904255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9905255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9906255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9907255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9908255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9909255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9910255736Sdavidch}
9911255736Sdavidch
9912255736Sdavidchstatic void
9913255736Sdavidchbxe_init_sb(struct bxe_softc *sc,
9914255736Sdavidch            bus_addr_t       busaddr,
9915255736Sdavidch            int              vfid,
9916255736Sdavidch            uint8_t          vf_valid,
9917255736Sdavidch            int              fw_sb_id,
9918255736Sdavidch            int              igu_sb_id)
9919255736Sdavidch{
9920255736Sdavidch    struct hc_status_block_data_e2  sb_data_e2;
9921255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9922255736Sdavidch    struct hc_status_block_sm       *hc_sm_p;
9923255736Sdavidch    uint32_t *sb_data_p;
9924255736Sdavidch    int igu_seg_id;
9925255736Sdavidch    int data_size;
9926255736Sdavidch
9927255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9928255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_NORM;
9929255736Sdavidch    } else {
9930255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_NORM;
9931255736Sdavidch    }
9932255736Sdavidch
9933255736Sdavidch    bxe_zero_fp_sb(sc, fw_sb_id);
9934255736Sdavidch
9935255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9936255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9937255736Sdavidch        sb_data_e2.common.state = SB_ENABLED;
9938255736Sdavidch        sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9939255736Sdavidch        sb_data_e2.common.p_func.vf_id = vfid;
9940255736Sdavidch        sb_data_e2.common.p_func.vf_valid = vf_valid;
9941255736Sdavidch        sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9942255736Sdavidch        sb_data_e2.common.same_igu_sb_1b = TRUE;
9943255736Sdavidch        sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9944255736Sdavidch        sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9945255736Sdavidch        hc_sm_p = sb_data_e2.common.state_machine;
9946255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9947255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9948255736Sdavidch                     sizeof(uint32_t));
9949255736Sdavidch        bxe_map_sb_state_machines(sb_data_e2.index_data);
9950255736Sdavidch    } else {
9951255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9952255736Sdavidch        sb_data_e1x.common.state = SB_ENABLED;
9953255736Sdavidch        sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9954255736Sdavidch        sb_data_e1x.common.p_func.vf_id = 0xff;
9955255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9956255736Sdavidch        sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9957255736Sdavidch        sb_data_e1x.common.same_igu_sb_1b = TRUE;
9958255736Sdavidch        sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9959255736Sdavidch        sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9960255736Sdavidch        hc_sm_p = sb_data_e1x.common.state_machine;
9961255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9962255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9963255736Sdavidch                     sizeof(uint32_t));
9964255736Sdavidch        bxe_map_sb_state_machines(sb_data_e1x.index_data);
9965255736Sdavidch    }
9966255736Sdavidch
9967255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9968255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9969255736Sdavidch
9970255736Sdavidch    BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9971255736Sdavidch
9972255736Sdavidch    /* write indices to HW - PCI guarantees endianity of regpairs */
9973255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9974255736Sdavidch}
9975255736Sdavidch
9976255736Sdavidchstatic inline uint8_t
9977255736Sdavidchbxe_fp_qzone_id(struct bxe_fastpath *fp)
9978255736Sdavidch{
9979255736Sdavidch    if (CHIP_IS_E1x(fp->sc)) {
9980255736Sdavidch        return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9981255736Sdavidch    } else {
9982255736Sdavidch        return (fp->cl_id);
9983255736Sdavidch    }
9984255736Sdavidch}
9985255736Sdavidch
9986255736Sdavidchstatic inline uint32_t
9987255736Sdavidchbxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9988255736Sdavidch                           struct bxe_fastpath *fp)
9989255736Sdavidch{
9990255736Sdavidch    uint32_t offset = BAR_USTRORM_INTMEM;
9991255736Sdavidch
9992255736Sdavidch#if 0
9993255736Sdavidch    if (IS_VF(sc)) {
9994255736Sdavidch        return (PXP_VF_ADDR_USDM_QUEUES_START +
9995255736Sdavidch                (sc->acquire_resp.resc.hw_qid[fp->index] *
9996255736Sdavidch                 sizeof(struct ustorm_queue_zone_data)));
9997255736Sdavidch    } else
9998255736Sdavidch#endif
9999255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10000255736Sdavidch        offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
10001255736Sdavidch    } else {
10002255736Sdavidch        offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
10003255736Sdavidch    }
10004255736Sdavidch
10005255736Sdavidch    return (offset);
10006255736Sdavidch}
10007255736Sdavidch
10008255736Sdavidchstatic void
10009255736Sdavidchbxe_init_eth_fp(struct bxe_softc *sc,
10010255736Sdavidch                int              idx)
10011255736Sdavidch{
10012255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[idx];
10013255736Sdavidch    uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
10014255736Sdavidch    unsigned long q_type = 0;
10015255736Sdavidch    int cos;
10016255736Sdavidch
10017255736Sdavidch    fp->sc    = sc;
10018255736Sdavidch    fp->index = idx;
10019255736Sdavidch
10020255736Sdavidch    snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
10021255736Sdavidch             "bxe%d_fp%d_tx_lock", sc->unit, idx);
10022255736Sdavidch    mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
10023255736Sdavidch
10024255736Sdavidch    snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
10025255736Sdavidch             "bxe%d_fp%d_rx_lock", sc->unit, idx);
10026255736Sdavidch    mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
10027255736Sdavidch
10028255736Sdavidch    fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
10029255736Sdavidch    fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
10030255736Sdavidch
10031255736Sdavidch    fp->cl_id = (CHIP_IS_E1x(sc)) ?
10032255736Sdavidch                    (SC_L_ID(sc) + idx) :
10033255736Sdavidch                    /* want client ID same as IGU SB ID for non-E1 */
10034255736Sdavidch                    fp->igu_sb_id;
10035255736Sdavidch    fp->cl_qzone_id = bxe_fp_qzone_id(fp);
10036255736Sdavidch
10037255736Sdavidch    /* setup sb indices */
10038255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10039255736Sdavidch        fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
10040255736Sdavidch        fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
10041255736Sdavidch    } else {
10042255736Sdavidch        fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
10043255736Sdavidch        fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
10044255736Sdavidch    }
10045255736Sdavidch
10046255736Sdavidch    /* init shortcut */
10047255736Sdavidch    fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
10048255736Sdavidch
10049255736Sdavidch    fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
10050255736Sdavidch
10051255736Sdavidch    /*
10052255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
10053255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
10054255736Sdavidch     */
10055255736Sdavidch    for (cos = 0; cos < sc->max_cos; cos++) {
10056255736Sdavidch        cids[cos] = idx;
10057255736Sdavidch    }
10058255736Sdavidch    fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
10059255736Sdavidch
10060255736Sdavidch    /* nothing more for a VF to do */
10061255736Sdavidch    if (IS_VF(sc)) {
10062255736Sdavidch        return;
10063255736Sdavidch    }
10064255736Sdavidch
10065255736Sdavidch    bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
10066255736Sdavidch                fp->fw_sb_id, fp->igu_sb_id);
10067255736Sdavidch
10068255736Sdavidch    bxe_update_fp_sb_idx(fp);
10069255736Sdavidch
10070255736Sdavidch    /* Configure Queue State object */
10071255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
10072255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
10073255736Sdavidch
10074255736Sdavidch    ecore_init_queue_obj(sc,
10075255736Sdavidch                         &sc->sp_objs[idx].q_obj,
10076255736Sdavidch                         fp->cl_id,
10077255736Sdavidch                         cids,
10078255736Sdavidch                         sc->max_cos,
10079255736Sdavidch                         SC_FUNC(sc),
10080255736Sdavidch                         BXE_SP(sc, q_rdata),
10081255736Sdavidch                         BXE_SP_MAPPING(sc, q_rdata),
10082255736Sdavidch                         q_type);
10083255736Sdavidch
10084255736Sdavidch    /* configure classification DBs */
10085255736Sdavidch    ecore_init_mac_obj(sc,
10086255736Sdavidch                       &sc->sp_objs[idx].mac_obj,
10087255736Sdavidch                       fp->cl_id,
10088255736Sdavidch                       idx,
10089255736Sdavidch                       SC_FUNC(sc),
10090255736Sdavidch                       BXE_SP(sc, mac_rdata),
10091255736Sdavidch                       BXE_SP_MAPPING(sc, mac_rdata),
10092255736Sdavidch                       ECORE_FILTER_MAC_PENDING,
10093255736Sdavidch                       &sc->sp_state,
10094255736Sdavidch                       ECORE_OBJ_TYPE_RX_TX,
10095255736Sdavidch                       &sc->macs_pool);
10096255736Sdavidch
10097255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
10098255736Sdavidch          idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
10099255736Sdavidch}
10100255736Sdavidch
10101255736Sdavidchstatic inline void
10102255736Sdavidchbxe_update_rx_prod(struct bxe_softc    *sc,
10103255736Sdavidch                   struct bxe_fastpath *fp,
10104255736Sdavidch                   uint16_t            rx_bd_prod,
10105255736Sdavidch                   uint16_t            rx_cq_prod,
10106255736Sdavidch                   uint16_t            rx_sge_prod)
10107255736Sdavidch{
10108255736Sdavidch    struct ustorm_eth_rx_producers rx_prods = { 0 };
10109255736Sdavidch    uint32_t i;
10110255736Sdavidch
10111255736Sdavidch    /* update producers */
10112255736Sdavidch    rx_prods.bd_prod  = rx_bd_prod;
10113255736Sdavidch    rx_prods.cqe_prod = rx_cq_prod;
10114255736Sdavidch    rx_prods.sge_prod = rx_sge_prod;
10115255736Sdavidch
10116255736Sdavidch    /*
10117255736Sdavidch     * Make sure that the BD and SGE data is updated before updating the
10118255736Sdavidch     * producers since FW might read the BD/SGE right after the producer
10119255736Sdavidch     * is updated.
10120255736Sdavidch     * This is only applicable for weak-ordered memory model archs such
10121255736Sdavidch     * as IA-64. The following barrier is also mandatory since FW will
10122255736Sdavidch     * assumes BDs must have buffers.
10123255736Sdavidch     */
10124255736Sdavidch    wmb();
10125255736Sdavidch
10126255736Sdavidch    for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
10127255736Sdavidch        REG_WR(sc,
10128255736Sdavidch               (fp->ustorm_rx_prods_offset + (i * 4)),
10129255736Sdavidch               ((uint32_t *)&rx_prods)[i]);
10130255736Sdavidch    }
10131255736Sdavidch
10132255736Sdavidch    wmb(); /* keep prod updates ordered */
10133255736Sdavidch
10134255736Sdavidch    BLOGD(sc, DBG_RX,
10135255736Sdavidch          "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
10136255736Sdavidch          fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
10137255736Sdavidch}
10138255736Sdavidch
10139255736Sdavidchstatic void
10140255736Sdavidchbxe_init_rx_rings(struct bxe_softc *sc)
10141255736Sdavidch{
10142255736Sdavidch    struct bxe_fastpath *fp;
10143255736Sdavidch    int i;
10144255736Sdavidch
10145255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
10146255736Sdavidch        fp = &sc->fp[i];
10147255736Sdavidch
10148255736Sdavidch        fp->rx_bd_cons = 0;
10149255736Sdavidch
10150255736Sdavidch        /*
10151255736Sdavidch         * Activate the BD ring...
10152255736Sdavidch         * Warning, this will generate an interrupt (to the TSTORM)
10153255736Sdavidch         * so this can only be done after the chip is initialized
10154255736Sdavidch         */
10155255736Sdavidch        bxe_update_rx_prod(sc, fp,
10156255736Sdavidch                           fp->rx_bd_prod,
10157255736Sdavidch                           fp->rx_cq_prod,
10158255736Sdavidch                           fp->rx_sge_prod);
10159255736Sdavidch
10160255736Sdavidch        if (i != 0) {
10161255736Sdavidch            continue;
10162255736Sdavidch        }
10163255736Sdavidch
10164255736Sdavidch        if (CHIP_IS_E1(sc)) {
10165255736Sdavidch            REG_WR(sc,
10166255736Sdavidch                   (BAR_USTRORM_INTMEM +
10167255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
10168255736Sdavidch                   U64_LO(fp->rcq_dma.paddr));
10169255736Sdavidch            REG_WR(sc,
10170255736Sdavidch                   (BAR_USTRORM_INTMEM +
10171255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
10172255736Sdavidch                   U64_HI(fp->rcq_dma.paddr));
10173255736Sdavidch        }
10174255736Sdavidch    }
10175255736Sdavidch}
10176255736Sdavidch
10177255736Sdavidchstatic void
10178255736Sdavidchbxe_init_tx_ring_one(struct bxe_fastpath *fp)
10179255736Sdavidch{
10180255736Sdavidch    SET_FLAG(fp->tx_db.data.header.header, DOORBELL_HDR_DB_TYPE, 1);
10181255736Sdavidch    fp->tx_db.data.zero_fill1 = 0;
10182255736Sdavidch    fp->tx_db.data.prod = 0;
10183255736Sdavidch
10184255736Sdavidch    fp->tx_pkt_prod = 0;
10185255736Sdavidch    fp->tx_pkt_cons = 0;
10186255736Sdavidch    fp->tx_bd_prod = 0;
10187255736Sdavidch    fp->tx_bd_cons = 0;
10188255736Sdavidch    fp->eth_q_stats.tx_pkts = 0;
10189255736Sdavidch}
10190255736Sdavidch
10191255736Sdavidchstatic inline void
10192255736Sdavidchbxe_init_tx_rings(struct bxe_softc *sc)
10193255736Sdavidch{
10194255736Sdavidch    int i;
10195255736Sdavidch
10196255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
10197255736Sdavidch#if 0
10198255736Sdavidch        uint8_t cos;
10199255736Sdavidch        for (cos = 0; cos < sc->max_cos; cos++) {
10200255736Sdavidch            bxe_init_tx_ring_one(&sc->fp[i].txdata[cos]);
10201255736Sdavidch        }
10202255736Sdavidch#else
10203255736Sdavidch        bxe_init_tx_ring_one(&sc->fp[i]);
10204255736Sdavidch#endif
10205255736Sdavidch    }
10206255736Sdavidch}
10207255736Sdavidch
10208255736Sdavidchstatic void
10209255736Sdavidchbxe_init_def_sb(struct bxe_softc *sc)
10210255736Sdavidch{
10211255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
10212255736Sdavidch    bus_addr_t mapping = sc->def_sb_dma.paddr;
10213255736Sdavidch    int igu_sp_sb_index;
10214255736Sdavidch    int igu_seg_id;
10215255736Sdavidch    int port = SC_PORT(sc);
10216255736Sdavidch    int func = SC_FUNC(sc);
10217255736Sdavidch    int reg_offset, reg_offset_en5;
10218255736Sdavidch    uint64_t section;
10219255736Sdavidch    int index, sindex;
10220255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
10221255736Sdavidch
10222255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
10223255736Sdavidch
10224255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
10225255736Sdavidch        igu_sp_sb_index = DEF_SB_IGU_ID;
10226255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_DEF;
10227255736Sdavidch    } else {
10228255736Sdavidch        igu_sp_sb_index = sc->igu_dsb_id;
10229255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_DEF;
10230255736Sdavidch    }
10231255736Sdavidch
10232255736Sdavidch    /* attentions */
10233255736Sdavidch    section = ((uint64_t)mapping +
10234255736Sdavidch               offsetof(struct host_sp_status_block, atten_status_block));
10235255736Sdavidch    def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
10236255736Sdavidch    sc->attn_state = 0;
10237255736Sdavidch
10238255736Sdavidch    reg_offset = (port) ?
10239255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
10240255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
10241255736Sdavidch    reg_offset_en5 = (port) ?
10242255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
10243255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
10244255736Sdavidch
10245255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
10246255736Sdavidch        /* take care of sig[0]..sig[4] */
10247255736Sdavidch        for (sindex = 0; sindex < 4; sindex++) {
10248255736Sdavidch            sc->attn_group[index].sig[sindex] =
10249255736Sdavidch                REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
10250255736Sdavidch        }
10251255736Sdavidch
10252255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
10253255736Sdavidch            /*
10254255736Sdavidch             * enable5 is separate from the rest of the registers,
10255255736Sdavidch             * and the address skip is 4 and not 16 between the
10256255736Sdavidch             * different groups
10257255736Sdavidch             */
10258255736Sdavidch            sc->attn_group[index].sig[4] =
10259255736Sdavidch                REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
10260255736Sdavidch        } else {
10261255736Sdavidch            sc->attn_group[index].sig[4] = 0;
10262255736Sdavidch        }
10263255736Sdavidch    }
10264255736Sdavidch
10265255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10266255736Sdavidch        reg_offset = (port) ?
10267255736Sdavidch                         HC_REG_ATTN_MSG1_ADDR_L :
10268255736Sdavidch                         HC_REG_ATTN_MSG0_ADDR_L;
10269255736Sdavidch        REG_WR(sc, reg_offset, U64_LO(section));
10270255736Sdavidch        REG_WR(sc, (reg_offset + 4), U64_HI(section));
10271255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
10272255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
10273255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
10274255736Sdavidch    }
10275255736Sdavidch
10276255736Sdavidch    section = ((uint64_t)mapping +
10277255736Sdavidch               offsetof(struct host_sp_status_block, sp_sb));
10278255736Sdavidch
10279255736Sdavidch    bxe_zero_sp_sb(sc);
10280255736Sdavidch
10281255736Sdavidch    /* PCI guarantees endianity of regpair */
10282255736Sdavidch    sp_sb_data.state           = SB_ENABLED;
10283255736Sdavidch    sp_sb_data.host_sb_addr.lo = U64_LO(section);
10284255736Sdavidch    sp_sb_data.host_sb_addr.hi = U64_HI(section);
10285255736Sdavidch    sp_sb_data.igu_sb_id       = igu_sp_sb_index;
10286255736Sdavidch    sp_sb_data.igu_seg_id      = igu_seg_id;
10287255736Sdavidch    sp_sb_data.p_func.pf_id    = func;
10288255736Sdavidch    sp_sb_data.p_func.vnic_id  = SC_VN(sc);
10289255736Sdavidch    sp_sb_data.p_func.vf_id    = 0xff;
10290255736Sdavidch
10291255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
10292255736Sdavidch
10293255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
10294255736Sdavidch}
10295255736Sdavidch
10296255736Sdavidchstatic void
10297255736Sdavidchbxe_init_sp_ring(struct bxe_softc *sc)
10298255736Sdavidch{
10299255736Sdavidch    atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
10300255736Sdavidch    sc->spq_prod_idx = 0;
10301255736Sdavidch    sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
10302255736Sdavidch    sc->spq_prod_bd = sc->spq;
10303255736Sdavidch    sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
10304255736Sdavidch}
10305255736Sdavidch
10306255736Sdavidchstatic void
10307255736Sdavidchbxe_init_eq_ring(struct bxe_softc *sc)
10308255736Sdavidch{
10309255736Sdavidch    union event_ring_elem *elem;
10310255736Sdavidch    int i;
10311255736Sdavidch
10312255736Sdavidch    for (i = 1; i <= NUM_EQ_PAGES; i++) {
10313255736Sdavidch        elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
10314255736Sdavidch
10315255736Sdavidch        elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
10316255736Sdavidch                                                 BCM_PAGE_SIZE *
10317255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
10318255736Sdavidch        elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
10319255736Sdavidch                                                 BCM_PAGE_SIZE *
10320255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
10321255736Sdavidch    }
10322255736Sdavidch
10323255736Sdavidch    sc->eq_cons    = 0;
10324255736Sdavidch    sc->eq_prod    = NUM_EQ_DESC;
10325255736Sdavidch    sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
10326255736Sdavidch
10327255736Sdavidch    atomic_store_rel_long(&sc->eq_spq_left,
10328255736Sdavidch                          (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
10329255736Sdavidch                               NUM_EQ_DESC) - 1));
10330255736Sdavidch}
10331255736Sdavidch
10332255736Sdavidchstatic void
10333255736Sdavidchbxe_init_internal_common(struct bxe_softc *sc)
10334255736Sdavidch{
10335255736Sdavidch    int i;
10336255736Sdavidch
10337255736Sdavidch    if (IS_MF_SI(sc)) {
10338255736Sdavidch        /*
10339255736Sdavidch         * In switch independent mode, the TSTORM needs to accept
10340255736Sdavidch         * packets that failed classification, since approximate match
10341255736Sdavidch         * mac addresses aren't written to NIG LLH.
10342255736Sdavidch         */
10343255736Sdavidch        REG_WR8(sc,
10344255736Sdavidch                (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
10345255736Sdavidch                2);
10346255736Sdavidch    } else if (!CHIP_IS_E1(sc)) { /* 57710 doesn't support MF */
10347255736Sdavidch        REG_WR8(sc,
10348255736Sdavidch                (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
10349255736Sdavidch                0);
10350255736Sdavidch    }
10351255736Sdavidch
10352255736Sdavidch    /*
10353255736Sdavidch     * Zero this manually as its initialization is currently missing
10354255736Sdavidch     * in the initTool.
10355255736Sdavidch     */
10356255736Sdavidch    for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
10357255736Sdavidch        REG_WR(sc,
10358255736Sdavidch               (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
10359255736Sdavidch               0);
10360255736Sdavidch    }
10361255736Sdavidch
10362255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10363255736Sdavidch        REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
10364255736Sdavidch                CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
10365255736Sdavidch    }
10366255736Sdavidch}
10367255736Sdavidch
10368255736Sdavidchstatic void
10369255736Sdavidchbxe_init_internal(struct bxe_softc *sc,
10370255736Sdavidch                  uint32_t         load_code)
10371255736Sdavidch{
10372255736Sdavidch    switch (load_code) {
10373255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON:
10374255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
10375255736Sdavidch        bxe_init_internal_common(sc);
10376255736Sdavidch        /* no break */
10377255736Sdavidch
10378255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_PORT:
10379255736Sdavidch        /* nothing to do */
10380255736Sdavidch        /* no break */
10381255736Sdavidch
10382255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_FUNCTION:
10383255736Sdavidch        /* internal memory per function is initialized inside bxe_pf_init */
10384255736Sdavidch        break;
10385255736Sdavidch
10386255736Sdavidch    default:
10387255736Sdavidch        BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
10388255736Sdavidch        break;
10389255736Sdavidch    }
10390255736Sdavidch}
10391255736Sdavidch
10392255736Sdavidchstatic void
10393255736Sdavidchstorm_memset_func_cfg(struct bxe_softc                         *sc,
10394255736Sdavidch                      struct tstorm_eth_function_common_config *tcfg,
10395255736Sdavidch                      uint16_t                                  abs_fid)
10396255736Sdavidch{
10397255736Sdavidch    uint32_t addr;
10398255736Sdavidch    size_t size;
10399255736Sdavidch
10400255736Sdavidch    addr = (BAR_TSTRORM_INTMEM +
10401255736Sdavidch            TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
10402255736Sdavidch    size = sizeof(struct tstorm_eth_function_common_config);
10403255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
10404255736Sdavidch}
10405255736Sdavidch
10406255736Sdavidchstatic void
10407255736Sdavidchbxe_func_init(struct bxe_softc            *sc,
10408255736Sdavidch              struct bxe_func_init_params *p)
10409255736Sdavidch{
10410255736Sdavidch    struct tstorm_eth_function_common_config tcfg = { 0 };
10411255736Sdavidch
10412255736Sdavidch    if (CHIP_IS_E1x(sc)) {
10413255736Sdavidch        storm_memset_func_cfg(sc, &tcfg, p->func_id);
10414255736Sdavidch    }
10415255736Sdavidch
10416255736Sdavidch    /* Enable the function in the FW */
10417255736Sdavidch    storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
10418255736Sdavidch    storm_memset_func_en(sc, p->func_id, 1);
10419255736Sdavidch
10420255736Sdavidch    /* spq */
10421255736Sdavidch    if (p->func_flgs & FUNC_FLG_SPQ) {
10422255736Sdavidch        storm_memset_spq_addr(sc, p->spq_map, p->func_id);
10423255736Sdavidch        REG_WR(sc,
10424255736Sdavidch               (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
10425255736Sdavidch               p->spq_prod);
10426255736Sdavidch    }
10427255736Sdavidch}
10428255736Sdavidch
10429255736Sdavidch/*
10430255736Sdavidch * Calculates the sum of vn_min_rates.
10431255736Sdavidch * It's needed for further normalizing of the min_rates.
10432255736Sdavidch * Returns:
10433255736Sdavidch *   sum of vn_min_rates.
10434255736Sdavidch *     or
10435255736Sdavidch *   0 - if all the min_rates are 0.
10436255736Sdavidch * In the later case fainess algorithm should be deactivated.
10437255736Sdavidch * If all min rates are not zero then those that are zeroes will be set to 1.
10438255736Sdavidch */
10439255736Sdavidchstatic void
10440255736Sdavidchbxe_calc_vn_min(struct bxe_softc       *sc,
10441255736Sdavidch                struct cmng_init_input *input)
10442255736Sdavidch{
10443255736Sdavidch    uint32_t vn_cfg;
10444255736Sdavidch    uint32_t vn_min_rate;
10445255736Sdavidch    int all_zero = 1;
10446255736Sdavidch    int vn;
10447255736Sdavidch
10448255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10449255736Sdavidch        vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10450255736Sdavidch        vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
10451255736Sdavidch                        FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
10452255736Sdavidch
10453255736Sdavidch        if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10454255736Sdavidch            /* skip hidden VNs */
10455255736Sdavidch            vn_min_rate = 0;
10456255736Sdavidch        } else if (!vn_min_rate) {
10457255736Sdavidch            /* If min rate is zero - set it to 100 */
10458255736Sdavidch            vn_min_rate = DEF_MIN_RATE;
10459255736Sdavidch        } else {
10460255736Sdavidch            all_zero = 0;
10461255736Sdavidch        }
10462255736Sdavidch
10463255736Sdavidch        input->vnic_min_rate[vn] = vn_min_rate;
10464255736Sdavidch    }
10465255736Sdavidch
10466255736Sdavidch    /* if ETS or all min rates are zeros - disable fairness */
10467255736Sdavidch    if (BXE_IS_ETS_ENABLED(sc)) {
10468255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10469255736Sdavidch        BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10470255736Sdavidch    } else if (all_zero) {
10471255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10472255736Sdavidch        BLOGD(sc, DBG_LOAD,
10473255736Sdavidch              "Fariness disabled (all MIN values are zeroes)\n");
10474255736Sdavidch    } else {
10475255736Sdavidch        input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10476255736Sdavidch    }
10477255736Sdavidch}
10478255736Sdavidch
10479255736Sdavidchstatic inline uint16_t
10480255736Sdavidchbxe_extract_max_cfg(struct bxe_softc *sc,
10481255736Sdavidch                    uint32_t         mf_cfg)
10482255736Sdavidch{
10483255736Sdavidch    uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10484255736Sdavidch                        FUNC_MF_CFG_MAX_BW_SHIFT);
10485255736Sdavidch
10486255736Sdavidch    if (!max_cfg) {
10487255736Sdavidch        BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10488255736Sdavidch        max_cfg = 100;
10489255736Sdavidch    }
10490255736Sdavidch
10491255736Sdavidch    return (max_cfg);
10492255736Sdavidch}
10493255736Sdavidch
10494255736Sdavidchstatic void
10495255736Sdavidchbxe_calc_vn_max(struct bxe_softc       *sc,
10496255736Sdavidch                int                    vn,
10497255736Sdavidch                struct cmng_init_input *input)
10498255736Sdavidch{
10499255736Sdavidch    uint16_t vn_max_rate;
10500255736Sdavidch    uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10501255736Sdavidch    uint32_t max_cfg;
10502255736Sdavidch
10503255736Sdavidch    if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10504255736Sdavidch        vn_max_rate = 0;
10505255736Sdavidch    } else {
10506255736Sdavidch        max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10507255736Sdavidch
10508255736Sdavidch        if (IS_MF_SI(sc)) {
10509255736Sdavidch            /* max_cfg in percents of linkspeed */
10510255736Sdavidch            vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10511255736Sdavidch        } else { /* SD modes */
10512255736Sdavidch            /* max_cfg is absolute in 100Mb units */
10513255736Sdavidch            vn_max_rate = (max_cfg * 100);
10514255736Sdavidch        }
10515255736Sdavidch    }
10516255736Sdavidch
10517255736Sdavidch    BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10518255736Sdavidch
10519255736Sdavidch    input->vnic_max_rate[vn] = vn_max_rate;
10520255736Sdavidch}
10521255736Sdavidch
10522255736Sdavidchstatic void
10523255736Sdavidchbxe_cmng_fns_init(struct bxe_softc *sc,
10524255736Sdavidch                  uint8_t          read_cfg,
10525255736Sdavidch                  uint8_t          cmng_type)
10526255736Sdavidch{
10527255736Sdavidch    struct cmng_init_input input;
10528255736Sdavidch    int vn;
10529255736Sdavidch
10530255736Sdavidch    memset(&input, 0, sizeof(struct cmng_init_input));
10531255736Sdavidch
10532255736Sdavidch    input.port_rate = sc->link_vars.line_speed;
10533255736Sdavidch
10534255736Sdavidch    if (cmng_type == CMNG_FNS_MINMAX) {
10535255736Sdavidch        /* read mf conf from shmem */
10536255736Sdavidch        if (read_cfg) {
10537255736Sdavidch            bxe_read_mf_cfg(sc);
10538255736Sdavidch        }
10539255736Sdavidch
10540255736Sdavidch        /* get VN min rate and enable fairness if not 0 */
10541255736Sdavidch        bxe_calc_vn_min(sc, &input);
10542255736Sdavidch
10543255736Sdavidch        /* get VN max rate */
10544255736Sdavidch        if (sc->port.pmf) {
10545255736Sdavidch            for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10546255736Sdavidch                bxe_calc_vn_max(sc, vn, &input);
10547255736Sdavidch            }
10548255736Sdavidch        }
10549255736Sdavidch
10550255736Sdavidch        /* always enable rate shaping and fairness */
10551255736Sdavidch        input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10552255736Sdavidch
10553255736Sdavidch        ecore_init_cmng(&input, &sc->cmng);
10554255736Sdavidch        return;
10555255736Sdavidch    }
10556255736Sdavidch
10557255736Sdavidch    /* rate shaping and fairness are disabled */
10558255736Sdavidch    BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10559255736Sdavidch}
10560255736Sdavidch
10561255736Sdavidchstatic int
10562255736Sdavidchbxe_get_cmng_fns_mode(struct bxe_softc *sc)
10563255736Sdavidch{
10564255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10565255736Sdavidch        return (CMNG_FNS_NONE);
10566255736Sdavidch    }
10567255736Sdavidch
10568255736Sdavidch    if (IS_MF(sc)) {
10569255736Sdavidch        return (CMNG_FNS_MINMAX);
10570255736Sdavidch    }
10571255736Sdavidch
10572255736Sdavidch    return (CMNG_FNS_NONE);
10573255736Sdavidch}
10574255736Sdavidch
10575255736Sdavidchstatic void
10576255736Sdavidchstorm_memset_cmng(struct bxe_softc *sc,
10577255736Sdavidch                  struct cmng_init *cmng,
10578255736Sdavidch                  uint8_t          port)
10579255736Sdavidch{
10580255736Sdavidch    int vn;
10581255736Sdavidch    int func;
10582255736Sdavidch    uint32_t addr;
10583255736Sdavidch    size_t size;
10584255736Sdavidch
10585255736Sdavidch    addr = (BAR_XSTRORM_INTMEM +
10586255736Sdavidch            XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10587255736Sdavidch    size = sizeof(struct cmng_struct_per_port);
10588255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10589255736Sdavidch
10590255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10591255736Sdavidch        func = func_by_vn(sc, vn);
10592255736Sdavidch
10593255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10594255736Sdavidch                XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10595255736Sdavidch        size = sizeof(struct rate_shaping_vars_per_vn);
10596255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10597255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10598255736Sdavidch
10599255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10600255736Sdavidch                XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10601255736Sdavidch        size = sizeof(struct fairness_vars_per_vn);
10602255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10603255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10604255736Sdavidch    }
10605255736Sdavidch}
10606255736Sdavidch
10607255736Sdavidchstatic void
10608255736Sdavidchbxe_pf_init(struct bxe_softc *sc)
10609255736Sdavidch{
10610255736Sdavidch    struct bxe_func_init_params func_init = { 0 };
10611255736Sdavidch    struct event_ring_data eq_data = { { 0 } };
10612255736Sdavidch    uint16_t flags;
10613255736Sdavidch
10614255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10615255736Sdavidch        /* reset IGU PF statistics: MSIX + ATTN */
10616255736Sdavidch        /* PF */
10617255736Sdavidch        REG_WR(sc,
10618255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10619255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10620255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10621255736Sdavidch               0);
10622255736Sdavidch        /* ATTN */
10623255736Sdavidch        REG_WR(sc,
10624255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10625255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10626255736Sdavidch                (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10627255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10628255736Sdavidch               0);
10629255736Sdavidch    }
10630255736Sdavidch
10631255736Sdavidch    /* function setup flags */
10632255736Sdavidch    flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10633255736Sdavidch
10634255736Sdavidch    /*
10635255736Sdavidch     * This flag is relevant for E1x only.
10636255736Sdavidch     * E2 doesn't have a TPA configuration in a function level.
10637255736Sdavidch     */
10638266979Smarcel    flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10639255736Sdavidch
10640255736Sdavidch    func_init.func_flgs = flags;
10641255736Sdavidch    func_init.pf_id     = SC_FUNC(sc);
10642255736Sdavidch    func_init.func_id   = SC_FUNC(sc);
10643255736Sdavidch    func_init.spq_map   = sc->spq_dma.paddr;
10644255736Sdavidch    func_init.spq_prod  = sc->spq_prod_idx;
10645255736Sdavidch
10646255736Sdavidch    bxe_func_init(sc, &func_init);
10647255736Sdavidch
10648255736Sdavidch    memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10649255736Sdavidch
10650255736Sdavidch    /*
10651255736Sdavidch     * Congestion management values depend on the link rate.
10652255736Sdavidch     * There is no active link so initial link rate is set to 10Gbps.
10653255736Sdavidch     * When the link comes up the congestion management values are
10654255736Sdavidch     * re-calculated according to the actual link rate.
10655255736Sdavidch     */
10656255736Sdavidch    sc->link_vars.line_speed = SPEED_10000;
10657255736Sdavidch    bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10658255736Sdavidch
10659255736Sdavidch    /* Only the PMF sets the HW */
10660255736Sdavidch    if (sc->port.pmf) {
10661255736Sdavidch        storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10662255736Sdavidch    }
10663255736Sdavidch
10664255736Sdavidch    /* init Event Queue - PCI bus guarantees correct endainity */
10665255736Sdavidch    eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10666255736Sdavidch    eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10667255736Sdavidch    eq_data.producer     = sc->eq_prod;
10668255736Sdavidch    eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10669255736Sdavidch    eq_data.sb_id        = DEF_SB_ID;
10670255736Sdavidch    storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10671255736Sdavidch}
10672255736Sdavidch
10673255736Sdavidchstatic void
10674255736Sdavidchbxe_hc_int_enable(struct bxe_softc *sc)
10675255736Sdavidch{
10676255736Sdavidch    int port = SC_PORT(sc);
10677255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10678255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10679255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10680255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10681255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10682255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10683255736Sdavidch
10684255736Sdavidch    if (msix) {
10685255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10686255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0);
10687255736Sdavidch        val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10688255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10689255736Sdavidch        if (single_msix) {
10690255736Sdavidch            val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10691255736Sdavidch        }
10692255736Sdavidch    } else if (msi) {
10693255736Sdavidch        val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10694255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10695255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10696255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10697255736Sdavidch    } else {
10698255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10699255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10700255736Sdavidch                HC_CONFIG_0_REG_INT_LINE_EN_0 |
10701255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10702255736Sdavidch
10703255736Sdavidch        if (!CHIP_IS_E1(sc)) {
10704255736Sdavidch            BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10705255736Sdavidch                  val, port, addr);
10706255736Sdavidch
10707255736Sdavidch            REG_WR(sc, addr, val);
10708255736Sdavidch
10709255736Sdavidch            val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10710255736Sdavidch        }
10711255736Sdavidch    }
10712255736Sdavidch
10713255736Sdavidch    if (CHIP_IS_E1(sc)) {
10714255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10715255736Sdavidch    }
10716255736Sdavidch
10717255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10718255736Sdavidch          val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10719255736Sdavidch
10720255736Sdavidch    REG_WR(sc, addr, val);
10721255736Sdavidch
10722255736Sdavidch    /* ensure that HC_CONFIG is written before leading/trailing edge config */
10723255736Sdavidch    mb();
10724255736Sdavidch
10725255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10726255736Sdavidch        /* init leading/trailing edge */
10727255736Sdavidch        if (IS_MF(sc)) {
10728255736Sdavidch            val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10729255736Sdavidch            if (sc->port.pmf) {
10730255736Sdavidch                /* enable nig and gpio3 attention */
10731255736Sdavidch                val |= 0x1100;
10732255736Sdavidch            }
10733255736Sdavidch        } else {
10734255736Sdavidch            val = 0xffff;
10735255736Sdavidch        }
10736255736Sdavidch
10737255736Sdavidch        REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10738255736Sdavidch        REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10739255736Sdavidch    }
10740255736Sdavidch
10741255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10742255736Sdavidch    mb();
10743255736Sdavidch}
10744255736Sdavidch
10745255736Sdavidchstatic void
10746255736Sdavidchbxe_igu_int_enable(struct bxe_softc *sc)
10747255736Sdavidch{
10748255736Sdavidch    uint32_t val;
10749255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10750255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10751255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10752255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10753255736Sdavidch
10754255736Sdavidch    val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10755255736Sdavidch
10756255736Sdavidch    if (msix) {
10757255736Sdavidch        val &= ~(IGU_PF_CONF_INT_LINE_EN |
10758255736Sdavidch                 IGU_PF_CONF_SINGLE_ISR_EN);
10759255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10760255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN);
10761255736Sdavidch        if (single_msix) {
10762255736Sdavidch            val |= IGU_PF_CONF_SINGLE_ISR_EN;
10763255736Sdavidch        }
10764255736Sdavidch    } else if (msi) {
10765255736Sdavidch        val &= ~IGU_PF_CONF_INT_LINE_EN;
10766255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10767255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10768255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10769255736Sdavidch    } else {
10770255736Sdavidch        val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10771255736Sdavidch        val |= (IGU_PF_CONF_INT_LINE_EN |
10772255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10773255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10774255736Sdavidch    }
10775255736Sdavidch
10776255736Sdavidch    /* clean previous status - need to configure igu prior to ack*/
10777255736Sdavidch    if ((!msix) || single_msix) {
10778255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10779255736Sdavidch        bxe_ack_int(sc);
10780255736Sdavidch    }
10781255736Sdavidch
10782255736Sdavidch    val |= IGU_PF_CONF_FUNC_EN;
10783255736Sdavidch
10784255736Sdavidch    BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10785255736Sdavidch          val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10786255736Sdavidch
10787255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10788255736Sdavidch
10789255736Sdavidch    mb();
10790255736Sdavidch
10791255736Sdavidch    /* init leading/trailing edge */
10792255736Sdavidch    if (IS_MF(sc)) {
10793255736Sdavidch        val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10794255736Sdavidch        if (sc->port.pmf) {
10795255736Sdavidch            /* enable nig and gpio3 attention */
10796255736Sdavidch            val |= 0x1100;
10797255736Sdavidch        }
10798255736Sdavidch    } else {
10799255736Sdavidch        val = 0xffff;
10800255736Sdavidch    }
10801255736Sdavidch
10802255736Sdavidch    REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10803255736Sdavidch    REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10804255736Sdavidch
10805255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10806255736Sdavidch    mb();
10807255736Sdavidch}
10808255736Sdavidch
10809255736Sdavidchstatic void
10810255736Sdavidchbxe_int_enable(struct bxe_softc *sc)
10811255736Sdavidch{
10812255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10813255736Sdavidch        bxe_hc_int_enable(sc);
10814255736Sdavidch    } else {
10815255736Sdavidch        bxe_igu_int_enable(sc);
10816255736Sdavidch    }
10817255736Sdavidch}
10818255736Sdavidch
10819255736Sdavidchstatic void
10820255736Sdavidchbxe_hc_int_disable(struct bxe_softc *sc)
10821255736Sdavidch{
10822255736Sdavidch    int port = SC_PORT(sc);
10823255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10824255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10825255736Sdavidch
10826255736Sdavidch    /*
10827255736Sdavidch     * In E1 we must use only PCI configuration space to disable MSI/MSIX
10828255736Sdavidch     * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10829255736Sdavidch     * block
10830255736Sdavidch     */
10831255736Sdavidch    if (CHIP_IS_E1(sc)) {
10832255736Sdavidch        /*
10833255736Sdavidch         * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10834255736Sdavidch         * to prevent from HC sending interrupts after we exit the function
10835255736Sdavidch         */
10836255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10837255736Sdavidch
10838255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10839255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10840255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10841255736Sdavidch    } else {
10842255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10843255736Sdavidch                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10844255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10845255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10846255736Sdavidch    }
10847255736Sdavidch
10848255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10849255736Sdavidch
10850255736Sdavidch    /* flush all outstanding writes */
10851255736Sdavidch    mb();
10852255736Sdavidch
10853255736Sdavidch    REG_WR(sc, addr, val);
10854255736Sdavidch    if (REG_RD(sc, addr) != val) {
10855255736Sdavidch        BLOGE(sc, "proper val not read from HC IGU!\n");
10856255736Sdavidch    }
10857255736Sdavidch}
10858255736Sdavidch
10859255736Sdavidchstatic void
10860255736Sdavidchbxe_igu_int_disable(struct bxe_softc *sc)
10861255736Sdavidch{
10862255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10863255736Sdavidch
10864255736Sdavidch    val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10865255736Sdavidch             IGU_PF_CONF_INT_LINE_EN |
10866255736Sdavidch             IGU_PF_CONF_ATTN_BIT_EN);
10867255736Sdavidch
10868255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10869255736Sdavidch
10870255736Sdavidch    /* flush all outstanding writes */
10871255736Sdavidch    mb();
10872255736Sdavidch
10873255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10874255736Sdavidch    if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10875255736Sdavidch        BLOGE(sc, "proper val not read from IGU!\n");
10876255736Sdavidch    }
10877255736Sdavidch}
10878255736Sdavidch
10879255736Sdavidchstatic void
10880255736Sdavidchbxe_int_disable(struct bxe_softc *sc)
10881255736Sdavidch{
10882255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10883255736Sdavidch        bxe_hc_int_disable(sc);
10884255736Sdavidch    } else {
10885255736Sdavidch        bxe_igu_int_disable(sc);
10886255736Sdavidch    }
10887255736Sdavidch}
10888255736Sdavidch
10889255736Sdavidchstatic void
10890255736Sdavidchbxe_nic_init(struct bxe_softc *sc,
10891255736Sdavidch             int              load_code)
10892255736Sdavidch{
10893255736Sdavidch    int i;
10894255736Sdavidch
10895255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
10896255736Sdavidch        bxe_init_eth_fp(sc, i);
10897255736Sdavidch    }
10898255736Sdavidch
10899255736Sdavidch    rmb(); /* ensure status block indices were read */
10900255736Sdavidch
10901255736Sdavidch    bxe_init_rx_rings(sc);
10902255736Sdavidch    bxe_init_tx_rings(sc);
10903255736Sdavidch
10904255736Sdavidch    if (IS_VF(sc)) {
10905255736Sdavidch        return;
10906255736Sdavidch    }
10907255736Sdavidch
10908255736Sdavidch    /* initialize MOD_ABS interrupts */
10909255736Sdavidch    elink_init_mod_abs_int(sc, &sc->link_vars,
10910255736Sdavidch                           sc->devinfo.chip_id,
10911255736Sdavidch                           sc->devinfo.shmem_base,
10912255736Sdavidch                           sc->devinfo.shmem2_base,
10913255736Sdavidch                           SC_PORT(sc));
10914255736Sdavidch
10915255736Sdavidch    bxe_init_def_sb(sc);
10916255736Sdavidch    bxe_update_dsb_idx(sc);
10917255736Sdavidch    bxe_init_sp_ring(sc);
10918255736Sdavidch    bxe_init_eq_ring(sc);
10919255736Sdavidch    bxe_init_internal(sc, load_code);
10920255736Sdavidch    bxe_pf_init(sc);
10921255736Sdavidch    bxe_stats_init(sc);
10922255736Sdavidch
10923255736Sdavidch    /* flush all before enabling interrupts */
10924255736Sdavidch    mb();
10925255736Sdavidch
10926255736Sdavidch    bxe_int_enable(sc);
10927255736Sdavidch
10928255736Sdavidch    /* check for SPIO5 */
10929255736Sdavidch    bxe_attn_int_deasserted0(sc,
10930255736Sdavidch                             REG_RD(sc,
10931255736Sdavidch                                    (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10932255736Sdavidch                                     SC_PORT(sc)*4)) &
10933255736Sdavidch                             AEU_INPUTS_ATTN_BITS_SPIO5);
10934255736Sdavidch}
10935255736Sdavidch
10936255736Sdavidchstatic inline void
10937255736Sdavidchbxe_init_objs(struct bxe_softc *sc)
10938255736Sdavidch{
10939255736Sdavidch    /* mcast rules must be added to tx if tx switching is enabled */
10940255736Sdavidch    ecore_obj_type o_type =
10941255736Sdavidch        (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10942255736Sdavidch                                         ECORE_OBJ_TYPE_RX;
10943255736Sdavidch
10944255736Sdavidch    /* RX_MODE controlling object */
10945255736Sdavidch    ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10946255736Sdavidch
10947255736Sdavidch    /* multicast configuration controlling object */
10948255736Sdavidch    ecore_init_mcast_obj(sc,
10949255736Sdavidch                         &sc->mcast_obj,
10950255736Sdavidch                         sc->fp[0].cl_id,
10951255736Sdavidch                         sc->fp[0].index,
10952255736Sdavidch                         SC_FUNC(sc),
10953255736Sdavidch                         SC_FUNC(sc),
10954255736Sdavidch                         BXE_SP(sc, mcast_rdata),
10955255736Sdavidch                         BXE_SP_MAPPING(sc, mcast_rdata),
10956255736Sdavidch                         ECORE_FILTER_MCAST_PENDING,
10957255736Sdavidch                         &sc->sp_state,
10958255736Sdavidch                         o_type);
10959255736Sdavidch
10960255736Sdavidch    /* Setup CAM credit pools */
10961255736Sdavidch    ecore_init_mac_credit_pool(sc,
10962255736Sdavidch                               &sc->macs_pool,
10963255736Sdavidch                               SC_FUNC(sc),
10964255736Sdavidch                               CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10965255736Sdavidch                                                 VNICS_PER_PATH(sc));
10966255736Sdavidch
10967255736Sdavidch    ecore_init_vlan_credit_pool(sc,
10968255736Sdavidch                                &sc->vlans_pool,
10969255736Sdavidch                                SC_ABS_FUNC(sc) >> 1,
10970255736Sdavidch                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10971255736Sdavidch                                                  VNICS_PER_PATH(sc));
10972255736Sdavidch
10973255736Sdavidch    /* RSS configuration object */
10974255736Sdavidch    ecore_init_rss_config_obj(sc,
10975255736Sdavidch                              &sc->rss_conf_obj,
10976255736Sdavidch                              sc->fp[0].cl_id,
10977255736Sdavidch                              sc->fp[0].index,
10978255736Sdavidch                              SC_FUNC(sc),
10979255736Sdavidch                              SC_FUNC(sc),
10980255736Sdavidch                              BXE_SP(sc, rss_rdata),
10981255736Sdavidch                              BXE_SP_MAPPING(sc, rss_rdata),
10982255736Sdavidch                              ECORE_FILTER_RSS_CONF_PENDING,
10983255736Sdavidch                              &sc->sp_state, ECORE_OBJ_TYPE_RX);
10984255736Sdavidch}
10985255736Sdavidch
10986255736Sdavidch/*
10987255736Sdavidch * Initialize the function. This must be called before sending CLIENT_SETUP
10988255736Sdavidch * for the first client.
10989255736Sdavidch */
10990255736Sdavidchstatic inline int
10991255736Sdavidchbxe_func_start(struct bxe_softc *sc)
10992255736Sdavidch{
10993255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
10994255736Sdavidch    struct ecore_func_start_params *start_params = &func_params.params.start;
10995255736Sdavidch
10996255736Sdavidch    /* Prepare parameters for function state transitions */
10997255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10998255736Sdavidch
10999255736Sdavidch    func_params.f_obj = &sc->func_obj;
11000255736Sdavidch    func_params.cmd = ECORE_F_CMD_START;
11001255736Sdavidch
11002255736Sdavidch    /* Function parameters */
11003255736Sdavidch    start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
11004255736Sdavidch    start_params->sd_vlan_tag = OVLAN(sc);
11005255736Sdavidch
11006255736Sdavidch    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
11007255736Sdavidch        start_params->network_cos_mode = STATIC_COS;
11008255736Sdavidch    } else { /* CHIP_IS_E1X */
11009255736Sdavidch        start_params->network_cos_mode = FW_WRR;
11010255736Sdavidch    }
11011255736Sdavidch
11012255736Sdavidch    start_params->gre_tunnel_mode = 0;
11013255736Sdavidch    start_params->gre_tunnel_rss  = 0;
11014255736Sdavidch
11015255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
11016255736Sdavidch}
11017255736Sdavidch
11018255736Sdavidchstatic int
11019255736Sdavidchbxe_set_power_state(struct bxe_softc *sc,
11020255736Sdavidch                    uint8_t          state)
11021255736Sdavidch{
11022255736Sdavidch    uint16_t pmcsr;
11023255736Sdavidch
11024255736Sdavidch    /* If there is no power capability, silently succeed */
11025255736Sdavidch    if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
11026255736Sdavidch        BLOGW(sc, "No power capability\n");
11027255736Sdavidch        return (0);
11028255736Sdavidch    }
11029255736Sdavidch
11030255736Sdavidch    pmcsr = pci_read_config(sc->dev,
11031255736Sdavidch                            (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
11032255736Sdavidch                            2);
11033255736Sdavidch
11034255736Sdavidch    switch (state) {
11035255736Sdavidch    case PCI_PM_D0:
11036255736Sdavidch        pci_write_config(sc->dev,
11037255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
11038255736Sdavidch                         ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
11039255736Sdavidch
11040255736Sdavidch        if (pmcsr & PCIM_PSTAT_DMASK) {
11041255736Sdavidch            /* delay required during transition out of D3hot */
11042255736Sdavidch            DELAY(20000);
11043255736Sdavidch        }
11044255736Sdavidch
11045255736Sdavidch        break;
11046255736Sdavidch
11047255736Sdavidch    case PCI_PM_D3hot:
11048255736Sdavidch        /* XXX if there are other clients above don't shut down the power */
11049255736Sdavidch
11050255736Sdavidch        /* don't shut down the power for emulation and FPGA */
11051255736Sdavidch        if (CHIP_REV_IS_SLOW(sc)) {
11052255736Sdavidch            return (0);
11053255736Sdavidch        }
11054255736Sdavidch
11055255736Sdavidch        pmcsr &= ~PCIM_PSTAT_DMASK;
11056255736Sdavidch        pmcsr |= PCIM_PSTAT_D3;
11057255736Sdavidch
11058255736Sdavidch        if (sc->wol) {
11059255736Sdavidch            pmcsr |= PCIM_PSTAT_PMEENABLE;
11060255736Sdavidch        }
11061255736Sdavidch
11062255736Sdavidch        pci_write_config(sc->dev,
11063255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
11064255736Sdavidch                         pmcsr, 4);
11065255736Sdavidch
11066255736Sdavidch        /*
11067255736Sdavidch         * No more memory access after this point until device is brought back
11068255736Sdavidch         * to D0 state.
11069255736Sdavidch         */
11070255736Sdavidch        break;
11071255736Sdavidch
11072255736Sdavidch    default:
11073255736Sdavidch        BLOGE(sc, "Can't support PCI power state = %d\n", state);
11074255736Sdavidch        return (-1);
11075255736Sdavidch    }
11076255736Sdavidch
11077255736Sdavidch    return (0);
11078255736Sdavidch}
11079255736Sdavidch
11080255736Sdavidch
11081255736Sdavidch/* return true if succeeded to acquire the lock */
11082255736Sdavidchstatic uint8_t
11083255736Sdavidchbxe_trylock_hw_lock(struct bxe_softc *sc,
11084255736Sdavidch                    uint32_t         resource)
11085255736Sdavidch{
11086255736Sdavidch    uint32_t lock_status;
11087255736Sdavidch    uint32_t resource_bit = (1 << resource);
11088255736Sdavidch    int func = SC_FUNC(sc);
11089255736Sdavidch    uint32_t hw_lock_control_reg;
11090255736Sdavidch
11091255736Sdavidch    BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
11092255736Sdavidch
11093255736Sdavidch    /* Validating that the resource is within range */
11094255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
11095255736Sdavidch        BLOGD(sc, DBG_LOAD,
11096255736Sdavidch              "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
11097255736Sdavidch              resource, HW_LOCK_MAX_RESOURCE_VALUE);
11098255736Sdavidch        return (FALSE);
11099255736Sdavidch    }
11100255736Sdavidch
11101255736Sdavidch    if (func <= 5) {
11102255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
11103255736Sdavidch    } else {
11104255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
11105255736Sdavidch    }
11106255736Sdavidch
11107255736Sdavidch    /* try to acquire the lock */
11108255736Sdavidch    REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
11109255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
11110255736Sdavidch    if (lock_status & resource_bit) {
11111255736Sdavidch        return (TRUE);
11112255736Sdavidch    }
11113255736Sdavidch
11114255736Sdavidch    BLOGE(sc, "Failed to get a resource lock 0x%x\n", resource);
11115255736Sdavidch
11116255736Sdavidch    return (FALSE);
11117255736Sdavidch}
11118255736Sdavidch
11119255736Sdavidch/*
11120255736Sdavidch * Get the recovery leader resource id according to the engine this function
11121255736Sdavidch * belongs to. Currently only only 2 engines is supported.
11122255736Sdavidch */
11123255736Sdavidchstatic int
11124255736Sdavidchbxe_get_leader_lock_resource(struct bxe_softc *sc)
11125255736Sdavidch{
11126255736Sdavidch    if (SC_PATH(sc)) {
11127255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
11128255736Sdavidch    } else {
11129255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
11130255736Sdavidch    }
11131255736Sdavidch}
11132255736Sdavidch
11133255736Sdavidch/* try to acquire a leader lock for current engine */
11134255736Sdavidchstatic uint8_t
11135255736Sdavidchbxe_trylock_leader_lock(struct bxe_softc *sc)
11136255736Sdavidch{
11137255736Sdavidch    return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
11138255736Sdavidch}
11139255736Sdavidch
11140255736Sdavidchstatic int
11141255736Sdavidchbxe_release_leader_lock(struct bxe_softc *sc)
11142255736Sdavidch{
11143255736Sdavidch    return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
11144255736Sdavidch}
11145255736Sdavidch
11146255736Sdavidch/* close gates #2, #3 and #4 */
11147255736Sdavidchstatic void
11148255736Sdavidchbxe_set_234_gates(struct bxe_softc *sc,
11149255736Sdavidch                  uint8_t          close)
11150255736Sdavidch{
11151255736Sdavidch    uint32_t val;
11152255736Sdavidch
11153255736Sdavidch    /* gates #2 and #4a are closed/opened for "not E1" only */
11154255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11155255736Sdavidch        /* #4 */
11156255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
11157255736Sdavidch        /* #2 */
11158255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
11159255736Sdavidch    }
11160255736Sdavidch
11161255736Sdavidch    /* #3 */
11162255736Sdavidch    if (CHIP_IS_E1x(sc)) {
11163255736Sdavidch        /* prevent interrupts from HC on both ports */
11164255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_1);
11165255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_1,
11166255736Sdavidch               (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
11167255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
11168255736Sdavidch
11169255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_0);
11170255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_0,
11171255736Sdavidch               (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
11172255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
11173255736Sdavidch    } else {
11174255736Sdavidch        /* Prevent incomming interrupts in IGU */
11175255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
11176255736Sdavidch
11177255736Sdavidch        REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
11178255736Sdavidch               (!close) ?
11179255736Sdavidch               (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
11180255736Sdavidch               (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
11181255736Sdavidch    }
11182255736Sdavidch
11183255736Sdavidch    BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
11184255736Sdavidch          close ? "closing" : "opening");
11185255736Sdavidch
11186255736Sdavidch    wmb();
11187255736Sdavidch}
11188255736Sdavidch
11189255736Sdavidch/* poll for pending writes bit, it should get cleared in no more than 1s */
11190255736Sdavidchstatic int
11191255736Sdavidchbxe_er_poll_igu_vq(struct bxe_softc *sc)
11192255736Sdavidch{
11193255736Sdavidch    uint32_t cnt = 1000;
11194255736Sdavidch    uint32_t pend_bits = 0;
11195255736Sdavidch
11196255736Sdavidch    do {
11197255736Sdavidch        pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
11198255736Sdavidch
11199255736Sdavidch        if (pend_bits == 0) {
11200255736Sdavidch            break;
11201255736Sdavidch        }
11202255736Sdavidch
11203255736Sdavidch        DELAY(1000);
11204255736Sdavidch    } while (--cnt > 0);
11205255736Sdavidch
11206255736Sdavidch    if (cnt == 0) {
11207255736Sdavidch        BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
11208255736Sdavidch        return (-1);
11209255736Sdavidch    }
11210255736Sdavidch
11211255736Sdavidch    return (0);
11212255736Sdavidch}
11213255736Sdavidch
11214255736Sdavidch#define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
11215255736Sdavidch
11216255736Sdavidchstatic void
11217255736Sdavidchbxe_clp_reset_prep(struct bxe_softc *sc,
11218255736Sdavidch                   uint32_t         *magic_val)
11219255736Sdavidch{
11220255736Sdavidch    /* Do some magic... */
11221255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
11222255736Sdavidch    *magic_val = val & SHARED_MF_CLP_MAGIC;
11223255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
11224255736Sdavidch}
11225255736Sdavidch
11226255736Sdavidch/* restore the value of the 'magic' bit */
11227255736Sdavidchstatic void
11228255736Sdavidchbxe_clp_reset_done(struct bxe_softc *sc,
11229255736Sdavidch                   uint32_t         magic_val)
11230255736Sdavidch{
11231255736Sdavidch    /* Restore the 'magic' bit value... */
11232255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
11233255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb,
11234255736Sdavidch              (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
11235255736Sdavidch}
11236255736Sdavidch
11237255736Sdavidch/* prepare for MCP reset, takes care of CLP configurations */
11238255736Sdavidchstatic void
11239255736Sdavidchbxe_reset_mcp_prep(struct bxe_softc *sc,
11240255736Sdavidch                   uint32_t         *magic_val)
11241255736Sdavidch{
11242255736Sdavidch    uint32_t shmem;
11243255736Sdavidch    uint32_t validity_offset;
11244255736Sdavidch
11245255736Sdavidch    /* set `magic' bit in order to save MF config */
11246255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11247255736Sdavidch        bxe_clp_reset_prep(sc, magic_val);
11248255736Sdavidch    }
11249255736Sdavidch
11250255736Sdavidch    /* get shmem offset */
11251255736Sdavidch    shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
11252255736Sdavidch    validity_offset =
11253255736Sdavidch        offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
11254255736Sdavidch
11255255736Sdavidch    /* Clear validity map flags */
11256255736Sdavidch    if (shmem > 0) {
11257255736Sdavidch        REG_WR(sc, shmem + validity_offset, 0);
11258255736Sdavidch    }
11259255736Sdavidch}
11260255736Sdavidch
11261255736Sdavidch#define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
11262255736Sdavidch#define MCP_ONE_TIMEOUT  100    /* 100 ms */
11263255736Sdavidch
11264255736Sdavidchstatic void
11265255736Sdavidchbxe_mcp_wait_one(struct bxe_softc *sc)
11266255736Sdavidch{
11267255736Sdavidch    /* special handling for emulation and FPGA (10 times longer) */
11268255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
11269255736Sdavidch        DELAY((MCP_ONE_TIMEOUT*10) * 1000);
11270255736Sdavidch    } else {
11271255736Sdavidch        DELAY((MCP_ONE_TIMEOUT) * 1000);
11272255736Sdavidch    }
11273255736Sdavidch}
11274255736Sdavidch
11275255736Sdavidch/* initialize shmem_base and waits for validity signature to appear */
11276255736Sdavidchstatic int
11277255736Sdavidchbxe_init_shmem(struct bxe_softc *sc)
11278255736Sdavidch{
11279255736Sdavidch    int cnt = 0;
11280255736Sdavidch    uint32_t val = 0;
11281255736Sdavidch
11282255736Sdavidch    do {
11283255736Sdavidch        sc->devinfo.shmem_base     =
11284255736Sdavidch        sc->link_params.shmem_base =
11285255736Sdavidch            REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
11286255736Sdavidch
11287255736Sdavidch        if (sc->devinfo.shmem_base) {
11288255736Sdavidch            val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
11289255736Sdavidch            if (val & SHR_MEM_VALIDITY_MB)
11290255736Sdavidch                return (0);
11291255736Sdavidch        }
11292255736Sdavidch
11293255736Sdavidch        bxe_mcp_wait_one(sc);
11294255736Sdavidch
11295255736Sdavidch    } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
11296255736Sdavidch
11297255736Sdavidch    BLOGE(sc, "BAD MCP validity signature\n");
11298255736Sdavidch
11299255736Sdavidch    return (-1);
11300255736Sdavidch}
11301255736Sdavidch
11302255736Sdavidchstatic int
11303255736Sdavidchbxe_reset_mcp_comp(struct bxe_softc *sc,
11304255736Sdavidch                   uint32_t         magic_val)
11305255736Sdavidch{
11306255736Sdavidch    int rc = bxe_init_shmem(sc);
11307255736Sdavidch
11308255736Sdavidch    /* Restore the `magic' bit value */
11309255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11310255736Sdavidch        bxe_clp_reset_done(sc, magic_val);
11311255736Sdavidch    }
11312255736Sdavidch
11313255736Sdavidch    return (rc);
11314255736Sdavidch}
11315255736Sdavidch
11316255736Sdavidchstatic void
11317255736Sdavidchbxe_pxp_prep(struct bxe_softc *sc)
11318255736Sdavidch{
11319255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11320255736Sdavidch        REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
11321255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
11322255736Sdavidch        wmb();
11323255736Sdavidch    }
11324255736Sdavidch}
11325255736Sdavidch
11326255736Sdavidch/*
11327255736Sdavidch * Reset the whole chip except for:
11328255736Sdavidch *      - PCIE core
11329255736Sdavidch *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
11330255736Sdavidch *      - IGU
11331255736Sdavidch *      - MISC (including AEU)
11332255736Sdavidch *      - GRC
11333255736Sdavidch *      - RBCN, RBCP
11334255736Sdavidch */
11335255736Sdavidchstatic void
11336255736Sdavidchbxe_process_kill_chip_reset(struct bxe_softc *sc,
11337255736Sdavidch                            uint8_t          global)
11338255736Sdavidch{
11339255736Sdavidch    uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
11340255736Sdavidch    uint32_t global_bits2, stay_reset2;
11341255736Sdavidch
11342255736Sdavidch    /*
11343255736Sdavidch     * Bits that have to be set in reset_mask2 if we want to reset 'global'
11344255736Sdavidch     * (per chip) blocks.
11345255736Sdavidch     */
11346255736Sdavidch    global_bits2 =
11347255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
11348255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
11349255736Sdavidch
11350255736Sdavidch    /*
11351255736Sdavidch     * Don't reset the following blocks.
11352255736Sdavidch     * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
11353255736Sdavidch     *            reset, as in 4 port device they might still be owned
11354255736Sdavidch     *            by the MCP (there is only one leader per path).
11355255736Sdavidch     */
11356255736Sdavidch    not_reset_mask1 =
11357255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_HC |
11358255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXPV |
11359255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXP;
11360255736Sdavidch
11361255736Sdavidch    not_reset_mask2 =
11362255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
11363255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
11364255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
11365255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
11366255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_RBCN |
11367255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_GRC  |
11368255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
11369255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
11370255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_ATC |
11371255736Sdavidch        MISC_REGISTERS_RESET_REG_2_PGLC |
11372255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
11373255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
11374255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
11375255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
11376255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC0 |
11377255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC1;
11378255736Sdavidch
11379255736Sdavidch    /*
11380255736Sdavidch     * Keep the following blocks in reset:
11381255736Sdavidch     *  - all xxMACs are handled by the elink code.
11382255736Sdavidch     */
11383255736Sdavidch    stay_reset2 =
11384255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC |
11385255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
11386255736Sdavidch
11387255736Sdavidch    /* Full reset masks according to the chip */
11388255736Sdavidch    reset_mask1 = 0xffffffff;
11389255736Sdavidch
11390255736Sdavidch    if (CHIP_IS_E1(sc))
11391255736Sdavidch        reset_mask2 = 0xffff;
11392255736Sdavidch    else if (CHIP_IS_E1H(sc))
11393255736Sdavidch        reset_mask2 = 0x1ffff;
11394255736Sdavidch    else if (CHIP_IS_E2(sc))
11395255736Sdavidch        reset_mask2 = 0xfffff;
11396255736Sdavidch    else /* CHIP_IS_E3 */
11397255736Sdavidch        reset_mask2 = 0x3ffffff;
11398255736Sdavidch
11399255736Sdavidch    /* Don't reset global blocks unless we need to */
11400255736Sdavidch    if (!global)
11401255736Sdavidch        reset_mask2 &= ~global_bits2;
11402255736Sdavidch
11403255736Sdavidch    /*
11404255736Sdavidch     * In case of attention in the QM, we need to reset PXP
11405255736Sdavidch     * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
11406255736Sdavidch     * because otherwise QM reset would release 'close the gates' shortly
11407255736Sdavidch     * before resetting the PXP, then the PSWRQ would send a write
11408255736Sdavidch     * request to PGLUE. Then when PXP is reset, PGLUE would try to
11409255736Sdavidch     * read the payload data from PSWWR, but PSWWR would not
11410255736Sdavidch     * respond. The write queue in PGLUE would stuck, dmae commands
11411255736Sdavidch     * would not return. Therefore it's important to reset the second
11412255736Sdavidch     * reset register (containing the
11413255736Sdavidch     * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
11414255736Sdavidch     * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
11415255736Sdavidch     * bit).
11416255736Sdavidch     */
11417255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
11418255736Sdavidch           reset_mask2 & (~not_reset_mask2));
11419255736Sdavidch
11420255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
11421255736Sdavidch           reset_mask1 & (~not_reset_mask1));
11422255736Sdavidch
11423255736Sdavidch    mb();
11424255736Sdavidch    wmb();
11425255736Sdavidch
11426255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
11427255736Sdavidch           reset_mask2 & (~stay_reset2));
11428255736Sdavidch
11429255736Sdavidch    mb();
11430255736Sdavidch    wmb();
11431255736Sdavidch
11432255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
11433255736Sdavidch    wmb();
11434255736Sdavidch}
11435255736Sdavidch
11436255736Sdavidchstatic int
11437255736Sdavidchbxe_process_kill(struct bxe_softc *sc,
11438255736Sdavidch                 uint8_t          global)
11439255736Sdavidch{
11440255736Sdavidch    int cnt = 1000;
11441255736Sdavidch    uint32_t val = 0;
11442255736Sdavidch    uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
11443255736Sdavidch    uint32_t tags_63_32 = 0;
11444255736Sdavidch
11445255736Sdavidch    /* Empty the Tetris buffer, wait for 1s */
11446255736Sdavidch    do {
11447255736Sdavidch        sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
11448255736Sdavidch        blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
11449255736Sdavidch        port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
11450255736Sdavidch        port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
11451255736Sdavidch        pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
11452255736Sdavidch        if (CHIP_IS_E3(sc)) {
11453255736Sdavidch            tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
11454255736Sdavidch        }
11455255736Sdavidch
11456255736Sdavidch        if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
11457255736Sdavidch            ((port_is_idle_0 & 0x1) == 0x1) &&
11458255736Sdavidch            ((port_is_idle_1 & 0x1) == 0x1) &&
11459255736Sdavidch            (pgl_exp_rom2 == 0xffffffff) &&
11460255736Sdavidch            (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11461255736Sdavidch            break;
11462255736Sdavidch        DELAY(1000);
11463255736Sdavidch    } while (cnt-- > 0);
11464255736Sdavidch
11465255736Sdavidch    if (cnt <= 0) {
11466255736Sdavidch        BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11467255736Sdavidch                  "are still outstanding read requests after 1s! "
11468255736Sdavidch                  "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11469255736Sdavidch                  "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11470255736Sdavidch              sr_cnt, blk_cnt, port_is_idle_0,
11471255736Sdavidch              port_is_idle_1, pgl_exp_rom2);
11472255736Sdavidch        return (-1);
11473255736Sdavidch    }
11474255736Sdavidch
11475255736Sdavidch    mb();
11476255736Sdavidch
11477255736Sdavidch    /* Close gates #2, #3 and #4 */
11478255736Sdavidch    bxe_set_234_gates(sc, TRUE);
11479255736Sdavidch
11480255736Sdavidch    /* Poll for IGU VQs for 57712 and newer chips */
11481255736Sdavidch    if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11482255736Sdavidch        return (-1);
11483255736Sdavidch    }
11484255736Sdavidch
11485255736Sdavidch    /* XXX indicate that "process kill" is in progress to MCP */
11486255736Sdavidch
11487255736Sdavidch    /* clear "unprepared" bit */
11488255736Sdavidch    REG_WR(sc, MISC_REG_UNPREPARED, 0);
11489255736Sdavidch    mb();
11490255736Sdavidch
11491255736Sdavidch    /* Make sure all is written to the chip before the reset */
11492255736Sdavidch    wmb();
11493255736Sdavidch
11494255736Sdavidch    /*
11495255736Sdavidch     * Wait for 1ms to empty GLUE and PCI-E core queues,
11496255736Sdavidch     * PSWHST, GRC and PSWRD Tetris buffer.
11497255736Sdavidch     */
11498255736Sdavidch    DELAY(1000);
11499255736Sdavidch
11500255736Sdavidch    /* Prepare to chip reset: */
11501255736Sdavidch    /* MCP */
11502255736Sdavidch    if (global) {
11503255736Sdavidch        bxe_reset_mcp_prep(sc, &val);
11504255736Sdavidch    }
11505255736Sdavidch
11506255736Sdavidch    /* PXP */
11507255736Sdavidch    bxe_pxp_prep(sc);
11508255736Sdavidch    mb();
11509255736Sdavidch
11510255736Sdavidch    /* reset the chip */
11511255736Sdavidch    bxe_process_kill_chip_reset(sc, global);
11512255736Sdavidch    mb();
11513255736Sdavidch
11514268854Sdavidcs    /* clear errors in PGB */
11515268854Sdavidcs    if (!CHIP_IS_E1(sc))
11516268854Sdavidcs        REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11517268854Sdavidcs
11518255736Sdavidch    /* Recover after reset: */
11519255736Sdavidch    /* MCP */
11520255736Sdavidch    if (global && bxe_reset_mcp_comp(sc, val)) {
11521255736Sdavidch        return (-1);
11522255736Sdavidch    }
11523255736Sdavidch
11524255736Sdavidch    /* XXX add resetting the NO_MCP mode DB here */
11525255736Sdavidch
11526255736Sdavidch    /* Open the gates #2, #3 and #4 */
11527255736Sdavidch    bxe_set_234_gates(sc, FALSE);
11528255736Sdavidch
11529255736Sdavidch    /* XXX
11530255736Sdavidch     * IGU/AEU preparation bring back the AEU/IGU to a reset state
11531255736Sdavidch     * re-enable attentions
11532255736Sdavidch     */
11533255736Sdavidch
11534255736Sdavidch    return (0);
11535255736Sdavidch}
11536255736Sdavidch
11537255736Sdavidchstatic int
11538255736Sdavidchbxe_leader_reset(struct bxe_softc *sc)
11539255736Sdavidch{
11540255736Sdavidch    int rc = 0;
11541255736Sdavidch    uint8_t global = bxe_reset_is_global(sc);
11542255736Sdavidch    uint32_t load_code;
11543255736Sdavidch
11544255736Sdavidch    /*
11545255736Sdavidch     * If not going to reset MCP, load "fake" driver to reset HW while
11546255736Sdavidch     * driver is owner of the HW.
11547255736Sdavidch     */
11548255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11549255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11550255736Sdavidch                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11551255736Sdavidch        if (!load_code) {
11552255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11553255736Sdavidch            rc = -1;
11554255736Sdavidch            goto exit_leader_reset;
11555255736Sdavidch        }
11556255736Sdavidch
11557255736Sdavidch        if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11558255736Sdavidch            (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11559255736Sdavidch            BLOGE(sc, "MCP unexpected response, aborting\n");
11560255736Sdavidch            rc = -1;
11561255736Sdavidch            goto exit_leader_reset2;
11562255736Sdavidch        }
11563255736Sdavidch
11564255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11565255736Sdavidch        if (!load_code) {
11566255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11567255736Sdavidch            rc = -1;
11568255736Sdavidch            goto exit_leader_reset2;
11569255736Sdavidch        }
11570255736Sdavidch    }
11571255736Sdavidch
11572255736Sdavidch    /* try to recover after the failure */
11573255736Sdavidch    if (bxe_process_kill(sc, global)) {
11574255736Sdavidch        BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11575255736Sdavidch        rc = -1;
11576255736Sdavidch        goto exit_leader_reset2;
11577255736Sdavidch    }
11578255736Sdavidch
11579255736Sdavidch    /*
11580255736Sdavidch     * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11581255736Sdavidch     * state.
11582255736Sdavidch     */
11583255736Sdavidch    bxe_set_reset_done(sc);
11584255736Sdavidch    if (global) {
11585255736Sdavidch        bxe_clear_reset_global(sc);
11586255736Sdavidch    }
11587255736Sdavidch
11588255736Sdavidchexit_leader_reset2:
11589255736Sdavidch
11590255736Sdavidch    /* unload "fake driver" if it was loaded */
11591255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11592255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11593255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11594255736Sdavidch    }
11595255736Sdavidch
11596255736Sdavidchexit_leader_reset:
11597255736Sdavidch
11598255736Sdavidch    sc->is_leader = 0;
11599255736Sdavidch    bxe_release_leader_lock(sc);
11600255736Sdavidch
11601255736Sdavidch    mb();
11602255736Sdavidch    return (rc);
11603255736Sdavidch}
11604255736Sdavidch
11605255736Sdavidch/*
11606255736Sdavidch * prepare INIT transition, parameters configured:
11607255736Sdavidch *   - HC configuration
11608255736Sdavidch *   - Queue's CDU context
11609255736Sdavidch */
11610255736Sdavidchstatic void
11611255736Sdavidchbxe_pf_q_prep_init(struct bxe_softc               *sc,
11612255736Sdavidch                   struct bxe_fastpath            *fp,
11613255736Sdavidch                   struct ecore_queue_init_params *init_params)
11614255736Sdavidch{
11615255736Sdavidch    uint8_t cos;
11616255736Sdavidch    int cxt_index, cxt_offset;
11617255736Sdavidch
11618255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11619255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11620255736Sdavidch
11621255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11622255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11623255736Sdavidch
11624255736Sdavidch    /* HC rate */
11625255736Sdavidch    init_params->rx.hc_rate =
11626255736Sdavidch        sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11627255736Sdavidch    init_params->tx.hc_rate =
11628255736Sdavidch        sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11629255736Sdavidch
11630255736Sdavidch    /* FW SB ID */
11631255736Sdavidch    init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11632255736Sdavidch
11633255736Sdavidch    /* CQ index among the SB indices */
11634255736Sdavidch    init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11635255736Sdavidch    init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11636255736Sdavidch
11637255736Sdavidch    /* set maximum number of COSs supported by this queue */
11638255736Sdavidch    init_params->max_cos = sc->max_cos;
11639255736Sdavidch
11640255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11641255736Sdavidch          fp->index, init_params->max_cos);
11642255736Sdavidch
11643255736Sdavidch    /* set the context pointers queue object */
11644255736Sdavidch    for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11645255736Sdavidch        /* XXX change index/cid here if ever support multiple tx CoS */
11646255736Sdavidch        /* fp->txdata[cos]->cid */
11647255736Sdavidch        cxt_index = fp->index / ILT_PAGE_CIDS;
11648255736Sdavidch        cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11649255736Sdavidch        init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11650255736Sdavidch    }
11651255736Sdavidch}
11652255736Sdavidch
11653255736Sdavidch/* set flags that are common for the Tx-only and not normal connections */
11654255736Sdavidchstatic unsigned long
11655255736Sdavidchbxe_get_common_flags(struct bxe_softc    *sc,
11656255736Sdavidch                     struct bxe_fastpath *fp,
11657255736Sdavidch                     uint8_t             zero_stats)
11658255736Sdavidch{
11659255736Sdavidch    unsigned long flags = 0;
11660255736Sdavidch
11661255736Sdavidch    /* PF driver will always initialize the Queue to an ACTIVE state */
11662255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11663255736Sdavidch
11664255736Sdavidch    /*
11665255736Sdavidch     * tx only connections collect statistics (on the same index as the
11666255736Sdavidch     * parent connection). The statistics are zeroed when the parent
11667255736Sdavidch     * connection is initialized.
11668255736Sdavidch     */
11669255736Sdavidch
11670255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11671255736Sdavidch    if (zero_stats) {
11672255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11673255736Sdavidch    }
11674255736Sdavidch
11675255736Sdavidch    /*
11676255736Sdavidch     * tx only connections can support tx-switching, though their
11677255736Sdavidch     * CoS-ness doesn't survive the loopback
11678255736Sdavidch     */
11679255736Sdavidch    if (sc->flags & BXE_TX_SWITCHING) {
11680255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11681255736Sdavidch    }
11682255736Sdavidch
11683255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11684255736Sdavidch
11685255736Sdavidch    return (flags);
11686255736Sdavidch}
11687255736Sdavidch
11688255736Sdavidchstatic unsigned long
11689255736Sdavidchbxe_get_q_flags(struct bxe_softc    *sc,
11690255736Sdavidch                struct bxe_fastpath *fp,
11691255736Sdavidch                uint8_t             leading)
11692255736Sdavidch{
11693255736Sdavidch    unsigned long flags = 0;
11694255736Sdavidch
11695255736Sdavidch    if (IS_MF_SD(sc)) {
11696255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11697255736Sdavidch    }
11698255736Sdavidch
11699266979Smarcel    if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11700255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11701255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11702255736Sdavidch#if 0
11703255736Sdavidch        if (fp->mode == TPA_MODE_GRO)
11704255736Sdavidch            __set_bit(ECORE_Q_FLG_TPA_GRO, &flags);
11705255736Sdavidch#endif
11706255736Sdavidch    }
11707255736Sdavidch
11708255736Sdavidch    if (leading) {
11709255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11710255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11711255736Sdavidch    }
11712255736Sdavidch
11713255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11714255736Sdavidch
11715255736Sdavidch#if 0
11716255736Sdavidch    /* configure silent vlan removal */
11717255736Sdavidch    if (IS_MF_AFEX(sc)) {
11718255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_SILENT_VLAN_REM, &flags);
11719255736Sdavidch    }
11720255736Sdavidch#endif
11721255736Sdavidch
11722255736Sdavidch    /* merge with common flags */
11723255736Sdavidch    return (flags | bxe_get_common_flags(sc, fp, TRUE));
11724255736Sdavidch}
11725255736Sdavidch
11726255736Sdavidchstatic void
11727255736Sdavidchbxe_pf_q_prep_general(struct bxe_softc                  *sc,
11728255736Sdavidch                      struct bxe_fastpath               *fp,
11729255736Sdavidch                      struct ecore_general_setup_params *gen_init,
11730255736Sdavidch                      uint8_t                           cos)
11731255736Sdavidch{
11732255736Sdavidch    gen_init->stat_id = bxe_stats_id(fp);
11733255736Sdavidch    gen_init->spcl_id = fp->cl_id;
11734255736Sdavidch    gen_init->mtu = sc->mtu;
11735255736Sdavidch    gen_init->cos = cos;
11736255736Sdavidch}
11737255736Sdavidch
11738255736Sdavidchstatic void
11739255736Sdavidchbxe_pf_rx_q_prep(struct bxe_softc              *sc,
11740255736Sdavidch                 struct bxe_fastpath           *fp,
11741255736Sdavidch                 struct rxq_pause_params       *pause,
11742255736Sdavidch                 struct ecore_rxq_setup_params *rxq_init)
11743255736Sdavidch{
11744255736Sdavidch    uint8_t max_sge = 0;
11745255736Sdavidch    uint16_t sge_sz = 0;
11746255736Sdavidch    uint16_t tpa_agg_size = 0;
11747255736Sdavidch
11748283274Sdavidcs    pause->sge_th_lo = SGE_TH_LO(sc);
11749283274Sdavidcs    pause->sge_th_hi = SGE_TH_HI(sc);
11750255736Sdavidch
11751283274Sdavidcs    /* validate SGE ring has enough to cross high threshold */
11752283274Sdavidcs    if (sc->dropless_fc &&
11753255736Sdavidch            (pause->sge_th_hi + FW_PREFETCH_CNT) >
11754255736Sdavidch            (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11755283274Sdavidcs        BLOGW(sc, "sge ring threshold limit\n");
11756283274Sdavidcs    }
11757255736Sdavidch
11758283274Sdavidcs    /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11759283274Sdavidcs    tpa_agg_size = (2 * sc->mtu);
11760283274Sdavidcs    if (tpa_agg_size < sc->max_aggregation_size) {
11761283274Sdavidcs        tpa_agg_size = sc->max_aggregation_size;
11762283274Sdavidcs    }
11763255736Sdavidch
11764283274Sdavidcs    max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11765283274Sdavidcs    max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11766255736Sdavidch                   (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11767283274Sdavidcs    sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11768255736Sdavidch
11769255736Sdavidch    /* pause - not for e1 */
11770255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11771255736Sdavidch        pause->bd_th_lo = BD_TH_LO(sc);
11772255736Sdavidch        pause->bd_th_hi = BD_TH_HI(sc);
11773255736Sdavidch
11774255736Sdavidch        pause->rcq_th_lo = RCQ_TH_LO(sc);
11775255736Sdavidch        pause->rcq_th_hi = RCQ_TH_HI(sc);
11776255736Sdavidch
11777255736Sdavidch        /* validate rings have enough entries to cross high thresholds */
11778255736Sdavidch        if (sc->dropless_fc &&
11779255736Sdavidch            pause->bd_th_hi + FW_PREFETCH_CNT >
11780255736Sdavidch            sc->rx_ring_size) {
11781255736Sdavidch            BLOGW(sc, "rx bd ring threshold limit\n");
11782255736Sdavidch        }
11783255736Sdavidch
11784255736Sdavidch        if (sc->dropless_fc &&
11785255736Sdavidch            pause->rcq_th_hi + FW_PREFETCH_CNT >
11786255736Sdavidch            RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11787255736Sdavidch            BLOGW(sc, "rcq ring threshold limit\n");
11788255736Sdavidch        }
11789255736Sdavidch
11790255736Sdavidch        pause->pri_map = 1;
11791255736Sdavidch    }
11792255736Sdavidch
11793255736Sdavidch    /* rxq setup */
11794255736Sdavidch    rxq_init->dscr_map   = fp->rx_dma.paddr;
11795255736Sdavidch    rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11796255736Sdavidch    rxq_init->rcq_map    = fp->rcq_dma.paddr;
11797255736Sdavidch    rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11798255736Sdavidch
11799255736Sdavidch    /*
11800255736Sdavidch     * This should be a maximum number of data bytes that may be
11801255736Sdavidch     * placed on the BD (not including paddings).
11802255736Sdavidch     */
11803255736Sdavidch    rxq_init->buf_sz = (fp->rx_buf_size -
11804255736Sdavidch                        IP_HEADER_ALIGNMENT_PADDING);
11805255736Sdavidch
11806255736Sdavidch    rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11807255736Sdavidch    rxq_init->tpa_agg_sz      = tpa_agg_size;
11808255736Sdavidch    rxq_init->sge_buf_sz      = sge_sz;
11809255736Sdavidch    rxq_init->max_sges_pkt    = max_sge;
11810255736Sdavidch    rxq_init->rss_engine_id   = SC_FUNC(sc);
11811255736Sdavidch    rxq_init->mcast_engine_id = SC_FUNC(sc);
11812255736Sdavidch
11813255736Sdavidch    /*
11814255736Sdavidch     * Maximum number or simultaneous TPA aggregation for this Queue.
11815255736Sdavidch     * For PF Clients it should be the maximum available number.
11816255736Sdavidch     * VF driver(s) may want to define it to a smaller value.
11817255736Sdavidch     */
11818255736Sdavidch    rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11819255736Sdavidch
11820255736Sdavidch    rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11821255736Sdavidch    rxq_init->fw_sb_id = fp->fw_sb_id;
11822255736Sdavidch
11823255736Sdavidch    rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11824255736Sdavidch
11825255736Sdavidch    /*
11826255736Sdavidch     * configure silent vlan removal
11827255736Sdavidch     * if multi function mode is afex, then mask default vlan
11828255736Sdavidch     */
11829255736Sdavidch    if (IS_MF_AFEX(sc)) {
11830255736Sdavidch        rxq_init->silent_removal_value =
11831255736Sdavidch            sc->devinfo.mf_info.afex_def_vlan_tag;
11832255736Sdavidch        rxq_init->silent_removal_mask = EVL_VLID_MASK;
11833255736Sdavidch    }
11834255736Sdavidch}
11835255736Sdavidch
11836255736Sdavidchstatic void
11837255736Sdavidchbxe_pf_tx_q_prep(struct bxe_softc              *sc,
11838255736Sdavidch                 struct bxe_fastpath           *fp,
11839255736Sdavidch                 struct ecore_txq_setup_params *txq_init,
11840255736Sdavidch                 uint8_t                       cos)
11841255736Sdavidch{
11842255736Sdavidch    /*
11843255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
11844255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11845255736Sdavidch     * fp->txdata[cos]->tx_dma.paddr;
11846255736Sdavidch     */
11847255736Sdavidch    txq_init->dscr_map     = fp->tx_dma.paddr;
11848255736Sdavidch    txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11849255736Sdavidch    txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11850255736Sdavidch    txq_init->fw_sb_id     = fp->fw_sb_id;
11851255736Sdavidch
11852255736Sdavidch    /*
11853255736Sdavidch     * set the TSS leading client id for TX classfication to the
11854255736Sdavidch     * leading RSS client id
11855255736Sdavidch     */
11856255736Sdavidch    txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11857255736Sdavidch}
11858255736Sdavidch
11859255736Sdavidch/*
11860255736Sdavidch * This function performs 2 steps in a queue state machine:
11861255736Sdavidch *   1) RESET->INIT
11862255736Sdavidch *   2) INIT->SETUP
11863255736Sdavidch */
11864255736Sdavidchstatic int
11865255736Sdavidchbxe_setup_queue(struct bxe_softc    *sc,
11866255736Sdavidch                struct bxe_fastpath *fp,
11867255736Sdavidch                uint8_t             leading)
11868255736Sdavidch{
11869255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
11870255736Sdavidch    struct ecore_queue_setup_params *setup_params =
11871255736Sdavidch                        &q_params.params.setup;
11872255736Sdavidch#if 0
11873255736Sdavidch    struct ecore_queue_setup_tx_only_params *tx_only_params =
11874255736Sdavidch                        &q_params.params.tx_only;
11875255736Sdavidch    uint8_t tx_index;
11876255736Sdavidch#endif
11877255736Sdavidch    int rc;
11878255736Sdavidch
11879255736Sdavidch    BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11880255736Sdavidch
11881255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11882255736Sdavidch
11883255736Sdavidch    q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11884255736Sdavidch
11885255736Sdavidch    /* we want to wait for completion in this context */
11886255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11887255736Sdavidch
11888255736Sdavidch    /* prepare the INIT parameters */
11889255736Sdavidch    bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11890255736Sdavidch
11891255736Sdavidch    /* Set the command */
11892255736Sdavidch    q_params.cmd = ECORE_Q_CMD_INIT;
11893255736Sdavidch
11894255736Sdavidch    /* Change the state to INIT */
11895255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11896255736Sdavidch    if (rc) {
11897255736Sdavidch        BLOGE(sc, "Queue(%d) INIT failed\n", fp->index);
11898255736Sdavidch        return (rc);
11899255736Sdavidch    }
11900255736Sdavidch
11901255736Sdavidch    BLOGD(sc, DBG_LOAD, "init complete\n");
11902255736Sdavidch
11903255736Sdavidch    /* now move the Queue to the SETUP state */
11904255736Sdavidch    memset(setup_params, 0, sizeof(*setup_params));
11905255736Sdavidch
11906255736Sdavidch    /* set Queue flags */
11907255736Sdavidch    setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11908255736Sdavidch
11909255736Sdavidch    /* set general SETUP parameters */
11910255736Sdavidch    bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11911255736Sdavidch                          FIRST_TX_COS_INDEX);
11912255736Sdavidch
11913255736Sdavidch    bxe_pf_rx_q_prep(sc, fp,
11914255736Sdavidch                     &setup_params->pause_params,
11915255736Sdavidch                     &setup_params->rxq_params);
11916255736Sdavidch
11917255736Sdavidch    bxe_pf_tx_q_prep(sc, fp,
11918255736Sdavidch                     &setup_params->txq_params,
11919255736Sdavidch                     FIRST_TX_COS_INDEX);
11920255736Sdavidch
11921255736Sdavidch    /* Set the command */
11922255736Sdavidch    q_params.cmd = ECORE_Q_CMD_SETUP;
11923255736Sdavidch
11924255736Sdavidch    /* change the state to SETUP */
11925255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11926255736Sdavidch    if (rc) {
11927255736Sdavidch        BLOGE(sc, "Queue(%d) SETUP failed\n", fp->index);
11928255736Sdavidch        return (rc);
11929255736Sdavidch    }
11930255736Sdavidch
11931255736Sdavidch#if 0
11932255736Sdavidch    /* loop through the relevant tx-only indices */
11933255736Sdavidch    for (tx_index = FIRST_TX_ONLY_COS_INDEX;
11934255736Sdavidch         tx_index < sc->max_cos;
11935255736Sdavidch         tx_index++) {
11936255736Sdavidch        /* prepare and send tx-only ramrod*/
11937255736Sdavidch        rc = bxe_setup_tx_only(sc, fp, &q_params,
11938255736Sdavidch                               tx_only_params, tx_index, leading);
11939255736Sdavidch        if (rc) {
11940255736Sdavidch            BLOGE(sc, "Queue(%d.%d) TX_ONLY_SETUP failed\n",
11941255736Sdavidch                  fp->index, tx_index);
11942255736Sdavidch            return (rc);
11943255736Sdavidch        }
11944255736Sdavidch    }
11945255736Sdavidch#endif
11946255736Sdavidch
11947255736Sdavidch    return (rc);
11948255736Sdavidch}
11949255736Sdavidch
11950255736Sdavidchstatic int
11951255736Sdavidchbxe_setup_leading(struct bxe_softc *sc)
11952255736Sdavidch{
11953255736Sdavidch    return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11954255736Sdavidch}
11955255736Sdavidch
11956255736Sdavidchstatic int
11957255736Sdavidchbxe_config_rss_pf(struct bxe_softc            *sc,
11958255736Sdavidch                  struct ecore_rss_config_obj *rss_obj,
11959255736Sdavidch                  uint8_t                     config_hash)
11960255736Sdavidch{
11961255736Sdavidch    struct ecore_config_rss_params params = { NULL };
11962255736Sdavidch    int i;
11963255736Sdavidch
11964255736Sdavidch    /*
11965255736Sdavidch     * Although RSS is meaningless when there is a single HW queue we
11966255736Sdavidch     * still need it enabled in order to have HW Rx hash generated.
11967255736Sdavidch     */
11968255736Sdavidch
11969255736Sdavidch    params.rss_obj = rss_obj;
11970255736Sdavidch
11971255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11972255736Sdavidch
11973255736Sdavidch    bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11974255736Sdavidch
11975255736Sdavidch    /* RSS configuration */
11976255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11977255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11978255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11979255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11980255736Sdavidch    if (rss_obj->udp_rss_v4) {
11981255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11982255736Sdavidch    }
11983255736Sdavidch    if (rss_obj->udp_rss_v6) {
11984255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11985255736Sdavidch    }
11986255736Sdavidch
11987255736Sdavidch    /* Hash bits */
11988255736Sdavidch    params.rss_result_mask = MULTI_MASK;
11989255736Sdavidch
11990255736Sdavidch    memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11991255736Sdavidch
11992255736Sdavidch    if (config_hash) {
11993255736Sdavidch        /* RSS keys */
11994255736Sdavidch        for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11995255736Sdavidch            params.rss_key[i] = arc4random();
11996255736Sdavidch        }
11997255736Sdavidch
11998255736Sdavidch        bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11999255736Sdavidch    }
12000255736Sdavidch
12001255736Sdavidch    return (ecore_config_rss(sc, &params));
12002255736Sdavidch}
12003255736Sdavidch
12004255736Sdavidchstatic int
12005255736Sdavidchbxe_config_rss_eth(struct bxe_softc *sc,
12006255736Sdavidch                   uint8_t          config_hash)
12007255736Sdavidch{
12008255736Sdavidch    return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
12009255736Sdavidch}
12010255736Sdavidch
12011255736Sdavidchstatic int
12012255736Sdavidchbxe_init_rss_pf(struct bxe_softc *sc)
12013255736Sdavidch{
12014255736Sdavidch    uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
12015255736Sdavidch    int i;
12016255736Sdavidch
12017255736Sdavidch    /*
12018255736Sdavidch     * Prepare the initial contents of the indirection table if
12019255736Sdavidch     * RSS is enabled
12020255736Sdavidch     */
12021255736Sdavidch    for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
12022255736Sdavidch        sc->rss_conf_obj.ind_table[i] =
12023255736Sdavidch            (sc->fp->cl_id + (i % num_eth_queues));
12024255736Sdavidch    }
12025255736Sdavidch
12026255736Sdavidch    if (sc->udp_rss) {
12027255736Sdavidch        sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
12028255736Sdavidch    }
12029255736Sdavidch
12030255736Sdavidch    /*
12031255736Sdavidch     * For 57710 and 57711 SEARCHER configuration (rss_keys) is
12032255736Sdavidch     * per-port, so if explicit configuration is needed, do it only
12033255736Sdavidch     * for a PMF.
12034255736Sdavidch     *
12035255736Sdavidch     * For 57712 and newer it's a per-function configuration.
12036255736Sdavidch     */
12037255736Sdavidch    return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
12038255736Sdavidch}
12039255736Sdavidch
12040255736Sdavidchstatic int
12041255736Sdavidchbxe_set_mac_one(struct bxe_softc          *sc,
12042255736Sdavidch                uint8_t                   *mac,
12043255736Sdavidch                struct ecore_vlan_mac_obj *obj,
12044255736Sdavidch                uint8_t                   set,
12045255736Sdavidch                int                       mac_type,
12046255736Sdavidch                unsigned long             *ramrod_flags)
12047255736Sdavidch{
12048255736Sdavidch    struct ecore_vlan_mac_ramrod_params ramrod_param;
12049255736Sdavidch    int rc;
12050255736Sdavidch
12051255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
12052255736Sdavidch
12053255736Sdavidch    /* fill in general parameters */
12054255736Sdavidch    ramrod_param.vlan_mac_obj = obj;
12055255736Sdavidch    ramrod_param.ramrod_flags = *ramrod_flags;
12056255736Sdavidch
12057255736Sdavidch    /* fill a user request section if needed */
12058255736Sdavidch    if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
12059255736Sdavidch        memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
12060255736Sdavidch
12061255736Sdavidch        bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
12062255736Sdavidch
12063255736Sdavidch        /* Set the command: ADD or DEL */
12064255736Sdavidch        ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
12065255736Sdavidch                                            ECORE_VLAN_MAC_DEL;
12066255736Sdavidch    }
12067255736Sdavidch
12068255736Sdavidch    rc = ecore_config_vlan_mac(sc, &ramrod_param);
12069255736Sdavidch
12070255736Sdavidch    if (rc == ECORE_EXISTS) {
12071255736Sdavidch        BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12072255736Sdavidch        /* do not treat adding same MAC as error */
12073255736Sdavidch        rc = 0;
12074255736Sdavidch    } else if (rc < 0) {
12075255736Sdavidch        BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
12076255736Sdavidch    }
12077255736Sdavidch
12078255736Sdavidch    return (rc);
12079255736Sdavidch}
12080255736Sdavidch
12081255736Sdavidchstatic int
12082255736Sdavidchbxe_set_eth_mac(struct bxe_softc *sc,
12083255736Sdavidch                uint8_t          set)
12084255736Sdavidch{
12085255736Sdavidch    unsigned long ramrod_flags = 0;
12086255736Sdavidch
12087255736Sdavidch    BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
12088255736Sdavidch
12089255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
12090255736Sdavidch
12091255736Sdavidch    /* Eth MAC is set on RSS leading client (fp[0]) */
12092255736Sdavidch    return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
12093255736Sdavidch                            &sc->sp_objs->mac_obj,
12094255736Sdavidch                            set, ECORE_ETH_MAC, &ramrod_flags));
12095255736Sdavidch}
12096255736Sdavidch
12097255736Sdavidch#if 0
12098255736Sdavidchstatic void
12099255736Sdavidchbxe_update_max_mf_config(struct bxe_softc *sc,
12100255736Sdavidch                         uint32_t         value)
12101255736Sdavidch{
12102255736Sdavidch    /* load old values */
12103255736Sdavidch    uint32_t mf_cfg = sc->devinfo.mf_info.mf_config[SC_VN(sc)];
12104255736Sdavidch
12105255736Sdavidch    if (value != bxe_extract_max_cfg(sc, mf_cfg)) {
12106255736Sdavidch        /* leave all but MAX value */
12107255736Sdavidch        mf_cfg &= ~FUNC_MF_CFG_MAX_BW_MASK;
12108255736Sdavidch
12109255736Sdavidch        /* set new MAX value */
12110255736Sdavidch        mf_cfg |= ((value << FUNC_MF_CFG_MAX_BW_SHIFT) &
12111255736Sdavidch                   FUNC_MF_CFG_MAX_BW_MASK);
12112255736Sdavidch
12113255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW, mf_cfg);
12114255736Sdavidch    }
12115255736Sdavidch}
12116255736Sdavidch#endif
12117255736Sdavidch
12118255736Sdavidchstatic int
12119255736Sdavidchbxe_get_cur_phy_idx(struct bxe_softc *sc)
12120255736Sdavidch{
12121255736Sdavidch    uint32_t sel_phy_idx = 0;
12122255736Sdavidch
12123255736Sdavidch    if (sc->link_params.num_phys <= 1) {
12124255736Sdavidch        return (ELINK_INT_PHY);
12125255736Sdavidch    }
12126255736Sdavidch
12127255736Sdavidch    if (sc->link_vars.link_up) {
12128255736Sdavidch        sel_phy_idx = ELINK_EXT_PHY1;
12129255736Sdavidch        /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
12130255736Sdavidch        if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
12131255736Sdavidch            (sc->link_params.phy[ELINK_EXT_PHY2].supported &
12132255736Sdavidch             ELINK_SUPPORTED_FIBRE))
12133255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
12134255736Sdavidch    } else {
12135255736Sdavidch        switch (elink_phy_selection(&sc->link_params)) {
12136255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
12137255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
12138255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
12139255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY1;
12140255736Sdavidch               break;
12141255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
12142255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
12143255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY2;
12144255736Sdavidch               break;
12145255736Sdavidch        }
12146255736Sdavidch    }
12147255736Sdavidch
12148255736Sdavidch    return (sel_phy_idx);
12149255736Sdavidch}
12150255736Sdavidch
12151255736Sdavidchstatic int
12152255736Sdavidchbxe_get_link_cfg_idx(struct bxe_softc *sc)
12153255736Sdavidch{
12154255736Sdavidch    uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
12155255736Sdavidch
12156255736Sdavidch    /*
12157255736Sdavidch     * The selected activated PHY is always after swapping (in case PHY
12158255736Sdavidch     * swapping is enabled). So when swapping is enabled, we need to reverse
12159255736Sdavidch     * the configuration
12160255736Sdavidch     */
12161255736Sdavidch
12162255736Sdavidch    if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
12163255736Sdavidch        if (sel_phy_idx == ELINK_EXT_PHY1)
12164255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
12165255736Sdavidch        else if (sel_phy_idx == ELINK_EXT_PHY2)
12166255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY1;
12167255736Sdavidch    }
12168255736Sdavidch
12169255736Sdavidch    return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
12170255736Sdavidch}
12171255736Sdavidch
12172255736Sdavidchstatic void
12173255736Sdavidchbxe_set_requested_fc(struct bxe_softc *sc)
12174255736Sdavidch{
12175255736Sdavidch    /*
12176255736Sdavidch     * Initialize link parameters structure variables
12177255736Sdavidch     * It is recommended to turn off RX FC for jumbo frames
12178255736Sdavidch     * for better performance
12179255736Sdavidch     */
12180255736Sdavidch    if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
12181255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
12182255736Sdavidch    } else {
12183255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
12184255736Sdavidch    }
12185255736Sdavidch}
12186255736Sdavidch
12187255736Sdavidchstatic void
12188255736Sdavidchbxe_calc_fc_adv(struct bxe_softc *sc)
12189255736Sdavidch{
12190255736Sdavidch    uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
12191255736Sdavidch    switch (sc->link_vars.ieee_fc &
12192255736Sdavidch            MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
12193255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
12194255736Sdavidch    default:
12195255736Sdavidch        sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
12196255736Sdavidch                                           ADVERTISED_Pause);
12197255736Sdavidch        break;
12198255736Sdavidch
12199255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
12200255736Sdavidch        sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
12201255736Sdavidch                                          ADVERTISED_Pause);
12202255736Sdavidch        break;
12203255736Sdavidch
12204255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
12205255736Sdavidch        sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
12206255736Sdavidch        break;
12207255736Sdavidch    }
12208255736Sdavidch}
12209255736Sdavidch
12210255736Sdavidchstatic uint16_t
12211255736Sdavidchbxe_get_mf_speed(struct bxe_softc *sc)
12212255736Sdavidch{
12213255736Sdavidch    uint16_t line_speed = sc->link_vars.line_speed;
12214255736Sdavidch    if (IS_MF(sc)) {
12215255736Sdavidch        uint16_t maxCfg =
12216255736Sdavidch            bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
12217255736Sdavidch
12218255736Sdavidch        /* calculate the current MAX line speed limit for the MF devices */
12219255736Sdavidch        if (IS_MF_SI(sc)) {
12220255736Sdavidch            line_speed = (line_speed * maxCfg) / 100;
12221255736Sdavidch        } else { /* SD mode */
12222255736Sdavidch            uint16_t vn_max_rate = maxCfg * 100;
12223255736Sdavidch
12224255736Sdavidch            if (vn_max_rate < line_speed) {
12225255736Sdavidch                line_speed = vn_max_rate;
12226255736Sdavidch            }
12227255736Sdavidch        }
12228255736Sdavidch    }
12229255736Sdavidch
12230255736Sdavidch    return (line_speed);
12231255736Sdavidch}
12232255736Sdavidch
12233255736Sdavidchstatic void
12234255736Sdavidchbxe_fill_report_data(struct bxe_softc            *sc,
12235255736Sdavidch                     struct bxe_link_report_data *data)
12236255736Sdavidch{
12237255736Sdavidch    uint16_t line_speed = bxe_get_mf_speed(sc);
12238255736Sdavidch
12239255736Sdavidch    memset(data, 0, sizeof(*data));
12240255736Sdavidch
12241255736Sdavidch    /* fill the report data with the effective line speed */
12242255736Sdavidch    data->line_speed = line_speed;
12243255736Sdavidch
12244255736Sdavidch    /* Link is down */
12245255736Sdavidch    if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
12246255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
12247255736Sdavidch    }
12248255736Sdavidch
12249255736Sdavidch    /* Full DUPLEX */
12250255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
12251255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
12252255736Sdavidch    }
12253255736Sdavidch
12254255736Sdavidch    /* Rx Flow Control is ON */
12255255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
12256255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
12257255736Sdavidch    }
12258255736Sdavidch
12259255736Sdavidch    /* Tx Flow Control is ON */
12260255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
12261255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
12262255736Sdavidch    }
12263255736Sdavidch}
12264255736Sdavidch
12265255736Sdavidch/* report link status to OS, should be called under phy_lock */
12266255736Sdavidchstatic void
12267255736Sdavidchbxe_link_report_locked(struct bxe_softc *sc)
12268255736Sdavidch{
12269255736Sdavidch    struct bxe_link_report_data cur_data;
12270255736Sdavidch
12271255736Sdavidch    /* reread mf_cfg */
12272255736Sdavidch    if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
12273255736Sdavidch        bxe_read_mf_cfg(sc);
12274255736Sdavidch    }
12275255736Sdavidch
12276255736Sdavidch    /* Read the current link report info */
12277255736Sdavidch    bxe_fill_report_data(sc, &cur_data);
12278255736Sdavidch
12279255736Sdavidch    /* Don't report link down or exactly the same link status twice */
12280255736Sdavidch    if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
12281255736Sdavidch        (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
12282255736Sdavidch                      &sc->last_reported_link.link_report_flags) &&
12283255736Sdavidch         bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
12284255736Sdavidch                      &cur_data.link_report_flags))) {
12285255736Sdavidch        return;
12286255736Sdavidch    }
12287255736Sdavidch
12288255736Sdavidch    sc->link_cnt++;
12289255736Sdavidch
12290255736Sdavidch    /* report new link params and remember the state for the next time */
12291255736Sdavidch    memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
12292255736Sdavidch
12293255736Sdavidch    if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
12294255736Sdavidch                     &cur_data.link_report_flags)) {
12295270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_DOWN);
12296255736Sdavidch        BLOGI(sc, "NIC Link is Down\n");
12297255736Sdavidch    } else {
12298255736Sdavidch        const char *duplex;
12299255736Sdavidch        const char *flow;
12300255736Sdavidch
12301255736Sdavidch        if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
12302255736Sdavidch                                   &cur_data.link_report_flags)) {
12303255736Sdavidch            duplex = "full";
12304255736Sdavidch        } else {
12305255736Sdavidch            duplex = "half";
12306255736Sdavidch        }
12307255736Sdavidch
12308255736Sdavidch        /*
12309255736Sdavidch         * Handle the FC at the end so that only these flags would be
12310255736Sdavidch         * possibly set. This way we may easily check if there is no FC
12311255736Sdavidch         * enabled.
12312255736Sdavidch         */
12313255736Sdavidch        if (cur_data.link_report_flags) {
12314255736Sdavidch            if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
12315255736Sdavidch                             &cur_data.link_report_flags) &&
12316255736Sdavidch                bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
12317255736Sdavidch                             &cur_data.link_report_flags)) {
12318255736Sdavidch                flow = "ON - receive & transmit";
12319255736Sdavidch            } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
12320255736Sdavidch                                    &cur_data.link_report_flags) &&
12321255736Sdavidch                       !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
12322255736Sdavidch                                     &cur_data.link_report_flags)) {
12323255736Sdavidch                flow = "ON - receive";
12324255736Sdavidch            } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
12325255736Sdavidch                                     &cur_data.link_report_flags) &&
12326255736Sdavidch                       bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
12327255736Sdavidch                                    &cur_data.link_report_flags)) {
12328255736Sdavidch                flow = "ON - transmit";
12329255736Sdavidch            } else {
12330255736Sdavidch                flow = "none"; /* possible? */
12331255736Sdavidch            }
12332255736Sdavidch        } else {
12333255736Sdavidch            flow = "none";
12334255736Sdavidch        }
12335255736Sdavidch
12336270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_UP);
12337255736Sdavidch        BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
12338255736Sdavidch              cur_data.line_speed, duplex, flow);
12339255736Sdavidch    }
12340255736Sdavidch}
12341255736Sdavidch
12342255736Sdavidchstatic void
12343255736Sdavidchbxe_link_report(struct bxe_softc *sc)
12344255736Sdavidch{
12345284335Sdavidcs    bxe_acquire_phy_lock(sc);
12346255736Sdavidch    bxe_link_report_locked(sc);
12347284335Sdavidcs    bxe_release_phy_lock(sc);
12348255736Sdavidch}
12349255736Sdavidch
12350255736Sdavidchstatic void
12351255736Sdavidchbxe_link_status_update(struct bxe_softc *sc)
12352255736Sdavidch{
12353255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
12354255736Sdavidch        return;
12355255736Sdavidch    }
12356255736Sdavidch
12357255736Sdavidch#if 0
12358255736Sdavidch    /* read updated dcb configuration */
12359255736Sdavidch    if (IS_PF(sc))
12360255736Sdavidch        bxe_dcbx_pmf_update(sc);
12361255736Sdavidch#endif
12362255736Sdavidch
12363255736Sdavidch    if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
12364255736Sdavidch        elink_link_status_update(&sc->link_params, &sc->link_vars);
12365255736Sdavidch    } else {
12366255736Sdavidch        sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
12367255736Sdavidch                                  ELINK_SUPPORTED_10baseT_Full |
12368255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Half |
12369255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Full |
12370255736Sdavidch                                  ELINK_SUPPORTED_1000baseT_Full |
12371255736Sdavidch                                  ELINK_SUPPORTED_2500baseX_Full |
12372255736Sdavidch                                  ELINK_SUPPORTED_10000baseT_Full |
12373255736Sdavidch                                  ELINK_SUPPORTED_TP |
12374255736Sdavidch                                  ELINK_SUPPORTED_FIBRE |
12375255736Sdavidch                                  ELINK_SUPPORTED_Autoneg |
12376255736Sdavidch                                  ELINK_SUPPORTED_Pause |
12377255736Sdavidch                                  ELINK_SUPPORTED_Asym_Pause);
12378255736Sdavidch        sc->port.advertising[0] = sc->port.supported[0];
12379255736Sdavidch
12380255736Sdavidch        sc->link_params.sc                = sc;
12381255736Sdavidch        sc->link_params.port              = SC_PORT(sc);
12382255736Sdavidch        sc->link_params.req_duplex[0]     = DUPLEX_FULL;
12383255736Sdavidch        sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
12384255736Sdavidch        sc->link_params.req_line_speed[0] = SPEED_10000;
12385255736Sdavidch        sc->link_params.speed_cap_mask[0] = 0x7f0000;
12386255736Sdavidch        sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
12387255736Sdavidch
12388255736Sdavidch        if (CHIP_REV_IS_FPGA(sc)) {
12389255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
12390255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_1000;
12391255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
12392255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
12393255736Sdavidch        } else {
12394255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
12395255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_10000;
12396255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
12397255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
12398255736Sdavidch        }
12399255736Sdavidch
12400255736Sdavidch        sc->link_vars.link_up = 1;
12401255736Sdavidch
12402255736Sdavidch        sc->link_vars.duplex    = DUPLEX_FULL;
12403255736Sdavidch        sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
12404255736Sdavidch
12405255736Sdavidch        if (IS_PF(sc)) {
12406255736Sdavidch            REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
12407255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12408255736Sdavidch            bxe_link_report(sc);
12409255736Sdavidch        }
12410255736Sdavidch    }
12411255736Sdavidch
12412255736Sdavidch    if (IS_PF(sc)) {
12413255736Sdavidch        if (sc->link_vars.link_up) {
12414255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12415255736Sdavidch        } else {
12416255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_STOP);
12417255736Sdavidch        }
12418255736Sdavidch        bxe_link_report(sc);
12419255736Sdavidch    } else {
12420255736Sdavidch        bxe_link_report(sc);
12421255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12422255736Sdavidch    }
12423255736Sdavidch}
12424255736Sdavidch
12425255736Sdavidchstatic int
12426255736Sdavidchbxe_initial_phy_init(struct bxe_softc *sc,
12427255736Sdavidch                     int              load_mode)
12428255736Sdavidch{
12429255736Sdavidch    int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
12430255736Sdavidch    uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
12431255736Sdavidch    struct elink_params *lp = &sc->link_params;
12432255736Sdavidch
12433255736Sdavidch    bxe_set_requested_fc(sc);
12434255736Sdavidch
12435255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
12436255736Sdavidch        uint32_t bond = CHIP_BOND_ID(sc);
12437255736Sdavidch        uint32_t feat = 0;
12438255736Sdavidch
12439255736Sdavidch        if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
12440255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12441255736Sdavidch        } else if (bond & 0x4) {
12442255736Sdavidch            if (CHIP_IS_E3(sc)) {
12443255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
12444255736Sdavidch            } else {
12445255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12446255736Sdavidch            }
12447255736Sdavidch        } else if (bond & 0x8) {
12448255736Sdavidch            if (CHIP_IS_E3(sc)) {
12449255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
12450255736Sdavidch            } else {
12451255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12452255736Sdavidch            }
12453255736Sdavidch        }
12454255736Sdavidch
12455255736Sdavidch        /* disable EMAC for E3 and above */
12456255736Sdavidch        if (bond & 0x2) {
12457255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12458255736Sdavidch        }
12459255736Sdavidch
12460255736Sdavidch        sc->link_params.feature_config_flags |= feat;
12461255736Sdavidch    }
12462255736Sdavidch
12463284335Sdavidcs    bxe_acquire_phy_lock(sc);
12464255736Sdavidch
12465255736Sdavidch    if (load_mode == LOAD_DIAG) {
12466255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_XGXS;
12467255736Sdavidch        /* Prefer doing PHY loopback at 10G speed, if possible */
12468255736Sdavidch        if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
12469255736Sdavidch            if (lp->speed_cap_mask[cfg_idx] &
12470255736Sdavidch                PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
12471255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
12472255736Sdavidch            } else {
12473255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
12474255736Sdavidch            }
12475255736Sdavidch        }
12476255736Sdavidch    }
12477255736Sdavidch
12478255736Sdavidch    if (load_mode == LOAD_LOOPBACK_EXT) {
12479255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_EXT;
12480255736Sdavidch    }
12481255736Sdavidch
12482255736Sdavidch    rc = elink_phy_init(&sc->link_params, &sc->link_vars);
12483255736Sdavidch
12484284335Sdavidcs    bxe_release_phy_lock(sc);
12485255736Sdavidch
12486255736Sdavidch    bxe_calc_fc_adv(sc);
12487255736Sdavidch
12488255736Sdavidch    if (sc->link_vars.link_up) {
12489255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12490255736Sdavidch        bxe_link_report(sc);
12491255736Sdavidch    }
12492255736Sdavidch
12493255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12494255736Sdavidch        bxe_periodic_start(sc);
12495255736Sdavidch    }
12496255736Sdavidch
12497255736Sdavidch    sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
12498255736Sdavidch    return (rc);
12499255736Sdavidch}
12500255736Sdavidch
12501255736Sdavidch/* must be called under IF_ADDR_LOCK */
12502284651Sdavidcs
12503255736Sdavidchstatic int
12504284651Sdavidcsbxe_set_mc_list(struct bxe_softc *sc)
12505255736Sdavidch{
12506284651Sdavidcs    struct ecore_mcast_ramrod_params rparam = { NULL };
12507284651Sdavidcs    int rc = 0;
12508255736Sdavidch    int mc_count = 0;
12509266979Smarcel    int mcnt, i;
12510284651Sdavidcs    struct ecore_mcast_list_elem *mc_mac, *mc_mac_start;
12511266979Smarcel    unsigned char *mta;
12512284651Sdavidcs    if_t ifp = sc->ifp;
12513255736Sdavidch
12514266979Smarcel    mc_count = if_multiaddr_count(ifp, -1);/* XXX they don't have a limit */
12515284651Sdavidcs    if (!mc_count)
12516255736Sdavidch        return (0);
12517255736Sdavidch
12518266979Smarcel    mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN *
12519266979Smarcel            mc_count, M_DEVBUF, M_NOWAIT);
12520266979Smarcel
12521266979Smarcel    if(mta == NULL) {
12522266979Smarcel        BLOGE(sc, "Failed to allocate temp mcast list\n");
12523266979Smarcel        return (-1);
12524266979Smarcel    }
12525284470Sdavidcs    bzero(mta, (sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count));
12526266979Smarcel
12527284651Sdavidcs    mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF, (M_NOWAIT | M_ZERO));
12528284651Sdavidcs    mc_mac_start = mc_mac;
12529284651Sdavidcs
12530255736Sdavidch    if (!mc_mac) {
12531266979Smarcel        free(mta, M_DEVBUF);
12532255736Sdavidch        BLOGE(sc, "Failed to allocate temp mcast list\n");
12533255736Sdavidch        return (-1);
12534255736Sdavidch    }
12535284470Sdavidcs    bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12536255736Sdavidch
12537284651Sdavidcs    /* mta and mcnt not expected to be  different */
12538284651Sdavidcs    if_multiaddr_array(ifp, mta, &mcnt, mc_count);
12539284651Sdavidcs
12540284651Sdavidcs
12541284651Sdavidcs    rparam.mcast_obj = &sc->mcast_obj;
12542284651Sdavidcs    ECORE_LIST_INIT(&rparam.mcast_list);
12543284651Sdavidcs
12544266979Smarcel    for(i=0; i< mcnt; i++) {
12545255736Sdavidch
12546284651Sdavidcs        mc_mac->mac = (uint8_t *)(mta + (i * ETHER_ADDR_LEN));
12547284651Sdavidcs        ECORE_LIST_PUSH_TAIL(&mc_mac->link, &rparam.mcast_list);
12548255736Sdavidch
12549255736Sdavidch        BLOGD(sc, DBG_LOAD,
12550255736Sdavidch              "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
12551255736Sdavidch              mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12552255736Sdavidch              mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
12553255736Sdavidch
12554255736Sdavidch        mc_mac++;
12555255736Sdavidch    }
12556284651Sdavidcs    rparam.mcast_list_len = mc_count;
12557255736Sdavidch
12558255736Sdavidch    BXE_MCAST_LOCK(sc);
12559255736Sdavidch
12560255736Sdavidch    /* first, clear all configured multicast MACs */
12561255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12562255736Sdavidch    if (rc < 0) {
12563255736Sdavidch        BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12564284470Sdavidcs        BXE_MCAST_UNLOCK(sc);
12565284651Sdavidcs    	free(mc_mac_start, M_DEVBUF);
12566284651Sdavidcs        free(mta, M_DEVBUF);
12567255736Sdavidch        return (rc);
12568255736Sdavidch    }
12569255736Sdavidch
12570255736Sdavidch    /* Now add the new MACs */
12571255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12572255736Sdavidch    if (rc < 0) {
12573255736Sdavidch        BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12574255736Sdavidch    }
12575255736Sdavidch
12576255736Sdavidch    BXE_MCAST_UNLOCK(sc);
12577255736Sdavidch
12578284651Sdavidcs    free(mc_mac_start, M_DEVBUF);
12579284651Sdavidcs    free(mta, M_DEVBUF);
12580284651Sdavidcs
12581255736Sdavidch    return (rc);
12582255736Sdavidch}
12583255736Sdavidch
12584255736Sdavidchstatic int
12585255736Sdavidchbxe_set_uc_list(struct bxe_softc *sc)
12586255736Sdavidch{
12587266979Smarcel    if_t ifp = sc->ifp;
12588255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12589255736Sdavidch    struct ifaddr *ifa;
12590255736Sdavidch    unsigned long ramrod_flags = 0;
12591255736Sdavidch    int rc;
12592255736Sdavidch
12593255736Sdavidch#if __FreeBSD_version < 800000
12594255736Sdavidch    IF_ADDR_LOCK(ifp);
12595255736Sdavidch#else
12596270876Sglebius    if_addr_rlock(ifp);
12597255736Sdavidch#endif
12598255736Sdavidch
12599255736Sdavidch    /* first schedule a cleanup up of old configuration */
12600255736Sdavidch    rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12601255736Sdavidch    if (rc < 0) {
12602255736Sdavidch        BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12603255736Sdavidch#if __FreeBSD_version < 800000
12604255736Sdavidch        IF_ADDR_UNLOCK(ifp);
12605255736Sdavidch#else
12606270876Sglebius        if_addr_runlock(ifp);
12607255736Sdavidch#endif
12608255736Sdavidch        return (rc);
12609255736Sdavidch    }
12610255736Sdavidch
12611266979Smarcel    ifa = if_getifaddr(ifp); /* XXX Is this structure */
12612255736Sdavidch    while (ifa) {
12613255736Sdavidch        if (ifa->ifa_addr->sa_family != AF_LINK) {
12614255736Sdavidch            ifa = TAILQ_NEXT(ifa, ifa_link);
12615255736Sdavidch            continue;
12616255736Sdavidch        }
12617255736Sdavidch
12618255736Sdavidch        rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12619255736Sdavidch                             mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12620255736Sdavidch        if (rc == -EEXIST) {
12621255736Sdavidch            BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12622255736Sdavidch            /* do not treat adding same MAC as an error */
12623255736Sdavidch            rc = 0;
12624255736Sdavidch        } else if (rc < 0) {
12625255736Sdavidch            BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12626255736Sdavidch#if __FreeBSD_version < 800000
12627255736Sdavidch            IF_ADDR_UNLOCK(ifp);
12628255736Sdavidch#else
12629270876Sglebius            if_addr_runlock(ifp);
12630255736Sdavidch#endif
12631255736Sdavidch            return (rc);
12632255736Sdavidch        }
12633255736Sdavidch
12634255736Sdavidch        ifa = TAILQ_NEXT(ifa, ifa_link);
12635255736Sdavidch    }
12636255736Sdavidch
12637255736Sdavidch#if __FreeBSD_version < 800000
12638255736Sdavidch    IF_ADDR_UNLOCK(ifp);
12639255736Sdavidch#else
12640270876Sglebius    if_addr_runlock(ifp);
12641255736Sdavidch#endif
12642255736Sdavidch
12643255736Sdavidch    /* Execute the pending commands */
12644255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
12645255736Sdavidch    return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12646255736Sdavidch                            ECORE_UC_LIST_MAC, &ramrod_flags));
12647255736Sdavidch}
12648255736Sdavidch
12649255736Sdavidchstatic void
12650284335Sdavidcsbxe_set_rx_mode(struct bxe_softc *sc)
12651255736Sdavidch{
12652266979Smarcel    if_t ifp = sc->ifp;
12653255736Sdavidch    uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12654255736Sdavidch
12655255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
12656255736Sdavidch        BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12657255736Sdavidch        return;
12658255736Sdavidch    }
12659255736Sdavidch
12660266979Smarcel    BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12661255736Sdavidch
12662266979Smarcel    if (if_getflags(ifp) & IFF_PROMISC) {
12663255736Sdavidch        rx_mode = BXE_RX_MODE_PROMISC;
12664266979Smarcel    } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12665266979Smarcel               ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12666255736Sdavidch                CHIP_IS_E1(sc))) {
12667255736Sdavidch        rx_mode = BXE_RX_MODE_ALLMULTI;
12668255736Sdavidch    } else {
12669255736Sdavidch        if (IS_PF(sc)) {
12670255736Sdavidch            /* some multicasts */
12671255736Sdavidch            if (bxe_set_mc_list(sc) < 0) {
12672255736Sdavidch                rx_mode = BXE_RX_MODE_ALLMULTI;
12673255736Sdavidch            }
12674255736Sdavidch            if (bxe_set_uc_list(sc) < 0) {
12675255736Sdavidch                rx_mode = BXE_RX_MODE_PROMISC;
12676255736Sdavidch            }
12677255736Sdavidch        }
12678255736Sdavidch#if 0
12679255736Sdavidch        else {
12680255736Sdavidch            /*
12681255736Sdavidch             * Configuring mcast to a VF involves sleeping (when we
12682255736Sdavidch             * wait for the PF's response). Since this function is
12683255736Sdavidch             * called from a non sleepable context we must schedule
12684255736Sdavidch             * a work item for this purpose
12685255736Sdavidch             */
12686255736Sdavidch            bxe_set_bit(BXE_SP_RTNL_VFPF_MCAST, &sc->sp_rtnl_state);
12687255736Sdavidch            schedule_delayed_work(&sc->sp_rtnl_task, 0);
12688255736Sdavidch        }
12689255736Sdavidch#endif
12690255736Sdavidch    }
12691255736Sdavidch
12692255736Sdavidch    sc->rx_mode = rx_mode;
12693255736Sdavidch
12694255736Sdavidch    /* schedule the rx_mode command */
12695255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12696255736Sdavidch        BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12697255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12698255736Sdavidch        return;
12699255736Sdavidch    }
12700255736Sdavidch
12701255736Sdavidch    if (IS_PF(sc)) {
12702255736Sdavidch        bxe_set_storm_rx_mode(sc);
12703255736Sdavidch    }
12704255736Sdavidch#if 0
12705255736Sdavidch    else {
12706255736Sdavidch        /*
12707255736Sdavidch         * Configuring mcast to a VF involves sleeping (when we
12708255736Sdavidch         * wait for the PF's response). Since this function is
12709255736Sdavidch         * called from a non sleepable context we must schedule
12710255736Sdavidch         * a work item for this purpose
12711255736Sdavidch         */
12712255736Sdavidch        bxe_set_bit(BXE_SP_RTNL_VFPF_STORM_RX_MODE, &sc->sp_rtnl_state);
12713255736Sdavidch        schedule_delayed_work(&sc->sp_rtnl_task, 0);
12714255736Sdavidch    }
12715255736Sdavidch#endif
12716255736Sdavidch
12717255736Sdavidch}
12718255736Sdavidch
12719255736Sdavidch
12720255736Sdavidch/* update flags in shmem */
12721255736Sdavidchstatic void
12722255736Sdavidchbxe_update_drv_flags(struct bxe_softc *sc,
12723255736Sdavidch                     uint32_t         flags,
12724255736Sdavidch                     uint32_t         set)
12725255736Sdavidch{
12726255736Sdavidch    uint32_t drv_flags;
12727255736Sdavidch
12728255736Sdavidch    if (SHMEM2_HAS(sc, drv_flags)) {
12729255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12730255736Sdavidch        drv_flags = SHMEM2_RD(sc, drv_flags);
12731255736Sdavidch
12732255736Sdavidch        if (set) {
12733255736Sdavidch            SET_FLAGS(drv_flags, flags);
12734255736Sdavidch        } else {
12735255736Sdavidch            RESET_FLAGS(drv_flags, flags);
12736255736Sdavidch        }
12737255736Sdavidch
12738255736Sdavidch        SHMEM2_WR(sc, drv_flags, drv_flags);
12739255736Sdavidch        BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12740255736Sdavidch
12741255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12742255736Sdavidch    }
12743255736Sdavidch}
12744255736Sdavidch
12745255736Sdavidch/* periodic timer callout routine, only runs when the interface is up */
12746255736Sdavidch
12747255736Sdavidchstatic void
12748255736Sdavidchbxe_periodic_callout_func(void *xsc)
12749255736Sdavidch{
12750255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12751255736Sdavidch    int i;
12752255736Sdavidch
12753255736Sdavidch    if (!BXE_CORE_TRYLOCK(sc)) {
12754255736Sdavidch        /* just bail and try again next time */
12755255736Sdavidch
12756255736Sdavidch        if ((sc->state == BXE_STATE_OPEN) &&
12757255736Sdavidch            (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12758255736Sdavidch            /* schedule the next periodic callout */
12759255736Sdavidch            callout_reset(&sc->periodic_callout, hz,
12760255736Sdavidch                          bxe_periodic_callout_func, sc);
12761255736Sdavidch        }
12762255736Sdavidch
12763255736Sdavidch        return;
12764255736Sdavidch    }
12765255736Sdavidch
12766255736Sdavidch    if ((sc->state != BXE_STATE_OPEN) ||
12767255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12768255736Sdavidch        BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12769255736Sdavidch        BXE_CORE_UNLOCK(sc);
12770255736Sdavidch        return;
12771255736Sdavidch    }
12772255736Sdavidch
12773255736Sdavidch    /* Check for TX timeouts on any fastpath. */
12774255736Sdavidch    FOR_EACH_QUEUE(sc, i) {
12775255736Sdavidch        if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12776255736Sdavidch            /* Ruh-Roh, chip was reset! */
12777255736Sdavidch            break;
12778255736Sdavidch        }
12779255736Sdavidch    }
12780255736Sdavidch
12781255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12782255736Sdavidch        /*
12783255736Sdavidch         * This barrier is needed to ensure the ordering between the writing
12784255736Sdavidch         * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12785255736Sdavidch         * the reading here.
12786255736Sdavidch         */
12787255736Sdavidch        mb();
12788255736Sdavidch        if (sc->port.pmf) {
12789284335Sdavidcs	    bxe_acquire_phy_lock(sc);
12790255736Sdavidch            elink_period_func(&sc->link_params, &sc->link_vars);
12791284335Sdavidcs	    bxe_release_phy_lock(sc);
12792255736Sdavidch        }
12793255736Sdavidch    }
12794255736Sdavidch
12795284335Sdavidcs    if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12796255736Sdavidch        int mb_idx = SC_FW_MB_IDX(sc);
12797255736Sdavidch        uint32_t drv_pulse;
12798255736Sdavidch        uint32_t mcp_pulse;
12799255736Sdavidch
12800255736Sdavidch        ++sc->fw_drv_pulse_wr_seq;
12801255736Sdavidch        sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12802255736Sdavidch
12803255736Sdavidch        drv_pulse = sc->fw_drv_pulse_wr_seq;
12804255736Sdavidch        bxe_drv_pulse(sc);
12805255736Sdavidch
12806255736Sdavidch        mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12807255736Sdavidch                     MCP_PULSE_SEQ_MASK);
12808255736Sdavidch
12809255736Sdavidch        /*
12810255736Sdavidch         * The delta between driver pulse and mcp response should
12811255736Sdavidch         * be 1 (before mcp response) or 0 (after mcp response).
12812255736Sdavidch         */
12813255736Sdavidch        if ((drv_pulse != mcp_pulse) &&
12814255736Sdavidch            (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12815255736Sdavidch            /* someone lost a heartbeat... */
12816255736Sdavidch            BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12817255736Sdavidch                  drv_pulse, mcp_pulse);
12818255736Sdavidch        }
12819255736Sdavidch    }
12820255736Sdavidch
12821255736Sdavidch    /* state is BXE_STATE_OPEN */
12822255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12823255736Sdavidch
12824255736Sdavidch#if 0
12825255736Sdavidch    /* sample VF bulletin board for new posts from PF */
12826255736Sdavidch    if (IS_VF(sc)) {
12827255736Sdavidch        bxe_sample_bulletin(sc);
12828255736Sdavidch    }
12829255736Sdavidch#endif
12830255736Sdavidch
12831255736Sdavidch    BXE_CORE_UNLOCK(sc);
12832255736Sdavidch
12833255736Sdavidch    if ((sc->state == BXE_STATE_OPEN) &&
12834255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12835255736Sdavidch        /* schedule the next periodic callout */
12836255736Sdavidch        callout_reset(&sc->periodic_callout, hz,
12837255736Sdavidch                      bxe_periodic_callout_func, sc);
12838255736Sdavidch    }
12839255736Sdavidch}
12840255736Sdavidch
12841255736Sdavidchstatic void
12842255736Sdavidchbxe_periodic_start(struct bxe_softc *sc)
12843255736Sdavidch{
12844255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12845255736Sdavidch    callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12846255736Sdavidch}
12847255736Sdavidch
12848255736Sdavidchstatic void
12849255736Sdavidchbxe_periodic_stop(struct bxe_softc *sc)
12850255736Sdavidch{
12851255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12852255736Sdavidch    callout_drain(&sc->periodic_callout);
12853255736Sdavidch}
12854255736Sdavidch
12855255736Sdavidch/* start the controller */
12856255736Sdavidchstatic __noinline int
12857255736Sdavidchbxe_nic_load(struct bxe_softc *sc,
12858255736Sdavidch             int              load_mode)
12859255736Sdavidch{
12860255736Sdavidch    uint32_t val;
12861255736Sdavidch    int load_code = 0;
12862255736Sdavidch    int i, rc = 0;
12863255736Sdavidch
12864255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12865255736Sdavidch
12866255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12867255736Sdavidch
12868255736Sdavidch    sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12869255736Sdavidch
12870255736Sdavidch    if (IS_PF(sc)) {
12871255736Sdavidch        /* must be called before memory allocation and HW init */
12872255736Sdavidch        bxe_ilt_set_info(sc);
12873255736Sdavidch    }
12874255736Sdavidch
12875255736Sdavidch    sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12876255736Sdavidch
12877255736Sdavidch    bxe_set_fp_rx_buf_size(sc);
12878255736Sdavidch
12879255736Sdavidch    if (bxe_alloc_fp_buffers(sc) != 0) {
12880255736Sdavidch        BLOGE(sc, "Failed to allocate fastpath memory\n");
12881255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12882255736Sdavidch        rc = ENOMEM;
12883255736Sdavidch        goto bxe_nic_load_error0;
12884255736Sdavidch    }
12885255736Sdavidch
12886255736Sdavidch    if (bxe_alloc_mem(sc) != 0) {
12887255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12888255736Sdavidch        rc = ENOMEM;
12889255736Sdavidch        goto bxe_nic_load_error0;
12890255736Sdavidch    }
12891255736Sdavidch
12892255736Sdavidch    if (bxe_alloc_fw_stats_mem(sc) != 0) {
12893255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12894255736Sdavidch        rc = ENOMEM;
12895255736Sdavidch        goto bxe_nic_load_error0;
12896255736Sdavidch    }
12897255736Sdavidch
12898255736Sdavidch    if (IS_PF(sc)) {
12899255736Sdavidch        /* set pf load just before approaching the MCP */
12900255736Sdavidch        bxe_set_pf_load(sc);
12901255736Sdavidch
12902255736Sdavidch        /* if MCP exists send load request and analyze response */
12903255736Sdavidch        if (!BXE_NOMCP(sc)) {
12904255736Sdavidch            /* attempt to load pf */
12905255736Sdavidch            if (bxe_nic_load_request(sc, &load_code) != 0) {
12906255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12907255736Sdavidch                rc = ENXIO;
12908255736Sdavidch                goto bxe_nic_load_error1;
12909255736Sdavidch            }
12910255736Sdavidch
12911255736Sdavidch            /* what did the MCP say? */
12912255736Sdavidch            if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12913255736Sdavidch                bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12914255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12915255736Sdavidch                rc = ENXIO;
12916255736Sdavidch                goto bxe_nic_load_error2;
12917255736Sdavidch            }
12918255736Sdavidch        } else {
12919255736Sdavidch            BLOGI(sc, "Device has no MCP!\n");
12920255736Sdavidch            load_code = bxe_nic_load_no_mcp(sc);
12921255736Sdavidch        }
12922255736Sdavidch
12923255736Sdavidch        /* mark PMF if applicable */
12924255736Sdavidch        bxe_nic_load_pmf(sc, load_code);
12925255736Sdavidch
12926255736Sdavidch        /* Init Function state controlling object */
12927255736Sdavidch        bxe_init_func_obj(sc);
12928255736Sdavidch
12929255736Sdavidch        /* Initialize HW */
12930255736Sdavidch        if (bxe_init_hw(sc, load_code) != 0) {
12931255736Sdavidch            BLOGE(sc, "HW init failed\n");
12932255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12933255736Sdavidch            sc->state = BXE_STATE_CLOSED;
12934255736Sdavidch            rc = ENXIO;
12935255736Sdavidch            goto bxe_nic_load_error2;
12936255736Sdavidch        }
12937255736Sdavidch    }
12938255736Sdavidch
12939284335Sdavidcs    /* set ALWAYS_ALIVE bit in shmem */
12940284335Sdavidcs    sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12941284335Sdavidcs    bxe_drv_pulse(sc);
12942284335Sdavidcs    sc->flags |= BXE_NO_PULSE;
12943284335Sdavidcs
12944255736Sdavidch    /* attach interrupts */
12945255736Sdavidch    if (bxe_interrupt_attach(sc) != 0) {
12946255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12947255736Sdavidch        rc = ENXIO;
12948255736Sdavidch        goto bxe_nic_load_error2;
12949255736Sdavidch    }
12950255736Sdavidch
12951255736Sdavidch    bxe_nic_init(sc, load_code);
12952255736Sdavidch
12953255736Sdavidch    /* Init per-function objects */
12954255736Sdavidch    if (IS_PF(sc)) {
12955255736Sdavidch        bxe_init_objs(sc);
12956255736Sdavidch        // XXX bxe_iov_nic_init(sc);
12957255736Sdavidch
12958255736Sdavidch        /* set AFEX default VLAN tag to an invalid value */
12959255736Sdavidch        sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12960255736Sdavidch        // XXX bxe_nic_load_afex_dcc(sc, load_code);
12961255736Sdavidch
12962255736Sdavidch        sc->state = BXE_STATE_OPENING_WAITING_PORT;
12963255736Sdavidch        rc = bxe_func_start(sc);
12964255736Sdavidch        if (rc) {
12965255736Sdavidch            BLOGE(sc, "Function start failed!\n");
12966255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12967255736Sdavidch            sc->state = BXE_STATE_ERROR;
12968255736Sdavidch            goto bxe_nic_load_error3;
12969255736Sdavidch        }
12970255736Sdavidch
12971255736Sdavidch        /* send LOAD_DONE command to MCP */
12972255736Sdavidch        if (!BXE_NOMCP(sc)) {
12973255736Sdavidch            load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12974255736Sdavidch            if (!load_code) {
12975255736Sdavidch                BLOGE(sc, "MCP response failure, aborting\n");
12976255736Sdavidch                sc->state = BXE_STATE_ERROR;
12977255736Sdavidch                rc = ENXIO;
12978255736Sdavidch                goto bxe_nic_load_error3;
12979255736Sdavidch            }
12980255736Sdavidch        }
12981255736Sdavidch
12982255736Sdavidch        rc = bxe_setup_leading(sc);
12983255736Sdavidch        if (rc) {
12984255736Sdavidch            BLOGE(sc, "Setup leading failed!\n");
12985255736Sdavidch            sc->state = BXE_STATE_ERROR;
12986255736Sdavidch            goto bxe_nic_load_error3;
12987255736Sdavidch        }
12988255736Sdavidch
12989255736Sdavidch        FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12990255736Sdavidch            rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12991255736Sdavidch            if (rc) {
12992255736Sdavidch                BLOGE(sc, "Queue(%d) setup failed\n", i);
12993255736Sdavidch                sc->state = BXE_STATE_ERROR;
12994255736Sdavidch                goto bxe_nic_load_error3;
12995255736Sdavidch            }
12996255736Sdavidch        }
12997255736Sdavidch
12998255736Sdavidch        rc = bxe_init_rss_pf(sc);
12999255736Sdavidch        if (rc) {
13000255736Sdavidch            BLOGE(sc, "PF RSS init failed\n");
13001255736Sdavidch            sc->state = BXE_STATE_ERROR;
13002255736Sdavidch            goto bxe_nic_load_error3;
13003255736Sdavidch        }
13004255736Sdavidch    }
13005255736Sdavidch    /* XXX VF */
13006255736Sdavidch#if 0
13007255736Sdavidch    else { /* VF */
13008255736Sdavidch        FOR_EACH_ETH_QUEUE(sc, i) {
13009255736Sdavidch            rc = bxe_vfpf_setup_q(sc, i);
13010255736Sdavidch            if (rc) {
13011255736Sdavidch                BLOGE(sc, "Queue(%d) setup failed\n", i);
13012255736Sdavidch                sc->state = BXE_STATE_ERROR;
13013255736Sdavidch                goto bxe_nic_load_error3;
13014255736Sdavidch            }
13015255736Sdavidch        }
13016255736Sdavidch    }
13017255736Sdavidch#endif
13018255736Sdavidch
13019255736Sdavidch    /* now when Clients are configured we are ready to work */
13020255736Sdavidch    sc->state = BXE_STATE_OPEN;
13021255736Sdavidch
13022255736Sdavidch    /* Configure a ucast MAC */
13023255736Sdavidch    if (IS_PF(sc)) {
13024255736Sdavidch        rc = bxe_set_eth_mac(sc, TRUE);
13025255736Sdavidch    }
13026255736Sdavidch#if 0
13027255736Sdavidch    else { /* IS_VF(sc) */
13028255736Sdavidch        rc = bxe_vfpf_set_mac(sc);
13029255736Sdavidch    }
13030255736Sdavidch#endif
13031255736Sdavidch    if (rc) {
13032255736Sdavidch        BLOGE(sc, "Setting Ethernet MAC failed\n");
13033255736Sdavidch        sc->state = BXE_STATE_ERROR;
13034255736Sdavidch        goto bxe_nic_load_error3;
13035255736Sdavidch    }
13036255736Sdavidch
13037255736Sdavidch#if 0
13038255736Sdavidch    if (IS_PF(sc) && sc->pending_max) {
13039255736Sdavidch        /* for AFEX */
13040255736Sdavidch        bxe_update_max_mf_config(sc, sc->pending_max);
13041255736Sdavidch        sc->pending_max = 0;
13042255736Sdavidch    }
13043255736Sdavidch#endif
13044255736Sdavidch
13045255736Sdavidch    if (sc->port.pmf) {
13046255736Sdavidch        rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
13047255736Sdavidch        if (rc) {
13048255736Sdavidch            sc->state = BXE_STATE_ERROR;
13049255736Sdavidch            goto bxe_nic_load_error3;
13050255736Sdavidch        }
13051255736Sdavidch    }
13052255736Sdavidch
13053255736Sdavidch    sc->link_params.feature_config_flags &=
13054255736Sdavidch        ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
13055255736Sdavidch
13056255736Sdavidch    /* start fast path */
13057255736Sdavidch
13058255736Sdavidch    /* Initialize Rx filter */
13059255736Sdavidch    bxe_set_rx_mode(sc);
13060255736Sdavidch
13061255736Sdavidch    /* start the Tx */
13062255736Sdavidch    switch (/* XXX load_mode */LOAD_OPEN) {
13063255736Sdavidch    case LOAD_NORMAL:
13064255736Sdavidch    case LOAD_OPEN:
13065255736Sdavidch        break;
13066255736Sdavidch
13067255736Sdavidch    case LOAD_DIAG:
13068255736Sdavidch    case LOAD_LOOPBACK_EXT:
13069255736Sdavidch        sc->state = BXE_STATE_DIAG;
13070255736Sdavidch        break;
13071255736Sdavidch
13072255736Sdavidch    default:
13073255736Sdavidch        break;
13074255736Sdavidch    }
13075255736Sdavidch
13076255736Sdavidch    if (sc->port.pmf) {
13077255736Sdavidch        bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
13078255736Sdavidch    } else {
13079255736Sdavidch        bxe_link_status_update(sc);
13080255736Sdavidch    }
13081255736Sdavidch
13082255736Sdavidch    /* start the periodic timer callout */
13083255736Sdavidch    bxe_periodic_start(sc);
13084255736Sdavidch
13085255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
13086255736Sdavidch        /* mark driver is loaded in shmem2 */
13087255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
13088255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
13089255736Sdavidch                  (val |
13090255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
13091255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_L2));
13092255736Sdavidch    }
13093255736Sdavidch
13094255736Sdavidch    /* wait for all pending SP commands to complete */
13095255736Sdavidch    if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
13096255736Sdavidch        BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
13097255736Sdavidch        bxe_periodic_stop(sc);
13098255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
13099255736Sdavidch        return (ENXIO);
13100255736Sdavidch    }
13101255736Sdavidch
13102255736Sdavidch#if 0
13103255736Sdavidch    /* If PMF - send ADMIN DCBX msg to MFW to initiate DCBX FSM */
13104255736Sdavidch    if (sc->port.pmf && (sc->state != BXE_STATE_DIAG)) {
13105255736Sdavidch        bxe_dcbx_init(sc, FALSE);
13106255736Sdavidch    }
13107255736Sdavidch#endif
13108255736Sdavidch
13109255736Sdavidch    /* Tell the stack the driver is running! */
13110266979Smarcel    if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
13111255736Sdavidch
13112255736Sdavidch    BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
13113255736Sdavidch
13114255736Sdavidch    return (0);
13115255736Sdavidch
13116255736Sdavidchbxe_nic_load_error3:
13117255736Sdavidch
13118255736Sdavidch    if (IS_PF(sc)) {
13119255736Sdavidch        bxe_int_disable_sync(sc, 1);
13120255736Sdavidch
13121255736Sdavidch        /* clean out queued objects */
13122255736Sdavidch        bxe_squeeze_objects(sc);
13123255736Sdavidch    }
13124255736Sdavidch
13125255736Sdavidch    bxe_interrupt_detach(sc);
13126255736Sdavidch
13127255736Sdavidchbxe_nic_load_error2:
13128255736Sdavidch
13129255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
13130255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
13131255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
13132255736Sdavidch    }
13133255736Sdavidch
13134255736Sdavidch    sc->port.pmf = 0;
13135255736Sdavidch
13136255736Sdavidchbxe_nic_load_error1:
13137255736Sdavidch
13138255736Sdavidch    /* clear pf_load status, as it was already set */
13139255736Sdavidch    if (IS_PF(sc)) {
13140255736Sdavidch        bxe_clear_pf_load(sc);
13141255736Sdavidch    }
13142255736Sdavidch
13143255736Sdavidchbxe_nic_load_error0:
13144255736Sdavidch
13145255736Sdavidch    bxe_free_fw_stats_mem(sc);
13146255736Sdavidch    bxe_free_fp_buffers(sc);
13147255736Sdavidch    bxe_free_mem(sc);
13148255736Sdavidch
13149255736Sdavidch    return (rc);
13150255736Sdavidch}
13151255736Sdavidch
13152255736Sdavidchstatic int
13153255736Sdavidchbxe_init_locked(struct bxe_softc *sc)
13154255736Sdavidch{
13155255736Sdavidch    int other_engine = SC_PATH(sc) ? 0 : 1;
13156255736Sdavidch    uint8_t other_load_status, load_status;
13157255736Sdavidch    uint8_t global = FALSE;
13158255736Sdavidch    int rc;
13159255736Sdavidch
13160255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
13161255736Sdavidch
13162255736Sdavidch    /* check if the driver is already running */
13163266979Smarcel    if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
13164255736Sdavidch        BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
13165255736Sdavidch        return (0);
13166255736Sdavidch    }
13167255736Sdavidch
13168255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
13169255736Sdavidch
13170255736Sdavidch    /*
13171255736Sdavidch     * If parity occurred during the unload, then attentions and/or
13172255736Sdavidch     * RECOVERY_IN_PROGRES may still be set. If so we want the first function
13173255736Sdavidch     * loaded on the current engine to complete the recovery. Parity recovery
13174255736Sdavidch     * is only relevant for PF driver.
13175255736Sdavidch     */
13176255736Sdavidch    if (IS_PF(sc)) {
13177255736Sdavidch        other_load_status = bxe_get_load_status(sc, other_engine);
13178255736Sdavidch        load_status = bxe_get_load_status(sc, SC_PATH(sc));
13179255736Sdavidch
13180255736Sdavidch        if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
13181255736Sdavidch            bxe_chk_parity_attn(sc, &global, TRUE)) {
13182255736Sdavidch            do {
13183255736Sdavidch                /*
13184255736Sdavidch                 * If there are attentions and they are in global blocks, set
13185255736Sdavidch                 * the GLOBAL_RESET bit regardless whether it will be this
13186255736Sdavidch                 * function that will complete the recovery or not.
13187255736Sdavidch                 */
13188255736Sdavidch                if (global) {
13189255736Sdavidch                    bxe_set_reset_global(sc);
13190255736Sdavidch                }
13191255736Sdavidch
13192255736Sdavidch                /*
13193255736Sdavidch                 * Only the first function on the current engine should try
13194255736Sdavidch                 * to recover in open. In case of attentions in global blocks
13195255736Sdavidch                 * only the first in the chip should try to recover.
13196255736Sdavidch                 */
13197255736Sdavidch                if ((!load_status && (!global || !other_load_status)) &&
13198255736Sdavidch                    bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
13199255736Sdavidch                    BLOGI(sc, "Recovered during init\n");
13200255736Sdavidch                    break;
13201255736Sdavidch                }
13202255736Sdavidch
13203255736Sdavidch                /* recovery has failed... */
13204255736Sdavidch                bxe_set_power_state(sc, PCI_PM_D3hot);
13205255736Sdavidch                sc->recovery_state = BXE_RECOVERY_FAILED;
13206255736Sdavidch
13207255736Sdavidch                BLOGE(sc, "Recovery flow hasn't properly "
13208255736Sdavidch                          "completed yet, try again later. "
13209255736Sdavidch                          "If you still see this message after a "
13210255736Sdavidch                          "few retries then power cycle is required.\n");
13211255736Sdavidch
13212255736Sdavidch                rc = ENXIO;
13213255736Sdavidch                goto bxe_init_locked_done;
13214255736Sdavidch            } while (0);
13215255736Sdavidch        }
13216255736Sdavidch    }
13217255736Sdavidch
13218255736Sdavidch    sc->recovery_state = BXE_RECOVERY_DONE;
13219255736Sdavidch
13220255736Sdavidch    rc = bxe_nic_load(sc, LOAD_OPEN);
13221255736Sdavidch
13222255736Sdavidchbxe_init_locked_done:
13223255736Sdavidch
13224255736Sdavidch    if (rc) {
13225255736Sdavidch        /* Tell the stack the driver is NOT running! */
13226255736Sdavidch        BLOGE(sc, "Initialization failed, "
13227255736Sdavidch                  "stack notified driver is NOT running!\n");
13228266979Smarcel	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
13229255736Sdavidch    }
13230255736Sdavidch
13231255736Sdavidch    return (rc);
13232255736Sdavidch}
13233255736Sdavidch
13234255736Sdavidchstatic int
13235255736Sdavidchbxe_stop_locked(struct bxe_softc *sc)
13236255736Sdavidch{
13237255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
13238255736Sdavidch    return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
13239255736Sdavidch}
13240255736Sdavidch
13241255736Sdavidch/*
13242255736Sdavidch * Handles controller initialization when called from an unlocked routine.
13243255736Sdavidch * ifconfig calls this function.
13244255736Sdavidch *
13245255736Sdavidch * Returns:
13246255736Sdavidch *   void
13247255736Sdavidch */
13248255736Sdavidchstatic void
13249255736Sdavidchbxe_init(void *xsc)
13250255736Sdavidch{
13251255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
13252255736Sdavidch
13253255736Sdavidch    BXE_CORE_LOCK(sc);
13254255736Sdavidch    bxe_init_locked(sc);
13255255736Sdavidch    BXE_CORE_UNLOCK(sc);
13256255736Sdavidch}
13257255736Sdavidch
13258255736Sdavidchstatic int
13259255736Sdavidchbxe_init_ifnet(struct bxe_softc *sc)
13260255736Sdavidch{
13261266979Smarcel    if_t ifp;
13262266979Smarcel    int capabilities;
13263255736Sdavidch
13264255736Sdavidch    /* ifconfig entrypoint for media type/status reporting */
13265255736Sdavidch    ifmedia_init(&sc->ifmedia, IFM_IMASK,
13266255736Sdavidch                 bxe_ifmedia_update,
13267255736Sdavidch                 bxe_ifmedia_status);
13268255736Sdavidch
13269255736Sdavidch    /* set the default interface values */
13270255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
13271255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
13272255736Sdavidch    ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
13273255736Sdavidch
13274255736Sdavidch    sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
13275255736Sdavidch
13276255736Sdavidch    /* allocate the ifnet structure */
13277266979Smarcel    if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
13278255736Sdavidch        BLOGE(sc, "Interface allocation failed!\n");
13279255736Sdavidch        return (ENXIO);
13280255736Sdavidch    }
13281255736Sdavidch
13282266979Smarcel    if_setsoftc(ifp, sc);
13283270876Sglebius    if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
13284266979Smarcel    if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
13285266979Smarcel    if_setioctlfn(ifp, bxe_ioctl);
13286266979Smarcel    if_setstartfn(ifp, bxe_tx_start);
13287271782Sglebius    if_setgetcounterfn(ifp, bxe_get_counter);
13288255736Sdavidch#if __FreeBSD_version >= 800000
13289266979Smarcel    if_settransmitfn(ifp, bxe_tx_mq_start);
13290266979Smarcel    if_setqflushfn(ifp, bxe_mq_flush);
13291255736Sdavidch#endif
13292255736Sdavidch#ifdef FreeBSD8_0
13293266979Smarcel    if_settimer(ifp, 0);
13294255736Sdavidch#endif
13295266979Smarcel    if_setinitfn(ifp, bxe_init);
13296266979Smarcel    if_setmtu(ifp, sc->mtu);
13297266979Smarcel    if_sethwassist(ifp, (CSUM_IP      |
13298255736Sdavidch                        CSUM_TCP      |
13299255736Sdavidch                        CSUM_UDP      |
13300255736Sdavidch                        CSUM_TSO      |
13301255736Sdavidch                        CSUM_TCP_IPV6 |
13302266979Smarcel                        CSUM_UDP_IPV6));
13303266979Smarcel
13304266979Smarcel    capabilities =
13305255736Sdavidch#if __FreeBSD_version < 700000
13306255736Sdavidch        (IFCAP_VLAN_MTU       |
13307255736Sdavidch         IFCAP_VLAN_HWTAGGING |
13308255736Sdavidch         IFCAP_HWCSUM         |
13309255736Sdavidch         IFCAP_JUMBO_MTU      |
13310255736Sdavidch         IFCAP_LRO);
13311255736Sdavidch#else
13312255736Sdavidch        (IFCAP_VLAN_MTU       |
13313255736Sdavidch         IFCAP_VLAN_HWTAGGING |
13314255736Sdavidch         IFCAP_VLAN_HWTSO     |
13315255736Sdavidch         IFCAP_VLAN_HWFILTER  |
13316255736Sdavidch         IFCAP_VLAN_HWCSUM    |
13317255736Sdavidch         IFCAP_HWCSUM         |
13318255736Sdavidch         IFCAP_JUMBO_MTU      |
13319255736Sdavidch         IFCAP_LRO            |
13320255736Sdavidch         IFCAP_TSO4           |
13321255736Sdavidch         IFCAP_TSO6           |
13322255736Sdavidch         IFCAP_WOL_MAGIC);
13323255736Sdavidch#endif
13324266979Smarcel    if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
13325266979Smarcel    if_setbaudrate(ifp, IF_Gbps(10));
13326266979Smarcel/* XXX */
13327266979Smarcel    if_setsendqlen(ifp, sc->tx_ring_size);
13328266979Smarcel    if_setsendqready(ifp);
13329266979Smarcel/* XXX */
13330255736Sdavidch
13331266979Smarcel    sc->ifp = ifp;
13332255736Sdavidch
13333255736Sdavidch    /* attach to the Ethernet interface list */
13334270876Sglebius    ether_ifattach(ifp, sc->link_params.mac_addr);
13335255736Sdavidch
13336255736Sdavidch    return (0);
13337255736Sdavidch}
13338255736Sdavidch
13339255736Sdavidchstatic void
13340255736Sdavidchbxe_deallocate_bars(struct bxe_softc *sc)
13341255736Sdavidch{
13342255736Sdavidch    int i;
13343255736Sdavidch
13344255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
13345255736Sdavidch        if (sc->bar[i].resource != NULL) {
13346255736Sdavidch            bus_release_resource(sc->dev,
13347255736Sdavidch                                 SYS_RES_MEMORY,
13348255736Sdavidch                                 sc->bar[i].rid,
13349255736Sdavidch                                 sc->bar[i].resource);
13350255736Sdavidch            BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
13351255736Sdavidch                  i, PCIR_BAR(i));
13352255736Sdavidch        }
13353255736Sdavidch    }
13354255736Sdavidch}
13355255736Sdavidch
13356255736Sdavidchstatic int
13357255736Sdavidchbxe_allocate_bars(struct bxe_softc *sc)
13358255736Sdavidch{
13359255736Sdavidch    u_int flags;
13360255736Sdavidch    int i;
13361255736Sdavidch
13362255736Sdavidch    memset(sc->bar, 0, sizeof(sc->bar));
13363255736Sdavidch
13364255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
13365255736Sdavidch
13366255736Sdavidch        /* memory resources reside at BARs 0, 2, 4 */
13367255736Sdavidch        /* Run `pciconf -lb` to see mappings */
13368255736Sdavidch        if ((i != 0) && (i != 2) && (i != 4)) {
13369255736Sdavidch            continue;
13370255736Sdavidch        }
13371255736Sdavidch
13372255736Sdavidch        sc->bar[i].rid = PCIR_BAR(i);
13373255736Sdavidch
13374255736Sdavidch        flags = RF_ACTIVE;
13375255736Sdavidch        if (i == 0) {
13376255736Sdavidch            flags |= RF_SHAREABLE;
13377255736Sdavidch        }
13378255736Sdavidch
13379255736Sdavidch        if ((sc->bar[i].resource =
13380255736Sdavidch             bus_alloc_resource_any(sc->dev,
13381255736Sdavidch                                    SYS_RES_MEMORY,
13382255736Sdavidch                                    &sc->bar[i].rid,
13383255736Sdavidch                                    flags)) == NULL) {
13384255736Sdavidch#if 0
13385255736Sdavidch            /* BAR4 doesn't exist for E1 */
13386255736Sdavidch            BLOGE(sc, "PCI BAR%d [%02x] memory allocation failed\n",
13387255736Sdavidch                  i, PCIR_BAR(i));
13388255736Sdavidch#endif
13389255736Sdavidch            return (0);
13390255736Sdavidch        }
13391255736Sdavidch
13392255736Sdavidch        sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
13393255736Sdavidch        sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
13394255736Sdavidch        sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
13395255736Sdavidch
13396255736Sdavidch        BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%ld) -> %p\n",
13397255736Sdavidch              i, PCIR_BAR(i),
13398255736Sdavidch              (void *)rman_get_start(sc->bar[i].resource),
13399255736Sdavidch              (void *)rman_get_end(sc->bar[i].resource),
13400255736Sdavidch              rman_get_size(sc->bar[i].resource),
13401255736Sdavidch              (void *)sc->bar[i].kva);
13402255736Sdavidch    }
13403255736Sdavidch
13404255736Sdavidch    return (0);
13405255736Sdavidch}
13406255736Sdavidch
13407255736Sdavidchstatic void
13408255736Sdavidchbxe_get_function_num(struct bxe_softc *sc)
13409255736Sdavidch{
13410255736Sdavidch    uint32_t val = 0;
13411255736Sdavidch
13412255736Sdavidch    /*
13413255736Sdavidch     * Read the ME register to get the function number. The ME register
13414255736Sdavidch     * holds the relative-function number and absolute-function number. The
13415255736Sdavidch     * absolute-function number appears only in E2 and above. Before that
13416255736Sdavidch     * these bits always contained zero, therefore we cannot blindly use them.
13417255736Sdavidch     */
13418255736Sdavidch
13419255736Sdavidch    val = REG_RD(sc, BAR_ME_REGISTER);
13420255736Sdavidch
13421255736Sdavidch    sc->pfunc_rel =
13422255736Sdavidch        (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
13423255736Sdavidch    sc->path_id =
13424255736Sdavidch        (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
13425255736Sdavidch
13426255736Sdavidch    if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13427255736Sdavidch        sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
13428255736Sdavidch    } else {
13429255736Sdavidch        sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
13430255736Sdavidch    }
13431255736Sdavidch
13432255736Sdavidch    BLOGD(sc, DBG_LOAD,
13433255736Sdavidch          "Relative function %d, Absolute function %d, Path %d\n",
13434255736Sdavidch          sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
13435255736Sdavidch}
13436255736Sdavidch
13437255736Sdavidchstatic uint32_t
13438255736Sdavidchbxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
13439255736Sdavidch{
13440255736Sdavidch    uint32_t shmem2_size;
13441255736Sdavidch    uint32_t offset;
13442255736Sdavidch    uint32_t mf_cfg_offset_value;
13443255736Sdavidch
13444255736Sdavidch    /* Non 57712 */
13445255736Sdavidch    offset = (SHMEM_RD(sc, func_mb) +
13446255736Sdavidch              (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
13447255736Sdavidch
13448255736Sdavidch    /* 57712 plus */
13449255736Sdavidch    if (sc->devinfo.shmem2_base != 0) {
13450255736Sdavidch        shmem2_size = SHMEM2_RD(sc, size);
13451255736Sdavidch        if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
13452255736Sdavidch            mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
13453255736Sdavidch            if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
13454255736Sdavidch                offset = mf_cfg_offset_value;
13455255736Sdavidch            }
13456255736Sdavidch        }
13457255736Sdavidch    }
13458255736Sdavidch
13459255736Sdavidch    return (offset);
13460255736Sdavidch}
13461255736Sdavidch
13462255736Sdavidchstatic uint32_t
13463255736Sdavidchbxe_pcie_capability_read(struct bxe_softc *sc,
13464255736Sdavidch                         int    reg,
13465255736Sdavidch                         int    width)
13466255736Sdavidch{
13467255736Sdavidch    int pcie_reg;
13468255736Sdavidch
13469255736Sdavidch    /* ensure PCIe capability is enabled */
13470255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
13471255736Sdavidch        if (pcie_reg != 0) {
13472255736Sdavidch            BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
13473255736Sdavidch            return (pci_read_config(sc->dev, (pcie_reg + reg), width));
13474255736Sdavidch        }
13475255736Sdavidch    }
13476255736Sdavidch
13477255736Sdavidch    BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
13478255736Sdavidch
13479255736Sdavidch    return (0);
13480255736Sdavidch}
13481255736Sdavidch
13482255736Sdavidchstatic uint8_t
13483255736Sdavidchbxe_is_pcie_pending(struct bxe_softc *sc)
13484255736Sdavidch{
13485255736Sdavidch    return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
13486255736Sdavidch            PCIM_EXP_STA_TRANSACTION_PND);
13487255736Sdavidch}
13488255736Sdavidch
13489255736Sdavidch/*
13490255736Sdavidch * Walk the PCI capabiites list for the device to find what features are
13491255736Sdavidch * supported. These capabilites may be enabled/disabled by firmware so it's
13492255736Sdavidch * best to walk the list rather than make assumptions.
13493255736Sdavidch */
13494255736Sdavidchstatic void
13495255736Sdavidchbxe_probe_pci_caps(struct bxe_softc *sc)
13496255736Sdavidch{
13497255736Sdavidch    uint16_t link_status;
13498255736Sdavidch    int reg;
13499255736Sdavidch
13500255736Sdavidch    /* check if PCI Power Management is enabled */
13501255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
13502255736Sdavidch        if (reg != 0) {
13503255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
13504255736Sdavidch
13505255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
13506255736Sdavidch            sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
13507255736Sdavidch        }
13508255736Sdavidch    }
13509255736Sdavidch
13510255736Sdavidch    link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
13511255736Sdavidch
13512255736Sdavidch    /* handle PCIe 2.0 workarounds for 57710 */
13513255736Sdavidch    if (CHIP_IS_E1(sc)) {
13514255736Sdavidch        /* workaround for 57710 errata E4_57710_27462 */
13515255736Sdavidch        sc->devinfo.pcie_link_speed =
13516255736Sdavidch            (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
13517255736Sdavidch
13518255736Sdavidch        /* workaround for 57710 errata E4_57710_27488 */
13519255736Sdavidch        sc->devinfo.pcie_link_width =
13520255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13521255736Sdavidch        if (sc->devinfo.pcie_link_speed > 1) {
13522255736Sdavidch            sc->devinfo.pcie_link_width =
13523255736Sdavidch                ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
13524255736Sdavidch        }
13525255736Sdavidch    } else {
13526255736Sdavidch        sc->devinfo.pcie_link_speed =
13527255736Sdavidch            (link_status & PCIM_LINK_STA_SPEED);
13528255736Sdavidch        sc->devinfo.pcie_link_width =
13529255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13530255736Sdavidch    }
13531255736Sdavidch
13532255736Sdavidch    BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
13533255736Sdavidch          sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
13534255736Sdavidch
13535255736Sdavidch    sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
13536255736Sdavidch    sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
13537255736Sdavidch
13538255736Sdavidch    /* check if MSI capability is enabled */
13539255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
13540255736Sdavidch        if (reg != 0) {
13541255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
13542255736Sdavidch
13543255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
13544255736Sdavidch            sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
13545255736Sdavidch        }
13546255736Sdavidch    }
13547255736Sdavidch
13548255736Sdavidch    /* check if MSI-X capability is enabled */
13549255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
13550255736Sdavidch        if (reg != 0) {
13551255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
13552255736Sdavidch
13553255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
13554255736Sdavidch            sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
13555255736Sdavidch        }
13556255736Sdavidch    }
13557255736Sdavidch}
13558255736Sdavidch
13559255736Sdavidchstatic int
13560255736Sdavidchbxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
13561255736Sdavidch{
13562255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13563255736Sdavidch    uint32_t val;
13564255736Sdavidch
13565255736Sdavidch    /* get the outer vlan if we're in switch-dependent mode */
13566255736Sdavidch
13567255736Sdavidch    val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13568255736Sdavidch    mf_info->ext_id = (uint16_t)val;
13569255736Sdavidch
13570255736Sdavidch    mf_info->multi_vnics_mode = 1;
13571255736Sdavidch
13572255736Sdavidch    if (!VALID_OVLAN(mf_info->ext_id)) {
13573255736Sdavidch        BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13574255736Sdavidch        return (1);
13575255736Sdavidch    }
13576255736Sdavidch
13577255736Sdavidch    /* get the capabilities */
13578255736Sdavidch    if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13579255736Sdavidch        FUNC_MF_CFG_PROTOCOL_ISCSI) {
13580255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13581255736Sdavidch    } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13582255736Sdavidch               FUNC_MF_CFG_PROTOCOL_FCOE) {
13583255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13584255736Sdavidch    } else {
13585255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13586255736Sdavidch    }
13587255736Sdavidch
13588255736Sdavidch    mf_info->vnics_per_port =
13589255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13590255736Sdavidch
13591255736Sdavidch    return (0);
13592255736Sdavidch}
13593255736Sdavidch
13594255736Sdavidchstatic uint32_t
13595255736Sdavidchbxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13596255736Sdavidch{
13597255736Sdavidch    uint32_t retval = 0;
13598255736Sdavidch    uint32_t val;
13599255736Sdavidch
13600255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13601255736Sdavidch
13602255736Sdavidch    if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13603255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13604255736Sdavidch            retval |= MF_PROTO_SUPPORT_ETHERNET;
13605255736Sdavidch        }
13606255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13607255736Sdavidch            retval |= MF_PROTO_SUPPORT_ISCSI;
13608255736Sdavidch        }
13609255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13610255736Sdavidch            retval |= MF_PROTO_SUPPORT_FCOE;
13611255736Sdavidch        }
13612255736Sdavidch    }
13613255736Sdavidch
13614255736Sdavidch    return (retval);
13615255736Sdavidch}
13616255736Sdavidch
13617255736Sdavidchstatic int
13618255736Sdavidchbxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13619255736Sdavidch{
13620255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13621255736Sdavidch    uint32_t val;
13622255736Sdavidch
13623255736Sdavidch    /*
13624255736Sdavidch     * There is no outer vlan if we're in switch-independent mode.
13625255736Sdavidch     * If the mac is valid then assume multi-function.
13626255736Sdavidch     */
13627255736Sdavidch
13628255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13629255736Sdavidch
13630255736Sdavidch    mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13631255736Sdavidch
13632255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13633255736Sdavidch
13634255736Sdavidch    mf_info->vnics_per_port =
13635255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13636255736Sdavidch
13637255736Sdavidch    return (0);
13638255736Sdavidch}
13639255736Sdavidch
13640255736Sdavidchstatic int
13641255736Sdavidchbxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13642255736Sdavidch{
13643255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13644255736Sdavidch    uint32_t e1hov_tag;
13645255736Sdavidch    uint32_t func_config;
13646255736Sdavidch    uint32_t niv_config;
13647255736Sdavidch
13648255736Sdavidch    mf_info->multi_vnics_mode = 1;
13649255736Sdavidch
13650255736Sdavidch    e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13651255736Sdavidch    func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13652255736Sdavidch    niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13653255736Sdavidch
13654255736Sdavidch    mf_info->ext_id =
13655255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13656255736Sdavidch                   FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13657255736Sdavidch
13658255736Sdavidch    mf_info->default_vlan =
13659255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13660255736Sdavidch                   FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13661255736Sdavidch
13662255736Sdavidch    mf_info->niv_allowed_priorities =
13663255736Sdavidch        (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13664255736Sdavidch                  FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13665255736Sdavidch
13666255736Sdavidch    mf_info->niv_default_cos =
13667255736Sdavidch        (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13668255736Sdavidch                  FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13669255736Sdavidch
13670255736Sdavidch    mf_info->afex_vlan_mode =
13671255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13672255736Sdavidch         FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13673255736Sdavidch
13674255736Sdavidch    mf_info->niv_mba_enabled =
13675255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13676255736Sdavidch         FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13677255736Sdavidch
13678255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13679255736Sdavidch
13680255736Sdavidch    mf_info->vnics_per_port =
13681255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13682255736Sdavidch
13683255736Sdavidch    return (0);
13684255736Sdavidch}
13685255736Sdavidch
13686255736Sdavidchstatic int
13687255736Sdavidchbxe_check_valid_mf_cfg(struct bxe_softc *sc)
13688255736Sdavidch{
13689255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13690255736Sdavidch    uint32_t mf_cfg1;
13691255736Sdavidch    uint32_t mf_cfg2;
13692255736Sdavidch    uint32_t ovlan1;
13693255736Sdavidch    uint32_t ovlan2;
13694255736Sdavidch    uint8_t i, j;
13695255736Sdavidch
13696255736Sdavidch    BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13697255736Sdavidch          SC_PORT(sc));
13698255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13699255736Sdavidch          mf_info->mf_config[SC_VN(sc)]);
13700255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13701255736Sdavidch          mf_info->multi_vnics_mode);
13702255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13703255736Sdavidch          mf_info->vnics_per_port);
13704255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13705255736Sdavidch          mf_info->ext_id);
13706255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13707255736Sdavidch          mf_info->min_bw[0], mf_info->min_bw[1],
13708255736Sdavidch          mf_info->min_bw[2], mf_info->min_bw[3]);
13709255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13710255736Sdavidch          mf_info->max_bw[0], mf_info->max_bw[1],
13711255736Sdavidch          mf_info->max_bw[2], mf_info->max_bw[3]);
13712255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13713255736Sdavidch          sc->mac_addr_str);
13714255736Sdavidch
13715255736Sdavidch    /* various MF mode sanity checks... */
13716255736Sdavidch
13717255736Sdavidch    if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13718255736Sdavidch        BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13719255736Sdavidch              SC_PORT(sc));
13720255736Sdavidch        return (1);
13721255736Sdavidch    }
13722255736Sdavidch
13723255736Sdavidch    if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13724255736Sdavidch        BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13725255736Sdavidch              mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13726255736Sdavidch        return (1);
13727255736Sdavidch    }
13728255736Sdavidch
13729255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13730255736Sdavidch        /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13731255736Sdavidch        if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13732255736Sdavidch            BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13733255736Sdavidch                  SC_VN(sc), OVLAN(sc));
13734255736Sdavidch            return (1);
13735255736Sdavidch        }
13736255736Sdavidch
13737255736Sdavidch        if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13738255736Sdavidch            BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13739255736Sdavidch                  mf_info->multi_vnics_mode, OVLAN(sc));
13740255736Sdavidch            return (1);
13741255736Sdavidch        }
13742255736Sdavidch
13743255736Sdavidch        /*
13744255736Sdavidch         * Verify all functions are either MF or SF mode. If MF, make sure
13745255736Sdavidch         * sure that all non-hidden functions have a valid ovlan. If SF,
13746255736Sdavidch         * make sure that all non-hidden functions have an invalid ovlan.
13747255736Sdavidch         */
13748255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13749255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13750255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13751255736Sdavidch            if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13752255736Sdavidch                (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13753255736Sdavidch                 ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13754255736Sdavidch                BLOGE(sc, "mf_mode=SD function %d MF config "
13755255736Sdavidch                          "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13756255736Sdavidch                      i, mf_info->multi_vnics_mode, ovlan1);
13757255736Sdavidch                return (1);
13758255736Sdavidch            }
13759255736Sdavidch        }
13760255736Sdavidch
13761255736Sdavidch        /* Verify all funcs on the same port each have a different ovlan. */
13762255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13763255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13764255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13765255736Sdavidch            /* iterate from the next function on the port to the max func */
13766255736Sdavidch            for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13767255736Sdavidch                mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13768255736Sdavidch                ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13769255736Sdavidch                if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13770255736Sdavidch                    VALID_OVLAN(ovlan1) &&
13771255736Sdavidch                    !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13772255736Sdavidch                    VALID_OVLAN(ovlan2) &&
13773255736Sdavidch                    (ovlan1 == ovlan2)) {
13774255736Sdavidch                    BLOGE(sc, "mf_mode=SD functions %d and %d "
13775255736Sdavidch                              "have the same ovlan (%d)\n",
13776255736Sdavidch                          i, j, ovlan1);
13777255736Sdavidch                    return (1);
13778255736Sdavidch                }
13779255736Sdavidch            }
13780255736Sdavidch        }
13781255736Sdavidch    } /* MULTI_FUNCTION_SD */
13782255736Sdavidch
13783255736Sdavidch    return (0);
13784255736Sdavidch}
13785255736Sdavidch
13786255736Sdavidchstatic int
13787255736Sdavidchbxe_get_mf_cfg_info(struct bxe_softc *sc)
13788255736Sdavidch{
13789255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13790255736Sdavidch    uint32_t val, mac_upper;
13791255736Sdavidch    uint8_t i, vnic;
13792255736Sdavidch
13793255736Sdavidch    /* initialize mf_info defaults */
13794255736Sdavidch    mf_info->vnics_per_port   = 1;
13795255736Sdavidch    mf_info->multi_vnics_mode = FALSE;
13796255736Sdavidch    mf_info->path_has_ovlan   = FALSE;
13797255736Sdavidch    mf_info->mf_mode          = SINGLE_FUNCTION;
13798255736Sdavidch
13799255736Sdavidch    if (!CHIP_IS_MF_CAP(sc)) {
13800255736Sdavidch        return (0);
13801255736Sdavidch    }
13802255736Sdavidch
13803255736Sdavidch    if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13804255736Sdavidch        BLOGE(sc, "Invalid mf_cfg_base!\n");
13805255736Sdavidch        return (1);
13806255736Sdavidch    }
13807255736Sdavidch
13808255736Sdavidch    /* get the MF mode (switch dependent / independent / single-function) */
13809255736Sdavidch
13810255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13811255736Sdavidch
13812255736Sdavidch    switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13813255736Sdavidch    {
13814255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13815255736Sdavidch
13816255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13817255736Sdavidch
13818255736Sdavidch        /* check for legal upper mac bytes */
13819255736Sdavidch        if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13820255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SI;
13821255736Sdavidch        } else {
13822255736Sdavidch            BLOGE(sc, "Invalid config for Switch Independent mode\n");
13823255736Sdavidch        }
13824255736Sdavidch
13825255736Sdavidch        break;
13826255736Sdavidch
13827255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13828255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13829255736Sdavidch
13830255736Sdavidch        /* get outer vlan configuration */
13831255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13832255736Sdavidch
13833255736Sdavidch        if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13834255736Sdavidch            FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13835255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SD;
13836255736Sdavidch        } else {
13837255736Sdavidch            BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13838255736Sdavidch        }
13839255736Sdavidch
13840255736Sdavidch        break;
13841255736Sdavidch
13842255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13843255736Sdavidch
13844255736Sdavidch        /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13845255736Sdavidch        return (0);
13846255736Sdavidch
13847255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13848255736Sdavidch
13849255736Sdavidch        /*
13850255736Sdavidch         * Mark MF mode as NIV if MCP version includes NPAR-SD support
13851255736Sdavidch         * and the MAC address is valid.
13852255736Sdavidch         */
13853255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13854255736Sdavidch
13855255736Sdavidch        if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13856255736Sdavidch            (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13857255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13858255736Sdavidch        } else {
13859255736Sdavidch            BLOGE(sc, "Invalid config for AFEX mode\n");
13860255736Sdavidch        }
13861255736Sdavidch
13862255736Sdavidch        break;
13863255736Sdavidch
13864255736Sdavidch    default:
13865255736Sdavidch
13866255736Sdavidch        BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13867255736Sdavidch              (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13868255736Sdavidch
13869255736Sdavidch        return (1);
13870255736Sdavidch    }
13871255736Sdavidch
13872255736Sdavidch    /* set path mf_mode (which could be different than function mf_mode) */
13873255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13874255736Sdavidch        mf_info->path_has_ovlan = TRUE;
13875255736Sdavidch    } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13876255736Sdavidch        /*
13877255736Sdavidch         * Decide on path multi vnics mode. If we're not in MF mode and in
13878255736Sdavidch         * 4-port mode, this is good enough to check vnic-0 of the other port
13879255736Sdavidch         * on the same path
13880255736Sdavidch         */
13881255736Sdavidch        if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13882255736Sdavidch            uint8_t other_port = !(PORT_ID(sc) & 1);
13883255736Sdavidch            uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13884255736Sdavidch
13885255736Sdavidch            val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13886255736Sdavidch
13887255736Sdavidch            mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13888255736Sdavidch        }
13889255736Sdavidch    }
13890255736Sdavidch
13891255736Sdavidch    if (mf_info->mf_mode == SINGLE_FUNCTION) {
13892255736Sdavidch        /* invalid MF config */
13893255736Sdavidch        if (SC_VN(sc) >= 1) {
13894255736Sdavidch            BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13895255736Sdavidch            return (1);
13896255736Sdavidch        }
13897255736Sdavidch
13898255736Sdavidch        return (0);
13899255736Sdavidch    }
13900255736Sdavidch
13901255736Sdavidch    /* get the MF configuration */
13902255736Sdavidch    mf_info->mf_config[SC_VN(sc)] =
13903255736Sdavidch        MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13904255736Sdavidch
13905255736Sdavidch    switch(mf_info->mf_mode)
13906255736Sdavidch    {
13907255736Sdavidch    case MULTI_FUNCTION_SD:
13908255736Sdavidch
13909255736Sdavidch        bxe_get_shmem_mf_cfg_info_sd(sc);
13910255736Sdavidch        break;
13911255736Sdavidch
13912255736Sdavidch    case MULTI_FUNCTION_SI:
13913255736Sdavidch
13914255736Sdavidch        bxe_get_shmem_mf_cfg_info_si(sc);
13915255736Sdavidch        break;
13916255736Sdavidch
13917255736Sdavidch    case MULTI_FUNCTION_AFEX:
13918255736Sdavidch
13919255736Sdavidch        bxe_get_shmem_mf_cfg_info_niv(sc);
13920255736Sdavidch        break;
13921255736Sdavidch
13922255736Sdavidch    default:
13923255736Sdavidch
13924255736Sdavidch        BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13925255736Sdavidch              mf_info->mf_mode);
13926255736Sdavidch        return (1);
13927255736Sdavidch    }
13928255736Sdavidch
13929255736Sdavidch    /* get the congestion management parameters */
13930255736Sdavidch
13931255736Sdavidch    vnic = 0;
13932255736Sdavidch    FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13933255736Sdavidch        /* get min/max bw */
13934255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[i].config);
13935255736Sdavidch        mf_info->min_bw[vnic] =
13936255736Sdavidch            ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13937255736Sdavidch        mf_info->max_bw[vnic] =
13938255736Sdavidch            ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13939255736Sdavidch        vnic++;
13940255736Sdavidch    }
13941255736Sdavidch
13942255736Sdavidch    return (bxe_check_valid_mf_cfg(sc));
13943255736Sdavidch}
13944255736Sdavidch
13945255736Sdavidchstatic int
13946255736Sdavidchbxe_get_shmem_info(struct bxe_softc *sc)
13947255736Sdavidch{
13948255736Sdavidch    int port;
13949255736Sdavidch    uint32_t mac_hi, mac_lo, val;
13950255736Sdavidch
13951255736Sdavidch    port = SC_PORT(sc);
13952255736Sdavidch    mac_hi = mac_lo = 0;
13953255736Sdavidch
13954255736Sdavidch    sc->link_params.sc   = sc;
13955255736Sdavidch    sc->link_params.port = port;
13956255736Sdavidch
13957255736Sdavidch    /* get the hardware config info */
13958255736Sdavidch    sc->devinfo.hw_config =
13959255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config);
13960255736Sdavidch    sc->devinfo.hw_config2 =
13961255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13962255736Sdavidch
13963255736Sdavidch    sc->link_params.hw_led_mode =
13964255736Sdavidch        ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13965255736Sdavidch         SHARED_HW_CFG_LED_MODE_SHIFT);
13966255736Sdavidch
13967255736Sdavidch    /* get the port feature config */
13968255736Sdavidch    sc->port.config =
13969255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].config),
13970255736Sdavidch
13971255736Sdavidch    /* get the link params */
13972255736Sdavidch    sc->link_params.speed_cap_mask[0] =
13973255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13974255736Sdavidch    sc->link_params.speed_cap_mask[1] =
13975255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13976255736Sdavidch
13977255736Sdavidch    /* get the lane config */
13978255736Sdavidch    sc->link_params.lane_config =
13979255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13980255736Sdavidch
13981255736Sdavidch    /* get the link config */
13982255736Sdavidch    val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13983255736Sdavidch    sc->port.link_config[ELINK_INT_PHY] = val;
13984255736Sdavidch    sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13985255736Sdavidch    sc->port.link_config[ELINK_EXT_PHY1] =
13986255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13987255736Sdavidch
13988255736Sdavidch    /* get the override preemphasis flag and enable it or turn it off */
13989255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13990255736Sdavidch    if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13991255736Sdavidch        sc->link_params.feature_config_flags |=
13992255736Sdavidch            ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13993255736Sdavidch    } else {
13994255736Sdavidch        sc->link_params.feature_config_flags &=
13995255736Sdavidch            ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13996255736Sdavidch    }
13997255736Sdavidch
13998255736Sdavidch    /* get the initial value of the link params */
13999255736Sdavidch    sc->link_params.multi_phy_config =
14000255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
14001255736Sdavidch
14002255736Sdavidch    /* get external phy info */
14003255736Sdavidch    sc->port.ext_phy_config =
14004255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
14005255736Sdavidch
14006255736Sdavidch    /* get the multifunction configuration */
14007255736Sdavidch    bxe_get_mf_cfg_info(sc);
14008255736Sdavidch
14009255736Sdavidch    /* get the mac address */
14010255736Sdavidch    if (IS_MF(sc)) {
14011255736Sdavidch        mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
14012255736Sdavidch        mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
14013255736Sdavidch    } else {
14014255736Sdavidch        mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
14015255736Sdavidch        mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
14016255736Sdavidch    }
14017255736Sdavidch
14018255736Sdavidch    if ((mac_lo == 0) && (mac_hi == 0)) {
14019255736Sdavidch        *sc->mac_addr_str = 0;
14020255736Sdavidch        BLOGE(sc, "No Ethernet address programmed!\n");
14021255736Sdavidch    } else {
14022255736Sdavidch        sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
14023255736Sdavidch        sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
14024255736Sdavidch        sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
14025255736Sdavidch        sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
14026255736Sdavidch        sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
14027255736Sdavidch        sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
14028255736Sdavidch        snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
14029255736Sdavidch                 "%02x:%02x:%02x:%02x:%02x:%02x",
14030255736Sdavidch                 sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
14031255736Sdavidch                 sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
14032255736Sdavidch                 sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
14033255736Sdavidch        BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
14034255736Sdavidch    }
14035255736Sdavidch
14036255736Sdavidch#if 0
14037255736Sdavidch    if (!IS_MF(sc) &&
14038255736Sdavidch        ((sc->port.config & PORT_FEAT_CFG_STORAGE_PERSONALITY_MASK) ==
14039255736Sdavidch         PORT_FEAT_CFG_STORAGE_PERSONALITY_FCOE)) {
14040255736Sdavidch        sc->flags |= BXE_NO_ISCSI;
14041255736Sdavidch    }
14042255736Sdavidch    if (!IS_MF(sc) &&
14043255736Sdavidch        ((sc->port.config & PORT_FEAT_CFG_STORAGE_PERSONALITY_MASK) ==
14044255736Sdavidch         PORT_FEAT_CFG_STORAGE_PERSONALITY_ISCSI)) {
14045255736Sdavidch        sc->flags |= BXE_NO_FCOE_FLAG;
14046255736Sdavidch    }
14047255736Sdavidch#endif
14048255736Sdavidch
14049255736Sdavidch    return (0);
14050255736Sdavidch}
14051255736Sdavidch
14052255736Sdavidchstatic void
14053255736Sdavidchbxe_get_tunable_params(struct bxe_softc *sc)
14054255736Sdavidch{
14055255736Sdavidch    /* sanity checks */
14056255736Sdavidch
14057255736Sdavidch    if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
14058255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSI)  &&
14059255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSIX)) {
14060255736Sdavidch        BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
14061255736Sdavidch        bxe_interrupt_mode = INTR_MODE_MSIX;
14062255736Sdavidch    }
14063255736Sdavidch
14064255736Sdavidch    if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
14065255736Sdavidch        BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
14066255736Sdavidch        bxe_queue_count = 0;
14067255736Sdavidch    }
14068255736Sdavidch
14069255736Sdavidch    if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
14070255736Sdavidch        if (bxe_max_rx_bufs == 0) {
14071255736Sdavidch            bxe_max_rx_bufs = RX_BD_USABLE;
14072255736Sdavidch        } else {
14073255736Sdavidch            BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
14074255736Sdavidch            bxe_max_rx_bufs = 2048;
14075255736Sdavidch        }
14076255736Sdavidch    }
14077255736Sdavidch
14078255736Sdavidch    if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
14079255736Sdavidch        BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
14080255736Sdavidch        bxe_hc_rx_ticks = 25;
14081255736Sdavidch    }
14082255736Sdavidch
14083255736Sdavidch    if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
14084255736Sdavidch        BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
14085255736Sdavidch        bxe_hc_tx_ticks = 50;
14086255736Sdavidch    }
14087255736Sdavidch
14088255736Sdavidch    if (bxe_max_aggregation_size == 0) {
14089255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
14090255736Sdavidch    }
14091255736Sdavidch
14092255736Sdavidch    if (bxe_max_aggregation_size > 0xffff) {
14093255736Sdavidch        BLOGW(sc, "invalid max_aggregation_size (%d)\n",
14094255736Sdavidch              bxe_max_aggregation_size);
14095255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
14096255736Sdavidch    }
14097255736Sdavidch
14098255736Sdavidch    if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
14099255736Sdavidch        BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
14100255736Sdavidch        bxe_mrrs = -1;
14101255736Sdavidch    }
14102255736Sdavidch
14103255736Sdavidch    if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
14104255736Sdavidch        BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
14105255736Sdavidch        bxe_autogreeen = 0;
14106255736Sdavidch    }
14107255736Sdavidch
14108255736Sdavidch    if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
14109255736Sdavidch        BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
14110255736Sdavidch        bxe_udp_rss = 0;
14111255736Sdavidch    }
14112255736Sdavidch
14113255736Sdavidch    /* pull in user settings */
14114255736Sdavidch
14115255736Sdavidch    sc->interrupt_mode       = bxe_interrupt_mode;
14116255736Sdavidch    sc->max_rx_bufs          = bxe_max_rx_bufs;
14117255736Sdavidch    sc->hc_rx_ticks          = bxe_hc_rx_ticks;
14118255736Sdavidch    sc->hc_tx_ticks          = bxe_hc_tx_ticks;
14119255736Sdavidch    sc->max_aggregation_size = bxe_max_aggregation_size;
14120255736Sdavidch    sc->mrrs                 = bxe_mrrs;
14121255736Sdavidch    sc->autogreeen           = bxe_autogreeen;
14122255736Sdavidch    sc->udp_rss              = bxe_udp_rss;
14123255736Sdavidch
14124255736Sdavidch    if (bxe_interrupt_mode == INTR_MODE_INTX) {
14125255736Sdavidch        sc->num_queues = 1;
14126255736Sdavidch    } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
14127255736Sdavidch        sc->num_queues =
14128255736Sdavidch            min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
14129255736Sdavidch                MAX_RSS_CHAINS);
14130255736Sdavidch        if (sc->num_queues > mp_ncpus) {
14131255736Sdavidch            sc->num_queues = mp_ncpus;
14132255736Sdavidch        }
14133255736Sdavidch    }
14134255736Sdavidch
14135255736Sdavidch    BLOGD(sc, DBG_LOAD,
14136255736Sdavidch          "User Config: "
14137258187Sedavis          "debug=0x%lx "
14138255736Sdavidch          "interrupt_mode=%d "
14139255736Sdavidch          "queue_count=%d "
14140255736Sdavidch          "hc_rx_ticks=%d "
14141255736Sdavidch          "hc_tx_ticks=%d "
14142255736Sdavidch          "rx_budget=%d "
14143255736Sdavidch          "max_aggregation_size=%d "
14144255736Sdavidch          "mrrs=%d "
14145255736Sdavidch          "autogreeen=%d "
14146255736Sdavidch          "udp_rss=%d\n",
14147255736Sdavidch          bxe_debug,
14148255736Sdavidch          sc->interrupt_mode,
14149255736Sdavidch          sc->num_queues,
14150255736Sdavidch          sc->hc_rx_ticks,
14151255736Sdavidch          sc->hc_tx_ticks,
14152255736Sdavidch          bxe_rx_budget,
14153255736Sdavidch          sc->max_aggregation_size,
14154255736Sdavidch          sc->mrrs,
14155255736Sdavidch          sc->autogreeen,
14156255736Sdavidch          sc->udp_rss);
14157255736Sdavidch}
14158255736Sdavidch
14159255736Sdavidchstatic void
14160255736Sdavidchbxe_media_detect(struct bxe_softc *sc)
14161255736Sdavidch{
14162255736Sdavidch    uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
14163255736Sdavidch    switch (sc->link_params.phy[phy_idx].media_type) {
14164255736Sdavidch    case ELINK_ETH_PHY_SFPP_10G_FIBER:
14165256299Sedavis    case ELINK_ETH_PHY_XFP_FIBER:
14166256299Sedavis        BLOGI(sc, "Found 10Gb Fiber media.\n");
14167256299Sedavis        sc->media = IFM_10G_SR;
14168256299Sedavis        break;
14169255736Sdavidch    case ELINK_ETH_PHY_SFP_1G_FIBER:
14170256299Sedavis        BLOGI(sc, "Found 1Gb Fiber media.\n");
14171256299Sedavis        sc->media = IFM_1000_SX;
14172256299Sedavis        break;
14173255736Sdavidch    case ELINK_ETH_PHY_KR:
14174255736Sdavidch    case ELINK_ETH_PHY_CX4:
14175255736Sdavidch        BLOGI(sc, "Found 10GBase-CX4 media.\n");
14176255736Sdavidch        sc->media = IFM_10G_CX4;
14177255736Sdavidch        break;
14178255736Sdavidch    case ELINK_ETH_PHY_DA_TWINAX:
14179255736Sdavidch        BLOGI(sc, "Found 10Gb Twinax media.\n");
14180255736Sdavidch        sc->media = IFM_10G_TWINAX;
14181255736Sdavidch        break;
14182255736Sdavidch    case ELINK_ETH_PHY_BASE_T:
14183256299Sedavis        if (sc->link_params.speed_cap_mask[0] &
14184256299Sedavis            PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
14185256299Sedavis            BLOGI(sc, "Found 10GBase-T media.\n");
14186256299Sedavis            sc->media = IFM_10G_T;
14187256299Sedavis        } else {
14188256299Sedavis            BLOGI(sc, "Found 1000Base-T media.\n");
14189256299Sedavis            sc->media = IFM_1000_T;
14190256299Sedavis        }
14191255736Sdavidch        break;
14192255736Sdavidch    case ELINK_ETH_PHY_NOT_PRESENT:
14193255736Sdavidch        BLOGI(sc, "Media not present.\n");
14194255736Sdavidch        sc->media = 0;
14195255736Sdavidch        break;
14196255736Sdavidch    case ELINK_ETH_PHY_UNSPECIFIED:
14197255736Sdavidch    default:
14198255736Sdavidch        BLOGI(sc, "Unknown media!\n");
14199255736Sdavidch        sc->media = 0;
14200255736Sdavidch        break;
14201255736Sdavidch    }
14202255736Sdavidch}
14203255736Sdavidch
14204255736Sdavidch#define GET_FIELD(value, fname)                     \
14205255736Sdavidch    (((value) & (fname##_MASK)) >> (fname##_SHIFT))
14206255736Sdavidch#define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
14207255736Sdavidch#define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
14208255736Sdavidch
14209255736Sdavidchstatic int
14210255736Sdavidchbxe_get_igu_cam_info(struct bxe_softc *sc)
14211255736Sdavidch{
14212255736Sdavidch    int pfid = SC_FUNC(sc);
14213255736Sdavidch    int igu_sb_id;
14214255736Sdavidch    uint32_t val;
14215255736Sdavidch    uint8_t fid, igu_sb_cnt = 0;
14216255736Sdavidch
14217255736Sdavidch    sc->igu_base_sb = 0xff;
14218255736Sdavidch
14219255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
14220255736Sdavidch        int vn = SC_VN(sc);
14221255736Sdavidch        igu_sb_cnt = sc->igu_sb_cnt;
14222255736Sdavidch        sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
14223255736Sdavidch                           FP_SB_MAX_E1x);
14224255736Sdavidch        sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
14225255736Sdavidch                          (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
14226255736Sdavidch        return (0);
14227255736Sdavidch    }
14228255736Sdavidch
14229255736Sdavidch    /* IGU in normal mode - read CAM */
14230255736Sdavidch    for (igu_sb_id = 0;
14231255736Sdavidch         igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
14232255736Sdavidch         igu_sb_id++) {
14233255736Sdavidch        val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
14234255736Sdavidch        if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
14235255736Sdavidch            continue;
14236255736Sdavidch        }
14237255736Sdavidch        fid = IGU_FID(val);
14238255736Sdavidch        if ((fid & IGU_FID_ENCODE_IS_PF)) {
14239255736Sdavidch            if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
14240255736Sdavidch                continue;
14241255736Sdavidch            }
14242255736Sdavidch            if (IGU_VEC(val) == 0) {
14243255736Sdavidch                /* default status block */
14244255736Sdavidch                sc->igu_dsb_id = igu_sb_id;
14245255736Sdavidch            } else {
14246255736Sdavidch                if (sc->igu_base_sb == 0xff) {
14247255736Sdavidch                    sc->igu_base_sb = igu_sb_id;
14248255736Sdavidch                }
14249255736Sdavidch                igu_sb_cnt++;
14250255736Sdavidch            }
14251255736Sdavidch        }
14252255736Sdavidch    }
14253255736Sdavidch
14254255736Sdavidch    /*
14255255736Sdavidch     * Due to new PF resource allocation by MFW T7.4 and above, it's optional
14256255736Sdavidch     * that number of CAM entries will not be equal to the value advertised in
14257255736Sdavidch     * PCI. Driver should use the minimal value of both as the actual status
14258255736Sdavidch     * block count
14259255736Sdavidch     */
14260255736Sdavidch    sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
14261255736Sdavidch
14262255736Sdavidch    if (igu_sb_cnt == 0) {
14263255736Sdavidch        BLOGE(sc, "CAM configuration error\n");
14264255736Sdavidch        return (-1);
14265255736Sdavidch    }
14266255736Sdavidch
14267255736Sdavidch    return (0);
14268255736Sdavidch}
14269255736Sdavidch
14270255736Sdavidch/*
14271255736Sdavidch * Gather various information from the device config space, the device itself,
14272255736Sdavidch * shmem, and the user input.
14273255736Sdavidch */
14274255736Sdavidchstatic int
14275255736Sdavidchbxe_get_device_info(struct bxe_softc *sc)
14276255736Sdavidch{
14277255736Sdavidch    uint32_t val;
14278255736Sdavidch    int rc;
14279255736Sdavidch
14280255736Sdavidch    /* Get the data for the device */
14281255736Sdavidch    sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
14282255736Sdavidch    sc->devinfo.device_id    = pci_get_device(sc->dev);
14283255736Sdavidch    sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
14284255736Sdavidch    sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
14285255736Sdavidch
14286255736Sdavidch    /* get the chip revision (chip metal comes from pci config space) */
14287255736Sdavidch    sc->devinfo.chip_id     =
14288255736Sdavidch    sc->link_params.chip_id =
14289255736Sdavidch        (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
14290255736Sdavidch         ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
14291255736Sdavidch         (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
14292255736Sdavidch         ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
14293255736Sdavidch
14294255736Sdavidch    /* force 57811 according to MISC register */
14295255736Sdavidch    if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
14296255736Sdavidch        if (CHIP_IS_57810(sc)) {
14297255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
14298255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
14299255736Sdavidch        } else if (CHIP_IS_57810_MF(sc)) {
14300255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
14301255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
14302255736Sdavidch        }
14303255736Sdavidch        sc->devinfo.chip_id |= 0x1;
14304255736Sdavidch    }
14305255736Sdavidch
14306255736Sdavidch    BLOGD(sc, DBG_LOAD,
14307255736Sdavidch          "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
14308255736Sdavidch          sc->devinfo.chip_id,
14309255736Sdavidch          ((sc->devinfo.chip_id >> 16) & 0xffff),
14310255736Sdavidch          ((sc->devinfo.chip_id >> 12) & 0xf),
14311255736Sdavidch          ((sc->devinfo.chip_id >>  4) & 0xff),
14312255736Sdavidch          ((sc->devinfo.chip_id >>  0) & 0xf));
14313255736Sdavidch
14314255736Sdavidch    val = (REG_RD(sc, 0x2874) & 0x55);
14315255736Sdavidch    if ((sc->devinfo.chip_id & 0x1) ||
14316255736Sdavidch        (CHIP_IS_E1(sc) && val) ||
14317255736Sdavidch        (CHIP_IS_E1H(sc) && (val == 0x55))) {
14318255736Sdavidch        sc->flags |= BXE_ONE_PORT_FLAG;
14319255736Sdavidch        BLOGD(sc, DBG_LOAD, "single port device\n");
14320255736Sdavidch    }
14321255736Sdavidch
14322255736Sdavidch    /* set the doorbell size */
14323255736Sdavidch    sc->doorbell_size = (1 << BXE_DB_SHIFT);
14324255736Sdavidch
14325255736Sdavidch    /* determine whether the device is in 2 port or 4 port mode */
14326255736Sdavidch    sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
14327255736Sdavidch    if (CHIP_IS_E2E3(sc)) {
14328255736Sdavidch        /*
14329255736Sdavidch         * Read port4mode_en_ovwr[0]:
14330255736Sdavidch         *   If 1, four port mode is in port4mode_en_ovwr[1].
14331255736Sdavidch         *   If 0, four port mode is in port4mode_en[0].
14332255736Sdavidch         */
14333255736Sdavidch        val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
14334255736Sdavidch        if (val & 1) {
14335255736Sdavidch            val = ((val >> 1) & 1);
14336255736Sdavidch        } else {
14337255736Sdavidch            val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
14338255736Sdavidch        }
14339255736Sdavidch
14340255736Sdavidch        sc->devinfo.chip_port_mode =
14341255736Sdavidch            (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
14342255736Sdavidch
14343255736Sdavidch        BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
14344255736Sdavidch    }
14345255736Sdavidch
14346255736Sdavidch    /* get the function and path info for the device */
14347255736Sdavidch    bxe_get_function_num(sc);
14348255736Sdavidch
14349255736Sdavidch    /* get the shared memory base address */
14350255736Sdavidch    sc->devinfo.shmem_base     =
14351255736Sdavidch    sc->link_params.shmem_base =
14352255736Sdavidch        REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
14353255736Sdavidch    sc->devinfo.shmem2_base =
14354255736Sdavidch        REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
14355255736Sdavidch                                  MISC_REG_GENERIC_CR_0));
14356255736Sdavidch
14357255736Sdavidch    BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
14358255736Sdavidch          sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
14359255736Sdavidch
14360255736Sdavidch    if (!sc->devinfo.shmem_base) {
14361255736Sdavidch        /* this should ONLY prevent upcoming shmem reads */
14362255736Sdavidch        BLOGI(sc, "MCP not active\n");
14363255736Sdavidch        sc->flags |= BXE_NO_MCP_FLAG;
14364255736Sdavidch        return (0);
14365255736Sdavidch    }
14366255736Sdavidch
14367255736Sdavidch    /* make sure the shared memory contents are valid */
14368255736Sdavidch    val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
14369255736Sdavidch    if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
14370255736Sdavidch        (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
14371255736Sdavidch        BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
14372255736Sdavidch        return (0);
14373255736Sdavidch    }
14374255736Sdavidch    BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
14375255736Sdavidch
14376255736Sdavidch    /* get the bootcode version */
14377255736Sdavidch    sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
14378255736Sdavidch    snprintf(sc->devinfo.bc_ver_str,
14379255736Sdavidch             sizeof(sc->devinfo.bc_ver_str),
14380255736Sdavidch             "%d.%d.%d",
14381255736Sdavidch             ((sc->devinfo.bc_ver >> 24) & 0xff),
14382255736Sdavidch             ((sc->devinfo.bc_ver >> 16) & 0xff),
14383255736Sdavidch             ((sc->devinfo.bc_ver >>  8) & 0xff));
14384255736Sdavidch    BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
14385255736Sdavidch
14386255736Sdavidch    /* get the bootcode shmem address */
14387255736Sdavidch    sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
14388255736Sdavidch    BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
14389255736Sdavidch
14390255736Sdavidch    /* clean indirect addresses as they're not used */
14391255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
14392255736Sdavidch    if (IS_PF(sc)) {
14393255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
14394255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
14395255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
14396255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
14397255736Sdavidch        if (CHIP_IS_E1x(sc)) {
14398255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
14399255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
14400255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
14401255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
14402255736Sdavidch        }
14403255736Sdavidch
14404255736Sdavidch        /*
14405255736Sdavidch         * Enable internal target-read (in case we are probed after PF
14406255736Sdavidch         * FLR). Must be done prior to any BAR read access. Only for
14407255736Sdavidch         * 57712 and up
14408255736Sdavidch         */
14409255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
14410255736Sdavidch            REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
14411255736Sdavidch        }
14412255736Sdavidch    }
14413255736Sdavidch
14414255736Sdavidch    /* get the nvram size */
14415255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
14416255736Sdavidch    sc->devinfo.flash_size =
14417255736Sdavidch        (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
14418255736Sdavidch    BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
14419255736Sdavidch
14420255736Sdavidch    /* get PCI capabilites */
14421255736Sdavidch    bxe_probe_pci_caps(sc);
14422255736Sdavidch
14423255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
14424255736Sdavidch
14425255736Sdavidch    /* get various configuration parameters from shmem */
14426255736Sdavidch    bxe_get_shmem_info(sc);
14427255736Sdavidch
14428255736Sdavidch    if (sc->devinfo.pcie_msix_cap_reg != 0) {
14429255736Sdavidch        val = pci_read_config(sc->dev,
14430255736Sdavidch                              (sc->devinfo.pcie_msix_cap_reg +
14431255736Sdavidch                               PCIR_MSIX_CTRL),
14432255736Sdavidch                              2);
14433255736Sdavidch        sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
14434255736Sdavidch    } else {
14435255736Sdavidch        sc->igu_sb_cnt = 1;
14436255736Sdavidch    }
14437255736Sdavidch
14438255736Sdavidch    sc->igu_base_addr = BAR_IGU_INTMEM;
14439255736Sdavidch
14440255736Sdavidch    /* initialize IGU parameters */
14441255736Sdavidch    if (CHIP_IS_E1x(sc)) {
14442255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_HC;
14443255736Sdavidch        sc->igu_dsb_id = DEF_SB_IGU_ID;
14444255736Sdavidch        sc->igu_base_sb = 0;
14445255736Sdavidch    } else {
14446255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_IGU;
14447255736Sdavidch
14448255736Sdavidch        /* do not allow device reset during IGU info preocessing */
14449255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14450255736Sdavidch
14451255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
14452255736Sdavidch
14453255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14454255736Sdavidch            int tout = 5000;
14455255736Sdavidch
14456255736Sdavidch            BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
14457255736Sdavidch
14458255736Sdavidch            val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
14459255736Sdavidch            REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
14460255736Sdavidch            REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
14461255736Sdavidch
14462255736Sdavidch            while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14463255736Sdavidch                tout--;
14464255736Sdavidch                DELAY(1000);
14465255736Sdavidch            }
14466255736Sdavidch
14467255736Sdavidch            if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14468255736Sdavidch                BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
14469255736Sdavidch                bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14470255736Sdavidch                return (-1);
14471255736Sdavidch            }
14472255736Sdavidch        }
14473255736Sdavidch
14474255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14475255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
14476255736Sdavidch            sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
14477255736Sdavidch        } else {
14478255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
14479255736Sdavidch        }
14480255736Sdavidch
14481255736Sdavidch        rc = bxe_get_igu_cam_info(sc);
14482255736Sdavidch
14483255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14484255736Sdavidch
14485255736Sdavidch        if (rc) {
14486255736Sdavidch            return (rc);
14487255736Sdavidch        }
14488255736Sdavidch    }
14489255736Sdavidch
14490255736Sdavidch    /*
14491255736Sdavidch     * Get base FW non-default (fast path) status block ID. This value is
14492255736Sdavidch     * used to initialize the fw_sb_id saved on the fp/queue structure to
14493255736Sdavidch     * determine the id used by the FW.
14494255736Sdavidch     */
14495255736Sdavidch    if (CHIP_IS_E1x(sc)) {
14496255736Sdavidch        sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
14497255736Sdavidch    } else {
14498255736Sdavidch        /*
14499255736Sdavidch         * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
14500255736Sdavidch         * the same queue are indicated on the same IGU SB). So we prefer
14501255736Sdavidch         * FW and IGU SBs to be the same value.
14502255736Sdavidch         */
14503255736Sdavidch        sc->base_fw_ndsb = sc->igu_base_sb;
14504255736Sdavidch    }
14505255736Sdavidch
14506255736Sdavidch    BLOGD(sc, DBG_LOAD,
14507255736Sdavidch          "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
14508255736Sdavidch          sc->igu_dsb_id, sc->igu_base_sb,
14509255736Sdavidch          sc->igu_sb_cnt, sc->base_fw_ndsb);
14510255736Sdavidch
14511255736Sdavidch    elink_phy_probe(&sc->link_params);
14512255736Sdavidch
14513255736Sdavidch    return (0);
14514255736Sdavidch}
14515255736Sdavidch
14516255736Sdavidchstatic void
14517255736Sdavidchbxe_link_settings_supported(struct bxe_softc *sc,
14518255736Sdavidch                            uint32_t         switch_cfg)
14519255736Sdavidch{
14520255736Sdavidch    uint32_t cfg_size = 0;
14521255736Sdavidch    uint32_t idx;
14522255736Sdavidch    uint8_t port = SC_PORT(sc);
14523255736Sdavidch
14524255736Sdavidch    /* aggregation of supported attributes of all external phys */
14525255736Sdavidch    sc->port.supported[0] = 0;
14526255736Sdavidch    sc->port.supported[1] = 0;
14527255736Sdavidch
14528255736Sdavidch    switch (sc->link_params.num_phys) {
14529255736Sdavidch    case 1:
14530255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
14531255736Sdavidch        cfg_size = 1;
14532255736Sdavidch        break;
14533255736Sdavidch    case 2:
14534255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
14535255736Sdavidch        cfg_size = 1;
14536255736Sdavidch        break;
14537255736Sdavidch    case 3:
14538255736Sdavidch        if (sc->link_params.multi_phy_config &
14539255736Sdavidch            PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
14540255736Sdavidch            sc->port.supported[1] =
14541255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
14542255736Sdavidch            sc->port.supported[0] =
14543255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
14544255736Sdavidch        } else {
14545255736Sdavidch            sc->port.supported[0] =
14546255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
14547255736Sdavidch            sc->port.supported[1] =
14548255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
14549255736Sdavidch        }
14550255736Sdavidch        cfg_size = 2;
14551255736Sdavidch        break;
14552255736Sdavidch    }
14553255736Sdavidch
14554255736Sdavidch    if (!(sc->port.supported[0] || sc->port.supported[1])) {
14555255736Sdavidch        BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
14556255736Sdavidch              SHMEM_RD(sc,
14557255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config),
14558255736Sdavidch              SHMEM_RD(sc,
14559255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config2));
14560255736Sdavidch        return;
14561255736Sdavidch    }
14562255736Sdavidch
14563255736Sdavidch    if (CHIP_IS_E3(sc))
14564255736Sdavidch        sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
14565255736Sdavidch    else {
14566255736Sdavidch        switch (switch_cfg) {
14567255736Sdavidch        case ELINK_SWITCH_CFG_1G:
14568255736Sdavidch            sc->port.phy_addr =
14569255736Sdavidch                REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
14570255736Sdavidch            break;
14571255736Sdavidch        case ELINK_SWITCH_CFG_10G:
14572255736Sdavidch            sc->port.phy_addr =
14573255736Sdavidch                REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
14574255736Sdavidch            break;
14575255736Sdavidch        default:
14576255736Sdavidch            BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14577255736Sdavidch                  sc->port.link_config[0]);
14578255736Sdavidch            return;
14579255736Sdavidch        }
14580255736Sdavidch    }
14581255736Sdavidch
14582255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14583255736Sdavidch
14584255736Sdavidch    /* mask what we support according to speed_cap_mask per configuration */
14585255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14586255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14587255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14588255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14589255736Sdavidch        }
14590255736Sdavidch
14591255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14592255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14593255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14594255736Sdavidch        }
14595255736Sdavidch
14596255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14597255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14598255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14599255736Sdavidch        }
14600255736Sdavidch
14601255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14602255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14603255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14604255736Sdavidch        }
14605255736Sdavidch
14606255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14607255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14608255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14609255736Sdavidch        }
14610255736Sdavidch
14611255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14612255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14613255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14614255736Sdavidch        }
14615255736Sdavidch
14616255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14617255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14618255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14619255736Sdavidch        }
14620255736Sdavidch
14621255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14622255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14623255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14624255736Sdavidch        }
14625255736Sdavidch    }
14626255736Sdavidch
14627255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14628255736Sdavidch          sc->port.supported[0], sc->port.supported[1]);
14629255736Sdavidch}
14630255736Sdavidch
14631255736Sdavidchstatic void
14632255736Sdavidchbxe_link_settings_requested(struct bxe_softc *sc)
14633255736Sdavidch{
14634255736Sdavidch    uint32_t link_config;
14635255736Sdavidch    uint32_t idx;
14636255736Sdavidch    uint32_t cfg_size = 0;
14637255736Sdavidch
14638255736Sdavidch    sc->port.advertising[0] = 0;
14639255736Sdavidch    sc->port.advertising[1] = 0;
14640255736Sdavidch
14641255736Sdavidch    switch (sc->link_params.num_phys) {
14642255736Sdavidch    case 1:
14643255736Sdavidch    case 2:
14644255736Sdavidch        cfg_size = 1;
14645255736Sdavidch        break;
14646255736Sdavidch    case 3:
14647255736Sdavidch        cfg_size = 2;
14648255736Sdavidch        break;
14649255736Sdavidch    }
14650255736Sdavidch
14651255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14652255736Sdavidch        sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14653255736Sdavidch        link_config = sc->port.link_config[idx];
14654255736Sdavidch
14655255736Sdavidch        switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14656255736Sdavidch        case PORT_FEATURE_LINK_SPEED_AUTO:
14657255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14658255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14659255736Sdavidch                sc->port.advertising[idx] |= sc->port.supported[idx];
14660255736Sdavidch                if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14661255736Sdavidch                    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14662255736Sdavidch                    sc->port.advertising[idx] |=
14663255736Sdavidch                        (ELINK_SUPPORTED_100baseT_Half |
14664255736Sdavidch                         ELINK_SUPPORTED_100baseT_Full);
14665255736Sdavidch            } else {
14666255736Sdavidch                /* force 10G, no AN */
14667255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14668255736Sdavidch                sc->port.advertising[idx] |=
14669255736Sdavidch                    (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14670255736Sdavidch                continue;
14671255736Sdavidch            }
14672255736Sdavidch            break;
14673255736Sdavidch
14674255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_FULL:
14675255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14676255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14677255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14678255736Sdavidch                                              ADVERTISED_TP);
14679255736Sdavidch            } else {
14680255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14681255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14682255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14683255736Sdavidch                return;
14684255736Sdavidch            }
14685255736Sdavidch            break;
14686255736Sdavidch
14687255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_HALF:
14688255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14689255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14690255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14691255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14692255736Sdavidch                                              ADVERTISED_TP);
14693255736Sdavidch            } else {
14694255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14695255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14696255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14697255736Sdavidch                return;
14698255736Sdavidch            }
14699255736Sdavidch            break;
14700255736Sdavidch
14701255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_FULL:
14702255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14703255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14704255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14705255736Sdavidch                                              ADVERTISED_TP);
14706255736Sdavidch            } else {
14707255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14708255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14709255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14710255736Sdavidch                return;
14711255736Sdavidch            }
14712255736Sdavidch            break;
14713255736Sdavidch
14714255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_HALF:
14715255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14716255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14717255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14718255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14719255736Sdavidch                                              ADVERTISED_TP);
14720255736Sdavidch            } else {
14721255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14722255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14723255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14724255736Sdavidch                return;
14725255736Sdavidch            }
14726255736Sdavidch            break;
14727255736Sdavidch
14728255736Sdavidch        case PORT_FEATURE_LINK_SPEED_1G:
14729255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14730255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14731255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14732255736Sdavidch                                              ADVERTISED_TP);
14733255736Sdavidch            } else {
14734255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14735255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14736255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14737255736Sdavidch                return;
14738255736Sdavidch            }
14739255736Sdavidch            break;
14740255736Sdavidch
14741255736Sdavidch        case PORT_FEATURE_LINK_SPEED_2_5G:
14742255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14743255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14744255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14745255736Sdavidch                                              ADVERTISED_TP);
14746255736Sdavidch            } else {
14747255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14748255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14749255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14750255736Sdavidch                return;
14751255736Sdavidch            }
14752255736Sdavidch            break;
14753255736Sdavidch
14754255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10G_CX4:
14755255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14756255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14757255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14758255736Sdavidch                                              ADVERTISED_FIBRE);
14759255736Sdavidch            } else {
14760255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14761255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14762255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14763255736Sdavidch                return;
14764255736Sdavidch            }
14765255736Sdavidch            break;
14766255736Sdavidch
14767255736Sdavidch        case PORT_FEATURE_LINK_SPEED_20G:
14768255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14769255736Sdavidch            break;
14770255736Sdavidch
14771255736Sdavidch        default:
14772255736Sdavidch            BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14773255736Sdavidch                      "speed_cap_mask=0x%08x\n",
14774255736Sdavidch                  link_config, sc->link_params.speed_cap_mask[idx]);
14775255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14776255736Sdavidch            sc->port.advertising[idx] = sc->port.supported[idx];
14777255736Sdavidch            break;
14778255736Sdavidch        }
14779255736Sdavidch
14780255736Sdavidch        sc->link_params.req_flow_ctrl[idx] =
14781255736Sdavidch            (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14782255736Sdavidch
14783255736Sdavidch        if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14784255736Sdavidch            if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14785255736Sdavidch                sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14786255736Sdavidch            } else {
14787255736Sdavidch                bxe_set_requested_fc(sc);
14788255736Sdavidch            }
14789255736Sdavidch        }
14790255736Sdavidch
14791255736Sdavidch        BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14792255736Sdavidch                            "req_flow_ctrl=0x%x advertising=0x%x\n",
14793255736Sdavidch              sc->link_params.req_line_speed[idx],
14794255736Sdavidch              sc->link_params.req_duplex[idx],
14795255736Sdavidch              sc->link_params.req_flow_ctrl[idx],
14796255736Sdavidch              sc->port.advertising[idx]);
14797255736Sdavidch    }
14798255736Sdavidch}
14799255736Sdavidch
14800255736Sdavidchstatic void
14801255736Sdavidchbxe_get_phy_info(struct bxe_softc *sc)
14802255736Sdavidch{
14803255736Sdavidch    uint8_t port = SC_PORT(sc);
14804255736Sdavidch    uint32_t config = sc->port.config;
14805255736Sdavidch    uint32_t eee_mode;
14806255736Sdavidch
14807255736Sdavidch    /* shmem data already read in bxe_get_shmem_info() */
14808255736Sdavidch
14809255736Sdavidch    BLOGD(sc, DBG_LOAD, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14810255736Sdavidch                        "link_config0=0x%08x\n",
14811255736Sdavidch               sc->link_params.lane_config,
14812255736Sdavidch               sc->link_params.speed_cap_mask[0],
14813255736Sdavidch               sc->port.link_config[0]);
14814255736Sdavidch
14815255736Sdavidch    bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14816255736Sdavidch    bxe_link_settings_requested(sc);
14817255736Sdavidch
14818255736Sdavidch    if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14819255736Sdavidch        sc->link_params.feature_config_flags |=
14820255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14821255736Sdavidch    } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14822255736Sdavidch        sc->link_params.feature_config_flags &=
14823255736Sdavidch            ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14824255736Sdavidch    } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14825255736Sdavidch        sc->link_params.feature_config_flags |=
14826255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14827255736Sdavidch    }
14828255736Sdavidch
14829255736Sdavidch    /* configure link feature according to nvram value */
14830255736Sdavidch    eee_mode =
14831255736Sdavidch        (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14832255736Sdavidch          PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14833255736Sdavidch         PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14834255736Sdavidch    if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14835255736Sdavidch        sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14836255736Sdavidch                                    ELINK_EEE_MODE_ENABLE_LPI |
14837255736Sdavidch                                    ELINK_EEE_MODE_OUTPUT_TIME);
14838255736Sdavidch    } else {
14839255736Sdavidch        sc->link_params.eee_mode = 0;
14840255736Sdavidch    }
14841255736Sdavidch
14842255736Sdavidch    /* get the media type */
14843255736Sdavidch    bxe_media_detect(sc);
14844255736Sdavidch}
14845255736Sdavidch
14846255736Sdavidchstatic void
14847255736Sdavidchbxe_get_params(struct bxe_softc *sc)
14848255736Sdavidch{
14849255736Sdavidch    /* get user tunable params */
14850255736Sdavidch    bxe_get_tunable_params(sc);
14851255736Sdavidch
14852255736Sdavidch    /* select the RX and TX ring sizes */
14853255736Sdavidch    sc->tx_ring_size = TX_BD_USABLE;
14854255736Sdavidch    sc->rx_ring_size = RX_BD_USABLE;
14855255736Sdavidch
14856255736Sdavidch    /* XXX disable WoL */
14857255736Sdavidch    sc->wol = 0;
14858255736Sdavidch}
14859255736Sdavidch
14860255736Sdavidchstatic void
14861255736Sdavidchbxe_set_modes_bitmap(struct bxe_softc *sc)
14862255736Sdavidch{
14863255736Sdavidch    uint32_t flags = 0;
14864255736Sdavidch
14865255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
14866255736Sdavidch        SET_FLAGS(flags, MODE_FPGA);
14867255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
14868255736Sdavidch        SET_FLAGS(flags, MODE_EMUL);
14869255736Sdavidch    } else {
14870255736Sdavidch        SET_FLAGS(flags, MODE_ASIC);
14871255736Sdavidch    }
14872255736Sdavidch
14873255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
14874255736Sdavidch        SET_FLAGS(flags, MODE_PORT4);
14875255736Sdavidch    } else {
14876255736Sdavidch        SET_FLAGS(flags, MODE_PORT2);
14877255736Sdavidch    }
14878255736Sdavidch
14879255736Sdavidch    if (CHIP_IS_E2(sc)) {
14880255736Sdavidch        SET_FLAGS(flags, MODE_E2);
14881255736Sdavidch    } else if (CHIP_IS_E3(sc)) {
14882255736Sdavidch        SET_FLAGS(flags, MODE_E3);
14883255736Sdavidch        if (CHIP_REV(sc) == CHIP_REV_Ax) {
14884255736Sdavidch            SET_FLAGS(flags, MODE_E3_A0);
14885255736Sdavidch        } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14886255736Sdavidch            SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14887255736Sdavidch        }
14888255736Sdavidch    }
14889255736Sdavidch
14890255736Sdavidch    if (IS_MF(sc)) {
14891255736Sdavidch        SET_FLAGS(flags, MODE_MF);
14892255736Sdavidch        switch (sc->devinfo.mf_info.mf_mode) {
14893255736Sdavidch        case MULTI_FUNCTION_SD:
14894255736Sdavidch            SET_FLAGS(flags, MODE_MF_SD);
14895255736Sdavidch            break;
14896255736Sdavidch        case MULTI_FUNCTION_SI:
14897255736Sdavidch            SET_FLAGS(flags, MODE_MF_SI);
14898255736Sdavidch            break;
14899255736Sdavidch        case MULTI_FUNCTION_AFEX:
14900255736Sdavidch            SET_FLAGS(flags, MODE_MF_AFEX);
14901255736Sdavidch            break;
14902255736Sdavidch        }
14903255736Sdavidch    } else {
14904255736Sdavidch        SET_FLAGS(flags, MODE_SF);
14905255736Sdavidch    }
14906255736Sdavidch
14907255736Sdavidch#if defined(__LITTLE_ENDIAN)
14908255736Sdavidch    SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14909255736Sdavidch#else /* __BIG_ENDIAN */
14910255736Sdavidch    SET_FLAGS(flags, MODE_BIG_ENDIAN);
14911255736Sdavidch#endif
14912255736Sdavidch
14913255736Sdavidch    INIT_MODE_FLAGS(sc) = flags;
14914255736Sdavidch}
14915255736Sdavidch
14916255736Sdavidchstatic int
14917255736Sdavidchbxe_alloc_hsi_mem(struct bxe_softc *sc)
14918255736Sdavidch{
14919255736Sdavidch    struct bxe_fastpath *fp;
14920255736Sdavidch    bus_addr_t busaddr;
14921255736Sdavidch    int max_agg_queues;
14922255736Sdavidch    int max_segments;
14923255736Sdavidch    bus_size_t max_size;
14924255736Sdavidch    bus_size_t max_seg_size;
14925255736Sdavidch    char buf[32];
14926255736Sdavidch    int rc;
14927255736Sdavidch    int i, j;
14928255736Sdavidch
14929255736Sdavidch    /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14930255736Sdavidch
14931255736Sdavidch    /* allocate the parent bus DMA tag */
14932255736Sdavidch    rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14933255736Sdavidch                            1,                        /* alignment */
14934255736Sdavidch                            0,                        /* boundary limit */
14935255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted low */
14936255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted hi */
14937255736Sdavidch                            NULL,                     /* addr filter() */
14938255736Sdavidch                            NULL,                     /* addr filter() arg */
14939255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14940255736Sdavidch                            BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14941255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14942255736Sdavidch                            0,                        /* flags */
14943255736Sdavidch                            NULL,                     /* lock() */
14944255736Sdavidch                            NULL,                     /* lock() arg */
14945255736Sdavidch                            &sc->parent_dma_tag);     /* returned dma tag */
14946255736Sdavidch    if (rc != 0) {
14947255736Sdavidch        BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14948255736Sdavidch        return (1);
14949255736Sdavidch    }
14950255736Sdavidch
14951255736Sdavidch    /************************/
14952255736Sdavidch    /* DEFAULT STATUS BLOCK */
14953255736Sdavidch    /************************/
14954255736Sdavidch
14955255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14956255736Sdavidch                      &sc->def_sb_dma, "default status block") != 0) {
14957255736Sdavidch        /* XXX */
14958255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14959255736Sdavidch        return (1);
14960255736Sdavidch    }
14961255736Sdavidch
14962255736Sdavidch    sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14963255736Sdavidch
14964255736Sdavidch    /***************/
14965255736Sdavidch    /* EVENT QUEUE */
14966255736Sdavidch    /***************/
14967255736Sdavidch
14968255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14969255736Sdavidch                      &sc->eq_dma, "event queue") != 0) {
14970255736Sdavidch        /* XXX */
14971255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14972255736Sdavidch        sc->def_sb = NULL;
14973255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14974255736Sdavidch        return (1);
14975255736Sdavidch    }
14976255736Sdavidch
14977255736Sdavidch    sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14978255736Sdavidch
14979255736Sdavidch    /*************/
14980255736Sdavidch    /* SLOW PATH */
14981255736Sdavidch    /*************/
14982255736Sdavidch
14983255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14984255736Sdavidch                      &sc->sp_dma, "slow path") != 0) {
14985255736Sdavidch        /* XXX */
14986255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14987255736Sdavidch        sc->eq = NULL;
14988255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14989255736Sdavidch        sc->def_sb = NULL;
14990255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14991255736Sdavidch        return (1);
14992255736Sdavidch    }
14993255736Sdavidch
14994255736Sdavidch    sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14995255736Sdavidch
14996255736Sdavidch    /*******************/
14997255736Sdavidch    /* SLOW PATH QUEUE */
14998255736Sdavidch    /*******************/
14999255736Sdavidch
15000255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
15001255736Sdavidch                      &sc->spq_dma, "slow path queue") != 0) {
15002255736Sdavidch        /* XXX */
15003255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
15004255736Sdavidch        sc->sp = NULL;
15005255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
15006255736Sdavidch        sc->eq = NULL;
15007255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
15008255736Sdavidch        sc->def_sb = NULL;
15009255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
15010255736Sdavidch        return (1);
15011255736Sdavidch    }
15012255736Sdavidch
15013255736Sdavidch    sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
15014255736Sdavidch
15015255736Sdavidch    /***************************/
15016255736Sdavidch    /* FW DECOMPRESSION BUFFER */
15017255736Sdavidch    /***************************/
15018255736Sdavidch
15019255736Sdavidch    if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
15020255736Sdavidch                      "fw decompression buffer") != 0) {
15021255736Sdavidch        /* XXX */
15022255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
15023255736Sdavidch        sc->spq = NULL;
15024255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
15025255736Sdavidch        sc->sp = NULL;
15026255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
15027255736Sdavidch        sc->eq = NULL;
15028255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
15029255736Sdavidch        sc->def_sb = NULL;
15030255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
15031255736Sdavidch        return (1);
15032255736Sdavidch    }
15033255736Sdavidch
15034255736Sdavidch    sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
15035255736Sdavidch
15036255736Sdavidch    if ((sc->gz_strm =
15037255736Sdavidch         malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
15038255736Sdavidch        /* XXX */
15039255736Sdavidch        bxe_dma_free(sc, &sc->gz_buf_dma);
15040255736Sdavidch        sc->gz_buf = NULL;
15041255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
15042255736Sdavidch        sc->spq = NULL;
15043255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
15044255736Sdavidch        sc->sp = NULL;
15045255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
15046255736Sdavidch        sc->eq = NULL;
15047255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
15048255736Sdavidch        sc->def_sb = NULL;
15049255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
15050255736Sdavidch        return (1);
15051255736Sdavidch    }
15052255736Sdavidch
15053255736Sdavidch    /*************/
15054255736Sdavidch    /* FASTPATHS */
15055255736Sdavidch    /*************/
15056255736Sdavidch
15057255736Sdavidch    /* allocate DMA memory for each fastpath structure */
15058255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
15059255736Sdavidch        fp = &sc->fp[i];
15060255736Sdavidch        fp->sc    = sc;
15061255736Sdavidch        fp->index = i;
15062255736Sdavidch
15063255736Sdavidch        /*******************/
15064255736Sdavidch        /* FP STATUS BLOCK */
15065255736Sdavidch        /*******************/
15066255736Sdavidch
15067255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d status block", i);
15068255736Sdavidch        if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
15069255736Sdavidch                          &fp->sb_dma, buf) != 0) {
15070255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15071255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
15072255736Sdavidch            return (1);
15073255736Sdavidch        } else {
15074255736Sdavidch            if (CHIP_IS_E2E3(sc)) {
15075255736Sdavidch                fp->status_block.e2_sb =
15076255736Sdavidch                    (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
15077255736Sdavidch            } else {
15078255736Sdavidch                fp->status_block.e1x_sb =
15079255736Sdavidch                    (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
15080255736Sdavidch            }
15081255736Sdavidch        }
15082255736Sdavidch
15083255736Sdavidch        /******************/
15084255736Sdavidch        /* FP TX BD CHAIN */
15085255736Sdavidch        /******************/
15086255736Sdavidch
15087255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
15088255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
15089255736Sdavidch                          &fp->tx_dma, buf) != 0) {
15090255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15091255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
15092255736Sdavidch            return (1);
15093255736Sdavidch        } else {
15094255736Sdavidch            fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
15095255736Sdavidch        }
15096255736Sdavidch
15097255736Sdavidch        /* link together the tx bd chain pages */
15098255736Sdavidch        for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
15099255736Sdavidch            /* index into the tx bd chain array to last entry per page */
15100255736Sdavidch            struct eth_tx_next_bd *tx_next_bd =
15101255736Sdavidch                &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
15102255736Sdavidch            /* point to the next page and wrap from last page */
15103255736Sdavidch            busaddr = (fp->tx_dma.paddr +
15104255736Sdavidch                       (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
15105255736Sdavidch            tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
15106255736Sdavidch            tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
15107255736Sdavidch        }
15108255736Sdavidch
15109255736Sdavidch        /******************/
15110255736Sdavidch        /* FP RX BD CHAIN */
15111255736Sdavidch        /******************/
15112255736Sdavidch
15113255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
15114255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
15115255736Sdavidch                          &fp->rx_dma, buf) != 0) {
15116255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15117255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
15118255736Sdavidch            return (1);
15119255736Sdavidch        } else {
15120255736Sdavidch            fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
15121255736Sdavidch        }
15122255736Sdavidch
15123255736Sdavidch        /* link together the rx bd chain pages */
15124255736Sdavidch        for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
15125255736Sdavidch            /* index into the rx bd chain array to last entry per page */
15126255736Sdavidch            struct eth_rx_bd *rx_bd =
15127255736Sdavidch                &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
15128255736Sdavidch            /* point to the next page and wrap from last page */
15129255736Sdavidch            busaddr = (fp->rx_dma.paddr +
15130255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
15131255736Sdavidch            rx_bd->addr_hi = htole32(U64_HI(busaddr));
15132255736Sdavidch            rx_bd->addr_lo = htole32(U64_LO(busaddr));
15133255736Sdavidch        }
15134255736Sdavidch
15135255736Sdavidch        /*******************/
15136255736Sdavidch        /* FP RX RCQ CHAIN */
15137255736Sdavidch        /*******************/
15138255736Sdavidch
15139255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
15140255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
15141255736Sdavidch                          &fp->rcq_dma, buf) != 0) {
15142255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15143255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
15144255736Sdavidch            return (1);
15145255736Sdavidch        } else {
15146255736Sdavidch            fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
15147255736Sdavidch        }
15148255736Sdavidch
15149255736Sdavidch        /* link together the rcq chain pages */
15150255736Sdavidch        for (j = 1; j <= RCQ_NUM_PAGES; j++) {
15151255736Sdavidch            /* index into the rcq chain array to last entry per page */
15152255736Sdavidch            struct eth_rx_cqe_next_page *rx_cqe_next =
15153255736Sdavidch                (struct eth_rx_cqe_next_page *)
15154255736Sdavidch                &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
15155255736Sdavidch            /* point to the next page and wrap from last page */
15156255736Sdavidch            busaddr = (fp->rcq_dma.paddr +
15157255736Sdavidch                       (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
15158255736Sdavidch            rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
15159255736Sdavidch            rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
15160255736Sdavidch        }
15161255736Sdavidch
15162255736Sdavidch        /*******************/
15163255736Sdavidch        /* FP RX SGE CHAIN */
15164255736Sdavidch        /*******************/
15165255736Sdavidch
15166255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d sge chain", i);
15167255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
15168255736Sdavidch                          &fp->rx_sge_dma, buf) != 0) {
15169255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15170255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
15171255736Sdavidch            return (1);
15172255736Sdavidch        } else {
15173255736Sdavidch            fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
15174255736Sdavidch        }
15175255736Sdavidch
15176255736Sdavidch        /* link together the sge chain pages */
15177255736Sdavidch        for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
15178255736Sdavidch            /* index into the rcq chain array to last entry per page */
15179255736Sdavidch            struct eth_rx_sge *rx_sge =
15180255736Sdavidch                &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
15181255736Sdavidch            /* point to the next page and wrap from last page */
15182255736Sdavidch            busaddr = (fp->rx_sge_dma.paddr +
15183255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
15184255736Sdavidch            rx_sge->addr_hi = htole32(U64_HI(busaddr));
15185255736Sdavidch            rx_sge->addr_lo = htole32(U64_LO(busaddr));
15186255736Sdavidch        }
15187255736Sdavidch
15188255736Sdavidch        /***********************/
15189255736Sdavidch        /* FP TX MBUF DMA MAPS */
15190255736Sdavidch        /***********************/
15191255736Sdavidch
15192255736Sdavidch        /* set required sizes before mapping to conserve resources */
15193266979Smarcel        if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
15194255736Sdavidch            max_size     = BXE_TSO_MAX_SIZE;
15195255736Sdavidch            max_segments = BXE_TSO_MAX_SEGMENTS;
15196255736Sdavidch            max_seg_size = BXE_TSO_MAX_SEG_SIZE;
15197255736Sdavidch        } else {
15198255736Sdavidch            max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
15199255736Sdavidch            max_segments = BXE_MAX_SEGMENTS;
15200255736Sdavidch            max_seg_size = MCLBYTES;
15201255736Sdavidch        }
15202255736Sdavidch
15203255736Sdavidch        /* create a dma tag for the tx mbufs */
15204255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15205255736Sdavidch                                1,                  /* alignment */
15206255736Sdavidch                                0,                  /* boundary limit */
15207255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
15208255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
15209255736Sdavidch                                NULL,               /* addr filter() */
15210255736Sdavidch                                NULL,               /* addr filter() arg */
15211255736Sdavidch                                max_size,           /* max map size */
15212255736Sdavidch                                max_segments,       /* num discontinuous */
15213255736Sdavidch                                max_seg_size,       /* max seg size */
15214255736Sdavidch                                0,                  /* flags */
15215255736Sdavidch                                NULL,               /* lock() */
15216255736Sdavidch                                NULL,               /* lock() arg */
15217255736Sdavidch                                &fp->tx_mbuf_tag);  /* returned dma tag */
15218255736Sdavidch        if (rc != 0) {
15219255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15220255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
15221255736Sdavidch                      "'fp %d tx mbufs' (%d)\n",
15222255736Sdavidch                  i, rc);
15223255736Sdavidch            return (1);
15224255736Sdavidch        }
15225255736Sdavidch
15226255736Sdavidch        /* create dma maps for each of the tx mbuf clusters */
15227255736Sdavidch        for (j = 0; j < TX_BD_TOTAL; j++) {
15228255736Sdavidch            if (bus_dmamap_create(fp->tx_mbuf_tag,
15229255736Sdavidch                                  BUS_DMA_NOWAIT,
15230255736Sdavidch                                  &fp->tx_mbuf_chain[j].m_map)) {
15231255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15232255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15233255736Sdavidch                          "'fp %d tx mbuf %d' (%d)\n",
15234255736Sdavidch                      i, j, rc);
15235255736Sdavidch                return (1);
15236255736Sdavidch            }
15237255736Sdavidch        }
15238255736Sdavidch
15239255736Sdavidch        /***********************/
15240255736Sdavidch        /* FP RX MBUF DMA MAPS */
15241255736Sdavidch        /***********************/
15242255736Sdavidch
15243255736Sdavidch        /* create a dma tag for the rx mbufs */
15244255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15245255736Sdavidch                                1,                  /* alignment */
15246255736Sdavidch                                0,                  /* boundary limit */
15247255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
15248255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
15249255736Sdavidch                                NULL,               /* addr filter() */
15250255736Sdavidch                                NULL,               /* addr filter() arg */
15251255736Sdavidch                                MJUM9BYTES,         /* max map size */
15252255736Sdavidch                                1,                  /* num discontinuous */
15253255736Sdavidch                                MJUM9BYTES,         /* max seg size */
15254255736Sdavidch                                0,                  /* flags */
15255255736Sdavidch                                NULL,               /* lock() */
15256255736Sdavidch                                NULL,               /* lock() arg */
15257255736Sdavidch                                &fp->rx_mbuf_tag);  /* returned dma tag */
15258255736Sdavidch        if (rc != 0) {
15259255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15260255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
15261255736Sdavidch                      "'fp %d rx mbufs' (%d)\n",
15262255736Sdavidch                  i, rc);
15263255736Sdavidch            return (1);
15264255736Sdavidch        }
15265255736Sdavidch
15266255736Sdavidch        /* create dma maps for each of the rx mbuf clusters */
15267255736Sdavidch        for (j = 0; j < RX_BD_TOTAL; j++) {
15268255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
15269255736Sdavidch                                  BUS_DMA_NOWAIT,
15270255736Sdavidch                                  &fp->rx_mbuf_chain[j].m_map)) {
15271255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15272255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15273255736Sdavidch                          "'fp %d rx mbuf %d' (%d)\n",
15274255736Sdavidch                      i, j, rc);
15275255736Sdavidch                return (1);
15276255736Sdavidch            }
15277255736Sdavidch        }
15278255736Sdavidch
15279255736Sdavidch        /* create dma map for the spare rx mbuf cluster */
15280255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
15281255736Sdavidch                              BUS_DMA_NOWAIT,
15282255736Sdavidch                              &fp->rx_mbuf_spare_map)) {
15283255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15284255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
15285255736Sdavidch                      "'fp %d spare rx mbuf' (%d)\n",
15286255736Sdavidch                  i, rc);
15287255736Sdavidch            return (1);
15288255736Sdavidch        }
15289255736Sdavidch
15290255736Sdavidch        /***************************/
15291255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
15292255736Sdavidch        /***************************/
15293255736Sdavidch
15294255736Sdavidch        /* create a dma tag for the rx sge mbufs */
15295255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15296255736Sdavidch                                1,                  /* alignment */
15297255736Sdavidch                                0,                  /* boundary limit */
15298255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
15299255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
15300255736Sdavidch                                NULL,               /* addr filter() */
15301255736Sdavidch                                NULL,               /* addr filter() arg */
15302255736Sdavidch                                BCM_PAGE_SIZE,      /* max map size */
15303255736Sdavidch                                1,                  /* num discontinuous */
15304255736Sdavidch                                BCM_PAGE_SIZE,      /* max seg size */
15305255736Sdavidch                                0,                  /* flags */
15306255736Sdavidch                                NULL,               /* lock() */
15307255736Sdavidch                                NULL,               /* lock() arg */
15308255736Sdavidch                                &fp->rx_sge_mbuf_tag); /* returned dma tag */
15309255736Sdavidch        if (rc != 0) {
15310255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15311255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
15312255736Sdavidch                      "'fp %d rx sge mbufs' (%d)\n",
15313255736Sdavidch                  i, rc);
15314255736Sdavidch            return (1);
15315255736Sdavidch        }
15316255736Sdavidch
15317255736Sdavidch        /* create dma maps for the rx sge mbuf clusters */
15318255736Sdavidch        for (j = 0; j < RX_SGE_TOTAL; j++) {
15319255736Sdavidch            if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15320255736Sdavidch                                  BUS_DMA_NOWAIT,
15321255736Sdavidch                                  &fp->rx_sge_mbuf_chain[j].m_map)) {
15322255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15323255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15324255736Sdavidch                          "'fp %d rx sge mbuf %d' (%d)\n",
15325255736Sdavidch                      i, j, rc);
15326255736Sdavidch                return (1);
15327255736Sdavidch            }
15328255736Sdavidch        }
15329255736Sdavidch
15330255736Sdavidch        /* create dma map for the spare rx sge mbuf cluster */
15331255736Sdavidch        if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15332255736Sdavidch                              BUS_DMA_NOWAIT,
15333255736Sdavidch                              &fp->rx_sge_mbuf_spare_map)) {
15334255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15335255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
15336255736Sdavidch                      "'fp %d spare rx sge mbuf' (%d)\n",
15337255736Sdavidch                  i, rc);
15338255736Sdavidch            return (1);
15339255736Sdavidch        }
15340255736Sdavidch
15341255736Sdavidch        /***************************/
15342255736Sdavidch        /* FP RX TPA MBUF DMA MAPS */
15343255736Sdavidch        /***************************/
15344255736Sdavidch
15345255736Sdavidch        /* create dma maps for the rx tpa mbuf clusters */
15346255736Sdavidch        max_agg_queues = MAX_AGG_QS(sc);
15347255736Sdavidch
15348255736Sdavidch        for (j = 0; j < max_agg_queues; j++) {
15349255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
15350255736Sdavidch                                  BUS_DMA_NOWAIT,
15351255736Sdavidch                                  &fp->rx_tpa_info[j].bd.m_map)) {
15352255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15353255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15354255736Sdavidch                          "'fp %d rx tpa mbuf %d' (%d)\n",
15355255736Sdavidch                      i, j, rc);
15356255736Sdavidch                return (1);
15357255736Sdavidch            }
15358255736Sdavidch        }
15359255736Sdavidch
15360255736Sdavidch        /* create dma map for the spare rx tpa mbuf cluster */
15361255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
15362255736Sdavidch                              BUS_DMA_NOWAIT,
15363255736Sdavidch                              &fp->rx_tpa_info_mbuf_spare_map)) {
15364255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15365255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
15366255736Sdavidch                      "'fp %d spare rx tpa mbuf' (%d)\n",
15367255736Sdavidch                  i, rc);
15368255736Sdavidch            return (1);
15369255736Sdavidch        }
15370255736Sdavidch
15371255736Sdavidch        bxe_init_sge_ring_bit_mask(fp);
15372255736Sdavidch    }
15373255736Sdavidch
15374255736Sdavidch    return (0);
15375255736Sdavidch}
15376255736Sdavidch
15377255736Sdavidchstatic void
15378255736Sdavidchbxe_free_hsi_mem(struct bxe_softc *sc)
15379255736Sdavidch{
15380255736Sdavidch    struct bxe_fastpath *fp;
15381255736Sdavidch    int max_agg_queues;
15382255736Sdavidch    int i, j;
15383255736Sdavidch
15384255736Sdavidch    if (sc->parent_dma_tag == NULL) {
15385255736Sdavidch        return; /* assume nothing was allocated */
15386255736Sdavidch    }
15387255736Sdavidch
15388255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
15389255736Sdavidch        fp = &sc->fp[i];
15390255736Sdavidch
15391255736Sdavidch        /*******************/
15392255736Sdavidch        /* FP STATUS BLOCK */
15393255736Sdavidch        /*******************/
15394255736Sdavidch
15395255736Sdavidch        bxe_dma_free(sc, &fp->sb_dma);
15396255736Sdavidch        memset(&fp->status_block, 0, sizeof(fp->status_block));
15397255736Sdavidch
15398255736Sdavidch        /******************/
15399255736Sdavidch        /* FP TX BD CHAIN */
15400255736Sdavidch        /******************/
15401255736Sdavidch
15402255736Sdavidch        bxe_dma_free(sc, &fp->tx_dma);
15403255736Sdavidch        fp->tx_chain = NULL;
15404255736Sdavidch
15405255736Sdavidch        /******************/
15406255736Sdavidch        /* FP RX BD CHAIN */
15407255736Sdavidch        /******************/
15408255736Sdavidch
15409255736Sdavidch        bxe_dma_free(sc, &fp->rx_dma);
15410255736Sdavidch        fp->rx_chain = NULL;
15411255736Sdavidch
15412255736Sdavidch        /*******************/
15413255736Sdavidch        /* FP RX RCQ CHAIN */
15414255736Sdavidch        /*******************/
15415255736Sdavidch
15416255736Sdavidch        bxe_dma_free(sc, &fp->rcq_dma);
15417255736Sdavidch        fp->rcq_chain = NULL;
15418255736Sdavidch
15419255736Sdavidch        /*******************/
15420255736Sdavidch        /* FP RX SGE CHAIN */
15421255736Sdavidch        /*******************/
15422255736Sdavidch
15423255736Sdavidch        bxe_dma_free(sc, &fp->rx_sge_dma);
15424255736Sdavidch        fp->rx_sge_chain = NULL;
15425255736Sdavidch
15426255736Sdavidch        /***********************/
15427255736Sdavidch        /* FP TX MBUF DMA MAPS */
15428255736Sdavidch        /***********************/
15429255736Sdavidch
15430255736Sdavidch        if (fp->tx_mbuf_tag != NULL) {
15431255736Sdavidch            for (j = 0; j < TX_BD_TOTAL; j++) {
15432255736Sdavidch                if (fp->tx_mbuf_chain[j].m_map != NULL) {
15433255736Sdavidch                    bus_dmamap_unload(fp->tx_mbuf_tag,
15434255736Sdavidch                                      fp->tx_mbuf_chain[j].m_map);
15435255736Sdavidch                    bus_dmamap_destroy(fp->tx_mbuf_tag,
15436255736Sdavidch                                       fp->tx_mbuf_chain[j].m_map);
15437255736Sdavidch                }
15438255736Sdavidch            }
15439255736Sdavidch
15440255736Sdavidch            bus_dma_tag_destroy(fp->tx_mbuf_tag);
15441255736Sdavidch            fp->tx_mbuf_tag = NULL;
15442255736Sdavidch        }
15443255736Sdavidch
15444255736Sdavidch        /***********************/
15445255736Sdavidch        /* FP RX MBUF DMA MAPS */
15446255736Sdavidch        /***********************/
15447255736Sdavidch
15448255736Sdavidch        if (fp->rx_mbuf_tag != NULL) {
15449255736Sdavidch            for (j = 0; j < RX_BD_TOTAL; j++) {
15450255736Sdavidch                if (fp->rx_mbuf_chain[j].m_map != NULL) {
15451255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
15452255736Sdavidch                                      fp->rx_mbuf_chain[j].m_map);
15453255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
15454255736Sdavidch                                       fp->rx_mbuf_chain[j].m_map);
15455255736Sdavidch                }
15456255736Sdavidch            }
15457255736Sdavidch
15458255736Sdavidch            if (fp->rx_mbuf_spare_map != NULL) {
15459255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15460255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15461255736Sdavidch            }
15462255736Sdavidch
15463255736Sdavidch            /***************************/
15464255736Sdavidch            /* FP RX TPA MBUF DMA MAPS */
15465255736Sdavidch            /***************************/
15466255736Sdavidch
15467255736Sdavidch            max_agg_queues = MAX_AGG_QS(sc);
15468255736Sdavidch
15469255736Sdavidch            for (j = 0; j < max_agg_queues; j++) {
15470255736Sdavidch                if (fp->rx_tpa_info[j].bd.m_map != NULL) {
15471255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
15472255736Sdavidch                                      fp->rx_tpa_info[j].bd.m_map);
15473255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
15474255736Sdavidch                                       fp->rx_tpa_info[j].bd.m_map);
15475255736Sdavidch                }
15476255736Sdavidch            }
15477255736Sdavidch
15478255736Sdavidch            if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
15479255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag,
15480255736Sdavidch                                  fp->rx_tpa_info_mbuf_spare_map);
15481255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag,
15482255736Sdavidch                                   fp->rx_tpa_info_mbuf_spare_map);
15483255736Sdavidch            }
15484255736Sdavidch
15485255736Sdavidch            bus_dma_tag_destroy(fp->rx_mbuf_tag);
15486255736Sdavidch            fp->rx_mbuf_tag = NULL;
15487255736Sdavidch        }
15488255736Sdavidch
15489255736Sdavidch        /***************************/
15490255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
15491255736Sdavidch        /***************************/
15492255736Sdavidch
15493255736Sdavidch        if (fp->rx_sge_mbuf_tag != NULL) {
15494255736Sdavidch            for (j = 0; j < RX_SGE_TOTAL; j++) {
15495255736Sdavidch                if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
15496255736Sdavidch                    bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15497255736Sdavidch                                      fp->rx_sge_mbuf_chain[j].m_map);
15498255736Sdavidch                    bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15499255736Sdavidch                                       fp->rx_sge_mbuf_chain[j].m_map);
15500255736Sdavidch                }
15501255736Sdavidch            }
15502255736Sdavidch
15503255736Sdavidch            if (fp->rx_sge_mbuf_spare_map != NULL) {
15504255736Sdavidch                bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15505255736Sdavidch                                  fp->rx_sge_mbuf_spare_map);
15506255736Sdavidch                bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15507255736Sdavidch                                   fp->rx_sge_mbuf_spare_map);
15508255736Sdavidch            }
15509255736Sdavidch
15510255736Sdavidch            bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
15511255736Sdavidch            fp->rx_sge_mbuf_tag = NULL;
15512255736Sdavidch        }
15513255736Sdavidch    }
15514255736Sdavidch
15515255736Sdavidch    /***************************/
15516255736Sdavidch    /* FW DECOMPRESSION BUFFER */
15517255736Sdavidch    /***************************/
15518255736Sdavidch
15519255736Sdavidch    bxe_dma_free(sc, &sc->gz_buf_dma);
15520255736Sdavidch    sc->gz_buf = NULL;
15521255736Sdavidch    free(sc->gz_strm, M_DEVBUF);
15522255736Sdavidch    sc->gz_strm = NULL;
15523255736Sdavidch
15524255736Sdavidch    /*******************/
15525255736Sdavidch    /* SLOW PATH QUEUE */
15526255736Sdavidch    /*******************/
15527255736Sdavidch
15528255736Sdavidch    bxe_dma_free(sc, &sc->spq_dma);
15529255736Sdavidch    sc->spq = NULL;
15530255736Sdavidch
15531255736Sdavidch    /*************/
15532255736Sdavidch    /* SLOW PATH */
15533255736Sdavidch    /*************/
15534255736Sdavidch
15535255736Sdavidch    bxe_dma_free(sc, &sc->sp_dma);
15536255736Sdavidch    sc->sp = NULL;
15537255736Sdavidch
15538255736Sdavidch    /***************/
15539255736Sdavidch    /* EVENT QUEUE */
15540255736Sdavidch    /***************/
15541255736Sdavidch
15542255736Sdavidch    bxe_dma_free(sc, &sc->eq_dma);
15543255736Sdavidch    sc->eq = NULL;
15544255736Sdavidch
15545255736Sdavidch    /************************/
15546255736Sdavidch    /* DEFAULT STATUS BLOCK */
15547255736Sdavidch    /************************/
15548255736Sdavidch
15549255736Sdavidch    bxe_dma_free(sc, &sc->def_sb_dma);
15550255736Sdavidch    sc->def_sb = NULL;
15551255736Sdavidch
15552255736Sdavidch    bus_dma_tag_destroy(sc->parent_dma_tag);
15553255736Sdavidch    sc->parent_dma_tag = NULL;
15554255736Sdavidch}
15555255736Sdavidch
15556255736Sdavidch/*
15557255736Sdavidch * Previous driver DMAE transaction may have occurred when pre-boot stage
15558255736Sdavidch * ended and boot began. This would invalidate the addresses of the
15559255736Sdavidch * transaction, resulting in was-error bit set in the PCI causing all
15560255736Sdavidch * hw-to-host PCIe transactions to timeout. If this happened we want to clear
15561255736Sdavidch * the interrupt which detected this from the pglueb and the was-done bit
15562255736Sdavidch */
15563255736Sdavidchstatic void
15564255736Sdavidchbxe_prev_interrupted_dmae(struct bxe_softc *sc)
15565255736Sdavidch{
15566255736Sdavidch    uint32_t val;
15567255736Sdavidch
15568255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
15569255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
15570255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
15571255736Sdavidch            BLOGD(sc, DBG_LOAD,
15572255736Sdavidch                  "Clearing 'was-error' bit that was set in pglueb");
15573255736Sdavidch            REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
15574255736Sdavidch        }
15575255736Sdavidch    }
15576255736Sdavidch}
15577255736Sdavidch
15578255736Sdavidchstatic int
15579255736Sdavidchbxe_prev_mcp_done(struct bxe_softc *sc)
15580255736Sdavidch{
15581255736Sdavidch    uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15582255736Sdavidch                                 DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15583255736Sdavidch    if (!rc) {
15584255736Sdavidch        BLOGE(sc, "MCP response failure, aborting\n");
15585255736Sdavidch        return (-1);
15586255736Sdavidch    }
15587255736Sdavidch
15588255736Sdavidch    return (0);
15589255736Sdavidch}
15590255736Sdavidch
15591255736Sdavidchstatic struct bxe_prev_list_node *
15592255736Sdavidchbxe_prev_path_get_entry(struct bxe_softc *sc)
15593255736Sdavidch{
15594255736Sdavidch    struct bxe_prev_list_node *tmp;
15595255736Sdavidch
15596255736Sdavidch    LIST_FOREACH(tmp, &bxe_prev_list, node) {
15597255736Sdavidch        if ((sc->pcie_bus == tmp->bus) &&
15598255736Sdavidch            (sc->pcie_device == tmp->slot) &&
15599255736Sdavidch            (SC_PATH(sc) == tmp->path)) {
15600255736Sdavidch            return (tmp);
15601255736Sdavidch        }
15602255736Sdavidch    }
15603255736Sdavidch
15604255736Sdavidch    return (NULL);
15605255736Sdavidch}
15606255736Sdavidch
15607255736Sdavidchstatic uint8_t
15608255736Sdavidchbxe_prev_is_path_marked(struct bxe_softc *sc)
15609255736Sdavidch{
15610255736Sdavidch    struct bxe_prev_list_node *tmp;
15611255736Sdavidch    int rc = FALSE;
15612255736Sdavidch
15613255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15614255736Sdavidch
15615255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15616255736Sdavidch    if (tmp) {
15617255736Sdavidch        if (tmp->aer) {
15618255736Sdavidch            BLOGD(sc, DBG_LOAD,
15619255736Sdavidch                  "Path %d/%d/%d was marked by AER\n",
15620255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15621255736Sdavidch        } else {
15622255736Sdavidch            rc = TRUE;
15623255736Sdavidch            BLOGD(sc, DBG_LOAD,
15624255736Sdavidch                  "Path %d/%d/%d was already cleaned from previous drivers\n",
15625255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15626255736Sdavidch        }
15627255736Sdavidch    }
15628255736Sdavidch
15629255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15630255736Sdavidch
15631255736Sdavidch    return (rc);
15632255736Sdavidch}
15633255736Sdavidch
15634255736Sdavidchstatic int
15635255736Sdavidchbxe_prev_mark_path(struct bxe_softc *sc,
15636255736Sdavidch                   uint8_t          after_undi)
15637255736Sdavidch{
15638255736Sdavidch    struct bxe_prev_list_node *tmp;
15639255736Sdavidch
15640255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15641255736Sdavidch
15642255736Sdavidch    /* Check whether the entry for this path already exists */
15643255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15644255736Sdavidch    if (tmp) {
15645255736Sdavidch        if (!tmp->aer) {
15646255736Sdavidch            BLOGD(sc, DBG_LOAD,
15647255736Sdavidch                  "Re-marking AER in path %d/%d/%d\n",
15648255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15649255736Sdavidch        } else {
15650255736Sdavidch            BLOGD(sc, DBG_LOAD,
15651255736Sdavidch                  "Removing AER indication from path %d/%d/%d\n",
15652255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15653255736Sdavidch            tmp->aer = 0;
15654255736Sdavidch        }
15655255736Sdavidch
15656255736Sdavidch        mtx_unlock(&bxe_prev_mtx);
15657255736Sdavidch        return (0);
15658255736Sdavidch    }
15659255736Sdavidch
15660255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15661255736Sdavidch
15662255736Sdavidch    /* Create an entry for this path and add it */
15663255736Sdavidch    tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15664255736Sdavidch                 (M_NOWAIT | M_ZERO));
15665255736Sdavidch    if (!tmp) {
15666255736Sdavidch        BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15667255736Sdavidch        return (-1);
15668255736Sdavidch    }
15669255736Sdavidch
15670255736Sdavidch    tmp->bus  = sc->pcie_bus;
15671255736Sdavidch    tmp->slot = sc->pcie_device;
15672255736Sdavidch    tmp->path = SC_PATH(sc);
15673255736Sdavidch    tmp->aer  = 0;
15674255736Sdavidch    tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15675255736Sdavidch
15676255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15677255736Sdavidch
15678255736Sdavidch    BLOGD(sc, DBG_LOAD,
15679255736Sdavidch          "Marked path %d/%d/%d - finished previous unload\n",
15680255736Sdavidch          sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15681255736Sdavidch    LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15682255736Sdavidch
15683255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15684255736Sdavidch
15685255736Sdavidch    return (0);
15686255736Sdavidch}
15687255736Sdavidch
15688255736Sdavidchstatic int
15689255736Sdavidchbxe_do_flr(struct bxe_softc *sc)
15690255736Sdavidch{
15691255736Sdavidch    int i;
15692255736Sdavidch
15693255736Sdavidch    /* only E2 and onwards support FLR */
15694255736Sdavidch    if (CHIP_IS_E1x(sc)) {
15695255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15696255736Sdavidch        return (-1);
15697255736Sdavidch    }
15698255736Sdavidch
15699255736Sdavidch    /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15700255736Sdavidch    if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15701255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15702255736Sdavidch              sc->devinfo.bc_ver);
15703255736Sdavidch        return (-1);
15704255736Sdavidch    }
15705255736Sdavidch
15706255736Sdavidch    /* Wait for Transaction Pending bit clean */
15707255736Sdavidch    for (i = 0; i < 4; i++) {
15708255736Sdavidch        if (i) {
15709255736Sdavidch            DELAY(((1 << (i - 1)) * 100) * 1000);
15710255736Sdavidch        }
15711255736Sdavidch
15712255736Sdavidch        if (!bxe_is_pcie_pending(sc)) {
15713255736Sdavidch            goto clear;
15714255736Sdavidch        }
15715255736Sdavidch    }
15716255736Sdavidch
15717255736Sdavidch    BLOGE(sc, "PCIE transaction is not cleared, "
15718255736Sdavidch              "proceeding with reset anyway\n");
15719255736Sdavidch
15720255736Sdavidchclear:
15721255736Sdavidch
15722255736Sdavidch    BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15723255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15724255736Sdavidch
15725255736Sdavidch    return (0);
15726255736Sdavidch}
15727255736Sdavidch
15728255736Sdavidchstruct bxe_mac_vals {
15729255736Sdavidch    uint32_t xmac_addr;
15730255736Sdavidch    uint32_t xmac_val;
15731255736Sdavidch    uint32_t emac_addr;
15732255736Sdavidch    uint32_t emac_val;
15733255736Sdavidch    uint32_t umac_addr;
15734255736Sdavidch    uint32_t umac_val;
15735255736Sdavidch    uint32_t bmac_addr;
15736255736Sdavidch    uint32_t bmac_val[2];
15737255736Sdavidch};
15738255736Sdavidch
15739255736Sdavidchstatic void
15740255736Sdavidchbxe_prev_unload_close_mac(struct bxe_softc *sc,
15741255736Sdavidch                          struct bxe_mac_vals *vals)
15742255736Sdavidch{
15743255736Sdavidch    uint32_t val, base_addr, offset, mask, reset_reg;
15744255736Sdavidch    uint8_t mac_stopped = FALSE;
15745255736Sdavidch    uint8_t port = SC_PORT(sc);
15746255736Sdavidch    uint32_t wb_data[2];
15747255736Sdavidch
15748255736Sdavidch    /* reset addresses as they also mark which values were changed */
15749255736Sdavidch    vals->bmac_addr = 0;
15750255736Sdavidch    vals->umac_addr = 0;
15751255736Sdavidch    vals->xmac_addr = 0;
15752255736Sdavidch    vals->emac_addr = 0;
15753255736Sdavidch
15754255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15755255736Sdavidch
15756255736Sdavidch    if (!CHIP_IS_E3(sc)) {
15757255736Sdavidch        val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15758255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15759255736Sdavidch        if ((mask & reset_reg) && val) {
15760255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15761255736Sdavidch            base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15762255736Sdavidch                                    : NIG_REG_INGRESS_BMAC0_MEM;
15763255736Sdavidch            offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15764255736Sdavidch                                    : BIGMAC_REGISTER_BMAC_CONTROL;
15765255736Sdavidch
15766255736Sdavidch            /*
15767255736Sdavidch             * use rd/wr since we cannot use dmae. This is safe
15768255736Sdavidch             * since MCP won't access the bus due to the request
15769255736Sdavidch             * to unload, and no function on the path can be
15770255736Sdavidch             * loaded at this time.
15771255736Sdavidch             */
15772255736Sdavidch            wb_data[0] = REG_RD(sc, base_addr + offset);
15773255736Sdavidch            wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15774255736Sdavidch            vals->bmac_addr = base_addr + offset;
15775255736Sdavidch            vals->bmac_val[0] = wb_data[0];
15776255736Sdavidch            vals->bmac_val[1] = wb_data[1];
15777255736Sdavidch            wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15778255736Sdavidch            REG_WR(sc, vals->bmac_addr, wb_data[0]);
15779255736Sdavidch            REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15780255736Sdavidch        }
15781255736Sdavidch
15782255736Sdavidch        BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15783255736Sdavidch        vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15784255736Sdavidch        vals->emac_val = REG_RD(sc, vals->emac_addr);
15785255736Sdavidch        REG_WR(sc, vals->emac_addr, 0);
15786255736Sdavidch        mac_stopped = TRUE;
15787255736Sdavidch    } else {
15788255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15789255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15790255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15791255736Sdavidch            val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15792255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15793255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15794255736Sdavidch            vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15795255736Sdavidch            vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15796255736Sdavidch            REG_WR(sc, vals->xmac_addr, 0);
15797255736Sdavidch            mac_stopped = TRUE;
15798255736Sdavidch        }
15799255736Sdavidch
15800255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15801255736Sdavidch        if (mask & reset_reg) {
15802255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15803255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15804255736Sdavidch            vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15805255736Sdavidch            vals->umac_val = REG_RD(sc, vals->umac_addr);
15806255736Sdavidch            REG_WR(sc, vals->umac_addr, 0);
15807255736Sdavidch            mac_stopped = TRUE;
15808255736Sdavidch        }
15809255736Sdavidch    }
15810255736Sdavidch
15811255736Sdavidch    if (mac_stopped) {
15812255736Sdavidch        DELAY(20000);
15813255736Sdavidch    }
15814255736Sdavidch}
15815255736Sdavidch
15816255736Sdavidch#define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15817255736Sdavidch#define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15818255736Sdavidch#define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15819255736Sdavidch#define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15820255736Sdavidch
15821255736Sdavidchstatic void
15822255736Sdavidchbxe_prev_unload_undi_inc(struct bxe_softc *sc,
15823255736Sdavidch                         uint8_t          port,
15824255736Sdavidch                         uint8_t          inc)
15825255736Sdavidch{
15826255736Sdavidch    uint16_t rcq, bd;
15827255736Sdavidch    uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15828255736Sdavidch
15829255736Sdavidch    rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15830255736Sdavidch    bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15831255736Sdavidch
15832255736Sdavidch    tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15833255736Sdavidch    REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15834255736Sdavidch
15835255736Sdavidch    BLOGD(sc, DBG_LOAD,
15836255736Sdavidch          "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15837255736Sdavidch          port, bd, rcq);
15838255736Sdavidch}
15839255736Sdavidch
15840255736Sdavidchstatic int
15841255736Sdavidchbxe_prev_unload_common(struct bxe_softc *sc)
15842255736Sdavidch{
15843255736Sdavidch    uint32_t reset_reg, tmp_reg = 0, rc;
15844255736Sdavidch    uint8_t prev_undi = FALSE;
15845255736Sdavidch    struct bxe_mac_vals mac_vals;
15846255736Sdavidch    uint32_t timer_count = 1000;
15847255736Sdavidch    uint32_t prev_brb;
15848255736Sdavidch
15849255736Sdavidch    /*
15850255736Sdavidch     * It is possible a previous function received 'common' answer,
15851255736Sdavidch     * but hasn't loaded yet, therefore creating a scenario of
15852255736Sdavidch     * multiple functions receiving 'common' on the same path.
15853255736Sdavidch     */
15854255736Sdavidch    BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15855255736Sdavidch
15856255736Sdavidch    memset(&mac_vals, 0, sizeof(mac_vals));
15857255736Sdavidch
15858255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15859255736Sdavidch        return (bxe_prev_mcp_done(sc));
15860255736Sdavidch    }
15861255736Sdavidch
15862255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15863255736Sdavidch
15864255736Sdavidch    /* Reset should be performed after BRB is emptied */
15865255736Sdavidch    if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15866255736Sdavidch        /* Close the MAC Rx to prevent BRB from filling up */
15867255736Sdavidch        bxe_prev_unload_close_mac(sc, &mac_vals);
15868255736Sdavidch
15869255736Sdavidch        /* close LLH filters towards the BRB */
15870255736Sdavidch        elink_set_rx_filter(&sc->link_params, 0);
15871255736Sdavidch
15872255736Sdavidch        /*
15873255736Sdavidch         * Check if the UNDI driver was previously loaded.
15874255736Sdavidch         * UNDI driver initializes CID offset for normal bell to 0x7
15875255736Sdavidch         */
15876255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15877255736Sdavidch            tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15878255736Sdavidch            if (tmp_reg == 0x7) {
15879255736Sdavidch                BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15880255736Sdavidch                prev_undi = TRUE;
15881255736Sdavidch                /* clear the UNDI indication */
15882255736Sdavidch                REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15883255736Sdavidch                /* clear possible idle check errors */
15884255736Sdavidch                REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15885255736Sdavidch            }
15886255736Sdavidch        }
15887255736Sdavidch
15888255736Sdavidch        /* wait until BRB is empty */
15889255736Sdavidch        tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15890255736Sdavidch        while (timer_count) {
15891255736Sdavidch            prev_brb = tmp_reg;
15892255736Sdavidch
15893255736Sdavidch            tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15894255736Sdavidch            if (!tmp_reg) {
15895255736Sdavidch                break;
15896255736Sdavidch            }
15897255736Sdavidch
15898255736Sdavidch            BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15899255736Sdavidch
15900255736Sdavidch            /* reset timer as long as BRB actually gets emptied */
15901255736Sdavidch            if (prev_brb > tmp_reg) {
15902255736Sdavidch                timer_count = 1000;
15903255736Sdavidch            } else {
15904255736Sdavidch                timer_count--;
15905255736Sdavidch            }
15906255736Sdavidch
15907255736Sdavidch            /* If UNDI resides in memory, manually increment it */
15908255736Sdavidch            if (prev_undi) {
15909255736Sdavidch                bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15910255736Sdavidch            }
15911255736Sdavidch
15912255736Sdavidch            DELAY(10);
15913255736Sdavidch        }
15914255736Sdavidch
15915255736Sdavidch        if (!timer_count) {
15916255736Sdavidch            BLOGE(sc, "Failed to empty BRB\n");
15917255736Sdavidch        }
15918255736Sdavidch    }
15919255736Sdavidch
15920255736Sdavidch    /* No packets are in the pipeline, path is ready for reset */
15921255736Sdavidch    bxe_reset_common(sc);
15922255736Sdavidch
15923255736Sdavidch    if (mac_vals.xmac_addr) {
15924255736Sdavidch        REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15925255736Sdavidch    }
15926255736Sdavidch    if (mac_vals.umac_addr) {
15927255736Sdavidch        REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15928255736Sdavidch    }
15929255736Sdavidch    if (mac_vals.emac_addr) {
15930255736Sdavidch        REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15931255736Sdavidch    }
15932255736Sdavidch    if (mac_vals.bmac_addr) {
15933255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15934255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15935255736Sdavidch    }
15936255736Sdavidch
15937255736Sdavidch    rc = bxe_prev_mark_path(sc, prev_undi);
15938255736Sdavidch    if (rc) {
15939255736Sdavidch        bxe_prev_mcp_done(sc);
15940255736Sdavidch        return (rc);
15941255736Sdavidch    }
15942255736Sdavidch
15943255736Sdavidch    return (bxe_prev_mcp_done(sc));
15944255736Sdavidch}
15945255736Sdavidch
15946255736Sdavidchstatic int
15947255736Sdavidchbxe_prev_unload_uncommon(struct bxe_softc *sc)
15948255736Sdavidch{
15949255736Sdavidch    int rc;
15950255736Sdavidch
15951255736Sdavidch    BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15952255736Sdavidch
15953255736Sdavidch    /* Test if previous unload process was already finished for this path */
15954255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15955255736Sdavidch        return (bxe_prev_mcp_done(sc));
15956255736Sdavidch    }
15957255736Sdavidch
15958255736Sdavidch    BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15959255736Sdavidch
15960255736Sdavidch    /*
15961255736Sdavidch     * If function has FLR capabilities, and existing FW version matches
15962255736Sdavidch     * the one required, then FLR will be sufficient to clean any residue
15963255736Sdavidch     * left by previous driver
15964255736Sdavidch     */
15965255736Sdavidch    rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15966255736Sdavidch    if (!rc) {
15967255736Sdavidch        /* fw version is good */
15968255736Sdavidch        BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15969255736Sdavidch        rc = bxe_do_flr(sc);
15970255736Sdavidch    }
15971255736Sdavidch
15972255736Sdavidch    if (!rc) {
15973255736Sdavidch        /* FLR was performed */
15974255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR successful\n");
15975255736Sdavidch        return (0);
15976255736Sdavidch    }
15977255736Sdavidch
15978255736Sdavidch    BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15979255736Sdavidch
15980255736Sdavidch    /* Close the MCP request, return failure*/
15981255736Sdavidch    rc = bxe_prev_mcp_done(sc);
15982255736Sdavidch    if (!rc) {
15983255736Sdavidch        rc = BXE_PREV_WAIT_NEEDED;
15984255736Sdavidch    }
15985255736Sdavidch
15986255736Sdavidch    return (rc);
15987255736Sdavidch}
15988255736Sdavidch
15989255736Sdavidchstatic int
15990255736Sdavidchbxe_prev_unload(struct bxe_softc *sc)
15991255736Sdavidch{
15992255736Sdavidch    int time_counter = 10;
15993255736Sdavidch    uint32_t fw, hw_lock_reg, hw_lock_val;
15994255736Sdavidch    uint32_t rc = 0;
15995255736Sdavidch
15996255736Sdavidch    /*
15997255736Sdavidch     * Clear HW from errors which may have resulted from an interrupted
15998255736Sdavidch     * DMAE transaction.
15999255736Sdavidch     */
16000255736Sdavidch    bxe_prev_interrupted_dmae(sc);
16001255736Sdavidch
16002255736Sdavidch    /* Release previously held locks */
16003255736Sdavidch    hw_lock_reg =
16004255736Sdavidch        (SC_FUNC(sc) <= 5) ?
16005255736Sdavidch            (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
16006255736Sdavidch            (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
16007255736Sdavidch
16008255736Sdavidch    hw_lock_val = (REG_RD(sc, hw_lock_reg));
16009255736Sdavidch    if (hw_lock_val) {
16010255736Sdavidch        if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
16011255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
16012255736Sdavidch            REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
16013255736Sdavidch                   (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
16014255736Sdavidch        }
16015255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
16016255736Sdavidch        REG_WR(sc, hw_lock_reg, 0xffffffff);
16017255736Sdavidch    } else {
16018255736Sdavidch        BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
16019255736Sdavidch    }
16020255736Sdavidch
16021255736Sdavidch    if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
16022255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
16023255736Sdavidch        REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
16024255736Sdavidch    }
16025255736Sdavidch
16026255736Sdavidch    do {
16027255736Sdavidch        /* Lock MCP using an unload request */
16028255736Sdavidch        fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
16029255736Sdavidch        if (!fw) {
16030255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
16031255736Sdavidch            rc = -1;
16032255736Sdavidch            break;
16033255736Sdavidch        }
16034255736Sdavidch
16035255736Sdavidch        if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
16036255736Sdavidch            rc = bxe_prev_unload_common(sc);
16037255736Sdavidch            break;
16038255736Sdavidch        }
16039255736Sdavidch
16040255736Sdavidch        /* non-common reply from MCP night require looping */
16041255736Sdavidch        rc = bxe_prev_unload_uncommon(sc);
16042255736Sdavidch        if (rc != BXE_PREV_WAIT_NEEDED) {
16043255736Sdavidch            break;
16044255736Sdavidch        }
16045255736Sdavidch
16046255736Sdavidch        DELAY(20000);
16047255736Sdavidch    } while (--time_counter);
16048255736Sdavidch
16049255736Sdavidch    if (!time_counter || rc) {
16050255736Sdavidch        BLOGE(sc, "Failed to unload previous driver!\n");
16051255736Sdavidch        rc = -1;
16052255736Sdavidch    }
16053255736Sdavidch
16054255736Sdavidch    return (rc);
16055255736Sdavidch}
16056255736Sdavidch
16057255736Sdavidchvoid
16058255736Sdavidchbxe_dcbx_set_state(struct bxe_softc *sc,
16059255736Sdavidch                   uint8_t          dcb_on,
16060255736Sdavidch                   uint32_t         dcbx_enabled)
16061255736Sdavidch{
16062255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16063255736Sdavidch        sc->dcb_state = dcb_on;
16064255736Sdavidch        sc->dcbx_enabled = dcbx_enabled;
16065255736Sdavidch    } else {
16066255736Sdavidch        sc->dcb_state = FALSE;
16067255736Sdavidch        sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
16068255736Sdavidch    }
16069255736Sdavidch    BLOGD(sc, DBG_LOAD,
16070255736Sdavidch          "DCB state [%s:%s]\n",
16071255736Sdavidch          dcb_on ? "ON" : "OFF",
16072255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
16073255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
16074255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
16075255736Sdavidch          "on-chip with negotiation" : "invalid");
16076255736Sdavidch}
16077255736Sdavidch
16078255736Sdavidch/* must be called after sriov-enable */
16079255736Sdavidchstatic int
16080255736Sdavidchbxe_set_qm_cid_count(struct bxe_softc *sc)
16081255736Sdavidch{
16082255736Sdavidch    int cid_count = BXE_L2_MAX_CID(sc);
16083255736Sdavidch
16084255736Sdavidch    if (IS_SRIOV(sc)) {
16085255736Sdavidch        cid_count += BXE_VF_CIDS;
16086255736Sdavidch    }
16087255736Sdavidch
16088255736Sdavidch    if (CNIC_SUPPORT(sc)) {
16089255736Sdavidch        cid_count += CNIC_CID_MAX;
16090255736Sdavidch    }
16091255736Sdavidch
16092255736Sdavidch    return (roundup(cid_count, QM_CID_ROUND));
16093255736Sdavidch}
16094255736Sdavidch
16095255736Sdavidchstatic void
16096255736Sdavidchbxe_init_multi_cos(struct bxe_softc *sc)
16097255736Sdavidch{
16098255736Sdavidch    int pri, cos;
16099255736Sdavidch
16100255736Sdavidch    uint32_t pri_map = 0; /* XXX change to user config */
16101255736Sdavidch
16102255736Sdavidch    for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
16103255736Sdavidch        cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
16104255736Sdavidch        if (cos < sc->max_cos) {
16105255736Sdavidch            sc->prio_to_cos[pri] = cos;
16106255736Sdavidch        } else {
16107255736Sdavidch            BLOGW(sc, "Invalid COS %d for priority %d "
16108255736Sdavidch                      "(max COS is %d), setting to 0\n",
16109255736Sdavidch                  cos, pri, (sc->max_cos - 1));
16110255736Sdavidch            sc->prio_to_cos[pri] = 0;
16111255736Sdavidch        }
16112255736Sdavidch    }
16113255736Sdavidch}
16114255736Sdavidch
16115255736Sdavidchstatic int
16116255736Sdavidchbxe_sysctl_state(SYSCTL_HANDLER_ARGS)
16117255736Sdavidch{
16118255736Sdavidch    struct bxe_softc *sc;
16119255736Sdavidch    int error, result;
16120255736Sdavidch
16121255736Sdavidch    result = 0;
16122255736Sdavidch    error = sysctl_handle_int(oidp, &result, 0, req);
16123255736Sdavidch
16124255736Sdavidch    if (error || !req->newptr) {
16125255736Sdavidch        return (error);
16126255736Sdavidch    }
16127255736Sdavidch
16128255736Sdavidch    if (result == 1) {
16129255736Sdavidch        sc = (struct bxe_softc *)arg1;
16130255736Sdavidch        BLOGI(sc, "... dumping driver state ...\n");
16131255736Sdavidch        /* XXX */
16132255736Sdavidch    }
16133255736Sdavidch
16134255736Sdavidch    return (error);
16135255736Sdavidch}
16136255736Sdavidch
16137255736Sdavidchstatic int
16138255736Sdavidchbxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
16139255736Sdavidch{
16140255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
16141255736Sdavidch    uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
16142255736Sdavidch    uint32_t *offset;
16143255736Sdavidch    uint64_t value = 0;
16144255736Sdavidch    int index = (int)arg2;
16145255736Sdavidch
16146255736Sdavidch    if (index >= BXE_NUM_ETH_STATS) {
16147255736Sdavidch        BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
16148255736Sdavidch        return (-1);
16149255736Sdavidch    }
16150255736Sdavidch
16151255736Sdavidch    offset = (eth_stats + bxe_eth_stats_arr[index].offset);
16152255736Sdavidch
16153255736Sdavidch    switch (bxe_eth_stats_arr[index].size) {
16154255736Sdavidch    case 4:
16155255736Sdavidch        value = (uint64_t)*offset;
16156255736Sdavidch        break;
16157255736Sdavidch    case 8:
16158255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
16159255736Sdavidch        break;
16160255736Sdavidch    default:
16161255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
16162255736Sdavidch              index, bxe_eth_stats_arr[index].size);
16163255736Sdavidch        return (-1);
16164255736Sdavidch    }
16165255736Sdavidch
16166255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
16167255736Sdavidch}
16168255736Sdavidch
16169255736Sdavidchstatic int
16170255736Sdavidchbxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
16171255736Sdavidch{
16172255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
16173255736Sdavidch    uint32_t *eth_stats;
16174255736Sdavidch    uint32_t *offset;
16175255736Sdavidch    uint64_t value = 0;
16176255736Sdavidch    uint32_t q_stat = (uint32_t)arg2;
16177255736Sdavidch    uint32_t fp_index = ((q_stat >> 16) & 0xffff);
16178255736Sdavidch    uint32_t index = (q_stat & 0xffff);
16179255736Sdavidch
16180255736Sdavidch    eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
16181255736Sdavidch
16182255736Sdavidch    if (index >= BXE_NUM_ETH_Q_STATS) {
16183255736Sdavidch        BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
16184255736Sdavidch        return (-1);
16185255736Sdavidch    }
16186255736Sdavidch
16187255736Sdavidch    offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
16188255736Sdavidch
16189255736Sdavidch    switch (bxe_eth_q_stats_arr[index].size) {
16190255736Sdavidch    case 4:
16191255736Sdavidch        value = (uint64_t)*offset;
16192255736Sdavidch        break;
16193255736Sdavidch    case 8:
16194255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
16195255736Sdavidch        break;
16196255736Sdavidch    default:
16197255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
16198255736Sdavidch              index, bxe_eth_q_stats_arr[index].size);
16199255736Sdavidch        return (-1);
16200255736Sdavidch    }
16201255736Sdavidch
16202255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
16203255736Sdavidch}
16204255736Sdavidch
16205255736Sdavidchstatic void
16206255736Sdavidchbxe_add_sysctls(struct bxe_softc *sc)
16207255736Sdavidch{
16208255736Sdavidch    struct sysctl_ctx_list *ctx;
16209255736Sdavidch    struct sysctl_oid_list *children;
16210255736Sdavidch    struct sysctl_oid *queue_top, *queue;
16211255736Sdavidch    struct sysctl_oid_list *queue_top_children, *queue_children;
16212255736Sdavidch    char queue_num_buf[32];
16213255736Sdavidch    uint32_t q_stat;
16214255736Sdavidch    int i, j;
16215255736Sdavidch
16216255736Sdavidch    ctx = device_get_sysctl_ctx(sc->dev);
16217255736Sdavidch    children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
16218255736Sdavidch
16219255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
16220255736Sdavidch                      CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
16221255736Sdavidch                      "version");
16222255736Sdavidch
16223255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
16224273377Shselasky                      CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
16225255736Sdavidch                      "bootcode version");
16226255736Sdavidch
16227255736Sdavidch    snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
16228255736Sdavidch             BCM_5710_FW_MAJOR_VERSION,
16229255736Sdavidch             BCM_5710_FW_MINOR_VERSION,
16230255736Sdavidch             BCM_5710_FW_REVISION_VERSION,
16231255736Sdavidch             BCM_5710_FW_ENGINEERING_VERSION);
16232255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
16233273377Shselasky                      CTLFLAG_RD, sc->fw_ver_str, 0,
16234255736Sdavidch                      "firmware version");
16235255736Sdavidch
16236255736Sdavidch    snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
16237255736Sdavidch        ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
16238255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
16239255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
16240255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
16241255736Sdavidch                                                                "Unknown"));
16242255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
16243273377Shselasky                      CTLFLAG_RD, sc->mf_mode_str, 0,
16244255736Sdavidch                      "multifunction mode");
16245255736Sdavidch
16246255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
16247255736Sdavidch                    CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
16248255736Sdavidch                    "multifunction vnics per port");
16249255736Sdavidch
16250255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
16251273377Shselasky                      CTLFLAG_RD, sc->mac_addr_str, 0,
16252255736Sdavidch                      "mac address");
16253255736Sdavidch
16254255736Sdavidch    snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
16255255736Sdavidch        ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
16256255736Sdavidch         (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
16257255736Sdavidch         (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
16258255736Sdavidch                                              "???GT/s"),
16259255736Sdavidch        sc->devinfo.pcie_link_width);
16260255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
16261273377Shselasky                      CTLFLAG_RD, sc->pci_link_str, 0,
16262255736Sdavidch                      "pci link status");
16263255736Sdavidch
16264255736Sdavidch    sc->debug = bxe_debug;
16265273377Shselasky    SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
16266273377Shselasky                    CTLFLAG_RW, &sc->debug,
16267255736Sdavidch                    "debug logging mode");
16268255736Sdavidch
16269255736Sdavidch    sc->rx_budget = bxe_rx_budget;
16270255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
16271255736Sdavidch                    CTLFLAG_RW, &sc->rx_budget, 0,
16272255736Sdavidch                    "rx processing budget");
16273255736Sdavidch
16274255736Sdavidch    SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
16275255736Sdavidch                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
16276255736Sdavidch                    bxe_sysctl_state, "IU", "dump driver state");
16277255736Sdavidch
16278255736Sdavidch    for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
16279255736Sdavidch        SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
16280255736Sdavidch                        bxe_eth_stats_arr[i].string,
16281255736Sdavidch                        CTLTYPE_U64 | CTLFLAG_RD, sc, i,
16282255736Sdavidch                        bxe_sysctl_eth_stat, "LU",
16283255736Sdavidch                        bxe_eth_stats_arr[i].string);
16284255736Sdavidch    }
16285255736Sdavidch
16286255736Sdavidch    /* add a new parent node for all queues "dev.bxe.#.queue" */
16287255736Sdavidch    queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
16288255736Sdavidch                                CTLFLAG_RD, NULL, "queue");
16289255736Sdavidch    queue_top_children = SYSCTL_CHILDREN(queue_top);
16290255736Sdavidch
16291255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
16292255736Sdavidch        /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
16293255736Sdavidch        snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
16294255736Sdavidch        queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
16295255736Sdavidch                                queue_num_buf, CTLFLAG_RD, NULL,
16296255736Sdavidch                                "single queue");
16297255736Sdavidch        queue_children = SYSCTL_CHILDREN(queue);
16298255736Sdavidch
16299255736Sdavidch        for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
16300255736Sdavidch            q_stat = ((i << 16) | j);
16301255736Sdavidch            SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
16302255736Sdavidch                            bxe_eth_q_stats_arr[j].string,
16303255736Sdavidch                            CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
16304255736Sdavidch                            bxe_sysctl_eth_q_stat, "LU",
16305255736Sdavidch                            bxe_eth_q_stats_arr[j].string);
16306255736Sdavidch        }
16307255736Sdavidch    }
16308255736Sdavidch}
16309255736Sdavidch
16310255736Sdavidch/*
16311255736Sdavidch * Device attach function.
16312255736Sdavidch *
16313255736Sdavidch * Allocates device resources, performs secondary chip identification, and
16314255736Sdavidch * initializes driver instance variables. This function is called from driver
16315255736Sdavidch * load after a successful probe.
16316255736Sdavidch *
16317255736Sdavidch * Returns:
16318255736Sdavidch *   0 = Success, >0 = Failure
16319255736Sdavidch */
16320255736Sdavidchstatic int
16321255736Sdavidchbxe_attach(device_t dev)
16322255736Sdavidch{
16323255736Sdavidch    struct bxe_softc *sc;
16324255736Sdavidch
16325255736Sdavidch    sc = device_get_softc(dev);
16326255736Sdavidch
16327255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting attach...\n");
16328255736Sdavidch
16329255736Sdavidch    sc->state = BXE_STATE_CLOSED;
16330255736Sdavidch
16331255736Sdavidch    sc->dev  = dev;
16332255736Sdavidch    sc->unit = device_get_unit(dev);
16333255736Sdavidch
16334255736Sdavidch    BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
16335255736Sdavidch
16336255736Sdavidch    sc->pcie_bus    = pci_get_bus(dev);
16337255736Sdavidch    sc->pcie_device = pci_get_slot(dev);
16338255736Sdavidch    sc->pcie_func   = pci_get_function(dev);
16339255736Sdavidch
16340255736Sdavidch    /* enable bus master capability */
16341255736Sdavidch    pci_enable_busmaster(dev);
16342255736Sdavidch
16343255736Sdavidch    /* get the BARs */
16344255736Sdavidch    if (bxe_allocate_bars(sc) != 0) {
16345255736Sdavidch        return (ENXIO);
16346255736Sdavidch    }
16347255736Sdavidch
16348255736Sdavidch    /* initialize the mutexes */
16349255736Sdavidch    bxe_init_mutexes(sc);
16350255736Sdavidch
16351255736Sdavidch    /* prepare the periodic callout */
16352255736Sdavidch    callout_init(&sc->periodic_callout, 0);
16353255736Sdavidch
16354255736Sdavidch    /* prepare the chip taskqueue */
16355255736Sdavidch    sc->chip_tq_flags = CHIP_TQ_NONE;
16356255736Sdavidch    snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
16357255736Sdavidch             "bxe%d_chip_tq", sc->unit);
16358255736Sdavidch    TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
16359255736Sdavidch    sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
16360255736Sdavidch                                   taskqueue_thread_enqueue,
16361255736Sdavidch                                   &sc->chip_tq);
16362255736Sdavidch    taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
16363255736Sdavidch                            "%s", sc->chip_tq_name);
16364255736Sdavidch
16365255736Sdavidch    /* get device info and set params */
16366255736Sdavidch    if (bxe_get_device_info(sc) != 0) {
16367255736Sdavidch        BLOGE(sc, "getting device info\n");
16368255736Sdavidch        bxe_deallocate_bars(sc);
16369255736Sdavidch        pci_disable_busmaster(dev);
16370255736Sdavidch        return (ENXIO);
16371255736Sdavidch    }
16372255736Sdavidch
16373255736Sdavidch    /* get final misc params */
16374255736Sdavidch    bxe_get_params(sc);
16375255736Sdavidch
16376255736Sdavidch    /* set the default MTU (changed via ifconfig) */
16377255736Sdavidch    sc->mtu = ETHERMTU;
16378255736Sdavidch
16379255736Sdavidch    bxe_set_modes_bitmap(sc);
16380255736Sdavidch
16381255736Sdavidch    /* XXX
16382255736Sdavidch     * If in AFEX mode and the function is configured for FCoE
16383255736Sdavidch     * then bail... no L2 allowed.
16384255736Sdavidch     */
16385255736Sdavidch
16386255736Sdavidch    /* get phy settings from shmem and 'and' against admin settings */
16387255736Sdavidch    bxe_get_phy_info(sc);
16388255736Sdavidch
16389255736Sdavidch    /* initialize the FreeBSD ifnet interface */
16390255736Sdavidch    if (bxe_init_ifnet(sc) != 0) {
16391255736Sdavidch        bxe_release_mutexes(sc);
16392255736Sdavidch        bxe_deallocate_bars(sc);
16393255736Sdavidch        pci_disable_busmaster(dev);
16394255736Sdavidch        return (ENXIO);
16395255736Sdavidch    }
16396255736Sdavidch
16397255736Sdavidch    /* allocate device interrupts */
16398255736Sdavidch    if (bxe_interrupt_alloc(sc) != 0) {
16399266979Smarcel        if (sc->ifp != NULL) {
16400270876Sglebius            ether_ifdetach(sc->ifp);
16401255736Sdavidch        }
16402255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16403255736Sdavidch        bxe_release_mutexes(sc);
16404255736Sdavidch        bxe_deallocate_bars(sc);
16405255736Sdavidch        pci_disable_busmaster(dev);
16406255736Sdavidch        return (ENXIO);
16407255736Sdavidch    }
16408255736Sdavidch
16409255736Sdavidch    /* allocate ilt */
16410255736Sdavidch    if (bxe_alloc_ilt_mem(sc) != 0) {
16411255736Sdavidch        bxe_interrupt_free(sc);
16412266979Smarcel        if (sc->ifp != NULL) {
16413270876Sglebius            ether_ifdetach(sc->ifp);
16414255736Sdavidch        }
16415255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16416255736Sdavidch        bxe_release_mutexes(sc);
16417255736Sdavidch        bxe_deallocate_bars(sc);
16418255736Sdavidch        pci_disable_busmaster(dev);
16419255736Sdavidch        return (ENXIO);
16420255736Sdavidch    }
16421255736Sdavidch
16422255736Sdavidch    /* allocate the host hardware/software hsi structures */
16423255736Sdavidch    if (bxe_alloc_hsi_mem(sc) != 0) {
16424255736Sdavidch        bxe_free_ilt_mem(sc);
16425255736Sdavidch        bxe_interrupt_free(sc);
16426266979Smarcel        if (sc->ifp != NULL) {
16427270876Sglebius            ether_ifdetach(sc->ifp);
16428255736Sdavidch        }
16429255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16430255736Sdavidch        bxe_release_mutexes(sc);
16431255736Sdavidch        bxe_deallocate_bars(sc);
16432255736Sdavidch        pci_disable_busmaster(dev);
16433255736Sdavidch        return (ENXIO);
16434255736Sdavidch    }
16435255736Sdavidch
16436255736Sdavidch    /* need to reset chip if UNDI was active */
16437255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
16438255736Sdavidch        /* init fw_seq */
16439255736Sdavidch        sc->fw_seq =
16440255736Sdavidch            (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
16441255736Sdavidch             DRV_MSG_SEQ_NUMBER_MASK);
16442255736Sdavidch        BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
16443255736Sdavidch        bxe_prev_unload(sc);
16444255736Sdavidch    }
16445255736Sdavidch
16446255736Sdavidch#if 1
16447255736Sdavidch    /* XXX */
16448255736Sdavidch    bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16449255736Sdavidch#else
16450255736Sdavidch    if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
16451255736Sdavidch        SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
16452255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
16453255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
16454255736Sdavidch        bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
16455255736Sdavidch        bxe_dcbx_init_params(sc);
16456255736Sdavidch    } else {
16457255736Sdavidch        bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16458255736Sdavidch    }
16459255736Sdavidch#endif
16460255736Sdavidch
16461255736Sdavidch    /* calculate qm_cid_count */
16462255736Sdavidch    sc->qm_cid_count = bxe_set_qm_cid_count(sc);
16463255736Sdavidch    BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
16464255736Sdavidch
16465255736Sdavidch    sc->max_cos = 1;
16466255736Sdavidch    bxe_init_multi_cos(sc);
16467255736Sdavidch
16468255736Sdavidch    bxe_add_sysctls(sc);
16469255736Sdavidch
16470255736Sdavidch    return (0);
16471255736Sdavidch}
16472255736Sdavidch
16473255736Sdavidch/*
16474255736Sdavidch * Device detach function.
16475255736Sdavidch *
16476255736Sdavidch * Stops the controller, resets the controller, and releases resources.
16477255736Sdavidch *
16478255736Sdavidch * Returns:
16479255736Sdavidch *   0 = Success, >0 = Failure
16480255736Sdavidch */
16481255736Sdavidchstatic int
16482255736Sdavidchbxe_detach(device_t dev)
16483255736Sdavidch{
16484255736Sdavidch    struct bxe_softc *sc;
16485266979Smarcel    if_t ifp;
16486255736Sdavidch
16487255736Sdavidch    sc = device_get_softc(dev);
16488255736Sdavidch
16489255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16490255736Sdavidch
16491266979Smarcel    ifp = sc->ifp;
16492266979Smarcel    if (ifp != NULL && if_vlantrunkinuse(ifp)) {
16493255736Sdavidch        BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16494255736Sdavidch        return(EBUSY);
16495255736Sdavidch    }
16496255736Sdavidch
16497255736Sdavidch    /* stop the periodic callout */
16498255736Sdavidch    bxe_periodic_stop(sc);
16499255736Sdavidch
16500255736Sdavidch    /* stop the chip taskqueue */
16501255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16502255736Sdavidch    if (sc->chip_tq) {
16503255736Sdavidch        taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16504255736Sdavidch        taskqueue_free(sc->chip_tq);
16505255736Sdavidch        sc->chip_tq = NULL;
16506255736Sdavidch    }
16507255736Sdavidch
16508255736Sdavidch    /* stop and reset the controller if it was open */
16509255736Sdavidch    if (sc->state != BXE_STATE_CLOSED) {
16510255736Sdavidch        BXE_CORE_LOCK(sc);
16511255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16512255736Sdavidch        BXE_CORE_UNLOCK(sc);
16513255736Sdavidch    }
16514255736Sdavidch
16515255736Sdavidch    /* release the network interface */
16516255736Sdavidch    if (ifp != NULL) {
16517270876Sglebius        ether_ifdetach(ifp);
16518255736Sdavidch    }
16519255736Sdavidch    ifmedia_removeall(&sc->ifmedia);
16520255736Sdavidch
16521255736Sdavidch    /* XXX do the following based on driver state... */
16522255736Sdavidch
16523255736Sdavidch    /* free the host hardware/software hsi structures */
16524255736Sdavidch    bxe_free_hsi_mem(sc);
16525255736Sdavidch
16526255736Sdavidch    /* free ilt */
16527255736Sdavidch    bxe_free_ilt_mem(sc);
16528255736Sdavidch
16529255736Sdavidch    /* release the interrupts */
16530255736Sdavidch    bxe_interrupt_free(sc);
16531255736Sdavidch
16532255736Sdavidch    /* Release the mutexes*/
16533255736Sdavidch    bxe_release_mutexes(sc);
16534255736Sdavidch
16535255736Sdavidch    /* Release the PCIe BAR mapped memory */
16536255736Sdavidch    bxe_deallocate_bars(sc);
16537255736Sdavidch
16538255736Sdavidch    /* Release the FreeBSD interface. */
16539266979Smarcel    if (sc->ifp != NULL) {
16540270876Sglebius        if_free(sc->ifp);
16541255736Sdavidch    }
16542255736Sdavidch
16543255736Sdavidch    pci_disable_busmaster(dev);
16544255736Sdavidch
16545255736Sdavidch    return (0);
16546255736Sdavidch}
16547255736Sdavidch
16548255736Sdavidch/*
16549255736Sdavidch * Device shutdown function.
16550255736Sdavidch *
16551255736Sdavidch * Stops and resets the controller.
16552255736Sdavidch *
16553255736Sdavidch * Returns:
16554255736Sdavidch *   Nothing
16555255736Sdavidch */
16556255736Sdavidchstatic int
16557255736Sdavidchbxe_shutdown(device_t dev)
16558255736Sdavidch{
16559255736Sdavidch    struct bxe_softc *sc;
16560255736Sdavidch
16561255736Sdavidch    sc = device_get_softc(dev);
16562255736Sdavidch
16563255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16564255736Sdavidch
16565255736Sdavidch    /* stop the periodic callout */
16566255736Sdavidch    bxe_periodic_stop(sc);
16567255736Sdavidch
16568255736Sdavidch    BXE_CORE_LOCK(sc);
16569255736Sdavidch    bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16570255736Sdavidch    BXE_CORE_UNLOCK(sc);
16571255736Sdavidch
16572255736Sdavidch    return (0);
16573255736Sdavidch}
16574255736Sdavidch
16575255736Sdavidchvoid
16576255736Sdavidchbxe_igu_ack_sb(struct bxe_softc *sc,
16577255736Sdavidch               uint8_t          igu_sb_id,
16578255736Sdavidch               uint8_t          segment,
16579255736Sdavidch               uint16_t         index,
16580255736Sdavidch               uint8_t          op,
16581255736Sdavidch               uint8_t          update)
16582255736Sdavidch{
16583255736Sdavidch    uint32_t igu_addr = sc->igu_base_addr;
16584255736Sdavidch    igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16585255736Sdavidch    bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16586255736Sdavidch}
16587255736Sdavidch
16588255736Sdavidchstatic void
16589255736Sdavidchbxe_igu_clear_sb_gen(struct bxe_softc *sc,
16590255736Sdavidch                     uint8_t          func,
16591255736Sdavidch                     uint8_t          idu_sb_id,
16592255736Sdavidch                     uint8_t          is_pf)
16593255736Sdavidch{
16594255736Sdavidch    uint32_t data, ctl, cnt = 100;
16595255736Sdavidch    uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16596255736Sdavidch    uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16597255736Sdavidch    uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16598255736Sdavidch    uint32_t sb_bit =  1 << (idu_sb_id%32);
16599255736Sdavidch    uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16600255736Sdavidch    uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16601255736Sdavidch
16602255736Sdavidch    /* Not supported in BC mode */
16603255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
16604255736Sdavidch        return;
16605255736Sdavidch    }
16606255736Sdavidch
16607255736Sdavidch    data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16608255736Sdavidch             IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16609255736Sdavidch            IGU_REGULAR_CLEANUP_SET |
16610255736Sdavidch            IGU_REGULAR_BCLEANUP);
16611255736Sdavidch
16612255736Sdavidch    ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16613255736Sdavidch           (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16614255736Sdavidch           (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16615255736Sdavidch
16616255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16617255736Sdavidch            data, igu_addr_data);
16618255736Sdavidch    REG_WR(sc, igu_addr_data, data);
16619255736Sdavidch
16620255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16621255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16622255736Sdavidch    mb();
16623255736Sdavidch
16624255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16625255736Sdavidch            ctl, igu_addr_ctl);
16626255736Sdavidch    REG_WR(sc, igu_addr_ctl, ctl);
16627255736Sdavidch
16628255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16629255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16630255736Sdavidch    mb();
16631255736Sdavidch
16632255736Sdavidch    /* wait for clean up to finish */
16633255736Sdavidch    while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16634255736Sdavidch        DELAY(20000);
16635255736Sdavidch    }
16636255736Sdavidch
16637255736Sdavidch    if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16638255736Sdavidch        BLOGD(sc, DBG_LOAD,
16639255736Sdavidch              "Unable to finish IGU cleanup: "
16640255736Sdavidch              "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16641255736Sdavidch              idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16642255736Sdavidch    }
16643255736Sdavidch}
16644255736Sdavidch
16645255736Sdavidchstatic void
16646255736Sdavidchbxe_igu_clear_sb(struct bxe_softc *sc,
16647255736Sdavidch                 uint8_t          idu_sb_id)
16648255736Sdavidch{
16649255736Sdavidch    bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16650255736Sdavidch}
16651255736Sdavidch
16652255736Sdavidch
16653255736Sdavidch
16654255736Sdavidch
16655255736Sdavidch
16656255736Sdavidch
16657255736Sdavidch
16658255736Sdavidch/*******************/
16659255736Sdavidch/* ECORE CALLBACKS */
16660255736Sdavidch/*******************/
16661255736Sdavidch
16662255736Sdavidchstatic void
16663255736Sdavidchbxe_reset_common(struct bxe_softc *sc)
16664255736Sdavidch{
16665255736Sdavidch    uint32_t val = 0x1400;
16666255736Sdavidch
16667255736Sdavidch    /* reset_common */
16668255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16669255736Sdavidch
16670255736Sdavidch    if (CHIP_IS_E3(sc)) {
16671255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16672255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16673255736Sdavidch    }
16674255736Sdavidch
16675255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16676255736Sdavidch}
16677255736Sdavidch
16678255736Sdavidchstatic void
16679255736Sdavidchbxe_common_init_phy(struct bxe_softc *sc)
16680255736Sdavidch{
16681255736Sdavidch    uint32_t shmem_base[2];
16682255736Sdavidch    uint32_t shmem2_base[2];
16683255736Sdavidch
16684255736Sdavidch    /* Avoid common init in case MFW supports LFA */
16685255736Sdavidch    if (SHMEM2_RD(sc, size) >
16686255736Sdavidch        (uint32_t)offsetof(struct shmem2_region,
16687255736Sdavidch                           lfa_host_addr[SC_PORT(sc)])) {
16688255736Sdavidch        return;
16689255736Sdavidch    }
16690255736Sdavidch
16691255736Sdavidch    shmem_base[0]  = sc->devinfo.shmem_base;
16692255736Sdavidch    shmem2_base[0] = sc->devinfo.shmem2_base;
16693255736Sdavidch
16694255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16695255736Sdavidch        shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16696255736Sdavidch        shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16697255736Sdavidch    }
16698255736Sdavidch
16699284335Sdavidcs    bxe_acquire_phy_lock(sc);
16700255736Sdavidch    elink_common_init_phy(sc, shmem_base, shmem2_base,
16701255736Sdavidch                          sc->devinfo.chip_id, 0);
16702284335Sdavidcs    bxe_release_phy_lock(sc);
16703255736Sdavidch}
16704255736Sdavidch
16705255736Sdavidchstatic void
16706255736Sdavidchbxe_pf_disable(struct bxe_softc *sc)
16707255736Sdavidch{
16708255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16709255736Sdavidch
16710255736Sdavidch    val &= ~IGU_PF_CONF_FUNC_EN;
16711255736Sdavidch
16712255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16713255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16714255736Sdavidch    REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16715255736Sdavidch}
16716255736Sdavidch
16717255736Sdavidchstatic void
16718255736Sdavidchbxe_init_pxp(struct bxe_softc *sc)
16719255736Sdavidch{
16720255736Sdavidch    uint16_t devctl;
16721255736Sdavidch    int r_order, w_order;
16722255736Sdavidch
16723255736Sdavidch    devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16724255736Sdavidch
16725255736Sdavidch    BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16726255736Sdavidch
16727255736Sdavidch    w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16728255736Sdavidch
16729255736Sdavidch    if (sc->mrrs == -1) {
16730255736Sdavidch        r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16731255736Sdavidch    } else {
16732255736Sdavidch        BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16733255736Sdavidch        r_order = sc->mrrs;
16734255736Sdavidch    }
16735255736Sdavidch
16736255736Sdavidch    ecore_init_pxp_arb(sc, r_order, w_order);
16737255736Sdavidch}
16738255736Sdavidch
16739255736Sdavidchstatic uint32_t
16740255736Sdavidchbxe_get_pretend_reg(struct bxe_softc *sc)
16741255736Sdavidch{
16742255736Sdavidch    uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16743255736Sdavidch    uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16744255736Sdavidch    return (base + (SC_ABS_FUNC(sc)) * stride);
16745255736Sdavidch}
16746255736Sdavidch
16747255736Sdavidch/*
16748255736Sdavidch * Called only on E1H or E2.
16749255736Sdavidch * When pretending to be PF, the pretend value is the function number 0..7.
16750255736Sdavidch * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16751255736Sdavidch * combination.
16752255736Sdavidch */
16753255736Sdavidchstatic int
16754255736Sdavidchbxe_pretend_func(struct bxe_softc *sc,
16755255736Sdavidch                 uint16_t         pretend_func_val)
16756255736Sdavidch{
16757255736Sdavidch    uint32_t pretend_reg;
16758255736Sdavidch
16759255736Sdavidch    if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16760255736Sdavidch        return (-1);
16761255736Sdavidch    }
16762255736Sdavidch
16763255736Sdavidch    /* get my own pretend register */
16764255736Sdavidch    pretend_reg = bxe_get_pretend_reg(sc);
16765255736Sdavidch    REG_WR(sc, pretend_reg, pretend_func_val);
16766255736Sdavidch    REG_RD(sc, pretend_reg);
16767255736Sdavidch    return (0);
16768255736Sdavidch}
16769255736Sdavidch
16770255736Sdavidchstatic void
16771255736Sdavidchbxe_iov_init_dmae(struct bxe_softc *sc)
16772255736Sdavidch{
16773255736Sdavidch    return;
16774255736Sdavidch#if 0
16775255736Sdavidch    BLOGD(sc, DBG_LOAD, "SRIOV is %s\n", IS_SRIOV(sc) ? "ON" : "OFF");
16776255736Sdavidch
16777255736Sdavidch    if (!IS_SRIOV(sc)) {
16778255736Sdavidch        return;
16779255736Sdavidch    }
16780255736Sdavidch
16781255736Sdavidch    REG_WR(sc, DMAE_REG_BACKWARD_COMP_EN, 0);
16782255736Sdavidch#endif
16783255736Sdavidch}
16784255736Sdavidch
16785255736Sdavidch#if 0
16786255736Sdavidchstatic int
16787255736Sdavidchbxe_iov_init_ilt(struct bxe_softc *sc,
16788255736Sdavidch                 uint16_t         line)
16789255736Sdavidch{
16790255736Sdavidch    return (line);
16791255736Sdavidch#if 0
16792255736Sdavidch    int i;
16793255736Sdavidch    struct ecore_ilt* ilt = sc->ilt;
16794255736Sdavidch
16795255736Sdavidch    if (!IS_SRIOV(sc)) {
16796255736Sdavidch        return (line);
16797255736Sdavidch    }
16798255736Sdavidch
16799255736Sdavidch    /* set vfs ilt lines */
16800255736Sdavidch    for (i = 0; i < BXE_VF_CIDS/ILT_PAGE_CIDS ; i++) {
16801255736Sdavidch        struct hw_dma *hw_cxt = SC_VF_CXT_PAGE(sc,i);
16802255736Sdavidch        ilt->lines[line+i].page = hw_cxt->addr;
16803255736Sdavidch        ilt->lines[line+i].page_mapping = hw_cxt->mapping;
16804255736Sdavidch        ilt->lines[line+i].size = hw_cxt->size; /* doesn't matter */
16805255736Sdavidch    }
16806255736Sdavidch    return (line+i);
16807255736Sdavidch#endif
16808255736Sdavidch}
16809255736Sdavidch#endif
16810255736Sdavidch
16811255736Sdavidchstatic void
16812255736Sdavidchbxe_iov_init_dq(struct bxe_softc *sc)
16813255736Sdavidch{
16814255736Sdavidch    return;
16815255736Sdavidch#if 0
16816255736Sdavidch    if (!IS_SRIOV(sc)) {
16817255736Sdavidch        return;
16818255736Sdavidch    }
16819255736Sdavidch
16820255736Sdavidch    /* Set the DQ such that the CID reflect the abs_vfid */
16821255736Sdavidch    REG_WR(sc, DORQ_REG_VF_NORM_VF_BASE, 0);
16822255736Sdavidch    REG_WR(sc, DORQ_REG_MAX_RVFID_SIZE, ilog2(BNX2X_MAX_NUM_OF_VFS));
16823255736Sdavidch
16824255736Sdavidch    /*
16825255736Sdavidch     * Set VFs starting CID. If its > 0 the preceding CIDs are belong to
16826255736Sdavidch     * the PF L2 queues
16827255736Sdavidch     */
16828255736Sdavidch    REG_WR(sc, DORQ_REG_VF_NORM_CID_BASE, BNX2X_FIRST_VF_CID);
16829255736Sdavidch
16830255736Sdavidch    /* The VF window size is the log2 of the max number of CIDs per VF */
16831255736Sdavidch    REG_WR(sc, DORQ_REG_VF_NORM_CID_WND_SIZE, BNX2X_VF_CID_WND);
16832255736Sdavidch
16833255736Sdavidch    /*
16834255736Sdavidch     * The VF doorbell size  0 - *B, 4 - 128B. We set it here to match
16835255736Sdavidch     * the Pf doorbell size although the 2 are independent.
16836255736Sdavidch     */
16837255736Sdavidch    REG_WR(sc, DORQ_REG_VF_NORM_CID_OFST,
16838255736Sdavidch           BNX2X_DB_SHIFT - BNX2X_DB_MIN_SHIFT);
16839255736Sdavidch
16840255736Sdavidch    /*
16841255736Sdavidch     * No security checks for now -
16842255736Sdavidch     * configure single rule (out of 16) mask = 0x1, value = 0x0,
16843255736Sdavidch     * CID range 0 - 0x1ffff
16844255736Sdavidch     */
16845255736Sdavidch    REG_WR(sc, DORQ_REG_VF_TYPE_MASK_0, 1);
16846255736Sdavidch    REG_WR(sc, DORQ_REG_VF_TYPE_VALUE_0, 0);
16847255736Sdavidch    REG_WR(sc, DORQ_REG_VF_TYPE_MIN_MCID_0, 0);
16848255736Sdavidch    REG_WR(sc, DORQ_REG_VF_TYPE_MAX_MCID_0, 0x1ffff);
16849255736Sdavidch
16850255736Sdavidch    /* set the number of VF alllowed doorbells to the full DQ range */
16851255736Sdavidch    REG_WR(sc, DORQ_REG_VF_NORM_MAX_CID_COUNT, 0x20000);
16852255736Sdavidch
16853255736Sdavidch    /* set the VF doorbell threshold */
16854255736Sdavidch    REG_WR(sc, DORQ_REG_VF_USAGE_CT_LIMIT, 4);
16855255736Sdavidch#endif
16856255736Sdavidch}
16857255736Sdavidch
16858255736Sdavidch/* send a NIG loopback debug packet */
16859255736Sdavidchstatic void
16860255736Sdavidchbxe_lb_pckt(struct bxe_softc *sc)
16861255736Sdavidch{
16862255736Sdavidch    uint32_t wb_write[3];
16863255736Sdavidch
16864255736Sdavidch    /* Ethernet source and destination addresses */
16865255736Sdavidch    wb_write[0] = 0x55555555;
16866255736Sdavidch    wb_write[1] = 0x55555555;
16867255736Sdavidch    wb_write[2] = 0x20;     /* SOP */
16868255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16869255736Sdavidch
16870255736Sdavidch    /* NON-IP protocol */
16871255736Sdavidch    wb_write[0] = 0x09000000;
16872255736Sdavidch    wb_write[1] = 0x55555555;
16873255736Sdavidch    wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16874255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16875255736Sdavidch}
16876255736Sdavidch
16877255736Sdavidch/*
16878255736Sdavidch * Some of the internal memories are not directly readable from the driver.
16879255736Sdavidch * To test them we send debug packets.
16880255736Sdavidch */
16881255736Sdavidchstatic int
16882255736Sdavidchbxe_int_mem_test(struct bxe_softc *sc)
16883255736Sdavidch{
16884255736Sdavidch    int factor;
16885255736Sdavidch    int count, i;
16886255736Sdavidch    uint32_t val = 0;
16887255736Sdavidch
16888255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
16889255736Sdavidch        factor = 120;
16890255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
16891255736Sdavidch        factor = 200;
16892255736Sdavidch    } else {
16893255736Sdavidch        factor = 1;
16894255736Sdavidch    }
16895255736Sdavidch
16896255736Sdavidch    /* disable inputs of parser neighbor blocks */
16897255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16898255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16899255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16900255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16901255736Sdavidch
16902255736Sdavidch    /*  write 0 to parser credits for CFC search request */
16903255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16904255736Sdavidch
16905255736Sdavidch    /* send Ethernet packet */
16906255736Sdavidch    bxe_lb_pckt(sc);
16907255736Sdavidch
16908255736Sdavidch    /* TODO do i reset NIG statistic? */
16909255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16910255736Sdavidch    count = 1000 * factor;
16911255736Sdavidch    while (count) {
16912255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16913255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16914255736Sdavidch        if (val == 0x10) {
16915255736Sdavidch            break;
16916255736Sdavidch        }
16917255736Sdavidch
16918255736Sdavidch        DELAY(10000);
16919255736Sdavidch        count--;
16920255736Sdavidch    }
16921255736Sdavidch
16922255736Sdavidch    if (val != 0x10) {
16923255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16924255736Sdavidch        return (-1);
16925255736Sdavidch    }
16926255736Sdavidch
16927255736Sdavidch    /* wait until PRS register shows 1 packet */
16928255736Sdavidch    count = (1000 * factor);
16929255736Sdavidch    while (count) {
16930255736Sdavidch        val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16931255736Sdavidch        if (val == 1) {
16932255736Sdavidch            break;
16933255736Sdavidch        }
16934255736Sdavidch
16935255736Sdavidch        DELAY(10000);
16936255736Sdavidch        count--;
16937255736Sdavidch    }
16938255736Sdavidch
16939255736Sdavidch    if (val != 0x1) {
16940255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16941255736Sdavidch        return (-2);
16942255736Sdavidch    }
16943255736Sdavidch
16944255736Sdavidch    /* Reset and init BRB, PRS */
16945255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16946255736Sdavidch    DELAY(50000);
16947255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16948255736Sdavidch    DELAY(50000);
16949255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16950255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16951255736Sdavidch
16952255736Sdavidch    /* Disable inputs of parser neighbor blocks */
16953255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16954255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16955255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16956255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16957255736Sdavidch
16958255736Sdavidch    /* Write 0 to parser credits for CFC search request */
16959255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16960255736Sdavidch
16961255736Sdavidch    /* send 10 Ethernet packets */
16962255736Sdavidch    for (i = 0; i < 10; i++) {
16963255736Sdavidch        bxe_lb_pckt(sc);
16964255736Sdavidch    }
16965255736Sdavidch
16966255736Sdavidch    /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16967255736Sdavidch    count = (1000 * factor);
16968255736Sdavidch    while (count) {
16969255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16970255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16971255736Sdavidch        if (val == 0xb0) {
16972255736Sdavidch            break;
16973255736Sdavidch        }
16974255736Sdavidch
16975255736Sdavidch        DELAY(10000);
16976255736Sdavidch        count--;
16977255736Sdavidch    }
16978255736Sdavidch
16979255736Sdavidch    if (val != 0xb0) {
16980255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16981255736Sdavidch        return (-3);
16982255736Sdavidch    }
16983255736Sdavidch
16984255736Sdavidch    /* Wait until PRS register shows 2 packets */
16985255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16986255736Sdavidch    if (val != 2) {
16987255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16988255736Sdavidch    }
16989255736Sdavidch
16990255736Sdavidch    /* Write 1 to parser credits for CFC search request */
16991255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16992255736Sdavidch
16993255736Sdavidch    /* Wait until PRS register shows 3 packets */
16994255736Sdavidch    DELAY(10000 * factor);
16995255736Sdavidch
16996255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16997255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16998255736Sdavidch    if (val != 3) {
16999255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
17000255736Sdavidch    }
17001255736Sdavidch
17002255736Sdavidch    /* clear NIG EOP FIFO */
17003255736Sdavidch    for (i = 0; i < 11; i++) {
17004255736Sdavidch        REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
17005255736Sdavidch    }
17006255736Sdavidch
17007255736Sdavidch    val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
17008255736Sdavidch    if (val != 1) {
17009255736Sdavidch        BLOGE(sc, "clear of NIG failed\n");
17010255736Sdavidch        return (-4);
17011255736Sdavidch    }
17012255736Sdavidch
17013255736Sdavidch    /* Reset and init BRB, PRS, NIG */
17014255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
17015255736Sdavidch    DELAY(50000);
17016255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
17017255736Sdavidch    DELAY(50000);
17018255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17019255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17020255736Sdavidch    if (!CNIC_SUPPORT(sc)) {
17021255736Sdavidch        /* set NIC mode */
17022255736Sdavidch        REG_WR(sc, PRS_REG_NIC_MODE, 1);
17023255736Sdavidch    }
17024255736Sdavidch
17025255736Sdavidch    /* Enable inputs of parser neighbor blocks */
17026255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
17027255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
17028255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x0);
17029255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
17030255736Sdavidch
17031255736Sdavidch    return (0);
17032255736Sdavidch}
17033255736Sdavidch
17034255736Sdavidchstatic void
17035255736Sdavidchbxe_setup_fan_failure_detection(struct bxe_softc *sc)
17036255736Sdavidch{
17037255736Sdavidch    int is_required;
17038255736Sdavidch    uint32_t val;
17039255736Sdavidch    int port;
17040255736Sdavidch
17041255736Sdavidch    is_required = 0;
17042255736Sdavidch    val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
17043255736Sdavidch           SHARED_HW_CFG_FAN_FAILURE_MASK);
17044255736Sdavidch
17045255736Sdavidch    if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
17046255736Sdavidch        is_required = 1;
17047255736Sdavidch    }
17048255736Sdavidch    /*
17049255736Sdavidch     * The fan failure mechanism is usually related to the PHY type since
17050255736Sdavidch     * the power consumption of the board is affected by the PHY. Currently,
17051255736Sdavidch     * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
17052255736Sdavidch     */
17053255736Sdavidch    else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
17054255736Sdavidch        for (port = PORT_0; port < PORT_MAX; port++) {
17055255736Sdavidch            is_required |= elink_fan_failure_det_req(sc,
17056255736Sdavidch                                                     sc->devinfo.shmem_base,
17057255736Sdavidch                                                     sc->devinfo.shmem2_base,
17058255736Sdavidch                                                     port);
17059255736Sdavidch        }
17060255736Sdavidch    }
17061255736Sdavidch
17062255736Sdavidch    BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
17063255736Sdavidch
17064255736Sdavidch    if (is_required == 0) {
17065255736Sdavidch        return;
17066255736Sdavidch    }
17067255736Sdavidch
17068255736Sdavidch    /* Fan failure is indicated by SPIO 5 */
17069255736Sdavidch    bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
17070255736Sdavidch
17071255736Sdavidch    /* set to active low mode */
17072255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_INT);
17073255736Sdavidch    val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
17074255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_INT, val);
17075255736Sdavidch
17076255736Sdavidch    /* enable interrupt to signal the IGU */
17077255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17078255736Sdavidch    val |= MISC_SPIO_SPIO5;
17079255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
17080255736Sdavidch}
17081255736Sdavidch
17082255736Sdavidchstatic void
17083255736Sdavidchbxe_enable_blocks_attention(struct bxe_softc *sc)
17084255736Sdavidch{
17085255736Sdavidch    uint32_t val;
17086255736Sdavidch
17087255736Sdavidch    REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17088255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17089255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
17090255736Sdavidch    } else {
17091255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
17092255736Sdavidch    }
17093255736Sdavidch    REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17094255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17095255736Sdavidch    /*
17096255736Sdavidch     * mask read length error interrupts in brb for parser
17097255736Sdavidch     * (parsing unit and 'checksum and crc' unit)
17098255736Sdavidch     * these errors are legal (PU reads fixed length and CAC can cause
17099255736Sdavidch     * read length error on truncated packets)
17100255736Sdavidch     */
17101255736Sdavidch    REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
17102255736Sdavidch    REG_WR(sc, QM_REG_QM_INT_MASK, 0);
17103255736Sdavidch    REG_WR(sc, TM_REG_TM_INT_MASK, 0);
17104255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
17105255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
17106255736Sdavidch    REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
17107255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
17108255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
17109255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
17110255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
17111255736Sdavidch    REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
17112255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
17113255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
17114255736Sdavidch    REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
17115255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
17116255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
17117255736Sdavidch    REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
17118255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
17119255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
17120255736Sdavidch
17121255736Sdavidch    val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
17122255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
17123255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
17124255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17125255736Sdavidch        val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
17126255736Sdavidch                PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
17127255736Sdavidch    }
17128255736Sdavidch    REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
17129255736Sdavidch
17130255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
17131255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
17132255736Sdavidch    REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
17133255736Sdavidch/*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
17134255736Sdavidch
17135255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17136255736Sdavidch        /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
17137255736Sdavidch        REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
17138255736Sdavidch    }
17139255736Sdavidch
17140255736Sdavidch    REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
17141255736Sdavidch    REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
17142255736Sdavidch/*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
17143255736Sdavidch    REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
17144255736Sdavidch}
17145255736Sdavidch
17146255736Sdavidch/**
17147255736Sdavidch * bxe_init_hw_common - initialize the HW at the COMMON phase.
17148255736Sdavidch *
17149255736Sdavidch * @sc:     driver handle
17150255736Sdavidch */
17151255736Sdavidchstatic int
17152255736Sdavidchbxe_init_hw_common(struct bxe_softc *sc)
17153255736Sdavidch{
17154255736Sdavidch    uint8_t abs_func_id;
17155255736Sdavidch    uint32_t val;
17156255736Sdavidch
17157255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
17158255736Sdavidch          SC_ABS_FUNC(sc));
17159255736Sdavidch
17160255736Sdavidch    /*
17161255736Sdavidch     * take the RESET lock to protect undi_unload flow from accessing
17162255736Sdavidch     * registers while we are resetting the chip
17163255736Sdavidch     */
17164255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17165255736Sdavidch
17166255736Sdavidch    bxe_reset_common(sc);
17167255736Sdavidch
17168255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
17169255736Sdavidch
17170255736Sdavidch    val = 0xfffc;
17171255736Sdavidch    if (CHIP_IS_E3(sc)) {
17172255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
17173255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
17174255736Sdavidch    }
17175255736Sdavidch
17176255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
17177255736Sdavidch
17178255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17179255736Sdavidch
17180255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
17181255736Sdavidch    BLOGD(sc, DBG_LOAD, "after misc block init\n");
17182255736Sdavidch
17183255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17184255736Sdavidch        /*
17185255736Sdavidch         * 4-port mode or 2-port mode we need to turn off master-enable for
17186255736Sdavidch         * everyone. After that we turn it back on for self. So, we disregard
17187255736Sdavidch         * multi-function, and always disable all functions on the given path,
17188255736Sdavidch         * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
17189255736Sdavidch         */
17190255736Sdavidch        for (abs_func_id = SC_PATH(sc);
17191255736Sdavidch             abs_func_id < (E2_FUNC_MAX * 2);
17192255736Sdavidch             abs_func_id += 2) {
17193255736Sdavidch            if (abs_func_id == SC_ABS_FUNC(sc)) {
17194255736Sdavidch                REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17195255736Sdavidch                continue;
17196255736Sdavidch            }
17197255736Sdavidch
17198255736Sdavidch            bxe_pretend_func(sc, abs_func_id);
17199255736Sdavidch
17200255736Sdavidch            /* clear pf enable */
17201255736Sdavidch            bxe_pf_disable(sc);
17202255736Sdavidch
17203255736Sdavidch            bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17204255736Sdavidch        }
17205255736Sdavidch    }
17206255736Sdavidch
17207255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pf disable\n");
17208255736Sdavidch
17209255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
17210255736Sdavidch
17211255736Sdavidch    if (CHIP_IS_E1(sc)) {
17212255736Sdavidch        /*
17213255736Sdavidch         * enable HW interrupt from PXP on USDM overflow
17214255736Sdavidch         * bit 16 on INT_MASK_0
17215255736Sdavidch         */
17216255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17217255736Sdavidch    }
17218255736Sdavidch
17219255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
17220255736Sdavidch    bxe_init_pxp(sc);
17221255736Sdavidch
17222255736Sdavidch#ifdef __BIG_ENDIAN
17223255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
17224255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
17225255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
17226255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
17227255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
17228255736Sdavidch    /* make sure this value is 0 */
17229255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
17230255736Sdavidch
17231255736Sdavidch    //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
17232255736Sdavidch    REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
17233255736Sdavidch    REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
17234255736Sdavidch    REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
17235255736Sdavidch    REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
17236255736Sdavidch#endif
17237255736Sdavidch
17238255736Sdavidch    ecore_ilt_init_page_size(sc, INITOP_SET);
17239255736Sdavidch
17240255736Sdavidch    if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
17241255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
17242255736Sdavidch    }
17243255736Sdavidch
17244255736Sdavidch    /* let the HW do it's magic... */
17245255736Sdavidch    DELAY(100000);
17246255736Sdavidch
17247255736Sdavidch    /* finish PXP init */
17248255736Sdavidch    val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
17249255736Sdavidch    if (val != 1) {
17250255736Sdavidch        BLOGE(sc, "PXP2 CFG failed\n");
17251255736Sdavidch        return (-1);
17252255736Sdavidch    }
17253255736Sdavidch    val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
17254255736Sdavidch    if (val != 1) {
17255255736Sdavidch        BLOGE(sc, "PXP2 RD_INIT failed\n");
17256255736Sdavidch        return (-1);
17257255736Sdavidch    }
17258255736Sdavidch
17259255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pxp init\n");
17260255736Sdavidch
17261255736Sdavidch    /*
17262255736Sdavidch     * Timer bug workaround for E2 only. We need to set the entire ILT to have
17263255736Sdavidch     * entries with value "0" and valid bit on. This needs to be done by the
17264255736Sdavidch     * first PF that is loaded in a path (i.e. common phase)
17265255736Sdavidch     */
17266255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17267255736Sdavidch/*
17268255736Sdavidch * In E2 there is a bug in the timers block that can cause function 6 / 7
17269255736Sdavidch * (i.e. vnic3) to start even if it is marked as "scan-off".
17270255736Sdavidch * This occurs when a different function (func2,3) is being marked
17271255736Sdavidch * as "scan-off". Real-life scenario for example: if a driver is being
17272255736Sdavidch * load-unloaded while func6,7 are down. This will cause the timer to access
17273255736Sdavidch * the ilt, translate to a logical address and send a request to read/write.
17274255736Sdavidch * Since the ilt for the function that is down is not valid, this will cause
17275255736Sdavidch * a translation error which is unrecoverable.
17276255736Sdavidch * The Workaround is intended to make sure that when this happens nothing
17277255736Sdavidch * fatal will occur. The workaround:
17278255736Sdavidch *  1.  First PF driver which loads on a path will:
17279255736Sdavidch *      a.  After taking the chip out of reset, by using pretend,
17280255736Sdavidch *          it will write "0" to the following registers of
17281255736Sdavidch *          the other vnics.
17282255736Sdavidch *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
17283255736Sdavidch *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
17284255736Sdavidch *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
17285255736Sdavidch *          And for itself it will write '1' to
17286255736Sdavidch *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
17287255736Sdavidch *          dmae-operations (writing to pram for example.)
17288255736Sdavidch *          note: can be done for only function 6,7 but cleaner this
17289255736Sdavidch *            way.
17290255736Sdavidch *      b.  Write zero+valid to the entire ILT.
17291255736Sdavidch *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
17292255736Sdavidch *          VNIC3 (of that port). The range allocated will be the
17293255736Sdavidch *          entire ILT. This is needed to prevent  ILT range error.
17294255736Sdavidch *  2.  Any PF driver load flow:
17295255736Sdavidch *      a.  ILT update with the physical addresses of the allocated
17296255736Sdavidch *          logical pages.
17297255736Sdavidch *      b.  Wait 20msec. - note that this timeout is needed to make
17298255736Sdavidch *          sure there are no requests in one of the PXP internal
17299255736Sdavidch *          queues with "old" ILT addresses.
17300255736Sdavidch *      c.  PF enable in the PGLC.
17301255736Sdavidch *      d.  Clear the was_error of the PF in the PGLC. (could have
17302255736Sdavidch *          occurred while driver was down)
17303255736Sdavidch *      e.  PF enable in the CFC (WEAK + STRONG)
17304255736Sdavidch *      f.  Timers scan enable
17305255736Sdavidch *  3.  PF driver unload flow:
17306255736Sdavidch *      a.  Clear the Timers scan_en.
17307255736Sdavidch *      b.  Polling for scan_on=0 for that PF.
17308255736Sdavidch *      c.  Clear the PF enable bit in the PXP.
17309255736Sdavidch *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
17310255736Sdavidch *      e.  Write zero+valid to all ILT entries (The valid bit must
17311255736Sdavidch *          stay set)
17312255736Sdavidch *      f.  If this is VNIC 3 of a port then also init
17313255736Sdavidch *          first_timers_ilt_entry to zero and last_timers_ilt_entry
17314255736Sdavidch *          to the last enrty in the ILT.
17315255736Sdavidch *
17316255736Sdavidch *      Notes:
17317255736Sdavidch *      Currently the PF error in the PGLC is non recoverable.
17318255736Sdavidch *      In the future the there will be a recovery routine for this error.
17319255736Sdavidch *      Currently attention is masked.
17320255736Sdavidch *      Having an MCP lock on the load/unload process does not guarantee that
17321255736Sdavidch *      there is no Timer disable during Func6/7 enable. This is because the
17322255736Sdavidch *      Timers scan is currently being cleared by the MCP on FLR.
17323255736Sdavidch *      Step 2.d can be done only for PF6/7 and the driver can also check if
17324255736Sdavidch *      there is error before clearing it. But the flow above is simpler and
17325255736Sdavidch *      more general.
17326255736Sdavidch *      All ILT entries are written by zero+valid and not just PF6/7
17327255736Sdavidch *      ILT entries since in the future the ILT entries allocation for
17328255736Sdavidch *      PF-s might be dynamic.
17329255736Sdavidch */
17330255736Sdavidch        struct ilt_client_info ilt_cli;
17331255736Sdavidch        struct ecore_ilt ilt;
17332255736Sdavidch
17333255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
17334255736Sdavidch        memset(&ilt, 0, sizeof(struct ecore_ilt));
17335255736Sdavidch
17336255736Sdavidch        /* initialize dummy TM client */
17337255736Sdavidch        ilt_cli.start      = 0;
17338255736Sdavidch        ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
17339255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
17340255736Sdavidch
17341255736Sdavidch        /*
17342255736Sdavidch         * Step 1: set zeroes to all ilt page entries with valid bit on
17343255736Sdavidch         * Step 2: set the timers first/last ilt entry to point
17344255736Sdavidch         * to the entire range to prevent ILT range error for 3rd/4th
17345255736Sdavidch         * vnic (this code assumes existence of the vnic)
17346255736Sdavidch         *
17347255736Sdavidch         * both steps performed by call to ecore_ilt_client_init_op()
17348255736Sdavidch         * with dummy TM client
17349255736Sdavidch         *
17350255736Sdavidch         * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
17351255736Sdavidch         * and his brother are split registers
17352255736Sdavidch         */
17353255736Sdavidch
17354255736Sdavidch        bxe_pretend_func(sc, (SC_PATH(sc) + 6));
17355255736Sdavidch        ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
17356255736Sdavidch        bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17357255736Sdavidch
17358255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
17359255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
17360255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
17361255736Sdavidch    }
17362255736Sdavidch
17363255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
17364255736Sdavidch    REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
17365255736Sdavidch
17366255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17367255736Sdavidch        int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
17368255736Sdavidch                     (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
17369255736Sdavidch
17370255736Sdavidch        ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
17371255736Sdavidch        ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
17372255736Sdavidch
17373255736Sdavidch        /* let the HW do it's magic... */
17374255736Sdavidch        do {
17375255736Sdavidch            DELAY(200000);
17376255736Sdavidch            val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
17377255736Sdavidch        } while (factor-- && (val != 1));
17378255736Sdavidch
17379255736Sdavidch        if (val != 1) {
17380255736Sdavidch            BLOGE(sc, "ATC_INIT failed\n");
17381255736Sdavidch            return (-1);
17382255736Sdavidch        }
17383255736Sdavidch    }
17384255736Sdavidch
17385255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
17386255736Sdavidch
17387255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
17388255736Sdavidch
17389255736Sdavidch    bxe_iov_init_dmae(sc);
17390255736Sdavidch
17391255736Sdavidch    /* clean the DMAE memory */
17392255736Sdavidch    sc->dmae_ready = 1;
17393255736Sdavidch    ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
17394255736Sdavidch
17395255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
17396255736Sdavidch
17397255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
17398255736Sdavidch
17399255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
17400255736Sdavidch
17401255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
17402255736Sdavidch
17403255736Sdavidch    bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
17404255736Sdavidch    bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
17405255736Sdavidch    bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
17406255736Sdavidch    bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
17407255736Sdavidch
17408255736Sdavidch    ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
17409255736Sdavidch
17410255736Sdavidch    /* QM queues pointers table */
17411255736Sdavidch    ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
17412255736Sdavidch
17413255736Sdavidch    /* soft reset pulse */
17414255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 1);
17415255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 0);
17416255736Sdavidch
17417255736Sdavidch    if (CNIC_SUPPORT(sc))
17418255736Sdavidch        ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
17419255736Sdavidch
17420255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
17421255736Sdavidch    REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
17422255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17423255736Sdavidch        /* enable hw interrupt from doorbell Q */
17424255736Sdavidch        REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17425255736Sdavidch    }
17426255736Sdavidch
17427255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17428255736Sdavidch
17429255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17430255736Sdavidch    REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
17431255736Sdavidch
17432255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17433255736Sdavidch        REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
17434255736Sdavidch    }
17435255736Sdavidch
17436255736Sdavidch    if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
17437255736Sdavidch        if (IS_MF_AFEX(sc)) {
17438255736Sdavidch            /*
17439255736Sdavidch             * configure that AFEX and VLAN headers must be
17440255736Sdavidch             * received in AFEX mode
17441255736Sdavidch             */
17442255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
17443255736Sdavidch            REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
17444255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
17445255736Sdavidch            REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
17446255736Sdavidch            REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
17447255736Sdavidch        } else {
17448255736Sdavidch            /*
17449255736Sdavidch             * Bit-map indicating which L2 hdrs may appear
17450255736Sdavidch             * after the basic Ethernet header
17451255736Sdavidch             */
17452255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
17453255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17454255736Sdavidch        }
17455255736Sdavidch    }
17456255736Sdavidch
17457255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
17458255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
17459255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
17460255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
17461255736Sdavidch
17462255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17463255736Sdavidch        /* reset VFC memories */
17464255736Sdavidch        REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17465255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
17466255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
17467255736Sdavidch        REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17468255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
17469255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
17470255736Sdavidch
17471255736Sdavidch        DELAY(20000);
17472255736Sdavidch    }
17473255736Sdavidch
17474255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
17475255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
17476255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
17477255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
17478255736Sdavidch
17479255736Sdavidch    /* sync semi rtc */
17480255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
17481255736Sdavidch           0x80000000);
17482255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
17483255736Sdavidch           0x80000000);
17484255736Sdavidch
17485255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
17486255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
17487255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
17488255736Sdavidch
17489255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17490255736Sdavidch        if (IS_MF_AFEX(sc)) {
17491255736Sdavidch            /*
17492255736Sdavidch             * configure that AFEX and VLAN headers must be
17493255736Sdavidch             * sent in AFEX mode
17494255736Sdavidch             */
17495255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
17496255736Sdavidch            REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
17497255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
17498255736Sdavidch            REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
17499255736Sdavidch            REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
17500255736Sdavidch        } else {
17501255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
17502255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17503255736Sdavidch        }
17504255736Sdavidch    }
17505255736Sdavidch
17506255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 1);
17507255736Sdavidch
17508255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
17509255736Sdavidch
17510255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17511255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
17512255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
17513255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
17514255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
17515255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
17516255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
17517255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
17518255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
17519255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
17520255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
17521255736Sdavidch    }
17522255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 0);
17523255736Sdavidch
17524255736Sdavidch    if (sizeof(union cdu_context) != 1024) {
17525255736Sdavidch        /* we currently assume that a context is 1024 bytes */
17526255736Sdavidch        BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
17527255736Sdavidch              (long)sizeof(union cdu_context));
17528255736Sdavidch    }
17529255736Sdavidch
17530255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
17531255736Sdavidch    val = (4 << 24) + (0 << 12) + 1024;
17532255736Sdavidch    REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
17533255736Sdavidch
17534255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
17535255736Sdavidch
17536255736Sdavidch    REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
17537255736Sdavidch    /* enable context validation interrupt from CFC */
17538255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17539255736Sdavidch
17540255736Sdavidch    /* set the thresholds to prevent CFC/CDU race */
17541255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
17542255736Sdavidch    ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
17543255736Sdavidch
17544255736Sdavidch    if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
17545255736Sdavidch        REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
17546255736Sdavidch    }
17547255736Sdavidch
17548255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
17549255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
17550255736Sdavidch
17551255736Sdavidch    /* Reset PCIE errors for debug */
17552255736Sdavidch    REG_WR(sc, 0x2814, 0xffffffff);
17553255736Sdavidch    REG_WR(sc, 0x3820, 0xffffffff);
17554255736Sdavidch
17555255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17556255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
17557255736Sdavidch               (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17558255736Sdavidch                PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17559255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17560255736Sdavidch               (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17561255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17562255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17563255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17564255736Sdavidch               (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17565255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17566255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17567255736Sdavidch    }
17568255736Sdavidch
17569255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17570255736Sdavidch
17571255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17572255736Sdavidch        /* in E3 this done in per-port section */
17573255736Sdavidch        if (!CHIP_IS_E3(sc))
17574255736Sdavidch            REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17575255736Sdavidch    }
17576255736Sdavidch
17577255736Sdavidch    if (CHIP_IS_E1H(sc)) {
17578255736Sdavidch        /* not applicable for E2 (and above ...) */
17579255736Sdavidch        REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17580255736Sdavidch    }
17581255736Sdavidch
17582255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
17583255736Sdavidch        DELAY(200000);
17584255736Sdavidch    }
17585255736Sdavidch
17586255736Sdavidch    /* finish CFC init */
17587255736Sdavidch    val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17588255736Sdavidch    if (val != 1) {
17589255736Sdavidch        BLOGE(sc, "CFC LL_INIT failed\n");
17590255736Sdavidch        return (-1);
17591255736Sdavidch    }
17592255736Sdavidch    val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17593255736Sdavidch    if (val != 1) {
17594255736Sdavidch        BLOGE(sc, "CFC AC_INIT failed\n");
17595255736Sdavidch        return (-1);
17596255736Sdavidch    }
17597255736Sdavidch    val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17598255736Sdavidch    if (val != 1) {
17599255736Sdavidch        BLOGE(sc, "CFC CAM_INIT failed\n");
17600255736Sdavidch        return (-1);
17601255736Sdavidch    }
17602255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0);
17603255736Sdavidch
17604255736Sdavidch    if (CHIP_IS_E1(sc)) {
17605255736Sdavidch        /* read NIG statistic to see if this is our first up since powerup */
17606255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17607255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
17608255736Sdavidch
17609255736Sdavidch        /* do internal memory self test */
17610255736Sdavidch        if ((val == 0) && bxe_int_mem_test(sc)) {
17611255736Sdavidch            BLOGE(sc, "internal mem self test failed\n");
17612255736Sdavidch            return (-1);
17613255736Sdavidch        }
17614255736Sdavidch    }
17615255736Sdavidch
17616255736Sdavidch    bxe_setup_fan_failure_detection(sc);
17617255736Sdavidch
17618255736Sdavidch    /* clear PXP2 attentions */
17619255736Sdavidch    REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17620255736Sdavidch
17621255736Sdavidch    bxe_enable_blocks_attention(sc);
17622255736Sdavidch
17623255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17624255736Sdavidch        ecore_enable_blocks_parity(sc);
17625255736Sdavidch    }
17626255736Sdavidch
17627255736Sdavidch    if (!BXE_NOMCP(sc)) {
17628255736Sdavidch        if (CHIP_IS_E1x(sc)) {
17629255736Sdavidch            bxe_common_init_phy(sc);
17630255736Sdavidch        }
17631255736Sdavidch    }
17632255736Sdavidch
17633255736Sdavidch    return (0);
17634255736Sdavidch}
17635255736Sdavidch
17636255736Sdavidch/**
17637255736Sdavidch * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17638255736Sdavidch *
17639255736Sdavidch * @sc:     driver handle
17640255736Sdavidch */
17641255736Sdavidchstatic int
17642255736Sdavidchbxe_init_hw_common_chip(struct bxe_softc *sc)
17643255736Sdavidch{
17644255736Sdavidch    int rc = bxe_init_hw_common(sc);
17645255736Sdavidch
17646255736Sdavidch    if (rc) {
17647255736Sdavidch        return (rc);
17648255736Sdavidch    }
17649255736Sdavidch
17650255736Sdavidch    /* In E2 2-PORT mode, same ext phy is used for the two paths */
17651255736Sdavidch    if (!BXE_NOMCP(sc)) {
17652255736Sdavidch        bxe_common_init_phy(sc);
17653255736Sdavidch    }
17654255736Sdavidch
17655255736Sdavidch    return (0);
17656255736Sdavidch}
17657255736Sdavidch
17658255736Sdavidchstatic int
17659255736Sdavidchbxe_init_hw_port(struct bxe_softc *sc)
17660255736Sdavidch{
17661255736Sdavidch    int port = SC_PORT(sc);
17662255736Sdavidch    int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17663255736Sdavidch    uint32_t low, high;
17664255736Sdavidch    uint32_t val;
17665255736Sdavidch
17666255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17667255736Sdavidch
17668255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17669255736Sdavidch
17670255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17671255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17672255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17673255736Sdavidch
17674255736Sdavidch    /*
17675255736Sdavidch     * Timers bug workaround: disables the pf_master bit in pglue at
17676255736Sdavidch     * common phase, we need to enable it here before any dmae access are
17677255736Sdavidch     * attempted. Therefore we manually added the enable-master to the
17678255736Sdavidch     * port phase (it also happens in the function phase)
17679255736Sdavidch     */
17680255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17681255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17682255736Sdavidch    }
17683255736Sdavidch
17684255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17685255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17686255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17687255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17688255736Sdavidch
17689255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17690255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17691255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17692255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17693255736Sdavidch
17694255736Sdavidch    /* QM cid (connection) count */
17695255736Sdavidch    ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17696255736Sdavidch
17697255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17698255736Sdavidch        ecore_init_block(sc, BLOCK_TM, init_phase);
17699255736Sdavidch        REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17700255736Sdavidch        REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17701255736Sdavidch    }
17702255736Sdavidch
17703255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17704255736Sdavidch
17705255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17706255736Sdavidch
17707255736Sdavidch    if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17708255736Sdavidch        if (IS_MF(sc)) {
17709255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 160 : 246);
17710255736Sdavidch        } else if (sc->mtu > 4096) {
17711255736Sdavidch            if (BXE_ONE_PORT(sc)) {
17712255736Sdavidch                low = 160;
17713255736Sdavidch            } else {
17714255736Sdavidch                val = sc->mtu;
17715255736Sdavidch                /* (24*1024 + val*4)/256 */
17716255736Sdavidch                low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17717255736Sdavidch            }
17718255736Sdavidch        } else {
17719255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 80 : 160);
17720255736Sdavidch        }
17721255736Sdavidch        high = (low + 56); /* 14*1024/256 */
17722255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17723255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17724255736Sdavidch    }
17725255736Sdavidch
17726255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
17727255736Sdavidch        REG_WR(sc, SC_PORT(sc) ?
17728255736Sdavidch               BRB1_REG_MAC_GUARANTIED_1 :
17729255736Sdavidch               BRB1_REG_MAC_GUARANTIED_0, 40);
17730255736Sdavidch    }
17731255736Sdavidch
17732255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17733255736Sdavidch    if (CHIP_IS_E3B0(sc)) {
17734255736Sdavidch        if (IS_MF_AFEX(sc)) {
17735255736Sdavidch            /* configure headers for AFEX mode */
17736255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17737255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17738255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17739255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17740255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17741255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17742255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17743255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17744255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17745255736Sdavidch        } else {
17746255736Sdavidch            /* Ovlan exists only if we are in multi-function +
17747255736Sdavidch             * switch-dependent mode, in switch-independent there
17748255736Sdavidch             * is no ovlan headers
17749255736Sdavidch             */
17750255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17751255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17752255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17753255736Sdavidch                   (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17754255736Sdavidch        }
17755255736Sdavidch    }
17756255736Sdavidch
17757255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17758255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17759255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17760255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17761255736Sdavidch
17762255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17763255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17764255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17765255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17766255736Sdavidch
17767255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17768255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17769255736Sdavidch
17770255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17771255736Sdavidch
17772255736Sdavidch    if (CHIP_IS_E1x(sc)) {
17773255736Sdavidch        /* configure PBF to work without PAUSE mtu 9000 */
17774255736Sdavidch        REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17775255736Sdavidch
17776255736Sdavidch        /* update threshold */
17777255736Sdavidch        REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17778255736Sdavidch        /* update init credit */
17779255736Sdavidch        REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17780255736Sdavidch
17781255736Sdavidch        /* probe changes */
17782255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17783255736Sdavidch        DELAY(50);
17784255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17785255736Sdavidch    }
17786255736Sdavidch
17787255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17788255736Sdavidch        ecore_init_block(sc, BLOCK_SRC, init_phase);
17789255736Sdavidch    }
17790255736Sdavidch
17791255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17792255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17793255736Sdavidch
17794255736Sdavidch    if (CHIP_IS_E1(sc)) {
17795255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17796255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17797255736Sdavidch    }
17798255736Sdavidch    ecore_init_block(sc, BLOCK_HC, init_phase);
17799255736Sdavidch
17800255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, init_phase);
17801255736Sdavidch
17802255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17803255736Sdavidch    /* init aeu_mask_attn_func_0/1:
17804255736Sdavidch     *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17805255736Sdavidch     *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17806255736Sdavidch     *             bits 4-7 are used for "per vn group attention" */
17807255736Sdavidch    val = IS_MF(sc) ? 0xF7 : 0x7;
17808255736Sdavidch    /* Enable DCBX attention for all but E1 */
17809255736Sdavidch    val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17810255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17811255736Sdavidch
17812255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17813255736Sdavidch
17814255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17815255736Sdavidch        /* Bit-map indicating which L2 hdrs may appear after the
17816255736Sdavidch         * basic Ethernet header
17817255736Sdavidch         */
17818255736Sdavidch        if (IS_MF_AFEX(sc)) {
17819255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17820255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17821255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17822255736Sdavidch        } else {
17823255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17824255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17825255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC,
17826255736Sdavidch                   IS_MF_SD(sc) ? 7 : 6);
17827255736Sdavidch        }
17828255736Sdavidch
17829255736Sdavidch        if (CHIP_IS_E3(sc)) {
17830255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17831255736Sdavidch                   NIG_REG_LLH1_MF_MODE :
17832255736Sdavidch                   NIG_REG_LLH_MF_MODE, IS_MF(sc));
17833255736Sdavidch        }
17834255736Sdavidch    }
17835255736Sdavidch    if (!CHIP_IS_E3(sc)) {
17836255736Sdavidch        REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17837255736Sdavidch    }
17838255736Sdavidch
17839255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17840255736Sdavidch        /* 0x2 disable mf_ov, 0x1 enable */
17841255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17842255736Sdavidch               (IS_MF_SD(sc) ? 0x1 : 0x2));
17843255736Sdavidch
17844255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17845255736Sdavidch            val = 0;
17846255736Sdavidch            switch (sc->devinfo.mf_info.mf_mode) {
17847255736Sdavidch            case MULTI_FUNCTION_SD:
17848255736Sdavidch                val = 1;
17849255736Sdavidch                break;
17850255736Sdavidch            case MULTI_FUNCTION_SI:
17851255736Sdavidch            case MULTI_FUNCTION_AFEX:
17852255736Sdavidch                val = 2;
17853255736Sdavidch                break;
17854255736Sdavidch            }
17855255736Sdavidch
17856255736Sdavidch            REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17857255736Sdavidch                        NIG_REG_LLH0_CLS_TYPE), val);
17858255736Sdavidch        }
17859255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17860255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17861255736Sdavidch        REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17862255736Sdavidch    }
17863255736Sdavidch
17864255736Sdavidch    /* If SPIO5 is set to generate interrupts, enable it for this port */
17865255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17866255736Sdavidch    if (val & MISC_SPIO_SPIO5) {
17867255736Sdavidch        uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17868255736Sdavidch                                    MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17869255736Sdavidch        val = REG_RD(sc, reg_addr);
17870255736Sdavidch        val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17871255736Sdavidch        REG_WR(sc, reg_addr, val);
17872255736Sdavidch    }
17873255736Sdavidch
17874255736Sdavidch    return (0);
17875255736Sdavidch}
17876255736Sdavidch
17877255736Sdavidchstatic uint32_t
17878255736Sdavidchbxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17879255736Sdavidch                       uint32_t         reg,
17880255736Sdavidch                       uint32_t         expected,
17881255736Sdavidch                       uint32_t         poll_count)
17882255736Sdavidch{
17883255736Sdavidch    uint32_t cur_cnt = poll_count;
17884255736Sdavidch    uint32_t val;
17885255736Sdavidch
17886255736Sdavidch    while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17887255736Sdavidch        DELAY(FLR_WAIT_INTERVAL);
17888255736Sdavidch    }
17889255736Sdavidch
17890255736Sdavidch    return (val);
17891255736Sdavidch}
17892255736Sdavidch
17893255736Sdavidchstatic int
17894255736Sdavidchbxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17895255736Sdavidch                              uint32_t         reg,
17896255736Sdavidch                              char             *msg,
17897255736Sdavidch                              uint32_t         poll_cnt)
17898255736Sdavidch{
17899255736Sdavidch    uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17900255736Sdavidch
17901255736Sdavidch    if (val != 0) {
17902255736Sdavidch        BLOGE(sc, "%s usage count=%d\n", msg, val);
17903255736Sdavidch        return (1);
17904255736Sdavidch    }
17905255736Sdavidch
17906255736Sdavidch    return (0);
17907255736Sdavidch}
17908255736Sdavidch
17909255736Sdavidch/* Common routines with VF FLR cleanup */
17910255736Sdavidchstatic uint32_t
17911255736Sdavidchbxe_flr_clnup_poll_count(struct bxe_softc *sc)
17912255736Sdavidch{
17913255736Sdavidch    /* adjust polling timeout */
17914255736Sdavidch    if (CHIP_REV_IS_EMUL(sc)) {
17915255736Sdavidch        return (FLR_POLL_CNT * 2000);
17916255736Sdavidch    }
17917255736Sdavidch
17918255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
17919255736Sdavidch        return (FLR_POLL_CNT * 120);
17920255736Sdavidch    }
17921255736Sdavidch
17922255736Sdavidch    return (FLR_POLL_CNT);
17923255736Sdavidch}
17924255736Sdavidch
17925255736Sdavidchstatic int
17926255736Sdavidchbxe_poll_hw_usage_counters(struct bxe_softc *sc,
17927255736Sdavidch                           uint32_t         poll_cnt)
17928255736Sdavidch{
17929255736Sdavidch    /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17930255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17931255736Sdavidch                                      CFC_REG_NUM_LCIDS_INSIDE_PF,
17932255736Sdavidch                                      "CFC PF usage counter timed out",
17933255736Sdavidch                                      poll_cnt)) {
17934255736Sdavidch        return (1);
17935255736Sdavidch    }
17936255736Sdavidch
17937255736Sdavidch    /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17938255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17939255736Sdavidch                                      DORQ_REG_PF_USAGE_CNT,
17940255736Sdavidch                                      "DQ PF usage counter timed out",
17941255736Sdavidch                                      poll_cnt)) {
17942255736Sdavidch        return (1);
17943255736Sdavidch    }
17944255736Sdavidch
17945255736Sdavidch    /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17946255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17947255736Sdavidch                                      QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17948255736Sdavidch                                      "QM PF usage counter timed out",
17949255736Sdavidch                                      poll_cnt)) {
17950255736Sdavidch        return (1);
17951255736Sdavidch    }
17952255736Sdavidch
17953255736Sdavidch    /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17954255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17955255736Sdavidch                                      TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17956255736Sdavidch                                      "Timers VNIC usage counter timed out",
17957255736Sdavidch                                      poll_cnt)) {
17958255736Sdavidch        return (1);
17959255736Sdavidch    }
17960255736Sdavidch
17961255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17962255736Sdavidch                                      TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17963255736Sdavidch                                      "Timers NUM_SCANS usage counter timed out",
17964255736Sdavidch                                      poll_cnt)) {
17965255736Sdavidch        return (1);
17966255736Sdavidch    }
17967255736Sdavidch
17968255736Sdavidch    /* Wait DMAE PF usage counter to zero */
17969255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17970255736Sdavidch                                      dmae_reg_go_c[INIT_DMAE_C(sc)],
17971255736Sdavidch                                      "DMAE dommand register timed out",
17972255736Sdavidch                                      poll_cnt)) {
17973255736Sdavidch        return (1);
17974255736Sdavidch    }
17975255736Sdavidch
17976255736Sdavidch    return (0);
17977255736Sdavidch}
17978255736Sdavidch
17979255736Sdavidch#define OP_GEN_PARAM(param)                                            \
17980255736Sdavidch    (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17981255736Sdavidch#define OP_GEN_TYPE(type)                                           \
17982255736Sdavidch    (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17983255736Sdavidch#define OP_GEN_AGG_VECT(index)                                             \
17984255736Sdavidch    (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17985255736Sdavidch
17986255736Sdavidchstatic int
17987255736Sdavidchbxe_send_final_clnup(struct bxe_softc *sc,
17988255736Sdavidch                     uint8_t          clnup_func,
17989255736Sdavidch                     uint32_t         poll_cnt)
17990255736Sdavidch{
17991255736Sdavidch    uint32_t op_gen_command = 0;
17992255736Sdavidch    uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17993255736Sdavidch                          CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17994255736Sdavidch    int ret = 0;
17995255736Sdavidch
17996255736Sdavidch    if (REG_RD(sc, comp_addr)) {
17997255736Sdavidch        BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17998255736Sdavidch        return (1);
17999255736Sdavidch    }
18000255736Sdavidch
18001255736Sdavidch    op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
18002255736Sdavidch    op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
18003255736Sdavidch    op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
18004255736Sdavidch    op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
18005255736Sdavidch
18006255736Sdavidch    BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
18007255736Sdavidch    REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
18008255736Sdavidch
18009255736Sdavidch    if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
18010255736Sdavidch        BLOGE(sc, "FW final cleanup did not succeed\n");
18011255736Sdavidch        BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
18012255736Sdavidch              (REG_RD(sc, comp_addr)));
18013255736Sdavidch        bxe_panic(sc, ("FLR cleanup failed\n"));
18014255736Sdavidch        return (1);
18015255736Sdavidch    }
18016255736Sdavidch
18017255736Sdavidch    /* Zero completion for nxt FLR */
18018255736Sdavidch    REG_WR(sc, comp_addr, 0);
18019255736Sdavidch
18020255736Sdavidch    return (ret);
18021255736Sdavidch}
18022255736Sdavidch
18023255736Sdavidchstatic void
18024255736Sdavidchbxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
18025255736Sdavidch                       struct pbf_pN_buf_regs *regs,
18026255736Sdavidch                       uint32_t               poll_count)
18027255736Sdavidch{
18028255736Sdavidch    uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
18029255736Sdavidch    uint32_t cur_cnt = poll_count;
18030255736Sdavidch
18031255736Sdavidch    crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
18032255736Sdavidch    crd = crd_start = REG_RD(sc, regs->crd);
18033255736Sdavidch    init_crd = REG_RD(sc, regs->init_crd);
18034255736Sdavidch
18035255736Sdavidch    BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
18036255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
18037255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
18038255736Sdavidch
18039255736Sdavidch    while ((crd != init_crd) &&
18040255736Sdavidch           ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
18041255736Sdavidch            (init_crd - crd_start))) {
18042255736Sdavidch        if (cur_cnt--) {
18043255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
18044255736Sdavidch            crd = REG_RD(sc, regs->crd);
18045255736Sdavidch            crd_freed = REG_RD(sc, regs->crd_freed);
18046255736Sdavidch        } else {
18047255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
18048255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
18049255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
18050255736Sdavidch            break;
18051255736Sdavidch        }
18052255736Sdavidch    }
18053255736Sdavidch
18054255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
18055255736Sdavidch          poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18056255736Sdavidch}
18057255736Sdavidch
18058255736Sdavidchstatic void
18059255736Sdavidchbxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
18060255736Sdavidch                       struct pbf_pN_cmd_regs *regs,
18061255736Sdavidch                       uint32_t               poll_count)
18062255736Sdavidch{
18063255736Sdavidch    uint32_t occup, to_free, freed, freed_start;
18064255736Sdavidch    uint32_t cur_cnt = poll_count;
18065255736Sdavidch
18066255736Sdavidch    occup = to_free = REG_RD(sc, regs->lines_occup);
18067255736Sdavidch    freed = freed_start = REG_RD(sc, regs->lines_freed);
18068255736Sdavidch
18069255736Sdavidch    BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18070255736Sdavidch    BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18071255736Sdavidch
18072255736Sdavidch    while (occup &&
18073255736Sdavidch           ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
18074255736Sdavidch        if (cur_cnt--) {
18075255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
18076255736Sdavidch            occup = REG_RD(sc, regs->lines_occup);
18077255736Sdavidch            freed = REG_RD(sc, regs->lines_freed);
18078255736Sdavidch        } else {
18079255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
18080255736Sdavidch            BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18081255736Sdavidch            BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18082255736Sdavidch            break;
18083255736Sdavidch        }
18084255736Sdavidch    }
18085255736Sdavidch
18086255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
18087255736Sdavidch          poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18088255736Sdavidch}
18089255736Sdavidch
18090255736Sdavidchstatic void
18091255736Sdavidchbxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
18092255736Sdavidch{
18093255736Sdavidch    struct pbf_pN_cmd_regs cmd_regs[] = {
18094255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
18095255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q0 :
18096255736Sdavidch            PBF_REG_P0_TQ_OCCUPANCY,
18097255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18098255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q0 :
18099255736Sdavidch            PBF_REG_P0_TQ_LINES_FREED_CNT},
18100255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
18101255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q1 :
18102255736Sdavidch            PBF_REG_P1_TQ_OCCUPANCY,
18103255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18104255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q1 :
18105255736Sdavidch            PBF_REG_P1_TQ_LINES_FREED_CNT},
18106255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
18107255736Sdavidch            PBF_REG_TQ_OCCUPANCY_LB_Q :
18108255736Sdavidch            PBF_REG_P4_TQ_OCCUPANCY,
18109255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18110255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
18111255736Sdavidch            PBF_REG_P4_TQ_LINES_FREED_CNT}
18112255736Sdavidch    };
18113255736Sdavidch
18114255736Sdavidch    struct pbf_pN_buf_regs buf_regs[] = {
18115255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
18116255736Sdavidch            PBF_REG_INIT_CRD_Q0 :
18117255736Sdavidch            PBF_REG_P0_INIT_CRD ,
18118255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18119255736Sdavidch            PBF_REG_CREDIT_Q0 :
18120255736Sdavidch            PBF_REG_P0_CREDIT,
18121255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18122255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
18123255736Sdavidch            PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
18124255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
18125255736Sdavidch            PBF_REG_INIT_CRD_Q1 :
18126255736Sdavidch            PBF_REG_P1_INIT_CRD,
18127255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18128255736Sdavidch            PBF_REG_CREDIT_Q1 :
18129255736Sdavidch            PBF_REG_P1_CREDIT,
18130255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18131255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
18132255736Sdavidch            PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
18133255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
18134255736Sdavidch            PBF_REG_INIT_CRD_LB_Q :
18135255736Sdavidch            PBF_REG_P4_INIT_CRD,
18136255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18137255736Sdavidch            PBF_REG_CREDIT_LB_Q :
18138255736Sdavidch            PBF_REG_P4_CREDIT,
18139255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18140255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
18141255736Sdavidch            PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
18142255736Sdavidch    };
18143255736Sdavidch
18144255736Sdavidch    int i;
18145255736Sdavidch
18146255736Sdavidch    /* Verify the command queues are flushed P0, P1, P4 */
18147255736Sdavidch    for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
18148255736Sdavidch        bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
18149255736Sdavidch    }
18150255736Sdavidch
18151255736Sdavidch    /* Verify the transmission buffers are flushed P0, P1, P4 */
18152255736Sdavidch    for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
18153255736Sdavidch        bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
18154255736Sdavidch    }
18155255736Sdavidch}
18156255736Sdavidch
18157255736Sdavidchstatic void
18158255736Sdavidchbxe_hw_enable_status(struct bxe_softc *sc)
18159255736Sdavidch{
18160255736Sdavidch    uint32_t val;
18161255736Sdavidch
18162255736Sdavidch    val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
18163255736Sdavidch    BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
18164255736Sdavidch
18165255736Sdavidch    val = REG_RD(sc, PBF_REG_DISABLE_PF);
18166255736Sdavidch    BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
18167255736Sdavidch
18168255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
18169255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
18170255736Sdavidch
18171255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
18172255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
18173255736Sdavidch
18174255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
18175255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
18176255736Sdavidch
18177255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
18178255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
18179255736Sdavidch
18180255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
18181255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
18182255736Sdavidch
18183255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
18184255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
18185255736Sdavidch}
18186255736Sdavidch
18187255736Sdavidchstatic int
18188255736Sdavidchbxe_pf_flr_clnup(struct bxe_softc *sc)
18189255736Sdavidch{
18190255736Sdavidch    uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
18191255736Sdavidch
18192255736Sdavidch    BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
18193255736Sdavidch
18194255736Sdavidch    /* Re-enable PF target read access */
18195255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
18196255736Sdavidch
18197255736Sdavidch    /* Poll HW usage counters */
18198255736Sdavidch    BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
18199255736Sdavidch    if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
18200255736Sdavidch        return (-1);
18201255736Sdavidch    }
18202255736Sdavidch
18203255736Sdavidch    /* Zero the igu 'trailing edge' and 'leading edge' */
18204255736Sdavidch
18205255736Sdavidch    /* Send the FW cleanup command */
18206255736Sdavidch    if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
18207255736Sdavidch        return (-1);
18208255736Sdavidch    }
18209255736Sdavidch
18210255736Sdavidch    /* ATC cleanup */
18211255736Sdavidch
18212255736Sdavidch    /* Verify TX hw is flushed */
18213255736Sdavidch    bxe_tx_hw_flushed(sc, poll_cnt);
18214255736Sdavidch
18215255736Sdavidch    /* Wait 100ms (not adjusted according to platform) */
18216255736Sdavidch    DELAY(100000);
18217255736Sdavidch
18218255736Sdavidch    /* Verify no pending pci transactions */
18219255736Sdavidch    if (bxe_is_pcie_pending(sc)) {
18220255736Sdavidch        BLOGE(sc, "PCIE Transactions still pending\n");
18221255736Sdavidch    }
18222255736Sdavidch
18223255736Sdavidch    /* Debug */
18224255736Sdavidch    bxe_hw_enable_status(sc);
18225255736Sdavidch
18226255736Sdavidch    /*
18227255736Sdavidch     * Master enable - Due to WB DMAE writes performed before this
18228255736Sdavidch     * register is re-initialized as part of the regular function init
18229255736Sdavidch     */
18230255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18231255736Sdavidch
18232255736Sdavidch    return (0);
18233255736Sdavidch}
18234255736Sdavidch
18235255736Sdavidch#if 0
18236255736Sdavidchstatic void
18237255736Sdavidchbxe_init_searcher(struct bxe_softc *sc)
18238255736Sdavidch{
18239255736Sdavidch    int port = SC_PORT(sc);
18240255736Sdavidch    ecore_src_init_t2(sc, sc->t2, sc->t2_mapping, SRC_CONN_NUM);
18241255736Sdavidch    /* T1 hash bits value determines the T1 number of entries */
18242255736Sdavidch    REG_WR(sc, SRC_REG_NUMBER_HASH_BITS0 + port*4, SRC_HASH_BITS);
18243255736Sdavidch}
18244255736Sdavidch#endif
18245255736Sdavidch
18246255736Sdavidchstatic int
18247255736Sdavidchbxe_init_hw_func(struct bxe_softc *sc)
18248255736Sdavidch{
18249255736Sdavidch    int port = SC_PORT(sc);
18250255736Sdavidch    int func = SC_FUNC(sc);
18251255736Sdavidch    int init_phase = PHASE_PF0 + func;
18252255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
18253255736Sdavidch    uint16_t cdu_ilt_start;
18254255736Sdavidch    uint32_t addr, val;
18255255736Sdavidch    uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
18256255736Sdavidch    int i, main_mem_width, rc;
18257255736Sdavidch
18258255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
18259255736Sdavidch
18260255736Sdavidch    /* FLR cleanup */
18261255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18262255736Sdavidch        rc = bxe_pf_flr_clnup(sc);
18263255736Sdavidch        if (rc) {
18264255736Sdavidch            BLOGE(sc, "FLR cleanup failed!\n");
18265255736Sdavidch            // XXX bxe_fw_dump(sc);
18266255736Sdavidch            // XXX bxe_idle_chk(sc);
18267255736Sdavidch            return (rc);
18268255736Sdavidch        }
18269255736Sdavidch    }
18270255736Sdavidch
18271255736Sdavidch    /* set MSI reconfigure capability */
18272255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18273255736Sdavidch        addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
18274255736Sdavidch        val = REG_RD(sc, addr);
18275255736Sdavidch        val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
18276255736Sdavidch        REG_WR(sc, addr, val);
18277255736Sdavidch    }
18278255736Sdavidch
18279255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
18280255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
18281255736Sdavidch
18282255736Sdavidch    ilt = sc->ilt;
18283255736Sdavidch    cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
18284255736Sdavidch
18285255736Sdavidch#if 0
18286255736Sdavidch    if (IS_SRIOV(sc)) {
18287255736Sdavidch        cdu_ilt_start += BXE_FIRST_VF_CID/ILT_PAGE_CIDS;
18288255736Sdavidch    }
18289255736Sdavidch    cdu_ilt_start = bxe_iov_init_ilt(sc, cdu_ilt_start);
18290255736Sdavidch
18291255736Sdavidch#if (BXE_FIRST_VF_CID > 0)
18292255736Sdavidch    /*
18293255736Sdavidch     * If BXE_FIRST_VF_CID > 0 then the PF L2 cids precedes
18294255736Sdavidch     * those of the VFs, so start line should be reset
18295255736Sdavidch     */
18296255736Sdavidch    cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
18297255736Sdavidch#endif
18298255736Sdavidch#endif
18299255736Sdavidch
18300255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
18301255736Sdavidch        ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
18302255736Sdavidch        ilt->lines[cdu_ilt_start + i].page_mapping =
18303255736Sdavidch            sc->context[i].vcxt_dma.paddr;
18304255736Sdavidch        ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
18305255736Sdavidch    }
18306255736Sdavidch    ecore_ilt_init_op(sc, INITOP_SET);
18307255736Sdavidch
18308255736Sdavidch#if 0
18309255736Sdavidch    if (!CONFIGURE_NIC_MODE(sc)) {
18310255736Sdavidch        bxe_init_searcher(sc);
18311255736Sdavidch        REG_WR(sc, PRS_REG_NIC_MODE, 0);
18312255736Sdavidch        BLOGD(sc, DBG_LOAD, "NIC MODE disabled\n");
18313255736Sdavidch    } else
18314255736Sdavidch#endif
18315255736Sdavidch    {
18316255736Sdavidch        /* Set NIC mode */
18317255736Sdavidch        REG_WR(sc, PRS_REG_NIC_MODE, 1);
18318255736Sdavidch        BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
18319255736Sdavidch    }
18320255736Sdavidch
18321255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18322255736Sdavidch        uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
18323255736Sdavidch
18324255736Sdavidch        /* Turn on a single ISR mode in IGU if driver is going to use
18325255736Sdavidch         * INT#x or MSI
18326255736Sdavidch         */
18327255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
18328255736Sdavidch            pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
18329255736Sdavidch        }
18330255736Sdavidch
18331255736Sdavidch        /*
18332255736Sdavidch         * Timers workaround bug: function init part.
18333255736Sdavidch         * Need to wait 20msec after initializing ILT,
18334255736Sdavidch         * needed to make sure there are no requests in
18335255736Sdavidch         * one of the PXP internal queues with "old" ILT addresses
18336255736Sdavidch         */
18337255736Sdavidch        DELAY(20000);
18338255736Sdavidch
18339255736Sdavidch        /*
18340255736Sdavidch         * Master enable - Due to WB DMAE writes performed before this
18341255736Sdavidch         * register is re-initialized as part of the regular function
18342255736Sdavidch         * init
18343255736Sdavidch         */
18344255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18345255736Sdavidch        /* Enable the function in IGU */
18346255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
18347255736Sdavidch    }
18348255736Sdavidch
18349255736Sdavidch    sc->dmae_ready = 1;
18350255736Sdavidch
18351255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
18352255736Sdavidch
18353255736Sdavidch    if (!CHIP_IS_E1x(sc))
18354255736Sdavidch        REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
18355255736Sdavidch
18356255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
18357255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
18358255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
18359255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, init_phase);
18360255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
18361255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
18362255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
18363255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
18364255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
18365255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
18366255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
18367255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
18368255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
18369255736Sdavidch
18370255736Sdavidch    if (!CHIP_IS_E1x(sc))
18371255736Sdavidch        REG_WR(sc, QM_REG_PF_EN, 1);
18372255736Sdavidch
18373255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18374255736Sdavidch        REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18375255736Sdavidch        REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18376255736Sdavidch        REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18377255736Sdavidch        REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18378255736Sdavidch    }
18379255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
18380255736Sdavidch
18381255736Sdavidch    ecore_init_block(sc, BLOCK_TM, init_phase);
18382255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
18383255736Sdavidch
18384255736Sdavidch    bxe_iov_init_dq(sc);
18385255736Sdavidch
18386255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
18387255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
18388255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
18389255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
18390255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
18391255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
18392255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
18393255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
18394255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
18395255736Sdavidch    if (!CHIP_IS_E1x(sc))
18396255736Sdavidch        REG_WR(sc, PBF_REG_DISABLE_PF, 0);
18397255736Sdavidch
18398255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
18399255736Sdavidch
18400255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
18401255736Sdavidch
18402255736Sdavidch    if (!CHIP_IS_E1x(sc))
18403255736Sdavidch        REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
18404255736Sdavidch
18405255736Sdavidch    if (IS_MF(sc)) {
18406255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
18407255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
18408255736Sdavidch    }
18409255736Sdavidch
18410255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
18411255736Sdavidch
18412255736Sdavidch    /* HC init per function */
18413255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18414255736Sdavidch        if (CHIP_IS_E1H(sc)) {
18415255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18416255736Sdavidch
18417255736Sdavidch            REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18418255736Sdavidch            REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18419255736Sdavidch        }
18420255736Sdavidch        ecore_init_block(sc, BLOCK_HC, init_phase);
18421255736Sdavidch
18422255736Sdavidch    } else {
18423255736Sdavidch        int num_segs, sb_idx, prod_offset;
18424255736Sdavidch
18425255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18426255736Sdavidch
18427255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
18428255736Sdavidch            REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18429255736Sdavidch            REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18430255736Sdavidch        }
18431255736Sdavidch
18432255736Sdavidch        ecore_init_block(sc, BLOCK_IGU, init_phase);
18433255736Sdavidch
18434255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
18435255736Sdavidch            int dsb_idx = 0;
18436255736Sdavidch            /**
18437255736Sdavidch             * Producer memory:
18438255736Sdavidch             * E2 mode: address 0-135 match to the mapping memory;
18439255736Sdavidch             * 136 - PF0 default prod; 137 - PF1 default prod;
18440255736Sdavidch             * 138 - PF2 default prod; 139 - PF3 default prod;
18441255736Sdavidch             * 140 - PF0 attn prod;    141 - PF1 attn prod;
18442255736Sdavidch             * 142 - PF2 attn prod;    143 - PF3 attn prod;
18443255736Sdavidch             * 144-147 reserved.
18444255736Sdavidch             *
18445255736Sdavidch             * E1.5 mode - In backward compatible mode;
18446255736Sdavidch             * for non default SB; each even line in the memory
18447255736Sdavidch             * holds the U producer and each odd line hold
18448255736Sdavidch             * the C producer. The first 128 producers are for
18449255736Sdavidch             * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
18450255736Sdavidch             * producers are for the DSB for each PF.
18451255736Sdavidch             * Each PF has five segments: (the order inside each
18452255736Sdavidch             * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
18453255736Sdavidch             * 132-135 C prods; 136-139 X prods; 140-143 T prods;
18454255736Sdavidch             * 144-147 attn prods;
18455255736Sdavidch             */
18456255736Sdavidch            /* non-default-status-blocks */
18457255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18458255736Sdavidch                IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
18459255736Sdavidch            for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
18460255736Sdavidch                prod_offset = (sc->igu_base_sb + sb_idx) *
18461255736Sdavidch                    num_segs;
18462255736Sdavidch
18463255736Sdavidch                for (i = 0; i < num_segs; i++) {
18464255736Sdavidch                    addr = IGU_REG_PROD_CONS_MEMORY +
18465255736Sdavidch                            (prod_offset + i) * 4;
18466255736Sdavidch                    REG_WR(sc, addr, 0);
18467255736Sdavidch                }
18468255736Sdavidch                /* send consumer update with value 0 */
18469255736Sdavidch                bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
18470255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18471255736Sdavidch                bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
18472255736Sdavidch            }
18473255736Sdavidch
18474255736Sdavidch            /* default-status-blocks */
18475255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18476255736Sdavidch                IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
18477255736Sdavidch
18478255736Sdavidch            if (CHIP_IS_MODE_4_PORT(sc))
18479255736Sdavidch                dsb_idx = SC_FUNC(sc);
18480255736Sdavidch            else
18481255736Sdavidch                dsb_idx = SC_VN(sc);
18482255736Sdavidch
18483255736Sdavidch            prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
18484255736Sdavidch                       IGU_BC_BASE_DSB_PROD + dsb_idx :
18485255736Sdavidch                       IGU_NORM_BASE_DSB_PROD + dsb_idx);
18486255736Sdavidch
18487255736Sdavidch            /*
18488255736Sdavidch             * igu prods come in chunks of E1HVN_MAX (4) -
18489255736Sdavidch             * does not matters what is the current chip mode
18490255736Sdavidch             */
18491255736Sdavidch            for (i = 0; i < (num_segs * E1HVN_MAX);
18492255736Sdavidch                 i += E1HVN_MAX) {
18493255736Sdavidch                addr = IGU_REG_PROD_CONS_MEMORY +
18494255736Sdavidch                            (prod_offset + i)*4;
18495255736Sdavidch                REG_WR(sc, addr, 0);
18496255736Sdavidch            }
18497255736Sdavidch            /* send consumer update with 0 */
18498255736Sdavidch            if (CHIP_INT_MODE_IS_BC(sc)) {
18499255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18500255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18501255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18502255736Sdavidch                           CSTORM_ID, 0, IGU_INT_NOP, 1);
18503255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18504255736Sdavidch                           XSTORM_ID, 0, IGU_INT_NOP, 1);
18505255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18506255736Sdavidch                           TSTORM_ID, 0, IGU_INT_NOP, 1);
18507255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18508255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
18509255736Sdavidch            } else {
18510255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18511255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18512255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18513255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
18514255736Sdavidch            }
18515255736Sdavidch            bxe_igu_clear_sb(sc, sc->igu_dsb_id);
18516255736Sdavidch
18517255736Sdavidch            /* !!! these should become driver const once
18518255736Sdavidch               rf-tool supports split-68 const */
18519255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
18520255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
18521255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
18522255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
18523255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
18524255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
18525255736Sdavidch        }
18526255736Sdavidch    }
18527255736Sdavidch
18528255736Sdavidch    /* Reset PCIE errors for debug */
18529255736Sdavidch    REG_WR(sc, 0x2114, 0xffffffff);
18530255736Sdavidch    REG_WR(sc, 0x2120, 0xffffffff);
18531255736Sdavidch
18532255736Sdavidch    if (CHIP_IS_E1x(sc)) {
18533255736Sdavidch        main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
18534255736Sdavidch        main_mem_base = HC_REG_MAIN_MEMORY +
18535255736Sdavidch                SC_PORT(sc) * (main_mem_size * 4);
18536255736Sdavidch        main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
18537255736Sdavidch        main_mem_width = 8;
18538255736Sdavidch
18539255736Sdavidch        val = REG_RD(sc, main_mem_prty_clr);
18540255736Sdavidch        if (val) {
18541255736Sdavidch            BLOGD(sc, DBG_LOAD,
18542255736Sdavidch                  "Parity errors in HC block during function init (0x%x)!\n",
18543255736Sdavidch                  val);
18544255736Sdavidch        }
18545255736Sdavidch
18546255736Sdavidch        /* Clear "false" parity errors in MSI-X table */
18547255736Sdavidch        for (i = main_mem_base;
18548255736Sdavidch             i < main_mem_base + main_mem_size * 4;
18549255736Sdavidch             i += main_mem_width) {
18550255736Sdavidch            bxe_read_dmae(sc, i, main_mem_width / 4);
18551255736Sdavidch            bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
18552255736Sdavidch                           i, main_mem_width / 4);
18553255736Sdavidch        }
18554255736Sdavidch        /* Clear HC parity attention */
18555255736Sdavidch        REG_RD(sc, main_mem_prty_clr);
18556255736Sdavidch    }
18557255736Sdavidch
18558255736Sdavidch#if 1
18559255736Sdavidch    /* Enable STORMs SP logging */
18560255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM +
18561255736Sdavidch           USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18562255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM +
18563255736Sdavidch           TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18564255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18565255736Sdavidch           CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18566255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM +
18567255736Sdavidch           XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18568255736Sdavidch#endif
18569255736Sdavidch
18570255736Sdavidch    elink_phy_probe(&sc->link_params);
18571255736Sdavidch
18572255736Sdavidch    return (0);
18573255736Sdavidch}
18574255736Sdavidch
18575255736Sdavidchstatic void
18576255736Sdavidchbxe_link_reset(struct bxe_softc *sc)
18577255736Sdavidch{
18578255736Sdavidch    if (!BXE_NOMCP(sc)) {
18579284335Sdavidcs	bxe_acquire_phy_lock(sc);
18580255736Sdavidch        elink_lfa_reset(&sc->link_params, &sc->link_vars);
18581284335Sdavidcs	bxe_release_phy_lock(sc);
18582255736Sdavidch    } else {
18583255736Sdavidch        if (!CHIP_REV_IS_SLOW(sc)) {
18584255736Sdavidch            BLOGW(sc, "Bootcode is missing - cannot reset link\n");
18585255736Sdavidch        }
18586255736Sdavidch    }
18587255736Sdavidch}
18588255736Sdavidch
18589255736Sdavidchstatic void
18590255736Sdavidchbxe_reset_port(struct bxe_softc *sc)
18591255736Sdavidch{
18592255736Sdavidch    int port = SC_PORT(sc);
18593255736Sdavidch    uint32_t val;
18594255736Sdavidch
18595255736Sdavidch    /* reset physical Link */
18596255736Sdavidch    bxe_link_reset(sc);
18597255736Sdavidch
18598255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18599255736Sdavidch
18600255736Sdavidch    /* Do not rcv packets to BRB */
18601255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18602255736Sdavidch    /* Do not direct rcv packets that are not for MCP to the BRB */
18603255736Sdavidch    REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18604255736Sdavidch               NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18605255736Sdavidch
18606255736Sdavidch    /* Configure AEU */
18607255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18608255736Sdavidch
18609255736Sdavidch    DELAY(100000);
18610255736Sdavidch
18611255736Sdavidch    /* Check for BRB port occupancy */
18612255736Sdavidch    val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18613255736Sdavidch    if (val) {
18614255736Sdavidch        BLOGD(sc, DBG_LOAD,
18615255736Sdavidch              "BRB1 is not empty, %d blocks are occupied\n", val);
18616255736Sdavidch    }
18617255736Sdavidch
18618255736Sdavidch    /* TODO: Close Doorbell port? */
18619255736Sdavidch}
18620255736Sdavidch
18621255736Sdavidchstatic void
18622255736Sdavidchbxe_ilt_wr(struct bxe_softc *sc,
18623255736Sdavidch           uint32_t         index,
18624255736Sdavidch           bus_addr_t       addr)
18625255736Sdavidch{
18626255736Sdavidch    int reg;
18627255736Sdavidch    uint32_t wb_write[2];
18628255736Sdavidch
18629255736Sdavidch    if (CHIP_IS_E1(sc)) {
18630255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18631255736Sdavidch    } else {
18632255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18633255736Sdavidch    }
18634255736Sdavidch
18635255736Sdavidch    wb_write[0] = ONCHIP_ADDR1(addr);
18636255736Sdavidch    wb_write[1] = ONCHIP_ADDR2(addr);
18637255736Sdavidch    REG_WR_DMAE(sc, reg, wb_write, 2);
18638255736Sdavidch}
18639255736Sdavidch
18640255736Sdavidchstatic void
18641255736Sdavidchbxe_clear_func_ilt(struct bxe_softc *sc,
18642255736Sdavidch                   uint32_t         func)
18643255736Sdavidch{
18644255736Sdavidch    uint32_t i, base = FUNC_ILT_BASE(func);
18645255736Sdavidch    for (i = base; i < base + ILT_PER_FUNC; i++) {
18646255736Sdavidch        bxe_ilt_wr(sc, i, 0);
18647255736Sdavidch    }
18648255736Sdavidch}
18649255736Sdavidch
18650255736Sdavidchstatic void
18651255736Sdavidchbxe_reset_func(struct bxe_softc *sc)
18652255736Sdavidch{
18653255736Sdavidch    struct bxe_fastpath *fp;
18654255736Sdavidch    int port = SC_PORT(sc);
18655255736Sdavidch    int func = SC_FUNC(sc);
18656255736Sdavidch    int i;
18657255736Sdavidch
18658255736Sdavidch    /* Disable the function in the FW */
18659255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18660255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18661255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18662255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18663255736Sdavidch
18664255736Sdavidch    /* FP SBs */
18665255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
18666255736Sdavidch        fp = &sc->fp[i];
18667255736Sdavidch        REG_WR8(sc, BAR_CSTRORM_INTMEM +
18668255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18669255736Sdavidch                SB_DISABLED);
18670255736Sdavidch    }
18671255736Sdavidch
18672255736Sdavidch#if 0
18673255736Sdavidch    if (CNIC_LOADED(sc)) {
18674255736Sdavidch        /* CNIC SB */
18675255736Sdavidch        REG_WR8(sc, BAR_CSTRORM_INTMEM +
18676255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET
18677255736Sdavidch                (bxe_cnic_fw_sb_id(sc)), SB_DISABLED);
18678255736Sdavidch    }
18679255736Sdavidch#endif
18680255736Sdavidch
18681255736Sdavidch    /* SP SB */
18682255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18683255736Sdavidch            CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18684255736Sdavidch            SB_DISABLED);
18685255736Sdavidch
18686255736Sdavidch    for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18687255736Sdavidch        REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18688255736Sdavidch    }
18689255736Sdavidch
18690255736Sdavidch    /* Configure IGU */
18691255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18692255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18693255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18694255736Sdavidch    } else {
18695255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18696255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18697255736Sdavidch    }
18698255736Sdavidch
18699255736Sdavidch    if (CNIC_LOADED(sc)) {
18700255736Sdavidch        /* Disable Timer scan */
18701255736Sdavidch        REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18702255736Sdavidch        /*
18703255736Sdavidch         * Wait for at least 10ms and up to 2 second for the timers
18704255736Sdavidch         * scan to complete
18705255736Sdavidch         */
18706255736Sdavidch        for (i = 0; i < 200; i++) {
18707255736Sdavidch            DELAY(10000);
18708255736Sdavidch            if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18709255736Sdavidch                break;
18710255736Sdavidch        }
18711255736Sdavidch    }
18712255736Sdavidch
18713255736Sdavidch    /* Clear ILT */
18714255736Sdavidch    bxe_clear_func_ilt(sc, func);
18715255736Sdavidch
18716255736Sdavidch    /*
18717255736Sdavidch     * Timers workaround bug for E2: if this is vnic-3,
18718255736Sdavidch     * we need to set the entire ilt range for this timers.
18719255736Sdavidch     */
18720255736Sdavidch    if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18721255736Sdavidch        struct ilt_client_info ilt_cli;
18722255736Sdavidch        /* use dummy TM client */
18723255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18724255736Sdavidch        ilt_cli.start = 0;
18725255736Sdavidch        ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18726255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
18727255736Sdavidch
18728255736Sdavidch        ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18729255736Sdavidch    }
18730255736Sdavidch
18731255736Sdavidch    /* this assumes that reset_port() called before reset_func()*/
18732255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18733255736Sdavidch        bxe_pf_disable(sc);
18734255736Sdavidch    }
18735255736Sdavidch
18736255736Sdavidch    sc->dmae_ready = 0;
18737255736Sdavidch}
18738255736Sdavidch
18739255736Sdavidchstatic int
18740255736Sdavidchbxe_gunzip_init(struct bxe_softc *sc)
18741255736Sdavidch{
18742255736Sdavidch    return (0);
18743255736Sdavidch}
18744255736Sdavidch
18745255736Sdavidchstatic void
18746255736Sdavidchbxe_gunzip_end(struct bxe_softc *sc)
18747255736Sdavidch{
18748255736Sdavidch    return;
18749255736Sdavidch}
18750255736Sdavidch
18751255736Sdavidchstatic int
18752255736Sdavidchbxe_init_firmware(struct bxe_softc *sc)
18753255736Sdavidch{
18754255736Sdavidch    if (CHIP_IS_E1(sc)) {
18755255736Sdavidch        ecore_init_e1_firmware(sc);
18756255736Sdavidch        sc->iro_array = e1_iro_arr;
18757255736Sdavidch    } else if (CHIP_IS_E1H(sc)) {
18758255736Sdavidch        ecore_init_e1h_firmware(sc);
18759255736Sdavidch        sc->iro_array = e1h_iro_arr;
18760255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
18761255736Sdavidch        ecore_init_e2_firmware(sc);
18762255736Sdavidch        sc->iro_array = e2_iro_arr;
18763255736Sdavidch    } else {
18764255736Sdavidch        BLOGE(sc, "Unsupported chip revision\n");
18765255736Sdavidch        return (-1);
18766255736Sdavidch    }
18767255736Sdavidch
18768255736Sdavidch    return (0);
18769255736Sdavidch}
18770255736Sdavidch
18771255736Sdavidchstatic void
18772255736Sdavidchbxe_release_firmware(struct bxe_softc *sc)
18773255736Sdavidch{
18774255736Sdavidch    /* Do nothing */
18775255736Sdavidch    return;
18776255736Sdavidch}
18777255736Sdavidch
18778255736Sdavidchstatic int
18779255736Sdavidchecore_gunzip(struct bxe_softc *sc,
18780255736Sdavidch             const uint8_t    *zbuf,
18781255736Sdavidch             int              len)
18782255736Sdavidch{
18783255736Sdavidch    /* XXX : Implement... */
18784255736Sdavidch    BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18785255736Sdavidch    return (FALSE);
18786255736Sdavidch}
18787255736Sdavidch
18788255736Sdavidchstatic void
18789255736Sdavidchecore_reg_wr_ind(struct bxe_softc *sc,
18790255736Sdavidch                 uint32_t         addr,
18791255736Sdavidch                 uint32_t         val)
18792255736Sdavidch{
18793255736Sdavidch    bxe_reg_wr_ind(sc, addr, val);
18794255736Sdavidch}
18795255736Sdavidch
18796255736Sdavidchstatic void
18797255736Sdavidchecore_write_dmae_phys_len(struct bxe_softc *sc,
18798255736Sdavidch                          bus_addr_t       phys_addr,
18799255736Sdavidch                          uint32_t         addr,
18800255736Sdavidch                          uint32_t         len)
18801255736Sdavidch{
18802255736Sdavidch    bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18803255736Sdavidch}
18804255736Sdavidch
18805255736Sdavidchvoid
18806255736Sdavidchecore_storm_memset_struct(struct bxe_softc *sc,
18807255736Sdavidch                          uint32_t         addr,
18808255736Sdavidch                          size_t           size,
18809255736Sdavidch                          uint32_t         *data)
18810255736Sdavidch{
18811255736Sdavidch    uint8_t i;
18812255736Sdavidch    for (i = 0; i < size/4; i++) {
18813255736Sdavidch        REG_WR(sc, addr + (i * 4), data[i]);
18814255736Sdavidch    }
18815255736Sdavidch}
18816255736Sdavidch
18817