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 339881 2018-10-29 21:09:39Z davidcs $");
29255736Sdavidch
30336146Sdavidcs#define BXE_DRIVER_VERSION "1.78.91"
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
203339881Sdavidcs
204255736Sdavidch/*
205255736Sdavidch * FreeBSD KLD module/device interface event handler method.
206255736Sdavidch */
207255736Sdavidchstatic device_method_t bxe_methods[] = {
208255736Sdavidch    /* Device interface (device_if.h) */
209255736Sdavidch    DEVMETHOD(device_probe,     bxe_probe),
210255736Sdavidch    DEVMETHOD(device_attach,    bxe_attach),
211255736Sdavidch    DEVMETHOD(device_detach,    bxe_detach),
212255736Sdavidch    DEVMETHOD(device_shutdown,  bxe_shutdown),
213255736Sdavidch    /* Bus interface (bus_if.h) */
214255736Sdavidch    DEVMETHOD(bus_print_child,  bus_generic_print_child),
215255736Sdavidch    DEVMETHOD(bus_driver_added, bus_generic_driver_added),
216255736Sdavidch    KOBJMETHOD_END
217255736Sdavidch};
218255736Sdavidch
219255736Sdavidch/*
220255736Sdavidch * FreeBSD KLD Module data declaration
221255736Sdavidch */
222255736Sdavidchstatic driver_t bxe_driver = {
223255736Sdavidch    "bxe",                   /* module name */
224255736Sdavidch    bxe_methods,             /* event handler */
225255736Sdavidch    sizeof(struct bxe_softc) /* extra data */
226255736Sdavidch};
227255736Sdavidch
228255736Sdavidch/*
229255736Sdavidch * FreeBSD dev class is needed to manage dev instances and
230255736Sdavidch * to associate with a bus type
231255736Sdavidch */
232255736Sdavidchstatic devclass_t bxe_devclass;
233255736Sdavidch
234255736SdavidchMODULE_DEPEND(bxe, pci, 1, 1, 1);
235255736SdavidchMODULE_DEPEND(bxe, ether, 1, 1, 1);
236255736SdavidchDRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
237255736Sdavidch
238255736Sdavidch/* resources needed for unloading a previously loaded device */
239255736Sdavidch
240255736Sdavidch#define BXE_PREV_WAIT_NEEDED 1
241255736Sdavidchstruct mtx bxe_prev_mtx;
242255736SdavidchMTX_SYSINIT(bxe_prev_mtx, &bxe_prev_mtx, "bxe_prev_lock", MTX_DEF);
243255736Sdavidchstruct bxe_prev_list_node {
244255736Sdavidch    LIST_ENTRY(bxe_prev_list_node) node;
245255736Sdavidch    uint8_t bus;
246255736Sdavidch    uint8_t slot;
247255736Sdavidch    uint8_t path;
248255736Sdavidch    uint8_t aer; /* XXX automatic error recovery */
249255736Sdavidch    uint8_t undi;
250255736Sdavidch};
251255736Sdavidchstatic LIST_HEAD(, bxe_prev_list_node) bxe_prev_list = LIST_HEAD_INITIALIZER(bxe_prev_list);
252255736Sdavidch
253255736Sdavidchstatic int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
254255736Sdavidch
255255736Sdavidch/* Tunable device values... */
256255736Sdavidch
257255736SdavidchSYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
258255736Sdavidch
259255736Sdavidch/* Debug */
260258187Sedavisunsigned long bxe_debug = 0;
261267992ShselaskySYSCTL_ULONG(_hw_bxe, OID_AUTO, debug, CTLFLAG_RDTUN,
262258187Sedavis             &bxe_debug, 0, "Debug logging mode");
263255736Sdavidch
264255736Sdavidch/* Interrupt Mode: 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
265255736Sdavidchstatic int bxe_interrupt_mode = INTR_MODE_MSIX;
266258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, interrupt_mode, CTLFLAG_RDTUN,
267258187Sedavis           &bxe_interrupt_mode, 0, "Interrupt (MSI-X/MSI/INTx) mode");
268255736Sdavidch
269255736Sdavidch/* Number of Queues: 0 (Auto) or 1 to 16 (fixed queue number) */
270255736Sdavidchstatic int bxe_queue_count = 4;
271258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN,
272258187Sedavis           &bxe_queue_count, 0, "Multi-Queue queue count");
273255736Sdavidch
274255736Sdavidch/* max number of buffers per queue (default RX_BD_USABLE) */
275258187Sedavisstatic int bxe_max_rx_bufs = 0;
276258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_rx_bufs, CTLFLAG_RDTUN,
277258187Sedavis           &bxe_max_rx_bufs, 0, "Maximum Number of Rx Buffers Per Queue");
278255736Sdavidch
279255736Sdavidch/* Host interrupt coalescing RX tick timer (usecs) */
280258187Sedavisstatic int bxe_hc_rx_ticks = 25;
281258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_rx_ticks, CTLFLAG_RDTUN,
282258187Sedavis           &bxe_hc_rx_ticks, 0, "Host Coalescing Rx ticks");
283255736Sdavidch
284255736Sdavidch/* Host interrupt coalescing TX tick timer (usecs) */
285258187Sedavisstatic int bxe_hc_tx_ticks = 50;
286258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_tx_ticks, CTLFLAG_RDTUN,
287258187Sedavis           &bxe_hc_tx_ticks, 0, "Host Coalescing Tx ticks");
288255736Sdavidch
289255736Sdavidch/* Maximum number of Rx packets to process at a time */
290258187Sedavisstatic int bxe_rx_budget = 0xffffffff;
291258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, rx_budget, CTLFLAG_TUN,
292258187Sedavis           &bxe_rx_budget, 0, "Rx processing budget");
293255736Sdavidch
294255736Sdavidch/* Maximum LRO aggregation size */
295258187Sedavisstatic int bxe_max_aggregation_size = 0;
296258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_aggregation_size, CTLFLAG_TUN,
297258187Sedavis           &bxe_max_aggregation_size, 0, "max aggregation size");
298255736Sdavidch
299255736Sdavidch/* PCI MRRS: -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB) */
300255736Sdavidchstatic int bxe_mrrs = -1;
301258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN,
302258187Sedavis           &bxe_mrrs, 0, "PCIe maximum read request size");
303255736Sdavidch
304255736Sdavidch/* AutoGrEEEn: 0 (hardware default), 1 (force on), 2 (force off) */
305255736Sdavidchstatic int bxe_autogreeen = 0;
306258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, autogreeen, CTLFLAG_RDTUN,
307258187Sedavis           &bxe_autogreeen, 0, "AutoGrEEEn support");
308255736Sdavidch
309255736Sdavidch/* 4-tuple RSS support for UDP: 0 (disabled), 1 (enabled) */
310255736Sdavidchstatic int bxe_udp_rss = 0;
311258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, udp_rss, CTLFLAG_RDTUN,
312258187Sedavis           &bxe_udp_rss, 0, "UDP RSS support");
313255736Sdavidch
314255736Sdavidch
315255736Sdavidch#define STAT_NAME_LEN 32 /* no stat names below can be longer than this */
316255736Sdavidch
317255736Sdavidch#define STATS_OFFSET32(stat_name)                   \
318255736Sdavidch    (offsetof(struct bxe_eth_stats, stat_name) / 4)
319255736Sdavidch
320255736Sdavidch#define Q_STATS_OFFSET32(stat_name)                   \
321255736Sdavidch    (offsetof(struct bxe_eth_q_stats, stat_name) / 4)
322255736Sdavidch
323255736Sdavidchstatic const struct {
324255736Sdavidch    uint32_t offset;
325255736Sdavidch    uint32_t size;
326255736Sdavidch    uint32_t flags;
327255736Sdavidch#define STATS_FLAGS_PORT  1
328255736Sdavidch#define STATS_FLAGS_FUNC  2 /* MF only cares about function stats */
329255736Sdavidch#define STATS_FLAGS_BOTH  (STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
330255736Sdavidch    char string[STAT_NAME_LEN];
331255736Sdavidch} bxe_eth_stats_arr[] = {
332255736Sdavidch    { STATS_OFFSET32(total_bytes_received_hi),
333255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bytes" },
334255736Sdavidch    { STATS_OFFSET32(error_bytes_received_hi),
335255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_error_bytes" },
336255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_received_hi),
337255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
338255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_received_hi),
339255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
340255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_received_hi),
341255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
342255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
343255736Sdavidch                8, STATS_FLAGS_PORT, "rx_crc_errors" },
344255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
345255736Sdavidch                8, STATS_FLAGS_PORT, "rx_align_errors" },
346255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
347255736Sdavidch                8, STATS_FLAGS_PORT, "rx_undersize_packets" },
348255736Sdavidch    { STATS_OFFSET32(etherstatsoverrsizepkts_hi),
349255736Sdavidch                8, STATS_FLAGS_PORT, "rx_oversize_packets" },
350255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
351255736Sdavidch                8, STATS_FLAGS_PORT, "rx_fragments" },
352255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
353255736Sdavidch                8, STATS_FLAGS_PORT, "rx_jabbers" },
354255736Sdavidch    { STATS_OFFSET32(no_buff_discard_hi),
355255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_discards" },
356255736Sdavidch    { STATS_OFFSET32(mac_filter_discard),
357255736Sdavidch                4, STATS_FLAGS_PORT, "rx_filtered_packets" },
358255736Sdavidch    { STATS_OFFSET32(mf_tag_discard),
359255736Sdavidch                4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
360255736Sdavidch    { STATS_OFFSET32(pfc_frames_received_hi),
361255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_received" },
362255736Sdavidch    { STATS_OFFSET32(pfc_frames_sent_hi),
363255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_sent" },
364255736Sdavidch    { STATS_OFFSET32(brb_drop_hi),
365255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_discard" },
366255736Sdavidch    { STATS_OFFSET32(brb_truncate_hi),
367255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_truncate" },
368255736Sdavidch    { STATS_OFFSET32(pause_frames_received_hi),
369255736Sdavidch                8, STATS_FLAGS_PORT, "rx_pause_frames" },
370255736Sdavidch    { STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
371255736Sdavidch                8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
372255736Sdavidch    { STATS_OFFSET32(nig_timer_max),
373255736Sdavidch                4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
374255736Sdavidch    { STATS_OFFSET32(total_bytes_transmitted_hi),
375255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bytes" },
376255736Sdavidch    { STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
377255736Sdavidch                8, STATS_FLAGS_PORT, "tx_error_bytes" },
378255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_transmitted_hi),
379255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
380255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_transmitted_hi),
381255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
382255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
383255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
384255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
385255736Sdavidch                8, STATS_FLAGS_PORT, "tx_mac_errors" },
386255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
387255736Sdavidch                8, STATS_FLAGS_PORT, "tx_carrier_errors" },
388255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
389255736Sdavidch                8, STATS_FLAGS_PORT, "tx_single_collisions" },
390255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
391255736Sdavidch                8, STATS_FLAGS_PORT, "tx_multi_collisions" },
392255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
393255736Sdavidch                8, STATS_FLAGS_PORT, "tx_deferred" },
394255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
395255736Sdavidch                8, STATS_FLAGS_PORT, "tx_excess_collisions" },
396255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
397255736Sdavidch                8, STATS_FLAGS_PORT, "tx_late_collisions" },
398255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
399255736Sdavidch                8, STATS_FLAGS_PORT, "tx_total_collisions" },
400255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
401255736Sdavidch                8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
402255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
403255736Sdavidch                8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
404255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
405255736Sdavidch                8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
406255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
407255736Sdavidch                8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
408255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
409255736Sdavidch                8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
410255736Sdavidch    { STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
411255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
412255736Sdavidch    { STATS_OFFSET32(etherstatspktsover1522octets_hi),
413255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
414255736Sdavidch    { STATS_OFFSET32(pause_frames_sent_hi),
415255736Sdavidch                8, STATS_FLAGS_PORT, "tx_pause_frames" },
416255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregations_hi),
417255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregations" },
418255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregated_frames_hi),
419255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
420255736Sdavidch    { STATS_OFFSET32(total_tpa_bytes_hi),
421255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_bytes"},
422255736Sdavidch    { STATS_OFFSET32(eee_tx_lpi),
423255736Sdavidch                4, STATS_FLAGS_PORT, "eee_tx_lpi"},
424255736Sdavidch    { STATS_OFFSET32(rx_calls),
425255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_calls"},
426255736Sdavidch    { STATS_OFFSET32(rx_pkts),
427255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_pkts"},
428255736Sdavidch    { STATS_OFFSET32(rx_tpa_pkts),
429255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_tpa_pkts"},
430292638Sdavidcs    { STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
431292638Sdavidcs                4, STATS_FLAGS_FUNC, "rx_erroneous_jumbo_sge_pkts"},
432292638Sdavidcs    { STATS_OFFSET32(rx_bxe_service_rxsgl),
433292638Sdavidcs                4, STATS_FLAGS_FUNC, "rx_bxe_service_rxsgl"},
434283274Sdavidcs    { STATS_OFFSET32(rx_jumbo_sge_pkts),
435283274Sdavidcs                4, STATS_FLAGS_FUNC, "rx_jumbo_sge_pkts"},
436255736Sdavidch    { STATS_OFFSET32(rx_soft_errors),
437255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_soft_errors"},
438255736Sdavidch    { STATS_OFFSET32(rx_hw_csum_errors),
439255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_hw_csum_errors"},
440255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_ip),
441255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_ip"},
442255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
443255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_tcp_udp"},
444255736Sdavidch    { STATS_OFFSET32(rx_budget_reached),
445255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_budget_reached"},
446255736Sdavidch    { STATS_OFFSET32(tx_pkts),
447255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_pkts"},
448255736Sdavidch    { STATS_OFFSET32(tx_soft_errors),
449255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_soft_errors"},
450255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_ip),
451255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_ip"},
452255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_tcp),
453255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_tcp"},
454255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_udp),
455255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_udp"},
456255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso),
457255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso"},
458255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
459255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso_hdr_splits"},
460255736Sdavidch    { STATS_OFFSET32(tx_encap_failures),
461255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_encap_failures"},
462255736Sdavidch    { STATS_OFFSET32(tx_hw_queue_full),
463255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_queue_full"},
464255736Sdavidch    { STATS_OFFSET32(tx_hw_max_queue_depth),
465255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_max_queue_depth"},
466255736Sdavidch    { STATS_OFFSET32(tx_dma_mapping_failure),
467255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_dma_mapping_failure"},
468255736Sdavidch    { STATS_OFFSET32(tx_max_drbr_queue_depth),
469255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_max_drbr_queue_depth"},
470255736Sdavidch    { STATS_OFFSET32(tx_window_violation_std),
471255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_std"},
472255736Sdavidch    { STATS_OFFSET32(tx_window_violation_tso),
473255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_tso"},
474255736Sdavidch    { STATS_OFFSET32(tx_chain_lost_mbuf),
475255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_chain_lost_mbuf"},
476255736Sdavidch    { STATS_OFFSET32(tx_frames_deferred),
477255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_frames_deferred"},
478255736Sdavidch    { STATS_OFFSET32(tx_queue_xoff),
479255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_queue_xoff"},
480255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_attempts),
481255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_attempts"},
482255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_failures),
483255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_failures"},
484255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
485255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_alloc_failed"},
486255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
487255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_mapping_failed"},
488255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
489255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_alloc_failed"},
490255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
491255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_mapping_failed"},
492255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
493255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_alloc_failed"},
494255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
495255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_mapping_failed"},
496255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tx),
497255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_tx"},
498255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_rx),
499255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_rx"},
500255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_sge),
501255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_sge"},
502255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tpa),
503297873Sdavidcs                4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"},
504297873Sdavidcs    { STATS_OFFSET32(tx_queue_full_return),
505307972Sdavidcs                4, STATS_FLAGS_FUNC, "tx_queue_full_return"},
506315881Sdavidcs    { STATS_OFFSET32(bxe_tx_mq_sc_state_failures),
507315881Sdavidcs                4, STATS_FLAGS_FUNC, "bxe_tx_mq_sc_state_failures"},
508307972Sdavidcs    { STATS_OFFSET32(tx_request_link_down_failures),
509307972Sdavidcs                4, STATS_FLAGS_FUNC, "tx_request_link_down_failures"},
510307972Sdavidcs    { STATS_OFFSET32(bd_avail_too_less_failures),
511307972Sdavidcs                4, STATS_FLAGS_FUNC, "bd_avail_too_less_failures"},
512307972Sdavidcs    { STATS_OFFSET32(tx_mq_not_empty),
513315881Sdavidcs                4, STATS_FLAGS_FUNC, "tx_mq_not_empty"},
514315881Sdavidcs    { STATS_OFFSET32(nsegs_path1_errors),
515315881Sdavidcs                4, STATS_FLAGS_FUNC, "nsegs_path1_errors"},
516315881Sdavidcs    { STATS_OFFSET32(nsegs_path2_errors),
517315881Sdavidcs                4, STATS_FLAGS_FUNC, "nsegs_path2_errors"}
518307972Sdavidcs
519315881Sdavidcs
520255736Sdavidch};
521255736Sdavidch
522255736Sdavidchstatic const struct {
523255736Sdavidch    uint32_t offset;
524255736Sdavidch    uint32_t size;
525255736Sdavidch    char string[STAT_NAME_LEN];
526255736Sdavidch} bxe_eth_q_stats_arr[] = {
527255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_received_hi),
528255736Sdavidch                8, "rx_bytes" },
529255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
530255736Sdavidch                8, "rx_ucast_packets" },
531255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
532255736Sdavidch                8, "rx_mcast_packets" },
533255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
534255736Sdavidch                8, "rx_bcast_packets" },
535255736Sdavidch    { Q_STATS_OFFSET32(no_buff_discard_hi),
536255736Sdavidch                8, "rx_discards" },
537255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
538255736Sdavidch                8, "tx_bytes" },
539255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
540255736Sdavidch                8, "tx_ucast_packets" },
541255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
542255736Sdavidch                8, "tx_mcast_packets" },
543255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
544255736Sdavidch                8, "tx_bcast_packets" },
545255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
546255736Sdavidch                8, "tpa_aggregations" },
547255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
548255736Sdavidch                8, "tpa_aggregated_frames"},
549255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_bytes_hi),
550255736Sdavidch                8, "tpa_bytes"},
551255736Sdavidch    { Q_STATS_OFFSET32(rx_calls),
552255736Sdavidch                4, "rx_calls"},
553255736Sdavidch    { Q_STATS_OFFSET32(rx_pkts),
554255736Sdavidch                4, "rx_pkts"},
555255736Sdavidch    { Q_STATS_OFFSET32(rx_tpa_pkts),
556255736Sdavidch                4, "rx_tpa_pkts"},
557292638Sdavidcs    { Q_STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
558292638Sdavidcs                4, "rx_erroneous_jumbo_sge_pkts"},
559292638Sdavidcs    { Q_STATS_OFFSET32(rx_bxe_service_rxsgl),
560292638Sdavidcs                4, "rx_bxe_service_rxsgl"},
561283274Sdavidcs    { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
562283274Sdavidcs                4, "rx_jumbo_sge_pkts"},
563255736Sdavidch    { Q_STATS_OFFSET32(rx_soft_errors),
564255736Sdavidch                4, "rx_soft_errors"},
565255736Sdavidch    { Q_STATS_OFFSET32(rx_hw_csum_errors),
566255736Sdavidch                4, "rx_hw_csum_errors"},
567255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
568255736Sdavidch                4, "rx_ofld_frames_csum_ip"},
569255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
570255736Sdavidch                4, "rx_ofld_frames_csum_tcp_udp"},
571255736Sdavidch    { Q_STATS_OFFSET32(rx_budget_reached),
572255736Sdavidch                4, "rx_budget_reached"},
573255736Sdavidch    { Q_STATS_OFFSET32(tx_pkts),
574255736Sdavidch                4, "tx_pkts"},
575255736Sdavidch    { Q_STATS_OFFSET32(tx_soft_errors),
576255736Sdavidch                4, "tx_soft_errors"},
577255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
578255736Sdavidch                4, "tx_ofld_frames_csum_ip"},
579255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
580255736Sdavidch                4, "tx_ofld_frames_csum_tcp"},
581255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
582255736Sdavidch                4, "tx_ofld_frames_csum_udp"},
583255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso),
584255736Sdavidch                4, "tx_ofld_frames_lso"},
585255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
586255736Sdavidch                4, "tx_ofld_frames_lso_hdr_splits"},
587255736Sdavidch    { Q_STATS_OFFSET32(tx_encap_failures),
588255736Sdavidch                4, "tx_encap_failures"},
589255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_queue_full),
590255736Sdavidch                4, "tx_hw_queue_full"},
591255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
592255736Sdavidch                4, "tx_hw_max_queue_depth"},
593255736Sdavidch    { Q_STATS_OFFSET32(tx_dma_mapping_failure),
594255736Sdavidch                4, "tx_dma_mapping_failure"},
595255736Sdavidch    { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
596255736Sdavidch                4, "tx_max_drbr_queue_depth"},
597255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_std),
598255736Sdavidch                4, "tx_window_violation_std"},
599255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_tso),
600255736Sdavidch                4, "tx_window_violation_tso"},
601255736Sdavidch    { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
602255736Sdavidch                4, "tx_chain_lost_mbuf"},
603255736Sdavidch    { Q_STATS_OFFSET32(tx_frames_deferred),
604255736Sdavidch                4, "tx_frames_deferred"},
605255736Sdavidch    { Q_STATS_OFFSET32(tx_queue_xoff),
606255736Sdavidch                4, "tx_queue_xoff"},
607255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_attempts),
608255736Sdavidch                4, "mbuf_defrag_attempts"},
609255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_failures),
610255736Sdavidch                4, "mbuf_defrag_failures"},
611255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
612255736Sdavidch                4, "mbuf_rx_bd_alloc_failed"},
613255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
614255736Sdavidch                4, "mbuf_rx_bd_mapping_failed"},
615255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
616255736Sdavidch                4, "mbuf_rx_tpa_alloc_failed"},
617255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
618255736Sdavidch                4, "mbuf_rx_tpa_mapping_failed"},
619255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
620255736Sdavidch                4, "mbuf_rx_sge_alloc_failed"},
621255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
622255736Sdavidch                4, "mbuf_rx_sge_mapping_failed"},
623255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tx),
624255736Sdavidch                4, "mbuf_alloc_tx"},
625255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_rx),
626255736Sdavidch                4, "mbuf_alloc_rx"},
627255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_sge),
628255736Sdavidch                4, "mbuf_alloc_sge"},
629255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tpa),
630297873Sdavidcs                4, "mbuf_alloc_tpa"},
631297873Sdavidcs    { Q_STATS_OFFSET32(tx_queue_full_return),
632307972Sdavidcs                4, "tx_queue_full_return"},
633315881Sdavidcs    { Q_STATS_OFFSET32(bxe_tx_mq_sc_state_failures),
634315881Sdavidcs                4, "bxe_tx_mq_sc_state_failures"},
635307972Sdavidcs    { Q_STATS_OFFSET32(tx_request_link_down_failures),
636307972Sdavidcs                4, "tx_request_link_down_failures"},
637307972Sdavidcs    { Q_STATS_OFFSET32(bd_avail_too_less_failures),
638307972Sdavidcs                4, "bd_avail_too_less_failures"},
639307972Sdavidcs    { Q_STATS_OFFSET32(tx_mq_not_empty),
640315881Sdavidcs                4, "tx_mq_not_empty"},
641315881Sdavidcs    { Q_STATS_OFFSET32(nsegs_path1_errors),
642315881Sdavidcs                4, "nsegs_path1_errors"},
643315881Sdavidcs    { Q_STATS_OFFSET32(nsegs_path2_errors),
644315881Sdavidcs                4, "nsegs_path2_errors"}
645307972Sdavidcs
646315881Sdavidcs
647255736Sdavidch};
648255736Sdavidch
649255736Sdavidch#define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
650255736Sdavidch#define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
651255736Sdavidch
652255736Sdavidch
653255736Sdavidchstatic void    bxe_cmng_fns_init(struct bxe_softc *sc,
654255736Sdavidch                                 uint8_t          read_cfg,
655255736Sdavidch                                 uint8_t          cmng_type);
656255736Sdavidchstatic int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
657255736Sdavidchstatic void    storm_memset_cmng(struct bxe_softc *sc,
658255736Sdavidch                                 struct cmng_init *cmng,
659255736Sdavidch                                 uint8_t          port);
660255736Sdavidchstatic void    bxe_set_reset_global(struct bxe_softc *sc);
661255736Sdavidchstatic void    bxe_set_reset_in_progress(struct bxe_softc *sc);
662255736Sdavidchstatic uint8_t bxe_reset_is_done(struct bxe_softc *sc,
663255736Sdavidch                                 int              engine);
664255736Sdavidchstatic uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
665255736Sdavidchstatic uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
666255736Sdavidch                                   uint8_t          *global,
667255736Sdavidch                                   uint8_t          print);
668255736Sdavidchstatic void    bxe_int_disable(struct bxe_softc *sc);
669255736Sdavidchstatic int     bxe_release_leader_lock(struct bxe_softc *sc);
670255736Sdavidchstatic void    bxe_pf_disable(struct bxe_softc *sc);
671255736Sdavidchstatic void    bxe_free_fp_buffers(struct bxe_softc *sc);
672255736Sdavidchstatic inline void bxe_update_rx_prod(struct bxe_softc    *sc,
673255736Sdavidch                                      struct bxe_fastpath *fp,
674255736Sdavidch                                      uint16_t            rx_bd_prod,
675255736Sdavidch                                      uint16_t            rx_cq_prod,
676255736Sdavidch                                      uint16_t            rx_sge_prod);
677255736Sdavidchstatic void    bxe_link_report_locked(struct bxe_softc *sc);
678255736Sdavidchstatic void    bxe_link_report(struct bxe_softc *sc);
679255736Sdavidchstatic void    bxe_link_status_update(struct bxe_softc *sc);
680255736Sdavidchstatic void    bxe_periodic_callout_func(void *xsc);
681255736Sdavidchstatic void    bxe_periodic_start(struct bxe_softc *sc);
682255736Sdavidchstatic void    bxe_periodic_stop(struct bxe_softc *sc);
683255736Sdavidchstatic int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
684255736Sdavidch                                    uint16_t prev_index,
685255736Sdavidch                                    uint16_t index);
686255736Sdavidchstatic int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
687255736Sdavidch                                     int                 queue);
688255736Sdavidchstatic int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
689255736Sdavidch                                     uint16_t            index);
690255736Sdavidchstatic uint8_t bxe_txeof(struct bxe_softc *sc,
691255736Sdavidch                         struct bxe_fastpath *fp);
692255736Sdavidchstatic void    bxe_task_fp(struct bxe_fastpath *fp);
693255736Sdavidchstatic __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
694255736Sdavidch                                     struct mbuf      *m,
695255736Sdavidch                                     uint8_t          contents);
696255736Sdavidchstatic int     bxe_alloc_mem(struct bxe_softc *sc);
697255736Sdavidchstatic void    bxe_free_mem(struct bxe_softc *sc);
698255736Sdavidchstatic int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
699255736Sdavidchstatic void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
700255736Sdavidchstatic int     bxe_interrupt_attach(struct bxe_softc *sc);
701255736Sdavidchstatic void    bxe_interrupt_detach(struct bxe_softc *sc);
702255736Sdavidchstatic void    bxe_set_rx_mode(struct bxe_softc *sc);
703255736Sdavidchstatic int     bxe_init_locked(struct bxe_softc *sc);
704255736Sdavidchstatic int     bxe_stop_locked(struct bxe_softc *sc);
705339881Sdavidcsstatic void    bxe_sp_err_timeout_task(void *arg, int pending);
706339881Sdavidcsvoid           bxe_parity_recover(struct bxe_softc *sc);
707339881Sdavidcsvoid           bxe_handle_error(struct bxe_softc *sc);
708255736Sdavidchstatic __noinline int bxe_nic_load(struct bxe_softc *sc,
709255736Sdavidch                                   int              load_mode);
710255736Sdavidchstatic __noinline int bxe_nic_unload(struct bxe_softc *sc,
711255736Sdavidch                                     uint32_t         unload_mode,
712255736Sdavidch                                     uint8_t          keep_link);
713255736Sdavidch
714255736Sdavidchstatic void bxe_handle_sp_tq(void *context, int pending);
715255736Sdavidchstatic void bxe_handle_fp_tq(void *context, int pending);
716255736Sdavidch
717292639Sdavidcsstatic int bxe_add_cdev(struct bxe_softc *sc);
718292639Sdavidcsstatic void bxe_del_cdev(struct bxe_softc *sc);
719315881Sdavidcsint bxe_grc_dump(struct bxe_softc *sc);
720296579Sdavidcsstatic int bxe_alloc_buf_rings(struct bxe_softc *sc);
721296579Sdavidcsstatic void bxe_free_buf_rings(struct bxe_softc *sc);
722255736Sdavidch
723255736Sdavidch/* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
724255736Sdavidchuint32_t
725255736Sdavidchcalc_crc32(uint8_t  *crc32_packet,
726255736Sdavidch           uint32_t crc32_length,
727255736Sdavidch           uint32_t crc32_seed,
728255736Sdavidch           uint8_t  complement)
729255736Sdavidch{
730255736Sdavidch   uint32_t byte         = 0;
731255736Sdavidch   uint32_t bit          = 0;
732255736Sdavidch   uint8_t  msb          = 0;
733255736Sdavidch   uint32_t temp         = 0;
734255736Sdavidch   uint32_t shft         = 0;
735255736Sdavidch   uint8_t  current_byte = 0;
736255736Sdavidch   uint32_t crc32_result = crc32_seed;
737255736Sdavidch   const uint32_t CRC32_POLY = 0x1edc6f41;
738255736Sdavidch
739255736Sdavidch   if ((crc32_packet == NULL) ||
740255736Sdavidch       (crc32_length == 0) ||
741255736Sdavidch       ((crc32_length % 8) != 0))
742255736Sdavidch    {
743255736Sdavidch        return (crc32_result);
744255736Sdavidch    }
745255736Sdavidch
746255736Sdavidch    for (byte = 0; byte < crc32_length; byte = byte + 1)
747255736Sdavidch    {
748255736Sdavidch        current_byte = crc32_packet[byte];
749255736Sdavidch        for (bit = 0; bit < 8; bit = bit + 1)
750255736Sdavidch        {
751255736Sdavidch            /* msb = crc32_result[31]; */
752255736Sdavidch            msb = (uint8_t)(crc32_result >> 31);
753255736Sdavidch
754255736Sdavidch            crc32_result = crc32_result << 1;
755255736Sdavidch
756255736Sdavidch            /* it (msb != current_byte[bit]) */
757255736Sdavidch            if (msb != (0x1 & (current_byte >> bit)))
758255736Sdavidch            {
759255736Sdavidch                crc32_result = crc32_result ^ CRC32_POLY;
760255736Sdavidch                /* crc32_result[0] = 1 */
761255736Sdavidch                crc32_result |= 1;
762255736Sdavidch            }
763255736Sdavidch        }
764255736Sdavidch    }
765255736Sdavidch
766255736Sdavidch    /* Last step is to:
767255736Sdavidch     * 1. "mirror" every bit
768255736Sdavidch     * 2. swap the 4 bytes
769255736Sdavidch     * 3. complement each bit
770255736Sdavidch     */
771255736Sdavidch
772255736Sdavidch    /* Mirror */
773255736Sdavidch    temp = crc32_result;
774255736Sdavidch    shft = sizeof(crc32_result) * 8 - 1;
775255736Sdavidch
776255736Sdavidch    for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
777255736Sdavidch    {
778255736Sdavidch        temp <<= 1;
779255736Sdavidch        temp |= crc32_result & 1;
780255736Sdavidch        shft-- ;
781255736Sdavidch    }
782255736Sdavidch
783255736Sdavidch    /* temp[31-bit] = crc32_result[bit] */
784255736Sdavidch    temp <<= shft;
785255736Sdavidch
786255736Sdavidch    /* Swap */
787255736Sdavidch    /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
788255736Sdavidch    {
789255736Sdavidch        uint32_t t0, t1, t2, t3;
790255736Sdavidch        t0 = (0x000000ff & (temp >> 24));
791255736Sdavidch        t1 = (0x0000ff00 & (temp >> 8));
792255736Sdavidch        t2 = (0x00ff0000 & (temp << 8));
793255736Sdavidch        t3 = (0xff000000 & (temp << 24));
794255736Sdavidch        crc32_result = t0 | t1 | t2 | t3;
795255736Sdavidch    }
796255736Sdavidch
797255736Sdavidch    /* Complement */
798255736Sdavidch    if (complement)
799255736Sdavidch    {
800255736Sdavidch        crc32_result = ~crc32_result;
801255736Sdavidch    }
802255736Sdavidch
803255736Sdavidch    return (crc32_result);
804255736Sdavidch}
805255736Sdavidch
806255736Sdavidchint
807255736Sdavidchbxe_test_bit(int                    nr,
808255736Sdavidch             volatile unsigned long *addr)
809255736Sdavidch{
810255736Sdavidch    return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
811255736Sdavidch}
812255736Sdavidch
813255736Sdavidchvoid
814255736Sdavidchbxe_set_bit(unsigned int           nr,
815255736Sdavidch            volatile unsigned long *addr)
816255736Sdavidch{
817255736Sdavidch    atomic_set_acq_long(addr, (1 << nr));
818255736Sdavidch}
819255736Sdavidch
820255736Sdavidchvoid
821255736Sdavidchbxe_clear_bit(int                    nr,
822255736Sdavidch              volatile unsigned long *addr)
823255736Sdavidch{
824255736Sdavidch    atomic_clear_acq_long(addr, (1 << nr));
825255736Sdavidch}
826255736Sdavidch
827255736Sdavidchint
828255736Sdavidchbxe_test_and_set_bit(int                    nr,
829255736Sdavidch                       volatile unsigned long *addr)
830255736Sdavidch{
831255736Sdavidch    unsigned long x;
832255736Sdavidch    nr = (1 << nr);
833255736Sdavidch    do {
834255736Sdavidch        x = *addr;
835255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
836255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
837255736Sdavidch    return (x & nr);
838255736Sdavidch}
839255736Sdavidch
840255736Sdavidchint
841255736Sdavidchbxe_test_and_clear_bit(int                    nr,
842255736Sdavidch                       volatile unsigned long *addr)
843255736Sdavidch{
844255736Sdavidch    unsigned long x;
845255736Sdavidch    nr = (1 << nr);
846255736Sdavidch    do {
847255736Sdavidch        x = *addr;
848255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
849255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
850255736Sdavidch    return (x & nr);
851255736Sdavidch}
852255736Sdavidch
853255736Sdavidchint
854255736Sdavidchbxe_cmpxchg(volatile int *addr,
855255736Sdavidch            int          old,
856255736Sdavidch            int          new)
857255736Sdavidch{
858255736Sdavidch    int x;
859255736Sdavidch    do {
860255736Sdavidch        x = *addr;
861255736Sdavidch    } while (atomic_cmpset_acq_int(addr, old, new) == 0);
862255736Sdavidch    return (x);
863255736Sdavidch}
864255736Sdavidch
865255736Sdavidch/*
866255736Sdavidch * Get DMA memory from the OS.
867255736Sdavidch *
868255736Sdavidch * Validates that the OS has provided DMA buffers in response to a
869255736Sdavidch * bus_dmamap_load call and saves the physical address of those buffers.
870255736Sdavidch * When the callback is used the OS will return 0 for the mapping function
871255736Sdavidch * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
872255736Sdavidch * failures back to the caller.
873255736Sdavidch *
874255736Sdavidch * Returns:
875255736Sdavidch *   Nothing.
876255736Sdavidch */
877255736Sdavidchstatic void
878255736Sdavidchbxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
879255736Sdavidch{
880255736Sdavidch    struct bxe_dma *dma = arg;
881255736Sdavidch
882255736Sdavidch    if (error) {
883255736Sdavidch        dma->paddr = 0;
884255736Sdavidch        dma->nseg  = 0;
885255736Sdavidch        BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
886255736Sdavidch    } else {
887255736Sdavidch        dma->paddr = segs->ds_addr;
888255736Sdavidch        dma->nseg  = nseg;
889255736Sdavidch    }
890255736Sdavidch}
891255736Sdavidch
892255736Sdavidch/*
893255736Sdavidch * Allocate a block of memory and map it for DMA. No partial completions
894255736Sdavidch * allowed and release any resources acquired if we can't acquire all
895255736Sdavidch * resources.
896255736Sdavidch *
897255736Sdavidch * Returns:
898255736Sdavidch *   0 = Success, !0 = Failure
899255736Sdavidch */
900255736Sdavidchint
901255736Sdavidchbxe_dma_alloc(struct bxe_softc *sc,
902255736Sdavidch              bus_size_t       size,
903255736Sdavidch              struct bxe_dma   *dma,
904255736Sdavidch              const char       *msg)
905255736Sdavidch{
906255736Sdavidch    int rc;
907255736Sdavidch
908255736Sdavidch    if (dma->size > 0) {
909256299Sedavis        BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
910256299Sedavis              (unsigned long)dma->size);
911255736Sdavidch        return (1);
912255736Sdavidch    }
913255736Sdavidch
914255736Sdavidch    memset(dma, 0, sizeof(*dma)); /* sanity */
915255736Sdavidch    dma->sc   = sc;
916255736Sdavidch    dma->size = size;
917255736Sdavidch    snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
918255736Sdavidch
919255736Sdavidch    rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
920255736Sdavidch                            BCM_PAGE_SIZE,      /* alignment */
921255736Sdavidch                            0,                  /* boundary limit */
922255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted low */
923255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted hi */
924255736Sdavidch                            NULL,               /* addr filter() */
925255736Sdavidch                            NULL,               /* addr filter() arg */
926255736Sdavidch                            size,               /* max map size */
927255736Sdavidch                            1,                  /* num discontinuous */
928255736Sdavidch                            size,               /* max seg size */
929255736Sdavidch                            BUS_DMA_ALLOCNOW,   /* flags */
930255736Sdavidch                            NULL,               /* lock() */
931255736Sdavidch                            NULL,               /* lock() arg */
932255736Sdavidch                            &dma->tag);         /* returned dma tag */
933255736Sdavidch    if (rc != 0) {
934255736Sdavidch        BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
935255736Sdavidch        memset(dma, 0, sizeof(*dma));
936255736Sdavidch        return (1);
937255736Sdavidch    }
938255736Sdavidch
939255736Sdavidch    rc = bus_dmamem_alloc(dma->tag,
940255736Sdavidch                          (void **)&dma->vaddr,
941255736Sdavidch                          (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
942255736Sdavidch                          &dma->map);
943255736Sdavidch    if (rc != 0) {
944255736Sdavidch        BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
945255736Sdavidch        bus_dma_tag_destroy(dma->tag);
946255736Sdavidch        memset(dma, 0, sizeof(*dma));
947255736Sdavidch        return (1);
948255736Sdavidch    }
949255736Sdavidch
950255736Sdavidch    rc = bus_dmamap_load(dma->tag,
951255736Sdavidch                         dma->map,
952255736Sdavidch                         dma->vaddr,
953255736Sdavidch                         size,
954255736Sdavidch                         bxe_dma_map_addr, /* BLOGD in here */
955255736Sdavidch                         dma,
956255736Sdavidch                         BUS_DMA_NOWAIT);
957255736Sdavidch    if (rc != 0) {
958255736Sdavidch        BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
959255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
960255736Sdavidch        bus_dma_tag_destroy(dma->tag);
961255736Sdavidch        memset(dma, 0, sizeof(*dma));
962255736Sdavidch        return (1);
963255736Sdavidch    }
964255736Sdavidch
965255736Sdavidch    return (0);
966255736Sdavidch}
967255736Sdavidch
968255736Sdavidchvoid
969255736Sdavidchbxe_dma_free(struct bxe_softc *sc,
970255736Sdavidch             struct bxe_dma   *dma)
971255736Sdavidch{
972255736Sdavidch    if (dma->size > 0) {
973255736Sdavidch        DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
974255736Sdavidch
975255736Sdavidch        bus_dmamap_sync(dma->tag, dma->map,
976255736Sdavidch                        (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
977255736Sdavidch        bus_dmamap_unload(dma->tag, dma->map);
978255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
979255736Sdavidch        bus_dma_tag_destroy(dma->tag);
980255736Sdavidch    }
981255736Sdavidch
982255736Sdavidch    memset(dma, 0, sizeof(*dma));
983255736Sdavidch}
984255736Sdavidch
985255736Sdavidch/*
986255736Sdavidch * These indirect read and write routines are only during init.
987255736Sdavidch * The locking is handled by the MCP.
988255736Sdavidch */
989255736Sdavidch
990255736Sdavidchvoid
991255736Sdavidchbxe_reg_wr_ind(struct bxe_softc *sc,
992255736Sdavidch               uint32_t         addr,
993255736Sdavidch               uint32_t         val)
994255736Sdavidch{
995255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
996255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
997255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
998255736Sdavidch}
999255736Sdavidch
1000255736Sdavidchuint32_t
1001255736Sdavidchbxe_reg_rd_ind(struct bxe_softc *sc,
1002255736Sdavidch               uint32_t         addr)
1003255736Sdavidch{
1004255736Sdavidch    uint32_t val;
1005255736Sdavidch
1006255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
1007255736Sdavidch    val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
1008255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
1009255736Sdavidch
1010255736Sdavidch    return (val);
1011255736Sdavidch}
1012255736Sdavidch
1013255736Sdavidchstatic int
1014255736Sdavidchbxe_acquire_hw_lock(struct bxe_softc *sc,
1015255736Sdavidch                    uint32_t         resource)
1016255736Sdavidch{
1017255736Sdavidch    uint32_t lock_status;
1018255736Sdavidch    uint32_t resource_bit = (1 << resource);
1019255736Sdavidch    int func = SC_FUNC(sc);
1020255736Sdavidch    uint32_t hw_lock_control_reg;
1021255736Sdavidch    int cnt;
1022255736Sdavidch
1023255736Sdavidch    /* validate the resource is within range */
1024255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1025295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1026295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
1027255736Sdavidch        return (-1);
1028255736Sdavidch    }
1029255736Sdavidch
1030255736Sdavidch    if (func <= 5) {
1031255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1032255736Sdavidch    } else {
1033255736Sdavidch        hw_lock_control_reg =
1034255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1035255736Sdavidch    }
1036255736Sdavidch
1037255736Sdavidch    /* validate the resource is not already taken */
1038255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1039255736Sdavidch    if (lock_status & resource_bit) {
1040295830Sdavidcs        BLOGE(sc, "resource (0x%x) in use (status 0x%x bit 0x%x)\n",
1041295830Sdavidcs              resource, lock_status, resource_bit);
1042255736Sdavidch        return (-1);
1043255736Sdavidch    }
1044255736Sdavidch
1045255736Sdavidch    /* try every 5ms for 5 seconds */
1046255736Sdavidch    for (cnt = 0; cnt < 1000; cnt++) {
1047255736Sdavidch        REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1048255736Sdavidch        lock_status = REG_RD(sc, hw_lock_control_reg);
1049255736Sdavidch        if (lock_status & resource_bit) {
1050255736Sdavidch            return (0);
1051255736Sdavidch        }
1052255736Sdavidch        DELAY(5000);
1053255736Sdavidch    }
1054255736Sdavidch
1055295830Sdavidcs    BLOGE(sc, "Resource 0x%x resource_bit 0x%x lock timeout!\n",
1056295830Sdavidcs        resource, resource_bit);
1057255736Sdavidch    return (-1);
1058255736Sdavidch}
1059255736Sdavidch
1060255736Sdavidchstatic int
1061255736Sdavidchbxe_release_hw_lock(struct bxe_softc *sc,
1062255736Sdavidch                    uint32_t         resource)
1063255736Sdavidch{
1064255736Sdavidch    uint32_t lock_status;
1065255736Sdavidch    uint32_t resource_bit = (1 << resource);
1066255736Sdavidch    int func = SC_FUNC(sc);
1067255736Sdavidch    uint32_t hw_lock_control_reg;
1068255736Sdavidch
1069255736Sdavidch    /* validate the resource is within range */
1070255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1071295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1072295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
1073255736Sdavidch        return (-1);
1074255736Sdavidch    }
1075255736Sdavidch
1076255736Sdavidch    if (func <= 5) {
1077255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1078255736Sdavidch    } else {
1079255736Sdavidch        hw_lock_control_reg =
1080255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1081255736Sdavidch    }
1082255736Sdavidch
1083255736Sdavidch    /* validate the resource is currently taken */
1084255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1085255736Sdavidch    if (!(lock_status & resource_bit)) {
1086295830Sdavidcs        BLOGE(sc, "resource (0x%x) not in use (status 0x%x bit 0x%x)\n",
1087295830Sdavidcs              resource, lock_status, resource_bit);
1088255736Sdavidch        return (-1);
1089255736Sdavidch    }
1090255736Sdavidch
1091255736Sdavidch    REG_WR(sc, hw_lock_control_reg, resource_bit);
1092255736Sdavidch    return (0);
1093255736Sdavidch}
1094284335Sdavidcsstatic void bxe_acquire_phy_lock(struct bxe_softc *sc)
1095284335Sdavidcs{
1096284335Sdavidcs	BXE_PHY_LOCK(sc);
1097284335Sdavidcs	bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1098284335Sdavidcs}
1099255736Sdavidch
1100284335Sdavidcsstatic void bxe_release_phy_lock(struct bxe_softc *sc)
1101284335Sdavidcs{
1102284335Sdavidcs	bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1103284335Sdavidcs	BXE_PHY_UNLOCK(sc);
1104284335Sdavidcs}
1105255736Sdavidch/*
1106255736Sdavidch * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1107255736Sdavidch * had we done things the other way around, if two pfs from the same port
1108255736Sdavidch * would attempt to access nvram at the same time, we could run into a
1109255736Sdavidch * scenario such as:
1110255736Sdavidch * pf A takes the port lock.
1111255736Sdavidch * pf B succeeds in taking the same lock since they are from the same port.
1112255736Sdavidch * pf A takes the per pf misc lock. Performs eeprom access.
1113255736Sdavidch * pf A finishes. Unlocks the per pf misc lock.
1114255736Sdavidch * Pf B takes the lock and proceeds to perform it's own access.
1115255736Sdavidch * pf A unlocks the per port lock, while pf B is still working (!).
1116255736Sdavidch * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1117255736Sdavidch * access corrupted by pf B).*
1118255736Sdavidch */
1119255736Sdavidchstatic int
1120255736Sdavidchbxe_acquire_nvram_lock(struct bxe_softc *sc)
1121255736Sdavidch{
1122255736Sdavidch    int port = SC_PORT(sc);
1123255736Sdavidch    int count, i;
1124255736Sdavidch    uint32_t val = 0;
1125255736Sdavidch
1126255736Sdavidch    /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1127255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1128255736Sdavidch
1129255736Sdavidch    /* adjust timeout for emulation/FPGA */
1130255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1131255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1132255736Sdavidch        count *= 100;
1133255736Sdavidch    }
1134255736Sdavidch
1135255736Sdavidch    /* request access to nvram interface */
1136255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1137255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1138255736Sdavidch
1139255736Sdavidch    for (i = 0; i < count*10; i++) {
1140255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1141255736Sdavidch        if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1142255736Sdavidch            break;
1143255736Sdavidch        }
1144255736Sdavidch
1145255736Sdavidch        DELAY(5);
1146255736Sdavidch    }
1147255736Sdavidch
1148255736Sdavidch    if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1149295830Sdavidcs        BLOGE(sc, "Cannot get access to nvram interface "
1150295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1151295830Sdavidcs            port, val);
1152255736Sdavidch        return (-1);
1153255736Sdavidch    }
1154255736Sdavidch
1155255736Sdavidch    return (0);
1156255736Sdavidch}
1157255736Sdavidch
1158255736Sdavidchstatic int
1159255736Sdavidchbxe_release_nvram_lock(struct bxe_softc *sc)
1160255736Sdavidch{
1161255736Sdavidch    int port = SC_PORT(sc);
1162255736Sdavidch    int count, i;
1163255736Sdavidch    uint32_t val = 0;
1164255736Sdavidch
1165255736Sdavidch    /* adjust timeout for emulation/FPGA */
1166255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1167255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1168255736Sdavidch        count *= 100;
1169255736Sdavidch    }
1170255736Sdavidch
1171255736Sdavidch    /* relinquish nvram interface */
1172255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1173255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1174255736Sdavidch
1175255736Sdavidch    for (i = 0; i < count*10; i++) {
1176255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1177255736Sdavidch        if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1178255736Sdavidch            break;
1179255736Sdavidch        }
1180255736Sdavidch
1181255736Sdavidch        DELAY(5);
1182255736Sdavidch    }
1183255736Sdavidch
1184255736Sdavidch    if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1185295830Sdavidcs        BLOGE(sc, "Cannot free access to nvram interface "
1186295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1187295830Sdavidcs            port, val);
1188255736Sdavidch        return (-1);
1189255736Sdavidch    }
1190255736Sdavidch
1191255736Sdavidch    /* release HW lock: protect against other PFs in PF Direct Assignment */
1192255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1193255736Sdavidch
1194255736Sdavidch    return (0);
1195255736Sdavidch}
1196255736Sdavidch
1197255736Sdavidchstatic void
1198255736Sdavidchbxe_enable_nvram_access(struct bxe_softc *sc)
1199255736Sdavidch{
1200255736Sdavidch    uint32_t val;
1201255736Sdavidch
1202255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1203255736Sdavidch
1204255736Sdavidch    /* enable both bits, even on read */
1205255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1206255736Sdavidch           (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1207255736Sdavidch}
1208255736Sdavidch
1209255736Sdavidchstatic void
1210255736Sdavidchbxe_disable_nvram_access(struct bxe_softc *sc)
1211255736Sdavidch{
1212255736Sdavidch    uint32_t val;
1213255736Sdavidch
1214255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1215255736Sdavidch
1216255736Sdavidch    /* disable both bits, even after read */
1217255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1218255736Sdavidch           (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1219255736Sdavidch                    MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1220255736Sdavidch}
1221255736Sdavidch
1222255736Sdavidchstatic int
1223255736Sdavidchbxe_nvram_read_dword(struct bxe_softc *sc,
1224255736Sdavidch                     uint32_t         offset,
1225255736Sdavidch                     uint32_t         *ret_val,
1226255736Sdavidch                     uint32_t         cmd_flags)
1227255736Sdavidch{
1228255736Sdavidch    int count, i, rc;
1229255736Sdavidch    uint32_t val;
1230255736Sdavidch
1231255736Sdavidch    /* build the command word */
1232255736Sdavidch    cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1233255736Sdavidch
1234255736Sdavidch    /* need to clear DONE bit separately */
1235255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1236255736Sdavidch
1237255736Sdavidch    /* address of the NVRAM to read from */
1238255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1239255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1240255736Sdavidch
1241255736Sdavidch    /* issue a read command */
1242255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1243255736Sdavidch
1244255736Sdavidch    /* adjust timeout for emulation/FPGA */
1245255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1246255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1247255736Sdavidch        count *= 100;
1248255736Sdavidch    }
1249255736Sdavidch
1250255736Sdavidch    /* wait for completion */
1251255736Sdavidch    *ret_val = 0;
1252255736Sdavidch    rc = -1;
1253255736Sdavidch    for (i = 0; i < count; i++) {
1254255736Sdavidch        DELAY(5);
1255255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1256255736Sdavidch
1257255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1258255736Sdavidch            val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1259255736Sdavidch            /* we read nvram data in cpu order
1260255736Sdavidch             * but ethtool sees it as an array of bytes
1261255736Sdavidch             * converting to big-endian will do the work
1262255736Sdavidch             */
1263255736Sdavidch            *ret_val = htobe32(val);
1264255736Sdavidch            rc = 0;
1265255736Sdavidch            break;
1266255736Sdavidch        }
1267255736Sdavidch    }
1268255736Sdavidch
1269255736Sdavidch    if (rc == -1) {
1270295830Sdavidcs        BLOGE(sc, "nvram read timeout expired "
1271295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1272295830Sdavidcs            offset, cmd_flags, val);
1273255736Sdavidch    }
1274255736Sdavidch
1275255736Sdavidch    return (rc);
1276255736Sdavidch}
1277255736Sdavidch
1278255736Sdavidchstatic int
1279255736Sdavidchbxe_nvram_read(struct bxe_softc *sc,
1280255736Sdavidch               uint32_t         offset,
1281255736Sdavidch               uint8_t          *ret_buf,
1282255736Sdavidch               int              buf_size)
1283255736Sdavidch{
1284255736Sdavidch    uint32_t cmd_flags;
1285255736Sdavidch    uint32_t val;
1286255736Sdavidch    int rc;
1287255736Sdavidch
1288255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1289255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1290255736Sdavidch              offset, buf_size);
1291255736Sdavidch        return (-1);
1292255736Sdavidch    }
1293255736Sdavidch
1294255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1295255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1296255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1297255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1298255736Sdavidch        return (-1);
1299255736Sdavidch    }
1300255736Sdavidch
1301255736Sdavidch    /* request access to nvram interface */
1302255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1303255736Sdavidch    if (rc) {
1304255736Sdavidch        return (rc);
1305255736Sdavidch    }
1306255736Sdavidch
1307255736Sdavidch    /* enable access to nvram interface */
1308255736Sdavidch    bxe_enable_nvram_access(sc);
1309255736Sdavidch
1310255736Sdavidch    /* read the first word(s) */
1311255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1312255736Sdavidch    while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1313255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1314255736Sdavidch        memcpy(ret_buf, &val, 4);
1315255736Sdavidch
1316255736Sdavidch        /* advance to the next dword */
1317255736Sdavidch        offset += sizeof(uint32_t);
1318255736Sdavidch        ret_buf += sizeof(uint32_t);
1319255736Sdavidch        buf_size -= sizeof(uint32_t);
1320255736Sdavidch        cmd_flags = 0;
1321255736Sdavidch    }
1322255736Sdavidch
1323255736Sdavidch    if (rc == 0) {
1324255736Sdavidch        cmd_flags |= MCPR_NVM_COMMAND_LAST;
1325255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1326255736Sdavidch        memcpy(ret_buf, &val, 4);
1327255736Sdavidch    }
1328255736Sdavidch
1329255736Sdavidch    /* disable access to nvram interface */
1330255736Sdavidch    bxe_disable_nvram_access(sc);
1331255736Sdavidch    bxe_release_nvram_lock(sc);
1332255736Sdavidch
1333255736Sdavidch    return (rc);
1334255736Sdavidch}
1335255736Sdavidch
1336255736Sdavidchstatic int
1337255736Sdavidchbxe_nvram_write_dword(struct bxe_softc *sc,
1338255736Sdavidch                      uint32_t         offset,
1339255736Sdavidch                      uint32_t         val,
1340255736Sdavidch                      uint32_t         cmd_flags)
1341255736Sdavidch{
1342255736Sdavidch    int count, i, rc;
1343255736Sdavidch
1344255736Sdavidch    /* build the command word */
1345255736Sdavidch    cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1346255736Sdavidch
1347255736Sdavidch    /* need to clear DONE bit separately */
1348255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1349255736Sdavidch
1350255736Sdavidch    /* write the data */
1351255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1352255736Sdavidch
1353255736Sdavidch    /* address of the NVRAM to write to */
1354255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1355255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1356255736Sdavidch
1357255736Sdavidch    /* issue the write command */
1358255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1359255736Sdavidch
1360255736Sdavidch    /* adjust timeout for emulation/FPGA */
1361255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1362255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1363255736Sdavidch        count *= 100;
1364255736Sdavidch    }
1365255736Sdavidch
1366255736Sdavidch    /* wait for completion */
1367255736Sdavidch    rc = -1;
1368255736Sdavidch    for (i = 0; i < count; i++) {
1369255736Sdavidch        DELAY(5);
1370255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1371255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1372255736Sdavidch            rc = 0;
1373255736Sdavidch            break;
1374255736Sdavidch        }
1375255736Sdavidch    }
1376255736Sdavidch
1377255736Sdavidch    if (rc == -1) {
1378295830Sdavidcs        BLOGE(sc, "nvram write timeout expired "
1379295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1380295830Sdavidcs            offset, cmd_flags, val);
1381255736Sdavidch    }
1382255736Sdavidch
1383255736Sdavidch    return (rc);
1384255736Sdavidch}
1385255736Sdavidch
1386255736Sdavidch#define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1387255736Sdavidch
1388255736Sdavidchstatic int
1389255736Sdavidchbxe_nvram_write1(struct bxe_softc *sc,
1390255736Sdavidch                 uint32_t         offset,
1391255736Sdavidch                 uint8_t          *data_buf,
1392255736Sdavidch                 int              buf_size)
1393255736Sdavidch{
1394255736Sdavidch    uint32_t cmd_flags;
1395255736Sdavidch    uint32_t align_offset;
1396255736Sdavidch    uint32_t val;
1397255736Sdavidch    int rc;
1398255736Sdavidch
1399255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1400255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1401255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1402255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1403255736Sdavidch        return (-1);
1404255736Sdavidch    }
1405255736Sdavidch
1406255736Sdavidch    /* request access to nvram interface */
1407255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1408255736Sdavidch    if (rc) {
1409255736Sdavidch        return (rc);
1410255736Sdavidch    }
1411255736Sdavidch
1412255736Sdavidch    /* enable access to nvram interface */
1413255736Sdavidch    bxe_enable_nvram_access(sc);
1414255736Sdavidch
1415255736Sdavidch    cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1416255736Sdavidch    align_offset = (offset & ~0x03);
1417255736Sdavidch    rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1418255736Sdavidch
1419255736Sdavidch    if (rc == 0) {
1420255736Sdavidch        val &= ~(0xff << BYTE_OFFSET(offset));
1421255736Sdavidch        val |= (*data_buf << BYTE_OFFSET(offset));
1422255736Sdavidch
1423255736Sdavidch        /* nvram data is returned as an array of bytes
1424255736Sdavidch         * convert it back to cpu order
1425255736Sdavidch         */
1426255736Sdavidch        val = be32toh(val);
1427255736Sdavidch
1428255736Sdavidch        rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1429255736Sdavidch    }
1430255736Sdavidch
1431255736Sdavidch    /* disable access to nvram interface */
1432255736Sdavidch    bxe_disable_nvram_access(sc);
1433255736Sdavidch    bxe_release_nvram_lock(sc);
1434255736Sdavidch
1435255736Sdavidch    return (rc);
1436255736Sdavidch}
1437255736Sdavidch
1438255736Sdavidchstatic int
1439255736Sdavidchbxe_nvram_write(struct bxe_softc *sc,
1440255736Sdavidch                uint32_t         offset,
1441255736Sdavidch                uint8_t          *data_buf,
1442255736Sdavidch                int              buf_size)
1443255736Sdavidch{
1444255736Sdavidch    uint32_t cmd_flags;
1445255736Sdavidch    uint32_t val;
1446255736Sdavidch    uint32_t written_so_far;
1447255736Sdavidch    int rc;
1448255736Sdavidch
1449255736Sdavidch    if (buf_size == 1) {
1450255736Sdavidch        return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1451255736Sdavidch    }
1452255736Sdavidch
1453255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1454255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1455255736Sdavidch              offset, buf_size);
1456255736Sdavidch        return (-1);
1457255736Sdavidch    }
1458255736Sdavidch
1459255736Sdavidch    if (buf_size == 0) {
1460255736Sdavidch        return (0); /* nothing to do */
1461255736Sdavidch    }
1462255736Sdavidch
1463255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1464255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1465255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1466255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1467255736Sdavidch        return (-1);
1468255736Sdavidch    }
1469255736Sdavidch
1470255736Sdavidch    /* request access to nvram interface */
1471255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1472255736Sdavidch    if (rc) {
1473255736Sdavidch        return (rc);
1474255736Sdavidch    }
1475255736Sdavidch
1476255736Sdavidch    /* enable access to nvram interface */
1477255736Sdavidch    bxe_enable_nvram_access(sc);
1478255736Sdavidch
1479255736Sdavidch    written_so_far = 0;
1480255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1481255736Sdavidch    while ((written_so_far < buf_size) && (rc == 0)) {
1482255736Sdavidch        if (written_so_far == (buf_size - sizeof(uint32_t))) {
1483255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1484255736Sdavidch        } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1485255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1486255736Sdavidch        } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1487255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1488255736Sdavidch        }
1489255736Sdavidch
1490255736Sdavidch        memcpy(&val, data_buf, 4);
1491255736Sdavidch
1492255736Sdavidch        rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1493255736Sdavidch
1494255736Sdavidch        /* advance to the next dword */
1495255736Sdavidch        offset += sizeof(uint32_t);
1496255736Sdavidch        data_buf += sizeof(uint32_t);
1497255736Sdavidch        written_so_far += sizeof(uint32_t);
1498255736Sdavidch        cmd_flags = 0;
1499255736Sdavidch    }
1500255736Sdavidch
1501255736Sdavidch    /* disable access to nvram interface */
1502255736Sdavidch    bxe_disable_nvram_access(sc);
1503255736Sdavidch    bxe_release_nvram_lock(sc);
1504255736Sdavidch
1505255736Sdavidch    return (rc);
1506255736Sdavidch}
1507255736Sdavidch
1508255736Sdavidch/* copy command into DMAE command memory and set DMAE command Go */
1509255736Sdavidchvoid
1510255736Sdavidchbxe_post_dmae(struct bxe_softc    *sc,
1511296071Sdavidcs              struct dmae_cmd *dmae,
1512255736Sdavidch              int                 idx)
1513255736Sdavidch{
1514255736Sdavidch    uint32_t cmd_offset;
1515255736Sdavidch    int i;
1516255736Sdavidch
1517296071Sdavidcs    cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_cmd) * idx));
1518296071Sdavidcs    for (i = 0; i < ((sizeof(struct dmae_cmd) / 4)); i++) {
1519255736Sdavidch        REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1520255736Sdavidch    }
1521255736Sdavidch
1522255736Sdavidch    REG_WR(sc, dmae_reg_go_c[idx], 1);
1523255736Sdavidch}
1524255736Sdavidch
1525255736Sdavidchuint32_t
1526255736Sdavidchbxe_dmae_opcode_add_comp(uint32_t opcode,
1527255736Sdavidch                         uint8_t  comp_type)
1528255736Sdavidch{
1529296071Sdavidcs    return (opcode | ((comp_type << DMAE_CMD_C_DST_SHIFT) |
1530296071Sdavidcs                      DMAE_CMD_C_TYPE_ENABLE));
1531255736Sdavidch}
1532255736Sdavidch
1533255736Sdavidchuint32_t
1534255736Sdavidchbxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1535255736Sdavidch{
1536296071Sdavidcs    return (opcode & ~DMAE_CMD_SRC_RESET);
1537255736Sdavidch}
1538255736Sdavidch
1539255736Sdavidchuint32_t
1540255736Sdavidchbxe_dmae_opcode(struct bxe_softc *sc,
1541255736Sdavidch                uint8_t          src_type,
1542255736Sdavidch                uint8_t          dst_type,
1543255736Sdavidch                uint8_t          with_comp,
1544255736Sdavidch                uint8_t          comp_type)
1545255736Sdavidch{
1546255736Sdavidch    uint32_t opcode = 0;
1547255736Sdavidch
1548296071Sdavidcs    opcode |= ((src_type << DMAE_CMD_SRC_SHIFT) |
1549296071Sdavidcs               (dst_type << DMAE_CMD_DST_SHIFT));
1550255736Sdavidch
1551296071Sdavidcs    opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
1552255736Sdavidch
1553255736Sdavidch    opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1554255736Sdavidch
1555296071Sdavidcs    opcode |= ((SC_VN(sc) << DMAE_CMD_E1HVN_SHIFT) |
1556296071Sdavidcs               (SC_VN(sc) << DMAE_CMD_DST_VN_SHIFT));
1557255736Sdavidch
1558296071Sdavidcs    opcode |= (DMAE_COM_SET_ERR << DMAE_CMD_ERR_POLICY_SHIFT);
1559255736Sdavidch
1560255736Sdavidch#ifdef __BIG_ENDIAN
1561255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1562255736Sdavidch#else
1563255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1564255736Sdavidch#endif
1565255736Sdavidch
1566255736Sdavidch    if (with_comp) {
1567255736Sdavidch        opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1568255736Sdavidch    }
1569255736Sdavidch
1570255736Sdavidch    return (opcode);
1571255736Sdavidch}
1572255736Sdavidch
1573255736Sdavidchstatic void
1574255736Sdavidchbxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1575296071Sdavidcs                        struct dmae_cmd *dmae,
1576255736Sdavidch                        uint8_t             src_type,
1577255736Sdavidch                        uint8_t             dst_type)
1578255736Sdavidch{
1579296071Sdavidcs    memset(dmae, 0, sizeof(struct dmae_cmd));
1580255736Sdavidch
1581255736Sdavidch    /* set the opcode */
1582255736Sdavidch    dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1583255736Sdavidch                                   TRUE, DMAE_COMP_PCI);
1584255736Sdavidch
1585255736Sdavidch    /* fill in the completion parameters */
1586255736Sdavidch    dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1587255736Sdavidch    dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1588255736Sdavidch    dmae->comp_val     = DMAE_COMP_VAL;
1589255736Sdavidch}
1590255736Sdavidch
1591255736Sdavidch/* issue a DMAE command over the init channel and wait for completion */
1592255736Sdavidchstatic int
1593255736Sdavidchbxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1594296071Sdavidcs                         struct dmae_cmd *dmae)
1595255736Sdavidch{
1596255736Sdavidch    uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1597255736Sdavidch    int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1598255736Sdavidch
1599255736Sdavidch    BXE_DMAE_LOCK(sc);
1600255736Sdavidch
1601255736Sdavidch    /* reset completion */
1602255736Sdavidch    *wb_comp = 0;
1603255736Sdavidch
1604255736Sdavidch    /* post the command on the channel used for initializations */
1605255736Sdavidch    bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1606255736Sdavidch
1607255736Sdavidch    /* wait for completion */
1608255736Sdavidch    DELAY(5);
1609255736Sdavidch
1610255736Sdavidch    while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1611255736Sdavidch        if (!timeout ||
1612255736Sdavidch            (sc->recovery_state != BXE_RECOVERY_DONE &&
1613255736Sdavidch             sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1614295830Sdavidcs            BLOGE(sc, "DMAE timeout! *wb_comp 0x%x recovery_state 0x%x\n",
1615295830Sdavidcs                *wb_comp, sc->recovery_state);
1616255736Sdavidch            BXE_DMAE_UNLOCK(sc);
1617255736Sdavidch            return (DMAE_TIMEOUT);
1618255736Sdavidch        }
1619255736Sdavidch
1620255736Sdavidch        timeout--;
1621255736Sdavidch        DELAY(50);
1622255736Sdavidch    }
1623255736Sdavidch
1624255736Sdavidch    if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1625295830Sdavidcs        BLOGE(sc, "DMAE PCI error! *wb_comp 0x%x recovery_state 0x%x\n",
1626295830Sdavidcs                *wb_comp, sc->recovery_state);
1627255736Sdavidch        BXE_DMAE_UNLOCK(sc);
1628255736Sdavidch        return (DMAE_PCI_ERROR);
1629255736Sdavidch    }
1630255736Sdavidch
1631255736Sdavidch    BXE_DMAE_UNLOCK(sc);
1632255736Sdavidch    return (0);
1633255736Sdavidch}
1634255736Sdavidch
1635255736Sdavidchvoid
1636255736Sdavidchbxe_read_dmae(struct bxe_softc *sc,
1637255736Sdavidch              uint32_t         src_addr,
1638255736Sdavidch              uint32_t         len32)
1639255736Sdavidch{
1640296071Sdavidcs    struct dmae_cmd dmae;
1641255736Sdavidch    uint32_t *data;
1642255736Sdavidch    int i, rc;
1643255736Sdavidch
1644255736Sdavidch    DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1645255736Sdavidch
1646255736Sdavidch    if (!sc->dmae_ready) {
1647255736Sdavidch        data = BXE_SP(sc, wb_data[0]);
1648255736Sdavidch
1649255736Sdavidch        for (i = 0; i < len32; i++) {
1650255736Sdavidch            data[i] = (CHIP_IS_E1(sc)) ?
1651255736Sdavidch                          bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1652255736Sdavidch                          REG_RD(sc, (src_addr + (i * 4)));
1653255736Sdavidch        }
1654255736Sdavidch
1655255736Sdavidch        return;
1656255736Sdavidch    }
1657255736Sdavidch
1658255736Sdavidch    /* set opcode and fixed command fields */
1659255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1660255736Sdavidch
1661255736Sdavidch    /* fill in addresses and len */
1662255736Sdavidch    dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1663255736Sdavidch    dmae.src_addr_hi = 0;
1664255736Sdavidch    dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1665255736Sdavidch    dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1666255736Sdavidch    dmae.len         = len32;
1667255736Sdavidch
1668255736Sdavidch    /* issue the command and wait for completion */
1669255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1670255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1671297793Spfg    }
1672255736Sdavidch}
1673255736Sdavidch
1674255736Sdavidchvoid
1675255736Sdavidchbxe_write_dmae(struct bxe_softc *sc,
1676255736Sdavidch               bus_addr_t       dma_addr,
1677255736Sdavidch               uint32_t         dst_addr,
1678255736Sdavidch               uint32_t         len32)
1679255736Sdavidch{
1680296071Sdavidcs    struct dmae_cmd dmae;
1681255736Sdavidch    int rc;
1682255736Sdavidch
1683255736Sdavidch    if (!sc->dmae_ready) {
1684255736Sdavidch        DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1685255736Sdavidch
1686255736Sdavidch        if (CHIP_IS_E1(sc)) {
1687255736Sdavidch            ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1688255736Sdavidch        } else {
1689255736Sdavidch            ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1690255736Sdavidch        }
1691255736Sdavidch
1692255736Sdavidch        return;
1693255736Sdavidch    }
1694255736Sdavidch
1695255736Sdavidch    /* set opcode and fixed command fields */
1696255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1697255736Sdavidch
1698255736Sdavidch    /* fill in addresses and len */
1699255736Sdavidch    dmae.src_addr_lo = U64_LO(dma_addr);
1700255736Sdavidch    dmae.src_addr_hi = U64_HI(dma_addr);
1701255736Sdavidch    dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1702255736Sdavidch    dmae.dst_addr_hi = 0;
1703255736Sdavidch    dmae.len         = len32;
1704255736Sdavidch
1705255736Sdavidch    /* issue the command and wait for completion */
1706255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1707255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1708255736Sdavidch    }
1709255736Sdavidch}
1710255736Sdavidch
1711255736Sdavidchvoid
1712255736Sdavidchbxe_write_dmae_phys_len(struct bxe_softc *sc,
1713255736Sdavidch                        bus_addr_t       phys_addr,
1714255736Sdavidch                        uint32_t         addr,
1715255736Sdavidch                        uint32_t         len)
1716255736Sdavidch{
1717255736Sdavidch    int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1718255736Sdavidch    int offset = 0;
1719255736Sdavidch
1720255736Sdavidch    while (len > dmae_wr_max) {
1721255736Sdavidch        bxe_write_dmae(sc,
1722255736Sdavidch                       (phys_addr + offset), /* src DMA address */
1723255736Sdavidch                       (addr + offset),      /* dst GRC address */
1724255736Sdavidch                       dmae_wr_max);
1725255736Sdavidch        offset += (dmae_wr_max * 4);
1726255736Sdavidch        len -= dmae_wr_max;
1727255736Sdavidch    }
1728255736Sdavidch
1729255736Sdavidch    bxe_write_dmae(sc,
1730255736Sdavidch                   (phys_addr + offset), /* src DMA address */
1731255736Sdavidch                   (addr + offset),      /* dst GRC address */
1732255736Sdavidch                   len);
1733255736Sdavidch}
1734255736Sdavidch
1735255736Sdavidchvoid
1736255736Sdavidchbxe_set_ctx_validation(struct bxe_softc   *sc,
1737255736Sdavidch                       struct eth_context *cxt,
1738255736Sdavidch                       uint32_t           cid)
1739255736Sdavidch{
1740255736Sdavidch    /* ustorm cxt validation */
1741255736Sdavidch    cxt->ustorm_ag_context.cdu_usage =
1742255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1743255736Sdavidch            CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1744255736Sdavidch    /* xcontext validation */
1745255736Sdavidch    cxt->xstorm_ag_context.cdu_reserved =
1746255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1747255736Sdavidch            CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1748255736Sdavidch}
1749255736Sdavidch
1750255736Sdavidchstatic void
1751255736Sdavidchbxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1752255736Sdavidch                            uint8_t          port,
1753255736Sdavidch                            uint8_t          fw_sb_id,
1754255736Sdavidch                            uint8_t          sb_index,
1755255736Sdavidch                            uint8_t          ticks)
1756255736Sdavidch{
1757255736Sdavidch    uint32_t addr =
1758255736Sdavidch        (BAR_CSTRORM_INTMEM +
1759255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1760255736Sdavidch
1761255736Sdavidch    REG_WR8(sc, addr, ticks);
1762255736Sdavidch
1763255736Sdavidch    BLOGD(sc, DBG_LOAD,
1764255736Sdavidch          "port %d fw_sb_id %d sb_index %d ticks %d\n",
1765255736Sdavidch          port, fw_sb_id, sb_index, ticks);
1766255736Sdavidch}
1767255736Sdavidch
1768255736Sdavidchstatic void
1769255736Sdavidchbxe_storm_memset_hc_disable(struct bxe_softc *sc,
1770255736Sdavidch                            uint8_t          port,
1771255736Sdavidch                            uint16_t         fw_sb_id,
1772255736Sdavidch                            uint8_t          sb_index,
1773255736Sdavidch                            uint8_t          disable)
1774255736Sdavidch{
1775255736Sdavidch    uint32_t enable_flag =
1776255736Sdavidch        (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1777255736Sdavidch    uint32_t addr =
1778255736Sdavidch        (BAR_CSTRORM_INTMEM +
1779255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1780255736Sdavidch    uint8_t flags;
1781255736Sdavidch
1782255736Sdavidch    /* clear and set */
1783255736Sdavidch    flags = REG_RD8(sc, addr);
1784255736Sdavidch    flags &= ~HC_INDEX_DATA_HC_ENABLED;
1785255736Sdavidch    flags |= enable_flag;
1786255736Sdavidch    REG_WR8(sc, addr, flags);
1787255736Sdavidch
1788255736Sdavidch    BLOGD(sc, DBG_LOAD,
1789255736Sdavidch          "port %d fw_sb_id %d sb_index %d disable %d\n",
1790255736Sdavidch          port, fw_sb_id, sb_index, disable);
1791255736Sdavidch}
1792255736Sdavidch
1793255736Sdavidchvoid
1794255736Sdavidchbxe_update_coalesce_sb_index(struct bxe_softc *sc,
1795255736Sdavidch                             uint8_t          fw_sb_id,
1796255736Sdavidch                             uint8_t          sb_index,
1797255736Sdavidch                             uint8_t          disable,
1798255736Sdavidch                             uint16_t         usec)
1799255736Sdavidch{
1800255736Sdavidch    int port = SC_PORT(sc);
1801255736Sdavidch    uint8_t ticks = (usec / 4); /* XXX ??? */
1802255736Sdavidch
1803255736Sdavidch    bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1804255736Sdavidch
1805255736Sdavidch    disable = (disable) ? 1 : ((usec) ? 0 : 1);
1806255736Sdavidch    bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1807255736Sdavidch}
1808255736Sdavidch
1809255736Sdavidchvoid
1810255736Sdavidchelink_cb_udelay(struct bxe_softc *sc,
1811255736Sdavidch                uint32_t         usecs)
1812255736Sdavidch{
1813255736Sdavidch    DELAY(usecs);
1814255736Sdavidch}
1815255736Sdavidch
1816255736Sdavidchuint32_t
1817255736Sdavidchelink_cb_reg_read(struct bxe_softc *sc,
1818255736Sdavidch                  uint32_t         reg_addr)
1819255736Sdavidch{
1820255736Sdavidch    return (REG_RD(sc, reg_addr));
1821255736Sdavidch}
1822255736Sdavidch
1823255736Sdavidchvoid
1824255736Sdavidchelink_cb_reg_write(struct bxe_softc *sc,
1825255736Sdavidch                   uint32_t         reg_addr,
1826255736Sdavidch                   uint32_t         val)
1827255736Sdavidch{
1828255736Sdavidch    REG_WR(sc, reg_addr, val);
1829255736Sdavidch}
1830255736Sdavidch
1831255736Sdavidchvoid
1832255736Sdavidchelink_cb_reg_wb_write(struct bxe_softc *sc,
1833255736Sdavidch                      uint32_t         offset,
1834255736Sdavidch                      uint32_t         *wb_write,
1835255736Sdavidch                      uint16_t         len)
1836255736Sdavidch{
1837255736Sdavidch    REG_WR_DMAE(sc, offset, wb_write, len);
1838255736Sdavidch}
1839255736Sdavidch
1840255736Sdavidchvoid
1841255736Sdavidchelink_cb_reg_wb_read(struct bxe_softc *sc,
1842255736Sdavidch                     uint32_t         offset,
1843255736Sdavidch                     uint32_t         *wb_write,
1844255736Sdavidch                     uint16_t         len)
1845255736Sdavidch{
1846255736Sdavidch    REG_RD_DMAE(sc, offset, wb_write, len);
1847255736Sdavidch}
1848255736Sdavidch
1849255736Sdavidchuint8_t
1850255736Sdavidchelink_cb_path_id(struct bxe_softc *sc)
1851255736Sdavidch{
1852255736Sdavidch    return (SC_PATH(sc));
1853255736Sdavidch}
1854255736Sdavidch
1855255736Sdavidchvoid
1856255736Sdavidchelink_cb_event_log(struct bxe_softc     *sc,
1857255736Sdavidch                   const elink_log_id_t elink_log_id,
1858255736Sdavidch                   ...)
1859255736Sdavidch{
1860255736Sdavidch    /* XXX */
1861255736Sdavidch    BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1862255736Sdavidch}
1863255736Sdavidch
1864255736Sdavidchstatic int
1865255736Sdavidchbxe_set_spio(struct bxe_softc *sc,
1866255736Sdavidch             int              spio,
1867255736Sdavidch             uint32_t         mode)
1868255736Sdavidch{
1869255736Sdavidch    uint32_t spio_reg;
1870255736Sdavidch
1871255736Sdavidch    /* Only 2 SPIOs are configurable */
1872255736Sdavidch    if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1873295830Sdavidcs        BLOGE(sc, "Invalid SPIO 0x%x mode 0x%x\n", spio, mode);
1874255736Sdavidch        return (-1);
1875255736Sdavidch    }
1876255736Sdavidch
1877255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1878255736Sdavidch
1879255736Sdavidch    /* read SPIO and mask except the float bits */
1880255736Sdavidch    spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1881255736Sdavidch
1882255736Sdavidch    switch (mode) {
1883255736Sdavidch    case MISC_SPIO_OUTPUT_LOW:
1884255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1885255736Sdavidch        /* clear FLOAT and set CLR */
1886255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1887255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1888255736Sdavidch        break;
1889255736Sdavidch
1890255736Sdavidch    case MISC_SPIO_OUTPUT_HIGH:
1891255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1892255736Sdavidch        /* clear FLOAT and set SET */
1893255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1894255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_SET_POS);
1895255736Sdavidch        break;
1896255736Sdavidch
1897255736Sdavidch    case MISC_SPIO_INPUT_HI_Z:
1898255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1899255736Sdavidch        /* set FLOAT */
1900255736Sdavidch        spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1901255736Sdavidch        break;
1902255736Sdavidch
1903255736Sdavidch    default:
1904255736Sdavidch        break;
1905255736Sdavidch    }
1906255736Sdavidch
1907255736Sdavidch    REG_WR(sc, MISC_REG_SPIO, spio_reg);
1908255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1909255736Sdavidch
1910255736Sdavidch    return (0);
1911255736Sdavidch}
1912255736Sdavidch
1913255736Sdavidchstatic int
1914255736Sdavidchbxe_gpio_read(struct bxe_softc *sc,
1915255736Sdavidch              int              gpio_num,
1916255736Sdavidch              uint8_t          port)
1917255736Sdavidch{
1918255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1919255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1920255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1921255736Sdavidch    int gpio_shift = (gpio_num +
1922255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1923255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1924255736Sdavidch    uint32_t gpio_reg;
1925255736Sdavidch
1926255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1927295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d port 0x%x gpio_port %d gpio_shift %d"
1928295830Sdavidcs            " gpio_mask 0x%x\n", gpio_num, port, gpio_port, gpio_shift,
1929295830Sdavidcs            gpio_mask);
1930255736Sdavidch        return (-1);
1931255736Sdavidch    }
1932255736Sdavidch
1933255736Sdavidch    /* read GPIO value */
1934255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1935255736Sdavidch
1936255736Sdavidch    /* get the requested pin value */
1937255736Sdavidch    return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
1938255736Sdavidch}
1939255736Sdavidch
1940255736Sdavidchstatic int
1941255736Sdavidchbxe_gpio_write(struct bxe_softc *sc,
1942255736Sdavidch               int              gpio_num,
1943255736Sdavidch               uint32_t         mode,
1944255736Sdavidch               uint8_t          port)
1945255736Sdavidch{
1946255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1947255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1948255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1949255736Sdavidch    int gpio_shift = (gpio_num +
1950255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1951255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1952255736Sdavidch    uint32_t gpio_reg;
1953255736Sdavidch
1954255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1955295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
1956295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
1957295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
1958255736Sdavidch        return (-1);
1959255736Sdavidch    }
1960255736Sdavidch
1961255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1962255736Sdavidch
1963255736Sdavidch    /* read GPIO and mask except the float bits */
1964255736Sdavidch    gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1965255736Sdavidch
1966255736Sdavidch    switch (mode) {
1967255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1968255736Sdavidch        BLOGD(sc, DBG_PHY,
1969255736Sdavidch              "Set GPIO %d (shift %d) -> output low\n",
1970255736Sdavidch              gpio_num, gpio_shift);
1971255736Sdavidch        /* clear FLOAT and set CLR */
1972255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1973255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1974255736Sdavidch        break;
1975255736Sdavidch
1976255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1977255736Sdavidch        BLOGD(sc, DBG_PHY,
1978255736Sdavidch              "Set GPIO %d (shift %d) -> output high\n",
1979255736Sdavidch              gpio_num, gpio_shift);
1980255736Sdavidch        /* clear FLOAT and set SET */
1981255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1982255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1983255736Sdavidch        break;
1984255736Sdavidch
1985255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1986255736Sdavidch        BLOGD(sc, DBG_PHY,
1987255736Sdavidch              "Set GPIO %d (shift %d) -> input\n",
1988255736Sdavidch              gpio_num, gpio_shift);
1989255736Sdavidch        /* set FLOAT */
1990255736Sdavidch        gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1991255736Sdavidch        break;
1992255736Sdavidch
1993255736Sdavidch    default:
1994255736Sdavidch        break;
1995255736Sdavidch    }
1996255736Sdavidch
1997255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1998255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1999255736Sdavidch
2000255736Sdavidch    return (0);
2001255736Sdavidch}
2002255736Sdavidch
2003255736Sdavidchstatic int
2004255736Sdavidchbxe_gpio_mult_write(struct bxe_softc *sc,
2005255736Sdavidch                    uint8_t          pins,
2006255736Sdavidch                    uint32_t         mode)
2007255736Sdavidch{
2008255736Sdavidch    uint32_t gpio_reg;
2009255736Sdavidch
2010255736Sdavidch    /* any port swapping should be handled by caller */
2011255736Sdavidch
2012255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2013255736Sdavidch
2014255736Sdavidch    /* read GPIO and mask except the float bits */
2015255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
2016255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2017255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
2018255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
2019255736Sdavidch
2020255736Sdavidch    switch (mode) {
2021255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2022255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
2023255736Sdavidch        /* set CLR */
2024255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
2025255736Sdavidch        break;
2026255736Sdavidch
2027255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2028255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
2029255736Sdavidch        /* set SET */
2030255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
2031255736Sdavidch        break;
2032255736Sdavidch
2033255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2034255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
2035255736Sdavidch        /* set FLOAT */
2036255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2037255736Sdavidch        break;
2038255736Sdavidch
2039255736Sdavidch    default:
2040295830Sdavidcs        BLOGE(sc, "Invalid GPIO mode assignment pins 0x%x mode 0x%x"
2041295830Sdavidcs            " gpio_reg 0x%x\n", pins, mode, gpio_reg);
2042255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2043255736Sdavidch        return (-1);
2044255736Sdavidch    }
2045255736Sdavidch
2046255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2047255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2048255736Sdavidch
2049255736Sdavidch    return (0);
2050255736Sdavidch}
2051255736Sdavidch
2052255736Sdavidchstatic int
2053255736Sdavidchbxe_gpio_int_write(struct bxe_softc *sc,
2054255736Sdavidch                   int              gpio_num,
2055255736Sdavidch                   uint32_t         mode,
2056255736Sdavidch                   uint8_t          port)
2057255736Sdavidch{
2058255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
2059255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2060255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2061255736Sdavidch    int gpio_shift = (gpio_num +
2062255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2063255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
2064255736Sdavidch    uint32_t gpio_reg;
2065255736Sdavidch
2066255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
2067295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
2068295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
2069295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
2070255736Sdavidch        return (-1);
2071255736Sdavidch    }
2072255736Sdavidch
2073255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2074255736Sdavidch
2075255736Sdavidch    /* read GPIO int */
2076255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2077255736Sdavidch
2078255736Sdavidch    switch (mode) {
2079255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2080255736Sdavidch        BLOGD(sc, DBG_PHY,
2081255736Sdavidch              "Clear GPIO INT %d (shift %d) -> output low\n",
2082255736Sdavidch              gpio_num, gpio_shift);
2083255736Sdavidch        /* clear SET and set CLR */
2084255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2085255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2086255736Sdavidch        break;
2087255736Sdavidch
2088255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2089255736Sdavidch        BLOGD(sc, DBG_PHY,
2090255736Sdavidch              "Set GPIO INT %d (shift %d) -> output high\n",
2091255736Sdavidch              gpio_num, gpio_shift);
2092255736Sdavidch        /* clear CLR and set SET */
2093255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2094255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2095255736Sdavidch        break;
2096255736Sdavidch
2097255736Sdavidch    default:
2098255736Sdavidch        break;
2099255736Sdavidch    }
2100255736Sdavidch
2101255736Sdavidch    REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2102255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2103255736Sdavidch
2104255736Sdavidch    return (0);
2105255736Sdavidch}
2106255736Sdavidch
2107255736Sdavidchuint32_t
2108255736Sdavidchelink_cb_gpio_read(struct bxe_softc *sc,
2109255736Sdavidch                   uint16_t         gpio_num,
2110255736Sdavidch                   uint8_t          port)
2111255736Sdavidch{
2112255736Sdavidch    return (bxe_gpio_read(sc, gpio_num, port));
2113255736Sdavidch}
2114255736Sdavidch
2115255736Sdavidchuint8_t
2116255736Sdavidchelink_cb_gpio_write(struct bxe_softc *sc,
2117255736Sdavidch                    uint16_t         gpio_num,
2118255736Sdavidch                    uint8_t          mode, /* 0=low 1=high */
2119255736Sdavidch                    uint8_t          port)
2120255736Sdavidch{
2121255736Sdavidch    return (bxe_gpio_write(sc, gpio_num, mode, port));
2122255736Sdavidch}
2123255736Sdavidch
2124255736Sdavidchuint8_t
2125255736Sdavidchelink_cb_gpio_mult_write(struct bxe_softc *sc,
2126255736Sdavidch                         uint8_t          pins,
2127255736Sdavidch                         uint8_t          mode) /* 0=low 1=high */
2128255736Sdavidch{
2129255736Sdavidch    return (bxe_gpio_mult_write(sc, pins, mode));
2130255736Sdavidch}
2131255736Sdavidch
2132255736Sdavidchuint8_t
2133255736Sdavidchelink_cb_gpio_int_write(struct bxe_softc *sc,
2134255736Sdavidch                        uint16_t         gpio_num,
2135255736Sdavidch                        uint8_t          mode, /* 0=low 1=high */
2136255736Sdavidch                        uint8_t          port)
2137255736Sdavidch{
2138255736Sdavidch    return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2139255736Sdavidch}
2140255736Sdavidch
2141255736Sdavidchvoid
2142255736Sdavidchelink_cb_notify_link_changed(struct bxe_softc *sc)
2143255736Sdavidch{
2144255736Sdavidch    REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2145255736Sdavidch                (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2146255736Sdavidch}
2147255736Sdavidch
2148255736Sdavidch/* send the MCP a request, block until there is a reply */
2149255736Sdavidchuint32_t
2150255736Sdavidchelink_cb_fw_command(struct bxe_softc *sc,
2151255736Sdavidch                    uint32_t         command,
2152255736Sdavidch                    uint32_t         param)
2153255736Sdavidch{
2154255736Sdavidch    int mb_idx = SC_FW_MB_IDX(sc);
2155255736Sdavidch    uint32_t seq;
2156255736Sdavidch    uint32_t rc = 0;
2157255736Sdavidch    uint32_t cnt = 1;
2158255736Sdavidch    uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2159255736Sdavidch
2160255736Sdavidch    BXE_FWMB_LOCK(sc);
2161255736Sdavidch
2162255736Sdavidch    seq = ++sc->fw_seq;
2163255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2164255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2165255736Sdavidch
2166255736Sdavidch    BLOGD(sc, DBG_PHY,
2167255736Sdavidch          "wrote command 0x%08x to FW MB param 0x%08x\n",
2168255736Sdavidch          (command | seq), param);
2169255736Sdavidch
2170255736Sdavidch    /* Let the FW do it's magic. GIve it up to 5 seconds... */
2171255736Sdavidch    do {
2172255736Sdavidch        DELAY(delay * 1000);
2173255736Sdavidch        rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2174255736Sdavidch    } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2175255736Sdavidch
2176255736Sdavidch    BLOGD(sc, DBG_PHY,
2177255736Sdavidch          "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2178255736Sdavidch          cnt*delay, rc, seq);
2179255736Sdavidch
2180255736Sdavidch    /* is this a reply to our command? */
2181255736Sdavidch    if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2182255736Sdavidch        rc &= FW_MSG_CODE_MASK;
2183255736Sdavidch    } else {
2184255736Sdavidch        /* Ruh-roh! */
2185255736Sdavidch        BLOGE(sc, "FW failed to respond!\n");
2186255736Sdavidch        // XXX bxe_fw_dump(sc);
2187255736Sdavidch        rc = 0;
2188255736Sdavidch    }
2189255736Sdavidch
2190255736Sdavidch    BXE_FWMB_UNLOCK(sc);
2191255736Sdavidch    return (rc);
2192255736Sdavidch}
2193255736Sdavidch
2194255736Sdavidchstatic uint32_t
2195255736Sdavidchbxe_fw_command(struct bxe_softc *sc,
2196255736Sdavidch               uint32_t         command,
2197255736Sdavidch               uint32_t         param)
2198255736Sdavidch{
2199255736Sdavidch    return (elink_cb_fw_command(sc, command, param));
2200255736Sdavidch}
2201255736Sdavidch
2202255736Sdavidchstatic void
2203255736Sdavidch__storm_memset_dma_mapping(struct bxe_softc *sc,
2204255736Sdavidch                           uint32_t         addr,
2205255736Sdavidch                           bus_addr_t       mapping)
2206255736Sdavidch{
2207255736Sdavidch    REG_WR(sc, addr, U64_LO(mapping));
2208255736Sdavidch    REG_WR(sc, (addr + 4), U64_HI(mapping));
2209255736Sdavidch}
2210255736Sdavidch
2211255736Sdavidchstatic void
2212255736Sdavidchstorm_memset_spq_addr(struct bxe_softc *sc,
2213255736Sdavidch                      bus_addr_t       mapping,
2214255736Sdavidch                      uint16_t         abs_fid)
2215255736Sdavidch{
2216255736Sdavidch    uint32_t addr = (XSEM_REG_FAST_MEMORY +
2217255736Sdavidch                     XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2218255736Sdavidch    __storm_memset_dma_mapping(sc, addr, mapping);
2219255736Sdavidch}
2220255736Sdavidch
2221255736Sdavidchstatic void
2222255736Sdavidchstorm_memset_vf_to_pf(struct bxe_softc *sc,
2223255736Sdavidch                      uint16_t         abs_fid,
2224255736Sdavidch                      uint16_t         pf_id)
2225255736Sdavidch{
2226255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2227255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2228255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2229255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2230255736Sdavidch}
2231255736Sdavidch
2232255736Sdavidchstatic void
2233255736Sdavidchstorm_memset_func_en(struct bxe_softc *sc,
2234255736Sdavidch                     uint16_t         abs_fid,
2235255736Sdavidch                     uint8_t          enable)
2236255736Sdavidch{
2237255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2238255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2239255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2240255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2241255736Sdavidch}
2242255736Sdavidch
2243255736Sdavidchstatic void
2244255736Sdavidchstorm_memset_eq_data(struct bxe_softc       *sc,
2245255736Sdavidch                     struct event_ring_data *eq_data,
2246255736Sdavidch                     uint16_t               pfid)
2247255736Sdavidch{
2248255736Sdavidch    uint32_t addr;
2249255736Sdavidch    size_t size;
2250255736Sdavidch
2251255736Sdavidch    addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2252255736Sdavidch    size = sizeof(struct event_ring_data);
2253255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2254255736Sdavidch}
2255255736Sdavidch
2256255736Sdavidchstatic void
2257255736Sdavidchstorm_memset_eq_prod(struct bxe_softc *sc,
2258255736Sdavidch                     uint16_t         eq_prod,
2259255736Sdavidch                     uint16_t         pfid)
2260255736Sdavidch{
2261255736Sdavidch    uint32_t addr = (BAR_CSTRORM_INTMEM +
2262255736Sdavidch                     CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2263255736Sdavidch    REG_WR16(sc, addr, eq_prod);
2264255736Sdavidch}
2265255736Sdavidch
2266255736Sdavidch/*
2267255736Sdavidch * Post a slowpath command.
2268255736Sdavidch *
2269298955Spfg * A slowpath command is used to propagate a configuration change through
2270255736Sdavidch * the controller in a controlled manner, allowing each STORM processor and
2271255736Sdavidch * other H/W blocks to phase in the change.  The commands sent on the
2272255736Sdavidch * slowpath are referred to as ramrods.  Depending on the ramrod used the
2273255736Sdavidch * completion of the ramrod will occur in different ways.  Here's a
2274255736Sdavidch * breakdown of ramrods and how they complete:
2275255736Sdavidch *
2276255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_SETUP
2277255736Sdavidch *   Used to setup the leading connection on a port.  Completes on the
2278255736Sdavidch *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2279255736Sdavidch *
2280255736Sdavidch * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2281255736Sdavidch *   Used to setup an additional connection on a port.  Completes on the
2282255736Sdavidch *   RCQ of the multi-queue/RSS connection being initialized.
2283255736Sdavidch *
2284255736Sdavidch * RAMROD_CMD_ID_ETH_STAT_QUERY
2285255736Sdavidch *   Used to force the storm processors to update the statistics database
2286255736Sdavidch *   in host memory.  This ramrod is send on the leading connection CID and
2287255736Sdavidch *   completes as an index increment of the CSTORM on the default status
2288255736Sdavidch *   block.
2289255736Sdavidch *
2290255736Sdavidch * RAMROD_CMD_ID_ETH_UPDATE
2291255736Sdavidch *   Used to update the state of the leading connection, usually to udpate
2292255736Sdavidch *   the RSS indirection table.  Completes on the RCQ of the leading
2293255736Sdavidch *   connection. (Not currently used under FreeBSD until OS support becomes
2294255736Sdavidch *   available.)
2295255736Sdavidch *
2296255736Sdavidch * RAMROD_CMD_ID_ETH_HALT
2297255736Sdavidch *   Used when tearing down a connection prior to driver unload.  Completes
2298255736Sdavidch *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2299255736Sdavidch *   use this on the leading connection.
2300255736Sdavidch *
2301255736Sdavidch * RAMROD_CMD_ID_ETH_SET_MAC
2302255736Sdavidch *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2303255736Sdavidch *   the RCQ of the leading connection.
2304255736Sdavidch *
2305255736Sdavidch * RAMROD_CMD_ID_ETH_CFC_DEL
2306255736Sdavidch *   Used when tearing down a conneciton prior to driver unload.  Completes
2307255736Sdavidch *   on the RCQ of the leading connection (since the current connection
2308255736Sdavidch *   has been completely removed from controller memory).
2309255736Sdavidch *
2310255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_DEL
2311255736Sdavidch *   Used to tear down the leading connection prior to driver unload,
2312255736Sdavidch *   typically fp[0].  Completes as an index increment of the CSTORM on the
2313255736Sdavidch *   default status block.
2314255736Sdavidch *
2315255736Sdavidch * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2316255736Sdavidch *   Used for connection offload.  Completes on the RCQ of the multi-queue
2317255736Sdavidch *   RSS connection that is being offloaded.  (Not currently used under
2318255736Sdavidch *   FreeBSD.)
2319255736Sdavidch *
2320255736Sdavidch * There can only be one command pending per function.
2321255736Sdavidch *
2322255736Sdavidch * Returns:
2323255736Sdavidch *   0 = Success, !0 = Failure.
2324255736Sdavidch */
2325255736Sdavidch
2326255736Sdavidch/* must be called under the spq lock */
2327255736Sdavidchstatic inline
2328255736Sdavidchstruct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2329255736Sdavidch{
2330255736Sdavidch    struct eth_spe *next_spe = sc->spq_prod_bd;
2331255736Sdavidch
2332255736Sdavidch    if (sc->spq_prod_bd == sc->spq_last_bd) {
2333255736Sdavidch        /* wrap back to the first eth_spq */
2334255736Sdavidch        sc->spq_prod_bd = sc->spq;
2335255736Sdavidch        sc->spq_prod_idx = 0;
2336255736Sdavidch    } else {
2337255736Sdavidch        sc->spq_prod_bd++;
2338255736Sdavidch        sc->spq_prod_idx++;
2339255736Sdavidch    }
2340255736Sdavidch
2341255736Sdavidch    return (next_spe);
2342255736Sdavidch}
2343255736Sdavidch
2344255736Sdavidch/* must be called under the spq lock */
2345255736Sdavidchstatic inline
2346255736Sdavidchvoid bxe_sp_prod_update(struct bxe_softc *sc)
2347255736Sdavidch{
2348255736Sdavidch    int func = SC_FUNC(sc);
2349255736Sdavidch
2350255736Sdavidch    /*
2351255736Sdavidch     * Make sure that BD data is updated before writing the producer.
2352255736Sdavidch     * BD data is written to the memory, the producer is read from the
2353255736Sdavidch     * memory, thus we need a full memory barrier to ensure the ordering.
2354255736Sdavidch     */
2355255736Sdavidch    mb();
2356255736Sdavidch
2357255736Sdavidch    REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2358255736Sdavidch             sc->spq_prod_idx);
2359255736Sdavidch
2360255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2361255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
2362255736Sdavidch}
2363255736Sdavidch
2364255736Sdavidch/**
2365255736Sdavidch * bxe_is_contextless_ramrod - check if the current command ends on EQ
2366255736Sdavidch *
2367255736Sdavidch * @cmd:      command to check
2368255736Sdavidch * @cmd_type: command type
2369255736Sdavidch */
2370255736Sdavidchstatic inline
2371255736Sdavidchint bxe_is_contextless_ramrod(int cmd,
2372255736Sdavidch                              int cmd_type)
2373255736Sdavidch{
2374255736Sdavidch    if ((cmd_type == NONE_CONNECTION_TYPE) ||
2375255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2376255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2377255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2378255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2379255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2380255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2381255736Sdavidch        return (TRUE);
2382255736Sdavidch    } else {
2383255736Sdavidch        return (FALSE);
2384255736Sdavidch    }
2385255736Sdavidch}
2386255736Sdavidch
2387255736Sdavidch/**
2388255736Sdavidch * bxe_sp_post - place a single command on an SP ring
2389255736Sdavidch *
2390255736Sdavidch * @sc:         driver handle
2391255736Sdavidch * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2392255736Sdavidch * @cid:        SW CID the command is related to
2393255736Sdavidch * @data_hi:    command private data address (high 32 bits)
2394255736Sdavidch * @data_lo:    command private data address (low 32 bits)
2395255736Sdavidch * @cmd_type:   command type (e.g. NONE, ETH)
2396255736Sdavidch *
2397255736Sdavidch * SP data is handled as if it's always an address pair, thus data fields are
2398255736Sdavidch * not swapped to little endian in upper functions. Instead this function swaps
2399255736Sdavidch * data as if it's two uint32 fields.
2400255736Sdavidch */
2401255736Sdavidchint
2402255736Sdavidchbxe_sp_post(struct bxe_softc *sc,
2403255736Sdavidch            int              command,
2404255736Sdavidch            int              cid,
2405255736Sdavidch            uint32_t         data_hi,
2406255736Sdavidch            uint32_t         data_lo,
2407255736Sdavidch            int              cmd_type)
2408255736Sdavidch{
2409255736Sdavidch    struct eth_spe *spe;
2410255736Sdavidch    uint16_t type;
2411255736Sdavidch    int common;
2412255736Sdavidch
2413255736Sdavidch    common = bxe_is_contextless_ramrod(command, cmd_type);
2414255736Sdavidch
2415255736Sdavidch    BXE_SP_LOCK(sc);
2416255736Sdavidch
2417255736Sdavidch    if (common) {
2418255736Sdavidch        if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2419255736Sdavidch            BLOGE(sc, "EQ ring is full!\n");
2420255736Sdavidch            BXE_SP_UNLOCK(sc);
2421255736Sdavidch            return (-1);
2422255736Sdavidch        }
2423255736Sdavidch    } else {
2424255736Sdavidch        if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2425255736Sdavidch            BLOGE(sc, "SPQ ring is full!\n");
2426255736Sdavidch            BXE_SP_UNLOCK(sc);
2427255736Sdavidch            return (-1);
2428255736Sdavidch        }
2429255736Sdavidch    }
2430255736Sdavidch
2431255736Sdavidch    spe = bxe_sp_get_next(sc);
2432255736Sdavidch
2433255736Sdavidch    /* CID needs port number to be encoded int it */
2434255736Sdavidch    spe->hdr.conn_and_cmd_data =
2435296071Sdavidcs        htole32((command << SPE_HDR_T_CMD_ID_SHIFT) | HW_CID(sc, cid));
2436255736Sdavidch
2437296071Sdavidcs    type = (cmd_type << SPE_HDR_T_CONN_TYPE_SHIFT) & SPE_HDR_T_CONN_TYPE;
2438255736Sdavidch
2439255736Sdavidch    /* TBD: Check if it works for VFs */
2440296071Sdavidcs    type |= ((SC_FUNC(sc) << SPE_HDR_T_FUNCTION_ID_SHIFT) &
2441296071Sdavidcs             SPE_HDR_T_FUNCTION_ID);
2442255736Sdavidch
2443255736Sdavidch    spe->hdr.type = htole16(type);
2444255736Sdavidch
2445255736Sdavidch    spe->data.update_data_addr.hi = htole32(data_hi);
2446255736Sdavidch    spe->data.update_data_addr.lo = htole32(data_lo);
2447255736Sdavidch
2448255736Sdavidch    /*
2449255736Sdavidch     * It's ok if the actual decrement is issued towards the memory
2450255736Sdavidch     * somewhere between the lock and unlock. Thus no more explict
2451255736Sdavidch     * memory barrier is needed.
2452255736Sdavidch     */
2453255736Sdavidch    if (common) {
2454255736Sdavidch        atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2455255736Sdavidch    } else {
2456255736Sdavidch        atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2457255736Sdavidch    }
2458255736Sdavidch
2459256341Sdim    BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2460256341Sdim    BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2461256341Sdim          BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2462255736Sdavidch    BLOGD(sc, DBG_SP,
2463255736Sdavidch          "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2464255736Sdavidch          sc->spq_prod_idx,
2465255736Sdavidch          (uint32_t)U64_HI(sc->spq_dma.paddr),
2466255736Sdavidch          (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2467255736Sdavidch          command,
2468255736Sdavidch          common,
2469255736Sdavidch          HW_CID(sc, cid),
2470255736Sdavidch          data_hi,
2471255736Sdavidch          data_lo,
2472255736Sdavidch          type,
2473255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left),
2474255736Sdavidch          atomic_load_acq_long(&sc->eq_spq_left));
2475255736Sdavidch
2476255736Sdavidch    bxe_sp_prod_update(sc);
2477255736Sdavidch
2478255736Sdavidch    BXE_SP_UNLOCK(sc);
2479255736Sdavidch    return (0);
2480255736Sdavidch}
2481255736Sdavidch
2482255736Sdavidch/**
2483255736Sdavidch * bxe_debug_print_ind_table - prints the indirection table configuration.
2484255736Sdavidch *
2485255736Sdavidch * @sc: driver hanlde
2486255736Sdavidch * @p:  pointer to rss configuration
2487255736Sdavidch */
2488255736Sdavidch
2489255736Sdavidch/*
2490255736Sdavidch * FreeBSD Device probe function.
2491255736Sdavidch *
2492255736Sdavidch * Compares the device found to the driver's list of supported devices and
2493255736Sdavidch * reports back to the bsd loader whether this is the right driver for the device.
2494255736Sdavidch * This is the driver entry function called from the "kldload" command.
2495255736Sdavidch *
2496255736Sdavidch * Returns:
2497255736Sdavidch *   BUS_PROBE_DEFAULT on success, positive value on failure.
2498255736Sdavidch */
2499255736Sdavidchstatic int
2500255736Sdavidchbxe_probe(device_t dev)
2501255736Sdavidch{
2502255736Sdavidch    struct bxe_device_type *t;
2503255736Sdavidch    char *descbuf;
2504255736Sdavidch    uint16_t did, sdid, svid, vid;
2505255736Sdavidch
2506255736Sdavidch    /* Find our device structure */
2507255736Sdavidch    t = bxe_devs;
2508255736Sdavidch
2509255736Sdavidch    /* Get the data for the device to be probed. */
2510255736Sdavidch    vid  = pci_get_vendor(dev);
2511255736Sdavidch    did  = pci_get_device(dev);
2512255736Sdavidch    svid = pci_get_subvendor(dev);
2513255736Sdavidch    sdid = pci_get_subdevice(dev);
2514255736Sdavidch
2515255736Sdavidch    /* Look through the list of known devices for a match. */
2516255736Sdavidch    while (t->bxe_name != NULL) {
2517255736Sdavidch        if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2518255736Sdavidch            ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2519255736Sdavidch            ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2520255736Sdavidch            descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2521255736Sdavidch            if (descbuf == NULL)
2522255736Sdavidch                return (ENOMEM);
2523255736Sdavidch
2524255736Sdavidch            /* Print out the device identity. */
2525255736Sdavidch            snprintf(descbuf, BXE_DEVDESC_MAX,
2526255736Sdavidch                     "%s (%c%d) BXE v:%s\n", t->bxe_name,
2527255736Sdavidch                     (((pci_read_config(dev, PCIR_REVID, 4) &
2528255736Sdavidch                        0xf0) >> 4) + 'A'),
2529255736Sdavidch                     (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2530255736Sdavidch                     BXE_DRIVER_VERSION);
2531255736Sdavidch
2532255736Sdavidch            device_set_desc_copy(dev, descbuf);
2533255736Sdavidch            free(descbuf, M_TEMP);
2534255736Sdavidch            return (BUS_PROBE_DEFAULT);
2535255736Sdavidch        }
2536255736Sdavidch        t++;
2537255736Sdavidch    }
2538255736Sdavidch
2539255736Sdavidch    return (ENXIO);
2540255736Sdavidch}
2541255736Sdavidch
2542255736Sdavidchstatic void
2543255736Sdavidchbxe_init_mutexes(struct bxe_softc *sc)
2544255736Sdavidch{
2545255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2546255736Sdavidch    snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2547255736Sdavidch             "bxe%d_core_lock", sc->unit);
2548255736Sdavidch    sx_init(&sc->core_sx, sc->core_sx_name);
2549255736Sdavidch#else
2550255736Sdavidch    snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2551255736Sdavidch             "bxe%d_core_lock", sc->unit);
2552255736Sdavidch    mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2553255736Sdavidch#endif
2554255736Sdavidch
2555255736Sdavidch    snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2556255736Sdavidch             "bxe%d_sp_lock", sc->unit);
2557255736Sdavidch    mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2558255736Sdavidch
2559255736Sdavidch    snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2560255736Sdavidch             "bxe%d_dmae_lock", sc->unit);
2561255736Sdavidch    mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2562255736Sdavidch
2563255736Sdavidch    snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2564255736Sdavidch             "bxe%d_phy_lock", sc->unit);
2565255736Sdavidch    mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2566255736Sdavidch
2567255736Sdavidch    snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2568255736Sdavidch             "bxe%d_fwmb_lock", sc->unit);
2569255736Sdavidch    mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2570255736Sdavidch
2571255736Sdavidch    snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2572255736Sdavidch             "bxe%d_print_lock", sc->unit);
2573255736Sdavidch    mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2574255736Sdavidch
2575255736Sdavidch    snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2576255736Sdavidch             "bxe%d_stats_lock", sc->unit);
2577255736Sdavidch    mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2578255736Sdavidch
2579255736Sdavidch    snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2580255736Sdavidch             "bxe%d_mcast_lock", sc->unit);
2581255736Sdavidch    mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2582255736Sdavidch}
2583255736Sdavidch
2584255736Sdavidchstatic void
2585255736Sdavidchbxe_release_mutexes(struct bxe_softc *sc)
2586255736Sdavidch{
2587255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2588255736Sdavidch    sx_destroy(&sc->core_sx);
2589255736Sdavidch#else
2590255736Sdavidch    if (mtx_initialized(&sc->core_mtx)) {
2591255736Sdavidch        mtx_destroy(&sc->core_mtx);
2592255736Sdavidch    }
2593255736Sdavidch#endif
2594255736Sdavidch
2595255736Sdavidch    if (mtx_initialized(&sc->sp_mtx)) {
2596255736Sdavidch        mtx_destroy(&sc->sp_mtx);
2597255736Sdavidch    }
2598255736Sdavidch
2599255736Sdavidch    if (mtx_initialized(&sc->dmae_mtx)) {
2600255736Sdavidch        mtx_destroy(&sc->dmae_mtx);
2601255736Sdavidch    }
2602255736Sdavidch
2603255736Sdavidch    if (mtx_initialized(&sc->port.phy_mtx)) {
2604255736Sdavidch        mtx_destroy(&sc->port.phy_mtx);
2605255736Sdavidch    }
2606255736Sdavidch
2607255736Sdavidch    if (mtx_initialized(&sc->fwmb_mtx)) {
2608255736Sdavidch        mtx_destroy(&sc->fwmb_mtx);
2609255736Sdavidch    }
2610255736Sdavidch
2611255736Sdavidch    if (mtx_initialized(&sc->print_mtx)) {
2612255736Sdavidch        mtx_destroy(&sc->print_mtx);
2613255736Sdavidch    }
2614255736Sdavidch
2615255736Sdavidch    if (mtx_initialized(&sc->stats_mtx)) {
2616255736Sdavidch        mtx_destroy(&sc->stats_mtx);
2617255736Sdavidch    }
2618255736Sdavidch
2619255736Sdavidch    if (mtx_initialized(&sc->mcast_mtx)) {
2620255736Sdavidch        mtx_destroy(&sc->mcast_mtx);
2621255736Sdavidch    }
2622255736Sdavidch}
2623255736Sdavidch
2624255736Sdavidchstatic void
2625255736Sdavidchbxe_tx_disable(struct bxe_softc* sc)
2626255736Sdavidch{
2627266979Smarcel    if_t ifp = sc->ifp;
2628255736Sdavidch
2629255736Sdavidch    /* tell the stack the driver is stopped and TX queue is full */
2630266979Smarcel    if (ifp !=  NULL) {
2631266979Smarcel        if_setdrvflags(ifp, 0);
2632255736Sdavidch    }
2633255736Sdavidch}
2634255736Sdavidch
2635255736Sdavidchstatic void
2636255736Sdavidchbxe_drv_pulse(struct bxe_softc *sc)
2637255736Sdavidch{
2638255736Sdavidch    SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2639255736Sdavidch             sc->fw_drv_pulse_wr_seq);
2640255736Sdavidch}
2641255736Sdavidch
2642255736Sdavidchstatic inline uint16_t
2643255736Sdavidchbxe_tx_avail(struct bxe_softc *sc,
2644255736Sdavidch             struct bxe_fastpath *fp)
2645255736Sdavidch{
2646255736Sdavidch    int16_t  used;
2647255736Sdavidch    uint16_t prod;
2648255736Sdavidch    uint16_t cons;
2649255736Sdavidch
2650255736Sdavidch    prod = fp->tx_bd_prod;
2651255736Sdavidch    cons = fp->tx_bd_cons;
2652255736Sdavidch
2653255736Sdavidch    used = SUB_S16(prod, cons);
2654255736Sdavidch
2655255736Sdavidch    return (int16_t)(sc->tx_ring_size) - used;
2656255736Sdavidch}
2657255736Sdavidch
2658255736Sdavidchstatic inline int
2659255736Sdavidchbxe_tx_queue_has_work(struct bxe_fastpath *fp)
2660255736Sdavidch{
2661255736Sdavidch    uint16_t hw_cons;
2662255736Sdavidch
2663255736Sdavidch    mb(); /* status block fields can change */
2664255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
2665255736Sdavidch    return (hw_cons != fp->tx_pkt_cons);
2666255736Sdavidch}
2667255736Sdavidch
2668255736Sdavidchstatic inline uint8_t
2669255736Sdavidchbxe_has_tx_work(struct bxe_fastpath *fp)
2670255736Sdavidch{
2671255736Sdavidch    /* expand this for multi-cos if ever supported */
2672255736Sdavidch    return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2673255736Sdavidch}
2674255736Sdavidch
2675255736Sdavidchstatic inline int
2676255736Sdavidchbxe_has_rx_work(struct bxe_fastpath *fp)
2677255736Sdavidch{
2678255736Sdavidch    uint16_t rx_cq_cons_sb;
2679255736Sdavidch
2680255736Sdavidch    mb(); /* status block fields can change */
2681255736Sdavidch    rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2682255736Sdavidch    if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2683255736Sdavidch        rx_cq_cons_sb++;
2684255736Sdavidch    return (fp->rx_cq_cons != rx_cq_cons_sb);
2685255736Sdavidch}
2686255736Sdavidch
2687255736Sdavidchstatic void
2688255736Sdavidchbxe_sp_event(struct bxe_softc    *sc,
2689255736Sdavidch             struct bxe_fastpath *fp,
2690255736Sdavidch             union eth_rx_cqe    *rr_cqe)
2691255736Sdavidch{
2692255736Sdavidch    int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2693255736Sdavidch    int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2694255736Sdavidch    enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2695255736Sdavidch    struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2696255736Sdavidch
2697255736Sdavidch    BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2698255736Sdavidch          fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2699255736Sdavidch
2700255736Sdavidch    switch (command) {
2701255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2702255736Sdavidch        BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2703255736Sdavidch        drv_cmd = ECORE_Q_CMD_UPDATE;
2704255736Sdavidch        break;
2705255736Sdavidch
2706255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2707255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2708255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP;
2709255736Sdavidch        break;
2710255736Sdavidch
2711255736Sdavidch    case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2712255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2713255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2714255736Sdavidch        break;
2715255736Sdavidch
2716255736Sdavidch    case (RAMROD_CMD_ID_ETH_HALT):
2717255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2718255736Sdavidch        drv_cmd = ECORE_Q_CMD_HALT;
2719255736Sdavidch        break;
2720255736Sdavidch
2721255736Sdavidch    case (RAMROD_CMD_ID_ETH_TERMINATE):
2722255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2723255736Sdavidch        drv_cmd = ECORE_Q_CMD_TERMINATE;
2724255736Sdavidch        break;
2725255736Sdavidch
2726255736Sdavidch    case (RAMROD_CMD_ID_ETH_EMPTY):
2727255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2728255736Sdavidch        drv_cmd = ECORE_Q_CMD_EMPTY;
2729255736Sdavidch        break;
2730255736Sdavidch
2731255736Sdavidch    default:
2732255736Sdavidch        BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2733255736Sdavidch              command, fp->index);
2734255736Sdavidch        return;
2735255736Sdavidch    }
2736255736Sdavidch
2737255736Sdavidch    if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2738255736Sdavidch        q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2739255736Sdavidch        /*
2740255736Sdavidch         * q_obj->complete_cmd() failure means that this was
2741255736Sdavidch         * an unexpected completion.
2742255736Sdavidch         *
2743255736Sdavidch         * In this case we don't want to increase the sc->spq_left
2744255736Sdavidch         * because apparently we haven't sent this command the first
2745255736Sdavidch         * place.
2746255736Sdavidch         */
2747255736Sdavidch        // bxe_panic(sc, ("Unexpected SP completion\n"));
2748255736Sdavidch        return;
2749255736Sdavidch    }
2750255736Sdavidch
2751255736Sdavidch    atomic_add_acq_long(&sc->cq_spq_left, 1);
2752255736Sdavidch
2753255736Sdavidch    BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2754255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left));
2755255736Sdavidch}
2756255736Sdavidch
2757255736Sdavidch/*
2758255736Sdavidch * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2759255736Sdavidch * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2760255736Sdavidch * the current aggregation queue as in-progress.
2761255736Sdavidch */
2762255736Sdavidchstatic void
2763255736Sdavidchbxe_tpa_start(struct bxe_softc            *sc,
2764255736Sdavidch              struct bxe_fastpath         *fp,
2765255736Sdavidch              uint16_t                    queue,
2766255736Sdavidch              uint16_t                    cons,
2767255736Sdavidch              uint16_t                    prod,
2768255736Sdavidch              struct eth_fast_path_rx_cqe *cqe)
2769255736Sdavidch{
2770255736Sdavidch    struct bxe_sw_rx_bd tmp_bd;
2771255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
2772255736Sdavidch    struct eth_rx_bd *rx_bd;
2773255736Sdavidch    int max_agg_queues;
2774255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2775255736Sdavidch    uint16_t index;
2776255736Sdavidch
2777255736Sdavidch    BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2778255736Sdavidch                       "cons=%d prod=%d\n",
2779255736Sdavidch          fp->index, queue, cons, prod);
2780255736Sdavidch
2781255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
2782255736Sdavidch
2783255736Sdavidch    KASSERT((queue < max_agg_queues),
2784255736Sdavidch            ("fp[%02d] invalid aggr queue (%d >= %d)!",
2785255736Sdavidch             fp->index, queue, max_agg_queues));
2786255736Sdavidch
2787255736Sdavidch    KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2788255736Sdavidch            ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2789255736Sdavidch             fp->index, queue));
2790255736Sdavidch
2791255736Sdavidch    /* copy the existing mbuf and mapping from the TPA pool */
2792255736Sdavidch    tmp_bd = tpa_info->bd;
2793255736Sdavidch
2794255736Sdavidch    if (tmp_bd.m == NULL) {
2795295830Sdavidcs        uint32_t *tmp;
2796295830Sdavidcs
2797295830Sdavidcs        tmp = (uint32_t *)cqe;
2798295830Sdavidcs
2799295830Sdavidcs        BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2800295830Sdavidcs              fp->index, queue, cons, prod);
2801295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2802295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2803295830Sdavidcs
2804255736Sdavidch        /* XXX Error handling? */
2805255736Sdavidch        return;
2806255736Sdavidch    }
2807255736Sdavidch
2808255736Sdavidch    /* change the TPA queue to the start state */
2809255736Sdavidch    tpa_info->state            = BXE_TPA_STATE_START;
2810255736Sdavidch    tpa_info->placement_offset = cqe->placement_offset;
2811255736Sdavidch    tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2812255736Sdavidch    tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2813255736Sdavidch    tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2814255736Sdavidch
2815255736Sdavidch    fp->rx_tpa_queue_used |= (1 << queue);
2816255736Sdavidch
2817255736Sdavidch    /*
2818255736Sdavidch     * If all the buffer descriptors are filled with mbufs then fill in
2819255736Sdavidch     * the current consumer index with a new BD. Else if a maximum Rx
2820255736Sdavidch     * buffer limit is imposed then fill in the next producer index.
2821255736Sdavidch     */
2822255736Sdavidch    index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2823255736Sdavidch                prod : cons;
2824255736Sdavidch
2825255736Sdavidch    /* move the received mbuf and mapping to TPA pool */
2826255736Sdavidch    tpa_info->bd = fp->rx_mbuf_chain[cons];
2827255736Sdavidch
2828255736Sdavidch    /* release any existing RX BD mbuf mappings */
2829255736Sdavidch    if (cons != index) {
2830255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[cons];
2831255736Sdavidch
2832255736Sdavidch        if (rx_buf->m_map != NULL) {
2833255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2834255736Sdavidch                            BUS_DMASYNC_POSTREAD);
2835255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2836255736Sdavidch        }
2837255736Sdavidch
2838255736Sdavidch        /*
2839255736Sdavidch         * We get here when the maximum number of rx buffers is less than
2840255736Sdavidch         * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2841255736Sdavidch         * it out here without concern of a memory leak.
2842255736Sdavidch         */
2843255736Sdavidch        fp->rx_mbuf_chain[cons].m = NULL;
2844255736Sdavidch    }
2845255736Sdavidch
2846255736Sdavidch    /* update the Rx SW BD with the mbuf info from the TPA pool */
2847255736Sdavidch    fp->rx_mbuf_chain[index] = tmp_bd;
2848255736Sdavidch
2849255736Sdavidch    /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2850255736Sdavidch    rx_bd = &fp->rx_chain[index];
2851255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2852255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2853255736Sdavidch}
2854255736Sdavidch
2855255736Sdavidch/*
2856255736Sdavidch * When a TPA aggregation is completed, loop through the individual mbufs
2857255736Sdavidch * of the aggregation, combining them into a single mbuf which will be sent
2858255736Sdavidch * up the stack. Refill all freed SGEs with mbufs as we go along.
2859255736Sdavidch */
2860255736Sdavidchstatic int
2861255736Sdavidchbxe_fill_frag_mbuf(struct bxe_softc          *sc,
2862255736Sdavidch                   struct bxe_fastpath       *fp,
2863255736Sdavidch                   struct bxe_sw_tpa_info    *tpa_info,
2864255736Sdavidch                   uint16_t                  queue,
2865255736Sdavidch                   uint16_t                  pages,
2866255736Sdavidch                   struct mbuf               *m,
2867255736Sdavidch			       struct eth_end_agg_rx_cqe *cqe,
2868255736Sdavidch                   uint16_t                  cqe_idx)
2869255736Sdavidch{
2870255736Sdavidch    struct mbuf *m_frag;
2871255736Sdavidch    uint32_t frag_len, frag_size, i;
2872255736Sdavidch    uint16_t sge_idx;
2873255736Sdavidch    int rc = 0;
2874255736Sdavidch    int j;
2875255736Sdavidch
2876255736Sdavidch    frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2877255736Sdavidch
2878255736Sdavidch    BLOGD(sc, DBG_LRO,
2879255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2880255736Sdavidch          fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2881255736Sdavidch
2882255736Sdavidch    /* make sure the aggregated frame is not too big to handle */
2883255736Sdavidch    if (pages > 8 * PAGES_PER_SGE) {
2884295830Sdavidcs
2885295830Sdavidcs        uint32_t *tmp = (uint32_t *)cqe;
2886295830Sdavidcs
2887255736Sdavidch        BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2888255736Sdavidch                  "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2889255736Sdavidch              fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2890255736Sdavidch              tpa_info->len_on_bd, frag_size);
2891295830Sdavidcs
2892295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2893295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2894295830Sdavidcs
2895255736Sdavidch        bxe_panic(sc, ("sge page count error\n"));
2896255736Sdavidch        return (EINVAL);
2897255736Sdavidch    }
2898255736Sdavidch
2899255736Sdavidch    /*
2900255736Sdavidch     * Scan through the scatter gather list pulling individual mbufs into a
2901255736Sdavidch     * single mbuf for the host stack.
2902255736Sdavidch     */
2903255736Sdavidch    for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2904255736Sdavidch        sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2905255736Sdavidch
2906255736Sdavidch        /*
2907255736Sdavidch         * Firmware gives the indices of the SGE as if the ring is an array
2908255736Sdavidch         * (meaning that the "next" element will consume 2 indices).
2909255736Sdavidch         */
2910255736Sdavidch        frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2911255736Sdavidch
2912255736Sdavidch        BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2913255736Sdavidch                           "sge_idx=%d frag_size=%d frag_len=%d\n",
2914255736Sdavidch              fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2915255736Sdavidch
2916255736Sdavidch        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2917255736Sdavidch
2918255736Sdavidch        /* allocate a new mbuf for the SGE */
2919255736Sdavidch        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2920255736Sdavidch        if (rc) {
2921255736Sdavidch            /* Leave all remaining SGEs in the ring! */
2922255736Sdavidch            return (rc);
2923255736Sdavidch        }
2924255736Sdavidch
2925255736Sdavidch        /* update the fragment length */
2926255736Sdavidch        m_frag->m_len = frag_len;
2927255736Sdavidch
2928255736Sdavidch        /* concatenate the fragment to the head mbuf */
2929255736Sdavidch        m_cat(m, m_frag);
2930255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
2931255736Sdavidch
2932255736Sdavidch        /* update the TPA mbuf size and remaining fragment size */
2933255736Sdavidch        m->m_pkthdr.len += frag_len;
2934255736Sdavidch        frag_size -= frag_len;
2935255736Sdavidch    }
2936255736Sdavidch
2937255736Sdavidch    BLOGD(sc, DBG_LRO,
2938255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2939255736Sdavidch          fp->index, queue, frag_size);
2940255736Sdavidch
2941255736Sdavidch    return (rc);
2942255736Sdavidch}
2943255736Sdavidch
2944255736Sdavidchstatic inline void
2945255736Sdavidchbxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2946255736Sdavidch{
2947255736Sdavidch    int i, j;
2948255736Sdavidch
2949255736Sdavidch    for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2950255736Sdavidch        int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2951255736Sdavidch
2952255736Sdavidch        for (j = 0; j < 2; j++) {
2953255736Sdavidch            BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2954255736Sdavidch            idx--;
2955255736Sdavidch        }
2956255736Sdavidch    }
2957255736Sdavidch}
2958255736Sdavidch
2959255736Sdavidchstatic inline void
2960255736Sdavidchbxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2961255736Sdavidch{
2962255736Sdavidch    /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2963255736Sdavidch    memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2964255736Sdavidch
2965255736Sdavidch    /*
2966255736Sdavidch     * Clear the two last indices in the page to 1. These are the indices that
2967255736Sdavidch     * correspond to the "next" element, hence will never be indicated and
2968255736Sdavidch     * should be removed from the calculations.
2969255736Sdavidch     */
2970255736Sdavidch    bxe_clear_sge_mask_next_elems(fp);
2971255736Sdavidch}
2972255736Sdavidch
2973255736Sdavidchstatic inline void
2974255736Sdavidchbxe_update_last_max_sge(struct bxe_fastpath *fp,
2975255736Sdavidch                        uint16_t            idx)
2976255736Sdavidch{
2977255736Sdavidch    uint16_t last_max = fp->last_max_sge;
2978255736Sdavidch
2979255736Sdavidch    if (SUB_S16(idx, last_max) > 0) {
2980255736Sdavidch        fp->last_max_sge = idx;
2981255736Sdavidch    }
2982255736Sdavidch}
2983255736Sdavidch
2984255736Sdavidchstatic inline void
2985255736Sdavidchbxe_update_sge_prod(struct bxe_softc          *sc,
2986255736Sdavidch                    struct bxe_fastpath       *fp,
2987255736Sdavidch                    uint16_t                  sge_len,
2988283269Sdavidcs                    union eth_sgl_or_raw_data *cqe)
2989255736Sdavidch{
2990255736Sdavidch    uint16_t last_max, last_elem, first_elem;
2991255736Sdavidch    uint16_t delta = 0;
2992255736Sdavidch    uint16_t i;
2993255736Sdavidch
2994255736Sdavidch    if (!sge_len) {
2995255736Sdavidch        return;
2996255736Sdavidch    }
2997255736Sdavidch
2998255736Sdavidch    /* first mark all used pages */
2999255736Sdavidch    for (i = 0; i < sge_len; i++) {
3000255736Sdavidch        BIT_VEC64_CLEAR_BIT(fp->sge_mask,
3001283269Sdavidcs                            RX_SGE(le16toh(cqe->sgl[i])));
3002255736Sdavidch    }
3003255736Sdavidch
3004255736Sdavidch    BLOGD(sc, DBG_LRO,
3005255736Sdavidch          "fp[%02d] fp_cqe->sgl[%d] = %d\n",
3006255736Sdavidch          fp->index, sge_len - 1,
3007283269Sdavidcs          le16toh(cqe->sgl[sge_len - 1]));
3008255736Sdavidch
3009255736Sdavidch    /* assume that the last SGE index is the biggest */
3010255736Sdavidch    bxe_update_last_max_sge(fp,
3011283269Sdavidcs                            le16toh(cqe->sgl[sge_len - 1]));
3012255736Sdavidch
3013255736Sdavidch    last_max = RX_SGE(fp->last_max_sge);
3014255736Sdavidch    last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
3015255736Sdavidch    first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
3016255736Sdavidch
3017255736Sdavidch    /* if ring is not full */
3018255736Sdavidch    if (last_elem + 1 != first_elem) {
3019255736Sdavidch        last_elem++;
3020255736Sdavidch    }
3021255736Sdavidch
3022255736Sdavidch    /* now update the prod */
3023255736Sdavidch    for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
3024255736Sdavidch        if (__predict_true(fp->sge_mask[i])) {
3025255736Sdavidch            break;
3026255736Sdavidch        }
3027255736Sdavidch
3028255736Sdavidch        fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
3029255736Sdavidch        delta += BIT_VEC64_ELEM_SZ;
3030255736Sdavidch    }
3031255736Sdavidch
3032255736Sdavidch    if (delta > 0) {
3033255736Sdavidch        fp->rx_sge_prod += delta;
3034255736Sdavidch        /* clear page-end entries */
3035255736Sdavidch        bxe_clear_sge_mask_next_elems(fp);
3036255736Sdavidch    }
3037255736Sdavidch
3038255736Sdavidch    BLOGD(sc, DBG_LRO,
3039255736Sdavidch          "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
3040255736Sdavidch          fp->index, fp->last_max_sge, fp->rx_sge_prod);
3041255736Sdavidch}
3042255736Sdavidch
3043255736Sdavidch/*
3044255736Sdavidch * The aggregation on the current TPA queue has completed. Pull the individual
3045255736Sdavidch * mbuf fragments together into a single mbuf, perform all necessary checksum
3046255736Sdavidch * calculations, and send the resuting mbuf to the stack.
3047255736Sdavidch */
3048255736Sdavidchstatic void
3049255736Sdavidchbxe_tpa_stop(struct bxe_softc          *sc,
3050255736Sdavidch             struct bxe_fastpath       *fp,
3051255736Sdavidch             struct bxe_sw_tpa_info    *tpa_info,
3052255736Sdavidch             uint16_t                  queue,
3053255736Sdavidch             uint16_t                  pages,
3054255736Sdavidch			 struct eth_end_agg_rx_cqe *cqe,
3055255736Sdavidch             uint16_t                  cqe_idx)
3056255736Sdavidch{
3057266979Smarcel    if_t ifp = sc->ifp;
3058255736Sdavidch    struct mbuf *m;
3059255736Sdavidch    int rc = 0;
3060255736Sdavidch
3061255736Sdavidch    BLOGD(sc, DBG_LRO,
3062255736Sdavidch          "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3063255736Sdavidch          fp->index, queue, tpa_info->placement_offset,
3064255736Sdavidch          le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3065255736Sdavidch
3066255736Sdavidch    m = tpa_info->bd.m;
3067255736Sdavidch
3068255736Sdavidch    /* allocate a replacement before modifying existing mbuf */
3069255736Sdavidch    rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3070255736Sdavidch    if (rc) {
3071255736Sdavidch        /* drop the frame and log an error */
3072255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3073255736Sdavidch        goto bxe_tpa_stop_exit;
3074255736Sdavidch    }
3075255736Sdavidch
3076255736Sdavidch    /* we have a replacement, fixup the current mbuf */
3077255736Sdavidch    m_adj(m, tpa_info->placement_offset);
3078255736Sdavidch    m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3079255736Sdavidch
3080255736Sdavidch    /* mark the checksums valid (taken care of by the firmware) */
3081255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3082255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3083255736Sdavidch    m->m_pkthdr.csum_data = 0xffff;
3084255736Sdavidch    m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3085255736Sdavidch                               CSUM_IP_VALID   |
3086255736Sdavidch                               CSUM_DATA_VALID |
3087255736Sdavidch                               CSUM_PSEUDO_HDR);
3088255736Sdavidch
3089255736Sdavidch    /* aggregate all of the SGEs into a single mbuf */
3090255736Sdavidch    rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3091255736Sdavidch    if (rc) {
3092255736Sdavidch        /* drop the packet and log an error */
3093255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3094255736Sdavidch        m_freem(m);
3095255736Sdavidch    } else {
3096296071Sdavidcs        if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3097255736Sdavidch            m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3098255736Sdavidch            m->m_flags |= M_VLANTAG;
3099255736Sdavidch        }
3100255736Sdavidch
3101255736Sdavidch        /* assign packet to this interface interface */
3102266979Smarcel        if_setrcvif(m, ifp);
3103255736Sdavidch
3104255736Sdavidch#if __FreeBSD_version >= 800000
3105255736Sdavidch        /* specify what RSS queue was used for this flow */
3106255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3107297155Sdavidcs        BXE_SET_FLOWID(m);
3108255736Sdavidch#endif
3109255736Sdavidch
3110271782Sglebius        if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3111255736Sdavidch        fp->eth_q_stats.rx_tpa_pkts++;
3112255736Sdavidch
3113255736Sdavidch        /* pass the frame to the stack */
3114266979Smarcel        if_input(ifp, m);
3115255736Sdavidch    }
3116255736Sdavidch
3117255736Sdavidch    /* we passed an mbuf up the stack or dropped the frame */
3118255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa--;
3119255736Sdavidch
3120255736Sdavidchbxe_tpa_stop_exit:
3121255736Sdavidch
3122255736Sdavidch    fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3123255736Sdavidch    fp->rx_tpa_queue_used &= ~(1 << queue);
3124255736Sdavidch}
3125255736Sdavidch
3126255736Sdavidchstatic uint8_t
3127283269Sdavidcsbxe_service_rxsgl(
3128283269Sdavidcs                 struct bxe_fastpath *fp,
3129283269Sdavidcs                 uint16_t len,
3130283269Sdavidcs                 uint16_t lenonbd,
3131283269Sdavidcs                 struct mbuf *m,
3132283269Sdavidcs                 struct eth_fast_path_rx_cqe *cqe_fp)
3133283269Sdavidcs{
3134283269Sdavidcs    struct mbuf *m_frag;
3135283269Sdavidcs    uint16_t frags, frag_len;
3136283269Sdavidcs    uint16_t sge_idx = 0;
3137283269Sdavidcs    uint16_t j;
3138283269Sdavidcs    uint8_t i, rc = 0;
3139283269Sdavidcs    uint32_t frag_size;
3140283269Sdavidcs
3141283269Sdavidcs    /* adjust the mbuf */
3142283269Sdavidcs    m->m_len = lenonbd;
3143283269Sdavidcs
3144283269Sdavidcs    frag_size =  len - lenonbd;
3145283269Sdavidcs    frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3146283269Sdavidcs
3147283269Sdavidcs    for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3148283269Sdavidcs        sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3149283269Sdavidcs
3150283269Sdavidcs        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3151283269Sdavidcs        frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3152283269Sdavidcs        m_frag->m_len = frag_len;
3153283269Sdavidcs
3154283269Sdavidcs       /* allocate a new mbuf for the SGE */
3155283269Sdavidcs        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3156283269Sdavidcs        if (rc) {
3157283269Sdavidcs            /* Leave all remaining SGEs in the ring! */
3158283269Sdavidcs            return (rc);
3159283269Sdavidcs        }
3160283269Sdavidcs        fp->eth_q_stats.mbuf_alloc_sge--;
3161283269Sdavidcs
3162283269Sdavidcs        /* concatenate the fragment to the head mbuf */
3163283269Sdavidcs        m_cat(m, m_frag);
3164283269Sdavidcs
3165283269Sdavidcs        frag_size -= frag_len;
3166283269Sdavidcs    }
3167283269Sdavidcs
3168283269Sdavidcs    bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3169283269Sdavidcs
3170283269Sdavidcs    return rc;
3171283269Sdavidcs}
3172283269Sdavidcs
3173283269Sdavidcsstatic uint8_t
3174255736Sdavidchbxe_rxeof(struct bxe_softc    *sc,
3175255736Sdavidch          struct bxe_fastpath *fp)
3176255736Sdavidch{
3177266979Smarcel    if_t ifp = sc->ifp;
3178255736Sdavidch    uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3179255736Sdavidch    uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3180255736Sdavidch    int rx_pkts = 0;
3181281006Sdavidcs    int rc = 0;
3182255736Sdavidch
3183255736Sdavidch    BXE_FP_RX_LOCK(fp);
3184255736Sdavidch
3185255736Sdavidch    /* CQ "next element" is of the size of the regular element */
3186255736Sdavidch    hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3187255736Sdavidch    if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3188255736Sdavidch        hw_cq_cons++;
3189255736Sdavidch    }
3190255736Sdavidch
3191255736Sdavidch    bd_cons = fp->rx_bd_cons;
3192255736Sdavidch    bd_prod = fp->rx_bd_prod;
3193255736Sdavidch    bd_prod_fw = bd_prod;
3194255736Sdavidch    sw_cq_cons = fp->rx_cq_cons;
3195255736Sdavidch    sw_cq_prod = fp->rx_cq_prod;
3196255736Sdavidch
3197255736Sdavidch    /*
3198255736Sdavidch     * Memory barrier necessary as speculative reads of the rx
3199255736Sdavidch     * buffer can be ahead of the index in the status block
3200255736Sdavidch     */
3201255736Sdavidch    rmb();
3202255736Sdavidch
3203255736Sdavidch    BLOGD(sc, DBG_RX,
3204255736Sdavidch          "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3205255736Sdavidch          fp->index, hw_cq_cons, sw_cq_cons);
3206255736Sdavidch
3207255736Sdavidch    while (sw_cq_cons != hw_cq_cons) {
3208255736Sdavidch        struct bxe_sw_rx_bd *rx_buf = NULL;
3209255736Sdavidch        union eth_rx_cqe *cqe;
3210255736Sdavidch        struct eth_fast_path_rx_cqe *cqe_fp;
3211255736Sdavidch        uint8_t cqe_fp_flags;
3212255736Sdavidch        enum eth_rx_cqe_type cqe_fp_type;
3213283269Sdavidcs        uint16_t len, lenonbd,  pad;
3214255736Sdavidch        struct mbuf *m = NULL;
3215255736Sdavidch
3216255736Sdavidch        comp_ring_cons = RCQ(sw_cq_cons);
3217255736Sdavidch        bd_prod = RX_BD(bd_prod);
3218255736Sdavidch        bd_cons = RX_BD(bd_cons);
3219255736Sdavidch
3220255736Sdavidch        cqe          = &fp->rcq_chain[comp_ring_cons];
3221255736Sdavidch        cqe_fp       = &cqe->fast_path_cqe;
3222255736Sdavidch        cqe_fp_flags = cqe_fp->type_error_flags;
3223255736Sdavidch        cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3224255736Sdavidch
3225255736Sdavidch        BLOGD(sc, DBG_RX,
3226255736Sdavidch              "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3227255736Sdavidch              "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3228283269Sdavidcs              "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3229255736Sdavidch              fp->index,
3230255736Sdavidch              hw_cq_cons,
3231255736Sdavidch              sw_cq_cons,
3232255736Sdavidch              bd_prod,
3233255736Sdavidch              bd_cons,
3234255736Sdavidch              CQE_TYPE(cqe_fp_flags),
3235255736Sdavidch              cqe_fp_flags,
3236255736Sdavidch              cqe_fp->status_flags,
3237255736Sdavidch              le32toh(cqe_fp->rss_hash_result),
3238255736Sdavidch              le16toh(cqe_fp->vlan_tag),
3239283269Sdavidcs              le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3240283269Sdavidcs              le16toh(cqe_fp->len_on_bd));
3241255736Sdavidch
3242255736Sdavidch        /* is this a slowpath msg? */
3243255736Sdavidch        if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3244255736Sdavidch            bxe_sp_event(sc, fp, cqe);
3245255736Sdavidch            goto next_cqe;
3246255736Sdavidch        }
3247255736Sdavidch
3248255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[bd_cons];
3249255736Sdavidch
3250255736Sdavidch        if (!CQE_TYPE_FAST(cqe_fp_type)) {
3251255736Sdavidch            struct bxe_sw_tpa_info *tpa_info;
3252255736Sdavidch            uint16_t frag_size, pages;
3253255736Sdavidch            uint8_t queue;
3254255736Sdavidch
3255255736Sdavidch            if (CQE_TYPE_START(cqe_fp_type)) {
3256255736Sdavidch                bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3257255736Sdavidch                              bd_cons, bd_prod, cqe_fp);
3258255736Sdavidch                m = NULL; /* packet not ready yet */
3259255736Sdavidch                goto next_rx;
3260255736Sdavidch            }
3261255736Sdavidch
3262255736Sdavidch            KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3263255736Sdavidch                    ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3264255736Sdavidch
3265255736Sdavidch            queue = cqe->end_agg_cqe.queue_index;
3266255736Sdavidch            tpa_info = &fp->rx_tpa_info[queue];
3267255736Sdavidch
3268255736Sdavidch            BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3269255736Sdavidch                  fp->index, queue);
3270255736Sdavidch
3271255736Sdavidch            frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3272255736Sdavidch                         tpa_info->len_on_bd);
3273255736Sdavidch            pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3274255736Sdavidch
3275255736Sdavidch            bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3276255736Sdavidch                         &cqe->end_agg_cqe, comp_ring_cons);
3277255736Sdavidch
3278283269Sdavidcs            bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3279255736Sdavidch
3280255736Sdavidch            goto next_cqe;
3281255736Sdavidch        }
3282255736Sdavidch
3283255736Sdavidch        /* non TPA */
3284255736Sdavidch
3285255736Sdavidch        /* is this an error packet? */
3286255736Sdavidch        if (__predict_false(cqe_fp_flags &
3287255736Sdavidch                            ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3288255736Sdavidch            BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3289255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3290255736Sdavidch            goto next_rx;
3291255736Sdavidch        }
3292255736Sdavidch
3293255736Sdavidch        len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3294283269Sdavidcs        lenonbd = le16toh(cqe_fp->len_on_bd);
3295255736Sdavidch        pad = cqe_fp->placement_offset;
3296255736Sdavidch
3297255736Sdavidch        m = rx_buf->m;
3298255736Sdavidch
3299255736Sdavidch        if (__predict_false(m == NULL)) {
3300255736Sdavidch            BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3301255736Sdavidch                  bd_cons, fp->index);
3302255736Sdavidch            goto next_rx;
3303255736Sdavidch        }
3304255736Sdavidch
3305255736Sdavidch        /* XXX double copy if packet length under a threshold */
3306255736Sdavidch
3307255736Sdavidch        /*
3308255736Sdavidch         * If all the buffer descriptors are filled with mbufs then fill in
3309255736Sdavidch         * the current consumer index with a new BD. Else if a maximum Rx
3310255736Sdavidch         * buffer limit is imposed then fill in the next producer index.
3311255736Sdavidch         */
3312255736Sdavidch        rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3313255736Sdavidch                                  (sc->max_rx_bufs != RX_BD_USABLE) ?
3314255736Sdavidch                                      bd_prod : bd_cons);
3315255736Sdavidch        if (rc != 0) {
3316281006Sdavidcs
3317281006Sdavidcs            /* we simply reuse the received mbuf and don't post it to the stack */
3318281006Sdavidcs            m = NULL;
3319281006Sdavidcs
3320255736Sdavidch            BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3321255736Sdavidch                  fp->index, rc);
3322255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3323255736Sdavidch
3324255736Sdavidch            if (sc->max_rx_bufs != RX_BD_USABLE) {
3325255736Sdavidch                /* copy this consumer index to the producer index */
3326255736Sdavidch                memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3327255736Sdavidch                       sizeof(struct bxe_sw_rx_bd));
3328255736Sdavidch                memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3329255736Sdavidch            }
3330255736Sdavidch
3331255736Sdavidch            goto next_rx;
3332255736Sdavidch        }
3333255736Sdavidch
3334255736Sdavidch        /* current mbuf was detached from the bd */
3335255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
3336255736Sdavidch
3337255736Sdavidch        /* we allocated a replacement mbuf, fixup the current one */
3338255736Sdavidch        m_adj(m, pad);
3339255736Sdavidch        m->m_pkthdr.len = m->m_len = len;
3340255736Sdavidch
3341292638Sdavidcs        if ((len > 60) && (len > lenonbd)) {
3342292638Sdavidcs            fp->eth_q_stats.rx_bxe_service_rxsgl++;
3343283269Sdavidcs            rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3344283269Sdavidcs            if (rc)
3345283269Sdavidcs                break;
3346283274Sdavidcs            fp->eth_q_stats.rx_jumbo_sge_pkts++;
3347292638Sdavidcs        } else if (lenonbd < len) {
3348292638Sdavidcs            fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3349283269Sdavidcs        }
3350283269Sdavidcs
3351255736Sdavidch        /* assign packet to this interface interface */
3352266979Smarcel	if_setrcvif(m, ifp);
3353255736Sdavidch
3354255736Sdavidch        /* assume no hardware checksum has complated */
3355255736Sdavidch        m->m_pkthdr.csum_flags = 0;
3356255736Sdavidch
3357255736Sdavidch        /* validate checksum if offload enabled */
3358266979Smarcel        if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3359255736Sdavidch            /* check for a valid IP frame */
3360255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3361255736Sdavidch                  ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3362255736Sdavidch                m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3363255736Sdavidch                if (__predict_false(cqe_fp_flags &
3364255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3365255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3366255736Sdavidch                } else {
3367255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3368255736Sdavidch                    m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3369255736Sdavidch                }
3370255736Sdavidch            }
3371255736Sdavidch
3372255736Sdavidch            /* check for a valid TCP/UDP frame */
3373255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3374255736Sdavidch                  ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3375255736Sdavidch                if (__predict_false(cqe_fp_flags &
3376255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3377255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3378255736Sdavidch                } else {
3379255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3380255736Sdavidch                    m->m_pkthdr.csum_data = 0xFFFF;
3381255736Sdavidch                    m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3382255736Sdavidch                                               CSUM_PSEUDO_HDR);
3383255736Sdavidch                }
3384255736Sdavidch            }
3385255736Sdavidch        }
3386255736Sdavidch
3387255736Sdavidch        /* if there is a VLAN tag then flag that info */
3388296071Sdavidcs        if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3389255736Sdavidch            m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3390255736Sdavidch            m->m_flags |= M_VLANTAG;
3391255736Sdavidch        }
3392255736Sdavidch
3393255736Sdavidch#if __FreeBSD_version >= 800000
3394255736Sdavidch        /* specify what RSS queue was used for this flow */
3395255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3396297155Sdavidcs        BXE_SET_FLOWID(m);
3397255736Sdavidch#endif
3398255736Sdavidch
3399255736Sdavidchnext_rx:
3400255736Sdavidch
3401255736Sdavidch        bd_cons    = RX_BD_NEXT(bd_cons);
3402255736Sdavidch        bd_prod    = RX_BD_NEXT(bd_prod);
3403255736Sdavidch        bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3404255736Sdavidch
3405255736Sdavidch        /* pass the frame to the stack */
3406255736Sdavidch        if (__predict_true(m != NULL)) {
3407271782Sglebius            if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3408255736Sdavidch            rx_pkts++;
3409266979Smarcel            if_input(ifp, m);
3410255736Sdavidch        }
3411255736Sdavidch
3412255736Sdavidchnext_cqe:
3413255736Sdavidch
3414255736Sdavidch        sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3415255736Sdavidch        sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3416255736Sdavidch
3417255736Sdavidch        /* limit spinning on the queue */
3418281006Sdavidcs        if (rc != 0)
3419281006Sdavidcs            break;
3420281006Sdavidcs
3421255736Sdavidch        if (rx_pkts == sc->rx_budget) {
3422255736Sdavidch            fp->eth_q_stats.rx_budget_reached++;
3423255736Sdavidch            break;
3424255736Sdavidch        }
3425255736Sdavidch    } /* while work to do */
3426255736Sdavidch
3427255736Sdavidch    fp->rx_bd_cons = bd_cons;
3428255736Sdavidch    fp->rx_bd_prod = bd_prod_fw;
3429255736Sdavidch    fp->rx_cq_cons = sw_cq_cons;
3430255736Sdavidch    fp->rx_cq_prod = sw_cq_prod;
3431255736Sdavidch
3432255736Sdavidch    /* Update producers */
3433255736Sdavidch    bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3434255736Sdavidch
3435255736Sdavidch    fp->eth_q_stats.rx_pkts += rx_pkts;
3436255736Sdavidch    fp->eth_q_stats.rx_calls++;
3437255736Sdavidch
3438255736Sdavidch    BXE_FP_RX_UNLOCK(fp);
3439255736Sdavidch
3440255736Sdavidch    return (sw_cq_cons != hw_cq_cons);
3441255736Sdavidch}
3442255736Sdavidch
3443255736Sdavidchstatic uint16_t
3444255736Sdavidchbxe_free_tx_pkt(struct bxe_softc    *sc,
3445255736Sdavidch                struct bxe_fastpath *fp,
3446255736Sdavidch                uint16_t            idx)
3447255736Sdavidch{
3448255736Sdavidch    struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3449255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
3450255736Sdavidch    uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3451255736Sdavidch    uint16_t new_cons;
3452255736Sdavidch    int nbd;
3453255736Sdavidch
3454255736Sdavidch    /* unmap the mbuf from non-paged memory */
3455255736Sdavidch    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3456255736Sdavidch
3457255736Sdavidch    tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3458255736Sdavidch    nbd = le16toh(tx_start_bd->nbd) - 1;
3459255736Sdavidch
3460255736Sdavidch    new_cons = (tx_buf->first_bd + nbd);
3461255736Sdavidch
3462255736Sdavidch    /* free the mbuf */
3463255736Sdavidch    if (__predict_true(tx_buf->m != NULL)) {
3464255736Sdavidch        m_freem(tx_buf->m);
3465255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx--;
3466255736Sdavidch    } else {
3467255736Sdavidch        fp->eth_q_stats.tx_chain_lost_mbuf++;
3468255736Sdavidch    }
3469255736Sdavidch
3470255736Sdavidch    tx_buf->m = NULL;
3471255736Sdavidch    tx_buf->first_bd = 0;
3472255736Sdavidch
3473255736Sdavidch    return (new_cons);
3474255736Sdavidch}
3475255736Sdavidch
3476255736Sdavidch/* transmit timeout watchdog */
3477255736Sdavidchstatic int
3478255736Sdavidchbxe_watchdog(struct bxe_softc    *sc,
3479255736Sdavidch             struct bxe_fastpath *fp)
3480255736Sdavidch{
3481255736Sdavidch    BXE_FP_TX_LOCK(fp);
3482255736Sdavidch
3483255736Sdavidch    if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3484255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
3485255736Sdavidch        return (0);
3486255736Sdavidch    }
3487255736Sdavidch
3488258187Sedavis    BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3489258187Sedavis
3490255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
3491339881Sdavidcs    BXE_SET_ERROR_BIT(sc, BXE_ERR_TXQ_STUCK);
3492339881Sdavidcs    taskqueue_enqueue_timeout(taskqueue_thread,
3493339881Sdavidcs        &sc->sp_err_timeout_task, hz/10);
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];
4249339881Sdavidcs	fp->watchdog_timer = 0;
4250296579Sdavidcs        BXE_FP_TX_LOCK(fp);
4251296579Sdavidcs        BXE_FP_TX_UNLOCK(fp);
4252296579Sdavidcs    }
4253296579Sdavidcs
4254255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4255255736Sdavidch
4256255736Sdavidch    /* mark driver as unloaded in shmem2 */
4257255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4258255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4259255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4260255736Sdavidch                  val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4261255736Sdavidch    }
4262255736Sdavidch
4263255736Sdavidch    if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4264255736Sdavidch        (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4265255736Sdavidch
4266339881Sdavidcs	if(CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
4267339881Sdavidcs            /*
4268339881Sdavidcs             * We can get here if the driver has been unloaded
4269339881Sdavidcs             * during parity error recovery and is either waiting for a
4270339881Sdavidcs             * leader to complete or for other functions to unload and
4271339881Sdavidcs             * then ifconfig down has been issued. In this case we want to
4272339881Sdavidcs             * unload and let other functions to complete a recovery
4273339881Sdavidcs             * process.
4274339881Sdavidcs             */
4275339881Sdavidcs            sc->recovery_state = BXE_RECOVERY_DONE;
4276339881Sdavidcs            sc->is_leader = 0;
4277339881Sdavidcs            bxe_release_leader_lock(sc);
4278339881Sdavidcs            mb();
4279339881Sdavidcs            BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4280339881Sdavidcs	}
4281295830Sdavidcs        BLOGE(sc, "Can't unload in closed or error state recover_state 0x%x"
4282295830Sdavidcs            " state = 0x%x\n", sc->recovery_state, sc->state);
4283255736Sdavidch        return (-1);
4284255736Sdavidch    }
4285255736Sdavidch
4286255736Sdavidch    /*
4287255736Sdavidch     * Nothing to do during unload if previous bxe_nic_load()
4288298955Spfg     * did not completed successfully - all resourses are released.
4289255736Sdavidch     */
4290255736Sdavidch    if ((sc->state == BXE_STATE_CLOSED) ||
4291255736Sdavidch        (sc->state == BXE_STATE_ERROR)) {
4292255736Sdavidch        return (0);
4293255736Sdavidch    }
4294255736Sdavidch
4295255736Sdavidch    sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4296255736Sdavidch    mb();
4297255736Sdavidch
4298255736Sdavidch    /* stop tx */
4299255736Sdavidch    bxe_tx_disable(sc);
4300255736Sdavidch
4301255736Sdavidch    sc->rx_mode = BXE_RX_MODE_NONE;
4302255736Sdavidch    /* XXX set rx mode ??? */
4303255736Sdavidch
4304292639Sdavidcs    if (IS_PF(sc) && !sc->grcdump_done) {
4305255736Sdavidch        /* set ALWAYS_ALIVE bit in shmem */
4306255736Sdavidch        sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4307255736Sdavidch
4308255736Sdavidch        bxe_drv_pulse(sc);
4309255736Sdavidch
4310255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_STOP);
4311255736Sdavidch        bxe_save_statistics(sc);
4312255736Sdavidch    }
4313255736Sdavidch
4314255736Sdavidch    /* wait till consumers catch up with producers in all queues */
4315255736Sdavidch    bxe_drain_tx_queues(sc);
4316255736Sdavidch
4317255736Sdavidch    /* if VF indicate to PF this function is going down (PF will delete sp
4318255736Sdavidch     * elements and clear initializations
4319255736Sdavidch     */
4320255736Sdavidch    if (IS_VF(sc)) {
4321255736Sdavidch        ; /* bxe_vfpf_close_vf(sc); */
4322255736Sdavidch    } else if (unload_mode != UNLOAD_RECOVERY) {
4323255736Sdavidch        /* if this is a normal/close unload need to clean up chip */
4324292639Sdavidcs        if (!sc->grcdump_done)
4325292639Sdavidcs            bxe_chip_cleanup(sc, unload_mode, keep_link);
4326255736Sdavidch    } else {
4327255736Sdavidch        /* Send the UNLOAD_REQUEST to the MCP */
4328255736Sdavidch        bxe_send_unload_req(sc, unload_mode);
4329255736Sdavidch
4330255736Sdavidch        /*
4331255736Sdavidch         * Prevent transactions to host from the functions on the
4332255736Sdavidch         * engine that doesn't reset global blocks in case of global
4333255736Sdavidch         * attention once gloabl blocks are reset and gates are opened
4334255736Sdavidch         * (the engine which leader will perform the recovery
4335255736Sdavidch         * last).
4336255736Sdavidch         */
4337255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
4338255736Sdavidch            bxe_pf_disable(sc);
4339255736Sdavidch        }
4340255736Sdavidch
4341255736Sdavidch        /* disable HW interrupts */
4342255736Sdavidch        bxe_int_disable_sync(sc, TRUE);
4343255736Sdavidch
4344255736Sdavidch        /* detach interrupts */
4345255736Sdavidch        bxe_interrupt_detach(sc);
4346255736Sdavidch
4347255736Sdavidch        /* Report UNLOAD_DONE to MCP */
4348255736Sdavidch        bxe_send_unload_done(sc, FALSE);
4349255736Sdavidch    }
4350255736Sdavidch
4351255736Sdavidch    /*
4352255736Sdavidch     * At this stage no more interrupts will arrive so we may safely clean
4353255736Sdavidch     * the queue'able objects here in case they failed to get cleaned so far.
4354255736Sdavidch     */
4355255736Sdavidch    if (IS_PF(sc)) {
4356255736Sdavidch        bxe_squeeze_objects(sc);
4357255736Sdavidch    }
4358255736Sdavidch
4359255736Sdavidch    /* There should be no more pending SP commands at this stage */
4360255736Sdavidch    sc->sp_state = 0;
4361255736Sdavidch
4362255736Sdavidch    sc->port.pmf = 0;
4363255736Sdavidch
4364255736Sdavidch    bxe_free_fp_buffers(sc);
4365255736Sdavidch
4366255736Sdavidch    if (IS_PF(sc)) {
4367255736Sdavidch        bxe_free_mem(sc);
4368255736Sdavidch    }
4369255736Sdavidch
4370255736Sdavidch    bxe_free_fw_stats_mem(sc);
4371255736Sdavidch
4372255736Sdavidch    sc->state = BXE_STATE_CLOSED;
4373255736Sdavidch
4374255736Sdavidch    /*
4375255736Sdavidch     * Check if there are pending parity attentions. If there are - set
4376255736Sdavidch     * RECOVERY_IN_PROGRESS.
4377255736Sdavidch     */
4378255736Sdavidch    if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4379255736Sdavidch        bxe_set_reset_in_progress(sc);
4380255736Sdavidch
4381255736Sdavidch        /* Set RESET_IS_GLOBAL if needed */
4382255736Sdavidch        if (global) {
4383255736Sdavidch            bxe_set_reset_global(sc);
4384255736Sdavidch        }
4385255736Sdavidch    }
4386255736Sdavidch
4387255736Sdavidch    /*
4388255736Sdavidch     * The last driver must disable a "close the gate" if there is no
4389255736Sdavidch     * parity attention or "process kill" pending.
4390255736Sdavidch     */
4391255736Sdavidch    if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4392255736Sdavidch        bxe_reset_is_done(sc, SC_PATH(sc))) {
4393255736Sdavidch        bxe_disable_close_the_gate(sc);
4394255736Sdavidch    }
4395255736Sdavidch
4396255736Sdavidch    BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4397255736Sdavidch
4398337510Sdavidcs    bxe_link_report(sc);
4399337510Sdavidcs
4400255736Sdavidch    return (0);
4401255736Sdavidch}
4402255736Sdavidch
4403255736Sdavidch/*
4404255736Sdavidch * Called by the OS to set various media options (i.e. link, speed, etc.) when
4405255736Sdavidch * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4406255736Sdavidch */
4407255736Sdavidchstatic int
4408266979Smarcelbxe_ifmedia_update(struct ifnet  *ifp)
4409255736Sdavidch{
4410266979Smarcel    struct bxe_softc *sc = (struct bxe_softc *)if_getsoftc(ifp);
4411255736Sdavidch    struct ifmedia *ifm;
4412255736Sdavidch
4413255736Sdavidch    ifm = &sc->ifmedia;
4414255736Sdavidch
4415255736Sdavidch    /* We only support Ethernet media type. */
4416255736Sdavidch    if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4417255736Sdavidch        return (EINVAL);
4418255736Sdavidch    }
4419255736Sdavidch
4420255736Sdavidch    switch (IFM_SUBTYPE(ifm->ifm_media)) {
4421255736Sdavidch    case IFM_AUTO:
4422255736Sdavidch         break;
4423255736Sdavidch    case IFM_10G_CX4:
4424255736Sdavidch    case IFM_10G_SR:
4425255736Sdavidch    case IFM_10G_T:
4426255736Sdavidch    case IFM_10G_TWINAX:
4427255736Sdavidch    default:
4428255736Sdavidch        /* We don't support changing the media type. */
4429255736Sdavidch        BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4430255736Sdavidch              IFM_SUBTYPE(ifm->ifm_media));
4431255736Sdavidch        return (EINVAL);
4432255736Sdavidch    }
4433255736Sdavidch
4434255736Sdavidch    return (0);
4435255736Sdavidch}
4436255736Sdavidch
4437255736Sdavidch/*
4438255736Sdavidch * Called by the OS to get the current media status (i.e. link, speed, etc.).
4439255736Sdavidch */
4440255736Sdavidchstatic void
4441255736Sdavidchbxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4442255736Sdavidch{
4443266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4444255736Sdavidch
4445337510Sdavidcs    /* Bug 165447: the 'ifconfig' tool skips printing of the "status: ..."
4446337510Sdavidcs       line if the IFM_AVALID flag is *NOT* set. So we need to set this
4447337510Sdavidcs       flag unconditionally (irrespective of the admininistrative
4448337510Sdavidcs       'up/down' state of the interface) to ensure that that line is always
4449337510Sdavidcs       displayed.
4450337510Sdavidcs    */
4451337510Sdavidcs    ifmr->ifm_status = IFM_AVALID;
4452337510Sdavidcs
4453337510Sdavidcs    /* Setup the default interface info. */
4454337510Sdavidcs    ifmr->ifm_active = IFM_ETHER;
4455337510Sdavidcs
4456255736Sdavidch    /* Report link down if the driver isn't running. */
4457337510Sdavidcs    if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
4458255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4459337510Sdavidcs        BLOGD(sc, DBG_PHY, "in %s : nic still not loaded fully\n", __func__);
4460337510Sdavidcs        BLOGD(sc, DBG_PHY, "in %s : link_up (1) : %d\n",
4461337510Sdavidcs                __func__, sc->link_vars.link_up);
4462255736Sdavidch        return;
4463255736Sdavidch    }
4464255736Sdavidch
4465255736Sdavidch
4466255736Sdavidch    if (sc->link_vars.link_up) {
4467255736Sdavidch        ifmr->ifm_status |= IFM_ACTIVE;
4468337510Sdavidcs        ifmr->ifm_active |= IFM_FDX;
4469255736Sdavidch    } else {
4470255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4471337510Sdavidcs        BLOGD(sc, DBG_PHY, "in %s : setting IFM_NONE\n",
4472337510Sdavidcs                __func__);
4473255736Sdavidch        return;
4474255736Sdavidch    }
4475255736Sdavidch
4476255736Sdavidch    ifmr->ifm_active |= sc->media;
4477337510Sdavidcs    return;
4478255736Sdavidch}
4479255736Sdavidch
4480255736Sdavidchstatic void
4481255736Sdavidchbxe_handle_chip_tq(void *context,
4482255736Sdavidch                   int  pending)
4483255736Sdavidch{
4484255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
4485255736Sdavidch    long work = atomic_load_acq_long(&sc->chip_tq_flags);
4486255736Sdavidch
4487255736Sdavidch    switch (work)
4488255736Sdavidch    {
4489255736Sdavidch
4490255736Sdavidch    case CHIP_TQ_REINIT:
4491266979Smarcel        if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4492255736Sdavidch            /* restart the interface */
4493255736Sdavidch            BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4494255736Sdavidch            bxe_periodic_stop(sc);
4495255736Sdavidch            BXE_CORE_LOCK(sc);
4496255736Sdavidch            bxe_stop_locked(sc);
4497255736Sdavidch            bxe_init_locked(sc);
4498255736Sdavidch            BXE_CORE_UNLOCK(sc);
4499255736Sdavidch        }
4500255736Sdavidch        break;
4501255736Sdavidch
4502255736Sdavidch    default:
4503255736Sdavidch        break;
4504255736Sdavidch    }
4505255736Sdavidch}
4506255736Sdavidch
4507255736Sdavidch/*
4508255736Sdavidch * Handles any IOCTL calls from the operating system.
4509255736Sdavidch *
4510255736Sdavidch * Returns:
4511255736Sdavidch *   0 = Success, >0 Failure
4512255736Sdavidch */
4513255736Sdavidchstatic int
4514266979Smarcelbxe_ioctl(if_t ifp,
4515255736Sdavidch          u_long       command,
4516255736Sdavidch          caddr_t      data)
4517255736Sdavidch{
4518266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4519255736Sdavidch    struct ifreq *ifr = (struct ifreq *)data;
4520255736Sdavidch    int mask = 0;
4521255736Sdavidch    int reinit = 0;
4522255736Sdavidch    int error = 0;
4523255736Sdavidch
4524255736Sdavidch    int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4525255736Sdavidch    int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4526255736Sdavidch
4527255736Sdavidch    switch (command)
4528255736Sdavidch    {
4529255736Sdavidch    case SIOCSIFMTU:
4530255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4531255736Sdavidch              ifr->ifr_mtu);
4532255736Sdavidch
4533255736Sdavidch        if (sc->mtu == ifr->ifr_mtu) {
4534255736Sdavidch            /* nothing to change */
4535255736Sdavidch            break;
4536255736Sdavidch        }
4537255736Sdavidch
4538255736Sdavidch        if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4539255736Sdavidch            BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4540255736Sdavidch                  ifr->ifr_mtu, mtu_min, mtu_max);
4541255736Sdavidch            error = EINVAL;
4542255736Sdavidch            break;
4543255736Sdavidch        }
4544255736Sdavidch
4545255736Sdavidch        atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4546255736Sdavidch                             (unsigned long)ifr->ifr_mtu);
4547266979Smarcel	/*
4548266979Smarcel        atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4549255736Sdavidch                              (unsigned long)ifr->ifr_mtu);
4550266979Smarcel	XXX - Not sure why it needs to be atomic
4551266979Smarcel	*/
4552266979Smarcel	if_setmtu(ifp, ifr->ifr_mtu);
4553255736Sdavidch        reinit = 1;
4554255736Sdavidch        break;
4555255736Sdavidch
4556255736Sdavidch    case SIOCSIFFLAGS:
4557255736Sdavidch        /* toggle the interface state up or down */
4558255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4559255736Sdavidch
4560284335Sdavidcs	BXE_CORE_LOCK(sc);
4561255736Sdavidch        /* check if the interface is up */
4562266979Smarcel        if (if_getflags(ifp) & IFF_UP) {
4563266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4564255736Sdavidch                /* set the receive mode flags */
4565255736Sdavidch                bxe_set_rx_mode(sc);
4566297873Sdavidcs            } else if(sc->state != BXE_STATE_DISABLED) {
4567284335Sdavidcs		bxe_init_locked(sc);
4568255736Sdavidch            }
4569255736Sdavidch        } else {
4570266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4571284335Sdavidcs		bxe_periodic_stop(sc);
4572284335Sdavidcs		bxe_stop_locked(sc);
4573255736Sdavidch            }
4574255736Sdavidch        }
4575284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4576255736Sdavidch
4577255736Sdavidch        break;
4578255736Sdavidch
4579255736Sdavidch    case SIOCADDMULTI:
4580255736Sdavidch    case SIOCDELMULTI:
4581255736Sdavidch        /* add/delete multicast addresses */
4582255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4583255736Sdavidch
4584255736Sdavidch        /* check if the interface is up */
4585266979Smarcel        if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4586255736Sdavidch            /* set the receive mode flags */
4587284335Sdavidcs	    BXE_CORE_LOCK(sc);
4588255736Sdavidch            bxe_set_rx_mode(sc);
4589284335Sdavidcs	    BXE_CORE_UNLOCK(sc);
4590255736Sdavidch        }
4591255736Sdavidch
4592255736Sdavidch        break;
4593255736Sdavidch
4594255736Sdavidch    case SIOCSIFCAP:
4595255736Sdavidch        /* find out which capabilities have changed */
4596266979Smarcel        mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4597255736Sdavidch
4598255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4599255736Sdavidch              mask);
4600255736Sdavidch
4601255736Sdavidch        /* toggle the LRO capabilites enable flag */
4602255736Sdavidch        if (mask & IFCAP_LRO) {
4603266979Smarcel	    if_togglecapenable(ifp, IFCAP_LRO);
4604255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4605266979Smarcel                  (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4606255736Sdavidch            reinit = 1;
4607255736Sdavidch        }
4608255736Sdavidch
4609255736Sdavidch        /* toggle the TXCSUM checksum capabilites enable flag */
4610255736Sdavidch        if (mask & IFCAP_TXCSUM) {
4611266979Smarcel	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4612255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4613266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4614266979Smarcel            if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4615266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4616255736Sdavidch                                    CSUM_TCP      |
4617255736Sdavidch                                    CSUM_UDP      |
4618255736Sdavidch                                    CSUM_TSO      |
4619255736Sdavidch                                    CSUM_TCP_IPV6 |
4620266979Smarcel                                    CSUM_UDP_IPV6), 0);
4621255736Sdavidch            } else {
4622266979Smarcel		if_clearhwassist(ifp); /* XXX */
4623255736Sdavidch            }
4624255736Sdavidch        }
4625255736Sdavidch
4626255736Sdavidch        /* toggle the RXCSUM checksum capabilities enable flag */
4627255736Sdavidch        if (mask & IFCAP_RXCSUM) {
4628266979Smarcel	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4629255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4630266979Smarcel                  (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4631266979Smarcel            if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4632266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4633255736Sdavidch                                    CSUM_TCP      |
4634255736Sdavidch                                    CSUM_UDP      |
4635255736Sdavidch                                    CSUM_TSO      |
4636255736Sdavidch                                    CSUM_TCP_IPV6 |
4637266979Smarcel                                    CSUM_UDP_IPV6), 0);
4638255736Sdavidch            } else {
4639266979Smarcel		if_clearhwassist(ifp); /* XXX */
4640255736Sdavidch            }
4641255736Sdavidch        }
4642255736Sdavidch
4643255736Sdavidch        /* toggle TSO4 capabilities enabled flag */
4644255736Sdavidch        if (mask & IFCAP_TSO4) {
4645266979Smarcel            if_togglecapenable(ifp, IFCAP_TSO4);
4646255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4647266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4648255736Sdavidch        }
4649255736Sdavidch
4650255736Sdavidch        /* toggle TSO6 capabilities enabled flag */
4651255736Sdavidch        if (mask & IFCAP_TSO6) {
4652266979Smarcel	    if_togglecapenable(ifp, IFCAP_TSO6);
4653255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4654266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4655255736Sdavidch        }
4656255736Sdavidch
4657255736Sdavidch        /* toggle VLAN_HWTSO capabilities enabled flag */
4658255736Sdavidch        if (mask & IFCAP_VLAN_HWTSO) {
4659266979Smarcel
4660266979Smarcel	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4661255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4662266979Smarcel                  (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4663255736Sdavidch        }
4664255736Sdavidch
4665255736Sdavidch        /* toggle VLAN_HWCSUM capabilities enabled flag */
4666255736Sdavidch        if (mask & IFCAP_VLAN_HWCSUM) {
4667255736Sdavidch            /* XXX investigate this... */
4668255736Sdavidch            BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4669255736Sdavidch            error = EINVAL;
4670255736Sdavidch        }
4671255736Sdavidch
4672255736Sdavidch        /* toggle VLAN_MTU capabilities enable flag */
4673255736Sdavidch        if (mask & IFCAP_VLAN_MTU) {
4674255736Sdavidch            /* XXX investigate this... */
4675255736Sdavidch            BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4676255736Sdavidch            error = EINVAL;
4677255736Sdavidch        }
4678255736Sdavidch
4679255736Sdavidch        /* toggle VLAN_HWTAGGING capabilities enabled flag */
4680255736Sdavidch        if (mask & IFCAP_VLAN_HWTAGGING) {
4681255736Sdavidch            /* XXX investigate this... */
4682255736Sdavidch            BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4683255736Sdavidch            error = EINVAL;
4684255736Sdavidch        }
4685255736Sdavidch
4686255736Sdavidch        /* toggle VLAN_HWFILTER capabilities enabled flag */
4687255736Sdavidch        if (mask & IFCAP_VLAN_HWFILTER) {
4688255736Sdavidch            /* XXX investigate this... */
4689255736Sdavidch            BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4690255736Sdavidch            error = EINVAL;
4691255736Sdavidch        }
4692255736Sdavidch
4693255736Sdavidch        /* XXX not yet...
4694255736Sdavidch         * IFCAP_WOL_MAGIC
4695255736Sdavidch         */
4696255736Sdavidch
4697255736Sdavidch        break;
4698255736Sdavidch
4699255736Sdavidch    case SIOCSIFMEDIA:
4700255736Sdavidch    case SIOCGIFMEDIA:
4701255736Sdavidch        /* set/get interface media */
4702255736Sdavidch        BLOGD(sc, DBG_IOCTL,
4703255736Sdavidch              "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4704255736Sdavidch              (command & 0xff));
4705270876Sglebius        error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4706255736Sdavidch        break;
4707255736Sdavidch
4708255736Sdavidch    default:
4709255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4710255736Sdavidch              (command & 0xff));
4711270876Sglebius        error = ether_ioctl(ifp, command, data);
4712255736Sdavidch        break;
4713255736Sdavidch    }
4714255736Sdavidch
4715266979Smarcel    if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
4716255736Sdavidch        BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4717255736Sdavidch              "Re-initializing hardware from IOCTL change\n");
4718284335Sdavidcs	bxe_periodic_stop(sc);
4719284335Sdavidcs	BXE_CORE_LOCK(sc);
4720284335Sdavidcs	bxe_stop_locked(sc);
4721284335Sdavidcs	bxe_init_locked(sc);
4722284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4723255736Sdavidch    }
4724255736Sdavidch
4725255736Sdavidch    return (error);
4726255736Sdavidch}
4727255736Sdavidch
4728255736Sdavidchstatic __noinline void
4729255736Sdavidchbxe_dump_mbuf(struct bxe_softc *sc,
4730255736Sdavidch              struct mbuf      *m,
4731255736Sdavidch              uint8_t          contents)
4732255736Sdavidch{
4733255736Sdavidch    char * type;
4734260113Sedavis    int i = 0;
4735255736Sdavidch
4736255736Sdavidch    if (!(sc->debug & DBG_MBUF)) {
4737255736Sdavidch        return;
4738255736Sdavidch    }
4739255736Sdavidch
4740255736Sdavidch    if (m == NULL) {
4741255736Sdavidch        BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4742255736Sdavidch        return;
4743255736Sdavidch    }
4744255736Sdavidch
4745255736Sdavidch    while (m) {
4746297155Sdavidcs
4747297155Sdavidcs#if __FreeBSD_version >= 1000000
4748255736Sdavidch        BLOGD(sc, DBG_MBUF,
4749260113Sedavis              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4750260113Sedavis              i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
4751255736Sdavidch
4752255736Sdavidch        if (m->m_flags & M_PKTHDR) {
4753255736Sdavidch             BLOGD(sc, DBG_MBUF,
4754260113Sedavis                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4755260113Sedavis                   i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
4756260113Sedavis                   (int)m->m_pkthdr.csum_flags, CSUM_BITS);
4757255736Sdavidch        }
4758297155Sdavidcs#else
4759297155Sdavidcs        BLOGD(sc, DBG_MBUF,
4760297155Sdavidcs              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4761297155Sdavidcs              i, m, m->m_len, m->m_flags,
4762297155Sdavidcs              "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
4763255736Sdavidch
4764297155Sdavidcs        if (m->m_flags & M_PKTHDR) {
4765297155Sdavidcs             BLOGD(sc, DBG_MBUF,
4766297155Sdavidcs                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4767297155Sdavidcs                   i, m->m_pkthdr.len, m->m_flags,
4768297155Sdavidcs                   "\20\12M_BCAST\13M_MCAST\14M_FRAG"
4769297155Sdavidcs                   "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
4770297155Sdavidcs                   "\22M_PROMISC\23M_NOFREE",
4771297155Sdavidcs                   (int)m->m_pkthdr.csum_flags,
4772297155Sdavidcs                   "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
4773297155Sdavidcs                   "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
4774297155Sdavidcs                   "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
4775297155Sdavidcs                   "\14CSUM_PSEUDO_HDR");
4776297155Sdavidcs        }
4777297155Sdavidcs#endif /* #if __FreeBSD_version >= 1000000 */
4778297155Sdavidcs
4779255736Sdavidch        if (m->m_flags & M_EXT) {
4780255736Sdavidch            switch (m->m_ext.ext_type) {
4781255736Sdavidch            case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4782255736Sdavidch            case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4783260113Sedavis            case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4784255736Sdavidch            case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4785255736Sdavidch            case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4786255736Sdavidch            case EXT_PACKET:     type = "EXT_PACKET";     break;
4787255736Sdavidch            case EXT_MBUF:       type = "EXT_MBUF";       break;
4788255736Sdavidch            case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4789255736Sdavidch            case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4790255736Sdavidch            case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4791255736Sdavidch            case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4792255736Sdavidch            default:             type = "UNKNOWN";        break;
4793255736Sdavidch            }
4794255736Sdavidch
4795255736Sdavidch            BLOGD(sc, DBG_MBUF,
4796260113Sedavis                  "%02d: - m_ext: %p ext_size=%d type=%s\n",
4797260113Sedavis                  i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4798255736Sdavidch        }
4799255736Sdavidch
4800255736Sdavidch        if (contents) {
4801255736Sdavidch            bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4802255736Sdavidch        }
4803255736Sdavidch
4804255736Sdavidch        m = m->m_next;
4805260113Sedavis        i++;
4806255736Sdavidch    }
4807255736Sdavidch}
4808255736Sdavidch
4809255736Sdavidch/*
4810255736Sdavidch * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4811255736Sdavidch * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4812255736Sdavidch * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4813298955Spfg * The headers comes in a separate bd in FreeBSD so 13-3=10.
4814255736Sdavidch * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4815255736Sdavidch */
4816255736Sdavidchstatic int
4817255736Sdavidchbxe_chktso_window(struct bxe_softc  *sc,
4818255736Sdavidch                  int               nsegs,
4819255736Sdavidch                  bus_dma_segment_t *segs,
4820255736Sdavidch                  struct mbuf       *m)
4821255736Sdavidch{
4822255736Sdavidch    uint32_t num_wnds, wnd_size, wnd_sum;
4823255736Sdavidch    int32_t frag_idx, wnd_idx;
4824255736Sdavidch    unsigned short lso_mss;
4825255736Sdavidch    int defrag;
4826255736Sdavidch
4827255736Sdavidch    defrag = 0;
4828255736Sdavidch    wnd_sum = 0;
4829255736Sdavidch    wnd_size = 10;
4830255736Sdavidch    num_wnds = nsegs - wnd_size;
4831255736Sdavidch    lso_mss = htole16(m->m_pkthdr.tso_segsz);
4832255736Sdavidch
4833255736Sdavidch    /*
4834255736Sdavidch     * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4835255736Sdavidch     * first window sum of data while skipping the first assuming it is the
4836255736Sdavidch     * header in FreeBSD.
4837255736Sdavidch     */
4838255736Sdavidch    for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4839255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4840255736Sdavidch    }
4841255736Sdavidch
4842255736Sdavidch    /* check the first 10 bd window size */
4843255736Sdavidch    if (wnd_sum < lso_mss) {
4844255736Sdavidch        return (1);
4845255736Sdavidch    }
4846255736Sdavidch
4847255736Sdavidch    /* run through the windows */
4848255736Sdavidch    for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4849255736Sdavidch        /* subtract the first mbuf->m_len of the last wndw(-header) */
4850255736Sdavidch        wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4851255736Sdavidch        /* add the next mbuf len to the len of our new window */
4852255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4853255736Sdavidch        if (wnd_sum < lso_mss) {
4854255736Sdavidch            return (1);
4855255736Sdavidch        }
4856255736Sdavidch    }
4857255736Sdavidch
4858255736Sdavidch    return (0);
4859255736Sdavidch}
4860255736Sdavidch
4861255736Sdavidchstatic uint8_t
4862255736Sdavidchbxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4863255736Sdavidch                    struct mbuf         *m,
4864255736Sdavidch                    uint32_t            *parsing_data)
4865255736Sdavidch{
4866255736Sdavidch    struct ether_vlan_header *eh = NULL;
4867255736Sdavidch    struct ip *ip4 = NULL;
4868255736Sdavidch    struct ip6_hdr *ip6 = NULL;
4869255736Sdavidch    caddr_t ip = NULL;
4870255736Sdavidch    struct tcphdr *th = NULL;
4871255736Sdavidch    int e_hlen, ip_hlen, l4_off;
4872255736Sdavidch    uint16_t proto;
4873255736Sdavidch
4874255736Sdavidch    if (m->m_pkthdr.csum_flags == CSUM_IP) {
4875255736Sdavidch        /* no L4 checksum offload needed */
4876255736Sdavidch        return (0);
4877255736Sdavidch    }
4878255736Sdavidch
4879255736Sdavidch    /* get the Ethernet header */
4880255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
4881255736Sdavidch
4882255736Sdavidch    /* handle VLAN encapsulation if present */
4883255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4884255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4885255736Sdavidch        proto  = ntohs(eh->evl_proto);
4886255736Sdavidch    } else {
4887255736Sdavidch        e_hlen = ETHER_HDR_LEN;
4888255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
4889255736Sdavidch    }
4890255736Sdavidch
4891255736Sdavidch    switch (proto) {
4892255736Sdavidch    case ETHERTYPE_IP:
4893255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
4894255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
4895255736Sdavidch                  (struct ip *)m->m_next->m_data :
4896255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
4897255736Sdavidch        /* ip_hl is number of 32-bit words */
4898255736Sdavidch        ip_hlen = (ip4->ip_hl << 2);
4899255736Sdavidch        ip = (caddr_t)ip4;
4900255736Sdavidch        break;
4901255736Sdavidch    case ETHERTYPE_IPV6:
4902255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4903255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4904255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
4905255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
4906255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
4907255736Sdavidch        ip_hlen = sizeof(struct ip6_hdr);
4908255736Sdavidch        ip = (caddr_t)ip6;
4909255736Sdavidch        break;
4910255736Sdavidch    default:
4911255736Sdavidch        /* We can't offload in this case... */
4912255736Sdavidch        /* XXX error stat ??? */
4913255736Sdavidch        return (0);
4914255736Sdavidch    }
4915255736Sdavidch
4916255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4917255736Sdavidch    l4_off = (e_hlen + ip_hlen);
4918255736Sdavidch
4919255736Sdavidch    *parsing_data |=
4920255736Sdavidch        (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
4921255736Sdavidch         ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
4922255736Sdavidch
4923255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4924255736Sdavidch                                  CSUM_TSO |
4925255736Sdavidch                                  CSUM_TCP_IPV6)) {
4926255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4927255736Sdavidch        th = (struct tcphdr *)(ip + ip_hlen);
4928255736Sdavidch        /* th_off is number of 32-bit words */
4929255736Sdavidch        *parsing_data |= ((th->th_off <<
4930255736Sdavidch                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
4931255736Sdavidch                          ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
4932255736Sdavidch        return (l4_off + (th->th_off << 2)); /* entire header length */
4933255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4934255736Sdavidch                                         CSUM_UDP_IPV6)) {
4935255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4936255736Sdavidch        return (l4_off + sizeof(struct udphdr)); /* entire header length */
4937255736Sdavidch    } else {
4938255736Sdavidch        /* XXX error stat ??? */
4939255736Sdavidch        return (0);
4940255736Sdavidch    }
4941255736Sdavidch}
4942255736Sdavidch
4943255736Sdavidchstatic uint8_t
4944255736Sdavidchbxe_set_pbd_csum(struct bxe_fastpath        *fp,
4945255736Sdavidch                 struct mbuf                *m,
4946255736Sdavidch                 struct eth_tx_parse_bd_e1x *pbd)
4947255736Sdavidch{
4948255736Sdavidch    struct ether_vlan_header *eh = NULL;
4949255736Sdavidch    struct ip *ip4 = NULL;
4950255736Sdavidch    struct ip6_hdr *ip6 = NULL;
4951255736Sdavidch    caddr_t ip = NULL;
4952255736Sdavidch    struct tcphdr *th = NULL;
4953255736Sdavidch    struct udphdr *uh = NULL;
4954255736Sdavidch    int e_hlen, ip_hlen;
4955255736Sdavidch    uint16_t proto;
4956255736Sdavidch    uint8_t hlen;
4957255736Sdavidch    uint16_t tmp_csum;
4958255736Sdavidch    uint32_t *tmp_uh;
4959255736Sdavidch
4960255736Sdavidch    /* get the Ethernet header */
4961255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
4962255736Sdavidch
4963255736Sdavidch    /* handle VLAN encapsulation if present */
4964255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4965255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4966255736Sdavidch        proto  = ntohs(eh->evl_proto);
4967255736Sdavidch    } else {
4968255736Sdavidch        e_hlen = ETHER_HDR_LEN;
4969255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
4970255736Sdavidch    }
4971255736Sdavidch
4972255736Sdavidch    switch (proto) {
4973255736Sdavidch    case ETHERTYPE_IP:
4974255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
4975255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
4976255736Sdavidch                  (struct ip *)m->m_next->m_data :
4977255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
4978255736Sdavidch        /* ip_hl is number of 32-bit words */
4979255736Sdavidch        ip_hlen = (ip4->ip_hl << 1);
4980255736Sdavidch        ip = (caddr_t)ip4;
4981255736Sdavidch        break;
4982255736Sdavidch    case ETHERTYPE_IPV6:
4983255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4984255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4985255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
4986255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
4987255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
4988255736Sdavidch        ip_hlen = (sizeof(struct ip6_hdr) >> 1);
4989255736Sdavidch        ip = (caddr_t)ip6;
4990255736Sdavidch        break;
4991255736Sdavidch    default:
4992255736Sdavidch        /* We can't offload in this case... */
4993255736Sdavidch        /* XXX error stat ??? */
4994255736Sdavidch        return (0);
4995255736Sdavidch    }
4996255736Sdavidch
4997255736Sdavidch    hlen = (e_hlen >> 1);
4998255736Sdavidch
4999255736Sdavidch    /* note that rest of global_data is indirectly zeroed here */
5000255736Sdavidch    if (m->m_flags & M_VLANTAG) {
5001255736Sdavidch        pbd->global_data =
5002255736Sdavidch            htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
5003255736Sdavidch    } else {
5004255736Sdavidch        pbd->global_data = htole16(hlen);
5005255736Sdavidch    }
5006255736Sdavidch
5007255736Sdavidch    pbd->ip_hlen_w = ip_hlen;
5008255736Sdavidch
5009255736Sdavidch    hlen += pbd->ip_hlen_w;
5010255736Sdavidch
5011255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5012255736Sdavidch
5013255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5014255736Sdavidch                                  CSUM_TSO |
5015255736Sdavidch                                  CSUM_TCP_IPV6)) {
5016255736Sdavidch        th = (struct tcphdr *)(ip + (ip_hlen << 1));
5017255736Sdavidch        /* th_off is number of 32-bit words */
5018255736Sdavidch        hlen += (uint16_t)(th->th_off << 1);
5019255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5020255736Sdavidch                                         CSUM_UDP_IPV6)) {
5021255736Sdavidch        uh = (struct udphdr *)(ip + (ip_hlen << 1));
5022255736Sdavidch        hlen += (sizeof(struct udphdr) / 2);
5023255736Sdavidch    } else {
5024255736Sdavidch        /* valid case as only CSUM_IP was set */
5025255736Sdavidch        return (0);
5026255736Sdavidch    }
5027255736Sdavidch
5028255736Sdavidch    pbd->total_hlen_w = htole16(hlen);
5029255736Sdavidch
5030255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5031255736Sdavidch                                  CSUM_TSO |
5032255736Sdavidch                                  CSUM_TCP_IPV6)) {
5033255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5034255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5035255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5036255736Sdavidch                                         CSUM_UDP_IPV6)) {
5037255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5038255736Sdavidch
5039255736Sdavidch        /*
5040255736Sdavidch         * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5041255736Sdavidch         * checksums and does not know anything about the UDP header and where
5042255736Sdavidch         * the checksum field is located. It only knows about TCP. Therefore
5043255736Sdavidch         * we "lie" to the hardware for outgoing UDP packets w/ checksum
5044255736Sdavidch         * offload. Since the checksum field offset for TCP is 16 bytes and
5045255736Sdavidch         * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5046255736Sdavidch         * bytes less than the start of the UDP header. This allows the
5047255736Sdavidch         * hardware to write the checksum in the correct spot. But the
5048255736Sdavidch         * hardware will compute a checksum which includes the last 10 bytes
5049255736Sdavidch         * of the IP header. To correct this we tweak the stack computed
5050255736Sdavidch         * pseudo checksum by folding in the calculation of the inverse
5051255736Sdavidch         * checksum for those final 10 bytes of the IP header. This allows
5052255736Sdavidch         * the correct checksum to be computed by the hardware.
5053255736Sdavidch         */
5054255736Sdavidch
5055255736Sdavidch        /* set pointer 10 bytes before UDP header */
5056255736Sdavidch        tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5057255736Sdavidch
5058255736Sdavidch        /* calculate a pseudo header checksum over the first 10 bytes */
5059255736Sdavidch        tmp_csum = in_pseudo(*tmp_uh,
5060255736Sdavidch                             *(tmp_uh + 1),
5061255736Sdavidch                             *(uint16_t *)(tmp_uh + 2));
5062255736Sdavidch
5063255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5064255736Sdavidch    }
5065255736Sdavidch
5066255736Sdavidch    return (hlen * 2); /* entire header length, number of bytes */
5067255736Sdavidch}
5068255736Sdavidch
5069255736Sdavidchstatic void
5070255736Sdavidchbxe_set_pbd_lso_e2(struct mbuf *m,
5071255736Sdavidch                   uint32_t    *parsing_data)
5072255736Sdavidch{
5073255736Sdavidch    *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5074255736Sdavidch                       ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5075255736Sdavidch                      ETH_TX_PARSE_BD_E2_LSO_MSS);
5076255736Sdavidch
5077255736Sdavidch    /* XXX test for IPv6 with extension header... */
5078255736Sdavidch}
5079255736Sdavidch
5080255736Sdavidchstatic void
5081255736Sdavidchbxe_set_pbd_lso(struct mbuf                *m,
5082255736Sdavidch                struct eth_tx_parse_bd_e1x *pbd)
5083255736Sdavidch{
5084255736Sdavidch    struct ether_vlan_header *eh = NULL;
5085255736Sdavidch    struct ip *ip = NULL;
5086255736Sdavidch    struct tcphdr *th = NULL;
5087255736Sdavidch    int e_hlen;
5088255736Sdavidch
5089255736Sdavidch    /* get the Ethernet header */
5090255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5091255736Sdavidch
5092255736Sdavidch    /* handle VLAN encapsulation if present */
5093255736Sdavidch    e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5094255736Sdavidch                 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5095255736Sdavidch
5096255736Sdavidch    /* get the IP and TCP header, with LSO entire header in first mbuf */
5097255736Sdavidch    /* XXX assuming IPv4 */
5098255736Sdavidch    ip = (struct ip *)(m->m_data + e_hlen);
5099255736Sdavidch    th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5100255736Sdavidch
5101255736Sdavidch    pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5102255736Sdavidch    pbd->tcp_send_seq = ntohl(th->th_seq);
5103255736Sdavidch    pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5104255736Sdavidch
5105255736Sdavidch#if 1
5106255736Sdavidch        /* XXX IPv4 */
5107255736Sdavidch        pbd->ip_id = ntohs(ip->ip_id);
5108255736Sdavidch        pbd->tcp_pseudo_csum =
5109255736Sdavidch            ntohs(in_pseudo(ip->ip_src.s_addr,
5110255736Sdavidch                            ip->ip_dst.s_addr,
5111255736Sdavidch                            htons(IPPROTO_TCP)));
5112255736Sdavidch#else
5113255736Sdavidch        /* XXX IPv6 */
5114255736Sdavidch        pbd->tcp_pseudo_csum =
5115255736Sdavidch            ntohs(in_pseudo(&ip6->ip6_src,
5116255736Sdavidch                            &ip6->ip6_dst,
5117255736Sdavidch                            htons(IPPROTO_TCP)));
5118255736Sdavidch#endif
5119255736Sdavidch
5120255736Sdavidch    pbd->global_data |=
5121255736Sdavidch        htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5122255736Sdavidch}
5123255736Sdavidch
5124255736Sdavidch/*
5125255736Sdavidch * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5126255736Sdavidch * visible to the controller.
5127255736Sdavidch *
5128255736Sdavidch * If an mbuf is submitted to this routine and cannot be given to the
5129255736Sdavidch * controller (e.g. it has too many fragments) then the function may free
5130255736Sdavidch * the mbuf and return to the caller.
5131255736Sdavidch *
5132255736Sdavidch * Returns:
5133255736Sdavidch *   0 = Success, !0 = Failure
5134255736Sdavidch *   Note the side effect that an mbuf may be freed if it causes a problem.
5135255736Sdavidch */
5136255736Sdavidchstatic int
5137255736Sdavidchbxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5138255736Sdavidch{
5139255736Sdavidch    bus_dma_segment_t segs[32];
5140255736Sdavidch    struct mbuf *m0;
5141255736Sdavidch    struct bxe_sw_tx_bd *tx_buf;
5142255736Sdavidch    struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5143255736Sdavidch    struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5144255736Sdavidch    /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5145255736Sdavidch    struct eth_tx_bd *tx_data_bd;
5146255736Sdavidch    struct eth_tx_bd *tx_total_pkt_size_bd;
5147255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
5148255736Sdavidch    uint16_t bd_prod, pkt_prod, total_pkt_size;
5149255736Sdavidch    uint8_t mac_type;
5150255736Sdavidch    int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5151255736Sdavidch    struct bxe_softc *sc;
5152255736Sdavidch    uint16_t tx_bd_avail;
5153255736Sdavidch    struct ether_vlan_header *eh;
5154255736Sdavidch    uint32_t pbd_e2_parsing_data = 0;
5155255736Sdavidch    uint8_t hlen = 0;
5156255736Sdavidch    int tmp_bd;
5157255736Sdavidch    int i;
5158255736Sdavidch
5159255736Sdavidch    sc = fp->sc;
5160255736Sdavidch
5161297155Sdavidcs#if __FreeBSD_version >= 800000
5162255736Sdavidch    M_ASSERTPKTHDR(*m_head);
5163297155Sdavidcs#endif /* #if __FreeBSD_version >= 800000 */
5164255736Sdavidch
5165255736Sdavidch    m0 = *m_head;
5166255736Sdavidch    rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5167255736Sdavidch    tx_start_bd = NULL;
5168255736Sdavidch    tx_data_bd = NULL;
5169255736Sdavidch    tx_total_pkt_size_bd = NULL;
5170255736Sdavidch
5171255736Sdavidch    /* get the H/W pointer for packets and BDs */
5172255736Sdavidch    pkt_prod = fp->tx_pkt_prod;
5173255736Sdavidch    bd_prod = fp->tx_bd_prod;
5174255736Sdavidch
5175255736Sdavidch    mac_type = UNICAST_ADDRESS;
5176255736Sdavidch
5177255736Sdavidch    /* map the mbuf into the next open DMAable memory */
5178255736Sdavidch    tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5179255736Sdavidch    error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5180255736Sdavidch                                    tx_buf->m_map, m0,
5181255736Sdavidch                                    segs, &nsegs, BUS_DMA_NOWAIT);
5182255736Sdavidch
5183255736Sdavidch    /* mapping errors */
5184255736Sdavidch    if(__predict_false(error != 0)) {
5185255736Sdavidch        fp->eth_q_stats.tx_dma_mapping_failure++;
5186255736Sdavidch        if (error == ENOMEM) {
5187255736Sdavidch            /* resource issue, try again later */
5188255736Sdavidch            rc = ENOMEM;
5189255736Sdavidch        } else if (error == EFBIG) {
5190255736Sdavidch            /* possibly recoverable with defragmentation */
5191255736Sdavidch            fp->eth_q_stats.mbuf_defrag_attempts++;
5192260718Sglebius            m0 = m_defrag(*m_head, M_NOWAIT);
5193255736Sdavidch            if (m0 == NULL) {
5194255736Sdavidch                fp->eth_q_stats.mbuf_defrag_failures++;
5195255736Sdavidch                rc = ENOBUFS;
5196255736Sdavidch            } else {
5197255736Sdavidch                /* defrag successful, try mapping again */
5198255736Sdavidch                *m_head = m0;
5199255736Sdavidch                error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5200255736Sdavidch                                                tx_buf->m_map, m0,
5201255736Sdavidch                                                segs, &nsegs, BUS_DMA_NOWAIT);
5202255736Sdavidch                if (error) {
5203255736Sdavidch                    fp->eth_q_stats.tx_dma_mapping_failure++;
5204255736Sdavidch                    rc = error;
5205255736Sdavidch                }
5206255736Sdavidch            }
5207255736Sdavidch        } else {
5208255736Sdavidch            /* unknown, unrecoverable mapping error */
5209255736Sdavidch            BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5210255736Sdavidch            bxe_dump_mbuf(sc, m0, FALSE);
5211255736Sdavidch            rc = error;
5212255736Sdavidch        }
5213255736Sdavidch
5214255736Sdavidch        goto bxe_tx_encap_continue;
5215255736Sdavidch    }
5216255736Sdavidch
5217255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
5218255736Sdavidch
5219255736Sdavidch    /* make sure there is enough room in the send queue */
5220255736Sdavidch    if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5221255736Sdavidch        /* Recoverable, try again later. */
5222255736Sdavidch        fp->eth_q_stats.tx_hw_queue_full++;
5223255736Sdavidch        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5224255736Sdavidch        rc = ENOMEM;
5225255736Sdavidch        goto bxe_tx_encap_continue;
5226255736Sdavidch    }
5227255736Sdavidch
5228255736Sdavidch    /* capture the current H/W TX chain high watermark */
5229255736Sdavidch    if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5230255736Sdavidch                        (TX_BD_USABLE - tx_bd_avail))) {
5231255736Sdavidch        fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5232255736Sdavidch    }
5233255736Sdavidch
5234255736Sdavidch    /* make sure it fits in the packet window */
5235262999Sedavis    if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5236255736Sdavidch        /*
5237255736Sdavidch         * The mbuf may be to big for the controller to handle. If the frame
5238255736Sdavidch         * is a TSO frame we'll need to do an additional check.
5239255736Sdavidch         */
5240255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5241255736Sdavidch            if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5242255736Sdavidch                goto bxe_tx_encap_continue; /* OK to send */
5243255736Sdavidch            } else {
5244255736Sdavidch                fp->eth_q_stats.tx_window_violation_tso++;
5245255736Sdavidch            }
5246255736Sdavidch        } else {
5247255736Sdavidch            fp->eth_q_stats.tx_window_violation_std++;
5248255736Sdavidch        }
5249255736Sdavidch
5250262999Sedavis        /* lets try to defragment this mbuf and remap it */
5251260415Sedavis        fp->eth_q_stats.mbuf_defrag_attempts++;
5252262999Sedavis        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5253255736Sdavidch
5254260718Sglebius        m0 = m_defrag(*m_head, M_NOWAIT);
5255260415Sedavis        if (m0 == NULL) {
5256260415Sedavis            fp->eth_q_stats.mbuf_defrag_failures++;
5257260415Sedavis            /* Ugh, just drop the frame... :( */
5258260415Sedavis            rc = ENOBUFS;
5259260415Sedavis        } else {
5260260415Sedavis            /* defrag successful, try mapping again */
5261260415Sedavis            *m_head = m0;
5262260415Sedavis            error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5263260415Sedavis                                            tx_buf->m_map, m0,
5264260415Sedavis                                            segs, &nsegs, BUS_DMA_NOWAIT);
5265260415Sedavis            if (error) {
5266260415Sedavis                fp->eth_q_stats.tx_dma_mapping_failure++;
5267260415Sedavis                /* No sense in trying to defrag/copy chain, drop it. :( */
5268260415Sedavis                rc = error;
5269315881Sdavidcs            } else {
5270315881Sdavidcs               /* if the chain is still too long then drop it */
5271315881Sdavidcs                if(m0->m_pkthdr.csum_flags & CSUM_TSO) {
5272315881Sdavidcs                    /*
5273315881Sdavidcs                     * in case TSO is enabled nsegs should be checked against
5274315881Sdavidcs                     * BXE_TSO_MAX_SEGMENTS
5275315881Sdavidcs                     */
5276315881Sdavidcs                    if (__predict_false(nsegs > BXE_TSO_MAX_SEGMENTS)) {
5277315881Sdavidcs                        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5278315881Sdavidcs                        fp->eth_q_stats.nsegs_path1_errors++;
5279315881Sdavidcs                        rc = ENODEV;
5280315881Sdavidcs                    }
5281315881Sdavidcs                } else {
5282315881Sdavidcs                    if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5283315881Sdavidcs                        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5284315881Sdavidcs                        fp->eth_q_stats.nsegs_path2_errors++;
5285315881Sdavidcs                        rc = ENODEV;
5286315881Sdavidcs                    }
5287262999Sedavis                }
5288260415Sedavis            }
5289260415Sedavis        }
5290255736Sdavidch    }
5291255736Sdavidch
5292255736Sdavidchbxe_tx_encap_continue:
5293255736Sdavidch
5294255736Sdavidch    /* Check for errors */
5295255736Sdavidch    if (rc) {
5296255736Sdavidch        if (rc == ENOMEM) {
5297255736Sdavidch            /* recoverable try again later  */
5298255736Sdavidch        } else {
5299255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
5300255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tx--;
5301255736Sdavidch            m_freem(*m_head);
5302255736Sdavidch            *m_head = NULL;
5303255736Sdavidch        }
5304255736Sdavidch
5305255736Sdavidch        return (rc);
5306255736Sdavidch    }
5307255736Sdavidch
5308255736Sdavidch    /* set flag according to packet type (UNICAST_ADDRESS is default) */
5309255736Sdavidch    if (m0->m_flags & M_BCAST) {
5310255736Sdavidch        mac_type = BROADCAST_ADDRESS;
5311255736Sdavidch    } else if (m0->m_flags & M_MCAST) {
5312255736Sdavidch        mac_type = MULTICAST_ADDRESS;
5313255736Sdavidch    }
5314255736Sdavidch
5315255736Sdavidch    /* store the mbuf into the mbuf ring */
5316255736Sdavidch    tx_buf->m        = m0;
5317255736Sdavidch    tx_buf->first_bd = fp->tx_bd_prod;
5318255736Sdavidch    tx_buf->flags    = 0;
5319255736Sdavidch
5320255736Sdavidch    /* prepare the first transmit (start) BD for the mbuf */
5321255736Sdavidch    tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5322255736Sdavidch
5323255736Sdavidch    BLOGD(sc, DBG_TX,
5324255736Sdavidch          "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5325255736Sdavidch          pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5326255736Sdavidch
5327255736Sdavidch    tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5328255736Sdavidch    tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5329255736Sdavidch    tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5330255736Sdavidch    total_pkt_size += tx_start_bd->nbytes;
5331255736Sdavidch    tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5332255736Sdavidch
5333255736Sdavidch    tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5334255736Sdavidch
5335255736Sdavidch    /* all frames have at least Start BD + Parsing BD */
5336255736Sdavidch    nbds = nsegs + 1;
5337255736Sdavidch    tx_start_bd->nbd = htole16(nbds);
5338255736Sdavidch
5339255736Sdavidch    if (m0->m_flags & M_VLANTAG) {
5340255736Sdavidch        tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5341255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |=
5342255736Sdavidch            (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5343255736Sdavidch    } else {
5344255736Sdavidch        /* vf tx, start bd must hold the ethertype for fw to enforce it */
5345255736Sdavidch        if (IS_VF(sc)) {
5346255736Sdavidch            /* map ethernet header to find type and header length */
5347255736Sdavidch            eh = mtod(m0, struct ether_vlan_header *);
5348255736Sdavidch            tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5349255736Sdavidch        } else {
5350255736Sdavidch            /* used by FW for packet accounting */
5351255736Sdavidch            tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5352255736Sdavidch        }
5353255736Sdavidch    }
5354255736Sdavidch
5355255736Sdavidch    /*
5356255736Sdavidch     * add a parsing BD from the chain. The parsing BD is always added
5357255736Sdavidch     * though it is only used for TSO and chksum
5358255736Sdavidch     */
5359255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5360255736Sdavidch
5361255736Sdavidch    if (m0->m_pkthdr.csum_flags) {
5362255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5363255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5364255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5365255736Sdavidch        }
5366255736Sdavidch
5367255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5368255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5369255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5370255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5371255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5372255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP |
5373255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5374255736Sdavidch        } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5375255736Sdavidch                   (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5376255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5377255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5378255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5379255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP);
5380255736Sdavidch        }
5381255736Sdavidch    }
5382255736Sdavidch
5383255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
5384255736Sdavidch        pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5385255736Sdavidch        memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5386255736Sdavidch
5387255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5388255736Sdavidch            hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5389255736Sdavidch        }
5390255736Sdavidch
5391255736Sdavidch        SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5392255736Sdavidch                 mac_type);
5393255736Sdavidch    } else {
5394255736Sdavidch        uint16_t global_data = 0;
5395255736Sdavidch
5396255736Sdavidch        pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5397255736Sdavidch        memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5398255736Sdavidch
5399255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5400255736Sdavidch            hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5401255736Sdavidch        }
5402255736Sdavidch
5403255736Sdavidch        SET_FLAG(global_data,
5404255736Sdavidch                 ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5405255736Sdavidch        pbd_e1x->global_data |= htole16(global_data);
5406255736Sdavidch    }
5407255736Sdavidch
5408255736Sdavidch    /* setup the parsing BD with TSO specific info */
5409255736Sdavidch    if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5410255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_lso++;
5411255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5412255736Sdavidch
5413255736Sdavidch        if (__predict_false(tx_start_bd->nbytes > hlen)) {
5414255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5415255736Sdavidch
5416255736Sdavidch            /* split the first BD into header/data making the fw job easy */
5417255736Sdavidch            nbds++;
5418255736Sdavidch            tx_start_bd->nbd = htole16(nbds);
5419260113Sedavis            tx_start_bd->nbytes = htole16(hlen);
5420255736Sdavidch
5421255736Sdavidch            bd_prod = TX_BD_NEXT(bd_prod);
5422255736Sdavidch
5423255736Sdavidch            /* new transmit BD after the tx_parse_bd */
5424255736Sdavidch            tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5425255736Sdavidch            tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5426255736Sdavidch            tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5427255736Sdavidch            tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5428255736Sdavidch            if (tx_total_pkt_size_bd == NULL) {
5429255736Sdavidch                tx_total_pkt_size_bd = tx_data_bd;
5430255736Sdavidch            }
5431255736Sdavidch
5432255736Sdavidch            BLOGD(sc, DBG_TX,
5433255736Sdavidch                  "TSO split header size is %d (%x:%x) nbds %d\n",
5434255736Sdavidch                  le16toh(tx_start_bd->nbytes),
5435255736Sdavidch                  le32toh(tx_start_bd->addr_hi),
5436255736Sdavidch                  le32toh(tx_start_bd->addr_lo),
5437255736Sdavidch                  nbds);
5438255736Sdavidch        }
5439255736Sdavidch
5440255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
5441255736Sdavidch            bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5442255736Sdavidch        } else {
5443255736Sdavidch            bxe_set_pbd_lso(m0, pbd_e1x);
5444255736Sdavidch        }
5445255736Sdavidch    }
5446255736Sdavidch
5447255736Sdavidch    if (pbd_e2_parsing_data) {
5448255736Sdavidch        pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5449255736Sdavidch    }
5450255736Sdavidch
5451255736Sdavidch    /* prepare remaining BDs, start tx bd contains first seg/frag */
5452255736Sdavidch    for (i = 1; i < nsegs ; i++) {
5453255736Sdavidch        bd_prod = TX_BD_NEXT(bd_prod);
5454255736Sdavidch        tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5455255736Sdavidch        tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5456255736Sdavidch        tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5457255736Sdavidch        tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5458255736Sdavidch        if (tx_total_pkt_size_bd == NULL) {
5459255736Sdavidch            tx_total_pkt_size_bd = tx_data_bd;
5460255736Sdavidch        }
5461255736Sdavidch        total_pkt_size += tx_data_bd->nbytes;
5462255736Sdavidch    }
5463255736Sdavidch
5464255736Sdavidch    BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5465255736Sdavidch
5466255736Sdavidch    if (tx_total_pkt_size_bd != NULL) {
5467255736Sdavidch        tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5468255736Sdavidch    }
5469255736Sdavidch
5470255736Sdavidch    if (__predict_false(sc->debug & DBG_TX)) {
5471255736Sdavidch        tmp_bd = tx_buf->first_bd;
5472255736Sdavidch        for (i = 0; i < nbds; i++)
5473255736Sdavidch        {
5474255736Sdavidch            if (i == 0) {
5475255736Sdavidch                BLOGD(sc, DBG_TX,
5476255736Sdavidch                      "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5477255736Sdavidch                      "bd_flags=0x%x hdr_nbds=%d\n",
5478255736Sdavidch                      tx_start_bd,
5479255736Sdavidch                      tmp_bd,
5480255736Sdavidch                      le16toh(tx_start_bd->nbd),
5481255736Sdavidch                      le16toh(tx_start_bd->vlan_or_ethertype),
5482255736Sdavidch                      tx_start_bd->bd_flags.as_bitfield,
5483255736Sdavidch                      (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5484255736Sdavidch            } else if (i == 1) {
5485255736Sdavidch                if (pbd_e1x) {
5486255736Sdavidch                    BLOGD(sc, DBG_TX,
5487255736Sdavidch                          "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5488255736Sdavidch                          "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5489255736Sdavidch                          "tcp_seq=%u total_hlen_w=%u\n",
5490255736Sdavidch                          pbd_e1x,
5491255736Sdavidch                          tmp_bd,
5492255736Sdavidch                          pbd_e1x->global_data,
5493255736Sdavidch                          pbd_e1x->ip_hlen_w,
5494255736Sdavidch                          pbd_e1x->ip_id,
5495255736Sdavidch                          pbd_e1x->lso_mss,
5496255736Sdavidch                          pbd_e1x->tcp_flags,
5497255736Sdavidch                          pbd_e1x->tcp_pseudo_csum,
5498255736Sdavidch                          pbd_e1x->tcp_send_seq,
5499255736Sdavidch                          le16toh(pbd_e1x->total_hlen_w));
5500255736Sdavidch                } else { /* if (pbd_e2) */
5501255736Sdavidch                    BLOGD(sc, DBG_TX,
5502255736Sdavidch                          "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5503255736Sdavidch                          "src=%02x:%02x:%02x parsing_data=0x%x\n",
5504255736Sdavidch                          pbd_e2,
5505255736Sdavidch                          tmp_bd,
5506255736Sdavidch                          pbd_e2->data.mac_addr.dst_hi,
5507255736Sdavidch                          pbd_e2->data.mac_addr.dst_mid,
5508255736Sdavidch                          pbd_e2->data.mac_addr.dst_lo,
5509255736Sdavidch                          pbd_e2->data.mac_addr.src_hi,
5510255736Sdavidch                          pbd_e2->data.mac_addr.src_mid,
5511255736Sdavidch                          pbd_e2->data.mac_addr.src_lo,
5512255736Sdavidch                          pbd_e2->parsing_data);
5513255736Sdavidch                }
5514255736Sdavidch            }
5515255736Sdavidch
5516255736Sdavidch            if (i != 1) { /* skip parse db as it doesn't hold data */
5517255736Sdavidch                tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5518255736Sdavidch                BLOGD(sc, DBG_TX,
5519255736Sdavidch                      "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5520255736Sdavidch                      tx_data_bd,
5521255736Sdavidch                      tmp_bd,
5522255736Sdavidch                      le16toh(tx_data_bd->nbytes),
5523255736Sdavidch                      le32toh(tx_data_bd->addr_hi),
5524255736Sdavidch                      le32toh(tx_data_bd->addr_lo));
5525255736Sdavidch            }
5526255736Sdavidch
5527255736Sdavidch            tmp_bd = TX_BD_NEXT(tmp_bd);
5528255736Sdavidch        }
5529255736Sdavidch    }
5530255736Sdavidch
5531255736Sdavidch    BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5532255736Sdavidch
5533255736Sdavidch    /* update TX BD producer index value for next TX */
5534255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5535255736Sdavidch
5536255736Sdavidch    /*
5537255736Sdavidch     * If the chain of tx_bd's describing this frame is adjacent to or spans
5538255736Sdavidch     * an eth_tx_next_bd element then we need to increment the nbds value.
5539255736Sdavidch     */
5540255736Sdavidch    if (TX_BD_IDX(bd_prod) < nbds) {
5541255736Sdavidch        nbds++;
5542255736Sdavidch    }
5543255736Sdavidch
5544255736Sdavidch    /* don't allow reordering of writes for nbd and packets */
5545255736Sdavidch    mb();
5546255736Sdavidch
5547255736Sdavidch    fp->tx_db.data.prod += nbds;
5548255736Sdavidch
5549255736Sdavidch    /* producer points to the next free tx_bd at this point */
5550255736Sdavidch    fp->tx_pkt_prod++;
5551255736Sdavidch    fp->tx_bd_prod = bd_prod;
5552255736Sdavidch
5553255736Sdavidch    DOORBELL(sc, fp->index, fp->tx_db.raw);
5554255736Sdavidch
5555255736Sdavidch    fp->eth_q_stats.tx_pkts++;
5556255736Sdavidch
5557255736Sdavidch    /* Prevent speculative reads from getting ahead of the status block. */
5558255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5559255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5560255736Sdavidch
5561255736Sdavidch    /* Prevent speculative reads from getting ahead of the doorbell. */
5562255736Sdavidch    bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5563255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5564255736Sdavidch
5565255736Sdavidch    return (0);
5566255736Sdavidch}
5567255736Sdavidch
5568255736Sdavidchstatic void
5569266979Smarcelbxe_tx_start_locked(struct bxe_softc *sc,
5570266979Smarcel                    if_t ifp,
5571255736Sdavidch                    struct bxe_fastpath *fp)
5572255736Sdavidch{
5573255736Sdavidch    struct mbuf *m = NULL;
5574255736Sdavidch    int tx_count = 0;
5575255736Sdavidch    uint16_t tx_bd_avail;
5576255736Sdavidch
5577255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
5578255736Sdavidch
5579255736Sdavidch    /* keep adding entries while there are frames to send */
5580266979Smarcel    while (!if_sendq_empty(ifp)) {
5581255736Sdavidch
5582255736Sdavidch        /*
5583255736Sdavidch         * check for any frames to send
5584255736Sdavidch         * dequeue can still be NULL even if queue is not empty
5585255736Sdavidch         */
5586266979Smarcel        m = if_dequeue(ifp);
5587255736Sdavidch        if (__predict_false(m == NULL)) {
5588255736Sdavidch            break;
5589255736Sdavidch        }
5590255736Sdavidch
5591255736Sdavidch        /* the mbuf now belongs to us */
5592255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5593255736Sdavidch
5594255736Sdavidch        /*
5595255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5596255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5597255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5598255736Sdavidch         */
5599255736Sdavidch        if (__predict_false(bxe_tx_encap(fp, &m))) {
5600255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5601255736Sdavidch            if (m != NULL) {
5602255736Sdavidch                /* mark the TX queue as full and return the frame */
5603266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5604266979Smarcel		if_sendq_prepend(ifp, m);
5605255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5606255736Sdavidch                fp->eth_q_stats.tx_queue_xoff++;
5607255736Sdavidch            }
5608255736Sdavidch
5609255736Sdavidch            /* stop looking for more work */
5610255736Sdavidch            break;
5611255736Sdavidch        }
5612255736Sdavidch
5613255736Sdavidch        /* the frame was enqueued successfully */
5614255736Sdavidch        tx_count++;
5615255736Sdavidch
5616255736Sdavidch        /* send a copy of the frame to any BPF listeners. */
5617266979Smarcel        if_etherbpfmtap(ifp, m);
5618255736Sdavidch
5619255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
5620255736Sdavidch
5621255736Sdavidch        /* handle any completions if we're running low */
5622255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5623258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5624255736Sdavidch            bxe_txeof(sc, fp);
5625266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5626258187Sedavis                break;
5627258187Sedavis            }
5628255736Sdavidch        }
5629255736Sdavidch    }
5630255736Sdavidch
5631255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5632255736Sdavidch    if (tx_count > 0) {
5633255736Sdavidch        /* reset the TX watchdog timeout timer */
5634255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5635255736Sdavidch    }
5636255736Sdavidch}
5637255736Sdavidch
5638255736Sdavidch/* Legacy (non-RSS) dispatch routine */
5639255736Sdavidchstatic void
5640266979Smarcelbxe_tx_start(if_t ifp)
5641255736Sdavidch{
5642255736Sdavidch    struct bxe_softc *sc;
5643255736Sdavidch    struct bxe_fastpath *fp;
5644255736Sdavidch
5645266979Smarcel    sc = if_getsoftc(ifp);
5646255736Sdavidch
5647266979Smarcel    if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5648255736Sdavidch        BLOGW(sc, "Interface not running, ignoring transmit request\n");
5649255736Sdavidch        return;
5650255736Sdavidch    }
5651255736Sdavidch
5652255736Sdavidch    if (!sc->link_vars.link_up) {
5653255736Sdavidch        BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5654255736Sdavidch        return;
5655255736Sdavidch    }
5656255736Sdavidch
5657255736Sdavidch    fp = &sc->fp[0];
5658255736Sdavidch
5659297873Sdavidcs    if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5660297873Sdavidcs        fp->eth_q_stats.tx_queue_full_return++;
5661297873Sdavidcs        return;
5662297873Sdavidcs    }
5663297873Sdavidcs
5664255736Sdavidch    BXE_FP_TX_LOCK(fp);
5665255736Sdavidch    bxe_tx_start_locked(sc, ifp, fp);
5666255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
5667255736Sdavidch}
5668255736Sdavidch
5669307972Sdavidcs#if __FreeBSD_version >= 901504
5670255736Sdavidch
5671255736Sdavidchstatic int
5672255736Sdavidchbxe_tx_mq_start_locked(struct bxe_softc    *sc,
5673266979Smarcel                       if_t                ifp,
5674255736Sdavidch                       struct bxe_fastpath *fp,
5675255736Sdavidch                       struct mbuf         *m)
5676255736Sdavidch{
5677255736Sdavidch    struct buf_ring *tx_br = fp->tx_br;
5678255736Sdavidch    struct mbuf *next;
5679255736Sdavidch    int depth, rc, tx_count;
5680255736Sdavidch    uint16_t tx_bd_avail;
5681255736Sdavidch
5682255736Sdavidch    rc = tx_count = 0;
5683255736Sdavidch
5684285973Sdavidcs    BXE_FP_TX_LOCK_ASSERT(fp);
5685285973Sdavidcs
5686315881Sdavidcs    if (sc->state != BXE_STATE_OPEN)  {
5687315881Sdavidcs        fp->eth_q_stats.bxe_tx_mq_sc_state_failures++;
5688315881Sdavidcs        return ENETDOWN;
5689315881Sdavidcs    }
5690315881Sdavidcs
5691255736Sdavidch    if (!tx_br) {
5692255736Sdavidch        BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5693255736Sdavidch        return (EINVAL);
5694255736Sdavidch    }
5695255736Sdavidch
5696307972Sdavidcs    if (m != NULL) {
5697307972Sdavidcs        rc = drbr_enqueue(ifp, tx_br, m);
5698307972Sdavidcs        if (rc != 0) {
5699307972Sdavidcs            fp->eth_q_stats.tx_soft_errors++;
5700307972Sdavidcs            goto bxe_tx_mq_start_locked_exit;
5701307972Sdavidcs        }
5702307972Sdavidcs    }
5703307972Sdavidcs
5704307972Sdavidcs    if (!sc->link_vars.link_up || !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
5705307972Sdavidcs        fp->eth_q_stats.tx_request_link_down_failures++;
5706285973Sdavidcs        goto bxe_tx_mq_start_locked_exit;
5707285973Sdavidcs    }
5708285973Sdavidcs
5709255736Sdavidch    /* fetch the depth of the driver queue */
5710266979Smarcel    depth = drbr_inuse_drv(ifp, tx_br);
5711255736Sdavidch    if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5712255736Sdavidch        fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5713255736Sdavidch    }
5714255736Sdavidch
5715307972Sdavidcs    /* keep adding entries while there are frames to send */
5716307972Sdavidcs    while ((next = drbr_peek(ifp, tx_br)) != NULL) {
5717307972Sdavidcs        /* handle any completions if we're running low */
5718307972Sdavidcs        tx_bd_avail = bxe_tx_avail(sc, fp);
5719307972Sdavidcs        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5720307972Sdavidcs            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5721307972Sdavidcs            bxe_txeof(sc, fp);
5722307972Sdavidcs            tx_bd_avail = bxe_tx_avail(sc, fp);
5723307972Sdavidcs            if (tx_bd_avail < (BXE_TSO_MAX_SEGMENTS + 1)) {
5724307972Sdavidcs                fp->eth_q_stats.bd_avail_too_less_failures++;
5725307972Sdavidcs                m_freem(next);
5726307972Sdavidcs                drbr_advance(ifp, tx_br);
5727307972Sdavidcs                rc = ENOBUFS;
5728307972Sdavidcs                break;
5729307972Sdavidcs            }
5730255736Sdavidch        }
5731255736Sdavidch
5732255736Sdavidch        /* the mbuf now belongs to us */
5733255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5734255736Sdavidch
5735255736Sdavidch        /*
5736255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5737255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5738255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5739255736Sdavidch         */
5740255736Sdavidch        rc = bxe_tx_encap(fp, &next);
5741255736Sdavidch        if (__predict_false(rc != 0)) {
5742255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5743255736Sdavidch            if (next != NULL) {
5744255736Sdavidch                /* mark the TX queue as full and save the frame */
5745266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5746307972Sdavidcs                drbr_putback(ifp, tx_br, next);
5747255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5748255736Sdavidch                fp->eth_q_stats.tx_frames_deferred++;
5749307972Sdavidcs            } else
5750307972Sdavidcs                drbr_advance(ifp, tx_br);
5751255736Sdavidch
5752255736Sdavidch            /* stop looking for more work */
5753255736Sdavidch            break;
5754255736Sdavidch        }
5755255736Sdavidch
5756255736Sdavidch        /* the transmit frame was enqueued successfully */
5757255736Sdavidch        tx_count++;
5758255736Sdavidch
5759255736Sdavidch        /* send a copy of the frame to any BPF listeners */
5760266979Smarcel	if_etherbpfmtap(ifp, next);
5761255736Sdavidch
5762307972Sdavidcs        drbr_advance(ifp, tx_br);
5763255736Sdavidch    }
5764255736Sdavidch
5765255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5766255736Sdavidch    if (tx_count > 0) {
5767255736Sdavidch        /* reset the TX watchdog timeout timer */
5768255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5769255736Sdavidch    }
5770255736Sdavidch
5771255736Sdavidchbxe_tx_mq_start_locked_exit:
5772307972Sdavidcs    /* If we didn't drain the drbr, enqueue a task in the future to do it. */
5773307972Sdavidcs    if (!drbr_empty(ifp, tx_br)) {
5774307972Sdavidcs        fp->eth_q_stats.tx_mq_not_empty++;
5775307972Sdavidcs        taskqueue_enqueue_timeout(fp->tq, &fp->tx_timeout_task, 1);
5776307972Sdavidcs    }
5777255736Sdavidch
5778255736Sdavidch    return (rc);
5779255736Sdavidch}
5780255736Sdavidch
5781307972Sdavidcsstatic void
5782307972Sdavidcsbxe_tx_mq_start_deferred(void *arg,
5783307972Sdavidcs                         int pending)
5784307972Sdavidcs{
5785307972Sdavidcs    struct bxe_fastpath *fp = (struct bxe_fastpath *)arg;
5786307972Sdavidcs    struct bxe_softc *sc = fp->sc;
5787307972Sdavidcs    if_t ifp = sc->ifp;
5788307972Sdavidcs
5789307972Sdavidcs    BXE_FP_TX_LOCK(fp);
5790307972Sdavidcs    bxe_tx_mq_start_locked(sc, ifp, fp, NULL);
5791307972Sdavidcs    BXE_FP_TX_UNLOCK(fp);
5792307972Sdavidcs}
5793307972Sdavidcs
5794255736Sdavidch/* Multiqueue (TSS) dispatch routine. */
5795255736Sdavidchstatic int
5796255736Sdavidchbxe_tx_mq_start(struct ifnet *ifp,
5797255736Sdavidch                struct mbuf  *m)
5798255736Sdavidch{
5799266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5800255736Sdavidch    struct bxe_fastpath *fp;
5801255736Sdavidch    int fp_index, rc;
5802255736Sdavidch
5803255736Sdavidch    fp_index = 0; /* default is the first queue */
5804255736Sdavidch
5805275358Shselasky    /* check if flowid is set */
5806297155Sdavidcs
5807297155Sdavidcs    if (BXE_VALID_FLOWID(m))
5808255736Sdavidch        fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5809255736Sdavidch
5810255736Sdavidch    fp = &sc->fp[fp_index];
5811255736Sdavidch
5812315881Sdavidcs    if (sc->state != BXE_STATE_OPEN)  {
5813315881Sdavidcs        fp->eth_q_stats.bxe_tx_mq_sc_state_failures++;
5814315881Sdavidcs        return ENETDOWN;
5815315881Sdavidcs    }
5816315881Sdavidcs
5817285973Sdavidcs    if (BXE_FP_TX_TRYLOCK(fp)) {
5818285973Sdavidcs        rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5819285973Sdavidcs        BXE_FP_TX_UNLOCK(fp);
5820307972Sdavidcs    } else {
5821297155Sdavidcs        rc = drbr_enqueue(ifp, fp->tx_br, m);
5822307972Sdavidcs        taskqueue_enqueue(fp->tq, &fp->tx_task);
5823307972Sdavidcs    }
5824255736Sdavidch
5825255736Sdavidch    return (rc);
5826255736Sdavidch}
5827255736Sdavidch
5828255736Sdavidchstatic void
5829255736Sdavidchbxe_mq_flush(struct ifnet *ifp)
5830255736Sdavidch{
5831266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5832255736Sdavidch    struct bxe_fastpath *fp;
5833255736Sdavidch    struct mbuf *m;
5834255736Sdavidch    int i;
5835255736Sdavidch
5836255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5837255736Sdavidch        fp = &sc->fp[i];
5838255736Sdavidch
5839315881Sdavidcs        if (fp->state != BXE_FP_STATE_IRQ) {
5840255736Sdavidch            BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5841255736Sdavidch                  fp->index, fp->state);
5842255736Sdavidch            continue;
5843255736Sdavidch        }
5844255736Sdavidch
5845255736Sdavidch        if (fp->tx_br != NULL) {
5846255736Sdavidch            BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5847255736Sdavidch            BXE_FP_TX_LOCK(fp);
5848255736Sdavidch            while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5849255736Sdavidch                m_freem(m);
5850255736Sdavidch            }
5851255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
5852255736Sdavidch        }
5853255736Sdavidch    }
5854255736Sdavidch
5855270876Sglebius    if_qflush(ifp);
5856255736Sdavidch}
5857255736Sdavidch
5858307972Sdavidcs#endif /* FreeBSD_version >= 901504 */
5859255736Sdavidch
5860255736Sdavidchstatic uint16_t
5861255736Sdavidchbxe_cid_ilt_lines(struct bxe_softc *sc)
5862255736Sdavidch{
5863255736Sdavidch    if (IS_SRIOV(sc)) {
5864255736Sdavidch        return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5865255736Sdavidch    }
5866255736Sdavidch    return (L2_ILT_LINES(sc));
5867255736Sdavidch}
5868255736Sdavidch
5869255736Sdavidchstatic void
5870255736Sdavidchbxe_ilt_set_info(struct bxe_softc *sc)
5871255736Sdavidch{
5872255736Sdavidch    struct ilt_client_info *ilt_client;
5873255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
5874255736Sdavidch    uint16_t line = 0;
5875255736Sdavidch
5876255736Sdavidch    ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5877255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5878255736Sdavidch
5879255736Sdavidch    /* CDU */
5880255736Sdavidch    ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5881255736Sdavidch    ilt_client->client_num = ILT_CLIENT_CDU;
5882255736Sdavidch    ilt_client->page_size = CDU_ILT_PAGE_SZ;
5883255736Sdavidch    ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5884255736Sdavidch    ilt_client->start = line;
5885255736Sdavidch    line += bxe_cid_ilt_lines(sc);
5886255736Sdavidch
5887255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5888255736Sdavidch        line += CNIC_ILT_LINES;
5889255736Sdavidch    }
5890255736Sdavidch
5891255736Sdavidch    ilt_client->end = (line - 1);
5892255736Sdavidch
5893255736Sdavidch    BLOGD(sc, DBG_LOAD,
5894255736Sdavidch          "ilt client[CDU]: start %d, end %d, "
5895255736Sdavidch          "psz 0x%x, flags 0x%x, hw psz %d\n",
5896255736Sdavidch          ilt_client->start, ilt_client->end,
5897255736Sdavidch          ilt_client->page_size,
5898255736Sdavidch          ilt_client->flags,
5899255736Sdavidch          ilog2(ilt_client->page_size >> 12));
5900255736Sdavidch
5901255736Sdavidch    /* QM */
5902255736Sdavidch    if (QM_INIT(sc->qm_cid_count)) {
5903255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_QM];
5904255736Sdavidch        ilt_client->client_num = ILT_CLIENT_QM;
5905255736Sdavidch        ilt_client->page_size = QM_ILT_PAGE_SZ;
5906255736Sdavidch        ilt_client->flags = 0;
5907255736Sdavidch        ilt_client->start = line;
5908255736Sdavidch
5909255736Sdavidch        /* 4 bytes for each cid */
5910255736Sdavidch        line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5911255736Sdavidch                             QM_ILT_PAGE_SZ);
5912255736Sdavidch
5913255736Sdavidch        ilt_client->end = (line - 1);
5914255736Sdavidch
5915255736Sdavidch        BLOGD(sc, DBG_LOAD,
5916255736Sdavidch              "ilt client[QM]: start %d, end %d, "
5917255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5918255736Sdavidch              ilt_client->start, ilt_client->end,
5919255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5920255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5921255736Sdavidch    }
5922255736Sdavidch
5923255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5924255736Sdavidch        /* SRC */
5925255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5926255736Sdavidch        ilt_client->client_num = ILT_CLIENT_SRC;
5927255736Sdavidch        ilt_client->page_size = SRC_ILT_PAGE_SZ;
5928255736Sdavidch        ilt_client->flags = 0;
5929255736Sdavidch        ilt_client->start = line;
5930255736Sdavidch        line += SRC_ILT_LINES;
5931255736Sdavidch        ilt_client->end = (line - 1);
5932255736Sdavidch
5933255736Sdavidch        BLOGD(sc, DBG_LOAD,
5934255736Sdavidch              "ilt client[SRC]: start %d, end %d, "
5935255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5936255736Sdavidch              ilt_client->start, ilt_client->end,
5937255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5938255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5939255736Sdavidch
5940255736Sdavidch        /* TM */
5941255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_TM];
5942255736Sdavidch        ilt_client->client_num = ILT_CLIENT_TM;
5943255736Sdavidch        ilt_client->page_size = TM_ILT_PAGE_SZ;
5944255736Sdavidch        ilt_client->flags = 0;
5945255736Sdavidch        ilt_client->start = line;
5946255736Sdavidch        line += TM_ILT_LINES;
5947255736Sdavidch        ilt_client->end = (line - 1);
5948255736Sdavidch
5949255736Sdavidch        BLOGD(sc, DBG_LOAD,
5950255736Sdavidch              "ilt client[TM]: start %d, end %d, "
5951255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5952255736Sdavidch              ilt_client->start, ilt_client->end,
5953255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5954255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5955255736Sdavidch    }
5956255736Sdavidch
5957255736Sdavidch    KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
5958255736Sdavidch}
5959255736Sdavidch
5960255736Sdavidchstatic void
5961255736Sdavidchbxe_set_fp_rx_buf_size(struct bxe_softc *sc)
5962255736Sdavidch{
5963255736Sdavidch    int i;
5964283269Sdavidcs    uint32_t rx_buf_size;
5965255736Sdavidch
5966283269Sdavidcs    rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
5967255736Sdavidch
5968255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5969283269Sdavidcs        if(rx_buf_size <= MCLBYTES){
5970283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5971255736Sdavidch            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5972283269Sdavidcs        }else if (rx_buf_size <= MJUMPAGESIZE){
5973283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5974283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5975283269Sdavidcs        }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
5976283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5977283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5978283269Sdavidcs        }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
5979283269Sdavidcs            sc->fp[i].rx_buf_size = MJUMPAGESIZE;
5980283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5981283269Sdavidcs        }else {
5982283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5983283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5984255736Sdavidch        }
5985255736Sdavidch    }
5986255736Sdavidch}
5987255736Sdavidch
5988255736Sdavidchstatic int
5989255736Sdavidchbxe_alloc_ilt_mem(struct bxe_softc *sc)
5990255736Sdavidch{
5991255736Sdavidch    int rc = 0;
5992255736Sdavidch
5993255736Sdavidch    if ((sc->ilt =
5994255736Sdavidch         (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
5995255736Sdavidch                                    M_BXE_ILT,
5996255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
5997255736Sdavidch        rc = 1;
5998255736Sdavidch    }
5999255736Sdavidch
6000255736Sdavidch    return (rc);
6001255736Sdavidch}
6002255736Sdavidch
6003255736Sdavidchstatic int
6004255736Sdavidchbxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
6005255736Sdavidch{
6006255736Sdavidch    int rc = 0;
6007255736Sdavidch
6008255736Sdavidch    if ((sc->ilt->lines =
6009255736Sdavidch         (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
6010255736Sdavidch                                    M_BXE_ILT,
6011255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
6012255736Sdavidch        rc = 1;
6013255736Sdavidch    }
6014255736Sdavidch
6015255736Sdavidch    return (rc);
6016255736Sdavidch}
6017255736Sdavidch
6018255736Sdavidchstatic void
6019255736Sdavidchbxe_free_ilt_mem(struct bxe_softc *sc)
6020255736Sdavidch{
6021255736Sdavidch    if (sc->ilt != NULL) {
6022255736Sdavidch        free(sc->ilt, M_BXE_ILT);
6023255736Sdavidch        sc->ilt = NULL;
6024255736Sdavidch    }
6025255736Sdavidch}
6026255736Sdavidch
6027255736Sdavidchstatic void
6028255736Sdavidchbxe_free_ilt_lines_mem(struct bxe_softc *sc)
6029255736Sdavidch{
6030255736Sdavidch    if (sc->ilt->lines != NULL) {
6031255736Sdavidch        free(sc->ilt->lines, M_BXE_ILT);
6032255736Sdavidch        sc->ilt->lines = NULL;
6033255736Sdavidch    }
6034255736Sdavidch}
6035255736Sdavidch
6036255736Sdavidchstatic void
6037255736Sdavidchbxe_free_mem(struct bxe_softc *sc)
6038255736Sdavidch{
6039255736Sdavidch    int i;
6040255736Sdavidch
6041255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
6042255736Sdavidch        bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6043255736Sdavidch        sc->context[i].vcxt = NULL;
6044255736Sdavidch        sc->context[i].size = 0;
6045255736Sdavidch    }
6046255736Sdavidch
6047255736Sdavidch    ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6048255736Sdavidch
6049255736Sdavidch    bxe_free_ilt_lines_mem(sc);
6050255736Sdavidch
6051255736Sdavidch}
6052255736Sdavidch
6053255736Sdavidchstatic int
6054255736Sdavidchbxe_alloc_mem(struct bxe_softc *sc)
6055255736Sdavidch{
6056315881Sdavidcs
6057255736Sdavidch    int context_size;
6058255736Sdavidch    int allocated;
6059255736Sdavidch    int i;
6060255736Sdavidch
6061255736Sdavidch    /*
6062255736Sdavidch     * Allocate memory for CDU context:
6063255736Sdavidch     * This memory is allocated separately and not in the generic ILT
6064255736Sdavidch     * functions because CDU differs in few aspects:
6065255736Sdavidch     * 1. There can be multiple entities allocating memory for context -
6066255736Sdavidch     * regular L2, CNIC, and SRIOV drivers. Each separately controls
6067255736Sdavidch     * its own ILT lines.
6068255736Sdavidch     * 2. Since CDU page-size is not a single 4KB page (which is the case
6069255736Sdavidch     * for the other ILT clients), to be efficient we want to support
6070255736Sdavidch     * allocation of sub-page-size in the last entry.
6071255736Sdavidch     * 3. Context pointers are used by the driver to pass to FW / update
6072255736Sdavidch     * the context (for the other ILT clients the pointers are used just to
6073255736Sdavidch     * free the memory during unload).
6074255736Sdavidch     */
6075255736Sdavidch    context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6076255736Sdavidch    for (i = 0, allocated = 0; allocated < context_size; i++) {
6077255736Sdavidch        sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6078255736Sdavidch                                  (context_size - allocated));
6079255736Sdavidch
6080255736Sdavidch        if (bxe_dma_alloc(sc, sc->context[i].size,
6081255736Sdavidch                          &sc->context[i].vcxt_dma,
6082255736Sdavidch                          "cdu context") != 0) {
6083255736Sdavidch            bxe_free_mem(sc);
6084255736Sdavidch            return (-1);
6085255736Sdavidch        }
6086255736Sdavidch
6087255736Sdavidch        sc->context[i].vcxt =
6088255736Sdavidch            (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6089255736Sdavidch
6090255736Sdavidch        allocated += sc->context[i].size;
6091255736Sdavidch    }
6092255736Sdavidch
6093255736Sdavidch    bxe_alloc_ilt_lines_mem(sc);
6094255736Sdavidch
6095255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6096255736Sdavidch          sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6097255736Sdavidch    {
6098255736Sdavidch        for (i = 0; i < 4; i++) {
6099255736Sdavidch            BLOGD(sc, DBG_LOAD,
6100255736Sdavidch                  "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6101255736Sdavidch                  i,
6102255736Sdavidch                  sc->ilt->clients[i].page_size,
6103255736Sdavidch                  sc->ilt->clients[i].start,
6104255736Sdavidch                  sc->ilt->clients[i].end,
6105255736Sdavidch                  sc->ilt->clients[i].client_num,
6106255736Sdavidch                  sc->ilt->clients[i].flags);
6107255736Sdavidch        }
6108255736Sdavidch    }
6109255736Sdavidch    if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6110255736Sdavidch        BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6111255736Sdavidch        bxe_free_mem(sc);
6112255736Sdavidch        return (-1);
6113255736Sdavidch    }
6114255736Sdavidch
6115255736Sdavidch    return (0);
6116255736Sdavidch}
6117255736Sdavidch
6118255736Sdavidchstatic void
6119255736Sdavidchbxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6120255736Sdavidch{
6121255736Sdavidch    struct bxe_softc *sc;
6122255736Sdavidch    int i;
6123255736Sdavidch
6124255736Sdavidch    sc = fp->sc;
6125255736Sdavidch
6126255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6127255736Sdavidch        return;
6128255736Sdavidch    }
6129255736Sdavidch
6130255736Sdavidch    /* free all mbufs and unload all maps */
6131255736Sdavidch    for (i = 0; i < RX_BD_TOTAL; i++) {
6132255736Sdavidch        if (fp->rx_mbuf_chain[i].m_map != NULL) {
6133255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6134255736Sdavidch                            fp->rx_mbuf_chain[i].m_map,
6135255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6136255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6137255736Sdavidch                              fp->rx_mbuf_chain[i].m_map);
6138255736Sdavidch        }
6139255736Sdavidch
6140255736Sdavidch        if (fp->rx_mbuf_chain[i].m != NULL) {
6141255736Sdavidch            m_freem(fp->rx_mbuf_chain[i].m);
6142255736Sdavidch            fp->rx_mbuf_chain[i].m = NULL;
6143255736Sdavidch            fp->eth_q_stats.mbuf_alloc_rx--;
6144255736Sdavidch        }
6145255736Sdavidch    }
6146255736Sdavidch}
6147255736Sdavidch
6148255736Sdavidchstatic void
6149255736Sdavidchbxe_free_tpa_pool(struct bxe_fastpath *fp)
6150255736Sdavidch{
6151255736Sdavidch    struct bxe_softc *sc;
6152255736Sdavidch    int i, max_agg_queues;
6153255736Sdavidch
6154255736Sdavidch    sc = fp->sc;
6155255736Sdavidch
6156255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6157255736Sdavidch        return;
6158255736Sdavidch    }
6159255736Sdavidch
6160255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
6161255736Sdavidch
6162255736Sdavidch    /* release all mbufs and unload all DMA maps in the TPA pool */
6163255736Sdavidch    for (i = 0; i < max_agg_queues; i++) {
6164255736Sdavidch        if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6165255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6166255736Sdavidch                            fp->rx_tpa_info[i].bd.m_map,
6167255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6168255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6169255736Sdavidch                              fp->rx_tpa_info[i].bd.m_map);
6170255736Sdavidch        }
6171255736Sdavidch
6172255736Sdavidch        if (fp->rx_tpa_info[i].bd.m != NULL) {
6173255736Sdavidch            m_freem(fp->rx_tpa_info[i].bd.m);
6174255736Sdavidch            fp->rx_tpa_info[i].bd.m = NULL;
6175255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tpa--;
6176255736Sdavidch        }
6177255736Sdavidch    }
6178255736Sdavidch}
6179255736Sdavidch
6180255736Sdavidchstatic void
6181255736Sdavidchbxe_free_sge_chain(struct bxe_fastpath *fp)
6182255736Sdavidch{
6183255736Sdavidch    struct bxe_softc *sc;
6184255736Sdavidch    int i;
6185255736Sdavidch
6186255736Sdavidch    sc = fp->sc;
6187255736Sdavidch
6188255736Sdavidch    if (fp->rx_sge_mbuf_tag == NULL) {
6189255736Sdavidch        return;
6190255736Sdavidch    }
6191255736Sdavidch
6192255736Sdavidch    /* rree all mbufs and unload all maps */
6193255736Sdavidch    for (i = 0; i < RX_SGE_TOTAL; i++) {
6194255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6195255736Sdavidch            bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6196255736Sdavidch                            fp->rx_sge_mbuf_chain[i].m_map,
6197255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6198255736Sdavidch            bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6199255736Sdavidch                              fp->rx_sge_mbuf_chain[i].m_map);
6200255736Sdavidch        }
6201255736Sdavidch
6202255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6203255736Sdavidch            m_freem(fp->rx_sge_mbuf_chain[i].m);
6204255736Sdavidch            fp->rx_sge_mbuf_chain[i].m = NULL;
6205255736Sdavidch            fp->eth_q_stats.mbuf_alloc_sge--;
6206255736Sdavidch        }
6207255736Sdavidch    }
6208255736Sdavidch}
6209255736Sdavidch
6210255736Sdavidchstatic void
6211255736Sdavidchbxe_free_fp_buffers(struct bxe_softc *sc)
6212255736Sdavidch{
6213255736Sdavidch    struct bxe_fastpath *fp;
6214255736Sdavidch    int i;
6215255736Sdavidch
6216255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6217255736Sdavidch        fp = &sc->fp[i];
6218255736Sdavidch
6219307972Sdavidcs#if __FreeBSD_version >= 901504
6220255736Sdavidch        if (fp->tx_br != NULL) {
6221255736Sdavidch            /* just in case bxe_mq_flush() wasn't called */
6222284739Sdavidcs            if (mtx_initialized(&fp->tx_mtx)) {
6223284739Sdavidcs                struct mbuf *m;
6224284739Sdavidcs
6225284739Sdavidcs                BXE_FP_TX_LOCK(fp);
6226284739Sdavidcs                while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6227284739Sdavidcs                    m_freem(m);
6228284739Sdavidcs                BXE_FP_TX_UNLOCK(fp);
6229255736Sdavidch            }
6230255736Sdavidch        }
6231255736Sdavidch#endif
6232255736Sdavidch
6233255736Sdavidch        /* free all RX buffers */
6234255736Sdavidch        bxe_free_rx_bd_chain(fp);
6235255736Sdavidch        bxe_free_tpa_pool(fp);
6236255736Sdavidch        bxe_free_sge_chain(fp);
6237255736Sdavidch
6238255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6239255736Sdavidch            BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6240255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_rx);
6241255736Sdavidch        }
6242255736Sdavidch
6243255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6244255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6245255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_sge);
6246255736Sdavidch        }
6247255736Sdavidch
6248255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6249255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6250255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tpa);
6251255736Sdavidch        }
6252255736Sdavidch
6253255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6254255736Sdavidch            BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6255255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tx);
6256255736Sdavidch        }
6257255736Sdavidch
6258255736Sdavidch        /* XXX verify all mbufs were reclaimed */
6259255736Sdavidch    }
6260255736Sdavidch}
6261255736Sdavidch
6262255736Sdavidchstatic int
6263255736Sdavidchbxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6264255736Sdavidch                     uint16_t            prev_index,
6265255736Sdavidch                     uint16_t            index)
6266255736Sdavidch{
6267255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
6268255736Sdavidch    struct eth_rx_bd *rx_bd;
6269255736Sdavidch    bus_dma_segment_t segs[1];
6270255736Sdavidch    bus_dmamap_t map;
6271255736Sdavidch    struct mbuf *m;
6272255736Sdavidch    int nsegs, rc;
6273255736Sdavidch
6274255736Sdavidch    rc = 0;
6275255736Sdavidch
6276255736Sdavidch    /* allocate the new RX BD mbuf */
6277260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6278255736Sdavidch    if (__predict_false(m == NULL)) {
6279255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6280255736Sdavidch        return (ENOBUFS);
6281255736Sdavidch    }
6282255736Sdavidch
6283255736Sdavidch    fp->eth_q_stats.mbuf_alloc_rx++;
6284255736Sdavidch
6285255736Sdavidch    /* initialize the mbuf buffer length */
6286255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6287255736Sdavidch
6288255736Sdavidch    /* map the mbuf into non-paged pool */
6289255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6290255736Sdavidch                                 fp->rx_mbuf_spare_map,
6291255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6292255736Sdavidch    if (__predict_false(rc != 0)) {
6293255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6294255736Sdavidch        m_freem(m);
6295255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
6296255736Sdavidch        return (rc);
6297255736Sdavidch    }
6298255736Sdavidch
6299255736Sdavidch    /* all mbufs must map to a single segment */
6300255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6301255736Sdavidch
6302255736Sdavidch    /* release any existing RX BD mbuf mappings */
6303255736Sdavidch
6304255736Sdavidch    if (prev_index != index) {
6305255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[prev_index];
6306255736Sdavidch
6307255736Sdavidch        if (rx_buf->m_map != NULL) {
6308255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6309255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6310255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6311255736Sdavidch        }
6312255736Sdavidch
6313255736Sdavidch        /*
6314255736Sdavidch         * We only get here from bxe_rxeof() when the maximum number
6315255736Sdavidch         * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6316255736Sdavidch         * holds the mbuf in the prev_index so it's OK to NULL it out
6317255736Sdavidch         * here without concern of a memory leak.
6318255736Sdavidch         */
6319255736Sdavidch        fp->rx_mbuf_chain[prev_index].m = NULL;
6320255736Sdavidch    }
6321255736Sdavidch
6322255736Sdavidch    rx_buf = &fp->rx_mbuf_chain[index];
6323255736Sdavidch
6324255736Sdavidch    if (rx_buf->m_map != NULL) {
6325255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6326255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6327255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6328255736Sdavidch    }
6329255736Sdavidch
6330255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6331255736Sdavidch    map = (prev_index != index) ?
6332255736Sdavidch              fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6333255736Sdavidch    rx_buf->m_map = fp->rx_mbuf_spare_map;
6334255736Sdavidch    fp->rx_mbuf_spare_map = map;
6335255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6336255736Sdavidch                    BUS_DMASYNC_PREREAD);
6337255736Sdavidch    rx_buf->m = m;
6338255736Sdavidch
6339255736Sdavidch    rx_bd = &fp->rx_chain[index];
6340255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6341255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6342255736Sdavidch
6343255736Sdavidch    return (rc);
6344255736Sdavidch}
6345255736Sdavidch
6346255736Sdavidchstatic int
6347255736Sdavidchbxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6348255736Sdavidch                      int                 queue)
6349255736Sdavidch{
6350255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6351255736Sdavidch    bus_dma_segment_t segs[1];
6352255736Sdavidch    bus_dmamap_t map;
6353255736Sdavidch    struct mbuf *m;
6354255736Sdavidch    int nsegs;
6355255736Sdavidch    int rc = 0;
6356255736Sdavidch
6357255736Sdavidch    /* allocate the new TPA mbuf */
6358260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6359255736Sdavidch    if (__predict_false(m == NULL)) {
6360255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6361255736Sdavidch        return (ENOBUFS);
6362255736Sdavidch    }
6363255736Sdavidch
6364255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa++;
6365255736Sdavidch
6366255736Sdavidch    /* initialize the mbuf buffer length */
6367255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6368255736Sdavidch
6369255736Sdavidch    /* map the mbuf into non-paged pool */
6370255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6371255736Sdavidch                                 fp->rx_tpa_info_mbuf_spare_map,
6372255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6373255736Sdavidch    if (__predict_false(rc != 0)) {
6374255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6375255736Sdavidch        m_free(m);
6376255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tpa--;
6377255736Sdavidch        return (rc);
6378255736Sdavidch    }
6379255736Sdavidch
6380255736Sdavidch    /* all mbufs must map to a single segment */
6381255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6382255736Sdavidch
6383255736Sdavidch    /* release any existing TPA mbuf mapping */
6384255736Sdavidch    if (tpa_info->bd.m_map != NULL) {
6385255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6386255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6387255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6388255736Sdavidch    }
6389255736Sdavidch
6390255736Sdavidch    /* save the mbuf and mapping info for the TPA mbuf */
6391255736Sdavidch    map = tpa_info->bd.m_map;
6392255736Sdavidch    tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6393255736Sdavidch    fp->rx_tpa_info_mbuf_spare_map = map;
6394255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6395255736Sdavidch                    BUS_DMASYNC_PREREAD);
6396255736Sdavidch    tpa_info->bd.m = m;
6397255736Sdavidch    tpa_info->seg = segs[0];
6398255736Sdavidch
6399255736Sdavidch    return (rc);
6400255736Sdavidch}
6401255736Sdavidch
6402255736Sdavidch/*
6403255736Sdavidch * Allocate an mbuf and assign it to the receive scatter gather chain. The
6404255736Sdavidch * caller must take care to save a copy of the existing mbuf in the SG mbuf
6405255736Sdavidch * chain.
6406255736Sdavidch */
6407255736Sdavidchstatic int
6408255736Sdavidchbxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6409255736Sdavidch                      uint16_t            index)
6410255736Sdavidch{
6411255736Sdavidch    struct bxe_sw_rx_bd *sge_buf;
6412255736Sdavidch    struct eth_rx_sge *sge;
6413255736Sdavidch    bus_dma_segment_t segs[1];
6414255736Sdavidch    bus_dmamap_t map;
6415255736Sdavidch    struct mbuf *m;
6416255736Sdavidch    int nsegs;
6417255736Sdavidch    int rc = 0;
6418255736Sdavidch
6419255736Sdavidch    /* allocate a new SGE mbuf */
6420260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6421255736Sdavidch    if (__predict_false(m == NULL)) {
6422255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6423255736Sdavidch        return (ENOMEM);
6424255736Sdavidch    }
6425255736Sdavidch
6426255736Sdavidch    fp->eth_q_stats.mbuf_alloc_sge++;
6427255736Sdavidch
6428255736Sdavidch    /* initialize the mbuf buffer length */
6429255736Sdavidch    m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6430255736Sdavidch
6431255736Sdavidch    /* map the SGE mbuf into non-paged pool */
6432255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6433255736Sdavidch                                 fp->rx_sge_mbuf_spare_map,
6434255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6435255736Sdavidch    if (__predict_false(rc != 0)) {
6436255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6437255736Sdavidch        m_freem(m);
6438255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
6439255736Sdavidch        return (rc);
6440255736Sdavidch    }
6441255736Sdavidch
6442255736Sdavidch    /* all mbufs must map to a single segment */
6443255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6444255736Sdavidch
6445255736Sdavidch    sge_buf = &fp->rx_sge_mbuf_chain[index];
6446255736Sdavidch
6447255736Sdavidch    /* release any existing SGE mbuf mapping */
6448255736Sdavidch    if (sge_buf->m_map != NULL) {
6449255736Sdavidch        bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6450255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6451255736Sdavidch        bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6452255736Sdavidch    }
6453255736Sdavidch
6454255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6455255736Sdavidch    map = sge_buf->m_map;
6456255736Sdavidch    sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6457255736Sdavidch    fp->rx_sge_mbuf_spare_map = map;
6458255736Sdavidch    bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6459255736Sdavidch                    BUS_DMASYNC_PREREAD);
6460255736Sdavidch    sge_buf->m = m;
6461255736Sdavidch
6462255736Sdavidch    sge = &fp->rx_sge_chain[index];
6463255736Sdavidch    sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6464255736Sdavidch    sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6465255736Sdavidch
6466255736Sdavidch    return (rc);
6467255736Sdavidch}
6468255736Sdavidch
6469255736Sdavidchstatic __noinline int
6470255736Sdavidchbxe_alloc_fp_buffers(struct bxe_softc *sc)
6471255736Sdavidch{
6472255736Sdavidch    struct bxe_fastpath *fp;
6473255736Sdavidch    int i, j, rc = 0;
6474255736Sdavidch    int ring_prod, cqe_ring_prod;
6475255736Sdavidch    int max_agg_queues;
6476255736Sdavidch
6477255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6478255736Sdavidch        fp = &sc->fp[i];
6479255736Sdavidch
6480255736Sdavidch        ring_prod = cqe_ring_prod = 0;
6481255736Sdavidch        fp->rx_bd_cons = 0;
6482255736Sdavidch        fp->rx_cq_cons = 0;
6483255736Sdavidch
6484255736Sdavidch        /* allocate buffers for the RX BDs in RX BD chain */
6485255736Sdavidch        for (j = 0; j < sc->max_rx_bufs; j++) {
6486255736Sdavidch            rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6487255736Sdavidch            if (rc != 0) {
6488255736Sdavidch                BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6489255736Sdavidch                      i, rc);
6490255736Sdavidch                goto bxe_alloc_fp_buffers_error;
6491255736Sdavidch            }
6492255736Sdavidch
6493255736Sdavidch            ring_prod     = RX_BD_NEXT(ring_prod);
6494255736Sdavidch            cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6495255736Sdavidch        }
6496255736Sdavidch
6497255736Sdavidch        fp->rx_bd_prod = ring_prod;
6498255736Sdavidch        fp->rx_cq_prod = cqe_ring_prod;
6499255736Sdavidch        fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6500255736Sdavidch
6501283274Sdavidcs        max_agg_queues = MAX_AGG_QS(sc);
6502255736Sdavidch
6503283274Sdavidcs        fp->tpa_enable = TRUE;
6504255736Sdavidch
6505283274Sdavidcs        /* fill the TPA pool */
6506283274Sdavidcs        for (j = 0; j < max_agg_queues; j++) {
6507283274Sdavidcs            rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6508283274Sdavidcs            if (rc != 0) {
6509283274Sdavidcs                BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6510283274Sdavidcs                          i, j);
6511283274Sdavidcs                fp->tpa_enable = FALSE;
6512283274Sdavidcs                goto bxe_alloc_fp_buffers_error;
6513283274Sdavidcs            }
6514283274Sdavidcs
6515283274Sdavidcs            fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6516283274Sdavidcs        }
6517283274Sdavidcs
6518283274Sdavidcs        if (fp->tpa_enable) {
6519283274Sdavidcs            /* fill the RX SGE chain */
6520283274Sdavidcs            ring_prod = 0;
6521283274Sdavidcs            for (j = 0; j < RX_SGE_USABLE; j++) {
6522283274Sdavidcs                rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6523255736Sdavidch                if (rc != 0) {
6524283274Sdavidcs                    BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6525283274Sdavidcs                              i, ring_prod);
6526255736Sdavidch                    fp->tpa_enable = FALSE;
6527283274Sdavidcs                    ring_prod = 0;
6528255736Sdavidch                    goto bxe_alloc_fp_buffers_error;
6529255736Sdavidch                }
6530255736Sdavidch
6531283274Sdavidcs                ring_prod = RX_SGE_NEXT(ring_prod);
6532255736Sdavidch            }
6533255736Sdavidch
6534283274Sdavidcs            fp->rx_sge_prod = ring_prod;
6535255736Sdavidch        }
6536255736Sdavidch    }
6537255736Sdavidch
6538255736Sdavidch    return (0);
6539255736Sdavidch
6540255736Sdavidchbxe_alloc_fp_buffers_error:
6541255736Sdavidch
6542255736Sdavidch    /* unwind what was already allocated */
6543255736Sdavidch    bxe_free_rx_bd_chain(fp);
6544255736Sdavidch    bxe_free_tpa_pool(fp);
6545255736Sdavidch    bxe_free_sge_chain(fp);
6546255736Sdavidch
6547255736Sdavidch    return (ENOBUFS);
6548255736Sdavidch}
6549255736Sdavidch
6550255736Sdavidchstatic void
6551255736Sdavidchbxe_free_fw_stats_mem(struct bxe_softc *sc)
6552255736Sdavidch{
6553255736Sdavidch    bxe_dma_free(sc, &sc->fw_stats_dma);
6554255736Sdavidch
6555255736Sdavidch    sc->fw_stats_num = 0;
6556255736Sdavidch
6557255736Sdavidch    sc->fw_stats_req_size = 0;
6558255736Sdavidch    sc->fw_stats_req = NULL;
6559255736Sdavidch    sc->fw_stats_req_mapping = 0;
6560255736Sdavidch
6561255736Sdavidch    sc->fw_stats_data_size = 0;
6562255736Sdavidch    sc->fw_stats_data = NULL;
6563255736Sdavidch    sc->fw_stats_data_mapping = 0;
6564255736Sdavidch}
6565255736Sdavidch
6566255736Sdavidchstatic int
6567255736Sdavidchbxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6568255736Sdavidch{
6569255736Sdavidch    uint8_t num_queue_stats;
6570255736Sdavidch    int num_groups;
6571255736Sdavidch
6572255736Sdavidch    /* number of queues for statistics is number of eth queues */
6573255736Sdavidch    num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6574255736Sdavidch
6575255736Sdavidch    /*
6576255736Sdavidch     * Total number of FW statistics requests =
6577255736Sdavidch     *   1 for port stats + 1 for PF stats + num of queues
6578255736Sdavidch     */
6579255736Sdavidch    sc->fw_stats_num = (2 + num_queue_stats);
6580255736Sdavidch
6581255736Sdavidch    /*
6582255736Sdavidch     * Request is built from stats_query_header and an array of
6583255736Sdavidch     * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6584255736Sdavidch     * rules. The real number or requests is configured in the
6585255736Sdavidch     * stats_query_header.
6586255736Sdavidch     */
6587255736Sdavidch    num_groups =
6588255736Sdavidch        ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6589255736Sdavidch         ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6590255736Sdavidch
6591255736Sdavidch    BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6592255736Sdavidch          sc->fw_stats_num, num_groups);
6593255736Sdavidch
6594255736Sdavidch    sc->fw_stats_req_size =
6595255736Sdavidch        (sizeof(struct stats_query_header) +
6596255736Sdavidch         (num_groups * sizeof(struct stats_query_cmd_group)));
6597255736Sdavidch
6598255736Sdavidch    /*
6599255736Sdavidch     * Data for statistics requests + stats_counter.
6600255736Sdavidch     * stats_counter holds per-STORM counters that are incremented when
6601255736Sdavidch     * STORM has finished with the current request. Memory for FCoE
6602255736Sdavidch     * offloaded statistics are counted anyway, even if they will not be sent.
6603255736Sdavidch     * VF stats are not accounted for here as the data of VF stats is stored
6604255736Sdavidch     * in memory allocated by the VF, not here.
6605255736Sdavidch     */
6606255736Sdavidch    sc->fw_stats_data_size =
6607255736Sdavidch        (sizeof(struct stats_counter) +
6608255736Sdavidch         sizeof(struct per_port_stats) +
6609255736Sdavidch         sizeof(struct per_pf_stats) +
6610255736Sdavidch         /* sizeof(struct fcoe_statistics_params) + */
6611255736Sdavidch         (sizeof(struct per_queue_stats) * num_queue_stats));
6612255736Sdavidch
6613255736Sdavidch    if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6614255736Sdavidch                      &sc->fw_stats_dma, "fw stats") != 0) {
6615255736Sdavidch        bxe_free_fw_stats_mem(sc);
6616255736Sdavidch        return (-1);
6617255736Sdavidch    }
6618255736Sdavidch
6619255736Sdavidch    /* set up the shortcuts */
6620255736Sdavidch
6621255736Sdavidch    sc->fw_stats_req =
6622255736Sdavidch        (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6623255736Sdavidch    sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6624255736Sdavidch
6625255736Sdavidch    sc->fw_stats_data =
6626255736Sdavidch        (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6627255736Sdavidch                                     sc->fw_stats_req_size);
6628255736Sdavidch    sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6629255736Sdavidch                                 sc->fw_stats_req_size);
6630255736Sdavidch
6631256341Sdim    BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6632256341Sdim          (uintmax_t)sc->fw_stats_req_mapping);
6633255736Sdavidch
6634256341Sdim    BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6635256341Sdim          (uintmax_t)sc->fw_stats_data_mapping);
6636255736Sdavidch
6637255736Sdavidch    return (0);
6638255736Sdavidch}
6639255736Sdavidch
6640255736Sdavidch/*
6641255736Sdavidch * Bits map:
6642255736Sdavidch * 0-7  - Engine0 load counter.
6643255736Sdavidch * 8-15 - Engine1 load counter.
6644255736Sdavidch * 16   - Engine0 RESET_IN_PROGRESS bit.
6645255736Sdavidch * 17   - Engine1 RESET_IN_PROGRESS bit.
6646255736Sdavidch * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6647255736Sdavidch *        function on the engine
6648255736Sdavidch * 19   - Engine1 ONE_IS_LOADED.
6649255736Sdavidch * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6650255736Sdavidch *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6651255736Sdavidch *        for just the one belonging to its engine).
6652255736Sdavidch */
6653255736Sdavidch#define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6654255736Sdavidch#define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6655255736Sdavidch#define BXE_PATH0_LOAD_CNT_SHIFT  0
6656255736Sdavidch#define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6657255736Sdavidch#define BXE_PATH1_LOAD_CNT_SHIFT  8
6658255736Sdavidch#define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6659255736Sdavidch#define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6660255736Sdavidch#define BXE_GLOBAL_RESET_BIT      0x00040000
6661255736Sdavidch
6662255736Sdavidch/* set the GLOBAL_RESET bit, should be run under rtnl lock */
6663255736Sdavidchstatic void
6664255736Sdavidchbxe_set_reset_global(struct bxe_softc *sc)
6665255736Sdavidch{
6666255736Sdavidch    uint32_t val;
6667255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6668255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6669255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6670255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6671255736Sdavidch}
6672255736Sdavidch
6673255736Sdavidch/* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6674255736Sdavidchstatic void
6675255736Sdavidchbxe_clear_reset_global(struct bxe_softc *sc)
6676255736Sdavidch{
6677255736Sdavidch    uint32_t val;
6678255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6679255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6680255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6681255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6682255736Sdavidch}
6683255736Sdavidch
6684255736Sdavidch/* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6685255736Sdavidchstatic uint8_t
6686255736Sdavidchbxe_reset_is_global(struct bxe_softc *sc)
6687255736Sdavidch{
6688255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6689255736Sdavidch    BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6690255736Sdavidch    return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6691255736Sdavidch}
6692255736Sdavidch
6693255736Sdavidch/* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6694255736Sdavidchstatic void
6695255736Sdavidchbxe_set_reset_done(struct bxe_softc *sc)
6696255736Sdavidch{
6697255736Sdavidch    uint32_t val;
6698255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6699255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6700255736Sdavidch
6701255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6702255736Sdavidch
6703255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6704255736Sdavidch    /* Clear the bit */
6705255736Sdavidch    val &= ~bit;
6706255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6707255736Sdavidch
6708255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6709255736Sdavidch}
6710255736Sdavidch
6711255736Sdavidch/* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6712255736Sdavidchstatic void
6713255736Sdavidchbxe_set_reset_in_progress(struct bxe_softc *sc)
6714255736Sdavidch{
6715255736Sdavidch    uint32_t val;
6716255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6717255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6718255736Sdavidch
6719255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6720255736Sdavidch
6721255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6722255736Sdavidch    /* Set the bit */
6723255736Sdavidch    val |= bit;
6724255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6725255736Sdavidch
6726255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6727255736Sdavidch}
6728255736Sdavidch
6729255736Sdavidch/* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6730255736Sdavidchstatic uint8_t
6731255736Sdavidchbxe_reset_is_done(struct bxe_softc *sc,
6732255736Sdavidch                  int              engine)
6733255736Sdavidch{
6734255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6735255736Sdavidch    uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6736255736Sdavidch                            BXE_PATH0_RST_IN_PROG_BIT;
6737255736Sdavidch
6738255736Sdavidch    /* return false if bit is set */
6739255736Sdavidch    return (val & bit) ? FALSE : TRUE;
6740255736Sdavidch}
6741255736Sdavidch
6742255736Sdavidch/* get the load status for an engine, should be run under rtnl lock */
6743255736Sdavidchstatic uint8_t
6744255736Sdavidchbxe_get_load_status(struct bxe_softc *sc,
6745255736Sdavidch                    int              engine)
6746255736Sdavidch{
6747255736Sdavidch    uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6748255736Sdavidch                             BXE_PATH0_LOAD_CNT_MASK;
6749255736Sdavidch    uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6750255736Sdavidch                              BXE_PATH0_LOAD_CNT_SHIFT;
6751255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6752255736Sdavidch
6753255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6754255736Sdavidch
6755255736Sdavidch    val = ((val & mask) >> shift);
6756255736Sdavidch
6757255736Sdavidch    BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6758255736Sdavidch
6759255736Sdavidch    return (val != 0);
6760255736Sdavidch}
6761255736Sdavidch
6762255736Sdavidch/* set pf load mark */
6763255736Sdavidch/* XXX needs to be under rtnl lock */
6764255736Sdavidchstatic void
6765255736Sdavidchbxe_set_pf_load(struct bxe_softc *sc)
6766255736Sdavidch{
6767255736Sdavidch    uint32_t val;
6768255736Sdavidch    uint32_t val1;
6769255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6770255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6771255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6772255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6773255736Sdavidch
6774255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6775255736Sdavidch
6776255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6777255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6778255736Sdavidch
6779255736Sdavidch    /* get the current counter value */
6780255736Sdavidch    val1 = ((val & mask) >> shift);
6781255736Sdavidch
6782255736Sdavidch    /* set bit of this PF */
6783255736Sdavidch    val1 |= (1 << SC_ABS_FUNC(sc));
6784255736Sdavidch
6785255736Sdavidch    /* clear the old value */
6786255736Sdavidch    val &= ~mask;
6787255736Sdavidch
6788255736Sdavidch    /* set the new one */
6789255736Sdavidch    val |= ((val1 << shift) & mask);
6790255736Sdavidch
6791255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6792255736Sdavidch
6793255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6794255736Sdavidch}
6795255736Sdavidch
6796255736Sdavidch/* clear pf load mark */
6797255736Sdavidch/* XXX needs to be under rtnl lock */
6798255736Sdavidchstatic uint8_t
6799255736Sdavidchbxe_clear_pf_load(struct bxe_softc *sc)
6800255736Sdavidch{
6801255736Sdavidch    uint32_t val1, val;
6802255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6803255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6804255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6805255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6806255736Sdavidch
6807255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6808255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6809255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6810255736Sdavidch
6811255736Sdavidch    /* get the current counter value */
6812255736Sdavidch    val1 = (val & mask) >> shift;
6813255736Sdavidch
6814255736Sdavidch    /* clear bit of that PF */
6815255736Sdavidch    val1 &= ~(1 << SC_ABS_FUNC(sc));
6816255736Sdavidch
6817255736Sdavidch    /* clear the old value */
6818255736Sdavidch    val &= ~mask;
6819255736Sdavidch
6820255736Sdavidch    /* set the new one */
6821255736Sdavidch    val |= ((val1 << shift) & mask);
6822255736Sdavidch
6823255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6824255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6825255736Sdavidch    return (val1 != 0);
6826255736Sdavidch}
6827255736Sdavidch
6828255736Sdavidch/* send load requrest to mcp and analyze response */
6829255736Sdavidchstatic int
6830255736Sdavidchbxe_nic_load_request(struct bxe_softc *sc,
6831255736Sdavidch                     uint32_t         *load_code)
6832255736Sdavidch{
6833255736Sdavidch    /* init fw_seq */
6834255736Sdavidch    sc->fw_seq =
6835255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6836255736Sdavidch         DRV_MSG_SEQ_NUMBER_MASK);
6837255736Sdavidch
6838255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6839255736Sdavidch
6840255736Sdavidch    /* get the current FW pulse sequence */
6841255736Sdavidch    sc->fw_drv_pulse_wr_seq =
6842255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6843255736Sdavidch         DRV_PULSE_SEQ_MASK);
6844255736Sdavidch
6845255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6846255736Sdavidch          sc->fw_drv_pulse_wr_seq);
6847255736Sdavidch
6848255736Sdavidch    /* load request */
6849255736Sdavidch    (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6850255736Sdavidch                                  DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6851255736Sdavidch
6852255736Sdavidch    /* if the MCP fails to respond we must abort */
6853255736Sdavidch    if (!(*load_code)) {
6854255736Sdavidch        BLOGE(sc, "MCP response failure!\n");
6855255736Sdavidch        return (-1);
6856255736Sdavidch    }
6857255736Sdavidch
6858255736Sdavidch    /* if MCP refused then must abort */
6859255736Sdavidch    if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6860255736Sdavidch        BLOGE(sc, "MCP refused load request\n");
6861255736Sdavidch        return (-1);
6862255736Sdavidch    }
6863255736Sdavidch
6864255736Sdavidch    return (0);
6865255736Sdavidch}
6866255736Sdavidch
6867255736Sdavidch/*
6868255736Sdavidch * Check whether another PF has already loaded FW to chip. In virtualized
6869255736Sdavidch * environments a pf from anoth VM may have already initialized the device
6870255736Sdavidch * including loading FW.
6871255736Sdavidch */
6872255736Sdavidchstatic int
6873255736Sdavidchbxe_nic_load_analyze_req(struct bxe_softc *sc,
6874255736Sdavidch                         uint32_t         load_code)
6875255736Sdavidch{
6876255736Sdavidch    uint32_t my_fw, loaded_fw;
6877255736Sdavidch
6878255736Sdavidch    /* is another pf loaded on this engine? */
6879255736Sdavidch    if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6880255736Sdavidch        (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6881255736Sdavidch        /* build my FW version dword */
6882255736Sdavidch        my_fw = (BCM_5710_FW_MAJOR_VERSION +
6883255736Sdavidch                 (BCM_5710_FW_MINOR_VERSION << 8 ) +
6884255736Sdavidch                 (BCM_5710_FW_REVISION_VERSION << 16) +
6885255736Sdavidch                 (BCM_5710_FW_ENGINEERING_VERSION << 24));
6886255736Sdavidch
6887255736Sdavidch        /* read loaded FW from chip */
6888255736Sdavidch        loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6889255736Sdavidch        BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6890255736Sdavidch              loaded_fw, my_fw);
6891255736Sdavidch
6892255736Sdavidch        /* abort nic load if version mismatch */
6893255736Sdavidch        if (my_fw != loaded_fw) {
6894255736Sdavidch            BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6895255736Sdavidch                  loaded_fw, my_fw);
6896255736Sdavidch            return (-1);
6897255736Sdavidch        }
6898255736Sdavidch    }
6899255736Sdavidch
6900255736Sdavidch    return (0);
6901255736Sdavidch}
6902255736Sdavidch
6903255736Sdavidch/* mark PMF if applicable */
6904255736Sdavidchstatic void
6905255736Sdavidchbxe_nic_load_pmf(struct bxe_softc *sc,
6906255736Sdavidch                 uint32_t         load_code)
6907255736Sdavidch{
6908255736Sdavidch    uint32_t ncsi_oem_data_addr;
6909255736Sdavidch
6910255736Sdavidch    if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6911255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6912255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6913255736Sdavidch        /*
6914255736Sdavidch         * Barrier here for ordering between the writing to sc->port.pmf here
6915255736Sdavidch         * and reading it from the periodic task.
6916255736Sdavidch         */
6917255736Sdavidch        sc->port.pmf = 1;
6918255736Sdavidch        mb();
6919255736Sdavidch    } else {
6920255736Sdavidch        sc->port.pmf = 0;
6921255736Sdavidch    }
6922255736Sdavidch
6923255736Sdavidch    BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6924255736Sdavidch
6925255736Sdavidch    /* XXX needed? */
6926255736Sdavidch    if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6927255736Sdavidch        if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6928255736Sdavidch            ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6929255736Sdavidch            if (ncsi_oem_data_addr) {
6930255736Sdavidch                REG_WR(sc,
6931255736Sdavidch                       (ncsi_oem_data_addr +
6932255736Sdavidch                        offsetof(struct glob_ncsi_oem_data, driver_version)),
6933255736Sdavidch                       0);
6934255736Sdavidch            }
6935255736Sdavidch        }
6936255736Sdavidch    }
6937255736Sdavidch}
6938255736Sdavidch
6939255736Sdavidchstatic void
6940255736Sdavidchbxe_read_mf_cfg(struct bxe_softc *sc)
6941255736Sdavidch{
6942255736Sdavidch    int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
6943255736Sdavidch    int abs_func;
6944255736Sdavidch    int vn;
6945255736Sdavidch
6946255736Sdavidch    if (BXE_NOMCP(sc)) {
6947255736Sdavidch        return; /* what should be the default bvalue in this case */
6948255736Sdavidch    }
6949255736Sdavidch
6950255736Sdavidch    /*
6951255736Sdavidch     * The formula for computing the absolute function number is...
6952255736Sdavidch     * For 2 port configuration (4 functions per port):
6953255736Sdavidch     *   abs_func = 2 * vn + SC_PORT + SC_PATH
6954255736Sdavidch     * For 4 port configuration (2 functions per port):
6955255736Sdavidch     *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
6956255736Sdavidch     */
6957255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
6958255736Sdavidch        abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
6959255736Sdavidch        if (abs_func >= E1H_FUNC_MAX) {
6960255736Sdavidch            break;
6961255736Sdavidch        }
6962255736Sdavidch        sc->devinfo.mf_info.mf_config[vn] =
6963255736Sdavidch            MFCFG_RD(sc, func_mf_config[abs_func].config);
6964255736Sdavidch    }
6965255736Sdavidch
6966255736Sdavidch    if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
6967255736Sdavidch        FUNC_MF_CFG_FUNC_DISABLED) {
6968255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
6969255736Sdavidch        sc->flags |= BXE_MF_FUNC_DIS;
6970255736Sdavidch    } else {
6971255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
6972255736Sdavidch        sc->flags &= ~BXE_MF_FUNC_DIS;
6973255736Sdavidch    }
6974255736Sdavidch}
6975255736Sdavidch
6976255736Sdavidch/* acquire split MCP access lock register */
6977255736Sdavidchstatic int bxe_acquire_alr(struct bxe_softc *sc)
6978255736Sdavidch{
6979255736Sdavidch    uint32_t j, val;
6980255736Sdavidch
6981255736Sdavidch    for (j = 0; j < 1000; j++) {
6982255736Sdavidch        val = (1UL << 31);
6983255736Sdavidch        REG_WR(sc, GRCBASE_MCP + 0x9c, val);
6984255736Sdavidch        val = REG_RD(sc, GRCBASE_MCP + 0x9c);
6985255736Sdavidch        if (val & (1L << 31))
6986255736Sdavidch            break;
6987255736Sdavidch
6988255736Sdavidch        DELAY(5000);
6989255736Sdavidch    }
6990255736Sdavidch
6991255736Sdavidch    if (!(val & (1L << 31))) {
6992255736Sdavidch        BLOGE(sc, "Cannot acquire MCP access lock register\n");
6993255736Sdavidch        return (-1);
6994255736Sdavidch    }
6995255736Sdavidch
6996255736Sdavidch    return (0);
6997255736Sdavidch}
6998255736Sdavidch
6999255736Sdavidch/* release split MCP access lock register */
7000255736Sdavidchstatic void bxe_release_alr(struct bxe_softc *sc)
7001255736Sdavidch{
7002255736Sdavidch    REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
7003255736Sdavidch}
7004255736Sdavidch
7005255736Sdavidchstatic void
7006255736Sdavidchbxe_fan_failure(struct bxe_softc *sc)
7007255736Sdavidch{
7008255736Sdavidch    int port = SC_PORT(sc);
7009255736Sdavidch    uint32_t ext_phy_config;
7010255736Sdavidch
7011255736Sdavidch    /* mark the failure */
7012255736Sdavidch    ext_phy_config =
7013255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7014255736Sdavidch
7015255736Sdavidch    ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7016255736Sdavidch    ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7017255736Sdavidch    SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7018255736Sdavidch             ext_phy_config);
7019255736Sdavidch
7020255736Sdavidch    /* log the failure */
7021255736Sdavidch    BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7022255736Sdavidch              "the card to prevent permanent damage. "
7023255736Sdavidch              "Please contact OEM Support for assistance\n");
7024255736Sdavidch
7025255736Sdavidch    /* XXX */
7026255736Sdavidch#if 1
7027255736Sdavidch    bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7028255736Sdavidch#else
7029255736Sdavidch    /*
7030255736Sdavidch     * Schedule device reset (unload)
7031255736Sdavidch     * This is due to some boards consuming sufficient power when driver is
7032255736Sdavidch     * up to overheat if fan fails.
7033255736Sdavidch     */
7034255736Sdavidch    bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7035255736Sdavidch    schedule_delayed_work(&sc->sp_rtnl_task, 0);
7036255736Sdavidch#endif
7037255736Sdavidch}
7038255736Sdavidch
7039255736Sdavidch/* this function is called upon a link interrupt */
7040255736Sdavidchstatic void
7041255736Sdavidchbxe_link_attn(struct bxe_softc *sc)
7042255736Sdavidch{
7043255736Sdavidch    uint32_t pause_enabled = 0;
7044255736Sdavidch    struct host_port_stats *pstats;
7045255736Sdavidch    int cmng_fns;
7046307972Sdavidcs    struct bxe_fastpath *fp;
7047307972Sdavidcs    int i;
7048255736Sdavidch
7049255736Sdavidch    /* Make sure that we are synced with the current statistics */
7050255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_STOP);
7051337510Sdavidcs    BLOGD(sc, DBG_LOAD, "link_vars phy_flags : %x\n", sc->link_vars.phy_flags);
7052255736Sdavidch    elink_link_update(&sc->link_params, &sc->link_vars);
7053255736Sdavidch
7054255736Sdavidch    if (sc->link_vars.link_up) {
7055255736Sdavidch
7056255736Sdavidch        /* dropless flow control */
7057255736Sdavidch        if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7058255736Sdavidch            pause_enabled = 0;
7059255736Sdavidch
7060255736Sdavidch            if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7061255736Sdavidch                pause_enabled = 1;
7062255736Sdavidch            }
7063255736Sdavidch
7064255736Sdavidch            REG_WR(sc,
7065255736Sdavidch                   (BAR_USTRORM_INTMEM +
7066255736Sdavidch                    USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7067255736Sdavidch                   pause_enabled);
7068255736Sdavidch        }
7069255736Sdavidch
7070255736Sdavidch        if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7071255736Sdavidch            pstats = BXE_SP(sc, port_stats);
7072255736Sdavidch            /* reset old mac stats */
7073255736Sdavidch            memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7074255736Sdavidch        }
7075255736Sdavidch
7076255736Sdavidch        if (sc->state == BXE_STATE_OPEN) {
7077255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7078339874Sdavidcs	    /* Restart tx when the link comes back. */
7079339874Sdavidcs	    FOR_EACH_ETH_QUEUE(sc, i) {
7080339874Sdavidcs		fp = &sc->fp[i];
7081339874Sdavidcs		taskqueue_enqueue(fp->tq, &fp->tx_task);
7082339874Sdavidcs	    }
7083255736Sdavidch        }
7084307972Sdavidcs
7085255736Sdavidch    }
7086255736Sdavidch
7087255736Sdavidch    if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7088255736Sdavidch        cmng_fns = bxe_get_cmng_fns_mode(sc);
7089255736Sdavidch
7090255736Sdavidch        if (cmng_fns != CMNG_FNS_NONE) {
7091255736Sdavidch            bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7092255736Sdavidch            storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7093255736Sdavidch        } else {
7094255736Sdavidch            /* rate shaping and fairness are disabled */
7095255736Sdavidch            BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7096255736Sdavidch        }
7097255736Sdavidch    }
7098255736Sdavidch
7099255736Sdavidch    bxe_link_report_locked(sc);
7100255736Sdavidch
7101255736Sdavidch    if (IS_MF(sc)) {
7102255736Sdavidch        ; // XXX bxe_link_sync_notify(sc);
7103255736Sdavidch    }
7104255736Sdavidch}
7105255736Sdavidch
7106255736Sdavidchstatic void
7107255736Sdavidchbxe_attn_int_asserted(struct bxe_softc *sc,
7108255736Sdavidch                      uint32_t         asserted)
7109255736Sdavidch{
7110255736Sdavidch    int port = SC_PORT(sc);
7111255736Sdavidch    uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7112255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
7113255736Sdavidch    uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7114255736Sdavidch                                        NIG_REG_MASK_INTERRUPT_PORT0;
7115255736Sdavidch    uint32_t aeu_mask;
7116255736Sdavidch    uint32_t nig_mask = 0;
7117255736Sdavidch    uint32_t reg_addr;
7118255736Sdavidch    uint32_t igu_acked;
7119255736Sdavidch    uint32_t cnt;
7120255736Sdavidch
7121255736Sdavidch    if (sc->attn_state & asserted) {
7122255736Sdavidch        BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7123255736Sdavidch    }
7124255736Sdavidch
7125255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7126255736Sdavidch
7127255736Sdavidch    aeu_mask = REG_RD(sc, aeu_addr);
7128255736Sdavidch
7129255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7130255736Sdavidch          aeu_mask, asserted);
7131255736Sdavidch
7132255736Sdavidch    aeu_mask &= ~(asserted & 0x3ff);
7133255736Sdavidch
7134255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7135255736Sdavidch
7136255736Sdavidch    REG_WR(sc, aeu_addr, aeu_mask);
7137255736Sdavidch
7138255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7139255736Sdavidch
7140255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7141255736Sdavidch    sc->attn_state |= asserted;
7142255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7143255736Sdavidch
7144255736Sdavidch    if (asserted & ATTN_HARD_WIRED_MASK) {
7145255736Sdavidch        if (asserted & ATTN_NIG_FOR_FUNC) {
7146255736Sdavidch
7147284335Sdavidcs	    bxe_acquire_phy_lock(sc);
7148255736Sdavidch            /* save nig interrupt mask */
7149255736Sdavidch            nig_mask = REG_RD(sc, nig_int_mask_addr);
7150255736Sdavidch
7151255736Sdavidch            /* If nig_mask is not set, no need to call the update function */
7152255736Sdavidch            if (nig_mask) {
7153255736Sdavidch                REG_WR(sc, nig_int_mask_addr, 0);
7154255736Sdavidch
7155255736Sdavidch                bxe_link_attn(sc);
7156255736Sdavidch            }
7157255736Sdavidch
7158255736Sdavidch            /* handle unicore attn? */
7159255736Sdavidch        }
7160255736Sdavidch
7161255736Sdavidch        if (asserted & ATTN_SW_TIMER_4_FUNC) {
7162255736Sdavidch            BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7163255736Sdavidch        }
7164255736Sdavidch
7165255736Sdavidch        if (asserted & GPIO_2_FUNC) {
7166255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7167255736Sdavidch        }
7168255736Sdavidch
7169255736Sdavidch        if (asserted & GPIO_3_FUNC) {
7170255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7171255736Sdavidch        }
7172255736Sdavidch
7173255736Sdavidch        if (asserted & GPIO_4_FUNC) {
7174255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7175255736Sdavidch        }
7176255736Sdavidch
7177255736Sdavidch        if (port == 0) {
7178255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_1) {
7179255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7180255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7181255736Sdavidch            }
7182255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_2) {
7183255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7184255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7185255736Sdavidch            }
7186255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_3) {
7187255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7188255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7189255736Sdavidch            }
7190255736Sdavidch        } else {
7191255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_4) {
7192255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7193255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7194255736Sdavidch            }
7195255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_5) {
7196255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7197255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7198255736Sdavidch            }
7199255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_6) {
7200255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7201255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7202255736Sdavidch            }
7203255736Sdavidch        }
7204255736Sdavidch    } /* hardwired */
7205255736Sdavidch
7206255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7207255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7208255736Sdavidch    } else {
7209255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7210255736Sdavidch    }
7211255736Sdavidch
7212255736Sdavidch    BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7213255736Sdavidch          asserted,
7214255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7215255736Sdavidch    REG_WR(sc, reg_addr, asserted);
7216255736Sdavidch
7217255736Sdavidch    /* now set back the mask */
7218255736Sdavidch    if (asserted & ATTN_NIG_FOR_FUNC) {
7219255736Sdavidch        /*
7220255736Sdavidch         * Verify that IGU ack through BAR was written before restoring
7221255736Sdavidch         * NIG mask. This loop should exit after 2-3 iterations max.
7222255736Sdavidch         */
7223255736Sdavidch        if (sc->devinfo.int_block != INT_BLOCK_HC) {
7224255736Sdavidch            cnt = 0;
7225255736Sdavidch
7226255736Sdavidch            do {
7227255736Sdavidch                igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7228255736Sdavidch            } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7229255736Sdavidch                     (++cnt < MAX_IGU_ATTN_ACK_TO));
7230255736Sdavidch
7231255736Sdavidch            if (!igu_acked) {
7232255736Sdavidch                BLOGE(sc, "Failed to verify IGU ack on time\n");
7233255736Sdavidch            }
7234255736Sdavidch
7235255736Sdavidch            mb();
7236255736Sdavidch        }
7237255736Sdavidch
7238255736Sdavidch        REG_WR(sc, nig_int_mask_addr, nig_mask);
7239255736Sdavidch
7240284335Sdavidcs	bxe_release_phy_lock(sc);
7241255736Sdavidch    }
7242255736Sdavidch}
7243255736Sdavidch
7244255736Sdavidchstatic void
7245255736Sdavidchbxe_print_next_block(struct bxe_softc *sc,
7246255736Sdavidch                     int              idx,
7247255736Sdavidch                     const char       *blk)
7248255736Sdavidch{
7249255736Sdavidch    BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7250255736Sdavidch}
7251255736Sdavidch
7252255736Sdavidchstatic int
7253255736Sdavidchbxe_check_blocks_with_parity0(struct bxe_softc *sc,
7254255736Sdavidch                              uint32_t         sig,
7255255736Sdavidch                              int              par_num,
7256255736Sdavidch                              uint8_t          print)
7257255736Sdavidch{
7258255736Sdavidch    uint32_t cur_bit = 0;
7259255736Sdavidch    int i = 0;
7260255736Sdavidch
7261255736Sdavidch    for (i = 0; sig; i++) {
7262255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7263255736Sdavidch        if (sig & cur_bit) {
7264255736Sdavidch            switch (cur_bit) {
7265255736Sdavidch            case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7266255736Sdavidch                if (print)
7267255736Sdavidch                    bxe_print_next_block(sc, par_num++, "BRB");
7268255736Sdavidch                break;
7269255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7270255736Sdavidch                if (print)
7271255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PARSER");
7272255736Sdavidch                break;
7273255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7274255736Sdavidch                if (print)
7275255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSDM");
7276255736Sdavidch                break;
7277255736Sdavidch            case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7278255736Sdavidch                if (print)
7279255736Sdavidch                    bxe_print_next_block(sc, par_num++, "SEARCHER");
7280255736Sdavidch                break;
7281255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7282255736Sdavidch                if (print)
7283255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TCM");
7284255736Sdavidch                break;
7285255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7286255736Sdavidch                if (print)
7287255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSEMI");
7288255736Sdavidch                break;
7289255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7290255736Sdavidch                if (print)
7291255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XPB");
7292255736Sdavidch                break;
7293255736Sdavidch            }
7294255736Sdavidch
7295255736Sdavidch            /* Clear the bit */
7296255736Sdavidch            sig &= ~cur_bit;
7297255736Sdavidch        }
7298255736Sdavidch    }
7299255736Sdavidch
7300255736Sdavidch    return (par_num);
7301255736Sdavidch}
7302255736Sdavidch
7303255736Sdavidchstatic int
7304255736Sdavidchbxe_check_blocks_with_parity1(struct bxe_softc *sc,
7305255736Sdavidch                              uint32_t         sig,
7306255736Sdavidch                              int              par_num,
7307255736Sdavidch                              uint8_t          *global,
7308255736Sdavidch                              uint8_t          print)
7309255736Sdavidch{
7310255736Sdavidch    int i = 0;
7311255736Sdavidch    uint32_t cur_bit = 0;
7312255736Sdavidch    for (i = 0; sig; i++) {
7313255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7314255736Sdavidch        if (sig & cur_bit) {
7315255736Sdavidch            switch (cur_bit) {
7316255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7317255736Sdavidch                if (print)
7318255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PBF");
7319255736Sdavidch                break;
7320255736Sdavidch            case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7321255736Sdavidch                if (print)
7322255736Sdavidch                    bxe_print_next_block(sc, par_num++, "QM");
7323255736Sdavidch                break;
7324255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7325255736Sdavidch                if (print)
7326255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TM");
7327255736Sdavidch                break;
7328255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7329255736Sdavidch                if (print)
7330255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSDM");
7331255736Sdavidch                break;
7332255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7333255736Sdavidch                if (print)
7334255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XCM");
7335255736Sdavidch                break;
7336255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7337255736Sdavidch                if (print)
7338255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSEMI");
7339255736Sdavidch                break;
7340255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7341255736Sdavidch                if (print)
7342255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7343255736Sdavidch                break;
7344255736Sdavidch            case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7345255736Sdavidch                if (print)
7346255736Sdavidch                    bxe_print_next_block(sc, par_num++, "NIG");
7347255736Sdavidch                break;
7348255736Sdavidch            case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7349255736Sdavidch                if (print)
7350255736Sdavidch                    bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7351255736Sdavidch                *global = TRUE;
7352255736Sdavidch                break;
7353255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7354255736Sdavidch                if (print)
7355255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DEBUG");
7356255736Sdavidch                break;
7357255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7358255736Sdavidch                if (print)
7359255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USDM");
7360255736Sdavidch                break;
7361255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7362255736Sdavidch                if (print)
7363255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UCM");
7364255736Sdavidch                break;
7365255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7366255736Sdavidch                if (print)
7367255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USEMI");
7368255736Sdavidch                break;
7369255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7370255736Sdavidch                if (print)
7371255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UPB");
7372255736Sdavidch                break;
7373255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7374255736Sdavidch                if (print)
7375255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSDM");
7376255736Sdavidch                break;
7377255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7378255736Sdavidch                if (print)
7379255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CCM");
7380255736Sdavidch                break;
7381255736Sdavidch            }
7382255736Sdavidch
7383255736Sdavidch            /* Clear the bit */
7384255736Sdavidch            sig &= ~cur_bit;
7385255736Sdavidch        }
7386255736Sdavidch    }
7387255736Sdavidch
7388255736Sdavidch    return (par_num);
7389255736Sdavidch}
7390255736Sdavidch
7391255736Sdavidchstatic int
7392255736Sdavidchbxe_check_blocks_with_parity2(struct bxe_softc *sc,
7393255736Sdavidch                              uint32_t         sig,
7394255736Sdavidch                              int              par_num,
7395255736Sdavidch                              uint8_t          print)
7396255736Sdavidch{
7397255736Sdavidch    uint32_t cur_bit = 0;
7398255736Sdavidch    int i = 0;
7399255736Sdavidch
7400255736Sdavidch    for (i = 0; sig; i++) {
7401255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7402255736Sdavidch        if (sig & cur_bit) {
7403255736Sdavidch            switch (cur_bit) {
7404255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7405255736Sdavidch                if (print)
7406255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSEMI");
7407255736Sdavidch                break;
7408255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7409255736Sdavidch                if (print)
7410255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXP");
7411255736Sdavidch                break;
7412255736Sdavidch            case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7413255736Sdavidch                if (print)
7414255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7415255736Sdavidch                break;
7416255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7417255736Sdavidch                if (print)
7418255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CFC");
7419255736Sdavidch                break;
7420255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7421255736Sdavidch                if (print)
7422255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CDU");
7423255736Sdavidch                break;
7424255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7425255736Sdavidch                if (print)
7426255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DMAE");
7427255736Sdavidch                break;
7428255736Sdavidch            case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7429255736Sdavidch                if (print)
7430255736Sdavidch                    bxe_print_next_block(sc, par_num++, "IGU");
7431255736Sdavidch                break;
7432255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7433255736Sdavidch                if (print)
7434255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MISC");
7435255736Sdavidch                break;
7436255736Sdavidch            }
7437255736Sdavidch
7438255736Sdavidch            /* Clear the bit */
7439255736Sdavidch            sig &= ~cur_bit;
7440255736Sdavidch        }
7441255736Sdavidch    }
7442255736Sdavidch
7443255736Sdavidch    return (par_num);
7444255736Sdavidch}
7445255736Sdavidch
7446255736Sdavidchstatic int
7447255736Sdavidchbxe_check_blocks_with_parity3(struct bxe_softc *sc,
7448255736Sdavidch                              uint32_t         sig,
7449255736Sdavidch                              int              par_num,
7450255736Sdavidch                              uint8_t          *global,
7451255736Sdavidch                              uint8_t          print)
7452255736Sdavidch{
7453255736Sdavidch    uint32_t cur_bit = 0;
7454255736Sdavidch    int i = 0;
7455255736Sdavidch
7456255736Sdavidch    for (i = 0; sig; i++) {
7457255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7458255736Sdavidch        if (sig & cur_bit) {
7459255736Sdavidch            switch (cur_bit) {
7460255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7461255736Sdavidch                if (print)
7462255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MCP ROM");
7463255736Sdavidch                *global = TRUE;
7464255736Sdavidch                break;
7465255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7466255736Sdavidch                if (print)
7467255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7468255736Sdavidch                              "MCP UMP RX");
7469255736Sdavidch                *global = TRUE;
7470255736Sdavidch                break;
7471255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7472255736Sdavidch                if (print)
7473255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7474255736Sdavidch                              "MCP UMP TX");
7475255736Sdavidch                *global = TRUE;
7476255736Sdavidch                break;
7477255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7478255736Sdavidch                if (print)
7479255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7480255736Sdavidch                              "MCP SCPAD");
7481255736Sdavidch                *global = TRUE;
7482255736Sdavidch                break;
7483255736Sdavidch            }
7484255736Sdavidch
7485255736Sdavidch            /* Clear the bit */
7486255736Sdavidch            sig &= ~cur_bit;
7487255736Sdavidch        }
7488255736Sdavidch    }
7489255736Sdavidch
7490255736Sdavidch    return (par_num);
7491255736Sdavidch}
7492255736Sdavidch
7493255736Sdavidchstatic int
7494255736Sdavidchbxe_check_blocks_with_parity4(struct bxe_softc *sc,
7495255736Sdavidch                              uint32_t         sig,
7496255736Sdavidch                              int              par_num,
7497255736Sdavidch                              uint8_t          print)
7498255736Sdavidch{
7499255736Sdavidch    uint32_t cur_bit = 0;
7500255736Sdavidch    int i = 0;
7501255736Sdavidch
7502255736Sdavidch    for (i = 0; sig; i++) {
7503255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7504255736Sdavidch        if (sig & cur_bit) {
7505255736Sdavidch            switch (cur_bit) {
7506255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7507255736Sdavidch                if (print)
7508255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PGLUE_B");
7509255736Sdavidch                break;
7510255736Sdavidch            case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7511255736Sdavidch                if (print)
7512255736Sdavidch                    bxe_print_next_block(sc, par_num++, "ATC");
7513255736Sdavidch                break;
7514255736Sdavidch            }
7515255736Sdavidch
7516255736Sdavidch            /* Clear the bit */
7517255736Sdavidch            sig &= ~cur_bit;
7518255736Sdavidch        }
7519255736Sdavidch    }
7520255736Sdavidch
7521255736Sdavidch    return (par_num);
7522255736Sdavidch}
7523255736Sdavidch
7524255736Sdavidchstatic uint8_t
7525255736Sdavidchbxe_parity_attn(struct bxe_softc *sc,
7526255736Sdavidch                uint8_t          *global,
7527255736Sdavidch                uint8_t          print,
7528255736Sdavidch                uint32_t         *sig)
7529255736Sdavidch{
7530255736Sdavidch    int par_num = 0;
7531255736Sdavidch
7532255736Sdavidch    if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7533255736Sdavidch        (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7534255736Sdavidch        (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7535255736Sdavidch        (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7536255736Sdavidch        (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7537255736Sdavidch        BLOGE(sc, "Parity error: HW block parity attention:\n"
7538255736Sdavidch                  "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7539255736Sdavidch              (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7540255736Sdavidch              (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7541255736Sdavidch              (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7542255736Sdavidch              (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7543255736Sdavidch              (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7544255736Sdavidch
7545255736Sdavidch        if (print)
7546255736Sdavidch            BLOGI(sc, "Parity errors detected in blocks: ");
7547255736Sdavidch
7548255736Sdavidch        par_num =
7549255736Sdavidch            bxe_check_blocks_with_parity0(sc, sig[0] &
7550255736Sdavidch                                          HW_PRTY_ASSERT_SET_0,
7551255736Sdavidch                                          par_num, print);
7552255736Sdavidch        par_num =
7553255736Sdavidch            bxe_check_blocks_with_parity1(sc, sig[1] &
7554255736Sdavidch                                          HW_PRTY_ASSERT_SET_1,
7555255736Sdavidch                                          par_num, global, print);
7556255736Sdavidch        par_num =
7557255736Sdavidch            bxe_check_blocks_with_parity2(sc, sig[2] &
7558255736Sdavidch                                          HW_PRTY_ASSERT_SET_2,
7559255736Sdavidch                                          par_num, print);
7560255736Sdavidch        par_num =
7561255736Sdavidch            bxe_check_blocks_with_parity3(sc, sig[3] &
7562255736Sdavidch                                          HW_PRTY_ASSERT_SET_3,
7563255736Sdavidch                                          par_num, global, print);
7564255736Sdavidch        par_num =
7565255736Sdavidch            bxe_check_blocks_with_parity4(sc, sig[4] &
7566255736Sdavidch                                          HW_PRTY_ASSERT_SET_4,
7567255736Sdavidch                                          par_num, print);
7568255736Sdavidch
7569255736Sdavidch        if (print)
7570255736Sdavidch            BLOGI(sc, "\n");
7571255736Sdavidch
7572339881Sdavidcs	if( *global == TRUE ) {
7573339881Sdavidcs                BXE_SET_ERROR_BIT(sc, BXE_ERR_GLOBAL);
7574339881Sdavidcs        }
7575339881Sdavidcs
7576255736Sdavidch        return (TRUE);
7577255736Sdavidch    }
7578255736Sdavidch
7579255736Sdavidch    return (FALSE);
7580255736Sdavidch}
7581255736Sdavidch
7582255736Sdavidchstatic uint8_t
7583255736Sdavidchbxe_chk_parity_attn(struct bxe_softc *sc,
7584255736Sdavidch                    uint8_t          *global,
7585255736Sdavidch                    uint8_t          print)
7586255736Sdavidch{
7587255736Sdavidch    struct attn_route attn = { {0} };
7588255736Sdavidch    int port = SC_PORT(sc);
7589255736Sdavidch
7590339881Sdavidcs    if(sc->state != BXE_STATE_OPEN)
7591339881Sdavidcs        return FALSE;
7592339881Sdavidcs
7593255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7594255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7595255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7596255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7597255736Sdavidch
7598295823Sdavidcs    /*
7599295823Sdavidcs     * Since MCP attentions can't be disabled inside the block, we need to
7600295823Sdavidcs     * read AEU registers to see whether they're currently disabled
7601295823Sdavidcs     */
7602295823Sdavidcs    attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7603295823Sdavidcs                                      : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7604295823Sdavidcs                         MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7605295823Sdavidcs                        ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7606295823Sdavidcs
7607295823Sdavidcs
7608255736Sdavidch    if (!CHIP_IS_E1x(sc))
7609255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7610255736Sdavidch
7611255736Sdavidch    return (bxe_parity_attn(sc, global, print, attn.sig));
7612255736Sdavidch}
7613255736Sdavidch
7614255736Sdavidchstatic void
7615255736Sdavidchbxe_attn_int_deasserted4(struct bxe_softc *sc,
7616255736Sdavidch                         uint32_t         attn)
7617255736Sdavidch{
7618255736Sdavidch    uint32_t val;
7619339881Sdavidcs    boolean_t err_flg = FALSE;
7620255736Sdavidch
7621255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7622255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7623255736Sdavidch        BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7624339881Sdavidcs        err_flg = TRUE;
7625255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7626255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7627255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7628255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7629255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7630255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7631255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7632255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7633255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7634255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7635255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7636255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7637255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7638255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7639255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7640255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7641255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7642255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7643255736Sdavidch    }
7644255736Sdavidch
7645255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7646255736Sdavidch        val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7647255736Sdavidch        BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7648339881Sdavidcs	err_flg = TRUE;
7649255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7650255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7651255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7652255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7653255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7654255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7655255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7656255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7657255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7658255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7659255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7660255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7661255736Sdavidch    }
7662255736Sdavidch
7663255736Sdavidch    if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7664255736Sdavidch                AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7665255736Sdavidch        BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7666255736Sdavidch              (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7667255736Sdavidch                                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7668339881Sdavidcs	err_flg = TRUE;
7669255736Sdavidch    }
7670339881Sdavidcs    if (err_flg) {
7671339881Sdavidcs	BXE_SET_ERROR_BIT(sc, BXE_ERR_MISC);
7672339881Sdavidcs	taskqueue_enqueue_timeout(taskqueue_thread,
7673339881Sdavidcs	    &sc->sp_err_timeout_task, hz/10);
7674339881Sdavidcs    }
7675339881Sdavidcs
7676255736Sdavidch}
7677255736Sdavidch
7678255736Sdavidchstatic void
7679255736Sdavidchbxe_e1h_disable(struct bxe_softc *sc)
7680255736Sdavidch{
7681255736Sdavidch    int port = SC_PORT(sc);
7682255736Sdavidch
7683255736Sdavidch    bxe_tx_disable(sc);
7684255736Sdavidch
7685255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7686255736Sdavidch}
7687255736Sdavidch
7688255736Sdavidchstatic void
7689255736Sdavidchbxe_e1h_enable(struct bxe_softc *sc)
7690255736Sdavidch{
7691255736Sdavidch    int port = SC_PORT(sc);
7692255736Sdavidch
7693255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7694255736Sdavidch
7695255736Sdavidch    // XXX bxe_tx_enable(sc);
7696255736Sdavidch}
7697255736Sdavidch
7698255736Sdavidch/*
7699255736Sdavidch * called due to MCP event (on pmf):
7700255736Sdavidch *   reread new bandwidth configuration
7701255736Sdavidch *   configure FW
7702255736Sdavidch *   notify others function about the change
7703255736Sdavidch */
7704255736Sdavidchstatic void
7705255736Sdavidchbxe_config_mf_bw(struct bxe_softc *sc)
7706255736Sdavidch{
7707255736Sdavidch    if (sc->link_vars.link_up) {
7708255736Sdavidch        bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7709255736Sdavidch        // XXX bxe_link_sync_notify(sc);
7710255736Sdavidch    }
7711255736Sdavidch
7712255736Sdavidch    storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7713255736Sdavidch}
7714255736Sdavidch
7715255736Sdavidchstatic void
7716255736Sdavidchbxe_set_mf_bw(struct bxe_softc *sc)
7717255736Sdavidch{
7718255736Sdavidch    bxe_config_mf_bw(sc);
7719255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7720255736Sdavidch}
7721255736Sdavidch
7722255736Sdavidchstatic void
7723255736Sdavidchbxe_handle_eee_event(struct bxe_softc *sc)
7724255736Sdavidch{
7725255736Sdavidch    BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7726255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7727255736Sdavidch}
7728255736Sdavidch
7729255736Sdavidch#define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7730255736Sdavidch
7731255736Sdavidchstatic void
7732255736Sdavidchbxe_drv_info_ether_stat(struct bxe_softc *sc)
7733255736Sdavidch{
7734255736Sdavidch    struct eth_stats_info *ether_stat =
7735255736Sdavidch        &sc->sp->drv_info_to_mcp.ether_stat;
7736255736Sdavidch
7737255736Sdavidch    strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7738255736Sdavidch            ETH_STAT_INFO_VERSION_LEN);
7739255736Sdavidch
7740255736Sdavidch    /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7741255736Sdavidch    sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7742255736Sdavidch                                          DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7743255736Sdavidch                                          ether_stat->mac_local + MAC_PAD,
7744255736Sdavidch                                          MAC_PAD, ETH_ALEN);
7745255736Sdavidch
7746255736Sdavidch    ether_stat->mtu_size = sc->mtu;
7747255736Sdavidch
7748255736Sdavidch    ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7749266979Smarcel    if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
7750255736Sdavidch        ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7751255736Sdavidch    }
7752255736Sdavidch
7753255736Sdavidch    // XXX ether_stat->feature_flags |= ???;
7754255736Sdavidch
7755255736Sdavidch    ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7756255736Sdavidch
7757255736Sdavidch    ether_stat->txq_size = sc->tx_ring_size;
7758255736Sdavidch    ether_stat->rxq_size = sc->rx_ring_size;
7759255736Sdavidch}
7760255736Sdavidch
7761255736Sdavidchstatic void
7762255736Sdavidchbxe_handle_drv_info_req(struct bxe_softc *sc)
7763255736Sdavidch{
7764255736Sdavidch    enum drv_info_opcode op_code;
7765255736Sdavidch    uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7766255736Sdavidch
7767255736Sdavidch    /* if drv_info version supported by MFW doesn't match - send NACK */
7768255736Sdavidch    if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7769255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7770255736Sdavidch        return;
7771255736Sdavidch    }
7772255736Sdavidch
7773255736Sdavidch    op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7774255736Sdavidch               DRV_INFO_CONTROL_OP_CODE_SHIFT);
7775255736Sdavidch
7776255736Sdavidch    memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7777255736Sdavidch
7778255736Sdavidch    switch (op_code) {
7779255736Sdavidch    case ETH_STATS_OPCODE:
7780255736Sdavidch        bxe_drv_info_ether_stat(sc);
7781255736Sdavidch        break;
7782255736Sdavidch    case FCOE_STATS_OPCODE:
7783255736Sdavidch    case ISCSI_STATS_OPCODE:
7784255736Sdavidch    default:
7785255736Sdavidch        /* if op code isn't supported - send NACK */
7786255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7787255736Sdavidch        return;
7788255736Sdavidch    }
7789255736Sdavidch
7790255736Sdavidch    /*
7791255736Sdavidch     * If we got drv_info attn from MFW then these fields are defined in
7792255736Sdavidch     * shmem2 for sure
7793255736Sdavidch     */
7794255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_lo,
7795255736Sdavidch              U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7796255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_hi,
7797255736Sdavidch              U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7798255736Sdavidch
7799255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7800255736Sdavidch}
7801255736Sdavidch
7802255736Sdavidchstatic void
7803255736Sdavidchbxe_dcc_event(struct bxe_softc *sc,
7804255736Sdavidch              uint32_t         dcc_event)
7805255736Sdavidch{
7806255736Sdavidch    BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7807255736Sdavidch
7808255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7809255736Sdavidch        /*
7810255736Sdavidch         * This is the only place besides the function initialization
7811255736Sdavidch         * where the sc->flags can change so it is done without any
7812255736Sdavidch         * locks
7813255736Sdavidch         */
7814255736Sdavidch        if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7815255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7816255736Sdavidch            sc->flags |= BXE_MF_FUNC_DIS;
7817255736Sdavidch            bxe_e1h_disable(sc);
7818255736Sdavidch        } else {
7819255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7820255736Sdavidch            sc->flags &= ~BXE_MF_FUNC_DIS;
7821255736Sdavidch            bxe_e1h_enable(sc);
7822255736Sdavidch        }
7823255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7824255736Sdavidch    }
7825255736Sdavidch
7826255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7827255736Sdavidch        bxe_config_mf_bw(sc);
7828255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7829255736Sdavidch    }
7830255736Sdavidch
7831255736Sdavidch    /* Report results to MCP */
7832255736Sdavidch    if (dcc_event)
7833255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7834255736Sdavidch    else
7835255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7836255736Sdavidch}
7837255736Sdavidch
7838255736Sdavidchstatic void
7839255736Sdavidchbxe_pmf_update(struct bxe_softc *sc)
7840255736Sdavidch{
7841255736Sdavidch    int port = SC_PORT(sc);
7842255736Sdavidch    uint32_t val;
7843255736Sdavidch
7844255736Sdavidch    sc->port.pmf = 1;
7845255736Sdavidch    BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7846255736Sdavidch
7847255736Sdavidch    /*
7848255736Sdavidch     * We need the mb() to ensure the ordering between the writing to
7849255736Sdavidch     * sc->port.pmf here and reading it from the bxe_periodic_task().
7850255736Sdavidch     */
7851255736Sdavidch    mb();
7852255736Sdavidch
7853255736Sdavidch    /* queue a periodic task */
7854255736Sdavidch    // XXX schedule task...
7855255736Sdavidch
7856255736Sdavidch    // XXX bxe_dcbx_pmf_update(sc);
7857255736Sdavidch
7858255736Sdavidch    /* enable nig attention */
7859255736Sdavidch    val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7860255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7861255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7862255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7863255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
7864255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7865255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7866255736Sdavidch    }
7867255736Sdavidch
7868255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_PMF);
7869255736Sdavidch}
7870255736Sdavidch
7871255736Sdavidchstatic int
7872255736Sdavidchbxe_mc_assert(struct bxe_softc *sc)
7873255736Sdavidch{
7874255736Sdavidch    char last_idx;
7875255736Sdavidch    int i, rc = 0;
7876255736Sdavidch    uint32_t row0, row1, row2, row3;
7877255736Sdavidch
7878255736Sdavidch    /* XSTORM */
7879255736Sdavidch    last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7880255736Sdavidch    if (last_idx)
7881255736Sdavidch        BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7882255736Sdavidch
7883255736Sdavidch    /* print the asserts */
7884255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7885255736Sdavidch
7886255736Sdavidch        row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7887255736Sdavidch        row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7888255736Sdavidch        row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7889255736Sdavidch        row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7890255736Sdavidch
7891255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7892255736Sdavidch            BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7893255736Sdavidch                  i, row3, row2, row1, row0);
7894255736Sdavidch            rc++;
7895255736Sdavidch        } else {
7896255736Sdavidch            break;
7897255736Sdavidch        }
7898255736Sdavidch    }
7899255736Sdavidch
7900255736Sdavidch    /* TSTORM */
7901255736Sdavidch    last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7902255736Sdavidch    if (last_idx) {
7903255736Sdavidch        BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7904255736Sdavidch    }
7905255736Sdavidch
7906255736Sdavidch    /* print the asserts */
7907255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7908255736Sdavidch
7909255736Sdavidch        row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7910255736Sdavidch        row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7911255736Sdavidch        row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7912255736Sdavidch        row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7913255736Sdavidch
7914255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7915255736Sdavidch            BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7916255736Sdavidch                  i, row3, row2, row1, row0);
7917255736Sdavidch            rc++;
7918255736Sdavidch        } else {
7919255736Sdavidch            break;
7920255736Sdavidch        }
7921255736Sdavidch    }
7922255736Sdavidch
7923255736Sdavidch    /* CSTORM */
7924255736Sdavidch    last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7925255736Sdavidch    if (last_idx) {
7926255736Sdavidch        BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7927255736Sdavidch    }
7928255736Sdavidch
7929255736Sdavidch    /* print the asserts */
7930255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7931255736Sdavidch
7932255736Sdavidch        row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7933255736Sdavidch        row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7934255736Sdavidch        row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7935255736Sdavidch        row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7936255736Sdavidch
7937255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7938255736Sdavidch            BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7939255736Sdavidch                  i, row3, row2, row1, row0);
7940255736Sdavidch            rc++;
7941255736Sdavidch        } else {
7942255736Sdavidch            break;
7943255736Sdavidch        }
7944255736Sdavidch    }
7945255736Sdavidch
7946255736Sdavidch    /* USTORM */
7947255736Sdavidch    last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7948255736Sdavidch    if (last_idx) {
7949255736Sdavidch        BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7950255736Sdavidch    }
7951255736Sdavidch
7952255736Sdavidch    /* print the asserts */
7953255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7954255736Sdavidch
7955255736Sdavidch        row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7956255736Sdavidch        row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7957255736Sdavidch        row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7958255736Sdavidch        row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7959255736Sdavidch
7960255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7961255736Sdavidch            BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7962255736Sdavidch                  i, row3, row2, row1, row0);
7963255736Sdavidch            rc++;
7964255736Sdavidch        } else {
7965255736Sdavidch            break;
7966255736Sdavidch        }
7967255736Sdavidch    }
7968255736Sdavidch
7969255736Sdavidch    return (rc);
7970255736Sdavidch}
7971255736Sdavidch
7972255736Sdavidchstatic void
7973255736Sdavidchbxe_attn_int_deasserted3(struct bxe_softc *sc,
7974255736Sdavidch                         uint32_t         attn)
7975255736Sdavidch{
7976255736Sdavidch    int func = SC_FUNC(sc);
7977255736Sdavidch    uint32_t val;
7978255736Sdavidch
7979255736Sdavidch    if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
7980255736Sdavidch
7981255736Sdavidch        if (attn & BXE_PMF_LINK_ASSERT(sc)) {
7982255736Sdavidch
7983255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
7984255736Sdavidch            bxe_read_mf_cfg(sc);
7985255736Sdavidch            sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
7986255736Sdavidch                MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
7987255736Sdavidch            val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
7988255736Sdavidch
7989255736Sdavidch            if (val & DRV_STATUS_DCC_EVENT_MASK)
7990255736Sdavidch                bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
7991255736Sdavidch
7992255736Sdavidch            if (val & DRV_STATUS_SET_MF_BW)
7993255736Sdavidch                bxe_set_mf_bw(sc);
7994255736Sdavidch
7995255736Sdavidch            if (val & DRV_STATUS_DRV_INFO_REQ)
7996255736Sdavidch                bxe_handle_drv_info_req(sc);
7997255736Sdavidch
7998255736Sdavidch            if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
7999255736Sdavidch                bxe_pmf_update(sc);
8000255736Sdavidch
8001255736Sdavidch            if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
8002255736Sdavidch                bxe_handle_eee_event(sc);
8003255736Sdavidch
8004255736Sdavidch            if (sc->link_vars.periodic_flags &
8005255736Sdavidch                ELINK_PERIODIC_FLAGS_LINK_EVENT) {
8006255736Sdavidch                /* sync with link */
8007284335Sdavidcs		bxe_acquire_phy_lock(sc);
8008255736Sdavidch                sc->link_vars.periodic_flags &=
8009255736Sdavidch                    ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
8010284335Sdavidcs		bxe_release_phy_lock(sc);
8011255736Sdavidch                if (IS_MF(sc))
8012255736Sdavidch                    ; // XXX bxe_link_sync_notify(sc);
8013255736Sdavidch                bxe_link_report(sc);
8014255736Sdavidch            }
8015255736Sdavidch
8016255736Sdavidch            /*
8017255736Sdavidch             * Always call it here: bxe_link_report() will
8018255736Sdavidch             * prevent the link indication duplication.
8019255736Sdavidch             */
8020255736Sdavidch            bxe_link_status_update(sc);
8021255736Sdavidch
8022255736Sdavidch        } else if (attn & BXE_MC_ASSERT_BITS) {
8023255736Sdavidch
8024255736Sdavidch            BLOGE(sc, "MC assert!\n");
8025255736Sdavidch            bxe_mc_assert(sc);
8026255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
8027255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
8028255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
8029255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
8030339881Sdavidcs            bxe_int_disable(sc);
8031339881Sdavidcs            BXE_SET_ERROR_BIT(sc, BXE_ERR_MC_ASSERT);
8032339881Sdavidcs            taskqueue_enqueue_timeout(taskqueue_thread,
8033339881Sdavidcs                &sc->sp_err_timeout_task, hz/10);
8034339881Sdavidcs
8035255736Sdavidch        } else if (attn & BXE_MCP_ASSERT) {
8036255736Sdavidch
8037255736Sdavidch            BLOGE(sc, "MCP assert!\n");
8038255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8039339881Sdavidcs            BXE_SET_ERROR_BIT(sc, BXE_ERR_MCP_ASSERT);
8040339881Sdavidcs            taskqueue_enqueue_timeout(taskqueue_thread,
8041339881Sdavidcs                &sc->sp_err_timeout_task, hz/10);
8042339881Sdavidcs            bxe_int_disable(sc);  /*avoid repetive assert alert */
8043255736Sdavidch
8044339881Sdavidcs
8045255736Sdavidch        } else {
8046255736Sdavidch            BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8047255736Sdavidch        }
8048255736Sdavidch    }
8049255736Sdavidch
8050255736Sdavidch    if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8051255736Sdavidch        BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8052255736Sdavidch        if (attn & BXE_GRC_TIMEOUT) {
8053255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8054255736Sdavidch            BLOGE(sc, "GRC time-out 0x%08x\n", val);
8055255736Sdavidch        }
8056255736Sdavidch        if (attn & BXE_GRC_RSV) {
8057255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8058255736Sdavidch            BLOGE(sc, "GRC reserved 0x%08x\n", val);
8059255736Sdavidch        }
8060255736Sdavidch        REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8061255736Sdavidch    }
8062255736Sdavidch}
8063255736Sdavidch
8064255736Sdavidchstatic void
8065255736Sdavidchbxe_attn_int_deasserted2(struct bxe_softc *sc,
8066255736Sdavidch                         uint32_t         attn)
8067255736Sdavidch{
8068255736Sdavidch    int port = SC_PORT(sc);
8069255736Sdavidch    int reg_offset;
8070255736Sdavidch    uint32_t val0, mask0, val1, mask1;
8071255736Sdavidch    uint32_t val;
8072339881Sdavidcs    boolean_t err_flg = FALSE;
8073255736Sdavidch
8074255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8075255736Sdavidch        val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8076255736Sdavidch        BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8077255736Sdavidch        /* CFC error attention */
8078255736Sdavidch        if (val & 0x2) {
8079255736Sdavidch            BLOGE(sc, "FATAL error from CFC\n");
8080339881Sdavidcs	    err_flg = TRUE;
8081255736Sdavidch        }
8082255736Sdavidch    }
8083255736Sdavidch
8084255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8085255736Sdavidch        val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8086255736Sdavidch        BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8087255736Sdavidch        /* RQ_USDMDP_FIFO_OVERFLOW */
8088255736Sdavidch        if (val & 0x18000) {
8089255736Sdavidch            BLOGE(sc, "FATAL error from PXP\n");
8090339881Sdavidcs	    err_flg = TRUE;
8091255736Sdavidch        }
8092255736Sdavidch
8093255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8094255736Sdavidch            val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8095255736Sdavidch            BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8096339881Sdavidcs	    err_flg = TRUE;
8097255736Sdavidch        }
8098255736Sdavidch    }
8099255736Sdavidch
8100255736Sdavidch#define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8101255736Sdavidch#define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8102255736Sdavidch
8103255736Sdavidch    if (attn & AEU_PXP2_HW_INT_BIT) {
8104255736Sdavidch        /*  CQ47854 workaround do not panic on
8105255736Sdavidch         *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8106255736Sdavidch         */
8107255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8108255736Sdavidch            mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8109255736Sdavidch            val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8110255736Sdavidch            mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8111255736Sdavidch            val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8112255736Sdavidch            /*
8113298955Spfg             * If the only PXP2_EOP_ERROR_BIT is set in
8114255736Sdavidch             * STS0 and STS1 - clear it
8115255736Sdavidch             *
8116255736Sdavidch             * probably we lose additional attentions between
8117255736Sdavidch             * STS0 and STS_CLR0, in this case user will not
8118255736Sdavidch             * be notified about them
8119255736Sdavidch             */
8120255736Sdavidch            if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8121255736Sdavidch                !(val1 & mask1))
8122255736Sdavidch                val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8123255736Sdavidch
8124255736Sdavidch            /* print the register, since no one can restore it */
8125255736Sdavidch            BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8126255736Sdavidch
8127255736Sdavidch            /*
8128255736Sdavidch             * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8129255736Sdavidch             * then notify
8130255736Sdavidch             */
8131255736Sdavidch            if (val0 & PXP2_EOP_ERROR_BIT) {
8132255736Sdavidch                BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8133339881Sdavidcs		err_flg = TRUE;
8134255736Sdavidch
8135255736Sdavidch                /*
8136255736Sdavidch                 * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8137255736Sdavidch                 * set then clear attention from PXP2 block without panic
8138255736Sdavidch                 */
8139255736Sdavidch                if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8140255736Sdavidch                    ((val1 & mask1) == 0))
8141255736Sdavidch                    attn &= ~AEU_PXP2_HW_INT_BIT;
8142255736Sdavidch            }
8143255736Sdavidch        }
8144255736Sdavidch    }
8145255736Sdavidch
8146255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_2) {
8147255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8148255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8149255736Sdavidch
8150255736Sdavidch        val = REG_RD(sc, reg_offset);
8151255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8152255736Sdavidch        REG_WR(sc, reg_offset, val);
8153255736Sdavidch
8154255736Sdavidch        BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8155255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8156339881Sdavidcs	err_flg = TRUE;
8157255736Sdavidch        bxe_panic(sc, ("HW block attention set2\n"));
8158255736Sdavidch    }
8159339881Sdavidcs    if(err_flg) {
8160339881Sdavidcs        BXE_SET_ERROR_BIT(sc, BXE_ERR_GLOBAL);
8161339881Sdavidcs        taskqueue_enqueue_timeout(taskqueue_thread,
8162339881Sdavidcs           &sc->sp_err_timeout_task, hz/10);
8163339881Sdavidcs    }
8164339881Sdavidcs
8165255736Sdavidch}
8166255736Sdavidch
8167255736Sdavidchstatic void
8168255736Sdavidchbxe_attn_int_deasserted1(struct bxe_softc *sc,
8169255736Sdavidch                         uint32_t         attn)
8170255736Sdavidch{
8171255736Sdavidch    int port = SC_PORT(sc);
8172255736Sdavidch    int reg_offset;
8173255736Sdavidch    uint32_t val;
8174339881Sdavidcs    boolean_t err_flg = FALSE;
8175255736Sdavidch
8176255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8177255736Sdavidch        val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8178255736Sdavidch        BLOGE(sc, "DB hw attention 0x%08x\n", val);
8179255736Sdavidch        /* DORQ discard attention */
8180255736Sdavidch        if (val & 0x2) {
8181255736Sdavidch            BLOGE(sc, "FATAL error from DORQ\n");
8182339881Sdavidcs	    err_flg = TRUE;
8183255736Sdavidch        }
8184255736Sdavidch    }
8185255736Sdavidch
8186255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_1) {
8187255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8188255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8189255736Sdavidch
8190255736Sdavidch        val = REG_RD(sc, reg_offset);
8191255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8192255736Sdavidch        REG_WR(sc, reg_offset, val);
8193255736Sdavidch
8194255736Sdavidch        BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8195255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8196339881Sdavidcs        err_flg = TRUE;
8197255736Sdavidch        bxe_panic(sc, ("HW block attention set1\n"));
8198255736Sdavidch    }
8199339881Sdavidcs    if(err_flg) {
8200339881Sdavidcs        BXE_SET_ERROR_BIT(sc, BXE_ERR_MISC);
8201339881Sdavidcs        taskqueue_enqueue_timeout(taskqueue_thread,
8202339881Sdavidcs           &sc->sp_err_timeout_task, hz/10);
8203339881Sdavidcs    }
8204339881Sdavidcs
8205255736Sdavidch}
8206255736Sdavidch
8207255736Sdavidchstatic void
8208255736Sdavidchbxe_attn_int_deasserted0(struct bxe_softc *sc,
8209255736Sdavidch                         uint32_t         attn)
8210255736Sdavidch{
8211255736Sdavidch    int port = SC_PORT(sc);
8212255736Sdavidch    int reg_offset;
8213255736Sdavidch    uint32_t val;
8214255736Sdavidch
8215255736Sdavidch    reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8216255736Sdavidch                          MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8217255736Sdavidch
8218255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8219255736Sdavidch        val = REG_RD(sc, reg_offset);
8220255736Sdavidch        val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8221255736Sdavidch        REG_WR(sc, reg_offset, val);
8222255736Sdavidch
8223255736Sdavidch        BLOGW(sc, "SPIO5 hw attention\n");
8224255736Sdavidch
8225255736Sdavidch        /* Fan failure attention */
8226255736Sdavidch        elink_hw_reset_phy(&sc->link_params);
8227255736Sdavidch        bxe_fan_failure(sc);
8228255736Sdavidch    }
8229255736Sdavidch
8230255736Sdavidch    if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8231284335Sdavidcs	bxe_acquire_phy_lock(sc);
8232255736Sdavidch        elink_handle_module_detect_int(&sc->link_params);
8233284335Sdavidcs	bxe_release_phy_lock(sc);
8234255736Sdavidch    }
8235255736Sdavidch
8236255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_0) {
8237255736Sdavidch        val = REG_RD(sc, reg_offset);
8238255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8239255736Sdavidch        REG_WR(sc, reg_offset, val);
8240255736Sdavidch
8241339881Sdavidcs
8242339881Sdavidcs        BXE_SET_ERROR_BIT(sc, BXE_ERR_MISC);
8243339881Sdavidcs        taskqueue_enqueue_timeout(taskqueue_thread,
8244339881Sdavidcs           &sc->sp_err_timeout_task, hz/10);
8245339881Sdavidcs
8246255736Sdavidch        bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8247255736Sdavidch                       (attn & HW_INTERRUT_ASSERT_SET_0)));
8248255736Sdavidch    }
8249255736Sdavidch}
8250255736Sdavidch
8251255736Sdavidchstatic void
8252255736Sdavidchbxe_attn_int_deasserted(struct bxe_softc *sc,
8253255736Sdavidch                        uint32_t         deasserted)
8254255736Sdavidch{
8255255736Sdavidch    struct attn_route attn;
8256255736Sdavidch    struct attn_route *group_mask;
8257255736Sdavidch    int port = SC_PORT(sc);
8258255736Sdavidch    int index;
8259255736Sdavidch    uint32_t reg_addr;
8260255736Sdavidch    uint32_t val;
8261255736Sdavidch    uint32_t aeu_mask;
8262255736Sdavidch    uint8_t global = FALSE;
8263255736Sdavidch
8264255736Sdavidch    /*
8265255736Sdavidch     * Need to take HW lock because MCP or other port might also
8266255736Sdavidch     * try to handle this event.
8267255736Sdavidch     */
8268255736Sdavidch    bxe_acquire_alr(sc);
8269255736Sdavidch
8270255736Sdavidch    if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8271255736Sdavidch        /* XXX
8272255736Sdavidch         * In case of parity errors don't handle attentions so that
8273255736Sdavidch         * other function would "see" parity errors.
8274255736Sdavidch         */
8275255736Sdavidch        // XXX schedule a recovery task...
8276255736Sdavidch        /* disable HW interrupts */
8277255736Sdavidch        bxe_int_disable(sc);
8278339881Sdavidcs        BXE_SET_ERROR_BIT(sc, BXE_ERR_PARITY);
8279339881Sdavidcs        taskqueue_enqueue_timeout(taskqueue_thread,
8280339881Sdavidcs           &sc->sp_err_timeout_task, hz/10);
8281255736Sdavidch        bxe_release_alr(sc);
8282255736Sdavidch        return;
8283255736Sdavidch    }
8284255736Sdavidch
8285255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8286255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8287255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8288255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8289255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
8290255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8291255736Sdavidch    } else {
8292255736Sdavidch        attn.sig[4] = 0;
8293255736Sdavidch    }
8294255736Sdavidch
8295255736Sdavidch    BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8296255736Sdavidch          attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8297255736Sdavidch
8298255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8299255736Sdavidch        if (deasserted & (1 << index)) {
8300255736Sdavidch            group_mask = &sc->attn_group[index];
8301255736Sdavidch
8302255736Sdavidch            BLOGD(sc, DBG_INTR,
8303255736Sdavidch                  "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8304255736Sdavidch                  group_mask->sig[0], group_mask->sig[1],
8305255736Sdavidch                  group_mask->sig[2], group_mask->sig[3],
8306255736Sdavidch                  group_mask->sig[4]);
8307255736Sdavidch
8308255736Sdavidch            bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8309255736Sdavidch            bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8310255736Sdavidch            bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8311255736Sdavidch            bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8312255736Sdavidch            bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8313255736Sdavidch        }
8314255736Sdavidch    }
8315255736Sdavidch
8316255736Sdavidch    bxe_release_alr(sc);
8317255736Sdavidch
8318255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
8319255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 +
8320255736Sdavidch                    COMMAND_REG_ATTN_BITS_CLR);
8321255736Sdavidch    } else {
8322255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8323255736Sdavidch    }
8324255736Sdavidch
8325255736Sdavidch    val = ~deasserted;
8326255736Sdavidch    BLOGD(sc, DBG_INTR,
8327255736Sdavidch          "about to mask 0x%08x at %s addr 0x%08x\n", val,
8328255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8329255736Sdavidch    REG_WR(sc, reg_addr, val);
8330255736Sdavidch
8331255736Sdavidch    if (~sc->attn_state & deasserted) {
8332255736Sdavidch        BLOGE(sc, "IGU error\n");
8333255736Sdavidch    }
8334255736Sdavidch
8335255736Sdavidch    reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8336255736Sdavidch                      MISC_REG_AEU_MASK_ATTN_FUNC_0;
8337255736Sdavidch
8338255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8339255736Sdavidch
8340255736Sdavidch    aeu_mask = REG_RD(sc, reg_addr);
8341255736Sdavidch
8342255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8343255736Sdavidch          aeu_mask, deasserted);
8344255736Sdavidch    aeu_mask |= (deasserted & 0x3ff);
8345255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8346255736Sdavidch
8347255736Sdavidch    REG_WR(sc, reg_addr, aeu_mask);
8348255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8349255736Sdavidch
8350255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8351255736Sdavidch    sc->attn_state &= ~deasserted;
8352255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8353255736Sdavidch}
8354255736Sdavidch
8355255736Sdavidchstatic void
8356255736Sdavidchbxe_attn_int(struct bxe_softc *sc)
8357255736Sdavidch{
8358255736Sdavidch    /* read local copy of bits */
8359255736Sdavidch    uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8360255736Sdavidch    uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8361255736Sdavidch    uint32_t attn_state = sc->attn_state;
8362255736Sdavidch
8363255736Sdavidch    /* look for changed bits */
8364255736Sdavidch    uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8365255736Sdavidch    uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8366255736Sdavidch
8367255736Sdavidch    BLOGD(sc, DBG_INTR,
8368255736Sdavidch          "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8369255736Sdavidch          attn_bits, attn_ack, asserted, deasserted);
8370255736Sdavidch
8371255736Sdavidch    if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8372255736Sdavidch        BLOGE(sc, "BAD attention state\n");
8373255736Sdavidch    }
8374255736Sdavidch
8375255736Sdavidch    /* handle bits that were raised */
8376255736Sdavidch    if (asserted) {
8377255736Sdavidch        bxe_attn_int_asserted(sc, asserted);
8378255736Sdavidch    }
8379255736Sdavidch
8380255736Sdavidch    if (deasserted) {
8381255736Sdavidch        bxe_attn_int_deasserted(sc, deasserted);
8382255736Sdavidch    }
8383255736Sdavidch}
8384255736Sdavidch
8385255736Sdavidchstatic uint16_t
8386255736Sdavidchbxe_update_dsb_idx(struct bxe_softc *sc)
8387255736Sdavidch{
8388255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
8389255736Sdavidch    uint16_t rc = 0;
8390255736Sdavidch
8391255736Sdavidch    mb(); /* status block is written to by the chip */
8392255736Sdavidch
8393255736Sdavidch    if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8394255736Sdavidch        sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8395255736Sdavidch        rc |= BXE_DEF_SB_ATT_IDX;
8396255736Sdavidch    }
8397255736Sdavidch
8398255736Sdavidch    if (sc->def_idx != def_sb->sp_sb.running_index) {
8399255736Sdavidch        sc->def_idx = def_sb->sp_sb.running_index;
8400255736Sdavidch        rc |= BXE_DEF_SB_IDX;
8401255736Sdavidch    }
8402255736Sdavidch
8403255736Sdavidch    mb();
8404255736Sdavidch
8405255736Sdavidch    return (rc);
8406255736Sdavidch}
8407255736Sdavidch
8408255736Sdavidchstatic inline struct ecore_queue_sp_obj *
8409255736Sdavidchbxe_cid_to_q_obj(struct bxe_softc *sc,
8410255736Sdavidch                 uint32_t         cid)
8411255736Sdavidch{
8412255736Sdavidch    BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8413255736Sdavidch    return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8414255736Sdavidch}
8415255736Sdavidch
8416255736Sdavidchstatic void
8417255736Sdavidchbxe_handle_mcast_eqe(struct bxe_softc *sc)
8418255736Sdavidch{
8419255736Sdavidch    struct ecore_mcast_ramrod_params rparam;
8420255736Sdavidch    int rc;
8421255736Sdavidch
8422255736Sdavidch    memset(&rparam, 0, sizeof(rparam));
8423255736Sdavidch
8424255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
8425255736Sdavidch
8426255736Sdavidch    BXE_MCAST_LOCK(sc);
8427255736Sdavidch
8428255736Sdavidch    /* clear pending state for the last command */
8429255736Sdavidch    sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8430255736Sdavidch
8431255736Sdavidch    /* if there are pending mcast commands - send them */
8432255736Sdavidch    if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8433255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8434255736Sdavidch        if (rc < 0) {
8435255736Sdavidch            BLOGD(sc, DBG_SP,
8436295830Sdavidcs                "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8437255736Sdavidch        }
8438255736Sdavidch    }
8439255736Sdavidch
8440255736Sdavidch    BXE_MCAST_UNLOCK(sc);
8441255736Sdavidch}
8442255736Sdavidch
8443255736Sdavidchstatic void
8444255736Sdavidchbxe_handle_classification_eqe(struct bxe_softc      *sc,
8445255736Sdavidch                              union event_ring_elem *elem)
8446255736Sdavidch{
8447255736Sdavidch    unsigned long ramrod_flags = 0;
8448255736Sdavidch    int rc = 0;
8449255736Sdavidch    uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8450255736Sdavidch    struct ecore_vlan_mac_obj *vlan_mac_obj;
8451255736Sdavidch
8452255736Sdavidch    /* always push next commands out, don't wait here */
8453255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
8454255736Sdavidch
8455255736Sdavidch    switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8456255736Sdavidch    case ECORE_FILTER_MAC_PENDING:
8457255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8458255736Sdavidch        vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8459255736Sdavidch        break;
8460255736Sdavidch
8461255736Sdavidch    case ECORE_FILTER_MCAST_PENDING:
8462255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8463255736Sdavidch        /*
8464255736Sdavidch         * This is only relevant for 57710 where multicast MACs are
8465255736Sdavidch         * configured as unicast MACs using the same ramrod.
8466255736Sdavidch         */
8467255736Sdavidch        bxe_handle_mcast_eqe(sc);
8468255736Sdavidch        return;
8469255736Sdavidch
8470255736Sdavidch    default:
8471255736Sdavidch        BLOGE(sc, "Unsupported classification command: %d\n",
8472255736Sdavidch              elem->message.data.eth_event.echo);
8473255736Sdavidch        return;
8474255736Sdavidch    }
8475255736Sdavidch
8476255736Sdavidch    rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8477255736Sdavidch
8478255736Sdavidch    if (rc < 0) {
8479255736Sdavidch        BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8480255736Sdavidch    } else if (rc > 0) {
8481255736Sdavidch        BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8482255736Sdavidch    }
8483255736Sdavidch}
8484255736Sdavidch
8485255736Sdavidchstatic void
8486255736Sdavidchbxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8487255736Sdavidch                       union event_ring_elem *elem)
8488255736Sdavidch{
8489255736Sdavidch    bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8490255736Sdavidch
8491255736Sdavidch    /* send rx_mode command again if was requested */
8492255736Sdavidch    if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8493255736Sdavidch                               &sc->sp_state)) {
8494255736Sdavidch        bxe_set_storm_rx_mode(sc);
8495255736Sdavidch    }
8496255736Sdavidch}
8497255736Sdavidch
8498255736Sdavidchstatic void
8499255736Sdavidchbxe_update_eq_prod(struct bxe_softc *sc,
8500255736Sdavidch                   uint16_t         prod)
8501255736Sdavidch{
8502255736Sdavidch    storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8503255736Sdavidch    wmb(); /* keep prod updates ordered */
8504255736Sdavidch}
8505255736Sdavidch
8506255736Sdavidchstatic void
8507255736Sdavidchbxe_eq_int(struct bxe_softc *sc)
8508255736Sdavidch{
8509255736Sdavidch    uint16_t hw_cons, sw_cons, sw_prod;
8510255736Sdavidch    union event_ring_elem *elem;
8511255736Sdavidch    uint8_t echo;
8512255736Sdavidch    uint32_t cid;
8513255736Sdavidch    uint8_t opcode;
8514255736Sdavidch    int spqe_cnt = 0;
8515255736Sdavidch    struct ecore_queue_sp_obj *q_obj;
8516255736Sdavidch    struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8517255736Sdavidch    struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8518255736Sdavidch
8519255736Sdavidch    hw_cons = le16toh(*sc->eq_cons_sb);
8520255736Sdavidch
8521255736Sdavidch    /*
8522255736Sdavidch     * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8523255736Sdavidch     * when we get to the next-page we need to adjust so the loop
8524255736Sdavidch     * condition below will be met. The next element is the size of a
8525255736Sdavidch     * regular element and hence incrementing by 1
8526255736Sdavidch     */
8527255736Sdavidch    if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8528255736Sdavidch        hw_cons++;
8529255736Sdavidch    }
8530255736Sdavidch
8531255736Sdavidch    /*
8532255736Sdavidch     * This function may never run in parallel with itself for a
8533255736Sdavidch     * specific sc and no need for a read memory barrier here.
8534255736Sdavidch     */
8535255736Sdavidch    sw_cons = sc->eq_cons;
8536255736Sdavidch    sw_prod = sc->eq_prod;
8537255736Sdavidch
8538255736Sdavidch    BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8539255736Sdavidch          hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8540255736Sdavidch
8541255736Sdavidch    for (;
8542255736Sdavidch         sw_cons != hw_cons;
8543255736Sdavidch         sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8544255736Sdavidch
8545255736Sdavidch        elem = &sc->eq[EQ_DESC(sw_cons)];
8546255736Sdavidch
8547255736Sdavidch        /* elem CID originates from FW, actually LE */
8548255736Sdavidch        cid = SW_CID(elem->message.data.cfc_del_event.cid);
8549255736Sdavidch        opcode = elem->message.opcode;
8550255736Sdavidch
8551255736Sdavidch        /* handle eq element */
8552255736Sdavidch        switch (opcode) {
8553255736Sdavidch
8554255736Sdavidch        case EVENT_RING_OPCODE_STAT_QUERY:
8555255736Sdavidch            BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8556255736Sdavidch                  sc->stats_comp++);
8557255736Sdavidch            /* nothing to do with stats comp */
8558255736Sdavidch            goto next_spqe;
8559255736Sdavidch
8560255736Sdavidch        case EVENT_RING_OPCODE_CFC_DEL:
8561255736Sdavidch            /* handle according to cid range */
8562255736Sdavidch            /* we may want to verify here that the sc state is HALTING */
8563255736Sdavidch            BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8564255736Sdavidch            q_obj = bxe_cid_to_q_obj(sc, cid);
8565255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8566255736Sdavidch                break;
8567255736Sdavidch            }
8568255736Sdavidch            goto next_spqe;
8569255736Sdavidch
8570255736Sdavidch        case EVENT_RING_OPCODE_STOP_TRAFFIC:
8571255736Sdavidch            BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8572255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8573255736Sdavidch                break;
8574255736Sdavidch            }
8575255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8576255736Sdavidch            goto next_spqe;
8577255736Sdavidch
8578255736Sdavidch        case EVENT_RING_OPCODE_START_TRAFFIC:
8579255736Sdavidch            BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8580255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8581255736Sdavidch                break;
8582255736Sdavidch            }
8583255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8584255736Sdavidch            goto next_spqe;
8585255736Sdavidch
8586255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8587255736Sdavidch            echo = elem->message.data.function_update_event.echo;
8588255736Sdavidch            if (echo == SWITCH_UPDATE) {
8589255736Sdavidch                BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8590255736Sdavidch                if (f_obj->complete_cmd(sc, f_obj,
8591255736Sdavidch                                        ECORE_F_CMD_SWITCH_UPDATE)) {
8592255736Sdavidch                    break;
8593255736Sdavidch                }
8594255736Sdavidch            }
8595255736Sdavidch            else {
8596255736Sdavidch                BLOGD(sc, DBG_SP,
8597255736Sdavidch                      "AFEX: ramrod completed FUNCTION_UPDATE\n");
8598255736Sdavidch            }
8599255736Sdavidch            goto next_spqe;
8600255736Sdavidch
8601255736Sdavidch        case EVENT_RING_OPCODE_FORWARD_SETUP:
8602255736Sdavidch            q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8603255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj,
8604255736Sdavidch                                    ECORE_Q_CMD_SETUP_TX_ONLY)) {
8605255736Sdavidch                break;
8606255736Sdavidch            }
8607255736Sdavidch            goto next_spqe;
8608255736Sdavidch
8609255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_START:
8610255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8611255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8612255736Sdavidch                break;
8613255736Sdavidch            }
8614255736Sdavidch            goto next_spqe;
8615255736Sdavidch
8616255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_STOP:
8617255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8618255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8619255736Sdavidch                break;
8620255736Sdavidch            }
8621255736Sdavidch            goto next_spqe;
8622255736Sdavidch        }
8623255736Sdavidch
8624255736Sdavidch        switch (opcode | sc->state) {
8625255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8626255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8627255736Sdavidch            cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8628255736Sdavidch            BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8629255736Sdavidch            rss_raw->clear_pending(rss_raw);
8630255736Sdavidch            break;
8631255736Sdavidch
8632255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8633255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8634255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8635255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8636255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8637255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8638255736Sdavidch            BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8639255736Sdavidch            bxe_handle_classification_eqe(sc, elem);
8640255736Sdavidch            break;
8641255736Sdavidch
8642255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8643255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8644255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8645255736Sdavidch            BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8646255736Sdavidch            bxe_handle_mcast_eqe(sc);
8647255736Sdavidch            break;
8648255736Sdavidch
8649255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8650255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8651255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8652255736Sdavidch            BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8653255736Sdavidch            bxe_handle_rx_mode_eqe(sc, elem);
8654255736Sdavidch            break;
8655255736Sdavidch
8656255736Sdavidch        default:
8657255736Sdavidch            /* unknown event log error and continue */
8658255736Sdavidch            BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8659255736Sdavidch                  elem->message.opcode, sc->state);
8660255736Sdavidch        }
8661255736Sdavidch
8662255736Sdavidchnext_spqe:
8663255736Sdavidch        spqe_cnt++;
8664255736Sdavidch    } /* for */
8665255736Sdavidch
8666255736Sdavidch    mb();
8667255736Sdavidch    atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8668255736Sdavidch
8669255736Sdavidch    sc->eq_cons = sw_cons;
8670255736Sdavidch    sc->eq_prod = sw_prod;
8671255736Sdavidch
8672255736Sdavidch    /* make sure that above mem writes were issued towards the memory */
8673255736Sdavidch    wmb();
8674255736Sdavidch
8675255736Sdavidch    /* update producer */
8676255736Sdavidch    bxe_update_eq_prod(sc, sc->eq_prod);
8677255736Sdavidch}
8678255736Sdavidch
8679255736Sdavidchstatic void
8680255736Sdavidchbxe_handle_sp_tq(void *context,
8681255736Sdavidch                 int  pending)
8682255736Sdavidch{
8683255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
8684255736Sdavidch    uint16_t status;
8685255736Sdavidch
8686255736Sdavidch    BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8687255736Sdavidch
8688255736Sdavidch    /* what work needs to be performed? */
8689255736Sdavidch    status = bxe_update_dsb_idx(sc);
8690255736Sdavidch
8691255736Sdavidch    BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8692255736Sdavidch
8693255736Sdavidch    /* HW attentions */
8694255736Sdavidch    if (status & BXE_DEF_SB_ATT_IDX) {
8695255736Sdavidch        BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8696255736Sdavidch        bxe_attn_int(sc);
8697255736Sdavidch        status &= ~BXE_DEF_SB_ATT_IDX;
8698255736Sdavidch    }
8699255736Sdavidch
8700255736Sdavidch    /* SP events: STAT_QUERY and others */
8701255736Sdavidch    if (status & BXE_DEF_SB_IDX) {
8702255736Sdavidch        /* handle EQ completions */
8703255736Sdavidch        BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8704255736Sdavidch        bxe_eq_int(sc);
8705255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8706255736Sdavidch                   le16toh(sc->def_idx), IGU_INT_NOP, 1);
8707255736Sdavidch        status &= ~BXE_DEF_SB_IDX;
8708255736Sdavidch    }
8709255736Sdavidch
8710255736Sdavidch    /* if status is non zero then something went wrong */
8711255736Sdavidch    if (__predict_false(status)) {
8712255736Sdavidch        BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8713255736Sdavidch    }
8714255736Sdavidch
8715255736Sdavidch    /* ack status block only if something was actually handled */
8716255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8717255736Sdavidch               le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8718255736Sdavidch
8719255736Sdavidch    /*
8720255736Sdavidch     * Must be called after the EQ processing (since eq leads to sriov
8721255736Sdavidch     * ramrod completion flows).
8722255736Sdavidch     * This flow may have been scheduled by the arrival of a ramrod
8723255736Sdavidch     * completion, or by the sriov code rescheduling itself.
8724255736Sdavidch     */
8725255736Sdavidch    // XXX bxe_iov_sp_task(sc);
8726255736Sdavidch
8727255736Sdavidch}
8728255736Sdavidch
8729255736Sdavidchstatic void
8730255736Sdavidchbxe_handle_fp_tq(void *context,
8731255736Sdavidch                 int  pending)
8732255736Sdavidch{
8733255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8734255736Sdavidch    struct bxe_softc *sc = fp->sc;
8735255736Sdavidch    uint8_t more_tx = FALSE;
8736255736Sdavidch    uint8_t more_rx = FALSE;
8737255736Sdavidch
8738255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8739255736Sdavidch
8740255736Sdavidch    /* XXX
8741255736Sdavidch     * IFF_DRV_RUNNING state can't be checked here since we process
8742255736Sdavidch     * slowpath events on a client queue during setup. Instead
8743255736Sdavidch     * we need to add a "process/continue" flag here that the driver
8744255736Sdavidch     * can use to tell the task here not to do anything.
8745255736Sdavidch     */
8746255736Sdavidch#if 0
8747266979Smarcel    if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
8748255736Sdavidch        return;
8749255736Sdavidch    }
8750255736Sdavidch#endif
8751255736Sdavidch
8752255736Sdavidch    /* update the fastpath index */
8753255736Sdavidch    bxe_update_fp_sb_idx(fp);
8754255736Sdavidch
8755255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8756255736Sdavidch    /* fp->txdata[cos] */
8757255736Sdavidch    if (bxe_has_tx_work(fp)) {
8758255736Sdavidch        BXE_FP_TX_LOCK(fp);
8759255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8760255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8761255736Sdavidch    }
8762255736Sdavidch
8763255736Sdavidch    if (bxe_has_rx_work(fp)) {
8764255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8765255736Sdavidch    }
8766255736Sdavidch
8767255736Sdavidch    if (more_rx /*|| more_tx*/) {
8768255736Sdavidch        /* still more work to do */
8769296272Sjhb        taskqueue_enqueue(fp->tq, &fp->tq_task);
8770255736Sdavidch        return;
8771255736Sdavidch    }
8772255736Sdavidch
8773255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8774255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8775255736Sdavidch}
8776255736Sdavidch
8777255736Sdavidchstatic void
8778255736Sdavidchbxe_task_fp(struct bxe_fastpath *fp)
8779255736Sdavidch{
8780255736Sdavidch    struct bxe_softc *sc = fp->sc;
8781255736Sdavidch    uint8_t more_tx = FALSE;
8782255736Sdavidch    uint8_t more_rx = FALSE;
8783255736Sdavidch
8784255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8785255736Sdavidch
8786255736Sdavidch    /* update the fastpath index */
8787255736Sdavidch    bxe_update_fp_sb_idx(fp);
8788255736Sdavidch
8789255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8790255736Sdavidch    /* fp->txdata[cos] */
8791255736Sdavidch    if (bxe_has_tx_work(fp)) {
8792255736Sdavidch        BXE_FP_TX_LOCK(fp);
8793255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8794255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8795255736Sdavidch    }
8796255736Sdavidch
8797255736Sdavidch    if (bxe_has_rx_work(fp)) {
8798255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8799255736Sdavidch    }
8800255736Sdavidch
8801255736Sdavidch    if (more_rx /*|| more_tx*/) {
8802255736Sdavidch        /* still more work to do, bail out if this ISR and process later */
8803296272Sjhb        taskqueue_enqueue(fp->tq, &fp->tq_task);
8804255736Sdavidch        return;
8805255736Sdavidch    }
8806255736Sdavidch
8807255736Sdavidch    /*
8808255736Sdavidch     * Here we write the fastpath index taken before doing any tx or rx work.
8809255736Sdavidch     * It is very well possible other hw events occurred up to this point and
8810255736Sdavidch     * they were actually processed accordingly above. Since we're going to
8811255736Sdavidch     * write an older fastpath index, an interrupt is coming which we might
8812255736Sdavidch     * not do any work in.
8813255736Sdavidch     */
8814255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8815255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8816255736Sdavidch}
8817255736Sdavidch
8818255736Sdavidch/*
8819255736Sdavidch * Legacy interrupt entry point.
8820255736Sdavidch *
8821255736Sdavidch * Verifies that the controller generated the interrupt and
8822255736Sdavidch * then calls a separate routine to handle the various
8823255736Sdavidch * interrupt causes: link, RX, and TX.
8824255736Sdavidch */
8825255736Sdavidchstatic void
8826255736Sdavidchbxe_intr_legacy(void *xsc)
8827255736Sdavidch{
8828255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8829255736Sdavidch    struct bxe_fastpath *fp;
8830255736Sdavidch    uint16_t status, mask;
8831255736Sdavidch    int i;
8832255736Sdavidch
8833255736Sdavidch    BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8834255736Sdavidch
8835255736Sdavidch    /*
8836255736Sdavidch     * 0 for ustorm, 1 for cstorm
8837255736Sdavidch     * the bits returned from ack_int() are 0-15
8838255736Sdavidch     * bit 0 = attention status block
8839255736Sdavidch     * bit 1 = fast path status block
8840255736Sdavidch     * a mask of 0x2 or more = tx/rx event
8841255736Sdavidch     * a mask of 1 = slow path event
8842255736Sdavidch     */
8843255736Sdavidch
8844255736Sdavidch    status = bxe_ack_int(sc);
8845255736Sdavidch
8846255736Sdavidch    /* the interrupt is not for us */
8847255736Sdavidch    if (__predict_false(status == 0)) {
8848255736Sdavidch        BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8849255736Sdavidch        return;
8850255736Sdavidch    }
8851255736Sdavidch
8852255736Sdavidch    BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8853255736Sdavidch
8854255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
8855255736Sdavidch        fp = &sc->fp[i];
8856255736Sdavidch        mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8857255736Sdavidch        if (status & mask) {
8858255736Sdavidch            /* acknowledge and disable further fastpath interrupts */
8859255736Sdavidch            bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8860255736Sdavidch            bxe_task_fp(fp);
8861255736Sdavidch            status &= ~mask;
8862255736Sdavidch        }
8863255736Sdavidch    }
8864255736Sdavidch
8865255736Sdavidch    if (__predict_false(status & 0x1)) {
8866255736Sdavidch        /* acknowledge and disable further slowpath interrupts */
8867255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8868255736Sdavidch
8869255736Sdavidch        /* schedule slowpath handler */
8870296272Sjhb        taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8871255736Sdavidch
8872255736Sdavidch        status &= ~0x1;
8873255736Sdavidch    }
8874255736Sdavidch
8875255736Sdavidch    if (__predict_false(status)) {
8876255736Sdavidch        BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8877255736Sdavidch    }
8878255736Sdavidch}
8879255736Sdavidch
8880255736Sdavidch/* slowpath interrupt entry point */
8881255736Sdavidchstatic void
8882255736Sdavidchbxe_intr_sp(void *xsc)
8883255736Sdavidch{
8884255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8885255736Sdavidch
8886255736Sdavidch    BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8887255736Sdavidch
8888255736Sdavidch    /* acknowledge and disable further slowpath interrupts */
8889255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8890255736Sdavidch
8891255736Sdavidch    /* schedule slowpath handler */
8892296272Sjhb    taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8893255736Sdavidch}
8894255736Sdavidch
8895255736Sdavidch/* fastpath interrupt entry point */
8896255736Sdavidchstatic void
8897255736Sdavidchbxe_intr_fp(void *xfp)
8898255736Sdavidch{
8899255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8900255736Sdavidch    struct bxe_softc *sc = fp->sc;
8901255736Sdavidch
8902255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8903255736Sdavidch
8904255736Sdavidch    BLOGD(sc, DBG_INTR,
8905255736Sdavidch          "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8906255736Sdavidch          curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8907255736Sdavidch
8908255736Sdavidch    /* acknowledge and disable further fastpath interrupts */
8909255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8910255736Sdavidch
8911255736Sdavidch    bxe_task_fp(fp);
8912255736Sdavidch}
8913255736Sdavidch
8914255736Sdavidch/* Release all interrupts allocated by the driver. */
8915255736Sdavidchstatic void
8916255736Sdavidchbxe_interrupt_free(struct bxe_softc *sc)
8917255736Sdavidch{
8918255736Sdavidch    int i;
8919255736Sdavidch
8920255736Sdavidch    switch (sc->interrupt_mode) {
8921255736Sdavidch    case INTR_MODE_INTX:
8922255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8923255736Sdavidch        if (sc->intr[0].resource != NULL) {
8924255736Sdavidch            bus_release_resource(sc->dev,
8925255736Sdavidch                                 SYS_RES_IRQ,
8926255736Sdavidch                                 sc->intr[0].rid,
8927255736Sdavidch                                 sc->intr[0].resource);
8928255736Sdavidch        }
8929255736Sdavidch        break;
8930255736Sdavidch    case INTR_MODE_MSI:
8931255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8932255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
8933255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8934255736Sdavidch                bus_release_resource(sc->dev,
8935255736Sdavidch                                     SYS_RES_IRQ,
8936255736Sdavidch                                     sc->intr[i].rid,
8937255736Sdavidch                                     sc->intr[i].resource);
8938255736Sdavidch            }
8939255736Sdavidch        }
8940255736Sdavidch        pci_release_msi(sc->dev);
8941255736Sdavidch        break;
8942255736Sdavidch    case INTR_MODE_MSIX:
8943255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8944255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8945255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8946255736Sdavidch                bus_release_resource(sc->dev,
8947255736Sdavidch                                     SYS_RES_IRQ,
8948255736Sdavidch                                     sc->intr[i].rid,
8949255736Sdavidch                                     sc->intr[i].resource);
8950255736Sdavidch            }
8951255736Sdavidch        }
8952255736Sdavidch        pci_release_msi(sc->dev);
8953255736Sdavidch        break;
8954255736Sdavidch    default:
8955255736Sdavidch        /* nothing to do as initial allocation failed */
8956255736Sdavidch        break;
8957255736Sdavidch    }
8958255736Sdavidch}
8959255736Sdavidch
8960255736Sdavidch/*
8961255736Sdavidch * This function determines and allocates the appropriate
8962255736Sdavidch * interrupt based on system capabilites and user request.
8963255736Sdavidch *
8964255736Sdavidch * The user may force a particular interrupt mode, specify
8965255736Sdavidch * the number of receive queues, specify the method for
8966255736Sdavidch * distribuitng received frames to receive queues, or use
8967255736Sdavidch * the default settings which will automatically select the
8968255736Sdavidch * best supported combination.  In addition, the OS may or
8969255736Sdavidch * may not support certain combinations of these settings.
8970255736Sdavidch * This routine attempts to reconcile the settings requested
8971255736Sdavidch * by the user with the capabilites available from the system
8972255736Sdavidch * to select the optimal combination of features.
8973255736Sdavidch *
8974255736Sdavidch * Returns:
8975255736Sdavidch *   0 = Success, !0 = Failure.
8976255736Sdavidch */
8977255736Sdavidchstatic int
8978255736Sdavidchbxe_interrupt_alloc(struct bxe_softc *sc)
8979255736Sdavidch{
8980255736Sdavidch    int msix_count = 0;
8981255736Sdavidch    int msi_count = 0;
8982255736Sdavidch    int num_requested = 0;
8983255736Sdavidch    int num_allocated = 0;
8984255736Sdavidch    int rid, i, j;
8985255736Sdavidch    int rc;
8986255736Sdavidch
8987255736Sdavidch    /* get the number of available MSI/MSI-X interrupts from the OS */
8988255736Sdavidch    if (sc->interrupt_mode > 0) {
8989255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8990255736Sdavidch            msix_count = pci_msix_count(sc->dev);
8991255736Sdavidch        }
8992255736Sdavidch
8993255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
8994255736Sdavidch            msi_count = pci_msi_count(sc->dev);
8995255736Sdavidch        }
8996255736Sdavidch
8997255736Sdavidch        BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
8998255736Sdavidch              msi_count, msix_count);
8999255736Sdavidch    }
9000255736Sdavidch
9001255736Sdavidch    do { /* try allocating MSI-X interrupt resources (at least 2) */
9002255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
9003255736Sdavidch            break;
9004255736Sdavidch        }
9005255736Sdavidch
9006255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
9007255736Sdavidch            (msix_count < 2)) {
9008255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9009255736Sdavidch            break;
9010255736Sdavidch        }
9011255736Sdavidch
9012255736Sdavidch        /* ask for the necessary number of MSI-X vectors */
9013255736Sdavidch        num_requested = min((sc->num_queues + 1), msix_count);
9014255736Sdavidch
9015255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
9016255736Sdavidch
9017255736Sdavidch        num_allocated = num_requested;
9018255736Sdavidch        if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
9019255736Sdavidch            BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
9020255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9021255736Sdavidch            break;
9022255736Sdavidch        }
9023255736Sdavidch
9024255736Sdavidch        if (num_allocated < 2) { /* possible? */
9025255736Sdavidch            BLOGE(sc, "MSI-X allocation less than 2!\n");
9026255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9027255736Sdavidch            pci_release_msi(sc->dev);
9028255736Sdavidch            break;
9029255736Sdavidch        }
9030255736Sdavidch
9031255736Sdavidch        BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
9032255736Sdavidch              num_requested, num_allocated);
9033255736Sdavidch
9034255736Sdavidch        /* best effort so use the number of vectors allocated to us */
9035255736Sdavidch        sc->intr_count = num_allocated;
9036255736Sdavidch        sc->num_queues = num_allocated - 1;
9037255736Sdavidch
9038255736Sdavidch        rid = 1; /* initial resource identifier */
9039255736Sdavidch
9040255736Sdavidch        /* allocate the MSI-X vectors */
9041255736Sdavidch        for (i = 0; i < num_allocated; i++) {
9042255736Sdavidch            sc->intr[i].rid = (rid + i);
9043255736Sdavidch
9044255736Sdavidch            if ((sc->intr[i].resource =
9045255736Sdavidch                 bus_alloc_resource_any(sc->dev,
9046255736Sdavidch                                        SYS_RES_IRQ,
9047255736Sdavidch                                        &sc->intr[i].rid,
9048255736Sdavidch                                        RF_ACTIVE)) == NULL) {
9049255736Sdavidch                BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
9050255736Sdavidch                      i, (rid + i));
9051255736Sdavidch
9052255736Sdavidch                for (j = (i - 1); j >= 0; j--) {
9053255736Sdavidch                    bus_release_resource(sc->dev,
9054255736Sdavidch                                         SYS_RES_IRQ,
9055255736Sdavidch                                         sc->intr[j].rid,
9056255736Sdavidch                                         sc->intr[j].resource);
9057255736Sdavidch                }
9058255736Sdavidch
9059255736Sdavidch                sc->intr_count = 0;
9060255736Sdavidch                sc->num_queues = 0;
9061255736Sdavidch                sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9062255736Sdavidch                pci_release_msi(sc->dev);
9063255736Sdavidch                break;
9064255736Sdavidch            }
9065255736Sdavidch
9066255736Sdavidch            BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9067255736Sdavidch        }
9068255736Sdavidch    } while (0);
9069255736Sdavidch
9070255736Sdavidch    do { /* try allocating MSI vector resources (at least 2) */
9071255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSI) {
9072255736Sdavidch            break;
9073255736Sdavidch        }
9074255736Sdavidch
9075255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9076262999Sedavis            (msi_count < 1)) {
9077255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9078255736Sdavidch            break;
9079255736Sdavidch        }
9080255736Sdavidch
9081262999Sedavis        /* ask for a single MSI vector */
9082262999Sedavis        num_requested = 1;
9083255736Sdavidch
9084255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9085255736Sdavidch
9086255736Sdavidch        num_allocated = num_requested;
9087255736Sdavidch        if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9088255736Sdavidch            BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9089255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9090255736Sdavidch            break;
9091255736Sdavidch        }
9092255736Sdavidch
9093262999Sedavis        if (num_allocated != 1) { /* possible? */
9094262999Sedavis            BLOGE(sc, "MSI allocation is not 1!\n");
9095255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9096255736Sdavidch            pci_release_msi(sc->dev);
9097255736Sdavidch            break;
9098255736Sdavidch        }
9099255736Sdavidch
9100255736Sdavidch        BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9101255736Sdavidch              num_requested, num_allocated);
9102255736Sdavidch
9103255736Sdavidch        /* best effort so use the number of vectors allocated to us */
9104255736Sdavidch        sc->intr_count = num_allocated;
9105262999Sedavis        sc->num_queues = num_allocated;
9106255736Sdavidch
9107255736Sdavidch        rid = 1; /* initial resource identifier */
9108255736Sdavidch
9109262999Sedavis        sc->intr[0].rid = rid;
9110255736Sdavidch
9111262999Sedavis        if ((sc->intr[0].resource =
9112262999Sedavis             bus_alloc_resource_any(sc->dev,
9113262999Sedavis                                    SYS_RES_IRQ,
9114262999Sedavis                                    &sc->intr[0].rid,
9115262999Sedavis                                    RF_ACTIVE)) == NULL) {
9116262999Sedavis            BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9117262999Sedavis            sc->intr_count = 0;
9118262999Sedavis            sc->num_queues = 0;
9119262999Sedavis            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9120262999Sedavis            pci_release_msi(sc->dev);
9121262999Sedavis            break;
9122262999Sedavis        }
9123255736Sdavidch
9124262999Sedavis        BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9125255736Sdavidch    } while (0);
9126255736Sdavidch
9127255736Sdavidch    do { /* try allocating INTx vector resources */
9128255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_INTX) {
9129255736Sdavidch            break;
9130255736Sdavidch        }
9131255736Sdavidch
9132255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9133255736Sdavidch
9134255736Sdavidch        /* only one vector for INTx */
9135255736Sdavidch        sc->intr_count = 1;
9136255736Sdavidch        sc->num_queues = 1;
9137255736Sdavidch
9138255736Sdavidch        rid = 0; /* initial resource identifier */
9139255736Sdavidch
9140255736Sdavidch        sc->intr[0].rid = rid;
9141255736Sdavidch
9142255736Sdavidch        if ((sc->intr[0].resource =
9143255736Sdavidch             bus_alloc_resource_any(sc->dev,
9144255736Sdavidch                                    SYS_RES_IRQ,
9145255736Sdavidch                                    &sc->intr[0].rid,
9146255736Sdavidch                                    (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9147255736Sdavidch            BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9148255736Sdavidch            sc->intr_count = 0;
9149255736Sdavidch            sc->num_queues = 0;
9150255736Sdavidch            sc->interrupt_mode = -1; /* Failed! */
9151255736Sdavidch            break;
9152255736Sdavidch        }
9153255736Sdavidch
9154255736Sdavidch        BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9155255736Sdavidch    } while (0);
9156255736Sdavidch
9157255736Sdavidch    if (sc->interrupt_mode == -1) {
9158255736Sdavidch        BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9159255736Sdavidch        rc = 1;
9160255736Sdavidch    } else {
9161255736Sdavidch        BLOGD(sc, DBG_LOAD,
9162255736Sdavidch              "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9163255736Sdavidch              sc->interrupt_mode, sc->num_queues);
9164255736Sdavidch        rc = 0;
9165255736Sdavidch    }
9166255736Sdavidch
9167255736Sdavidch    return (rc);
9168255736Sdavidch}
9169255736Sdavidch
9170255736Sdavidchstatic void
9171255736Sdavidchbxe_interrupt_detach(struct bxe_softc *sc)
9172255736Sdavidch{
9173255736Sdavidch    struct bxe_fastpath *fp;
9174255736Sdavidch    int i;
9175255736Sdavidch
9176255736Sdavidch    /* release interrupt resources */
9177255736Sdavidch    for (i = 0; i < sc->intr_count; i++) {
9178255736Sdavidch        if (sc->intr[i].resource && sc->intr[i].tag) {
9179255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9180255736Sdavidch            bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9181255736Sdavidch        }
9182255736Sdavidch    }
9183255736Sdavidch
9184255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9185255736Sdavidch        fp = &sc->fp[i];
9186255736Sdavidch        if (fp->tq) {
9187255736Sdavidch            taskqueue_drain(fp->tq, &fp->tq_task);
9188307972Sdavidcs            taskqueue_drain(fp->tq, &fp->tx_task);
9189307972Sdavidcs            while (taskqueue_cancel_timeout(fp->tq, &fp->tx_timeout_task,
9190307972Sdavidcs                NULL))
9191307972Sdavidcs                taskqueue_drain_timeout(fp->tq, &fp->tx_timeout_task);
9192255736Sdavidch        }
9193337510Sdavidcs
9194337510Sdavidcs        for (i = 0; i < sc->num_queues; i++) {
9195337510Sdavidcs            fp = &sc->fp[i];
9196337510Sdavidcs            if (fp->tq != NULL) {
9197337510Sdavidcs                taskqueue_free(fp->tq);
9198337510Sdavidcs                fp->tq = NULL;
9199337510Sdavidcs            }
9200337510Sdavidcs        }
9201255736Sdavidch    }
9202255736Sdavidch
9203255736Sdavidch    if (sc->sp_tq) {
9204255736Sdavidch        taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9205255736Sdavidch        taskqueue_free(sc->sp_tq);
9206255736Sdavidch        sc->sp_tq = NULL;
9207255736Sdavidch    }
9208255736Sdavidch}
9209255736Sdavidch
9210255736Sdavidch/*
9211255736Sdavidch * Enables interrupts and attach to the ISR.
9212255736Sdavidch *
9213255736Sdavidch * When using multiple MSI/MSI-X vectors the first vector
9214255736Sdavidch * is used for slowpath operations while all remaining
9215255736Sdavidch * vectors are used for fastpath operations.  If only a
9216255736Sdavidch * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9217255736Sdavidch * ISR must look for both slowpath and fastpath completions.
9218255736Sdavidch */
9219255736Sdavidchstatic int
9220255736Sdavidchbxe_interrupt_attach(struct bxe_softc *sc)
9221255736Sdavidch{
9222255736Sdavidch    struct bxe_fastpath *fp;
9223255736Sdavidch    int rc = 0;
9224255736Sdavidch    int i;
9225255736Sdavidch
9226255736Sdavidch    snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9227255736Sdavidch             "bxe%d_sp_tq", sc->unit);
9228255736Sdavidch    TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9229307972Sdavidcs    sc->sp_tq = taskqueue_create(sc->sp_tq_name, M_NOWAIT,
9230307972Sdavidcs                                 taskqueue_thread_enqueue,
9231307972Sdavidcs                                 &sc->sp_tq);
9232255736Sdavidch    taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9233255736Sdavidch                            "%s", sc->sp_tq_name);
9234255736Sdavidch
9235255736Sdavidch
9236255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9237255736Sdavidch        fp = &sc->fp[i];
9238255736Sdavidch        snprintf(fp->tq_name, sizeof(fp->tq_name),
9239255736Sdavidch                 "bxe%d_fp%d_tq", sc->unit, i);
9240255736Sdavidch        TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9241307972Sdavidcs        TASK_INIT(&fp->tx_task, 0, bxe_tx_mq_start_deferred, fp);
9242307972Sdavidcs        fp->tq = taskqueue_create(fp->tq_name, M_NOWAIT,
9243307972Sdavidcs                                  taskqueue_thread_enqueue,
9244307972Sdavidcs                                  &fp->tq);
9245307972Sdavidcs        TIMEOUT_TASK_INIT(fp->tq, &fp->tx_timeout_task, 0,
9246307972Sdavidcs                          bxe_tx_mq_start_deferred, fp);
9247255736Sdavidch        taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9248255736Sdavidch                                "%s", fp->tq_name);
9249255736Sdavidch    }
9250255736Sdavidch
9251255736Sdavidch    /* setup interrupt handlers */
9252255736Sdavidch    if (sc->interrupt_mode == INTR_MODE_MSIX) {
9253255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9254255736Sdavidch
9255255736Sdavidch        /*
9256255736Sdavidch         * Setup the interrupt handler. Note that we pass the driver instance
9257255736Sdavidch         * to the interrupt handler for the slowpath.
9258255736Sdavidch         */
9259255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9260255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9261255736Sdavidch                                 NULL, bxe_intr_sp, sc,
9262255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9263255736Sdavidch            BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9264255736Sdavidch            goto bxe_interrupt_attach_exit;
9265255736Sdavidch        }
9266255736Sdavidch
9267255736Sdavidch        bus_describe_intr(sc->dev, sc->intr[0].resource,
9268255736Sdavidch                          sc->intr[0].tag, "sp");
9269255736Sdavidch
9270255736Sdavidch        /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9271255736Sdavidch
9272255736Sdavidch        /* initialize the fastpath vectors (note the first was used for sp) */
9273255736Sdavidch        for (i = 0; i < sc->num_queues; i++) {
9274255736Sdavidch            fp = &sc->fp[i];
9275255736Sdavidch            BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9276255736Sdavidch
9277255736Sdavidch            /*
9278255736Sdavidch             * Setup the interrupt handler. Note that we pass the
9279255736Sdavidch             * fastpath context to the interrupt handler in this
9280255736Sdavidch             * case.
9281255736Sdavidch             */
9282255736Sdavidch            if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9283255736Sdavidch                                     (INTR_TYPE_NET | INTR_MPSAFE),
9284255736Sdavidch                                     NULL, bxe_intr_fp, fp,
9285255736Sdavidch                                     &sc->intr[i + 1].tag)) != 0) {
9286255736Sdavidch                BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9287255736Sdavidch                      (i + 1), rc);
9288255736Sdavidch                goto bxe_interrupt_attach_exit;
9289255736Sdavidch            }
9290255736Sdavidch
9291255736Sdavidch            bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9292255736Sdavidch                              sc->intr[i + 1].tag, "fp%02d", i);
9293255736Sdavidch
9294255736Sdavidch            /* bind the fastpath instance to a cpu */
9295255736Sdavidch            if (sc->num_queues > 1) {
9296255736Sdavidch                bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9297255736Sdavidch            }
9298255736Sdavidch
9299255736Sdavidch            fp->state = BXE_FP_STATE_IRQ;
9300255736Sdavidch        }
9301255736Sdavidch    } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9302262999Sedavis        BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9303255736Sdavidch
9304255736Sdavidch        /*
9305262999Sedavis         * Setup the interrupt handler. Note that we pass the
9306262999Sedavis         * driver instance to the interrupt handler which
9307262999Sedavis         * will handle both the slowpath and fastpath.
9308255736Sdavidch         */
9309255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9310255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9311262999Sedavis                                 NULL, bxe_intr_legacy, sc,
9312255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9313255736Sdavidch            BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9314255736Sdavidch            goto bxe_interrupt_attach_exit;
9315255736Sdavidch        }
9316255736Sdavidch
9317255736Sdavidch    } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9318255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9319255736Sdavidch
9320255736Sdavidch        /*
9321255736Sdavidch         * Setup the interrupt handler. Note that we pass the
9322255736Sdavidch         * driver instance to the interrupt handler which
9323255736Sdavidch         * will handle both the slowpath and fastpath.
9324255736Sdavidch         */
9325255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9326255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9327255736Sdavidch                                 NULL, bxe_intr_legacy, sc,
9328255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9329255736Sdavidch            BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9330255736Sdavidch            goto bxe_interrupt_attach_exit;
9331255736Sdavidch        }
9332255736Sdavidch    }
9333255736Sdavidch
9334255736Sdavidchbxe_interrupt_attach_exit:
9335255736Sdavidch
9336255736Sdavidch    return (rc);
9337255736Sdavidch}
9338255736Sdavidch
9339255736Sdavidchstatic int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9340255736Sdavidchstatic int  bxe_init_hw_common(struct bxe_softc *sc);
9341255736Sdavidchstatic int  bxe_init_hw_port(struct bxe_softc *sc);
9342255736Sdavidchstatic int  bxe_init_hw_func(struct bxe_softc *sc);
9343255736Sdavidchstatic void bxe_reset_common(struct bxe_softc *sc);
9344255736Sdavidchstatic void bxe_reset_port(struct bxe_softc *sc);
9345255736Sdavidchstatic void bxe_reset_func(struct bxe_softc *sc);
9346255736Sdavidchstatic int  bxe_gunzip_init(struct bxe_softc *sc);
9347255736Sdavidchstatic void bxe_gunzip_end(struct bxe_softc *sc);
9348255736Sdavidchstatic int  bxe_init_firmware(struct bxe_softc *sc);
9349255736Sdavidchstatic void bxe_release_firmware(struct bxe_softc *sc);
9350255736Sdavidch
9351255736Sdavidchstatic struct
9352255736Sdavidchecore_func_sp_drv_ops bxe_func_sp_drv = {
9353255736Sdavidch    .init_hw_cmn_chip = bxe_init_hw_common_chip,
9354255736Sdavidch    .init_hw_cmn      = bxe_init_hw_common,
9355255736Sdavidch    .init_hw_port     = bxe_init_hw_port,
9356255736Sdavidch    .init_hw_func     = bxe_init_hw_func,
9357255736Sdavidch
9358255736Sdavidch    .reset_hw_cmn     = bxe_reset_common,
9359255736Sdavidch    .reset_hw_port    = bxe_reset_port,
9360255736Sdavidch    .reset_hw_func    = bxe_reset_func,
9361255736Sdavidch
9362255736Sdavidch    .gunzip_init      = bxe_gunzip_init,
9363255736Sdavidch    .gunzip_end       = bxe_gunzip_end,
9364255736Sdavidch
9365255736Sdavidch    .init_fw          = bxe_init_firmware,
9366255736Sdavidch    .release_fw       = bxe_release_firmware,
9367255736Sdavidch};
9368255736Sdavidch
9369255736Sdavidchstatic void
9370255736Sdavidchbxe_init_func_obj(struct bxe_softc *sc)
9371255736Sdavidch{
9372255736Sdavidch    sc->dmae_ready = 0;
9373255736Sdavidch
9374255736Sdavidch    ecore_init_func_obj(sc,
9375255736Sdavidch                        &sc->func_obj,
9376255736Sdavidch                        BXE_SP(sc, func_rdata),
9377255736Sdavidch                        BXE_SP_MAPPING(sc, func_rdata),
9378255736Sdavidch                        BXE_SP(sc, func_afex_rdata),
9379255736Sdavidch                        BXE_SP_MAPPING(sc, func_afex_rdata),
9380255736Sdavidch                        &bxe_func_sp_drv);
9381255736Sdavidch}
9382255736Sdavidch
9383255736Sdavidchstatic int
9384255736Sdavidchbxe_init_hw(struct bxe_softc *sc,
9385255736Sdavidch            uint32_t         load_code)
9386255736Sdavidch{
9387255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
9388255736Sdavidch    int rc;
9389255736Sdavidch
9390255736Sdavidch    /* prepare the parameters for function state transitions */
9391255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9392255736Sdavidch
9393255736Sdavidch    func_params.f_obj = &sc->func_obj;
9394255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_INIT;
9395255736Sdavidch
9396255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
9397255736Sdavidch
9398255736Sdavidch    /*
9399255736Sdavidch     * Via a plethora of function pointers, we will eventually reach
9400255736Sdavidch     * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9401255736Sdavidch     */
9402255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
9403255736Sdavidch
9404255736Sdavidch    return (rc);
9405255736Sdavidch}
9406255736Sdavidch
9407255736Sdavidchstatic void
9408255736Sdavidchbxe_fill(struct bxe_softc *sc,
9409255736Sdavidch         uint32_t         addr,
9410255736Sdavidch         int              fill,
9411255736Sdavidch         uint32_t         len)
9412255736Sdavidch{
9413255736Sdavidch    uint32_t i;
9414255736Sdavidch
9415255736Sdavidch    if (!(len % 4) && !(addr % 4)) {
9416255736Sdavidch        for (i = 0; i < len; i += 4) {
9417255736Sdavidch            REG_WR(sc, (addr + i), fill);
9418255736Sdavidch        }
9419255736Sdavidch    } else {
9420255736Sdavidch        for (i = 0; i < len; i++) {
9421255736Sdavidch            REG_WR8(sc, (addr + i), fill);
9422255736Sdavidch        }
9423255736Sdavidch    }
9424255736Sdavidch}
9425255736Sdavidch
9426255736Sdavidch/* writes FP SP data to FW - data_size in dwords */
9427255736Sdavidchstatic void
9428255736Sdavidchbxe_wr_fp_sb_data(struct bxe_softc *sc,
9429255736Sdavidch                  int              fw_sb_id,
9430255736Sdavidch                  uint32_t         *sb_data_p,
9431255736Sdavidch                  uint32_t         data_size)
9432255736Sdavidch{
9433255736Sdavidch    int index;
9434255736Sdavidch
9435255736Sdavidch    for (index = 0; index < data_size; index++) {
9436255736Sdavidch        REG_WR(sc,
9437255736Sdavidch               (BAR_CSTRORM_INTMEM +
9438255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9439255736Sdavidch                (sizeof(uint32_t) * index)),
9440255736Sdavidch               *(sb_data_p + index));
9441255736Sdavidch    }
9442255736Sdavidch}
9443255736Sdavidch
9444255736Sdavidchstatic void
9445255736Sdavidchbxe_zero_fp_sb(struct bxe_softc *sc,
9446255736Sdavidch               int              fw_sb_id)
9447255736Sdavidch{
9448255736Sdavidch    struct hc_status_block_data_e2 sb_data_e2;
9449255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9450255736Sdavidch    uint32_t *sb_data_p;
9451255736Sdavidch    uint32_t data_size = 0;
9452255736Sdavidch
9453255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9454255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9455255736Sdavidch        sb_data_e2.common.state = SB_DISABLED;
9456255736Sdavidch        sb_data_e2.common.p_func.vf_valid = FALSE;
9457255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9458255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9459255736Sdavidch                     sizeof(uint32_t));
9460255736Sdavidch    } else {
9461255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9462255736Sdavidch        sb_data_e1x.common.state = SB_DISABLED;
9463255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9464255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9465255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9466255736Sdavidch                     sizeof(uint32_t));
9467255736Sdavidch    }
9468255736Sdavidch
9469255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9470255736Sdavidch
9471255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9472255736Sdavidch             0, CSTORM_STATUS_BLOCK_SIZE);
9473255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9474255736Sdavidch             0, CSTORM_SYNC_BLOCK_SIZE);
9475255736Sdavidch}
9476255736Sdavidch
9477255736Sdavidchstatic void
9478255736Sdavidchbxe_wr_sp_sb_data(struct bxe_softc               *sc,
9479255736Sdavidch                  struct hc_sp_status_block_data *sp_sb_data)
9480255736Sdavidch{
9481255736Sdavidch    int i;
9482255736Sdavidch
9483255736Sdavidch    for (i = 0;
9484255736Sdavidch         i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9485255736Sdavidch         i++) {
9486255736Sdavidch        REG_WR(sc,
9487255736Sdavidch               (BAR_CSTRORM_INTMEM +
9488255736Sdavidch                CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9489255736Sdavidch                (i * sizeof(uint32_t))),
9490255736Sdavidch               *((uint32_t *)sp_sb_data + i));
9491255736Sdavidch    }
9492255736Sdavidch}
9493255736Sdavidch
9494255736Sdavidchstatic void
9495255736Sdavidchbxe_zero_sp_sb(struct bxe_softc *sc)
9496255736Sdavidch{
9497255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9498255736Sdavidch
9499255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9500255736Sdavidch
9501255736Sdavidch    sp_sb_data.state           = SB_DISABLED;
9502255736Sdavidch    sp_sb_data.p_func.vf_valid = FALSE;
9503255736Sdavidch
9504255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9505255736Sdavidch
9506255736Sdavidch    bxe_fill(sc,
9507255736Sdavidch             (BAR_CSTRORM_INTMEM +
9508255736Sdavidch              CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9509255736Sdavidch              0, CSTORM_SP_STATUS_BLOCK_SIZE);
9510255736Sdavidch    bxe_fill(sc,
9511255736Sdavidch             (BAR_CSTRORM_INTMEM +
9512255736Sdavidch              CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9513255736Sdavidch              0, CSTORM_SP_SYNC_BLOCK_SIZE);
9514255736Sdavidch}
9515255736Sdavidch
9516255736Sdavidchstatic void
9517255736Sdavidchbxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9518255736Sdavidch                             int                       igu_sb_id,
9519255736Sdavidch                             int                       igu_seg_id)
9520255736Sdavidch{
9521255736Sdavidch    hc_sm->igu_sb_id      = igu_sb_id;
9522255736Sdavidch    hc_sm->igu_seg_id     = igu_seg_id;
9523255736Sdavidch    hc_sm->timer_value    = 0xFF;
9524255736Sdavidch    hc_sm->time_to_expire = 0xFFFFFFFF;
9525255736Sdavidch}
9526255736Sdavidch
9527255736Sdavidchstatic void
9528255736Sdavidchbxe_map_sb_state_machines(struct hc_index_data *index_data)
9529255736Sdavidch{
9530255736Sdavidch    /* zero out state machine indices */
9531255736Sdavidch
9532255736Sdavidch    /* rx indices */
9533255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9534255736Sdavidch
9535255736Sdavidch    /* tx indices */
9536255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9537255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9538255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9539255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9540255736Sdavidch
9541255736Sdavidch    /* map indices */
9542255736Sdavidch
9543255736Sdavidch    /* rx indices */
9544255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9545255736Sdavidch        (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9546255736Sdavidch
9547255736Sdavidch    /* tx indices */
9548255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9549255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9550255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9551255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9552255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9553255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9554255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9555255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9556255736Sdavidch}
9557255736Sdavidch
9558255736Sdavidchstatic void
9559255736Sdavidchbxe_init_sb(struct bxe_softc *sc,
9560255736Sdavidch            bus_addr_t       busaddr,
9561255736Sdavidch            int              vfid,
9562255736Sdavidch            uint8_t          vf_valid,
9563255736Sdavidch            int              fw_sb_id,
9564255736Sdavidch            int              igu_sb_id)
9565255736Sdavidch{
9566255736Sdavidch    struct hc_status_block_data_e2  sb_data_e2;
9567255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9568255736Sdavidch    struct hc_status_block_sm       *hc_sm_p;
9569255736Sdavidch    uint32_t *sb_data_p;
9570255736Sdavidch    int igu_seg_id;
9571255736Sdavidch    int data_size;
9572255736Sdavidch
9573255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9574255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_NORM;
9575255736Sdavidch    } else {
9576255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_NORM;
9577255736Sdavidch    }
9578255736Sdavidch
9579255736Sdavidch    bxe_zero_fp_sb(sc, fw_sb_id);
9580255736Sdavidch
9581255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9582255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9583255736Sdavidch        sb_data_e2.common.state = SB_ENABLED;
9584255736Sdavidch        sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9585255736Sdavidch        sb_data_e2.common.p_func.vf_id = vfid;
9586255736Sdavidch        sb_data_e2.common.p_func.vf_valid = vf_valid;
9587255736Sdavidch        sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9588255736Sdavidch        sb_data_e2.common.same_igu_sb_1b = TRUE;
9589255736Sdavidch        sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9590255736Sdavidch        sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9591255736Sdavidch        hc_sm_p = sb_data_e2.common.state_machine;
9592255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9593255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9594255736Sdavidch                     sizeof(uint32_t));
9595255736Sdavidch        bxe_map_sb_state_machines(sb_data_e2.index_data);
9596255736Sdavidch    } else {
9597255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9598255736Sdavidch        sb_data_e1x.common.state = SB_ENABLED;
9599255736Sdavidch        sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9600255736Sdavidch        sb_data_e1x.common.p_func.vf_id = 0xff;
9601255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9602255736Sdavidch        sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9603255736Sdavidch        sb_data_e1x.common.same_igu_sb_1b = TRUE;
9604255736Sdavidch        sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9605255736Sdavidch        sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9606255736Sdavidch        hc_sm_p = sb_data_e1x.common.state_machine;
9607255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9608255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9609255736Sdavidch                     sizeof(uint32_t));
9610255736Sdavidch        bxe_map_sb_state_machines(sb_data_e1x.index_data);
9611255736Sdavidch    }
9612255736Sdavidch
9613255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9614255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9615255736Sdavidch
9616255736Sdavidch    BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9617255736Sdavidch
9618255736Sdavidch    /* write indices to HW - PCI guarantees endianity of regpairs */
9619255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9620255736Sdavidch}
9621255736Sdavidch
9622255736Sdavidchstatic inline uint8_t
9623255736Sdavidchbxe_fp_qzone_id(struct bxe_fastpath *fp)
9624255736Sdavidch{
9625255736Sdavidch    if (CHIP_IS_E1x(fp->sc)) {
9626255736Sdavidch        return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9627255736Sdavidch    } else {
9628255736Sdavidch        return (fp->cl_id);
9629255736Sdavidch    }
9630255736Sdavidch}
9631255736Sdavidch
9632255736Sdavidchstatic inline uint32_t
9633255736Sdavidchbxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9634255736Sdavidch                           struct bxe_fastpath *fp)
9635255736Sdavidch{
9636255736Sdavidch    uint32_t offset = BAR_USTRORM_INTMEM;
9637255736Sdavidch
9638255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9639255736Sdavidch        offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9640255736Sdavidch    } else {
9641255736Sdavidch        offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9642255736Sdavidch    }
9643255736Sdavidch
9644255736Sdavidch    return (offset);
9645255736Sdavidch}
9646255736Sdavidch
9647255736Sdavidchstatic void
9648255736Sdavidchbxe_init_eth_fp(struct bxe_softc *sc,
9649255736Sdavidch                int              idx)
9650255736Sdavidch{
9651255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[idx];
9652255736Sdavidch    uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9653255736Sdavidch    unsigned long q_type = 0;
9654255736Sdavidch    int cos;
9655255736Sdavidch
9656255736Sdavidch    fp->sc    = sc;
9657255736Sdavidch    fp->index = idx;
9658255736Sdavidch
9659255736Sdavidch    fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9660255736Sdavidch    fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9661255736Sdavidch
9662255736Sdavidch    fp->cl_id = (CHIP_IS_E1x(sc)) ?
9663255736Sdavidch                    (SC_L_ID(sc) + idx) :
9664255736Sdavidch                    /* want client ID same as IGU SB ID for non-E1 */
9665255736Sdavidch                    fp->igu_sb_id;
9666255736Sdavidch    fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9667255736Sdavidch
9668255736Sdavidch    /* setup sb indices */
9669255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9670255736Sdavidch        fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9671255736Sdavidch        fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9672255736Sdavidch    } else {
9673255736Sdavidch        fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9674255736Sdavidch        fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9675255736Sdavidch    }
9676255736Sdavidch
9677255736Sdavidch    /* init shortcut */
9678255736Sdavidch    fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9679255736Sdavidch
9680255736Sdavidch    fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9681255736Sdavidch
9682255736Sdavidch    /*
9683255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
9684255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9685255736Sdavidch     */
9686255736Sdavidch    for (cos = 0; cos < sc->max_cos; cos++) {
9687255736Sdavidch        cids[cos] = idx;
9688255736Sdavidch    }
9689255736Sdavidch    fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9690255736Sdavidch
9691255736Sdavidch    /* nothing more for a VF to do */
9692255736Sdavidch    if (IS_VF(sc)) {
9693255736Sdavidch        return;
9694255736Sdavidch    }
9695255736Sdavidch
9696255736Sdavidch    bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9697255736Sdavidch                fp->fw_sb_id, fp->igu_sb_id);
9698255736Sdavidch
9699255736Sdavidch    bxe_update_fp_sb_idx(fp);
9700255736Sdavidch
9701255736Sdavidch    /* Configure Queue State object */
9702255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9703255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9704255736Sdavidch
9705255736Sdavidch    ecore_init_queue_obj(sc,
9706255736Sdavidch                         &sc->sp_objs[idx].q_obj,
9707255736Sdavidch                         fp->cl_id,
9708255736Sdavidch                         cids,
9709255736Sdavidch                         sc->max_cos,
9710255736Sdavidch                         SC_FUNC(sc),
9711255736Sdavidch                         BXE_SP(sc, q_rdata),
9712255736Sdavidch                         BXE_SP_MAPPING(sc, q_rdata),
9713255736Sdavidch                         q_type);
9714255736Sdavidch
9715255736Sdavidch    /* configure classification DBs */
9716255736Sdavidch    ecore_init_mac_obj(sc,
9717255736Sdavidch                       &sc->sp_objs[idx].mac_obj,
9718255736Sdavidch                       fp->cl_id,
9719255736Sdavidch                       idx,
9720255736Sdavidch                       SC_FUNC(sc),
9721255736Sdavidch                       BXE_SP(sc, mac_rdata),
9722255736Sdavidch                       BXE_SP_MAPPING(sc, mac_rdata),
9723255736Sdavidch                       ECORE_FILTER_MAC_PENDING,
9724255736Sdavidch                       &sc->sp_state,
9725255736Sdavidch                       ECORE_OBJ_TYPE_RX_TX,
9726255736Sdavidch                       &sc->macs_pool);
9727255736Sdavidch
9728255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9729255736Sdavidch          idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9730255736Sdavidch}
9731255736Sdavidch
9732255736Sdavidchstatic inline void
9733255736Sdavidchbxe_update_rx_prod(struct bxe_softc    *sc,
9734255736Sdavidch                   struct bxe_fastpath *fp,
9735255736Sdavidch                   uint16_t            rx_bd_prod,
9736255736Sdavidch                   uint16_t            rx_cq_prod,
9737255736Sdavidch                   uint16_t            rx_sge_prod)
9738255736Sdavidch{
9739255736Sdavidch    struct ustorm_eth_rx_producers rx_prods = { 0 };
9740255736Sdavidch    uint32_t i;
9741255736Sdavidch
9742255736Sdavidch    /* update producers */
9743255736Sdavidch    rx_prods.bd_prod  = rx_bd_prod;
9744255736Sdavidch    rx_prods.cqe_prod = rx_cq_prod;
9745255736Sdavidch    rx_prods.sge_prod = rx_sge_prod;
9746255736Sdavidch
9747255736Sdavidch    /*
9748255736Sdavidch     * Make sure that the BD and SGE data is updated before updating the
9749255736Sdavidch     * producers since FW might read the BD/SGE right after the producer
9750255736Sdavidch     * is updated.
9751255736Sdavidch     * This is only applicable for weak-ordered memory model archs such
9752255736Sdavidch     * as IA-64. The following barrier is also mandatory since FW will
9753255736Sdavidch     * assumes BDs must have buffers.
9754255736Sdavidch     */
9755255736Sdavidch    wmb();
9756255736Sdavidch
9757255736Sdavidch    for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9758255736Sdavidch        REG_WR(sc,
9759255736Sdavidch               (fp->ustorm_rx_prods_offset + (i * 4)),
9760255736Sdavidch               ((uint32_t *)&rx_prods)[i]);
9761255736Sdavidch    }
9762255736Sdavidch
9763255736Sdavidch    wmb(); /* keep prod updates ordered */
9764255736Sdavidch
9765255736Sdavidch    BLOGD(sc, DBG_RX,
9766255736Sdavidch          "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9767255736Sdavidch          fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9768255736Sdavidch}
9769255736Sdavidch
9770255736Sdavidchstatic void
9771255736Sdavidchbxe_init_rx_rings(struct bxe_softc *sc)
9772255736Sdavidch{
9773255736Sdavidch    struct bxe_fastpath *fp;
9774255736Sdavidch    int i;
9775255736Sdavidch
9776255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9777255736Sdavidch        fp = &sc->fp[i];
9778255736Sdavidch
9779255736Sdavidch        fp->rx_bd_cons = 0;
9780255736Sdavidch
9781255736Sdavidch        /*
9782255736Sdavidch         * Activate the BD ring...
9783255736Sdavidch         * Warning, this will generate an interrupt (to the TSTORM)
9784255736Sdavidch         * so this can only be done after the chip is initialized
9785255736Sdavidch         */
9786255736Sdavidch        bxe_update_rx_prod(sc, fp,
9787255736Sdavidch                           fp->rx_bd_prod,
9788255736Sdavidch                           fp->rx_cq_prod,
9789255736Sdavidch                           fp->rx_sge_prod);
9790255736Sdavidch
9791255736Sdavidch        if (i != 0) {
9792255736Sdavidch            continue;
9793255736Sdavidch        }
9794255736Sdavidch
9795255736Sdavidch        if (CHIP_IS_E1(sc)) {
9796255736Sdavidch            REG_WR(sc,
9797255736Sdavidch                   (BAR_USTRORM_INTMEM +
9798255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9799255736Sdavidch                   U64_LO(fp->rcq_dma.paddr));
9800255736Sdavidch            REG_WR(sc,
9801255736Sdavidch                   (BAR_USTRORM_INTMEM +
9802255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9803255736Sdavidch                   U64_HI(fp->rcq_dma.paddr));
9804255736Sdavidch        }
9805255736Sdavidch    }
9806255736Sdavidch}
9807255736Sdavidch
9808255736Sdavidchstatic void
9809255736Sdavidchbxe_init_tx_ring_one(struct bxe_fastpath *fp)
9810255736Sdavidch{
9811296071Sdavidcs    SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9812255736Sdavidch    fp->tx_db.data.zero_fill1 = 0;
9813255736Sdavidch    fp->tx_db.data.prod = 0;
9814255736Sdavidch
9815255736Sdavidch    fp->tx_pkt_prod = 0;
9816255736Sdavidch    fp->tx_pkt_cons = 0;
9817255736Sdavidch    fp->tx_bd_prod = 0;
9818255736Sdavidch    fp->tx_bd_cons = 0;
9819255736Sdavidch    fp->eth_q_stats.tx_pkts = 0;
9820255736Sdavidch}
9821255736Sdavidch
9822255736Sdavidchstatic inline void
9823255736Sdavidchbxe_init_tx_rings(struct bxe_softc *sc)
9824255736Sdavidch{
9825255736Sdavidch    int i;
9826255736Sdavidch
9827255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9828255736Sdavidch        bxe_init_tx_ring_one(&sc->fp[i]);
9829255736Sdavidch    }
9830255736Sdavidch}
9831255736Sdavidch
9832255736Sdavidchstatic void
9833255736Sdavidchbxe_init_def_sb(struct bxe_softc *sc)
9834255736Sdavidch{
9835255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
9836255736Sdavidch    bus_addr_t mapping = sc->def_sb_dma.paddr;
9837255736Sdavidch    int igu_sp_sb_index;
9838255736Sdavidch    int igu_seg_id;
9839255736Sdavidch    int port = SC_PORT(sc);
9840255736Sdavidch    int func = SC_FUNC(sc);
9841255736Sdavidch    int reg_offset, reg_offset_en5;
9842255736Sdavidch    uint64_t section;
9843255736Sdavidch    int index, sindex;
9844255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9845255736Sdavidch
9846255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9847255736Sdavidch
9848255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9849255736Sdavidch        igu_sp_sb_index = DEF_SB_IGU_ID;
9850255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_DEF;
9851255736Sdavidch    } else {
9852255736Sdavidch        igu_sp_sb_index = sc->igu_dsb_id;
9853255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_DEF;
9854255736Sdavidch    }
9855255736Sdavidch
9856255736Sdavidch    /* attentions */
9857255736Sdavidch    section = ((uint64_t)mapping +
9858255736Sdavidch               offsetof(struct host_sp_status_block, atten_status_block));
9859255736Sdavidch    def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9860255736Sdavidch    sc->attn_state = 0;
9861255736Sdavidch
9862255736Sdavidch    reg_offset = (port) ?
9863255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9864255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9865255736Sdavidch    reg_offset_en5 = (port) ?
9866255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9867255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9868255736Sdavidch
9869255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9870255736Sdavidch        /* take care of sig[0]..sig[4] */
9871255736Sdavidch        for (sindex = 0; sindex < 4; sindex++) {
9872255736Sdavidch            sc->attn_group[index].sig[sindex] =
9873255736Sdavidch                REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9874255736Sdavidch        }
9875255736Sdavidch
9876255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
9877255736Sdavidch            /*
9878255736Sdavidch             * enable5 is separate from the rest of the registers,
9879255736Sdavidch             * and the address skip is 4 and not 16 between the
9880255736Sdavidch             * different groups
9881255736Sdavidch             */
9882255736Sdavidch            sc->attn_group[index].sig[4] =
9883255736Sdavidch                REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9884255736Sdavidch        } else {
9885255736Sdavidch            sc->attn_group[index].sig[4] = 0;
9886255736Sdavidch        }
9887255736Sdavidch    }
9888255736Sdavidch
9889255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
9890255736Sdavidch        reg_offset = (port) ?
9891255736Sdavidch                         HC_REG_ATTN_MSG1_ADDR_L :
9892255736Sdavidch                         HC_REG_ATTN_MSG0_ADDR_L;
9893255736Sdavidch        REG_WR(sc, reg_offset, U64_LO(section));
9894255736Sdavidch        REG_WR(sc, (reg_offset + 4), U64_HI(section));
9895255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
9896255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9897255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9898255736Sdavidch    }
9899255736Sdavidch
9900255736Sdavidch    section = ((uint64_t)mapping +
9901255736Sdavidch               offsetof(struct host_sp_status_block, sp_sb));
9902255736Sdavidch
9903255736Sdavidch    bxe_zero_sp_sb(sc);
9904255736Sdavidch
9905255736Sdavidch    /* PCI guarantees endianity of regpair */
9906255736Sdavidch    sp_sb_data.state           = SB_ENABLED;
9907255736Sdavidch    sp_sb_data.host_sb_addr.lo = U64_LO(section);
9908255736Sdavidch    sp_sb_data.host_sb_addr.hi = U64_HI(section);
9909255736Sdavidch    sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9910255736Sdavidch    sp_sb_data.igu_seg_id      = igu_seg_id;
9911255736Sdavidch    sp_sb_data.p_func.pf_id    = func;
9912255736Sdavidch    sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9913255736Sdavidch    sp_sb_data.p_func.vf_id    = 0xff;
9914255736Sdavidch
9915255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9916255736Sdavidch
9917255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9918255736Sdavidch}
9919255736Sdavidch
9920255736Sdavidchstatic void
9921255736Sdavidchbxe_init_sp_ring(struct bxe_softc *sc)
9922255736Sdavidch{
9923255736Sdavidch    atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9924255736Sdavidch    sc->spq_prod_idx = 0;
9925255736Sdavidch    sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9926255736Sdavidch    sc->spq_prod_bd = sc->spq;
9927255736Sdavidch    sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9928255736Sdavidch}
9929255736Sdavidch
9930255736Sdavidchstatic void
9931255736Sdavidchbxe_init_eq_ring(struct bxe_softc *sc)
9932255736Sdavidch{
9933255736Sdavidch    union event_ring_elem *elem;
9934255736Sdavidch    int i;
9935255736Sdavidch
9936255736Sdavidch    for (i = 1; i <= NUM_EQ_PAGES; i++) {
9937255736Sdavidch        elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9938255736Sdavidch
9939255736Sdavidch        elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9940255736Sdavidch                                                 BCM_PAGE_SIZE *
9941255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9942255736Sdavidch        elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9943255736Sdavidch                                                 BCM_PAGE_SIZE *
9944255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9945255736Sdavidch    }
9946255736Sdavidch
9947255736Sdavidch    sc->eq_cons    = 0;
9948255736Sdavidch    sc->eq_prod    = NUM_EQ_DESC;
9949255736Sdavidch    sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9950255736Sdavidch
9951255736Sdavidch    atomic_store_rel_long(&sc->eq_spq_left,
9952255736Sdavidch                          (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9953255736Sdavidch                               NUM_EQ_DESC) - 1));
9954255736Sdavidch}
9955255736Sdavidch
9956255736Sdavidchstatic void
9957255736Sdavidchbxe_init_internal_common(struct bxe_softc *sc)
9958255736Sdavidch{
9959255736Sdavidch    int i;
9960255736Sdavidch
9961255736Sdavidch    /*
9962255736Sdavidch     * Zero this manually as its initialization is currently missing
9963255736Sdavidch     * in the initTool.
9964255736Sdavidch     */
9965255736Sdavidch    for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9966255736Sdavidch        REG_WR(sc,
9967255736Sdavidch               (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9968255736Sdavidch               0);
9969255736Sdavidch    }
9970255736Sdavidch
9971255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9972255736Sdavidch        REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9973255736Sdavidch                CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9974255736Sdavidch    }
9975255736Sdavidch}
9976255736Sdavidch
9977255736Sdavidchstatic void
9978255736Sdavidchbxe_init_internal(struct bxe_softc *sc,
9979255736Sdavidch                  uint32_t         load_code)
9980255736Sdavidch{
9981255736Sdavidch    switch (load_code) {
9982255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON:
9983255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9984255736Sdavidch        bxe_init_internal_common(sc);
9985255736Sdavidch        /* no break */
9986255736Sdavidch
9987255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_PORT:
9988255736Sdavidch        /* nothing to do */
9989255736Sdavidch        /* no break */
9990255736Sdavidch
9991255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_FUNCTION:
9992255736Sdavidch        /* internal memory per function is initialized inside bxe_pf_init */
9993255736Sdavidch        break;
9994255736Sdavidch
9995255736Sdavidch    default:
9996255736Sdavidch        BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
9997255736Sdavidch        break;
9998255736Sdavidch    }
9999255736Sdavidch}
10000255736Sdavidch
10001255736Sdavidchstatic void
10002255736Sdavidchstorm_memset_func_cfg(struct bxe_softc                         *sc,
10003255736Sdavidch                      struct tstorm_eth_function_common_config *tcfg,
10004255736Sdavidch                      uint16_t                                  abs_fid)
10005255736Sdavidch{
10006255736Sdavidch    uint32_t addr;
10007255736Sdavidch    size_t size;
10008255736Sdavidch
10009255736Sdavidch    addr = (BAR_TSTRORM_INTMEM +
10010255736Sdavidch            TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
10011255736Sdavidch    size = sizeof(struct tstorm_eth_function_common_config);
10012255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
10013255736Sdavidch}
10014255736Sdavidch
10015255736Sdavidchstatic void
10016255736Sdavidchbxe_func_init(struct bxe_softc            *sc,
10017255736Sdavidch              struct bxe_func_init_params *p)
10018255736Sdavidch{
10019255736Sdavidch    struct tstorm_eth_function_common_config tcfg = { 0 };
10020255736Sdavidch
10021255736Sdavidch    if (CHIP_IS_E1x(sc)) {
10022255736Sdavidch        storm_memset_func_cfg(sc, &tcfg, p->func_id);
10023255736Sdavidch    }
10024255736Sdavidch
10025255736Sdavidch    /* Enable the function in the FW */
10026255736Sdavidch    storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
10027255736Sdavidch    storm_memset_func_en(sc, p->func_id, 1);
10028255736Sdavidch
10029255736Sdavidch    /* spq */
10030255736Sdavidch    if (p->func_flgs & FUNC_FLG_SPQ) {
10031255736Sdavidch        storm_memset_spq_addr(sc, p->spq_map, p->func_id);
10032255736Sdavidch        REG_WR(sc,
10033255736Sdavidch               (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
10034255736Sdavidch               p->spq_prod);
10035255736Sdavidch    }
10036255736Sdavidch}
10037255736Sdavidch
10038255736Sdavidch/*
10039255736Sdavidch * Calculates the sum of vn_min_rates.
10040255736Sdavidch * It's needed for further normalizing of the min_rates.
10041255736Sdavidch * Returns:
10042255736Sdavidch *   sum of vn_min_rates.
10043255736Sdavidch *     or
10044255736Sdavidch *   0 - if all the min_rates are 0.
10045255736Sdavidch * In the later case fainess algorithm should be deactivated.
10046255736Sdavidch * If all min rates are not zero then those that are zeroes will be set to 1.
10047255736Sdavidch */
10048255736Sdavidchstatic void
10049255736Sdavidchbxe_calc_vn_min(struct bxe_softc       *sc,
10050255736Sdavidch                struct cmng_init_input *input)
10051255736Sdavidch{
10052255736Sdavidch    uint32_t vn_cfg;
10053255736Sdavidch    uint32_t vn_min_rate;
10054255736Sdavidch    int all_zero = 1;
10055255736Sdavidch    int vn;
10056255736Sdavidch
10057255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10058255736Sdavidch        vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10059255736Sdavidch        vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
10060255736Sdavidch                        FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
10061255736Sdavidch
10062255736Sdavidch        if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10063255736Sdavidch            /* skip hidden VNs */
10064255736Sdavidch            vn_min_rate = 0;
10065255736Sdavidch        } else if (!vn_min_rate) {
10066255736Sdavidch            /* If min rate is zero - set it to 100 */
10067255736Sdavidch            vn_min_rate = DEF_MIN_RATE;
10068255736Sdavidch        } else {
10069255736Sdavidch            all_zero = 0;
10070255736Sdavidch        }
10071255736Sdavidch
10072255736Sdavidch        input->vnic_min_rate[vn] = vn_min_rate;
10073255736Sdavidch    }
10074255736Sdavidch
10075255736Sdavidch    /* if ETS or all min rates are zeros - disable fairness */
10076255736Sdavidch    if (BXE_IS_ETS_ENABLED(sc)) {
10077255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10078255736Sdavidch        BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10079255736Sdavidch    } else if (all_zero) {
10080255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10081255736Sdavidch        BLOGD(sc, DBG_LOAD,
10082255736Sdavidch              "Fariness disabled (all MIN values are zeroes)\n");
10083255736Sdavidch    } else {
10084255736Sdavidch        input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10085255736Sdavidch    }
10086255736Sdavidch}
10087255736Sdavidch
10088255736Sdavidchstatic inline uint16_t
10089255736Sdavidchbxe_extract_max_cfg(struct bxe_softc *sc,
10090255736Sdavidch                    uint32_t         mf_cfg)
10091255736Sdavidch{
10092255736Sdavidch    uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10093255736Sdavidch                        FUNC_MF_CFG_MAX_BW_SHIFT);
10094255736Sdavidch
10095255736Sdavidch    if (!max_cfg) {
10096255736Sdavidch        BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10097255736Sdavidch        max_cfg = 100;
10098255736Sdavidch    }
10099255736Sdavidch
10100255736Sdavidch    return (max_cfg);
10101255736Sdavidch}
10102255736Sdavidch
10103255736Sdavidchstatic void
10104255736Sdavidchbxe_calc_vn_max(struct bxe_softc       *sc,
10105255736Sdavidch                int                    vn,
10106255736Sdavidch                struct cmng_init_input *input)
10107255736Sdavidch{
10108255736Sdavidch    uint16_t vn_max_rate;
10109255736Sdavidch    uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10110255736Sdavidch    uint32_t max_cfg;
10111255736Sdavidch
10112255736Sdavidch    if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10113255736Sdavidch        vn_max_rate = 0;
10114255736Sdavidch    } else {
10115255736Sdavidch        max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10116255736Sdavidch
10117255736Sdavidch        if (IS_MF_SI(sc)) {
10118255736Sdavidch            /* max_cfg in percents of linkspeed */
10119255736Sdavidch            vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10120255736Sdavidch        } else { /* SD modes */
10121255736Sdavidch            /* max_cfg is absolute in 100Mb units */
10122255736Sdavidch            vn_max_rate = (max_cfg * 100);
10123255736Sdavidch        }
10124255736Sdavidch    }
10125255736Sdavidch
10126255736Sdavidch    BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10127255736Sdavidch
10128255736Sdavidch    input->vnic_max_rate[vn] = vn_max_rate;
10129255736Sdavidch}
10130255736Sdavidch
10131255736Sdavidchstatic void
10132255736Sdavidchbxe_cmng_fns_init(struct bxe_softc *sc,
10133255736Sdavidch                  uint8_t          read_cfg,
10134255736Sdavidch                  uint8_t          cmng_type)
10135255736Sdavidch{
10136255736Sdavidch    struct cmng_init_input input;
10137255736Sdavidch    int vn;
10138255736Sdavidch
10139255736Sdavidch    memset(&input, 0, sizeof(struct cmng_init_input));
10140255736Sdavidch
10141255736Sdavidch    input.port_rate = sc->link_vars.line_speed;
10142255736Sdavidch
10143255736Sdavidch    if (cmng_type == CMNG_FNS_MINMAX) {
10144255736Sdavidch        /* read mf conf from shmem */
10145255736Sdavidch        if (read_cfg) {
10146255736Sdavidch            bxe_read_mf_cfg(sc);
10147255736Sdavidch        }
10148255736Sdavidch
10149255736Sdavidch        /* get VN min rate and enable fairness if not 0 */
10150255736Sdavidch        bxe_calc_vn_min(sc, &input);
10151255736Sdavidch
10152255736Sdavidch        /* get VN max rate */
10153255736Sdavidch        if (sc->port.pmf) {
10154255736Sdavidch            for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10155255736Sdavidch                bxe_calc_vn_max(sc, vn, &input);
10156255736Sdavidch            }
10157255736Sdavidch        }
10158255736Sdavidch
10159255736Sdavidch        /* always enable rate shaping and fairness */
10160255736Sdavidch        input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10161255736Sdavidch
10162255736Sdavidch        ecore_init_cmng(&input, &sc->cmng);
10163255736Sdavidch        return;
10164255736Sdavidch    }
10165255736Sdavidch
10166255736Sdavidch    /* rate shaping and fairness are disabled */
10167255736Sdavidch    BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10168255736Sdavidch}
10169255736Sdavidch
10170255736Sdavidchstatic int
10171255736Sdavidchbxe_get_cmng_fns_mode(struct bxe_softc *sc)
10172255736Sdavidch{
10173255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10174255736Sdavidch        return (CMNG_FNS_NONE);
10175255736Sdavidch    }
10176255736Sdavidch
10177255736Sdavidch    if (IS_MF(sc)) {
10178255736Sdavidch        return (CMNG_FNS_MINMAX);
10179255736Sdavidch    }
10180255736Sdavidch
10181255736Sdavidch    return (CMNG_FNS_NONE);
10182255736Sdavidch}
10183255736Sdavidch
10184255736Sdavidchstatic void
10185255736Sdavidchstorm_memset_cmng(struct bxe_softc *sc,
10186255736Sdavidch                  struct cmng_init *cmng,
10187255736Sdavidch                  uint8_t          port)
10188255736Sdavidch{
10189255736Sdavidch    int vn;
10190255736Sdavidch    int func;
10191255736Sdavidch    uint32_t addr;
10192255736Sdavidch    size_t size;
10193255736Sdavidch
10194255736Sdavidch    addr = (BAR_XSTRORM_INTMEM +
10195255736Sdavidch            XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10196255736Sdavidch    size = sizeof(struct cmng_struct_per_port);
10197255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10198255736Sdavidch
10199255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10200255736Sdavidch        func = func_by_vn(sc, vn);
10201255736Sdavidch
10202255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10203255736Sdavidch                XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10204255736Sdavidch        size = sizeof(struct rate_shaping_vars_per_vn);
10205255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10206255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10207255736Sdavidch
10208255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10209255736Sdavidch                XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10210255736Sdavidch        size = sizeof(struct fairness_vars_per_vn);
10211255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10212255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10213255736Sdavidch    }
10214255736Sdavidch}
10215255736Sdavidch
10216255736Sdavidchstatic void
10217255736Sdavidchbxe_pf_init(struct bxe_softc *sc)
10218255736Sdavidch{
10219255736Sdavidch    struct bxe_func_init_params func_init = { 0 };
10220255736Sdavidch    struct event_ring_data eq_data = { { 0 } };
10221255736Sdavidch    uint16_t flags;
10222255736Sdavidch
10223255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10224255736Sdavidch        /* reset IGU PF statistics: MSIX + ATTN */
10225255736Sdavidch        /* PF */
10226255736Sdavidch        REG_WR(sc,
10227255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10228255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10229255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10230255736Sdavidch               0);
10231255736Sdavidch        /* ATTN */
10232255736Sdavidch        REG_WR(sc,
10233255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10234255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10235255736Sdavidch                (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10236255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10237255736Sdavidch               0);
10238255736Sdavidch    }
10239255736Sdavidch
10240255736Sdavidch    /* function setup flags */
10241255736Sdavidch    flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10242255736Sdavidch
10243255736Sdavidch    /*
10244255736Sdavidch     * This flag is relevant for E1x only.
10245255736Sdavidch     * E2 doesn't have a TPA configuration in a function level.
10246255736Sdavidch     */
10247266979Smarcel    flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10248255736Sdavidch
10249255736Sdavidch    func_init.func_flgs = flags;
10250255736Sdavidch    func_init.pf_id     = SC_FUNC(sc);
10251255736Sdavidch    func_init.func_id   = SC_FUNC(sc);
10252255736Sdavidch    func_init.spq_map   = sc->spq_dma.paddr;
10253255736Sdavidch    func_init.spq_prod  = sc->spq_prod_idx;
10254255736Sdavidch
10255255736Sdavidch    bxe_func_init(sc, &func_init);
10256255736Sdavidch
10257255736Sdavidch    memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10258255736Sdavidch
10259255736Sdavidch    /*
10260255736Sdavidch     * Congestion management values depend on the link rate.
10261255736Sdavidch     * There is no active link so initial link rate is set to 10Gbps.
10262255736Sdavidch     * When the link comes up the congestion management values are
10263255736Sdavidch     * re-calculated according to the actual link rate.
10264255736Sdavidch     */
10265255736Sdavidch    sc->link_vars.line_speed = SPEED_10000;
10266255736Sdavidch    bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10267255736Sdavidch
10268255736Sdavidch    /* Only the PMF sets the HW */
10269255736Sdavidch    if (sc->port.pmf) {
10270255736Sdavidch        storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10271255736Sdavidch    }
10272255736Sdavidch
10273255736Sdavidch    /* init Event Queue - PCI bus guarantees correct endainity */
10274255736Sdavidch    eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10275255736Sdavidch    eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10276255736Sdavidch    eq_data.producer     = sc->eq_prod;
10277255736Sdavidch    eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10278255736Sdavidch    eq_data.sb_id        = DEF_SB_ID;
10279255736Sdavidch    storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10280255736Sdavidch}
10281255736Sdavidch
10282255736Sdavidchstatic void
10283255736Sdavidchbxe_hc_int_enable(struct bxe_softc *sc)
10284255736Sdavidch{
10285255736Sdavidch    int port = SC_PORT(sc);
10286255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10287255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10288255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10289255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10290255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10291255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10292255736Sdavidch
10293255736Sdavidch    if (msix) {
10294255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10295255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0);
10296255736Sdavidch        val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10297255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10298255736Sdavidch        if (single_msix) {
10299255736Sdavidch            val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10300255736Sdavidch        }
10301255736Sdavidch    } else if (msi) {
10302255736Sdavidch        val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10303255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10304255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10305255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10306255736Sdavidch    } else {
10307255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10308255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10309255736Sdavidch                HC_CONFIG_0_REG_INT_LINE_EN_0 |
10310255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10311255736Sdavidch
10312255736Sdavidch        if (!CHIP_IS_E1(sc)) {
10313255736Sdavidch            BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10314255736Sdavidch                  val, port, addr);
10315255736Sdavidch
10316255736Sdavidch            REG_WR(sc, addr, val);
10317255736Sdavidch
10318255736Sdavidch            val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10319255736Sdavidch        }
10320255736Sdavidch    }
10321255736Sdavidch
10322255736Sdavidch    if (CHIP_IS_E1(sc)) {
10323255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10324255736Sdavidch    }
10325255736Sdavidch
10326255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10327255736Sdavidch          val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10328255736Sdavidch
10329255736Sdavidch    REG_WR(sc, addr, val);
10330255736Sdavidch
10331255736Sdavidch    /* ensure that HC_CONFIG is written before leading/trailing edge config */
10332255736Sdavidch    mb();
10333255736Sdavidch
10334255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10335255736Sdavidch        /* init leading/trailing edge */
10336255736Sdavidch        if (IS_MF(sc)) {
10337255736Sdavidch            val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10338255736Sdavidch            if (sc->port.pmf) {
10339255736Sdavidch                /* enable nig and gpio3 attention */
10340255736Sdavidch                val |= 0x1100;
10341255736Sdavidch            }
10342255736Sdavidch        } else {
10343255736Sdavidch            val = 0xffff;
10344255736Sdavidch        }
10345255736Sdavidch
10346255736Sdavidch        REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10347255736Sdavidch        REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10348255736Sdavidch    }
10349255736Sdavidch
10350255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10351255736Sdavidch    mb();
10352255736Sdavidch}
10353255736Sdavidch
10354255736Sdavidchstatic void
10355255736Sdavidchbxe_igu_int_enable(struct bxe_softc *sc)
10356255736Sdavidch{
10357255736Sdavidch    uint32_t val;
10358255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10359255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10360255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10361255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10362255736Sdavidch
10363255736Sdavidch    val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10364255736Sdavidch
10365255736Sdavidch    if (msix) {
10366255736Sdavidch        val &= ~(IGU_PF_CONF_INT_LINE_EN |
10367255736Sdavidch                 IGU_PF_CONF_SINGLE_ISR_EN);
10368255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10369255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN);
10370255736Sdavidch        if (single_msix) {
10371255736Sdavidch            val |= IGU_PF_CONF_SINGLE_ISR_EN;
10372255736Sdavidch        }
10373255736Sdavidch    } else if (msi) {
10374255736Sdavidch        val &= ~IGU_PF_CONF_INT_LINE_EN;
10375255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10376255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10377255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10378255736Sdavidch    } else {
10379255736Sdavidch        val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10380255736Sdavidch        val |= (IGU_PF_CONF_INT_LINE_EN |
10381255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10382255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10383255736Sdavidch    }
10384255736Sdavidch
10385255736Sdavidch    /* clean previous status - need to configure igu prior to ack*/
10386255736Sdavidch    if ((!msix) || single_msix) {
10387255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10388255736Sdavidch        bxe_ack_int(sc);
10389255736Sdavidch    }
10390255736Sdavidch
10391255736Sdavidch    val |= IGU_PF_CONF_FUNC_EN;
10392255736Sdavidch
10393255736Sdavidch    BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10394255736Sdavidch          val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10395255736Sdavidch
10396255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10397255736Sdavidch
10398255736Sdavidch    mb();
10399255736Sdavidch
10400255736Sdavidch    /* init leading/trailing edge */
10401255736Sdavidch    if (IS_MF(sc)) {
10402255736Sdavidch        val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10403255736Sdavidch        if (sc->port.pmf) {
10404255736Sdavidch            /* enable nig and gpio3 attention */
10405255736Sdavidch            val |= 0x1100;
10406255736Sdavidch        }
10407255736Sdavidch    } else {
10408255736Sdavidch        val = 0xffff;
10409255736Sdavidch    }
10410255736Sdavidch
10411255736Sdavidch    REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10412255736Sdavidch    REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10413255736Sdavidch
10414255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10415255736Sdavidch    mb();
10416255736Sdavidch}
10417255736Sdavidch
10418255736Sdavidchstatic void
10419255736Sdavidchbxe_int_enable(struct bxe_softc *sc)
10420255736Sdavidch{
10421255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10422255736Sdavidch        bxe_hc_int_enable(sc);
10423255736Sdavidch    } else {
10424255736Sdavidch        bxe_igu_int_enable(sc);
10425255736Sdavidch    }
10426255736Sdavidch}
10427255736Sdavidch
10428255736Sdavidchstatic void
10429255736Sdavidchbxe_hc_int_disable(struct bxe_softc *sc)
10430255736Sdavidch{
10431255736Sdavidch    int port = SC_PORT(sc);
10432255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10433255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10434255736Sdavidch
10435255736Sdavidch    /*
10436255736Sdavidch     * In E1 we must use only PCI configuration space to disable MSI/MSIX
10437255736Sdavidch     * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10438255736Sdavidch     * block
10439255736Sdavidch     */
10440255736Sdavidch    if (CHIP_IS_E1(sc)) {
10441255736Sdavidch        /*
10442255736Sdavidch         * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10443255736Sdavidch         * to prevent from HC sending interrupts after we exit the function
10444255736Sdavidch         */
10445255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10446255736Sdavidch
10447255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10448255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10449255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10450255736Sdavidch    } else {
10451255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10452255736Sdavidch                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10453255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10454255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10455255736Sdavidch    }
10456255736Sdavidch
10457255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10458255736Sdavidch
10459255736Sdavidch    /* flush all outstanding writes */
10460255736Sdavidch    mb();
10461255736Sdavidch
10462255736Sdavidch    REG_WR(sc, addr, val);
10463255736Sdavidch    if (REG_RD(sc, addr) != val) {
10464255736Sdavidch        BLOGE(sc, "proper val not read from HC IGU!\n");
10465255736Sdavidch    }
10466255736Sdavidch}
10467255736Sdavidch
10468255736Sdavidchstatic void
10469255736Sdavidchbxe_igu_int_disable(struct bxe_softc *sc)
10470255736Sdavidch{
10471255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10472255736Sdavidch
10473255736Sdavidch    val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10474255736Sdavidch             IGU_PF_CONF_INT_LINE_EN |
10475255736Sdavidch             IGU_PF_CONF_ATTN_BIT_EN);
10476255736Sdavidch
10477255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10478255736Sdavidch
10479255736Sdavidch    /* flush all outstanding writes */
10480255736Sdavidch    mb();
10481255736Sdavidch
10482255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10483255736Sdavidch    if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10484255736Sdavidch        BLOGE(sc, "proper val not read from IGU!\n");
10485255736Sdavidch    }
10486255736Sdavidch}
10487255736Sdavidch
10488255736Sdavidchstatic void
10489255736Sdavidchbxe_int_disable(struct bxe_softc *sc)
10490255736Sdavidch{
10491255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10492255736Sdavidch        bxe_hc_int_disable(sc);
10493255736Sdavidch    } else {
10494255736Sdavidch        bxe_igu_int_disable(sc);
10495255736Sdavidch    }
10496255736Sdavidch}
10497255736Sdavidch
10498255736Sdavidchstatic void
10499255736Sdavidchbxe_nic_init(struct bxe_softc *sc,
10500255736Sdavidch             int              load_code)
10501255736Sdavidch{
10502255736Sdavidch    int i;
10503255736Sdavidch
10504255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
10505255736Sdavidch        bxe_init_eth_fp(sc, i);
10506255736Sdavidch    }
10507255736Sdavidch
10508255736Sdavidch    rmb(); /* ensure status block indices were read */
10509255736Sdavidch
10510255736Sdavidch    bxe_init_rx_rings(sc);
10511255736Sdavidch    bxe_init_tx_rings(sc);
10512255736Sdavidch
10513255736Sdavidch    if (IS_VF(sc)) {
10514255736Sdavidch        return;
10515255736Sdavidch    }
10516255736Sdavidch
10517255736Sdavidch    /* initialize MOD_ABS interrupts */
10518255736Sdavidch    elink_init_mod_abs_int(sc, &sc->link_vars,
10519255736Sdavidch                           sc->devinfo.chip_id,
10520255736Sdavidch                           sc->devinfo.shmem_base,
10521255736Sdavidch                           sc->devinfo.shmem2_base,
10522255736Sdavidch                           SC_PORT(sc));
10523255736Sdavidch
10524255736Sdavidch    bxe_init_def_sb(sc);
10525255736Sdavidch    bxe_update_dsb_idx(sc);
10526255736Sdavidch    bxe_init_sp_ring(sc);
10527255736Sdavidch    bxe_init_eq_ring(sc);
10528255736Sdavidch    bxe_init_internal(sc, load_code);
10529255736Sdavidch    bxe_pf_init(sc);
10530255736Sdavidch    bxe_stats_init(sc);
10531255736Sdavidch
10532255736Sdavidch    /* flush all before enabling interrupts */
10533255736Sdavidch    mb();
10534255736Sdavidch
10535255736Sdavidch    bxe_int_enable(sc);
10536255736Sdavidch
10537255736Sdavidch    /* check for SPIO5 */
10538255736Sdavidch    bxe_attn_int_deasserted0(sc,
10539255736Sdavidch                             REG_RD(sc,
10540255736Sdavidch                                    (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10541255736Sdavidch                                     SC_PORT(sc)*4)) &
10542255736Sdavidch                             AEU_INPUTS_ATTN_BITS_SPIO5);
10543255736Sdavidch}
10544255736Sdavidch
10545255736Sdavidchstatic inline void
10546255736Sdavidchbxe_init_objs(struct bxe_softc *sc)
10547255736Sdavidch{
10548255736Sdavidch    /* mcast rules must be added to tx if tx switching is enabled */
10549255736Sdavidch    ecore_obj_type o_type =
10550255736Sdavidch        (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10551255736Sdavidch                                         ECORE_OBJ_TYPE_RX;
10552255736Sdavidch
10553255736Sdavidch    /* RX_MODE controlling object */
10554255736Sdavidch    ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10555255736Sdavidch
10556255736Sdavidch    /* multicast configuration controlling object */
10557255736Sdavidch    ecore_init_mcast_obj(sc,
10558255736Sdavidch                         &sc->mcast_obj,
10559255736Sdavidch                         sc->fp[0].cl_id,
10560255736Sdavidch                         sc->fp[0].index,
10561255736Sdavidch                         SC_FUNC(sc),
10562255736Sdavidch                         SC_FUNC(sc),
10563255736Sdavidch                         BXE_SP(sc, mcast_rdata),
10564255736Sdavidch                         BXE_SP_MAPPING(sc, mcast_rdata),
10565255736Sdavidch                         ECORE_FILTER_MCAST_PENDING,
10566255736Sdavidch                         &sc->sp_state,
10567255736Sdavidch                         o_type);
10568255736Sdavidch
10569255736Sdavidch    /* Setup CAM credit pools */
10570255736Sdavidch    ecore_init_mac_credit_pool(sc,
10571255736Sdavidch                               &sc->macs_pool,
10572255736Sdavidch                               SC_FUNC(sc),
10573255736Sdavidch                               CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10574255736Sdavidch                                                 VNICS_PER_PATH(sc));
10575255736Sdavidch
10576255736Sdavidch    ecore_init_vlan_credit_pool(sc,
10577255736Sdavidch                                &sc->vlans_pool,
10578255736Sdavidch                                SC_ABS_FUNC(sc) >> 1,
10579255736Sdavidch                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10580255736Sdavidch                                                  VNICS_PER_PATH(sc));
10581255736Sdavidch
10582255736Sdavidch    /* RSS configuration object */
10583255736Sdavidch    ecore_init_rss_config_obj(sc,
10584255736Sdavidch                              &sc->rss_conf_obj,
10585255736Sdavidch                              sc->fp[0].cl_id,
10586255736Sdavidch                              sc->fp[0].index,
10587255736Sdavidch                              SC_FUNC(sc),
10588255736Sdavidch                              SC_FUNC(sc),
10589255736Sdavidch                              BXE_SP(sc, rss_rdata),
10590255736Sdavidch                              BXE_SP_MAPPING(sc, rss_rdata),
10591255736Sdavidch                              ECORE_FILTER_RSS_CONF_PENDING,
10592255736Sdavidch                              &sc->sp_state, ECORE_OBJ_TYPE_RX);
10593255736Sdavidch}
10594255736Sdavidch
10595255736Sdavidch/*
10596255736Sdavidch * Initialize the function. This must be called before sending CLIENT_SETUP
10597255736Sdavidch * for the first client.
10598255736Sdavidch */
10599255736Sdavidchstatic inline int
10600255736Sdavidchbxe_func_start(struct bxe_softc *sc)
10601255736Sdavidch{
10602255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
10603255736Sdavidch    struct ecore_func_start_params *start_params = &func_params.params.start;
10604255736Sdavidch
10605255736Sdavidch    /* Prepare parameters for function state transitions */
10606255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10607255736Sdavidch
10608255736Sdavidch    func_params.f_obj = &sc->func_obj;
10609255736Sdavidch    func_params.cmd = ECORE_F_CMD_START;
10610255736Sdavidch
10611255736Sdavidch    /* Function parameters */
10612255736Sdavidch    start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10613255736Sdavidch    start_params->sd_vlan_tag = OVLAN(sc);
10614255736Sdavidch
10615255736Sdavidch    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10616255736Sdavidch        start_params->network_cos_mode = STATIC_COS;
10617255736Sdavidch    } else { /* CHIP_IS_E1X */
10618255736Sdavidch        start_params->network_cos_mode = FW_WRR;
10619255736Sdavidch    }
10620255736Sdavidch
10621296071Sdavidcs    //start_params->gre_tunnel_mode = 0;
10622296071Sdavidcs    //start_params->gre_tunnel_rss  = 0;
10623255736Sdavidch
10624255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
10625255736Sdavidch}
10626255736Sdavidch
10627255736Sdavidchstatic int
10628255736Sdavidchbxe_set_power_state(struct bxe_softc *sc,
10629255736Sdavidch                    uint8_t          state)
10630255736Sdavidch{
10631255736Sdavidch    uint16_t pmcsr;
10632255736Sdavidch
10633255736Sdavidch    /* If there is no power capability, silently succeed */
10634255736Sdavidch    if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10635255736Sdavidch        BLOGW(sc, "No power capability\n");
10636255736Sdavidch        return (0);
10637255736Sdavidch    }
10638255736Sdavidch
10639255736Sdavidch    pmcsr = pci_read_config(sc->dev,
10640255736Sdavidch                            (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10641255736Sdavidch                            2);
10642255736Sdavidch
10643255736Sdavidch    switch (state) {
10644255736Sdavidch    case PCI_PM_D0:
10645255736Sdavidch        pci_write_config(sc->dev,
10646255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10647255736Sdavidch                         ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10648255736Sdavidch
10649255736Sdavidch        if (pmcsr & PCIM_PSTAT_DMASK) {
10650255736Sdavidch            /* delay required during transition out of D3hot */
10651255736Sdavidch            DELAY(20000);
10652255736Sdavidch        }
10653255736Sdavidch
10654255736Sdavidch        break;
10655255736Sdavidch
10656255736Sdavidch    case PCI_PM_D3hot:
10657255736Sdavidch        /* XXX if there are other clients above don't shut down the power */
10658255736Sdavidch
10659255736Sdavidch        /* don't shut down the power for emulation and FPGA */
10660255736Sdavidch        if (CHIP_REV_IS_SLOW(sc)) {
10661255736Sdavidch            return (0);
10662255736Sdavidch        }
10663255736Sdavidch
10664255736Sdavidch        pmcsr &= ~PCIM_PSTAT_DMASK;
10665255736Sdavidch        pmcsr |= PCIM_PSTAT_D3;
10666255736Sdavidch
10667255736Sdavidch        if (sc->wol) {
10668255736Sdavidch            pmcsr |= PCIM_PSTAT_PMEENABLE;
10669255736Sdavidch        }
10670255736Sdavidch
10671255736Sdavidch        pci_write_config(sc->dev,
10672255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10673255736Sdavidch                         pmcsr, 4);
10674255736Sdavidch
10675255736Sdavidch        /*
10676255736Sdavidch         * No more memory access after this point until device is brought back
10677255736Sdavidch         * to D0 state.
10678255736Sdavidch         */
10679255736Sdavidch        break;
10680255736Sdavidch
10681255736Sdavidch    default:
10682295830Sdavidcs        BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10683295830Sdavidcs            state, pmcsr);
10684255736Sdavidch        return (-1);
10685255736Sdavidch    }
10686255736Sdavidch
10687255736Sdavidch    return (0);
10688255736Sdavidch}
10689255736Sdavidch
10690255736Sdavidch
10691255736Sdavidch/* return true if succeeded to acquire the lock */
10692255736Sdavidchstatic uint8_t
10693255736Sdavidchbxe_trylock_hw_lock(struct bxe_softc *sc,
10694255736Sdavidch                    uint32_t         resource)
10695255736Sdavidch{
10696255736Sdavidch    uint32_t lock_status;
10697255736Sdavidch    uint32_t resource_bit = (1 << resource);
10698255736Sdavidch    int func = SC_FUNC(sc);
10699255736Sdavidch    uint32_t hw_lock_control_reg;
10700255736Sdavidch
10701255736Sdavidch    BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10702255736Sdavidch
10703255736Sdavidch    /* Validating that the resource is within range */
10704255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10705255736Sdavidch        BLOGD(sc, DBG_LOAD,
10706255736Sdavidch              "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10707255736Sdavidch              resource, HW_LOCK_MAX_RESOURCE_VALUE);
10708255736Sdavidch        return (FALSE);
10709255736Sdavidch    }
10710255736Sdavidch
10711255736Sdavidch    if (func <= 5) {
10712255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10713255736Sdavidch    } else {
10714255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10715255736Sdavidch    }
10716255736Sdavidch
10717255736Sdavidch    /* try to acquire the lock */
10718255736Sdavidch    REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10719255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
10720255736Sdavidch    if (lock_status & resource_bit) {
10721255736Sdavidch        return (TRUE);
10722255736Sdavidch    }
10723255736Sdavidch
10724295830Sdavidcs    BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10725295830Sdavidcs        "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10726295830Sdavidcs        lock_status, resource_bit);
10727255736Sdavidch
10728255736Sdavidch    return (FALSE);
10729255736Sdavidch}
10730255736Sdavidch
10731255736Sdavidch/*
10732255736Sdavidch * Get the recovery leader resource id according to the engine this function
10733255736Sdavidch * belongs to. Currently only only 2 engines is supported.
10734255736Sdavidch */
10735255736Sdavidchstatic int
10736255736Sdavidchbxe_get_leader_lock_resource(struct bxe_softc *sc)
10737255736Sdavidch{
10738255736Sdavidch    if (SC_PATH(sc)) {
10739255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10740255736Sdavidch    } else {
10741255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10742255736Sdavidch    }
10743255736Sdavidch}
10744255736Sdavidch
10745255736Sdavidch/* try to acquire a leader lock for current engine */
10746255736Sdavidchstatic uint8_t
10747255736Sdavidchbxe_trylock_leader_lock(struct bxe_softc *sc)
10748255736Sdavidch{
10749255736Sdavidch    return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10750255736Sdavidch}
10751255736Sdavidch
10752255736Sdavidchstatic int
10753255736Sdavidchbxe_release_leader_lock(struct bxe_softc *sc)
10754255736Sdavidch{
10755255736Sdavidch    return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10756255736Sdavidch}
10757255736Sdavidch
10758255736Sdavidch/* close gates #2, #3 and #4 */
10759255736Sdavidchstatic void
10760255736Sdavidchbxe_set_234_gates(struct bxe_softc *sc,
10761255736Sdavidch                  uint8_t          close)
10762255736Sdavidch{
10763255736Sdavidch    uint32_t val;
10764255736Sdavidch
10765255736Sdavidch    /* gates #2 and #4a are closed/opened for "not E1" only */
10766255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10767255736Sdavidch        /* #4 */
10768255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10769255736Sdavidch        /* #2 */
10770255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10771255736Sdavidch    }
10772255736Sdavidch
10773255736Sdavidch    /* #3 */
10774255736Sdavidch    if (CHIP_IS_E1x(sc)) {
10775255736Sdavidch        /* prevent interrupts from HC on both ports */
10776255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_1);
10777255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_1,
10778255736Sdavidch               (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10779255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10780255736Sdavidch
10781255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_0);
10782255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_0,
10783255736Sdavidch               (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10784255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10785255736Sdavidch    } else {
10786298955Spfg        /* Prevent incoming interrupts in IGU */
10787255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10788255736Sdavidch
10789255736Sdavidch        REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10790255736Sdavidch               (!close) ?
10791255736Sdavidch               (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10792255736Sdavidch               (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10793255736Sdavidch    }
10794255736Sdavidch
10795255736Sdavidch    BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10796255736Sdavidch          close ? "closing" : "opening");
10797255736Sdavidch
10798255736Sdavidch    wmb();
10799255736Sdavidch}
10800255736Sdavidch
10801255736Sdavidch/* poll for pending writes bit, it should get cleared in no more than 1s */
10802255736Sdavidchstatic int
10803255736Sdavidchbxe_er_poll_igu_vq(struct bxe_softc *sc)
10804255736Sdavidch{
10805255736Sdavidch    uint32_t cnt = 1000;
10806255736Sdavidch    uint32_t pend_bits = 0;
10807255736Sdavidch
10808255736Sdavidch    do {
10809255736Sdavidch        pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10810255736Sdavidch
10811255736Sdavidch        if (pend_bits == 0) {
10812255736Sdavidch            break;
10813255736Sdavidch        }
10814255736Sdavidch
10815255736Sdavidch        DELAY(1000);
10816255736Sdavidch    } while (--cnt > 0);
10817255736Sdavidch
10818255736Sdavidch    if (cnt == 0) {
10819255736Sdavidch        BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10820255736Sdavidch        return (-1);
10821255736Sdavidch    }
10822255736Sdavidch
10823255736Sdavidch    return (0);
10824255736Sdavidch}
10825255736Sdavidch
10826255736Sdavidch#define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10827255736Sdavidch
10828255736Sdavidchstatic void
10829255736Sdavidchbxe_clp_reset_prep(struct bxe_softc *sc,
10830255736Sdavidch                   uint32_t         *magic_val)
10831255736Sdavidch{
10832255736Sdavidch    /* Do some magic... */
10833255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10834255736Sdavidch    *magic_val = val & SHARED_MF_CLP_MAGIC;
10835255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10836255736Sdavidch}
10837255736Sdavidch
10838255736Sdavidch/* restore the value of the 'magic' bit */
10839255736Sdavidchstatic void
10840255736Sdavidchbxe_clp_reset_done(struct bxe_softc *sc,
10841255736Sdavidch                   uint32_t         magic_val)
10842255736Sdavidch{
10843255736Sdavidch    /* Restore the 'magic' bit value... */
10844255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10845255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb,
10846255736Sdavidch              (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10847255736Sdavidch}
10848255736Sdavidch
10849255736Sdavidch/* prepare for MCP reset, takes care of CLP configurations */
10850255736Sdavidchstatic void
10851255736Sdavidchbxe_reset_mcp_prep(struct bxe_softc *sc,
10852255736Sdavidch                   uint32_t         *magic_val)
10853255736Sdavidch{
10854255736Sdavidch    uint32_t shmem;
10855255736Sdavidch    uint32_t validity_offset;
10856255736Sdavidch
10857255736Sdavidch    /* set `magic' bit in order to save MF config */
10858255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10859255736Sdavidch        bxe_clp_reset_prep(sc, magic_val);
10860255736Sdavidch    }
10861255736Sdavidch
10862255736Sdavidch    /* get shmem offset */
10863255736Sdavidch    shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10864255736Sdavidch    validity_offset =
10865255736Sdavidch        offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10866255736Sdavidch
10867255736Sdavidch    /* Clear validity map flags */
10868255736Sdavidch    if (shmem > 0) {
10869255736Sdavidch        REG_WR(sc, shmem + validity_offset, 0);
10870255736Sdavidch    }
10871255736Sdavidch}
10872255736Sdavidch
10873255736Sdavidch#define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10874255736Sdavidch#define MCP_ONE_TIMEOUT  100    /* 100 ms */
10875255736Sdavidch
10876255736Sdavidchstatic void
10877255736Sdavidchbxe_mcp_wait_one(struct bxe_softc *sc)
10878255736Sdavidch{
10879255736Sdavidch    /* special handling for emulation and FPGA (10 times longer) */
10880255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10881255736Sdavidch        DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10882255736Sdavidch    } else {
10883255736Sdavidch        DELAY((MCP_ONE_TIMEOUT) * 1000);
10884255736Sdavidch    }
10885255736Sdavidch}
10886255736Sdavidch
10887255736Sdavidch/* initialize shmem_base and waits for validity signature to appear */
10888255736Sdavidchstatic int
10889255736Sdavidchbxe_init_shmem(struct bxe_softc *sc)
10890255736Sdavidch{
10891255736Sdavidch    int cnt = 0;
10892255736Sdavidch    uint32_t val = 0;
10893255736Sdavidch
10894255736Sdavidch    do {
10895255736Sdavidch        sc->devinfo.shmem_base     =
10896255736Sdavidch        sc->link_params.shmem_base =
10897255736Sdavidch            REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10898255736Sdavidch
10899255736Sdavidch        if (sc->devinfo.shmem_base) {
10900255736Sdavidch            val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10901255736Sdavidch            if (val & SHR_MEM_VALIDITY_MB)
10902255736Sdavidch                return (0);
10903255736Sdavidch        }
10904255736Sdavidch
10905255736Sdavidch        bxe_mcp_wait_one(sc);
10906255736Sdavidch
10907255736Sdavidch    } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10908255736Sdavidch
10909255736Sdavidch    BLOGE(sc, "BAD MCP validity signature\n");
10910255736Sdavidch
10911255736Sdavidch    return (-1);
10912255736Sdavidch}
10913255736Sdavidch
10914255736Sdavidchstatic int
10915255736Sdavidchbxe_reset_mcp_comp(struct bxe_softc *sc,
10916255736Sdavidch                   uint32_t         magic_val)
10917255736Sdavidch{
10918255736Sdavidch    int rc = bxe_init_shmem(sc);
10919255736Sdavidch
10920255736Sdavidch    /* Restore the `magic' bit value */
10921255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10922255736Sdavidch        bxe_clp_reset_done(sc, magic_val);
10923255736Sdavidch    }
10924255736Sdavidch
10925255736Sdavidch    return (rc);
10926255736Sdavidch}
10927255736Sdavidch
10928255736Sdavidchstatic void
10929255736Sdavidchbxe_pxp_prep(struct bxe_softc *sc)
10930255736Sdavidch{
10931255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10932255736Sdavidch        REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10933255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10934255736Sdavidch        wmb();
10935255736Sdavidch    }
10936255736Sdavidch}
10937255736Sdavidch
10938255736Sdavidch/*
10939255736Sdavidch * Reset the whole chip except for:
10940255736Sdavidch *      - PCIE core
10941255736Sdavidch *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10942255736Sdavidch *      - IGU
10943255736Sdavidch *      - MISC (including AEU)
10944255736Sdavidch *      - GRC
10945255736Sdavidch *      - RBCN, RBCP
10946255736Sdavidch */
10947255736Sdavidchstatic void
10948255736Sdavidchbxe_process_kill_chip_reset(struct bxe_softc *sc,
10949255736Sdavidch                            uint8_t          global)
10950255736Sdavidch{
10951255736Sdavidch    uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10952255736Sdavidch    uint32_t global_bits2, stay_reset2;
10953255736Sdavidch
10954255736Sdavidch    /*
10955255736Sdavidch     * Bits that have to be set in reset_mask2 if we want to reset 'global'
10956255736Sdavidch     * (per chip) blocks.
10957255736Sdavidch     */
10958255736Sdavidch    global_bits2 =
10959255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10960255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10961255736Sdavidch
10962255736Sdavidch    /*
10963255736Sdavidch     * Don't reset the following blocks.
10964255736Sdavidch     * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10965255736Sdavidch     *            reset, as in 4 port device they might still be owned
10966255736Sdavidch     *            by the MCP (there is only one leader per path).
10967255736Sdavidch     */
10968255736Sdavidch    not_reset_mask1 =
10969255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_HC |
10970255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10971255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXP;
10972255736Sdavidch
10973255736Sdavidch    not_reset_mask2 =
10974255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10975255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10976255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10977255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10978255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10979255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10980255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10981255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10982255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_ATC |
10983255736Sdavidch        MISC_REGISTERS_RESET_REG_2_PGLC |
10984255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10985255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10986255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10987255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10988255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC0 |
10989255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC1;
10990255736Sdavidch
10991255736Sdavidch    /*
10992255736Sdavidch     * Keep the following blocks in reset:
10993255736Sdavidch     *  - all xxMACs are handled by the elink code.
10994255736Sdavidch     */
10995255736Sdavidch    stay_reset2 =
10996255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC |
10997255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
10998255736Sdavidch
10999255736Sdavidch    /* Full reset masks according to the chip */
11000255736Sdavidch    reset_mask1 = 0xffffffff;
11001255736Sdavidch
11002255736Sdavidch    if (CHIP_IS_E1(sc))
11003255736Sdavidch        reset_mask2 = 0xffff;
11004255736Sdavidch    else if (CHIP_IS_E1H(sc))
11005255736Sdavidch        reset_mask2 = 0x1ffff;
11006255736Sdavidch    else if (CHIP_IS_E2(sc))
11007255736Sdavidch        reset_mask2 = 0xfffff;
11008255736Sdavidch    else /* CHIP_IS_E3 */
11009255736Sdavidch        reset_mask2 = 0x3ffffff;
11010255736Sdavidch
11011255736Sdavidch    /* Don't reset global blocks unless we need to */
11012255736Sdavidch    if (!global)
11013255736Sdavidch        reset_mask2 &= ~global_bits2;
11014255736Sdavidch
11015255736Sdavidch    /*
11016255736Sdavidch     * In case of attention in the QM, we need to reset PXP
11017255736Sdavidch     * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
11018255736Sdavidch     * because otherwise QM reset would release 'close the gates' shortly
11019255736Sdavidch     * before resetting the PXP, then the PSWRQ would send a write
11020255736Sdavidch     * request to PGLUE. Then when PXP is reset, PGLUE would try to
11021255736Sdavidch     * read the payload data from PSWWR, but PSWWR would not
11022255736Sdavidch     * respond. The write queue in PGLUE would stuck, dmae commands
11023255736Sdavidch     * would not return. Therefore it's important to reset the second
11024255736Sdavidch     * reset register (containing the
11025255736Sdavidch     * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
11026255736Sdavidch     * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
11027255736Sdavidch     * bit).
11028255736Sdavidch     */
11029255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
11030255736Sdavidch           reset_mask2 & (~not_reset_mask2));
11031255736Sdavidch
11032255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
11033255736Sdavidch           reset_mask1 & (~not_reset_mask1));
11034255736Sdavidch
11035255736Sdavidch    mb();
11036255736Sdavidch    wmb();
11037255736Sdavidch
11038255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
11039255736Sdavidch           reset_mask2 & (~stay_reset2));
11040255736Sdavidch
11041255736Sdavidch    mb();
11042255736Sdavidch    wmb();
11043255736Sdavidch
11044255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
11045255736Sdavidch    wmb();
11046255736Sdavidch}
11047255736Sdavidch
11048255736Sdavidchstatic int
11049255736Sdavidchbxe_process_kill(struct bxe_softc *sc,
11050255736Sdavidch                 uint8_t          global)
11051255736Sdavidch{
11052255736Sdavidch    int cnt = 1000;
11053255736Sdavidch    uint32_t val = 0;
11054255736Sdavidch    uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
11055255736Sdavidch    uint32_t tags_63_32 = 0;
11056255736Sdavidch
11057255736Sdavidch    /* Empty the Tetris buffer, wait for 1s */
11058255736Sdavidch    do {
11059255736Sdavidch        sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
11060255736Sdavidch        blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
11061255736Sdavidch        port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
11062255736Sdavidch        port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
11063255736Sdavidch        pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
11064255736Sdavidch        if (CHIP_IS_E3(sc)) {
11065255736Sdavidch            tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
11066255736Sdavidch        }
11067255736Sdavidch
11068255736Sdavidch        if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
11069255736Sdavidch            ((port_is_idle_0 & 0x1) == 0x1) &&
11070255736Sdavidch            ((port_is_idle_1 & 0x1) == 0x1) &&
11071255736Sdavidch            (pgl_exp_rom2 == 0xffffffff) &&
11072255736Sdavidch            (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11073255736Sdavidch            break;
11074255736Sdavidch        DELAY(1000);
11075255736Sdavidch    } while (cnt-- > 0);
11076255736Sdavidch
11077255736Sdavidch    if (cnt <= 0) {
11078255736Sdavidch        BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11079255736Sdavidch                  "are still outstanding read requests after 1s! "
11080255736Sdavidch                  "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11081255736Sdavidch                  "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11082255736Sdavidch              sr_cnt, blk_cnt, port_is_idle_0,
11083255736Sdavidch              port_is_idle_1, pgl_exp_rom2);
11084255736Sdavidch        return (-1);
11085255736Sdavidch    }
11086255736Sdavidch
11087255736Sdavidch    mb();
11088255736Sdavidch
11089255736Sdavidch    /* Close gates #2, #3 and #4 */
11090255736Sdavidch    bxe_set_234_gates(sc, TRUE);
11091255736Sdavidch
11092255736Sdavidch    /* Poll for IGU VQs for 57712 and newer chips */
11093255736Sdavidch    if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11094255736Sdavidch        return (-1);
11095255736Sdavidch    }
11096255736Sdavidch
11097255736Sdavidch    /* XXX indicate that "process kill" is in progress to MCP */
11098255736Sdavidch
11099255736Sdavidch    /* clear "unprepared" bit */
11100255736Sdavidch    REG_WR(sc, MISC_REG_UNPREPARED, 0);
11101255736Sdavidch    mb();
11102255736Sdavidch
11103255736Sdavidch    /* Make sure all is written to the chip before the reset */
11104255736Sdavidch    wmb();
11105255736Sdavidch
11106255736Sdavidch    /*
11107255736Sdavidch     * Wait for 1ms to empty GLUE and PCI-E core queues,
11108255736Sdavidch     * PSWHST, GRC and PSWRD Tetris buffer.
11109255736Sdavidch     */
11110255736Sdavidch    DELAY(1000);
11111255736Sdavidch
11112255736Sdavidch    /* Prepare to chip reset: */
11113255736Sdavidch    /* MCP */
11114255736Sdavidch    if (global) {
11115255736Sdavidch        bxe_reset_mcp_prep(sc, &val);
11116255736Sdavidch    }
11117255736Sdavidch
11118255736Sdavidch    /* PXP */
11119255736Sdavidch    bxe_pxp_prep(sc);
11120255736Sdavidch    mb();
11121255736Sdavidch
11122255736Sdavidch    /* reset the chip */
11123255736Sdavidch    bxe_process_kill_chip_reset(sc, global);
11124255736Sdavidch    mb();
11125255736Sdavidch
11126268854Sdavidcs    /* clear errors in PGB */
11127268854Sdavidcs    if (!CHIP_IS_E1(sc))
11128268854Sdavidcs        REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11129268854Sdavidcs
11130255736Sdavidch    /* Recover after reset: */
11131255736Sdavidch    /* MCP */
11132255736Sdavidch    if (global && bxe_reset_mcp_comp(sc, val)) {
11133255736Sdavidch        return (-1);
11134255736Sdavidch    }
11135255736Sdavidch
11136255736Sdavidch    /* XXX add resetting the NO_MCP mode DB here */
11137255736Sdavidch
11138255736Sdavidch    /* Open the gates #2, #3 and #4 */
11139255736Sdavidch    bxe_set_234_gates(sc, FALSE);
11140255736Sdavidch
11141255736Sdavidch    /* XXX
11142255736Sdavidch     * IGU/AEU preparation bring back the AEU/IGU to a reset state
11143255736Sdavidch     * re-enable attentions
11144255736Sdavidch     */
11145255736Sdavidch
11146255736Sdavidch    return (0);
11147255736Sdavidch}
11148255736Sdavidch
11149255736Sdavidchstatic int
11150255736Sdavidchbxe_leader_reset(struct bxe_softc *sc)
11151255736Sdavidch{
11152255736Sdavidch    int rc = 0;
11153255736Sdavidch    uint8_t global = bxe_reset_is_global(sc);
11154255736Sdavidch    uint32_t load_code;
11155255736Sdavidch
11156255736Sdavidch    /*
11157255736Sdavidch     * If not going to reset MCP, load "fake" driver to reset HW while
11158255736Sdavidch     * driver is owner of the HW.
11159255736Sdavidch     */
11160255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11161255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11162255736Sdavidch                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11163255736Sdavidch        if (!load_code) {
11164255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11165255736Sdavidch            rc = -1;
11166255736Sdavidch            goto exit_leader_reset;
11167255736Sdavidch        }
11168255736Sdavidch
11169255736Sdavidch        if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11170255736Sdavidch            (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11171255736Sdavidch            BLOGE(sc, "MCP unexpected response, aborting\n");
11172255736Sdavidch            rc = -1;
11173255736Sdavidch            goto exit_leader_reset2;
11174255736Sdavidch        }
11175255736Sdavidch
11176255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11177255736Sdavidch        if (!load_code) {
11178255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11179255736Sdavidch            rc = -1;
11180255736Sdavidch            goto exit_leader_reset2;
11181255736Sdavidch        }
11182255736Sdavidch    }
11183255736Sdavidch
11184255736Sdavidch    /* try to recover after the failure */
11185255736Sdavidch    if (bxe_process_kill(sc, global)) {
11186255736Sdavidch        BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11187255736Sdavidch        rc = -1;
11188255736Sdavidch        goto exit_leader_reset2;
11189255736Sdavidch    }
11190255736Sdavidch
11191255736Sdavidch    /*
11192255736Sdavidch     * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11193255736Sdavidch     * state.
11194255736Sdavidch     */
11195255736Sdavidch    bxe_set_reset_done(sc);
11196255736Sdavidch    if (global) {
11197255736Sdavidch        bxe_clear_reset_global(sc);
11198255736Sdavidch    }
11199255736Sdavidch
11200255736Sdavidchexit_leader_reset2:
11201255736Sdavidch
11202255736Sdavidch    /* unload "fake driver" if it was loaded */
11203255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11204255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11205255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11206255736Sdavidch    }
11207255736Sdavidch
11208255736Sdavidchexit_leader_reset:
11209255736Sdavidch
11210255736Sdavidch    sc->is_leader = 0;
11211255736Sdavidch    bxe_release_leader_lock(sc);
11212255736Sdavidch
11213255736Sdavidch    mb();
11214255736Sdavidch    return (rc);
11215255736Sdavidch}
11216255736Sdavidch
11217255736Sdavidch/*
11218255736Sdavidch * prepare INIT transition, parameters configured:
11219255736Sdavidch *   - HC configuration
11220255736Sdavidch *   - Queue's CDU context
11221255736Sdavidch */
11222255736Sdavidchstatic void
11223255736Sdavidchbxe_pf_q_prep_init(struct bxe_softc               *sc,
11224255736Sdavidch                   struct bxe_fastpath            *fp,
11225255736Sdavidch                   struct ecore_queue_init_params *init_params)
11226255736Sdavidch{
11227255736Sdavidch    uint8_t cos;
11228255736Sdavidch    int cxt_index, cxt_offset;
11229255736Sdavidch
11230255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11231255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11232255736Sdavidch
11233255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11234255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11235255736Sdavidch
11236255736Sdavidch    /* HC rate */
11237255736Sdavidch    init_params->rx.hc_rate =
11238255736Sdavidch        sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11239255736Sdavidch    init_params->tx.hc_rate =
11240255736Sdavidch        sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11241255736Sdavidch
11242255736Sdavidch    /* FW SB ID */
11243255736Sdavidch    init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11244255736Sdavidch
11245255736Sdavidch    /* CQ index among the SB indices */
11246255736Sdavidch    init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11247255736Sdavidch    init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11248255736Sdavidch
11249255736Sdavidch    /* set maximum number of COSs supported by this queue */
11250255736Sdavidch    init_params->max_cos = sc->max_cos;
11251255736Sdavidch
11252255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11253255736Sdavidch          fp->index, init_params->max_cos);
11254255736Sdavidch
11255255736Sdavidch    /* set the context pointers queue object */
11256255736Sdavidch    for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11257255736Sdavidch        /* XXX change index/cid here if ever support multiple tx CoS */
11258255736Sdavidch        /* fp->txdata[cos]->cid */
11259255736Sdavidch        cxt_index = fp->index / ILT_PAGE_CIDS;
11260255736Sdavidch        cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11261255736Sdavidch        init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11262255736Sdavidch    }
11263255736Sdavidch}
11264255736Sdavidch
11265255736Sdavidch/* set flags that are common for the Tx-only and not normal connections */
11266255736Sdavidchstatic unsigned long
11267255736Sdavidchbxe_get_common_flags(struct bxe_softc    *sc,
11268255736Sdavidch                     struct bxe_fastpath *fp,
11269255736Sdavidch                     uint8_t             zero_stats)
11270255736Sdavidch{
11271255736Sdavidch    unsigned long flags = 0;
11272255736Sdavidch
11273255736Sdavidch    /* PF driver will always initialize the Queue to an ACTIVE state */
11274255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11275255736Sdavidch
11276255736Sdavidch    /*
11277255736Sdavidch     * tx only connections collect statistics (on the same index as the
11278255736Sdavidch     * parent connection). The statistics are zeroed when the parent
11279255736Sdavidch     * connection is initialized.
11280255736Sdavidch     */
11281255736Sdavidch
11282255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11283255736Sdavidch    if (zero_stats) {
11284255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11285255736Sdavidch    }
11286255736Sdavidch
11287255736Sdavidch    /*
11288255736Sdavidch     * tx only connections can support tx-switching, though their
11289255736Sdavidch     * CoS-ness doesn't survive the loopback
11290255736Sdavidch     */
11291255736Sdavidch    if (sc->flags & BXE_TX_SWITCHING) {
11292255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11293255736Sdavidch    }
11294255736Sdavidch
11295255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11296255736Sdavidch
11297255736Sdavidch    return (flags);
11298255736Sdavidch}
11299255736Sdavidch
11300255736Sdavidchstatic unsigned long
11301255736Sdavidchbxe_get_q_flags(struct bxe_softc    *sc,
11302255736Sdavidch                struct bxe_fastpath *fp,
11303255736Sdavidch                uint8_t             leading)
11304255736Sdavidch{
11305255736Sdavidch    unsigned long flags = 0;
11306255736Sdavidch
11307255736Sdavidch    if (IS_MF_SD(sc)) {
11308255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11309255736Sdavidch    }
11310255736Sdavidch
11311266979Smarcel    if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11312255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11313315881Sdavidcs#if __FreeBSD_version >= 800000
11314255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11315315881Sdavidcs#endif
11316255736Sdavidch    }
11317255736Sdavidch
11318255736Sdavidch    if (leading) {
11319255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11320255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11321255736Sdavidch    }
11322255736Sdavidch
11323255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11324255736Sdavidch
11325255736Sdavidch    /* merge with common flags */
11326255736Sdavidch    return (flags | bxe_get_common_flags(sc, fp, TRUE));
11327255736Sdavidch}
11328255736Sdavidch
11329255736Sdavidchstatic void
11330255736Sdavidchbxe_pf_q_prep_general(struct bxe_softc                  *sc,
11331255736Sdavidch                      struct bxe_fastpath               *fp,
11332255736Sdavidch                      struct ecore_general_setup_params *gen_init,
11333255736Sdavidch                      uint8_t                           cos)
11334255736Sdavidch{
11335255736Sdavidch    gen_init->stat_id = bxe_stats_id(fp);
11336255736Sdavidch    gen_init->spcl_id = fp->cl_id;
11337255736Sdavidch    gen_init->mtu = sc->mtu;
11338255736Sdavidch    gen_init->cos = cos;
11339255736Sdavidch}
11340255736Sdavidch
11341255736Sdavidchstatic void
11342255736Sdavidchbxe_pf_rx_q_prep(struct bxe_softc              *sc,
11343255736Sdavidch                 struct bxe_fastpath           *fp,
11344255736Sdavidch                 struct rxq_pause_params       *pause,
11345255736Sdavidch                 struct ecore_rxq_setup_params *rxq_init)
11346255736Sdavidch{
11347255736Sdavidch    uint8_t max_sge = 0;
11348255736Sdavidch    uint16_t sge_sz = 0;
11349255736Sdavidch    uint16_t tpa_agg_size = 0;
11350255736Sdavidch
11351283274Sdavidcs    pause->sge_th_lo = SGE_TH_LO(sc);
11352283274Sdavidcs    pause->sge_th_hi = SGE_TH_HI(sc);
11353255736Sdavidch
11354283274Sdavidcs    /* validate SGE ring has enough to cross high threshold */
11355283274Sdavidcs    if (sc->dropless_fc &&
11356255736Sdavidch            (pause->sge_th_hi + FW_PREFETCH_CNT) >
11357255736Sdavidch            (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11358283274Sdavidcs        BLOGW(sc, "sge ring threshold limit\n");
11359283274Sdavidcs    }
11360255736Sdavidch
11361283274Sdavidcs    /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11362283274Sdavidcs    tpa_agg_size = (2 * sc->mtu);
11363283274Sdavidcs    if (tpa_agg_size < sc->max_aggregation_size) {
11364283274Sdavidcs        tpa_agg_size = sc->max_aggregation_size;
11365283274Sdavidcs    }
11366255736Sdavidch
11367283274Sdavidcs    max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11368283274Sdavidcs    max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11369255736Sdavidch                   (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11370283274Sdavidcs    sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11371255736Sdavidch
11372255736Sdavidch    /* pause - not for e1 */
11373255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11374255736Sdavidch        pause->bd_th_lo = BD_TH_LO(sc);
11375255736Sdavidch        pause->bd_th_hi = BD_TH_HI(sc);
11376255736Sdavidch
11377255736Sdavidch        pause->rcq_th_lo = RCQ_TH_LO(sc);
11378255736Sdavidch        pause->rcq_th_hi = RCQ_TH_HI(sc);
11379255736Sdavidch
11380255736Sdavidch        /* validate rings have enough entries to cross high thresholds */
11381255736Sdavidch        if (sc->dropless_fc &&
11382255736Sdavidch            pause->bd_th_hi + FW_PREFETCH_CNT >
11383255736Sdavidch            sc->rx_ring_size) {
11384255736Sdavidch            BLOGW(sc, "rx bd ring threshold limit\n");
11385255736Sdavidch        }
11386255736Sdavidch
11387255736Sdavidch        if (sc->dropless_fc &&
11388255736Sdavidch            pause->rcq_th_hi + FW_PREFETCH_CNT >
11389255736Sdavidch            RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11390255736Sdavidch            BLOGW(sc, "rcq ring threshold limit\n");
11391255736Sdavidch        }
11392255736Sdavidch
11393255736Sdavidch        pause->pri_map = 1;
11394255736Sdavidch    }
11395255736Sdavidch
11396255736Sdavidch    /* rxq setup */
11397255736Sdavidch    rxq_init->dscr_map   = fp->rx_dma.paddr;
11398255736Sdavidch    rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11399255736Sdavidch    rxq_init->rcq_map    = fp->rcq_dma.paddr;
11400255736Sdavidch    rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11401255736Sdavidch
11402255736Sdavidch    /*
11403255736Sdavidch     * This should be a maximum number of data bytes that may be
11404255736Sdavidch     * placed on the BD (not including paddings).
11405255736Sdavidch     */
11406255736Sdavidch    rxq_init->buf_sz = (fp->rx_buf_size -
11407255736Sdavidch                        IP_HEADER_ALIGNMENT_PADDING);
11408255736Sdavidch
11409255736Sdavidch    rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11410255736Sdavidch    rxq_init->tpa_agg_sz      = tpa_agg_size;
11411255736Sdavidch    rxq_init->sge_buf_sz      = sge_sz;
11412255736Sdavidch    rxq_init->max_sges_pkt    = max_sge;
11413255736Sdavidch    rxq_init->rss_engine_id   = SC_FUNC(sc);
11414255736Sdavidch    rxq_init->mcast_engine_id = SC_FUNC(sc);
11415255736Sdavidch
11416255736Sdavidch    /*
11417255736Sdavidch     * Maximum number or simultaneous TPA aggregation for this Queue.
11418255736Sdavidch     * For PF Clients it should be the maximum available number.
11419255736Sdavidch     * VF driver(s) may want to define it to a smaller value.
11420255736Sdavidch     */
11421255736Sdavidch    rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11422255736Sdavidch
11423255736Sdavidch    rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11424255736Sdavidch    rxq_init->fw_sb_id = fp->fw_sb_id;
11425255736Sdavidch
11426255736Sdavidch    rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11427255736Sdavidch
11428255736Sdavidch    /*
11429255736Sdavidch     * configure silent vlan removal
11430255736Sdavidch     * if multi function mode is afex, then mask default vlan
11431255736Sdavidch     */
11432255736Sdavidch    if (IS_MF_AFEX(sc)) {
11433255736Sdavidch        rxq_init->silent_removal_value =
11434255736Sdavidch            sc->devinfo.mf_info.afex_def_vlan_tag;
11435255736Sdavidch        rxq_init->silent_removal_mask = EVL_VLID_MASK;
11436255736Sdavidch    }
11437255736Sdavidch}
11438255736Sdavidch
11439255736Sdavidchstatic void
11440255736Sdavidchbxe_pf_tx_q_prep(struct bxe_softc              *sc,
11441255736Sdavidch                 struct bxe_fastpath           *fp,
11442255736Sdavidch                 struct ecore_txq_setup_params *txq_init,
11443255736Sdavidch                 uint8_t                       cos)
11444255736Sdavidch{
11445255736Sdavidch    /*
11446255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
11447255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11448255736Sdavidch     * fp->txdata[cos]->tx_dma.paddr;
11449255736Sdavidch     */
11450255736Sdavidch    txq_init->dscr_map     = fp->tx_dma.paddr;
11451255736Sdavidch    txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11452255736Sdavidch    txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11453255736Sdavidch    txq_init->fw_sb_id     = fp->fw_sb_id;
11454255736Sdavidch
11455255736Sdavidch    /*
11456255736Sdavidch     * set the TSS leading client id for TX classfication to the
11457255736Sdavidch     * leading RSS client id
11458255736Sdavidch     */
11459255736Sdavidch    txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11460255736Sdavidch}
11461255736Sdavidch
11462255736Sdavidch/*
11463255736Sdavidch * This function performs 2 steps in a queue state machine:
11464255736Sdavidch *   1) RESET->INIT
11465255736Sdavidch *   2) INIT->SETUP
11466255736Sdavidch */
11467255736Sdavidchstatic int
11468255736Sdavidchbxe_setup_queue(struct bxe_softc    *sc,
11469255736Sdavidch                struct bxe_fastpath *fp,
11470255736Sdavidch                uint8_t             leading)
11471255736Sdavidch{
11472255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
11473255736Sdavidch    struct ecore_queue_setup_params *setup_params =
11474255736Sdavidch                        &q_params.params.setup;
11475255736Sdavidch    int rc;
11476255736Sdavidch
11477255736Sdavidch    BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11478255736Sdavidch
11479255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11480255736Sdavidch
11481255736Sdavidch    q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11482255736Sdavidch
11483255736Sdavidch    /* we want to wait for completion in this context */
11484255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11485255736Sdavidch
11486255736Sdavidch    /* prepare the INIT parameters */
11487255736Sdavidch    bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11488255736Sdavidch
11489255736Sdavidch    /* Set the command */
11490255736Sdavidch    q_params.cmd = ECORE_Q_CMD_INIT;
11491255736Sdavidch
11492255736Sdavidch    /* Change the state to INIT */
11493255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11494255736Sdavidch    if (rc) {
11495295830Sdavidcs        BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11496255736Sdavidch        return (rc);
11497255736Sdavidch    }
11498255736Sdavidch
11499255736Sdavidch    BLOGD(sc, DBG_LOAD, "init complete\n");
11500255736Sdavidch
11501255736Sdavidch    /* now move the Queue to the SETUP state */
11502255736Sdavidch    memset(setup_params, 0, sizeof(*setup_params));
11503255736Sdavidch
11504255736Sdavidch    /* set Queue flags */
11505255736Sdavidch    setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11506255736Sdavidch
11507255736Sdavidch    /* set general SETUP parameters */
11508255736Sdavidch    bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11509255736Sdavidch                          FIRST_TX_COS_INDEX);
11510255736Sdavidch
11511255736Sdavidch    bxe_pf_rx_q_prep(sc, fp,
11512255736Sdavidch                     &setup_params->pause_params,
11513255736Sdavidch                     &setup_params->rxq_params);
11514255736Sdavidch
11515255736Sdavidch    bxe_pf_tx_q_prep(sc, fp,
11516255736Sdavidch                     &setup_params->txq_params,
11517255736Sdavidch                     FIRST_TX_COS_INDEX);
11518255736Sdavidch
11519255736Sdavidch    /* Set the command */
11520255736Sdavidch    q_params.cmd = ECORE_Q_CMD_SETUP;
11521255736Sdavidch
11522255736Sdavidch    /* change the state to SETUP */
11523255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11524255736Sdavidch    if (rc) {
11525295830Sdavidcs        BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11526255736Sdavidch        return (rc);
11527255736Sdavidch    }
11528255736Sdavidch
11529255736Sdavidch    return (rc);
11530255736Sdavidch}
11531255736Sdavidch
11532255736Sdavidchstatic int
11533255736Sdavidchbxe_setup_leading(struct bxe_softc *sc)
11534255736Sdavidch{
11535255736Sdavidch    return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11536255736Sdavidch}
11537255736Sdavidch
11538255736Sdavidchstatic int
11539255736Sdavidchbxe_config_rss_pf(struct bxe_softc            *sc,
11540255736Sdavidch                  struct ecore_rss_config_obj *rss_obj,
11541255736Sdavidch                  uint8_t                     config_hash)
11542255736Sdavidch{
11543255736Sdavidch    struct ecore_config_rss_params params = { NULL };
11544255736Sdavidch    int i;
11545255736Sdavidch
11546255736Sdavidch    /*
11547255736Sdavidch     * Although RSS is meaningless when there is a single HW queue we
11548255736Sdavidch     * still need it enabled in order to have HW Rx hash generated.
11549255736Sdavidch     */
11550255736Sdavidch
11551255736Sdavidch    params.rss_obj = rss_obj;
11552255736Sdavidch
11553255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11554255736Sdavidch
11555255736Sdavidch    bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11556255736Sdavidch
11557255736Sdavidch    /* RSS configuration */
11558255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11559255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11560255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11561255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11562255736Sdavidch    if (rss_obj->udp_rss_v4) {
11563255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11564255736Sdavidch    }
11565255736Sdavidch    if (rss_obj->udp_rss_v6) {
11566255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11567255736Sdavidch    }
11568255736Sdavidch
11569255736Sdavidch    /* Hash bits */
11570255736Sdavidch    params.rss_result_mask = MULTI_MASK;
11571255736Sdavidch
11572255736Sdavidch    memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11573255736Sdavidch
11574255736Sdavidch    if (config_hash) {
11575255736Sdavidch        /* RSS keys */
11576255736Sdavidch        for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11577255736Sdavidch            params.rss_key[i] = arc4random();
11578255736Sdavidch        }
11579255736Sdavidch
11580255736Sdavidch        bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11581255736Sdavidch    }
11582255736Sdavidch
11583255736Sdavidch    return (ecore_config_rss(sc, &params));
11584255736Sdavidch}
11585255736Sdavidch
11586255736Sdavidchstatic int
11587255736Sdavidchbxe_config_rss_eth(struct bxe_softc *sc,
11588255736Sdavidch                   uint8_t          config_hash)
11589255736Sdavidch{
11590255736Sdavidch    return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11591255736Sdavidch}
11592255736Sdavidch
11593255736Sdavidchstatic int
11594255736Sdavidchbxe_init_rss_pf(struct bxe_softc *sc)
11595255736Sdavidch{
11596255736Sdavidch    uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11597255736Sdavidch    int i;
11598255736Sdavidch
11599255736Sdavidch    /*
11600255736Sdavidch     * Prepare the initial contents of the indirection table if
11601255736Sdavidch     * RSS is enabled
11602255736Sdavidch     */
11603255736Sdavidch    for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11604255736Sdavidch        sc->rss_conf_obj.ind_table[i] =
11605255736Sdavidch            (sc->fp->cl_id + (i % num_eth_queues));
11606255736Sdavidch    }
11607255736Sdavidch
11608255736Sdavidch    if (sc->udp_rss) {
11609255736Sdavidch        sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11610255736Sdavidch    }
11611255736Sdavidch
11612255736Sdavidch    /*
11613255736Sdavidch     * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11614255736Sdavidch     * per-port, so if explicit configuration is needed, do it only
11615255736Sdavidch     * for a PMF.
11616255736Sdavidch     *
11617255736Sdavidch     * For 57712 and newer it's a per-function configuration.
11618255736Sdavidch     */
11619255736Sdavidch    return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11620255736Sdavidch}
11621255736Sdavidch
11622255736Sdavidchstatic int
11623255736Sdavidchbxe_set_mac_one(struct bxe_softc          *sc,
11624255736Sdavidch                uint8_t                   *mac,
11625255736Sdavidch                struct ecore_vlan_mac_obj *obj,
11626255736Sdavidch                uint8_t                   set,
11627255736Sdavidch                int                       mac_type,
11628255736Sdavidch                unsigned long             *ramrod_flags)
11629255736Sdavidch{
11630255736Sdavidch    struct ecore_vlan_mac_ramrod_params ramrod_param;
11631255736Sdavidch    int rc;
11632255736Sdavidch
11633255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
11634255736Sdavidch
11635255736Sdavidch    /* fill in general parameters */
11636255736Sdavidch    ramrod_param.vlan_mac_obj = obj;
11637255736Sdavidch    ramrod_param.ramrod_flags = *ramrod_flags;
11638255736Sdavidch
11639255736Sdavidch    /* fill a user request section if needed */
11640255736Sdavidch    if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11641255736Sdavidch        memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11642255736Sdavidch
11643255736Sdavidch        bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11644255736Sdavidch
11645255736Sdavidch        /* Set the command: ADD or DEL */
11646255736Sdavidch        ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11647255736Sdavidch                                            ECORE_VLAN_MAC_DEL;
11648255736Sdavidch    }
11649255736Sdavidch
11650255736Sdavidch    rc = ecore_config_vlan_mac(sc, &ramrod_param);
11651255736Sdavidch
11652255736Sdavidch    if (rc == ECORE_EXISTS) {
11653255736Sdavidch        BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11654255736Sdavidch        /* do not treat adding same MAC as error */
11655255736Sdavidch        rc = 0;
11656255736Sdavidch    } else if (rc < 0) {
11657255736Sdavidch        BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11658255736Sdavidch    }
11659255736Sdavidch
11660255736Sdavidch    return (rc);
11661255736Sdavidch}
11662255736Sdavidch
11663255736Sdavidchstatic int
11664255736Sdavidchbxe_set_eth_mac(struct bxe_softc *sc,
11665255736Sdavidch                uint8_t          set)
11666255736Sdavidch{
11667255736Sdavidch    unsigned long ramrod_flags = 0;
11668255736Sdavidch
11669255736Sdavidch    BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11670255736Sdavidch
11671255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11672255736Sdavidch
11673255736Sdavidch    /* Eth MAC is set on RSS leading client (fp[0]) */
11674255736Sdavidch    return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11675255736Sdavidch                            &sc->sp_objs->mac_obj,
11676255736Sdavidch                            set, ECORE_ETH_MAC, &ramrod_flags));
11677255736Sdavidch}
11678255736Sdavidch
11679255736Sdavidchstatic int
11680255736Sdavidchbxe_get_cur_phy_idx(struct bxe_softc *sc)
11681255736Sdavidch{
11682255736Sdavidch    uint32_t sel_phy_idx = 0;
11683255736Sdavidch
11684255736Sdavidch    if (sc->link_params.num_phys <= 1) {
11685255736Sdavidch        return (ELINK_INT_PHY);
11686255736Sdavidch    }
11687255736Sdavidch
11688255736Sdavidch    if (sc->link_vars.link_up) {
11689255736Sdavidch        sel_phy_idx = ELINK_EXT_PHY1;
11690255736Sdavidch        /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11691255736Sdavidch        if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11692255736Sdavidch            (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11693255736Sdavidch             ELINK_SUPPORTED_FIBRE))
11694255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11695255736Sdavidch    } else {
11696255736Sdavidch        switch (elink_phy_selection(&sc->link_params)) {
11697255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11698255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11699255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11700255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY1;
11701255736Sdavidch               break;
11702255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11703255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11704255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY2;
11705255736Sdavidch               break;
11706255736Sdavidch        }
11707255736Sdavidch    }
11708255736Sdavidch
11709255736Sdavidch    return (sel_phy_idx);
11710255736Sdavidch}
11711255736Sdavidch
11712255736Sdavidchstatic int
11713255736Sdavidchbxe_get_link_cfg_idx(struct bxe_softc *sc)
11714255736Sdavidch{
11715255736Sdavidch    uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11716255736Sdavidch
11717255736Sdavidch    /*
11718255736Sdavidch     * The selected activated PHY is always after swapping (in case PHY
11719255736Sdavidch     * swapping is enabled). So when swapping is enabled, we need to reverse
11720255736Sdavidch     * the configuration
11721255736Sdavidch     */
11722255736Sdavidch
11723255736Sdavidch    if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11724255736Sdavidch        if (sel_phy_idx == ELINK_EXT_PHY1)
11725255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11726255736Sdavidch        else if (sel_phy_idx == ELINK_EXT_PHY2)
11727255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY1;
11728255736Sdavidch    }
11729255736Sdavidch
11730255736Sdavidch    return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11731255736Sdavidch}
11732255736Sdavidch
11733255736Sdavidchstatic void
11734255736Sdavidchbxe_set_requested_fc(struct bxe_softc *sc)
11735255736Sdavidch{
11736255736Sdavidch    /*
11737255736Sdavidch     * Initialize link parameters structure variables
11738255736Sdavidch     * It is recommended to turn off RX FC for jumbo frames
11739255736Sdavidch     * for better performance
11740255736Sdavidch     */
11741255736Sdavidch    if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11742255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11743255736Sdavidch    } else {
11744255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11745255736Sdavidch    }
11746255736Sdavidch}
11747255736Sdavidch
11748255736Sdavidchstatic void
11749255736Sdavidchbxe_calc_fc_adv(struct bxe_softc *sc)
11750255736Sdavidch{
11751255736Sdavidch    uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11752315881Sdavidcs
11753315881Sdavidcs
11754315881Sdavidcs    sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11755315881Sdavidcs                                           ADVERTISED_Pause);
11756315881Sdavidcs
11757255736Sdavidch    switch (sc->link_vars.ieee_fc &
11758255736Sdavidch            MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11759255736Sdavidch
11760255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11761255736Sdavidch        sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11762255736Sdavidch                                          ADVERTISED_Pause);
11763255736Sdavidch        break;
11764255736Sdavidch
11765255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11766255736Sdavidch        sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11767255736Sdavidch        break;
11768315881Sdavidcs
11769315881Sdavidcs    default:
11770315881Sdavidcs        break;
11771315881Sdavidcs
11772255736Sdavidch    }
11773255736Sdavidch}
11774255736Sdavidch
11775255736Sdavidchstatic uint16_t
11776255736Sdavidchbxe_get_mf_speed(struct bxe_softc *sc)
11777255736Sdavidch{
11778255736Sdavidch    uint16_t line_speed = sc->link_vars.line_speed;
11779255736Sdavidch    if (IS_MF(sc)) {
11780255736Sdavidch        uint16_t maxCfg =
11781255736Sdavidch            bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11782255736Sdavidch
11783255736Sdavidch        /* calculate the current MAX line speed limit for the MF devices */
11784255736Sdavidch        if (IS_MF_SI(sc)) {
11785255736Sdavidch            line_speed = (line_speed * maxCfg) / 100;
11786255736Sdavidch        } else { /* SD mode */
11787255736Sdavidch            uint16_t vn_max_rate = maxCfg * 100;
11788255736Sdavidch
11789255736Sdavidch            if (vn_max_rate < line_speed) {
11790255736Sdavidch                line_speed = vn_max_rate;
11791255736Sdavidch            }
11792255736Sdavidch        }
11793255736Sdavidch    }
11794255736Sdavidch
11795255736Sdavidch    return (line_speed);
11796255736Sdavidch}
11797255736Sdavidch
11798255736Sdavidchstatic void
11799255736Sdavidchbxe_fill_report_data(struct bxe_softc            *sc,
11800255736Sdavidch                     struct bxe_link_report_data *data)
11801255736Sdavidch{
11802255736Sdavidch    uint16_t line_speed = bxe_get_mf_speed(sc);
11803255736Sdavidch
11804255736Sdavidch    memset(data, 0, sizeof(*data));
11805255736Sdavidch
11806255736Sdavidch    /* fill the report data with the effective line speed */
11807255736Sdavidch    data->line_speed = line_speed;
11808255736Sdavidch
11809255736Sdavidch    /* Link is down */
11810255736Sdavidch    if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11811255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11812255736Sdavidch    }
11813255736Sdavidch
11814255736Sdavidch    /* Full DUPLEX */
11815255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
11816255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11817255736Sdavidch    }
11818255736Sdavidch
11819255736Sdavidch    /* Rx Flow Control is ON */
11820255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11821255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11822255736Sdavidch    }
11823255736Sdavidch
11824255736Sdavidch    /* Tx Flow Control is ON */
11825255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11826255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11827255736Sdavidch    }
11828255736Sdavidch}
11829255736Sdavidch
11830255736Sdavidch/* report link status to OS, should be called under phy_lock */
11831255736Sdavidchstatic void
11832255736Sdavidchbxe_link_report_locked(struct bxe_softc *sc)
11833255736Sdavidch{
11834255736Sdavidch    struct bxe_link_report_data cur_data;
11835255736Sdavidch
11836255736Sdavidch    /* reread mf_cfg */
11837255736Sdavidch    if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11838255736Sdavidch        bxe_read_mf_cfg(sc);
11839255736Sdavidch    }
11840255736Sdavidch
11841255736Sdavidch    /* Read the current link report info */
11842255736Sdavidch    bxe_fill_report_data(sc, &cur_data);
11843255736Sdavidch
11844255736Sdavidch    /* Don't report link down or exactly the same link status twice */
11845255736Sdavidch    if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11846255736Sdavidch        (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11847255736Sdavidch                      &sc->last_reported_link.link_report_flags) &&
11848255736Sdavidch         bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11849255736Sdavidch                      &cur_data.link_report_flags))) {
11850255736Sdavidch        return;
11851255736Sdavidch    }
11852255736Sdavidch
11853315881Sdavidcs	ELINK_DEBUG_P2(sc, "Change in link status : cur_data = %x, last_reported_link = %x\n",
11854315881Sdavidcs					cur_data.link_report_flags, sc->last_reported_link.link_report_flags);
11855255736Sdavidch    sc->link_cnt++;
11856255736Sdavidch
11857315881Sdavidcs	ELINK_DEBUG_P1(sc, "link status change count = %x\n", sc->link_cnt);
11858255736Sdavidch    /* report new link params and remember the state for the next time */
11859255736Sdavidch    memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11860255736Sdavidch
11861255736Sdavidch    if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11862255736Sdavidch                     &cur_data.link_report_flags)) {
11863270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_DOWN);
11864255736Sdavidch    } else {
11865255736Sdavidch        const char *duplex;
11866255736Sdavidch        const char *flow;
11867255736Sdavidch
11868255736Sdavidch        if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11869255736Sdavidch                                   &cur_data.link_report_flags)) {
11870255736Sdavidch            duplex = "full";
11871315881Sdavidcs			ELINK_DEBUG_P0(sc, "link set to full duplex\n");
11872255736Sdavidch        } else {
11873255736Sdavidch            duplex = "half";
11874315881Sdavidcs			ELINK_DEBUG_P0(sc, "link set to half duplex\n");
11875255736Sdavidch        }
11876255736Sdavidch
11877255736Sdavidch        /*
11878255736Sdavidch         * Handle the FC at the end so that only these flags would be
11879255736Sdavidch         * possibly set. This way we may easily check if there is no FC
11880255736Sdavidch         * enabled.
11881255736Sdavidch         */
11882255736Sdavidch        if (cur_data.link_report_flags) {
11883255736Sdavidch            if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11884255736Sdavidch                             &cur_data.link_report_flags) &&
11885255736Sdavidch                bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11886255736Sdavidch                             &cur_data.link_report_flags)) {
11887255736Sdavidch                flow = "ON - receive & transmit";
11888255736Sdavidch            } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11889255736Sdavidch                                    &cur_data.link_report_flags) &&
11890255736Sdavidch                       !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11891255736Sdavidch                                     &cur_data.link_report_flags)) {
11892255736Sdavidch                flow = "ON - receive";
11893255736Sdavidch            } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11894255736Sdavidch                                     &cur_data.link_report_flags) &&
11895255736Sdavidch                       bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11896255736Sdavidch                                    &cur_data.link_report_flags)) {
11897255736Sdavidch                flow = "ON - transmit";
11898255736Sdavidch            } else {
11899255736Sdavidch                flow = "none"; /* possible? */
11900255736Sdavidch            }
11901255736Sdavidch        } else {
11902255736Sdavidch            flow = "none";
11903255736Sdavidch        }
11904255736Sdavidch
11905270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_UP);
11906255736Sdavidch        BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11907255736Sdavidch              cur_data.line_speed, duplex, flow);
11908255736Sdavidch    }
11909255736Sdavidch}
11910255736Sdavidch
11911255736Sdavidchstatic void
11912255736Sdavidchbxe_link_report(struct bxe_softc *sc)
11913255736Sdavidch{
11914284335Sdavidcs    bxe_acquire_phy_lock(sc);
11915255736Sdavidch    bxe_link_report_locked(sc);
11916284335Sdavidcs    bxe_release_phy_lock(sc);
11917255736Sdavidch}
11918255736Sdavidch
11919255736Sdavidchstatic void
11920255736Sdavidchbxe_link_status_update(struct bxe_softc *sc)
11921255736Sdavidch{
11922255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
11923255736Sdavidch        return;
11924255736Sdavidch    }
11925255736Sdavidch
11926255736Sdavidch    if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11927255736Sdavidch        elink_link_status_update(&sc->link_params, &sc->link_vars);
11928255736Sdavidch    } else {
11929255736Sdavidch        sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11930255736Sdavidch                                  ELINK_SUPPORTED_10baseT_Full |
11931255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Half |
11932255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Full |
11933255736Sdavidch                                  ELINK_SUPPORTED_1000baseT_Full |
11934255736Sdavidch                                  ELINK_SUPPORTED_2500baseX_Full |
11935255736Sdavidch                                  ELINK_SUPPORTED_10000baseT_Full |
11936255736Sdavidch                                  ELINK_SUPPORTED_TP |
11937255736Sdavidch                                  ELINK_SUPPORTED_FIBRE |
11938255736Sdavidch                                  ELINK_SUPPORTED_Autoneg |
11939255736Sdavidch                                  ELINK_SUPPORTED_Pause |
11940255736Sdavidch                                  ELINK_SUPPORTED_Asym_Pause);
11941255736Sdavidch        sc->port.advertising[0] = sc->port.supported[0];
11942255736Sdavidch
11943255736Sdavidch        sc->link_params.sc                = sc;
11944255736Sdavidch        sc->link_params.port              = SC_PORT(sc);
11945255736Sdavidch        sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11946255736Sdavidch        sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11947255736Sdavidch        sc->link_params.req_line_speed[0] = SPEED_10000;
11948255736Sdavidch        sc->link_params.speed_cap_mask[0] = 0x7f0000;
11949255736Sdavidch        sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11950255736Sdavidch
11951255736Sdavidch        if (CHIP_REV_IS_FPGA(sc)) {
11952255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11953255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_1000;
11954255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11955255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11956255736Sdavidch        } else {
11957255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11958255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_10000;
11959255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11960255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11961255736Sdavidch        }
11962255736Sdavidch
11963255736Sdavidch        sc->link_vars.link_up = 1;
11964255736Sdavidch
11965255736Sdavidch        sc->link_vars.duplex    = DUPLEX_FULL;
11966255736Sdavidch        sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11967255736Sdavidch
11968255736Sdavidch        if (IS_PF(sc)) {
11969255736Sdavidch            REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11970255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11971255736Sdavidch            bxe_link_report(sc);
11972255736Sdavidch        }
11973255736Sdavidch    }
11974255736Sdavidch
11975255736Sdavidch    if (IS_PF(sc)) {
11976255736Sdavidch        if (sc->link_vars.link_up) {
11977255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11978255736Sdavidch        } else {
11979255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_STOP);
11980255736Sdavidch        }
11981255736Sdavidch        bxe_link_report(sc);
11982255736Sdavidch    } else {
11983255736Sdavidch        bxe_link_report(sc);
11984255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11985255736Sdavidch    }
11986255736Sdavidch}
11987255736Sdavidch
11988255736Sdavidchstatic int
11989255736Sdavidchbxe_initial_phy_init(struct bxe_softc *sc,
11990255736Sdavidch                     int              load_mode)
11991255736Sdavidch{
11992255736Sdavidch    int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
11993255736Sdavidch    uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
11994255736Sdavidch    struct elink_params *lp = &sc->link_params;
11995255736Sdavidch
11996255736Sdavidch    bxe_set_requested_fc(sc);
11997255736Sdavidch
11998255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
11999255736Sdavidch        uint32_t bond = CHIP_BOND_ID(sc);
12000255736Sdavidch        uint32_t feat = 0;
12001255736Sdavidch
12002255736Sdavidch        if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
12003255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12004255736Sdavidch        } else if (bond & 0x4) {
12005255736Sdavidch            if (CHIP_IS_E3(sc)) {
12006255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
12007255736Sdavidch            } else {
12008255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12009255736Sdavidch            }
12010255736Sdavidch        } else if (bond & 0x8) {
12011255736Sdavidch            if (CHIP_IS_E3(sc)) {
12012255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
12013255736Sdavidch            } else {
12014255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12015255736Sdavidch            }
12016255736Sdavidch        }
12017255736Sdavidch
12018255736Sdavidch        /* disable EMAC for E3 and above */
12019255736Sdavidch        if (bond & 0x2) {
12020255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12021255736Sdavidch        }
12022255736Sdavidch
12023255736Sdavidch        sc->link_params.feature_config_flags |= feat;
12024255736Sdavidch    }
12025255736Sdavidch
12026284335Sdavidcs    bxe_acquire_phy_lock(sc);
12027255736Sdavidch
12028255736Sdavidch    if (load_mode == LOAD_DIAG) {
12029255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_XGXS;
12030255736Sdavidch        /* Prefer doing PHY loopback at 10G speed, if possible */
12031255736Sdavidch        if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
12032255736Sdavidch            if (lp->speed_cap_mask[cfg_idx] &
12033255736Sdavidch                PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
12034255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
12035255736Sdavidch            } else {
12036255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
12037255736Sdavidch            }
12038255736Sdavidch        }
12039255736Sdavidch    }
12040255736Sdavidch
12041255736Sdavidch    if (load_mode == LOAD_LOOPBACK_EXT) {
12042255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_EXT;
12043255736Sdavidch    }
12044255736Sdavidch
12045255736Sdavidch    rc = elink_phy_init(&sc->link_params, &sc->link_vars);
12046255736Sdavidch
12047284335Sdavidcs    bxe_release_phy_lock(sc);
12048255736Sdavidch
12049255736Sdavidch    bxe_calc_fc_adv(sc);
12050255736Sdavidch
12051255736Sdavidch    if (sc->link_vars.link_up) {
12052255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12053255736Sdavidch        bxe_link_report(sc);
12054255736Sdavidch    }
12055255736Sdavidch
12056255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12057255736Sdavidch        bxe_periodic_start(sc);
12058255736Sdavidch    }
12059255736Sdavidch
12060255736Sdavidch    sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
12061255736Sdavidch    return (rc);
12062255736Sdavidch}
12063255736Sdavidch
12064255736Sdavidch/* must be called under IF_ADDR_LOCK */
12065255736Sdavidchstatic int
12066336146Sdavidcsbxe_init_mcast_macs_list(struct bxe_softc                 *sc,
12067336146Sdavidcs                         struct ecore_mcast_ramrod_params *p)
12068255736Sdavidch{
12069336146Sdavidcs    if_t ifp = sc->ifp;
12070255736Sdavidch    int mc_count = 0;
12071336146Sdavidcs    struct ifmultiaddr *ifma;
12072336146Sdavidcs    struct ecore_mcast_list_elem *mc_mac;
12073255736Sdavidch
12074336146Sdavidcs    TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
12075336146Sdavidcs        if (ifma->ifma_addr->sa_family != AF_LINK) {
12076336146Sdavidcs            continue;
12077336146Sdavidcs        }
12078255736Sdavidch
12079336146Sdavidcs        mc_count++;
12080336146Sdavidcs    }
12081266979Smarcel
12082336146Sdavidcs    ECORE_LIST_INIT(&p->mcast_list);
12083336146Sdavidcs    p->mcast_list_len = 0;
12084336146Sdavidcs
12085336146Sdavidcs    if (!mc_count) {
12086336146Sdavidcs        return (0);
12087266979Smarcel    }
12088284651Sdavidcs
12089336146Sdavidcs    mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF,
12090336146Sdavidcs                    (M_NOWAIT | M_ZERO));
12091255736Sdavidch    if (!mc_mac) {
12092255736Sdavidch        BLOGE(sc, "Failed to allocate temp mcast list\n");
12093255736Sdavidch        return (-1);
12094255736Sdavidch    }
12095284470Sdavidcs    bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12096255736Sdavidch
12097336146Sdavidcs    TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
12098336146Sdavidcs        if (ifma->ifma_addr->sa_family != AF_LINK) {
12099336146Sdavidcs            continue;
12100336146Sdavidcs        }
12101284651Sdavidcs
12102336146Sdavidcs        mc_mac->mac = (uint8_t *)LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
12103336146Sdavidcs        ECORE_LIST_PUSH_TAIL(&mc_mac->link, &p->mcast_list);
12104284651Sdavidcs
12105336146Sdavidcs        BLOGD(sc, DBG_LOAD,
12106336146Sdavidcs              "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X and mc_count %d\n",
12107336146Sdavidcs              mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12108336146Sdavidcs              mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5], mc_count);
12109336146Sdavidcs       mc_mac++;
12110336146Sdavidcs    }
12111284651Sdavidcs
12112336146Sdavidcs    p->mcast_list_len = mc_count;
12113255736Sdavidch
12114336146Sdavidcs    return (0);
12115336146Sdavidcs}
12116255736Sdavidch
12117336146Sdavidcsstatic void
12118336146Sdavidcsbxe_free_mcast_macs_list(struct ecore_mcast_ramrod_params *p)
12119336146Sdavidcs{
12120336146Sdavidcs    struct ecore_mcast_list_elem *mc_mac =
12121336146Sdavidcs        ECORE_LIST_FIRST_ENTRY(&p->mcast_list,
12122336146Sdavidcs                               struct ecore_mcast_list_elem,
12123336146Sdavidcs                               link);
12124255736Sdavidch
12125336146Sdavidcs    if (mc_mac) {
12126336146Sdavidcs        /* only a single free as all mc_macs are in the same heap array */
12127336146Sdavidcs        free(mc_mac, M_DEVBUF);
12128255736Sdavidch    }
12129336146Sdavidcs}
12130336146Sdavidcsstatic int
12131336146Sdavidcsbxe_set_mc_list(struct bxe_softc *sc)
12132336146Sdavidcs{
12133336146Sdavidcs    struct ecore_mcast_ramrod_params rparam = { NULL };
12134336146Sdavidcs    int rc = 0;
12135255736Sdavidch
12136336146Sdavidcs    rparam.mcast_obj = &sc->mcast_obj;
12137336146Sdavidcs
12138255736Sdavidch    BXE_MCAST_LOCK(sc);
12139255736Sdavidch
12140255736Sdavidch    /* first, clear all configured multicast MACs */
12141255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12142255736Sdavidch    if (rc < 0) {
12143255736Sdavidch        BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12144336146Sdavidcs        /* Manual backport parts of FreeBSD upstream r284470. */
12145284470Sdavidcs        BXE_MCAST_UNLOCK(sc);
12146255736Sdavidch        return (rc);
12147255736Sdavidch    }
12148255736Sdavidch
12149336146Sdavidcs    /* configure a new MACs list */
12150336146Sdavidcs    rc = bxe_init_mcast_macs_list(sc, &rparam);
12151336146Sdavidcs    if (rc) {
12152336146Sdavidcs        BLOGE(sc, "Failed to create mcast MACs list (%d)\n", rc);
12153336146Sdavidcs        BXE_MCAST_UNLOCK(sc);
12154336146Sdavidcs        return (rc);
12155336146Sdavidcs    }
12156336146Sdavidcs
12157255736Sdavidch    /* Now add the new MACs */
12158255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12159255736Sdavidch    if (rc < 0) {
12160255736Sdavidch        BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12161255736Sdavidch    }
12162255736Sdavidch
12163336146Sdavidcs    bxe_free_mcast_macs_list(&rparam);
12164336146Sdavidcs
12165255736Sdavidch    BXE_MCAST_UNLOCK(sc);
12166255736Sdavidch
12167255736Sdavidch    return (rc);
12168255736Sdavidch}
12169255736Sdavidch
12170255736Sdavidchstatic int
12171255736Sdavidchbxe_set_uc_list(struct bxe_softc *sc)
12172255736Sdavidch{
12173266979Smarcel    if_t ifp = sc->ifp;
12174255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12175255736Sdavidch    struct ifaddr *ifa;
12176255736Sdavidch    unsigned long ramrod_flags = 0;
12177255736Sdavidch    int rc;
12178255736Sdavidch
12179255736Sdavidch#if __FreeBSD_version < 800000
12180255736Sdavidch    IF_ADDR_LOCK(ifp);
12181255736Sdavidch#else
12182270876Sglebius    if_addr_rlock(ifp);
12183255736Sdavidch#endif
12184255736Sdavidch
12185255736Sdavidch    /* first schedule a cleanup up of old configuration */
12186255736Sdavidch    rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12187255736Sdavidch    if (rc < 0) {
12188255736Sdavidch        BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12189255736Sdavidch#if __FreeBSD_version < 800000
12190255736Sdavidch        IF_ADDR_UNLOCK(ifp);
12191255736Sdavidch#else
12192270876Sglebius        if_addr_runlock(ifp);
12193255736Sdavidch#endif
12194255736Sdavidch        return (rc);
12195255736Sdavidch    }
12196255736Sdavidch
12197266979Smarcel    ifa = if_getifaddr(ifp); /* XXX Is this structure */
12198255736Sdavidch    while (ifa) {
12199255736Sdavidch        if (ifa->ifa_addr->sa_family != AF_LINK) {
12200255736Sdavidch            ifa = TAILQ_NEXT(ifa, ifa_link);
12201255736Sdavidch            continue;
12202255736Sdavidch        }
12203255736Sdavidch
12204255736Sdavidch        rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12205255736Sdavidch                             mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12206255736Sdavidch        if (rc == -EEXIST) {
12207255736Sdavidch            BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12208255736Sdavidch            /* do not treat adding same MAC as an error */
12209255736Sdavidch            rc = 0;
12210255736Sdavidch        } else if (rc < 0) {
12211255736Sdavidch            BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12212255736Sdavidch#if __FreeBSD_version < 800000
12213255736Sdavidch            IF_ADDR_UNLOCK(ifp);
12214255736Sdavidch#else
12215270876Sglebius            if_addr_runlock(ifp);
12216255736Sdavidch#endif
12217255736Sdavidch            return (rc);
12218255736Sdavidch        }
12219255736Sdavidch
12220255736Sdavidch        ifa = TAILQ_NEXT(ifa, ifa_link);
12221255736Sdavidch    }
12222255736Sdavidch
12223255736Sdavidch#if __FreeBSD_version < 800000
12224255736Sdavidch    IF_ADDR_UNLOCK(ifp);
12225255736Sdavidch#else
12226270876Sglebius    if_addr_runlock(ifp);
12227255736Sdavidch#endif
12228255736Sdavidch
12229255736Sdavidch    /* Execute the pending commands */
12230255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
12231255736Sdavidch    return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12232255736Sdavidch                            ECORE_UC_LIST_MAC, &ramrod_flags));
12233255736Sdavidch}
12234255736Sdavidch
12235255736Sdavidchstatic void
12236284335Sdavidcsbxe_set_rx_mode(struct bxe_softc *sc)
12237255736Sdavidch{
12238266979Smarcel    if_t ifp = sc->ifp;
12239255736Sdavidch    uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12240255736Sdavidch
12241255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
12242255736Sdavidch        BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12243255736Sdavidch        return;
12244255736Sdavidch    }
12245255736Sdavidch
12246266979Smarcel    BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12247255736Sdavidch
12248266979Smarcel    if (if_getflags(ifp) & IFF_PROMISC) {
12249255736Sdavidch        rx_mode = BXE_RX_MODE_PROMISC;
12250266979Smarcel    } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12251266979Smarcel               ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12252255736Sdavidch                CHIP_IS_E1(sc))) {
12253255736Sdavidch        rx_mode = BXE_RX_MODE_ALLMULTI;
12254255736Sdavidch    } else {
12255255736Sdavidch        if (IS_PF(sc)) {
12256255736Sdavidch            /* some multicasts */
12257255736Sdavidch            if (bxe_set_mc_list(sc) < 0) {
12258255736Sdavidch                rx_mode = BXE_RX_MODE_ALLMULTI;
12259255736Sdavidch            }
12260255736Sdavidch            if (bxe_set_uc_list(sc) < 0) {
12261255736Sdavidch                rx_mode = BXE_RX_MODE_PROMISC;
12262255736Sdavidch            }
12263255736Sdavidch        }
12264255736Sdavidch    }
12265255736Sdavidch
12266255736Sdavidch    sc->rx_mode = rx_mode;
12267255736Sdavidch
12268255736Sdavidch    /* schedule the rx_mode command */
12269255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12270255736Sdavidch        BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12271255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12272255736Sdavidch        return;
12273255736Sdavidch    }
12274255736Sdavidch
12275255736Sdavidch    if (IS_PF(sc)) {
12276255736Sdavidch        bxe_set_storm_rx_mode(sc);
12277255736Sdavidch    }
12278255736Sdavidch}
12279255736Sdavidch
12280255736Sdavidch
12281255736Sdavidch/* update flags in shmem */
12282255736Sdavidchstatic void
12283255736Sdavidchbxe_update_drv_flags(struct bxe_softc *sc,
12284255736Sdavidch                     uint32_t         flags,
12285255736Sdavidch                     uint32_t         set)
12286255736Sdavidch{
12287255736Sdavidch    uint32_t drv_flags;
12288255736Sdavidch
12289255736Sdavidch    if (SHMEM2_HAS(sc, drv_flags)) {
12290255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12291255736Sdavidch        drv_flags = SHMEM2_RD(sc, drv_flags);
12292255736Sdavidch
12293255736Sdavidch        if (set) {
12294255736Sdavidch            SET_FLAGS(drv_flags, flags);
12295255736Sdavidch        } else {
12296255736Sdavidch            RESET_FLAGS(drv_flags, flags);
12297255736Sdavidch        }
12298255736Sdavidch
12299255736Sdavidch        SHMEM2_WR(sc, drv_flags, drv_flags);
12300255736Sdavidch        BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12301255736Sdavidch
12302255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12303255736Sdavidch    }
12304255736Sdavidch}
12305255736Sdavidch
12306255736Sdavidch/* periodic timer callout routine, only runs when the interface is up */
12307255736Sdavidch
12308255736Sdavidchstatic void
12309255736Sdavidchbxe_periodic_callout_func(void *xsc)
12310255736Sdavidch{
12311255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12312255736Sdavidch    int i;
12313255736Sdavidch
12314255736Sdavidch    if (!BXE_CORE_TRYLOCK(sc)) {
12315255736Sdavidch        /* just bail and try again next time */
12316255736Sdavidch
12317255736Sdavidch        if ((sc->state == BXE_STATE_OPEN) &&
12318255736Sdavidch            (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12319255736Sdavidch            /* schedule the next periodic callout */
12320255736Sdavidch            callout_reset(&sc->periodic_callout, hz,
12321255736Sdavidch                          bxe_periodic_callout_func, sc);
12322255736Sdavidch        }
12323255736Sdavidch
12324255736Sdavidch        return;
12325255736Sdavidch    }
12326255736Sdavidch
12327255736Sdavidch    if ((sc->state != BXE_STATE_OPEN) ||
12328255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12329255736Sdavidch        BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12330255736Sdavidch        BXE_CORE_UNLOCK(sc);
12331255736Sdavidch        return;
12332297873Sdavidcs        }
12333297873Sdavidcs
12334297873Sdavidcs
12335255736Sdavidch    /* Check for TX timeouts on any fastpath. */
12336255736Sdavidch    FOR_EACH_QUEUE(sc, i) {
12337255736Sdavidch        if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12338255736Sdavidch            /* Ruh-Roh, chip was reset! */
12339255736Sdavidch            break;
12340255736Sdavidch        }
12341255736Sdavidch    }
12342255736Sdavidch
12343255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12344255736Sdavidch        /*
12345255736Sdavidch         * This barrier is needed to ensure the ordering between the writing
12346255736Sdavidch         * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12347255736Sdavidch         * the reading here.
12348255736Sdavidch         */
12349255736Sdavidch        mb();
12350255736Sdavidch        if (sc->port.pmf) {
12351284335Sdavidcs	    bxe_acquire_phy_lock(sc);
12352255736Sdavidch            elink_period_func(&sc->link_params, &sc->link_vars);
12353284335Sdavidcs	    bxe_release_phy_lock(sc);
12354255736Sdavidch        }
12355255736Sdavidch    }
12356255736Sdavidch
12357284335Sdavidcs    if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12358255736Sdavidch        int mb_idx = SC_FW_MB_IDX(sc);
12359255736Sdavidch        uint32_t drv_pulse;
12360255736Sdavidch        uint32_t mcp_pulse;
12361255736Sdavidch
12362255736Sdavidch        ++sc->fw_drv_pulse_wr_seq;
12363255736Sdavidch        sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12364255736Sdavidch
12365255736Sdavidch        drv_pulse = sc->fw_drv_pulse_wr_seq;
12366255736Sdavidch        bxe_drv_pulse(sc);
12367255736Sdavidch
12368255736Sdavidch        mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12369255736Sdavidch                     MCP_PULSE_SEQ_MASK);
12370255736Sdavidch
12371255736Sdavidch        /*
12372255736Sdavidch         * The delta between driver pulse and mcp response should
12373255736Sdavidch         * be 1 (before mcp response) or 0 (after mcp response).
12374255736Sdavidch         */
12375255736Sdavidch        if ((drv_pulse != mcp_pulse) &&
12376255736Sdavidch            (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12377255736Sdavidch            /* someone lost a heartbeat... */
12378255736Sdavidch            BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12379255736Sdavidch                  drv_pulse, mcp_pulse);
12380255736Sdavidch        }
12381255736Sdavidch    }
12382255736Sdavidch
12383255736Sdavidch    /* state is BXE_STATE_OPEN */
12384255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12385255736Sdavidch
12386255736Sdavidch    BXE_CORE_UNLOCK(sc);
12387255736Sdavidch
12388255736Sdavidch    if ((sc->state == BXE_STATE_OPEN) &&
12389255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12390255736Sdavidch        /* schedule the next periodic callout */
12391255736Sdavidch        callout_reset(&sc->periodic_callout, hz,
12392255736Sdavidch                      bxe_periodic_callout_func, sc);
12393255736Sdavidch    }
12394255736Sdavidch}
12395255736Sdavidch
12396255736Sdavidchstatic void
12397255736Sdavidchbxe_periodic_start(struct bxe_softc *sc)
12398255736Sdavidch{
12399255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12400255736Sdavidch    callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12401255736Sdavidch}
12402255736Sdavidch
12403255736Sdavidchstatic void
12404255736Sdavidchbxe_periodic_stop(struct bxe_softc *sc)
12405255736Sdavidch{
12406255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12407255736Sdavidch    callout_drain(&sc->periodic_callout);
12408255736Sdavidch}
12409255736Sdavidch
12410339881Sdavidcsvoid
12411339881Sdavidcsbxe_parity_recover(struct bxe_softc *sc)
12412339881Sdavidcs{
12413339881Sdavidcs    uint8_t global = FALSE;
12414339881Sdavidcs    uint32_t error_recovered, error_unrecovered;
12415339881Sdavidcs    bool is_parity;
12416339881Sdavidcs
12417339881Sdavidcs
12418339881Sdavidcs    if ((sc->recovery_state == BXE_RECOVERY_FAILED) &&
12419339881Sdavidcs        (sc->state == BXE_STATE_ERROR)) {
12420339881Sdavidcs        BLOGE(sc, "RECOVERY failed, "
12421339881Sdavidcs            "stack notified driver is NOT running! "
12422339881Sdavidcs            "Please reboot/power cycle the system.\n");
12423339881Sdavidcs        return;
12424339881Sdavidcs    }
12425339881Sdavidcs
12426339881Sdavidcs    while (1) {
12427339881Sdavidcs        BLOGD(sc, DBG_SP,
12428339881Sdavidcs           "%s sc=%p state=0x%x rec_state=0x%x error_status=%x\n",
12429339881Sdavidcs            __func__, sc, sc->state, sc->recovery_state, sc->error_status);
12430339881Sdavidcs
12431339881Sdavidcs        switch(sc->recovery_state) {
12432339881Sdavidcs
12433339881Sdavidcs        case BXE_RECOVERY_INIT:
12434339881Sdavidcs            is_parity = bxe_chk_parity_attn(sc, &global, FALSE);
12435339881Sdavidcs
12436339881Sdavidcs            if ((CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ||
12437339881Sdavidcs                (sc->error_status & BXE_ERR_MCP_ASSERT) ||
12438339881Sdavidcs                (sc->error_status & BXE_ERR_GLOBAL)) {
12439339881Sdavidcs
12440339881Sdavidcs                BXE_CORE_LOCK(sc);
12441339881Sdavidcs                if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12442339881Sdavidcs                    bxe_periodic_stop(sc);
12443339881Sdavidcs                }
12444339881Sdavidcs                bxe_nic_unload(sc, UNLOAD_RECOVERY, false);
12445339881Sdavidcs                sc->state = BXE_STATE_ERROR;
12446339881Sdavidcs                sc->recovery_state = BXE_RECOVERY_FAILED;
12447339881Sdavidcs                BLOGE(sc, " No Recovery tried for error 0x%x"
12448339881Sdavidcs                    " stack notified driver is NOT running!"
12449339881Sdavidcs                    " Please reboot/power cycle the system.\n",
12450339881Sdavidcs                    sc->error_status);
12451339881Sdavidcs                BXE_CORE_UNLOCK(sc);
12452339881Sdavidcs                return;
12453339881Sdavidcs            }
12454339881Sdavidcs
12455339881Sdavidcs
12456339881Sdavidcs           /* Try to get a LEADER_LOCK HW lock */
12457339881Sdavidcs            if (bxe_trylock_leader_lock(sc)) {
12458339881Sdavidcs
12459339881Sdavidcs                bxe_set_reset_in_progress(sc);
12460339881Sdavidcs                /*
12461339881Sdavidcs                 * Check if there is a global attention and if
12462339881Sdavidcs                 * there was a global attention, set the global
12463339881Sdavidcs                 * reset bit.
12464339881Sdavidcs                 */
12465339881Sdavidcs                if (global) {
12466339881Sdavidcs                    bxe_set_reset_global(sc);
12467339881Sdavidcs                }
12468339881Sdavidcs                sc->is_leader = 1;
12469339881Sdavidcs            }
12470339881Sdavidcs
12471339881Sdavidcs            /* If interface has been removed - break */
12472339881Sdavidcs
12473339881Sdavidcs            if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12474339881Sdavidcs                bxe_periodic_stop(sc);
12475339881Sdavidcs            }
12476339881Sdavidcs
12477339881Sdavidcs            BXE_CORE_LOCK(sc);
12478339881Sdavidcs            bxe_nic_unload(sc,UNLOAD_RECOVERY, false);
12479339881Sdavidcs            sc->recovery_state = BXE_RECOVERY_WAIT;
12480339881Sdavidcs            BXE_CORE_UNLOCK(sc);
12481339881Sdavidcs
12482339881Sdavidcs            /*
12483339881Sdavidcs             * Ensure "is_leader", MCP command sequence and
12484339881Sdavidcs             * "recovery_state" update values are seen on other
12485339881Sdavidcs             * CPUs.
12486339881Sdavidcs             */
12487339881Sdavidcs            mb();
12488339881Sdavidcs            break;
12489339881Sdavidcs        case BXE_RECOVERY_WAIT:
12490339881Sdavidcs
12491339881Sdavidcs            if (sc->is_leader) {
12492339881Sdavidcs                int other_engine = SC_PATH(sc) ? 0 : 1;
12493339881Sdavidcs                bool other_load_status =
12494339881Sdavidcs                    bxe_get_load_status(sc, other_engine);
12495339881Sdavidcs                bool load_status =
12496339881Sdavidcs                    bxe_get_load_status(sc, SC_PATH(sc));
12497339881Sdavidcs                global = bxe_reset_is_global(sc);
12498339881Sdavidcs
12499339881Sdavidcs                /*
12500339881Sdavidcs                 * In case of a parity in a global block, let
12501339881Sdavidcs                 * the first leader that performs a
12502339881Sdavidcs                 * leader_reset() reset the global blocks in
12503339881Sdavidcs                 * order to clear global attentions. Otherwise
12504339881Sdavidcs                 * the gates will remain closed for that
12505339881Sdavidcs                 * engine.
12506339881Sdavidcs                 */
12507339881Sdavidcs                if (load_status ||
12508339881Sdavidcs                    (global && other_load_status)) {
12509339881Sdavidcs                    /*
12510339881Sdavidcs                     * Wait until all other functions get
12511339881Sdavidcs                     * down.
12512339881Sdavidcs                     */
12513339881Sdavidcs                    taskqueue_enqueue_timeout(taskqueue_thread,
12514339881Sdavidcs                        &sc->sp_err_timeout_task, hz/10);
12515339881Sdavidcs                    return;
12516339881Sdavidcs                } else {
12517339881Sdavidcs                    /*
12518339881Sdavidcs                     * If all other functions got down
12519339881Sdavidcs                     * try to bring the chip back to
12520339881Sdavidcs                     * normal. In any case it's an exit
12521339881Sdavidcs                     * point for a leader.
12522339881Sdavidcs                     */
12523339881Sdavidcs                    if (bxe_leader_reset(sc)) {
12524339881Sdavidcs                        BLOGE(sc, "RECOVERY failed, "
12525339881Sdavidcs                            "stack notified driver is NOT running!\n");
12526339881Sdavidcs                        sc->recovery_state = BXE_RECOVERY_FAILED;
12527339881Sdavidcs                        sc->state = BXE_STATE_ERROR;
12528339881Sdavidcs                        mb();
12529339881Sdavidcs                        return;
12530339881Sdavidcs                    }
12531339881Sdavidcs
12532339881Sdavidcs                    /*
12533339881Sdavidcs                     * If we are here, means that the
12534339881Sdavidcs                     * leader has succeeded and doesn't
12535339881Sdavidcs                     * want to be a leader any more. Try
12536339881Sdavidcs                     * to continue as a none-leader.
12537339881Sdavidcs                     */
12538339881Sdavidcs                break;
12539339881Sdavidcs                }
12540339881Sdavidcs
12541339881Sdavidcs            } else { /* non-leader */
12542339881Sdavidcs                if (!bxe_reset_is_done(sc, SC_PATH(sc))) {
12543339881Sdavidcs                    /*
12544339881Sdavidcs                     * Try to get a LEADER_LOCK HW lock as
12545339881Sdavidcs                     * long as a former leader may have
12546339881Sdavidcs                     * been unloaded by the user or
12547339881Sdavidcs                     * released a leadership by another
12548339881Sdavidcs                     * reason.
12549339881Sdavidcs                     */
12550339881Sdavidcs                    if (bxe_trylock_leader_lock(sc)) {
12551339881Sdavidcs                        /*
12552339881Sdavidcs                         * I'm a leader now! Restart a
12553339881Sdavidcs                         * switch case.
12554339881Sdavidcs                         */
12555339881Sdavidcs                        sc->is_leader = 1;
12556339881Sdavidcs                        break;
12557339881Sdavidcs                    }
12558339881Sdavidcs
12559339881Sdavidcs                    taskqueue_enqueue_timeout(taskqueue_thread,
12560339881Sdavidcs                        &sc->sp_err_timeout_task, hz/10);
12561339881Sdavidcs                    return;
12562339881Sdavidcs
12563339881Sdavidcs                } else {
12564339881Sdavidcs                    /*
12565339881Sdavidcs                     * If there was a global attention, wait
12566339881Sdavidcs                     * for it to be cleared.
12567339881Sdavidcs                     */
12568339881Sdavidcs                    if (bxe_reset_is_global(sc)) {
12569339881Sdavidcs                        taskqueue_enqueue_timeout(taskqueue_thread,
12570339881Sdavidcs                            &sc->sp_err_timeout_task, hz/10);
12571339881Sdavidcs                        return;
12572339881Sdavidcs                     }
12573339881Sdavidcs
12574339881Sdavidcs                     error_recovered =
12575339881Sdavidcs                         sc->eth_stats.recoverable_error;
12576339881Sdavidcs                     error_unrecovered =
12577339881Sdavidcs                         sc->eth_stats.unrecoverable_error;
12578339881Sdavidcs                     BXE_CORE_LOCK(sc);
12579339881Sdavidcs                     sc->recovery_state =
12580339881Sdavidcs                         BXE_RECOVERY_NIC_LOADING;
12581339881Sdavidcs                     if (bxe_nic_load(sc, LOAD_NORMAL)) {
12582339881Sdavidcs                         error_unrecovered++;
12583339881Sdavidcs                         sc->recovery_state = BXE_RECOVERY_FAILED;
12584339881Sdavidcs                         sc->state = BXE_STATE_ERROR;
12585339881Sdavidcs                         BLOGE(sc, "Recovery is NOT successfull, "
12586339881Sdavidcs                            " state=0x%x recovery_state=0x%x error=%x\n",
12587339881Sdavidcs                            sc->state, sc->recovery_state, sc->error_status);
12588339881Sdavidcs                         sc->error_status = 0;
12589339881Sdavidcs                     } else {
12590339881Sdavidcs                         sc->recovery_state =
12591339881Sdavidcs                             BXE_RECOVERY_DONE;
12592339881Sdavidcs                         error_recovered++;
12593339881Sdavidcs                         BLOGI(sc, "Recovery is successfull from errors %x,"
12594339881Sdavidcs                            " state=0x%x"
12595339881Sdavidcs                            " recovery_state=0x%x \n", sc->error_status,
12596339881Sdavidcs                            sc->state, sc->recovery_state);
12597339881Sdavidcs                         mb();
12598339881Sdavidcs                     }
12599339881Sdavidcs                     sc->error_status = 0;
12600339881Sdavidcs                     BXE_CORE_UNLOCK(sc);
12601339881Sdavidcs                     sc->eth_stats.recoverable_error =
12602339881Sdavidcs                         error_recovered;
12603339881Sdavidcs                     sc->eth_stats.unrecoverable_error =
12604339881Sdavidcs                         error_unrecovered;
12605339881Sdavidcs
12606339881Sdavidcs                     return;
12607339881Sdavidcs                 }
12608339881Sdavidcs             }
12609339881Sdavidcs         default:
12610339881Sdavidcs             return;
12611339881Sdavidcs         }
12612339881Sdavidcs    }
12613339881Sdavidcs}
12614339881Sdavidcsvoid
12615339881Sdavidcsbxe_handle_error(struct bxe_softc * sc)
12616339881Sdavidcs{
12617339881Sdavidcs
12618339881Sdavidcs    if(sc->recovery_state == BXE_RECOVERY_WAIT) {
12619339881Sdavidcs        return;
12620339881Sdavidcs    }
12621339881Sdavidcs    if(sc->error_status) {
12622339881Sdavidcs        if (sc->state == BXE_STATE_OPEN)  {
12623339881Sdavidcs            bxe_int_disable(sc);
12624339881Sdavidcs        }
12625339881Sdavidcs        if (sc->link_vars.link_up) {
12626339881Sdavidcs            if_link_state_change(sc->ifp, LINK_STATE_DOWN);
12627339881Sdavidcs        }
12628339881Sdavidcs        sc->recovery_state = BXE_RECOVERY_INIT;
12629339881Sdavidcs        BLOGI(sc, "bxe%d: Recovery started errors 0x%x recovery state 0x%x\n",
12630339881Sdavidcs            sc->unit, sc->error_status, sc->recovery_state);
12631339881Sdavidcs        bxe_parity_recover(sc);
12632339881Sdavidcs   }
12633339881Sdavidcs}
12634339881Sdavidcs
12635339881Sdavidcsstatic void
12636339881Sdavidcsbxe_sp_err_timeout_task(void *arg, int pending)
12637339881Sdavidcs{
12638339881Sdavidcs
12639339881Sdavidcs    struct bxe_softc *sc = (struct bxe_softc *)arg;
12640339881Sdavidcs
12641339881Sdavidcs    BLOGD(sc, DBG_SP,
12642339881Sdavidcs        "%s state = 0x%x rec state=0x%x error_status=%x\n",
12643339881Sdavidcs        __func__, sc->state, sc->recovery_state, sc->error_status);
12644339881Sdavidcs
12645339881Sdavidcs    if((sc->recovery_state == BXE_RECOVERY_FAILED) &&
12646339881Sdavidcs       (sc->state == BXE_STATE_ERROR)) {
12647339881Sdavidcs        return;
12648339881Sdavidcs    }
12649339881Sdavidcs    /* if can be taken */
12650339881Sdavidcs    if ((sc->error_status) && (sc->trigger_grcdump)) {
12651339881Sdavidcs        bxe_grc_dump(sc);
12652339881Sdavidcs    }
12653339881Sdavidcs    if (sc->recovery_state != BXE_RECOVERY_DONE) {
12654339881Sdavidcs        bxe_handle_error(sc);
12655339881Sdavidcs        bxe_parity_recover(sc);
12656339881Sdavidcs    } else if (sc->error_status) {
12657339881Sdavidcs        bxe_handle_error(sc);
12658339881Sdavidcs    }
12659339881Sdavidcs
12660339881Sdavidcs    return;
12661339881Sdavidcs}
12662339881Sdavidcs
12663255736Sdavidch/* start the controller */
12664255736Sdavidchstatic __noinline int
12665255736Sdavidchbxe_nic_load(struct bxe_softc *sc,
12666255736Sdavidch             int              load_mode)
12667255736Sdavidch{
12668255736Sdavidch    uint32_t val;
12669255736Sdavidch    int load_code = 0;
12670255736Sdavidch    int i, rc = 0;
12671255736Sdavidch
12672255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12673255736Sdavidch
12674255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12675255736Sdavidch
12676255736Sdavidch    sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12677255736Sdavidch
12678255736Sdavidch    if (IS_PF(sc)) {
12679255736Sdavidch        /* must be called before memory allocation and HW init */
12680255736Sdavidch        bxe_ilt_set_info(sc);
12681255736Sdavidch    }
12682255736Sdavidch
12683255736Sdavidch    sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12684255736Sdavidch
12685255736Sdavidch    bxe_set_fp_rx_buf_size(sc);
12686255736Sdavidch
12687255736Sdavidch    if (bxe_alloc_fp_buffers(sc) != 0) {
12688255736Sdavidch        BLOGE(sc, "Failed to allocate fastpath memory\n");
12689255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12690255736Sdavidch        rc = ENOMEM;
12691255736Sdavidch        goto bxe_nic_load_error0;
12692255736Sdavidch    }
12693255736Sdavidch
12694255736Sdavidch    if (bxe_alloc_mem(sc) != 0) {
12695255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12696255736Sdavidch        rc = ENOMEM;
12697255736Sdavidch        goto bxe_nic_load_error0;
12698255736Sdavidch    }
12699255736Sdavidch
12700255736Sdavidch    if (bxe_alloc_fw_stats_mem(sc) != 0) {
12701255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12702255736Sdavidch        rc = ENOMEM;
12703255736Sdavidch        goto bxe_nic_load_error0;
12704255736Sdavidch    }
12705255736Sdavidch
12706255736Sdavidch    if (IS_PF(sc)) {
12707255736Sdavidch        /* set pf load just before approaching the MCP */
12708255736Sdavidch        bxe_set_pf_load(sc);
12709255736Sdavidch
12710255736Sdavidch        /* if MCP exists send load request and analyze response */
12711255736Sdavidch        if (!BXE_NOMCP(sc)) {
12712255736Sdavidch            /* attempt to load pf */
12713255736Sdavidch            if (bxe_nic_load_request(sc, &load_code) != 0) {
12714255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12715255736Sdavidch                rc = ENXIO;
12716255736Sdavidch                goto bxe_nic_load_error1;
12717255736Sdavidch            }
12718255736Sdavidch
12719255736Sdavidch            /* what did the MCP say? */
12720255736Sdavidch            if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12721255736Sdavidch                bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12722255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12723255736Sdavidch                rc = ENXIO;
12724255736Sdavidch                goto bxe_nic_load_error2;
12725255736Sdavidch            }
12726255736Sdavidch        } else {
12727255736Sdavidch            BLOGI(sc, "Device has no MCP!\n");
12728255736Sdavidch            load_code = bxe_nic_load_no_mcp(sc);
12729255736Sdavidch        }
12730255736Sdavidch
12731255736Sdavidch        /* mark PMF if applicable */
12732255736Sdavidch        bxe_nic_load_pmf(sc, load_code);
12733255736Sdavidch
12734255736Sdavidch        /* Init Function state controlling object */
12735255736Sdavidch        bxe_init_func_obj(sc);
12736255736Sdavidch
12737255736Sdavidch        /* Initialize HW */
12738255736Sdavidch        if (bxe_init_hw(sc, load_code) != 0) {
12739255736Sdavidch            BLOGE(sc, "HW init failed\n");
12740255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12741255736Sdavidch            sc->state = BXE_STATE_CLOSED;
12742255736Sdavidch            rc = ENXIO;
12743255736Sdavidch            goto bxe_nic_load_error2;
12744255736Sdavidch        }
12745255736Sdavidch    }
12746255736Sdavidch
12747284335Sdavidcs    /* set ALWAYS_ALIVE bit in shmem */
12748284335Sdavidcs    sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12749284335Sdavidcs    bxe_drv_pulse(sc);
12750284335Sdavidcs    sc->flags |= BXE_NO_PULSE;
12751284335Sdavidcs
12752255736Sdavidch    /* attach interrupts */
12753255736Sdavidch    if (bxe_interrupt_attach(sc) != 0) {
12754255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12755255736Sdavidch        rc = ENXIO;
12756255736Sdavidch        goto bxe_nic_load_error2;
12757255736Sdavidch    }
12758255736Sdavidch
12759255736Sdavidch    bxe_nic_init(sc, load_code);
12760255736Sdavidch
12761255736Sdavidch    /* Init per-function objects */
12762255736Sdavidch    if (IS_PF(sc)) {
12763255736Sdavidch        bxe_init_objs(sc);
12764255736Sdavidch        // XXX bxe_iov_nic_init(sc);
12765255736Sdavidch
12766255736Sdavidch        /* set AFEX default VLAN tag to an invalid value */
12767255736Sdavidch        sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12768255736Sdavidch        // XXX bxe_nic_load_afex_dcc(sc, load_code);
12769255736Sdavidch
12770255736Sdavidch        sc->state = BXE_STATE_OPENING_WAITING_PORT;
12771255736Sdavidch        rc = bxe_func_start(sc);
12772255736Sdavidch        if (rc) {
12773295830Sdavidcs            BLOGE(sc, "Function start failed! rc = %d\n", rc);
12774255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12775255736Sdavidch            sc->state = BXE_STATE_ERROR;
12776255736Sdavidch            goto bxe_nic_load_error3;
12777255736Sdavidch        }
12778255736Sdavidch
12779255736Sdavidch        /* send LOAD_DONE command to MCP */
12780255736Sdavidch        if (!BXE_NOMCP(sc)) {
12781255736Sdavidch            load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12782255736Sdavidch            if (!load_code) {
12783255736Sdavidch                BLOGE(sc, "MCP response failure, aborting\n");
12784255736Sdavidch                sc->state = BXE_STATE_ERROR;
12785255736Sdavidch                rc = ENXIO;
12786255736Sdavidch                goto bxe_nic_load_error3;
12787255736Sdavidch            }
12788255736Sdavidch        }
12789255736Sdavidch
12790255736Sdavidch        rc = bxe_setup_leading(sc);
12791255736Sdavidch        if (rc) {
12792295830Sdavidcs            BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12793255736Sdavidch            sc->state = BXE_STATE_ERROR;
12794255736Sdavidch            goto bxe_nic_load_error3;
12795255736Sdavidch        }
12796255736Sdavidch
12797255736Sdavidch        FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12798255736Sdavidch            rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12799255736Sdavidch            if (rc) {
12800295830Sdavidcs                BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12801255736Sdavidch                sc->state = BXE_STATE_ERROR;
12802255736Sdavidch                goto bxe_nic_load_error3;
12803255736Sdavidch            }
12804255736Sdavidch        }
12805255736Sdavidch
12806255736Sdavidch        rc = bxe_init_rss_pf(sc);
12807255736Sdavidch        if (rc) {
12808255736Sdavidch            BLOGE(sc, "PF RSS init failed\n");
12809255736Sdavidch            sc->state = BXE_STATE_ERROR;
12810255736Sdavidch            goto bxe_nic_load_error3;
12811255736Sdavidch        }
12812255736Sdavidch    }
12813255736Sdavidch    /* XXX VF */
12814255736Sdavidch
12815255736Sdavidch    /* now when Clients are configured we are ready to work */
12816255736Sdavidch    sc->state = BXE_STATE_OPEN;
12817255736Sdavidch
12818255736Sdavidch    /* Configure a ucast MAC */
12819255736Sdavidch    if (IS_PF(sc)) {
12820255736Sdavidch        rc = bxe_set_eth_mac(sc, TRUE);
12821255736Sdavidch    }
12822255736Sdavidch    if (rc) {
12823295830Sdavidcs        BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12824255736Sdavidch        sc->state = BXE_STATE_ERROR;
12825255736Sdavidch        goto bxe_nic_load_error3;
12826255736Sdavidch    }
12827255736Sdavidch
12828255736Sdavidch    if (sc->port.pmf) {
12829255736Sdavidch        rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12830255736Sdavidch        if (rc) {
12831255736Sdavidch            sc->state = BXE_STATE_ERROR;
12832255736Sdavidch            goto bxe_nic_load_error3;
12833255736Sdavidch        }
12834255736Sdavidch    }
12835255736Sdavidch
12836255736Sdavidch    sc->link_params.feature_config_flags &=
12837255736Sdavidch        ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12838255736Sdavidch
12839255736Sdavidch    /* start fast path */
12840255736Sdavidch
12841255736Sdavidch    /* Initialize Rx filter */
12842255736Sdavidch    bxe_set_rx_mode(sc);
12843255736Sdavidch
12844255736Sdavidch    /* start the Tx */
12845255736Sdavidch    switch (/* XXX load_mode */LOAD_OPEN) {
12846255736Sdavidch    case LOAD_NORMAL:
12847255736Sdavidch    case LOAD_OPEN:
12848255736Sdavidch        break;
12849255736Sdavidch
12850255736Sdavidch    case LOAD_DIAG:
12851255736Sdavidch    case LOAD_LOOPBACK_EXT:
12852255736Sdavidch        sc->state = BXE_STATE_DIAG;
12853255736Sdavidch        break;
12854255736Sdavidch
12855255736Sdavidch    default:
12856255736Sdavidch        break;
12857255736Sdavidch    }
12858255736Sdavidch
12859255736Sdavidch    if (sc->port.pmf) {
12860255736Sdavidch        bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12861255736Sdavidch    } else {
12862255736Sdavidch        bxe_link_status_update(sc);
12863255736Sdavidch    }
12864255736Sdavidch
12865255736Sdavidch    /* start the periodic timer callout */
12866255736Sdavidch    bxe_periodic_start(sc);
12867255736Sdavidch
12868255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12869255736Sdavidch        /* mark driver is loaded in shmem2 */
12870255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12871255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12872255736Sdavidch                  (val |
12873255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12874255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_L2));
12875255736Sdavidch    }
12876255736Sdavidch
12877255736Sdavidch    /* wait for all pending SP commands to complete */
12878255736Sdavidch    if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12879255736Sdavidch        BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12880255736Sdavidch        bxe_periodic_stop(sc);
12881255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12882255736Sdavidch        return (ENXIO);
12883255736Sdavidch    }
12884255736Sdavidch
12885255736Sdavidch    /* Tell the stack the driver is running! */
12886266979Smarcel    if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
12887255736Sdavidch
12888255736Sdavidch    BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12889255736Sdavidch
12890255736Sdavidch    return (0);
12891255736Sdavidch
12892255736Sdavidchbxe_nic_load_error3:
12893255736Sdavidch
12894255736Sdavidch    if (IS_PF(sc)) {
12895255736Sdavidch        bxe_int_disable_sync(sc, 1);
12896255736Sdavidch
12897255736Sdavidch        /* clean out queued objects */
12898255736Sdavidch        bxe_squeeze_objects(sc);
12899255736Sdavidch    }
12900255736Sdavidch
12901255736Sdavidch    bxe_interrupt_detach(sc);
12902255736Sdavidch
12903255736Sdavidchbxe_nic_load_error2:
12904255736Sdavidch
12905255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12906255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12907255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12908255736Sdavidch    }
12909255736Sdavidch
12910255736Sdavidch    sc->port.pmf = 0;
12911255736Sdavidch
12912255736Sdavidchbxe_nic_load_error1:
12913255736Sdavidch
12914255736Sdavidch    /* clear pf_load status, as it was already set */
12915255736Sdavidch    if (IS_PF(sc)) {
12916255736Sdavidch        bxe_clear_pf_load(sc);
12917255736Sdavidch    }
12918255736Sdavidch
12919255736Sdavidchbxe_nic_load_error0:
12920255736Sdavidch
12921255736Sdavidch    bxe_free_fw_stats_mem(sc);
12922255736Sdavidch    bxe_free_fp_buffers(sc);
12923255736Sdavidch    bxe_free_mem(sc);
12924255736Sdavidch
12925255736Sdavidch    return (rc);
12926255736Sdavidch}
12927255736Sdavidch
12928255736Sdavidchstatic int
12929255736Sdavidchbxe_init_locked(struct bxe_softc *sc)
12930255736Sdavidch{
12931255736Sdavidch    int other_engine = SC_PATH(sc) ? 0 : 1;
12932255736Sdavidch    uint8_t other_load_status, load_status;
12933255736Sdavidch    uint8_t global = FALSE;
12934255736Sdavidch    int rc;
12935255736Sdavidch
12936255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12937255736Sdavidch
12938255736Sdavidch    /* check if the driver is already running */
12939266979Smarcel    if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12940255736Sdavidch        BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12941255736Sdavidch        return (0);
12942255736Sdavidch    }
12943255736Sdavidch
12944339881Sdavidcs    if((sc->state == BXE_STATE_ERROR) &&
12945339881Sdavidcs        (sc->recovery_state == BXE_RECOVERY_FAILED)) {
12946339881Sdavidcs        BLOGE(sc, "Initialization not done, "
12947339881Sdavidcs                  "as previous recovery failed."
12948339881Sdavidcs                  "Reboot/Power-cycle the system\n" );
12949339881Sdavidcs        return (ENXIO);
12950339881Sdavidcs    }
12951339881Sdavidcs
12952339881Sdavidcs
12953255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
12954255736Sdavidch
12955255736Sdavidch    /*
12956255736Sdavidch     * If parity occurred during the unload, then attentions and/or
12957255736Sdavidch     * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12958255736Sdavidch     * loaded on the current engine to complete the recovery. Parity recovery
12959255736Sdavidch     * is only relevant for PF driver.
12960255736Sdavidch     */
12961255736Sdavidch    if (IS_PF(sc)) {
12962255736Sdavidch        other_load_status = bxe_get_load_status(sc, other_engine);
12963255736Sdavidch        load_status = bxe_get_load_status(sc, SC_PATH(sc));
12964255736Sdavidch
12965255736Sdavidch        if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12966255736Sdavidch            bxe_chk_parity_attn(sc, &global, TRUE)) {
12967255736Sdavidch            do {
12968255736Sdavidch                /*
12969255736Sdavidch                 * If there are attentions and they are in global blocks, set
12970255736Sdavidch                 * the GLOBAL_RESET bit regardless whether it will be this
12971255736Sdavidch                 * function that will complete the recovery or not.
12972255736Sdavidch                 */
12973255736Sdavidch                if (global) {
12974255736Sdavidch                    bxe_set_reset_global(sc);
12975255736Sdavidch                }
12976255736Sdavidch
12977255736Sdavidch                /*
12978255736Sdavidch                 * Only the first function on the current engine should try
12979255736Sdavidch                 * to recover in open. In case of attentions in global blocks
12980255736Sdavidch                 * only the first in the chip should try to recover.
12981255736Sdavidch                 */
12982255736Sdavidch                if ((!load_status && (!global || !other_load_status)) &&
12983255736Sdavidch                    bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12984255736Sdavidch                    BLOGI(sc, "Recovered during init\n");
12985255736Sdavidch                    break;
12986255736Sdavidch                }
12987255736Sdavidch
12988255736Sdavidch                /* recovery has failed... */
12989255736Sdavidch                bxe_set_power_state(sc, PCI_PM_D3hot);
12990255736Sdavidch                sc->recovery_state = BXE_RECOVERY_FAILED;
12991255736Sdavidch
12992255736Sdavidch                BLOGE(sc, "Recovery flow hasn't properly "
12993255736Sdavidch                          "completed yet, try again later. "
12994255736Sdavidch                          "If you still see this message after a "
12995255736Sdavidch                          "few retries then power cycle is required.\n");
12996255736Sdavidch
12997255736Sdavidch                rc = ENXIO;
12998255736Sdavidch                goto bxe_init_locked_done;
12999255736Sdavidch            } while (0);
13000255736Sdavidch        }
13001255736Sdavidch    }
13002255736Sdavidch
13003255736Sdavidch    sc->recovery_state = BXE_RECOVERY_DONE;
13004255736Sdavidch
13005255736Sdavidch    rc = bxe_nic_load(sc, LOAD_OPEN);
13006255736Sdavidch
13007255736Sdavidchbxe_init_locked_done:
13008255736Sdavidch
13009255736Sdavidch    if (rc) {
13010255736Sdavidch        /* Tell the stack the driver is NOT running! */
13011255736Sdavidch        BLOGE(sc, "Initialization failed, "
13012255736Sdavidch                  "stack notified driver is NOT running!\n");
13013266979Smarcel	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
13014255736Sdavidch    }
13015255736Sdavidch
13016255736Sdavidch    return (rc);
13017255736Sdavidch}
13018255736Sdavidch
13019255736Sdavidchstatic int
13020255736Sdavidchbxe_stop_locked(struct bxe_softc *sc)
13021255736Sdavidch{
13022255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
13023255736Sdavidch    return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
13024255736Sdavidch}
13025255736Sdavidch
13026255736Sdavidch/*
13027255736Sdavidch * Handles controller initialization when called from an unlocked routine.
13028255736Sdavidch * ifconfig calls this function.
13029255736Sdavidch *
13030255736Sdavidch * Returns:
13031255736Sdavidch *   void
13032255736Sdavidch */
13033255736Sdavidchstatic void
13034255736Sdavidchbxe_init(void *xsc)
13035255736Sdavidch{
13036255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
13037255736Sdavidch
13038255736Sdavidch    BXE_CORE_LOCK(sc);
13039255736Sdavidch    bxe_init_locked(sc);
13040255736Sdavidch    BXE_CORE_UNLOCK(sc);
13041255736Sdavidch}
13042255736Sdavidch
13043255736Sdavidchstatic int
13044255736Sdavidchbxe_init_ifnet(struct bxe_softc *sc)
13045255736Sdavidch{
13046266979Smarcel    if_t ifp;
13047266979Smarcel    int capabilities;
13048255736Sdavidch
13049255736Sdavidch    /* ifconfig entrypoint for media type/status reporting */
13050255736Sdavidch    ifmedia_init(&sc->ifmedia, IFM_IMASK,
13051255736Sdavidch                 bxe_ifmedia_update,
13052255736Sdavidch                 bxe_ifmedia_status);
13053255736Sdavidch
13054255736Sdavidch    /* set the default interface values */
13055255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
13056255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
13057255736Sdavidch    ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
13058255736Sdavidch
13059255736Sdavidch    sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
13060315881Sdavidcs	BLOGI(sc, "IFMEDIA flags : %x\n", sc->ifmedia.ifm_media);
13061255736Sdavidch
13062255736Sdavidch    /* allocate the ifnet structure */
13063266979Smarcel    if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
13064255736Sdavidch        BLOGE(sc, "Interface allocation failed!\n");
13065255736Sdavidch        return (ENXIO);
13066255736Sdavidch    }
13067255736Sdavidch
13068266979Smarcel    if_setsoftc(ifp, sc);
13069270876Sglebius    if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
13070266979Smarcel    if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
13071266979Smarcel    if_setioctlfn(ifp, bxe_ioctl);
13072266979Smarcel    if_setstartfn(ifp, bxe_tx_start);
13073271782Sglebius    if_setgetcounterfn(ifp, bxe_get_counter);
13074307972Sdavidcs#if __FreeBSD_version >= 901504
13075266979Smarcel    if_settransmitfn(ifp, bxe_tx_mq_start);
13076266979Smarcel    if_setqflushfn(ifp, bxe_mq_flush);
13077255736Sdavidch#endif
13078255736Sdavidch#ifdef FreeBSD8_0
13079266979Smarcel    if_settimer(ifp, 0);
13080255736Sdavidch#endif
13081266979Smarcel    if_setinitfn(ifp, bxe_init);
13082266979Smarcel    if_setmtu(ifp, sc->mtu);
13083266979Smarcel    if_sethwassist(ifp, (CSUM_IP      |
13084255736Sdavidch                        CSUM_TCP      |
13085255736Sdavidch                        CSUM_UDP      |
13086255736Sdavidch                        CSUM_TSO      |
13087255736Sdavidch                        CSUM_TCP_IPV6 |
13088266979Smarcel                        CSUM_UDP_IPV6));
13089266979Smarcel
13090266979Smarcel    capabilities =
13091255736Sdavidch#if __FreeBSD_version < 700000
13092255736Sdavidch        (IFCAP_VLAN_MTU       |
13093255736Sdavidch         IFCAP_VLAN_HWTAGGING |
13094255736Sdavidch         IFCAP_HWCSUM         |
13095255736Sdavidch         IFCAP_JUMBO_MTU      |
13096255736Sdavidch         IFCAP_LRO);
13097255736Sdavidch#else
13098255736Sdavidch        (IFCAP_VLAN_MTU       |
13099255736Sdavidch         IFCAP_VLAN_HWTAGGING |
13100255736Sdavidch         IFCAP_VLAN_HWTSO     |
13101255736Sdavidch         IFCAP_VLAN_HWFILTER  |
13102255736Sdavidch         IFCAP_VLAN_HWCSUM    |
13103255736Sdavidch         IFCAP_HWCSUM         |
13104255736Sdavidch         IFCAP_JUMBO_MTU      |
13105255736Sdavidch         IFCAP_LRO            |
13106255736Sdavidch         IFCAP_TSO4           |
13107255736Sdavidch         IFCAP_TSO6           |
13108255736Sdavidch         IFCAP_WOL_MAGIC);
13109255736Sdavidch#endif
13110266979Smarcel    if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
13111316539Ssbruno    if_setcapenable(ifp, if_getcapabilities(ifp));
13112266979Smarcel    if_setbaudrate(ifp, IF_Gbps(10));
13113266979Smarcel/* XXX */
13114266979Smarcel    if_setsendqlen(ifp, sc->tx_ring_size);
13115266979Smarcel    if_setsendqready(ifp);
13116266979Smarcel/* XXX */
13117255736Sdavidch
13118266979Smarcel    sc->ifp = ifp;
13119255736Sdavidch
13120255736Sdavidch    /* attach to the Ethernet interface list */
13121270876Sglebius    ether_ifattach(ifp, sc->link_params.mac_addr);
13122255736Sdavidch
13123255736Sdavidch    return (0);
13124255736Sdavidch}
13125255736Sdavidch
13126255736Sdavidchstatic void
13127255736Sdavidchbxe_deallocate_bars(struct bxe_softc *sc)
13128255736Sdavidch{
13129255736Sdavidch    int i;
13130255736Sdavidch
13131255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
13132255736Sdavidch        if (sc->bar[i].resource != NULL) {
13133255736Sdavidch            bus_release_resource(sc->dev,
13134255736Sdavidch                                 SYS_RES_MEMORY,
13135255736Sdavidch                                 sc->bar[i].rid,
13136255736Sdavidch                                 sc->bar[i].resource);
13137255736Sdavidch            BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
13138255736Sdavidch                  i, PCIR_BAR(i));
13139255736Sdavidch        }
13140255736Sdavidch    }
13141255736Sdavidch}
13142255736Sdavidch
13143255736Sdavidchstatic int
13144255736Sdavidchbxe_allocate_bars(struct bxe_softc *sc)
13145255736Sdavidch{
13146255736Sdavidch    u_int flags;
13147255736Sdavidch    int i;
13148255736Sdavidch
13149255736Sdavidch    memset(sc->bar, 0, sizeof(sc->bar));
13150255736Sdavidch
13151255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
13152255736Sdavidch
13153255736Sdavidch        /* memory resources reside at BARs 0, 2, 4 */
13154255736Sdavidch        /* Run `pciconf -lb` to see mappings */
13155255736Sdavidch        if ((i != 0) && (i != 2) && (i != 4)) {
13156255736Sdavidch            continue;
13157255736Sdavidch        }
13158255736Sdavidch
13159255736Sdavidch        sc->bar[i].rid = PCIR_BAR(i);
13160255736Sdavidch
13161255736Sdavidch        flags = RF_ACTIVE;
13162255736Sdavidch        if (i == 0) {
13163255736Sdavidch            flags |= RF_SHAREABLE;
13164255736Sdavidch        }
13165255736Sdavidch
13166255736Sdavidch        if ((sc->bar[i].resource =
13167255736Sdavidch             bus_alloc_resource_any(sc->dev,
13168255736Sdavidch                                    SYS_RES_MEMORY,
13169255736Sdavidch                                    &sc->bar[i].rid,
13170255736Sdavidch                                    flags)) == NULL) {
13171255736Sdavidch            return (0);
13172255736Sdavidch        }
13173255736Sdavidch
13174255736Sdavidch        sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
13175255736Sdavidch        sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
13176255736Sdavidch        sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
13177255736Sdavidch
13178335289Sdim        BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %#jx-%#jx (%jd) -> %#jx\n",
13179255736Sdavidch              i, PCIR_BAR(i),
13180335289Sdim              rman_get_start(sc->bar[i].resource),
13181335289Sdim              rman_get_end(sc->bar[i].resource),
13182255736Sdavidch              rman_get_size(sc->bar[i].resource),
13183335289Sdim              (uintmax_t)sc->bar[i].kva);
13184255736Sdavidch    }
13185255736Sdavidch
13186255736Sdavidch    return (0);
13187255736Sdavidch}
13188255736Sdavidch
13189255736Sdavidchstatic void
13190255736Sdavidchbxe_get_function_num(struct bxe_softc *sc)
13191255736Sdavidch{
13192255736Sdavidch    uint32_t val = 0;
13193255736Sdavidch
13194255736Sdavidch    /*
13195255736Sdavidch     * Read the ME register to get the function number. The ME register
13196255736Sdavidch     * holds the relative-function number and absolute-function number. The
13197255736Sdavidch     * absolute-function number appears only in E2 and above. Before that
13198255736Sdavidch     * these bits always contained zero, therefore we cannot blindly use them.
13199255736Sdavidch     */
13200255736Sdavidch
13201255736Sdavidch    val = REG_RD(sc, BAR_ME_REGISTER);
13202255736Sdavidch
13203255736Sdavidch    sc->pfunc_rel =
13204255736Sdavidch        (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
13205255736Sdavidch    sc->path_id =
13206255736Sdavidch        (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
13207255736Sdavidch
13208255736Sdavidch    if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13209255736Sdavidch        sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
13210255736Sdavidch    } else {
13211255736Sdavidch        sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
13212255736Sdavidch    }
13213255736Sdavidch
13214255736Sdavidch    BLOGD(sc, DBG_LOAD,
13215255736Sdavidch          "Relative function %d, Absolute function %d, Path %d\n",
13216255736Sdavidch          sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
13217255736Sdavidch}
13218255736Sdavidch
13219255736Sdavidchstatic uint32_t
13220255736Sdavidchbxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
13221255736Sdavidch{
13222255736Sdavidch    uint32_t shmem2_size;
13223255736Sdavidch    uint32_t offset;
13224255736Sdavidch    uint32_t mf_cfg_offset_value;
13225255736Sdavidch
13226255736Sdavidch    /* Non 57712 */
13227255736Sdavidch    offset = (SHMEM_RD(sc, func_mb) +
13228255736Sdavidch              (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
13229255736Sdavidch
13230255736Sdavidch    /* 57712 plus */
13231255736Sdavidch    if (sc->devinfo.shmem2_base != 0) {
13232255736Sdavidch        shmem2_size = SHMEM2_RD(sc, size);
13233255736Sdavidch        if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
13234255736Sdavidch            mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
13235255736Sdavidch            if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
13236255736Sdavidch                offset = mf_cfg_offset_value;
13237255736Sdavidch            }
13238255736Sdavidch        }
13239255736Sdavidch    }
13240255736Sdavidch
13241255736Sdavidch    return (offset);
13242255736Sdavidch}
13243255736Sdavidch
13244255736Sdavidchstatic uint32_t
13245255736Sdavidchbxe_pcie_capability_read(struct bxe_softc *sc,
13246255736Sdavidch                         int    reg,
13247255736Sdavidch                         int    width)
13248255736Sdavidch{
13249255736Sdavidch    int pcie_reg;
13250255736Sdavidch
13251255736Sdavidch    /* ensure PCIe capability is enabled */
13252255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
13253255736Sdavidch        if (pcie_reg != 0) {
13254255736Sdavidch            BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
13255255736Sdavidch            return (pci_read_config(sc->dev, (pcie_reg + reg), width));
13256255736Sdavidch        }
13257255736Sdavidch    }
13258255736Sdavidch
13259255736Sdavidch    BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
13260255736Sdavidch
13261255736Sdavidch    return (0);
13262255736Sdavidch}
13263255736Sdavidch
13264255736Sdavidchstatic uint8_t
13265255736Sdavidchbxe_is_pcie_pending(struct bxe_softc *sc)
13266255736Sdavidch{
13267255736Sdavidch    return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
13268255736Sdavidch            PCIM_EXP_STA_TRANSACTION_PND);
13269255736Sdavidch}
13270255736Sdavidch
13271255736Sdavidch/*
13272255736Sdavidch * Walk the PCI capabiites list for the device to find what features are
13273255736Sdavidch * supported. These capabilites may be enabled/disabled by firmware so it's
13274255736Sdavidch * best to walk the list rather than make assumptions.
13275255736Sdavidch */
13276255736Sdavidchstatic void
13277255736Sdavidchbxe_probe_pci_caps(struct bxe_softc *sc)
13278255736Sdavidch{
13279255736Sdavidch    uint16_t link_status;
13280255736Sdavidch    int reg;
13281255736Sdavidch
13282255736Sdavidch    /* check if PCI Power Management is enabled */
13283255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
13284255736Sdavidch        if (reg != 0) {
13285255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
13286255736Sdavidch
13287255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
13288255736Sdavidch            sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
13289255736Sdavidch        }
13290255736Sdavidch    }
13291255736Sdavidch
13292255736Sdavidch    link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
13293255736Sdavidch
13294255736Sdavidch    /* handle PCIe 2.0 workarounds for 57710 */
13295255736Sdavidch    if (CHIP_IS_E1(sc)) {
13296255736Sdavidch        /* workaround for 57710 errata E4_57710_27462 */
13297255736Sdavidch        sc->devinfo.pcie_link_speed =
13298255736Sdavidch            (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
13299255736Sdavidch
13300255736Sdavidch        /* workaround for 57710 errata E4_57710_27488 */
13301255736Sdavidch        sc->devinfo.pcie_link_width =
13302255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13303255736Sdavidch        if (sc->devinfo.pcie_link_speed > 1) {
13304255736Sdavidch            sc->devinfo.pcie_link_width =
13305255736Sdavidch                ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
13306255736Sdavidch        }
13307255736Sdavidch    } else {
13308255736Sdavidch        sc->devinfo.pcie_link_speed =
13309255736Sdavidch            (link_status & PCIM_LINK_STA_SPEED);
13310255736Sdavidch        sc->devinfo.pcie_link_width =
13311255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13312255736Sdavidch    }
13313255736Sdavidch
13314255736Sdavidch    BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
13315255736Sdavidch          sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
13316255736Sdavidch
13317255736Sdavidch    sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
13318255736Sdavidch    sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
13319255736Sdavidch
13320255736Sdavidch    /* check if MSI capability is enabled */
13321255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
13322255736Sdavidch        if (reg != 0) {
13323255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
13324255736Sdavidch
13325255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
13326255736Sdavidch            sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
13327255736Sdavidch        }
13328255736Sdavidch    }
13329255736Sdavidch
13330255736Sdavidch    /* check if MSI-X capability is enabled */
13331255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
13332255736Sdavidch        if (reg != 0) {
13333255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
13334255736Sdavidch
13335255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
13336255736Sdavidch            sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
13337255736Sdavidch        }
13338255736Sdavidch    }
13339255736Sdavidch}
13340255736Sdavidch
13341255736Sdavidchstatic int
13342255736Sdavidchbxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
13343255736Sdavidch{
13344255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13345255736Sdavidch    uint32_t val;
13346255736Sdavidch
13347255736Sdavidch    /* get the outer vlan if we're in switch-dependent mode */
13348255736Sdavidch
13349255736Sdavidch    val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13350255736Sdavidch    mf_info->ext_id = (uint16_t)val;
13351255736Sdavidch
13352255736Sdavidch    mf_info->multi_vnics_mode = 1;
13353255736Sdavidch
13354255736Sdavidch    if (!VALID_OVLAN(mf_info->ext_id)) {
13355255736Sdavidch        BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13356255736Sdavidch        return (1);
13357255736Sdavidch    }
13358255736Sdavidch
13359255736Sdavidch    /* get the capabilities */
13360255736Sdavidch    if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13361255736Sdavidch        FUNC_MF_CFG_PROTOCOL_ISCSI) {
13362255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13363255736Sdavidch    } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13364255736Sdavidch               FUNC_MF_CFG_PROTOCOL_FCOE) {
13365255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13366255736Sdavidch    } else {
13367255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13368255736Sdavidch    }
13369255736Sdavidch
13370255736Sdavidch    mf_info->vnics_per_port =
13371255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13372255736Sdavidch
13373255736Sdavidch    return (0);
13374255736Sdavidch}
13375255736Sdavidch
13376255736Sdavidchstatic uint32_t
13377255736Sdavidchbxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13378255736Sdavidch{
13379255736Sdavidch    uint32_t retval = 0;
13380255736Sdavidch    uint32_t val;
13381255736Sdavidch
13382255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13383255736Sdavidch
13384255736Sdavidch    if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13385255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13386255736Sdavidch            retval |= MF_PROTO_SUPPORT_ETHERNET;
13387255736Sdavidch        }
13388255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13389255736Sdavidch            retval |= MF_PROTO_SUPPORT_ISCSI;
13390255736Sdavidch        }
13391255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13392255736Sdavidch            retval |= MF_PROTO_SUPPORT_FCOE;
13393255736Sdavidch        }
13394255736Sdavidch    }
13395255736Sdavidch
13396255736Sdavidch    return (retval);
13397255736Sdavidch}
13398255736Sdavidch
13399255736Sdavidchstatic int
13400255736Sdavidchbxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13401255736Sdavidch{
13402255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13403255736Sdavidch    uint32_t val;
13404255736Sdavidch
13405255736Sdavidch    /*
13406255736Sdavidch     * There is no outer vlan if we're in switch-independent mode.
13407255736Sdavidch     * If the mac is valid then assume multi-function.
13408255736Sdavidch     */
13409255736Sdavidch
13410255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13411255736Sdavidch
13412255736Sdavidch    mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13413255736Sdavidch
13414255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13415255736Sdavidch
13416255736Sdavidch    mf_info->vnics_per_port =
13417255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13418255736Sdavidch
13419255736Sdavidch    return (0);
13420255736Sdavidch}
13421255736Sdavidch
13422255736Sdavidchstatic int
13423255736Sdavidchbxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13424255736Sdavidch{
13425255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13426255736Sdavidch    uint32_t e1hov_tag;
13427255736Sdavidch    uint32_t func_config;
13428255736Sdavidch    uint32_t niv_config;
13429255736Sdavidch
13430255736Sdavidch    mf_info->multi_vnics_mode = 1;
13431255736Sdavidch
13432255736Sdavidch    e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13433255736Sdavidch    func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13434255736Sdavidch    niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13435255736Sdavidch
13436255736Sdavidch    mf_info->ext_id =
13437255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13438255736Sdavidch                   FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13439255736Sdavidch
13440255736Sdavidch    mf_info->default_vlan =
13441255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13442255736Sdavidch                   FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13443255736Sdavidch
13444255736Sdavidch    mf_info->niv_allowed_priorities =
13445255736Sdavidch        (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13446255736Sdavidch                  FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13447255736Sdavidch
13448255736Sdavidch    mf_info->niv_default_cos =
13449255736Sdavidch        (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13450255736Sdavidch                  FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13451255736Sdavidch
13452255736Sdavidch    mf_info->afex_vlan_mode =
13453255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13454255736Sdavidch         FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13455255736Sdavidch
13456255736Sdavidch    mf_info->niv_mba_enabled =
13457255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13458255736Sdavidch         FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13459255736Sdavidch
13460255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13461255736Sdavidch
13462255736Sdavidch    mf_info->vnics_per_port =
13463255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13464255736Sdavidch
13465255736Sdavidch    return (0);
13466255736Sdavidch}
13467255736Sdavidch
13468255736Sdavidchstatic int
13469255736Sdavidchbxe_check_valid_mf_cfg(struct bxe_softc *sc)
13470255736Sdavidch{
13471255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13472255736Sdavidch    uint32_t mf_cfg1;
13473255736Sdavidch    uint32_t mf_cfg2;
13474255736Sdavidch    uint32_t ovlan1;
13475255736Sdavidch    uint32_t ovlan2;
13476255736Sdavidch    uint8_t i, j;
13477255736Sdavidch
13478255736Sdavidch    BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13479255736Sdavidch          SC_PORT(sc));
13480255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13481255736Sdavidch          mf_info->mf_config[SC_VN(sc)]);
13482255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13483255736Sdavidch          mf_info->multi_vnics_mode);
13484255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13485255736Sdavidch          mf_info->vnics_per_port);
13486255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13487255736Sdavidch          mf_info->ext_id);
13488255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13489255736Sdavidch          mf_info->min_bw[0], mf_info->min_bw[1],
13490255736Sdavidch          mf_info->min_bw[2], mf_info->min_bw[3]);
13491255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13492255736Sdavidch          mf_info->max_bw[0], mf_info->max_bw[1],
13493255736Sdavidch          mf_info->max_bw[2], mf_info->max_bw[3]);
13494255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13495255736Sdavidch          sc->mac_addr_str);
13496255736Sdavidch
13497255736Sdavidch    /* various MF mode sanity checks... */
13498255736Sdavidch
13499255736Sdavidch    if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13500255736Sdavidch        BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13501255736Sdavidch              SC_PORT(sc));
13502255736Sdavidch        return (1);
13503255736Sdavidch    }
13504255736Sdavidch
13505255736Sdavidch    if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13506255736Sdavidch        BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13507255736Sdavidch              mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13508255736Sdavidch        return (1);
13509255736Sdavidch    }
13510255736Sdavidch
13511255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13512255736Sdavidch        /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13513255736Sdavidch        if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13514255736Sdavidch            BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13515255736Sdavidch                  SC_VN(sc), OVLAN(sc));
13516255736Sdavidch            return (1);
13517255736Sdavidch        }
13518255736Sdavidch
13519255736Sdavidch        if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13520255736Sdavidch            BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13521255736Sdavidch                  mf_info->multi_vnics_mode, OVLAN(sc));
13522255736Sdavidch            return (1);
13523255736Sdavidch        }
13524255736Sdavidch
13525255736Sdavidch        /*
13526255736Sdavidch         * Verify all functions are either MF or SF mode. If MF, make sure
13527255736Sdavidch         * sure that all non-hidden functions have a valid ovlan. If SF,
13528255736Sdavidch         * make sure that all non-hidden functions have an invalid ovlan.
13529255736Sdavidch         */
13530255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13531255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13532255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13533255736Sdavidch            if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13534255736Sdavidch                (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13535255736Sdavidch                 ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13536255736Sdavidch                BLOGE(sc, "mf_mode=SD function %d MF config "
13537255736Sdavidch                          "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13538255736Sdavidch                      i, mf_info->multi_vnics_mode, ovlan1);
13539255736Sdavidch                return (1);
13540255736Sdavidch            }
13541255736Sdavidch        }
13542255736Sdavidch
13543255736Sdavidch        /* Verify all funcs on the same port each have a different ovlan. */
13544255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13545255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13546255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13547255736Sdavidch            /* iterate from the next function on the port to the max func */
13548255736Sdavidch            for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13549255736Sdavidch                mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13550255736Sdavidch                ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13551255736Sdavidch                if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13552255736Sdavidch                    VALID_OVLAN(ovlan1) &&
13553255736Sdavidch                    !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13554255736Sdavidch                    VALID_OVLAN(ovlan2) &&
13555255736Sdavidch                    (ovlan1 == ovlan2)) {
13556255736Sdavidch                    BLOGE(sc, "mf_mode=SD functions %d and %d "
13557255736Sdavidch                              "have the same ovlan (%d)\n",
13558255736Sdavidch                          i, j, ovlan1);
13559255736Sdavidch                    return (1);
13560255736Sdavidch                }
13561255736Sdavidch            }
13562255736Sdavidch        }
13563255736Sdavidch    } /* MULTI_FUNCTION_SD */
13564255736Sdavidch
13565255736Sdavidch    return (0);
13566255736Sdavidch}
13567255736Sdavidch
13568255736Sdavidchstatic int
13569255736Sdavidchbxe_get_mf_cfg_info(struct bxe_softc *sc)
13570255736Sdavidch{
13571255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13572255736Sdavidch    uint32_t val, mac_upper;
13573255736Sdavidch    uint8_t i, vnic;
13574255736Sdavidch
13575255736Sdavidch    /* initialize mf_info defaults */
13576255736Sdavidch    mf_info->vnics_per_port   = 1;
13577255736Sdavidch    mf_info->multi_vnics_mode = FALSE;
13578255736Sdavidch    mf_info->path_has_ovlan   = FALSE;
13579255736Sdavidch    mf_info->mf_mode          = SINGLE_FUNCTION;
13580255736Sdavidch
13581255736Sdavidch    if (!CHIP_IS_MF_CAP(sc)) {
13582255736Sdavidch        return (0);
13583255736Sdavidch    }
13584255736Sdavidch
13585255736Sdavidch    if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13586255736Sdavidch        BLOGE(sc, "Invalid mf_cfg_base!\n");
13587255736Sdavidch        return (1);
13588255736Sdavidch    }
13589255736Sdavidch
13590255736Sdavidch    /* get the MF mode (switch dependent / independent / single-function) */
13591255736Sdavidch
13592255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13593255736Sdavidch
13594255736Sdavidch    switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13595255736Sdavidch    {
13596255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13597255736Sdavidch
13598255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13599255736Sdavidch
13600255736Sdavidch        /* check for legal upper mac bytes */
13601255736Sdavidch        if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13602255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SI;
13603255736Sdavidch        } else {
13604255736Sdavidch            BLOGE(sc, "Invalid config for Switch Independent mode\n");
13605255736Sdavidch        }
13606255736Sdavidch
13607255736Sdavidch        break;
13608255736Sdavidch
13609255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13610255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13611255736Sdavidch
13612255736Sdavidch        /* get outer vlan configuration */
13613255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13614255736Sdavidch
13615255736Sdavidch        if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13616255736Sdavidch            FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13617255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SD;
13618255736Sdavidch        } else {
13619255736Sdavidch            BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13620255736Sdavidch        }
13621255736Sdavidch
13622255736Sdavidch        break;
13623255736Sdavidch
13624255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13625255736Sdavidch
13626255736Sdavidch        /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13627255736Sdavidch        return (0);
13628255736Sdavidch
13629255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13630255736Sdavidch
13631255736Sdavidch        /*
13632255736Sdavidch         * Mark MF mode as NIV if MCP version includes NPAR-SD support
13633255736Sdavidch         * and the MAC address is valid.
13634255736Sdavidch         */
13635255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13636255736Sdavidch
13637255736Sdavidch        if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13638255736Sdavidch            (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13639255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13640255736Sdavidch        } else {
13641255736Sdavidch            BLOGE(sc, "Invalid config for AFEX mode\n");
13642255736Sdavidch        }
13643255736Sdavidch
13644255736Sdavidch        break;
13645255736Sdavidch
13646255736Sdavidch    default:
13647255736Sdavidch
13648255736Sdavidch        BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13649255736Sdavidch              (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13650255736Sdavidch
13651255736Sdavidch        return (1);
13652255736Sdavidch    }
13653255736Sdavidch
13654255736Sdavidch    /* set path mf_mode (which could be different than function mf_mode) */
13655255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13656255736Sdavidch        mf_info->path_has_ovlan = TRUE;
13657255736Sdavidch    } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13658255736Sdavidch        /*
13659255736Sdavidch         * Decide on path multi vnics mode. If we're not in MF mode and in
13660255736Sdavidch         * 4-port mode, this is good enough to check vnic-0 of the other port
13661255736Sdavidch         * on the same path
13662255736Sdavidch         */
13663255736Sdavidch        if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13664255736Sdavidch            uint8_t other_port = !(PORT_ID(sc) & 1);
13665255736Sdavidch            uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13666255736Sdavidch
13667255736Sdavidch            val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13668255736Sdavidch
13669255736Sdavidch            mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13670255736Sdavidch        }
13671255736Sdavidch    }
13672255736Sdavidch
13673255736Sdavidch    if (mf_info->mf_mode == SINGLE_FUNCTION) {
13674255736Sdavidch        /* invalid MF config */
13675255736Sdavidch        if (SC_VN(sc) >= 1) {
13676255736Sdavidch            BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13677255736Sdavidch            return (1);
13678255736Sdavidch        }
13679255736Sdavidch
13680255736Sdavidch        return (0);
13681255736Sdavidch    }
13682255736Sdavidch
13683255736Sdavidch    /* get the MF configuration */
13684255736Sdavidch    mf_info->mf_config[SC_VN(sc)] =
13685255736Sdavidch        MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13686255736Sdavidch
13687255736Sdavidch    switch(mf_info->mf_mode)
13688255736Sdavidch    {
13689255736Sdavidch    case MULTI_FUNCTION_SD:
13690255736Sdavidch
13691255736Sdavidch        bxe_get_shmem_mf_cfg_info_sd(sc);
13692255736Sdavidch        break;
13693255736Sdavidch
13694255736Sdavidch    case MULTI_FUNCTION_SI:
13695255736Sdavidch
13696255736Sdavidch        bxe_get_shmem_mf_cfg_info_si(sc);
13697255736Sdavidch        break;
13698255736Sdavidch
13699255736Sdavidch    case MULTI_FUNCTION_AFEX:
13700255736Sdavidch
13701255736Sdavidch        bxe_get_shmem_mf_cfg_info_niv(sc);
13702255736Sdavidch        break;
13703255736Sdavidch
13704255736Sdavidch    default:
13705255736Sdavidch
13706255736Sdavidch        BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13707255736Sdavidch              mf_info->mf_mode);
13708255736Sdavidch        return (1);
13709255736Sdavidch    }
13710255736Sdavidch
13711255736Sdavidch    /* get the congestion management parameters */
13712255736Sdavidch
13713255736Sdavidch    vnic = 0;
13714255736Sdavidch    FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13715255736Sdavidch        /* get min/max bw */
13716255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[i].config);
13717255736Sdavidch        mf_info->min_bw[vnic] =
13718255736Sdavidch            ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13719255736Sdavidch        mf_info->max_bw[vnic] =
13720255736Sdavidch            ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13721255736Sdavidch        vnic++;
13722255736Sdavidch    }
13723255736Sdavidch
13724255736Sdavidch    return (bxe_check_valid_mf_cfg(sc));
13725255736Sdavidch}
13726255736Sdavidch
13727255736Sdavidchstatic int
13728255736Sdavidchbxe_get_shmem_info(struct bxe_softc *sc)
13729255736Sdavidch{
13730255736Sdavidch    int port;
13731255736Sdavidch    uint32_t mac_hi, mac_lo, val;
13732255736Sdavidch
13733255736Sdavidch    port = SC_PORT(sc);
13734255736Sdavidch    mac_hi = mac_lo = 0;
13735255736Sdavidch
13736255736Sdavidch    sc->link_params.sc   = sc;
13737255736Sdavidch    sc->link_params.port = port;
13738255736Sdavidch
13739255736Sdavidch    /* get the hardware config info */
13740255736Sdavidch    sc->devinfo.hw_config =
13741255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config);
13742255736Sdavidch    sc->devinfo.hw_config2 =
13743255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13744255736Sdavidch
13745255736Sdavidch    sc->link_params.hw_led_mode =
13746255736Sdavidch        ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13747255736Sdavidch         SHARED_HW_CFG_LED_MODE_SHIFT);
13748255736Sdavidch
13749255736Sdavidch    /* get the port feature config */
13750255736Sdavidch    sc->port.config =
13751305614Spfg        SHMEM_RD(sc, dev_info.port_feature_config[port].config);
13752255736Sdavidch
13753255736Sdavidch    /* get the link params */
13754255736Sdavidch    sc->link_params.speed_cap_mask[0] =
13755255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13756255736Sdavidch    sc->link_params.speed_cap_mask[1] =
13757255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13758255736Sdavidch
13759255736Sdavidch    /* get the lane config */
13760255736Sdavidch    sc->link_params.lane_config =
13761255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13762255736Sdavidch
13763255736Sdavidch    /* get the link config */
13764255736Sdavidch    val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13765255736Sdavidch    sc->port.link_config[ELINK_INT_PHY] = val;
13766255736Sdavidch    sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13767255736Sdavidch    sc->port.link_config[ELINK_EXT_PHY1] =
13768255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13769255736Sdavidch
13770255736Sdavidch    /* get the override preemphasis flag and enable it or turn it off */
13771255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13772255736Sdavidch    if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13773255736Sdavidch        sc->link_params.feature_config_flags |=
13774255736Sdavidch            ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13775255736Sdavidch    } else {
13776255736Sdavidch        sc->link_params.feature_config_flags &=
13777255736Sdavidch            ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13778255736Sdavidch    }
13779255736Sdavidch
13780255736Sdavidch    /* get the initial value of the link params */
13781255736Sdavidch    sc->link_params.multi_phy_config =
13782255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13783255736Sdavidch
13784255736Sdavidch    /* get external phy info */
13785255736Sdavidch    sc->port.ext_phy_config =
13786255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13787255736Sdavidch
13788255736Sdavidch    /* get the multifunction configuration */
13789255736Sdavidch    bxe_get_mf_cfg_info(sc);
13790255736Sdavidch
13791255736Sdavidch    /* get the mac address */
13792255736Sdavidch    if (IS_MF(sc)) {
13793255736Sdavidch        mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13794255736Sdavidch        mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13795255736Sdavidch    } else {
13796255736Sdavidch        mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13797255736Sdavidch        mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13798255736Sdavidch    }
13799255736Sdavidch
13800255736Sdavidch    if ((mac_lo == 0) && (mac_hi == 0)) {
13801255736Sdavidch        *sc->mac_addr_str = 0;
13802255736Sdavidch        BLOGE(sc, "No Ethernet address programmed!\n");
13803255736Sdavidch    } else {
13804255736Sdavidch        sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13805255736Sdavidch        sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13806255736Sdavidch        sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13807255736Sdavidch        sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13808255736Sdavidch        sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13809255736Sdavidch        sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13810255736Sdavidch        snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13811255736Sdavidch                 "%02x:%02x:%02x:%02x:%02x:%02x",
13812255736Sdavidch                 sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13813255736Sdavidch                 sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13814255736Sdavidch                 sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13815255736Sdavidch        BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13816255736Sdavidch    }
13817255736Sdavidch
13818255736Sdavidch    return (0);
13819255736Sdavidch}
13820255736Sdavidch
13821255736Sdavidchstatic void
13822255736Sdavidchbxe_get_tunable_params(struct bxe_softc *sc)
13823255736Sdavidch{
13824255736Sdavidch    /* sanity checks */
13825255736Sdavidch
13826255736Sdavidch    if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13827255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13828255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13829255736Sdavidch        BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13830255736Sdavidch        bxe_interrupt_mode = INTR_MODE_MSIX;
13831255736Sdavidch    }
13832255736Sdavidch
13833255736Sdavidch    if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13834255736Sdavidch        BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13835255736Sdavidch        bxe_queue_count = 0;
13836255736Sdavidch    }
13837255736Sdavidch
13838255736Sdavidch    if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13839255736Sdavidch        if (bxe_max_rx_bufs == 0) {
13840255736Sdavidch            bxe_max_rx_bufs = RX_BD_USABLE;
13841255736Sdavidch        } else {
13842255736Sdavidch            BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13843255736Sdavidch            bxe_max_rx_bufs = 2048;
13844255736Sdavidch        }
13845255736Sdavidch    }
13846255736Sdavidch
13847255736Sdavidch    if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13848255736Sdavidch        BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13849255736Sdavidch        bxe_hc_rx_ticks = 25;
13850255736Sdavidch    }
13851255736Sdavidch
13852255736Sdavidch    if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13853255736Sdavidch        BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13854255736Sdavidch        bxe_hc_tx_ticks = 50;
13855255736Sdavidch    }
13856255736Sdavidch
13857255736Sdavidch    if (bxe_max_aggregation_size == 0) {
13858255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13859255736Sdavidch    }
13860255736Sdavidch
13861255736Sdavidch    if (bxe_max_aggregation_size > 0xffff) {
13862255736Sdavidch        BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13863255736Sdavidch              bxe_max_aggregation_size);
13864255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13865255736Sdavidch    }
13866255736Sdavidch
13867255736Sdavidch    if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13868255736Sdavidch        BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13869255736Sdavidch        bxe_mrrs = -1;
13870255736Sdavidch    }
13871255736Sdavidch
13872255736Sdavidch    if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13873255736Sdavidch        BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13874255736Sdavidch        bxe_autogreeen = 0;
13875255736Sdavidch    }
13876255736Sdavidch
13877255736Sdavidch    if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13878255736Sdavidch        BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13879255736Sdavidch        bxe_udp_rss = 0;
13880255736Sdavidch    }
13881255736Sdavidch
13882255736Sdavidch    /* pull in user settings */
13883255736Sdavidch
13884255736Sdavidch    sc->interrupt_mode       = bxe_interrupt_mode;
13885255736Sdavidch    sc->max_rx_bufs          = bxe_max_rx_bufs;
13886255736Sdavidch    sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13887255736Sdavidch    sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13888255736Sdavidch    sc->max_aggregation_size = bxe_max_aggregation_size;
13889255736Sdavidch    sc->mrrs                 = bxe_mrrs;
13890255736Sdavidch    sc->autogreeen           = bxe_autogreeen;
13891255736Sdavidch    sc->udp_rss              = bxe_udp_rss;
13892255736Sdavidch
13893255736Sdavidch    if (bxe_interrupt_mode == INTR_MODE_INTX) {
13894255736Sdavidch        sc->num_queues = 1;
13895255736Sdavidch    } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13896255736Sdavidch        sc->num_queues =
13897255736Sdavidch            min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13898255736Sdavidch                MAX_RSS_CHAINS);
13899255736Sdavidch        if (sc->num_queues > mp_ncpus) {
13900255736Sdavidch            sc->num_queues = mp_ncpus;
13901255736Sdavidch        }
13902255736Sdavidch    }
13903255736Sdavidch
13904255736Sdavidch    BLOGD(sc, DBG_LOAD,
13905255736Sdavidch          "User Config: "
13906258187Sedavis          "debug=0x%lx "
13907255736Sdavidch          "interrupt_mode=%d "
13908255736Sdavidch          "queue_count=%d "
13909255736Sdavidch          "hc_rx_ticks=%d "
13910255736Sdavidch          "hc_tx_ticks=%d "
13911255736Sdavidch          "rx_budget=%d "
13912255736Sdavidch          "max_aggregation_size=%d "
13913255736Sdavidch          "mrrs=%d "
13914255736Sdavidch          "autogreeen=%d "
13915255736Sdavidch          "udp_rss=%d\n",
13916255736Sdavidch          bxe_debug,
13917255736Sdavidch          sc->interrupt_mode,
13918255736Sdavidch          sc->num_queues,
13919255736Sdavidch          sc->hc_rx_ticks,
13920255736Sdavidch          sc->hc_tx_ticks,
13921255736Sdavidch          bxe_rx_budget,
13922255736Sdavidch          sc->max_aggregation_size,
13923255736Sdavidch          sc->mrrs,
13924255736Sdavidch          sc->autogreeen,
13925255736Sdavidch          sc->udp_rss);
13926255736Sdavidch}
13927255736Sdavidch
13928297884Sdavidcsstatic int
13929255736Sdavidchbxe_media_detect(struct bxe_softc *sc)
13930255736Sdavidch{
13931297884Sdavidcs    int port_type;
13932255736Sdavidch    uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13933297884Sdavidcs
13934255736Sdavidch    switch (sc->link_params.phy[phy_idx].media_type) {
13935255736Sdavidch    case ELINK_ETH_PHY_SFPP_10G_FIBER:
13936256299Sedavis    case ELINK_ETH_PHY_XFP_FIBER:
13937256299Sedavis        BLOGI(sc, "Found 10Gb Fiber media.\n");
13938256299Sedavis        sc->media = IFM_10G_SR;
13939297884Sdavidcs        port_type = PORT_FIBRE;
13940256299Sedavis        break;
13941255736Sdavidch    case ELINK_ETH_PHY_SFP_1G_FIBER:
13942256299Sedavis        BLOGI(sc, "Found 1Gb Fiber media.\n");
13943256299Sedavis        sc->media = IFM_1000_SX;
13944297884Sdavidcs        port_type = PORT_FIBRE;
13945256299Sedavis        break;
13946255736Sdavidch    case ELINK_ETH_PHY_KR:
13947255736Sdavidch    case ELINK_ETH_PHY_CX4:
13948255736Sdavidch        BLOGI(sc, "Found 10GBase-CX4 media.\n");
13949255736Sdavidch        sc->media = IFM_10G_CX4;
13950297884Sdavidcs        port_type = PORT_FIBRE;
13951255736Sdavidch        break;
13952255736Sdavidch    case ELINK_ETH_PHY_DA_TWINAX:
13953255736Sdavidch        BLOGI(sc, "Found 10Gb Twinax media.\n");
13954255736Sdavidch        sc->media = IFM_10G_TWINAX;
13955297884Sdavidcs        port_type = PORT_DA;
13956255736Sdavidch        break;
13957255736Sdavidch    case ELINK_ETH_PHY_BASE_T:
13958256299Sedavis        if (sc->link_params.speed_cap_mask[0] &
13959256299Sedavis            PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13960256299Sedavis            BLOGI(sc, "Found 10GBase-T media.\n");
13961256299Sedavis            sc->media = IFM_10G_T;
13962297884Sdavidcs            port_type = PORT_TP;
13963256299Sedavis        } else {
13964256299Sedavis            BLOGI(sc, "Found 1000Base-T media.\n");
13965256299Sedavis            sc->media = IFM_1000_T;
13966297884Sdavidcs            port_type = PORT_TP;
13967256299Sedavis        }
13968255736Sdavidch        break;
13969255736Sdavidch    case ELINK_ETH_PHY_NOT_PRESENT:
13970255736Sdavidch        BLOGI(sc, "Media not present.\n");
13971255736Sdavidch        sc->media = 0;
13972297884Sdavidcs        port_type = PORT_OTHER;
13973255736Sdavidch        break;
13974255736Sdavidch    case ELINK_ETH_PHY_UNSPECIFIED:
13975255736Sdavidch    default:
13976255736Sdavidch        BLOGI(sc, "Unknown media!\n");
13977255736Sdavidch        sc->media = 0;
13978297884Sdavidcs        port_type = PORT_OTHER;
13979255736Sdavidch        break;
13980255736Sdavidch    }
13981297884Sdavidcs    return port_type;
13982255736Sdavidch}
13983255736Sdavidch
13984255736Sdavidch#define GET_FIELD(value, fname)                     \
13985255736Sdavidch    (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13986255736Sdavidch#define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13987255736Sdavidch#define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
13988255736Sdavidch
13989255736Sdavidchstatic int
13990255736Sdavidchbxe_get_igu_cam_info(struct bxe_softc *sc)
13991255736Sdavidch{
13992255736Sdavidch    int pfid = SC_FUNC(sc);
13993255736Sdavidch    int igu_sb_id;
13994255736Sdavidch    uint32_t val;
13995255736Sdavidch    uint8_t fid, igu_sb_cnt = 0;
13996255736Sdavidch
13997255736Sdavidch    sc->igu_base_sb = 0xff;
13998255736Sdavidch
13999255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
14000255736Sdavidch        int vn = SC_VN(sc);
14001255736Sdavidch        igu_sb_cnt = sc->igu_sb_cnt;
14002255736Sdavidch        sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
14003255736Sdavidch                           FP_SB_MAX_E1x);
14004255736Sdavidch        sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
14005255736Sdavidch                          (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
14006255736Sdavidch        return (0);
14007255736Sdavidch    }
14008255736Sdavidch
14009255736Sdavidch    /* IGU in normal mode - read CAM */
14010255736Sdavidch    for (igu_sb_id = 0;
14011255736Sdavidch         igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
14012255736Sdavidch         igu_sb_id++) {
14013255736Sdavidch        val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
14014255736Sdavidch        if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
14015255736Sdavidch            continue;
14016255736Sdavidch        }
14017255736Sdavidch        fid = IGU_FID(val);
14018255736Sdavidch        if ((fid & IGU_FID_ENCODE_IS_PF)) {
14019255736Sdavidch            if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
14020255736Sdavidch                continue;
14021255736Sdavidch            }
14022255736Sdavidch            if (IGU_VEC(val) == 0) {
14023255736Sdavidch                /* default status block */
14024255736Sdavidch                sc->igu_dsb_id = igu_sb_id;
14025255736Sdavidch            } else {
14026255736Sdavidch                if (sc->igu_base_sb == 0xff) {
14027255736Sdavidch                    sc->igu_base_sb = igu_sb_id;
14028255736Sdavidch                }
14029255736Sdavidch                igu_sb_cnt++;
14030255736Sdavidch            }
14031255736Sdavidch        }
14032255736Sdavidch    }
14033255736Sdavidch
14034255736Sdavidch    /*
14035255736Sdavidch     * Due to new PF resource allocation by MFW T7.4 and above, it's optional
14036255736Sdavidch     * that number of CAM entries will not be equal to the value advertised in
14037255736Sdavidch     * PCI. Driver should use the minimal value of both as the actual status
14038255736Sdavidch     * block count
14039255736Sdavidch     */
14040255736Sdavidch    sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
14041255736Sdavidch
14042255736Sdavidch    if (igu_sb_cnt == 0) {
14043255736Sdavidch        BLOGE(sc, "CAM configuration error\n");
14044255736Sdavidch        return (-1);
14045255736Sdavidch    }
14046255736Sdavidch
14047255736Sdavidch    return (0);
14048255736Sdavidch}
14049255736Sdavidch
14050255736Sdavidch/*
14051255736Sdavidch * Gather various information from the device config space, the device itself,
14052255736Sdavidch * shmem, and the user input.
14053255736Sdavidch */
14054255736Sdavidchstatic int
14055255736Sdavidchbxe_get_device_info(struct bxe_softc *sc)
14056255736Sdavidch{
14057255736Sdavidch    uint32_t val;
14058255736Sdavidch    int rc;
14059255736Sdavidch
14060255736Sdavidch    /* Get the data for the device */
14061255736Sdavidch    sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
14062255736Sdavidch    sc->devinfo.device_id    = pci_get_device(sc->dev);
14063255736Sdavidch    sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
14064255736Sdavidch    sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
14065255736Sdavidch
14066255736Sdavidch    /* get the chip revision (chip metal comes from pci config space) */
14067255736Sdavidch    sc->devinfo.chip_id     =
14068255736Sdavidch    sc->link_params.chip_id =
14069255736Sdavidch        (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
14070255736Sdavidch         ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
14071255736Sdavidch         (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
14072255736Sdavidch         ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
14073255736Sdavidch
14074255736Sdavidch    /* force 57811 according to MISC register */
14075255736Sdavidch    if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
14076255736Sdavidch        if (CHIP_IS_57810(sc)) {
14077255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
14078255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
14079255736Sdavidch        } else if (CHIP_IS_57810_MF(sc)) {
14080255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
14081255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
14082255736Sdavidch        }
14083255736Sdavidch        sc->devinfo.chip_id |= 0x1;
14084255736Sdavidch    }
14085255736Sdavidch
14086255736Sdavidch    BLOGD(sc, DBG_LOAD,
14087255736Sdavidch          "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
14088255736Sdavidch          sc->devinfo.chip_id,
14089255736Sdavidch          ((sc->devinfo.chip_id >> 16) & 0xffff),
14090255736Sdavidch          ((sc->devinfo.chip_id >> 12) & 0xf),
14091255736Sdavidch          ((sc->devinfo.chip_id >>  4) & 0xff),
14092255736Sdavidch          ((sc->devinfo.chip_id >>  0) & 0xf));
14093255736Sdavidch
14094255736Sdavidch    val = (REG_RD(sc, 0x2874) & 0x55);
14095255736Sdavidch    if ((sc->devinfo.chip_id & 0x1) ||
14096255736Sdavidch        (CHIP_IS_E1(sc) && val) ||
14097255736Sdavidch        (CHIP_IS_E1H(sc) && (val == 0x55))) {
14098255736Sdavidch        sc->flags |= BXE_ONE_PORT_FLAG;
14099255736Sdavidch        BLOGD(sc, DBG_LOAD, "single port device\n");
14100255736Sdavidch    }
14101255736Sdavidch
14102255736Sdavidch    /* set the doorbell size */
14103255736Sdavidch    sc->doorbell_size = (1 << BXE_DB_SHIFT);
14104255736Sdavidch
14105255736Sdavidch    /* determine whether the device is in 2 port or 4 port mode */
14106255736Sdavidch    sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
14107255736Sdavidch    if (CHIP_IS_E2E3(sc)) {
14108255736Sdavidch        /*
14109255736Sdavidch         * Read port4mode_en_ovwr[0]:
14110255736Sdavidch         *   If 1, four port mode is in port4mode_en_ovwr[1].
14111255736Sdavidch         *   If 0, four port mode is in port4mode_en[0].
14112255736Sdavidch         */
14113255736Sdavidch        val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
14114255736Sdavidch        if (val & 1) {
14115255736Sdavidch            val = ((val >> 1) & 1);
14116255736Sdavidch        } else {
14117255736Sdavidch            val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
14118255736Sdavidch        }
14119255736Sdavidch
14120255736Sdavidch        sc->devinfo.chip_port_mode =
14121255736Sdavidch            (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
14122255736Sdavidch
14123255736Sdavidch        BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
14124255736Sdavidch    }
14125255736Sdavidch
14126255736Sdavidch    /* get the function and path info for the device */
14127255736Sdavidch    bxe_get_function_num(sc);
14128255736Sdavidch
14129255736Sdavidch    /* get the shared memory base address */
14130255736Sdavidch    sc->devinfo.shmem_base     =
14131255736Sdavidch    sc->link_params.shmem_base =
14132255736Sdavidch        REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
14133255736Sdavidch    sc->devinfo.shmem2_base =
14134255736Sdavidch        REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
14135255736Sdavidch                                  MISC_REG_GENERIC_CR_0));
14136255736Sdavidch
14137255736Sdavidch    BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
14138255736Sdavidch          sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
14139255736Sdavidch
14140255736Sdavidch    if (!sc->devinfo.shmem_base) {
14141255736Sdavidch        /* this should ONLY prevent upcoming shmem reads */
14142255736Sdavidch        BLOGI(sc, "MCP not active\n");
14143255736Sdavidch        sc->flags |= BXE_NO_MCP_FLAG;
14144255736Sdavidch        return (0);
14145255736Sdavidch    }
14146255736Sdavidch
14147255736Sdavidch    /* make sure the shared memory contents are valid */
14148255736Sdavidch    val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
14149255736Sdavidch    if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
14150255736Sdavidch        (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
14151255736Sdavidch        BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
14152255736Sdavidch        return (0);
14153255736Sdavidch    }
14154255736Sdavidch    BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
14155255736Sdavidch
14156255736Sdavidch    /* get the bootcode version */
14157255736Sdavidch    sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
14158255736Sdavidch    snprintf(sc->devinfo.bc_ver_str,
14159255736Sdavidch             sizeof(sc->devinfo.bc_ver_str),
14160255736Sdavidch             "%d.%d.%d",
14161255736Sdavidch             ((sc->devinfo.bc_ver >> 24) & 0xff),
14162255736Sdavidch             ((sc->devinfo.bc_ver >> 16) & 0xff),
14163255736Sdavidch             ((sc->devinfo.bc_ver >>  8) & 0xff));
14164255736Sdavidch    BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
14165255736Sdavidch
14166255736Sdavidch    /* get the bootcode shmem address */
14167255736Sdavidch    sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
14168255736Sdavidch    BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
14169255736Sdavidch
14170255736Sdavidch    /* clean indirect addresses as they're not used */
14171255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
14172255736Sdavidch    if (IS_PF(sc)) {
14173255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
14174255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
14175255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
14176255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
14177255736Sdavidch        if (CHIP_IS_E1x(sc)) {
14178255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
14179255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
14180255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
14181255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
14182255736Sdavidch        }
14183255736Sdavidch
14184255736Sdavidch        /*
14185255736Sdavidch         * Enable internal target-read (in case we are probed after PF
14186255736Sdavidch         * FLR). Must be done prior to any BAR read access. Only for
14187255736Sdavidch         * 57712 and up
14188255736Sdavidch         */
14189255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
14190255736Sdavidch            REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
14191255736Sdavidch        }
14192255736Sdavidch    }
14193255736Sdavidch
14194255736Sdavidch    /* get the nvram size */
14195255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
14196255736Sdavidch    sc->devinfo.flash_size =
14197255736Sdavidch        (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
14198255736Sdavidch    BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
14199255736Sdavidch
14200255736Sdavidch    /* get PCI capabilites */
14201255736Sdavidch    bxe_probe_pci_caps(sc);
14202255736Sdavidch
14203255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
14204255736Sdavidch
14205255736Sdavidch    /* get various configuration parameters from shmem */
14206255736Sdavidch    bxe_get_shmem_info(sc);
14207255736Sdavidch
14208255736Sdavidch    if (sc->devinfo.pcie_msix_cap_reg != 0) {
14209255736Sdavidch        val = pci_read_config(sc->dev,
14210255736Sdavidch                              (sc->devinfo.pcie_msix_cap_reg +
14211255736Sdavidch                               PCIR_MSIX_CTRL),
14212255736Sdavidch                              2);
14213255736Sdavidch        sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
14214255736Sdavidch    } else {
14215255736Sdavidch        sc->igu_sb_cnt = 1;
14216255736Sdavidch    }
14217255736Sdavidch
14218255736Sdavidch    sc->igu_base_addr = BAR_IGU_INTMEM;
14219255736Sdavidch
14220255736Sdavidch    /* initialize IGU parameters */
14221255736Sdavidch    if (CHIP_IS_E1x(sc)) {
14222255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_HC;
14223255736Sdavidch        sc->igu_dsb_id = DEF_SB_IGU_ID;
14224255736Sdavidch        sc->igu_base_sb = 0;
14225255736Sdavidch    } else {
14226255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_IGU;
14227255736Sdavidch
14228255736Sdavidch        /* do not allow device reset during IGU info preocessing */
14229255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14230255736Sdavidch
14231255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
14232255736Sdavidch
14233255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14234255736Sdavidch            int tout = 5000;
14235255736Sdavidch
14236255736Sdavidch            BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
14237255736Sdavidch
14238255736Sdavidch            val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
14239255736Sdavidch            REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
14240255736Sdavidch            REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
14241255736Sdavidch
14242255736Sdavidch            while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14243255736Sdavidch                tout--;
14244255736Sdavidch                DELAY(1000);
14245255736Sdavidch            }
14246255736Sdavidch
14247255736Sdavidch            if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14248255736Sdavidch                BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
14249255736Sdavidch                bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14250255736Sdavidch                return (-1);
14251255736Sdavidch            }
14252255736Sdavidch        }
14253255736Sdavidch
14254255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14255255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
14256255736Sdavidch            sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
14257255736Sdavidch        } else {
14258255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
14259255736Sdavidch        }
14260255736Sdavidch
14261255736Sdavidch        rc = bxe_get_igu_cam_info(sc);
14262255736Sdavidch
14263255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14264255736Sdavidch
14265255736Sdavidch        if (rc) {
14266255736Sdavidch            return (rc);
14267255736Sdavidch        }
14268255736Sdavidch    }
14269255736Sdavidch
14270255736Sdavidch    /*
14271255736Sdavidch     * Get base FW non-default (fast path) status block ID. This value is
14272255736Sdavidch     * used to initialize the fw_sb_id saved on the fp/queue structure to
14273255736Sdavidch     * determine the id used by the FW.
14274255736Sdavidch     */
14275255736Sdavidch    if (CHIP_IS_E1x(sc)) {
14276255736Sdavidch        sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
14277255736Sdavidch    } else {
14278255736Sdavidch        /*
14279255736Sdavidch         * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
14280255736Sdavidch         * the same queue are indicated on the same IGU SB). So we prefer
14281255736Sdavidch         * FW and IGU SBs to be the same value.
14282255736Sdavidch         */
14283255736Sdavidch        sc->base_fw_ndsb = sc->igu_base_sb;
14284255736Sdavidch    }
14285255736Sdavidch
14286255736Sdavidch    BLOGD(sc, DBG_LOAD,
14287255736Sdavidch          "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
14288255736Sdavidch          sc->igu_dsb_id, sc->igu_base_sb,
14289255736Sdavidch          sc->igu_sb_cnt, sc->base_fw_ndsb);
14290255736Sdavidch
14291255736Sdavidch    elink_phy_probe(&sc->link_params);
14292255736Sdavidch
14293255736Sdavidch    return (0);
14294255736Sdavidch}
14295255736Sdavidch
14296255736Sdavidchstatic void
14297255736Sdavidchbxe_link_settings_supported(struct bxe_softc *sc,
14298255736Sdavidch                            uint32_t         switch_cfg)
14299255736Sdavidch{
14300255736Sdavidch    uint32_t cfg_size = 0;
14301255736Sdavidch    uint32_t idx;
14302255736Sdavidch    uint8_t port = SC_PORT(sc);
14303255736Sdavidch
14304255736Sdavidch    /* aggregation of supported attributes of all external phys */
14305255736Sdavidch    sc->port.supported[0] = 0;
14306255736Sdavidch    sc->port.supported[1] = 0;
14307255736Sdavidch
14308255736Sdavidch    switch (sc->link_params.num_phys) {
14309255736Sdavidch    case 1:
14310255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
14311255736Sdavidch        cfg_size = 1;
14312255736Sdavidch        break;
14313255736Sdavidch    case 2:
14314255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
14315255736Sdavidch        cfg_size = 1;
14316255736Sdavidch        break;
14317255736Sdavidch    case 3:
14318255736Sdavidch        if (sc->link_params.multi_phy_config &
14319255736Sdavidch            PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
14320255736Sdavidch            sc->port.supported[1] =
14321255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
14322255736Sdavidch            sc->port.supported[0] =
14323255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
14324255736Sdavidch        } else {
14325255736Sdavidch            sc->port.supported[0] =
14326255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
14327255736Sdavidch            sc->port.supported[1] =
14328255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
14329255736Sdavidch        }
14330255736Sdavidch        cfg_size = 2;
14331255736Sdavidch        break;
14332255736Sdavidch    }
14333255736Sdavidch
14334255736Sdavidch    if (!(sc->port.supported[0] || sc->port.supported[1])) {
14335255736Sdavidch        BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
14336255736Sdavidch              SHMEM_RD(sc,
14337255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config),
14338255736Sdavidch              SHMEM_RD(sc,
14339255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config2));
14340255736Sdavidch        return;
14341255736Sdavidch    }
14342255736Sdavidch
14343255736Sdavidch    if (CHIP_IS_E3(sc))
14344255736Sdavidch        sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
14345255736Sdavidch    else {
14346255736Sdavidch        switch (switch_cfg) {
14347255736Sdavidch        case ELINK_SWITCH_CFG_1G:
14348255736Sdavidch            sc->port.phy_addr =
14349255736Sdavidch                REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
14350255736Sdavidch            break;
14351255736Sdavidch        case ELINK_SWITCH_CFG_10G:
14352255736Sdavidch            sc->port.phy_addr =
14353255736Sdavidch                REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
14354255736Sdavidch            break;
14355255736Sdavidch        default:
14356255736Sdavidch            BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14357255736Sdavidch                  sc->port.link_config[0]);
14358255736Sdavidch            return;
14359255736Sdavidch        }
14360255736Sdavidch    }
14361255736Sdavidch
14362255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14363255736Sdavidch
14364255736Sdavidch    /* mask what we support according to speed_cap_mask per configuration */
14365255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14366255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14367255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14368255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14369255736Sdavidch        }
14370255736Sdavidch
14371255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14372255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14373255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14374255736Sdavidch        }
14375255736Sdavidch
14376255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14377255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14378255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14379255736Sdavidch        }
14380255736Sdavidch
14381255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14382255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14383255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14384255736Sdavidch        }
14385255736Sdavidch
14386255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14387255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14388255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14389255736Sdavidch        }
14390255736Sdavidch
14391255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14392255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14393255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14394255736Sdavidch        }
14395255736Sdavidch
14396255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14397255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14398255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14399255736Sdavidch        }
14400255736Sdavidch
14401255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14402255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14403255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14404255736Sdavidch        }
14405255736Sdavidch    }
14406255736Sdavidch
14407255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14408255736Sdavidch          sc->port.supported[0], sc->port.supported[1]);
14409315881Sdavidcs	ELINK_DEBUG_P2(sc, "PHY supported 0=0x%08x 1=0x%08x\n",
14410315881Sdavidcs					sc->port.supported[0], sc->port.supported[1]);
14411255736Sdavidch}
14412255736Sdavidch
14413255736Sdavidchstatic void
14414255736Sdavidchbxe_link_settings_requested(struct bxe_softc *sc)
14415255736Sdavidch{
14416255736Sdavidch    uint32_t link_config;
14417255736Sdavidch    uint32_t idx;
14418255736Sdavidch    uint32_t cfg_size = 0;
14419255736Sdavidch
14420255736Sdavidch    sc->port.advertising[0] = 0;
14421255736Sdavidch    sc->port.advertising[1] = 0;
14422255736Sdavidch
14423255736Sdavidch    switch (sc->link_params.num_phys) {
14424255736Sdavidch    case 1:
14425255736Sdavidch    case 2:
14426255736Sdavidch        cfg_size = 1;
14427255736Sdavidch        break;
14428255736Sdavidch    case 3:
14429255736Sdavidch        cfg_size = 2;
14430255736Sdavidch        break;
14431255736Sdavidch    }
14432255736Sdavidch
14433255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14434255736Sdavidch        sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14435255736Sdavidch        link_config = sc->port.link_config[idx];
14436255736Sdavidch
14437255736Sdavidch        switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14438255736Sdavidch        case PORT_FEATURE_LINK_SPEED_AUTO:
14439255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14440255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14441255736Sdavidch                sc->port.advertising[idx] |= sc->port.supported[idx];
14442255736Sdavidch                if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14443255736Sdavidch                    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14444255736Sdavidch                    sc->port.advertising[idx] |=
14445255736Sdavidch                        (ELINK_SUPPORTED_100baseT_Half |
14446255736Sdavidch                         ELINK_SUPPORTED_100baseT_Full);
14447255736Sdavidch            } else {
14448255736Sdavidch                /* force 10G, no AN */
14449255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14450255736Sdavidch                sc->port.advertising[idx] |=
14451255736Sdavidch                    (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14452255736Sdavidch                continue;
14453255736Sdavidch            }
14454255736Sdavidch            break;
14455255736Sdavidch
14456255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_FULL:
14457255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14458255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14459255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14460255736Sdavidch                                              ADVERTISED_TP);
14461255736Sdavidch            } else {
14462255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14463255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14464255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14465255736Sdavidch                return;
14466255736Sdavidch            }
14467255736Sdavidch            break;
14468255736Sdavidch
14469255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_HALF:
14470255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14471255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14472255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14473255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14474255736Sdavidch                                              ADVERTISED_TP);
14475315881Sdavidcs				ELINK_DEBUG_P1(sc, "driver requesting DUPLEX_HALF req_duplex = %x!\n",
14476315881Sdavidcs								sc->link_params.req_duplex[idx]);
14477255736Sdavidch            } else {
14478255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14479255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14480255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14481255736Sdavidch                return;
14482255736Sdavidch            }
14483255736Sdavidch            break;
14484255736Sdavidch
14485255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_FULL:
14486255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14487255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14488255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14489255736Sdavidch                                              ADVERTISED_TP);
14490255736Sdavidch            } else {
14491255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14492255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14493255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14494255736Sdavidch                return;
14495255736Sdavidch            }
14496255736Sdavidch            break;
14497255736Sdavidch
14498255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_HALF:
14499255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14500255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14501255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14502255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14503255736Sdavidch                                              ADVERTISED_TP);
14504255736Sdavidch            } else {
14505255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14506255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14507255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14508255736Sdavidch                return;
14509255736Sdavidch            }
14510255736Sdavidch            break;
14511255736Sdavidch
14512255736Sdavidch        case PORT_FEATURE_LINK_SPEED_1G:
14513255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14514255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14515255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14516255736Sdavidch                                              ADVERTISED_TP);
14517255736Sdavidch            } else {
14518255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14519255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14520255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14521255736Sdavidch                return;
14522255736Sdavidch            }
14523255736Sdavidch            break;
14524255736Sdavidch
14525255736Sdavidch        case PORT_FEATURE_LINK_SPEED_2_5G:
14526255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14527255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14528255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14529255736Sdavidch                                              ADVERTISED_TP);
14530255736Sdavidch            } else {
14531255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14532255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14533255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14534255736Sdavidch                return;
14535255736Sdavidch            }
14536255736Sdavidch            break;
14537255736Sdavidch
14538255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10G_CX4:
14539255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14540255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14541255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14542255736Sdavidch                                              ADVERTISED_FIBRE);
14543255736Sdavidch            } else {
14544255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14545255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14546255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14547255736Sdavidch                return;
14548255736Sdavidch            }
14549255736Sdavidch            break;
14550255736Sdavidch
14551255736Sdavidch        case PORT_FEATURE_LINK_SPEED_20G:
14552255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14553255736Sdavidch            break;
14554255736Sdavidch
14555255736Sdavidch        default:
14556255736Sdavidch            BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14557255736Sdavidch                      "speed_cap_mask=0x%08x\n",
14558255736Sdavidch                  link_config, sc->link_params.speed_cap_mask[idx]);
14559255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14560255736Sdavidch            sc->port.advertising[idx] = sc->port.supported[idx];
14561255736Sdavidch            break;
14562255736Sdavidch        }
14563255736Sdavidch
14564255736Sdavidch        sc->link_params.req_flow_ctrl[idx] =
14565255736Sdavidch            (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14566255736Sdavidch
14567255736Sdavidch        if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14568255736Sdavidch            if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14569255736Sdavidch                sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14570255736Sdavidch            } else {
14571255736Sdavidch                bxe_set_requested_fc(sc);
14572255736Sdavidch            }
14573255736Sdavidch        }
14574255736Sdavidch
14575255736Sdavidch        BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14576255736Sdavidch                            "req_flow_ctrl=0x%x advertising=0x%x\n",
14577255736Sdavidch              sc->link_params.req_line_speed[idx],
14578255736Sdavidch              sc->link_params.req_duplex[idx],
14579255736Sdavidch              sc->link_params.req_flow_ctrl[idx],
14580255736Sdavidch              sc->port.advertising[idx]);
14581315881Sdavidcs		ELINK_DEBUG_P3(sc, "req_line_speed=%d req_duplex=%d "
14582315881Sdavidcs						"advertising=0x%x\n",
14583315881Sdavidcs						sc->link_params.req_line_speed[idx],
14584315881Sdavidcs						sc->link_params.req_duplex[idx],
14585315881Sdavidcs						sc->port.advertising[idx]);
14586255736Sdavidch    }
14587255736Sdavidch}
14588255736Sdavidch
14589255736Sdavidchstatic void
14590255736Sdavidchbxe_get_phy_info(struct bxe_softc *sc)
14591255736Sdavidch{
14592255736Sdavidch    uint8_t port = SC_PORT(sc);
14593255736Sdavidch    uint32_t config = sc->port.config;
14594255736Sdavidch    uint32_t eee_mode;
14595255736Sdavidch
14596255736Sdavidch    /* shmem data already read in bxe_get_shmem_info() */
14597255736Sdavidch
14598315881Sdavidcs    ELINK_DEBUG_P3(sc, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14599255736Sdavidch                        "link_config0=0x%08x\n",
14600255736Sdavidch               sc->link_params.lane_config,
14601255736Sdavidch               sc->link_params.speed_cap_mask[0],
14602255736Sdavidch               sc->port.link_config[0]);
14603315881Sdavidcs
14604255736Sdavidch
14605255736Sdavidch    bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14606255736Sdavidch    bxe_link_settings_requested(sc);
14607255736Sdavidch
14608255736Sdavidch    if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14609255736Sdavidch        sc->link_params.feature_config_flags |=
14610255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14611255736Sdavidch    } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14612255736Sdavidch        sc->link_params.feature_config_flags &=
14613255736Sdavidch            ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14614255736Sdavidch    } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14615255736Sdavidch        sc->link_params.feature_config_flags |=
14616255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14617255736Sdavidch    }
14618255736Sdavidch
14619255736Sdavidch    /* configure link feature according to nvram value */
14620255736Sdavidch    eee_mode =
14621255736Sdavidch        (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14622255736Sdavidch          PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14623255736Sdavidch         PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14624255736Sdavidch    if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14625255736Sdavidch        sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14626255736Sdavidch                                    ELINK_EEE_MODE_ENABLE_LPI |
14627255736Sdavidch                                    ELINK_EEE_MODE_OUTPUT_TIME);
14628255736Sdavidch    } else {
14629255736Sdavidch        sc->link_params.eee_mode = 0;
14630255736Sdavidch    }
14631255736Sdavidch
14632255736Sdavidch    /* get the media type */
14633255736Sdavidch    bxe_media_detect(sc);
14634315881Sdavidcs	ELINK_DEBUG_P1(sc, "detected media type\n", sc->media);
14635255736Sdavidch}
14636255736Sdavidch
14637255736Sdavidchstatic void
14638255736Sdavidchbxe_get_params(struct bxe_softc *sc)
14639255736Sdavidch{
14640255736Sdavidch    /* get user tunable params */
14641255736Sdavidch    bxe_get_tunable_params(sc);
14642255736Sdavidch
14643255736Sdavidch    /* select the RX and TX ring sizes */
14644255736Sdavidch    sc->tx_ring_size = TX_BD_USABLE;
14645255736Sdavidch    sc->rx_ring_size = RX_BD_USABLE;
14646255736Sdavidch
14647255736Sdavidch    /* XXX disable WoL */
14648255736Sdavidch    sc->wol = 0;
14649255736Sdavidch}
14650255736Sdavidch
14651255736Sdavidchstatic void
14652255736Sdavidchbxe_set_modes_bitmap(struct bxe_softc *sc)
14653255736Sdavidch{
14654255736Sdavidch    uint32_t flags = 0;
14655255736Sdavidch
14656255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
14657255736Sdavidch        SET_FLAGS(flags, MODE_FPGA);
14658255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
14659255736Sdavidch        SET_FLAGS(flags, MODE_EMUL);
14660255736Sdavidch    } else {
14661255736Sdavidch        SET_FLAGS(flags, MODE_ASIC);
14662255736Sdavidch    }
14663255736Sdavidch
14664255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
14665255736Sdavidch        SET_FLAGS(flags, MODE_PORT4);
14666255736Sdavidch    } else {
14667255736Sdavidch        SET_FLAGS(flags, MODE_PORT2);
14668255736Sdavidch    }
14669255736Sdavidch
14670255736Sdavidch    if (CHIP_IS_E2(sc)) {
14671255736Sdavidch        SET_FLAGS(flags, MODE_E2);
14672255736Sdavidch    } else if (CHIP_IS_E3(sc)) {
14673255736Sdavidch        SET_FLAGS(flags, MODE_E3);
14674255736Sdavidch        if (CHIP_REV(sc) == CHIP_REV_Ax) {
14675255736Sdavidch            SET_FLAGS(flags, MODE_E3_A0);
14676255736Sdavidch        } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14677255736Sdavidch            SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14678255736Sdavidch        }
14679255736Sdavidch    }
14680255736Sdavidch
14681255736Sdavidch    if (IS_MF(sc)) {
14682255736Sdavidch        SET_FLAGS(flags, MODE_MF);
14683255736Sdavidch        switch (sc->devinfo.mf_info.mf_mode) {
14684255736Sdavidch        case MULTI_FUNCTION_SD:
14685255736Sdavidch            SET_FLAGS(flags, MODE_MF_SD);
14686255736Sdavidch            break;
14687255736Sdavidch        case MULTI_FUNCTION_SI:
14688255736Sdavidch            SET_FLAGS(flags, MODE_MF_SI);
14689255736Sdavidch            break;
14690255736Sdavidch        case MULTI_FUNCTION_AFEX:
14691255736Sdavidch            SET_FLAGS(flags, MODE_MF_AFEX);
14692255736Sdavidch            break;
14693255736Sdavidch        }
14694255736Sdavidch    } else {
14695255736Sdavidch        SET_FLAGS(flags, MODE_SF);
14696255736Sdavidch    }
14697255736Sdavidch
14698255736Sdavidch#if defined(__LITTLE_ENDIAN)
14699255736Sdavidch    SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14700255736Sdavidch#else /* __BIG_ENDIAN */
14701255736Sdavidch    SET_FLAGS(flags, MODE_BIG_ENDIAN);
14702255736Sdavidch#endif
14703255736Sdavidch
14704255736Sdavidch    INIT_MODE_FLAGS(sc) = flags;
14705255736Sdavidch}
14706255736Sdavidch
14707255736Sdavidchstatic int
14708255736Sdavidchbxe_alloc_hsi_mem(struct bxe_softc *sc)
14709255736Sdavidch{
14710255736Sdavidch    struct bxe_fastpath *fp;
14711255736Sdavidch    bus_addr_t busaddr;
14712255736Sdavidch    int max_agg_queues;
14713255736Sdavidch    int max_segments;
14714255736Sdavidch    bus_size_t max_size;
14715255736Sdavidch    bus_size_t max_seg_size;
14716255736Sdavidch    char buf[32];
14717255736Sdavidch    int rc;
14718255736Sdavidch    int i, j;
14719255736Sdavidch
14720255736Sdavidch    /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14721255736Sdavidch
14722255736Sdavidch    /* allocate the parent bus DMA tag */
14723255736Sdavidch    rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14724255736Sdavidch                            1,                        /* alignment */
14725255736Sdavidch                            0,                        /* boundary limit */
14726255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted low */
14727255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted hi */
14728255736Sdavidch                            NULL,                     /* addr filter() */
14729255736Sdavidch                            NULL,                     /* addr filter() arg */
14730255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14731255736Sdavidch                            BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14732255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14733255736Sdavidch                            0,                        /* flags */
14734255736Sdavidch                            NULL,                     /* lock() */
14735255736Sdavidch                            NULL,                     /* lock() arg */
14736255736Sdavidch                            &sc->parent_dma_tag);     /* returned dma tag */
14737255736Sdavidch    if (rc != 0) {
14738255736Sdavidch        BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14739255736Sdavidch        return (1);
14740255736Sdavidch    }
14741255736Sdavidch
14742255736Sdavidch    /************************/
14743255736Sdavidch    /* DEFAULT STATUS BLOCK */
14744255736Sdavidch    /************************/
14745255736Sdavidch
14746255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14747255736Sdavidch                      &sc->def_sb_dma, "default status block") != 0) {
14748255736Sdavidch        /* XXX */
14749255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14750255736Sdavidch        return (1);
14751255736Sdavidch    }
14752255736Sdavidch
14753255736Sdavidch    sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14754255736Sdavidch
14755255736Sdavidch    /***************/
14756255736Sdavidch    /* EVENT QUEUE */
14757255736Sdavidch    /***************/
14758255736Sdavidch
14759255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14760255736Sdavidch                      &sc->eq_dma, "event queue") != 0) {
14761255736Sdavidch        /* XXX */
14762255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14763255736Sdavidch        sc->def_sb = NULL;
14764255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14765255736Sdavidch        return (1);
14766255736Sdavidch    }
14767255736Sdavidch
14768255736Sdavidch    sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14769255736Sdavidch
14770255736Sdavidch    /*************/
14771255736Sdavidch    /* SLOW PATH */
14772255736Sdavidch    /*************/
14773255736Sdavidch
14774255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14775255736Sdavidch                      &sc->sp_dma, "slow path") != 0) {
14776255736Sdavidch        /* XXX */
14777255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14778255736Sdavidch        sc->eq = NULL;
14779255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14780255736Sdavidch        sc->def_sb = NULL;
14781255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14782255736Sdavidch        return (1);
14783255736Sdavidch    }
14784255736Sdavidch
14785255736Sdavidch    sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14786255736Sdavidch
14787255736Sdavidch    /*******************/
14788255736Sdavidch    /* SLOW PATH QUEUE */
14789255736Sdavidch    /*******************/
14790255736Sdavidch
14791255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14792255736Sdavidch                      &sc->spq_dma, "slow path queue") != 0) {
14793255736Sdavidch        /* XXX */
14794255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14795255736Sdavidch        sc->sp = NULL;
14796255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14797255736Sdavidch        sc->eq = NULL;
14798255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14799255736Sdavidch        sc->def_sb = NULL;
14800255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14801255736Sdavidch        return (1);
14802255736Sdavidch    }
14803255736Sdavidch
14804255736Sdavidch    sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14805255736Sdavidch
14806255736Sdavidch    /***************************/
14807255736Sdavidch    /* FW DECOMPRESSION BUFFER */
14808255736Sdavidch    /***************************/
14809255736Sdavidch
14810255736Sdavidch    if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14811255736Sdavidch                      "fw decompression buffer") != 0) {
14812255736Sdavidch        /* XXX */
14813255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14814255736Sdavidch        sc->spq = NULL;
14815255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14816255736Sdavidch        sc->sp = NULL;
14817255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14818255736Sdavidch        sc->eq = NULL;
14819255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14820255736Sdavidch        sc->def_sb = NULL;
14821255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14822255736Sdavidch        return (1);
14823255736Sdavidch    }
14824255736Sdavidch
14825255736Sdavidch    sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14826255736Sdavidch
14827255736Sdavidch    if ((sc->gz_strm =
14828255736Sdavidch         malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14829255736Sdavidch        /* XXX */
14830255736Sdavidch        bxe_dma_free(sc, &sc->gz_buf_dma);
14831255736Sdavidch        sc->gz_buf = NULL;
14832255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14833255736Sdavidch        sc->spq = NULL;
14834255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14835255736Sdavidch        sc->sp = NULL;
14836255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14837255736Sdavidch        sc->eq = NULL;
14838255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14839255736Sdavidch        sc->def_sb = NULL;
14840255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14841255736Sdavidch        return (1);
14842255736Sdavidch    }
14843255736Sdavidch
14844255736Sdavidch    /*************/
14845255736Sdavidch    /* FASTPATHS */
14846255736Sdavidch    /*************/
14847255736Sdavidch
14848255736Sdavidch    /* allocate DMA memory for each fastpath structure */
14849255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
14850255736Sdavidch        fp = &sc->fp[i];
14851255736Sdavidch        fp->sc    = sc;
14852255736Sdavidch        fp->index = i;
14853255736Sdavidch
14854255736Sdavidch        /*******************/
14855255736Sdavidch        /* FP STATUS BLOCK */
14856255736Sdavidch        /*******************/
14857255736Sdavidch
14858255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d status block", i);
14859255736Sdavidch        if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14860255736Sdavidch                          &fp->sb_dma, buf) != 0) {
14861255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14862255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14863255736Sdavidch            return (1);
14864255736Sdavidch        } else {
14865255736Sdavidch            if (CHIP_IS_E2E3(sc)) {
14866255736Sdavidch                fp->status_block.e2_sb =
14867255736Sdavidch                    (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14868255736Sdavidch            } else {
14869255736Sdavidch                fp->status_block.e1x_sb =
14870255736Sdavidch                    (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14871255736Sdavidch            }
14872255736Sdavidch        }
14873255736Sdavidch
14874255736Sdavidch        /******************/
14875255736Sdavidch        /* FP TX BD CHAIN */
14876255736Sdavidch        /******************/
14877255736Sdavidch
14878255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14879255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14880255736Sdavidch                          &fp->tx_dma, buf) != 0) {
14881255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14882255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14883255736Sdavidch            return (1);
14884255736Sdavidch        } else {
14885255736Sdavidch            fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14886255736Sdavidch        }
14887255736Sdavidch
14888255736Sdavidch        /* link together the tx bd chain pages */
14889255736Sdavidch        for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14890255736Sdavidch            /* index into the tx bd chain array to last entry per page */
14891255736Sdavidch            struct eth_tx_next_bd *tx_next_bd =
14892255736Sdavidch                &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14893255736Sdavidch            /* point to the next page and wrap from last page */
14894255736Sdavidch            busaddr = (fp->tx_dma.paddr +
14895255736Sdavidch                       (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14896255736Sdavidch            tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14897255736Sdavidch            tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14898255736Sdavidch        }
14899255736Sdavidch
14900255736Sdavidch        /******************/
14901255736Sdavidch        /* FP RX BD CHAIN */
14902255736Sdavidch        /******************/
14903255736Sdavidch
14904255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14905255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14906255736Sdavidch                          &fp->rx_dma, buf) != 0) {
14907255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14908255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14909255736Sdavidch            return (1);
14910255736Sdavidch        } else {
14911255736Sdavidch            fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14912255736Sdavidch        }
14913255736Sdavidch
14914255736Sdavidch        /* link together the rx bd chain pages */
14915255736Sdavidch        for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14916255736Sdavidch            /* index into the rx bd chain array to last entry per page */
14917255736Sdavidch            struct eth_rx_bd *rx_bd =
14918255736Sdavidch                &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14919255736Sdavidch            /* point to the next page and wrap from last page */
14920255736Sdavidch            busaddr = (fp->rx_dma.paddr +
14921255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14922255736Sdavidch            rx_bd->addr_hi = htole32(U64_HI(busaddr));
14923255736Sdavidch            rx_bd->addr_lo = htole32(U64_LO(busaddr));
14924255736Sdavidch        }
14925255736Sdavidch
14926255736Sdavidch        /*******************/
14927255736Sdavidch        /* FP RX RCQ CHAIN */
14928255736Sdavidch        /*******************/
14929255736Sdavidch
14930255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14931255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14932255736Sdavidch                          &fp->rcq_dma, buf) != 0) {
14933255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14934255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14935255736Sdavidch            return (1);
14936255736Sdavidch        } else {
14937255736Sdavidch            fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14938255736Sdavidch        }
14939255736Sdavidch
14940255736Sdavidch        /* link together the rcq chain pages */
14941255736Sdavidch        for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14942255736Sdavidch            /* index into the rcq chain array to last entry per page */
14943255736Sdavidch            struct eth_rx_cqe_next_page *rx_cqe_next =
14944255736Sdavidch                (struct eth_rx_cqe_next_page *)
14945255736Sdavidch                &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14946255736Sdavidch            /* point to the next page and wrap from last page */
14947255736Sdavidch            busaddr = (fp->rcq_dma.paddr +
14948255736Sdavidch                       (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14949255736Sdavidch            rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14950255736Sdavidch            rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14951255736Sdavidch        }
14952255736Sdavidch
14953255736Sdavidch        /*******************/
14954255736Sdavidch        /* FP RX SGE CHAIN */
14955255736Sdavidch        /*******************/
14956255736Sdavidch
14957255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14958255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14959255736Sdavidch                          &fp->rx_sge_dma, buf) != 0) {
14960255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14961255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14962255736Sdavidch            return (1);
14963255736Sdavidch        } else {
14964255736Sdavidch            fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14965255736Sdavidch        }
14966255736Sdavidch
14967255736Sdavidch        /* link together the sge chain pages */
14968255736Sdavidch        for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14969255736Sdavidch            /* index into the rcq chain array to last entry per page */
14970255736Sdavidch            struct eth_rx_sge *rx_sge =
14971255736Sdavidch                &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14972255736Sdavidch            /* point to the next page and wrap from last page */
14973255736Sdavidch            busaddr = (fp->rx_sge_dma.paddr +
14974255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14975255736Sdavidch            rx_sge->addr_hi = htole32(U64_HI(busaddr));
14976255736Sdavidch            rx_sge->addr_lo = htole32(U64_LO(busaddr));
14977255736Sdavidch        }
14978255736Sdavidch
14979255736Sdavidch        /***********************/
14980255736Sdavidch        /* FP TX MBUF DMA MAPS */
14981255736Sdavidch        /***********************/
14982255736Sdavidch
14983255736Sdavidch        /* set required sizes before mapping to conserve resources */
14984266979Smarcel        if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
14985255736Sdavidch            max_size     = BXE_TSO_MAX_SIZE;
14986255736Sdavidch            max_segments = BXE_TSO_MAX_SEGMENTS;
14987255736Sdavidch            max_seg_size = BXE_TSO_MAX_SEG_SIZE;
14988255736Sdavidch        } else {
14989255736Sdavidch            max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
14990255736Sdavidch            max_segments = BXE_MAX_SEGMENTS;
14991255736Sdavidch            max_seg_size = MCLBYTES;
14992255736Sdavidch        }
14993255736Sdavidch
14994255736Sdavidch        /* create a dma tag for the tx mbufs */
14995255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14996255736Sdavidch                                1,                  /* alignment */
14997255736Sdavidch                                0,                  /* boundary limit */
14998255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14999255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
15000255736Sdavidch                                NULL,               /* addr filter() */
15001255736Sdavidch                                NULL,               /* addr filter() arg */
15002255736Sdavidch                                max_size,           /* max map size */
15003255736Sdavidch                                max_segments,       /* num discontinuous */
15004255736Sdavidch                                max_seg_size,       /* max seg size */
15005255736Sdavidch                                0,                  /* flags */
15006255736Sdavidch                                NULL,               /* lock() */
15007255736Sdavidch                                NULL,               /* lock() arg */
15008255736Sdavidch                                &fp->tx_mbuf_tag);  /* returned dma tag */
15009255736Sdavidch        if (rc != 0) {
15010255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15011255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
15012295830Sdavidcs                      "'fp %d tx mbufs' (%d)\n", i, rc);
15013255736Sdavidch            return (1);
15014255736Sdavidch        }
15015255736Sdavidch
15016255736Sdavidch        /* create dma maps for each of the tx mbuf clusters */
15017255736Sdavidch        for (j = 0; j < TX_BD_TOTAL; j++) {
15018255736Sdavidch            if (bus_dmamap_create(fp->tx_mbuf_tag,
15019255736Sdavidch                                  BUS_DMA_NOWAIT,
15020255736Sdavidch                                  &fp->tx_mbuf_chain[j].m_map)) {
15021255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15022255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15023295830Sdavidcs                          "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
15024255736Sdavidch                return (1);
15025255736Sdavidch            }
15026255736Sdavidch        }
15027255736Sdavidch
15028255736Sdavidch        /***********************/
15029255736Sdavidch        /* FP RX MBUF DMA MAPS */
15030255736Sdavidch        /***********************/
15031255736Sdavidch
15032255736Sdavidch        /* create a dma tag for the rx mbufs */
15033255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15034255736Sdavidch                                1,                  /* alignment */
15035255736Sdavidch                                0,                  /* boundary limit */
15036255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
15037255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
15038255736Sdavidch                                NULL,               /* addr filter() */
15039255736Sdavidch                                NULL,               /* addr filter() arg */
15040255736Sdavidch                                MJUM9BYTES,         /* max map size */
15041255736Sdavidch                                1,                  /* num discontinuous */
15042255736Sdavidch                                MJUM9BYTES,         /* max seg size */
15043255736Sdavidch                                0,                  /* flags */
15044255736Sdavidch                                NULL,               /* lock() */
15045255736Sdavidch                                NULL,               /* lock() arg */
15046255736Sdavidch                                &fp->rx_mbuf_tag);  /* returned dma tag */
15047255736Sdavidch        if (rc != 0) {
15048255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15049255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
15050295830Sdavidcs                      "'fp %d rx mbufs' (%d)\n", i, rc);
15051255736Sdavidch            return (1);
15052255736Sdavidch        }
15053255736Sdavidch
15054255736Sdavidch        /* create dma maps for each of the rx mbuf clusters */
15055255736Sdavidch        for (j = 0; j < RX_BD_TOTAL; j++) {
15056255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
15057255736Sdavidch                                  BUS_DMA_NOWAIT,
15058255736Sdavidch                                  &fp->rx_mbuf_chain[j].m_map)) {
15059255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15060255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15061295830Sdavidcs                          "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
15062255736Sdavidch                return (1);
15063255736Sdavidch            }
15064255736Sdavidch        }
15065255736Sdavidch
15066255736Sdavidch        /* create dma map for the spare rx mbuf cluster */
15067255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
15068255736Sdavidch                              BUS_DMA_NOWAIT,
15069255736Sdavidch                              &fp->rx_mbuf_spare_map)) {
15070255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15071255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
15072295830Sdavidcs                      "'fp %d spare rx mbuf' (%d)\n", i, rc);
15073255736Sdavidch            return (1);
15074255736Sdavidch        }
15075255736Sdavidch
15076255736Sdavidch        /***************************/
15077255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
15078255736Sdavidch        /***************************/
15079255736Sdavidch
15080255736Sdavidch        /* create a dma tag for the rx sge mbufs */
15081255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15082255736Sdavidch                                1,                  /* alignment */
15083255736Sdavidch                                0,                  /* boundary limit */
15084255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
15085255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
15086255736Sdavidch                                NULL,               /* addr filter() */
15087255736Sdavidch                                NULL,               /* addr filter() arg */
15088255736Sdavidch                                BCM_PAGE_SIZE,      /* max map size */
15089255736Sdavidch                                1,                  /* num discontinuous */
15090255736Sdavidch                                BCM_PAGE_SIZE,      /* max seg size */
15091255736Sdavidch                                0,                  /* flags */
15092255736Sdavidch                                NULL,               /* lock() */
15093255736Sdavidch                                NULL,               /* lock() arg */
15094255736Sdavidch                                &fp->rx_sge_mbuf_tag); /* returned dma tag */
15095255736Sdavidch        if (rc != 0) {
15096255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15097255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
15098295830Sdavidcs                      "'fp %d rx sge mbufs' (%d)\n", i, rc);
15099255736Sdavidch            return (1);
15100255736Sdavidch        }
15101255736Sdavidch
15102255736Sdavidch        /* create dma maps for the rx sge mbuf clusters */
15103255736Sdavidch        for (j = 0; j < RX_SGE_TOTAL; j++) {
15104255736Sdavidch            if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15105255736Sdavidch                                  BUS_DMA_NOWAIT,
15106255736Sdavidch                                  &fp->rx_sge_mbuf_chain[j].m_map)) {
15107255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15108255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15109295830Sdavidcs                          "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
15110255736Sdavidch                return (1);
15111255736Sdavidch            }
15112255736Sdavidch        }
15113255736Sdavidch
15114255736Sdavidch        /* create dma map for the spare rx sge mbuf cluster */
15115255736Sdavidch        if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15116255736Sdavidch                              BUS_DMA_NOWAIT,
15117255736Sdavidch                              &fp->rx_sge_mbuf_spare_map)) {
15118255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15119255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
15120295830Sdavidcs                      "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
15121255736Sdavidch            return (1);
15122255736Sdavidch        }
15123255736Sdavidch
15124255736Sdavidch        /***************************/
15125255736Sdavidch        /* FP RX TPA MBUF DMA MAPS */
15126255736Sdavidch        /***************************/
15127255736Sdavidch
15128255736Sdavidch        /* create dma maps for the rx tpa mbuf clusters */
15129255736Sdavidch        max_agg_queues = MAX_AGG_QS(sc);
15130255736Sdavidch
15131255736Sdavidch        for (j = 0; j < max_agg_queues; j++) {
15132255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
15133255736Sdavidch                                  BUS_DMA_NOWAIT,
15134255736Sdavidch                                  &fp->rx_tpa_info[j].bd.m_map)) {
15135255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
15136255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
15137295830Sdavidcs                          "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
15138255736Sdavidch                return (1);
15139255736Sdavidch            }
15140255736Sdavidch        }
15141255736Sdavidch
15142255736Sdavidch        /* create dma map for the spare rx tpa mbuf cluster */
15143255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
15144255736Sdavidch                              BUS_DMA_NOWAIT,
15145255736Sdavidch                              &fp->rx_tpa_info_mbuf_spare_map)) {
15146255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
15147255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
15148295830Sdavidcs                      "'fp %d spare rx tpa mbuf' (%d)\n", i, rc);
15149255736Sdavidch            return (1);
15150255736Sdavidch        }
15151255736Sdavidch
15152255736Sdavidch        bxe_init_sge_ring_bit_mask(fp);
15153255736Sdavidch    }
15154255736Sdavidch
15155255736Sdavidch    return (0);
15156255736Sdavidch}
15157255736Sdavidch
15158255736Sdavidchstatic void
15159255736Sdavidchbxe_free_hsi_mem(struct bxe_softc *sc)
15160255736Sdavidch{
15161255736Sdavidch    struct bxe_fastpath *fp;
15162255736Sdavidch    int max_agg_queues;
15163255736Sdavidch    int i, j;
15164255736Sdavidch
15165255736Sdavidch    if (sc->parent_dma_tag == NULL) {
15166255736Sdavidch        return; /* assume nothing was allocated */
15167255736Sdavidch    }
15168255736Sdavidch
15169255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
15170255736Sdavidch        fp = &sc->fp[i];
15171255736Sdavidch
15172255736Sdavidch        /*******************/
15173255736Sdavidch        /* FP STATUS BLOCK */
15174255736Sdavidch        /*******************/
15175255736Sdavidch
15176255736Sdavidch        bxe_dma_free(sc, &fp->sb_dma);
15177255736Sdavidch        memset(&fp->status_block, 0, sizeof(fp->status_block));
15178255736Sdavidch
15179255736Sdavidch        /******************/
15180255736Sdavidch        /* FP TX BD CHAIN */
15181255736Sdavidch        /******************/
15182255736Sdavidch
15183255736Sdavidch        bxe_dma_free(sc, &fp->tx_dma);
15184255736Sdavidch        fp->tx_chain = NULL;
15185255736Sdavidch
15186255736Sdavidch        /******************/
15187255736Sdavidch        /* FP RX BD CHAIN */
15188255736Sdavidch        /******************/
15189255736Sdavidch
15190255736Sdavidch        bxe_dma_free(sc, &fp->rx_dma);
15191255736Sdavidch        fp->rx_chain = NULL;
15192255736Sdavidch
15193255736Sdavidch        /*******************/
15194255736Sdavidch        /* FP RX RCQ CHAIN */
15195255736Sdavidch        /*******************/
15196255736Sdavidch
15197255736Sdavidch        bxe_dma_free(sc, &fp->rcq_dma);
15198255736Sdavidch        fp->rcq_chain = NULL;
15199255736Sdavidch
15200255736Sdavidch        /*******************/
15201255736Sdavidch        /* FP RX SGE CHAIN */
15202255736Sdavidch        /*******************/
15203255736Sdavidch
15204255736Sdavidch        bxe_dma_free(sc, &fp->rx_sge_dma);
15205255736Sdavidch        fp->rx_sge_chain = NULL;
15206255736Sdavidch
15207255736Sdavidch        /***********************/
15208255736Sdavidch        /* FP TX MBUF DMA MAPS */
15209255736Sdavidch        /***********************/
15210255736Sdavidch
15211255736Sdavidch        if (fp->tx_mbuf_tag != NULL) {
15212255736Sdavidch            for (j = 0; j < TX_BD_TOTAL; j++) {
15213255736Sdavidch                if (fp->tx_mbuf_chain[j].m_map != NULL) {
15214255736Sdavidch                    bus_dmamap_unload(fp->tx_mbuf_tag,
15215255736Sdavidch                                      fp->tx_mbuf_chain[j].m_map);
15216255736Sdavidch                    bus_dmamap_destroy(fp->tx_mbuf_tag,
15217255736Sdavidch                                       fp->tx_mbuf_chain[j].m_map);
15218255736Sdavidch                }
15219255736Sdavidch            }
15220255736Sdavidch
15221255736Sdavidch            bus_dma_tag_destroy(fp->tx_mbuf_tag);
15222255736Sdavidch            fp->tx_mbuf_tag = NULL;
15223255736Sdavidch        }
15224255736Sdavidch
15225255736Sdavidch        /***********************/
15226255736Sdavidch        /* FP RX MBUF DMA MAPS */
15227255736Sdavidch        /***********************/
15228255736Sdavidch
15229255736Sdavidch        if (fp->rx_mbuf_tag != NULL) {
15230255736Sdavidch            for (j = 0; j < RX_BD_TOTAL; j++) {
15231255736Sdavidch                if (fp->rx_mbuf_chain[j].m_map != NULL) {
15232255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
15233255736Sdavidch                                      fp->rx_mbuf_chain[j].m_map);
15234255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
15235255736Sdavidch                                       fp->rx_mbuf_chain[j].m_map);
15236255736Sdavidch                }
15237255736Sdavidch            }
15238255736Sdavidch
15239255736Sdavidch            if (fp->rx_mbuf_spare_map != NULL) {
15240255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15241255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15242255736Sdavidch            }
15243255736Sdavidch
15244255736Sdavidch            /***************************/
15245255736Sdavidch            /* FP RX TPA MBUF DMA MAPS */
15246255736Sdavidch            /***************************/
15247255736Sdavidch
15248255736Sdavidch            max_agg_queues = MAX_AGG_QS(sc);
15249255736Sdavidch
15250255736Sdavidch            for (j = 0; j < max_agg_queues; j++) {
15251255736Sdavidch                if (fp->rx_tpa_info[j].bd.m_map != NULL) {
15252255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
15253255736Sdavidch                                      fp->rx_tpa_info[j].bd.m_map);
15254255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
15255255736Sdavidch                                       fp->rx_tpa_info[j].bd.m_map);
15256255736Sdavidch                }
15257255736Sdavidch            }
15258255736Sdavidch
15259255736Sdavidch            if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
15260255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag,
15261255736Sdavidch                                  fp->rx_tpa_info_mbuf_spare_map);
15262255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag,
15263255736Sdavidch                                   fp->rx_tpa_info_mbuf_spare_map);
15264255736Sdavidch            }
15265255736Sdavidch
15266255736Sdavidch            bus_dma_tag_destroy(fp->rx_mbuf_tag);
15267255736Sdavidch            fp->rx_mbuf_tag = NULL;
15268255736Sdavidch        }
15269255736Sdavidch
15270255736Sdavidch        /***************************/
15271255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
15272255736Sdavidch        /***************************/
15273255736Sdavidch
15274255736Sdavidch        if (fp->rx_sge_mbuf_tag != NULL) {
15275255736Sdavidch            for (j = 0; j < RX_SGE_TOTAL; j++) {
15276255736Sdavidch                if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
15277255736Sdavidch                    bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15278255736Sdavidch                                      fp->rx_sge_mbuf_chain[j].m_map);
15279255736Sdavidch                    bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15280255736Sdavidch                                       fp->rx_sge_mbuf_chain[j].m_map);
15281255736Sdavidch                }
15282255736Sdavidch            }
15283255736Sdavidch
15284255736Sdavidch            if (fp->rx_sge_mbuf_spare_map != NULL) {
15285255736Sdavidch                bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15286255736Sdavidch                                  fp->rx_sge_mbuf_spare_map);
15287255736Sdavidch                bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15288255736Sdavidch                                   fp->rx_sge_mbuf_spare_map);
15289255736Sdavidch            }
15290255736Sdavidch
15291255736Sdavidch            bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
15292255736Sdavidch            fp->rx_sge_mbuf_tag = NULL;
15293255736Sdavidch        }
15294255736Sdavidch    }
15295255736Sdavidch
15296255736Sdavidch    /***************************/
15297255736Sdavidch    /* FW DECOMPRESSION BUFFER */
15298255736Sdavidch    /***************************/
15299255736Sdavidch
15300255736Sdavidch    bxe_dma_free(sc, &sc->gz_buf_dma);
15301255736Sdavidch    sc->gz_buf = NULL;
15302255736Sdavidch    free(sc->gz_strm, M_DEVBUF);
15303255736Sdavidch    sc->gz_strm = NULL;
15304255736Sdavidch
15305255736Sdavidch    /*******************/
15306255736Sdavidch    /* SLOW PATH QUEUE */
15307255736Sdavidch    /*******************/
15308255736Sdavidch
15309255736Sdavidch    bxe_dma_free(sc, &sc->spq_dma);
15310255736Sdavidch    sc->spq = NULL;
15311255736Sdavidch
15312255736Sdavidch    /*************/
15313255736Sdavidch    /* SLOW PATH */
15314255736Sdavidch    /*************/
15315255736Sdavidch
15316255736Sdavidch    bxe_dma_free(sc, &sc->sp_dma);
15317255736Sdavidch    sc->sp = NULL;
15318255736Sdavidch
15319255736Sdavidch    /***************/
15320255736Sdavidch    /* EVENT QUEUE */
15321255736Sdavidch    /***************/
15322255736Sdavidch
15323255736Sdavidch    bxe_dma_free(sc, &sc->eq_dma);
15324255736Sdavidch    sc->eq = NULL;
15325255736Sdavidch
15326255736Sdavidch    /************************/
15327255736Sdavidch    /* DEFAULT STATUS BLOCK */
15328255736Sdavidch    /************************/
15329255736Sdavidch
15330255736Sdavidch    bxe_dma_free(sc, &sc->def_sb_dma);
15331255736Sdavidch    sc->def_sb = NULL;
15332255736Sdavidch
15333255736Sdavidch    bus_dma_tag_destroy(sc->parent_dma_tag);
15334255736Sdavidch    sc->parent_dma_tag = NULL;
15335255736Sdavidch}
15336255736Sdavidch
15337255736Sdavidch/*
15338255736Sdavidch * Previous driver DMAE transaction may have occurred when pre-boot stage
15339255736Sdavidch * ended and boot began. This would invalidate the addresses of the
15340255736Sdavidch * transaction, resulting in was-error bit set in the PCI causing all
15341255736Sdavidch * hw-to-host PCIe transactions to timeout. If this happened we want to clear
15342255736Sdavidch * the interrupt which detected this from the pglueb and the was-done bit
15343255736Sdavidch */
15344255736Sdavidchstatic void
15345255736Sdavidchbxe_prev_interrupted_dmae(struct bxe_softc *sc)
15346255736Sdavidch{
15347255736Sdavidch    uint32_t val;
15348255736Sdavidch
15349255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
15350255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
15351255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
15352255736Sdavidch            BLOGD(sc, DBG_LOAD,
15353255736Sdavidch                  "Clearing 'was-error' bit that was set in pglueb");
15354255736Sdavidch            REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
15355255736Sdavidch        }
15356255736Sdavidch    }
15357255736Sdavidch}
15358255736Sdavidch
15359255736Sdavidchstatic int
15360255736Sdavidchbxe_prev_mcp_done(struct bxe_softc *sc)
15361255736Sdavidch{
15362255736Sdavidch    uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15363255736Sdavidch                                 DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15364255736Sdavidch    if (!rc) {
15365255736Sdavidch        BLOGE(sc, "MCP response failure, aborting\n");
15366255736Sdavidch        return (-1);
15367255736Sdavidch    }
15368255736Sdavidch
15369255736Sdavidch    return (0);
15370255736Sdavidch}
15371255736Sdavidch
15372255736Sdavidchstatic struct bxe_prev_list_node *
15373255736Sdavidchbxe_prev_path_get_entry(struct bxe_softc *sc)
15374255736Sdavidch{
15375255736Sdavidch    struct bxe_prev_list_node *tmp;
15376255736Sdavidch
15377255736Sdavidch    LIST_FOREACH(tmp, &bxe_prev_list, node) {
15378255736Sdavidch        if ((sc->pcie_bus == tmp->bus) &&
15379255736Sdavidch            (sc->pcie_device == tmp->slot) &&
15380255736Sdavidch            (SC_PATH(sc) == tmp->path)) {
15381255736Sdavidch            return (tmp);
15382255736Sdavidch        }
15383255736Sdavidch    }
15384255736Sdavidch
15385255736Sdavidch    return (NULL);
15386255736Sdavidch}
15387255736Sdavidch
15388255736Sdavidchstatic uint8_t
15389255736Sdavidchbxe_prev_is_path_marked(struct bxe_softc *sc)
15390255736Sdavidch{
15391255736Sdavidch    struct bxe_prev_list_node *tmp;
15392255736Sdavidch    int rc = FALSE;
15393255736Sdavidch
15394255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15395255736Sdavidch
15396255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15397255736Sdavidch    if (tmp) {
15398255736Sdavidch        if (tmp->aer) {
15399255736Sdavidch            BLOGD(sc, DBG_LOAD,
15400255736Sdavidch                  "Path %d/%d/%d was marked by AER\n",
15401255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15402255736Sdavidch        } else {
15403255736Sdavidch            rc = TRUE;
15404255736Sdavidch            BLOGD(sc, DBG_LOAD,
15405255736Sdavidch                  "Path %d/%d/%d was already cleaned from previous drivers\n",
15406255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15407255736Sdavidch        }
15408255736Sdavidch    }
15409255736Sdavidch
15410255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15411255736Sdavidch
15412255736Sdavidch    return (rc);
15413255736Sdavidch}
15414255736Sdavidch
15415255736Sdavidchstatic int
15416255736Sdavidchbxe_prev_mark_path(struct bxe_softc *sc,
15417255736Sdavidch                   uint8_t          after_undi)
15418255736Sdavidch{
15419255736Sdavidch    struct bxe_prev_list_node *tmp;
15420255736Sdavidch
15421255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15422255736Sdavidch
15423255736Sdavidch    /* Check whether the entry for this path already exists */
15424255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15425255736Sdavidch    if (tmp) {
15426255736Sdavidch        if (!tmp->aer) {
15427255736Sdavidch            BLOGD(sc, DBG_LOAD,
15428255736Sdavidch                  "Re-marking AER in path %d/%d/%d\n",
15429255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15430255736Sdavidch        } else {
15431255736Sdavidch            BLOGD(sc, DBG_LOAD,
15432255736Sdavidch                  "Removing AER indication from path %d/%d/%d\n",
15433255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15434255736Sdavidch            tmp->aer = 0;
15435255736Sdavidch        }
15436255736Sdavidch
15437255736Sdavidch        mtx_unlock(&bxe_prev_mtx);
15438255736Sdavidch        return (0);
15439255736Sdavidch    }
15440255736Sdavidch
15441255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15442255736Sdavidch
15443255736Sdavidch    /* Create an entry for this path and add it */
15444255736Sdavidch    tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15445255736Sdavidch                 (M_NOWAIT | M_ZERO));
15446255736Sdavidch    if (!tmp) {
15447255736Sdavidch        BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15448255736Sdavidch        return (-1);
15449255736Sdavidch    }
15450255736Sdavidch
15451255736Sdavidch    tmp->bus  = sc->pcie_bus;
15452255736Sdavidch    tmp->slot = sc->pcie_device;
15453255736Sdavidch    tmp->path = SC_PATH(sc);
15454255736Sdavidch    tmp->aer  = 0;
15455255736Sdavidch    tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15456255736Sdavidch
15457255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15458255736Sdavidch
15459255736Sdavidch    BLOGD(sc, DBG_LOAD,
15460255736Sdavidch          "Marked path %d/%d/%d - finished previous unload\n",
15461255736Sdavidch          sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15462255736Sdavidch    LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15463255736Sdavidch
15464255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15465255736Sdavidch
15466255736Sdavidch    return (0);
15467255736Sdavidch}
15468255736Sdavidch
15469255736Sdavidchstatic int
15470255736Sdavidchbxe_do_flr(struct bxe_softc *sc)
15471255736Sdavidch{
15472255736Sdavidch    int i;
15473255736Sdavidch
15474255736Sdavidch    /* only E2 and onwards support FLR */
15475255736Sdavidch    if (CHIP_IS_E1x(sc)) {
15476255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15477255736Sdavidch        return (-1);
15478255736Sdavidch    }
15479255736Sdavidch
15480255736Sdavidch    /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15481255736Sdavidch    if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15482255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15483255736Sdavidch              sc->devinfo.bc_ver);
15484255736Sdavidch        return (-1);
15485255736Sdavidch    }
15486255736Sdavidch
15487255736Sdavidch    /* Wait for Transaction Pending bit clean */
15488255736Sdavidch    for (i = 0; i < 4; i++) {
15489255736Sdavidch        if (i) {
15490255736Sdavidch            DELAY(((1 << (i - 1)) * 100) * 1000);
15491255736Sdavidch        }
15492255736Sdavidch
15493255736Sdavidch        if (!bxe_is_pcie_pending(sc)) {
15494255736Sdavidch            goto clear;
15495255736Sdavidch        }
15496255736Sdavidch    }
15497255736Sdavidch
15498255736Sdavidch    BLOGE(sc, "PCIE transaction is not cleared, "
15499255736Sdavidch              "proceeding with reset anyway\n");
15500255736Sdavidch
15501255736Sdavidchclear:
15502255736Sdavidch
15503255736Sdavidch    BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15504255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15505255736Sdavidch
15506255736Sdavidch    return (0);
15507255736Sdavidch}
15508255736Sdavidch
15509255736Sdavidchstruct bxe_mac_vals {
15510255736Sdavidch    uint32_t xmac_addr;
15511255736Sdavidch    uint32_t xmac_val;
15512255736Sdavidch    uint32_t emac_addr;
15513255736Sdavidch    uint32_t emac_val;
15514255736Sdavidch    uint32_t umac_addr;
15515255736Sdavidch    uint32_t umac_val;
15516255736Sdavidch    uint32_t bmac_addr;
15517255736Sdavidch    uint32_t bmac_val[2];
15518255736Sdavidch};
15519255736Sdavidch
15520255736Sdavidchstatic void
15521255736Sdavidchbxe_prev_unload_close_mac(struct bxe_softc *sc,
15522255736Sdavidch                          struct bxe_mac_vals *vals)
15523255736Sdavidch{
15524255736Sdavidch    uint32_t val, base_addr, offset, mask, reset_reg;
15525255736Sdavidch    uint8_t mac_stopped = FALSE;
15526255736Sdavidch    uint8_t port = SC_PORT(sc);
15527255736Sdavidch    uint32_t wb_data[2];
15528255736Sdavidch
15529255736Sdavidch    /* reset addresses as they also mark which values were changed */
15530255736Sdavidch    vals->bmac_addr = 0;
15531255736Sdavidch    vals->umac_addr = 0;
15532255736Sdavidch    vals->xmac_addr = 0;
15533255736Sdavidch    vals->emac_addr = 0;
15534255736Sdavidch
15535255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15536255736Sdavidch
15537255736Sdavidch    if (!CHIP_IS_E3(sc)) {
15538255736Sdavidch        val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15539255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15540255736Sdavidch        if ((mask & reset_reg) && val) {
15541255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15542255736Sdavidch            base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15543255736Sdavidch                                    : NIG_REG_INGRESS_BMAC0_MEM;
15544255736Sdavidch            offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15545255736Sdavidch                                    : BIGMAC_REGISTER_BMAC_CONTROL;
15546255736Sdavidch
15547255736Sdavidch            /*
15548255736Sdavidch             * use rd/wr since we cannot use dmae. This is safe
15549255736Sdavidch             * since MCP won't access the bus due to the request
15550255736Sdavidch             * to unload, and no function on the path can be
15551255736Sdavidch             * loaded at this time.
15552255736Sdavidch             */
15553255736Sdavidch            wb_data[0] = REG_RD(sc, base_addr + offset);
15554255736Sdavidch            wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15555255736Sdavidch            vals->bmac_addr = base_addr + offset;
15556255736Sdavidch            vals->bmac_val[0] = wb_data[0];
15557255736Sdavidch            vals->bmac_val[1] = wb_data[1];
15558255736Sdavidch            wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15559255736Sdavidch            REG_WR(sc, vals->bmac_addr, wb_data[0]);
15560255736Sdavidch            REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15561255736Sdavidch        }
15562255736Sdavidch
15563255736Sdavidch        BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15564255736Sdavidch        vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15565255736Sdavidch        vals->emac_val = REG_RD(sc, vals->emac_addr);
15566255736Sdavidch        REG_WR(sc, vals->emac_addr, 0);
15567255736Sdavidch        mac_stopped = TRUE;
15568255736Sdavidch    } else {
15569255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15570255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15571255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15572255736Sdavidch            val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15573255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15574255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15575255736Sdavidch            vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15576255736Sdavidch            vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15577255736Sdavidch            REG_WR(sc, vals->xmac_addr, 0);
15578255736Sdavidch            mac_stopped = TRUE;
15579255736Sdavidch        }
15580255736Sdavidch
15581255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15582255736Sdavidch        if (mask & reset_reg) {
15583255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15584255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15585255736Sdavidch            vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15586255736Sdavidch            vals->umac_val = REG_RD(sc, vals->umac_addr);
15587255736Sdavidch            REG_WR(sc, vals->umac_addr, 0);
15588255736Sdavidch            mac_stopped = TRUE;
15589255736Sdavidch        }
15590255736Sdavidch    }
15591255736Sdavidch
15592255736Sdavidch    if (mac_stopped) {
15593255736Sdavidch        DELAY(20000);
15594255736Sdavidch    }
15595255736Sdavidch}
15596255736Sdavidch
15597255736Sdavidch#define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15598255736Sdavidch#define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15599255736Sdavidch#define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15600255736Sdavidch#define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15601255736Sdavidch
15602255736Sdavidchstatic void
15603255736Sdavidchbxe_prev_unload_undi_inc(struct bxe_softc *sc,
15604255736Sdavidch                         uint8_t          port,
15605255736Sdavidch                         uint8_t          inc)
15606255736Sdavidch{
15607255736Sdavidch    uint16_t rcq, bd;
15608255736Sdavidch    uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15609255736Sdavidch
15610255736Sdavidch    rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15611255736Sdavidch    bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15612255736Sdavidch
15613255736Sdavidch    tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15614255736Sdavidch    REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15615255736Sdavidch
15616255736Sdavidch    BLOGD(sc, DBG_LOAD,
15617255736Sdavidch          "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15618255736Sdavidch          port, bd, rcq);
15619255736Sdavidch}
15620255736Sdavidch
15621255736Sdavidchstatic int
15622255736Sdavidchbxe_prev_unload_common(struct bxe_softc *sc)
15623255736Sdavidch{
15624255736Sdavidch    uint32_t reset_reg, tmp_reg = 0, rc;
15625255736Sdavidch    uint8_t prev_undi = FALSE;
15626255736Sdavidch    struct bxe_mac_vals mac_vals;
15627255736Sdavidch    uint32_t timer_count = 1000;
15628255736Sdavidch    uint32_t prev_brb;
15629255736Sdavidch
15630255736Sdavidch    /*
15631255736Sdavidch     * It is possible a previous function received 'common' answer,
15632255736Sdavidch     * but hasn't loaded yet, therefore creating a scenario of
15633255736Sdavidch     * multiple functions receiving 'common' on the same path.
15634255736Sdavidch     */
15635255736Sdavidch    BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15636255736Sdavidch
15637255736Sdavidch    memset(&mac_vals, 0, sizeof(mac_vals));
15638255736Sdavidch
15639255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15640255736Sdavidch        return (bxe_prev_mcp_done(sc));
15641255736Sdavidch    }
15642255736Sdavidch
15643255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15644255736Sdavidch
15645255736Sdavidch    /* Reset should be performed after BRB is emptied */
15646255736Sdavidch    if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15647255736Sdavidch        /* Close the MAC Rx to prevent BRB from filling up */
15648255736Sdavidch        bxe_prev_unload_close_mac(sc, &mac_vals);
15649255736Sdavidch
15650255736Sdavidch        /* close LLH filters towards the BRB */
15651255736Sdavidch        elink_set_rx_filter(&sc->link_params, 0);
15652255736Sdavidch
15653255736Sdavidch        /*
15654255736Sdavidch         * Check if the UNDI driver was previously loaded.
15655255736Sdavidch         * UNDI driver initializes CID offset for normal bell to 0x7
15656255736Sdavidch         */
15657255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15658255736Sdavidch            tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15659255736Sdavidch            if (tmp_reg == 0x7) {
15660255736Sdavidch                BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15661255736Sdavidch                prev_undi = TRUE;
15662255736Sdavidch                /* clear the UNDI indication */
15663255736Sdavidch                REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15664255736Sdavidch                /* clear possible idle check errors */
15665255736Sdavidch                REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15666255736Sdavidch            }
15667255736Sdavidch        }
15668255736Sdavidch
15669255736Sdavidch        /* wait until BRB is empty */
15670255736Sdavidch        tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15671255736Sdavidch        while (timer_count) {
15672255736Sdavidch            prev_brb = tmp_reg;
15673255736Sdavidch
15674255736Sdavidch            tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15675255736Sdavidch            if (!tmp_reg) {
15676255736Sdavidch                break;
15677255736Sdavidch            }
15678255736Sdavidch
15679255736Sdavidch            BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15680255736Sdavidch
15681255736Sdavidch            /* reset timer as long as BRB actually gets emptied */
15682255736Sdavidch            if (prev_brb > tmp_reg) {
15683255736Sdavidch                timer_count = 1000;
15684255736Sdavidch            } else {
15685255736Sdavidch                timer_count--;
15686255736Sdavidch            }
15687255736Sdavidch
15688255736Sdavidch            /* If UNDI resides in memory, manually increment it */
15689255736Sdavidch            if (prev_undi) {
15690255736Sdavidch                bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15691255736Sdavidch            }
15692255736Sdavidch
15693255736Sdavidch            DELAY(10);
15694255736Sdavidch        }
15695255736Sdavidch
15696255736Sdavidch        if (!timer_count) {
15697255736Sdavidch            BLOGE(sc, "Failed to empty BRB\n");
15698255736Sdavidch        }
15699255736Sdavidch    }
15700255736Sdavidch
15701255736Sdavidch    /* No packets are in the pipeline, path is ready for reset */
15702255736Sdavidch    bxe_reset_common(sc);
15703255736Sdavidch
15704255736Sdavidch    if (mac_vals.xmac_addr) {
15705255736Sdavidch        REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15706255736Sdavidch    }
15707255736Sdavidch    if (mac_vals.umac_addr) {
15708255736Sdavidch        REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15709255736Sdavidch    }
15710255736Sdavidch    if (mac_vals.emac_addr) {
15711255736Sdavidch        REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15712255736Sdavidch    }
15713255736Sdavidch    if (mac_vals.bmac_addr) {
15714255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15715255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15716255736Sdavidch    }
15717255736Sdavidch
15718255736Sdavidch    rc = bxe_prev_mark_path(sc, prev_undi);
15719255736Sdavidch    if (rc) {
15720255736Sdavidch        bxe_prev_mcp_done(sc);
15721255736Sdavidch        return (rc);
15722255736Sdavidch    }
15723255736Sdavidch
15724255736Sdavidch    return (bxe_prev_mcp_done(sc));
15725255736Sdavidch}
15726255736Sdavidch
15727255736Sdavidchstatic int
15728255736Sdavidchbxe_prev_unload_uncommon(struct bxe_softc *sc)
15729255736Sdavidch{
15730255736Sdavidch    int rc;
15731255736Sdavidch
15732255736Sdavidch    BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15733255736Sdavidch
15734255736Sdavidch    /* Test if previous unload process was already finished for this path */
15735255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15736255736Sdavidch        return (bxe_prev_mcp_done(sc));
15737255736Sdavidch    }
15738255736Sdavidch
15739255736Sdavidch    BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15740255736Sdavidch
15741255736Sdavidch    /*
15742255736Sdavidch     * If function has FLR capabilities, and existing FW version matches
15743255736Sdavidch     * the one required, then FLR will be sufficient to clean any residue
15744255736Sdavidch     * left by previous driver
15745255736Sdavidch     */
15746255736Sdavidch    rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15747255736Sdavidch    if (!rc) {
15748255736Sdavidch        /* fw version is good */
15749255736Sdavidch        BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15750255736Sdavidch        rc = bxe_do_flr(sc);
15751255736Sdavidch    }
15752255736Sdavidch
15753255736Sdavidch    if (!rc) {
15754255736Sdavidch        /* FLR was performed */
15755255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR successful\n");
15756255736Sdavidch        return (0);
15757255736Sdavidch    }
15758255736Sdavidch
15759255736Sdavidch    BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15760255736Sdavidch
15761255736Sdavidch    /* Close the MCP request, return failure*/
15762255736Sdavidch    rc = bxe_prev_mcp_done(sc);
15763255736Sdavidch    if (!rc) {
15764255736Sdavidch        rc = BXE_PREV_WAIT_NEEDED;
15765255736Sdavidch    }
15766255736Sdavidch
15767255736Sdavidch    return (rc);
15768255736Sdavidch}
15769255736Sdavidch
15770255736Sdavidchstatic int
15771255736Sdavidchbxe_prev_unload(struct bxe_softc *sc)
15772255736Sdavidch{
15773255736Sdavidch    int time_counter = 10;
15774255736Sdavidch    uint32_t fw, hw_lock_reg, hw_lock_val;
15775255736Sdavidch    uint32_t rc = 0;
15776255736Sdavidch
15777255736Sdavidch    /*
15778255736Sdavidch     * Clear HW from errors which may have resulted from an interrupted
15779255736Sdavidch     * DMAE transaction.
15780255736Sdavidch     */
15781255736Sdavidch    bxe_prev_interrupted_dmae(sc);
15782255736Sdavidch
15783255736Sdavidch    /* Release previously held locks */
15784255736Sdavidch    hw_lock_reg =
15785255736Sdavidch        (SC_FUNC(sc) <= 5) ?
15786255736Sdavidch            (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15787255736Sdavidch            (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15788255736Sdavidch
15789255736Sdavidch    hw_lock_val = (REG_RD(sc, hw_lock_reg));
15790255736Sdavidch    if (hw_lock_val) {
15791255736Sdavidch        if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15792255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15793255736Sdavidch            REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15794255736Sdavidch                   (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15795255736Sdavidch        }
15796255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15797255736Sdavidch        REG_WR(sc, hw_lock_reg, 0xffffffff);
15798255736Sdavidch    } else {
15799255736Sdavidch        BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15800255736Sdavidch    }
15801255736Sdavidch
15802255736Sdavidch    if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15803255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15804255736Sdavidch        REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15805255736Sdavidch    }
15806255736Sdavidch
15807255736Sdavidch    do {
15808255736Sdavidch        /* Lock MCP using an unload request */
15809255736Sdavidch        fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15810255736Sdavidch        if (!fw) {
15811255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
15812255736Sdavidch            rc = -1;
15813255736Sdavidch            break;
15814255736Sdavidch        }
15815255736Sdavidch
15816255736Sdavidch        if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15817255736Sdavidch            rc = bxe_prev_unload_common(sc);
15818255736Sdavidch            break;
15819255736Sdavidch        }
15820255736Sdavidch
15821255736Sdavidch        /* non-common reply from MCP night require looping */
15822255736Sdavidch        rc = bxe_prev_unload_uncommon(sc);
15823255736Sdavidch        if (rc != BXE_PREV_WAIT_NEEDED) {
15824255736Sdavidch            break;
15825255736Sdavidch        }
15826255736Sdavidch
15827255736Sdavidch        DELAY(20000);
15828255736Sdavidch    } while (--time_counter);
15829255736Sdavidch
15830255736Sdavidch    if (!time_counter || rc) {
15831295830Sdavidcs        BLOGE(sc, "Failed to unload previous driver!"
15832295830Sdavidcs            " time_counter %d rc %d\n", time_counter, rc);
15833255736Sdavidch        rc = -1;
15834255736Sdavidch    }
15835255736Sdavidch
15836255736Sdavidch    return (rc);
15837255736Sdavidch}
15838255736Sdavidch
15839255736Sdavidchvoid
15840255736Sdavidchbxe_dcbx_set_state(struct bxe_softc *sc,
15841255736Sdavidch                   uint8_t          dcb_on,
15842255736Sdavidch                   uint32_t         dcbx_enabled)
15843255736Sdavidch{
15844255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
15845255736Sdavidch        sc->dcb_state = dcb_on;
15846255736Sdavidch        sc->dcbx_enabled = dcbx_enabled;
15847255736Sdavidch    } else {
15848255736Sdavidch        sc->dcb_state = FALSE;
15849255736Sdavidch        sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15850255736Sdavidch    }
15851255736Sdavidch    BLOGD(sc, DBG_LOAD,
15852255736Sdavidch          "DCB state [%s:%s]\n",
15853255736Sdavidch          dcb_on ? "ON" : "OFF",
15854255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15855255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15856255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15857255736Sdavidch          "on-chip with negotiation" : "invalid");
15858255736Sdavidch}
15859255736Sdavidch
15860255736Sdavidch/* must be called after sriov-enable */
15861255736Sdavidchstatic int
15862255736Sdavidchbxe_set_qm_cid_count(struct bxe_softc *sc)
15863255736Sdavidch{
15864255736Sdavidch    int cid_count = BXE_L2_MAX_CID(sc);
15865255736Sdavidch
15866255736Sdavidch    if (IS_SRIOV(sc)) {
15867255736Sdavidch        cid_count += BXE_VF_CIDS;
15868255736Sdavidch    }
15869255736Sdavidch
15870255736Sdavidch    if (CNIC_SUPPORT(sc)) {
15871255736Sdavidch        cid_count += CNIC_CID_MAX;
15872255736Sdavidch    }
15873255736Sdavidch
15874255736Sdavidch    return (roundup(cid_count, QM_CID_ROUND));
15875255736Sdavidch}
15876255736Sdavidch
15877255736Sdavidchstatic void
15878255736Sdavidchbxe_init_multi_cos(struct bxe_softc *sc)
15879255736Sdavidch{
15880255736Sdavidch    int pri, cos;
15881255736Sdavidch
15882255736Sdavidch    uint32_t pri_map = 0; /* XXX change to user config */
15883255736Sdavidch
15884255736Sdavidch    for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15885255736Sdavidch        cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15886255736Sdavidch        if (cos < sc->max_cos) {
15887255736Sdavidch            sc->prio_to_cos[pri] = cos;
15888255736Sdavidch        } else {
15889255736Sdavidch            BLOGW(sc, "Invalid COS %d for priority %d "
15890255736Sdavidch                      "(max COS is %d), setting to 0\n",
15891255736Sdavidch                  cos, pri, (sc->max_cos - 1));
15892255736Sdavidch            sc->prio_to_cos[pri] = 0;
15893255736Sdavidch        }
15894255736Sdavidch    }
15895255736Sdavidch}
15896255736Sdavidch
15897255736Sdavidchstatic int
15898255736Sdavidchbxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15899255736Sdavidch{
15900255736Sdavidch    struct bxe_softc *sc;
15901255736Sdavidch    int error, result;
15902255736Sdavidch
15903255736Sdavidch    result = 0;
15904255736Sdavidch    error = sysctl_handle_int(oidp, &result, 0, req);
15905255736Sdavidch
15906255736Sdavidch    if (error || !req->newptr) {
15907255736Sdavidch        return (error);
15908255736Sdavidch    }
15909255736Sdavidch
15910255736Sdavidch    if (result == 1) {
15911292638Sdavidcs        uint32_t  temp;
15912255736Sdavidch        sc = (struct bxe_softc *)arg1;
15913289199Sdavidcs
15914255736Sdavidch        BLOGI(sc, "... dumping driver state ...\n");
15915292638Sdavidcs        temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15916292638Sdavidcs        BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15917255736Sdavidch    }
15918255736Sdavidch
15919255736Sdavidch    return (error);
15920255736Sdavidch}
15921255736Sdavidch
15922255736Sdavidchstatic int
15923255736Sdavidchbxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15924255736Sdavidch{
15925255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15926255736Sdavidch    uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15927255736Sdavidch    uint32_t *offset;
15928255736Sdavidch    uint64_t value = 0;
15929255736Sdavidch    int index = (int)arg2;
15930255736Sdavidch
15931255736Sdavidch    if (index >= BXE_NUM_ETH_STATS) {
15932255736Sdavidch        BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15933255736Sdavidch        return (-1);
15934255736Sdavidch    }
15935255736Sdavidch
15936255736Sdavidch    offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15937255736Sdavidch
15938255736Sdavidch    switch (bxe_eth_stats_arr[index].size) {
15939255736Sdavidch    case 4:
15940255736Sdavidch        value = (uint64_t)*offset;
15941255736Sdavidch        break;
15942255736Sdavidch    case 8:
15943255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15944255736Sdavidch        break;
15945255736Sdavidch    default:
15946255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15947255736Sdavidch              index, bxe_eth_stats_arr[index].size);
15948255736Sdavidch        return (-1);
15949255736Sdavidch    }
15950255736Sdavidch
15951255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15952255736Sdavidch}
15953255736Sdavidch
15954255736Sdavidchstatic int
15955255736Sdavidchbxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15956255736Sdavidch{
15957255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15958255736Sdavidch    uint32_t *eth_stats;
15959255736Sdavidch    uint32_t *offset;
15960255736Sdavidch    uint64_t value = 0;
15961255736Sdavidch    uint32_t q_stat = (uint32_t)arg2;
15962255736Sdavidch    uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15963255736Sdavidch    uint32_t index = (q_stat & 0xffff);
15964255736Sdavidch
15965255736Sdavidch    eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15966255736Sdavidch
15967255736Sdavidch    if (index >= BXE_NUM_ETH_Q_STATS) {
15968255736Sdavidch        BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15969255736Sdavidch        return (-1);
15970255736Sdavidch    }
15971255736Sdavidch
15972255736Sdavidch    offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15973255736Sdavidch
15974255736Sdavidch    switch (bxe_eth_q_stats_arr[index].size) {
15975255736Sdavidch    case 4:
15976255736Sdavidch        value = (uint64_t)*offset;
15977255736Sdavidch        break;
15978255736Sdavidch    case 8:
15979255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15980255736Sdavidch        break;
15981255736Sdavidch    default:
15982255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15983255736Sdavidch              index, bxe_eth_q_stats_arr[index].size);
15984255736Sdavidch        return (-1);
15985255736Sdavidch    }
15986255736Sdavidch
15987255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15988255736Sdavidch}
15989255736Sdavidch
15990315881Sdavidcsstatic void bxe_force_link_reset(struct bxe_softc *sc)
15991315881Sdavidcs{
15992315881Sdavidcs
15993315881Sdavidcs        bxe_acquire_phy_lock(sc);
15994315881Sdavidcs        elink_link_reset(&sc->link_params, &sc->link_vars, 1);
15995315881Sdavidcs        bxe_release_phy_lock(sc);
15996315881Sdavidcs}
15997315881Sdavidcs
15998315881Sdavidcsstatic int
15999315881Sdavidcsbxe_sysctl_pauseparam(SYSCTL_HANDLER_ARGS)
16000315881Sdavidcs{
16001315881Sdavidcs        struct bxe_softc *sc = (struct bxe_softc *)arg1;;
16002315881Sdavidcs        uint32_t cfg_idx = bxe_get_link_cfg_idx(sc);
16003315881Sdavidcs        int rc = 0;
16004315881Sdavidcs        int error;
16005315881Sdavidcs        int result;
16006315881Sdavidcs
16007315881Sdavidcs
16008315881Sdavidcs        error = sysctl_handle_int(oidp, &sc->bxe_pause_param, 0, req);
16009315881Sdavidcs
16010315881Sdavidcs        if (error || !req->newptr) {
16011315881Sdavidcs                return (error);
16012315881Sdavidcs        }
16013315881Sdavidcs        if ((sc->bxe_pause_param < 0) ||  (sc->bxe_pause_param > 8)) {
16014315881Sdavidcs                BLOGW(sc, "invalid pause param (%d) - use intergers between 1 & 8\n",sc->bxe_pause_param);
16015315881Sdavidcs                sc->bxe_pause_param = 8;
16016315881Sdavidcs        }
16017315881Sdavidcs
16018315881Sdavidcs        result = (sc->bxe_pause_param << PORT_FEATURE_FLOW_CONTROL_SHIFT);
16019315881Sdavidcs
16020315881Sdavidcs
16021315881Sdavidcs        if((result & 0x400) && !(sc->port.supported[cfg_idx] & ELINK_SUPPORTED_Autoneg))  {
16022315881Sdavidcs                        BLOGW(sc, "Does not support Autoneg pause_param %d\n", sc->bxe_pause_param);
16023315881Sdavidcs                        return -EINVAL;
16024315881Sdavidcs        }
16025315881Sdavidcs
16026315881Sdavidcs        if(IS_MF(sc))
16027315881Sdavidcs                return 0;
16028315881Sdavidcs       sc->link_params.req_flow_ctrl[cfg_idx] = ELINK_FLOW_CTRL_AUTO;
16029315881Sdavidcs        if(result & ELINK_FLOW_CTRL_RX)
16030315881Sdavidcs                sc->link_params.req_flow_ctrl[cfg_idx] |= ELINK_FLOW_CTRL_RX;
16031315881Sdavidcs
16032315881Sdavidcs        if(result & ELINK_FLOW_CTRL_TX)
16033315881Sdavidcs                sc->link_params.req_flow_ctrl[cfg_idx] |= ELINK_FLOW_CTRL_TX;
16034315881Sdavidcs        if(sc->link_params.req_flow_ctrl[cfg_idx] == ELINK_FLOW_CTRL_AUTO)
16035315881Sdavidcs                sc->link_params.req_flow_ctrl[cfg_idx] = ELINK_FLOW_CTRL_NONE;
16036315881Sdavidcs
16037315881Sdavidcs        if(result & 0x400) {
16038315881Sdavidcs                if (sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG) {
16039315881Sdavidcs                        sc->link_params.req_flow_ctrl[cfg_idx] =
16040315881Sdavidcs                                ELINK_FLOW_CTRL_AUTO;
16041315881Sdavidcs                }
16042315881Sdavidcs                sc->link_params.req_fc_auto_adv = 0;
16043315881Sdavidcs                if (result & ELINK_FLOW_CTRL_RX)
16044315881Sdavidcs                        sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_RX;
16045315881Sdavidcs
16046315881Sdavidcs                if (result & ELINK_FLOW_CTRL_TX)
16047315881Sdavidcs                        sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_TX;
16048315881Sdavidcs                if (!sc->link_params.req_fc_auto_adv)
16049315881Sdavidcs                        sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_NONE;
16050315881Sdavidcs        }
16051315881Sdavidcs         if (IS_PF(sc)) {
16052315881Sdavidcs                        if (sc->link_vars.link_up) {
16053315881Sdavidcs                                bxe_stats_handle(sc, STATS_EVENT_STOP);
16054315881Sdavidcs                        }
16055315881Sdavidcs			if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
16056315881Sdavidcs                        bxe_force_link_reset(sc);
16057315881Sdavidcs                        bxe_acquire_phy_lock(sc);
16058315881Sdavidcs
16059315881Sdavidcs                        rc = elink_phy_init(&sc->link_params, &sc->link_vars);
16060315881Sdavidcs
16061315881Sdavidcs                        bxe_release_phy_lock(sc);
16062315881Sdavidcs
16063315881Sdavidcs                        bxe_calc_fc_adv(sc);
16064315881Sdavidcs                        }
16065315881Sdavidcs        }
16066315881Sdavidcs        return rc;
16067315881Sdavidcs}
16068315881Sdavidcs
16069315881Sdavidcs
16070255736Sdavidchstatic void
16071255736Sdavidchbxe_add_sysctls(struct bxe_softc *sc)
16072255736Sdavidch{
16073255736Sdavidch    struct sysctl_ctx_list *ctx;
16074255736Sdavidch    struct sysctl_oid_list *children;
16075255736Sdavidch    struct sysctl_oid *queue_top, *queue;
16076255736Sdavidch    struct sysctl_oid_list *queue_top_children, *queue_children;
16077255736Sdavidch    char queue_num_buf[32];
16078255736Sdavidch    uint32_t q_stat;
16079255736Sdavidch    int i, j;
16080255736Sdavidch
16081255736Sdavidch    ctx = device_get_sysctl_ctx(sc->dev);
16082255736Sdavidch    children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
16083255736Sdavidch
16084255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
16085255736Sdavidch                      CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
16086255736Sdavidch                      "version");
16087255736Sdavidch
16088255736Sdavidch    snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
16089255736Sdavidch             BCM_5710_FW_MAJOR_VERSION,
16090255736Sdavidch             BCM_5710_FW_MINOR_VERSION,
16091255736Sdavidch             BCM_5710_FW_REVISION_VERSION,
16092255736Sdavidch             BCM_5710_FW_ENGINEERING_VERSION);
16093255736Sdavidch
16094255736Sdavidch    snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
16095255736Sdavidch        ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
16096255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
16097255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
16098255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
16099255736Sdavidch                                                                "Unknown"));
16100255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
16101255736Sdavidch                    CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
16102255736Sdavidch                    "multifunction vnics per port");
16103255736Sdavidch
16104255736Sdavidch    snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
16105255736Sdavidch        ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
16106255736Sdavidch         (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
16107255736Sdavidch         (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
16108255736Sdavidch                                              "???GT/s"),
16109255736Sdavidch        sc->devinfo.pcie_link_width);
16110297155Sdavidcs
16111297155Sdavidcs    sc->debug = bxe_debug;
16112297155Sdavidcs
16113297155Sdavidcs#if __FreeBSD_version >= 900000
16114297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
16115297155Sdavidcs                      CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
16116297155Sdavidcs                      "bootcode version");
16117297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
16118297155Sdavidcs                      CTLFLAG_RD, sc->fw_ver_str, 0,
16119297155Sdavidcs                      "firmware version");
16120297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
16121297155Sdavidcs                      CTLFLAG_RD, sc->mf_mode_str, 0,
16122297155Sdavidcs                      "multifunction mode");
16123297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
16124297155Sdavidcs                      CTLFLAG_RD, sc->mac_addr_str, 0,
16125297155Sdavidcs                      "mac address");
16126255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
16127273377Shselasky                      CTLFLAG_RD, sc->pci_link_str, 0,
16128255736Sdavidch                      "pci link status");
16129273377Shselasky    SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
16130273377Shselasky                    CTLFLAG_RW, &sc->debug,
16131255736Sdavidch                    "debug logging mode");
16132297155Sdavidcs#else
16133297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
16134297155Sdavidcs                      CTLFLAG_RD, &sc->devinfo.bc_ver_str, 0,
16135297155Sdavidcs                      "bootcode version");
16136297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
16137297155Sdavidcs                      CTLFLAG_RD, &sc->fw_ver_str, 0,
16138297155Sdavidcs                      "firmware version");
16139297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
16140297155Sdavidcs                      CTLFLAG_RD, &sc->mf_mode_str, 0,
16141297155Sdavidcs                      "multifunction mode");
16142297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
16143297155Sdavidcs                      CTLFLAG_RD, &sc->mac_addr_str, 0,
16144297155Sdavidcs                      "mac address");
16145297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
16146297155Sdavidcs                      CTLFLAG_RD, &sc->pci_link_str, 0,
16147297155Sdavidcs                      "pci link status");
16148297155Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "debug",
16149297155Sdavidcs                    CTLFLAG_RW, &sc->debug, 0,
16150297155Sdavidcs                    "debug logging mode");
16151297155Sdavidcs#endif /* #if __FreeBSD_version >= 900000 */
16152255736Sdavidch
16153298294Sdavidcs    sc->trigger_grcdump = 0;
16154298294Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "trigger_grcdump",
16155298294Sdavidcs                   CTLFLAG_RW, &sc->trigger_grcdump, 0,
16156298294Sdavidcs                   "trigger grcdump should be invoked"
16157298294Sdavidcs                   "  before collecting grcdump");
16158292639Sdavidcs
16159298294Sdavidcs    sc->grcdump_started = 0;
16160295823Sdavidcs    sc->grcdump_done = 0;
16161295823Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
16162298294Sdavidcs                   CTLFLAG_RD, &sc->grcdump_done, 0,
16163295823Sdavidcs                   "set by driver when grcdump is done");
16164292639Sdavidcs
16165255736Sdavidch    sc->rx_budget = bxe_rx_budget;
16166255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
16167255736Sdavidch                    CTLFLAG_RW, &sc->rx_budget, 0,
16168255736Sdavidch                    "rx processing budget");
16169255736Sdavidch
16170315881Sdavidcs   SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_param",
16171315881Sdavidcs                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
16172315881Sdavidcs                    bxe_sysctl_pauseparam, "IU",
16173315881Sdavidcs                    "need pause frames- DEF:0/TX:1/RX:2/BOTH:3/AUTO:4/AUTOTX:5/AUTORX:6/AUTORXTX:7/NONE:8");
16174315881Sdavidcs
16175315881Sdavidcs
16176255736Sdavidch    SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
16177255736Sdavidch                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
16178255736Sdavidch                    bxe_sysctl_state, "IU", "dump driver state");
16179255736Sdavidch
16180255736Sdavidch    for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
16181255736Sdavidch        SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
16182255736Sdavidch                        bxe_eth_stats_arr[i].string,
16183255736Sdavidch                        CTLTYPE_U64 | CTLFLAG_RD, sc, i,
16184255736Sdavidch                        bxe_sysctl_eth_stat, "LU",
16185255736Sdavidch                        bxe_eth_stats_arr[i].string);
16186255736Sdavidch    }
16187255736Sdavidch
16188255736Sdavidch    /* add a new parent node for all queues "dev.bxe.#.queue" */
16189255736Sdavidch    queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
16190255736Sdavidch                                CTLFLAG_RD, NULL, "queue");
16191255736Sdavidch    queue_top_children = SYSCTL_CHILDREN(queue_top);
16192255736Sdavidch
16193255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
16194255736Sdavidch        /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
16195255736Sdavidch        snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
16196255736Sdavidch        queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
16197255736Sdavidch                                queue_num_buf, CTLFLAG_RD, NULL,
16198255736Sdavidch                                "single queue");
16199255736Sdavidch        queue_children = SYSCTL_CHILDREN(queue);
16200255736Sdavidch
16201255736Sdavidch        for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
16202255736Sdavidch            q_stat = ((i << 16) | j);
16203255736Sdavidch            SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
16204255736Sdavidch                            bxe_eth_q_stats_arr[j].string,
16205255736Sdavidch                            CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
16206255736Sdavidch                            bxe_sysctl_eth_q_stat, "LU",
16207255736Sdavidch                            bxe_eth_q_stats_arr[j].string);
16208255736Sdavidch        }
16209255736Sdavidch    }
16210255736Sdavidch}
16211255736Sdavidch
16212296579Sdavidcsstatic int
16213296579Sdavidcsbxe_alloc_buf_rings(struct bxe_softc *sc)
16214296579Sdavidcs{
16215307972Sdavidcs#if __FreeBSD_version >= 901504
16216296579Sdavidcs
16217296579Sdavidcs    int i;
16218296579Sdavidcs    struct bxe_fastpath *fp;
16219296579Sdavidcs
16220296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
16221296579Sdavidcs
16222296579Sdavidcs        fp = &sc->fp[i];
16223296579Sdavidcs
16224296579Sdavidcs        fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
16225296579Sdavidcs                                   M_NOWAIT, &fp->tx_mtx);
16226296579Sdavidcs        if (fp->tx_br == NULL)
16227296579Sdavidcs            return (-1);
16228296579Sdavidcs    }
16229296579Sdavidcs#endif
16230296579Sdavidcs    return (0);
16231296579Sdavidcs}
16232296579Sdavidcs
16233296579Sdavidcsstatic void
16234296579Sdavidcsbxe_free_buf_rings(struct bxe_softc *sc)
16235296579Sdavidcs{
16236307972Sdavidcs#if __FreeBSD_version >= 901504
16237296579Sdavidcs
16238296579Sdavidcs    int i;
16239296579Sdavidcs    struct bxe_fastpath *fp;
16240296579Sdavidcs
16241296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
16242296579Sdavidcs
16243296579Sdavidcs        fp = &sc->fp[i];
16244296579Sdavidcs
16245296579Sdavidcs        if (fp->tx_br) {
16246296579Sdavidcs            buf_ring_free(fp->tx_br, M_DEVBUF);
16247296579Sdavidcs            fp->tx_br = NULL;
16248296579Sdavidcs        }
16249296579Sdavidcs    }
16250296579Sdavidcs
16251296579Sdavidcs#endif
16252296579Sdavidcs}
16253296579Sdavidcs
16254296579Sdavidcsstatic void
16255296579Sdavidcsbxe_init_fp_mutexs(struct bxe_softc *sc)
16256296579Sdavidcs{
16257296579Sdavidcs    int i;
16258296579Sdavidcs    struct bxe_fastpath *fp;
16259296579Sdavidcs
16260296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
16261296579Sdavidcs
16262296579Sdavidcs        fp = &sc->fp[i];
16263296579Sdavidcs
16264296579Sdavidcs        snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
16265296579Sdavidcs            "bxe%d_fp%d_tx_lock", sc->unit, i);
16266296579Sdavidcs        mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
16267296579Sdavidcs
16268296579Sdavidcs        snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
16269296579Sdavidcs            "bxe%d_fp%d_rx_lock", sc->unit, i);
16270296579Sdavidcs        mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
16271296579Sdavidcs    }
16272296579Sdavidcs}
16273296579Sdavidcs
16274296579Sdavidcsstatic void
16275296579Sdavidcsbxe_destroy_fp_mutexs(struct bxe_softc *sc)
16276296579Sdavidcs{
16277296579Sdavidcs    int i;
16278296579Sdavidcs    struct bxe_fastpath *fp;
16279296579Sdavidcs
16280296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
16281296579Sdavidcs
16282296579Sdavidcs        fp = &sc->fp[i];
16283296579Sdavidcs
16284296579Sdavidcs        if (mtx_initialized(&fp->tx_mtx)) {
16285296579Sdavidcs            mtx_destroy(&fp->tx_mtx);
16286296579Sdavidcs        }
16287296579Sdavidcs
16288296579Sdavidcs        if (mtx_initialized(&fp->rx_mtx)) {
16289296579Sdavidcs            mtx_destroy(&fp->rx_mtx);
16290296579Sdavidcs        }
16291296579Sdavidcs    }
16292296579Sdavidcs}
16293296579Sdavidcs
16294296579Sdavidcs
16295255736Sdavidch/*
16296255736Sdavidch * Device attach function.
16297255736Sdavidch *
16298255736Sdavidch * Allocates device resources, performs secondary chip identification, and
16299255736Sdavidch * initializes driver instance variables. This function is called from driver
16300255736Sdavidch * load after a successful probe.
16301255736Sdavidch *
16302255736Sdavidch * Returns:
16303255736Sdavidch *   0 = Success, >0 = Failure
16304255736Sdavidch */
16305255736Sdavidchstatic int
16306255736Sdavidchbxe_attach(device_t dev)
16307255736Sdavidch{
16308255736Sdavidch    struct bxe_softc *sc;
16309255736Sdavidch
16310255736Sdavidch    sc = device_get_softc(dev);
16311255736Sdavidch
16312255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting attach...\n");
16313255736Sdavidch
16314255736Sdavidch    sc->state = BXE_STATE_CLOSED;
16315255736Sdavidch
16316255736Sdavidch    sc->dev  = dev;
16317255736Sdavidch    sc->unit = device_get_unit(dev);
16318255736Sdavidch
16319255736Sdavidch    BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
16320255736Sdavidch
16321255736Sdavidch    sc->pcie_bus    = pci_get_bus(dev);
16322255736Sdavidch    sc->pcie_device = pci_get_slot(dev);
16323255736Sdavidch    sc->pcie_func   = pci_get_function(dev);
16324255736Sdavidch
16325255736Sdavidch    /* enable bus master capability */
16326255736Sdavidch    pci_enable_busmaster(dev);
16327255736Sdavidch
16328255736Sdavidch    /* get the BARs */
16329255736Sdavidch    if (bxe_allocate_bars(sc) != 0) {
16330255736Sdavidch        return (ENXIO);
16331255736Sdavidch    }
16332255736Sdavidch
16333255736Sdavidch    /* initialize the mutexes */
16334255736Sdavidch    bxe_init_mutexes(sc);
16335255736Sdavidch
16336255736Sdavidch    /* prepare the periodic callout */
16337255736Sdavidch    callout_init(&sc->periodic_callout, 0);
16338255736Sdavidch
16339255736Sdavidch    /* prepare the chip taskqueue */
16340255736Sdavidch    sc->chip_tq_flags = CHIP_TQ_NONE;
16341255736Sdavidch    snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
16342255736Sdavidch             "bxe%d_chip_tq", sc->unit);
16343255736Sdavidch    TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
16344255736Sdavidch    sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
16345255736Sdavidch                                   taskqueue_thread_enqueue,
16346255736Sdavidch                                   &sc->chip_tq);
16347255736Sdavidch    taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
16348255736Sdavidch                            "%s", sc->chip_tq_name);
16349255736Sdavidch
16350339881Sdavidcs    TIMEOUT_TASK_INIT(taskqueue_thread,
16351339881Sdavidcs        &sc->sp_err_timeout_task, 0, bxe_sp_err_timeout_task,  sc);
16352339881Sdavidcs
16353339881Sdavidcs
16354255736Sdavidch    /* get device info and set params */
16355255736Sdavidch    if (bxe_get_device_info(sc) != 0) {
16356255736Sdavidch        BLOGE(sc, "getting device info\n");
16357255736Sdavidch        bxe_deallocate_bars(sc);
16358255736Sdavidch        pci_disable_busmaster(dev);
16359255736Sdavidch        return (ENXIO);
16360255736Sdavidch    }
16361255736Sdavidch
16362255736Sdavidch    /* get final misc params */
16363255736Sdavidch    bxe_get_params(sc);
16364255736Sdavidch
16365255736Sdavidch    /* set the default MTU (changed via ifconfig) */
16366255736Sdavidch    sc->mtu = ETHERMTU;
16367255736Sdavidch
16368255736Sdavidch    bxe_set_modes_bitmap(sc);
16369255736Sdavidch
16370255736Sdavidch    /* XXX
16371255736Sdavidch     * If in AFEX mode and the function is configured for FCoE
16372255736Sdavidch     * then bail... no L2 allowed.
16373255736Sdavidch     */
16374255736Sdavidch
16375255736Sdavidch    /* get phy settings from shmem and 'and' against admin settings */
16376255736Sdavidch    bxe_get_phy_info(sc);
16377255736Sdavidch
16378255736Sdavidch    /* initialize the FreeBSD ifnet interface */
16379255736Sdavidch    if (bxe_init_ifnet(sc) != 0) {
16380255736Sdavidch        bxe_release_mutexes(sc);
16381255736Sdavidch        bxe_deallocate_bars(sc);
16382255736Sdavidch        pci_disable_busmaster(dev);
16383255736Sdavidch        return (ENXIO);
16384255736Sdavidch    }
16385255736Sdavidch
16386292639Sdavidcs    if (bxe_add_cdev(sc) != 0) {
16387292639Sdavidcs        if (sc->ifp != NULL) {
16388292639Sdavidcs            ether_ifdetach(sc->ifp);
16389292639Sdavidcs        }
16390292639Sdavidcs        ifmedia_removeall(&sc->ifmedia);
16391292639Sdavidcs        bxe_release_mutexes(sc);
16392292639Sdavidcs        bxe_deallocate_bars(sc);
16393292639Sdavidcs        pci_disable_busmaster(dev);
16394292639Sdavidcs        return (ENXIO);
16395292639Sdavidcs    }
16396292639Sdavidcs
16397255736Sdavidch    /* allocate device interrupts */
16398255736Sdavidch    if (bxe_interrupt_alloc(sc) != 0) {
16399292639Sdavidcs        bxe_del_cdev(sc);
16400266979Smarcel        if (sc->ifp != NULL) {
16401270876Sglebius            ether_ifdetach(sc->ifp);
16402255736Sdavidch        }
16403255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16404255736Sdavidch        bxe_release_mutexes(sc);
16405255736Sdavidch        bxe_deallocate_bars(sc);
16406255736Sdavidch        pci_disable_busmaster(dev);
16407255736Sdavidch        return (ENXIO);
16408255736Sdavidch    }
16409255736Sdavidch
16410296579Sdavidcs    bxe_init_fp_mutexs(sc);
16411296579Sdavidcs
16412296579Sdavidcs    if (bxe_alloc_buf_rings(sc) != 0) {
16413296579Sdavidcs	bxe_free_buf_rings(sc);
16414296579Sdavidcs        bxe_interrupt_free(sc);
16415296579Sdavidcs        bxe_del_cdev(sc);
16416296579Sdavidcs        if (sc->ifp != NULL) {
16417296579Sdavidcs            ether_ifdetach(sc->ifp);
16418296579Sdavidcs        }
16419296579Sdavidcs        ifmedia_removeall(&sc->ifmedia);
16420296579Sdavidcs        bxe_release_mutexes(sc);
16421296579Sdavidcs        bxe_deallocate_bars(sc);
16422296579Sdavidcs        pci_disable_busmaster(dev);
16423296579Sdavidcs        return (ENXIO);
16424296579Sdavidcs    }
16425296579Sdavidcs
16426255736Sdavidch    /* allocate ilt */
16427255736Sdavidch    if (bxe_alloc_ilt_mem(sc) != 0) {
16428296579Sdavidcs	bxe_free_buf_rings(sc);
16429255736Sdavidch        bxe_interrupt_free(sc);
16430292639Sdavidcs        bxe_del_cdev(sc);
16431266979Smarcel        if (sc->ifp != NULL) {
16432270876Sglebius            ether_ifdetach(sc->ifp);
16433255736Sdavidch        }
16434255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16435255736Sdavidch        bxe_release_mutexes(sc);
16436255736Sdavidch        bxe_deallocate_bars(sc);
16437255736Sdavidch        pci_disable_busmaster(dev);
16438255736Sdavidch        return (ENXIO);
16439255736Sdavidch    }
16440255736Sdavidch
16441255736Sdavidch    /* allocate the host hardware/software hsi structures */
16442255736Sdavidch    if (bxe_alloc_hsi_mem(sc) != 0) {
16443255736Sdavidch        bxe_free_ilt_mem(sc);
16444296579Sdavidcs	bxe_free_buf_rings(sc);
16445255736Sdavidch        bxe_interrupt_free(sc);
16446292639Sdavidcs        bxe_del_cdev(sc);
16447266979Smarcel        if (sc->ifp != NULL) {
16448270876Sglebius            ether_ifdetach(sc->ifp);
16449255736Sdavidch        }
16450255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
16451255736Sdavidch        bxe_release_mutexes(sc);
16452255736Sdavidch        bxe_deallocate_bars(sc);
16453255736Sdavidch        pci_disable_busmaster(dev);
16454255736Sdavidch        return (ENXIO);
16455255736Sdavidch    }
16456255736Sdavidch
16457255736Sdavidch    /* need to reset chip if UNDI was active */
16458255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
16459255736Sdavidch        /* init fw_seq */
16460255736Sdavidch        sc->fw_seq =
16461255736Sdavidch            (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
16462255736Sdavidch             DRV_MSG_SEQ_NUMBER_MASK);
16463255736Sdavidch        BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
16464255736Sdavidch        bxe_prev_unload(sc);
16465255736Sdavidch    }
16466255736Sdavidch
16467255736Sdavidch#if 1
16468255736Sdavidch    /* XXX */
16469255736Sdavidch    bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16470255736Sdavidch#else
16471255736Sdavidch    if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
16472255736Sdavidch        SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
16473255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
16474255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
16475255736Sdavidch        bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
16476255736Sdavidch        bxe_dcbx_init_params(sc);
16477255736Sdavidch    } else {
16478255736Sdavidch        bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16479255736Sdavidch    }
16480255736Sdavidch#endif
16481255736Sdavidch
16482255736Sdavidch    /* calculate qm_cid_count */
16483255736Sdavidch    sc->qm_cid_count = bxe_set_qm_cid_count(sc);
16484255736Sdavidch    BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
16485255736Sdavidch
16486255736Sdavidch    sc->max_cos = 1;
16487255736Sdavidch    bxe_init_multi_cos(sc);
16488255736Sdavidch
16489255736Sdavidch    bxe_add_sysctls(sc);
16490255736Sdavidch
16491255736Sdavidch    return (0);
16492255736Sdavidch}
16493255736Sdavidch
16494255736Sdavidch/*
16495255736Sdavidch * Device detach function.
16496255736Sdavidch *
16497255736Sdavidch * Stops the controller, resets the controller, and releases resources.
16498255736Sdavidch *
16499255736Sdavidch * Returns:
16500255736Sdavidch *   0 = Success, >0 = Failure
16501255736Sdavidch */
16502255736Sdavidchstatic int
16503255736Sdavidchbxe_detach(device_t dev)
16504255736Sdavidch{
16505255736Sdavidch    struct bxe_softc *sc;
16506266979Smarcel    if_t ifp;
16507255736Sdavidch
16508255736Sdavidch    sc = device_get_softc(dev);
16509255736Sdavidch
16510255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16511255736Sdavidch
16512266979Smarcel    ifp = sc->ifp;
16513266979Smarcel    if (ifp != NULL && if_vlantrunkinuse(ifp)) {
16514255736Sdavidch        BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16515255736Sdavidch        return(EBUSY);
16516255736Sdavidch    }
16517255736Sdavidch
16518292639Sdavidcs    bxe_del_cdev(sc);
16519292639Sdavidcs
16520255736Sdavidch    /* stop the periodic callout */
16521255736Sdavidch    bxe_periodic_stop(sc);
16522255736Sdavidch
16523255736Sdavidch    /* stop the chip taskqueue */
16524255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16525255736Sdavidch    if (sc->chip_tq) {
16526255736Sdavidch        taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16527255736Sdavidch        taskqueue_free(sc->chip_tq);
16528255736Sdavidch        sc->chip_tq = NULL;
16529339881Sdavidcs        taskqueue_drain_timeout(taskqueue_thread,
16530339881Sdavidcs            &sc->sp_err_timeout_task);
16531255736Sdavidch    }
16532255736Sdavidch
16533255736Sdavidch    /* stop and reset the controller if it was open */
16534255736Sdavidch    if (sc->state != BXE_STATE_CLOSED) {
16535255736Sdavidch        BXE_CORE_LOCK(sc);
16536255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16537297873Sdavidcs        sc->state = BXE_STATE_DISABLED;
16538255736Sdavidch        BXE_CORE_UNLOCK(sc);
16539255736Sdavidch    }
16540255736Sdavidch
16541255736Sdavidch    /* release the network interface */
16542255736Sdavidch    if (ifp != NULL) {
16543270876Sglebius        ether_ifdetach(ifp);
16544255736Sdavidch    }
16545255736Sdavidch    ifmedia_removeall(&sc->ifmedia);
16546255736Sdavidch
16547255736Sdavidch    /* XXX do the following based on driver state... */
16548255736Sdavidch
16549255736Sdavidch    /* free the host hardware/software hsi structures */
16550255736Sdavidch    bxe_free_hsi_mem(sc);
16551255736Sdavidch
16552255736Sdavidch    /* free ilt */
16553255736Sdavidch    bxe_free_ilt_mem(sc);
16554255736Sdavidch
16555296579Sdavidcs    bxe_free_buf_rings(sc);
16556296579Sdavidcs
16557255736Sdavidch    /* release the interrupts */
16558255736Sdavidch    bxe_interrupt_free(sc);
16559255736Sdavidch
16560255736Sdavidch    /* Release the mutexes*/
16561296579Sdavidcs    bxe_destroy_fp_mutexs(sc);
16562255736Sdavidch    bxe_release_mutexes(sc);
16563255736Sdavidch
16564296579Sdavidcs
16565255736Sdavidch    /* Release the PCIe BAR mapped memory */
16566255736Sdavidch    bxe_deallocate_bars(sc);
16567255736Sdavidch
16568255736Sdavidch    /* Release the FreeBSD interface. */
16569266979Smarcel    if (sc->ifp != NULL) {
16570270876Sglebius        if_free(sc->ifp);
16571255736Sdavidch    }
16572255736Sdavidch
16573255736Sdavidch    pci_disable_busmaster(dev);
16574255736Sdavidch
16575255736Sdavidch    return (0);
16576255736Sdavidch}
16577255736Sdavidch
16578255736Sdavidch/*
16579255736Sdavidch * Device shutdown function.
16580255736Sdavidch *
16581255736Sdavidch * Stops and resets the controller.
16582255736Sdavidch *
16583255736Sdavidch * Returns:
16584255736Sdavidch *   Nothing
16585255736Sdavidch */
16586255736Sdavidchstatic int
16587255736Sdavidchbxe_shutdown(device_t dev)
16588255736Sdavidch{
16589255736Sdavidch    struct bxe_softc *sc;
16590255736Sdavidch
16591255736Sdavidch    sc = device_get_softc(dev);
16592255736Sdavidch
16593255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16594255736Sdavidch
16595255736Sdavidch    /* stop the periodic callout */
16596255736Sdavidch    bxe_periodic_stop(sc);
16597255736Sdavidch
16598339874Sdavidcs    if (sc->state != BXE_STATE_CLOSED) {
16599339874Sdavidcs    	BXE_CORE_LOCK(sc);
16600339874Sdavidcs    	bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16601339874Sdavidcs    	BXE_CORE_UNLOCK(sc);
16602339874Sdavidcs    }
16603255736Sdavidch
16604255736Sdavidch    return (0);
16605255736Sdavidch}
16606255736Sdavidch
16607255736Sdavidchvoid
16608255736Sdavidchbxe_igu_ack_sb(struct bxe_softc *sc,
16609255736Sdavidch               uint8_t          igu_sb_id,
16610255736Sdavidch               uint8_t          segment,
16611255736Sdavidch               uint16_t         index,
16612255736Sdavidch               uint8_t          op,
16613255736Sdavidch               uint8_t          update)
16614255736Sdavidch{
16615255736Sdavidch    uint32_t igu_addr = sc->igu_base_addr;
16616255736Sdavidch    igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16617255736Sdavidch    bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16618255736Sdavidch}
16619255736Sdavidch
16620255736Sdavidchstatic void
16621255736Sdavidchbxe_igu_clear_sb_gen(struct bxe_softc *sc,
16622255736Sdavidch                     uint8_t          func,
16623255736Sdavidch                     uint8_t          idu_sb_id,
16624255736Sdavidch                     uint8_t          is_pf)
16625255736Sdavidch{
16626255736Sdavidch    uint32_t data, ctl, cnt = 100;
16627255736Sdavidch    uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16628255736Sdavidch    uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16629255736Sdavidch    uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16630255736Sdavidch    uint32_t sb_bit =  1 << (idu_sb_id%32);
16631255736Sdavidch    uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16632255736Sdavidch    uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16633255736Sdavidch
16634255736Sdavidch    /* Not supported in BC mode */
16635255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
16636255736Sdavidch        return;
16637255736Sdavidch    }
16638255736Sdavidch
16639255736Sdavidch    data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16640255736Sdavidch             IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16641255736Sdavidch            IGU_REGULAR_CLEANUP_SET |
16642255736Sdavidch            IGU_REGULAR_BCLEANUP);
16643255736Sdavidch
16644255736Sdavidch    ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16645255736Sdavidch           (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16646255736Sdavidch           (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16647255736Sdavidch
16648255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16649255736Sdavidch            data, igu_addr_data);
16650255736Sdavidch    REG_WR(sc, igu_addr_data, data);
16651255736Sdavidch
16652255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16653255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16654255736Sdavidch    mb();
16655255736Sdavidch
16656255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16657255736Sdavidch            ctl, igu_addr_ctl);
16658255736Sdavidch    REG_WR(sc, igu_addr_ctl, ctl);
16659255736Sdavidch
16660255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16661255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16662255736Sdavidch    mb();
16663255736Sdavidch
16664255736Sdavidch    /* wait for clean up to finish */
16665255736Sdavidch    while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16666255736Sdavidch        DELAY(20000);
16667255736Sdavidch    }
16668255736Sdavidch
16669255736Sdavidch    if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16670255736Sdavidch        BLOGD(sc, DBG_LOAD,
16671255736Sdavidch              "Unable to finish IGU cleanup: "
16672255736Sdavidch              "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16673255736Sdavidch              idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16674255736Sdavidch    }
16675255736Sdavidch}
16676255736Sdavidch
16677255736Sdavidchstatic void
16678255736Sdavidchbxe_igu_clear_sb(struct bxe_softc *sc,
16679255736Sdavidch                 uint8_t          idu_sb_id)
16680255736Sdavidch{
16681255736Sdavidch    bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16682255736Sdavidch}
16683255736Sdavidch
16684255736Sdavidch
16685255736Sdavidch
16686255736Sdavidch
16687255736Sdavidch
16688255736Sdavidch
16689255736Sdavidch
16690255736Sdavidch/*******************/
16691255736Sdavidch/* ECORE CALLBACKS */
16692255736Sdavidch/*******************/
16693255736Sdavidch
16694255736Sdavidchstatic void
16695255736Sdavidchbxe_reset_common(struct bxe_softc *sc)
16696255736Sdavidch{
16697255736Sdavidch    uint32_t val = 0x1400;
16698255736Sdavidch
16699255736Sdavidch    /* reset_common */
16700255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16701255736Sdavidch
16702255736Sdavidch    if (CHIP_IS_E3(sc)) {
16703255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16704255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16705255736Sdavidch    }
16706255736Sdavidch
16707255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16708255736Sdavidch}
16709255736Sdavidch
16710255736Sdavidchstatic void
16711255736Sdavidchbxe_common_init_phy(struct bxe_softc *sc)
16712255736Sdavidch{
16713255736Sdavidch    uint32_t shmem_base[2];
16714255736Sdavidch    uint32_t shmem2_base[2];
16715255736Sdavidch
16716255736Sdavidch    /* Avoid common init in case MFW supports LFA */
16717255736Sdavidch    if (SHMEM2_RD(sc, size) >
16718255736Sdavidch        (uint32_t)offsetof(struct shmem2_region,
16719255736Sdavidch                           lfa_host_addr[SC_PORT(sc)])) {
16720255736Sdavidch        return;
16721255736Sdavidch    }
16722255736Sdavidch
16723255736Sdavidch    shmem_base[0]  = sc->devinfo.shmem_base;
16724255736Sdavidch    shmem2_base[0] = sc->devinfo.shmem2_base;
16725255736Sdavidch
16726255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16727255736Sdavidch        shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16728255736Sdavidch        shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16729255736Sdavidch    }
16730255736Sdavidch
16731284335Sdavidcs    bxe_acquire_phy_lock(sc);
16732255736Sdavidch    elink_common_init_phy(sc, shmem_base, shmem2_base,
16733255736Sdavidch                          sc->devinfo.chip_id, 0);
16734284335Sdavidcs    bxe_release_phy_lock(sc);
16735255736Sdavidch}
16736255736Sdavidch
16737255736Sdavidchstatic void
16738255736Sdavidchbxe_pf_disable(struct bxe_softc *sc)
16739255736Sdavidch{
16740255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16741255736Sdavidch
16742255736Sdavidch    val &= ~IGU_PF_CONF_FUNC_EN;
16743255736Sdavidch
16744255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16745255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16746255736Sdavidch    REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16747255736Sdavidch}
16748255736Sdavidch
16749255736Sdavidchstatic void
16750255736Sdavidchbxe_init_pxp(struct bxe_softc *sc)
16751255736Sdavidch{
16752255736Sdavidch    uint16_t devctl;
16753255736Sdavidch    int r_order, w_order;
16754255736Sdavidch
16755255736Sdavidch    devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16756255736Sdavidch
16757255736Sdavidch    BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16758255736Sdavidch
16759255736Sdavidch    w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16760255736Sdavidch
16761255736Sdavidch    if (sc->mrrs == -1) {
16762255736Sdavidch        r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16763255736Sdavidch    } else {
16764255736Sdavidch        BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16765255736Sdavidch        r_order = sc->mrrs;
16766255736Sdavidch    }
16767255736Sdavidch
16768255736Sdavidch    ecore_init_pxp_arb(sc, r_order, w_order);
16769255736Sdavidch}
16770255736Sdavidch
16771255736Sdavidchstatic uint32_t
16772255736Sdavidchbxe_get_pretend_reg(struct bxe_softc *sc)
16773255736Sdavidch{
16774255736Sdavidch    uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16775255736Sdavidch    uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16776255736Sdavidch    return (base + (SC_ABS_FUNC(sc)) * stride);
16777255736Sdavidch}
16778255736Sdavidch
16779255736Sdavidch/*
16780255736Sdavidch * Called only on E1H or E2.
16781255736Sdavidch * When pretending to be PF, the pretend value is the function number 0..7.
16782255736Sdavidch * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16783255736Sdavidch * combination.
16784255736Sdavidch */
16785255736Sdavidchstatic int
16786255736Sdavidchbxe_pretend_func(struct bxe_softc *sc,
16787255736Sdavidch                 uint16_t         pretend_func_val)
16788255736Sdavidch{
16789255736Sdavidch    uint32_t pretend_reg;
16790255736Sdavidch
16791255736Sdavidch    if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16792255736Sdavidch        return (-1);
16793255736Sdavidch    }
16794255736Sdavidch
16795255736Sdavidch    /* get my own pretend register */
16796255736Sdavidch    pretend_reg = bxe_get_pretend_reg(sc);
16797255736Sdavidch    REG_WR(sc, pretend_reg, pretend_func_val);
16798255736Sdavidch    REG_RD(sc, pretend_reg);
16799255736Sdavidch    return (0);
16800255736Sdavidch}
16801255736Sdavidch
16802255736Sdavidchstatic void
16803255736Sdavidchbxe_iov_init_dmae(struct bxe_softc *sc)
16804255736Sdavidch{
16805255736Sdavidch    return;
16806255736Sdavidch}
16807255736Sdavidch
16808255736Sdavidchstatic void
16809255736Sdavidchbxe_iov_init_dq(struct bxe_softc *sc)
16810255736Sdavidch{
16811255736Sdavidch    return;
16812255736Sdavidch}
16813255736Sdavidch
16814255736Sdavidch/* send a NIG loopback debug packet */
16815255736Sdavidchstatic void
16816255736Sdavidchbxe_lb_pckt(struct bxe_softc *sc)
16817255736Sdavidch{
16818255736Sdavidch    uint32_t wb_write[3];
16819255736Sdavidch
16820255736Sdavidch    /* Ethernet source and destination addresses */
16821255736Sdavidch    wb_write[0] = 0x55555555;
16822255736Sdavidch    wb_write[1] = 0x55555555;
16823255736Sdavidch    wb_write[2] = 0x20;     /* SOP */
16824255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16825255736Sdavidch
16826255736Sdavidch    /* NON-IP protocol */
16827255736Sdavidch    wb_write[0] = 0x09000000;
16828255736Sdavidch    wb_write[1] = 0x55555555;
16829255736Sdavidch    wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16830255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16831255736Sdavidch}
16832255736Sdavidch
16833255736Sdavidch/*
16834255736Sdavidch * Some of the internal memories are not directly readable from the driver.
16835255736Sdavidch * To test them we send debug packets.
16836255736Sdavidch */
16837255736Sdavidchstatic int
16838255736Sdavidchbxe_int_mem_test(struct bxe_softc *sc)
16839255736Sdavidch{
16840255736Sdavidch    int factor;
16841255736Sdavidch    int count, i;
16842255736Sdavidch    uint32_t val = 0;
16843255736Sdavidch
16844255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
16845255736Sdavidch        factor = 120;
16846255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
16847255736Sdavidch        factor = 200;
16848255736Sdavidch    } else {
16849255736Sdavidch        factor = 1;
16850255736Sdavidch    }
16851255736Sdavidch
16852255736Sdavidch    /* disable inputs of parser neighbor blocks */
16853255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16854255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16855255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16856255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16857255736Sdavidch
16858255736Sdavidch    /*  write 0 to parser credits for CFC search request */
16859255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16860255736Sdavidch
16861255736Sdavidch    /* send Ethernet packet */
16862255736Sdavidch    bxe_lb_pckt(sc);
16863255736Sdavidch
16864255736Sdavidch    /* TODO do i reset NIG statistic? */
16865255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16866255736Sdavidch    count = 1000 * factor;
16867255736Sdavidch    while (count) {
16868255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16869255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16870255736Sdavidch        if (val == 0x10) {
16871255736Sdavidch            break;
16872255736Sdavidch        }
16873255736Sdavidch
16874255736Sdavidch        DELAY(10000);
16875255736Sdavidch        count--;
16876255736Sdavidch    }
16877255736Sdavidch
16878255736Sdavidch    if (val != 0x10) {
16879255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16880255736Sdavidch        return (-1);
16881255736Sdavidch    }
16882255736Sdavidch
16883255736Sdavidch    /* wait until PRS register shows 1 packet */
16884255736Sdavidch    count = (1000 * factor);
16885255736Sdavidch    while (count) {
16886255736Sdavidch        val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16887255736Sdavidch        if (val == 1) {
16888255736Sdavidch            break;
16889255736Sdavidch        }
16890255736Sdavidch
16891255736Sdavidch        DELAY(10000);
16892255736Sdavidch        count--;
16893255736Sdavidch    }
16894255736Sdavidch
16895255736Sdavidch    if (val != 0x1) {
16896255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16897255736Sdavidch        return (-2);
16898255736Sdavidch    }
16899255736Sdavidch
16900255736Sdavidch    /* Reset and init BRB, PRS */
16901255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16902255736Sdavidch    DELAY(50000);
16903255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16904255736Sdavidch    DELAY(50000);
16905255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16906255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16907255736Sdavidch
16908255736Sdavidch    /* Disable inputs of parser neighbor blocks */
16909255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16910255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16911255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16912255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16913255736Sdavidch
16914255736Sdavidch    /* Write 0 to parser credits for CFC search request */
16915255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16916255736Sdavidch
16917255736Sdavidch    /* send 10 Ethernet packets */
16918255736Sdavidch    for (i = 0; i < 10; i++) {
16919255736Sdavidch        bxe_lb_pckt(sc);
16920255736Sdavidch    }
16921255736Sdavidch
16922255736Sdavidch    /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16923255736Sdavidch    count = (1000 * factor);
16924255736Sdavidch    while (count) {
16925255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16926255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16927255736Sdavidch        if (val == 0xb0) {
16928255736Sdavidch            break;
16929255736Sdavidch        }
16930255736Sdavidch
16931255736Sdavidch        DELAY(10000);
16932255736Sdavidch        count--;
16933255736Sdavidch    }
16934255736Sdavidch
16935255736Sdavidch    if (val != 0xb0) {
16936255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16937255736Sdavidch        return (-3);
16938255736Sdavidch    }
16939255736Sdavidch
16940255736Sdavidch    /* Wait until PRS register shows 2 packets */
16941255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16942255736Sdavidch    if (val != 2) {
16943255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16944255736Sdavidch    }
16945255736Sdavidch
16946255736Sdavidch    /* Write 1 to parser credits for CFC search request */
16947255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16948255736Sdavidch
16949255736Sdavidch    /* Wait until PRS register shows 3 packets */
16950255736Sdavidch    DELAY(10000 * factor);
16951255736Sdavidch
16952255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16953255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16954255736Sdavidch    if (val != 3) {
16955255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16956255736Sdavidch    }
16957255736Sdavidch
16958255736Sdavidch    /* clear NIG EOP FIFO */
16959255736Sdavidch    for (i = 0; i < 11; i++) {
16960255736Sdavidch        REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16961255736Sdavidch    }
16962255736Sdavidch
16963255736Sdavidch    val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16964255736Sdavidch    if (val != 1) {
16965295830Sdavidcs        BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16966255736Sdavidch        return (-4);
16967255736Sdavidch    }
16968255736Sdavidch
16969255736Sdavidch    /* Reset and init BRB, PRS, NIG */
16970255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16971255736Sdavidch    DELAY(50000);
16972255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16973255736Sdavidch    DELAY(50000);
16974255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16975255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16976255736Sdavidch    if (!CNIC_SUPPORT(sc)) {
16977255736Sdavidch        /* set NIC mode */
16978255736Sdavidch        REG_WR(sc, PRS_REG_NIC_MODE, 1);
16979255736Sdavidch    }
16980255736Sdavidch
16981255736Sdavidch    /* Enable inputs of parser neighbor blocks */
16982255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16983255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16984255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16985255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16986255736Sdavidch
16987255736Sdavidch    return (0);
16988255736Sdavidch}
16989255736Sdavidch
16990255736Sdavidchstatic void
16991255736Sdavidchbxe_setup_fan_failure_detection(struct bxe_softc *sc)
16992255736Sdavidch{
16993255736Sdavidch    int is_required;
16994255736Sdavidch    uint32_t val;
16995255736Sdavidch    int port;
16996255736Sdavidch
16997255736Sdavidch    is_required = 0;
16998255736Sdavidch    val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
16999255736Sdavidch           SHARED_HW_CFG_FAN_FAILURE_MASK);
17000255736Sdavidch
17001255736Sdavidch    if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
17002255736Sdavidch        is_required = 1;
17003255736Sdavidch    }
17004255736Sdavidch    /*
17005255736Sdavidch     * The fan failure mechanism is usually related to the PHY type since
17006255736Sdavidch     * the power consumption of the board is affected by the PHY. Currently,
17007255736Sdavidch     * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
17008255736Sdavidch     */
17009255736Sdavidch    else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
17010255736Sdavidch        for (port = PORT_0; port < PORT_MAX; port++) {
17011255736Sdavidch            is_required |= elink_fan_failure_det_req(sc,
17012255736Sdavidch                                                     sc->devinfo.shmem_base,
17013255736Sdavidch                                                     sc->devinfo.shmem2_base,
17014255736Sdavidch                                                     port);
17015255736Sdavidch        }
17016255736Sdavidch    }
17017255736Sdavidch
17018255736Sdavidch    BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
17019255736Sdavidch
17020255736Sdavidch    if (is_required == 0) {
17021255736Sdavidch        return;
17022255736Sdavidch    }
17023255736Sdavidch
17024255736Sdavidch    /* Fan failure is indicated by SPIO 5 */
17025255736Sdavidch    bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
17026255736Sdavidch
17027255736Sdavidch    /* set to active low mode */
17028255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_INT);
17029255736Sdavidch    val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
17030255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_INT, val);
17031255736Sdavidch
17032255736Sdavidch    /* enable interrupt to signal the IGU */
17033255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17034255736Sdavidch    val |= MISC_SPIO_SPIO5;
17035255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
17036255736Sdavidch}
17037255736Sdavidch
17038255736Sdavidchstatic void
17039255736Sdavidchbxe_enable_blocks_attention(struct bxe_softc *sc)
17040255736Sdavidch{
17041255736Sdavidch    uint32_t val;
17042255736Sdavidch
17043255736Sdavidch    REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17044255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17045255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
17046255736Sdavidch    } else {
17047255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
17048255736Sdavidch    }
17049255736Sdavidch    REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17050255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17051255736Sdavidch    /*
17052255736Sdavidch     * mask read length error interrupts in brb for parser
17053255736Sdavidch     * (parsing unit and 'checksum and crc' unit)
17054255736Sdavidch     * these errors are legal (PU reads fixed length and CAC can cause
17055255736Sdavidch     * read length error on truncated packets)
17056255736Sdavidch     */
17057255736Sdavidch    REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
17058255736Sdavidch    REG_WR(sc, QM_REG_QM_INT_MASK, 0);
17059255736Sdavidch    REG_WR(sc, TM_REG_TM_INT_MASK, 0);
17060255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
17061255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
17062255736Sdavidch    REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
17063255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
17064255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
17065255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
17066255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
17067255736Sdavidch    REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
17068255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
17069255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
17070255736Sdavidch    REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
17071255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
17072255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
17073255736Sdavidch    REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
17074255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
17075255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
17076255736Sdavidch
17077255736Sdavidch    val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
17078255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
17079255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
17080255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17081255736Sdavidch        val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
17082255736Sdavidch                PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
17083255736Sdavidch    }
17084255736Sdavidch    REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
17085255736Sdavidch
17086255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
17087255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
17088255736Sdavidch    REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
17089255736Sdavidch/*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
17090255736Sdavidch
17091255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17092255736Sdavidch        /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
17093255736Sdavidch        REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
17094255736Sdavidch    }
17095255736Sdavidch
17096255736Sdavidch    REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
17097255736Sdavidch    REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
17098255736Sdavidch/*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
17099255736Sdavidch    REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
17100255736Sdavidch}
17101255736Sdavidch
17102255736Sdavidch/**
17103255736Sdavidch * bxe_init_hw_common - initialize the HW at the COMMON phase.
17104255736Sdavidch *
17105255736Sdavidch * @sc:     driver handle
17106255736Sdavidch */
17107255736Sdavidchstatic int
17108255736Sdavidchbxe_init_hw_common(struct bxe_softc *sc)
17109255736Sdavidch{
17110255736Sdavidch    uint8_t abs_func_id;
17111255736Sdavidch    uint32_t val;
17112255736Sdavidch
17113255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
17114255736Sdavidch          SC_ABS_FUNC(sc));
17115255736Sdavidch
17116255736Sdavidch    /*
17117255736Sdavidch     * take the RESET lock to protect undi_unload flow from accessing
17118255736Sdavidch     * registers while we are resetting the chip
17119255736Sdavidch     */
17120255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17121255736Sdavidch
17122255736Sdavidch    bxe_reset_common(sc);
17123255736Sdavidch
17124255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
17125255736Sdavidch
17126255736Sdavidch    val = 0xfffc;
17127255736Sdavidch    if (CHIP_IS_E3(sc)) {
17128255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
17129255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
17130255736Sdavidch    }
17131255736Sdavidch
17132255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
17133255736Sdavidch
17134255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17135255736Sdavidch
17136255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
17137255736Sdavidch    BLOGD(sc, DBG_LOAD, "after misc block init\n");
17138255736Sdavidch
17139255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17140255736Sdavidch        /*
17141255736Sdavidch         * 4-port mode or 2-port mode we need to turn off master-enable for
17142255736Sdavidch         * everyone. After that we turn it back on for self. So, we disregard
17143255736Sdavidch         * multi-function, and always disable all functions on the given path,
17144255736Sdavidch         * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
17145255736Sdavidch         */
17146255736Sdavidch        for (abs_func_id = SC_PATH(sc);
17147255736Sdavidch             abs_func_id < (E2_FUNC_MAX * 2);
17148255736Sdavidch             abs_func_id += 2) {
17149255736Sdavidch            if (abs_func_id == SC_ABS_FUNC(sc)) {
17150255736Sdavidch                REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17151255736Sdavidch                continue;
17152255736Sdavidch            }
17153255736Sdavidch
17154255736Sdavidch            bxe_pretend_func(sc, abs_func_id);
17155255736Sdavidch
17156255736Sdavidch            /* clear pf enable */
17157255736Sdavidch            bxe_pf_disable(sc);
17158255736Sdavidch
17159255736Sdavidch            bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17160255736Sdavidch        }
17161255736Sdavidch    }
17162255736Sdavidch
17163255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pf disable\n");
17164255736Sdavidch
17165255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
17166255736Sdavidch
17167255736Sdavidch    if (CHIP_IS_E1(sc)) {
17168255736Sdavidch        /*
17169255736Sdavidch         * enable HW interrupt from PXP on USDM overflow
17170255736Sdavidch         * bit 16 on INT_MASK_0
17171255736Sdavidch         */
17172255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17173255736Sdavidch    }
17174255736Sdavidch
17175255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
17176255736Sdavidch    bxe_init_pxp(sc);
17177255736Sdavidch
17178255736Sdavidch#ifdef __BIG_ENDIAN
17179255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
17180255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
17181255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
17182255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
17183255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
17184255736Sdavidch    /* make sure this value is 0 */
17185255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
17186255736Sdavidch
17187255736Sdavidch    //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
17188255736Sdavidch    REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
17189255736Sdavidch    REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
17190255736Sdavidch    REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
17191255736Sdavidch    REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
17192255736Sdavidch#endif
17193255736Sdavidch
17194255736Sdavidch    ecore_ilt_init_page_size(sc, INITOP_SET);
17195255736Sdavidch
17196255736Sdavidch    if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
17197255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
17198255736Sdavidch    }
17199255736Sdavidch
17200255736Sdavidch    /* let the HW do it's magic... */
17201255736Sdavidch    DELAY(100000);
17202255736Sdavidch
17203255736Sdavidch    /* finish PXP init */
17204255736Sdavidch    val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
17205255736Sdavidch    if (val != 1) {
17206295830Sdavidcs        BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
17207295830Sdavidcs            val);
17208255736Sdavidch        return (-1);
17209255736Sdavidch    }
17210255736Sdavidch    val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
17211255736Sdavidch    if (val != 1) {
17212295830Sdavidcs        BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
17213255736Sdavidch        return (-1);
17214255736Sdavidch    }
17215255736Sdavidch
17216255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pxp init\n");
17217255736Sdavidch
17218255736Sdavidch    /*
17219255736Sdavidch     * Timer bug workaround for E2 only. We need to set the entire ILT to have
17220255736Sdavidch     * entries with value "0" and valid bit on. This needs to be done by the
17221255736Sdavidch     * first PF that is loaded in a path (i.e. common phase)
17222255736Sdavidch     */
17223255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17224255736Sdavidch/*
17225255736Sdavidch * In E2 there is a bug in the timers block that can cause function 6 / 7
17226255736Sdavidch * (i.e. vnic3) to start even if it is marked as "scan-off".
17227255736Sdavidch * This occurs when a different function (func2,3) is being marked
17228255736Sdavidch * as "scan-off". Real-life scenario for example: if a driver is being
17229255736Sdavidch * load-unloaded while func6,7 are down. This will cause the timer to access
17230255736Sdavidch * the ilt, translate to a logical address and send a request to read/write.
17231255736Sdavidch * Since the ilt for the function that is down is not valid, this will cause
17232255736Sdavidch * a translation error which is unrecoverable.
17233255736Sdavidch * The Workaround is intended to make sure that when this happens nothing
17234255736Sdavidch * fatal will occur. The workaround:
17235255736Sdavidch *  1.  First PF driver which loads on a path will:
17236255736Sdavidch *      a.  After taking the chip out of reset, by using pretend,
17237255736Sdavidch *          it will write "0" to the following registers of
17238255736Sdavidch *          the other vnics.
17239255736Sdavidch *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
17240255736Sdavidch *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
17241255736Sdavidch *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
17242255736Sdavidch *          And for itself it will write '1' to
17243255736Sdavidch *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
17244255736Sdavidch *          dmae-operations (writing to pram for example.)
17245255736Sdavidch *          note: can be done for only function 6,7 but cleaner this
17246255736Sdavidch *            way.
17247255736Sdavidch *      b.  Write zero+valid to the entire ILT.
17248255736Sdavidch *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
17249255736Sdavidch *          VNIC3 (of that port). The range allocated will be the
17250255736Sdavidch *          entire ILT. This is needed to prevent  ILT range error.
17251255736Sdavidch *  2.  Any PF driver load flow:
17252255736Sdavidch *      a.  ILT update with the physical addresses of the allocated
17253255736Sdavidch *          logical pages.
17254255736Sdavidch *      b.  Wait 20msec. - note that this timeout is needed to make
17255255736Sdavidch *          sure there are no requests in one of the PXP internal
17256255736Sdavidch *          queues with "old" ILT addresses.
17257255736Sdavidch *      c.  PF enable in the PGLC.
17258255736Sdavidch *      d.  Clear the was_error of the PF in the PGLC. (could have
17259255736Sdavidch *          occurred while driver was down)
17260255736Sdavidch *      e.  PF enable in the CFC (WEAK + STRONG)
17261255736Sdavidch *      f.  Timers scan enable
17262255736Sdavidch *  3.  PF driver unload flow:
17263255736Sdavidch *      a.  Clear the Timers scan_en.
17264255736Sdavidch *      b.  Polling for scan_on=0 for that PF.
17265255736Sdavidch *      c.  Clear the PF enable bit in the PXP.
17266255736Sdavidch *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
17267255736Sdavidch *      e.  Write zero+valid to all ILT entries (The valid bit must
17268255736Sdavidch *          stay set)
17269255736Sdavidch *      f.  If this is VNIC 3 of a port then also init
17270255736Sdavidch *          first_timers_ilt_entry to zero and last_timers_ilt_entry
17271255736Sdavidch *          to the last enrty in the ILT.
17272255736Sdavidch *
17273255736Sdavidch *      Notes:
17274255736Sdavidch *      Currently the PF error in the PGLC is non recoverable.
17275255736Sdavidch *      In the future the there will be a recovery routine for this error.
17276255736Sdavidch *      Currently attention is masked.
17277255736Sdavidch *      Having an MCP lock on the load/unload process does not guarantee that
17278255736Sdavidch *      there is no Timer disable during Func6/7 enable. This is because the
17279255736Sdavidch *      Timers scan is currently being cleared by the MCP on FLR.
17280255736Sdavidch *      Step 2.d can be done only for PF6/7 and the driver can also check if
17281255736Sdavidch *      there is error before clearing it. But the flow above is simpler and
17282255736Sdavidch *      more general.
17283255736Sdavidch *      All ILT entries are written by zero+valid and not just PF6/7
17284255736Sdavidch *      ILT entries since in the future the ILT entries allocation for
17285255736Sdavidch *      PF-s might be dynamic.
17286255736Sdavidch */
17287255736Sdavidch        struct ilt_client_info ilt_cli;
17288255736Sdavidch        struct ecore_ilt ilt;
17289255736Sdavidch
17290255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
17291255736Sdavidch        memset(&ilt, 0, sizeof(struct ecore_ilt));
17292255736Sdavidch
17293255736Sdavidch        /* initialize dummy TM client */
17294255736Sdavidch        ilt_cli.start      = 0;
17295255736Sdavidch        ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
17296255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
17297255736Sdavidch
17298255736Sdavidch        /*
17299255736Sdavidch         * Step 1: set zeroes to all ilt page entries with valid bit on
17300255736Sdavidch         * Step 2: set the timers first/last ilt entry to point
17301255736Sdavidch         * to the entire range to prevent ILT range error for 3rd/4th
17302255736Sdavidch         * vnic (this code assumes existence of the vnic)
17303255736Sdavidch         *
17304255736Sdavidch         * both steps performed by call to ecore_ilt_client_init_op()
17305255736Sdavidch         * with dummy TM client
17306255736Sdavidch         *
17307255736Sdavidch         * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
17308255736Sdavidch         * and his brother are split registers
17309255736Sdavidch         */
17310255736Sdavidch
17311255736Sdavidch        bxe_pretend_func(sc, (SC_PATH(sc) + 6));
17312255736Sdavidch        ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
17313255736Sdavidch        bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17314255736Sdavidch
17315255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
17316255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
17317255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
17318255736Sdavidch    }
17319255736Sdavidch
17320255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
17321255736Sdavidch    REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
17322255736Sdavidch
17323255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17324255736Sdavidch        int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
17325255736Sdavidch                     (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
17326255736Sdavidch
17327255736Sdavidch        ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
17328255736Sdavidch        ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
17329255736Sdavidch
17330255736Sdavidch        /* let the HW do it's magic... */
17331255736Sdavidch        do {
17332255736Sdavidch            DELAY(200000);
17333255736Sdavidch            val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
17334255736Sdavidch        } while (factor-- && (val != 1));
17335255736Sdavidch
17336255736Sdavidch        if (val != 1) {
17337295830Sdavidcs            BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
17338255736Sdavidch            return (-1);
17339255736Sdavidch        }
17340255736Sdavidch    }
17341255736Sdavidch
17342255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
17343255736Sdavidch
17344255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
17345255736Sdavidch
17346255736Sdavidch    bxe_iov_init_dmae(sc);
17347255736Sdavidch
17348255736Sdavidch    /* clean the DMAE memory */
17349255736Sdavidch    sc->dmae_ready = 1;
17350255736Sdavidch    ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
17351255736Sdavidch
17352255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
17353255736Sdavidch
17354255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
17355255736Sdavidch
17356255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
17357255736Sdavidch
17358255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
17359255736Sdavidch
17360255736Sdavidch    bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
17361255736Sdavidch    bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
17362255736Sdavidch    bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
17363255736Sdavidch    bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
17364255736Sdavidch
17365255736Sdavidch    ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
17366255736Sdavidch
17367255736Sdavidch    /* QM queues pointers table */
17368255736Sdavidch    ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
17369255736Sdavidch
17370255736Sdavidch    /* soft reset pulse */
17371255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 1);
17372255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 0);
17373255736Sdavidch
17374255736Sdavidch    if (CNIC_SUPPORT(sc))
17375255736Sdavidch        ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
17376255736Sdavidch
17377255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
17378255736Sdavidch    REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
17379255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17380255736Sdavidch        /* enable hw interrupt from doorbell Q */
17381255736Sdavidch        REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17382255736Sdavidch    }
17383255736Sdavidch
17384255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17385255736Sdavidch
17386255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17387255736Sdavidch    REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
17388255736Sdavidch
17389255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17390255736Sdavidch        REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
17391255736Sdavidch    }
17392255736Sdavidch
17393255736Sdavidch    if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
17394255736Sdavidch        if (IS_MF_AFEX(sc)) {
17395255736Sdavidch            /*
17396255736Sdavidch             * configure that AFEX and VLAN headers must be
17397255736Sdavidch             * received in AFEX mode
17398255736Sdavidch             */
17399255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
17400255736Sdavidch            REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
17401255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
17402255736Sdavidch            REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
17403255736Sdavidch            REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
17404255736Sdavidch        } else {
17405255736Sdavidch            /*
17406255736Sdavidch             * Bit-map indicating which L2 hdrs may appear
17407255736Sdavidch             * after the basic Ethernet header
17408255736Sdavidch             */
17409255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
17410255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17411255736Sdavidch        }
17412255736Sdavidch    }
17413255736Sdavidch
17414255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
17415255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
17416255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
17417255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
17418255736Sdavidch
17419255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17420255736Sdavidch        /* reset VFC memories */
17421255736Sdavidch        REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17422255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
17423255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
17424255736Sdavidch        REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17425255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
17426255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
17427255736Sdavidch
17428255736Sdavidch        DELAY(20000);
17429255736Sdavidch    }
17430255736Sdavidch
17431255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
17432255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
17433255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
17434255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
17435255736Sdavidch
17436255736Sdavidch    /* sync semi rtc */
17437255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
17438255736Sdavidch           0x80000000);
17439255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
17440255736Sdavidch           0x80000000);
17441255736Sdavidch
17442255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
17443255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
17444255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
17445255736Sdavidch
17446255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17447255736Sdavidch        if (IS_MF_AFEX(sc)) {
17448255736Sdavidch            /*
17449255736Sdavidch             * configure that AFEX and VLAN headers must be
17450255736Sdavidch             * sent in AFEX mode
17451255736Sdavidch             */
17452255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
17453255736Sdavidch            REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
17454255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
17455255736Sdavidch            REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
17456255736Sdavidch            REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
17457255736Sdavidch        } else {
17458255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
17459255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17460255736Sdavidch        }
17461255736Sdavidch    }
17462255736Sdavidch
17463255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 1);
17464255736Sdavidch
17465255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
17466255736Sdavidch
17467255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17468255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
17469255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
17470255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
17471255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
17472255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
17473255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
17474255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
17475255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
17476255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
17477255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
17478255736Sdavidch    }
17479255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 0);
17480255736Sdavidch
17481255736Sdavidch    if (sizeof(union cdu_context) != 1024) {
17482255736Sdavidch        /* we currently assume that a context is 1024 bytes */
17483255736Sdavidch        BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
17484255736Sdavidch              (long)sizeof(union cdu_context));
17485255736Sdavidch    }
17486255736Sdavidch
17487255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
17488255736Sdavidch    val = (4 << 24) + (0 << 12) + 1024;
17489255736Sdavidch    REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
17490255736Sdavidch
17491255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
17492255736Sdavidch
17493255736Sdavidch    REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
17494255736Sdavidch    /* enable context validation interrupt from CFC */
17495255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17496255736Sdavidch
17497255736Sdavidch    /* set the thresholds to prevent CFC/CDU race */
17498255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
17499255736Sdavidch    ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
17500255736Sdavidch
17501255736Sdavidch    if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
17502255736Sdavidch        REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
17503255736Sdavidch    }
17504255736Sdavidch
17505255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
17506255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
17507255736Sdavidch
17508255736Sdavidch    /* Reset PCIE errors for debug */
17509255736Sdavidch    REG_WR(sc, 0x2814, 0xffffffff);
17510255736Sdavidch    REG_WR(sc, 0x3820, 0xffffffff);
17511255736Sdavidch
17512255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17513255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
17514255736Sdavidch               (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17515255736Sdavidch                PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17516255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17517255736Sdavidch               (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17518255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17519255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17520255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17521255736Sdavidch               (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17522255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17523255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17524255736Sdavidch    }
17525255736Sdavidch
17526255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17527255736Sdavidch
17528255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17529255736Sdavidch        /* in E3 this done in per-port section */
17530255736Sdavidch        if (!CHIP_IS_E3(sc))
17531255736Sdavidch            REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17532255736Sdavidch    }
17533255736Sdavidch
17534255736Sdavidch    if (CHIP_IS_E1H(sc)) {
17535255736Sdavidch        /* not applicable for E2 (and above ...) */
17536255736Sdavidch        REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17537255736Sdavidch    }
17538255736Sdavidch
17539255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
17540255736Sdavidch        DELAY(200000);
17541255736Sdavidch    }
17542255736Sdavidch
17543255736Sdavidch    /* finish CFC init */
17544255736Sdavidch    val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17545255736Sdavidch    if (val != 1) {
17546295830Sdavidcs        BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17547255736Sdavidch        return (-1);
17548255736Sdavidch    }
17549255736Sdavidch    val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17550255736Sdavidch    if (val != 1) {
17551295830Sdavidcs        BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17552255736Sdavidch        return (-1);
17553255736Sdavidch    }
17554255736Sdavidch    val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17555255736Sdavidch    if (val != 1) {
17556295830Sdavidcs        BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17557255736Sdavidch        return (-1);
17558255736Sdavidch    }
17559255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0);
17560255736Sdavidch
17561255736Sdavidch    if (CHIP_IS_E1(sc)) {
17562255736Sdavidch        /* read NIG statistic to see if this is our first up since powerup */
17563255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17564255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
17565255736Sdavidch
17566255736Sdavidch        /* do internal memory self test */
17567255736Sdavidch        if ((val == 0) && bxe_int_mem_test(sc)) {
17568295830Sdavidcs            BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17569255736Sdavidch            return (-1);
17570255736Sdavidch        }
17571255736Sdavidch    }
17572255736Sdavidch
17573255736Sdavidch    bxe_setup_fan_failure_detection(sc);
17574255736Sdavidch
17575255736Sdavidch    /* clear PXP2 attentions */
17576255736Sdavidch    REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17577255736Sdavidch
17578255736Sdavidch    bxe_enable_blocks_attention(sc);
17579255736Sdavidch
17580255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17581255736Sdavidch        ecore_enable_blocks_parity(sc);
17582255736Sdavidch    }
17583255736Sdavidch
17584255736Sdavidch    if (!BXE_NOMCP(sc)) {
17585255736Sdavidch        if (CHIP_IS_E1x(sc)) {
17586255736Sdavidch            bxe_common_init_phy(sc);
17587255736Sdavidch        }
17588255736Sdavidch    }
17589255736Sdavidch
17590255736Sdavidch    return (0);
17591255736Sdavidch}
17592255736Sdavidch
17593255736Sdavidch/**
17594255736Sdavidch * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17595255736Sdavidch *
17596255736Sdavidch * @sc:     driver handle
17597255736Sdavidch */
17598255736Sdavidchstatic int
17599255736Sdavidchbxe_init_hw_common_chip(struct bxe_softc *sc)
17600255736Sdavidch{
17601255736Sdavidch    int rc = bxe_init_hw_common(sc);
17602255736Sdavidch
17603255736Sdavidch    if (rc) {
17604295830Sdavidcs        BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17605255736Sdavidch        return (rc);
17606255736Sdavidch    }
17607255736Sdavidch
17608255736Sdavidch    /* In E2 2-PORT mode, same ext phy is used for the two paths */
17609255736Sdavidch    if (!BXE_NOMCP(sc)) {
17610255736Sdavidch        bxe_common_init_phy(sc);
17611255736Sdavidch    }
17612255736Sdavidch
17613255736Sdavidch    return (0);
17614255736Sdavidch}
17615255736Sdavidch
17616255736Sdavidchstatic int
17617255736Sdavidchbxe_init_hw_port(struct bxe_softc *sc)
17618255736Sdavidch{
17619255736Sdavidch    int port = SC_PORT(sc);
17620255736Sdavidch    int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17621255736Sdavidch    uint32_t low, high;
17622255736Sdavidch    uint32_t val;
17623255736Sdavidch
17624255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17625255736Sdavidch
17626255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17627255736Sdavidch
17628255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17629255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17630255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17631255736Sdavidch
17632255736Sdavidch    /*
17633255736Sdavidch     * Timers bug workaround: disables the pf_master bit in pglue at
17634255736Sdavidch     * common phase, we need to enable it here before any dmae access are
17635255736Sdavidch     * attempted. Therefore we manually added the enable-master to the
17636255736Sdavidch     * port phase (it also happens in the function phase)
17637255736Sdavidch     */
17638255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17639255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17640255736Sdavidch    }
17641255736Sdavidch
17642255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17643255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17644255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17645255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17646255736Sdavidch
17647255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17648255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17649255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17650255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17651255736Sdavidch
17652255736Sdavidch    /* QM cid (connection) count */
17653255736Sdavidch    ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17654255736Sdavidch
17655255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17656255736Sdavidch        ecore_init_block(sc, BLOCK_TM, init_phase);
17657255736Sdavidch        REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17658255736Sdavidch        REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17659255736Sdavidch    }
17660255736Sdavidch
17661255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17662255736Sdavidch
17663255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17664255736Sdavidch
17665255736Sdavidch    if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17666255736Sdavidch        if (IS_MF(sc)) {
17667255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 160 : 246);
17668255736Sdavidch        } else if (sc->mtu > 4096) {
17669255736Sdavidch            if (BXE_ONE_PORT(sc)) {
17670255736Sdavidch                low = 160;
17671255736Sdavidch            } else {
17672255736Sdavidch                val = sc->mtu;
17673255736Sdavidch                /* (24*1024 + val*4)/256 */
17674255736Sdavidch                low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17675255736Sdavidch            }
17676255736Sdavidch        } else {
17677255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 80 : 160);
17678255736Sdavidch        }
17679255736Sdavidch        high = (low + 56); /* 14*1024/256 */
17680255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17681255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17682255736Sdavidch    }
17683255736Sdavidch
17684255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
17685255736Sdavidch        REG_WR(sc, SC_PORT(sc) ?
17686255736Sdavidch               BRB1_REG_MAC_GUARANTIED_1 :
17687255736Sdavidch               BRB1_REG_MAC_GUARANTIED_0, 40);
17688255736Sdavidch    }
17689255736Sdavidch
17690255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17691255736Sdavidch    if (CHIP_IS_E3B0(sc)) {
17692255736Sdavidch        if (IS_MF_AFEX(sc)) {
17693255736Sdavidch            /* configure headers for AFEX mode */
17694255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17695255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17696255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17697255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17698255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17699255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17700255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17701255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17702255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17703255736Sdavidch        } else {
17704255736Sdavidch            /* Ovlan exists only if we are in multi-function +
17705255736Sdavidch             * switch-dependent mode, in switch-independent there
17706255736Sdavidch             * is no ovlan headers
17707255736Sdavidch             */
17708255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17709255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17710255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17711255736Sdavidch                   (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17712255736Sdavidch        }
17713255736Sdavidch    }
17714255736Sdavidch
17715255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17716255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17717255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17718255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17719255736Sdavidch
17720255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17721255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17722255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17723255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17724255736Sdavidch
17725255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17726255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17727255736Sdavidch
17728255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17729255736Sdavidch
17730255736Sdavidch    if (CHIP_IS_E1x(sc)) {
17731255736Sdavidch        /* configure PBF to work without PAUSE mtu 9000 */
17732255736Sdavidch        REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17733255736Sdavidch
17734255736Sdavidch        /* update threshold */
17735255736Sdavidch        REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17736255736Sdavidch        /* update init credit */
17737255736Sdavidch        REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17738255736Sdavidch
17739255736Sdavidch        /* probe changes */
17740255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17741255736Sdavidch        DELAY(50);
17742255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17743255736Sdavidch    }
17744255736Sdavidch
17745255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17746255736Sdavidch        ecore_init_block(sc, BLOCK_SRC, init_phase);
17747255736Sdavidch    }
17748255736Sdavidch
17749255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17750255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17751255736Sdavidch
17752255736Sdavidch    if (CHIP_IS_E1(sc)) {
17753255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17754255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17755255736Sdavidch    }
17756255736Sdavidch    ecore_init_block(sc, BLOCK_HC, init_phase);
17757255736Sdavidch
17758255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, init_phase);
17759255736Sdavidch
17760255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17761255736Sdavidch    /* init aeu_mask_attn_func_0/1:
17762255736Sdavidch     *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17763255736Sdavidch     *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17764255736Sdavidch     *             bits 4-7 are used for "per vn group attention" */
17765255736Sdavidch    val = IS_MF(sc) ? 0xF7 : 0x7;
17766255736Sdavidch    /* Enable DCBX attention for all but E1 */
17767255736Sdavidch    val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17768255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17769255736Sdavidch
17770255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17771255736Sdavidch
17772255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17773255736Sdavidch        /* Bit-map indicating which L2 hdrs may appear after the
17774255736Sdavidch         * basic Ethernet header
17775255736Sdavidch         */
17776255736Sdavidch        if (IS_MF_AFEX(sc)) {
17777255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17778255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17779255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17780255736Sdavidch        } else {
17781255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17782255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17783255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC,
17784255736Sdavidch                   IS_MF_SD(sc) ? 7 : 6);
17785255736Sdavidch        }
17786255736Sdavidch
17787255736Sdavidch        if (CHIP_IS_E3(sc)) {
17788255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17789255736Sdavidch                   NIG_REG_LLH1_MF_MODE :
17790255736Sdavidch                   NIG_REG_LLH_MF_MODE, IS_MF(sc));
17791255736Sdavidch        }
17792255736Sdavidch    }
17793255736Sdavidch    if (!CHIP_IS_E3(sc)) {
17794255736Sdavidch        REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17795255736Sdavidch    }
17796255736Sdavidch
17797255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17798255736Sdavidch        /* 0x2 disable mf_ov, 0x1 enable */
17799255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17800255736Sdavidch               (IS_MF_SD(sc) ? 0x1 : 0x2));
17801255736Sdavidch
17802255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17803255736Sdavidch            val = 0;
17804255736Sdavidch            switch (sc->devinfo.mf_info.mf_mode) {
17805255736Sdavidch            case MULTI_FUNCTION_SD:
17806255736Sdavidch                val = 1;
17807255736Sdavidch                break;
17808255736Sdavidch            case MULTI_FUNCTION_SI:
17809255736Sdavidch            case MULTI_FUNCTION_AFEX:
17810255736Sdavidch                val = 2;
17811255736Sdavidch                break;
17812255736Sdavidch            }
17813255736Sdavidch
17814255736Sdavidch            REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17815255736Sdavidch                        NIG_REG_LLH0_CLS_TYPE), val);
17816255736Sdavidch        }
17817255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17818255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17819255736Sdavidch        REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17820255736Sdavidch    }
17821255736Sdavidch
17822255736Sdavidch    /* If SPIO5 is set to generate interrupts, enable it for this port */
17823255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17824255736Sdavidch    if (val & MISC_SPIO_SPIO5) {
17825255736Sdavidch        uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17826255736Sdavidch                                    MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17827255736Sdavidch        val = REG_RD(sc, reg_addr);
17828255736Sdavidch        val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17829255736Sdavidch        REG_WR(sc, reg_addr, val);
17830255736Sdavidch    }
17831255736Sdavidch
17832255736Sdavidch    return (0);
17833255736Sdavidch}
17834255736Sdavidch
17835255736Sdavidchstatic uint32_t
17836255736Sdavidchbxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17837255736Sdavidch                       uint32_t         reg,
17838255736Sdavidch                       uint32_t         expected,
17839255736Sdavidch                       uint32_t         poll_count)
17840255736Sdavidch{
17841255736Sdavidch    uint32_t cur_cnt = poll_count;
17842255736Sdavidch    uint32_t val;
17843255736Sdavidch
17844255736Sdavidch    while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17845255736Sdavidch        DELAY(FLR_WAIT_INTERVAL);
17846255736Sdavidch    }
17847255736Sdavidch
17848255736Sdavidch    return (val);
17849255736Sdavidch}
17850255736Sdavidch
17851255736Sdavidchstatic int
17852255736Sdavidchbxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17853255736Sdavidch                              uint32_t         reg,
17854255736Sdavidch                              char             *msg,
17855255736Sdavidch                              uint32_t         poll_cnt)
17856255736Sdavidch{
17857255736Sdavidch    uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17858255736Sdavidch
17859255736Sdavidch    if (val != 0) {
17860255736Sdavidch        BLOGE(sc, "%s usage count=%d\n", msg, val);
17861255736Sdavidch        return (1);
17862255736Sdavidch    }
17863255736Sdavidch
17864255736Sdavidch    return (0);
17865255736Sdavidch}
17866255736Sdavidch
17867255736Sdavidch/* Common routines with VF FLR cleanup */
17868255736Sdavidchstatic uint32_t
17869255736Sdavidchbxe_flr_clnup_poll_count(struct bxe_softc *sc)
17870255736Sdavidch{
17871255736Sdavidch    /* adjust polling timeout */
17872255736Sdavidch    if (CHIP_REV_IS_EMUL(sc)) {
17873255736Sdavidch        return (FLR_POLL_CNT * 2000);
17874255736Sdavidch    }
17875255736Sdavidch
17876255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
17877255736Sdavidch        return (FLR_POLL_CNT * 120);
17878255736Sdavidch    }
17879255736Sdavidch
17880255736Sdavidch    return (FLR_POLL_CNT);
17881255736Sdavidch}
17882255736Sdavidch
17883255736Sdavidchstatic int
17884255736Sdavidchbxe_poll_hw_usage_counters(struct bxe_softc *sc,
17885255736Sdavidch                           uint32_t         poll_cnt)
17886255736Sdavidch{
17887255736Sdavidch    /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17888255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17889255736Sdavidch                                      CFC_REG_NUM_LCIDS_INSIDE_PF,
17890255736Sdavidch                                      "CFC PF usage counter timed out",
17891255736Sdavidch                                      poll_cnt)) {
17892255736Sdavidch        return (1);
17893255736Sdavidch    }
17894255736Sdavidch
17895255736Sdavidch    /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17896255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17897255736Sdavidch                                      DORQ_REG_PF_USAGE_CNT,
17898255736Sdavidch                                      "DQ PF usage counter timed out",
17899255736Sdavidch                                      poll_cnt)) {
17900255736Sdavidch        return (1);
17901255736Sdavidch    }
17902255736Sdavidch
17903255736Sdavidch    /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17904255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17905255736Sdavidch                                      QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17906255736Sdavidch                                      "QM PF usage counter timed out",
17907255736Sdavidch                                      poll_cnt)) {
17908255736Sdavidch        return (1);
17909255736Sdavidch    }
17910255736Sdavidch
17911255736Sdavidch    /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17912255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17913255736Sdavidch                                      TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17914255736Sdavidch                                      "Timers VNIC usage counter timed out",
17915255736Sdavidch                                      poll_cnt)) {
17916255736Sdavidch        return (1);
17917255736Sdavidch    }
17918255736Sdavidch
17919255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17920255736Sdavidch                                      TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17921255736Sdavidch                                      "Timers NUM_SCANS usage counter timed out",
17922255736Sdavidch                                      poll_cnt)) {
17923255736Sdavidch        return (1);
17924255736Sdavidch    }
17925255736Sdavidch
17926255736Sdavidch    /* Wait DMAE PF usage counter to zero */
17927255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17928255736Sdavidch                                      dmae_reg_go_c[INIT_DMAE_C(sc)],
17929255736Sdavidch                                      "DMAE dommand register timed out",
17930255736Sdavidch                                      poll_cnt)) {
17931255736Sdavidch        return (1);
17932255736Sdavidch    }
17933255736Sdavidch
17934255736Sdavidch    return (0);
17935255736Sdavidch}
17936255736Sdavidch
17937255736Sdavidch#define OP_GEN_PARAM(param)                                            \
17938255736Sdavidch    (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17939255736Sdavidch#define OP_GEN_TYPE(type)                                           \
17940255736Sdavidch    (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17941255736Sdavidch#define OP_GEN_AGG_VECT(index)                                             \
17942255736Sdavidch    (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17943255736Sdavidch
17944255736Sdavidchstatic int
17945255736Sdavidchbxe_send_final_clnup(struct bxe_softc *sc,
17946255736Sdavidch                     uint8_t          clnup_func,
17947255736Sdavidch                     uint32_t         poll_cnt)
17948255736Sdavidch{
17949255736Sdavidch    uint32_t op_gen_command = 0;
17950255736Sdavidch    uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17951255736Sdavidch                          CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17952255736Sdavidch    int ret = 0;
17953255736Sdavidch
17954255736Sdavidch    if (REG_RD(sc, comp_addr)) {
17955255736Sdavidch        BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17956255736Sdavidch        return (1);
17957255736Sdavidch    }
17958255736Sdavidch
17959255736Sdavidch    op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17960255736Sdavidch    op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17961255736Sdavidch    op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17962255736Sdavidch    op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17963255736Sdavidch
17964255736Sdavidch    BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17965255736Sdavidch    REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17966255736Sdavidch
17967255736Sdavidch    if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17968255736Sdavidch        BLOGE(sc, "FW final cleanup did not succeed\n");
17969255736Sdavidch        BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17970255736Sdavidch              (REG_RD(sc, comp_addr)));
17971255736Sdavidch        bxe_panic(sc, ("FLR cleanup failed\n"));
17972255736Sdavidch        return (1);
17973255736Sdavidch    }
17974255736Sdavidch
17975255736Sdavidch    /* Zero completion for nxt FLR */
17976255736Sdavidch    REG_WR(sc, comp_addr, 0);
17977255736Sdavidch
17978255736Sdavidch    return (ret);
17979255736Sdavidch}
17980255736Sdavidch
17981255736Sdavidchstatic void
17982255736Sdavidchbxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17983255736Sdavidch                       struct pbf_pN_buf_regs *regs,
17984255736Sdavidch                       uint32_t               poll_count)
17985255736Sdavidch{
17986255736Sdavidch    uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17987255736Sdavidch    uint32_t cur_cnt = poll_count;
17988255736Sdavidch
17989255736Sdavidch    crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
17990255736Sdavidch    crd = crd_start = REG_RD(sc, regs->crd);
17991255736Sdavidch    init_crd = REG_RD(sc, regs->init_crd);
17992255736Sdavidch
17993255736Sdavidch    BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
17994255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
17995255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
17996255736Sdavidch
17997255736Sdavidch    while ((crd != init_crd) &&
17998255736Sdavidch           ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
17999255736Sdavidch            (init_crd - crd_start))) {
18000255736Sdavidch        if (cur_cnt--) {
18001255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
18002255736Sdavidch            crd = REG_RD(sc, regs->crd);
18003255736Sdavidch            crd_freed = REG_RD(sc, regs->crd_freed);
18004255736Sdavidch        } else {
18005255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
18006255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
18007255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
18008255736Sdavidch            break;
18009255736Sdavidch        }
18010255736Sdavidch    }
18011255736Sdavidch
18012255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
18013255736Sdavidch          poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18014255736Sdavidch}
18015255736Sdavidch
18016255736Sdavidchstatic void
18017255736Sdavidchbxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
18018255736Sdavidch                       struct pbf_pN_cmd_regs *regs,
18019255736Sdavidch                       uint32_t               poll_count)
18020255736Sdavidch{
18021255736Sdavidch    uint32_t occup, to_free, freed, freed_start;
18022255736Sdavidch    uint32_t cur_cnt = poll_count;
18023255736Sdavidch
18024255736Sdavidch    occup = to_free = REG_RD(sc, regs->lines_occup);
18025255736Sdavidch    freed = freed_start = REG_RD(sc, regs->lines_freed);
18026255736Sdavidch
18027255736Sdavidch    BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18028255736Sdavidch    BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18029255736Sdavidch
18030255736Sdavidch    while (occup &&
18031255736Sdavidch           ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
18032255736Sdavidch        if (cur_cnt--) {
18033255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
18034255736Sdavidch            occup = REG_RD(sc, regs->lines_occup);
18035255736Sdavidch            freed = REG_RD(sc, regs->lines_freed);
18036255736Sdavidch        } else {
18037255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
18038255736Sdavidch            BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18039255736Sdavidch            BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18040255736Sdavidch            break;
18041255736Sdavidch        }
18042255736Sdavidch    }
18043255736Sdavidch
18044255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
18045255736Sdavidch          poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18046255736Sdavidch}
18047255736Sdavidch
18048255736Sdavidchstatic void
18049255736Sdavidchbxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
18050255736Sdavidch{
18051255736Sdavidch    struct pbf_pN_cmd_regs cmd_regs[] = {
18052255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
18053255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q0 :
18054255736Sdavidch            PBF_REG_P0_TQ_OCCUPANCY,
18055255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18056255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q0 :
18057255736Sdavidch            PBF_REG_P0_TQ_LINES_FREED_CNT},
18058255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
18059255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q1 :
18060255736Sdavidch            PBF_REG_P1_TQ_OCCUPANCY,
18061255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18062255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q1 :
18063255736Sdavidch            PBF_REG_P1_TQ_LINES_FREED_CNT},
18064255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
18065255736Sdavidch            PBF_REG_TQ_OCCUPANCY_LB_Q :
18066255736Sdavidch            PBF_REG_P4_TQ_OCCUPANCY,
18067255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18068255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
18069255736Sdavidch            PBF_REG_P4_TQ_LINES_FREED_CNT}
18070255736Sdavidch    };
18071255736Sdavidch
18072255736Sdavidch    struct pbf_pN_buf_regs buf_regs[] = {
18073255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
18074255736Sdavidch            PBF_REG_INIT_CRD_Q0 :
18075255736Sdavidch            PBF_REG_P0_INIT_CRD ,
18076255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18077255736Sdavidch            PBF_REG_CREDIT_Q0 :
18078255736Sdavidch            PBF_REG_P0_CREDIT,
18079255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18080255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
18081255736Sdavidch            PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
18082255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
18083255736Sdavidch            PBF_REG_INIT_CRD_Q1 :
18084255736Sdavidch            PBF_REG_P1_INIT_CRD,
18085255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18086255736Sdavidch            PBF_REG_CREDIT_Q1 :
18087255736Sdavidch            PBF_REG_P1_CREDIT,
18088255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18089255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
18090255736Sdavidch            PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
18091255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
18092255736Sdavidch            PBF_REG_INIT_CRD_LB_Q :
18093255736Sdavidch            PBF_REG_P4_INIT_CRD,
18094255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18095255736Sdavidch            PBF_REG_CREDIT_LB_Q :
18096255736Sdavidch            PBF_REG_P4_CREDIT,
18097255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
18098255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
18099255736Sdavidch            PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
18100255736Sdavidch    };
18101255736Sdavidch
18102255736Sdavidch    int i;
18103255736Sdavidch
18104255736Sdavidch    /* Verify the command queues are flushed P0, P1, P4 */
18105255736Sdavidch    for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
18106255736Sdavidch        bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
18107255736Sdavidch    }
18108255736Sdavidch
18109255736Sdavidch    /* Verify the transmission buffers are flushed P0, P1, P4 */
18110255736Sdavidch    for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
18111255736Sdavidch        bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
18112255736Sdavidch    }
18113255736Sdavidch}
18114255736Sdavidch
18115255736Sdavidchstatic void
18116255736Sdavidchbxe_hw_enable_status(struct bxe_softc *sc)
18117255736Sdavidch{
18118255736Sdavidch    uint32_t val;
18119255736Sdavidch
18120255736Sdavidch    val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
18121255736Sdavidch    BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
18122255736Sdavidch
18123255736Sdavidch    val = REG_RD(sc, PBF_REG_DISABLE_PF);
18124255736Sdavidch    BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
18125255736Sdavidch
18126255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
18127255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
18128255736Sdavidch
18129255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
18130255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
18131255736Sdavidch
18132255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
18133255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
18134255736Sdavidch
18135255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
18136255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
18137255736Sdavidch
18138255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
18139255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
18140255736Sdavidch
18141255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
18142255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
18143255736Sdavidch}
18144255736Sdavidch
18145255736Sdavidchstatic int
18146255736Sdavidchbxe_pf_flr_clnup(struct bxe_softc *sc)
18147255736Sdavidch{
18148255736Sdavidch    uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
18149255736Sdavidch
18150255736Sdavidch    BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
18151255736Sdavidch
18152255736Sdavidch    /* Re-enable PF target read access */
18153255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
18154255736Sdavidch
18155255736Sdavidch    /* Poll HW usage counters */
18156255736Sdavidch    BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
18157255736Sdavidch    if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
18158255736Sdavidch        return (-1);
18159255736Sdavidch    }
18160255736Sdavidch
18161255736Sdavidch    /* Zero the igu 'trailing edge' and 'leading edge' */
18162255736Sdavidch
18163255736Sdavidch    /* Send the FW cleanup command */
18164255736Sdavidch    if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
18165255736Sdavidch        return (-1);
18166255736Sdavidch    }
18167255736Sdavidch
18168255736Sdavidch    /* ATC cleanup */
18169255736Sdavidch
18170255736Sdavidch    /* Verify TX hw is flushed */
18171255736Sdavidch    bxe_tx_hw_flushed(sc, poll_cnt);
18172255736Sdavidch
18173255736Sdavidch    /* Wait 100ms (not adjusted according to platform) */
18174255736Sdavidch    DELAY(100000);
18175255736Sdavidch
18176255736Sdavidch    /* Verify no pending pci transactions */
18177255736Sdavidch    if (bxe_is_pcie_pending(sc)) {
18178255736Sdavidch        BLOGE(sc, "PCIE Transactions still pending\n");
18179255736Sdavidch    }
18180255736Sdavidch
18181255736Sdavidch    /* Debug */
18182255736Sdavidch    bxe_hw_enable_status(sc);
18183255736Sdavidch
18184255736Sdavidch    /*
18185255736Sdavidch     * Master enable - Due to WB DMAE writes performed before this
18186255736Sdavidch     * register is re-initialized as part of the regular function init
18187255736Sdavidch     */
18188255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18189255736Sdavidch
18190255736Sdavidch    return (0);
18191255736Sdavidch}
18192255736Sdavidch
18193255736Sdavidchstatic int
18194255736Sdavidchbxe_init_hw_func(struct bxe_softc *sc)
18195255736Sdavidch{
18196255736Sdavidch    int port = SC_PORT(sc);
18197255736Sdavidch    int func = SC_FUNC(sc);
18198255736Sdavidch    int init_phase = PHASE_PF0 + func;
18199255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
18200255736Sdavidch    uint16_t cdu_ilt_start;
18201255736Sdavidch    uint32_t addr, val;
18202255736Sdavidch    uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
18203255736Sdavidch    int i, main_mem_width, rc;
18204255736Sdavidch
18205255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
18206255736Sdavidch
18207255736Sdavidch    /* FLR cleanup */
18208255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18209255736Sdavidch        rc = bxe_pf_flr_clnup(sc);
18210255736Sdavidch        if (rc) {
18211255736Sdavidch            BLOGE(sc, "FLR cleanup failed!\n");
18212255736Sdavidch            // XXX bxe_fw_dump(sc);
18213255736Sdavidch            // XXX bxe_idle_chk(sc);
18214255736Sdavidch            return (rc);
18215255736Sdavidch        }
18216255736Sdavidch    }
18217255736Sdavidch
18218255736Sdavidch    /* set MSI reconfigure capability */
18219255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18220255736Sdavidch        addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
18221255736Sdavidch        val = REG_RD(sc, addr);
18222255736Sdavidch        val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
18223255736Sdavidch        REG_WR(sc, addr, val);
18224255736Sdavidch    }
18225255736Sdavidch
18226255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
18227255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
18228255736Sdavidch
18229255736Sdavidch    ilt = sc->ilt;
18230255736Sdavidch    cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
18231255736Sdavidch
18232255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
18233255736Sdavidch        ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
18234255736Sdavidch        ilt->lines[cdu_ilt_start + i].page_mapping =
18235255736Sdavidch            sc->context[i].vcxt_dma.paddr;
18236255736Sdavidch        ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
18237255736Sdavidch    }
18238255736Sdavidch    ecore_ilt_init_op(sc, INITOP_SET);
18239255736Sdavidch
18240295830Sdavidcs    /* Set NIC mode */
18241295830Sdavidcs    REG_WR(sc, PRS_REG_NIC_MODE, 1);
18242295830Sdavidcs    BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
18243255736Sdavidch
18244255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18245255736Sdavidch        uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
18246255736Sdavidch
18247255736Sdavidch        /* Turn on a single ISR mode in IGU if driver is going to use
18248255736Sdavidch         * INT#x or MSI
18249255736Sdavidch         */
18250255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
18251255736Sdavidch            pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
18252255736Sdavidch        }
18253255736Sdavidch
18254255736Sdavidch        /*
18255255736Sdavidch         * Timers workaround bug: function init part.
18256255736Sdavidch         * Need to wait 20msec after initializing ILT,
18257255736Sdavidch         * needed to make sure there are no requests in
18258255736Sdavidch         * one of the PXP internal queues with "old" ILT addresses
18259255736Sdavidch         */
18260255736Sdavidch        DELAY(20000);
18261255736Sdavidch
18262255736Sdavidch        /*
18263255736Sdavidch         * Master enable - Due to WB DMAE writes performed before this
18264255736Sdavidch         * register is re-initialized as part of the regular function
18265255736Sdavidch         * init
18266255736Sdavidch         */
18267255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18268255736Sdavidch        /* Enable the function in IGU */
18269255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
18270255736Sdavidch    }
18271255736Sdavidch
18272255736Sdavidch    sc->dmae_ready = 1;
18273255736Sdavidch
18274255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
18275255736Sdavidch
18276255736Sdavidch    if (!CHIP_IS_E1x(sc))
18277255736Sdavidch        REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
18278255736Sdavidch
18279255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
18280255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
18281255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
18282255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, init_phase);
18283255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
18284255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
18285255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
18286255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
18287255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
18288255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
18289255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
18290255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
18291255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
18292255736Sdavidch
18293255736Sdavidch    if (!CHIP_IS_E1x(sc))
18294255736Sdavidch        REG_WR(sc, QM_REG_PF_EN, 1);
18295255736Sdavidch
18296255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18297255736Sdavidch        REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18298255736Sdavidch        REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18299255736Sdavidch        REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18300255736Sdavidch        REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18301255736Sdavidch    }
18302255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
18303255736Sdavidch
18304255736Sdavidch    ecore_init_block(sc, BLOCK_TM, init_phase);
18305255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
18306255736Sdavidch
18307255736Sdavidch    bxe_iov_init_dq(sc);
18308255736Sdavidch
18309255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
18310255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
18311255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
18312255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
18313255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
18314255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
18315255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
18316255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
18317255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
18318255736Sdavidch    if (!CHIP_IS_E1x(sc))
18319255736Sdavidch        REG_WR(sc, PBF_REG_DISABLE_PF, 0);
18320255736Sdavidch
18321255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
18322255736Sdavidch
18323255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
18324255736Sdavidch
18325255736Sdavidch    if (!CHIP_IS_E1x(sc))
18326255736Sdavidch        REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
18327255736Sdavidch
18328255736Sdavidch    if (IS_MF(sc)) {
18329255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
18330255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
18331255736Sdavidch    }
18332255736Sdavidch
18333255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
18334255736Sdavidch
18335255736Sdavidch    /* HC init per function */
18336255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18337255736Sdavidch        if (CHIP_IS_E1H(sc)) {
18338255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18339255736Sdavidch
18340255736Sdavidch            REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18341255736Sdavidch            REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18342255736Sdavidch        }
18343255736Sdavidch        ecore_init_block(sc, BLOCK_HC, init_phase);
18344255736Sdavidch
18345255736Sdavidch    } else {
18346255736Sdavidch        int num_segs, sb_idx, prod_offset;
18347255736Sdavidch
18348255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18349255736Sdavidch
18350255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
18351255736Sdavidch            REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18352255736Sdavidch            REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18353255736Sdavidch        }
18354255736Sdavidch
18355255736Sdavidch        ecore_init_block(sc, BLOCK_IGU, init_phase);
18356255736Sdavidch
18357255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
18358255736Sdavidch            int dsb_idx = 0;
18359255736Sdavidch            /**
18360255736Sdavidch             * Producer memory:
18361255736Sdavidch             * E2 mode: address 0-135 match to the mapping memory;
18362255736Sdavidch             * 136 - PF0 default prod; 137 - PF1 default prod;
18363255736Sdavidch             * 138 - PF2 default prod; 139 - PF3 default prod;
18364255736Sdavidch             * 140 - PF0 attn prod;    141 - PF1 attn prod;
18365255736Sdavidch             * 142 - PF2 attn prod;    143 - PF3 attn prod;
18366255736Sdavidch             * 144-147 reserved.
18367255736Sdavidch             *
18368255736Sdavidch             * E1.5 mode - In backward compatible mode;
18369255736Sdavidch             * for non default SB; each even line in the memory
18370255736Sdavidch             * holds the U producer and each odd line hold
18371255736Sdavidch             * the C producer. The first 128 producers are for
18372255736Sdavidch             * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
18373255736Sdavidch             * producers are for the DSB for each PF.
18374255736Sdavidch             * Each PF has five segments: (the order inside each
18375255736Sdavidch             * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
18376255736Sdavidch             * 132-135 C prods; 136-139 X prods; 140-143 T prods;
18377255736Sdavidch             * 144-147 attn prods;
18378255736Sdavidch             */
18379255736Sdavidch            /* non-default-status-blocks */
18380255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18381255736Sdavidch                IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
18382255736Sdavidch            for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
18383255736Sdavidch                prod_offset = (sc->igu_base_sb + sb_idx) *
18384255736Sdavidch                    num_segs;
18385255736Sdavidch
18386255736Sdavidch                for (i = 0; i < num_segs; i++) {
18387255736Sdavidch                    addr = IGU_REG_PROD_CONS_MEMORY +
18388255736Sdavidch                            (prod_offset + i) * 4;
18389255736Sdavidch                    REG_WR(sc, addr, 0);
18390255736Sdavidch                }
18391255736Sdavidch                /* send consumer update with value 0 */
18392255736Sdavidch                bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
18393255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18394255736Sdavidch                bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
18395255736Sdavidch            }
18396255736Sdavidch
18397255736Sdavidch            /* default-status-blocks */
18398255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18399255736Sdavidch                IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
18400255736Sdavidch
18401255736Sdavidch            if (CHIP_IS_MODE_4_PORT(sc))
18402255736Sdavidch                dsb_idx = SC_FUNC(sc);
18403255736Sdavidch            else
18404255736Sdavidch                dsb_idx = SC_VN(sc);
18405255736Sdavidch
18406255736Sdavidch            prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
18407255736Sdavidch                       IGU_BC_BASE_DSB_PROD + dsb_idx :
18408255736Sdavidch                       IGU_NORM_BASE_DSB_PROD + dsb_idx);
18409255736Sdavidch
18410255736Sdavidch            /*
18411255736Sdavidch             * igu prods come in chunks of E1HVN_MAX (4) -
18412255736Sdavidch             * does not matters what is the current chip mode
18413255736Sdavidch             */
18414255736Sdavidch            for (i = 0; i < (num_segs * E1HVN_MAX);
18415255736Sdavidch                 i += E1HVN_MAX) {
18416255736Sdavidch                addr = IGU_REG_PROD_CONS_MEMORY +
18417255736Sdavidch                            (prod_offset + i)*4;
18418255736Sdavidch                REG_WR(sc, addr, 0);
18419255736Sdavidch            }
18420255736Sdavidch            /* send consumer update with 0 */
18421255736Sdavidch            if (CHIP_INT_MODE_IS_BC(sc)) {
18422255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18423255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18424255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18425255736Sdavidch                           CSTORM_ID, 0, IGU_INT_NOP, 1);
18426255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18427255736Sdavidch                           XSTORM_ID, 0, IGU_INT_NOP, 1);
18428255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18429255736Sdavidch                           TSTORM_ID, 0, IGU_INT_NOP, 1);
18430255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18431255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
18432255736Sdavidch            } else {
18433255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18434255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
18435255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
18436255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
18437255736Sdavidch            }
18438255736Sdavidch            bxe_igu_clear_sb(sc, sc->igu_dsb_id);
18439255736Sdavidch
18440255736Sdavidch            /* !!! these should become driver const once
18441255736Sdavidch               rf-tool supports split-68 const */
18442255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
18443255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
18444255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
18445255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
18446255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
18447255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
18448255736Sdavidch        }
18449255736Sdavidch    }
18450255736Sdavidch
18451255736Sdavidch    /* Reset PCIE errors for debug */
18452255736Sdavidch    REG_WR(sc, 0x2114, 0xffffffff);
18453255736Sdavidch    REG_WR(sc, 0x2120, 0xffffffff);
18454255736Sdavidch
18455255736Sdavidch    if (CHIP_IS_E1x(sc)) {
18456255736Sdavidch        main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
18457255736Sdavidch        main_mem_base = HC_REG_MAIN_MEMORY +
18458255736Sdavidch                SC_PORT(sc) * (main_mem_size * 4);
18459255736Sdavidch        main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
18460255736Sdavidch        main_mem_width = 8;
18461255736Sdavidch
18462255736Sdavidch        val = REG_RD(sc, main_mem_prty_clr);
18463255736Sdavidch        if (val) {
18464255736Sdavidch            BLOGD(sc, DBG_LOAD,
18465255736Sdavidch                  "Parity errors in HC block during function init (0x%x)!\n",
18466255736Sdavidch                  val);
18467255736Sdavidch        }
18468255736Sdavidch
18469255736Sdavidch        /* Clear "false" parity errors in MSI-X table */
18470255736Sdavidch        for (i = main_mem_base;
18471255736Sdavidch             i < main_mem_base + main_mem_size * 4;
18472255736Sdavidch             i += main_mem_width) {
18473255736Sdavidch            bxe_read_dmae(sc, i, main_mem_width / 4);
18474255736Sdavidch            bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
18475255736Sdavidch                           i, main_mem_width / 4);
18476255736Sdavidch        }
18477255736Sdavidch        /* Clear HC parity attention */
18478255736Sdavidch        REG_RD(sc, main_mem_prty_clr);
18479255736Sdavidch    }
18480255736Sdavidch
18481255736Sdavidch#if 1
18482255736Sdavidch    /* Enable STORMs SP logging */
18483255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM +
18484255736Sdavidch           USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18485255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM +
18486255736Sdavidch           TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18487255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18488255736Sdavidch           CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18489255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM +
18490255736Sdavidch           XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18491255736Sdavidch#endif
18492255736Sdavidch
18493255736Sdavidch    elink_phy_probe(&sc->link_params);
18494255736Sdavidch
18495255736Sdavidch    return (0);
18496255736Sdavidch}
18497255736Sdavidch
18498255736Sdavidchstatic void
18499255736Sdavidchbxe_link_reset(struct bxe_softc *sc)
18500255736Sdavidch{
18501255736Sdavidch    if (!BXE_NOMCP(sc)) {
18502284335Sdavidcs	bxe_acquire_phy_lock(sc);
18503255736Sdavidch        elink_lfa_reset(&sc->link_params, &sc->link_vars);
18504284335Sdavidcs	bxe_release_phy_lock(sc);
18505255736Sdavidch    } else {
18506255736Sdavidch        if (!CHIP_REV_IS_SLOW(sc)) {
18507255736Sdavidch            BLOGW(sc, "Bootcode is missing - cannot reset link\n");
18508255736Sdavidch        }
18509255736Sdavidch    }
18510255736Sdavidch}
18511255736Sdavidch
18512255736Sdavidchstatic void
18513255736Sdavidchbxe_reset_port(struct bxe_softc *sc)
18514255736Sdavidch{
18515255736Sdavidch    int port = SC_PORT(sc);
18516255736Sdavidch    uint32_t val;
18517255736Sdavidch
18518315881Sdavidcs	ELINK_DEBUG_P0(sc, "bxe_reset_port called\n");
18519255736Sdavidch    /* reset physical Link */
18520255736Sdavidch    bxe_link_reset(sc);
18521255736Sdavidch
18522255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18523255736Sdavidch
18524255736Sdavidch    /* Do not rcv packets to BRB */
18525255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18526255736Sdavidch    /* Do not direct rcv packets that are not for MCP to the BRB */
18527255736Sdavidch    REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18528255736Sdavidch               NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18529255736Sdavidch
18530255736Sdavidch    /* Configure AEU */
18531255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18532255736Sdavidch
18533255736Sdavidch    DELAY(100000);
18534255736Sdavidch
18535255736Sdavidch    /* Check for BRB port occupancy */
18536255736Sdavidch    val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18537255736Sdavidch    if (val) {
18538255736Sdavidch        BLOGD(sc, DBG_LOAD,
18539255736Sdavidch              "BRB1 is not empty, %d blocks are occupied\n", val);
18540255736Sdavidch    }
18541255736Sdavidch
18542255736Sdavidch    /* TODO: Close Doorbell port? */
18543255736Sdavidch}
18544255736Sdavidch
18545255736Sdavidchstatic void
18546255736Sdavidchbxe_ilt_wr(struct bxe_softc *sc,
18547255736Sdavidch           uint32_t         index,
18548255736Sdavidch           bus_addr_t       addr)
18549255736Sdavidch{
18550255736Sdavidch    int reg;
18551255736Sdavidch    uint32_t wb_write[2];
18552255736Sdavidch
18553255736Sdavidch    if (CHIP_IS_E1(sc)) {
18554255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18555255736Sdavidch    } else {
18556255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18557255736Sdavidch    }
18558255736Sdavidch
18559255736Sdavidch    wb_write[0] = ONCHIP_ADDR1(addr);
18560255736Sdavidch    wb_write[1] = ONCHIP_ADDR2(addr);
18561255736Sdavidch    REG_WR_DMAE(sc, reg, wb_write, 2);
18562255736Sdavidch}
18563255736Sdavidch
18564255736Sdavidchstatic void
18565255736Sdavidchbxe_clear_func_ilt(struct bxe_softc *sc,
18566255736Sdavidch                   uint32_t         func)
18567255736Sdavidch{
18568255736Sdavidch    uint32_t i, base = FUNC_ILT_BASE(func);
18569255736Sdavidch    for (i = base; i < base + ILT_PER_FUNC; i++) {
18570255736Sdavidch        bxe_ilt_wr(sc, i, 0);
18571255736Sdavidch    }
18572255736Sdavidch}
18573255736Sdavidch
18574255736Sdavidchstatic void
18575255736Sdavidchbxe_reset_func(struct bxe_softc *sc)
18576255736Sdavidch{
18577255736Sdavidch    struct bxe_fastpath *fp;
18578255736Sdavidch    int port = SC_PORT(sc);
18579255736Sdavidch    int func = SC_FUNC(sc);
18580255736Sdavidch    int i;
18581255736Sdavidch
18582255736Sdavidch    /* Disable the function in the FW */
18583255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18584255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18585255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18586255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18587255736Sdavidch
18588255736Sdavidch    /* FP SBs */
18589255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
18590255736Sdavidch        fp = &sc->fp[i];
18591255736Sdavidch        REG_WR8(sc, BAR_CSTRORM_INTMEM +
18592255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18593255736Sdavidch                SB_DISABLED);
18594255736Sdavidch    }
18595255736Sdavidch
18596255736Sdavidch    /* SP SB */
18597255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18598255736Sdavidch            CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18599255736Sdavidch            SB_DISABLED);
18600255736Sdavidch
18601255736Sdavidch    for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18602255736Sdavidch        REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18603255736Sdavidch    }
18604255736Sdavidch
18605255736Sdavidch    /* Configure IGU */
18606255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18607255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18608255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18609255736Sdavidch    } else {
18610255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18611255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18612255736Sdavidch    }
18613255736Sdavidch
18614255736Sdavidch    if (CNIC_LOADED(sc)) {
18615255736Sdavidch        /* Disable Timer scan */
18616255736Sdavidch        REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18617255736Sdavidch        /*
18618255736Sdavidch         * Wait for at least 10ms and up to 2 second for the timers
18619255736Sdavidch         * scan to complete
18620255736Sdavidch         */
18621255736Sdavidch        for (i = 0; i < 200; i++) {
18622255736Sdavidch            DELAY(10000);
18623255736Sdavidch            if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18624255736Sdavidch                break;
18625255736Sdavidch        }
18626255736Sdavidch    }
18627255736Sdavidch
18628255736Sdavidch    /* Clear ILT */
18629255736Sdavidch    bxe_clear_func_ilt(sc, func);
18630255736Sdavidch
18631255736Sdavidch    /*
18632255736Sdavidch     * Timers workaround bug for E2: if this is vnic-3,
18633255736Sdavidch     * we need to set the entire ilt range for this timers.
18634255736Sdavidch     */
18635255736Sdavidch    if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18636255736Sdavidch        struct ilt_client_info ilt_cli;
18637255736Sdavidch        /* use dummy TM client */
18638255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18639255736Sdavidch        ilt_cli.start = 0;
18640255736Sdavidch        ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18641255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
18642255736Sdavidch
18643255736Sdavidch        ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18644255736Sdavidch    }
18645255736Sdavidch
18646255736Sdavidch    /* this assumes that reset_port() called before reset_func()*/
18647255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18648255736Sdavidch        bxe_pf_disable(sc);
18649255736Sdavidch    }
18650255736Sdavidch
18651255736Sdavidch    sc->dmae_ready = 0;
18652255736Sdavidch}
18653255736Sdavidch
18654255736Sdavidchstatic int
18655255736Sdavidchbxe_gunzip_init(struct bxe_softc *sc)
18656255736Sdavidch{
18657255736Sdavidch    return (0);
18658255736Sdavidch}
18659255736Sdavidch
18660255736Sdavidchstatic void
18661255736Sdavidchbxe_gunzip_end(struct bxe_softc *sc)
18662255736Sdavidch{
18663255736Sdavidch    return;
18664255736Sdavidch}
18665255736Sdavidch
18666255736Sdavidchstatic int
18667255736Sdavidchbxe_init_firmware(struct bxe_softc *sc)
18668255736Sdavidch{
18669255736Sdavidch    if (CHIP_IS_E1(sc)) {
18670255736Sdavidch        ecore_init_e1_firmware(sc);
18671255736Sdavidch        sc->iro_array = e1_iro_arr;
18672255736Sdavidch    } else if (CHIP_IS_E1H(sc)) {
18673255736Sdavidch        ecore_init_e1h_firmware(sc);
18674255736Sdavidch        sc->iro_array = e1h_iro_arr;
18675255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
18676255736Sdavidch        ecore_init_e2_firmware(sc);
18677255736Sdavidch        sc->iro_array = e2_iro_arr;
18678255736Sdavidch    } else {
18679255736Sdavidch        BLOGE(sc, "Unsupported chip revision\n");
18680255736Sdavidch        return (-1);
18681255736Sdavidch    }
18682255736Sdavidch
18683255736Sdavidch    return (0);
18684255736Sdavidch}
18685255736Sdavidch
18686255736Sdavidchstatic void
18687255736Sdavidchbxe_release_firmware(struct bxe_softc *sc)
18688255736Sdavidch{
18689255736Sdavidch    /* Do nothing */
18690255736Sdavidch    return;
18691255736Sdavidch}
18692255736Sdavidch
18693255736Sdavidchstatic int
18694255736Sdavidchecore_gunzip(struct bxe_softc *sc,
18695255736Sdavidch             const uint8_t    *zbuf,
18696255736Sdavidch             int              len)
18697255736Sdavidch{
18698255736Sdavidch    /* XXX : Implement... */
18699255736Sdavidch    BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18700255736Sdavidch    return (FALSE);
18701255736Sdavidch}
18702255736Sdavidch
18703255736Sdavidchstatic void
18704255736Sdavidchecore_reg_wr_ind(struct bxe_softc *sc,
18705255736Sdavidch                 uint32_t         addr,
18706255736Sdavidch                 uint32_t         val)
18707255736Sdavidch{
18708255736Sdavidch    bxe_reg_wr_ind(sc, addr, val);
18709255736Sdavidch}
18710255736Sdavidch
18711255736Sdavidchstatic void
18712255736Sdavidchecore_write_dmae_phys_len(struct bxe_softc *sc,
18713255736Sdavidch                          bus_addr_t       phys_addr,
18714255736Sdavidch                          uint32_t         addr,
18715255736Sdavidch                          uint32_t         len)
18716255736Sdavidch{
18717255736Sdavidch    bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18718255736Sdavidch}
18719255736Sdavidch
18720255736Sdavidchvoid
18721255736Sdavidchecore_storm_memset_struct(struct bxe_softc *sc,
18722255736Sdavidch                          uint32_t         addr,
18723255736Sdavidch                          size_t           size,
18724255736Sdavidch                          uint32_t         *data)
18725255736Sdavidch{
18726255736Sdavidch    uint8_t i;
18727255736Sdavidch    for (i = 0; i < size/4; i++) {
18728255736Sdavidch        REG_WR(sc, addr + (i * 4), data[i]);
18729255736Sdavidch    }
18730255736Sdavidch}
18731255736Sdavidch
18732292639Sdavidcs
18733292639Sdavidcs/*
18734292639Sdavidcs * character device - ioctl interface definitions
18735292639Sdavidcs */
18736292639Sdavidcs
18737292639Sdavidcs
18738292639Sdavidcs#include "bxe_dump.h"
18739292639Sdavidcs#include "bxe_ioctl.h"
18740292639Sdavidcs#include <sys/conf.h>
18741292639Sdavidcs
18742292639Sdavidcsstatic int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18743292639Sdavidcs                struct thread *td);
18744292639Sdavidcs
18745292639Sdavidcsstatic struct cdevsw bxe_cdevsw = {
18746292639Sdavidcs    .d_version = D_VERSION,
18747292639Sdavidcs    .d_ioctl = bxe_eioctl,
18748292639Sdavidcs    .d_name = "bxecnic",
18749292639Sdavidcs};
18750292639Sdavidcs
18751292639Sdavidcs#define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18752292639Sdavidcs
18753292639Sdavidcs
18754292639Sdavidcs#define DUMP_ALL_PRESETS        0x1FFF
18755292639Sdavidcs#define DUMP_MAX_PRESETS        13
18756292639Sdavidcs#define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18757292639Sdavidcs#define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18758292639Sdavidcs#define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18759292639Sdavidcs#define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18760292639Sdavidcs#define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18761292639Sdavidcs
18762292639Sdavidcs#define IS_REG_IN_PRESET(presets, idx)  \
18763292639Sdavidcs                ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18764292639Sdavidcs
18765292639Sdavidcs
18766292639Sdavidcsstatic int
18767292639Sdavidcsbxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18768292639Sdavidcs{
18769292639Sdavidcs    if (CHIP_IS_E1(sc))
18770292639Sdavidcs        return dump_num_registers[0][preset-1];
18771292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18772292639Sdavidcs        return dump_num_registers[1][preset-1];
18773292639Sdavidcs    else if (CHIP_IS_E2(sc))
18774292639Sdavidcs        return dump_num_registers[2][preset-1];
18775292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18776292639Sdavidcs        return dump_num_registers[3][preset-1];
18777292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18778292639Sdavidcs        return dump_num_registers[4][preset-1];
18779292639Sdavidcs    else
18780292639Sdavidcs        return 0;
18781292639Sdavidcs}
18782292639Sdavidcs
18783292639Sdavidcsstatic int
18784292639Sdavidcsbxe_get_total_regs_len32(struct bxe_softc *sc)
18785292639Sdavidcs{
18786292639Sdavidcs    uint32_t preset_idx;
18787292639Sdavidcs    int regdump_len32 = 0;
18788292639Sdavidcs
18789292639Sdavidcs
18790292639Sdavidcs    /* Calculate the total preset regs length */
18791292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18792292639Sdavidcs        regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18793292639Sdavidcs    }
18794292639Sdavidcs
18795292639Sdavidcs    return regdump_len32;
18796292639Sdavidcs}
18797292639Sdavidcs
18798292639Sdavidcsstatic const uint32_t *
18799292639Sdavidcs__bxe_get_page_addr_ar(struct bxe_softc *sc)
18800292639Sdavidcs{
18801292639Sdavidcs    if (CHIP_IS_E2(sc))
18802292639Sdavidcs        return page_vals_e2;
18803292639Sdavidcs    else if (CHIP_IS_E3(sc))
18804292639Sdavidcs        return page_vals_e3;
18805292639Sdavidcs    else
18806292639Sdavidcs        return NULL;
18807292639Sdavidcs}
18808292639Sdavidcs
18809292639Sdavidcsstatic uint32_t
18810292639Sdavidcs__bxe_get_page_reg_num(struct bxe_softc *sc)
18811292639Sdavidcs{
18812292639Sdavidcs    if (CHIP_IS_E2(sc))
18813292639Sdavidcs        return PAGE_MODE_VALUES_E2;
18814292639Sdavidcs    else if (CHIP_IS_E3(sc))
18815292639Sdavidcs        return PAGE_MODE_VALUES_E3;
18816292639Sdavidcs    else
18817292639Sdavidcs        return 0;
18818292639Sdavidcs}
18819292639Sdavidcs
18820292639Sdavidcsstatic const uint32_t *
18821292639Sdavidcs__bxe_get_page_write_ar(struct bxe_softc *sc)
18822292639Sdavidcs{
18823292639Sdavidcs    if (CHIP_IS_E2(sc))
18824292639Sdavidcs        return page_write_regs_e2;
18825292639Sdavidcs    else if (CHIP_IS_E3(sc))
18826292639Sdavidcs        return page_write_regs_e3;
18827292639Sdavidcs    else
18828292639Sdavidcs        return NULL;
18829292639Sdavidcs}
18830292639Sdavidcs
18831292639Sdavidcsstatic uint32_t
18832292639Sdavidcs__bxe_get_page_write_num(struct bxe_softc *sc)
18833292639Sdavidcs{
18834292639Sdavidcs    if (CHIP_IS_E2(sc))
18835292639Sdavidcs        return PAGE_WRITE_REGS_E2;
18836292639Sdavidcs    else if (CHIP_IS_E3(sc))
18837292639Sdavidcs        return PAGE_WRITE_REGS_E3;
18838292639Sdavidcs    else
18839292639Sdavidcs        return 0;
18840292639Sdavidcs}
18841292639Sdavidcs
18842292639Sdavidcsstatic const struct reg_addr *
18843292639Sdavidcs__bxe_get_page_read_ar(struct bxe_softc *sc)
18844292639Sdavidcs{
18845292639Sdavidcs    if (CHIP_IS_E2(sc))
18846292639Sdavidcs        return page_read_regs_e2;
18847292639Sdavidcs    else if (CHIP_IS_E3(sc))
18848292639Sdavidcs        return page_read_regs_e3;
18849292639Sdavidcs    else
18850292639Sdavidcs        return NULL;
18851292639Sdavidcs}
18852292639Sdavidcs
18853292639Sdavidcsstatic uint32_t
18854292639Sdavidcs__bxe_get_page_read_num(struct bxe_softc *sc)
18855292639Sdavidcs{
18856292639Sdavidcs    if (CHIP_IS_E2(sc))
18857292639Sdavidcs        return PAGE_READ_REGS_E2;
18858292639Sdavidcs    else if (CHIP_IS_E3(sc))
18859292639Sdavidcs        return PAGE_READ_REGS_E3;
18860292639Sdavidcs    else
18861292639Sdavidcs        return 0;
18862292639Sdavidcs}
18863292639Sdavidcs
18864292639Sdavidcsstatic bool
18865292639Sdavidcsbxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18866292639Sdavidcs{
18867292639Sdavidcs    if (CHIP_IS_E1(sc))
18868292639Sdavidcs        return IS_E1_REG(reg_info->chips);
18869292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18870292639Sdavidcs        return IS_E1H_REG(reg_info->chips);
18871292639Sdavidcs    else if (CHIP_IS_E2(sc))
18872292639Sdavidcs        return IS_E2_REG(reg_info->chips);
18873292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18874292639Sdavidcs        return IS_E3A0_REG(reg_info->chips);
18875292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18876292639Sdavidcs        return IS_E3B0_REG(reg_info->chips);
18877292639Sdavidcs    else
18878292639Sdavidcs        return 0;
18879292639Sdavidcs}
18880292639Sdavidcs
18881292639Sdavidcsstatic bool
18882292639Sdavidcsbxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18883292639Sdavidcs{
18884292639Sdavidcs    if (CHIP_IS_E1(sc))
18885292639Sdavidcs        return IS_E1_REG(wreg_info->chips);
18886292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18887292639Sdavidcs        return IS_E1H_REG(wreg_info->chips);
18888292639Sdavidcs    else if (CHIP_IS_E2(sc))
18889292639Sdavidcs        return IS_E2_REG(wreg_info->chips);
18890292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18891292639Sdavidcs        return IS_E3A0_REG(wreg_info->chips);
18892292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18893292639Sdavidcs        return IS_E3B0_REG(wreg_info->chips);
18894292639Sdavidcs    else
18895292639Sdavidcs        return 0;
18896292639Sdavidcs}
18897292639Sdavidcs
18898292639Sdavidcs/**
18899292639Sdavidcs * bxe_read_pages_regs - read "paged" registers
18900292639Sdavidcs *
18901292639Sdavidcs * @bp          device handle
18902292639Sdavidcs * @p           output buffer
18903292639Sdavidcs *
18904292639Sdavidcs * Reads "paged" memories: memories that may only be read by first writing to a
18905292639Sdavidcs * specific address ("write address") and then reading from a specific address
18906292639Sdavidcs * ("read address"). There may be more than one write address per "page" and
18907292639Sdavidcs * more than one read address per write address.
18908292639Sdavidcs */
18909292639Sdavidcsstatic void
18910292639Sdavidcsbxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18911292639Sdavidcs{
18912292639Sdavidcs    uint32_t i, j, k, n;
18913292639Sdavidcs
18914292639Sdavidcs    /* addresses of the paged registers */
18915292639Sdavidcs    const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18916292639Sdavidcs    /* number of paged registers */
18917292639Sdavidcs    int num_pages = __bxe_get_page_reg_num(sc);
18918292639Sdavidcs    /* write addresses */
18919292639Sdavidcs    const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18920292639Sdavidcs    /* number of write addresses */
18921292639Sdavidcs    int write_num = __bxe_get_page_write_num(sc);
18922292639Sdavidcs    /* read addresses info */
18923292639Sdavidcs    const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18924292639Sdavidcs    /* number of read addresses */
18925292639Sdavidcs    int read_num = __bxe_get_page_read_num(sc);
18926292639Sdavidcs    uint32_t addr, size;
18927292639Sdavidcs
18928292639Sdavidcs    for (i = 0; i < num_pages; i++) {
18929292639Sdavidcs        for (j = 0; j < write_num; j++) {
18930292639Sdavidcs            REG_WR(sc, write_addr[j], page_addr[i]);
18931292639Sdavidcs
18932292639Sdavidcs            for (k = 0; k < read_num; k++) {
18933292639Sdavidcs                if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18934292639Sdavidcs                    size = read_addr[k].size;
18935292639Sdavidcs                    for (n = 0; n < size; n++) {
18936292639Sdavidcs                        addr = read_addr[k].addr + n*4;
18937292639Sdavidcs                        *p++ = REG_RD(sc, addr);
18938292639Sdavidcs                    }
18939292639Sdavidcs                }
18940292639Sdavidcs            }
18941292639Sdavidcs        }
18942292639Sdavidcs    }
18943292639Sdavidcs    return;
18944292639Sdavidcs}
18945292639Sdavidcs
18946292639Sdavidcs
18947292639Sdavidcsstatic int
18948292639Sdavidcsbxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18949292639Sdavidcs{
18950292639Sdavidcs    uint32_t i, j, addr;
18951292639Sdavidcs    const struct wreg_addr *wreg_addr_p = NULL;
18952292639Sdavidcs
18953292639Sdavidcs    if (CHIP_IS_E1(sc))
18954292639Sdavidcs        wreg_addr_p = &wreg_addr_e1;
18955292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18956292639Sdavidcs        wreg_addr_p = &wreg_addr_e1h;
18957292639Sdavidcs    else if (CHIP_IS_E2(sc))
18958292639Sdavidcs        wreg_addr_p = &wreg_addr_e2;
18959292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18960292639Sdavidcs        wreg_addr_p = &wreg_addr_e3;
18961292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18962292639Sdavidcs        wreg_addr_p = &wreg_addr_e3b0;
18963292639Sdavidcs    else
18964292639Sdavidcs        return (-1);
18965292639Sdavidcs
18966292639Sdavidcs    /* Read the idle_chk registers */
18967292639Sdavidcs    for (i = 0; i < IDLE_REGS_COUNT; i++) {
18968292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18969292639Sdavidcs            IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18970292639Sdavidcs            for (j = 0; j < idle_reg_addrs[i].size; j++)
18971292639Sdavidcs                *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18972292639Sdavidcs        }
18973292639Sdavidcs    }
18974292639Sdavidcs
18975292639Sdavidcs    /* Read the regular registers */
18976292639Sdavidcs    for (i = 0; i < REGS_COUNT; i++) {
18977292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18978292639Sdavidcs            IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18979292639Sdavidcs            for (j = 0; j < reg_addrs[i].size; j++)
18980292639Sdavidcs                *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18981292639Sdavidcs        }
18982292639Sdavidcs    }
18983292639Sdavidcs
18984292639Sdavidcs    /* Read the CAM registers */
18985292639Sdavidcs    if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18986292639Sdavidcs        IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18987292639Sdavidcs        for (i = 0; i < wreg_addr_p->size; i++) {
18988292639Sdavidcs            *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
18989292639Sdavidcs
18990292639Sdavidcs            /* In case of wreg_addr register, read additional
18991292639Sdavidcs               registers from read_regs array
18992292639Sdavidcs             */
18993292639Sdavidcs            for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
18994292639Sdavidcs                addr = *(wreg_addr_p->read_regs);
18995292639Sdavidcs                *p++ = REG_RD(sc, addr + j*4);
18996292639Sdavidcs            }
18997292639Sdavidcs        }
18998292639Sdavidcs    }
18999292639Sdavidcs
19000292639Sdavidcs    /* Paged registers are supported in E2 & E3 only */
19001292639Sdavidcs    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
19002292639Sdavidcs        /* Read "paged" registers */
19003292639Sdavidcs        bxe_read_pages_regs(sc, p, preset);
19004292639Sdavidcs    }
19005292639Sdavidcs
19006292639Sdavidcs    return 0;
19007292639Sdavidcs}
19008292639Sdavidcs
19009298294Sdavidcsint
19010295823Sdavidcsbxe_grc_dump(struct bxe_softc *sc)
19011292639Sdavidcs{
19012292639Sdavidcs    int rval = 0;
19013292639Sdavidcs    uint32_t preset_idx;
19014292639Sdavidcs    uint8_t *buf;
19015292639Sdavidcs    uint32_t size;
19016292639Sdavidcs    struct  dump_header *d_hdr;
19017298294Sdavidcs    uint32_t i;
19018298294Sdavidcs    uint32_t reg_val;
19019298294Sdavidcs    uint32_t reg_addr;
19020298294Sdavidcs    uint32_t cmd_offset;
19021298294Sdavidcs    struct ecore_ilt *ilt = SC_ILT(sc);
19022298294Sdavidcs    struct bxe_fastpath *fp;
19023298294Sdavidcs    struct ilt_client_info *ilt_cli;
19024298294Sdavidcs    int grc_dump_size;
19025295823Sdavidcs
19026298294Sdavidcs
19027298294Sdavidcs    if (sc->grcdump_done || sc->grcdump_started)
19028295823Sdavidcs	return (rval);
19029292639Sdavidcs
19030298294Sdavidcs    sc->grcdump_started = 1;
19031298294Sdavidcs    BLOGI(sc, "Started collecting grcdump\n");
19032298294Sdavidcs
19033298294Sdavidcs    grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19034298294Sdavidcs                sizeof(struct  dump_header);
19035298294Sdavidcs
19036298294Sdavidcs    sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
19037298294Sdavidcs
19038298294Sdavidcs    if (sc->grc_dump == NULL) {
19039298294Sdavidcs        BLOGW(sc, "Unable to allocate memory for grcdump collection\n");
19040298294Sdavidcs        return(ENOMEM);
19041298294Sdavidcs    }
19042298294Sdavidcs
19043298294Sdavidcs
19044298294Sdavidcs
19045298294Sdavidcs    /* Disable parity attentions as long as following dump may
19046298294Sdavidcs     * cause false alarms by reading never written registers. We
19047298294Sdavidcs     * will re-enable parity attentions right after the dump.
19048298294Sdavidcs     */
19049298294Sdavidcs
19050298294Sdavidcs    /* Disable parity on path 0 */
19051298294Sdavidcs    bxe_pretend_func(sc, 0);
19052298294Sdavidcs
19053292639Sdavidcs    ecore_disable_blocks_parity(sc);
19054292639Sdavidcs
19055298294Sdavidcs    /* Disable parity on path 1 */
19056298294Sdavidcs    bxe_pretend_func(sc, 1);
19057298294Sdavidcs    ecore_disable_blocks_parity(sc);
19058298294Sdavidcs
19059298294Sdavidcs    /* Return to current function */
19060298294Sdavidcs    bxe_pretend_func(sc, SC_ABS_FUNC(sc));
19061298294Sdavidcs
19062295823Sdavidcs    buf = sc->grc_dump;
19063295823Sdavidcs    d_hdr = sc->grc_dump;
19064292639Sdavidcs
19065292639Sdavidcs    d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
19066292639Sdavidcs    d_hdr->version = BNX2X_DUMP_VERSION;
19067292639Sdavidcs    d_hdr->preset = DUMP_ALL_PRESETS;
19068292639Sdavidcs
19069292639Sdavidcs    if (CHIP_IS_E1(sc)) {
19070292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1;
19071292639Sdavidcs    } else if (CHIP_IS_E1H(sc)) {
19072292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1H;
19073292639Sdavidcs    } else if (CHIP_IS_E2(sc)) {
19074292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E2 |
19075292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
19076292639Sdavidcs    } else if (CHIP_IS_E3A0(sc)) {
19077292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
19078292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
19079292639Sdavidcs    } else if (CHIP_IS_E3B0(sc)) {
19080292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
19081292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
19082292639Sdavidcs    }
19083292639Sdavidcs
19084292639Sdavidcs    buf += sizeof(struct  dump_header);
19085292639Sdavidcs
19086292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
19087292639Sdavidcs
19088292639Sdavidcs        /* Skip presets with IOR */
19089292639Sdavidcs        if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
19090292639Sdavidcs            (preset_idx == 11))
19091292639Sdavidcs            continue;
19092292639Sdavidcs
19093298294Sdavidcs        rval = bxe_get_preset_regs(sc, (uint32_t *)buf, preset_idx);
19094292639Sdavidcs
19095292639Sdavidcs	if (rval)
19096292639Sdavidcs            break;
19097292639Sdavidcs
19098292639Sdavidcs        size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
19099292639Sdavidcs
19100292639Sdavidcs        buf += size;
19101292639Sdavidcs    }
19102292639Sdavidcs
19103298294Sdavidcs    bxe_pretend_func(sc, 0);
19104292639Sdavidcs    ecore_clear_blocks_parity(sc);
19105292639Sdavidcs    ecore_enable_blocks_parity(sc);
19106292639Sdavidcs
19107298294Sdavidcs    bxe_pretend_func(sc, 1);
19108298294Sdavidcs    ecore_clear_blocks_parity(sc);
19109298294Sdavidcs    ecore_enable_blocks_parity(sc);
19110298294Sdavidcs
19111298294Sdavidcs    /* Return to current function */
19112298294Sdavidcs    bxe_pretend_func(sc, SC_ABS_FUNC(sc));
19113298294Sdavidcs
19114298294Sdavidcs
19115298294Sdavidcs
19116315881Sdavidcs    if(sc->state == BXE_STATE_OPEN) {
19117315881Sdavidcs        if(sc->fw_stats_req  != NULL) {
19118315881Sdavidcs    		BLOGI(sc, "fw stats start_paddr %#jx end_paddr %#jx vaddr %p size 0x%x\n",
19119315881Sdavidcs        			(uintmax_t)sc->fw_stats_req_mapping,
19120315881Sdavidcs        			(uintmax_t)sc->fw_stats_data_mapping,
19121315881Sdavidcs        			sc->fw_stats_req, (sc->fw_stats_req_size + sc->fw_stats_data_size));
19122315881Sdavidcs		}
19123315881Sdavidcs		if(sc->def_sb != NULL) {
19124315881Sdavidcs			BLOGI(sc, "def_status_block paddr %p vaddr %p size 0x%zx\n",
19125315881Sdavidcs        			(void *)sc->def_sb_dma.paddr, sc->def_sb,
19126315881Sdavidcs        			sizeof(struct host_sp_status_block));
19127315881Sdavidcs		}
19128315881Sdavidcs		if(sc->eq_dma.vaddr != NULL) {
19129315881Sdavidcs    		BLOGI(sc, "event_queue paddr %#jx vaddr %p size 0x%x\n",
19130315881Sdavidcs        			(uintmax_t)sc->eq_dma.paddr, sc->eq_dma.vaddr, BCM_PAGE_SIZE);
19131315881Sdavidcs		}
19132315881Sdavidcs		if(sc->sp_dma.vaddr != NULL) {
19133315881Sdavidcs    		BLOGI(sc, "slow path paddr %#jx vaddr %p size 0x%zx\n",
19134315881Sdavidcs        			(uintmax_t)sc->sp_dma.paddr, sc->sp_dma.vaddr,
19135315881Sdavidcs        			sizeof(struct bxe_slowpath));
19136315881Sdavidcs		}
19137315881Sdavidcs		if(sc->spq_dma.vaddr != NULL) {
19138315881Sdavidcs    		BLOGI(sc, "slow path queue paddr %#jx vaddr %p size 0x%x\n",
19139315881Sdavidcs        			(uintmax_t)sc->spq_dma.paddr, sc->spq_dma.vaddr, BCM_PAGE_SIZE);
19140315881Sdavidcs		}
19141315881Sdavidcs		if(sc->gz_buf_dma.vaddr != NULL) {
19142315881Sdavidcs    		BLOGI(sc, "fw_buf paddr %#jx vaddr %p size 0x%x\n",
19143315881Sdavidcs        			(uintmax_t)sc->gz_buf_dma.paddr, sc->gz_buf_dma.vaddr,
19144315881Sdavidcs        			FW_BUF_SIZE);
19145315881Sdavidcs		}
19146315881Sdavidcs    	for (i = 0; i < sc->num_queues; i++) {
19147315881Sdavidcs        	fp = &sc->fp[i];
19148315881Sdavidcs			if(fp->sb_dma.vaddr != NULL && fp->tx_dma.vaddr != NULL &&
19149315881Sdavidcs                        fp->rx_dma.vaddr != NULL && fp->rcq_dma.vaddr != NULL &&
19150315881Sdavidcs                        fp->rx_sge_dma.vaddr != NULL) {
19151298294Sdavidcs
19152315881Sdavidcs				BLOGI(sc, "FP status block fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
19153315881Sdavidcs            			(uintmax_t)fp->sb_dma.paddr, fp->sb_dma.vaddr,
19154315881Sdavidcs            			sizeof(union bxe_host_hc_status_block));
19155315881Sdavidcs				BLOGI(sc, "TX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
19156315881Sdavidcs            			(uintmax_t)fp->tx_dma.paddr, fp->tx_dma.vaddr,
19157315881Sdavidcs            			(BCM_PAGE_SIZE * TX_BD_NUM_PAGES));
19158315881Sdavidcs        		BLOGI(sc, "RX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
19159315881Sdavidcs            			(uintmax_t)fp->rx_dma.paddr, fp->rx_dma.vaddr,
19160315881Sdavidcs            			(BCM_PAGE_SIZE * RX_BD_NUM_PAGES));
19161315881Sdavidcs        		BLOGI(sc, "RX RCQ CHAIN fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
19162315881Sdavidcs            			(uintmax_t)fp->rcq_dma.paddr, fp->rcq_dma.vaddr,
19163315881Sdavidcs            			(BCM_PAGE_SIZE * RCQ_NUM_PAGES));
19164315881Sdavidcs        		BLOGI(sc, "RX SGE CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
19165315881Sdavidcs            			(uintmax_t)fp->rx_sge_dma.paddr, fp->rx_sge_dma.vaddr,
19166315881Sdavidcs            			(BCM_PAGE_SIZE * RX_SGE_NUM_PAGES));
19167315881Sdavidcs    		}
19168315881Sdavidcs		}
19169315881Sdavidcs		if(ilt != NULL ) {
19170315881Sdavidcs    		ilt_cli = &ilt->clients[1];
19171315881Sdavidcs			if(ilt->lines != NULL) {
19172315881Sdavidcs    		for (i = ilt_cli->start; i <= ilt_cli->end; i++) {
19173315881Sdavidcs        		BLOGI(sc, "ECORE_ILT paddr %#jx vaddr %p size 0x%x\n",
19174315881Sdavidcs            			(uintmax_t)(((struct bxe_dma *)((&ilt->lines[i])->page))->paddr),
19175315881Sdavidcs            			((struct bxe_dma *)((&ilt->lines[i])->page))->vaddr, BCM_PAGE_SIZE);
19176315881Sdavidcs    		}
19177315881Sdavidcs			}
19178315881Sdavidcs		}
19179298294Sdavidcs
19180298294Sdavidcs
19181315881Sdavidcs    	cmd_offset = DMAE_REG_CMD_MEM;
19182315881Sdavidcs    	for (i = 0; i < 224; i++) {
19183315881Sdavidcs        	reg_addr = (cmd_offset +(i * 4));
19184315881Sdavidcs        	reg_val = REG_RD(sc, reg_addr);
19185315881Sdavidcs        	BLOGI(sc, "DMAE_REG_CMD_MEM i=%d reg_addr 0x%x reg_val 0x%08x\n",i,
19186315881Sdavidcs            			reg_addr, reg_val);
19187315881Sdavidcs    	}
19188315881Sdavidcs	}
19189298294Sdavidcs
19190298294Sdavidcs    BLOGI(sc, "Collection of grcdump done\n");
19191292639Sdavidcs    sc->grcdump_done = 1;
19192292639Sdavidcs    return(rval);
19193292639Sdavidcs}
19194292639Sdavidcs
19195292639Sdavidcsstatic int
19196292639Sdavidcsbxe_add_cdev(struct bxe_softc *sc)
19197292639Sdavidcs{
19198297884Sdavidcs    sc->eeprom = malloc(BXE_EEPROM_MAX_DATA_LEN, M_DEVBUF, M_NOWAIT);
19199297884Sdavidcs
19200297884Sdavidcs    if (sc->eeprom == NULL) {
19201297884Sdavidcs        BLOGW(sc, "Unable to alloc for eeprom size buffer\n");
19202297884Sdavidcs        return (-1);
19203297884Sdavidcs    }
19204297884Sdavidcs
19205292639Sdavidcs    sc->ioctl_dev = make_dev(&bxe_cdevsw,
19206292639Sdavidcs                            sc->ifp->if_dunit,
19207292639Sdavidcs                            UID_ROOT,
19208292639Sdavidcs                            GID_WHEEL,
19209292639Sdavidcs                            0600,
19210292639Sdavidcs                            "%s",
19211292639Sdavidcs                            if_name(sc->ifp));
19212292639Sdavidcs
19213292639Sdavidcs    if (sc->ioctl_dev == NULL) {
19214297884Sdavidcs        free(sc->eeprom, M_DEVBUF);
19215297884Sdavidcs        sc->eeprom = NULL;
19216292639Sdavidcs        return (-1);
19217292639Sdavidcs    }
19218292639Sdavidcs
19219292639Sdavidcs    sc->ioctl_dev->si_drv1 = sc;
19220292639Sdavidcs
19221292639Sdavidcs    return (0);
19222292639Sdavidcs}
19223292639Sdavidcs
19224292639Sdavidcsstatic void
19225292639Sdavidcsbxe_del_cdev(struct bxe_softc *sc)
19226292639Sdavidcs{
19227292639Sdavidcs    if (sc->ioctl_dev != NULL)
19228292639Sdavidcs        destroy_dev(sc->ioctl_dev);
19229292639Sdavidcs
19230297884Sdavidcs    if (sc->eeprom != NULL) {
19231297884Sdavidcs        free(sc->eeprom, M_DEVBUF);
19232297884Sdavidcs        sc->eeprom = NULL;
19233297884Sdavidcs    }
19234298294Sdavidcs    sc->ioctl_dev = NULL;
19235297884Sdavidcs
19236292639Sdavidcs    return;
19237292639Sdavidcs}
19238292639Sdavidcs
19239297884Sdavidcsstatic bool bxe_is_nvram_accessible(struct bxe_softc *sc)
19240297884Sdavidcs{
19241297884Sdavidcs
19242297884Sdavidcs    if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
19243297884Sdavidcs        return FALSE;
19244297884Sdavidcs
19245297884Sdavidcs    return TRUE;
19246297884Sdavidcs}
19247297884Sdavidcs
19248297884Sdavidcs
19249292639Sdavidcsstatic int
19250297884Sdavidcsbxe_wr_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
19251297884Sdavidcs{
19252297884Sdavidcs    int rval = 0;
19253297884Sdavidcs
19254297884Sdavidcs    if(!bxe_is_nvram_accessible(sc)) {
19255297884Sdavidcs        BLOGW(sc, "Cannot access eeprom when interface is down\n");
19256297884Sdavidcs        return (-EAGAIN);
19257297884Sdavidcs    }
19258297884Sdavidcs    rval = bxe_nvram_write(sc, offset, (uint8_t *)data, len);
19259297884Sdavidcs
19260297884Sdavidcs
19261297884Sdavidcs   return (rval);
19262297884Sdavidcs}
19263297884Sdavidcs
19264297884Sdavidcsstatic int
19265297884Sdavidcsbxe_rd_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
19266297884Sdavidcs{
19267297884Sdavidcs    int rval = 0;
19268297884Sdavidcs
19269297884Sdavidcs    if(!bxe_is_nvram_accessible(sc)) {
19270297884Sdavidcs        BLOGW(sc, "Cannot access eeprom when interface is down\n");
19271297884Sdavidcs        return (-EAGAIN);
19272297884Sdavidcs    }
19273297884Sdavidcs    rval = bxe_nvram_read(sc, offset, (uint8_t *)data, len);
19274297884Sdavidcs
19275297884Sdavidcs   return (rval);
19276297884Sdavidcs}
19277297884Sdavidcs
19278297884Sdavidcsstatic int
19279297884Sdavidcsbxe_eeprom_rd_wr(struct bxe_softc *sc, bxe_eeprom_t *eeprom)
19280297884Sdavidcs{
19281297884Sdavidcs    int rval = 0;
19282297884Sdavidcs
19283297884Sdavidcs    switch (eeprom->eeprom_cmd) {
19284297884Sdavidcs
19285297884Sdavidcs    case BXE_EEPROM_CMD_SET_EEPROM:
19286297884Sdavidcs
19287297884Sdavidcs        rval = copyin(eeprom->eeprom_data, sc->eeprom,
19288297884Sdavidcs                       eeprom->eeprom_data_len);
19289297884Sdavidcs
19290297884Sdavidcs        if (rval)
19291297884Sdavidcs            break;
19292297884Sdavidcs
19293297884Sdavidcs        rval = bxe_wr_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
19294297884Sdavidcs                       eeprom->eeprom_data_len);
19295297884Sdavidcs        break;
19296297884Sdavidcs
19297297884Sdavidcs    case BXE_EEPROM_CMD_GET_EEPROM:
19298297884Sdavidcs
19299297884Sdavidcs        rval = bxe_rd_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
19300297884Sdavidcs                       eeprom->eeprom_data_len);
19301297884Sdavidcs
19302297884Sdavidcs        if (rval) {
19303297884Sdavidcs            break;
19304297884Sdavidcs        }
19305297884Sdavidcs
19306297884Sdavidcs        rval = copyout(sc->eeprom, eeprom->eeprom_data,
19307297884Sdavidcs                       eeprom->eeprom_data_len);
19308297884Sdavidcs        break;
19309297884Sdavidcs
19310297884Sdavidcs    default:
19311297884Sdavidcs            rval = EINVAL;
19312297884Sdavidcs            break;
19313297884Sdavidcs    }
19314297884Sdavidcs
19315297884Sdavidcs    if (rval) {
19316297884Sdavidcs        BLOGW(sc, "ioctl cmd %d  failed rval %d\n", eeprom->eeprom_cmd, rval);
19317297884Sdavidcs    }
19318297884Sdavidcs
19319297884Sdavidcs    return (rval);
19320297884Sdavidcs}
19321297884Sdavidcs
19322297884Sdavidcsstatic int
19323297884Sdavidcsbxe_get_settings(struct bxe_softc *sc, bxe_dev_setting_t *dev_p)
19324297884Sdavidcs{
19325297884Sdavidcs    uint32_t ext_phy_config;
19326297884Sdavidcs    int port = SC_PORT(sc);
19327297884Sdavidcs    int cfg_idx = bxe_get_link_cfg_idx(sc);
19328297884Sdavidcs
19329297884Sdavidcs    dev_p->supported = sc->port.supported[cfg_idx] |
19330297884Sdavidcs            (sc->port.supported[cfg_idx ^ 1] &
19331297884Sdavidcs            (ELINK_SUPPORTED_TP | ELINK_SUPPORTED_FIBRE));
19332297884Sdavidcs    dev_p->advertising = sc->port.advertising[cfg_idx];
19333297884Sdavidcs    if(sc->link_params.phy[bxe_get_cur_phy_idx(sc)].media_type ==
19334297884Sdavidcs        ELINK_ETH_PHY_SFP_1G_FIBER) {
19335297884Sdavidcs        dev_p->supported = ~(ELINK_SUPPORTED_10000baseT_Full);
19336297884Sdavidcs        dev_p->advertising &= ~(ADVERTISED_10000baseT_Full);
19337297884Sdavidcs    }
19338297884Sdavidcs    if ((sc->state == BXE_STATE_OPEN) && sc->link_vars.link_up &&
19339297884Sdavidcs        !(sc->flags & BXE_MF_FUNC_DIS)) {
19340297884Sdavidcs        dev_p->duplex = sc->link_vars.duplex;
19341297884Sdavidcs        if (IS_MF(sc) && !BXE_NOMCP(sc))
19342297884Sdavidcs            dev_p->speed = bxe_get_mf_speed(sc);
19343297884Sdavidcs        else
19344297884Sdavidcs            dev_p->speed = sc->link_vars.line_speed;
19345297884Sdavidcs    } else {
19346297884Sdavidcs        dev_p->duplex = DUPLEX_UNKNOWN;
19347297884Sdavidcs        dev_p->speed = SPEED_UNKNOWN;
19348297884Sdavidcs    }
19349297884Sdavidcs
19350297884Sdavidcs    dev_p->port = bxe_media_detect(sc);
19351297884Sdavidcs
19352297884Sdavidcs    ext_phy_config = SHMEM_RD(sc,
19353297884Sdavidcs                         dev_info.port_hw_config[port].external_phy_config);
19354297884Sdavidcs    if((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) ==
19355297884Sdavidcs        PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
19356297884Sdavidcs        dev_p->phy_address =  sc->port.phy_addr;
19357297884Sdavidcs    else if(((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
19358297884Sdavidcs            PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
19359297884Sdavidcs        ((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
19360297884Sdavidcs            PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
19361297884Sdavidcs        dev_p->phy_address = ELINK_XGXS_EXT_PHY_ADDR(ext_phy_config);
19362297884Sdavidcs    else
19363297884Sdavidcs        dev_p->phy_address = 0;
19364297884Sdavidcs
19365297884Sdavidcs    if(sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG)
19366297884Sdavidcs        dev_p->autoneg = AUTONEG_ENABLE;
19367297884Sdavidcs    else
19368297884Sdavidcs       dev_p->autoneg = AUTONEG_DISABLE;
19369297884Sdavidcs
19370297884Sdavidcs
19371297884Sdavidcs    return 0;
19372297884Sdavidcs}
19373297884Sdavidcs
19374297884Sdavidcsstatic int
19375292639Sdavidcsbxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
19376292639Sdavidcs        struct thread *td)
19377292639Sdavidcs{
19378292639Sdavidcs    struct bxe_softc    *sc;
19379292639Sdavidcs    int                 rval = 0;
19380292639Sdavidcs    device_t            pci_dev;
19381292639Sdavidcs    bxe_grcdump_t       *dump = NULL;
19382292639Sdavidcs    int grc_dump_size;
19383297884Sdavidcs    bxe_drvinfo_t   *drv_infop = NULL;
19384297884Sdavidcs    bxe_dev_setting_t  *dev_p;
19385297884Sdavidcs    bxe_dev_setting_t  dev_set;
19386297884Sdavidcs    bxe_get_regs_t  *reg_p;
19387297884Sdavidcs    bxe_reg_rdw_t *reg_rdw_p;
19388297884Sdavidcs    bxe_pcicfg_rdw_t *cfg_rdw_p;
19389297884Sdavidcs    bxe_perm_mac_addr_t *mac_addr_p;
19390292639Sdavidcs
19391297884Sdavidcs
19392292639Sdavidcs    if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
19393292639Sdavidcs        return ENXIO;
19394292639Sdavidcs
19395292639Sdavidcs    pci_dev= sc->dev;
19396292639Sdavidcs
19397292639Sdavidcs    dump = (bxe_grcdump_t *)data;
19398292639Sdavidcs
19399292639Sdavidcs    switch(cmd) {
19400292639Sdavidcs
19401292639Sdavidcs        case BXE_GRC_DUMP_SIZE:
19402292639Sdavidcs            dump->pci_func = sc->pcie_func;
19403297884Sdavidcs            dump->grcdump_size =
19404297884Sdavidcs                (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19405297884Sdavidcs                     sizeof(struct  dump_header);
19406292639Sdavidcs            break;
19407292639Sdavidcs
19408292639Sdavidcs        case BXE_GRC_DUMP:
19409292639Sdavidcs
19410292639Sdavidcs            grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19411297884Sdavidcs                                sizeof(struct  dump_header);
19412298294Sdavidcs            if ((!sc->trigger_grcdump) || (dump->grcdump == NULL) ||
19413298294Sdavidcs                (dump->grcdump_size < grc_dump_size)) {
19414292639Sdavidcs                rval = EINVAL;
19415292639Sdavidcs                break;
19416292639Sdavidcs            }
19417292639Sdavidcs
19418298294Sdavidcs            if((sc->trigger_grcdump) && (!sc->grcdump_done) &&
19419298294Sdavidcs                (!sc->grcdump_started)) {
19420298294Sdavidcs                rval =  bxe_grc_dump(sc);
19421298294Sdavidcs            }
19422298294Sdavidcs
19423298294Sdavidcs            if((!rval) && (sc->grcdump_done) && (sc->grcdump_started) &&
19424298294Sdavidcs                (sc->grc_dump != NULL))  {
19425298294Sdavidcs                dump->grcdump_dwords = grc_dump_size >> 2;
19426298294Sdavidcs                rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
19427298294Sdavidcs                free(sc->grc_dump, M_DEVBUF);
19428298294Sdavidcs                sc->grc_dump = NULL;
19429298294Sdavidcs                sc->grcdump_started = 0;
19430298294Sdavidcs                sc->grcdump_done = 0;
19431298294Sdavidcs            }
19432298294Sdavidcs
19433292639Sdavidcs            break;
19434292639Sdavidcs
19435297884Sdavidcs        case BXE_DRV_INFO:
19436297884Sdavidcs            drv_infop = (bxe_drvinfo_t *)data;
19437297884Sdavidcs            snprintf(drv_infop->drv_name, BXE_DRV_NAME_LENGTH, "%s", "bxe");
19438297884Sdavidcs            snprintf(drv_infop->drv_version, BXE_DRV_VERSION_LENGTH, "v:%s",
19439297884Sdavidcs                BXE_DRIVER_VERSION);
19440297884Sdavidcs            snprintf(drv_infop->mfw_version, BXE_MFW_VERSION_LENGTH, "%s",
19441297884Sdavidcs                sc->devinfo.bc_ver_str);
19442297884Sdavidcs            snprintf(drv_infop->stormfw_version, BXE_STORMFW_VERSION_LENGTH,
19443297884Sdavidcs                "%s", sc->fw_ver_str);
19444297884Sdavidcs            drv_infop->eeprom_dump_len = sc->devinfo.flash_size;
19445297884Sdavidcs            drv_infop->reg_dump_len =
19446297884Sdavidcs                (bxe_get_total_regs_len32(sc) * sizeof(uint32_t))
19447297884Sdavidcs                    + sizeof(struct  dump_header);
19448297884Sdavidcs            snprintf(drv_infop->bus_info, BXE_BUS_INFO_LENGTH, "%d:%d:%d",
19449297884Sdavidcs                sc->pcie_bus, sc->pcie_device, sc->pcie_func);
19450297884Sdavidcs            break;
19451298294Sdavidcs
19452297884Sdavidcs        case BXE_DEV_SETTING:
19453297884Sdavidcs            dev_p = (bxe_dev_setting_t *)data;
19454297884Sdavidcs            bxe_get_settings(sc, &dev_set);
19455297884Sdavidcs            dev_p->supported = dev_set.supported;
19456297884Sdavidcs            dev_p->advertising = dev_set.advertising;
19457297884Sdavidcs            dev_p->speed = dev_set.speed;
19458297884Sdavidcs            dev_p->duplex = dev_set.duplex;
19459297884Sdavidcs            dev_p->port = dev_set.port;
19460297884Sdavidcs            dev_p->phy_address = dev_set.phy_address;
19461297884Sdavidcs            dev_p->autoneg = dev_set.autoneg;
19462297884Sdavidcs
19463297884Sdavidcs            break;
19464297884Sdavidcs
19465297884Sdavidcs        case BXE_GET_REGS:
19466297884Sdavidcs
19467297884Sdavidcs            reg_p = (bxe_get_regs_t *)data;
19468297884Sdavidcs            grc_dump_size = reg_p->reg_buf_len;
19469297884Sdavidcs
19470298294Sdavidcs            if((!sc->grcdump_done) && (!sc->grcdump_started)) {
19471297884Sdavidcs                bxe_grc_dump(sc);
19472297884Sdavidcs            }
19473298294Sdavidcs            if((sc->grcdump_done) && (sc->grcdump_started) &&
19474298294Sdavidcs                (sc->grc_dump != NULL))  {
19475297884Sdavidcs                rval = copyout(sc->grc_dump, reg_p->reg_buf, grc_dump_size);
19476298294Sdavidcs                free(sc->grc_dump, M_DEVBUF);
19477298294Sdavidcs                sc->grc_dump = NULL;
19478298294Sdavidcs                sc->grcdump_started = 0;
19479297884Sdavidcs                sc->grcdump_done = 0;
19480297884Sdavidcs            }
19481297884Sdavidcs
19482297884Sdavidcs            break;
19483298294Sdavidcs
19484297884Sdavidcs        case BXE_RDW_REG:
19485297884Sdavidcs            reg_rdw_p = (bxe_reg_rdw_t *)data;
19486297884Sdavidcs            if((reg_rdw_p->reg_cmd == BXE_READ_REG_CMD) &&
19487297884Sdavidcs                (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19488297884Sdavidcs                reg_rdw_p->reg_val = REG_RD(sc, reg_rdw_p->reg_id);
19489297884Sdavidcs
19490297884Sdavidcs            if((reg_rdw_p->reg_cmd == BXE_WRITE_REG_CMD) &&
19491297884Sdavidcs                (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19492297884Sdavidcs                REG_WR(sc, reg_rdw_p->reg_id, reg_rdw_p->reg_val);
19493297884Sdavidcs
19494297884Sdavidcs            break;
19495297884Sdavidcs
19496297884Sdavidcs        case BXE_RDW_PCICFG:
19497297884Sdavidcs            cfg_rdw_p = (bxe_pcicfg_rdw_t *)data;
19498297884Sdavidcs            if(cfg_rdw_p->cfg_cmd == BXE_READ_PCICFG) {
19499297884Sdavidcs
19500297884Sdavidcs                cfg_rdw_p->cfg_val = pci_read_config(sc->dev, cfg_rdw_p->cfg_id,
19501297884Sdavidcs                                         cfg_rdw_p->cfg_width);
19502297884Sdavidcs
19503297884Sdavidcs            } else if(cfg_rdw_p->cfg_cmd == BXE_WRITE_PCICFG) {
19504297884Sdavidcs                pci_write_config(sc->dev, cfg_rdw_p->cfg_id, cfg_rdw_p->cfg_val,
19505297884Sdavidcs                            cfg_rdw_p->cfg_width);
19506297884Sdavidcs            } else {
19507297884Sdavidcs                BLOGW(sc, "BXE_RDW_PCICFG ioctl wrong cmd passed\n");
19508297884Sdavidcs            }
19509297884Sdavidcs            break;
19510297884Sdavidcs
19511297884Sdavidcs        case BXE_MAC_ADDR:
19512297884Sdavidcs            mac_addr_p = (bxe_perm_mac_addr_t *)data;
19513297884Sdavidcs            snprintf(mac_addr_p->mac_addr_str, sizeof(sc->mac_addr_str), "%s",
19514297884Sdavidcs                sc->mac_addr_str);
19515297884Sdavidcs            break;
19516297884Sdavidcs
19517297884Sdavidcs        case BXE_EEPROM:
19518297884Sdavidcs            rval = bxe_eeprom_rd_wr(sc, (bxe_eeprom_t *)data);
19519297884Sdavidcs            break;
19520297884Sdavidcs
19521297884Sdavidcs
19522292639Sdavidcs        default:
19523292639Sdavidcs            break;
19524292639Sdavidcs    }
19525292639Sdavidcs
19526292639Sdavidcs    return (rval);
19527292639Sdavidcs}
19528