bxe.c revision 335289
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: stable/11/sys/dev/bxe/bxe.c 335289 2018-06-17 17:28:27Z dim $");
29255736Sdavidch
30315881Sdavidcs#define BXE_DRIVER_VERSION "1.78.90"
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    {
128255736Sdavidch        BRCM_VENDORID,
129255736Sdavidch        CHIP_NUM_57800,
130255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
131265703Sdavidcs        "QLogic NetXtreme II BCM57800 10GbE"
132255736Sdavidch    },
133255736Sdavidch    {
134255736Sdavidch        BRCM_VENDORID,
135255736Sdavidch        CHIP_NUM_57800_MF,
136255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
137265703Sdavidcs        "QLogic NetXtreme II BCM57800 MF 10GbE"
138255736Sdavidch    },
139255736Sdavidch    {
140255736Sdavidch        BRCM_VENDORID,
141255736Sdavidch        CHIP_NUM_57810,
142255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
143265703Sdavidcs        "QLogic NetXtreme II BCM57810 10GbE"
144255736Sdavidch    },
145255736Sdavidch    {
146255736Sdavidch        BRCM_VENDORID,
147255736Sdavidch        CHIP_NUM_57810_MF,
148255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
149265703Sdavidcs        "QLogic NetXtreme II BCM57810 MF 10GbE"
150255736Sdavidch    },
151255736Sdavidch    {
152255736Sdavidch        BRCM_VENDORID,
153255736Sdavidch        CHIP_NUM_57811,
154255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
155265703Sdavidcs        "QLogic NetXtreme II BCM57811 10GbE"
156255736Sdavidch    },
157255736Sdavidch    {
158255736Sdavidch        BRCM_VENDORID,
159255736Sdavidch        CHIP_NUM_57811_MF,
160255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
161265703Sdavidcs        "QLogic NetXtreme II BCM57811 MF 10GbE"
162255736Sdavidch    },
163255736Sdavidch    {
164255736Sdavidch        BRCM_VENDORID,
165255736Sdavidch        CHIP_NUM_57840_4_10,
166255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
167265703Sdavidcs        "QLogic NetXtreme II BCM57840 4x10GbE"
168255736Sdavidch    },
169255736Sdavidch    {
170321517Sae        QLOGIC_VENDORID,
171321517Sae        CHIP_NUM_57840_4_10,
172321517Sae        PCI_ANY_ID, PCI_ANY_ID,
173321517Sae        "QLogic NetXtreme II BCM57840 4x10GbE"
174321517Sae    },
175321517Sae    {
176255736Sdavidch        BRCM_VENDORID,
177314093Sroyger        CHIP_NUM_57840_2_20,
178314093Sroyger        PCI_ANY_ID, PCI_ANY_ID,
179314093Sroyger        "QLogic NetXtreme II BCM57840 2x20GbE"
180314093Sroyger    },
181314093Sroyger    {
182314093Sroyger        BRCM_VENDORID,
183255736Sdavidch        CHIP_NUM_57840_MF,
184255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
185265703Sdavidcs        "QLogic NetXtreme II BCM57840 MF 10GbE"
186255736Sdavidch    },
187255736Sdavidch    {
188255736Sdavidch        0, 0, 0, 0, NULL
189255736Sdavidch    }
190255736Sdavidch};
191255736Sdavidch
192255736SdavidchMALLOC_DECLARE(M_BXE_ILT);
193255736SdavidchMALLOC_DEFINE(M_BXE_ILT, "bxe_ilt", "bxe ILT pointer");
194255736Sdavidch
195255736Sdavidch/*
196255736Sdavidch * FreeBSD device entry points.
197255736Sdavidch */
198255736Sdavidchstatic int bxe_probe(device_t);
199255736Sdavidchstatic int bxe_attach(device_t);
200255736Sdavidchstatic int bxe_detach(device_t);
201255736Sdavidchstatic int bxe_shutdown(device_t);
202255736Sdavidch
203255736Sdavidch/*
204255736Sdavidch * FreeBSD KLD module/device interface event handler method.
205255736Sdavidch */
206255736Sdavidchstatic device_method_t bxe_methods[] = {
207255736Sdavidch    /* Device interface (device_if.h) */
208255736Sdavidch    DEVMETHOD(device_probe,     bxe_probe),
209255736Sdavidch    DEVMETHOD(device_attach,    bxe_attach),
210255736Sdavidch    DEVMETHOD(device_detach,    bxe_detach),
211255736Sdavidch    DEVMETHOD(device_shutdown,  bxe_shutdown),
212255736Sdavidch    /* Bus interface (bus_if.h) */
213255736Sdavidch    DEVMETHOD(bus_print_child,  bus_generic_print_child),
214255736Sdavidch    DEVMETHOD(bus_driver_added, bus_generic_driver_added),
215255736Sdavidch    KOBJMETHOD_END
216255736Sdavidch};
217255736Sdavidch
218255736Sdavidch/*
219255736Sdavidch * FreeBSD KLD Module data declaration
220255736Sdavidch */
221255736Sdavidchstatic driver_t bxe_driver = {
222255736Sdavidch    "bxe",                   /* module name */
223255736Sdavidch    bxe_methods,             /* event handler */
224255736Sdavidch    sizeof(struct bxe_softc) /* extra data */
225255736Sdavidch};
226255736Sdavidch
227255736Sdavidch/*
228255736Sdavidch * FreeBSD dev class is needed to manage dev instances and
229255736Sdavidch * to associate with a bus type
230255736Sdavidch */
231255736Sdavidchstatic devclass_t bxe_devclass;
232255736Sdavidch
233255736SdavidchMODULE_DEPEND(bxe, pci, 1, 1, 1);
234255736SdavidchMODULE_DEPEND(bxe, ether, 1, 1, 1);
235255736SdavidchDRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
236255736Sdavidch
237255736Sdavidch/* resources needed for unloading a previously loaded device */
238255736Sdavidch
239255736Sdavidch#define BXE_PREV_WAIT_NEEDED 1
240255736Sdavidchstruct mtx bxe_prev_mtx;
241255736SdavidchMTX_SYSINIT(bxe_prev_mtx, &bxe_prev_mtx, "bxe_prev_lock", MTX_DEF);
242255736Sdavidchstruct bxe_prev_list_node {
243255736Sdavidch    LIST_ENTRY(bxe_prev_list_node) node;
244255736Sdavidch    uint8_t bus;
245255736Sdavidch    uint8_t slot;
246255736Sdavidch    uint8_t path;
247255736Sdavidch    uint8_t aer; /* XXX automatic error recovery */
248255736Sdavidch    uint8_t undi;
249255736Sdavidch};
250255736Sdavidchstatic LIST_HEAD(, bxe_prev_list_node) bxe_prev_list = LIST_HEAD_INITIALIZER(bxe_prev_list);
251255736Sdavidch
252255736Sdavidchstatic int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
253255736Sdavidch
254255736Sdavidch/* Tunable device values... */
255255736Sdavidch
256255736SdavidchSYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
257255736Sdavidch
258255736Sdavidch/* Debug */
259258187Sedavisunsigned long bxe_debug = 0;
260267992ShselaskySYSCTL_ULONG(_hw_bxe, OID_AUTO, debug, CTLFLAG_RDTUN,
261258187Sedavis             &bxe_debug, 0, "Debug logging mode");
262255736Sdavidch
263255736Sdavidch/* Interrupt Mode: 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
264255736Sdavidchstatic int bxe_interrupt_mode = INTR_MODE_MSIX;
265258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, interrupt_mode, CTLFLAG_RDTUN,
266258187Sedavis           &bxe_interrupt_mode, 0, "Interrupt (MSI-X/MSI/INTx) mode");
267255736Sdavidch
268255736Sdavidch/* Number of Queues: 0 (Auto) or 1 to 16 (fixed queue number) */
269255736Sdavidchstatic int bxe_queue_count = 4;
270258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN,
271258187Sedavis           &bxe_queue_count, 0, "Multi-Queue queue count");
272255736Sdavidch
273255736Sdavidch/* max number of buffers per queue (default RX_BD_USABLE) */
274258187Sedavisstatic int bxe_max_rx_bufs = 0;
275258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_rx_bufs, CTLFLAG_RDTUN,
276258187Sedavis           &bxe_max_rx_bufs, 0, "Maximum Number of Rx Buffers Per Queue");
277255736Sdavidch
278255736Sdavidch/* Host interrupt coalescing RX tick timer (usecs) */
279258187Sedavisstatic int bxe_hc_rx_ticks = 25;
280258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_rx_ticks, CTLFLAG_RDTUN,
281258187Sedavis           &bxe_hc_rx_ticks, 0, "Host Coalescing Rx ticks");
282255736Sdavidch
283255736Sdavidch/* Host interrupt coalescing TX tick timer (usecs) */
284258187Sedavisstatic int bxe_hc_tx_ticks = 50;
285258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_tx_ticks, CTLFLAG_RDTUN,
286258187Sedavis           &bxe_hc_tx_ticks, 0, "Host Coalescing Tx ticks");
287255736Sdavidch
288255736Sdavidch/* Maximum number of Rx packets to process at a time */
289258187Sedavisstatic int bxe_rx_budget = 0xffffffff;
290258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, rx_budget, CTLFLAG_TUN,
291258187Sedavis           &bxe_rx_budget, 0, "Rx processing budget");
292255736Sdavidch
293255736Sdavidch/* Maximum LRO aggregation size */
294258187Sedavisstatic int bxe_max_aggregation_size = 0;
295258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_aggregation_size, CTLFLAG_TUN,
296258187Sedavis           &bxe_max_aggregation_size, 0, "max aggregation size");
297255736Sdavidch
298255736Sdavidch/* PCI MRRS: -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB) */
299255736Sdavidchstatic int bxe_mrrs = -1;
300258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN,
301258187Sedavis           &bxe_mrrs, 0, "PCIe maximum read request size");
302255736Sdavidch
303255736Sdavidch/* AutoGrEEEn: 0 (hardware default), 1 (force on), 2 (force off) */
304255736Sdavidchstatic int bxe_autogreeen = 0;
305258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, autogreeen, CTLFLAG_RDTUN,
306258187Sedavis           &bxe_autogreeen, 0, "AutoGrEEEn support");
307255736Sdavidch
308255736Sdavidch/* 4-tuple RSS support for UDP: 0 (disabled), 1 (enabled) */
309255736Sdavidchstatic int bxe_udp_rss = 0;
310258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, udp_rss, CTLFLAG_RDTUN,
311258187Sedavis           &bxe_udp_rss, 0, "UDP RSS support");
312255736Sdavidch
313255736Sdavidch
314255736Sdavidch#define STAT_NAME_LEN 32 /* no stat names below can be longer than this */
315255736Sdavidch
316255736Sdavidch#define STATS_OFFSET32(stat_name)                   \
317255736Sdavidch    (offsetof(struct bxe_eth_stats, stat_name) / 4)
318255736Sdavidch
319255736Sdavidch#define Q_STATS_OFFSET32(stat_name)                   \
320255736Sdavidch    (offsetof(struct bxe_eth_q_stats, stat_name) / 4)
321255736Sdavidch
322255736Sdavidchstatic const struct {
323255736Sdavidch    uint32_t offset;
324255736Sdavidch    uint32_t size;
325255736Sdavidch    uint32_t flags;
326255736Sdavidch#define STATS_FLAGS_PORT  1
327255736Sdavidch#define STATS_FLAGS_FUNC  2 /* MF only cares about function stats */
328255736Sdavidch#define STATS_FLAGS_BOTH  (STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
329255736Sdavidch    char string[STAT_NAME_LEN];
330255736Sdavidch} bxe_eth_stats_arr[] = {
331255736Sdavidch    { STATS_OFFSET32(total_bytes_received_hi),
332255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bytes" },
333255736Sdavidch    { STATS_OFFSET32(error_bytes_received_hi),
334255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_error_bytes" },
335255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_received_hi),
336255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
337255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_received_hi),
338255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
339255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_received_hi),
340255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
341255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
342255736Sdavidch                8, STATS_FLAGS_PORT, "rx_crc_errors" },
343255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
344255736Sdavidch                8, STATS_FLAGS_PORT, "rx_align_errors" },
345255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
346255736Sdavidch                8, STATS_FLAGS_PORT, "rx_undersize_packets" },
347255736Sdavidch    { STATS_OFFSET32(etherstatsoverrsizepkts_hi),
348255736Sdavidch                8, STATS_FLAGS_PORT, "rx_oversize_packets" },
349255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
350255736Sdavidch                8, STATS_FLAGS_PORT, "rx_fragments" },
351255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
352255736Sdavidch                8, STATS_FLAGS_PORT, "rx_jabbers" },
353255736Sdavidch    { STATS_OFFSET32(no_buff_discard_hi),
354255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_discards" },
355255736Sdavidch    { STATS_OFFSET32(mac_filter_discard),
356255736Sdavidch                4, STATS_FLAGS_PORT, "rx_filtered_packets" },
357255736Sdavidch    { STATS_OFFSET32(mf_tag_discard),
358255736Sdavidch                4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
359255736Sdavidch    { STATS_OFFSET32(pfc_frames_received_hi),
360255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_received" },
361255736Sdavidch    { STATS_OFFSET32(pfc_frames_sent_hi),
362255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_sent" },
363255736Sdavidch    { STATS_OFFSET32(brb_drop_hi),
364255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_discard" },
365255736Sdavidch    { STATS_OFFSET32(brb_truncate_hi),
366255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_truncate" },
367255736Sdavidch    { STATS_OFFSET32(pause_frames_received_hi),
368255736Sdavidch                8, STATS_FLAGS_PORT, "rx_pause_frames" },
369255736Sdavidch    { STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
370255736Sdavidch                8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
371255736Sdavidch    { STATS_OFFSET32(nig_timer_max),
372255736Sdavidch                4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
373255736Sdavidch    { STATS_OFFSET32(total_bytes_transmitted_hi),
374255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bytes" },
375255736Sdavidch    { STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
376255736Sdavidch                8, STATS_FLAGS_PORT, "tx_error_bytes" },
377255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_transmitted_hi),
378255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
379255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_transmitted_hi),
380255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
381255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
382255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
383255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
384255736Sdavidch                8, STATS_FLAGS_PORT, "tx_mac_errors" },
385255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
386255736Sdavidch                8, STATS_FLAGS_PORT, "tx_carrier_errors" },
387255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
388255736Sdavidch                8, STATS_FLAGS_PORT, "tx_single_collisions" },
389255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
390255736Sdavidch                8, STATS_FLAGS_PORT, "tx_multi_collisions" },
391255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
392255736Sdavidch                8, STATS_FLAGS_PORT, "tx_deferred" },
393255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
394255736Sdavidch                8, STATS_FLAGS_PORT, "tx_excess_collisions" },
395255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
396255736Sdavidch                8, STATS_FLAGS_PORT, "tx_late_collisions" },
397255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
398255736Sdavidch                8, STATS_FLAGS_PORT, "tx_total_collisions" },
399255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
400255736Sdavidch                8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
401255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
402255736Sdavidch                8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
403255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
404255736Sdavidch                8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
405255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
406255736Sdavidch                8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
407255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
408255736Sdavidch                8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
409255736Sdavidch    { STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
410255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
411255736Sdavidch    { STATS_OFFSET32(etherstatspktsover1522octets_hi),
412255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
413255736Sdavidch    { STATS_OFFSET32(pause_frames_sent_hi),
414255736Sdavidch                8, STATS_FLAGS_PORT, "tx_pause_frames" },
415255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregations_hi),
416255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregations" },
417255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregated_frames_hi),
418255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
419255736Sdavidch    { STATS_OFFSET32(total_tpa_bytes_hi),
420255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_bytes"},
421255736Sdavidch    { STATS_OFFSET32(eee_tx_lpi),
422255736Sdavidch                4, STATS_FLAGS_PORT, "eee_tx_lpi"},
423255736Sdavidch    { STATS_OFFSET32(rx_calls),
424255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_calls"},
425255736Sdavidch    { STATS_OFFSET32(rx_pkts),
426255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_pkts"},
427255736Sdavidch    { STATS_OFFSET32(rx_tpa_pkts),
428255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_tpa_pkts"},
429292638Sdavidcs    { STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
430292638Sdavidcs                4, STATS_FLAGS_FUNC, "rx_erroneous_jumbo_sge_pkts"},
431292638Sdavidcs    { STATS_OFFSET32(rx_bxe_service_rxsgl),
432292638Sdavidcs                4, STATS_FLAGS_FUNC, "rx_bxe_service_rxsgl"},
433283274Sdavidcs    { STATS_OFFSET32(rx_jumbo_sge_pkts),
434283274Sdavidcs                4, STATS_FLAGS_FUNC, "rx_jumbo_sge_pkts"},
435255736Sdavidch    { STATS_OFFSET32(rx_soft_errors),
436255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_soft_errors"},
437255736Sdavidch    { STATS_OFFSET32(rx_hw_csum_errors),
438255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_hw_csum_errors"},
439255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_ip),
440255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_ip"},
441255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
442255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_tcp_udp"},
443255736Sdavidch    { STATS_OFFSET32(rx_budget_reached),
444255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_budget_reached"},
445255736Sdavidch    { STATS_OFFSET32(tx_pkts),
446255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_pkts"},
447255736Sdavidch    { STATS_OFFSET32(tx_soft_errors),
448255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_soft_errors"},
449255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_ip),
450255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_ip"},
451255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_tcp),
452255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_tcp"},
453255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_udp),
454255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_udp"},
455255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso),
456255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso"},
457255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
458255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso_hdr_splits"},
459255736Sdavidch    { STATS_OFFSET32(tx_encap_failures),
460255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_encap_failures"},
461255736Sdavidch    { STATS_OFFSET32(tx_hw_queue_full),
462255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_queue_full"},
463255736Sdavidch    { STATS_OFFSET32(tx_hw_max_queue_depth),
464255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_max_queue_depth"},
465255736Sdavidch    { STATS_OFFSET32(tx_dma_mapping_failure),
466255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_dma_mapping_failure"},
467255736Sdavidch    { STATS_OFFSET32(tx_max_drbr_queue_depth),
468255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_max_drbr_queue_depth"},
469255736Sdavidch    { STATS_OFFSET32(tx_window_violation_std),
470255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_std"},
471255736Sdavidch    { STATS_OFFSET32(tx_window_violation_tso),
472255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_tso"},
473255736Sdavidch    { STATS_OFFSET32(tx_chain_lost_mbuf),
474255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_chain_lost_mbuf"},
475255736Sdavidch    { STATS_OFFSET32(tx_frames_deferred),
476255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_frames_deferred"},
477255736Sdavidch    { STATS_OFFSET32(tx_queue_xoff),
478255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_queue_xoff"},
479255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_attempts),
480255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_attempts"},
481255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_failures),
482255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_failures"},
483255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
484255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_alloc_failed"},
485255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
486255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_mapping_failed"},
487255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
488255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_alloc_failed"},
489255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
490255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_mapping_failed"},
491255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
492255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_alloc_failed"},
493255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
494255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_mapping_failed"},
495255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tx),
496255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_tx"},
497255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_rx),
498255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_rx"},
499255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_sge),
500255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_sge"},
501255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tpa),
502297873Sdavidcs                4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"},
503297873Sdavidcs    { STATS_OFFSET32(tx_queue_full_return),
504307972Sdavidcs                4, STATS_FLAGS_FUNC, "tx_queue_full_return"},
505315881Sdavidcs    { STATS_OFFSET32(bxe_tx_mq_sc_state_failures),
506315881Sdavidcs                4, STATS_FLAGS_FUNC, "bxe_tx_mq_sc_state_failures"},
507307972Sdavidcs    { STATS_OFFSET32(tx_request_link_down_failures),
508307972Sdavidcs                4, STATS_FLAGS_FUNC, "tx_request_link_down_failures"},
509307972Sdavidcs    { STATS_OFFSET32(bd_avail_too_less_failures),
510307972Sdavidcs                4, STATS_FLAGS_FUNC, "bd_avail_too_less_failures"},
511307972Sdavidcs    { STATS_OFFSET32(tx_mq_not_empty),
512315881Sdavidcs                4, STATS_FLAGS_FUNC, "tx_mq_not_empty"},
513315881Sdavidcs    { STATS_OFFSET32(nsegs_path1_errors),
514315881Sdavidcs                4, STATS_FLAGS_FUNC, "nsegs_path1_errors"},
515315881Sdavidcs    { STATS_OFFSET32(nsegs_path2_errors),
516315881Sdavidcs                4, STATS_FLAGS_FUNC, "nsegs_path2_errors"}
517307972Sdavidcs
518315881Sdavidcs
519255736Sdavidch};
520255736Sdavidch
521255736Sdavidchstatic const struct {
522255736Sdavidch    uint32_t offset;
523255736Sdavidch    uint32_t size;
524255736Sdavidch    char string[STAT_NAME_LEN];
525255736Sdavidch} bxe_eth_q_stats_arr[] = {
526255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_received_hi),
527255736Sdavidch                8, "rx_bytes" },
528255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
529255736Sdavidch                8, "rx_ucast_packets" },
530255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
531255736Sdavidch                8, "rx_mcast_packets" },
532255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
533255736Sdavidch                8, "rx_bcast_packets" },
534255736Sdavidch    { Q_STATS_OFFSET32(no_buff_discard_hi),
535255736Sdavidch                8, "rx_discards" },
536255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
537255736Sdavidch                8, "tx_bytes" },
538255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
539255736Sdavidch                8, "tx_ucast_packets" },
540255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
541255736Sdavidch                8, "tx_mcast_packets" },
542255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
543255736Sdavidch                8, "tx_bcast_packets" },
544255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
545255736Sdavidch                8, "tpa_aggregations" },
546255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
547255736Sdavidch                8, "tpa_aggregated_frames"},
548255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_bytes_hi),
549255736Sdavidch                8, "tpa_bytes"},
550255736Sdavidch    { Q_STATS_OFFSET32(rx_calls),
551255736Sdavidch                4, "rx_calls"},
552255736Sdavidch    { Q_STATS_OFFSET32(rx_pkts),
553255736Sdavidch                4, "rx_pkts"},
554255736Sdavidch    { Q_STATS_OFFSET32(rx_tpa_pkts),
555255736Sdavidch                4, "rx_tpa_pkts"},
556292638Sdavidcs    { Q_STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
557292638Sdavidcs                4, "rx_erroneous_jumbo_sge_pkts"},
558292638Sdavidcs    { Q_STATS_OFFSET32(rx_bxe_service_rxsgl),
559292638Sdavidcs                4, "rx_bxe_service_rxsgl"},
560283274Sdavidcs    { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
561283274Sdavidcs                4, "rx_jumbo_sge_pkts"},
562255736Sdavidch    { Q_STATS_OFFSET32(rx_soft_errors),
563255736Sdavidch                4, "rx_soft_errors"},
564255736Sdavidch    { Q_STATS_OFFSET32(rx_hw_csum_errors),
565255736Sdavidch                4, "rx_hw_csum_errors"},
566255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
567255736Sdavidch                4, "rx_ofld_frames_csum_ip"},
568255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
569255736Sdavidch                4, "rx_ofld_frames_csum_tcp_udp"},
570255736Sdavidch    { Q_STATS_OFFSET32(rx_budget_reached),
571255736Sdavidch                4, "rx_budget_reached"},
572255736Sdavidch    { Q_STATS_OFFSET32(tx_pkts),
573255736Sdavidch                4, "tx_pkts"},
574255736Sdavidch    { Q_STATS_OFFSET32(tx_soft_errors),
575255736Sdavidch                4, "tx_soft_errors"},
576255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
577255736Sdavidch                4, "tx_ofld_frames_csum_ip"},
578255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
579255736Sdavidch                4, "tx_ofld_frames_csum_tcp"},
580255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
581255736Sdavidch                4, "tx_ofld_frames_csum_udp"},
582255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso),
583255736Sdavidch                4, "tx_ofld_frames_lso"},
584255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
585255736Sdavidch                4, "tx_ofld_frames_lso_hdr_splits"},
586255736Sdavidch    { Q_STATS_OFFSET32(tx_encap_failures),
587255736Sdavidch                4, "tx_encap_failures"},
588255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_queue_full),
589255736Sdavidch                4, "tx_hw_queue_full"},
590255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
591255736Sdavidch                4, "tx_hw_max_queue_depth"},
592255736Sdavidch    { Q_STATS_OFFSET32(tx_dma_mapping_failure),
593255736Sdavidch                4, "tx_dma_mapping_failure"},
594255736Sdavidch    { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
595255736Sdavidch                4, "tx_max_drbr_queue_depth"},
596255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_std),
597255736Sdavidch                4, "tx_window_violation_std"},
598255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_tso),
599255736Sdavidch                4, "tx_window_violation_tso"},
600255736Sdavidch    { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
601255736Sdavidch                4, "tx_chain_lost_mbuf"},
602255736Sdavidch    { Q_STATS_OFFSET32(tx_frames_deferred),
603255736Sdavidch                4, "tx_frames_deferred"},
604255736Sdavidch    { Q_STATS_OFFSET32(tx_queue_xoff),
605255736Sdavidch                4, "tx_queue_xoff"},
606255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_attempts),
607255736Sdavidch                4, "mbuf_defrag_attempts"},
608255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_failures),
609255736Sdavidch                4, "mbuf_defrag_failures"},
610255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
611255736Sdavidch                4, "mbuf_rx_bd_alloc_failed"},
612255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
613255736Sdavidch                4, "mbuf_rx_bd_mapping_failed"},
614255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
615255736Sdavidch                4, "mbuf_rx_tpa_alloc_failed"},
616255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
617255736Sdavidch                4, "mbuf_rx_tpa_mapping_failed"},
618255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
619255736Sdavidch                4, "mbuf_rx_sge_alloc_failed"},
620255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
621255736Sdavidch                4, "mbuf_rx_sge_mapping_failed"},
622255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tx),
623255736Sdavidch                4, "mbuf_alloc_tx"},
624255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_rx),
625255736Sdavidch                4, "mbuf_alloc_rx"},
626255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_sge),
627255736Sdavidch                4, "mbuf_alloc_sge"},
628255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tpa),
629297873Sdavidcs                4, "mbuf_alloc_tpa"},
630297873Sdavidcs    { Q_STATS_OFFSET32(tx_queue_full_return),
631307972Sdavidcs                4, "tx_queue_full_return"},
632315881Sdavidcs    { Q_STATS_OFFSET32(bxe_tx_mq_sc_state_failures),
633315881Sdavidcs                4, "bxe_tx_mq_sc_state_failures"},
634307972Sdavidcs    { Q_STATS_OFFSET32(tx_request_link_down_failures),
635307972Sdavidcs                4, "tx_request_link_down_failures"},
636307972Sdavidcs    { Q_STATS_OFFSET32(bd_avail_too_less_failures),
637307972Sdavidcs                4, "bd_avail_too_less_failures"},
638307972Sdavidcs    { Q_STATS_OFFSET32(tx_mq_not_empty),
639315881Sdavidcs                4, "tx_mq_not_empty"},
640315881Sdavidcs    { Q_STATS_OFFSET32(nsegs_path1_errors),
641315881Sdavidcs                4, "nsegs_path1_errors"},
642315881Sdavidcs    { Q_STATS_OFFSET32(nsegs_path2_errors),
643315881Sdavidcs                4, "nsegs_path2_errors"}
644307972Sdavidcs
645315881Sdavidcs
646255736Sdavidch};
647255736Sdavidch
648255736Sdavidch#define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
649255736Sdavidch#define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
650255736Sdavidch
651255736Sdavidch
652255736Sdavidchstatic void    bxe_cmng_fns_init(struct bxe_softc *sc,
653255736Sdavidch                                 uint8_t          read_cfg,
654255736Sdavidch                                 uint8_t          cmng_type);
655255736Sdavidchstatic int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
656255736Sdavidchstatic void    storm_memset_cmng(struct bxe_softc *sc,
657255736Sdavidch                                 struct cmng_init *cmng,
658255736Sdavidch                                 uint8_t          port);
659255736Sdavidchstatic void    bxe_set_reset_global(struct bxe_softc *sc);
660255736Sdavidchstatic void    bxe_set_reset_in_progress(struct bxe_softc *sc);
661255736Sdavidchstatic uint8_t bxe_reset_is_done(struct bxe_softc *sc,
662255736Sdavidch                                 int              engine);
663255736Sdavidchstatic uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
664255736Sdavidchstatic uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
665255736Sdavidch                                   uint8_t          *global,
666255736Sdavidch                                   uint8_t          print);
667255736Sdavidchstatic void    bxe_int_disable(struct bxe_softc *sc);
668255736Sdavidchstatic int     bxe_release_leader_lock(struct bxe_softc *sc);
669255736Sdavidchstatic void    bxe_pf_disable(struct bxe_softc *sc);
670255736Sdavidchstatic void    bxe_free_fp_buffers(struct bxe_softc *sc);
671255736Sdavidchstatic inline void bxe_update_rx_prod(struct bxe_softc    *sc,
672255736Sdavidch                                      struct bxe_fastpath *fp,
673255736Sdavidch                                      uint16_t            rx_bd_prod,
674255736Sdavidch                                      uint16_t            rx_cq_prod,
675255736Sdavidch                                      uint16_t            rx_sge_prod);
676255736Sdavidchstatic void    bxe_link_report_locked(struct bxe_softc *sc);
677255736Sdavidchstatic void    bxe_link_report(struct bxe_softc *sc);
678255736Sdavidchstatic void    bxe_link_status_update(struct bxe_softc *sc);
679255736Sdavidchstatic void    bxe_periodic_callout_func(void *xsc);
680255736Sdavidchstatic void    bxe_periodic_start(struct bxe_softc *sc);
681255736Sdavidchstatic void    bxe_periodic_stop(struct bxe_softc *sc);
682255736Sdavidchstatic int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
683255736Sdavidch                                    uint16_t prev_index,
684255736Sdavidch                                    uint16_t index);
685255736Sdavidchstatic int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
686255736Sdavidch                                     int                 queue);
687255736Sdavidchstatic int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
688255736Sdavidch                                     uint16_t            index);
689255736Sdavidchstatic uint8_t bxe_txeof(struct bxe_softc *sc,
690255736Sdavidch                         struct bxe_fastpath *fp);
691255736Sdavidchstatic void    bxe_task_fp(struct bxe_fastpath *fp);
692255736Sdavidchstatic __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
693255736Sdavidch                                     struct mbuf      *m,
694255736Sdavidch                                     uint8_t          contents);
695255736Sdavidchstatic int     bxe_alloc_mem(struct bxe_softc *sc);
696255736Sdavidchstatic void    bxe_free_mem(struct bxe_softc *sc);
697255736Sdavidchstatic int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
698255736Sdavidchstatic void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
699255736Sdavidchstatic int     bxe_interrupt_attach(struct bxe_softc *sc);
700255736Sdavidchstatic void    bxe_interrupt_detach(struct bxe_softc *sc);
701255736Sdavidchstatic void    bxe_set_rx_mode(struct bxe_softc *sc);
702255736Sdavidchstatic int     bxe_init_locked(struct bxe_softc *sc);
703255736Sdavidchstatic int     bxe_stop_locked(struct bxe_softc *sc);
704255736Sdavidchstatic __noinline int bxe_nic_load(struct bxe_softc *sc,
705255736Sdavidch                                   int              load_mode);
706255736Sdavidchstatic __noinline int bxe_nic_unload(struct bxe_softc *sc,
707255736Sdavidch                                     uint32_t         unload_mode,
708255736Sdavidch                                     uint8_t          keep_link);
709255736Sdavidch
710255736Sdavidchstatic void bxe_handle_sp_tq(void *context, int pending);
711255736Sdavidchstatic void bxe_handle_fp_tq(void *context, int pending);
712255736Sdavidch
713292639Sdavidcsstatic int bxe_add_cdev(struct bxe_softc *sc);
714292639Sdavidcsstatic void bxe_del_cdev(struct bxe_softc *sc);
715315881Sdavidcsint bxe_grc_dump(struct bxe_softc *sc);
716296579Sdavidcsstatic int bxe_alloc_buf_rings(struct bxe_softc *sc);
717296579Sdavidcsstatic void bxe_free_buf_rings(struct bxe_softc *sc);
718255736Sdavidch
719255736Sdavidch/* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
720255736Sdavidchuint32_t
721255736Sdavidchcalc_crc32(uint8_t  *crc32_packet,
722255736Sdavidch           uint32_t crc32_length,
723255736Sdavidch           uint32_t crc32_seed,
724255736Sdavidch           uint8_t  complement)
725255736Sdavidch{
726255736Sdavidch   uint32_t byte         = 0;
727255736Sdavidch   uint32_t bit          = 0;
728255736Sdavidch   uint8_t  msb          = 0;
729255736Sdavidch   uint32_t temp         = 0;
730255736Sdavidch   uint32_t shft         = 0;
731255736Sdavidch   uint8_t  current_byte = 0;
732255736Sdavidch   uint32_t crc32_result = crc32_seed;
733255736Sdavidch   const uint32_t CRC32_POLY = 0x1edc6f41;
734255736Sdavidch
735255736Sdavidch   if ((crc32_packet == NULL) ||
736255736Sdavidch       (crc32_length == 0) ||
737255736Sdavidch       ((crc32_length % 8) != 0))
738255736Sdavidch    {
739255736Sdavidch        return (crc32_result);
740255736Sdavidch    }
741255736Sdavidch
742255736Sdavidch    for (byte = 0; byte < crc32_length; byte = byte + 1)
743255736Sdavidch    {
744255736Sdavidch        current_byte = crc32_packet[byte];
745255736Sdavidch        for (bit = 0; bit < 8; bit = bit + 1)
746255736Sdavidch        {
747255736Sdavidch            /* msb = crc32_result[31]; */
748255736Sdavidch            msb = (uint8_t)(crc32_result >> 31);
749255736Sdavidch
750255736Sdavidch            crc32_result = crc32_result << 1;
751255736Sdavidch
752255736Sdavidch            /* it (msb != current_byte[bit]) */
753255736Sdavidch            if (msb != (0x1 & (current_byte >> bit)))
754255736Sdavidch            {
755255736Sdavidch                crc32_result = crc32_result ^ CRC32_POLY;
756255736Sdavidch                /* crc32_result[0] = 1 */
757255736Sdavidch                crc32_result |= 1;
758255736Sdavidch            }
759255736Sdavidch        }
760255736Sdavidch    }
761255736Sdavidch
762255736Sdavidch    /* Last step is to:
763255736Sdavidch     * 1. "mirror" every bit
764255736Sdavidch     * 2. swap the 4 bytes
765255736Sdavidch     * 3. complement each bit
766255736Sdavidch     */
767255736Sdavidch
768255736Sdavidch    /* Mirror */
769255736Sdavidch    temp = crc32_result;
770255736Sdavidch    shft = sizeof(crc32_result) * 8 - 1;
771255736Sdavidch
772255736Sdavidch    for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
773255736Sdavidch    {
774255736Sdavidch        temp <<= 1;
775255736Sdavidch        temp |= crc32_result & 1;
776255736Sdavidch        shft-- ;
777255736Sdavidch    }
778255736Sdavidch
779255736Sdavidch    /* temp[31-bit] = crc32_result[bit] */
780255736Sdavidch    temp <<= shft;
781255736Sdavidch
782255736Sdavidch    /* Swap */
783255736Sdavidch    /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
784255736Sdavidch    {
785255736Sdavidch        uint32_t t0, t1, t2, t3;
786255736Sdavidch        t0 = (0x000000ff & (temp >> 24));
787255736Sdavidch        t1 = (0x0000ff00 & (temp >> 8));
788255736Sdavidch        t2 = (0x00ff0000 & (temp << 8));
789255736Sdavidch        t3 = (0xff000000 & (temp << 24));
790255736Sdavidch        crc32_result = t0 | t1 | t2 | t3;
791255736Sdavidch    }
792255736Sdavidch
793255736Sdavidch    /* Complement */
794255736Sdavidch    if (complement)
795255736Sdavidch    {
796255736Sdavidch        crc32_result = ~crc32_result;
797255736Sdavidch    }
798255736Sdavidch
799255736Sdavidch    return (crc32_result);
800255736Sdavidch}
801255736Sdavidch
802255736Sdavidchint
803255736Sdavidchbxe_test_bit(int                    nr,
804255736Sdavidch             volatile unsigned long *addr)
805255736Sdavidch{
806255736Sdavidch    return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
807255736Sdavidch}
808255736Sdavidch
809255736Sdavidchvoid
810255736Sdavidchbxe_set_bit(unsigned int           nr,
811255736Sdavidch            volatile unsigned long *addr)
812255736Sdavidch{
813255736Sdavidch    atomic_set_acq_long(addr, (1 << nr));
814255736Sdavidch}
815255736Sdavidch
816255736Sdavidchvoid
817255736Sdavidchbxe_clear_bit(int                    nr,
818255736Sdavidch              volatile unsigned long *addr)
819255736Sdavidch{
820255736Sdavidch    atomic_clear_acq_long(addr, (1 << nr));
821255736Sdavidch}
822255736Sdavidch
823255736Sdavidchint
824255736Sdavidchbxe_test_and_set_bit(int                    nr,
825255736Sdavidch                       volatile unsigned long *addr)
826255736Sdavidch{
827255736Sdavidch    unsigned long x;
828255736Sdavidch    nr = (1 << nr);
829255736Sdavidch    do {
830255736Sdavidch        x = *addr;
831255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
832255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
833255736Sdavidch    return (x & nr);
834255736Sdavidch}
835255736Sdavidch
836255736Sdavidchint
837255736Sdavidchbxe_test_and_clear_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_cmpxchg(volatile int *addr,
851255736Sdavidch            int          old,
852255736Sdavidch            int          new)
853255736Sdavidch{
854255736Sdavidch    int x;
855255736Sdavidch    do {
856255736Sdavidch        x = *addr;
857255736Sdavidch    } while (atomic_cmpset_acq_int(addr, old, new) == 0);
858255736Sdavidch    return (x);
859255736Sdavidch}
860255736Sdavidch
861255736Sdavidch/*
862255736Sdavidch * Get DMA memory from the OS.
863255736Sdavidch *
864255736Sdavidch * Validates that the OS has provided DMA buffers in response to a
865255736Sdavidch * bus_dmamap_load call and saves the physical address of those buffers.
866255736Sdavidch * When the callback is used the OS will return 0 for the mapping function
867255736Sdavidch * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
868255736Sdavidch * failures back to the caller.
869255736Sdavidch *
870255736Sdavidch * Returns:
871255736Sdavidch *   Nothing.
872255736Sdavidch */
873255736Sdavidchstatic void
874255736Sdavidchbxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
875255736Sdavidch{
876255736Sdavidch    struct bxe_dma *dma = arg;
877255736Sdavidch
878255736Sdavidch    if (error) {
879255736Sdavidch        dma->paddr = 0;
880255736Sdavidch        dma->nseg  = 0;
881255736Sdavidch        BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
882255736Sdavidch    } else {
883255736Sdavidch        dma->paddr = segs->ds_addr;
884255736Sdavidch        dma->nseg  = nseg;
885255736Sdavidch    }
886255736Sdavidch}
887255736Sdavidch
888255736Sdavidch/*
889255736Sdavidch * Allocate a block of memory and map it for DMA. No partial completions
890255736Sdavidch * allowed and release any resources acquired if we can't acquire all
891255736Sdavidch * resources.
892255736Sdavidch *
893255736Sdavidch * Returns:
894255736Sdavidch *   0 = Success, !0 = Failure
895255736Sdavidch */
896255736Sdavidchint
897255736Sdavidchbxe_dma_alloc(struct bxe_softc *sc,
898255736Sdavidch              bus_size_t       size,
899255736Sdavidch              struct bxe_dma   *dma,
900255736Sdavidch              const char       *msg)
901255736Sdavidch{
902255736Sdavidch    int rc;
903255736Sdavidch
904255736Sdavidch    if (dma->size > 0) {
905256299Sedavis        BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
906256299Sedavis              (unsigned long)dma->size);
907255736Sdavidch        return (1);
908255736Sdavidch    }
909255736Sdavidch
910255736Sdavidch    memset(dma, 0, sizeof(*dma)); /* sanity */
911255736Sdavidch    dma->sc   = sc;
912255736Sdavidch    dma->size = size;
913255736Sdavidch    snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
914255736Sdavidch
915255736Sdavidch    rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
916255736Sdavidch                            BCM_PAGE_SIZE,      /* alignment */
917255736Sdavidch                            0,                  /* boundary limit */
918255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted low */
919255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted hi */
920255736Sdavidch                            NULL,               /* addr filter() */
921255736Sdavidch                            NULL,               /* addr filter() arg */
922255736Sdavidch                            size,               /* max map size */
923255736Sdavidch                            1,                  /* num discontinuous */
924255736Sdavidch                            size,               /* max seg size */
925255736Sdavidch                            BUS_DMA_ALLOCNOW,   /* flags */
926255736Sdavidch                            NULL,               /* lock() */
927255736Sdavidch                            NULL,               /* lock() arg */
928255736Sdavidch                            &dma->tag);         /* returned dma tag */
929255736Sdavidch    if (rc != 0) {
930255736Sdavidch        BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
931255736Sdavidch        memset(dma, 0, sizeof(*dma));
932255736Sdavidch        return (1);
933255736Sdavidch    }
934255736Sdavidch
935255736Sdavidch    rc = bus_dmamem_alloc(dma->tag,
936255736Sdavidch                          (void **)&dma->vaddr,
937255736Sdavidch                          (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
938255736Sdavidch                          &dma->map);
939255736Sdavidch    if (rc != 0) {
940255736Sdavidch        BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
941255736Sdavidch        bus_dma_tag_destroy(dma->tag);
942255736Sdavidch        memset(dma, 0, sizeof(*dma));
943255736Sdavidch        return (1);
944255736Sdavidch    }
945255736Sdavidch
946255736Sdavidch    rc = bus_dmamap_load(dma->tag,
947255736Sdavidch                         dma->map,
948255736Sdavidch                         dma->vaddr,
949255736Sdavidch                         size,
950255736Sdavidch                         bxe_dma_map_addr, /* BLOGD in here */
951255736Sdavidch                         dma,
952255736Sdavidch                         BUS_DMA_NOWAIT);
953255736Sdavidch    if (rc != 0) {
954255736Sdavidch        BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
955255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
956255736Sdavidch        bus_dma_tag_destroy(dma->tag);
957255736Sdavidch        memset(dma, 0, sizeof(*dma));
958255736Sdavidch        return (1);
959255736Sdavidch    }
960255736Sdavidch
961255736Sdavidch    return (0);
962255736Sdavidch}
963255736Sdavidch
964255736Sdavidchvoid
965255736Sdavidchbxe_dma_free(struct bxe_softc *sc,
966255736Sdavidch             struct bxe_dma   *dma)
967255736Sdavidch{
968255736Sdavidch    if (dma->size > 0) {
969255736Sdavidch        DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
970255736Sdavidch
971255736Sdavidch        bus_dmamap_sync(dma->tag, dma->map,
972255736Sdavidch                        (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
973255736Sdavidch        bus_dmamap_unload(dma->tag, dma->map);
974255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
975255736Sdavidch        bus_dma_tag_destroy(dma->tag);
976255736Sdavidch    }
977255736Sdavidch
978255736Sdavidch    memset(dma, 0, sizeof(*dma));
979255736Sdavidch}
980255736Sdavidch
981255736Sdavidch/*
982255736Sdavidch * These indirect read and write routines are only during init.
983255736Sdavidch * The locking is handled by the MCP.
984255736Sdavidch */
985255736Sdavidch
986255736Sdavidchvoid
987255736Sdavidchbxe_reg_wr_ind(struct bxe_softc *sc,
988255736Sdavidch               uint32_t         addr,
989255736Sdavidch               uint32_t         val)
990255736Sdavidch{
991255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
992255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
993255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
994255736Sdavidch}
995255736Sdavidch
996255736Sdavidchuint32_t
997255736Sdavidchbxe_reg_rd_ind(struct bxe_softc *sc,
998255736Sdavidch               uint32_t         addr)
999255736Sdavidch{
1000255736Sdavidch    uint32_t val;
1001255736Sdavidch
1002255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
1003255736Sdavidch    val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
1004255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
1005255736Sdavidch
1006255736Sdavidch    return (val);
1007255736Sdavidch}
1008255736Sdavidch
1009255736Sdavidchstatic int
1010255736Sdavidchbxe_acquire_hw_lock(struct bxe_softc *sc,
1011255736Sdavidch                    uint32_t         resource)
1012255736Sdavidch{
1013255736Sdavidch    uint32_t lock_status;
1014255736Sdavidch    uint32_t resource_bit = (1 << resource);
1015255736Sdavidch    int func = SC_FUNC(sc);
1016255736Sdavidch    uint32_t hw_lock_control_reg;
1017255736Sdavidch    int cnt;
1018255736Sdavidch
1019255736Sdavidch    /* validate the resource is within range */
1020255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1021295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1022295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
1023255736Sdavidch        return (-1);
1024255736Sdavidch    }
1025255736Sdavidch
1026255736Sdavidch    if (func <= 5) {
1027255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1028255736Sdavidch    } else {
1029255736Sdavidch        hw_lock_control_reg =
1030255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1031255736Sdavidch    }
1032255736Sdavidch
1033255736Sdavidch    /* validate the resource is not already taken */
1034255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1035255736Sdavidch    if (lock_status & resource_bit) {
1036295830Sdavidcs        BLOGE(sc, "resource (0x%x) in use (status 0x%x bit 0x%x)\n",
1037295830Sdavidcs              resource, lock_status, resource_bit);
1038255736Sdavidch        return (-1);
1039255736Sdavidch    }
1040255736Sdavidch
1041255736Sdavidch    /* try every 5ms for 5 seconds */
1042255736Sdavidch    for (cnt = 0; cnt < 1000; cnt++) {
1043255736Sdavidch        REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1044255736Sdavidch        lock_status = REG_RD(sc, hw_lock_control_reg);
1045255736Sdavidch        if (lock_status & resource_bit) {
1046255736Sdavidch            return (0);
1047255736Sdavidch        }
1048255736Sdavidch        DELAY(5000);
1049255736Sdavidch    }
1050255736Sdavidch
1051295830Sdavidcs    BLOGE(sc, "Resource 0x%x resource_bit 0x%x lock timeout!\n",
1052295830Sdavidcs        resource, resource_bit);
1053255736Sdavidch    return (-1);
1054255736Sdavidch}
1055255736Sdavidch
1056255736Sdavidchstatic int
1057255736Sdavidchbxe_release_hw_lock(struct bxe_softc *sc,
1058255736Sdavidch                    uint32_t         resource)
1059255736Sdavidch{
1060255736Sdavidch    uint32_t lock_status;
1061255736Sdavidch    uint32_t resource_bit = (1 << resource);
1062255736Sdavidch    int func = SC_FUNC(sc);
1063255736Sdavidch    uint32_t hw_lock_control_reg;
1064255736Sdavidch
1065255736Sdavidch    /* validate the resource is within range */
1066255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1067295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1068295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
1069255736Sdavidch        return (-1);
1070255736Sdavidch    }
1071255736Sdavidch
1072255736Sdavidch    if (func <= 5) {
1073255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1074255736Sdavidch    } else {
1075255736Sdavidch        hw_lock_control_reg =
1076255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1077255736Sdavidch    }
1078255736Sdavidch
1079255736Sdavidch    /* validate the resource is currently taken */
1080255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1081255736Sdavidch    if (!(lock_status & resource_bit)) {
1082295830Sdavidcs        BLOGE(sc, "resource (0x%x) not in use (status 0x%x bit 0x%x)\n",
1083295830Sdavidcs              resource, lock_status, resource_bit);
1084255736Sdavidch        return (-1);
1085255736Sdavidch    }
1086255736Sdavidch
1087255736Sdavidch    REG_WR(sc, hw_lock_control_reg, resource_bit);
1088255736Sdavidch    return (0);
1089255736Sdavidch}
1090284335Sdavidcsstatic void bxe_acquire_phy_lock(struct bxe_softc *sc)
1091284335Sdavidcs{
1092284335Sdavidcs	BXE_PHY_LOCK(sc);
1093284335Sdavidcs	bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1094284335Sdavidcs}
1095255736Sdavidch
1096284335Sdavidcsstatic void bxe_release_phy_lock(struct bxe_softc *sc)
1097284335Sdavidcs{
1098284335Sdavidcs	bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1099284335Sdavidcs	BXE_PHY_UNLOCK(sc);
1100284335Sdavidcs}
1101255736Sdavidch/*
1102255736Sdavidch * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1103255736Sdavidch * had we done things the other way around, if two pfs from the same port
1104255736Sdavidch * would attempt to access nvram at the same time, we could run into a
1105255736Sdavidch * scenario such as:
1106255736Sdavidch * pf A takes the port lock.
1107255736Sdavidch * pf B succeeds in taking the same lock since they are from the same port.
1108255736Sdavidch * pf A takes the per pf misc lock. Performs eeprom access.
1109255736Sdavidch * pf A finishes. Unlocks the per pf misc lock.
1110255736Sdavidch * Pf B takes the lock and proceeds to perform it's own access.
1111255736Sdavidch * pf A unlocks the per port lock, while pf B is still working (!).
1112255736Sdavidch * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1113255736Sdavidch * access corrupted by pf B).*
1114255736Sdavidch */
1115255736Sdavidchstatic int
1116255736Sdavidchbxe_acquire_nvram_lock(struct bxe_softc *sc)
1117255736Sdavidch{
1118255736Sdavidch    int port = SC_PORT(sc);
1119255736Sdavidch    int count, i;
1120255736Sdavidch    uint32_t val = 0;
1121255736Sdavidch
1122255736Sdavidch    /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1123255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1124255736Sdavidch
1125255736Sdavidch    /* adjust timeout for emulation/FPGA */
1126255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1127255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1128255736Sdavidch        count *= 100;
1129255736Sdavidch    }
1130255736Sdavidch
1131255736Sdavidch    /* request access to nvram interface */
1132255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1133255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1134255736Sdavidch
1135255736Sdavidch    for (i = 0; i < count*10; i++) {
1136255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1137255736Sdavidch        if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1138255736Sdavidch            break;
1139255736Sdavidch        }
1140255736Sdavidch
1141255736Sdavidch        DELAY(5);
1142255736Sdavidch    }
1143255736Sdavidch
1144255736Sdavidch    if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1145295830Sdavidcs        BLOGE(sc, "Cannot get access to nvram interface "
1146295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1147295830Sdavidcs            port, val);
1148255736Sdavidch        return (-1);
1149255736Sdavidch    }
1150255736Sdavidch
1151255736Sdavidch    return (0);
1152255736Sdavidch}
1153255736Sdavidch
1154255736Sdavidchstatic int
1155255736Sdavidchbxe_release_nvram_lock(struct bxe_softc *sc)
1156255736Sdavidch{
1157255736Sdavidch    int port = SC_PORT(sc);
1158255736Sdavidch    int count, i;
1159255736Sdavidch    uint32_t val = 0;
1160255736Sdavidch
1161255736Sdavidch    /* adjust timeout for emulation/FPGA */
1162255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1163255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1164255736Sdavidch        count *= 100;
1165255736Sdavidch    }
1166255736Sdavidch
1167255736Sdavidch    /* relinquish nvram interface */
1168255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1169255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1170255736Sdavidch
1171255736Sdavidch    for (i = 0; i < count*10; i++) {
1172255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1173255736Sdavidch        if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1174255736Sdavidch            break;
1175255736Sdavidch        }
1176255736Sdavidch
1177255736Sdavidch        DELAY(5);
1178255736Sdavidch    }
1179255736Sdavidch
1180255736Sdavidch    if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1181295830Sdavidcs        BLOGE(sc, "Cannot free access to nvram interface "
1182295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1183295830Sdavidcs            port, val);
1184255736Sdavidch        return (-1);
1185255736Sdavidch    }
1186255736Sdavidch
1187255736Sdavidch    /* release HW lock: protect against other PFs in PF Direct Assignment */
1188255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1189255736Sdavidch
1190255736Sdavidch    return (0);
1191255736Sdavidch}
1192255736Sdavidch
1193255736Sdavidchstatic void
1194255736Sdavidchbxe_enable_nvram_access(struct bxe_softc *sc)
1195255736Sdavidch{
1196255736Sdavidch    uint32_t val;
1197255736Sdavidch
1198255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1199255736Sdavidch
1200255736Sdavidch    /* enable both bits, even on read */
1201255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1202255736Sdavidch           (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1203255736Sdavidch}
1204255736Sdavidch
1205255736Sdavidchstatic void
1206255736Sdavidchbxe_disable_nvram_access(struct bxe_softc *sc)
1207255736Sdavidch{
1208255736Sdavidch    uint32_t val;
1209255736Sdavidch
1210255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1211255736Sdavidch
1212255736Sdavidch    /* disable both bits, even after read */
1213255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1214255736Sdavidch           (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1215255736Sdavidch                    MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1216255736Sdavidch}
1217255736Sdavidch
1218255736Sdavidchstatic int
1219255736Sdavidchbxe_nvram_read_dword(struct bxe_softc *sc,
1220255736Sdavidch                     uint32_t         offset,
1221255736Sdavidch                     uint32_t         *ret_val,
1222255736Sdavidch                     uint32_t         cmd_flags)
1223255736Sdavidch{
1224255736Sdavidch    int count, i, rc;
1225255736Sdavidch    uint32_t val;
1226255736Sdavidch
1227255736Sdavidch    /* build the command word */
1228255736Sdavidch    cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1229255736Sdavidch
1230255736Sdavidch    /* need to clear DONE bit separately */
1231255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1232255736Sdavidch
1233255736Sdavidch    /* address of the NVRAM to read from */
1234255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1235255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1236255736Sdavidch
1237255736Sdavidch    /* issue a read command */
1238255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1239255736Sdavidch
1240255736Sdavidch    /* adjust timeout for emulation/FPGA */
1241255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1242255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1243255736Sdavidch        count *= 100;
1244255736Sdavidch    }
1245255736Sdavidch
1246255736Sdavidch    /* wait for completion */
1247255736Sdavidch    *ret_val = 0;
1248255736Sdavidch    rc = -1;
1249255736Sdavidch    for (i = 0; i < count; i++) {
1250255736Sdavidch        DELAY(5);
1251255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1252255736Sdavidch
1253255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1254255736Sdavidch            val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1255255736Sdavidch            /* we read nvram data in cpu order
1256255736Sdavidch             * but ethtool sees it as an array of bytes
1257255736Sdavidch             * converting to big-endian will do the work
1258255736Sdavidch             */
1259255736Sdavidch            *ret_val = htobe32(val);
1260255736Sdavidch            rc = 0;
1261255736Sdavidch            break;
1262255736Sdavidch        }
1263255736Sdavidch    }
1264255736Sdavidch
1265255736Sdavidch    if (rc == -1) {
1266295830Sdavidcs        BLOGE(sc, "nvram read timeout expired "
1267295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1268295830Sdavidcs            offset, cmd_flags, val);
1269255736Sdavidch    }
1270255736Sdavidch
1271255736Sdavidch    return (rc);
1272255736Sdavidch}
1273255736Sdavidch
1274255736Sdavidchstatic int
1275255736Sdavidchbxe_nvram_read(struct bxe_softc *sc,
1276255736Sdavidch               uint32_t         offset,
1277255736Sdavidch               uint8_t          *ret_buf,
1278255736Sdavidch               int              buf_size)
1279255736Sdavidch{
1280255736Sdavidch    uint32_t cmd_flags;
1281255736Sdavidch    uint32_t val;
1282255736Sdavidch    int rc;
1283255736Sdavidch
1284255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1285255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1286255736Sdavidch              offset, buf_size);
1287255736Sdavidch        return (-1);
1288255736Sdavidch    }
1289255736Sdavidch
1290255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1291255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1292255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1293255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1294255736Sdavidch        return (-1);
1295255736Sdavidch    }
1296255736Sdavidch
1297255736Sdavidch    /* request access to nvram interface */
1298255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1299255736Sdavidch    if (rc) {
1300255736Sdavidch        return (rc);
1301255736Sdavidch    }
1302255736Sdavidch
1303255736Sdavidch    /* enable access to nvram interface */
1304255736Sdavidch    bxe_enable_nvram_access(sc);
1305255736Sdavidch
1306255736Sdavidch    /* read the first word(s) */
1307255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1308255736Sdavidch    while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1309255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1310255736Sdavidch        memcpy(ret_buf, &val, 4);
1311255736Sdavidch
1312255736Sdavidch        /* advance to the next dword */
1313255736Sdavidch        offset += sizeof(uint32_t);
1314255736Sdavidch        ret_buf += sizeof(uint32_t);
1315255736Sdavidch        buf_size -= sizeof(uint32_t);
1316255736Sdavidch        cmd_flags = 0;
1317255736Sdavidch    }
1318255736Sdavidch
1319255736Sdavidch    if (rc == 0) {
1320255736Sdavidch        cmd_flags |= MCPR_NVM_COMMAND_LAST;
1321255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1322255736Sdavidch        memcpy(ret_buf, &val, 4);
1323255736Sdavidch    }
1324255736Sdavidch
1325255736Sdavidch    /* disable access to nvram interface */
1326255736Sdavidch    bxe_disable_nvram_access(sc);
1327255736Sdavidch    bxe_release_nvram_lock(sc);
1328255736Sdavidch
1329255736Sdavidch    return (rc);
1330255736Sdavidch}
1331255736Sdavidch
1332255736Sdavidchstatic int
1333255736Sdavidchbxe_nvram_write_dword(struct bxe_softc *sc,
1334255736Sdavidch                      uint32_t         offset,
1335255736Sdavidch                      uint32_t         val,
1336255736Sdavidch                      uint32_t         cmd_flags)
1337255736Sdavidch{
1338255736Sdavidch    int count, i, rc;
1339255736Sdavidch
1340255736Sdavidch    /* build the command word */
1341255736Sdavidch    cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1342255736Sdavidch
1343255736Sdavidch    /* need to clear DONE bit separately */
1344255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1345255736Sdavidch
1346255736Sdavidch    /* write the data */
1347255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1348255736Sdavidch
1349255736Sdavidch    /* address of the NVRAM to write to */
1350255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1351255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1352255736Sdavidch
1353255736Sdavidch    /* issue the write command */
1354255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1355255736Sdavidch
1356255736Sdavidch    /* adjust timeout for emulation/FPGA */
1357255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1358255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1359255736Sdavidch        count *= 100;
1360255736Sdavidch    }
1361255736Sdavidch
1362255736Sdavidch    /* wait for completion */
1363255736Sdavidch    rc = -1;
1364255736Sdavidch    for (i = 0; i < count; i++) {
1365255736Sdavidch        DELAY(5);
1366255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1367255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1368255736Sdavidch            rc = 0;
1369255736Sdavidch            break;
1370255736Sdavidch        }
1371255736Sdavidch    }
1372255736Sdavidch
1373255736Sdavidch    if (rc == -1) {
1374295830Sdavidcs        BLOGE(sc, "nvram write timeout expired "
1375295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1376295830Sdavidcs            offset, cmd_flags, val);
1377255736Sdavidch    }
1378255736Sdavidch
1379255736Sdavidch    return (rc);
1380255736Sdavidch}
1381255736Sdavidch
1382255736Sdavidch#define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1383255736Sdavidch
1384255736Sdavidchstatic int
1385255736Sdavidchbxe_nvram_write1(struct bxe_softc *sc,
1386255736Sdavidch                 uint32_t         offset,
1387255736Sdavidch                 uint8_t          *data_buf,
1388255736Sdavidch                 int              buf_size)
1389255736Sdavidch{
1390255736Sdavidch    uint32_t cmd_flags;
1391255736Sdavidch    uint32_t align_offset;
1392255736Sdavidch    uint32_t val;
1393255736Sdavidch    int rc;
1394255736Sdavidch
1395255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1396255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1397255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1398255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1399255736Sdavidch        return (-1);
1400255736Sdavidch    }
1401255736Sdavidch
1402255736Sdavidch    /* request access to nvram interface */
1403255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1404255736Sdavidch    if (rc) {
1405255736Sdavidch        return (rc);
1406255736Sdavidch    }
1407255736Sdavidch
1408255736Sdavidch    /* enable access to nvram interface */
1409255736Sdavidch    bxe_enable_nvram_access(sc);
1410255736Sdavidch
1411255736Sdavidch    cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1412255736Sdavidch    align_offset = (offset & ~0x03);
1413255736Sdavidch    rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1414255736Sdavidch
1415255736Sdavidch    if (rc == 0) {
1416255736Sdavidch        val &= ~(0xff << BYTE_OFFSET(offset));
1417255736Sdavidch        val |= (*data_buf << BYTE_OFFSET(offset));
1418255736Sdavidch
1419255736Sdavidch        /* nvram data is returned as an array of bytes
1420255736Sdavidch         * convert it back to cpu order
1421255736Sdavidch         */
1422255736Sdavidch        val = be32toh(val);
1423255736Sdavidch
1424255736Sdavidch        rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1425255736Sdavidch    }
1426255736Sdavidch
1427255736Sdavidch    /* disable access to nvram interface */
1428255736Sdavidch    bxe_disable_nvram_access(sc);
1429255736Sdavidch    bxe_release_nvram_lock(sc);
1430255736Sdavidch
1431255736Sdavidch    return (rc);
1432255736Sdavidch}
1433255736Sdavidch
1434255736Sdavidchstatic int
1435255736Sdavidchbxe_nvram_write(struct bxe_softc *sc,
1436255736Sdavidch                uint32_t         offset,
1437255736Sdavidch                uint8_t          *data_buf,
1438255736Sdavidch                int              buf_size)
1439255736Sdavidch{
1440255736Sdavidch    uint32_t cmd_flags;
1441255736Sdavidch    uint32_t val;
1442255736Sdavidch    uint32_t written_so_far;
1443255736Sdavidch    int rc;
1444255736Sdavidch
1445255736Sdavidch    if (buf_size == 1) {
1446255736Sdavidch        return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1447255736Sdavidch    }
1448255736Sdavidch
1449255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1450255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1451255736Sdavidch              offset, buf_size);
1452255736Sdavidch        return (-1);
1453255736Sdavidch    }
1454255736Sdavidch
1455255736Sdavidch    if (buf_size == 0) {
1456255736Sdavidch        return (0); /* nothing to do */
1457255736Sdavidch    }
1458255736Sdavidch
1459255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1460255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1461255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1462255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1463255736Sdavidch        return (-1);
1464255736Sdavidch    }
1465255736Sdavidch
1466255736Sdavidch    /* request access to nvram interface */
1467255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1468255736Sdavidch    if (rc) {
1469255736Sdavidch        return (rc);
1470255736Sdavidch    }
1471255736Sdavidch
1472255736Sdavidch    /* enable access to nvram interface */
1473255736Sdavidch    bxe_enable_nvram_access(sc);
1474255736Sdavidch
1475255736Sdavidch    written_so_far = 0;
1476255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1477255736Sdavidch    while ((written_so_far < buf_size) && (rc == 0)) {
1478255736Sdavidch        if (written_so_far == (buf_size - sizeof(uint32_t))) {
1479255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1480255736Sdavidch        } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1481255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1482255736Sdavidch        } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1483255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1484255736Sdavidch        }
1485255736Sdavidch
1486255736Sdavidch        memcpy(&val, data_buf, 4);
1487255736Sdavidch
1488255736Sdavidch        rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1489255736Sdavidch
1490255736Sdavidch        /* advance to the next dword */
1491255736Sdavidch        offset += sizeof(uint32_t);
1492255736Sdavidch        data_buf += sizeof(uint32_t);
1493255736Sdavidch        written_so_far += sizeof(uint32_t);
1494255736Sdavidch        cmd_flags = 0;
1495255736Sdavidch    }
1496255736Sdavidch
1497255736Sdavidch    /* disable access to nvram interface */
1498255736Sdavidch    bxe_disable_nvram_access(sc);
1499255736Sdavidch    bxe_release_nvram_lock(sc);
1500255736Sdavidch
1501255736Sdavidch    return (rc);
1502255736Sdavidch}
1503255736Sdavidch
1504255736Sdavidch/* copy command into DMAE command memory and set DMAE command Go */
1505255736Sdavidchvoid
1506255736Sdavidchbxe_post_dmae(struct bxe_softc    *sc,
1507296071Sdavidcs              struct dmae_cmd *dmae,
1508255736Sdavidch              int                 idx)
1509255736Sdavidch{
1510255736Sdavidch    uint32_t cmd_offset;
1511255736Sdavidch    int i;
1512255736Sdavidch
1513296071Sdavidcs    cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_cmd) * idx));
1514296071Sdavidcs    for (i = 0; i < ((sizeof(struct dmae_cmd) / 4)); i++) {
1515255736Sdavidch        REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1516255736Sdavidch    }
1517255736Sdavidch
1518255736Sdavidch    REG_WR(sc, dmae_reg_go_c[idx], 1);
1519255736Sdavidch}
1520255736Sdavidch
1521255736Sdavidchuint32_t
1522255736Sdavidchbxe_dmae_opcode_add_comp(uint32_t opcode,
1523255736Sdavidch                         uint8_t  comp_type)
1524255736Sdavidch{
1525296071Sdavidcs    return (opcode | ((comp_type << DMAE_CMD_C_DST_SHIFT) |
1526296071Sdavidcs                      DMAE_CMD_C_TYPE_ENABLE));
1527255736Sdavidch}
1528255736Sdavidch
1529255736Sdavidchuint32_t
1530255736Sdavidchbxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1531255736Sdavidch{
1532296071Sdavidcs    return (opcode & ~DMAE_CMD_SRC_RESET);
1533255736Sdavidch}
1534255736Sdavidch
1535255736Sdavidchuint32_t
1536255736Sdavidchbxe_dmae_opcode(struct bxe_softc *sc,
1537255736Sdavidch                uint8_t          src_type,
1538255736Sdavidch                uint8_t          dst_type,
1539255736Sdavidch                uint8_t          with_comp,
1540255736Sdavidch                uint8_t          comp_type)
1541255736Sdavidch{
1542255736Sdavidch    uint32_t opcode = 0;
1543255736Sdavidch
1544296071Sdavidcs    opcode |= ((src_type << DMAE_CMD_SRC_SHIFT) |
1545296071Sdavidcs               (dst_type << DMAE_CMD_DST_SHIFT));
1546255736Sdavidch
1547296071Sdavidcs    opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
1548255736Sdavidch
1549255736Sdavidch    opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1550255736Sdavidch
1551296071Sdavidcs    opcode |= ((SC_VN(sc) << DMAE_CMD_E1HVN_SHIFT) |
1552296071Sdavidcs               (SC_VN(sc) << DMAE_CMD_DST_VN_SHIFT));
1553255736Sdavidch
1554296071Sdavidcs    opcode |= (DMAE_COM_SET_ERR << DMAE_CMD_ERR_POLICY_SHIFT);
1555255736Sdavidch
1556255736Sdavidch#ifdef __BIG_ENDIAN
1557255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1558255736Sdavidch#else
1559255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1560255736Sdavidch#endif
1561255736Sdavidch
1562255736Sdavidch    if (with_comp) {
1563255736Sdavidch        opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1564255736Sdavidch    }
1565255736Sdavidch
1566255736Sdavidch    return (opcode);
1567255736Sdavidch}
1568255736Sdavidch
1569255736Sdavidchstatic void
1570255736Sdavidchbxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1571296071Sdavidcs                        struct dmae_cmd *dmae,
1572255736Sdavidch                        uint8_t             src_type,
1573255736Sdavidch                        uint8_t             dst_type)
1574255736Sdavidch{
1575296071Sdavidcs    memset(dmae, 0, sizeof(struct dmae_cmd));
1576255736Sdavidch
1577255736Sdavidch    /* set the opcode */
1578255736Sdavidch    dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1579255736Sdavidch                                   TRUE, DMAE_COMP_PCI);
1580255736Sdavidch
1581255736Sdavidch    /* fill in the completion parameters */
1582255736Sdavidch    dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1583255736Sdavidch    dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1584255736Sdavidch    dmae->comp_val     = DMAE_COMP_VAL;
1585255736Sdavidch}
1586255736Sdavidch
1587255736Sdavidch/* issue a DMAE command over the init channel and wait for completion */
1588255736Sdavidchstatic int
1589255736Sdavidchbxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1590296071Sdavidcs                         struct dmae_cmd *dmae)
1591255736Sdavidch{
1592255736Sdavidch    uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1593255736Sdavidch    int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1594255736Sdavidch
1595255736Sdavidch    BXE_DMAE_LOCK(sc);
1596255736Sdavidch
1597255736Sdavidch    /* reset completion */
1598255736Sdavidch    *wb_comp = 0;
1599255736Sdavidch
1600255736Sdavidch    /* post the command on the channel used for initializations */
1601255736Sdavidch    bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1602255736Sdavidch
1603255736Sdavidch    /* wait for completion */
1604255736Sdavidch    DELAY(5);
1605255736Sdavidch
1606255736Sdavidch    while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1607255736Sdavidch        if (!timeout ||
1608255736Sdavidch            (sc->recovery_state != BXE_RECOVERY_DONE &&
1609255736Sdavidch             sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1610295830Sdavidcs            BLOGE(sc, "DMAE timeout! *wb_comp 0x%x recovery_state 0x%x\n",
1611295830Sdavidcs                *wb_comp, sc->recovery_state);
1612255736Sdavidch            BXE_DMAE_UNLOCK(sc);
1613255736Sdavidch            return (DMAE_TIMEOUT);
1614255736Sdavidch        }
1615255736Sdavidch
1616255736Sdavidch        timeout--;
1617255736Sdavidch        DELAY(50);
1618255736Sdavidch    }
1619255736Sdavidch
1620255736Sdavidch    if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1621295830Sdavidcs        BLOGE(sc, "DMAE PCI error! *wb_comp 0x%x recovery_state 0x%x\n",
1622295830Sdavidcs                *wb_comp, sc->recovery_state);
1623255736Sdavidch        BXE_DMAE_UNLOCK(sc);
1624255736Sdavidch        return (DMAE_PCI_ERROR);
1625255736Sdavidch    }
1626255736Sdavidch
1627255736Sdavidch    BXE_DMAE_UNLOCK(sc);
1628255736Sdavidch    return (0);
1629255736Sdavidch}
1630255736Sdavidch
1631255736Sdavidchvoid
1632255736Sdavidchbxe_read_dmae(struct bxe_softc *sc,
1633255736Sdavidch              uint32_t         src_addr,
1634255736Sdavidch              uint32_t         len32)
1635255736Sdavidch{
1636296071Sdavidcs    struct dmae_cmd dmae;
1637255736Sdavidch    uint32_t *data;
1638255736Sdavidch    int i, rc;
1639255736Sdavidch
1640255736Sdavidch    DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1641255736Sdavidch
1642255736Sdavidch    if (!sc->dmae_ready) {
1643255736Sdavidch        data = BXE_SP(sc, wb_data[0]);
1644255736Sdavidch
1645255736Sdavidch        for (i = 0; i < len32; i++) {
1646255736Sdavidch            data[i] = (CHIP_IS_E1(sc)) ?
1647255736Sdavidch                          bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1648255736Sdavidch                          REG_RD(sc, (src_addr + (i * 4)));
1649255736Sdavidch        }
1650255736Sdavidch
1651255736Sdavidch        return;
1652255736Sdavidch    }
1653255736Sdavidch
1654255736Sdavidch    /* set opcode and fixed command fields */
1655255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1656255736Sdavidch
1657255736Sdavidch    /* fill in addresses and len */
1658255736Sdavidch    dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1659255736Sdavidch    dmae.src_addr_hi = 0;
1660255736Sdavidch    dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1661255736Sdavidch    dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1662255736Sdavidch    dmae.len         = len32;
1663255736Sdavidch
1664255736Sdavidch    /* issue the command and wait for completion */
1665255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1666255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1667297793Spfg    }
1668255736Sdavidch}
1669255736Sdavidch
1670255736Sdavidchvoid
1671255736Sdavidchbxe_write_dmae(struct bxe_softc *sc,
1672255736Sdavidch               bus_addr_t       dma_addr,
1673255736Sdavidch               uint32_t         dst_addr,
1674255736Sdavidch               uint32_t         len32)
1675255736Sdavidch{
1676296071Sdavidcs    struct dmae_cmd dmae;
1677255736Sdavidch    int rc;
1678255736Sdavidch
1679255736Sdavidch    if (!sc->dmae_ready) {
1680255736Sdavidch        DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1681255736Sdavidch
1682255736Sdavidch        if (CHIP_IS_E1(sc)) {
1683255736Sdavidch            ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1684255736Sdavidch        } else {
1685255736Sdavidch            ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1686255736Sdavidch        }
1687255736Sdavidch
1688255736Sdavidch        return;
1689255736Sdavidch    }
1690255736Sdavidch
1691255736Sdavidch    /* set opcode and fixed command fields */
1692255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1693255736Sdavidch
1694255736Sdavidch    /* fill in addresses and len */
1695255736Sdavidch    dmae.src_addr_lo = U64_LO(dma_addr);
1696255736Sdavidch    dmae.src_addr_hi = U64_HI(dma_addr);
1697255736Sdavidch    dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1698255736Sdavidch    dmae.dst_addr_hi = 0;
1699255736Sdavidch    dmae.len         = len32;
1700255736Sdavidch
1701255736Sdavidch    /* issue the command and wait for completion */
1702255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1703255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1704255736Sdavidch    }
1705255736Sdavidch}
1706255736Sdavidch
1707255736Sdavidchvoid
1708255736Sdavidchbxe_write_dmae_phys_len(struct bxe_softc *sc,
1709255736Sdavidch                        bus_addr_t       phys_addr,
1710255736Sdavidch                        uint32_t         addr,
1711255736Sdavidch                        uint32_t         len)
1712255736Sdavidch{
1713255736Sdavidch    int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1714255736Sdavidch    int offset = 0;
1715255736Sdavidch
1716255736Sdavidch    while (len > dmae_wr_max) {
1717255736Sdavidch        bxe_write_dmae(sc,
1718255736Sdavidch                       (phys_addr + offset), /* src DMA address */
1719255736Sdavidch                       (addr + offset),      /* dst GRC address */
1720255736Sdavidch                       dmae_wr_max);
1721255736Sdavidch        offset += (dmae_wr_max * 4);
1722255736Sdavidch        len -= dmae_wr_max;
1723255736Sdavidch    }
1724255736Sdavidch
1725255736Sdavidch    bxe_write_dmae(sc,
1726255736Sdavidch                   (phys_addr + offset), /* src DMA address */
1727255736Sdavidch                   (addr + offset),      /* dst GRC address */
1728255736Sdavidch                   len);
1729255736Sdavidch}
1730255736Sdavidch
1731255736Sdavidchvoid
1732255736Sdavidchbxe_set_ctx_validation(struct bxe_softc   *sc,
1733255736Sdavidch                       struct eth_context *cxt,
1734255736Sdavidch                       uint32_t           cid)
1735255736Sdavidch{
1736255736Sdavidch    /* ustorm cxt validation */
1737255736Sdavidch    cxt->ustorm_ag_context.cdu_usage =
1738255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1739255736Sdavidch            CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1740255736Sdavidch    /* xcontext validation */
1741255736Sdavidch    cxt->xstorm_ag_context.cdu_reserved =
1742255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1743255736Sdavidch            CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1744255736Sdavidch}
1745255736Sdavidch
1746255736Sdavidchstatic void
1747255736Sdavidchbxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1748255736Sdavidch                            uint8_t          port,
1749255736Sdavidch                            uint8_t          fw_sb_id,
1750255736Sdavidch                            uint8_t          sb_index,
1751255736Sdavidch                            uint8_t          ticks)
1752255736Sdavidch{
1753255736Sdavidch    uint32_t addr =
1754255736Sdavidch        (BAR_CSTRORM_INTMEM +
1755255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1756255736Sdavidch
1757255736Sdavidch    REG_WR8(sc, addr, ticks);
1758255736Sdavidch
1759255736Sdavidch    BLOGD(sc, DBG_LOAD,
1760255736Sdavidch          "port %d fw_sb_id %d sb_index %d ticks %d\n",
1761255736Sdavidch          port, fw_sb_id, sb_index, ticks);
1762255736Sdavidch}
1763255736Sdavidch
1764255736Sdavidchstatic void
1765255736Sdavidchbxe_storm_memset_hc_disable(struct bxe_softc *sc,
1766255736Sdavidch                            uint8_t          port,
1767255736Sdavidch                            uint16_t         fw_sb_id,
1768255736Sdavidch                            uint8_t          sb_index,
1769255736Sdavidch                            uint8_t          disable)
1770255736Sdavidch{
1771255736Sdavidch    uint32_t enable_flag =
1772255736Sdavidch        (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1773255736Sdavidch    uint32_t addr =
1774255736Sdavidch        (BAR_CSTRORM_INTMEM +
1775255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1776255736Sdavidch    uint8_t flags;
1777255736Sdavidch
1778255736Sdavidch    /* clear and set */
1779255736Sdavidch    flags = REG_RD8(sc, addr);
1780255736Sdavidch    flags &= ~HC_INDEX_DATA_HC_ENABLED;
1781255736Sdavidch    flags |= enable_flag;
1782255736Sdavidch    REG_WR8(sc, addr, flags);
1783255736Sdavidch
1784255736Sdavidch    BLOGD(sc, DBG_LOAD,
1785255736Sdavidch          "port %d fw_sb_id %d sb_index %d disable %d\n",
1786255736Sdavidch          port, fw_sb_id, sb_index, disable);
1787255736Sdavidch}
1788255736Sdavidch
1789255736Sdavidchvoid
1790255736Sdavidchbxe_update_coalesce_sb_index(struct bxe_softc *sc,
1791255736Sdavidch                             uint8_t          fw_sb_id,
1792255736Sdavidch                             uint8_t          sb_index,
1793255736Sdavidch                             uint8_t          disable,
1794255736Sdavidch                             uint16_t         usec)
1795255736Sdavidch{
1796255736Sdavidch    int port = SC_PORT(sc);
1797255736Sdavidch    uint8_t ticks = (usec / 4); /* XXX ??? */
1798255736Sdavidch
1799255736Sdavidch    bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1800255736Sdavidch
1801255736Sdavidch    disable = (disable) ? 1 : ((usec) ? 0 : 1);
1802255736Sdavidch    bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1803255736Sdavidch}
1804255736Sdavidch
1805255736Sdavidchvoid
1806255736Sdavidchelink_cb_udelay(struct bxe_softc *sc,
1807255736Sdavidch                uint32_t         usecs)
1808255736Sdavidch{
1809255736Sdavidch    DELAY(usecs);
1810255736Sdavidch}
1811255736Sdavidch
1812255736Sdavidchuint32_t
1813255736Sdavidchelink_cb_reg_read(struct bxe_softc *sc,
1814255736Sdavidch                  uint32_t         reg_addr)
1815255736Sdavidch{
1816255736Sdavidch    return (REG_RD(sc, reg_addr));
1817255736Sdavidch}
1818255736Sdavidch
1819255736Sdavidchvoid
1820255736Sdavidchelink_cb_reg_write(struct bxe_softc *sc,
1821255736Sdavidch                   uint32_t         reg_addr,
1822255736Sdavidch                   uint32_t         val)
1823255736Sdavidch{
1824255736Sdavidch    REG_WR(sc, reg_addr, val);
1825255736Sdavidch}
1826255736Sdavidch
1827255736Sdavidchvoid
1828255736Sdavidchelink_cb_reg_wb_write(struct bxe_softc *sc,
1829255736Sdavidch                      uint32_t         offset,
1830255736Sdavidch                      uint32_t         *wb_write,
1831255736Sdavidch                      uint16_t         len)
1832255736Sdavidch{
1833255736Sdavidch    REG_WR_DMAE(sc, offset, wb_write, len);
1834255736Sdavidch}
1835255736Sdavidch
1836255736Sdavidchvoid
1837255736Sdavidchelink_cb_reg_wb_read(struct bxe_softc *sc,
1838255736Sdavidch                     uint32_t         offset,
1839255736Sdavidch                     uint32_t         *wb_write,
1840255736Sdavidch                     uint16_t         len)
1841255736Sdavidch{
1842255736Sdavidch    REG_RD_DMAE(sc, offset, wb_write, len);
1843255736Sdavidch}
1844255736Sdavidch
1845255736Sdavidchuint8_t
1846255736Sdavidchelink_cb_path_id(struct bxe_softc *sc)
1847255736Sdavidch{
1848255736Sdavidch    return (SC_PATH(sc));
1849255736Sdavidch}
1850255736Sdavidch
1851255736Sdavidchvoid
1852255736Sdavidchelink_cb_event_log(struct bxe_softc     *sc,
1853255736Sdavidch                   const elink_log_id_t elink_log_id,
1854255736Sdavidch                   ...)
1855255736Sdavidch{
1856255736Sdavidch    /* XXX */
1857255736Sdavidch    BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1858255736Sdavidch}
1859255736Sdavidch
1860255736Sdavidchstatic int
1861255736Sdavidchbxe_set_spio(struct bxe_softc *sc,
1862255736Sdavidch             int              spio,
1863255736Sdavidch             uint32_t         mode)
1864255736Sdavidch{
1865255736Sdavidch    uint32_t spio_reg;
1866255736Sdavidch
1867255736Sdavidch    /* Only 2 SPIOs are configurable */
1868255736Sdavidch    if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1869295830Sdavidcs        BLOGE(sc, "Invalid SPIO 0x%x mode 0x%x\n", spio, mode);
1870255736Sdavidch        return (-1);
1871255736Sdavidch    }
1872255736Sdavidch
1873255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1874255736Sdavidch
1875255736Sdavidch    /* read SPIO and mask except the float bits */
1876255736Sdavidch    spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1877255736Sdavidch
1878255736Sdavidch    switch (mode) {
1879255736Sdavidch    case MISC_SPIO_OUTPUT_LOW:
1880255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1881255736Sdavidch        /* clear FLOAT and set CLR */
1882255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1883255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1884255736Sdavidch        break;
1885255736Sdavidch
1886255736Sdavidch    case MISC_SPIO_OUTPUT_HIGH:
1887255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1888255736Sdavidch        /* clear FLOAT and set SET */
1889255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1890255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_SET_POS);
1891255736Sdavidch        break;
1892255736Sdavidch
1893255736Sdavidch    case MISC_SPIO_INPUT_HI_Z:
1894255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1895255736Sdavidch        /* set FLOAT */
1896255736Sdavidch        spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1897255736Sdavidch        break;
1898255736Sdavidch
1899255736Sdavidch    default:
1900255736Sdavidch        break;
1901255736Sdavidch    }
1902255736Sdavidch
1903255736Sdavidch    REG_WR(sc, MISC_REG_SPIO, spio_reg);
1904255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1905255736Sdavidch
1906255736Sdavidch    return (0);
1907255736Sdavidch}
1908255736Sdavidch
1909255736Sdavidchstatic int
1910255736Sdavidchbxe_gpio_read(struct bxe_softc *sc,
1911255736Sdavidch              int              gpio_num,
1912255736Sdavidch              uint8_t          port)
1913255736Sdavidch{
1914255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1915255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1916255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1917255736Sdavidch    int gpio_shift = (gpio_num +
1918255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1919255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1920255736Sdavidch    uint32_t gpio_reg;
1921255736Sdavidch
1922255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1923295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d port 0x%x gpio_port %d gpio_shift %d"
1924295830Sdavidcs            " gpio_mask 0x%x\n", gpio_num, port, gpio_port, gpio_shift,
1925295830Sdavidcs            gpio_mask);
1926255736Sdavidch        return (-1);
1927255736Sdavidch    }
1928255736Sdavidch
1929255736Sdavidch    /* read GPIO value */
1930255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1931255736Sdavidch
1932255736Sdavidch    /* get the requested pin value */
1933255736Sdavidch    return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
1934255736Sdavidch}
1935255736Sdavidch
1936255736Sdavidchstatic int
1937255736Sdavidchbxe_gpio_write(struct bxe_softc *sc,
1938255736Sdavidch               int              gpio_num,
1939255736Sdavidch               uint32_t         mode,
1940255736Sdavidch               uint8_t          port)
1941255736Sdavidch{
1942255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1943255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1944255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1945255736Sdavidch    int gpio_shift = (gpio_num +
1946255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1947255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1948255736Sdavidch    uint32_t gpio_reg;
1949255736Sdavidch
1950255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1951295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
1952295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
1953295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
1954255736Sdavidch        return (-1);
1955255736Sdavidch    }
1956255736Sdavidch
1957255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1958255736Sdavidch
1959255736Sdavidch    /* read GPIO and mask except the float bits */
1960255736Sdavidch    gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1961255736Sdavidch
1962255736Sdavidch    switch (mode) {
1963255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1964255736Sdavidch        BLOGD(sc, DBG_PHY,
1965255736Sdavidch              "Set GPIO %d (shift %d) -> output low\n",
1966255736Sdavidch              gpio_num, gpio_shift);
1967255736Sdavidch        /* clear FLOAT and set CLR */
1968255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1969255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1970255736Sdavidch        break;
1971255736Sdavidch
1972255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1973255736Sdavidch        BLOGD(sc, DBG_PHY,
1974255736Sdavidch              "Set GPIO %d (shift %d) -> output high\n",
1975255736Sdavidch              gpio_num, gpio_shift);
1976255736Sdavidch        /* clear FLOAT and set SET */
1977255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1978255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1979255736Sdavidch        break;
1980255736Sdavidch
1981255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1982255736Sdavidch        BLOGD(sc, DBG_PHY,
1983255736Sdavidch              "Set GPIO %d (shift %d) -> input\n",
1984255736Sdavidch              gpio_num, gpio_shift);
1985255736Sdavidch        /* set FLOAT */
1986255736Sdavidch        gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1987255736Sdavidch        break;
1988255736Sdavidch
1989255736Sdavidch    default:
1990255736Sdavidch        break;
1991255736Sdavidch    }
1992255736Sdavidch
1993255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1994255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1995255736Sdavidch
1996255736Sdavidch    return (0);
1997255736Sdavidch}
1998255736Sdavidch
1999255736Sdavidchstatic int
2000255736Sdavidchbxe_gpio_mult_write(struct bxe_softc *sc,
2001255736Sdavidch                    uint8_t          pins,
2002255736Sdavidch                    uint32_t         mode)
2003255736Sdavidch{
2004255736Sdavidch    uint32_t gpio_reg;
2005255736Sdavidch
2006255736Sdavidch    /* any port swapping should be handled by caller */
2007255736Sdavidch
2008255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2009255736Sdavidch
2010255736Sdavidch    /* read GPIO and mask except the float bits */
2011255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
2012255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2013255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
2014255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
2015255736Sdavidch
2016255736Sdavidch    switch (mode) {
2017255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2018255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
2019255736Sdavidch        /* set CLR */
2020255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
2021255736Sdavidch        break;
2022255736Sdavidch
2023255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2024255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
2025255736Sdavidch        /* set SET */
2026255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
2027255736Sdavidch        break;
2028255736Sdavidch
2029255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2030255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
2031255736Sdavidch        /* set FLOAT */
2032255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2033255736Sdavidch        break;
2034255736Sdavidch
2035255736Sdavidch    default:
2036295830Sdavidcs        BLOGE(sc, "Invalid GPIO mode assignment pins 0x%x mode 0x%x"
2037295830Sdavidcs            " gpio_reg 0x%x\n", pins, mode, gpio_reg);
2038255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2039255736Sdavidch        return (-1);
2040255736Sdavidch    }
2041255736Sdavidch
2042255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2043255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2044255736Sdavidch
2045255736Sdavidch    return (0);
2046255736Sdavidch}
2047255736Sdavidch
2048255736Sdavidchstatic int
2049255736Sdavidchbxe_gpio_int_write(struct bxe_softc *sc,
2050255736Sdavidch                   int              gpio_num,
2051255736Sdavidch                   uint32_t         mode,
2052255736Sdavidch                   uint8_t          port)
2053255736Sdavidch{
2054255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
2055255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2056255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2057255736Sdavidch    int gpio_shift = (gpio_num +
2058255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2059255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
2060255736Sdavidch    uint32_t gpio_reg;
2061255736Sdavidch
2062255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
2063295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
2064295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
2065295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
2066255736Sdavidch        return (-1);
2067255736Sdavidch    }
2068255736Sdavidch
2069255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2070255736Sdavidch
2071255736Sdavidch    /* read GPIO int */
2072255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2073255736Sdavidch
2074255736Sdavidch    switch (mode) {
2075255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2076255736Sdavidch        BLOGD(sc, DBG_PHY,
2077255736Sdavidch              "Clear GPIO INT %d (shift %d) -> output low\n",
2078255736Sdavidch              gpio_num, gpio_shift);
2079255736Sdavidch        /* clear SET and set CLR */
2080255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2081255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2082255736Sdavidch        break;
2083255736Sdavidch
2084255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2085255736Sdavidch        BLOGD(sc, DBG_PHY,
2086255736Sdavidch              "Set GPIO INT %d (shift %d) -> output high\n",
2087255736Sdavidch              gpio_num, gpio_shift);
2088255736Sdavidch        /* clear CLR and set SET */
2089255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2090255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2091255736Sdavidch        break;
2092255736Sdavidch
2093255736Sdavidch    default:
2094255736Sdavidch        break;
2095255736Sdavidch    }
2096255736Sdavidch
2097255736Sdavidch    REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2098255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2099255736Sdavidch
2100255736Sdavidch    return (0);
2101255736Sdavidch}
2102255736Sdavidch
2103255736Sdavidchuint32_t
2104255736Sdavidchelink_cb_gpio_read(struct bxe_softc *sc,
2105255736Sdavidch                   uint16_t         gpio_num,
2106255736Sdavidch                   uint8_t          port)
2107255736Sdavidch{
2108255736Sdavidch    return (bxe_gpio_read(sc, gpio_num, port));
2109255736Sdavidch}
2110255736Sdavidch
2111255736Sdavidchuint8_t
2112255736Sdavidchelink_cb_gpio_write(struct bxe_softc *sc,
2113255736Sdavidch                    uint16_t         gpio_num,
2114255736Sdavidch                    uint8_t          mode, /* 0=low 1=high */
2115255736Sdavidch                    uint8_t          port)
2116255736Sdavidch{
2117255736Sdavidch    return (bxe_gpio_write(sc, gpio_num, mode, port));
2118255736Sdavidch}
2119255736Sdavidch
2120255736Sdavidchuint8_t
2121255736Sdavidchelink_cb_gpio_mult_write(struct bxe_softc *sc,
2122255736Sdavidch                         uint8_t          pins,
2123255736Sdavidch                         uint8_t          mode) /* 0=low 1=high */
2124255736Sdavidch{
2125255736Sdavidch    return (bxe_gpio_mult_write(sc, pins, mode));
2126255736Sdavidch}
2127255736Sdavidch
2128255736Sdavidchuint8_t
2129255736Sdavidchelink_cb_gpio_int_write(struct bxe_softc *sc,
2130255736Sdavidch                        uint16_t         gpio_num,
2131255736Sdavidch                        uint8_t          mode, /* 0=low 1=high */
2132255736Sdavidch                        uint8_t          port)
2133255736Sdavidch{
2134255736Sdavidch    return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2135255736Sdavidch}
2136255736Sdavidch
2137255736Sdavidchvoid
2138255736Sdavidchelink_cb_notify_link_changed(struct bxe_softc *sc)
2139255736Sdavidch{
2140255736Sdavidch    REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2141255736Sdavidch                (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2142255736Sdavidch}
2143255736Sdavidch
2144255736Sdavidch/* send the MCP a request, block until there is a reply */
2145255736Sdavidchuint32_t
2146255736Sdavidchelink_cb_fw_command(struct bxe_softc *sc,
2147255736Sdavidch                    uint32_t         command,
2148255736Sdavidch                    uint32_t         param)
2149255736Sdavidch{
2150255736Sdavidch    int mb_idx = SC_FW_MB_IDX(sc);
2151255736Sdavidch    uint32_t seq;
2152255736Sdavidch    uint32_t rc = 0;
2153255736Sdavidch    uint32_t cnt = 1;
2154255736Sdavidch    uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2155255736Sdavidch
2156255736Sdavidch    BXE_FWMB_LOCK(sc);
2157255736Sdavidch
2158255736Sdavidch    seq = ++sc->fw_seq;
2159255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2160255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2161255736Sdavidch
2162255736Sdavidch    BLOGD(sc, DBG_PHY,
2163255736Sdavidch          "wrote command 0x%08x to FW MB param 0x%08x\n",
2164255736Sdavidch          (command | seq), param);
2165255736Sdavidch
2166255736Sdavidch    /* Let the FW do it's magic. GIve it up to 5 seconds... */
2167255736Sdavidch    do {
2168255736Sdavidch        DELAY(delay * 1000);
2169255736Sdavidch        rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2170255736Sdavidch    } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2171255736Sdavidch
2172255736Sdavidch    BLOGD(sc, DBG_PHY,
2173255736Sdavidch          "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2174255736Sdavidch          cnt*delay, rc, seq);
2175255736Sdavidch
2176255736Sdavidch    /* is this a reply to our command? */
2177255736Sdavidch    if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2178255736Sdavidch        rc &= FW_MSG_CODE_MASK;
2179255736Sdavidch    } else {
2180255736Sdavidch        /* Ruh-roh! */
2181255736Sdavidch        BLOGE(sc, "FW failed to respond!\n");
2182255736Sdavidch        // XXX bxe_fw_dump(sc);
2183255736Sdavidch        rc = 0;
2184255736Sdavidch    }
2185255736Sdavidch
2186255736Sdavidch    BXE_FWMB_UNLOCK(sc);
2187255736Sdavidch    return (rc);
2188255736Sdavidch}
2189255736Sdavidch
2190255736Sdavidchstatic uint32_t
2191255736Sdavidchbxe_fw_command(struct bxe_softc *sc,
2192255736Sdavidch               uint32_t         command,
2193255736Sdavidch               uint32_t         param)
2194255736Sdavidch{
2195255736Sdavidch    return (elink_cb_fw_command(sc, command, param));
2196255736Sdavidch}
2197255736Sdavidch
2198255736Sdavidchstatic void
2199255736Sdavidch__storm_memset_dma_mapping(struct bxe_softc *sc,
2200255736Sdavidch                           uint32_t         addr,
2201255736Sdavidch                           bus_addr_t       mapping)
2202255736Sdavidch{
2203255736Sdavidch    REG_WR(sc, addr, U64_LO(mapping));
2204255736Sdavidch    REG_WR(sc, (addr + 4), U64_HI(mapping));
2205255736Sdavidch}
2206255736Sdavidch
2207255736Sdavidchstatic void
2208255736Sdavidchstorm_memset_spq_addr(struct bxe_softc *sc,
2209255736Sdavidch                      bus_addr_t       mapping,
2210255736Sdavidch                      uint16_t         abs_fid)
2211255736Sdavidch{
2212255736Sdavidch    uint32_t addr = (XSEM_REG_FAST_MEMORY +
2213255736Sdavidch                     XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2214255736Sdavidch    __storm_memset_dma_mapping(sc, addr, mapping);
2215255736Sdavidch}
2216255736Sdavidch
2217255736Sdavidchstatic void
2218255736Sdavidchstorm_memset_vf_to_pf(struct bxe_softc *sc,
2219255736Sdavidch                      uint16_t         abs_fid,
2220255736Sdavidch                      uint16_t         pf_id)
2221255736Sdavidch{
2222255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2223255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2224255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2225255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2226255736Sdavidch}
2227255736Sdavidch
2228255736Sdavidchstatic void
2229255736Sdavidchstorm_memset_func_en(struct bxe_softc *sc,
2230255736Sdavidch                     uint16_t         abs_fid,
2231255736Sdavidch                     uint8_t          enable)
2232255736Sdavidch{
2233255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2234255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2235255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2236255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2237255736Sdavidch}
2238255736Sdavidch
2239255736Sdavidchstatic void
2240255736Sdavidchstorm_memset_eq_data(struct bxe_softc       *sc,
2241255736Sdavidch                     struct event_ring_data *eq_data,
2242255736Sdavidch                     uint16_t               pfid)
2243255736Sdavidch{
2244255736Sdavidch    uint32_t addr;
2245255736Sdavidch    size_t size;
2246255736Sdavidch
2247255736Sdavidch    addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2248255736Sdavidch    size = sizeof(struct event_ring_data);
2249255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2250255736Sdavidch}
2251255736Sdavidch
2252255736Sdavidchstatic void
2253255736Sdavidchstorm_memset_eq_prod(struct bxe_softc *sc,
2254255736Sdavidch                     uint16_t         eq_prod,
2255255736Sdavidch                     uint16_t         pfid)
2256255736Sdavidch{
2257255736Sdavidch    uint32_t addr = (BAR_CSTRORM_INTMEM +
2258255736Sdavidch                     CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2259255736Sdavidch    REG_WR16(sc, addr, eq_prod);
2260255736Sdavidch}
2261255736Sdavidch
2262255736Sdavidch/*
2263255736Sdavidch * Post a slowpath command.
2264255736Sdavidch *
2265298955Spfg * A slowpath command is used to propagate a configuration change through
2266255736Sdavidch * the controller in a controlled manner, allowing each STORM processor and
2267255736Sdavidch * other H/W blocks to phase in the change.  The commands sent on the
2268255736Sdavidch * slowpath are referred to as ramrods.  Depending on the ramrod used the
2269255736Sdavidch * completion of the ramrod will occur in different ways.  Here's a
2270255736Sdavidch * breakdown of ramrods and how they complete:
2271255736Sdavidch *
2272255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_SETUP
2273255736Sdavidch *   Used to setup the leading connection on a port.  Completes on the
2274255736Sdavidch *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2275255736Sdavidch *
2276255736Sdavidch * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2277255736Sdavidch *   Used to setup an additional connection on a port.  Completes on the
2278255736Sdavidch *   RCQ of the multi-queue/RSS connection being initialized.
2279255736Sdavidch *
2280255736Sdavidch * RAMROD_CMD_ID_ETH_STAT_QUERY
2281255736Sdavidch *   Used to force the storm processors to update the statistics database
2282255736Sdavidch *   in host memory.  This ramrod is send on the leading connection CID and
2283255736Sdavidch *   completes as an index increment of the CSTORM on the default status
2284255736Sdavidch *   block.
2285255736Sdavidch *
2286255736Sdavidch * RAMROD_CMD_ID_ETH_UPDATE
2287255736Sdavidch *   Used to update the state of the leading connection, usually to udpate
2288255736Sdavidch *   the RSS indirection table.  Completes on the RCQ of the leading
2289255736Sdavidch *   connection. (Not currently used under FreeBSD until OS support becomes
2290255736Sdavidch *   available.)
2291255736Sdavidch *
2292255736Sdavidch * RAMROD_CMD_ID_ETH_HALT
2293255736Sdavidch *   Used when tearing down a connection prior to driver unload.  Completes
2294255736Sdavidch *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2295255736Sdavidch *   use this on the leading connection.
2296255736Sdavidch *
2297255736Sdavidch * RAMROD_CMD_ID_ETH_SET_MAC
2298255736Sdavidch *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2299255736Sdavidch *   the RCQ of the leading connection.
2300255736Sdavidch *
2301255736Sdavidch * RAMROD_CMD_ID_ETH_CFC_DEL
2302255736Sdavidch *   Used when tearing down a conneciton prior to driver unload.  Completes
2303255736Sdavidch *   on the RCQ of the leading connection (since the current connection
2304255736Sdavidch *   has been completely removed from controller memory).
2305255736Sdavidch *
2306255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_DEL
2307255736Sdavidch *   Used to tear down the leading connection prior to driver unload,
2308255736Sdavidch *   typically fp[0].  Completes as an index increment of the CSTORM on the
2309255736Sdavidch *   default status block.
2310255736Sdavidch *
2311255736Sdavidch * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2312255736Sdavidch *   Used for connection offload.  Completes on the RCQ of the multi-queue
2313255736Sdavidch *   RSS connection that is being offloaded.  (Not currently used under
2314255736Sdavidch *   FreeBSD.)
2315255736Sdavidch *
2316255736Sdavidch * There can only be one command pending per function.
2317255736Sdavidch *
2318255736Sdavidch * Returns:
2319255736Sdavidch *   0 = Success, !0 = Failure.
2320255736Sdavidch */
2321255736Sdavidch
2322255736Sdavidch/* must be called under the spq lock */
2323255736Sdavidchstatic inline
2324255736Sdavidchstruct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2325255736Sdavidch{
2326255736Sdavidch    struct eth_spe *next_spe = sc->spq_prod_bd;
2327255736Sdavidch
2328255736Sdavidch    if (sc->spq_prod_bd == sc->spq_last_bd) {
2329255736Sdavidch        /* wrap back to the first eth_spq */
2330255736Sdavidch        sc->spq_prod_bd = sc->spq;
2331255736Sdavidch        sc->spq_prod_idx = 0;
2332255736Sdavidch    } else {
2333255736Sdavidch        sc->spq_prod_bd++;
2334255736Sdavidch        sc->spq_prod_idx++;
2335255736Sdavidch    }
2336255736Sdavidch
2337255736Sdavidch    return (next_spe);
2338255736Sdavidch}
2339255736Sdavidch
2340255736Sdavidch/* must be called under the spq lock */
2341255736Sdavidchstatic inline
2342255736Sdavidchvoid bxe_sp_prod_update(struct bxe_softc *sc)
2343255736Sdavidch{
2344255736Sdavidch    int func = SC_FUNC(sc);
2345255736Sdavidch
2346255736Sdavidch    /*
2347255736Sdavidch     * Make sure that BD data is updated before writing the producer.
2348255736Sdavidch     * BD data is written to the memory, the producer is read from the
2349255736Sdavidch     * memory, thus we need a full memory barrier to ensure the ordering.
2350255736Sdavidch     */
2351255736Sdavidch    mb();
2352255736Sdavidch
2353255736Sdavidch    REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2354255736Sdavidch             sc->spq_prod_idx);
2355255736Sdavidch
2356255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2357255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
2358255736Sdavidch}
2359255736Sdavidch
2360255736Sdavidch/**
2361255736Sdavidch * bxe_is_contextless_ramrod - check if the current command ends on EQ
2362255736Sdavidch *
2363255736Sdavidch * @cmd:      command to check
2364255736Sdavidch * @cmd_type: command type
2365255736Sdavidch */
2366255736Sdavidchstatic inline
2367255736Sdavidchint bxe_is_contextless_ramrod(int cmd,
2368255736Sdavidch                              int cmd_type)
2369255736Sdavidch{
2370255736Sdavidch    if ((cmd_type == NONE_CONNECTION_TYPE) ||
2371255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2372255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2373255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2374255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2375255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2376255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2377255736Sdavidch        return (TRUE);
2378255736Sdavidch    } else {
2379255736Sdavidch        return (FALSE);
2380255736Sdavidch    }
2381255736Sdavidch}
2382255736Sdavidch
2383255736Sdavidch/**
2384255736Sdavidch * bxe_sp_post - place a single command on an SP ring
2385255736Sdavidch *
2386255736Sdavidch * @sc:         driver handle
2387255736Sdavidch * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2388255736Sdavidch * @cid:        SW CID the command is related to
2389255736Sdavidch * @data_hi:    command private data address (high 32 bits)
2390255736Sdavidch * @data_lo:    command private data address (low 32 bits)
2391255736Sdavidch * @cmd_type:   command type (e.g. NONE, ETH)
2392255736Sdavidch *
2393255736Sdavidch * SP data is handled as if it's always an address pair, thus data fields are
2394255736Sdavidch * not swapped to little endian in upper functions. Instead this function swaps
2395255736Sdavidch * data as if it's two uint32 fields.
2396255736Sdavidch */
2397255736Sdavidchint
2398255736Sdavidchbxe_sp_post(struct bxe_softc *sc,
2399255736Sdavidch            int              command,
2400255736Sdavidch            int              cid,
2401255736Sdavidch            uint32_t         data_hi,
2402255736Sdavidch            uint32_t         data_lo,
2403255736Sdavidch            int              cmd_type)
2404255736Sdavidch{
2405255736Sdavidch    struct eth_spe *spe;
2406255736Sdavidch    uint16_t type;
2407255736Sdavidch    int common;
2408255736Sdavidch
2409255736Sdavidch    common = bxe_is_contextless_ramrod(command, cmd_type);
2410255736Sdavidch
2411255736Sdavidch    BXE_SP_LOCK(sc);
2412255736Sdavidch
2413255736Sdavidch    if (common) {
2414255736Sdavidch        if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2415255736Sdavidch            BLOGE(sc, "EQ ring is full!\n");
2416255736Sdavidch            BXE_SP_UNLOCK(sc);
2417255736Sdavidch            return (-1);
2418255736Sdavidch        }
2419255736Sdavidch    } else {
2420255736Sdavidch        if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2421255736Sdavidch            BLOGE(sc, "SPQ ring is full!\n");
2422255736Sdavidch            BXE_SP_UNLOCK(sc);
2423255736Sdavidch            return (-1);
2424255736Sdavidch        }
2425255736Sdavidch    }
2426255736Sdavidch
2427255736Sdavidch    spe = bxe_sp_get_next(sc);
2428255736Sdavidch
2429255736Sdavidch    /* CID needs port number to be encoded int it */
2430255736Sdavidch    spe->hdr.conn_and_cmd_data =
2431296071Sdavidcs        htole32((command << SPE_HDR_T_CMD_ID_SHIFT) | HW_CID(sc, cid));
2432255736Sdavidch
2433296071Sdavidcs    type = (cmd_type << SPE_HDR_T_CONN_TYPE_SHIFT) & SPE_HDR_T_CONN_TYPE;
2434255736Sdavidch
2435255736Sdavidch    /* TBD: Check if it works for VFs */
2436296071Sdavidcs    type |= ((SC_FUNC(sc) << SPE_HDR_T_FUNCTION_ID_SHIFT) &
2437296071Sdavidcs             SPE_HDR_T_FUNCTION_ID);
2438255736Sdavidch
2439255736Sdavidch    spe->hdr.type = htole16(type);
2440255736Sdavidch
2441255736Sdavidch    spe->data.update_data_addr.hi = htole32(data_hi);
2442255736Sdavidch    spe->data.update_data_addr.lo = htole32(data_lo);
2443255736Sdavidch
2444255736Sdavidch    /*
2445255736Sdavidch     * It's ok if the actual decrement is issued towards the memory
2446255736Sdavidch     * somewhere between the lock and unlock. Thus no more explict
2447255736Sdavidch     * memory barrier is needed.
2448255736Sdavidch     */
2449255736Sdavidch    if (common) {
2450255736Sdavidch        atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2451255736Sdavidch    } else {
2452255736Sdavidch        atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2453255736Sdavidch    }
2454255736Sdavidch
2455256341Sdim    BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2456256341Sdim    BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2457256341Sdim          BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2458255736Sdavidch    BLOGD(sc, DBG_SP,
2459255736Sdavidch          "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2460255736Sdavidch          sc->spq_prod_idx,
2461255736Sdavidch          (uint32_t)U64_HI(sc->spq_dma.paddr),
2462255736Sdavidch          (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2463255736Sdavidch          command,
2464255736Sdavidch          common,
2465255736Sdavidch          HW_CID(sc, cid),
2466255736Sdavidch          data_hi,
2467255736Sdavidch          data_lo,
2468255736Sdavidch          type,
2469255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left),
2470255736Sdavidch          atomic_load_acq_long(&sc->eq_spq_left));
2471255736Sdavidch
2472255736Sdavidch    bxe_sp_prod_update(sc);
2473255736Sdavidch
2474255736Sdavidch    BXE_SP_UNLOCK(sc);
2475255736Sdavidch    return (0);
2476255736Sdavidch}
2477255736Sdavidch
2478255736Sdavidch/**
2479255736Sdavidch * bxe_debug_print_ind_table - prints the indirection table configuration.
2480255736Sdavidch *
2481255736Sdavidch * @sc: driver hanlde
2482255736Sdavidch * @p:  pointer to rss configuration
2483255736Sdavidch */
2484255736Sdavidch
2485255736Sdavidch/*
2486255736Sdavidch * FreeBSD Device probe function.
2487255736Sdavidch *
2488255736Sdavidch * Compares the device found to the driver's list of supported devices and
2489255736Sdavidch * reports back to the bsd loader whether this is the right driver for the device.
2490255736Sdavidch * This is the driver entry function called from the "kldload" command.
2491255736Sdavidch *
2492255736Sdavidch * Returns:
2493255736Sdavidch *   BUS_PROBE_DEFAULT on success, positive value on failure.
2494255736Sdavidch */
2495255736Sdavidchstatic int
2496255736Sdavidchbxe_probe(device_t dev)
2497255736Sdavidch{
2498255736Sdavidch    struct bxe_device_type *t;
2499255736Sdavidch    char *descbuf;
2500255736Sdavidch    uint16_t did, sdid, svid, vid;
2501255736Sdavidch
2502255736Sdavidch    /* Find our device structure */
2503255736Sdavidch    t = bxe_devs;
2504255736Sdavidch
2505255736Sdavidch    /* Get the data for the device to be probed. */
2506255736Sdavidch    vid  = pci_get_vendor(dev);
2507255736Sdavidch    did  = pci_get_device(dev);
2508255736Sdavidch    svid = pci_get_subvendor(dev);
2509255736Sdavidch    sdid = pci_get_subdevice(dev);
2510255736Sdavidch
2511255736Sdavidch    /* Look through the list of known devices for a match. */
2512255736Sdavidch    while (t->bxe_name != NULL) {
2513255736Sdavidch        if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2514255736Sdavidch            ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2515255736Sdavidch            ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2516255736Sdavidch            descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2517255736Sdavidch            if (descbuf == NULL)
2518255736Sdavidch                return (ENOMEM);
2519255736Sdavidch
2520255736Sdavidch            /* Print out the device identity. */
2521255736Sdavidch            snprintf(descbuf, BXE_DEVDESC_MAX,
2522255736Sdavidch                     "%s (%c%d) BXE v:%s\n", t->bxe_name,
2523255736Sdavidch                     (((pci_read_config(dev, PCIR_REVID, 4) &
2524255736Sdavidch                        0xf0) >> 4) + 'A'),
2525255736Sdavidch                     (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2526255736Sdavidch                     BXE_DRIVER_VERSION);
2527255736Sdavidch
2528255736Sdavidch            device_set_desc_copy(dev, descbuf);
2529255736Sdavidch            free(descbuf, M_TEMP);
2530255736Sdavidch            return (BUS_PROBE_DEFAULT);
2531255736Sdavidch        }
2532255736Sdavidch        t++;
2533255736Sdavidch    }
2534255736Sdavidch
2535255736Sdavidch    return (ENXIO);
2536255736Sdavidch}
2537255736Sdavidch
2538255736Sdavidchstatic void
2539255736Sdavidchbxe_init_mutexes(struct bxe_softc *sc)
2540255736Sdavidch{
2541255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2542255736Sdavidch    snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2543255736Sdavidch             "bxe%d_core_lock", sc->unit);
2544255736Sdavidch    sx_init(&sc->core_sx, sc->core_sx_name);
2545255736Sdavidch#else
2546255736Sdavidch    snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2547255736Sdavidch             "bxe%d_core_lock", sc->unit);
2548255736Sdavidch    mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2549255736Sdavidch#endif
2550255736Sdavidch
2551255736Sdavidch    snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2552255736Sdavidch             "bxe%d_sp_lock", sc->unit);
2553255736Sdavidch    mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2554255736Sdavidch
2555255736Sdavidch    snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2556255736Sdavidch             "bxe%d_dmae_lock", sc->unit);
2557255736Sdavidch    mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2558255736Sdavidch
2559255736Sdavidch    snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2560255736Sdavidch             "bxe%d_phy_lock", sc->unit);
2561255736Sdavidch    mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2562255736Sdavidch
2563255736Sdavidch    snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2564255736Sdavidch             "bxe%d_fwmb_lock", sc->unit);
2565255736Sdavidch    mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2566255736Sdavidch
2567255736Sdavidch    snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2568255736Sdavidch             "bxe%d_print_lock", sc->unit);
2569255736Sdavidch    mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2570255736Sdavidch
2571255736Sdavidch    snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2572255736Sdavidch             "bxe%d_stats_lock", sc->unit);
2573255736Sdavidch    mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2574255736Sdavidch
2575255736Sdavidch    snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2576255736Sdavidch             "bxe%d_mcast_lock", sc->unit);
2577255736Sdavidch    mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2578255736Sdavidch}
2579255736Sdavidch
2580255736Sdavidchstatic void
2581255736Sdavidchbxe_release_mutexes(struct bxe_softc *sc)
2582255736Sdavidch{
2583255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2584255736Sdavidch    sx_destroy(&sc->core_sx);
2585255736Sdavidch#else
2586255736Sdavidch    if (mtx_initialized(&sc->core_mtx)) {
2587255736Sdavidch        mtx_destroy(&sc->core_mtx);
2588255736Sdavidch    }
2589255736Sdavidch#endif
2590255736Sdavidch
2591255736Sdavidch    if (mtx_initialized(&sc->sp_mtx)) {
2592255736Sdavidch        mtx_destroy(&sc->sp_mtx);
2593255736Sdavidch    }
2594255736Sdavidch
2595255736Sdavidch    if (mtx_initialized(&sc->dmae_mtx)) {
2596255736Sdavidch        mtx_destroy(&sc->dmae_mtx);
2597255736Sdavidch    }
2598255736Sdavidch
2599255736Sdavidch    if (mtx_initialized(&sc->port.phy_mtx)) {
2600255736Sdavidch        mtx_destroy(&sc->port.phy_mtx);
2601255736Sdavidch    }
2602255736Sdavidch
2603255736Sdavidch    if (mtx_initialized(&sc->fwmb_mtx)) {
2604255736Sdavidch        mtx_destroy(&sc->fwmb_mtx);
2605255736Sdavidch    }
2606255736Sdavidch
2607255736Sdavidch    if (mtx_initialized(&sc->print_mtx)) {
2608255736Sdavidch        mtx_destroy(&sc->print_mtx);
2609255736Sdavidch    }
2610255736Sdavidch
2611255736Sdavidch    if (mtx_initialized(&sc->stats_mtx)) {
2612255736Sdavidch        mtx_destroy(&sc->stats_mtx);
2613255736Sdavidch    }
2614255736Sdavidch
2615255736Sdavidch    if (mtx_initialized(&sc->mcast_mtx)) {
2616255736Sdavidch        mtx_destroy(&sc->mcast_mtx);
2617255736Sdavidch    }
2618255736Sdavidch}
2619255736Sdavidch
2620255736Sdavidchstatic void
2621255736Sdavidchbxe_tx_disable(struct bxe_softc* sc)
2622255736Sdavidch{
2623266979Smarcel    if_t ifp = sc->ifp;
2624255736Sdavidch
2625255736Sdavidch    /* tell the stack the driver is stopped and TX queue is full */
2626266979Smarcel    if (ifp !=  NULL) {
2627266979Smarcel        if_setdrvflags(ifp, 0);
2628255736Sdavidch    }
2629255736Sdavidch}
2630255736Sdavidch
2631255736Sdavidchstatic void
2632255736Sdavidchbxe_drv_pulse(struct bxe_softc *sc)
2633255736Sdavidch{
2634255736Sdavidch    SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2635255736Sdavidch             sc->fw_drv_pulse_wr_seq);
2636255736Sdavidch}
2637255736Sdavidch
2638255736Sdavidchstatic inline uint16_t
2639255736Sdavidchbxe_tx_avail(struct bxe_softc *sc,
2640255736Sdavidch             struct bxe_fastpath *fp)
2641255736Sdavidch{
2642255736Sdavidch    int16_t  used;
2643255736Sdavidch    uint16_t prod;
2644255736Sdavidch    uint16_t cons;
2645255736Sdavidch
2646255736Sdavidch    prod = fp->tx_bd_prod;
2647255736Sdavidch    cons = fp->tx_bd_cons;
2648255736Sdavidch
2649255736Sdavidch    used = SUB_S16(prod, cons);
2650255736Sdavidch
2651255736Sdavidch    return (int16_t)(sc->tx_ring_size) - used;
2652255736Sdavidch}
2653255736Sdavidch
2654255736Sdavidchstatic inline int
2655255736Sdavidchbxe_tx_queue_has_work(struct bxe_fastpath *fp)
2656255736Sdavidch{
2657255736Sdavidch    uint16_t hw_cons;
2658255736Sdavidch
2659255736Sdavidch    mb(); /* status block fields can change */
2660255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
2661255736Sdavidch    return (hw_cons != fp->tx_pkt_cons);
2662255736Sdavidch}
2663255736Sdavidch
2664255736Sdavidchstatic inline uint8_t
2665255736Sdavidchbxe_has_tx_work(struct bxe_fastpath *fp)
2666255736Sdavidch{
2667255736Sdavidch    /* expand this for multi-cos if ever supported */
2668255736Sdavidch    return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2669255736Sdavidch}
2670255736Sdavidch
2671255736Sdavidchstatic inline int
2672255736Sdavidchbxe_has_rx_work(struct bxe_fastpath *fp)
2673255736Sdavidch{
2674255736Sdavidch    uint16_t rx_cq_cons_sb;
2675255736Sdavidch
2676255736Sdavidch    mb(); /* status block fields can change */
2677255736Sdavidch    rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2678255736Sdavidch    if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2679255736Sdavidch        rx_cq_cons_sb++;
2680255736Sdavidch    return (fp->rx_cq_cons != rx_cq_cons_sb);
2681255736Sdavidch}
2682255736Sdavidch
2683255736Sdavidchstatic void
2684255736Sdavidchbxe_sp_event(struct bxe_softc    *sc,
2685255736Sdavidch             struct bxe_fastpath *fp,
2686255736Sdavidch             union eth_rx_cqe    *rr_cqe)
2687255736Sdavidch{
2688255736Sdavidch    int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2689255736Sdavidch    int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2690255736Sdavidch    enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2691255736Sdavidch    struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2692255736Sdavidch
2693255736Sdavidch    BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2694255736Sdavidch          fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2695255736Sdavidch
2696255736Sdavidch    switch (command) {
2697255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2698255736Sdavidch        BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2699255736Sdavidch        drv_cmd = ECORE_Q_CMD_UPDATE;
2700255736Sdavidch        break;
2701255736Sdavidch
2702255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2703255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2704255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP;
2705255736Sdavidch        break;
2706255736Sdavidch
2707255736Sdavidch    case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2708255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2709255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2710255736Sdavidch        break;
2711255736Sdavidch
2712255736Sdavidch    case (RAMROD_CMD_ID_ETH_HALT):
2713255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2714255736Sdavidch        drv_cmd = ECORE_Q_CMD_HALT;
2715255736Sdavidch        break;
2716255736Sdavidch
2717255736Sdavidch    case (RAMROD_CMD_ID_ETH_TERMINATE):
2718255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2719255736Sdavidch        drv_cmd = ECORE_Q_CMD_TERMINATE;
2720255736Sdavidch        break;
2721255736Sdavidch
2722255736Sdavidch    case (RAMROD_CMD_ID_ETH_EMPTY):
2723255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2724255736Sdavidch        drv_cmd = ECORE_Q_CMD_EMPTY;
2725255736Sdavidch        break;
2726255736Sdavidch
2727255736Sdavidch    default:
2728255736Sdavidch        BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2729255736Sdavidch              command, fp->index);
2730255736Sdavidch        return;
2731255736Sdavidch    }
2732255736Sdavidch
2733255736Sdavidch    if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2734255736Sdavidch        q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2735255736Sdavidch        /*
2736255736Sdavidch         * q_obj->complete_cmd() failure means that this was
2737255736Sdavidch         * an unexpected completion.
2738255736Sdavidch         *
2739255736Sdavidch         * In this case we don't want to increase the sc->spq_left
2740255736Sdavidch         * because apparently we haven't sent this command the first
2741255736Sdavidch         * place.
2742255736Sdavidch         */
2743255736Sdavidch        // bxe_panic(sc, ("Unexpected SP completion\n"));
2744255736Sdavidch        return;
2745255736Sdavidch    }
2746255736Sdavidch
2747255736Sdavidch    atomic_add_acq_long(&sc->cq_spq_left, 1);
2748255736Sdavidch
2749255736Sdavidch    BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2750255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left));
2751255736Sdavidch}
2752255736Sdavidch
2753255736Sdavidch/*
2754255736Sdavidch * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2755255736Sdavidch * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2756255736Sdavidch * the current aggregation queue as in-progress.
2757255736Sdavidch */
2758255736Sdavidchstatic void
2759255736Sdavidchbxe_tpa_start(struct bxe_softc            *sc,
2760255736Sdavidch              struct bxe_fastpath         *fp,
2761255736Sdavidch              uint16_t                    queue,
2762255736Sdavidch              uint16_t                    cons,
2763255736Sdavidch              uint16_t                    prod,
2764255736Sdavidch              struct eth_fast_path_rx_cqe *cqe)
2765255736Sdavidch{
2766255736Sdavidch    struct bxe_sw_rx_bd tmp_bd;
2767255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
2768255736Sdavidch    struct eth_rx_bd *rx_bd;
2769255736Sdavidch    int max_agg_queues;
2770255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2771255736Sdavidch    uint16_t index;
2772255736Sdavidch
2773255736Sdavidch    BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2774255736Sdavidch                       "cons=%d prod=%d\n",
2775255736Sdavidch          fp->index, queue, cons, prod);
2776255736Sdavidch
2777255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
2778255736Sdavidch
2779255736Sdavidch    KASSERT((queue < max_agg_queues),
2780255736Sdavidch            ("fp[%02d] invalid aggr queue (%d >= %d)!",
2781255736Sdavidch             fp->index, queue, max_agg_queues));
2782255736Sdavidch
2783255736Sdavidch    KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2784255736Sdavidch            ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2785255736Sdavidch             fp->index, queue));
2786255736Sdavidch
2787255736Sdavidch    /* copy the existing mbuf and mapping from the TPA pool */
2788255736Sdavidch    tmp_bd = tpa_info->bd;
2789255736Sdavidch
2790255736Sdavidch    if (tmp_bd.m == NULL) {
2791295830Sdavidcs        uint32_t *tmp;
2792295830Sdavidcs
2793295830Sdavidcs        tmp = (uint32_t *)cqe;
2794295830Sdavidcs
2795295830Sdavidcs        BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2796295830Sdavidcs              fp->index, queue, cons, prod);
2797295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2798295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2799295830Sdavidcs
2800255736Sdavidch        /* XXX Error handling? */
2801255736Sdavidch        return;
2802255736Sdavidch    }
2803255736Sdavidch
2804255736Sdavidch    /* change the TPA queue to the start state */
2805255736Sdavidch    tpa_info->state            = BXE_TPA_STATE_START;
2806255736Sdavidch    tpa_info->placement_offset = cqe->placement_offset;
2807255736Sdavidch    tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2808255736Sdavidch    tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2809255736Sdavidch    tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2810255736Sdavidch
2811255736Sdavidch    fp->rx_tpa_queue_used |= (1 << queue);
2812255736Sdavidch
2813255736Sdavidch    /*
2814255736Sdavidch     * If all the buffer descriptors are filled with mbufs then fill in
2815255736Sdavidch     * the current consumer index with a new BD. Else if a maximum Rx
2816255736Sdavidch     * buffer limit is imposed then fill in the next producer index.
2817255736Sdavidch     */
2818255736Sdavidch    index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2819255736Sdavidch                prod : cons;
2820255736Sdavidch
2821255736Sdavidch    /* move the received mbuf and mapping to TPA pool */
2822255736Sdavidch    tpa_info->bd = fp->rx_mbuf_chain[cons];
2823255736Sdavidch
2824255736Sdavidch    /* release any existing RX BD mbuf mappings */
2825255736Sdavidch    if (cons != index) {
2826255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[cons];
2827255736Sdavidch
2828255736Sdavidch        if (rx_buf->m_map != NULL) {
2829255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2830255736Sdavidch                            BUS_DMASYNC_POSTREAD);
2831255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2832255736Sdavidch        }
2833255736Sdavidch
2834255736Sdavidch        /*
2835255736Sdavidch         * We get here when the maximum number of rx buffers is less than
2836255736Sdavidch         * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2837255736Sdavidch         * it out here without concern of a memory leak.
2838255736Sdavidch         */
2839255736Sdavidch        fp->rx_mbuf_chain[cons].m = NULL;
2840255736Sdavidch    }
2841255736Sdavidch
2842255736Sdavidch    /* update the Rx SW BD with the mbuf info from the TPA pool */
2843255736Sdavidch    fp->rx_mbuf_chain[index] = tmp_bd;
2844255736Sdavidch
2845255736Sdavidch    /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2846255736Sdavidch    rx_bd = &fp->rx_chain[index];
2847255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2848255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2849255736Sdavidch}
2850255736Sdavidch
2851255736Sdavidch/*
2852255736Sdavidch * When a TPA aggregation is completed, loop through the individual mbufs
2853255736Sdavidch * of the aggregation, combining them into a single mbuf which will be sent
2854255736Sdavidch * up the stack. Refill all freed SGEs with mbufs as we go along.
2855255736Sdavidch */
2856255736Sdavidchstatic int
2857255736Sdavidchbxe_fill_frag_mbuf(struct bxe_softc          *sc,
2858255736Sdavidch                   struct bxe_fastpath       *fp,
2859255736Sdavidch                   struct bxe_sw_tpa_info    *tpa_info,
2860255736Sdavidch                   uint16_t                  queue,
2861255736Sdavidch                   uint16_t                  pages,
2862255736Sdavidch                   struct mbuf               *m,
2863255736Sdavidch			       struct eth_end_agg_rx_cqe *cqe,
2864255736Sdavidch                   uint16_t                  cqe_idx)
2865255736Sdavidch{
2866255736Sdavidch    struct mbuf *m_frag;
2867255736Sdavidch    uint32_t frag_len, frag_size, i;
2868255736Sdavidch    uint16_t sge_idx;
2869255736Sdavidch    int rc = 0;
2870255736Sdavidch    int j;
2871255736Sdavidch
2872255736Sdavidch    frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2873255736Sdavidch
2874255736Sdavidch    BLOGD(sc, DBG_LRO,
2875255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2876255736Sdavidch          fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2877255736Sdavidch
2878255736Sdavidch    /* make sure the aggregated frame is not too big to handle */
2879255736Sdavidch    if (pages > 8 * PAGES_PER_SGE) {
2880295830Sdavidcs
2881295830Sdavidcs        uint32_t *tmp = (uint32_t *)cqe;
2882295830Sdavidcs
2883255736Sdavidch        BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2884255736Sdavidch                  "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2885255736Sdavidch              fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2886255736Sdavidch              tpa_info->len_on_bd, frag_size);
2887295830Sdavidcs
2888295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2889295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2890295830Sdavidcs
2891255736Sdavidch        bxe_panic(sc, ("sge page count error\n"));
2892255736Sdavidch        return (EINVAL);
2893255736Sdavidch    }
2894255736Sdavidch
2895255736Sdavidch    /*
2896255736Sdavidch     * Scan through the scatter gather list pulling individual mbufs into a
2897255736Sdavidch     * single mbuf for the host stack.
2898255736Sdavidch     */
2899255736Sdavidch    for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2900255736Sdavidch        sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2901255736Sdavidch
2902255736Sdavidch        /*
2903255736Sdavidch         * Firmware gives the indices of the SGE as if the ring is an array
2904255736Sdavidch         * (meaning that the "next" element will consume 2 indices).
2905255736Sdavidch         */
2906255736Sdavidch        frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2907255736Sdavidch
2908255736Sdavidch        BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2909255736Sdavidch                           "sge_idx=%d frag_size=%d frag_len=%d\n",
2910255736Sdavidch              fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2911255736Sdavidch
2912255736Sdavidch        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2913255736Sdavidch
2914255736Sdavidch        /* allocate a new mbuf for the SGE */
2915255736Sdavidch        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2916255736Sdavidch        if (rc) {
2917255736Sdavidch            /* Leave all remaining SGEs in the ring! */
2918255736Sdavidch            return (rc);
2919255736Sdavidch        }
2920255736Sdavidch
2921255736Sdavidch        /* update the fragment length */
2922255736Sdavidch        m_frag->m_len = frag_len;
2923255736Sdavidch
2924255736Sdavidch        /* concatenate the fragment to the head mbuf */
2925255736Sdavidch        m_cat(m, m_frag);
2926255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
2927255736Sdavidch
2928255736Sdavidch        /* update the TPA mbuf size and remaining fragment size */
2929255736Sdavidch        m->m_pkthdr.len += frag_len;
2930255736Sdavidch        frag_size -= frag_len;
2931255736Sdavidch    }
2932255736Sdavidch
2933255736Sdavidch    BLOGD(sc, DBG_LRO,
2934255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2935255736Sdavidch          fp->index, queue, frag_size);
2936255736Sdavidch
2937255736Sdavidch    return (rc);
2938255736Sdavidch}
2939255736Sdavidch
2940255736Sdavidchstatic inline void
2941255736Sdavidchbxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2942255736Sdavidch{
2943255736Sdavidch    int i, j;
2944255736Sdavidch
2945255736Sdavidch    for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2946255736Sdavidch        int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2947255736Sdavidch
2948255736Sdavidch        for (j = 0; j < 2; j++) {
2949255736Sdavidch            BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2950255736Sdavidch            idx--;
2951255736Sdavidch        }
2952255736Sdavidch    }
2953255736Sdavidch}
2954255736Sdavidch
2955255736Sdavidchstatic inline void
2956255736Sdavidchbxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2957255736Sdavidch{
2958255736Sdavidch    /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2959255736Sdavidch    memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2960255736Sdavidch
2961255736Sdavidch    /*
2962255736Sdavidch     * Clear the two last indices in the page to 1. These are the indices that
2963255736Sdavidch     * correspond to the "next" element, hence will never be indicated and
2964255736Sdavidch     * should be removed from the calculations.
2965255736Sdavidch     */
2966255736Sdavidch    bxe_clear_sge_mask_next_elems(fp);
2967255736Sdavidch}
2968255736Sdavidch
2969255736Sdavidchstatic inline void
2970255736Sdavidchbxe_update_last_max_sge(struct bxe_fastpath *fp,
2971255736Sdavidch                        uint16_t            idx)
2972255736Sdavidch{
2973255736Sdavidch    uint16_t last_max = fp->last_max_sge;
2974255736Sdavidch
2975255736Sdavidch    if (SUB_S16(idx, last_max) > 0) {
2976255736Sdavidch        fp->last_max_sge = idx;
2977255736Sdavidch    }
2978255736Sdavidch}
2979255736Sdavidch
2980255736Sdavidchstatic inline void
2981255736Sdavidchbxe_update_sge_prod(struct bxe_softc          *sc,
2982255736Sdavidch                    struct bxe_fastpath       *fp,
2983255736Sdavidch                    uint16_t                  sge_len,
2984283269Sdavidcs                    union eth_sgl_or_raw_data *cqe)
2985255736Sdavidch{
2986255736Sdavidch    uint16_t last_max, last_elem, first_elem;
2987255736Sdavidch    uint16_t delta = 0;
2988255736Sdavidch    uint16_t i;
2989255736Sdavidch
2990255736Sdavidch    if (!sge_len) {
2991255736Sdavidch        return;
2992255736Sdavidch    }
2993255736Sdavidch
2994255736Sdavidch    /* first mark all used pages */
2995255736Sdavidch    for (i = 0; i < sge_len; i++) {
2996255736Sdavidch        BIT_VEC64_CLEAR_BIT(fp->sge_mask,
2997283269Sdavidcs                            RX_SGE(le16toh(cqe->sgl[i])));
2998255736Sdavidch    }
2999255736Sdavidch
3000255736Sdavidch    BLOGD(sc, DBG_LRO,
3001255736Sdavidch          "fp[%02d] fp_cqe->sgl[%d] = %d\n",
3002255736Sdavidch          fp->index, sge_len - 1,
3003283269Sdavidcs          le16toh(cqe->sgl[sge_len - 1]));
3004255736Sdavidch
3005255736Sdavidch    /* assume that the last SGE index is the biggest */
3006255736Sdavidch    bxe_update_last_max_sge(fp,
3007283269Sdavidcs                            le16toh(cqe->sgl[sge_len - 1]));
3008255736Sdavidch
3009255736Sdavidch    last_max = RX_SGE(fp->last_max_sge);
3010255736Sdavidch    last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
3011255736Sdavidch    first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
3012255736Sdavidch
3013255736Sdavidch    /* if ring is not full */
3014255736Sdavidch    if (last_elem + 1 != first_elem) {
3015255736Sdavidch        last_elem++;
3016255736Sdavidch    }
3017255736Sdavidch
3018255736Sdavidch    /* now update the prod */
3019255736Sdavidch    for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
3020255736Sdavidch        if (__predict_true(fp->sge_mask[i])) {
3021255736Sdavidch            break;
3022255736Sdavidch        }
3023255736Sdavidch
3024255736Sdavidch        fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
3025255736Sdavidch        delta += BIT_VEC64_ELEM_SZ;
3026255736Sdavidch    }
3027255736Sdavidch
3028255736Sdavidch    if (delta > 0) {
3029255736Sdavidch        fp->rx_sge_prod += delta;
3030255736Sdavidch        /* clear page-end entries */
3031255736Sdavidch        bxe_clear_sge_mask_next_elems(fp);
3032255736Sdavidch    }
3033255736Sdavidch
3034255736Sdavidch    BLOGD(sc, DBG_LRO,
3035255736Sdavidch          "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
3036255736Sdavidch          fp->index, fp->last_max_sge, fp->rx_sge_prod);
3037255736Sdavidch}
3038255736Sdavidch
3039255736Sdavidch/*
3040255736Sdavidch * The aggregation on the current TPA queue has completed. Pull the individual
3041255736Sdavidch * mbuf fragments together into a single mbuf, perform all necessary checksum
3042255736Sdavidch * calculations, and send the resuting mbuf to the stack.
3043255736Sdavidch */
3044255736Sdavidchstatic void
3045255736Sdavidchbxe_tpa_stop(struct bxe_softc          *sc,
3046255736Sdavidch             struct bxe_fastpath       *fp,
3047255736Sdavidch             struct bxe_sw_tpa_info    *tpa_info,
3048255736Sdavidch             uint16_t                  queue,
3049255736Sdavidch             uint16_t                  pages,
3050255736Sdavidch			 struct eth_end_agg_rx_cqe *cqe,
3051255736Sdavidch             uint16_t                  cqe_idx)
3052255736Sdavidch{
3053266979Smarcel    if_t ifp = sc->ifp;
3054255736Sdavidch    struct mbuf *m;
3055255736Sdavidch    int rc = 0;
3056255736Sdavidch
3057255736Sdavidch    BLOGD(sc, DBG_LRO,
3058255736Sdavidch          "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3059255736Sdavidch          fp->index, queue, tpa_info->placement_offset,
3060255736Sdavidch          le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3061255736Sdavidch
3062255736Sdavidch    m = tpa_info->bd.m;
3063255736Sdavidch
3064255736Sdavidch    /* allocate a replacement before modifying existing mbuf */
3065255736Sdavidch    rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3066255736Sdavidch    if (rc) {
3067255736Sdavidch        /* drop the frame and log an error */
3068255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3069255736Sdavidch        goto bxe_tpa_stop_exit;
3070255736Sdavidch    }
3071255736Sdavidch
3072255736Sdavidch    /* we have a replacement, fixup the current mbuf */
3073255736Sdavidch    m_adj(m, tpa_info->placement_offset);
3074255736Sdavidch    m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3075255736Sdavidch
3076255736Sdavidch    /* mark the checksums valid (taken care of by the firmware) */
3077255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3078255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3079255736Sdavidch    m->m_pkthdr.csum_data = 0xffff;
3080255736Sdavidch    m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3081255736Sdavidch                               CSUM_IP_VALID   |
3082255736Sdavidch                               CSUM_DATA_VALID |
3083255736Sdavidch                               CSUM_PSEUDO_HDR);
3084255736Sdavidch
3085255736Sdavidch    /* aggregate all of the SGEs into a single mbuf */
3086255736Sdavidch    rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3087255736Sdavidch    if (rc) {
3088255736Sdavidch        /* drop the packet and log an error */
3089255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3090255736Sdavidch        m_freem(m);
3091255736Sdavidch    } else {
3092296071Sdavidcs        if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3093255736Sdavidch            m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3094255736Sdavidch            m->m_flags |= M_VLANTAG;
3095255736Sdavidch        }
3096255736Sdavidch
3097255736Sdavidch        /* assign packet to this interface interface */
3098266979Smarcel        if_setrcvif(m, ifp);
3099255736Sdavidch
3100255736Sdavidch#if __FreeBSD_version >= 800000
3101255736Sdavidch        /* specify what RSS queue was used for this flow */
3102255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3103297155Sdavidcs        BXE_SET_FLOWID(m);
3104255736Sdavidch#endif
3105255736Sdavidch
3106271782Sglebius        if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3107255736Sdavidch        fp->eth_q_stats.rx_tpa_pkts++;
3108255736Sdavidch
3109255736Sdavidch        /* pass the frame to the stack */
3110266979Smarcel        if_input(ifp, m);
3111255736Sdavidch    }
3112255736Sdavidch
3113255736Sdavidch    /* we passed an mbuf up the stack or dropped the frame */
3114255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa--;
3115255736Sdavidch
3116255736Sdavidchbxe_tpa_stop_exit:
3117255736Sdavidch
3118255736Sdavidch    fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3119255736Sdavidch    fp->rx_tpa_queue_used &= ~(1 << queue);
3120255736Sdavidch}
3121255736Sdavidch
3122255736Sdavidchstatic uint8_t
3123283269Sdavidcsbxe_service_rxsgl(
3124283269Sdavidcs                 struct bxe_fastpath *fp,
3125283269Sdavidcs                 uint16_t len,
3126283269Sdavidcs                 uint16_t lenonbd,
3127283269Sdavidcs                 struct mbuf *m,
3128283269Sdavidcs                 struct eth_fast_path_rx_cqe *cqe_fp)
3129283269Sdavidcs{
3130283269Sdavidcs    struct mbuf *m_frag;
3131283269Sdavidcs    uint16_t frags, frag_len;
3132283269Sdavidcs    uint16_t sge_idx = 0;
3133283269Sdavidcs    uint16_t j;
3134283269Sdavidcs    uint8_t i, rc = 0;
3135283269Sdavidcs    uint32_t frag_size;
3136283269Sdavidcs
3137283269Sdavidcs    /* adjust the mbuf */
3138283269Sdavidcs    m->m_len = lenonbd;
3139283269Sdavidcs
3140283269Sdavidcs    frag_size =  len - lenonbd;
3141283269Sdavidcs    frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3142283269Sdavidcs
3143283269Sdavidcs    for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3144283269Sdavidcs        sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3145283269Sdavidcs
3146283269Sdavidcs        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3147283269Sdavidcs        frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3148283269Sdavidcs        m_frag->m_len = frag_len;
3149283269Sdavidcs
3150283269Sdavidcs       /* allocate a new mbuf for the SGE */
3151283269Sdavidcs        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3152283269Sdavidcs        if (rc) {
3153283269Sdavidcs            /* Leave all remaining SGEs in the ring! */
3154283269Sdavidcs            return (rc);
3155283269Sdavidcs        }
3156283269Sdavidcs        fp->eth_q_stats.mbuf_alloc_sge--;
3157283269Sdavidcs
3158283269Sdavidcs        /* concatenate the fragment to the head mbuf */
3159283269Sdavidcs        m_cat(m, m_frag);
3160283269Sdavidcs
3161283269Sdavidcs        frag_size -= frag_len;
3162283269Sdavidcs    }
3163283269Sdavidcs
3164283269Sdavidcs    bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3165283269Sdavidcs
3166283269Sdavidcs    return rc;
3167283269Sdavidcs}
3168283269Sdavidcs
3169283269Sdavidcsstatic uint8_t
3170255736Sdavidchbxe_rxeof(struct bxe_softc    *sc,
3171255736Sdavidch          struct bxe_fastpath *fp)
3172255736Sdavidch{
3173266979Smarcel    if_t ifp = sc->ifp;
3174255736Sdavidch    uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3175255736Sdavidch    uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3176255736Sdavidch    int rx_pkts = 0;
3177281006Sdavidcs    int rc = 0;
3178255736Sdavidch
3179255736Sdavidch    BXE_FP_RX_LOCK(fp);
3180255736Sdavidch
3181255736Sdavidch    /* CQ "next element" is of the size of the regular element */
3182255736Sdavidch    hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3183255736Sdavidch    if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3184255736Sdavidch        hw_cq_cons++;
3185255736Sdavidch    }
3186255736Sdavidch
3187255736Sdavidch    bd_cons = fp->rx_bd_cons;
3188255736Sdavidch    bd_prod = fp->rx_bd_prod;
3189255736Sdavidch    bd_prod_fw = bd_prod;
3190255736Sdavidch    sw_cq_cons = fp->rx_cq_cons;
3191255736Sdavidch    sw_cq_prod = fp->rx_cq_prod;
3192255736Sdavidch
3193255736Sdavidch    /*
3194255736Sdavidch     * Memory barrier necessary as speculative reads of the rx
3195255736Sdavidch     * buffer can be ahead of the index in the status block
3196255736Sdavidch     */
3197255736Sdavidch    rmb();
3198255736Sdavidch
3199255736Sdavidch    BLOGD(sc, DBG_RX,
3200255736Sdavidch          "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3201255736Sdavidch          fp->index, hw_cq_cons, sw_cq_cons);
3202255736Sdavidch
3203255736Sdavidch    while (sw_cq_cons != hw_cq_cons) {
3204255736Sdavidch        struct bxe_sw_rx_bd *rx_buf = NULL;
3205255736Sdavidch        union eth_rx_cqe *cqe;
3206255736Sdavidch        struct eth_fast_path_rx_cqe *cqe_fp;
3207255736Sdavidch        uint8_t cqe_fp_flags;
3208255736Sdavidch        enum eth_rx_cqe_type cqe_fp_type;
3209283269Sdavidcs        uint16_t len, lenonbd,  pad;
3210255736Sdavidch        struct mbuf *m = NULL;
3211255736Sdavidch
3212255736Sdavidch        comp_ring_cons = RCQ(sw_cq_cons);
3213255736Sdavidch        bd_prod = RX_BD(bd_prod);
3214255736Sdavidch        bd_cons = RX_BD(bd_cons);
3215255736Sdavidch
3216255736Sdavidch        cqe          = &fp->rcq_chain[comp_ring_cons];
3217255736Sdavidch        cqe_fp       = &cqe->fast_path_cqe;
3218255736Sdavidch        cqe_fp_flags = cqe_fp->type_error_flags;
3219255736Sdavidch        cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3220255736Sdavidch
3221255736Sdavidch        BLOGD(sc, DBG_RX,
3222255736Sdavidch              "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3223255736Sdavidch              "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3224283269Sdavidcs              "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3225255736Sdavidch              fp->index,
3226255736Sdavidch              hw_cq_cons,
3227255736Sdavidch              sw_cq_cons,
3228255736Sdavidch              bd_prod,
3229255736Sdavidch              bd_cons,
3230255736Sdavidch              CQE_TYPE(cqe_fp_flags),
3231255736Sdavidch              cqe_fp_flags,
3232255736Sdavidch              cqe_fp->status_flags,
3233255736Sdavidch              le32toh(cqe_fp->rss_hash_result),
3234255736Sdavidch              le16toh(cqe_fp->vlan_tag),
3235283269Sdavidcs              le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3236283269Sdavidcs              le16toh(cqe_fp->len_on_bd));
3237255736Sdavidch
3238255736Sdavidch        /* is this a slowpath msg? */
3239255736Sdavidch        if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3240255736Sdavidch            bxe_sp_event(sc, fp, cqe);
3241255736Sdavidch            goto next_cqe;
3242255736Sdavidch        }
3243255736Sdavidch
3244255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[bd_cons];
3245255736Sdavidch
3246255736Sdavidch        if (!CQE_TYPE_FAST(cqe_fp_type)) {
3247255736Sdavidch            struct bxe_sw_tpa_info *tpa_info;
3248255736Sdavidch            uint16_t frag_size, pages;
3249255736Sdavidch            uint8_t queue;
3250255736Sdavidch
3251255736Sdavidch            if (CQE_TYPE_START(cqe_fp_type)) {
3252255736Sdavidch                bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3253255736Sdavidch                              bd_cons, bd_prod, cqe_fp);
3254255736Sdavidch                m = NULL; /* packet not ready yet */
3255255736Sdavidch                goto next_rx;
3256255736Sdavidch            }
3257255736Sdavidch
3258255736Sdavidch            KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3259255736Sdavidch                    ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3260255736Sdavidch
3261255736Sdavidch            queue = cqe->end_agg_cqe.queue_index;
3262255736Sdavidch            tpa_info = &fp->rx_tpa_info[queue];
3263255736Sdavidch
3264255736Sdavidch            BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3265255736Sdavidch                  fp->index, queue);
3266255736Sdavidch
3267255736Sdavidch            frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3268255736Sdavidch                         tpa_info->len_on_bd);
3269255736Sdavidch            pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3270255736Sdavidch
3271255736Sdavidch            bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3272255736Sdavidch                         &cqe->end_agg_cqe, comp_ring_cons);
3273255736Sdavidch
3274283269Sdavidcs            bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3275255736Sdavidch
3276255736Sdavidch            goto next_cqe;
3277255736Sdavidch        }
3278255736Sdavidch
3279255736Sdavidch        /* non TPA */
3280255736Sdavidch
3281255736Sdavidch        /* is this an error packet? */
3282255736Sdavidch        if (__predict_false(cqe_fp_flags &
3283255736Sdavidch                            ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3284255736Sdavidch            BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3285255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3286255736Sdavidch            goto next_rx;
3287255736Sdavidch        }
3288255736Sdavidch
3289255736Sdavidch        len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3290283269Sdavidcs        lenonbd = le16toh(cqe_fp->len_on_bd);
3291255736Sdavidch        pad = cqe_fp->placement_offset;
3292255736Sdavidch
3293255736Sdavidch        m = rx_buf->m;
3294255736Sdavidch
3295255736Sdavidch        if (__predict_false(m == NULL)) {
3296255736Sdavidch            BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3297255736Sdavidch                  bd_cons, fp->index);
3298255736Sdavidch            goto next_rx;
3299255736Sdavidch        }
3300255736Sdavidch
3301255736Sdavidch        /* XXX double copy if packet length under a threshold */
3302255736Sdavidch
3303255736Sdavidch        /*
3304255736Sdavidch         * If all the buffer descriptors are filled with mbufs then fill in
3305255736Sdavidch         * the current consumer index with a new BD. Else if a maximum Rx
3306255736Sdavidch         * buffer limit is imposed then fill in the next producer index.
3307255736Sdavidch         */
3308255736Sdavidch        rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3309255736Sdavidch                                  (sc->max_rx_bufs != RX_BD_USABLE) ?
3310255736Sdavidch                                      bd_prod : bd_cons);
3311255736Sdavidch        if (rc != 0) {
3312281006Sdavidcs
3313281006Sdavidcs            /* we simply reuse the received mbuf and don't post it to the stack */
3314281006Sdavidcs            m = NULL;
3315281006Sdavidcs
3316255736Sdavidch            BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3317255736Sdavidch                  fp->index, rc);
3318255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3319255736Sdavidch
3320255736Sdavidch            if (sc->max_rx_bufs != RX_BD_USABLE) {
3321255736Sdavidch                /* copy this consumer index to the producer index */
3322255736Sdavidch                memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3323255736Sdavidch                       sizeof(struct bxe_sw_rx_bd));
3324255736Sdavidch                memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3325255736Sdavidch            }
3326255736Sdavidch
3327255736Sdavidch            goto next_rx;
3328255736Sdavidch        }
3329255736Sdavidch
3330255736Sdavidch        /* current mbuf was detached from the bd */
3331255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
3332255736Sdavidch
3333255736Sdavidch        /* we allocated a replacement mbuf, fixup the current one */
3334255736Sdavidch        m_adj(m, pad);
3335255736Sdavidch        m->m_pkthdr.len = m->m_len = len;
3336255736Sdavidch
3337292638Sdavidcs        if ((len > 60) && (len > lenonbd)) {
3338292638Sdavidcs            fp->eth_q_stats.rx_bxe_service_rxsgl++;
3339283269Sdavidcs            rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3340283269Sdavidcs            if (rc)
3341283269Sdavidcs                break;
3342283274Sdavidcs            fp->eth_q_stats.rx_jumbo_sge_pkts++;
3343292638Sdavidcs        } else if (lenonbd < len) {
3344292638Sdavidcs            fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3345283269Sdavidcs        }
3346283269Sdavidcs
3347255736Sdavidch        /* assign packet to this interface interface */
3348266979Smarcel	if_setrcvif(m, ifp);
3349255736Sdavidch
3350255736Sdavidch        /* assume no hardware checksum has complated */
3351255736Sdavidch        m->m_pkthdr.csum_flags = 0;
3352255736Sdavidch
3353255736Sdavidch        /* validate checksum if offload enabled */
3354266979Smarcel        if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3355255736Sdavidch            /* check for a valid IP frame */
3356255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3357255736Sdavidch                  ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3358255736Sdavidch                m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3359255736Sdavidch                if (__predict_false(cqe_fp_flags &
3360255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3361255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3362255736Sdavidch                } else {
3363255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3364255736Sdavidch                    m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3365255736Sdavidch                }
3366255736Sdavidch            }
3367255736Sdavidch
3368255736Sdavidch            /* check for a valid TCP/UDP frame */
3369255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3370255736Sdavidch                  ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3371255736Sdavidch                if (__predict_false(cqe_fp_flags &
3372255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3373255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3374255736Sdavidch                } else {
3375255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3376255736Sdavidch                    m->m_pkthdr.csum_data = 0xFFFF;
3377255736Sdavidch                    m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3378255736Sdavidch                                               CSUM_PSEUDO_HDR);
3379255736Sdavidch                }
3380255736Sdavidch            }
3381255736Sdavidch        }
3382255736Sdavidch
3383255736Sdavidch        /* if there is a VLAN tag then flag that info */
3384296071Sdavidcs        if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3385255736Sdavidch            m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3386255736Sdavidch            m->m_flags |= M_VLANTAG;
3387255736Sdavidch        }
3388255736Sdavidch
3389255736Sdavidch#if __FreeBSD_version >= 800000
3390255736Sdavidch        /* specify what RSS queue was used for this flow */
3391255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3392297155Sdavidcs        BXE_SET_FLOWID(m);
3393255736Sdavidch#endif
3394255736Sdavidch
3395255736Sdavidchnext_rx:
3396255736Sdavidch
3397255736Sdavidch        bd_cons    = RX_BD_NEXT(bd_cons);
3398255736Sdavidch        bd_prod    = RX_BD_NEXT(bd_prod);
3399255736Sdavidch        bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3400255736Sdavidch
3401255736Sdavidch        /* pass the frame to the stack */
3402255736Sdavidch        if (__predict_true(m != NULL)) {
3403271782Sglebius            if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3404255736Sdavidch            rx_pkts++;
3405266979Smarcel            if_input(ifp, m);
3406255736Sdavidch        }
3407255736Sdavidch
3408255736Sdavidchnext_cqe:
3409255736Sdavidch
3410255736Sdavidch        sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3411255736Sdavidch        sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3412255736Sdavidch
3413255736Sdavidch        /* limit spinning on the queue */
3414281006Sdavidcs        if (rc != 0)
3415281006Sdavidcs            break;
3416281006Sdavidcs
3417255736Sdavidch        if (rx_pkts == sc->rx_budget) {
3418255736Sdavidch            fp->eth_q_stats.rx_budget_reached++;
3419255736Sdavidch            break;
3420255736Sdavidch        }
3421255736Sdavidch    } /* while work to do */
3422255736Sdavidch
3423255736Sdavidch    fp->rx_bd_cons = bd_cons;
3424255736Sdavidch    fp->rx_bd_prod = bd_prod_fw;
3425255736Sdavidch    fp->rx_cq_cons = sw_cq_cons;
3426255736Sdavidch    fp->rx_cq_prod = sw_cq_prod;
3427255736Sdavidch
3428255736Sdavidch    /* Update producers */
3429255736Sdavidch    bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3430255736Sdavidch
3431255736Sdavidch    fp->eth_q_stats.rx_pkts += rx_pkts;
3432255736Sdavidch    fp->eth_q_stats.rx_calls++;
3433255736Sdavidch
3434255736Sdavidch    BXE_FP_RX_UNLOCK(fp);
3435255736Sdavidch
3436255736Sdavidch    return (sw_cq_cons != hw_cq_cons);
3437255736Sdavidch}
3438255736Sdavidch
3439255736Sdavidchstatic uint16_t
3440255736Sdavidchbxe_free_tx_pkt(struct bxe_softc    *sc,
3441255736Sdavidch                struct bxe_fastpath *fp,
3442255736Sdavidch                uint16_t            idx)
3443255736Sdavidch{
3444255736Sdavidch    struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3445255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
3446255736Sdavidch    uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3447255736Sdavidch    uint16_t new_cons;
3448255736Sdavidch    int nbd;
3449255736Sdavidch
3450255736Sdavidch    /* unmap the mbuf from non-paged memory */
3451255736Sdavidch    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3452255736Sdavidch
3453255736Sdavidch    tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3454255736Sdavidch    nbd = le16toh(tx_start_bd->nbd) - 1;
3455255736Sdavidch
3456255736Sdavidch    new_cons = (tx_buf->first_bd + nbd);
3457255736Sdavidch
3458255736Sdavidch    /* free the mbuf */
3459255736Sdavidch    if (__predict_true(tx_buf->m != NULL)) {
3460255736Sdavidch        m_freem(tx_buf->m);
3461255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx--;
3462255736Sdavidch    } else {
3463255736Sdavidch        fp->eth_q_stats.tx_chain_lost_mbuf++;
3464255736Sdavidch    }
3465255736Sdavidch
3466255736Sdavidch    tx_buf->m = NULL;
3467255736Sdavidch    tx_buf->first_bd = 0;
3468255736Sdavidch
3469255736Sdavidch    return (new_cons);
3470255736Sdavidch}
3471255736Sdavidch
3472255736Sdavidch/* transmit timeout watchdog */
3473255736Sdavidchstatic int
3474255736Sdavidchbxe_watchdog(struct bxe_softc    *sc,
3475255736Sdavidch             struct bxe_fastpath *fp)
3476255736Sdavidch{
3477255736Sdavidch    BXE_FP_TX_LOCK(fp);
3478255736Sdavidch
3479255736Sdavidch    if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3480255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
3481255736Sdavidch        return (0);
3482255736Sdavidch    }
3483255736Sdavidch
3484258187Sedavis    BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3485298294Sdavidcs    if(sc->trigger_grcdump) {
3486298294Sdavidcs         /* taking grcdump */
3487298294Sdavidcs         bxe_grc_dump(sc);
3488298294Sdavidcs    }
3489258187Sedavis
3490255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
3491255736Sdavidch
3492255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_REINIT);
3493255736Sdavidch    taskqueue_enqueue(sc->chip_tq, &sc->chip_tq_task);
3494255736Sdavidch
3495255736Sdavidch    return (-1);
3496255736Sdavidch}
3497255736Sdavidch
3498255736Sdavidch/* processes transmit completions */
3499255736Sdavidchstatic uint8_t
3500255736Sdavidchbxe_txeof(struct bxe_softc    *sc,
3501255736Sdavidch          struct bxe_fastpath *fp)
3502255736Sdavidch{
3503266979Smarcel    if_t ifp = sc->ifp;
3504255736Sdavidch    uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3505255736Sdavidch    uint16_t tx_bd_avail;
3506255736Sdavidch
3507255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
3508255736Sdavidch
3509255736Sdavidch    bd_cons = fp->tx_bd_cons;
3510255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
3511255736Sdavidch    sw_cons = fp->tx_pkt_cons;
3512255736Sdavidch
3513255736Sdavidch    while (sw_cons != hw_cons) {
3514255736Sdavidch        pkt_cons = TX_BD(sw_cons);
3515255736Sdavidch
3516255736Sdavidch        BLOGD(sc, DBG_TX,
3517255736Sdavidch              "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3518255736Sdavidch              fp->index, hw_cons, sw_cons, pkt_cons);
3519255736Sdavidch
3520255736Sdavidch        bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3521255736Sdavidch
3522255736Sdavidch        sw_cons++;
3523255736Sdavidch    }
3524255736Sdavidch
3525255736Sdavidch    fp->tx_pkt_cons = sw_cons;
3526255736Sdavidch    fp->tx_bd_cons  = bd_cons;
3527255736Sdavidch
3528255736Sdavidch    BLOGD(sc, DBG_TX,
3529255736Sdavidch          "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3530255736Sdavidch          fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3531255736Sdavidch
3532255736Sdavidch    mb();
3533255736Sdavidch
3534255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
3535255736Sdavidch
3536258187Sedavis    if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3537266979Smarcel        if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
3538258187Sedavis    } else {
3539266979Smarcel        if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3540258187Sedavis    }
3541255736Sdavidch
3542258187Sedavis    if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3543258187Sedavis        /* reset the watchdog timer if there are pending transmits */
3544255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
3545258187Sedavis        return (TRUE);
3546258187Sedavis    } else {
3547258187Sedavis        /* clear watchdog when there are no pending transmits */
3548258187Sedavis        fp->watchdog_timer = 0;
3549258187Sedavis        return (FALSE);
3550255736Sdavidch    }
3551255736Sdavidch}
3552255736Sdavidch
3553255736Sdavidchstatic void
3554255736Sdavidchbxe_drain_tx_queues(struct bxe_softc *sc)
3555255736Sdavidch{
3556255736Sdavidch    struct bxe_fastpath *fp;
3557255736Sdavidch    int i, count;
3558255736Sdavidch
3559255736Sdavidch    /* wait until all TX fastpath tasks have completed */
3560255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
3561255736Sdavidch        fp = &sc->fp[i];
3562255736Sdavidch
3563255736Sdavidch        count = 1000;
3564255736Sdavidch
3565255736Sdavidch        while (bxe_has_tx_work(fp)) {
3566255736Sdavidch
3567255736Sdavidch            BXE_FP_TX_LOCK(fp);
3568255736Sdavidch            bxe_txeof(sc, fp);
3569255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
3570255736Sdavidch
3571255736Sdavidch            if (count == 0) {
3572255736Sdavidch                BLOGE(sc, "Timeout waiting for fp[%d] "
3573255736Sdavidch                          "transmits to complete!\n", i);
3574255736Sdavidch                bxe_panic(sc, ("tx drain failure\n"));
3575255736Sdavidch                return;
3576255736Sdavidch            }
3577255736Sdavidch
3578255736Sdavidch            count--;
3579255736Sdavidch            DELAY(1000);
3580255736Sdavidch            rmb();
3581255736Sdavidch        }
3582255736Sdavidch    }
3583255736Sdavidch
3584255736Sdavidch    return;
3585255736Sdavidch}
3586255736Sdavidch
3587255736Sdavidchstatic int
3588255736Sdavidchbxe_del_all_macs(struct bxe_softc          *sc,
3589255736Sdavidch                 struct ecore_vlan_mac_obj *mac_obj,
3590255736Sdavidch                 int                       mac_type,
3591255736Sdavidch                 uint8_t                   wait_for_comp)
3592255736Sdavidch{
3593255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3594255736Sdavidch    int rc;
3595255736Sdavidch
3596255736Sdavidch    /* wait for completion of requested */
3597255736Sdavidch    if (wait_for_comp) {
3598255736Sdavidch        bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3599255736Sdavidch    }
3600255736Sdavidch
3601255736Sdavidch    /* Set the mac type of addresses we want to clear */
3602255736Sdavidch    bxe_set_bit(mac_type, &vlan_mac_flags);
3603255736Sdavidch
3604255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3605255736Sdavidch    if (rc < 0) {
3606295830Sdavidcs        BLOGE(sc, "Failed to delete MACs (%d) mac_type %d wait_for_comp 0x%x\n",
3607295830Sdavidcs            rc, mac_type, wait_for_comp);
3608255736Sdavidch    }
3609255736Sdavidch
3610255736Sdavidch    return (rc);
3611255736Sdavidch}
3612255736Sdavidch
3613255736Sdavidchstatic int
3614255736Sdavidchbxe_fill_accept_flags(struct bxe_softc *sc,
3615255736Sdavidch                      uint32_t         rx_mode,
3616255736Sdavidch                      unsigned long    *rx_accept_flags,
3617255736Sdavidch                      unsigned long    *tx_accept_flags)
3618255736Sdavidch{
3619255736Sdavidch    /* Clear the flags first */
3620255736Sdavidch    *rx_accept_flags = 0;
3621255736Sdavidch    *tx_accept_flags = 0;
3622255736Sdavidch
3623255736Sdavidch    switch (rx_mode) {
3624255736Sdavidch    case BXE_RX_MODE_NONE:
3625255736Sdavidch        /*
3626255736Sdavidch         * 'drop all' supersedes any accept flags that may have been
3627255736Sdavidch         * passed to the function.
3628255736Sdavidch         */
3629255736Sdavidch        break;
3630255736Sdavidch
3631255736Sdavidch    case BXE_RX_MODE_NORMAL:
3632255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3633255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3634255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3635255736Sdavidch
3636255736Sdavidch        /* internal switching mode */
3637255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3638255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3639255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3640255736Sdavidch
3641255736Sdavidch        break;
3642255736Sdavidch
3643255736Sdavidch    case BXE_RX_MODE_ALLMULTI:
3644255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3645255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3646255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3647255736Sdavidch
3648255736Sdavidch        /* internal switching mode */
3649255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3650255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3651255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3652255736Sdavidch
3653255736Sdavidch        break;
3654255736Sdavidch
3655255736Sdavidch    case BXE_RX_MODE_PROMISC:
3656255736Sdavidch        /*
3657255736Sdavidch         * According to deffinition of SI mode, iface in promisc mode
3658255736Sdavidch         * should receive matched and unmatched (in resolution of port)
3659255736Sdavidch         * unicast packets.
3660255736Sdavidch         */
3661255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3662255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3663255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3664255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3665255736Sdavidch
3666255736Sdavidch        /* internal switching mode */
3667255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3668255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3669255736Sdavidch
3670255736Sdavidch        if (IS_MF_SI(sc)) {
3671255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3672255736Sdavidch        } else {
3673255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3674255736Sdavidch        }
3675255736Sdavidch
3676255736Sdavidch        break;
3677255736Sdavidch
3678255736Sdavidch    default:
3679295830Sdavidcs        BLOGE(sc, "Unknown rx_mode (0x%x)\n", rx_mode);
3680255736Sdavidch        return (-1);
3681255736Sdavidch    }
3682255736Sdavidch
3683255736Sdavidch    /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3684255736Sdavidch    if (rx_mode != BXE_RX_MODE_NONE) {
3685255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3686255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3687255736Sdavidch    }
3688255736Sdavidch
3689255736Sdavidch    return (0);
3690255736Sdavidch}
3691255736Sdavidch
3692255736Sdavidchstatic int
3693255736Sdavidchbxe_set_q_rx_mode(struct bxe_softc *sc,
3694255736Sdavidch                  uint8_t          cl_id,
3695255736Sdavidch                  unsigned long    rx_mode_flags,
3696255736Sdavidch                  unsigned long    rx_accept_flags,
3697255736Sdavidch                  unsigned long    tx_accept_flags,
3698255736Sdavidch                  unsigned long    ramrod_flags)
3699255736Sdavidch{
3700255736Sdavidch    struct ecore_rx_mode_ramrod_params ramrod_param;
3701255736Sdavidch    int rc;
3702255736Sdavidch
3703255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
3704255736Sdavidch
3705255736Sdavidch    /* Prepare ramrod parameters */
3706255736Sdavidch    ramrod_param.cid = 0;
3707255736Sdavidch    ramrod_param.cl_id = cl_id;
3708255736Sdavidch    ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3709255736Sdavidch    ramrod_param.func_id = SC_FUNC(sc);
3710255736Sdavidch
3711255736Sdavidch    ramrod_param.pstate = &sc->sp_state;
3712255736Sdavidch    ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3713255736Sdavidch
3714255736Sdavidch    ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3715255736Sdavidch    ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3716255736Sdavidch
3717255736Sdavidch    bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3718255736Sdavidch
3719255736Sdavidch    ramrod_param.ramrod_flags = ramrod_flags;
3720255736Sdavidch    ramrod_param.rx_mode_flags = rx_mode_flags;
3721255736Sdavidch
3722255736Sdavidch    ramrod_param.rx_accept_flags = rx_accept_flags;
3723255736Sdavidch    ramrod_param.tx_accept_flags = tx_accept_flags;
3724255736Sdavidch
3725255736Sdavidch    rc = ecore_config_rx_mode(sc, &ramrod_param);
3726255736Sdavidch    if (rc < 0) {
3727295830Sdavidcs        BLOGE(sc, "Set rx_mode %d cli_id 0x%x rx_mode_flags 0x%x "
3728295830Sdavidcs            "rx_accept_flags 0x%x tx_accept_flags 0x%x "
3729295830Sdavidcs            "ramrod_flags 0x%x rc %d failed\n", sc->rx_mode, cl_id,
3730295830Sdavidcs            (uint32_t)rx_mode_flags, (uint32_t)rx_accept_flags,
3731295830Sdavidcs            (uint32_t)tx_accept_flags, (uint32_t)ramrod_flags, rc);
3732255736Sdavidch        return (rc);
3733255736Sdavidch    }
3734255736Sdavidch
3735255736Sdavidch    return (0);
3736255736Sdavidch}
3737255736Sdavidch
3738255736Sdavidchstatic int
3739255736Sdavidchbxe_set_storm_rx_mode(struct bxe_softc *sc)
3740255736Sdavidch{
3741255736Sdavidch    unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3742255736Sdavidch    unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3743255736Sdavidch    int rc;
3744255736Sdavidch
3745255736Sdavidch    rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3746255736Sdavidch                               &tx_accept_flags);
3747255736Sdavidch    if (rc) {
3748255736Sdavidch        return (rc);
3749255736Sdavidch    }
3750255736Sdavidch
3751255736Sdavidch    bxe_set_bit(RAMROD_RX, &ramrod_flags);
3752255736Sdavidch    bxe_set_bit(RAMROD_TX, &ramrod_flags);
3753255736Sdavidch
3754255736Sdavidch    /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3755255736Sdavidch    return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3756255736Sdavidch                              rx_accept_flags, tx_accept_flags,
3757255736Sdavidch                              ramrod_flags));
3758255736Sdavidch}
3759255736Sdavidch
3760255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3761255736Sdavidchstatic int
3762255736Sdavidchbxe_nic_load_no_mcp(struct bxe_softc *sc)
3763255736Sdavidch{
3764255736Sdavidch    int path = SC_PATH(sc);
3765255736Sdavidch    int port = SC_PORT(sc);
3766255736Sdavidch
3767255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3768255736Sdavidch          path, load_count[path][0], load_count[path][1],
3769255736Sdavidch          load_count[path][2]);
3770255736Sdavidch    load_count[path][0]++;
3771255736Sdavidch    load_count[path][1 + port]++;
3772255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3773255736Sdavidch          path, load_count[path][0], load_count[path][1],
3774255736Sdavidch          load_count[path][2]);
3775255736Sdavidch    if (load_count[path][0] == 1) {
3776255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_COMMON);
3777255736Sdavidch    } else if (load_count[path][1 + port] == 1) {
3778255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_PORT);
3779255736Sdavidch    } else {
3780255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3781255736Sdavidch    }
3782255736Sdavidch}
3783255736Sdavidch
3784255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3785255736Sdavidchstatic int
3786255736Sdavidchbxe_nic_unload_no_mcp(struct bxe_softc *sc)
3787255736Sdavidch{
3788255736Sdavidch    int port = SC_PORT(sc);
3789255736Sdavidch    int path = SC_PATH(sc);
3790255736Sdavidch
3791255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3792255736Sdavidch          path, load_count[path][0], load_count[path][1],
3793255736Sdavidch          load_count[path][2]);
3794255736Sdavidch    load_count[path][0]--;
3795255736Sdavidch    load_count[path][1 + port]--;
3796255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3797255736Sdavidch          path, load_count[path][0], load_count[path][1],
3798255736Sdavidch          load_count[path][2]);
3799255736Sdavidch    if (load_count[path][0] == 0) {
3800255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3801255736Sdavidch    } else if (load_count[path][1 + port] == 0) {
3802255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3803255736Sdavidch    } else {
3804255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3805255736Sdavidch    }
3806255736Sdavidch}
3807255736Sdavidch
3808255736Sdavidch/* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3809255736Sdavidchstatic uint32_t
3810255736Sdavidchbxe_send_unload_req(struct bxe_softc *sc,
3811255736Sdavidch                    int              unload_mode)
3812255736Sdavidch{
3813255736Sdavidch    uint32_t reset_code = 0;
3814255736Sdavidch
3815255736Sdavidch    /* Select the UNLOAD request mode */
3816255736Sdavidch    if (unload_mode == UNLOAD_NORMAL) {
3817255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3818295830Sdavidcs    } else {
3819255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3820255736Sdavidch    }
3821255736Sdavidch
3822255736Sdavidch    /* Send the request to the MCP */
3823255736Sdavidch    if (!BXE_NOMCP(sc)) {
3824255736Sdavidch        reset_code = bxe_fw_command(sc, reset_code, 0);
3825255736Sdavidch    } else {
3826255736Sdavidch        reset_code = bxe_nic_unload_no_mcp(sc);
3827255736Sdavidch    }
3828255736Sdavidch
3829255736Sdavidch    return (reset_code);
3830255736Sdavidch}
3831255736Sdavidch
3832255736Sdavidch/* send UNLOAD_DONE command to the MCP */
3833255736Sdavidchstatic void
3834255736Sdavidchbxe_send_unload_done(struct bxe_softc *sc,
3835255736Sdavidch                     uint8_t          keep_link)
3836255736Sdavidch{
3837255736Sdavidch    uint32_t reset_param =
3838255736Sdavidch        keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
3839255736Sdavidch
3840255736Sdavidch    /* Report UNLOAD_DONE to MCP */
3841255736Sdavidch    if (!BXE_NOMCP(sc)) {
3842255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
3843255736Sdavidch    }
3844255736Sdavidch}
3845255736Sdavidch
3846255736Sdavidchstatic int
3847255736Sdavidchbxe_func_wait_started(struct bxe_softc *sc)
3848255736Sdavidch{
3849255736Sdavidch    int tout = 50;
3850255736Sdavidch
3851255736Sdavidch    if (!sc->port.pmf) {
3852255736Sdavidch        return (0);
3853255736Sdavidch    }
3854255736Sdavidch
3855255736Sdavidch    /*
3856255736Sdavidch     * (assumption: No Attention from MCP at this stage)
3857255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
3858255736Sdavidch     * 1. Sync IRS for default SB
3859255736Sdavidch     * 2. Sync SP queue - this guarantees us that attention handling started
3860255736Sdavidch     * 3. Wait, that TX disable/enable transaction completes
3861255736Sdavidch     *
3862255736Sdavidch     * 1+2 guarantee that if DCBX attention was scheduled it already changed
3863255736Sdavidch     * pending bit of transaction from STARTED-->TX_STOPPED, if we already
3864255736Sdavidch     * received completion for the transaction the state is TX_STOPPED.
3865255736Sdavidch     * State will return to STARTED after completion of TX_STOPPED-->STARTED
3866255736Sdavidch     * transaction.
3867255736Sdavidch     */
3868255736Sdavidch
3869255736Sdavidch    /* XXX make sure default SB ISR is done */
3870255736Sdavidch    /* need a way to synchronize an irq (intr_mtx?) */
3871255736Sdavidch
3872255736Sdavidch    /* XXX flush any work queues */
3873255736Sdavidch
3874255736Sdavidch    while (ecore_func_get_state(sc, &sc->func_obj) !=
3875255736Sdavidch           ECORE_F_STATE_STARTED && tout--) {
3876255736Sdavidch        DELAY(20000);
3877255736Sdavidch    }
3878255736Sdavidch
3879255736Sdavidch    if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
3880255736Sdavidch        /*
3881255736Sdavidch         * Failed to complete the transaction in a "good way"
3882255736Sdavidch         * Force both transactions with CLR bit.
3883255736Sdavidch         */
3884255736Sdavidch        struct ecore_func_state_params func_params = { NULL };
3885255736Sdavidch
3886255736Sdavidch        BLOGE(sc, "Unexpected function state! "
3887255736Sdavidch                  "Forcing STARTED-->TX_STOPPED-->STARTED\n");
3888255736Sdavidch
3889255736Sdavidch        func_params.f_obj = &sc->func_obj;
3890255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3891255736Sdavidch
3892255736Sdavidch        /* STARTED-->TX_STOPPED */
3893255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_STOP;
3894255736Sdavidch        ecore_func_state_change(sc, &func_params);
3895255736Sdavidch
3896255736Sdavidch        /* TX_STOPPED-->STARTED */
3897255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_START;
3898255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
3899255736Sdavidch    }
3900255736Sdavidch
3901255736Sdavidch    return (0);
3902255736Sdavidch}
3903255736Sdavidch
3904255736Sdavidchstatic int
3905255736Sdavidchbxe_stop_queue(struct bxe_softc *sc,
3906255736Sdavidch               int              index)
3907255736Sdavidch{
3908255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[index];
3909255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
3910255736Sdavidch    int rc;
3911255736Sdavidch
3912255736Sdavidch    BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
3913255736Sdavidch
3914255736Sdavidch    q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
3915255736Sdavidch    /* We want to wait for completion in this context */
3916255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
3917255736Sdavidch
3918255736Sdavidch    /* Stop the primary connection: */
3919255736Sdavidch
3920255736Sdavidch    /* ...halt the connection */
3921255736Sdavidch    q_params.cmd = ECORE_Q_CMD_HALT;
3922255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
3923255736Sdavidch    if (rc) {
3924255736Sdavidch        return (rc);
3925255736Sdavidch    }
3926255736Sdavidch
3927255736Sdavidch    /* ...terminate the connection */
3928255736Sdavidch    q_params.cmd = ECORE_Q_CMD_TERMINATE;
3929255736Sdavidch    memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
3930255736Sdavidch    q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
3931255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
3932255736Sdavidch    if (rc) {
3933255736Sdavidch        return (rc);
3934255736Sdavidch    }
3935255736Sdavidch
3936255736Sdavidch    /* ...delete cfc entry */
3937255736Sdavidch    q_params.cmd = ECORE_Q_CMD_CFC_DEL;
3938255736Sdavidch    memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
3939255736Sdavidch    q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
3940255736Sdavidch    return (ecore_queue_state_change(sc, &q_params));
3941255736Sdavidch}
3942255736Sdavidch
3943255736Sdavidch/* wait for the outstanding SP commands */
3944255736Sdavidchstatic inline uint8_t
3945255736Sdavidchbxe_wait_sp_comp(struct bxe_softc *sc,
3946255736Sdavidch                 unsigned long    mask)
3947255736Sdavidch{
3948255736Sdavidch    unsigned long tmp;
3949255736Sdavidch    int tout = 5000; /* wait for 5 secs tops */
3950255736Sdavidch
3951255736Sdavidch    while (tout--) {
3952255736Sdavidch        mb();
3953255736Sdavidch        if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
3954255736Sdavidch            return (TRUE);
3955255736Sdavidch        }
3956255736Sdavidch
3957255736Sdavidch        DELAY(1000);
3958255736Sdavidch    }
3959255736Sdavidch
3960255736Sdavidch    mb();
3961255736Sdavidch
3962255736Sdavidch    tmp = atomic_load_acq_long(&sc->sp_state);
3963255736Sdavidch    if (tmp & mask) {
3964255736Sdavidch        BLOGE(sc, "Filtering completion timed out: "
3965255736Sdavidch                  "sp_state 0x%lx, mask 0x%lx\n",
3966255736Sdavidch              tmp, mask);
3967255736Sdavidch        return (FALSE);
3968255736Sdavidch    }
3969255736Sdavidch
3970255736Sdavidch    return (FALSE);
3971255736Sdavidch}
3972255736Sdavidch
3973255736Sdavidchstatic int
3974255736Sdavidchbxe_func_stop(struct bxe_softc *sc)
3975255736Sdavidch{
3976255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
3977255736Sdavidch    int rc;
3978255736Sdavidch
3979255736Sdavidch    /* prepare parameters for function state transitions */
3980255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3981255736Sdavidch    func_params.f_obj = &sc->func_obj;
3982255736Sdavidch    func_params.cmd = ECORE_F_CMD_STOP;
3983255736Sdavidch
3984255736Sdavidch    /*
3985255736Sdavidch     * Try to stop the function the 'good way'. If it fails (in case
3986255736Sdavidch     * of a parity error during bxe_chip_cleanup()) and we are
3987255736Sdavidch     * not in a debug mode, perform a state transaction in order to
3988255736Sdavidch     * enable further HW_RESET transaction.
3989255736Sdavidch     */
3990255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
3991255736Sdavidch    if (rc) {
3992255736Sdavidch        BLOGE(sc, "FUNC_STOP ramrod failed. "
3993295830Sdavidcs                  "Running a dry transaction (%d)\n", rc);
3994255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3995255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
3996255736Sdavidch    }
3997255736Sdavidch
3998255736Sdavidch    return (0);
3999255736Sdavidch}
4000255736Sdavidch
4001255736Sdavidchstatic int
4002255736Sdavidchbxe_reset_hw(struct bxe_softc *sc,
4003255736Sdavidch             uint32_t         load_code)
4004255736Sdavidch{
4005255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
4006255736Sdavidch
4007255736Sdavidch    /* Prepare parameters for function state transitions */
4008255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
4009255736Sdavidch
4010255736Sdavidch    func_params.f_obj = &sc->func_obj;
4011255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_RESET;
4012255736Sdavidch
4013255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
4014255736Sdavidch
4015255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
4016255736Sdavidch}
4017255736Sdavidch
4018255736Sdavidchstatic void
4019255736Sdavidchbxe_int_disable_sync(struct bxe_softc *sc,
4020255736Sdavidch                     int              disable_hw)
4021255736Sdavidch{
4022255736Sdavidch    if (disable_hw) {
4023255736Sdavidch        /* prevent the HW from sending interrupts */
4024255736Sdavidch        bxe_int_disable(sc);
4025255736Sdavidch    }
4026255736Sdavidch
4027255736Sdavidch    /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
4028255736Sdavidch    /* make sure all ISRs are done */
4029255736Sdavidch
4030255736Sdavidch    /* XXX make sure sp_task is not running */
4031255736Sdavidch    /* cancel and flush work queues */
4032255736Sdavidch}
4033255736Sdavidch
4034255736Sdavidchstatic void
4035255736Sdavidchbxe_chip_cleanup(struct bxe_softc *sc,
4036255736Sdavidch                 uint32_t         unload_mode,
4037255736Sdavidch                 uint8_t          keep_link)
4038255736Sdavidch{
4039255736Sdavidch    int port = SC_PORT(sc);
4040255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
4041255736Sdavidch    uint32_t reset_code;
4042255736Sdavidch    int i, rc = 0;
4043255736Sdavidch
4044255736Sdavidch    bxe_drain_tx_queues(sc);
4045255736Sdavidch
4046255736Sdavidch    /* give HW time to discard old tx messages */
4047255736Sdavidch    DELAY(1000);
4048255736Sdavidch
4049255736Sdavidch    /* Clean all ETH MACs */
4050255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4051255736Sdavidch    if (rc < 0) {
4052255736Sdavidch        BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4053255736Sdavidch    }
4054255736Sdavidch
4055255736Sdavidch    /* Clean up UC list  */
4056255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4057255736Sdavidch    if (rc < 0) {
4058255736Sdavidch        BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4059255736Sdavidch    }
4060255736Sdavidch
4061255736Sdavidch    /* Disable LLH */
4062255736Sdavidch    if (!CHIP_IS_E1(sc)) {
4063255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4064255736Sdavidch    }
4065255736Sdavidch
4066255736Sdavidch    /* Set "drop all" to stop Rx */
4067255736Sdavidch
4068255736Sdavidch    /*
4069255736Sdavidch     * We need to take the BXE_MCAST_LOCK() here in order to prevent
4070255736Sdavidch     * a race between the completion code and this code.
4071255736Sdavidch     */
4072255736Sdavidch    BXE_MCAST_LOCK(sc);
4073255736Sdavidch
4074255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4075255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4076255736Sdavidch    } else {
4077255736Sdavidch        bxe_set_storm_rx_mode(sc);
4078255736Sdavidch    }
4079255736Sdavidch
4080255736Sdavidch    /* Clean up multicast configuration */
4081255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4082255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4083255736Sdavidch    if (rc < 0) {
4084255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4085255736Sdavidch    }
4086255736Sdavidch
4087255736Sdavidch    BXE_MCAST_UNLOCK(sc);
4088255736Sdavidch
4089255736Sdavidch    // XXX bxe_iov_chip_cleanup(sc);
4090255736Sdavidch
4091255736Sdavidch    /*
4092255736Sdavidch     * Send the UNLOAD_REQUEST to the MCP. This will return if
4093255736Sdavidch     * this function should perform FUNCTION, PORT, or COMMON HW
4094255736Sdavidch     * reset.
4095255736Sdavidch     */
4096255736Sdavidch    reset_code = bxe_send_unload_req(sc, unload_mode);
4097255736Sdavidch
4098255736Sdavidch    /*
4099255736Sdavidch     * (assumption: No Attention from MCP at this stage)
4100255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
4101255736Sdavidch     */
4102255736Sdavidch    rc = bxe_func_wait_started(sc);
4103255736Sdavidch    if (rc) {
4104295830Sdavidcs        BLOGE(sc, "bxe_func_wait_started failed (%d)\n", rc);
4105255736Sdavidch    }
4106255736Sdavidch
4107255736Sdavidch    /*
4108255736Sdavidch     * Close multi and leading connections
4109255736Sdavidch     * Completions for ramrods are collected in a synchronous way
4110255736Sdavidch     */
4111255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
4112255736Sdavidch        if (bxe_stop_queue(sc, i)) {
4113255736Sdavidch            goto unload_error;
4114255736Sdavidch        }
4115255736Sdavidch    }
4116255736Sdavidch
4117255736Sdavidch    /*
4118255736Sdavidch     * If SP settings didn't get completed so far - something
4119255736Sdavidch     * very wrong has happen.
4120255736Sdavidch     */
4121255736Sdavidch    if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4122295830Sdavidcs        BLOGE(sc, "Common slow path ramrods got stuck!(%d)\n", rc);
4123255736Sdavidch    }
4124255736Sdavidch
4125255736Sdavidchunload_error:
4126255736Sdavidch
4127255736Sdavidch    rc = bxe_func_stop(sc);
4128255736Sdavidch    if (rc) {
4129295830Sdavidcs        BLOGE(sc, "Function stop failed!(%d)\n", rc);
4130255736Sdavidch    }
4131255736Sdavidch
4132255736Sdavidch    /* disable HW interrupts */
4133255736Sdavidch    bxe_int_disable_sync(sc, TRUE);
4134255736Sdavidch
4135255736Sdavidch    /* detach interrupts */
4136255736Sdavidch    bxe_interrupt_detach(sc);
4137255736Sdavidch
4138255736Sdavidch    /* Reset the chip */
4139255736Sdavidch    rc = bxe_reset_hw(sc, reset_code);
4140255736Sdavidch    if (rc) {
4141295830Sdavidcs        BLOGE(sc, "Hardware reset failed(%d)\n", rc);
4142255736Sdavidch    }
4143255736Sdavidch
4144255736Sdavidch    /* Report UNLOAD_DONE to MCP */
4145255736Sdavidch    bxe_send_unload_done(sc, keep_link);
4146255736Sdavidch}
4147255736Sdavidch
4148255736Sdavidchstatic void
4149255736Sdavidchbxe_disable_close_the_gate(struct bxe_softc *sc)
4150255736Sdavidch{
4151255736Sdavidch    uint32_t val;
4152255736Sdavidch    int port = SC_PORT(sc);
4153255736Sdavidch
4154255736Sdavidch    BLOGD(sc, DBG_LOAD,
4155255736Sdavidch          "Disabling 'close the gates'\n");
4156255736Sdavidch
4157255736Sdavidch    if (CHIP_IS_E1(sc)) {
4158255736Sdavidch        uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4159255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
4160255736Sdavidch        val = REG_RD(sc, addr);
4161255736Sdavidch        val &= ~(0x300);
4162255736Sdavidch        REG_WR(sc, addr, val);
4163255736Sdavidch    } else {
4164255736Sdavidch        val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4165255736Sdavidch        val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4166255736Sdavidch                 MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4167255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4168255736Sdavidch    }
4169255736Sdavidch}
4170255736Sdavidch
4171255736Sdavidch/*
4172255736Sdavidch * Cleans the object that have internal lists without sending
4173255736Sdavidch * ramrods. Should be run when interrutps are disabled.
4174255736Sdavidch */
4175255736Sdavidchstatic void
4176255736Sdavidchbxe_squeeze_objects(struct bxe_softc *sc)
4177255736Sdavidch{
4178255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4179255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
4180255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4181255736Sdavidch    int rc;
4182255736Sdavidch
4183255736Sdavidch    /* Cleanup MACs' object first... */
4184255736Sdavidch
4185255736Sdavidch    /* Wait for completion of requested */
4186255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4187255736Sdavidch    /* Perform a dry cleanup */
4188255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4189255736Sdavidch
4190255736Sdavidch    /* Clean ETH primary MAC */
4191255736Sdavidch    bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4192255736Sdavidch    rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4193255736Sdavidch                             &ramrod_flags);
4194255736Sdavidch    if (rc != 0) {
4195255736Sdavidch        BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4196255736Sdavidch    }
4197255736Sdavidch
4198255736Sdavidch    /* Cleanup UC list */
4199255736Sdavidch    vlan_mac_flags = 0;
4200255736Sdavidch    bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4201255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4202255736Sdavidch                             &ramrod_flags);
4203255736Sdavidch    if (rc != 0) {
4204255736Sdavidch        BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4205255736Sdavidch    }
4206255736Sdavidch
4207255736Sdavidch    /* Now clean mcast object... */
4208255736Sdavidch
4209255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4210255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4211255736Sdavidch
4212255736Sdavidch    /* Add a DEL command... */
4213255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4214255736Sdavidch    if (rc < 0) {
4215255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4216255736Sdavidch    }
4217255736Sdavidch
4218255736Sdavidch    /* now wait until all pending commands are cleared */
4219255736Sdavidch
4220255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4221255736Sdavidch    while (rc != 0) {
4222255736Sdavidch        if (rc < 0) {
4223255736Sdavidch            BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4224255736Sdavidch            return;
4225255736Sdavidch        }
4226255736Sdavidch
4227255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4228255736Sdavidch    }
4229255736Sdavidch}
4230255736Sdavidch
4231255736Sdavidch/* stop the controller */
4232255736Sdavidchstatic __noinline int
4233255736Sdavidchbxe_nic_unload(struct bxe_softc *sc,
4234255736Sdavidch               uint32_t         unload_mode,
4235255736Sdavidch               uint8_t          keep_link)
4236255736Sdavidch{
4237255736Sdavidch    uint8_t global = FALSE;
4238255736Sdavidch    uint32_t val;
4239296579Sdavidcs    int i;
4240255736Sdavidch
4241255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
4242255736Sdavidch
4243296579Sdavidcs    if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
4244296579Sdavidcs
4245296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
4246296579Sdavidcs        struct bxe_fastpath *fp;
4247296579Sdavidcs
4248296579Sdavidcs        fp = &sc->fp[i];
4249296579Sdavidcs        BXE_FP_TX_LOCK(fp);
4250296579Sdavidcs        BXE_FP_TX_UNLOCK(fp);
4251296579Sdavidcs    }
4252296579Sdavidcs
4253255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4254255736Sdavidch
4255255736Sdavidch    /* mark driver as unloaded in shmem2 */
4256255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4257255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4258255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4259255736Sdavidch                  val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4260255736Sdavidch    }
4261255736Sdavidch
4262255736Sdavidch    if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4263255736Sdavidch        (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4264255736Sdavidch        /*
4265255736Sdavidch         * We can get here if the driver has been unloaded
4266255736Sdavidch         * during parity error recovery and is either waiting for a
4267255736Sdavidch         * leader to complete or for other functions to unload and
4268255736Sdavidch         * then ifconfig down has been issued. In this case we want to
4269255736Sdavidch         * unload and let other functions to complete a recovery
4270255736Sdavidch         * process.
4271255736Sdavidch         */
4272255736Sdavidch        sc->recovery_state = BXE_RECOVERY_DONE;
4273255736Sdavidch        sc->is_leader = 0;
4274255736Sdavidch        bxe_release_leader_lock(sc);
4275255736Sdavidch        mb();
4276255736Sdavidch
4277255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4278295830Sdavidcs        BLOGE(sc, "Can't unload in closed or error state recover_state 0x%x"
4279295830Sdavidcs            " state = 0x%x\n", sc->recovery_state, sc->state);
4280255736Sdavidch        return (-1);
4281255736Sdavidch    }
4282255736Sdavidch
4283255736Sdavidch    /*
4284255736Sdavidch     * Nothing to do during unload if previous bxe_nic_load()
4285298955Spfg     * did not completed successfully - all resourses are released.
4286255736Sdavidch     */
4287255736Sdavidch    if ((sc->state == BXE_STATE_CLOSED) ||
4288255736Sdavidch        (sc->state == BXE_STATE_ERROR)) {
4289255736Sdavidch        return (0);
4290255736Sdavidch    }
4291255736Sdavidch
4292255736Sdavidch    sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4293255736Sdavidch    mb();
4294255736Sdavidch
4295255736Sdavidch    /* stop tx */
4296255736Sdavidch    bxe_tx_disable(sc);
4297255736Sdavidch
4298255736Sdavidch    sc->rx_mode = BXE_RX_MODE_NONE;
4299255736Sdavidch    /* XXX set rx mode ??? */
4300255736Sdavidch
4301292639Sdavidcs    if (IS_PF(sc) && !sc->grcdump_done) {
4302255736Sdavidch        /* set ALWAYS_ALIVE bit in shmem */
4303255736Sdavidch        sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4304255736Sdavidch
4305255736Sdavidch        bxe_drv_pulse(sc);
4306255736Sdavidch
4307255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_STOP);
4308255736Sdavidch        bxe_save_statistics(sc);
4309255736Sdavidch    }
4310255736Sdavidch
4311255736Sdavidch    /* wait till consumers catch up with producers in all queues */
4312255736Sdavidch    bxe_drain_tx_queues(sc);
4313255736Sdavidch
4314255736Sdavidch    /* if VF indicate to PF this function is going down (PF will delete sp
4315255736Sdavidch     * elements and clear initializations
4316255736Sdavidch     */
4317255736Sdavidch    if (IS_VF(sc)) {
4318255736Sdavidch        ; /* bxe_vfpf_close_vf(sc); */
4319255736Sdavidch    } else if (unload_mode != UNLOAD_RECOVERY) {
4320255736Sdavidch        /* if this is a normal/close unload need to clean up chip */
4321292639Sdavidcs        if (!sc->grcdump_done)
4322292639Sdavidcs            bxe_chip_cleanup(sc, unload_mode, keep_link);
4323255736Sdavidch    } else {
4324255736Sdavidch        /* Send the UNLOAD_REQUEST to the MCP */
4325255736Sdavidch        bxe_send_unload_req(sc, unload_mode);
4326255736Sdavidch
4327255736Sdavidch        /*
4328255736Sdavidch         * Prevent transactions to host from the functions on the
4329255736Sdavidch         * engine that doesn't reset global blocks in case of global
4330255736Sdavidch         * attention once gloabl blocks are reset and gates are opened
4331255736Sdavidch         * (the engine which leader will perform the recovery
4332255736Sdavidch         * last).
4333255736Sdavidch         */
4334255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
4335255736Sdavidch            bxe_pf_disable(sc);
4336255736Sdavidch        }
4337255736Sdavidch
4338255736Sdavidch        /* disable HW interrupts */
4339255736Sdavidch        bxe_int_disable_sync(sc, TRUE);
4340255736Sdavidch
4341255736Sdavidch        /* detach interrupts */
4342255736Sdavidch        bxe_interrupt_detach(sc);
4343255736Sdavidch
4344255736Sdavidch        /* Report UNLOAD_DONE to MCP */
4345255736Sdavidch        bxe_send_unload_done(sc, FALSE);
4346255736Sdavidch    }
4347255736Sdavidch
4348255736Sdavidch    /*
4349255736Sdavidch     * At this stage no more interrupts will arrive so we may safely clean
4350255736Sdavidch     * the queue'able objects here in case they failed to get cleaned so far.
4351255736Sdavidch     */
4352255736Sdavidch    if (IS_PF(sc)) {
4353255736Sdavidch        bxe_squeeze_objects(sc);
4354255736Sdavidch    }
4355255736Sdavidch
4356255736Sdavidch    /* There should be no more pending SP commands at this stage */
4357255736Sdavidch    sc->sp_state = 0;
4358255736Sdavidch
4359255736Sdavidch    sc->port.pmf = 0;
4360255736Sdavidch
4361255736Sdavidch    bxe_free_fp_buffers(sc);
4362255736Sdavidch
4363255736Sdavidch    if (IS_PF(sc)) {
4364255736Sdavidch        bxe_free_mem(sc);
4365255736Sdavidch    }
4366255736Sdavidch
4367255736Sdavidch    bxe_free_fw_stats_mem(sc);
4368255736Sdavidch
4369255736Sdavidch    sc->state = BXE_STATE_CLOSED;
4370255736Sdavidch
4371255736Sdavidch    /*
4372255736Sdavidch     * Check if there are pending parity attentions. If there are - set
4373255736Sdavidch     * RECOVERY_IN_PROGRESS.
4374255736Sdavidch     */
4375255736Sdavidch    if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4376255736Sdavidch        bxe_set_reset_in_progress(sc);
4377255736Sdavidch
4378255736Sdavidch        /* Set RESET_IS_GLOBAL if needed */
4379255736Sdavidch        if (global) {
4380255736Sdavidch            bxe_set_reset_global(sc);
4381255736Sdavidch        }
4382255736Sdavidch    }
4383255736Sdavidch
4384255736Sdavidch    /*
4385255736Sdavidch     * The last driver must disable a "close the gate" if there is no
4386255736Sdavidch     * parity attention or "process kill" pending.
4387255736Sdavidch     */
4388255736Sdavidch    if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4389255736Sdavidch        bxe_reset_is_done(sc, SC_PATH(sc))) {
4390255736Sdavidch        bxe_disable_close_the_gate(sc);
4391255736Sdavidch    }
4392255736Sdavidch
4393255736Sdavidch    BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4394255736Sdavidch
4395255736Sdavidch    return (0);
4396255736Sdavidch}
4397255736Sdavidch
4398255736Sdavidch/*
4399255736Sdavidch * Called by the OS to set various media options (i.e. link, speed, etc.) when
4400255736Sdavidch * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4401255736Sdavidch */
4402255736Sdavidchstatic int
4403266979Smarcelbxe_ifmedia_update(struct ifnet  *ifp)
4404255736Sdavidch{
4405266979Smarcel    struct bxe_softc *sc = (struct bxe_softc *)if_getsoftc(ifp);
4406255736Sdavidch    struct ifmedia *ifm;
4407255736Sdavidch
4408255736Sdavidch    ifm = &sc->ifmedia;
4409255736Sdavidch
4410255736Sdavidch    /* We only support Ethernet media type. */
4411255736Sdavidch    if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4412255736Sdavidch        return (EINVAL);
4413255736Sdavidch    }
4414255736Sdavidch
4415255736Sdavidch    switch (IFM_SUBTYPE(ifm->ifm_media)) {
4416255736Sdavidch    case IFM_AUTO:
4417255736Sdavidch         break;
4418255736Sdavidch    case IFM_10G_CX4:
4419255736Sdavidch    case IFM_10G_SR:
4420255736Sdavidch    case IFM_10G_T:
4421255736Sdavidch    case IFM_10G_TWINAX:
4422255736Sdavidch    default:
4423255736Sdavidch        /* We don't support changing the media type. */
4424255736Sdavidch        BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4425255736Sdavidch              IFM_SUBTYPE(ifm->ifm_media));
4426255736Sdavidch        return (EINVAL);
4427255736Sdavidch    }
4428255736Sdavidch
4429255736Sdavidch    return (0);
4430255736Sdavidch}
4431255736Sdavidch
4432255736Sdavidch/*
4433255736Sdavidch * Called by the OS to get the current media status (i.e. link, speed, etc.).
4434255736Sdavidch */
4435255736Sdavidchstatic void
4436255736Sdavidchbxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4437255736Sdavidch{
4438266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4439255736Sdavidch
4440255736Sdavidch    /* Report link down if the driver isn't running. */
4441266979Smarcel    if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
4442255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4443255736Sdavidch        return;
4444255736Sdavidch    }
4445255736Sdavidch
4446255736Sdavidch    /* Setup the default interface info. */
4447255736Sdavidch    ifmr->ifm_status = IFM_AVALID;
4448255736Sdavidch    ifmr->ifm_active = IFM_ETHER;
4449255736Sdavidch
4450255736Sdavidch    if (sc->link_vars.link_up) {
4451255736Sdavidch        ifmr->ifm_status |= IFM_ACTIVE;
4452255736Sdavidch    } else {
4453255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4454255736Sdavidch        return;
4455255736Sdavidch    }
4456255736Sdavidch
4457255736Sdavidch    ifmr->ifm_active |= sc->media;
4458255736Sdavidch
4459255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
4460255736Sdavidch        ifmr->ifm_active |= IFM_FDX;
4461255736Sdavidch    } else {
4462255736Sdavidch        ifmr->ifm_active |= IFM_HDX;
4463255736Sdavidch    }
4464255736Sdavidch}
4465255736Sdavidch
4466255736Sdavidchstatic void
4467255736Sdavidchbxe_handle_chip_tq(void *context,
4468255736Sdavidch                   int  pending)
4469255736Sdavidch{
4470255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
4471255736Sdavidch    long work = atomic_load_acq_long(&sc->chip_tq_flags);
4472255736Sdavidch
4473255736Sdavidch    switch (work)
4474255736Sdavidch    {
4475255736Sdavidch
4476255736Sdavidch    case CHIP_TQ_REINIT:
4477266979Smarcel        if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4478255736Sdavidch            /* restart the interface */
4479255736Sdavidch            BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4480255736Sdavidch            bxe_periodic_stop(sc);
4481255736Sdavidch            BXE_CORE_LOCK(sc);
4482255736Sdavidch            bxe_stop_locked(sc);
4483255736Sdavidch            bxe_init_locked(sc);
4484255736Sdavidch            BXE_CORE_UNLOCK(sc);
4485255736Sdavidch        }
4486255736Sdavidch        break;
4487255736Sdavidch
4488255736Sdavidch    default:
4489255736Sdavidch        break;
4490255736Sdavidch    }
4491255736Sdavidch}
4492255736Sdavidch
4493255736Sdavidch/*
4494255736Sdavidch * Handles any IOCTL calls from the operating system.
4495255736Sdavidch *
4496255736Sdavidch * Returns:
4497255736Sdavidch *   0 = Success, >0 Failure
4498255736Sdavidch */
4499255736Sdavidchstatic int
4500266979Smarcelbxe_ioctl(if_t ifp,
4501255736Sdavidch          u_long       command,
4502255736Sdavidch          caddr_t      data)
4503255736Sdavidch{
4504266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4505255736Sdavidch    struct ifreq *ifr = (struct ifreq *)data;
4506255736Sdavidch    int mask = 0;
4507255736Sdavidch    int reinit = 0;
4508255736Sdavidch    int error = 0;
4509255736Sdavidch
4510255736Sdavidch    int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4511255736Sdavidch    int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4512255736Sdavidch
4513255736Sdavidch    switch (command)
4514255736Sdavidch    {
4515255736Sdavidch    case SIOCSIFMTU:
4516255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4517255736Sdavidch              ifr->ifr_mtu);
4518255736Sdavidch
4519255736Sdavidch        if (sc->mtu == ifr->ifr_mtu) {
4520255736Sdavidch            /* nothing to change */
4521255736Sdavidch            break;
4522255736Sdavidch        }
4523255736Sdavidch
4524255736Sdavidch        if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4525255736Sdavidch            BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4526255736Sdavidch                  ifr->ifr_mtu, mtu_min, mtu_max);
4527255736Sdavidch            error = EINVAL;
4528255736Sdavidch            break;
4529255736Sdavidch        }
4530255736Sdavidch
4531255736Sdavidch        atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4532255736Sdavidch                             (unsigned long)ifr->ifr_mtu);
4533266979Smarcel	/*
4534266979Smarcel        atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4535255736Sdavidch                              (unsigned long)ifr->ifr_mtu);
4536266979Smarcel	XXX - Not sure why it needs to be atomic
4537266979Smarcel	*/
4538266979Smarcel	if_setmtu(ifp, ifr->ifr_mtu);
4539255736Sdavidch        reinit = 1;
4540255736Sdavidch        break;
4541255736Sdavidch
4542255736Sdavidch    case SIOCSIFFLAGS:
4543255736Sdavidch        /* toggle the interface state up or down */
4544255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4545255736Sdavidch
4546284335Sdavidcs	BXE_CORE_LOCK(sc);
4547255736Sdavidch        /* check if the interface is up */
4548266979Smarcel        if (if_getflags(ifp) & IFF_UP) {
4549266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4550255736Sdavidch                /* set the receive mode flags */
4551255736Sdavidch                bxe_set_rx_mode(sc);
4552297873Sdavidcs            } else if(sc->state != BXE_STATE_DISABLED) {
4553284335Sdavidcs		bxe_init_locked(sc);
4554255736Sdavidch            }
4555255736Sdavidch        } else {
4556266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4557284335Sdavidcs		bxe_periodic_stop(sc);
4558284335Sdavidcs		bxe_stop_locked(sc);
4559255736Sdavidch            }
4560255736Sdavidch        }
4561284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4562255736Sdavidch
4563255736Sdavidch        break;
4564255736Sdavidch
4565255736Sdavidch    case SIOCADDMULTI:
4566255736Sdavidch    case SIOCDELMULTI:
4567255736Sdavidch        /* add/delete multicast addresses */
4568255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4569255736Sdavidch
4570255736Sdavidch        /* check if the interface is up */
4571266979Smarcel        if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4572255736Sdavidch            /* set the receive mode flags */
4573284335Sdavidcs	    BXE_CORE_LOCK(sc);
4574255736Sdavidch            bxe_set_rx_mode(sc);
4575284335Sdavidcs	    BXE_CORE_UNLOCK(sc);
4576255736Sdavidch        }
4577255736Sdavidch
4578255736Sdavidch        break;
4579255736Sdavidch
4580255736Sdavidch    case SIOCSIFCAP:
4581255736Sdavidch        /* find out which capabilities have changed */
4582266979Smarcel        mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4583255736Sdavidch
4584255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4585255736Sdavidch              mask);
4586255736Sdavidch
4587255736Sdavidch        /* toggle the LRO capabilites enable flag */
4588255736Sdavidch        if (mask & IFCAP_LRO) {
4589266979Smarcel	    if_togglecapenable(ifp, IFCAP_LRO);
4590255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4591266979Smarcel                  (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4592255736Sdavidch            reinit = 1;
4593255736Sdavidch        }
4594255736Sdavidch
4595255736Sdavidch        /* toggle the TXCSUM checksum capabilites enable flag */
4596255736Sdavidch        if (mask & IFCAP_TXCSUM) {
4597266979Smarcel	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4598255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4599266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4600266979Smarcel            if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4601266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4602255736Sdavidch                                    CSUM_TCP      |
4603255736Sdavidch                                    CSUM_UDP      |
4604255736Sdavidch                                    CSUM_TSO      |
4605255736Sdavidch                                    CSUM_TCP_IPV6 |
4606266979Smarcel                                    CSUM_UDP_IPV6), 0);
4607255736Sdavidch            } else {
4608266979Smarcel		if_clearhwassist(ifp); /* XXX */
4609255736Sdavidch            }
4610255736Sdavidch        }
4611255736Sdavidch
4612255736Sdavidch        /* toggle the RXCSUM checksum capabilities enable flag */
4613255736Sdavidch        if (mask & IFCAP_RXCSUM) {
4614266979Smarcel	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4615255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4616266979Smarcel                  (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4617266979Smarcel            if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4618266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4619255736Sdavidch                                    CSUM_TCP      |
4620255736Sdavidch                                    CSUM_UDP      |
4621255736Sdavidch                                    CSUM_TSO      |
4622255736Sdavidch                                    CSUM_TCP_IPV6 |
4623266979Smarcel                                    CSUM_UDP_IPV6), 0);
4624255736Sdavidch            } else {
4625266979Smarcel		if_clearhwassist(ifp); /* XXX */
4626255736Sdavidch            }
4627255736Sdavidch        }
4628255736Sdavidch
4629255736Sdavidch        /* toggle TSO4 capabilities enabled flag */
4630255736Sdavidch        if (mask & IFCAP_TSO4) {
4631266979Smarcel            if_togglecapenable(ifp, IFCAP_TSO4);
4632255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4633266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4634255736Sdavidch        }
4635255736Sdavidch
4636255736Sdavidch        /* toggle TSO6 capabilities enabled flag */
4637255736Sdavidch        if (mask & IFCAP_TSO6) {
4638266979Smarcel	    if_togglecapenable(ifp, IFCAP_TSO6);
4639255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4640266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4641255736Sdavidch        }
4642255736Sdavidch
4643255736Sdavidch        /* toggle VLAN_HWTSO capabilities enabled flag */
4644255736Sdavidch        if (mask & IFCAP_VLAN_HWTSO) {
4645266979Smarcel
4646266979Smarcel	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4647255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4648266979Smarcel                  (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4649255736Sdavidch        }
4650255736Sdavidch
4651255736Sdavidch        /* toggle VLAN_HWCSUM capabilities enabled flag */
4652255736Sdavidch        if (mask & IFCAP_VLAN_HWCSUM) {
4653255736Sdavidch            /* XXX investigate this... */
4654255736Sdavidch            BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4655255736Sdavidch            error = EINVAL;
4656255736Sdavidch        }
4657255736Sdavidch
4658255736Sdavidch        /* toggle VLAN_MTU capabilities enable flag */
4659255736Sdavidch        if (mask & IFCAP_VLAN_MTU) {
4660255736Sdavidch            /* XXX investigate this... */
4661255736Sdavidch            BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4662255736Sdavidch            error = EINVAL;
4663255736Sdavidch        }
4664255736Sdavidch
4665255736Sdavidch        /* toggle VLAN_HWTAGGING capabilities enabled flag */
4666255736Sdavidch        if (mask & IFCAP_VLAN_HWTAGGING) {
4667255736Sdavidch            /* XXX investigate this... */
4668255736Sdavidch            BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4669255736Sdavidch            error = EINVAL;
4670255736Sdavidch        }
4671255736Sdavidch
4672255736Sdavidch        /* toggle VLAN_HWFILTER capabilities enabled flag */
4673255736Sdavidch        if (mask & IFCAP_VLAN_HWFILTER) {
4674255736Sdavidch            /* XXX investigate this... */
4675255736Sdavidch            BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4676255736Sdavidch            error = EINVAL;
4677255736Sdavidch        }
4678255736Sdavidch
4679255736Sdavidch        /* XXX not yet...
4680255736Sdavidch         * IFCAP_WOL_MAGIC
4681255736Sdavidch         */
4682255736Sdavidch
4683255736Sdavidch        break;
4684255736Sdavidch
4685255736Sdavidch    case SIOCSIFMEDIA:
4686255736Sdavidch    case SIOCGIFMEDIA:
4687255736Sdavidch        /* set/get interface media */
4688255736Sdavidch        BLOGD(sc, DBG_IOCTL,
4689255736Sdavidch              "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4690255736Sdavidch              (command & 0xff));
4691270876Sglebius        error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4692255736Sdavidch        break;
4693255736Sdavidch
4694255736Sdavidch    default:
4695255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4696255736Sdavidch              (command & 0xff));
4697270876Sglebius        error = ether_ioctl(ifp, command, data);
4698255736Sdavidch        break;
4699255736Sdavidch    }
4700255736Sdavidch
4701266979Smarcel    if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
4702255736Sdavidch        BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4703255736Sdavidch              "Re-initializing hardware from IOCTL change\n");
4704284335Sdavidcs	bxe_periodic_stop(sc);
4705284335Sdavidcs	BXE_CORE_LOCK(sc);
4706284335Sdavidcs	bxe_stop_locked(sc);
4707284335Sdavidcs	bxe_init_locked(sc);
4708284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4709255736Sdavidch    }
4710255736Sdavidch
4711255736Sdavidch    return (error);
4712255736Sdavidch}
4713255736Sdavidch
4714255736Sdavidchstatic __noinline void
4715255736Sdavidchbxe_dump_mbuf(struct bxe_softc *sc,
4716255736Sdavidch              struct mbuf      *m,
4717255736Sdavidch              uint8_t          contents)
4718255736Sdavidch{
4719255736Sdavidch    char * type;
4720260113Sedavis    int i = 0;
4721255736Sdavidch
4722255736Sdavidch    if (!(sc->debug & DBG_MBUF)) {
4723255736Sdavidch        return;
4724255736Sdavidch    }
4725255736Sdavidch
4726255736Sdavidch    if (m == NULL) {
4727255736Sdavidch        BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4728255736Sdavidch        return;
4729255736Sdavidch    }
4730255736Sdavidch
4731255736Sdavidch    while (m) {
4732297155Sdavidcs
4733297155Sdavidcs#if __FreeBSD_version >= 1000000
4734255736Sdavidch        BLOGD(sc, DBG_MBUF,
4735260113Sedavis              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4736260113Sedavis              i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
4737255736Sdavidch
4738255736Sdavidch        if (m->m_flags & M_PKTHDR) {
4739255736Sdavidch             BLOGD(sc, DBG_MBUF,
4740260113Sedavis                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4741260113Sedavis                   i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
4742260113Sedavis                   (int)m->m_pkthdr.csum_flags, CSUM_BITS);
4743255736Sdavidch        }
4744297155Sdavidcs#else
4745297155Sdavidcs        BLOGD(sc, DBG_MBUF,
4746297155Sdavidcs              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4747297155Sdavidcs              i, m, m->m_len, m->m_flags,
4748297155Sdavidcs              "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
4749255736Sdavidch
4750297155Sdavidcs        if (m->m_flags & M_PKTHDR) {
4751297155Sdavidcs             BLOGD(sc, DBG_MBUF,
4752297155Sdavidcs                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4753297155Sdavidcs                   i, m->m_pkthdr.len, m->m_flags,
4754297155Sdavidcs                   "\20\12M_BCAST\13M_MCAST\14M_FRAG"
4755297155Sdavidcs                   "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
4756297155Sdavidcs                   "\22M_PROMISC\23M_NOFREE",
4757297155Sdavidcs                   (int)m->m_pkthdr.csum_flags,
4758297155Sdavidcs                   "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
4759297155Sdavidcs                   "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
4760297155Sdavidcs                   "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
4761297155Sdavidcs                   "\14CSUM_PSEUDO_HDR");
4762297155Sdavidcs        }
4763297155Sdavidcs#endif /* #if __FreeBSD_version >= 1000000 */
4764297155Sdavidcs
4765255736Sdavidch        if (m->m_flags & M_EXT) {
4766255736Sdavidch            switch (m->m_ext.ext_type) {
4767255736Sdavidch            case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4768255736Sdavidch            case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4769260113Sedavis            case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4770255736Sdavidch            case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4771255736Sdavidch            case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4772255736Sdavidch            case EXT_PACKET:     type = "EXT_PACKET";     break;
4773255736Sdavidch            case EXT_MBUF:       type = "EXT_MBUF";       break;
4774255736Sdavidch            case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4775255736Sdavidch            case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4776255736Sdavidch            case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4777255736Sdavidch            case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4778255736Sdavidch            default:             type = "UNKNOWN";        break;
4779255736Sdavidch            }
4780255736Sdavidch
4781255736Sdavidch            BLOGD(sc, DBG_MBUF,
4782260113Sedavis                  "%02d: - m_ext: %p ext_size=%d type=%s\n",
4783260113Sedavis                  i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4784255736Sdavidch        }
4785255736Sdavidch
4786255736Sdavidch        if (contents) {
4787255736Sdavidch            bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4788255736Sdavidch        }
4789255736Sdavidch
4790255736Sdavidch        m = m->m_next;
4791260113Sedavis        i++;
4792255736Sdavidch    }
4793255736Sdavidch}
4794255736Sdavidch
4795255736Sdavidch/*
4796255736Sdavidch * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4797255736Sdavidch * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4798255736Sdavidch * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4799298955Spfg * The headers comes in a separate bd in FreeBSD so 13-3=10.
4800255736Sdavidch * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4801255736Sdavidch */
4802255736Sdavidchstatic int
4803255736Sdavidchbxe_chktso_window(struct bxe_softc  *sc,
4804255736Sdavidch                  int               nsegs,
4805255736Sdavidch                  bus_dma_segment_t *segs,
4806255736Sdavidch                  struct mbuf       *m)
4807255736Sdavidch{
4808255736Sdavidch    uint32_t num_wnds, wnd_size, wnd_sum;
4809255736Sdavidch    int32_t frag_idx, wnd_idx;
4810255736Sdavidch    unsigned short lso_mss;
4811255736Sdavidch    int defrag;
4812255736Sdavidch
4813255736Sdavidch    defrag = 0;
4814255736Sdavidch    wnd_sum = 0;
4815255736Sdavidch    wnd_size = 10;
4816255736Sdavidch    num_wnds = nsegs - wnd_size;
4817255736Sdavidch    lso_mss = htole16(m->m_pkthdr.tso_segsz);
4818255736Sdavidch
4819255736Sdavidch    /*
4820255736Sdavidch     * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4821255736Sdavidch     * first window sum of data while skipping the first assuming it is the
4822255736Sdavidch     * header in FreeBSD.
4823255736Sdavidch     */
4824255736Sdavidch    for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4825255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4826255736Sdavidch    }
4827255736Sdavidch
4828255736Sdavidch    /* check the first 10 bd window size */
4829255736Sdavidch    if (wnd_sum < lso_mss) {
4830255736Sdavidch        return (1);
4831255736Sdavidch    }
4832255736Sdavidch
4833255736Sdavidch    /* run through the windows */
4834255736Sdavidch    for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4835255736Sdavidch        /* subtract the first mbuf->m_len of the last wndw(-header) */
4836255736Sdavidch        wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4837255736Sdavidch        /* add the next mbuf len to the len of our new window */
4838255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4839255736Sdavidch        if (wnd_sum < lso_mss) {
4840255736Sdavidch            return (1);
4841255736Sdavidch        }
4842255736Sdavidch    }
4843255736Sdavidch
4844255736Sdavidch    return (0);
4845255736Sdavidch}
4846255736Sdavidch
4847255736Sdavidchstatic uint8_t
4848255736Sdavidchbxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4849255736Sdavidch                    struct mbuf         *m,
4850255736Sdavidch                    uint32_t            *parsing_data)
4851255736Sdavidch{
4852255736Sdavidch    struct ether_vlan_header *eh = NULL;
4853255736Sdavidch    struct ip *ip4 = NULL;
4854255736Sdavidch    struct ip6_hdr *ip6 = NULL;
4855255736Sdavidch    caddr_t ip = NULL;
4856255736Sdavidch    struct tcphdr *th = NULL;
4857255736Sdavidch    int e_hlen, ip_hlen, l4_off;
4858255736Sdavidch    uint16_t proto;
4859255736Sdavidch
4860255736Sdavidch    if (m->m_pkthdr.csum_flags == CSUM_IP) {
4861255736Sdavidch        /* no L4 checksum offload needed */
4862255736Sdavidch        return (0);
4863255736Sdavidch    }
4864255736Sdavidch
4865255736Sdavidch    /* get the Ethernet header */
4866255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
4867255736Sdavidch
4868255736Sdavidch    /* handle VLAN encapsulation if present */
4869255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4870255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4871255736Sdavidch        proto  = ntohs(eh->evl_proto);
4872255736Sdavidch    } else {
4873255736Sdavidch        e_hlen = ETHER_HDR_LEN;
4874255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
4875255736Sdavidch    }
4876255736Sdavidch
4877255736Sdavidch    switch (proto) {
4878255736Sdavidch    case ETHERTYPE_IP:
4879255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
4880255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
4881255736Sdavidch                  (struct ip *)m->m_next->m_data :
4882255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
4883255736Sdavidch        /* ip_hl is number of 32-bit words */
4884255736Sdavidch        ip_hlen = (ip4->ip_hl << 2);
4885255736Sdavidch        ip = (caddr_t)ip4;
4886255736Sdavidch        break;
4887255736Sdavidch    case ETHERTYPE_IPV6:
4888255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4889255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4890255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
4891255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
4892255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
4893255736Sdavidch        ip_hlen = sizeof(struct ip6_hdr);
4894255736Sdavidch        ip = (caddr_t)ip6;
4895255736Sdavidch        break;
4896255736Sdavidch    default:
4897255736Sdavidch        /* We can't offload in this case... */
4898255736Sdavidch        /* XXX error stat ??? */
4899255736Sdavidch        return (0);
4900255736Sdavidch    }
4901255736Sdavidch
4902255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4903255736Sdavidch    l4_off = (e_hlen + ip_hlen);
4904255736Sdavidch
4905255736Sdavidch    *parsing_data |=
4906255736Sdavidch        (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
4907255736Sdavidch         ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
4908255736Sdavidch
4909255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4910255736Sdavidch                                  CSUM_TSO |
4911255736Sdavidch                                  CSUM_TCP_IPV6)) {
4912255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4913255736Sdavidch        th = (struct tcphdr *)(ip + ip_hlen);
4914255736Sdavidch        /* th_off is number of 32-bit words */
4915255736Sdavidch        *parsing_data |= ((th->th_off <<
4916255736Sdavidch                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
4917255736Sdavidch                          ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
4918255736Sdavidch        return (l4_off + (th->th_off << 2)); /* entire header length */
4919255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4920255736Sdavidch                                         CSUM_UDP_IPV6)) {
4921255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4922255736Sdavidch        return (l4_off + sizeof(struct udphdr)); /* entire header length */
4923255736Sdavidch    } else {
4924255736Sdavidch        /* XXX error stat ??? */
4925255736Sdavidch        return (0);
4926255736Sdavidch    }
4927255736Sdavidch}
4928255736Sdavidch
4929255736Sdavidchstatic uint8_t
4930255736Sdavidchbxe_set_pbd_csum(struct bxe_fastpath        *fp,
4931255736Sdavidch                 struct mbuf                *m,
4932255736Sdavidch                 struct eth_tx_parse_bd_e1x *pbd)
4933255736Sdavidch{
4934255736Sdavidch    struct ether_vlan_header *eh = NULL;
4935255736Sdavidch    struct ip *ip4 = NULL;
4936255736Sdavidch    struct ip6_hdr *ip6 = NULL;
4937255736Sdavidch    caddr_t ip = NULL;
4938255736Sdavidch    struct tcphdr *th = NULL;
4939255736Sdavidch    struct udphdr *uh = NULL;
4940255736Sdavidch    int e_hlen, ip_hlen;
4941255736Sdavidch    uint16_t proto;
4942255736Sdavidch    uint8_t hlen;
4943255736Sdavidch    uint16_t tmp_csum;
4944255736Sdavidch    uint32_t *tmp_uh;
4945255736Sdavidch
4946255736Sdavidch    /* get the Ethernet header */
4947255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
4948255736Sdavidch
4949255736Sdavidch    /* handle VLAN encapsulation if present */
4950255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4951255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4952255736Sdavidch        proto  = ntohs(eh->evl_proto);
4953255736Sdavidch    } else {
4954255736Sdavidch        e_hlen = ETHER_HDR_LEN;
4955255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
4956255736Sdavidch    }
4957255736Sdavidch
4958255736Sdavidch    switch (proto) {
4959255736Sdavidch    case ETHERTYPE_IP:
4960255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
4961255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
4962255736Sdavidch                  (struct ip *)m->m_next->m_data :
4963255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
4964255736Sdavidch        /* ip_hl is number of 32-bit words */
4965255736Sdavidch        ip_hlen = (ip4->ip_hl << 1);
4966255736Sdavidch        ip = (caddr_t)ip4;
4967255736Sdavidch        break;
4968255736Sdavidch    case ETHERTYPE_IPV6:
4969255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4970255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4971255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
4972255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
4973255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
4974255736Sdavidch        ip_hlen = (sizeof(struct ip6_hdr) >> 1);
4975255736Sdavidch        ip = (caddr_t)ip6;
4976255736Sdavidch        break;
4977255736Sdavidch    default:
4978255736Sdavidch        /* We can't offload in this case... */
4979255736Sdavidch        /* XXX error stat ??? */
4980255736Sdavidch        return (0);
4981255736Sdavidch    }
4982255736Sdavidch
4983255736Sdavidch    hlen = (e_hlen >> 1);
4984255736Sdavidch
4985255736Sdavidch    /* note that rest of global_data is indirectly zeroed here */
4986255736Sdavidch    if (m->m_flags & M_VLANTAG) {
4987255736Sdavidch        pbd->global_data =
4988255736Sdavidch            htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
4989255736Sdavidch    } else {
4990255736Sdavidch        pbd->global_data = htole16(hlen);
4991255736Sdavidch    }
4992255736Sdavidch
4993255736Sdavidch    pbd->ip_hlen_w = ip_hlen;
4994255736Sdavidch
4995255736Sdavidch    hlen += pbd->ip_hlen_w;
4996255736Sdavidch
4997255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4998255736Sdavidch
4999255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5000255736Sdavidch                                  CSUM_TSO |
5001255736Sdavidch                                  CSUM_TCP_IPV6)) {
5002255736Sdavidch        th = (struct tcphdr *)(ip + (ip_hlen << 1));
5003255736Sdavidch        /* th_off is number of 32-bit words */
5004255736Sdavidch        hlen += (uint16_t)(th->th_off << 1);
5005255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5006255736Sdavidch                                         CSUM_UDP_IPV6)) {
5007255736Sdavidch        uh = (struct udphdr *)(ip + (ip_hlen << 1));
5008255736Sdavidch        hlen += (sizeof(struct udphdr) / 2);
5009255736Sdavidch    } else {
5010255736Sdavidch        /* valid case as only CSUM_IP was set */
5011255736Sdavidch        return (0);
5012255736Sdavidch    }
5013255736Sdavidch
5014255736Sdavidch    pbd->total_hlen_w = htole16(hlen);
5015255736Sdavidch
5016255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5017255736Sdavidch                                  CSUM_TSO |
5018255736Sdavidch                                  CSUM_TCP_IPV6)) {
5019255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5020255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5021255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5022255736Sdavidch                                         CSUM_UDP_IPV6)) {
5023255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5024255736Sdavidch
5025255736Sdavidch        /*
5026255736Sdavidch         * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5027255736Sdavidch         * checksums and does not know anything about the UDP header and where
5028255736Sdavidch         * the checksum field is located. It only knows about TCP. Therefore
5029255736Sdavidch         * we "lie" to the hardware for outgoing UDP packets w/ checksum
5030255736Sdavidch         * offload. Since the checksum field offset for TCP is 16 bytes and
5031255736Sdavidch         * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5032255736Sdavidch         * bytes less than the start of the UDP header. This allows the
5033255736Sdavidch         * hardware to write the checksum in the correct spot. But the
5034255736Sdavidch         * hardware will compute a checksum which includes the last 10 bytes
5035255736Sdavidch         * of the IP header. To correct this we tweak the stack computed
5036255736Sdavidch         * pseudo checksum by folding in the calculation of the inverse
5037255736Sdavidch         * checksum for those final 10 bytes of the IP header. This allows
5038255736Sdavidch         * the correct checksum to be computed by the hardware.
5039255736Sdavidch         */
5040255736Sdavidch
5041255736Sdavidch        /* set pointer 10 bytes before UDP header */
5042255736Sdavidch        tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5043255736Sdavidch
5044255736Sdavidch        /* calculate a pseudo header checksum over the first 10 bytes */
5045255736Sdavidch        tmp_csum = in_pseudo(*tmp_uh,
5046255736Sdavidch                             *(tmp_uh + 1),
5047255736Sdavidch                             *(uint16_t *)(tmp_uh + 2));
5048255736Sdavidch
5049255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5050255736Sdavidch    }
5051255736Sdavidch
5052255736Sdavidch    return (hlen * 2); /* entire header length, number of bytes */
5053255736Sdavidch}
5054255736Sdavidch
5055255736Sdavidchstatic void
5056255736Sdavidchbxe_set_pbd_lso_e2(struct mbuf *m,
5057255736Sdavidch                   uint32_t    *parsing_data)
5058255736Sdavidch{
5059255736Sdavidch    *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5060255736Sdavidch                       ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5061255736Sdavidch                      ETH_TX_PARSE_BD_E2_LSO_MSS);
5062255736Sdavidch
5063255736Sdavidch    /* XXX test for IPv6 with extension header... */
5064255736Sdavidch}
5065255736Sdavidch
5066255736Sdavidchstatic void
5067255736Sdavidchbxe_set_pbd_lso(struct mbuf                *m,
5068255736Sdavidch                struct eth_tx_parse_bd_e1x *pbd)
5069255736Sdavidch{
5070255736Sdavidch    struct ether_vlan_header *eh = NULL;
5071255736Sdavidch    struct ip *ip = NULL;
5072255736Sdavidch    struct tcphdr *th = NULL;
5073255736Sdavidch    int e_hlen;
5074255736Sdavidch
5075255736Sdavidch    /* get the Ethernet header */
5076255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5077255736Sdavidch
5078255736Sdavidch    /* handle VLAN encapsulation if present */
5079255736Sdavidch    e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5080255736Sdavidch                 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5081255736Sdavidch
5082255736Sdavidch    /* get the IP and TCP header, with LSO entire header in first mbuf */
5083255736Sdavidch    /* XXX assuming IPv4 */
5084255736Sdavidch    ip = (struct ip *)(m->m_data + e_hlen);
5085255736Sdavidch    th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5086255736Sdavidch
5087255736Sdavidch    pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5088255736Sdavidch    pbd->tcp_send_seq = ntohl(th->th_seq);
5089255736Sdavidch    pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5090255736Sdavidch
5091255736Sdavidch#if 1
5092255736Sdavidch        /* XXX IPv4 */
5093255736Sdavidch        pbd->ip_id = ntohs(ip->ip_id);
5094255736Sdavidch        pbd->tcp_pseudo_csum =
5095255736Sdavidch            ntohs(in_pseudo(ip->ip_src.s_addr,
5096255736Sdavidch                            ip->ip_dst.s_addr,
5097255736Sdavidch                            htons(IPPROTO_TCP)));
5098255736Sdavidch#else
5099255736Sdavidch        /* XXX IPv6 */
5100255736Sdavidch        pbd->tcp_pseudo_csum =
5101255736Sdavidch            ntohs(in_pseudo(&ip6->ip6_src,
5102255736Sdavidch                            &ip6->ip6_dst,
5103255736Sdavidch                            htons(IPPROTO_TCP)));
5104255736Sdavidch#endif
5105255736Sdavidch
5106255736Sdavidch    pbd->global_data |=
5107255736Sdavidch        htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5108255736Sdavidch}
5109255736Sdavidch
5110255736Sdavidch/*
5111255736Sdavidch * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5112255736Sdavidch * visible to the controller.
5113255736Sdavidch *
5114255736Sdavidch * If an mbuf is submitted to this routine and cannot be given to the
5115255736Sdavidch * controller (e.g. it has too many fragments) then the function may free
5116255736Sdavidch * the mbuf and return to the caller.
5117255736Sdavidch *
5118255736Sdavidch * Returns:
5119255736Sdavidch *   0 = Success, !0 = Failure
5120255736Sdavidch *   Note the side effect that an mbuf may be freed if it causes a problem.
5121255736Sdavidch */
5122255736Sdavidchstatic int
5123255736Sdavidchbxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5124255736Sdavidch{
5125255736Sdavidch    bus_dma_segment_t segs[32];
5126255736Sdavidch    struct mbuf *m0;
5127255736Sdavidch    struct bxe_sw_tx_bd *tx_buf;
5128255736Sdavidch    struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5129255736Sdavidch    struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5130255736Sdavidch    /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5131255736Sdavidch    struct eth_tx_bd *tx_data_bd;
5132255736Sdavidch    struct eth_tx_bd *tx_total_pkt_size_bd;
5133255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
5134255736Sdavidch    uint16_t bd_prod, pkt_prod, total_pkt_size;
5135255736Sdavidch    uint8_t mac_type;
5136255736Sdavidch    int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5137255736Sdavidch    struct bxe_softc *sc;
5138255736Sdavidch    uint16_t tx_bd_avail;
5139255736Sdavidch    struct ether_vlan_header *eh;
5140255736Sdavidch    uint32_t pbd_e2_parsing_data = 0;
5141255736Sdavidch    uint8_t hlen = 0;
5142255736Sdavidch    int tmp_bd;
5143255736Sdavidch    int i;
5144255736Sdavidch
5145255736Sdavidch    sc = fp->sc;
5146255736Sdavidch
5147297155Sdavidcs#if __FreeBSD_version >= 800000
5148255736Sdavidch    M_ASSERTPKTHDR(*m_head);
5149297155Sdavidcs#endif /* #if __FreeBSD_version >= 800000 */
5150255736Sdavidch
5151255736Sdavidch    m0 = *m_head;
5152255736Sdavidch    rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5153255736Sdavidch    tx_start_bd = NULL;
5154255736Sdavidch    tx_data_bd = NULL;
5155255736Sdavidch    tx_total_pkt_size_bd = NULL;
5156255736Sdavidch
5157255736Sdavidch    /* get the H/W pointer for packets and BDs */
5158255736Sdavidch    pkt_prod = fp->tx_pkt_prod;
5159255736Sdavidch    bd_prod = fp->tx_bd_prod;
5160255736Sdavidch
5161255736Sdavidch    mac_type = UNICAST_ADDRESS;
5162255736Sdavidch
5163255736Sdavidch    /* map the mbuf into the next open DMAable memory */
5164255736Sdavidch    tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5165255736Sdavidch    error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5166255736Sdavidch                                    tx_buf->m_map, m0,
5167255736Sdavidch                                    segs, &nsegs, BUS_DMA_NOWAIT);
5168255736Sdavidch
5169255736Sdavidch    /* mapping errors */
5170255736Sdavidch    if(__predict_false(error != 0)) {
5171255736Sdavidch        fp->eth_q_stats.tx_dma_mapping_failure++;
5172255736Sdavidch        if (error == ENOMEM) {
5173255736Sdavidch            /* resource issue, try again later */
5174255736Sdavidch            rc = ENOMEM;
5175255736Sdavidch        } else if (error == EFBIG) {
5176255736Sdavidch            /* possibly recoverable with defragmentation */
5177255736Sdavidch            fp->eth_q_stats.mbuf_defrag_attempts++;
5178260718Sglebius            m0 = m_defrag(*m_head, M_NOWAIT);
5179255736Sdavidch            if (m0 == NULL) {
5180255736Sdavidch                fp->eth_q_stats.mbuf_defrag_failures++;
5181255736Sdavidch                rc = ENOBUFS;
5182255736Sdavidch            } else {
5183255736Sdavidch                /* defrag successful, try mapping again */
5184255736Sdavidch                *m_head = m0;
5185255736Sdavidch                error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5186255736Sdavidch                                                tx_buf->m_map, m0,
5187255736Sdavidch                                                segs, &nsegs, BUS_DMA_NOWAIT);
5188255736Sdavidch                if (error) {
5189255736Sdavidch                    fp->eth_q_stats.tx_dma_mapping_failure++;
5190255736Sdavidch                    rc = error;
5191255736Sdavidch                }
5192255736Sdavidch            }
5193255736Sdavidch        } else {
5194255736Sdavidch            /* unknown, unrecoverable mapping error */
5195255736Sdavidch            BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5196255736Sdavidch            bxe_dump_mbuf(sc, m0, FALSE);
5197255736Sdavidch            rc = error;
5198255736Sdavidch        }
5199255736Sdavidch
5200255736Sdavidch        goto bxe_tx_encap_continue;
5201255736Sdavidch    }
5202255736Sdavidch
5203255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
5204255736Sdavidch
5205255736Sdavidch    /* make sure there is enough room in the send queue */
5206255736Sdavidch    if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5207255736Sdavidch        /* Recoverable, try again later. */
5208255736Sdavidch        fp->eth_q_stats.tx_hw_queue_full++;
5209255736Sdavidch        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5210255736Sdavidch        rc = ENOMEM;
5211255736Sdavidch        goto bxe_tx_encap_continue;
5212255736Sdavidch    }
5213255736Sdavidch
5214255736Sdavidch    /* capture the current H/W TX chain high watermark */
5215255736Sdavidch    if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5216255736Sdavidch                        (TX_BD_USABLE - tx_bd_avail))) {
5217255736Sdavidch        fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5218255736Sdavidch    }
5219255736Sdavidch
5220255736Sdavidch    /* make sure it fits in the packet window */
5221262999Sedavis    if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5222255736Sdavidch        /*
5223255736Sdavidch         * The mbuf may be to big for the controller to handle. If the frame
5224255736Sdavidch         * is a TSO frame we'll need to do an additional check.
5225255736Sdavidch         */
5226255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5227255736Sdavidch            if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5228255736Sdavidch                goto bxe_tx_encap_continue; /* OK to send */
5229255736Sdavidch            } else {
5230255736Sdavidch                fp->eth_q_stats.tx_window_violation_tso++;
5231255736Sdavidch            }
5232255736Sdavidch        } else {
5233255736Sdavidch            fp->eth_q_stats.tx_window_violation_std++;
5234255736Sdavidch        }
5235255736Sdavidch
5236262999Sedavis        /* lets try to defragment this mbuf and remap it */
5237260415Sedavis        fp->eth_q_stats.mbuf_defrag_attempts++;
5238262999Sedavis        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5239255736Sdavidch
5240260718Sglebius        m0 = m_defrag(*m_head, M_NOWAIT);
5241260415Sedavis        if (m0 == NULL) {
5242260415Sedavis            fp->eth_q_stats.mbuf_defrag_failures++;
5243260415Sedavis            /* Ugh, just drop the frame... :( */
5244260415Sedavis            rc = ENOBUFS;
5245260415Sedavis        } else {
5246260415Sedavis            /* defrag successful, try mapping again */
5247260415Sedavis            *m_head = m0;
5248260415Sedavis            error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5249260415Sedavis                                            tx_buf->m_map, m0,
5250260415Sedavis                                            segs, &nsegs, BUS_DMA_NOWAIT);
5251260415Sedavis            if (error) {
5252260415Sedavis                fp->eth_q_stats.tx_dma_mapping_failure++;
5253260415Sedavis                /* No sense in trying to defrag/copy chain, drop it. :( */
5254260415Sedavis                rc = error;
5255315881Sdavidcs            } else {
5256315881Sdavidcs               /* if the chain is still too long then drop it */
5257315881Sdavidcs                if(m0->m_pkthdr.csum_flags & CSUM_TSO) {
5258315881Sdavidcs                    /*
5259315881Sdavidcs                     * in case TSO is enabled nsegs should be checked against
5260315881Sdavidcs                     * BXE_TSO_MAX_SEGMENTS
5261315881Sdavidcs                     */
5262315881Sdavidcs                    if (__predict_false(nsegs > BXE_TSO_MAX_SEGMENTS)) {
5263315881Sdavidcs                        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5264315881Sdavidcs                        fp->eth_q_stats.nsegs_path1_errors++;
5265315881Sdavidcs                        rc = ENODEV;
5266315881Sdavidcs                    }
5267315881Sdavidcs                } else {
5268315881Sdavidcs                    if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5269315881Sdavidcs                        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5270315881Sdavidcs                        fp->eth_q_stats.nsegs_path2_errors++;
5271315881Sdavidcs                        rc = ENODEV;
5272315881Sdavidcs                    }
5273262999Sedavis                }
5274260415Sedavis            }
5275260415Sedavis        }
5276255736Sdavidch    }
5277255736Sdavidch
5278255736Sdavidchbxe_tx_encap_continue:
5279255736Sdavidch
5280255736Sdavidch    /* Check for errors */
5281255736Sdavidch    if (rc) {
5282255736Sdavidch        if (rc == ENOMEM) {
5283255736Sdavidch            /* recoverable try again later  */
5284255736Sdavidch        } else {
5285255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
5286255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tx--;
5287255736Sdavidch            m_freem(*m_head);
5288255736Sdavidch            *m_head = NULL;
5289255736Sdavidch        }
5290255736Sdavidch
5291255736Sdavidch        return (rc);
5292255736Sdavidch    }
5293255736Sdavidch
5294255736Sdavidch    /* set flag according to packet type (UNICAST_ADDRESS is default) */
5295255736Sdavidch    if (m0->m_flags & M_BCAST) {
5296255736Sdavidch        mac_type = BROADCAST_ADDRESS;
5297255736Sdavidch    } else if (m0->m_flags & M_MCAST) {
5298255736Sdavidch        mac_type = MULTICAST_ADDRESS;
5299255736Sdavidch    }
5300255736Sdavidch
5301255736Sdavidch    /* store the mbuf into the mbuf ring */
5302255736Sdavidch    tx_buf->m        = m0;
5303255736Sdavidch    tx_buf->first_bd = fp->tx_bd_prod;
5304255736Sdavidch    tx_buf->flags    = 0;
5305255736Sdavidch
5306255736Sdavidch    /* prepare the first transmit (start) BD for the mbuf */
5307255736Sdavidch    tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5308255736Sdavidch
5309255736Sdavidch    BLOGD(sc, DBG_TX,
5310255736Sdavidch          "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5311255736Sdavidch          pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5312255736Sdavidch
5313255736Sdavidch    tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5314255736Sdavidch    tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5315255736Sdavidch    tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5316255736Sdavidch    total_pkt_size += tx_start_bd->nbytes;
5317255736Sdavidch    tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5318255736Sdavidch
5319255736Sdavidch    tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5320255736Sdavidch
5321255736Sdavidch    /* all frames have at least Start BD + Parsing BD */
5322255736Sdavidch    nbds = nsegs + 1;
5323255736Sdavidch    tx_start_bd->nbd = htole16(nbds);
5324255736Sdavidch
5325255736Sdavidch    if (m0->m_flags & M_VLANTAG) {
5326255736Sdavidch        tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5327255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |=
5328255736Sdavidch            (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5329255736Sdavidch    } else {
5330255736Sdavidch        /* vf tx, start bd must hold the ethertype for fw to enforce it */
5331255736Sdavidch        if (IS_VF(sc)) {
5332255736Sdavidch            /* map ethernet header to find type and header length */
5333255736Sdavidch            eh = mtod(m0, struct ether_vlan_header *);
5334255736Sdavidch            tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5335255736Sdavidch        } else {
5336255736Sdavidch            /* used by FW for packet accounting */
5337255736Sdavidch            tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5338255736Sdavidch        }
5339255736Sdavidch    }
5340255736Sdavidch
5341255736Sdavidch    /*
5342255736Sdavidch     * add a parsing BD from the chain. The parsing BD is always added
5343255736Sdavidch     * though it is only used for TSO and chksum
5344255736Sdavidch     */
5345255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5346255736Sdavidch
5347255736Sdavidch    if (m0->m_pkthdr.csum_flags) {
5348255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5349255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5350255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5351255736Sdavidch        }
5352255736Sdavidch
5353255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5354255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5355255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5356255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5357255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5358255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP |
5359255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5360255736Sdavidch        } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5361255736Sdavidch                   (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5362255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5363255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5364255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5365255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP);
5366255736Sdavidch        }
5367255736Sdavidch    }
5368255736Sdavidch
5369255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
5370255736Sdavidch        pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5371255736Sdavidch        memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5372255736Sdavidch
5373255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5374255736Sdavidch            hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5375255736Sdavidch        }
5376255736Sdavidch
5377255736Sdavidch        SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5378255736Sdavidch                 mac_type);
5379255736Sdavidch    } else {
5380255736Sdavidch        uint16_t global_data = 0;
5381255736Sdavidch
5382255736Sdavidch        pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5383255736Sdavidch        memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5384255736Sdavidch
5385255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5386255736Sdavidch            hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5387255736Sdavidch        }
5388255736Sdavidch
5389255736Sdavidch        SET_FLAG(global_data,
5390255736Sdavidch                 ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5391255736Sdavidch        pbd_e1x->global_data |= htole16(global_data);
5392255736Sdavidch    }
5393255736Sdavidch
5394255736Sdavidch    /* setup the parsing BD with TSO specific info */
5395255736Sdavidch    if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5396255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_lso++;
5397255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5398255736Sdavidch
5399255736Sdavidch        if (__predict_false(tx_start_bd->nbytes > hlen)) {
5400255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5401255736Sdavidch
5402255736Sdavidch            /* split the first BD into header/data making the fw job easy */
5403255736Sdavidch            nbds++;
5404255736Sdavidch            tx_start_bd->nbd = htole16(nbds);
5405260113Sedavis            tx_start_bd->nbytes = htole16(hlen);
5406255736Sdavidch
5407255736Sdavidch            bd_prod = TX_BD_NEXT(bd_prod);
5408255736Sdavidch
5409255736Sdavidch            /* new transmit BD after the tx_parse_bd */
5410255736Sdavidch            tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5411255736Sdavidch            tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5412255736Sdavidch            tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5413255736Sdavidch            tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5414255736Sdavidch            if (tx_total_pkt_size_bd == NULL) {
5415255736Sdavidch                tx_total_pkt_size_bd = tx_data_bd;
5416255736Sdavidch            }
5417255736Sdavidch
5418255736Sdavidch            BLOGD(sc, DBG_TX,
5419255736Sdavidch                  "TSO split header size is %d (%x:%x) nbds %d\n",
5420255736Sdavidch                  le16toh(tx_start_bd->nbytes),
5421255736Sdavidch                  le32toh(tx_start_bd->addr_hi),
5422255736Sdavidch                  le32toh(tx_start_bd->addr_lo),
5423255736Sdavidch                  nbds);
5424255736Sdavidch        }
5425255736Sdavidch
5426255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
5427255736Sdavidch            bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5428255736Sdavidch        } else {
5429255736Sdavidch            bxe_set_pbd_lso(m0, pbd_e1x);
5430255736Sdavidch        }
5431255736Sdavidch    }
5432255736Sdavidch
5433255736Sdavidch    if (pbd_e2_parsing_data) {
5434255736Sdavidch        pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5435255736Sdavidch    }
5436255736Sdavidch
5437255736Sdavidch    /* prepare remaining BDs, start tx bd contains first seg/frag */
5438255736Sdavidch    for (i = 1; i < nsegs ; i++) {
5439255736Sdavidch        bd_prod = TX_BD_NEXT(bd_prod);
5440255736Sdavidch        tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5441255736Sdavidch        tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5442255736Sdavidch        tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5443255736Sdavidch        tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5444255736Sdavidch        if (tx_total_pkt_size_bd == NULL) {
5445255736Sdavidch            tx_total_pkt_size_bd = tx_data_bd;
5446255736Sdavidch        }
5447255736Sdavidch        total_pkt_size += tx_data_bd->nbytes;
5448255736Sdavidch    }
5449255736Sdavidch
5450255736Sdavidch    BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5451255736Sdavidch
5452255736Sdavidch    if (tx_total_pkt_size_bd != NULL) {
5453255736Sdavidch        tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5454255736Sdavidch    }
5455255736Sdavidch
5456255736Sdavidch    if (__predict_false(sc->debug & DBG_TX)) {
5457255736Sdavidch        tmp_bd = tx_buf->first_bd;
5458255736Sdavidch        for (i = 0; i < nbds; i++)
5459255736Sdavidch        {
5460255736Sdavidch            if (i == 0) {
5461255736Sdavidch                BLOGD(sc, DBG_TX,
5462255736Sdavidch                      "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5463255736Sdavidch                      "bd_flags=0x%x hdr_nbds=%d\n",
5464255736Sdavidch                      tx_start_bd,
5465255736Sdavidch                      tmp_bd,
5466255736Sdavidch                      le16toh(tx_start_bd->nbd),
5467255736Sdavidch                      le16toh(tx_start_bd->vlan_or_ethertype),
5468255736Sdavidch                      tx_start_bd->bd_flags.as_bitfield,
5469255736Sdavidch                      (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5470255736Sdavidch            } else if (i == 1) {
5471255736Sdavidch                if (pbd_e1x) {
5472255736Sdavidch                    BLOGD(sc, DBG_TX,
5473255736Sdavidch                          "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5474255736Sdavidch                          "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5475255736Sdavidch                          "tcp_seq=%u total_hlen_w=%u\n",
5476255736Sdavidch                          pbd_e1x,
5477255736Sdavidch                          tmp_bd,
5478255736Sdavidch                          pbd_e1x->global_data,
5479255736Sdavidch                          pbd_e1x->ip_hlen_w,
5480255736Sdavidch                          pbd_e1x->ip_id,
5481255736Sdavidch                          pbd_e1x->lso_mss,
5482255736Sdavidch                          pbd_e1x->tcp_flags,
5483255736Sdavidch                          pbd_e1x->tcp_pseudo_csum,
5484255736Sdavidch                          pbd_e1x->tcp_send_seq,
5485255736Sdavidch                          le16toh(pbd_e1x->total_hlen_w));
5486255736Sdavidch                } else { /* if (pbd_e2) */
5487255736Sdavidch                    BLOGD(sc, DBG_TX,
5488255736Sdavidch                          "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5489255736Sdavidch                          "src=%02x:%02x:%02x parsing_data=0x%x\n",
5490255736Sdavidch                          pbd_e2,
5491255736Sdavidch                          tmp_bd,
5492255736Sdavidch                          pbd_e2->data.mac_addr.dst_hi,
5493255736Sdavidch                          pbd_e2->data.mac_addr.dst_mid,
5494255736Sdavidch                          pbd_e2->data.mac_addr.dst_lo,
5495255736Sdavidch                          pbd_e2->data.mac_addr.src_hi,
5496255736Sdavidch                          pbd_e2->data.mac_addr.src_mid,
5497255736Sdavidch                          pbd_e2->data.mac_addr.src_lo,
5498255736Sdavidch                          pbd_e2->parsing_data);
5499255736Sdavidch                }
5500255736Sdavidch            }
5501255736Sdavidch
5502255736Sdavidch            if (i != 1) { /* skip parse db as it doesn't hold data */
5503255736Sdavidch                tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5504255736Sdavidch                BLOGD(sc, DBG_TX,
5505255736Sdavidch                      "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5506255736Sdavidch                      tx_data_bd,
5507255736Sdavidch                      tmp_bd,
5508255736Sdavidch                      le16toh(tx_data_bd->nbytes),
5509255736Sdavidch                      le32toh(tx_data_bd->addr_hi),
5510255736Sdavidch                      le32toh(tx_data_bd->addr_lo));
5511255736Sdavidch            }
5512255736Sdavidch
5513255736Sdavidch            tmp_bd = TX_BD_NEXT(tmp_bd);
5514255736Sdavidch        }
5515255736Sdavidch    }
5516255736Sdavidch
5517255736Sdavidch    BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5518255736Sdavidch
5519255736Sdavidch    /* update TX BD producer index value for next TX */
5520255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5521255736Sdavidch
5522255736Sdavidch    /*
5523255736Sdavidch     * If the chain of tx_bd's describing this frame is adjacent to or spans
5524255736Sdavidch     * an eth_tx_next_bd element then we need to increment the nbds value.
5525255736Sdavidch     */
5526255736Sdavidch    if (TX_BD_IDX(bd_prod) < nbds) {
5527255736Sdavidch        nbds++;
5528255736Sdavidch    }
5529255736Sdavidch
5530255736Sdavidch    /* don't allow reordering of writes for nbd and packets */
5531255736Sdavidch    mb();
5532255736Sdavidch
5533255736Sdavidch    fp->tx_db.data.prod += nbds;
5534255736Sdavidch
5535255736Sdavidch    /* producer points to the next free tx_bd at this point */
5536255736Sdavidch    fp->tx_pkt_prod++;
5537255736Sdavidch    fp->tx_bd_prod = bd_prod;
5538255736Sdavidch
5539255736Sdavidch    DOORBELL(sc, fp->index, fp->tx_db.raw);
5540255736Sdavidch
5541255736Sdavidch    fp->eth_q_stats.tx_pkts++;
5542255736Sdavidch
5543255736Sdavidch    /* Prevent speculative reads from getting ahead of the status block. */
5544255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5545255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5546255736Sdavidch
5547255736Sdavidch    /* Prevent speculative reads from getting ahead of the doorbell. */
5548255736Sdavidch    bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5549255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5550255736Sdavidch
5551255736Sdavidch    return (0);
5552255736Sdavidch}
5553255736Sdavidch
5554255736Sdavidchstatic void
5555266979Smarcelbxe_tx_start_locked(struct bxe_softc *sc,
5556266979Smarcel                    if_t ifp,
5557255736Sdavidch                    struct bxe_fastpath *fp)
5558255736Sdavidch{
5559255736Sdavidch    struct mbuf *m = NULL;
5560255736Sdavidch    int tx_count = 0;
5561255736Sdavidch    uint16_t tx_bd_avail;
5562255736Sdavidch
5563255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
5564255736Sdavidch
5565255736Sdavidch    /* keep adding entries while there are frames to send */
5566266979Smarcel    while (!if_sendq_empty(ifp)) {
5567255736Sdavidch
5568255736Sdavidch        /*
5569255736Sdavidch         * check for any frames to send
5570255736Sdavidch         * dequeue can still be NULL even if queue is not empty
5571255736Sdavidch         */
5572266979Smarcel        m = if_dequeue(ifp);
5573255736Sdavidch        if (__predict_false(m == NULL)) {
5574255736Sdavidch            break;
5575255736Sdavidch        }
5576255736Sdavidch
5577255736Sdavidch        /* the mbuf now belongs to us */
5578255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5579255736Sdavidch
5580255736Sdavidch        /*
5581255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5582255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5583255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5584255736Sdavidch         */
5585255736Sdavidch        if (__predict_false(bxe_tx_encap(fp, &m))) {
5586255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5587255736Sdavidch            if (m != NULL) {
5588255736Sdavidch                /* mark the TX queue as full and return the frame */
5589266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5590266979Smarcel		if_sendq_prepend(ifp, m);
5591255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5592255736Sdavidch                fp->eth_q_stats.tx_queue_xoff++;
5593255736Sdavidch            }
5594255736Sdavidch
5595255736Sdavidch            /* stop looking for more work */
5596255736Sdavidch            break;
5597255736Sdavidch        }
5598255736Sdavidch
5599255736Sdavidch        /* the frame was enqueued successfully */
5600255736Sdavidch        tx_count++;
5601255736Sdavidch
5602255736Sdavidch        /* send a copy of the frame to any BPF listeners. */
5603266979Smarcel        if_etherbpfmtap(ifp, m);
5604255736Sdavidch
5605255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
5606255736Sdavidch
5607255736Sdavidch        /* handle any completions if we're running low */
5608255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5609258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5610255736Sdavidch            bxe_txeof(sc, fp);
5611266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5612258187Sedavis                break;
5613258187Sedavis            }
5614255736Sdavidch        }
5615255736Sdavidch    }
5616255736Sdavidch
5617255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5618255736Sdavidch    if (tx_count > 0) {
5619255736Sdavidch        /* reset the TX watchdog timeout timer */
5620255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5621255736Sdavidch    }
5622255736Sdavidch}
5623255736Sdavidch
5624255736Sdavidch/* Legacy (non-RSS) dispatch routine */
5625255736Sdavidchstatic void
5626266979Smarcelbxe_tx_start(if_t ifp)
5627255736Sdavidch{
5628255736Sdavidch    struct bxe_softc *sc;
5629255736Sdavidch    struct bxe_fastpath *fp;
5630255736Sdavidch
5631266979Smarcel    sc = if_getsoftc(ifp);
5632255736Sdavidch
5633266979Smarcel    if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5634255736Sdavidch        BLOGW(sc, "Interface not running, ignoring transmit request\n");
5635255736Sdavidch        return;
5636255736Sdavidch    }
5637255736Sdavidch
5638255736Sdavidch    if (!sc->link_vars.link_up) {
5639255736Sdavidch        BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5640255736Sdavidch        return;
5641255736Sdavidch    }
5642255736Sdavidch
5643255736Sdavidch    fp = &sc->fp[0];
5644255736Sdavidch
5645297873Sdavidcs    if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5646297873Sdavidcs        fp->eth_q_stats.tx_queue_full_return++;
5647297873Sdavidcs        return;
5648297873Sdavidcs    }
5649297873Sdavidcs
5650255736Sdavidch    BXE_FP_TX_LOCK(fp);
5651255736Sdavidch    bxe_tx_start_locked(sc, ifp, fp);
5652255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
5653255736Sdavidch}
5654255736Sdavidch
5655307972Sdavidcs#if __FreeBSD_version >= 901504
5656255736Sdavidch
5657255736Sdavidchstatic int
5658255736Sdavidchbxe_tx_mq_start_locked(struct bxe_softc    *sc,
5659266979Smarcel                       if_t                ifp,
5660255736Sdavidch                       struct bxe_fastpath *fp,
5661255736Sdavidch                       struct mbuf         *m)
5662255736Sdavidch{
5663255736Sdavidch    struct buf_ring *tx_br = fp->tx_br;
5664255736Sdavidch    struct mbuf *next;
5665255736Sdavidch    int depth, rc, tx_count;
5666255736Sdavidch    uint16_t tx_bd_avail;
5667255736Sdavidch
5668255736Sdavidch    rc = tx_count = 0;
5669255736Sdavidch
5670285973Sdavidcs    BXE_FP_TX_LOCK_ASSERT(fp);
5671285973Sdavidcs
5672315881Sdavidcs    if (sc->state != BXE_STATE_OPEN)  {
5673315881Sdavidcs        fp->eth_q_stats.bxe_tx_mq_sc_state_failures++;
5674315881Sdavidcs        return ENETDOWN;
5675315881Sdavidcs    }
5676315881Sdavidcs
5677255736Sdavidch    if (!tx_br) {
5678255736Sdavidch        BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5679255736Sdavidch        return (EINVAL);
5680255736Sdavidch    }
5681255736Sdavidch
5682307972Sdavidcs    if (m != NULL) {
5683307972Sdavidcs        rc = drbr_enqueue(ifp, tx_br, m);
5684307972Sdavidcs        if (rc != 0) {
5685307972Sdavidcs            fp->eth_q_stats.tx_soft_errors++;
5686307972Sdavidcs            goto bxe_tx_mq_start_locked_exit;
5687307972Sdavidcs        }
5688307972Sdavidcs    }
5689307972Sdavidcs
5690307972Sdavidcs    if (!sc->link_vars.link_up || !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
5691307972Sdavidcs        fp->eth_q_stats.tx_request_link_down_failures++;
5692285973Sdavidcs        goto bxe_tx_mq_start_locked_exit;
5693285973Sdavidcs    }
5694285973Sdavidcs
5695255736Sdavidch    /* fetch the depth of the driver queue */
5696266979Smarcel    depth = drbr_inuse_drv(ifp, tx_br);
5697255736Sdavidch    if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5698255736Sdavidch        fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5699255736Sdavidch    }
5700255736Sdavidch
5701307972Sdavidcs    /* keep adding entries while there are frames to send */
5702307972Sdavidcs    while ((next = drbr_peek(ifp, tx_br)) != NULL) {
5703307972Sdavidcs        /* handle any completions if we're running low */
5704307972Sdavidcs        tx_bd_avail = bxe_tx_avail(sc, fp);
5705307972Sdavidcs        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5706307972Sdavidcs            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5707307972Sdavidcs            bxe_txeof(sc, fp);
5708307972Sdavidcs            tx_bd_avail = bxe_tx_avail(sc, fp);
5709307972Sdavidcs            if (tx_bd_avail < (BXE_TSO_MAX_SEGMENTS + 1)) {
5710307972Sdavidcs                fp->eth_q_stats.bd_avail_too_less_failures++;
5711307972Sdavidcs                m_freem(next);
5712307972Sdavidcs                drbr_advance(ifp, tx_br);
5713307972Sdavidcs                rc = ENOBUFS;
5714307972Sdavidcs                break;
5715307972Sdavidcs            }
5716255736Sdavidch        }
5717255736Sdavidch
5718255736Sdavidch        /* the mbuf now belongs to us */
5719255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5720255736Sdavidch
5721255736Sdavidch        /*
5722255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5723255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5724255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5725255736Sdavidch         */
5726255736Sdavidch        rc = bxe_tx_encap(fp, &next);
5727255736Sdavidch        if (__predict_false(rc != 0)) {
5728255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5729255736Sdavidch            if (next != NULL) {
5730255736Sdavidch                /* mark the TX queue as full and save the frame */
5731266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5732307972Sdavidcs                drbr_putback(ifp, tx_br, next);
5733255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5734255736Sdavidch                fp->eth_q_stats.tx_frames_deferred++;
5735307972Sdavidcs            } else
5736307972Sdavidcs                drbr_advance(ifp, tx_br);
5737255736Sdavidch
5738255736Sdavidch            /* stop looking for more work */
5739255736Sdavidch            break;
5740255736Sdavidch        }
5741255736Sdavidch
5742255736Sdavidch        /* the transmit frame was enqueued successfully */
5743255736Sdavidch        tx_count++;
5744255736Sdavidch
5745255736Sdavidch        /* send a copy of the frame to any BPF listeners */
5746266979Smarcel	if_etherbpfmtap(ifp, next);
5747255736Sdavidch
5748307972Sdavidcs        drbr_advance(ifp, tx_br);
5749255736Sdavidch    }
5750255736Sdavidch
5751255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5752255736Sdavidch    if (tx_count > 0) {
5753255736Sdavidch        /* reset the TX watchdog timeout timer */
5754255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5755255736Sdavidch    }
5756255736Sdavidch
5757255736Sdavidchbxe_tx_mq_start_locked_exit:
5758307972Sdavidcs    /* If we didn't drain the drbr, enqueue a task in the future to do it. */
5759307972Sdavidcs    if (!drbr_empty(ifp, tx_br)) {
5760307972Sdavidcs        fp->eth_q_stats.tx_mq_not_empty++;
5761307972Sdavidcs        taskqueue_enqueue_timeout(fp->tq, &fp->tx_timeout_task, 1);
5762307972Sdavidcs    }
5763255736Sdavidch
5764255736Sdavidch    return (rc);
5765255736Sdavidch}
5766255736Sdavidch
5767307972Sdavidcsstatic void
5768307972Sdavidcsbxe_tx_mq_start_deferred(void *arg,
5769307972Sdavidcs                         int pending)
5770307972Sdavidcs{
5771307972Sdavidcs    struct bxe_fastpath *fp = (struct bxe_fastpath *)arg;
5772307972Sdavidcs    struct bxe_softc *sc = fp->sc;
5773307972Sdavidcs    if_t ifp = sc->ifp;
5774307972Sdavidcs
5775307972Sdavidcs    BXE_FP_TX_LOCK(fp);
5776307972Sdavidcs    bxe_tx_mq_start_locked(sc, ifp, fp, NULL);
5777307972Sdavidcs    BXE_FP_TX_UNLOCK(fp);
5778307972Sdavidcs}
5779307972Sdavidcs
5780255736Sdavidch/* Multiqueue (TSS) dispatch routine. */
5781255736Sdavidchstatic int
5782255736Sdavidchbxe_tx_mq_start(struct ifnet *ifp,
5783255736Sdavidch                struct mbuf  *m)
5784255736Sdavidch{
5785266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5786255736Sdavidch    struct bxe_fastpath *fp;
5787255736Sdavidch    int fp_index, rc;
5788255736Sdavidch
5789255736Sdavidch    fp_index = 0; /* default is the first queue */
5790255736Sdavidch
5791275358Shselasky    /* check if flowid is set */
5792297155Sdavidcs
5793297155Sdavidcs    if (BXE_VALID_FLOWID(m))
5794255736Sdavidch        fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5795255736Sdavidch
5796255736Sdavidch    fp = &sc->fp[fp_index];
5797255736Sdavidch
5798315881Sdavidcs    if (sc->state != BXE_STATE_OPEN)  {
5799315881Sdavidcs        fp->eth_q_stats.bxe_tx_mq_sc_state_failures++;
5800315881Sdavidcs        return ENETDOWN;
5801315881Sdavidcs    }
5802315881Sdavidcs
5803285973Sdavidcs    if (BXE_FP_TX_TRYLOCK(fp)) {
5804285973Sdavidcs        rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5805285973Sdavidcs        BXE_FP_TX_UNLOCK(fp);
5806307972Sdavidcs    } else {
5807297155Sdavidcs        rc = drbr_enqueue(ifp, fp->tx_br, m);
5808307972Sdavidcs        taskqueue_enqueue(fp->tq, &fp->tx_task);
5809307972Sdavidcs    }
5810255736Sdavidch
5811255736Sdavidch    return (rc);
5812255736Sdavidch}
5813255736Sdavidch
5814255736Sdavidchstatic void
5815255736Sdavidchbxe_mq_flush(struct ifnet *ifp)
5816255736Sdavidch{
5817266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5818255736Sdavidch    struct bxe_fastpath *fp;
5819255736Sdavidch    struct mbuf *m;
5820255736Sdavidch    int i;
5821255736Sdavidch
5822255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5823255736Sdavidch        fp = &sc->fp[i];
5824255736Sdavidch
5825315881Sdavidcs        if (fp->state != BXE_FP_STATE_IRQ) {
5826255736Sdavidch            BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5827255736Sdavidch                  fp->index, fp->state);
5828255736Sdavidch            continue;
5829255736Sdavidch        }
5830255736Sdavidch
5831255736Sdavidch        if (fp->tx_br != NULL) {
5832255736Sdavidch            BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5833255736Sdavidch            BXE_FP_TX_LOCK(fp);
5834255736Sdavidch            while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5835255736Sdavidch                m_freem(m);
5836255736Sdavidch            }
5837255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
5838255736Sdavidch        }
5839255736Sdavidch    }
5840255736Sdavidch
5841270876Sglebius    if_qflush(ifp);
5842255736Sdavidch}
5843255736Sdavidch
5844307972Sdavidcs#endif /* FreeBSD_version >= 901504 */
5845255736Sdavidch
5846255736Sdavidchstatic uint16_t
5847255736Sdavidchbxe_cid_ilt_lines(struct bxe_softc *sc)
5848255736Sdavidch{
5849255736Sdavidch    if (IS_SRIOV(sc)) {
5850255736Sdavidch        return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5851255736Sdavidch    }
5852255736Sdavidch    return (L2_ILT_LINES(sc));
5853255736Sdavidch}
5854255736Sdavidch
5855255736Sdavidchstatic void
5856255736Sdavidchbxe_ilt_set_info(struct bxe_softc *sc)
5857255736Sdavidch{
5858255736Sdavidch    struct ilt_client_info *ilt_client;
5859255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
5860255736Sdavidch    uint16_t line = 0;
5861255736Sdavidch
5862255736Sdavidch    ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5863255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5864255736Sdavidch
5865255736Sdavidch    /* CDU */
5866255736Sdavidch    ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5867255736Sdavidch    ilt_client->client_num = ILT_CLIENT_CDU;
5868255736Sdavidch    ilt_client->page_size = CDU_ILT_PAGE_SZ;
5869255736Sdavidch    ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5870255736Sdavidch    ilt_client->start = line;
5871255736Sdavidch    line += bxe_cid_ilt_lines(sc);
5872255736Sdavidch
5873255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5874255736Sdavidch        line += CNIC_ILT_LINES;
5875255736Sdavidch    }
5876255736Sdavidch
5877255736Sdavidch    ilt_client->end = (line - 1);
5878255736Sdavidch
5879255736Sdavidch    BLOGD(sc, DBG_LOAD,
5880255736Sdavidch          "ilt client[CDU]: start %d, end %d, "
5881255736Sdavidch          "psz 0x%x, flags 0x%x, hw psz %d\n",
5882255736Sdavidch          ilt_client->start, ilt_client->end,
5883255736Sdavidch          ilt_client->page_size,
5884255736Sdavidch          ilt_client->flags,
5885255736Sdavidch          ilog2(ilt_client->page_size >> 12));
5886255736Sdavidch
5887255736Sdavidch    /* QM */
5888255736Sdavidch    if (QM_INIT(sc->qm_cid_count)) {
5889255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_QM];
5890255736Sdavidch        ilt_client->client_num = ILT_CLIENT_QM;
5891255736Sdavidch        ilt_client->page_size = QM_ILT_PAGE_SZ;
5892255736Sdavidch        ilt_client->flags = 0;
5893255736Sdavidch        ilt_client->start = line;
5894255736Sdavidch
5895255736Sdavidch        /* 4 bytes for each cid */
5896255736Sdavidch        line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5897255736Sdavidch                             QM_ILT_PAGE_SZ);
5898255736Sdavidch
5899255736Sdavidch        ilt_client->end = (line - 1);
5900255736Sdavidch
5901255736Sdavidch        BLOGD(sc, DBG_LOAD,
5902255736Sdavidch              "ilt client[QM]: start %d, end %d, "
5903255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5904255736Sdavidch              ilt_client->start, ilt_client->end,
5905255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5906255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5907255736Sdavidch    }
5908255736Sdavidch
5909255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5910255736Sdavidch        /* SRC */
5911255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5912255736Sdavidch        ilt_client->client_num = ILT_CLIENT_SRC;
5913255736Sdavidch        ilt_client->page_size = SRC_ILT_PAGE_SZ;
5914255736Sdavidch        ilt_client->flags = 0;
5915255736Sdavidch        ilt_client->start = line;
5916255736Sdavidch        line += SRC_ILT_LINES;
5917255736Sdavidch        ilt_client->end = (line - 1);
5918255736Sdavidch
5919255736Sdavidch        BLOGD(sc, DBG_LOAD,
5920255736Sdavidch              "ilt client[SRC]: start %d, end %d, "
5921255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5922255736Sdavidch              ilt_client->start, ilt_client->end,
5923255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5924255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5925255736Sdavidch
5926255736Sdavidch        /* TM */
5927255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_TM];
5928255736Sdavidch        ilt_client->client_num = ILT_CLIENT_TM;
5929255736Sdavidch        ilt_client->page_size = TM_ILT_PAGE_SZ;
5930255736Sdavidch        ilt_client->flags = 0;
5931255736Sdavidch        ilt_client->start = line;
5932255736Sdavidch        line += TM_ILT_LINES;
5933255736Sdavidch        ilt_client->end = (line - 1);
5934255736Sdavidch
5935255736Sdavidch        BLOGD(sc, DBG_LOAD,
5936255736Sdavidch              "ilt client[TM]: start %d, end %d, "
5937255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5938255736Sdavidch              ilt_client->start, ilt_client->end,
5939255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5940255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5941255736Sdavidch    }
5942255736Sdavidch
5943255736Sdavidch    KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
5944255736Sdavidch}
5945255736Sdavidch
5946255736Sdavidchstatic void
5947255736Sdavidchbxe_set_fp_rx_buf_size(struct bxe_softc *sc)
5948255736Sdavidch{
5949255736Sdavidch    int i;
5950283269Sdavidcs    uint32_t rx_buf_size;
5951255736Sdavidch
5952283269Sdavidcs    rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
5953255736Sdavidch
5954255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5955283269Sdavidcs        if(rx_buf_size <= MCLBYTES){
5956283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5957255736Sdavidch            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5958283269Sdavidcs        }else if (rx_buf_size <= MJUMPAGESIZE){
5959283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5960283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5961283269Sdavidcs        }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
5962283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5963283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5964283269Sdavidcs        }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
5965283269Sdavidcs            sc->fp[i].rx_buf_size = MJUMPAGESIZE;
5966283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5967283269Sdavidcs        }else {
5968283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5969283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5970255736Sdavidch        }
5971255736Sdavidch    }
5972255736Sdavidch}
5973255736Sdavidch
5974255736Sdavidchstatic int
5975255736Sdavidchbxe_alloc_ilt_mem(struct bxe_softc *sc)
5976255736Sdavidch{
5977255736Sdavidch    int rc = 0;
5978255736Sdavidch
5979255736Sdavidch    if ((sc->ilt =
5980255736Sdavidch         (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
5981255736Sdavidch                                    M_BXE_ILT,
5982255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
5983255736Sdavidch        rc = 1;
5984255736Sdavidch    }
5985255736Sdavidch
5986255736Sdavidch    return (rc);
5987255736Sdavidch}
5988255736Sdavidch
5989255736Sdavidchstatic int
5990255736Sdavidchbxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
5991255736Sdavidch{
5992255736Sdavidch    int rc = 0;
5993255736Sdavidch
5994255736Sdavidch    if ((sc->ilt->lines =
5995255736Sdavidch         (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
5996255736Sdavidch                                    M_BXE_ILT,
5997255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
5998255736Sdavidch        rc = 1;
5999255736Sdavidch    }
6000255736Sdavidch
6001255736Sdavidch    return (rc);
6002255736Sdavidch}
6003255736Sdavidch
6004255736Sdavidchstatic void
6005255736Sdavidchbxe_free_ilt_mem(struct bxe_softc *sc)
6006255736Sdavidch{
6007255736Sdavidch    if (sc->ilt != NULL) {
6008255736Sdavidch        free(sc->ilt, M_BXE_ILT);
6009255736Sdavidch        sc->ilt = NULL;
6010255736Sdavidch    }
6011255736Sdavidch}
6012255736Sdavidch
6013255736Sdavidchstatic void
6014255736Sdavidchbxe_free_ilt_lines_mem(struct bxe_softc *sc)
6015255736Sdavidch{
6016255736Sdavidch    if (sc->ilt->lines != NULL) {
6017255736Sdavidch        free(sc->ilt->lines, M_BXE_ILT);
6018255736Sdavidch        sc->ilt->lines = NULL;
6019255736Sdavidch    }
6020255736Sdavidch}
6021255736Sdavidch
6022255736Sdavidchstatic void
6023255736Sdavidchbxe_free_mem(struct bxe_softc *sc)
6024255736Sdavidch{
6025255736Sdavidch    int i;
6026255736Sdavidch
6027255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
6028255736Sdavidch        bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6029255736Sdavidch        sc->context[i].vcxt = NULL;
6030255736Sdavidch        sc->context[i].size = 0;
6031255736Sdavidch    }
6032255736Sdavidch
6033255736Sdavidch    ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6034255736Sdavidch
6035255736Sdavidch    bxe_free_ilt_lines_mem(sc);
6036255736Sdavidch
6037255736Sdavidch}
6038255736Sdavidch
6039255736Sdavidchstatic int
6040255736Sdavidchbxe_alloc_mem(struct bxe_softc *sc)
6041255736Sdavidch{
6042315881Sdavidcs
6043255736Sdavidch    int context_size;
6044255736Sdavidch    int allocated;
6045255736Sdavidch    int i;
6046255736Sdavidch
6047255736Sdavidch    /*
6048255736Sdavidch     * Allocate memory for CDU context:
6049255736Sdavidch     * This memory is allocated separately and not in the generic ILT
6050255736Sdavidch     * functions because CDU differs in few aspects:
6051255736Sdavidch     * 1. There can be multiple entities allocating memory for context -
6052255736Sdavidch     * regular L2, CNIC, and SRIOV drivers. Each separately controls
6053255736Sdavidch     * its own ILT lines.
6054255736Sdavidch     * 2. Since CDU page-size is not a single 4KB page (which is the case
6055255736Sdavidch     * for the other ILT clients), to be efficient we want to support
6056255736Sdavidch     * allocation of sub-page-size in the last entry.
6057255736Sdavidch     * 3. Context pointers are used by the driver to pass to FW / update
6058255736Sdavidch     * the context (for the other ILT clients the pointers are used just to
6059255736Sdavidch     * free the memory during unload).
6060255736Sdavidch     */
6061255736Sdavidch    context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6062255736Sdavidch    for (i = 0, allocated = 0; allocated < context_size; i++) {
6063255736Sdavidch        sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6064255736Sdavidch                                  (context_size - allocated));
6065255736Sdavidch
6066255736Sdavidch        if (bxe_dma_alloc(sc, sc->context[i].size,
6067255736Sdavidch                          &sc->context[i].vcxt_dma,
6068255736Sdavidch                          "cdu context") != 0) {
6069255736Sdavidch            bxe_free_mem(sc);
6070255736Sdavidch            return (-1);
6071255736Sdavidch        }
6072255736Sdavidch
6073255736Sdavidch        sc->context[i].vcxt =
6074255736Sdavidch            (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6075255736Sdavidch
6076255736Sdavidch        allocated += sc->context[i].size;
6077255736Sdavidch    }
6078255736Sdavidch
6079255736Sdavidch    bxe_alloc_ilt_lines_mem(sc);
6080255736Sdavidch
6081255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6082255736Sdavidch          sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6083255736Sdavidch    {
6084255736Sdavidch        for (i = 0; i < 4; i++) {
6085255736Sdavidch            BLOGD(sc, DBG_LOAD,
6086255736Sdavidch                  "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6087255736Sdavidch                  i,
6088255736Sdavidch                  sc->ilt->clients[i].page_size,
6089255736Sdavidch                  sc->ilt->clients[i].start,
6090255736Sdavidch                  sc->ilt->clients[i].end,
6091255736Sdavidch                  sc->ilt->clients[i].client_num,
6092255736Sdavidch                  sc->ilt->clients[i].flags);
6093255736Sdavidch        }
6094255736Sdavidch    }
6095255736Sdavidch    if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6096255736Sdavidch        BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6097255736Sdavidch        bxe_free_mem(sc);
6098255736Sdavidch        return (-1);
6099255736Sdavidch    }
6100255736Sdavidch
6101255736Sdavidch    return (0);
6102255736Sdavidch}
6103255736Sdavidch
6104255736Sdavidchstatic void
6105255736Sdavidchbxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6106255736Sdavidch{
6107255736Sdavidch    struct bxe_softc *sc;
6108255736Sdavidch    int i;
6109255736Sdavidch
6110255736Sdavidch    sc = fp->sc;
6111255736Sdavidch
6112255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6113255736Sdavidch        return;
6114255736Sdavidch    }
6115255736Sdavidch
6116255736Sdavidch    /* free all mbufs and unload all maps */
6117255736Sdavidch    for (i = 0; i < RX_BD_TOTAL; i++) {
6118255736Sdavidch        if (fp->rx_mbuf_chain[i].m_map != NULL) {
6119255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6120255736Sdavidch                            fp->rx_mbuf_chain[i].m_map,
6121255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6122255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6123255736Sdavidch                              fp->rx_mbuf_chain[i].m_map);
6124255736Sdavidch        }
6125255736Sdavidch
6126255736Sdavidch        if (fp->rx_mbuf_chain[i].m != NULL) {
6127255736Sdavidch            m_freem(fp->rx_mbuf_chain[i].m);
6128255736Sdavidch            fp->rx_mbuf_chain[i].m = NULL;
6129255736Sdavidch            fp->eth_q_stats.mbuf_alloc_rx--;
6130255736Sdavidch        }
6131255736Sdavidch    }
6132255736Sdavidch}
6133255736Sdavidch
6134255736Sdavidchstatic void
6135255736Sdavidchbxe_free_tpa_pool(struct bxe_fastpath *fp)
6136255736Sdavidch{
6137255736Sdavidch    struct bxe_softc *sc;
6138255736Sdavidch    int i, max_agg_queues;
6139255736Sdavidch
6140255736Sdavidch    sc = fp->sc;
6141255736Sdavidch
6142255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6143255736Sdavidch        return;
6144255736Sdavidch    }
6145255736Sdavidch
6146255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
6147255736Sdavidch
6148255736Sdavidch    /* release all mbufs and unload all DMA maps in the TPA pool */
6149255736Sdavidch    for (i = 0; i < max_agg_queues; i++) {
6150255736Sdavidch        if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6151255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6152255736Sdavidch                            fp->rx_tpa_info[i].bd.m_map,
6153255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6154255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6155255736Sdavidch                              fp->rx_tpa_info[i].bd.m_map);
6156255736Sdavidch        }
6157255736Sdavidch
6158255736Sdavidch        if (fp->rx_tpa_info[i].bd.m != NULL) {
6159255736Sdavidch            m_freem(fp->rx_tpa_info[i].bd.m);
6160255736Sdavidch            fp->rx_tpa_info[i].bd.m = NULL;
6161255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tpa--;
6162255736Sdavidch        }
6163255736Sdavidch    }
6164255736Sdavidch}
6165255736Sdavidch
6166255736Sdavidchstatic void
6167255736Sdavidchbxe_free_sge_chain(struct bxe_fastpath *fp)
6168255736Sdavidch{
6169255736Sdavidch    struct bxe_softc *sc;
6170255736Sdavidch    int i;
6171255736Sdavidch
6172255736Sdavidch    sc = fp->sc;
6173255736Sdavidch
6174255736Sdavidch    if (fp->rx_sge_mbuf_tag == NULL) {
6175255736Sdavidch        return;
6176255736Sdavidch    }
6177255736Sdavidch
6178255736Sdavidch    /* rree all mbufs and unload all maps */
6179255736Sdavidch    for (i = 0; i < RX_SGE_TOTAL; i++) {
6180255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6181255736Sdavidch            bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6182255736Sdavidch                            fp->rx_sge_mbuf_chain[i].m_map,
6183255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6184255736Sdavidch            bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6185255736Sdavidch                              fp->rx_sge_mbuf_chain[i].m_map);
6186255736Sdavidch        }
6187255736Sdavidch
6188255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6189255736Sdavidch            m_freem(fp->rx_sge_mbuf_chain[i].m);
6190255736Sdavidch            fp->rx_sge_mbuf_chain[i].m = NULL;
6191255736Sdavidch            fp->eth_q_stats.mbuf_alloc_sge--;
6192255736Sdavidch        }
6193255736Sdavidch    }
6194255736Sdavidch}
6195255736Sdavidch
6196255736Sdavidchstatic void
6197255736Sdavidchbxe_free_fp_buffers(struct bxe_softc *sc)
6198255736Sdavidch{
6199255736Sdavidch    struct bxe_fastpath *fp;
6200255736Sdavidch    int i;
6201255736Sdavidch
6202255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6203255736Sdavidch        fp = &sc->fp[i];
6204255736Sdavidch
6205307972Sdavidcs#if __FreeBSD_version >= 901504
6206255736Sdavidch        if (fp->tx_br != NULL) {
6207255736Sdavidch            /* just in case bxe_mq_flush() wasn't called */
6208284739Sdavidcs            if (mtx_initialized(&fp->tx_mtx)) {
6209284739Sdavidcs                struct mbuf *m;
6210284739Sdavidcs
6211284739Sdavidcs                BXE_FP_TX_LOCK(fp);
6212284739Sdavidcs                while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6213284739Sdavidcs                    m_freem(m);
6214284739Sdavidcs                BXE_FP_TX_UNLOCK(fp);
6215255736Sdavidch            }
6216255736Sdavidch        }
6217255736Sdavidch#endif
6218255736Sdavidch
6219255736Sdavidch        /* free all RX buffers */
6220255736Sdavidch        bxe_free_rx_bd_chain(fp);
6221255736Sdavidch        bxe_free_tpa_pool(fp);
6222255736Sdavidch        bxe_free_sge_chain(fp);
6223255736Sdavidch
6224255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6225255736Sdavidch            BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6226255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_rx);
6227255736Sdavidch        }
6228255736Sdavidch
6229255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6230255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6231255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_sge);
6232255736Sdavidch        }
6233255736Sdavidch
6234255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6235255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6236255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tpa);
6237255736Sdavidch        }
6238255736Sdavidch
6239255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6240255736Sdavidch            BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6241255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tx);
6242255736Sdavidch        }
6243255736Sdavidch
6244255736Sdavidch        /* XXX verify all mbufs were reclaimed */
6245255736Sdavidch    }
6246255736Sdavidch}
6247255736Sdavidch
6248255736Sdavidchstatic int
6249255736Sdavidchbxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6250255736Sdavidch                     uint16_t            prev_index,
6251255736Sdavidch                     uint16_t            index)
6252255736Sdavidch{
6253255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
6254255736Sdavidch    struct eth_rx_bd *rx_bd;
6255255736Sdavidch    bus_dma_segment_t segs[1];
6256255736Sdavidch    bus_dmamap_t map;
6257255736Sdavidch    struct mbuf *m;
6258255736Sdavidch    int nsegs, rc;
6259255736Sdavidch
6260255736Sdavidch    rc = 0;
6261255736Sdavidch
6262255736Sdavidch    /* allocate the new RX BD mbuf */
6263260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6264255736Sdavidch    if (__predict_false(m == NULL)) {
6265255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6266255736Sdavidch        return (ENOBUFS);
6267255736Sdavidch    }
6268255736Sdavidch
6269255736Sdavidch    fp->eth_q_stats.mbuf_alloc_rx++;
6270255736Sdavidch
6271255736Sdavidch    /* initialize the mbuf buffer length */
6272255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6273255736Sdavidch
6274255736Sdavidch    /* map the mbuf into non-paged pool */
6275255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6276255736Sdavidch                                 fp->rx_mbuf_spare_map,
6277255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6278255736Sdavidch    if (__predict_false(rc != 0)) {
6279255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6280255736Sdavidch        m_freem(m);
6281255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
6282255736Sdavidch        return (rc);
6283255736Sdavidch    }
6284255736Sdavidch
6285255736Sdavidch    /* all mbufs must map to a single segment */
6286255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6287255736Sdavidch
6288255736Sdavidch    /* release any existing RX BD mbuf mappings */
6289255736Sdavidch
6290255736Sdavidch    if (prev_index != index) {
6291255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[prev_index];
6292255736Sdavidch
6293255736Sdavidch        if (rx_buf->m_map != NULL) {
6294255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6295255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6296255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6297255736Sdavidch        }
6298255736Sdavidch
6299255736Sdavidch        /*
6300255736Sdavidch         * We only get here from bxe_rxeof() when the maximum number
6301255736Sdavidch         * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6302255736Sdavidch         * holds the mbuf in the prev_index so it's OK to NULL it out
6303255736Sdavidch         * here without concern of a memory leak.
6304255736Sdavidch         */
6305255736Sdavidch        fp->rx_mbuf_chain[prev_index].m = NULL;
6306255736Sdavidch    }
6307255736Sdavidch
6308255736Sdavidch    rx_buf = &fp->rx_mbuf_chain[index];
6309255736Sdavidch
6310255736Sdavidch    if (rx_buf->m_map != NULL) {
6311255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6312255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6313255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6314255736Sdavidch    }
6315255736Sdavidch
6316255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6317255736Sdavidch    map = (prev_index != index) ?
6318255736Sdavidch              fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6319255736Sdavidch    rx_buf->m_map = fp->rx_mbuf_spare_map;
6320255736Sdavidch    fp->rx_mbuf_spare_map = map;
6321255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6322255736Sdavidch                    BUS_DMASYNC_PREREAD);
6323255736Sdavidch    rx_buf->m = m;
6324255736Sdavidch
6325255736Sdavidch    rx_bd = &fp->rx_chain[index];
6326255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6327255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6328255736Sdavidch
6329255736Sdavidch    return (rc);
6330255736Sdavidch}
6331255736Sdavidch
6332255736Sdavidchstatic int
6333255736Sdavidchbxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6334255736Sdavidch                      int                 queue)
6335255736Sdavidch{
6336255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6337255736Sdavidch    bus_dma_segment_t segs[1];
6338255736Sdavidch    bus_dmamap_t map;
6339255736Sdavidch    struct mbuf *m;
6340255736Sdavidch    int nsegs;
6341255736Sdavidch    int rc = 0;
6342255736Sdavidch
6343255736Sdavidch    /* allocate the new TPA mbuf */
6344260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6345255736Sdavidch    if (__predict_false(m == NULL)) {
6346255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6347255736Sdavidch        return (ENOBUFS);
6348255736Sdavidch    }
6349255736Sdavidch
6350255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa++;
6351255736Sdavidch
6352255736Sdavidch    /* initialize the mbuf buffer length */
6353255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6354255736Sdavidch
6355255736Sdavidch    /* map the mbuf into non-paged pool */
6356255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6357255736Sdavidch                                 fp->rx_tpa_info_mbuf_spare_map,
6358255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6359255736Sdavidch    if (__predict_false(rc != 0)) {
6360255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6361255736Sdavidch        m_free(m);
6362255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tpa--;
6363255736Sdavidch        return (rc);
6364255736Sdavidch    }
6365255736Sdavidch
6366255736Sdavidch    /* all mbufs must map to a single segment */
6367255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6368255736Sdavidch
6369255736Sdavidch    /* release any existing TPA mbuf mapping */
6370255736Sdavidch    if (tpa_info->bd.m_map != NULL) {
6371255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6372255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6373255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6374255736Sdavidch    }
6375255736Sdavidch
6376255736Sdavidch    /* save the mbuf and mapping info for the TPA mbuf */
6377255736Sdavidch    map = tpa_info->bd.m_map;
6378255736Sdavidch    tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6379255736Sdavidch    fp->rx_tpa_info_mbuf_spare_map = map;
6380255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6381255736Sdavidch                    BUS_DMASYNC_PREREAD);
6382255736Sdavidch    tpa_info->bd.m = m;
6383255736Sdavidch    tpa_info->seg = segs[0];
6384255736Sdavidch
6385255736Sdavidch    return (rc);
6386255736Sdavidch}
6387255736Sdavidch
6388255736Sdavidch/*
6389255736Sdavidch * Allocate an mbuf and assign it to the receive scatter gather chain. The
6390255736Sdavidch * caller must take care to save a copy of the existing mbuf in the SG mbuf
6391255736Sdavidch * chain.
6392255736Sdavidch */
6393255736Sdavidchstatic int
6394255736Sdavidchbxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6395255736Sdavidch                      uint16_t            index)
6396255736Sdavidch{
6397255736Sdavidch    struct bxe_sw_rx_bd *sge_buf;
6398255736Sdavidch    struct eth_rx_sge *sge;
6399255736Sdavidch    bus_dma_segment_t segs[1];
6400255736Sdavidch    bus_dmamap_t map;
6401255736Sdavidch    struct mbuf *m;
6402255736Sdavidch    int nsegs;
6403255736Sdavidch    int rc = 0;
6404255736Sdavidch
6405255736Sdavidch    /* allocate a new SGE mbuf */
6406260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6407255736Sdavidch    if (__predict_false(m == NULL)) {
6408255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6409255736Sdavidch        return (ENOMEM);
6410255736Sdavidch    }
6411255736Sdavidch
6412255736Sdavidch    fp->eth_q_stats.mbuf_alloc_sge++;
6413255736Sdavidch
6414255736Sdavidch    /* initialize the mbuf buffer length */
6415255736Sdavidch    m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6416255736Sdavidch
6417255736Sdavidch    /* map the SGE mbuf into non-paged pool */
6418255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6419255736Sdavidch                                 fp->rx_sge_mbuf_spare_map,
6420255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6421255736Sdavidch    if (__predict_false(rc != 0)) {
6422255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6423255736Sdavidch        m_freem(m);
6424255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
6425255736Sdavidch        return (rc);
6426255736Sdavidch    }
6427255736Sdavidch
6428255736Sdavidch    /* all mbufs must map to a single segment */
6429255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6430255736Sdavidch
6431255736Sdavidch    sge_buf = &fp->rx_sge_mbuf_chain[index];
6432255736Sdavidch
6433255736Sdavidch    /* release any existing SGE mbuf mapping */
6434255736Sdavidch    if (sge_buf->m_map != NULL) {
6435255736Sdavidch        bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6436255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6437255736Sdavidch        bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6438255736Sdavidch    }
6439255736Sdavidch
6440255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6441255736Sdavidch    map = sge_buf->m_map;
6442255736Sdavidch    sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6443255736Sdavidch    fp->rx_sge_mbuf_spare_map = map;
6444255736Sdavidch    bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6445255736Sdavidch                    BUS_DMASYNC_PREREAD);
6446255736Sdavidch    sge_buf->m = m;
6447255736Sdavidch
6448255736Sdavidch    sge = &fp->rx_sge_chain[index];
6449255736Sdavidch    sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6450255736Sdavidch    sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6451255736Sdavidch
6452255736Sdavidch    return (rc);
6453255736Sdavidch}
6454255736Sdavidch
6455255736Sdavidchstatic __noinline int
6456255736Sdavidchbxe_alloc_fp_buffers(struct bxe_softc *sc)
6457255736Sdavidch{
6458255736Sdavidch    struct bxe_fastpath *fp;
6459255736Sdavidch    int i, j, rc = 0;
6460255736Sdavidch    int ring_prod, cqe_ring_prod;
6461255736Sdavidch    int max_agg_queues;
6462255736Sdavidch
6463255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6464255736Sdavidch        fp = &sc->fp[i];
6465255736Sdavidch
6466255736Sdavidch        ring_prod = cqe_ring_prod = 0;
6467255736Sdavidch        fp->rx_bd_cons = 0;
6468255736Sdavidch        fp->rx_cq_cons = 0;
6469255736Sdavidch
6470255736Sdavidch        /* allocate buffers for the RX BDs in RX BD chain */
6471255736Sdavidch        for (j = 0; j < sc->max_rx_bufs; j++) {
6472255736Sdavidch            rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6473255736Sdavidch            if (rc != 0) {
6474255736Sdavidch                BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6475255736Sdavidch                      i, rc);
6476255736Sdavidch                goto bxe_alloc_fp_buffers_error;
6477255736Sdavidch            }
6478255736Sdavidch
6479255736Sdavidch            ring_prod     = RX_BD_NEXT(ring_prod);
6480255736Sdavidch            cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6481255736Sdavidch        }
6482255736Sdavidch
6483255736Sdavidch        fp->rx_bd_prod = ring_prod;
6484255736Sdavidch        fp->rx_cq_prod = cqe_ring_prod;
6485255736Sdavidch        fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6486255736Sdavidch
6487283274Sdavidcs        max_agg_queues = MAX_AGG_QS(sc);
6488255736Sdavidch
6489283274Sdavidcs        fp->tpa_enable = TRUE;
6490255736Sdavidch
6491283274Sdavidcs        /* fill the TPA pool */
6492283274Sdavidcs        for (j = 0; j < max_agg_queues; j++) {
6493283274Sdavidcs            rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6494283274Sdavidcs            if (rc != 0) {
6495283274Sdavidcs                BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6496283274Sdavidcs                          i, j);
6497283274Sdavidcs                fp->tpa_enable = FALSE;
6498283274Sdavidcs                goto bxe_alloc_fp_buffers_error;
6499283274Sdavidcs            }
6500283274Sdavidcs
6501283274Sdavidcs            fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6502283274Sdavidcs        }
6503283274Sdavidcs
6504283274Sdavidcs        if (fp->tpa_enable) {
6505283274Sdavidcs            /* fill the RX SGE chain */
6506283274Sdavidcs            ring_prod = 0;
6507283274Sdavidcs            for (j = 0; j < RX_SGE_USABLE; j++) {
6508283274Sdavidcs                rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6509255736Sdavidch                if (rc != 0) {
6510283274Sdavidcs                    BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6511283274Sdavidcs                              i, ring_prod);
6512255736Sdavidch                    fp->tpa_enable = FALSE;
6513283274Sdavidcs                    ring_prod = 0;
6514255736Sdavidch                    goto bxe_alloc_fp_buffers_error;
6515255736Sdavidch                }
6516255736Sdavidch
6517283274Sdavidcs                ring_prod = RX_SGE_NEXT(ring_prod);
6518255736Sdavidch            }
6519255736Sdavidch
6520283274Sdavidcs            fp->rx_sge_prod = ring_prod;
6521255736Sdavidch        }
6522255736Sdavidch    }
6523255736Sdavidch
6524255736Sdavidch    return (0);
6525255736Sdavidch
6526255736Sdavidchbxe_alloc_fp_buffers_error:
6527255736Sdavidch
6528255736Sdavidch    /* unwind what was already allocated */
6529255736Sdavidch    bxe_free_rx_bd_chain(fp);
6530255736Sdavidch    bxe_free_tpa_pool(fp);
6531255736Sdavidch    bxe_free_sge_chain(fp);
6532255736Sdavidch
6533255736Sdavidch    return (ENOBUFS);
6534255736Sdavidch}
6535255736Sdavidch
6536255736Sdavidchstatic void
6537255736Sdavidchbxe_free_fw_stats_mem(struct bxe_softc *sc)
6538255736Sdavidch{
6539255736Sdavidch    bxe_dma_free(sc, &sc->fw_stats_dma);
6540255736Sdavidch
6541255736Sdavidch    sc->fw_stats_num = 0;
6542255736Sdavidch
6543255736Sdavidch    sc->fw_stats_req_size = 0;
6544255736Sdavidch    sc->fw_stats_req = NULL;
6545255736Sdavidch    sc->fw_stats_req_mapping = 0;
6546255736Sdavidch
6547255736Sdavidch    sc->fw_stats_data_size = 0;
6548255736Sdavidch    sc->fw_stats_data = NULL;
6549255736Sdavidch    sc->fw_stats_data_mapping = 0;
6550255736Sdavidch}
6551255736Sdavidch
6552255736Sdavidchstatic int
6553255736Sdavidchbxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6554255736Sdavidch{
6555255736Sdavidch    uint8_t num_queue_stats;
6556255736Sdavidch    int num_groups;
6557255736Sdavidch
6558255736Sdavidch    /* number of queues for statistics is number of eth queues */
6559255736Sdavidch    num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6560255736Sdavidch
6561255736Sdavidch    /*
6562255736Sdavidch     * Total number of FW statistics requests =
6563255736Sdavidch     *   1 for port stats + 1 for PF stats + num of queues
6564255736Sdavidch     */
6565255736Sdavidch    sc->fw_stats_num = (2 + num_queue_stats);
6566255736Sdavidch
6567255736Sdavidch    /*
6568255736Sdavidch     * Request is built from stats_query_header and an array of
6569255736Sdavidch     * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6570255736Sdavidch     * rules. The real number or requests is configured in the
6571255736Sdavidch     * stats_query_header.
6572255736Sdavidch     */
6573255736Sdavidch    num_groups =
6574255736Sdavidch        ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6575255736Sdavidch         ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6576255736Sdavidch
6577255736Sdavidch    BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6578255736Sdavidch          sc->fw_stats_num, num_groups);
6579255736Sdavidch
6580255736Sdavidch    sc->fw_stats_req_size =
6581255736Sdavidch        (sizeof(struct stats_query_header) +
6582255736Sdavidch         (num_groups * sizeof(struct stats_query_cmd_group)));
6583255736Sdavidch
6584255736Sdavidch    /*
6585255736Sdavidch     * Data for statistics requests + stats_counter.
6586255736Sdavidch     * stats_counter holds per-STORM counters that are incremented when
6587255736Sdavidch     * STORM has finished with the current request. Memory for FCoE
6588255736Sdavidch     * offloaded statistics are counted anyway, even if they will not be sent.
6589255736Sdavidch     * VF stats are not accounted for here as the data of VF stats is stored
6590255736Sdavidch     * in memory allocated by the VF, not here.
6591255736Sdavidch     */
6592255736Sdavidch    sc->fw_stats_data_size =
6593255736Sdavidch        (sizeof(struct stats_counter) +
6594255736Sdavidch         sizeof(struct per_port_stats) +
6595255736Sdavidch         sizeof(struct per_pf_stats) +
6596255736Sdavidch         /* sizeof(struct fcoe_statistics_params) + */
6597255736Sdavidch         (sizeof(struct per_queue_stats) * num_queue_stats));
6598255736Sdavidch
6599255736Sdavidch    if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6600255736Sdavidch                      &sc->fw_stats_dma, "fw stats") != 0) {
6601255736Sdavidch        bxe_free_fw_stats_mem(sc);
6602255736Sdavidch        return (-1);
6603255736Sdavidch    }
6604255736Sdavidch
6605255736Sdavidch    /* set up the shortcuts */
6606255736Sdavidch
6607255736Sdavidch    sc->fw_stats_req =
6608255736Sdavidch        (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6609255736Sdavidch    sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6610255736Sdavidch
6611255736Sdavidch    sc->fw_stats_data =
6612255736Sdavidch        (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6613255736Sdavidch                                     sc->fw_stats_req_size);
6614255736Sdavidch    sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6615255736Sdavidch                                 sc->fw_stats_req_size);
6616255736Sdavidch
6617256341Sdim    BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6618256341Sdim          (uintmax_t)sc->fw_stats_req_mapping);
6619255736Sdavidch
6620256341Sdim    BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6621256341Sdim          (uintmax_t)sc->fw_stats_data_mapping);
6622255736Sdavidch
6623255736Sdavidch    return (0);
6624255736Sdavidch}
6625255736Sdavidch
6626255736Sdavidch/*
6627255736Sdavidch * Bits map:
6628255736Sdavidch * 0-7  - Engine0 load counter.
6629255736Sdavidch * 8-15 - Engine1 load counter.
6630255736Sdavidch * 16   - Engine0 RESET_IN_PROGRESS bit.
6631255736Sdavidch * 17   - Engine1 RESET_IN_PROGRESS bit.
6632255736Sdavidch * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6633255736Sdavidch *        function on the engine
6634255736Sdavidch * 19   - Engine1 ONE_IS_LOADED.
6635255736Sdavidch * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6636255736Sdavidch *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6637255736Sdavidch *        for just the one belonging to its engine).
6638255736Sdavidch */
6639255736Sdavidch#define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6640255736Sdavidch#define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6641255736Sdavidch#define BXE_PATH0_LOAD_CNT_SHIFT  0
6642255736Sdavidch#define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6643255736Sdavidch#define BXE_PATH1_LOAD_CNT_SHIFT  8
6644255736Sdavidch#define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6645255736Sdavidch#define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6646255736Sdavidch#define BXE_GLOBAL_RESET_BIT      0x00040000
6647255736Sdavidch
6648255736Sdavidch/* set the GLOBAL_RESET bit, should be run under rtnl lock */
6649255736Sdavidchstatic void
6650255736Sdavidchbxe_set_reset_global(struct bxe_softc *sc)
6651255736Sdavidch{
6652255736Sdavidch    uint32_t val;
6653255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6654255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6655255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6656255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6657255736Sdavidch}
6658255736Sdavidch
6659255736Sdavidch/* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6660255736Sdavidchstatic void
6661255736Sdavidchbxe_clear_reset_global(struct bxe_softc *sc)
6662255736Sdavidch{
6663255736Sdavidch    uint32_t val;
6664255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6665255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6666255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6667255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6668255736Sdavidch}
6669255736Sdavidch
6670255736Sdavidch/* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6671255736Sdavidchstatic uint8_t
6672255736Sdavidchbxe_reset_is_global(struct bxe_softc *sc)
6673255736Sdavidch{
6674255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6675255736Sdavidch    BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6676255736Sdavidch    return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6677255736Sdavidch}
6678255736Sdavidch
6679255736Sdavidch/* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6680255736Sdavidchstatic void
6681255736Sdavidchbxe_set_reset_done(struct bxe_softc *sc)
6682255736Sdavidch{
6683255736Sdavidch    uint32_t val;
6684255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6685255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6686255736Sdavidch
6687255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6688255736Sdavidch
6689255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6690255736Sdavidch    /* Clear the bit */
6691255736Sdavidch    val &= ~bit;
6692255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6693255736Sdavidch
6694255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6695255736Sdavidch}
6696255736Sdavidch
6697255736Sdavidch/* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6698255736Sdavidchstatic void
6699255736Sdavidchbxe_set_reset_in_progress(struct bxe_softc *sc)
6700255736Sdavidch{
6701255736Sdavidch    uint32_t val;
6702255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6703255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6704255736Sdavidch
6705255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6706255736Sdavidch
6707255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6708255736Sdavidch    /* Set the bit */
6709255736Sdavidch    val |= bit;
6710255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6711255736Sdavidch
6712255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6713255736Sdavidch}
6714255736Sdavidch
6715255736Sdavidch/* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6716255736Sdavidchstatic uint8_t
6717255736Sdavidchbxe_reset_is_done(struct bxe_softc *sc,
6718255736Sdavidch                  int              engine)
6719255736Sdavidch{
6720255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6721255736Sdavidch    uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6722255736Sdavidch                            BXE_PATH0_RST_IN_PROG_BIT;
6723255736Sdavidch
6724255736Sdavidch    /* return false if bit is set */
6725255736Sdavidch    return (val & bit) ? FALSE : TRUE;
6726255736Sdavidch}
6727255736Sdavidch
6728255736Sdavidch/* get the load status for an engine, should be run under rtnl lock */
6729255736Sdavidchstatic uint8_t
6730255736Sdavidchbxe_get_load_status(struct bxe_softc *sc,
6731255736Sdavidch                    int              engine)
6732255736Sdavidch{
6733255736Sdavidch    uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6734255736Sdavidch                             BXE_PATH0_LOAD_CNT_MASK;
6735255736Sdavidch    uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6736255736Sdavidch                              BXE_PATH0_LOAD_CNT_SHIFT;
6737255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6738255736Sdavidch
6739255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6740255736Sdavidch
6741255736Sdavidch    val = ((val & mask) >> shift);
6742255736Sdavidch
6743255736Sdavidch    BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6744255736Sdavidch
6745255736Sdavidch    return (val != 0);
6746255736Sdavidch}
6747255736Sdavidch
6748255736Sdavidch/* set pf load mark */
6749255736Sdavidch/* XXX needs to be under rtnl lock */
6750255736Sdavidchstatic void
6751255736Sdavidchbxe_set_pf_load(struct bxe_softc *sc)
6752255736Sdavidch{
6753255736Sdavidch    uint32_t val;
6754255736Sdavidch    uint32_t val1;
6755255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6756255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6757255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6758255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6759255736Sdavidch
6760255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6761255736Sdavidch
6762255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6763255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6764255736Sdavidch
6765255736Sdavidch    /* get the current counter value */
6766255736Sdavidch    val1 = ((val & mask) >> shift);
6767255736Sdavidch
6768255736Sdavidch    /* set bit of this PF */
6769255736Sdavidch    val1 |= (1 << SC_ABS_FUNC(sc));
6770255736Sdavidch
6771255736Sdavidch    /* clear the old value */
6772255736Sdavidch    val &= ~mask;
6773255736Sdavidch
6774255736Sdavidch    /* set the new one */
6775255736Sdavidch    val |= ((val1 << shift) & mask);
6776255736Sdavidch
6777255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6778255736Sdavidch
6779255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6780255736Sdavidch}
6781255736Sdavidch
6782255736Sdavidch/* clear pf load mark */
6783255736Sdavidch/* XXX needs to be under rtnl lock */
6784255736Sdavidchstatic uint8_t
6785255736Sdavidchbxe_clear_pf_load(struct bxe_softc *sc)
6786255736Sdavidch{
6787255736Sdavidch    uint32_t val1, val;
6788255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6789255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6790255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6791255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6792255736Sdavidch
6793255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6794255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6795255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6796255736Sdavidch
6797255736Sdavidch    /* get the current counter value */
6798255736Sdavidch    val1 = (val & mask) >> shift;
6799255736Sdavidch
6800255736Sdavidch    /* clear bit of that PF */
6801255736Sdavidch    val1 &= ~(1 << SC_ABS_FUNC(sc));
6802255736Sdavidch
6803255736Sdavidch    /* clear the old value */
6804255736Sdavidch    val &= ~mask;
6805255736Sdavidch
6806255736Sdavidch    /* set the new one */
6807255736Sdavidch    val |= ((val1 << shift) & mask);
6808255736Sdavidch
6809255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6810255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6811255736Sdavidch    return (val1 != 0);
6812255736Sdavidch}
6813255736Sdavidch
6814255736Sdavidch/* send load requrest to mcp and analyze response */
6815255736Sdavidchstatic int
6816255736Sdavidchbxe_nic_load_request(struct bxe_softc *sc,
6817255736Sdavidch                     uint32_t         *load_code)
6818255736Sdavidch{
6819255736Sdavidch    /* init fw_seq */
6820255736Sdavidch    sc->fw_seq =
6821255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6822255736Sdavidch         DRV_MSG_SEQ_NUMBER_MASK);
6823255736Sdavidch
6824255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6825255736Sdavidch
6826255736Sdavidch    /* get the current FW pulse sequence */
6827255736Sdavidch    sc->fw_drv_pulse_wr_seq =
6828255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6829255736Sdavidch         DRV_PULSE_SEQ_MASK);
6830255736Sdavidch
6831255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6832255736Sdavidch          sc->fw_drv_pulse_wr_seq);
6833255736Sdavidch
6834255736Sdavidch    /* load request */
6835255736Sdavidch    (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6836255736Sdavidch                                  DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6837255736Sdavidch
6838255736Sdavidch    /* if the MCP fails to respond we must abort */
6839255736Sdavidch    if (!(*load_code)) {
6840255736Sdavidch        BLOGE(sc, "MCP response failure!\n");
6841255736Sdavidch        return (-1);
6842255736Sdavidch    }
6843255736Sdavidch
6844255736Sdavidch    /* if MCP refused then must abort */
6845255736Sdavidch    if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6846255736Sdavidch        BLOGE(sc, "MCP refused load request\n");
6847255736Sdavidch        return (-1);
6848255736Sdavidch    }
6849255736Sdavidch
6850255736Sdavidch    return (0);
6851255736Sdavidch}
6852255736Sdavidch
6853255736Sdavidch/*
6854255736Sdavidch * Check whether another PF has already loaded FW to chip. In virtualized
6855255736Sdavidch * environments a pf from anoth VM may have already initialized the device
6856255736Sdavidch * including loading FW.
6857255736Sdavidch */
6858255736Sdavidchstatic int
6859255736Sdavidchbxe_nic_load_analyze_req(struct bxe_softc *sc,
6860255736Sdavidch                         uint32_t         load_code)
6861255736Sdavidch{
6862255736Sdavidch    uint32_t my_fw, loaded_fw;
6863255736Sdavidch
6864255736Sdavidch    /* is another pf loaded on this engine? */
6865255736Sdavidch    if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6866255736Sdavidch        (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6867255736Sdavidch        /* build my FW version dword */
6868255736Sdavidch        my_fw = (BCM_5710_FW_MAJOR_VERSION +
6869255736Sdavidch                 (BCM_5710_FW_MINOR_VERSION << 8 ) +
6870255736Sdavidch                 (BCM_5710_FW_REVISION_VERSION << 16) +
6871255736Sdavidch                 (BCM_5710_FW_ENGINEERING_VERSION << 24));
6872255736Sdavidch
6873255736Sdavidch        /* read loaded FW from chip */
6874255736Sdavidch        loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6875255736Sdavidch        BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6876255736Sdavidch              loaded_fw, my_fw);
6877255736Sdavidch
6878255736Sdavidch        /* abort nic load if version mismatch */
6879255736Sdavidch        if (my_fw != loaded_fw) {
6880255736Sdavidch            BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6881255736Sdavidch                  loaded_fw, my_fw);
6882255736Sdavidch            return (-1);
6883255736Sdavidch        }
6884255736Sdavidch    }
6885255736Sdavidch
6886255736Sdavidch    return (0);
6887255736Sdavidch}
6888255736Sdavidch
6889255736Sdavidch/* mark PMF if applicable */
6890255736Sdavidchstatic void
6891255736Sdavidchbxe_nic_load_pmf(struct bxe_softc *sc,
6892255736Sdavidch                 uint32_t         load_code)
6893255736Sdavidch{
6894255736Sdavidch    uint32_t ncsi_oem_data_addr;
6895255736Sdavidch
6896255736Sdavidch    if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6897255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6898255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6899255736Sdavidch        /*
6900255736Sdavidch         * Barrier here for ordering between the writing to sc->port.pmf here
6901255736Sdavidch         * and reading it from the periodic task.
6902255736Sdavidch         */
6903255736Sdavidch        sc->port.pmf = 1;
6904255736Sdavidch        mb();
6905255736Sdavidch    } else {
6906255736Sdavidch        sc->port.pmf = 0;
6907255736Sdavidch    }
6908255736Sdavidch
6909255736Sdavidch    BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6910255736Sdavidch
6911255736Sdavidch    /* XXX needed? */
6912255736Sdavidch    if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6913255736Sdavidch        if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6914255736Sdavidch            ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6915255736Sdavidch            if (ncsi_oem_data_addr) {
6916255736Sdavidch                REG_WR(sc,
6917255736Sdavidch                       (ncsi_oem_data_addr +
6918255736Sdavidch                        offsetof(struct glob_ncsi_oem_data, driver_version)),
6919255736Sdavidch                       0);
6920255736Sdavidch            }
6921255736Sdavidch        }
6922255736Sdavidch    }
6923255736Sdavidch}
6924255736Sdavidch
6925255736Sdavidchstatic void
6926255736Sdavidchbxe_read_mf_cfg(struct bxe_softc *sc)
6927255736Sdavidch{
6928255736Sdavidch    int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
6929255736Sdavidch    int abs_func;
6930255736Sdavidch    int vn;
6931255736Sdavidch
6932255736Sdavidch    if (BXE_NOMCP(sc)) {
6933255736Sdavidch        return; /* what should be the default bvalue in this case */
6934255736Sdavidch    }
6935255736Sdavidch
6936255736Sdavidch    /*
6937255736Sdavidch     * The formula for computing the absolute function number is...
6938255736Sdavidch     * For 2 port configuration (4 functions per port):
6939255736Sdavidch     *   abs_func = 2 * vn + SC_PORT + SC_PATH
6940255736Sdavidch     * For 4 port configuration (2 functions per port):
6941255736Sdavidch     *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
6942255736Sdavidch     */
6943255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
6944255736Sdavidch        abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
6945255736Sdavidch        if (abs_func >= E1H_FUNC_MAX) {
6946255736Sdavidch            break;
6947255736Sdavidch        }
6948255736Sdavidch        sc->devinfo.mf_info.mf_config[vn] =
6949255736Sdavidch            MFCFG_RD(sc, func_mf_config[abs_func].config);
6950255736Sdavidch    }
6951255736Sdavidch
6952255736Sdavidch    if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
6953255736Sdavidch        FUNC_MF_CFG_FUNC_DISABLED) {
6954255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
6955255736Sdavidch        sc->flags |= BXE_MF_FUNC_DIS;
6956255736Sdavidch    } else {
6957255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
6958255736Sdavidch        sc->flags &= ~BXE_MF_FUNC_DIS;
6959255736Sdavidch    }
6960255736Sdavidch}
6961255736Sdavidch
6962255736Sdavidch/* acquire split MCP access lock register */
6963255736Sdavidchstatic int bxe_acquire_alr(struct bxe_softc *sc)
6964255736Sdavidch{
6965255736Sdavidch    uint32_t j, val;
6966255736Sdavidch
6967255736Sdavidch    for (j = 0; j < 1000; j++) {
6968255736Sdavidch        val = (1UL << 31);
6969255736Sdavidch        REG_WR(sc, GRCBASE_MCP + 0x9c, val);
6970255736Sdavidch        val = REG_RD(sc, GRCBASE_MCP + 0x9c);
6971255736Sdavidch        if (val & (1L << 31))
6972255736Sdavidch            break;
6973255736Sdavidch
6974255736Sdavidch        DELAY(5000);
6975255736Sdavidch    }
6976255736Sdavidch
6977255736Sdavidch    if (!(val & (1L << 31))) {
6978255736Sdavidch        BLOGE(sc, "Cannot acquire MCP access lock register\n");
6979255736Sdavidch        return (-1);
6980255736Sdavidch    }
6981255736Sdavidch
6982255736Sdavidch    return (0);
6983255736Sdavidch}
6984255736Sdavidch
6985255736Sdavidch/* release split MCP access lock register */
6986255736Sdavidchstatic void bxe_release_alr(struct bxe_softc *sc)
6987255736Sdavidch{
6988255736Sdavidch    REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
6989255736Sdavidch}
6990255736Sdavidch
6991255736Sdavidchstatic void
6992255736Sdavidchbxe_fan_failure(struct bxe_softc *sc)
6993255736Sdavidch{
6994255736Sdavidch    int port = SC_PORT(sc);
6995255736Sdavidch    uint32_t ext_phy_config;
6996255736Sdavidch
6997255736Sdavidch    /* mark the failure */
6998255736Sdavidch    ext_phy_config =
6999255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7000255736Sdavidch
7001255736Sdavidch    ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7002255736Sdavidch    ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7003255736Sdavidch    SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7004255736Sdavidch             ext_phy_config);
7005255736Sdavidch
7006255736Sdavidch    /* log the failure */
7007255736Sdavidch    BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7008255736Sdavidch              "the card to prevent permanent damage. "
7009255736Sdavidch              "Please contact OEM Support for assistance\n");
7010255736Sdavidch
7011255736Sdavidch    /* XXX */
7012255736Sdavidch#if 1
7013255736Sdavidch    bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7014255736Sdavidch#else
7015255736Sdavidch    /*
7016255736Sdavidch     * Schedule device reset (unload)
7017255736Sdavidch     * This is due to some boards consuming sufficient power when driver is
7018255736Sdavidch     * up to overheat if fan fails.
7019255736Sdavidch     */
7020255736Sdavidch    bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7021255736Sdavidch    schedule_delayed_work(&sc->sp_rtnl_task, 0);
7022255736Sdavidch#endif
7023255736Sdavidch}
7024255736Sdavidch
7025255736Sdavidch/* this function is called upon a link interrupt */
7026255736Sdavidchstatic void
7027255736Sdavidchbxe_link_attn(struct bxe_softc *sc)
7028255736Sdavidch{
7029255736Sdavidch    uint32_t pause_enabled = 0;
7030255736Sdavidch    struct host_port_stats *pstats;
7031255736Sdavidch    int cmng_fns;
7032307972Sdavidcs    struct bxe_fastpath *fp;
7033307972Sdavidcs    int i;
7034255736Sdavidch
7035255736Sdavidch    /* Make sure that we are synced with the current statistics */
7036255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_STOP);
7037315881Sdavidcs	BLOGI(sc, "link_vars phy_flags : %x\n", sc->link_vars.phy_flags);
7038255736Sdavidch    elink_link_update(&sc->link_params, &sc->link_vars);
7039255736Sdavidch
7040255736Sdavidch    if (sc->link_vars.link_up) {
7041255736Sdavidch
7042255736Sdavidch        /* dropless flow control */
7043255736Sdavidch        if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7044255736Sdavidch            pause_enabled = 0;
7045255736Sdavidch
7046255736Sdavidch            if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7047255736Sdavidch                pause_enabled = 1;
7048255736Sdavidch            }
7049255736Sdavidch
7050255736Sdavidch            REG_WR(sc,
7051255736Sdavidch                   (BAR_USTRORM_INTMEM +
7052255736Sdavidch                    USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7053255736Sdavidch                   pause_enabled);
7054255736Sdavidch        }
7055255736Sdavidch
7056255736Sdavidch        if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7057255736Sdavidch            pstats = BXE_SP(sc, port_stats);
7058255736Sdavidch            /* reset old mac stats */
7059255736Sdavidch            memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7060255736Sdavidch        }
7061255736Sdavidch
7062255736Sdavidch        if (sc->state == BXE_STATE_OPEN) {
7063255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7064255736Sdavidch        }
7065307972Sdavidcs
7066307972Sdavidcs	/* Restart tx when the link comes back. */
7067307972Sdavidcs        FOR_EACH_ETH_QUEUE(sc, i) {
7068307972Sdavidcs            fp = &sc->fp[i];
7069307972Sdavidcs            taskqueue_enqueue(fp->tq, &fp->tx_task);
7070307972Sdavidcs	}
7071255736Sdavidch    }
7072255736Sdavidch
7073255736Sdavidch    if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7074255736Sdavidch        cmng_fns = bxe_get_cmng_fns_mode(sc);
7075255736Sdavidch
7076255736Sdavidch        if (cmng_fns != CMNG_FNS_NONE) {
7077255736Sdavidch            bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7078255736Sdavidch            storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7079255736Sdavidch        } else {
7080255736Sdavidch            /* rate shaping and fairness are disabled */
7081255736Sdavidch            BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7082255736Sdavidch        }
7083255736Sdavidch    }
7084255736Sdavidch
7085255736Sdavidch    bxe_link_report_locked(sc);
7086255736Sdavidch
7087255736Sdavidch    if (IS_MF(sc)) {
7088255736Sdavidch        ; // XXX bxe_link_sync_notify(sc);
7089255736Sdavidch    }
7090255736Sdavidch}
7091255736Sdavidch
7092255736Sdavidchstatic void
7093255736Sdavidchbxe_attn_int_asserted(struct bxe_softc *sc,
7094255736Sdavidch                      uint32_t         asserted)
7095255736Sdavidch{
7096255736Sdavidch    int port = SC_PORT(sc);
7097255736Sdavidch    uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7098255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
7099255736Sdavidch    uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7100255736Sdavidch                                        NIG_REG_MASK_INTERRUPT_PORT0;
7101255736Sdavidch    uint32_t aeu_mask;
7102255736Sdavidch    uint32_t nig_mask = 0;
7103255736Sdavidch    uint32_t reg_addr;
7104255736Sdavidch    uint32_t igu_acked;
7105255736Sdavidch    uint32_t cnt;
7106255736Sdavidch
7107255736Sdavidch    if (sc->attn_state & asserted) {
7108255736Sdavidch        BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7109255736Sdavidch    }
7110255736Sdavidch
7111255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7112255736Sdavidch
7113255736Sdavidch    aeu_mask = REG_RD(sc, aeu_addr);
7114255736Sdavidch
7115255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7116255736Sdavidch          aeu_mask, asserted);
7117255736Sdavidch
7118255736Sdavidch    aeu_mask &= ~(asserted & 0x3ff);
7119255736Sdavidch
7120255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7121255736Sdavidch
7122255736Sdavidch    REG_WR(sc, aeu_addr, aeu_mask);
7123255736Sdavidch
7124255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7125255736Sdavidch
7126255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7127255736Sdavidch    sc->attn_state |= asserted;
7128255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7129255736Sdavidch
7130255736Sdavidch    if (asserted & ATTN_HARD_WIRED_MASK) {
7131255736Sdavidch        if (asserted & ATTN_NIG_FOR_FUNC) {
7132255736Sdavidch
7133284335Sdavidcs	    bxe_acquire_phy_lock(sc);
7134255736Sdavidch            /* save nig interrupt mask */
7135255736Sdavidch            nig_mask = REG_RD(sc, nig_int_mask_addr);
7136255736Sdavidch
7137255736Sdavidch            /* If nig_mask is not set, no need to call the update function */
7138255736Sdavidch            if (nig_mask) {
7139255736Sdavidch                REG_WR(sc, nig_int_mask_addr, 0);
7140255736Sdavidch
7141255736Sdavidch                bxe_link_attn(sc);
7142255736Sdavidch            }
7143255736Sdavidch
7144255736Sdavidch            /* handle unicore attn? */
7145255736Sdavidch        }
7146255736Sdavidch
7147255736Sdavidch        if (asserted & ATTN_SW_TIMER_4_FUNC) {
7148255736Sdavidch            BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7149255736Sdavidch        }
7150255736Sdavidch
7151255736Sdavidch        if (asserted & GPIO_2_FUNC) {
7152255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7153255736Sdavidch        }
7154255736Sdavidch
7155255736Sdavidch        if (asserted & GPIO_3_FUNC) {
7156255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7157255736Sdavidch        }
7158255736Sdavidch
7159255736Sdavidch        if (asserted & GPIO_4_FUNC) {
7160255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7161255736Sdavidch        }
7162255736Sdavidch
7163255736Sdavidch        if (port == 0) {
7164255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_1) {
7165255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7166255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7167255736Sdavidch            }
7168255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_2) {
7169255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7170255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7171255736Sdavidch            }
7172255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_3) {
7173255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7174255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7175255736Sdavidch            }
7176255736Sdavidch        } else {
7177255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_4) {
7178255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7179255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7180255736Sdavidch            }
7181255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_5) {
7182255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7183255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7184255736Sdavidch            }
7185255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_6) {
7186255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7187255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7188255736Sdavidch            }
7189255736Sdavidch        }
7190255736Sdavidch    } /* hardwired */
7191255736Sdavidch
7192255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7193255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7194255736Sdavidch    } else {
7195255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7196255736Sdavidch    }
7197255736Sdavidch
7198255736Sdavidch    BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7199255736Sdavidch          asserted,
7200255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7201255736Sdavidch    REG_WR(sc, reg_addr, asserted);
7202255736Sdavidch
7203255736Sdavidch    /* now set back the mask */
7204255736Sdavidch    if (asserted & ATTN_NIG_FOR_FUNC) {
7205255736Sdavidch        /*
7206255736Sdavidch         * Verify that IGU ack through BAR was written before restoring
7207255736Sdavidch         * NIG mask. This loop should exit after 2-3 iterations max.
7208255736Sdavidch         */
7209255736Sdavidch        if (sc->devinfo.int_block != INT_BLOCK_HC) {
7210255736Sdavidch            cnt = 0;
7211255736Sdavidch
7212255736Sdavidch            do {
7213255736Sdavidch                igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7214255736Sdavidch            } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7215255736Sdavidch                     (++cnt < MAX_IGU_ATTN_ACK_TO));
7216255736Sdavidch
7217255736Sdavidch            if (!igu_acked) {
7218255736Sdavidch                BLOGE(sc, "Failed to verify IGU ack on time\n");
7219255736Sdavidch            }
7220255736Sdavidch
7221255736Sdavidch            mb();
7222255736Sdavidch        }
7223255736Sdavidch
7224255736Sdavidch        REG_WR(sc, nig_int_mask_addr, nig_mask);
7225255736Sdavidch
7226284335Sdavidcs	bxe_release_phy_lock(sc);
7227255736Sdavidch    }
7228255736Sdavidch}
7229255736Sdavidch
7230255736Sdavidchstatic void
7231255736Sdavidchbxe_print_next_block(struct bxe_softc *sc,
7232255736Sdavidch                     int              idx,
7233255736Sdavidch                     const char       *blk)
7234255736Sdavidch{
7235255736Sdavidch    BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7236255736Sdavidch}
7237255736Sdavidch
7238255736Sdavidchstatic int
7239255736Sdavidchbxe_check_blocks_with_parity0(struct bxe_softc *sc,
7240255736Sdavidch                              uint32_t         sig,
7241255736Sdavidch                              int              par_num,
7242255736Sdavidch                              uint8_t          print)
7243255736Sdavidch{
7244255736Sdavidch    uint32_t cur_bit = 0;
7245255736Sdavidch    int i = 0;
7246255736Sdavidch
7247255736Sdavidch    for (i = 0; sig; i++) {
7248255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7249255736Sdavidch        if (sig & cur_bit) {
7250255736Sdavidch            switch (cur_bit) {
7251255736Sdavidch            case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7252255736Sdavidch                if (print)
7253255736Sdavidch                    bxe_print_next_block(sc, par_num++, "BRB");
7254255736Sdavidch                break;
7255255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7256255736Sdavidch                if (print)
7257255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PARSER");
7258255736Sdavidch                break;
7259255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7260255736Sdavidch                if (print)
7261255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSDM");
7262255736Sdavidch                break;
7263255736Sdavidch            case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7264255736Sdavidch                if (print)
7265255736Sdavidch                    bxe_print_next_block(sc, par_num++, "SEARCHER");
7266255736Sdavidch                break;
7267255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7268255736Sdavidch                if (print)
7269255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TCM");
7270255736Sdavidch                break;
7271255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7272255736Sdavidch                if (print)
7273255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSEMI");
7274255736Sdavidch                break;
7275255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7276255736Sdavidch                if (print)
7277255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XPB");
7278255736Sdavidch                break;
7279255736Sdavidch            }
7280255736Sdavidch
7281255736Sdavidch            /* Clear the bit */
7282255736Sdavidch            sig &= ~cur_bit;
7283255736Sdavidch        }
7284255736Sdavidch    }
7285255736Sdavidch
7286255736Sdavidch    return (par_num);
7287255736Sdavidch}
7288255736Sdavidch
7289255736Sdavidchstatic int
7290255736Sdavidchbxe_check_blocks_with_parity1(struct bxe_softc *sc,
7291255736Sdavidch                              uint32_t         sig,
7292255736Sdavidch                              int              par_num,
7293255736Sdavidch                              uint8_t          *global,
7294255736Sdavidch                              uint8_t          print)
7295255736Sdavidch{
7296255736Sdavidch    int i = 0;
7297255736Sdavidch    uint32_t cur_bit = 0;
7298255736Sdavidch    for (i = 0; sig; i++) {
7299255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7300255736Sdavidch        if (sig & cur_bit) {
7301255736Sdavidch            switch (cur_bit) {
7302255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7303255736Sdavidch                if (print)
7304255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PBF");
7305255736Sdavidch                break;
7306255736Sdavidch            case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7307255736Sdavidch                if (print)
7308255736Sdavidch                    bxe_print_next_block(sc, par_num++, "QM");
7309255736Sdavidch                break;
7310255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7311255736Sdavidch                if (print)
7312255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TM");
7313255736Sdavidch                break;
7314255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7315255736Sdavidch                if (print)
7316255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSDM");
7317255736Sdavidch                break;
7318255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7319255736Sdavidch                if (print)
7320255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XCM");
7321255736Sdavidch                break;
7322255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7323255736Sdavidch                if (print)
7324255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSEMI");
7325255736Sdavidch                break;
7326255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7327255736Sdavidch                if (print)
7328255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7329255736Sdavidch                break;
7330255736Sdavidch            case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7331255736Sdavidch                if (print)
7332255736Sdavidch                    bxe_print_next_block(sc, par_num++, "NIG");
7333255736Sdavidch                break;
7334255736Sdavidch            case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7335255736Sdavidch                if (print)
7336255736Sdavidch                    bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7337255736Sdavidch                *global = TRUE;
7338255736Sdavidch                break;
7339255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7340255736Sdavidch                if (print)
7341255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DEBUG");
7342255736Sdavidch                break;
7343255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7344255736Sdavidch                if (print)
7345255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USDM");
7346255736Sdavidch                break;
7347255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7348255736Sdavidch                if (print)
7349255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UCM");
7350255736Sdavidch                break;
7351255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7352255736Sdavidch                if (print)
7353255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USEMI");
7354255736Sdavidch                break;
7355255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7356255736Sdavidch                if (print)
7357255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UPB");
7358255736Sdavidch                break;
7359255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7360255736Sdavidch                if (print)
7361255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSDM");
7362255736Sdavidch                break;
7363255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7364255736Sdavidch                if (print)
7365255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CCM");
7366255736Sdavidch                break;
7367255736Sdavidch            }
7368255736Sdavidch
7369255736Sdavidch            /* Clear the bit */
7370255736Sdavidch            sig &= ~cur_bit;
7371255736Sdavidch        }
7372255736Sdavidch    }
7373255736Sdavidch
7374255736Sdavidch    return (par_num);
7375255736Sdavidch}
7376255736Sdavidch
7377255736Sdavidchstatic int
7378255736Sdavidchbxe_check_blocks_with_parity2(struct bxe_softc *sc,
7379255736Sdavidch                              uint32_t         sig,
7380255736Sdavidch                              int              par_num,
7381255736Sdavidch                              uint8_t          print)
7382255736Sdavidch{
7383255736Sdavidch    uint32_t cur_bit = 0;
7384255736Sdavidch    int i = 0;
7385255736Sdavidch
7386255736Sdavidch    for (i = 0; sig; i++) {
7387255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7388255736Sdavidch        if (sig & cur_bit) {
7389255736Sdavidch            switch (cur_bit) {
7390255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7391255736Sdavidch                if (print)
7392255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSEMI");
7393255736Sdavidch                break;
7394255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7395255736Sdavidch                if (print)
7396255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXP");
7397255736Sdavidch                break;
7398255736Sdavidch            case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7399255736Sdavidch                if (print)
7400255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7401255736Sdavidch                break;
7402255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7403255736Sdavidch                if (print)
7404255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CFC");
7405255736Sdavidch                break;
7406255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7407255736Sdavidch                if (print)
7408255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CDU");
7409255736Sdavidch                break;
7410255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7411255736Sdavidch                if (print)
7412255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DMAE");
7413255736Sdavidch                break;
7414255736Sdavidch            case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7415255736Sdavidch                if (print)
7416255736Sdavidch                    bxe_print_next_block(sc, par_num++, "IGU");
7417255736Sdavidch                break;
7418255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7419255736Sdavidch                if (print)
7420255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MISC");
7421255736Sdavidch                break;
7422255736Sdavidch            }
7423255736Sdavidch
7424255736Sdavidch            /* Clear the bit */
7425255736Sdavidch            sig &= ~cur_bit;
7426255736Sdavidch        }
7427255736Sdavidch    }
7428255736Sdavidch
7429255736Sdavidch    return (par_num);
7430255736Sdavidch}
7431255736Sdavidch
7432255736Sdavidchstatic int
7433255736Sdavidchbxe_check_blocks_with_parity3(struct bxe_softc *sc,
7434255736Sdavidch                              uint32_t         sig,
7435255736Sdavidch                              int              par_num,
7436255736Sdavidch                              uint8_t          *global,
7437255736Sdavidch                              uint8_t          print)
7438255736Sdavidch{
7439255736Sdavidch    uint32_t cur_bit = 0;
7440255736Sdavidch    int i = 0;
7441255736Sdavidch
7442255736Sdavidch    for (i = 0; sig; i++) {
7443255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7444255736Sdavidch        if (sig & cur_bit) {
7445255736Sdavidch            switch (cur_bit) {
7446255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7447255736Sdavidch                if (print)
7448255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MCP ROM");
7449255736Sdavidch                *global = TRUE;
7450255736Sdavidch                break;
7451255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7452255736Sdavidch                if (print)
7453255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7454255736Sdavidch                              "MCP UMP RX");
7455255736Sdavidch                *global = TRUE;
7456255736Sdavidch                break;
7457255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7458255736Sdavidch                if (print)
7459255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7460255736Sdavidch                              "MCP UMP TX");
7461255736Sdavidch                *global = TRUE;
7462255736Sdavidch                break;
7463255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7464255736Sdavidch                if (print)
7465255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7466255736Sdavidch                              "MCP SCPAD");
7467255736Sdavidch                *global = TRUE;
7468255736Sdavidch                break;
7469255736Sdavidch            }
7470255736Sdavidch
7471255736Sdavidch            /* Clear the bit */
7472255736Sdavidch            sig &= ~cur_bit;
7473255736Sdavidch        }
7474255736Sdavidch    }
7475255736Sdavidch
7476255736Sdavidch    return (par_num);
7477255736Sdavidch}
7478255736Sdavidch
7479255736Sdavidchstatic int
7480255736Sdavidchbxe_check_blocks_with_parity4(struct bxe_softc *sc,
7481255736Sdavidch                              uint32_t         sig,
7482255736Sdavidch                              int              par_num,
7483255736Sdavidch                              uint8_t          print)
7484255736Sdavidch{
7485255736Sdavidch    uint32_t cur_bit = 0;
7486255736Sdavidch    int i = 0;
7487255736Sdavidch
7488255736Sdavidch    for (i = 0; sig; i++) {
7489255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7490255736Sdavidch        if (sig & cur_bit) {
7491255736Sdavidch            switch (cur_bit) {
7492255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7493255736Sdavidch                if (print)
7494255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PGLUE_B");
7495255736Sdavidch                break;
7496255736Sdavidch            case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7497255736Sdavidch                if (print)
7498255736Sdavidch                    bxe_print_next_block(sc, par_num++, "ATC");
7499255736Sdavidch                break;
7500255736Sdavidch            }
7501255736Sdavidch
7502255736Sdavidch            /* Clear the bit */
7503255736Sdavidch            sig &= ~cur_bit;
7504255736Sdavidch        }
7505255736Sdavidch    }
7506255736Sdavidch
7507255736Sdavidch    return (par_num);
7508255736Sdavidch}
7509255736Sdavidch
7510255736Sdavidchstatic uint8_t
7511255736Sdavidchbxe_parity_attn(struct bxe_softc *sc,
7512255736Sdavidch                uint8_t          *global,
7513255736Sdavidch                uint8_t          print,
7514255736Sdavidch                uint32_t         *sig)
7515255736Sdavidch{
7516255736Sdavidch    int par_num = 0;
7517255736Sdavidch
7518255736Sdavidch    if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7519255736Sdavidch        (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7520255736Sdavidch        (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7521255736Sdavidch        (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7522255736Sdavidch        (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7523255736Sdavidch        BLOGE(sc, "Parity error: HW block parity attention:\n"
7524255736Sdavidch                  "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7525255736Sdavidch              (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7526255736Sdavidch              (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7527255736Sdavidch              (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7528255736Sdavidch              (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7529255736Sdavidch              (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7530255736Sdavidch
7531255736Sdavidch        if (print)
7532255736Sdavidch            BLOGI(sc, "Parity errors detected in blocks: ");
7533255736Sdavidch
7534255736Sdavidch        par_num =
7535255736Sdavidch            bxe_check_blocks_with_parity0(sc, sig[0] &
7536255736Sdavidch                                          HW_PRTY_ASSERT_SET_0,
7537255736Sdavidch                                          par_num, print);
7538255736Sdavidch        par_num =
7539255736Sdavidch            bxe_check_blocks_with_parity1(sc, sig[1] &
7540255736Sdavidch                                          HW_PRTY_ASSERT_SET_1,
7541255736Sdavidch                                          par_num, global, print);
7542255736Sdavidch        par_num =
7543255736Sdavidch            bxe_check_blocks_with_parity2(sc, sig[2] &
7544255736Sdavidch                                          HW_PRTY_ASSERT_SET_2,
7545255736Sdavidch                                          par_num, print);
7546255736Sdavidch        par_num =
7547255736Sdavidch            bxe_check_blocks_with_parity3(sc, sig[3] &
7548255736Sdavidch                                          HW_PRTY_ASSERT_SET_3,
7549255736Sdavidch                                          par_num, global, print);
7550255736Sdavidch        par_num =
7551255736Sdavidch            bxe_check_blocks_with_parity4(sc, sig[4] &
7552255736Sdavidch                                          HW_PRTY_ASSERT_SET_4,
7553255736Sdavidch                                          par_num, print);
7554255736Sdavidch
7555255736Sdavidch        if (print)
7556255736Sdavidch            BLOGI(sc, "\n");
7557255736Sdavidch
7558255736Sdavidch        return (TRUE);
7559255736Sdavidch    }
7560255736Sdavidch
7561255736Sdavidch    return (FALSE);
7562255736Sdavidch}
7563255736Sdavidch
7564255736Sdavidchstatic uint8_t
7565255736Sdavidchbxe_chk_parity_attn(struct bxe_softc *sc,
7566255736Sdavidch                    uint8_t          *global,
7567255736Sdavidch                    uint8_t          print)
7568255736Sdavidch{
7569255736Sdavidch    struct attn_route attn = { {0} };
7570255736Sdavidch    int port = SC_PORT(sc);
7571255736Sdavidch
7572255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7573255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7574255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7575255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7576255736Sdavidch
7577295823Sdavidcs    /*
7578295823Sdavidcs     * Since MCP attentions can't be disabled inside the block, we need to
7579295823Sdavidcs     * read AEU registers to see whether they're currently disabled
7580295823Sdavidcs     */
7581295823Sdavidcs    attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7582295823Sdavidcs                                      : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7583295823Sdavidcs                         MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7584295823Sdavidcs                        ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7585295823Sdavidcs
7586295823Sdavidcs
7587255736Sdavidch    if (!CHIP_IS_E1x(sc))
7588255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7589255736Sdavidch
7590255736Sdavidch    return (bxe_parity_attn(sc, global, print, attn.sig));
7591255736Sdavidch}
7592255736Sdavidch
7593255736Sdavidchstatic void
7594255736Sdavidchbxe_attn_int_deasserted4(struct bxe_softc *sc,
7595255736Sdavidch                         uint32_t         attn)
7596255736Sdavidch{
7597255736Sdavidch    uint32_t val;
7598255736Sdavidch
7599255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7600255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7601255736Sdavidch        BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7602255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7603255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7604255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7605255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7606255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7607255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7608255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7609255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7610255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7611255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7612255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7613255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7614255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7615255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7616255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7617255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7618255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7619255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7620255736Sdavidch    }
7621255736Sdavidch
7622255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7623255736Sdavidch        val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7624255736Sdavidch        BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7625255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7626255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7627255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7628255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7629255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7630255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7631255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7632255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7633255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7634255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7635255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7636255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7637255736Sdavidch    }
7638255736Sdavidch
7639255736Sdavidch    if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7640255736Sdavidch                AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7641255736Sdavidch        BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7642255736Sdavidch              (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7643255736Sdavidch                                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7644255736Sdavidch    }
7645255736Sdavidch}
7646255736Sdavidch
7647255736Sdavidchstatic void
7648255736Sdavidchbxe_e1h_disable(struct bxe_softc *sc)
7649255736Sdavidch{
7650255736Sdavidch    int port = SC_PORT(sc);
7651255736Sdavidch
7652255736Sdavidch    bxe_tx_disable(sc);
7653255736Sdavidch
7654255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7655255736Sdavidch}
7656255736Sdavidch
7657255736Sdavidchstatic void
7658255736Sdavidchbxe_e1h_enable(struct bxe_softc *sc)
7659255736Sdavidch{
7660255736Sdavidch    int port = SC_PORT(sc);
7661255736Sdavidch
7662255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7663255736Sdavidch
7664255736Sdavidch    // XXX bxe_tx_enable(sc);
7665255736Sdavidch}
7666255736Sdavidch
7667255736Sdavidch/*
7668255736Sdavidch * called due to MCP event (on pmf):
7669255736Sdavidch *   reread new bandwidth configuration
7670255736Sdavidch *   configure FW
7671255736Sdavidch *   notify others function about the change
7672255736Sdavidch */
7673255736Sdavidchstatic void
7674255736Sdavidchbxe_config_mf_bw(struct bxe_softc *sc)
7675255736Sdavidch{
7676255736Sdavidch    if (sc->link_vars.link_up) {
7677255736Sdavidch        bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7678255736Sdavidch        // XXX bxe_link_sync_notify(sc);
7679255736Sdavidch    }
7680255736Sdavidch
7681255736Sdavidch    storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7682255736Sdavidch}
7683255736Sdavidch
7684255736Sdavidchstatic void
7685255736Sdavidchbxe_set_mf_bw(struct bxe_softc *sc)
7686255736Sdavidch{
7687255736Sdavidch    bxe_config_mf_bw(sc);
7688255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7689255736Sdavidch}
7690255736Sdavidch
7691255736Sdavidchstatic void
7692255736Sdavidchbxe_handle_eee_event(struct bxe_softc *sc)
7693255736Sdavidch{
7694255736Sdavidch    BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7695255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7696255736Sdavidch}
7697255736Sdavidch
7698255736Sdavidch#define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7699255736Sdavidch
7700255736Sdavidchstatic void
7701255736Sdavidchbxe_drv_info_ether_stat(struct bxe_softc *sc)
7702255736Sdavidch{
7703255736Sdavidch    struct eth_stats_info *ether_stat =
7704255736Sdavidch        &sc->sp->drv_info_to_mcp.ether_stat;
7705255736Sdavidch
7706255736Sdavidch    strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7707255736Sdavidch            ETH_STAT_INFO_VERSION_LEN);
7708255736Sdavidch
7709255736Sdavidch    /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7710255736Sdavidch    sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7711255736Sdavidch                                          DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7712255736Sdavidch                                          ether_stat->mac_local + MAC_PAD,
7713255736Sdavidch                                          MAC_PAD, ETH_ALEN);
7714255736Sdavidch
7715255736Sdavidch    ether_stat->mtu_size = sc->mtu;
7716255736Sdavidch
7717255736Sdavidch    ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7718266979Smarcel    if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
7719255736Sdavidch        ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7720255736Sdavidch    }
7721255736Sdavidch
7722255736Sdavidch    // XXX ether_stat->feature_flags |= ???;
7723255736Sdavidch
7724255736Sdavidch    ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7725255736Sdavidch
7726255736Sdavidch    ether_stat->txq_size = sc->tx_ring_size;
7727255736Sdavidch    ether_stat->rxq_size = sc->rx_ring_size;
7728255736Sdavidch}
7729255736Sdavidch
7730255736Sdavidchstatic void
7731255736Sdavidchbxe_handle_drv_info_req(struct bxe_softc *sc)
7732255736Sdavidch{
7733255736Sdavidch    enum drv_info_opcode op_code;
7734255736Sdavidch    uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7735255736Sdavidch
7736255736Sdavidch    /* if drv_info version supported by MFW doesn't match - send NACK */
7737255736Sdavidch    if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7738255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7739255736Sdavidch        return;
7740255736Sdavidch    }
7741255736Sdavidch
7742255736Sdavidch    op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7743255736Sdavidch               DRV_INFO_CONTROL_OP_CODE_SHIFT);
7744255736Sdavidch
7745255736Sdavidch    memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7746255736Sdavidch
7747255736Sdavidch    switch (op_code) {
7748255736Sdavidch    case ETH_STATS_OPCODE:
7749255736Sdavidch        bxe_drv_info_ether_stat(sc);
7750255736Sdavidch        break;
7751255736Sdavidch    case FCOE_STATS_OPCODE:
7752255736Sdavidch    case ISCSI_STATS_OPCODE:
7753255736Sdavidch    default:
7754255736Sdavidch        /* if op code isn't supported - send NACK */
7755255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7756255736Sdavidch        return;
7757255736Sdavidch    }
7758255736Sdavidch
7759255736Sdavidch    /*
7760255736Sdavidch     * If we got drv_info attn from MFW then these fields are defined in
7761255736Sdavidch     * shmem2 for sure
7762255736Sdavidch     */
7763255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_lo,
7764255736Sdavidch              U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7765255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_hi,
7766255736Sdavidch              U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7767255736Sdavidch
7768255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7769255736Sdavidch}
7770255736Sdavidch
7771255736Sdavidchstatic void
7772255736Sdavidchbxe_dcc_event(struct bxe_softc *sc,
7773255736Sdavidch              uint32_t         dcc_event)
7774255736Sdavidch{
7775255736Sdavidch    BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7776255736Sdavidch
7777255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7778255736Sdavidch        /*
7779255736Sdavidch         * This is the only place besides the function initialization
7780255736Sdavidch         * where the sc->flags can change so it is done without any
7781255736Sdavidch         * locks
7782255736Sdavidch         */
7783255736Sdavidch        if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7784255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7785255736Sdavidch            sc->flags |= BXE_MF_FUNC_DIS;
7786255736Sdavidch            bxe_e1h_disable(sc);
7787255736Sdavidch        } else {
7788255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7789255736Sdavidch            sc->flags &= ~BXE_MF_FUNC_DIS;
7790255736Sdavidch            bxe_e1h_enable(sc);
7791255736Sdavidch        }
7792255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7793255736Sdavidch    }
7794255736Sdavidch
7795255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7796255736Sdavidch        bxe_config_mf_bw(sc);
7797255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7798255736Sdavidch    }
7799255736Sdavidch
7800255736Sdavidch    /* Report results to MCP */
7801255736Sdavidch    if (dcc_event)
7802255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7803255736Sdavidch    else
7804255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7805255736Sdavidch}
7806255736Sdavidch
7807255736Sdavidchstatic void
7808255736Sdavidchbxe_pmf_update(struct bxe_softc *sc)
7809255736Sdavidch{
7810255736Sdavidch    int port = SC_PORT(sc);
7811255736Sdavidch    uint32_t val;
7812255736Sdavidch
7813255736Sdavidch    sc->port.pmf = 1;
7814255736Sdavidch    BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7815255736Sdavidch
7816255736Sdavidch    /*
7817255736Sdavidch     * We need the mb() to ensure the ordering between the writing to
7818255736Sdavidch     * sc->port.pmf here and reading it from the bxe_periodic_task().
7819255736Sdavidch     */
7820255736Sdavidch    mb();
7821255736Sdavidch
7822255736Sdavidch    /* queue a periodic task */
7823255736Sdavidch    // XXX schedule task...
7824255736Sdavidch
7825255736Sdavidch    // XXX bxe_dcbx_pmf_update(sc);
7826255736Sdavidch
7827255736Sdavidch    /* enable nig attention */
7828255736Sdavidch    val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7829255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7830255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7831255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7832255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
7833255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7834255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7835255736Sdavidch    }
7836255736Sdavidch
7837255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_PMF);
7838255736Sdavidch}
7839255736Sdavidch
7840255736Sdavidchstatic int
7841255736Sdavidchbxe_mc_assert(struct bxe_softc *sc)
7842255736Sdavidch{
7843255736Sdavidch    char last_idx;
7844255736Sdavidch    int i, rc = 0;
7845255736Sdavidch    uint32_t row0, row1, row2, row3;
7846255736Sdavidch
7847255736Sdavidch    /* XSTORM */
7848255736Sdavidch    last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7849255736Sdavidch    if (last_idx)
7850255736Sdavidch        BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7851255736Sdavidch
7852255736Sdavidch    /* print the asserts */
7853255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7854255736Sdavidch
7855255736Sdavidch        row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7856255736Sdavidch        row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7857255736Sdavidch        row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7858255736Sdavidch        row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7859255736Sdavidch
7860255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7861255736Sdavidch            BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7862255736Sdavidch                  i, row3, row2, row1, row0);
7863255736Sdavidch            rc++;
7864255736Sdavidch        } else {
7865255736Sdavidch            break;
7866255736Sdavidch        }
7867255736Sdavidch    }
7868255736Sdavidch
7869255736Sdavidch    /* TSTORM */
7870255736Sdavidch    last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7871255736Sdavidch    if (last_idx) {
7872255736Sdavidch        BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7873255736Sdavidch    }
7874255736Sdavidch
7875255736Sdavidch    /* print the asserts */
7876255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7877255736Sdavidch
7878255736Sdavidch        row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7879255736Sdavidch        row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7880255736Sdavidch        row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7881255736Sdavidch        row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7882255736Sdavidch
7883255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7884255736Sdavidch            BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7885255736Sdavidch                  i, row3, row2, row1, row0);
7886255736Sdavidch            rc++;
7887255736Sdavidch        } else {
7888255736Sdavidch            break;
7889255736Sdavidch        }
7890255736Sdavidch    }
7891255736Sdavidch
7892255736Sdavidch    /* CSTORM */
7893255736Sdavidch    last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7894255736Sdavidch    if (last_idx) {
7895255736Sdavidch        BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7896255736Sdavidch    }
7897255736Sdavidch
7898255736Sdavidch    /* print the asserts */
7899255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7900255736Sdavidch
7901255736Sdavidch        row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7902255736Sdavidch        row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7903255736Sdavidch        row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7904255736Sdavidch        row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7905255736Sdavidch
7906255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7907255736Sdavidch            BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7908255736Sdavidch                  i, row3, row2, row1, row0);
7909255736Sdavidch            rc++;
7910255736Sdavidch        } else {
7911255736Sdavidch            break;
7912255736Sdavidch        }
7913255736Sdavidch    }
7914255736Sdavidch
7915255736Sdavidch    /* USTORM */
7916255736Sdavidch    last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7917255736Sdavidch    if (last_idx) {
7918255736Sdavidch        BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7919255736Sdavidch    }
7920255736Sdavidch
7921255736Sdavidch    /* print the asserts */
7922255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7923255736Sdavidch
7924255736Sdavidch        row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7925255736Sdavidch        row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7926255736Sdavidch        row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7927255736Sdavidch        row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7928255736Sdavidch
7929255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7930255736Sdavidch            BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7931255736Sdavidch                  i, row3, row2, row1, row0);
7932255736Sdavidch            rc++;
7933255736Sdavidch        } else {
7934255736Sdavidch            break;
7935255736Sdavidch        }
7936255736Sdavidch    }
7937255736Sdavidch
7938255736Sdavidch    return (rc);
7939255736Sdavidch}
7940255736Sdavidch
7941255736Sdavidchstatic void
7942255736Sdavidchbxe_attn_int_deasserted3(struct bxe_softc *sc,
7943255736Sdavidch                         uint32_t         attn)
7944255736Sdavidch{
7945255736Sdavidch    int func = SC_FUNC(sc);
7946255736Sdavidch    uint32_t val;
7947255736Sdavidch
7948255736Sdavidch    if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
7949255736Sdavidch
7950255736Sdavidch        if (attn & BXE_PMF_LINK_ASSERT(sc)) {
7951255736Sdavidch
7952255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
7953255736Sdavidch            bxe_read_mf_cfg(sc);
7954255736Sdavidch            sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
7955255736Sdavidch                MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
7956255736Sdavidch            val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
7957255736Sdavidch
7958255736Sdavidch            if (val & DRV_STATUS_DCC_EVENT_MASK)
7959255736Sdavidch                bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
7960255736Sdavidch
7961255736Sdavidch            if (val & DRV_STATUS_SET_MF_BW)
7962255736Sdavidch                bxe_set_mf_bw(sc);
7963255736Sdavidch
7964255736Sdavidch            if (val & DRV_STATUS_DRV_INFO_REQ)
7965255736Sdavidch                bxe_handle_drv_info_req(sc);
7966255736Sdavidch
7967255736Sdavidch            if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
7968255736Sdavidch                bxe_pmf_update(sc);
7969255736Sdavidch
7970255736Sdavidch            if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
7971255736Sdavidch                bxe_handle_eee_event(sc);
7972255736Sdavidch
7973255736Sdavidch            if (sc->link_vars.periodic_flags &
7974255736Sdavidch                ELINK_PERIODIC_FLAGS_LINK_EVENT) {
7975255736Sdavidch                /* sync with link */
7976284335Sdavidcs		bxe_acquire_phy_lock(sc);
7977255736Sdavidch                sc->link_vars.periodic_flags &=
7978255736Sdavidch                    ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
7979284335Sdavidcs		bxe_release_phy_lock(sc);
7980255736Sdavidch                if (IS_MF(sc))
7981255736Sdavidch                    ; // XXX bxe_link_sync_notify(sc);
7982255736Sdavidch                bxe_link_report(sc);
7983255736Sdavidch            }
7984255736Sdavidch
7985255736Sdavidch            /*
7986255736Sdavidch             * Always call it here: bxe_link_report() will
7987255736Sdavidch             * prevent the link indication duplication.
7988255736Sdavidch             */
7989255736Sdavidch            bxe_link_status_update(sc);
7990255736Sdavidch
7991255736Sdavidch        } else if (attn & BXE_MC_ASSERT_BITS) {
7992255736Sdavidch
7993255736Sdavidch            BLOGE(sc, "MC assert!\n");
7994255736Sdavidch            bxe_mc_assert(sc);
7995255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
7996255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
7997255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
7998255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
7999255736Sdavidch            bxe_panic(sc, ("MC assert!\n"));
8000255736Sdavidch
8001255736Sdavidch        } else if (attn & BXE_MCP_ASSERT) {
8002255736Sdavidch
8003255736Sdavidch            BLOGE(sc, "MCP assert!\n");
8004255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8005255736Sdavidch            // XXX bxe_fw_dump(sc);
8006255736Sdavidch
8007255736Sdavidch        } else {
8008255736Sdavidch            BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8009255736Sdavidch        }
8010255736Sdavidch    }
8011255736Sdavidch
8012255736Sdavidch    if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8013255736Sdavidch        BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8014255736Sdavidch        if (attn & BXE_GRC_TIMEOUT) {
8015255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8016255736Sdavidch            BLOGE(sc, "GRC time-out 0x%08x\n", val);
8017255736Sdavidch        }
8018255736Sdavidch        if (attn & BXE_GRC_RSV) {
8019255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8020255736Sdavidch            BLOGE(sc, "GRC reserved 0x%08x\n", val);
8021255736Sdavidch        }
8022255736Sdavidch        REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8023255736Sdavidch    }
8024255736Sdavidch}
8025255736Sdavidch
8026255736Sdavidchstatic void
8027255736Sdavidchbxe_attn_int_deasserted2(struct bxe_softc *sc,
8028255736Sdavidch                         uint32_t         attn)
8029255736Sdavidch{
8030255736Sdavidch    int port = SC_PORT(sc);
8031255736Sdavidch    int reg_offset;
8032255736Sdavidch    uint32_t val0, mask0, val1, mask1;
8033255736Sdavidch    uint32_t val;
8034255736Sdavidch
8035255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8036255736Sdavidch        val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8037255736Sdavidch        BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8038255736Sdavidch        /* CFC error attention */
8039255736Sdavidch        if (val & 0x2) {
8040255736Sdavidch            BLOGE(sc, "FATAL error from CFC\n");
8041255736Sdavidch        }
8042255736Sdavidch    }
8043255736Sdavidch
8044255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8045255736Sdavidch        val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8046255736Sdavidch        BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8047255736Sdavidch        /* RQ_USDMDP_FIFO_OVERFLOW */
8048255736Sdavidch        if (val & 0x18000) {
8049255736Sdavidch            BLOGE(sc, "FATAL error from PXP\n");
8050255736Sdavidch        }
8051255736Sdavidch
8052255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8053255736Sdavidch            val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8054255736Sdavidch            BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8055255736Sdavidch        }
8056255736Sdavidch    }
8057255736Sdavidch
8058255736Sdavidch#define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8059255736Sdavidch#define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8060255736Sdavidch
8061255736Sdavidch    if (attn & AEU_PXP2_HW_INT_BIT) {
8062255736Sdavidch        /*  CQ47854 workaround do not panic on
8063255736Sdavidch         *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8064255736Sdavidch         */
8065255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8066255736Sdavidch            mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8067255736Sdavidch            val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8068255736Sdavidch            mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8069255736Sdavidch            val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8070255736Sdavidch            /*
8071298955Spfg             * If the only PXP2_EOP_ERROR_BIT is set in
8072255736Sdavidch             * STS0 and STS1 - clear it
8073255736Sdavidch             *
8074255736Sdavidch             * probably we lose additional attentions between
8075255736Sdavidch             * STS0 and STS_CLR0, in this case user will not
8076255736Sdavidch             * be notified about them
8077255736Sdavidch             */
8078255736Sdavidch            if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8079255736Sdavidch                !(val1 & mask1))
8080255736Sdavidch                val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8081255736Sdavidch
8082255736Sdavidch            /* print the register, since no one can restore it */
8083255736Sdavidch            BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8084255736Sdavidch
8085255736Sdavidch            /*
8086255736Sdavidch             * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8087255736Sdavidch             * then notify
8088255736Sdavidch             */
8089255736Sdavidch            if (val0 & PXP2_EOP_ERROR_BIT) {
8090255736Sdavidch                BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8091255736Sdavidch
8092255736Sdavidch                /*
8093255736Sdavidch                 * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8094255736Sdavidch                 * set then clear attention from PXP2 block without panic
8095255736Sdavidch                 */
8096255736Sdavidch                if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8097255736Sdavidch                    ((val1 & mask1) == 0))
8098255736Sdavidch                    attn &= ~AEU_PXP2_HW_INT_BIT;
8099255736Sdavidch            }
8100255736Sdavidch        }
8101255736Sdavidch    }
8102255736Sdavidch
8103255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_2) {
8104255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8105255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8106255736Sdavidch
8107255736Sdavidch        val = REG_RD(sc, reg_offset);
8108255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8109255736Sdavidch        REG_WR(sc, reg_offset, val);
8110255736Sdavidch
8111255736Sdavidch        BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8112255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8113255736Sdavidch        bxe_panic(sc, ("HW block attention set2\n"));
8114255736Sdavidch    }
8115255736Sdavidch}
8116255736Sdavidch
8117255736Sdavidchstatic void
8118255736Sdavidchbxe_attn_int_deasserted1(struct bxe_softc *sc,
8119255736Sdavidch                         uint32_t         attn)
8120255736Sdavidch{
8121255736Sdavidch    int port = SC_PORT(sc);
8122255736Sdavidch    int reg_offset;
8123255736Sdavidch    uint32_t val;
8124255736Sdavidch
8125255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8126255736Sdavidch        val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8127255736Sdavidch        BLOGE(sc, "DB hw attention 0x%08x\n", val);
8128255736Sdavidch        /* DORQ discard attention */
8129255736Sdavidch        if (val & 0x2) {
8130255736Sdavidch            BLOGE(sc, "FATAL error from DORQ\n");
8131255736Sdavidch        }
8132255736Sdavidch    }
8133255736Sdavidch
8134255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_1) {
8135255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8136255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8137255736Sdavidch
8138255736Sdavidch        val = REG_RD(sc, reg_offset);
8139255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8140255736Sdavidch        REG_WR(sc, reg_offset, val);
8141255736Sdavidch
8142255736Sdavidch        BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8143255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8144255736Sdavidch        bxe_panic(sc, ("HW block attention set1\n"));
8145255736Sdavidch    }
8146255736Sdavidch}
8147255736Sdavidch
8148255736Sdavidchstatic void
8149255736Sdavidchbxe_attn_int_deasserted0(struct bxe_softc *sc,
8150255736Sdavidch                         uint32_t         attn)
8151255736Sdavidch{
8152255736Sdavidch    int port = SC_PORT(sc);
8153255736Sdavidch    int reg_offset;
8154255736Sdavidch    uint32_t val;
8155255736Sdavidch
8156255736Sdavidch    reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8157255736Sdavidch                          MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8158255736Sdavidch
8159255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8160255736Sdavidch        val = REG_RD(sc, reg_offset);
8161255736Sdavidch        val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8162255736Sdavidch        REG_WR(sc, reg_offset, val);
8163255736Sdavidch
8164255736Sdavidch        BLOGW(sc, "SPIO5 hw attention\n");
8165255736Sdavidch
8166255736Sdavidch        /* Fan failure attention */
8167255736Sdavidch        elink_hw_reset_phy(&sc->link_params);
8168255736Sdavidch        bxe_fan_failure(sc);
8169255736Sdavidch    }
8170255736Sdavidch
8171255736Sdavidch    if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8172284335Sdavidcs	bxe_acquire_phy_lock(sc);
8173255736Sdavidch        elink_handle_module_detect_int(&sc->link_params);
8174284335Sdavidcs	bxe_release_phy_lock(sc);
8175255736Sdavidch    }
8176255736Sdavidch
8177255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_0) {
8178255736Sdavidch        val = REG_RD(sc, reg_offset);
8179255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8180255736Sdavidch        REG_WR(sc, reg_offset, val);
8181255736Sdavidch
8182255736Sdavidch        bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8183255736Sdavidch                       (attn & HW_INTERRUT_ASSERT_SET_0)));
8184255736Sdavidch    }
8185255736Sdavidch}
8186255736Sdavidch
8187255736Sdavidchstatic void
8188255736Sdavidchbxe_attn_int_deasserted(struct bxe_softc *sc,
8189255736Sdavidch                        uint32_t         deasserted)
8190255736Sdavidch{
8191255736Sdavidch    struct attn_route attn;
8192255736Sdavidch    struct attn_route *group_mask;
8193255736Sdavidch    int port = SC_PORT(sc);
8194255736Sdavidch    int index;
8195255736Sdavidch    uint32_t reg_addr;
8196255736Sdavidch    uint32_t val;
8197255736Sdavidch    uint32_t aeu_mask;
8198255736Sdavidch    uint8_t global = FALSE;
8199255736Sdavidch
8200255736Sdavidch    /*
8201255736Sdavidch     * Need to take HW lock because MCP or other port might also
8202255736Sdavidch     * try to handle this event.
8203255736Sdavidch     */
8204255736Sdavidch    bxe_acquire_alr(sc);
8205255736Sdavidch
8206255736Sdavidch    if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8207255736Sdavidch        /* XXX
8208255736Sdavidch         * In case of parity errors don't handle attentions so that
8209255736Sdavidch         * other function would "see" parity errors.
8210255736Sdavidch         */
8211255736Sdavidch        sc->recovery_state = BXE_RECOVERY_INIT;
8212255736Sdavidch        // XXX schedule a recovery task...
8213255736Sdavidch        /* disable HW interrupts */
8214255736Sdavidch        bxe_int_disable(sc);
8215255736Sdavidch        bxe_release_alr(sc);
8216255736Sdavidch        return;
8217255736Sdavidch    }
8218255736Sdavidch
8219255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8220255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8221255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8222255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8223255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
8224255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8225255736Sdavidch    } else {
8226255736Sdavidch        attn.sig[4] = 0;
8227255736Sdavidch    }
8228255736Sdavidch
8229255736Sdavidch    BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8230255736Sdavidch          attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8231255736Sdavidch
8232255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8233255736Sdavidch        if (deasserted & (1 << index)) {
8234255736Sdavidch            group_mask = &sc->attn_group[index];
8235255736Sdavidch
8236255736Sdavidch            BLOGD(sc, DBG_INTR,
8237255736Sdavidch                  "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8238255736Sdavidch                  group_mask->sig[0], group_mask->sig[1],
8239255736Sdavidch                  group_mask->sig[2], group_mask->sig[3],
8240255736Sdavidch                  group_mask->sig[4]);
8241255736Sdavidch
8242255736Sdavidch            bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8243255736Sdavidch            bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8244255736Sdavidch            bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8245255736Sdavidch            bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8246255736Sdavidch            bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8247255736Sdavidch        }
8248255736Sdavidch    }
8249255736Sdavidch
8250255736Sdavidch    bxe_release_alr(sc);
8251255736Sdavidch
8252255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
8253255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 +
8254255736Sdavidch                    COMMAND_REG_ATTN_BITS_CLR);
8255255736Sdavidch    } else {
8256255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8257255736Sdavidch    }
8258255736Sdavidch
8259255736Sdavidch    val = ~deasserted;
8260255736Sdavidch    BLOGD(sc, DBG_INTR,
8261255736Sdavidch          "about to mask 0x%08x at %s addr 0x%08x\n", val,
8262255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8263255736Sdavidch    REG_WR(sc, reg_addr, val);
8264255736Sdavidch
8265255736Sdavidch    if (~sc->attn_state & deasserted) {
8266255736Sdavidch        BLOGE(sc, "IGU error\n");
8267255736Sdavidch    }
8268255736Sdavidch
8269255736Sdavidch    reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8270255736Sdavidch                      MISC_REG_AEU_MASK_ATTN_FUNC_0;
8271255736Sdavidch
8272255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8273255736Sdavidch
8274255736Sdavidch    aeu_mask = REG_RD(sc, reg_addr);
8275255736Sdavidch
8276255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8277255736Sdavidch          aeu_mask, deasserted);
8278255736Sdavidch    aeu_mask |= (deasserted & 0x3ff);
8279255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8280255736Sdavidch
8281255736Sdavidch    REG_WR(sc, reg_addr, aeu_mask);
8282255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8283255736Sdavidch
8284255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8285255736Sdavidch    sc->attn_state &= ~deasserted;
8286255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8287255736Sdavidch}
8288255736Sdavidch
8289255736Sdavidchstatic void
8290255736Sdavidchbxe_attn_int(struct bxe_softc *sc)
8291255736Sdavidch{
8292255736Sdavidch    /* read local copy of bits */
8293255736Sdavidch    uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8294255736Sdavidch    uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8295255736Sdavidch    uint32_t attn_state = sc->attn_state;
8296255736Sdavidch
8297255736Sdavidch    /* look for changed bits */
8298255736Sdavidch    uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8299255736Sdavidch    uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8300255736Sdavidch
8301255736Sdavidch    BLOGD(sc, DBG_INTR,
8302255736Sdavidch          "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8303255736Sdavidch          attn_bits, attn_ack, asserted, deasserted);
8304255736Sdavidch
8305255736Sdavidch    if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8306255736Sdavidch        BLOGE(sc, "BAD attention state\n");
8307255736Sdavidch    }
8308255736Sdavidch
8309255736Sdavidch    /* handle bits that were raised */
8310255736Sdavidch    if (asserted) {
8311255736Sdavidch        bxe_attn_int_asserted(sc, asserted);
8312255736Sdavidch    }
8313255736Sdavidch
8314255736Sdavidch    if (deasserted) {
8315255736Sdavidch        bxe_attn_int_deasserted(sc, deasserted);
8316255736Sdavidch    }
8317255736Sdavidch}
8318255736Sdavidch
8319255736Sdavidchstatic uint16_t
8320255736Sdavidchbxe_update_dsb_idx(struct bxe_softc *sc)
8321255736Sdavidch{
8322255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
8323255736Sdavidch    uint16_t rc = 0;
8324255736Sdavidch
8325255736Sdavidch    mb(); /* status block is written to by the chip */
8326255736Sdavidch
8327255736Sdavidch    if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8328255736Sdavidch        sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8329255736Sdavidch        rc |= BXE_DEF_SB_ATT_IDX;
8330255736Sdavidch    }
8331255736Sdavidch
8332255736Sdavidch    if (sc->def_idx != def_sb->sp_sb.running_index) {
8333255736Sdavidch        sc->def_idx = def_sb->sp_sb.running_index;
8334255736Sdavidch        rc |= BXE_DEF_SB_IDX;
8335255736Sdavidch    }
8336255736Sdavidch
8337255736Sdavidch    mb();
8338255736Sdavidch
8339255736Sdavidch    return (rc);
8340255736Sdavidch}
8341255736Sdavidch
8342255736Sdavidchstatic inline struct ecore_queue_sp_obj *
8343255736Sdavidchbxe_cid_to_q_obj(struct bxe_softc *sc,
8344255736Sdavidch                 uint32_t         cid)
8345255736Sdavidch{
8346255736Sdavidch    BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8347255736Sdavidch    return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8348255736Sdavidch}
8349255736Sdavidch
8350255736Sdavidchstatic void
8351255736Sdavidchbxe_handle_mcast_eqe(struct bxe_softc *sc)
8352255736Sdavidch{
8353255736Sdavidch    struct ecore_mcast_ramrod_params rparam;
8354255736Sdavidch    int rc;
8355255736Sdavidch
8356255736Sdavidch    memset(&rparam, 0, sizeof(rparam));
8357255736Sdavidch
8358255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
8359255736Sdavidch
8360255736Sdavidch    BXE_MCAST_LOCK(sc);
8361255736Sdavidch
8362255736Sdavidch    /* clear pending state for the last command */
8363255736Sdavidch    sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8364255736Sdavidch
8365255736Sdavidch    /* if there are pending mcast commands - send them */
8366255736Sdavidch    if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8367255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8368255736Sdavidch        if (rc < 0) {
8369255736Sdavidch            BLOGD(sc, DBG_SP,
8370295830Sdavidcs                "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8371255736Sdavidch        }
8372255736Sdavidch    }
8373255736Sdavidch
8374255736Sdavidch    BXE_MCAST_UNLOCK(sc);
8375255736Sdavidch}
8376255736Sdavidch
8377255736Sdavidchstatic void
8378255736Sdavidchbxe_handle_classification_eqe(struct bxe_softc      *sc,
8379255736Sdavidch                              union event_ring_elem *elem)
8380255736Sdavidch{
8381255736Sdavidch    unsigned long ramrod_flags = 0;
8382255736Sdavidch    int rc = 0;
8383255736Sdavidch    uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8384255736Sdavidch    struct ecore_vlan_mac_obj *vlan_mac_obj;
8385255736Sdavidch
8386255736Sdavidch    /* always push next commands out, don't wait here */
8387255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
8388255736Sdavidch
8389255736Sdavidch    switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8390255736Sdavidch    case ECORE_FILTER_MAC_PENDING:
8391255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8392255736Sdavidch        vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8393255736Sdavidch        break;
8394255736Sdavidch
8395255736Sdavidch    case ECORE_FILTER_MCAST_PENDING:
8396255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8397255736Sdavidch        /*
8398255736Sdavidch         * This is only relevant for 57710 where multicast MACs are
8399255736Sdavidch         * configured as unicast MACs using the same ramrod.
8400255736Sdavidch         */
8401255736Sdavidch        bxe_handle_mcast_eqe(sc);
8402255736Sdavidch        return;
8403255736Sdavidch
8404255736Sdavidch    default:
8405255736Sdavidch        BLOGE(sc, "Unsupported classification command: %d\n",
8406255736Sdavidch              elem->message.data.eth_event.echo);
8407255736Sdavidch        return;
8408255736Sdavidch    }
8409255736Sdavidch
8410255736Sdavidch    rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8411255736Sdavidch
8412255736Sdavidch    if (rc < 0) {
8413255736Sdavidch        BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8414255736Sdavidch    } else if (rc > 0) {
8415255736Sdavidch        BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8416255736Sdavidch    }
8417255736Sdavidch}
8418255736Sdavidch
8419255736Sdavidchstatic void
8420255736Sdavidchbxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8421255736Sdavidch                       union event_ring_elem *elem)
8422255736Sdavidch{
8423255736Sdavidch    bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8424255736Sdavidch
8425255736Sdavidch    /* send rx_mode command again if was requested */
8426255736Sdavidch    if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8427255736Sdavidch                               &sc->sp_state)) {
8428255736Sdavidch        bxe_set_storm_rx_mode(sc);
8429255736Sdavidch    }
8430255736Sdavidch}
8431255736Sdavidch
8432255736Sdavidchstatic void
8433255736Sdavidchbxe_update_eq_prod(struct bxe_softc *sc,
8434255736Sdavidch                   uint16_t         prod)
8435255736Sdavidch{
8436255736Sdavidch    storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8437255736Sdavidch    wmb(); /* keep prod updates ordered */
8438255736Sdavidch}
8439255736Sdavidch
8440255736Sdavidchstatic void
8441255736Sdavidchbxe_eq_int(struct bxe_softc *sc)
8442255736Sdavidch{
8443255736Sdavidch    uint16_t hw_cons, sw_cons, sw_prod;
8444255736Sdavidch    union event_ring_elem *elem;
8445255736Sdavidch    uint8_t echo;
8446255736Sdavidch    uint32_t cid;
8447255736Sdavidch    uint8_t opcode;
8448255736Sdavidch    int spqe_cnt = 0;
8449255736Sdavidch    struct ecore_queue_sp_obj *q_obj;
8450255736Sdavidch    struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8451255736Sdavidch    struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8452255736Sdavidch
8453255736Sdavidch    hw_cons = le16toh(*sc->eq_cons_sb);
8454255736Sdavidch
8455255736Sdavidch    /*
8456255736Sdavidch     * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8457255736Sdavidch     * when we get to the next-page we need to adjust so the loop
8458255736Sdavidch     * condition below will be met. The next element is the size of a
8459255736Sdavidch     * regular element and hence incrementing by 1
8460255736Sdavidch     */
8461255736Sdavidch    if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8462255736Sdavidch        hw_cons++;
8463255736Sdavidch    }
8464255736Sdavidch
8465255736Sdavidch    /*
8466255736Sdavidch     * This function may never run in parallel with itself for a
8467255736Sdavidch     * specific sc and no need for a read memory barrier here.
8468255736Sdavidch     */
8469255736Sdavidch    sw_cons = sc->eq_cons;
8470255736Sdavidch    sw_prod = sc->eq_prod;
8471255736Sdavidch
8472255736Sdavidch    BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8473255736Sdavidch          hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8474255736Sdavidch
8475255736Sdavidch    for (;
8476255736Sdavidch         sw_cons != hw_cons;
8477255736Sdavidch         sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8478255736Sdavidch
8479255736Sdavidch        elem = &sc->eq[EQ_DESC(sw_cons)];
8480255736Sdavidch
8481255736Sdavidch        /* elem CID originates from FW, actually LE */
8482255736Sdavidch        cid = SW_CID(elem->message.data.cfc_del_event.cid);
8483255736Sdavidch        opcode = elem->message.opcode;
8484255736Sdavidch
8485255736Sdavidch        /* handle eq element */
8486255736Sdavidch        switch (opcode) {
8487255736Sdavidch
8488255736Sdavidch        case EVENT_RING_OPCODE_STAT_QUERY:
8489255736Sdavidch            BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8490255736Sdavidch                  sc->stats_comp++);
8491255736Sdavidch            /* nothing to do with stats comp */
8492255736Sdavidch            goto next_spqe;
8493255736Sdavidch
8494255736Sdavidch        case EVENT_RING_OPCODE_CFC_DEL:
8495255736Sdavidch            /* handle according to cid range */
8496255736Sdavidch            /* we may want to verify here that the sc state is HALTING */
8497255736Sdavidch            BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8498255736Sdavidch            q_obj = bxe_cid_to_q_obj(sc, cid);
8499255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8500255736Sdavidch                break;
8501255736Sdavidch            }
8502255736Sdavidch            goto next_spqe;
8503255736Sdavidch
8504255736Sdavidch        case EVENT_RING_OPCODE_STOP_TRAFFIC:
8505255736Sdavidch            BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8506255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8507255736Sdavidch                break;
8508255736Sdavidch            }
8509255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8510255736Sdavidch            goto next_spqe;
8511255736Sdavidch
8512255736Sdavidch        case EVENT_RING_OPCODE_START_TRAFFIC:
8513255736Sdavidch            BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8514255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8515255736Sdavidch                break;
8516255736Sdavidch            }
8517255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8518255736Sdavidch            goto next_spqe;
8519255736Sdavidch
8520255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8521255736Sdavidch            echo = elem->message.data.function_update_event.echo;
8522255736Sdavidch            if (echo == SWITCH_UPDATE) {
8523255736Sdavidch                BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8524255736Sdavidch                if (f_obj->complete_cmd(sc, f_obj,
8525255736Sdavidch                                        ECORE_F_CMD_SWITCH_UPDATE)) {
8526255736Sdavidch                    break;
8527255736Sdavidch                }
8528255736Sdavidch            }
8529255736Sdavidch            else {
8530255736Sdavidch                BLOGD(sc, DBG_SP,
8531255736Sdavidch                      "AFEX: ramrod completed FUNCTION_UPDATE\n");
8532255736Sdavidch            }
8533255736Sdavidch            goto next_spqe;
8534255736Sdavidch
8535255736Sdavidch        case EVENT_RING_OPCODE_FORWARD_SETUP:
8536255736Sdavidch            q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8537255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj,
8538255736Sdavidch                                    ECORE_Q_CMD_SETUP_TX_ONLY)) {
8539255736Sdavidch                break;
8540255736Sdavidch            }
8541255736Sdavidch            goto next_spqe;
8542255736Sdavidch
8543255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_START:
8544255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8545255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8546255736Sdavidch                break;
8547255736Sdavidch            }
8548255736Sdavidch            goto next_spqe;
8549255736Sdavidch
8550255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_STOP:
8551255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8552255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8553255736Sdavidch                break;
8554255736Sdavidch            }
8555255736Sdavidch            goto next_spqe;
8556255736Sdavidch        }
8557255736Sdavidch
8558255736Sdavidch        switch (opcode | sc->state) {
8559255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8560255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8561255736Sdavidch            cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8562255736Sdavidch            BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8563255736Sdavidch            rss_raw->clear_pending(rss_raw);
8564255736Sdavidch            break;
8565255736Sdavidch
8566255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8567255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8568255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8569255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8570255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8571255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8572255736Sdavidch            BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8573255736Sdavidch            bxe_handle_classification_eqe(sc, elem);
8574255736Sdavidch            break;
8575255736Sdavidch
8576255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8577255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8578255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8579255736Sdavidch            BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8580255736Sdavidch            bxe_handle_mcast_eqe(sc);
8581255736Sdavidch            break;
8582255736Sdavidch
8583255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8584255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8585255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8586255736Sdavidch            BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8587255736Sdavidch            bxe_handle_rx_mode_eqe(sc, elem);
8588255736Sdavidch            break;
8589255736Sdavidch
8590255736Sdavidch        default:
8591255736Sdavidch            /* unknown event log error and continue */
8592255736Sdavidch            BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8593255736Sdavidch                  elem->message.opcode, sc->state);
8594255736Sdavidch        }
8595255736Sdavidch
8596255736Sdavidchnext_spqe:
8597255736Sdavidch        spqe_cnt++;
8598255736Sdavidch    } /* for */
8599255736Sdavidch
8600255736Sdavidch    mb();
8601255736Sdavidch    atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8602255736Sdavidch
8603255736Sdavidch    sc->eq_cons = sw_cons;
8604255736Sdavidch    sc->eq_prod = sw_prod;
8605255736Sdavidch
8606255736Sdavidch    /* make sure that above mem writes were issued towards the memory */
8607255736Sdavidch    wmb();
8608255736Sdavidch
8609255736Sdavidch    /* update producer */
8610255736Sdavidch    bxe_update_eq_prod(sc, sc->eq_prod);
8611255736Sdavidch}
8612255736Sdavidch
8613255736Sdavidchstatic void
8614255736Sdavidchbxe_handle_sp_tq(void *context,
8615255736Sdavidch                 int  pending)
8616255736Sdavidch{
8617255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
8618255736Sdavidch    uint16_t status;
8619255736Sdavidch
8620255736Sdavidch    BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8621255736Sdavidch
8622255736Sdavidch    /* what work needs to be performed? */
8623255736Sdavidch    status = bxe_update_dsb_idx(sc);
8624255736Sdavidch
8625255736Sdavidch    BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8626255736Sdavidch
8627255736Sdavidch    /* HW attentions */
8628255736Sdavidch    if (status & BXE_DEF_SB_ATT_IDX) {
8629255736Sdavidch        BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8630255736Sdavidch        bxe_attn_int(sc);
8631255736Sdavidch        status &= ~BXE_DEF_SB_ATT_IDX;
8632255736Sdavidch    }
8633255736Sdavidch
8634255736Sdavidch    /* SP events: STAT_QUERY and others */
8635255736Sdavidch    if (status & BXE_DEF_SB_IDX) {
8636255736Sdavidch        /* handle EQ completions */
8637255736Sdavidch        BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8638255736Sdavidch        bxe_eq_int(sc);
8639255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8640255736Sdavidch                   le16toh(sc->def_idx), IGU_INT_NOP, 1);
8641255736Sdavidch        status &= ~BXE_DEF_SB_IDX;
8642255736Sdavidch    }
8643255736Sdavidch
8644255736Sdavidch    /* if status is non zero then something went wrong */
8645255736Sdavidch    if (__predict_false(status)) {
8646255736Sdavidch        BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8647255736Sdavidch    }
8648255736Sdavidch
8649255736Sdavidch    /* ack status block only if something was actually handled */
8650255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8651255736Sdavidch               le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8652255736Sdavidch
8653255736Sdavidch    /*
8654255736Sdavidch     * Must be called after the EQ processing (since eq leads to sriov
8655255736Sdavidch     * ramrod completion flows).
8656255736Sdavidch     * This flow may have been scheduled by the arrival of a ramrod
8657255736Sdavidch     * completion, or by the sriov code rescheduling itself.
8658255736Sdavidch     */
8659255736Sdavidch    // XXX bxe_iov_sp_task(sc);
8660255736Sdavidch
8661255736Sdavidch}
8662255736Sdavidch
8663255736Sdavidchstatic void
8664255736Sdavidchbxe_handle_fp_tq(void *context,
8665255736Sdavidch                 int  pending)
8666255736Sdavidch{
8667255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8668255736Sdavidch    struct bxe_softc *sc = fp->sc;
8669255736Sdavidch    uint8_t more_tx = FALSE;
8670255736Sdavidch    uint8_t more_rx = FALSE;
8671255736Sdavidch
8672255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8673255736Sdavidch
8674255736Sdavidch    /* XXX
8675255736Sdavidch     * IFF_DRV_RUNNING state can't be checked here since we process
8676255736Sdavidch     * slowpath events on a client queue during setup. Instead
8677255736Sdavidch     * we need to add a "process/continue" flag here that the driver
8678255736Sdavidch     * can use to tell the task here not to do anything.
8679255736Sdavidch     */
8680255736Sdavidch#if 0
8681266979Smarcel    if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
8682255736Sdavidch        return;
8683255736Sdavidch    }
8684255736Sdavidch#endif
8685255736Sdavidch
8686255736Sdavidch    /* update the fastpath index */
8687255736Sdavidch    bxe_update_fp_sb_idx(fp);
8688255736Sdavidch
8689255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8690255736Sdavidch    /* fp->txdata[cos] */
8691255736Sdavidch    if (bxe_has_tx_work(fp)) {
8692255736Sdavidch        BXE_FP_TX_LOCK(fp);
8693255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8694255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8695255736Sdavidch    }
8696255736Sdavidch
8697255736Sdavidch    if (bxe_has_rx_work(fp)) {
8698255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8699255736Sdavidch    }
8700255736Sdavidch
8701255736Sdavidch    if (more_rx /*|| more_tx*/) {
8702255736Sdavidch        /* still more work to do */
8703296272Sjhb        taskqueue_enqueue(fp->tq, &fp->tq_task);
8704255736Sdavidch        return;
8705255736Sdavidch    }
8706255736Sdavidch
8707255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8708255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8709255736Sdavidch}
8710255736Sdavidch
8711255736Sdavidchstatic void
8712255736Sdavidchbxe_task_fp(struct bxe_fastpath *fp)
8713255736Sdavidch{
8714255736Sdavidch    struct bxe_softc *sc = fp->sc;
8715255736Sdavidch    uint8_t more_tx = FALSE;
8716255736Sdavidch    uint8_t more_rx = FALSE;
8717255736Sdavidch
8718255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8719255736Sdavidch
8720255736Sdavidch    /* update the fastpath index */
8721255736Sdavidch    bxe_update_fp_sb_idx(fp);
8722255736Sdavidch
8723255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8724255736Sdavidch    /* fp->txdata[cos] */
8725255736Sdavidch    if (bxe_has_tx_work(fp)) {
8726255736Sdavidch        BXE_FP_TX_LOCK(fp);
8727255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8728255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8729255736Sdavidch    }
8730255736Sdavidch
8731255736Sdavidch    if (bxe_has_rx_work(fp)) {
8732255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8733255736Sdavidch    }
8734255736Sdavidch
8735255736Sdavidch    if (more_rx /*|| more_tx*/) {
8736255736Sdavidch        /* still more work to do, bail out if this ISR and process later */
8737296272Sjhb        taskqueue_enqueue(fp->tq, &fp->tq_task);
8738255736Sdavidch        return;
8739255736Sdavidch    }
8740255736Sdavidch
8741255736Sdavidch    /*
8742255736Sdavidch     * Here we write the fastpath index taken before doing any tx or rx work.
8743255736Sdavidch     * It is very well possible other hw events occurred up to this point and
8744255736Sdavidch     * they were actually processed accordingly above. Since we're going to
8745255736Sdavidch     * write an older fastpath index, an interrupt is coming which we might
8746255736Sdavidch     * not do any work in.
8747255736Sdavidch     */
8748255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8749255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8750255736Sdavidch}
8751255736Sdavidch
8752255736Sdavidch/*
8753255736Sdavidch * Legacy interrupt entry point.
8754255736Sdavidch *
8755255736Sdavidch * Verifies that the controller generated the interrupt and
8756255736Sdavidch * then calls a separate routine to handle the various
8757255736Sdavidch * interrupt causes: link, RX, and TX.
8758255736Sdavidch */
8759255736Sdavidchstatic void
8760255736Sdavidchbxe_intr_legacy(void *xsc)
8761255736Sdavidch{
8762255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8763255736Sdavidch    struct bxe_fastpath *fp;
8764255736Sdavidch    uint16_t status, mask;
8765255736Sdavidch    int i;
8766255736Sdavidch
8767255736Sdavidch    BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8768255736Sdavidch
8769255736Sdavidch    /*
8770255736Sdavidch     * 0 for ustorm, 1 for cstorm
8771255736Sdavidch     * the bits returned from ack_int() are 0-15
8772255736Sdavidch     * bit 0 = attention status block
8773255736Sdavidch     * bit 1 = fast path status block
8774255736Sdavidch     * a mask of 0x2 or more = tx/rx event
8775255736Sdavidch     * a mask of 1 = slow path event
8776255736Sdavidch     */
8777255736Sdavidch
8778255736Sdavidch    status = bxe_ack_int(sc);
8779255736Sdavidch
8780255736Sdavidch    /* the interrupt is not for us */
8781255736Sdavidch    if (__predict_false(status == 0)) {
8782255736Sdavidch        BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8783255736Sdavidch        return;
8784255736Sdavidch    }
8785255736Sdavidch
8786255736Sdavidch    BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8787255736Sdavidch
8788255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
8789255736Sdavidch        fp = &sc->fp[i];
8790255736Sdavidch        mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8791255736Sdavidch        if (status & mask) {
8792255736Sdavidch            /* acknowledge and disable further fastpath interrupts */
8793255736Sdavidch            bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8794255736Sdavidch            bxe_task_fp(fp);
8795255736Sdavidch            status &= ~mask;
8796255736Sdavidch        }
8797255736Sdavidch    }
8798255736Sdavidch
8799255736Sdavidch    if (__predict_false(status & 0x1)) {
8800255736Sdavidch        /* acknowledge and disable further slowpath interrupts */
8801255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8802255736Sdavidch
8803255736Sdavidch        /* schedule slowpath handler */
8804296272Sjhb        taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8805255736Sdavidch
8806255736Sdavidch        status &= ~0x1;
8807255736Sdavidch    }
8808255736Sdavidch
8809255736Sdavidch    if (__predict_false(status)) {
8810255736Sdavidch        BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8811255736Sdavidch    }
8812255736Sdavidch}
8813255736Sdavidch
8814255736Sdavidch/* slowpath interrupt entry point */
8815255736Sdavidchstatic void
8816255736Sdavidchbxe_intr_sp(void *xsc)
8817255736Sdavidch{
8818255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8819255736Sdavidch
8820255736Sdavidch    BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8821255736Sdavidch
8822255736Sdavidch    /* acknowledge and disable further slowpath interrupts */
8823255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8824255736Sdavidch
8825255736Sdavidch    /* schedule slowpath handler */
8826296272Sjhb    taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8827255736Sdavidch}
8828255736Sdavidch
8829255736Sdavidch/* fastpath interrupt entry point */
8830255736Sdavidchstatic void
8831255736Sdavidchbxe_intr_fp(void *xfp)
8832255736Sdavidch{
8833255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8834255736Sdavidch    struct bxe_softc *sc = fp->sc;
8835255736Sdavidch
8836255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8837255736Sdavidch
8838255736Sdavidch    BLOGD(sc, DBG_INTR,
8839255736Sdavidch          "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8840255736Sdavidch          curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8841255736Sdavidch
8842255736Sdavidch    /* acknowledge and disable further fastpath interrupts */
8843255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8844255736Sdavidch
8845255736Sdavidch    bxe_task_fp(fp);
8846255736Sdavidch}
8847255736Sdavidch
8848255736Sdavidch/* Release all interrupts allocated by the driver. */
8849255736Sdavidchstatic void
8850255736Sdavidchbxe_interrupt_free(struct bxe_softc *sc)
8851255736Sdavidch{
8852255736Sdavidch    int i;
8853255736Sdavidch
8854255736Sdavidch    switch (sc->interrupt_mode) {
8855255736Sdavidch    case INTR_MODE_INTX:
8856255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8857255736Sdavidch        if (sc->intr[0].resource != NULL) {
8858255736Sdavidch            bus_release_resource(sc->dev,
8859255736Sdavidch                                 SYS_RES_IRQ,
8860255736Sdavidch                                 sc->intr[0].rid,
8861255736Sdavidch                                 sc->intr[0].resource);
8862255736Sdavidch        }
8863255736Sdavidch        break;
8864255736Sdavidch    case INTR_MODE_MSI:
8865255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8866255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
8867255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8868255736Sdavidch                bus_release_resource(sc->dev,
8869255736Sdavidch                                     SYS_RES_IRQ,
8870255736Sdavidch                                     sc->intr[i].rid,
8871255736Sdavidch                                     sc->intr[i].resource);
8872255736Sdavidch            }
8873255736Sdavidch        }
8874255736Sdavidch        pci_release_msi(sc->dev);
8875255736Sdavidch        break;
8876255736Sdavidch    case INTR_MODE_MSIX:
8877255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8878255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8879255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8880255736Sdavidch                bus_release_resource(sc->dev,
8881255736Sdavidch                                     SYS_RES_IRQ,
8882255736Sdavidch                                     sc->intr[i].rid,
8883255736Sdavidch                                     sc->intr[i].resource);
8884255736Sdavidch            }
8885255736Sdavidch        }
8886255736Sdavidch        pci_release_msi(sc->dev);
8887255736Sdavidch        break;
8888255736Sdavidch    default:
8889255736Sdavidch        /* nothing to do as initial allocation failed */
8890255736Sdavidch        break;
8891255736Sdavidch    }
8892255736Sdavidch}
8893255736Sdavidch
8894255736Sdavidch/*
8895255736Sdavidch * This function determines and allocates the appropriate
8896255736Sdavidch * interrupt based on system capabilites and user request.
8897255736Sdavidch *
8898255736Sdavidch * The user may force a particular interrupt mode, specify
8899255736Sdavidch * the number of receive queues, specify the method for
8900255736Sdavidch * distribuitng received frames to receive queues, or use
8901255736Sdavidch * the default settings which will automatically select the
8902255736Sdavidch * best supported combination.  In addition, the OS may or
8903255736Sdavidch * may not support certain combinations of these settings.
8904255736Sdavidch * This routine attempts to reconcile the settings requested
8905255736Sdavidch * by the user with the capabilites available from the system
8906255736Sdavidch * to select the optimal combination of features.
8907255736Sdavidch *
8908255736Sdavidch * Returns:
8909255736Sdavidch *   0 = Success, !0 = Failure.
8910255736Sdavidch */
8911255736Sdavidchstatic int
8912255736Sdavidchbxe_interrupt_alloc(struct bxe_softc *sc)
8913255736Sdavidch{
8914255736Sdavidch    int msix_count = 0;
8915255736Sdavidch    int msi_count = 0;
8916255736Sdavidch    int num_requested = 0;
8917255736Sdavidch    int num_allocated = 0;
8918255736Sdavidch    int rid, i, j;
8919255736Sdavidch    int rc;
8920255736Sdavidch
8921255736Sdavidch    /* get the number of available MSI/MSI-X interrupts from the OS */
8922255736Sdavidch    if (sc->interrupt_mode > 0) {
8923255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8924255736Sdavidch            msix_count = pci_msix_count(sc->dev);
8925255736Sdavidch        }
8926255736Sdavidch
8927255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
8928255736Sdavidch            msi_count = pci_msi_count(sc->dev);
8929255736Sdavidch        }
8930255736Sdavidch
8931255736Sdavidch        BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
8932255736Sdavidch              msi_count, msix_count);
8933255736Sdavidch    }
8934255736Sdavidch
8935255736Sdavidch    do { /* try allocating MSI-X interrupt resources (at least 2) */
8936255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
8937255736Sdavidch            break;
8938255736Sdavidch        }
8939255736Sdavidch
8940255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
8941255736Sdavidch            (msix_count < 2)) {
8942255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8943255736Sdavidch            break;
8944255736Sdavidch        }
8945255736Sdavidch
8946255736Sdavidch        /* ask for the necessary number of MSI-X vectors */
8947255736Sdavidch        num_requested = min((sc->num_queues + 1), msix_count);
8948255736Sdavidch
8949255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
8950255736Sdavidch
8951255736Sdavidch        num_allocated = num_requested;
8952255736Sdavidch        if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
8953255736Sdavidch            BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
8954255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8955255736Sdavidch            break;
8956255736Sdavidch        }
8957255736Sdavidch
8958255736Sdavidch        if (num_allocated < 2) { /* possible? */
8959255736Sdavidch            BLOGE(sc, "MSI-X allocation less than 2!\n");
8960255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8961255736Sdavidch            pci_release_msi(sc->dev);
8962255736Sdavidch            break;
8963255736Sdavidch        }
8964255736Sdavidch
8965255736Sdavidch        BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
8966255736Sdavidch              num_requested, num_allocated);
8967255736Sdavidch
8968255736Sdavidch        /* best effort so use the number of vectors allocated to us */
8969255736Sdavidch        sc->intr_count = num_allocated;
8970255736Sdavidch        sc->num_queues = num_allocated - 1;
8971255736Sdavidch
8972255736Sdavidch        rid = 1; /* initial resource identifier */
8973255736Sdavidch
8974255736Sdavidch        /* allocate the MSI-X vectors */
8975255736Sdavidch        for (i = 0; i < num_allocated; i++) {
8976255736Sdavidch            sc->intr[i].rid = (rid + i);
8977255736Sdavidch
8978255736Sdavidch            if ((sc->intr[i].resource =
8979255736Sdavidch                 bus_alloc_resource_any(sc->dev,
8980255736Sdavidch                                        SYS_RES_IRQ,
8981255736Sdavidch                                        &sc->intr[i].rid,
8982255736Sdavidch                                        RF_ACTIVE)) == NULL) {
8983255736Sdavidch                BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
8984255736Sdavidch                      i, (rid + i));
8985255736Sdavidch
8986255736Sdavidch                for (j = (i - 1); j >= 0; j--) {
8987255736Sdavidch                    bus_release_resource(sc->dev,
8988255736Sdavidch                                         SYS_RES_IRQ,
8989255736Sdavidch                                         sc->intr[j].rid,
8990255736Sdavidch                                         sc->intr[j].resource);
8991255736Sdavidch                }
8992255736Sdavidch
8993255736Sdavidch                sc->intr_count = 0;
8994255736Sdavidch                sc->num_queues = 0;
8995255736Sdavidch                sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8996255736Sdavidch                pci_release_msi(sc->dev);
8997255736Sdavidch                break;
8998255736Sdavidch            }
8999255736Sdavidch
9000255736Sdavidch            BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9001255736Sdavidch        }
9002255736Sdavidch    } while (0);
9003255736Sdavidch
9004255736Sdavidch    do { /* try allocating MSI vector resources (at least 2) */
9005255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSI) {
9006255736Sdavidch            break;
9007255736Sdavidch        }
9008255736Sdavidch
9009255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9010262999Sedavis            (msi_count < 1)) {
9011255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9012255736Sdavidch            break;
9013255736Sdavidch        }
9014255736Sdavidch
9015262999Sedavis        /* ask for a single MSI vector */
9016262999Sedavis        num_requested = 1;
9017255736Sdavidch
9018255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9019255736Sdavidch
9020255736Sdavidch        num_allocated = num_requested;
9021255736Sdavidch        if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9022255736Sdavidch            BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9023255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9024255736Sdavidch            break;
9025255736Sdavidch        }
9026255736Sdavidch
9027262999Sedavis        if (num_allocated != 1) { /* possible? */
9028262999Sedavis            BLOGE(sc, "MSI allocation is not 1!\n");
9029255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9030255736Sdavidch            pci_release_msi(sc->dev);
9031255736Sdavidch            break;
9032255736Sdavidch        }
9033255736Sdavidch
9034255736Sdavidch        BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9035255736Sdavidch              num_requested, num_allocated);
9036255736Sdavidch
9037255736Sdavidch        /* best effort so use the number of vectors allocated to us */
9038255736Sdavidch        sc->intr_count = num_allocated;
9039262999Sedavis        sc->num_queues = num_allocated;
9040255736Sdavidch
9041255736Sdavidch        rid = 1; /* initial resource identifier */
9042255736Sdavidch
9043262999Sedavis        sc->intr[0].rid = rid;
9044255736Sdavidch
9045262999Sedavis        if ((sc->intr[0].resource =
9046262999Sedavis             bus_alloc_resource_any(sc->dev,
9047262999Sedavis                                    SYS_RES_IRQ,
9048262999Sedavis                                    &sc->intr[0].rid,
9049262999Sedavis                                    RF_ACTIVE)) == NULL) {
9050262999Sedavis            BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9051262999Sedavis            sc->intr_count = 0;
9052262999Sedavis            sc->num_queues = 0;
9053262999Sedavis            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9054262999Sedavis            pci_release_msi(sc->dev);
9055262999Sedavis            break;
9056262999Sedavis        }
9057255736Sdavidch
9058262999Sedavis        BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9059255736Sdavidch    } while (0);
9060255736Sdavidch
9061255736Sdavidch    do { /* try allocating INTx vector resources */
9062255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_INTX) {
9063255736Sdavidch            break;
9064255736Sdavidch        }
9065255736Sdavidch
9066255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9067255736Sdavidch
9068255736Sdavidch        /* only one vector for INTx */
9069255736Sdavidch        sc->intr_count = 1;
9070255736Sdavidch        sc->num_queues = 1;
9071255736Sdavidch
9072255736Sdavidch        rid = 0; /* initial resource identifier */
9073255736Sdavidch
9074255736Sdavidch        sc->intr[0].rid = rid;
9075255736Sdavidch
9076255736Sdavidch        if ((sc->intr[0].resource =
9077255736Sdavidch             bus_alloc_resource_any(sc->dev,
9078255736Sdavidch                                    SYS_RES_IRQ,
9079255736Sdavidch                                    &sc->intr[0].rid,
9080255736Sdavidch                                    (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9081255736Sdavidch            BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9082255736Sdavidch            sc->intr_count = 0;
9083255736Sdavidch            sc->num_queues = 0;
9084255736Sdavidch            sc->interrupt_mode = -1; /* Failed! */
9085255736Sdavidch            break;
9086255736Sdavidch        }
9087255736Sdavidch
9088255736Sdavidch        BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9089255736Sdavidch    } while (0);
9090255736Sdavidch
9091255736Sdavidch    if (sc->interrupt_mode == -1) {
9092255736Sdavidch        BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9093255736Sdavidch        rc = 1;
9094255736Sdavidch    } else {
9095255736Sdavidch        BLOGD(sc, DBG_LOAD,
9096255736Sdavidch              "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9097255736Sdavidch              sc->interrupt_mode, sc->num_queues);
9098255736Sdavidch        rc = 0;
9099255736Sdavidch    }
9100255736Sdavidch
9101255736Sdavidch    return (rc);
9102255736Sdavidch}
9103255736Sdavidch
9104255736Sdavidchstatic void
9105255736Sdavidchbxe_interrupt_detach(struct bxe_softc *sc)
9106255736Sdavidch{
9107255736Sdavidch    struct bxe_fastpath *fp;
9108255736Sdavidch    int i;
9109255736Sdavidch
9110255736Sdavidch    /* release interrupt resources */
9111255736Sdavidch    for (i = 0; i < sc->intr_count; i++) {
9112255736Sdavidch        if (sc->intr[i].resource && sc->intr[i].tag) {
9113255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9114255736Sdavidch            bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9115255736Sdavidch        }
9116255736Sdavidch    }
9117255736Sdavidch
9118255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9119255736Sdavidch        fp = &sc->fp[i];
9120255736Sdavidch        if (fp->tq) {
9121255736Sdavidch            taskqueue_drain(fp->tq, &fp->tq_task);
9122307972Sdavidcs            taskqueue_drain(fp->tq, &fp->tx_task);
9123307972Sdavidcs            while (taskqueue_cancel_timeout(fp->tq, &fp->tx_timeout_task,
9124307972Sdavidcs                NULL))
9125307972Sdavidcs                taskqueue_drain_timeout(fp->tq, &fp->tx_timeout_task);
9126255736Sdavidch            taskqueue_free(fp->tq);
9127255736Sdavidch            fp->tq = NULL;
9128255736Sdavidch        }
9129255736Sdavidch    }
9130255736Sdavidch
9131255736Sdavidch
9132255736Sdavidch    if (sc->sp_tq) {
9133255736Sdavidch        taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9134255736Sdavidch        taskqueue_free(sc->sp_tq);
9135255736Sdavidch        sc->sp_tq = NULL;
9136255736Sdavidch    }
9137255736Sdavidch}
9138255736Sdavidch
9139255736Sdavidch/*
9140255736Sdavidch * Enables interrupts and attach to the ISR.
9141255736Sdavidch *
9142255736Sdavidch * When using multiple MSI/MSI-X vectors the first vector
9143255736Sdavidch * is used for slowpath operations while all remaining
9144255736Sdavidch * vectors are used for fastpath operations.  If only a
9145255736Sdavidch * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9146255736Sdavidch * ISR must look for both slowpath and fastpath completions.
9147255736Sdavidch */
9148255736Sdavidchstatic int
9149255736Sdavidchbxe_interrupt_attach(struct bxe_softc *sc)
9150255736Sdavidch{
9151255736Sdavidch    struct bxe_fastpath *fp;
9152255736Sdavidch    int rc = 0;
9153255736Sdavidch    int i;
9154255736Sdavidch
9155255736Sdavidch    snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9156255736Sdavidch             "bxe%d_sp_tq", sc->unit);
9157255736Sdavidch    TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9158307972Sdavidcs    sc->sp_tq = taskqueue_create(sc->sp_tq_name, M_NOWAIT,
9159307972Sdavidcs                                 taskqueue_thread_enqueue,
9160307972Sdavidcs                                 &sc->sp_tq);
9161255736Sdavidch    taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9162255736Sdavidch                            "%s", sc->sp_tq_name);
9163255736Sdavidch
9164255736Sdavidch
9165255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9166255736Sdavidch        fp = &sc->fp[i];
9167255736Sdavidch        snprintf(fp->tq_name, sizeof(fp->tq_name),
9168255736Sdavidch                 "bxe%d_fp%d_tq", sc->unit, i);
9169255736Sdavidch        TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9170307972Sdavidcs        TASK_INIT(&fp->tx_task, 0, bxe_tx_mq_start_deferred, fp);
9171307972Sdavidcs        fp->tq = taskqueue_create(fp->tq_name, M_NOWAIT,
9172307972Sdavidcs                                  taskqueue_thread_enqueue,
9173307972Sdavidcs                                  &fp->tq);
9174307972Sdavidcs        TIMEOUT_TASK_INIT(fp->tq, &fp->tx_timeout_task, 0,
9175307972Sdavidcs                          bxe_tx_mq_start_deferred, fp);
9176255736Sdavidch        taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9177255736Sdavidch                                "%s", fp->tq_name);
9178255736Sdavidch    }
9179255736Sdavidch
9180255736Sdavidch    /* setup interrupt handlers */
9181255736Sdavidch    if (sc->interrupt_mode == INTR_MODE_MSIX) {
9182255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9183255736Sdavidch
9184255736Sdavidch        /*
9185255736Sdavidch         * Setup the interrupt handler. Note that we pass the driver instance
9186255736Sdavidch         * to the interrupt handler for the slowpath.
9187255736Sdavidch         */
9188255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9189255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9190255736Sdavidch                                 NULL, bxe_intr_sp, sc,
9191255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9192255736Sdavidch            BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9193255736Sdavidch            goto bxe_interrupt_attach_exit;
9194255736Sdavidch        }
9195255736Sdavidch
9196255736Sdavidch        bus_describe_intr(sc->dev, sc->intr[0].resource,
9197255736Sdavidch                          sc->intr[0].tag, "sp");
9198255736Sdavidch
9199255736Sdavidch        /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9200255736Sdavidch
9201255736Sdavidch        /* initialize the fastpath vectors (note the first was used for sp) */
9202255736Sdavidch        for (i = 0; i < sc->num_queues; i++) {
9203255736Sdavidch            fp = &sc->fp[i];
9204255736Sdavidch            BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9205255736Sdavidch
9206255736Sdavidch            /*
9207255736Sdavidch             * Setup the interrupt handler. Note that we pass the
9208255736Sdavidch             * fastpath context to the interrupt handler in this
9209255736Sdavidch             * case.
9210255736Sdavidch             */
9211255736Sdavidch            if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9212255736Sdavidch                                     (INTR_TYPE_NET | INTR_MPSAFE),
9213255736Sdavidch                                     NULL, bxe_intr_fp, fp,
9214255736Sdavidch                                     &sc->intr[i + 1].tag)) != 0) {
9215255736Sdavidch                BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9216255736Sdavidch                      (i + 1), rc);
9217255736Sdavidch                goto bxe_interrupt_attach_exit;
9218255736Sdavidch            }
9219255736Sdavidch
9220255736Sdavidch            bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9221255736Sdavidch                              sc->intr[i + 1].tag, "fp%02d", i);
9222255736Sdavidch
9223255736Sdavidch            /* bind the fastpath instance to a cpu */
9224255736Sdavidch            if (sc->num_queues > 1) {
9225255736Sdavidch                bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9226255736Sdavidch            }
9227255736Sdavidch
9228255736Sdavidch            fp->state = BXE_FP_STATE_IRQ;
9229255736Sdavidch        }
9230255736Sdavidch    } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9231262999Sedavis        BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9232255736Sdavidch
9233255736Sdavidch        /*
9234262999Sedavis         * Setup the interrupt handler. Note that we pass the
9235262999Sedavis         * driver instance to the interrupt handler which
9236262999Sedavis         * will handle both the slowpath and fastpath.
9237255736Sdavidch         */
9238255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9239255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9240262999Sedavis                                 NULL, bxe_intr_legacy, sc,
9241255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9242255736Sdavidch            BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9243255736Sdavidch            goto bxe_interrupt_attach_exit;
9244255736Sdavidch        }
9245255736Sdavidch
9246255736Sdavidch    } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9247255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9248255736Sdavidch
9249255736Sdavidch        /*
9250255736Sdavidch         * Setup the interrupt handler. Note that we pass the
9251255736Sdavidch         * driver instance to the interrupt handler which
9252255736Sdavidch         * will handle both the slowpath and fastpath.
9253255736Sdavidch         */
9254255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9255255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9256255736Sdavidch                                 NULL, bxe_intr_legacy, sc,
9257255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9258255736Sdavidch            BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9259255736Sdavidch            goto bxe_interrupt_attach_exit;
9260255736Sdavidch        }
9261255736Sdavidch    }
9262255736Sdavidch
9263255736Sdavidchbxe_interrupt_attach_exit:
9264255736Sdavidch
9265255736Sdavidch    return (rc);
9266255736Sdavidch}
9267255736Sdavidch
9268255736Sdavidchstatic int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9269255736Sdavidchstatic int  bxe_init_hw_common(struct bxe_softc *sc);
9270255736Sdavidchstatic int  bxe_init_hw_port(struct bxe_softc *sc);
9271255736Sdavidchstatic int  bxe_init_hw_func(struct bxe_softc *sc);
9272255736Sdavidchstatic void bxe_reset_common(struct bxe_softc *sc);
9273255736Sdavidchstatic void bxe_reset_port(struct bxe_softc *sc);
9274255736Sdavidchstatic void bxe_reset_func(struct bxe_softc *sc);
9275255736Sdavidchstatic int  bxe_gunzip_init(struct bxe_softc *sc);
9276255736Sdavidchstatic void bxe_gunzip_end(struct bxe_softc *sc);
9277255736Sdavidchstatic int  bxe_init_firmware(struct bxe_softc *sc);
9278255736Sdavidchstatic void bxe_release_firmware(struct bxe_softc *sc);
9279255736Sdavidch
9280255736Sdavidchstatic struct
9281255736Sdavidchecore_func_sp_drv_ops bxe_func_sp_drv = {
9282255736Sdavidch    .init_hw_cmn_chip = bxe_init_hw_common_chip,
9283255736Sdavidch    .init_hw_cmn      = bxe_init_hw_common,
9284255736Sdavidch    .init_hw_port     = bxe_init_hw_port,
9285255736Sdavidch    .init_hw_func     = bxe_init_hw_func,
9286255736Sdavidch
9287255736Sdavidch    .reset_hw_cmn     = bxe_reset_common,
9288255736Sdavidch    .reset_hw_port    = bxe_reset_port,
9289255736Sdavidch    .reset_hw_func    = bxe_reset_func,
9290255736Sdavidch
9291255736Sdavidch    .gunzip_init      = bxe_gunzip_init,
9292255736Sdavidch    .gunzip_end       = bxe_gunzip_end,
9293255736Sdavidch
9294255736Sdavidch    .init_fw          = bxe_init_firmware,
9295255736Sdavidch    .release_fw       = bxe_release_firmware,
9296255736Sdavidch};
9297255736Sdavidch
9298255736Sdavidchstatic void
9299255736Sdavidchbxe_init_func_obj(struct bxe_softc *sc)
9300255736Sdavidch{
9301255736Sdavidch    sc->dmae_ready = 0;
9302255736Sdavidch
9303255736Sdavidch    ecore_init_func_obj(sc,
9304255736Sdavidch                        &sc->func_obj,
9305255736Sdavidch                        BXE_SP(sc, func_rdata),
9306255736Sdavidch                        BXE_SP_MAPPING(sc, func_rdata),
9307255736Sdavidch                        BXE_SP(sc, func_afex_rdata),
9308255736Sdavidch                        BXE_SP_MAPPING(sc, func_afex_rdata),
9309255736Sdavidch                        &bxe_func_sp_drv);
9310255736Sdavidch}
9311255736Sdavidch
9312255736Sdavidchstatic int
9313255736Sdavidchbxe_init_hw(struct bxe_softc *sc,
9314255736Sdavidch            uint32_t         load_code)
9315255736Sdavidch{
9316255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
9317255736Sdavidch    int rc;
9318255736Sdavidch
9319255736Sdavidch    /* prepare the parameters for function state transitions */
9320255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9321255736Sdavidch
9322255736Sdavidch    func_params.f_obj = &sc->func_obj;
9323255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_INIT;
9324255736Sdavidch
9325255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
9326255736Sdavidch
9327255736Sdavidch    /*
9328255736Sdavidch     * Via a plethora of function pointers, we will eventually reach
9329255736Sdavidch     * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9330255736Sdavidch     */
9331255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
9332255736Sdavidch
9333255736Sdavidch    return (rc);
9334255736Sdavidch}
9335255736Sdavidch
9336255736Sdavidchstatic void
9337255736Sdavidchbxe_fill(struct bxe_softc *sc,
9338255736Sdavidch         uint32_t         addr,
9339255736Sdavidch         int              fill,
9340255736Sdavidch         uint32_t         len)
9341255736Sdavidch{
9342255736Sdavidch    uint32_t i;
9343255736Sdavidch
9344255736Sdavidch    if (!(len % 4) && !(addr % 4)) {
9345255736Sdavidch        for (i = 0; i < len; i += 4) {
9346255736Sdavidch            REG_WR(sc, (addr + i), fill);
9347255736Sdavidch        }
9348255736Sdavidch    } else {
9349255736Sdavidch        for (i = 0; i < len; i++) {
9350255736Sdavidch            REG_WR8(sc, (addr + i), fill);
9351255736Sdavidch        }
9352255736Sdavidch    }
9353255736Sdavidch}
9354255736Sdavidch
9355255736Sdavidch/* writes FP SP data to FW - data_size in dwords */
9356255736Sdavidchstatic void
9357255736Sdavidchbxe_wr_fp_sb_data(struct bxe_softc *sc,
9358255736Sdavidch                  int              fw_sb_id,
9359255736Sdavidch                  uint32_t         *sb_data_p,
9360255736Sdavidch                  uint32_t         data_size)
9361255736Sdavidch{
9362255736Sdavidch    int index;
9363255736Sdavidch
9364255736Sdavidch    for (index = 0; index < data_size; index++) {
9365255736Sdavidch        REG_WR(sc,
9366255736Sdavidch               (BAR_CSTRORM_INTMEM +
9367255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9368255736Sdavidch                (sizeof(uint32_t) * index)),
9369255736Sdavidch               *(sb_data_p + index));
9370255736Sdavidch    }
9371255736Sdavidch}
9372255736Sdavidch
9373255736Sdavidchstatic void
9374255736Sdavidchbxe_zero_fp_sb(struct bxe_softc *sc,
9375255736Sdavidch               int              fw_sb_id)
9376255736Sdavidch{
9377255736Sdavidch    struct hc_status_block_data_e2 sb_data_e2;
9378255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9379255736Sdavidch    uint32_t *sb_data_p;
9380255736Sdavidch    uint32_t data_size = 0;
9381255736Sdavidch
9382255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9383255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9384255736Sdavidch        sb_data_e2.common.state = SB_DISABLED;
9385255736Sdavidch        sb_data_e2.common.p_func.vf_valid = FALSE;
9386255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9387255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9388255736Sdavidch                     sizeof(uint32_t));
9389255736Sdavidch    } else {
9390255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9391255736Sdavidch        sb_data_e1x.common.state = SB_DISABLED;
9392255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9393255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9394255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9395255736Sdavidch                     sizeof(uint32_t));
9396255736Sdavidch    }
9397255736Sdavidch
9398255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9399255736Sdavidch
9400255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9401255736Sdavidch             0, CSTORM_STATUS_BLOCK_SIZE);
9402255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9403255736Sdavidch             0, CSTORM_SYNC_BLOCK_SIZE);
9404255736Sdavidch}
9405255736Sdavidch
9406255736Sdavidchstatic void
9407255736Sdavidchbxe_wr_sp_sb_data(struct bxe_softc               *sc,
9408255736Sdavidch                  struct hc_sp_status_block_data *sp_sb_data)
9409255736Sdavidch{
9410255736Sdavidch    int i;
9411255736Sdavidch
9412255736Sdavidch    for (i = 0;
9413255736Sdavidch         i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9414255736Sdavidch         i++) {
9415255736Sdavidch        REG_WR(sc,
9416255736Sdavidch               (BAR_CSTRORM_INTMEM +
9417255736Sdavidch                CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9418255736Sdavidch                (i * sizeof(uint32_t))),
9419255736Sdavidch               *((uint32_t *)sp_sb_data + i));
9420255736Sdavidch    }
9421255736Sdavidch}
9422255736Sdavidch
9423255736Sdavidchstatic void
9424255736Sdavidchbxe_zero_sp_sb(struct bxe_softc *sc)
9425255736Sdavidch{
9426255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9427255736Sdavidch
9428255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9429255736Sdavidch
9430255736Sdavidch    sp_sb_data.state           = SB_DISABLED;
9431255736Sdavidch    sp_sb_data.p_func.vf_valid = FALSE;
9432255736Sdavidch
9433255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9434255736Sdavidch
9435255736Sdavidch    bxe_fill(sc,
9436255736Sdavidch             (BAR_CSTRORM_INTMEM +
9437255736Sdavidch              CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9438255736Sdavidch              0, CSTORM_SP_STATUS_BLOCK_SIZE);
9439255736Sdavidch    bxe_fill(sc,
9440255736Sdavidch             (BAR_CSTRORM_INTMEM +
9441255736Sdavidch              CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9442255736Sdavidch              0, CSTORM_SP_SYNC_BLOCK_SIZE);
9443255736Sdavidch}
9444255736Sdavidch
9445255736Sdavidchstatic void
9446255736Sdavidchbxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9447255736Sdavidch                             int                       igu_sb_id,
9448255736Sdavidch                             int                       igu_seg_id)
9449255736Sdavidch{
9450255736Sdavidch    hc_sm->igu_sb_id      = igu_sb_id;
9451255736Sdavidch    hc_sm->igu_seg_id     = igu_seg_id;
9452255736Sdavidch    hc_sm->timer_value    = 0xFF;
9453255736Sdavidch    hc_sm->time_to_expire = 0xFFFFFFFF;
9454255736Sdavidch}
9455255736Sdavidch
9456255736Sdavidchstatic void
9457255736Sdavidchbxe_map_sb_state_machines(struct hc_index_data *index_data)
9458255736Sdavidch{
9459255736Sdavidch    /* zero out state machine indices */
9460255736Sdavidch
9461255736Sdavidch    /* rx indices */
9462255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9463255736Sdavidch
9464255736Sdavidch    /* tx indices */
9465255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9466255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9467255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9468255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9469255736Sdavidch
9470255736Sdavidch    /* map indices */
9471255736Sdavidch
9472255736Sdavidch    /* rx indices */
9473255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9474255736Sdavidch        (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9475255736Sdavidch
9476255736Sdavidch    /* tx indices */
9477255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9478255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9479255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9480255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9481255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9482255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9483255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9484255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9485255736Sdavidch}
9486255736Sdavidch
9487255736Sdavidchstatic void
9488255736Sdavidchbxe_init_sb(struct bxe_softc *sc,
9489255736Sdavidch            bus_addr_t       busaddr,
9490255736Sdavidch            int              vfid,
9491255736Sdavidch            uint8_t          vf_valid,
9492255736Sdavidch            int              fw_sb_id,
9493255736Sdavidch            int              igu_sb_id)
9494255736Sdavidch{
9495255736Sdavidch    struct hc_status_block_data_e2  sb_data_e2;
9496255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9497255736Sdavidch    struct hc_status_block_sm       *hc_sm_p;
9498255736Sdavidch    uint32_t *sb_data_p;
9499255736Sdavidch    int igu_seg_id;
9500255736Sdavidch    int data_size;
9501255736Sdavidch
9502255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9503255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_NORM;
9504255736Sdavidch    } else {
9505255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_NORM;
9506255736Sdavidch    }
9507255736Sdavidch
9508255736Sdavidch    bxe_zero_fp_sb(sc, fw_sb_id);
9509255736Sdavidch
9510255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9511255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9512255736Sdavidch        sb_data_e2.common.state = SB_ENABLED;
9513255736Sdavidch        sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9514255736Sdavidch        sb_data_e2.common.p_func.vf_id = vfid;
9515255736Sdavidch        sb_data_e2.common.p_func.vf_valid = vf_valid;
9516255736Sdavidch        sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9517255736Sdavidch        sb_data_e2.common.same_igu_sb_1b = TRUE;
9518255736Sdavidch        sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9519255736Sdavidch        sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9520255736Sdavidch        hc_sm_p = sb_data_e2.common.state_machine;
9521255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9522255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9523255736Sdavidch                     sizeof(uint32_t));
9524255736Sdavidch        bxe_map_sb_state_machines(sb_data_e2.index_data);
9525255736Sdavidch    } else {
9526255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9527255736Sdavidch        sb_data_e1x.common.state = SB_ENABLED;
9528255736Sdavidch        sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9529255736Sdavidch        sb_data_e1x.common.p_func.vf_id = 0xff;
9530255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9531255736Sdavidch        sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9532255736Sdavidch        sb_data_e1x.common.same_igu_sb_1b = TRUE;
9533255736Sdavidch        sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9534255736Sdavidch        sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9535255736Sdavidch        hc_sm_p = sb_data_e1x.common.state_machine;
9536255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9537255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9538255736Sdavidch                     sizeof(uint32_t));
9539255736Sdavidch        bxe_map_sb_state_machines(sb_data_e1x.index_data);
9540255736Sdavidch    }
9541255736Sdavidch
9542255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9543255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9544255736Sdavidch
9545255736Sdavidch    BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9546255736Sdavidch
9547255736Sdavidch    /* write indices to HW - PCI guarantees endianity of regpairs */
9548255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9549255736Sdavidch}
9550255736Sdavidch
9551255736Sdavidchstatic inline uint8_t
9552255736Sdavidchbxe_fp_qzone_id(struct bxe_fastpath *fp)
9553255736Sdavidch{
9554255736Sdavidch    if (CHIP_IS_E1x(fp->sc)) {
9555255736Sdavidch        return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9556255736Sdavidch    } else {
9557255736Sdavidch        return (fp->cl_id);
9558255736Sdavidch    }
9559255736Sdavidch}
9560255736Sdavidch
9561255736Sdavidchstatic inline uint32_t
9562255736Sdavidchbxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9563255736Sdavidch                           struct bxe_fastpath *fp)
9564255736Sdavidch{
9565255736Sdavidch    uint32_t offset = BAR_USTRORM_INTMEM;
9566255736Sdavidch
9567255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9568255736Sdavidch        offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9569255736Sdavidch    } else {
9570255736Sdavidch        offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9571255736Sdavidch    }
9572255736Sdavidch
9573255736Sdavidch    return (offset);
9574255736Sdavidch}
9575255736Sdavidch
9576255736Sdavidchstatic void
9577255736Sdavidchbxe_init_eth_fp(struct bxe_softc *sc,
9578255736Sdavidch                int              idx)
9579255736Sdavidch{
9580255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[idx];
9581255736Sdavidch    uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9582255736Sdavidch    unsigned long q_type = 0;
9583255736Sdavidch    int cos;
9584255736Sdavidch
9585255736Sdavidch    fp->sc    = sc;
9586255736Sdavidch    fp->index = idx;
9587255736Sdavidch
9588255736Sdavidch    fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9589255736Sdavidch    fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9590255736Sdavidch
9591255736Sdavidch    fp->cl_id = (CHIP_IS_E1x(sc)) ?
9592255736Sdavidch                    (SC_L_ID(sc) + idx) :
9593255736Sdavidch                    /* want client ID same as IGU SB ID for non-E1 */
9594255736Sdavidch                    fp->igu_sb_id;
9595255736Sdavidch    fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9596255736Sdavidch
9597255736Sdavidch    /* setup sb indices */
9598255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9599255736Sdavidch        fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9600255736Sdavidch        fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9601255736Sdavidch    } else {
9602255736Sdavidch        fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9603255736Sdavidch        fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9604255736Sdavidch    }
9605255736Sdavidch
9606255736Sdavidch    /* init shortcut */
9607255736Sdavidch    fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9608255736Sdavidch
9609255736Sdavidch    fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9610255736Sdavidch
9611255736Sdavidch    /*
9612255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
9613255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9614255736Sdavidch     */
9615255736Sdavidch    for (cos = 0; cos < sc->max_cos; cos++) {
9616255736Sdavidch        cids[cos] = idx;
9617255736Sdavidch    }
9618255736Sdavidch    fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9619255736Sdavidch
9620255736Sdavidch    /* nothing more for a VF to do */
9621255736Sdavidch    if (IS_VF(sc)) {
9622255736Sdavidch        return;
9623255736Sdavidch    }
9624255736Sdavidch
9625255736Sdavidch    bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9626255736Sdavidch                fp->fw_sb_id, fp->igu_sb_id);
9627255736Sdavidch
9628255736Sdavidch    bxe_update_fp_sb_idx(fp);
9629255736Sdavidch
9630255736Sdavidch    /* Configure Queue State object */
9631255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9632255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9633255736Sdavidch
9634255736Sdavidch    ecore_init_queue_obj(sc,
9635255736Sdavidch                         &sc->sp_objs[idx].q_obj,
9636255736Sdavidch                         fp->cl_id,
9637255736Sdavidch                         cids,
9638255736Sdavidch                         sc->max_cos,
9639255736Sdavidch                         SC_FUNC(sc),
9640255736Sdavidch                         BXE_SP(sc, q_rdata),
9641255736Sdavidch                         BXE_SP_MAPPING(sc, q_rdata),
9642255736Sdavidch                         q_type);
9643255736Sdavidch
9644255736Sdavidch    /* configure classification DBs */
9645255736Sdavidch    ecore_init_mac_obj(sc,
9646255736Sdavidch                       &sc->sp_objs[idx].mac_obj,
9647255736Sdavidch                       fp->cl_id,
9648255736Sdavidch                       idx,
9649255736Sdavidch                       SC_FUNC(sc),
9650255736Sdavidch                       BXE_SP(sc, mac_rdata),
9651255736Sdavidch                       BXE_SP_MAPPING(sc, mac_rdata),
9652255736Sdavidch                       ECORE_FILTER_MAC_PENDING,
9653255736Sdavidch                       &sc->sp_state,
9654255736Sdavidch                       ECORE_OBJ_TYPE_RX_TX,
9655255736Sdavidch                       &sc->macs_pool);
9656255736Sdavidch
9657255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9658255736Sdavidch          idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9659255736Sdavidch}
9660255736Sdavidch
9661255736Sdavidchstatic inline void
9662255736Sdavidchbxe_update_rx_prod(struct bxe_softc    *sc,
9663255736Sdavidch                   struct bxe_fastpath *fp,
9664255736Sdavidch                   uint16_t            rx_bd_prod,
9665255736Sdavidch                   uint16_t            rx_cq_prod,
9666255736Sdavidch                   uint16_t            rx_sge_prod)
9667255736Sdavidch{
9668255736Sdavidch    struct ustorm_eth_rx_producers rx_prods = { 0 };
9669255736Sdavidch    uint32_t i;
9670255736Sdavidch
9671255736Sdavidch    /* update producers */
9672255736Sdavidch    rx_prods.bd_prod  = rx_bd_prod;
9673255736Sdavidch    rx_prods.cqe_prod = rx_cq_prod;
9674255736Sdavidch    rx_prods.sge_prod = rx_sge_prod;
9675255736Sdavidch
9676255736Sdavidch    /*
9677255736Sdavidch     * Make sure that the BD and SGE data is updated before updating the
9678255736Sdavidch     * producers since FW might read the BD/SGE right after the producer
9679255736Sdavidch     * is updated.
9680255736Sdavidch     * This is only applicable for weak-ordered memory model archs such
9681255736Sdavidch     * as IA-64. The following barrier is also mandatory since FW will
9682255736Sdavidch     * assumes BDs must have buffers.
9683255736Sdavidch     */
9684255736Sdavidch    wmb();
9685255736Sdavidch
9686255736Sdavidch    for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9687255736Sdavidch        REG_WR(sc,
9688255736Sdavidch               (fp->ustorm_rx_prods_offset + (i * 4)),
9689255736Sdavidch               ((uint32_t *)&rx_prods)[i]);
9690255736Sdavidch    }
9691255736Sdavidch
9692255736Sdavidch    wmb(); /* keep prod updates ordered */
9693255736Sdavidch
9694255736Sdavidch    BLOGD(sc, DBG_RX,
9695255736Sdavidch          "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9696255736Sdavidch          fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9697255736Sdavidch}
9698255736Sdavidch
9699255736Sdavidchstatic void
9700255736Sdavidchbxe_init_rx_rings(struct bxe_softc *sc)
9701255736Sdavidch{
9702255736Sdavidch    struct bxe_fastpath *fp;
9703255736Sdavidch    int i;
9704255736Sdavidch
9705255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9706255736Sdavidch        fp = &sc->fp[i];
9707255736Sdavidch
9708255736Sdavidch        fp->rx_bd_cons = 0;
9709255736Sdavidch
9710255736Sdavidch        /*
9711255736Sdavidch         * Activate the BD ring...
9712255736Sdavidch         * Warning, this will generate an interrupt (to the TSTORM)
9713255736Sdavidch         * so this can only be done after the chip is initialized
9714255736Sdavidch         */
9715255736Sdavidch        bxe_update_rx_prod(sc, fp,
9716255736Sdavidch                           fp->rx_bd_prod,
9717255736Sdavidch                           fp->rx_cq_prod,
9718255736Sdavidch                           fp->rx_sge_prod);
9719255736Sdavidch
9720255736Sdavidch        if (i != 0) {
9721255736Sdavidch            continue;
9722255736Sdavidch        }
9723255736Sdavidch
9724255736Sdavidch        if (CHIP_IS_E1(sc)) {
9725255736Sdavidch            REG_WR(sc,
9726255736Sdavidch                   (BAR_USTRORM_INTMEM +
9727255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9728255736Sdavidch                   U64_LO(fp->rcq_dma.paddr));
9729255736Sdavidch            REG_WR(sc,
9730255736Sdavidch                   (BAR_USTRORM_INTMEM +
9731255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9732255736Sdavidch                   U64_HI(fp->rcq_dma.paddr));
9733255736Sdavidch        }
9734255736Sdavidch    }
9735255736Sdavidch}
9736255736Sdavidch
9737255736Sdavidchstatic void
9738255736Sdavidchbxe_init_tx_ring_one(struct bxe_fastpath *fp)
9739255736Sdavidch{
9740296071Sdavidcs    SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9741255736Sdavidch    fp->tx_db.data.zero_fill1 = 0;
9742255736Sdavidch    fp->tx_db.data.prod = 0;
9743255736Sdavidch
9744255736Sdavidch    fp->tx_pkt_prod = 0;
9745255736Sdavidch    fp->tx_pkt_cons = 0;
9746255736Sdavidch    fp->tx_bd_prod = 0;
9747255736Sdavidch    fp->tx_bd_cons = 0;
9748255736Sdavidch    fp->eth_q_stats.tx_pkts = 0;
9749255736Sdavidch}
9750255736Sdavidch
9751255736Sdavidchstatic inline void
9752255736Sdavidchbxe_init_tx_rings(struct bxe_softc *sc)
9753255736Sdavidch{
9754255736Sdavidch    int i;
9755255736Sdavidch
9756255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9757255736Sdavidch        bxe_init_tx_ring_one(&sc->fp[i]);
9758255736Sdavidch    }
9759255736Sdavidch}
9760255736Sdavidch
9761255736Sdavidchstatic void
9762255736Sdavidchbxe_init_def_sb(struct bxe_softc *sc)
9763255736Sdavidch{
9764255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
9765255736Sdavidch    bus_addr_t mapping = sc->def_sb_dma.paddr;
9766255736Sdavidch    int igu_sp_sb_index;
9767255736Sdavidch    int igu_seg_id;
9768255736Sdavidch    int port = SC_PORT(sc);
9769255736Sdavidch    int func = SC_FUNC(sc);
9770255736Sdavidch    int reg_offset, reg_offset_en5;
9771255736Sdavidch    uint64_t section;
9772255736Sdavidch    int index, sindex;
9773255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9774255736Sdavidch
9775255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9776255736Sdavidch
9777255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9778255736Sdavidch        igu_sp_sb_index = DEF_SB_IGU_ID;
9779255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_DEF;
9780255736Sdavidch    } else {
9781255736Sdavidch        igu_sp_sb_index = sc->igu_dsb_id;
9782255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_DEF;
9783255736Sdavidch    }
9784255736Sdavidch
9785255736Sdavidch    /* attentions */
9786255736Sdavidch    section = ((uint64_t)mapping +
9787255736Sdavidch               offsetof(struct host_sp_status_block, atten_status_block));
9788255736Sdavidch    def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9789255736Sdavidch    sc->attn_state = 0;
9790255736Sdavidch
9791255736Sdavidch    reg_offset = (port) ?
9792255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9793255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9794255736Sdavidch    reg_offset_en5 = (port) ?
9795255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9796255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9797255736Sdavidch
9798255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9799255736Sdavidch        /* take care of sig[0]..sig[4] */
9800255736Sdavidch        for (sindex = 0; sindex < 4; sindex++) {
9801255736Sdavidch            sc->attn_group[index].sig[sindex] =
9802255736Sdavidch                REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9803255736Sdavidch        }
9804255736Sdavidch
9805255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
9806255736Sdavidch            /*
9807255736Sdavidch             * enable5 is separate from the rest of the registers,
9808255736Sdavidch             * and the address skip is 4 and not 16 between the
9809255736Sdavidch             * different groups
9810255736Sdavidch             */
9811255736Sdavidch            sc->attn_group[index].sig[4] =
9812255736Sdavidch                REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9813255736Sdavidch        } else {
9814255736Sdavidch            sc->attn_group[index].sig[4] = 0;
9815255736Sdavidch        }
9816255736Sdavidch    }
9817255736Sdavidch
9818255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
9819255736Sdavidch        reg_offset = (port) ?
9820255736Sdavidch                         HC_REG_ATTN_MSG1_ADDR_L :
9821255736Sdavidch                         HC_REG_ATTN_MSG0_ADDR_L;
9822255736Sdavidch        REG_WR(sc, reg_offset, U64_LO(section));
9823255736Sdavidch        REG_WR(sc, (reg_offset + 4), U64_HI(section));
9824255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
9825255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9826255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9827255736Sdavidch    }
9828255736Sdavidch
9829255736Sdavidch    section = ((uint64_t)mapping +
9830255736Sdavidch               offsetof(struct host_sp_status_block, sp_sb));
9831255736Sdavidch
9832255736Sdavidch    bxe_zero_sp_sb(sc);
9833255736Sdavidch
9834255736Sdavidch    /* PCI guarantees endianity of regpair */
9835255736Sdavidch    sp_sb_data.state           = SB_ENABLED;
9836255736Sdavidch    sp_sb_data.host_sb_addr.lo = U64_LO(section);
9837255736Sdavidch    sp_sb_data.host_sb_addr.hi = U64_HI(section);
9838255736Sdavidch    sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9839255736Sdavidch    sp_sb_data.igu_seg_id      = igu_seg_id;
9840255736Sdavidch    sp_sb_data.p_func.pf_id    = func;
9841255736Sdavidch    sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9842255736Sdavidch    sp_sb_data.p_func.vf_id    = 0xff;
9843255736Sdavidch
9844255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9845255736Sdavidch
9846255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9847255736Sdavidch}
9848255736Sdavidch
9849255736Sdavidchstatic void
9850255736Sdavidchbxe_init_sp_ring(struct bxe_softc *sc)
9851255736Sdavidch{
9852255736Sdavidch    atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9853255736Sdavidch    sc->spq_prod_idx = 0;
9854255736Sdavidch    sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9855255736Sdavidch    sc->spq_prod_bd = sc->spq;
9856255736Sdavidch    sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9857255736Sdavidch}
9858255736Sdavidch
9859255736Sdavidchstatic void
9860255736Sdavidchbxe_init_eq_ring(struct bxe_softc *sc)
9861255736Sdavidch{
9862255736Sdavidch    union event_ring_elem *elem;
9863255736Sdavidch    int i;
9864255736Sdavidch
9865255736Sdavidch    for (i = 1; i <= NUM_EQ_PAGES; i++) {
9866255736Sdavidch        elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9867255736Sdavidch
9868255736Sdavidch        elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9869255736Sdavidch                                                 BCM_PAGE_SIZE *
9870255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9871255736Sdavidch        elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9872255736Sdavidch                                                 BCM_PAGE_SIZE *
9873255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9874255736Sdavidch    }
9875255736Sdavidch
9876255736Sdavidch    sc->eq_cons    = 0;
9877255736Sdavidch    sc->eq_prod    = NUM_EQ_DESC;
9878255736Sdavidch    sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9879255736Sdavidch
9880255736Sdavidch    atomic_store_rel_long(&sc->eq_spq_left,
9881255736Sdavidch                          (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9882255736Sdavidch                               NUM_EQ_DESC) - 1));
9883255736Sdavidch}
9884255736Sdavidch
9885255736Sdavidchstatic void
9886255736Sdavidchbxe_init_internal_common(struct bxe_softc *sc)
9887255736Sdavidch{
9888255736Sdavidch    int i;
9889255736Sdavidch
9890255736Sdavidch    /*
9891255736Sdavidch     * Zero this manually as its initialization is currently missing
9892255736Sdavidch     * in the initTool.
9893255736Sdavidch     */
9894255736Sdavidch    for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9895255736Sdavidch        REG_WR(sc,
9896255736Sdavidch               (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9897255736Sdavidch               0);
9898255736Sdavidch    }
9899255736Sdavidch
9900255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9901255736Sdavidch        REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9902255736Sdavidch                CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9903255736Sdavidch    }
9904255736Sdavidch}
9905255736Sdavidch
9906255736Sdavidchstatic void
9907255736Sdavidchbxe_init_internal(struct bxe_softc *sc,
9908255736Sdavidch                  uint32_t         load_code)
9909255736Sdavidch{
9910255736Sdavidch    switch (load_code) {
9911255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON:
9912255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9913255736Sdavidch        bxe_init_internal_common(sc);
9914255736Sdavidch        /* no break */
9915255736Sdavidch
9916255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_PORT:
9917255736Sdavidch        /* nothing to do */
9918255736Sdavidch        /* no break */
9919255736Sdavidch
9920255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_FUNCTION:
9921255736Sdavidch        /* internal memory per function is initialized inside bxe_pf_init */
9922255736Sdavidch        break;
9923255736Sdavidch
9924255736Sdavidch    default:
9925255736Sdavidch        BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
9926255736Sdavidch        break;
9927255736Sdavidch    }
9928255736Sdavidch}
9929255736Sdavidch
9930255736Sdavidchstatic void
9931255736Sdavidchstorm_memset_func_cfg(struct bxe_softc                         *sc,
9932255736Sdavidch                      struct tstorm_eth_function_common_config *tcfg,
9933255736Sdavidch                      uint16_t                                  abs_fid)
9934255736Sdavidch{
9935255736Sdavidch    uint32_t addr;
9936255736Sdavidch    size_t size;
9937255736Sdavidch
9938255736Sdavidch    addr = (BAR_TSTRORM_INTMEM +
9939255736Sdavidch            TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
9940255736Sdavidch    size = sizeof(struct tstorm_eth_function_common_config);
9941255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
9942255736Sdavidch}
9943255736Sdavidch
9944255736Sdavidchstatic void
9945255736Sdavidchbxe_func_init(struct bxe_softc            *sc,
9946255736Sdavidch              struct bxe_func_init_params *p)
9947255736Sdavidch{
9948255736Sdavidch    struct tstorm_eth_function_common_config tcfg = { 0 };
9949255736Sdavidch
9950255736Sdavidch    if (CHIP_IS_E1x(sc)) {
9951255736Sdavidch        storm_memset_func_cfg(sc, &tcfg, p->func_id);
9952255736Sdavidch    }
9953255736Sdavidch
9954255736Sdavidch    /* Enable the function in the FW */
9955255736Sdavidch    storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
9956255736Sdavidch    storm_memset_func_en(sc, p->func_id, 1);
9957255736Sdavidch
9958255736Sdavidch    /* spq */
9959255736Sdavidch    if (p->func_flgs & FUNC_FLG_SPQ) {
9960255736Sdavidch        storm_memset_spq_addr(sc, p->spq_map, p->func_id);
9961255736Sdavidch        REG_WR(sc,
9962255736Sdavidch               (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
9963255736Sdavidch               p->spq_prod);
9964255736Sdavidch    }
9965255736Sdavidch}
9966255736Sdavidch
9967255736Sdavidch/*
9968255736Sdavidch * Calculates the sum of vn_min_rates.
9969255736Sdavidch * It's needed for further normalizing of the min_rates.
9970255736Sdavidch * Returns:
9971255736Sdavidch *   sum of vn_min_rates.
9972255736Sdavidch *     or
9973255736Sdavidch *   0 - if all the min_rates are 0.
9974255736Sdavidch * In the later case fainess algorithm should be deactivated.
9975255736Sdavidch * If all min rates are not zero then those that are zeroes will be set to 1.
9976255736Sdavidch */
9977255736Sdavidchstatic void
9978255736Sdavidchbxe_calc_vn_min(struct bxe_softc       *sc,
9979255736Sdavidch                struct cmng_init_input *input)
9980255736Sdavidch{
9981255736Sdavidch    uint32_t vn_cfg;
9982255736Sdavidch    uint32_t vn_min_rate;
9983255736Sdavidch    int all_zero = 1;
9984255736Sdavidch    int vn;
9985255736Sdavidch
9986255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
9987255736Sdavidch        vn_cfg = sc->devinfo.mf_info.mf_config[vn];
9988255736Sdavidch        vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
9989255736Sdavidch                        FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
9990255736Sdavidch
9991255736Sdavidch        if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
9992255736Sdavidch            /* skip hidden VNs */
9993255736Sdavidch            vn_min_rate = 0;
9994255736Sdavidch        } else if (!vn_min_rate) {
9995255736Sdavidch            /* If min rate is zero - set it to 100 */
9996255736Sdavidch            vn_min_rate = DEF_MIN_RATE;
9997255736Sdavidch        } else {
9998255736Sdavidch            all_zero = 0;
9999255736Sdavidch        }
10000255736Sdavidch
10001255736Sdavidch        input->vnic_min_rate[vn] = vn_min_rate;
10002255736Sdavidch    }
10003255736Sdavidch
10004255736Sdavidch    /* if ETS or all min rates are zeros - disable fairness */
10005255736Sdavidch    if (BXE_IS_ETS_ENABLED(sc)) {
10006255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10007255736Sdavidch        BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10008255736Sdavidch    } else if (all_zero) {
10009255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10010255736Sdavidch        BLOGD(sc, DBG_LOAD,
10011255736Sdavidch              "Fariness disabled (all MIN values are zeroes)\n");
10012255736Sdavidch    } else {
10013255736Sdavidch        input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10014255736Sdavidch    }
10015255736Sdavidch}
10016255736Sdavidch
10017255736Sdavidchstatic inline uint16_t
10018255736Sdavidchbxe_extract_max_cfg(struct bxe_softc *sc,
10019255736Sdavidch                    uint32_t         mf_cfg)
10020255736Sdavidch{
10021255736Sdavidch    uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10022255736Sdavidch                        FUNC_MF_CFG_MAX_BW_SHIFT);
10023255736Sdavidch
10024255736Sdavidch    if (!max_cfg) {
10025255736Sdavidch        BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10026255736Sdavidch        max_cfg = 100;
10027255736Sdavidch    }
10028255736Sdavidch
10029255736Sdavidch    return (max_cfg);
10030255736Sdavidch}
10031255736Sdavidch
10032255736Sdavidchstatic void
10033255736Sdavidchbxe_calc_vn_max(struct bxe_softc       *sc,
10034255736Sdavidch                int                    vn,
10035255736Sdavidch                struct cmng_init_input *input)
10036255736Sdavidch{
10037255736Sdavidch    uint16_t vn_max_rate;
10038255736Sdavidch    uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10039255736Sdavidch    uint32_t max_cfg;
10040255736Sdavidch
10041255736Sdavidch    if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10042255736Sdavidch        vn_max_rate = 0;
10043255736Sdavidch    } else {
10044255736Sdavidch        max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10045255736Sdavidch
10046255736Sdavidch        if (IS_MF_SI(sc)) {
10047255736Sdavidch            /* max_cfg in percents of linkspeed */
10048255736Sdavidch            vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10049255736Sdavidch        } else { /* SD modes */
10050255736Sdavidch            /* max_cfg is absolute in 100Mb units */
10051255736Sdavidch            vn_max_rate = (max_cfg * 100);
10052255736Sdavidch        }
10053255736Sdavidch    }
10054255736Sdavidch
10055255736Sdavidch    BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10056255736Sdavidch
10057255736Sdavidch    input->vnic_max_rate[vn] = vn_max_rate;
10058255736Sdavidch}
10059255736Sdavidch
10060255736Sdavidchstatic void
10061255736Sdavidchbxe_cmng_fns_init(struct bxe_softc *sc,
10062255736Sdavidch                  uint8_t          read_cfg,
10063255736Sdavidch                  uint8_t          cmng_type)
10064255736Sdavidch{
10065255736Sdavidch    struct cmng_init_input input;
10066255736Sdavidch    int vn;
10067255736Sdavidch
10068255736Sdavidch    memset(&input, 0, sizeof(struct cmng_init_input));
10069255736Sdavidch
10070255736Sdavidch    input.port_rate = sc->link_vars.line_speed;
10071255736Sdavidch
10072255736Sdavidch    if (cmng_type == CMNG_FNS_MINMAX) {
10073255736Sdavidch        /* read mf conf from shmem */
10074255736Sdavidch        if (read_cfg) {
10075255736Sdavidch            bxe_read_mf_cfg(sc);
10076255736Sdavidch        }
10077255736Sdavidch
10078255736Sdavidch        /* get VN min rate and enable fairness if not 0 */
10079255736Sdavidch        bxe_calc_vn_min(sc, &input);
10080255736Sdavidch
10081255736Sdavidch        /* get VN max rate */
10082255736Sdavidch        if (sc->port.pmf) {
10083255736Sdavidch            for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10084255736Sdavidch                bxe_calc_vn_max(sc, vn, &input);
10085255736Sdavidch            }
10086255736Sdavidch        }
10087255736Sdavidch
10088255736Sdavidch        /* always enable rate shaping and fairness */
10089255736Sdavidch        input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10090255736Sdavidch
10091255736Sdavidch        ecore_init_cmng(&input, &sc->cmng);
10092255736Sdavidch        return;
10093255736Sdavidch    }
10094255736Sdavidch
10095255736Sdavidch    /* rate shaping and fairness are disabled */
10096255736Sdavidch    BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10097255736Sdavidch}
10098255736Sdavidch
10099255736Sdavidchstatic int
10100255736Sdavidchbxe_get_cmng_fns_mode(struct bxe_softc *sc)
10101255736Sdavidch{
10102255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10103255736Sdavidch        return (CMNG_FNS_NONE);
10104255736Sdavidch    }
10105255736Sdavidch
10106255736Sdavidch    if (IS_MF(sc)) {
10107255736Sdavidch        return (CMNG_FNS_MINMAX);
10108255736Sdavidch    }
10109255736Sdavidch
10110255736Sdavidch    return (CMNG_FNS_NONE);
10111255736Sdavidch}
10112255736Sdavidch
10113255736Sdavidchstatic void
10114255736Sdavidchstorm_memset_cmng(struct bxe_softc *sc,
10115255736Sdavidch                  struct cmng_init *cmng,
10116255736Sdavidch                  uint8_t          port)
10117255736Sdavidch{
10118255736Sdavidch    int vn;
10119255736Sdavidch    int func;
10120255736Sdavidch    uint32_t addr;
10121255736Sdavidch    size_t size;
10122255736Sdavidch
10123255736Sdavidch    addr = (BAR_XSTRORM_INTMEM +
10124255736Sdavidch            XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10125255736Sdavidch    size = sizeof(struct cmng_struct_per_port);
10126255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10127255736Sdavidch
10128255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10129255736Sdavidch        func = func_by_vn(sc, vn);
10130255736Sdavidch
10131255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10132255736Sdavidch                XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10133255736Sdavidch        size = sizeof(struct rate_shaping_vars_per_vn);
10134255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10135255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10136255736Sdavidch
10137255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10138255736Sdavidch                XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10139255736Sdavidch        size = sizeof(struct fairness_vars_per_vn);
10140255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10141255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10142255736Sdavidch    }
10143255736Sdavidch}
10144255736Sdavidch
10145255736Sdavidchstatic void
10146255736Sdavidchbxe_pf_init(struct bxe_softc *sc)
10147255736Sdavidch{
10148255736Sdavidch    struct bxe_func_init_params func_init = { 0 };
10149255736Sdavidch    struct event_ring_data eq_data = { { 0 } };
10150255736Sdavidch    uint16_t flags;
10151255736Sdavidch
10152255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10153255736Sdavidch        /* reset IGU PF statistics: MSIX + ATTN */
10154255736Sdavidch        /* PF */
10155255736Sdavidch        REG_WR(sc,
10156255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10157255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10158255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10159255736Sdavidch               0);
10160255736Sdavidch        /* ATTN */
10161255736Sdavidch        REG_WR(sc,
10162255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10163255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10164255736Sdavidch                (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10165255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10166255736Sdavidch               0);
10167255736Sdavidch    }
10168255736Sdavidch
10169255736Sdavidch    /* function setup flags */
10170255736Sdavidch    flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10171255736Sdavidch
10172255736Sdavidch    /*
10173255736Sdavidch     * This flag is relevant for E1x only.
10174255736Sdavidch     * E2 doesn't have a TPA configuration in a function level.
10175255736Sdavidch     */
10176266979Smarcel    flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10177255736Sdavidch
10178255736Sdavidch    func_init.func_flgs = flags;
10179255736Sdavidch    func_init.pf_id     = SC_FUNC(sc);
10180255736Sdavidch    func_init.func_id   = SC_FUNC(sc);
10181255736Sdavidch    func_init.spq_map   = sc->spq_dma.paddr;
10182255736Sdavidch    func_init.spq_prod  = sc->spq_prod_idx;
10183255736Sdavidch
10184255736Sdavidch    bxe_func_init(sc, &func_init);
10185255736Sdavidch
10186255736Sdavidch    memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10187255736Sdavidch
10188255736Sdavidch    /*
10189255736Sdavidch     * Congestion management values depend on the link rate.
10190255736Sdavidch     * There is no active link so initial link rate is set to 10Gbps.
10191255736Sdavidch     * When the link comes up the congestion management values are
10192255736Sdavidch     * re-calculated according to the actual link rate.
10193255736Sdavidch     */
10194255736Sdavidch    sc->link_vars.line_speed = SPEED_10000;
10195255736Sdavidch    bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10196255736Sdavidch
10197255736Sdavidch    /* Only the PMF sets the HW */
10198255736Sdavidch    if (sc->port.pmf) {
10199255736Sdavidch        storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10200255736Sdavidch    }
10201255736Sdavidch
10202255736Sdavidch    /* init Event Queue - PCI bus guarantees correct endainity */
10203255736Sdavidch    eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10204255736Sdavidch    eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10205255736Sdavidch    eq_data.producer     = sc->eq_prod;
10206255736Sdavidch    eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10207255736Sdavidch    eq_data.sb_id        = DEF_SB_ID;
10208255736Sdavidch    storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10209255736Sdavidch}
10210255736Sdavidch
10211255736Sdavidchstatic void
10212255736Sdavidchbxe_hc_int_enable(struct bxe_softc *sc)
10213255736Sdavidch{
10214255736Sdavidch    int port = SC_PORT(sc);
10215255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10216255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10217255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10218255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10219255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10220255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10221255736Sdavidch
10222255736Sdavidch    if (msix) {
10223255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10224255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0);
10225255736Sdavidch        val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10226255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10227255736Sdavidch        if (single_msix) {
10228255736Sdavidch            val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10229255736Sdavidch        }
10230255736Sdavidch    } else if (msi) {
10231255736Sdavidch        val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10232255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10233255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10234255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10235255736Sdavidch    } else {
10236255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10237255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10238255736Sdavidch                HC_CONFIG_0_REG_INT_LINE_EN_0 |
10239255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10240255736Sdavidch
10241255736Sdavidch        if (!CHIP_IS_E1(sc)) {
10242255736Sdavidch            BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10243255736Sdavidch                  val, port, addr);
10244255736Sdavidch
10245255736Sdavidch            REG_WR(sc, addr, val);
10246255736Sdavidch
10247255736Sdavidch            val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10248255736Sdavidch        }
10249255736Sdavidch    }
10250255736Sdavidch
10251255736Sdavidch    if (CHIP_IS_E1(sc)) {
10252255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10253255736Sdavidch    }
10254255736Sdavidch
10255255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10256255736Sdavidch          val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10257255736Sdavidch
10258255736Sdavidch    REG_WR(sc, addr, val);
10259255736Sdavidch
10260255736Sdavidch    /* ensure that HC_CONFIG is written before leading/trailing edge config */
10261255736Sdavidch    mb();
10262255736Sdavidch
10263255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10264255736Sdavidch        /* init leading/trailing edge */
10265255736Sdavidch        if (IS_MF(sc)) {
10266255736Sdavidch            val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10267255736Sdavidch            if (sc->port.pmf) {
10268255736Sdavidch                /* enable nig and gpio3 attention */
10269255736Sdavidch                val |= 0x1100;
10270255736Sdavidch            }
10271255736Sdavidch        } else {
10272255736Sdavidch            val = 0xffff;
10273255736Sdavidch        }
10274255736Sdavidch
10275255736Sdavidch        REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10276255736Sdavidch        REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10277255736Sdavidch    }
10278255736Sdavidch
10279255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10280255736Sdavidch    mb();
10281255736Sdavidch}
10282255736Sdavidch
10283255736Sdavidchstatic void
10284255736Sdavidchbxe_igu_int_enable(struct bxe_softc *sc)
10285255736Sdavidch{
10286255736Sdavidch    uint32_t val;
10287255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10288255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10289255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10290255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10291255736Sdavidch
10292255736Sdavidch    val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10293255736Sdavidch
10294255736Sdavidch    if (msix) {
10295255736Sdavidch        val &= ~(IGU_PF_CONF_INT_LINE_EN |
10296255736Sdavidch                 IGU_PF_CONF_SINGLE_ISR_EN);
10297255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10298255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN);
10299255736Sdavidch        if (single_msix) {
10300255736Sdavidch            val |= IGU_PF_CONF_SINGLE_ISR_EN;
10301255736Sdavidch        }
10302255736Sdavidch    } else if (msi) {
10303255736Sdavidch        val &= ~IGU_PF_CONF_INT_LINE_EN;
10304255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10305255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10306255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10307255736Sdavidch    } else {
10308255736Sdavidch        val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10309255736Sdavidch        val |= (IGU_PF_CONF_INT_LINE_EN |
10310255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10311255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10312255736Sdavidch    }
10313255736Sdavidch
10314255736Sdavidch    /* clean previous status - need to configure igu prior to ack*/
10315255736Sdavidch    if ((!msix) || single_msix) {
10316255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10317255736Sdavidch        bxe_ack_int(sc);
10318255736Sdavidch    }
10319255736Sdavidch
10320255736Sdavidch    val |= IGU_PF_CONF_FUNC_EN;
10321255736Sdavidch
10322255736Sdavidch    BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10323255736Sdavidch          val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10324255736Sdavidch
10325255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10326255736Sdavidch
10327255736Sdavidch    mb();
10328255736Sdavidch
10329255736Sdavidch    /* init leading/trailing edge */
10330255736Sdavidch    if (IS_MF(sc)) {
10331255736Sdavidch        val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10332255736Sdavidch        if (sc->port.pmf) {
10333255736Sdavidch            /* enable nig and gpio3 attention */
10334255736Sdavidch            val |= 0x1100;
10335255736Sdavidch        }
10336255736Sdavidch    } else {
10337255736Sdavidch        val = 0xffff;
10338255736Sdavidch    }
10339255736Sdavidch
10340255736Sdavidch    REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10341255736Sdavidch    REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10342255736Sdavidch
10343255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10344255736Sdavidch    mb();
10345255736Sdavidch}
10346255736Sdavidch
10347255736Sdavidchstatic void
10348255736Sdavidchbxe_int_enable(struct bxe_softc *sc)
10349255736Sdavidch{
10350255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10351255736Sdavidch        bxe_hc_int_enable(sc);
10352255736Sdavidch    } else {
10353255736Sdavidch        bxe_igu_int_enable(sc);
10354255736Sdavidch    }
10355255736Sdavidch}
10356255736Sdavidch
10357255736Sdavidchstatic void
10358255736Sdavidchbxe_hc_int_disable(struct bxe_softc *sc)
10359255736Sdavidch{
10360255736Sdavidch    int port = SC_PORT(sc);
10361255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10362255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10363255736Sdavidch
10364255736Sdavidch    /*
10365255736Sdavidch     * In E1 we must use only PCI configuration space to disable MSI/MSIX
10366255736Sdavidch     * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10367255736Sdavidch     * block
10368255736Sdavidch     */
10369255736Sdavidch    if (CHIP_IS_E1(sc)) {
10370255736Sdavidch        /*
10371255736Sdavidch         * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10372255736Sdavidch         * to prevent from HC sending interrupts after we exit the function
10373255736Sdavidch         */
10374255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10375255736Sdavidch
10376255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10377255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10378255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10379255736Sdavidch    } else {
10380255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10381255736Sdavidch                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10382255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10383255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10384255736Sdavidch    }
10385255736Sdavidch
10386255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10387255736Sdavidch
10388255736Sdavidch    /* flush all outstanding writes */
10389255736Sdavidch    mb();
10390255736Sdavidch
10391255736Sdavidch    REG_WR(sc, addr, val);
10392255736Sdavidch    if (REG_RD(sc, addr) != val) {
10393255736Sdavidch        BLOGE(sc, "proper val not read from HC IGU!\n");
10394255736Sdavidch    }
10395255736Sdavidch}
10396255736Sdavidch
10397255736Sdavidchstatic void
10398255736Sdavidchbxe_igu_int_disable(struct bxe_softc *sc)
10399255736Sdavidch{
10400255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10401255736Sdavidch
10402255736Sdavidch    val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10403255736Sdavidch             IGU_PF_CONF_INT_LINE_EN |
10404255736Sdavidch             IGU_PF_CONF_ATTN_BIT_EN);
10405255736Sdavidch
10406255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10407255736Sdavidch
10408255736Sdavidch    /* flush all outstanding writes */
10409255736Sdavidch    mb();
10410255736Sdavidch
10411255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10412255736Sdavidch    if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10413255736Sdavidch        BLOGE(sc, "proper val not read from IGU!\n");
10414255736Sdavidch    }
10415255736Sdavidch}
10416255736Sdavidch
10417255736Sdavidchstatic void
10418255736Sdavidchbxe_int_disable(struct bxe_softc *sc)
10419255736Sdavidch{
10420255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10421255736Sdavidch        bxe_hc_int_disable(sc);
10422255736Sdavidch    } else {
10423255736Sdavidch        bxe_igu_int_disable(sc);
10424255736Sdavidch    }
10425255736Sdavidch}
10426255736Sdavidch
10427255736Sdavidchstatic void
10428255736Sdavidchbxe_nic_init(struct bxe_softc *sc,
10429255736Sdavidch             int              load_code)
10430255736Sdavidch{
10431255736Sdavidch    int i;
10432255736Sdavidch
10433255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
10434255736Sdavidch        bxe_init_eth_fp(sc, i);
10435255736Sdavidch    }
10436255736Sdavidch
10437255736Sdavidch    rmb(); /* ensure status block indices were read */
10438255736Sdavidch
10439255736Sdavidch    bxe_init_rx_rings(sc);
10440255736Sdavidch    bxe_init_tx_rings(sc);
10441255736Sdavidch
10442255736Sdavidch    if (IS_VF(sc)) {
10443255736Sdavidch        return;
10444255736Sdavidch    }
10445255736Sdavidch
10446255736Sdavidch    /* initialize MOD_ABS interrupts */
10447255736Sdavidch    elink_init_mod_abs_int(sc, &sc->link_vars,
10448255736Sdavidch                           sc->devinfo.chip_id,
10449255736Sdavidch                           sc->devinfo.shmem_base,
10450255736Sdavidch                           sc->devinfo.shmem2_base,
10451255736Sdavidch                           SC_PORT(sc));
10452255736Sdavidch
10453255736Sdavidch    bxe_init_def_sb(sc);
10454255736Sdavidch    bxe_update_dsb_idx(sc);
10455255736Sdavidch    bxe_init_sp_ring(sc);
10456255736Sdavidch    bxe_init_eq_ring(sc);
10457255736Sdavidch    bxe_init_internal(sc, load_code);
10458255736Sdavidch    bxe_pf_init(sc);
10459255736Sdavidch    bxe_stats_init(sc);
10460255736Sdavidch
10461255736Sdavidch    /* flush all before enabling interrupts */
10462255736Sdavidch    mb();
10463255736Sdavidch
10464255736Sdavidch    bxe_int_enable(sc);
10465255736Sdavidch
10466255736Sdavidch    /* check for SPIO5 */
10467255736Sdavidch    bxe_attn_int_deasserted0(sc,
10468255736Sdavidch                             REG_RD(sc,
10469255736Sdavidch                                    (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10470255736Sdavidch                                     SC_PORT(sc)*4)) &
10471255736Sdavidch                             AEU_INPUTS_ATTN_BITS_SPIO5);
10472255736Sdavidch}
10473255736Sdavidch
10474255736Sdavidchstatic inline void
10475255736Sdavidchbxe_init_objs(struct bxe_softc *sc)
10476255736Sdavidch{
10477255736Sdavidch    /* mcast rules must be added to tx if tx switching is enabled */
10478255736Sdavidch    ecore_obj_type o_type =
10479255736Sdavidch        (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10480255736Sdavidch                                         ECORE_OBJ_TYPE_RX;
10481255736Sdavidch
10482255736Sdavidch    /* RX_MODE controlling object */
10483255736Sdavidch    ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10484255736Sdavidch
10485255736Sdavidch    /* multicast configuration controlling object */
10486255736Sdavidch    ecore_init_mcast_obj(sc,
10487255736Sdavidch                         &sc->mcast_obj,
10488255736Sdavidch                         sc->fp[0].cl_id,
10489255736Sdavidch                         sc->fp[0].index,
10490255736Sdavidch                         SC_FUNC(sc),
10491255736Sdavidch                         SC_FUNC(sc),
10492255736Sdavidch                         BXE_SP(sc, mcast_rdata),
10493255736Sdavidch                         BXE_SP_MAPPING(sc, mcast_rdata),
10494255736Sdavidch                         ECORE_FILTER_MCAST_PENDING,
10495255736Sdavidch                         &sc->sp_state,
10496255736Sdavidch                         o_type);
10497255736Sdavidch
10498255736Sdavidch    /* Setup CAM credit pools */
10499255736Sdavidch    ecore_init_mac_credit_pool(sc,
10500255736Sdavidch                               &sc->macs_pool,
10501255736Sdavidch                               SC_FUNC(sc),
10502255736Sdavidch                               CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10503255736Sdavidch                                                 VNICS_PER_PATH(sc));
10504255736Sdavidch
10505255736Sdavidch    ecore_init_vlan_credit_pool(sc,
10506255736Sdavidch                                &sc->vlans_pool,
10507255736Sdavidch                                SC_ABS_FUNC(sc) >> 1,
10508255736Sdavidch                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10509255736Sdavidch                                                  VNICS_PER_PATH(sc));
10510255736Sdavidch
10511255736Sdavidch    /* RSS configuration object */
10512255736Sdavidch    ecore_init_rss_config_obj(sc,
10513255736Sdavidch                              &sc->rss_conf_obj,
10514255736Sdavidch                              sc->fp[0].cl_id,
10515255736Sdavidch                              sc->fp[0].index,
10516255736Sdavidch                              SC_FUNC(sc),
10517255736Sdavidch                              SC_FUNC(sc),
10518255736Sdavidch                              BXE_SP(sc, rss_rdata),
10519255736Sdavidch                              BXE_SP_MAPPING(sc, rss_rdata),
10520255736Sdavidch                              ECORE_FILTER_RSS_CONF_PENDING,
10521255736Sdavidch                              &sc->sp_state, ECORE_OBJ_TYPE_RX);
10522255736Sdavidch}
10523255736Sdavidch
10524255736Sdavidch/*
10525255736Sdavidch * Initialize the function. This must be called before sending CLIENT_SETUP
10526255736Sdavidch * for the first client.
10527255736Sdavidch */
10528255736Sdavidchstatic inline int
10529255736Sdavidchbxe_func_start(struct bxe_softc *sc)
10530255736Sdavidch{
10531255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
10532255736Sdavidch    struct ecore_func_start_params *start_params = &func_params.params.start;
10533255736Sdavidch
10534255736Sdavidch    /* Prepare parameters for function state transitions */
10535255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10536255736Sdavidch
10537255736Sdavidch    func_params.f_obj = &sc->func_obj;
10538255736Sdavidch    func_params.cmd = ECORE_F_CMD_START;
10539255736Sdavidch
10540255736Sdavidch    /* Function parameters */
10541255736Sdavidch    start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10542255736Sdavidch    start_params->sd_vlan_tag = OVLAN(sc);
10543255736Sdavidch
10544255736Sdavidch    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10545255736Sdavidch        start_params->network_cos_mode = STATIC_COS;
10546255736Sdavidch    } else { /* CHIP_IS_E1X */
10547255736Sdavidch        start_params->network_cos_mode = FW_WRR;
10548255736Sdavidch    }
10549255736Sdavidch
10550296071Sdavidcs    //start_params->gre_tunnel_mode = 0;
10551296071Sdavidcs    //start_params->gre_tunnel_rss  = 0;
10552255736Sdavidch
10553255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
10554255736Sdavidch}
10555255736Sdavidch
10556255736Sdavidchstatic int
10557255736Sdavidchbxe_set_power_state(struct bxe_softc *sc,
10558255736Sdavidch                    uint8_t          state)
10559255736Sdavidch{
10560255736Sdavidch    uint16_t pmcsr;
10561255736Sdavidch
10562255736Sdavidch    /* If there is no power capability, silently succeed */
10563255736Sdavidch    if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10564255736Sdavidch        BLOGW(sc, "No power capability\n");
10565255736Sdavidch        return (0);
10566255736Sdavidch    }
10567255736Sdavidch
10568255736Sdavidch    pmcsr = pci_read_config(sc->dev,
10569255736Sdavidch                            (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10570255736Sdavidch                            2);
10571255736Sdavidch
10572255736Sdavidch    switch (state) {
10573255736Sdavidch    case PCI_PM_D0:
10574255736Sdavidch        pci_write_config(sc->dev,
10575255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10576255736Sdavidch                         ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10577255736Sdavidch
10578255736Sdavidch        if (pmcsr & PCIM_PSTAT_DMASK) {
10579255736Sdavidch            /* delay required during transition out of D3hot */
10580255736Sdavidch            DELAY(20000);
10581255736Sdavidch        }
10582255736Sdavidch
10583255736Sdavidch        break;
10584255736Sdavidch
10585255736Sdavidch    case PCI_PM_D3hot:
10586255736Sdavidch        /* XXX if there are other clients above don't shut down the power */
10587255736Sdavidch
10588255736Sdavidch        /* don't shut down the power for emulation and FPGA */
10589255736Sdavidch        if (CHIP_REV_IS_SLOW(sc)) {
10590255736Sdavidch            return (0);
10591255736Sdavidch        }
10592255736Sdavidch
10593255736Sdavidch        pmcsr &= ~PCIM_PSTAT_DMASK;
10594255736Sdavidch        pmcsr |= PCIM_PSTAT_D3;
10595255736Sdavidch
10596255736Sdavidch        if (sc->wol) {
10597255736Sdavidch            pmcsr |= PCIM_PSTAT_PMEENABLE;
10598255736Sdavidch        }
10599255736Sdavidch
10600255736Sdavidch        pci_write_config(sc->dev,
10601255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10602255736Sdavidch                         pmcsr, 4);
10603255736Sdavidch
10604255736Sdavidch        /*
10605255736Sdavidch         * No more memory access after this point until device is brought back
10606255736Sdavidch         * to D0 state.
10607255736Sdavidch         */
10608255736Sdavidch        break;
10609255736Sdavidch
10610255736Sdavidch    default:
10611295830Sdavidcs        BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10612295830Sdavidcs            state, pmcsr);
10613255736Sdavidch        return (-1);
10614255736Sdavidch    }
10615255736Sdavidch
10616255736Sdavidch    return (0);
10617255736Sdavidch}
10618255736Sdavidch
10619255736Sdavidch
10620255736Sdavidch/* return true if succeeded to acquire the lock */
10621255736Sdavidchstatic uint8_t
10622255736Sdavidchbxe_trylock_hw_lock(struct bxe_softc *sc,
10623255736Sdavidch                    uint32_t         resource)
10624255736Sdavidch{
10625255736Sdavidch    uint32_t lock_status;
10626255736Sdavidch    uint32_t resource_bit = (1 << resource);
10627255736Sdavidch    int func = SC_FUNC(sc);
10628255736Sdavidch    uint32_t hw_lock_control_reg;
10629255736Sdavidch
10630255736Sdavidch    BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10631255736Sdavidch
10632255736Sdavidch    /* Validating that the resource is within range */
10633255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10634255736Sdavidch        BLOGD(sc, DBG_LOAD,
10635255736Sdavidch              "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10636255736Sdavidch              resource, HW_LOCK_MAX_RESOURCE_VALUE);
10637255736Sdavidch        return (FALSE);
10638255736Sdavidch    }
10639255736Sdavidch
10640255736Sdavidch    if (func <= 5) {
10641255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10642255736Sdavidch    } else {
10643255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10644255736Sdavidch    }
10645255736Sdavidch
10646255736Sdavidch    /* try to acquire the lock */
10647255736Sdavidch    REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10648255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
10649255736Sdavidch    if (lock_status & resource_bit) {
10650255736Sdavidch        return (TRUE);
10651255736Sdavidch    }
10652255736Sdavidch
10653295830Sdavidcs    BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10654295830Sdavidcs        "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10655295830Sdavidcs        lock_status, resource_bit);
10656255736Sdavidch
10657255736Sdavidch    return (FALSE);
10658255736Sdavidch}
10659255736Sdavidch
10660255736Sdavidch/*
10661255736Sdavidch * Get the recovery leader resource id according to the engine this function
10662255736Sdavidch * belongs to. Currently only only 2 engines is supported.
10663255736Sdavidch */
10664255736Sdavidchstatic int
10665255736Sdavidchbxe_get_leader_lock_resource(struct bxe_softc *sc)
10666255736Sdavidch{
10667255736Sdavidch    if (SC_PATH(sc)) {
10668255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10669255736Sdavidch    } else {
10670255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10671255736Sdavidch    }
10672255736Sdavidch}
10673255736Sdavidch
10674255736Sdavidch/* try to acquire a leader lock for current engine */
10675255736Sdavidchstatic uint8_t
10676255736Sdavidchbxe_trylock_leader_lock(struct bxe_softc *sc)
10677255736Sdavidch{
10678255736Sdavidch    return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10679255736Sdavidch}
10680255736Sdavidch
10681255736Sdavidchstatic int
10682255736Sdavidchbxe_release_leader_lock(struct bxe_softc *sc)
10683255736Sdavidch{
10684255736Sdavidch    return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10685255736Sdavidch}
10686255736Sdavidch
10687255736Sdavidch/* close gates #2, #3 and #4 */
10688255736Sdavidchstatic void
10689255736Sdavidchbxe_set_234_gates(struct bxe_softc *sc,
10690255736Sdavidch                  uint8_t          close)
10691255736Sdavidch{
10692255736Sdavidch    uint32_t val;
10693255736Sdavidch
10694255736Sdavidch    /* gates #2 and #4a are closed/opened for "not E1" only */
10695255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10696255736Sdavidch        /* #4 */
10697255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10698255736Sdavidch        /* #2 */
10699255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10700255736Sdavidch    }
10701255736Sdavidch
10702255736Sdavidch    /* #3 */
10703255736Sdavidch    if (CHIP_IS_E1x(sc)) {
10704255736Sdavidch        /* prevent interrupts from HC on both ports */
10705255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_1);
10706255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_1,
10707255736Sdavidch               (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10708255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10709255736Sdavidch
10710255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_0);
10711255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_0,
10712255736Sdavidch               (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10713255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10714255736Sdavidch    } else {
10715298955Spfg        /* Prevent incoming interrupts in IGU */
10716255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10717255736Sdavidch
10718255736Sdavidch        REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10719255736Sdavidch               (!close) ?
10720255736Sdavidch               (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10721255736Sdavidch               (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10722255736Sdavidch    }
10723255736Sdavidch
10724255736Sdavidch    BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10725255736Sdavidch          close ? "closing" : "opening");
10726255736Sdavidch
10727255736Sdavidch    wmb();
10728255736Sdavidch}
10729255736Sdavidch
10730255736Sdavidch/* poll for pending writes bit, it should get cleared in no more than 1s */
10731255736Sdavidchstatic int
10732255736Sdavidchbxe_er_poll_igu_vq(struct bxe_softc *sc)
10733255736Sdavidch{
10734255736Sdavidch    uint32_t cnt = 1000;
10735255736Sdavidch    uint32_t pend_bits = 0;
10736255736Sdavidch
10737255736Sdavidch    do {
10738255736Sdavidch        pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10739255736Sdavidch
10740255736Sdavidch        if (pend_bits == 0) {
10741255736Sdavidch            break;
10742255736Sdavidch        }
10743255736Sdavidch
10744255736Sdavidch        DELAY(1000);
10745255736Sdavidch    } while (--cnt > 0);
10746255736Sdavidch
10747255736Sdavidch    if (cnt == 0) {
10748255736Sdavidch        BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10749255736Sdavidch        return (-1);
10750255736Sdavidch    }
10751255736Sdavidch
10752255736Sdavidch    return (0);
10753255736Sdavidch}
10754255736Sdavidch
10755255736Sdavidch#define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10756255736Sdavidch
10757255736Sdavidchstatic void
10758255736Sdavidchbxe_clp_reset_prep(struct bxe_softc *sc,
10759255736Sdavidch                   uint32_t         *magic_val)
10760255736Sdavidch{
10761255736Sdavidch    /* Do some magic... */
10762255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10763255736Sdavidch    *magic_val = val & SHARED_MF_CLP_MAGIC;
10764255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10765255736Sdavidch}
10766255736Sdavidch
10767255736Sdavidch/* restore the value of the 'magic' bit */
10768255736Sdavidchstatic void
10769255736Sdavidchbxe_clp_reset_done(struct bxe_softc *sc,
10770255736Sdavidch                   uint32_t         magic_val)
10771255736Sdavidch{
10772255736Sdavidch    /* Restore the 'magic' bit value... */
10773255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10774255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb,
10775255736Sdavidch              (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10776255736Sdavidch}
10777255736Sdavidch
10778255736Sdavidch/* prepare for MCP reset, takes care of CLP configurations */
10779255736Sdavidchstatic void
10780255736Sdavidchbxe_reset_mcp_prep(struct bxe_softc *sc,
10781255736Sdavidch                   uint32_t         *magic_val)
10782255736Sdavidch{
10783255736Sdavidch    uint32_t shmem;
10784255736Sdavidch    uint32_t validity_offset;
10785255736Sdavidch
10786255736Sdavidch    /* set `magic' bit in order to save MF config */
10787255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10788255736Sdavidch        bxe_clp_reset_prep(sc, magic_val);
10789255736Sdavidch    }
10790255736Sdavidch
10791255736Sdavidch    /* get shmem offset */
10792255736Sdavidch    shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10793255736Sdavidch    validity_offset =
10794255736Sdavidch        offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10795255736Sdavidch
10796255736Sdavidch    /* Clear validity map flags */
10797255736Sdavidch    if (shmem > 0) {
10798255736Sdavidch        REG_WR(sc, shmem + validity_offset, 0);
10799255736Sdavidch    }
10800255736Sdavidch}
10801255736Sdavidch
10802255736Sdavidch#define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10803255736Sdavidch#define MCP_ONE_TIMEOUT  100    /* 100 ms */
10804255736Sdavidch
10805255736Sdavidchstatic void
10806255736Sdavidchbxe_mcp_wait_one(struct bxe_softc *sc)
10807255736Sdavidch{
10808255736Sdavidch    /* special handling for emulation and FPGA (10 times longer) */
10809255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10810255736Sdavidch        DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10811255736Sdavidch    } else {
10812255736Sdavidch        DELAY((MCP_ONE_TIMEOUT) * 1000);
10813255736Sdavidch    }
10814255736Sdavidch}
10815255736Sdavidch
10816255736Sdavidch/* initialize shmem_base and waits for validity signature to appear */
10817255736Sdavidchstatic int
10818255736Sdavidchbxe_init_shmem(struct bxe_softc *sc)
10819255736Sdavidch{
10820255736Sdavidch    int cnt = 0;
10821255736Sdavidch    uint32_t val = 0;
10822255736Sdavidch
10823255736Sdavidch    do {
10824255736Sdavidch        sc->devinfo.shmem_base     =
10825255736Sdavidch        sc->link_params.shmem_base =
10826255736Sdavidch            REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10827255736Sdavidch
10828255736Sdavidch        if (sc->devinfo.shmem_base) {
10829255736Sdavidch            val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10830255736Sdavidch            if (val & SHR_MEM_VALIDITY_MB)
10831255736Sdavidch                return (0);
10832255736Sdavidch        }
10833255736Sdavidch
10834255736Sdavidch        bxe_mcp_wait_one(sc);
10835255736Sdavidch
10836255736Sdavidch    } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10837255736Sdavidch
10838255736Sdavidch    BLOGE(sc, "BAD MCP validity signature\n");
10839255736Sdavidch
10840255736Sdavidch    return (-1);
10841255736Sdavidch}
10842255736Sdavidch
10843255736Sdavidchstatic int
10844255736Sdavidchbxe_reset_mcp_comp(struct bxe_softc *sc,
10845255736Sdavidch                   uint32_t         magic_val)
10846255736Sdavidch{
10847255736Sdavidch    int rc = bxe_init_shmem(sc);
10848255736Sdavidch
10849255736Sdavidch    /* Restore the `magic' bit value */
10850255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10851255736Sdavidch        bxe_clp_reset_done(sc, magic_val);
10852255736Sdavidch    }
10853255736Sdavidch
10854255736Sdavidch    return (rc);
10855255736Sdavidch}
10856255736Sdavidch
10857255736Sdavidchstatic void
10858255736Sdavidchbxe_pxp_prep(struct bxe_softc *sc)
10859255736Sdavidch{
10860255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10861255736Sdavidch        REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10862255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10863255736Sdavidch        wmb();
10864255736Sdavidch    }
10865255736Sdavidch}
10866255736Sdavidch
10867255736Sdavidch/*
10868255736Sdavidch * Reset the whole chip except for:
10869255736Sdavidch *      - PCIE core
10870255736Sdavidch *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10871255736Sdavidch *      - IGU
10872255736Sdavidch *      - MISC (including AEU)
10873255736Sdavidch *      - GRC
10874255736Sdavidch *      - RBCN, RBCP
10875255736Sdavidch */
10876255736Sdavidchstatic void
10877255736Sdavidchbxe_process_kill_chip_reset(struct bxe_softc *sc,
10878255736Sdavidch                            uint8_t          global)
10879255736Sdavidch{
10880255736Sdavidch    uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10881255736Sdavidch    uint32_t global_bits2, stay_reset2;
10882255736Sdavidch
10883255736Sdavidch    /*
10884255736Sdavidch     * Bits that have to be set in reset_mask2 if we want to reset 'global'
10885255736Sdavidch     * (per chip) blocks.
10886255736Sdavidch     */
10887255736Sdavidch    global_bits2 =
10888255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10889255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10890255736Sdavidch
10891255736Sdavidch    /*
10892255736Sdavidch     * Don't reset the following blocks.
10893255736Sdavidch     * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10894255736Sdavidch     *            reset, as in 4 port device they might still be owned
10895255736Sdavidch     *            by the MCP (there is only one leader per path).
10896255736Sdavidch     */
10897255736Sdavidch    not_reset_mask1 =
10898255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_HC |
10899255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10900255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXP;
10901255736Sdavidch
10902255736Sdavidch    not_reset_mask2 =
10903255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10904255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10905255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10906255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10907255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10908255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10909255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10910255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10911255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_ATC |
10912255736Sdavidch        MISC_REGISTERS_RESET_REG_2_PGLC |
10913255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10914255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10915255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10916255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10917255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC0 |
10918255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC1;
10919255736Sdavidch
10920255736Sdavidch    /*
10921255736Sdavidch     * Keep the following blocks in reset:
10922255736Sdavidch     *  - all xxMACs are handled by the elink code.
10923255736Sdavidch     */
10924255736Sdavidch    stay_reset2 =
10925255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC |
10926255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
10927255736Sdavidch
10928255736Sdavidch    /* Full reset masks according to the chip */
10929255736Sdavidch    reset_mask1 = 0xffffffff;
10930255736Sdavidch
10931255736Sdavidch    if (CHIP_IS_E1(sc))
10932255736Sdavidch        reset_mask2 = 0xffff;
10933255736Sdavidch    else if (CHIP_IS_E1H(sc))
10934255736Sdavidch        reset_mask2 = 0x1ffff;
10935255736Sdavidch    else if (CHIP_IS_E2(sc))
10936255736Sdavidch        reset_mask2 = 0xfffff;
10937255736Sdavidch    else /* CHIP_IS_E3 */
10938255736Sdavidch        reset_mask2 = 0x3ffffff;
10939255736Sdavidch
10940255736Sdavidch    /* Don't reset global blocks unless we need to */
10941255736Sdavidch    if (!global)
10942255736Sdavidch        reset_mask2 &= ~global_bits2;
10943255736Sdavidch
10944255736Sdavidch    /*
10945255736Sdavidch     * In case of attention in the QM, we need to reset PXP
10946255736Sdavidch     * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
10947255736Sdavidch     * because otherwise QM reset would release 'close the gates' shortly
10948255736Sdavidch     * before resetting the PXP, then the PSWRQ would send a write
10949255736Sdavidch     * request to PGLUE. Then when PXP is reset, PGLUE would try to
10950255736Sdavidch     * read the payload data from PSWWR, but PSWWR would not
10951255736Sdavidch     * respond. The write queue in PGLUE would stuck, dmae commands
10952255736Sdavidch     * would not return. Therefore it's important to reset the second
10953255736Sdavidch     * reset register (containing the
10954255736Sdavidch     * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
10955255736Sdavidch     * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
10956255736Sdavidch     * bit).
10957255736Sdavidch     */
10958255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
10959255736Sdavidch           reset_mask2 & (~not_reset_mask2));
10960255736Sdavidch
10961255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
10962255736Sdavidch           reset_mask1 & (~not_reset_mask1));
10963255736Sdavidch
10964255736Sdavidch    mb();
10965255736Sdavidch    wmb();
10966255736Sdavidch
10967255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
10968255736Sdavidch           reset_mask2 & (~stay_reset2));
10969255736Sdavidch
10970255736Sdavidch    mb();
10971255736Sdavidch    wmb();
10972255736Sdavidch
10973255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
10974255736Sdavidch    wmb();
10975255736Sdavidch}
10976255736Sdavidch
10977255736Sdavidchstatic int
10978255736Sdavidchbxe_process_kill(struct bxe_softc *sc,
10979255736Sdavidch                 uint8_t          global)
10980255736Sdavidch{
10981255736Sdavidch    int cnt = 1000;
10982255736Sdavidch    uint32_t val = 0;
10983255736Sdavidch    uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
10984255736Sdavidch    uint32_t tags_63_32 = 0;
10985255736Sdavidch
10986255736Sdavidch    /* Empty the Tetris buffer, wait for 1s */
10987255736Sdavidch    do {
10988255736Sdavidch        sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
10989255736Sdavidch        blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
10990255736Sdavidch        port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
10991255736Sdavidch        port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
10992255736Sdavidch        pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
10993255736Sdavidch        if (CHIP_IS_E3(sc)) {
10994255736Sdavidch            tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
10995255736Sdavidch        }
10996255736Sdavidch
10997255736Sdavidch        if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
10998255736Sdavidch            ((port_is_idle_0 & 0x1) == 0x1) &&
10999255736Sdavidch            ((port_is_idle_1 & 0x1) == 0x1) &&
11000255736Sdavidch            (pgl_exp_rom2 == 0xffffffff) &&
11001255736Sdavidch            (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11002255736Sdavidch            break;
11003255736Sdavidch        DELAY(1000);
11004255736Sdavidch    } while (cnt-- > 0);
11005255736Sdavidch
11006255736Sdavidch    if (cnt <= 0) {
11007255736Sdavidch        BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11008255736Sdavidch                  "are still outstanding read requests after 1s! "
11009255736Sdavidch                  "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11010255736Sdavidch                  "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11011255736Sdavidch              sr_cnt, blk_cnt, port_is_idle_0,
11012255736Sdavidch              port_is_idle_1, pgl_exp_rom2);
11013255736Sdavidch        return (-1);
11014255736Sdavidch    }
11015255736Sdavidch
11016255736Sdavidch    mb();
11017255736Sdavidch
11018255736Sdavidch    /* Close gates #2, #3 and #4 */
11019255736Sdavidch    bxe_set_234_gates(sc, TRUE);
11020255736Sdavidch
11021255736Sdavidch    /* Poll for IGU VQs for 57712 and newer chips */
11022255736Sdavidch    if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11023255736Sdavidch        return (-1);
11024255736Sdavidch    }
11025255736Sdavidch
11026255736Sdavidch    /* XXX indicate that "process kill" is in progress to MCP */
11027255736Sdavidch
11028255736Sdavidch    /* clear "unprepared" bit */
11029255736Sdavidch    REG_WR(sc, MISC_REG_UNPREPARED, 0);
11030255736Sdavidch    mb();
11031255736Sdavidch
11032255736Sdavidch    /* Make sure all is written to the chip before the reset */
11033255736Sdavidch    wmb();
11034255736Sdavidch
11035255736Sdavidch    /*
11036255736Sdavidch     * Wait for 1ms to empty GLUE and PCI-E core queues,
11037255736Sdavidch     * PSWHST, GRC and PSWRD Tetris buffer.
11038255736Sdavidch     */
11039255736Sdavidch    DELAY(1000);
11040255736Sdavidch
11041255736Sdavidch    /* Prepare to chip reset: */
11042255736Sdavidch    /* MCP */
11043255736Sdavidch    if (global) {
11044255736Sdavidch        bxe_reset_mcp_prep(sc, &val);
11045255736Sdavidch    }
11046255736Sdavidch
11047255736Sdavidch    /* PXP */
11048255736Sdavidch    bxe_pxp_prep(sc);
11049255736Sdavidch    mb();
11050255736Sdavidch
11051255736Sdavidch    /* reset the chip */
11052255736Sdavidch    bxe_process_kill_chip_reset(sc, global);
11053255736Sdavidch    mb();
11054255736Sdavidch
11055268854Sdavidcs    /* clear errors in PGB */
11056268854Sdavidcs    if (!CHIP_IS_E1(sc))
11057268854Sdavidcs        REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11058268854Sdavidcs
11059255736Sdavidch    /* Recover after reset: */
11060255736Sdavidch    /* MCP */
11061255736Sdavidch    if (global && bxe_reset_mcp_comp(sc, val)) {
11062255736Sdavidch        return (-1);
11063255736Sdavidch    }
11064255736Sdavidch
11065255736Sdavidch    /* XXX add resetting the NO_MCP mode DB here */
11066255736Sdavidch
11067255736Sdavidch    /* Open the gates #2, #3 and #4 */
11068255736Sdavidch    bxe_set_234_gates(sc, FALSE);
11069255736Sdavidch
11070255736Sdavidch    /* XXX
11071255736Sdavidch     * IGU/AEU preparation bring back the AEU/IGU to a reset state
11072255736Sdavidch     * re-enable attentions
11073255736Sdavidch     */
11074255736Sdavidch
11075255736Sdavidch    return (0);
11076255736Sdavidch}
11077255736Sdavidch
11078255736Sdavidchstatic int
11079255736Sdavidchbxe_leader_reset(struct bxe_softc *sc)
11080255736Sdavidch{
11081255736Sdavidch    int rc = 0;
11082255736Sdavidch    uint8_t global = bxe_reset_is_global(sc);
11083255736Sdavidch    uint32_t load_code;
11084255736Sdavidch
11085255736Sdavidch    /*
11086255736Sdavidch     * If not going to reset MCP, load "fake" driver to reset HW while
11087255736Sdavidch     * driver is owner of the HW.
11088255736Sdavidch     */
11089255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11090255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11091255736Sdavidch                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11092255736Sdavidch        if (!load_code) {
11093255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11094255736Sdavidch            rc = -1;
11095255736Sdavidch            goto exit_leader_reset;
11096255736Sdavidch        }
11097255736Sdavidch
11098255736Sdavidch        if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11099255736Sdavidch            (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11100255736Sdavidch            BLOGE(sc, "MCP unexpected response, aborting\n");
11101255736Sdavidch            rc = -1;
11102255736Sdavidch            goto exit_leader_reset2;
11103255736Sdavidch        }
11104255736Sdavidch
11105255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11106255736Sdavidch        if (!load_code) {
11107255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11108255736Sdavidch            rc = -1;
11109255736Sdavidch            goto exit_leader_reset2;
11110255736Sdavidch        }
11111255736Sdavidch    }
11112255736Sdavidch
11113255736Sdavidch    /* try to recover after the failure */
11114255736Sdavidch    if (bxe_process_kill(sc, global)) {
11115255736Sdavidch        BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11116255736Sdavidch        rc = -1;
11117255736Sdavidch        goto exit_leader_reset2;
11118255736Sdavidch    }
11119255736Sdavidch
11120255736Sdavidch    /*
11121255736Sdavidch     * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11122255736Sdavidch     * state.
11123255736Sdavidch     */
11124255736Sdavidch    bxe_set_reset_done(sc);
11125255736Sdavidch    if (global) {
11126255736Sdavidch        bxe_clear_reset_global(sc);
11127255736Sdavidch    }
11128255736Sdavidch
11129255736Sdavidchexit_leader_reset2:
11130255736Sdavidch
11131255736Sdavidch    /* unload "fake driver" if it was loaded */
11132255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11133255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11134255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11135255736Sdavidch    }
11136255736Sdavidch
11137255736Sdavidchexit_leader_reset:
11138255736Sdavidch
11139255736Sdavidch    sc->is_leader = 0;
11140255736Sdavidch    bxe_release_leader_lock(sc);
11141255736Sdavidch
11142255736Sdavidch    mb();
11143255736Sdavidch    return (rc);
11144255736Sdavidch}
11145255736Sdavidch
11146255736Sdavidch/*
11147255736Sdavidch * prepare INIT transition, parameters configured:
11148255736Sdavidch *   - HC configuration
11149255736Sdavidch *   - Queue's CDU context
11150255736Sdavidch */
11151255736Sdavidchstatic void
11152255736Sdavidchbxe_pf_q_prep_init(struct bxe_softc               *sc,
11153255736Sdavidch                   struct bxe_fastpath            *fp,
11154255736Sdavidch                   struct ecore_queue_init_params *init_params)
11155255736Sdavidch{
11156255736Sdavidch    uint8_t cos;
11157255736Sdavidch    int cxt_index, cxt_offset;
11158255736Sdavidch
11159255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11160255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11161255736Sdavidch
11162255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11163255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11164255736Sdavidch
11165255736Sdavidch    /* HC rate */
11166255736Sdavidch    init_params->rx.hc_rate =
11167255736Sdavidch        sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11168255736Sdavidch    init_params->tx.hc_rate =
11169255736Sdavidch        sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11170255736Sdavidch
11171255736Sdavidch    /* FW SB ID */
11172255736Sdavidch    init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11173255736Sdavidch
11174255736Sdavidch    /* CQ index among the SB indices */
11175255736Sdavidch    init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11176255736Sdavidch    init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11177255736Sdavidch
11178255736Sdavidch    /* set maximum number of COSs supported by this queue */
11179255736Sdavidch    init_params->max_cos = sc->max_cos;
11180255736Sdavidch
11181255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11182255736Sdavidch          fp->index, init_params->max_cos);
11183255736Sdavidch
11184255736Sdavidch    /* set the context pointers queue object */
11185255736Sdavidch    for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11186255736Sdavidch        /* XXX change index/cid here if ever support multiple tx CoS */
11187255736Sdavidch        /* fp->txdata[cos]->cid */
11188255736Sdavidch        cxt_index = fp->index / ILT_PAGE_CIDS;
11189255736Sdavidch        cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11190255736Sdavidch        init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11191255736Sdavidch    }
11192255736Sdavidch}
11193255736Sdavidch
11194255736Sdavidch/* set flags that are common for the Tx-only and not normal connections */
11195255736Sdavidchstatic unsigned long
11196255736Sdavidchbxe_get_common_flags(struct bxe_softc    *sc,
11197255736Sdavidch                     struct bxe_fastpath *fp,
11198255736Sdavidch                     uint8_t             zero_stats)
11199255736Sdavidch{
11200255736Sdavidch    unsigned long flags = 0;
11201255736Sdavidch
11202255736Sdavidch    /* PF driver will always initialize the Queue to an ACTIVE state */
11203255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11204255736Sdavidch
11205255736Sdavidch    /*
11206255736Sdavidch     * tx only connections collect statistics (on the same index as the
11207255736Sdavidch     * parent connection). The statistics are zeroed when the parent
11208255736Sdavidch     * connection is initialized.
11209255736Sdavidch     */
11210255736Sdavidch
11211255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11212255736Sdavidch    if (zero_stats) {
11213255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11214255736Sdavidch    }
11215255736Sdavidch
11216255736Sdavidch    /*
11217255736Sdavidch     * tx only connections can support tx-switching, though their
11218255736Sdavidch     * CoS-ness doesn't survive the loopback
11219255736Sdavidch     */
11220255736Sdavidch    if (sc->flags & BXE_TX_SWITCHING) {
11221255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11222255736Sdavidch    }
11223255736Sdavidch
11224255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11225255736Sdavidch
11226255736Sdavidch    return (flags);
11227255736Sdavidch}
11228255736Sdavidch
11229255736Sdavidchstatic unsigned long
11230255736Sdavidchbxe_get_q_flags(struct bxe_softc    *sc,
11231255736Sdavidch                struct bxe_fastpath *fp,
11232255736Sdavidch                uint8_t             leading)
11233255736Sdavidch{
11234255736Sdavidch    unsigned long flags = 0;
11235255736Sdavidch
11236255736Sdavidch    if (IS_MF_SD(sc)) {
11237255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11238255736Sdavidch    }
11239255736Sdavidch
11240266979Smarcel    if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11241255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11242315881Sdavidcs#if __FreeBSD_version >= 800000
11243255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11244315881Sdavidcs#endif
11245255736Sdavidch    }
11246255736Sdavidch
11247255736Sdavidch    if (leading) {
11248255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11249255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11250255736Sdavidch    }
11251255736Sdavidch
11252255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11253255736Sdavidch
11254255736Sdavidch    /* merge with common flags */
11255255736Sdavidch    return (flags | bxe_get_common_flags(sc, fp, TRUE));
11256255736Sdavidch}
11257255736Sdavidch
11258255736Sdavidchstatic void
11259255736Sdavidchbxe_pf_q_prep_general(struct bxe_softc                  *sc,
11260255736Sdavidch                      struct bxe_fastpath               *fp,
11261255736Sdavidch                      struct ecore_general_setup_params *gen_init,
11262255736Sdavidch                      uint8_t                           cos)
11263255736Sdavidch{
11264255736Sdavidch    gen_init->stat_id = bxe_stats_id(fp);
11265255736Sdavidch    gen_init->spcl_id = fp->cl_id;
11266255736Sdavidch    gen_init->mtu = sc->mtu;
11267255736Sdavidch    gen_init->cos = cos;
11268255736Sdavidch}
11269255736Sdavidch
11270255736Sdavidchstatic void
11271255736Sdavidchbxe_pf_rx_q_prep(struct bxe_softc              *sc,
11272255736Sdavidch                 struct bxe_fastpath           *fp,
11273255736Sdavidch                 struct rxq_pause_params       *pause,
11274255736Sdavidch                 struct ecore_rxq_setup_params *rxq_init)
11275255736Sdavidch{
11276255736Sdavidch    uint8_t max_sge = 0;
11277255736Sdavidch    uint16_t sge_sz = 0;
11278255736Sdavidch    uint16_t tpa_agg_size = 0;
11279255736Sdavidch
11280283274Sdavidcs    pause->sge_th_lo = SGE_TH_LO(sc);
11281283274Sdavidcs    pause->sge_th_hi = SGE_TH_HI(sc);
11282255736Sdavidch
11283283274Sdavidcs    /* validate SGE ring has enough to cross high threshold */
11284283274Sdavidcs    if (sc->dropless_fc &&
11285255736Sdavidch            (pause->sge_th_hi + FW_PREFETCH_CNT) >
11286255736Sdavidch            (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11287283274Sdavidcs        BLOGW(sc, "sge ring threshold limit\n");
11288283274Sdavidcs    }
11289255736Sdavidch
11290283274Sdavidcs    /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11291283274Sdavidcs    tpa_agg_size = (2 * sc->mtu);
11292283274Sdavidcs    if (tpa_agg_size < sc->max_aggregation_size) {
11293283274Sdavidcs        tpa_agg_size = sc->max_aggregation_size;
11294283274Sdavidcs    }
11295255736Sdavidch
11296283274Sdavidcs    max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11297283274Sdavidcs    max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11298255736Sdavidch                   (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11299283274Sdavidcs    sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11300255736Sdavidch
11301255736Sdavidch    /* pause - not for e1 */
11302255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11303255736Sdavidch        pause->bd_th_lo = BD_TH_LO(sc);
11304255736Sdavidch        pause->bd_th_hi = BD_TH_HI(sc);
11305255736Sdavidch
11306255736Sdavidch        pause->rcq_th_lo = RCQ_TH_LO(sc);
11307255736Sdavidch        pause->rcq_th_hi = RCQ_TH_HI(sc);
11308255736Sdavidch
11309255736Sdavidch        /* validate rings have enough entries to cross high thresholds */
11310255736Sdavidch        if (sc->dropless_fc &&
11311255736Sdavidch            pause->bd_th_hi + FW_PREFETCH_CNT >
11312255736Sdavidch            sc->rx_ring_size) {
11313255736Sdavidch            BLOGW(sc, "rx bd ring threshold limit\n");
11314255736Sdavidch        }
11315255736Sdavidch
11316255736Sdavidch        if (sc->dropless_fc &&
11317255736Sdavidch            pause->rcq_th_hi + FW_PREFETCH_CNT >
11318255736Sdavidch            RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11319255736Sdavidch            BLOGW(sc, "rcq ring threshold limit\n");
11320255736Sdavidch        }
11321255736Sdavidch
11322255736Sdavidch        pause->pri_map = 1;
11323255736Sdavidch    }
11324255736Sdavidch
11325255736Sdavidch    /* rxq setup */
11326255736Sdavidch    rxq_init->dscr_map   = fp->rx_dma.paddr;
11327255736Sdavidch    rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11328255736Sdavidch    rxq_init->rcq_map    = fp->rcq_dma.paddr;
11329255736Sdavidch    rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11330255736Sdavidch
11331255736Sdavidch    /*
11332255736Sdavidch     * This should be a maximum number of data bytes that may be
11333255736Sdavidch     * placed on the BD (not including paddings).
11334255736Sdavidch     */
11335255736Sdavidch    rxq_init->buf_sz = (fp->rx_buf_size -
11336255736Sdavidch                        IP_HEADER_ALIGNMENT_PADDING);
11337255736Sdavidch
11338255736Sdavidch    rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11339255736Sdavidch    rxq_init->tpa_agg_sz      = tpa_agg_size;
11340255736Sdavidch    rxq_init->sge_buf_sz      = sge_sz;
11341255736Sdavidch    rxq_init->max_sges_pkt    = max_sge;
11342255736Sdavidch    rxq_init->rss_engine_id   = SC_FUNC(sc);
11343255736Sdavidch    rxq_init->mcast_engine_id = SC_FUNC(sc);
11344255736Sdavidch
11345255736Sdavidch    /*
11346255736Sdavidch     * Maximum number or simultaneous TPA aggregation for this Queue.
11347255736Sdavidch     * For PF Clients it should be the maximum available number.
11348255736Sdavidch     * VF driver(s) may want to define it to a smaller value.
11349255736Sdavidch     */
11350255736Sdavidch    rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11351255736Sdavidch
11352255736Sdavidch    rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11353255736Sdavidch    rxq_init->fw_sb_id = fp->fw_sb_id;
11354255736Sdavidch
11355255736Sdavidch    rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11356255736Sdavidch
11357255736Sdavidch    /*
11358255736Sdavidch     * configure silent vlan removal
11359255736Sdavidch     * if multi function mode is afex, then mask default vlan
11360255736Sdavidch     */
11361255736Sdavidch    if (IS_MF_AFEX(sc)) {
11362255736Sdavidch        rxq_init->silent_removal_value =
11363255736Sdavidch            sc->devinfo.mf_info.afex_def_vlan_tag;
11364255736Sdavidch        rxq_init->silent_removal_mask = EVL_VLID_MASK;
11365255736Sdavidch    }
11366255736Sdavidch}
11367255736Sdavidch
11368255736Sdavidchstatic void
11369255736Sdavidchbxe_pf_tx_q_prep(struct bxe_softc              *sc,
11370255736Sdavidch                 struct bxe_fastpath           *fp,
11371255736Sdavidch                 struct ecore_txq_setup_params *txq_init,
11372255736Sdavidch                 uint8_t                       cos)
11373255736Sdavidch{
11374255736Sdavidch    /*
11375255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
11376255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11377255736Sdavidch     * fp->txdata[cos]->tx_dma.paddr;
11378255736Sdavidch     */
11379255736Sdavidch    txq_init->dscr_map     = fp->tx_dma.paddr;
11380255736Sdavidch    txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11381255736Sdavidch    txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11382255736Sdavidch    txq_init->fw_sb_id     = fp->fw_sb_id;
11383255736Sdavidch
11384255736Sdavidch    /*
11385255736Sdavidch     * set the TSS leading client id for TX classfication to the
11386255736Sdavidch     * leading RSS client id
11387255736Sdavidch     */
11388255736Sdavidch    txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11389255736Sdavidch}
11390255736Sdavidch
11391255736Sdavidch/*
11392255736Sdavidch * This function performs 2 steps in a queue state machine:
11393255736Sdavidch *   1) RESET->INIT
11394255736Sdavidch *   2) INIT->SETUP
11395255736Sdavidch */
11396255736Sdavidchstatic int
11397255736Sdavidchbxe_setup_queue(struct bxe_softc    *sc,
11398255736Sdavidch                struct bxe_fastpath *fp,
11399255736Sdavidch                uint8_t             leading)
11400255736Sdavidch{
11401255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
11402255736Sdavidch    struct ecore_queue_setup_params *setup_params =
11403255736Sdavidch                        &q_params.params.setup;
11404255736Sdavidch    int rc;
11405255736Sdavidch
11406255736Sdavidch    BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11407255736Sdavidch
11408255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11409255736Sdavidch
11410255736Sdavidch    q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11411255736Sdavidch
11412255736Sdavidch    /* we want to wait for completion in this context */
11413255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11414255736Sdavidch
11415255736Sdavidch    /* prepare the INIT parameters */
11416255736Sdavidch    bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11417255736Sdavidch
11418255736Sdavidch    /* Set the command */
11419255736Sdavidch    q_params.cmd = ECORE_Q_CMD_INIT;
11420255736Sdavidch
11421255736Sdavidch    /* Change the state to INIT */
11422255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11423255736Sdavidch    if (rc) {
11424295830Sdavidcs        BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11425255736Sdavidch        return (rc);
11426255736Sdavidch    }
11427255736Sdavidch
11428255736Sdavidch    BLOGD(sc, DBG_LOAD, "init complete\n");
11429255736Sdavidch
11430255736Sdavidch    /* now move the Queue to the SETUP state */
11431255736Sdavidch    memset(setup_params, 0, sizeof(*setup_params));
11432255736Sdavidch
11433255736Sdavidch    /* set Queue flags */
11434255736Sdavidch    setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11435255736Sdavidch
11436255736Sdavidch    /* set general SETUP parameters */
11437255736Sdavidch    bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11438255736Sdavidch                          FIRST_TX_COS_INDEX);
11439255736Sdavidch
11440255736Sdavidch    bxe_pf_rx_q_prep(sc, fp,
11441255736Sdavidch                     &setup_params->pause_params,
11442255736Sdavidch                     &setup_params->rxq_params);
11443255736Sdavidch
11444255736Sdavidch    bxe_pf_tx_q_prep(sc, fp,
11445255736Sdavidch                     &setup_params->txq_params,
11446255736Sdavidch                     FIRST_TX_COS_INDEX);
11447255736Sdavidch
11448255736Sdavidch    /* Set the command */
11449255736Sdavidch    q_params.cmd = ECORE_Q_CMD_SETUP;
11450255736Sdavidch
11451255736Sdavidch    /* change the state to SETUP */
11452255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11453255736Sdavidch    if (rc) {
11454295830Sdavidcs        BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11455255736Sdavidch        return (rc);
11456255736Sdavidch    }
11457255736Sdavidch
11458255736Sdavidch    return (rc);
11459255736Sdavidch}
11460255736Sdavidch
11461255736Sdavidchstatic int
11462255736Sdavidchbxe_setup_leading(struct bxe_softc *sc)
11463255736Sdavidch{
11464255736Sdavidch    return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11465255736Sdavidch}
11466255736Sdavidch
11467255736Sdavidchstatic int
11468255736Sdavidchbxe_config_rss_pf(struct bxe_softc            *sc,
11469255736Sdavidch                  struct ecore_rss_config_obj *rss_obj,
11470255736Sdavidch                  uint8_t                     config_hash)
11471255736Sdavidch{
11472255736Sdavidch    struct ecore_config_rss_params params = { NULL };
11473255736Sdavidch    int i;
11474255736Sdavidch
11475255736Sdavidch    /*
11476255736Sdavidch     * Although RSS is meaningless when there is a single HW queue we
11477255736Sdavidch     * still need it enabled in order to have HW Rx hash generated.
11478255736Sdavidch     */
11479255736Sdavidch
11480255736Sdavidch    params.rss_obj = rss_obj;
11481255736Sdavidch
11482255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11483255736Sdavidch
11484255736Sdavidch    bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11485255736Sdavidch
11486255736Sdavidch    /* RSS configuration */
11487255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11488255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11489255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11490255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11491255736Sdavidch    if (rss_obj->udp_rss_v4) {
11492255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11493255736Sdavidch    }
11494255736Sdavidch    if (rss_obj->udp_rss_v6) {
11495255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11496255736Sdavidch    }
11497255736Sdavidch
11498255736Sdavidch    /* Hash bits */
11499255736Sdavidch    params.rss_result_mask = MULTI_MASK;
11500255736Sdavidch
11501255736Sdavidch    memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11502255736Sdavidch
11503255736Sdavidch    if (config_hash) {
11504255736Sdavidch        /* RSS keys */
11505255736Sdavidch        for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11506255736Sdavidch            params.rss_key[i] = arc4random();
11507255736Sdavidch        }
11508255736Sdavidch
11509255736Sdavidch        bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11510255736Sdavidch    }
11511255736Sdavidch
11512255736Sdavidch    return (ecore_config_rss(sc, &params));
11513255736Sdavidch}
11514255736Sdavidch
11515255736Sdavidchstatic int
11516255736Sdavidchbxe_config_rss_eth(struct bxe_softc *sc,
11517255736Sdavidch                   uint8_t          config_hash)
11518255736Sdavidch{
11519255736Sdavidch    return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11520255736Sdavidch}
11521255736Sdavidch
11522255736Sdavidchstatic int
11523255736Sdavidchbxe_init_rss_pf(struct bxe_softc *sc)
11524255736Sdavidch{
11525255736Sdavidch    uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11526255736Sdavidch    int i;
11527255736Sdavidch
11528255736Sdavidch    /*
11529255736Sdavidch     * Prepare the initial contents of the indirection table if
11530255736Sdavidch     * RSS is enabled
11531255736Sdavidch     */
11532255736Sdavidch    for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11533255736Sdavidch        sc->rss_conf_obj.ind_table[i] =
11534255736Sdavidch            (sc->fp->cl_id + (i % num_eth_queues));
11535255736Sdavidch    }
11536255736Sdavidch
11537255736Sdavidch    if (sc->udp_rss) {
11538255736Sdavidch        sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11539255736Sdavidch    }
11540255736Sdavidch
11541255736Sdavidch    /*
11542255736Sdavidch     * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11543255736Sdavidch     * per-port, so if explicit configuration is needed, do it only
11544255736Sdavidch     * for a PMF.
11545255736Sdavidch     *
11546255736Sdavidch     * For 57712 and newer it's a per-function configuration.
11547255736Sdavidch     */
11548255736Sdavidch    return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11549255736Sdavidch}
11550255736Sdavidch
11551255736Sdavidchstatic int
11552255736Sdavidchbxe_set_mac_one(struct bxe_softc          *sc,
11553255736Sdavidch                uint8_t                   *mac,
11554255736Sdavidch                struct ecore_vlan_mac_obj *obj,
11555255736Sdavidch                uint8_t                   set,
11556255736Sdavidch                int                       mac_type,
11557255736Sdavidch                unsigned long             *ramrod_flags)
11558255736Sdavidch{
11559255736Sdavidch    struct ecore_vlan_mac_ramrod_params ramrod_param;
11560255736Sdavidch    int rc;
11561255736Sdavidch
11562255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
11563255736Sdavidch
11564255736Sdavidch    /* fill in general parameters */
11565255736Sdavidch    ramrod_param.vlan_mac_obj = obj;
11566255736Sdavidch    ramrod_param.ramrod_flags = *ramrod_flags;
11567255736Sdavidch
11568255736Sdavidch    /* fill a user request section if needed */
11569255736Sdavidch    if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11570255736Sdavidch        memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11571255736Sdavidch
11572255736Sdavidch        bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11573255736Sdavidch
11574255736Sdavidch        /* Set the command: ADD or DEL */
11575255736Sdavidch        ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11576255736Sdavidch                                            ECORE_VLAN_MAC_DEL;
11577255736Sdavidch    }
11578255736Sdavidch
11579255736Sdavidch    rc = ecore_config_vlan_mac(sc, &ramrod_param);
11580255736Sdavidch
11581255736Sdavidch    if (rc == ECORE_EXISTS) {
11582255736Sdavidch        BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11583255736Sdavidch        /* do not treat adding same MAC as error */
11584255736Sdavidch        rc = 0;
11585255736Sdavidch    } else if (rc < 0) {
11586255736Sdavidch        BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11587255736Sdavidch    }
11588255736Sdavidch
11589255736Sdavidch    return (rc);
11590255736Sdavidch}
11591255736Sdavidch
11592255736Sdavidchstatic int
11593255736Sdavidchbxe_set_eth_mac(struct bxe_softc *sc,
11594255736Sdavidch                uint8_t          set)
11595255736Sdavidch{
11596255736Sdavidch    unsigned long ramrod_flags = 0;
11597255736Sdavidch
11598255736Sdavidch    BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11599255736Sdavidch
11600255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11601255736Sdavidch
11602255736Sdavidch    /* Eth MAC is set on RSS leading client (fp[0]) */
11603255736Sdavidch    return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11604255736Sdavidch                            &sc->sp_objs->mac_obj,
11605255736Sdavidch                            set, ECORE_ETH_MAC, &ramrod_flags));
11606255736Sdavidch}
11607255736Sdavidch
11608255736Sdavidchstatic int
11609255736Sdavidchbxe_get_cur_phy_idx(struct bxe_softc *sc)
11610255736Sdavidch{
11611255736Sdavidch    uint32_t sel_phy_idx = 0;
11612255736Sdavidch
11613255736Sdavidch    if (sc->link_params.num_phys <= 1) {
11614255736Sdavidch        return (ELINK_INT_PHY);
11615255736Sdavidch    }
11616255736Sdavidch
11617255736Sdavidch    if (sc->link_vars.link_up) {
11618255736Sdavidch        sel_phy_idx = ELINK_EXT_PHY1;
11619255736Sdavidch        /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11620255736Sdavidch        if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11621255736Sdavidch            (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11622255736Sdavidch             ELINK_SUPPORTED_FIBRE))
11623255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11624255736Sdavidch    } else {
11625255736Sdavidch        switch (elink_phy_selection(&sc->link_params)) {
11626255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11627255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11628255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11629255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY1;
11630255736Sdavidch               break;
11631255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11632255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11633255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY2;
11634255736Sdavidch               break;
11635255736Sdavidch        }
11636255736Sdavidch    }
11637255736Sdavidch
11638255736Sdavidch    return (sel_phy_idx);
11639255736Sdavidch}
11640255736Sdavidch
11641255736Sdavidchstatic int
11642255736Sdavidchbxe_get_link_cfg_idx(struct bxe_softc *sc)
11643255736Sdavidch{
11644255736Sdavidch    uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11645255736Sdavidch
11646255736Sdavidch    /*
11647255736Sdavidch     * The selected activated PHY is always after swapping (in case PHY
11648255736Sdavidch     * swapping is enabled). So when swapping is enabled, we need to reverse
11649255736Sdavidch     * the configuration
11650255736Sdavidch     */
11651255736Sdavidch
11652255736Sdavidch    if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11653255736Sdavidch        if (sel_phy_idx == ELINK_EXT_PHY1)
11654255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11655255736Sdavidch        else if (sel_phy_idx == ELINK_EXT_PHY2)
11656255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY1;
11657255736Sdavidch    }
11658255736Sdavidch
11659255736Sdavidch    return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11660255736Sdavidch}
11661255736Sdavidch
11662255736Sdavidchstatic void
11663255736Sdavidchbxe_set_requested_fc(struct bxe_softc *sc)
11664255736Sdavidch{
11665255736Sdavidch    /*
11666255736Sdavidch     * Initialize link parameters structure variables
11667255736Sdavidch     * It is recommended to turn off RX FC for jumbo frames
11668255736Sdavidch     * for better performance
11669255736Sdavidch     */
11670255736Sdavidch    if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11671255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11672255736Sdavidch    } else {
11673255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11674255736Sdavidch    }
11675255736Sdavidch}
11676255736Sdavidch
11677255736Sdavidchstatic void
11678255736Sdavidchbxe_calc_fc_adv(struct bxe_softc *sc)
11679255736Sdavidch{
11680255736Sdavidch    uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11681315881Sdavidcs
11682315881Sdavidcs
11683315881Sdavidcs    sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11684315881Sdavidcs                                           ADVERTISED_Pause);
11685315881Sdavidcs
11686255736Sdavidch    switch (sc->link_vars.ieee_fc &
11687255736Sdavidch            MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11688255736Sdavidch
11689255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11690255736Sdavidch        sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11691255736Sdavidch                                          ADVERTISED_Pause);
11692255736Sdavidch        break;
11693255736Sdavidch
11694255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11695255736Sdavidch        sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11696255736Sdavidch        break;
11697315881Sdavidcs
11698315881Sdavidcs    default:
11699315881Sdavidcs        break;
11700315881Sdavidcs
11701255736Sdavidch    }
11702255736Sdavidch}
11703255736Sdavidch
11704255736Sdavidchstatic uint16_t
11705255736Sdavidchbxe_get_mf_speed(struct bxe_softc *sc)
11706255736Sdavidch{
11707255736Sdavidch    uint16_t line_speed = sc->link_vars.line_speed;
11708255736Sdavidch    if (IS_MF(sc)) {
11709255736Sdavidch        uint16_t maxCfg =
11710255736Sdavidch            bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11711255736Sdavidch
11712255736Sdavidch        /* calculate the current MAX line speed limit for the MF devices */
11713255736Sdavidch        if (IS_MF_SI(sc)) {
11714255736Sdavidch            line_speed = (line_speed * maxCfg) / 100;
11715255736Sdavidch        } else { /* SD mode */
11716255736Sdavidch            uint16_t vn_max_rate = maxCfg * 100;
11717255736Sdavidch
11718255736Sdavidch            if (vn_max_rate < line_speed) {
11719255736Sdavidch                line_speed = vn_max_rate;
11720255736Sdavidch            }
11721255736Sdavidch        }
11722255736Sdavidch    }
11723255736Sdavidch
11724255736Sdavidch    return (line_speed);
11725255736Sdavidch}
11726255736Sdavidch
11727255736Sdavidchstatic void
11728255736Sdavidchbxe_fill_report_data(struct bxe_softc            *sc,
11729255736Sdavidch                     struct bxe_link_report_data *data)
11730255736Sdavidch{
11731255736Sdavidch    uint16_t line_speed = bxe_get_mf_speed(sc);
11732255736Sdavidch
11733255736Sdavidch    memset(data, 0, sizeof(*data));
11734255736Sdavidch
11735255736Sdavidch    /* fill the report data with the effective line speed */
11736255736Sdavidch    data->line_speed = line_speed;
11737255736Sdavidch
11738255736Sdavidch    /* Link is down */
11739255736Sdavidch    if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11740255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11741255736Sdavidch    }
11742255736Sdavidch
11743255736Sdavidch    /* Full DUPLEX */
11744255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
11745255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11746255736Sdavidch    }
11747255736Sdavidch
11748255736Sdavidch    /* Rx Flow Control is ON */
11749255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11750255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11751255736Sdavidch    }
11752255736Sdavidch
11753255736Sdavidch    /* Tx Flow Control is ON */
11754255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11755255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11756255736Sdavidch    }
11757255736Sdavidch}
11758255736Sdavidch
11759255736Sdavidch/* report link status to OS, should be called under phy_lock */
11760255736Sdavidchstatic void
11761255736Sdavidchbxe_link_report_locked(struct bxe_softc *sc)
11762255736Sdavidch{
11763255736Sdavidch    struct bxe_link_report_data cur_data;
11764255736Sdavidch
11765255736Sdavidch    /* reread mf_cfg */
11766255736Sdavidch    if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11767255736Sdavidch        bxe_read_mf_cfg(sc);
11768255736Sdavidch    }
11769255736Sdavidch
11770255736Sdavidch    /* Read the current link report info */
11771255736Sdavidch    bxe_fill_report_data(sc, &cur_data);
11772255736Sdavidch
11773255736Sdavidch    /* Don't report link down or exactly the same link status twice */
11774255736Sdavidch    if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11775255736Sdavidch        (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11776255736Sdavidch                      &sc->last_reported_link.link_report_flags) &&
11777255736Sdavidch         bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11778255736Sdavidch                      &cur_data.link_report_flags))) {
11779255736Sdavidch        return;
11780255736Sdavidch    }
11781255736Sdavidch
11782315881Sdavidcs	ELINK_DEBUG_P2(sc, "Change in link status : cur_data = %x, last_reported_link = %x\n",
11783315881Sdavidcs					cur_data.link_report_flags, sc->last_reported_link.link_report_flags);
11784255736Sdavidch    sc->link_cnt++;
11785255736Sdavidch
11786315881Sdavidcs	ELINK_DEBUG_P1(sc, "link status change count = %x\n", sc->link_cnt);
11787255736Sdavidch    /* report new link params and remember the state for the next time */
11788255736Sdavidch    memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11789255736Sdavidch
11790255736Sdavidch    if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11791255736Sdavidch                     &cur_data.link_report_flags)) {
11792270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_DOWN);
11793255736Sdavidch    } else {
11794255736Sdavidch        const char *duplex;
11795255736Sdavidch        const char *flow;
11796255736Sdavidch
11797255736Sdavidch        if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11798255736Sdavidch                                   &cur_data.link_report_flags)) {
11799255736Sdavidch            duplex = "full";
11800315881Sdavidcs			ELINK_DEBUG_P0(sc, "link set to full duplex\n");
11801255736Sdavidch        } else {
11802255736Sdavidch            duplex = "half";
11803315881Sdavidcs			ELINK_DEBUG_P0(sc, "link set to half duplex\n");
11804255736Sdavidch        }
11805255736Sdavidch
11806255736Sdavidch        /*
11807255736Sdavidch         * Handle the FC at the end so that only these flags would be
11808255736Sdavidch         * possibly set. This way we may easily check if there is no FC
11809255736Sdavidch         * enabled.
11810255736Sdavidch         */
11811255736Sdavidch        if (cur_data.link_report_flags) {
11812255736Sdavidch            if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11813255736Sdavidch                             &cur_data.link_report_flags) &&
11814255736Sdavidch                bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11815255736Sdavidch                             &cur_data.link_report_flags)) {
11816255736Sdavidch                flow = "ON - receive & transmit";
11817255736Sdavidch            } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11818255736Sdavidch                                    &cur_data.link_report_flags) &&
11819255736Sdavidch                       !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11820255736Sdavidch                                     &cur_data.link_report_flags)) {
11821255736Sdavidch                flow = "ON - receive";
11822255736Sdavidch            } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11823255736Sdavidch                                     &cur_data.link_report_flags) &&
11824255736Sdavidch                       bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11825255736Sdavidch                                    &cur_data.link_report_flags)) {
11826255736Sdavidch                flow = "ON - transmit";
11827255736Sdavidch            } else {
11828255736Sdavidch                flow = "none"; /* possible? */
11829255736Sdavidch            }
11830255736Sdavidch        } else {
11831255736Sdavidch            flow = "none";
11832255736Sdavidch        }
11833255736Sdavidch
11834270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_UP);
11835255736Sdavidch        BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11836255736Sdavidch              cur_data.line_speed, duplex, flow);
11837255736Sdavidch    }
11838255736Sdavidch}
11839255736Sdavidch
11840255736Sdavidchstatic void
11841255736Sdavidchbxe_link_report(struct bxe_softc *sc)
11842255736Sdavidch{
11843284335Sdavidcs    bxe_acquire_phy_lock(sc);
11844255736Sdavidch    bxe_link_report_locked(sc);
11845284335Sdavidcs    bxe_release_phy_lock(sc);
11846255736Sdavidch}
11847255736Sdavidch
11848255736Sdavidchstatic void
11849255736Sdavidchbxe_link_status_update(struct bxe_softc *sc)
11850255736Sdavidch{
11851255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
11852255736Sdavidch        return;
11853255736Sdavidch    }
11854255736Sdavidch
11855255736Sdavidch    if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11856255736Sdavidch        elink_link_status_update(&sc->link_params, &sc->link_vars);
11857255736Sdavidch    } else {
11858255736Sdavidch        sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11859255736Sdavidch                                  ELINK_SUPPORTED_10baseT_Full |
11860255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Half |
11861255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Full |
11862255736Sdavidch                                  ELINK_SUPPORTED_1000baseT_Full |
11863255736Sdavidch                                  ELINK_SUPPORTED_2500baseX_Full |
11864255736Sdavidch                                  ELINK_SUPPORTED_10000baseT_Full |
11865255736Sdavidch                                  ELINK_SUPPORTED_TP |
11866255736Sdavidch                                  ELINK_SUPPORTED_FIBRE |
11867255736Sdavidch                                  ELINK_SUPPORTED_Autoneg |
11868255736Sdavidch                                  ELINK_SUPPORTED_Pause |
11869255736Sdavidch                                  ELINK_SUPPORTED_Asym_Pause);
11870255736Sdavidch        sc->port.advertising[0] = sc->port.supported[0];
11871255736Sdavidch
11872255736Sdavidch        sc->link_params.sc                = sc;
11873255736Sdavidch        sc->link_params.port              = SC_PORT(sc);
11874255736Sdavidch        sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11875255736Sdavidch        sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11876255736Sdavidch        sc->link_params.req_line_speed[0] = SPEED_10000;
11877255736Sdavidch        sc->link_params.speed_cap_mask[0] = 0x7f0000;
11878255736Sdavidch        sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11879255736Sdavidch
11880255736Sdavidch        if (CHIP_REV_IS_FPGA(sc)) {
11881255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11882255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_1000;
11883255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11884255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11885255736Sdavidch        } else {
11886255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11887255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_10000;
11888255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11889255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11890255736Sdavidch        }
11891255736Sdavidch
11892255736Sdavidch        sc->link_vars.link_up = 1;
11893255736Sdavidch
11894255736Sdavidch        sc->link_vars.duplex    = DUPLEX_FULL;
11895255736Sdavidch        sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11896255736Sdavidch
11897255736Sdavidch        if (IS_PF(sc)) {
11898255736Sdavidch            REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11899255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11900255736Sdavidch            bxe_link_report(sc);
11901255736Sdavidch        }
11902255736Sdavidch    }
11903255736Sdavidch
11904255736Sdavidch    if (IS_PF(sc)) {
11905255736Sdavidch        if (sc->link_vars.link_up) {
11906255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11907255736Sdavidch        } else {
11908255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_STOP);
11909255736Sdavidch        }
11910255736Sdavidch        bxe_link_report(sc);
11911255736Sdavidch    } else {
11912255736Sdavidch        bxe_link_report(sc);
11913255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11914255736Sdavidch    }
11915255736Sdavidch}
11916255736Sdavidch
11917255736Sdavidchstatic int
11918255736Sdavidchbxe_initial_phy_init(struct bxe_softc *sc,
11919255736Sdavidch                     int              load_mode)
11920255736Sdavidch{
11921255736Sdavidch    int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
11922255736Sdavidch    uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
11923255736Sdavidch    struct elink_params *lp = &sc->link_params;
11924255736Sdavidch
11925255736Sdavidch    bxe_set_requested_fc(sc);
11926255736Sdavidch
11927255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
11928255736Sdavidch        uint32_t bond = CHIP_BOND_ID(sc);
11929255736Sdavidch        uint32_t feat = 0;
11930255736Sdavidch
11931255736Sdavidch        if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
11932255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11933255736Sdavidch        } else if (bond & 0x4) {
11934255736Sdavidch            if (CHIP_IS_E3(sc)) {
11935255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
11936255736Sdavidch            } else {
11937255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11938255736Sdavidch            }
11939255736Sdavidch        } else if (bond & 0x8) {
11940255736Sdavidch            if (CHIP_IS_E3(sc)) {
11941255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
11942255736Sdavidch            } else {
11943255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11944255736Sdavidch            }
11945255736Sdavidch        }
11946255736Sdavidch
11947255736Sdavidch        /* disable EMAC for E3 and above */
11948255736Sdavidch        if (bond & 0x2) {
11949255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11950255736Sdavidch        }
11951255736Sdavidch
11952255736Sdavidch        sc->link_params.feature_config_flags |= feat;
11953255736Sdavidch    }
11954255736Sdavidch
11955284335Sdavidcs    bxe_acquire_phy_lock(sc);
11956255736Sdavidch
11957255736Sdavidch    if (load_mode == LOAD_DIAG) {
11958255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_XGXS;
11959255736Sdavidch        /* Prefer doing PHY loopback at 10G speed, if possible */
11960255736Sdavidch        if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
11961255736Sdavidch            if (lp->speed_cap_mask[cfg_idx] &
11962255736Sdavidch                PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
11963255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
11964255736Sdavidch            } else {
11965255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
11966255736Sdavidch            }
11967255736Sdavidch        }
11968255736Sdavidch    }
11969255736Sdavidch
11970255736Sdavidch    if (load_mode == LOAD_LOOPBACK_EXT) {
11971255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_EXT;
11972255736Sdavidch    }
11973255736Sdavidch
11974255736Sdavidch    rc = elink_phy_init(&sc->link_params, &sc->link_vars);
11975255736Sdavidch
11976284335Sdavidcs    bxe_release_phy_lock(sc);
11977255736Sdavidch
11978255736Sdavidch    bxe_calc_fc_adv(sc);
11979255736Sdavidch
11980255736Sdavidch    if (sc->link_vars.link_up) {
11981255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11982255736Sdavidch        bxe_link_report(sc);
11983255736Sdavidch    }
11984255736Sdavidch
11985255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
11986255736Sdavidch        bxe_periodic_start(sc);
11987255736Sdavidch    }
11988255736Sdavidch
11989255736Sdavidch    sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
11990255736Sdavidch    return (rc);
11991255736Sdavidch}
11992255736Sdavidch
11993255736Sdavidch/* must be called under IF_ADDR_LOCK */
11994284651Sdavidcs
11995255736Sdavidchstatic int
11996284651Sdavidcsbxe_set_mc_list(struct bxe_softc *sc)
11997255736Sdavidch{
11998284651Sdavidcs    struct ecore_mcast_ramrod_params rparam = { NULL };
11999284651Sdavidcs    int rc = 0;
12000255736Sdavidch    int mc_count = 0;
12001266979Smarcel    int mcnt, i;
12002284651Sdavidcs    struct ecore_mcast_list_elem *mc_mac, *mc_mac_start;
12003266979Smarcel    unsigned char *mta;
12004284651Sdavidcs    if_t ifp = sc->ifp;
12005255736Sdavidch
12006266979Smarcel    mc_count = if_multiaddr_count(ifp, -1);/* XXX they don't have a limit */
12007284651Sdavidcs    if (!mc_count)
12008255736Sdavidch        return (0);
12009255736Sdavidch
12010266979Smarcel    mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN *
12011266979Smarcel            mc_count, M_DEVBUF, M_NOWAIT);
12012266979Smarcel
12013266979Smarcel    if(mta == NULL) {
12014266979Smarcel        BLOGE(sc, "Failed to allocate temp mcast list\n");
12015266979Smarcel        return (-1);
12016266979Smarcel    }
12017284470Sdavidcs    bzero(mta, (sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count));
12018266979Smarcel
12019284651Sdavidcs    mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF, (M_NOWAIT | M_ZERO));
12020284651Sdavidcs    mc_mac_start = mc_mac;
12021284651Sdavidcs
12022255736Sdavidch    if (!mc_mac) {
12023266979Smarcel        free(mta, M_DEVBUF);
12024255736Sdavidch        BLOGE(sc, "Failed to allocate temp mcast list\n");
12025255736Sdavidch        return (-1);
12026255736Sdavidch    }
12027284470Sdavidcs    bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12028255736Sdavidch
12029284651Sdavidcs    /* mta and mcnt not expected to be  different */
12030284651Sdavidcs    if_multiaddr_array(ifp, mta, &mcnt, mc_count);
12031284651Sdavidcs
12032284651Sdavidcs
12033284651Sdavidcs    rparam.mcast_obj = &sc->mcast_obj;
12034284651Sdavidcs    ECORE_LIST_INIT(&rparam.mcast_list);
12035284651Sdavidcs
12036266979Smarcel    for(i=0; i< mcnt; i++) {
12037255736Sdavidch
12038284651Sdavidcs        mc_mac->mac = (uint8_t *)(mta + (i * ETHER_ADDR_LEN));
12039284651Sdavidcs        ECORE_LIST_PUSH_TAIL(&mc_mac->link, &rparam.mcast_list);
12040255736Sdavidch
12041255736Sdavidch        BLOGD(sc, DBG_LOAD,
12042255736Sdavidch              "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
12043255736Sdavidch              mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12044255736Sdavidch              mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
12045255736Sdavidch
12046255736Sdavidch        mc_mac++;
12047255736Sdavidch    }
12048284651Sdavidcs    rparam.mcast_list_len = mc_count;
12049255736Sdavidch
12050255736Sdavidch    BXE_MCAST_LOCK(sc);
12051255736Sdavidch
12052255736Sdavidch    /* first, clear all configured multicast MACs */
12053255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12054255736Sdavidch    if (rc < 0) {
12055255736Sdavidch        BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12056284470Sdavidcs        BXE_MCAST_UNLOCK(sc);
12057284651Sdavidcs    	free(mc_mac_start, M_DEVBUF);
12058284651Sdavidcs        free(mta, M_DEVBUF);
12059255736Sdavidch        return (rc);
12060255736Sdavidch    }
12061255736Sdavidch
12062255736Sdavidch    /* Now add the new MACs */
12063255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12064255736Sdavidch    if (rc < 0) {
12065255736Sdavidch        BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12066255736Sdavidch    }
12067255736Sdavidch
12068255736Sdavidch    BXE_MCAST_UNLOCK(sc);
12069255736Sdavidch
12070284651Sdavidcs    free(mc_mac_start, M_DEVBUF);
12071284651Sdavidcs    free(mta, M_DEVBUF);
12072284651Sdavidcs
12073255736Sdavidch    return (rc);
12074255736Sdavidch}
12075255736Sdavidch
12076255736Sdavidchstatic int
12077255736Sdavidchbxe_set_uc_list(struct bxe_softc *sc)
12078255736Sdavidch{
12079266979Smarcel    if_t ifp = sc->ifp;
12080255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12081255736Sdavidch    struct ifaddr *ifa;
12082255736Sdavidch    unsigned long ramrod_flags = 0;
12083255736Sdavidch    int rc;
12084255736Sdavidch
12085255736Sdavidch#if __FreeBSD_version < 800000
12086255736Sdavidch    IF_ADDR_LOCK(ifp);
12087255736Sdavidch#else
12088270876Sglebius    if_addr_rlock(ifp);
12089255736Sdavidch#endif
12090255736Sdavidch
12091255736Sdavidch    /* first schedule a cleanup up of old configuration */
12092255736Sdavidch    rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12093255736Sdavidch    if (rc < 0) {
12094255736Sdavidch        BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12095255736Sdavidch#if __FreeBSD_version < 800000
12096255736Sdavidch        IF_ADDR_UNLOCK(ifp);
12097255736Sdavidch#else
12098270876Sglebius        if_addr_runlock(ifp);
12099255736Sdavidch#endif
12100255736Sdavidch        return (rc);
12101255736Sdavidch    }
12102255736Sdavidch
12103266979Smarcel    ifa = if_getifaddr(ifp); /* XXX Is this structure */
12104255736Sdavidch    while (ifa) {
12105255736Sdavidch        if (ifa->ifa_addr->sa_family != AF_LINK) {
12106255736Sdavidch            ifa = TAILQ_NEXT(ifa, ifa_link);
12107255736Sdavidch            continue;
12108255736Sdavidch        }
12109255736Sdavidch
12110255736Sdavidch        rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12111255736Sdavidch                             mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12112255736Sdavidch        if (rc == -EEXIST) {
12113255736Sdavidch            BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12114255736Sdavidch            /* do not treat adding same MAC as an error */
12115255736Sdavidch            rc = 0;
12116255736Sdavidch        } else if (rc < 0) {
12117255736Sdavidch            BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12118255736Sdavidch#if __FreeBSD_version < 800000
12119255736Sdavidch            IF_ADDR_UNLOCK(ifp);
12120255736Sdavidch#else
12121270876Sglebius            if_addr_runlock(ifp);
12122255736Sdavidch#endif
12123255736Sdavidch            return (rc);
12124255736Sdavidch        }
12125255736Sdavidch
12126255736Sdavidch        ifa = TAILQ_NEXT(ifa, ifa_link);
12127255736Sdavidch    }
12128255736Sdavidch
12129255736Sdavidch#if __FreeBSD_version < 800000
12130255736Sdavidch    IF_ADDR_UNLOCK(ifp);
12131255736Sdavidch#else
12132270876Sglebius    if_addr_runlock(ifp);
12133255736Sdavidch#endif
12134255736Sdavidch
12135255736Sdavidch    /* Execute the pending commands */
12136255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
12137255736Sdavidch    return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12138255736Sdavidch                            ECORE_UC_LIST_MAC, &ramrod_flags));
12139255736Sdavidch}
12140255736Sdavidch
12141255736Sdavidchstatic void
12142284335Sdavidcsbxe_set_rx_mode(struct bxe_softc *sc)
12143255736Sdavidch{
12144266979Smarcel    if_t ifp = sc->ifp;
12145255736Sdavidch    uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12146255736Sdavidch
12147255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
12148255736Sdavidch        BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12149255736Sdavidch        return;
12150255736Sdavidch    }
12151255736Sdavidch
12152266979Smarcel    BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12153255736Sdavidch
12154266979Smarcel    if (if_getflags(ifp) & IFF_PROMISC) {
12155255736Sdavidch        rx_mode = BXE_RX_MODE_PROMISC;
12156266979Smarcel    } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12157266979Smarcel               ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12158255736Sdavidch                CHIP_IS_E1(sc))) {
12159255736Sdavidch        rx_mode = BXE_RX_MODE_ALLMULTI;
12160255736Sdavidch    } else {
12161255736Sdavidch        if (IS_PF(sc)) {
12162255736Sdavidch            /* some multicasts */
12163255736Sdavidch            if (bxe_set_mc_list(sc) < 0) {
12164255736Sdavidch                rx_mode = BXE_RX_MODE_ALLMULTI;
12165255736Sdavidch            }
12166255736Sdavidch            if (bxe_set_uc_list(sc) < 0) {
12167255736Sdavidch                rx_mode = BXE_RX_MODE_PROMISC;
12168255736Sdavidch            }
12169255736Sdavidch        }
12170255736Sdavidch    }
12171255736Sdavidch
12172255736Sdavidch    sc->rx_mode = rx_mode;
12173255736Sdavidch
12174255736Sdavidch    /* schedule the rx_mode command */
12175255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12176255736Sdavidch        BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12177255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12178255736Sdavidch        return;
12179255736Sdavidch    }
12180255736Sdavidch
12181255736Sdavidch    if (IS_PF(sc)) {
12182255736Sdavidch        bxe_set_storm_rx_mode(sc);
12183255736Sdavidch    }
12184255736Sdavidch}
12185255736Sdavidch
12186255736Sdavidch
12187255736Sdavidch/* update flags in shmem */
12188255736Sdavidchstatic void
12189255736Sdavidchbxe_update_drv_flags(struct bxe_softc *sc,
12190255736Sdavidch                     uint32_t         flags,
12191255736Sdavidch                     uint32_t         set)
12192255736Sdavidch{
12193255736Sdavidch    uint32_t drv_flags;
12194255736Sdavidch
12195255736Sdavidch    if (SHMEM2_HAS(sc, drv_flags)) {
12196255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12197255736Sdavidch        drv_flags = SHMEM2_RD(sc, drv_flags);
12198255736Sdavidch
12199255736Sdavidch        if (set) {
12200255736Sdavidch            SET_FLAGS(drv_flags, flags);
12201255736Sdavidch        } else {
12202255736Sdavidch            RESET_FLAGS(drv_flags, flags);
12203255736Sdavidch        }
12204255736Sdavidch
12205255736Sdavidch        SHMEM2_WR(sc, drv_flags, drv_flags);
12206255736Sdavidch        BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12207255736Sdavidch
12208255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12209255736Sdavidch    }
12210255736Sdavidch}
12211255736Sdavidch
12212255736Sdavidch/* periodic timer callout routine, only runs when the interface is up */
12213255736Sdavidch
12214255736Sdavidchstatic void
12215255736Sdavidchbxe_periodic_callout_func(void *xsc)
12216255736Sdavidch{
12217255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12218255736Sdavidch    int i;
12219255736Sdavidch
12220255736Sdavidch    if (!BXE_CORE_TRYLOCK(sc)) {
12221255736Sdavidch        /* just bail and try again next time */
12222255736Sdavidch
12223255736Sdavidch        if ((sc->state == BXE_STATE_OPEN) &&
12224255736Sdavidch            (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12225255736Sdavidch            /* schedule the next periodic callout */
12226255736Sdavidch            callout_reset(&sc->periodic_callout, hz,
12227255736Sdavidch                          bxe_periodic_callout_func, sc);
12228255736Sdavidch        }
12229255736Sdavidch
12230255736Sdavidch        return;
12231255736Sdavidch    }
12232255736Sdavidch
12233255736Sdavidch    if ((sc->state != BXE_STATE_OPEN) ||
12234255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12235255736Sdavidch        BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12236255736Sdavidch        BXE_CORE_UNLOCK(sc);
12237255736Sdavidch        return;
12238297873Sdavidcs        }
12239297873Sdavidcs
12240297873Sdavidcs
12241255736Sdavidch    /* Check for TX timeouts on any fastpath. */
12242255736Sdavidch    FOR_EACH_QUEUE(sc, i) {
12243255736Sdavidch        if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12244255736Sdavidch            /* Ruh-Roh, chip was reset! */
12245255736Sdavidch            break;
12246255736Sdavidch        }
12247255736Sdavidch    }
12248255736Sdavidch
12249255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12250255736Sdavidch        /*
12251255736Sdavidch         * This barrier is needed to ensure the ordering between the writing
12252255736Sdavidch         * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12253255736Sdavidch         * the reading here.
12254255736Sdavidch         */
12255255736Sdavidch        mb();
12256255736Sdavidch        if (sc->port.pmf) {
12257284335Sdavidcs	    bxe_acquire_phy_lock(sc);
12258255736Sdavidch            elink_period_func(&sc->link_params, &sc->link_vars);
12259284335Sdavidcs	    bxe_release_phy_lock(sc);
12260255736Sdavidch        }
12261255736Sdavidch    }
12262255736Sdavidch
12263284335Sdavidcs    if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12264255736Sdavidch        int mb_idx = SC_FW_MB_IDX(sc);
12265255736Sdavidch        uint32_t drv_pulse;
12266255736Sdavidch        uint32_t mcp_pulse;
12267255736Sdavidch
12268255736Sdavidch        ++sc->fw_drv_pulse_wr_seq;
12269255736Sdavidch        sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12270255736Sdavidch
12271255736Sdavidch        drv_pulse = sc->fw_drv_pulse_wr_seq;
12272255736Sdavidch        bxe_drv_pulse(sc);
12273255736Sdavidch
12274255736Sdavidch        mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12275255736Sdavidch                     MCP_PULSE_SEQ_MASK);
12276255736Sdavidch
12277255736Sdavidch        /*
12278255736Sdavidch         * The delta between driver pulse and mcp response should
12279255736Sdavidch         * be 1 (before mcp response) or 0 (after mcp response).
12280255736Sdavidch         */
12281255736Sdavidch        if ((drv_pulse != mcp_pulse) &&
12282255736Sdavidch            (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12283255736Sdavidch            /* someone lost a heartbeat... */
12284255736Sdavidch            BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12285255736Sdavidch                  drv_pulse, mcp_pulse);
12286255736Sdavidch        }
12287255736Sdavidch    }
12288255736Sdavidch
12289255736Sdavidch    /* state is BXE_STATE_OPEN */
12290255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12291255736Sdavidch
12292255736Sdavidch    BXE_CORE_UNLOCK(sc);
12293255736Sdavidch
12294255736Sdavidch    if ((sc->state == BXE_STATE_OPEN) &&
12295255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12296255736Sdavidch        /* schedule the next periodic callout */
12297255736Sdavidch        callout_reset(&sc->periodic_callout, hz,
12298255736Sdavidch                      bxe_periodic_callout_func, sc);
12299255736Sdavidch    }
12300255736Sdavidch}
12301255736Sdavidch
12302255736Sdavidchstatic void
12303255736Sdavidchbxe_periodic_start(struct bxe_softc *sc)
12304255736Sdavidch{
12305255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12306255736Sdavidch    callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12307255736Sdavidch}
12308255736Sdavidch
12309255736Sdavidchstatic void
12310255736Sdavidchbxe_periodic_stop(struct bxe_softc *sc)
12311255736Sdavidch{
12312255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12313255736Sdavidch    callout_drain(&sc->periodic_callout);
12314255736Sdavidch}
12315255736Sdavidch
12316255736Sdavidch/* start the controller */
12317255736Sdavidchstatic __noinline int
12318255736Sdavidchbxe_nic_load(struct bxe_softc *sc,
12319255736Sdavidch             int              load_mode)
12320255736Sdavidch{
12321255736Sdavidch    uint32_t val;
12322255736Sdavidch    int load_code = 0;
12323255736Sdavidch    int i, rc = 0;
12324255736Sdavidch
12325255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12326255736Sdavidch
12327255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12328255736Sdavidch
12329255736Sdavidch    sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12330255736Sdavidch
12331255736Sdavidch    if (IS_PF(sc)) {
12332255736Sdavidch        /* must be called before memory allocation and HW init */
12333255736Sdavidch        bxe_ilt_set_info(sc);
12334255736Sdavidch    }
12335255736Sdavidch
12336255736Sdavidch    sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12337255736Sdavidch
12338255736Sdavidch    bxe_set_fp_rx_buf_size(sc);
12339255736Sdavidch
12340255736Sdavidch    if (bxe_alloc_fp_buffers(sc) != 0) {
12341255736Sdavidch        BLOGE(sc, "Failed to allocate fastpath memory\n");
12342255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12343255736Sdavidch        rc = ENOMEM;
12344255736Sdavidch        goto bxe_nic_load_error0;
12345255736Sdavidch    }
12346255736Sdavidch
12347255736Sdavidch    if (bxe_alloc_mem(sc) != 0) {
12348255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12349255736Sdavidch        rc = ENOMEM;
12350255736Sdavidch        goto bxe_nic_load_error0;
12351255736Sdavidch    }
12352255736Sdavidch
12353255736Sdavidch    if (bxe_alloc_fw_stats_mem(sc) != 0) {
12354255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12355255736Sdavidch        rc = ENOMEM;
12356255736Sdavidch        goto bxe_nic_load_error0;
12357255736Sdavidch    }
12358255736Sdavidch
12359255736Sdavidch    if (IS_PF(sc)) {
12360255736Sdavidch        /* set pf load just before approaching the MCP */
12361255736Sdavidch        bxe_set_pf_load(sc);
12362255736Sdavidch
12363255736Sdavidch        /* if MCP exists send load request and analyze response */
12364255736Sdavidch        if (!BXE_NOMCP(sc)) {
12365255736Sdavidch            /* attempt to load pf */
12366255736Sdavidch            if (bxe_nic_load_request(sc, &load_code) != 0) {
12367255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12368255736Sdavidch                rc = ENXIO;
12369255736Sdavidch                goto bxe_nic_load_error1;
12370255736Sdavidch            }
12371255736Sdavidch
12372255736Sdavidch            /* what did the MCP say? */
12373255736Sdavidch            if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12374255736Sdavidch                bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12375255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12376255736Sdavidch                rc = ENXIO;
12377255736Sdavidch                goto bxe_nic_load_error2;
12378255736Sdavidch            }
12379255736Sdavidch        } else {
12380255736Sdavidch            BLOGI(sc, "Device has no MCP!\n");
12381255736Sdavidch            load_code = bxe_nic_load_no_mcp(sc);
12382255736Sdavidch        }
12383255736Sdavidch
12384255736Sdavidch        /* mark PMF if applicable */
12385255736Sdavidch        bxe_nic_load_pmf(sc, load_code);
12386255736Sdavidch
12387255736Sdavidch        /* Init Function state controlling object */
12388255736Sdavidch        bxe_init_func_obj(sc);
12389255736Sdavidch
12390255736Sdavidch        /* Initialize HW */
12391255736Sdavidch        if (bxe_init_hw(sc, load_code) != 0) {
12392255736Sdavidch            BLOGE(sc, "HW init failed\n");
12393255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12394255736Sdavidch            sc->state = BXE_STATE_CLOSED;
12395255736Sdavidch            rc = ENXIO;
12396255736Sdavidch            goto bxe_nic_load_error2;
12397255736Sdavidch        }
12398255736Sdavidch    }
12399255736Sdavidch
12400284335Sdavidcs    /* set ALWAYS_ALIVE bit in shmem */
12401284335Sdavidcs    sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12402284335Sdavidcs    bxe_drv_pulse(sc);
12403284335Sdavidcs    sc->flags |= BXE_NO_PULSE;
12404284335Sdavidcs
12405255736Sdavidch    /* attach interrupts */
12406255736Sdavidch    if (bxe_interrupt_attach(sc) != 0) {
12407255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12408255736Sdavidch        rc = ENXIO;
12409255736Sdavidch        goto bxe_nic_load_error2;
12410255736Sdavidch    }
12411255736Sdavidch
12412255736Sdavidch    bxe_nic_init(sc, load_code);
12413255736Sdavidch
12414255736Sdavidch    /* Init per-function objects */
12415255736Sdavidch    if (IS_PF(sc)) {
12416255736Sdavidch        bxe_init_objs(sc);
12417255736Sdavidch        // XXX bxe_iov_nic_init(sc);
12418255736Sdavidch
12419255736Sdavidch        /* set AFEX default VLAN tag to an invalid value */
12420255736Sdavidch        sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12421255736Sdavidch        // XXX bxe_nic_load_afex_dcc(sc, load_code);
12422255736Sdavidch
12423255736Sdavidch        sc->state = BXE_STATE_OPENING_WAITING_PORT;
12424255736Sdavidch        rc = bxe_func_start(sc);
12425255736Sdavidch        if (rc) {
12426295830Sdavidcs            BLOGE(sc, "Function start failed! rc = %d\n", rc);
12427255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12428255736Sdavidch            sc->state = BXE_STATE_ERROR;
12429255736Sdavidch            goto bxe_nic_load_error3;
12430255736Sdavidch        }
12431255736Sdavidch
12432255736Sdavidch        /* send LOAD_DONE command to MCP */
12433255736Sdavidch        if (!BXE_NOMCP(sc)) {
12434255736Sdavidch            load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12435255736Sdavidch            if (!load_code) {
12436255736Sdavidch                BLOGE(sc, "MCP response failure, aborting\n");
12437255736Sdavidch                sc->state = BXE_STATE_ERROR;
12438255736Sdavidch                rc = ENXIO;
12439255736Sdavidch                goto bxe_nic_load_error3;
12440255736Sdavidch            }
12441255736Sdavidch        }
12442255736Sdavidch
12443255736Sdavidch        rc = bxe_setup_leading(sc);
12444255736Sdavidch        if (rc) {
12445295830Sdavidcs            BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12446255736Sdavidch            sc->state = BXE_STATE_ERROR;
12447255736Sdavidch            goto bxe_nic_load_error3;
12448255736Sdavidch        }
12449255736Sdavidch
12450255736Sdavidch        FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12451255736Sdavidch            rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12452255736Sdavidch            if (rc) {
12453295830Sdavidcs                BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12454255736Sdavidch                sc->state = BXE_STATE_ERROR;
12455255736Sdavidch                goto bxe_nic_load_error3;
12456255736Sdavidch            }
12457255736Sdavidch        }
12458255736Sdavidch
12459255736Sdavidch        rc = bxe_init_rss_pf(sc);
12460255736Sdavidch        if (rc) {
12461255736Sdavidch            BLOGE(sc, "PF RSS init failed\n");
12462255736Sdavidch            sc->state = BXE_STATE_ERROR;
12463255736Sdavidch            goto bxe_nic_load_error3;
12464255736Sdavidch        }
12465255736Sdavidch    }
12466255736Sdavidch    /* XXX VF */
12467255736Sdavidch
12468255736Sdavidch    /* now when Clients are configured we are ready to work */
12469255736Sdavidch    sc->state = BXE_STATE_OPEN;
12470255736Sdavidch
12471255736Sdavidch    /* Configure a ucast MAC */
12472255736Sdavidch    if (IS_PF(sc)) {
12473255736Sdavidch        rc = bxe_set_eth_mac(sc, TRUE);
12474255736Sdavidch    }
12475255736Sdavidch    if (rc) {
12476295830Sdavidcs        BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12477255736Sdavidch        sc->state = BXE_STATE_ERROR;
12478255736Sdavidch        goto bxe_nic_load_error3;
12479255736Sdavidch    }
12480255736Sdavidch
12481255736Sdavidch    if (sc->port.pmf) {
12482255736Sdavidch        rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12483255736Sdavidch        if (rc) {
12484255736Sdavidch            sc->state = BXE_STATE_ERROR;
12485255736Sdavidch            goto bxe_nic_load_error3;
12486255736Sdavidch        }
12487255736Sdavidch    }
12488255736Sdavidch
12489255736Sdavidch    sc->link_params.feature_config_flags &=
12490255736Sdavidch        ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12491255736Sdavidch
12492255736Sdavidch    /* start fast path */
12493255736Sdavidch
12494255736Sdavidch    /* Initialize Rx filter */
12495255736Sdavidch    bxe_set_rx_mode(sc);
12496255736Sdavidch
12497255736Sdavidch    /* start the Tx */
12498255736Sdavidch    switch (/* XXX load_mode */LOAD_OPEN) {
12499255736Sdavidch    case LOAD_NORMAL:
12500255736Sdavidch    case LOAD_OPEN:
12501255736Sdavidch        break;
12502255736Sdavidch
12503255736Sdavidch    case LOAD_DIAG:
12504255736Sdavidch    case LOAD_LOOPBACK_EXT:
12505255736Sdavidch        sc->state = BXE_STATE_DIAG;
12506255736Sdavidch        break;
12507255736Sdavidch
12508255736Sdavidch    default:
12509255736Sdavidch        break;
12510255736Sdavidch    }
12511255736Sdavidch
12512255736Sdavidch    if (sc->port.pmf) {
12513255736Sdavidch        bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12514255736Sdavidch    } else {
12515255736Sdavidch        bxe_link_status_update(sc);
12516255736Sdavidch    }
12517255736Sdavidch
12518255736Sdavidch    /* start the periodic timer callout */
12519255736Sdavidch    bxe_periodic_start(sc);
12520255736Sdavidch
12521255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12522255736Sdavidch        /* mark driver is loaded in shmem2 */
12523255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12524255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12525255736Sdavidch                  (val |
12526255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12527255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_L2));
12528255736Sdavidch    }
12529255736Sdavidch
12530255736Sdavidch    /* wait for all pending SP commands to complete */
12531255736Sdavidch    if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12532255736Sdavidch        BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12533255736Sdavidch        bxe_periodic_stop(sc);
12534255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12535255736Sdavidch        return (ENXIO);
12536255736Sdavidch    }
12537255736Sdavidch
12538255736Sdavidch    /* Tell the stack the driver is running! */
12539266979Smarcel    if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
12540255736Sdavidch
12541255736Sdavidch    BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12542255736Sdavidch
12543255736Sdavidch    return (0);
12544255736Sdavidch
12545255736Sdavidchbxe_nic_load_error3:
12546255736Sdavidch
12547255736Sdavidch    if (IS_PF(sc)) {
12548255736Sdavidch        bxe_int_disable_sync(sc, 1);
12549255736Sdavidch
12550255736Sdavidch        /* clean out queued objects */
12551255736Sdavidch        bxe_squeeze_objects(sc);
12552255736Sdavidch    }
12553255736Sdavidch
12554255736Sdavidch    bxe_interrupt_detach(sc);
12555255736Sdavidch
12556255736Sdavidchbxe_nic_load_error2:
12557255736Sdavidch
12558255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12559255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12560255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12561255736Sdavidch    }
12562255736Sdavidch
12563255736Sdavidch    sc->port.pmf = 0;
12564255736Sdavidch
12565255736Sdavidchbxe_nic_load_error1:
12566255736Sdavidch
12567255736Sdavidch    /* clear pf_load status, as it was already set */
12568255736Sdavidch    if (IS_PF(sc)) {
12569255736Sdavidch        bxe_clear_pf_load(sc);
12570255736Sdavidch    }
12571255736Sdavidch
12572255736Sdavidchbxe_nic_load_error0:
12573255736Sdavidch
12574255736Sdavidch    bxe_free_fw_stats_mem(sc);
12575255736Sdavidch    bxe_free_fp_buffers(sc);
12576255736Sdavidch    bxe_free_mem(sc);
12577255736Sdavidch
12578255736Sdavidch    return (rc);
12579255736Sdavidch}
12580255736Sdavidch
12581255736Sdavidchstatic int
12582255736Sdavidchbxe_init_locked(struct bxe_softc *sc)
12583255736Sdavidch{
12584255736Sdavidch    int other_engine = SC_PATH(sc) ? 0 : 1;
12585255736Sdavidch    uint8_t other_load_status, load_status;
12586255736Sdavidch    uint8_t global = FALSE;
12587255736Sdavidch    int rc;
12588255736Sdavidch
12589255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12590255736Sdavidch
12591255736Sdavidch    /* check if the driver is already running */
12592266979Smarcel    if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12593255736Sdavidch        BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12594255736Sdavidch        return (0);
12595255736Sdavidch    }
12596255736Sdavidch
12597255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
12598255736Sdavidch
12599255736Sdavidch    /*
12600255736Sdavidch     * If parity occurred during the unload, then attentions and/or
12601255736Sdavidch     * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12602255736Sdavidch     * loaded on the current engine to complete the recovery. Parity recovery
12603255736Sdavidch     * is only relevant for PF driver.
12604255736Sdavidch     */
12605255736Sdavidch    if (IS_PF(sc)) {
12606255736Sdavidch        other_load_status = bxe_get_load_status(sc, other_engine);
12607255736Sdavidch        load_status = bxe_get_load_status(sc, SC_PATH(sc));
12608255736Sdavidch
12609255736Sdavidch        if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12610255736Sdavidch            bxe_chk_parity_attn(sc, &global, TRUE)) {
12611255736Sdavidch            do {
12612255736Sdavidch                /*
12613255736Sdavidch                 * If there are attentions and they are in global blocks, set
12614255736Sdavidch                 * the GLOBAL_RESET bit regardless whether it will be this
12615255736Sdavidch                 * function that will complete the recovery or not.
12616255736Sdavidch                 */
12617255736Sdavidch                if (global) {
12618255736Sdavidch                    bxe_set_reset_global(sc);
12619255736Sdavidch                }
12620255736Sdavidch
12621255736Sdavidch                /*
12622255736Sdavidch                 * Only the first function on the current engine should try
12623255736Sdavidch                 * to recover in open. In case of attentions in global blocks
12624255736Sdavidch                 * only the first in the chip should try to recover.
12625255736Sdavidch                 */
12626255736Sdavidch                if ((!load_status && (!global || !other_load_status)) &&
12627255736Sdavidch                    bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12628255736Sdavidch                    BLOGI(sc, "Recovered during init\n");
12629255736Sdavidch                    break;
12630255736Sdavidch                }
12631255736Sdavidch
12632255736Sdavidch                /* recovery has failed... */
12633255736Sdavidch                bxe_set_power_state(sc, PCI_PM_D3hot);
12634255736Sdavidch                sc->recovery_state = BXE_RECOVERY_FAILED;
12635255736Sdavidch
12636255736Sdavidch                BLOGE(sc, "Recovery flow hasn't properly "
12637255736Sdavidch                          "completed yet, try again later. "
12638255736Sdavidch                          "If you still see this message after a "
12639255736Sdavidch                          "few retries then power cycle is required.\n");
12640255736Sdavidch
12641255736Sdavidch                rc = ENXIO;
12642255736Sdavidch                goto bxe_init_locked_done;
12643255736Sdavidch            } while (0);
12644255736Sdavidch        }
12645255736Sdavidch    }
12646255736Sdavidch
12647255736Sdavidch    sc->recovery_state = BXE_RECOVERY_DONE;
12648255736Sdavidch
12649255736Sdavidch    rc = bxe_nic_load(sc, LOAD_OPEN);
12650255736Sdavidch
12651255736Sdavidchbxe_init_locked_done:
12652255736Sdavidch
12653255736Sdavidch    if (rc) {
12654255736Sdavidch        /* Tell the stack the driver is NOT running! */
12655255736Sdavidch        BLOGE(sc, "Initialization failed, "
12656255736Sdavidch                  "stack notified driver is NOT running!\n");
12657266979Smarcel	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
12658255736Sdavidch    }
12659255736Sdavidch
12660255736Sdavidch    return (rc);
12661255736Sdavidch}
12662255736Sdavidch
12663255736Sdavidchstatic int
12664255736Sdavidchbxe_stop_locked(struct bxe_softc *sc)
12665255736Sdavidch{
12666255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12667255736Sdavidch    return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
12668255736Sdavidch}
12669255736Sdavidch
12670255736Sdavidch/*
12671255736Sdavidch * Handles controller initialization when called from an unlocked routine.
12672255736Sdavidch * ifconfig calls this function.
12673255736Sdavidch *
12674255736Sdavidch * Returns:
12675255736Sdavidch *   void
12676255736Sdavidch */
12677255736Sdavidchstatic void
12678255736Sdavidchbxe_init(void *xsc)
12679255736Sdavidch{
12680255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12681255736Sdavidch
12682255736Sdavidch    BXE_CORE_LOCK(sc);
12683255736Sdavidch    bxe_init_locked(sc);
12684255736Sdavidch    BXE_CORE_UNLOCK(sc);
12685255736Sdavidch}
12686255736Sdavidch
12687255736Sdavidchstatic int
12688255736Sdavidchbxe_init_ifnet(struct bxe_softc *sc)
12689255736Sdavidch{
12690266979Smarcel    if_t ifp;
12691266979Smarcel    int capabilities;
12692255736Sdavidch
12693255736Sdavidch    /* ifconfig entrypoint for media type/status reporting */
12694255736Sdavidch    ifmedia_init(&sc->ifmedia, IFM_IMASK,
12695255736Sdavidch                 bxe_ifmedia_update,
12696255736Sdavidch                 bxe_ifmedia_status);
12697255736Sdavidch
12698255736Sdavidch    /* set the default interface values */
12699255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
12700255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
12701255736Sdavidch    ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
12702255736Sdavidch
12703255736Sdavidch    sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
12704315881Sdavidcs	BLOGI(sc, "IFMEDIA flags : %x\n", sc->ifmedia.ifm_media);
12705255736Sdavidch
12706255736Sdavidch    /* allocate the ifnet structure */
12707266979Smarcel    if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
12708255736Sdavidch        BLOGE(sc, "Interface allocation failed!\n");
12709255736Sdavidch        return (ENXIO);
12710255736Sdavidch    }
12711255736Sdavidch
12712266979Smarcel    if_setsoftc(ifp, sc);
12713270876Sglebius    if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
12714266979Smarcel    if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
12715266979Smarcel    if_setioctlfn(ifp, bxe_ioctl);
12716266979Smarcel    if_setstartfn(ifp, bxe_tx_start);
12717271782Sglebius    if_setgetcounterfn(ifp, bxe_get_counter);
12718307972Sdavidcs#if __FreeBSD_version >= 901504
12719266979Smarcel    if_settransmitfn(ifp, bxe_tx_mq_start);
12720266979Smarcel    if_setqflushfn(ifp, bxe_mq_flush);
12721255736Sdavidch#endif
12722255736Sdavidch#ifdef FreeBSD8_0
12723266979Smarcel    if_settimer(ifp, 0);
12724255736Sdavidch#endif
12725266979Smarcel    if_setinitfn(ifp, bxe_init);
12726266979Smarcel    if_setmtu(ifp, sc->mtu);
12727266979Smarcel    if_sethwassist(ifp, (CSUM_IP      |
12728255736Sdavidch                        CSUM_TCP      |
12729255736Sdavidch                        CSUM_UDP      |
12730255736Sdavidch                        CSUM_TSO      |
12731255736Sdavidch                        CSUM_TCP_IPV6 |
12732266979Smarcel                        CSUM_UDP_IPV6));
12733266979Smarcel
12734266979Smarcel    capabilities =
12735255736Sdavidch#if __FreeBSD_version < 700000
12736255736Sdavidch        (IFCAP_VLAN_MTU       |
12737255736Sdavidch         IFCAP_VLAN_HWTAGGING |
12738255736Sdavidch         IFCAP_HWCSUM         |
12739255736Sdavidch         IFCAP_JUMBO_MTU      |
12740255736Sdavidch         IFCAP_LRO);
12741255736Sdavidch#else
12742255736Sdavidch        (IFCAP_VLAN_MTU       |
12743255736Sdavidch         IFCAP_VLAN_HWTAGGING |
12744255736Sdavidch         IFCAP_VLAN_HWTSO     |
12745255736Sdavidch         IFCAP_VLAN_HWFILTER  |
12746255736Sdavidch         IFCAP_VLAN_HWCSUM    |
12747255736Sdavidch         IFCAP_HWCSUM         |
12748255736Sdavidch         IFCAP_JUMBO_MTU      |
12749255736Sdavidch         IFCAP_LRO            |
12750255736Sdavidch         IFCAP_TSO4           |
12751255736Sdavidch         IFCAP_TSO6           |
12752255736Sdavidch         IFCAP_WOL_MAGIC);
12753255736Sdavidch#endif
12754266979Smarcel    if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
12755316539Ssbruno    if_setcapenable(ifp, if_getcapabilities(ifp));
12756266979Smarcel    if_setbaudrate(ifp, IF_Gbps(10));
12757266979Smarcel/* XXX */
12758266979Smarcel    if_setsendqlen(ifp, sc->tx_ring_size);
12759266979Smarcel    if_setsendqready(ifp);
12760266979Smarcel/* XXX */
12761255736Sdavidch
12762266979Smarcel    sc->ifp = ifp;
12763255736Sdavidch
12764255736Sdavidch    /* attach to the Ethernet interface list */
12765270876Sglebius    ether_ifattach(ifp, sc->link_params.mac_addr);
12766255736Sdavidch
12767255736Sdavidch    return (0);
12768255736Sdavidch}
12769255736Sdavidch
12770255736Sdavidchstatic void
12771255736Sdavidchbxe_deallocate_bars(struct bxe_softc *sc)
12772255736Sdavidch{
12773255736Sdavidch    int i;
12774255736Sdavidch
12775255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
12776255736Sdavidch        if (sc->bar[i].resource != NULL) {
12777255736Sdavidch            bus_release_resource(sc->dev,
12778255736Sdavidch                                 SYS_RES_MEMORY,
12779255736Sdavidch                                 sc->bar[i].rid,
12780255736Sdavidch                                 sc->bar[i].resource);
12781255736Sdavidch            BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
12782255736Sdavidch                  i, PCIR_BAR(i));
12783255736Sdavidch        }
12784255736Sdavidch    }
12785255736Sdavidch}
12786255736Sdavidch
12787255736Sdavidchstatic int
12788255736Sdavidchbxe_allocate_bars(struct bxe_softc *sc)
12789255736Sdavidch{
12790255736Sdavidch    u_int flags;
12791255736Sdavidch    int i;
12792255736Sdavidch
12793255736Sdavidch    memset(sc->bar, 0, sizeof(sc->bar));
12794255736Sdavidch
12795255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
12796255736Sdavidch
12797255736Sdavidch        /* memory resources reside at BARs 0, 2, 4 */
12798255736Sdavidch        /* Run `pciconf -lb` to see mappings */
12799255736Sdavidch        if ((i != 0) && (i != 2) && (i != 4)) {
12800255736Sdavidch            continue;
12801255736Sdavidch        }
12802255736Sdavidch
12803255736Sdavidch        sc->bar[i].rid = PCIR_BAR(i);
12804255736Sdavidch
12805255736Sdavidch        flags = RF_ACTIVE;
12806255736Sdavidch        if (i == 0) {
12807255736Sdavidch            flags |= RF_SHAREABLE;
12808255736Sdavidch        }
12809255736Sdavidch
12810255736Sdavidch        if ((sc->bar[i].resource =
12811255736Sdavidch             bus_alloc_resource_any(sc->dev,
12812255736Sdavidch                                    SYS_RES_MEMORY,
12813255736Sdavidch                                    &sc->bar[i].rid,
12814255736Sdavidch                                    flags)) == NULL) {
12815255736Sdavidch            return (0);
12816255736Sdavidch        }
12817255736Sdavidch
12818255736Sdavidch        sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
12819255736Sdavidch        sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
12820255736Sdavidch        sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
12821255736Sdavidch
12822335289Sdim        BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %#jx-%#jx (%jd) -> %#jx\n",
12823255736Sdavidch              i, PCIR_BAR(i),
12824335289Sdim              rman_get_start(sc->bar[i].resource),
12825335289Sdim              rman_get_end(sc->bar[i].resource),
12826255736Sdavidch              rman_get_size(sc->bar[i].resource),
12827335289Sdim              (uintmax_t)sc->bar[i].kva);
12828255736Sdavidch    }
12829255736Sdavidch
12830255736Sdavidch    return (0);
12831255736Sdavidch}
12832255736Sdavidch
12833255736Sdavidchstatic void
12834255736Sdavidchbxe_get_function_num(struct bxe_softc *sc)
12835255736Sdavidch{
12836255736Sdavidch    uint32_t val = 0;
12837255736Sdavidch
12838255736Sdavidch    /*
12839255736Sdavidch     * Read the ME register to get the function number. The ME register
12840255736Sdavidch     * holds the relative-function number and absolute-function number. The
12841255736Sdavidch     * absolute-function number appears only in E2 and above. Before that
12842255736Sdavidch     * these bits always contained zero, therefore we cannot blindly use them.
12843255736Sdavidch     */
12844255736Sdavidch
12845255736Sdavidch    val = REG_RD(sc, BAR_ME_REGISTER);
12846255736Sdavidch
12847255736Sdavidch    sc->pfunc_rel =
12848255736Sdavidch        (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
12849255736Sdavidch    sc->path_id =
12850255736Sdavidch        (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
12851255736Sdavidch
12852255736Sdavidch    if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
12853255736Sdavidch        sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
12854255736Sdavidch    } else {
12855255736Sdavidch        sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
12856255736Sdavidch    }
12857255736Sdavidch
12858255736Sdavidch    BLOGD(sc, DBG_LOAD,
12859255736Sdavidch          "Relative function %d, Absolute function %d, Path %d\n",
12860255736Sdavidch          sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
12861255736Sdavidch}
12862255736Sdavidch
12863255736Sdavidchstatic uint32_t
12864255736Sdavidchbxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
12865255736Sdavidch{
12866255736Sdavidch    uint32_t shmem2_size;
12867255736Sdavidch    uint32_t offset;
12868255736Sdavidch    uint32_t mf_cfg_offset_value;
12869255736Sdavidch
12870255736Sdavidch    /* Non 57712 */
12871255736Sdavidch    offset = (SHMEM_RD(sc, func_mb) +
12872255736Sdavidch              (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
12873255736Sdavidch
12874255736Sdavidch    /* 57712 plus */
12875255736Sdavidch    if (sc->devinfo.shmem2_base != 0) {
12876255736Sdavidch        shmem2_size = SHMEM2_RD(sc, size);
12877255736Sdavidch        if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
12878255736Sdavidch            mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
12879255736Sdavidch            if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
12880255736Sdavidch                offset = mf_cfg_offset_value;
12881255736Sdavidch            }
12882255736Sdavidch        }
12883255736Sdavidch    }
12884255736Sdavidch
12885255736Sdavidch    return (offset);
12886255736Sdavidch}
12887255736Sdavidch
12888255736Sdavidchstatic uint32_t
12889255736Sdavidchbxe_pcie_capability_read(struct bxe_softc *sc,
12890255736Sdavidch                         int    reg,
12891255736Sdavidch                         int    width)
12892255736Sdavidch{
12893255736Sdavidch    int pcie_reg;
12894255736Sdavidch
12895255736Sdavidch    /* ensure PCIe capability is enabled */
12896255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
12897255736Sdavidch        if (pcie_reg != 0) {
12898255736Sdavidch            BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
12899255736Sdavidch            return (pci_read_config(sc->dev, (pcie_reg + reg), width));
12900255736Sdavidch        }
12901255736Sdavidch    }
12902255736Sdavidch
12903255736Sdavidch    BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
12904255736Sdavidch
12905255736Sdavidch    return (0);
12906255736Sdavidch}
12907255736Sdavidch
12908255736Sdavidchstatic uint8_t
12909255736Sdavidchbxe_is_pcie_pending(struct bxe_softc *sc)
12910255736Sdavidch{
12911255736Sdavidch    return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
12912255736Sdavidch            PCIM_EXP_STA_TRANSACTION_PND);
12913255736Sdavidch}
12914255736Sdavidch
12915255736Sdavidch/*
12916255736Sdavidch * Walk the PCI capabiites list for the device to find what features are
12917255736Sdavidch * supported. These capabilites may be enabled/disabled by firmware so it's
12918255736Sdavidch * best to walk the list rather than make assumptions.
12919255736Sdavidch */
12920255736Sdavidchstatic void
12921255736Sdavidchbxe_probe_pci_caps(struct bxe_softc *sc)
12922255736Sdavidch{
12923255736Sdavidch    uint16_t link_status;
12924255736Sdavidch    int reg;
12925255736Sdavidch
12926255736Sdavidch    /* check if PCI Power Management is enabled */
12927255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
12928255736Sdavidch        if (reg != 0) {
12929255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
12930255736Sdavidch
12931255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
12932255736Sdavidch            sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
12933255736Sdavidch        }
12934255736Sdavidch    }
12935255736Sdavidch
12936255736Sdavidch    link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
12937255736Sdavidch
12938255736Sdavidch    /* handle PCIe 2.0 workarounds for 57710 */
12939255736Sdavidch    if (CHIP_IS_E1(sc)) {
12940255736Sdavidch        /* workaround for 57710 errata E4_57710_27462 */
12941255736Sdavidch        sc->devinfo.pcie_link_speed =
12942255736Sdavidch            (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
12943255736Sdavidch
12944255736Sdavidch        /* workaround for 57710 errata E4_57710_27488 */
12945255736Sdavidch        sc->devinfo.pcie_link_width =
12946255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12947255736Sdavidch        if (sc->devinfo.pcie_link_speed > 1) {
12948255736Sdavidch            sc->devinfo.pcie_link_width =
12949255736Sdavidch                ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
12950255736Sdavidch        }
12951255736Sdavidch    } else {
12952255736Sdavidch        sc->devinfo.pcie_link_speed =
12953255736Sdavidch            (link_status & PCIM_LINK_STA_SPEED);
12954255736Sdavidch        sc->devinfo.pcie_link_width =
12955255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12956255736Sdavidch    }
12957255736Sdavidch
12958255736Sdavidch    BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
12959255736Sdavidch          sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
12960255736Sdavidch
12961255736Sdavidch    sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
12962255736Sdavidch    sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
12963255736Sdavidch
12964255736Sdavidch    /* check if MSI capability is enabled */
12965255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
12966255736Sdavidch        if (reg != 0) {
12967255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
12968255736Sdavidch
12969255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
12970255736Sdavidch            sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
12971255736Sdavidch        }
12972255736Sdavidch    }
12973255736Sdavidch
12974255736Sdavidch    /* check if MSI-X capability is enabled */
12975255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
12976255736Sdavidch        if (reg != 0) {
12977255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
12978255736Sdavidch
12979255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
12980255736Sdavidch            sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
12981255736Sdavidch        }
12982255736Sdavidch    }
12983255736Sdavidch}
12984255736Sdavidch
12985255736Sdavidchstatic int
12986255736Sdavidchbxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
12987255736Sdavidch{
12988255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
12989255736Sdavidch    uint32_t val;
12990255736Sdavidch
12991255736Sdavidch    /* get the outer vlan if we're in switch-dependent mode */
12992255736Sdavidch
12993255736Sdavidch    val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
12994255736Sdavidch    mf_info->ext_id = (uint16_t)val;
12995255736Sdavidch
12996255736Sdavidch    mf_info->multi_vnics_mode = 1;
12997255736Sdavidch
12998255736Sdavidch    if (!VALID_OVLAN(mf_info->ext_id)) {
12999255736Sdavidch        BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13000255736Sdavidch        return (1);
13001255736Sdavidch    }
13002255736Sdavidch
13003255736Sdavidch    /* get the capabilities */
13004255736Sdavidch    if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13005255736Sdavidch        FUNC_MF_CFG_PROTOCOL_ISCSI) {
13006255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13007255736Sdavidch    } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13008255736Sdavidch               FUNC_MF_CFG_PROTOCOL_FCOE) {
13009255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13010255736Sdavidch    } else {
13011255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13012255736Sdavidch    }
13013255736Sdavidch
13014255736Sdavidch    mf_info->vnics_per_port =
13015255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13016255736Sdavidch
13017255736Sdavidch    return (0);
13018255736Sdavidch}
13019255736Sdavidch
13020255736Sdavidchstatic uint32_t
13021255736Sdavidchbxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13022255736Sdavidch{
13023255736Sdavidch    uint32_t retval = 0;
13024255736Sdavidch    uint32_t val;
13025255736Sdavidch
13026255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13027255736Sdavidch
13028255736Sdavidch    if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13029255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13030255736Sdavidch            retval |= MF_PROTO_SUPPORT_ETHERNET;
13031255736Sdavidch        }
13032255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13033255736Sdavidch            retval |= MF_PROTO_SUPPORT_ISCSI;
13034255736Sdavidch        }
13035255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13036255736Sdavidch            retval |= MF_PROTO_SUPPORT_FCOE;
13037255736Sdavidch        }
13038255736Sdavidch    }
13039255736Sdavidch
13040255736Sdavidch    return (retval);
13041255736Sdavidch}
13042255736Sdavidch
13043255736Sdavidchstatic int
13044255736Sdavidchbxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13045255736Sdavidch{
13046255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13047255736Sdavidch    uint32_t val;
13048255736Sdavidch
13049255736Sdavidch    /*
13050255736Sdavidch     * There is no outer vlan if we're in switch-independent mode.
13051255736Sdavidch     * If the mac is valid then assume multi-function.
13052255736Sdavidch     */
13053255736Sdavidch
13054255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13055255736Sdavidch
13056255736Sdavidch    mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13057255736Sdavidch
13058255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13059255736Sdavidch
13060255736Sdavidch    mf_info->vnics_per_port =
13061255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13062255736Sdavidch
13063255736Sdavidch    return (0);
13064255736Sdavidch}
13065255736Sdavidch
13066255736Sdavidchstatic int
13067255736Sdavidchbxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13068255736Sdavidch{
13069255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13070255736Sdavidch    uint32_t e1hov_tag;
13071255736Sdavidch    uint32_t func_config;
13072255736Sdavidch    uint32_t niv_config;
13073255736Sdavidch
13074255736Sdavidch    mf_info->multi_vnics_mode = 1;
13075255736Sdavidch
13076255736Sdavidch    e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13077255736Sdavidch    func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13078255736Sdavidch    niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13079255736Sdavidch
13080255736Sdavidch    mf_info->ext_id =
13081255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13082255736Sdavidch                   FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13083255736Sdavidch
13084255736Sdavidch    mf_info->default_vlan =
13085255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13086255736Sdavidch                   FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13087255736Sdavidch
13088255736Sdavidch    mf_info->niv_allowed_priorities =
13089255736Sdavidch        (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13090255736Sdavidch                  FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13091255736Sdavidch
13092255736Sdavidch    mf_info->niv_default_cos =
13093255736Sdavidch        (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13094255736Sdavidch                  FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13095255736Sdavidch
13096255736Sdavidch    mf_info->afex_vlan_mode =
13097255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13098255736Sdavidch         FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13099255736Sdavidch
13100255736Sdavidch    mf_info->niv_mba_enabled =
13101255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13102255736Sdavidch         FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13103255736Sdavidch
13104255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13105255736Sdavidch
13106255736Sdavidch    mf_info->vnics_per_port =
13107255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13108255736Sdavidch
13109255736Sdavidch    return (0);
13110255736Sdavidch}
13111255736Sdavidch
13112255736Sdavidchstatic int
13113255736Sdavidchbxe_check_valid_mf_cfg(struct bxe_softc *sc)
13114255736Sdavidch{
13115255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13116255736Sdavidch    uint32_t mf_cfg1;
13117255736Sdavidch    uint32_t mf_cfg2;
13118255736Sdavidch    uint32_t ovlan1;
13119255736Sdavidch    uint32_t ovlan2;
13120255736Sdavidch    uint8_t i, j;
13121255736Sdavidch
13122255736Sdavidch    BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13123255736Sdavidch          SC_PORT(sc));
13124255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13125255736Sdavidch          mf_info->mf_config[SC_VN(sc)]);
13126255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13127255736Sdavidch          mf_info->multi_vnics_mode);
13128255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13129255736Sdavidch          mf_info->vnics_per_port);
13130255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13131255736Sdavidch          mf_info->ext_id);
13132255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13133255736Sdavidch          mf_info->min_bw[0], mf_info->min_bw[1],
13134255736Sdavidch          mf_info->min_bw[2], mf_info->min_bw[3]);
13135255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13136255736Sdavidch          mf_info->max_bw[0], mf_info->max_bw[1],
13137255736Sdavidch          mf_info->max_bw[2], mf_info->max_bw[3]);
13138255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13139255736Sdavidch          sc->mac_addr_str);
13140255736Sdavidch
13141255736Sdavidch    /* various MF mode sanity checks... */
13142255736Sdavidch
13143255736Sdavidch    if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13144255736Sdavidch        BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13145255736Sdavidch              SC_PORT(sc));
13146255736Sdavidch        return (1);
13147255736Sdavidch    }
13148255736Sdavidch
13149255736Sdavidch    if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13150255736Sdavidch        BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13151255736Sdavidch              mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13152255736Sdavidch        return (1);
13153255736Sdavidch    }
13154255736Sdavidch
13155255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13156255736Sdavidch        /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13157255736Sdavidch        if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13158255736Sdavidch            BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13159255736Sdavidch                  SC_VN(sc), OVLAN(sc));
13160255736Sdavidch            return (1);
13161255736Sdavidch        }
13162255736Sdavidch
13163255736Sdavidch        if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13164255736Sdavidch            BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13165255736Sdavidch                  mf_info->multi_vnics_mode, OVLAN(sc));
13166255736Sdavidch            return (1);
13167255736Sdavidch        }
13168255736Sdavidch
13169255736Sdavidch        /*
13170255736Sdavidch         * Verify all functions are either MF or SF mode. If MF, make sure
13171255736Sdavidch         * sure that all non-hidden functions have a valid ovlan. If SF,
13172255736Sdavidch         * make sure that all non-hidden functions have an invalid ovlan.
13173255736Sdavidch         */
13174255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13175255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13176255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13177255736Sdavidch            if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13178255736Sdavidch                (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13179255736Sdavidch                 ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13180255736Sdavidch                BLOGE(sc, "mf_mode=SD function %d MF config "
13181255736Sdavidch                          "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13182255736Sdavidch                      i, mf_info->multi_vnics_mode, ovlan1);
13183255736Sdavidch                return (1);
13184255736Sdavidch            }
13185255736Sdavidch        }
13186255736Sdavidch
13187255736Sdavidch        /* Verify all funcs on the same port each have a different ovlan. */
13188255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13189255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13190255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13191255736Sdavidch            /* iterate from the next function on the port to the max func */
13192255736Sdavidch            for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13193255736Sdavidch                mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13194255736Sdavidch                ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13195255736Sdavidch                if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13196255736Sdavidch                    VALID_OVLAN(ovlan1) &&
13197255736Sdavidch                    !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13198255736Sdavidch                    VALID_OVLAN(ovlan2) &&
13199255736Sdavidch                    (ovlan1 == ovlan2)) {
13200255736Sdavidch                    BLOGE(sc, "mf_mode=SD functions %d and %d "
13201255736Sdavidch                              "have the same ovlan (%d)\n",
13202255736Sdavidch                          i, j, ovlan1);
13203255736Sdavidch                    return (1);
13204255736Sdavidch                }
13205255736Sdavidch            }
13206255736Sdavidch        }
13207255736Sdavidch    } /* MULTI_FUNCTION_SD */
13208255736Sdavidch
13209255736Sdavidch    return (0);
13210255736Sdavidch}
13211255736Sdavidch
13212255736Sdavidchstatic int
13213255736Sdavidchbxe_get_mf_cfg_info(struct bxe_softc *sc)
13214255736Sdavidch{
13215255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13216255736Sdavidch    uint32_t val, mac_upper;
13217255736Sdavidch    uint8_t i, vnic;
13218255736Sdavidch
13219255736Sdavidch    /* initialize mf_info defaults */
13220255736Sdavidch    mf_info->vnics_per_port   = 1;
13221255736Sdavidch    mf_info->multi_vnics_mode = FALSE;
13222255736Sdavidch    mf_info->path_has_ovlan   = FALSE;
13223255736Sdavidch    mf_info->mf_mode          = SINGLE_FUNCTION;
13224255736Sdavidch
13225255736Sdavidch    if (!CHIP_IS_MF_CAP(sc)) {
13226255736Sdavidch        return (0);
13227255736Sdavidch    }
13228255736Sdavidch
13229255736Sdavidch    if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13230255736Sdavidch        BLOGE(sc, "Invalid mf_cfg_base!\n");
13231255736Sdavidch        return (1);
13232255736Sdavidch    }
13233255736Sdavidch
13234255736Sdavidch    /* get the MF mode (switch dependent / independent / single-function) */
13235255736Sdavidch
13236255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13237255736Sdavidch
13238255736Sdavidch    switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13239255736Sdavidch    {
13240255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13241255736Sdavidch
13242255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13243255736Sdavidch
13244255736Sdavidch        /* check for legal upper mac bytes */
13245255736Sdavidch        if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13246255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SI;
13247255736Sdavidch        } else {
13248255736Sdavidch            BLOGE(sc, "Invalid config for Switch Independent mode\n");
13249255736Sdavidch        }
13250255736Sdavidch
13251255736Sdavidch        break;
13252255736Sdavidch
13253255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13254255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13255255736Sdavidch
13256255736Sdavidch        /* get outer vlan configuration */
13257255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13258255736Sdavidch
13259255736Sdavidch        if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13260255736Sdavidch            FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13261255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SD;
13262255736Sdavidch        } else {
13263255736Sdavidch            BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13264255736Sdavidch        }
13265255736Sdavidch
13266255736Sdavidch        break;
13267255736Sdavidch
13268255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13269255736Sdavidch
13270255736Sdavidch        /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13271255736Sdavidch        return (0);
13272255736Sdavidch
13273255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13274255736Sdavidch
13275255736Sdavidch        /*
13276255736Sdavidch         * Mark MF mode as NIV if MCP version includes NPAR-SD support
13277255736Sdavidch         * and the MAC address is valid.
13278255736Sdavidch         */
13279255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13280255736Sdavidch
13281255736Sdavidch        if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13282255736Sdavidch            (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13283255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13284255736Sdavidch        } else {
13285255736Sdavidch            BLOGE(sc, "Invalid config for AFEX mode\n");
13286255736Sdavidch        }
13287255736Sdavidch
13288255736Sdavidch        break;
13289255736Sdavidch
13290255736Sdavidch    default:
13291255736Sdavidch
13292255736Sdavidch        BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13293255736Sdavidch              (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13294255736Sdavidch
13295255736Sdavidch        return (1);
13296255736Sdavidch    }
13297255736Sdavidch
13298255736Sdavidch    /* set path mf_mode (which could be different than function mf_mode) */
13299255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13300255736Sdavidch        mf_info->path_has_ovlan = TRUE;
13301255736Sdavidch    } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13302255736Sdavidch        /*
13303255736Sdavidch         * Decide on path multi vnics mode. If we're not in MF mode and in
13304255736Sdavidch         * 4-port mode, this is good enough to check vnic-0 of the other port
13305255736Sdavidch         * on the same path
13306255736Sdavidch         */
13307255736Sdavidch        if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13308255736Sdavidch            uint8_t other_port = !(PORT_ID(sc) & 1);
13309255736Sdavidch            uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13310255736Sdavidch
13311255736Sdavidch            val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13312255736Sdavidch
13313255736Sdavidch            mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13314255736Sdavidch        }
13315255736Sdavidch    }
13316255736Sdavidch
13317255736Sdavidch    if (mf_info->mf_mode == SINGLE_FUNCTION) {
13318255736Sdavidch        /* invalid MF config */
13319255736Sdavidch        if (SC_VN(sc) >= 1) {
13320255736Sdavidch            BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13321255736Sdavidch            return (1);
13322255736Sdavidch        }
13323255736Sdavidch
13324255736Sdavidch        return (0);
13325255736Sdavidch    }
13326255736Sdavidch
13327255736Sdavidch    /* get the MF configuration */
13328255736Sdavidch    mf_info->mf_config[SC_VN(sc)] =
13329255736Sdavidch        MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13330255736Sdavidch
13331255736Sdavidch    switch(mf_info->mf_mode)
13332255736Sdavidch    {
13333255736Sdavidch    case MULTI_FUNCTION_SD:
13334255736Sdavidch
13335255736Sdavidch        bxe_get_shmem_mf_cfg_info_sd(sc);
13336255736Sdavidch        break;
13337255736Sdavidch
13338255736Sdavidch    case MULTI_FUNCTION_SI:
13339255736Sdavidch
13340255736Sdavidch        bxe_get_shmem_mf_cfg_info_si(sc);
13341255736Sdavidch        break;
13342255736Sdavidch
13343255736Sdavidch    case MULTI_FUNCTION_AFEX:
13344255736Sdavidch
13345255736Sdavidch        bxe_get_shmem_mf_cfg_info_niv(sc);
13346255736Sdavidch        break;
13347255736Sdavidch
13348255736Sdavidch    default:
13349255736Sdavidch
13350255736Sdavidch        BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13351255736Sdavidch              mf_info->mf_mode);
13352255736Sdavidch        return (1);
13353255736Sdavidch    }
13354255736Sdavidch
13355255736Sdavidch    /* get the congestion management parameters */
13356255736Sdavidch
13357255736Sdavidch    vnic = 0;
13358255736Sdavidch    FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13359255736Sdavidch        /* get min/max bw */
13360255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[i].config);
13361255736Sdavidch        mf_info->min_bw[vnic] =
13362255736Sdavidch            ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13363255736Sdavidch        mf_info->max_bw[vnic] =
13364255736Sdavidch            ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13365255736Sdavidch        vnic++;
13366255736Sdavidch    }
13367255736Sdavidch
13368255736Sdavidch    return (bxe_check_valid_mf_cfg(sc));
13369255736Sdavidch}
13370255736Sdavidch
13371255736Sdavidchstatic int
13372255736Sdavidchbxe_get_shmem_info(struct bxe_softc *sc)
13373255736Sdavidch{
13374255736Sdavidch    int port;
13375255736Sdavidch    uint32_t mac_hi, mac_lo, val;
13376255736Sdavidch
13377255736Sdavidch    port = SC_PORT(sc);
13378255736Sdavidch    mac_hi = mac_lo = 0;
13379255736Sdavidch
13380255736Sdavidch    sc->link_params.sc   = sc;
13381255736Sdavidch    sc->link_params.port = port;
13382255736Sdavidch
13383255736Sdavidch    /* get the hardware config info */
13384255736Sdavidch    sc->devinfo.hw_config =
13385255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config);
13386255736Sdavidch    sc->devinfo.hw_config2 =
13387255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13388255736Sdavidch
13389255736Sdavidch    sc->link_params.hw_led_mode =
13390255736Sdavidch        ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13391255736Sdavidch         SHARED_HW_CFG_LED_MODE_SHIFT);
13392255736Sdavidch
13393255736Sdavidch    /* get the port feature config */
13394255736Sdavidch    sc->port.config =
13395305614Spfg        SHMEM_RD(sc, dev_info.port_feature_config[port].config);
13396255736Sdavidch
13397255736Sdavidch    /* get the link params */
13398255736Sdavidch    sc->link_params.speed_cap_mask[0] =
13399255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13400255736Sdavidch    sc->link_params.speed_cap_mask[1] =
13401255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13402255736Sdavidch
13403255736Sdavidch    /* get the lane config */
13404255736Sdavidch    sc->link_params.lane_config =
13405255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13406255736Sdavidch
13407255736Sdavidch    /* get the link config */
13408255736Sdavidch    val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13409255736Sdavidch    sc->port.link_config[ELINK_INT_PHY] = val;
13410255736Sdavidch    sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13411255736Sdavidch    sc->port.link_config[ELINK_EXT_PHY1] =
13412255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13413255736Sdavidch
13414255736Sdavidch    /* get the override preemphasis flag and enable it or turn it off */
13415255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13416255736Sdavidch    if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13417255736Sdavidch        sc->link_params.feature_config_flags |=
13418255736Sdavidch            ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13419255736Sdavidch    } else {
13420255736Sdavidch        sc->link_params.feature_config_flags &=
13421255736Sdavidch            ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13422255736Sdavidch    }
13423255736Sdavidch
13424255736Sdavidch    /* get the initial value of the link params */
13425255736Sdavidch    sc->link_params.multi_phy_config =
13426255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13427255736Sdavidch
13428255736Sdavidch    /* get external phy info */
13429255736Sdavidch    sc->port.ext_phy_config =
13430255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13431255736Sdavidch
13432255736Sdavidch    /* get the multifunction configuration */
13433255736Sdavidch    bxe_get_mf_cfg_info(sc);
13434255736Sdavidch
13435255736Sdavidch    /* get the mac address */
13436255736Sdavidch    if (IS_MF(sc)) {
13437255736Sdavidch        mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13438255736Sdavidch        mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13439255736Sdavidch    } else {
13440255736Sdavidch        mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13441255736Sdavidch        mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13442255736Sdavidch    }
13443255736Sdavidch
13444255736Sdavidch    if ((mac_lo == 0) && (mac_hi == 0)) {
13445255736Sdavidch        *sc->mac_addr_str = 0;
13446255736Sdavidch        BLOGE(sc, "No Ethernet address programmed!\n");
13447255736Sdavidch    } else {
13448255736Sdavidch        sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13449255736Sdavidch        sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13450255736Sdavidch        sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13451255736Sdavidch        sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13452255736Sdavidch        sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13453255736Sdavidch        sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13454255736Sdavidch        snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13455255736Sdavidch                 "%02x:%02x:%02x:%02x:%02x:%02x",
13456255736Sdavidch                 sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13457255736Sdavidch                 sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13458255736Sdavidch                 sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13459255736Sdavidch        BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13460255736Sdavidch    }
13461255736Sdavidch
13462255736Sdavidch    return (0);
13463255736Sdavidch}
13464255736Sdavidch
13465255736Sdavidchstatic void
13466255736Sdavidchbxe_get_tunable_params(struct bxe_softc *sc)
13467255736Sdavidch{
13468255736Sdavidch    /* sanity checks */
13469255736Sdavidch
13470255736Sdavidch    if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13471255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13472255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13473255736Sdavidch        BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13474255736Sdavidch        bxe_interrupt_mode = INTR_MODE_MSIX;
13475255736Sdavidch    }
13476255736Sdavidch
13477255736Sdavidch    if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13478255736Sdavidch        BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13479255736Sdavidch        bxe_queue_count = 0;
13480255736Sdavidch    }
13481255736Sdavidch
13482255736Sdavidch    if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13483255736Sdavidch        if (bxe_max_rx_bufs == 0) {
13484255736Sdavidch            bxe_max_rx_bufs = RX_BD_USABLE;
13485255736Sdavidch        } else {
13486255736Sdavidch            BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13487255736Sdavidch            bxe_max_rx_bufs = 2048;
13488255736Sdavidch        }
13489255736Sdavidch    }
13490255736Sdavidch
13491255736Sdavidch    if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13492255736Sdavidch        BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13493255736Sdavidch        bxe_hc_rx_ticks = 25;
13494255736Sdavidch    }
13495255736Sdavidch
13496255736Sdavidch    if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13497255736Sdavidch        BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13498255736Sdavidch        bxe_hc_tx_ticks = 50;
13499255736Sdavidch    }
13500255736Sdavidch
13501255736Sdavidch    if (bxe_max_aggregation_size == 0) {
13502255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13503255736Sdavidch    }
13504255736Sdavidch
13505255736Sdavidch    if (bxe_max_aggregation_size > 0xffff) {
13506255736Sdavidch        BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13507255736Sdavidch              bxe_max_aggregation_size);
13508255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13509255736Sdavidch    }
13510255736Sdavidch
13511255736Sdavidch    if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13512255736Sdavidch        BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13513255736Sdavidch        bxe_mrrs = -1;
13514255736Sdavidch    }
13515255736Sdavidch
13516255736Sdavidch    if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13517255736Sdavidch        BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13518255736Sdavidch        bxe_autogreeen = 0;
13519255736Sdavidch    }
13520255736Sdavidch
13521255736Sdavidch    if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13522255736Sdavidch        BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13523255736Sdavidch        bxe_udp_rss = 0;
13524255736Sdavidch    }
13525255736Sdavidch
13526255736Sdavidch    /* pull in user settings */
13527255736Sdavidch
13528255736Sdavidch    sc->interrupt_mode       = bxe_interrupt_mode;
13529255736Sdavidch    sc->max_rx_bufs          = bxe_max_rx_bufs;
13530255736Sdavidch    sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13531255736Sdavidch    sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13532255736Sdavidch    sc->max_aggregation_size = bxe_max_aggregation_size;
13533255736Sdavidch    sc->mrrs                 = bxe_mrrs;
13534255736Sdavidch    sc->autogreeen           = bxe_autogreeen;
13535255736Sdavidch    sc->udp_rss              = bxe_udp_rss;
13536255736Sdavidch
13537255736Sdavidch    if (bxe_interrupt_mode == INTR_MODE_INTX) {
13538255736Sdavidch        sc->num_queues = 1;
13539255736Sdavidch    } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13540255736Sdavidch        sc->num_queues =
13541255736Sdavidch            min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13542255736Sdavidch                MAX_RSS_CHAINS);
13543255736Sdavidch        if (sc->num_queues > mp_ncpus) {
13544255736Sdavidch            sc->num_queues = mp_ncpus;
13545255736Sdavidch        }
13546255736Sdavidch    }
13547255736Sdavidch
13548255736Sdavidch    BLOGD(sc, DBG_LOAD,
13549255736Sdavidch          "User Config: "
13550258187Sedavis          "debug=0x%lx "
13551255736Sdavidch          "interrupt_mode=%d "
13552255736Sdavidch          "queue_count=%d "
13553255736Sdavidch          "hc_rx_ticks=%d "
13554255736Sdavidch          "hc_tx_ticks=%d "
13555255736Sdavidch          "rx_budget=%d "
13556255736Sdavidch          "max_aggregation_size=%d "
13557255736Sdavidch          "mrrs=%d "
13558255736Sdavidch          "autogreeen=%d "
13559255736Sdavidch          "udp_rss=%d\n",
13560255736Sdavidch          bxe_debug,
13561255736Sdavidch          sc->interrupt_mode,
13562255736Sdavidch          sc->num_queues,
13563255736Sdavidch          sc->hc_rx_ticks,
13564255736Sdavidch          sc->hc_tx_ticks,
13565255736Sdavidch          bxe_rx_budget,
13566255736Sdavidch          sc->max_aggregation_size,
13567255736Sdavidch          sc->mrrs,
13568255736Sdavidch          sc->autogreeen,
13569255736Sdavidch          sc->udp_rss);
13570255736Sdavidch}
13571255736Sdavidch
13572297884Sdavidcsstatic int
13573255736Sdavidchbxe_media_detect(struct bxe_softc *sc)
13574255736Sdavidch{
13575297884Sdavidcs    int port_type;
13576255736Sdavidch    uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13577297884Sdavidcs
13578255736Sdavidch    switch (sc->link_params.phy[phy_idx].media_type) {
13579255736Sdavidch    case ELINK_ETH_PHY_SFPP_10G_FIBER:
13580256299Sedavis    case ELINK_ETH_PHY_XFP_FIBER:
13581256299Sedavis        BLOGI(sc, "Found 10Gb Fiber media.\n");
13582256299Sedavis        sc->media = IFM_10G_SR;
13583297884Sdavidcs        port_type = PORT_FIBRE;
13584256299Sedavis        break;
13585255736Sdavidch    case ELINK_ETH_PHY_SFP_1G_FIBER:
13586256299Sedavis        BLOGI(sc, "Found 1Gb Fiber media.\n");
13587256299Sedavis        sc->media = IFM_1000_SX;
13588297884Sdavidcs        port_type = PORT_FIBRE;
13589256299Sedavis        break;
13590255736Sdavidch    case ELINK_ETH_PHY_KR:
13591255736Sdavidch    case ELINK_ETH_PHY_CX4:
13592255736Sdavidch        BLOGI(sc, "Found 10GBase-CX4 media.\n");
13593255736Sdavidch        sc->media = IFM_10G_CX4;
13594297884Sdavidcs        port_type = PORT_FIBRE;
13595255736Sdavidch        break;
13596255736Sdavidch    case ELINK_ETH_PHY_DA_TWINAX:
13597255736Sdavidch        BLOGI(sc, "Found 10Gb Twinax media.\n");
13598255736Sdavidch        sc->media = IFM_10G_TWINAX;
13599297884Sdavidcs        port_type = PORT_DA;
13600255736Sdavidch        break;
13601255736Sdavidch    case ELINK_ETH_PHY_BASE_T:
13602256299Sedavis        if (sc->link_params.speed_cap_mask[0] &
13603256299Sedavis            PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13604256299Sedavis            BLOGI(sc, "Found 10GBase-T media.\n");
13605256299Sedavis            sc->media = IFM_10G_T;
13606297884Sdavidcs            port_type = PORT_TP;
13607256299Sedavis        } else {
13608256299Sedavis            BLOGI(sc, "Found 1000Base-T media.\n");
13609256299Sedavis            sc->media = IFM_1000_T;
13610297884Sdavidcs            port_type = PORT_TP;
13611256299Sedavis        }
13612255736Sdavidch        break;
13613255736Sdavidch    case ELINK_ETH_PHY_NOT_PRESENT:
13614255736Sdavidch        BLOGI(sc, "Media not present.\n");
13615255736Sdavidch        sc->media = 0;
13616297884Sdavidcs        port_type = PORT_OTHER;
13617255736Sdavidch        break;
13618255736Sdavidch    case ELINK_ETH_PHY_UNSPECIFIED:
13619255736Sdavidch    default:
13620255736Sdavidch        BLOGI(sc, "Unknown media!\n");
13621255736Sdavidch        sc->media = 0;
13622297884Sdavidcs        port_type = PORT_OTHER;
13623255736Sdavidch        break;
13624255736Sdavidch    }
13625297884Sdavidcs    return port_type;
13626255736Sdavidch}
13627255736Sdavidch
13628255736Sdavidch#define GET_FIELD(value, fname)                     \
13629255736Sdavidch    (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13630255736Sdavidch#define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13631255736Sdavidch#define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
13632255736Sdavidch
13633255736Sdavidchstatic int
13634255736Sdavidchbxe_get_igu_cam_info(struct bxe_softc *sc)
13635255736Sdavidch{
13636255736Sdavidch    int pfid = SC_FUNC(sc);
13637255736Sdavidch    int igu_sb_id;
13638255736Sdavidch    uint32_t val;
13639255736Sdavidch    uint8_t fid, igu_sb_cnt = 0;
13640255736Sdavidch
13641255736Sdavidch    sc->igu_base_sb = 0xff;
13642255736Sdavidch
13643255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
13644255736Sdavidch        int vn = SC_VN(sc);
13645255736Sdavidch        igu_sb_cnt = sc->igu_sb_cnt;
13646255736Sdavidch        sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
13647255736Sdavidch                           FP_SB_MAX_E1x);
13648255736Sdavidch        sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
13649255736Sdavidch                          (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
13650255736Sdavidch        return (0);
13651255736Sdavidch    }
13652255736Sdavidch
13653255736Sdavidch    /* IGU in normal mode - read CAM */
13654255736Sdavidch    for (igu_sb_id = 0;
13655255736Sdavidch         igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
13656255736Sdavidch         igu_sb_id++) {
13657255736Sdavidch        val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
13658255736Sdavidch        if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
13659255736Sdavidch            continue;
13660255736Sdavidch        }
13661255736Sdavidch        fid = IGU_FID(val);
13662255736Sdavidch        if ((fid & IGU_FID_ENCODE_IS_PF)) {
13663255736Sdavidch            if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
13664255736Sdavidch                continue;
13665255736Sdavidch            }
13666255736Sdavidch            if (IGU_VEC(val) == 0) {
13667255736Sdavidch                /* default status block */
13668255736Sdavidch                sc->igu_dsb_id = igu_sb_id;
13669255736Sdavidch            } else {
13670255736Sdavidch                if (sc->igu_base_sb == 0xff) {
13671255736Sdavidch                    sc->igu_base_sb = igu_sb_id;
13672255736Sdavidch                }
13673255736Sdavidch                igu_sb_cnt++;
13674255736Sdavidch            }
13675255736Sdavidch        }
13676255736Sdavidch    }
13677255736Sdavidch
13678255736Sdavidch    /*
13679255736Sdavidch     * Due to new PF resource allocation by MFW T7.4 and above, it's optional
13680255736Sdavidch     * that number of CAM entries will not be equal to the value advertised in
13681255736Sdavidch     * PCI. Driver should use the minimal value of both as the actual status
13682255736Sdavidch     * block count
13683255736Sdavidch     */
13684255736Sdavidch    sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
13685255736Sdavidch
13686255736Sdavidch    if (igu_sb_cnt == 0) {
13687255736Sdavidch        BLOGE(sc, "CAM configuration error\n");
13688255736Sdavidch        return (-1);
13689255736Sdavidch    }
13690255736Sdavidch
13691255736Sdavidch    return (0);
13692255736Sdavidch}
13693255736Sdavidch
13694255736Sdavidch/*
13695255736Sdavidch * Gather various information from the device config space, the device itself,
13696255736Sdavidch * shmem, and the user input.
13697255736Sdavidch */
13698255736Sdavidchstatic int
13699255736Sdavidchbxe_get_device_info(struct bxe_softc *sc)
13700255736Sdavidch{
13701255736Sdavidch    uint32_t val;
13702255736Sdavidch    int rc;
13703255736Sdavidch
13704255736Sdavidch    /* Get the data for the device */
13705255736Sdavidch    sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
13706255736Sdavidch    sc->devinfo.device_id    = pci_get_device(sc->dev);
13707255736Sdavidch    sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
13708255736Sdavidch    sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
13709255736Sdavidch
13710255736Sdavidch    /* get the chip revision (chip metal comes from pci config space) */
13711255736Sdavidch    sc->devinfo.chip_id     =
13712255736Sdavidch    sc->link_params.chip_id =
13713255736Sdavidch        (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
13714255736Sdavidch         ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
13715255736Sdavidch         (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
13716255736Sdavidch         ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
13717255736Sdavidch
13718255736Sdavidch    /* force 57811 according to MISC register */
13719255736Sdavidch    if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
13720255736Sdavidch        if (CHIP_IS_57810(sc)) {
13721255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
13722255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
13723255736Sdavidch        } else if (CHIP_IS_57810_MF(sc)) {
13724255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
13725255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
13726255736Sdavidch        }
13727255736Sdavidch        sc->devinfo.chip_id |= 0x1;
13728255736Sdavidch    }
13729255736Sdavidch
13730255736Sdavidch    BLOGD(sc, DBG_LOAD,
13731255736Sdavidch          "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
13732255736Sdavidch          sc->devinfo.chip_id,
13733255736Sdavidch          ((sc->devinfo.chip_id >> 16) & 0xffff),
13734255736Sdavidch          ((sc->devinfo.chip_id >> 12) & 0xf),
13735255736Sdavidch          ((sc->devinfo.chip_id >>  4) & 0xff),
13736255736Sdavidch          ((sc->devinfo.chip_id >>  0) & 0xf));
13737255736Sdavidch
13738255736Sdavidch    val = (REG_RD(sc, 0x2874) & 0x55);
13739255736Sdavidch    if ((sc->devinfo.chip_id & 0x1) ||
13740255736Sdavidch        (CHIP_IS_E1(sc) && val) ||
13741255736Sdavidch        (CHIP_IS_E1H(sc) && (val == 0x55))) {
13742255736Sdavidch        sc->flags |= BXE_ONE_PORT_FLAG;
13743255736Sdavidch        BLOGD(sc, DBG_LOAD, "single port device\n");
13744255736Sdavidch    }
13745255736Sdavidch
13746255736Sdavidch    /* set the doorbell size */
13747255736Sdavidch    sc->doorbell_size = (1 << BXE_DB_SHIFT);
13748255736Sdavidch
13749255736Sdavidch    /* determine whether the device is in 2 port or 4 port mode */
13750255736Sdavidch    sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
13751255736Sdavidch    if (CHIP_IS_E2E3(sc)) {
13752255736Sdavidch        /*
13753255736Sdavidch         * Read port4mode_en_ovwr[0]:
13754255736Sdavidch         *   If 1, four port mode is in port4mode_en_ovwr[1].
13755255736Sdavidch         *   If 0, four port mode is in port4mode_en[0].
13756255736Sdavidch         */
13757255736Sdavidch        val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
13758255736Sdavidch        if (val & 1) {
13759255736Sdavidch            val = ((val >> 1) & 1);
13760255736Sdavidch        } else {
13761255736Sdavidch            val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
13762255736Sdavidch        }
13763255736Sdavidch
13764255736Sdavidch        sc->devinfo.chip_port_mode =
13765255736Sdavidch            (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
13766255736Sdavidch
13767255736Sdavidch        BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
13768255736Sdavidch    }
13769255736Sdavidch
13770255736Sdavidch    /* get the function and path info for the device */
13771255736Sdavidch    bxe_get_function_num(sc);
13772255736Sdavidch
13773255736Sdavidch    /* get the shared memory base address */
13774255736Sdavidch    sc->devinfo.shmem_base     =
13775255736Sdavidch    sc->link_params.shmem_base =
13776255736Sdavidch        REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
13777255736Sdavidch    sc->devinfo.shmem2_base =
13778255736Sdavidch        REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
13779255736Sdavidch                                  MISC_REG_GENERIC_CR_0));
13780255736Sdavidch
13781255736Sdavidch    BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
13782255736Sdavidch          sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
13783255736Sdavidch
13784255736Sdavidch    if (!sc->devinfo.shmem_base) {
13785255736Sdavidch        /* this should ONLY prevent upcoming shmem reads */
13786255736Sdavidch        BLOGI(sc, "MCP not active\n");
13787255736Sdavidch        sc->flags |= BXE_NO_MCP_FLAG;
13788255736Sdavidch        return (0);
13789255736Sdavidch    }
13790255736Sdavidch
13791255736Sdavidch    /* make sure the shared memory contents are valid */
13792255736Sdavidch    val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
13793255736Sdavidch    if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
13794255736Sdavidch        (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
13795255736Sdavidch        BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
13796255736Sdavidch        return (0);
13797255736Sdavidch    }
13798255736Sdavidch    BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
13799255736Sdavidch
13800255736Sdavidch    /* get the bootcode version */
13801255736Sdavidch    sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
13802255736Sdavidch    snprintf(sc->devinfo.bc_ver_str,
13803255736Sdavidch             sizeof(sc->devinfo.bc_ver_str),
13804255736Sdavidch             "%d.%d.%d",
13805255736Sdavidch             ((sc->devinfo.bc_ver >> 24) & 0xff),
13806255736Sdavidch             ((sc->devinfo.bc_ver >> 16) & 0xff),
13807255736Sdavidch             ((sc->devinfo.bc_ver >>  8) & 0xff));
13808255736Sdavidch    BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
13809255736Sdavidch
13810255736Sdavidch    /* get the bootcode shmem address */
13811255736Sdavidch    sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
13812255736Sdavidch    BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
13813255736Sdavidch
13814255736Sdavidch    /* clean indirect addresses as they're not used */
13815255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
13816255736Sdavidch    if (IS_PF(sc)) {
13817255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
13818255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
13819255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
13820255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
13821255736Sdavidch        if (CHIP_IS_E1x(sc)) {
13822255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
13823255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
13824255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
13825255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
13826255736Sdavidch        }
13827255736Sdavidch
13828255736Sdavidch        /*
13829255736Sdavidch         * Enable internal target-read (in case we are probed after PF
13830255736Sdavidch         * FLR). Must be done prior to any BAR read access. Only for
13831255736Sdavidch         * 57712 and up
13832255736Sdavidch         */
13833255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
13834255736Sdavidch            REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
13835255736Sdavidch        }
13836255736Sdavidch    }
13837255736Sdavidch
13838255736Sdavidch    /* get the nvram size */
13839255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
13840255736Sdavidch    sc->devinfo.flash_size =
13841255736Sdavidch        (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
13842255736Sdavidch    BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
13843255736Sdavidch
13844255736Sdavidch    /* get PCI capabilites */
13845255736Sdavidch    bxe_probe_pci_caps(sc);
13846255736Sdavidch
13847255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
13848255736Sdavidch
13849255736Sdavidch    /* get various configuration parameters from shmem */
13850255736Sdavidch    bxe_get_shmem_info(sc);
13851255736Sdavidch
13852255736Sdavidch    if (sc->devinfo.pcie_msix_cap_reg != 0) {
13853255736Sdavidch        val = pci_read_config(sc->dev,
13854255736Sdavidch                              (sc->devinfo.pcie_msix_cap_reg +
13855255736Sdavidch                               PCIR_MSIX_CTRL),
13856255736Sdavidch                              2);
13857255736Sdavidch        sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
13858255736Sdavidch    } else {
13859255736Sdavidch        sc->igu_sb_cnt = 1;
13860255736Sdavidch    }
13861255736Sdavidch
13862255736Sdavidch    sc->igu_base_addr = BAR_IGU_INTMEM;
13863255736Sdavidch
13864255736Sdavidch    /* initialize IGU parameters */
13865255736Sdavidch    if (CHIP_IS_E1x(sc)) {
13866255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_HC;
13867255736Sdavidch        sc->igu_dsb_id = DEF_SB_IGU_ID;
13868255736Sdavidch        sc->igu_base_sb = 0;
13869255736Sdavidch    } else {
13870255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_IGU;
13871255736Sdavidch
13872255736Sdavidch        /* do not allow device reset during IGU info preocessing */
13873255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13874255736Sdavidch
13875255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
13876255736Sdavidch
13877255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13878255736Sdavidch            int tout = 5000;
13879255736Sdavidch
13880255736Sdavidch            BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
13881255736Sdavidch
13882255736Sdavidch            val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
13883255736Sdavidch            REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
13884255736Sdavidch            REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
13885255736Sdavidch
13886255736Sdavidch            while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13887255736Sdavidch                tout--;
13888255736Sdavidch                DELAY(1000);
13889255736Sdavidch            }
13890255736Sdavidch
13891255736Sdavidch            if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13892255736Sdavidch                BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
13893255736Sdavidch                bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13894255736Sdavidch                return (-1);
13895255736Sdavidch            }
13896255736Sdavidch        }
13897255736Sdavidch
13898255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13899255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
13900255736Sdavidch            sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
13901255736Sdavidch        } else {
13902255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
13903255736Sdavidch        }
13904255736Sdavidch
13905255736Sdavidch        rc = bxe_get_igu_cam_info(sc);
13906255736Sdavidch
13907255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13908255736Sdavidch
13909255736Sdavidch        if (rc) {
13910255736Sdavidch            return (rc);
13911255736Sdavidch        }
13912255736Sdavidch    }
13913255736Sdavidch
13914255736Sdavidch    /*
13915255736Sdavidch     * Get base FW non-default (fast path) status block ID. This value is
13916255736Sdavidch     * used to initialize the fw_sb_id saved on the fp/queue structure to
13917255736Sdavidch     * determine the id used by the FW.
13918255736Sdavidch     */
13919255736Sdavidch    if (CHIP_IS_E1x(sc)) {
13920255736Sdavidch        sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
13921255736Sdavidch    } else {
13922255736Sdavidch        /*
13923255736Sdavidch         * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
13924255736Sdavidch         * the same queue are indicated on the same IGU SB). So we prefer
13925255736Sdavidch         * FW and IGU SBs to be the same value.
13926255736Sdavidch         */
13927255736Sdavidch        sc->base_fw_ndsb = sc->igu_base_sb;
13928255736Sdavidch    }
13929255736Sdavidch
13930255736Sdavidch    BLOGD(sc, DBG_LOAD,
13931255736Sdavidch          "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
13932255736Sdavidch          sc->igu_dsb_id, sc->igu_base_sb,
13933255736Sdavidch          sc->igu_sb_cnt, sc->base_fw_ndsb);
13934255736Sdavidch
13935255736Sdavidch    elink_phy_probe(&sc->link_params);
13936255736Sdavidch
13937255736Sdavidch    return (0);
13938255736Sdavidch}
13939255736Sdavidch
13940255736Sdavidchstatic void
13941255736Sdavidchbxe_link_settings_supported(struct bxe_softc *sc,
13942255736Sdavidch                            uint32_t         switch_cfg)
13943255736Sdavidch{
13944255736Sdavidch    uint32_t cfg_size = 0;
13945255736Sdavidch    uint32_t idx;
13946255736Sdavidch    uint8_t port = SC_PORT(sc);
13947255736Sdavidch
13948255736Sdavidch    /* aggregation of supported attributes of all external phys */
13949255736Sdavidch    sc->port.supported[0] = 0;
13950255736Sdavidch    sc->port.supported[1] = 0;
13951255736Sdavidch
13952255736Sdavidch    switch (sc->link_params.num_phys) {
13953255736Sdavidch    case 1:
13954255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
13955255736Sdavidch        cfg_size = 1;
13956255736Sdavidch        break;
13957255736Sdavidch    case 2:
13958255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
13959255736Sdavidch        cfg_size = 1;
13960255736Sdavidch        break;
13961255736Sdavidch    case 3:
13962255736Sdavidch        if (sc->link_params.multi_phy_config &
13963255736Sdavidch            PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
13964255736Sdavidch            sc->port.supported[1] =
13965255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
13966255736Sdavidch            sc->port.supported[0] =
13967255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
13968255736Sdavidch        } else {
13969255736Sdavidch            sc->port.supported[0] =
13970255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
13971255736Sdavidch            sc->port.supported[1] =
13972255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
13973255736Sdavidch        }
13974255736Sdavidch        cfg_size = 2;
13975255736Sdavidch        break;
13976255736Sdavidch    }
13977255736Sdavidch
13978255736Sdavidch    if (!(sc->port.supported[0] || sc->port.supported[1])) {
13979255736Sdavidch        BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
13980255736Sdavidch              SHMEM_RD(sc,
13981255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config),
13982255736Sdavidch              SHMEM_RD(sc,
13983255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config2));
13984255736Sdavidch        return;
13985255736Sdavidch    }
13986255736Sdavidch
13987255736Sdavidch    if (CHIP_IS_E3(sc))
13988255736Sdavidch        sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
13989255736Sdavidch    else {
13990255736Sdavidch        switch (switch_cfg) {
13991255736Sdavidch        case ELINK_SWITCH_CFG_1G:
13992255736Sdavidch            sc->port.phy_addr =
13993255736Sdavidch                REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
13994255736Sdavidch            break;
13995255736Sdavidch        case ELINK_SWITCH_CFG_10G:
13996255736Sdavidch            sc->port.phy_addr =
13997255736Sdavidch                REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
13998255736Sdavidch            break;
13999255736Sdavidch        default:
14000255736Sdavidch            BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14001255736Sdavidch                  sc->port.link_config[0]);
14002255736Sdavidch            return;
14003255736Sdavidch        }
14004255736Sdavidch    }
14005255736Sdavidch
14006255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14007255736Sdavidch
14008255736Sdavidch    /* mask what we support according to speed_cap_mask per configuration */
14009255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14010255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14011255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14012255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14013255736Sdavidch        }
14014255736Sdavidch
14015255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14016255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14017255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14018255736Sdavidch        }
14019255736Sdavidch
14020255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14021255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14022255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14023255736Sdavidch        }
14024255736Sdavidch
14025255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14026255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14027255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14028255736Sdavidch        }
14029255736Sdavidch
14030255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14031255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14032255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14033255736Sdavidch        }
14034255736Sdavidch
14035255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14036255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14037255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14038255736Sdavidch        }
14039255736Sdavidch
14040255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14041255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14042255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14043255736Sdavidch        }
14044255736Sdavidch
14045255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14046255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14047255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14048255736Sdavidch        }
14049255736Sdavidch    }
14050255736Sdavidch
14051255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14052255736Sdavidch          sc->port.supported[0], sc->port.supported[1]);
14053315881Sdavidcs	ELINK_DEBUG_P2(sc, "PHY supported 0=0x%08x 1=0x%08x\n",
14054315881Sdavidcs					sc->port.supported[0], sc->port.supported[1]);
14055255736Sdavidch}
14056255736Sdavidch
14057255736Sdavidchstatic void
14058255736Sdavidchbxe_link_settings_requested(struct bxe_softc *sc)
14059255736Sdavidch{
14060255736Sdavidch    uint32_t link_config;
14061255736Sdavidch    uint32_t idx;
14062255736Sdavidch    uint32_t cfg_size = 0;
14063255736Sdavidch
14064255736Sdavidch    sc->port.advertising[0] = 0;
14065255736Sdavidch    sc->port.advertising[1] = 0;
14066255736Sdavidch
14067255736Sdavidch    switch (sc->link_params.num_phys) {
14068255736Sdavidch    case 1:
14069255736Sdavidch    case 2:
14070255736Sdavidch        cfg_size = 1;
14071255736Sdavidch        break;
14072255736Sdavidch    case 3:
14073255736Sdavidch        cfg_size = 2;
14074255736Sdavidch        break;
14075255736Sdavidch    }
14076255736Sdavidch
14077255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14078255736Sdavidch        sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14079255736Sdavidch        link_config = sc->port.link_config[idx];
14080255736Sdavidch
14081255736Sdavidch        switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14082255736Sdavidch        case PORT_FEATURE_LINK_SPEED_AUTO:
14083255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14084255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14085255736Sdavidch                sc->port.advertising[idx] |= sc->port.supported[idx];
14086255736Sdavidch                if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14087255736Sdavidch                    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14088255736Sdavidch                    sc->port.advertising[idx] |=
14089255736Sdavidch                        (ELINK_SUPPORTED_100baseT_Half |
14090255736Sdavidch                         ELINK_SUPPORTED_100baseT_Full);
14091255736Sdavidch            } else {
14092255736Sdavidch                /* force 10G, no AN */
14093255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14094255736Sdavidch                sc->port.advertising[idx] |=
14095255736Sdavidch                    (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14096255736Sdavidch                continue;
14097255736Sdavidch            }
14098255736Sdavidch            break;
14099255736Sdavidch
14100255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_FULL:
14101255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14102255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14103255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14104255736Sdavidch                                              ADVERTISED_TP);
14105255736Sdavidch            } else {
14106255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14107255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14108255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14109255736Sdavidch                return;
14110255736Sdavidch            }
14111255736Sdavidch            break;
14112255736Sdavidch
14113255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_HALF:
14114255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14115255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14116255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14117255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14118255736Sdavidch                                              ADVERTISED_TP);
14119315881Sdavidcs				ELINK_DEBUG_P1(sc, "driver requesting DUPLEX_HALF req_duplex = %x!\n",
14120315881Sdavidcs								sc->link_params.req_duplex[idx]);
14121255736Sdavidch            } else {
14122255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14123255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14124255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14125255736Sdavidch                return;
14126255736Sdavidch            }
14127255736Sdavidch            break;
14128255736Sdavidch
14129255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_FULL:
14130255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14131255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14132255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14133255736Sdavidch                                              ADVERTISED_TP);
14134255736Sdavidch            } else {
14135255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14136255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14137255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14138255736Sdavidch                return;
14139255736Sdavidch            }
14140255736Sdavidch            break;
14141255736Sdavidch
14142255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_HALF:
14143255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14144255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14145255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14146255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14147255736Sdavidch                                              ADVERTISED_TP);
14148255736Sdavidch            } else {
14149255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14150255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14151255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14152255736Sdavidch                return;
14153255736Sdavidch            }
14154255736Sdavidch            break;
14155255736Sdavidch
14156255736Sdavidch        case PORT_FEATURE_LINK_SPEED_1G:
14157255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14158255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14159255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14160255736Sdavidch                                              ADVERTISED_TP);
14161255736Sdavidch            } else {
14162255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14163255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14164255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14165255736Sdavidch                return;
14166255736Sdavidch            }
14167255736Sdavidch            break;
14168255736Sdavidch
14169255736Sdavidch        case PORT_FEATURE_LINK_SPEED_2_5G:
14170255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14171255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14172255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14173255736Sdavidch                                              ADVERTISED_TP);
14174255736Sdavidch            } else {
14175255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14176255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14177255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14178255736Sdavidch                return;
14179255736Sdavidch            }
14180255736Sdavidch            break;
14181255736Sdavidch
14182255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10G_CX4:
14183255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14184255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14185255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14186255736Sdavidch                                              ADVERTISED_FIBRE);
14187255736Sdavidch            } else {
14188255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14189255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14190255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14191255736Sdavidch                return;
14192255736Sdavidch            }
14193255736Sdavidch            break;
14194255736Sdavidch
14195255736Sdavidch        case PORT_FEATURE_LINK_SPEED_20G:
14196255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14197255736Sdavidch            break;
14198255736Sdavidch
14199255736Sdavidch        default:
14200255736Sdavidch            BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14201255736Sdavidch                      "speed_cap_mask=0x%08x\n",
14202255736Sdavidch                  link_config, sc->link_params.speed_cap_mask[idx]);
14203255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14204255736Sdavidch            sc->port.advertising[idx] = sc->port.supported[idx];
14205255736Sdavidch            break;
14206255736Sdavidch        }
14207255736Sdavidch
14208255736Sdavidch        sc->link_params.req_flow_ctrl[idx] =
14209255736Sdavidch            (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14210255736Sdavidch
14211255736Sdavidch        if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14212255736Sdavidch            if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14213255736Sdavidch                sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14214255736Sdavidch            } else {
14215255736Sdavidch                bxe_set_requested_fc(sc);
14216255736Sdavidch            }
14217255736Sdavidch        }
14218255736Sdavidch
14219255736Sdavidch        BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14220255736Sdavidch                            "req_flow_ctrl=0x%x advertising=0x%x\n",
14221255736Sdavidch              sc->link_params.req_line_speed[idx],
14222255736Sdavidch              sc->link_params.req_duplex[idx],
14223255736Sdavidch              sc->link_params.req_flow_ctrl[idx],
14224255736Sdavidch              sc->port.advertising[idx]);
14225315881Sdavidcs		ELINK_DEBUG_P3(sc, "req_line_speed=%d req_duplex=%d "
14226315881Sdavidcs						"advertising=0x%x\n",
14227315881Sdavidcs						sc->link_params.req_line_speed[idx],
14228315881Sdavidcs						sc->link_params.req_duplex[idx],
14229315881Sdavidcs						sc->port.advertising[idx]);
14230255736Sdavidch    }
14231255736Sdavidch}
14232255736Sdavidch
14233255736Sdavidchstatic void
14234255736Sdavidchbxe_get_phy_info(struct bxe_softc *sc)
14235255736Sdavidch{
14236255736Sdavidch    uint8_t port = SC_PORT(sc);
14237255736Sdavidch    uint32_t config = sc->port.config;
14238255736Sdavidch    uint32_t eee_mode;
14239255736Sdavidch
14240255736Sdavidch    /* shmem data already read in bxe_get_shmem_info() */
14241255736Sdavidch
14242315881Sdavidcs    ELINK_DEBUG_P3(sc, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14243255736Sdavidch                        "link_config0=0x%08x\n",
14244255736Sdavidch               sc->link_params.lane_config,
14245255736Sdavidch               sc->link_params.speed_cap_mask[0],
14246255736Sdavidch               sc->port.link_config[0]);
14247315881Sdavidcs
14248255736Sdavidch
14249255736Sdavidch    bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14250255736Sdavidch    bxe_link_settings_requested(sc);
14251255736Sdavidch
14252255736Sdavidch    if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14253255736Sdavidch        sc->link_params.feature_config_flags |=
14254255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14255255736Sdavidch    } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14256255736Sdavidch        sc->link_params.feature_config_flags &=
14257255736Sdavidch            ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14258255736Sdavidch    } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14259255736Sdavidch        sc->link_params.feature_config_flags |=
14260255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14261255736Sdavidch    }
14262255736Sdavidch
14263255736Sdavidch    /* configure link feature according to nvram value */
14264255736Sdavidch    eee_mode =
14265255736Sdavidch        (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14266255736Sdavidch          PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14267255736Sdavidch         PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14268255736Sdavidch    if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14269255736Sdavidch        sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14270255736Sdavidch                                    ELINK_EEE_MODE_ENABLE_LPI |
14271255736Sdavidch                                    ELINK_EEE_MODE_OUTPUT_TIME);
14272255736Sdavidch    } else {
14273255736Sdavidch        sc->link_params.eee_mode = 0;
14274255736Sdavidch    }
14275255736Sdavidch
14276255736Sdavidch    /* get the media type */
14277255736Sdavidch    bxe_media_detect(sc);
14278315881Sdavidcs	ELINK_DEBUG_P1(sc, "detected media type\n", sc->media);
14279255736Sdavidch}
14280255736Sdavidch
14281255736Sdavidchstatic void
14282255736Sdavidchbxe_get_params(struct bxe_softc *sc)
14283255736Sdavidch{
14284255736Sdavidch    /* get user tunable params */
14285255736Sdavidch    bxe_get_tunable_params(sc);
14286255736Sdavidch
14287255736Sdavidch    /* select the RX and TX ring sizes */
14288255736Sdavidch    sc->tx_ring_size = TX_BD_USABLE;
14289255736Sdavidch    sc->rx_ring_size = RX_BD_USABLE;
14290255736Sdavidch
14291255736Sdavidch    /* XXX disable WoL */
14292255736Sdavidch    sc->wol = 0;
14293255736Sdavidch}
14294255736Sdavidch
14295255736Sdavidchstatic void
14296255736Sdavidchbxe_set_modes_bitmap(struct bxe_softc *sc)
14297255736Sdavidch{
14298255736Sdavidch    uint32_t flags = 0;
14299255736Sdavidch
14300255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
14301255736Sdavidch        SET_FLAGS(flags, MODE_FPGA);
14302255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
14303255736Sdavidch        SET_FLAGS(flags, MODE_EMUL);
14304255736Sdavidch    } else {
14305255736Sdavidch        SET_FLAGS(flags, MODE_ASIC);
14306255736Sdavidch    }
14307255736Sdavidch
14308255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
14309255736Sdavidch        SET_FLAGS(flags, MODE_PORT4);
14310255736Sdavidch    } else {
14311255736Sdavidch        SET_FLAGS(flags, MODE_PORT2);
14312255736Sdavidch    }
14313255736Sdavidch
14314255736Sdavidch    if (CHIP_IS_E2(sc)) {
14315255736Sdavidch        SET_FLAGS(flags, MODE_E2);
14316255736Sdavidch    } else if (CHIP_IS_E3(sc)) {
14317255736Sdavidch        SET_FLAGS(flags, MODE_E3);
14318255736Sdavidch        if (CHIP_REV(sc) == CHIP_REV_Ax) {
14319255736Sdavidch            SET_FLAGS(flags, MODE_E3_A0);
14320255736Sdavidch        } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14321255736Sdavidch            SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14322255736Sdavidch        }
14323255736Sdavidch    }
14324255736Sdavidch
14325255736Sdavidch    if (IS_MF(sc)) {
14326255736Sdavidch        SET_FLAGS(flags, MODE_MF);
14327255736Sdavidch        switch (sc->devinfo.mf_info.mf_mode) {
14328255736Sdavidch        case MULTI_FUNCTION_SD:
14329255736Sdavidch            SET_FLAGS(flags, MODE_MF_SD);
14330255736Sdavidch            break;
14331255736Sdavidch        case MULTI_FUNCTION_SI:
14332255736Sdavidch            SET_FLAGS(flags, MODE_MF_SI);
14333255736Sdavidch            break;
14334255736Sdavidch        case MULTI_FUNCTION_AFEX:
14335255736Sdavidch            SET_FLAGS(flags, MODE_MF_AFEX);
14336255736Sdavidch            break;
14337255736Sdavidch        }
14338255736Sdavidch    } else {
14339255736Sdavidch        SET_FLAGS(flags, MODE_SF);
14340255736Sdavidch    }
14341255736Sdavidch
14342255736Sdavidch#if defined(__LITTLE_ENDIAN)
14343255736Sdavidch    SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14344255736Sdavidch#else /* __BIG_ENDIAN */
14345255736Sdavidch    SET_FLAGS(flags, MODE_BIG_ENDIAN);
14346255736Sdavidch#endif
14347255736Sdavidch
14348255736Sdavidch    INIT_MODE_FLAGS(sc) = flags;
14349255736Sdavidch}
14350255736Sdavidch
14351255736Sdavidchstatic int
14352255736Sdavidchbxe_alloc_hsi_mem(struct bxe_softc *sc)
14353255736Sdavidch{
14354255736Sdavidch    struct bxe_fastpath *fp;
14355255736Sdavidch    bus_addr_t busaddr;
14356255736Sdavidch    int max_agg_queues;
14357255736Sdavidch    int max_segments;
14358255736Sdavidch    bus_size_t max_size;
14359255736Sdavidch    bus_size_t max_seg_size;
14360255736Sdavidch    char buf[32];
14361255736Sdavidch    int rc;
14362255736Sdavidch    int i, j;
14363255736Sdavidch
14364255736Sdavidch    /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14365255736Sdavidch
14366255736Sdavidch    /* allocate the parent bus DMA tag */
14367255736Sdavidch    rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14368255736Sdavidch                            1,                        /* alignment */
14369255736Sdavidch                            0,                        /* boundary limit */
14370255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted low */
14371255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted hi */
14372255736Sdavidch                            NULL,                     /* addr filter() */
14373255736Sdavidch                            NULL,                     /* addr filter() arg */
14374255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14375255736Sdavidch                            BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14376255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14377255736Sdavidch                            0,                        /* flags */
14378255736Sdavidch                            NULL,                     /* lock() */
14379255736Sdavidch                            NULL,                     /* lock() arg */
14380255736Sdavidch                            &sc->parent_dma_tag);     /* returned dma tag */
14381255736Sdavidch    if (rc != 0) {
14382255736Sdavidch        BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14383255736Sdavidch        return (1);
14384255736Sdavidch    }
14385255736Sdavidch
14386255736Sdavidch    /************************/
14387255736Sdavidch    /* DEFAULT STATUS BLOCK */
14388255736Sdavidch    /************************/
14389255736Sdavidch
14390255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14391255736Sdavidch                      &sc->def_sb_dma, "default status block") != 0) {
14392255736Sdavidch        /* XXX */
14393255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14394255736Sdavidch        return (1);
14395255736Sdavidch    }
14396255736Sdavidch
14397255736Sdavidch    sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14398255736Sdavidch
14399255736Sdavidch    /***************/
14400255736Sdavidch    /* EVENT QUEUE */
14401255736Sdavidch    /***************/
14402255736Sdavidch
14403255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14404255736Sdavidch                      &sc->eq_dma, "event queue") != 0) {
14405255736Sdavidch        /* XXX */
14406255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14407255736Sdavidch        sc->def_sb = NULL;
14408255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14409255736Sdavidch        return (1);
14410255736Sdavidch    }
14411255736Sdavidch
14412255736Sdavidch    sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14413255736Sdavidch
14414255736Sdavidch    /*************/
14415255736Sdavidch    /* SLOW PATH */
14416255736Sdavidch    /*************/
14417255736Sdavidch
14418255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14419255736Sdavidch                      &sc->sp_dma, "slow path") != 0) {
14420255736Sdavidch        /* XXX */
14421255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14422255736Sdavidch        sc->eq = NULL;
14423255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14424255736Sdavidch        sc->def_sb = NULL;
14425255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14426255736Sdavidch        return (1);
14427255736Sdavidch    }
14428255736Sdavidch
14429255736Sdavidch    sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14430255736Sdavidch
14431255736Sdavidch    /*******************/
14432255736Sdavidch    /* SLOW PATH QUEUE */
14433255736Sdavidch    /*******************/
14434255736Sdavidch
14435255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14436255736Sdavidch                      &sc->spq_dma, "slow path queue") != 0) {
14437255736Sdavidch        /* XXX */
14438255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14439255736Sdavidch        sc->sp = NULL;
14440255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14441255736Sdavidch        sc->eq = NULL;
14442255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14443255736Sdavidch        sc->def_sb = NULL;
14444255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14445255736Sdavidch        return (1);
14446255736Sdavidch    }
14447255736Sdavidch
14448255736Sdavidch    sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14449255736Sdavidch
14450255736Sdavidch    /***************************/
14451255736Sdavidch    /* FW DECOMPRESSION BUFFER */
14452255736Sdavidch    /***************************/
14453255736Sdavidch
14454255736Sdavidch    if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14455255736Sdavidch                      "fw decompression buffer") != 0) {
14456255736Sdavidch        /* XXX */
14457255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14458255736Sdavidch        sc->spq = NULL;
14459255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14460255736Sdavidch        sc->sp = NULL;
14461255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14462255736Sdavidch        sc->eq = NULL;
14463255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14464255736Sdavidch        sc->def_sb = NULL;
14465255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14466255736Sdavidch        return (1);
14467255736Sdavidch    }
14468255736Sdavidch
14469255736Sdavidch    sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14470255736Sdavidch
14471255736Sdavidch    if ((sc->gz_strm =
14472255736Sdavidch         malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14473255736Sdavidch        /* XXX */
14474255736Sdavidch        bxe_dma_free(sc, &sc->gz_buf_dma);
14475255736Sdavidch        sc->gz_buf = NULL;
14476255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14477255736Sdavidch        sc->spq = NULL;
14478255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14479255736Sdavidch        sc->sp = NULL;
14480255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14481255736Sdavidch        sc->eq = NULL;
14482255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14483255736Sdavidch        sc->def_sb = NULL;
14484255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14485255736Sdavidch        return (1);
14486255736Sdavidch    }
14487255736Sdavidch
14488255736Sdavidch    /*************/
14489255736Sdavidch    /* FASTPATHS */
14490255736Sdavidch    /*************/
14491255736Sdavidch
14492255736Sdavidch    /* allocate DMA memory for each fastpath structure */
14493255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
14494255736Sdavidch        fp = &sc->fp[i];
14495255736Sdavidch        fp->sc    = sc;
14496255736Sdavidch        fp->index = i;
14497255736Sdavidch
14498255736Sdavidch        /*******************/
14499255736Sdavidch        /* FP STATUS BLOCK */
14500255736Sdavidch        /*******************/
14501255736Sdavidch
14502255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d status block", i);
14503255736Sdavidch        if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14504255736Sdavidch                          &fp->sb_dma, buf) != 0) {
14505255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14506255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14507255736Sdavidch            return (1);
14508255736Sdavidch        } else {
14509255736Sdavidch            if (CHIP_IS_E2E3(sc)) {
14510255736Sdavidch                fp->status_block.e2_sb =
14511255736Sdavidch                    (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14512255736Sdavidch            } else {
14513255736Sdavidch                fp->status_block.e1x_sb =
14514255736Sdavidch                    (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14515255736Sdavidch            }
14516255736Sdavidch        }
14517255736Sdavidch
14518255736Sdavidch        /******************/
14519255736Sdavidch        /* FP TX BD CHAIN */
14520255736Sdavidch        /******************/
14521255736Sdavidch
14522255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14523255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14524255736Sdavidch                          &fp->tx_dma, buf) != 0) {
14525255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14526255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14527255736Sdavidch            return (1);
14528255736Sdavidch        } else {
14529255736Sdavidch            fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14530255736Sdavidch        }
14531255736Sdavidch
14532255736Sdavidch        /* link together the tx bd chain pages */
14533255736Sdavidch        for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14534255736Sdavidch            /* index into the tx bd chain array to last entry per page */
14535255736Sdavidch            struct eth_tx_next_bd *tx_next_bd =
14536255736Sdavidch                &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14537255736Sdavidch            /* point to the next page and wrap from last page */
14538255736Sdavidch            busaddr = (fp->tx_dma.paddr +
14539255736Sdavidch                       (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14540255736Sdavidch            tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14541255736Sdavidch            tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14542255736Sdavidch        }
14543255736Sdavidch
14544255736Sdavidch        /******************/
14545255736Sdavidch        /* FP RX BD CHAIN */
14546255736Sdavidch        /******************/
14547255736Sdavidch
14548255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14549255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14550255736Sdavidch                          &fp->rx_dma, buf) != 0) {
14551255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14552255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14553255736Sdavidch            return (1);
14554255736Sdavidch        } else {
14555255736Sdavidch            fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14556255736Sdavidch        }
14557255736Sdavidch
14558255736Sdavidch        /* link together the rx bd chain pages */
14559255736Sdavidch        for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14560255736Sdavidch            /* index into the rx bd chain array to last entry per page */
14561255736Sdavidch            struct eth_rx_bd *rx_bd =
14562255736Sdavidch                &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14563255736Sdavidch            /* point to the next page and wrap from last page */
14564255736Sdavidch            busaddr = (fp->rx_dma.paddr +
14565255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14566255736Sdavidch            rx_bd->addr_hi = htole32(U64_HI(busaddr));
14567255736Sdavidch            rx_bd->addr_lo = htole32(U64_LO(busaddr));
14568255736Sdavidch        }
14569255736Sdavidch
14570255736Sdavidch        /*******************/
14571255736Sdavidch        /* FP RX RCQ CHAIN */
14572255736Sdavidch        /*******************/
14573255736Sdavidch
14574255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14575255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14576255736Sdavidch                          &fp->rcq_dma, buf) != 0) {
14577255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14578255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14579255736Sdavidch            return (1);
14580255736Sdavidch        } else {
14581255736Sdavidch            fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14582255736Sdavidch        }
14583255736Sdavidch
14584255736Sdavidch        /* link together the rcq chain pages */
14585255736Sdavidch        for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14586255736Sdavidch            /* index into the rcq chain array to last entry per page */
14587255736Sdavidch            struct eth_rx_cqe_next_page *rx_cqe_next =
14588255736Sdavidch                (struct eth_rx_cqe_next_page *)
14589255736Sdavidch                &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14590255736Sdavidch            /* point to the next page and wrap from last page */
14591255736Sdavidch            busaddr = (fp->rcq_dma.paddr +
14592255736Sdavidch                       (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14593255736Sdavidch            rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14594255736Sdavidch            rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14595255736Sdavidch        }
14596255736Sdavidch
14597255736Sdavidch        /*******************/
14598255736Sdavidch        /* FP RX SGE CHAIN */
14599255736Sdavidch        /*******************/
14600255736Sdavidch
14601255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14602255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14603255736Sdavidch                          &fp->rx_sge_dma, buf) != 0) {
14604255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14605255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14606255736Sdavidch            return (1);
14607255736Sdavidch        } else {
14608255736Sdavidch            fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14609255736Sdavidch        }
14610255736Sdavidch
14611255736Sdavidch        /* link together the sge chain pages */
14612255736Sdavidch        for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14613255736Sdavidch            /* index into the rcq chain array to last entry per page */
14614255736Sdavidch            struct eth_rx_sge *rx_sge =
14615255736Sdavidch                &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14616255736Sdavidch            /* point to the next page and wrap from last page */
14617255736Sdavidch            busaddr = (fp->rx_sge_dma.paddr +
14618255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14619255736Sdavidch            rx_sge->addr_hi = htole32(U64_HI(busaddr));
14620255736Sdavidch            rx_sge->addr_lo = htole32(U64_LO(busaddr));
14621255736Sdavidch        }
14622255736Sdavidch
14623255736Sdavidch        /***********************/
14624255736Sdavidch        /* FP TX MBUF DMA MAPS */
14625255736Sdavidch        /***********************/
14626255736Sdavidch
14627255736Sdavidch        /* set required sizes before mapping to conserve resources */
14628266979Smarcel        if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
14629255736Sdavidch            max_size     = BXE_TSO_MAX_SIZE;
14630255736Sdavidch            max_segments = BXE_TSO_MAX_SEGMENTS;
14631255736Sdavidch            max_seg_size = BXE_TSO_MAX_SEG_SIZE;
14632255736Sdavidch        } else {
14633255736Sdavidch            max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
14634255736Sdavidch            max_segments = BXE_MAX_SEGMENTS;
14635255736Sdavidch            max_seg_size = MCLBYTES;
14636255736Sdavidch        }
14637255736Sdavidch
14638255736Sdavidch        /* create a dma tag for the tx mbufs */
14639255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14640255736Sdavidch                                1,                  /* alignment */
14641255736Sdavidch                                0,                  /* boundary limit */
14642255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14643255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14644255736Sdavidch                                NULL,               /* addr filter() */
14645255736Sdavidch                                NULL,               /* addr filter() arg */
14646255736Sdavidch                                max_size,           /* max map size */
14647255736Sdavidch                                max_segments,       /* num discontinuous */
14648255736Sdavidch                                max_seg_size,       /* max seg size */
14649255736Sdavidch                                0,                  /* flags */
14650255736Sdavidch                                NULL,               /* lock() */
14651255736Sdavidch                                NULL,               /* lock() arg */
14652255736Sdavidch                                &fp->tx_mbuf_tag);  /* returned dma tag */
14653255736Sdavidch        if (rc != 0) {
14654255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14655255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14656295830Sdavidcs                      "'fp %d tx mbufs' (%d)\n", i, rc);
14657255736Sdavidch            return (1);
14658255736Sdavidch        }
14659255736Sdavidch
14660255736Sdavidch        /* create dma maps for each of the tx mbuf clusters */
14661255736Sdavidch        for (j = 0; j < TX_BD_TOTAL; j++) {
14662255736Sdavidch            if (bus_dmamap_create(fp->tx_mbuf_tag,
14663255736Sdavidch                                  BUS_DMA_NOWAIT,
14664255736Sdavidch                                  &fp->tx_mbuf_chain[j].m_map)) {
14665255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14666255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14667295830Sdavidcs                          "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
14668255736Sdavidch                return (1);
14669255736Sdavidch            }
14670255736Sdavidch        }
14671255736Sdavidch
14672255736Sdavidch        /***********************/
14673255736Sdavidch        /* FP RX MBUF DMA MAPS */
14674255736Sdavidch        /***********************/
14675255736Sdavidch
14676255736Sdavidch        /* create a dma tag for the rx mbufs */
14677255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14678255736Sdavidch                                1,                  /* alignment */
14679255736Sdavidch                                0,                  /* boundary limit */
14680255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14681255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14682255736Sdavidch                                NULL,               /* addr filter() */
14683255736Sdavidch                                NULL,               /* addr filter() arg */
14684255736Sdavidch                                MJUM9BYTES,         /* max map size */
14685255736Sdavidch                                1,                  /* num discontinuous */
14686255736Sdavidch                                MJUM9BYTES,         /* max seg size */
14687255736Sdavidch                                0,                  /* flags */
14688255736Sdavidch                                NULL,               /* lock() */
14689255736Sdavidch                                NULL,               /* lock() arg */
14690255736Sdavidch                                &fp->rx_mbuf_tag);  /* returned dma tag */
14691255736Sdavidch        if (rc != 0) {
14692255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14693255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14694295830Sdavidcs                      "'fp %d rx mbufs' (%d)\n", i, rc);
14695255736Sdavidch            return (1);
14696255736Sdavidch        }
14697255736Sdavidch
14698255736Sdavidch        /* create dma maps for each of the rx mbuf clusters */
14699255736Sdavidch        for (j = 0; j < RX_BD_TOTAL; j++) {
14700255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
14701255736Sdavidch                                  BUS_DMA_NOWAIT,
14702255736Sdavidch                                  &fp->rx_mbuf_chain[j].m_map)) {
14703255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14704255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14705295830Sdavidcs                          "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
14706255736Sdavidch                return (1);
14707255736Sdavidch            }
14708255736Sdavidch        }
14709255736Sdavidch
14710255736Sdavidch        /* create dma map for the spare rx mbuf cluster */
14711255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
14712255736Sdavidch                              BUS_DMA_NOWAIT,
14713255736Sdavidch                              &fp->rx_mbuf_spare_map)) {
14714255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14715255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14716295830Sdavidcs                      "'fp %d spare rx mbuf' (%d)\n", i, rc);
14717255736Sdavidch            return (1);
14718255736Sdavidch        }
14719255736Sdavidch
14720255736Sdavidch        /***************************/
14721255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
14722255736Sdavidch        /***************************/
14723255736Sdavidch
14724255736Sdavidch        /* create a dma tag for the rx sge mbufs */
14725255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14726255736Sdavidch                                1,                  /* alignment */
14727255736Sdavidch                                0,                  /* boundary limit */
14728255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14729255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14730255736Sdavidch                                NULL,               /* addr filter() */
14731255736Sdavidch                                NULL,               /* addr filter() arg */
14732255736Sdavidch                                BCM_PAGE_SIZE,      /* max map size */
14733255736Sdavidch                                1,                  /* num discontinuous */
14734255736Sdavidch                                BCM_PAGE_SIZE,      /* max seg size */
14735255736Sdavidch                                0,                  /* flags */
14736255736Sdavidch                                NULL,               /* lock() */
14737255736Sdavidch                                NULL,               /* lock() arg */
14738255736Sdavidch                                &fp->rx_sge_mbuf_tag); /* returned dma tag */
14739255736Sdavidch        if (rc != 0) {
14740255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14741255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14742295830Sdavidcs                      "'fp %d rx sge mbufs' (%d)\n", i, rc);
14743255736Sdavidch            return (1);
14744255736Sdavidch        }
14745255736Sdavidch
14746255736Sdavidch        /* create dma maps for the rx sge mbuf clusters */
14747255736Sdavidch        for (j = 0; j < RX_SGE_TOTAL; j++) {
14748255736Sdavidch            if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14749255736Sdavidch                                  BUS_DMA_NOWAIT,
14750255736Sdavidch                                  &fp->rx_sge_mbuf_chain[j].m_map)) {
14751255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14752255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14753295830Sdavidcs                          "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
14754255736Sdavidch                return (1);
14755255736Sdavidch            }
14756255736Sdavidch        }
14757255736Sdavidch
14758255736Sdavidch        /* create dma map for the spare rx sge mbuf cluster */
14759255736Sdavidch        if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14760255736Sdavidch                              BUS_DMA_NOWAIT,
14761255736Sdavidch                              &fp->rx_sge_mbuf_spare_map)) {
14762255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14763255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14764295830Sdavidcs                      "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
14765255736Sdavidch            return (1);
14766255736Sdavidch        }
14767255736Sdavidch
14768255736Sdavidch        /***************************/
14769255736Sdavidch        /* FP RX TPA MBUF DMA MAPS */
14770255736Sdavidch        /***************************/
14771255736Sdavidch
14772255736Sdavidch        /* create dma maps for the rx tpa mbuf clusters */
14773255736Sdavidch        max_agg_queues = MAX_AGG_QS(sc);
14774255736Sdavidch
14775255736Sdavidch        for (j = 0; j < max_agg_queues; j++) {
14776255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
14777255736Sdavidch                                  BUS_DMA_NOWAIT,
14778255736Sdavidch                                  &fp->rx_tpa_info[j].bd.m_map)) {
14779255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14780255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14781295830Sdavidcs                          "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
14782255736Sdavidch                return (1);
14783255736Sdavidch            }
14784255736Sdavidch        }
14785255736Sdavidch
14786255736Sdavidch        /* create dma map for the spare rx tpa mbuf cluster */
14787255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
14788255736Sdavidch                              BUS_DMA_NOWAIT,
14789255736Sdavidch                              &fp->rx_tpa_info_mbuf_spare_map)) {
14790255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14791255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14792295830Sdavidcs                      "'fp %d spare rx tpa mbuf' (%d)\n", i, rc);
14793255736Sdavidch            return (1);
14794255736Sdavidch        }
14795255736Sdavidch
14796255736Sdavidch        bxe_init_sge_ring_bit_mask(fp);
14797255736Sdavidch    }
14798255736Sdavidch
14799255736Sdavidch    return (0);
14800255736Sdavidch}
14801255736Sdavidch
14802255736Sdavidchstatic void
14803255736Sdavidchbxe_free_hsi_mem(struct bxe_softc *sc)
14804255736Sdavidch{
14805255736Sdavidch    struct bxe_fastpath *fp;
14806255736Sdavidch    int max_agg_queues;
14807255736Sdavidch    int i, j;
14808255736Sdavidch
14809255736Sdavidch    if (sc->parent_dma_tag == NULL) {
14810255736Sdavidch        return; /* assume nothing was allocated */
14811255736Sdavidch    }
14812255736Sdavidch
14813255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
14814255736Sdavidch        fp = &sc->fp[i];
14815255736Sdavidch
14816255736Sdavidch        /*******************/
14817255736Sdavidch        /* FP STATUS BLOCK */
14818255736Sdavidch        /*******************/
14819255736Sdavidch
14820255736Sdavidch        bxe_dma_free(sc, &fp->sb_dma);
14821255736Sdavidch        memset(&fp->status_block, 0, sizeof(fp->status_block));
14822255736Sdavidch
14823255736Sdavidch        /******************/
14824255736Sdavidch        /* FP TX BD CHAIN */
14825255736Sdavidch        /******************/
14826255736Sdavidch
14827255736Sdavidch        bxe_dma_free(sc, &fp->tx_dma);
14828255736Sdavidch        fp->tx_chain = NULL;
14829255736Sdavidch
14830255736Sdavidch        /******************/
14831255736Sdavidch        /* FP RX BD CHAIN */
14832255736Sdavidch        /******************/
14833255736Sdavidch
14834255736Sdavidch        bxe_dma_free(sc, &fp->rx_dma);
14835255736Sdavidch        fp->rx_chain = NULL;
14836255736Sdavidch
14837255736Sdavidch        /*******************/
14838255736Sdavidch        /* FP RX RCQ CHAIN */
14839255736Sdavidch        /*******************/
14840255736Sdavidch
14841255736Sdavidch        bxe_dma_free(sc, &fp->rcq_dma);
14842255736Sdavidch        fp->rcq_chain = NULL;
14843255736Sdavidch
14844255736Sdavidch        /*******************/
14845255736Sdavidch        /* FP RX SGE CHAIN */
14846255736Sdavidch        /*******************/
14847255736Sdavidch
14848255736Sdavidch        bxe_dma_free(sc, &fp->rx_sge_dma);
14849255736Sdavidch        fp->rx_sge_chain = NULL;
14850255736Sdavidch
14851255736Sdavidch        /***********************/
14852255736Sdavidch        /* FP TX MBUF DMA MAPS */
14853255736Sdavidch        /***********************/
14854255736Sdavidch
14855255736Sdavidch        if (fp->tx_mbuf_tag != NULL) {
14856255736Sdavidch            for (j = 0; j < TX_BD_TOTAL; j++) {
14857255736Sdavidch                if (fp->tx_mbuf_chain[j].m_map != NULL) {
14858255736Sdavidch                    bus_dmamap_unload(fp->tx_mbuf_tag,
14859255736Sdavidch                                      fp->tx_mbuf_chain[j].m_map);
14860255736Sdavidch                    bus_dmamap_destroy(fp->tx_mbuf_tag,
14861255736Sdavidch                                       fp->tx_mbuf_chain[j].m_map);
14862255736Sdavidch                }
14863255736Sdavidch            }
14864255736Sdavidch
14865255736Sdavidch            bus_dma_tag_destroy(fp->tx_mbuf_tag);
14866255736Sdavidch            fp->tx_mbuf_tag = NULL;
14867255736Sdavidch        }
14868255736Sdavidch
14869255736Sdavidch        /***********************/
14870255736Sdavidch        /* FP RX MBUF DMA MAPS */
14871255736Sdavidch        /***********************/
14872255736Sdavidch
14873255736Sdavidch        if (fp->rx_mbuf_tag != NULL) {
14874255736Sdavidch            for (j = 0; j < RX_BD_TOTAL; j++) {
14875255736Sdavidch                if (fp->rx_mbuf_chain[j].m_map != NULL) {
14876255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
14877255736Sdavidch                                      fp->rx_mbuf_chain[j].m_map);
14878255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
14879255736Sdavidch                                       fp->rx_mbuf_chain[j].m_map);
14880255736Sdavidch                }
14881255736Sdavidch            }
14882255736Sdavidch
14883255736Sdavidch            if (fp->rx_mbuf_spare_map != NULL) {
14884255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14885255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14886255736Sdavidch            }
14887255736Sdavidch
14888255736Sdavidch            /***************************/
14889255736Sdavidch            /* FP RX TPA MBUF DMA MAPS */
14890255736Sdavidch            /***************************/
14891255736Sdavidch
14892255736Sdavidch            max_agg_queues = MAX_AGG_QS(sc);
14893255736Sdavidch
14894255736Sdavidch            for (j = 0; j < max_agg_queues; j++) {
14895255736Sdavidch                if (fp->rx_tpa_info[j].bd.m_map != NULL) {
14896255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
14897255736Sdavidch                                      fp->rx_tpa_info[j].bd.m_map);
14898255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
14899255736Sdavidch                                       fp->rx_tpa_info[j].bd.m_map);
14900255736Sdavidch                }
14901255736Sdavidch            }
14902255736Sdavidch
14903255736Sdavidch            if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
14904255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag,
14905255736Sdavidch                                  fp->rx_tpa_info_mbuf_spare_map);
14906255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag,
14907255736Sdavidch                                   fp->rx_tpa_info_mbuf_spare_map);
14908255736Sdavidch            }
14909255736Sdavidch
14910255736Sdavidch            bus_dma_tag_destroy(fp->rx_mbuf_tag);
14911255736Sdavidch            fp->rx_mbuf_tag = NULL;
14912255736Sdavidch        }
14913255736Sdavidch
14914255736Sdavidch        /***************************/
14915255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
14916255736Sdavidch        /***************************/
14917255736Sdavidch
14918255736Sdavidch        if (fp->rx_sge_mbuf_tag != NULL) {
14919255736Sdavidch            for (j = 0; j < RX_SGE_TOTAL; j++) {
14920255736Sdavidch                if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
14921255736Sdavidch                    bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14922255736Sdavidch                                      fp->rx_sge_mbuf_chain[j].m_map);
14923255736Sdavidch                    bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14924255736Sdavidch                                       fp->rx_sge_mbuf_chain[j].m_map);
14925255736Sdavidch                }
14926255736Sdavidch            }
14927255736Sdavidch
14928255736Sdavidch            if (fp->rx_sge_mbuf_spare_map != NULL) {
14929255736Sdavidch                bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14930255736Sdavidch                                  fp->rx_sge_mbuf_spare_map);
14931255736Sdavidch                bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14932255736Sdavidch                                   fp->rx_sge_mbuf_spare_map);
14933255736Sdavidch            }
14934255736Sdavidch
14935255736Sdavidch            bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
14936255736Sdavidch            fp->rx_sge_mbuf_tag = NULL;
14937255736Sdavidch        }
14938255736Sdavidch    }
14939255736Sdavidch
14940255736Sdavidch    /***************************/
14941255736Sdavidch    /* FW DECOMPRESSION BUFFER */
14942255736Sdavidch    /***************************/
14943255736Sdavidch
14944255736Sdavidch    bxe_dma_free(sc, &sc->gz_buf_dma);
14945255736Sdavidch    sc->gz_buf = NULL;
14946255736Sdavidch    free(sc->gz_strm, M_DEVBUF);
14947255736Sdavidch    sc->gz_strm = NULL;
14948255736Sdavidch
14949255736Sdavidch    /*******************/
14950255736Sdavidch    /* SLOW PATH QUEUE */
14951255736Sdavidch    /*******************/
14952255736Sdavidch
14953255736Sdavidch    bxe_dma_free(sc, &sc->spq_dma);
14954255736Sdavidch    sc->spq = NULL;
14955255736Sdavidch
14956255736Sdavidch    /*************/
14957255736Sdavidch    /* SLOW PATH */
14958255736Sdavidch    /*************/
14959255736Sdavidch
14960255736Sdavidch    bxe_dma_free(sc, &sc->sp_dma);
14961255736Sdavidch    sc->sp = NULL;
14962255736Sdavidch
14963255736Sdavidch    /***************/
14964255736Sdavidch    /* EVENT QUEUE */
14965255736Sdavidch    /***************/
14966255736Sdavidch
14967255736Sdavidch    bxe_dma_free(sc, &sc->eq_dma);
14968255736Sdavidch    sc->eq = NULL;
14969255736Sdavidch
14970255736Sdavidch    /************************/
14971255736Sdavidch    /* DEFAULT STATUS BLOCK */
14972255736Sdavidch    /************************/
14973255736Sdavidch
14974255736Sdavidch    bxe_dma_free(sc, &sc->def_sb_dma);
14975255736Sdavidch    sc->def_sb = NULL;
14976255736Sdavidch
14977255736Sdavidch    bus_dma_tag_destroy(sc->parent_dma_tag);
14978255736Sdavidch    sc->parent_dma_tag = NULL;
14979255736Sdavidch}
14980255736Sdavidch
14981255736Sdavidch/*
14982255736Sdavidch * Previous driver DMAE transaction may have occurred when pre-boot stage
14983255736Sdavidch * ended and boot began. This would invalidate the addresses of the
14984255736Sdavidch * transaction, resulting in was-error bit set in the PCI causing all
14985255736Sdavidch * hw-to-host PCIe transactions to timeout. If this happened we want to clear
14986255736Sdavidch * the interrupt which detected this from the pglueb and the was-done bit
14987255736Sdavidch */
14988255736Sdavidchstatic void
14989255736Sdavidchbxe_prev_interrupted_dmae(struct bxe_softc *sc)
14990255736Sdavidch{
14991255736Sdavidch    uint32_t val;
14992255736Sdavidch
14993255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
14994255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
14995255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
14996255736Sdavidch            BLOGD(sc, DBG_LOAD,
14997255736Sdavidch                  "Clearing 'was-error' bit that was set in pglueb");
14998255736Sdavidch            REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
14999255736Sdavidch        }
15000255736Sdavidch    }
15001255736Sdavidch}
15002255736Sdavidch
15003255736Sdavidchstatic int
15004255736Sdavidchbxe_prev_mcp_done(struct bxe_softc *sc)
15005255736Sdavidch{
15006255736Sdavidch    uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15007255736Sdavidch                                 DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15008255736Sdavidch    if (!rc) {
15009255736Sdavidch        BLOGE(sc, "MCP response failure, aborting\n");
15010255736Sdavidch        return (-1);
15011255736Sdavidch    }
15012255736Sdavidch
15013255736Sdavidch    return (0);
15014255736Sdavidch}
15015255736Sdavidch
15016255736Sdavidchstatic struct bxe_prev_list_node *
15017255736Sdavidchbxe_prev_path_get_entry(struct bxe_softc *sc)
15018255736Sdavidch{
15019255736Sdavidch    struct bxe_prev_list_node *tmp;
15020255736Sdavidch
15021255736Sdavidch    LIST_FOREACH(tmp, &bxe_prev_list, node) {
15022255736Sdavidch        if ((sc->pcie_bus == tmp->bus) &&
15023255736Sdavidch            (sc->pcie_device == tmp->slot) &&
15024255736Sdavidch            (SC_PATH(sc) == tmp->path)) {
15025255736Sdavidch            return (tmp);
15026255736Sdavidch        }
15027255736Sdavidch    }
15028255736Sdavidch
15029255736Sdavidch    return (NULL);
15030255736Sdavidch}
15031255736Sdavidch
15032255736Sdavidchstatic uint8_t
15033255736Sdavidchbxe_prev_is_path_marked(struct bxe_softc *sc)
15034255736Sdavidch{
15035255736Sdavidch    struct bxe_prev_list_node *tmp;
15036255736Sdavidch    int rc = FALSE;
15037255736Sdavidch
15038255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15039255736Sdavidch
15040255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15041255736Sdavidch    if (tmp) {
15042255736Sdavidch        if (tmp->aer) {
15043255736Sdavidch            BLOGD(sc, DBG_LOAD,
15044255736Sdavidch                  "Path %d/%d/%d was marked by AER\n",
15045255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15046255736Sdavidch        } else {
15047255736Sdavidch            rc = TRUE;
15048255736Sdavidch            BLOGD(sc, DBG_LOAD,
15049255736Sdavidch                  "Path %d/%d/%d was already cleaned from previous drivers\n",
15050255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15051255736Sdavidch        }
15052255736Sdavidch    }
15053255736Sdavidch
15054255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15055255736Sdavidch
15056255736Sdavidch    return (rc);
15057255736Sdavidch}
15058255736Sdavidch
15059255736Sdavidchstatic int
15060255736Sdavidchbxe_prev_mark_path(struct bxe_softc *sc,
15061255736Sdavidch                   uint8_t          after_undi)
15062255736Sdavidch{
15063255736Sdavidch    struct bxe_prev_list_node *tmp;
15064255736Sdavidch
15065255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15066255736Sdavidch
15067255736Sdavidch    /* Check whether the entry for this path already exists */
15068255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15069255736Sdavidch    if (tmp) {
15070255736Sdavidch        if (!tmp->aer) {
15071255736Sdavidch            BLOGD(sc, DBG_LOAD,
15072255736Sdavidch                  "Re-marking AER in path %d/%d/%d\n",
15073255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15074255736Sdavidch        } else {
15075255736Sdavidch            BLOGD(sc, DBG_LOAD,
15076255736Sdavidch                  "Removing AER indication from path %d/%d/%d\n",
15077255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15078255736Sdavidch            tmp->aer = 0;
15079255736Sdavidch        }
15080255736Sdavidch
15081255736Sdavidch        mtx_unlock(&bxe_prev_mtx);
15082255736Sdavidch        return (0);
15083255736Sdavidch    }
15084255736Sdavidch
15085255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15086255736Sdavidch
15087255736Sdavidch    /* Create an entry for this path and add it */
15088255736Sdavidch    tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15089255736Sdavidch                 (M_NOWAIT | M_ZERO));
15090255736Sdavidch    if (!tmp) {
15091255736Sdavidch        BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15092255736Sdavidch        return (-1);
15093255736Sdavidch    }
15094255736Sdavidch
15095255736Sdavidch    tmp->bus  = sc->pcie_bus;
15096255736Sdavidch    tmp->slot = sc->pcie_device;
15097255736Sdavidch    tmp->path = SC_PATH(sc);
15098255736Sdavidch    tmp->aer  = 0;
15099255736Sdavidch    tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15100255736Sdavidch
15101255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15102255736Sdavidch
15103255736Sdavidch    BLOGD(sc, DBG_LOAD,
15104255736Sdavidch          "Marked path %d/%d/%d - finished previous unload\n",
15105255736Sdavidch          sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15106255736Sdavidch    LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15107255736Sdavidch
15108255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15109255736Sdavidch
15110255736Sdavidch    return (0);
15111255736Sdavidch}
15112255736Sdavidch
15113255736Sdavidchstatic int
15114255736Sdavidchbxe_do_flr(struct bxe_softc *sc)
15115255736Sdavidch{
15116255736Sdavidch    int i;
15117255736Sdavidch
15118255736Sdavidch    /* only E2 and onwards support FLR */
15119255736Sdavidch    if (CHIP_IS_E1x(sc)) {
15120255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15121255736Sdavidch        return (-1);
15122255736Sdavidch    }
15123255736Sdavidch
15124255736Sdavidch    /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15125255736Sdavidch    if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15126255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15127255736Sdavidch              sc->devinfo.bc_ver);
15128255736Sdavidch        return (-1);
15129255736Sdavidch    }
15130255736Sdavidch
15131255736Sdavidch    /* Wait for Transaction Pending bit clean */
15132255736Sdavidch    for (i = 0; i < 4; i++) {
15133255736Sdavidch        if (i) {
15134255736Sdavidch            DELAY(((1 << (i - 1)) * 100) * 1000);
15135255736Sdavidch        }
15136255736Sdavidch
15137255736Sdavidch        if (!bxe_is_pcie_pending(sc)) {
15138255736Sdavidch            goto clear;
15139255736Sdavidch        }
15140255736Sdavidch    }
15141255736Sdavidch
15142255736Sdavidch    BLOGE(sc, "PCIE transaction is not cleared, "
15143255736Sdavidch              "proceeding with reset anyway\n");
15144255736Sdavidch
15145255736Sdavidchclear:
15146255736Sdavidch
15147255736Sdavidch    BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15148255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15149255736Sdavidch
15150255736Sdavidch    return (0);
15151255736Sdavidch}
15152255736Sdavidch
15153255736Sdavidchstruct bxe_mac_vals {
15154255736Sdavidch    uint32_t xmac_addr;
15155255736Sdavidch    uint32_t xmac_val;
15156255736Sdavidch    uint32_t emac_addr;
15157255736Sdavidch    uint32_t emac_val;
15158255736Sdavidch    uint32_t umac_addr;
15159255736Sdavidch    uint32_t umac_val;
15160255736Sdavidch    uint32_t bmac_addr;
15161255736Sdavidch    uint32_t bmac_val[2];
15162255736Sdavidch};
15163255736Sdavidch
15164255736Sdavidchstatic void
15165255736Sdavidchbxe_prev_unload_close_mac(struct bxe_softc *sc,
15166255736Sdavidch                          struct bxe_mac_vals *vals)
15167255736Sdavidch{
15168255736Sdavidch    uint32_t val, base_addr, offset, mask, reset_reg;
15169255736Sdavidch    uint8_t mac_stopped = FALSE;
15170255736Sdavidch    uint8_t port = SC_PORT(sc);
15171255736Sdavidch    uint32_t wb_data[2];
15172255736Sdavidch
15173255736Sdavidch    /* reset addresses as they also mark which values were changed */
15174255736Sdavidch    vals->bmac_addr = 0;
15175255736Sdavidch    vals->umac_addr = 0;
15176255736Sdavidch    vals->xmac_addr = 0;
15177255736Sdavidch    vals->emac_addr = 0;
15178255736Sdavidch
15179255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15180255736Sdavidch
15181255736Sdavidch    if (!CHIP_IS_E3(sc)) {
15182255736Sdavidch        val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15183255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15184255736Sdavidch        if ((mask & reset_reg) && val) {
15185255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15186255736Sdavidch            base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15187255736Sdavidch                                    : NIG_REG_INGRESS_BMAC0_MEM;
15188255736Sdavidch            offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15189255736Sdavidch                                    : BIGMAC_REGISTER_BMAC_CONTROL;
15190255736Sdavidch
15191255736Sdavidch            /*
15192255736Sdavidch             * use rd/wr since we cannot use dmae. This is safe
15193255736Sdavidch             * since MCP won't access the bus due to the request
15194255736Sdavidch             * to unload, and no function on the path can be
15195255736Sdavidch             * loaded at this time.
15196255736Sdavidch             */
15197255736Sdavidch            wb_data[0] = REG_RD(sc, base_addr + offset);
15198255736Sdavidch            wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15199255736Sdavidch            vals->bmac_addr = base_addr + offset;
15200255736Sdavidch            vals->bmac_val[0] = wb_data[0];
15201255736Sdavidch            vals->bmac_val[1] = wb_data[1];
15202255736Sdavidch            wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15203255736Sdavidch            REG_WR(sc, vals->bmac_addr, wb_data[0]);
15204255736Sdavidch            REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15205255736Sdavidch        }
15206255736Sdavidch
15207255736Sdavidch        BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15208255736Sdavidch        vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15209255736Sdavidch        vals->emac_val = REG_RD(sc, vals->emac_addr);
15210255736Sdavidch        REG_WR(sc, vals->emac_addr, 0);
15211255736Sdavidch        mac_stopped = TRUE;
15212255736Sdavidch    } else {
15213255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15214255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15215255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15216255736Sdavidch            val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15217255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15218255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15219255736Sdavidch            vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15220255736Sdavidch            vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15221255736Sdavidch            REG_WR(sc, vals->xmac_addr, 0);
15222255736Sdavidch            mac_stopped = TRUE;
15223255736Sdavidch        }
15224255736Sdavidch
15225255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15226255736Sdavidch        if (mask & reset_reg) {
15227255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15228255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15229255736Sdavidch            vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15230255736Sdavidch            vals->umac_val = REG_RD(sc, vals->umac_addr);
15231255736Sdavidch            REG_WR(sc, vals->umac_addr, 0);
15232255736Sdavidch            mac_stopped = TRUE;
15233255736Sdavidch        }
15234255736Sdavidch    }
15235255736Sdavidch
15236255736Sdavidch    if (mac_stopped) {
15237255736Sdavidch        DELAY(20000);
15238255736Sdavidch    }
15239255736Sdavidch}
15240255736Sdavidch
15241255736Sdavidch#define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15242255736Sdavidch#define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15243255736Sdavidch#define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15244255736Sdavidch#define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15245255736Sdavidch
15246255736Sdavidchstatic void
15247255736Sdavidchbxe_prev_unload_undi_inc(struct bxe_softc *sc,
15248255736Sdavidch                         uint8_t          port,
15249255736Sdavidch                         uint8_t          inc)
15250255736Sdavidch{
15251255736Sdavidch    uint16_t rcq, bd;
15252255736Sdavidch    uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15253255736Sdavidch
15254255736Sdavidch    rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15255255736Sdavidch    bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15256255736Sdavidch
15257255736Sdavidch    tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15258255736Sdavidch    REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15259255736Sdavidch
15260255736Sdavidch    BLOGD(sc, DBG_LOAD,
15261255736Sdavidch          "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15262255736Sdavidch          port, bd, rcq);
15263255736Sdavidch}
15264255736Sdavidch
15265255736Sdavidchstatic int
15266255736Sdavidchbxe_prev_unload_common(struct bxe_softc *sc)
15267255736Sdavidch{
15268255736Sdavidch    uint32_t reset_reg, tmp_reg = 0, rc;
15269255736Sdavidch    uint8_t prev_undi = FALSE;
15270255736Sdavidch    struct bxe_mac_vals mac_vals;
15271255736Sdavidch    uint32_t timer_count = 1000;
15272255736Sdavidch    uint32_t prev_brb;
15273255736Sdavidch
15274255736Sdavidch    /*
15275255736Sdavidch     * It is possible a previous function received 'common' answer,
15276255736Sdavidch     * but hasn't loaded yet, therefore creating a scenario of
15277255736Sdavidch     * multiple functions receiving 'common' on the same path.
15278255736Sdavidch     */
15279255736Sdavidch    BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15280255736Sdavidch
15281255736Sdavidch    memset(&mac_vals, 0, sizeof(mac_vals));
15282255736Sdavidch
15283255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15284255736Sdavidch        return (bxe_prev_mcp_done(sc));
15285255736Sdavidch    }
15286255736Sdavidch
15287255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15288255736Sdavidch
15289255736Sdavidch    /* Reset should be performed after BRB is emptied */
15290255736Sdavidch    if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15291255736Sdavidch        /* Close the MAC Rx to prevent BRB from filling up */
15292255736Sdavidch        bxe_prev_unload_close_mac(sc, &mac_vals);
15293255736Sdavidch
15294255736Sdavidch        /* close LLH filters towards the BRB */
15295255736Sdavidch        elink_set_rx_filter(&sc->link_params, 0);
15296255736Sdavidch
15297255736Sdavidch        /*
15298255736Sdavidch         * Check if the UNDI driver was previously loaded.
15299255736Sdavidch         * UNDI driver initializes CID offset for normal bell to 0x7
15300255736Sdavidch         */
15301255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15302255736Sdavidch            tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15303255736Sdavidch            if (tmp_reg == 0x7) {
15304255736Sdavidch                BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15305255736Sdavidch                prev_undi = TRUE;
15306255736Sdavidch                /* clear the UNDI indication */
15307255736Sdavidch                REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15308255736Sdavidch                /* clear possible idle check errors */
15309255736Sdavidch                REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15310255736Sdavidch            }
15311255736Sdavidch        }
15312255736Sdavidch
15313255736Sdavidch        /* wait until BRB is empty */
15314255736Sdavidch        tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15315255736Sdavidch        while (timer_count) {
15316255736Sdavidch            prev_brb = tmp_reg;
15317255736Sdavidch
15318255736Sdavidch            tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15319255736Sdavidch            if (!tmp_reg) {
15320255736Sdavidch                break;
15321255736Sdavidch            }
15322255736Sdavidch
15323255736Sdavidch            BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15324255736Sdavidch
15325255736Sdavidch            /* reset timer as long as BRB actually gets emptied */
15326255736Sdavidch            if (prev_brb > tmp_reg) {
15327255736Sdavidch                timer_count = 1000;
15328255736Sdavidch            } else {
15329255736Sdavidch                timer_count--;
15330255736Sdavidch            }
15331255736Sdavidch
15332255736Sdavidch            /* If UNDI resides in memory, manually increment it */
15333255736Sdavidch            if (prev_undi) {
15334255736Sdavidch                bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15335255736Sdavidch            }
15336255736Sdavidch
15337255736Sdavidch            DELAY(10);
15338255736Sdavidch        }
15339255736Sdavidch
15340255736Sdavidch        if (!timer_count) {
15341255736Sdavidch            BLOGE(sc, "Failed to empty BRB\n");
15342255736Sdavidch        }
15343255736Sdavidch    }
15344255736Sdavidch
15345255736Sdavidch    /* No packets are in the pipeline, path is ready for reset */
15346255736Sdavidch    bxe_reset_common(sc);
15347255736Sdavidch
15348255736Sdavidch    if (mac_vals.xmac_addr) {
15349255736Sdavidch        REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15350255736Sdavidch    }
15351255736Sdavidch    if (mac_vals.umac_addr) {
15352255736Sdavidch        REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15353255736Sdavidch    }
15354255736Sdavidch    if (mac_vals.emac_addr) {
15355255736Sdavidch        REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15356255736Sdavidch    }
15357255736Sdavidch    if (mac_vals.bmac_addr) {
15358255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15359255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15360255736Sdavidch    }
15361255736Sdavidch
15362255736Sdavidch    rc = bxe_prev_mark_path(sc, prev_undi);
15363255736Sdavidch    if (rc) {
15364255736Sdavidch        bxe_prev_mcp_done(sc);
15365255736Sdavidch        return (rc);
15366255736Sdavidch    }
15367255736Sdavidch
15368255736Sdavidch    return (bxe_prev_mcp_done(sc));
15369255736Sdavidch}
15370255736Sdavidch
15371255736Sdavidchstatic int
15372255736Sdavidchbxe_prev_unload_uncommon(struct bxe_softc *sc)
15373255736Sdavidch{
15374255736Sdavidch    int rc;
15375255736Sdavidch
15376255736Sdavidch    BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15377255736Sdavidch
15378255736Sdavidch    /* Test if previous unload process was already finished for this path */
15379255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15380255736Sdavidch        return (bxe_prev_mcp_done(sc));
15381255736Sdavidch    }
15382255736Sdavidch
15383255736Sdavidch    BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15384255736Sdavidch
15385255736Sdavidch    /*
15386255736Sdavidch     * If function has FLR capabilities, and existing FW version matches
15387255736Sdavidch     * the one required, then FLR will be sufficient to clean any residue
15388255736Sdavidch     * left by previous driver
15389255736Sdavidch     */
15390255736Sdavidch    rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15391255736Sdavidch    if (!rc) {
15392255736Sdavidch        /* fw version is good */
15393255736Sdavidch        BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15394255736Sdavidch        rc = bxe_do_flr(sc);
15395255736Sdavidch    }
15396255736Sdavidch
15397255736Sdavidch    if (!rc) {
15398255736Sdavidch        /* FLR was performed */
15399255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR successful\n");
15400255736Sdavidch        return (0);
15401255736Sdavidch    }
15402255736Sdavidch
15403255736Sdavidch    BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15404255736Sdavidch
15405255736Sdavidch    /* Close the MCP request, return failure*/
15406255736Sdavidch    rc = bxe_prev_mcp_done(sc);
15407255736Sdavidch    if (!rc) {
15408255736Sdavidch        rc = BXE_PREV_WAIT_NEEDED;
15409255736Sdavidch    }
15410255736Sdavidch
15411255736Sdavidch    return (rc);
15412255736Sdavidch}
15413255736Sdavidch
15414255736Sdavidchstatic int
15415255736Sdavidchbxe_prev_unload(struct bxe_softc *sc)
15416255736Sdavidch{
15417255736Sdavidch    int time_counter = 10;
15418255736Sdavidch    uint32_t fw, hw_lock_reg, hw_lock_val;
15419255736Sdavidch    uint32_t rc = 0;
15420255736Sdavidch
15421255736Sdavidch    /*
15422255736Sdavidch     * Clear HW from errors which may have resulted from an interrupted
15423255736Sdavidch     * DMAE transaction.
15424255736Sdavidch     */
15425255736Sdavidch    bxe_prev_interrupted_dmae(sc);
15426255736Sdavidch
15427255736Sdavidch    /* Release previously held locks */
15428255736Sdavidch    hw_lock_reg =
15429255736Sdavidch        (SC_FUNC(sc) <= 5) ?
15430255736Sdavidch            (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15431255736Sdavidch            (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15432255736Sdavidch
15433255736Sdavidch    hw_lock_val = (REG_RD(sc, hw_lock_reg));
15434255736Sdavidch    if (hw_lock_val) {
15435255736Sdavidch        if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15436255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15437255736Sdavidch            REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15438255736Sdavidch                   (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15439255736Sdavidch        }
15440255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15441255736Sdavidch        REG_WR(sc, hw_lock_reg, 0xffffffff);
15442255736Sdavidch    } else {
15443255736Sdavidch        BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15444255736Sdavidch    }
15445255736Sdavidch
15446255736Sdavidch    if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15447255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15448255736Sdavidch        REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15449255736Sdavidch    }
15450255736Sdavidch
15451255736Sdavidch    do {
15452255736Sdavidch        /* Lock MCP using an unload request */
15453255736Sdavidch        fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15454255736Sdavidch        if (!fw) {
15455255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
15456255736Sdavidch            rc = -1;
15457255736Sdavidch            break;
15458255736Sdavidch        }
15459255736Sdavidch
15460255736Sdavidch        if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15461255736Sdavidch            rc = bxe_prev_unload_common(sc);
15462255736Sdavidch            break;
15463255736Sdavidch        }
15464255736Sdavidch
15465255736Sdavidch        /* non-common reply from MCP night require looping */
15466255736Sdavidch        rc = bxe_prev_unload_uncommon(sc);
15467255736Sdavidch        if (rc != BXE_PREV_WAIT_NEEDED) {
15468255736Sdavidch            break;
15469255736Sdavidch        }
15470255736Sdavidch
15471255736Sdavidch        DELAY(20000);
15472255736Sdavidch    } while (--time_counter);
15473255736Sdavidch
15474255736Sdavidch    if (!time_counter || rc) {
15475295830Sdavidcs        BLOGE(sc, "Failed to unload previous driver!"
15476295830Sdavidcs            " time_counter %d rc %d\n", time_counter, rc);
15477255736Sdavidch        rc = -1;
15478255736Sdavidch    }
15479255736Sdavidch
15480255736Sdavidch    return (rc);
15481255736Sdavidch}
15482255736Sdavidch
15483255736Sdavidchvoid
15484255736Sdavidchbxe_dcbx_set_state(struct bxe_softc *sc,
15485255736Sdavidch                   uint8_t          dcb_on,
15486255736Sdavidch                   uint32_t         dcbx_enabled)
15487255736Sdavidch{
15488255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
15489255736Sdavidch        sc->dcb_state = dcb_on;
15490255736Sdavidch        sc->dcbx_enabled = dcbx_enabled;
15491255736Sdavidch    } else {
15492255736Sdavidch        sc->dcb_state = FALSE;
15493255736Sdavidch        sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15494255736Sdavidch    }
15495255736Sdavidch    BLOGD(sc, DBG_LOAD,
15496255736Sdavidch          "DCB state [%s:%s]\n",
15497255736Sdavidch          dcb_on ? "ON" : "OFF",
15498255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15499255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15500255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15501255736Sdavidch          "on-chip with negotiation" : "invalid");
15502255736Sdavidch}
15503255736Sdavidch
15504255736Sdavidch/* must be called after sriov-enable */
15505255736Sdavidchstatic int
15506255736Sdavidchbxe_set_qm_cid_count(struct bxe_softc *sc)
15507255736Sdavidch{
15508255736Sdavidch    int cid_count = BXE_L2_MAX_CID(sc);
15509255736Sdavidch
15510255736Sdavidch    if (IS_SRIOV(sc)) {
15511255736Sdavidch        cid_count += BXE_VF_CIDS;
15512255736Sdavidch    }
15513255736Sdavidch
15514255736Sdavidch    if (CNIC_SUPPORT(sc)) {
15515255736Sdavidch        cid_count += CNIC_CID_MAX;
15516255736Sdavidch    }
15517255736Sdavidch
15518255736Sdavidch    return (roundup(cid_count, QM_CID_ROUND));
15519255736Sdavidch}
15520255736Sdavidch
15521255736Sdavidchstatic void
15522255736Sdavidchbxe_init_multi_cos(struct bxe_softc *sc)
15523255736Sdavidch{
15524255736Sdavidch    int pri, cos;
15525255736Sdavidch
15526255736Sdavidch    uint32_t pri_map = 0; /* XXX change to user config */
15527255736Sdavidch
15528255736Sdavidch    for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15529255736Sdavidch        cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15530255736Sdavidch        if (cos < sc->max_cos) {
15531255736Sdavidch            sc->prio_to_cos[pri] = cos;
15532255736Sdavidch        } else {
15533255736Sdavidch            BLOGW(sc, "Invalid COS %d for priority %d "
15534255736Sdavidch                      "(max COS is %d), setting to 0\n",
15535255736Sdavidch                  cos, pri, (sc->max_cos - 1));
15536255736Sdavidch            sc->prio_to_cos[pri] = 0;
15537255736Sdavidch        }
15538255736Sdavidch    }
15539255736Sdavidch}
15540255736Sdavidch
15541255736Sdavidchstatic int
15542255736Sdavidchbxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15543255736Sdavidch{
15544255736Sdavidch    struct bxe_softc *sc;
15545255736Sdavidch    int error, result;
15546255736Sdavidch
15547255736Sdavidch    result = 0;
15548255736Sdavidch    error = sysctl_handle_int(oidp, &result, 0, req);
15549255736Sdavidch
15550255736Sdavidch    if (error || !req->newptr) {
15551255736Sdavidch        return (error);
15552255736Sdavidch    }
15553255736Sdavidch
15554255736Sdavidch    if (result == 1) {
15555292638Sdavidcs        uint32_t  temp;
15556255736Sdavidch        sc = (struct bxe_softc *)arg1;
15557289199Sdavidcs
15558255736Sdavidch        BLOGI(sc, "... dumping driver state ...\n");
15559292638Sdavidcs        temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15560292638Sdavidcs        BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15561255736Sdavidch    }
15562255736Sdavidch
15563255736Sdavidch    return (error);
15564255736Sdavidch}
15565255736Sdavidch
15566255736Sdavidchstatic int
15567255736Sdavidchbxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15568255736Sdavidch{
15569255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15570255736Sdavidch    uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15571255736Sdavidch    uint32_t *offset;
15572255736Sdavidch    uint64_t value = 0;
15573255736Sdavidch    int index = (int)arg2;
15574255736Sdavidch
15575255736Sdavidch    if (index >= BXE_NUM_ETH_STATS) {
15576255736Sdavidch        BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15577255736Sdavidch        return (-1);
15578255736Sdavidch    }
15579255736Sdavidch
15580255736Sdavidch    offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15581255736Sdavidch
15582255736Sdavidch    switch (bxe_eth_stats_arr[index].size) {
15583255736Sdavidch    case 4:
15584255736Sdavidch        value = (uint64_t)*offset;
15585255736Sdavidch        break;
15586255736Sdavidch    case 8:
15587255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15588255736Sdavidch        break;
15589255736Sdavidch    default:
15590255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15591255736Sdavidch              index, bxe_eth_stats_arr[index].size);
15592255736Sdavidch        return (-1);
15593255736Sdavidch    }
15594255736Sdavidch
15595255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15596255736Sdavidch}
15597255736Sdavidch
15598255736Sdavidchstatic int
15599255736Sdavidchbxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15600255736Sdavidch{
15601255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15602255736Sdavidch    uint32_t *eth_stats;
15603255736Sdavidch    uint32_t *offset;
15604255736Sdavidch    uint64_t value = 0;
15605255736Sdavidch    uint32_t q_stat = (uint32_t)arg2;
15606255736Sdavidch    uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15607255736Sdavidch    uint32_t index = (q_stat & 0xffff);
15608255736Sdavidch
15609255736Sdavidch    eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15610255736Sdavidch
15611255736Sdavidch    if (index >= BXE_NUM_ETH_Q_STATS) {
15612255736Sdavidch        BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15613255736Sdavidch        return (-1);
15614255736Sdavidch    }
15615255736Sdavidch
15616255736Sdavidch    offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15617255736Sdavidch
15618255736Sdavidch    switch (bxe_eth_q_stats_arr[index].size) {
15619255736Sdavidch    case 4:
15620255736Sdavidch        value = (uint64_t)*offset;
15621255736Sdavidch        break;
15622255736Sdavidch    case 8:
15623255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15624255736Sdavidch        break;
15625255736Sdavidch    default:
15626255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15627255736Sdavidch              index, bxe_eth_q_stats_arr[index].size);
15628255736Sdavidch        return (-1);
15629255736Sdavidch    }
15630255736Sdavidch
15631255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15632255736Sdavidch}
15633255736Sdavidch
15634315881Sdavidcsstatic void bxe_force_link_reset(struct bxe_softc *sc)
15635315881Sdavidcs{
15636315881Sdavidcs
15637315881Sdavidcs        bxe_acquire_phy_lock(sc);
15638315881Sdavidcs        elink_link_reset(&sc->link_params, &sc->link_vars, 1);
15639315881Sdavidcs        bxe_release_phy_lock(sc);
15640315881Sdavidcs}
15641315881Sdavidcs
15642315881Sdavidcsstatic int
15643315881Sdavidcsbxe_sysctl_pauseparam(SYSCTL_HANDLER_ARGS)
15644315881Sdavidcs{
15645315881Sdavidcs        struct bxe_softc *sc = (struct bxe_softc *)arg1;;
15646315881Sdavidcs        uint32_t cfg_idx = bxe_get_link_cfg_idx(sc);
15647315881Sdavidcs        int rc = 0;
15648315881Sdavidcs        int error;
15649315881Sdavidcs        int result;
15650315881Sdavidcs
15651315881Sdavidcs
15652315881Sdavidcs        error = sysctl_handle_int(oidp, &sc->bxe_pause_param, 0, req);
15653315881Sdavidcs
15654315881Sdavidcs        if (error || !req->newptr) {
15655315881Sdavidcs                return (error);
15656315881Sdavidcs        }
15657315881Sdavidcs        if ((sc->bxe_pause_param < 0) ||  (sc->bxe_pause_param > 8)) {
15658315881Sdavidcs                BLOGW(sc, "invalid pause param (%d) - use intergers between 1 & 8\n",sc->bxe_pause_param);
15659315881Sdavidcs                sc->bxe_pause_param = 8;
15660315881Sdavidcs        }
15661315881Sdavidcs
15662315881Sdavidcs        result = (sc->bxe_pause_param << PORT_FEATURE_FLOW_CONTROL_SHIFT);
15663315881Sdavidcs
15664315881Sdavidcs
15665315881Sdavidcs        if((result & 0x400) && !(sc->port.supported[cfg_idx] & ELINK_SUPPORTED_Autoneg))  {
15666315881Sdavidcs                        BLOGW(sc, "Does not support Autoneg pause_param %d\n", sc->bxe_pause_param);
15667315881Sdavidcs                        return -EINVAL;
15668315881Sdavidcs        }
15669315881Sdavidcs
15670315881Sdavidcs        if(IS_MF(sc))
15671315881Sdavidcs                return 0;
15672315881Sdavidcs       sc->link_params.req_flow_ctrl[cfg_idx] = ELINK_FLOW_CTRL_AUTO;
15673315881Sdavidcs        if(result & ELINK_FLOW_CTRL_RX)
15674315881Sdavidcs                sc->link_params.req_flow_ctrl[cfg_idx] |= ELINK_FLOW_CTRL_RX;
15675315881Sdavidcs
15676315881Sdavidcs        if(result & ELINK_FLOW_CTRL_TX)
15677315881Sdavidcs                sc->link_params.req_flow_ctrl[cfg_idx] |= ELINK_FLOW_CTRL_TX;
15678315881Sdavidcs        if(sc->link_params.req_flow_ctrl[cfg_idx] == ELINK_FLOW_CTRL_AUTO)
15679315881Sdavidcs                sc->link_params.req_flow_ctrl[cfg_idx] = ELINK_FLOW_CTRL_NONE;
15680315881Sdavidcs
15681315881Sdavidcs        if(result & 0x400) {
15682315881Sdavidcs                if (sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG) {
15683315881Sdavidcs                        sc->link_params.req_flow_ctrl[cfg_idx] =
15684315881Sdavidcs                                ELINK_FLOW_CTRL_AUTO;
15685315881Sdavidcs                }
15686315881Sdavidcs                sc->link_params.req_fc_auto_adv = 0;
15687315881Sdavidcs                if (result & ELINK_FLOW_CTRL_RX)
15688315881Sdavidcs                        sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_RX;
15689315881Sdavidcs
15690315881Sdavidcs                if (result & ELINK_FLOW_CTRL_TX)
15691315881Sdavidcs                        sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_TX;
15692315881Sdavidcs                if (!sc->link_params.req_fc_auto_adv)
15693315881Sdavidcs                        sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_NONE;
15694315881Sdavidcs        }
15695315881Sdavidcs         if (IS_PF(sc)) {
15696315881Sdavidcs                        if (sc->link_vars.link_up) {
15697315881Sdavidcs                                bxe_stats_handle(sc, STATS_EVENT_STOP);
15698315881Sdavidcs                        }
15699315881Sdavidcs			if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
15700315881Sdavidcs                        bxe_force_link_reset(sc);
15701315881Sdavidcs                        bxe_acquire_phy_lock(sc);
15702315881Sdavidcs
15703315881Sdavidcs                        rc = elink_phy_init(&sc->link_params, &sc->link_vars);
15704315881Sdavidcs
15705315881Sdavidcs                        bxe_release_phy_lock(sc);
15706315881Sdavidcs
15707315881Sdavidcs                        bxe_calc_fc_adv(sc);
15708315881Sdavidcs                        }
15709315881Sdavidcs        }
15710315881Sdavidcs        return rc;
15711315881Sdavidcs}
15712315881Sdavidcs
15713315881Sdavidcs
15714255736Sdavidchstatic void
15715255736Sdavidchbxe_add_sysctls(struct bxe_softc *sc)
15716255736Sdavidch{
15717255736Sdavidch    struct sysctl_ctx_list *ctx;
15718255736Sdavidch    struct sysctl_oid_list *children;
15719255736Sdavidch    struct sysctl_oid *queue_top, *queue;
15720255736Sdavidch    struct sysctl_oid_list *queue_top_children, *queue_children;
15721255736Sdavidch    char queue_num_buf[32];
15722255736Sdavidch    uint32_t q_stat;
15723255736Sdavidch    int i, j;
15724255736Sdavidch
15725255736Sdavidch    ctx = device_get_sysctl_ctx(sc->dev);
15726255736Sdavidch    children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15727255736Sdavidch
15728255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
15729255736Sdavidch                      CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
15730255736Sdavidch                      "version");
15731255736Sdavidch
15732255736Sdavidch    snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
15733255736Sdavidch             BCM_5710_FW_MAJOR_VERSION,
15734255736Sdavidch             BCM_5710_FW_MINOR_VERSION,
15735255736Sdavidch             BCM_5710_FW_REVISION_VERSION,
15736255736Sdavidch             BCM_5710_FW_ENGINEERING_VERSION);
15737255736Sdavidch
15738255736Sdavidch    snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
15739255736Sdavidch        ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
15740255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
15741255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
15742255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
15743255736Sdavidch                                                                "Unknown"));
15744255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
15745255736Sdavidch                    CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
15746255736Sdavidch                    "multifunction vnics per port");
15747255736Sdavidch
15748255736Sdavidch    snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
15749255736Sdavidch        ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
15750255736Sdavidch         (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
15751255736Sdavidch         (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
15752255736Sdavidch                                              "???GT/s"),
15753255736Sdavidch        sc->devinfo.pcie_link_width);
15754297155Sdavidcs
15755297155Sdavidcs    sc->debug = bxe_debug;
15756297155Sdavidcs
15757297155Sdavidcs#if __FreeBSD_version >= 900000
15758297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15759297155Sdavidcs                      CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
15760297155Sdavidcs                      "bootcode version");
15761297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15762297155Sdavidcs                      CTLFLAG_RD, sc->fw_ver_str, 0,
15763297155Sdavidcs                      "firmware version");
15764297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15765297155Sdavidcs                      CTLFLAG_RD, sc->mf_mode_str, 0,
15766297155Sdavidcs                      "multifunction mode");
15767297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15768297155Sdavidcs                      CTLFLAG_RD, sc->mac_addr_str, 0,
15769297155Sdavidcs                      "mac address");
15770255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15771273377Shselasky                      CTLFLAG_RD, sc->pci_link_str, 0,
15772255736Sdavidch                      "pci link status");
15773273377Shselasky    SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
15774273377Shselasky                    CTLFLAG_RW, &sc->debug,
15775255736Sdavidch                    "debug logging mode");
15776297155Sdavidcs#else
15777297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15778297155Sdavidcs                      CTLFLAG_RD, &sc->devinfo.bc_ver_str, 0,
15779297155Sdavidcs                      "bootcode version");
15780297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15781297155Sdavidcs                      CTLFLAG_RD, &sc->fw_ver_str, 0,
15782297155Sdavidcs                      "firmware version");
15783297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15784297155Sdavidcs                      CTLFLAG_RD, &sc->mf_mode_str, 0,
15785297155Sdavidcs                      "multifunction mode");
15786297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15787297155Sdavidcs                      CTLFLAG_RD, &sc->mac_addr_str, 0,
15788297155Sdavidcs                      "mac address");
15789297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15790297155Sdavidcs                      CTLFLAG_RD, &sc->pci_link_str, 0,
15791297155Sdavidcs                      "pci link status");
15792297155Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "debug",
15793297155Sdavidcs                    CTLFLAG_RW, &sc->debug, 0,
15794297155Sdavidcs                    "debug logging mode");
15795297155Sdavidcs#endif /* #if __FreeBSD_version >= 900000 */
15796255736Sdavidch
15797298294Sdavidcs    sc->trigger_grcdump = 0;
15798298294Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "trigger_grcdump",
15799298294Sdavidcs                   CTLFLAG_RW, &sc->trigger_grcdump, 0,
15800298294Sdavidcs                   "trigger grcdump should be invoked"
15801298294Sdavidcs                   "  before collecting grcdump");
15802292639Sdavidcs
15803298294Sdavidcs    sc->grcdump_started = 0;
15804295823Sdavidcs    sc->grcdump_done = 0;
15805295823Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
15806298294Sdavidcs                   CTLFLAG_RD, &sc->grcdump_done, 0,
15807295823Sdavidcs                   "set by driver when grcdump is done");
15808292639Sdavidcs
15809255736Sdavidch    sc->rx_budget = bxe_rx_budget;
15810255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
15811255736Sdavidch                    CTLFLAG_RW, &sc->rx_budget, 0,
15812255736Sdavidch                    "rx processing budget");
15813255736Sdavidch
15814315881Sdavidcs   SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_param",
15815315881Sdavidcs                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15816315881Sdavidcs                    bxe_sysctl_pauseparam, "IU",
15817315881Sdavidcs                    "need pause frames- DEF:0/TX:1/RX:2/BOTH:3/AUTO:4/AUTOTX:5/AUTORX:6/AUTORXTX:7/NONE:8");
15818315881Sdavidcs
15819315881Sdavidcs
15820255736Sdavidch    SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
15821255736Sdavidch                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15822255736Sdavidch                    bxe_sysctl_state, "IU", "dump driver state");
15823255736Sdavidch
15824255736Sdavidch    for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
15825255736Sdavidch        SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
15826255736Sdavidch                        bxe_eth_stats_arr[i].string,
15827255736Sdavidch                        CTLTYPE_U64 | CTLFLAG_RD, sc, i,
15828255736Sdavidch                        bxe_sysctl_eth_stat, "LU",
15829255736Sdavidch                        bxe_eth_stats_arr[i].string);
15830255736Sdavidch    }
15831255736Sdavidch
15832255736Sdavidch    /* add a new parent node for all queues "dev.bxe.#.queue" */
15833255736Sdavidch    queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
15834255736Sdavidch                                CTLFLAG_RD, NULL, "queue");
15835255736Sdavidch    queue_top_children = SYSCTL_CHILDREN(queue_top);
15836255736Sdavidch
15837255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
15838255736Sdavidch        /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
15839255736Sdavidch        snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
15840255736Sdavidch        queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
15841255736Sdavidch                                queue_num_buf, CTLFLAG_RD, NULL,
15842255736Sdavidch                                "single queue");
15843255736Sdavidch        queue_children = SYSCTL_CHILDREN(queue);
15844255736Sdavidch
15845255736Sdavidch        for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
15846255736Sdavidch            q_stat = ((i << 16) | j);
15847255736Sdavidch            SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
15848255736Sdavidch                            bxe_eth_q_stats_arr[j].string,
15849255736Sdavidch                            CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
15850255736Sdavidch                            bxe_sysctl_eth_q_stat, "LU",
15851255736Sdavidch                            bxe_eth_q_stats_arr[j].string);
15852255736Sdavidch        }
15853255736Sdavidch    }
15854255736Sdavidch}
15855255736Sdavidch
15856296579Sdavidcsstatic int
15857296579Sdavidcsbxe_alloc_buf_rings(struct bxe_softc *sc)
15858296579Sdavidcs{
15859307972Sdavidcs#if __FreeBSD_version >= 901504
15860296579Sdavidcs
15861296579Sdavidcs    int i;
15862296579Sdavidcs    struct bxe_fastpath *fp;
15863296579Sdavidcs
15864296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15865296579Sdavidcs
15866296579Sdavidcs        fp = &sc->fp[i];
15867296579Sdavidcs
15868296579Sdavidcs        fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
15869296579Sdavidcs                                   M_NOWAIT, &fp->tx_mtx);
15870296579Sdavidcs        if (fp->tx_br == NULL)
15871296579Sdavidcs            return (-1);
15872296579Sdavidcs    }
15873296579Sdavidcs#endif
15874296579Sdavidcs    return (0);
15875296579Sdavidcs}
15876296579Sdavidcs
15877296579Sdavidcsstatic void
15878296579Sdavidcsbxe_free_buf_rings(struct bxe_softc *sc)
15879296579Sdavidcs{
15880307972Sdavidcs#if __FreeBSD_version >= 901504
15881296579Sdavidcs
15882296579Sdavidcs    int i;
15883296579Sdavidcs    struct bxe_fastpath *fp;
15884296579Sdavidcs
15885296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15886296579Sdavidcs
15887296579Sdavidcs        fp = &sc->fp[i];
15888296579Sdavidcs
15889296579Sdavidcs        if (fp->tx_br) {
15890296579Sdavidcs            buf_ring_free(fp->tx_br, M_DEVBUF);
15891296579Sdavidcs            fp->tx_br = NULL;
15892296579Sdavidcs        }
15893296579Sdavidcs    }
15894296579Sdavidcs
15895296579Sdavidcs#endif
15896296579Sdavidcs}
15897296579Sdavidcs
15898296579Sdavidcsstatic void
15899296579Sdavidcsbxe_init_fp_mutexs(struct bxe_softc *sc)
15900296579Sdavidcs{
15901296579Sdavidcs    int i;
15902296579Sdavidcs    struct bxe_fastpath *fp;
15903296579Sdavidcs
15904296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15905296579Sdavidcs
15906296579Sdavidcs        fp = &sc->fp[i];
15907296579Sdavidcs
15908296579Sdavidcs        snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
15909296579Sdavidcs            "bxe%d_fp%d_tx_lock", sc->unit, i);
15910296579Sdavidcs        mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
15911296579Sdavidcs
15912296579Sdavidcs        snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
15913296579Sdavidcs            "bxe%d_fp%d_rx_lock", sc->unit, i);
15914296579Sdavidcs        mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
15915296579Sdavidcs    }
15916296579Sdavidcs}
15917296579Sdavidcs
15918296579Sdavidcsstatic void
15919296579Sdavidcsbxe_destroy_fp_mutexs(struct bxe_softc *sc)
15920296579Sdavidcs{
15921296579Sdavidcs    int i;
15922296579Sdavidcs    struct bxe_fastpath *fp;
15923296579Sdavidcs
15924296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15925296579Sdavidcs
15926296579Sdavidcs        fp = &sc->fp[i];
15927296579Sdavidcs
15928296579Sdavidcs        if (mtx_initialized(&fp->tx_mtx)) {
15929296579Sdavidcs            mtx_destroy(&fp->tx_mtx);
15930296579Sdavidcs        }
15931296579Sdavidcs
15932296579Sdavidcs        if (mtx_initialized(&fp->rx_mtx)) {
15933296579Sdavidcs            mtx_destroy(&fp->rx_mtx);
15934296579Sdavidcs        }
15935296579Sdavidcs    }
15936296579Sdavidcs}
15937296579Sdavidcs
15938296579Sdavidcs
15939255736Sdavidch/*
15940255736Sdavidch * Device attach function.
15941255736Sdavidch *
15942255736Sdavidch * Allocates device resources, performs secondary chip identification, and
15943255736Sdavidch * initializes driver instance variables. This function is called from driver
15944255736Sdavidch * load after a successful probe.
15945255736Sdavidch *
15946255736Sdavidch * Returns:
15947255736Sdavidch *   0 = Success, >0 = Failure
15948255736Sdavidch */
15949255736Sdavidchstatic int
15950255736Sdavidchbxe_attach(device_t dev)
15951255736Sdavidch{
15952255736Sdavidch    struct bxe_softc *sc;
15953255736Sdavidch
15954255736Sdavidch    sc = device_get_softc(dev);
15955255736Sdavidch
15956255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting attach...\n");
15957255736Sdavidch
15958255736Sdavidch    sc->state = BXE_STATE_CLOSED;
15959255736Sdavidch
15960255736Sdavidch    sc->dev  = dev;
15961255736Sdavidch    sc->unit = device_get_unit(dev);
15962255736Sdavidch
15963255736Sdavidch    BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
15964255736Sdavidch
15965255736Sdavidch    sc->pcie_bus    = pci_get_bus(dev);
15966255736Sdavidch    sc->pcie_device = pci_get_slot(dev);
15967255736Sdavidch    sc->pcie_func   = pci_get_function(dev);
15968255736Sdavidch
15969255736Sdavidch    /* enable bus master capability */
15970255736Sdavidch    pci_enable_busmaster(dev);
15971255736Sdavidch
15972255736Sdavidch    /* get the BARs */
15973255736Sdavidch    if (bxe_allocate_bars(sc) != 0) {
15974255736Sdavidch        return (ENXIO);
15975255736Sdavidch    }
15976255736Sdavidch
15977255736Sdavidch    /* initialize the mutexes */
15978255736Sdavidch    bxe_init_mutexes(sc);
15979255736Sdavidch
15980255736Sdavidch    /* prepare the periodic callout */
15981255736Sdavidch    callout_init(&sc->periodic_callout, 0);
15982255736Sdavidch
15983255736Sdavidch    /* prepare the chip taskqueue */
15984255736Sdavidch    sc->chip_tq_flags = CHIP_TQ_NONE;
15985255736Sdavidch    snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
15986255736Sdavidch             "bxe%d_chip_tq", sc->unit);
15987255736Sdavidch    TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
15988255736Sdavidch    sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
15989255736Sdavidch                                   taskqueue_thread_enqueue,
15990255736Sdavidch                                   &sc->chip_tq);
15991255736Sdavidch    taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
15992255736Sdavidch                            "%s", sc->chip_tq_name);
15993255736Sdavidch
15994255736Sdavidch    /* get device info and set params */
15995255736Sdavidch    if (bxe_get_device_info(sc) != 0) {
15996255736Sdavidch        BLOGE(sc, "getting device info\n");
15997255736Sdavidch        bxe_deallocate_bars(sc);
15998255736Sdavidch        pci_disable_busmaster(dev);
15999255736Sdavidch        return (ENXIO);
16000255736Sdavidch    }
16001255736Sdavidch
16002255736Sdavidch    /* get final misc params */
16003255736Sdavidch    bxe_get_params(sc);
16004255736Sdavidch
16005255736Sdavidch    /* set the default MTU (changed via ifconfig) */
16006255736Sdavidch    sc->mtu = ETHERMTU;
16007255736Sdavidch
16008255736Sdavidch    bxe_set_modes_bitmap(sc);
16009255736Sdavidch
16010255736Sdavidch    /* XXX
16011255736Sdavidch     * If in AFEX mode and the function is configured for FCoE
16012255736Sdavidch     * then bail... no L2 allowed.
16013255736Sdavidch     */
16014255736Sdavidch
16015255736Sdavidch    /* get phy settings from shmem and 'and' against admin settings */
16016255736Sdavidch    bxe_get_phy_info(sc);
16017255736Sdavidch
16018255736Sdavidch    /* initialize the FreeBSD ifnet interface */
16019255736Sdavidch    if (bxe_init_ifnet(sc) != 0) {
16020255736Sdavidch        bxe_release_mutexes(sc);
16021255736Sdavidch        bxe_deallocate_bars(sc);
16022255736Sdavidch        pci_disable_busmaster(dev);
16023255736Sdavidch        return (ENXIO);
16024255736Sdavidch    }
16025255736Sdavidch
16026292639Sdavidcs    if (bxe_add_cdev(sc) != 0) {
16027292639Sdavidcs        if (sc->ifp != NULL) {
16028292639Sdavidcs            ether_ifdetach(sc->ifp);
16029292639Sdavidcs        }
16030292639Sdavidcs        ifmedia_removeall(&sc->ifmedia);
16031292639Sdavidcs        bxe_release_mutexes(sc);
16032292639Sdavidcs        bxe_deallocate_bars(sc);
16033292639Sdavidcs        pci_disable_busmaster(dev);
16034292639Sdavidcs        return (ENXIO);
16035292639Sdavidcs    }
16036292639Sdavidcs
16037255736Sdavidch    /* allocate device interrupts */
16038255736Sdavidch    if (bxe_interrupt_alloc(sc) != 0) {
16039292639Sdavidcs        bxe_del_cdev(sc);
16040266979Smarcel        if (sc->ifp != NULL) {
16041270876Sglebius            ether_ifdetach(sc->ifp);
16042255736Sdavidch        }
16043255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16044255736Sdavidch        bxe_release_mutexes(sc);
16045255736Sdavidch        bxe_deallocate_bars(sc);
16046255736Sdavidch        pci_disable_busmaster(dev);
16047255736Sdavidch        return (ENXIO);
16048255736Sdavidch    }
16049255736Sdavidch
16050296579Sdavidcs    bxe_init_fp_mutexs(sc);
16051296579Sdavidcs
16052296579Sdavidcs    if (bxe_alloc_buf_rings(sc) != 0) {
16053296579Sdavidcs	bxe_free_buf_rings(sc);
16054296579Sdavidcs        bxe_interrupt_free(sc);
16055296579Sdavidcs        bxe_del_cdev(sc);
16056296579Sdavidcs        if (sc->ifp != NULL) {
16057296579Sdavidcs            ether_ifdetach(sc->ifp);
16058296579Sdavidcs        }
16059296579Sdavidcs        ifmedia_removeall(&sc->ifmedia);
16060296579Sdavidcs        bxe_release_mutexes(sc);
16061296579Sdavidcs        bxe_deallocate_bars(sc);
16062296579Sdavidcs        pci_disable_busmaster(dev);
16063296579Sdavidcs        return (ENXIO);
16064296579Sdavidcs    }
16065296579Sdavidcs
16066255736Sdavidch    /* allocate ilt */
16067255736Sdavidch    if (bxe_alloc_ilt_mem(sc) != 0) {
16068296579Sdavidcs	bxe_free_buf_rings(sc);
16069255736Sdavidch        bxe_interrupt_free(sc);
16070292639Sdavidcs        bxe_del_cdev(sc);
16071266979Smarcel        if (sc->ifp != NULL) {
16072270876Sglebius            ether_ifdetach(sc->ifp);
16073255736Sdavidch        }
16074255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16075255736Sdavidch        bxe_release_mutexes(sc);
16076255736Sdavidch        bxe_deallocate_bars(sc);
16077255736Sdavidch        pci_disable_busmaster(dev);
16078255736Sdavidch        return (ENXIO);
16079255736Sdavidch    }
16080255736Sdavidch
16081255736Sdavidch    /* allocate the host hardware/software hsi structures */
16082255736Sdavidch    if (bxe_alloc_hsi_mem(sc) != 0) {
16083255736Sdavidch        bxe_free_ilt_mem(sc);
16084296579Sdavidcs	bxe_free_buf_rings(sc);
16085255736Sdavidch        bxe_interrupt_free(sc);
16086292639Sdavidcs        bxe_del_cdev(sc);
16087266979Smarcel        if (sc->ifp != NULL) {
16088270876Sglebius            ether_ifdetach(sc->ifp);
16089255736Sdavidch        }
16090255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16091255736Sdavidch        bxe_release_mutexes(sc);
16092255736Sdavidch        bxe_deallocate_bars(sc);
16093255736Sdavidch        pci_disable_busmaster(dev);
16094255736Sdavidch        return (ENXIO);
16095255736Sdavidch    }
16096255736Sdavidch
16097255736Sdavidch    /* need to reset chip if UNDI was active */
16098255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
16099255736Sdavidch        /* init fw_seq */
16100255736Sdavidch        sc->fw_seq =
16101255736Sdavidch            (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
16102255736Sdavidch             DRV_MSG_SEQ_NUMBER_MASK);
16103255736Sdavidch        BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
16104255736Sdavidch        bxe_prev_unload(sc);
16105255736Sdavidch    }
16106255736Sdavidch
16107255736Sdavidch#if 1
16108255736Sdavidch    /* XXX */
16109255736Sdavidch    bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16110255736Sdavidch#else
16111255736Sdavidch    if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
16112255736Sdavidch        SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
16113255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
16114255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
16115255736Sdavidch        bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
16116255736Sdavidch        bxe_dcbx_init_params(sc);
16117255736Sdavidch    } else {
16118255736Sdavidch        bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16119255736Sdavidch    }
16120255736Sdavidch#endif
16121255736Sdavidch
16122255736Sdavidch    /* calculate qm_cid_count */
16123255736Sdavidch    sc->qm_cid_count = bxe_set_qm_cid_count(sc);
16124255736Sdavidch    BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
16125255736Sdavidch
16126255736Sdavidch    sc->max_cos = 1;
16127255736Sdavidch    bxe_init_multi_cos(sc);
16128255736Sdavidch
16129255736Sdavidch    bxe_add_sysctls(sc);
16130255736Sdavidch
16131255736Sdavidch    return (0);
16132255736Sdavidch}
16133255736Sdavidch
16134255736Sdavidch/*
16135255736Sdavidch * Device detach function.
16136255736Sdavidch *
16137255736Sdavidch * Stops the controller, resets the controller, and releases resources.
16138255736Sdavidch *
16139255736Sdavidch * Returns:
16140255736Sdavidch *   0 = Success, >0 = Failure
16141255736Sdavidch */
16142255736Sdavidchstatic int
16143255736Sdavidchbxe_detach(device_t dev)
16144255736Sdavidch{
16145255736Sdavidch    struct bxe_softc *sc;
16146266979Smarcel    if_t ifp;
16147255736Sdavidch
16148255736Sdavidch    sc = device_get_softc(dev);
16149255736Sdavidch
16150255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16151255736Sdavidch
16152266979Smarcel    ifp = sc->ifp;
16153266979Smarcel    if (ifp != NULL && if_vlantrunkinuse(ifp)) {
16154255736Sdavidch        BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16155255736Sdavidch        return(EBUSY);
16156255736Sdavidch    }
16157255736Sdavidch
16158292639Sdavidcs    bxe_del_cdev(sc);
16159292639Sdavidcs
16160255736Sdavidch    /* stop the periodic callout */
16161255736Sdavidch    bxe_periodic_stop(sc);
16162255736Sdavidch
16163255736Sdavidch    /* stop the chip taskqueue */
16164255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16165255736Sdavidch    if (sc->chip_tq) {
16166255736Sdavidch        taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16167255736Sdavidch        taskqueue_free(sc->chip_tq);
16168255736Sdavidch        sc->chip_tq = NULL;
16169255736Sdavidch    }
16170255736Sdavidch
16171255736Sdavidch    /* stop and reset the controller if it was open */
16172255736Sdavidch    if (sc->state != BXE_STATE_CLOSED) {
16173255736Sdavidch        BXE_CORE_LOCK(sc);
16174255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16175297873Sdavidcs        sc->state = BXE_STATE_DISABLED;
16176255736Sdavidch        BXE_CORE_UNLOCK(sc);
16177255736Sdavidch    }
16178255736Sdavidch
16179255736Sdavidch    /* release the network interface */
16180255736Sdavidch    if (ifp != NULL) {
16181270876Sglebius        ether_ifdetach(ifp);
16182255736Sdavidch    }
16183255736Sdavidch    ifmedia_removeall(&sc->ifmedia);
16184255736Sdavidch
16185255736Sdavidch    /* XXX do the following based on driver state... */
16186255736Sdavidch
16187255736Sdavidch    /* free the host hardware/software hsi structures */
16188255736Sdavidch    bxe_free_hsi_mem(sc);
16189255736Sdavidch
16190255736Sdavidch    /* free ilt */
16191255736Sdavidch    bxe_free_ilt_mem(sc);
16192255736Sdavidch
16193296579Sdavidcs    bxe_free_buf_rings(sc);
16194296579Sdavidcs
16195255736Sdavidch    /* release the interrupts */
16196255736Sdavidch    bxe_interrupt_free(sc);
16197255736Sdavidch
16198255736Sdavidch    /* Release the mutexes*/
16199296579Sdavidcs    bxe_destroy_fp_mutexs(sc);
16200255736Sdavidch    bxe_release_mutexes(sc);
16201255736Sdavidch
16202296579Sdavidcs
16203255736Sdavidch    /* Release the PCIe BAR mapped memory */
16204255736Sdavidch    bxe_deallocate_bars(sc);
16205255736Sdavidch
16206255736Sdavidch    /* Release the FreeBSD interface. */
16207266979Smarcel    if (sc->ifp != NULL) {
16208270876Sglebius        if_free(sc->ifp);
16209255736Sdavidch    }
16210255736Sdavidch
16211255736Sdavidch    pci_disable_busmaster(dev);
16212255736Sdavidch
16213255736Sdavidch    return (0);
16214255736Sdavidch}
16215255736Sdavidch
16216255736Sdavidch/*
16217255736Sdavidch * Device shutdown function.
16218255736Sdavidch *
16219255736Sdavidch * Stops and resets the controller.
16220255736Sdavidch *
16221255736Sdavidch * Returns:
16222255736Sdavidch *   Nothing
16223255736Sdavidch */
16224255736Sdavidchstatic int
16225255736Sdavidchbxe_shutdown(device_t dev)
16226255736Sdavidch{
16227255736Sdavidch    struct bxe_softc *sc;
16228255736Sdavidch
16229255736Sdavidch    sc = device_get_softc(dev);
16230255736Sdavidch
16231255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16232255736Sdavidch
16233255736Sdavidch    /* stop the periodic callout */
16234255736Sdavidch    bxe_periodic_stop(sc);
16235255736Sdavidch
16236255736Sdavidch    BXE_CORE_LOCK(sc);
16237255736Sdavidch    bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16238255736Sdavidch    BXE_CORE_UNLOCK(sc);
16239255736Sdavidch
16240255736Sdavidch    return (0);
16241255736Sdavidch}
16242255736Sdavidch
16243255736Sdavidchvoid
16244255736Sdavidchbxe_igu_ack_sb(struct bxe_softc *sc,
16245255736Sdavidch               uint8_t          igu_sb_id,
16246255736Sdavidch               uint8_t          segment,
16247255736Sdavidch               uint16_t         index,
16248255736Sdavidch               uint8_t          op,
16249255736Sdavidch               uint8_t          update)
16250255736Sdavidch{
16251255736Sdavidch    uint32_t igu_addr = sc->igu_base_addr;
16252255736Sdavidch    igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16253255736Sdavidch    bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16254255736Sdavidch}
16255255736Sdavidch
16256255736Sdavidchstatic void
16257255736Sdavidchbxe_igu_clear_sb_gen(struct bxe_softc *sc,
16258255736Sdavidch                     uint8_t          func,
16259255736Sdavidch                     uint8_t          idu_sb_id,
16260255736Sdavidch                     uint8_t          is_pf)
16261255736Sdavidch{
16262255736Sdavidch    uint32_t data, ctl, cnt = 100;
16263255736Sdavidch    uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16264255736Sdavidch    uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16265255736Sdavidch    uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16266255736Sdavidch    uint32_t sb_bit =  1 << (idu_sb_id%32);
16267255736Sdavidch    uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16268255736Sdavidch    uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16269255736Sdavidch
16270255736Sdavidch    /* Not supported in BC mode */
16271255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
16272255736Sdavidch        return;
16273255736Sdavidch    }
16274255736Sdavidch
16275255736Sdavidch    data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16276255736Sdavidch             IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16277255736Sdavidch            IGU_REGULAR_CLEANUP_SET |
16278255736Sdavidch            IGU_REGULAR_BCLEANUP);
16279255736Sdavidch
16280255736Sdavidch    ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16281255736Sdavidch           (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16282255736Sdavidch           (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16283255736Sdavidch
16284255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16285255736Sdavidch            data, igu_addr_data);
16286255736Sdavidch    REG_WR(sc, igu_addr_data, data);
16287255736Sdavidch
16288255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16289255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16290255736Sdavidch    mb();
16291255736Sdavidch
16292255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16293255736Sdavidch            ctl, igu_addr_ctl);
16294255736Sdavidch    REG_WR(sc, igu_addr_ctl, ctl);
16295255736Sdavidch
16296255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16297255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16298255736Sdavidch    mb();
16299255736Sdavidch
16300255736Sdavidch    /* wait for clean up to finish */
16301255736Sdavidch    while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16302255736Sdavidch        DELAY(20000);
16303255736Sdavidch    }
16304255736Sdavidch
16305255736Sdavidch    if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16306255736Sdavidch        BLOGD(sc, DBG_LOAD,
16307255736Sdavidch              "Unable to finish IGU cleanup: "
16308255736Sdavidch              "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16309255736Sdavidch              idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16310255736Sdavidch    }
16311255736Sdavidch}
16312255736Sdavidch
16313255736Sdavidchstatic void
16314255736Sdavidchbxe_igu_clear_sb(struct bxe_softc *sc,
16315255736Sdavidch                 uint8_t          idu_sb_id)
16316255736Sdavidch{
16317255736Sdavidch    bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16318255736Sdavidch}
16319255736Sdavidch
16320255736Sdavidch
16321255736Sdavidch
16322255736Sdavidch
16323255736Sdavidch
16324255736Sdavidch
16325255736Sdavidch
16326255736Sdavidch/*******************/
16327255736Sdavidch/* ECORE CALLBACKS */
16328255736Sdavidch/*******************/
16329255736Sdavidch
16330255736Sdavidchstatic void
16331255736Sdavidchbxe_reset_common(struct bxe_softc *sc)
16332255736Sdavidch{
16333255736Sdavidch    uint32_t val = 0x1400;
16334255736Sdavidch
16335255736Sdavidch    /* reset_common */
16336255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16337255736Sdavidch
16338255736Sdavidch    if (CHIP_IS_E3(sc)) {
16339255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16340255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16341255736Sdavidch    }
16342255736Sdavidch
16343255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16344255736Sdavidch}
16345255736Sdavidch
16346255736Sdavidchstatic void
16347255736Sdavidchbxe_common_init_phy(struct bxe_softc *sc)
16348255736Sdavidch{
16349255736Sdavidch    uint32_t shmem_base[2];
16350255736Sdavidch    uint32_t shmem2_base[2];
16351255736Sdavidch
16352255736Sdavidch    /* Avoid common init in case MFW supports LFA */
16353255736Sdavidch    if (SHMEM2_RD(sc, size) >
16354255736Sdavidch        (uint32_t)offsetof(struct shmem2_region,
16355255736Sdavidch                           lfa_host_addr[SC_PORT(sc)])) {
16356255736Sdavidch        return;
16357255736Sdavidch    }
16358255736Sdavidch
16359255736Sdavidch    shmem_base[0]  = sc->devinfo.shmem_base;
16360255736Sdavidch    shmem2_base[0] = sc->devinfo.shmem2_base;
16361255736Sdavidch
16362255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16363255736Sdavidch        shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16364255736Sdavidch        shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16365255736Sdavidch    }
16366255736Sdavidch
16367284335Sdavidcs    bxe_acquire_phy_lock(sc);
16368255736Sdavidch    elink_common_init_phy(sc, shmem_base, shmem2_base,
16369255736Sdavidch                          sc->devinfo.chip_id, 0);
16370284335Sdavidcs    bxe_release_phy_lock(sc);
16371255736Sdavidch}
16372255736Sdavidch
16373255736Sdavidchstatic void
16374255736Sdavidchbxe_pf_disable(struct bxe_softc *sc)
16375255736Sdavidch{
16376255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16377255736Sdavidch
16378255736Sdavidch    val &= ~IGU_PF_CONF_FUNC_EN;
16379255736Sdavidch
16380255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16381255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16382255736Sdavidch    REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16383255736Sdavidch}
16384255736Sdavidch
16385255736Sdavidchstatic void
16386255736Sdavidchbxe_init_pxp(struct bxe_softc *sc)
16387255736Sdavidch{
16388255736Sdavidch    uint16_t devctl;
16389255736Sdavidch    int r_order, w_order;
16390255736Sdavidch
16391255736Sdavidch    devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16392255736Sdavidch
16393255736Sdavidch    BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16394255736Sdavidch
16395255736Sdavidch    w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16396255736Sdavidch
16397255736Sdavidch    if (sc->mrrs == -1) {
16398255736Sdavidch        r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16399255736Sdavidch    } else {
16400255736Sdavidch        BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16401255736Sdavidch        r_order = sc->mrrs;
16402255736Sdavidch    }
16403255736Sdavidch
16404255736Sdavidch    ecore_init_pxp_arb(sc, r_order, w_order);
16405255736Sdavidch}
16406255736Sdavidch
16407255736Sdavidchstatic uint32_t
16408255736Sdavidchbxe_get_pretend_reg(struct bxe_softc *sc)
16409255736Sdavidch{
16410255736Sdavidch    uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16411255736Sdavidch    uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16412255736Sdavidch    return (base + (SC_ABS_FUNC(sc)) * stride);
16413255736Sdavidch}
16414255736Sdavidch
16415255736Sdavidch/*
16416255736Sdavidch * Called only on E1H or E2.
16417255736Sdavidch * When pretending to be PF, the pretend value is the function number 0..7.
16418255736Sdavidch * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16419255736Sdavidch * combination.
16420255736Sdavidch */
16421255736Sdavidchstatic int
16422255736Sdavidchbxe_pretend_func(struct bxe_softc *sc,
16423255736Sdavidch                 uint16_t         pretend_func_val)
16424255736Sdavidch{
16425255736Sdavidch    uint32_t pretend_reg;
16426255736Sdavidch
16427255736Sdavidch    if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16428255736Sdavidch        return (-1);
16429255736Sdavidch    }
16430255736Sdavidch
16431255736Sdavidch    /* get my own pretend register */
16432255736Sdavidch    pretend_reg = bxe_get_pretend_reg(sc);
16433255736Sdavidch    REG_WR(sc, pretend_reg, pretend_func_val);
16434255736Sdavidch    REG_RD(sc, pretend_reg);
16435255736Sdavidch    return (0);
16436255736Sdavidch}
16437255736Sdavidch
16438255736Sdavidchstatic void
16439255736Sdavidchbxe_iov_init_dmae(struct bxe_softc *sc)
16440255736Sdavidch{
16441255736Sdavidch    return;
16442255736Sdavidch}
16443255736Sdavidch
16444255736Sdavidchstatic void
16445255736Sdavidchbxe_iov_init_dq(struct bxe_softc *sc)
16446255736Sdavidch{
16447255736Sdavidch    return;
16448255736Sdavidch}
16449255736Sdavidch
16450255736Sdavidch/* send a NIG loopback debug packet */
16451255736Sdavidchstatic void
16452255736Sdavidchbxe_lb_pckt(struct bxe_softc *sc)
16453255736Sdavidch{
16454255736Sdavidch    uint32_t wb_write[3];
16455255736Sdavidch
16456255736Sdavidch    /* Ethernet source and destination addresses */
16457255736Sdavidch    wb_write[0] = 0x55555555;
16458255736Sdavidch    wb_write[1] = 0x55555555;
16459255736Sdavidch    wb_write[2] = 0x20;     /* SOP */
16460255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16461255736Sdavidch
16462255736Sdavidch    /* NON-IP protocol */
16463255736Sdavidch    wb_write[0] = 0x09000000;
16464255736Sdavidch    wb_write[1] = 0x55555555;
16465255736Sdavidch    wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16466255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16467255736Sdavidch}
16468255736Sdavidch
16469255736Sdavidch/*
16470255736Sdavidch * Some of the internal memories are not directly readable from the driver.
16471255736Sdavidch * To test them we send debug packets.
16472255736Sdavidch */
16473255736Sdavidchstatic int
16474255736Sdavidchbxe_int_mem_test(struct bxe_softc *sc)
16475255736Sdavidch{
16476255736Sdavidch    int factor;
16477255736Sdavidch    int count, i;
16478255736Sdavidch    uint32_t val = 0;
16479255736Sdavidch
16480255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
16481255736Sdavidch        factor = 120;
16482255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
16483255736Sdavidch        factor = 200;
16484255736Sdavidch    } else {
16485255736Sdavidch        factor = 1;
16486255736Sdavidch    }
16487255736Sdavidch
16488255736Sdavidch    /* disable inputs of parser neighbor blocks */
16489255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16490255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16491255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16492255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16493255736Sdavidch
16494255736Sdavidch    /*  write 0 to parser credits for CFC search request */
16495255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16496255736Sdavidch
16497255736Sdavidch    /* send Ethernet packet */
16498255736Sdavidch    bxe_lb_pckt(sc);
16499255736Sdavidch
16500255736Sdavidch    /* TODO do i reset NIG statistic? */
16501255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16502255736Sdavidch    count = 1000 * factor;
16503255736Sdavidch    while (count) {
16504255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16505255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16506255736Sdavidch        if (val == 0x10) {
16507255736Sdavidch            break;
16508255736Sdavidch        }
16509255736Sdavidch
16510255736Sdavidch        DELAY(10000);
16511255736Sdavidch        count--;
16512255736Sdavidch    }
16513255736Sdavidch
16514255736Sdavidch    if (val != 0x10) {
16515255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16516255736Sdavidch        return (-1);
16517255736Sdavidch    }
16518255736Sdavidch
16519255736Sdavidch    /* wait until PRS register shows 1 packet */
16520255736Sdavidch    count = (1000 * factor);
16521255736Sdavidch    while (count) {
16522255736Sdavidch        val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16523255736Sdavidch        if (val == 1) {
16524255736Sdavidch            break;
16525255736Sdavidch        }
16526255736Sdavidch
16527255736Sdavidch        DELAY(10000);
16528255736Sdavidch        count--;
16529255736Sdavidch    }
16530255736Sdavidch
16531255736Sdavidch    if (val != 0x1) {
16532255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16533255736Sdavidch        return (-2);
16534255736Sdavidch    }
16535255736Sdavidch
16536255736Sdavidch    /* Reset and init BRB, PRS */
16537255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16538255736Sdavidch    DELAY(50000);
16539255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16540255736Sdavidch    DELAY(50000);
16541255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16542255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16543255736Sdavidch
16544255736Sdavidch    /* Disable inputs of parser neighbor blocks */
16545255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16546255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16547255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16548255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16549255736Sdavidch
16550255736Sdavidch    /* Write 0 to parser credits for CFC search request */
16551255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16552255736Sdavidch
16553255736Sdavidch    /* send 10 Ethernet packets */
16554255736Sdavidch    for (i = 0; i < 10; i++) {
16555255736Sdavidch        bxe_lb_pckt(sc);
16556255736Sdavidch    }
16557255736Sdavidch
16558255736Sdavidch    /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16559255736Sdavidch    count = (1000 * factor);
16560255736Sdavidch    while (count) {
16561255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16562255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16563255736Sdavidch        if (val == 0xb0) {
16564255736Sdavidch            break;
16565255736Sdavidch        }
16566255736Sdavidch
16567255736Sdavidch        DELAY(10000);
16568255736Sdavidch        count--;
16569255736Sdavidch    }
16570255736Sdavidch
16571255736Sdavidch    if (val != 0xb0) {
16572255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16573255736Sdavidch        return (-3);
16574255736Sdavidch    }
16575255736Sdavidch
16576255736Sdavidch    /* Wait until PRS register shows 2 packets */
16577255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16578255736Sdavidch    if (val != 2) {
16579255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16580255736Sdavidch    }
16581255736Sdavidch
16582255736Sdavidch    /* Write 1 to parser credits for CFC search request */
16583255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16584255736Sdavidch
16585255736Sdavidch    /* Wait until PRS register shows 3 packets */
16586255736Sdavidch    DELAY(10000 * factor);
16587255736Sdavidch
16588255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16589255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16590255736Sdavidch    if (val != 3) {
16591255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16592255736Sdavidch    }
16593255736Sdavidch
16594255736Sdavidch    /* clear NIG EOP FIFO */
16595255736Sdavidch    for (i = 0; i < 11; i++) {
16596255736Sdavidch        REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16597255736Sdavidch    }
16598255736Sdavidch
16599255736Sdavidch    val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16600255736Sdavidch    if (val != 1) {
16601295830Sdavidcs        BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16602255736Sdavidch        return (-4);
16603255736Sdavidch    }
16604255736Sdavidch
16605255736Sdavidch    /* Reset and init BRB, PRS, NIG */
16606255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16607255736Sdavidch    DELAY(50000);
16608255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16609255736Sdavidch    DELAY(50000);
16610255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16611255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16612255736Sdavidch    if (!CNIC_SUPPORT(sc)) {
16613255736Sdavidch        /* set NIC mode */
16614255736Sdavidch        REG_WR(sc, PRS_REG_NIC_MODE, 1);
16615255736Sdavidch    }
16616255736Sdavidch
16617255736Sdavidch    /* Enable inputs of parser neighbor blocks */
16618255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16619255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16620255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16621255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16622255736Sdavidch
16623255736Sdavidch    return (0);
16624255736Sdavidch}
16625255736Sdavidch
16626255736Sdavidchstatic void
16627255736Sdavidchbxe_setup_fan_failure_detection(struct bxe_softc *sc)
16628255736Sdavidch{
16629255736Sdavidch    int is_required;
16630255736Sdavidch    uint32_t val;
16631255736Sdavidch    int port;
16632255736Sdavidch
16633255736Sdavidch    is_required = 0;
16634255736Sdavidch    val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
16635255736Sdavidch           SHARED_HW_CFG_FAN_FAILURE_MASK);
16636255736Sdavidch
16637255736Sdavidch    if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
16638255736Sdavidch        is_required = 1;
16639255736Sdavidch    }
16640255736Sdavidch    /*
16641255736Sdavidch     * The fan failure mechanism is usually related to the PHY type since
16642255736Sdavidch     * the power consumption of the board is affected by the PHY. Currently,
16643255736Sdavidch     * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
16644255736Sdavidch     */
16645255736Sdavidch    else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
16646255736Sdavidch        for (port = PORT_0; port < PORT_MAX; port++) {
16647255736Sdavidch            is_required |= elink_fan_failure_det_req(sc,
16648255736Sdavidch                                                     sc->devinfo.shmem_base,
16649255736Sdavidch                                                     sc->devinfo.shmem2_base,
16650255736Sdavidch                                                     port);
16651255736Sdavidch        }
16652255736Sdavidch    }
16653255736Sdavidch
16654255736Sdavidch    BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
16655255736Sdavidch
16656255736Sdavidch    if (is_required == 0) {
16657255736Sdavidch        return;
16658255736Sdavidch    }
16659255736Sdavidch
16660255736Sdavidch    /* Fan failure is indicated by SPIO 5 */
16661255736Sdavidch    bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
16662255736Sdavidch
16663255736Sdavidch    /* set to active low mode */
16664255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_INT);
16665255736Sdavidch    val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
16666255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_INT, val);
16667255736Sdavidch
16668255736Sdavidch    /* enable interrupt to signal the IGU */
16669255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
16670255736Sdavidch    val |= MISC_SPIO_SPIO5;
16671255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
16672255736Sdavidch}
16673255736Sdavidch
16674255736Sdavidchstatic void
16675255736Sdavidchbxe_enable_blocks_attention(struct bxe_softc *sc)
16676255736Sdavidch{
16677255736Sdavidch    uint32_t val;
16678255736Sdavidch
16679255736Sdavidch    REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16680255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16681255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
16682255736Sdavidch    } else {
16683255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
16684255736Sdavidch    }
16685255736Sdavidch    REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16686255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16687255736Sdavidch    /*
16688255736Sdavidch     * mask read length error interrupts in brb for parser
16689255736Sdavidch     * (parsing unit and 'checksum and crc' unit)
16690255736Sdavidch     * these errors are legal (PU reads fixed length and CAC can cause
16691255736Sdavidch     * read length error on truncated packets)
16692255736Sdavidch     */
16693255736Sdavidch    REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
16694255736Sdavidch    REG_WR(sc, QM_REG_QM_INT_MASK, 0);
16695255736Sdavidch    REG_WR(sc, TM_REG_TM_INT_MASK, 0);
16696255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
16697255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
16698255736Sdavidch    REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
16699255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
16700255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
16701255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
16702255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
16703255736Sdavidch    REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
16704255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
16705255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
16706255736Sdavidch    REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
16707255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
16708255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
16709255736Sdavidch    REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
16710255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
16711255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
16712255736Sdavidch
16713255736Sdavidch    val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
16714255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
16715255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
16716255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16717255736Sdavidch        val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
16718255736Sdavidch                PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
16719255736Sdavidch    }
16720255736Sdavidch    REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
16721255736Sdavidch
16722255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
16723255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
16724255736Sdavidch    REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
16725255736Sdavidch/*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
16726255736Sdavidch
16727255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16728255736Sdavidch        /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
16729255736Sdavidch        REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
16730255736Sdavidch    }
16731255736Sdavidch
16732255736Sdavidch    REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
16733255736Sdavidch    REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
16734255736Sdavidch/*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
16735255736Sdavidch    REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
16736255736Sdavidch}
16737255736Sdavidch
16738255736Sdavidch/**
16739255736Sdavidch * bxe_init_hw_common - initialize the HW at the COMMON phase.
16740255736Sdavidch *
16741255736Sdavidch * @sc:     driver handle
16742255736Sdavidch */
16743255736Sdavidchstatic int
16744255736Sdavidchbxe_init_hw_common(struct bxe_softc *sc)
16745255736Sdavidch{
16746255736Sdavidch    uint8_t abs_func_id;
16747255736Sdavidch    uint32_t val;
16748255736Sdavidch
16749255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
16750255736Sdavidch          SC_ABS_FUNC(sc));
16751255736Sdavidch
16752255736Sdavidch    /*
16753255736Sdavidch     * take the RESET lock to protect undi_unload flow from accessing
16754255736Sdavidch     * registers while we are resetting the chip
16755255736Sdavidch     */
16756255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16757255736Sdavidch
16758255736Sdavidch    bxe_reset_common(sc);
16759255736Sdavidch
16760255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
16761255736Sdavidch
16762255736Sdavidch    val = 0xfffc;
16763255736Sdavidch    if (CHIP_IS_E3(sc)) {
16764255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16765255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16766255736Sdavidch    }
16767255736Sdavidch
16768255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
16769255736Sdavidch
16770255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16771255736Sdavidch
16772255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
16773255736Sdavidch    BLOGD(sc, DBG_LOAD, "after misc block init\n");
16774255736Sdavidch
16775255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16776255736Sdavidch        /*
16777255736Sdavidch         * 4-port mode or 2-port mode we need to turn off master-enable for
16778255736Sdavidch         * everyone. After that we turn it back on for self. So, we disregard
16779255736Sdavidch         * multi-function, and always disable all functions on the given path,
16780255736Sdavidch         * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
16781255736Sdavidch         */
16782255736Sdavidch        for (abs_func_id = SC_PATH(sc);
16783255736Sdavidch             abs_func_id < (E2_FUNC_MAX * 2);
16784255736Sdavidch             abs_func_id += 2) {
16785255736Sdavidch            if (abs_func_id == SC_ABS_FUNC(sc)) {
16786255736Sdavidch                REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
16787255736Sdavidch                continue;
16788255736Sdavidch            }
16789255736Sdavidch
16790255736Sdavidch            bxe_pretend_func(sc, abs_func_id);
16791255736Sdavidch
16792255736Sdavidch            /* clear pf enable */
16793255736Sdavidch            bxe_pf_disable(sc);
16794255736Sdavidch
16795255736Sdavidch            bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16796255736Sdavidch        }
16797255736Sdavidch    }
16798255736Sdavidch
16799255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pf disable\n");
16800255736Sdavidch
16801255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
16802255736Sdavidch
16803255736Sdavidch    if (CHIP_IS_E1(sc)) {
16804255736Sdavidch        /*
16805255736Sdavidch         * enable HW interrupt from PXP on USDM overflow
16806255736Sdavidch         * bit 16 on INT_MASK_0
16807255736Sdavidch         */
16808255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16809255736Sdavidch    }
16810255736Sdavidch
16811255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
16812255736Sdavidch    bxe_init_pxp(sc);
16813255736Sdavidch
16814255736Sdavidch#ifdef __BIG_ENDIAN
16815255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
16816255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
16817255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
16818255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
16819255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
16820255736Sdavidch    /* make sure this value is 0 */
16821255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
16822255736Sdavidch
16823255736Sdavidch    //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
16824255736Sdavidch    REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
16825255736Sdavidch    REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
16826255736Sdavidch    REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
16827255736Sdavidch    REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
16828255736Sdavidch#endif
16829255736Sdavidch
16830255736Sdavidch    ecore_ilt_init_page_size(sc, INITOP_SET);
16831255736Sdavidch
16832255736Sdavidch    if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
16833255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
16834255736Sdavidch    }
16835255736Sdavidch
16836255736Sdavidch    /* let the HW do it's magic... */
16837255736Sdavidch    DELAY(100000);
16838255736Sdavidch
16839255736Sdavidch    /* finish PXP init */
16840255736Sdavidch    val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
16841255736Sdavidch    if (val != 1) {
16842295830Sdavidcs        BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
16843295830Sdavidcs            val);
16844255736Sdavidch        return (-1);
16845255736Sdavidch    }
16846255736Sdavidch    val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
16847255736Sdavidch    if (val != 1) {
16848295830Sdavidcs        BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
16849255736Sdavidch        return (-1);
16850255736Sdavidch    }
16851255736Sdavidch
16852255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pxp init\n");
16853255736Sdavidch
16854255736Sdavidch    /*
16855255736Sdavidch     * Timer bug workaround for E2 only. We need to set the entire ILT to have
16856255736Sdavidch     * entries with value "0" and valid bit on. This needs to be done by the
16857255736Sdavidch     * first PF that is loaded in a path (i.e. common phase)
16858255736Sdavidch     */
16859255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16860255736Sdavidch/*
16861255736Sdavidch * In E2 there is a bug in the timers block that can cause function 6 / 7
16862255736Sdavidch * (i.e. vnic3) to start even if it is marked as "scan-off".
16863255736Sdavidch * This occurs when a different function (func2,3) is being marked
16864255736Sdavidch * as "scan-off". Real-life scenario for example: if a driver is being
16865255736Sdavidch * load-unloaded while func6,7 are down. This will cause the timer to access
16866255736Sdavidch * the ilt, translate to a logical address and send a request to read/write.
16867255736Sdavidch * Since the ilt for the function that is down is not valid, this will cause
16868255736Sdavidch * a translation error which is unrecoverable.
16869255736Sdavidch * The Workaround is intended to make sure that when this happens nothing
16870255736Sdavidch * fatal will occur. The workaround:
16871255736Sdavidch *  1.  First PF driver which loads on a path will:
16872255736Sdavidch *      a.  After taking the chip out of reset, by using pretend,
16873255736Sdavidch *          it will write "0" to the following registers of
16874255736Sdavidch *          the other vnics.
16875255736Sdavidch *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16876255736Sdavidch *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
16877255736Sdavidch *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
16878255736Sdavidch *          And for itself it will write '1' to
16879255736Sdavidch *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
16880255736Sdavidch *          dmae-operations (writing to pram for example.)
16881255736Sdavidch *          note: can be done for only function 6,7 but cleaner this
16882255736Sdavidch *            way.
16883255736Sdavidch *      b.  Write zero+valid to the entire ILT.
16884255736Sdavidch *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
16885255736Sdavidch *          VNIC3 (of that port). The range allocated will be the
16886255736Sdavidch *          entire ILT. This is needed to prevent  ILT range error.
16887255736Sdavidch *  2.  Any PF driver load flow:
16888255736Sdavidch *      a.  ILT update with the physical addresses of the allocated
16889255736Sdavidch *          logical pages.
16890255736Sdavidch *      b.  Wait 20msec. - note that this timeout is needed to make
16891255736Sdavidch *          sure there are no requests in one of the PXP internal
16892255736Sdavidch *          queues with "old" ILT addresses.
16893255736Sdavidch *      c.  PF enable in the PGLC.
16894255736Sdavidch *      d.  Clear the was_error of the PF in the PGLC. (could have
16895255736Sdavidch *          occurred while driver was down)
16896255736Sdavidch *      e.  PF enable in the CFC (WEAK + STRONG)
16897255736Sdavidch *      f.  Timers scan enable
16898255736Sdavidch *  3.  PF driver unload flow:
16899255736Sdavidch *      a.  Clear the Timers scan_en.
16900255736Sdavidch *      b.  Polling for scan_on=0 for that PF.
16901255736Sdavidch *      c.  Clear the PF enable bit in the PXP.
16902255736Sdavidch *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
16903255736Sdavidch *      e.  Write zero+valid to all ILT entries (The valid bit must
16904255736Sdavidch *          stay set)
16905255736Sdavidch *      f.  If this is VNIC 3 of a port then also init
16906255736Sdavidch *          first_timers_ilt_entry to zero and last_timers_ilt_entry
16907255736Sdavidch *          to the last enrty in the ILT.
16908255736Sdavidch *
16909255736Sdavidch *      Notes:
16910255736Sdavidch *      Currently the PF error in the PGLC is non recoverable.
16911255736Sdavidch *      In the future the there will be a recovery routine for this error.
16912255736Sdavidch *      Currently attention is masked.
16913255736Sdavidch *      Having an MCP lock on the load/unload process does not guarantee that
16914255736Sdavidch *      there is no Timer disable during Func6/7 enable. This is because the
16915255736Sdavidch *      Timers scan is currently being cleared by the MCP on FLR.
16916255736Sdavidch *      Step 2.d can be done only for PF6/7 and the driver can also check if
16917255736Sdavidch *      there is error before clearing it. But the flow above is simpler and
16918255736Sdavidch *      more general.
16919255736Sdavidch *      All ILT entries are written by zero+valid and not just PF6/7
16920255736Sdavidch *      ILT entries since in the future the ILT entries allocation for
16921255736Sdavidch *      PF-s might be dynamic.
16922255736Sdavidch */
16923255736Sdavidch        struct ilt_client_info ilt_cli;
16924255736Sdavidch        struct ecore_ilt ilt;
16925255736Sdavidch
16926255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
16927255736Sdavidch        memset(&ilt, 0, sizeof(struct ecore_ilt));
16928255736Sdavidch
16929255736Sdavidch        /* initialize dummy TM client */
16930255736Sdavidch        ilt_cli.start      = 0;
16931255736Sdavidch        ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
16932255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
16933255736Sdavidch
16934255736Sdavidch        /*
16935255736Sdavidch         * Step 1: set zeroes to all ilt page entries with valid bit on
16936255736Sdavidch         * Step 2: set the timers first/last ilt entry to point
16937255736Sdavidch         * to the entire range to prevent ILT range error for 3rd/4th
16938255736Sdavidch         * vnic (this code assumes existence of the vnic)
16939255736Sdavidch         *
16940255736Sdavidch         * both steps performed by call to ecore_ilt_client_init_op()
16941255736Sdavidch         * with dummy TM client
16942255736Sdavidch         *
16943255736Sdavidch         * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
16944255736Sdavidch         * and his brother are split registers
16945255736Sdavidch         */
16946255736Sdavidch
16947255736Sdavidch        bxe_pretend_func(sc, (SC_PATH(sc) + 6));
16948255736Sdavidch        ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
16949255736Sdavidch        bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16950255736Sdavidch
16951255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
16952255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
16953255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
16954255736Sdavidch    }
16955255736Sdavidch
16956255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
16957255736Sdavidch    REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
16958255736Sdavidch
16959255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16960255736Sdavidch        int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
16961255736Sdavidch                     (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
16962255736Sdavidch
16963255736Sdavidch        ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
16964255736Sdavidch        ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
16965255736Sdavidch
16966255736Sdavidch        /* let the HW do it's magic... */
16967255736Sdavidch        do {
16968255736Sdavidch            DELAY(200000);
16969255736Sdavidch            val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
16970255736Sdavidch        } while (factor-- && (val != 1));
16971255736Sdavidch
16972255736Sdavidch        if (val != 1) {
16973295830Sdavidcs            BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
16974255736Sdavidch            return (-1);
16975255736Sdavidch        }
16976255736Sdavidch    }
16977255736Sdavidch
16978255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
16979255736Sdavidch
16980255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
16981255736Sdavidch
16982255736Sdavidch    bxe_iov_init_dmae(sc);
16983255736Sdavidch
16984255736Sdavidch    /* clean the DMAE memory */
16985255736Sdavidch    sc->dmae_ready = 1;
16986255736Sdavidch    ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
16987255736Sdavidch
16988255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
16989255736Sdavidch
16990255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
16991255736Sdavidch
16992255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
16993255736Sdavidch
16994255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
16995255736Sdavidch
16996255736Sdavidch    bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
16997255736Sdavidch    bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
16998255736Sdavidch    bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
16999255736Sdavidch    bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
17000255736Sdavidch
17001255736Sdavidch    ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
17002255736Sdavidch
17003255736Sdavidch    /* QM queues pointers table */
17004255736Sdavidch    ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
17005255736Sdavidch
17006255736Sdavidch    /* soft reset pulse */
17007255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 1);
17008255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 0);
17009255736Sdavidch
17010255736Sdavidch    if (CNIC_SUPPORT(sc))
17011255736Sdavidch        ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
17012255736Sdavidch
17013255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
17014255736Sdavidch    REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
17015255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17016255736Sdavidch        /* enable hw interrupt from doorbell Q */
17017255736Sdavidch        REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17018255736Sdavidch    }
17019255736Sdavidch
17020255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17021255736Sdavidch
17022255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17023255736Sdavidch    REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
17024255736Sdavidch
17025255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17026255736Sdavidch        REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
17027255736Sdavidch    }
17028255736Sdavidch
17029255736Sdavidch    if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
17030255736Sdavidch        if (IS_MF_AFEX(sc)) {
17031255736Sdavidch            /*
17032255736Sdavidch             * configure that AFEX and VLAN headers must be
17033255736Sdavidch             * received in AFEX mode
17034255736Sdavidch             */
17035255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
17036255736Sdavidch            REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
17037255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
17038255736Sdavidch            REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
17039255736Sdavidch            REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
17040255736Sdavidch        } else {
17041255736Sdavidch            /*
17042255736Sdavidch             * Bit-map indicating which L2 hdrs may appear
17043255736Sdavidch             * after the basic Ethernet header
17044255736Sdavidch             */
17045255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
17046255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17047255736Sdavidch        }
17048255736Sdavidch    }
17049255736Sdavidch
17050255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
17051255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
17052255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
17053255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
17054255736Sdavidch
17055255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17056255736Sdavidch        /* reset VFC memories */
17057255736Sdavidch        REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17058255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
17059255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
17060255736Sdavidch        REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17061255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
17062255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
17063255736Sdavidch
17064255736Sdavidch        DELAY(20000);
17065255736Sdavidch    }
17066255736Sdavidch
17067255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
17068255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
17069255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
17070255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
17071255736Sdavidch
17072255736Sdavidch    /* sync semi rtc */
17073255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
17074255736Sdavidch           0x80000000);
17075255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
17076255736Sdavidch           0x80000000);
17077255736Sdavidch
17078255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
17079255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
17080255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
17081255736Sdavidch
17082255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17083255736Sdavidch        if (IS_MF_AFEX(sc)) {
17084255736Sdavidch            /*
17085255736Sdavidch             * configure that AFEX and VLAN headers must be
17086255736Sdavidch             * sent in AFEX mode
17087255736Sdavidch             */
17088255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
17089255736Sdavidch            REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
17090255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
17091255736Sdavidch            REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
17092255736Sdavidch            REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
17093255736Sdavidch        } else {
17094255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
17095255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17096255736Sdavidch        }
17097255736Sdavidch    }
17098255736Sdavidch
17099255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 1);
17100255736Sdavidch
17101255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
17102255736Sdavidch
17103255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17104255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
17105255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
17106255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
17107255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
17108255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
17109255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
17110255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
17111255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
17112255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
17113255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
17114255736Sdavidch    }
17115255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 0);
17116255736Sdavidch
17117255736Sdavidch    if (sizeof(union cdu_context) != 1024) {
17118255736Sdavidch        /* we currently assume that a context is 1024 bytes */
17119255736Sdavidch        BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
17120255736Sdavidch              (long)sizeof(union cdu_context));
17121255736Sdavidch    }
17122255736Sdavidch
17123255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
17124255736Sdavidch    val = (4 << 24) + (0 << 12) + 1024;
17125255736Sdavidch    REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
17126255736Sdavidch
17127255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
17128255736Sdavidch
17129255736Sdavidch    REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
17130255736Sdavidch    /* enable context validation interrupt from CFC */
17131255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17132255736Sdavidch
17133255736Sdavidch    /* set the thresholds to prevent CFC/CDU race */
17134255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
17135255736Sdavidch    ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
17136255736Sdavidch
17137255736Sdavidch    if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
17138255736Sdavidch        REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
17139255736Sdavidch    }
17140255736Sdavidch
17141255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
17142255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
17143255736Sdavidch
17144255736Sdavidch    /* Reset PCIE errors for debug */
17145255736Sdavidch    REG_WR(sc, 0x2814, 0xffffffff);
17146255736Sdavidch    REG_WR(sc, 0x3820, 0xffffffff);
17147255736Sdavidch
17148255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17149255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
17150255736Sdavidch               (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17151255736Sdavidch                PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17152255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17153255736Sdavidch               (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17154255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17155255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17156255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17157255736Sdavidch               (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17158255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17159255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17160255736Sdavidch    }
17161255736Sdavidch
17162255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17163255736Sdavidch
17164255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17165255736Sdavidch        /* in E3 this done in per-port section */
17166255736Sdavidch        if (!CHIP_IS_E3(sc))
17167255736Sdavidch            REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17168255736Sdavidch    }
17169255736Sdavidch
17170255736Sdavidch    if (CHIP_IS_E1H(sc)) {
17171255736Sdavidch        /* not applicable for E2 (and above ...) */
17172255736Sdavidch        REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17173255736Sdavidch    }
17174255736Sdavidch
17175255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
17176255736Sdavidch        DELAY(200000);
17177255736Sdavidch    }
17178255736Sdavidch
17179255736Sdavidch    /* finish CFC init */
17180255736Sdavidch    val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17181255736Sdavidch    if (val != 1) {
17182295830Sdavidcs        BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17183255736Sdavidch        return (-1);
17184255736Sdavidch    }
17185255736Sdavidch    val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17186255736Sdavidch    if (val != 1) {
17187295830Sdavidcs        BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17188255736Sdavidch        return (-1);
17189255736Sdavidch    }
17190255736Sdavidch    val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17191255736Sdavidch    if (val != 1) {
17192295830Sdavidcs        BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17193255736Sdavidch        return (-1);
17194255736Sdavidch    }
17195255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0);
17196255736Sdavidch
17197255736Sdavidch    if (CHIP_IS_E1(sc)) {
17198255736Sdavidch        /* read NIG statistic to see if this is our first up since powerup */
17199255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17200255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
17201255736Sdavidch
17202255736Sdavidch        /* do internal memory self test */
17203255736Sdavidch        if ((val == 0) && bxe_int_mem_test(sc)) {
17204295830Sdavidcs            BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17205255736Sdavidch            return (-1);
17206255736Sdavidch        }
17207255736Sdavidch    }
17208255736Sdavidch
17209255736Sdavidch    bxe_setup_fan_failure_detection(sc);
17210255736Sdavidch
17211255736Sdavidch    /* clear PXP2 attentions */
17212255736Sdavidch    REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17213255736Sdavidch
17214255736Sdavidch    bxe_enable_blocks_attention(sc);
17215255736Sdavidch
17216255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17217255736Sdavidch        ecore_enable_blocks_parity(sc);
17218255736Sdavidch    }
17219255736Sdavidch
17220255736Sdavidch    if (!BXE_NOMCP(sc)) {
17221255736Sdavidch        if (CHIP_IS_E1x(sc)) {
17222255736Sdavidch            bxe_common_init_phy(sc);
17223255736Sdavidch        }
17224255736Sdavidch    }
17225255736Sdavidch
17226255736Sdavidch    return (0);
17227255736Sdavidch}
17228255736Sdavidch
17229255736Sdavidch/**
17230255736Sdavidch * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17231255736Sdavidch *
17232255736Sdavidch * @sc:     driver handle
17233255736Sdavidch */
17234255736Sdavidchstatic int
17235255736Sdavidchbxe_init_hw_common_chip(struct bxe_softc *sc)
17236255736Sdavidch{
17237255736Sdavidch    int rc = bxe_init_hw_common(sc);
17238255736Sdavidch
17239255736Sdavidch    if (rc) {
17240295830Sdavidcs        BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17241255736Sdavidch        return (rc);
17242255736Sdavidch    }
17243255736Sdavidch
17244255736Sdavidch    /* In E2 2-PORT mode, same ext phy is used for the two paths */
17245255736Sdavidch    if (!BXE_NOMCP(sc)) {
17246255736Sdavidch        bxe_common_init_phy(sc);
17247255736Sdavidch    }
17248255736Sdavidch
17249255736Sdavidch    return (0);
17250255736Sdavidch}
17251255736Sdavidch
17252255736Sdavidchstatic int
17253255736Sdavidchbxe_init_hw_port(struct bxe_softc *sc)
17254255736Sdavidch{
17255255736Sdavidch    int port = SC_PORT(sc);
17256255736Sdavidch    int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17257255736Sdavidch    uint32_t low, high;
17258255736Sdavidch    uint32_t val;
17259255736Sdavidch
17260255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17261255736Sdavidch
17262255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17263255736Sdavidch
17264255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17265255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17266255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17267255736Sdavidch
17268255736Sdavidch    /*
17269255736Sdavidch     * Timers bug workaround: disables the pf_master bit in pglue at
17270255736Sdavidch     * common phase, we need to enable it here before any dmae access are
17271255736Sdavidch     * attempted. Therefore we manually added the enable-master to the
17272255736Sdavidch     * port phase (it also happens in the function phase)
17273255736Sdavidch     */
17274255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17275255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17276255736Sdavidch    }
17277255736Sdavidch
17278255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17279255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17280255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17281255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17282255736Sdavidch
17283255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17284255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17285255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17286255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17287255736Sdavidch
17288255736Sdavidch    /* QM cid (connection) count */
17289255736Sdavidch    ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17290255736Sdavidch
17291255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17292255736Sdavidch        ecore_init_block(sc, BLOCK_TM, init_phase);
17293255736Sdavidch        REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17294255736Sdavidch        REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17295255736Sdavidch    }
17296255736Sdavidch
17297255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17298255736Sdavidch
17299255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17300255736Sdavidch
17301255736Sdavidch    if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17302255736Sdavidch        if (IS_MF(sc)) {
17303255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 160 : 246);
17304255736Sdavidch        } else if (sc->mtu > 4096) {
17305255736Sdavidch            if (BXE_ONE_PORT(sc)) {
17306255736Sdavidch                low = 160;
17307255736Sdavidch            } else {
17308255736Sdavidch                val = sc->mtu;
17309255736Sdavidch                /* (24*1024 + val*4)/256 */
17310255736Sdavidch                low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17311255736Sdavidch            }
17312255736Sdavidch        } else {
17313255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 80 : 160);
17314255736Sdavidch        }
17315255736Sdavidch        high = (low + 56); /* 14*1024/256 */
17316255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17317255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17318255736Sdavidch    }
17319255736Sdavidch
17320255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
17321255736Sdavidch        REG_WR(sc, SC_PORT(sc) ?
17322255736Sdavidch               BRB1_REG_MAC_GUARANTIED_1 :
17323255736Sdavidch               BRB1_REG_MAC_GUARANTIED_0, 40);
17324255736Sdavidch    }
17325255736Sdavidch
17326255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17327255736Sdavidch    if (CHIP_IS_E3B0(sc)) {
17328255736Sdavidch        if (IS_MF_AFEX(sc)) {
17329255736Sdavidch            /* configure headers for AFEX mode */
17330255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17331255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17332255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17333255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17334255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17335255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17336255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17337255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17338255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17339255736Sdavidch        } else {
17340255736Sdavidch            /* Ovlan exists only if we are in multi-function +
17341255736Sdavidch             * switch-dependent mode, in switch-independent there
17342255736Sdavidch             * is no ovlan headers
17343255736Sdavidch             */
17344255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17345255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17346255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17347255736Sdavidch                   (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17348255736Sdavidch        }
17349255736Sdavidch    }
17350255736Sdavidch
17351255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17352255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17353255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17354255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17355255736Sdavidch
17356255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17357255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17358255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17359255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17360255736Sdavidch
17361255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17362255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17363255736Sdavidch
17364255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17365255736Sdavidch
17366255736Sdavidch    if (CHIP_IS_E1x(sc)) {
17367255736Sdavidch        /* configure PBF to work without PAUSE mtu 9000 */
17368255736Sdavidch        REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17369255736Sdavidch
17370255736Sdavidch        /* update threshold */
17371255736Sdavidch        REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17372255736Sdavidch        /* update init credit */
17373255736Sdavidch        REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17374255736Sdavidch
17375255736Sdavidch        /* probe changes */
17376255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17377255736Sdavidch        DELAY(50);
17378255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17379255736Sdavidch    }
17380255736Sdavidch
17381255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17382255736Sdavidch        ecore_init_block(sc, BLOCK_SRC, init_phase);
17383255736Sdavidch    }
17384255736Sdavidch
17385255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17386255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17387255736Sdavidch
17388255736Sdavidch    if (CHIP_IS_E1(sc)) {
17389255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17390255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17391255736Sdavidch    }
17392255736Sdavidch    ecore_init_block(sc, BLOCK_HC, init_phase);
17393255736Sdavidch
17394255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, init_phase);
17395255736Sdavidch
17396255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17397255736Sdavidch    /* init aeu_mask_attn_func_0/1:
17398255736Sdavidch     *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17399255736Sdavidch     *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17400255736Sdavidch     *             bits 4-7 are used for "per vn group attention" */
17401255736Sdavidch    val = IS_MF(sc) ? 0xF7 : 0x7;
17402255736Sdavidch    /* Enable DCBX attention for all but E1 */
17403255736Sdavidch    val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17404255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17405255736Sdavidch
17406255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17407255736Sdavidch
17408255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17409255736Sdavidch        /* Bit-map indicating which L2 hdrs may appear after the
17410255736Sdavidch         * basic Ethernet header
17411255736Sdavidch         */
17412255736Sdavidch        if (IS_MF_AFEX(sc)) {
17413255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17414255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17415255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17416255736Sdavidch        } else {
17417255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17418255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17419255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC,
17420255736Sdavidch                   IS_MF_SD(sc) ? 7 : 6);
17421255736Sdavidch        }
17422255736Sdavidch
17423255736Sdavidch        if (CHIP_IS_E3(sc)) {
17424255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17425255736Sdavidch                   NIG_REG_LLH1_MF_MODE :
17426255736Sdavidch                   NIG_REG_LLH_MF_MODE, IS_MF(sc));
17427255736Sdavidch        }
17428255736Sdavidch    }
17429255736Sdavidch    if (!CHIP_IS_E3(sc)) {
17430255736Sdavidch        REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17431255736Sdavidch    }
17432255736Sdavidch
17433255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17434255736Sdavidch        /* 0x2 disable mf_ov, 0x1 enable */
17435255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17436255736Sdavidch               (IS_MF_SD(sc) ? 0x1 : 0x2));
17437255736Sdavidch
17438255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17439255736Sdavidch            val = 0;
17440255736Sdavidch            switch (sc->devinfo.mf_info.mf_mode) {
17441255736Sdavidch            case MULTI_FUNCTION_SD:
17442255736Sdavidch                val = 1;
17443255736Sdavidch                break;
17444255736Sdavidch            case MULTI_FUNCTION_SI:
17445255736Sdavidch            case MULTI_FUNCTION_AFEX:
17446255736Sdavidch                val = 2;
17447255736Sdavidch                break;
17448255736Sdavidch            }
17449255736Sdavidch
17450255736Sdavidch            REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17451255736Sdavidch                        NIG_REG_LLH0_CLS_TYPE), val);
17452255736Sdavidch        }
17453255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17454255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17455255736Sdavidch        REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17456255736Sdavidch    }
17457255736Sdavidch
17458255736Sdavidch    /* If SPIO5 is set to generate interrupts, enable it for this port */
17459255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17460255736Sdavidch    if (val & MISC_SPIO_SPIO5) {
17461255736Sdavidch        uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17462255736Sdavidch                                    MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17463255736Sdavidch        val = REG_RD(sc, reg_addr);
17464255736Sdavidch        val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17465255736Sdavidch        REG_WR(sc, reg_addr, val);
17466255736Sdavidch    }
17467255736Sdavidch
17468255736Sdavidch    return (0);
17469255736Sdavidch}
17470255736Sdavidch
17471255736Sdavidchstatic uint32_t
17472255736Sdavidchbxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17473255736Sdavidch                       uint32_t         reg,
17474255736Sdavidch                       uint32_t         expected,
17475255736Sdavidch                       uint32_t         poll_count)
17476255736Sdavidch{
17477255736Sdavidch    uint32_t cur_cnt = poll_count;
17478255736Sdavidch    uint32_t val;
17479255736Sdavidch
17480255736Sdavidch    while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17481255736Sdavidch        DELAY(FLR_WAIT_INTERVAL);
17482255736Sdavidch    }
17483255736Sdavidch
17484255736Sdavidch    return (val);
17485255736Sdavidch}
17486255736Sdavidch
17487255736Sdavidchstatic int
17488255736Sdavidchbxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17489255736Sdavidch                              uint32_t         reg,
17490255736Sdavidch                              char             *msg,
17491255736Sdavidch                              uint32_t         poll_cnt)
17492255736Sdavidch{
17493255736Sdavidch    uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17494255736Sdavidch
17495255736Sdavidch    if (val != 0) {
17496255736Sdavidch        BLOGE(sc, "%s usage count=%d\n", msg, val);
17497255736Sdavidch        return (1);
17498255736Sdavidch    }
17499255736Sdavidch
17500255736Sdavidch    return (0);
17501255736Sdavidch}
17502255736Sdavidch
17503255736Sdavidch/* Common routines with VF FLR cleanup */
17504255736Sdavidchstatic uint32_t
17505255736Sdavidchbxe_flr_clnup_poll_count(struct bxe_softc *sc)
17506255736Sdavidch{
17507255736Sdavidch    /* adjust polling timeout */
17508255736Sdavidch    if (CHIP_REV_IS_EMUL(sc)) {
17509255736Sdavidch        return (FLR_POLL_CNT * 2000);
17510255736Sdavidch    }
17511255736Sdavidch
17512255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
17513255736Sdavidch        return (FLR_POLL_CNT * 120);
17514255736Sdavidch    }
17515255736Sdavidch
17516255736Sdavidch    return (FLR_POLL_CNT);
17517255736Sdavidch}
17518255736Sdavidch
17519255736Sdavidchstatic int
17520255736Sdavidchbxe_poll_hw_usage_counters(struct bxe_softc *sc,
17521255736Sdavidch                           uint32_t         poll_cnt)
17522255736Sdavidch{
17523255736Sdavidch    /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17524255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17525255736Sdavidch                                      CFC_REG_NUM_LCIDS_INSIDE_PF,
17526255736Sdavidch                                      "CFC PF usage counter timed out",
17527255736Sdavidch                                      poll_cnt)) {
17528255736Sdavidch        return (1);
17529255736Sdavidch    }
17530255736Sdavidch
17531255736Sdavidch    /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17532255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17533255736Sdavidch                                      DORQ_REG_PF_USAGE_CNT,
17534255736Sdavidch                                      "DQ PF usage counter timed out",
17535255736Sdavidch                                      poll_cnt)) {
17536255736Sdavidch        return (1);
17537255736Sdavidch    }
17538255736Sdavidch
17539255736Sdavidch    /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17540255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17541255736Sdavidch                                      QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17542255736Sdavidch                                      "QM PF usage counter timed out",
17543255736Sdavidch                                      poll_cnt)) {
17544255736Sdavidch        return (1);
17545255736Sdavidch    }
17546255736Sdavidch
17547255736Sdavidch    /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17548255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17549255736Sdavidch                                      TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17550255736Sdavidch                                      "Timers VNIC usage counter timed out",
17551255736Sdavidch                                      poll_cnt)) {
17552255736Sdavidch        return (1);
17553255736Sdavidch    }
17554255736Sdavidch
17555255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17556255736Sdavidch                                      TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17557255736Sdavidch                                      "Timers NUM_SCANS usage counter timed out",
17558255736Sdavidch                                      poll_cnt)) {
17559255736Sdavidch        return (1);
17560255736Sdavidch    }
17561255736Sdavidch
17562255736Sdavidch    /* Wait DMAE PF usage counter to zero */
17563255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17564255736Sdavidch                                      dmae_reg_go_c[INIT_DMAE_C(sc)],
17565255736Sdavidch                                      "DMAE dommand register timed out",
17566255736Sdavidch                                      poll_cnt)) {
17567255736Sdavidch        return (1);
17568255736Sdavidch    }
17569255736Sdavidch
17570255736Sdavidch    return (0);
17571255736Sdavidch}
17572255736Sdavidch
17573255736Sdavidch#define OP_GEN_PARAM(param)                                            \
17574255736Sdavidch    (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17575255736Sdavidch#define OP_GEN_TYPE(type)                                           \
17576255736Sdavidch    (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17577255736Sdavidch#define OP_GEN_AGG_VECT(index)                                             \
17578255736Sdavidch    (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17579255736Sdavidch
17580255736Sdavidchstatic int
17581255736Sdavidchbxe_send_final_clnup(struct bxe_softc *sc,
17582255736Sdavidch                     uint8_t          clnup_func,
17583255736Sdavidch                     uint32_t         poll_cnt)
17584255736Sdavidch{
17585255736Sdavidch    uint32_t op_gen_command = 0;
17586255736Sdavidch    uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17587255736Sdavidch                          CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17588255736Sdavidch    int ret = 0;
17589255736Sdavidch
17590255736Sdavidch    if (REG_RD(sc, comp_addr)) {
17591255736Sdavidch        BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17592255736Sdavidch        return (1);
17593255736Sdavidch    }
17594255736Sdavidch
17595255736Sdavidch    op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17596255736Sdavidch    op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17597255736Sdavidch    op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17598255736Sdavidch    op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17599255736Sdavidch
17600255736Sdavidch    BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17601255736Sdavidch    REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17602255736Sdavidch
17603255736Sdavidch    if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17604255736Sdavidch        BLOGE(sc, "FW final cleanup did not succeed\n");
17605255736Sdavidch        BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17606255736Sdavidch              (REG_RD(sc, comp_addr)));
17607255736Sdavidch        bxe_panic(sc, ("FLR cleanup failed\n"));
17608255736Sdavidch        return (1);
17609255736Sdavidch    }
17610255736Sdavidch
17611255736Sdavidch    /* Zero completion for nxt FLR */
17612255736Sdavidch    REG_WR(sc, comp_addr, 0);
17613255736Sdavidch
17614255736Sdavidch    return (ret);
17615255736Sdavidch}
17616255736Sdavidch
17617255736Sdavidchstatic void
17618255736Sdavidchbxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17619255736Sdavidch                       struct pbf_pN_buf_regs *regs,
17620255736Sdavidch                       uint32_t               poll_count)
17621255736Sdavidch{
17622255736Sdavidch    uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17623255736Sdavidch    uint32_t cur_cnt = poll_count;
17624255736Sdavidch
17625255736Sdavidch    crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
17626255736Sdavidch    crd = crd_start = REG_RD(sc, regs->crd);
17627255736Sdavidch    init_crd = REG_RD(sc, regs->init_crd);
17628255736Sdavidch
17629255736Sdavidch    BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
17630255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
17631255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
17632255736Sdavidch
17633255736Sdavidch    while ((crd != init_crd) &&
17634255736Sdavidch           ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
17635255736Sdavidch            (init_crd - crd_start))) {
17636255736Sdavidch        if (cur_cnt--) {
17637255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
17638255736Sdavidch            crd = REG_RD(sc, regs->crd);
17639255736Sdavidch            crd_freed = REG_RD(sc, regs->crd_freed);
17640255736Sdavidch        } else {
17641255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
17642255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
17643255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
17644255736Sdavidch            break;
17645255736Sdavidch        }
17646255736Sdavidch    }
17647255736Sdavidch
17648255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
17649255736Sdavidch          poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17650255736Sdavidch}
17651255736Sdavidch
17652255736Sdavidchstatic void
17653255736Sdavidchbxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
17654255736Sdavidch                       struct pbf_pN_cmd_regs *regs,
17655255736Sdavidch                       uint32_t               poll_count)
17656255736Sdavidch{
17657255736Sdavidch    uint32_t occup, to_free, freed, freed_start;
17658255736Sdavidch    uint32_t cur_cnt = poll_count;
17659255736Sdavidch
17660255736Sdavidch    occup = to_free = REG_RD(sc, regs->lines_occup);
17661255736Sdavidch    freed = freed_start = REG_RD(sc, regs->lines_freed);
17662255736Sdavidch
17663255736Sdavidch    BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17664255736Sdavidch    BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17665255736Sdavidch
17666255736Sdavidch    while (occup &&
17667255736Sdavidch           ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
17668255736Sdavidch        if (cur_cnt--) {
17669255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
17670255736Sdavidch            occup = REG_RD(sc, regs->lines_occup);
17671255736Sdavidch            freed = REG_RD(sc, regs->lines_freed);
17672255736Sdavidch        } else {
17673255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
17674255736Sdavidch            BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17675255736Sdavidch            BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17676255736Sdavidch            break;
17677255736Sdavidch        }
17678255736Sdavidch    }
17679255736Sdavidch
17680255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
17681255736Sdavidch          poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17682255736Sdavidch}
17683255736Sdavidch
17684255736Sdavidchstatic void
17685255736Sdavidchbxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
17686255736Sdavidch{
17687255736Sdavidch    struct pbf_pN_cmd_regs cmd_regs[] = {
17688255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
17689255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q0 :
17690255736Sdavidch            PBF_REG_P0_TQ_OCCUPANCY,
17691255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17692255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q0 :
17693255736Sdavidch            PBF_REG_P0_TQ_LINES_FREED_CNT},
17694255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
17695255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q1 :
17696255736Sdavidch            PBF_REG_P1_TQ_OCCUPANCY,
17697255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17698255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q1 :
17699255736Sdavidch            PBF_REG_P1_TQ_LINES_FREED_CNT},
17700255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
17701255736Sdavidch            PBF_REG_TQ_OCCUPANCY_LB_Q :
17702255736Sdavidch            PBF_REG_P4_TQ_OCCUPANCY,
17703255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17704255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
17705255736Sdavidch            PBF_REG_P4_TQ_LINES_FREED_CNT}
17706255736Sdavidch    };
17707255736Sdavidch
17708255736Sdavidch    struct pbf_pN_buf_regs buf_regs[] = {
17709255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
17710255736Sdavidch            PBF_REG_INIT_CRD_Q0 :
17711255736Sdavidch            PBF_REG_P0_INIT_CRD ,
17712255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17713255736Sdavidch            PBF_REG_CREDIT_Q0 :
17714255736Sdavidch            PBF_REG_P0_CREDIT,
17715255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17716255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
17717255736Sdavidch            PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
17718255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
17719255736Sdavidch            PBF_REG_INIT_CRD_Q1 :
17720255736Sdavidch            PBF_REG_P1_INIT_CRD,
17721255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17722255736Sdavidch            PBF_REG_CREDIT_Q1 :
17723255736Sdavidch            PBF_REG_P1_CREDIT,
17724255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17725255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
17726255736Sdavidch            PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
17727255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
17728255736Sdavidch            PBF_REG_INIT_CRD_LB_Q :
17729255736Sdavidch            PBF_REG_P4_INIT_CRD,
17730255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17731255736Sdavidch            PBF_REG_CREDIT_LB_Q :
17732255736Sdavidch            PBF_REG_P4_CREDIT,
17733255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17734255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
17735255736Sdavidch            PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
17736255736Sdavidch    };
17737255736Sdavidch
17738255736Sdavidch    int i;
17739255736Sdavidch
17740255736Sdavidch    /* Verify the command queues are flushed P0, P1, P4 */
17741255736Sdavidch    for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
17742255736Sdavidch        bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
17743255736Sdavidch    }
17744255736Sdavidch
17745255736Sdavidch    /* Verify the transmission buffers are flushed P0, P1, P4 */
17746255736Sdavidch    for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
17747255736Sdavidch        bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
17748255736Sdavidch    }
17749255736Sdavidch}
17750255736Sdavidch
17751255736Sdavidchstatic void
17752255736Sdavidchbxe_hw_enable_status(struct bxe_softc *sc)
17753255736Sdavidch{
17754255736Sdavidch    uint32_t val;
17755255736Sdavidch
17756255736Sdavidch    val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
17757255736Sdavidch    BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
17758255736Sdavidch
17759255736Sdavidch    val = REG_RD(sc, PBF_REG_DISABLE_PF);
17760255736Sdavidch    BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
17761255736Sdavidch
17762255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
17763255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
17764255736Sdavidch
17765255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
17766255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
17767255736Sdavidch
17768255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
17769255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
17770255736Sdavidch
17771255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
17772255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
17773255736Sdavidch
17774255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
17775255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
17776255736Sdavidch
17777255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
17778255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
17779255736Sdavidch}
17780255736Sdavidch
17781255736Sdavidchstatic int
17782255736Sdavidchbxe_pf_flr_clnup(struct bxe_softc *sc)
17783255736Sdavidch{
17784255736Sdavidch    uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
17785255736Sdavidch
17786255736Sdavidch    BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
17787255736Sdavidch
17788255736Sdavidch    /* Re-enable PF target read access */
17789255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
17790255736Sdavidch
17791255736Sdavidch    /* Poll HW usage counters */
17792255736Sdavidch    BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
17793255736Sdavidch    if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
17794255736Sdavidch        return (-1);
17795255736Sdavidch    }
17796255736Sdavidch
17797255736Sdavidch    /* Zero the igu 'trailing edge' and 'leading edge' */
17798255736Sdavidch
17799255736Sdavidch    /* Send the FW cleanup command */
17800255736Sdavidch    if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
17801255736Sdavidch        return (-1);
17802255736Sdavidch    }
17803255736Sdavidch
17804255736Sdavidch    /* ATC cleanup */
17805255736Sdavidch
17806255736Sdavidch    /* Verify TX hw is flushed */
17807255736Sdavidch    bxe_tx_hw_flushed(sc, poll_cnt);
17808255736Sdavidch
17809255736Sdavidch    /* Wait 100ms (not adjusted according to platform) */
17810255736Sdavidch    DELAY(100000);
17811255736Sdavidch
17812255736Sdavidch    /* Verify no pending pci transactions */
17813255736Sdavidch    if (bxe_is_pcie_pending(sc)) {
17814255736Sdavidch        BLOGE(sc, "PCIE Transactions still pending\n");
17815255736Sdavidch    }
17816255736Sdavidch
17817255736Sdavidch    /* Debug */
17818255736Sdavidch    bxe_hw_enable_status(sc);
17819255736Sdavidch
17820255736Sdavidch    /*
17821255736Sdavidch     * Master enable - Due to WB DMAE writes performed before this
17822255736Sdavidch     * register is re-initialized as part of the regular function init
17823255736Sdavidch     */
17824255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17825255736Sdavidch
17826255736Sdavidch    return (0);
17827255736Sdavidch}
17828255736Sdavidch
17829255736Sdavidchstatic int
17830255736Sdavidchbxe_init_hw_func(struct bxe_softc *sc)
17831255736Sdavidch{
17832255736Sdavidch    int port = SC_PORT(sc);
17833255736Sdavidch    int func = SC_FUNC(sc);
17834255736Sdavidch    int init_phase = PHASE_PF0 + func;
17835255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
17836255736Sdavidch    uint16_t cdu_ilt_start;
17837255736Sdavidch    uint32_t addr, val;
17838255736Sdavidch    uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
17839255736Sdavidch    int i, main_mem_width, rc;
17840255736Sdavidch
17841255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
17842255736Sdavidch
17843255736Sdavidch    /* FLR cleanup */
17844255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17845255736Sdavidch        rc = bxe_pf_flr_clnup(sc);
17846255736Sdavidch        if (rc) {
17847255736Sdavidch            BLOGE(sc, "FLR cleanup failed!\n");
17848255736Sdavidch            // XXX bxe_fw_dump(sc);
17849255736Sdavidch            // XXX bxe_idle_chk(sc);
17850255736Sdavidch            return (rc);
17851255736Sdavidch        }
17852255736Sdavidch    }
17853255736Sdavidch
17854255736Sdavidch    /* set MSI reconfigure capability */
17855255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
17856255736Sdavidch        addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
17857255736Sdavidch        val = REG_RD(sc, addr);
17858255736Sdavidch        val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
17859255736Sdavidch        REG_WR(sc, addr, val);
17860255736Sdavidch    }
17861255736Sdavidch
17862255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17863255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17864255736Sdavidch
17865255736Sdavidch    ilt = sc->ilt;
17866255736Sdavidch    cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
17867255736Sdavidch
17868255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
17869255736Sdavidch        ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
17870255736Sdavidch        ilt->lines[cdu_ilt_start + i].page_mapping =
17871255736Sdavidch            sc->context[i].vcxt_dma.paddr;
17872255736Sdavidch        ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
17873255736Sdavidch    }
17874255736Sdavidch    ecore_ilt_init_op(sc, INITOP_SET);
17875255736Sdavidch
17876295830Sdavidcs    /* Set NIC mode */
17877295830Sdavidcs    REG_WR(sc, PRS_REG_NIC_MODE, 1);
17878295830Sdavidcs    BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
17879255736Sdavidch
17880255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17881255736Sdavidch        uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
17882255736Sdavidch
17883255736Sdavidch        /* Turn on a single ISR mode in IGU if driver is going to use
17884255736Sdavidch         * INT#x or MSI
17885255736Sdavidch         */
17886255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
17887255736Sdavidch            pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
17888255736Sdavidch        }
17889255736Sdavidch
17890255736Sdavidch        /*
17891255736Sdavidch         * Timers workaround bug: function init part.
17892255736Sdavidch         * Need to wait 20msec after initializing ILT,
17893255736Sdavidch         * needed to make sure there are no requests in
17894255736Sdavidch         * one of the PXP internal queues with "old" ILT addresses
17895255736Sdavidch         */
17896255736Sdavidch        DELAY(20000);
17897255736Sdavidch
17898255736Sdavidch        /*
17899255736Sdavidch         * Master enable - Due to WB DMAE writes performed before this
17900255736Sdavidch         * register is re-initialized as part of the regular function
17901255736Sdavidch         * init
17902255736Sdavidch         */
17903255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17904255736Sdavidch        /* Enable the function in IGU */
17905255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
17906255736Sdavidch    }
17907255736Sdavidch
17908255736Sdavidch    sc->dmae_ready = 1;
17909255736Sdavidch
17910255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17911255736Sdavidch
17912255736Sdavidch    if (!CHIP_IS_E1x(sc))
17913255736Sdavidch        REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
17914255736Sdavidch
17915255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17916255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17917255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17918255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, init_phase);
17919255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17920255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17921255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17922255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17923255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17924255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17925255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17926255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17927255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17928255736Sdavidch
17929255736Sdavidch    if (!CHIP_IS_E1x(sc))
17930255736Sdavidch        REG_WR(sc, QM_REG_PF_EN, 1);
17931255736Sdavidch
17932255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17933255736Sdavidch        REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17934255736Sdavidch        REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17935255736Sdavidch        REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17936255736Sdavidch        REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17937255736Sdavidch    }
17938255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17939255736Sdavidch
17940255736Sdavidch    ecore_init_block(sc, BLOCK_TM, init_phase);
17941255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17942255736Sdavidch
17943255736Sdavidch    bxe_iov_init_dq(sc);
17944255736Sdavidch
17945255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17946255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17947255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17948255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17949255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17950255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17951255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17952255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17953255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17954255736Sdavidch    if (!CHIP_IS_E1x(sc))
17955255736Sdavidch        REG_WR(sc, PBF_REG_DISABLE_PF, 0);
17956255736Sdavidch
17957255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17958255736Sdavidch
17959255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17960255736Sdavidch
17961255736Sdavidch    if (!CHIP_IS_E1x(sc))
17962255736Sdavidch        REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
17963255736Sdavidch
17964255736Sdavidch    if (IS_MF(sc)) {
17965255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
17966255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
17967255736Sdavidch    }
17968255736Sdavidch
17969255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17970255736Sdavidch
17971255736Sdavidch    /* HC init per function */
17972255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
17973255736Sdavidch        if (CHIP_IS_E1H(sc)) {
17974255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17975255736Sdavidch
17976255736Sdavidch            REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17977255736Sdavidch            REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17978255736Sdavidch        }
17979255736Sdavidch        ecore_init_block(sc, BLOCK_HC, init_phase);
17980255736Sdavidch
17981255736Sdavidch    } else {
17982255736Sdavidch        int num_segs, sb_idx, prod_offset;
17983255736Sdavidch
17984255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17985255736Sdavidch
17986255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17987255736Sdavidch            REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
17988255736Sdavidch            REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
17989255736Sdavidch        }
17990255736Sdavidch
17991255736Sdavidch        ecore_init_block(sc, BLOCK_IGU, init_phase);
17992255736Sdavidch
17993255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17994255736Sdavidch            int dsb_idx = 0;
17995255736Sdavidch            /**
17996255736Sdavidch             * Producer memory:
17997255736Sdavidch             * E2 mode: address 0-135 match to the mapping memory;
17998255736Sdavidch             * 136 - PF0 default prod; 137 - PF1 default prod;
17999255736Sdavidch             * 138 - PF2 default prod; 139 - PF3 default prod;
18000255736Sdavidch             * 140 - PF0 attn prod;    141 - PF1 attn prod;
18001255736Sdavidch             * 142 - PF2 attn prod;    143 - PF3 attn prod;
18002255736Sdavidch             * 144-147 reserved.
18003255736Sdavidch             *
18004255736Sdavidch             * E1.5 mode - In backward compatible mode;
18005255736Sdavidch             * for non default SB; each even line in the memory
18006255736Sdavidch             * holds the U producer and each odd line hold
18007255736Sdavidch             * the C producer. The first 128 producers are for
18008255736Sdavidch             * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
18009255736Sdavidch             * producers are for the DSB for each PF.
18010255736Sdavidch             * Each PF has five segments: (the order inside each
18011255736Sdavidch             * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
18012255736Sdavidch             * 132-135 C prods; 136-139 X prods; 140-143 T prods;
18013255736Sdavidch             * 144-147 attn prods;
18014255736Sdavidch             */
18015255736Sdavidch            /* non-default-status-blocks */
18016255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18017255736Sdavidch                IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
18018255736Sdavidch            for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
18019255736Sdavidch                prod_offset = (sc->igu_base_sb + sb_idx) *
18020255736Sdavidch                    num_segs;
18021255736Sdavidch
18022255736Sdavidch                for (i = 0; i < num_segs; i++) {
18023255736Sdavidch                    addr = IGU_REG_PROD_CONS_MEMORY +
18024255736Sdavidch                            (prod_offset + i) * 4;
18025255736Sdavidch                    REG_WR(sc, addr, 0);
18026255736Sdavidch                }
18027255736Sdavidch                /* send consumer update with value 0 */
18028255736Sdavidch                bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
18029255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18030255736Sdavidch                bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
18031255736Sdavidch            }
18032255736Sdavidch
18033255736Sdavidch            /* default-status-blocks */
18034255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18035255736Sdavidch                IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
18036255736Sdavidch
18037255736Sdavidch            if (CHIP_IS_MODE_4_PORT(sc))
18038255736Sdavidch                dsb_idx = SC_FUNC(sc);
18039255736Sdavidch            else
18040255736Sdavidch                dsb_idx = SC_VN(sc);
18041255736Sdavidch
18042255736Sdavidch            prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
18043255736Sdavidch                       IGU_BC_BASE_DSB_PROD + dsb_idx :
18044255736Sdavidch                       IGU_NORM_BASE_DSB_PROD + dsb_idx);
18045255736Sdavidch
18046255736Sdavidch            /*
18047255736Sdavidch             * igu prods come in chunks of E1HVN_MAX (4) -
18048255736Sdavidch             * does not matters what is the current chip mode
18049255736Sdavidch             */
18050255736Sdavidch            for (i = 0; i < (num_segs * E1HVN_MAX);
18051255736Sdavidch                 i += E1HVN_MAX) {
18052255736Sdavidch                addr = IGU_REG_PROD_CONS_MEMORY +
18053255736Sdavidch                            (prod_offset + i)*4;
18054255736Sdavidch                REG_WR(sc, addr, 0);
18055255736Sdavidch            }
18056255736Sdavidch            /* send consumer update with 0 */
18057255736Sdavidch            if (CHIP_INT_MODE_IS_BC(sc)) {
18058255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18059255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18060255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18061255736Sdavidch                           CSTORM_ID, 0, IGU_INT_NOP, 1);
18062255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18063255736Sdavidch                           XSTORM_ID, 0, IGU_INT_NOP, 1);
18064255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18065255736Sdavidch                           TSTORM_ID, 0, IGU_INT_NOP, 1);
18066255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18067255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
18068255736Sdavidch            } else {
18069255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18070255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18071255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18072255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
18073255736Sdavidch            }
18074255736Sdavidch            bxe_igu_clear_sb(sc, sc->igu_dsb_id);
18075255736Sdavidch
18076255736Sdavidch            /* !!! these should become driver const once
18077255736Sdavidch               rf-tool supports split-68 const */
18078255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
18079255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
18080255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
18081255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
18082255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
18083255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
18084255736Sdavidch        }
18085255736Sdavidch    }
18086255736Sdavidch
18087255736Sdavidch    /* Reset PCIE errors for debug */
18088255736Sdavidch    REG_WR(sc, 0x2114, 0xffffffff);
18089255736Sdavidch    REG_WR(sc, 0x2120, 0xffffffff);
18090255736Sdavidch
18091255736Sdavidch    if (CHIP_IS_E1x(sc)) {
18092255736Sdavidch        main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
18093255736Sdavidch        main_mem_base = HC_REG_MAIN_MEMORY +
18094255736Sdavidch                SC_PORT(sc) * (main_mem_size * 4);
18095255736Sdavidch        main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
18096255736Sdavidch        main_mem_width = 8;
18097255736Sdavidch
18098255736Sdavidch        val = REG_RD(sc, main_mem_prty_clr);
18099255736Sdavidch        if (val) {
18100255736Sdavidch            BLOGD(sc, DBG_LOAD,
18101255736Sdavidch                  "Parity errors in HC block during function init (0x%x)!\n",
18102255736Sdavidch                  val);
18103255736Sdavidch        }
18104255736Sdavidch
18105255736Sdavidch        /* Clear "false" parity errors in MSI-X table */
18106255736Sdavidch        for (i = main_mem_base;
18107255736Sdavidch             i < main_mem_base + main_mem_size * 4;
18108255736Sdavidch             i += main_mem_width) {
18109255736Sdavidch            bxe_read_dmae(sc, i, main_mem_width / 4);
18110255736Sdavidch            bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
18111255736Sdavidch                           i, main_mem_width / 4);
18112255736Sdavidch        }
18113255736Sdavidch        /* Clear HC parity attention */
18114255736Sdavidch        REG_RD(sc, main_mem_prty_clr);
18115255736Sdavidch    }
18116255736Sdavidch
18117255736Sdavidch#if 1
18118255736Sdavidch    /* Enable STORMs SP logging */
18119255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM +
18120255736Sdavidch           USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18121255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM +
18122255736Sdavidch           TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18123255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18124255736Sdavidch           CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18125255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM +
18126255736Sdavidch           XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18127255736Sdavidch#endif
18128255736Sdavidch
18129255736Sdavidch    elink_phy_probe(&sc->link_params);
18130255736Sdavidch
18131255736Sdavidch    return (0);
18132255736Sdavidch}
18133255736Sdavidch
18134255736Sdavidchstatic void
18135255736Sdavidchbxe_link_reset(struct bxe_softc *sc)
18136255736Sdavidch{
18137255736Sdavidch    if (!BXE_NOMCP(sc)) {
18138284335Sdavidcs	bxe_acquire_phy_lock(sc);
18139255736Sdavidch        elink_lfa_reset(&sc->link_params, &sc->link_vars);
18140284335Sdavidcs	bxe_release_phy_lock(sc);
18141255736Sdavidch    } else {
18142255736Sdavidch        if (!CHIP_REV_IS_SLOW(sc)) {
18143255736Sdavidch            BLOGW(sc, "Bootcode is missing - cannot reset link\n");
18144255736Sdavidch        }
18145255736Sdavidch    }
18146255736Sdavidch}
18147255736Sdavidch
18148255736Sdavidchstatic void
18149255736Sdavidchbxe_reset_port(struct bxe_softc *sc)
18150255736Sdavidch{
18151255736Sdavidch    int port = SC_PORT(sc);
18152255736Sdavidch    uint32_t val;
18153255736Sdavidch
18154315881Sdavidcs	ELINK_DEBUG_P0(sc, "bxe_reset_port called\n");
18155255736Sdavidch    /* reset physical Link */
18156255736Sdavidch    bxe_link_reset(sc);
18157255736Sdavidch
18158255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18159255736Sdavidch
18160255736Sdavidch    /* Do not rcv packets to BRB */
18161255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18162255736Sdavidch    /* Do not direct rcv packets that are not for MCP to the BRB */
18163255736Sdavidch    REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18164255736Sdavidch               NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18165255736Sdavidch
18166255736Sdavidch    /* Configure AEU */
18167255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18168255736Sdavidch
18169255736Sdavidch    DELAY(100000);
18170255736Sdavidch
18171255736Sdavidch    /* Check for BRB port occupancy */
18172255736Sdavidch    val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18173255736Sdavidch    if (val) {
18174255736Sdavidch        BLOGD(sc, DBG_LOAD,
18175255736Sdavidch              "BRB1 is not empty, %d blocks are occupied\n", val);
18176255736Sdavidch    }
18177255736Sdavidch
18178255736Sdavidch    /* TODO: Close Doorbell port? */
18179255736Sdavidch}
18180255736Sdavidch
18181255736Sdavidchstatic void
18182255736Sdavidchbxe_ilt_wr(struct bxe_softc *sc,
18183255736Sdavidch           uint32_t         index,
18184255736Sdavidch           bus_addr_t       addr)
18185255736Sdavidch{
18186255736Sdavidch    int reg;
18187255736Sdavidch    uint32_t wb_write[2];
18188255736Sdavidch
18189255736Sdavidch    if (CHIP_IS_E1(sc)) {
18190255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18191255736Sdavidch    } else {
18192255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18193255736Sdavidch    }
18194255736Sdavidch
18195255736Sdavidch    wb_write[0] = ONCHIP_ADDR1(addr);
18196255736Sdavidch    wb_write[1] = ONCHIP_ADDR2(addr);
18197255736Sdavidch    REG_WR_DMAE(sc, reg, wb_write, 2);
18198255736Sdavidch}
18199255736Sdavidch
18200255736Sdavidchstatic void
18201255736Sdavidchbxe_clear_func_ilt(struct bxe_softc *sc,
18202255736Sdavidch                   uint32_t         func)
18203255736Sdavidch{
18204255736Sdavidch    uint32_t i, base = FUNC_ILT_BASE(func);
18205255736Sdavidch    for (i = base; i < base + ILT_PER_FUNC; i++) {
18206255736Sdavidch        bxe_ilt_wr(sc, i, 0);
18207255736Sdavidch    }
18208255736Sdavidch}
18209255736Sdavidch
18210255736Sdavidchstatic void
18211255736Sdavidchbxe_reset_func(struct bxe_softc *sc)
18212255736Sdavidch{
18213255736Sdavidch    struct bxe_fastpath *fp;
18214255736Sdavidch    int port = SC_PORT(sc);
18215255736Sdavidch    int func = SC_FUNC(sc);
18216255736Sdavidch    int i;
18217255736Sdavidch
18218255736Sdavidch    /* Disable the function in the FW */
18219255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18220255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18221255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18222255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18223255736Sdavidch
18224255736Sdavidch    /* FP SBs */
18225255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
18226255736Sdavidch        fp = &sc->fp[i];
18227255736Sdavidch        REG_WR8(sc, BAR_CSTRORM_INTMEM +
18228255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18229255736Sdavidch                SB_DISABLED);
18230255736Sdavidch    }
18231255736Sdavidch
18232255736Sdavidch    /* SP SB */
18233255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18234255736Sdavidch            CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18235255736Sdavidch            SB_DISABLED);
18236255736Sdavidch
18237255736Sdavidch    for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18238255736Sdavidch        REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18239255736Sdavidch    }
18240255736Sdavidch
18241255736Sdavidch    /* Configure IGU */
18242255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18243255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18244255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18245255736Sdavidch    } else {
18246255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18247255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18248255736Sdavidch    }
18249255736Sdavidch
18250255736Sdavidch    if (CNIC_LOADED(sc)) {
18251255736Sdavidch        /* Disable Timer scan */
18252255736Sdavidch        REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18253255736Sdavidch        /*
18254255736Sdavidch         * Wait for at least 10ms and up to 2 second for the timers
18255255736Sdavidch         * scan to complete
18256255736Sdavidch         */
18257255736Sdavidch        for (i = 0; i < 200; i++) {
18258255736Sdavidch            DELAY(10000);
18259255736Sdavidch            if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18260255736Sdavidch                break;
18261255736Sdavidch        }
18262255736Sdavidch    }
18263255736Sdavidch
18264255736Sdavidch    /* Clear ILT */
18265255736Sdavidch    bxe_clear_func_ilt(sc, func);
18266255736Sdavidch
18267255736Sdavidch    /*
18268255736Sdavidch     * Timers workaround bug for E2: if this is vnic-3,
18269255736Sdavidch     * we need to set the entire ilt range for this timers.
18270255736Sdavidch     */
18271255736Sdavidch    if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18272255736Sdavidch        struct ilt_client_info ilt_cli;
18273255736Sdavidch        /* use dummy TM client */
18274255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18275255736Sdavidch        ilt_cli.start = 0;
18276255736Sdavidch        ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18277255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
18278255736Sdavidch
18279255736Sdavidch        ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18280255736Sdavidch    }
18281255736Sdavidch
18282255736Sdavidch    /* this assumes that reset_port() called before reset_func()*/
18283255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18284255736Sdavidch        bxe_pf_disable(sc);
18285255736Sdavidch    }
18286255736Sdavidch
18287255736Sdavidch    sc->dmae_ready = 0;
18288255736Sdavidch}
18289255736Sdavidch
18290255736Sdavidchstatic int
18291255736Sdavidchbxe_gunzip_init(struct bxe_softc *sc)
18292255736Sdavidch{
18293255736Sdavidch    return (0);
18294255736Sdavidch}
18295255736Sdavidch
18296255736Sdavidchstatic void
18297255736Sdavidchbxe_gunzip_end(struct bxe_softc *sc)
18298255736Sdavidch{
18299255736Sdavidch    return;
18300255736Sdavidch}
18301255736Sdavidch
18302255736Sdavidchstatic int
18303255736Sdavidchbxe_init_firmware(struct bxe_softc *sc)
18304255736Sdavidch{
18305255736Sdavidch    if (CHIP_IS_E1(sc)) {
18306255736Sdavidch        ecore_init_e1_firmware(sc);
18307255736Sdavidch        sc->iro_array = e1_iro_arr;
18308255736Sdavidch    } else if (CHIP_IS_E1H(sc)) {
18309255736Sdavidch        ecore_init_e1h_firmware(sc);
18310255736Sdavidch        sc->iro_array = e1h_iro_arr;
18311255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
18312255736Sdavidch        ecore_init_e2_firmware(sc);
18313255736Sdavidch        sc->iro_array = e2_iro_arr;
18314255736Sdavidch    } else {
18315255736Sdavidch        BLOGE(sc, "Unsupported chip revision\n");
18316255736Sdavidch        return (-1);
18317255736Sdavidch    }
18318255736Sdavidch
18319255736Sdavidch    return (0);
18320255736Sdavidch}
18321255736Sdavidch
18322255736Sdavidchstatic void
18323255736Sdavidchbxe_release_firmware(struct bxe_softc *sc)
18324255736Sdavidch{
18325255736Sdavidch    /* Do nothing */
18326255736Sdavidch    return;
18327255736Sdavidch}
18328255736Sdavidch
18329255736Sdavidchstatic int
18330255736Sdavidchecore_gunzip(struct bxe_softc *sc,
18331255736Sdavidch             const uint8_t    *zbuf,
18332255736Sdavidch             int              len)
18333255736Sdavidch{
18334255736Sdavidch    /* XXX : Implement... */
18335255736Sdavidch    BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18336255736Sdavidch    return (FALSE);
18337255736Sdavidch}
18338255736Sdavidch
18339255736Sdavidchstatic void
18340255736Sdavidchecore_reg_wr_ind(struct bxe_softc *sc,
18341255736Sdavidch                 uint32_t         addr,
18342255736Sdavidch                 uint32_t         val)
18343255736Sdavidch{
18344255736Sdavidch    bxe_reg_wr_ind(sc, addr, val);
18345255736Sdavidch}
18346255736Sdavidch
18347255736Sdavidchstatic void
18348255736Sdavidchecore_write_dmae_phys_len(struct bxe_softc *sc,
18349255736Sdavidch                          bus_addr_t       phys_addr,
18350255736Sdavidch                          uint32_t         addr,
18351255736Sdavidch                          uint32_t         len)
18352255736Sdavidch{
18353255736Sdavidch    bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18354255736Sdavidch}
18355255736Sdavidch
18356255736Sdavidchvoid
18357255736Sdavidchecore_storm_memset_struct(struct bxe_softc *sc,
18358255736Sdavidch                          uint32_t         addr,
18359255736Sdavidch                          size_t           size,
18360255736Sdavidch                          uint32_t         *data)
18361255736Sdavidch{
18362255736Sdavidch    uint8_t i;
18363255736Sdavidch    for (i = 0; i < size/4; i++) {
18364255736Sdavidch        REG_WR(sc, addr + (i * 4), data[i]);
18365255736Sdavidch    }
18366255736Sdavidch}
18367255736Sdavidch
18368292639Sdavidcs
18369292639Sdavidcs/*
18370292639Sdavidcs * character device - ioctl interface definitions
18371292639Sdavidcs */
18372292639Sdavidcs
18373292639Sdavidcs
18374292639Sdavidcs#include "bxe_dump.h"
18375292639Sdavidcs#include "bxe_ioctl.h"
18376292639Sdavidcs#include <sys/conf.h>
18377292639Sdavidcs
18378292639Sdavidcsstatic int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18379292639Sdavidcs                struct thread *td);
18380292639Sdavidcs
18381292639Sdavidcsstatic struct cdevsw bxe_cdevsw = {
18382292639Sdavidcs    .d_version = D_VERSION,
18383292639Sdavidcs    .d_ioctl = bxe_eioctl,
18384292639Sdavidcs    .d_name = "bxecnic",
18385292639Sdavidcs};
18386292639Sdavidcs
18387292639Sdavidcs#define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18388292639Sdavidcs
18389292639Sdavidcs
18390292639Sdavidcs#define DUMP_ALL_PRESETS        0x1FFF
18391292639Sdavidcs#define DUMP_MAX_PRESETS        13
18392292639Sdavidcs#define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18393292639Sdavidcs#define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18394292639Sdavidcs#define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18395292639Sdavidcs#define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18396292639Sdavidcs#define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18397292639Sdavidcs
18398292639Sdavidcs#define IS_REG_IN_PRESET(presets, idx)  \
18399292639Sdavidcs                ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18400292639Sdavidcs
18401292639Sdavidcs
18402292639Sdavidcsstatic int
18403292639Sdavidcsbxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18404292639Sdavidcs{
18405292639Sdavidcs    if (CHIP_IS_E1(sc))
18406292639Sdavidcs        return dump_num_registers[0][preset-1];
18407292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18408292639Sdavidcs        return dump_num_registers[1][preset-1];
18409292639Sdavidcs    else if (CHIP_IS_E2(sc))
18410292639Sdavidcs        return dump_num_registers[2][preset-1];
18411292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18412292639Sdavidcs        return dump_num_registers[3][preset-1];
18413292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18414292639Sdavidcs        return dump_num_registers[4][preset-1];
18415292639Sdavidcs    else
18416292639Sdavidcs        return 0;
18417292639Sdavidcs}
18418292639Sdavidcs
18419292639Sdavidcsstatic int
18420292639Sdavidcsbxe_get_total_regs_len32(struct bxe_softc *sc)
18421292639Sdavidcs{
18422292639Sdavidcs    uint32_t preset_idx;
18423292639Sdavidcs    int regdump_len32 = 0;
18424292639Sdavidcs
18425292639Sdavidcs
18426292639Sdavidcs    /* Calculate the total preset regs length */
18427292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18428292639Sdavidcs        regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18429292639Sdavidcs    }
18430292639Sdavidcs
18431292639Sdavidcs    return regdump_len32;
18432292639Sdavidcs}
18433292639Sdavidcs
18434292639Sdavidcsstatic const uint32_t *
18435292639Sdavidcs__bxe_get_page_addr_ar(struct bxe_softc *sc)
18436292639Sdavidcs{
18437292639Sdavidcs    if (CHIP_IS_E2(sc))
18438292639Sdavidcs        return page_vals_e2;
18439292639Sdavidcs    else if (CHIP_IS_E3(sc))
18440292639Sdavidcs        return page_vals_e3;
18441292639Sdavidcs    else
18442292639Sdavidcs        return NULL;
18443292639Sdavidcs}
18444292639Sdavidcs
18445292639Sdavidcsstatic uint32_t
18446292639Sdavidcs__bxe_get_page_reg_num(struct bxe_softc *sc)
18447292639Sdavidcs{
18448292639Sdavidcs    if (CHIP_IS_E2(sc))
18449292639Sdavidcs        return PAGE_MODE_VALUES_E2;
18450292639Sdavidcs    else if (CHIP_IS_E3(sc))
18451292639Sdavidcs        return PAGE_MODE_VALUES_E3;
18452292639Sdavidcs    else
18453292639Sdavidcs        return 0;
18454292639Sdavidcs}
18455292639Sdavidcs
18456292639Sdavidcsstatic const uint32_t *
18457292639Sdavidcs__bxe_get_page_write_ar(struct bxe_softc *sc)
18458292639Sdavidcs{
18459292639Sdavidcs    if (CHIP_IS_E2(sc))
18460292639Sdavidcs        return page_write_regs_e2;
18461292639Sdavidcs    else if (CHIP_IS_E3(sc))
18462292639Sdavidcs        return page_write_regs_e3;
18463292639Sdavidcs    else
18464292639Sdavidcs        return NULL;
18465292639Sdavidcs}
18466292639Sdavidcs
18467292639Sdavidcsstatic uint32_t
18468292639Sdavidcs__bxe_get_page_write_num(struct bxe_softc *sc)
18469292639Sdavidcs{
18470292639Sdavidcs    if (CHIP_IS_E2(sc))
18471292639Sdavidcs        return PAGE_WRITE_REGS_E2;
18472292639Sdavidcs    else if (CHIP_IS_E3(sc))
18473292639Sdavidcs        return PAGE_WRITE_REGS_E3;
18474292639Sdavidcs    else
18475292639Sdavidcs        return 0;
18476292639Sdavidcs}
18477292639Sdavidcs
18478292639Sdavidcsstatic const struct reg_addr *
18479292639Sdavidcs__bxe_get_page_read_ar(struct bxe_softc *sc)
18480292639Sdavidcs{
18481292639Sdavidcs    if (CHIP_IS_E2(sc))
18482292639Sdavidcs        return page_read_regs_e2;
18483292639Sdavidcs    else if (CHIP_IS_E3(sc))
18484292639Sdavidcs        return page_read_regs_e3;
18485292639Sdavidcs    else
18486292639Sdavidcs        return NULL;
18487292639Sdavidcs}
18488292639Sdavidcs
18489292639Sdavidcsstatic uint32_t
18490292639Sdavidcs__bxe_get_page_read_num(struct bxe_softc *sc)
18491292639Sdavidcs{
18492292639Sdavidcs    if (CHIP_IS_E2(sc))
18493292639Sdavidcs        return PAGE_READ_REGS_E2;
18494292639Sdavidcs    else if (CHIP_IS_E3(sc))
18495292639Sdavidcs        return PAGE_READ_REGS_E3;
18496292639Sdavidcs    else
18497292639Sdavidcs        return 0;
18498292639Sdavidcs}
18499292639Sdavidcs
18500292639Sdavidcsstatic bool
18501292639Sdavidcsbxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18502292639Sdavidcs{
18503292639Sdavidcs    if (CHIP_IS_E1(sc))
18504292639Sdavidcs        return IS_E1_REG(reg_info->chips);
18505292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18506292639Sdavidcs        return IS_E1H_REG(reg_info->chips);
18507292639Sdavidcs    else if (CHIP_IS_E2(sc))
18508292639Sdavidcs        return IS_E2_REG(reg_info->chips);
18509292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18510292639Sdavidcs        return IS_E3A0_REG(reg_info->chips);
18511292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18512292639Sdavidcs        return IS_E3B0_REG(reg_info->chips);
18513292639Sdavidcs    else
18514292639Sdavidcs        return 0;
18515292639Sdavidcs}
18516292639Sdavidcs
18517292639Sdavidcsstatic bool
18518292639Sdavidcsbxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18519292639Sdavidcs{
18520292639Sdavidcs    if (CHIP_IS_E1(sc))
18521292639Sdavidcs        return IS_E1_REG(wreg_info->chips);
18522292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18523292639Sdavidcs        return IS_E1H_REG(wreg_info->chips);
18524292639Sdavidcs    else if (CHIP_IS_E2(sc))
18525292639Sdavidcs        return IS_E2_REG(wreg_info->chips);
18526292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18527292639Sdavidcs        return IS_E3A0_REG(wreg_info->chips);
18528292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18529292639Sdavidcs        return IS_E3B0_REG(wreg_info->chips);
18530292639Sdavidcs    else
18531292639Sdavidcs        return 0;
18532292639Sdavidcs}
18533292639Sdavidcs
18534292639Sdavidcs/**
18535292639Sdavidcs * bxe_read_pages_regs - read "paged" registers
18536292639Sdavidcs *
18537292639Sdavidcs * @bp          device handle
18538292639Sdavidcs * @p           output buffer
18539292639Sdavidcs *
18540292639Sdavidcs * Reads "paged" memories: memories that may only be read by first writing to a
18541292639Sdavidcs * specific address ("write address") and then reading from a specific address
18542292639Sdavidcs * ("read address"). There may be more than one write address per "page" and
18543292639Sdavidcs * more than one read address per write address.
18544292639Sdavidcs */
18545292639Sdavidcsstatic void
18546292639Sdavidcsbxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18547292639Sdavidcs{
18548292639Sdavidcs    uint32_t i, j, k, n;
18549292639Sdavidcs
18550292639Sdavidcs    /* addresses of the paged registers */
18551292639Sdavidcs    const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18552292639Sdavidcs    /* number of paged registers */
18553292639Sdavidcs    int num_pages = __bxe_get_page_reg_num(sc);
18554292639Sdavidcs    /* write addresses */
18555292639Sdavidcs    const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18556292639Sdavidcs    /* number of write addresses */
18557292639Sdavidcs    int write_num = __bxe_get_page_write_num(sc);
18558292639Sdavidcs    /* read addresses info */
18559292639Sdavidcs    const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18560292639Sdavidcs    /* number of read addresses */
18561292639Sdavidcs    int read_num = __bxe_get_page_read_num(sc);
18562292639Sdavidcs    uint32_t addr, size;
18563292639Sdavidcs
18564292639Sdavidcs    for (i = 0; i < num_pages; i++) {
18565292639Sdavidcs        for (j = 0; j < write_num; j++) {
18566292639Sdavidcs            REG_WR(sc, write_addr[j], page_addr[i]);
18567292639Sdavidcs
18568292639Sdavidcs            for (k = 0; k < read_num; k++) {
18569292639Sdavidcs                if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18570292639Sdavidcs                    size = read_addr[k].size;
18571292639Sdavidcs                    for (n = 0; n < size; n++) {
18572292639Sdavidcs                        addr = read_addr[k].addr + n*4;
18573292639Sdavidcs                        *p++ = REG_RD(sc, addr);
18574292639Sdavidcs                    }
18575292639Sdavidcs                }
18576292639Sdavidcs            }
18577292639Sdavidcs        }
18578292639Sdavidcs    }
18579292639Sdavidcs    return;
18580292639Sdavidcs}
18581292639Sdavidcs
18582292639Sdavidcs
18583292639Sdavidcsstatic int
18584292639Sdavidcsbxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18585292639Sdavidcs{
18586292639Sdavidcs    uint32_t i, j, addr;
18587292639Sdavidcs    const struct wreg_addr *wreg_addr_p = NULL;
18588292639Sdavidcs
18589292639Sdavidcs    if (CHIP_IS_E1(sc))
18590292639Sdavidcs        wreg_addr_p = &wreg_addr_e1;
18591292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18592292639Sdavidcs        wreg_addr_p = &wreg_addr_e1h;
18593292639Sdavidcs    else if (CHIP_IS_E2(sc))
18594292639Sdavidcs        wreg_addr_p = &wreg_addr_e2;
18595292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18596292639Sdavidcs        wreg_addr_p = &wreg_addr_e3;
18597292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18598292639Sdavidcs        wreg_addr_p = &wreg_addr_e3b0;
18599292639Sdavidcs    else
18600292639Sdavidcs        return (-1);
18601292639Sdavidcs
18602292639Sdavidcs    /* Read the idle_chk registers */
18603292639Sdavidcs    for (i = 0; i < IDLE_REGS_COUNT; i++) {
18604292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18605292639Sdavidcs            IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18606292639Sdavidcs            for (j = 0; j < idle_reg_addrs[i].size; j++)
18607292639Sdavidcs                *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18608292639Sdavidcs        }
18609292639Sdavidcs    }
18610292639Sdavidcs
18611292639Sdavidcs    /* Read the regular registers */
18612292639Sdavidcs    for (i = 0; i < REGS_COUNT; i++) {
18613292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18614292639Sdavidcs            IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18615292639Sdavidcs            for (j = 0; j < reg_addrs[i].size; j++)
18616292639Sdavidcs                *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18617292639Sdavidcs        }
18618292639Sdavidcs    }
18619292639Sdavidcs
18620292639Sdavidcs    /* Read the CAM registers */
18621292639Sdavidcs    if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18622292639Sdavidcs        IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18623292639Sdavidcs        for (i = 0; i < wreg_addr_p->size; i++) {
18624292639Sdavidcs            *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
18625292639Sdavidcs
18626292639Sdavidcs            /* In case of wreg_addr register, read additional
18627292639Sdavidcs               registers from read_regs array
18628292639Sdavidcs             */
18629292639Sdavidcs            for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
18630292639Sdavidcs                addr = *(wreg_addr_p->read_regs);
18631292639Sdavidcs                *p++ = REG_RD(sc, addr + j*4);
18632292639Sdavidcs            }
18633292639Sdavidcs        }
18634292639Sdavidcs    }
18635292639Sdavidcs
18636292639Sdavidcs    /* Paged registers are supported in E2 & E3 only */
18637292639Sdavidcs    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
18638292639Sdavidcs        /* Read "paged" registers */
18639292639Sdavidcs        bxe_read_pages_regs(sc, p, preset);
18640292639Sdavidcs    }
18641292639Sdavidcs
18642292639Sdavidcs    return 0;
18643292639Sdavidcs}
18644292639Sdavidcs
18645298294Sdavidcsint
18646295823Sdavidcsbxe_grc_dump(struct bxe_softc *sc)
18647292639Sdavidcs{
18648292639Sdavidcs    int rval = 0;
18649292639Sdavidcs    uint32_t preset_idx;
18650292639Sdavidcs    uint8_t *buf;
18651292639Sdavidcs    uint32_t size;
18652292639Sdavidcs    struct  dump_header *d_hdr;
18653298294Sdavidcs    uint32_t i;
18654298294Sdavidcs    uint32_t reg_val;
18655298294Sdavidcs    uint32_t reg_addr;
18656298294Sdavidcs    uint32_t cmd_offset;
18657298294Sdavidcs    struct ecore_ilt *ilt = SC_ILT(sc);
18658298294Sdavidcs    struct bxe_fastpath *fp;
18659298294Sdavidcs    struct ilt_client_info *ilt_cli;
18660298294Sdavidcs    int grc_dump_size;
18661295823Sdavidcs
18662298294Sdavidcs
18663298294Sdavidcs    if (sc->grcdump_done || sc->grcdump_started)
18664295823Sdavidcs	return (rval);
18665292639Sdavidcs
18666298294Sdavidcs    sc->grcdump_started = 1;
18667298294Sdavidcs    BLOGI(sc, "Started collecting grcdump\n");
18668298294Sdavidcs
18669298294Sdavidcs    grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18670298294Sdavidcs                sizeof(struct  dump_header);
18671298294Sdavidcs
18672298294Sdavidcs    sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
18673298294Sdavidcs
18674298294Sdavidcs    if (sc->grc_dump == NULL) {
18675298294Sdavidcs        BLOGW(sc, "Unable to allocate memory for grcdump collection\n");
18676298294Sdavidcs        return(ENOMEM);
18677298294Sdavidcs    }
18678298294Sdavidcs
18679298294Sdavidcs
18680298294Sdavidcs
18681298294Sdavidcs    /* Disable parity attentions as long as following dump may
18682298294Sdavidcs     * cause false alarms by reading never written registers. We
18683298294Sdavidcs     * will re-enable parity attentions right after the dump.
18684298294Sdavidcs     */
18685298294Sdavidcs
18686298294Sdavidcs    /* Disable parity on path 0 */
18687298294Sdavidcs    bxe_pretend_func(sc, 0);
18688298294Sdavidcs
18689292639Sdavidcs    ecore_disable_blocks_parity(sc);
18690292639Sdavidcs
18691298294Sdavidcs    /* Disable parity on path 1 */
18692298294Sdavidcs    bxe_pretend_func(sc, 1);
18693298294Sdavidcs    ecore_disable_blocks_parity(sc);
18694298294Sdavidcs
18695298294Sdavidcs    /* Return to current function */
18696298294Sdavidcs    bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18697298294Sdavidcs
18698295823Sdavidcs    buf = sc->grc_dump;
18699295823Sdavidcs    d_hdr = sc->grc_dump;
18700292639Sdavidcs
18701292639Sdavidcs    d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
18702292639Sdavidcs    d_hdr->version = BNX2X_DUMP_VERSION;
18703292639Sdavidcs    d_hdr->preset = DUMP_ALL_PRESETS;
18704292639Sdavidcs
18705292639Sdavidcs    if (CHIP_IS_E1(sc)) {
18706292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1;
18707292639Sdavidcs    } else if (CHIP_IS_E1H(sc)) {
18708292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1H;
18709292639Sdavidcs    } else if (CHIP_IS_E2(sc)) {
18710292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E2 |
18711292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18712292639Sdavidcs    } else if (CHIP_IS_E3A0(sc)) {
18713292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
18714292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18715292639Sdavidcs    } else if (CHIP_IS_E3B0(sc)) {
18716292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
18717292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18718292639Sdavidcs    }
18719292639Sdavidcs
18720292639Sdavidcs    buf += sizeof(struct  dump_header);
18721292639Sdavidcs
18722292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18723292639Sdavidcs
18724292639Sdavidcs        /* Skip presets with IOR */
18725292639Sdavidcs        if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
18726292639Sdavidcs            (preset_idx == 11))
18727292639Sdavidcs            continue;
18728292639Sdavidcs
18729298294Sdavidcs        rval = bxe_get_preset_regs(sc, (uint32_t *)buf, preset_idx);
18730292639Sdavidcs
18731292639Sdavidcs	if (rval)
18732292639Sdavidcs            break;
18733292639Sdavidcs
18734292639Sdavidcs        size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
18735292639Sdavidcs
18736292639Sdavidcs        buf += size;
18737292639Sdavidcs    }
18738292639Sdavidcs
18739298294Sdavidcs    bxe_pretend_func(sc, 0);
18740292639Sdavidcs    ecore_clear_blocks_parity(sc);
18741292639Sdavidcs    ecore_enable_blocks_parity(sc);
18742292639Sdavidcs
18743298294Sdavidcs    bxe_pretend_func(sc, 1);
18744298294Sdavidcs    ecore_clear_blocks_parity(sc);
18745298294Sdavidcs    ecore_enable_blocks_parity(sc);
18746298294Sdavidcs
18747298294Sdavidcs    /* Return to current function */
18748298294Sdavidcs    bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18749298294Sdavidcs
18750298294Sdavidcs
18751298294Sdavidcs
18752315881Sdavidcs    if(sc->state == BXE_STATE_OPEN) {
18753315881Sdavidcs        if(sc->fw_stats_req  != NULL) {
18754315881Sdavidcs    		BLOGI(sc, "fw stats start_paddr %#jx end_paddr %#jx vaddr %p size 0x%x\n",
18755315881Sdavidcs        			(uintmax_t)sc->fw_stats_req_mapping,
18756315881Sdavidcs        			(uintmax_t)sc->fw_stats_data_mapping,
18757315881Sdavidcs        			sc->fw_stats_req, (sc->fw_stats_req_size + sc->fw_stats_data_size));
18758315881Sdavidcs		}
18759315881Sdavidcs		if(sc->def_sb != NULL) {
18760315881Sdavidcs			BLOGI(sc, "def_status_block paddr %p vaddr %p size 0x%zx\n",
18761315881Sdavidcs        			(void *)sc->def_sb_dma.paddr, sc->def_sb,
18762315881Sdavidcs        			sizeof(struct host_sp_status_block));
18763315881Sdavidcs		}
18764315881Sdavidcs		if(sc->eq_dma.vaddr != NULL) {
18765315881Sdavidcs    		BLOGI(sc, "event_queue paddr %#jx vaddr %p size 0x%x\n",
18766315881Sdavidcs        			(uintmax_t)sc->eq_dma.paddr, sc->eq_dma.vaddr, BCM_PAGE_SIZE);
18767315881Sdavidcs		}
18768315881Sdavidcs		if(sc->sp_dma.vaddr != NULL) {
18769315881Sdavidcs    		BLOGI(sc, "slow path paddr %#jx vaddr %p size 0x%zx\n",
18770315881Sdavidcs        			(uintmax_t)sc->sp_dma.paddr, sc->sp_dma.vaddr,
18771315881Sdavidcs        			sizeof(struct bxe_slowpath));
18772315881Sdavidcs		}
18773315881Sdavidcs		if(sc->spq_dma.vaddr != NULL) {
18774315881Sdavidcs    		BLOGI(sc, "slow path queue paddr %#jx vaddr %p size 0x%x\n",
18775315881Sdavidcs        			(uintmax_t)sc->spq_dma.paddr, sc->spq_dma.vaddr, BCM_PAGE_SIZE);
18776315881Sdavidcs		}
18777315881Sdavidcs		if(sc->gz_buf_dma.vaddr != NULL) {
18778315881Sdavidcs    		BLOGI(sc, "fw_buf paddr %#jx vaddr %p size 0x%x\n",
18779315881Sdavidcs        			(uintmax_t)sc->gz_buf_dma.paddr, sc->gz_buf_dma.vaddr,
18780315881Sdavidcs        			FW_BUF_SIZE);
18781315881Sdavidcs		}
18782315881Sdavidcs    	for (i = 0; i < sc->num_queues; i++) {
18783315881Sdavidcs        	fp = &sc->fp[i];
18784315881Sdavidcs			if(fp->sb_dma.vaddr != NULL && fp->tx_dma.vaddr != NULL &&
18785315881Sdavidcs                        fp->rx_dma.vaddr != NULL && fp->rcq_dma.vaddr != NULL &&
18786315881Sdavidcs                        fp->rx_sge_dma.vaddr != NULL) {
18787298294Sdavidcs
18788315881Sdavidcs				BLOGI(sc, "FP status block fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18789315881Sdavidcs            			(uintmax_t)fp->sb_dma.paddr, fp->sb_dma.vaddr,
18790315881Sdavidcs            			sizeof(union bxe_host_hc_status_block));
18791315881Sdavidcs				BLOGI(sc, "TX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18792315881Sdavidcs            			(uintmax_t)fp->tx_dma.paddr, fp->tx_dma.vaddr,
18793315881Sdavidcs            			(BCM_PAGE_SIZE * TX_BD_NUM_PAGES));
18794315881Sdavidcs        		BLOGI(sc, "RX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18795315881Sdavidcs            			(uintmax_t)fp->rx_dma.paddr, fp->rx_dma.vaddr,
18796315881Sdavidcs            			(BCM_PAGE_SIZE * RX_BD_NUM_PAGES));
18797315881Sdavidcs        		BLOGI(sc, "RX RCQ CHAIN fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18798315881Sdavidcs            			(uintmax_t)fp->rcq_dma.paddr, fp->rcq_dma.vaddr,
18799315881Sdavidcs            			(BCM_PAGE_SIZE * RCQ_NUM_PAGES));
18800315881Sdavidcs        		BLOGI(sc, "RX SGE CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18801315881Sdavidcs            			(uintmax_t)fp->rx_sge_dma.paddr, fp->rx_sge_dma.vaddr,
18802315881Sdavidcs            			(BCM_PAGE_SIZE * RX_SGE_NUM_PAGES));
18803315881Sdavidcs    		}
18804315881Sdavidcs		}
18805315881Sdavidcs		if(ilt != NULL ) {
18806315881Sdavidcs    		ilt_cli = &ilt->clients[1];
18807315881Sdavidcs			if(ilt->lines != NULL) {
18808315881Sdavidcs    		for (i = ilt_cli->start; i <= ilt_cli->end; i++) {
18809315881Sdavidcs        		BLOGI(sc, "ECORE_ILT paddr %#jx vaddr %p size 0x%x\n",
18810315881Sdavidcs            			(uintmax_t)(((struct bxe_dma *)((&ilt->lines[i])->page))->paddr),
18811315881Sdavidcs            			((struct bxe_dma *)((&ilt->lines[i])->page))->vaddr, BCM_PAGE_SIZE);
18812315881Sdavidcs    		}
18813315881Sdavidcs			}
18814315881Sdavidcs		}
18815298294Sdavidcs
18816298294Sdavidcs
18817315881Sdavidcs    	cmd_offset = DMAE_REG_CMD_MEM;
18818315881Sdavidcs    	for (i = 0; i < 224; i++) {
18819315881Sdavidcs        	reg_addr = (cmd_offset +(i * 4));
18820315881Sdavidcs        	reg_val = REG_RD(sc, reg_addr);
18821315881Sdavidcs        	BLOGI(sc, "DMAE_REG_CMD_MEM i=%d reg_addr 0x%x reg_val 0x%08x\n",i,
18822315881Sdavidcs            			reg_addr, reg_val);
18823315881Sdavidcs    	}
18824315881Sdavidcs	}
18825298294Sdavidcs
18826298294Sdavidcs    BLOGI(sc, "Collection of grcdump done\n");
18827292639Sdavidcs    sc->grcdump_done = 1;
18828292639Sdavidcs    return(rval);
18829292639Sdavidcs}
18830292639Sdavidcs
18831292639Sdavidcsstatic int
18832292639Sdavidcsbxe_add_cdev(struct bxe_softc *sc)
18833292639Sdavidcs{
18834297884Sdavidcs    sc->eeprom = malloc(BXE_EEPROM_MAX_DATA_LEN, M_DEVBUF, M_NOWAIT);
18835297884Sdavidcs
18836297884Sdavidcs    if (sc->eeprom == NULL) {
18837297884Sdavidcs        BLOGW(sc, "Unable to alloc for eeprom size buffer\n");
18838297884Sdavidcs        return (-1);
18839297884Sdavidcs    }
18840297884Sdavidcs
18841292639Sdavidcs    sc->ioctl_dev = make_dev(&bxe_cdevsw,
18842292639Sdavidcs                            sc->ifp->if_dunit,
18843292639Sdavidcs                            UID_ROOT,
18844292639Sdavidcs                            GID_WHEEL,
18845292639Sdavidcs                            0600,
18846292639Sdavidcs                            "%s",
18847292639Sdavidcs                            if_name(sc->ifp));
18848292639Sdavidcs
18849292639Sdavidcs    if (sc->ioctl_dev == NULL) {
18850297884Sdavidcs        free(sc->eeprom, M_DEVBUF);
18851297884Sdavidcs        sc->eeprom = NULL;
18852292639Sdavidcs        return (-1);
18853292639Sdavidcs    }
18854292639Sdavidcs
18855292639Sdavidcs    sc->ioctl_dev->si_drv1 = sc;
18856292639Sdavidcs
18857292639Sdavidcs    return (0);
18858292639Sdavidcs}
18859292639Sdavidcs
18860292639Sdavidcsstatic void
18861292639Sdavidcsbxe_del_cdev(struct bxe_softc *sc)
18862292639Sdavidcs{
18863292639Sdavidcs    if (sc->ioctl_dev != NULL)
18864292639Sdavidcs        destroy_dev(sc->ioctl_dev);
18865292639Sdavidcs
18866297884Sdavidcs    if (sc->eeprom != NULL) {
18867297884Sdavidcs        free(sc->eeprom, M_DEVBUF);
18868297884Sdavidcs        sc->eeprom = NULL;
18869297884Sdavidcs    }
18870298294Sdavidcs    sc->ioctl_dev = NULL;
18871297884Sdavidcs
18872292639Sdavidcs    return;
18873292639Sdavidcs}
18874292639Sdavidcs
18875297884Sdavidcsstatic bool bxe_is_nvram_accessible(struct bxe_softc *sc)
18876297884Sdavidcs{
18877297884Sdavidcs
18878297884Sdavidcs    if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
18879297884Sdavidcs        return FALSE;
18880297884Sdavidcs
18881297884Sdavidcs    return TRUE;
18882297884Sdavidcs}
18883297884Sdavidcs
18884297884Sdavidcs
18885292639Sdavidcsstatic int
18886297884Sdavidcsbxe_wr_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18887297884Sdavidcs{
18888297884Sdavidcs    int rval = 0;
18889297884Sdavidcs
18890297884Sdavidcs    if(!bxe_is_nvram_accessible(sc)) {
18891297884Sdavidcs        BLOGW(sc, "Cannot access eeprom when interface is down\n");
18892297884Sdavidcs        return (-EAGAIN);
18893297884Sdavidcs    }
18894297884Sdavidcs    rval = bxe_nvram_write(sc, offset, (uint8_t *)data, len);
18895297884Sdavidcs
18896297884Sdavidcs
18897297884Sdavidcs   return (rval);
18898297884Sdavidcs}
18899297884Sdavidcs
18900297884Sdavidcsstatic int
18901297884Sdavidcsbxe_rd_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18902297884Sdavidcs{
18903297884Sdavidcs    int rval = 0;
18904297884Sdavidcs
18905297884Sdavidcs    if(!bxe_is_nvram_accessible(sc)) {
18906297884Sdavidcs        BLOGW(sc, "Cannot access eeprom when interface is down\n");
18907297884Sdavidcs        return (-EAGAIN);
18908297884Sdavidcs    }
18909297884Sdavidcs    rval = bxe_nvram_read(sc, offset, (uint8_t *)data, len);
18910297884Sdavidcs
18911297884Sdavidcs   return (rval);
18912297884Sdavidcs}
18913297884Sdavidcs
18914297884Sdavidcsstatic int
18915297884Sdavidcsbxe_eeprom_rd_wr(struct bxe_softc *sc, bxe_eeprom_t *eeprom)
18916297884Sdavidcs{
18917297884Sdavidcs    int rval = 0;
18918297884Sdavidcs
18919297884Sdavidcs    switch (eeprom->eeprom_cmd) {
18920297884Sdavidcs
18921297884Sdavidcs    case BXE_EEPROM_CMD_SET_EEPROM:
18922297884Sdavidcs
18923297884Sdavidcs        rval = copyin(eeprom->eeprom_data, sc->eeprom,
18924297884Sdavidcs                       eeprom->eeprom_data_len);
18925297884Sdavidcs
18926297884Sdavidcs        if (rval)
18927297884Sdavidcs            break;
18928297884Sdavidcs
18929297884Sdavidcs        rval = bxe_wr_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18930297884Sdavidcs                       eeprom->eeprom_data_len);
18931297884Sdavidcs        break;
18932297884Sdavidcs
18933297884Sdavidcs    case BXE_EEPROM_CMD_GET_EEPROM:
18934297884Sdavidcs
18935297884Sdavidcs        rval = bxe_rd_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18936297884Sdavidcs                       eeprom->eeprom_data_len);
18937297884Sdavidcs
18938297884Sdavidcs        if (rval) {
18939297884Sdavidcs            break;
18940297884Sdavidcs        }
18941297884Sdavidcs
18942297884Sdavidcs        rval = copyout(sc->eeprom, eeprom->eeprom_data,
18943297884Sdavidcs                       eeprom->eeprom_data_len);
18944297884Sdavidcs        break;
18945297884Sdavidcs
18946297884Sdavidcs    default:
18947297884Sdavidcs            rval = EINVAL;
18948297884Sdavidcs            break;
18949297884Sdavidcs    }
18950297884Sdavidcs
18951297884Sdavidcs    if (rval) {
18952297884Sdavidcs        BLOGW(sc, "ioctl cmd %d  failed rval %d\n", eeprom->eeprom_cmd, rval);
18953297884Sdavidcs    }
18954297884Sdavidcs
18955297884Sdavidcs    return (rval);
18956297884Sdavidcs}
18957297884Sdavidcs
18958297884Sdavidcsstatic int
18959297884Sdavidcsbxe_get_settings(struct bxe_softc *sc, bxe_dev_setting_t *dev_p)
18960297884Sdavidcs{
18961297884Sdavidcs    uint32_t ext_phy_config;
18962297884Sdavidcs    int port = SC_PORT(sc);
18963297884Sdavidcs    int cfg_idx = bxe_get_link_cfg_idx(sc);
18964297884Sdavidcs
18965297884Sdavidcs    dev_p->supported = sc->port.supported[cfg_idx] |
18966297884Sdavidcs            (sc->port.supported[cfg_idx ^ 1] &
18967297884Sdavidcs            (ELINK_SUPPORTED_TP | ELINK_SUPPORTED_FIBRE));
18968297884Sdavidcs    dev_p->advertising = sc->port.advertising[cfg_idx];
18969297884Sdavidcs    if(sc->link_params.phy[bxe_get_cur_phy_idx(sc)].media_type ==
18970297884Sdavidcs        ELINK_ETH_PHY_SFP_1G_FIBER) {
18971297884Sdavidcs        dev_p->supported = ~(ELINK_SUPPORTED_10000baseT_Full);
18972297884Sdavidcs        dev_p->advertising &= ~(ADVERTISED_10000baseT_Full);
18973297884Sdavidcs    }
18974297884Sdavidcs    if ((sc->state == BXE_STATE_OPEN) && sc->link_vars.link_up &&
18975297884Sdavidcs        !(sc->flags & BXE_MF_FUNC_DIS)) {
18976297884Sdavidcs        dev_p->duplex = sc->link_vars.duplex;
18977297884Sdavidcs        if (IS_MF(sc) && !BXE_NOMCP(sc))
18978297884Sdavidcs            dev_p->speed = bxe_get_mf_speed(sc);
18979297884Sdavidcs        else
18980297884Sdavidcs            dev_p->speed = sc->link_vars.line_speed;
18981297884Sdavidcs    } else {
18982297884Sdavidcs        dev_p->duplex = DUPLEX_UNKNOWN;
18983297884Sdavidcs        dev_p->speed = SPEED_UNKNOWN;
18984297884Sdavidcs    }
18985297884Sdavidcs
18986297884Sdavidcs    dev_p->port = bxe_media_detect(sc);
18987297884Sdavidcs
18988297884Sdavidcs    ext_phy_config = SHMEM_RD(sc,
18989297884Sdavidcs                         dev_info.port_hw_config[port].external_phy_config);
18990297884Sdavidcs    if((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) ==
18991297884Sdavidcs        PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
18992297884Sdavidcs        dev_p->phy_address =  sc->port.phy_addr;
18993297884Sdavidcs    else if(((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18994297884Sdavidcs            PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
18995297884Sdavidcs        ((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18996297884Sdavidcs            PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
18997297884Sdavidcs        dev_p->phy_address = ELINK_XGXS_EXT_PHY_ADDR(ext_phy_config);
18998297884Sdavidcs    else
18999297884Sdavidcs        dev_p->phy_address = 0;
19000297884Sdavidcs
19001297884Sdavidcs    if(sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG)
19002297884Sdavidcs        dev_p->autoneg = AUTONEG_ENABLE;
19003297884Sdavidcs    else
19004297884Sdavidcs       dev_p->autoneg = AUTONEG_DISABLE;
19005297884Sdavidcs
19006297884Sdavidcs
19007297884Sdavidcs    return 0;
19008297884Sdavidcs}
19009297884Sdavidcs
19010297884Sdavidcsstatic int
19011292639Sdavidcsbxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
19012292639Sdavidcs        struct thread *td)
19013292639Sdavidcs{
19014292639Sdavidcs    struct bxe_softc    *sc;
19015292639Sdavidcs    int                 rval = 0;
19016292639Sdavidcs    device_t            pci_dev;
19017292639Sdavidcs    bxe_grcdump_t       *dump = NULL;
19018292639Sdavidcs    int grc_dump_size;
19019297884Sdavidcs    bxe_drvinfo_t   *drv_infop = NULL;
19020297884Sdavidcs    bxe_dev_setting_t  *dev_p;
19021297884Sdavidcs    bxe_dev_setting_t  dev_set;
19022297884Sdavidcs    bxe_get_regs_t  *reg_p;
19023297884Sdavidcs    bxe_reg_rdw_t *reg_rdw_p;
19024297884Sdavidcs    bxe_pcicfg_rdw_t *cfg_rdw_p;
19025297884Sdavidcs    bxe_perm_mac_addr_t *mac_addr_p;
19026292639Sdavidcs
19027297884Sdavidcs
19028292639Sdavidcs    if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
19029292639Sdavidcs        return ENXIO;
19030292639Sdavidcs
19031292639Sdavidcs    pci_dev= sc->dev;
19032292639Sdavidcs
19033292639Sdavidcs    dump = (bxe_grcdump_t *)data;
19034292639Sdavidcs
19035292639Sdavidcs    switch(cmd) {
19036292639Sdavidcs
19037292639Sdavidcs        case BXE_GRC_DUMP_SIZE:
19038292639Sdavidcs            dump->pci_func = sc->pcie_func;
19039297884Sdavidcs            dump->grcdump_size =
19040297884Sdavidcs                (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19041297884Sdavidcs                     sizeof(struct  dump_header);
19042292639Sdavidcs            break;
19043292639Sdavidcs
19044292639Sdavidcs        case BXE_GRC_DUMP:
19045292639Sdavidcs
19046292639Sdavidcs            grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19047297884Sdavidcs                                sizeof(struct  dump_header);
19048298294Sdavidcs            if ((!sc->trigger_grcdump) || (dump->grcdump == NULL) ||
19049298294Sdavidcs                (dump->grcdump_size < grc_dump_size)) {
19050292639Sdavidcs                rval = EINVAL;
19051292639Sdavidcs                break;
19052292639Sdavidcs            }
19053292639Sdavidcs
19054298294Sdavidcs            if((sc->trigger_grcdump) && (!sc->grcdump_done) &&
19055298294Sdavidcs                (!sc->grcdump_started)) {
19056298294Sdavidcs                rval =  bxe_grc_dump(sc);
19057298294Sdavidcs            }
19058298294Sdavidcs
19059298294Sdavidcs            if((!rval) && (sc->grcdump_done) && (sc->grcdump_started) &&
19060298294Sdavidcs                (sc->grc_dump != NULL))  {
19061298294Sdavidcs                dump->grcdump_dwords = grc_dump_size >> 2;
19062298294Sdavidcs                rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
19063298294Sdavidcs                free(sc->grc_dump, M_DEVBUF);
19064298294Sdavidcs                sc->grc_dump = NULL;
19065298294Sdavidcs                sc->grcdump_started = 0;
19066298294Sdavidcs                sc->grcdump_done = 0;
19067298294Sdavidcs            }
19068298294Sdavidcs
19069292639Sdavidcs            break;
19070292639Sdavidcs
19071297884Sdavidcs        case BXE_DRV_INFO:
19072297884Sdavidcs            drv_infop = (bxe_drvinfo_t *)data;
19073297884Sdavidcs            snprintf(drv_infop->drv_name, BXE_DRV_NAME_LENGTH, "%s", "bxe");
19074297884Sdavidcs            snprintf(drv_infop->drv_version, BXE_DRV_VERSION_LENGTH, "v:%s",
19075297884Sdavidcs                BXE_DRIVER_VERSION);
19076297884Sdavidcs            snprintf(drv_infop->mfw_version, BXE_MFW_VERSION_LENGTH, "%s",
19077297884Sdavidcs                sc->devinfo.bc_ver_str);
19078297884Sdavidcs            snprintf(drv_infop->stormfw_version, BXE_STORMFW_VERSION_LENGTH,
19079297884Sdavidcs                "%s", sc->fw_ver_str);
19080297884Sdavidcs            drv_infop->eeprom_dump_len = sc->devinfo.flash_size;
19081297884Sdavidcs            drv_infop->reg_dump_len =
19082297884Sdavidcs                (bxe_get_total_regs_len32(sc) * sizeof(uint32_t))
19083297884Sdavidcs                    + sizeof(struct  dump_header);
19084297884Sdavidcs            snprintf(drv_infop->bus_info, BXE_BUS_INFO_LENGTH, "%d:%d:%d",
19085297884Sdavidcs                sc->pcie_bus, sc->pcie_device, sc->pcie_func);
19086297884Sdavidcs            break;
19087298294Sdavidcs
19088297884Sdavidcs        case BXE_DEV_SETTING:
19089297884Sdavidcs            dev_p = (bxe_dev_setting_t *)data;
19090297884Sdavidcs            bxe_get_settings(sc, &dev_set);
19091297884Sdavidcs            dev_p->supported = dev_set.supported;
19092297884Sdavidcs            dev_p->advertising = dev_set.advertising;
19093297884Sdavidcs            dev_p->speed = dev_set.speed;
19094297884Sdavidcs            dev_p->duplex = dev_set.duplex;
19095297884Sdavidcs            dev_p->port = dev_set.port;
19096297884Sdavidcs            dev_p->phy_address = dev_set.phy_address;
19097297884Sdavidcs            dev_p->autoneg = dev_set.autoneg;
19098297884Sdavidcs
19099297884Sdavidcs            break;
19100297884Sdavidcs
19101297884Sdavidcs        case BXE_GET_REGS:
19102297884Sdavidcs
19103297884Sdavidcs            reg_p = (bxe_get_regs_t *)data;
19104297884Sdavidcs            grc_dump_size = reg_p->reg_buf_len;
19105297884Sdavidcs
19106298294Sdavidcs            if((!sc->grcdump_done) && (!sc->grcdump_started)) {
19107297884Sdavidcs                bxe_grc_dump(sc);
19108297884Sdavidcs            }
19109298294Sdavidcs            if((sc->grcdump_done) && (sc->grcdump_started) &&
19110298294Sdavidcs                (sc->grc_dump != NULL))  {
19111297884Sdavidcs                rval = copyout(sc->grc_dump, reg_p->reg_buf, grc_dump_size);
19112298294Sdavidcs                free(sc->grc_dump, M_DEVBUF);
19113298294Sdavidcs                sc->grc_dump = NULL;
19114298294Sdavidcs                sc->grcdump_started = 0;
19115297884Sdavidcs                sc->grcdump_done = 0;
19116297884Sdavidcs            }
19117297884Sdavidcs
19118297884Sdavidcs            break;
19119298294Sdavidcs
19120297884Sdavidcs        case BXE_RDW_REG:
19121297884Sdavidcs            reg_rdw_p = (bxe_reg_rdw_t *)data;
19122297884Sdavidcs            if((reg_rdw_p->reg_cmd == BXE_READ_REG_CMD) &&
19123297884Sdavidcs                (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19124297884Sdavidcs                reg_rdw_p->reg_val = REG_RD(sc, reg_rdw_p->reg_id);
19125297884Sdavidcs
19126297884Sdavidcs            if((reg_rdw_p->reg_cmd == BXE_WRITE_REG_CMD) &&
19127297884Sdavidcs                (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19128297884Sdavidcs                REG_WR(sc, reg_rdw_p->reg_id, reg_rdw_p->reg_val);
19129297884Sdavidcs
19130297884Sdavidcs            break;
19131297884Sdavidcs
19132297884Sdavidcs        case BXE_RDW_PCICFG:
19133297884Sdavidcs            cfg_rdw_p = (bxe_pcicfg_rdw_t *)data;
19134297884Sdavidcs            if(cfg_rdw_p->cfg_cmd == BXE_READ_PCICFG) {
19135297884Sdavidcs
19136297884Sdavidcs                cfg_rdw_p->cfg_val = pci_read_config(sc->dev, cfg_rdw_p->cfg_id,
19137297884Sdavidcs                                         cfg_rdw_p->cfg_width);
19138297884Sdavidcs
19139297884Sdavidcs            } else if(cfg_rdw_p->cfg_cmd == BXE_WRITE_PCICFG) {
19140297884Sdavidcs                pci_write_config(sc->dev, cfg_rdw_p->cfg_id, cfg_rdw_p->cfg_val,
19141297884Sdavidcs                            cfg_rdw_p->cfg_width);
19142297884Sdavidcs            } else {
19143297884Sdavidcs                BLOGW(sc, "BXE_RDW_PCICFG ioctl wrong cmd passed\n");
19144297884Sdavidcs            }
19145297884Sdavidcs            break;
19146297884Sdavidcs
19147297884Sdavidcs        case BXE_MAC_ADDR:
19148297884Sdavidcs            mac_addr_p = (bxe_perm_mac_addr_t *)data;
19149297884Sdavidcs            snprintf(mac_addr_p->mac_addr_str, sizeof(sc->mac_addr_str), "%s",
19150297884Sdavidcs                sc->mac_addr_str);
19151297884Sdavidcs            break;
19152297884Sdavidcs
19153297884Sdavidcs        case BXE_EEPROM:
19154297884Sdavidcs            rval = bxe_eeprom_rd_wr(sc, (bxe_eeprom_t *)data);
19155297884Sdavidcs            break;
19156297884Sdavidcs
19157297884Sdavidcs
19158292639Sdavidcs        default:
19159292639Sdavidcs            break;
19160292639Sdavidcs    }
19161292639Sdavidcs
19162292639Sdavidcs    return (rval);
19163292639Sdavidcs}
19164