bxe.c revision 298955
1255736Sdavidch/*-
2265411Sdavidcs * Copyright (c) 2007-2014 QLogic Corporation. All rights reserved.
3255736Sdavidch *
4255736Sdavidch * Redistribution and use in source and binary forms, with or without
5255736Sdavidch * modification, are permitted provided that the following conditions
6255736Sdavidch * are met:
7255736Sdavidch *
8255736Sdavidch * 1. Redistributions of source code must retain the above copyright
9255736Sdavidch *    notice, this list of conditions and the following disclaimer.
10255736Sdavidch * 2. Redistributions in binary form must reproduce the above copyright
11255736Sdavidch *    notice, this list of conditions and the following disclaimer in the
12255736Sdavidch *    documentation and/or other materials provided with the distribution.
13255736Sdavidch *
14255736Sdavidch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
15255736Sdavidch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16255736Sdavidch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17255736Sdavidch * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
18255736Sdavidch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19255736Sdavidch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20255736Sdavidch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21255736Sdavidch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22255736Sdavidch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23255736Sdavidch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24255736Sdavidch * THE POSSIBILITY OF SUCH DAMAGE.
25255736Sdavidch */
26255736Sdavidch
27255736Sdavidch#include <sys/cdefs.h>
28255736Sdavidch__FBSDID("$FreeBSD: head/sys/dev/bxe/bxe.c 298955 2016-05-03 03:41:25Z pfg $");
29255736Sdavidch
30296071Sdavidcs#define BXE_DRIVER_VERSION "1.78.81"
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    {
170255736Sdavidch        BRCM_VENDORID,
171255736Sdavidch        CHIP_NUM_57840_MF,
172255736Sdavidch        PCI_ANY_ID, PCI_ANY_ID,
173265703Sdavidcs        "QLogic NetXtreme II BCM57840 MF 10GbE"
174255736Sdavidch    },
175255736Sdavidch    {
176255736Sdavidch        0, 0, 0, 0, NULL
177255736Sdavidch    }
178255736Sdavidch};
179255736Sdavidch
180255736SdavidchMALLOC_DECLARE(M_BXE_ILT);
181255736SdavidchMALLOC_DEFINE(M_BXE_ILT, "bxe_ilt", "bxe ILT pointer");
182255736Sdavidch
183255736Sdavidch/*
184255736Sdavidch * FreeBSD device entry points.
185255736Sdavidch */
186255736Sdavidchstatic int bxe_probe(device_t);
187255736Sdavidchstatic int bxe_attach(device_t);
188255736Sdavidchstatic int bxe_detach(device_t);
189255736Sdavidchstatic int bxe_shutdown(device_t);
190255736Sdavidch
191255736Sdavidch/*
192255736Sdavidch * FreeBSD KLD module/device interface event handler method.
193255736Sdavidch */
194255736Sdavidchstatic device_method_t bxe_methods[] = {
195255736Sdavidch    /* Device interface (device_if.h) */
196255736Sdavidch    DEVMETHOD(device_probe,     bxe_probe),
197255736Sdavidch    DEVMETHOD(device_attach,    bxe_attach),
198255736Sdavidch    DEVMETHOD(device_detach,    bxe_detach),
199255736Sdavidch    DEVMETHOD(device_shutdown,  bxe_shutdown),
200255736Sdavidch    /* Bus interface (bus_if.h) */
201255736Sdavidch    DEVMETHOD(bus_print_child,  bus_generic_print_child),
202255736Sdavidch    DEVMETHOD(bus_driver_added, bus_generic_driver_added),
203255736Sdavidch    KOBJMETHOD_END
204255736Sdavidch};
205255736Sdavidch
206255736Sdavidch/*
207255736Sdavidch * FreeBSD KLD Module data declaration
208255736Sdavidch */
209255736Sdavidchstatic driver_t bxe_driver = {
210255736Sdavidch    "bxe",                   /* module name */
211255736Sdavidch    bxe_methods,             /* event handler */
212255736Sdavidch    sizeof(struct bxe_softc) /* extra data */
213255736Sdavidch};
214255736Sdavidch
215255736Sdavidch/*
216255736Sdavidch * FreeBSD dev class is needed to manage dev instances and
217255736Sdavidch * to associate with a bus type
218255736Sdavidch */
219255736Sdavidchstatic devclass_t bxe_devclass;
220255736Sdavidch
221255736SdavidchMODULE_DEPEND(bxe, pci, 1, 1, 1);
222255736SdavidchMODULE_DEPEND(bxe, ether, 1, 1, 1);
223255736SdavidchDRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
224255736Sdavidch
225255736Sdavidch/* resources needed for unloading a previously loaded device */
226255736Sdavidch
227255736Sdavidch#define BXE_PREV_WAIT_NEEDED 1
228255736Sdavidchstruct mtx bxe_prev_mtx;
229255736SdavidchMTX_SYSINIT(bxe_prev_mtx, &bxe_prev_mtx, "bxe_prev_lock", MTX_DEF);
230255736Sdavidchstruct bxe_prev_list_node {
231255736Sdavidch    LIST_ENTRY(bxe_prev_list_node) node;
232255736Sdavidch    uint8_t bus;
233255736Sdavidch    uint8_t slot;
234255736Sdavidch    uint8_t path;
235255736Sdavidch    uint8_t aer; /* XXX automatic error recovery */
236255736Sdavidch    uint8_t undi;
237255736Sdavidch};
238255736Sdavidchstatic LIST_HEAD(, bxe_prev_list_node) bxe_prev_list = LIST_HEAD_INITIALIZER(bxe_prev_list);
239255736Sdavidch
240255736Sdavidchstatic int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
241255736Sdavidch
242255736Sdavidch/* Tunable device values... */
243255736Sdavidch
244255736SdavidchSYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
245255736Sdavidch
246255736Sdavidch/* Debug */
247258187Sedavisunsigned long bxe_debug = 0;
248267992ShselaskySYSCTL_ULONG(_hw_bxe, OID_AUTO, debug, CTLFLAG_RDTUN,
249258187Sedavis             &bxe_debug, 0, "Debug logging mode");
250255736Sdavidch
251255736Sdavidch/* Interrupt Mode: 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
252255736Sdavidchstatic int bxe_interrupt_mode = INTR_MODE_MSIX;
253258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, interrupt_mode, CTLFLAG_RDTUN,
254258187Sedavis           &bxe_interrupt_mode, 0, "Interrupt (MSI-X/MSI/INTx) mode");
255255736Sdavidch
256255736Sdavidch/* Number of Queues: 0 (Auto) or 1 to 16 (fixed queue number) */
257255736Sdavidchstatic int bxe_queue_count = 4;
258258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN,
259258187Sedavis           &bxe_queue_count, 0, "Multi-Queue queue count");
260255736Sdavidch
261255736Sdavidch/* max number of buffers per queue (default RX_BD_USABLE) */
262258187Sedavisstatic int bxe_max_rx_bufs = 0;
263258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_rx_bufs, CTLFLAG_RDTUN,
264258187Sedavis           &bxe_max_rx_bufs, 0, "Maximum Number of Rx Buffers Per Queue");
265255736Sdavidch
266255736Sdavidch/* Host interrupt coalescing RX tick timer (usecs) */
267258187Sedavisstatic int bxe_hc_rx_ticks = 25;
268258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_rx_ticks, CTLFLAG_RDTUN,
269258187Sedavis           &bxe_hc_rx_ticks, 0, "Host Coalescing Rx ticks");
270255736Sdavidch
271255736Sdavidch/* Host interrupt coalescing TX tick timer (usecs) */
272258187Sedavisstatic int bxe_hc_tx_ticks = 50;
273258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, hc_tx_ticks, CTLFLAG_RDTUN,
274258187Sedavis           &bxe_hc_tx_ticks, 0, "Host Coalescing Tx ticks");
275255736Sdavidch
276255736Sdavidch/* Maximum number of Rx packets to process at a time */
277258187Sedavisstatic int bxe_rx_budget = 0xffffffff;
278258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, rx_budget, CTLFLAG_TUN,
279258187Sedavis           &bxe_rx_budget, 0, "Rx processing budget");
280255736Sdavidch
281255736Sdavidch/* Maximum LRO aggregation size */
282258187Sedavisstatic int bxe_max_aggregation_size = 0;
283258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, max_aggregation_size, CTLFLAG_TUN,
284258187Sedavis           &bxe_max_aggregation_size, 0, "max aggregation size");
285255736Sdavidch
286255736Sdavidch/* PCI MRRS: -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB) */
287255736Sdavidchstatic int bxe_mrrs = -1;
288258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN,
289258187Sedavis           &bxe_mrrs, 0, "PCIe maximum read request size");
290255736Sdavidch
291255736Sdavidch/* AutoGrEEEn: 0 (hardware default), 1 (force on), 2 (force off) */
292255736Sdavidchstatic int bxe_autogreeen = 0;
293258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, autogreeen, CTLFLAG_RDTUN,
294258187Sedavis           &bxe_autogreeen, 0, "AutoGrEEEn support");
295255736Sdavidch
296255736Sdavidch/* 4-tuple RSS support for UDP: 0 (disabled), 1 (enabled) */
297255736Sdavidchstatic int bxe_udp_rss = 0;
298258187SedavisSYSCTL_INT(_hw_bxe, OID_AUTO, udp_rss, CTLFLAG_RDTUN,
299258187Sedavis           &bxe_udp_rss, 0, "UDP RSS support");
300255736Sdavidch
301255736Sdavidch
302255736Sdavidch#define STAT_NAME_LEN 32 /* no stat names below can be longer than this */
303255736Sdavidch
304255736Sdavidch#define STATS_OFFSET32(stat_name)                   \
305255736Sdavidch    (offsetof(struct bxe_eth_stats, stat_name) / 4)
306255736Sdavidch
307255736Sdavidch#define Q_STATS_OFFSET32(stat_name)                   \
308255736Sdavidch    (offsetof(struct bxe_eth_q_stats, stat_name) / 4)
309255736Sdavidch
310255736Sdavidchstatic const struct {
311255736Sdavidch    uint32_t offset;
312255736Sdavidch    uint32_t size;
313255736Sdavidch    uint32_t flags;
314255736Sdavidch#define STATS_FLAGS_PORT  1
315255736Sdavidch#define STATS_FLAGS_FUNC  2 /* MF only cares about function stats */
316255736Sdavidch#define STATS_FLAGS_BOTH  (STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
317255736Sdavidch    char string[STAT_NAME_LEN];
318255736Sdavidch} bxe_eth_stats_arr[] = {
319255736Sdavidch    { STATS_OFFSET32(total_bytes_received_hi),
320255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bytes" },
321255736Sdavidch    { STATS_OFFSET32(error_bytes_received_hi),
322255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_error_bytes" },
323255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_received_hi),
324255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
325255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_received_hi),
326255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
327255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_received_hi),
328255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
329255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
330255736Sdavidch                8, STATS_FLAGS_PORT, "rx_crc_errors" },
331255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
332255736Sdavidch                8, STATS_FLAGS_PORT, "rx_align_errors" },
333255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
334255736Sdavidch                8, STATS_FLAGS_PORT, "rx_undersize_packets" },
335255736Sdavidch    { STATS_OFFSET32(etherstatsoverrsizepkts_hi),
336255736Sdavidch                8, STATS_FLAGS_PORT, "rx_oversize_packets" },
337255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
338255736Sdavidch                8, STATS_FLAGS_PORT, "rx_fragments" },
339255736Sdavidch    { STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
340255736Sdavidch                8, STATS_FLAGS_PORT, "rx_jabbers" },
341255736Sdavidch    { STATS_OFFSET32(no_buff_discard_hi),
342255736Sdavidch                8, STATS_FLAGS_BOTH, "rx_discards" },
343255736Sdavidch    { STATS_OFFSET32(mac_filter_discard),
344255736Sdavidch                4, STATS_FLAGS_PORT, "rx_filtered_packets" },
345255736Sdavidch    { STATS_OFFSET32(mf_tag_discard),
346255736Sdavidch                4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
347255736Sdavidch    { STATS_OFFSET32(pfc_frames_received_hi),
348255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_received" },
349255736Sdavidch    { STATS_OFFSET32(pfc_frames_sent_hi),
350255736Sdavidch                8, STATS_FLAGS_PORT, "pfc_frames_sent" },
351255736Sdavidch    { STATS_OFFSET32(brb_drop_hi),
352255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_discard" },
353255736Sdavidch    { STATS_OFFSET32(brb_truncate_hi),
354255736Sdavidch                8, STATS_FLAGS_PORT, "rx_brb_truncate" },
355255736Sdavidch    { STATS_OFFSET32(pause_frames_received_hi),
356255736Sdavidch                8, STATS_FLAGS_PORT, "rx_pause_frames" },
357255736Sdavidch    { STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
358255736Sdavidch                8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
359255736Sdavidch    { STATS_OFFSET32(nig_timer_max),
360255736Sdavidch                4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
361255736Sdavidch    { STATS_OFFSET32(total_bytes_transmitted_hi),
362255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bytes" },
363255736Sdavidch    { STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
364255736Sdavidch                8, STATS_FLAGS_PORT, "tx_error_bytes" },
365255736Sdavidch    { STATS_OFFSET32(total_unicast_packets_transmitted_hi),
366255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
367255736Sdavidch    { STATS_OFFSET32(total_multicast_packets_transmitted_hi),
368255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
369255736Sdavidch    { STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
370255736Sdavidch                8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
371255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
372255736Sdavidch                8, STATS_FLAGS_PORT, "tx_mac_errors" },
373255736Sdavidch    { STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
374255736Sdavidch                8, STATS_FLAGS_PORT, "tx_carrier_errors" },
375255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
376255736Sdavidch                8, STATS_FLAGS_PORT, "tx_single_collisions" },
377255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
378255736Sdavidch                8, STATS_FLAGS_PORT, "tx_multi_collisions" },
379255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
380255736Sdavidch                8, STATS_FLAGS_PORT, "tx_deferred" },
381255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
382255736Sdavidch                8, STATS_FLAGS_PORT, "tx_excess_collisions" },
383255736Sdavidch    { STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
384255736Sdavidch                8, STATS_FLAGS_PORT, "tx_late_collisions" },
385255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
386255736Sdavidch                8, STATS_FLAGS_PORT, "tx_total_collisions" },
387255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
388255736Sdavidch                8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
389255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
390255736Sdavidch                8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
391255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
392255736Sdavidch                8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
393255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
394255736Sdavidch                8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
395255736Sdavidch    { STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
396255736Sdavidch                8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
397255736Sdavidch    { STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
398255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
399255736Sdavidch    { STATS_OFFSET32(etherstatspktsover1522octets_hi),
400255736Sdavidch                8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
401255736Sdavidch    { STATS_OFFSET32(pause_frames_sent_hi),
402255736Sdavidch                8, STATS_FLAGS_PORT, "tx_pause_frames" },
403255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregations_hi),
404255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregations" },
405255736Sdavidch    { STATS_OFFSET32(total_tpa_aggregated_frames_hi),
406255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
407255736Sdavidch    { STATS_OFFSET32(total_tpa_bytes_hi),
408255736Sdavidch                8, STATS_FLAGS_FUNC, "tpa_bytes"},
409255736Sdavidch    { STATS_OFFSET32(eee_tx_lpi),
410255736Sdavidch                4, STATS_FLAGS_PORT, "eee_tx_lpi"},
411255736Sdavidch    { STATS_OFFSET32(rx_calls),
412255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_calls"},
413255736Sdavidch    { STATS_OFFSET32(rx_pkts),
414255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_pkts"},
415255736Sdavidch    { STATS_OFFSET32(rx_tpa_pkts),
416255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_tpa_pkts"},
417292638Sdavidcs    { STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
418292638Sdavidcs                4, STATS_FLAGS_FUNC, "rx_erroneous_jumbo_sge_pkts"},
419292638Sdavidcs    { STATS_OFFSET32(rx_bxe_service_rxsgl),
420292638Sdavidcs                4, STATS_FLAGS_FUNC, "rx_bxe_service_rxsgl"},
421283274Sdavidcs    { STATS_OFFSET32(rx_jumbo_sge_pkts),
422283274Sdavidcs                4, STATS_FLAGS_FUNC, "rx_jumbo_sge_pkts"},
423255736Sdavidch    { STATS_OFFSET32(rx_soft_errors),
424255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_soft_errors"},
425255736Sdavidch    { STATS_OFFSET32(rx_hw_csum_errors),
426255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_hw_csum_errors"},
427255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_ip),
428255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_ip"},
429255736Sdavidch    { STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
430255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_tcp_udp"},
431255736Sdavidch    { STATS_OFFSET32(rx_budget_reached),
432255736Sdavidch                4, STATS_FLAGS_FUNC, "rx_budget_reached"},
433255736Sdavidch    { STATS_OFFSET32(tx_pkts),
434255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_pkts"},
435255736Sdavidch    { STATS_OFFSET32(tx_soft_errors),
436255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_soft_errors"},
437255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_ip),
438255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_ip"},
439255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_tcp),
440255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_tcp"},
441255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_csum_udp),
442255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_udp"},
443255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso),
444255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso"},
445255736Sdavidch    { STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
446255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso_hdr_splits"},
447255736Sdavidch    { STATS_OFFSET32(tx_encap_failures),
448255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_encap_failures"},
449255736Sdavidch    { STATS_OFFSET32(tx_hw_queue_full),
450255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_queue_full"},
451255736Sdavidch    { STATS_OFFSET32(tx_hw_max_queue_depth),
452255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_hw_max_queue_depth"},
453255736Sdavidch    { STATS_OFFSET32(tx_dma_mapping_failure),
454255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_dma_mapping_failure"},
455255736Sdavidch    { STATS_OFFSET32(tx_max_drbr_queue_depth),
456255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_max_drbr_queue_depth"},
457255736Sdavidch    { STATS_OFFSET32(tx_window_violation_std),
458255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_std"},
459255736Sdavidch    { STATS_OFFSET32(tx_window_violation_tso),
460255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_window_violation_tso"},
461255736Sdavidch    { STATS_OFFSET32(tx_chain_lost_mbuf),
462255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_chain_lost_mbuf"},
463255736Sdavidch    { STATS_OFFSET32(tx_frames_deferred),
464255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_frames_deferred"},
465255736Sdavidch    { STATS_OFFSET32(tx_queue_xoff),
466255736Sdavidch                4, STATS_FLAGS_FUNC, "tx_queue_xoff"},
467255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_attempts),
468255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_attempts"},
469255736Sdavidch    { STATS_OFFSET32(mbuf_defrag_failures),
470255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_defrag_failures"},
471255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
472255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_alloc_failed"},
473255736Sdavidch    { STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
474255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_bd_mapping_failed"},
475255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
476255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_alloc_failed"},
477255736Sdavidch    { STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
478255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_mapping_failed"},
479255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
480255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_alloc_failed"},
481255736Sdavidch    { STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
482255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_rx_sge_mapping_failed"},
483255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tx),
484255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_tx"},
485255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_rx),
486255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_rx"},
487255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_sge),
488255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_sge"},
489255736Sdavidch    { STATS_OFFSET32(mbuf_alloc_tpa),
490297873Sdavidcs                4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"},
491297873Sdavidcs    { STATS_OFFSET32(tx_queue_full_return),
492297873Sdavidcs                4, STATS_FLAGS_FUNC, "tx_queue_full_return"}
493255736Sdavidch};
494255736Sdavidch
495255736Sdavidchstatic const struct {
496255736Sdavidch    uint32_t offset;
497255736Sdavidch    uint32_t size;
498255736Sdavidch    char string[STAT_NAME_LEN];
499255736Sdavidch} bxe_eth_q_stats_arr[] = {
500255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_received_hi),
501255736Sdavidch                8, "rx_bytes" },
502255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
503255736Sdavidch                8, "rx_ucast_packets" },
504255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
505255736Sdavidch                8, "rx_mcast_packets" },
506255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
507255736Sdavidch                8, "rx_bcast_packets" },
508255736Sdavidch    { Q_STATS_OFFSET32(no_buff_discard_hi),
509255736Sdavidch                8, "rx_discards" },
510255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
511255736Sdavidch                8, "tx_bytes" },
512255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
513255736Sdavidch                8, "tx_ucast_packets" },
514255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
515255736Sdavidch                8, "tx_mcast_packets" },
516255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
517255736Sdavidch                8, "tx_bcast_packets" },
518255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
519255736Sdavidch                8, "tpa_aggregations" },
520255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
521255736Sdavidch                8, "tpa_aggregated_frames"},
522255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_bytes_hi),
523255736Sdavidch                8, "tpa_bytes"},
524255736Sdavidch    { Q_STATS_OFFSET32(rx_calls),
525255736Sdavidch                4, "rx_calls"},
526255736Sdavidch    { Q_STATS_OFFSET32(rx_pkts),
527255736Sdavidch                4, "rx_pkts"},
528255736Sdavidch    { Q_STATS_OFFSET32(rx_tpa_pkts),
529255736Sdavidch                4, "rx_tpa_pkts"},
530292638Sdavidcs    { Q_STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
531292638Sdavidcs                4, "rx_erroneous_jumbo_sge_pkts"},
532292638Sdavidcs    { Q_STATS_OFFSET32(rx_bxe_service_rxsgl),
533292638Sdavidcs                4, "rx_bxe_service_rxsgl"},
534283274Sdavidcs    { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
535283274Sdavidcs                4, "rx_jumbo_sge_pkts"},
536255736Sdavidch    { Q_STATS_OFFSET32(rx_soft_errors),
537255736Sdavidch                4, "rx_soft_errors"},
538255736Sdavidch    { Q_STATS_OFFSET32(rx_hw_csum_errors),
539255736Sdavidch                4, "rx_hw_csum_errors"},
540255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
541255736Sdavidch                4, "rx_ofld_frames_csum_ip"},
542255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
543255736Sdavidch                4, "rx_ofld_frames_csum_tcp_udp"},
544255736Sdavidch    { Q_STATS_OFFSET32(rx_budget_reached),
545255736Sdavidch                4, "rx_budget_reached"},
546255736Sdavidch    { Q_STATS_OFFSET32(tx_pkts),
547255736Sdavidch                4, "tx_pkts"},
548255736Sdavidch    { Q_STATS_OFFSET32(tx_soft_errors),
549255736Sdavidch                4, "tx_soft_errors"},
550255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
551255736Sdavidch                4, "tx_ofld_frames_csum_ip"},
552255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
553255736Sdavidch                4, "tx_ofld_frames_csum_tcp"},
554255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
555255736Sdavidch                4, "tx_ofld_frames_csum_udp"},
556255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso),
557255736Sdavidch                4, "tx_ofld_frames_lso"},
558255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
559255736Sdavidch                4, "tx_ofld_frames_lso_hdr_splits"},
560255736Sdavidch    { Q_STATS_OFFSET32(tx_encap_failures),
561255736Sdavidch                4, "tx_encap_failures"},
562255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_queue_full),
563255736Sdavidch                4, "tx_hw_queue_full"},
564255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
565255736Sdavidch                4, "tx_hw_max_queue_depth"},
566255736Sdavidch    { Q_STATS_OFFSET32(tx_dma_mapping_failure),
567255736Sdavidch                4, "tx_dma_mapping_failure"},
568255736Sdavidch    { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
569255736Sdavidch                4, "tx_max_drbr_queue_depth"},
570255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_std),
571255736Sdavidch                4, "tx_window_violation_std"},
572255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_tso),
573255736Sdavidch                4, "tx_window_violation_tso"},
574255736Sdavidch    { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
575255736Sdavidch                4, "tx_chain_lost_mbuf"},
576255736Sdavidch    { Q_STATS_OFFSET32(tx_frames_deferred),
577255736Sdavidch                4, "tx_frames_deferred"},
578255736Sdavidch    { Q_STATS_OFFSET32(tx_queue_xoff),
579255736Sdavidch                4, "tx_queue_xoff"},
580255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_attempts),
581255736Sdavidch                4, "mbuf_defrag_attempts"},
582255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_failures),
583255736Sdavidch                4, "mbuf_defrag_failures"},
584255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
585255736Sdavidch                4, "mbuf_rx_bd_alloc_failed"},
586255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
587255736Sdavidch                4, "mbuf_rx_bd_mapping_failed"},
588255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
589255736Sdavidch                4, "mbuf_rx_tpa_alloc_failed"},
590255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
591255736Sdavidch                4, "mbuf_rx_tpa_mapping_failed"},
592255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
593255736Sdavidch                4, "mbuf_rx_sge_alloc_failed"},
594255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
595255736Sdavidch                4, "mbuf_rx_sge_mapping_failed"},
596255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tx),
597255736Sdavidch                4, "mbuf_alloc_tx"},
598255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_rx),
599255736Sdavidch                4, "mbuf_alloc_rx"},
600255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_sge),
601255736Sdavidch                4, "mbuf_alloc_sge"},
602255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tpa),
603297873Sdavidcs                4, "mbuf_alloc_tpa"},
604297873Sdavidcs    { Q_STATS_OFFSET32(tx_queue_full_return),
605297873Sdavidcs                4, "tx_queue_full_return"}
606255736Sdavidch};
607255736Sdavidch
608255736Sdavidch#define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
609255736Sdavidch#define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
610255736Sdavidch
611255736Sdavidch
612255736Sdavidchstatic void    bxe_cmng_fns_init(struct bxe_softc *sc,
613255736Sdavidch                                 uint8_t          read_cfg,
614255736Sdavidch                                 uint8_t          cmng_type);
615255736Sdavidchstatic int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
616255736Sdavidchstatic void    storm_memset_cmng(struct bxe_softc *sc,
617255736Sdavidch                                 struct cmng_init *cmng,
618255736Sdavidch                                 uint8_t          port);
619255736Sdavidchstatic void    bxe_set_reset_global(struct bxe_softc *sc);
620255736Sdavidchstatic void    bxe_set_reset_in_progress(struct bxe_softc *sc);
621255736Sdavidchstatic uint8_t bxe_reset_is_done(struct bxe_softc *sc,
622255736Sdavidch                                 int              engine);
623255736Sdavidchstatic uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
624255736Sdavidchstatic uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
625255736Sdavidch                                   uint8_t          *global,
626255736Sdavidch                                   uint8_t          print);
627255736Sdavidchstatic void    bxe_int_disable(struct bxe_softc *sc);
628255736Sdavidchstatic int     bxe_release_leader_lock(struct bxe_softc *sc);
629255736Sdavidchstatic void    bxe_pf_disable(struct bxe_softc *sc);
630255736Sdavidchstatic void    bxe_free_fp_buffers(struct bxe_softc *sc);
631255736Sdavidchstatic inline void bxe_update_rx_prod(struct bxe_softc    *sc,
632255736Sdavidch                                      struct bxe_fastpath *fp,
633255736Sdavidch                                      uint16_t            rx_bd_prod,
634255736Sdavidch                                      uint16_t            rx_cq_prod,
635255736Sdavidch                                      uint16_t            rx_sge_prod);
636255736Sdavidchstatic void    bxe_link_report_locked(struct bxe_softc *sc);
637255736Sdavidchstatic void    bxe_link_report(struct bxe_softc *sc);
638255736Sdavidchstatic void    bxe_link_status_update(struct bxe_softc *sc);
639255736Sdavidchstatic void    bxe_periodic_callout_func(void *xsc);
640255736Sdavidchstatic void    bxe_periodic_start(struct bxe_softc *sc);
641255736Sdavidchstatic void    bxe_periodic_stop(struct bxe_softc *sc);
642255736Sdavidchstatic int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
643255736Sdavidch                                    uint16_t prev_index,
644255736Sdavidch                                    uint16_t index);
645255736Sdavidchstatic int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
646255736Sdavidch                                     int                 queue);
647255736Sdavidchstatic int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
648255736Sdavidch                                     uint16_t            index);
649255736Sdavidchstatic uint8_t bxe_txeof(struct bxe_softc *sc,
650255736Sdavidch                         struct bxe_fastpath *fp);
651255736Sdavidchstatic void    bxe_task_fp(struct bxe_fastpath *fp);
652255736Sdavidchstatic __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
653255736Sdavidch                                     struct mbuf      *m,
654255736Sdavidch                                     uint8_t          contents);
655255736Sdavidchstatic int     bxe_alloc_mem(struct bxe_softc *sc);
656255736Sdavidchstatic void    bxe_free_mem(struct bxe_softc *sc);
657255736Sdavidchstatic int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
658255736Sdavidchstatic void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
659255736Sdavidchstatic int     bxe_interrupt_attach(struct bxe_softc *sc);
660255736Sdavidchstatic void    bxe_interrupt_detach(struct bxe_softc *sc);
661255736Sdavidchstatic void    bxe_set_rx_mode(struct bxe_softc *sc);
662255736Sdavidchstatic int     bxe_init_locked(struct bxe_softc *sc);
663255736Sdavidchstatic int     bxe_stop_locked(struct bxe_softc *sc);
664255736Sdavidchstatic __noinline int bxe_nic_load(struct bxe_softc *sc,
665255736Sdavidch                                   int              load_mode);
666255736Sdavidchstatic __noinline int bxe_nic_unload(struct bxe_softc *sc,
667255736Sdavidch                                     uint32_t         unload_mode,
668255736Sdavidch                                     uint8_t          keep_link);
669255736Sdavidch
670255736Sdavidchstatic void bxe_handle_sp_tq(void *context, int pending);
671255736Sdavidchstatic void bxe_handle_fp_tq(void *context, int pending);
672255736Sdavidch
673292639Sdavidcsstatic int bxe_add_cdev(struct bxe_softc *sc);
674292639Sdavidcsstatic void bxe_del_cdev(struct bxe_softc *sc);
675296579Sdavidcsstatic int bxe_alloc_buf_rings(struct bxe_softc *sc);
676296579Sdavidcsstatic void bxe_free_buf_rings(struct bxe_softc *sc);
677255736Sdavidch
678255736Sdavidch/* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
679255736Sdavidchuint32_t
680255736Sdavidchcalc_crc32(uint8_t  *crc32_packet,
681255736Sdavidch           uint32_t crc32_length,
682255736Sdavidch           uint32_t crc32_seed,
683255736Sdavidch           uint8_t  complement)
684255736Sdavidch{
685255736Sdavidch   uint32_t byte         = 0;
686255736Sdavidch   uint32_t bit          = 0;
687255736Sdavidch   uint8_t  msb          = 0;
688255736Sdavidch   uint32_t temp         = 0;
689255736Sdavidch   uint32_t shft         = 0;
690255736Sdavidch   uint8_t  current_byte = 0;
691255736Sdavidch   uint32_t crc32_result = crc32_seed;
692255736Sdavidch   const uint32_t CRC32_POLY = 0x1edc6f41;
693255736Sdavidch
694255736Sdavidch   if ((crc32_packet == NULL) ||
695255736Sdavidch       (crc32_length == 0) ||
696255736Sdavidch       ((crc32_length % 8) != 0))
697255736Sdavidch    {
698255736Sdavidch        return (crc32_result);
699255736Sdavidch    }
700255736Sdavidch
701255736Sdavidch    for (byte = 0; byte < crc32_length; byte = byte + 1)
702255736Sdavidch    {
703255736Sdavidch        current_byte = crc32_packet[byte];
704255736Sdavidch        for (bit = 0; bit < 8; bit = bit + 1)
705255736Sdavidch        {
706255736Sdavidch            /* msb = crc32_result[31]; */
707255736Sdavidch            msb = (uint8_t)(crc32_result >> 31);
708255736Sdavidch
709255736Sdavidch            crc32_result = crc32_result << 1;
710255736Sdavidch
711255736Sdavidch            /* it (msb != current_byte[bit]) */
712255736Sdavidch            if (msb != (0x1 & (current_byte >> bit)))
713255736Sdavidch            {
714255736Sdavidch                crc32_result = crc32_result ^ CRC32_POLY;
715255736Sdavidch                /* crc32_result[0] = 1 */
716255736Sdavidch                crc32_result |= 1;
717255736Sdavidch            }
718255736Sdavidch        }
719255736Sdavidch    }
720255736Sdavidch
721255736Sdavidch    /* Last step is to:
722255736Sdavidch     * 1. "mirror" every bit
723255736Sdavidch     * 2. swap the 4 bytes
724255736Sdavidch     * 3. complement each bit
725255736Sdavidch     */
726255736Sdavidch
727255736Sdavidch    /* Mirror */
728255736Sdavidch    temp = crc32_result;
729255736Sdavidch    shft = sizeof(crc32_result) * 8 - 1;
730255736Sdavidch
731255736Sdavidch    for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
732255736Sdavidch    {
733255736Sdavidch        temp <<= 1;
734255736Sdavidch        temp |= crc32_result & 1;
735255736Sdavidch        shft-- ;
736255736Sdavidch    }
737255736Sdavidch
738255736Sdavidch    /* temp[31-bit] = crc32_result[bit] */
739255736Sdavidch    temp <<= shft;
740255736Sdavidch
741255736Sdavidch    /* Swap */
742255736Sdavidch    /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
743255736Sdavidch    {
744255736Sdavidch        uint32_t t0, t1, t2, t3;
745255736Sdavidch        t0 = (0x000000ff & (temp >> 24));
746255736Sdavidch        t1 = (0x0000ff00 & (temp >> 8));
747255736Sdavidch        t2 = (0x00ff0000 & (temp << 8));
748255736Sdavidch        t3 = (0xff000000 & (temp << 24));
749255736Sdavidch        crc32_result = t0 | t1 | t2 | t3;
750255736Sdavidch    }
751255736Sdavidch
752255736Sdavidch    /* Complement */
753255736Sdavidch    if (complement)
754255736Sdavidch    {
755255736Sdavidch        crc32_result = ~crc32_result;
756255736Sdavidch    }
757255736Sdavidch
758255736Sdavidch    return (crc32_result);
759255736Sdavidch}
760255736Sdavidch
761255736Sdavidchint
762255736Sdavidchbxe_test_bit(int                    nr,
763255736Sdavidch             volatile unsigned long *addr)
764255736Sdavidch{
765255736Sdavidch    return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
766255736Sdavidch}
767255736Sdavidch
768255736Sdavidchvoid
769255736Sdavidchbxe_set_bit(unsigned int           nr,
770255736Sdavidch            volatile unsigned long *addr)
771255736Sdavidch{
772255736Sdavidch    atomic_set_acq_long(addr, (1 << nr));
773255736Sdavidch}
774255736Sdavidch
775255736Sdavidchvoid
776255736Sdavidchbxe_clear_bit(int                    nr,
777255736Sdavidch              volatile unsigned long *addr)
778255736Sdavidch{
779255736Sdavidch    atomic_clear_acq_long(addr, (1 << nr));
780255736Sdavidch}
781255736Sdavidch
782255736Sdavidchint
783255736Sdavidchbxe_test_and_set_bit(int                    nr,
784255736Sdavidch                       volatile unsigned long *addr)
785255736Sdavidch{
786255736Sdavidch    unsigned long x;
787255736Sdavidch    nr = (1 << nr);
788255736Sdavidch    do {
789255736Sdavidch        x = *addr;
790255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
791255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
792255736Sdavidch    return (x & nr);
793255736Sdavidch}
794255736Sdavidch
795255736Sdavidchint
796255736Sdavidchbxe_test_and_clear_bit(int                    nr,
797255736Sdavidch                       volatile unsigned long *addr)
798255736Sdavidch{
799255736Sdavidch    unsigned long x;
800255736Sdavidch    nr = (1 << nr);
801255736Sdavidch    do {
802255736Sdavidch        x = *addr;
803255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
804255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
805255736Sdavidch    return (x & nr);
806255736Sdavidch}
807255736Sdavidch
808255736Sdavidchint
809255736Sdavidchbxe_cmpxchg(volatile int *addr,
810255736Sdavidch            int          old,
811255736Sdavidch            int          new)
812255736Sdavidch{
813255736Sdavidch    int x;
814255736Sdavidch    do {
815255736Sdavidch        x = *addr;
816255736Sdavidch    } while (atomic_cmpset_acq_int(addr, old, new) == 0);
817255736Sdavidch    return (x);
818255736Sdavidch}
819255736Sdavidch
820255736Sdavidch/*
821255736Sdavidch * Get DMA memory from the OS.
822255736Sdavidch *
823255736Sdavidch * Validates that the OS has provided DMA buffers in response to a
824255736Sdavidch * bus_dmamap_load call and saves the physical address of those buffers.
825255736Sdavidch * When the callback is used the OS will return 0 for the mapping function
826255736Sdavidch * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
827255736Sdavidch * failures back to the caller.
828255736Sdavidch *
829255736Sdavidch * Returns:
830255736Sdavidch *   Nothing.
831255736Sdavidch */
832255736Sdavidchstatic void
833255736Sdavidchbxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
834255736Sdavidch{
835255736Sdavidch    struct bxe_dma *dma = arg;
836255736Sdavidch
837255736Sdavidch    if (error) {
838255736Sdavidch        dma->paddr = 0;
839255736Sdavidch        dma->nseg  = 0;
840255736Sdavidch        BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
841255736Sdavidch    } else {
842255736Sdavidch        dma->paddr = segs->ds_addr;
843255736Sdavidch        dma->nseg  = nseg;
844255736Sdavidch    }
845255736Sdavidch}
846255736Sdavidch
847255736Sdavidch/*
848255736Sdavidch * Allocate a block of memory and map it for DMA. No partial completions
849255736Sdavidch * allowed and release any resources acquired if we can't acquire all
850255736Sdavidch * resources.
851255736Sdavidch *
852255736Sdavidch * Returns:
853255736Sdavidch *   0 = Success, !0 = Failure
854255736Sdavidch */
855255736Sdavidchint
856255736Sdavidchbxe_dma_alloc(struct bxe_softc *sc,
857255736Sdavidch              bus_size_t       size,
858255736Sdavidch              struct bxe_dma   *dma,
859255736Sdavidch              const char       *msg)
860255736Sdavidch{
861255736Sdavidch    int rc;
862255736Sdavidch
863255736Sdavidch    if (dma->size > 0) {
864256299Sedavis        BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
865256299Sedavis              (unsigned long)dma->size);
866255736Sdavidch        return (1);
867255736Sdavidch    }
868255736Sdavidch
869255736Sdavidch    memset(dma, 0, sizeof(*dma)); /* sanity */
870255736Sdavidch    dma->sc   = sc;
871255736Sdavidch    dma->size = size;
872255736Sdavidch    snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
873255736Sdavidch
874255736Sdavidch    rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
875255736Sdavidch                            BCM_PAGE_SIZE,      /* alignment */
876255736Sdavidch                            0,                  /* boundary limit */
877255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted low */
878255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted hi */
879255736Sdavidch                            NULL,               /* addr filter() */
880255736Sdavidch                            NULL,               /* addr filter() arg */
881255736Sdavidch                            size,               /* max map size */
882255736Sdavidch                            1,                  /* num discontinuous */
883255736Sdavidch                            size,               /* max seg size */
884255736Sdavidch                            BUS_DMA_ALLOCNOW,   /* flags */
885255736Sdavidch                            NULL,               /* lock() */
886255736Sdavidch                            NULL,               /* lock() arg */
887255736Sdavidch                            &dma->tag);         /* returned dma tag */
888255736Sdavidch    if (rc != 0) {
889255736Sdavidch        BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
890255736Sdavidch        memset(dma, 0, sizeof(*dma));
891255736Sdavidch        return (1);
892255736Sdavidch    }
893255736Sdavidch
894255736Sdavidch    rc = bus_dmamem_alloc(dma->tag,
895255736Sdavidch                          (void **)&dma->vaddr,
896255736Sdavidch                          (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
897255736Sdavidch                          &dma->map);
898255736Sdavidch    if (rc != 0) {
899255736Sdavidch        BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
900255736Sdavidch        bus_dma_tag_destroy(dma->tag);
901255736Sdavidch        memset(dma, 0, sizeof(*dma));
902255736Sdavidch        return (1);
903255736Sdavidch    }
904255736Sdavidch
905255736Sdavidch    rc = bus_dmamap_load(dma->tag,
906255736Sdavidch                         dma->map,
907255736Sdavidch                         dma->vaddr,
908255736Sdavidch                         size,
909255736Sdavidch                         bxe_dma_map_addr, /* BLOGD in here */
910255736Sdavidch                         dma,
911255736Sdavidch                         BUS_DMA_NOWAIT);
912255736Sdavidch    if (rc != 0) {
913255736Sdavidch        BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
914255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
915255736Sdavidch        bus_dma_tag_destroy(dma->tag);
916255736Sdavidch        memset(dma, 0, sizeof(*dma));
917255736Sdavidch        return (1);
918255736Sdavidch    }
919255736Sdavidch
920255736Sdavidch    return (0);
921255736Sdavidch}
922255736Sdavidch
923255736Sdavidchvoid
924255736Sdavidchbxe_dma_free(struct bxe_softc *sc,
925255736Sdavidch             struct bxe_dma   *dma)
926255736Sdavidch{
927255736Sdavidch    if (dma->size > 0) {
928255736Sdavidch        DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
929255736Sdavidch
930255736Sdavidch        bus_dmamap_sync(dma->tag, dma->map,
931255736Sdavidch                        (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
932255736Sdavidch        bus_dmamap_unload(dma->tag, dma->map);
933255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
934255736Sdavidch        bus_dma_tag_destroy(dma->tag);
935255736Sdavidch    }
936255736Sdavidch
937255736Sdavidch    memset(dma, 0, sizeof(*dma));
938255736Sdavidch}
939255736Sdavidch
940255736Sdavidch/*
941255736Sdavidch * These indirect read and write routines are only during init.
942255736Sdavidch * The locking is handled by the MCP.
943255736Sdavidch */
944255736Sdavidch
945255736Sdavidchvoid
946255736Sdavidchbxe_reg_wr_ind(struct bxe_softc *sc,
947255736Sdavidch               uint32_t         addr,
948255736Sdavidch               uint32_t         val)
949255736Sdavidch{
950255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
951255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
952255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
953255736Sdavidch}
954255736Sdavidch
955255736Sdavidchuint32_t
956255736Sdavidchbxe_reg_rd_ind(struct bxe_softc *sc,
957255736Sdavidch               uint32_t         addr)
958255736Sdavidch{
959255736Sdavidch    uint32_t val;
960255736Sdavidch
961255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
962255736Sdavidch    val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
963255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
964255736Sdavidch
965255736Sdavidch    return (val);
966255736Sdavidch}
967255736Sdavidch
968255736Sdavidchstatic int
969255736Sdavidchbxe_acquire_hw_lock(struct bxe_softc *sc,
970255736Sdavidch                    uint32_t         resource)
971255736Sdavidch{
972255736Sdavidch    uint32_t lock_status;
973255736Sdavidch    uint32_t resource_bit = (1 << resource);
974255736Sdavidch    int func = SC_FUNC(sc);
975255736Sdavidch    uint32_t hw_lock_control_reg;
976255736Sdavidch    int cnt;
977255736Sdavidch
978255736Sdavidch    /* validate the resource is within range */
979255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
980295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
981295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
982255736Sdavidch        return (-1);
983255736Sdavidch    }
984255736Sdavidch
985255736Sdavidch    if (func <= 5) {
986255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
987255736Sdavidch    } else {
988255736Sdavidch        hw_lock_control_reg =
989255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
990255736Sdavidch    }
991255736Sdavidch
992255736Sdavidch    /* validate the resource is not already taken */
993255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
994255736Sdavidch    if (lock_status & resource_bit) {
995295830Sdavidcs        BLOGE(sc, "resource (0x%x) in use (status 0x%x bit 0x%x)\n",
996295830Sdavidcs              resource, lock_status, resource_bit);
997255736Sdavidch        return (-1);
998255736Sdavidch    }
999255736Sdavidch
1000255736Sdavidch    /* try every 5ms for 5 seconds */
1001255736Sdavidch    for (cnt = 0; cnt < 1000; cnt++) {
1002255736Sdavidch        REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1003255736Sdavidch        lock_status = REG_RD(sc, hw_lock_control_reg);
1004255736Sdavidch        if (lock_status & resource_bit) {
1005255736Sdavidch            return (0);
1006255736Sdavidch        }
1007255736Sdavidch        DELAY(5000);
1008255736Sdavidch    }
1009255736Sdavidch
1010295830Sdavidcs    BLOGE(sc, "Resource 0x%x resource_bit 0x%x lock timeout!\n",
1011295830Sdavidcs        resource, resource_bit);
1012255736Sdavidch    return (-1);
1013255736Sdavidch}
1014255736Sdavidch
1015255736Sdavidchstatic int
1016255736Sdavidchbxe_release_hw_lock(struct bxe_softc *sc,
1017255736Sdavidch                    uint32_t         resource)
1018255736Sdavidch{
1019255736Sdavidch    uint32_t lock_status;
1020255736Sdavidch    uint32_t resource_bit = (1 << resource);
1021255736Sdavidch    int func = SC_FUNC(sc);
1022255736Sdavidch    uint32_t hw_lock_control_reg;
1023255736Sdavidch
1024255736Sdavidch    /* validate the resource is within range */
1025255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1026295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1027295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
1028255736Sdavidch        return (-1);
1029255736Sdavidch    }
1030255736Sdavidch
1031255736Sdavidch    if (func <= 5) {
1032255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1033255736Sdavidch    } else {
1034255736Sdavidch        hw_lock_control_reg =
1035255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1036255736Sdavidch    }
1037255736Sdavidch
1038255736Sdavidch    /* validate the resource is currently taken */
1039255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1040255736Sdavidch    if (!(lock_status & resource_bit)) {
1041295830Sdavidcs        BLOGE(sc, "resource (0x%x) not in use (status 0x%x bit 0x%x)\n",
1042295830Sdavidcs              resource, lock_status, resource_bit);
1043255736Sdavidch        return (-1);
1044255736Sdavidch    }
1045255736Sdavidch
1046255736Sdavidch    REG_WR(sc, hw_lock_control_reg, resource_bit);
1047255736Sdavidch    return (0);
1048255736Sdavidch}
1049284335Sdavidcsstatic void bxe_acquire_phy_lock(struct bxe_softc *sc)
1050284335Sdavidcs{
1051284335Sdavidcs	BXE_PHY_LOCK(sc);
1052284335Sdavidcs	bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1053284335Sdavidcs}
1054255736Sdavidch
1055284335Sdavidcsstatic void bxe_release_phy_lock(struct bxe_softc *sc)
1056284335Sdavidcs{
1057284335Sdavidcs	bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1058284335Sdavidcs	BXE_PHY_UNLOCK(sc);
1059284335Sdavidcs}
1060255736Sdavidch/*
1061255736Sdavidch * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1062255736Sdavidch * had we done things the other way around, if two pfs from the same port
1063255736Sdavidch * would attempt to access nvram at the same time, we could run into a
1064255736Sdavidch * scenario such as:
1065255736Sdavidch * pf A takes the port lock.
1066255736Sdavidch * pf B succeeds in taking the same lock since they are from the same port.
1067255736Sdavidch * pf A takes the per pf misc lock. Performs eeprom access.
1068255736Sdavidch * pf A finishes. Unlocks the per pf misc lock.
1069255736Sdavidch * Pf B takes the lock and proceeds to perform it's own access.
1070255736Sdavidch * pf A unlocks the per port lock, while pf B is still working (!).
1071255736Sdavidch * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1072255736Sdavidch * access corrupted by pf B).*
1073255736Sdavidch */
1074255736Sdavidchstatic int
1075255736Sdavidchbxe_acquire_nvram_lock(struct bxe_softc *sc)
1076255736Sdavidch{
1077255736Sdavidch    int port = SC_PORT(sc);
1078255736Sdavidch    int count, i;
1079255736Sdavidch    uint32_t val = 0;
1080255736Sdavidch
1081255736Sdavidch    /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1082255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1083255736Sdavidch
1084255736Sdavidch    /* adjust timeout for emulation/FPGA */
1085255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1086255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1087255736Sdavidch        count *= 100;
1088255736Sdavidch    }
1089255736Sdavidch
1090255736Sdavidch    /* request access to nvram interface */
1091255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1092255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1093255736Sdavidch
1094255736Sdavidch    for (i = 0; i < count*10; i++) {
1095255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1096255736Sdavidch        if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1097255736Sdavidch            break;
1098255736Sdavidch        }
1099255736Sdavidch
1100255736Sdavidch        DELAY(5);
1101255736Sdavidch    }
1102255736Sdavidch
1103255736Sdavidch    if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1104295830Sdavidcs        BLOGE(sc, "Cannot get access to nvram interface "
1105295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1106295830Sdavidcs            port, val);
1107255736Sdavidch        return (-1);
1108255736Sdavidch    }
1109255736Sdavidch
1110255736Sdavidch    return (0);
1111255736Sdavidch}
1112255736Sdavidch
1113255736Sdavidchstatic int
1114255736Sdavidchbxe_release_nvram_lock(struct bxe_softc *sc)
1115255736Sdavidch{
1116255736Sdavidch    int port = SC_PORT(sc);
1117255736Sdavidch    int count, i;
1118255736Sdavidch    uint32_t val = 0;
1119255736Sdavidch
1120255736Sdavidch    /* adjust timeout for emulation/FPGA */
1121255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1122255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1123255736Sdavidch        count *= 100;
1124255736Sdavidch    }
1125255736Sdavidch
1126255736Sdavidch    /* relinquish nvram interface */
1127255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1128255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1129255736Sdavidch
1130255736Sdavidch    for (i = 0; i < count*10; i++) {
1131255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1132255736Sdavidch        if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1133255736Sdavidch            break;
1134255736Sdavidch        }
1135255736Sdavidch
1136255736Sdavidch        DELAY(5);
1137255736Sdavidch    }
1138255736Sdavidch
1139255736Sdavidch    if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1140295830Sdavidcs        BLOGE(sc, "Cannot free access to nvram interface "
1141295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1142295830Sdavidcs            port, val);
1143255736Sdavidch        return (-1);
1144255736Sdavidch    }
1145255736Sdavidch
1146255736Sdavidch    /* release HW lock: protect against other PFs in PF Direct Assignment */
1147255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1148255736Sdavidch
1149255736Sdavidch    return (0);
1150255736Sdavidch}
1151255736Sdavidch
1152255736Sdavidchstatic void
1153255736Sdavidchbxe_enable_nvram_access(struct bxe_softc *sc)
1154255736Sdavidch{
1155255736Sdavidch    uint32_t val;
1156255736Sdavidch
1157255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1158255736Sdavidch
1159255736Sdavidch    /* enable both bits, even on read */
1160255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1161255736Sdavidch           (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1162255736Sdavidch}
1163255736Sdavidch
1164255736Sdavidchstatic void
1165255736Sdavidchbxe_disable_nvram_access(struct bxe_softc *sc)
1166255736Sdavidch{
1167255736Sdavidch    uint32_t val;
1168255736Sdavidch
1169255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1170255736Sdavidch
1171255736Sdavidch    /* disable both bits, even after read */
1172255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1173255736Sdavidch           (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1174255736Sdavidch                    MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1175255736Sdavidch}
1176255736Sdavidch
1177255736Sdavidchstatic int
1178255736Sdavidchbxe_nvram_read_dword(struct bxe_softc *sc,
1179255736Sdavidch                     uint32_t         offset,
1180255736Sdavidch                     uint32_t         *ret_val,
1181255736Sdavidch                     uint32_t         cmd_flags)
1182255736Sdavidch{
1183255736Sdavidch    int count, i, rc;
1184255736Sdavidch    uint32_t val;
1185255736Sdavidch
1186255736Sdavidch    /* build the command word */
1187255736Sdavidch    cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1188255736Sdavidch
1189255736Sdavidch    /* need to clear DONE bit separately */
1190255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1191255736Sdavidch
1192255736Sdavidch    /* address of the NVRAM to read from */
1193255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1194255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1195255736Sdavidch
1196255736Sdavidch    /* issue a read command */
1197255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1198255736Sdavidch
1199255736Sdavidch    /* adjust timeout for emulation/FPGA */
1200255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1201255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1202255736Sdavidch        count *= 100;
1203255736Sdavidch    }
1204255736Sdavidch
1205255736Sdavidch    /* wait for completion */
1206255736Sdavidch    *ret_val = 0;
1207255736Sdavidch    rc = -1;
1208255736Sdavidch    for (i = 0; i < count; i++) {
1209255736Sdavidch        DELAY(5);
1210255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1211255736Sdavidch
1212255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1213255736Sdavidch            val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1214255736Sdavidch            /* we read nvram data in cpu order
1215255736Sdavidch             * but ethtool sees it as an array of bytes
1216255736Sdavidch             * converting to big-endian will do the work
1217255736Sdavidch             */
1218255736Sdavidch            *ret_val = htobe32(val);
1219255736Sdavidch            rc = 0;
1220255736Sdavidch            break;
1221255736Sdavidch        }
1222255736Sdavidch    }
1223255736Sdavidch
1224255736Sdavidch    if (rc == -1) {
1225295830Sdavidcs        BLOGE(sc, "nvram read timeout expired "
1226295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1227295830Sdavidcs            offset, cmd_flags, val);
1228255736Sdavidch    }
1229255736Sdavidch
1230255736Sdavidch    return (rc);
1231255736Sdavidch}
1232255736Sdavidch
1233255736Sdavidchstatic int
1234255736Sdavidchbxe_nvram_read(struct bxe_softc *sc,
1235255736Sdavidch               uint32_t         offset,
1236255736Sdavidch               uint8_t          *ret_buf,
1237255736Sdavidch               int              buf_size)
1238255736Sdavidch{
1239255736Sdavidch    uint32_t cmd_flags;
1240255736Sdavidch    uint32_t val;
1241255736Sdavidch    int rc;
1242255736Sdavidch
1243255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1244255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1245255736Sdavidch              offset, buf_size);
1246255736Sdavidch        return (-1);
1247255736Sdavidch    }
1248255736Sdavidch
1249255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1250255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1251255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1252255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1253255736Sdavidch        return (-1);
1254255736Sdavidch    }
1255255736Sdavidch
1256255736Sdavidch    /* request access to nvram interface */
1257255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1258255736Sdavidch    if (rc) {
1259255736Sdavidch        return (rc);
1260255736Sdavidch    }
1261255736Sdavidch
1262255736Sdavidch    /* enable access to nvram interface */
1263255736Sdavidch    bxe_enable_nvram_access(sc);
1264255736Sdavidch
1265255736Sdavidch    /* read the first word(s) */
1266255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1267255736Sdavidch    while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1268255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1269255736Sdavidch        memcpy(ret_buf, &val, 4);
1270255736Sdavidch
1271255736Sdavidch        /* advance to the next dword */
1272255736Sdavidch        offset += sizeof(uint32_t);
1273255736Sdavidch        ret_buf += sizeof(uint32_t);
1274255736Sdavidch        buf_size -= sizeof(uint32_t);
1275255736Sdavidch        cmd_flags = 0;
1276255736Sdavidch    }
1277255736Sdavidch
1278255736Sdavidch    if (rc == 0) {
1279255736Sdavidch        cmd_flags |= MCPR_NVM_COMMAND_LAST;
1280255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1281255736Sdavidch        memcpy(ret_buf, &val, 4);
1282255736Sdavidch    }
1283255736Sdavidch
1284255736Sdavidch    /* disable access to nvram interface */
1285255736Sdavidch    bxe_disable_nvram_access(sc);
1286255736Sdavidch    bxe_release_nvram_lock(sc);
1287255736Sdavidch
1288255736Sdavidch    return (rc);
1289255736Sdavidch}
1290255736Sdavidch
1291255736Sdavidchstatic int
1292255736Sdavidchbxe_nvram_write_dword(struct bxe_softc *sc,
1293255736Sdavidch                      uint32_t         offset,
1294255736Sdavidch                      uint32_t         val,
1295255736Sdavidch                      uint32_t         cmd_flags)
1296255736Sdavidch{
1297255736Sdavidch    int count, i, rc;
1298255736Sdavidch
1299255736Sdavidch    /* build the command word */
1300255736Sdavidch    cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1301255736Sdavidch
1302255736Sdavidch    /* need to clear DONE bit separately */
1303255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1304255736Sdavidch
1305255736Sdavidch    /* write the data */
1306255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1307255736Sdavidch
1308255736Sdavidch    /* address of the NVRAM to write to */
1309255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1310255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1311255736Sdavidch
1312255736Sdavidch    /* issue the write command */
1313255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1314255736Sdavidch
1315255736Sdavidch    /* adjust timeout for emulation/FPGA */
1316255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1317255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1318255736Sdavidch        count *= 100;
1319255736Sdavidch    }
1320255736Sdavidch
1321255736Sdavidch    /* wait for completion */
1322255736Sdavidch    rc = -1;
1323255736Sdavidch    for (i = 0; i < count; i++) {
1324255736Sdavidch        DELAY(5);
1325255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1326255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1327255736Sdavidch            rc = 0;
1328255736Sdavidch            break;
1329255736Sdavidch        }
1330255736Sdavidch    }
1331255736Sdavidch
1332255736Sdavidch    if (rc == -1) {
1333295830Sdavidcs        BLOGE(sc, "nvram write timeout expired "
1334295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1335295830Sdavidcs            offset, cmd_flags, val);
1336255736Sdavidch    }
1337255736Sdavidch
1338255736Sdavidch    return (rc);
1339255736Sdavidch}
1340255736Sdavidch
1341255736Sdavidch#define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1342255736Sdavidch
1343255736Sdavidchstatic int
1344255736Sdavidchbxe_nvram_write1(struct bxe_softc *sc,
1345255736Sdavidch                 uint32_t         offset,
1346255736Sdavidch                 uint8_t          *data_buf,
1347255736Sdavidch                 int              buf_size)
1348255736Sdavidch{
1349255736Sdavidch    uint32_t cmd_flags;
1350255736Sdavidch    uint32_t align_offset;
1351255736Sdavidch    uint32_t val;
1352255736Sdavidch    int rc;
1353255736Sdavidch
1354255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1355255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1356255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1357255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1358255736Sdavidch        return (-1);
1359255736Sdavidch    }
1360255736Sdavidch
1361255736Sdavidch    /* request access to nvram interface */
1362255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1363255736Sdavidch    if (rc) {
1364255736Sdavidch        return (rc);
1365255736Sdavidch    }
1366255736Sdavidch
1367255736Sdavidch    /* enable access to nvram interface */
1368255736Sdavidch    bxe_enable_nvram_access(sc);
1369255736Sdavidch
1370255736Sdavidch    cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1371255736Sdavidch    align_offset = (offset & ~0x03);
1372255736Sdavidch    rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1373255736Sdavidch
1374255736Sdavidch    if (rc == 0) {
1375255736Sdavidch        val &= ~(0xff << BYTE_OFFSET(offset));
1376255736Sdavidch        val |= (*data_buf << BYTE_OFFSET(offset));
1377255736Sdavidch
1378255736Sdavidch        /* nvram data is returned as an array of bytes
1379255736Sdavidch         * convert it back to cpu order
1380255736Sdavidch         */
1381255736Sdavidch        val = be32toh(val);
1382255736Sdavidch
1383255736Sdavidch        rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1384255736Sdavidch    }
1385255736Sdavidch
1386255736Sdavidch    /* disable access to nvram interface */
1387255736Sdavidch    bxe_disable_nvram_access(sc);
1388255736Sdavidch    bxe_release_nvram_lock(sc);
1389255736Sdavidch
1390255736Sdavidch    return (rc);
1391255736Sdavidch}
1392255736Sdavidch
1393255736Sdavidchstatic int
1394255736Sdavidchbxe_nvram_write(struct bxe_softc *sc,
1395255736Sdavidch                uint32_t         offset,
1396255736Sdavidch                uint8_t          *data_buf,
1397255736Sdavidch                int              buf_size)
1398255736Sdavidch{
1399255736Sdavidch    uint32_t cmd_flags;
1400255736Sdavidch    uint32_t val;
1401255736Sdavidch    uint32_t written_so_far;
1402255736Sdavidch    int rc;
1403255736Sdavidch
1404255736Sdavidch    if (buf_size == 1) {
1405255736Sdavidch        return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1406255736Sdavidch    }
1407255736Sdavidch
1408255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1409255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1410255736Sdavidch              offset, buf_size);
1411255736Sdavidch        return (-1);
1412255736Sdavidch    }
1413255736Sdavidch
1414255736Sdavidch    if (buf_size == 0) {
1415255736Sdavidch        return (0); /* nothing to do */
1416255736Sdavidch    }
1417255736Sdavidch
1418255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1419255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1420255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1421255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1422255736Sdavidch        return (-1);
1423255736Sdavidch    }
1424255736Sdavidch
1425255736Sdavidch    /* request access to nvram interface */
1426255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1427255736Sdavidch    if (rc) {
1428255736Sdavidch        return (rc);
1429255736Sdavidch    }
1430255736Sdavidch
1431255736Sdavidch    /* enable access to nvram interface */
1432255736Sdavidch    bxe_enable_nvram_access(sc);
1433255736Sdavidch
1434255736Sdavidch    written_so_far = 0;
1435255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1436255736Sdavidch    while ((written_so_far < buf_size) && (rc == 0)) {
1437255736Sdavidch        if (written_so_far == (buf_size - sizeof(uint32_t))) {
1438255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1439255736Sdavidch        } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1440255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1441255736Sdavidch        } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1442255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1443255736Sdavidch        }
1444255736Sdavidch
1445255736Sdavidch        memcpy(&val, data_buf, 4);
1446255736Sdavidch
1447255736Sdavidch        rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1448255736Sdavidch
1449255736Sdavidch        /* advance to the next dword */
1450255736Sdavidch        offset += sizeof(uint32_t);
1451255736Sdavidch        data_buf += sizeof(uint32_t);
1452255736Sdavidch        written_so_far += sizeof(uint32_t);
1453255736Sdavidch        cmd_flags = 0;
1454255736Sdavidch    }
1455255736Sdavidch
1456255736Sdavidch    /* disable access to nvram interface */
1457255736Sdavidch    bxe_disable_nvram_access(sc);
1458255736Sdavidch    bxe_release_nvram_lock(sc);
1459255736Sdavidch
1460255736Sdavidch    return (rc);
1461255736Sdavidch}
1462255736Sdavidch
1463255736Sdavidch/* copy command into DMAE command memory and set DMAE command Go */
1464255736Sdavidchvoid
1465255736Sdavidchbxe_post_dmae(struct bxe_softc    *sc,
1466296071Sdavidcs              struct dmae_cmd *dmae,
1467255736Sdavidch              int                 idx)
1468255736Sdavidch{
1469255736Sdavidch    uint32_t cmd_offset;
1470255736Sdavidch    int i;
1471255736Sdavidch
1472296071Sdavidcs    cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_cmd) * idx));
1473296071Sdavidcs    for (i = 0; i < ((sizeof(struct dmae_cmd) / 4)); i++) {
1474255736Sdavidch        REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1475255736Sdavidch    }
1476255736Sdavidch
1477255736Sdavidch    REG_WR(sc, dmae_reg_go_c[idx], 1);
1478255736Sdavidch}
1479255736Sdavidch
1480255736Sdavidchuint32_t
1481255736Sdavidchbxe_dmae_opcode_add_comp(uint32_t opcode,
1482255736Sdavidch                         uint8_t  comp_type)
1483255736Sdavidch{
1484296071Sdavidcs    return (opcode | ((comp_type << DMAE_CMD_C_DST_SHIFT) |
1485296071Sdavidcs                      DMAE_CMD_C_TYPE_ENABLE));
1486255736Sdavidch}
1487255736Sdavidch
1488255736Sdavidchuint32_t
1489255736Sdavidchbxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1490255736Sdavidch{
1491296071Sdavidcs    return (opcode & ~DMAE_CMD_SRC_RESET);
1492255736Sdavidch}
1493255736Sdavidch
1494255736Sdavidchuint32_t
1495255736Sdavidchbxe_dmae_opcode(struct bxe_softc *sc,
1496255736Sdavidch                uint8_t          src_type,
1497255736Sdavidch                uint8_t          dst_type,
1498255736Sdavidch                uint8_t          with_comp,
1499255736Sdavidch                uint8_t          comp_type)
1500255736Sdavidch{
1501255736Sdavidch    uint32_t opcode = 0;
1502255736Sdavidch
1503296071Sdavidcs    opcode |= ((src_type << DMAE_CMD_SRC_SHIFT) |
1504296071Sdavidcs               (dst_type << DMAE_CMD_DST_SHIFT));
1505255736Sdavidch
1506296071Sdavidcs    opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
1507255736Sdavidch
1508255736Sdavidch    opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1509255736Sdavidch
1510296071Sdavidcs    opcode |= ((SC_VN(sc) << DMAE_CMD_E1HVN_SHIFT) |
1511296071Sdavidcs               (SC_VN(sc) << DMAE_CMD_DST_VN_SHIFT));
1512255736Sdavidch
1513296071Sdavidcs    opcode |= (DMAE_COM_SET_ERR << DMAE_CMD_ERR_POLICY_SHIFT);
1514255736Sdavidch
1515255736Sdavidch#ifdef __BIG_ENDIAN
1516255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1517255736Sdavidch#else
1518255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1519255736Sdavidch#endif
1520255736Sdavidch
1521255736Sdavidch    if (with_comp) {
1522255736Sdavidch        opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1523255736Sdavidch    }
1524255736Sdavidch
1525255736Sdavidch    return (opcode);
1526255736Sdavidch}
1527255736Sdavidch
1528255736Sdavidchstatic void
1529255736Sdavidchbxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1530296071Sdavidcs                        struct dmae_cmd *dmae,
1531255736Sdavidch                        uint8_t             src_type,
1532255736Sdavidch                        uint8_t             dst_type)
1533255736Sdavidch{
1534296071Sdavidcs    memset(dmae, 0, sizeof(struct dmae_cmd));
1535255736Sdavidch
1536255736Sdavidch    /* set the opcode */
1537255736Sdavidch    dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1538255736Sdavidch                                   TRUE, DMAE_COMP_PCI);
1539255736Sdavidch
1540255736Sdavidch    /* fill in the completion parameters */
1541255736Sdavidch    dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1542255736Sdavidch    dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1543255736Sdavidch    dmae->comp_val     = DMAE_COMP_VAL;
1544255736Sdavidch}
1545255736Sdavidch
1546255736Sdavidch/* issue a DMAE command over the init channel and wait for completion */
1547255736Sdavidchstatic int
1548255736Sdavidchbxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1549296071Sdavidcs                         struct dmae_cmd *dmae)
1550255736Sdavidch{
1551255736Sdavidch    uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1552255736Sdavidch    int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1553255736Sdavidch
1554255736Sdavidch    BXE_DMAE_LOCK(sc);
1555255736Sdavidch
1556255736Sdavidch    /* reset completion */
1557255736Sdavidch    *wb_comp = 0;
1558255736Sdavidch
1559255736Sdavidch    /* post the command on the channel used for initializations */
1560255736Sdavidch    bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1561255736Sdavidch
1562255736Sdavidch    /* wait for completion */
1563255736Sdavidch    DELAY(5);
1564255736Sdavidch
1565255736Sdavidch    while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1566255736Sdavidch        if (!timeout ||
1567255736Sdavidch            (sc->recovery_state != BXE_RECOVERY_DONE &&
1568255736Sdavidch             sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1569295830Sdavidcs            BLOGE(sc, "DMAE timeout! *wb_comp 0x%x recovery_state 0x%x\n",
1570295830Sdavidcs                *wb_comp, sc->recovery_state);
1571255736Sdavidch            BXE_DMAE_UNLOCK(sc);
1572255736Sdavidch            return (DMAE_TIMEOUT);
1573255736Sdavidch        }
1574255736Sdavidch
1575255736Sdavidch        timeout--;
1576255736Sdavidch        DELAY(50);
1577255736Sdavidch    }
1578255736Sdavidch
1579255736Sdavidch    if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1580295830Sdavidcs        BLOGE(sc, "DMAE PCI error! *wb_comp 0x%x recovery_state 0x%x\n",
1581295830Sdavidcs                *wb_comp, sc->recovery_state);
1582255736Sdavidch        BXE_DMAE_UNLOCK(sc);
1583255736Sdavidch        return (DMAE_PCI_ERROR);
1584255736Sdavidch    }
1585255736Sdavidch
1586255736Sdavidch    BXE_DMAE_UNLOCK(sc);
1587255736Sdavidch    return (0);
1588255736Sdavidch}
1589255736Sdavidch
1590255736Sdavidchvoid
1591255736Sdavidchbxe_read_dmae(struct bxe_softc *sc,
1592255736Sdavidch              uint32_t         src_addr,
1593255736Sdavidch              uint32_t         len32)
1594255736Sdavidch{
1595296071Sdavidcs    struct dmae_cmd dmae;
1596255736Sdavidch    uint32_t *data;
1597255736Sdavidch    int i, rc;
1598255736Sdavidch
1599255736Sdavidch    DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1600255736Sdavidch
1601255736Sdavidch    if (!sc->dmae_ready) {
1602255736Sdavidch        data = BXE_SP(sc, wb_data[0]);
1603255736Sdavidch
1604255736Sdavidch        for (i = 0; i < len32; i++) {
1605255736Sdavidch            data[i] = (CHIP_IS_E1(sc)) ?
1606255736Sdavidch                          bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1607255736Sdavidch                          REG_RD(sc, (src_addr + (i * 4)));
1608255736Sdavidch        }
1609255736Sdavidch
1610255736Sdavidch        return;
1611255736Sdavidch    }
1612255736Sdavidch
1613255736Sdavidch    /* set opcode and fixed command fields */
1614255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1615255736Sdavidch
1616255736Sdavidch    /* fill in addresses and len */
1617255736Sdavidch    dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1618255736Sdavidch    dmae.src_addr_hi = 0;
1619255736Sdavidch    dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1620255736Sdavidch    dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1621255736Sdavidch    dmae.len         = len32;
1622255736Sdavidch
1623255736Sdavidch    /* issue the command and wait for completion */
1624255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1625255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1626297793Spfg    }
1627255736Sdavidch}
1628255736Sdavidch
1629255736Sdavidchvoid
1630255736Sdavidchbxe_write_dmae(struct bxe_softc *sc,
1631255736Sdavidch               bus_addr_t       dma_addr,
1632255736Sdavidch               uint32_t         dst_addr,
1633255736Sdavidch               uint32_t         len32)
1634255736Sdavidch{
1635296071Sdavidcs    struct dmae_cmd dmae;
1636255736Sdavidch    int rc;
1637255736Sdavidch
1638255736Sdavidch    if (!sc->dmae_ready) {
1639255736Sdavidch        DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1640255736Sdavidch
1641255736Sdavidch        if (CHIP_IS_E1(sc)) {
1642255736Sdavidch            ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1643255736Sdavidch        } else {
1644255736Sdavidch            ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1645255736Sdavidch        }
1646255736Sdavidch
1647255736Sdavidch        return;
1648255736Sdavidch    }
1649255736Sdavidch
1650255736Sdavidch    /* set opcode and fixed command fields */
1651255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1652255736Sdavidch
1653255736Sdavidch    /* fill in addresses and len */
1654255736Sdavidch    dmae.src_addr_lo = U64_LO(dma_addr);
1655255736Sdavidch    dmae.src_addr_hi = U64_HI(dma_addr);
1656255736Sdavidch    dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1657255736Sdavidch    dmae.dst_addr_hi = 0;
1658255736Sdavidch    dmae.len         = len32;
1659255736Sdavidch
1660255736Sdavidch    /* issue the command and wait for completion */
1661255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1662255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1663255736Sdavidch    }
1664255736Sdavidch}
1665255736Sdavidch
1666255736Sdavidchvoid
1667255736Sdavidchbxe_write_dmae_phys_len(struct bxe_softc *sc,
1668255736Sdavidch                        bus_addr_t       phys_addr,
1669255736Sdavidch                        uint32_t         addr,
1670255736Sdavidch                        uint32_t         len)
1671255736Sdavidch{
1672255736Sdavidch    int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1673255736Sdavidch    int offset = 0;
1674255736Sdavidch
1675255736Sdavidch    while (len > dmae_wr_max) {
1676255736Sdavidch        bxe_write_dmae(sc,
1677255736Sdavidch                       (phys_addr + offset), /* src DMA address */
1678255736Sdavidch                       (addr + offset),      /* dst GRC address */
1679255736Sdavidch                       dmae_wr_max);
1680255736Sdavidch        offset += (dmae_wr_max * 4);
1681255736Sdavidch        len -= dmae_wr_max;
1682255736Sdavidch    }
1683255736Sdavidch
1684255736Sdavidch    bxe_write_dmae(sc,
1685255736Sdavidch                   (phys_addr + offset), /* src DMA address */
1686255736Sdavidch                   (addr + offset),      /* dst GRC address */
1687255736Sdavidch                   len);
1688255736Sdavidch}
1689255736Sdavidch
1690255736Sdavidchvoid
1691255736Sdavidchbxe_set_ctx_validation(struct bxe_softc   *sc,
1692255736Sdavidch                       struct eth_context *cxt,
1693255736Sdavidch                       uint32_t           cid)
1694255736Sdavidch{
1695255736Sdavidch    /* ustorm cxt validation */
1696255736Sdavidch    cxt->ustorm_ag_context.cdu_usage =
1697255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1698255736Sdavidch            CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1699255736Sdavidch    /* xcontext validation */
1700255736Sdavidch    cxt->xstorm_ag_context.cdu_reserved =
1701255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1702255736Sdavidch            CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1703255736Sdavidch}
1704255736Sdavidch
1705255736Sdavidchstatic void
1706255736Sdavidchbxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1707255736Sdavidch                            uint8_t          port,
1708255736Sdavidch                            uint8_t          fw_sb_id,
1709255736Sdavidch                            uint8_t          sb_index,
1710255736Sdavidch                            uint8_t          ticks)
1711255736Sdavidch{
1712255736Sdavidch    uint32_t addr =
1713255736Sdavidch        (BAR_CSTRORM_INTMEM +
1714255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1715255736Sdavidch
1716255736Sdavidch    REG_WR8(sc, addr, ticks);
1717255736Sdavidch
1718255736Sdavidch    BLOGD(sc, DBG_LOAD,
1719255736Sdavidch          "port %d fw_sb_id %d sb_index %d ticks %d\n",
1720255736Sdavidch          port, fw_sb_id, sb_index, ticks);
1721255736Sdavidch}
1722255736Sdavidch
1723255736Sdavidchstatic void
1724255736Sdavidchbxe_storm_memset_hc_disable(struct bxe_softc *sc,
1725255736Sdavidch                            uint8_t          port,
1726255736Sdavidch                            uint16_t         fw_sb_id,
1727255736Sdavidch                            uint8_t          sb_index,
1728255736Sdavidch                            uint8_t          disable)
1729255736Sdavidch{
1730255736Sdavidch    uint32_t enable_flag =
1731255736Sdavidch        (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1732255736Sdavidch    uint32_t addr =
1733255736Sdavidch        (BAR_CSTRORM_INTMEM +
1734255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1735255736Sdavidch    uint8_t flags;
1736255736Sdavidch
1737255736Sdavidch    /* clear and set */
1738255736Sdavidch    flags = REG_RD8(sc, addr);
1739255736Sdavidch    flags &= ~HC_INDEX_DATA_HC_ENABLED;
1740255736Sdavidch    flags |= enable_flag;
1741255736Sdavidch    REG_WR8(sc, addr, flags);
1742255736Sdavidch
1743255736Sdavidch    BLOGD(sc, DBG_LOAD,
1744255736Sdavidch          "port %d fw_sb_id %d sb_index %d disable %d\n",
1745255736Sdavidch          port, fw_sb_id, sb_index, disable);
1746255736Sdavidch}
1747255736Sdavidch
1748255736Sdavidchvoid
1749255736Sdavidchbxe_update_coalesce_sb_index(struct bxe_softc *sc,
1750255736Sdavidch                             uint8_t          fw_sb_id,
1751255736Sdavidch                             uint8_t          sb_index,
1752255736Sdavidch                             uint8_t          disable,
1753255736Sdavidch                             uint16_t         usec)
1754255736Sdavidch{
1755255736Sdavidch    int port = SC_PORT(sc);
1756255736Sdavidch    uint8_t ticks = (usec / 4); /* XXX ??? */
1757255736Sdavidch
1758255736Sdavidch    bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1759255736Sdavidch
1760255736Sdavidch    disable = (disable) ? 1 : ((usec) ? 0 : 1);
1761255736Sdavidch    bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1762255736Sdavidch}
1763255736Sdavidch
1764255736Sdavidchvoid
1765255736Sdavidchelink_cb_udelay(struct bxe_softc *sc,
1766255736Sdavidch                uint32_t         usecs)
1767255736Sdavidch{
1768255736Sdavidch    DELAY(usecs);
1769255736Sdavidch}
1770255736Sdavidch
1771255736Sdavidchuint32_t
1772255736Sdavidchelink_cb_reg_read(struct bxe_softc *sc,
1773255736Sdavidch                  uint32_t         reg_addr)
1774255736Sdavidch{
1775255736Sdavidch    return (REG_RD(sc, reg_addr));
1776255736Sdavidch}
1777255736Sdavidch
1778255736Sdavidchvoid
1779255736Sdavidchelink_cb_reg_write(struct bxe_softc *sc,
1780255736Sdavidch                   uint32_t         reg_addr,
1781255736Sdavidch                   uint32_t         val)
1782255736Sdavidch{
1783255736Sdavidch    REG_WR(sc, reg_addr, val);
1784255736Sdavidch}
1785255736Sdavidch
1786255736Sdavidchvoid
1787255736Sdavidchelink_cb_reg_wb_write(struct bxe_softc *sc,
1788255736Sdavidch                      uint32_t         offset,
1789255736Sdavidch                      uint32_t         *wb_write,
1790255736Sdavidch                      uint16_t         len)
1791255736Sdavidch{
1792255736Sdavidch    REG_WR_DMAE(sc, offset, wb_write, len);
1793255736Sdavidch}
1794255736Sdavidch
1795255736Sdavidchvoid
1796255736Sdavidchelink_cb_reg_wb_read(struct bxe_softc *sc,
1797255736Sdavidch                     uint32_t         offset,
1798255736Sdavidch                     uint32_t         *wb_write,
1799255736Sdavidch                     uint16_t         len)
1800255736Sdavidch{
1801255736Sdavidch    REG_RD_DMAE(sc, offset, wb_write, len);
1802255736Sdavidch}
1803255736Sdavidch
1804255736Sdavidchuint8_t
1805255736Sdavidchelink_cb_path_id(struct bxe_softc *sc)
1806255736Sdavidch{
1807255736Sdavidch    return (SC_PATH(sc));
1808255736Sdavidch}
1809255736Sdavidch
1810255736Sdavidchvoid
1811255736Sdavidchelink_cb_event_log(struct bxe_softc     *sc,
1812255736Sdavidch                   const elink_log_id_t elink_log_id,
1813255736Sdavidch                   ...)
1814255736Sdavidch{
1815255736Sdavidch    /* XXX */
1816255736Sdavidch    BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1817255736Sdavidch}
1818255736Sdavidch
1819255736Sdavidchstatic int
1820255736Sdavidchbxe_set_spio(struct bxe_softc *sc,
1821255736Sdavidch             int              spio,
1822255736Sdavidch             uint32_t         mode)
1823255736Sdavidch{
1824255736Sdavidch    uint32_t spio_reg;
1825255736Sdavidch
1826255736Sdavidch    /* Only 2 SPIOs are configurable */
1827255736Sdavidch    if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1828295830Sdavidcs        BLOGE(sc, "Invalid SPIO 0x%x mode 0x%x\n", spio, mode);
1829255736Sdavidch        return (-1);
1830255736Sdavidch    }
1831255736Sdavidch
1832255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1833255736Sdavidch
1834255736Sdavidch    /* read SPIO and mask except the float bits */
1835255736Sdavidch    spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1836255736Sdavidch
1837255736Sdavidch    switch (mode) {
1838255736Sdavidch    case MISC_SPIO_OUTPUT_LOW:
1839255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1840255736Sdavidch        /* clear FLOAT and set CLR */
1841255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1842255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1843255736Sdavidch        break;
1844255736Sdavidch
1845255736Sdavidch    case MISC_SPIO_OUTPUT_HIGH:
1846255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1847255736Sdavidch        /* clear FLOAT and set SET */
1848255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1849255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_SET_POS);
1850255736Sdavidch        break;
1851255736Sdavidch
1852255736Sdavidch    case MISC_SPIO_INPUT_HI_Z:
1853255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1854255736Sdavidch        /* set FLOAT */
1855255736Sdavidch        spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1856255736Sdavidch        break;
1857255736Sdavidch
1858255736Sdavidch    default:
1859255736Sdavidch        break;
1860255736Sdavidch    }
1861255736Sdavidch
1862255736Sdavidch    REG_WR(sc, MISC_REG_SPIO, spio_reg);
1863255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1864255736Sdavidch
1865255736Sdavidch    return (0);
1866255736Sdavidch}
1867255736Sdavidch
1868255736Sdavidchstatic int
1869255736Sdavidchbxe_gpio_read(struct bxe_softc *sc,
1870255736Sdavidch              int              gpio_num,
1871255736Sdavidch              uint8_t          port)
1872255736Sdavidch{
1873255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1874255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1875255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1876255736Sdavidch    int gpio_shift = (gpio_num +
1877255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1878255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1879255736Sdavidch    uint32_t gpio_reg;
1880255736Sdavidch
1881255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1882295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d port 0x%x gpio_port %d gpio_shift %d"
1883295830Sdavidcs            " gpio_mask 0x%x\n", gpio_num, port, gpio_port, gpio_shift,
1884295830Sdavidcs            gpio_mask);
1885255736Sdavidch        return (-1);
1886255736Sdavidch    }
1887255736Sdavidch
1888255736Sdavidch    /* read GPIO value */
1889255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1890255736Sdavidch
1891255736Sdavidch    /* get the requested pin value */
1892255736Sdavidch    return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
1893255736Sdavidch}
1894255736Sdavidch
1895255736Sdavidchstatic int
1896255736Sdavidchbxe_gpio_write(struct bxe_softc *sc,
1897255736Sdavidch               int              gpio_num,
1898255736Sdavidch               uint32_t         mode,
1899255736Sdavidch               uint8_t          port)
1900255736Sdavidch{
1901255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1902255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1903255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1904255736Sdavidch    int gpio_shift = (gpio_num +
1905255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1906255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1907255736Sdavidch    uint32_t gpio_reg;
1908255736Sdavidch
1909255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1910295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
1911295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
1912295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
1913255736Sdavidch        return (-1);
1914255736Sdavidch    }
1915255736Sdavidch
1916255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1917255736Sdavidch
1918255736Sdavidch    /* read GPIO and mask except the float bits */
1919255736Sdavidch    gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1920255736Sdavidch
1921255736Sdavidch    switch (mode) {
1922255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1923255736Sdavidch        BLOGD(sc, DBG_PHY,
1924255736Sdavidch              "Set GPIO %d (shift %d) -> output low\n",
1925255736Sdavidch              gpio_num, gpio_shift);
1926255736Sdavidch        /* clear FLOAT and set CLR */
1927255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1928255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1929255736Sdavidch        break;
1930255736Sdavidch
1931255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1932255736Sdavidch        BLOGD(sc, DBG_PHY,
1933255736Sdavidch              "Set GPIO %d (shift %d) -> output high\n",
1934255736Sdavidch              gpio_num, gpio_shift);
1935255736Sdavidch        /* clear FLOAT and set SET */
1936255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1937255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1938255736Sdavidch        break;
1939255736Sdavidch
1940255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1941255736Sdavidch        BLOGD(sc, DBG_PHY,
1942255736Sdavidch              "Set GPIO %d (shift %d) -> input\n",
1943255736Sdavidch              gpio_num, gpio_shift);
1944255736Sdavidch        /* set FLOAT */
1945255736Sdavidch        gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1946255736Sdavidch        break;
1947255736Sdavidch
1948255736Sdavidch    default:
1949255736Sdavidch        break;
1950255736Sdavidch    }
1951255736Sdavidch
1952255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1953255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1954255736Sdavidch
1955255736Sdavidch    return (0);
1956255736Sdavidch}
1957255736Sdavidch
1958255736Sdavidchstatic int
1959255736Sdavidchbxe_gpio_mult_write(struct bxe_softc *sc,
1960255736Sdavidch                    uint8_t          pins,
1961255736Sdavidch                    uint32_t         mode)
1962255736Sdavidch{
1963255736Sdavidch    uint32_t gpio_reg;
1964255736Sdavidch
1965255736Sdavidch    /* any port swapping should be handled by caller */
1966255736Sdavidch
1967255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1968255736Sdavidch
1969255736Sdavidch    /* read GPIO and mask except the float bits */
1970255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1971255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1972255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
1973255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
1974255736Sdavidch
1975255736Sdavidch    switch (mode) {
1976255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1977255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
1978255736Sdavidch        /* set CLR */
1979255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
1980255736Sdavidch        break;
1981255736Sdavidch
1982255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1983255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
1984255736Sdavidch        /* set SET */
1985255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
1986255736Sdavidch        break;
1987255736Sdavidch
1988255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1989255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
1990255736Sdavidch        /* set FLOAT */
1991255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1992255736Sdavidch        break;
1993255736Sdavidch
1994255736Sdavidch    default:
1995295830Sdavidcs        BLOGE(sc, "Invalid GPIO mode assignment pins 0x%x mode 0x%x"
1996295830Sdavidcs            " gpio_reg 0x%x\n", pins, mode, gpio_reg);
1997255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1998255736Sdavidch        return (-1);
1999255736Sdavidch    }
2000255736Sdavidch
2001255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2002255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2003255736Sdavidch
2004255736Sdavidch    return (0);
2005255736Sdavidch}
2006255736Sdavidch
2007255736Sdavidchstatic int
2008255736Sdavidchbxe_gpio_int_write(struct bxe_softc *sc,
2009255736Sdavidch                   int              gpio_num,
2010255736Sdavidch                   uint32_t         mode,
2011255736Sdavidch                   uint8_t          port)
2012255736Sdavidch{
2013255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
2014255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2015255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2016255736Sdavidch    int gpio_shift = (gpio_num +
2017255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2018255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
2019255736Sdavidch    uint32_t gpio_reg;
2020255736Sdavidch
2021255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
2022295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
2023295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
2024295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
2025255736Sdavidch        return (-1);
2026255736Sdavidch    }
2027255736Sdavidch
2028255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2029255736Sdavidch
2030255736Sdavidch    /* read GPIO int */
2031255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2032255736Sdavidch
2033255736Sdavidch    switch (mode) {
2034255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2035255736Sdavidch        BLOGD(sc, DBG_PHY,
2036255736Sdavidch              "Clear GPIO INT %d (shift %d) -> output low\n",
2037255736Sdavidch              gpio_num, gpio_shift);
2038255736Sdavidch        /* clear SET and set CLR */
2039255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2040255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2041255736Sdavidch        break;
2042255736Sdavidch
2043255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2044255736Sdavidch        BLOGD(sc, DBG_PHY,
2045255736Sdavidch              "Set GPIO INT %d (shift %d) -> output high\n",
2046255736Sdavidch              gpio_num, gpio_shift);
2047255736Sdavidch        /* clear CLR and set SET */
2048255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2049255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2050255736Sdavidch        break;
2051255736Sdavidch
2052255736Sdavidch    default:
2053255736Sdavidch        break;
2054255736Sdavidch    }
2055255736Sdavidch
2056255736Sdavidch    REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2057255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2058255736Sdavidch
2059255736Sdavidch    return (0);
2060255736Sdavidch}
2061255736Sdavidch
2062255736Sdavidchuint32_t
2063255736Sdavidchelink_cb_gpio_read(struct bxe_softc *sc,
2064255736Sdavidch                   uint16_t         gpio_num,
2065255736Sdavidch                   uint8_t          port)
2066255736Sdavidch{
2067255736Sdavidch    return (bxe_gpio_read(sc, gpio_num, port));
2068255736Sdavidch}
2069255736Sdavidch
2070255736Sdavidchuint8_t
2071255736Sdavidchelink_cb_gpio_write(struct bxe_softc *sc,
2072255736Sdavidch                    uint16_t         gpio_num,
2073255736Sdavidch                    uint8_t          mode, /* 0=low 1=high */
2074255736Sdavidch                    uint8_t          port)
2075255736Sdavidch{
2076255736Sdavidch    return (bxe_gpio_write(sc, gpio_num, mode, port));
2077255736Sdavidch}
2078255736Sdavidch
2079255736Sdavidchuint8_t
2080255736Sdavidchelink_cb_gpio_mult_write(struct bxe_softc *sc,
2081255736Sdavidch                         uint8_t          pins,
2082255736Sdavidch                         uint8_t          mode) /* 0=low 1=high */
2083255736Sdavidch{
2084255736Sdavidch    return (bxe_gpio_mult_write(sc, pins, mode));
2085255736Sdavidch}
2086255736Sdavidch
2087255736Sdavidchuint8_t
2088255736Sdavidchelink_cb_gpio_int_write(struct bxe_softc *sc,
2089255736Sdavidch                        uint16_t         gpio_num,
2090255736Sdavidch                        uint8_t          mode, /* 0=low 1=high */
2091255736Sdavidch                        uint8_t          port)
2092255736Sdavidch{
2093255736Sdavidch    return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2094255736Sdavidch}
2095255736Sdavidch
2096255736Sdavidchvoid
2097255736Sdavidchelink_cb_notify_link_changed(struct bxe_softc *sc)
2098255736Sdavidch{
2099255736Sdavidch    REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2100255736Sdavidch                (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2101255736Sdavidch}
2102255736Sdavidch
2103255736Sdavidch/* send the MCP a request, block until there is a reply */
2104255736Sdavidchuint32_t
2105255736Sdavidchelink_cb_fw_command(struct bxe_softc *sc,
2106255736Sdavidch                    uint32_t         command,
2107255736Sdavidch                    uint32_t         param)
2108255736Sdavidch{
2109255736Sdavidch    int mb_idx = SC_FW_MB_IDX(sc);
2110255736Sdavidch    uint32_t seq;
2111255736Sdavidch    uint32_t rc = 0;
2112255736Sdavidch    uint32_t cnt = 1;
2113255736Sdavidch    uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2114255736Sdavidch
2115255736Sdavidch    BXE_FWMB_LOCK(sc);
2116255736Sdavidch
2117255736Sdavidch    seq = ++sc->fw_seq;
2118255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2119255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2120255736Sdavidch
2121255736Sdavidch    BLOGD(sc, DBG_PHY,
2122255736Sdavidch          "wrote command 0x%08x to FW MB param 0x%08x\n",
2123255736Sdavidch          (command | seq), param);
2124255736Sdavidch
2125255736Sdavidch    /* Let the FW do it's magic. GIve it up to 5 seconds... */
2126255736Sdavidch    do {
2127255736Sdavidch        DELAY(delay * 1000);
2128255736Sdavidch        rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2129255736Sdavidch    } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2130255736Sdavidch
2131255736Sdavidch    BLOGD(sc, DBG_PHY,
2132255736Sdavidch          "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2133255736Sdavidch          cnt*delay, rc, seq);
2134255736Sdavidch
2135255736Sdavidch    /* is this a reply to our command? */
2136255736Sdavidch    if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2137255736Sdavidch        rc &= FW_MSG_CODE_MASK;
2138255736Sdavidch    } else {
2139255736Sdavidch        /* Ruh-roh! */
2140255736Sdavidch        BLOGE(sc, "FW failed to respond!\n");
2141255736Sdavidch        // XXX bxe_fw_dump(sc);
2142255736Sdavidch        rc = 0;
2143255736Sdavidch    }
2144255736Sdavidch
2145255736Sdavidch    BXE_FWMB_UNLOCK(sc);
2146255736Sdavidch    return (rc);
2147255736Sdavidch}
2148255736Sdavidch
2149255736Sdavidchstatic uint32_t
2150255736Sdavidchbxe_fw_command(struct bxe_softc *sc,
2151255736Sdavidch               uint32_t         command,
2152255736Sdavidch               uint32_t         param)
2153255736Sdavidch{
2154255736Sdavidch    return (elink_cb_fw_command(sc, command, param));
2155255736Sdavidch}
2156255736Sdavidch
2157255736Sdavidchstatic void
2158255736Sdavidch__storm_memset_dma_mapping(struct bxe_softc *sc,
2159255736Sdavidch                           uint32_t         addr,
2160255736Sdavidch                           bus_addr_t       mapping)
2161255736Sdavidch{
2162255736Sdavidch    REG_WR(sc, addr, U64_LO(mapping));
2163255736Sdavidch    REG_WR(sc, (addr + 4), U64_HI(mapping));
2164255736Sdavidch}
2165255736Sdavidch
2166255736Sdavidchstatic void
2167255736Sdavidchstorm_memset_spq_addr(struct bxe_softc *sc,
2168255736Sdavidch                      bus_addr_t       mapping,
2169255736Sdavidch                      uint16_t         abs_fid)
2170255736Sdavidch{
2171255736Sdavidch    uint32_t addr = (XSEM_REG_FAST_MEMORY +
2172255736Sdavidch                     XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2173255736Sdavidch    __storm_memset_dma_mapping(sc, addr, mapping);
2174255736Sdavidch}
2175255736Sdavidch
2176255736Sdavidchstatic void
2177255736Sdavidchstorm_memset_vf_to_pf(struct bxe_softc *sc,
2178255736Sdavidch                      uint16_t         abs_fid,
2179255736Sdavidch                      uint16_t         pf_id)
2180255736Sdavidch{
2181255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2182255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2183255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2184255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2185255736Sdavidch}
2186255736Sdavidch
2187255736Sdavidchstatic void
2188255736Sdavidchstorm_memset_func_en(struct bxe_softc *sc,
2189255736Sdavidch                     uint16_t         abs_fid,
2190255736Sdavidch                     uint8_t          enable)
2191255736Sdavidch{
2192255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2193255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2194255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2195255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2196255736Sdavidch}
2197255736Sdavidch
2198255736Sdavidchstatic void
2199255736Sdavidchstorm_memset_eq_data(struct bxe_softc       *sc,
2200255736Sdavidch                     struct event_ring_data *eq_data,
2201255736Sdavidch                     uint16_t               pfid)
2202255736Sdavidch{
2203255736Sdavidch    uint32_t addr;
2204255736Sdavidch    size_t size;
2205255736Sdavidch
2206255736Sdavidch    addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2207255736Sdavidch    size = sizeof(struct event_ring_data);
2208255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2209255736Sdavidch}
2210255736Sdavidch
2211255736Sdavidchstatic void
2212255736Sdavidchstorm_memset_eq_prod(struct bxe_softc *sc,
2213255736Sdavidch                     uint16_t         eq_prod,
2214255736Sdavidch                     uint16_t         pfid)
2215255736Sdavidch{
2216255736Sdavidch    uint32_t addr = (BAR_CSTRORM_INTMEM +
2217255736Sdavidch                     CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2218255736Sdavidch    REG_WR16(sc, addr, eq_prod);
2219255736Sdavidch}
2220255736Sdavidch
2221255736Sdavidch/*
2222255736Sdavidch * Post a slowpath command.
2223255736Sdavidch *
2224298955Spfg * A slowpath command is used to propagate a configuration change through
2225255736Sdavidch * the controller in a controlled manner, allowing each STORM processor and
2226255736Sdavidch * other H/W blocks to phase in the change.  The commands sent on the
2227255736Sdavidch * slowpath are referred to as ramrods.  Depending on the ramrod used the
2228255736Sdavidch * completion of the ramrod will occur in different ways.  Here's a
2229255736Sdavidch * breakdown of ramrods and how they complete:
2230255736Sdavidch *
2231255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_SETUP
2232255736Sdavidch *   Used to setup the leading connection on a port.  Completes on the
2233255736Sdavidch *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2234255736Sdavidch *
2235255736Sdavidch * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2236255736Sdavidch *   Used to setup an additional connection on a port.  Completes on the
2237255736Sdavidch *   RCQ of the multi-queue/RSS connection being initialized.
2238255736Sdavidch *
2239255736Sdavidch * RAMROD_CMD_ID_ETH_STAT_QUERY
2240255736Sdavidch *   Used to force the storm processors to update the statistics database
2241255736Sdavidch *   in host memory.  This ramrod is send on the leading connection CID and
2242255736Sdavidch *   completes as an index increment of the CSTORM on the default status
2243255736Sdavidch *   block.
2244255736Sdavidch *
2245255736Sdavidch * RAMROD_CMD_ID_ETH_UPDATE
2246255736Sdavidch *   Used to update the state of the leading connection, usually to udpate
2247255736Sdavidch *   the RSS indirection table.  Completes on the RCQ of the leading
2248255736Sdavidch *   connection. (Not currently used under FreeBSD until OS support becomes
2249255736Sdavidch *   available.)
2250255736Sdavidch *
2251255736Sdavidch * RAMROD_CMD_ID_ETH_HALT
2252255736Sdavidch *   Used when tearing down a connection prior to driver unload.  Completes
2253255736Sdavidch *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2254255736Sdavidch *   use this on the leading connection.
2255255736Sdavidch *
2256255736Sdavidch * RAMROD_CMD_ID_ETH_SET_MAC
2257255736Sdavidch *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2258255736Sdavidch *   the RCQ of the leading connection.
2259255736Sdavidch *
2260255736Sdavidch * RAMROD_CMD_ID_ETH_CFC_DEL
2261255736Sdavidch *   Used when tearing down a conneciton prior to driver unload.  Completes
2262255736Sdavidch *   on the RCQ of the leading connection (since the current connection
2263255736Sdavidch *   has been completely removed from controller memory).
2264255736Sdavidch *
2265255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_DEL
2266255736Sdavidch *   Used to tear down the leading connection prior to driver unload,
2267255736Sdavidch *   typically fp[0].  Completes as an index increment of the CSTORM on the
2268255736Sdavidch *   default status block.
2269255736Sdavidch *
2270255736Sdavidch * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2271255736Sdavidch *   Used for connection offload.  Completes on the RCQ of the multi-queue
2272255736Sdavidch *   RSS connection that is being offloaded.  (Not currently used under
2273255736Sdavidch *   FreeBSD.)
2274255736Sdavidch *
2275255736Sdavidch * There can only be one command pending per function.
2276255736Sdavidch *
2277255736Sdavidch * Returns:
2278255736Sdavidch *   0 = Success, !0 = Failure.
2279255736Sdavidch */
2280255736Sdavidch
2281255736Sdavidch/* must be called under the spq lock */
2282255736Sdavidchstatic inline
2283255736Sdavidchstruct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2284255736Sdavidch{
2285255736Sdavidch    struct eth_spe *next_spe = sc->spq_prod_bd;
2286255736Sdavidch
2287255736Sdavidch    if (sc->spq_prod_bd == sc->spq_last_bd) {
2288255736Sdavidch        /* wrap back to the first eth_spq */
2289255736Sdavidch        sc->spq_prod_bd = sc->spq;
2290255736Sdavidch        sc->spq_prod_idx = 0;
2291255736Sdavidch    } else {
2292255736Sdavidch        sc->spq_prod_bd++;
2293255736Sdavidch        sc->spq_prod_idx++;
2294255736Sdavidch    }
2295255736Sdavidch
2296255736Sdavidch    return (next_spe);
2297255736Sdavidch}
2298255736Sdavidch
2299255736Sdavidch/* must be called under the spq lock */
2300255736Sdavidchstatic inline
2301255736Sdavidchvoid bxe_sp_prod_update(struct bxe_softc *sc)
2302255736Sdavidch{
2303255736Sdavidch    int func = SC_FUNC(sc);
2304255736Sdavidch
2305255736Sdavidch    /*
2306255736Sdavidch     * Make sure that BD data is updated before writing the producer.
2307255736Sdavidch     * BD data is written to the memory, the producer is read from the
2308255736Sdavidch     * memory, thus we need a full memory barrier to ensure the ordering.
2309255736Sdavidch     */
2310255736Sdavidch    mb();
2311255736Sdavidch
2312255736Sdavidch    REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2313255736Sdavidch             sc->spq_prod_idx);
2314255736Sdavidch
2315255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2316255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
2317255736Sdavidch}
2318255736Sdavidch
2319255736Sdavidch/**
2320255736Sdavidch * bxe_is_contextless_ramrod - check if the current command ends on EQ
2321255736Sdavidch *
2322255736Sdavidch * @cmd:      command to check
2323255736Sdavidch * @cmd_type: command type
2324255736Sdavidch */
2325255736Sdavidchstatic inline
2326255736Sdavidchint bxe_is_contextless_ramrod(int cmd,
2327255736Sdavidch                              int cmd_type)
2328255736Sdavidch{
2329255736Sdavidch    if ((cmd_type == NONE_CONNECTION_TYPE) ||
2330255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2331255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2332255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2333255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2334255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2335255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2336255736Sdavidch        return (TRUE);
2337255736Sdavidch    } else {
2338255736Sdavidch        return (FALSE);
2339255736Sdavidch    }
2340255736Sdavidch}
2341255736Sdavidch
2342255736Sdavidch/**
2343255736Sdavidch * bxe_sp_post - place a single command on an SP ring
2344255736Sdavidch *
2345255736Sdavidch * @sc:         driver handle
2346255736Sdavidch * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2347255736Sdavidch * @cid:        SW CID the command is related to
2348255736Sdavidch * @data_hi:    command private data address (high 32 bits)
2349255736Sdavidch * @data_lo:    command private data address (low 32 bits)
2350255736Sdavidch * @cmd_type:   command type (e.g. NONE, ETH)
2351255736Sdavidch *
2352255736Sdavidch * SP data is handled as if it's always an address pair, thus data fields are
2353255736Sdavidch * not swapped to little endian in upper functions. Instead this function swaps
2354255736Sdavidch * data as if it's two uint32 fields.
2355255736Sdavidch */
2356255736Sdavidchint
2357255736Sdavidchbxe_sp_post(struct bxe_softc *sc,
2358255736Sdavidch            int              command,
2359255736Sdavidch            int              cid,
2360255736Sdavidch            uint32_t         data_hi,
2361255736Sdavidch            uint32_t         data_lo,
2362255736Sdavidch            int              cmd_type)
2363255736Sdavidch{
2364255736Sdavidch    struct eth_spe *spe;
2365255736Sdavidch    uint16_t type;
2366255736Sdavidch    int common;
2367255736Sdavidch
2368255736Sdavidch    common = bxe_is_contextless_ramrod(command, cmd_type);
2369255736Sdavidch
2370255736Sdavidch    BXE_SP_LOCK(sc);
2371255736Sdavidch
2372255736Sdavidch    if (common) {
2373255736Sdavidch        if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2374255736Sdavidch            BLOGE(sc, "EQ ring is full!\n");
2375255736Sdavidch            BXE_SP_UNLOCK(sc);
2376255736Sdavidch            return (-1);
2377255736Sdavidch        }
2378255736Sdavidch    } else {
2379255736Sdavidch        if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2380255736Sdavidch            BLOGE(sc, "SPQ ring is full!\n");
2381255736Sdavidch            BXE_SP_UNLOCK(sc);
2382255736Sdavidch            return (-1);
2383255736Sdavidch        }
2384255736Sdavidch    }
2385255736Sdavidch
2386255736Sdavidch    spe = bxe_sp_get_next(sc);
2387255736Sdavidch
2388255736Sdavidch    /* CID needs port number to be encoded int it */
2389255736Sdavidch    spe->hdr.conn_and_cmd_data =
2390296071Sdavidcs        htole32((command << SPE_HDR_T_CMD_ID_SHIFT) | HW_CID(sc, cid));
2391255736Sdavidch
2392296071Sdavidcs    type = (cmd_type << SPE_HDR_T_CONN_TYPE_SHIFT) & SPE_HDR_T_CONN_TYPE;
2393255736Sdavidch
2394255736Sdavidch    /* TBD: Check if it works for VFs */
2395296071Sdavidcs    type |= ((SC_FUNC(sc) << SPE_HDR_T_FUNCTION_ID_SHIFT) &
2396296071Sdavidcs             SPE_HDR_T_FUNCTION_ID);
2397255736Sdavidch
2398255736Sdavidch    spe->hdr.type = htole16(type);
2399255736Sdavidch
2400255736Sdavidch    spe->data.update_data_addr.hi = htole32(data_hi);
2401255736Sdavidch    spe->data.update_data_addr.lo = htole32(data_lo);
2402255736Sdavidch
2403255736Sdavidch    /*
2404255736Sdavidch     * It's ok if the actual decrement is issued towards the memory
2405255736Sdavidch     * somewhere between the lock and unlock. Thus no more explict
2406255736Sdavidch     * memory barrier is needed.
2407255736Sdavidch     */
2408255736Sdavidch    if (common) {
2409255736Sdavidch        atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2410255736Sdavidch    } else {
2411255736Sdavidch        atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2412255736Sdavidch    }
2413255736Sdavidch
2414256341Sdim    BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2415256341Sdim    BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2416256341Sdim          BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2417255736Sdavidch    BLOGD(sc, DBG_SP,
2418255736Sdavidch          "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2419255736Sdavidch          sc->spq_prod_idx,
2420255736Sdavidch          (uint32_t)U64_HI(sc->spq_dma.paddr),
2421255736Sdavidch          (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2422255736Sdavidch          command,
2423255736Sdavidch          common,
2424255736Sdavidch          HW_CID(sc, cid),
2425255736Sdavidch          data_hi,
2426255736Sdavidch          data_lo,
2427255736Sdavidch          type,
2428255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left),
2429255736Sdavidch          atomic_load_acq_long(&sc->eq_spq_left));
2430255736Sdavidch
2431255736Sdavidch    bxe_sp_prod_update(sc);
2432255736Sdavidch
2433255736Sdavidch    BXE_SP_UNLOCK(sc);
2434255736Sdavidch    return (0);
2435255736Sdavidch}
2436255736Sdavidch
2437255736Sdavidch/**
2438255736Sdavidch * bxe_debug_print_ind_table - prints the indirection table configuration.
2439255736Sdavidch *
2440255736Sdavidch * @sc: driver hanlde
2441255736Sdavidch * @p:  pointer to rss configuration
2442255736Sdavidch */
2443255736Sdavidch
2444255736Sdavidch/*
2445255736Sdavidch * FreeBSD Device probe function.
2446255736Sdavidch *
2447255736Sdavidch * Compares the device found to the driver's list of supported devices and
2448255736Sdavidch * reports back to the bsd loader whether this is the right driver for the device.
2449255736Sdavidch * This is the driver entry function called from the "kldload" command.
2450255736Sdavidch *
2451255736Sdavidch * Returns:
2452255736Sdavidch *   BUS_PROBE_DEFAULT on success, positive value on failure.
2453255736Sdavidch */
2454255736Sdavidchstatic int
2455255736Sdavidchbxe_probe(device_t dev)
2456255736Sdavidch{
2457255736Sdavidch    struct bxe_softc *sc;
2458255736Sdavidch    struct bxe_device_type *t;
2459255736Sdavidch    char *descbuf;
2460255736Sdavidch    uint16_t did, sdid, svid, vid;
2461255736Sdavidch
2462255736Sdavidch    /* Find our device structure */
2463255736Sdavidch    sc = device_get_softc(dev);
2464255736Sdavidch    sc->dev = dev;
2465255736Sdavidch    t = bxe_devs;
2466255736Sdavidch
2467255736Sdavidch    /* Get the data for the device to be probed. */
2468255736Sdavidch    vid  = pci_get_vendor(dev);
2469255736Sdavidch    did  = pci_get_device(dev);
2470255736Sdavidch    svid = pci_get_subvendor(dev);
2471255736Sdavidch    sdid = pci_get_subdevice(dev);
2472255736Sdavidch
2473255736Sdavidch    BLOGD(sc, DBG_LOAD,
2474255736Sdavidch          "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
2475255736Sdavidch          "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
2476255736Sdavidch
2477255736Sdavidch    /* Look through the list of known devices for a match. */
2478255736Sdavidch    while (t->bxe_name != NULL) {
2479255736Sdavidch        if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2480255736Sdavidch            ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2481255736Sdavidch            ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2482255736Sdavidch            descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2483255736Sdavidch            if (descbuf == NULL)
2484255736Sdavidch                return (ENOMEM);
2485255736Sdavidch
2486255736Sdavidch            /* Print out the device identity. */
2487255736Sdavidch            snprintf(descbuf, BXE_DEVDESC_MAX,
2488255736Sdavidch                     "%s (%c%d) BXE v:%s\n", t->bxe_name,
2489255736Sdavidch                     (((pci_read_config(dev, PCIR_REVID, 4) &
2490255736Sdavidch                        0xf0) >> 4) + 'A'),
2491255736Sdavidch                     (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2492255736Sdavidch                     BXE_DRIVER_VERSION);
2493255736Sdavidch
2494255736Sdavidch            device_set_desc_copy(dev, descbuf);
2495255736Sdavidch            free(descbuf, M_TEMP);
2496255736Sdavidch            return (BUS_PROBE_DEFAULT);
2497255736Sdavidch        }
2498255736Sdavidch        t++;
2499255736Sdavidch    }
2500255736Sdavidch
2501255736Sdavidch    return (ENXIO);
2502255736Sdavidch}
2503255736Sdavidch
2504255736Sdavidchstatic void
2505255736Sdavidchbxe_init_mutexes(struct bxe_softc *sc)
2506255736Sdavidch{
2507255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2508255736Sdavidch    snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2509255736Sdavidch             "bxe%d_core_lock", sc->unit);
2510255736Sdavidch    sx_init(&sc->core_sx, sc->core_sx_name);
2511255736Sdavidch#else
2512255736Sdavidch    snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2513255736Sdavidch             "bxe%d_core_lock", sc->unit);
2514255736Sdavidch    mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2515255736Sdavidch#endif
2516255736Sdavidch
2517255736Sdavidch    snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2518255736Sdavidch             "bxe%d_sp_lock", sc->unit);
2519255736Sdavidch    mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2520255736Sdavidch
2521255736Sdavidch    snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2522255736Sdavidch             "bxe%d_dmae_lock", sc->unit);
2523255736Sdavidch    mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2524255736Sdavidch
2525255736Sdavidch    snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2526255736Sdavidch             "bxe%d_phy_lock", sc->unit);
2527255736Sdavidch    mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2528255736Sdavidch
2529255736Sdavidch    snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2530255736Sdavidch             "bxe%d_fwmb_lock", sc->unit);
2531255736Sdavidch    mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2532255736Sdavidch
2533255736Sdavidch    snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2534255736Sdavidch             "bxe%d_print_lock", sc->unit);
2535255736Sdavidch    mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2536255736Sdavidch
2537255736Sdavidch    snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2538255736Sdavidch             "bxe%d_stats_lock", sc->unit);
2539255736Sdavidch    mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2540255736Sdavidch
2541255736Sdavidch    snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2542255736Sdavidch             "bxe%d_mcast_lock", sc->unit);
2543255736Sdavidch    mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2544255736Sdavidch}
2545255736Sdavidch
2546255736Sdavidchstatic void
2547255736Sdavidchbxe_release_mutexes(struct bxe_softc *sc)
2548255736Sdavidch{
2549255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2550255736Sdavidch    sx_destroy(&sc->core_sx);
2551255736Sdavidch#else
2552255736Sdavidch    if (mtx_initialized(&sc->core_mtx)) {
2553255736Sdavidch        mtx_destroy(&sc->core_mtx);
2554255736Sdavidch    }
2555255736Sdavidch#endif
2556255736Sdavidch
2557255736Sdavidch    if (mtx_initialized(&sc->sp_mtx)) {
2558255736Sdavidch        mtx_destroy(&sc->sp_mtx);
2559255736Sdavidch    }
2560255736Sdavidch
2561255736Sdavidch    if (mtx_initialized(&sc->dmae_mtx)) {
2562255736Sdavidch        mtx_destroy(&sc->dmae_mtx);
2563255736Sdavidch    }
2564255736Sdavidch
2565255736Sdavidch    if (mtx_initialized(&sc->port.phy_mtx)) {
2566255736Sdavidch        mtx_destroy(&sc->port.phy_mtx);
2567255736Sdavidch    }
2568255736Sdavidch
2569255736Sdavidch    if (mtx_initialized(&sc->fwmb_mtx)) {
2570255736Sdavidch        mtx_destroy(&sc->fwmb_mtx);
2571255736Sdavidch    }
2572255736Sdavidch
2573255736Sdavidch    if (mtx_initialized(&sc->print_mtx)) {
2574255736Sdavidch        mtx_destroy(&sc->print_mtx);
2575255736Sdavidch    }
2576255736Sdavidch
2577255736Sdavidch    if (mtx_initialized(&sc->stats_mtx)) {
2578255736Sdavidch        mtx_destroy(&sc->stats_mtx);
2579255736Sdavidch    }
2580255736Sdavidch
2581255736Sdavidch    if (mtx_initialized(&sc->mcast_mtx)) {
2582255736Sdavidch        mtx_destroy(&sc->mcast_mtx);
2583255736Sdavidch    }
2584255736Sdavidch}
2585255736Sdavidch
2586255736Sdavidchstatic void
2587255736Sdavidchbxe_tx_disable(struct bxe_softc* sc)
2588255736Sdavidch{
2589266979Smarcel    if_t ifp = sc->ifp;
2590255736Sdavidch
2591255736Sdavidch    /* tell the stack the driver is stopped and TX queue is full */
2592266979Smarcel    if (ifp !=  NULL) {
2593266979Smarcel        if_setdrvflags(ifp, 0);
2594255736Sdavidch    }
2595255736Sdavidch}
2596255736Sdavidch
2597255736Sdavidchstatic void
2598255736Sdavidchbxe_drv_pulse(struct bxe_softc *sc)
2599255736Sdavidch{
2600255736Sdavidch    SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2601255736Sdavidch             sc->fw_drv_pulse_wr_seq);
2602255736Sdavidch}
2603255736Sdavidch
2604255736Sdavidchstatic inline uint16_t
2605255736Sdavidchbxe_tx_avail(struct bxe_softc *sc,
2606255736Sdavidch             struct bxe_fastpath *fp)
2607255736Sdavidch{
2608255736Sdavidch    int16_t  used;
2609255736Sdavidch    uint16_t prod;
2610255736Sdavidch    uint16_t cons;
2611255736Sdavidch
2612255736Sdavidch    prod = fp->tx_bd_prod;
2613255736Sdavidch    cons = fp->tx_bd_cons;
2614255736Sdavidch
2615255736Sdavidch    used = SUB_S16(prod, cons);
2616255736Sdavidch
2617255736Sdavidch    return (int16_t)(sc->tx_ring_size) - used;
2618255736Sdavidch}
2619255736Sdavidch
2620255736Sdavidchstatic inline int
2621255736Sdavidchbxe_tx_queue_has_work(struct bxe_fastpath *fp)
2622255736Sdavidch{
2623255736Sdavidch    uint16_t hw_cons;
2624255736Sdavidch
2625255736Sdavidch    mb(); /* status block fields can change */
2626255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
2627255736Sdavidch    return (hw_cons != fp->tx_pkt_cons);
2628255736Sdavidch}
2629255736Sdavidch
2630255736Sdavidchstatic inline uint8_t
2631255736Sdavidchbxe_has_tx_work(struct bxe_fastpath *fp)
2632255736Sdavidch{
2633255736Sdavidch    /* expand this for multi-cos if ever supported */
2634255736Sdavidch    return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2635255736Sdavidch}
2636255736Sdavidch
2637255736Sdavidchstatic inline int
2638255736Sdavidchbxe_has_rx_work(struct bxe_fastpath *fp)
2639255736Sdavidch{
2640255736Sdavidch    uint16_t rx_cq_cons_sb;
2641255736Sdavidch
2642255736Sdavidch    mb(); /* status block fields can change */
2643255736Sdavidch    rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2644255736Sdavidch    if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2645255736Sdavidch        rx_cq_cons_sb++;
2646255736Sdavidch    return (fp->rx_cq_cons != rx_cq_cons_sb);
2647255736Sdavidch}
2648255736Sdavidch
2649255736Sdavidchstatic void
2650255736Sdavidchbxe_sp_event(struct bxe_softc    *sc,
2651255736Sdavidch             struct bxe_fastpath *fp,
2652255736Sdavidch             union eth_rx_cqe    *rr_cqe)
2653255736Sdavidch{
2654255736Sdavidch    int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2655255736Sdavidch    int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2656255736Sdavidch    enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2657255736Sdavidch    struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2658255736Sdavidch
2659255736Sdavidch    BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2660255736Sdavidch          fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2661255736Sdavidch
2662255736Sdavidch    switch (command) {
2663255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2664255736Sdavidch        BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2665255736Sdavidch        drv_cmd = ECORE_Q_CMD_UPDATE;
2666255736Sdavidch        break;
2667255736Sdavidch
2668255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2669255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2670255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP;
2671255736Sdavidch        break;
2672255736Sdavidch
2673255736Sdavidch    case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2674255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2675255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2676255736Sdavidch        break;
2677255736Sdavidch
2678255736Sdavidch    case (RAMROD_CMD_ID_ETH_HALT):
2679255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2680255736Sdavidch        drv_cmd = ECORE_Q_CMD_HALT;
2681255736Sdavidch        break;
2682255736Sdavidch
2683255736Sdavidch    case (RAMROD_CMD_ID_ETH_TERMINATE):
2684255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2685255736Sdavidch        drv_cmd = ECORE_Q_CMD_TERMINATE;
2686255736Sdavidch        break;
2687255736Sdavidch
2688255736Sdavidch    case (RAMROD_CMD_ID_ETH_EMPTY):
2689255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2690255736Sdavidch        drv_cmd = ECORE_Q_CMD_EMPTY;
2691255736Sdavidch        break;
2692255736Sdavidch
2693255736Sdavidch    default:
2694255736Sdavidch        BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2695255736Sdavidch              command, fp->index);
2696255736Sdavidch        return;
2697255736Sdavidch    }
2698255736Sdavidch
2699255736Sdavidch    if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2700255736Sdavidch        q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2701255736Sdavidch        /*
2702255736Sdavidch         * q_obj->complete_cmd() failure means that this was
2703255736Sdavidch         * an unexpected completion.
2704255736Sdavidch         *
2705255736Sdavidch         * In this case we don't want to increase the sc->spq_left
2706255736Sdavidch         * because apparently we haven't sent this command the first
2707255736Sdavidch         * place.
2708255736Sdavidch         */
2709255736Sdavidch        // bxe_panic(sc, ("Unexpected SP completion\n"));
2710255736Sdavidch        return;
2711255736Sdavidch    }
2712255736Sdavidch
2713255736Sdavidch    atomic_add_acq_long(&sc->cq_spq_left, 1);
2714255736Sdavidch
2715255736Sdavidch    BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2716255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left));
2717255736Sdavidch}
2718255736Sdavidch
2719255736Sdavidch/*
2720255736Sdavidch * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2721255736Sdavidch * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2722255736Sdavidch * the current aggregation queue as in-progress.
2723255736Sdavidch */
2724255736Sdavidchstatic void
2725255736Sdavidchbxe_tpa_start(struct bxe_softc            *sc,
2726255736Sdavidch              struct bxe_fastpath         *fp,
2727255736Sdavidch              uint16_t                    queue,
2728255736Sdavidch              uint16_t                    cons,
2729255736Sdavidch              uint16_t                    prod,
2730255736Sdavidch              struct eth_fast_path_rx_cqe *cqe)
2731255736Sdavidch{
2732255736Sdavidch    struct bxe_sw_rx_bd tmp_bd;
2733255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
2734255736Sdavidch    struct eth_rx_bd *rx_bd;
2735255736Sdavidch    int max_agg_queues;
2736255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2737255736Sdavidch    uint16_t index;
2738255736Sdavidch
2739255736Sdavidch    BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2740255736Sdavidch                       "cons=%d prod=%d\n",
2741255736Sdavidch          fp->index, queue, cons, prod);
2742255736Sdavidch
2743255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
2744255736Sdavidch
2745255736Sdavidch    KASSERT((queue < max_agg_queues),
2746255736Sdavidch            ("fp[%02d] invalid aggr queue (%d >= %d)!",
2747255736Sdavidch             fp->index, queue, max_agg_queues));
2748255736Sdavidch
2749255736Sdavidch    KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2750255736Sdavidch            ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2751255736Sdavidch             fp->index, queue));
2752255736Sdavidch
2753255736Sdavidch    /* copy the existing mbuf and mapping from the TPA pool */
2754255736Sdavidch    tmp_bd = tpa_info->bd;
2755255736Sdavidch
2756255736Sdavidch    if (tmp_bd.m == NULL) {
2757295830Sdavidcs        uint32_t *tmp;
2758295830Sdavidcs
2759295830Sdavidcs        tmp = (uint32_t *)cqe;
2760295830Sdavidcs
2761295830Sdavidcs        BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2762295830Sdavidcs              fp->index, queue, cons, prod);
2763295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2764295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2765295830Sdavidcs
2766255736Sdavidch        /* XXX Error handling? */
2767255736Sdavidch        return;
2768255736Sdavidch    }
2769255736Sdavidch
2770255736Sdavidch    /* change the TPA queue to the start state */
2771255736Sdavidch    tpa_info->state            = BXE_TPA_STATE_START;
2772255736Sdavidch    tpa_info->placement_offset = cqe->placement_offset;
2773255736Sdavidch    tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2774255736Sdavidch    tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2775255736Sdavidch    tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2776255736Sdavidch
2777255736Sdavidch    fp->rx_tpa_queue_used |= (1 << queue);
2778255736Sdavidch
2779255736Sdavidch    /*
2780255736Sdavidch     * If all the buffer descriptors are filled with mbufs then fill in
2781255736Sdavidch     * the current consumer index with a new BD. Else if a maximum Rx
2782255736Sdavidch     * buffer limit is imposed then fill in the next producer index.
2783255736Sdavidch     */
2784255736Sdavidch    index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2785255736Sdavidch                prod : cons;
2786255736Sdavidch
2787255736Sdavidch    /* move the received mbuf and mapping to TPA pool */
2788255736Sdavidch    tpa_info->bd = fp->rx_mbuf_chain[cons];
2789255736Sdavidch
2790255736Sdavidch    /* release any existing RX BD mbuf mappings */
2791255736Sdavidch    if (cons != index) {
2792255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[cons];
2793255736Sdavidch
2794255736Sdavidch        if (rx_buf->m_map != NULL) {
2795255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2796255736Sdavidch                            BUS_DMASYNC_POSTREAD);
2797255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2798255736Sdavidch        }
2799255736Sdavidch
2800255736Sdavidch        /*
2801255736Sdavidch         * We get here when the maximum number of rx buffers is less than
2802255736Sdavidch         * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2803255736Sdavidch         * it out here without concern of a memory leak.
2804255736Sdavidch         */
2805255736Sdavidch        fp->rx_mbuf_chain[cons].m = NULL;
2806255736Sdavidch    }
2807255736Sdavidch
2808255736Sdavidch    /* update the Rx SW BD with the mbuf info from the TPA pool */
2809255736Sdavidch    fp->rx_mbuf_chain[index] = tmp_bd;
2810255736Sdavidch
2811255736Sdavidch    /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2812255736Sdavidch    rx_bd = &fp->rx_chain[index];
2813255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2814255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2815255736Sdavidch}
2816255736Sdavidch
2817255736Sdavidch/*
2818255736Sdavidch * When a TPA aggregation is completed, loop through the individual mbufs
2819255736Sdavidch * of the aggregation, combining them into a single mbuf which will be sent
2820255736Sdavidch * up the stack. Refill all freed SGEs with mbufs as we go along.
2821255736Sdavidch */
2822255736Sdavidchstatic int
2823255736Sdavidchbxe_fill_frag_mbuf(struct bxe_softc          *sc,
2824255736Sdavidch                   struct bxe_fastpath       *fp,
2825255736Sdavidch                   struct bxe_sw_tpa_info    *tpa_info,
2826255736Sdavidch                   uint16_t                  queue,
2827255736Sdavidch                   uint16_t                  pages,
2828255736Sdavidch                   struct mbuf               *m,
2829255736Sdavidch			       struct eth_end_agg_rx_cqe *cqe,
2830255736Sdavidch                   uint16_t                  cqe_idx)
2831255736Sdavidch{
2832255736Sdavidch    struct mbuf *m_frag;
2833255736Sdavidch    uint32_t frag_len, frag_size, i;
2834255736Sdavidch    uint16_t sge_idx;
2835255736Sdavidch    int rc = 0;
2836255736Sdavidch    int j;
2837255736Sdavidch
2838255736Sdavidch    frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2839255736Sdavidch
2840255736Sdavidch    BLOGD(sc, DBG_LRO,
2841255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2842255736Sdavidch          fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2843255736Sdavidch
2844255736Sdavidch    /* make sure the aggregated frame is not too big to handle */
2845255736Sdavidch    if (pages > 8 * PAGES_PER_SGE) {
2846295830Sdavidcs
2847295830Sdavidcs        uint32_t *tmp = (uint32_t *)cqe;
2848295830Sdavidcs
2849255736Sdavidch        BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2850255736Sdavidch                  "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2851255736Sdavidch              fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2852255736Sdavidch              tpa_info->len_on_bd, frag_size);
2853295830Sdavidcs
2854295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2855295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2856295830Sdavidcs
2857255736Sdavidch        bxe_panic(sc, ("sge page count error\n"));
2858255736Sdavidch        return (EINVAL);
2859255736Sdavidch    }
2860255736Sdavidch
2861255736Sdavidch    /*
2862255736Sdavidch     * Scan through the scatter gather list pulling individual mbufs into a
2863255736Sdavidch     * single mbuf for the host stack.
2864255736Sdavidch     */
2865255736Sdavidch    for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2866255736Sdavidch        sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2867255736Sdavidch
2868255736Sdavidch        /*
2869255736Sdavidch         * Firmware gives the indices of the SGE as if the ring is an array
2870255736Sdavidch         * (meaning that the "next" element will consume 2 indices).
2871255736Sdavidch         */
2872255736Sdavidch        frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2873255736Sdavidch
2874255736Sdavidch        BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2875255736Sdavidch                           "sge_idx=%d frag_size=%d frag_len=%d\n",
2876255736Sdavidch              fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2877255736Sdavidch
2878255736Sdavidch        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2879255736Sdavidch
2880255736Sdavidch        /* allocate a new mbuf for the SGE */
2881255736Sdavidch        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2882255736Sdavidch        if (rc) {
2883255736Sdavidch            /* Leave all remaining SGEs in the ring! */
2884255736Sdavidch            return (rc);
2885255736Sdavidch        }
2886255736Sdavidch
2887255736Sdavidch        /* update the fragment length */
2888255736Sdavidch        m_frag->m_len = frag_len;
2889255736Sdavidch
2890255736Sdavidch        /* concatenate the fragment to the head mbuf */
2891255736Sdavidch        m_cat(m, m_frag);
2892255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
2893255736Sdavidch
2894255736Sdavidch        /* update the TPA mbuf size and remaining fragment size */
2895255736Sdavidch        m->m_pkthdr.len += frag_len;
2896255736Sdavidch        frag_size -= frag_len;
2897255736Sdavidch    }
2898255736Sdavidch
2899255736Sdavidch    BLOGD(sc, DBG_LRO,
2900255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2901255736Sdavidch          fp->index, queue, frag_size);
2902255736Sdavidch
2903255736Sdavidch    return (rc);
2904255736Sdavidch}
2905255736Sdavidch
2906255736Sdavidchstatic inline void
2907255736Sdavidchbxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2908255736Sdavidch{
2909255736Sdavidch    int i, j;
2910255736Sdavidch
2911255736Sdavidch    for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2912255736Sdavidch        int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2913255736Sdavidch
2914255736Sdavidch        for (j = 0; j < 2; j++) {
2915255736Sdavidch            BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2916255736Sdavidch            idx--;
2917255736Sdavidch        }
2918255736Sdavidch    }
2919255736Sdavidch}
2920255736Sdavidch
2921255736Sdavidchstatic inline void
2922255736Sdavidchbxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2923255736Sdavidch{
2924255736Sdavidch    /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2925255736Sdavidch    memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2926255736Sdavidch
2927255736Sdavidch    /*
2928255736Sdavidch     * Clear the two last indices in the page to 1. These are the indices that
2929255736Sdavidch     * correspond to the "next" element, hence will never be indicated and
2930255736Sdavidch     * should be removed from the calculations.
2931255736Sdavidch     */
2932255736Sdavidch    bxe_clear_sge_mask_next_elems(fp);
2933255736Sdavidch}
2934255736Sdavidch
2935255736Sdavidchstatic inline void
2936255736Sdavidchbxe_update_last_max_sge(struct bxe_fastpath *fp,
2937255736Sdavidch                        uint16_t            idx)
2938255736Sdavidch{
2939255736Sdavidch    uint16_t last_max = fp->last_max_sge;
2940255736Sdavidch
2941255736Sdavidch    if (SUB_S16(idx, last_max) > 0) {
2942255736Sdavidch        fp->last_max_sge = idx;
2943255736Sdavidch    }
2944255736Sdavidch}
2945255736Sdavidch
2946255736Sdavidchstatic inline void
2947255736Sdavidchbxe_update_sge_prod(struct bxe_softc          *sc,
2948255736Sdavidch                    struct bxe_fastpath       *fp,
2949255736Sdavidch                    uint16_t                  sge_len,
2950283269Sdavidcs                    union eth_sgl_or_raw_data *cqe)
2951255736Sdavidch{
2952255736Sdavidch    uint16_t last_max, last_elem, first_elem;
2953255736Sdavidch    uint16_t delta = 0;
2954255736Sdavidch    uint16_t i;
2955255736Sdavidch
2956255736Sdavidch    if (!sge_len) {
2957255736Sdavidch        return;
2958255736Sdavidch    }
2959255736Sdavidch
2960255736Sdavidch    /* first mark all used pages */
2961255736Sdavidch    for (i = 0; i < sge_len; i++) {
2962255736Sdavidch        BIT_VEC64_CLEAR_BIT(fp->sge_mask,
2963283269Sdavidcs                            RX_SGE(le16toh(cqe->sgl[i])));
2964255736Sdavidch    }
2965255736Sdavidch
2966255736Sdavidch    BLOGD(sc, DBG_LRO,
2967255736Sdavidch          "fp[%02d] fp_cqe->sgl[%d] = %d\n",
2968255736Sdavidch          fp->index, sge_len - 1,
2969283269Sdavidcs          le16toh(cqe->sgl[sge_len - 1]));
2970255736Sdavidch
2971255736Sdavidch    /* assume that the last SGE index is the biggest */
2972255736Sdavidch    bxe_update_last_max_sge(fp,
2973283269Sdavidcs                            le16toh(cqe->sgl[sge_len - 1]));
2974255736Sdavidch
2975255736Sdavidch    last_max = RX_SGE(fp->last_max_sge);
2976255736Sdavidch    last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
2977255736Sdavidch    first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
2978255736Sdavidch
2979255736Sdavidch    /* if ring is not full */
2980255736Sdavidch    if (last_elem + 1 != first_elem) {
2981255736Sdavidch        last_elem++;
2982255736Sdavidch    }
2983255736Sdavidch
2984255736Sdavidch    /* now update the prod */
2985255736Sdavidch    for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
2986255736Sdavidch        if (__predict_true(fp->sge_mask[i])) {
2987255736Sdavidch            break;
2988255736Sdavidch        }
2989255736Sdavidch
2990255736Sdavidch        fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
2991255736Sdavidch        delta += BIT_VEC64_ELEM_SZ;
2992255736Sdavidch    }
2993255736Sdavidch
2994255736Sdavidch    if (delta > 0) {
2995255736Sdavidch        fp->rx_sge_prod += delta;
2996255736Sdavidch        /* clear page-end entries */
2997255736Sdavidch        bxe_clear_sge_mask_next_elems(fp);
2998255736Sdavidch    }
2999255736Sdavidch
3000255736Sdavidch    BLOGD(sc, DBG_LRO,
3001255736Sdavidch          "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
3002255736Sdavidch          fp->index, fp->last_max_sge, fp->rx_sge_prod);
3003255736Sdavidch}
3004255736Sdavidch
3005255736Sdavidch/*
3006255736Sdavidch * The aggregation on the current TPA queue has completed. Pull the individual
3007255736Sdavidch * mbuf fragments together into a single mbuf, perform all necessary checksum
3008255736Sdavidch * calculations, and send the resuting mbuf to the stack.
3009255736Sdavidch */
3010255736Sdavidchstatic void
3011255736Sdavidchbxe_tpa_stop(struct bxe_softc          *sc,
3012255736Sdavidch             struct bxe_fastpath       *fp,
3013255736Sdavidch             struct bxe_sw_tpa_info    *tpa_info,
3014255736Sdavidch             uint16_t                  queue,
3015255736Sdavidch             uint16_t                  pages,
3016255736Sdavidch			 struct eth_end_agg_rx_cqe *cqe,
3017255736Sdavidch             uint16_t                  cqe_idx)
3018255736Sdavidch{
3019266979Smarcel    if_t ifp = sc->ifp;
3020255736Sdavidch    struct mbuf *m;
3021255736Sdavidch    int rc = 0;
3022255736Sdavidch
3023255736Sdavidch    BLOGD(sc, DBG_LRO,
3024255736Sdavidch          "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3025255736Sdavidch          fp->index, queue, tpa_info->placement_offset,
3026255736Sdavidch          le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3027255736Sdavidch
3028255736Sdavidch    m = tpa_info->bd.m;
3029255736Sdavidch
3030255736Sdavidch    /* allocate a replacement before modifying existing mbuf */
3031255736Sdavidch    rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3032255736Sdavidch    if (rc) {
3033255736Sdavidch        /* drop the frame and log an error */
3034255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3035255736Sdavidch        goto bxe_tpa_stop_exit;
3036255736Sdavidch    }
3037255736Sdavidch
3038255736Sdavidch    /* we have a replacement, fixup the current mbuf */
3039255736Sdavidch    m_adj(m, tpa_info->placement_offset);
3040255736Sdavidch    m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3041255736Sdavidch
3042255736Sdavidch    /* mark the checksums valid (taken care of by the firmware) */
3043255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3044255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3045255736Sdavidch    m->m_pkthdr.csum_data = 0xffff;
3046255736Sdavidch    m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3047255736Sdavidch                               CSUM_IP_VALID   |
3048255736Sdavidch                               CSUM_DATA_VALID |
3049255736Sdavidch                               CSUM_PSEUDO_HDR);
3050255736Sdavidch
3051255736Sdavidch    /* aggregate all of the SGEs into a single mbuf */
3052255736Sdavidch    rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3053255736Sdavidch    if (rc) {
3054255736Sdavidch        /* drop the packet and log an error */
3055255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3056255736Sdavidch        m_freem(m);
3057255736Sdavidch    } else {
3058296071Sdavidcs        if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3059255736Sdavidch            m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3060255736Sdavidch            m->m_flags |= M_VLANTAG;
3061255736Sdavidch        }
3062255736Sdavidch
3063255736Sdavidch        /* assign packet to this interface interface */
3064266979Smarcel        if_setrcvif(m, ifp);
3065255736Sdavidch
3066255736Sdavidch#if __FreeBSD_version >= 800000
3067255736Sdavidch        /* specify what RSS queue was used for this flow */
3068255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3069297155Sdavidcs        BXE_SET_FLOWID(m);
3070255736Sdavidch#endif
3071255736Sdavidch
3072271782Sglebius        if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3073255736Sdavidch        fp->eth_q_stats.rx_tpa_pkts++;
3074255736Sdavidch
3075255736Sdavidch        /* pass the frame to the stack */
3076266979Smarcel        if_input(ifp, m);
3077255736Sdavidch    }
3078255736Sdavidch
3079255736Sdavidch    /* we passed an mbuf up the stack or dropped the frame */
3080255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa--;
3081255736Sdavidch
3082255736Sdavidchbxe_tpa_stop_exit:
3083255736Sdavidch
3084255736Sdavidch    fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3085255736Sdavidch    fp->rx_tpa_queue_used &= ~(1 << queue);
3086255736Sdavidch}
3087255736Sdavidch
3088255736Sdavidchstatic uint8_t
3089283269Sdavidcsbxe_service_rxsgl(
3090283269Sdavidcs                 struct bxe_fastpath *fp,
3091283269Sdavidcs                 uint16_t len,
3092283269Sdavidcs                 uint16_t lenonbd,
3093283269Sdavidcs                 struct mbuf *m,
3094283269Sdavidcs                 struct eth_fast_path_rx_cqe *cqe_fp)
3095283269Sdavidcs{
3096283269Sdavidcs    struct mbuf *m_frag;
3097283269Sdavidcs    uint16_t frags, frag_len;
3098283269Sdavidcs    uint16_t sge_idx = 0;
3099283269Sdavidcs    uint16_t j;
3100283269Sdavidcs    uint8_t i, rc = 0;
3101283269Sdavidcs    uint32_t frag_size;
3102283269Sdavidcs
3103283269Sdavidcs    /* adjust the mbuf */
3104283269Sdavidcs    m->m_len = lenonbd;
3105283269Sdavidcs
3106283269Sdavidcs    frag_size =  len - lenonbd;
3107283269Sdavidcs    frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3108283269Sdavidcs
3109283269Sdavidcs    for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3110283269Sdavidcs        sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3111283269Sdavidcs
3112283269Sdavidcs        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3113283269Sdavidcs        frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3114283269Sdavidcs        m_frag->m_len = frag_len;
3115283269Sdavidcs
3116283269Sdavidcs       /* allocate a new mbuf for the SGE */
3117283269Sdavidcs        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3118283269Sdavidcs        if (rc) {
3119283269Sdavidcs            /* Leave all remaining SGEs in the ring! */
3120283269Sdavidcs            return (rc);
3121283269Sdavidcs        }
3122283269Sdavidcs        fp->eth_q_stats.mbuf_alloc_sge--;
3123283269Sdavidcs
3124283269Sdavidcs        /* concatenate the fragment to the head mbuf */
3125283269Sdavidcs        m_cat(m, m_frag);
3126283269Sdavidcs
3127283269Sdavidcs        frag_size -= frag_len;
3128283269Sdavidcs    }
3129283269Sdavidcs
3130283269Sdavidcs    bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3131283269Sdavidcs
3132283269Sdavidcs    return rc;
3133283269Sdavidcs}
3134283269Sdavidcs
3135283269Sdavidcsstatic uint8_t
3136255736Sdavidchbxe_rxeof(struct bxe_softc    *sc,
3137255736Sdavidch          struct bxe_fastpath *fp)
3138255736Sdavidch{
3139266979Smarcel    if_t ifp = sc->ifp;
3140255736Sdavidch    uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3141255736Sdavidch    uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3142255736Sdavidch    int rx_pkts = 0;
3143281006Sdavidcs    int rc = 0;
3144255736Sdavidch
3145255736Sdavidch    BXE_FP_RX_LOCK(fp);
3146255736Sdavidch
3147255736Sdavidch    /* CQ "next element" is of the size of the regular element */
3148255736Sdavidch    hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3149255736Sdavidch    if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3150255736Sdavidch        hw_cq_cons++;
3151255736Sdavidch    }
3152255736Sdavidch
3153255736Sdavidch    bd_cons = fp->rx_bd_cons;
3154255736Sdavidch    bd_prod = fp->rx_bd_prod;
3155255736Sdavidch    bd_prod_fw = bd_prod;
3156255736Sdavidch    sw_cq_cons = fp->rx_cq_cons;
3157255736Sdavidch    sw_cq_prod = fp->rx_cq_prod;
3158255736Sdavidch
3159255736Sdavidch    /*
3160255736Sdavidch     * Memory barrier necessary as speculative reads of the rx
3161255736Sdavidch     * buffer can be ahead of the index in the status block
3162255736Sdavidch     */
3163255736Sdavidch    rmb();
3164255736Sdavidch
3165255736Sdavidch    BLOGD(sc, DBG_RX,
3166255736Sdavidch          "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3167255736Sdavidch          fp->index, hw_cq_cons, sw_cq_cons);
3168255736Sdavidch
3169255736Sdavidch    while (sw_cq_cons != hw_cq_cons) {
3170255736Sdavidch        struct bxe_sw_rx_bd *rx_buf = NULL;
3171255736Sdavidch        union eth_rx_cqe *cqe;
3172255736Sdavidch        struct eth_fast_path_rx_cqe *cqe_fp;
3173255736Sdavidch        uint8_t cqe_fp_flags;
3174255736Sdavidch        enum eth_rx_cqe_type cqe_fp_type;
3175283269Sdavidcs        uint16_t len, lenonbd,  pad;
3176255736Sdavidch        struct mbuf *m = NULL;
3177255736Sdavidch
3178255736Sdavidch        comp_ring_cons = RCQ(sw_cq_cons);
3179255736Sdavidch        bd_prod = RX_BD(bd_prod);
3180255736Sdavidch        bd_cons = RX_BD(bd_cons);
3181255736Sdavidch
3182255736Sdavidch        cqe          = &fp->rcq_chain[comp_ring_cons];
3183255736Sdavidch        cqe_fp       = &cqe->fast_path_cqe;
3184255736Sdavidch        cqe_fp_flags = cqe_fp->type_error_flags;
3185255736Sdavidch        cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3186255736Sdavidch
3187255736Sdavidch        BLOGD(sc, DBG_RX,
3188255736Sdavidch              "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3189255736Sdavidch              "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3190283269Sdavidcs              "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3191255736Sdavidch              fp->index,
3192255736Sdavidch              hw_cq_cons,
3193255736Sdavidch              sw_cq_cons,
3194255736Sdavidch              bd_prod,
3195255736Sdavidch              bd_cons,
3196255736Sdavidch              CQE_TYPE(cqe_fp_flags),
3197255736Sdavidch              cqe_fp_flags,
3198255736Sdavidch              cqe_fp->status_flags,
3199255736Sdavidch              le32toh(cqe_fp->rss_hash_result),
3200255736Sdavidch              le16toh(cqe_fp->vlan_tag),
3201283269Sdavidcs              le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3202283269Sdavidcs              le16toh(cqe_fp->len_on_bd));
3203255736Sdavidch
3204255736Sdavidch        /* is this a slowpath msg? */
3205255736Sdavidch        if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3206255736Sdavidch            bxe_sp_event(sc, fp, cqe);
3207255736Sdavidch            goto next_cqe;
3208255736Sdavidch        }
3209255736Sdavidch
3210255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[bd_cons];
3211255736Sdavidch
3212255736Sdavidch        if (!CQE_TYPE_FAST(cqe_fp_type)) {
3213255736Sdavidch            struct bxe_sw_tpa_info *tpa_info;
3214255736Sdavidch            uint16_t frag_size, pages;
3215255736Sdavidch            uint8_t queue;
3216255736Sdavidch
3217255736Sdavidch            if (CQE_TYPE_START(cqe_fp_type)) {
3218255736Sdavidch                bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3219255736Sdavidch                              bd_cons, bd_prod, cqe_fp);
3220255736Sdavidch                m = NULL; /* packet not ready yet */
3221255736Sdavidch                goto next_rx;
3222255736Sdavidch            }
3223255736Sdavidch
3224255736Sdavidch            KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3225255736Sdavidch                    ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3226255736Sdavidch
3227255736Sdavidch            queue = cqe->end_agg_cqe.queue_index;
3228255736Sdavidch            tpa_info = &fp->rx_tpa_info[queue];
3229255736Sdavidch
3230255736Sdavidch            BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3231255736Sdavidch                  fp->index, queue);
3232255736Sdavidch
3233255736Sdavidch            frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3234255736Sdavidch                         tpa_info->len_on_bd);
3235255736Sdavidch            pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3236255736Sdavidch
3237255736Sdavidch            bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3238255736Sdavidch                         &cqe->end_agg_cqe, comp_ring_cons);
3239255736Sdavidch
3240283269Sdavidcs            bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3241255736Sdavidch
3242255736Sdavidch            goto next_cqe;
3243255736Sdavidch        }
3244255736Sdavidch
3245255736Sdavidch        /* non TPA */
3246255736Sdavidch
3247255736Sdavidch        /* is this an error packet? */
3248255736Sdavidch        if (__predict_false(cqe_fp_flags &
3249255736Sdavidch                            ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3250255736Sdavidch            BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3251255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3252255736Sdavidch            goto next_rx;
3253255736Sdavidch        }
3254255736Sdavidch
3255255736Sdavidch        len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3256283269Sdavidcs        lenonbd = le16toh(cqe_fp->len_on_bd);
3257255736Sdavidch        pad = cqe_fp->placement_offset;
3258255736Sdavidch
3259255736Sdavidch        m = rx_buf->m;
3260255736Sdavidch
3261255736Sdavidch        if (__predict_false(m == NULL)) {
3262255736Sdavidch            BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3263255736Sdavidch                  bd_cons, fp->index);
3264255736Sdavidch            goto next_rx;
3265255736Sdavidch        }
3266255736Sdavidch
3267255736Sdavidch        /* XXX double copy if packet length under a threshold */
3268255736Sdavidch
3269255736Sdavidch        /*
3270255736Sdavidch         * If all the buffer descriptors are filled with mbufs then fill in
3271255736Sdavidch         * the current consumer index with a new BD. Else if a maximum Rx
3272255736Sdavidch         * buffer limit is imposed then fill in the next producer index.
3273255736Sdavidch         */
3274255736Sdavidch        rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3275255736Sdavidch                                  (sc->max_rx_bufs != RX_BD_USABLE) ?
3276255736Sdavidch                                      bd_prod : bd_cons);
3277255736Sdavidch        if (rc != 0) {
3278281006Sdavidcs
3279281006Sdavidcs            /* we simply reuse the received mbuf and don't post it to the stack */
3280281006Sdavidcs            m = NULL;
3281281006Sdavidcs
3282255736Sdavidch            BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3283255736Sdavidch                  fp->index, rc);
3284255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3285255736Sdavidch
3286255736Sdavidch            if (sc->max_rx_bufs != RX_BD_USABLE) {
3287255736Sdavidch                /* copy this consumer index to the producer index */
3288255736Sdavidch                memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3289255736Sdavidch                       sizeof(struct bxe_sw_rx_bd));
3290255736Sdavidch                memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3291255736Sdavidch            }
3292255736Sdavidch
3293255736Sdavidch            goto next_rx;
3294255736Sdavidch        }
3295255736Sdavidch
3296255736Sdavidch        /* current mbuf was detached from the bd */
3297255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
3298255736Sdavidch
3299255736Sdavidch        /* we allocated a replacement mbuf, fixup the current one */
3300255736Sdavidch        m_adj(m, pad);
3301255736Sdavidch        m->m_pkthdr.len = m->m_len = len;
3302255736Sdavidch
3303292638Sdavidcs        if ((len > 60) && (len > lenonbd)) {
3304292638Sdavidcs            fp->eth_q_stats.rx_bxe_service_rxsgl++;
3305283269Sdavidcs            rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3306283269Sdavidcs            if (rc)
3307283269Sdavidcs                break;
3308283274Sdavidcs            fp->eth_q_stats.rx_jumbo_sge_pkts++;
3309292638Sdavidcs        } else if (lenonbd < len) {
3310292638Sdavidcs            fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3311283269Sdavidcs        }
3312283269Sdavidcs
3313255736Sdavidch        /* assign packet to this interface interface */
3314266979Smarcel	if_setrcvif(m, ifp);
3315255736Sdavidch
3316255736Sdavidch        /* assume no hardware checksum has complated */
3317255736Sdavidch        m->m_pkthdr.csum_flags = 0;
3318255736Sdavidch
3319255736Sdavidch        /* validate checksum if offload enabled */
3320266979Smarcel        if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3321255736Sdavidch            /* check for a valid IP frame */
3322255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3323255736Sdavidch                  ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3324255736Sdavidch                m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3325255736Sdavidch                if (__predict_false(cqe_fp_flags &
3326255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3327255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3328255736Sdavidch                } else {
3329255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3330255736Sdavidch                    m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3331255736Sdavidch                }
3332255736Sdavidch            }
3333255736Sdavidch
3334255736Sdavidch            /* check for a valid TCP/UDP frame */
3335255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3336255736Sdavidch                  ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3337255736Sdavidch                if (__predict_false(cqe_fp_flags &
3338255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3339255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3340255736Sdavidch                } else {
3341255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3342255736Sdavidch                    m->m_pkthdr.csum_data = 0xFFFF;
3343255736Sdavidch                    m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3344255736Sdavidch                                               CSUM_PSEUDO_HDR);
3345255736Sdavidch                }
3346255736Sdavidch            }
3347255736Sdavidch        }
3348255736Sdavidch
3349255736Sdavidch        /* if there is a VLAN tag then flag that info */
3350296071Sdavidcs        if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3351255736Sdavidch            m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3352255736Sdavidch            m->m_flags |= M_VLANTAG;
3353255736Sdavidch        }
3354255736Sdavidch
3355255736Sdavidch#if __FreeBSD_version >= 800000
3356255736Sdavidch        /* specify what RSS queue was used for this flow */
3357255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3358297155Sdavidcs        BXE_SET_FLOWID(m);
3359255736Sdavidch#endif
3360255736Sdavidch
3361255736Sdavidchnext_rx:
3362255736Sdavidch
3363255736Sdavidch        bd_cons    = RX_BD_NEXT(bd_cons);
3364255736Sdavidch        bd_prod    = RX_BD_NEXT(bd_prod);
3365255736Sdavidch        bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3366255736Sdavidch
3367255736Sdavidch        /* pass the frame to the stack */
3368255736Sdavidch        if (__predict_true(m != NULL)) {
3369271782Sglebius            if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3370255736Sdavidch            rx_pkts++;
3371266979Smarcel            if_input(ifp, m);
3372255736Sdavidch        }
3373255736Sdavidch
3374255736Sdavidchnext_cqe:
3375255736Sdavidch
3376255736Sdavidch        sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3377255736Sdavidch        sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3378255736Sdavidch
3379255736Sdavidch        /* limit spinning on the queue */
3380281006Sdavidcs        if (rc != 0)
3381281006Sdavidcs            break;
3382281006Sdavidcs
3383255736Sdavidch        if (rx_pkts == sc->rx_budget) {
3384255736Sdavidch            fp->eth_q_stats.rx_budget_reached++;
3385255736Sdavidch            break;
3386255736Sdavidch        }
3387255736Sdavidch    } /* while work to do */
3388255736Sdavidch
3389255736Sdavidch    fp->rx_bd_cons = bd_cons;
3390255736Sdavidch    fp->rx_bd_prod = bd_prod_fw;
3391255736Sdavidch    fp->rx_cq_cons = sw_cq_cons;
3392255736Sdavidch    fp->rx_cq_prod = sw_cq_prod;
3393255736Sdavidch
3394255736Sdavidch    /* Update producers */
3395255736Sdavidch    bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3396255736Sdavidch
3397255736Sdavidch    fp->eth_q_stats.rx_pkts += rx_pkts;
3398255736Sdavidch    fp->eth_q_stats.rx_calls++;
3399255736Sdavidch
3400255736Sdavidch    BXE_FP_RX_UNLOCK(fp);
3401255736Sdavidch
3402255736Sdavidch    return (sw_cq_cons != hw_cq_cons);
3403255736Sdavidch}
3404255736Sdavidch
3405255736Sdavidchstatic uint16_t
3406255736Sdavidchbxe_free_tx_pkt(struct bxe_softc    *sc,
3407255736Sdavidch                struct bxe_fastpath *fp,
3408255736Sdavidch                uint16_t            idx)
3409255736Sdavidch{
3410255736Sdavidch    struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3411255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
3412255736Sdavidch    uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3413255736Sdavidch    uint16_t new_cons;
3414255736Sdavidch    int nbd;
3415255736Sdavidch
3416255736Sdavidch    /* unmap the mbuf from non-paged memory */
3417255736Sdavidch    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3418255736Sdavidch
3419255736Sdavidch    tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3420255736Sdavidch    nbd = le16toh(tx_start_bd->nbd) - 1;
3421255736Sdavidch
3422255736Sdavidch    new_cons = (tx_buf->first_bd + nbd);
3423255736Sdavidch
3424255736Sdavidch    /* free the mbuf */
3425255736Sdavidch    if (__predict_true(tx_buf->m != NULL)) {
3426255736Sdavidch        m_freem(tx_buf->m);
3427255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx--;
3428255736Sdavidch    } else {
3429255736Sdavidch        fp->eth_q_stats.tx_chain_lost_mbuf++;
3430255736Sdavidch    }
3431255736Sdavidch
3432255736Sdavidch    tx_buf->m = NULL;
3433255736Sdavidch    tx_buf->first_bd = 0;
3434255736Sdavidch
3435255736Sdavidch    return (new_cons);
3436255736Sdavidch}
3437255736Sdavidch
3438255736Sdavidch/* transmit timeout watchdog */
3439255736Sdavidchstatic int
3440255736Sdavidchbxe_watchdog(struct bxe_softc    *sc,
3441255736Sdavidch             struct bxe_fastpath *fp)
3442255736Sdavidch{
3443255736Sdavidch    BXE_FP_TX_LOCK(fp);
3444255736Sdavidch
3445255736Sdavidch    if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3446255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
3447255736Sdavidch        return (0);
3448255736Sdavidch    }
3449255736Sdavidch
3450258187Sedavis    BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3451298294Sdavidcs    if(sc->trigger_grcdump) {
3452298294Sdavidcs         /* taking grcdump */
3453298294Sdavidcs         bxe_grc_dump(sc);
3454298294Sdavidcs    }
3455258187Sedavis
3456255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
3457255736Sdavidch
3458255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_REINIT);
3459255736Sdavidch    taskqueue_enqueue(sc->chip_tq, &sc->chip_tq_task);
3460255736Sdavidch
3461255736Sdavidch    return (-1);
3462255736Sdavidch}
3463255736Sdavidch
3464255736Sdavidch/* processes transmit completions */
3465255736Sdavidchstatic uint8_t
3466255736Sdavidchbxe_txeof(struct bxe_softc    *sc,
3467255736Sdavidch          struct bxe_fastpath *fp)
3468255736Sdavidch{
3469266979Smarcel    if_t ifp = sc->ifp;
3470255736Sdavidch    uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3471255736Sdavidch    uint16_t tx_bd_avail;
3472255736Sdavidch
3473255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
3474255736Sdavidch
3475255736Sdavidch    bd_cons = fp->tx_bd_cons;
3476255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
3477255736Sdavidch    sw_cons = fp->tx_pkt_cons;
3478255736Sdavidch
3479255736Sdavidch    while (sw_cons != hw_cons) {
3480255736Sdavidch        pkt_cons = TX_BD(sw_cons);
3481255736Sdavidch
3482255736Sdavidch        BLOGD(sc, DBG_TX,
3483255736Sdavidch              "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3484255736Sdavidch              fp->index, hw_cons, sw_cons, pkt_cons);
3485255736Sdavidch
3486255736Sdavidch        bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3487255736Sdavidch
3488255736Sdavidch        sw_cons++;
3489255736Sdavidch    }
3490255736Sdavidch
3491255736Sdavidch    fp->tx_pkt_cons = sw_cons;
3492255736Sdavidch    fp->tx_bd_cons  = bd_cons;
3493255736Sdavidch
3494255736Sdavidch    BLOGD(sc, DBG_TX,
3495255736Sdavidch          "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3496255736Sdavidch          fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3497255736Sdavidch
3498255736Sdavidch    mb();
3499255736Sdavidch
3500255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
3501255736Sdavidch
3502258187Sedavis    if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3503266979Smarcel        if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
3504258187Sedavis    } else {
3505266979Smarcel        if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3506258187Sedavis    }
3507255736Sdavidch
3508258187Sedavis    if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3509258187Sedavis        /* reset the watchdog timer if there are pending transmits */
3510255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
3511258187Sedavis        return (TRUE);
3512258187Sedavis    } else {
3513258187Sedavis        /* clear watchdog when there are no pending transmits */
3514258187Sedavis        fp->watchdog_timer = 0;
3515258187Sedavis        return (FALSE);
3516255736Sdavidch    }
3517255736Sdavidch}
3518255736Sdavidch
3519255736Sdavidchstatic void
3520255736Sdavidchbxe_drain_tx_queues(struct bxe_softc *sc)
3521255736Sdavidch{
3522255736Sdavidch    struct bxe_fastpath *fp;
3523255736Sdavidch    int i, count;
3524255736Sdavidch
3525255736Sdavidch    /* wait until all TX fastpath tasks have completed */
3526255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
3527255736Sdavidch        fp = &sc->fp[i];
3528255736Sdavidch
3529255736Sdavidch        count = 1000;
3530255736Sdavidch
3531255736Sdavidch        while (bxe_has_tx_work(fp)) {
3532255736Sdavidch
3533255736Sdavidch            BXE_FP_TX_LOCK(fp);
3534255736Sdavidch            bxe_txeof(sc, fp);
3535255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
3536255736Sdavidch
3537255736Sdavidch            if (count == 0) {
3538255736Sdavidch                BLOGE(sc, "Timeout waiting for fp[%d] "
3539255736Sdavidch                          "transmits to complete!\n", i);
3540255736Sdavidch                bxe_panic(sc, ("tx drain failure\n"));
3541255736Sdavidch                return;
3542255736Sdavidch            }
3543255736Sdavidch
3544255736Sdavidch            count--;
3545255736Sdavidch            DELAY(1000);
3546255736Sdavidch            rmb();
3547255736Sdavidch        }
3548255736Sdavidch    }
3549255736Sdavidch
3550255736Sdavidch    return;
3551255736Sdavidch}
3552255736Sdavidch
3553255736Sdavidchstatic int
3554255736Sdavidchbxe_del_all_macs(struct bxe_softc          *sc,
3555255736Sdavidch                 struct ecore_vlan_mac_obj *mac_obj,
3556255736Sdavidch                 int                       mac_type,
3557255736Sdavidch                 uint8_t                   wait_for_comp)
3558255736Sdavidch{
3559255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3560255736Sdavidch    int rc;
3561255736Sdavidch
3562255736Sdavidch    /* wait for completion of requested */
3563255736Sdavidch    if (wait_for_comp) {
3564255736Sdavidch        bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3565255736Sdavidch    }
3566255736Sdavidch
3567255736Sdavidch    /* Set the mac type of addresses we want to clear */
3568255736Sdavidch    bxe_set_bit(mac_type, &vlan_mac_flags);
3569255736Sdavidch
3570255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3571255736Sdavidch    if (rc < 0) {
3572295830Sdavidcs        BLOGE(sc, "Failed to delete MACs (%d) mac_type %d wait_for_comp 0x%x\n",
3573295830Sdavidcs            rc, mac_type, wait_for_comp);
3574255736Sdavidch    }
3575255736Sdavidch
3576255736Sdavidch    return (rc);
3577255736Sdavidch}
3578255736Sdavidch
3579255736Sdavidchstatic int
3580255736Sdavidchbxe_fill_accept_flags(struct bxe_softc *sc,
3581255736Sdavidch                      uint32_t         rx_mode,
3582255736Sdavidch                      unsigned long    *rx_accept_flags,
3583255736Sdavidch                      unsigned long    *tx_accept_flags)
3584255736Sdavidch{
3585255736Sdavidch    /* Clear the flags first */
3586255736Sdavidch    *rx_accept_flags = 0;
3587255736Sdavidch    *tx_accept_flags = 0;
3588255736Sdavidch
3589255736Sdavidch    switch (rx_mode) {
3590255736Sdavidch    case BXE_RX_MODE_NONE:
3591255736Sdavidch        /*
3592255736Sdavidch         * 'drop all' supersedes any accept flags that may have been
3593255736Sdavidch         * passed to the function.
3594255736Sdavidch         */
3595255736Sdavidch        break;
3596255736Sdavidch
3597255736Sdavidch    case BXE_RX_MODE_NORMAL:
3598255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3599255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3600255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3601255736Sdavidch
3602255736Sdavidch        /* internal switching mode */
3603255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3604255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3605255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3606255736Sdavidch
3607255736Sdavidch        break;
3608255736Sdavidch
3609255736Sdavidch    case BXE_RX_MODE_ALLMULTI:
3610255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3611255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3612255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3613255736Sdavidch
3614255736Sdavidch        /* internal switching mode */
3615255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3616255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3617255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3618255736Sdavidch
3619255736Sdavidch        break;
3620255736Sdavidch
3621255736Sdavidch    case BXE_RX_MODE_PROMISC:
3622255736Sdavidch        /*
3623255736Sdavidch         * According to deffinition of SI mode, iface in promisc mode
3624255736Sdavidch         * should receive matched and unmatched (in resolution of port)
3625255736Sdavidch         * unicast packets.
3626255736Sdavidch         */
3627255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3628255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3629255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3630255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3631255736Sdavidch
3632255736Sdavidch        /* internal switching mode */
3633255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3634255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3635255736Sdavidch
3636255736Sdavidch        if (IS_MF_SI(sc)) {
3637255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3638255736Sdavidch        } else {
3639255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3640255736Sdavidch        }
3641255736Sdavidch
3642255736Sdavidch        break;
3643255736Sdavidch
3644255736Sdavidch    default:
3645295830Sdavidcs        BLOGE(sc, "Unknown rx_mode (0x%x)\n", rx_mode);
3646255736Sdavidch        return (-1);
3647255736Sdavidch    }
3648255736Sdavidch
3649255736Sdavidch    /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3650255736Sdavidch    if (rx_mode != BXE_RX_MODE_NONE) {
3651255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3652255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3653255736Sdavidch    }
3654255736Sdavidch
3655255736Sdavidch    return (0);
3656255736Sdavidch}
3657255736Sdavidch
3658255736Sdavidchstatic int
3659255736Sdavidchbxe_set_q_rx_mode(struct bxe_softc *sc,
3660255736Sdavidch                  uint8_t          cl_id,
3661255736Sdavidch                  unsigned long    rx_mode_flags,
3662255736Sdavidch                  unsigned long    rx_accept_flags,
3663255736Sdavidch                  unsigned long    tx_accept_flags,
3664255736Sdavidch                  unsigned long    ramrod_flags)
3665255736Sdavidch{
3666255736Sdavidch    struct ecore_rx_mode_ramrod_params ramrod_param;
3667255736Sdavidch    int rc;
3668255736Sdavidch
3669255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
3670255736Sdavidch
3671255736Sdavidch    /* Prepare ramrod parameters */
3672255736Sdavidch    ramrod_param.cid = 0;
3673255736Sdavidch    ramrod_param.cl_id = cl_id;
3674255736Sdavidch    ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3675255736Sdavidch    ramrod_param.func_id = SC_FUNC(sc);
3676255736Sdavidch
3677255736Sdavidch    ramrod_param.pstate = &sc->sp_state;
3678255736Sdavidch    ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3679255736Sdavidch
3680255736Sdavidch    ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3681255736Sdavidch    ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3682255736Sdavidch
3683255736Sdavidch    bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3684255736Sdavidch
3685255736Sdavidch    ramrod_param.ramrod_flags = ramrod_flags;
3686255736Sdavidch    ramrod_param.rx_mode_flags = rx_mode_flags;
3687255736Sdavidch
3688255736Sdavidch    ramrod_param.rx_accept_flags = rx_accept_flags;
3689255736Sdavidch    ramrod_param.tx_accept_flags = tx_accept_flags;
3690255736Sdavidch
3691255736Sdavidch    rc = ecore_config_rx_mode(sc, &ramrod_param);
3692255736Sdavidch    if (rc < 0) {
3693295830Sdavidcs        BLOGE(sc, "Set rx_mode %d cli_id 0x%x rx_mode_flags 0x%x "
3694295830Sdavidcs            "rx_accept_flags 0x%x tx_accept_flags 0x%x "
3695295830Sdavidcs            "ramrod_flags 0x%x rc %d failed\n", sc->rx_mode, cl_id,
3696295830Sdavidcs            (uint32_t)rx_mode_flags, (uint32_t)rx_accept_flags,
3697295830Sdavidcs            (uint32_t)tx_accept_flags, (uint32_t)ramrod_flags, rc);
3698255736Sdavidch        return (rc);
3699255736Sdavidch    }
3700255736Sdavidch
3701255736Sdavidch    return (0);
3702255736Sdavidch}
3703255736Sdavidch
3704255736Sdavidchstatic int
3705255736Sdavidchbxe_set_storm_rx_mode(struct bxe_softc *sc)
3706255736Sdavidch{
3707255736Sdavidch    unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3708255736Sdavidch    unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3709255736Sdavidch    int rc;
3710255736Sdavidch
3711255736Sdavidch    rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3712255736Sdavidch                               &tx_accept_flags);
3713255736Sdavidch    if (rc) {
3714255736Sdavidch        return (rc);
3715255736Sdavidch    }
3716255736Sdavidch
3717255736Sdavidch    bxe_set_bit(RAMROD_RX, &ramrod_flags);
3718255736Sdavidch    bxe_set_bit(RAMROD_TX, &ramrod_flags);
3719255736Sdavidch
3720255736Sdavidch    /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3721255736Sdavidch    return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3722255736Sdavidch                              rx_accept_flags, tx_accept_flags,
3723255736Sdavidch                              ramrod_flags));
3724255736Sdavidch}
3725255736Sdavidch
3726255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3727255736Sdavidchstatic int
3728255736Sdavidchbxe_nic_load_no_mcp(struct bxe_softc *sc)
3729255736Sdavidch{
3730255736Sdavidch    int path = SC_PATH(sc);
3731255736Sdavidch    int port = SC_PORT(sc);
3732255736Sdavidch
3733255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3734255736Sdavidch          path, load_count[path][0], load_count[path][1],
3735255736Sdavidch          load_count[path][2]);
3736255736Sdavidch    load_count[path][0]++;
3737255736Sdavidch    load_count[path][1 + port]++;
3738255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3739255736Sdavidch          path, load_count[path][0], load_count[path][1],
3740255736Sdavidch          load_count[path][2]);
3741255736Sdavidch    if (load_count[path][0] == 1) {
3742255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_COMMON);
3743255736Sdavidch    } else if (load_count[path][1 + port] == 1) {
3744255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_PORT);
3745255736Sdavidch    } else {
3746255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3747255736Sdavidch    }
3748255736Sdavidch}
3749255736Sdavidch
3750255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3751255736Sdavidchstatic int
3752255736Sdavidchbxe_nic_unload_no_mcp(struct bxe_softc *sc)
3753255736Sdavidch{
3754255736Sdavidch    int port = SC_PORT(sc);
3755255736Sdavidch    int path = SC_PATH(sc);
3756255736Sdavidch
3757255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3758255736Sdavidch          path, load_count[path][0], load_count[path][1],
3759255736Sdavidch          load_count[path][2]);
3760255736Sdavidch    load_count[path][0]--;
3761255736Sdavidch    load_count[path][1 + port]--;
3762255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3763255736Sdavidch          path, load_count[path][0], load_count[path][1],
3764255736Sdavidch          load_count[path][2]);
3765255736Sdavidch    if (load_count[path][0] == 0) {
3766255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3767255736Sdavidch    } else if (load_count[path][1 + port] == 0) {
3768255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3769255736Sdavidch    } else {
3770255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3771255736Sdavidch    }
3772255736Sdavidch}
3773255736Sdavidch
3774255736Sdavidch/* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3775255736Sdavidchstatic uint32_t
3776255736Sdavidchbxe_send_unload_req(struct bxe_softc *sc,
3777255736Sdavidch                    int              unload_mode)
3778255736Sdavidch{
3779255736Sdavidch    uint32_t reset_code = 0;
3780255736Sdavidch
3781255736Sdavidch    /* Select the UNLOAD request mode */
3782255736Sdavidch    if (unload_mode == UNLOAD_NORMAL) {
3783255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3784295830Sdavidcs    } else {
3785255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3786255736Sdavidch    }
3787255736Sdavidch
3788255736Sdavidch    /* Send the request to the MCP */
3789255736Sdavidch    if (!BXE_NOMCP(sc)) {
3790255736Sdavidch        reset_code = bxe_fw_command(sc, reset_code, 0);
3791255736Sdavidch    } else {
3792255736Sdavidch        reset_code = bxe_nic_unload_no_mcp(sc);
3793255736Sdavidch    }
3794255736Sdavidch
3795255736Sdavidch    return (reset_code);
3796255736Sdavidch}
3797255736Sdavidch
3798255736Sdavidch/* send UNLOAD_DONE command to the MCP */
3799255736Sdavidchstatic void
3800255736Sdavidchbxe_send_unload_done(struct bxe_softc *sc,
3801255736Sdavidch                     uint8_t          keep_link)
3802255736Sdavidch{
3803255736Sdavidch    uint32_t reset_param =
3804255736Sdavidch        keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
3805255736Sdavidch
3806255736Sdavidch    /* Report UNLOAD_DONE to MCP */
3807255736Sdavidch    if (!BXE_NOMCP(sc)) {
3808255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
3809255736Sdavidch    }
3810255736Sdavidch}
3811255736Sdavidch
3812255736Sdavidchstatic int
3813255736Sdavidchbxe_func_wait_started(struct bxe_softc *sc)
3814255736Sdavidch{
3815255736Sdavidch    int tout = 50;
3816255736Sdavidch
3817255736Sdavidch    if (!sc->port.pmf) {
3818255736Sdavidch        return (0);
3819255736Sdavidch    }
3820255736Sdavidch
3821255736Sdavidch    /*
3822255736Sdavidch     * (assumption: No Attention from MCP at this stage)
3823255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
3824255736Sdavidch     * 1. Sync IRS for default SB
3825255736Sdavidch     * 2. Sync SP queue - this guarantees us that attention handling started
3826255736Sdavidch     * 3. Wait, that TX disable/enable transaction completes
3827255736Sdavidch     *
3828255736Sdavidch     * 1+2 guarantee that if DCBX attention was scheduled it already changed
3829255736Sdavidch     * pending bit of transaction from STARTED-->TX_STOPPED, if we already
3830255736Sdavidch     * received completion for the transaction the state is TX_STOPPED.
3831255736Sdavidch     * State will return to STARTED after completion of TX_STOPPED-->STARTED
3832255736Sdavidch     * transaction.
3833255736Sdavidch     */
3834255736Sdavidch
3835255736Sdavidch    /* XXX make sure default SB ISR is done */
3836255736Sdavidch    /* need a way to synchronize an irq (intr_mtx?) */
3837255736Sdavidch
3838255736Sdavidch    /* XXX flush any work queues */
3839255736Sdavidch
3840255736Sdavidch    while (ecore_func_get_state(sc, &sc->func_obj) !=
3841255736Sdavidch           ECORE_F_STATE_STARTED && tout--) {
3842255736Sdavidch        DELAY(20000);
3843255736Sdavidch    }
3844255736Sdavidch
3845255736Sdavidch    if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
3846255736Sdavidch        /*
3847255736Sdavidch         * Failed to complete the transaction in a "good way"
3848255736Sdavidch         * Force both transactions with CLR bit.
3849255736Sdavidch         */
3850255736Sdavidch        struct ecore_func_state_params func_params = { NULL };
3851255736Sdavidch
3852255736Sdavidch        BLOGE(sc, "Unexpected function state! "
3853255736Sdavidch                  "Forcing STARTED-->TX_STOPPED-->STARTED\n");
3854255736Sdavidch
3855255736Sdavidch        func_params.f_obj = &sc->func_obj;
3856255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3857255736Sdavidch
3858255736Sdavidch        /* STARTED-->TX_STOPPED */
3859255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_STOP;
3860255736Sdavidch        ecore_func_state_change(sc, &func_params);
3861255736Sdavidch
3862255736Sdavidch        /* TX_STOPPED-->STARTED */
3863255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_START;
3864255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
3865255736Sdavidch    }
3866255736Sdavidch
3867255736Sdavidch    return (0);
3868255736Sdavidch}
3869255736Sdavidch
3870255736Sdavidchstatic int
3871255736Sdavidchbxe_stop_queue(struct bxe_softc *sc,
3872255736Sdavidch               int              index)
3873255736Sdavidch{
3874255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[index];
3875255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
3876255736Sdavidch    int rc;
3877255736Sdavidch
3878255736Sdavidch    BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
3879255736Sdavidch
3880255736Sdavidch    q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
3881255736Sdavidch    /* We want to wait for completion in this context */
3882255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
3883255736Sdavidch
3884255736Sdavidch    /* Stop the primary connection: */
3885255736Sdavidch
3886255736Sdavidch    /* ...halt the connection */
3887255736Sdavidch    q_params.cmd = ECORE_Q_CMD_HALT;
3888255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
3889255736Sdavidch    if (rc) {
3890255736Sdavidch        return (rc);
3891255736Sdavidch    }
3892255736Sdavidch
3893255736Sdavidch    /* ...terminate the connection */
3894255736Sdavidch    q_params.cmd = ECORE_Q_CMD_TERMINATE;
3895255736Sdavidch    memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
3896255736Sdavidch    q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
3897255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
3898255736Sdavidch    if (rc) {
3899255736Sdavidch        return (rc);
3900255736Sdavidch    }
3901255736Sdavidch
3902255736Sdavidch    /* ...delete cfc entry */
3903255736Sdavidch    q_params.cmd = ECORE_Q_CMD_CFC_DEL;
3904255736Sdavidch    memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
3905255736Sdavidch    q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
3906255736Sdavidch    return (ecore_queue_state_change(sc, &q_params));
3907255736Sdavidch}
3908255736Sdavidch
3909255736Sdavidch/* wait for the outstanding SP commands */
3910255736Sdavidchstatic inline uint8_t
3911255736Sdavidchbxe_wait_sp_comp(struct bxe_softc *sc,
3912255736Sdavidch                 unsigned long    mask)
3913255736Sdavidch{
3914255736Sdavidch    unsigned long tmp;
3915255736Sdavidch    int tout = 5000; /* wait for 5 secs tops */
3916255736Sdavidch
3917255736Sdavidch    while (tout--) {
3918255736Sdavidch        mb();
3919255736Sdavidch        if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
3920255736Sdavidch            return (TRUE);
3921255736Sdavidch        }
3922255736Sdavidch
3923255736Sdavidch        DELAY(1000);
3924255736Sdavidch    }
3925255736Sdavidch
3926255736Sdavidch    mb();
3927255736Sdavidch
3928255736Sdavidch    tmp = atomic_load_acq_long(&sc->sp_state);
3929255736Sdavidch    if (tmp & mask) {
3930255736Sdavidch        BLOGE(sc, "Filtering completion timed out: "
3931255736Sdavidch                  "sp_state 0x%lx, mask 0x%lx\n",
3932255736Sdavidch              tmp, mask);
3933255736Sdavidch        return (FALSE);
3934255736Sdavidch    }
3935255736Sdavidch
3936255736Sdavidch    return (FALSE);
3937255736Sdavidch}
3938255736Sdavidch
3939255736Sdavidchstatic int
3940255736Sdavidchbxe_func_stop(struct bxe_softc *sc)
3941255736Sdavidch{
3942255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
3943255736Sdavidch    int rc;
3944255736Sdavidch
3945255736Sdavidch    /* prepare parameters for function state transitions */
3946255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3947255736Sdavidch    func_params.f_obj = &sc->func_obj;
3948255736Sdavidch    func_params.cmd = ECORE_F_CMD_STOP;
3949255736Sdavidch
3950255736Sdavidch    /*
3951255736Sdavidch     * Try to stop the function the 'good way'. If it fails (in case
3952255736Sdavidch     * of a parity error during bxe_chip_cleanup()) and we are
3953255736Sdavidch     * not in a debug mode, perform a state transaction in order to
3954255736Sdavidch     * enable further HW_RESET transaction.
3955255736Sdavidch     */
3956255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
3957255736Sdavidch    if (rc) {
3958255736Sdavidch        BLOGE(sc, "FUNC_STOP ramrod failed. "
3959295830Sdavidcs                  "Running a dry transaction (%d)\n", rc);
3960255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3961255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
3962255736Sdavidch    }
3963255736Sdavidch
3964255736Sdavidch    return (0);
3965255736Sdavidch}
3966255736Sdavidch
3967255736Sdavidchstatic int
3968255736Sdavidchbxe_reset_hw(struct bxe_softc *sc,
3969255736Sdavidch             uint32_t         load_code)
3970255736Sdavidch{
3971255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
3972255736Sdavidch
3973255736Sdavidch    /* Prepare parameters for function state transitions */
3974255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3975255736Sdavidch
3976255736Sdavidch    func_params.f_obj = &sc->func_obj;
3977255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_RESET;
3978255736Sdavidch
3979255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
3980255736Sdavidch
3981255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
3982255736Sdavidch}
3983255736Sdavidch
3984255736Sdavidchstatic void
3985255736Sdavidchbxe_int_disable_sync(struct bxe_softc *sc,
3986255736Sdavidch                     int              disable_hw)
3987255736Sdavidch{
3988255736Sdavidch    if (disable_hw) {
3989255736Sdavidch        /* prevent the HW from sending interrupts */
3990255736Sdavidch        bxe_int_disable(sc);
3991255736Sdavidch    }
3992255736Sdavidch
3993255736Sdavidch    /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
3994255736Sdavidch    /* make sure all ISRs are done */
3995255736Sdavidch
3996255736Sdavidch    /* XXX make sure sp_task is not running */
3997255736Sdavidch    /* cancel and flush work queues */
3998255736Sdavidch}
3999255736Sdavidch
4000255736Sdavidchstatic void
4001255736Sdavidchbxe_chip_cleanup(struct bxe_softc *sc,
4002255736Sdavidch                 uint32_t         unload_mode,
4003255736Sdavidch                 uint8_t          keep_link)
4004255736Sdavidch{
4005255736Sdavidch    int port = SC_PORT(sc);
4006255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
4007255736Sdavidch    uint32_t reset_code;
4008255736Sdavidch    int i, rc = 0;
4009255736Sdavidch
4010255736Sdavidch    bxe_drain_tx_queues(sc);
4011255736Sdavidch
4012255736Sdavidch    /* give HW time to discard old tx messages */
4013255736Sdavidch    DELAY(1000);
4014255736Sdavidch
4015255736Sdavidch    /* Clean all ETH MACs */
4016255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4017255736Sdavidch    if (rc < 0) {
4018255736Sdavidch        BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4019255736Sdavidch    }
4020255736Sdavidch
4021255736Sdavidch    /* Clean up UC list  */
4022255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4023255736Sdavidch    if (rc < 0) {
4024255736Sdavidch        BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4025255736Sdavidch    }
4026255736Sdavidch
4027255736Sdavidch    /* Disable LLH */
4028255736Sdavidch    if (!CHIP_IS_E1(sc)) {
4029255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4030255736Sdavidch    }
4031255736Sdavidch
4032255736Sdavidch    /* Set "drop all" to stop Rx */
4033255736Sdavidch
4034255736Sdavidch    /*
4035255736Sdavidch     * We need to take the BXE_MCAST_LOCK() here in order to prevent
4036255736Sdavidch     * a race between the completion code and this code.
4037255736Sdavidch     */
4038255736Sdavidch    BXE_MCAST_LOCK(sc);
4039255736Sdavidch
4040255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4041255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4042255736Sdavidch    } else {
4043255736Sdavidch        bxe_set_storm_rx_mode(sc);
4044255736Sdavidch    }
4045255736Sdavidch
4046255736Sdavidch    /* Clean up multicast configuration */
4047255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4048255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4049255736Sdavidch    if (rc < 0) {
4050255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4051255736Sdavidch    }
4052255736Sdavidch
4053255736Sdavidch    BXE_MCAST_UNLOCK(sc);
4054255736Sdavidch
4055255736Sdavidch    // XXX bxe_iov_chip_cleanup(sc);
4056255736Sdavidch
4057255736Sdavidch    /*
4058255736Sdavidch     * Send the UNLOAD_REQUEST to the MCP. This will return if
4059255736Sdavidch     * this function should perform FUNCTION, PORT, or COMMON HW
4060255736Sdavidch     * reset.
4061255736Sdavidch     */
4062255736Sdavidch    reset_code = bxe_send_unload_req(sc, unload_mode);
4063255736Sdavidch
4064255736Sdavidch    /*
4065255736Sdavidch     * (assumption: No Attention from MCP at this stage)
4066255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
4067255736Sdavidch     */
4068255736Sdavidch    rc = bxe_func_wait_started(sc);
4069255736Sdavidch    if (rc) {
4070295830Sdavidcs        BLOGE(sc, "bxe_func_wait_started failed (%d)\n", rc);
4071255736Sdavidch    }
4072255736Sdavidch
4073255736Sdavidch    /*
4074255736Sdavidch     * Close multi and leading connections
4075255736Sdavidch     * Completions for ramrods are collected in a synchronous way
4076255736Sdavidch     */
4077255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
4078255736Sdavidch        if (bxe_stop_queue(sc, i)) {
4079255736Sdavidch            goto unload_error;
4080255736Sdavidch        }
4081255736Sdavidch    }
4082255736Sdavidch
4083255736Sdavidch    /*
4084255736Sdavidch     * If SP settings didn't get completed so far - something
4085255736Sdavidch     * very wrong has happen.
4086255736Sdavidch     */
4087255736Sdavidch    if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4088295830Sdavidcs        BLOGE(sc, "Common slow path ramrods got stuck!(%d)\n", rc);
4089255736Sdavidch    }
4090255736Sdavidch
4091255736Sdavidchunload_error:
4092255736Sdavidch
4093255736Sdavidch    rc = bxe_func_stop(sc);
4094255736Sdavidch    if (rc) {
4095295830Sdavidcs        BLOGE(sc, "Function stop failed!(%d)\n", rc);
4096255736Sdavidch    }
4097255736Sdavidch
4098255736Sdavidch    /* disable HW interrupts */
4099255736Sdavidch    bxe_int_disable_sync(sc, TRUE);
4100255736Sdavidch
4101255736Sdavidch    /* detach interrupts */
4102255736Sdavidch    bxe_interrupt_detach(sc);
4103255736Sdavidch
4104255736Sdavidch    /* Reset the chip */
4105255736Sdavidch    rc = bxe_reset_hw(sc, reset_code);
4106255736Sdavidch    if (rc) {
4107295830Sdavidcs        BLOGE(sc, "Hardware reset failed(%d)\n", rc);
4108255736Sdavidch    }
4109255736Sdavidch
4110255736Sdavidch    /* Report UNLOAD_DONE to MCP */
4111255736Sdavidch    bxe_send_unload_done(sc, keep_link);
4112255736Sdavidch}
4113255736Sdavidch
4114255736Sdavidchstatic void
4115255736Sdavidchbxe_disable_close_the_gate(struct bxe_softc *sc)
4116255736Sdavidch{
4117255736Sdavidch    uint32_t val;
4118255736Sdavidch    int port = SC_PORT(sc);
4119255736Sdavidch
4120255736Sdavidch    BLOGD(sc, DBG_LOAD,
4121255736Sdavidch          "Disabling 'close the gates'\n");
4122255736Sdavidch
4123255736Sdavidch    if (CHIP_IS_E1(sc)) {
4124255736Sdavidch        uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4125255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
4126255736Sdavidch        val = REG_RD(sc, addr);
4127255736Sdavidch        val &= ~(0x300);
4128255736Sdavidch        REG_WR(sc, addr, val);
4129255736Sdavidch    } else {
4130255736Sdavidch        val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4131255736Sdavidch        val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4132255736Sdavidch                 MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4133255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4134255736Sdavidch    }
4135255736Sdavidch}
4136255736Sdavidch
4137255736Sdavidch/*
4138255736Sdavidch * Cleans the object that have internal lists without sending
4139255736Sdavidch * ramrods. Should be run when interrutps are disabled.
4140255736Sdavidch */
4141255736Sdavidchstatic void
4142255736Sdavidchbxe_squeeze_objects(struct bxe_softc *sc)
4143255736Sdavidch{
4144255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4145255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
4146255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4147255736Sdavidch    int rc;
4148255736Sdavidch
4149255736Sdavidch    /* Cleanup MACs' object first... */
4150255736Sdavidch
4151255736Sdavidch    /* Wait for completion of requested */
4152255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4153255736Sdavidch    /* Perform a dry cleanup */
4154255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4155255736Sdavidch
4156255736Sdavidch    /* Clean ETH primary MAC */
4157255736Sdavidch    bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4158255736Sdavidch    rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4159255736Sdavidch                             &ramrod_flags);
4160255736Sdavidch    if (rc != 0) {
4161255736Sdavidch        BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4162255736Sdavidch    }
4163255736Sdavidch
4164255736Sdavidch    /* Cleanup UC list */
4165255736Sdavidch    vlan_mac_flags = 0;
4166255736Sdavidch    bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4167255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4168255736Sdavidch                             &ramrod_flags);
4169255736Sdavidch    if (rc != 0) {
4170255736Sdavidch        BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4171255736Sdavidch    }
4172255736Sdavidch
4173255736Sdavidch    /* Now clean mcast object... */
4174255736Sdavidch
4175255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4176255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4177255736Sdavidch
4178255736Sdavidch    /* Add a DEL command... */
4179255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4180255736Sdavidch    if (rc < 0) {
4181255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4182255736Sdavidch    }
4183255736Sdavidch
4184255736Sdavidch    /* now wait until all pending commands are cleared */
4185255736Sdavidch
4186255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4187255736Sdavidch    while (rc != 0) {
4188255736Sdavidch        if (rc < 0) {
4189255736Sdavidch            BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4190255736Sdavidch            return;
4191255736Sdavidch        }
4192255736Sdavidch
4193255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4194255736Sdavidch    }
4195255736Sdavidch}
4196255736Sdavidch
4197255736Sdavidch/* stop the controller */
4198255736Sdavidchstatic __noinline int
4199255736Sdavidchbxe_nic_unload(struct bxe_softc *sc,
4200255736Sdavidch               uint32_t         unload_mode,
4201255736Sdavidch               uint8_t          keep_link)
4202255736Sdavidch{
4203255736Sdavidch    uint8_t global = FALSE;
4204255736Sdavidch    uint32_t val;
4205296579Sdavidcs    int i;
4206255736Sdavidch
4207255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
4208255736Sdavidch
4209296579Sdavidcs    if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
4210296579Sdavidcs
4211296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
4212296579Sdavidcs        struct bxe_fastpath *fp;
4213296579Sdavidcs
4214296579Sdavidcs        fp = &sc->fp[i];
4215296579Sdavidcs        BXE_FP_TX_LOCK(fp);
4216296579Sdavidcs        BXE_FP_TX_UNLOCK(fp);
4217296579Sdavidcs    }
4218296579Sdavidcs
4219255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4220255736Sdavidch
4221255736Sdavidch    /* mark driver as unloaded in shmem2 */
4222255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4223255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4224255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4225255736Sdavidch                  val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4226255736Sdavidch    }
4227255736Sdavidch
4228255736Sdavidch    if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4229255736Sdavidch        (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4230255736Sdavidch        /*
4231255736Sdavidch         * We can get here if the driver has been unloaded
4232255736Sdavidch         * during parity error recovery and is either waiting for a
4233255736Sdavidch         * leader to complete or for other functions to unload and
4234255736Sdavidch         * then ifconfig down has been issued. In this case we want to
4235255736Sdavidch         * unload and let other functions to complete a recovery
4236255736Sdavidch         * process.
4237255736Sdavidch         */
4238255736Sdavidch        sc->recovery_state = BXE_RECOVERY_DONE;
4239255736Sdavidch        sc->is_leader = 0;
4240255736Sdavidch        bxe_release_leader_lock(sc);
4241255736Sdavidch        mb();
4242255736Sdavidch
4243255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4244295830Sdavidcs        BLOGE(sc, "Can't unload in closed or error state recover_state 0x%x"
4245295830Sdavidcs            " state = 0x%x\n", sc->recovery_state, sc->state);
4246255736Sdavidch        return (-1);
4247255736Sdavidch    }
4248255736Sdavidch
4249255736Sdavidch    /*
4250255736Sdavidch     * Nothing to do during unload if previous bxe_nic_load()
4251298955Spfg     * did not completed successfully - all resourses are released.
4252255736Sdavidch     */
4253255736Sdavidch    if ((sc->state == BXE_STATE_CLOSED) ||
4254255736Sdavidch        (sc->state == BXE_STATE_ERROR)) {
4255255736Sdavidch        return (0);
4256255736Sdavidch    }
4257255736Sdavidch
4258255736Sdavidch    sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4259255736Sdavidch    mb();
4260255736Sdavidch
4261255736Sdavidch    /* stop tx */
4262255736Sdavidch    bxe_tx_disable(sc);
4263255736Sdavidch
4264255736Sdavidch    sc->rx_mode = BXE_RX_MODE_NONE;
4265255736Sdavidch    /* XXX set rx mode ??? */
4266255736Sdavidch
4267292639Sdavidcs    if (IS_PF(sc) && !sc->grcdump_done) {
4268255736Sdavidch        /* set ALWAYS_ALIVE bit in shmem */
4269255736Sdavidch        sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4270255736Sdavidch
4271255736Sdavidch        bxe_drv_pulse(sc);
4272255736Sdavidch
4273255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_STOP);
4274255736Sdavidch        bxe_save_statistics(sc);
4275255736Sdavidch    }
4276255736Sdavidch
4277255736Sdavidch    /* wait till consumers catch up with producers in all queues */
4278255736Sdavidch    bxe_drain_tx_queues(sc);
4279255736Sdavidch
4280255736Sdavidch    /* if VF indicate to PF this function is going down (PF will delete sp
4281255736Sdavidch     * elements and clear initializations
4282255736Sdavidch     */
4283255736Sdavidch    if (IS_VF(sc)) {
4284255736Sdavidch        ; /* bxe_vfpf_close_vf(sc); */
4285255736Sdavidch    } else if (unload_mode != UNLOAD_RECOVERY) {
4286255736Sdavidch        /* if this is a normal/close unload need to clean up chip */
4287292639Sdavidcs        if (!sc->grcdump_done)
4288292639Sdavidcs            bxe_chip_cleanup(sc, unload_mode, keep_link);
4289255736Sdavidch    } else {
4290255736Sdavidch        /* Send the UNLOAD_REQUEST to the MCP */
4291255736Sdavidch        bxe_send_unload_req(sc, unload_mode);
4292255736Sdavidch
4293255736Sdavidch        /*
4294255736Sdavidch         * Prevent transactions to host from the functions on the
4295255736Sdavidch         * engine that doesn't reset global blocks in case of global
4296255736Sdavidch         * attention once gloabl blocks are reset and gates are opened
4297255736Sdavidch         * (the engine which leader will perform the recovery
4298255736Sdavidch         * last).
4299255736Sdavidch         */
4300255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
4301255736Sdavidch            bxe_pf_disable(sc);
4302255736Sdavidch        }
4303255736Sdavidch
4304255736Sdavidch        /* disable HW interrupts */
4305255736Sdavidch        bxe_int_disable_sync(sc, TRUE);
4306255736Sdavidch
4307255736Sdavidch        /* detach interrupts */
4308255736Sdavidch        bxe_interrupt_detach(sc);
4309255736Sdavidch
4310255736Sdavidch        /* Report UNLOAD_DONE to MCP */
4311255736Sdavidch        bxe_send_unload_done(sc, FALSE);
4312255736Sdavidch    }
4313255736Sdavidch
4314255736Sdavidch    /*
4315255736Sdavidch     * At this stage no more interrupts will arrive so we may safely clean
4316255736Sdavidch     * the queue'able objects here in case they failed to get cleaned so far.
4317255736Sdavidch     */
4318255736Sdavidch    if (IS_PF(sc)) {
4319255736Sdavidch        bxe_squeeze_objects(sc);
4320255736Sdavidch    }
4321255736Sdavidch
4322255736Sdavidch    /* There should be no more pending SP commands at this stage */
4323255736Sdavidch    sc->sp_state = 0;
4324255736Sdavidch
4325255736Sdavidch    sc->port.pmf = 0;
4326255736Sdavidch
4327255736Sdavidch    bxe_free_fp_buffers(sc);
4328255736Sdavidch
4329255736Sdavidch    if (IS_PF(sc)) {
4330255736Sdavidch        bxe_free_mem(sc);
4331255736Sdavidch    }
4332255736Sdavidch
4333255736Sdavidch    bxe_free_fw_stats_mem(sc);
4334255736Sdavidch
4335255736Sdavidch    sc->state = BXE_STATE_CLOSED;
4336255736Sdavidch
4337255736Sdavidch    /*
4338255736Sdavidch     * Check if there are pending parity attentions. If there are - set
4339255736Sdavidch     * RECOVERY_IN_PROGRESS.
4340255736Sdavidch     */
4341255736Sdavidch    if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4342255736Sdavidch        bxe_set_reset_in_progress(sc);
4343255736Sdavidch
4344255736Sdavidch        /* Set RESET_IS_GLOBAL if needed */
4345255736Sdavidch        if (global) {
4346255736Sdavidch            bxe_set_reset_global(sc);
4347255736Sdavidch        }
4348255736Sdavidch    }
4349255736Sdavidch
4350255736Sdavidch    /*
4351255736Sdavidch     * The last driver must disable a "close the gate" if there is no
4352255736Sdavidch     * parity attention or "process kill" pending.
4353255736Sdavidch     */
4354255736Sdavidch    if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4355255736Sdavidch        bxe_reset_is_done(sc, SC_PATH(sc))) {
4356255736Sdavidch        bxe_disable_close_the_gate(sc);
4357255736Sdavidch    }
4358255736Sdavidch
4359255736Sdavidch    BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4360255736Sdavidch
4361255736Sdavidch    return (0);
4362255736Sdavidch}
4363255736Sdavidch
4364255736Sdavidch/*
4365255736Sdavidch * Called by the OS to set various media options (i.e. link, speed, etc.) when
4366255736Sdavidch * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4367255736Sdavidch */
4368255736Sdavidchstatic int
4369266979Smarcelbxe_ifmedia_update(struct ifnet  *ifp)
4370255736Sdavidch{
4371266979Smarcel    struct bxe_softc *sc = (struct bxe_softc *)if_getsoftc(ifp);
4372255736Sdavidch    struct ifmedia *ifm;
4373255736Sdavidch
4374255736Sdavidch    ifm = &sc->ifmedia;
4375255736Sdavidch
4376255736Sdavidch    /* We only support Ethernet media type. */
4377255736Sdavidch    if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4378255736Sdavidch        return (EINVAL);
4379255736Sdavidch    }
4380255736Sdavidch
4381255736Sdavidch    switch (IFM_SUBTYPE(ifm->ifm_media)) {
4382255736Sdavidch    case IFM_AUTO:
4383255736Sdavidch         break;
4384255736Sdavidch    case IFM_10G_CX4:
4385255736Sdavidch    case IFM_10G_SR:
4386255736Sdavidch    case IFM_10G_T:
4387255736Sdavidch    case IFM_10G_TWINAX:
4388255736Sdavidch    default:
4389255736Sdavidch        /* We don't support changing the media type. */
4390255736Sdavidch        BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4391255736Sdavidch              IFM_SUBTYPE(ifm->ifm_media));
4392255736Sdavidch        return (EINVAL);
4393255736Sdavidch    }
4394255736Sdavidch
4395255736Sdavidch    return (0);
4396255736Sdavidch}
4397255736Sdavidch
4398255736Sdavidch/*
4399255736Sdavidch * Called by the OS to get the current media status (i.e. link, speed, etc.).
4400255736Sdavidch */
4401255736Sdavidchstatic void
4402255736Sdavidchbxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4403255736Sdavidch{
4404266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4405255736Sdavidch
4406255736Sdavidch    /* Report link down if the driver isn't running. */
4407266979Smarcel    if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
4408255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4409255736Sdavidch        return;
4410255736Sdavidch    }
4411255736Sdavidch
4412255736Sdavidch    /* Setup the default interface info. */
4413255736Sdavidch    ifmr->ifm_status = IFM_AVALID;
4414255736Sdavidch    ifmr->ifm_active = IFM_ETHER;
4415255736Sdavidch
4416255736Sdavidch    if (sc->link_vars.link_up) {
4417255736Sdavidch        ifmr->ifm_status |= IFM_ACTIVE;
4418255736Sdavidch    } else {
4419255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4420255736Sdavidch        return;
4421255736Sdavidch    }
4422255736Sdavidch
4423255736Sdavidch    ifmr->ifm_active |= sc->media;
4424255736Sdavidch
4425255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
4426255736Sdavidch        ifmr->ifm_active |= IFM_FDX;
4427255736Sdavidch    } else {
4428255736Sdavidch        ifmr->ifm_active |= IFM_HDX;
4429255736Sdavidch    }
4430255736Sdavidch}
4431255736Sdavidch
4432255736Sdavidchstatic void
4433255736Sdavidchbxe_handle_chip_tq(void *context,
4434255736Sdavidch                   int  pending)
4435255736Sdavidch{
4436255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
4437255736Sdavidch    long work = atomic_load_acq_long(&sc->chip_tq_flags);
4438255736Sdavidch
4439255736Sdavidch    switch (work)
4440255736Sdavidch    {
4441255736Sdavidch
4442255736Sdavidch    case CHIP_TQ_REINIT:
4443266979Smarcel        if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4444255736Sdavidch            /* restart the interface */
4445255736Sdavidch            BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4446255736Sdavidch            bxe_periodic_stop(sc);
4447255736Sdavidch            BXE_CORE_LOCK(sc);
4448255736Sdavidch            bxe_stop_locked(sc);
4449255736Sdavidch            bxe_init_locked(sc);
4450255736Sdavidch            BXE_CORE_UNLOCK(sc);
4451255736Sdavidch        }
4452255736Sdavidch        break;
4453255736Sdavidch
4454255736Sdavidch    default:
4455255736Sdavidch        break;
4456255736Sdavidch    }
4457255736Sdavidch}
4458255736Sdavidch
4459255736Sdavidch/*
4460255736Sdavidch * Handles any IOCTL calls from the operating system.
4461255736Sdavidch *
4462255736Sdavidch * Returns:
4463255736Sdavidch *   0 = Success, >0 Failure
4464255736Sdavidch */
4465255736Sdavidchstatic int
4466266979Smarcelbxe_ioctl(if_t ifp,
4467255736Sdavidch          u_long       command,
4468255736Sdavidch          caddr_t      data)
4469255736Sdavidch{
4470266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4471255736Sdavidch    struct ifreq *ifr = (struct ifreq *)data;
4472255736Sdavidch    int mask = 0;
4473255736Sdavidch    int reinit = 0;
4474255736Sdavidch    int error = 0;
4475255736Sdavidch
4476255736Sdavidch    int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4477255736Sdavidch    int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4478255736Sdavidch
4479255736Sdavidch    switch (command)
4480255736Sdavidch    {
4481255736Sdavidch    case SIOCSIFMTU:
4482255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4483255736Sdavidch              ifr->ifr_mtu);
4484255736Sdavidch
4485255736Sdavidch        if (sc->mtu == ifr->ifr_mtu) {
4486255736Sdavidch            /* nothing to change */
4487255736Sdavidch            break;
4488255736Sdavidch        }
4489255736Sdavidch
4490255736Sdavidch        if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4491255736Sdavidch            BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4492255736Sdavidch                  ifr->ifr_mtu, mtu_min, mtu_max);
4493255736Sdavidch            error = EINVAL;
4494255736Sdavidch            break;
4495255736Sdavidch        }
4496255736Sdavidch
4497255736Sdavidch        atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4498255736Sdavidch                             (unsigned long)ifr->ifr_mtu);
4499266979Smarcel	/*
4500266979Smarcel        atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4501255736Sdavidch                              (unsigned long)ifr->ifr_mtu);
4502266979Smarcel	XXX - Not sure why it needs to be atomic
4503266979Smarcel	*/
4504266979Smarcel	if_setmtu(ifp, ifr->ifr_mtu);
4505255736Sdavidch        reinit = 1;
4506255736Sdavidch        break;
4507255736Sdavidch
4508255736Sdavidch    case SIOCSIFFLAGS:
4509255736Sdavidch        /* toggle the interface state up or down */
4510255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4511255736Sdavidch
4512284335Sdavidcs	BXE_CORE_LOCK(sc);
4513255736Sdavidch        /* check if the interface is up */
4514266979Smarcel        if (if_getflags(ifp) & IFF_UP) {
4515266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4516255736Sdavidch                /* set the receive mode flags */
4517255736Sdavidch                bxe_set_rx_mode(sc);
4518297873Sdavidcs            } else if(sc->state != BXE_STATE_DISABLED) {
4519284335Sdavidcs		bxe_init_locked(sc);
4520255736Sdavidch            }
4521255736Sdavidch        } else {
4522266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4523284335Sdavidcs		bxe_periodic_stop(sc);
4524284335Sdavidcs		bxe_stop_locked(sc);
4525255736Sdavidch            }
4526255736Sdavidch        }
4527284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4528255736Sdavidch
4529255736Sdavidch        break;
4530255736Sdavidch
4531255736Sdavidch    case SIOCADDMULTI:
4532255736Sdavidch    case SIOCDELMULTI:
4533255736Sdavidch        /* add/delete multicast addresses */
4534255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4535255736Sdavidch
4536255736Sdavidch        /* check if the interface is up */
4537266979Smarcel        if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4538255736Sdavidch            /* set the receive mode flags */
4539284335Sdavidcs	    BXE_CORE_LOCK(sc);
4540255736Sdavidch            bxe_set_rx_mode(sc);
4541284335Sdavidcs	    BXE_CORE_UNLOCK(sc);
4542255736Sdavidch        }
4543255736Sdavidch
4544255736Sdavidch        break;
4545255736Sdavidch
4546255736Sdavidch    case SIOCSIFCAP:
4547255736Sdavidch        /* find out which capabilities have changed */
4548266979Smarcel        mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4549255736Sdavidch
4550255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4551255736Sdavidch              mask);
4552255736Sdavidch
4553255736Sdavidch        /* toggle the LRO capabilites enable flag */
4554255736Sdavidch        if (mask & IFCAP_LRO) {
4555266979Smarcel	    if_togglecapenable(ifp, IFCAP_LRO);
4556255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4557266979Smarcel                  (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4558255736Sdavidch            reinit = 1;
4559255736Sdavidch        }
4560255736Sdavidch
4561255736Sdavidch        /* toggle the TXCSUM checksum capabilites enable flag */
4562255736Sdavidch        if (mask & IFCAP_TXCSUM) {
4563266979Smarcel	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4564255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4565266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4566266979Smarcel            if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4567266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4568255736Sdavidch                                    CSUM_TCP      |
4569255736Sdavidch                                    CSUM_UDP      |
4570255736Sdavidch                                    CSUM_TSO      |
4571255736Sdavidch                                    CSUM_TCP_IPV6 |
4572266979Smarcel                                    CSUM_UDP_IPV6), 0);
4573255736Sdavidch            } else {
4574266979Smarcel		if_clearhwassist(ifp); /* XXX */
4575255736Sdavidch            }
4576255736Sdavidch        }
4577255736Sdavidch
4578255736Sdavidch        /* toggle the RXCSUM checksum capabilities enable flag */
4579255736Sdavidch        if (mask & IFCAP_RXCSUM) {
4580266979Smarcel	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4581255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4582266979Smarcel                  (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4583266979Smarcel            if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4584266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4585255736Sdavidch                                    CSUM_TCP      |
4586255736Sdavidch                                    CSUM_UDP      |
4587255736Sdavidch                                    CSUM_TSO      |
4588255736Sdavidch                                    CSUM_TCP_IPV6 |
4589266979Smarcel                                    CSUM_UDP_IPV6), 0);
4590255736Sdavidch            } else {
4591266979Smarcel		if_clearhwassist(ifp); /* XXX */
4592255736Sdavidch            }
4593255736Sdavidch        }
4594255736Sdavidch
4595255736Sdavidch        /* toggle TSO4 capabilities enabled flag */
4596255736Sdavidch        if (mask & IFCAP_TSO4) {
4597266979Smarcel            if_togglecapenable(ifp, IFCAP_TSO4);
4598255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4599266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4600255736Sdavidch        }
4601255736Sdavidch
4602255736Sdavidch        /* toggle TSO6 capabilities enabled flag */
4603255736Sdavidch        if (mask & IFCAP_TSO6) {
4604266979Smarcel	    if_togglecapenable(ifp, IFCAP_TSO6);
4605255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4606266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4607255736Sdavidch        }
4608255736Sdavidch
4609255736Sdavidch        /* toggle VLAN_HWTSO capabilities enabled flag */
4610255736Sdavidch        if (mask & IFCAP_VLAN_HWTSO) {
4611266979Smarcel
4612266979Smarcel	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4613255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4614266979Smarcel                  (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4615255736Sdavidch        }
4616255736Sdavidch
4617255736Sdavidch        /* toggle VLAN_HWCSUM capabilities enabled flag */
4618255736Sdavidch        if (mask & IFCAP_VLAN_HWCSUM) {
4619255736Sdavidch            /* XXX investigate this... */
4620255736Sdavidch            BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4621255736Sdavidch            error = EINVAL;
4622255736Sdavidch        }
4623255736Sdavidch
4624255736Sdavidch        /* toggle VLAN_MTU capabilities enable flag */
4625255736Sdavidch        if (mask & IFCAP_VLAN_MTU) {
4626255736Sdavidch            /* XXX investigate this... */
4627255736Sdavidch            BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4628255736Sdavidch            error = EINVAL;
4629255736Sdavidch        }
4630255736Sdavidch
4631255736Sdavidch        /* toggle VLAN_HWTAGGING capabilities enabled flag */
4632255736Sdavidch        if (mask & IFCAP_VLAN_HWTAGGING) {
4633255736Sdavidch            /* XXX investigate this... */
4634255736Sdavidch            BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4635255736Sdavidch            error = EINVAL;
4636255736Sdavidch        }
4637255736Sdavidch
4638255736Sdavidch        /* toggle VLAN_HWFILTER capabilities enabled flag */
4639255736Sdavidch        if (mask & IFCAP_VLAN_HWFILTER) {
4640255736Sdavidch            /* XXX investigate this... */
4641255736Sdavidch            BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4642255736Sdavidch            error = EINVAL;
4643255736Sdavidch        }
4644255736Sdavidch
4645255736Sdavidch        /* XXX not yet...
4646255736Sdavidch         * IFCAP_WOL_MAGIC
4647255736Sdavidch         */
4648255736Sdavidch
4649255736Sdavidch        break;
4650255736Sdavidch
4651255736Sdavidch    case SIOCSIFMEDIA:
4652255736Sdavidch    case SIOCGIFMEDIA:
4653255736Sdavidch        /* set/get interface media */
4654255736Sdavidch        BLOGD(sc, DBG_IOCTL,
4655255736Sdavidch              "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4656255736Sdavidch              (command & 0xff));
4657270876Sglebius        error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4658255736Sdavidch        break;
4659255736Sdavidch
4660255736Sdavidch    default:
4661255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4662255736Sdavidch              (command & 0xff));
4663270876Sglebius        error = ether_ioctl(ifp, command, data);
4664255736Sdavidch        break;
4665255736Sdavidch    }
4666255736Sdavidch
4667266979Smarcel    if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
4668255736Sdavidch        BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4669255736Sdavidch              "Re-initializing hardware from IOCTL change\n");
4670284335Sdavidcs	bxe_periodic_stop(sc);
4671284335Sdavidcs	BXE_CORE_LOCK(sc);
4672284335Sdavidcs	bxe_stop_locked(sc);
4673284335Sdavidcs	bxe_init_locked(sc);
4674284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4675255736Sdavidch    }
4676255736Sdavidch
4677255736Sdavidch    return (error);
4678255736Sdavidch}
4679255736Sdavidch
4680255736Sdavidchstatic __noinline void
4681255736Sdavidchbxe_dump_mbuf(struct bxe_softc *sc,
4682255736Sdavidch              struct mbuf      *m,
4683255736Sdavidch              uint8_t          contents)
4684255736Sdavidch{
4685255736Sdavidch    char * type;
4686260113Sedavis    int i = 0;
4687255736Sdavidch
4688255736Sdavidch    if (!(sc->debug & DBG_MBUF)) {
4689255736Sdavidch        return;
4690255736Sdavidch    }
4691255736Sdavidch
4692255736Sdavidch    if (m == NULL) {
4693255736Sdavidch        BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4694255736Sdavidch        return;
4695255736Sdavidch    }
4696255736Sdavidch
4697255736Sdavidch    while (m) {
4698297155Sdavidcs
4699297155Sdavidcs#if __FreeBSD_version >= 1000000
4700255736Sdavidch        BLOGD(sc, DBG_MBUF,
4701260113Sedavis              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4702260113Sedavis              i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
4703255736Sdavidch
4704255736Sdavidch        if (m->m_flags & M_PKTHDR) {
4705255736Sdavidch             BLOGD(sc, DBG_MBUF,
4706260113Sedavis                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4707260113Sedavis                   i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
4708260113Sedavis                   (int)m->m_pkthdr.csum_flags, CSUM_BITS);
4709255736Sdavidch        }
4710297155Sdavidcs#else
4711297155Sdavidcs        BLOGD(sc, DBG_MBUF,
4712297155Sdavidcs              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4713297155Sdavidcs              i, m, m->m_len, m->m_flags,
4714297155Sdavidcs              "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
4715255736Sdavidch
4716297155Sdavidcs        if (m->m_flags & M_PKTHDR) {
4717297155Sdavidcs             BLOGD(sc, DBG_MBUF,
4718297155Sdavidcs                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4719297155Sdavidcs                   i, m->m_pkthdr.len, m->m_flags,
4720297155Sdavidcs                   "\20\12M_BCAST\13M_MCAST\14M_FRAG"
4721297155Sdavidcs                   "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
4722297155Sdavidcs                   "\22M_PROMISC\23M_NOFREE",
4723297155Sdavidcs                   (int)m->m_pkthdr.csum_flags,
4724297155Sdavidcs                   "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
4725297155Sdavidcs                   "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
4726297155Sdavidcs                   "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
4727297155Sdavidcs                   "\14CSUM_PSEUDO_HDR");
4728297155Sdavidcs        }
4729297155Sdavidcs#endif /* #if __FreeBSD_version >= 1000000 */
4730297155Sdavidcs
4731255736Sdavidch        if (m->m_flags & M_EXT) {
4732255736Sdavidch            switch (m->m_ext.ext_type) {
4733255736Sdavidch            case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4734255736Sdavidch            case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4735260113Sedavis            case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4736255736Sdavidch            case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4737255736Sdavidch            case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4738255736Sdavidch            case EXT_PACKET:     type = "EXT_PACKET";     break;
4739255736Sdavidch            case EXT_MBUF:       type = "EXT_MBUF";       break;
4740255736Sdavidch            case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4741255736Sdavidch            case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4742255736Sdavidch            case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4743255736Sdavidch            case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4744255736Sdavidch            default:             type = "UNKNOWN";        break;
4745255736Sdavidch            }
4746255736Sdavidch
4747255736Sdavidch            BLOGD(sc, DBG_MBUF,
4748260113Sedavis                  "%02d: - m_ext: %p ext_size=%d type=%s\n",
4749260113Sedavis                  i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4750255736Sdavidch        }
4751255736Sdavidch
4752255736Sdavidch        if (contents) {
4753255736Sdavidch            bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4754255736Sdavidch        }
4755255736Sdavidch
4756255736Sdavidch        m = m->m_next;
4757260113Sedavis        i++;
4758255736Sdavidch    }
4759255736Sdavidch}
4760255736Sdavidch
4761255736Sdavidch/*
4762255736Sdavidch * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4763255736Sdavidch * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4764255736Sdavidch * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4765298955Spfg * The headers comes in a separate bd in FreeBSD so 13-3=10.
4766255736Sdavidch * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4767255736Sdavidch */
4768255736Sdavidchstatic int
4769255736Sdavidchbxe_chktso_window(struct bxe_softc  *sc,
4770255736Sdavidch                  int               nsegs,
4771255736Sdavidch                  bus_dma_segment_t *segs,
4772255736Sdavidch                  struct mbuf       *m)
4773255736Sdavidch{
4774255736Sdavidch    uint32_t num_wnds, wnd_size, wnd_sum;
4775255736Sdavidch    int32_t frag_idx, wnd_idx;
4776255736Sdavidch    unsigned short lso_mss;
4777255736Sdavidch    int defrag;
4778255736Sdavidch
4779255736Sdavidch    defrag = 0;
4780255736Sdavidch    wnd_sum = 0;
4781255736Sdavidch    wnd_size = 10;
4782255736Sdavidch    num_wnds = nsegs - wnd_size;
4783255736Sdavidch    lso_mss = htole16(m->m_pkthdr.tso_segsz);
4784255736Sdavidch
4785255736Sdavidch    /*
4786255736Sdavidch     * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4787255736Sdavidch     * first window sum of data while skipping the first assuming it is the
4788255736Sdavidch     * header in FreeBSD.
4789255736Sdavidch     */
4790255736Sdavidch    for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4791255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4792255736Sdavidch    }
4793255736Sdavidch
4794255736Sdavidch    /* check the first 10 bd window size */
4795255736Sdavidch    if (wnd_sum < lso_mss) {
4796255736Sdavidch        return (1);
4797255736Sdavidch    }
4798255736Sdavidch
4799255736Sdavidch    /* run through the windows */
4800255736Sdavidch    for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4801255736Sdavidch        /* subtract the first mbuf->m_len of the last wndw(-header) */
4802255736Sdavidch        wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4803255736Sdavidch        /* add the next mbuf len to the len of our new window */
4804255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4805255736Sdavidch        if (wnd_sum < lso_mss) {
4806255736Sdavidch            return (1);
4807255736Sdavidch        }
4808255736Sdavidch    }
4809255736Sdavidch
4810255736Sdavidch    return (0);
4811255736Sdavidch}
4812255736Sdavidch
4813255736Sdavidchstatic uint8_t
4814255736Sdavidchbxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4815255736Sdavidch                    struct mbuf         *m,
4816255736Sdavidch                    uint32_t            *parsing_data)
4817255736Sdavidch{
4818255736Sdavidch    struct ether_vlan_header *eh = NULL;
4819255736Sdavidch    struct ip *ip4 = NULL;
4820255736Sdavidch    struct ip6_hdr *ip6 = NULL;
4821255736Sdavidch    caddr_t ip = NULL;
4822255736Sdavidch    struct tcphdr *th = NULL;
4823255736Sdavidch    int e_hlen, ip_hlen, l4_off;
4824255736Sdavidch    uint16_t proto;
4825255736Sdavidch
4826255736Sdavidch    if (m->m_pkthdr.csum_flags == CSUM_IP) {
4827255736Sdavidch        /* no L4 checksum offload needed */
4828255736Sdavidch        return (0);
4829255736Sdavidch    }
4830255736Sdavidch
4831255736Sdavidch    /* get the Ethernet header */
4832255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
4833255736Sdavidch
4834255736Sdavidch    /* handle VLAN encapsulation if present */
4835255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4836255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4837255736Sdavidch        proto  = ntohs(eh->evl_proto);
4838255736Sdavidch    } else {
4839255736Sdavidch        e_hlen = ETHER_HDR_LEN;
4840255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
4841255736Sdavidch    }
4842255736Sdavidch
4843255736Sdavidch    switch (proto) {
4844255736Sdavidch    case ETHERTYPE_IP:
4845255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
4846255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
4847255736Sdavidch                  (struct ip *)m->m_next->m_data :
4848255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
4849255736Sdavidch        /* ip_hl is number of 32-bit words */
4850255736Sdavidch        ip_hlen = (ip4->ip_hl << 2);
4851255736Sdavidch        ip = (caddr_t)ip4;
4852255736Sdavidch        break;
4853255736Sdavidch    case ETHERTYPE_IPV6:
4854255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4855255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4856255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
4857255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
4858255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
4859255736Sdavidch        ip_hlen = sizeof(struct ip6_hdr);
4860255736Sdavidch        ip = (caddr_t)ip6;
4861255736Sdavidch        break;
4862255736Sdavidch    default:
4863255736Sdavidch        /* We can't offload in this case... */
4864255736Sdavidch        /* XXX error stat ??? */
4865255736Sdavidch        return (0);
4866255736Sdavidch    }
4867255736Sdavidch
4868255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4869255736Sdavidch    l4_off = (e_hlen + ip_hlen);
4870255736Sdavidch
4871255736Sdavidch    *parsing_data |=
4872255736Sdavidch        (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
4873255736Sdavidch         ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
4874255736Sdavidch
4875255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4876255736Sdavidch                                  CSUM_TSO |
4877255736Sdavidch                                  CSUM_TCP_IPV6)) {
4878255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4879255736Sdavidch        th = (struct tcphdr *)(ip + ip_hlen);
4880255736Sdavidch        /* th_off is number of 32-bit words */
4881255736Sdavidch        *parsing_data |= ((th->th_off <<
4882255736Sdavidch                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
4883255736Sdavidch                          ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
4884255736Sdavidch        return (l4_off + (th->th_off << 2)); /* entire header length */
4885255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4886255736Sdavidch                                         CSUM_UDP_IPV6)) {
4887255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4888255736Sdavidch        return (l4_off + sizeof(struct udphdr)); /* entire header length */
4889255736Sdavidch    } else {
4890255736Sdavidch        /* XXX error stat ??? */
4891255736Sdavidch        return (0);
4892255736Sdavidch    }
4893255736Sdavidch}
4894255736Sdavidch
4895255736Sdavidchstatic uint8_t
4896255736Sdavidchbxe_set_pbd_csum(struct bxe_fastpath        *fp,
4897255736Sdavidch                 struct mbuf                *m,
4898255736Sdavidch                 struct eth_tx_parse_bd_e1x *pbd)
4899255736Sdavidch{
4900255736Sdavidch    struct ether_vlan_header *eh = NULL;
4901255736Sdavidch    struct ip *ip4 = NULL;
4902255736Sdavidch    struct ip6_hdr *ip6 = NULL;
4903255736Sdavidch    caddr_t ip = NULL;
4904255736Sdavidch    struct tcphdr *th = NULL;
4905255736Sdavidch    struct udphdr *uh = NULL;
4906255736Sdavidch    int e_hlen, ip_hlen;
4907255736Sdavidch    uint16_t proto;
4908255736Sdavidch    uint8_t hlen;
4909255736Sdavidch    uint16_t tmp_csum;
4910255736Sdavidch    uint32_t *tmp_uh;
4911255736Sdavidch
4912255736Sdavidch    /* get the Ethernet header */
4913255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
4914255736Sdavidch
4915255736Sdavidch    /* handle VLAN encapsulation if present */
4916255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4917255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4918255736Sdavidch        proto  = ntohs(eh->evl_proto);
4919255736Sdavidch    } else {
4920255736Sdavidch        e_hlen = ETHER_HDR_LEN;
4921255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
4922255736Sdavidch    }
4923255736Sdavidch
4924255736Sdavidch    switch (proto) {
4925255736Sdavidch    case ETHERTYPE_IP:
4926255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
4927255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
4928255736Sdavidch                  (struct ip *)m->m_next->m_data :
4929255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
4930255736Sdavidch        /* ip_hl is number of 32-bit words */
4931255736Sdavidch        ip_hlen = (ip4->ip_hl << 1);
4932255736Sdavidch        ip = (caddr_t)ip4;
4933255736Sdavidch        break;
4934255736Sdavidch    case ETHERTYPE_IPV6:
4935255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4936255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4937255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
4938255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
4939255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
4940255736Sdavidch        ip_hlen = (sizeof(struct ip6_hdr) >> 1);
4941255736Sdavidch        ip = (caddr_t)ip6;
4942255736Sdavidch        break;
4943255736Sdavidch    default:
4944255736Sdavidch        /* We can't offload in this case... */
4945255736Sdavidch        /* XXX error stat ??? */
4946255736Sdavidch        return (0);
4947255736Sdavidch    }
4948255736Sdavidch
4949255736Sdavidch    hlen = (e_hlen >> 1);
4950255736Sdavidch
4951255736Sdavidch    /* note that rest of global_data is indirectly zeroed here */
4952255736Sdavidch    if (m->m_flags & M_VLANTAG) {
4953255736Sdavidch        pbd->global_data =
4954255736Sdavidch            htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
4955255736Sdavidch    } else {
4956255736Sdavidch        pbd->global_data = htole16(hlen);
4957255736Sdavidch    }
4958255736Sdavidch
4959255736Sdavidch    pbd->ip_hlen_w = ip_hlen;
4960255736Sdavidch
4961255736Sdavidch    hlen += pbd->ip_hlen_w;
4962255736Sdavidch
4963255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4964255736Sdavidch
4965255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4966255736Sdavidch                                  CSUM_TSO |
4967255736Sdavidch                                  CSUM_TCP_IPV6)) {
4968255736Sdavidch        th = (struct tcphdr *)(ip + (ip_hlen << 1));
4969255736Sdavidch        /* th_off is number of 32-bit words */
4970255736Sdavidch        hlen += (uint16_t)(th->th_off << 1);
4971255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4972255736Sdavidch                                         CSUM_UDP_IPV6)) {
4973255736Sdavidch        uh = (struct udphdr *)(ip + (ip_hlen << 1));
4974255736Sdavidch        hlen += (sizeof(struct udphdr) / 2);
4975255736Sdavidch    } else {
4976255736Sdavidch        /* valid case as only CSUM_IP was set */
4977255736Sdavidch        return (0);
4978255736Sdavidch    }
4979255736Sdavidch
4980255736Sdavidch    pbd->total_hlen_w = htole16(hlen);
4981255736Sdavidch
4982255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4983255736Sdavidch                                  CSUM_TSO |
4984255736Sdavidch                                  CSUM_TCP_IPV6)) {
4985255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4986255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(th->th_sum);
4987255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4988255736Sdavidch                                         CSUM_UDP_IPV6)) {
4989255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4990255736Sdavidch
4991255736Sdavidch        /*
4992255736Sdavidch         * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
4993255736Sdavidch         * checksums and does not know anything about the UDP header and where
4994255736Sdavidch         * the checksum field is located. It only knows about TCP. Therefore
4995255736Sdavidch         * we "lie" to the hardware for outgoing UDP packets w/ checksum
4996255736Sdavidch         * offload. Since the checksum field offset for TCP is 16 bytes and
4997255736Sdavidch         * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
4998255736Sdavidch         * bytes less than the start of the UDP header. This allows the
4999255736Sdavidch         * hardware to write the checksum in the correct spot. But the
5000255736Sdavidch         * hardware will compute a checksum which includes the last 10 bytes
5001255736Sdavidch         * of the IP header. To correct this we tweak the stack computed
5002255736Sdavidch         * pseudo checksum by folding in the calculation of the inverse
5003255736Sdavidch         * checksum for those final 10 bytes of the IP header. This allows
5004255736Sdavidch         * the correct checksum to be computed by the hardware.
5005255736Sdavidch         */
5006255736Sdavidch
5007255736Sdavidch        /* set pointer 10 bytes before UDP header */
5008255736Sdavidch        tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5009255736Sdavidch
5010255736Sdavidch        /* calculate a pseudo header checksum over the first 10 bytes */
5011255736Sdavidch        tmp_csum = in_pseudo(*tmp_uh,
5012255736Sdavidch                             *(tmp_uh + 1),
5013255736Sdavidch                             *(uint16_t *)(tmp_uh + 2));
5014255736Sdavidch
5015255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5016255736Sdavidch    }
5017255736Sdavidch
5018255736Sdavidch    return (hlen * 2); /* entire header length, number of bytes */
5019255736Sdavidch}
5020255736Sdavidch
5021255736Sdavidchstatic void
5022255736Sdavidchbxe_set_pbd_lso_e2(struct mbuf *m,
5023255736Sdavidch                   uint32_t    *parsing_data)
5024255736Sdavidch{
5025255736Sdavidch    *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5026255736Sdavidch                       ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5027255736Sdavidch                      ETH_TX_PARSE_BD_E2_LSO_MSS);
5028255736Sdavidch
5029255736Sdavidch    /* XXX test for IPv6 with extension header... */
5030255736Sdavidch}
5031255736Sdavidch
5032255736Sdavidchstatic void
5033255736Sdavidchbxe_set_pbd_lso(struct mbuf                *m,
5034255736Sdavidch                struct eth_tx_parse_bd_e1x *pbd)
5035255736Sdavidch{
5036255736Sdavidch    struct ether_vlan_header *eh = NULL;
5037255736Sdavidch    struct ip *ip = NULL;
5038255736Sdavidch    struct tcphdr *th = NULL;
5039255736Sdavidch    int e_hlen;
5040255736Sdavidch
5041255736Sdavidch    /* get the Ethernet header */
5042255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5043255736Sdavidch
5044255736Sdavidch    /* handle VLAN encapsulation if present */
5045255736Sdavidch    e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5046255736Sdavidch                 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5047255736Sdavidch
5048255736Sdavidch    /* get the IP and TCP header, with LSO entire header in first mbuf */
5049255736Sdavidch    /* XXX assuming IPv4 */
5050255736Sdavidch    ip = (struct ip *)(m->m_data + e_hlen);
5051255736Sdavidch    th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5052255736Sdavidch
5053255736Sdavidch    pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5054255736Sdavidch    pbd->tcp_send_seq = ntohl(th->th_seq);
5055255736Sdavidch    pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5056255736Sdavidch
5057255736Sdavidch#if 1
5058255736Sdavidch        /* XXX IPv4 */
5059255736Sdavidch        pbd->ip_id = ntohs(ip->ip_id);
5060255736Sdavidch        pbd->tcp_pseudo_csum =
5061255736Sdavidch            ntohs(in_pseudo(ip->ip_src.s_addr,
5062255736Sdavidch                            ip->ip_dst.s_addr,
5063255736Sdavidch                            htons(IPPROTO_TCP)));
5064255736Sdavidch#else
5065255736Sdavidch        /* XXX IPv6 */
5066255736Sdavidch        pbd->tcp_pseudo_csum =
5067255736Sdavidch            ntohs(in_pseudo(&ip6->ip6_src,
5068255736Sdavidch                            &ip6->ip6_dst,
5069255736Sdavidch                            htons(IPPROTO_TCP)));
5070255736Sdavidch#endif
5071255736Sdavidch
5072255736Sdavidch    pbd->global_data |=
5073255736Sdavidch        htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5074255736Sdavidch}
5075255736Sdavidch
5076255736Sdavidch/*
5077255736Sdavidch * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5078255736Sdavidch * visible to the controller.
5079255736Sdavidch *
5080255736Sdavidch * If an mbuf is submitted to this routine and cannot be given to the
5081255736Sdavidch * controller (e.g. it has too many fragments) then the function may free
5082255736Sdavidch * the mbuf and return to the caller.
5083255736Sdavidch *
5084255736Sdavidch * Returns:
5085255736Sdavidch *   0 = Success, !0 = Failure
5086255736Sdavidch *   Note the side effect that an mbuf may be freed if it causes a problem.
5087255736Sdavidch */
5088255736Sdavidchstatic int
5089255736Sdavidchbxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5090255736Sdavidch{
5091255736Sdavidch    bus_dma_segment_t segs[32];
5092255736Sdavidch    struct mbuf *m0;
5093255736Sdavidch    struct bxe_sw_tx_bd *tx_buf;
5094255736Sdavidch    struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5095255736Sdavidch    struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5096255736Sdavidch    /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5097255736Sdavidch    struct eth_tx_bd *tx_data_bd;
5098255736Sdavidch    struct eth_tx_bd *tx_total_pkt_size_bd;
5099255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
5100255736Sdavidch    uint16_t bd_prod, pkt_prod, total_pkt_size;
5101255736Sdavidch    uint8_t mac_type;
5102255736Sdavidch    int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5103255736Sdavidch    struct bxe_softc *sc;
5104255736Sdavidch    uint16_t tx_bd_avail;
5105255736Sdavidch    struct ether_vlan_header *eh;
5106255736Sdavidch    uint32_t pbd_e2_parsing_data = 0;
5107255736Sdavidch    uint8_t hlen = 0;
5108255736Sdavidch    int tmp_bd;
5109255736Sdavidch    int i;
5110255736Sdavidch
5111255736Sdavidch    sc = fp->sc;
5112255736Sdavidch
5113297155Sdavidcs#if __FreeBSD_version >= 800000
5114255736Sdavidch    M_ASSERTPKTHDR(*m_head);
5115297155Sdavidcs#endif /* #if __FreeBSD_version >= 800000 */
5116255736Sdavidch
5117255736Sdavidch    m0 = *m_head;
5118255736Sdavidch    rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5119255736Sdavidch    tx_start_bd = NULL;
5120255736Sdavidch    tx_data_bd = NULL;
5121255736Sdavidch    tx_total_pkt_size_bd = NULL;
5122255736Sdavidch
5123255736Sdavidch    /* get the H/W pointer for packets and BDs */
5124255736Sdavidch    pkt_prod = fp->tx_pkt_prod;
5125255736Sdavidch    bd_prod = fp->tx_bd_prod;
5126255736Sdavidch
5127255736Sdavidch    mac_type = UNICAST_ADDRESS;
5128255736Sdavidch
5129255736Sdavidch    /* map the mbuf into the next open DMAable memory */
5130255736Sdavidch    tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5131255736Sdavidch    error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5132255736Sdavidch                                    tx_buf->m_map, m0,
5133255736Sdavidch                                    segs, &nsegs, BUS_DMA_NOWAIT);
5134255736Sdavidch
5135255736Sdavidch    /* mapping errors */
5136255736Sdavidch    if(__predict_false(error != 0)) {
5137255736Sdavidch        fp->eth_q_stats.tx_dma_mapping_failure++;
5138255736Sdavidch        if (error == ENOMEM) {
5139255736Sdavidch            /* resource issue, try again later */
5140255736Sdavidch            rc = ENOMEM;
5141255736Sdavidch        } else if (error == EFBIG) {
5142255736Sdavidch            /* possibly recoverable with defragmentation */
5143255736Sdavidch            fp->eth_q_stats.mbuf_defrag_attempts++;
5144260718Sglebius            m0 = m_defrag(*m_head, M_NOWAIT);
5145255736Sdavidch            if (m0 == NULL) {
5146255736Sdavidch                fp->eth_q_stats.mbuf_defrag_failures++;
5147255736Sdavidch                rc = ENOBUFS;
5148255736Sdavidch            } else {
5149255736Sdavidch                /* defrag successful, try mapping again */
5150255736Sdavidch                *m_head = m0;
5151255736Sdavidch                error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5152255736Sdavidch                                                tx_buf->m_map, m0,
5153255736Sdavidch                                                segs, &nsegs, BUS_DMA_NOWAIT);
5154255736Sdavidch                if (error) {
5155255736Sdavidch                    fp->eth_q_stats.tx_dma_mapping_failure++;
5156255736Sdavidch                    rc = error;
5157255736Sdavidch                }
5158255736Sdavidch            }
5159255736Sdavidch        } else {
5160255736Sdavidch            /* unknown, unrecoverable mapping error */
5161255736Sdavidch            BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5162255736Sdavidch            bxe_dump_mbuf(sc, m0, FALSE);
5163255736Sdavidch            rc = error;
5164255736Sdavidch        }
5165255736Sdavidch
5166255736Sdavidch        goto bxe_tx_encap_continue;
5167255736Sdavidch    }
5168255736Sdavidch
5169255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
5170255736Sdavidch
5171255736Sdavidch    /* make sure there is enough room in the send queue */
5172255736Sdavidch    if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5173255736Sdavidch        /* Recoverable, try again later. */
5174255736Sdavidch        fp->eth_q_stats.tx_hw_queue_full++;
5175255736Sdavidch        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5176255736Sdavidch        rc = ENOMEM;
5177255736Sdavidch        goto bxe_tx_encap_continue;
5178255736Sdavidch    }
5179255736Sdavidch
5180255736Sdavidch    /* capture the current H/W TX chain high watermark */
5181255736Sdavidch    if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5182255736Sdavidch                        (TX_BD_USABLE - tx_bd_avail))) {
5183255736Sdavidch        fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5184255736Sdavidch    }
5185255736Sdavidch
5186255736Sdavidch    /* make sure it fits in the packet window */
5187262999Sedavis    if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5188255736Sdavidch        /*
5189255736Sdavidch         * The mbuf may be to big for the controller to handle. If the frame
5190255736Sdavidch         * is a TSO frame we'll need to do an additional check.
5191255736Sdavidch         */
5192255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5193255736Sdavidch            if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5194255736Sdavidch                goto bxe_tx_encap_continue; /* OK to send */
5195255736Sdavidch            } else {
5196255736Sdavidch                fp->eth_q_stats.tx_window_violation_tso++;
5197255736Sdavidch            }
5198255736Sdavidch        } else {
5199255736Sdavidch            fp->eth_q_stats.tx_window_violation_std++;
5200255736Sdavidch        }
5201255736Sdavidch
5202262999Sedavis        /* lets try to defragment this mbuf and remap it */
5203260415Sedavis        fp->eth_q_stats.mbuf_defrag_attempts++;
5204262999Sedavis        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5205255736Sdavidch
5206260718Sglebius        m0 = m_defrag(*m_head, M_NOWAIT);
5207260415Sedavis        if (m0 == NULL) {
5208260415Sedavis            fp->eth_q_stats.mbuf_defrag_failures++;
5209260415Sedavis            /* Ugh, just drop the frame... :( */
5210260415Sedavis            rc = ENOBUFS;
5211260415Sedavis        } else {
5212260415Sedavis            /* defrag successful, try mapping again */
5213260415Sedavis            *m_head = m0;
5214260415Sedavis            error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5215260415Sedavis                                            tx_buf->m_map, m0,
5216260415Sedavis                                            segs, &nsegs, BUS_DMA_NOWAIT);
5217260415Sedavis            if (error) {
5218260415Sedavis                fp->eth_q_stats.tx_dma_mapping_failure++;
5219260415Sedavis                /* No sense in trying to defrag/copy chain, drop it. :( */
5220260415Sedavis                rc = error;
5221260415Sedavis            }
5222262999Sedavis            else {
5223262999Sedavis                /* if the chain is still too long then drop it */
5224262999Sedavis                if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5225262999Sedavis                    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5226262999Sedavis                    rc = ENODEV;
5227262999Sedavis                }
5228260415Sedavis            }
5229260415Sedavis        }
5230255736Sdavidch    }
5231255736Sdavidch
5232255736Sdavidchbxe_tx_encap_continue:
5233255736Sdavidch
5234255736Sdavidch    /* Check for errors */
5235255736Sdavidch    if (rc) {
5236255736Sdavidch        if (rc == ENOMEM) {
5237255736Sdavidch            /* recoverable try again later  */
5238255736Sdavidch        } else {
5239255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
5240255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tx--;
5241255736Sdavidch            m_freem(*m_head);
5242255736Sdavidch            *m_head = NULL;
5243255736Sdavidch        }
5244255736Sdavidch
5245255736Sdavidch        return (rc);
5246255736Sdavidch    }
5247255736Sdavidch
5248255736Sdavidch    /* set flag according to packet type (UNICAST_ADDRESS is default) */
5249255736Sdavidch    if (m0->m_flags & M_BCAST) {
5250255736Sdavidch        mac_type = BROADCAST_ADDRESS;
5251255736Sdavidch    } else if (m0->m_flags & M_MCAST) {
5252255736Sdavidch        mac_type = MULTICAST_ADDRESS;
5253255736Sdavidch    }
5254255736Sdavidch
5255255736Sdavidch    /* store the mbuf into the mbuf ring */
5256255736Sdavidch    tx_buf->m        = m0;
5257255736Sdavidch    tx_buf->first_bd = fp->tx_bd_prod;
5258255736Sdavidch    tx_buf->flags    = 0;
5259255736Sdavidch
5260255736Sdavidch    /* prepare the first transmit (start) BD for the mbuf */
5261255736Sdavidch    tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5262255736Sdavidch
5263255736Sdavidch    BLOGD(sc, DBG_TX,
5264255736Sdavidch          "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5265255736Sdavidch          pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5266255736Sdavidch
5267255736Sdavidch    tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5268255736Sdavidch    tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5269255736Sdavidch    tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5270255736Sdavidch    total_pkt_size += tx_start_bd->nbytes;
5271255736Sdavidch    tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5272255736Sdavidch
5273255736Sdavidch    tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5274255736Sdavidch
5275255736Sdavidch    /* all frames have at least Start BD + Parsing BD */
5276255736Sdavidch    nbds = nsegs + 1;
5277255736Sdavidch    tx_start_bd->nbd = htole16(nbds);
5278255736Sdavidch
5279255736Sdavidch    if (m0->m_flags & M_VLANTAG) {
5280255736Sdavidch        tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5281255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |=
5282255736Sdavidch            (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5283255736Sdavidch    } else {
5284255736Sdavidch        /* vf tx, start bd must hold the ethertype for fw to enforce it */
5285255736Sdavidch        if (IS_VF(sc)) {
5286255736Sdavidch            /* map ethernet header to find type and header length */
5287255736Sdavidch            eh = mtod(m0, struct ether_vlan_header *);
5288255736Sdavidch            tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5289255736Sdavidch        } else {
5290255736Sdavidch            /* used by FW for packet accounting */
5291255736Sdavidch            tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5292255736Sdavidch        }
5293255736Sdavidch    }
5294255736Sdavidch
5295255736Sdavidch    /*
5296255736Sdavidch     * add a parsing BD from the chain. The parsing BD is always added
5297255736Sdavidch     * though it is only used for TSO and chksum
5298255736Sdavidch     */
5299255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5300255736Sdavidch
5301255736Sdavidch    if (m0->m_pkthdr.csum_flags) {
5302255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5303255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5304255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5305255736Sdavidch        }
5306255736Sdavidch
5307255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5308255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5309255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5310255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5311255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5312255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP |
5313255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5314255736Sdavidch        } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5315255736Sdavidch                   (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5316255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5317255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5318255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5319255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP);
5320255736Sdavidch        }
5321255736Sdavidch    }
5322255736Sdavidch
5323255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
5324255736Sdavidch        pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5325255736Sdavidch        memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5326255736Sdavidch
5327255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5328255736Sdavidch            hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5329255736Sdavidch        }
5330255736Sdavidch
5331255736Sdavidch        SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5332255736Sdavidch                 mac_type);
5333255736Sdavidch    } else {
5334255736Sdavidch        uint16_t global_data = 0;
5335255736Sdavidch
5336255736Sdavidch        pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5337255736Sdavidch        memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5338255736Sdavidch
5339255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5340255736Sdavidch            hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5341255736Sdavidch        }
5342255736Sdavidch
5343255736Sdavidch        SET_FLAG(global_data,
5344255736Sdavidch                 ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5345255736Sdavidch        pbd_e1x->global_data |= htole16(global_data);
5346255736Sdavidch    }
5347255736Sdavidch
5348255736Sdavidch    /* setup the parsing BD with TSO specific info */
5349255736Sdavidch    if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5350255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_lso++;
5351255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5352255736Sdavidch
5353255736Sdavidch        if (__predict_false(tx_start_bd->nbytes > hlen)) {
5354255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5355255736Sdavidch
5356255736Sdavidch            /* split the first BD into header/data making the fw job easy */
5357255736Sdavidch            nbds++;
5358255736Sdavidch            tx_start_bd->nbd = htole16(nbds);
5359260113Sedavis            tx_start_bd->nbytes = htole16(hlen);
5360255736Sdavidch
5361255736Sdavidch            bd_prod = TX_BD_NEXT(bd_prod);
5362255736Sdavidch
5363255736Sdavidch            /* new transmit BD after the tx_parse_bd */
5364255736Sdavidch            tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5365255736Sdavidch            tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5366255736Sdavidch            tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5367255736Sdavidch            tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5368255736Sdavidch            if (tx_total_pkt_size_bd == NULL) {
5369255736Sdavidch                tx_total_pkt_size_bd = tx_data_bd;
5370255736Sdavidch            }
5371255736Sdavidch
5372255736Sdavidch            BLOGD(sc, DBG_TX,
5373255736Sdavidch                  "TSO split header size is %d (%x:%x) nbds %d\n",
5374255736Sdavidch                  le16toh(tx_start_bd->nbytes),
5375255736Sdavidch                  le32toh(tx_start_bd->addr_hi),
5376255736Sdavidch                  le32toh(tx_start_bd->addr_lo),
5377255736Sdavidch                  nbds);
5378255736Sdavidch        }
5379255736Sdavidch
5380255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
5381255736Sdavidch            bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5382255736Sdavidch        } else {
5383255736Sdavidch            bxe_set_pbd_lso(m0, pbd_e1x);
5384255736Sdavidch        }
5385255736Sdavidch    }
5386255736Sdavidch
5387255736Sdavidch    if (pbd_e2_parsing_data) {
5388255736Sdavidch        pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5389255736Sdavidch    }
5390255736Sdavidch
5391255736Sdavidch    /* prepare remaining BDs, start tx bd contains first seg/frag */
5392255736Sdavidch    for (i = 1; i < nsegs ; i++) {
5393255736Sdavidch        bd_prod = TX_BD_NEXT(bd_prod);
5394255736Sdavidch        tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5395255736Sdavidch        tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5396255736Sdavidch        tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5397255736Sdavidch        tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5398255736Sdavidch        if (tx_total_pkt_size_bd == NULL) {
5399255736Sdavidch            tx_total_pkt_size_bd = tx_data_bd;
5400255736Sdavidch        }
5401255736Sdavidch        total_pkt_size += tx_data_bd->nbytes;
5402255736Sdavidch    }
5403255736Sdavidch
5404255736Sdavidch    BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5405255736Sdavidch
5406255736Sdavidch    if (tx_total_pkt_size_bd != NULL) {
5407255736Sdavidch        tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5408255736Sdavidch    }
5409255736Sdavidch
5410255736Sdavidch    if (__predict_false(sc->debug & DBG_TX)) {
5411255736Sdavidch        tmp_bd = tx_buf->first_bd;
5412255736Sdavidch        for (i = 0; i < nbds; i++)
5413255736Sdavidch        {
5414255736Sdavidch            if (i == 0) {
5415255736Sdavidch                BLOGD(sc, DBG_TX,
5416255736Sdavidch                      "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5417255736Sdavidch                      "bd_flags=0x%x hdr_nbds=%d\n",
5418255736Sdavidch                      tx_start_bd,
5419255736Sdavidch                      tmp_bd,
5420255736Sdavidch                      le16toh(tx_start_bd->nbd),
5421255736Sdavidch                      le16toh(tx_start_bd->vlan_or_ethertype),
5422255736Sdavidch                      tx_start_bd->bd_flags.as_bitfield,
5423255736Sdavidch                      (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5424255736Sdavidch            } else if (i == 1) {
5425255736Sdavidch                if (pbd_e1x) {
5426255736Sdavidch                    BLOGD(sc, DBG_TX,
5427255736Sdavidch                          "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5428255736Sdavidch                          "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5429255736Sdavidch                          "tcp_seq=%u total_hlen_w=%u\n",
5430255736Sdavidch                          pbd_e1x,
5431255736Sdavidch                          tmp_bd,
5432255736Sdavidch                          pbd_e1x->global_data,
5433255736Sdavidch                          pbd_e1x->ip_hlen_w,
5434255736Sdavidch                          pbd_e1x->ip_id,
5435255736Sdavidch                          pbd_e1x->lso_mss,
5436255736Sdavidch                          pbd_e1x->tcp_flags,
5437255736Sdavidch                          pbd_e1x->tcp_pseudo_csum,
5438255736Sdavidch                          pbd_e1x->tcp_send_seq,
5439255736Sdavidch                          le16toh(pbd_e1x->total_hlen_w));
5440255736Sdavidch                } else { /* if (pbd_e2) */
5441255736Sdavidch                    BLOGD(sc, DBG_TX,
5442255736Sdavidch                          "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5443255736Sdavidch                          "src=%02x:%02x:%02x parsing_data=0x%x\n",
5444255736Sdavidch                          pbd_e2,
5445255736Sdavidch                          tmp_bd,
5446255736Sdavidch                          pbd_e2->data.mac_addr.dst_hi,
5447255736Sdavidch                          pbd_e2->data.mac_addr.dst_mid,
5448255736Sdavidch                          pbd_e2->data.mac_addr.dst_lo,
5449255736Sdavidch                          pbd_e2->data.mac_addr.src_hi,
5450255736Sdavidch                          pbd_e2->data.mac_addr.src_mid,
5451255736Sdavidch                          pbd_e2->data.mac_addr.src_lo,
5452255736Sdavidch                          pbd_e2->parsing_data);
5453255736Sdavidch                }
5454255736Sdavidch            }
5455255736Sdavidch
5456255736Sdavidch            if (i != 1) { /* skip parse db as it doesn't hold data */
5457255736Sdavidch                tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5458255736Sdavidch                BLOGD(sc, DBG_TX,
5459255736Sdavidch                      "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5460255736Sdavidch                      tx_data_bd,
5461255736Sdavidch                      tmp_bd,
5462255736Sdavidch                      le16toh(tx_data_bd->nbytes),
5463255736Sdavidch                      le32toh(tx_data_bd->addr_hi),
5464255736Sdavidch                      le32toh(tx_data_bd->addr_lo));
5465255736Sdavidch            }
5466255736Sdavidch
5467255736Sdavidch            tmp_bd = TX_BD_NEXT(tmp_bd);
5468255736Sdavidch        }
5469255736Sdavidch    }
5470255736Sdavidch
5471255736Sdavidch    BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5472255736Sdavidch
5473255736Sdavidch    /* update TX BD producer index value for next TX */
5474255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5475255736Sdavidch
5476255736Sdavidch    /*
5477255736Sdavidch     * If the chain of tx_bd's describing this frame is adjacent to or spans
5478255736Sdavidch     * an eth_tx_next_bd element then we need to increment the nbds value.
5479255736Sdavidch     */
5480255736Sdavidch    if (TX_BD_IDX(bd_prod) < nbds) {
5481255736Sdavidch        nbds++;
5482255736Sdavidch    }
5483255736Sdavidch
5484255736Sdavidch    /* don't allow reordering of writes for nbd and packets */
5485255736Sdavidch    mb();
5486255736Sdavidch
5487255736Sdavidch    fp->tx_db.data.prod += nbds;
5488255736Sdavidch
5489255736Sdavidch    /* producer points to the next free tx_bd at this point */
5490255736Sdavidch    fp->tx_pkt_prod++;
5491255736Sdavidch    fp->tx_bd_prod = bd_prod;
5492255736Sdavidch
5493255736Sdavidch    DOORBELL(sc, fp->index, fp->tx_db.raw);
5494255736Sdavidch
5495255736Sdavidch    fp->eth_q_stats.tx_pkts++;
5496255736Sdavidch
5497255736Sdavidch    /* Prevent speculative reads from getting ahead of the status block. */
5498255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5499255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5500255736Sdavidch
5501255736Sdavidch    /* Prevent speculative reads from getting ahead of the doorbell. */
5502255736Sdavidch    bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5503255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5504255736Sdavidch
5505255736Sdavidch    return (0);
5506255736Sdavidch}
5507255736Sdavidch
5508255736Sdavidchstatic void
5509266979Smarcelbxe_tx_start_locked(struct bxe_softc *sc,
5510266979Smarcel                    if_t ifp,
5511255736Sdavidch                    struct bxe_fastpath *fp)
5512255736Sdavidch{
5513255736Sdavidch    struct mbuf *m = NULL;
5514255736Sdavidch    int tx_count = 0;
5515255736Sdavidch    uint16_t tx_bd_avail;
5516255736Sdavidch
5517255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
5518255736Sdavidch
5519255736Sdavidch    /* keep adding entries while there are frames to send */
5520266979Smarcel    while (!if_sendq_empty(ifp)) {
5521255736Sdavidch
5522255736Sdavidch        /*
5523255736Sdavidch         * check for any frames to send
5524255736Sdavidch         * dequeue can still be NULL even if queue is not empty
5525255736Sdavidch         */
5526266979Smarcel        m = if_dequeue(ifp);
5527255736Sdavidch        if (__predict_false(m == NULL)) {
5528255736Sdavidch            break;
5529255736Sdavidch        }
5530255736Sdavidch
5531255736Sdavidch        /* the mbuf now belongs to us */
5532255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5533255736Sdavidch
5534255736Sdavidch        /*
5535255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5536255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5537255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5538255736Sdavidch         */
5539255736Sdavidch        if (__predict_false(bxe_tx_encap(fp, &m))) {
5540255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5541255736Sdavidch            if (m != NULL) {
5542255736Sdavidch                /* mark the TX queue as full and return the frame */
5543266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5544266979Smarcel		if_sendq_prepend(ifp, m);
5545255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5546255736Sdavidch                fp->eth_q_stats.tx_queue_xoff++;
5547255736Sdavidch            }
5548255736Sdavidch
5549255736Sdavidch            /* stop looking for more work */
5550255736Sdavidch            break;
5551255736Sdavidch        }
5552255736Sdavidch
5553255736Sdavidch        /* the frame was enqueued successfully */
5554255736Sdavidch        tx_count++;
5555255736Sdavidch
5556255736Sdavidch        /* send a copy of the frame to any BPF listeners. */
5557266979Smarcel        if_etherbpfmtap(ifp, m);
5558255736Sdavidch
5559255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
5560255736Sdavidch
5561255736Sdavidch        /* handle any completions if we're running low */
5562255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5563258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5564255736Sdavidch            bxe_txeof(sc, fp);
5565266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5566258187Sedavis                break;
5567258187Sedavis            }
5568255736Sdavidch        }
5569255736Sdavidch    }
5570255736Sdavidch
5571255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5572255736Sdavidch    if (tx_count > 0) {
5573255736Sdavidch        /* reset the TX watchdog timeout timer */
5574255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5575255736Sdavidch    }
5576255736Sdavidch}
5577255736Sdavidch
5578255736Sdavidch/* Legacy (non-RSS) dispatch routine */
5579255736Sdavidchstatic void
5580266979Smarcelbxe_tx_start(if_t ifp)
5581255736Sdavidch{
5582255736Sdavidch    struct bxe_softc *sc;
5583255736Sdavidch    struct bxe_fastpath *fp;
5584255736Sdavidch
5585266979Smarcel    sc = if_getsoftc(ifp);
5586255736Sdavidch
5587266979Smarcel    if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5588255736Sdavidch        BLOGW(sc, "Interface not running, ignoring transmit request\n");
5589255736Sdavidch        return;
5590255736Sdavidch    }
5591255736Sdavidch
5592255736Sdavidch    if (!sc->link_vars.link_up) {
5593255736Sdavidch        BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5594255736Sdavidch        return;
5595255736Sdavidch    }
5596255736Sdavidch
5597255736Sdavidch    fp = &sc->fp[0];
5598255736Sdavidch
5599297873Sdavidcs    if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5600297873Sdavidcs        fp->eth_q_stats.tx_queue_full_return++;
5601297873Sdavidcs        return;
5602297873Sdavidcs    }
5603297873Sdavidcs
5604255736Sdavidch    BXE_FP_TX_LOCK(fp);
5605255736Sdavidch    bxe_tx_start_locked(sc, ifp, fp);
5606255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
5607255736Sdavidch}
5608255736Sdavidch
5609255736Sdavidch#if __FreeBSD_version >= 800000
5610255736Sdavidch
5611255736Sdavidchstatic int
5612255736Sdavidchbxe_tx_mq_start_locked(struct bxe_softc    *sc,
5613266979Smarcel                       if_t                ifp,
5614255736Sdavidch                       struct bxe_fastpath *fp,
5615255736Sdavidch                       struct mbuf         *m)
5616255736Sdavidch{
5617255736Sdavidch    struct buf_ring *tx_br = fp->tx_br;
5618255736Sdavidch    struct mbuf *next;
5619255736Sdavidch    int depth, rc, tx_count;
5620255736Sdavidch    uint16_t tx_bd_avail;
5621255736Sdavidch
5622255736Sdavidch    rc = tx_count = 0;
5623255736Sdavidch
5624285973Sdavidcs    BXE_FP_TX_LOCK_ASSERT(fp);
5625285973Sdavidcs
5626255736Sdavidch    if (!tx_br) {
5627255736Sdavidch        BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5628255736Sdavidch        return (EINVAL);
5629255736Sdavidch    }
5630255736Sdavidch
5631285973Sdavidcs    if (!sc->link_vars.link_up ||
5632296579Sdavidcs        (if_getdrvflags(ifp) &
5633285973Sdavidcs        (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING) {
5634297155Sdavidcs        rc = drbr_enqueue(ifp, tx_br, m);
5635285973Sdavidcs        goto bxe_tx_mq_start_locked_exit;
5636285973Sdavidcs    }
5637285973Sdavidcs
5638255736Sdavidch    /* fetch the depth of the driver queue */
5639266979Smarcel    depth = drbr_inuse_drv(ifp, tx_br);
5640255736Sdavidch    if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5641255736Sdavidch        fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5642255736Sdavidch    }
5643255736Sdavidch
5644255736Sdavidch    if (m == NULL) {
5645255736Sdavidch        /* no new work, check for pending frames */
5646266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
5647266979Smarcel    } else if (drbr_needs_enqueue_drv(ifp, tx_br)) {
5648255736Sdavidch        /* have both new and pending work, maintain packet order */
5649297155Sdavidcs        rc = drbr_enqueue(ifp, tx_br, m);
5650255736Sdavidch        if (rc != 0) {
5651255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
5652255736Sdavidch            goto bxe_tx_mq_start_locked_exit;
5653255736Sdavidch        }
5654266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
5655255736Sdavidch    } else {
5656255736Sdavidch        /* new work only and nothing pending */
5657255736Sdavidch        next = m;
5658255736Sdavidch    }
5659255736Sdavidch
5660255736Sdavidch    /* keep adding entries while there are frames to send */
5661255736Sdavidch    while (next != NULL) {
5662255736Sdavidch
5663255736Sdavidch        /* the mbuf now belongs to us */
5664255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5665255736Sdavidch
5666255736Sdavidch        /*
5667255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5668255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5669255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5670255736Sdavidch         */
5671255736Sdavidch        rc = bxe_tx_encap(fp, &next);
5672255736Sdavidch        if (__predict_false(rc != 0)) {
5673255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5674255736Sdavidch            if (next != NULL) {
5675255736Sdavidch                /* mark the TX queue as full and save the frame */
5676266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5677255736Sdavidch                /* XXX this may reorder the frame */
5678297155Sdavidcs                rc = drbr_enqueue(ifp, tx_br, next);
5679255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5680255736Sdavidch                fp->eth_q_stats.tx_frames_deferred++;
5681255736Sdavidch            }
5682255736Sdavidch
5683255736Sdavidch            /* stop looking for more work */
5684255736Sdavidch            break;
5685255736Sdavidch        }
5686255736Sdavidch
5687255736Sdavidch        /* the transmit frame was enqueued successfully */
5688255736Sdavidch        tx_count++;
5689255736Sdavidch
5690255736Sdavidch        /* send a copy of the frame to any BPF listeners */
5691266979Smarcel	if_etherbpfmtap(ifp, next);
5692255736Sdavidch
5693255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
5694255736Sdavidch
5695255736Sdavidch        /* handle any completions if we're running low */
5696255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5697258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5698255736Sdavidch            bxe_txeof(sc, fp);
5699266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5700258187Sedavis                break;
5701258187Sedavis            }
5702255736Sdavidch        }
5703255736Sdavidch
5704266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
5705255736Sdavidch    }
5706255736Sdavidch
5707255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5708255736Sdavidch    if (tx_count > 0) {
5709255736Sdavidch        /* reset the TX watchdog timeout timer */
5710255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5711255736Sdavidch    }
5712255736Sdavidch
5713255736Sdavidchbxe_tx_mq_start_locked_exit:
5714255736Sdavidch
5715255736Sdavidch    return (rc);
5716255736Sdavidch}
5717255736Sdavidch
5718255736Sdavidch/* Multiqueue (TSS) dispatch routine. */
5719255736Sdavidchstatic int
5720255736Sdavidchbxe_tx_mq_start(struct ifnet *ifp,
5721255736Sdavidch                struct mbuf  *m)
5722255736Sdavidch{
5723266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5724255736Sdavidch    struct bxe_fastpath *fp;
5725255736Sdavidch    int fp_index, rc;
5726255736Sdavidch
5727255736Sdavidch    fp_index = 0; /* default is the first queue */
5728255736Sdavidch
5729275358Shselasky    /* check if flowid is set */
5730297155Sdavidcs
5731297155Sdavidcs    if (BXE_VALID_FLOWID(m))
5732255736Sdavidch        fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5733255736Sdavidch
5734255736Sdavidch    fp = &sc->fp[fp_index];
5735255736Sdavidch
5736285973Sdavidcs    if (BXE_FP_TX_TRYLOCK(fp)) {
5737285973Sdavidcs        rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5738285973Sdavidcs        BXE_FP_TX_UNLOCK(fp);
5739285973Sdavidcs    } else
5740297155Sdavidcs        rc = drbr_enqueue(ifp, fp->tx_br, m);
5741255736Sdavidch
5742255736Sdavidch    return (rc);
5743255736Sdavidch}
5744255736Sdavidch
5745255736Sdavidchstatic void
5746255736Sdavidchbxe_mq_flush(struct ifnet *ifp)
5747255736Sdavidch{
5748266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5749255736Sdavidch    struct bxe_fastpath *fp;
5750255736Sdavidch    struct mbuf *m;
5751255736Sdavidch    int i;
5752255736Sdavidch
5753255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5754255736Sdavidch        fp = &sc->fp[i];
5755255736Sdavidch
5756255736Sdavidch        if (fp->state != BXE_FP_STATE_OPEN) {
5757255736Sdavidch            BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5758255736Sdavidch                  fp->index, fp->state);
5759255736Sdavidch            continue;
5760255736Sdavidch        }
5761255736Sdavidch
5762255736Sdavidch        if (fp->tx_br != NULL) {
5763255736Sdavidch            BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5764255736Sdavidch            BXE_FP_TX_LOCK(fp);
5765255736Sdavidch            while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5766255736Sdavidch                m_freem(m);
5767255736Sdavidch            }
5768255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
5769255736Sdavidch        }
5770255736Sdavidch    }
5771255736Sdavidch
5772270876Sglebius    if_qflush(ifp);
5773255736Sdavidch}
5774255736Sdavidch
5775255736Sdavidch#endif /* FreeBSD_version >= 800000 */
5776255736Sdavidch
5777255736Sdavidchstatic uint16_t
5778255736Sdavidchbxe_cid_ilt_lines(struct bxe_softc *sc)
5779255736Sdavidch{
5780255736Sdavidch    if (IS_SRIOV(sc)) {
5781255736Sdavidch        return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5782255736Sdavidch    }
5783255736Sdavidch    return (L2_ILT_LINES(sc));
5784255736Sdavidch}
5785255736Sdavidch
5786255736Sdavidchstatic void
5787255736Sdavidchbxe_ilt_set_info(struct bxe_softc *sc)
5788255736Sdavidch{
5789255736Sdavidch    struct ilt_client_info *ilt_client;
5790255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
5791255736Sdavidch    uint16_t line = 0;
5792255736Sdavidch
5793255736Sdavidch    ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5794255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5795255736Sdavidch
5796255736Sdavidch    /* CDU */
5797255736Sdavidch    ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5798255736Sdavidch    ilt_client->client_num = ILT_CLIENT_CDU;
5799255736Sdavidch    ilt_client->page_size = CDU_ILT_PAGE_SZ;
5800255736Sdavidch    ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5801255736Sdavidch    ilt_client->start = line;
5802255736Sdavidch    line += bxe_cid_ilt_lines(sc);
5803255736Sdavidch
5804255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5805255736Sdavidch        line += CNIC_ILT_LINES;
5806255736Sdavidch    }
5807255736Sdavidch
5808255736Sdavidch    ilt_client->end = (line - 1);
5809255736Sdavidch
5810255736Sdavidch    BLOGD(sc, DBG_LOAD,
5811255736Sdavidch          "ilt client[CDU]: start %d, end %d, "
5812255736Sdavidch          "psz 0x%x, flags 0x%x, hw psz %d\n",
5813255736Sdavidch          ilt_client->start, ilt_client->end,
5814255736Sdavidch          ilt_client->page_size,
5815255736Sdavidch          ilt_client->flags,
5816255736Sdavidch          ilog2(ilt_client->page_size >> 12));
5817255736Sdavidch
5818255736Sdavidch    /* QM */
5819255736Sdavidch    if (QM_INIT(sc->qm_cid_count)) {
5820255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_QM];
5821255736Sdavidch        ilt_client->client_num = ILT_CLIENT_QM;
5822255736Sdavidch        ilt_client->page_size = QM_ILT_PAGE_SZ;
5823255736Sdavidch        ilt_client->flags = 0;
5824255736Sdavidch        ilt_client->start = line;
5825255736Sdavidch
5826255736Sdavidch        /* 4 bytes for each cid */
5827255736Sdavidch        line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5828255736Sdavidch                             QM_ILT_PAGE_SZ);
5829255736Sdavidch
5830255736Sdavidch        ilt_client->end = (line - 1);
5831255736Sdavidch
5832255736Sdavidch        BLOGD(sc, DBG_LOAD,
5833255736Sdavidch              "ilt client[QM]: start %d, end %d, "
5834255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5835255736Sdavidch              ilt_client->start, ilt_client->end,
5836255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5837255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5838255736Sdavidch    }
5839255736Sdavidch
5840255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5841255736Sdavidch        /* SRC */
5842255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5843255736Sdavidch        ilt_client->client_num = ILT_CLIENT_SRC;
5844255736Sdavidch        ilt_client->page_size = SRC_ILT_PAGE_SZ;
5845255736Sdavidch        ilt_client->flags = 0;
5846255736Sdavidch        ilt_client->start = line;
5847255736Sdavidch        line += SRC_ILT_LINES;
5848255736Sdavidch        ilt_client->end = (line - 1);
5849255736Sdavidch
5850255736Sdavidch        BLOGD(sc, DBG_LOAD,
5851255736Sdavidch              "ilt client[SRC]: start %d, end %d, "
5852255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5853255736Sdavidch              ilt_client->start, ilt_client->end,
5854255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5855255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5856255736Sdavidch
5857255736Sdavidch        /* TM */
5858255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_TM];
5859255736Sdavidch        ilt_client->client_num = ILT_CLIENT_TM;
5860255736Sdavidch        ilt_client->page_size = TM_ILT_PAGE_SZ;
5861255736Sdavidch        ilt_client->flags = 0;
5862255736Sdavidch        ilt_client->start = line;
5863255736Sdavidch        line += TM_ILT_LINES;
5864255736Sdavidch        ilt_client->end = (line - 1);
5865255736Sdavidch
5866255736Sdavidch        BLOGD(sc, DBG_LOAD,
5867255736Sdavidch              "ilt client[TM]: start %d, end %d, "
5868255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5869255736Sdavidch              ilt_client->start, ilt_client->end,
5870255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5871255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5872255736Sdavidch    }
5873255736Sdavidch
5874255736Sdavidch    KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
5875255736Sdavidch}
5876255736Sdavidch
5877255736Sdavidchstatic void
5878255736Sdavidchbxe_set_fp_rx_buf_size(struct bxe_softc *sc)
5879255736Sdavidch{
5880255736Sdavidch    int i;
5881283269Sdavidcs    uint32_t rx_buf_size;
5882255736Sdavidch
5883283269Sdavidcs    rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
5884255736Sdavidch
5885255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5886283269Sdavidcs        if(rx_buf_size <= MCLBYTES){
5887283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5888255736Sdavidch            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5889283269Sdavidcs        }else if (rx_buf_size <= MJUMPAGESIZE){
5890283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5891283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5892283269Sdavidcs        }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
5893283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5894283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5895283269Sdavidcs        }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
5896283269Sdavidcs            sc->fp[i].rx_buf_size = MJUMPAGESIZE;
5897283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5898283269Sdavidcs        }else {
5899283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5900283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5901255736Sdavidch        }
5902255736Sdavidch    }
5903255736Sdavidch}
5904255736Sdavidch
5905255736Sdavidchstatic int
5906255736Sdavidchbxe_alloc_ilt_mem(struct bxe_softc *sc)
5907255736Sdavidch{
5908255736Sdavidch    int rc = 0;
5909255736Sdavidch
5910255736Sdavidch    if ((sc->ilt =
5911255736Sdavidch         (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
5912255736Sdavidch                                    M_BXE_ILT,
5913255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
5914255736Sdavidch        rc = 1;
5915255736Sdavidch    }
5916255736Sdavidch
5917255736Sdavidch    return (rc);
5918255736Sdavidch}
5919255736Sdavidch
5920255736Sdavidchstatic int
5921255736Sdavidchbxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
5922255736Sdavidch{
5923255736Sdavidch    int rc = 0;
5924255736Sdavidch
5925255736Sdavidch    if ((sc->ilt->lines =
5926255736Sdavidch         (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
5927255736Sdavidch                                    M_BXE_ILT,
5928255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
5929255736Sdavidch        rc = 1;
5930255736Sdavidch    }
5931255736Sdavidch
5932255736Sdavidch    return (rc);
5933255736Sdavidch}
5934255736Sdavidch
5935255736Sdavidchstatic void
5936255736Sdavidchbxe_free_ilt_mem(struct bxe_softc *sc)
5937255736Sdavidch{
5938255736Sdavidch    if (sc->ilt != NULL) {
5939255736Sdavidch        free(sc->ilt, M_BXE_ILT);
5940255736Sdavidch        sc->ilt = NULL;
5941255736Sdavidch    }
5942255736Sdavidch}
5943255736Sdavidch
5944255736Sdavidchstatic void
5945255736Sdavidchbxe_free_ilt_lines_mem(struct bxe_softc *sc)
5946255736Sdavidch{
5947255736Sdavidch    if (sc->ilt->lines != NULL) {
5948255736Sdavidch        free(sc->ilt->lines, M_BXE_ILT);
5949255736Sdavidch        sc->ilt->lines = NULL;
5950255736Sdavidch    }
5951255736Sdavidch}
5952255736Sdavidch
5953255736Sdavidchstatic void
5954255736Sdavidchbxe_free_mem(struct bxe_softc *sc)
5955255736Sdavidch{
5956255736Sdavidch    int i;
5957255736Sdavidch
5958255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
5959255736Sdavidch        bxe_dma_free(sc, &sc->context[i].vcxt_dma);
5960255736Sdavidch        sc->context[i].vcxt = NULL;
5961255736Sdavidch        sc->context[i].size = 0;
5962255736Sdavidch    }
5963255736Sdavidch
5964255736Sdavidch    ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
5965255736Sdavidch
5966255736Sdavidch    bxe_free_ilt_lines_mem(sc);
5967255736Sdavidch
5968255736Sdavidch}
5969255736Sdavidch
5970255736Sdavidchstatic int
5971255736Sdavidchbxe_alloc_mem(struct bxe_softc *sc)
5972255736Sdavidch{
5973255736Sdavidch    int context_size;
5974255736Sdavidch    int allocated;
5975255736Sdavidch    int i;
5976255736Sdavidch
5977255736Sdavidch    /*
5978255736Sdavidch     * Allocate memory for CDU context:
5979255736Sdavidch     * This memory is allocated separately and not in the generic ILT
5980255736Sdavidch     * functions because CDU differs in few aspects:
5981255736Sdavidch     * 1. There can be multiple entities allocating memory for context -
5982255736Sdavidch     * regular L2, CNIC, and SRIOV drivers. Each separately controls
5983255736Sdavidch     * its own ILT lines.
5984255736Sdavidch     * 2. Since CDU page-size is not a single 4KB page (which is the case
5985255736Sdavidch     * for the other ILT clients), to be efficient we want to support
5986255736Sdavidch     * allocation of sub-page-size in the last entry.
5987255736Sdavidch     * 3. Context pointers are used by the driver to pass to FW / update
5988255736Sdavidch     * the context (for the other ILT clients the pointers are used just to
5989255736Sdavidch     * free the memory during unload).
5990255736Sdavidch     */
5991255736Sdavidch    context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
5992255736Sdavidch    for (i = 0, allocated = 0; allocated < context_size; i++) {
5993255736Sdavidch        sc->context[i].size = min(CDU_ILT_PAGE_SZ,
5994255736Sdavidch                                  (context_size - allocated));
5995255736Sdavidch
5996255736Sdavidch        if (bxe_dma_alloc(sc, sc->context[i].size,
5997255736Sdavidch                          &sc->context[i].vcxt_dma,
5998255736Sdavidch                          "cdu context") != 0) {
5999255736Sdavidch            bxe_free_mem(sc);
6000255736Sdavidch            return (-1);
6001255736Sdavidch        }
6002255736Sdavidch
6003255736Sdavidch        sc->context[i].vcxt =
6004255736Sdavidch            (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6005255736Sdavidch
6006255736Sdavidch        allocated += sc->context[i].size;
6007255736Sdavidch    }
6008255736Sdavidch
6009255736Sdavidch    bxe_alloc_ilt_lines_mem(sc);
6010255736Sdavidch
6011255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6012255736Sdavidch          sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6013255736Sdavidch    {
6014255736Sdavidch        for (i = 0; i < 4; i++) {
6015255736Sdavidch            BLOGD(sc, DBG_LOAD,
6016255736Sdavidch                  "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6017255736Sdavidch                  i,
6018255736Sdavidch                  sc->ilt->clients[i].page_size,
6019255736Sdavidch                  sc->ilt->clients[i].start,
6020255736Sdavidch                  sc->ilt->clients[i].end,
6021255736Sdavidch                  sc->ilt->clients[i].client_num,
6022255736Sdavidch                  sc->ilt->clients[i].flags);
6023255736Sdavidch        }
6024255736Sdavidch    }
6025255736Sdavidch    if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6026255736Sdavidch        BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6027255736Sdavidch        bxe_free_mem(sc);
6028255736Sdavidch        return (-1);
6029255736Sdavidch    }
6030255736Sdavidch
6031255736Sdavidch    return (0);
6032255736Sdavidch}
6033255736Sdavidch
6034255736Sdavidchstatic void
6035255736Sdavidchbxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6036255736Sdavidch{
6037255736Sdavidch    struct bxe_softc *sc;
6038255736Sdavidch    int i;
6039255736Sdavidch
6040255736Sdavidch    sc = fp->sc;
6041255736Sdavidch
6042255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6043255736Sdavidch        return;
6044255736Sdavidch    }
6045255736Sdavidch
6046255736Sdavidch    /* free all mbufs and unload all maps */
6047255736Sdavidch    for (i = 0; i < RX_BD_TOTAL; i++) {
6048255736Sdavidch        if (fp->rx_mbuf_chain[i].m_map != NULL) {
6049255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6050255736Sdavidch                            fp->rx_mbuf_chain[i].m_map,
6051255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6052255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6053255736Sdavidch                              fp->rx_mbuf_chain[i].m_map);
6054255736Sdavidch        }
6055255736Sdavidch
6056255736Sdavidch        if (fp->rx_mbuf_chain[i].m != NULL) {
6057255736Sdavidch            m_freem(fp->rx_mbuf_chain[i].m);
6058255736Sdavidch            fp->rx_mbuf_chain[i].m = NULL;
6059255736Sdavidch            fp->eth_q_stats.mbuf_alloc_rx--;
6060255736Sdavidch        }
6061255736Sdavidch    }
6062255736Sdavidch}
6063255736Sdavidch
6064255736Sdavidchstatic void
6065255736Sdavidchbxe_free_tpa_pool(struct bxe_fastpath *fp)
6066255736Sdavidch{
6067255736Sdavidch    struct bxe_softc *sc;
6068255736Sdavidch    int i, max_agg_queues;
6069255736Sdavidch
6070255736Sdavidch    sc = fp->sc;
6071255736Sdavidch
6072255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6073255736Sdavidch        return;
6074255736Sdavidch    }
6075255736Sdavidch
6076255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
6077255736Sdavidch
6078255736Sdavidch    /* release all mbufs and unload all DMA maps in the TPA pool */
6079255736Sdavidch    for (i = 0; i < max_agg_queues; i++) {
6080255736Sdavidch        if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6081255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6082255736Sdavidch                            fp->rx_tpa_info[i].bd.m_map,
6083255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6084255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6085255736Sdavidch                              fp->rx_tpa_info[i].bd.m_map);
6086255736Sdavidch        }
6087255736Sdavidch
6088255736Sdavidch        if (fp->rx_tpa_info[i].bd.m != NULL) {
6089255736Sdavidch            m_freem(fp->rx_tpa_info[i].bd.m);
6090255736Sdavidch            fp->rx_tpa_info[i].bd.m = NULL;
6091255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tpa--;
6092255736Sdavidch        }
6093255736Sdavidch    }
6094255736Sdavidch}
6095255736Sdavidch
6096255736Sdavidchstatic void
6097255736Sdavidchbxe_free_sge_chain(struct bxe_fastpath *fp)
6098255736Sdavidch{
6099255736Sdavidch    struct bxe_softc *sc;
6100255736Sdavidch    int i;
6101255736Sdavidch
6102255736Sdavidch    sc = fp->sc;
6103255736Sdavidch
6104255736Sdavidch    if (fp->rx_sge_mbuf_tag == NULL) {
6105255736Sdavidch        return;
6106255736Sdavidch    }
6107255736Sdavidch
6108255736Sdavidch    /* rree all mbufs and unload all maps */
6109255736Sdavidch    for (i = 0; i < RX_SGE_TOTAL; i++) {
6110255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6111255736Sdavidch            bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6112255736Sdavidch                            fp->rx_sge_mbuf_chain[i].m_map,
6113255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6114255736Sdavidch            bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6115255736Sdavidch                              fp->rx_sge_mbuf_chain[i].m_map);
6116255736Sdavidch        }
6117255736Sdavidch
6118255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6119255736Sdavidch            m_freem(fp->rx_sge_mbuf_chain[i].m);
6120255736Sdavidch            fp->rx_sge_mbuf_chain[i].m = NULL;
6121255736Sdavidch            fp->eth_q_stats.mbuf_alloc_sge--;
6122255736Sdavidch        }
6123255736Sdavidch    }
6124255736Sdavidch}
6125255736Sdavidch
6126255736Sdavidchstatic void
6127255736Sdavidchbxe_free_fp_buffers(struct bxe_softc *sc)
6128255736Sdavidch{
6129255736Sdavidch    struct bxe_fastpath *fp;
6130255736Sdavidch    int i;
6131255736Sdavidch
6132255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6133255736Sdavidch        fp = &sc->fp[i];
6134255736Sdavidch
6135255736Sdavidch#if __FreeBSD_version >= 800000
6136255736Sdavidch        if (fp->tx_br != NULL) {
6137255736Sdavidch            /* just in case bxe_mq_flush() wasn't called */
6138284739Sdavidcs            if (mtx_initialized(&fp->tx_mtx)) {
6139284739Sdavidcs                struct mbuf *m;
6140284739Sdavidcs
6141284739Sdavidcs                BXE_FP_TX_LOCK(fp);
6142284739Sdavidcs                while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6143284739Sdavidcs                    m_freem(m);
6144284739Sdavidcs                BXE_FP_TX_UNLOCK(fp);
6145255736Sdavidch            }
6146255736Sdavidch        }
6147255736Sdavidch#endif
6148255736Sdavidch
6149255736Sdavidch        /* free all RX buffers */
6150255736Sdavidch        bxe_free_rx_bd_chain(fp);
6151255736Sdavidch        bxe_free_tpa_pool(fp);
6152255736Sdavidch        bxe_free_sge_chain(fp);
6153255736Sdavidch
6154255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6155255736Sdavidch            BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6156255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_rx);
6157255736Sdavidch        }
6158255736Sdavidch
6159255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6160255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6161255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_sge);
6162255736Sdavidch        }
6163255736Sdavidch
6164255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6165255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6166255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tpa);
6167255736Sdavidch        }
6168255736Sdavidch
6169255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6170255736Sdavidch            BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6171255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tx);
6172255736Sdavidch        }
6173255736Sdavidch
6174255736Sdavidch        /* XXX verify all mbufs were reclaimed */
6175255736Sdavidch    }
6176255736Sdavidch}
6177255736Sdavidch
6178255736Sdavidchstatic int
6179255736Sdavidchbxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6180255736Sdavidch                     uint16_t            prev_index,
6181255736Sdavidch                     uint16_t            index)
6182255736Sdavidch{
6183255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
6184255736Sdavidch    struct eth_rx_bd *rx_bd;
6185255736Sdavidch    bus_dma_segment_t segs[1];
6186255736Sdavidch    bus_dmamap_t map;
6187255736Sdavidch    struct mbuf *m;
6188255736Sdavidch    int nsegs, rc;
6189255736Sdavidch
6190255736Sdavidch    rc = 0;
6191255736Sdavidch
6192255736Sdavidch    /* allocate the new RX BD mbuf */
6193260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6194255736Sdavidch    if (__predict_false(m == NULL)) {
6195255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6196255736Sdavidch        return (ENOBUFS);
6197255736Sdavidch    }
6198255736Sdavidch
6199255736Sdavidch    fp->eth_q_stats.mbuf_alloc_rx++;
6200255736Sdavidch
6201255736Sdavidch    /* initialize the mbuf buffer length */
6202255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6203255736Sdavidch
6204255736Sdavidch    /* map the mbuf into non-paged pool */
6205255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6206255736Sdavidch                                 fp->rx_mbuf_spare_map,
6207255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6208255736Sdavidch    if (__predict_false(rc != 0)) {
6209255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6210255736Sdavidch        m_freem(m);
6211255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
6212255736Sdavidch        return (rc);
6213255736Sdavidch    }
6214255736Sdavidch
6215255736Sdavidch    /* all mbufs must map to a single segment */
6216255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6217255736Sdavidch
6218255736Sdavidch    /* release any existing RX BD mbuf mappings */
6219255736Sdavidch
6220255736Sdavidch    if (prev_index != index) {
6221255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[prev_index];
6222255736Sdavidch
6223255736Sdavidch        if (rx_buf->m_map != NULL) {
6224255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6225255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6226255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6227255736Sdavidch        }
6228255736Sdavidch
6229255736Sdavidch        /*
6230255736Sdavidch         * We only get here from bxe_rxeof() when the maximum number
6231255736Sdavidch         * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6232255736Sdavidch         * holds the mbuf in the prev_index so it's OK to NULL it out
6233255736Sdavidch         * here without concern of a memory leak.
6234255736Sdavidch         */
6235255736Sdavidch        fp->rx_mbuf_chain[prev_index].m = NULL;
6236255736Sdavidch    }
6237255736Sdavidch
6238255736Sdavidch    rx_buf = &fp->rx_mbuf_chain[index];
6239255736Sdavidch
6240255736Sdavidch    if (rx_buf->m_map != NULL) {
6241255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6242255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6243255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6244255736Sdavidch    }
6245255736Sdavidch
6246255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6247255736Sdavidch    map = (prev_index != index) ?
6248255736Sdavidch              fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6249255736Sdavidch    rx_buf->m_map = fp->rx_mbuf_spare_map;
6250255736Sdavidch    fp->rx_mbuf_spare_map = map;
6251255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6252255736Sdavidch                    BUS_DMASYNC_PREREAD);
6253255736Sdavidch    rx_buf->m = m;
6254255736Sdavidch
6255255736Sdavidch    rx_bd = &fp->rx_chain[index];
6256255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6257255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6258255736Sdavidch
6259255736Sdavidch    return (rc);
6260255736Sdavidch}
6261255736Sdavidch
6262255736Sdavidchstatic int
6263255736Sdavidchbxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6264255736Sdavidch                      int                 queue)
6265255736Sdavidch{
6266255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6267255736Sdavidch    bus_dma_segment_t segs[1];
6268255736Sdavidch    bus_dmamap_t map;
6269255736Sdavidch    struct mbuf *m;
6270255736Sdavidch    int nsegs;
6271255736Sdavidch    int rc = 0;
6272255736Sdavidch
6273255736Sdavidch    /* allocate the new TPA mbuf */
6274260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6275255736Sdavidch    if (__predict_false(m == NULL)) {
6276255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6277255736Sdavidch        return (ENOBUFS);
6278255736Sdavidch    }
6279255736Sdavidch
6280255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa++;
6281255736Sdavidch
6282255736Sdavidch    /* initialize the mbuf buffer length */
6283255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6284255736Sdavidch
6285255736Sdavidch    /* map the mbuf into non-paged pool */
6286255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6287255736Sdavidch                                 fp->rx_tpa_info_mbuf_spare_map,
6288255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6289255736Sdavidch    if (__predict_false(rc != 0)) {
6290255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6291255736Sdavidch        m_free(m);
6292255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tpa--;
6293255736Sdavidch        return (rc);
6294255736Sdavidch    }
6295255736Sdavidch
6296255736Sdavidch    /* all mbufs must map to a single segment */
6297255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6298255736Sdavidch
6299255736Sdavidch    /* release any existing TPA mbuf mapping */
6300255736Sdavidch    if (tpa_info->bd.m_map != NULL) {
6301255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6302255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6303255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6304255736Sdavidch    }
6305255736Sdavidch
6306255736Sdavidch    /* save the mbuf and mapping info for the TPA mbuf */
6307255736Sdavidch    map = tpa_info->bd.m_map;
6308255736Sdavidch    tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6309255736Sdavidch    fp->rx_tpa_info_mbuf_spare_map = map;
6310255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6311255736Sdavidch                    BUS_DMASYNC_PREREAD);
6312255736Sdavidch    tpa_info->bd.m = m;
6313255736Sdavidch    tpa_info->seg = segs[0];
6314255736Sdavidch
6315255736Sdavidch    return (rc);
6316255736Sdavidch}
6317255736Sdavidch
6318255736Sdavidch/*
6319255736Sdavidch * Allocate an mbuf and assign it to the receive scatter gather chain. The
6320255736Sdavidch * caller must take care to save a copy of the existing mbuf in the SG mbuf
6321255736Sdavidch * chain.
6322255736Sdavidch */
6323255736Sdavidchstatic int
6324255736Sdavidchbxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6325255736Sdavidch                      uint16_t            index)
6326255736Sdavidch{
6327255736Sdavidch    struct bxe_sw_rx_bd *sge_buf;
6328255736Sdavidch    struct eth_rx_sge *sge;
6329255736Sdavidch    bus_dma_segment_t segs[1];
6330255736Sdavidch    bus_dmamap_t map;
6331255736Sdavidch    struct mbuf *m;
6332255736Sdavidch    int nsegs;
6333255736Sdavidch    int rc = 0;
6334255736Sdavidch
6335255736Sdavidch    /* allocate a new SGE mbuf */
6336260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6337255736Sdavidch    if (__predict_false(m == NULL)) {
6338255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6339255736Sdavidch        return (ENOMEM);
6340255736Sdavidch    }
6341255736Sdavidch
6342255736Sdavidch    fp->eth_q_stats.mbuf_alloc_sge++;
6343255736Sdavidch
6344255736Sdavidch    /* initialize the mbuf buffer length */
6345255736Sdavidch    m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6346255736Sdavidch
6347255736Sdavidch    /* map the SGE mbuf into non-paged pool */
6348255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6349255736Sdavidch                                 fp->rx_sge_mbuf_spare_map,
6350255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6351255736Sdavidch    if (__predict_false(rc != 0)) {
6352255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6353255736Sdavidch        m_freem(m);
6354255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
6355255736Sdavidch        return (rc);
6356255736Sdavidch    }
6357255736Sdavidch
6358255736Sdavidch    /* all mbufs must map to a single segment */
6359255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6360255736Sdavidch
6361255736Sdavidch    sge_buf = &fp->rx_sge_mbuf_chain[index];
6362255736Sdavidch
6363255736Sdavidch    /* release any existing SGE mbuf mapping */
6364255736Sdavidch    if (sge_buf->m_map != NULL) {
6365255736Sdavidch        bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6366255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6367255736Sdavidch        bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6368255736Sdavidch    }
6369255736Sdavidch
6370255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6371255736Sdavidch    map = sge_buf->m_map;
6372255736Sdavidch    sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6373255736Sdavidch    fp->rx_sge_mbuf_spare_map = map;
6374255736Sdavidch    bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6375255736Sdavidch                    BUS_DMASYNC_PREREAD);
6376255736Sdavidch    sge_buf->m = m;
6377255736Sdavidch
6378255736Sdavidch    sge = &fp->rx_sge_chain[index];
6379255736Sdavidch    sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6380255736Sdavidch    sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6381255736Sdavidch
6382255736Sdavidch    return (rc);
6383255736Sdavidch}
6384255736Sdavidch
6385255736Sdavidchstatic __noinline int
6386255736Sdavidchbxe_alloc_fp_buffers(struct bxe_softc *sc)
6387255736Sdavidch{
6388255736Sdavidch    struct bxe_fastpath *fp;
6389255736Sdavidch    int i, j, rc = 0;
6390255736Sdavidch    int ring_prod, cqe_ring_prod;
6391255736Sdavidch    int max_agg_queues;
6392255736Sdavidch
6393255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6394255736Sdavidch        fp = &sc->fp[i];
6395255736Sdavidch
6396255736Sdavidch        ring_prod = cqe_ring_prod = 0;
6397255736Sdavidch        fp->rx_bd_cons = 0;
6398255736Sdavidch        fp->rx_cq_cons = 0;
6399255736Sdavidch
6400255736Sdavidch        /* allocate buffers for the RX BDs in RX BD chain */
6401255736Sdavidch        for (j = 0; j < sc->max_rx_bufs; j++) {
6402255736Sdavidch            rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6403255736Sdavidch            if (rc != 0) {
6404255736Sdavidch                BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6405255736Sdavidch                      i, rc);
6406255736Sdavidch                goto bxe_alloc_fp_buffers_error;
6407255736Sdavidch            }
6408255736Sdavidch
6409255736Sdavidch            ring_prod     = RX_BD_NEXT(ring_prod);
6410255736Sdavidch            cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6411255736Sdavidch        }
6412255736Sdavidch
6413255736Sdavidch        fp->rx_bd_prod = ring_prod;
6414255736Sdavidch        fp->rx_cq_prod = cqe_ring_prod;
6415255736Sdavidch        fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6416255736Sdavidch
6417283274Sdavidcs        max_agg_queues = MAX_AGG_QS(sc);
6418255736Sdavidch
6419283274Sdavidcs        fp->tpa_enable = TRUE;
6420255736Sdavidch
6421283274Sdavidcs        /* fill the TPA pool */
6422283274Sdavidcs        for (j = 0; j < max_agg_queues; j++) {
6423283274Sdavidcs            rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6424283274Sdavidcs            if (rc != 0) {
6425283274Sdavidcs                BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6426283274Sdavidcs                          i, j);
6427283274Sdavidcs                fp->tpa_enable = FALSE;
6428283274Sdavidcs                goto bxe_alloc_fp_buffers_error;
6429283274Sdavidcs            }
6430283274Sdavidcs
6431283274Sdavidcs            fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6432283274Sdavidcs        }
6433283274Sdavidcs
6434283274Sdavidcs        if (fp->tpa_enable) {
6435283274Sdavidcs            /* fill the RX SGE chain */
6436283274Sdavidcs            ring_prod = 0;
6437283274Sdavidcs            for (j = 0; j < RX_SGE_USABLE; j++) {
6438283274Sdavidcs                rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6439255736Sdavidch                if (rc != 0) {
6440283274Sdavidcs                    BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6441283274Sdavidcs                              i, ring_prod);
6442255736Sdavidch                    fp->tpa_enable = FALSE;
6443283274Sdavidcs                    ring_prod = 0;
6444255736Sdavidch                    goto bxe_alloc_fp_buffers_error;
6445255736Sdavidch                }
6446255736Sdavidch
6447283274Sdavidcs                ring_prod = RX_SGE_NEXT(ring_prod);
6448255736Sdavidch            }
6449255736Sdavidch
6450283274Sdavidcs            fp->rx_sge_prod = ring_prod;
6451255736Sdavidch        }
6452255736Sdavidch    }
6453255736Sdavidch
6454255736Sdavidch    return (0);
6455255736Sdavidch
6456255736Sdavidchbxe_alloc_fp_buffers_error:
6457255736Sdavidch
6458255736Sdavidch    /* unwind what was already allocated */
6459255736Sdavidch    bxe_free_rx_bd_chain(fp);
6460255736Sdavidch    bxe_free_tpa_pool(fp);
6461255736Sdavidch    bxe_free_sge_chain(fp);
6462255736Sdavidch
6463255736Sdavidch    return (ENOBUFS);
6464255736Sdavidch}
6465255736Sdavidch
6466255736Sdavidchstatic void
6467255736Sdavidchbxe_free_fw_stats_mem(struct bxe_softc *sc)
6468255736Sdavidch{
6469255736Sdavidch    bxe_dma_free(sc, &sc->fw_stats_dma);
6470255736Sdavidch
6471255736Sdavidch    sc->fw_stats_num = 0;
6472255736Sdavidch
6473255736Sdavidch    sc->fw_stats_req_size = 0;
6474255736Sdavidch    sc->fw_stats_req = NULL;
6475255736Sdavidch    sc->fw_stats_req_mapping = 0;
6476255736Sdavidch
6477255736Sdavidch    sc->fw_stats_data_size = 0;
6478255736Sdavidch    sc->fw_stats_data = NULL;
6479255736Sdavidch    sc->fw_stats_data_mapping = 0;
6480255736Sdavidch}
6481255736Sdavidch
6482255736Sdavidchstatic int
6483255736Sdavidchbxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6484255736Sdavidch{
6485255736Sdavidch    uint8_t num_queue_stats;
6486255736Sdavidch    int num_groups;
6487255736Sdavidch
6488255736Sdavidch    /* number of queues for statistics is number of eth queues */
6489255736Sdavidch    num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6490255736Sdavidch
6491255736Sdavidch    /*
6492255736Sdavidch     * Total number of FW statistics requests =
6493255736Sdavidch     *   1 for port stats + 1 for PF stats + num of queues
6494255736Sdavidch     */
6495255736Sdavidch    sc->fw_stats_num = (2 + num_queue_stats);
6496255736Sdavidch
6497255736Sdavidch    /*
6498255736Sdavidch     * Request is built from stats_query_header and an array of
6499255736Sdavidch     * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6500255736Sdavidch     * rules. The real number or requests is configured in the
6501255736Sdavidch     * stats_query_header.
6502255736Sdavidch     */
6503255736Sdavidch    num_groups =
6504255736Sdavidch        ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6505255736Sdavidch         ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6506255736Sdavidch
6507255736Sdavidch    BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6508255736Sdavidch          sc->fw_stats_num, num_groups);
6509255736Sdavidch
6510255736Sdavidch    sc->fw_stats_req_size =
6511255736Sdavidch        (sizeof(struct stats_query_header) +
6512255736Sdavidch         (num_groups * sizeof(struct stats_query_cmd_group)));
6513255736Sdavidch
6514255736Sdavidch    /*
6515255736Sdavidch     * Data for statistics requests + stats_counter.
6516255736Sdavidch     * stats_counter holds per-STORM counters that are incremented when
6517255736Sdavidch     * STORM has finished with the current request. Memory for FCoE
6518255736Sdavidch     * offloaded statistics are counted anyway, even if they will not be sent.
6519255736Sdavidch     * VF stats are not accounted for here as the data of VF stats is stored
6520255736Sdavidch     * in memory allocated by the VF, not here.
6521255736Sdavidch     */
6522255736Sdavidch    sc->fw_stats_data_size =
6523255736Sdavidch        (sizeof(struct stats_counter) +
6524255736Sdavidch         sizeof(struct per_port_stats) +
6525255736Sdavidch         sizeof(struct per_pf_stats) +
6526255736Sdavidch         /* sizeof(struct fcoe_statistics_params) + */
6527255736Sdavidch         (sizeof(struct per_queue_stats) * num_queue_stats));
6528255736Sdavidch
6529255736Sdavidch    if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6530255736Sdavidch                      &sc->fw_stats_dma, "fw stats") != 0) {
6531255736Sdavidch        bxe_free_fw_stats_mem(sc);
6532255736Sdavidch        return (-1);
6533255736Sdavidch    }
6534255736Sdavidch
6535255736Sdavidch    /* set up the shortcuts */
6536255736Sdavidch
6537255736Sdavidch    sc->fw_stats_req =
6538255736Sdavidch        (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6539255736Sdavidch    sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6540255736Sdavidch
6541255736Sdavidch    sc->fw_stats_data =
6542255736Sdavidch        (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6543255736Sdavidch                                     sc->fw_stats_req_size);
6544255736Sdavidch    sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6545255736Sdavidch                                 sc->fw_stats_req_size);
6546255736Sdavidch
6547256341Sdim    BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6548256341Sdim          (uintmax_t)sc->fw_stats_req_mapping);
6549255736Sdavidch
6550256341Sdim    BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6551256341Sdim          (uintmax_t)sc->fw_stats_data_mapping);
6552255736Sdavidch
6553255736Sdavidch    return (0);
6554255736Sdavidch}
6555255736Sdavidch
6556255736Sdavidch/*
6557255736Sdavidch * Bits map:
6558255736Sdavidch * 0-7  - Engine0 load counter.
6559255736Sdavidch * 8-15 - Engine1 load counter.
6560255736Sdavidch * 16   - Engine0 RESET_IN_PROGRESS bit.
6561255736Sdavidch * 17   - Engine1 RESET_IN_PROGRESS bit.
6562255736Sdavidch * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6563255736Sdavidch *        function on the engine
6564255736Sdavidch * 19   - Engine1 ONE_IS_LOADED.
6565255736Sdavidch * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6566255736Sdavidch *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6567255736Sdavidch *        for just the one belonging to its engine).
6568255736Sdavidch */
6569255736Sdavidch#define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6570255736Sdavidch#define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6571255736Sdavidch#define BXE_PATH0_LOAD_CNT_SHIFT  0
6572255736Sdavidch#define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6573255736Sdavidch#define BXE_PATH1_LOAD_CNT_SHIFT  8
6574255736Sdavidch#define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6575255736Sdavidch#define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6576255736Sdavidch#define BXE_GLOBAL_RESET_BIT      0x00040000
6577255736Sdavidch
6578255736Sdavidch/* set the GLOBAL_RESET bit, should be run under rtnl lock */
6579255736Sdavidchstatic void
6580255736Sdavidchbxe_set_reset_global(struct bxe_softc *sc)
6581255736Sdavidch{
6582255736Sdavidch    uint32_t val;
6583255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6584255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6585255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6586255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6587255736Sdavidch}
6588255736Sdavidch
6589255736Sdavidch/* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6590255736Sdavidchstatic void
6591255736Sdavidchbxe_clear_reset_global(struct bxe_softc *sc)
6592255736Sdavidch{
6593255736Sdavidch    uint32_t val;
6594255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6595255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6596255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6597255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6598255736Sdavidch}
6599255736Sdavidch
6600255736Sdavidch/* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6601255736Sdavidchstatic uint8_t
6602255736Sdavidchbxe_reset_is_global(struct bxe_softc *sc)
6603255736Sdavidch{
6604255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6605255736Sdavidch    BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6606255736Sdavidch    return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6607255736Sdavidch}
6608255736Sdavidch
6609255736Sdavidch/* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6610255736Sdavidchstatic void
6611255736Sdavidchbxe_set_reset_done(struct bxe_softc *sc)
6612255736Sdavidch{
6613255736Sdavidch    uint32_t val;
6614255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6615255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6616255736Sdavidch
6617255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6618255736Sdavidch
6619255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6620255736Sdavidch    /* Clear the bit */
6621255736Sdavidch    val &= ~bit;
6622255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6623255736Sdavidch
6624255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6625255736Sdavidch}
6626255736Sdavidch
6627255736Sdavidch/* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6628255736Sdavidchstatic void
6629255736Sdavidchbxe_set_reset_in_progress(struct bxe_softc *sc)
6630255736Sdavidch{
6631255736Sdavidch    uint32_t val;
6632255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6633255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6634255736Sdavidch
6635255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6636255736Sdavidch
6637255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6638255736Sdavidch    /* Set the bit */
6639255736Sdavidch    val |= bit;
6640255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6641255736Sdavidch
6642255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6643255736Sdavidch}
6644255736Sdavidch
6645255736Sdavidch/* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6646255736Sdavidchstatic uint8_t
6647255736Sdavidchbxe_reset_is_done(struct bxe_softc *sc,
6648255736Sdavidch                  int              engine)
6649255736Sdavidch{
6650255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6651255736Sdavidch    uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6652255736Sdavidch                            BXE_PATH0_RST_IN_PROG_BIT;
6653255736Sdavidch
6654255736Sdavidch    /* return false if bit is set */
6655255736Sdavidch    return (val & bit) ? FALSE : TRUE;
6656255736Sdavidch}
6657255736Sdavidch
6658255736Sdavidch/* get the load status for an engine, should be run under rtnl lock */
6659255736Sdavidchstatic uint8_t
6660255736Sdavidchbxe_get_load_status(struct bxe_softc *sc,
6661255736Sdavidch                    int              engine)
6662255736Sdavidch{
6663255736Sdavidch    uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6664255736Sdavidch                             BXE_PATH0_LOAD_CNT_MASK;
6665255736Sdavidch    uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6666255736Sdavidch                              BXE_PATH0_LOAD_CNT_SHIFT;
6667255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6668255736Sdavidch
6669255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6670255736Sdavidch
6671255736Sdavidch    val = ((val & mask) >> shift);
6672255736Sdavidch
6673255736Sdavidch    BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6674255736Sdavidch
6675255736Sdavidch    return (val != 0);
6676255736Sdavidch}
6677255736Sdavidch
6678255736Sdavidch/* set pf load mark */
6679255736Sdavidch/* XXX needs to be under rtnl lock */
6680255736Sdavidchstatic void
6681255736Sdavidchbxe_set_pf_load(struct bxe_softc *sc)
6682255736Sdavidch{
6683255736Sdavidch    uint32_t val;
6684255736Sdavidch    uint32_t val1;
6685255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6686255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6687255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6688255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6689255736Sdavidch
6690255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6691255736Sdavidch
6692255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6693255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6694255736Sdavidch
6695255736Sdavidch    /* get the current counter value */
6696255736Sdavidch    val1 = ((val & mask) >> shift);
6697255736Sdavidch
6698255736Sdavidch    /* set bit of this PF */
6699255736Sdavidch    val1 |= (1 << SC_ABS_FUNC(sc));
6700255736Sdavidch
6701255736Sdavidch    /* clear the old value */
6702255736Sdavidch    val &= ~mask;
6703255736Sdavidch
6704255736Sdavidch    /* set the new one */
6705255736Sdavidch    val |= ((val1 << shift) & mask);
6706255736Sdavidch
6707255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6708255736Sdavidch
6709255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6710255736Sdavidch}
6711255736Sdavidch
6712255736Sdavidch/* clear pf load mark */
6713255736Sdavidch/* XXX needs to be under rtnl lock */
6714255736Sdavidchstatic uint8_t
6715255736Sdavidchbxe_clear_pf_load(struct bxe_softc *sc)
6716255736Sdavidch{
6717255736Sdavidch    uint32_t val1, val;
6718255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6719255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6720255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6721255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6722255736Sdavidch
6723255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6724255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6725255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6726255736Sdavidch
6727255736Sdavidch    /* get the current counter value */
6728255736Sdavidch    val1 = (val & mask) >> shift;
6729255736Sdavidch
6730255736Sdavidch    /* clear bit of that PF */
6731255736Sdavidch    val1 &= ~(1 << SC_ABS_FUNC(sc));
6732255736Sdavidch
6733255736Sdavidch    /* clear the old value */
6734255736Sdavidch    val &= ~mask;
6735255736Sdavidch
6736255736Sdavidch    /* set the new one */
6737255736Sdavidch    val |= ((val1 << shift) & mask);
6738255736Sdavidch
6739255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6740255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6741255736Sdavidch    return (val1 != 0);
6742255736Sdavidch}
6743255736Sdavidch
6744255736Sdavidch/* send load requrest to mcp and analyze response */
6745255736Sdavidchstatic int
6746255736Sdavidchbxe_nic_load_request(struct bxe_softc *sc,
6747255736Sdavidch                     uint32_t         *load_code)
6748255736Sdavidch{
6749255736Sdavidch    /* init fw_seq */
6750255736Sdavidch    sc->fw_seq =
6751255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6752255736Sdavidch         DRV_MSG_SEQ_NUMBER_MASK);
6753255736Sdavidch
6754255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6755255736Sdavidch
6756255736Sdavidch    /* get the current FW pulse sequence */
6757255736Sdavidch    sc->fw_drv_pulse_wr_seq =
6758255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6759255736Sdavidch         DRV_PULSE_SEQ_MASK);
6760255736Sdavidch
6761255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6762255736Sdavidch          sc->fw_drv_pulse_wr_seq);
6763255736Sdavidch
6764255736Sdavidch    /* load request */
6765255736Sdavidch    (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6766255736Sdavidch                                  DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6767255736Sdavidch
6768255736Sdavidch    /* if the MCP fails to respond we must abort */
6769255736Sdavidch    if (!(*load_code)) {
6770255736Sdavidch        BLOGE(sc, "MCP response failure!\n");
6771255736Sdavidch        return (-1);
6772255736Sdavidch    }
6773255736Sdavidch
6774255736Sdavidch    /* if MCP refused then must abort */
6775255736Sdavidch    if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6776255736Sdavidch        BLOGE(sc, "MCP refused load request\n");
6777255736Sdavidch        return (-1);
6778255736Sdavidch    }
6779255736Sdavidch
6780255736Sdavidch    return (0);
6781255736Sdavidch}
6782255736Sdavidch
6783255736Sdavidch/*
6784255736Sdavidch * Check whether another PF has already loaded FW to chip. In virtualized
6785255736Sdavidch * environments a pf from anoth VM may have already initialized the device
6786255736Sdavidch * including loading FW.
6787255736Sdavidch */
6788255736Sdavidchstatic int
6789255736Sdavidchbxe_nic_load_analyze_req(struct bxe_softc *sc,
6790255736Sdavidch                         uint32_t         load_code)
6791255736Sdavidch{
6792255736Sdavidch    uint32_t my_fw, loaded_fw;
6793255736Sdavidch
6794255736Sdavidch    /* is another pf loaded on this engine? */
6795255736Sdavidch    if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6796255736Sdavidch        (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6797255736Sdavidch        /* build my FW version dword */
6798255736Sdavidch        my_fw = (BCM_5710_FW_MAJOR_VERSION +
6799255736Sdavidch                 (BCM_5710_FW_MINOR_VERSION << 8 ) +
6800255736Sdavidch                 (BCM_5710_FW_REVISION_VERSION << 16) +
6801255736Sdavidch                 (BCM_5710_FW_ENGINEERING_VERSION << 24));
6802255736Sdavidch
6803255736Sdavidch        /* read loaded FW from chip */
6804255736Sdavidch        loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6805255736Sdavidch        BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6806255736Sdavidch              loaded_fw, my_fw);
6807255736Sdavidch
6808255736Sdavidch        /* abort nic load if version mismatch */
6809255736Sdavidch        if (my_fw != loaded_fw) {
6810255736Sdavidch            BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6811255736Sdavidch                  loaded_fw, my_fw);
6812255736Sdavidch            return (-1);
6813255736Sdavidch        }
6814255736Sdavidch    }
6815255736Sdavidch
6816255736Sdavidch    return (0);
6817255736Sdavidch}
6818255736Sdavidch
6819255736Sdavidch/* mark PMF if applicable */
6820255736Sdavidchstatic void
6821255736Sdavidchbxe_nic_load_pmf(struct bxe_softc *sc,
6822255736Sdavidch                 uint32_t         load_code)
6823255736Sdavidch{
6824255736Sdavidch    uint32_t ncsi_oem_data_addr;
6825255736Sdavidch
6826255736Sdavidch    if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6827255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6828255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6829255736Sdavidch        /*
6830255736Sdavidch         * Barrier here for ordering between the writing to sc->port.pmf here
6831255736Sdavidch         * and reading it from the periodic task.
6832255736Sdavidch         */
6833255736Sdavidch        sc->port.pmf = 1;
6834255736Sdavidch        mb();
6835255736Sdavidch    } else {
6836255736Sdavidch        sc->port.pmf = 0;
6837255736Sdavidch    }
6838255736Sdavidch
6839255736Sdavidch    BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6840255736Sdavidch
6841255736Sdavidch    /* XXX needed? */
6842255736Sdavidch    if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6843255736Sdavidch        if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6844255736Sdavidch            ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6845255736Sdavidch            if (ncsi_oem_data_addr) {
6846255736Sdavidch                REG_WR(sc,
6847255736Sdavidch                       (ncsi_oem_data_addr +
6848255736Sdavidch                        offsetof(struct glob_ncsi_oem_data, driver_version)),
6849255736Sdavidch                       0);
6850255736Sdavidch            }
6851255736Sdavidch        }
6852255736Sdavidch    }
6853255736Sdavidch}
6854255736Sdavidch
6855255736Sdavidchstatic void
6856255736Sdavidchbxe_read_mf_cfg(struct bxe_softc *sc)
6857255736Sdavidch{
6858255736Sdavidch    int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
6859255736Sdavidch    int abs_func;
6860255736Sdavidch    int vn;
6861255736Sdavidch
6862255736Sdavidch    if (BXE_NOMCP(sc)) {
6863255736Sdavidch        return; /* what should be the default bvalue in this case */
6864255736Sdavidch    }
6865255736Sdavidch
6866255736Sdavidch    /*
6867255736Sdavidch     * The formula for computing the absolute function number is...
6868255736Sdavidch     * For 2 port configuration (4 functions per port):
6869255736Sdavidch     *   abs_func = 2 * vn + SC_PORT + SC_PATH
6870255736Sdavidch     * For 4 port configuration (2 functions per port):
6871255736Sdavidch     *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
6872255736Sdavidch     */
6873255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
6874255736Sdavidch        abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
6875255736Sdavidch        if (abs_func >= E1H_FUNC_MAX) {
6876255736Sdavidch            break;
6877255736Sdavidch        }
6878255736Sdavidch        sc->devinfo.mf_info.mf_config[vn] =
6879255736Sdavidch            MFCFG_RD(sc, func_mf_config[abs_func].config);
6880255736Sdavidch    }
6881255736Sdavidch
6882255736Sdavidch    if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
6883255736Sdavidch        FUNC_MF_CFG_FUNC_DISABLED) {
6884255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
6885255736Sdavidch        sc->flags |= BXE_MF_FUNC_DIS;
6886255736Sdavidch    } else {
6887255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
6888255736Sdavidch        sc->flags &= ~BXE_MF_FUNC_DIS;
6889255736Sdavidch    }
6890255736Sdavidch}
6891255736Sdavidch
6892255736Sdavidch/* acquire split MCP access lock register */
6893255736Sdavidchstatic int bxe_acquire_alr(struct bxe_softc *sc)
6894255736Sdavidch{
6895255736Sdavidch    uint32_t j, val;
6896255736Sdavidch
6897255736Sdavidch    for (j = 0; j < 1000; j++) {
6898255736Sdavidch        val = (1UL << 31);
6899255736Sdavidch        REG_WR(sc, GRCBASE_MCP + 0x9c, val);
6900255736Sdavidch        val = REG_RD(sc, GRCBASE_MCP + 0x9c);
6901255736Sdavidch        if (val & (1L << 31))
6902255736Sdavidch            break;
6903255736Sdavidch
6904255736Sdavidch        DELAY(5000);
6905255736Sdavidch    }
6906255736Sdavidch
6907255736Sdavidch    if (!(val & (1L << 31))) {
6908255736Sdavidch        BLOGE(sc, "Cannot acquire MCP access lock register\n");
6909255736Sdavidch        return (-1);
6910255736Sdavidch    }
6911255736Sdavidch
6912255736Sdavidch    return (0);
6913255736Sdavidch}
6914255736Sdavidch
6915255736Sdavidch/* release split MCP access lock register */
6916255736Sdavidchstatic void bxe_release_alr(struct bxe_softc *sc)
6917255736Sdavidch{
6918255736Sdavidch    REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
6919255736Sdavidch}
6920255736Sdavidch
6921255736Sdavidchstatic void
6922255736Sdavidchbxe_fan_failure(struct bxe_softc *sc)
6923255736Sdavidch{
6924255736Sdavidch    int port = SC_PORT(sc);
6925255736Sdavidch    uint32_t ext_phy_config;
6926255736Sdavidch
6927255736Sdavidch    /* mark the failure */
6928255736Sdavidch    ext_phy_config =
6929255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
6930255736Sdavidch
6931255736Sdavidch    ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
6932255736Sdavidch    ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
6933255736Sdavidch    SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
6934255736Sdavidch             ext_phy_config);
6935255736Sdavidch
6936255736Sdavidch    /* log the failure */
6937255736Sdavidch    BLOGW(sc, "Fan Failure has caused the driver to shutdown "
6938255736Sdavidch              "the card to prevent permanent damage. "
6939255736Sdavidch              "Please contact OEM Support for assistance\n");
6940255736Sdavidch
6941255736Sdavidch    /* XXX */
6942255736Sdavidch#if 1
6943255736Sdavidch    bxe_panic(sc, ("Schedule task to handle fan failure\n"));
6944255736Sdavidch#else
6945255736Sdavidch    /*
6946255736Sdavidch     * Schedule device reset (unload)
6947255736Sdavidch     * This is due to some boards consuming sufficient power when driver is
6948255736Sdavidch     * up to overheat if fan fails.
6949255736Sdavidch     */
6950255736Sdavidch    bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
6951255736Sdavidch    schedule_delayed_work(&sc->sp_rtnl_task, 0);
6952255736Sdavidch#endif
6953255736Sdavidch}
6954255736Sdavidch
6955255736Sdavidch/* this function is called upon a link interrupt */
6956255736Sdavidchstatic void
6957255736Sdavidchbxe_link_attn(struct bxe_softc *sc)
6958255736Sdavidch{
6959255736Sdavidch    uint32_t pause_enabled = 0;
6960255736Sdavidch    struct host_port_stats *pstats;
6961255736Sdavidch    int cmng_fns;
6962255736Sdavidch
6963255736Sdavidch    /* Make sure that we are synced with the current statistics */
6964255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_STOP);
6965255736Sdavidch
6966255736Sdavidch    elink_link_update(&sc->link_params, &sc->link_vars);
6967255736Sdavidch
6968255736Sdavidch    if (sc->link_vars.link_up) {
6969255736Sdavidch
6970255736Sdavidch        /* dropless flow control */
6971255736Sdavidch        if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
6972255736Sdavidch            pause_enabled = 0;
6973255736Sdavidch
6974255736Sdavidch            if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
6975255736Sdavidch                pause_enabled = 1;
6976255736Sdavidch            }
6977255736Sdavidch
6978255736Sdavidch            REG_WR(sc,
6979255736Sdavidch                   (BAR_USTRORM_INTMEM +
6980255736Sdavidch                    USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
6981255736Sdavidch                   pause_enabled);
6982255736Sdavidch        }
6983255736Sdavidch
6984255736Sdavidch        if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
6985255736Sdavidch            pstats = BXE_SP(sc, port_stats);
6986255736Sdavidch            /* reset old mac stats */
6987255736Sdavidch            memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
6988255736Sdavidch        }
6989255736Sdavidch
6990255736Sdavidch        if (sc->state == BXE_STATE_OPEN) {
6991255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
6992255736Sdavidch        }
6993255736Sdavidch    }
6994255736Sdavidch
6995255736Sdavidch    if (sc->link_vars.link_up && sc->link_vars.line_speed) {
6996255736Sdavidch        cmng_fns = bxe_get_cmng_fns_mode(sc);
6997255736Sdavidch
6998255736Sdavidch        if (cmng_fns != CMNG_FNS_NONE) {
6999255736Sdavidch            bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7000255736Sdavidch            storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7001255736Sdavidch        } else {
7002255736Sdavidch            /* rate shaping and fairness are disabled */
7003255736Sdavidch            BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7004255736Sdavidch        }
7005255736Sdavidch    }
7006255736Sdavidch
7007255736Sdavidch    bxe_link_report_locked(sc);
7008255736Sdavidch
7009255736Sdavidch    if (IS_MF(sc)) {
7010255736Sdavidch        ; // XXX bxe_link_sync_notify(sc);
7011255736Sdavidch    }
7012255736Sdavidch}
7013255736Sdavidch
7014255736Sdavidchstatic void
7015255736Sdavidchbxe_attn_int_asserted(struct bxe_softc *sc,
7016255736Sdavidch                      uint32_t         asserted)
7017255736Sdavidch{
7018255736Sdavidch    int port = SC_PORT(sc);
7019255736Sdavidch    uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7020255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
7021255736Sdavidch    uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7022255736Sdavidch                                        NIG_REG_MASK_INTERRUPT_PORT0;
7023255736Sdavidch    uint32_t aeu_mask;
7024255736Sdavidch    uint32_t nig_mask = 0;
7025255736Sdavidch    uint32_t reg_addr;
7026255736Sdavidch    uint32_t igu_acked;
7027255736Sdavidch    uint32_t cnt;
7028255736Sdavidch
7029255736Sdavidch    if (sc->attn_state & asserted) {
7030255736Sdavidch        BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7031255736Sdavidch    }
7032255736Sdavidch
7033255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7034255736Sdavidch
7035255736Sdavidch    aeu_mask = REG_RD(sc, aeu_addr);
7036255736Sdavidch
7037255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7038255736Sdavidch          aeu_mask, asserted);
7039255736Sdavidch
7040255736Sdavidch    aeu_mask &= ~(asserted & 0x3ff);
7041255736Sdavidch
7042255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7043255736Sdavidch
7044255736Sdavidch    REG_WR(sc, aeu_addr, aeu_mask);
7045255736Sdavidch
7046255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7047255736Sdavidch
7048255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7049255736Sdavidch    sc->attn_state |= asserted;
7050255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7051255736Sdavidch
7052255736Sdavidch    if (asserted & ATTN_HARD_WIRED_MASK) {
7053255736Sdavidch        if (asserted & ATTN_NIG_FOR_FUNC) {
7054255736Sdavidch
7055284335Sdavidcs	    bxe_acquire_phy_lock(sc);
7056255736Sdavidch            /* save nig interrupt mask */
7057255736Sdavidch            nig_mask = REG_RD(sc, nig_int_mask_addr);
7058255736Sdavidch
7059255736Sdavidch            /* If nig_mask is not set, no need to call the update function */
7060255736Sdavidch            if (nig_mask) {
7061255736Sdavidch                REG_WR(sc, nig_int_mask_addr, 0);
7062255736Sdavidch
7063255736Sdavidch                bxe_link_attn(sc);
7064255736Sdavidch            }
7065255736Sdavidch
7066255736Sdavidch            /* handle unicore attn? */
7067255736Sdavidch        }
7068255736Sdavidch
7069255736Sdavidch        if (asserted & ATTN_SW_TIMER_4_FUNC) {
7070255736Sdavidch            BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7071255736Sdavidch        }
7072255736Sdavidch
7073255736Sdavidch        if (asserted & GPIO_2_FUNC) {
7074255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7075255736Sdavidch        }
7076255736Sdavidch
7077255736Sdavidch        if (asserted & GPIO_3_FUNC) {
7078255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7079255736Sdavidch        }
7080255736Sdavidch
7081255736Sdavidch        if (asserted & GPIO_4_FUNC) {
7082255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7083255736Sdavidch        }
7084255736Sdavidch
7085255736Sdavidch        if (port == 0) {
7086255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_1) {
7087255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7088255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7089255736Sdavidch            }
7090255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_2) {
7091255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7092255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7093255736Sdavidch            }
7094255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_3) {
7095255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7096255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7097255736Sdavidch            }
7098255736Sdavidch        } else {
7099255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_4) {
7100255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7101255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7102255736Sdavidch            }
7103255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_5) {
7104255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7105255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7106255736Sdavidch            }
7107255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_6) {
7108255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7109255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7110255736Sdavidch            }
7111255736Sdavidch        }
7112255736Sdavidch    } /* hardwired */
7113255736Sdavidch
7114255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7115255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7116255736Sdavidch    } else {
7117255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7118255736Sdavidch    }
7119255736Sdavidch
7120255736Sdavidch    BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7121255736Sdavidch          asserted,
7122255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7123255736Sdavidch    REG_WR(sc, reg_addr, asserted);
7124255736Sdavidch
7125255736Sdavidch    /* now set back the mask */
7126255736Sdavidch    if (asserted & ATTN_NIG_FOR_FUNC) {
7127255736Sdavidch        /*
7128255736Sdavidch         * Verify that IGU ack through BAR was written before restoring
7129255736Sdavidch         * NIG mask. This loop should exit after 2-3 iterations max.
7130255736Sdavidch         */
7131255736Sdavidch        if (sc->devinfo.int_block != INT_BLOCK_HC) {
7132255736Sdavidch            cnt = 0;
7133255736Sdavidch
7134255736Sdavidch            do {
7135255736Sdavidch                igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7136255736Sdavidch            } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7137255736Sdavidch                     (++cnt < MAX_IGU_ATTN_ACK_TO));
7138255736Sdavidch
7139255736Sdavidch            if (!igu_acked) {
7140255736Sdavidch                BLOGE(sc, "Failed to verify IGU ack on time\n");
7141255736Sdavidch            }
7142255736Sdavidch
7143255736Sdavidch            mb();
7144255736Sdavidch        }
7145255736Sdavidch
7146255736Sdavidch        REG_WR(sc, nig_int_mask_addr, nig_mask);
7147255736Sdavidch
7148284335Sdavidcs	bxe_release_phy_lock(sc);
7149255736Sdavidch    }
7150255736Sdavidch}
7151255736Sdavidch
7152255736Sdavidchstatic void
7153255736Sdavidchbxe_print_next_block(struct bxe_softc *sc,
7154255736Sdavidch                     int              idx,
7155255736Sdavidch                     const char       *blk)
7156255736Sdavidch{
7157255736Sdavidch    BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7158255736Sdavidch}
7159255736Sdavidch
7160255736Sdavidchstatic int
7161255736Sdavidchbxe_check_blocks_with_parity0(struct bxe_softc *sc,
7162255736Sdavidch                              uint32_t         sig,
7163255736Sdavidch                              int              par_num,
7164255736Sdavidch                              uint8_t          print)
7165255736Sdavidch{
7166255736Sdavidch    uint32_t cur_bit = 0;
7167255736Sdavidch    int i = 0;
7168255736Sdavidch
7169255736Sdavidch    for (i = 0; sig; i++) {
7170255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7171255736Sdavidch        if (sig & cur_bit) {
7172255736Sdavidch            switch (cur_bit) {
7173255736Sdavidch            case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7174255736Sdavidch                if (print)
7175255736Sdavidch                    bxe_print_next_block(sc, par_num++, "BRB");
7176255736Sdavidch                break;
7177255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7178255736Sdavidch                if (print)
7179255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PARSER");
7180255736Sdavidch                break;
7181255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7182255736Sdavidch                if (print)
7183255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSDM");
7184255736Sdavidch                break;
7185255736Sdavidch            case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7186255736Sdavidch                if (print)
7187255736Sdavidch                    bxe_print_next_block(sc, par_num++, "SEARCHER");
7188255736Sdavidch                break;
7189255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7190255736Sdavidch                if (print)
7191255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TCM");
7192255736Sdavidch                break;
7193255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7194255736Sdavidch                if (print)
7195255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSEMI");
7196255736Sdavidch                break;
7197255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7198255736Sdavidch                if (print)
7199255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XPB");
7200255736Sdavidch                break;
7201255736Sdavidch            }
7202255736Sdavidch
7203255736Sdavidch            /* Clear the bit */
7204255736Sdavidch            sig &= ~cur_bit;
7205255736Sdavidch        }
7206255736Sdavidch    }
7207255736Sdavidch
7208255736Sdavidch    return (par_num);
7209255736Sdavidch}
7210255736Sdavidch
7211255736Sdavidchstatic int
7212255736Sdavidchbxe_check_blocks_with_parity1(struct bxe_softc *sc,
7213255736Sdavidch                              uint32_t         sig,
7214255736Sdavidch                              int              par_num,
7215255736Sdavidch                              uint8_t          *global,
7216255736Sdavidch                              uint8_t          print)
7217255736Sdavidch{
7218255736Sdavidch    int i = 0;
7219255736Sdavidch    uint32_t cur_bit = 0;
7220255736Sdavidch    for (i = 0; sig; i++) {
7221255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7222255736Sdavidch        if (sig & cur_bit) {
7223255736Sdavidch            switch (cur_bit) {
7224255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7225255736Sdavidch                if (print)
7226255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PBF");
7227255736Sdavidch                break;
7228255736Sdavidch            case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7229255736Sdavidch                if (print)
7230255736Sdavidch                    bxe_print_next_block(sc, par_num++, "QM");
7231255736Sdavidch                break;
7232255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7233255736Sdavidch                if (print)
7234255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TM");
7235255736Sdavidch                break;
7236255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7237255736Sdavidch                if (print)
7238255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSDM");
7239255736Sdavidch                break;
7240255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7241255736Sdavidch                if (print)
7242255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XCM");
7243255736Sdavidch                break;
7244255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7245255736Sdavidch                if (print)
7246255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSEMI");
7247255736Sdavidch                break;
7248255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7249255736Sdavidch                if (print)
7250255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7251255736Sdavidch                break;
7252255736Sdavidch            case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7253255736Sdavidch                if (print)
7254255736Sdavidch                    bxe_print_next_block(sc, par_num++, "NIG");
7255255736Sdavidch                break;
7256255736Sdavidch            case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7257255736Sdavidch                if (print)
7258255736Sdavidch                    bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7259255736Sdavidch                *global = TRUE;
7260255736Sdavidch                break;
7261255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7262255736Sdavidch                if (print)
7263255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DEBUG");
7264255736Sdavidch                break;
7265255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7266255736Sdavidch                if (print)
7267255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USDM");
7268255736Sdavidch                break;
7269255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7270255736Sdavidch                if (print)
7271255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UCM");
7272255736Sdavidch                break;
7273255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7274255736Sdavidch                if (print)
7275255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USEMI");
7276255736Sdavidch                break;
7277255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7278255736Sdavidch                if (print)
7279255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UPB");
7280255736Sdavidch                break;
7281255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7282255736Sdavidch                if (print)
7283255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSDM");
7284255736Sdavidch                break;
7285255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7286255736Sdavidch                if (print)
7287255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CCM");
7288255736Sdavidch                break;
7289255736Sdavidch            }
7290255736Sdavidch
7291255736Sdavidch            /* Clear the bit */
7292255736Sdavidch            sig &= ~cur_bit;
7293255736Sdavidch        }
7294255736Sdavidch    }
7295255736Sdavidch
7296255736Sdavidch    return (par_num);
7297255736Sdavidch}
7298255736Sdavidch
7299255736Sdavidchstatic int
7300255736Sdavidchbxe_check_blocks_with_parity2(struct bxe_softc *sc,
7301255736Sdavidch                              uint32_t         sig,
7302255736Sdavidch                              int              par_num,
7303255736Sdavidch                              uint8_t          print)
7304255736Sdavidch{
7305255736Sdavidch    uint32_t cur_bit = 0;
7306255736Sdavidch    int i = 0;
7307255736Sdavidch
7308255736Sdavidch    for (i = 0; sig; i++) {
7309255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7310255736Sdavidch        if (sig & cur_bit) {
7311255736Sdavidch            switch (cur_bit) {
7312255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7313255736Sdavidch                if (print)
7314255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSEMI");
7315255736Sdavidch                break;
7316255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7317255736Sdavidch                if (print)
7318255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXP");
7319255736Sdavidch                break;
7320255736Sdavidch            case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7321255736Sdavidch                if (print)
7322255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7323255736Sdavidch                break;
7324255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7325255736Sdavidch                if (print)
7326255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CFC");
7327255736Sdavidch                break;
7328255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7329255736Sdavidch                if (print)
7330255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CDU");
7331255736Sdavidch                break;
7332255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7333255736Sdavidch                if (print)
7334255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DMAE");
7335255736Sdavidch                break;
7336255736Sdavidch            case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7337255736Sdavidch                if (print)
7338255736Sdavidch                    bxe_print_next_block(sc, par_num++, "IGU");
7339255736Sdavidch                break;
7340255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7341255736Sdavidch                if (print)
7342255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MISC");
7343255736Sdavidch                break;
7344255736Sdavidch            }
7345255736Sdavidch
7346255736Sdavidch            /* Clear the bit */
7347255736Sdavidch            sig &= ~cur_bit;
7348255736Sdavidch        }
7349255736Sdavidch    }
7350255736Sdavidch
7351255736Sdavidch    return (par_num);
7352255736Sdavidch}
7353255736Sdavidch
7354255736Sdavidchstatic int
7355255736Sdavidchbxe_check_blocks_with_parity3(struct bxe_softc *sc,
7356255736Sdavidch                              uint32_t         sig,
7357255736Sdavidch                              int              par_num,
7358255736Sdavidch                              uint8_t          *global,
7359255736Sdavidch                              uint8_t          print)
7360255736Sdavidch{
7361255736Sdavidch    uint32_t cur_bit = 0;
7362255736Sdavidch    int i = 0;
7363255736Sdavidch
7364255736Sdavidch    for (i = 0; sig; i++) {
7365255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7366255736Sdavidch        if (sig & cur_bit) {
7367255736Sdavidch            switch (cur_bit) {
7368255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7369255736Sdavidch                if (print)
7370255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MCP ROM");
7371255736Sdavidch                *global = TRUE;
7372255736Sdavidch                break;
7373255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7374255736Sdavidch                if (print)
7375255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7376255736Sdavidch                              "MCP UMP RX");
7377255736Sdavidch                *global = TRUE;
7378255736Sdavidch                break;
7379255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7380255736Sdavidch                if (print)
7381255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7382255736Sdavidch                              "MCP UMP TX");
7383255736Sdavidch                *global = TRUE;
7384255736Sdavidch                break;
7385255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7386255736Sdavidch                if (print)
7387255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7388255736Sdavidch                              "MCP SCPAD");
7389255736Sdavidch                *global = TRUE;
7390255736Sdavidch                break;
7391255736Sdavidch            }
7392255736Sdavidch
7393255736Sdavidch            /* Clear the bit */
7394255736Sdavidch            sig &= ~cur_bit;
7395255736Sdavidch        }
7396255736Sdavidch    }
7397255736Sdavidch
7398255736Sdavidch    return (par_num);
7399255736Sdavidch}
7400255736Sdavidch
7401255736Sdavidchstatic int
7402255736Sdavidchbxe_check_blocks_with_parity4(struct bxe_softc *sc,
7403255736Sdavidch                              uint32_t         sig,
7404255736Sdavidch                              int              par_num,
7405255736Sdavidch                              uint8_t          print)
7406255736Sdavidch{
7407255736Sdavidch    uint32_t cur_bit = 0;
7408255736Sdavidch    int i = 0;
7409255736Sdavidch
7410255736Sdavidch    for (i = 0; sig; i++) {
7411255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7412255736Sdavidch        if (sig & cur_bit) {
7413255736Sdavidch            switch (cur_bit) {
7414255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7415255736Sdavidch                if (print)
7416255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PGLUE_B");
7417255736Sdavidch                break;
7418255736Sdavidch            case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7419255736Sdavidch                if (print)
7420255736Sdavidch                    bxe_print_next_block(sc, par_num++, "ATC");
7421255736Sdavidch                break;
7422255736Sdavidch            }
7423255736Sdavidch
7424255736Sdavidch            /* Clear the bit */
7425255736Sdavidch            sig &= ~cur_bit;
7426255736Sdavidch        }
7427255736Sdavidch    }
7428255736Sdavidch
7429255736Sdavidch    return (par_num);
7430255736Sdavidch}
7431255736Sdavidch
7432255736Sdavidchstatic uint8_t
7433255736Sdavidchbxe_parity_attn(struct bxe_softc *sc,
7434255736Sdavidch                uint8_t          *global,
7435255736Sdavidch                uint8_t          print,
7436255736Sdavidch                uint32_t         *sig)
7437255736Sdavidch{
7438255736Sdavidch    int par_num = 0;
7439255736Sdavidch
7440255736Sdavidch    if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7441255736Sdavidch        (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7442255736Sdavidch        (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7443255736Sdavidch        (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7444255736Sdavidch        (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7445255736Sdavidch        BLOGE(sc, "Parity error: HW block parity attention:\n"
7446255736Sdavidch                  "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7447255736Sdavidch              (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7448255736Sdavidch              (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7449255736Sdavidch              (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7450255736Sdavidch              (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7451255736Sdavidch              (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7452255736Sdavidch
7453255736Sdavidch        if (print)
7454255736Sdavidch            BLOGI(sc, "Parity errors detected in blocks: ");
7455255736Sdavidch
7456255736Sdavidch        par_num =
7457255736Sdavidch            bxe_check_blocks_with_parity0(sc, sig[0] &
7458255736Sdavidch                                          HW_PRTY_ASSERT_SET_0,
7459255736Sdavidch                                          par_num, print);
7460255736Sdavidch        par_num =
7461255736Sdavidch            bxe_check_blocks_with_parity1(sc, sig[1] &
7462255736Sdavidch                                          HW_PRTY_ASSERT_SET_1,
7463255736Sdavidch                                          par_num, global, print);
7464255736Sdavidch        par_num =
7465255736Sdavidch            bxe_check_blocks_with_parity2(sc, sig[2] &
7466255736Sdavidch                                          HW_PRTY_ASSERT_SET_2,
7467255736Sdavidch                                          par_num, print);
7468255736Sdavidch        par_num =
7469255736Sdavidch            bxe_check_blocks_with_parity3(sc, sig[3] &
7470255736Sdavidch                                          HW_PRTY_ASSERT_SET_3,
7471255736Sdavidch                                          par_num, global, print);
7472255736Sdavidch        par_num =
7473255736Sdavidch            bxe_check_blocks_with_parity4(sc, sig[4] &
7474255736Sdavidch                                          HW_PRTY_ASSERT_SET_4,
7475255736Sdavidch                                          par_num, print);
7476255736Sdavidch
7477255736Sdavidch        if (print)
7478255736Sdavidch            BLOGI(sc, "\n");
7479255736Sdavidch
7480255736Sdavidch        return (TRUE);
7481255736Sdavidch    }
7482255736Sdavidch
7483255736Sdavidch    return (FALSE);
7484255736Sdavidch}
7485255736Sdavidch
7486255736Sdavidchstatic uint8_t
7487255736Sdavidchbxe_chk_parity_attn(struct bxe_softc *sc,
7488255736Sdavidch                    uint8_t          *global,
7489255736Sdavidch                    uint8_t          print)
7490255736Sdavidch{
7491255736Sdavidch    struct attn_route attn = { {0} };
7492255736Sdavidch    int port = SC_PORT(sc);
7493255736Sdavidch
7494255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7495255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7496255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7497255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7498255736Sdavidch
7499295823Sdavidcs    /*
7500295823Sdavidcs     * Since MCP attentions can't be disabled inside the block, we need to
7501295823Sdavidcs     * read AEU registers to see whether they're currently disabled
7502295823Sdavidcs     */
7503295823Sdavidcs    attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7504295823Sdavidcs                                      : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7505295823Sdavidcs                         MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7506295823Sdavidcs                        ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7507295823Sdavidcs
7508295823Sdavidcs
7509255736Sdavidch    if (!CHIP_IS_E1x(sc))
7510255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7511255736Sdavidch
7512255736Sdavidch    return (bxe_parity_attn(sc, global, print, attn.sig));
7513255736Sdavidch}
7514255736Sdavidch
7515255736Sdavidchstatic void
7516255736Sdavidchbxe_attn_int_deasserted4(struct bxe_softc *sc,
7517255736Sdavidch                         uint32_t         attn)
7518255736Sdavidch{
7519255736Sdavidch    uint32_t val;
7520255736Sdavidch
7521255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7522255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7523255736Sdavidch        BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7524255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7525255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7526255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7527255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7528255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7529255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7530255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7531255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7532255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7533255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7534255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7535255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7536255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7537255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7538255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7539255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7540255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7541255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7542255736Sdavidch    }
7543255736Sdavidch
7544255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7545255736Sdavidch        val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7546255736Sdavidch        BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7547255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7548255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7549255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7550255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7551255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7552255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7553255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7554255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7555255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7556255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7557255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7558255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7559255736Sdavidch    }
7560255736Sdavidch
7561255736Sdavidch    if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7562255736Sdavidch                AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7563255736Sdavidch        BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7564255736Sdavidch              (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7565255736Sdavidch                                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7566255736Sdavidch    }
7567255736Sdavidch}
7568255736Sdavidch
7569255736Sdavidchstatic void
7570255736Sdavidchbxe_e1h_disable(struct bxe_softc *sc)
7571255736Sdavidch{
7572255736Sdavidch    int port = SC_PORT(sc);
7573255736Sdavidch
7574255736Sdavidch    bxe_tx_disable(sc);
7575255736Sdavidch
7576255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7577255736Sdavidch}
7578255736Sdavidch
7579255736Sdavidchstatic void
7580255736Sdavidchbxe_e1h_enable(struct bxe_softc *sc)
7581255736Sdavidch{
7582255736Sdavidch    int port = SC_PORT(sc);
7583255736Sdavidch
7584255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7585255736Sdavidch
7586255736Sdavidch    // XXX bxe_tx_enable(sc);
7587255736Sdavidch}
7588255736Sdavidch
7589255736Sdavidch/*
7590255736Sdavidch * called due to MCP event (on pmf):
7591255736Sdavidch *   reread new bandwidth configuration
7592255736Sdavidch *   configure FW
7593255736Sdavidch *   notify others function about the change
7594255736Sdavidch */
7595255736Sdavidchstatic void
7596255736Sdavidchbxe_config_mf_bw(struct bxe_softc *sc)
7597255736Sdavidch{
7598255736Sdavidch    if (sc->link_vars.link_up) {
7599255736Sdavidch        bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7600255736Sdavidch        // XXX bxe_link_sync_notify(sc);
7601255736Sdavidch    }
7602255736Sdavidch
7603255736Sdavidch    storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7604255736Sdavidch}
7605255736Sdavidch
7606255736Sdavidchstatic void
7607255736Sdavidchbxe_set_mf_bw(struct bxe_softc *sc)
7608255736Sdavidch{
7609255736Sdavidch    bxe_config_mf_bw(sc);
7610255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7611255736Sdavidch}
7612255736Sdavidch
7613255736Sdavidchstatic void
7614255736Sdavidchbxe_handle_eee_event(struct bxe_softc *sc)
7615255736Sdavidch{
7616255736Sdavidch    BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7617255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7618255736Sdavidch}
7619255736Sdavidch
7620255736Sdavidch#define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7621255736Sdavidch
7622255736Sdavidchstatic void
7623255736Sdavidchbxe_drv_info_ether_stat(struct bxe_softc *sc)
7624255736Sdavidch{
7625255736Sdavidch    struct eth_stats_info *ether_stat =
7626255736Sdavidch        &sc->sp->drv_info_to_mcp.ether_stat;
7627255736Sdavidch
7628255736Sdavidch    strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7629255736Sdavidch            ETH_STAT_INFO_VERSION_LEN);
7630255736Sdavidch
7631255736Sdavidch    /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7632255736Sdavidch    sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7633255736Sdavidch                                          DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7634255736Sdavidch                                          ether_stat->mac_local + MAC_PAD,
7635255736Sdavidch                                          MAC_PAD, ETH_ALEN);
7636255736Sdavidch
7637255736Sdavidch    ether_stat->mtu_size = sc->mtu;
7638255736Sdavidch
7639255736Sdavidch    ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7640266979Smarcel    if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
7641255736Sdavidch        ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7642255736Sdavidch    }
7643255736Sdavidch
7644255736Sdavidch    // XXX ether_stat->feature_flags |= ???;
7645255736Sdavidch
7646255736Sdavidch    ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7647255736Sdavidch
7648255736Sdavidch    ether_stat->txq_size = sc->tx_ring_size;
7649255736Sdavidch    ether_stat->rxq_size = sc->rx_ring_size;
7650255736Sdavidch}
7651255736Sdavidch
7652255736Sdavidchstatic void
7653255736Sdavidchbxe_handle_drv_info_req(struct bxe_softc *sc)
7654255736Sdavidch{
7655255736Sdavidch    enum drv_info_opcode op_code;
7656255736Sdavidch    uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7657255736Sdavidch
7658255736Sdavidch    /* if drv_info version supported by MFW doesn't match - send NACK */
7659255736Sdavidch    if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7660255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7661255736Sdavidch        return;
7662255736Sdavidch    }
7663255736Sdavidch
7664255736Sdavidch    op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7665255736Sdavidch               DRV_INFO_CONTROL_OP_CODE_SHIFT);
7666255736Sdavidch
7667255736Sdavidch    memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7668255736Sdavidch
7669255736Sdavidch    switch (op_code) {
7670255736Sdavidch    case ETH_STATS_OPCODE:
7671255736Sdavidch        bxe_drv_info_ether_stat(sc);
7672255736Sdavidch        break;
7673255736Sdavidch    case FCOE_STATS_OPCODE:
7674255736Sdavidch    case ISCSI_STATS_OPCODE:
7675255736Sdavidch    default:
7676255736Sdavidch        /* if op code isn't supported - send NACK */
7677255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7678255736Sdavidch        return;
7679255736Sdavidch    }
7680255736Sdavidch
7681255736Sdavidch    /*
7682255736Sdavidch     * If we got drv_info attn from MFW then these fields are defined in
7683255736Sdavidch     * shmem2 for sure
7684255736Sdavidch     */
7685255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_lo,
7686255736Sdavidch              U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7687255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_hi,
7688255736Sdavidch              U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7689255736Sdavidch
7690255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7691255736Sdavidch}
7692255736Sdavidch
7693255736Sdavidchstatic void
7694255736Sdavidchbxe_dcc_event(struct bxe_softc *sc,
7695255736Sdavidch              uint32_t         dcc_event)
7696255736Sdavidch{
7697255736Sdavidch    BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7698255736Sdavidch
7699255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7700255736Sdavidch        /*
7701255736Sdavidch         * This is the only place besides the function initialization
7702255736Sdavidch         * where the sc->flags can change so it is done without any
7703255736Sdavidch         * locks
7704255736Sdavidch         */
7705255736Sdavidch        if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7706255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7707255736Sdavidch            sc->flags |= BXE_MF_FUNC_DIS;
7708255736Sdavidch            bxe_e1h_disable(sc);
7709255736Sdavidch        } else {
7710255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7711255736Sdavidch            sc->flags &= ~BXE_MF_FUNC_DIS;
7712255736Sdavidch            bxe_e1h_enable(sc);
7713255736Sdavidch        }
7714255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7715255736Sdavidch    }
7716255736Sdavidch
7717255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7718255736Sdavidch        bxe_config_mf_bw(sc);
7719255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7720255736Sdavidch    }
7721255736Sdavidch
7722255736Sdavidch    /* Report results to MCP */
7723255736Sdavidch    if (dcc_event)
7724255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7725255736Sdavidch    else
7726255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7727255736Sdavidch}
7728255736Sdavidch
7729255736Sdavidchstatic void
7730255736Sdavidchbxe_pmf_update(struct bxe_softc *sc)
7731255736Sdavidch{
7732255736Sdavidch    int port = SC_PORT(sc);
7733255736Sdavidch    uint32_t val;
7734255736Sdavidch
7735255736Sdavidch    sc->port.pmf = 1;
7736255736Sdavidch    BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7737255736Sdavidch
7738255736Sdavidch    /*
7739255736Sdavidch     * We need the mb() to ensure the ordering between the writing to
7740255736Sdavidch     * sc->port.pmf here and reading it from the bxe_periodic_task().
7741255736Sdavidch     */
7742255736Sdavidch    mb();
7743255736Sdavidch
7744255736Sdavidch    /* queue a periodic task */
7745255736Sdavidch    // XXX schedule task...
7746255736Sdavidch
7747255736Sdavidch    // XXX bxe_dcbx_pmf_update(sc);
7748255736Sdavidch
7749255736Sdavidch    /* enable nig attention */
7750255736Sdavidch    val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7751255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7752255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7753255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7754255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
7755255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7756255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7757255736Sdavidch    }
7758255736Sdavidch
7759255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_PMF);
7760255736Sdavidch}
7761255736Sdavidch
7762255736Sdavidchstatic int
7763255736Sdavidchbxe_mc_assert(struct bxe_softc *sc)
7764255736Sdavidch{
7765255736Sdavidch    char last_idx;
7766255736Sdavidch    int i, rc = 0;
7767255736Sdavidch    uint32_t row0, row1, row2, row3;
7768255736Sdavidch
7769255736Sdavidch    /* XSTORM */
7770255736Sdavidch    last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7771255736Sdavidch    if (last_idx)
7772255736Sdavidch        BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7773255736Sdavidch
7774255736Sdavidch    /* print the asserts */
7775255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7776255736Sdavidch
7777255736Sdavidch        row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7778255736Sdavidch        row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7779255736Sdavidch        row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7780255736Sdavidch        row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7781255736Sdavidch
7782255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7783255736Sdavidch            BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7784255736Sdavidch                  i, row3, row2, row1, row0);
7785255736Sdavidch            rc++;
7786255736Sdavidch        } else {
7787255736Sdavidch            break;
7788255736Sdavidch        }
7789255736Sdavidch    }
7790255736Sdavidch
7791255736Sdavidch    /* TSTORM */
7792255736Sdavidch    last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7793255736Sdavidch    if (last_idx) {
7794255736Sdavidch        BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7795255736Sdavidch    }
7796255736Sdavidch
7797255736Sdavidch    /* print the asserts */
7798255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7799255736Sdavidch
7800255736Sdavidch        row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7801255736Sdavidch        row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7802255736Sdavidch        row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7803255736Sdavidch        row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7804255736Sdavidch
7805255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7806255736Sdavidch            BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7807255736Sdavidch                  i, row3, row2, row1, row0);
7808255736Sdavidch            rc++;
7809255736Sdavidch        } else {
7810255736Sdavidch            break;
7811255736Sdavidch        }
7812255736Sdavidch    }
7813255736Sdavidch
7814255736Sdavidch    /* CSTORM */
7815255736Sdavidch    last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7816255736Sdavidch    if (last_idx) {
7817255736Sdavidch        BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7818255736Sdavidch    }
7819255736Sdavidch
7820255736Sdavidch    /* print the asserts */
7821255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7822255736Sdavidch
7823255736Sdavidch        row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7824255736Sdavidch        row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7825255736Sdavidch        row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7826255736Sdavidch        row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7827255736Sdavidch
7828255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7829255736Sdavidch            BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7830255736Sdavidch                  i, row3, row2, row1, row0);
7831255736Sdavidch            rc++;
7832255736Sdavidch        } else {
7833255736Sdavidch            break;
7834255736Sdavidch        }
7835255736Sdavidch    }
7836255736Sdavidch
7837255736Sdavidch    /* USTORM */
7838255736Sdavidch    last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7839255736Sdavidch    if (last_idx) {
7840255736Sdavidch        BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7841255736Sdavidch    }
7842255736Sdavidch
7843255736Sdavidch    /* print the asserts */
7844255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7845255736Sdavidch
7846255736Sdavidch        row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7847255736Sdavidch        row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7848255736Sdavidch        row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7849255736Sdavidch        row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7850255736Sdavidch
7851255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7852255736Sdavidch            BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7853255736Sdavidch                  i, row3, row2, row1, row0);
7854255736Sdavidch            rc++;
7855255736Sdavidch        } else {
7856255736Sdavidch            break;
7857255736Sdavidch        }
7858255736Sdavidch    }
7859255736Sdavidch
7860255736Sdavidch    return (rc);
7861255736Sdavidch}
7862255736Sdavidch
7863255736Sdavidchstatic void
7864255736Sdavidchbxe_attn_int_deasserted3(struct bxe_softc *sc,
7865255736Sdavidch                         uint32_t         attn)
7866255736Sdavidch{
7867255736Sdavidch    int func = SC_FUNC(sc);
7868255736Sdavidch    uint32_t val;
7869255736Sdavidch
7870255736Sdavidch    if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
7871255736Sdavidch
7872255736Sdavidch        if (attn & BXE_PMF_LINK_ASSERT(sc)) {
7873255736Sdavidch
7874255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
7875255736Sdavidch            bxe_read_mf_cfg(sc);
7876255736Sdavidch            sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
7877255736Sdavidch                MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
7878255736Sdavidch            val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
7879255736Sdavidch
7880255736Sdavidch            if (val & DRV_STATUS_DCC_EVENT_MASK)
7881255736Sdavidch                bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
7882255736Sdavidch
7883255736Sdavidch            if (val & DRV_STATUS_SET_MF_BW)
7884255736Sdavidch                bxe_set_mf_bw(sc);
7885255736Sdavidch
7886255736Sdavidch            if (val & DRV_STATUS_DRV_INFO_REQ)
7887255736Sdavidch                bxe_handle_drv_info_req(sc);
7888255736Sdavidch
7889255736Sdavidch            if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
7890255736Sdavidch                bxe_pmf_update(sc);
7891255736Sdavidch
7892255736Sdavidch            if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
7893255736Sdavidch                bxe_handle_eee_event(sc);
7894255736Sdavidch
7895255736Sdavidch            if (sc->link_vars.periodic_flags &
7896255736Sdavidch                ELINK_PERIODIC_FLAGS_LINK_EVENT) {
7897255736Sdavidch                /* sync with link */
7898284335Sdavidcs		bxe_acquire_phy_lock(sc);
7899255736Sdavidch                sc->link_vars.periodic_flags &=
7900255736Sdavidch                    ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
7901284335Sdavidcs		bxe_release_phy_lock(sc);
7902255736Sdavidch                if (IS_MF(sc))
7903255736Sdavidch                    ; // XXX bxe_link_sync_notify(sc);
7904255736Sdavidch                bxe_link_report(sc);
7905255736Sdavidch            }
7906255736Sdavidch
7907255736Sdavidch            /*
7908255736Sdavidch             * Always call it here: bxe_link_report() will
7909255736Sdavidch             * prevent the link indication duplication.
7910255736Sdavidch             */
7911255736Sdavidch            bxe_link_status_update(sc);
7912255736Sdavidch
7913255736Sdavidch        } else if (attn & BXE_MC_ASSERT_BITS) {
7914255736Sdavidch
7915255736Sdavidch            BLOGE(sc, "MC assert!\n");
7916255736Sdavidch            bxe_mc_assert(sc);
7917255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
7918255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
7919255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
7920255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
7921255736Sdavidch            bxe_panic(sc, ("MC assert!\n"));
7922255736Sdavidch
7923255736Sdavidch        } else if (attn & BXE_MCP_ASSERT) {
7924255736Sdavidch
7925255736Sdavidch            BLOGE(sc, "MCP assert!\n");
7926255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
7927255736Sdavidch            // XXX bxe_fw_dump(sc);
7928255736Sdavidch
7929255736Sdavidch        } else {
7930255736Sdavidch            BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
7931255736Sdavidch        }
7932255736Sdavidch    }
7933255736Sdavidch
7934255736Sdavidch    if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
7935255736Sdavidch        BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
7936255736Sdavidch        if (attn & BXE_GRC_TIMEOUT) {
7937255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
7938255736Sdavidch            BLOGE(sc, "GRC time-out 0x%08x\n", val);
7939255736Sdavidch        }
7940255736Sdavidch        if (attn & BXE_GRC_RSV) {
7941255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
7942255736Sdavidch            BLOGE(sc, "GRC reserved 0x%08x\n", val);
7943255736Sdavidch        }
7944255736Sdavidch        REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
7945255736Sdavidch    }
7946255736Sdavidch}
7947255736Sdavidch
7948255736Sdavidchstatic void
7949255736Sdavidchbxe_attn_int_deasserted2(struct bxe_softc *sc,
7950255736Sdavidch                         uint32_t         attn)
7951255736Sdavidch{
7952255736Sdavidch    int port = SC_PORT(sc);
7953255736Sdavidch    int reg_offset;
7954255736Sdavidch    uint32_t val0, mask0, val1, mask1;
7955255736Sdavidch    uint32_t val;
7956255736Sdavidch
7957255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
7958255736Sdavidch        val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
7959255736Sdavidch        BLOGE(sc, "CFC hw attention 0x%08x\n", val);
7960255736Sdavidch        /* CFC error attention */
7961255736Sdavidch        if (val & 0x2) {
7962255736Sdavidch            BLOGE(sc, "FATAL error from CFC\n");
7963255736Sdavidch        }
7964255736Sdavidch    }
7965255736Sdavidch
7966255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
7967255736Sdavidch        val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
7968255736Sdavidch        BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
7969255736Sdavidch        /* RQ_USDMDP_FIFO_OVERFLOW */
7970255736Sdavidch        if (val & 0x18000) {
7971255736Sdavidch            BLOGE(sc, "FATAL error from PXP\n");
7972255736Sdavidch        }
7973255736Sdavidch
7974255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
7975255736Sdavidch            val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
7976255736Sdavidch            BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
7977255736Sdavidch        }
7978255736Sdavidch    }
7979255736Sdavidch
7980255736Sdavidch#define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
7981255736Sdavidch#define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
7982255736Sdavidch
7983255736Sdavidch    if (attn & AEU_PXP2_HW_INT_BIT) {
7984255736Sdavidch        /*  CQ47854 workaround do not panic on
7985255736Sdavidch         *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
7986255736Sdavidch         */
7987255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
7988255736Sdavidch            mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
7989255736Sdavidch            val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
7990255736Sdavidch            mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
7991255736Sdavidch            val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
7992255736Sdavidch            /*
7993298955Spfg             * If the only PXP2_EOP_ERROR_BIT is set in
7994255736Sdavidch             * STS0 and STS1 - clear it
7995255736Sdavidch             *
7996255736Sdavidch             * probably we lose additional attentions between
7997255736Sdavidch             * STS0 and STS_CLR0, in this case user will not
7998255736Sdavidch             * be notified about them
7999255736Sdavidch             */
8000255736Sdavidch            if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8001255736Sdavidch                !(val1 & mask1))
8002255736Sdavidch                val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8003255736Sdavidch
8004255736Sdavidch            /* print the register, since no one can restore it */
8005255736Sdavidch            BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8006255736Sdavidch
8007255736Sdavidch            /*
8008255736Sdavidch             * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8009255736Sdavidch             * then notify
8010255736Sdavidch             */
8011255736Sdavidch            if (val0 & PXP2_EOP_ERROR_BIT) {
8012255736Sdavidch                BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8013255736Sdavidch
8014255736Sdavidch                /*
8015255736Sdavidch                 * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8016255736Sdavidch                 * set then clear attention from PXP2 block without panic
8017255736Sdavidch                 */
8018255736Sdavidch                if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8019255736Sdavidch                    ((val1 & mask1) == 0))
8020255736Sdavidch                    attn &= ~AEU_PXP2_HW_INT_BIT;
8021255736Sdavidch            }
8022255736Sdavidch        }
8023255736Sdavidch    }
8024255736Sdavidch
8025255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_2) {
8026255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8027255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8028255736Sdavidch
8029255736Sdavidch        val = REG_RD(sc, reg_offset);
8030255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8031255736Sdavidch        REG_WR(sc, reg_offset, val);
8032255736Sdavidch
8033255736Sdavidch        BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8034255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8035255736Sdavidch        bxe_panic(sc, ("HW block attention set2\n"));
8036255736Sdavidch    }
8037255736Sdavidch}
8038255736Sdavidch
8039255736Sdavidchstatic void
8040255736Sdavidchbxe_attn_int_deasserted1(struct bxe_softc *sc,
8041255736Sdavidch                         uint32_t         attn)
8042255736Sdavidch{
8043255736Sdavidch    int port = SC_PORT(sc);
8044255736Sdavidch    int reg_offset;
8045255736Sdavidch    uint32_t val;
8046255736Sdavidch
8047255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8048255736Sdavidch        val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8049255736Sdavidch        BLOGE(sc, "DB hw attention 0x%08x\n", val);
8050255736Sdavidch        /* DORQ discard attention */
8051255736Sdavidch        if (val & 0x2) {
8052255736Sdavidch            BLOGE(sc, "FATAL error from DORQ\n");
8053255736Sdavidch        }
8054255736Sdavidch    }
8055255736Sdavidch
8056255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_1) {
8057255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8058255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8059255736Sdavidch
8060255736Sdavidch        val = REG_RD(sc, reg_offset);
8061255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8062255736Sdavidch        REG_WR(sc, reg_offset, val);
8063255736Sdavidch
8064255736Sdavidch        BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8065255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8066255736Sdavidch        bxe_panic(sc, ("HW block attention set1\n"));
8067255736Sdavidch    }
8068255736Sdavidch}
8069255736Sdavidch
8070255736Sdavidchstatic void
8071255736Sdavidchbxe_attn_int_deasserted0(struct bxe_softc *sc,
8072255736Sdavidch                         uint32_t         attn)
8073255736Sdavidch{
8074255736Sdavidch    int port = SC_PORT(sc);
8075255736Sdavidch    int reg_offset;
8076255736Sdavidch    uint32_t val;
8077255736Sdavidch
8078255736Sdavidch    reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8079255736Sdavidch                          MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8080255736Sdavidch
8081255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8082255736Sdavidch        val = REG_RD(sc, reg_offset);
8083255736Sdavidch        val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8084255736Sdavidch        REG_WR(sc, reg_offset, val);
8085255736Sdavidch
8086255736Sdavidch        BLOGW(sc, "SPIO5 hw attention\n");
8087255736Sdavidch
8088255736Sdavidch        /* Fan failure attention */
8089255736Sdavidch        elink_hw_reset_phy(&sc->link_params);
8090255736Sdavidch        bxe_fan_failure(sc);
8091255736Sdavidch    }
8092255736Sdavidch
8093255736Sdavidch    if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8094284335Sdavidcs	bxe_acquire_phy_lock(sc);
8095255736Sdavidch        elink_handle_module_detect_int(&sc->link_params);
8096284335Sdavidcs	bxe_release_phy_lock(sc);
8097255736Sdavidch    }
8098255736Sdavidch
8099255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_0) {
8100255736Sdavidch        val = REG_RD(sc, reg_offset);
8101255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8102255736Sdavidch        REG_WR(sc, reg_offset, val);
8103255736Sdavidch
8104255736Sdavidch        bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8105255736Sdavidch                       (attn & HW_INTERRUT_ASSERT_SET_0)));
8106255736Sdavidch    }
8107255736Sdavidch}
8108255736Sdavidch
8109255736Sdavidchstatic void
8110255736Sdavidchbxe_attn_int_deasserted(struct bxe_softc *sc,
8111255736Sdavidch                        uint32_t         deasserted)
8112255736Sdavidch{
8113255736Sdavidch    struct attn_route attn;
8114255736Sdavidch    struct attn_route *group_mask;
8115255736Sdavidch    int port = SC_PORT(sc);
8116255736Sdavidch    int index;
8117255736Sdavidch    uint32_t reg_addr;
8118255736Sdavidch    uint32_t val;
8119255736Sdavidch    uint32_t aeu_mask;
8120255736Sdavidch    uint8_t global = FALSE;
8121255736Sdavidch
8122255736Sdavidch    /*
8123255736Sdavidch     * Need to take HW lock because MCP or other port might also
8124255736Sdavidch     * try to handle this event.
8125255736Sdavidch     */
8126255736Sdavidch    bxe_acquire_alr(sc);
8127255736Sdavidch
8128255736Sdavidch    if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8129255736Sdavidch        /* XXX
8130255736Sdavidch         * In case of parity errors don't handle attentions so that
8131255736Sdavidch         * other function would "see" parity errors.
8132255736Sdavidch         */
8133255736Sdavidch        sc->recovery_state = BXE_RECOVERY_INIT;
8134255736Sdavidch        // XXX schedule a recovery task...
8135255736Sdavidch        /* disable HW interrupts */
8136255736Sdavidch        bxe_int_disable(sc);
8137255736Sdavidch        bxe_release_alr(sc);
8138255736Sdavidch        return;
8139255736Sdavidch    }
8140255736Sdavidch
8141255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8142255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8143255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8144255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8145255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
8146255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8147255736Sdavidch    } else {
8148255736Sdavidch        attn.sig[4] = 0;
8149255736Sdavidch    }
8150255736Sdavidch
8151255736Sdavidch    BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8152255736Sdavidch          attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8153255736Sdavidch
8154255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8155255736Sdavidch        if (deasserted & (1 << index)) {
8156255736Sdavidch            group_mask = &sc->attn_group[index];
8157255736Sdavidch
8158255736Sdavidch            BLOGD(sc, DBG_INTR,
8159255736Sdavidch                  "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8160255736Sdavidch                  group_mask->sig[0], group_mask->sig[1],
8161255736Sdavidch                  group_mask->sig[2], group_mask->sig[3],
8162255736Sdavidch                  group_mask->sig[4]);
8163255736Sdavidch
8164255736Sdavidch            bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8165255736Sdavidch            bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8166255736Sdavidch            bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8167255736Sdavidch            bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8168255736Sdavidch            bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8169255736Sdavidch        }
8170255736Sdavidch    }
8171255736Sdavidch
8172255736Sdavidch    bxe_release_alr(sc);
8173255736Sdavidch
8174255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
8175255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 +
8176255736Sdavidch                    COMMAND_REG_ATTN_BITS_CLR);
8177255736Sdavidch    } else {
8178255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8179255736Sdavidch    }
8180255736Sdavidch
8181255736Sdavidch    val = ~deasserted;
8182255736Sdavidch    BLOGD(sc, DBG_INTR,
8183255736Sdavidch          "about to mask 0x%08x at %s addr 0x%08x\n", val,
8184255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8185255736Sdavidch    REG_WR(sc, reg_addr, val);
8186255736Sdavidch
8187255736Sdavidch    if (~sc->attn_state & deasserted) {
8188255736Sdavidch        BLOGE(sc, "IGU error\n");
8189255736Sdavidch    }
8190255736Sdavidch
8191255736Sdavidch    reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8192255736Sdavidch                      MISC_REG_AEU_MASK_ATTN_FUNC_0;
8193255736Sdavidch
8194255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8195255736Sdavidch
8196255736Sdavidch    aeu_mask = REG_RD(sc, reg_addr);
8197255736Sdavidch
8198255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8199255736Sdavidch          aeu_mask, deasserted);
8200255736Sdavidch    aeu_mask |= (deasserted & 0x3ff);
8201255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8202255736Sdavidch
8203255736Sdavidch    REG_WR(sc, reg_addr, aeu_mask);
8204255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8205255736Sdavidch
8206255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8207255736Sdavidch    sc->attn_state &= ~deasserted;
8208255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8209255736Sdavidch}
8210255736Sdavidch
8211255736Sdavidchstatic void
8212255736Sdavidchbxe_attn_int(struct bxe_softc *sc)
8213255736Sdavidch{
8214255736Sdavidch    /* read local copy of bits */
8215255736Sdavidch    uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8216255736Sdavidch    uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8217255736Sdavidch    uint32_t attn_state = sc->attn_state;
8218255736Sdavidch
8219255736Sdavidch    /* look for changed bits */
8220255736Sdavidch    uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8221255736Sdavidch    uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8222255736Sdavidch
8223255736Sdavidch    BLOGD(sc, DBG_INTR,
8224255736Sdavidch          "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8225255736Sdavidch          attn_bits, attn_ack, asserted, deasserted);
8226255736Sdavidch
8227255736Sdavidch    if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8228255736Sdavidch        BLOGE(sc, "BAD attention state\n");
8229255736Sdavidch    }
8230255736Sdavidch
8231255736Sdavidch    /* handle bits that were raised */
8232255736Sdavidch    if (asserted) {
8233255736Sdavidch        bxe_attn_int_asserted(sc, asserted);
8234255736Sdavidch    }
8235255736Sdavidch
8236255736Sdavidch    if (deasserted) {
8237255736Sdavidch        bxe_attn_int_deasserted(sc, deasserted);
8238255736Sdavidch    }
8239255736Sdavidch}
8240255736Sdavidch
8241255736Sdavidchstatic uint16_t
8242255736Sdavidchbxe_update_dsb_idx(struct bxe_softc *sc)
8243255736Sdavidch{
8244255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
8245255736Sdavidch    uint16_t rc = 0;
8246255736Sdavidch
8247255736Sdavidch    mb(); /* status block is written to by the chip */
8248255736Sdavidch
8249255736Sdavidch    if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8250255736Sdavidch        sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8251255736Sdavidch        rc |= BXE_DEF_SB_ATT_IDX;
8252255736Sdavidch    }
8253255736Sdavidch
8254255736Sdavidch    if (sc->def_idx != def_sb->sp_sb.running_index) {
8255255736Sdavidch        sc->def_idx = def_sb->sp_sb.running_index;
8256255736Sdavidch        rc |= BXE_DEF_SB_IDX;
8257255736Sdavidch    }
8258255736Sdavidch
8259255736Sdavidch    mb();
8260255736Sdavidch
8261255736Sdavidch    return (rc);
8262255736Sdavidch}
8263255736Sdavidch
8264255736Sdavidchstatic inline struct ecore_queue_sp_obj *
8265255736Sdavidchbxe_cid_to_q_obj(struct bxe_softc *sc,
8266255736Sdavidch                 uint32_t         cid)
8267255736Sdavidch{
8268255736Sdavidch    BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8269255736Sdavidch    return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8270255736Sdavidch}
8271255736Sdavidch
8272255736Sdavidchstatic void
8273255736Sdavidchbxe_handle_mcast_eqe(struct bxe_softc *sc)
8274255736Sdavidch{
8275255736Sdavidch    struct ecore_mcast_ramrod_params rparam;
8276255736Sdavidch    int rc;
8277255736Sdavidch
8278255736Sdavidch    memset(&rparam, 0, sizeof(rparam));
8279255736Sdavidch
8280255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
8281255736Sdavidch
8282255736Sdavidch    BXE_MCAST_LOCK(sc);
8283255736Sdavidch
8284255736Sdavidch    /* clear pending state for the last command */
8285255736Sdavidch    sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8286255736Sdavidch
8287255736Sdavidch    /* if there are pending mcast commands - send them */
8288255736Sdavidch    if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8289255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8290255736Sdavidch        if (rc < 0) {
8291255736Sdavidch            BLOGD(sc, DBG_SP,
8292295830Sdavidcs                "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8293255736Sdavidch        }
8294255736Sdavidch    }
8295255736Sdavidch
8296255736Sdavidch    BXE_MCAST_UNLOCK(sc);
8297255736Sdavidch}
8298255736Sdavidch
8299255736Sdavidchstatic void
8300255736Sdavidchbxe_handle_classification_eqe(struct bxe_softc      *sc,
8301255736Sdavidch                              union event_ring_elem *elem)
8302255736Sdavidch{
8303255736Sdavidch    unsigned long ramrod_flags = 0;
8304255736Sdavidch    int rc = 0;
8305255736Sdavidch    uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8306255736Sdavidch    struct ecore_vlan_mac_obj *vlan_mac_obj;
8307255736Sdavidch
8308255736Sdavidch    /* always push next commands out, don't wait here */
8309255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
8310255736Sdavidch
8311255736Sdavidch    switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8312255736Sdavidch    case ECORE_FILTER_MAC_PENDING:
8313255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8314255736Sdavidch        vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8315255736Sdavidch        break;
8316255736Sdavidch
8317255736Sdavidch    case ECORE_FILTER_MCAST_PENDING:
8318255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8319255736Sdavidch        /*
8320255736Sdavidch         * This is only relevant for 57710 where multicast MACs are
8321255736Sdavidch         * configured as unicast MACs using the same ramrod.
8322255736Sdavidch         */
8323255736Sdavidch        bxe_handle_mcast_eqe(sc);
8324255736Sdavidch        return;
8325255736Sdavidch
8326255736Sdavidch    default:
8327255736Sdavidch        BLOGE(sc, "Unsupported classification command: %d\n",
8328255736Sdavidch              elem->message.data.eth_event.echo);
8329255736Sdavidch        return;
8330255736Sdavidch    }
8331255736Sdavidch
8332255736Sdavidch    rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8333255736Sdavidch
8334255736Sdavidch    if (rc < 0) {
8335255736Sdavidch        BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8336255736Sdavidch    } else if (rc > 0) {
8337255736Sdavidch        BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8338255736Sdavidch    }
8339255736Sdavidch}
8340255736Sdavidch
8341255736Sdavidchstatic void
8342255736Sdavidchbxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8343255736Sdavidch                       union event_ring_elem *elem)
8344255736Sdavidch{
8345255736Sdavidch    bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8346255736Sdavidch
8347255736Sdavidch    /* send rx_mode command again if was requested */
8348255736Sdavidch    if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8349255736Sdavidch                               &sc->sp_state)) {
8350255736Sdavidch        bxe_set_storm_rx_mode(sc);
8351255736Sdavidch    }
8352255736Sdavidch}
8353255736Sdavidch
8354255736Sdavidchstatic void
8355255736Sdavidchbxe_update_eq_prod(struct bxe_softc *sc,
8356255736Sdavidch                   uint16_t         prod)
8357255736Sdavidch{
8358255736Sdavidch    storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8359255736Sdavidch    wmb(); /* keep prod updates ordered */
8360255736Sdavidch}
8361255736Sdavidch
8362255736Sdavidchstatic void
8363255736Sdavidchbxe_eq_int(struct bxe_softc *sc)
8364255736Sdavidch{
8365255736Sdavidch    uint16_t hw_cons, sw_cons, sw_prod;
8366255736Sdavidch    union event_ring_elem *elem;
8367255736Sdavidch    uint8_t echo;
8368255736Sdavidch    uint32_t cid;
8369255736Sdavidch    uint8_t opcode;
8370255736Sdavidch    int spqe_cnt = 0;
8371255736Sdavidch    struct ecore_queue_sp_obj *q_obj;
8372255736Sdavidch    struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8373255736Sdavidch    struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8374255736Sdavidch
8375255736Sdavidch    hw_cons = le16toh(*sc->eq_cons_sb);
8376255736Sdavidch
8377255736Sdavidch    /*
8378255736Sdavidch     * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8379255736Sdavidch     * when we get to the next-page we need to adjust so the loop
8380255736Sdavidch     * condition below will be met. The next element is the size of a
8381255736Sdavidch     * regular element and hence incrementing by 1
8382255736Sdavidch     */
8383255736Sdavidch    if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8384255736Sdavidch        hw_cons++;
8385255736Sdavidch    }
8386255736Sdavidch
8387255736Sdavidch    /*
8388255736Sdavidch     * This function may never run in parallel with itself for a
8389255736Sdavidch     * specific sc and no need for a read memory barrier here.
8390255736Sdavidch     */
8391255736Sdavidch    sw_cons = sc->eq_cons;
8392255736Sdavidch    sw_prod = sc->eq_prod;
8393255736Sdavidch
8394255736Sdavidch    BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8395255736Sdavidch          hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8396255736Sdavidch
8397255736Sdavidch    for (;
8398255736Sdavidch         sw_cons != hw_cons;
8399255736Sdavidch         sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8400255736Sdavidch
8401255736Sdavidch        elem = &sc->eq[EQ_DESC(sw_cons)];
8402255736Sdavidch
8403255736Sdavidch        /* elem CID originates from FW, actually LE */
8404255736Sdavidch        cid = SW_CID(elem->message.data.cfc_del_event.cid);
8405255736Sdavidch        opcode = elem->message.opcode;
8406255736Sdavidch
8407255736Sdavidch        /* handle eq element */
8408255736Sdavidch        switch (opcode) {
8409255736Sdavidch
8410255736Sdavidch        case EVENT_RING_OPCODE_STAT_QUERY:
8411255736Sdavidch            BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8412255736Sdavidch                  sc->stats_comp++);
8413255736Sdavidch            /* nothing to do with stats comp */
8414255736Sdavidch            goto next_spqe;
8415255736Sdavidch
8416255736Sdavidch        case EVENT_RING_OPCODE_CFC_DEL:
8417255736Sdavidch            /* handle according to cid range */
8418255736Sdavidch            /* we may want to verify here that the sc state is HALTING */
8419255736Sdavidch            BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8420255736Sdavidch            q_obj = bxe_cid_to_q_obj(sc, cid);
8421255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8422255736Sdavidch                break;
8423255736Sdavidch            }
8424255736Sdavidch            goto next_spqe;
8425255736Sdavidch
8426255736Sdavidch        case EVENT_RING_OPCODE_STOP_TRAFFIC:
8427255736Sdavidch            BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8428255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8429255736Sdavidch                break;
8430255736Sdavidch            }
8431255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8432255736Sdavidch            goto next_spqe;
8433255736Sdavidch
8434255736Sdavidch        case EVENT_RING_OPCODE_START_TRAFFIC:
8435255736Sdavidch            BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8436255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8437255736Sdavidch                break;
8438255736Sdavidch            }
8439255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8440255736Sdavidch            goto next_spqe;
8441255736Sdavidch
8442255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8443255736Sdavidch            echo = elem->message.data.function_update_event.echo;
8444255736Sdavidch            if (echo == SWITCH_UPDATE) {
8445255736Sdavidch                BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8446255736Sdavidch                if (f_obj->complete_cmd(sc, f_obj,
8447255736Sdavidch                                        ECORE_F_CMD_SWITCH_UPDATE)) {
8448255736Sdavidch                    break;
8449255736Sdavidch                }
8450255736Sdavidch            }
8451255736Sdavidch            else {
8452255736Sdavidch                BLOGD(sc, DBG_SP,
8453255736Sdavidch                      "AFEX: ramrod completed FUNCTION_UPDATE\n");
8454255736Sdavidch            }
8455255736Sdavidch            goto next_spqe;
8456255736Sdavidch
8457255736Sdavidch        case EVENT_RING_OPCODE_FORWARD_SETUP:
8458255736Sdavidch            q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8459255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj,
8460255736Sdavidch                                    ECORE_Q_CMD_SETUP_TX_ONLY)) {
8461255736Sdavidch                break;
8462255736Sdavidch            }
8463255736Sdavidch            goto next_spqe;
8464255736Sdavidch
8465255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_START:
8466255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8467255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8468255736Sdavidch                break;
8469255736Sdavidch            }
8470255736Sdavidch            goto next_spqe;
8471255736Sdavidch
8472255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_STOP:
8473255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8474255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8475255736Sdavidch                break;
8476255736Sdavidch            }
8477255736Sdavidch            goto next_spqe;
8478255736Sdavidch        }
8479255736Sdavidch
8480255736Sdavidch        switch (opcode | sc->state) {
8481255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8482255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8483255736Sdavidch            cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8484255736Sdavidch            BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8485255736Sdavidch            rss_raw->clear_pending(rss_raw);
8486255736Sdavidch            break;
8487255736Sdavidch
8488255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8489255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8490255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8491255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8492255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8493255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8494255736Sdavidch            BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8495255736Sdavidch            bxe_handle_classification_eqe(sc, elem);
8496255736Sdavidch            break;
8497255736Sdavidch
8498255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8499255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8500255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8501255736Sdavidch            BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8502255736Sdavidch            bxe_handle_mcast_eqe(sc);
8503255736Sdavidch            break;
8504255736Sdavidch
8505255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8506255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8507255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8508255736Sdavidch            BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8509255736Sdavidch            bxe_handle_rx_mode_eqe(sc, elem);
8510255736Sdavidch            break;
8511255736Sdavidch
8512255736Sdavidch        default:
8513255736Sdavidch            /* unknown event log error and continue */
8514255736Sdavidch            BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8515255736Sdavidch                  elem->message.opcode, sc->state);
8516255736Sdavidch        }
8517255736Sdavidch
8518255736Sdavidchnext_spqe:
8519255736Sdavidch        spqe_cnt++;
8520255736Sdavidch    } /* for */
8521255736Sdavidch
8522255736Sdavidch    mb();
8523255736Sdavidch    atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8524255736Sdavidch
8525255736Sdavidch    sc->eq_cons = sw_cons;
8526255736Sdavidch    sc->eq_prod = sw_prod;
8527255736Sdavidch
8528255736Sdavidch    /* make sure that above mem writes were issued towards the memory */
8529255736Sdavidch    wmb();
8530255736Sdavidch
8531255736Sdavidch    /* update producer */
8532255736Sdavidch    bxe_update_eq_prod(sc, sc->eq_prod);
8533255736Sdavidch}
8534255736Sdavidch
8535255736Sdavidchstatic void
8536255736Sdavidchbxe_handle_sp_tq(void *context,
8537255736Sdavidch                 int  pending)
8538255736Sdavidch{
8539255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
8540255736Sdavidch    uint16_t status;
8541255736Sdavidch
8542255736Sdavidch    BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8543255736Sdavidch
8544255736Sdavidch    /* what work needs to be performed? */
8545255736Sdavidch    status = bxe_update_dsb_idx(sc);
8546255736Sdavidch
8547255736Sdavidch    BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8548255736Sdavidch
8549255736Sdavidch    /* HW attentions */
8550255736Sdavidch    if (status & BXE_DEF_SB_ATT_IDX) {
8551255736Sdavidch        BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8552255736Sdavidch        bxe_attn_int(sc);
8553255736Sdavidch        status &= ~BXE_DEF_SB_ATT_IDX;
8554255736Sdavidch    }
8555255736Sdavidch
8556255736Sdavidch    /* SP events: STAT_QUERY and others */
8557255736Sdavidch    if (status & BXE_DEF_SB_IDX) {
8558255736Sdavidch        /* handle EQ completions */
8559255736Sdavidch        BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8560255736Sdavidch        bxe_eq_int(sc);
8561255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8562255736Sdavidch                   le16toh(sc->def_idx), IGU_INT_NOP, 1);
8563255736Sdavidch        status &= ~BXE_DEF_SB_IDX;
8564255736Sdavidch    }
8565255736Sdavidch
8566255736Sdavidch    /* if status is non zero then something went wrong */
8567255736Sdavidch    if (__predict_false(status)) {
8568255736Sdavidch        BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8569255736Sdavidch    }
8570255736Sdavidch
8571255736Sdavidch    /* ack status block only if something was actually handled */
8572255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8573255736Sdavidch               le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8574255736Sdavidch
8575255736Sdavidch    /*
8576255736Sdavidch     * Must be called after the EQ processing (since eq leads to sriov
8577255736Sdavidch     * ramrod completion flows).
8578255736Sdavidch     * This flow may have been scheduled by the arrival of a ramrod
8579255736Sdavidch     * completion, or by the sriov code rescheduling itself.
8580255736Sdavidch     */
8581255736Sdavidch    // XXX bxe_iov_sp_task(sc);
8582255736Sdavidch
8583255736Sdavidch}
8584255736Sdavidch
8585255736Sdavidchstatic void
8586255736Sdavidchbxe_handle_fp_tq(void *context,
8587255736Sdavidch                 int  pending)
8588255736Sdavidch{
8589255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8590255736Sdavidch    struct bxe_softc *sc = fp->sc;
8591255736Sdavidch    uint8_t more_tx = FALSE;
8592255736Sdavidch    uint8_t more_rx = FALSE;
8593255736Sdavidch
8594255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8595255736Sdavidch
8596255736Sdavidch    /* XXX
8597255736Sdavidch     * IFF_DRV_RUNNING state can't be checked here since we process
8598255736Sdavidch     * slowpath events on a client queue during setup. Instead
8599255736Sdavidch     * we need to add a "process/continue" flag here that the driver
8600255736Sdavidch     * can use to tell the task here not to do anything.
8601255736Sdavidch     */
8602255736Sdavidch#if 0
8603266979Smarcel    if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
8604255736Sdavidch        return;
8605255736Sdavidch    }
8606255736Sdavidch#endif
8607255736Sdavidch
8608255736Sdavidch    /* update the fastpath index */
8609255736Sdavidch    bxe_update_fp_sb_idx(fp);
8610255736Sdavidch
8611255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8612255736Sdavidch    /* fp->txdata[cos] */
8613255736Sdavidch    if (bxe_has_tx_work(fp)) {
8614255736Sdavidch        BXE_FP_TX_LOCK(fp);
8615255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8616255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8617255736Sdavidch    }
8618255736Sdavidch
8619255736Sdavidch    if (bxe_has_rx_work(fp)) {
8620255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8621255736Sdavidch    }
8622255736Sdavidch
8623255736Sdavidch    if (more_rx /*|| more_tx*/) {
8624255736Sdavidch        /* still more work to do */
8625296272Sjhb        taskqueue_enqueue(fp->tq, &fp->tq_task);
8626255736Sdavidch        return;
8627255736Sdavidch    }
8628255736Sdavidch
8629255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8630255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8631255736Sdavidch}
8632255736Sdavidch
8633255736Sdavidchstatic void
8634255736Sdavidchbxe_task_fp(struct bxe_fastpath *fp)
8635255736Sdavidch{
8636255736Sdavidch    struct bxe_softc *sc = fp->sc;
8637255736Sdavidch    uint8_t more_tx = FALSE;
8638255736Sdavidch    uint8_t more_rx = FALSE;
8639255736Sdavidch
8640255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8641255736Sdavidch
8642255736Sdavidch    /* update the fastpath index */
8643255736Sdavidch    bxe_update_fp_sb_idx(fp);
8644255736Sdavidch
8645255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8646255736Sdavidch    /* fp->txdata[cos] */
8647255736Sdavidch    if (bxe_has_tx_work(fp)) {
8648255736Sdavidch        BXE_FP_TX_LOCK(fp);
8649255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8650255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8651255736Sdavidch    }
8652255736Sdavidch
8653255736Sdavidch    if (bxe_has_rx_work(fp)) {
8654255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8655255736Sdavidch    }
8656255736Sdavidch
8657255736Sdavidch    if (more_rx /*|| more_tx*/) {
8658255736Sdavidch        /* still more work to do, bail out if this ISR and process later */
8659296272Sjhb        taskqueue_enqueue(fp->tq, &fp->tq_task);
8660255736Sdavidch        return;
8661255736Sdavidch    }
8662255736Sdavidch
8663255736Sdavidch    /*
8664255736Sdavidch     * Here we write the fastpath index taken before doing any tx or rx work.
8665255736Sdavidch     * It is very well possible other hw events occurred up to this point and
8666255736Sdavidch     * they were actually processed accordingly above. Since we're going to
8667255736Sdavidch     * write an older fastpath index, an interrupt is coming which we might
8668255736Sdavidch     * not do any work in.
8669255736Sdavidch     */
8670255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8671255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8672255736Sdavidch}
8673255736Sdavidch
8674255736Sdavidch/*
8675255736Sdavidch * Legacy interrupt entry point.
8676255736Sdavidch *
8677255736Sdavidch * Verifies that the controller generated the interrupt and
8678255736Sdavidch * then calls a separate routine to handle the various
8679255736Sdavidch * interrupt causes: link, RX, and TX.
8680255736Sdavidch */
8681255736Sdavidchstatic void
8682255736Sdavidchbxe_intr_legacy(void *xsc)
8683255736Sdavidch{
8684255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8685255736Sdavidch    struct bxe_fastpath *fp;
8686255736Sdavidch    uint16_t status, mask;
8687255736Sdavidch    int i;
8688255736Sdavidch
8689255736Sdavidch    BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8690255736Sdavidch
8691255736Sdavidch    /*
8692255736Sdavidch     * 0 for ustorm, 1 for cstorm
8693255736Sdavidch     * the bits returned from ack_int() are 0-15
8694255736Sdavidch     * bit 0 = attention status block
8695255736Sdavidch     * bit 1 = fast path status block
8696255736Sdavidch     * a mask of 0x2 or more = tx/rx event
8697255736Sdavidch     * a mask of 1 = slow path event
8698255736Sdavidch     */
8699255736Sdavidch
8700255736Sdavidch    status = bxe_ack_int(sc);
8701255736Sdavidch
8702255736Sdavidch    /* the interrupt is not for us */
8703255736Sdavidch    if (__predict_false(status == 0)) {
8704255736Sdavidch        BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8705255736Sdavidch        return;
8706255736Sdavidch    }
8707255736Sdavidch
8708255736Sdavidch    BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8709255736Sdavidch
8710255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
8711255736Sdavidch        fp = &sc->fp[i];
8712255736Sdavidch        mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8713255736Sdavidch        if (status & mask) {
8714255736Sdavidch            /* acknowledge and disable further fastpath interrupts */
8715255736Sdavidch            bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8716255736Sdavidch            bxe_task_fp(fp);
8717255736Sdavidch            status &= ~mask;
8718255736Sdavidch        }
8719255736Sdavidch    }
8720255736Sdavidch
8721255736Sdavidch    if (__predict_false(status & 0x1)) {
8722255736Sdavidch        /* acknowledge and disable further slowpath interrupts */
8723255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8724255736Sdavidch
8725255736Sdavidch        /* schedule slowpath handler */
8726296272Sjhb        taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8727255736Sdavidch
8728255736Sdavidch        status &= ~0x1;
8729255736Sdavidch    }
8730255736Sdavidch
8731255736Sdavidch    if (__predict_false(status)) {
8732255736Sdavidch        BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8733255736Sdavidch    }
8734255736Sdavidch}
8735255736Sdavidch
8736255736Sdavidch/* slowpath interrupt entry point */
8737255736Sdavidchstatic void
8738255736Sdavidchbxe_intr_sp(void *xsc)
8739255736Sdavidch{
8740255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8741255736Sdavidch
8742255736Sdavidch    BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8743255736Sdavidch
8744255736Sdavidch    /* acknowledge and disable further slowpath interrupts */
8745255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8746255736Sdavidch
8747255736Sdavidch    /* schedule slowpath handler */
8748296272Sjhb    taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8749255736Sdavidch}
8750255736Sdavidch
8751255736Sdavidch/* fastpath interrupt entry point */
8752255736Sdavidchstatic void
8753255736Sdavidchbxe_intr_fp(void *xfp)
8754255736Sdavidch{
8755255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8756255736Sdavidch    struct bxe_softc *sc = fp->sc;
8757255736Sdavidch
8758255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8759255736Sdavidch
8760255736Sdavidch    BLOGD(sc, DBG_INTR,
8761255736Sdavidch          "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8762255736Sdavidch          curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8763255736Sdavidch
8764255736Sdavidch    /* acknowledge and disable further fastpath interrupts */
8765255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8766255736Sdavidch
8767255736Sdavidch    bxe_task_fp(fp);
8768255736Sdavidch}
8769255736Sdavidch
8770255736Sdavidch/* Release all interrupts allocated by the driver. */
8771255736Sdavidchstatic void
8772255736Sdavidchbxe_interrupt_free(struct bxe_softc *sc)
8773255736Sdavidch{
8774255736Sdavidch    int i;
8775255736Sdavidch
8776255736Sdavidch    switch (sc->interrupt_mode) {
8777255736Sdavidch    case INTR_MODE_INTX:
8778255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8779255736Sdavidch        if (sc->intr[0].resource != NULL) {
8780255736Sdavidch            bus_release_resource(sc->dev,
8781255736Sdavidch                                 SYS_RES_IRQ,
8782255736Sdavidch                                 sc->intr[0].rid,
8783255736Sdavidch                                 sc->intr[0].resource);
8784255736Sdavidch        }
8785255736Sdavidch        break;
8786255736Sdavidch    case INTR_MODE_MSI:
8787255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8788255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
8789255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8790255736Sdavidch                bus_release_resource(sc->dev,
8791255736Sdavidch                                     SYS_RES_IRQ,
8792255736Sdavidch                                     sc->intr[i].rid,
8793255736Sdavidch                                     sc->intr[i].resource);
8794255736Sdavidch            }
8795255736Sdavidch        }
8796255736Sdavidch        pci_release_msi(sc->dev);
8797255736Sdavidch        break;
8798255736Sdavidch    case INTR_MODE_MSIX:
8799255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8800255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8801255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8802255736Sdavidch                bus_release_resource(sc->dev,
8803255736Sdavidch                                     SYS_RES_IRQ,
8804255736Sdavidch                                     sc->intr[i].rid,
8805255736Sdavidch                                     sc->intr[i].resource);
8806255736Sdavidch            }
8807255736Sdavidch        }
8808255736Sdavidch        pci_release_msi(sc->dev);
8809255736Sdavidch        break;
8810255736Sdavidch    default:
8811255736Sdavidch        /* nothing to do as initial allocation failed */
8812255736Sdavidch        break;
8813255736Sdavidch    }
8814255736Sdavidch}
8815255736Sdavidch
8816255736Sdavidch/*
8817255736Sdavidch * This function determines and allocates the appropriate
8818255736Sdavidch * interrupt based on system capabilites and user request.
8819255736Sdavidch *
8820255736Sdavidch * The user may force a particular interrupt mode, specify
8821255736Sdavidch * the number of receive queues, specify the method for
8822255736Sdavidch * distribuitng received frames to receive queues, or use
8823255736Sdavidch * the default settings which will automatically select the
8824255736Sdavidch * best supported combination.  In addition, the OS may or
8825255736Sdavidch * may not support certain combinations of these settings.
8826255736Sdavidch * This routine attempts to reconcile the settings requested
8827255736Sdavidch * by the user with the capabilites available from the system
8828255736Sdavidch * to select the optimal combination of features.
8829255736Sdavidch *
8830255736Sdavidch * Returns:
8831255736Sdavidch *   0 = Success, !0 = Failure.
8832255736Sdavidch */
8833255736Sdavidchstatic int
8834255736Sdavidchbxe_interrupt_alloc(struct bxe_softc *sc)
8835255736Sdavidch{
8836255736Sdavidch    int msix_count = 0;
8837255736Sdavidch    int msi_count = 0;
8838255736Sdavidch    int num_requested = 0;
8839255736Sdavidch    int num_allocated = 0;
8840255736Sdavidch    int rid, i, j;
8841255736Sdavidch    int rc;
8842255736Sdavidch
8843255736Sdavidch    /* get the number of available MSI/MSI-X interrupts from the OS */
8844255736Sdavidch    if (sc->interrupt_mode > 0) {
8845255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8846255736Sdavidch            msix_count = pci_msix_count(sc->dev);
8847255736Sdavidch        }
8848255736Sdavidch
8849255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
8850255736Sdavidch            msi_count = pci_msi_count(sc->dev);
8851255736Sdavidch        }
8852255736Sdavidch
8853255736Sdavidch        BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
8854255736Sdavidch              msi_count, msix_count);
8855255736Sdavidch    }
8856255736Sdavidch
8857255736Sdavidch    do { /* try allocating MSI-X interrupt resources (at least 2) */
8858255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
8859255736Sdavidch            break;
8860255736Sdavidch        }
8861255736Sdavidch
8862255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
8863255736Sdavidch            (msix_count < 2)) {
8864255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8865255736Sdavidch            break;
8866255736Sdavidch        }
8867255736Sdavidch
8868255736Sdavidch        /* ask for the necessary number of MSI-X vectors */
8869255736Sdavidch        num_requested = min((sc->num_queues + 1), msix_count);
8870255736Sdavidch
8871255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
8872255736Sdavidch
8873255736Sdavidch        num_allocated = num_requested;
8874255736Sdavidch        if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
8875255736Sdavidch            BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
8876255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8877255736Sdavidch            break;
8878255736Sdavidch        }
8879255736Sdavidch
8880255736Sdavidch        if (num_allocated < 2) { /* possible? */
8881255736Sdavidch            BLOGE(sc, "MSI-X allocation less than 2!\n");
8882255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8883255736Sdavidch            pci_release_msi(sc->dev);
8884255736Sdavidch            break;
8885255736Sdavidch        }
8886255736Sdavidch
8887255736Sdavidch        BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
8888255736Sdavidch              num_requested, num_allocated);
8889255736Sdavidch
8890255736Sdavidch        /* best effort so use the number of vectors allocated to us */
8891255736Sdavidch        sc->intr_count = num_allocated;
8892255736Sdavidch        sc->num_queues = num_allocated - 1;
8893255736Sdavidch
8894255736Sdavidch        rid = 1; /* initial resource identifier */
8895255736Sdavidch
8896255736Sdavidch        /* allocate the MSI-X vectors */
8897255736Sdavidch        for (i = 0; i < num_allocated; i++) {
8898255736Sdavidch            sc->intr[i].rid = (rid + i);
8899255736Sdavidch
8900255736Sdavidch            if ((sc->intr[i].resource =
8901255736Sdavidch                 bus_alloc_resource_any(sc->dev,
8902255736Sdavidch                                        SYS_RES_IRQ,
8903255736Sdavidch                                        &sc->intr[i].rid,
8904255736Sdavidch                                        RF_ACTIVE)) == NULL) {
8905255736Sdavidch                BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
8906255736Sdavidch                      i, (rid + i));
8907255736Sdavidch
8908255736Sdavidch                for (j = (i - 1); j >= 0; j--) {
8909255736Sdavidch                    bus_release_resource(sc->dev,
8910255736Sdavidch                                         SYS_RES_IRQ,
8911255736Sdavidch                                         sc->intr[j].rid,
8912255736Sdavidch                                         sc->intr[j].resource);
8913255736Sdavidch                }
8914255736Sdavidch
8915255736Sdavidch                sc->intr_count = 0;
8916255736Sdavidch                sc->num_queues = 0;
8917255736Sdavidch                sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8918255736Sdavidch                pci_release_msi(sc->dev);
8919255736Sdavidch                break;
8920255736Sdavidch            }
8921255736Sdavidch
8922255736Sdavidch            BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
8923255736Sdavidch        }
8924255736Sdavidch    } while (0);
8925255736Sdavidch
8926255736Sdavidch    do { /* try allocating MSI vector resources (at least 2) */
8927255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSI) {
8928255736Sdavidch            break;
8929255736Sdavidch        }
8930255736Sdavidch
8931255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
8932262999Sedavis            (msi_count < 1)) {
8933255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8934255736Sdavidch            break;
8935255736Sdavidch        }
8936255736Sdavidch
8937262999Sedavis        /* ask for a single MSI vector */
8938262999Sedavis        num_requested = 1;
8939255736Sdavidch
8940255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
8941255736Sdavidch
8942255736Sdavidch        num_allocated = num_requested;
8943255736Sdavidch        if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
8944255736Sdavidch            BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
8945255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8946255736Sdavidch            break;
8947255736Sdavidch        }
8948255736Sdavidch
8949262999Sedavis        if (num_allocated != 1) { /* possible? */
8950262999Sedavis            BLOGE(sc, "MSI allocation is not 1!\n");
8951255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8952255736Sdavidch            pci_release_msi(sc->dev);
8953255736Sdavidch            break;
8954255736Sdavidch        }
8955255736Sdavidch
8956255736Sdavidch        BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
8957255736Sdavidch              num_requested, num_allocated);
8958255736Sdavidch
8959255736Sdavidch        /* best effort so use the number of vectors allocated to us */
8960255736Sdavidch        sc->intr_count = num_allocated;
8961262999Sedavis        sc->num_queues = num_allocated;
8962255736Sdavidch
8963255736Sdavidch        rid = 1; /* initial resource identifier */
8964255736Sdavidch
8965262999Sedavis        sc->intr[0].rid = rid;
8966255736Sdavidch
8967262999Sedavis        if ((sc->intr[0].resource =
8968262999Sedavis             bus_alloc_resource_any(sc->dev,
8969262999Sedavis                                    SYS_RES_IRQ,
8970262999Sedavis                                    &sc->intr[0].rid,
8971262999Sedavis                                    RF_ACTIVE)) == NULL) {
8972262999Sedavis            BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
8973262999Sedavis            sc->intr_count = 0;
8974262999Sedavis            sc->num_queues = 0;
8975262999Sedavis            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8976262999Sedavis            pci_release_msi(sc->dev);
8977262999Sedavis            break;
8978262999Sedavis        }
8979255736Sdavidch
8980262999Sedavis        BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
8981255736Sdavidch    } while (0);
8982255736Sdavidch
8983255736Sdavidch    do { /* try allocating INTx vector resources */
8984255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_INTX) {
8985255736Sdavidch            break;
8986255736Sdavidch        }
8987255736Sdavidch
8988255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
8989255736Sdavidch
8990255736Sdavidch        /* only one vector for INTx */
8991255736Sdavidch        sc->intr_count = 1;
8992255736Sdavidch        sc->num_queues = 1;
8993255736Sdavidch
8994255736Sdavidch        rid = 0; /* initial resource identifier */
8995255736Sdavidch
8996255736Sdavidch        sc->intr[0].rid = rid;
8997255736Sdavidch
8998255736Sdavidch        if ((sc->intr[0].resource =
8999255736Sdavidch             bus_alloc_resource_any(sc->dev,
9000255736Sdavidch                                    SYS_RES_IRQ,
9001255736Sdavidch                                    &sc->intr[0].rid,
9002255736Sdavidch                                    (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9003255736Sdavidch            BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9004255736Sdavidch            sc->intr_count = 0;
9005255736Sdavidch            sc->num_queues = 0;
9006255736Sdavidch            sc->interrupt_mode = -1; /* Failed! */
9007255736Sdavidch            break;
9008255736Sdavidch        }
9009255736Sdavidch
9010255736Sdavidch        BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9011255736Sdavidch    } while (0);
9012255736Sdavidch
9013255736Sdavidch    if (sc->interrupt_mode == -1) {
9014255736Sdavidch        BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9015255736Sdavidch        rc = 1;
9016255736Sdavidch    } else {
9017255736Sdavidch        BLOGD(sc, DBG_LOAD,
9018255736Sdavidch              "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9019255736Sdavidch              sc->interrupt_mode, sc->num_queues);
9020255736Sdavidch        rc = 0;
9021255736Sdavidch    }
9022255736Sdavidch
9023255736Sdavidch    return (rc);
9024255736Sdavidch}
9025255736Sdavidch
9026255736Sdavidchstatic void
9027255736Sdavidchbxe_interrupt_detach(struct bxe_softc *sc)
9028255736Sdavidch{
9029255736Sdavidch    struct bxe_fastpath *fp;
9030255736Sdavidch    int i;
9031255736Sdavidch
9032255736Sdavidch    /* release interrupt resources */
9033255736Sdavidch    for (i = 0; i < sc->intr_count; i++) {
9034255736Sdavidch        if (sc->intr[i].resource && sc->intr[i].tag) {
9035255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9036255736Sdavidch            bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9037255736Sdavidch        }
9038255736Sdavidch    }
9039255736Sdavidch
9040255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9041255736Sdavidch        fp = &sc->fp[i];
9042255736Sdavidch        if (fp->tq) {
9043255736Sdavidch            taskqueue_drain(fp->tq, &fp->tq_task);
9044255736Sdavidch            taskqueue_free(fp->tq);
9045255736Sdavidch            fp->tq = NULL;
9046255736Sdavidch        }
9047255736Sdavidch    }
9048255736Sdavidch
9049255736Sdavidch
9050255736Sdavidch    if (sc->sp_tq) {
9051255736Sdavidch        taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9052255736Sdavidch        taskqueue_free(sc->sp_tq);
9053255736Sdavidch        sc->sp_tq = NULL;
9054255736Sdavidch    }
9055255736Sdavidch}
9056255736Sdavidch
9057255736Sdavidch/*
9058255736Sdavidch * Enables interrupts and attach to the ISR.
9059255736Sdavidch *
9060255736Sdavidch * When using multiple MSI/MSI-X vectors the first vector
9061255736Sdavidch * is used for slowpath operations while all remaining
9062255736Sdavidch * vectors are used for fastpath operations.  If only a
9063255736Sdavidch * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9064255736Sdavidch * ISR must look for both slowpath and fastpath completions.
9065255736Sdavidch */
9066255736Sdavidchstatic int
9067255736Sdavidchbxe_interrupt_attach(struct bxe_softc *sc)
9068255736Sdavidch{
9069255736Sdavidch    struct bxe_fastpath *fp;
9070255736Sdavidch    int rc = 0;
9071255736Sdavidch    int i;
9072255736Sdavidch
9073255736Sdavidch    snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9074255736Sdavidch             "bxe%d_sp_tq", sc->unit);
9075255736Sdavidch    TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9076255736Sdavidch    sc->sp_tq = taskqueue_create_fast(sc->sp_tq_name, M_NOWAIT,
9077255736Sdavidch                                      taskqueue_thread_enqueue,
9078255736Sdavidch                                      &sc->sp_tq);
9079255736Sdavidch    taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9080255736Sdavidch                            "%s", sc->sp_tq_name);
9081255736Sdavidch
9082255736Sdavidch
9083255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9084255736Sdavidch        fp = &sc->fp[i];
9085255736Sdavidch        snprintf(fp->tq_name, sizeof(fp->tq_name),
9086255736Sdavidch                 "bxe%d_fp%d_tq", sc->unit, i);
9087255736Sdavidch        TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9088255736Sdavidch        fp->tq = taskqueue_create_fast(fp->tq_name, M_NOWAIT,
9089255736Sdavidch                                       taskqueue_thread_enqueue,
9090255736Sdavidch                                       &fp->tq);
9091255736Sdavidch        taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9092255736Sdavidch                                "%s", fp->tq_name);
9093255736Sdavidch    }
9094255736Sdavidch
9095255736Sdavidch    /* setup interrupt handlers */
9096255736Sdavidch    if (sc->interrupt_mode == INTR_MODE_MSIX) {
9097255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9098255736Sdavidch
9099255736Sdavidch        /*
9100255736Sdavidch         * Setup the interrupt handler. Note that we pass the driver instance
9101255736Sdavidch         * to the interrupt handler for the slowpath.
9102255736Sdavidch         */
9103255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9104255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9105255736Sdavidch                                 NULL, bxe_intr_sp, sc,
9106255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9107255736Sdavidch            BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9108255736Sdavidch            goto bxe_interrupt_attach_exit;
9109255736Sdavidch        }
9110255736Sdavidch
9111255736Sdavidch        bus_describe_intr(sc->dev, sc->intr[0].resource,
9112255736Sdavidch                          sc->intr[0].tag, "sp");
9113255736Sdavidch
9114255736Sdavidch        /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9115255736Sdavidch
9116255736Sdavidch        /* initialize the fastpath vectors (note the first was used for sp) */
9117255736Sdavidch        for (i = 0; i < sc->num_queues; i++) {
9118255736Sdavidch            fp = &sc->fp[i];
9119255736Sdavidch            BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9120255736Sdavidch
9121255736Sdavidch            /*
9122255736Sdavidch             * Setup the interrupt handler. Note that we pass the
9123255736Sdavidch             * fastpath context to the interrupt handler in this
9124255736Sdavidch             * case.
9125255736Sdavidch             */
9126255736Sdavidch            if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9127255736Sdavidch                                     (INTR_TYPE_NET | INTR_MPSAFE),
9128255736Sdavidch                                     NULL, bxe_intr_fp, fp,
9129255736Sdavidch                                     &sc->intr[i + 1].tag)) != 0) {
9130255736Sdavidch                BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9131255736Sdavidch                      (i + 1), rc);
9132255736Sdavidch                goto bxe_interrupt_attach_exit;
9133255736Sdavidch            }
9134255736Sdavidch
9135255736Sdavidch            bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9136255736Sdavidch                              sc->intr[i + 1].tag, "fp%02d", i);
9137255736Sdavidch
9138255736Sdavidch            /* bind the fastpath instance to a cpu */
9139255736Sdavidch            if (sc->num_queues > 1) {
9140255736Sdavidch                bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9141255736Sdavidch            }
9142255736Sdavidch
9143255736Sdavidch            fp->state = BXE_FP_STATE_IRQ;
9144255736Sdavidch        }
9145255736Sdavidch    } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9146262999Sedavis        BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9147255736Sdavidch
9148255736Sdavidch        /*
9149262999Sedavis         * Setup the interrupt handler. Note that we pass the
9150262999Sedavis         * driver instance to the interrupt handler which
9151262999Sedavis         * will handle both the slowpath and fastpath.
9152255736Sdavidch         */
9153255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9154255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9155262999Sedavis                                 NULL, bxe_intr_legacy, sc,
9156255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9157255736Sdavidch            BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9158255736Sdavidch            goto bxe_interrupt_attach_exit;
9159255736Sdavidch        }
9160255736Sdavidch
9161255736Sdavidch    } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9162255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9163255736Sdavidch
9164255736Sdavidch        /*
9165255736Sdavidch         * Setup the interrupt handler. Note that we pass the
9166255736Sdavidch         * driver instance to the interrupt handler which
9167255736Sdavidch         * will handle both the slowpath and fastpath.
9168255736Sdavidch         */
9169255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9170255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9171255736Sdavidch                                 NULL, bxe_intr_legacy, sc,
9172255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9173255736Sdavidch            BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9174255736Sdavidch            goto bxe_interrupt_attach_exit;
9175255736Sdavidch        }
9176255736Sdavidch    }
9177255736Sdavidch
9178255736Sdavidchbxe_interrupt_attach_exit:
9179255736Sdavidch
9180255736Sdavidch    return (rc);
9181255736Sdavidch}
9182255736Sdavidch
9183255736Sdavidchstatic int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9184255736Sdavidchstatic int  bxe_init_hw_common(struct bxe_softc *sc);
9185255736Sdavidchstatic int  bxe_init_hw_port(struct bxe_softc *sc);
9186255736Sdavidchstatic int  bxe_init_hw_func(struct bxe_softc *sc);
9187255736Sdavidchstatic void bxe_reset_common(struct bxe_softc *sc);
9188255736Sdavidchstatic void bxe_reset_port(struct bxe_softc *sc);
9189255736Sdavidchstatic void bxe_reset_func(struct bxe_softc *sc);
9190255736Sdavidchstatic int  bxe_gunzip_init(struct bxe_softc *sc);
9191255736Sdavidchstatic void bxe_gunzip_end(struct bxe_softc *sc);
9192255736Sdavidchstatic int  bxe_init_firmware(struct bxe_softc *sc);
9193255736Sdavidchstatic void bxe_release_firmware(struct bxe_softc *sc);
9194255736Sdavidch
9195255736Sdavidchstatic struct
9196255736Sdavidchecore_func_sp_drv_ops bxe_func_sp_drv = {
9197255736Sdavidch    .init_hw_cmn_chip = bxe_init_hw_common_chip,
9198255736Sdavidch    .init_hw_cmn      = bxe_init_hw_common,
9199255736Sdavidch    .init_hw_port     = bxe_init_hw_port,
9200255736Sdavidch    .init_hw_func     = bxe_init_hw_func,
9201255736Sdavidch
9202255736Sdavidch    .reset_hw_cmn     = bxe_reset_common,
9203255736Sdavidch    .reset_hw_port    = bxe_reset_port,
9204255736Sdavidch    .reset_hw_func    = bxe_reset_func,
9205255736Sdavidch
9206255736Sdavidch    .gunzip_init      = bxe_gunzip_init,
9207255736Sdavidch    .gunzip_end       = bxe_gunzip_end,
9208255736Sdavidch
9209255736Sdavidch    .init_fw          = bxe_init_firmware,
9210255736Sdavidch    .release_fw       = bxe_release_firmware,
9211255736Sdavidch};
9212255736Sdavidch
9213255736Sdavidchstatic void
9214255736Sdavidchbxe_init_func_obj(struct bxe_softc *sc)
9215255736Sdavidch{
9216255736Sdavidch    sc->dmae_ready = 0;
9217255736Sdavidch
9218255736Sdavidch    ecore_init_func_obj(sc,
9219255736Sdavidch                        &sc->func_obj,
9220255736Sdavidch                        BXE_SP(sc, func_rdata),
9221255736Sdavidch                        BXE_SP_MAPPING(sc, func_rdata),
9222255736Sdavidch                        BXE_SP(sc, func_afex_rdata),
9223255736Sdavidch                        BXE_SP_MAPPING(sc, func_afex_rdata),
9224255736Sdavidch                        &bxe_func_sp_drv);
9225255736Sdavidch}
9226255736Sdavidch
9227255736Sdavidchstatic int
9228255736Sdavidchbxe_init_hw(struct bxe_softc *sc,
9229255736Sdavidch            uint32_t         load_code)
9230255736Sdavidch{
9231255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
9232255736Sdavidch    int rc;
9233255736Sdavidch
9234255736Sdavidch    /* prepare the parameters for function state transitions */
9235255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9236255736Sdavidch
9237255736Sdavidch    func_params.f_obj = &sc->func_obj;
9238255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_INIT;
9239255736Sdavidch
9240255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
9241255736Sdavidch
9242255736Sdavidch    /*
9243255736Sdavidch     * Via a plethora of function pointers, we will eventually reach
9244255736Sdavidch     * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9245255736Sdavidch     */
9246255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
9247255736Sdavidch
9248255736Sdavidch    return (rc);
9249255736Sdavidch}
9250255736Sdavidch
9251255736Sdavidchstatic void
9252255736Sdavidchbxe_fill(struct bxe_softc *sc,
9253255736Sdavidch         uint32_t         addr,
9254255736Sdavidch         int              fill,
9255255736Sdavidch         uint32_t         len)
9256255736Sdavidch{
9257255736Sdavidch    uint32_t i;
9258255736Sdavidch
9259255736Sdavidch    if (!(len % 4) && !(addr % 4)) {
9260255736Sdavidch        for (i = 0; i < len; i += 4) {
9261255736Sdavidch            REG_WR(sc, (addr + i), fill);
9262255736Sdavidch        }
9263255736Sdavidch    } else {
9264255736Sdavidch        for (i = 0; i < len; i++) {
9265255736Sdavidch            REG_WR8(sc, (addr + i), fill);
9266255736Sdavidch        }
9267255736Sdavidch    }
9268255736Sdavidch}
9269255736Sdavidch
9270255736Sdavidch/* writes FP SP data to FW - data_size in dwords */
9271255736Sdavidchstatic void
9272255736Sdavidchbxe_wr_fp_sb_data(struct bxe_softc *sc,
9273255736Sdavidch                  int              fw_sb_id,
9274255736Sdavidch                  uint32_t         *sb_data_p,
9275255736Sdavidch                  uint32_t         data_size)
9276255736Sdavidch{
9277255736Sdavidch    int index;
9278255736Sdavidch
9279255736Sdavidch    for (index = 0; index < data_size; index++) {
9280255736Sdavidch        REG_WR(sc,
9281255736Sdavidch               (BAR_CSTRORM_INTMEM +
9282255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9283255736Sdavidch                (sizeof(uint32_t) * index)),
9284255736Sdavidch               *(sb_data_p + index));
9285255736Sdavidch    }
9286255736Sdavidch}
9287255736Sdavidch
9288255736Sdavidchstatic void
9289255736Sdavidchbxe_zero_fp_sb(struct bxe_softc *sc,
9290255736Sdavidch               int              fw_sb_id)
9291255736Sdavidch{
9292255736Sdavidch    struct hc_status_block_data_e2 sb_data_e2;
9293255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9294255736Sdavidch    uint32_t *sb_data_p;
9295255736Sdavidch    uint32_t data_size = 0;
9296255736Sdavidch
9297255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9298255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9299255736Sdavidch        sb_data_e2.common.state = SB_DISABLED;
9300255736Sdavidch        sb_data_e2.common.p_func.vf_valid = FALSE;
9301255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9302255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9303255736Sdavidch                     sizeof(uint32_t));
9304255736Sdavidch    } else {
9305255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9306255736Sdavidch        sb_data_e1x.common.state = SB_DISABLED;
9307255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9308255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9309255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9310255736Sdavidch                     sizeof(uint32_t));
9311255736Sdavidch    }
9312255736Sdavidch
9313255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9314255736Sdavidch
9315255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9316255736Sdavidch             0, CSTORM_STATUS_BLOCK_SIZE);
9317255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9318255736Sdavidch             0, CSTORM_SYNC_BLOCK_SIZE);
9319255736Sdavidch}
9320255736Sdavidch
9321255736Sdavidchstatic void
9322255736Sdavidchbxe_wr_sp_sb_data(struct bxe_softc               *sc,
9323255736Sdavidch                  struct hc_sp_status_block_data *sp_sb_data)
9324255736Sdavidch{
9325255736Sdavidch    int i;
9326255736Sdavidch
9327255736Sdavidch    for (i = 0;
9328255736Sdavidch         i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9329255736Sdavidch         i++) {
9330255736Sdavidch        REG_WR(sc,
9331255736Sdavidch               (BAR_CSTRORM_INTMEM +
9332255736Sdavidch                CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9333255736Sdavidch                (i * sizeof(uint32_t))),
9334255736Sdavidch               *((uint32_t *)sp_sb_data + i));
9335255736Sdavidch    }
9336255736Sdavidch}
9337255736Sdavidch
9338255736Sdavidchstatic void
9339255736Sdavidchbxe_zero_sp_sb(struct bxe_softc *sc)
9340255736Sdavidch{
9341255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9342255736Sdavidch
9343255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9344255736Sdavidch
9345255736Sdavidch    sp_sb_data.state           = SB_DISABLED;
9346255736Sdavidch    sp_sb_data.p_func.vf_valid = FALSE;
9347255736Sdavidch
9348255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9349255736Sdavidch
9350255736Sdavidch    bxe_fill(sc,
9351255736Sdavidch             (BAR_CSTRORM_INTMEM +
9352255736Sdavidch              CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9353255736Sdavidch              0, CSTORM_SP_STATUS_BLOCK_SIZE);
9354255736Sdavidch    bxe_fill(sc,
9355255736Sdavidch             (BAR_CSTRORM_INTMEM +
9356255736Sdavidch              CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9357255736Sdavidch              0, CSTORM_SP_SYNC_BLOCK_SIZE);
9358255736Sdavidch}
9359255736Sdavidch
9360255736Sdavidchstatic void
9361255736Sdavidchbxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9362255736Sdavidch                             int                       igu_sb_id,
9363255736Sdavidch                             int                       igu_seg_id)
9364255736Sdavidch{
9365255736Sdavidch    hc_sm->igu_sb_id      = igu_sb_id;
9366255736Sdavidch    hc_sm->igu_seg_id     = igu_seg_id;
9367255736Sdavidch    hc_sm->timer_value    = 0xFF;
9368255736Sdavidch    hc_sm->time_to_expire = 0xFFFFFFFF;
9369255736Sdavidch}
9370255736Sdavidch
9371255736Sdavidchstatic void
9372255736Sdavidchbxe_map_sb_state_machines(struct hc_index_data *index_data)
9373255736Sdavidch{
9374255736Sdavidch    /* zero out state machine indices */
9375255736Sdavidch
9376255736Sdavidch    /* rx indices */
9377255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9378255736Sdavidch
9379255736Sdavidch    /* tx indices */
9380255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9381255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9382255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9383255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9384255736Sdavidch
9385255736Sdavidch    /* map indices */
9386255736Sdavidch
9387255736Sdavidch    /* rx indices */
9388255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9389255736Sdavidch        (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9390255736Sdavidch
9391255736Sdavidch    /* tx indices */
9392255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9393255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9394255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9395255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9396255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9397255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9398255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9399255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9400255736Sdavidch}
9401255736Sdavidch
9402255736Sdavidchstatic void
9403255736Sdavidchbxe_init_sb(struct bxe_softc *sc,
9404255736Sdavidch            bus_addr_t       busaddr,
9405255736Sdavidch            int              vfid,
9406255736Sdavidch            uint8_t          vf_valid,
9407255736Sdavidch            int              fw_sb_id,
9408255736Sdavidch            int              igu_sb_id)
9409255736Sdavidch{
9410255736Sdavidch    struct hc_status_block_data_e2  sb_data_e2;
9411255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9412255736Sdavidch    struct hc_status_block_sm       *hc_sm_p;
9413255736Sdavidch    uint32_t *sb_data_p;
9414255736Sdavidch    int igu_seg_id;
9415255736Sdavidch    int data_size;
9416255736Sdavidch
9417255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9418255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_NORM;
9419255736Sdavidch    } else {
9420255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_NORM;
9421255736Sdavidch    }
9422255736Sdavidch
9423255736Sdavidch    bxe_zero_fp_sb(sc, fw_sb_id);
9424255736Sdavidch
9425255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9426255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9427255736Sdavidch        sb_data_e2.common.state = SB_ENABLED;
9428255736Sdavidch        sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9429255736Sdavidch        sb_data_e2.common.p_func.vf_id = vfid;
9430255736Sdavidch        sb_data_e2.common.p_func.vf_valid = vf_valid;
9431255736Sdavidch        sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9432255736Sdavidch        sb_data_e2.common.same_igu_sb_1b = TRUE;
9433255736Sdavidch        sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9434255736Sdavidch        sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9435255736Sdavidch        hc_sm_p = sb_data_e2.common.state_machine;
9436255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9437255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9438255736Sdavidch                     sizeof(uint32_t));
9439255736Sdavidch        bxe_map_sb_state_machines(sb_data_e2.index_data);
9440255736Sdavidch    } else {
9441255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9442255736Sdavidch        sb_data_e1x.common.state = SB_ENABLED;
9443255736Sdavidch        sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9444255736Sdavidch        sb_data_e1x.common.p_func.vf_id = 0xff;
9445255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9446255736Sdavidch        sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9447255736Sdavidch        sb_data_e1x.common.same_igu_sb_1b = TRUE;
9448255736Sdavidch        sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9449255736Sdavidch        sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9450255736Sdavidch        hc_sm_p = sb_data_e1x.common.state_machine;
9451255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9452255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9453255736Sdavidch                     sizeof(uint32_t));
9454255736Sdavidch        bxe_map_sb_state_machines(sb_data_e1x.index_data);
9455255736Sdavidch    }
9456255736Sdavidch
9457255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9458255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9459255736Sdavidch
9460255736Sdavidch    BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9461255736Sdavidch
9462255736Sdavidch    /* write indices to HW - PCI guarantees endianity of regpairs */
9463255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9464255736Sdavidch}
9465255736Sdavidch
9466255736Sdavidchstatic inline uint8_t
9467255736Sdavidchbxe_fp_qzone_id(struct bxe_fastpath *fp)
9468255736Sdavidch{
9469255736Sdavidch    if (CHIP_IS_E1x(fp->sc)) {
9470255736Sdavidch        return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9471255736Sdavidch    } else {
9472255736Sdavidch        return (fp->cl_id);
9473255736Sdavidch    }
9474255736Sdavidch}
9475255736Sdavidch
9476255736Sdavidchstatic inline uint32_t
9477255736Sdavidchbxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9478255736Sdavidch                           struct bxe_fastpath *fp)
9479255736Sdavidch{
9480255736Sdavidch    uint32_t offset = BAR_USTRORM_INTMEM;
9481255736Sdavidch
9482255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9483255736Sdavidch        offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9484255736Sdavidch    } else {
9485255736Sdavidch        offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9486255736Sdavidch    }
9487255736Sdavidch
9488255736Sdavidch    return (offset);
9489255736Sdavidch}
9490255736Sdavidch
9491255736Sdavidchstatic void
9492255736Sdavidchbxe_init_eth_fp(struct bxe_softc *sc,
9493255736Sdavidch                int              idx)
9494255736Sdavidch{
9495255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[idx];
9496255736Sdavidch    uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9497255736Sdavidch    unsigned long q_type = 0;
9498255736Sdavidch    int cos;
9499255736Sdavidch
9500255736Sdavidch    fp->sc    = sc;
9501255736Sdavidch    fp->index = idx;
9502255736Sdavidch
9503255736Sdavidch    fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9504255736Sdavidch    fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9505255736Sdavidch
9506255736Sdavidch    fp->cl_id = (CHIP_IS_E1x(sc)) ?
9507255736Sdavidch                    (SC_L_ID(sc) + idx) :
9508255736Sdavidch                    /* want client ID same as IGU SB ID for non-E1 */
9509255736Sdavidch                    fp->igu_sb_id;
9510255736Sdavidch    fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9511255736Sdavidch
9512255736Sdavidch    /* setup sb indices */
9513255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9514255736Sdavidch        fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9515255736Sdavidch        fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9516255736Sdavidch    } else {
9517255736Sdavidch        fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9518255736Sdavidch        fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9519255736Sdavidch    }
9520255736Sdavidch
9521255736Sdavidch    /* init shortcut */
9522255736Sdavidch    fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9523255736Sdavidch
9524255736Sdavidch    fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9525255736Sdavidch
9526255736Sdavidch    /*
9527255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
9528255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9529255736Sdavidch     */
9530255736Sdavidch    for (cos = 0; cos < sc->max_cos; cos++) {
9531255736Sdavidch        cids[cos] = idx;
9532255736Sdavidch    }
9533255736Sdavidch    fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9534255736Sdavidch
9535255736Sdavidch    /* nothing more for a VF to do */
9536255736Sdavidch    if (IS_VF(sc)) {
9537255736Sdavidch        return;
9538255736Sdavidch    }
9539255736Sdavidch
9540255736Sdavidch    bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9541255736Sdavidch                fp->fw_sb_id, fp->igu_sb_id);
9542255736Sdavidch
9543255736Sdavidch    bxe_update_fp_sb_idx(fp);
9544255736Sdavidch
9545255736Sdavidch    /* Configure Queue State object */
9546255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9547255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9548255736Sdavidch
9549255736Sdavidch    ecore_init_queue_obj(sc,
9550255736Sdavidch                         &sc->sp_objs[idx].q_obj,
9551255736Sdavidch                         fp->cl_id,
9552255736Sdavidch                         cids,
9553255736Sdavidch                         sc->max_cos,
9554255736Sdavidch                         SC_FUNC(sc),
9555255736Sdavidch                         BXE_SP(sc, q_rdata),
9556255736Sdavidch                         BXE_SP_MAPPING(sc, q_rdata),
9557255736Sdavidch                         q_type);
9558255736Sdavidch
9559255736Sdavidch    /* configure classification DBs */
9560255736Sdavidch    ecore_init_mac_obj(sc,
9561255736Sdavidch                       &sc->sp_objs[idx].mac_obj,
9562255736Sdavidch                       fp->cl_id,
9563255736Sdavidch                       idx,
9564255736Sdavidch                       SC_FUNC(sc),
9565255736Sdavidch                       BXE_SP(sc, mac_rdata),
9566255736Sdavidch                       BXE_SP_MAPPING(sc, mac_rdata),
9567255736Sdavidch                       ECORE_FILTER_MAC_PENDING,
9568255736Sdavidch                       &sc->sp_state,
9569255736Sdavidch                       ECORE_OBJ_TYPE_RX_TX,
9570255736Sdavidch                       &sc->macs_pool);
9571255736Sdavidch
9572255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9573255736Sdavidch          idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9574255736Sdavidch}
9575255736Sdavidch
9576255736Sdavidchstatic inline void
9577255736Sdavidchbxe_update_rx_prod(struct bxe_softc    *sc,
9578255736Sdavidch                   struct bxe_fastpath *fp,
9579255736Sdavidch                   uint16_t            rx_bd_prod,
9580255736Sdavidch                   uint16_t            rx_cq_prod,
9581255736Sdavidch                   uint16_t            rx_sge_prod)
9582255736Sdavidch{
9583255736Sdavidch    struct ustorm_eth_rx_producers rx_prods = { 0 };
9584255736Sdavidch    uint32_t i;
9585255736Sdavidch
9586255736Sdavidch    /* update producers */
9587255736Sdavidch    rx_prods.bd_prod  = rx_bd_prod;
9588255736Sdavidch    rx_prods.cqe_prod = rx_cq_prod;
9589255736Sdavidch    rx_prods.sge_prod = rx_sge_prod;
9590255736Sdavidch
9591255736Sdavidch    /*
9592255736Sdavidch     * Make sure that the BD and SGE data is updated before updating the
9593255736Sdavidch     * producers since FW might read the BD/SGE right after the producer
9594255736Sdavidch     * is updated.
9595255736Sdavidch     * This is only applicable for weak-ordered memory model archs such
9596255736Sdavidch     * as IA-64. The following barrier is also mandatory since FW will
9597255736Sdavidch     * assumes BDs must have buffers.
9598255736Sdavidch     */
9599255736Sdavidch    wmb();
9600255736Sdavidch
9601255736Sdavidch    for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9602255736Sdavidch        REG_WR(sc,
9603255736Sdavidch               (fp->ustorm_rx_prods_offset + (i * 4)),
9604255736Sdavidch               ((uint32_t *)&rx_prods)[i]);
9605255736Sdavidch    }
9606255736Sdavidch
9607255736Sdavidch    wmb(); /* keep prod updates ordered */
9608255736Sdavidch
9609255736Sdavidch    BLOGD(sc, DBG_RX,
9610255736Sdavidch          "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9611255736Sdavidch          fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9612255736Sdavidch}
9613255736Sdavidch
9614255736Sdavidchstatic void
9615255736Sdavidchbxe_init_rx_rings(struct bxe_softc *sc)
9616255736Sdavidch{
9617255736Sdavidch    struct bxe_fastpath *fp;
9618255736Sdavidch    int i;
9619255736Sdavidch
9620255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9621255736Sdavidch        fp = &sc->fp[i];
9622255736Sdavidch
9623255736Sdavidch        fp->rx_bd_cons = 0;
9624255736Sdavidch
9625255736Sdavidch        /*
9626255736Sdavidch         * Activate the BD ring...
9627255736Sdavidch         * Warning, this will generate an interrupt (to the TSTORM)
9628255736Sdavidch         * so this can only be done after the chip is initialized
9629255736Sdavidch         */
9630255736Sdavidch        bxe_update_rx_prod(sc, fp,
9631255736Sdavidch                           fp->rx_bd_prod,
9632255736Sdavidch                           fp->rx_cq_prod,
9633255736Sdavidch                           fp->rx_sge_prod);
9634255736Sdavidch
9635255736Sdavidch        if (i != 0) {
9636255736Sdavidch            continue;
9637255736Sdavidch        }
9638255736Sdavidch
9639255736Sdavidch        if (CHIP_IS_E1(sc)) {
9640255736Sdavidch            REG_WR(sc,
9641255736Sdavidch                   (BAR_USTRORM_INTMEM +
9642255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9643255736Sdavidch                   U64_LO(fp->rcq_dma.paddr));
9644255736Sdavidch            REG_WR(sc,
9645255736Sdavidch                   (BAR_USTRORM_INTMEM +
9646255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9647255736Sdavidch                   U64_HI(fp->rcq_dma.paddr));
9648255736Sdavidch        }
9649255736Sdavidch    }
9650255736Sdavidch}
9651255736Sdavidch
9652255736Sdavidchstatic void
9653255736Sdavidchbxe_init_tx_ring_one(struct bxe_fastpath *fp)
9654255736Sdavidch{
9655296071Sdavidcs    SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9656255736Sdavidch    fp->tx_db.data.zero_fill1 = 0;
9657255736Sdavidch    fp->tx_db.data.prod = 0;
9658255736Sdavidch
9659255736Sdavidch    fp->tx_pkt_prod = 0;
9660255736Sdavidch    fp->tx_pkt_cons = 0;
9661255736Sdavidch    fp->tx_bd_prod = 0;
9662255736Sdavidch    fp->tx_bd_cons = 0;
9663255736Sdavidch    fp->eth_q_stats.tx_pkts = 0;
9664255736Sdavidch}
9665255736Sdavidch
9666255736Sdavidchstatic inline void
9667255736Sdavidchbxe_init_tx_rings(struct bxe_softc *sc)
9668255736Sdavidch{
9669255736Sdavidch    int i;
9670255736Sdavidch
9671255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9672255736Sdavidch        bxe_init_tx_ring_one(&sc->fp[i]);
9673255736Sdavidch    }
9674255736Sdavidch}
9675255736Sdavidch
9676255736Sdavidchstatic void
9677255736Sdavidchbxe_init_def_sb(struct bxe_softc *sc)
9678255736Sdavidch{
9679255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
9680255736Sdavidch    bus_addr_t mapping = sc->def_sb_dma.paddr;
9681255736Sdavidch    int igu_sp_sb_index;
9682255736Sdavidch    int igu_seg_id;
9683255736Sdavidch    int port = SC_PORT(sc);
9684255736Sdavidch    int func = SC_FUNC(sc);
9685255736Sdavidch    int reg_offset, reg_offset_en5;
9686255736Sdavidch    uint64_t section;
9687255736Sdavidch    int index, sindex;
9688255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9689255736Sdavidch
9690255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9691255736Sdavidch
9692255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9693255736Sdavidch        igu_sp_sb_index = DEF_SB_IGU_ID;
9694255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_DEF;
9695255736Sdavidch    } else {
9696255736Sdavidch        igu_sp_sb_index = sc->igu_dsb_id;
9697255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_DEF;
9698255736Sdavidch    }
9699255736Sdavidch
9700255736Sdavidch    /* attentions */
9701255736Sdavidch    section = ((uint64_t)mapping +
9702255736Sdavidch               offsetof(struct host_sp_status_block, atten_status_block));
9703255736Sdavidch    def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9704255736Sdavidch    sc->attn_state = 0;
9705255736Sdavidch
9706255736Sdavidch    reg_offset = (port) ?
9707255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9708255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9709255736Sdavidch    reg_offset_en5 = (port) ?
9710255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9711255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9712255736Sdavidch
9713255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9714255736Sdavidch        /* take care of sig[0]..sig[4] */
9715255736Sdavidch        for (sindex = 0; sindex < 4; sindex++) {
9716255736Sdavidch            sc->attn_group[index].sig[sindex] =
9717255736Sdavidch                REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9718255736Sdavidch        }
9719255736Sdavidch
9720255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
9721255736Sdavidch            /*
9722255736Sdavidch             * enable5 is separate from the rest of the registers,
9723255736Sdavidch             * and the address skip is 4 and not 16 between the
9724255736Sdavidch             * different groups
9725255736Sdavidch             */
9726255736Sdavidch            sc->attn_group[index].sig[4] =
9727255736Sdavidch                REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9728255736Sdavidch        } else {
9729255736Sdavidch            sc->attn_group[index].sig[4] = 0;
9730255736Sdavidch        }
9731255736Sdavidch    }
9732255736Sdavidch
9733255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
9734255736Sdavidch        reg_offset = (port) ?
9735255736Sdavidch                         HC_REG_ATTN_MSG1_ADDR_L :
9736255736Sdavidch                         HC_REG_ATTN_MSG0_ADDR_L;
9737255736Sdavidch        REG_WR(sc, reg_offset, U64_LO(section));
9738255736Sdavidch        REG_WR(sc, (reg_offset + 4), U64_HI(section));
9739255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
9740255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9741255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9742255736Sdavidch    }
9743255736Sdavidch
9744255736Sdavidch    section = ((uint64_t)mapping +
9745255736Sdavidch               offsetof(struct host_sp_status_block, sp_sb));
9746255736Sdavidch
9747255736Sdavidch    bxe_zero_sp_sb(sc);
9748255736Sdavidch
9749255736Sdavidch    /* PCI guarantees endianity of regpair */
9750255736Sdavidch    sp_sb_data.state           = SB_ENABLED;
9751255736Sdavidch    sp_sb_data.host_sb_addr.lo = U64_LO(section);
9752255736Sdavidch    sp_sb_data.host_sb_addr.hi = U64_HI(section);
9753255736Sdavidch    sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9754255736Sdavidch    sp_sb_data.igu_seg_id      = igu_seg_id;
9755255736Sdavidch    sp_sb_data.p_func.pf_id    = func;
9756255736Sdavidch    sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9757255736Sdavidch    sp_sb_data.p_func.vf_id    = 0xff;
9758255736Sdavidch
9759255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9760255736Sdavidch
9761255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9762255736Sdavidch}
9763255736Sdavidch
9764255736Sdavidchstatic void
9765255736Sdavidchbxe_init_sp_ring(struct bxe_softc *sc)
9766255736Sdavidch{
9767255736Sdavidch    atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9768255736Sdavidch    sc->spq_prod_idx = 0;
9769255736Sdavidch    sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9770255736Sdavidch    sc->spq_prod_bd = sc->spq;
9771255736Sdavidch    sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9772255736Sdavidch}
9773255736Sdavidch
9774255736Sdavidchstatic void
9775255736Sdavidchbxe_init_eq_ring(struct bxe_softc *sc)
9776255736Sdavidch{
9777255736Sdavidch    union event_ring_elem *elem;
9778255736Sdavidch    int i;
9779255736Sdavidch
9780255736Sdavidch    for (i = 1; i <= NUM_EQ_PAGES; i++) {
9781255736Sdavidch        elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9782255736Sdavidch
9783255736Sdavidch        elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9784255736Sdavidch                                                 BCM_PAGE_SIZE *
9785255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9786255736Sdavidch        elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9787255736Sdavidch                                                 BCM_PAGE_SIZE *
9788255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9789255736Sdavidch    }
9790255736Sdavidch
9791255736Sdavidch    sc->eq_cons    = 0;
9792255736Sdavidch    sc->eq_prod    = NUM_EQ_DESC;
9793255736Sdavidch    sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9794255736Sdavidch
9795255736Sdavidch    atomic_store_rel_long(&sc->eq_spq_left,
9796255736Sdavidch                          (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9797255736Sdavidch                               NUM_EQ_DESC) - 1));
9798255736Sdavidch}
9799255736Sdavidch
9800255736Sdavidchstatic void
9801255736Sdavidchbxe_init_internal_common(struct bxe_softc *sc)
9802255736Sdavidch{
9803255736Sdavidch    int i;
9804255736Sdavidch
9805255736Sdavidch    /*
9806255736Sdavidch     * Zero this manually as its initialization is currently missing
9807255736Sdavidch     * in the initTool.
9808255736Sdavidch     */
9809255736Sdavidch    for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9810255736Sdavidch        REG_WR(sc,
9811255736Sdavidch               (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9812255736Sdavidch               0);
9813255736Sdavidch    }
9814255736Sdavidch
9815255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9816255736Sdavidch        REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9817255736Sdavidch                CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9818255736Sdavidch    }
9819255736Sdavidch}
9820255736Sdavidch
9821255736Sdavidchstatic void
9822255736Sdavidchbxe_init_internal(struct bxe_softc *sc,
9823255736Sdavidch                  uint32_t         load_code)
9824255736Sdavidch{
9825255736Sdavidch    switch (load_code) {
9826255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON:
9827255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9828255736Sdavidch        bxe_init_internal_common(sc);
9829255736Sdavidch        /* no break */
9830255736Sdavidch
9831255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_PORT:
9832255736Sdavidch        /* nothing to do */
9833255736Sdavidch        /* no break */
9834255736Sdavidch
9835255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_FUNCTION:
9836255736Sdavidch        /* internal memory per function is initialized inside bxe_pf_init */
9837255736Sdavidch        break;
9838255736Sdavidch
9839255736Sdavidch    default:
9840255736Sdavidch        BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
9841255736Sdavidch        break;
9842255736Sdavidch    }
9843255736Sdavidch}
9844255736Sdavidch
9845255736Sdavidchstatic void
9846255736Sdavidchstorm_memset_func_cfg(struct bxe_softc                         *sc,
9847255736Sdavidch                      struct tstorm_eth_function_common_config *tcfg,
9848255736Sdavidch                      uint16_t                                  abs_fid)
9849255736Sdavidch{
9850255736Sdavidch    uint32_t addr;
9851255736Sdavidch    size_t size;
9852255736Sdavidch
9853255736Sdavidch    addr = (BAR_TSTRORM_INTMEM +
9854255736Sdavidch            TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
9855255736Sdavidch    size = sizeof(struct tstorm_eth_function_common_config);
9856255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
9857255736Sdavidch}
9858255736Sdavidch
9859255736Sdavidchstatic void
9860255736Sdavidchbxe_func_init(struct bxe_softc            *sc,
9861255736Sdavidch              struct bxe_func_init_params *p)
9862255736Sdavidch{
9863255736Sdavidch    struct tstorm_eth_function_common_config tcfg = { 0 };
9864255736Sdavidch
9865255736Sdavidch    if (CHIP_IS_E1x(sc)) {
9866255736Sdavidch        storm_memset_func_cfg(sc, &tcfg, p->func_id);
9867255736Sdavidch    }
9868255736Sdavidch
9869255736Sdavidch    /* Enable the function in the FW */
9870255736Sdavidch    storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
9871255736Sdavidch    storm_memset_func_en(sc, p->func_id, 1);
9872255736Sdavidch
9873255736Sdavidch    /* spq */
9874255736Sdavidch    if (p->func_flgs & FUNC_FLG_SPQ) {
9875255736Sdavidch        storm_memset_spq_addr(sc, p->spq_map, p->func_id);
9876255736Sdavidch        REG_WR(sc,
9877255736Sdavidch               (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
9878255736Sdavidch               p->spq_prod);
9879255736Sdavidch    }
9880255736Sdavidch}
9881255736Sdavidch
9882255736Sdavidch/*
9883255736Sdavidch * Calculates the sum of vn_min_rates.
9884255736Sdavidch * It's needed for further normalizing of the min_rates.
9885255736Sdavidch * Returns:
9886255736Sdavidch *   sum of vn_min_rates.
9887255736Sdavidch *     or
9888255736Sdavidch *   0 - if all the min_rates are 0.
9889255736Sdavidch * In the later case fainess algorithm should be deactivated.
9890255736Sdavidch * If all min rates are not zero then those that are zeroes will be set to 1.
9891255736Sdavidch */
9892255736Sdavidchstatic void
9893255736Sdavidchbxe_calc_vn_min(struct bxe_softc       *sc,
9894255736Sdavidch                struct cmng_init_input *input)
9895255736Sdavidch{
9896255736Sdavidch    uint32_t vn_cfg;
9897255736Sdavidch    uint32_t vn_min_rate;
9898255736Sdavidch    int all_zero = 1;
9899255736Sdavidch    int vn;
9900255736Sdavidch
9901255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
9902255736Sdavidch        vn_cfg = sc->devinfo.mf_info.mf_config[vn];
9903255736Sdavidch        vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
9904255736Sdavidch                        FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
9905255736Sdavidch
9906255736Sdavidch        if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
9907255736Sdavidch            /* skip hidden VNs */
9908255736Sdavidch            vn_min_rate = 0;
9909255736Sdavidch        } else if (!vn_min_rate) {
9910255736Sdavidch            /* If min rate is zero - set it to 100 */
9911255736Sdavidch            vn_min_rate = DEF_MIN_RATE;
9912255736Sdavidch        } else {
9913255736Sdavidch            all_zero = 0;
9914255736Sdavidch        }
9915255736Sdavidch
9916255736Sdavidch        input->vnic_min_rate[vn] = vn_min_rate;
9917255736Sdavidch    }
9918255736Sdavidch
9919255736Sdavidch    /* if ETS or all min rates are zeros - disable fairness */
9920255736Sdavidch    if (BXE_IS_ETS_ENABLED(sc)) {
9921255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
9922255736Sdavidch        BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
9923255736Sdavidch    } else if (all_zero) {
9924255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
9925255736Sdavidch        BLOGD(sc, DBG_LOAD,
9926255736Sdavidch              "Fariness disabled (all MIN values are zeroes)\n");
9927255736Sdavidch    } else {
9928255736Sdavidch        input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
9929255736Sdavidch    }
9930255736Sdavidch}
9931255736Sdavidch
9932255736Sdavidchstatic inline uint16_t
9933255736Sdavidchbxe_extract_max_cfg(struct bxe_softc *sc,
9934255736Sdavidch                    uint32_t         mf_cfg)
9935255736Sdavidch{
9936255736Sdavidch    uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
9937255736Sdavidch                        FUNC_MF_CFG_MAX_BW_SHIFT);
9938255736Sdavidch
9939255736Sdavidch    if (!max_cfg) {
9940255736Sdavidch        BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
9941255736Sdavidch        max_cfg = 100;
9942255736Sdavidch    }
9943255736Sdavidch
9944255736Sdavidch    return (max_cfg);
9945255736Sdavidch}
9946255736Sdavidch
9947255736Sdavidchstatic void
9948255736Sdavidchbxe_calc_vn_max(struct bxe_softc       *sc,
9949255736Sdavidch                int                    vn,
9950255736Sdavidch                struct cmng_init_input *input)
9951255736Sdavidch{
9952255736Sdavidch    uint16_t vn_max_rate;
9953255736Sdavidch    uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
9954255736Sdavidch    uint32_t max_cfg;
9955255736Sdavidch
9956255736Sdavidch    if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
9957255736Sdavidch        vn_max_rate = 0;
9958255736Sdavidch    } else {
9959255736Sdavidch        max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
9960255736Sdavidch
9961255736Sdavidch        if (IS_MF_SI(sc)) {
9962255736Sdavidch            /* max_cfg in percents of linkspeed */
9963255736Sdavidch            vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
9964255736Sdavidch        } else { /* SD modes */
9965255736Sdavidch            /* max_cfg is absolute in 100Mb units */
9966255736Sdavidch            vn_max_rate = (max_cfg * 100);
9967255736Sdavidch        }
9968255736Sdavidch    }
9969255736Sdavidch
9970255736Sdavidch    BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
9971255736Sdavidch
9972255736Sdavidch    input->vnic_max_rate[vn] = vn_max_rate;
9973255736Sdavidch}
9974255736Sdavidch
9975255736Sdavidchstatic void
9976255736Sdavidchbxe_cmng_fns_init(struct bxe_softc *sc,
9977255736Sdavidch                  uint8_t          read_cfg,
9978255736Sdavidch                  uint8_t          cmng_type)
9979255736Sdavidch{
9980255736Sdavidch    struct cmng_init_input input;
9981255736Sdavidch    int vn;
9982255736Sdavidch
9983255736Sdavidch    memset(&input, 0, sizeof(struct cmng_init_input));
9984255736Sdavidch
9985255736Sdavidch    input.port_rate = sc->link_vars.line_speed;
9986255736Sdavidch
9987255736Sdavidch    if (cmng_type == CMNG_FNS_MINMAX) {
9988255736Sdavidch        /* read mf conf from shmem */
9989255736Sdavidch        if (read_cfg) {
9990255736Sdavidch            bxe_read_mf_cfg(sc);
9991255736Sdavidch        }
9992255736Sdavidch
9993255736Sdavidch        /* get VN min rate and enable fairness if not 0 */
9994255736Sdavidch        bxe_calc_vn_min(sc, &input);
9995255736Sdavidch
9996255736Sdavidch        /* get VN max rate */
9997255736Sdavidch        if (sc->port.pmf) {
9998255736Sdavidch            for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
9999255736Sdavidch                bxe_calc_vn_max(sc, vn, &input);
10000255736Sdavidch            }
10001255736Sdavidch        }
10002255736Sdavidch
10003255736Sdavidch        /* always enable rate shaping and fairness */
10004255736Sdavidch        input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10005255736Sdavidch
10006255736Sdavidch        ecore_init_cmng(&input, &sc->cmng);
10007255736Sdavidch        return;
10008255736Sdavidch    }
10009255736Sdavidch
10010255736Sdavidch    /* rate shaping and fairness are disabled */
10011255736Sdavidch    BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10012255736Sdavidch}
10013255736Sdavidch
10014255736Sdavidchstatic int
10015255736Sdavidchbxe_get_cmng_fns_mode(struct bxe_softc *sc)
10016255736Sdavidch{
10017255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10018255736Sdavidch        return (CMNG_FNS_NONE);
10019255736Sdavidch    }
10020255736Sdavidch
10021255736Sdavidch    if (IS_MF(sc)) {
10022255736Sdavidch        return (CMNG_FNS_MINMAX);
10023255736Sdavidch    }
10024255736Sdavidch
10025255736Sdavidch    return (CMNG_FNS_NONE);
10026255736Sdavidch}
10027255736Sdavidch
10028255736Sdavidchstatic void
10029255736Sdavidchstorm_memset_cmng(struct bxe_softc *sc,
10030255736Sdavidch                  struct cmng_init *cmng,
10031255736Sdavidch                  uint8_t          port)
10032255736Sdavidch{
10033255736Sdavidch    int vn;
10034255736Sdavidch    int func;
10035255736Sdavidch    uint32_t addr;
10036255736Sdavidch    size_t size;
10037255736Sdavidch
10038255736Sdavidch    addr = (BAR_XSTRORM_INTMEM +
10039255736Sdavidch            XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10040255736Sdavidch    size = sizeof(struct cmng_struct_per_port);
10041255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10042255736Sdavidch
10043255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10044255736Sdavidch        func = func_by_vn(sc, vn);
10045255736Sdavidch
10046255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10047255736Sdavidch                XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10048255736Sdavidch        size = sizeof(struct rate_shaping_vars_per_vn);
10049255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10050255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10051255736Sdavidch
10052255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10053255736Sdavidch                XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10054255736Sdavidch        size = sizeof(struct fairness_vars_per_vn);
10055255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10056255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10057255736Sdavidch    }
10058255736Sdavidch}
10059255736Sdavidch
10060255736Sdavidchstatic void
10061255736Sdavidchbxe_pf_init(struct bxe_softc *sc)
10062255736Sdavidch{
10063255736Sdavidch    struct bxe_func_init_params func_init = { 0 };
10064255736Sdavidch    struct event_ring_data eq_data = { { 0 } };
10065255736Sdavidch    uint16_t flags;
10066255736Sdavidch
10067255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10068255736Sdavidch        /* reset IGU PF statistics: MSIX + ATTN */
10069255736Sdavidch        /* PF */
10070255736Sdavidch        REG_WR(sc,
10071255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10072255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10073255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10074255736Sdavidch               0);
10075255736Sdavidch        /* ATTN */
10076255736Sdavidch        REG_WR(sc,
10077255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10078255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10079255736Sdavidch                (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10080255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10081255736Sdavidch               0);
10082255736Sdavidch    }
10083255736Sdavidch
10084255736Sdavidch    /* function setup flags */
10085255736Sdavidch    flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10086255736Sdavidch
10087255736Sdavidch    /*
10088255736Sdavidch     * This flag is relevant for E1x only.
10089255736Sdavidch     * E2 doesn't have a TPA configuration in a function level.
10090255736Sdavidch     */
10091266979Smarcel    flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10092255736Sdavidch
10093255736Sdavidch    func_init.func_flgs = flags;
10094255736Sdavidch    func_init.pf_id     = SC_FUNC(sc);
10095255736Sdavidch    func_init.func_id   = SC_FUNC(sc);
10096255736Sdavidch    func_init.spq_map   = sc->spq_dma.paddr;
10097255736Sdavidch    func_init.spq_prod  = sc->spq_prod_idx;
10098255736Sdavidch
10099255736Sdavidch    bxe_func_init(sc, &func_init);
10100255736Sdavidch
10101255736Sdavidch    memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10102255736Sdavidch
10103255736Sdavidch    /*
10104255736Sdavidch     * Congestion management values depend on the link rate.
10105255736Sdavidch     * There is no active link so initial link rate is set to 10Gbps.
10106255736Sdavidch     * When the link comes up the congestion management values are
10107255736Sdavidch     * re-calculated according to the actual link rate.
10108255736Sdavidch     */
10109255736Sdavidch    sc->link_vars.line_speed = SPEED_10000;
10110255736Sdavidch    bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10111255736Sdavidch
10112255736Sdavidch    /* Only the PMF sets the HW */
10113255736Sdavidch    if (sc->port.pmf) {
10114255736Sdavidch        storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10115255736Sdavidch    }
10116255736Sdavidch
10117255736Sdavidch    /* init Event Queue - PCI bus guarantees correct endainity */
10118255736Sdavidch    eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10119255736Sdavidch    eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10120255736Sdavidch    eq_data.producer     = sc->eq_prod;
10121255736Sdavidch    eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10122255736Sdavidch    eq_data.sb_id        = DEF_SB_ID;
10123255736Sdavidch    storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10124255736Sdavidch}
10125255736Sdavidch
10126255736Sdavidchstatic void
10127255736Sdavidchbxe_hc_int_enable(struct bxe_softc *sc)
10128255736Sdavidch{
10129255736Sdavidch    int port = SC_PORT(sc);
10130255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10131255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10132255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10133255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10134255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10135255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10136255736Sdavidch
10137255736Sdavidch    if (msix) {
10138255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10139255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0);
10140255736Sdavidch        val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10141255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10142255736Sdavidch        if (single_msix) {
10143255736Sdavidch            val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10144255736Sdavidch        }
10145255736Sdavidch    } else if (msi) {
10146255736Sdavidch        val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10147255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10148255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10149255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10150255736Sdavidch    } else {
10151255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10152255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10153255736Sdavidch                HC_CONFIG_0_REG_INT_LINE_EN_0 |
10154255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10155255736Sdavidch
10156255736Sdavidch        if (!CHIP_IS_E1(sc)) {
10157255736Sdavidch            BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10158255736Sdavidch                  val, port, addr);
10159255736Sdavidch
10160255736Sdavidch            REG_WR(sc, addr, val);
10161255736Sdavidch
10162255736Sdavidch            val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10163255736Sdavidch        }
10164255736Sdavidch    }
10165255736Sdavidch
10166255736Sdavidch    if (CHIP_IS_E1(sc)) {
10167255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10168255736Sdavidch    }
10169255736Sdavidch
10170255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10171255736Sdavidch          val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10172255736Sdavidch
10173255736Sdavidch    REG_WR(sc, addr, val);
10174255736Sdavidch
10175255736Sdavidch    /* ensure that HC_CONFIG is written before leading/trailing edge config */
10176255736Sdavidch    mb();
10177255736Sdavidch
10178255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10179255736Sdavidch        /* init leading/trailing edge */
10180255736Sdavidch        if (IS_MF(sc)) {
10181255736Sdavidch            val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10182255736Sdavidch            if (sc->port.pmf) {
10183255736Sdavidch                /* enable nig and gpio3 attention */
10184255736Sdavidch                val |= 0x1100;
10185255736Sdavidch            }
10186255736Sdavidch        } else {
10187255736Sdavidch            val = 0xffff;
10188255736Sdavidch        }
10189255736Sdavidch
10190255736Sdavidch        REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10191255736Sdavidch        REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10192255736Sdavidch    }
10193255736Sdavidch
10194255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10195255736Sdavidch    mb();
10196255736Sdavidch}
10197255736Sdavidch
10198255736Sdavidchstatic void
10199255736Sdavidchbxe_igu_int_enable(struct bxe_softc *sc)
10200255736Sdavidch{
10201255736Sdavidch    uint32_t val;
10202255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10203255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10204255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10205255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10206255736Sdavidch
10207255736Sdavidch    val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10208255736Sdavidch
10209255736Sdavidch    if (msix) {
10210255736Sdavidch        val &= ~(IGU_PF_CONF_INT_LINE_EN |
10211255736Sdavidch                 IGU_PF_CONF_SINGLE_ISR_EN);
10212255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10213255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN);
10214255736Sdavidch        if (single_msix) {
10215255736Sdavidch            val |= IGU_PF_CONF_SINGLE_ISR_EN;
10216255736Sdavidch        }
10217255736Sdavidch    } else if (msi) {
10218255736Sdavidch        val &= ~IGU_PF_CONF_INT_LINE_EN;
10219255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10220255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10221255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10222255736Sdavidch    } else {
10223255736Sdavidch        val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10224255736Sdavidch        val |= (IGU_PF_CONF_INT_LINE_EN |
10225255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10226255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10227255736Sdavidch    }
10228255736Sdavidch
10229255736Sdavidch    /* clean previous status - need to configure igu prior to ack*/
10230255736Sdavidch    if ((!msix) || single_msix) {
10231255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10232255736Sdavidch        bxe_ack_int(sc);
10233255736Sdavidch    }
10234255736Sdavidch
10235255736Sdavidch    val |= IGU_PF_CONF_FUNC_EN;
10236255736Sdavidch
10237255736Sdavidch    BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10238255736Sdavidch          val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10239255736Sdavidch
10240255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10241255736Sdavidch
10242255736Sdavidch    mb();
10243255736Sdavidch
10244255736Sdavidch    /* init leading/trailing edge */
10245255736Sdavidch    if (IS_MF(sc)) {
10246255736Sdavidch        val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10247255736Sdavidch        if (sc->port.pmf) {
10248255736Sdavidch            /* enable nig and gpio3 attention */
10249255736Sdavidch            val |= 0x1100;
10250255736Sdavidch        }
10251255736Sdavidch    } else {
10252255736Sdavidch        val = 0xffff;
10253255736Sdavidch    }
10254255736Sdavidch
10255255736Sdavidch    REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10256255736Sdavidch    REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10257255736Sdavidch
10258255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10259255736Sdavidch    mb();
10260255736Sdavidch}
10261255736Sdavidch
10262255736Sdavidchstatic void
10263255736Sdavidchbxe_int_enable(struct bxe_softc *sc)
10264255736Sdavidch{
10265255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10266255736Sdavidch        bxe_hc_int_enable(sc);
10267255736Sdavidch    } else {
10268255736Sdavidch        bxe_igu_int_enable(sc);
10269255736Sdavidch    }
10270255736Sdavidch}
10271255736Sdavidch
10272255736Sdavidchstatic void
10273255736Sdavidchbxe_hc_int_disable(struct bxe_softc *sc)
10274255736Sdavidch{
10275255736Sdavidch    int port = SC_PORT(sc);
10276255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10277255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10278255736Sdavidch
10279255736Sdavidch    /*
10280255736Sdavidch     * In E1 we must use only PCI configuration space to disable MSI/MSIX
10281255736Sdavidch     * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10282255736Sdavidch     * block
10283255736Sdavidch     */
10284255736Sdavidch    if (CHIP_IS_E1(sc)) {
10285255736Sdavidch        /*
10286255736Sdavidch         * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10287255736Sdavidch         * to prevent from HC sending interrupts after we exit the function
10288255736Sdavidch         */
10289255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10290255736Sdavidch
10291255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10292255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10293255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10294255736Sdavidch    } else {
10295255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10296255736Sdavidch                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10297255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10298255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10299255736Sdavidch    }
10300255736Sdavidch
10301255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10302255736Sdavidch
10303255736Sdavidch    /* flush all outstanding writes */
10304255736Sdavidch    mb();
10305255736Sdavidch
10306255736Sdavidch    REG_WR(sc, addr, val);
10307255736Sdavidch    if (REG_RD(sc, addr) != val) {
10308255736Sdavidch        BLOGE(sc, "proper val not read from HC IGU!\n");
10309255736Sdavidch    }
10310255736Sdavidch}
10311255736Sdavidch
10312255736Sdavidchstatic void
10313255736Sdavidchbxe_igu_int_disable(struct bxe_softc *sc)
10314255736Sdavidch{
10315255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10316255736Sdavidch
10317255736Sdavidch    val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10318255736Sdavidch             IGU_PF_CONF_INT_LINE_EN |
10319255736Sdavidch             IGU_PF_CONF_ATTN_BIT_EN);
10320255736Sdavidch
10321255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10322255736Sdavidch
10323255736Sdavidch    /* flush all outstanding writes */
10324255736Sdavidch    mb();
10325255736Sdavidch
10326255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10327255736Sdavidch    if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10328255736Sdavidch        BLOGE(sc, "proper val not read from IGU!\n");
10329255736Sdavidch    }
10330255736Sdavidch}
10331255736Sdavidch
10332255736Sdavidchstatic void
10333255736Sdavidchbxe_int_disable(struct bxe_softc *sc)
10334255736Sdavidch{
10335255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10336255736Sdavidch        bxe_hc_int_disable(sc);
10337255736Sdavidch    } else {
10338255736Sdavidch        bxe_igu_int_disable(sc);
10339255736Sdavidch    }
10340255736Sdavidch}
10341255736Sdavidch
10342255736Sdavidchstatic void
10343255736Sdavidchbxe_nic_init(struct bxe_softc *sc,
10344255736Sdavidch             int              load_code)
10345255736Sdavidch{
10346255736Sdavidch    int i;
10347255736Sdavidch
10348255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
10349255736Sdavidch        bxe_init_eth_fp(sc, i);
10350255736Sdavidch    }
10351255736Sdavidch
10352255736Sdavidch    rmb(); /* ensure status block indices were read */
10353255736Sdavidch
10354255736Sdavidch    bxe_init_rx_rings(sc);
10355255736Sdavidch    bxe_init_tx_rings(sc);
10356255736Sdavidch
10357255736Sdavidch    if (IS_VF(sc)) {
10358255736Sdavidch        return;
10359255736Sdavidch    }
10360255736Sdavidch
10361255736Sdavidch    /* initialize MOD_ABS interrupts */
10362255736Sdavidch    elink_init_mod_abs_int(sc, &sc->link_vars,
10363255736Sdavidch                           sc->devinfo.chip_id,
10364255736Sdavidch                           sc->devinfo.shmem_base,
10365255736Sdavidch                           sc->devinfo.shmem2_base,
10366255736Sdavidch                           SC_PORT(sc));
10367255736Sdavidch
10368255736Sdavidch    bxe_init_def_sb(sc);
10369255736Sdavidch    bxe_update_dsb_idx(sc);
10370255736Sdavidch    bxe_init_sp_ring(sc);
10371255736Sdavidch    bxe_init_eq_ring(sc);
10372255736Sdavidch    bxe_init_internal(sc, load_code);
10373255736Sdavidch    bxe_pf_init(sc);
10374255736Sdavidch    bxe_stats_init(sc);
10375255736Sdavidch
10376255736Sdavidch    /* flush all before enabling interrupts */
10377255736Sdavidch    mb();
10378255736Sdavidch
10379255736Sdavidch    bxe_int_enable(sc);
10380255736Sdavidch
10381255736Sdavidch    /* check for SPIO5 */
10382255736Sdavidch    bxe_attn_int_deasserted0(sc,
10383255736Sdavidch                             REG_RD(sc,
10384255736Sdavidch                                    (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10385255736Sdavidch                                     SC_PORT(sc)*4)) &
10386255736Sdavidch                             AEU_INPUTS_ATTN_BITS_SPIO5);
10387255736Sdavidch}
10388255736Sdavidch
10389255736Sdavidchstatic inline void
10390255736Sdavidchbxe_init_objs(struct bxe_softc *sc)
10391255736Sdavidch{
10392255736Sdavidch    /* mcast rules must be added to tx if tx switching is enabled */
10393255736Sdavidch    ecore_obj_type o_type =
10394255736Sdavidch        (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10395255736Sdavidch                                         ECORE_OBJ_TYPE_RX;
10396255736Sdavidch
10397255736Sdavidch    /* RX_MODE controlling object */
10398255736Sdavidch    ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10399255736Sdavidch
10400255736Sdavidch    /* multicast configuration controlling object */
10401255736Sdavidch    ecore_init_mcast_obj(sc,
10402255736Sdavidch                         &sc->mcast_obj,
10403255736Sdavidch                         sc->fp[0].cl_id,
10404255736Sdavidch                         sc->fp[0].index,
10405255736Sdavidch                         SC_FUNC(sc),
10406255736Sdavidch                         SC_FUNC(sc),
10407255736Sdavidch                         BXE_SP(sc, mcast_rdata),
10408255736Sdavidch                         BXE_SP_MAPPING(sc, mcast_rdata),
10409255736Sdavidch                         ECORE_FILTER_MCAST_PENDING,
10410255736Sdavidch                         &sc->sp_state,
10411255736Sdavidch                         o_type);
10412255736Sdavidch
10413255736Sdavidch    /* Setup CAM credit pools */
10414255736Sdavidch    ecore_init_mac_credit_pool(sc,
10415255736Sdavidch                               &sc->macs_pool,
10416255736Sdavidch                               SC_FUNC(sc),
10417255736Sdavidch                               CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10418255736Sdavidch                                                 VNICS_PER_PATH(sc));
10419255736Sdavidch
10420255736Sdavidch    ecore_init_vlan_credit_pool(sc,
10421255736Sdavidch                                &sc->vlans_pool,
10422255736Sdavidch                                SC_ABS_FUNC(sc) >> 1,
10423255736Sdavidch                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10424255736Sdavidch                                                  VNICS_PER_PATH(sc));
10425255736Sdavidch
10426255736Sdavidch    /* RSS configuration object */
10427255736Sdavidch    ecore_init_rss_config_obj(sc,
10428255736Sdavidch                              &sc->rss_conf_obj,
10429255736Sdavidch                              sc->fp[0].cl_id,
10430255736Sdavidch                              sc->fp[0].index,
10431255736Sdavidch                              SC_FUNC(sc),
10432255736Sdavidch                              SC_FUNC(sc),
10433255736Sdavidch                              BXE_SP(sc, rss_rdata),
10434255736Sdavidch                              BXE_SP_MAPPING(sc, rss_rdata),
10435255736Sdavidch                              ECORE_FILTER_RSS_CONF_PENDING,
10436255736Sdavidch                              &sc->sp_state, ECORE_OBJ_TYPE_RX);
10437255736Sdavidch}
10438255736Sdavidch
10439255736Sdavidch/*
10440255736Sdavidch * Initialize the function. This must be called before sending CLIENT_SETUP
10441255736Sdavidch * for the first client.
10442255736Sdavidch */
10443255736Sdavidchstatic inline int
10444255736Sdavidchbxe_func_start(struct bxe_softc *sc)
10445255736Sdavidch{
10446255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
10447255736Sdavidch    struct ecore_func_start_params *start_params = &func_params.params.start;
10448255736Sdavidch
10449255736Sdavidch    /* Prepare parameters for function state transitions */
10450255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10451255736Sdavidch
10452255736Sdavidch    func_params.f_obj = &sc->func_obj;
10453255736Sdavidch    func_params.cmd = ECORE_F_CMD_START;
10454255736Sdavidch
10455255736Sdavidch    /* Function parameters */
10456255736Sdavidch    start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10457255736Sdavidch    start_params->sd_vlan_tag = OVLAN(sc);
10458255736Sdavidch
10459255736Sdavidch    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10460255736Sdavidch        start_params->network_cos_mode = STATIC_COS;
10461255736Sdavidch    } else { /* CHIP_IS_E1X */
10462255736Sdavidch        start_params->network_cos_mode = FW_WRR;
10463255736Sdavidch    }
10464255736Sdavidch
10465296071Sdavidcs    //start_params->gre_tunnel_mode = 0;
10466296071Sdavidcs    //start_params->gre_tunnel_rss  = 0;
10467255736Sdavidch
10468255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
10469255736Sdavidch}
10470255736Sdavidch
10471255736Sdavidchstatic int
10472255736Sdavidchbxe_set_power_state(struct bxe_softc *sc,
10473255736Sdavidch                    uint8_t          state)
10474255736Sdavidch{
10475255736Sdavidch    uint16_t pmcsr;
10476255736Sdavidch
10477255736Sdavidch    /* If there is no power capability, silently succeed */
10478255736Sdavidch    if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10479255736Sdavidch        BLOGW(sc, "No power capability\n");
10480255736Sdavidch        return (0);
10481255736Sdavidch    }
10482255736Sdavidch
10483255736Sdavidch    pmcsr = pci_read_config(sc->dev,
10484255736Sdavidch                            (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10485255736Sdavidch                            2);
10486255736Sdavidch
10487255736Sdavidch    switch (state) {
10488255736Sdavidch    case PCI_PM_D0:
10489255736Sdavidch        pci_write_config(sc->dev,
10490255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10491255736Sdavidch                         ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10492255736Sdavidch
10493255736Sdavidch        if (pmcsr & PCIM_PSTAT_DMASK) {
10494255736Sdavidch            /* delay required during transition out of D3hot */
10495255736Sdavidch            DELAY(20000);
10496255736Sdavidch        }
10497255736Sdavidch
10498255736Sdavidch        break;
10499255736Sdavidch
10500255736Sdavidch    case PCI_PM_D3hot:
10501255736Sdavidch        /* XXX if there are other clients above don't shut down the power */
10502255736Sdavidch
10503255736Sdavidch        /* don't shut down the power for emulation and FPGA */
10504255736Sdavidch        if (CHIP_REV_IS_SLOW(sc)) {
10505255736Sdavidch            return (0);
10506255736Sdavidch        }
10507255736Sdavidch
10508255736Sdavidch        pmcsr &= ~PCIM_PSTAT_DMASK;
10509255736Sdavidch        pmcsr |= PCIM_PSTAT_D3;
10510255736Sdavidch
10511255736Sdavidch        if (sc->wol) {
10512255736Sdavidch            pmcsr |= PCIM_PSTAT_PMEENABLE;
10513255736Sdavidch        }
10514255736Sdavidch
10515255736Sdavidch        pci_write_config(sc->dev,
10516255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10517255736Sdavidch                         pmcsr, 4);
10518255736Sdavidch
10519255736Sdavidch        /*
10520255736Sdavidch         * No more memory access after this point until device is brought back
10521255736Sdavidch         * to D0 state.
10522255736Sdavidch         */
10523255736Sdavidch        break;
10524255736Sdavidch
10525255736Sdavidch    default:
10526295830Sdavidcs        BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10527295830Sdavidcs            state, pmcsr);
10528255736Sdavidch        return (-1);
10529255736Sdavidch    }
10530255736Sdavidch
10531255736Sdavidch    return (0);
10532255736Sdavidch}
10533255736Sdavidch
10534255736Sdavidch
10535255736Sdavidch/* return true if succeeded to acquire the lock */
10536255736Sdavidchstatic uint8_t
10537255736Sdavidchbxe_trylock_hw_lock(struct bxe_softc *sc,
10538255736Sdavidch                    uint32_t         resource)
10539255736Sdavidch{
10540255736Sdavidch    uint32_t lock_status;
10541255736Sdavidch    uint32_t resource_bit = (1 << resource);
10542255736Sdavidch    int func = SC_FUNC(sc);
10543255736Sdavidch    uint32_t hw_lock_control_reg;
10544255736Sdavidch
10545255736Sdavidch    BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10546255736Sdavidch
10547255736Sdavidch    /* Validating that the resource is within range */
10548255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10549255736Sdavidch        BLOGD(sc, DBG_LOAD,
10550255736Sdavidch              "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10551255736Sdavidch              resource, HW_LOCK_MAX_RESOURCE_VALUE);
10552255736Sdavidch        return (FALSE);
10553255736Sdavidch    }
10554255736Sdavidch
10555255736Sdavidch    if (func <= 5) {
10556255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10557255736Sdavidch    } else {
10558255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10559255736Sdavidch    }
10560255736Sdavidch
10561255736Sdavidch    /* try to acquire the lock */
10562255736Sdavidch    REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10563255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
10564255736Sdavidch    if (lock_status & resource_bit) {
10565255736Sdavidch        return (TRUE);
10566255736Sdavidch    }
10567255736Sdavidch
10568295830Sdavidcs    BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10569295830Sdavidcs        "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10570295830Sdavidcs        lock_status, resource_bit);
10571255736Sdavidch
10572255736Sdavidch    return (FALSE);
10573255736Sdavidch}
10574255736Sdavidch
10575255736Sdavidch/*
10576255736Sdavidch * Get the recovery leader resource id according to the engine this function
10577255736Sdavidch * belongs to. Currently only only 2 engines is supported.
10578255736Sdavidch */
10579255736Sdavidchstatic int
10580255736Sdavidchbxe_get_leader_lock_resource(struct bxe_softc *sc)
10581255736Sdavidch{
10582255736Sdavidch    if (SC_PATH(sc)) {
10583255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10584255736Sdavidch    } else {
10585255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10586255736Sdavidch    }
10587255736Sdavidch}
10588255736Sdavidch
10589255736Sdavidch/* try to acquire a leader lock for current engine */
10590255736Sdavidchstatic uint8_t
10591255736Sdavidchbxe_trylock_leader_lock(struct bxe_softc *sc)
10592255736Sdavidch{
10593255736Sdavidch    return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10594255736Sdavidch}
10595255736Sdavidch
10596255736Sdavidchstatic int
10597255736Sdavidchbxe_release_leader_lock(struct bxe_softc *sc)
10598255736Sdavidch{
10599255736Sdavidch    return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10600255736Sdavidch}
10601255736Sdavidch
10602255736Sdavidch/* close gates #2, #3 and #4 */
10603255736Sdavidchstatic void
10604255736Sdavidchbxe_set_234_gates(struct bxe_softc *sc,
10605255736Sdavidch                  uint8_t          close)
10606255736Sdavidch{
10607255736Sdavidch    uint32_t val;
10608255736Sdavidch
10609255736Sdavidch    /* gates #2 and #4a are closed/opened for "not E1" only */
10610255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10611255736Sdavidch        /* #4 */
10612255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10613255736Sdavidch        /* #2 */
10614255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10615255736Sdavidch    }
10616255736Sdavidch
10617255736Sdavidch    /* #3 */
10618255736Sdavidch    if (CHIP_IS_E1x(sc)) {
10619255736Sdavidch        /* prevent interrupts from HC on both ports */
10620255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_1);
10621255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_1,
10622255736Sdavidch               (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10623255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10624255736Sdavidch
10625255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_0);
10626255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_0,
10627255736Sdavidch               (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10628255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10629255736Sdavidch    } else {
10630298955Spfg        /* Prevent incoming interrupts in IGU */
10631255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10632255736Sdavidch
10633255736Sdavidch        REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10634255736Sdavidch               (!close) ?
10635255736Sdavidch               (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10636255736Sdavidch               (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10637255736Sdavidch    }
10638255736Sdavidch
10639255736Sdavidch    BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10640255736Sdavidch          close ? "closing" : "opening");
10641255736Sdavidch
10642255736Sdavidch    wmb();
10643255736Sdavidch}
10644255736Sdavidch
10645255736Sdavidch/* poll for pending writes bit, it should get cleared in no more than 1s */
10646255736Sdavidchstatic int
10647255736Sdavidchbxe_er_poll_igu_vq(struct bxe_softc *sc)
10648255736Sdavidch{
10649255736Sdavidch    uint32_t cnt = 1000;
10650255736Sdavidch    uint32_t pend_bits = 0;
10651255736Sdavidch
10652255736Sdavidch    do {
10653255736Sdavidch        pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10654255736Sdavidch
10655255736Sdavidch        if (pend_bits == 0) {
10656255736Sdavidch            break;
10657255736Sdavidch        }
10658255736Sdavidch
10659255736Sdavidch        DELAY(1000);
10660255736Sdavidch    } while (--cnt > 0);
10661255736Sdavidch
10662255736Sdavidch    if (cnt == 0) {
10663255736Sdavidch        BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10664255736Sdavidch        return (-1);
10665255736Sdavidch    }
10666255736Sdavidch
10667255736Sdavidch    return (0);
10668255736Sdavidch}
10669255736Sdavidch
10670255736Sdavidch#define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10671255736Sdavidch
10672255736Sdavidchstatic void
10673255736Sdavidchbxe_clp_reset_prep(struct bxe_softc *sc,
10674255736Sdavidch                   uint32_t         *magic_val)
10675255736Sdavidch{
10676255736Sdavidch    /* Do some magic... */
10677255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10678255736Sdavidch    *magic_val = val & SHARED_MF_CLP_MAGIC;
10679255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10680255736Sdavidch}
10681255736Sdavidch
10682255736Sdavidch/* restore the value of the 'magic' bit */
10683255736Sdavidchstatic void
10684255736Sdavidchbxe_clp_reset_done(struct bxe_softc *sc,
10685255736Sdavidch                   uint32_t         magic_val)
10686255736Sdavidch{
10687255736Sdavidch    /* Restore the 'magic' bit value... */
10688255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10689255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb,
10690255736Sdavidch              (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10691255736Sdavidch}
10692255736Sdavidch
10693255736Sdavidch/* prepare for MCP reset, takes care of CLP configurations */
10694255736Sdavidchstatic void
10695255736Sdavidchbxe_reset_mcp_prep(struct bxe_softc *sc,
10696255736Sdavidch                   uint32_t         *magic_val)
10697255736Sdavidch{
10698255736Sdavidch    uint32_t shmem;
10699255736Sdavidch    uint32_t validity_offset;
10700255736Sdavidch
10701255736Sdavidch    /* set `magic' bit in order to save MF config */
10702255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10703255736Sdavidch        bxe_clp_reset_prep(sc, magic_val);
10704255736Sdavidch    }
10705255736Sdavidch
10706255736Sdavidch    /* get shmem offset */
10707255736Sdavidch    shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10708255736Sdavidch    validity_offset =
10709255736Sdavidch        offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10710255736Sdavidch
10711255736Sdavidch    /* Clear validity map flags */
10712255736Sdavidch    if (shmem > 0) {
10713255736Sdavidch        REG_WR(sc, shmem + validity_offset, 0);
10714255736Sdavidch    }
10715255736Sdavidch}
10716255736Sdavidch
10717255736Sdavidch#define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10718255736Sdavidch#define MCP_ONE_TIMEOUT  100    /* 100 ms */
10719255736Sdavidch
10720255736Sdavidchstatic void
10721255736Sdavidchbxe_mcp_wait_one(struct bxe_softc *sc)
10722255736Sdavidch{
10723255736Sdavidch    /* special handling for emulation and FPGA (10 times longer) */
10724255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10725255736Sdavidch        DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10726255736Sdavidch    } else {
10727255736Sdavidch        DELAY((MCP_ONE_TIMEOUT) * 1000);
10728255736Sdavidch    }
10729255736Sdavidch}
10730255736Sdavidch
10731255736Sdavidch/* initialize shmem_base and waits for validity signature to appear */
10732255736Sdavidchstatic int
10733255736Sdavidchbxe_init_shmem(struct bxe_softc *sc)
10734255736Sdavidch{
10735255736Sdavidch    int cnt = 0;
10736255736Sdavidch    uint32_t val = 0;
10737255736Sdavidch
10738255736Sdavidch    do {
10739255736Sdavidch        sc->devinfo.shmem_base     =
10740255736Sdavidch        sc->link_params.shmem_base =
10741255736Sdavidch            REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10742255736Sdavidch
10743255736Sdavidch        if (sc->devinfo.shmem_base) {
10744255736Sdavidch            val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10745255736Sdavidch            if (val & SHR_MEM_VALIDITY_MB)
10746255736Sdavidch                return (0);
10747255736Sdavidch        }
10748255736Sdavidch
10749255736Sdavidch        bxe_mcp_wait_one(sc);
10750255736Sdavidch
10751255736Sdavidch    } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10752255736Sdavidch
10753255736Sdavidch    BLOGE(sc, "BAD MCP validity signature\n");
10754255736Sdavidch
10755255736Sdavidch    return (-1);
10756255736Sdavidch}
10757255736Sdavidch
10758255736Sdavidchstatic int
10759255736Sdavidchbxe_reset_mcp_comp(struct bxe_softc *sc,
10760255736Sdavidch                   uint32_t         magic_val)
10761255736Sdavidch{
10762255736Sdavidch    int rc = bxe_init_shmem(sc);
10763255736Sdavidch
10764255736Sdavidch    /* Restore the `magic' bit value */
10765255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10766255736Sdavidch        bxe_clp_reset_done(sc, magic_val);
10767255736Sdavidch    }
10768255736Sdavidch
10769255736Sdavidch    return (rc);
10770255736Sdavidch}
10771255736Sdavidch
10772255736Sdavidchstatic void
10773255736Sdavidchbxe_pxp_prep(struct bxe_softc *sc)
10774255736Sdavidch{
10775255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10776255736Sdavidch        REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10777255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10778255736Sdavidch        wmb();
10779255736Sdavidch    }
10780255736Sdavidch}
10781255736Sdavidch
10782255736Sdavidch/*
10783255736Sdavidch * Reset the whole chip except for:
10784255736Sdavidch *      - PCIE core
10785255736Sdavidch *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10786255736Sdavidch *      - IGU
10787255736Sdavidch *      - MISC (including AEU)
10788255736Sdavidch *      - GRC
10789255736Sdavidch *      - RBCN, RBCP
10790255736Sdavidch */
10791255736Sdavidchstatic void
10792255736Sdavidchbxe_process_kill_chip_reset(struct bxe_softc *sc,
10793255736Sdavidch                            uint8_t          global)
10794255736Sdavidch{
10795255736Sdavidch    uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10796255736Sdavidch    uint32_t global_bits2, stay_reset2;
10797255736Sdavidch
10798255736Sdavidch    /*
10799255736Sdavidch     * Bits that have to be set in reset_mask2 if we want to reset 'global'
10800255736Sdavidch     * (per chip) blocks.
10801255736Sdavidch     */
10802255736Sdavidch    global_bits2 =
10803255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10804255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10805255736Sdavidch
10806255736Sdavidch    /*
10807255736Sdavidch     * Don't reset the following blocks.
10808255736Sdavidch     * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10809255736Sdavidch     *            reset, as in 4 port device they might still be owned
10810255736Sdavidch     *            by the MCP (there is only one leader per path).
10811255736Sdavidch     */
10812255736Sdavidch    not_reset_mask1 =
10813255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_HC |
10814255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10815255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXP;
10816255736Sdavidch
10817255736Sdavidch    not_reset_mask2 =
10818255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10819255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10820255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10821255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10822255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10823255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10824255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10825255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10826255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_ATC |
10827255736Sdavidch        MISC_REGISTERS_RESET_REG_2_PGLC |
10828255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10829255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10830255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10831255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10832255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC0 |
10833255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC1;
10834255736Sdavidch
10835255736Sdavidch    /*
10836255736Sdavidch     * Keep the following blocks in reset:
10837255736Sdavidch     *  - all xxMACs are handled by the elink code.
10838255736Sdavidch     */
10839255736Sdavidch    stay_reset2 =
10840255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC |
10841255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
10842255736Sdavidch
10843255736Sdavidch    /* Full reset masks according to the chip */
10844255736Sdavidch    reset_mask1 = 0xffffffff;
10845255736Sdavidch
10846255736Sdavidch    if (CHIP_IS_E1(sc))
10847255736Sdavidch        reset_mask2 = 0xffff;
10848255736Sdavidch    else if (CHIP_IS_E1H(sc))
10849255736Sdavidch        reset_mask2 = 0x1ffff;
10850255736Sdavidch    else if (CHIP_IS_E2(sc))
10851255736Sdavidch        reset_mask2 = 0xfffff;
10852255736Sdavidch    else /* CHIP_IS_E3 */
10853255736Sdavidch        reset_mask2 = 0x3ffffff;
10854255736Sdavidch
10855255736Sdavidch    /* Don't reset global blocks unless we need to */
10856255736Sdavidch    if (!global)
10857255736Sdavidch        reset_mask2 &= ~global_bits2;
10858255736Sdavidch
10859255736Sdavidch    /*
10860255736Sdavidch     * In case of attention in the QM, we need to reset PXP
10861255736Sdavidch     * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
10862255736Sdavidch     * because otherwise QM reset would release 'close the gates' shortly
10863255736Sdavidch     * before resetting the PXP, then the PSWRQ would send a write
10864255736Sdavidch     * request to PGLUE. Then when PXP is reset, PGLUE would try to
10865255736Sdavidch     * read the payload data from PSWWR, but PSWWR would not
10866255736Sdavidch     * respond. The write queue in PGLUE would stuck, dmae commands
10867255736Sdavidch     * would not return. Therefore it's important to reset the second
10868255736Sdavidch     * reset register (containing the
10869255736Sdavidch     * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
10870255736Sdavidch     * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
10871255736Sdavidch     * bit).
10872255736Sdavidch     */
10873255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
10874255736Sdavidch           reset_mask2 & (~not_reset_mask2));
10875255736Sdavidch
10876255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
10877255736Sdavidch           reset_mask1 & (~not_reset_mask1));
10878255736Sdavidch
10879255736Sdavidch    mb();
10880255736Sdavidch    wmb();
10881255736Sdavidch
10882255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
10883255736Sdavidch           reset_mask2 & (~stay_reset2));
10884255736Sdavidch
10885255736Sdavidch    mb();
10886255736Sdavidch    wmb();
10887255736Sdavidch
10888255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
10889255736Sdavidch    wmb();
10890255736Sdavidch}
10891255736Sdavidch
10892255736Sdavidchstatic int
10893255736Sdavidchbxe_process_kill(struct bxe_softc *sc,
10894255736Sdavidch                 uint8_t          global)
10895255736Sdavidch{
10896255736Sdavidch    int cnt = 1000;
10897255736Sdavidch    uint32_t val = 0;
10898255736Sdavidch    uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
10899255736Sdavidch    uint32_t tags_63_32 = 0;
10900255736Sdavidch
10901255736Sdavidch    /* Empty the Tetris buffer, wait for 1s */
10902255736Sdavidch    do {
10903255736Sdavidch        sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
10904255736Sdavidch        blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
10905255736Sdavidch        port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
10906255736Sdavidch        port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
10907255736Sdavidch        pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
10908255736Sdavidch        if (CHIP_IS_E3(sc)) {
10909255736Sdavidch            tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
10910255736Sdavidch        }
10911255736Sdavidch
10912255736Sdavidch        if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
10913255736Sdavidch            ((port_is_idle_0 & 0x1) == 0x1) &&
10914255736Sdavidch            ((port_is_idle_1 & 0x1) == 0x1) &&
10915255736Sdavidch            (pgl_exp_rom2 == 0xffffffff) &&
10916255736Sdavidch            (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
10917255736Sdavidch            break;
10918255736Sdavidch        DELAY(1000);
10919255736Sdavidch    } while (cnt-- > 0);
10920255736Sdavidch
10921255736Sdavidch    if (cnt <= 0) {
10922255736Sdavidch        BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
10923255736Sdavidch                  "are still outstanding read requests after 1s! "
10924255736Sdavidch                  "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
10925255736Sdavidch                  "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
10926255736Sdavidch              sr_cnt, blk_cnt, port_is_idle_0,
10927255736Sdavidch              port_is_idle_1, pgl_exp_rom2);
10928255736Sdavidch        return (-1);
10929255736Sdavidch    }
10930255736Sdavidch
10931255736Sdavidch    mb();
10932255736Sdavidch
10933255736Sdavidch    /* Close gates #2, #3 and #4 */
10934255736Sdavidch    bxe_set_234_gates(sc, TRUE);
10935255736Sdavidch
10936255736Sdavidch    /* Poll for IGU VQs for 57712 and newer chips */
10937255736Sdavidch    if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
10938255736Sdavidch        return (-1);
10939255736Sdavidch    }
10940255736Sdavidch
10941255736Sdavidch    /* XXX indicate that "process kill" is in progress to MCP */
10942255736Sdavidch
10943255736Sdavidch    /* clear "unprepared" bit */
10944255736Sdavidch    REG_WR(sc, MISC_REG_UNPREPARED, 0);
10945255736Sdavidch    mb();
10946255736Sdavidch
10947255736Sdavidch    /* Make sure all is written to the chip before the reset */
10948255736Sdavidch    wmb();
10949255736Sdavidch
10950255736Sdavidch    /*
10951255736Sdavidch     * Wait for 1ms to empty GLUE and PCI-E core queues,
10952255736Sdavidch     * PSWHST, GRC and PSWRD Tetris buffer.
10953255736Sdavidch     */
10954255736Sdavidch    DELAY(1000);
10955255736Sdavidch
10956255736Sdavidch    /* Prepare to chip reset: */
10957255736Sdavidch    /* MCP */
10958255736Sdavidch    if (global) {
10959255736Sdavidch        bxe_reset_mcp_prep(sc, &val);
10960255736Sdavidch    }
10961255736Sdavidch
10962255736Sdavidch    /* PXP */
10963255736Sdavidch    bxe_pxp_prep(sc);
10964255736Sdavidch    mb();
10965255736Sdavidch
10966255736Sdavidch    /* reset the chip */
10967255736Sdavidch    bxe_process_kill_chip_reset(sc, global);
10968255736Sdavidch    mb();
10969255736Sdavidch
10970268854Sdavidcs    /* clear errors in PGB */
10971268854Sdavidcs    if (!CHIP_IS_E1(sc))
10972268854Sdavidcs        REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
10973268854Sdavidcs
10974255736Sdavidch    /* Recover after reset: */
10975255736Sdavidch    /* MCP */
10976255736Sdavidch    if (global && bxe_reset_mcp_comp(sc, val)) {
10977255736Sdavidch        return (-1);
10978255736Sdavidch    }
10979255736Sdavidch
10980255736Sdavidch    /* XXX add resetting the NO_MCP mode DB here */
10981255736Sdavidch
10982255736Sdavidch    /* Open the gates #2, #3 and #4 */
10983255736Sdavidch    bxe_set_234_gates(sc, FALSE);
10984255736Sdavidch
10985255736Sdavidch    /* XXX
10986255736Sdavidch     * IGU/AEU preparation bring back the AEU/IGU to a reset state
10987255736Sdavidch     * re-enable attentions
10988255736Sdavidch     */
10989255736Sdavidch
10990255736Sdavidch    return (0);
10991255736Sdavidch}
10992255736Sdavidch
10993255736Sdavidchstatic int
10994255736Sdavidchbxe_leader_reset(struct bxe_softc *sc)
10995255736Sdavidch{
10996255736Sdavidch    int rc = 0;
10997255736Sdavidch    uint8_t global = bxe_reset_is_global(sc);
10998255736Sdavidch    uint32_t load_code;
10999255736Sdavidch
11000255736Sdavidch    /*
11001255736Sdavidch     * If not going to reset MCP, load "fake" driver to reset HW while
11002255736Sdavidch     * driver is owner of the HW.
11003255736Sdavidch     */
11004255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11005255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11006255736Sdavidch                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11007255736Sdavidch        if (!load_code) {
11008255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11009255736Sdavidch            rc = -1;
11010255736Sdavidch            goto exit_leader_reset;
11011255736Sdavidch        }
11012255736Sdavidch
11013255736Sdavidch        if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11014255736Sdavidch            (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11015255736Sdavidch            BLOGE(sc, "MCP unexpected response, aborting\n");
11016255736Sdavidch            rc = -1;
11017255736Sdavidch            goto exit_leader_reset2;
11018255736Sdavidch        }
11019255736Sdavidch
11020255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11021255736Sdavidch        if (!load_code) {
11022255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11023255736Sdavidch            rc = -1;
11024255736Sdavidch            goto exit_leader_reset2;
11025255736Sdavidch        }
11026255736Sdavidch    }
11027255736Sdavidch
11028255736Sdavidch    /* try to recover after the failure */
11029255736Sdavidch    if (bxe_process_kill(sc, global)) {
11030255736Sdavidch        BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11031255736Sdavidch        rc = -1;
11032255736Sdavidch        goto exit_leader_reset2;
11033255736Sdavidch    }
11034255736Sdavidch
11035255736Sdavidch    /*
11036255736Sdavidch     * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11037255736Sdavidch     * state.
11038255736Sdavidch     */
11039255736Sdavidch    bxe_set_reset_done(sc);
11040255736Sdavidch    if (global) {
11041255736Sdavidch        bxe_clear_reset_global(sc);
11042255736Sdavidch    }
11043255736Sdavidch
11044255736Sdavidchexit_leader_reset2:
11045255736Sdavidch
11046255736Sdavidch    /* unload "fake driver" if it was loaded */
11047255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11048255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11049255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11050255736Sdavidch    }
11051255736Sdavidch
11052255736Sdavidchexit_leader_reset:
11053255736Sdavidch
11054255736Sdavidch    sc->is_leader = 0;
11055255736Sdavidch    bxe_release_leader_lock(sc);
11056255736Sdavidch
11057255736Sdavidch    mb();
11058255736Sdavidch    return (rc);
11059255736Sdavidch}
11060255736Sdavidch
11061255736Sdavidch/*
11062255736Sdavidch * prepare INIT transition, parameters configured:
11063255736Sdavidch *   - HC configuration
11064255736Sdavidch *   - Queue's CDU context
11065255736Sdavidch */
11066255736Sdavidchstatic void
11067255736Sdavidchbxe_pf_q_prep_init(struct bxe_softc               *sc,
11068255736Sdavidch                   struct bxe_fastpath            *fp,
11069255736Sdavidch                   struct ecore_queue_init_params *init_params)
11070255736Sdavidch{
11071255736Sdavidch    uint8_t cos;
11072255736Sdavidch    int cxt_index, cxt_offset;
11073255736Sdavidch
11074255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11075255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11076255736Sdavidch
11077255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11078255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11079255736Sdavidch
11080255736Sdavidch    /* HC rate */
11081255736Sdavidch    init_params->rx.hc_rate =
11082255736Sdavidch        sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11083255736Sdavidch    init_params->tx.hc_rate =
11084255736Sdavidch        sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11085255736Sdavidch
11086255736Sdavidch    /* FW SB ID */
11087255736Sdavidch    init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11088255736Sdavidch
11089255736Sdavidch    /* CQ index among the SB indices */
11090255736Sdavidch    init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11091255736Sdavidch    init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11092255736Sdavidch
11093255736Sdavidch    /* set maximum number of COSs supported by this queue */
11094255736Sdavidch    init_params->max_cos = sc->max_cos;
11095255736Sdavidch
11096255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11097255736Sdavidch          fp->index, init_params->max_cos);
11098255736Sdavidch
11099255736Sdavidch    /* set the context pointers queue object */
11100255736Sdavidch    for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11101255736Sdavidch        /* XXX change index/cid here if ever support multiple tx CoS */
11102255736Sdavidch        /* fp->txdata[cos]->cid */
11103255736Sdavidch        cxt_index = fp->index / ILT_PAGE_CIDS;
11104255736Sdavidch        cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11105255736Sdavidch        init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11106255736Sdavidch    }
11107255736Sdavidch}
11108255736Sdavidch
11109255736Sdavidch/* set flags that are common for the Tx-only and not normal connections */
11110255736Sdavidchstatic unsigned long
11111255736Sdavidchbxe_get_common_flags(struct bxe_softc    *sc,
11112255736Sdavidch                     struct bxe_fastpath *fp,
11113255736Sdavidch                     uint8_t             zero_stats)
11114255736Sdavidch{
11115255736Sdavidch    unsigned long flags = 0;
11116255736Sdavidch
11117255736Sdavidch    /* PF driver will always initialize the Queue to an ACTIVE state */
11118255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11119255736Sdavidch
11120255736Sdavidch    /*
11121255736Sdavidch     * tx only connections collect statistics (on the same index as the
11122255736Sdavidch     * parent connection). The statistics are zeroed when the parent
11123255736Sdavidch     * connection is initialized.
11124255736Sdavidch     */
11125255736Sdavidch
11126255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11127255736Sdavidch    if (zero_stats) {
11128255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11129255736Sdavidch    }
11130255736Sdavidch
11131255736Sdavidch    /*
11132255736Sdavidch     * tx only connections can support tx-switching, though their
11133255736Sdavidch     * CoS-ness doesn't survive the loopback
11134255736Sdavidch     */
11135255736Sdavidch    if (sc->flags & BXE_TX_SWITCHING) {
11136255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11137255736Sdavidch    }
11138255736Sdavidch
11139255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11140255736Sdavidch
11141255736Sdavidch    return (flags);
11142255736Sdavidch}
11143255736Sdavidch
11144255736Sdavidchstatic unsigned long
11145255736Sdavidchbxe_get_q_flags(struct bxe_softc    *sc,
11146255736Sdavidch                struct bxe_fastpath *fp,
11147255736Sdavidch                uint8_t             leading)
11148255736Sdavidch{
11149255736Sdavidch    unsigned long flags = 0;
11150255736Sdavidch
11151255736Sdavidch    if (IS_MF_SD(sc)) {
11152255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11153255736Sdavidch    }
11154255736Sdavidch
11155266979Smarcel    if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11156255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11157255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11158255736Sdavidch    }
11159255736Sdavidch
11160255736Sdavidch    if (leading) {
11161255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11162255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11163255736Sdavidch    }
11164255736Sdavidch
11165255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11166255736Sdavidch
11167255736Sdavidch    /* merge with common flags */
11168255736Sdavidch    return (flags | bxe_get_common_flags(sc, fp, TRUE));
11169255736Sdavidch}
11170255736Sdavidch
11171255736Sdavidchstatic void
11172255736Sdavidchbxe_pf_q_prep_general(struct bxe_softc                  *sc,
11173255736Sdavidch                      struct bxe_fastpath               *fp,
11174255736Sdavidch                      struct ecore_general_setup_params *gen_init,
11175255736Sdavidch                      uint8_t                           cos)
11176255736Sdavidch{
11177255736Sdavidch    gen_init->stat_id = bxe_stats_id(fp);
11178255736Sdavidch    gen_init->spcl_id = fp->cl_id;
11179255736Sdavidch    gen_init->mtu = sc->mtu;
11180255736Sdavidch    gen_init->cos = cos;
11181255736Sdavidch}
11182255736Sdavidch
11183255736Sdavidchstatic void
11184255736Sdavidchbxe_pf_rx_q_prep(struct bxe_softc              *sc,
11185255736Sdavidch                 struct bxe_fastpath           *fp,
11186255736Sdavidch                 struct rxq_pause_params       *pause,
11187255736Sdavidch                 struct ecore_rxq_setup_params *rxq_init)
11188255736Sdavidch{
11189255736Sdavidch    uint8_t max_sge = 0;
11190255736Sdavidch    uint16_t sge_sz = 0;
11191255736Sdavidch    uint16_t tpa_agg_size = 0;
11192255736Sdavidch
11193283274Sdavidcs    pause->sge_th_lo = SGE_TH_LO(sc);
11194283274Sdavidcs    pause->sge_th_hi = SGE_TH_HI(sc);
11195255736Sdavidch
11196283274Sdavidcs    /* validate SGE ring has enough to cross high threshold */
11197283274Sdavidcs    if (sc->dropless_fc &&
11198255736Sdavidch            (pause->sge_th_hi + FW_PREFETCH_CNT) >
11199255736Sdavidch            (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11200283274Sdavidcs        BLOGW(sc, "sge ring threshold limit\n");
11201283274Sdavidcs    }
11202255736Sdavidch
11203283274Sdavidcs    /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11204283274Sdavidcs    tpa_agg_size = (2 * sc->mtu);
11205283274Sdavidcs    if (tpa_agg_size < sc->max_aggregation_size) {
11206283274Sdavidcs        tpa_agg_size = sc->max_aggregation_size;
11207283274Sdavidcs    }
11208255736Sdavidch
11209283274Sdavidcs    max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11210283274Sdavidcs    max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11211255736Sdavidch                   (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11212283274Sdavidcs    sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11213255736Sdavidch
11214255736Sdavidch    /* pause - not for e1 */
11215255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11216255736Sdavidch        pause->bd_th_lo = BD_TH_LO(sc);
11217255736Sdavidch        pause->bd_th_hi = BD_TH_HI(sc);
11218255736Sdavidch
11219255736Sdavidch        pause->rcq_th_lo = RCQ_TH_LO(sc);
11220255736Sdavidch        pause->rcq_th_hi = RCQ_TH_HI(sc);
11221255736Sdavidch
11222255736Sdavidch        /* validate rings have enough entries to cross high thresholds */
11223255736Sdavidch        if (sc->dropless_fc &&
11224255736Sdavidch            pause->bd_th_hi + FW_PREFETCH_CNT >
11225255736Sdavidch            sc->rx_ring_size) {
11226255736Sdavidch            BLOGW(sc, "rx bd ring threshold limit\n");
11227255736Sdavidch        }
11228255736Sdavidch
11229255736Sdavidch        if (sc->dropless_fc &&
11230255736Sdavidch            pause->rcq_th_hi + FW_PREFETCH_CNT >
11231255736Sdavidch            RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11232255736Sdavidch            BLOGW(sc, "rcq ring threshold limit\n");
11233255736Sdavidch        }
11234255736Sdavidch
11235255736Sdavidch        pause->pri_map = 1;
11236255736Sdavidch    }
11237255736Sdavidch
11238255736Sdavidch    /* rxq setup */
11239255736Sdavidch    rxq_init->dscr_map   = fp->rx_dma.paddr;
11240255736Sdavidch    rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11241255736Sdavidch    rxq_init->rcq_map    = fp->rcq_dma.paddr;
11242255736Sdavidch    rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11243255736Sdavidch
11244255736Sdavidch    /*
11245255736Sdavidch     * This should be a maximum number of data bytes that may be
11246255736Sdavidch     * placed on the BD (not including paddings).
11247255736Sdavidch     */
11248255736Sdavidch    rxq_init->buf_sz = (fp->rx_buf_size -
11249255736Sdavidch                        IP_HEADER_ALIGNMENT_PADDING);
11250255736Sdavidch
11251255736Sdavidch    rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11252255736Sdavidch    rxq_init->tpa_agg_sz      = tpa_agg_size;
11253255736Sdavidch    rxq_init->sge_buf_sz      = sge_sz;
11254255736Sdavidch    rxq_init->max_sges_pkt    = max_sge;
11255255736Sdavidch    rxq_init->rss_engine_id   = SC_FUNC(sc);
11256255736Sdavidch    rxq_init->mcast_engine_id = SC_FUNC(sc);
11257255736Sdavidch
11258255736Sdavidch    /*
11259255736Sdavidch     * Maximum number or simultaneous TPA aggregation for this Queue.
11260255736Sdavidch     * For PF Clients it should be the maximum available number.
11261255736Sdavidch     * VF driver(s) may want to define it to a smaller value.
11262255736Sdavidch     */
11263255736Sdavidch    rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11264255736Sdavidch
11265255736Sdavidch    rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11266255736Sdavidch    rxq_init->fw_sb_id = fp->fw_sb_id;
11267255736Sdavidch
11268255736Sdavidch    rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11269255736Sdavidch
11270255736Sdavidch    /*
11271255736Sdavidch     * configure silent vlan removal
11272255736Sdavidch     * if multi function mode is afex, then mask default vlan
11273255736Sdavidch     */
11274255736Sdavidch    if (IS_MF_AFEX(sc)) {
11275255736Sdavidch        rxq_init->silent_removal_value =
11276255736Sdavidch            sc->devinfo.mf_info.afex_def_vlan_tag;
11277255736Sdavidch        rxq_init->silent_removal_mask = EVL_VLID_MASK;
11278255736Sdavidch    }
11279255736Sdavidch}
11280255736Sdavidch
11281255736Sdavidchstatic void
11282255736Sdavidchbxe_pf_tx_q_prep(struct bxe_softc              *sc,
11283255736Sdavidch                 struct bxe_fastpath           *fp,
11284255736Sdavidch                 struct ecore_txq_setup_params *txq_init,
11285255736Sdavidch                 uint8_t                       cos)
11286255736Sdavidch{
11287255736Sdavidch    /*
11288255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
11289255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11290255736Sdavidch     * fp->txdata[cos]->tx_dma.paddr;
11291255736Sdavidch     */
11292255736Sdavidch    txq_init->dscr_map     = fp->tx_dma.paddr;
11293255736Sdavidch    txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11294255736Sdavidch    txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11295255736Sdavidch    txq_init->fw_sb_id     = fp->fw_sb_id;
11296255736Sdavidch
11297255736Sdavidch    /*
11298255736Sdavidch     * set the TSS leading client id for TX classfication to the
11299255736Sdavidch     * leading RSS client id
11300255736Sdavidch     */
11301255736Sdavidch    txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11302255736Sdavidch}
11303255736Sdavidch
11304255736Sdavidch/*
11305255736Sdavidch * This function performs 2 steps in a queue state machine:
11306255736Sdavidch *   1) RESET->INIT
11307255736Sdavidch *   2) INIT->SETUP
11308255736Sdavidch */
11309255736Sdavidchstatic int
11310255736Sdavidchbxe_setup_queue(struct bxe_softc    *sc,
11311255736Sdavidch                struct bxe_fastpath *fp,
11312255736Sdavidch                uint8_t             leading)
11313255736Sdavidch{
11314255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
11315255736Sdavidch    struct ecore_queue_setup_params *setup_params =
11316255736Sdavidch                        &q_params.params.setup;
11317255736Sdavidch    int rc;
11318255736Sdavidch
11319255736Sdavidch    BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11320255736Sdavidch
11321255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11322255736Sdavidch
11323255736Sdavidch    q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11324255736Sdavidch
11325255736Sdavidch    /* we want to wait for completion in this context */
11326255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11327255736Sdavidch
11328255736Sdavidch    /* prepare the INIT parameters */
11329255736Sdavidch    bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11330255736Sdavidch
11331255736Sdavidch    /* Set the command */
11332255736Sdavidch    q_params.cmd = ECORE_Q_CMD_INIT;
11333255736Sdavidch
11334255736Sdavidch    /* Change the state to INIT */
11335255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11336255736Sdavidch    if (rc) {
11337295830Sdavidcs        BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11338255736Sdavidch        return (rc);
11339255736Sdavidch    }
11340255736Sdavidch
11341255736Sdavidch    BLOGD(sc, DBG_LOAD, "init complete\n");
11342255736Sdavidch
11343255736Sdavidch    /* now move the Queue to the SETUP state */
11344255736Sdavidch    memset(setup_params, 0, sizeof(*setup_params));
11345255736Sdavidch
11346255736Sdavidch    /* set Queue flags */
11347255736Sdavidch    setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11348255736Sdavidch
11349255736Sdavidch    /* set general SETUP parameters */
11350255736Sdavidch    bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11351255736Sdavidch                          FIRST_TX_COS_INDEX);
11352255736Sdavidch
11353255736Sdavidch    bxe_pf_rx_q_prep(sc, fp,
11354255736Sdavidch                     &setup_params->pause_params,
11355255736Sdavidch                     &setup_params->rxq_params);
11356255736Sdavidch
11357255736Sdavidch    bxe_pf_tx_q_prep(sc, fp,
11358255736Sdavidch                     &setup_params->txq_params,
11359255736Sdavidch                     FIRST_TX_COS_INDEX);
11360255736Sdavidch
11361255736Sdavidch    /* Set the command */
11362255736Sdavidch    q_params.cmd = ECORE_Q_CMD_SETUP;
11363255736Sdavidch
11364255736Sdavidch    /* change the state to SETUP */
11365255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11366255736Sdavidch    if (rc) {
11367295830Sdavidcs        BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11368255736Sdavidch        return (rc);
11369255736Sdavidch    }
11370255736Sdavidch
11371255736Sdavidch    return (rc);
11372255736Sdavidch}
11373255736Sdavidch
11374255736Sdavidchstatic int
11375255736Sdavidchbxe_setup_leading(struct bxe_softc *sc)
11376255736Sdavidch{
11377255736Sdavidch    return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11378255736Sdavidch}
11379255736Sdavidch
11380255736Sdavidchstatic int
11381255736Sdavidchbxe_config_rss_pf(struct bxe_softc            *sc,
11382255736Sdavidch                  struct ecore_rss_config_obj *rss_obj,
11383255736Sdavidch                  uint8_t                     config_hash)
11384255736Sdavidch{
11385255736Sdavidch    struct ecore_config_rss_params params = { NULL };
11386255736Sdavidch    int i;
11387255736Sdavidch
11388255736Sdavidch    /*
11389255736Sdavidch     * Although RSS is meaningless when there is a single HW queue we
11390255736Sdavidch     * still need it enabled in order to have HW Rx hash generated.
11391255736Sdavidch     */
11392255736Sdavidch
11393255736Sdavidch    params.rss_obj = rss_obj;
11394255736Sdavidch
11395255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11396255736Sdavidch
11397255736Sdavidch    bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11398255736Sdavidch
11399255736Sdavidch    /* RSS configuration */
11400255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11401255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11402255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11403255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11404255736Sdavidch    if (rss_obj->udp_rss_v4) {
11405255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11406255736Sdavidch    }
11407255736Sdavidch    if (rss_obj->udp_rss_v6) {
11408255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11409255736Sdavidch    }
11410255736Sdavidch
11411255736Sdavidch    /* Hash bits */
11412255736Sdavidch    params.rss_result_mask = MULTI_MASK;
11413255736Sdavidch
11414255736Sdavidch    memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11415255736Sdavidch
11416255736Sdavidch    if (config_hash) {
11417255736Sdavidch        /* RSS keys */
11418255736Sdavidch        for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11419255736Sdavidch            params.rss_key[i] = arc4random();
11420255736Sdavidch        }
11421255736Sdavidch
11422255736Sdavidch        bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11423255736Sdavidch    }
11424255736Sdavidch
11425255736Sdavidch    return (ecore_config_rss(sc, &params));
11426255736Sdavidch}
11427255736Sdavidch
11428255736Sdavidchstatic int
11429255736Sdavidchbxe_config_rss_eth(struct bxe_softc *sc,
11430255736Sdavidch                   uint8_t          config_hash)
11431255736Sdavidch{
11432255736Sdavidch    return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11433255736Sdavidch}
11434255736Sdavidch
11435255736Sdavidchstatic int
11436255736Sdavidchbxe_init_rss_pf(struct bxe_softc *sc)
11437255736Sdavidch{
11438255736Sdavidch    uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11439255736Sdavidch    int i;
11440255736Sdavidch
11441255736Sdavidch    /*
11442255736Sdavidch     * Prepare the initial contents of the indirection table if
11443255736Sdavidch     * RSS is enabled
11444255736Sdavidch     */
11445255736Sdavidch    for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11446255736Sdavidch        sc->rss_conf_obj.ind_table[i] =
11447255736Sdavidch            (sc->fp->cl_id + (i % num_eth_queues));
11448255736Sdavidch    }
11449255736Sdavidch
11450255736Sdavidch    if (sc->udp_rss) {
11451255736Sdavidch        sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11452255736Sdavidch    }
11453255736Sdavidch
11454255736Sdavidch    /*
11455255736Sdavidch     * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11456255736Sdavidch     * per-port, so if explicit configuration is needed, do it only
11457255736Sdavidch     * for a PMF.
11458255736Sdavidch     *
11459255736Sdavidch     * For 57712 and newer it's a per-function configuration.
11460255736Sdavidch     */
11461255736Sdavidch    return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11462255736Sdavidch}
11463255736Sdavidch
11464255736Sdavidchstatic int
11465255736Sdavidchbxe_set_mac_one(struct bxe_softc          *sc,
11466255736Sdavidch                uint8_t                   *mac,
11467255736Sdavidch                struct ecore_vlan_mac_obj *obj,
11468255736Sdavidch                uint8_t                   set,
11469255736Sdavidch                int                       mac_type,
11470255736Sdavidch                unsigned long             *ramrod_flags)
11471255736Sdavidch{
11472255736Sdavidch    struct ecore_vlan_mac_ramrod_params ramrod_param;
11473255736Sdavidch    int rc;
11474255736Sdavidch
11475255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
11476255736Sdavidch
11477255736Sdavidch    /* fill in general parameters */
11478255736Sdavidch    ramrod_param.vlan_mac_obj = obj;
11479255736Sdavidch    ramrod_param.ramrod_flags = *ramrod_flags;
11480255736Sdavidch
11481255736Sdavidch    /* fill a user request section if needed */
11482255736Sdavidch    if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11483255736Sdavidch        memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11484255736Sdavidch
11485255736Sdavidch        bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11486255736Sdavidch
11487255736Sdavidch        /* Set the command: ADD or DEL */
11488255736Sdavidch        ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11489255736Sdavidch                                            ECORE_VLAN_MAC_DEL;
11490255736Sdavidch    }
11491255736Sdavidch
11492255736Sdavidch    rc = ecore_config_vlan_mac(sc, &ramrod_param);
11493255736Sdavidch
11494255736Sdavidch    if (rc == ECORE_EXISTS) {
11495255736Sdavidch        BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11496255736Sdavidch        /* do not treat adding same MAC as error */
11497255736Sdavidch        rc = 0;
11498255736Sdavidch    } else if (rc < 0) {
11499255736Sdavidch        BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11500255736Sdavidch    }
11501255736Sdavidch
11502255736Sdavidch    return (rc);
11503255736Sdavidch}
11504255736Sdavidch
11505255736Sdavidchstatic int
11506255736Sdavidchbxe_set_eth_mac(struct bxe_softc *sc,
11507255736Sdavidch                uint8_t          set)
11508255736Sdavidch{
11509255736Sdavidch    unsigned long ramrod_flags = 0;
11510255736Sdavidch
11511255736Sdavidch    BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11512255736Sdavidch
11513255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11514255736Sdavidch
11515255736Sdavidch    /* Eth MAC is set on RSS leading client (fp[0]) */
11516255736Sdavidch    return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11517255736Sdavidch                            &sc->sp_objs->mac_obj,
11518255736Sdavidch                            set, ECORE_ETH_MAC, &ramrod_flags));
11519255736Sdavidch}
11520255736Sdavidch
11521255736Sdavidchstatic int
11522255736Sdavidchbxe_get_cur_phy_idx(struct bxe_softc *sc)
11523255736Sdavidch{
11524255736Sdavidch    uint32_t sel_phy_idx = 0;
11525255736Sdavidch
11526255736Sdavidch    if (sc->link_params.num_phys <= 1) {
11527255736Sdavidch        return (ELINK_INT_PHY);
11528255736Sdavidch    }
11529255736Sdavidch
11530255736Sdavidch    if (sc->link_vars.link_up) {
11531255736Sdavidch        sel_phy_idx = ELINK_EXT_PHY1;
11532255736Sdavidch        /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11533255736Sdavidch        if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11534255736Sdavidch            (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11535255736Sdavidch             ELINK_SUPPORTED_FIBRE))
11536255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11537255736Sdavidch    } else {
11538255736Sdavidch        switch (elink_phy_selection(&sc->link_params)) {
11539255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11540255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11541255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11542255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY1;
11543255736Sdavidch               break;
11544255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11545255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11546255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY2;
11547255736Sdavidch               break;
11548255736Sdavidch        }
11549255736Sdavidch    }
11550255736Sdavidch
11551255736Sdavidch    return (sel_phy_idx);
11552255736Sdavidch}
11553255736Sdavidch
11554255736Sdavidchstatic int
11555255736Sdavidchbxe_get_link_cfg_idx(struct bxe_softc *sc)
11556255736Sdavidch{
11557255736Sdavidch    uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11558255736Sdavidch
11559255736Sdavidch    /*
11560255736Sdavidch     * The selected activated PHY is always after swapping (in case PHY
11561255736Sdavidch     * swapping is enabled). So when swapping is enabled, we need to reverse
11562255736Sdavidch     * the configuration
11563255736Sdavidch     */
11564255736Sdavidch
11565255736Sdavidch    if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11566255736Sdavidch        if (sel_phy_idx == ELINK_EXT_PHY1)
11567255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11568255736Sdavidch        else if (sel_phy_idx == ELINK_EXT_PHY2)
11569255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY1;
11570255736Sdavidch    }
11571255736Sdavidch
11572255736Sdavidch    return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11573255736Sdavidch}
11574255736Sdavidch
11575255736Sdavidchstatic void
11576255736Sdavidchbxe_set_requested_fc(struct bxe_softc *sc)
11577255736Sdavidch{
11578255736Sdavidch    /*
11579255736Sdavidch     * Initialize link parameters structure variables
11580255736Sdavidch     * It is recommended to turn off RX FC for jumbo frames
11581255736Sdavidch     * for better performance
11582255736Sdavidch     */
11583255736Sdavidch    if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11584255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11585255736Sdavidch    } else {
11586255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11587255736Sdavidch    }
11588255736Sdavidch}
11589255736Sdavidch
11590255736Sdavidchstatic void
11591255736Sdavidchbxe_calc_fc_adv(struct bxe_softc *sc)
11592255736Sdavidch{
11593255736Sdavidch    uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11594255736Sdavidch    switch (sc->link_vars.ieee_fc &
11595255736Sdavidch            MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11596255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
11597255736Sdavidch    default:
11598255736Sdavidch        sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11599255736Sdavidch                                           ADVERTISED_Pause);
11600255736Sdavidch        break;
11601255736Sdavidch
11602255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11603255736Sdavidch        sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11604255736Sdavidch                                          ADVERTISED_Pause);
11605255736Sdavidch        break;
11606255736Sdavidch
11607255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11608255736Sdavidch        sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11609255736Sdavidch        break;
11610255736Sdavidch    }
11611255736Sdavidch}
11612255736Sdavidch
11613255736Sdavidchstatic uint16_t
11614255736Sdavidchbxe_get_mf_speed(struct bxe_softc *sc)
11615255736Sdavidch{
11616255736Sdavidch    uint16_t line_speed = sc->link_vars.line_speed;
11617255736Sdavidch    if (IS_MF(sc)) {
11618255736Sdavidch        uint16_t maxCfg =
11619255736Sdavidch            bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11620255736Sdavidch
11621255736Sdavidch        /* calculate the current MAX line speed limit for the MF devices */
11622255736Sdavidch        if (IS_MF_SI(sc)) {
11623255736Sdavidch            line_speed = (line_speed * maxCfg) / 100;
11624255736Sdavidch        } else { /* SD mode */
11625255736Sdavidch            uint16_t vn_max_rate = maxCfg * 100;
11626255736Sdavidch
11627255736Sdavidch            if (vn_max_rate < line_speed) {
11628255736Sdavidch                line_speed = vn_max_rate;
11629255736Sdavidch            }
11630255736Sdavidch        }
11631255736Sdavidch    }
11632255736Sdavidch
11633255736Sdavidch    return (line_speed);
11634255736Sdavidch}
11635255736Sdavidch
11636255736Sdavidchstatic void
11637255736Sdavidchbxe_fill_report_data(struct bxe_softc            *sc,
11638255736Sdavidch                     struct bxe_link_report_data *data)
11639255736Sdavidch{
11640255736Sdavidch    uint16_t line_speed = bxe_get_mf_speed(sc);
11641255736Sdavidch
11642255736Sdavidch    memset(data, 0, sizeof(*data));
11643255736Sdavidch
11644255736Sdavidch    /* fill the report data with the effective line speed */
11645255736Sdavidch    data->line_speed = line_speed;
11646255736Sdavidch
11647255736Sdavidch    /* Link is down */
11648255736Sdavidch    if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11649255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11650255736Sdavidch    }
11651255736Sdavidch
11652255736Sdavidch    /* Full DUPLEX */
11653255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
11654255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11655255736Sdavidch    }
11656255736Sdavidch
11657255736Sdavidch    /* Rx Flow Control is ON */
11658255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11659255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11660255736Sdavidch    }
11661255736Sdavidch
11662255736Sdavidch    /* Tx Flow Control is ON */
11663255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11664255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11665255736Sdavidch    }
11666255736Sdavidch}
11667255736Sdavidch
11668255736Sdavidch/* report link status to OS, should be called under phy_lock */
11669255736Sdavidchstatic void
11670255736Sdavidchbxe_link_report_locked(struct bxe_softc *sc)
11671255736Sdavidch{
11672255736Sdavidch    struct bxe_link_report_data cur_data;
11673255736Sdavidch
11674255736Sdavidch    /* reread mf_cfg */
11675255736Sdavidch    if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11676255736Sdavidch        bxe_read_mf_cfg(sc);
11677255736Sdavidch    }
11678255736Sdavidch
11679255736Sdavidch    /* Read the current link report info */
11680255736Sdavidch    bxe_fill_report_data(sc, &cur_data);
11681255736Sdavidch
11682255736Sdavidch    /* Don't report link down or exactly the same link status twice */
11683255736Sdavidch    if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11684255736Sdavidch        (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11685255736Sdavidch                      &sc->last_reported_link.link_report_flags) &&
11686255736Sdavidch         bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11687255736Sdavidch                      &cur_data.link_report_flags))) {
11688255736Sdavidch        return;
11689255736Sdavidch    }
11690255736Sdavidch
11691255736Sdavidch    sc->link_cnt++;
11692255736Sdavidch
11693255736Sdavidch    /* report new link params and remember the state for the next time */
11694255736Sdavidch    memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11695255736Sdavidch
11696255736Sdavidch    if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11697255736Sdavidch                     &cur_data.link_report_flags)) {
11698270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_DOWN);
11699255736Sdavidch        BLOGI(sc, "NIC Link is Down\n");
11700255736Sdavidch    } else {
11701255736Sdavidch        const char *duplex;
11702255736Sdavidch        const char *flow;
11703255736Sdavidch
11704255736Sdavidch        if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11705255736Sdavidch                                   &cur_data.link_report_flags)) {
11706255736Sdavidch            duplex = "full";
11707255736Sdavidch        } else {
11708255736Sdavidch            duplex = "half";
11709255736Sdavidch        }
11710255736Sdavidch
11711255736Sdavidch        /*
11712255736Sdavidch         * Handle the FC at the end so that only these flags would be
11713255736Sdavidch         * possibly set. This way we may easily check if there is no FC
11714255736Sdavidch         * enabled.
11715255736Sdavidch         */
11716255736Sdavidch        if (cur_data.link_report_flags) {
11717255736Sdavidch            if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11718255736Sdavidch                             &cur_data.link_report_flags) &&
11719255736Sdavidch                bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11720255736Sdavidch                             &cur_data.link_report_flags)) {
11721255736Sdavidch                flow = "ON - receive & transmit";
11722255736Sdavidch            } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11723255736Sdavidch                                    &cur_data.link_report_flags) &&
11724255736Sdavidch                       !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11725255736Sdavidch                                     &cur_data.link_report_flags)) {
11726255736Sdavidch                flow = "ON - receive";
11727255736Sdavidch            } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11728255736Sdavidch                                     &cur_data.link_report_flags) &&
11729255736Sdavidch                       bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11730255736Sdavidch                                    &cur_data.link_report_flags)) {
11731255736Sdavidch                flow = "ON - transmit";
11732255736Sdavidch            } else {
11733255736Sdavidch                flow = "none"; /* possible? */
11734255736Sdavidch            }
11735255736Sdavidch        } else {
11736255736Sdavidch            flow = "none";
11737255736Sdavidch        }
11738255736Sdavidch
11739270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_UP);
11740255736Sdavidch        BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11741255736Sdavidch              cur_data.line_speed, duplex, flow);
11742255736Sdavidch    }
11743255736Sdavidch}
11744255736Sdavidch
11745255736Sdavidchstatic void
11746255736Sdavidchbxe_link_report(struct bxe_softc *sc)
11747255736Sdavidch{
11748284335Sdavidcs    bxe_acquire_phy_lock(sc);
11749255736Sdavidch    bxe_link_report_locked(sc);
11750284335Sdavidcs    bxe_release_phy_lock(sc);
11751255736Sdavidch}
11752255736Sdavidch
11753255736Sdavidchstatic void
11754255736Sdavidchbxe_link_status_update(struct bxe_softc *sc)
11755255736Sdavidch{
11756255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
11757255736Sdavidch        return;
11758255736Sdavidch    }
11759255736Sdavidch
11760255736Sdavidch    if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11761255736Sdavidch        elink_link_status_update(&sc->link_params, &sc->link_vars);
11762255736Sdavidch    } else {
11763255736Sdavidch        sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11764255736Sdavidch                                  ELINK_SUPPORTED_10baseT_Full |
11765255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Half |
11766255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Full |
11767255736Sdavidch                                  ELINK_SUPPORTED_1000baseT_Full |
11768255736Sdavidch                                  ELINK_SUPPORTED_2500baseX_Full |
11769255736Sdavidch                                  ELINK_SUPPORTED_10000baseT_Full |
11770255736Sdavidch                                  ELINK_SUPPORTED_TP |
11771255736Sdavidch                                  ELINK_SUPPORTED_FIBRE |
11772255736Sdavidch                                  ELINK_SUPPORTED_Autoneg |
11773255736Sdavidch                                  ELINK_SUPPORTED_Pause |
11774255736Sdavidch                                  ELINK_SUPPORTED_Asym_Pause);
11775255736Sdavidch        sc->port.advertising[0] = sc->port.supported[0];
11776255736Sdavidch
11777255736Sdavidch        sc->link_params.sc                = sc;
11778255736Sdavidch        sc->link_params.port              = SC_PORT(sc);
11779255736Sdavidch        sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11780255736Sdavidch        sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11781255736Sdavidch        sc->link_params.req_line_speed[0] = SPEED_10000;
11782255736Sdavidch        sc->link_params.speed_cap_mask[0] = 0x7f0000;
11783255736Sdavidch        sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11784255736Sdavidch
11785255736Sdavidch        if (CHIP_REV_IS_FPGA(sc)) {
11786255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11787255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_1000;
11788255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11789255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11790255736Sdavidch        } else {
11791255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11792255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_10000;
11793255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11794255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11795255736Sdavidch        }
11796255736Sdavidch
11797255736Sdavidch        sc->link_vars.link_up = 1;
11798255736Sdavidch
11799255736Sdavidch        sc->link_vars.duplex    = DUPLEX_FULL;
11800255736Sdavidch        sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11801255736Sdavidch
11802255736Sdavidch        if (IS_PF(sc)) {
11803255736Sdavidch            REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11804255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11805255736Sdavidch            bxe_link_report(sc);
11806255736Sdavidch        }
11807255736Sdavidch    }
11808255736Sdavidch
11809255736Sdavidch    if (IS_PF(sc)) {
11810255736Sdavidch        if (sc->link_vars.link_up) {
11811255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11812255736Sdavidch        } else {
11813255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_STOP);
11814255736Sdavidch        }
11815255736Sdavidch        bxe_link_report(sc);
11816255736Sdavidch    } else {
11817255736Sdavidch        bxe_link_report(sc);
11818255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11819255736Sdavidch    }
11820255736Sdavidch}
11821255736Sdavidch
11822255736Sdavidchstatic int
11823255736Sdavidchbxe_initial_phy_init(struct bxe_softc *sc,
11824255736Sdavidch                     int              load_mode)
11825255736Sdavidch{
11826255736Sdavidch    int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
11827255736Sdavidch    uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
11828255736Sdavidch    struct elink_params *lp = &sc->link_params;
11829255736Sdavidch
11830255736Sdavidch    bxe_set_requested_fc(sc);
11831255736Sdavidch
11832255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
11833255736Sdavidch        uint32_t bond = CHIP_BOND_ID(sc);
11834255736Sdavidch        uint32_t feat = 0;
11835255736Sdavidch
11836255736Sdavidch        if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
11837255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11838255736Sdavidch        } else if (bond & 0x4) {
11839255736Sdavidch            if (CHIP_IS_E3(sc)) {
11840255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
11841255736Sdavidch            } else {
11842255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11843255736Sdavidch            }
11844255736Sdavidch        } else if (bond & 0x8) {
11845255736Sdavidch            if (CHIP_IS_E3(sc)) {
11846255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
11847255736Sdavidch            } else {
11848255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11849255736Sdavidch            }
11850255736Sdavidch        }
11851255736Sdavidch
11852255736Sdavidch        /* disable EMAC for E3 and above */
11853255736Sdavidch        if (bond & 0x2) {
11854255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11855255736Sdavidch        }
11856255736Sdavidch
11857255736Sdavidch        sc->link_params.feature_config_flags |= feat;
11858255736Sdavidch    }
11859255736Sdavidch
11860284335Sdavidcs    bxe_acquire_phy_lock(sc);
11861255736Sdavidch
11862255736Sdavidch    if (load_mode == LOAD_DIAG) {
11863255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_XGXS;
11864255736Sdavidch        /* Prefer doing PHY loopback at 10G speed, if possible */
11865255736Sdavidch        if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
11866255736Sdavidch            if (lp->speed_cap_mask[cfg_idx] &
11867255736Sdavidch                PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
11868255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
11869255736Sdavidch            } else {
11870255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
11871255736Sdavidch            }
11872255736Sdavidch        }
11873255736Sdavidch    }
11874255736Sdavidch
11875255736Sdavidch    if (load_mode == LOAD_LOOPBACK_EXT) {
11876255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_EXT;
11877255736Sdavidch    }
11878255736Sdavidch
11879255736Sdavidch    rc = elink_phy_init(&sc->link_params, &sc->link_vars);
11880255736Sdavidch
11881284335Sdavidcs    bxe_release_phy_lock(sc);
11882255736Sdavidch
11883255736Sdavidch    bxe_calc_fc_adv(sc);
11884255736Sdavidch
11885255736Sdavidch    if (sc->link_vars.link_up) {
11886255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11887255736Sdavidch        bxe_link_report(sc);
11888255736Sdavidch    }
11889255736Sdavidch
11890255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
11891255736Sdavidch        bxe_periodic_start(sc);
11892255736Sdavidch    }
11893255736Sdavidch
11894255736Sdavidch    sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
11895255736Sdavidch    return (rc);
11896255736Sdavidch}
11897255736Sdavidch
11898255736Sdavidch/* must be called under IF_ADDR_LOCK */
11899284651Sdavidcs
11900255736Sdavidchstatic int
11901284651Sdavidcsbxe_set_mc_list(struct bxe_softc *sc)
11902255736Sdavidch{
11903284651Sdavidcs    struct ecore_mcast_ramrod_params rparam = { NULL };
11904284651Sdavidcs    int rc = 0;
11905255736Sdavidch    int mc_count = 0;
11906266979Smarcel    int mcnt, i;
11907284651Sdavidcs    struct ecore_mcast_list_elem *mc_mac, *mc_mac_start;
11908266979Smarcel    unsigned char *mta;
11909284651Sdavidcs    if_t ifp = sc->ifp;
11910255736Sdavidch
11911266979Smarcel    mc_count = if_multiaddr_count(ifp, -1);/* XXX they don't have a limit */
11912284651Sdavidcs    if (!mc_count)
11913255736Sdavidch        return (0);
11914255736Sdavidch
11915266979Smarcel    mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN *
11916266979Smarcel            mc_count, M_DEVBUF, M_NOWAIT);
11917266979Smarcel
11918266979Smarcel    if(mta == NULL) {
11919266979Smarcel        BLOGE(sc, "Failed to allocate temp mcast list\n");
11920266979Smarcel        return (-1);
11921266979Smarcel    }
11922284470Sdavidcs    bzero(mta, (sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count));
11923266979Smarcel
11924284651Sdavidcs    mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF, (M_NOWAIT | M_ZERO));
11925284651Sdavidcs    mc_mac_start = mc_mac;
11926284651Sdavidcs
11927255736Sdavidch    if (!mc_mac) {
11928266979Smarcel        free(mta, M_DEVBUF);
11929255736Sdavidch        BLOGE(sc, "Failed to allocate temp mcast list\n");
11930255736Sdavidch        return (-1);
11931255736Sdavidch    }
11932284470Sdavidcs    bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
11933255736Sdavidch
11934284651Sdavidcs    /* mta and mcnt not expected to be  different */
11935284651Sdavidcs    if_multiaddr_array(ifp, mta, &mcnt, mc_count);
11936284651Sdavidcs
11937284651Sdavidcs
11938284651Sdavidcs    rparam.mcast_obj = &sc->mcast_obj;
11939284651Sdavidcs    ECORE_LIST_INIT(&rparam.mcast_list);
11940284651Sdavidcs
11941266979Smarcel    for(i=0; i< mcnt; i++) {
11942255736Sdavidch
11943284651Sdavidcs        mc_mac->mac = (uint8_t *)(mta + (i * ETHER_ADDR_LEN));
11944284651Sdavidcs        ECORE_LIST_PUSH_TAIL(&mc_mac->link, &rparam.mcast_list);
11945255736Sdavidch
11946255736Sdavidch        BLOGD(sc, DBG_LOAD,
11947255736Sdavidch              "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
11948255736Sdavidch              mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
11949255736Sdavidch              mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
11950255736Sdavidch
11951255736Sdavidch        mc_mac++;
11952255736Sdavidch    }
11953284651Sdavidcs    rparam.mcast_list_len = mc_count;
11954255736Sdavidch
11955255736Sdavidch    BXE_MCAST_LOCK(sc);
11956255736Sdavidch
11957255736Sdavidch    /* first, clear all configured multicast MACs */
11958255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
11959255736Sdavidch    if (rc < 0) {
11960255736Sdavidch        BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
11961284470Sdavidcs        BXE_MCAST_UNLOCK(sc);
11962284651Sdavidcs    	free(mc_mac_start, M_DEVBUF);
11963284651Sdavidcs        free(mta, M_DEVBUF);
11964255736Sdavidch        return (rc);
11965255736Sdavidch    }
11966255736Sdavidch
11967255736Sdavidch    /* Now add the new MACs */
11968255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
11969255736Sdavidch    if (rc < 0) {
11970255736Sdavidch        BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
11971255736Sdavidch    }
11972255736Sdavidch
11973255736Sdavidch    BXE_MCAST_UNLOCK(sc);
11974255736Sdavidch
11975284651Sdavidcs    free(mc_mac_start, M_DEVBUF);
11976284651Sdavidcs    free(mta, M_DEVBUF);
11977284651Sdavidcs
11978255736Sdavidch    return (rc);
11979255736Sdavidch}
11980255736Sdavidch
11981255736Sdavidchstatic int
11982255736Sdavidchbxe_set_uc_list(struct bxe_softc *sc)
11983255736Sdavidch{
11984266979Smarcel    if_t ifp = sc->ifp;
11985255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
11986255736Sdavidch    struct ifaddr *ifa;
11987255736Sdavidch    unsigned long ramrod_flags = 0;
11988255736Sdavidch    int rc;
11989255736Sdavidch
11990255736Sdavidch#if __FreeBSD_version < 800000
11991255736Sdavidch    IF_ADDR_LOCK(ifp);
11992255736Sdavidch#else
11993270876Sglebius    if_addr_rlock(ifp);
11994255736Sdavidch#endif
11995255736Sdavidch
11996255736Sdavidch    /* first schedule a cleanup up of old configuration */
11997255736Sdavidch    rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
11998255736Sdavidch    if (rc < 0) {
11999255736Sdavidch        BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12000255736Sdavidch#if __FreeBSD_version < 800000
12001255736Sdavidch        IF_ADDR_UNLOCK(ifp);
12002255736Sdavidch#else
12003270876Sglebius        if_addr_runlock(ifp);
12004255736Sdavidch#endif
12005255736Sdavidch        return (rc);
12006255736Sdavidch    }
12007255736Sdavidch
12008266979Smarcel    ifa = if_getifaddr(ifp); /* XXX Is this structure */
12009255736Sdavidch    while (ifa) {
12010255736Sdavidch        if (ifa->ifa_addr->sa_family != AF_LINK) {
12011255736Sdavidch            ifa = TAILQ_NEXT(ifa, ifa_link);
12012255736Sdavidch            continue;
12013255736Sdavidch        }
12014255736Sdavidch
12015255736Sdavidch        rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12016255736Sdavidch                             mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12017255736Sdavidch        if (rc == -EEXIST) {
12018255736Sdavidch            BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12019255736Sdavidch            /* do not treat adding same MAC as an error */
12020255736Sdavidch            rc = 0;
12021255736Sdavidch        } else if (rc < 0) {
12022255736Sdavidch            BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12023255736Sdavidch#if __FreeBSD_version < 800000
12024255736Sdavidch            IF_ADDR_UNLOCK(ifp);
12025255736Sdavidch#else
12026270876Sglebius            if_addr_runlock(ifp);
12027255736Sdavidch#endif
12028255736Sdavidch            return (rc);
12029255736Sdavidch        }
12030255736Sdavidch
12031255736Sdavidch        ifa = TAILQ_NEXT(ifa, ifa_link);
12032255736Sdavidch    }
12033255736Sdavidch
12034255736Sdavidch#if __FreeBSD_version < 800000
12035255736Sdavidch    IF_ADDR_UNLOCK(ifp);
12036255736Sdavidch#else
12037270876Sglebius    if_addr_runlock(ifp);
12038255736Sdavidch#endif
12039255736Sdavidch
12040255736Sdavidch    /* Execute the pending commands */
12041255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
12042255736Sdavidch    return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12043255736Sdavidch                            ECORE_UC_LIST_MAC, &ramrod_flags));
12044255736Sdavidch}
12045255736Sdavidch
12046255736Sdavidchstatic void
12047284335Sdavidcsbxe_set_rx_mode(struct bxe_softc *sc)
12048255736Sdavidch{
12049266979Smarcel    if_t ifp = sc->ifp;
12050255736Sdavidch    uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12051255736Sdavidch
12052255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
12053255736Sdavidch        BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12054255736Sdavidch        return;
12055255736Sdavidch    }
12056255736Sdavidch
12057266979Smarcel    BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12058255736Sdavidch
12059266979Smarcel    if (if_getflags(ifp) & IFF_PROMISC) {
12060255736Sdavidch        rx_mode = BXE_RX_MODE_PROMISC;
12061266979Smarcel    } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12062266979Smarcel               ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12063255736Sdavidch                CHIP_IS_E1(sc))) {
12064255736Sdavidch        rx_mode = BXE_RX_MODE_ALLMULTI;
12065255736Sdavidch    } else {
12066255736Sdavidch        if (IS_PF(sc)) {
12067255736Sdavidch            /* some multicasts */
12068255736Sdavidch            if (bxe_set_mc_list(sc) < 0) {
12069255736Sdavidch                rx_mode = BXE_RX_MODE_ALLMULTI;
12070255736Sdavidch            }
12071255736Sdavidch            if (bxe_set_uc_list(sc) < 0) {
12072255736Sdavidch                rx_mode = BXE_RX_MODE_PROMISC;
12073255736Sdavidch            }
12074255736Sdavidch        }
12075255736Sdavidch    }
12076255736Sdavidch
12077255736Sdavidch    sc->rx_mode = rx_mode;
12078255736Sdavidch
12079255736Sdavidch    /* schedule the rx_mode command */
12080255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12081255736Sdavidch        BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12082255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12083255736Sdavidch        return;
12084255736Sdavidch    }
12085255736Sdavidch
12086255736Sdavidch    if (IS_PF(sc)) {
12087255736Sdavidch        bxe_set_storm_rx_mode(sc);
12088255736Sdavidch    }
12089255736Sdavidch}
12090255736Sdavidch
12091255736Sdavidch
12092255736Sdavidch/* update flags in shmem */
12093255736Sdavidchstatic void
12094255736Sdavidchbxe_update_drv_flags(struct bxe_softc *sc,
12095255736Sdavidch                     uint32_t         flags,
12096255736Sdavidch                     uint32_t         set)
12097255736Sdavidch{
12098255736Sdavidch    uint32_t drv_flags;
12099255736Sdavidch
12100255736Sdavidch    if (SHMEM2_HAS(sc, drv_flags)) {
12101255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12102255736Sdavidch        drv_flags = SHMEM2_RD(sc, drv_flags);
12103255736Sdavidch
12104255736Sdavidch        if (set) {
12105255736Sdavidch            SET_FLAGS(drv_flags, flags);
12106255736Sdavidch        } else {
12107255736Sdavidch            RESET_FLAGS(drv_flags, flags);
12108255736Sdavidch        }
12109255736Sdavidch
12110255736Sdavidch        SHMEM2_WR(sc, drv_flags, drv_flags);
12111255736Sdavidch        BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12112255736Sdavidch
12113255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12114255736Sdavidch    }
12115255736Sdavidch}
12116255736Sdavidch
12117255736Sdavidch/* periodic timer callout routine, only runs when the interface is up */
12118255736Sdavidch
12119255736Sdavidchstatic void
12120255736Sdavidchbxe_periodic_callout_func(void *xsc)
12121255736Sdavidch{
12122255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12123297873Sdavidcs    struct bxe_fastpath *fp;
12124297873Sdavidcs    uint16_t tx_bd_avail;
12125255736Sdavidch    int i;
12126255736Sdavidch
12127255736Sdavidch    if (!BXE_CORE_TRYLOCK(sc)) {
12128255736Sdavidch        /* just bail and try again next time */
12129255736Sdavidch
12130255736Sdavidch        if ((sc->state == BXE_STATE_OPEN) &&
12131255736Sdavidch            (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12132255736Sdavidch            /* schedule the next periodic callout */
12133255736Sdavidch            callout_reset(&sc->periodic_callout, hz,
12134255736Sdavidch                          bxe_periodic_callout_func, sc);
12135255736Sdavidch        }
12136255736Sdavidch
12137255736Sdavidch        return;
12138255736Sdavidch    }
12139255736Sdavidch
12140255736Sdavidch    if ((sc->state != BXE_STATE_OPEN) ||
12141255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12142255736Sdavidch        BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12143255736Sdavidch        BXE_CORE_UNLOCK(sc);
12144255736Sdavidch        return;
12145255736Sdavidch    }
12146255736Sdavidch
12147297873Sdavidcs#if __FreeBSD_version >= 800000
12148297873Sdavidcs
12149297873Sdavidcs    FOR_EACH_QUEUE(sc, i) {
12150297873Sdavidcs        fp = &sc->fp[i];
12151297873Sdavidcs
12152297873Sdavidcs        if (BXE_FP_TX_TRYLOCK(fp)) {
12153297873Sdavidcs            if_t ifp = sc->ifp;
12154297873Sdavidcs            /*
12155297873Sdavidcs             * If interface was stopped due to unavailable
12156297873Sdavidcs             * bds, try to process some tx completions
12157297873Sdavidcs             */
12158297873Sdavidcs            (void) bxe_txeof(sc, fp);
12159297873Sdavidcs
12160297873Sdavidcs            tx_bd_avail = bxe_tx_avail(sc, fp);
12161297873Sdavidcs            if (tx_bd_avail >= BXE_TX_CLEANUP_THRESHOLD) {
12162297873Sdavidcs                bxe_tx_mq_start_locked(sc, ifp, fp, NULL);
12163297873Sdavidcs            }
12164297873Sdavidcs            BXE_FP_TX_UNLOCK(fp);
12165297873Sdavidcs        }
12166297873Sdavidcs    }
12167297873Sdavidcs
12168297873Sdavidcs#else
12169297873Sdavidcs
12170297873Sdavidcs    fp = &sc->fp[0];
12171297873Sdavidcs    if (BXE_FP_TX_TRYLOCK(fp)) {
12172297873Sdavidcs        struct ifnet *ifp = sc->ifnet;
12173297873Sdavidcs        /*
12174297873Sdavidcs         * If interface was stopped due to unavailable
12175297873Sdavidcs         * bds, try to process some tx completions
12176297873Sdavidcs         */
12177297873Sdavidcs        (void) bxe_txeof(sc, fp);
12178297873Sdavidcs
12179297873Sdavidcs        tx_bd_avail = bxe_tx_avail(sc, fp);
12180297873Sdavidcs        if (tx_bd_avail >= BXE_TX_CLEANUP_THRESHOLD) {
12181297873Sdavidcs            bxe_tx_start_locked(sc, ifp, fp);
12182297873Sdavidcs        }
12183297873Sdavidcs
12184297873Sdavidcs        BXE_FP_TX_UNLOCK(fp);
12185297873Sdavidcs    }
12186297873Sdavidcs
12187297873Sdavidcs#endif /* #if __FreeBSD_version >= 800000 */
12188297873Sdavidcs
12189255736Sdavidch    /* Check for TX timeouts on any fastpath. */
12190255736Sdavidch    FOR_EACH_QUEUE(sc, i) {
12191255736Sdavidch        if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12192255736Sdavidch            /* Ruh-Roh, chip was reset! */
12193255736Sdavidch            break;
12194255736Sdavidch        }
12195255736Sdavidch    }
12196255736Sdavidch
12197255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12198255736Sdavidch        /*
12199255736Sdavidch         * This barrier is needed to ensure the ordering between the writing
12200255736Sdavidch         * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12201255736Sdavidch         * the reading here.
12202255736Sdavidch         */
12203255736Sdavidch        mb();
12204255736Sdavidch        if (sc->port.pmf) {
12205284335Sdavidcs	    bxe_acquire_phy_lock(sc);
12206255736Sdavidch            elink_period_func(&sc->link_params, &sc->link_vars);
12207284335Sdavidcs	    bxe_release_phy_lock(sc);
12208255736Sdavidch        }
12209255736Sdavidch    }
12210255736Sdavidch
12211284335Sdavidcs    if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12212255736Sdavidch        int mb_idx = SC_FW_MB_IDX(sc);
12213255736Sdavidch        uint32_t drv_pulse;
12214255736Sdavidch        uint32_t mcp_pulse;
12215255736Sdavidch
12216255736Sdavidch        ++sc->fw_drv_pulse_wr_seq;
12217255736Sdavidch        sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12218255736Sdavidch
12219255736Sdavidch        drv_pulse = sc->fw_drv_pulse_wr_seq;
12220255736Sdavidch        bxe_drv_pulse(sc);
12221255736Sdavidch
12222255736Sdavidch        mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12223255736Sdavidch                     MCP_PULSE_SEQ_MASK);
12224255736Sdavidch
12225255736Sdavidch        /*
12226255736Sdavidch         * The delta between driver pulse and mcp response should
12227255736Sdavidch         * be 1 (before mcp response) or 0 (after mcp response).
12228255736Sdavidch         */
12229255736Sdavidch        if ((drv_pulse != mcp_pulse) &&
12230255736Sdavidch            (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12231255736Sdavidch            /* someone lost a heartbeat... */
12232255736Sdavidch            BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12233255736Sdavidch                  drv_pulse, mcp_pulse);
12234255736Sdavidch        }
12235255736Sdavidch    }
12236255736Sdavidch
12237255736Sdavidch    /* state is BXE_STATE_OPEN */
12238255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12239255736Sdavidch
12240255736Sdavidch    BXE_CORE_UNLOCK(sc);
12241255736Sdavidch
12242255736Sdavidch    if ((sc->state == BXE_STATE_OPEN) &&
12243255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12244255736Sdavidch        /* schedule the next periodic callout */
12245255736Sdavidch        callout_reset(&sc->periodic_callout, hz,
12246255736Sdavidch                      bxe_periodic_callout_func, sc);
12247255736Sdavidch    }
12248255736Sdavidch}
12249255736Sdavidch
12250255736Sdavidchstatic void
12251255736Sdavidchbxe_periodic_start(struct bxe_softc *sc)
12252255736Sdavidch{
12253255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12254255736Sdavidch    callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12255255736Sdavidch}
12256255736Sdavidch
12257255736Sdavidchstatic void
12258255736Sdavidchbxe_periodic_stop(struct bxe_softc *sc)
12259255736Sdavidch{
12260255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12261255736Sdavidch    callout_drain(&sc->periodic_callout);
12262255736Sdavidch}
12263255736Sdavidch
12264255736Sdavidch/* start the controller */
12265255736Sdavidchstatic __noinline int
12266255736Sdavidchbxe_nic_load(struct bxe_softc *sc,
12267255736Sdavidch             int              load_mode)
12268255736Sdavidch{
12269255736Sdavidch    uint32_t val;
12270255736Sdavidch    int load_code = 0;
12271255736Sdavidch    int i, rc = 0;
12272255736Sdavidch
12273255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12274255736Sdavidch
12275255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12276255736Sdavidch
12277255736Sdavidch    sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12278255736Sdavidch
12279255736Sdavidch    if (IS_PF(sc)) {
12280255736Sdavidch        /* must be called before memory allocation and HW init */
12281255736Sdavidch        bxe_ilt_set_info(sc);
12282255736Sdavidch    }
12283255736Sdavidch
12284255736Sdavidch    sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12285255736Sdavidch
12286255736Sdavidch    bxe_set_fp_rx_buf_size(sc);
12287255736Sdavidch
12288255736Sdavidch    if (bxe_alloc_fp_buffers(sc) != 0) {
12289255736Sdavidch        BLOGE(sc, "Failed to allocate fastpath memory\n");
12290255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12291255736Sdavidch        rc = ENOMEM;
12292255736Sdavidch        goto bxe_nic_load_error0;
12293255736Sdavidch    }
12294255736Sdavidch
12295255736Sdavidch    if (bxe_alloc_mem(sc) != 0) {
12296255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12297255736Sdavidch        rc = ENOMEM;
12298255736Sdavidch        goto bxe_nic_load_error0;
12299255736Sdavidch    }
12300255736Sdavidch
12301255736Sdavidch    if (bxe_alloc_fw_stats_mem(sc) != 0) {
12302255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12303255736Sdavidch        rc = ENOMEM;
12304255736Sdavidch        goto bxe_nic_load_error0;
12305255736Sdavidch    }
12306255736Sdavidch
12307255736Sdavidch    if (IS_PF(sc)) {
12308255736Sdavidch        /* set pf load just before approaching the MCP */
12309255736Sdavidch        bxe_set_pf_load(sc);
12310255736Sdavidch
12311255736Sdavidch        /* if MCP exists send load request and analyze response */
12312255736Sdavidch        if (!BXE_NOMCP(sc)) {
12313255736Sdavidch            /* attempt to load pf */
12314255736Sdavidch            if (bxe_nic_load_request(sc, &load_code) != 0) {
12315255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12316255736Sdavidch                rc = ENXIO;
12317255736Sdavidch                goto bxe_nic_load_error1;
12318255736Sdavidch            }
12319255736Sdavidch
12320255736Sdavidch            /* what did the MCP say? */
12321255736Sdavidch            if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12322255736Sdavidch                bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12323255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12324255736Sdavidch                rc = ENXIO;
12325255736Sdavidch                goto bxe_nic_load_error2;
12326255736Sdavidch            }
12327255736Sdavidch        } else {
12328255736Sdavidch            BLOGI(sc, "Device has no MCP!\n");
12329255736Sdavidch            load_code = bxe_nic_load_no_mcp(sc);
12330255736Sdavidch        }
12331255736Sdavidch
12332255736Sdavidch        /* mark PMF if applicable */
12333255736Sdavidch        bxe_nic_load_pmf(sc, load_code);
12334255736Sdavidch
12335255736Sdavidch        /* Init Function state controlling object */
12336255736Sdavidch        bxe_init_func_obj(sc);
12337255736Sdavidch
12338255736Sdavidch        /* Initialize HW */
12339255736Sdavidch        if (bxe_init_hw(sc, load_code) != 0) {
12340255736Sdavidch            BLOGE(sc, "HW init failed\n");
12341255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12342255736Sdavidch            sc->state = BXE_STATE_CLOSED;
12343255736Sdavidch            rc = ENXIO;
12344255736Sdavidch            goto bxe_nic_load_error2;
12345255736Sdavidch        }
12346255736Sdavidch    }
12347255736Sdavidch
12348284335Sdavidcs    /* set ALWAYS_ALIVE bit in shmem */
12349284335Sdavidcs    sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12350284335Sdavidcs    bxe_drv_pulse(sc);
12351284335Sdavidcs    sc->flags |= BXE_NO_PULSE;
12352284335Sdavidcs
12353255736Sdavidch    /* attach interrupts */
12354255736Sdavidch    if (bxe_interrupt_attach(sc) != 0) {
12355255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12356255736Sdavidch        rc = ENXIO;
12357255736Sdavidch        goto bxe_nic_load_error2;
12358255736Sdavidch    }
12359255736Sdavidch
12360255736Sdavidch    bxe_nic_init(sc, load_code);
12361255736Sdavidch
12362255736Sdavidch    /* Init per-function objects */
12363255736Sdavidch    if (IS_PF(sc)) {
12364255736Sdavidch        bxe_init_objs(sc);
12365255736Sdavidch        // XXX bxe_iov_nic_init(sc);
12366255736Sdavidch
12367255736Sdavidch        /* set AFEX default VLAN tag to an invalid value */
12368255736Sdavidch        sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12369255736Sdavidch        // XXX bxe_nic_load_afex_dcc(sc, load_code);
12370255736Sdavidch
12371255736Sdavidch        sc->state = BXE_STATE_OPENING_WAITING_PORT;
12372255736Sdavidch        rc = bxe_func_start(sc);
12373255736Sdavidch        if (rc) {
12374295830Sdavidcs            BLOGE(sc, "Function start failed! rc = %d\n", rc);
12375255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12376255736Sdavidch            sc->state = BXE_STATE_ERROR;
12377255736Sdavidch            goto bxe_nic_load_error3;
12378255736Sdavidch        }
12379255736Sdavidch
12380255736Sdavidch        /* send LOAD_DONE command to MCP */
12381255736Sdavidch        if (!BXE_NOMCP(sc)) {
12382255736Sdavidch            load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12383255736Sdavidch            if (!load_code) {
12384255736Sdavidch                BLOGE(sc, "MCP response failure, aborting\n");
12385255736Sdavidch                sc->state = BXE_STATE_ERROR;
12386255736Sdavidch                rc = ENXIO;
12387255736Sdavidch                goto bxe_nic_load_error3;
12388255736Sdavidch            }
12389255736Sdavidch        }
12390255736Sdavidch
12391255736Sdavidch        rc = bxe_setup_leading(sc);
12392255736Sdavidch        if (rc) {
12393295830Sdavidcs            BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12394255736Sdavidch            sc->state = BXE_STATE_ERROR;
12395255736Sdavidch            goto bxe_nic_load_error3;
12396255736Sdavidch        }
12397255736Sdavidch
12398255736Sdavidch        FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12399255736Sdavidch            rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12400255736Sdavidch            if (rc) {
12401295830Sdavidcs                BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12402255736Sdavidch                sc->state = BXE_STATE_ERROR;
12403255736Sdavidch                goto bxe_nic_load_error3;
12404255736Sdavidch            }
12405255736Sdavidch        }
12406255736Sdavidch
12407255736Sdavidch        rc = bxe_init_rss_pf(sc);
12408255736Sdavidch        if (rc) {
12409255736Sdavidch            BLOGE(sc, "PF RSS init failed\n");
12410255736Sdavidch            sc->state = BXE_STATE_ERROR;
12411255736Sdavidch            goto bxe_nic_load_error3;
12412255736Sdavidch        }
12413255736Sdavidch    }
12414255736Sdavidch    /* XXX VF */
12415255736Sdavidch
12416255736Sdavidch    /* now when Clients are configured we are ready to work */
12417255736Sdavidch    sc->state = BXE_STATE_OPEN;
12418255736Sdavidch
12419255736Sdavidch    /* Configure a ucast MAC */
12420255736Sdavidch    if (IS_PF(sc)) {
12421255736Sdavidch        rc = bxe_set_eth_mac(sc, TRUE);
12422255736Sdavidch    }
12423255736Sdavidch    if (rc) {
12424295830Sdavidcs        BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12425255736Sdavidch        sc->state = BXE_STATE_ERROR;
12426255736Sdavidch        goto bxe_nic_load_error3;
12427255736Sdavidch    }
12428255736Sdavidch
12429255736Sdavidch    if (sc->port.pmf) {
12430255736Sdavidch        rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12431255736Sdavidch        if (rc) {
12432255736Sdavidch            sc->state = BXE_STATE_ERROR;
12433255736Sdavidch            goto bxe_nic_load_error3;
12434255736Sdavidch        }
12435255736Sdavidch    }
12436255736Sdavidch
12437255736Sdavidch    sc->link_params.feature_config_flags &=
12438255736Sdavidch        ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12439255736Sdavidch
12440255736Sdavidch    /* start fast path */
12441255736Sdavidch
12442255736Sdavidch    /* Initialize Rx filter */
12443255736Sdavidch    bxe_set_rx_mode(sc);
12444255736Sdavidch
12445255736Sdavidch    /* start the Tx */
12446255736Sdavidch    switch (/* XXX load_mode */LOAD_OPEN) {
12447255736Sdavidch    case LOAD_NORMAL:
12448255736Sdavidch    case LOAD_OPEN:
12449255736Sdavidch        break;
12450255736Sdavidch
12451255736Sdavidch    case LOAD_DIAG:
12452255736Sdavidch    case LOAD_LOOPBACK_EXT:
12453255736Sdavidch        sc->state = BXE_STATE_DIAG;
12454255736Sdavidch        break;
12455255736Sdavidch
12456255736Sdavidch    default:
12457255736Sdavidch        break;
12458255736Sdavidch    }
12459255736Sdavidch
12460255736Sdavidch    if (sc->port.pmf) {
12461255736Sdavidch        bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12462255736Sdavidch    } else {
12463255736Sdavidch        bxe_link_status_update(sc);
12464255736Sdavidch    }
12465255736Sdavidch
12466255736Sdavidch    /* start the periodic timer callout */
12467255736Sdavidch    bxe_periodic_start(sc);
12468255736Sdavidch
12469255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12470255736Sdavidch        /* mark driver is loaded in shmem2 */
12471255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12472255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12473255736Sdavidch                  (val |
12474255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12475255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_L2));
12476255736Sdavidch    }
12477255736Sdavidch
12478255736Sdavidch    /* wait for all pending SP commands to complete */
12479255736Sdavidch    if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12480255736Sdavidch        BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12481255736Sdavidch        bxe_periodic_stop(sc);
12482255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12483255736Sdavidch        return (ENXIO);
12484255736Sdavidch    }
12485255736Sdavidch
12486255736Sdavidch    /* Tell the stack the driver is running! */
12487266979Smarcel    if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
12488255736Sdavidch
12489255736Sdavidch    BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12490255736Sdavidch
12491255736Sdavidch    return (0);
12492255736Sdavidch
12493255736Sdavidchbxe_nic_load_error3:
12494255736Sdavidch
12495255736Sdavidch    if (IS_PF(sc)) {
12496255736Sdavidch        bxe_int_disable_sync(sc, 1);
12497255736Sdavidch
12498255736Sdavidch        /* clean out queued objects */
12499255736Sdavidch        bxe_squeeze_objects(sc);
12500255736Sdavidch    }
12501255736Sdavidch
12502255736Sdavidch    bxe_interrupt_detach(sc);
12503255736Sdavidch
12504255736Sdavidchbxe_nic_load_error2:
12505255736Sdavidch
12506255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12507255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12508255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12509255736Sdavidch    }
12510255736Sdavidch
12511255736Sdavidch    sc->port.pmf = 0;
12512255736Sdavidch
12513255736Sdavidchbxe_nic_load_error1:
12514255736Sdavidch
12515255736Sdavidch    /* clear pf_load status, as it was already set */
12516255736Sdavidch    if (IS_PF(sc)) {
12517255736Sdavidch        bxe_clear_pf_load(sc);
12518255736Sdavidch    }
12519255736Sdavidch
12520255736Sdavidchbxe_nic_load_error0:
12521255736Sdavidch
12522255736Sdavidch    bxe_free_fw_stats_mem(sc);
12523255736Sdavidch    bxe_free_fp_buffers(sc);
12524255736Sdavidch    bxe_free_mem(sc);
12525255736Sdavidch
12526255736Sdavidch    return (rc);
12527255736Sdavidch}
12528255736Sdavidch
12529255736Sdavidchstatic int
12530255736Sdavidchbxe_init_locked(struct bxe_softc *sc)
12531255736Sdavidch{
12532255736Sdavidch    int other_engine = SC_PATH(sc) ? 0 : 1;
12533255736Sdavidch    uint8_t other_load_status, load_status;
12534255736Sdavidch    uint8_t global = FALSE;
12535255736Sdavidch    int rc;
12536255736Sdavidch
12537255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12538255736Sdavidch
12539255736Sdavidch    /* check if the driver is already running */
12540266979Smarcel    if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12541255736Sdavidch        BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12542255736Sdavidch        return (0);
12543255736Sdavidch    }
12544255736Sdavidch
12545255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
12546255736Sdavidch
12547255736Sdavidch    /*
12548255736Sdavidch     * If parity occurred during the unload, then attentions and/or
12549255736Sdavidch     * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12550255736Sdavidch     * loaded on the current engine to complete the recovery. Parity recovery
12551255736Sdavidch     * is only relevant for PF driver.
12552255736Sdavidch     */
12553255736Sdavidch    if (IS_PF(sc)) {
12554255736Sdavidch        other_load_status = bxe_get_load_status(sc, other_engine);
12555255736Sdavidch        load_status = bxe_get_load_status(sc, SC_PATH(sc));
12556255736Sdavidch
12557255736Sdavidch        if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12558255736Sdavidch            bxe_chk_parity_attn(sc, &global, TRUE)) {
12559255736Sdavidch            do {
12560255736Sdavidch                /*
12561255736Sdavidch                 * If there are attentions and they are in global blocks, set
12562255736Sdavidch                 * the GLOBAL_RESET bit regardless whether it will be this
12563255736Sdavidch                 * function that will complete the recovery or not.
12564255736Sdavidch                 */
12565255736Sdavidch                if (global) {
12566255736Sdavidch                    bxe_set_reset_global(sc);
12567255736Sdavidch                }
12568255736Sdavidch
12569255736Sdavidch                /*
12570255736Sdavidch                 * Only the first function on the current engine should try
12571255736Sdavidch                 * to recover in open. In case of attentions in global blocks
12572255736Sdavidch                 * only the first in the chip should try to recover.
12573255736Sdavidch                 */
12574255736Sdavidch                if ((!load_status && (!global || !other_load_status)) &&
12575255736Sdavidch                    bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12576255736Sdavidch                    BLOGI(sc, "Recovered during init\n");
12577255736Sdavidch                    break;
12578255736Sdavidch                }
12579255736Sdavidch
12580255736Sdavidch                /* recovery has failed... */
12581255736Sdavidch                bxe_set_power_state(sc, PCI_PM_D3hot);
12582255736Sdavidch                sc->recovery_state = BXE_RECOVERY_FAILED;
12583255736Sdavidch
12584255736Sdavidch                BLOGE(sc, "Recovery flow hasn't properly "
12585255736Sdavidch                          "completed yet, try again later. "
12586255736Sdavidch                          "If you still see this message after a "
12587255736Sdavidch                          "few retries then power cycle is required.\n");
12588255736Sdavidch
12589255736Sdavidch                rc = ENXIO;
12590255736Sdavidch                goto bxe_init_locked_done;
12591255736Sdavidch            } while (0);
12592255736Sdavidch        }
12593255736Sdavidch    }
12594255736Sdavidch
12595255736Sdavidch    sc->recovery_state = BXE_RECOVERY_DONE;
12596255736Sdavidch
12597255736Sdavidch    rc = bxe_nic_load(sc, LOAD_OPEN);
12598255736Sdavidch
12599255736Sdavidchbxe_init_locked_done:
12600255736Sdavidch
12601255736Sdavidch    if (rc) {
12602255736Sdavidch        /* Tell the stack the driver is NOT running! */
12603255736Sdavidch        BLOGE(sc, "Initialization failed, "
12604255736Sdavidch                  "stack notified driver is NOT running!\n");
12605266979Smarcel	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
12606255736Sdavidch    }
12607255736Sdavidch
12608255736Sdavidch    return (rc);
12609255736Sdavidch}
12610255736Sdavidch
12611255736Sdavidchstatic int
12612255736Sdavidchbxe_stop_locked(struct bxe_softc *sc)
12613255736Sdavidch{
12614255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12615255736Sdavidch    return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
12616255736Sdavidch}
12617255736Sdavidch
12618255736Sdavidch/*
12619255736Sdavidch * Handles controller initialization when called from an unlocked routine.
12620255736Sdavidch * ifconfig calls this function.
12621255736Sdavidch *
12622255736Sdavidch * Returns:
12623255736Sdavidch *   void
12624255736Sdavidch */
12625255736Sdavidchstatic void
12626255736Sdavidchbxe_init(void *xsc)
12627255736Sdavidch{
12628255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12629255736Sdavidch
12630255736Sdavidch    BXE_CORE_LOCK(sc);
12631255736Sdavidch    bxe_init_locked(sc);
12632255736Sdavidch    BXE_CORE_UNLOCK(sc);
12633255736Sdavidch}
12634255736Sdavidch
12635255736Sdavidchstatic int
12636255736Sdavidchbxe_init_ifnet(struct bxe_softc *sc)
12637255736Sdavidch{
12638266979Smarcel    if_t ifp;
12639266979Smarcel    int capabilities;
12640255736Sdavidch
12641255736Sdavidch    /* ifconfig entrypoint for media type/status reporting */
12642255736Sdavidch    ifmedia_init(&sc->ifmedia, IFM_IMASK,
12643255736Sdavidch                 bxe_ifmedia_update,
12644255736Sdavidch                 bxe_ifmedia_status);
12645255736Sdavidch
12646255736Sdavidch    /* set the default interface values */
12647255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
12648255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
12649255736Sdavidch    ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
12650255736Sdavidch
12651255736Sdavidch    sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
12652255736Sdavidch
12653255736Sdavidch    /* allocate the ifnet structure */
12654266979Smarcel    if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
12655255736Sdavidch        BLOGE(sc, "Interface allocation failed!\n");
12656255736Sdavidch        return (ENXIO);
12657255736Sdavidch    }
12658255736Sdavidch
12659266979Smarcel    if_setsoftc(ifp, sc);
12660270876Sglebius    if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
12661266979Smarcel    if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
12662266979Smarcel    if_setioctlfn(ifp, bxe_ioctl);
12663266979Smarcel    if_setstartfn(ifp, bxe_tx_start);
12664271782Sglebius    if_setgetcounterfn(ifp, bxe_get_counter);
12665255736Sdavidch#if __FreeBSD_version >= 800000
12666266979Smarcel    if_settransmitfn(ifp, bxe_tx_mq_start);
12667266979Smarcel    if_setqflushfn(ifp, bxe_mq_flush);
12668255736Sdavidch#endif
12669255736Sdavidch#ifdef FreeBSD8_0
12670266979Smarcel    if_settimer(ifp, 0);
12671255736Sdavidch#endif
12672266979Smarcel    if_setinitfn(ifp, bxe_init);
12673266979Smarcel    if_setmtu(ifp, sc->mtu);
12674266979Smarcel    if_sethwassist(ifp, (CSUM_IP      |
12675255736Sdavidch                        CSUM_TCP      |
12676255736Sdavidch                        CSUM_UDP      |
12677255736Sdavidch                        CSUM_TSO      |
12678255736Sdavidch                        CSUM_TCP_IPV6 |
12679266979Smarcel                        CSUM_UDP_IPV6));
12680266979Smarcel
12681266979Smarcel    capabilities =
12682255736Sdavidch#if __FreeBSD_version < 700000
12683255736Sdavidch        (IFCAP_VLAN_MTU       |
12684255736Sdavidch         IFCAP_VLAN_HWTAGGING |
12685255736Sdavidch         IFCAP_HWCSUM         |
12686255736Sdavidch         IFCAP_JUMBO_MTU      |
12687255736Sdavidch         IFCAP_LRO);
12688255736Sdavidch#else
12689255736Sdavidch        (IFCAP_VLAN_MTU       |
12690255736Sdavidch         IFCAP_VLAN_HWTAGGING |
12691255736Sdavidch         IFCAP_VLAN_HWTSO     |
12692255736Sdavidch         IFCAP_VLAN_HWFILTER  |
12693255736Sdavidch         IFCAP_VLAN_HWCSUM    |
12694255736Sdavidch         IFCAP_HWCSUM         |
12695255736Sdavidch         IFCAP_JUMBO_MTU      |
12696255736Sdavidch         IFCAP_LRO            |
12697255736Sdavidch         IFCAP_TSO4           |
12698255736Sdavidch         IFCAP_TSO6           |
12699255736Sdavidch         IFCAP_WOL_MAGIC);
12700255736Sdavidch#endif
12701266979Smarcel    if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
12702266979Smarcel    if_setbaudrate(ifp, IF_Gbps(10));
12703266979Smarcel/* XXX */
12704266979Smarcel    if_setsendqlen(ifp, sc->tx_ring_size);
12705266979Smarcel    if_setsendqready(ifp);
12706266979Smarcel/* XXX */
12707255736Sdavidch
12708266979Smarcel    sc->ifp = ifp;
12709255736Sdavidch
12710255736Sdavidch    /* attach to the Ethernet interface list */
12711270876Sglebius    ether_ifattach(ifp, sc->link_params.mac_addr);
12712255736Sdavidch
12713255736Sdavidch    return (0);
12714255736Sdavidch}
12715255736Sdavidch
12716255736Sdavidchstatic void
12717255736Sdavidchbxe_deallocate_bars(struct bxe_softc *sc)
12718255736Sdavidch{
12719255736Sdavidch    int i;
12720255736Sdavidch
12721255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
12722255736Sdavidch        if (sc->bar[i].resource != NULL) {
12723255736Sdavidch            bus_release_resource(sc->dev,
12724255736Sdavidch                                 SYS_RES_MEMORY,
12725255736Sdavidch                                 sc->bar[i].rid,
12726255736Sdavidch                                 sc->bar[i].resource);
12727255736Sdavidch            BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
12728255736Sdavidch                  i, PCIR_BAR(i));
12729255736Sdavidch        }
12730255736Sdavidch    }
12731255736Sdavidch}
12732255736Sdavidch
12733255736Sdavidchstatic int
12734255736Sdavidchbxe_allocate_bars(struct bxe_softc *sc)
12735255736Sdavidch{
12736255736Sdavidch    u_int flags;
12737255736Sdavidch    int i;
12738255736Sdavidch
12739255736Sdavidch    memset(sc->bar, 0, sizeof(sc->bar));
12740255736Sdavidch
12741255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
12742255736Sdavidch
12743255736Sdavidch        /* memory resources reside at BARs 0, 2, 4 */
12744255736Sdavidch        /* Run `pciconf -lb` to see mappings */
12745255736Sdavidch        if ((i != 0) && (i != 2) && (i != 4)) {
12746255736Sdavidch            continue;
12747255736Sdavidch        }
12748255736Sdavidch
12749255736Sdavidch        sc->bar[i].rid = PCIR_BAR(i);
12750255736Sdavidch
12751255736Sdavidch        flags = RF_ACTIVE;
12752255736Sdavidch        if (i == 0) {
12753255736Sdavidch            flags |= RF_SHAREABLE;
12754255736Sdavidch        }
12755255736Sdavidch
12756255736Sdavidch        if ((sc->bar[i].resource =
12757255736Sdavidch             bus_alloc_resource_any(sc->dev,
12758255736Sdavidch                                    SYS_RES_MEMORY,
12759255736Sdavidch                                    &sc->bar[i].rid,
12760255736Sdavidch                                    flags)) == NULL) {
12761255736Sdavidch            return (0);
12762255736Sdavidch        }
12763255736Sdavidch
12764255736Sdavidch        sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
12765255736Sdavidch        sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
12766255736Sdavidch        sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
12767255736Sdavidch
12768297000Sjhibbits        BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%jd) -> %p\n",
12769255736Sdavidch              i, PCIR_BAR(i),
12770255736Sdavidch              (void *)rman_get_start(sc->bar[i].resource),
12771255736Sdavidch              (void *)rman_get_end(sc->bar[i].resource),
12772255736Sdavidch              rman_get_size(sc->bar[i].resource),
12773255736Sdavidch              (void *)sc->bar[i].kva);
12774255736Sdavidch    }
12775255736Sdavidch
12776255736Sdavidch    return (0);
12777255736Sdavidch}
12778255736Sdavidch
12779255736Sdavidchstatic void
12780255736Sdavidchbxe_get_function_num(struct bxe_softc *sc)
12781255736Sdavidch{
12782255736Sdavidch    uint32_t val = 0;
12783255736Sdavidch
12784255736Sdavidch    /*
12785255736Sdavidch     * Read the ME register to get the function number. The ME register
12786255736Sdavidch     * holds the relative-function number and absolute-function number. The
12787255736Sdavidch     * absolute-function number appears only in E2 and above. Before that
12788255736Sdavidch     * these bits always contained zero, therefore we cannot blindly use them.
12789255736Sdavidch     */
12790255736Sdavidch
12791255736Sdavidch    val = REG_RD(sc, BAR_ME_REGISTER);
12792255736Sdavidch
12793255736Sdavidch    sc->pfunc_rel =
12794255736Sdavidch        (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
12795255736Sdavidch    sc->path_id =
12796255736Sdavidch        (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
12797255736Sdavidch
12798255736Sdavidch    if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
12799255736Sdavidch        sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
12800255736Sdavidch    } else {
12801255736Sdavidch        sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
12802255736Sdavidch    }
12803255736Sdavidch
12804255736Sdavidch    BLOGD(sc, DBG_LOAD,
12805255736Sdavidch          "Relative function %d, Absolute function %d, Path %d\n",
12806255736Sdavidch          sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
12807255736Sdavidch}
12808255736Sdavidch
12809255736Sdavidchstatic uint32_t
12810255736Sdavidchbxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
12811255736Sdavidch{
12812255736Sdavidch    uint32_t shmem2_size;
12813255736Sdavidch    uint32_t offset;
12814255736Sdavidch    uint32_t mf_cfg_offset_value;
12815255736Sdavidch
12816255736Sdavidch    /* Non 57712 */
12817255736Sdavidch    offset = (SHMEM_RD(sc, func_mb) +
12818255736Sdavidch              (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
12819255736Sdavidch
12820255736Sdavidch    /* 57712 plus */
12821255736Sdavidch    if (sc->devinfo.shmem2_base != 0) {
12822255736Sdavidch        shmem2_size = SHMEM2_RD(sc, size);
12823255736Sdavidch        if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
12824255736Sdavidch            mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
12825255736Sdavidch            if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
12826255736Sdavidch                offset = mf_cfg_offset_value;
12827255736Sdavidch            }
12828255736Sdavidch        }
12829255736Sdavidch    }
12830255736Sdavidch
12831255736Sdavidch    return (offset);
12832255736Sdavidch}
12833255736Sdavidch
12834255736Sdavidchstatic uint32_t
12835255736Sdavidchbxe_pcie_capability_read(struct bxe_softc *sc,
12836255736Sdavidch                         int    reg,
12837255736Sdavidch                         int    width)
12838255736Sdavidch{
12839255736Sdavidch    int pcie_reg;
12840255736Sdavidch
12841255736Sdavidch    /* ensure PCIe capability is enabled */
12842255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
12843255736Sdavidch        if (pcie_reg != 0) {
12844255736Sdavidch            BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
12845255736Sdavidch            return (pci_read_config(sc->dev, (pcie_reg + reg), width));
12846255736Sdavidch        }
12847255736Sdavidch    }
12848255736Sdavidch
12849255736Sdavidch    BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
12850255736Sdavidch
12851255736Sdavidch    return (0);
12852255736Sdavidch}
12853255736Sdavidch
12854255736Sdavidchstatic uint8_t
12855255736Sdavidchbxe_is_pcie_pending(struct bxe_softc *sc)
12856255736Sdavidch{
12857255736Sdavidch    return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
12858255736Sdavidch            PCIM_EXP_STA_TRANSACTION_PND);
12859255736Sdavidch}
12860255736Sdavidch
12861255736Sdavidch/*
12862255736Sdavidch * Walk the PCI capabiites list for the device to find what features are
12863255736Sdavidch * supported. These capabilites may be enabled/disabled by firmware so it's
12864255736Sdavidch * best to walk the list rather than make assumptions.
12865255736Sdavidch */
12866255736Sdavidchstatic void
12867255736Sdavidchbxe_probe_pci_caps(struct bxe_softc *sc)
12868255736Sdavidch{
12869255736Sdavidch    uint16_t link_status;
12870255736Sdavidch    int reg;
12871255736Sdavidch
12872255736Sdavidch    /* check if PCI Power Management is enabled */
12873255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
12874255736Sdavidch        if (reg != 0) {
12875255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
12876255736Sdavidch
12877255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
12878255736Sdavidch            sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
12879255736Sdavidch        }
12880255736Sdavidch    }
12881255736Sdavidch
12882255736Sdavidch    link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
12883255736Sdavidch
12884255736Sdavidch    /* handle PCIe 2.0 workarounds for 57710 */
12885255736Sdavidch    if (CHIP_IS_E1(sc)) {
12886255736Sdavidch        /* workaround for 57710 errata E4_57710_27462 */
12887255736Sdavidch        sc->devinfo.pcie_link_speed =
12888255736Sdavidch            (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
12889255736Sdavidch
12890255736Sdavidch        /* workaround for 57710 errata E4_57710_27488 */
12891255736Sdavidch        sc->devinfo.pcie_link_width =
12892255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12893255736Sdavidch        if (sc->devinfo.pcie_link_speed > 1) {
12894255736Sdavidch            sc->devinfo.pcie_link_width =
12895255736Sdavidch                ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
12896255736Sdavidch        }
12897255736Sdavidch    } else {
12898255736Sdavidch        sc->devinfo.pcie_link_speed =
12899255736Sdavidch            (link_status & PCIM_LINK_STA_SPEED);
12900255736Sdavidch        sc->devinfo.pcie_link_width =
12901255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12902255736Sdavidch    }
12903255736Sdavidch
12904255736Sdavidch    BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
12905255736Sdavidch          sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
12906255736Sdavidch
12907255736Sdavidch    sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
12908255736Sdavidch    sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
12909255736Sdavidch
12910255736Sdavidch    /* check if MSI capability is enabled */
12911255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
12912255736Sdavidch        if (reg != 0) {
12913255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
12914255736Sdavidch
12915255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
12916255736Sdavidch            sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
12917255736Sdavidch        }
12918255736Sdavidch    }
12919255736Sdavidch
12920255736Sdavidch    /* check if MSI-X capability is enabled */
12921255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
12922255736Sdavidch        if (reg != 0) {
12923255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
12924255736Sdavidch
12925255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
12926255736Sdavidch            sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
12927255736Sdavidch        }
12928255736Sdavidch    }
12929255736Sdavidch}
12930255736Sdavidch
12931255736Sdavidchstatic int
12932255736Sdavidchbxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
12933255736Sdavidch{
12934255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
12935255736Sdavidch    uint32_t val;
12936255736Sdavidch
12937255736Sdavidch    /* get the outer vlan if we're in switch-dependent mode */
12938255736Sdavidch
12939255736Sdavidch    val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
12940255736Sdavidch    mf_info->ext_id = (uint16_t)val;
12941255736Sdavidch
12942255736Sdavidch    mf_info->multi_vnics_mode = 1;
12943255736Sdavidch
12944255736Sdavidch    if (!VALID_OVLAN(mf_info->ext_id)) {
12945255736Sdavidch        BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
12946255736Sdavidch        return (1);
12947255736Sdavidch    }
12948255736Sdavidch
12949255736Sdavidch    /* get the capabilities */
12950255736Sdavidch    if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
12951255736Sdavidch        FUNC_MF_CFG_PROTOCOL_ISCSI) {
12952255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
12953255736Sdavidch    } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
12954255736Sdavidch               FUNC_MF_CFG_PROTOCOL_FCOE) {
12955255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
12956255736Sdavidch    } else {
12957255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
12958255736Sdavidch    }
12959255736Sdavidch
12960255736Sdavidch    mf_info->vnics_per_port =
12961255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
12962255736Sdavidch
12963255736Sdavidch    return (0);
12964255736Sdavidch}
12965255736Sdavidch
12966255736Sdavidchstatic uint32_t
12967255736Sdavidchbxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
12968255736Sdavidch{
12969255736Sdavidch    uint32_t retval = 0;
12970255736Sdavidch    uint32_t val;
12971255736Sdavidch
12972255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
12973255736Sdavidch
12974255736Sdavidch    if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
12975255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
12976255736Sdavidch            retval |= MF_PROTO_SUPPORT_ETHERNET;
12977255736Sdavidch        }
12978255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
12979255736Sdavidch            retval |= MF_PROTO_SUPPORT_ISCSI;
12980255736Sdavidch        }
12981255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
12982255736Sdavidch            retval |= MF_PROTO_SUPPORT_FCOE;
12983255736Sdavidch        }
12984255736Sdavidch    }
12985255736Sdavidch
12986255736Sdavidch    return (retval);
12987255736Sdavidch}
12988255736Sdavidch
12989255736Sdavidchstatic int
12990255736Sdavidchbxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
12991255736Sdavidch{
12992255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
12993255736Sdavidch    uint32_t val;
12994255736Sdavidch
12995255736Sdavidch    /*
12996255736Sdavidch     * There is no outer vlan if we're in switch-independent mode.
12997255736Sdavidch     * If the mac is valid then assume multi-function.
12998255736Sdavidch     */
12999255736Sdavidch
13000255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13001255736Sdavidch
13002255736Sdavidch    mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13003255736Sdavidch
13004255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13005255736Sdavidch
13006255736Sdavidch    mf_info->vnics_per_port =
13007255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13008255736Sdavidch
13009255736Sdavidch    return (0);
13010255736Sdavidch}
13011255736Sdavidch
13012255736Sdavidchstatic int
13013255736Sdavidchbxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13014255736Sdavidch{
13015255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13016255736Sdavidch    uint32_t e1hov_tag;
13017255736Sdavidch    uint32_t func_config;
13018255736Sdavidch    uint32_t niv_config;
13019255736Sdavidch
13020255736Sdavidch    mf_info->multi_vnics_mode = 1;
13021255736Sdavidch
13022255736Sdavidch    e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13023255736Sdavidch    func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13024255736Sdavidch    niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13025255736Sdavidch
13026255736Sdavidch    mf_info->ext_id =
13027255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13028255736Sdavidch                   FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13029255736Sdavidch
13030255736Sdavidch    mf_info->default_vlan =
13031255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13032255736Sdavidch                   FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13033255736Sdavidch
13034255736Sdavidch    mf_info->niv_allowed_priorities =
13035255736Sdavidch        (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13036255736Sdavidch                  FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13037255736Sdavidch
13038255736Sdavidch    mf_info->niv_default_cos =
13039255736Sdavidch        (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13040255736Sdavidch                  FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13041255736Sdavidch
13042255736Sdavidch    mf_info->afex_vlan_mode =
13043255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13044255736Sdavidch         FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13045255736Sdavidch
13046255736Sdavidch    mf_info->niv_mba_enabled =
13047255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13048255736Sdavidch         FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13049255736Sdavidch
13050255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13051255736Sdavidch
13052255736Sdavidch    mf_info->vnics_per_port =
13053255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13054255736Sdavidch
13055255736Sdavidch    return (0);
13056255736Sdavidch}
13057255736Sdavidch
13058255736Sdavidchstatic int
13059255736Sdavidchbxe_check_valid_mf_cfg(struct bxe_softc *sc)
13060255736Sdavidch{
13061255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13062255736Sdavidch    uint32_t mf_cfg1;
13063255736Sdavidch    uint32_t mf_cfg2;
13064255736Sdavidch    uint32_t ovlan1;
13065255736Sdavidch    uint32_t ovlan2;
13066255736Sdavidch    uint8_t i, j;
13067255736Sdavidch
13068255736Sdavidch    BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13069255736Sdavidch          SC_PORT(sc));
13070255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13071255736Sdavidch          mf_info->mf_config[SC_VN(sc)]);
13072255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13073255736Sdavidch          mf_info->multi_vnics_mode);
13074255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13075255736Sdavidch          mf_info->vnics_per_port);
13076255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13077255736Sdavidch          mf_info->ext_id);
13078255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13079255736Sdavidch          mf_info->min_bw[0], mf_info->min_bw[1],
13080255736Sdavidch          mf_info->min_bw[2], mf_info->min_bw[3]);
13081255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13082255736Sdavidch          mf_info->max_bw[0], mf_info->max_bw[1],
13083255736Sdavidch          mf_info->max_bw[2], mf_info->max_bw[3]);
13084255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13085255736Sdavidch          sc->mac_addr_str);
13086255736Sdavidch
13087255736Sdavidch    /* various MF mode sanity checks... */
13088255736Sdavidch
13089255736Sdavidch    if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13090255736Sdavidch        BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13091255736Sdavidch              SC_PORT(sc));
13092255736Sdavidch        return (1);
13093255736Sdavidch    }
13094255736Sdavidch
13095255736Sdavidch    if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13096255736Sdavidch        BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13097255736Sdavidch              mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13098255736Sdavidch        return (1);
13099255736Sdavidch    }
13100255736Sdavidch
13101255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13102255736Sdavidch        /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13103255736Sdavidch        if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13104255736Sdavidch            BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13105255736Sdavidch                  SC_VN(sc), OVLAN(sc));
13106255736Sdavidch            return (1);
13107255736Sdavidch        }
13108255736Sdavidch
13109255736Sdavidch        if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13110255736Sdavidch            BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13111255736Sdavidch                  mf_info->multi_vnics_mode, OVLAN(sc));
13112255736Sdavidch            return (1);
13113255736Sdavidch        }
13114255736Sdavidch
13115255736Sdavidch        /*
13116255736Sdavidch         * Verify all functions are either MF or SF mode. If MF, make sure
13117255736Sdavidch         * sure that all non-hidden functions have a valid ovlan. If SF,
13118255736Sdavidch         * make sure that all non-hidden functions have an invalid ovlan.
13119255736Sdavidch         */
13120255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13121255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13122255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13123255736Sdavidch            if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13124255736Sdavidch                (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13125255736Sdavidch                 ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13126255736Sdavidch                BLOGE(sc, "mf_mode=SD function %d MF config "
13127255736Sdavidch                          "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13128255736Sdavidch                      i, mf_info->multi_vnics_mode, ovlan1);
13129255736Sdavidch                return (1);
13130255736Sdavidch            }
13131255736Sdavidch        }
13132255736Sdavidch
13133255736Sdavidch        /* Verify all funcs on the same port each have a different ovlan. */
13134255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13135255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13136255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13137255736Sdavidch            /* iterate from the next function on the port to the max func */
13138255736Sdavidch            for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13139255736Sdavidch                mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13140255736Sdavidch                ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13141255736Sdavidch                if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13142255736Sdavidch                    VALID_OVLAN(ovlan1) &&
13143255736Sdavidch                    !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13144255736Sdavidch                    VALID_OVLAN(ovlan2) &&
13145255736Sdavidch                    (ovlan1 == ovlan2)) {
13146255736Sdavidch                    BLOGE(sc, "mf_mode=SD functions %d and %d "
13147255736Sdavidch                              "have the same ovlan (%d)\n",
13148255736Sdavidch                          i, j, ovlan1);
13149255736Sdavidch                    return (1);
13150255736Sdavidch                }
13151255736Sdavidch            }
13152255736Sdavidch        }
13153255736Sdavidch    } /* MULTI_FUNCTION_SD */
13154255736Sdavidch
13155255736Sdavidch    return (0);
13156255736Sdavidch}
13157255736Sdavidch
13158255736Sdavidchstatic int
13159255736Sdavidchbxe_get_mf_cfg_info(struct bxe_softc *sc)
13160255736Sdavidch{
13161255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13162255736Sdavidch    uint32_t val, mac_upper;
13163255736Sdavidch    uint8_t i, vnic;
13164255736Sdavidch
13165255736Sdavidch    /* initialize mf_info defaults */
13166255736Sdavidch    mf_info->vnics_per_port   = 1;
13167255736Sdavidch    mf_info->multi_vnics_mode = FALSE;
13168255736Sdavidch    mf_info->path_has_ovlan   = FALSE;
13169255736Sdavidch    mf_info->mf_mode          = SINGLE_FUNCTION;
13170255736Sdavidch
13171255736Sdavidch    if (!CHIP_IS_MF_CAP(sc)) {
13172255736Sdavidch        return (0);
13173255736Sdavidch    }
13174255736Sdavidch
13175255736Sdavidch    if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13176255736Sdavidch        BLOGE(sc, "Invalid mf_cfg_base!\n");
13177255736Sdavidch        return (1);
13178255736Sdavidch    }
13179255736Sdavidch
13180255736Sdavidch    /* get the MF mode (switch dependent / independent / single-function) */
13181255736Sdavidch
13182255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13183255736Sdavidch
13184255736Sdavidch    switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13185255736Sdavidch    {
13186255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13187255736Sdavidch
13188255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13189255736Sdavidch
13190255736Sdavidch        /* check for legal upper mac bytes */
13191255736Sdavidch        if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13192255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SI;
13193255736Sdavidch        } else {
13194255736Sdavidch            BLOGE(sc, "Invalid config for Switch Independent mode\n");
13195255736Sdavidch        }
13196255736Sdavidch
13197255736Sdavidch        break;
13198255736Sdavidch
13199255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13200255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13201255736Sdavidch
13202255736Sdavidch        /* get outer vlan configuration */
13203255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13204255736Sdavidch
13205255736Sdavidch        if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13206255736Sdavidch            FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13207255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SD;
13208255736Sdavidch        } else {
13209255736Sdavidch            BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13210255736Sdavidch        }
13211255736Sdavidch
13212255736Sdavidch        break;
13213255736Sdavidch
13214255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13215255736Sdavidch
13216255736Sdavidch        /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13217255736Sdavidch        return (0);
13218255736Sdavidch
13219255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13220255736Sdavidch
13221255736Sdavidch        /*
13222255736Sdavidch         * Mark MF mode as NIV if MCP version includes NPAR-SD support
13223255736Sdavidch         * and the MAC address is valid.
13224255736Sdavidch         */
13225255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13226255736Sdavidch
13227255736Sdavidch        if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13228255736Sdavidch            (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13229255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13230255736Sdavidch        } else {
13231255736Sdavidch            BLOGE(sc, "Invalid config for AFEX mode\n");
13232255736Sdavidch        }
13233255736Sdavidch
13234255736Sdavidch        break;
13235255736Sdavidch
13236255736Sdavidch    default:
13237255736Sdavidch
13238255736Sdavidch        BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13239255736Sdavidch              (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13240255736Sdavidch
13241255736Sdavidch        return (1);
13242255736Sdavidch    }
13243255736Sdavidch
13244255736Sdavidch    /* set path mf_mode (which could be different than function mf_mode) */
13245255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13246255736Sdavidch        mf_info->path_has_ovlan = TRUE;
13247255736Sdavidch    } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13248255736Sdavidch        /*
13249255736Sdavidch         * Decide on path multi vnics mode. If we're not in MF mode and in
13250255736Sdavidch         * 4-port mode, this is good enough to check vnic-0 of the other port
13251255736Sdavidch         * on the same path
13252255736Sdavidch         */
13253255736Sdavidch        if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13254255736Sdavidch            uint8_t other_port = !(PORT_ID(sc) & 1);
13255255736Sdavidch            uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13256255736Sdavidch
13257255736Sdavidch            val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13258255736Sdavidch
13259255736Sdavidch            mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13260255736Sdavidch        }
13261255736Sdavidch    }
13262255736Sdavidch
13263255736Sdavidch    if (mf_info->mf_mode == SINGLE_FUNCTION) {
13264255736Sdavidch        /* invalid MF config */
13265255736Sdavidch        if (SC_VN(sc) >= 1) {
13266255736Sdavidch            BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13267255736Sdavidch            return (1);
13268255736Sdavidch        }
13269255736Sdavidch
13270255736Sdavidch        return (0);
13271255736Sdavidch    }
13272255736Sdavidch
13273255736Sdavidch    /* get the MF configuration */
13274255736Sdavidch    mf_info->mf_config[SC_VN(sc)] =
13275255736Sdavidch        MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13276255736Sdavidch
13277255736Sdavidch    switch(mf_info->mf_mode)
13278255736Sdavidch    {
13279255736Sdavidch    case MULTI_FUNCTION_SD:
13280255736Sdavidch
13281255736Sdavidch        bxe_get_shmem_mf_cfg_info_sd(sc);
13282255736Sdavidch        break;
13283255736Sdavidch
13284255736Sdavidch    case MULTI_FUNCTION_SI:
13285255736Sdavidch
13286255736Sdavidch        bxe_get_shmem_mf_cfg_info_si(sc);
13287255736Sdavidch        break;
13288255736Sdavidch
13289255736Sdavidch    case MULTI_FUNCTION_AFEX:
13290255736Sdavidch
13291255736Sdavidch        bxe_get_shmem_mf_cfg_info_niv(sc);
13292255736Sdavidch        break;
13293255736Sdavidch
13294255736Sdavidch    default:
13295255736Sdavidch
13296255736Sdavidch        BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13297255736Sdavidch              mf_info->mf_mode);
13298255736Sdavidch        return (1);
13299255736Sdavidch    }
13300255736Sdavidch
13301255736Sdavidch    /* get the congestion management parameters */
13302255736Sdavidch
13303255736Sdavidch    vnic = 0;
13304255736Sdavidch    FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13305255736Sdavidch        /* get min/max bw */
13306255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[i].config);
13307255736Sdavidch        mf_info->min_bw[vnic] =
13308255736Sdavidch            ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13309255736Sdavidch        mf_info->max_bw[vnic] =
13310255736Sdavidch            ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13311255736Sdavidch        vnic++;
13312255736Sdavidch    }
13313255736Sdavidch
13314255736Sdavidch    return (bxe_check_valid_mf_cfg(sc));
13315255736Sdavidch}
13316255736Sdavidch
13317255736Sdavidchstatic int
13318255736Sdavidchbxe_get_shmem_info(struct bxe_softc *sc)
13319255736Sdavidch{
13320255736Sdavidch    int port;
13321255736Sdavidch    uint32_t mac_hi, mac_lo, val;
13322255736Sdavidch
13323255736Sdavidch    port = SC_PORT(sc);
13324255736Sdavidch    mac_hi = mac_lo = 0;
13325255736Sdavidch
13326255736Sdavidch    sc->link_params.sc   = sc;
13327255736Sdavidch    sc->link_params.port = port;
13328255736Sdavidch
13329255736Sdavidch    /* get the hardware config info */
13330255736Sdavidch    sc->devinfo.hw_config =
13331255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config);
13332255736Sdavidch    sc->devinfo.hw_config2 =
13333255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13334255736Sdavidch
13335255736Sdavidch    sc->link_params.hw_led_mode =
13336255736Sdavidch        ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13337255736Sdavidch         SHARED_HW_CFG_LED_MODE_SHIFT);
13338255736Sdavidch
13339255736Sdavidch    /* get the port feature config */
13340255736Sdavidch    sc->port.config =
13341255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].config),
13342255736Sdavidch
13343255736Sdavidch    /* get the link params */
13344255736Sdavidch    sc->link_params.speed_cap_mask[0] =
13345255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13346255736Sdavidch    sc->link_params.speed_cap_mask[1] =
13347255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13348255736Sdavidch
13349255736Sdavidch    /* get the lane config */
13350255736Sdavidch    sc->link_params.lane_config =
13351255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13352255736Sdavidch
13353255736Sdavidch    /* get the link config */
13354255736Sdavidch    val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13355255736Sdavidch    sc->port.link_config[ELINK_INT_PHY] = val;
13356255736Sdavidch    sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13357255736Sdavidch    sc->port.link_config[ELINK_EXT_PHY1] =
13358255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13359255736Sdavidch
13360255736Sdavidch    /* get the override preemphasis flag and enable it or turn it off */
13361255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13362255736Sdavidch    if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13363255736Sdavidch        sc->link_params.feature_config_flags |=
13364255736Sdavidch            ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13365255736Sdavidch    } else {
13366255736Sdavidch        sc->link_params.feature_config_flags &=
13367255736Sdavidch            ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13368255736Sdavidch    }
13369255736Sdavidch
13370255736Sdavidch    /* get the initial value of the link params */
13371255736Sdavidch    sc->link_params.multi_phy_config =
13372255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13373255736Sdavidch
13374255736Sdavidch    /* get external phy info */
13375255736Sdavidch    sc->port.ext_phy_config =
13376255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13377255736Sdavidch
13378255736Sdavidch    /* get the multifunction configuration */
13379255736Sdavidch    bxe_get_mf_cfg_info(sc);
13380255736Sdavidch
13381255736Sdavidch    /* get the mac address */
13382255736Sdavidch    if (IS_MF(sc)) {
13383255736Sdavidch        mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13384255736Sdavidch        mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13385255736Sdavidch    } else {
13386255736Sdavidch        mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13387255736Sdavidch        mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13388255736Sdavidch    }
13389255736Sdavidch
13390255736Sdavidch    if ((mac_lo == 0) && (mac_hi == 0)) {
13391255736Sdavidch        *sc->mac_addr_str = 0;
13392255736Sdavidch        BLOGE(sc, "No Ethernet address programmed!\n");
13393255736Sdavidch    } else {
13394255736Sdavidch        sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13395255736Sdavidch        sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13396255736Sdavidch        sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13397255736Sdavidch        sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13398255736Sdavidch        sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13399255736Sdavidch        sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13400255736Sdavidch        snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13401255736Sdavidch                 "%02x:%02x:%02x:%02x:%02x:%02x",
13402255736Sdavidch                 sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13403255736Sdavidch                 sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13404255736Sdavidch                 sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13405255736Sdavidch        BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13406255736Sdavidch    }
13407255736Sdavidch
13408255736Sdavidch    return (0);
13409255736Sdavidch}
13410255736Sdavidch
13411255736Sdavidchstatic void
13412255736Sdavidchbxe_get_tunable_params(struct bxe_softc *sc)
13413255736Sdavidch{
13414255736Sdavidch    /* sanity checks */
13415255736Sdavidch
13416255736Sdavidch    if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13417255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13418255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13419255736Sdavidch        BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13420255736Sdavidch        bxe_interrupt_mode = INTR_MODE_MSIX;
13421255736Sdavidch    }
13422255736Sdavidch
13423255736Sdavidch    if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13424255736Sdavidch        BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13425255736Sdavidch        bxe_queue_count = 0;
13426255736Sdavidch    }
13427255736Sdavidch
13428255736Sdavidch    if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13429255736Sdavidch        if (bxe_max_rx_bufs == 0) {
13430255736Sdavidch            bxe_max_rx_bufs = RX_BD_USABLE;
13431255736Sdavidch        } else {
13432255736Sdavidch            BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13433255736Sdavidch            bxe_max_rx_bufs = 2048;
13434255736Sdavidch        }
13435255736Sdavidch    }
13436255736Sdavidch
13437255736Sdavidch    if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13438255736Sdavidch        BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13439255736Sdavidch        bxe_hc_rx_ticks = 25;
13440255736Sdavidch    }
13441255736Sdavidch
13442255736Sdavidch    if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13443255736Sdavidch        BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13444255736Sdavidch        bxe_hc_tx_ticks = 50;
13445255736Sdavidch    }
13446255736Sdavidch
13447255736Sdavidch    if (bxe_max_aggregation_size == 0) {
13448255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13449255736Sdavidch    }
13450255736Sdavidch
13451255736Sdavidch    if (bxe_max_aggregation_size > 0xffff) {
13452255736Sdavidch        BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13453255736Sdavidch              bxe_max_aggregation_size);
13454255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13455255736Sdavidch    }
13456255736Sdavidch
13457255736Sdavidch    if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13458255736Sdavidch        BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13459255736Sdavidch        bxe_mrrs = -1;
13460255736Sdavidch    }
13461255736Sdavidch
13462255736Sdavidch    if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13463255736Sdavidch        BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13464255736Sdavidch        bxe_autogreeen = 0;
13465255736Sdavidch    }
13466255736Sdavidch
13467255736Sdavidch    if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13468255736Sdavidch        BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13469255736Sdavidch        bxe_udp_rss = 0;
13470255736Sdavidch    }
13471255736Sdavidch
13472255736Sdavidch    /* pull in user settings */
13473255736Sdavidch
13474255736Sdavidch    sc->interrupt_mode       = bxe_interrupt_mode;
13475255736Sdavidch    sc->max_rx_bufs          = bxe_max_rx_bufs;
13476255736Sdavidch    sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13477255736Sdavidch    sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13478255736Sdavidch    sc->max_aggregation_size = bxe_max_aggregation_size;
13479255736Sdavidch    sc->mrrs                 = bxe_mrrs;
13480255736Sdavidch    sc->autogreeen           = bxe_autogreeen;
13481255736Sdavidch    sc->udp_rss              = bxe_udp_rss;
13482255736Sdavidch
13483255736Sdavidch    if (bxe_interrupt_mode == INTR_MODE_INTX) {
13484255736Sdavidch        sc->num_queues = 1;
13485255736Sdavidch    } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13486255736Sdavidch        sc->num_queues =
13487255736Sdavidch            min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13488255736Sdavidch                MAX_RSS_CHAINS);
13489255736Sdavidch        if (sc->num_queues > mp_ncpus) {
13490255736Sdavidch            sc->num_queues = mp_ncpus;
13491255736Sdavidch        }
13492255736Sdavidch    }
13493255736Sdavidch
13494255736Sdavidch    BLOGD(sc, DBG_LOAD,
13495255736Sdavidch          "User Config: "
13496258187Sedavis          "debug=0x%lx "
13497255736Sdavidch          "interrupt_mode=%d "
13498255736Sdavidch          "queue_count=%d "
13499255736Sdavidch          "hc_rx_ticks=%d "
13500255736Sdavidch          "hc_tx_ticks=%d "
13501255736Sdavidch          "rx_budget=%d "
13502255736Sdavidch          "max_aggregation_size=%d "
13503255736Sdavidch          "mrrs=%d "
13504255736Sdavidch          "autogreeen=%d "
13505255736Sdavidch          "udp_rss=%d\n",
13506255736Sdavidch          bxe_debug,
13507255736Sdavidch          sc->interrupt_mode,
13508255736Sdavidch          sc->num_queues,
13509255736Sdavidch          sc->hc_rx_ticks,
13510255736Sdavidch          sc->hc_tx_ticks,
13511255736Sdavidch          bxe_rx_budget,
13512255736Sdavidch          sc->max_aggregation_size,
13513255736Sdavidch          sc->mrrs,
13514255736Sdavidch          sc->autogreeen,
13515255736Sdavidch          sc->udp_rss);
13516255736Sdavidch}
13517255736Sdavidch
13518297884Sdavidcsstatic int
13519255736Sdavidchbxe_media_detect(struct bxe_softc *sc)
13520255736Sdavidch{
13521297884Sdavidcs    int port_type;
13522255736Sdavidch    uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13523297884Sdavidcs
13524255736Sdavidch    switch (sc->link_params.phy[phy_idx].media_type) {
13525255736Sdavidch    case ELINK_ETH_PHY_SFPP_10G_FIBER:
13526256299Sedavis    case ELINK_ETH_PHY_XFP_FIBER:
13527256299Sedavis        BLOGI(sc, "Found 10Gb Fiber media.\n");
13528256299Sedavis        sc->media = IFM_10G_SR;
13529297884Sdavidcs        port_type = PORT_FIBRE;
13530256299Sedavis        break;
13531255736Sdavidch    case ELINK_ETH_PHY_SFP_1G_FIBER:
13532256299Sedavis        BLOGI(sc, "Found 1Gb Fiber media.\n");
13533256299Sedavis        sc->media = IFM_1000_SX;
13534297884Sdavidcs        port_type = PORT_FIBRE;
13535256299Sedavis        break;
13536255736Sdavidch    case ELINK_ETH_PHY_KR:
13537255736Sdavidch    case ELINK_ETH_PHY_CX4:
13538255736Sdavidch        BLOGI(sc, "Found 10GBase-CX4 media.\n");
13539255736Sdavidch        sc->media = IFM_10G_CX4;
13540297884Sdavidcs        port_type = PORT_FIBRE;
13541255736Sdavidch        break;
13542255736Sdavidch    case ELINK_ETH_PHY_DA_TWINAX:
13543255736Sdavidch        BLOGI(sc, "Found 10Gb Twinax media.\n");
13544255736Sdavidch        sc->media = IFM_10G_TWINAX;
13545297884Sdavidcs        port_type = PORT_DA;
13546255736Sdavidch        break;
13547255736Sdavidch    case ELINK_ETH_PHY_BASE_T:
13548256299Sedavis        if (sc->link_params.speed_cap_mask[0] &
13549256299Sedavis            PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13550256299Sedavis            BLOGI(sc, "Found 10GBase-T media.\n");
13551256299Sedavis            sc->media = IFM_10G_T;
13552297884Sdavidcs            port_type = PORT_TP;
13553256299Sedavis        } else {
13554256299Sedavis            BLOGI(sc, "Found 1000Base-T media.\n");
13555256299Sedavis            sc->media = IFM_1000_T;
13556297884Sdavidcs            port_type = PORT_TP;
13557256299Sedavis        }
13558255736Sdavidch        break;
13559255736Sdavidch    case ELINK_ETH_PHY_NOT_PRESENT:
13560255736Sdavidch        BLOGI(sc, "Media not present.\n");
13561255736Sdavidch        sc->media = 0;
13562297884Sdavidcs        port_type = PORT_OTHER;
13563255736Sdavidch        break;
13564255736Sdavidch    case ELINK_ETH_PHY_UNSPECIFIED:
13565255736Sdavidch    default:
13566255736Sdavidch        BLOGI(sc, "Unknown media!\n");
13567255736Sdavidch        sc->media = 0;
13568297884Sdavidcs        port_type = PORT_OTHER;
13569255736Sdavidch        break;
13570255736Sdavidch    }
13571297884Sdavidcs    return port_type;
13572255736Sdavidch}
13573255736Sdavidch
13574255736Sdavidch#define GET_FIELD(value, fname)                     \
13575255736Sdavidch    (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13576255736Sdavidch#define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13577255736Sdavidch#define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
13578255736Sdavidch
13579255736Sdavidchstatic int
13580255736Sdavidchbxe_get_igu_cam_info(struct bxe_softc *sc)
13581255736Sdavidch{
13582255736Sdavidch    int pfid = SC_FUNC(sc);
13583255736Sdavidch    int igu_sb_id;
13584255736Sdavidch    uint32_t val;
13585255736Sdavidch    uint8_t fid, igu_sb_cnt = 0;
13586255736Sdavidch
13587255736Sdavidch    sc->igu_base_sb = 0xff;
13588255736Sdavidch
13589255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
13590255736Sdavidch        int vn = SC_VN(sc);
13591255736Sdavidch        igu_sb_cnt = sc->igu_sb_cnt;
13592255736Sdavidch        sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
13593255736Sdavidch                           FP_SB_MAX_E1x);
13594255736Sdavidch        sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
13595255736Sdavidch                          (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
13596255736Sdavidch        return (0);
13597255736Sdavidch    }
13598255736Sdavidch
13599255736Sdavidch    /* IGU in normal mode - read CAM */
13600255736Sdavidch    for (igu_sb_id = 0;
13601255736Sdavidch         igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
13602255736Sdavidch         igu_sb_id++) {
13603255736Sdavidch        val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
13604255736Sdavidch        if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
13605255736Sdavidch            continue;
13606255736Sdavidch        }
13607255736Sdavidch        fid = IGU_FID(val);
13608255736Sdavidch        if ((fid & IGU_FID_ENCODE_IS_PF)) {
13609255736Sdavidch            if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
13610255736Sdavidch                continue;
13611255736Sdavidch            }
13612255736Sdavidch            if (IGU_VEC(val) == 0) {
13613255736Sdavidch                /* default status block */
13614255736Sdavidch                sc->igu_dsb_id = igu_sb_id;
13615255736Sdavidch            } else {
13616255736Sdavidch                if (sc->igu_base_sb == 0xff) {
13617255736Sdavidch                    sc->igu_base_sb = igu_sb_id;
13618255736Sdavidch                }
13619255736Sdavidch                igu_sb_cnt++;
13620255736Sdavidch            }
13621255736Sdavidch        }
13622255736Sdavidch    }
13623255736Sdavidch
13624255736Sdavidch    /*
13625255736Sdavidch     * Due to new PF resource allocation by MFW T7.4 and above, it's optional
13626255736Sdavidch     * that number of CAM entries will not be equal to the value advertised in
13627255736Sdavidch     * PCI. Driver should use the minimal value of both as the actual status
13628255736Sdavidch     * block count
13629255736Sdavidch     */
13630255736Sdavidch    sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
13631255736Sdavidch
13632255736Sdavidch    if (igu_sb_cnt == 0) {
13633255736Sdavidch        BLOGE(sc, "CAM configuration error\n");
13634255736Sdavidch        return (-1);
13635255736Sdavidch    }
13636255736Sdavidch
13637255736Sdavidch    return (0);
13638255736Sdavidch}
13639255736Sdavidch
13640255736Sdavidch/*
13641255736Sdavidch * Gather various information from the device config space, the device itself,
13642255736Sdavidch * shmem, and the user input.
13643255736Sdavidch */
13644255736Sdavidchstatic int
13645255736Sdavidchbxe_get_device_info(struct bxe_softc *sc)
13646255736Sdavidch{
13647255736Sdavidch    uint32_t val;
13648255736Sdavidch    int rc;
13649255736Sdavidch
13650255736Sdavidch    /* Get the data for the device */
13651255736Sdavidch    sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
13652255736Sdavidch    sc->devinfo.device_id    = pci_get_device(sc->dev);
13653255736Sdavidch    sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
13654255736Sdavidch    sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
13655255736Sdavidch
13656255736Sdavidch    /* get the chip revision (chip metal comes from pci config space) */
13657255736Sdavidch    sc->devinfo.chip_id     =
13658255736Sdavidch    sc->link_params.chip_id =
13659255736Sdavidch        (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
13660255736Sdavidch         ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
13661255736Sdavidch         (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
13662255736Sdavidch         ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
13663255736Sdavidch
13664255736Sdavidch    /* force 57811 according to MISC register */
13665255736Sdavidch    if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
13666255736Sdavidch        if (CHIP_IS_57810(sc)) {
13667255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
13668255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
13669255736Sdavidch        } else if (CHIP_IS_57810_MF(sc)) {
13670255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
13671255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
13672255736Sdavidch        }
13673255736Sdavidch        sc->devinfo.chip_id |= 0x1;
13674255736Sdavidch    }
13675255736Sdavidch
13676255736Sdavidch    BLOGD(sc, DBG_LOAD,
13677255736Sdavidch          "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
13678255736Sdavidch          sc->devinfo.chip_id,
13679255736Sdavidch          ((sc->devinfo.chip_id >> 16) & 0xffff),
13680255736Sdavidch          ((sc->devinfo.chip_id >> 12) & 0xf),
13681255736Sdavidch          ((sc->devinfo.chip_id >>  4) & 0xff),
13682255736Sdavidch          ((sc->devinfo.chip_id >>  0) & 0xf));
13683255736Sdavidch
13684255736Sdavidch    val = (REG_RD(sc, 0x2874) & 0x55);
13685255736Sdavidch    if ((sc->devinfo.chip_id & 0x1) ||
13686255736Sdavidch        (CHIP_IS_E1(sc) && val) ||
13687255736Sdavidch        (CHIP_IS_E1H(sc) && (val == 0x55))) {
13688255736Sdavidch        sc->flags |= BXE_ONE_PORT_FLAG;
13689255736Sdavidch        BLOGD(sc, DBG_LOAD, "single port device\n");
13690255736Sdavidch    }
13691255736Sdavidch
13692255736Sdavidch    /* set the doorbell size */
13693255736Sdavidch    sc->doorbell_size = (1 << BXE_DB_SHIFT);
13694255736Sdavidch
13695255736Sdavidch    /* determine whether the device is in 2 port or 4 port mode */
13696255736Sdavidch    sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
13697255736Sdavidch    if (CHIP_IS_E2E3(sc)) {
13698255736Sdavidch        /*
13699255736Sdavidch         * Read port4mode_en_ovwr[0]:
13700255736Sdavidch         *   If 1, four port mode is in port4mode_en_ovwr[1].
13701255736Sdavidch         *   If 0, four port mode is in port4mode_en[0].
13702255736Sdavidch         */
13703255736Sdavidch        val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
13704255736Sdavidch        if (val & 1) {
13705255736Sdavidch            val = ((val >> 1) & 1);
13706255736Sdavidch        } else {
13707255736Sdavidch            val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
13708255736Sdavidch        }
13709255736Sdavidch
13710255736Sdavidch        sc->devinfo.chip_port_mode =
13711255736Sdavidch            (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
13712255736Sdavidch
13713255736Sdavidch        BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
13714255736Sdavidch    }
13715255736Sdavidch
13716255736Sdavidch    /* get the function and path info for the device */
13717255736Sdavidch    bxe_get_function_num(sc);
13718255736Sdavidch
13719255736Sdavidch    /* get the shared memory base address */
13720255736Sdavidch    sc->devinfo.shmem_base     =
13721255736Sdavidch    sc->link_params.shmem_base =
13722255736Sdavidch        REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
13723255736Sdavidch    sc->devinfo.shmem2_base =
13724255736Sdavidch        REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
13725255736Sdavidch                                  MISC_REG_GENERIC_CR_0));
13726255736Sdavidch
13727255736Sdavidch    BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
13728255736Sdavidch          sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
13729255736Sdavidch
13730255736Sdavidch    if (!sc->devinfo.shmem_base) {
13731255736Sdavidch        /* this should ONLY prevent upcoming shmem reads */
13732255736Sdavidch        BLOGI(sc, "MCP not active\n");
13733255736Sdavidch        sc->flags |= BXE_NO_MCP_FLAG;
13734255736Sdavidch        return (0);
13735255736Sdavidch    }
13736255736Sdavidch
13737255736Sdavidch    /* make sure the shared memory contents are valid */
13738255736Sdavidch    val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
13739255736Sdavidch    if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
13740255736Sdavidch        (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
13741255736Sdavidch        BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
13742255736Sdavidch        return (0);
13743255736Sdavidch    }
13744255736Sdavidch    BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
13745255736Sdavidch
13746255736Sdavidch    /* get the bootcode version */
13747255736Sdavidch    sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
13748255736Sdavidch    snprintf(sc->devinfo.bc_ver_str,
13749255736Sdavidch             sizeof(sc->devinfo.bc_ver_str),
13750255736Sdavidch             "%d.%d.%d",
13751255736Sdavidch             ((sc->devinfo.bc_ver >> 24) & 0xff),
13752255736Sdavidch             ((sc->devinfo.bc_ver >> 16) & 0xff),
13753255736Sdavidch             ((sc->devinfo.bc_ver >>  8) & 0xff));
13754255736Sdavidch    BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
13755255736Sdavidch
13756255736Sdavidch    /* get the bootcode shmem address */
13757255736Sdavidch    sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
13758255736Sdavidch    BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
13759255736Sdavidch
13760255736Sdavidch    /* clean indirect addresses as they're not used */
13761255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
13762255736Sdavidch    if (IS_PF(sc)) {
13763255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
13764255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
13765255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
13766255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
13767255736Sdavidch        if (CHIP_IS_E1x(sc)) {
13768255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
13769255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
13770255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
13771255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
13772255736Sdavidch        }
13773255736Sdavidch
13774255736Sdavidch        /*
13775255736Sdavidch         * Enable internal target-read (in case we are probed after PF
13776255736Sdavidch         * FLR). Must be done prior to any BAR read access. Only for
13777255736Sdavidch         * 57712 and up
13778255736Sdavidch         */
13779255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
13780255736Sdavidch            REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
13781255736Sdavidch        }
13782255736Sdavidch    }
13783255736Sdavidch
13784255736Sdavidch    /* get the nvram size */
13785255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
13786255736Sdavidch    sc->devinfo.flash_size =
13787255736Sdavidch        (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
13788255736Sdavidch    BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
13789255736Sdavidch
13790255736Sdavidch    /* get PCI capabilites */
13791255736Sdavidch    bxe_probe_pci_caps(sc);
13792255736Sdavidch
13793255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
13794255736Sdavidch
13795255736Sdavidch    /* get various configuration parameters from shmem */
13796255736Sdavidch    bxe_get_shmem_info(sc);
13797255736Sdavidch
13798255736Sdavidch    if (sc->devinfo.pcie_msix_cap_reg != 0) {
13799255736Sdavidch        val = pci_read_config(sc->dev,
13800255736Sdavidch                              (sc->devinfo.pcie_msix_cap_reg +
13801255736Sdavidch                               PCIR_MSIX_CTRL),
13802255736Sdavidch                              2);
13803255736Sdavidch        sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
13804255736Sdavidch    } else {
13805255736Sdavidch        sc->igu_sb_cnt = 1;
13806255736Sdavidch    }
13807255736Sdavidch
13808255736Sdavidch    sc->igu_base_addr = BAR_IGU_INTMEM;
13809255736Sdavidch
13810255736Sdavidch    /* initialize IGU parameters */
13811255736Sdavidch    if (CHIP_IS_E1x(sc)) {
13812255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_HC;
13813255736Sdavidch        sc->igu_dsb_id = DEF_SB_IGU_ID;
13814255736Sdavidch        sc->igu_base_sb = 0;
13815255736Sdavidch    } else {
13816255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_IGU;
13817255736Sdavidch
13818255736Sdavidch        /* do not allow device reset during IGU info preocessing */
13819255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13820255736Sdavidch
13821255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
13822255736Sdavidch
13823255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13824255736Sdavidch            int tout = 5000;
13825255736Sdavidch
13826255736Sdavidch            BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
13827255736Sdavidch
13828255736Sdavidch            val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
13829255736Sdavidch            REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
13830255736Sdavidch            REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
13831255736Sdavidch
13832255736Sdavidch            while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13833255736Sdavidch                tout--;
13834255736Sdavidch                DELAY(1000);
13835255736Sdavidch            }
13836255736Sdavidch
13837255736Sdavidch            if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13838255736Sdavidch                BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
13839255736Sdavidch                bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13840255736Sdavidch                return (-1);
13841255736Sdavidch            }
13842255736Sdavidch        }
13843255736Sdavidch
13844255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13845255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
13846255736Sdavidch            sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
13847255736Sdavidch        } else {
13848255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
13849255736Sdavidch        }
13850255736Sdavidch
13851255736Sdavidch        rc = bxe_get_igu_cam_info(sc);
13852255736Sdavidch
13853255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13854255736Sdavidch
13855255736Sdavidch        if (rc) {
13856255736Sdavidch            return (rc);
13857255736Sdavidch        }
13858255736Sdavidch    }
13859255736Sdavidch
13860255736Sdavidch    /*
13861255736Sdavidch     * Get base FW non-default (fast path) status block ID. This value is
13862255736Sdavidch     * used to initialize the fw_sb_id saved on the fp/queue structure to
13863255736Sdavidch     * determine the id used by the FW.
13864255736Sdavidch     */
13865255736Sdavidch    if (CHIP_IS_E1x(sc)) {
13866255736Sdavidch        sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
13867255736Sdavidch    } else {
13868255736Sdavidch        /*
13869255736Sdavidch         * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
13870255736Sdavidch         * the same queue are indicated on the same IGU SB). So we prefer
13871255736Sdavidch         * FW and IGU SBs to be the same value.
13872255736Sdavidch         */
13873255736Sdavidch        sc->base_fw_ndsb = sc->igu_base_sb;
13874255736Sdavidch    }
13875255736Sdavidch
13876255736Sdavidch    BLOGD(sc, DBG_LOAD,
13877255736Sdavidch          "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
13878255736Sdavidch          sc->igu_dsb_id, sc->igu_base_sb,
13879255736Sdavidch          sc->igu_sb_cnt, sc->base_fw_ndsb);
13880255736Sdavidch
13881255736Sdavidch    elink_phy_probe(&sc->link_params);
13882255736Sdavidch
13883255736Sdavidch    return (0);
13884255736Sdavidch}
13885255736Sdavidch
13886255736Sdavidchstatic void
13887255736Sdavidchbxe_link_settings_supported(struct bxe_softc *sc,
13888255736Sdavidch                            uint32_t         switch_cfg)
13889255736Sdavidch{
13890255736Sdavidch    uint32_t cfg_size = 0;
13891255736Sdavidch    uint32_t idx;
13892255736Sdavidch    uint8_t port = SC_PORT(sc);
13893255736Sdavidch
13894255736Sdavidch    /* aggregation of supported attributes of all external phys */
13895255736Sdavidch    sc->port.supported[0] = 0;
13896255736Sdavidch    sc->port.supported[1] = 0;
13897255736Sdavidch
13898255736Sdavidch    switch (sc->link_params.num_phys) {
13899255736Sdavidch    case 1:
13900255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
13901255736Sdavidch        cfg_size = 1;
13902255736Sdavidch        break;
13903255736Sdavidch    case 2:
13904255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
13905255736Sdavidch        cfg_size = 1;
13906255736Sdavidch        break;
13907255736Sdavidch    case 3:
13908255736Sdavidch        if (sc->link_params.multi_phy_config &
13909255736Sdavidch            PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
13910255736Sdavidch            sc->port.supported[1] =
13911255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
13912255736Sdavidch            sc->port.supported[0] =
13913255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
13914255736Sdavidch        } else {
13915255736Sdavidch            sc->port.supported[0] =
13916255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
13917255736Sdavidch            sc->port.supported[1] =
13918255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
13919255736Sdavidch        }
13920255736Sdavidch        cfg_size = 2;
13921255736Sdavidch        break;
13922255736Sdavidch    }
13923255736Sdavidch
13924255736Sdavidch    if (!(sc->port.supported[0] || sc->port.supported[1])) {
13925255736Sdavidch        BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
13926255736Sdavidch              SHMEM_RD(sc,
13927255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config),
13928255736Sdavidch              SHMEM_RD(sc,
13929255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config2));
13930255736Sdavidch        return;
13931255736Sdavidch    }
13932255736Sdavidch
13933255736Sdavidch    if (CHIP_IS_E3(sc))
13934255736Sdavidch        sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
13935255736Sdavidch    else {
13936255736Sdavidch        switch (switch_cfg) {
13937255736Sdavidch        case ELINK_SWITCH_CFG_1G:
13938255736Sdavidch            sc->port.phy_addr =
13939255736Sdavidch                REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
13940255736Sdavidch            break;
13941255736Sdavidch        case ELINK_SWITCH_CFG_10G:
13942255736Sdavidch            sc->port.phy_addr =
13943255736Sdavidch                REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
13944255736Sdavidch            break;
13945255736Sdavidch        default:
13946255736Sdavidch            BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
13947255736Sdavidch                  sc->port.link_config[0]);
13948255736Sdavidch            return;
13949255736Sdavidch        }
13950255736Sdavidch    }
13951255736Sdavidch
13952255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
13953255736Sdavidch
13954255736Sdavidch    /* mask what we support according to speed_cap_mask per configuration */
13955255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
13956255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13957255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
13958255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
13959255736Sdavidch        }
13960255736Sdavidch
13961255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13962255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
13963255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
13964255736Sdavidch        }
13965255736Sdavidch
13966255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13967255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
13968255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
13969255736Sdavidch        }
13970255736Sdavidch
13971255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13972255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
13973255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
13974255736Sdavidch        }
13975255736Sdavidch
13976255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13977255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
13978255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
13979255736Sdavidch        }
13980255736Sdavidch
13981255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13982255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
13983255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
13984255736Sdavidch        }
13985255736Sdavidch
13986255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13987255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
13988255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
13989255736Sdavidch        }
13990255736Sdavidch
13991255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
13992255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
13993255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
13994255736Sdavidch        }
13995255736Sdavidch    }
13996255736Sdavidch
13997255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
13998255736Sdavidch          sc->port.supported[0], sc->port.supported[1]);
13999255736Sdavidch}
14000255736Sdavidch
14001255736Sdavidchstatic void
14002255736Sdavidchbxe_link_settings_requested(struct bxe_softc *sc)
14003255736Sdavidch{
14004255736Sdavidch    uint32_t link_config;
14005255736Sdavidch    uint32_t idx;
14006255736Sdavidch    uint32_t cfg_size = 0;
14007255736Sdavidch
14008255736Sdavidch    sc->port.advertising[0] = 0;
14009255736Sdavidch    sc->port.advertising[1] = 0;
14010255736Sdavidch
14011255736Sdavidch    switch (sc->link_params.num_phys) {
14012255736Sdavidch    case 1:
14013255736Sdavidch    case 2:
14014255736Sdavidch        cfg_size = 1;
14015255736Sdavidch        break;
14016255736Sdavidch    case 3:
14017255736Sdavidch        cfg_size = 2;
14018255736Sdavidch        break;
14019255736Sdavidch    }
14020255736Sdavidch
14021255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14022255736Sdavidch        sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14023255736Sdavidch        link_config = sc->port.link_config[idx];
14024255736Sdavidch
14025255736Sdavidch        switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14026255736Sdavidch        case PORT_FEATURE_LINK_SPEED_AUTO:
14027255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14028255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14029255736Sdavidch                sc->port.advertising[idx] |= sc->port.supported[idx];
14030255736Sdavidch                if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14031255736Sdavidch                    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14032255736Sdavidch                    sc->port.advertising[idx] |=
14033255736Sdavidch                        (ELINK_SUPPORTED_100baseT_Half |
14034255736Sdavidch                         ELINK_SUPPORTED_100baseT_Full);
14035255736Sdavidch            } else {
14036255736Sdavidch                /* force 10G, no AN */
14037255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14038255736Sdavidch                sc->port.advertising[idx] |=
14039255736Sdavidch                    (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14040255736Sdavidch                continue;
14041255736Sdavidch            }
14042255736Sdavidch            break;
14043255736Sdavidch
14044255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_FULL:
14045255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14046255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14047255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14048255736Sdavidch                                              ADVERTISED_TP);
14049255736Sdavidch            } else {
14050255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14051255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14052255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14053255736Sdavidch                return;
14054255736Sdavidch            }
14055255736Sdavidch            break;
14056255736Sdavidch
14057255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_HALF:
14058255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14059255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14060255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14061255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14062255736Sdavidch                                              ADVERTISED_TP);
14063255736Sdavidch            } else {
14064255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14065255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14066255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14067255736Sdavidch                return;
14068255736Sdavidch            }
14069255736Sdavidch            break;
14070255736Sdavidch
14071255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_FULL:
14072255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14073255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14074255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14075255736Sdavidch                                              ADVERTISED_TP);
14076255736Sdavidch            } else {
14077255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14078255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14079255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14080255736Sdavidch                return;
14081255736Sdavidch            }
14082255736Sdavidch            break;
14083255736Sdavidch
14084255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_HALF:
14085255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14086255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14087255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14088255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14089255736Sdavidch                                              ADVERTISED_TP);
14090255736Sdavidch            } else {
14091255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14092255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14093255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14094255736Sdavidch                return;
14095255736Sdavidch            }
14096255736Sdavidch            break;
14097255736Sdavidch
14098255736Sdavidch        case PORT_FEATURE_LINK_SPEED_1G:
14099255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14100255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14101255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14102255736Sdavidch                                              ADVERTISED_TP);
14103255736Sdavidch            } else {
14104255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14105255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14106255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14107255736Sdavidch                return;
14108255736Sdavidch            }
14109255736Sdavidch            break;
14110255736Sdavidch
14111255736Sdavidch        case PORT_FEATURE_LINK_SPEED_2_5G:
14112255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14113255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14114255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14115255736Sdavidch                                              ADVERTISED_TP);
14116255736Sdavidch            } else {
14117255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14118255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14119255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14120255736Sdavidch                return;
14121255736Sdavidch            }
14122255736Sdavidch            break;
14123255736Sdavidch
14124255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10G_CX4:
14125255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14126255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14127255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14128255736Sdavidch                                              ADVERTISED_FIBRE);
14129255736Sdavidch            } else {
14130255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14131255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14132255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14133255736Sdavidch                return;
14134255736Sdavidch            }
14135255736Sdavidch            break;
14136255736Sdavidch
14137255736Sdavidch        case PORT_FEATURE_LINK_SPEED_20G:
14138255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14139255736Sdavidch            break;
14140255736Sdavidch
14141255736Sdavidch        default:
14142255736Sdavidch            BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14143255736Sdavidch                      "speed_cap_mask=0x%08x\n",
14144255736Sdavidch                  link_config, sc->link_params.speed_cap_mask[idx]);
14145255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14146255736Sdavidch            sc->port.advertising[idx] = sc->port.supported[idx];
14147255736Sdavidch            break;
14148255736Sdavidch        }
14149255736Sdavidch
14150255736Sdavidch        sc->link_params.req_flow_ctrl[idx] =
14151255736Sdavidch            (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14152255736Sdavidch
14153255736Sdavidch        if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14154255736Sdavidch            if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14155255736Sdavidch                sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14156255736Sdavidch            } else {
14157255736Sdavidch                bxe_set_requested_fc(sc);
14158255736Sdavidch            }
14159255736Sdavidch        }
14160255736Sdavidch
14161255736Sdavidch        BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14162255736Sdavidch                            "req_flow_ctrl=0x%x advertising=0x%x\n",
14163255736Sdavidch              sc->link_params.req_line_speed[idx],
14164255736Sdavidch              sc->link_params.req_duplex[idx],
14165255736Sdavidch              sc->link_params.req_flow_ctrl[idx],
14166255736Sdavidch              sc->port.advertising[idx]);
14167255736Sdavidch    }
14168255736Sdavidch}
14169255736Sdavidch
14170255736Sdavidchstatic void
14171255736Sdavidchbxe_get_phy_info(struct bxe_softc *sc)
14172255736Sdavidch{
14173255736Sdavidch    uint8_t port = SC_PORT(sc);
14174255736Sdavidch    uint32_t config = sc->port.config;
14175255736Sdavidch    uint32_t eee_mode;
14176255736Sdavidch
14177255736Sdavidch    /* shmem data already read in bxe_get_shmem_info() */
14178255736Sdavidch
14179255736Sdavidch    BLOGD(sc, DBG_LOAD, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14180255736Sdavidch                        "link_config0=0x%08x\n",
14181255736Sdavidch               sc->link_params.lane_config,
14182255736Sdavidch               sc->link_params.speed_cap_mask[0],
14183255736Sdavidch               sc->port.link_config[0]);
14184255736Sdavidch
14185255736Sdavidch    bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14186255736Sdavidch    bxe_link_settings_requested(sc);
14187255736Sdavidch
14188255736Sdavidch    if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14189255736Sdavidch        sc->link_params.feature_config_flags |=
14190255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14191255736Sdavidch    } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14192255736Sdavidch        sc->link_params.feature_config_flags &=
14193255736Sdavidch            ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14194255736Sdavidch    } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14195255736Sdavidch        sc->link_params.feature_config_flags |=
14196255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14197255736Sdavidch    }
14198255736Sdavidch
14199255736Sdavidch    /* configure link feature according to nvram value */
14200255736Sdavidch    eee_mode =
14201255736Sdavidch        (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14202255736Sdavidch          PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14203255736Sdavidch         PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14204255736Sdavidch    if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14205255736Sdavidch        sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14206255736Sdavidch                                    ELINK_EEE_MODE_ENABLE_LPI |
14207255736Sdavidch                                    ELINK_EEE_MODE_OUTPUT_TIME);
14208255736Sdavidch    } else {
14209255736Sdavidch        sc->link_params.eee_mode = 0;
14210255736Sdavidch    }
14211255736Sdavidch
14212255736Sdavidch    /* get the media type */
14213255736Sdavidch    bxe_media_detect(sc);
14214255736Sdavidch}
14215255736Sdavidch
14216255736Sdavidchstatic void
14217255736Sdavidchbxe_get_params(struct bxe_softc *sc)
14218255736Sdavidch{
14219255736Sdavidch    /* get user tunable params */
14220255736Sdavidch    bxe_get_tunable_params(sc);
14221255736Sdavidch
14222255736Sdavidch    /* select the RX and TX ring sizes */
14223255736Sdavidch    sc->tx_ring_size = TX_BD_USABLE;
14224255736Sdavidch    sc->rx_ring_size = RX_BD_USABLE;
14225255736Sdavidch
14226255736Sdavidch    /* XXX disable WoL */
14227255736Sdavidch    sc->wol = 0;
14228255736Sdavidch}
14229255736Sdavidch
14230255736Sdavidchstatic void
14231255736Sdavidchbxe_set_modes_bitmap(struct bxe_softc *sc)
14232255736Sdavidch{
14233255736Sdavidch    uint32_t flags = 0;
14234255736Sdavidch
14235255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
14236255736Sdavidch        SET_FLAGS(flags, MODE_FPGA);
14237255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
14238255736Sdavidch        SET_FLAGS(flags, MODE_EMUL);
14239255736Sdavidch    } else {
14240255736Sdavidch        SET_FLAGS(flags, MODE_ASIC);
14241255736Sdavidch    }
14242255736Sdavidch
14243255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
14244255736Sdavidch        SET_FLAGS(flags, MODE_PORT4);
14245255736Sdavidch    } else {
14246255736Sdavidch        SET_FLAGS(flags, MODE_PORT2);
14247255736Sdavidch    }
14248255736Sdavidch
14249255736Sdavidch    if (CHIP_IS_E2(sc)) {
14250255736Sdavidch        SET_FLAGS(flags, MODE_E2);
14251255736Sdavidch    } else if (CHIP_IS_E3(sc)) {
14252255736Sdavidch        SET_FLAGS(flags, MODE_E3);
14253255736Sdavidch        if (CHIP_REV(sc) == CHIP_REV_Ax) {
14254255736Sdavidch            SET_FLAGS(flags, MODE_E3_A0);
14255255736Sdavidch        } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14256255736Sdavidch            SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14257255736Sdavidch        }
14258255736Sdavidch    }
14259255736Sdavidch
14260255736Sdavidch    if (IS_MF(sc)) {
14261255736Sdavidch        SET_FLAGS(flags, MODE_MF);
14262255736Sdavidch        switch (sc->devinfo.mf_info.mf_mode) {
14263255736Sdavidch        case MULTI_FUNCTION_SD:
14264255736Sdavidch            SET_FLAGS(flags, MODE_MF_SD);
14265255736Sdavidch            break;
14266255736Sdavidch        case MULTI_FUNCTION_SI:
14267255736Sdavidch            SET_FLAGS(flags, MODE_MF_SI);
14268255736Sdavidch            break;
14269255736Sdavidch        case MULTI_FUNCTION_AFEX:
14270255736Sdavidch            SET_FLAGS(flags, MODE_MF_AFEX);
14271255736Sdavidch            break;
14272255736Sdavidch        }
14273255736Sdavidch    } else {
14274255736Sdavidch        SET_FLAGS(flags, MODE_SF);
14275255736Sdavidch    }
14276255736Sdavidch
14277255736Sdavidch#if defined(__LITTLE_ENDIAN)
14278255736Sdavidch    SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14279255736Sdavidch#else /* __BIG_ENDIAN */
14280255736Sdavidch    SET_FLAGS(flags, MODE_BIG_ENDIAN);
14281255736Sdavidch#endif
14282255736Sdavidch
14283255736Sdavidch    INIT_MODE_FLAGS(sc) = flags;
14284255736Sdavidch}
14285255736Sdavidch
14286255736Sdavidchstatic int
14287255736Sdavidchbxe_alloc_hsi_mem(struct bxe_softc *sc)
14288255736Sdavidch{
14289255736Sdavidch    struct bxe_fastpath *fp;
14290255736Sdavidch    bus_addr_t busaddr;
14291255736Sdavidch    int max_agg_queues;
14292255736Sdavidch    int max_segments;
14293255736Sdavidch    bus_size_t max_size;
14294255736Sdavidch    bus_size_t max_seg_size;
14295255736Sdavidch    char buf[32];
14296255736Sdavidch    int rc;
14297255736Sdavidch    int i, j;
14298255736Sdavidch
14299255736Sdavidch    /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14300255736Sdavidch
14301255736Sdavidch    /* allocate the parent bus DMA tag */
14302255736Sdavidch    rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14303255736Sdavidch                            1,                        /* alignment */
14304255736Sdavidch                            0,                        /* boundary limit */
14305255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted low */
14306255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted hi */
14307255736Sdavidch                            NULL,                     /* addr filter() */
14308255736Sdavidch                            NULL,                     /* addr filter() arg */
14309255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14310255736Sdavidch                            BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14311255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14312255736Sdavidch                            0,                        /* flags */
14313255736Sdavidch                            NULL,                     /* lock() */
14314255736Sdavidch                            NULL,                     /* lock() arg */
14315255736Sdavidch                            &sc->parent_dma_tag);     /* returned dma tag */
14316255736Sdavidch    if (rc != 0) {
14317255736Sdavidch        BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14318255736Sdavidch        return (1);
14319255736Sdavidch    }
14320255736Sdavidch
14321255736Sdavidch    /************************/
14322255736Sdavidch    /* DEFAULT STATUS BLOCK */
14323255736Sdavidch    /************************/
14324255736Sdavidch
14325255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14326255736Sdavidch                      &sc->def_sb_dma, "default status block") != 0) {
14327255736Sdavidch        /* XXX */
14328255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14329255736Sdavidch        return (1);
14330255736Sdavidch    }
14331255736Sdavidch
14332255736Sdavidch    sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14333255736Sdavidch
14334255736Sdavidch    /***************/
14335255736Sdavidch    /* EVENT QUEUE */
14336255736Sdavidch    /***************/
14337255736Sdavidch
14338255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14339255736Sdavidch                      &sc->eq_dma, "event queue") != 0) {
14340255736Sdavidch        /* XXX */
14341255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14342255736Sdavidch        sc->def_sb = NULL;
14343255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14344255736Sdavidch        return (1);
14345255736Sdavidch    }
14346255736Sdavidch
14347255736Sdavidch    sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14348255736Sdavidch
14349255736Sdavidch    /*************/
14350255736Sdavidch    /* SLOW PATH */
14351255736Sdavidch    /*************/
14352255736Sdavidch
14353255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14354255736Sdavidch                      &sc->sp_dma, "slow path") != 0) {
14355255736Sdavidch        /* XXX */
14356255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14357255736Sdavidch        sc->eq = NULL;
14358255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14359255736Sdavidch        sc->def_sb = NULL;
14360255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14361255736Sdavidch        return (1);
14362255736Sdavidch    }
14363255736Sdavidch
14364255736Sdavidch    sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14365255736Sdavidch
14366255736Sdavidch    /*******************/
14367255736Sdavidch    /* SLOW PATH QUEUE */
14368255736Sdavidch    /*******************/
14369255736Sdavidch
14370255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14371255736Sdavidch                      &sc->spq_dma, "slow path queue") != 0) {
14372255736Sdavidch        /* XXX */
14373255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14374255736Sdavidch        sc->sp = NULL;
14375255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14376255736Sdavidch        sc->eq = NULL;
14377255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14378255736Sdavidch        sc->def_sb = NULL;
14379255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14380255736Sdavidch        return (1);
14381255736Sdavidch    }
14382255736Sdavidch
14383255736Sdavidch    sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14384255736Sdavidch
14385255736Sdavidch    /***************************/
14386255736Sdavidch    /* FW DECOMPRESSION BUFFER */
14387255736Sdavidch    /***************************/
14388255736Sdavidch
14389255736Sdavidch    if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14390255736Sdavidch                      "fw decompression buffer") != 0) {
14391255736Sdavidch        /* XXX */
14392255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14393255736Sdavidch        sc->spq = NULL;
14394255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14395255736Sdavidch        sc->sp = NULL;
14396255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14397255736Sdavidch        sc->eq = NULL;
14398255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14399255736Sdavidch        sc->def_sb = NULL;
14400255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14401255736Sdavidch        return (1);
14402255736Sdavidch    }
14403255736Sdavidch
14404255736Sdavidch    sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14405255736Sdavidch
14406255736Sdavidch    if ((sc->gz_strm =
14407255736Sdavidch         malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14408255736Sdavidch        /* XXX */
14409255736Sdavidch        bxe_dma_free(sc, &sc->gz_buf_dma);
14410255736Sdavidch        sc->gz_buf = NULL;
14411255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14412255736Sdavidch        sc->spq = NULL;
14413255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14414255736Sdavidch        sc->sp = NULL;
14415255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14416255736Sdavidch        sc->eq = NULL;
14417255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14418255736Sdavidch        sc->def_sb = NULL;
14419255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14420255736Sdavidch        return (1);
14421255736Sdavidch    }
14422255736Sdavidch
14423255736Sdavidch    /*************/
14424255736Sdavidch    /* FASTPATHS */
14425255736Sdavidch    /*************/
14426255736Sdavidch
14427255736Sdavidch    /* allocate DMA memory for each fastpath structure */
14428255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
14429255736Sdavidch        fp = &sc->fp[i];
14430255736Sdavidch        fp->sc    = sc;
14431255736Sdavidch        fp->index = i;
14432255736Sdavidch
14433255736Sdavidch        /*******************/
14434255736Sdavidch        /* FP STATUS BLOCK */
14435255736Sdavidch        /*******************/
14436255736Sdavidch
14437255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d status block", i);
14438255736Sdavidch        if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14439255736Sdavidch                          &fp->sb_dma, buf) != 0) {
14440255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14441255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14442255736Sdavidch            return (1);
14443255736Sdavidch        } else {
14444255736Sdavidch            if (CHIP_IS_E2E3(sc)) {
14445255736Sdavidch                fp->status_block.e2_sb =
14446255736Sdavidch                    (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14447255736Sdavidch            } else {
14448255736Sdavidch                fp->status_block.e1x_sb =
14449255736Sdavidch                    (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14450255736Sdavidch            }
14451255736Sdavidch        }
14452255736Sdavidch
14453255736Sdavidch        /******************/
14454255736Sdavidch        /* FP TX BD CHAIN */
14455255736Sdavidch        /******************/
14456255736Sdavidch
14457255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14458255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14459255736Sdavidch                          &fp->tx_dma, buf) != 0) {
14460255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14461255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14462255736Sdavidch            return (1);
14463255736Sdavidch        } else {
14464255736Sdavidch            fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14465255736Sdavidch        }
14466255736Sdavidch
14467255736Sdavidch        /* link together the tx bd chain pages */
14468255736Sdavidch        for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14469255736Sdavidch            /* index into the tx bd chain array to last entry per page */
14470255736Sdavidch            struct eth_tx_next_bd *tx_next_bd =
14471255736Sdavidch                &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14472255736Sdavidch            /* point to the next page and wrap from last page */
14473255736Sdavidch            busaddr = (fp->tx_dma.paddr +
14474255736Sdavidch                       (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14475255736Sdavidch            tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14476255736Sdavidch            tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14477255736Sdavidch        }
14478255736Sdavidch
14479255736Sdavidch        /******************/
14480255736Sdavidch        /* FP RX BD CHAIN */
14481255736Sdavidch        /******************/
14482255736Sdavidch
14483255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14484255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14485255736Sdavidch                          &fp->rx_dma, buf) != 0) {
14486255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14487255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14488255736Sdavidch            return (1);
14489255736Sdavidch        } else {
14490255736Sdavidch            fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14491255736Sdavidch        }
14492255736Sdavidch
14493255736Sdavidch        /* link together the rx bd chain pages */
14494255736Sdavidch        for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14495255736Sdavidch            /* index into the rx bd chain array to last entry per page */
14496255736Sdavidch            struct eth_rx_bd *rx_bd =
14497255736Sdavidch                &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14498255736Sdavidch            /* point to the next page and wrap from last page */
14499255736Sdavidch            busaddr = (fp->rx_dma.paddr +
14500255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14501255736Sdavidch            rx_bd->addr_hi = htole32(U64_HI(busaddr));
14502255736Sdavidch            rx_bd->addr_lo = htole32(U64_LO(busaddr));
14503255736Sdavidch        }
14504255736Sdavidch
14505255736Sdavidch        /*******************/
14506255736Sdavidch        /* FP RX RCQ CHAIN */
14507255736Sdavidch        /*******************/
14508255736Sdavidch
14509255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14510255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14511255736Sdavidch                          &fp->rcq_dma, buf) != 0) {
14512255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14513255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14514255736Sdavidch            return (1);
14515255736Sdavidch        } else {
14516255736Sdavidch            fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14517255736Sdavidch        }
14518255736Sdavidch
14519255736Sdavidch        /* link together the rcq chain pages */
14520255736Sdavidch        for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14521255736Sdavidch            /* index into the rcq chain array to last entry per page */
14522255736Sdavidch            struct eth_rx_cqe_next_page *rx_cqe_next =
14523255736Sdavidch                (struct eth_rx_cqe_next_page *)
14524255736Sdavidch                &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14525255736Sdavidch            /* point to the next page and wrap from last page */
14526255736Sdavidch            busaddr = (fp->rcq_dma.paddr +
14527255736Sdavidch                       (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14528255736Sdavidch            rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14529255736Sdavidch            rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14530255736Sdavidch        }
14531255736Sdavidch
14532255736Sdavidch        /*******************/
14533255736Sdavidch        /* FP RX SGE CHAIN */
14534255736Sdavidch        /*******************/
14535255736Sdavidch
14536255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14537255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14538255736Sdavidch                          &fp->rx_sge_dma, buf) != 0) {
14539255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14540255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14541255736Sdavidch            return (1);
14542255736Sdavidch        } else {
14543255736Sdavidch            fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14544255736Sdavidch        }
14545255736Sdavidch
14546255736Sdavidch        /* link together the sge chain pages */
14547255736Sdavidch        for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14548255736Sdavidch            /* index into the rcq chain array to last entry per page */
14549255736Sdavidch            struct eth_rx_sge *rx_sge =
14550255736Sdavidch                &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14551255736Sdavidch            /* point to the next page and wrap from last page */
14552255736Sdavidch            busaddr = (fp->rx_sge_dma.paddr +
14553255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14554255736Sdavidch            rx_sge->addr_hi = htole32(U64_HI(busaddr));
14555255736Sdavidch            rx_sge->addr_lo = htole32(U64_LO(busaddr));
14556255736Sdavidch        }
14557255736Sdavidch
14558255736Sdavidch        /***********************/
14559255736Sdavidch        /* FP TX MBUF DMA MAPS */
14560255736Sdavidch        /***********************/
14561255736Sdavidch
14562255736Sdavidch        /* set required sizes before mapping to conserve resources */
14563266979Smarcel        if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
14564255736Sdavidch            max_size     = BXE_TSO_MAX_SIZE;
14565255736Sdavidch            max_segments = BXE_TSO_MAX_SEGMENTS;
14566255736Sdavidch            max_seg_size = BXE_TSO_MAX_SEG_SIZE;
14567255736Sdavidch        } else {
14568255736Sdavidch            max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
14569255736Sdavidch            max_segments = BXE_MAX_SEGMENTS;
14570255736Sdavidch            max_seg_size = MCLBYTES;
14571255736Sdavidch        }
14572255736Sdavidch
14573255736Sdavidch        /* create a dma tag for the tx mbufs */
14574255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14575255736Sdavidch                                1,                  /* alignment */
14576255736Sdavidch                                0,                  /* boundary limit */
14577255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14578255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14579255736Sdavidch                                NULL,               /* addr filter() */
14580255736Sdavidch                                NULL,               /* addr filter() arg */
14581255736Sdavidch                                max_size,           /* max map size */
14582255736Sdavidch                                max_segments,       /* num discontinuous */
14583255736Sdavidch                                max_seg_size,       /* max seg size */
14584255736Sdavidch                                0,                  /* flags */
14585255736Sdavidch                                NULL,               /* lock() */
14586255736Sdavidch                                NULL,               /* lock() arg */
14587255736Sdavidch                                &fp->tx_mbuf_tag);  /* returned dma tag */
14588255736Sdavidch        if (rc != 0) {
14589255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14590255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14591295830Sdavidcs                      "'fp %d tx mbufs' (%d)\n", i, rc);
14592255736Sdavidch            return (1);
14593255736Sdavidch        }
14594255736Sdavidch
14595255736Sdavidch        /* create dma maps for each of the tx mbuf clusters */
14596255736Sdavidch        for (j = 0; j < TX_BD_TOTAL; j++) {
14597255736Sdavidch            if (bus_dmamap_create(fp->tx_mbuf_tag,
14598255736Sdavidch                                  BUS_DMA_NOWAIT,
14599255736Sdavidch                                  &fp->tx_mbuf_chain[j].m_map)) {
14600255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14601255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14602295830Sdavidcs                          "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
14603255736Sdavidch                return (1);
14604255736Sdavidch            }
14605255736Sdavidch        }
14606255736Sdavidch
14607255736Sdavidch        /***********************/
14608255736Sdavidch        /* FP RX MBUF DMA MAPS */
14609255736Sdavidch        /***********************/
14610255736Sdavidch
14611255736Sdavidch        /* create a dma tag for the rx mbufs */
14612255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14613255736Sdavidch                                1,                  /* alignment */
14614255736Sdavidch                                0,                  /* boundary limit */
14615255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14616255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14617255736Sdavidch                                NULL,               /* addr filter() */
14618255736Sdavidch                                NULL,               /* addr filter() arg */
14619255736Sdavidch                                MJUM9BYTES,         /* max map size */
14620255736Sdavidch                                1,                  /* num discontinuous */
14621255736Sdavidch                                MJUM9BYTES,         /* max seg size */
14622255736Sdavidch                                0,                  /* flags */
14623255736Sdavidch                                NULL,               /* lock() */
14624255736Sdavidch                                NULL,               /* lock() arg */
14625255736Sdavidch                                &fp->rx_mbuf_tag);  /* returned dma tag */
14626255736Sdavidch        if (rc != 0) {
14627255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14628255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14629295830Sdavidcs                      "'fp %d rx mbufs' (%d)\n", i, rc);
14630255736Sdavidch            return (1);
14631255736Sdavidch        }
14632255736Sdavidch
14633255736Sdavidch        /* create dma maps for each of the rx mbuf clusters */
14634255736Sdavidch        for (j = 0; j < RX_BD_TOTAL; j++) {
14635255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
14636255736Sdavidch                                  BUS_DMA_NOWAIT,
14637255736Sdavidch                                  &fp->rx_mbuf_chain[j].m_map)) {
14638255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14639255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14640295830Sdavidcs                          "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
14641255736Sdavidch                return (1);
14642255736Sdavidch            }
14643255736Sdavidch        }
14644255736Sdavidch
14645255736Sdavidch        /* create dma map for the spare rx mbuf cluster */
14646255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
14647255736Sdavidch                              BUS_DMA_NOWAIT,
14648255736Sdavidch                              &fp->rx_mbuf_spare_map)) {
14649255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14650255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14651295830Sdavidcs                      "'fp %d spare rx mbuf' (%d)\n", i, rc);
14652255736Sdavidch            return (1);
14653255736Sdavidch        }
14654255736Sdavidch
14655255736Sdavidch        /***************************/
14656255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
14657255736Sdavidch        /***************************/
14658255736Sdavidch
14659255736Sdavidch        /* create a dma tag for the rx sge mbufs */
14660255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14661255736Sdavidch                                1,                  /* alignment */
14662255736Sdavidch                                0,                  /* boundary limit */
14663255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14664255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14665255736Sdavidch                                NULL,               /* addr filter() */
14666255736Sdavidch                                NULL,               /* addr filter() arg */
14667255736Sdavidch                                BCM_PAGE_SIZE,      /* max map size */
14668255736Sdavidch                                1,                  /* num discontinuous */
14669255736Sdavidch                                BCM_PAGE_SIZE,      /* max seg size */
14670255736Sdavidch                                0,                  /* flags */
14671255736Sdavidch                                NULL,               /* lock() */
14672255736Sdavidch                                NULL,               /* lock() arg */
14673255736Sdavidch                                &fp->rx_sge_mbuf_tag); /* returned dma tag */
14674255736Sdavidch        if (rc != 0) {
14675255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14676255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14677295830Sdavidcs                      "'fp %d rx sge mbufs' (%d)\n", i, rc);
14678255736Sdavidch            return (1);
14679255736Sdavidch        }
14680255736Sdavidch
14681255736Sdavidch        /* create dma maps for the rx sge mbuf clusters */
14682255736Sdavidch        for (j = 0; j < RX_SGE_TOTAL; j++) {
14683255736Sdavidch            if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14684255736Sdavidch                                  BUS_DMA_NOWAIT,
14685255736Sdavidch                                  &fp->rx_sge_mbuf_chain[j].m_map)) {
14686255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14687255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14688295830Sdavidcs                          "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
14689255736Sdavidch                return (1);
14690255736Sdavidch            }
14691255736Sdavidch        }
14692255736Sdavidch
14693255736Sdavidch        /* create dma map for the spare rx sge mbuf cluster */
14694255736Sdavidch        if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14695255736Sdavidch                              BUS_DMA_NOWAIT,
14696255736Sdavidch                              &fp->rx_sge_mbuf_spare_map)) {
14697255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14698255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14699295830Sdavidcs                      "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
14700255736Sdavidch            return (1);
14701255736Sdavidch        }
14702255736Sdavidch
14703255736Sdavidch        /***************************/
14704255736Sdavidch        /* FP RX TPA MBUF DMA MAPS */
14705255736Sdavidch        /***************************/
14706255736Sdavidch
14707255736Sdavidch        /* create dma maps for the rx tpa mbuf clusters */
14708255736Sdavidch        max_agg_queues = MAX_AGG_QS(sc);
14709255736Sdavidch
14710255736Sdavidch        for (j = 0; j < max_agg_queues; j++) {
14711255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
14712255736Sdavidch                                  BUS_DMA_NOWAIT,
14713255736Sdavidch                                  &fp->rx_tpa_info[j].bd.m_map)) {
14714255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14715255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14716295830Sdavidcs                          "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
14717255736Sdavidch                return (1);
14718255736Sdavidch            }
14719255736Sdavidch        }
14720255736Sdavidch
14721255736Sdavidch        /* create dma map for the spare rx tpa mbuf cluster */
14722255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
14723255736Sdavidch                              BUS_DMA_NOWAIT,
14724255736Sdavidch                              &fp->rx_tpa_info_mbuf_spare_map)) {
14725255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14726255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14727295830Sdavidcs                      "'fp %d spare rx tpa mbuf' (%d)\n", i, rc);
14728255736Sdavidch            return (1);
14729255736Sdavidch        }
14730255736Sdavidch
14731255736Sdavidch        bxe_init_sge_ring_bit_mask(fp);
14732255736Sdavidch    }
14733255736Sdavidch
14734255736Sdavidch    return (0);
14735255736Sdavidch}
14736255736Sdavidch
14737255736Sdavidchstatic void
14738255736Sdavidchbxe_free_hsi_mem(struct bxe_softc *sc)
14739255736Sdavidch{
14740255736Sdavidch    struct bxe_fastpath *fp;
14741255736Sdavidch    int max_agg_queues;
14742255736Sdavidch    int i, j;
14743255736Sdavidch
14744255736Sdavidch    if (sc->parent_dma_tag == NULL) {
14745255736Sdavidch        return; /* assume nothing was allocated */
14746255736Sdavidch    }
14747255736Sdavidch
14748255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
14749255736Sdavidch        fp = &sc->fp[i];
14750255736Sdavidch
14751255736Sdavidch        /*******************/
14752255736Sdavidch        /* FP STATUS BLOCK */
14753255736Sdavidch        /*******************/
14754255736Sdavidch
14755255736Sdavidch        bxe_dma_free(sc, &fp->sb_dma);
14756255736Sdavidch        memset(&fp->status_block, 0, sizeof(fp->status_block));
14757255736Sdavidch
14758255736Sdavidch        /******************/
14759255736Sdavidch        /* FP TX BD CHAIN */
14760255736Sdavidch        /******************/
14761255736Sdavidch
14762255736Sdavidch        bxe_dma_free(sc, &fp->tx_dma);
14763255736Sdavidch        fp->tx_chain = NULL;
14764255736Sdavidch
14765255736Sdavidch        /******************/
14766255736Sdavidch        /* FP RX BD CHAIN */
14767255736Sdavidch        /******************/
14768255736Sdavidch
14769255736Sdavidch        bxe_dma_free(sc, &fp->rx_dma);
14770255736Sdavidch        fp->rx_chain = NULL;
14771255736Sdavidch
14772255736Sdavidch        /*******************/
14773255736Sdavidch        /* FP RX RCQ CHAIN */
14774255736Sdavidch        /*******************/
14775255736Sdavidch
14776255736Sdavidch        bxe_dma_free(sc, &fp->rcq_dma);
14777255736Sdavidch        fp->rcq_chain = NULL;
14778255736Sdavidch
14779255736Sdavidch        /*******************/
14780255736Sdavidch        /* FP RX SGE CHAIN */
14781255736Sdavidch        /*******************/
14782255736Sdavidch
14783255736Sdavidch        bxe_dma_free(sc, &fp->rx_sge_dma);
14784255736Sdavidch        fp->rx_sge_chain = NULL;
14785255736Sdavidch
14786255736Sdavidch        /***********************/
14787255736Sdavidch        /* FP TX MBUF DMA MAPS */
14788255736Sdavidch        /***********************/
14789255736Sdavidch
14790255736Sdavidch        if (fp->tx_mbuf_tag != NULL) {
14791255736Sdavidch            for (j = 0; j < TX_BD_TOTAL; j++) {
14792255736Sdavidch                if (fp->tx_mbuf_chain[j].m_map != NULL) {
14793255736Sdavidch                    bus_dmamap_unload(fp->tx_mbuf_tag,
14794255736Sdavidch                                      fp->tx_mbuf_chain[j].m_map);
14795255736Sdavidch                    bus_dmamap_destroy(fp->tx_mbuf_tag,
14796255736Sdavidch                                       fp->tx_mbuf_chain[j].m_map);
14797255736Sdavidch                }
14798255736Sdavidch            }
14799255736Sdavidch
14800255736Sdavidch            bus_dma_tag_destroy(fp->tx_mbuf_tag);
14801255736Sdavidch            fp->tx_mbuf_tag = NULL;
14802255736Sdavidch        }
14803255736Sdavidch
14804255736Sdavidch        /***********************/
14805255736Sdavidch        /* FP RX MBUF DMA MAPS */
14806255736Sdavidch        /***********************/
14807255736Sdavidch
14808255736Sdavidch        if (fp->rx_mbuf_tag != NULL) {
14809255736Sdavidch            for (j = 0; j < RX_BD_TOTAL; j++) {
14810255736Sdavidch                if (fp->rx_mbuf_chain[j].m_map != NULL) {
14811255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
14812255736Sdavidch                                      fp->rx_mbuf_chain[j].m_map);
14813255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
14814255736Sdavidch                                       fp->rx_mbuf_chain[j].m_map);
14815255736Sdavidch                }
14816255736Sdavidch            }
14817255736Sdavidch
14818255736Sdavidch            if (fp->rx_mbuf_spare_map != NULL) {
14819255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14820255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14821255736Sdavidch            }
14822255736Sdavidch
14823255736Sdavidch            /***************************/
14824255736Sdavidch            /* FP RX TPA MBUF DMA MAPS */
14825255736Sdavidch            /***************************/
14826255736Sdavidch
14827255736Sdavidch            max_agg_queues = MAX_AGG_QS(sc);
14828255736Sdavidch
14829255736Sdavidch            for (j = 0; j < max_agg_queues; j++) {
14830255736Sdavidch                if (fp->rx_tpa_info[j].bd.m_map != NULL) {
14831255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
14832255736Sdavidch                                      fp->rx_tpa_info[j].bd.m_map);
14833255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
14834255736Sdavidch                                       fp->rx_tpa_info[j].bd.m_map);
14835255736Sdavidch                }
14836255736Sdavidch            }
14837255736Sdavidch
14838255736Sdavidch            if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
14839255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag,
14840255736Sdavidch                                  fp->rx_tpa_info_mbuf_spare_map);
14841255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag,
14842255736Sdavidch                                   fp->rx_tpa_info_mbuf_spare_map);
14843255736Sdavidch            }
14844255736Sdavidch
14845255736Sdavidch            bus_dma_tag_destroy(fp->rx_mbuf_tag);
14846255736Sdavidch            fp->rx_mbuf_tag = NULL;
14847255736Sdavidch        }
14848255736Sdavidch
14849255736Sdavidch        /***************************/
14850255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
14851255736Sdavidch        /***************************/
14852255736Sdavidch
14853255736Sdavidch        if (fp->rx_sge_mbuf_tag != NULL) {
14854255736Sdavidch            for (j = 0; j < RX_SGE_TOTAL; j++) {
14855255736Sdavidch                if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
14856255736Sdavidch                    bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14857255736Sdavidch                                      fp->rx_sge_mbuf_chain[j].m_map);
14858255736Sdavidch                    bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14859255736Sdavidch                                       fp->rx_sge_mbuf_chain[j].m_map);
14860255736Sdavidch                }
14861255736Sdavidch            }
14862255736Sdavidch
14863255736Sdavidch            if (fp->rx_sge_mbuf_spare_map != NULL) {
14864255736Sdavidch                bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14865255736Sdavidch                                  fp->rx_sge_mbuf_spare_map);
14866255736Sdavidch                bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14867255736Sdavidch                                   fp->rx_sge_mbuf_spare_map);
14868255736Sdavidch            }
14869255736Sdavidch
14870255736Sdavidch            bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
14871255736Sdavidch            fp->rx_sge_mbuf_tag = NULL;
14872255736Sdavidch        }
14873255736Sdavidch    }
14874255736Sdavidch
14875255736Sdavidch    /***************************/
14876255736Sdavidch    /* FW DECOMPRESSION BUFFER */
14877255736Sdavidch    /***************************/
14878255736Sdavidch
14879255736Sdavidch    bxe_dma_free(sc, &sc->gz_buf_dma);
14880255736Sdavidch    sc->gz_buf = NULL;
14881255736Sdavidch    free(sc->gz_strm, M_DEVBUF);
14882255736Sdavidch    sc->gz_strm = NULL;
14883255736Sdavidch
14884255736Sdavidch    /*******************/
14885255736Sdavidch    /* SLOW PATH QUEUE */
14886255736Sdavidch    /*******************/
14887255736Sdavidch
14888255736Sdavidch    bxe_dma_free(sc, &sc->spq_dma);
14889255736Sdavidch    sc->spq = NULL;
14890255736Sdavidch
14891255736Sdavidch    /*************/
14892255736Sdavidch    /* SLOW PATH */
14893255736Sdavidch    /*************/
14894255736Sdavidch
14895255736Sdavidch    bxe_dma_free(sc, &sc->sp_dma);
14896255736Sdavidch    sc->sp = NULL;
14897255736Sdavidch
14898255736Sdavidch    /***************/
14899255736Sdavidch    /* EVENT QUEUE */
14900255736Sdavidch    /***************/
14901255736Sdavidch
14902255736Sdavidch    bxe_dma_free(sc, &sc->eq_dma);
14903255736Sdavidch    sc->eq = NULL;
14904255736Sdavidch
14905255736Sdavidch    /************************/
14906255736Sdavidch    /* DEFAULT STATUS BLOCK */
14907255736Sdavidch    /************************/
14908255736Sdavidch
14909255736Sdavidch    bxe_dma_free(sc, &sc->def_sb_dma);
14910255736Sdavidch    sc->def_sb = NULL;
14911255736Sdavidch
14912255736Sdavidch    bus_dma_tag_destroy(sc->parent_dma_tag);
14913255736Sdavidch    sc->parent_dma_tag = NULL;
14914255736Sdavidch}
14915255736Sdavidch
14916255736Sdavidch/*
14917255736Sdavidch * Previous driver DMAE transaction may have occurred when pre-boot stage
14918255736Sdavidch * ended and boot began. This would invalidate the addresses of the
14919255736Sdavidch * transaction, resulting in was-error bit set in the PCI causing all
14920255736Sdavidch * hw-to-host PCIe transactions to timeout. If this happened we want to clear
14921255736Sdavidch * the interrupt which detected this from the pglueb and the was-done bit
14922255736Sdavidch */
14923255736Sdavidchstatic void
14924255736Sdavidchbxe_prev_interrupted_dmae(struct bxe_softc *sc)
14925255736Sdavidch{
14926255736Sdavidch    uint32_t val;
14927255736Sdavidch
14928255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
14929255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
14930255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
14931255736Sdavidch            BLOGD(sc, DBG_LOAD,
14932255736Sdavidch                  "Clearing 'was-error' bit that was set in pglueb");
14933255736Sdavidch            REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
14934255736Sdavidch        }
14935255736Sdavidch    }
14936255736Sdavidch}
14937255736Sdavidch
14938255736Sdavidchstatic int
14939255736Sdavidchbxe_prev_mcp_done(struct bxe_softc *sc)
14940255736Sdavidch{
14941255736Sdavidch    uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
14942255736Sdavidch                                 DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
14943255736Sdavidch    if (!rc) {
14944255736Sdavidch        BLOGE(sc, "MCP response failure, aborting\n");
14945255736Sdavidch        return (-1);
14946255736Sdavidch    }
14947255736Sdavidch
14948255736Sdavidch    return (0);
14949255736Sdavidch}
14950255736Sdavidch
14951255736Sdavidchstatic struct bxe_prev_list_node *
14952255736Sdavidchbxe_prev_path_get_entry(struct bxe_softc *sc)
14953255736Sdavidch{
14954255736Sdavidch    struct bxe_prev_list_node *tmp;
14955255736Sdavidch
14956255736Sdavidch    LIST_FOREACH(tmp, &bxe_prev_list, node) {
14957255736Sdavidch        if ((sc->pcie_bus == tmp->bus) &&
14958255736Sdavidch            (sc->pcie_device == tmp->slot) &&
14959255736Sdavidch            (SC_PATH(sc) == tmp->path)) {
14960255736Sdavidch            return (tmp);
14961255736Sdavidch        }
14962255736Sdavidch    }
14963255736Sdavidch
14964255736Sdavidch    return (NULL);
14965255736Sdavidch}
14966255736Sdavidch
14967255736Sdavidchstatic uint8_t
14968255736Sdavidchbxe_prev_is_path_marked(struct bxe_softc *sc)
14969255736Sdavidch{
14970255736Sdavidch    struct bxe_prev_list_node *tmp;
14971255736Sdavidch    int rc = FALSE;
14972255736Sdavidch
14973255736Sdavidch    mtx_lock(&bxe_prev_mtx);
14974255736Sdavidch
14975255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
14976255736Sdavidch    if (tmp) {
14977255736Sdavidch        if (tmp->aer) {
14978255736Sdavidch            BLOGD(sc, DBG_LOAD,
14979255736Sdavidch                  "Path %d/%d/%d was marked by AER\n",
14980255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
14981255736Sdavidch        } else {
14982255736Sdavidch            rc = TRUE;
14983255736Sdavidch            BLOGD(sc, DBG_LOAD,
14984255736Sdavidch                  "Path %d/%d/%d was already cleaned from previous drivers\n",
14985255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
14986255736Sdavidch        }
14987255736Sdavidch    }
14988255736Sdavidch
14989255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
14990255736Sdavidch
14991255736Sdavidch    return (rc);
14992255736Sdavidch}
14993255736Sdavidch
14994255736Sdavidchstatic int
14995255736Sdavidchbxe_prev_mark_path(struct bxe_softc *sc,
14996255736Sdavidch                   uint8_t          after_undi)
14997255736Sdavidch{
14998255736Sdavidch    struct bxe_prev_list_node *tmp;
14999255736Sdavidch
15000255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15001255736Sdavidch
15002255736Sdavidch    /* Check whether the entry for this path already exists */
15003255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15004255736Sdavidch    if (tmp) {
15005255736Sdavidch        if (!tmp->aer) {
15006255736Sdavidch            BLOGD(sc, DBG_LOAD,
15007255736Sdavidch                  "Re-marking AER in path %d/%d/%d\n",
15008255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15009255736Sdavidch        } else {
15010255736Sdavidch            BLOGD(sc, DBG_LOAD,
15011255736Sdavidch                  "Removing AER indication from path %d/%d/%d\n",
15012255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15013255736Sdavidch            tmp->aer = 0;
15014255736Sdavidch        }
15015255736Sdavidch
15016255736Sdavidch        mtx_unlock(&bxe_prev_mtx);
15017255736Sdavidch        return (0);
15018255736Sdavidch    }
15019255736Sdavidch
15020255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15021255736Sdavidch
15022255736Sdavidch    /* Create an entry for this path and add it */
15023255736Sdavidch    tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15024255736Sdavidch                 (M_NOWAIT | M_ZERO));
15025255736Sdavidch    if (!tmp) {
15026255736Sdavidch        BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15027255736Sdavidch        return (-1);
15028255736Sdavidch    }
15029255736Sdavidch
15030255736Sdavidch    tmp->bus  = sc->pcie_bus;
15031255736Sdavidch    tmp->slot = sc->pcie_device;
15032255736Sdavidch    tmp->path = SC_PATH(sc);
15033255736Sdavidch    tmp->aer  = 0;
15034255736Sdavidch    tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15035255736Sdavidch
15036255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15037255736Sdavidch
15038255736Sdavidch    BLOGD(sc, DBG_LOAD,
15039255736Sdavidch          "Marked path %d/%d/%d - finished previous unload\n",
15040255736Sdavidch          sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15041255736Sdavidch    LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15042255736Sdavidch
15043255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15044255736Sdavidch
15045255736Sdavidch    return (0);
15046255736Sdavidch}
15047255736Sdavidch
15048255736Sdavidchstatic int
15049255736Sdavidchbxe_do_flr(struct bxe_softc *sc)
15050255736Sdavidch{
15051255736Sdavidch    int i;
15052255736Sdavidch
15053255736Sdavidch    /* only E2 and onwards support FLR */
15054255736Sdavidch    if (CHIP_IS_E1x(sc)) {
15055255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15056255736Sdavidch        return (-1);
15057255736Sdavidch    }
15058255736Sdavidch
15059255736Sdavidch    /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15060255736Sdavidch    if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15061255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15062255736Sdavidch              sc->devinfo.bc_ver);
15063255736Sdavidch        return (-1);
15064255736Sdavidch    }
15065255736Sdavidch
15066255736Sdavidch    /* Wait for Transaction Pending bit clean */
15067255736Sdavidch    for (i = 0; i < 4; i++) {
15068255736Sdavidch        if (i) {
15069255736Sdavidch            DELAY(((1 << (i - 1)) * 100) * 1000);
15070255736Sdavidch        }
15071255736Sdavidch
15072255736Sdavidch        if (!bxe_is_pcie_pending(sc)) {
15073255736Sdavidch            goto clear;
15074255736Sdavidch        }
15075255736Sdavidch    }
15076255736Sdavidch
15077255736Sdavidch    BLOGE(sc, "PCIE transaction is not cleared, "
15078255736Sdavidch              "proceeding with reset anyway\n");
15079255736Sdavidch
15080255736Sdavidchclear:
15081255736Sdavidch
15082255736Sdavidch    BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15083255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15084255736Sdavidch
15085255736Sdavidch    return (0);
15086255736Sdavidch}
15087255736Sdavidch
15088255736Sdavidchstruct bxe_mac_vals {
15089255736Sdavidch    uint32_t xmac_addr;
15090255736Sdavidch    uint32_t xmac_val;
15091255736Sdavidch    uint32_t emac_addr;
15092255736Sdavidch    uint32_t emac_val;
15093255736Sdavidch    uint32_t umac_addr;
15094255736Sdavidch    uint32_t umac_val;
15095255736Sdavidch    uint32_t bmac_addr;
15096255736Sdavidch    uint32_t bmac_val[2];
15097255736Sdavidch};
15098255736Sdavidch
15099255736Sdavidchstatic void
15100255736Sdavidchbxe_prev_unload_close_mac(struct bxe_softc *sc,
15101255736Sdavidch                          struct bxe_mac_vals *vals)
15102255736Sdavidch{
15103255736Sdavidch    uint32_t val, base_addr, offset, mask, reset_reg;
15104255736Sdavidch    uint8_t mac_stopped = FALSE;
15105255736Sdavidch    uint8_t port = SC_PORT(sc);
15106255736Sdavidch    uint32_t wb_data[2];
15107255736Sdavidch
15108255736Sdavidch    /* reset addresses as they also mark which values were changed */
15109255736Sdavidch    vals->bmac_addr = 0;
15110255736Sdavidch    vals->umac_addr = 0;
15111255736Sdavidch    vals->xmac_addr = 0;
15112255736Sdavidch    vals->emac_addr = 0;
15113255736Sdavidch
15114255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15115255736Sdavidch
15116255736Sdavidch    if (!CHIP_IS_E3(sc)) {
15117255736Sdavidch        val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15118255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15119255736Sdavidch        if ((mask & reset_reg) && val) {
15120255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15121255736Sdavidch            base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15122255736Sdavidch                                    : NIG_REG_INGRESS_BMAC0_MEM;
15123255736Sdavidch            offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15124255736Sdavidch                                    : BIGMAC_REGISTER_BMAC_CONTROL;
15125255736Sdavidch
15126255736Sdavidch            /*
15127255736Sdavidch             * use rd/wr since we cannot use dmae. This is safe
15128255736Sdavidch             * since MCP won't access the bus due to the request
15129255736Sdavidch             * to unload, and no function on the path can be
15130255736Sdavidch             * loaded at this time.
15131255736Sdavidch             */
15132255736Sdavidch            wb_data[0] = REG_RD(sc, base_addr + offset);
15133255736Sdavidch            wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15134255736Sdavidch            vals->bmac_addr = base_addr + offset;
15135255736Sdavidch            vals->bmac_val[0] = wb_data[0];
15136255736Sdavidch            vals->bmac_val[1] = wb_data[1];
15137255736Sdavidch            wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15138255736Sdavidch            REG_WR(sc, vals->bmac_addr, wb_data[0]);
15139255736Sdavidch            REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15140255736Sdavidch        }
15141255736Sdavidch
15142255736Sdavidch        BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15143255736Sdavidch        vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15144255736Sdavidch        vals->emac_val = REG_RD(sc, vals->emac_addr);
15145255736Sdavidch        REG_WR(sc, vals->emac_addr, 0);
15146255736Sdavidch        mac_stopped = TRUE;
15147255736Sdavidch    } else {
15148255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15149255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15150255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15151255736Sdavidch            val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15152255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15153255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15154255736Sdavidch            vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15155255736Sdavidch            vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15156255736Sdavidch            REG_WR(sc, vals->xmac_addr, 0);
15157255736Sdavidch            mac_stopped = TRUE;
15158255736Sdavidch        }
15159255736Sdavidch
15160255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15161255736Sdavidch        if (mask & reset_reg) {
15162255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15163255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15164255736Sdavidch            vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15165255736Sdavidch            vals->umac_val = REG_RD(sc, vals->umac_addr);
15166255736Sdavidch            REG_WR(sc, vals->umac_addr, 0);
15167255736Sdavidch            mac_stopped = TRUE;
15168255736Sdavidch        }
15169255736Sdavidch    }
15170255736Sdavidch
15171255736Sdavidch    if (mac_stopped) {
15172255736Sdavidch        DELAY(20000);
15173255736Sdavidch    }
15174255736Sdavidch}
15175255736Sdavidch
15176255736Sdavidch#define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15177255736Sdavidch#define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15178255736Sdavidch#define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15179255736Sdavidch#define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15180255736Sdavidch
15181255736Sdavidchstatic void
15182255736Sdavidchbxe_prev_unload_undi_inc(struct bxe_softc *sc,
15183255736Sdavidch                         uint8_t          port,
15184255736Sdavidch                         uint8_t          inc)
15185255736Sdavidch{
15186255736Sdavidch    uint16_t rcq, bd;
15187255736Sdavidch    uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15188255736Sdavidch
15189255736Sdavidch    rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15190255736Sdavidch    bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15191255736Sdavidch
15192255736Sdavidch    tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15193255736Sdavidch    REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15194255736Sdavidch
15195255736Sdavidch    BLOGD(sc, DBG_LOAD,
15196255736Sdavidch          "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15197255736Sdavidch          port, bd, rcq);
15198255736Sdavidch}
15199255736Sdavidch
15200255736Sdavidchstatic int
15201255736Sdavidchbxe_prev_unload_common(struct bxe_softc *sc)
15202255736Sdavidch{
15203255736Sdavidch    uint32_t reset_reg, tmp_reg = 0, rc;
15204255736Sdavidch    uint8_t prev_undi = FALSE;
15205255736Sdavidch    struct bxe_mac_vals mac_vals;
15206255736Sdavidch    uint32_t timer_count = 1000;
15207255736Sdavidch    uint32_t prev_brb;
15208255736Sdavidch
15209255736Sdavidch    /*
15210255736Sdavidch     * It is possible a previous function received 'common' answer,
15211255736Sdavidch     * but hasn't loaded yet, therefore creating a scenario of
15212255736Sdavidch     * multiple functions receiving 'common' on the same path.
15213255736Sdavidch     */
15214255736Sdavidch    BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15215255736Sdavidch
15216255736Sdavidch    memset(&mac_vals, 0, sizeof(mac_vals));
15217255736Sdavidch
15218255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15219255736Sdavidch        return (bxe_prev_mcp_done(sc));
15220255736Sdavidch    }
15221255736Sdavidch
15222255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15223255736Sdavidch
15224255736Sdavidch    /* Reset should be performed after BRB is emptied */
15225255736Sdavidch    if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15226255736Sdavidch        /* Close the MAC Rx to prevent BRB from filling up */
15227255736Sdavidch        bxe_prev_unload_close_mac(sc, &mac_vals);
15228255736Sdavidch
15229255736Sdavidch        /* close LLH filters towards the BRB */
15230255736Sdavidch        elink_set_rx_filter(&sc->link_params, 0);
15231255736Sdavidch
15232255736Sdavidch        /*
15233255736Sdavidch         * Check if the UNDI driver was previously loaded.
15234255736Sdavidch         * UNDI driver initializes CID offset for normal bell to 0x7
15235255736Sdavidch         */
15236255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15237255736Sdavidch            tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15238255736Sdavidch            if (tmp_reg == 0x7) {
15239255736Sdavidch                BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15240255736Sdavidch                prev_undi = TRUE;
15241255736Sdavidch                /* clear the UNDI indication */
15242255736Sdavidch                REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15243255736Sdavidch                /* clear possible idle check errors */
15244255736Sdavidch                REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15245255736Sdavidch            }
15246255736Sdavidch        }
15247255736Sdavidch
15248255736Sdavidch        /* wait until BRB is empty */
15249255736Sdavidch        tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15250255736Sdavidch        while (timer_count) {
15251255736Sdavidch            prev_brb = tmp_reg;
15252255736Sdavidch
15253255736Sdavidch            tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15254255736Sdavidch            if (!tmp_reg) {
15255255736Sdavidch                break;
15256255736Sdavidch            }
15257255736Sdavidch
15258255736Sdavidch            BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15259255736Sdavidch
15260255736Sdavidch            /* reset timer as long as BRB actually gets emptied */
15261255736Sdavidch            if (prev_brb > tmp_reg) {
15262255736Sdavidch                timer_count = 1000;
15263255736Sdavidch            } else {
15264255736Sdavidch                timer_count--;
15265255736Sdavidch            }
15266255736Sdavidch
15267255736Sdavidch            /* If UNDI resides in memory, manually increment it */
15268255736Sdavidch            if (prev_undi) {
15269255736Sdavidch                bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15270255736Sdavidch            }
15271255736Sdavidch
15272255736Sdavidch            DELAY(10);
15273255736Sdavidch        }
15274255736Sdavidch
15275255736Sdavidch        if (!timer_count) {
15276255736Sdavidch            BLOGE(sc, "Failed to empty BRB\n");
15277255736Sdavidch        }
15278255736Sdavidch    }
15279255736Sdavidch
15280255736Sdavidch    /* No packets are in the pipeline, path is ready for reset */
15281255736Sdavidch    bxe_reset_common(sc);
15282255736Sdavidch
15283255736Sdavidch    if (mac_vals.xmac_addr) {
15284255736Sdavidch        REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15285255736Sdavidch    }
15286255736Sdavidch    if (mac_vals.umac_addr) {
15287255736Sdavidch        REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15288255736Sdavidch    }
15289255736Sdavidch    if (mac_vals.emac_addr) {
15290255736Sdavidch        REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15291255736Sdavidch    }
15292255736Sdavidch    if (mac_vals.bmac_addr) {
15293255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15294255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15295255736Sdavidch    }
15296255736Sdavidch
15297255736Sdavidch    rc = bxe_prev_mark_path(sc, prev_undi);
15298255736Sdavidch    if (rc) {
15299255736Sdavidch        bxe_prev_mcp_done(sc);
15300255736Sdavidch        return (rc);
15301255736Sdavidch    }
15302255736Sdavidch
15303255736Sdavidch    return (bxe_prev_mcp_done(sc));
15304255736Sdavidch}
15305255736Sdavidch
15306255736Sdavidchstatic int
15307255736Sdavidchbxe_prev_unload_uncommon(struct bxe_softc *sc)
15308255736Sdavidch{
15309255736Sdavidch    int rc;
15310255736Sdavidch
15311255736Sdavidch    BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15312255736Sdavidch
15313255736Sdavidch    /* Test if previous unload process was already finished for this path */
15314255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15315255736Sdavidch        return (bxe_prev_mcp_done(sc));
15316255736Sdavidch    }
15317255736Sdavidch
15318255736Sdavidch    BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15319255736Sdavidch
15320255736Sdavidch    /*
15321255736Sdavidch     * If function has FLR capabilities, and existing FW version matches
15322255736Sdavidch     * the one required, then FLR will be sufficient to clean any residue
15323255736Sdavidch     * left by previous driver
15324255736Sdavidch     */
15325255736Sdavidch    rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15326255736Sdavidch    if (!rc) {
15327255736Sdavidch        /* fw version is good */
15328255736Sdavidch        BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15329255736Sdavidch        rc = bxe_do_flr(sc);
15330255736Sdavidch    }
15331255736Sdavidch
15332255736Sdavidch    if (!rc) {
15333255736Sdavidch        /* FLR was performed */
15334255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR successful\n");
15335255736Sdavidch        return (0);
15336255736Sdavidch    }
15337255736Sdavidch
15338255736Sdavidch    BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15339255736Sdavidch
15340255736Sdavidch    /* Close the MCP request, return failure*/
15341255736Sdavidch    rc = bxe_prev_mcp_done(sc);
15342255736Sdavidch    if (!rc) {
15343255736Sdavidch        rc = BXE_PREV_WAIT_NEEDED;
15344255736Sdavidch    }
15345255736Sdavidch
15346255736Sdavidch    return (rc);
15347255736Sdavidch}
15348255736Sdavidch
15349255736Sdavidchstatic int
15350255736Sdavidchbxe_prev_unload(struct bxe_softc *sc)
15351255736Sdavidch{
15352255736Sdavidch    int time_counter = 10;
15353255736Sdavidch    uint32_t fw, hw_lock_reg, hw_lock_val;
15354255736Sdavidch    uint32_t rc = 0;
15355255736Sdavidch
15356255736Sdavidch    /*
15357255736Sdavidch     * Clear HW from errors which may have resulted from an interrupted
15358255736Sdavidch     * DMAE transaction.
15359255736Sdavidch     */
15360255736Sdavidch    bxe_prev_interrupted_dmae(sc);
15361255736Sdavidch
15362255736Sdavidch    /* Release previously held locks */
15363255736Sdavidch    hw_lock_reg =
15364255736Sdavidch        (SC_FUNC(sc) <= 5) ?
15365255736Sdavidch            (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15366255736Sdavidch            (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15367255736Sdavidch
15368255736Sdavidch    hw_lock_val = (REG_RD(sc, hw_lock_reg));
15369255736Sdavidch    if (hw_lock_val) {
15370255736Sdavidch        if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15371255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15372255736Sdavidch            REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15373255736Sdavidch                   (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15374255736Sdavidch        }
15375255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15376255736Sdavidch        REG_WR(sc, hw_lock_reg, 0xffffffff);
15377255736Sdavidch    } else {
15378255736Sdavidch        BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15379255736Sdavidch    }
15380255736Sdavidch
15381255736Sdavidch    if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15382255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15383255736Sdavidch        REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15384255736Sdavidch    }
15385255736Sdavidch
15386255736Sdavidch    do {
15387255736Sdavidch        /* Lock MCP using an unload request */
15388255736Sdavidch        fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15389255736Sdavidch        if (!fw) {
15390255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
15391255736Sdavidch            rc = -1;
15392255736Sdavidch            break;
15393255736Sdavidch        }
15394255736Sdavidch
15395255736Sdavidch        if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15396255736Sdavidch            rc = bxe_prev_unload_common(sc);
15397255736Sdavidch            break;
15398255736Sdavidch        }
15399255736Sdavidch
15400255736Sdavidch        /* non-common reply from MCP night require looping */
15401255736Sdavidch        rc = bxe_prev_unload_uncommon(sc);
15402255736Sdavidch        if (rc != BXE_PREV_WAIT_NEEDED) {
15403255736Sdavidch            break;
15404255736Sdavidch        }
15405255736Sdavidch
15406255736Sdavidch        DELAY(20000);
15407255736Sdavidch    } while (--time_counter);
15408255736Sdavidch
15409255736Sdavidch    if (!time_counter || rc) {
15410295830Sdavidcs        BLOGE(sc, "Failed to unload previous driver!"
15411295830Sdavidcs            " time_counter %d rc %d\n", time_counter, rc);
15412255736Sdavidch        rc = -1;
15413255736Sdavidch    }
15414255736Sdavidch
15415255736Sdavidch    return (rc);
15416255736Sdavidch}
15417255736Sdavidch
15418255736Sdavidchvoid
15419255736Sdavidchbxe_dcbx_set_state(struct bxe_softc *sc,
15420255736Sdavidch                   uint8_t          dcb_on,
15421255736Sdavidch                   uint32_t         dcbx_enabled)
15422255736Sdavidch{
15423255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
15424255736Sdavidch        sc->dcb_state = dcb_on;
15425255736Sdavidch        sc->dcbx_enabled = dcbx_enabled;
15426255736Sdavidch    } else {
15427255736Sdavidch        sc->dcb_state = FALSE;
15428255736Sdavidch        sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15429255736Sdavidch    }
15430255736Sdavidch    BLOGD(sc, DBG_LOAD,
15431255736Sdavidch          "DCB state [%s:%s]\n",
15432255736Sdavidch          dcb_on ? "ON" : "OFF",
15433255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15434255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15435255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15436255736Sdavidch          "on-chip with negotiation" : "invalid");
15437255736Sdavidch}
15438255736Sdavidch
15439255736Sdavidch/* must be called after sriov-enable */
15440255736Sdavidchstatic int
15441255736Sdavidchbxe_set_qm_cid_count(struct bxe_softc *sc)
15442255736Sdavidch{
15443255736Sdavidch    int cid_count = BXE_L2_MAX_CID(sc);
15444255736Sdavidch
15445255736Sdavidch    if (IS_SRIOV(sc)) {
15446255736Sdavidch        cid_count += BXE_VF_CIDS;
15447255736Sdavidch    }
15448255736Sdavidch
15449255736Sdavidch    if (CNIC_SUPPORT(sc)) {
15450255736Sdavidch        cid_count += CNIC_CID_MAX;
15451255736Sdavidch    }
15452255736Sdavidch
15453255736Sdavidch    return (roundup(cid_count, QM_CID_ROUND));
15454255736Sdavidch}
15455255736Sdavidch
15456255736Sdavidchstatic void
15457255736Sdavidchbxe_init_multi_cos(struct bxe_softc *sc)
15458255736Sdavidch{
15459255736Sdavidch    int pri, cos;
15460255736Sdavidch
15461255736Sdavidch    uint32_t pri_map = 0; /* XXX change to user config */
15462255736Sdavidch
15463255736Sdavidch    for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15464255736Sdavidch        cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15465255736Sdavidch        if (cos < sc->max_cos) {
15466255736Sdavidch            sc->prio_to_cos[pri] = cos;
15467255736Sdavidch        } else {
15468255736Sdavidch            BLOGW(sc, "Invalid COS %d for priority %d "
15469255736Sdavidch                      "(max COS is %d), setting to 0\n",
15470255736Sdavidch                  cos, pri, (sc->max_cos - 1));
15471255736Sdavidch            sc->prio_to_cos[pri] = 0;
15472255736Sdavidch        }
15473255736Sdavidch    }
15474255736Sdavidch}
15475255736Sdavidch
15476255736Sdavidchstatic int
15477255736Sdavidchbxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15478255736Sdavidch{
15479255736Sdavidch    struct bxe_softc *sc;
15480255736Sdavidch    int error, result;
15481255736Sdavidch
15482255736Sdavidch    result = 0;
15483255736Sdavidch    error = sysctl_handle_int(oidp, &result, 0, req);
15484255736Sdavidch
15485255736Sdavidch    if (error || !req->newptr) {
15486255736Sdavidch        return (error);
15487255736Sdavidch    }
15488255736Sdavidch
15489255736Sdavidch    if (result == 1) {
15490292638Sdavidcs        uint32_t  temp;
15491255736Sdavidch        sc = (struct bxe_softc *)arg1;
15492289199Sdavidcs
15493255736Sdavidch        BLOGI(sc, "... dumping driver state ...\n");
15494292638Sdavidcs        temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15495292638Sdavidcs        BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15496255736Sdavidch    }
15497255736Sdavidch
15498255736Sdavidch    return (error);
15499255736Sdavidch}
15500255736Sdavidch
15501255736Sdavidchstatic int
15502255736Sdavidchbxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15503255736Sdavidch{
15504255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15505255736Sdavidch    uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15506255736Sdavidch    uint32_t *offset;
15507255736Sdavidch    uint64_t value = 0;
15508255736Sdavidch    int index = (int)arg2;
15509255736Sdavidch
15510255736Sdavidch    if (index >= BXE_NUM_ETH_STATS) {
15511255736Sdavidch        BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15512255736Sdavidch        return (-1);
15513255736Sdavidch    }
15514255736Sdavidch
15515255736Sdavidch    offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15516255736Sdavidch
15517255736Sdavidch    switch (bxe_eth_stats_arr[index].size) {
15518255736Sdavidch    case 4:
15519255736Sdavidch        value = (uint64_t)*offset;
15520255736Sdavidch        break;
15521255736Sdavidch    case 8:
15522255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15523255736Sdavidch        break;
15524255736Sdavidch    default:
15525255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15526255736Sdavidch              index, bxe_eth_stats_arr[index].size);
15527255736Sdavidch        return (-1);
15528255736Sdavidch    }
15529255736Sdavidch
15530255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15531255736Sdavidch}
15532255736Sdavidch
15533255736Sdavidchstatic int
15534255736Sdavidchbxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15535255736Sdavidch{
15536255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15537255736Sdavidch    uint32_t *eth_stats;
15538255736Sdavidch    uint32_t *offset;
15539255736Sdavidch    uint64_t value = 0;
15540255736Sdavidch    uint32_t q_stat = (uint32_t)arg2;
15541255736Sdavidch    uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15542255736Sdavidch    uint32_t index = (q_stat & 0xffff);
15543255736Sdavidch
15544255736Sdavidch    eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15545255736Sdavidch
15546255736Sdavidch    if (index >= BXE_NUM_ETH_Q_STATS) {
15547255736Sdavidch        BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15548255736Sdavidch        return (-1);
15549255736Sdavidch    }
15550255736Sdavidch
15551255736Sdavidch    offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15552255736Sdavidch
15553255736Sdavidch    switch (bxe_eth_q_stats_arr[index].size) {
15554255736Sdavidch    case 4:
15555255736Sdavidch        value = (uint64_t)*offset;
15556255736Sdavidch        break;
15557255736Sdavidch    case 8:
15558255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15559255736Sdavidch        break;
15560255736Sdavidch    default:
15561255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15562255736Sdavidch              index, bxe_eth_q_stats_arr[index].size);
15563255736Sdavidch        return (-1);
15564255736Sdavidch    }
15565255736Sdavidch
15566255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15567255736Sdavidch}
15568255736Sdavidch
15569255736Sdavidchstatic void
15570255736Sdavidchbxe_add_sysctls(struct bxe_softc *sc)
15571255736Sdavidch{
15572255736Sdavidch    struct sysctl_ctx_list *ctx;
15573255736Sdavidch    struct sysctl_oid_list *children;
15574255736Sdavidch    struct sysctl_oid *queue_top, *queue;
15575255736Sdavidch    struct sysctl_oid_list *queue_top_children, *queue_children;
15576255736Sdavidch    char queue_num_buf[32];
15577255736Sdavidch    uint32_t q_stat;
15578255736Sdavidch    int i, j;
15579255736Sdavidch
15580255736Sdavidch    ctx = device_get_sysctl_ctx(sc->dev);
15581255736Sdavidch    children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15582255736Sdavidch
15583255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
15584255736Sdavidch                      CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
15585255736Sdavidch                      "version");
15586255736Sdavidch
15587255736Sdavidch    snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
15588255736Sdavidch             BCM_5710_FW_MAJOR_VERSION,
15589255736Sdavidch             BCM_5710_FW_MINOR_VERSION,
15590255736Sdavidch             BCM_5710_FW_REVISION_VERSION,
15591255736Sdavidch             BCM_5710_FW_ENGINEERING_VERSION);
15592255736Sdavidch
15593255736Sdavidch    snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
15594255736Sdavidch        ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
15595255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
15596255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
15597255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
15598255736Sdavidch                                                                "Unknown"));
15599255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
15600255736Sdavidch                    CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
15601255736Sdavidch                    "multifunction vnics per port");
15602255736Sdavidch
15603255736Sdavidch    snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
15604255736Sdavidch        ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
15605255736Sdavidch         (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
15606255736Sdavidch         (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
15607255736Sdavidch                                              "???GT/s"),
15608255736Sdavidch        sc->devinfo.pcie_link_width);
15609297155Sdavidcs
15610297155Sdavidcs    sc->debug = bxe_debug;
15611297155Sdavidcs
15612297155Sdavidcs#if __FreeBSD_version >= 900000
15613297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15614297155Sdavidcs                      CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
15615297155Sdavidcs                      "bootcode version");
15616297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15617297155Sdavidcs                      CTLFLAG_RD, sc->fw_ver_str, 0,
15618297155Sdavidcs                      "firmware version");
15619297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15620297155Sdavidcs                      CTLFLAG_RD, sc->mf_mode_str, 0,
15621297155Sdavidcs                      "multifunction mode");
15622297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15623297155Sdavidcs                      CTLFLAG_RD, sc->mac_addr_str, 0,
15624297155Sdavidcs                      "mac address");
15625255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15626273377Shselasky                      CTLFLAG_RD, sc->pci_link_str, 0,
15627255736Sdavidch                      "pci link status");
15628273377Shselasky    SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
15629273377Shselasky                    CTLFLAG_RW, &sc->debug,
15630255736Sdavidch                    "debug logging mode");
15631297155Sdavidcs#else
15632297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15633297155Sdavidcs                      CTLFLAG_RD, &sc->devinfo.bc_ver_str, 0,
15634297155Sdavidcs                      "bootcode version");
15635297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15636297155Sdavidcs                      CTLFLAG_RD, &sc->fw_ver_str, 0,
15637297155Sdavidcs                      "firmware version");
15638297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15639297155Sdavidcs                      CTLFLAG_RD, &sc->mf_mode_str, 0,
15640297155Sdavidcs                      "multifunction mode");
15641297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15642297155Sdavidcs                      CTLFLAG_RD, &sc->mac_addr_str, 0,
15643297155Sdavidcs                      "mac address");
15644297155Sdavidcs    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15645297155Sdavidcs                      CTLFLAG_RD, &sc->pci_link_str, 0,
15646297155Sdavidcs                      "pci link status");
15647297155Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "debug",
15648297155Sdavidcs                    CTLFLAG_RW, &sc->debug, 0,
15649297155Sdavidcs                    "debug logging mode");
15650297155Sdavidcs#endif /* #if __FreeBSD_version >= 900000 */
15651255736Sdavidch
15652298294Sdavidcs    sc->trigger_grcdump = 0;
15653298294Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "trigger_grcdump",
15654298294Sdavidcs                   CTLFLAG_RW, &sc->trigger_grcdump, 0,
15655298294Sdavidcs                   "trigger grcdump should be invoked"
15656298294Sdavidcs                   "  before collecting grcdump");
15657292639Sdavidcs
15658298294Sdavidcs    sc->grcdump_started = 0;
15659295823Sdavidcs    sc->grcdump_done = 0;
15660295823Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
15661298294Sdavidcs                   CTLFLAG_RD, &sc->grcdump_done, 0,
15662295823Sdavidcs                   "set by driver when grcdump is done");
15663292639Sdavidcs
15664255736Sdavidch    sc->rx_budget = bxe_rx_budget;
15665255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
15666255736Sdavidch                    CTLFLAG_RW, &sc->rx_budget, 0,
15667255736Sdavidch                    "rx processing budget");
15668255736Sdavidch
15669255736Sdavidch    SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
15670255736Sdavidch                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15671255736Sdavidch                    bxe_sysctl_state, "IU", "dump driver state");
15672255736Sdavidch
15673255736Sdavidch    for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
15674255736Sdavidch        SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
15675255736Sdavidch                        bxe_eth_stats_arr[i].string,
15676255736Sdavidch                        CTLTYPE_U64 | CTLFLAG_RD, sc, i,
15677255736Sdavidch                        bxe_sysctl_eth_stat, "LU",
15678255736Sdavidch                        bxe_eth_stats_arr[i].string);
15679255736Sdavidch    }
15680255736Sdavidch
15681255736Sdavidch    /* add a new parent node for all queues "dev.bxe.#.queue" */
15682255736Sdavidch    queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
15683255736Sdavidch                                CTLFLAG_RD, NULL, "queue");
15684255736Sdavidch    queue_top_children = SYSCTL_CHILDREN(queue_top);
15685255736Sdavidch
15686255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
15687255736Sdavidch        /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
15688255736Sdavidch        snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
15689255736Sdavidch        queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
15690255736Sdavidch                                queue_num_buf, CTLFLAG_RD, NULL,
15691255736Sdavidch                                "single queue");
15692255736Sdavidch        queue_children = SYSCTL_CHILDREN(queue);
15693255736Sdavidch
15694255736Sdavidch        for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
15695255736Sdavidch            q_stat = ((i << 16) | j);
15696255736Sdavidch            SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
15697255736Sdavidch                            bxe_eth_q_stats_arr[j].string,
15698255736Sdavidch                            CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
15699255736Sdavidch                            bxe_sysctl_eth_q_stat, "LU",
15700255736Sdavidch                            bxe_eth_q_stats_arr[j].string);
15701255736Sdavidch        }
15702255736Sdavidch    }
15703255736Sdavidch}
15704255736Sdavidch
15705296579Sdavidcsstatic int
15706296579Sdavidcsbxe_alloc_buf_rings(struct bxe_softc *sc)
15707296579Sdavidcs{
15708296579Sdavidcs#if __FreeBSD_version >= 800000
15709296579Sdavidcs
15710296579Sdavidcs    int i;
15711296579Sdavidcs    struct bxe_fastpath *fp;
15712296579Sdavidcs
15713296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15714296579Sdavidcs
15715296579Sdavidcs        fp = &sc->fp[i];
15716296579Sdavidcs
15717296579Sdavidcs        fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
15718296579Sdavidcs                                   M_NOWAIT, &fp->tx_mtx);
15719296579Sdavidcs        if (fp->tx_br == NULL)
15720296579Sdavidcs            return (-1);
15721296579Sdavidcs    }
15722296579Sdavidcs#endif
15723296579Sdavidcs    return (0);
15724296579Sdavidcs}
15725296579Sdavidcs
15726296579Sdavidcsstatic void
15727296579Sdavidcsbxe_free_buf_rings(struct bxe_softc *sc)
15728296579Sdavidcs{
15729296579Sdavidcs#if __FreeBSD_version >= 800000
15730296579Sdavidcs
15731296579Sdavidcs    int i;
15732296579Sdavidcs    struct bxe_fastpath *fp;
15733296579Sdavidcs
15734296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15735296579Sdavidcs
15736296579Sdavidcs        fp = &sc->fp[i];
15737296579Sdavidcs
15738296579Sdavidcs        if (fp->tx_br) {
15739296579Sdavidcs            buf_ring_free(fp->tx_br, M_DEVBUF);
15740296579Sdavidcs            fp->tx_br = NULL;
15741296579Sdavidcs        }
15742296579Sdavidcs    }
15743296579Sdavidcs
15744296579Sdavidcs#endif
15745296579Sdavidcs}
15746296579Sdavidcs
15747296579Sdavidcsstatic void
15748296579Sdavidcsbxe_init_fp_mutexs(struct bxe_softc *sc)
15749296579Sdavidcs{
15750296579Sdavidcs    int i;
15751296579Sdavidcs    struct bxe_fastpath *fp;
15752296579Sdavidcs
15753296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15754296579Sdavidcs
15755296579Sdavidcs        fp = &sc->fp[i];
15756296579Sdavidcs
15757296579Sdavidcs        snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
15758296579Sdavidcs            "bxe%d_fp%d_tx_lock", sc->unit, i);
15759296579Sdavidcs        mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
15760296579Sdavidcs
15761296579Sdavidcs        snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
15762296579Sdavidcs            "bxe%d_fp%d_rx_lock", sc->unit, i);
15763296579Sdavidcs        mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
15764296579Sdavidcs    }
15765296579Sdavidcs}
15766296579Sdavidcs
15767296579Sdavidcsstatic void
15768296579Sdavidcsbxe_destroy_fp_mutexs(struct bxe_softc *sc)
15769296579Sdavidcs{
15770296579Sdavidcs    int i;
15771296579Sdavidcs    struct bxe_fastpath *fp;
15772296579Sdavidcs
15773296579Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
15774296579Sdavidcs
15775296579Sdavidcs        fp = &sc->fp[i];
15776296579Sdavidcs
15777296579Sdavidcs        if (mtx_initialized(&fp->tx_mtx)) {
15778296579Sdavidcs            mtx_destroy(&fp->tx_mtx);
15779296579Sdavidcs        }
15780296579Sdavidcs
15781296579Sdavidcs        if (mtx_initialized(&fp->rx_mtx)) {
15782296579Sdavidcs            mtx_destroy(&fp->rx_mtx);
15783296579Sdavidcs        }
15784296579Sdavidcs    }
15785296579Sdavidcs}
15786296579Sdavidcs
15787296579Sdavidcs
15788255736Sdavidch/*
15789255736Sdavidch * Device attach function.
15790255736Sdavidch *
15791255736Sdavidch * Allocates device resources, performs secondary chip identification, and
15792255736Sdavidch * initializes driver instance variables. This function is called from driver
15793255736Sdavidch * load after a successful probe.
15794255736Sdavidch *
15795255736Sdavidch * Returns:
15796255736Sdavidch *   0 = Success, >0 = Failure
15797255736Sdavidch */
15798255736Sdavidchstatic int
15799255736Sdavidchbxe_attach(device_t dev)
15800255736Sdavidch{
15801255736Sdavidch    struct bxe_softc *sc;
15802255736Sdavidch
15803255736Sdavidch    sc = device_get_softc(dev);
15804255736Sdavidch
15805255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting attach...\n");
15806255736Sdavidch
15807255736Sdavidch    sc->state = BXE_STATE_CLOSED;
15808255736Sdavidch
15809255736Sdavidch    sc->dev  = dev;
15810255736Sdavidch    sc->unit = device_get_unit(dev);
15811255736Sdavidch
15812255736Sdavidch    BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
15813255736Sdavidch
15814255736Sdavidch    sc->pcie_bus    = pci_get_bus(dev);
15815255736Sdavidch    sc->pcie_device = pci_get_slot(dev);
15816255736Sdavidch    sc->pcie_func   = pci_get_function(dev);
15817255736Sdavidch
15818255736Sdavidch    /* enable bus master capability */
15819255736Sdavidch    pci_enable_busmaster(dev);
15820255736Sdavidch
15821255736Sdavidch    /* get the BARs */
15822255736Sdavidch    if (bxe_allocate_bars(sc) != 0) {
15823255736Sdavidch        return (ENXIO);
15824255736Sdavidch    }
15825255736Sdavidch
15826255736Sdavidch    /* initialize the mutexes */
15827255736Sdavidch    bxe_init_mutexes(sc);
15828255736Sdavidch
15829255736Sdavidch    /* prepare the periodic callout */
15830255736Sdavidch    callout_init(&sc->periodic_callout, 0);
15831255736Sdavidch
15832255736Sdavidch    /* prepare the chip taskqueue */
15833255736Sdavidch    sc->chip_tq_flags = CHIP_TQ_NONE;
15834255736Sdavidch    snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
15835255736Sdavidch             "bxe%d_chip_tq", sc->unit);
15836255736Sdavidch    TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
15837255736Sdavidch    sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
15838255736Sdavidch                                   taskqueue_thread_enqueue,
15839255736Sdavidch                                   &sc->chip_tq);
15840255736Sdavidch    taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
15841255736Sdavidch                            "%s", sc->chip_tq_name);
15842255736Sdavidch
15843255736Sdavidch    /* get device info and set params */
15844255736Sdavidch    if (bxe_get_device_info(sc) != 0) {
15845255736Sdavidch        BLOGE(sc, "getting device info\n");
15846255736Sdavidch        bxe_deallocate_bars(sc);
15847255736Sdavidch        pci_disable_busmaster(dev);
15848255736Sdavidch        return (ENXIO);
15849255736Sdavidch    }
15850255736Sdavidch
15851255736Sdavidch    /* get final misc params */
15852255736Sdavidch    bxe_get_params(sc);
15853255736Sdavidch
15854255736Sdavidch    /* set the default MTU (changed via ifconfig) */
15855255736Sdavidch    sc->mtu = ETHERMTU;
15856255736Sdavidch
15857255736Sdavidch    bxe_set_modes_bitmap(sc);
15858255736Sdavidch
15859255736Sdavidch    /* XXX
15860255736Sdavidch     * If in AFEX mode and the function is configured for FCoE
15861255736Sdavidch     * then bail... no L2 allowed.
15862255736Sdavidch     */
15863255736Sdavidch
15864255736Sdavidch    /* get phy settings from shmem and 'and' against admin settings */
15865255736Sdavidch    bxe_get_phy_info(sc);
15866255736Sdavidch
15867255736Sdavidch    /* initialize the FreeBSD ifnet interface */
15868255736Sdavidch    if (bxe_init_ifnet(sc) != 0) {
15869255736Sdavidch        bxe_release_mutexes(sc);
15870255736Sdavidch        bxe_deallocate_bars(sc);
15871255736Sdavidch        pci_disable_busmaster(dev);
15872255736Sdavidch        return (ENXIO);
15873255736Sdavidch    }
15874255736Sdavidch
15875292639Sdavidcs    if (bxe_add_cdev(sc) != 0) {
15876292639Sdavidcs        if (sc->ifp != NULL) {
15877292639Sdavidcs            ether_ifdetach(sc->ifp);
15878292639Sdavidcs        }
15879292639Sdavidcs        ifmedia_removeall(&sc->ifmedia);
15880292639Sdavidcs        bxe_release_mutexes(sc);
15881292639Sdavidcs        bxe_deallocate_bars(sc);
15882292639Sdavidcs        pci_disable_busmaster(dev);
15883292639Sdavidcs        return (ENXIO);
15884292639Sdavidcs    }
15885292639Sdavidcs
15886255736Sdavidch    /* allocate device interrupts */
15887255736Sdavidch    if (bxe_interrupt_alloc(sc) != 0) {
15888292639Sdavidcs        bxe_del_cdev(sc);
15889266979Smarcel        if (sc->ifp != NULL) {
15890270876Sglebius            ether_ifdetach(sc->ifp);
15891255736Sdavidch        }
15892255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
15893255736Sdavidch        bxe_release_mutexes(sc);
15894255736Sdavidch        bxe_deallocate_bars(sc);
15895255736Sdavidch        pci_disable_busmaster(dev);
15896255736Sdavidch        return (ENXIO);
15897255736Sdavidch    }
15898255736Sdavidch
15899296579Sdavidcs    bxe_init_fp_mutexs(sc);
15900296579Sdavidcs
15901296579Sdavidcs    if (bxe_alloc_buf_rings(sc) != 0) {
15902296579Sdavidcs	bxe_free_buf_rings(sc);
15903296579Sdavidcs        bxe_interrupt_free(sc);
15904296579Sdavidcs        bxe_del_cdev(sc);
15905296579Sdavidcs        if (sc->ifp != NULL) {
15906296579Sdavidcs            ether_ifdetach(sc->ifp);
15907296579Sdavidcs        }
15908296579Sdavidcs        ifmedia_removeall(&sc->ifmedia);
15909296579Sdavidcs        bxe_release_mutexes(sc);
15910296579Sdavidcs        bxe_deallocate_bars(sc);
15911296579Sdavidcs        pci_disable_busmaster(dev);
15912296579Sdavidcs        return (ENXIO);
15913296579Sdavidcs    }
15914296579Sdavidcs
15915255736Sdavidch    /* allocate ilt */
15916255736Sdavidch    if (bxe_alloc_ilt_mem(sc) != 0) {
15917296579Sdavidcs	bxe_free_buf_rings(sc);
15918255736Sdavidch        bxe_interrupt_free(sc);
15919292639Sdavidcs        bxe_del_cdev(sc);
15920266979Smarcel        if (sc->ifp != NULL) {
15921270876Sglebius            ether_ifdetach(sc->ifp);
15922255736Sdavidch        }
15923255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
15924255736Sdavidch        bxe_release_mutexes(sc);
15925255736Sdavidch        bxe_deallocate_bars(sc);
15926255736Sdavidch        pci_disable_busmaster(dev);
15927255736Sdavidch        return (ENXIO);
15928255736Sdavidch    }
15929255736Sdavidch
15930255736Sdavidch    /* allocate the host hardware/software hsi structures */
15931255736Sdavidch    if (bxe_alloc_hsi_mem(sc) != 0) {
15932255736Sdavidch        bxe_free_ilt_mem(sc);
15933296579Sdavidcs	bxe_free_buf_rings(sc);
15934255736Sdavidch        bxe_interrupt_free(sc);
15935292639Sdavidcs        bxe_del_cdev(sc);
15936266979Smarcel        if (sc->ifp != NULL) {
15937270876Sglebius            ether_ifdetach(sc->ifp);
15938255736Sdavidch        }
15939255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
15940255736Sdavidch        bxe_release_mutexes(sc);
15941255736Sdavidch        bxe_deallocate_bars(sc);
15942255736Sdavidch        pci_disable_busmaster(dev);
15943255736Sdavidch        return (ENXIO);
15944255736Sdavidch    }
15945255736Sdavidch
15946255736Sdavidch    /* need to reset chip if UNDI was active */
15947255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
15948255736Sdavidch        /* init fw_seq */
15949255736Sdavidch        sc->fw_seq =
15950255736Sdavidch            (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
15951255736Sdavidch             DRV_MSG_SEQ_NUMBER_MASK);
15952255736Sdavidch        BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
15953255736Sdavidch        bxe_prev_unload(sc);
15954255736Sdavidch    }
15955255736Sdavidch
15956255736Sdavidch#if 1
15957255736Sdavidch    /* XXX */
15958255736Sdavidch    bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
15959255736Sdavidch#else
15960255736Sdavidch    if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
15961255736Sdavidch        SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
15962255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
15963255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
15964255736Sdavidch        bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
15965255736Sdavidch        bxe_dcbx_init_params(sc);
15966255736Sdavidch    } else {
15967255736Sdavidch        bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
15968255736Sdavidch    }
15969255736Sdavidch#endif
15970255736Sdavidch
15971255736Sdavidch    /* calculate qm_cid_count */
15972255736Sdavidch    sc->qm_cid_count = bxe_set_qm_cid_count(sc);
15973255736Sdavidch    BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
15974255736Sdavidch
15975255736Sdavidch    sc->max_cos = 1;
15976255736Sdavidch    bxe_init_multi_cos(sc);
15977255736Sdavidch
15978255736Sdavidch    bxe_add_sysctls(sc);
15979255736Sdavidch
15980255736Sdavidch    return (0);
15981255736Sdavidch}
15982255736Sdavidch
15983255736Sdavidch/*
15984255736Sdavidch * Device detach function.
15985255736Sdavidch *
15986255736Sdavidch * Stops the controller, resets the controller, and releases resources.
15987255736Sdavidch *
15988255736Sdavidch * Returns:
15989255736Sdavidch *   0 = Success, >0 = Failure
15990255736Sdavidch */
15991255736Sdavidchstatic int
15992255736Sdavidchbxe_detach(device_t dev)
15993255736Sdavidch{
15994255736Sdavidch    struct bxe_softc *sc;
15995266979Smarcel    if_t ifp;
15996255736Sdavidch
15997255736Sdavidch    sc = device_get_softc(dev);
15998255736Sdavidch
15999255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16000255736Sdavidch
16001266979Smarcel    ifp = sc->ifp;
16002266979Smarcel    if (ifp != NULL && if_vlantrunkinuse(ifp)) {
16003255736Sdavidch        BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16004255736Sdavidch        return(EBUSY);
16005255736Sdavidch    }
16006255736Sdavidch
16007292639Sdavidcs    bxe_del_cdev(sc);
16008292639Sdavidcs
16009255736Sdavidch    /* stop the periodic callout */
16010255736Sdavidch    bxe_periodic_stop(sc);
16011255736Sdavidch
16012255736Sdavidch    /* stop the chip taskqueue */
16013255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16014255736Sdavidch    if (sc->chip_tq) {
16015255736Sdavidch        taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16016255736Sdavidch        taskqueue_free(sc->chip_tq);
16017255736Sdavidch        sc->chip_tq = NULL;
16018255736Sdavidch    }
16019255736Sdavidch
16020255736Sdavidch    /* stop and reset the controller if it was open */
16021255736Sdavidch    if (sc->state != BXE_STATE_CLOSED) {
16022255736Sdavidch        BXE_CORE_LOCK(sc);
16023255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16024297873Sdavidcs        sc->state = BXE_STATE_DISABLED;
16025255736Sdavidch        BXE_CORE_UNLOCK(sc);
16026255736Sdavidch    }
16027255736Sdavidch
16028255736Sdavidch    /* release the network interface */
16029255736Sdavidch    if (ifp != NULL) {
16030270876Sglebius        ether_ifdetach(ifp);
16031255736Sdavidch    }
16032255736Sdavidch    ifmedia_removeall(&sc->ifmedia);
16033255736Sdavidch
16034255736Sdavidch    /* XXX do the following based on driver state... */
16035255736Sdavidch
16036255736Sdavidch    /* free the host hardware/software hsi structures */
16037255736Sdavidch    bxe_free_hsi_mem(sc);
16038255736Sdavidch
16039255736Sdavidch    /* free ilt */
16040255736Sdavidch    bxe_free_ilt_mem(sc);
16041255736Sdavidch
16042296579Sdavidcs    bxe_free_buf_rings(sc);
16043296579Sdavidcs
16044255736Sdavidch    /* release the interrupts */
16045255736Sdavidch    bxe_interrupt_free(sc);
16046255736Sdavidch
16047255736Sdavidch    /* Release the mutexes*/
16048296579Sdavidcs    bxe_destroy_fp_mutexs(sc);
16049255736Sdavidch    bxe_release_mutexes(sc);
16050255736Sdavidch
16051296579Sdavidcs
16052255736Sdavidch    /* Release the PCIe BAR mapped memory */
16053255736Sdavidch    bxe_deallocate_bars(sc);
16054255736Sdavidch
16055255736Sdavidch    /* Release the FreeBSD interface. */
16056266979Smarcel    if (sc->ifp != NULL) {
16057270876Sglebius        if_free(sc->ifp);
16058255736Sdavidch    }
16059255736Sdavidch
16060255736Sdavidch    pci_disable_busmaster(dev);
16061255736Sdavidch
16062255736Sdavidch    return (0);
16063255736Sdavidch}
16064255736Sdavidch
16065255736Sdavidch/*
16066255736Sdavidch * Device shutdown function.
16067255736Sdavidch *
16068255736Sdavidch * Stops and resets the controller.
16069255736Sdavidch *
16070255736Sdavidch * Returns:
16071255736Sdavidch *   Nothing
16072255736Sdavidch */
16073255736Sdavidchstatic int
16074255736Sdavidchbxe_shutdown(device_t dev)
16075255736Sdavidch{
16076255736Sdavidch    struct bxe_softc *sc;
16077255736Sdavidch
16078255736Sdavidch    sc = device_get_softc(dev);
16079255736Sdavidch
16080255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16081255736Sdavidch
16082255736Sdavidch    /* stop the periodic callout */
16083255736Sdavidch    bxe_periodic_stop(sc);
16084255736Sdavidch
16085255736Sdavidch    BXE_CORE_LOCK(sc);
16086255736Sdavidch    bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16087255736Sdavidch    BXE_CORE_UNLOCK(sc);
16088255736Sdavidch
16089255736Sdavidch    return (0);
16090255736Sdavidch}
16091255736Sdavidch
16092255736Sdavidchvoid
16093255736Sdavidchbxe_igu_ack_sb(struct bxe_softc *sc,
16094255736Sdavidch               uint8_t          igu_sb_id,
16095255736Sdavidch               uint8_t          segment,
16096255736Sdavidch               uint16_t         index,
16097255736Sdavidch               uint8_t          op,
16098255736Sdavidch               uint8_t          update)
16099255736Sdavidch{
16100255736Sdavidch    uint32_t igu_addr = sc->igu_base_addr;
16101255736Sdavidch    igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16102255736Sdavidch    bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16103255736Sdavidch}
16104255736Sdavidch
16105255736Sdavidchstatic void
16106255736Sdavidchbxe_igu_clear_sb_gen(struct bxe_softc *sc,
16107255736Sdavidch                     uint8_t          func,
16108255736Sdavidch                     uint8_t          idu_sb_id,
16109255736Sdavidch                     uint8_t          is_pf)
16110255736Sdavidch{
16111255736Sdavidch    uint32_t data, ctl, cnt = 100;
16112255736Sdavidch    uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16113255736Sdavidch    uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16114255736Sdavidch    uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16115255736Sdavidch    uint32_t sb_bit =  1 << (idu_sb_id%32);
16116255736Sdavidch    uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16117255736Sdavidch    uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16118255736Sdavidch
16119255736Sdavidch    /* Not supported in BC mode */
16120255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
16121255736Sdavidch        return;
16122255736Sdavidch    }
16123255736Sdavidch
16124255736Sdavidch    data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16125255736Sdavidch             IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16126255736Sdavidch            IGU_REGULAR_CLEANUP_SET |
16127255736Sdavidch            IGU_REGULAR_BCLEANUP);
16128255736Sdavidch
16129255736Sdavidch    ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16130255736Sdavidch           (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16131255736Sdavidch           (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16132255736Sdavidch
16133255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16134255736Sdavidch            data, igu_addr_data);
16135255736Sdavidch    REG_WR(sc, igu_addr_data, data);
16136255736Sdavidch
16137255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16138255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16139255736Sdavidch    mb();
16140255736Sdavidch
16141255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16142255736Sdavidch            ctl, igu_addr_ctl);
16143255736Sdavidch    REG_WR(sc, igu_addr_ctl, ctl);
16144255736Sdavidch
16145255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16146255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16147255736Sdavidch    mb();
16148255736Sdavidch
16149255736Sdavidch    /* wait for clean up to finish */
16150255736Sdavidch    while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16151255736Sdavidch        DELAY(20000);
16152255736Sdavidch    }
16153255736Sdavidch
16154255736Sdavidch    if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16155255736Sdavidch        BLOGD(sc, DBG_LOAD,
16156255736Sdavidch              "Unable to finish IGU cleanup: "
16157255736Sdavidch              "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16158255736Sdavidch              idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16159255736Sdavidch    }
16160255736Sdavidch}
16161255736Sdavidch
16162255736Sdavidchstatic void
16163255736Sdavidchbxe_igu_clear_sb(struct bxe_softc *sc,
16164255736Sdavidch                 uint8_t          idu_sb_id)
16165255736Sdavidch{
16166255736Sdavidch    bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16167255736Sdavidch}
16168255736Sdavidch
16169255736Sdavidch
16170255736Sdavidch
16171255736Sdavidch
16172255736Sdavidch
16173255736Sdavidch
16174255736Sdavidch
16175255736Sdavidch/*******************/
16176255736Sdavidch/* ECORE CALLBACKS */
16177255736Sdavidch/*******************/
16178255736Sdavidch
16179255736Sdavidchstatic void
16180255736Sdavidchbxe_reset_common(struct bxe_softc *sc)
16181255736Sdavidch{
16182255736Sdavidch    uint32_t val = 0x1400;
16183255736Sdavidch
16184255736Sdavidch    /* reset_common */
16185255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16186255736Sdavidch
16187255736Sdavidch    if (CHIP_IS_E3(sc)) {
16188255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16189255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16190255736Sdavidch    }
16191255736Sdavidch
16192255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16193255736Sdavidch}
16194255736Sdavidch
16195255736Sdavidchstatic void
16196255736Sdavidchbxe_common_init_phy(struct bxe_softc *sc)
16197255736Sdavidch{
16198255736Sdavidch    uint32_t shmem_base[2];
16199255736Sdavidch    uint32_t shmem2_base[2];
16200255736Sdavidch
16201255736Sdavidch    /* Avoid common init in case MFW supports LFA */
16202255736Sdavidch    if (SHMEM2_RD(sc, size) >
16203255736Sdavidch        (uint32_t)offsetof(struct shmem2_region,
16204255736Sdavidch                           lfa_host_addr[SC_PORT(sc)])) {
16205255736Sdavidch        return;
16206255736Sdavidch    }
16207255736Sdavidch
16208255736Sdavidch    shmem_base[0]  = sc->devinfo.shmem_base;
16209255736Sdavidch    shmem2_base[0] = sc->devinfo.shmem2_base;
16210255736Sdavidch
16211255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16212255736Sdavidch        shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16213255736Sdavidch        shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16214255736Sdavidch    }
16215255736Sdavidch
16216284335Sdavidcs    bxe_acquire_phy_lock(sc);
16217255736Sdavidch    elink_common_init_phy(sc, shmem_base, shmem2_base,
16218255736Sdavidch                          sc->devinfo.chip_id, 0);
16219284335Sdavidcs    bxe_release_phy_lock(sc);
16220255736Sdavidch}
16221255736Sdavidch
16222255736Sdavidchstatic void
16223255736Sdavidchbxe_pf_disable(struct bxe_softc *sc)
16224255736Sdavidch{
16225255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16226255736Sdavidch
16227255736Sdavidch    val &= ~IGU_PF_CONF_FUNC_EN;
16228255736Sdavidch
16229255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16230255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16231255736Sdavidch    REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16232255736Sdavidch}
16233255736Sdavidch
16234255736Sdavidchstatic void
16235255736Sdavidchbxe_init_pxp(struct bxe_softc *sc)
16236255736Sdavidch{
16237255736Sdavidch    uint16_t devctl;
16238255736Sdavidch    int r_order, w_order;
16239255736Sdavidch
16240255736Sdavidch    devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16241255736Sdavidch
16242255736Sdavidch    BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16243255736Sdavidch
16244255736Sdavidch    w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16245255736Sdavidch
16246255736Sdavidch    if (sc->mrrs == -1) {
16247255736Sdavidch        r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16248255736Sdavidch    } else {
16249255736Sdavidch        BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16250255736Sdavidch        r_order = sc->mrrs;
16251255736Sdavidch    }
16252255736Sdavidch
16253255736Sdavidch    ecore_init_pxp_arb(sc, r_order, w_order);
16254255736Sdavidch}
16255255736Sdavidch
16256255736Sdavidchstatic uint32_t
16257255736Sdavidchbxe_get_pretend_reg(struct bxe_softc *sc)
16258255736Sdavidch{
16259255736Sdavidch    uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16260255736Sdavidch    uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16261255736Sdavidch    return (base + (SC_ABS_FUNC(sc)) * stride);
16262255736Sdavidch}
16263255736Sdavidch
16264255736Sdavidch/*
16265255736Sdavidch * Called only on E1H or E2.
16266255736Sdavidch * When pretending to be PF, the pretend value is the function number 0..7.
16267255736Sdavidch * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16268255736Sdavidch * combination.
16269255736Sdavidch */
16270255736Sdavidchstatic int
16271255736Sdavidchbxe_pretend_func(struct bxe_softc *sc,
16272255736Sdavidch                 uint16_t         pretend_func_val)
16273255736Sdavidch{
16274255736Sdavidch    uint32_t pretend_reg;
16275255736Sdavidch
16276255736Sdavidch    if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16277255736Sdavidch        return (-1);
16278255736Sdavidch    }
16279255736Sdavidch
16280255736Sdavidch    /* get my own pretend register */
16281255736Sdavidch    pretend_reg = bxe_get_pretend_reg(sc);
16282255736Sdavidch    REG_WR(sc, pretend_reg, pretend_func_val);
16283255736Sdavidch    REG_RD(sc, pretend_reg);
16284255736Sdavidch    return (0);
16285255736Sdavidch}
16286255736Sdavidch
16287255736Sdavidchstatic void
16288255736Sdavidchbxe_iov_init_dmae(struct bxe_softc *sc)
16289255736Sdavidch{
16290255736Sdavidch    return;
16291255736Sdavidch}
16292255736Sdavidch
16293255736Sdavidchstatic void
16294255736Sdavidchbxe_iov_init_dq(struct bxe_softc *sc)
16295255736Sdavidch{
16296255736Sdavidch    return;
16297255736Sdavidch}
16298255736Sdavidch
16299255736Sdavidch/* send a NIG loopback debug packet */
16300255736Sdavidchstatic void
16301255736Sdavidchbxe_lb_pckt(struct bxe_softc *sc)
16302255736Sdavidch{
16303255736Sdavidch    uint32_t wb_write[3];
16304255736Sdavidch
16305255736Sdavidch    /* Ethernet source and destination addresses */
16306255736Sdavidch    wb_write[0] = 0x55555555;
16307255736Sdavidch    wb_write[1] = 0x55555555;
16308255736Sdavidch    wb_write[2] = 0x20;     /* SOP */
16309255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16310255736Sdavidch
16311255736Sdavidch    /* NON-IP protocol */
16312255736Sdavidch    wb_write[0] = 0x09000000;
16313255736Sdavidch    wb_write[1] = 0x55555555;
16314255736Sdavidch    wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16315255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16316255736Sdavidch}
16317255736Sdavidch
16318255736Sdavidch/*
16319255736Sdavidch * Some of the internal memories are not directly readable from the driver.
16320255736Sdavidch * To test them we send debug packets.
16321255736Sdavidch */
16322255736Sdavidchstatic int
16323255736Sdavidchbxe_int_mem_test(struct bxe_softc *sc)
16324255736Sdavidch{
16325255736Sdavidch    int factor;
16326255736Sdavidch    int count, i;
16327255736Sdavidch    uint32_t val = 0;
16328255736Sdavidch
16329255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
16330255736Sdavidch        factor = 120;
16331255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
16332255736Sdavidch        factor = 200;
16333255736Sdavidch    } else {
16334255736Sdavidch        factor = 1;
16335255736Sdavidch    }
16336255736Sdavidch
16337255736Sdavidch    /* disable inputs of parser neighbor blocks */
16338255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16339255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16340255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16341255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16342255736Sdavidch
16343255736Sdavidch    /*  write 0 to parser credits for CFC search request */
16344255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16345255736Sdavidch
16346255736Sdavidch    /* send Ethernet packet */
16347255736Sdavidch    bxe_lb_pckt(sc);
16348255736Sdavidch
16349255736Sdavidch    /* TODO do i reset NIG statistic? */
16350255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16351255736Sdavidch    count = 1000 * factor;
16352255736Sdavidch    while (count) {
16353255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16354255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16355255736Sdavidch        if (val == 0x10) {
16356255736Sdavidch            break;
16357255736Sdavidch        }
16358255736Sdavidch
16359255736Sdavidch        DELAY(10000);
16360255736Sdavidch        count--;
16361255736Sdavidch    }
16362255736Sdavidch
16363255736Sdavidch    if (val != 0x10) {
16364255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16365255736Sdavidch        return (-1);
16366255736Sdavidch    }
16367255736Sdavidch
16368255736Sdavidch    /* wait until PRS register shows 1 packet */
16369255736Sdavidch    count = (1000 * factor);
16370255736Sdavidch    while (count) {
16371255736Sdavidch        val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16372255736Sdavidch        if (val == 1) {
16373255736Sdavidch            break;
16374255736Sdavidch        }
16375255736Sdavidch
16376255736Sdavidch        DELAY(10000);
16377255736Sdavidch        count--;
16378255736Sdavidch    }
16379255736Sdavidch
16380255736Sdavidch    if (val != 0x1) {
16381255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16382255736Sdavidch        return (-2);
16383255736Sdavidch    }
16384255736Sdavidch
16385255736Sdavidch    /* Reset and init BRB, PRS */
16386255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16387255736Sdavidch    DELAY(50000);
16388255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16389255736Sdavidch    DELAY(50000);
16390255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16391255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16392255736Sdavidch
16393255736Sdavidch    /* Disable inputs of parser neighbor blocks */
16394255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16395255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16396255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16397255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16398255736Sdavidch
16399255736Sdavidch    /* Write 0 to parser credits for CFC search request */
16400255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16401255736Sdavidch
16402255736Sdavidch    /* send 10 Ethernet packets */
16403255736Sdavidch    for (i = 0; i < 10; i++) {
16404255736Sdavidch        bxe_lb_pckt(sc);
16405255736Sdavidch    }
16406255736Sdavidch
16407255736Sdavidch    /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16408255736Sdavidch    count = (1000 * factor);
16409255736Sdavidch    while (count) {
16410255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16411255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16412255736Sdavidch        if (val == 0xb0) {
16413255736Sdavidch            break;
16414255736Sdavidch        }
16415255736Sdavidch
16416255736Sdavidch        DELAY(10000);
16417255736Sdavidch        count--;
16418255736Sdavidch    }
16419255736Sdavidch
16420255736Sdavidch    if (val != 0xb0) {
16421255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16422255736Sdavidch        return (-3);
16423255736Sdavidch    }
16424255736Sdavidch
16425255736Sdavidch    /* Wait until PRS register shows 2 packets */
16426255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16427255736Sdavidch    if (val != 2) {
16428255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16429255736Sdavidch    }
16430255736Sdavidch
16431255736Sdavidch    /* Write 1 to parser credits for CFC search request */
16432255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16433255736Sdavidch
16434255736Sdavidch    /* Wait until PRS register shows 3 packets */
16435255736Sdavidch    DELAY(10000 * factor);
16436255736Sdavidch
16437255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16438255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16439255736Sdavidch    if (val != 3) {
16440255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16441255736Sdavidch    }
16442255736Sdavidch
16443255736Sdavidch    /* clear NIG EOP FIFO */
16444255736Sdavidch    for (i = 0; i < 11; i++) {
16445255736Sdavidch        REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16446255736Sdavidch    }
16447255736Sdavidch
16448255736Sdavidch    val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16449255736Sdavidch    if (val != 1) {
16450295830Sdavidcs        BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16451255736Sdavidch        return (-4);
16452255736Sdavidch    }
16453255736Sdavidch
16454255736Sdavidch    /* Reset and init BRB, PRS, NIG */
16455255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16456255736Sdavidch    DELAY(50000);
16457255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16458255736Sdavidch    DELAY(50000);
16459255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16460255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16461255736Sdavidch    if (!CNIC_SUPPORT(sc)) {
16462255736Sdavidch        /* set NIC mode */
16463255736Sdavidch        REG_WR(sc, PRS_REG_NIC_MODE, 1);
16464255736Sdavidch    }
16465255736Sdavidch
16466255736Sdavidch    /* Enable inputs of parser neighbor blocks */
16467255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16468255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16469255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16470255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16471255736Sdavidch
16472255736Sdavidch    return (0);
16473255736Sdavidch}
16474255736Sdavidch
16475255736Sdavidchstatic void
16476255736Sdavidchbxe_setup_fan_failure_detection(struct bxe_softc *sc)
16477255736Sdavidch{
16478255736Sdavidch    int is_required;
16479255736Sdavidch    uint32_t val;
16480255736Sdavidch    int port;
16481255736Sdavidch
16482255736Sdavidch    is_required = 0;
16483255736Sdavidch    val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
16484255736Sdavidch           SHARED_HW_CFG_FAN_FAILURE_MASK);
16485255736Sdavidch
16486255736Sdavidch    if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
16487255736Sdavidch        is_required = 1;
16488255736Sdavidch    }
16489255736Sdavidch    /*
16490255736Sdavidch     * The fan failure mechanism is usually related to the PHY type since
16491255736Sdavidch     * the power consumption of the board is affected by the PHY. Currently,
16492255736Sdavidch     * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
16493255736Sdavidch     */
16494255736Sdavidch    else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
16495255736Sdavidch        for (port = PORT_0; port < PORT_MAX; port++) {
16496255736Sdavidch            is_required |= elink_fan_failure_det_req(sc,
16497255736Sdavidch                                                     sc->devinfo.shmem_base,
16498255736Sdavidch                                                     sc->devinfo.shmem2_base,
16499255736Sdavidch                                                     port);
16500255736Sdavidch        }
16501255736Sdavidch    }
16502255736Sdavidch
16503255736Sdavidch    BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
16504255736Sdavidch
16505255736Sdavidch    if (is_required == 0) {
16506255736Sdavidch        return;
16507255736Sdavidch    }
16508255736Sdavidch
16509255736Sdavidch    /* Fan failure is indicated by SPIO 5 */
16510255736Sdavidch    bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
16511255736Sdavidch
16512255736Sdavidch    /* set to active low mode */
16513255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_INT);
16514255736Sdavidch    val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
16515255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_INT, val);
16516255736Sdavidch
16517255736Sdavidch    /* enable interrupt to signal the IGU */
16518255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
16519255736Sdavidch    val |= MISC_SPIO_SPIO5;
16520255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
16521255736Sdavidch}
16522255736Sdavidch
16523255736Sdavidchstatic void
16524255736Sdavidchbxe_enable_blocks_attention(struct bxe_softc *sc)
16525255736Sdavidch{
16526255736Sdavidch    uint32_t val;
16527255736Sdavidch
16528255736Sdavidch    REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16529255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16530255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
16531255736Sdavidch    } else {
16532255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
16533255736Sdavidch    }
16534255736Sdavidch    REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16535255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16536255736Sdavidch    /*
16537255736Sdavidch     * mask read length error interrupts in brb for parser
16538255736Sdavidch     * (parsing unit and 'checksum and crc' unit)
16539255736Sdavidch     * these errors are legal (PU reads fixed length and CAC can cause
16540255736Sdavidch     * read length error on truncated packets)
16541255736Sdavidch     */
16542255736Sdavidch    REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
16543255736Sdavidch    REG_WR(sc, QM_REG_QM_INT_MASK, 0);
16544255736Sdavidch    REG_WR(sc, TM_REG_TM_INT_MASK, 0);
16545255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
16546255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
16547255736Sdavidch    REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
16548255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
16549255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
16550255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
16551255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
16552255736Sdavidch    REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
16553255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
16554255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
16555255736Sdavidch    REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
16556255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
16557255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
16558255736Sdavidch    REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
16559255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
16560255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
16561255736Sdavidch
16562255736Sdavidch    val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
16563255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
16564255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
16565255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16566255736Sdavidch        val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
16567255736Sdavidch                PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
16568255736Sdavidch    }
16569255736Sdavidch    REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
16570255736Sdavidch
16571255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
16572255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
16573255736Sdavidch    REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
16574255736Sdavidch/*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
16575255736Sdavidch
16576255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16577255736Sdavidch        /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
16578255736Sdavidch        REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
16579255736Sdavidch    }
16580255736Sdavidch
16581255736Sdavidch    REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
16582255736Sdavidch    REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
16583255736Sdavidch/*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
16584255736Sdavidch    REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
16585255736Sdavidch}
16586255736Sdavidch
16587255736Sdavidch/**
16588255736Sdavidch * bxe_init_hw_common - initialize the HW at the COMMON phase.
16589255736Sdavidch *
16590255736Sdavidch * @sc:     driver handle
16591255736Sdavidch */
16592255736Sdavidchstatic int
16593255736Sdavidchbxe_init_hw_common(struct bxe_softc *sc)
16594255736Sdavidch{
16595255736Sdavidch    uint8_t abs_func_id;
16596255736Sdavidch    uint32_t val;
16597255736Sdavidch
16598255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
16599255736Sdavidch          SC_ABS_FUNC(sc));
16600255736Sdavidch
16601255736Sdavidch    /*
16602255736Sdavidch     * take the RESET lock to protect undi_unload flow from accessing
16603255736Sdavidch     * registers while we are resetting the chip
16604255736Sdavidch     */
16605255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16606255736Sdavidch
16607255736Sdavidch    bxe_reset_common(sc);
16608255736Sdavidch
16609255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
16610255736Sdavidch
16611255736Sdavidch    val = 0xfffc;
16612255736Sdavidch    if (CHIP_IS_E3(sc)) {
16613255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16614255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16615255736Sdavidch    }
16616255736Sdavidch
16617255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
16618255736Sdavidch
16619255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16620255736Sdavidch
16621255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
16622255736Sdavidch    BLOGD(sc, DBG_LOAD, "after misc block init\n");
16623255736Sdavidch
16624255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16625255736Sdavidch        /*
16626255736Sdavidch         * 4-port mode or 2-port mode we need to turn off master-enable for
16627255736Sdavidch         * everyone. After that we turn it back on for self. So, we disregard
16628255736Sdavidch         * multi-function, and always disable all functions on the given path,
16629255736Sdavidch         * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
16630255736Sdavidch         */
16631255736Sdavidch        for (abs_func_id = SC_PATH(sc);
16632255736Sdavidch             abs_func_id < (E2_FUNC_MAX * 2);
16633255736Sdavidch             abs_func_id += 2) {
16634255736Sdavidch            if (abs_func_id == SC_ABS_FUNC(sc)) {
16635255736Sdavidch                REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
16636255736Sdavidch                continue;
16637255736Sdavidch            }
16638255736Sdavidch
16639255736Sdavidch            bxe_pretend_func(sc, abs_func_id);
16640255736Sdavidch
16641255736Sdavidch            /* clear pf enable */
16642255736Sdavidch            bxe_pf_disable(sc);
16643255736Sdavidch
16644255736Sdavidch            bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16645255736Sdavidch        }
16646255736Sdavidch    }
16647255736Sdavidch
16648255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pf disable\n");
16649255736Sdavidch
16650255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
16651255736Sdavidch
16652255736Sdavidch    if (CHIP_IS_E1(sc)) {
16653255736Sdavidch        /*
16654255736Sdavidch         * enable HW interrupt from PXP on USDM overflow
16655255736Sdavidch         * bit 16 on INT_MASK_0
16656255736Sdavidch         */
16657255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16658255736Sdavidch    }
16659255736Sdavidch
16660255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
16661255736Sdavidch    bxe_init_pxp(sc);
16662255736Sdavidch
16663255736Sdavidch#ifdef __BIG_ENDIAN
16664255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
16665255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
16666255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
16667255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
16668255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
16669255736Sdavidch    /* make sure this value is 0 */
16670255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
16671255736Sdavidch
16672255736Sdavidch    //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
16673255736Sdavidch    REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
16674255736Sdavidch    REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
16675255736Sdavidch    REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
16676255736Sdavidch    REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
16677255736Sdavidch#endif
16678255736Sdavidch
16679255736Sdavidch    ecore_ilt_init_page_size(sc, INITOP_SET);
16680255736Sdavidch
16681255736Sdavidch    if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
16682255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
16683255736Sdavidch    }
16684255736Sdavidch
16685255736Sdavidch    /* let the HW do it's magic... */
16686255736Sdavidch    DELAY(100000);
16687255736Sdavidch
16688255736Sdavidch    /* finish PXP init */
16689255736Sdavidch    val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
16690255736Sdavidch    if (val != 1) {
16691295830Sdavidcs        BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
16692295830Sdavidcs            val);
16693255736Sdavidch        return (-1);
16694255736Sdavidch    }
16695255736Sdavidch    val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
16696255736Sdavidch    if (val != 1) {
16697295830Sdavidcs        BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
16698255736Sdavidch        return (-1);
16699255736Sdavidch    }
16700255736Sdavidch
16701255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pxp init\n");
16702255736Sdavidch
16703255736Sdavidch    /*
16704255736Sdavidch     * Timer bug workaround for E2 only. We need to set the entire ILT to have
16705255736Sdavidch     * entries with value "0" and valid bit on. This needs to be done by the
16706255736Sdavidch     * first PF that is loaded in a path (i.e. common phase)
16707255736Sdavidch     */
16708255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16709255736Sdavidch/*
16710255736Sdavidch * In E2 there is a bug in the timers block that can cause function 6 / 7
16711255736Sdavidch * (i.e. vnic3) to start even if it is marked as "scan-off".
16712255736Sdavidch * This occurs when a different function (func2,3) is being marked
16713255736Sdavidch * as "scan-off". Real-life scenario for example: if a driver is being
16714255736Sdavidch * load-unloaded while func6,7 are down. This will cause the timer to access
16715255736Sdavidch * the ilt, translate to a logical address and send a request to read/write.
16716255736Sdavidch * Since the ilt for the function that is down is not valid, this will cause
16717255736Sdavidch * a translation error which is unrecoverable.
16718255736Sdavidch * The Workaround is intended to make sure that when this happens nothing
16719255736Sdavidch * fatal will occur. The workaround:
16720255736Sdavidch *  1.  First PF driver which loads on a path will:
16721255736Sdavidch *      a.  After taking the chip out of reset, by using pretend,
16722255736Sdavidch *          it will write "0" to the following registers of
16723255736Sdavidch *          the other vnics.
16724255736Sdavidch *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16725255736Sdavidch *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
16726255736Sdavidch *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
16727255736Sdavidch *          And for itself it will write '1' to
16728255736Sdavidch *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
16729255736Sdavidch *          dmae-operations (writing to pram for example.)
16730255736Sdavidch *          note: can be done for only function 6,7 but cleaner this
16731255736Sdavidch *            way.
16732255736Sdavidch *      b.  Write zero+valid to the entire ILT.
16733255736Sdavidch *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
16734255736Sdavidch *          VNIC3 (of that port). The range allocated will be the
16735255736Sdavidch *          entire ILT. This is needed to prevent  ILT range error.
16736255736Sdavidch *  2.  Any PF driver load flow:
16737255736Sdavidch *      a.  ILT update with the physical addresses of the allocated
16738255736Sdavidch *          logical pages.
16739255736Sdavidch *      b.  Wait 20msec. - note that this timeout is needed to make
16740255736Sdavidch *          sure there are no requests in one of the PXP internal
16741255736Sdavidch *          queues with "old" ILT addresses.
16742255736Sdavidch *      c.  PF enable in the PGLC.
16743255736Sdavidch *      d.  Clear the was_error of the PF in the PGLC. (could have
16744255736Sdavidch *          occurred while driver was down)
16745255736Sdavidch *      e.  PF enable in the CFC (WEAK + STRONG)
16746255736Sdavidch *      f.  Timers scan enable
16747255736Sdavidch *  3.  PF driver unload flow:
16748255736Sdavidch *      a.  Clear the Timers scan_en.
16749255736Sdavidch *      b.  Polling for scan_on=0 for that PF.
16750255736Sdavidch *      c.  Clear the PF enable bit in the PXP.
16751255736Sdavidch *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
16752255736Sdavidch *      e.  Write zero+valid to all ILT entries (The valid bit must
16753255736Sdavidch *          stay set)
16754255736Sdavidch *      f.  If this is VNIC 3 of a port then also init
16755255736Sdavidch *          first_timers_ilt_entry to zero and last_timers_ilt_entry
16756255736Sdavidch *          to the last enrty in the ILT.
16757255736Sdavidch *
16758255736Sdavidch *      Notes:
16759255736Sdavidch *      Currently the PF error in the PGLC is non recoverable.
16760255736Sdavidch *      In the future the there will be a recovery routine for this error.
16761255736Sdavidch *      Currently attention is masked.
16762255736Sdavidch *      Having an MCP lock on the load/unload process does not guarantee that
16763255736Sdavidch *      there is no Timer disable during Func6/7 enable. This is because the
16764255736Sdavidch *      Timers scan is currently being cleared by the MCP on FLR.
16765255736Sdavidch *      Step 2.d can be done only for PF6/7 and the driver can also check if
16766255736Sdavidch *      there is error before clearing it. But the flow above is simpler and
16767255736Sdavidch *      more general.
16768255736Sdavidch *      All ILT entries are written by zero+valid and not just PF6/7
16769255736Sdavidch *      ILT entries since in the future the ILT entries allocation for
16770255736Sdavidch *      PF-s might be dynamic.
16771255736Sdavidch */
16772255736Sdavidch        struct ilt_client_info ilt_cli;
16773255736Sdavidch        struct ecore_ilt ilt;
16774255736Sdavidch
16775255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
16776255736Sdavidch        memset(&ilt, 0, sizeof(struct ecore_ilt));
16777255736Sdavidch
16778255736Sdavidch        /* initialize dummy TM client */
16779255736Sdavidch        ilt_cli.start      = 0;
16780255736Sdavidch        ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
16781255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
16782255736Sdavidch
16783255736Sdavidch        /*
16784255736Sdavidch         * Step 1: set zeroes to all ilt page entries with valid bit on
16785255736Sdavidch         * Step 2: set the timers first/last ilt entry to point
16786255736Sdavidch         * to the entire range to prevent ILT range error for 3rd/4th
16787255736Sdavidch         * vnic (this code assumes existence of the vnic)
16788255736Sdavidch         *
16789255736Sdavidch         * both steps performed by call to ecore_ilt_client_init_op()
16790255736Sdavidch         * with dummy TM client
16791255736Sdavidch         *
16792255736Sdavidch         * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
16793255736Sdavidch         * and his brother are split registers
16794255736Sdavidch         */
16795255736Sdavidch
16796255736Sdavidch        bxe_pretend_func(sc, (SC_PATH(sc) + 6));
16797255736Sdavidch        ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
16798255736Sdavidch        bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16799255736Sdavidch
16800255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
16801255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
16802255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
16803255736Sdavidch    }
16804255736Sdavidch
16805255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
16806255736Sdavidch    REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
16807255736Sdavidch
16808255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16809255736Sdavidch        int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
16810255736Sdavidch                     (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
16811255736Sdavidch
16812255736Sdavidch        ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
16813255736Sdavidch        ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
16814255736Sdavidch
16815255736Sdavidch        /* let the HW do it's magic... */
16816255736Sdavidch        do {
16817255736Sdavidch            DELAY(200000);
16818255736Sdavidch            val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
16819255736Sdavidch        } while (factor-- && (val != 1));
16820255736Sdavidch
16821255736Sdavidch        if (val != 1) {
16822295830Sdavidcs            BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
16823255736Sdavidch            return (-1);
16824255736Sdavidch        }
16825255736Sdavidch    }
16826255736Sdavidch
16827255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
16828255736Sdavidch
16829255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
16830255736Sdavidch
16831255736Sdavidch    bxe_iov_init_dmae(sc);
16832255736Sdavidch
16833255736Sdavidch    /* clean the DMAE memory */
16834255736Sdavidch    sc->dmae_ready = 1;
16835255736Sdavidch    ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
16836255736Sdavidch
16837255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
16838255736Sdavidch
16839255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
16840255736Sdavidch
16841255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
16842255736Sdavidch
16843255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
16844255736Sdavidch
16845255736Sdavidch    bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
16846255736Sdavidch    bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
16847255736Sdavidch    bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
16848255736Sdavidch    bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
16849255736Sdavidch
16850255736Sdavidch    ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
16851255736Sdavidch
16852255736Sdavidch    /* QM queues pointers table */
16853255736Sdavidch    ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
16854255736Sdavidch
16855255736Sdavidch    /* soft reset pulse */
16856255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 1);
16857255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 0);
16858255736Sdavidch
16859255736Sdavidch    if (CNIC_SUPPORT(sc))
16860255736Sdavidch        ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
16861255736Sdavidch
16862255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
16863255736Sdavidch    REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
16864255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
16865255736Sdavidch        /* enable hw interrupt from doorbell Q */
16866255736Sdavidch        REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16867255736Sdavidch    }
16868255736Sdavidch
16869255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16870255736Sdavidch
16871255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16872255736Sdavidch    REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
16873255736Sdavidch
16874255736Sdavidch    if (!CHIP_IS_E1(sc)) {
16875255736Sdavidch        REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
16876255736Sdavidch    }
16877255736Sdavidch
16878255736Sdavidch    if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
16879255736Sdavidch        if (IS_MF_AFEX(sc)) {
16880255736Sdavidch            /*
16881255736Sdavidch             * configure that AFEX and VLAN headers must be
16882255736Sdavidch             * received in AFEX mode
16883255736Sdavidch             */
16884255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
16885255736Sdavidch            REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
16886255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
16887255736Sdavidch            REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
16888255736Sdavidch            REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
16889255736Sdavidch        } else {
16890255736Sdavidch            /*
16891255736Sdavidch             * Bit-map indicating which L2 hdrs may appear
16892255736Sdavidch             * after the basic Ethernet header
16893255736Sdavidch             */
16894255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
16895255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
16896255736Sdavidch        }
16897255736Sdavidch    }
16898255736Sdavidch
16899255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
16900255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
16901255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
16902255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
16903255736Sdavidch
16904255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16905255736Sdavidch        /* reset VFC memories */
16906255736Sdavidch        REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16907255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
16908255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
16909255736Sdavidch        REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16910255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
16911255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
16912255736Sdavidch
16913255736Sdavidch        DELAY(20000);
16914255736Sdavidch    }
16915255736Sdavidch
16916255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
16917255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
16918255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
16919255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
16920255736Sdavidch
16921255736Sdavidch    /* sync semi rtc */
16922255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
16923255736Sdavidch           0x80000000);
16924255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
16925255736Sdavidch           0x80000000);
16926255736Sdavidch
16927255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
16928255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
16929255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
16930255736Sdavidch
16931255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16932255736Sdavidch        if (IS_MF_AFEX(sc)) {
16933255736Sdavidch            /*
16934255736Sdavidch             * configure that AFEX and VLAN headers must be
16935255736Sdavidch             * sent in AFEX mode
16936255736Sdavidch             */
16937255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
16938255736Sdavidch            REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
16939255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
16940255736Sdavidch            REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
16941255736Sdavidch            REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
16942255736Sdavidch        } else {
16943255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
16944255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
16945255736Sdavidch        }
16946255736Sdavidch    }
16947255736Sdavidch
16948255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 1);
16949255736Sdavidch
16950255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
16951255736Sdavidch
16952255736Sdavidch    if (CNIC_SUPPORT(sc)) {
16953255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
16954255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
16955255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
16956255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
16957255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
16958255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
16959255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
16960255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
16961255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
16962255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
16963255736Sdavidch    }
16964255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 0);
16965255736Sdavidch
16966255736Sdavidch    if (sizeof(union cdu_context) != 1024) {
16967255736Sdavidch        /* we currently assume that a context is 1024 bytes */
16968255736Sdavidch        BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
16969255736Sdavidch              (long)sizeof(union cdu_context));
16970255736Sdavidch    }
16971255736Sdavidch
16972255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
16973255736Sdavidch    val = (4 << 24) + (0 << 12) + 1024;
16974255736Sdavidch    REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
16975255736Sdavidch
16976255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
16977255736Sdavidch
16978255736Sdavidch    REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
16979255736Sdavidch    /* enable context validation interrupt from CFC */
16980255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16981255736Sdavidch
16982255736Sdavidch    /* set the thresholds to prevent CFC/CDU race */
16983255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
16984255736Sdavidch    ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
16985255736Sdavidch
16986255736Sdavidch    if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
16987255736Sdavidch        REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
16988255736Sdavidch    }
16989255736Sdavidch
16990255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
16991255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
16992255736Sdavidch
16993255736Sdavidch    /* Reset PCIE errors for debug */
16994255736Sdavidch    REG_WR(sc, 0x2814, 0xffffffff);
16995255736Sdavidch    REG_WR(sc, 0x3820, 0xffffffff);
16996255736Sdavidch
16997255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16998255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
16999255736Sdavidch               (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17000255736Sdavidch                PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17001255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17002255736Sdavidch               (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17003255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17004255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17005255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17006255736Sdavidch               (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17007255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17008255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17009255736Sdavidch    }
17010255736Sdavidch
17011255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17012255736Sdavidch
17013255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17014255736Sdavidch        /* in E3 this done in per-port section */
17015255736Sdavidch        if (!CHIP_IS_E3(sc))
17016255736Sdavidch            REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17017255736Sdavidch    }
17018255736Sdavidch
17019255736Sdavidch    if (CHIP_IS_E1H(sc)) {
17020255736Sdavidch        /* not applicable for E2 (and above ...) */
17021255736Sdavidch        REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17022255736Sdavidch    }
17023255736Sdavidch
17024255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
17025255736Sdavidch        DELAY(200000);
17026255736Sdavidch    }
17027255736Sdavidch
17028255736Sdavidch    /* finish CFC init */
17029255736Sdavidch    val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17030255736Sdavidch    if (val != 1) {
17031295830Sdavidcs        BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17032255736Sdavidch        return (-1);
17033255736Sdavidch    }
17034255736Sdavidch    val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17035255736Sdavidch    if (val != 1) {
17036295830Sdavidcs        BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17037255736Sdavidch        return (-1);
17038255736Sdavidch    }
17039255736Sdavidch    val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17040255736Sdavidch    if (val != 1) {
17041295830Sdavidcs        BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17042255736Sdavidch        return (-1);
17043255736Sdavidch    }
17044255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0);
17045255736Sdavidch
17046255736Sdavidch    if (CHIP_IS_E1(sc)) {
17047255736Sdavidch        /* read NIG statistic to see if this is our first up since powerup */
17048255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17049255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
17050255736Sdavidch
17051255736Sdavidch        /* do internal memory self test */
17052255736Sdavidch        if ((val == 0) && bxe_int_mem_test(sc)) {
17053295830Sdavidcs            BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17054255736Sdavidch            return (-1);
17055255736Sdavidch        }
17056255736Sdavidch    }
17057255736Sdavidch
17058255736Sdavidch    bxe_setup_fan_failure_detection(sc);
17059255736Sdavidch
17060255736Sdavidch    /* clear PXP2 attentions */
17061255736Sdavidch    REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17062255736Sdavidch
17063255736Sdavidch    bxe_enable_blocks_attention(sc);
17064255736Sdavidch
17065255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17066255736Sdavidch        ecore_enable_blocks_parity(sc);
17067255736Sdavidch    }
17068255736Sdavidch
17069255736Sdavidch    if (!BXE_NOMCP(sc)) {
17070255736Sdavidch        if (CHIP_IS_E1x(sc)) {
17071255736Sdavidch            bxe_common_init_phy(sc);
17072255736Sdavidch        }
17073255736Sdavidch    }
17074255736Sdavidch
17075255736Sdavidch    return (0);
17076255736Sdavidch}
17077255736Sdavidch
17078255736Sdavidch/**
17079255736Sdavidch * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17080255736Sdavidch *
17081255736Sdavidch * @sc:     driver handle
17082255736Sdavidch */
17083255736Sdavidchstatic int
17084255736Sdavidchbxe_init_hw_common_chip(struct bxe_softc *sc)
17085255736Sdavidch{
17086255736Sdavidch    int rc = bxe_init_hw_common(sc);
17087255736Sdavidch
17088255736Sdavidch    if (rc) {
17089295830Sdavidcs        BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17090255736Sdavidch        return (rc);
17091255736Sdavidch    }
17092255736Sdavidch
17093255736Sdavidch    /* In E2 2-PORT mode, same ext phy is used for the two paths */
17094255736Sdavidch    if (!BXE_NOMCP(sc)) {
17095255736Sdavidch        bxe_common_init_phy(sc);
17096255736Sdavidch    }
17097255736Sdavidch
17098255736Sdavidch    return (0);
17099255736Sdavidch}
17100255736Sdavidch
17101255736Sdavidchstatic int
17102255736Sdavidchbxe_init_hw_port(struct bxe_softc *sc)
17103255736Sdavidch{
17104255736Sdavidch    int port = SC_PORT(sc);
17105255736Sdavidch    int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17106255736Sdavidch    uint32_t low, high;
17107255736Sdavidch    uint32_t val;
17108255736Sdavidch
17109255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17110255736Sdavidch
17111255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17112255736Sdavidch
17113255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17114255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17115255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17116255736Sdavidch
17117255736Sdavidch    /*
17118255736Sdavidch     * Timers bug workaround: disables the pf_master bit in pglue at
17119255736Sdavidch     * common phase, we need to enable it here before any dmae access are
17120255736Sdavidch     * attempted. Therefore we manually added the enable-master to the
17121255736Sdavidch     * port phase (it also happens in the function phase)
17122255736Sdavidch     */
17123255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17124255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17125255736Sdavidch    }
17126255736Sdavidch
17127255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17128255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17129255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17130255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17131255736Sdavidch
17132255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17133255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17134255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17135255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17136255736Sdavidch
17137255736Sdavidch    /* QM cid (connection) count */
17138255736Sdavidch    ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17139255736Sdavidch
17140255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17141255736Sdavidch        ecore_init_block(sc, BLOCK_TM, init_phase);
17142255736Sdavidch        REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17143255736Sdavidch        REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17144255736Sdavidch    }
17145255736Sdavidch
17146255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17147255736Sdavidch
17148255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17149255736Sdavidch
17150255736Sdavidch    if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17151255736Sdavidch        if (IS_MF(sc)) {
17152255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 160 : 246);
17153255736Sdavidch        } else if (sc->mtu > 4096) {
17154255736Sdavidch            if (BXE_ONE_PORT(sc)) {
17155255736Sdavidch                low = 160;
17156255736Sdavidch            } else {
17157255736Sdavidch                val = sc->mtu;
17158255736Sdavidch                /* (24*1024 + val*4)/256 */
17159255736Sdavidch                low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17160255736Sdavidch            }
17161255736Sdavidch        } else {
17162255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 80 : 160);
17163255736Sdavidch        }
17164255736Sdavidch        high = (low + 56); /* 14*1024/256 */
17165255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17166255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17167255736Sdavidch    }
17168255736Sdavidch
17169255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
17170255736Sdavidch        REG_WR(sc, SC_PORT(sc) ?
17171255736Sdavidch               BRB1_REG_MAC_GUARANTIED_1 :
17172255736Sdavidch               BRB1_REG_MAC_GUARANTIED_0, 40);
17173255736Sdavidch    }
17174255736Sdavidch
17175255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17176255736Sdavidch    if (CHIP_IS_E3B0(sc)) {
17177255736Sdavidch        if (IS_MF_AFEX(sc)) {
17178255736Sdavidch            /* configure headers for AFEX mode */
17179255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17180255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17181255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17182255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17183255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17184255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17185255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17186255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17187255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17188255736Sdavidch        } else {
17189255736Sdavidch            /* Ovlan exists only if we are in multi-function +
17190255736Sdavidch             * switch-dependent mode, in switch-independent there
17191255736Sdavidch             * is no ovlan headers
17192255736Sdavidch             */
17193255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17194255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17195255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17196255736Sdavidch                   (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17197255736Sdavidch        }
17198255736Sdavidch    }
17199255736Sdavidch
17200255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17201255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17202255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17203255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17204255736Sdavidch
17205255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17206255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17207255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17208255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17209255736Sdavidch
17210255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17211255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17212255736Sdavidch
17213255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17214255736Sdavidch
17215255736Sdavidch    if (CHIP_IS_E1x(sc)) {
17216255736Sdavidch        /* configure PBF to work without PAUSE mtu 9000 */
17217255736Sdavidch        REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17218255736Sdavidch
17219255736Sdavidch        /* update threshold */
17220255736Sdavidch        REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17221255736Sdavidch        /* update init credit */
17222255736Sdavidch        REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17223255736Sdavidch
17224255736Sdavidch        /* probe changes */
17225255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17226255736Sdavidch        DELAY(50);
17227255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17228255736Sdavidch    }
17229255736Sdavidch
17230255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17231255736Sdavidch        ecore_init_block(sc, BLOCK_SRC, init_phase);
17232255736Sdavidch    }
17233255736Sdavidch
17234255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17235255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17236255736Sdavidch
17237255736Sdavidch    if (CHIP_IS_E1(sc)) {
17238255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17239255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17240255736Sdavidch    }
17241255736Sdavidch    ecore_init_block(sc, BLOCK_HC, init_phase);
17242255736Sdavidch
17243255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, init_phase);
17244255736Sdavidch
17245255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17246255736Sdavidch    /* init aeu_mask_attn_func_0/1:
17247255736Sdavidch     *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17248255736Sdavidch     *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17249255736Sdavidch     *             bits 4-7 are used for "per vn group attention" */
17250255736Sdavidch    val = IS_MF(sc) ? 0xF7 : 0x7;
17251255736Sdavidch    /* Enable DCBX attention for all but E1 */
17252255736Sdavidch    val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17253255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17254255736Sdavidch
17255255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17256255736Sdavidch
17257255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17258255736Sdavidch        /* Bit-map indicating which L2 hdrs may appear after the
17259255736Sdavidch         * basic Ethernet header
17260255736Sdavidch         */
17261255736Sdavidch        if (IS_MF_AFEX(sc)) {
17262255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17263255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17264255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17265255736Sdavidch        } else {
17266255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17267255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17268255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC,
17269255736Sdavidch                   IS_MF_SD(sc) ? 7 : 6);
17270255736Sdavidch        }
17271255736Sdavidch
17272255736Sdavidch        if (CHIP_IS_E3(sc)) {
17273255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17274255736Sdavidch                   NIG_REG_LLH1_MF_MODE :
17275255736Sdavidch                   NIG_REG_LLH_MF_MODE, IS_MF(sc));
17276255736Sdavidch        }
17277255736Sdavidch    }
17278255736Sdavidch    if (!CHIP_IS_E3(sc)) {
17279255736Sdavidch        REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17280255736Sdavidch    }
17281255736Sdavidch
17282255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17283255736Sdavidch        /* 0x2 disable mf_ov, 0x1 enable */
17284255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17285255736Sdavidch               (IS_MF_SD(sc) ? 0x1 : 0x2));
17286255736Sdavidch
17287255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17288255736Sdavidch            val = 0;
17289255736Sdavidch            switch (sc->devinfo.mf_info.mf_mode) {
17290255736Sdavidch            case MULTI_FUNCTION_SD:
17291255736Sdavidch                val = 1;
17292255736Sdavidch                break;
17293255736Sdavidch            case MULTI_FUNCTION_SI:
17294255736Sdavidch            case MULTI_FUNCTION_AFEX:
17295255736Sdavidch                val = 2;
17296255736Sdavidch                break;
17297255736Sdavidch            }
17298255736Sdavidch
17299255736Sdavidch            REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17300255736Sdavidch                        NIG_REG_LLH0_CLS_TYPE), val);
17301255736Sdavidch        }
17302255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17303255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17304255736Sdavidch        REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17305255736Sdavidch    }
17306255736Sdavidch
17307255736Sdavidch    /* If SPIO5 is set to generate interrupts, enable it for this port */
17308255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17309255736Sdavidch    if (val & MISC_SPIO_SPIO5) {
17310255736Sdavidch        uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17311255736Sdavidch                                    MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17312255736Sdavidch        val = REG_RD(sc, reg_addr);
17313255736Sdavidch        val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17314255736Sdavidch        REG_WR(sc, reg_addr, val);
17315255736Sdavidch    }
17316255736Sdavidch
17317255736Sdavidch    return (0);
17318255736Sdavidch}
17319255736Sdavidch
17320255736Sdavidchstatic uint32_t
17321255736Sdavidchbxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17322255736Sdavidch                       uint32_t         reg,
17323255736Sdavidch                       uint32_t         expected,
17324255736Sdavidch                       uint32_t         poll_count)
17325255736Sdavidch{
17326255736Sdavidch    uint32_t cur_cnt = poll_count;
17327255736Sdavidch    uint32_t val;
17328255736Sdavidch
17329255736Sdavidch    while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17330255736Sdavidch        DELAY(FLR_WAIT_INTERVAL);
17331255736Sdavidch    }
17332255736Sdavidch
17333255736Sdavidch    return (val);
17334255736Sdavidch}
17335255736Sdavidch
17336255736Sdavidchstatic int
17337255736Sdavidchbxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17338255736Sdavidch                              uint32_t         reg,
17339255736Sdavidch                              char             *msg,
17340255736Sdavidch                              uint32_t         poll_cnt)
17341255736Sdavidch{
17342255736Sdavidch    uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17343255736Sdavidch
17344255736Sdavidch    if (val != 0) {
17345255736Sdavidch        BLOGE(sc, "%s usage count=%d\n", msg, val);
17346255736Sdavidch        return (1);
17347255736Sdavidch    }
17348255736Sdavidch
17349255736Sdavidch    return (0);
17350255736Sdavidch}
17351255736Sdavidch
17352255736Sdavidch/* Common routines with VF FLR cleanup */
17353255736Sdavidchstatic uint32_t
17354255736Sdavidchbxe_flr_clnup_poll_count(struct bxe_softc *sc)
17355255736Sdavidch{
17356255736Sdavidch    /* adjust polling timeout */
17357255736Sdavidch    if (CHIP_REV_IS_EMUL(sc)) {
17358255736Sdavidch        return (FLR_POLL_CNT * 2000);
17359255736Sdavidch    }
17360255736Sdavidch
17361255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
17362255736Sdavidch        return (FLR_POLL_CNT * 120);
17363255736Sdavidch    }
17364255736Sdavidch
17365255736Sdavidch    return (FLR_POLL_CNT);
17366255736Sdavidch}
17367255736Sdavidch
17368255736Sdavidchstatic int
17369255736Sdavidchbxe_poll_hw_usage_counters(struct bxe_softc *sc,
17370255736Sdavidch                           uint32_t         poll_cnt)
17371255736Sdavidch{
17372255736Sdavidch    /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17373255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17374255736Sdavidch                                      CFC_REG_NUM_LCIDS_INSIDE_PF,
17375255736Sdavidch                                      "CFC PF usage counter timed out",
17376255736Sdavidch                                      poll_cnt)) {
17377255736Sdavidch        return (1);
17378255736Sdavidch    }
17379255736Sdavidch
17380255736Sdavidch    /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17381255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17382255736Sdavidch                                      DORQ_REG_PF_USAGE_CNT,
17383255736Sdavidch                                      "DQ PF usage counter timed out",
17384255736Sdavidch                                      poll_cnt)) {
17385255736Sdavidch        return (1);
17386255736Sdavidch    }
17387255736Sdavidch
17388255736Sdavidch    /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17389255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17390255736Sdavidch                                      QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17391255736Sdavidch                                      "QM PF usage counter timed out",
17392255736Sdavidch                                      poll_cnt)) {
17393255736Sdavidch        return (1);
17394255736Sdavidch    }
17395255736Sdavidch
17396255736Sdavidch    /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17397255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17398255736Sdavidch                                      TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17399255736Sdavidch                                      "Timers VNIC usage counter timed out",
17400255736Sdavidch                                      poll_cnt)) {
17401255736Sdavidch        return (1);
17402255736Sdavidch    }
17403255736Sdavidch
17404255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17405255736Sdavidch                                      TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17406255736Sdavidch                                      "Timers NUM_SCANS usage counter timed out",
17407255736Sdavidch                                      poll_cnt)) {
17408255736Sdavidch        return (1);
17409255736Sdavidch    }
17410255736Sdavidch
17411255736Sdavidch    /* Wait DMAE PF usage counter to zero */
17412255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17413255736Sdavidch                                      dmae_reg_go_c[INIT_DMAE_C(sc)],
17414255736Sdavidch                                      "DMAE dommand register timed out",
17415255736Sdavidch                                      poll_cnt)) {
17416255736Sdavidch        return (1);
17417255736Sdavidch    }
17418255736Sdavidch
17419255736Sdavidch    return (0);
17420255736Sdavidch}
17421255736Sdavidch
17422255736Sdavidch#define OP_GEN_PARAM(param)                                            \
17423255736Sdavidch    (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17424255736Sdavidch#define OP_GEN_TYPE(type)                                           \
17425255736Sdavidch    (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17426255736Sdavidch#define OP_GEN_AGG_VECT(index)                                             \
17427255736Sdavidch    (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17428255736Sdavidch
17429255736Sdavidchstatic int
17430255736Sdavidchbxe_send_final_clnup(struct bxe_softc *sc,
17431255736Sdavidch                     uint8_t          clnup_func,
17432255736Sdavidch                     uint32_t         poll_cnt)
17433255736Sdavidch{
17434255736Sdavidch    uint32_t op_gen_command = 0;
17435255736Sdavidch    uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17436255736Sdavidch                          CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17437255736Sdavidch    int ret = 0;
17438255736Sdavidch
17439255736Sdavidch    if (REG_RD(sc, comp_addr)) {
17440255736Sdavidch        BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17441255736Sdavidch        return (1);
17442255736Sdavidch    }
17443255736Sdavidch
17444255736Sdavidch    op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17445255736Sdavidch    op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17446255736Sdavidch    op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17447255736Sdavidch    op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17448255736Sdavidch
17449255736Sdavidch    BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17450255736Sdavidch    REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17451255736Sdavidch
17452255736Sdavidch    if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17453255736Sdavidch        BLOGE(sc, "FW final cleanup did not succeed\n");
17454255736Sdavidch        BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17455255736Sdavidch              (REG_RD(sc, comp_addr)));
17456255736Sdavidch        bxe_panic(sc, ("FLR cleanup failed\n"));
17457255736Sdavidch        return (1);
17458255736Sdavidch    }
17459255736Sdavidch
17460255736Sdavidch    /* Zero completion for nxt FLR */
17461255736Sdavidch    REG_WR(sc, comp_addr, 0);
17462255736Sdavidch
17463255736Sdavidch    return (ret);
17464255736Sdavidch}
17465255736Sdavidch
17466255736Sdavidchstatic void
17467255736Sdavidchbxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17468255736Sdavidch                       struct pbf_pN_buf_regs *regs,
17469255736Sdavidch                       uint32_t               poll_count)
17470255736Sdavidch{
17471255736Sdavidch    uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17472255736Sdavidch    uint32_t cur_cnt = poll_count;
17473255736Sdavidch
17474255736Sdavidch    crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
17475255736Sdavidch    crd = crd_start = REG_RD(sc, regs->crd);
17476255736Sdavidch    init_crd = REG_RD(sc, regs->init_crd);
17477255736Sdavidch
17478255736Sdavidch    BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
17479255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
17480255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
17481255736Sdavidch
17482255736Sdavidch    while ((crd != init_crd) &&
17483255736Sdavidch           ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
17484255736Sdavidch            (init_crd - crd_start))) {
17485255736Sdavidch        if (cur_cnt--) {
17486255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
17487255736Sdavidch            crd = REG_RD(sc, regs->crd);
17488255736Sdavidch            crd_freed = REG_RD(sc, regs->crd_freed);
17489255736Sdavidch        } else {
17490255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
17491255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
17492255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
17493255736Sdavidch            break;
17494255736Sdavidch        }
17495255736Sdavidch    }
17496255736Sdavidch
17497255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
17498255736Sdavidch          poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17499255736Sdavidch}
17500255736Sdavidch
17501255736Sdavidchstatic void
17502255736Sdavidchbxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
17503255736Sdavidch                       struct pbf_pN_cmd_regs *regs,
17504255736Sdavidch                       uint32_t               poll_count)
17505255736Sdavidch{
17506255736Sdavidch    uint32_t occup, to_free, freed, freed_start;
17507255736Sdavidch    uint32_t cur_cnt = poll_count;
17508255736Sdavidch
17509255736Sdavidch    occup = to_free = REG_RD(sc, regs->lines_occup);
17510255736Sdavidch    freed = freed_start = REG_RD(sc, regs->lines_freed);
17511255736Sdavidch
17512255736Sdavidch    BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17513255736Sdavidch    BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17514255736Sdavidch
17515255736Sdavidch    while (occup &&
17516255736Sdavidch           ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
17517255736Sdavidch        if (cur_cnt--) {
17518255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
17519255736Sdavidch            occup = REG_RD(sc, regs->lines_occup);
17520255736Sdavidch            freed = REG_RD(sc, regs->lines_freed);
17521255736Sdavidch        } else {
17522255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
17523255736Sdavidch            BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17524255736Sdavidch            BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17525255736Sdavidch            break;
17526255736Sdavidch        }
17527255736Sdavidch    }
17528255736Sdavidch
17529255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
17530255736Sdavidch          poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17531255736Sdavidch}
17532255736Sdavidch
17533255736Sdavidchstatic void
17534255736Sdavidchbxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
17535255736Sdavidch{
17536255736Sdavidch    struct pbf_pN_cmd_regs cmd_regs[] = {
17537255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
17538255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q0 :
17539255736Sdavidch            PBF_REG_P0_TQ_OCCUPANCY,
17540255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17541255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q0 :
17542255736Sdavidch            PBF_REG_P0_TQ_LINES_FREED_CNT},
17543255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
17544255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q1 :
17545255736Sdavidch            PBF_REG_P1_TQ_OCCUPANCY,
17546255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17547255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q1 :
17548255736Sdavidch            PBF_REG_P1_TQ_LINES_FREED_CNT},
17549255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
17550255736Sdavidch            PBF_REG_TQ_OCCUPANCY_LB_Q :
17551255736Sdavidch            PBF_REG_P4_TQ_OCCUPANCY,
17552255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17553255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
17554255736Sdavidch            PBF_REG_P4_TQ_LINES_FREED_CNT}
17555255736Sdavidch    };
17556255736Sdavidch
17557255736Sdavidch    struct pbf_pN_buf_regs buf_regs[] = {
17558255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
17559255736Sdavidch            PBF_REG_INIT_CRD_Q0 :
17560255736Sdavidch            PBF_REG_P0_INIT_CRD ,
17561255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17562255736Sdavidch            PBF_REG_CREDIT_Q0 :
17563255736Sdavidch            PBF_REG_P0_CREDIT,
17564255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17565255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
17566255736Sdavidch            PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
17567255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
17568255736Sdavidch            PBF_REG_INIT_CRD_Q1 :
17569255736Sdavidch            PBF_REG_P1_INIT_CRD,
17570255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17571255736Sdavidch            PBF_REG_CREDIT_Q1 :
17572255736Sdavidch            PBF_REG_P1_CREDIT,
17573255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17574255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
17575255736Sdavidch            PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
17576255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
17577255736Sdavidch            PBF_REG_INIT_CRD_LB_Q :
17578255736Sdavidch            PBF_REG_P4_INIT_CRD,
17579255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17580255736Sdavidch            PBF_REG_CREDIT_LB_Q :
17581255736Sdavidch            PBF_REG_P4_CREDIT,
17582255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17583255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
17584255736Sdavidch            PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
17585255736Sdavidch    };
17586255736Sdavidch
17587255736Sdavidch    int i;
17588255736Sdavidch
17589255736Sdavidch    /* Verify the command queues are flushed P0, P1, P4 */
17590255736Sdavidch    for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
17591255736Sdavidch        bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
17592255736Sdavidch    }
17593255736Sdavidch
17594255736Sdavidch    /* Verify the transmission buffers are flushed P0, P1, P4 */
17595255736Sdavidch    for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
17596255736Sdavidch        bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
17597255736Sdavidch    }
17598255736Sdavidch}
17599255736Sdavidch
17600255736Sdavidchstatic void
17601255736Sdavidchbxe_hw_enable_status(struct bxe_softc *sc)
17602255736Sdavidch{
17603255736Sdavidch    uint32_t val;
17604255736Sdavidch
17605255736Sdavidch    val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
17606255736Sdavidch    BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
17607255736Sdavidch
17608255736Sdavidch    val = REG_RD(sc, PBF_REG_DISABLE_PF);
17609255736Sdavidch    BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
17610255736Sdavidch
17611255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
17612255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
17613255736Sdavidch
17614255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
17615255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
17616255736Sdavidch
17617255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
17618255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
17619255736Sdavidch
17620255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
17621255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
17622255736Sdavidch
17623255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
17624255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
17625255736Sdavidch
17626255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
17627255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
17628255736Sdavidch}
17629255736Sdavidch
17630255736Sdavidchstatic int
17631255736Sdavidchbxe_pf_flr_clnup(struct bxe_softc *sc)
17632255736Sdavidch{
17633255736Sdavidch    uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
17634255736Sdavidch
17635255736Sdavidch    BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
17636255736Sdavidch
17637255736Sdavidch    /* Re-enable PF target read access */
17638255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
17639255736Sdavidch
17640255736Sdavidch    /* Poll HW usage counters */
17641255736Sdavidch    BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
17642255736Sdavidch    if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
17643255736Sdavidch        return (-1);
17644255736Sdavidch    }
17645255736Sdavidch
17646255736Sdavidch    /* Zero the igu 'trailing edge' and 'leading edge' */
17647255736Sdavidch
17648255736Sdavidch    /* Send the FW cleanup command */
17649255736Sdavidch    if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
17650255736Sdavidch        return (-1);
17651255736Sdavidch    }
17652255736Sdavidch
17653255736Sdavidch    /* ATC cleanup */
17654255736Sdavidch
17655255736Sdavidch    /* Verify TX hw is flushed */
17656255736Sdavidch    bxe_tx_hw_flushed(sc, poll_cnt);
17657255736Sdavidch
17658255736Sdavidch    /* Wait 100ms (not adjusted according to platform) */
17659255736Sdavidch    DELAY(100000);
17660255736Sdavidch
17661255736Sdavidch    /* Verify no pending pci transactions */
17662255736Sdavidch    if (bxe_is_pcie_pending(sc)) {
17663255736Sdavidch        BLOGE(sc, "PCIE Transactions still pending\n");
17664255736Sdavidch    }
17665255736Sdavidch
17666255736Sdavidch    /* Debug */
17667255736Sdavidch    bxe_hw_enable_status(sc);
17668255736Sdavidch
17669255736Sdavidch    /*
17670255736Sdavidch     * Master enable - Due to WB DMAE writes performed before this
17671255736Sdavidch     * register is re-initialized as part of the regular function init
17672255736Sdavidch     */
17673255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17674255736Sdavidch
17675255736Sdavidch    return (0);
17676255736Sdavidch}
17677255736Sdavidch
17678255736Sdavidchstatic int
17679255736Sdavidchbxe_init_hw_func(struct bxe_softc *sc)
17680255736Sdavidch{
17681255736Sdavidch    int port = SC_PORT(sc);
17682255736Sdavidch    int func = SC_FUNC(sc);
17683255736Sdavidch    int init_phase = PHASE_PF0 + func;
17684255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
17685255736Sdavidch    uint16_t cdu_ilt_start;
17686255736Sdavidch    uint32_t addr, val;
17687255736Sdavidch    uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
17688255736Sdavidch    int i, main_mem_width, rc;
17689255736Sdavidch
17690255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
17691255736Sdavidch
17692255736Sdavidch    /* FLR cleanup */
17693255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17694255736Sdavidch        rc = bxe_pf_flr_clnup(sc);
17695255736Sdavidch        if (rc) {
17696255736Sdavidch            BLOGE(sc, "FLR cleanup failed!\n");
17697255736Sdavidch            // XXX bxe_fw_dump(sc);
17698255736Sdavidch            // XXX bxe_idle_chk(sc);
17699255736Sdavidch            return (rc);
17700255736Sdavidch        }
17701255736Sdavidch    }
17702255736Sdavidch
17703255736Sdavidch    /* set MSI reconfigure capability */
17704255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
17705255736Sdavidch        addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
17706255736Sdavidch        val = REG_RD(sc, addr);
17707255736Sdavidch        val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
17708255736Sdavidch        REG_WR(sc, addr, val);
17709255736Sdavidch    }
17710255736Sdavidch
17711255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17712255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17713255736Sdavidch
17714255736Sdavidch    ilt = sc->ilt;
17715255736Sdavidch    cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
17716255736Sdavidch
17717255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
17718255736Sdavidch        ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
17719255736Sdavidch        ilt->lines[cdu_ilt_start + i].page_mapping =
17720255736Sdavidch            sc->context[i].vcxt_dma.paddr;
17721255736Sdavidch        ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
17722255736Sdavidch    }
17723255736Sdavidch    ecore_ilt_init_op(sc, INITOP_SET);
17724255736Sdavidch
17725295830Sdavidcs    /* Set NIC mode */
17726295830Sdavidcs    REG_WR(sc, PRS_REG_NIC_MODE, 1);
17727295830Sdavidcs    BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
17728255736Sdavidch
17729255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17730255736Sdavidch        uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
17731255736Sdavidch
17732255736Sdavidch        /* Turn on a single ISR mode in IGU if driver is going to use
17733255736Sdavidch         * INT#x or MSI
17734255736Sdavidch         */
17735255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
17736255736Sdavidch            pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
17737255736Sdavidch        }
17738255736Sdavidch
17739255736Sdavidch        /*
17740255736Sdavidch         * Timers workaround bug: function init part.
17741255736Sdavidch         * Need to wait 20msec after initializing ILT,
17742255736Sdavidch         * needed to make sure there are no requests in
17743255736Sdavidch         * one of the PXP internal queues with "old" ILT addresses
17744255736Sdavidch         */
17745255736Sdavidch        DELAY(20000);
17746255736Sdavidch
17747255736Sdavidch        /*
17748255736Sdavidch         * Master enable - Due to WB DMAE writes performed before this
17749255736Sdavidch         * register is re-initialized as part of the regular function
17750255736Sdavidch         * init
17751255736Sdavidch         */
17752255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17753255736Sdavidch        /* Enable the function in IGU */
17754255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
17755255736Sdavidch    }
17756255736Sdavidch
17757255736Sdavidch    sc->dmae_ready = 1;
17758255736Sdavidch
17759255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17760255736Sdavidch
17761255736Sdavidch    if (!CHIP_IS_E1x(sc))
17762255736Sdavidch        REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
17763255736Sdavidch
17764255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17765255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17766255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17767255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, init_phase);
17768255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17769255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17770255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17771255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17772255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17773255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17774255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17775255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17776255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17777255736Sdavidch
17778255736Sdavidch    if (!CHIP_IS_E1x(sc))
17779255736Sdavidch        REG_WR(sc, QM_REG_PF_EN, 1);
17780255736Sdavidch
17781255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17782255736Sdavidch        REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17783255736Sdavidch        REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17784255736Sdavidch        REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17785255736Sdavidch        REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17786255736Sdavidch    }
17787255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17788255736Sdavidch
17789255736Sdavidch    ecore_init_block(sc, BLOCK_TM, init_phase);
17790255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17791255736Sdavidch
17792255736Sdavidch    bxe_iov_init_dq(sc);
17793255736Sdavidch
17794255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17795255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17796255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17797255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17798255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17799255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17800255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17801255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17802255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17803255736Sdavidch    if (!CHIP_IS_E1x(sc))
17804255736Sdavidch        REG_WR(sc, PBF_REG_DISABLE_PF, 0);
17805255736Sdavidch
17806255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17807255736Sdavidch
17808255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17809255736Sdavidch
17810255736Sdavidch    if (!CHIP_IS_E1x(sc))
17811255736Sdavidch        REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
17812255736Sdavidch
17813255736Sdavidch    if (IS_MF(sc)) {
17814255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
17815255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
17816255736Sdavidch    }
17817255736Sdavidch
17818255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17819255736Sdavidch
17820255736Sdavidch    /* HC init per function */
17821255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
17822255736Sdavidch        if (CHIP_IS_E1H(sc)) {
17823255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17824255736Sdavidch
17825255736Sdavidch            REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17826255736Sdavidch            REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17827255736Sdavidch        }
17828255736Sdavidch        ecore_init_block(sc, BLOCK_HC, init_phase);
17829255736Sdavidch
17830255736Sdavidch    } else {
17831255736Sdavidch        int num_segs, sb_idx, prod_offset;
17832255736Sdavidch
17833255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17834255736Sdavidch
17835255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17836255736Sdavidch            REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
17837255736Sdavidch            REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
17838255736Sdavidch        }
17839255736Sdavidch
17840255736Sdavidch        ecore_init_block(sc, BLOCK_IGU, init_phase);
17841255736Sdavidch
17842255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17843255736Sdavidch            int dsb_idx = 0;
17844255736Sdavidch            /**
17845255736Sdavidch             * Producer memory:
17846255736Sdavidch             * E2 mode: address 0-135 match to the mapping memory;
17847255736Sdavidch             * 136 - PF0 default prod; 137 - PF1 default prod;
17848255736Sdavidch             * 138 - PF2 default prod; 139 - PF3 default prod;
17849255736Sdavidch             * 140 - PF0 attn prod;    141 - PF1 attn prod;
17850255736Sdavidch             * 142 - PF2 attn prod;    143 - PF3 attn prod;
17851255736Sdavidch             * 144-147 reserved.
17852255736Sdavidch             *
17853255736Sdavidch             * E1.5 mode - In backward compatible mode;
17854255736Sdavidch             * for non default SB; each even line in the memory
17855255736Sdavidch             * holds the U producer and each odd line hold
17856255736Sdavidch             * the C producer. The first 128 producers are for
17857255736Sdavidch             * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
17858255736Sdavidch             * producers are for the DSB for each PF.
17859255736Sdavidch             * Each PF has five segments: (the order inside each
17860255736Sdavidch             * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
17861255736Sdavidch             * 132-135 C prods; 136-139 X prods; 140-143 T prods;
17862255736Sdavidch             * 144-147 attn prods;
17863255736Sdavidch             */
17864255736Sdavidch            /* non-default-status-blocks */
17865255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17866255736Sdavidch                IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
17867255736Sdavidch            for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
17868255736Sdavidch                prod_offset = (sc->igu_base_sb + sb_idx) *
17869255736Sdavidch                    num_segs;
17870255736Sdavidch
17871255736Sdavidch                for (i = 0; i < num_segs; i++) {
17872255736Sdavidch                    addr = IGU_REG_PROD_CONS_MEMORY +
17873255736Sdavidch                            (prod_offset + i) * 4;
17874255736Sdavidch                    REG_WR(sc, addr, 0);
17875255736Sdavidch                }
17876255736Sdavidch                /* send consumer update with value 0 */
17877255736Sdavidch                bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
17878255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
17879255736Sdavidch                bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
17880255736Sdavidch            }
17881255736Sdavidch
17882255736Sdavidch            /* default-status-blocks */
17883255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17884255736Sdavidch                IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
17885255736Sdavidch
17886255736Sdavidch            if (CHIP_IS_MODE_4_PORT(sc))
17887255736Sdavidch                dsb_idx = SC_FUNC(sc);
17888255736Sdavidch            else
17889255736Sdavidch                dsb_idx = SC_VN(sc);
17890255736Sdavidch
17891255736Sdavidch            prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
17892255736Sdavidch                       IGU_BC_BASE_DSB_PROD + dsb_idx :
17893255736Sdavidch                       IGU_NORM_BASE_DSB_PROD + dsb_idx);
17894255736Sdavidch
17895255736Sdavidch            /*
17896255736Sdavidch             * igu prods come in chunks of E1HVN_MAX (4) -
17897255736Sdavidch             * does not matters what is the current chip mode
17898255736Sdavidch             */
17899255736Sdavidch            for (i = 0; i < (num_segs * E1HVN_MAX);
17900255736Sdavidch                 i += E1HVN_MAX) {
17901255736Sdavidch                addr = IGU_REG_PROD_CONS_MEMORY +
17902255736Sdavidch                            (prod_offset + i)*4;
17903255736Sdavidch                REG_WR(sc, addr, 0);
17904255736Sdavidch            }
17905255736Sdavidch            /* send consumer update with 0 */
17906255736Sdavidch            if (CHIP_INT_MODE_IS_BC(sc)) {
17907255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17908255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
17909255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17910255736Sdavidch                           CSTORM_ID, 0, IGU_INT_NOP, 1);
17911255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17912255736Sdavidch                           XSTORM_ID, 0, IGU_INT_NOP, 1);
17913255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17914255736Sdavidch                           TSTORM_ID, 0, IGU_INT_NOP, 1);
17915255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17916255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
17917255736Sdavidch            } else {
17918255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17919255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
17920255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17921255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
17922255736Sdavidch            }
17923255736Sdavidch            bxe_igu_clear_sb(sc, sc->igu_dsb_id);
17924255736Sdavidch
17925255736Sdavidch            /* !!! these should become driver const once
17926255736Sdavidch               rf-tool supports split-68 const */
17927255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
17928255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
17929255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
17930255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
17931255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
17932255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
17933255736Sdavidch        }
17934255736Sdavidch    }
17935255736Sdavidch
17936255736Sdavidch    /* Reset PCIE errors for debug */
17937255736Sdavidch    REG_WR(sc, 0x2114, 0xffffffff);
17938255736Sdavidch    REG_WR(sc, 0x2120, 0xffffffff);
17939255736Sdavidch
17940255736Sdavidch    if (CHIP_IS_E1x(sc)) {
17941255736Sdavidch        main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
17942255736Sdavidch        main_mem_base = HC_REG_MAIN_MEMORY +
17943255736Sdavidch                SC_PORT(sc) * (main_mem_size * 4);
17944255736Sdavidch        main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
17945255736Sdavidch        main_mem_width = 8;
17946255736Sdavidch
17947255736Sdavidch        val = REG_RD(sc, main_mem_prty_clr);
17948255736Sdavidch        if (val) {
17949255736Sdavidch            BLOGD(sc, DBG_LOAD,
17950255736Sdavidch                  "Parity errors in HC block during function init (0x%x)!\n",
17951255736Sdavidch                  val);
17952255736Sdavidch        }
17953255736Sdavidch
17954255736Sdavidch        /* Clear "false" parity errors in MSI-X table */
17955255736Sdavidch        for (i = main_mem_base;
17956255736Sdavidch             i < main_mem_base + main_mem_size * 4;
17957255736Sdavidch             i += main_mem_width) {
17958255736Sdavidch            bxe_read_dmae(sc, i, main_mem_width / 4);
17959255736Sdavidch            bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
17960255736Sdavidch                           i, main_mem_width / 4);
17961255736Sdavidch        }
17962255736Sdavidch        /* Clear HC parity attention */
17963255736Sdavidch        REG_RD(sc, main_mem_prty_clr);
17964255736Sdavidch    }
17965255736Sdavidch
17966255736Sdavidch#if 1
17967255736Sdavidch    /* Enable STORMs SP logging */
17968255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM +
17969255736Sdavidch           USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17970255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM +
17971255736Sdavidch           TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17972255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
17973255736Sdavidch           CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17974255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM +
17975255736Sdavidch           XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17976255736Sdavidch#endif
17977255736Sdavidch
17978255736Sdavidch    elink_phy_probe(&sc->link_params);
17979255736Sdavidch
17980255736Sdavidch    return (0);
17981255736Sdavidch}
17982255736Sdavidch
17983255736Sdavidchstatic void
17984255736Sdavidchbxe_link_reset(struct bxe_softc *sc)
17985255736Sdavidch{
17986255736Sdavidch    if (!BXE_NOMCP(sc)) {
17987284335Sdavidcs	bxe_acquire_phy_lock(sc);
17988255736Sdavidch        elink_lfa_reset(&sc->link_params, &sc->link_vars);
17989284335Sdavidcs	bxe_release_phy_lock(sc);
17990255736Sdavidch    } else {
17991255736Sdavidch        if (!CHIP_REV_IS_SLOW(sc)) {
17992255736Sdavidch            BLOGW(sc, "Bootcode is missing - cannot reset link\n");
17993255736Sdavidch        }
17994255736Sdavidch    }
17995255736Sdavidch}
17996255736Sdavidch
17997255736Sdavidchstatic void
17998255736Sdavidchbxe_reset_port(struct bxe_softc *sc)
17999255736Sdavidch{
18000255736Sdavidch    int port = SC_PORT(sc);
18001255736Sdavidch    uint32_t val;
18002255736Sdavidch
18003255736Sdavidch    /* reset physical Link */
18004255736Sdavidch    bxe_link_reset(sc);
18005255736Sdavidch
18006255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18007255736Sdavidch
18008255736Sdavidch    /* Do not rcv packets to BRB */
18009255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18010255736Sdavidch    /* Do not direct rcv packets that are not for MCP to the BRB */
18011255736Sdavidch    REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18012255736Sdavidch               NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18013255736Sdavidch
18014255736Sdavidch    /* Configure AEU */
18015255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18016255736Sdavidch
18017255736Sdavidch    DELAY(100000);
18018255736Sdavidch
18019255736Sdavidch    /* Check for BRB port occupancy */
18020255736Sdavidch    val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18021255736Sdavidch    if (val) {
18022255736Sdavidch        BLOGD(sc, DBG_LOAD,
18023255736Sdavidch              "BRB1 is not empty, %d blocks are occupied\n", val);
18024255736Sdavidch    }
18025255736Sdavidch
18026255736Sdavidch    /* TODO: Close Doorbell port? */
18027255736Sdavidch}
18028255736Sdavidch
18029255736Sdavidchstatic void
18030255736Sdavidchbxe_ilt_wr(struct bxe_softc *sc,
18031255736Sdavidch           uint32_t         index,
18032255736Sdavidch           bus_addr_t       addr)
18033255736Sdavidch{
18034255736Sdavidch    int reg;
18035255736Sdavidch    uint32_t wb_write[2];
18036255736Sdavidch
18037255736Sdavidch    if (CHIP_IS_E1(sc)) {
18038255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18039255736Sdavidch    } else {
18040255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18041255736Sdavidch    }
18042255736Sdavidch
18043255736Sdavidch    wb_write[0] = ONCHIP_ADDR1(addr);
18044255736Sdavidch    wb_write[1] = ONCHIP_ADDR2(addr);
18045255736Sdavidch    REG_WR_DMAE(sc, reg, wb_write, 2);
18046255736Sdavidch}
18047255736Sdavidch
18048255736Sdavidchstatic void
18049255736Sdavidchbxe_clear_func_ilt(struct bxe_softc *sc,
18050255736Sdavidch                   uint32_t         func)
18051255736Sdavidch{
18052255736Sdavidch    uint32_t i, base = FUNC_ILT_BASE(func);
18053255736Sdavidch    for (i = base; i < base + ILT_PER_FUNC; i++) {
18054255736Sdavidch        bxe_ilt_wr(sc, i, 0);
18055255736Sdavidch    }
18056255736Sdavidch}
18057255736Sdavidch
18058255736Sdavidchstatic void
18059255736Sdavidchbxe_reset_func(struct bxe_softc *sc)
18060255736Sdavidch{
18061255736Sdavidch    struct bxe_fastpath *fp;
18062255736Sdavidch    int port = SC_PORT(sc);
18063255736Sdavidch    int func = SC_FUNC(sc);
18064255736Sdavidch    int i;
18065255736Sdavidch
18066255736Sdavidch    /* Disable the function in the FW */
18067255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18068255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18069255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18070255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18071255736Sdavidch
18072255736Sdavidch    /* FP SBs */
18073255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
18074255736Sdavidch        fp = &sc->fp[i];
18075255736Sdavidch        REG_WR8(sc, BAR_CSTRORM_INTMEM +
18076255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18077255736Sdavidch                SB_DISABLED);
18078255736Sdavidch    }
18079255736Sdavidch
18080255736Sdavidch    /* SP SB */
18081255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18082255736Sdavidch            CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18083255736Sdavidch            SB_DISABLED);
18084255736Sdavidch
18085255736Sdavidch    for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18086255736Sdavidch        REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18087255736Sdavidch    }
18088255736Sdavidch
18089255736Sdavidch    /* Configure IGU */
18090255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18091255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18092255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18093255736Sdavidch    } else {
18094255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18095255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18096255736Sdavidch    }
18097255736Sdavidch
18098255736Sdavidch    if (CNIC_LOADED(sc)) {
18099255736Sdavidch        /* Disable Timer scan */
18100255736Sdavidch        REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18101255736Sdavidch        /*
18102255736Sdavidch         * Wait for at least 10ms and up to 2 second for the timers
18103255736Sdavidch         * scan to complete
18104255736Sdavidch         */
18105255736Sdavidch        for (i = 0; i < 200; i++) {
18106255736Sdavidch            DELAY(10000);
18107255736Sdavidch            if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18108255736Sdavidch                break;
18109255736Sdavidch        }
18110255736Sdavidch    }
18111255736Sdavidch
18112255736Sdavidch    /* Clear ILT */
18113255736Sdavidch    bxe_clear_func_ilt(sc, func);
18114255736Sdavidch
18115255736Sdavidch    /*
18116255736Sdavidch     * Timers workaround bug for E2: if this is vnic-3,
18117255736Sdavidch     * we need to set the entire ilt range for this timers.
18118255736Sdavidch     */
18119255736Sdavidch    if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18120255736Sdavidch        struct ilt_client_info ilt_cli;
18121255736Sdavidch        /* use dummy TM client */
18122255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18123255736Sdavidch        ilt_cli.start = 0;
18124255736Sdavidch        ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18125255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
18126255736Sdavidch
18127255736Sdavidch        ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18128255736Sdavidch    }
18129255736Sdavidch
18130255736Sdavidch    /* this assumes that reset_port() called before reset_func()*/
18131255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18132255736Sdavidch        bxe_pf_disable(sc);
18133255736Sdavidch    }
18134255736Sdavidch
18135255736Sdavidch    sc->dmae_ready = 0;
18136255736Sdavidch}
18137255736Sdavidch
18138255736Sdavidchstatic int
18139255736Sdavidchbxe_gunzip_init(struct bxe_softc *sc)
18140255736Sdavidch{
18141255736Sdavidch    return (0);
18142255736Sdavidch}
18143255736Sdavidch
18144255736Sdavidchstatic void
18145255736Sdavidchbxe_gunzip_end(struct bxe_softc *sc)
18146255736Sdavidch{
18147255736Sdavidch    return;
18148255736Sdavidch}
18149255736Sdavidch
18150255736Sdavidchstatic int
18151255736Sdavidchbxe_init_firmware(struct bxe_softc *sc)
18152255736Sdavidch{
18153255736Sdavidch    if (CHIP_IS_E1(sc)) {
18154255736Sdavidch        ecore_init_e1_firmware(sc);
18155255736Sdavidch        sc->iro_array = e1_iro_arr;
18156255736Sdavidch    } else if (CHIP_IS_E1H(sc)) {
18157255736Sdavidch        ecore_init_e1h_firmware(sc);
18158255736Sdavidch        sc->iro_array = e1h_iro_arr;
18159255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
18160255736Sdavidch        ecore_init_e2_firmware(sc);
18161255736Sdavidch        sc->iro_array = e2_iro_arr;
18162255736Sdavidch    } else {
18163255736Sdavidch        BLOGE(sc, "Unsupported chip revision\n");
18164255736Sdavidch        return (-1);
18165255736Sdavidch    }
18166255736Sdavidch
18167255736Sdavidch    return (0);
18168255736Sdavidch}
18169255736Sdavidch
18170255736Sdavidchstatic void
18171255736Sdavidchbxe_release_firmware(struct bxe_softc *sc)
18172255736Sdavidch{
18173255736Sdavidch    /* Do nothing */
18174255736Sdavidch    return;
18175255736Sdavidch}
18176255736Sdavidch
18177255736Sdavidchstatic int
18178255736Sdavidchecore_gunzip(struct bxe_softc *sc,
18179255736Sdavidch             const uint8_t    *zbuf,
18180255736Sdavidch             int              len)
18181255736Sdavidch{
18182255736Sdavidch    /* XXX : Implement... */
18183255736Sdavidch    BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18184255736Sdavidch    return (FALSE);
18185255736Sdavidch}
18186255736Sdavidch
18187255736Sdavidchstatic void
18188255736Sdavidchecore_reg_wr_ind(struct bxe_softc *sc,
18189255736Sdavidch                 uint32_t         addr,
18190255736Sdavidch                 uint32_t         val)
18191255736Sdavidch{
18192255736Sdavidch    bxe_reg_wr_ind(sc, addr, val);
18193255736Sdavidch}
18194255736Sdavidch
18195255736Sdavidchstatic void
18196255736Sdavidchecore_write_dmae_phys_len(struct bxe_softc *sc,
18197255736Sdavidch                          bus_addr_t       phys_addr,
18198255736Sdavidch                          uint32_t         addr,
18199255736Sdavidch                          uint32_t         len)
18200255736Sdavidch{
18201255736Sdavidch    bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18202255736Sdavidch}
18203255736Sdavidch
18204255736Sdavidchvoid
18205255736Sdavidchecore_storm_memset_struct(struct bxe_softc *sc,
18206255736Sdavidch                          uint32_t         addr,
18207255736Sdavidch                          size_t           size,
18208255736Sdavidch                          uint32_t         *data)
18209255736Sdavidch{
18210255736Sdavidch    uint8_t i;
18211255736Sdavidch    for (i = 0; i < size/4; i++) {
18212255736Sdavidch        REG_WR(sc, addr + (i * 4), data[i]);
18213255736Sdavidch    }
18214255736Sdavidch}
18215255736Sdavidch
18216292639Sdavidcs
18217292639Sdavidcs/*
18218292639Sdavidcs * character device - ioctl interface definitions
18219292639Sdavidcs */
18220292639Sdavidcs
18221292639Sdavidcs
18222292639Sdavidcs#include "bxe_dump.h"
18223292639Sdavidcs#include "bxe_ioctl.h"
18224292639Sdavidcs#include <sys/conf.h>
18225292639Sdavidcs
18226292639Sdavidcsstatic int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18227292639Sdavidcs                struct thread *td);
18228292639Sdavidcs
18229292639Sdavidcsstatic struct cdevsw bxe_cdevsw = {
18230292639Sdavidcs    .d_version = D_VERSION,
18231292639Sdavidcs    .d_ioctl = bxe_eioctl,
18232292639Sdavidcs    .d_name = "bxecnic",
18233292639Sdavidcs};
18234292639Sdavidcs
18235292639Sdavidcs#define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18236292639Sdavidcs
18237292639Sdavidcs
18238292639Sdavidcs#define DUMP_ALL_PRESETS        0x1FFF
18239292639Sdavidcs#define DUMP_MAX_PRESETS        13
18240292639Sdavidcs#define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18241292639Sdavidcs#define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18242292639Sdavidcs#define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18243292639Sdavidcs#define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18244292639Sdavidcs#define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18245292639Sdavidcs
18246292639Sdavidcs#define IS_REG_IN_PRESET(presets, idx)  \
18247292639Sdavidcs                ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18248292639Sdavidcs
18249292639Sdavidcs
18250292639Sdavidcsstatic int
18251292639Sdavidcsbxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18252292639Sdavidcs{
18253292639Sdavidcs    if (CHIP_IS_E1(sc))
18254292639Sdavidcs        return dump_num_registers[0][preset-1];
18255292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18256292639Sdavidcs        return dump_num_registers[1][preset-1];
18257292639Sdavidcs    else if (CHIP_IS_E2(sc))
18258292639Sdavidcs        return dump_num_registers[2][preset-1];
18259292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18260292639Sdavidcs        return dump_num_registers[3][preset-1];
18261292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18262292639Sdavidcs        return dump_num_registers[4][preset-1];
18263292639Sdavidcs    else
18264292639Sdavidcs        return 0;
18265292639Sdavidcs}
18266292639Sdavidcs
18267292639Sdavidcsstatic int
18268292639Sdavidcsbxe_get_total_regs_len32(struct bxe_softc *sc)
18269292639Sdavidcs{
18270292639Sdavidcs    uint32_t preset_idx;
18271292639Sdavidcs    int regdump_len32 = 0;
18272292639Sdavidcs
18273292639Sdavidcs
18274292639Sdavidcs    /* Calculate the total preset regs length */
18275292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18276292639Sdavidcs        regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18277292639Sdavidcs    }
18278292639Sdavidcs
18279292639Sdavidcs    return regdump_len32;
18280292639Sdavidcs}
18281292639Sdavidcs
18282292639Sdavidcsstatic const uint32_t *
18283292639Sdavidcs__bxe_get_page_addr_ar(struct bxe_softc *sc)
18284292639Sdavidcs{
18285292639Sdavidcs    if (CHIP_IS_E2(sc))
18286292639Sdavidcs        return page_vals_e2;
18287292639Sdavidcs    else if (CHIP_IS_E3(sc))
18288292639Sdavidcs        return page_vals_e3;
18289292639Sdavidcs    else
18290292639Sdavidcs        return NULL;
18291292639Sdavidcs}
18292292639Sdavidcs
18293292639Sdavidcsstatic uint32_t
18294292639Sdavidcs__bxe_get_page_reg_num(struct bxe_softc *sc)
18295292639Sdavidcs{
18296292639Sdavidcs    if (CHIP_IS_E2(sc))
18297292639Sdavidcs        return PAGE_MODE_VALUES_E2;
18298292639Sdavidcs    else if (CHIP_IS_E3(sc))
18299292639Sdavidcs        return PAGE_MODE_VALUES_E3;
18300292639Sdavidcs    else
18301292639Sdavidcs        return 0;
18302292639Sdavidcs}
18303292639Sdavidcs
18304292639Sdavidcsstatic const uint32_t *
18305292639Sdavidcs__bxe_get_page_write_ar(struct bxe_softc *sc)
18306292639Sdavidcs{
18307292639Sdavidcs    if (CHIP_IS_E2(sc))
18308292639Sdavidcs        return page_write_regs_e2;
18309292639Sdavidcs    else if (CHIP_IS_E3(sc))
18310292639Sdavidcs        return page_write_regs_e3;
18311292639Sdavidcs    else
18312292639Sdavidcs        return NULL;
18313292639Sdavidcs}
18314292639Sdavidcs
18315292639Sdavidcsstatic uint32_t
18316292639Sdavidcs__bxe_get_page_write_num(struct bxe_softc *sc)
18317292639Sdavidcs{
18318292639Sdavidcs    if (CHIP_IS_E2(sc))
18319292639Sdavidcs        return PAGE_WRITE_REGS_E2;
18320292639Sdavidcs    else if (CHIP_IS_E3(sc))
18321292639Sdavidcs        return PAGE_WRITE_REGS_E3;
18322292639Sdavidcs    else
18323292639Sdavidcs        return 0;
18324292639Sdavidcs}
18325292639Sdavidcs
18326292639Sdavidcsstatic const struct reg_addr *
18327292639Sdavidcs__bxe_get_page_read_ar(struct bxe_softc *sc)
18328292639Sdavidcs{
18329292639Sdavidcs    if (CHIP_IS_E2(sc))
18330292639Sdavidcs        return page_read_regs_e2;
18331292639Sdavidcs    else if (CHIP_IS_E3(sc))
18332292639Sdavidcs        return page_read_regs_e3;
18333292639Sdavidcs    else
18334292639Sdavidcs        return NULL;
18335292639Sdavidcs}
18336292639Sdavidcs
18337292639Sdavidcsstatic uint32_t
18338292639Sdavidcs__bxe_get_page_read_num(struct bxe_softc *sc)
18339292639Sdavidcs{
18340292639Sdavidcs    if (CHIP_IS_E2(sc))
18341292639Sdavidcs        return PAGE_READ_REGS_E2;
18342292639Sdavidcs    else if (CHIP_IS_E3(sc))
18343292639Sdavidcs        return PAGE_READ_REGS_E3;
18344292639Sdavidcs    else
18345292639Sdavidcs        return 0;
18346292639Sdavidcs}
18347292639Sdavidcs
18348292639Sdavidcsstatic bool
18349292639Sdavidcsbxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18350292639Sdavidcs{
18351292639Sdavidcs    if (CHIP_IS_E1(sc))
18352292639Sdavidcs        return IS_E1_REG(reg_info->chips);
18353292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18354292639Sdavidcs        return IS_E1H_REG(reg_info->chips);
18355292639Sdavidcs    else if (CHIP_IS_E2(sc))
18356292639Sdavidcs        return IS_E2_REG(reg_info->chips);
18357292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18358292639Sdavidcs        return IS_E3A0_REG(reg_info->chips);
18359292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18360292639Sdavidcs        return IS_E3B0_REG(reg_info->chips);
18361292639Sdavidcs    else
18362292639Sdavidcs        return 0;
18363292639Sdavidcs}
18364292639Sdavidcs
18365292639Sdavidcsstatic bool
18366292639Sdavidcsbxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18367292639Sdavidcs{
18368292639Sdavidcs    if (CHIP_IS_E1(sc))
18369292639Sdavidcs        return IS_E1_REG(wreg_info->chips);
18370292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18371292639Sdavidcs        return IS_E1H_REG(wreg_info->chips);
18372292639Sdavidcs    else if (CHIP_IS_E2(sc))
18373292639Sdavidcs        return IS_E2_REG(wreg_info->chips);
18374292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18375292639Sdavidcs        return IS_E3A0_REG(wreg_info->chips);
18376292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18377292639Sdavidcs        return IS_E3B0_REG(wreg_info->chips);
18378292639Sdavidcs    else
18379292639Sdavidcs        return 0;
18380292639Sdavidcs}
18381292639Sdavidcs
18382292639Sdavidcs/**
18383292639Sdavidcs * bxe_read_pages_regs - read "paged" registers
18384292639Sdavidcs *
18385292639Sdavidcs * @bp          device handle
18386292639Sdavidcs * @p           output buffer
18387292639Sdavidcs *
18388292639Sdavidcs * Reads "paged" memories: memories that may only be read by first writing to a
18389292639Sdavidcs * specific address ("write address") and then reading from a specific address
18390292639Sdavidcs * ("read address"). There may be more than one write address per "page" and
18391292639Sdavidcs * more than one read address per write address.
18392292639Sdavidcs */
18393292639Sdavidcsstatic void
18394292639Sdavidcsbxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18395292639Sdavidcs{
18396292639Sdavidcs    uint32_t i, j, k, n;
18397292639Sdavidcs
18398292639Sdavidcs    /* addresses of the paged registers */
18399292639Sdavidcs    const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18400292639Sdavidcs    /* number of paged registers */
18401292639Sdavidcs    int num_pages = __bxe_get_page_reg_num(sc);
18402292639Sdavidcs    /* write addresses */
18403292639Sdavidcs    const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18404292639Sdavidcs    /* number of write addresses */
18405292639Sdavidcs    int write_num = __bxe_get_page_write_num(sc);
18406292639Sdavidcs    /* read addresses info */
18407292639Sdavidcs    const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18408292639Sdavidcs    /* number of read addresses */
18409292639Sdavidcs    int read_num = __bxe_get_page_read_num(sc);
18410292639Sdavidcs    uint32_t addr, size;
18411292639Sdavidcs
18412292639Sdavidcs    for (i = 0; i < num_pages; i++) {
18413292639Sdavidcs        for (j = 0; j < write_num; j++) {
18414292639Sdavidcs            REG_WR(sc, write_addr[j], page_addr[i]);
18415292639Sdavidcs
18416292639Sdavidcs            for (k = 0; k < read_num; k++) {
18417292639Sdavidcs                if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18418292639Sdavidcs                    size = read_addr[k].size;
18419292639Sdavidcs                    for (n = 0; n < size; n++) {
18420292639Sdavidcs                        addr = read_addr[k].addr + n*4;
18421292639Sdavidcs                        *p++ = REG_RD(sc, addr);
18422292639Sdavidcs                    }
18423292639Sdavidcs                }
18424292639Sdavidcs            }
18425292639Sdavidcs        }
18426292639Sdavidcs    }
18427292639Sdavidcs    return;
18428292639Sdavidcs}
18429292639Sdavidcs
18430292639Sdavidcs
18431292639Sdavidcsstatic int
18432292639Sdavidcsbxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18433292639Sdavidcs{
18434292639Sdavidcs    uint32_t i, j, addr;
18435292639Sdavidcs    const struct wreg_addr *wreg_addr_p = NULL;
18436292639Sdavidcs
18437292639Sdavidcs    if (CHIP_IS_E1(sc))
18438292639Sdavidcs        wreg_addr_p = &wreg_addr_e1;
18439292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18440292639Sdavidcs        wreg_addr_p = &wreg_addr_e1h;
18441292639Sdavidcs    else if (CHIP_IS_E2(sc))
18442292639Sdavidcs        wreg_addr_p = &wreg_addr_e2;
18443292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18444292639Sdavidcs        wreg_addr_p = &wreg_addr_e3;
18445292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18446292639Sdavidcs        wreg_addr_p = &wreg_addr_e3b0;
18447292639Sdavidcs    else
18448292639Sdavidcs        return (-1);
18449292639Sdavidcs
18450292639Sdavidcs    /* Read the idle_chk registers */
18451292639Sdavidcs    for (i = 0; i < IDLE_REGS_COUNT; i++) {
18452292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18453292639Sdavidcs            IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18454292639Sdavidcs            for (j = 0; j < idle_reg_addrs[i].size; j++)
18455292639Sdavidcs                *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18456292639Sdavidcs        }
18457292639Sdavidcs    }
18458292639Sdavidcs
18459292639Sdavidcs    /* Read the regular registers */
18460292639Sdavidcs    for (i = 0; i < REGS_COUNT; i++) {
18461292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18462292639Sdavidcs            IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18463292639Sdavidcs            for (j = 0; j < reg_addrs[i].size; j++)
18464292639Sdavidcs                *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18465292639Sdavidcs        }
18466292639Sdavidcs    }
18467292639Sdavidcs
18468292639Sdavidcs    /* Read the CAM registers */
18469292639Sdavidcs    if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18470292639Sdavidcs        IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18471292639Sdavidcs        for (i = 0; i < wreg_addr_p->size; i++) {
18472292639Sdavidcs            *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
18473292639Sdavidcs
18474292639Sdavidcs            /* In case of wreg_addr register, read additional
18475292639Sdavidcs               registers from read_regs array
18476292639Sdavidcs             */
18477292639Sdavidcs            for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
18478292639Sdavidcs                addr = *(wreg_addr_p->read_regs);
18479292639Sdavidcs                *p++ = REG_RD(sc, addr + j*4);
18480292639Sdavidcs            }
18481292639Sdavidcs        }
18482292639Sdavidcs    }
18483292639Sdavidcs
18484292639Sdavidcs    /* Paged registers are supported in E2 & E3 only */
18485292639Sdavidcs    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
18486292639Sdavidcs        /* Read "paged" registers */
18487292639Sdavidcs        bxe_read_pages_regs(sc, p, preset);
18488292639Sdavidcs    }
18489292639Sdavidcs
18490292639Sdavidcs    return 0;
18491292639Sdavidcs}
18492292639Sdavidcs
18493298294Sdavidcsint
18494295823Sdavidcsbxe_grc_dump(struct bxe_softc *sc)
18495292639Sdavidcs{
18496292639Sdavidcs    int rval = 0;
18497292639Sdavidcs    uint32_t preset_idx;
18498292639Sdavidcs    uint8_t *buf;
18499292639Sdavidcs    uint32_t size;
18500292639Sdavidcs    struct  dump_header *d_hdr;
18501298294Sdavidcs    uint32_t i;
18502298294Sdavidcs    uint32_t reg_val;
18503298294Sdavidcs    uint32_t reg_addr;
18504298294Sdavidcs    uint32_t cmd_offset;
18505298294Sdavidcs    int context_size;
18506298294Sdavidcs    int allocated;
18507298294Sdavidcs    struct ecore_ilt *ilt = SC_ILT(sc);
18508298294Sdavidcs    struct bxe_fastpath *fp;
18509298294Sdavidcs    struct ilt_client_info *ilt_cli;
18510298294Sdavidcs    int grc_dump_size;
18511295823Sdavidcs
18512298294Sdavidcs
18513298294Sdavidcs    if (sc->grcdump_done || sc->grcdump_started)
18514295823Sdavidcs	return (rval);
18515292639Sdavidcs
18516298294Sdavidcs    sc->grcdump_started = 1;
18517298294Sdavidcs    BLOGI(sc, "Started collecting grcdump\n");
18518298294Sdavidcs
18519298294Sdavidcs    grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18520298294Sdavidcs                sizeof(struct  dump_header);
18521298294Sdavidcs
18522298294Sdavidcs    sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
18523298294Sdavidcs
18524298294Sdavidcs    if (sc->grc_dump == NULL) {
18525298294Sdavidcs        BLOGW(sc, "Unable to allocate memory for grcdump collection\n");
18526298294Sdavidcs        return(ENOMEM);
18527298294Sdavidcs    }
18528298294Sdavidcs
18529298294Sdavidcs
18530298294Sdavidcs
18531298294Sdavidcs    /* Disable parity attentions as long as following dump may
18532298294Sdavidcs     * cause false alarms by reading never written registers. We
18533298294Sdavidcs     * will re-enable parity attentions right after the dump.
18534298294Sdavidcs     */
18535298294Sdavidcs
18536298294Sdavidcs    /* Disable parity on path 0 */
18537298294Sdavidcs    bxe_pretend_func(sc, 0);
18538298294Sdavidcs
18539292639Sdavidcs    ecore_disable_blocks_parity(sc);
18540292639Sdavidcs
18541298294Sdavidcs    /* Disable parity on path 1 */
18542298294Sdavidcs    bxe_pretend_func(sc, 1);
18543298294Sdavidcs    ecore_disable_blocks_parity(sc);
18544298294Sdavidcs
18545298294Sdavidcs    /* Return to current function */
18546298294Sdavidcs    bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18547298294Sdavidcs
18548295823Sdavidcs    buf = sc->grc_dump;
18549295823Sdavidcs    d_hdr = sc->grc_dump;
18550292639Sdavidcs
18551292639Sdavidcs    d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
18552292639Sdavidcs    d_hdr->version = BNX2X_DUMP_VERSION;
18553292639Sdavidcs    d_hdr->preset = DUMP_ALL_PRESETS;
18554292639Sdavidcs
18555292639Sdavidcs    if (CHIP_IS_E1(sc)) {
18556292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1;
18557292639Sdavidcs    } else if (CHIP_IS_E1H(sc)) {
18558292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1H;
18559292639Sdavidcs    } else if (CHIP_IS_E2(sc)) {
18560292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E2 |
18561292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18562292639Sdavidcs    } else if (CHIP_IS_E3A0(sc)) {
18563292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
18564292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18565292639Sdavidcs    } else if (CHIP_IS_E3B0(sc)) {
18566292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
18567292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18568292639Sdavidcs    }
18569292639Sdavidcs
18570292639Sdavidcs    buf += sizeof(struct  dump_header);
18571292639Sdavidcs
18572292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18573292639Sdavidcs
18574292639Sdavidcs        /* Skip presets with IOR */
18575292639Sdavidcs        if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
18576292639Sdavidcs            (preset_idx == 11))
18577292639Sdavidcs            continue;
18578292639Sdavidcs
18579298294Sdavidcs        rval = bxe_get_preset_regs(sc, (uint32_t *)buf, preset_idx);
18580292639Sdavidcs
18581292639Sdavidcs	if (rval)
18582292639Sdavidcs            break;
18583292639Sdavidcs
18584292639Sdavidcs        size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
18585292639Sdavidcs
18586292639Sdavidcs        buf += size;
18587292639Sdavidcs    }
18588292639Sdavidcs
18589298294Sdavidcs    bxe_pretend_func(sc, 0);
18590292639Sdavidcs    ecore_clear_blocks_parity(sc);
18591292639Sdavidcs    ecore_enable_blocks_parity(sc);
18592292639Sdavidcs
18593298294Sdavidcs    bxe_pretend_func(sc, 1);
18594298294Sdavidcs    ecore_clear_blocks_parity(sc);
18595298294Sdavidcs    ecore_enable_blocks_parity(sc);
18596298294Sdavidcs
18597298294Sdavidcs    /* Return to current function */
18598298294Sdavidcs    bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18599298294Sdavidcs
18600298294Sdavidcs
18601298294Sdavidcs    context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
18602298294Sdavidcs    for (i = 0, allocated = 0; allocated < context_size; i++) {
18603298294Sdavidcs
18604298294Sdavidcs        BLOGI(sc, "cdu_context i %d paddr %#jx vaddr %p size 0x%zx\n", i,
18605298294Sdavidcs            (uintmax_t)sc->context[i].vcxt_dma.paddr,
18606298294Sdavidcs            sc->context[i].vcxt_dma.vaddr,
18607298294Sdavidcs            sc->context[i].size);
18608298294Sdavidcs        allocated += sc->context[i].size;
18609298294Sdavidcs    }
18610298294Sdavidcs    BLOGI(sc, "fw stats start_paddr %#jx end_paddr %#jx vaddr %p size 0x%x\n",
18611298294Sdavidcs        (uintmax_t)sc->fw_stats_req_mapping,
18612298294Sdavidcs        (uintmax_t)sc->fw_stats_data_mapping,
18613298294Sdavidcs        sc->fw_stats_req, (sc->fw_stats_req_size + sc->fw_stats_data_size));
18614298294Sdavidcs    BLOGI(sc, "def_status_block paddr %p vaddr %p size 0x%zx\n",
18615298294Sdavidcs        (void *)sc->def_sb_dma.paddr, sc->def_sb,
18616298294Sdavidcs        sizeof(struct host_sp_status_block));
18617298294Sdavidcs    BLOGI(sc, "event_queue paddr %#jx vaddr %p size 0x%x\n",
18618298294Sdavidcs        (uintmax_t)sc->eq_dma.paddr, sc->eq_dma.vaddr, BCM_PAGE_SIZE);
18619298294Sdavidcs    BLOGI(sc, "slow path paddr %#jx vaddr %p size 0x%zx\n",
18620298294Sdavidcs        (uintmax_t)sc->sp_dma.paddr, sc->sp_dma.vaddr,
18621298294Sdavidcs        sizeof(struct bxe_slowpath));
18622298294Sdavidcs    BLOGI(sc, "slow path queue paddr %#jx vaddr %p size 0x%x\n",
18623298294Sdavidcs        (uintmax_t)sc->spq_dma.paddr, sc->spq_dma.vaddr, BCM_PAGE_SIZE);
18624298294Sdavidcs    BLOGI(sc, "fw_buf paddr %#jx vaddr %p size 0x%x\n",
18625298294Sdavidcs        (uintmax_t)sc->gz_buf_dma.paddr, sc->gz_buf_dma.vaddr,
18626298294Sdavidcs        FW_BUF_SIZE);
18627298294Sdavidcs    for (i = 0; i < sc->num_queues; i++) {
18628298294Sdavidcs        fp = &sc->fp[i];
18629298294Sdavidcs        BLOGI(sc, "FP status block fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18630298294Sdavidcs            (uintmax_t)fp->sb_dma.paddr, fp->sb_dma.vaddr,
18631298294Sdavidcs            sizeof(union bxe_host_hc_status_block));
18632298294Sdavidcs        BLOGI(sc, "TX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18633298294Sdavidcs            (uintmax_t)fp->tx_dma.paddr, fp->tx_dma.vaddr,
18634298294Sdavidcs            (BCM_PAGE_SIZE * TX_BD_NUM_PAGES));
18635298294Sdavidcs        BLOGI(sc, "RX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18636298294Sdavidcs            (uintmax_t)fp->rx_dma.paddr, fp->rx_dma.vaddr,
18637298294Sdavidcs            (BCM_PAGE_SIZE * RX_BD_NUM_PAGES));
18638298294Sdavidcs        BLOGI(sc, "RX RCQ CHAIN fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18639298294Sdavidcs            (uintmax_t)fp->rcq_dma.paddr, fp->rcq_dma.vaddr,
18640298294Sdavidcs            (BCM_PAGE_SIZE * RCQ_NUM_PAGES));
18641298294Sdavidcs        BLOGI(sc, "RX SGE CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18642298294Sdavidcs            (uintmax_t)fp->rx_sge_dma.paddr, fp->rx_sge_dma.vaddr,
18643298294Sdavidcs            (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES));
18644298294Sdavidcs    }
18645298294Sdavidcs
18646298294Sdavidcs    ilt_cli = &ilt->clients[1];
18647298294Sdavidcs    for (i = ilt_cli->start; i <= ilt_cli->end; i++) {
18648298294Sdavidcs        BLOGI(sc, "ECORE_ILT paddr %#jx vaddr %p size 0x%x\n",
18649298294Sdavidcs            (uintmax_t)(((struct bxe_dma *)((&ilt->lines[i])->page))->paddr),
18650298294Sdavidcs            ((struct bxe_dma *)((&ilt->lines[i])->page))->vaddr, BCM_PAGE_SIZE);
18651298294Sdavidcs    }
18652298294Sdavidcs
18653298294Sdavidcs
18654298294Sdavidcs    cmd_offset = DMAE_REG_CMD_MEM;
18655298294Sdavidcs    for (i = 0; i < 224; i++) {
18656298294Sdavidcs        reg_addr = (cmd_offset +(i * 4));
18657298294Sdavidcs        reg_val = REG_RD(sc, reg_addr);
18658298294Sdavidcs        BLOGI(sc, "DMAE_REG_CMD_MEM i=%d reg_addr 0x%x reg_val 0x%08x\n",i,
18659298294Sdavidcs            reg_addr, reg_val);
18660298294Sdavidcs    }
18661298294Sdavidcs
18662298294Sdavidcs
18663298294Sdavidcs    BLOGI(sc, "Collection of grcdump done\n");
18664292639Sdavidcs    sc->grcdump_done = 1;
18665292639Sdavidcs    return(rval);
18666292639Sdavidcs}
18667292639Sdavidcs
18668292639Sdavidcsstatic int
18669292639Sdavidcsbxe_add_cdev(struct bxe_softc *sc)
18670292639Sdavidcs{
18671297884Sdavidcs    sc->eeprom = malloc(BXE_EEPROM_MAX_DATA_LEN, M_DEVBUF, M_NOWAIT);
18672297884Sdavidcs
18673297884Sdavidcs    if (sc->eeprom == NULL) {
18674297884Sdavidcs        BLOGW(sc, "Unable to alloc for eeprom size buffer\n");
18675297884Sdavidcs        return (-1);
18676297884Sdavidcs    }
18677297884Sdavidcs
18678292639Sdavidcs    sc->ioctl_dev = make_dev(&bxe_cdevsw,
18679292639Sdavidcs                            sc->ifp->if_dunit,
18680292639Sdavidcs                            UID_ROOT,
18681292639Sdavidcs                            GID_WHEEL,
18682292639Sdavidcs                            0600,
18683292639Sdavidcs                            "%s",
18684292639Sdavidcs                            if_name(sc->ifp));
18685292639Sdavidcs
18686292639Sdavidcs    if (sc->ioctl_dev == NULL) {
18687297884Sdavidcs        free(sc->eeprom, M_DEVBUF);
18688297884Sdavidcs        sc->eeprom = NULL;
18689292639Sdavidcs        return (-1);
18690292639Sdavidcs    }
18691292639Sdavidcs
18692292639Sdavidcs    sc->ioctl_dev->si_drv1 = sc;
18693292639Sdavidcs
18694292639Sdavidcs    return (0);
18695292639Sdavidcs}
18696292639Sdavidcs
18697292639Sdavidcsstatic void
18698292639Sdavidcsbxe_del_cdev(struct bxe_softc *sc)
18699292639Sdavidcs{
18700292639Sdavidcs    if (sc->ioctl_dev != NULL)
18701292639Sdavidcs        destroy_dev(sc->ioctl_dev);
18702292639Sdavidcs
18703297884Sdavidcs    if (sc->eeprom != NULL) {
18704297884Sdavidcs        free(sc->eeprom, M_DEVBUF);
18705297884Sdavidcs        sc->eeprom = NULL;
18706297884Sdavidcs    }
18707298294Sdavidcs    sc->ioctl_dev = NULL;
18708297884Sdavidcs
18709292639Sdavidcs    return;
18710292639Sdavidcs}
18711292639Sdavidcs
18712297884Sdavidcsstatic bool bxe_is_nvram_accessible(struct bxe_softc *sc)
18713297884Sdavidcs{
18714297884Sdavidcs
18715297884Sdavidcs    if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
18716297884Sdavidcs        return FALSE;
18717297884Sdavidcs
18718297884Sdavidcs    return TRUE;
18719297884Sdavidcs}
18720297884Sdavidcs
18721297884Sdavidcs
18722292639Sdavidcsstatic int
18723297884Sdavidcsbxe_wr_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18724297884Sdavidcs{
18725297884Sdavidcs    int rval = 0;
18726297884Sdavidcs
18727297884Sdavidcs    if(!bxe_is_nvram_accessible(sc)) {
18728297884Sdavidcs        BLOGW(sc, "Cannot access eeprom when interface is down\n");
18729297884Sdavidcs        return (-EAGAIN);
18730297884Sdavidcs    }
18731297884Sdavidcs    rval = bxe_nvram_write(sc, offset, (uint8_t *)data, len);
18732297884Sdavidcs
18733297884Sdavidcs
18734297884Sdavidcs   return (rval);
18735297884Sdavidcs}
18736297884Sdavidcs
18737297884Sdavidcsstatic int
18738297884Sdavidcsbxe_rd_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18739297884Sdavidcs{
18740297884Sdavidcs    int rval = 0;
18741297884Sdavidcs
18742297884Sdavidcs    if(!bxe_is_nvram_accessible(sc)) {
18743297884Sdavidcs        BLOGW(sc, "Cannot access eeprom when interface is down\n");
18744297884Sdavidcs        return (-EAGAIN);
18745297884Sdavidcs    }
18746297884Sdavidcs    rval = bxe_nvram_read(sc, offset, (uint8_t *)data, len);
18747297884Sdavidcs
18748297884Sdavidcs   return (rval);
18749297884Sdavidcs}
18750297884Sdavidcs
18751297884Sdavidcsstatic int
18752297884Sdavidcsbxe_eeprom_rd_wr(struct bxe_softc *sc, bxe_eeprom_t *eeprom)
18753297884Sdavidcs{
18754297884Sdavidcs    int rval = 0;
18755297884Sdavidcs
18756297884Sdavidcs    switch (eeprom->eeprom_cmd) {
18757297884Sdavidcs
18758297884Sdavidcs    case BXE_EEPROM_CMD_SET_EEPROM:
18759297884Sdavidcs
18760297884Sdavidcs        rval = copyin(eeprom->eeprom_data, sc->eeprom,
18761297884Sdavidcs                       eeprom->eeprom_data_len);
18762297884Sdavidcs
18763297884Sdavidcs        if (rval)
18764297884Sdavidcs            break;
18765297884Sdavidcs
18766297884Sdavidcs        rval = bxe_wr_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18767297884Sdavidcs                       eeprom->eeprom_data_len);
18768297884Sdavidcs        break;
18769297884Sdavidcs
18770297884Sdavidcs    case BXE_EEPROM_CMD_GET_EEPROM:
18771297884Sdavidcs
18772297884Sdavidcs        rval = bxe_rd_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18773297884Sdavidcs                       eeprom->eeprom_data_len);
18774297884Sdavidcs
18775297884Sdavidcs        if (rval) {
18776297884Sdavidcs            break;
18777297884Sdavidcs        }
18778297884Sdavidcs
18779297884Sdavidcs        rval = copyout(sc->eeprom, eeprom->eeprom_data,
18780297884Sdavidcs                       eeprom->eeprom_data_len);
18781297884Sdavidcs        break;
18782297884Sdavidcs
18783297884Sdavidcs    default:
18784297884Sdavidcs            rval = EINVAL;
18785297884Sdavidcs            break;
18786297884Sdavidcs    }
18787297884Sdavidcs
18788297884Sdavidcs    if (rval) {
18789297884Sdavidcs        BLOGW(sc, "ioctl cmd %d  failed rval %d\n", eeprom->eeprom_cmd, rval);
18790297884Sdavidcs    }
18791297884Sdavidcs
18792297884Sdavidcs    return (rval);
18793297884Sdavidcs}
18794297884Sdavidcs
18795297884Sdavidcsstatic int
18796297884Sdavidcsbxe_get_settings(struct bxe_softc *sc, bxe_dev_setting_t *dev_p)
18797297884Sdavidcs{
18798297884Sdavidcs    uint32_t ext_phy_config;
18799297884Sdavidcs    int port = SC_PORT(sc);
18800297884Sdavidcs    int cfg_idx = bxe_get_link_cfg_idx(sc);
18801297884Sdavidcs
18802297884Sdavidcs    dev_p->supported = sc->port.supported[cfg_idx] |
18803297884Sdavidcs            (sc->port.supported[cfg_idx ^ 1] &
18804297884Sdavidcs            (ELINK_SUPPORTED_TP | ELINK_SUPPORTED_FIBRE));
18805297884Sdavidcs    dev_p->advertising = sc->port.advertising[cfg_idx];
18806297884Sdavidcs    if(sc->link_params.phy[bxe_get_cur_phy_idx(sc)].media_type ==
18807297884Sdavidcs        ELINK_ETH_PHY_SFP_1G_FIBER) {
18808297884Sdavidcs        dev_p->supported = ~(ELINK_SUPPORTED_10000baseT_Full);
18809297884Sdavidcs        dev_p->advertising &= ~(ADVERTISED_10000baseT_Full);
18810297884Sdavidcs    }
18811297884Sdavidcs    if ((sc->state == BXE_STATE_OPEN) && sc->link_vars.link_up &&
18812297884Sdavidcs        !(sc->flags & BXE_MF_FUNC_DIS)) {
18813297884Sdavidcs        dev_p->duplex = sc->link_vars.duplex;
18814297884Sdavidcs        if (IS_MF(sc) && !BXE_NOMCP(sc))
18815297884Sdavidcs            dev_p->speed = bxe_get_mf_speed(sc);
18816297884Sdavidcs        else
18817297884Sdavidcs            dev_p->speed = sc->link_vars.line_speed;
18818297884Sdavidcs    } else {
18819297884Sdavidcs        dev_p->duplex = DUPLEX_UNKNOWN;
18820297884Sdavidcs        dev_p->speed = SPEED_UNKNOWN;
18821297884Sdavidcs    }
18822297884Sdavidcs
18823297884Sdavidcs    dev_p->port = bxe_media_detect(sc);
18824297884Sdavidcs
18825297884Sdavidcs    ext_phy_config = SHMEM_RD(sc,
18826297884Sdavidcs                         dev_info.port_hw_config[port].external_phy_config);
18827297884Sdavidcs    if((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) ==
18828297884Sdavidcs        PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
18829297884Sdavidcs        dev_p->phy_address =  sc->port.phy_addr;
18830297884Sdavidcs    else if(((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18831297884Sdavidcs            PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
18832297884Sdavidcs        ((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18833297884Sdavidcs            PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
18834297884Sdavidcs        dev_p->phy_address = ELINK_XGXS_EXT_PHY_ADDR(ext_phy_config);
18835297884Sdavidcs    else
18836297884Sdavidcs        dev_p->phy_address = 0;
18837297884Sdavidcs
18838297884Sdavidcs    if(sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG)
18839297884Sdavidcs        dev_p->autoneg = AUTONEG_ENABLE;
18840297884Sdavidcs    else
18841297884Sdavidcs       dev_p->autoneg = AUTONEG_DISABLE;
18842297884Sdavidcs
18843297884Sdavidcs
18844297884Sdavidcs    return 0;
18845297884Sdavidcs}
18846297884Sdavidcs
18847297884Sdavidcsstatic int
18848292639Sdavidcsbxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18849292639Sdavidcs        struct thread *td)
18850292639Sdavidcs{
18851292639Sdavidcs    struct bxe_softc    *sc;
18852292639Sdavidcs    int                 rval = 0;
18853292639Sdavidcs    device_t            pci_dev;
18854292639Sdavidcs    bxe_grcdump_t       *dump = NULL;
18855292639Sdavidcs    int grc_dump_size;
18856297884Sdavidcs    bxe_drvinfo_t   *drv_infop = NULL;
18857297884Sdavidcs    bxe_dev_setting_t  *dev_p;
18858297884Sdavidcs    bxe_dev_setting_t  dev_set;
18859297884Sdavidcs    bxe_get_regs_t  *reg_p;
18860297884Sdavidcs    bxe_reg_rdw_t *reg_rdw_p;
18861297884Sdavidcs    bxe_pcicfg_rdw_t *cfg_rdw_p;
18862297884Sdavidcs    bxe_perm_mac_addr_t *mac_addr_p;
18863292639Sdavidcs
18864297884Sdavidcs
18865292639Sdavidcs    if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
18866292639Sdavidcs        return ENXIO;
18867292639Sdavidcs
18868292639Sdavidcs    pci_dev= sc->dev;
18869292639Sdavidcs
18870292639Sdavidcs    dump = (bxe_grcdump_t *)data;
18871292639Sdavidcs
18872292639Sdavidcs    switch(cmd) {
18873292639Sdavidcs
18874292639Sdavidcs        case BXE_GRC_DUMP_SIZE:
18875292639Sdavidcs            dump->pci_func = sc->pcie_func;
18876297884Sdavidcs            dump->grcdump_size =
18877297884Sdavidcs                (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18878297884Sdavidcs                     sizeof(struct  dump_header);
18879292639Sdavidcs            break;
18880292639Sdavidcs
18881292639Sdavidcs        case BXE_GRC_DUMP:
18882292639Sdavidcs
18883292639Sdavidcs            grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18884297884Sdavidcs                                sizeof(struct  dump_header);
18885298294Sdavidcs            if ((!sc->trigger_grcdump) || (dump->grcdump == NULL) ||
18886298294Sdavidcs                (dump->grcdump_size < grc_dump_size)) {
18887292639Sdavidcs                rval = EINVAL;
18888292639Sdavidcs                break;
18889292639Sdavidcs            }
18890292639Sdavidcs
18891298294Sdavidcs            if((sc->trigger_grcdump) && (!sc->grcdump_done) &&
18892298294Sdavidcs                (!sc->grcdump_started)) {
18893298294Sdavidcs                rval =  bxe_grc_dump(sc);
18894298294Sdavidcs            }
18895298294Sdavidcs
18896298294Sdavidcs            if((!rval) && (sc->grcdump_done) && (sc->grcdump_started) &&
18897298294Sdavidcs                (sc->grc_dump != NULL))  {
18898298294Sdavidcs                dump->grcdump_dwords = grc_dump_size >> 2;
18899298294Sdavidcs                rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
18900298294Sdavidcs                free(sc->grc_dump, M_DEVBUF);
18901298294Sdavidcs                sc->grc_dump = NULL;
18902298294Sdavidcs                sc->grcdump_started = 0;
18903298294Sdavidcs                sc->grcdump_done = 0;
18904298294Sdavidcs            }
18905298294Sdavidcs
18906292639Sdavidcs            break;
18907292639Sdavidcs
18908297884Sdavidcs        case BXE_DRV_INFO:
18909297884Sdavidcs            drv_infop = (bxe_drvinfo_t *)data;
18910297884Sdavidcs            snprintf(drv_infop->drv_name, BXE_DRV_NAME_LENGTH, "%s", "bxe");
18911297884Sdavidcs            snprintf(drv_infop->drv_version, BXE_DRV_VERSION_LENGTH, "v:%s",
18912297884Sdavidcs                BXE_DRIVER_VERSION);
18913297884Sdavidcs            snprintf(drv_infop->mfw_version, BXE_MFW_VERSION_LENGTH, "%s",
18914297884Sdavidcs                sc->devinfo.bc_ver_str);
18915297884Sdavidcs            snprintf(drv_infop->stormfw_version, BXE_STORMFW_VERSION_LENGTH,
18916297884Sdavidcs                "%s", sc->fw_ver_str);
18917297884Sdavidcs            drv_infop->eeprom_dump_len = sc->devinfo.flash_size;
18918297884Sdavidcs            drv_infop->reg_dump_len =
18919297884Sdavidcs                (bxe_get_total_regs_len32(sc) * sizeof(uint32_t))
18920297884Sdavidcs                    + sizeof(struct  dump_header);
18921297884Sdavidcs            snprintf(drv_infop->bus_info, BXE_BUS_INFO_LENGTH, "%d:%d:%d",
18922297884Sdavidcs                sc->pcie_bus, sc->pcie_device, sc->pcie_func);
18923297884Sdavidcs            break;
18924298294Sdavidcs
18925297884Sdavidcs        case BXE_DEV_SETTING:
18926297884Sdavidcs            dev_p = (bxe_dev_setting_t *)data;
18927297884Sdavidcs            bxe_get_settings(sc, &dev_set);
18928297884Sdavidcs            dev_p->supported = dev_set.supported;
18929297884Sdavidcs            dev_p->advertising = dev_set.advertising;
18930297884Sdavidcs            dev_p->speed = dev_set.speed;
18931297884Sdavidcs            dev_p->duplex = dev_set.duplex;
18932297884Sdavidcs            dev_p->port = dev_set.port;
18933297884Sdavidcs            dev_p->phy_address = dev_set.phy_address;
18934297884Sdavidcs            dev_p->autoneg = dev_set.autoneg;
18935297884Sdavidcs
18936297884Sdavidcs            break;
18937297884Sdavidcs
18938297884Sdavidcs        case BXE_GET_REGS:
18939297884Sdavidcs
18940297884Sdavidcs            reg_p = (bxe_get_regs_t *)data;
18941297884Sdavidcs            grc_dump_size = reg_p->reg_buf_len;
18942297884Sdavidcs
18943298294Sdavidcs            if((!sc->grcdump_done) && (!sc->grcdump_started)) {
18944297884Sdavidcs                bxe_grc_dump(sc);
18945297884Sdavidcs            }
18946298294Sdavidcs            if((sc->grcdump_done) && (sc->grcdump_started) &&
18947298294Sdavidcs                (sc->grc_dump != NULL))  {
18948297884Sdavidcs                rval = copyout(sc->grc_dump, reg_p->reg_buf, grc_dump_size);
18949298294Sdavidcs                free(sc->grc_dump, M_DEVBUF);
18950298294Sdavidcs                sc->grc_dump = NULL;
18951298294Sdavidcs                sc->grcdump_started = 0;
18952297884Sdavidcs                sc->grcdump_done = 0;
18953297884Sdavidcs            }
18954297884Sdavidcs
18955297884Sdavidcs            break;
18956298294Sdavidcs
18957297884Sdavidcs        case BXE_RDW_REG:
18958297884Sdavidcs            reg_rdw_p = (bxe_reg_rdw_t *)data;
18959297884Sdavidcs            if((reg_rdw_p->reg_cmd == BXE_READ_REG_CMD) &&
18960297884Sdavidcs                (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
18961297884Sdavidcs                reg_rdw_p->reg_val = REG_RD(sc, reg_rdw_p->reg_id);
18962297884Sdavidcs
18963297884Sdavidcs            if((reg_rdw_p->reg_cmd == BXE_WRITE_REG_CMD) &&
18964297884Sdavidcs                (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
18965297884Sdavidcs                REG_WR(sc, reg_rdw_p->reg_id, reg_rdw_p->reg_val);
18966297884Sdavidcs
18967297884Sdavidcs            break;
18968297884Sdavidcs
18969297884Sdavidcs        case BXE_RDW_PCICFG:
18970297884Sdavidcs            cfg_rdw_p = (bxe_pcicfg_rdw_t *)data;
18971297884Sdavidcs            if(cfg_rdw_p->cfg_cmd == BXE_READ_PCICFG) {
18972297884Sdavidcs
18973297884Sdavidcs                cfg_rdw_p->cfg_val = pci_read_config(sc->dev, cfg_rdw_p->cfg_id,
18974297884Sdavidcs                                         cfg_rdw_p->cfg_width);
18975297884Sdavidcs
18976297884Sdavidcs            } else if(cfg_rdw_p->cfg_cmd == BXE_WRITE_PCICFG) {
18977297884Sdavidcs                pci_write_config(sc->dev, cfg_rdw_p->cfg_id, cfg_rdw_p->cfg_val,
18978297884Sdavidcs                            cfg_rdw_p->cfg_width);
18979297884Sdavidcs            } else {
18980297884Sdavidcs                BLOGW(sc, "BXE_RDW_PCICFG ioctl wrong cmd passed\n");
18981297884Sdavidcs            }
18982297884Sdavidcs            break;
18983297884Sdavidcs
18984297884Sdavidcs        case BXE_MAC_ADDR:
18985297884Sdavidcs            mac_addr_p = (bxe_perm_mac_addr_t *)data;
18986297884Sdavidcs            snprintf(mac_addr_p->mac_addr_str, sizeof(sc->mac_addr_str), "%s",
18987297884Sdavidcs                sc->mac_addr_str);
18988297884Sdavidcs            break;
18989297884Sdavidcs
18990297884Sdavidcs        case BXE_EEPROM:
18991297884Sdavidcs            rval = bxe_eeprom_rd_wr(sc, (bxe_eeprom_t *)data);
18992297884Sdavidcs            break;
18993297884Sdavidcs
18994297884Sdavidcs
18995292639Sdavidcs        default:
18996292639Sdavidcs            break;
18997292639Sdavidcs    }
18998292639Sdavidcs
18999292639Sdavidcs    return (rval);
19000292639Sdavidcs}
19001