bxe.c revision 296071
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 296071 2016-02-25 22:44:00Z davidcs $");
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),
490255736Sdavidch                4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"}
491255736Sdavidch};
492255736Sdavidch
493255736Sdavidchstatic const struct {
494255736Sdavidch    uint32_t offset;
495255736Sdavidch    uint32_t size;
496255736Sdavidch    char string[STAT_NAME_LEN];
497255736Sdavidch} bxe_eth_q_stats_arr[] = {
498255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_received_hi),
499255736Sdavidch                8, "rx_bytes" },
500255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
501255736Sdavidch                8, "rx_ucast_packets" },
502255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
503255736Sdavidch                8, "rx_mcast_packets" },
504255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
505255736Sdavidch                8, "rx_bcast_packets" },
506255736Sdavidch    { Q_STATS_OFFSET32(no_buff_discard_hi),
507255736Sdavidch                8, "rx_discards" },
508255736Sdavidch    { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
509255736Sdavidch                8, "tx_bytes" },
510255736Sdavidch    { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
511255736Sdavidch                8, "tx_ucast_packets" },
512255736Sdavidch    { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
513255736Sdavidch                8, "tx_mcast_packets" },
514255736Sdavidch    { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
515255736Sdavidch                8, "tx_bcast_packets" },
516255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
517255736Sdavidch                8, "tpa_aggregations" },
518255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
519255736Sdavidch                8, "tpa_aggregated_frames"},
520255736Sdavidch    { Q_STATS_OFFSET32(total_tpa_bytes_hi),
521255736Sdavidch                8, "tpa_bytes"},
522255736Sdavidch    { Q_STATS_OFFSET32(rx_calls),
523255736Sdavidch                4, "rx_calls"},
524255736Sdavidch    { Q_STATS_OFFSET32(rx_pkts),
525255736Sdavidch                4, "rx_pkts"},
526255736Sdavidch    { Q_STATS_OFFSET32(rx_tpa_pkts),
527255736Sdavidch                4, "rx_tpa_pkts"},
528292638Sdavidcs    { Q_STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
529292638Sdavidcs                4, "rx_erroneous_jumbo_sge_pkts"},
530292638Sdavidcs    { Q_STATS_OFFSET32(rx_bxe_service_rxsgl),
531292638Sdavidcs                4, "rx_bxe_service_rxsgl"},
532283274Sdavidcs    { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
533283274Sdavidcs                4, "rx_jumbo_sge_pkts"},
534255736Sdavidch    { Q_STATS_OFFSET32(rx_soft_errors),
535255736Sdavidch                4, "rx_soft_errors"},
536255736Sdavidch    { Q_STATS_OFFSET32(rx_hw_csum_errors),
537255736Sdavidch                4, "rx_hw_csum_errors"},
538255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
539255736Sdavidch                4, "rx_ofld_frames_csum_ip"},
540255736Sdavidch    { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
541255736Sdavidch                4, "rx_ofld_frames_csum_tcp_udp"},
542255736Sdavidch    { Q_STATS_OFFSET32(rx_budget_reached),
543255736Sdavidch                4, "rx_budget_reached"},
544255736Sdavidch    { Q_STATS_OFFSET32(tx_pkts),
545255736Sdavidch                4, "tx_pkts"},
546255736Sdavidch    { Q_STATS_OFFSET32(tx_soft_errors),
547255736Sdavidch                4, "tx_soft_errors"},
548255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
549255736Sdavidch                4, "tx_ofld_frames_csum_ip"},
550255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
551255736Sdavidch                4, "tx_ofld_frames_csum_tcp"},
552255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
553255736Sdavidch                4, "tx_ofld_frames_csum_udp"},
554255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso),
555255736Sdavidch                4, "tx_ofld_frames_lso"},
556255736Sdavidch    { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
557255736Sdavidch                4, "tx_ofld_frames_lso_hdr_splits"},
558255736Sdavidch    { Q_STATS_OFFSET32(tx_encap_failures),
559255736Sdavidch                4, "tx_encap_failures"},
560255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_queue_full),
561255736Sdavidch                4, "tx_hw_queue_full"},
562255736Sdavidch    { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
563255736Sdavidch                4, "tx_hw_max_queue_depth"},
564255736Sdavidch    { Q_STATS_OFFSET32(tx_dma_mapping_failure),
565255736Sdavidch                4, "tx_dma_mapping_failure"},
566255736Sdavidch    { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
567255736Sdavidch                4, "tx_max_drbr_queue_depth"},
568255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_std),
569255736Sdavidch                4, "tx_window_violation_std"},
570255736Sdavidch    { Q_STATS_OFFSET32(tx_window_violation_tso),
571255736Sdavidch                4, "tx_window_violation_tso"},
572255736Sdavidch    { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
573255736Sdavidch                4, "tx_chain_lost_mbuf"},
574255736Sdavidch    { Q_STATS_OFFSET32(tx_frames_deferred),
575255736Sdavidch                4, "tx_frames_deferred"},
576255736Sdavidch    { Q_STATS_OFFSET32(tx_queue_xoff),
577255736Sdavidch                4, "tx_queue_xoff"},
578255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_attempts),
579255736Sdavidch                4, "mbuf_defrag_attempts"},
580255736Sdavidch    { Q_STATS_OFFSET32(mbuf_defrag_failures),
581255736Sdavidch                4, "mbuf_defrag_failures"},
582255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
583255736Sdavidch                4, "mbuf_rx_bd_alloc_failed"},
584255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
585255736Sdavidch                4, "mbuf_rx_bd_mapping_failed"},
586255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
587255736Sdavidch                4, "mbuf_rx_tpa_alloc_failed"},
588255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
589255736Sdavidch                4, "mbuf_rx_tpa_mapping_failed"},
590255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
591255736Sdavidch                4, "mbuf_rx_sge_alloc_failed"},
592255736Sdavidch    { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
593255736Sdavidch                4, "mbuf_rx_sge_mapping_failed"},
594255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tx),
595255736Sdavidch                4, "mbuf_alloc_tx"},
596255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_rx),
597255736Sdavidch                4, "mbuf_alloc_rx"},
598255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_sge),
599255736Sdavidch                4, "mbuf_alloc_sge"},
600255736Sdavidch    { Q_STATS_OFFSET32(mbuf_alloc_tpa),
601255736Sdavidch                4, "mbuf_alloc_tpa"}
602255736Sdavidch};
603255736Sdavidch
604255736Sdavidch#define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
605255736Sdavidch#define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
606255736Sdavidch
607255736Sdavidch
608255736Sdavidchstatic void    bxe_cmng_fns_init(struct bxe_softc *sc,
609255736Sdavidch                                 uint8_t          read_cfg,
610255736Sdavidch                                 uint8_t          cmng_type);
611255736Sdavidchstatic int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
612255736Sdavidchstatic void    storm_memset_cmng(struct bxe_softc *sc,
613255736Sdavidch                                 struct cmng_init *cmng,
614255736Sdavidch                                 uint8_t          port);
615255736Sdavidchstatic void    bxe_set_reset_global(struct bxe_softc *sc);
616255736Sdavidchstatic void    bxe_set_reset_in_progress(struct bxe_softc *sc);
617255736Sdavidchstatic uint8_t bxe_reset_is_done(struct bxe_softc *sc,
618255736Sdavidch                                 int              engine);
619255736Sdavidchstatic uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
620255736Sdavidchstatic uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
621255736Sdavidch                                   uint8_t          *global,
622255736Sdavidch                                   uint8_t          print);
623255736Sdavidchstatic void    bxe_int_disable(struct bxe_softc *sc);
624255736Sdavidchstatic int     bxe_release_leader_lock(struct bxe_softc *sc);
625255736Sdavidchstatic void    bxe_pf_disable(struct bxe_softc *sc);
626255736Sdavidchstatic void    bxe_free_fp_buffers(struct bxe_softc *sc);
627255736Sdavidchstatic inline void bxe_update_rx_prod(struct bxe_softc    *sc,
628255736Sdavidch                                      struct bxe_fastpath *fp,
629255736Sdavidch                                      uint16_t            rx_bd_prod,
630255736Sdavidch                                      uint16_t            rx_cq_prod,
631255736Sdavidch                                      uint16_t            rx_sge_prod);
632255736Sdavidchstatic void    bxe_link_report_locked(struct bxe_softc *sc);
633255736Sdavidchstatic void    bxe_link_report(struct bxe_softc *sc);
634255736Sdavidchstatic void    bxe_link_status_update(struct bxe_softc *sc);
635255736Sdavidchstatic void    bxe_periodic_callout_func(void *xsc);
636255736Sdavidchstatic void    bxe_periodic_start(struct bxe_softc *sc);
637255736Sdavidchstatic void    bxe_periodic_stop(struct bxe_softc *sc);
638255736Sdavidchstatic int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
639255736Sdavidch                                    uint16_t prev_index,
640255736Sdavidch                                    uint16_t index);
641255736Sdavidchstatic int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
642255736Sdavidch                                     int                 queue);
643255736Sdavidchstatic int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
644255736Sdavidch                                     uint16_t            index);
645255736Sdavidchstatic uint8_t bxe_txeof(struct bxe_softc *sc,
646255736Sdavidch                         struct bxe_fastpath *fp);
647255736Sdavidchstatic void    bxe_task_fp(struct bxe_fastpath *fp);
648255736Sdavidchstatic __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
649255736Sdavidch                                     struct mbuf      *m,
650255736Sdavidch                                     uint8_t          contents);
651255736Sdavidchstatic int     bxe_alloc_mem(struct bxe_softc *sc);
652255736Sdavidchstatic void    bxe_free_mem(struct bxe_softc *sc);
653255736Sdavidchstatic int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
654255736Sdavidchstatic void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
655255736Sdavidchstatic int     bxe_interrupt_attach(struct bxe_softc *sc);
656255736Sdavidchstatic void    bxe_interrupt_detach(struct bxe_softc *sc);
657255736Sdavidchstatic void    bxe_set_rx_mode(struct bxe_softc *sc);
658255736Sdavidchstatic int     bxe_init_locked(struct bxe_softc *sc);
659255736Sdavidchstatic int     bxe_stop_locked(struct bxe_softc *sc);
660255736Sdavidchstatic __noinline int bxe_nic_load(struct bxe_softc *sc,
661255736Sdavidch                                   int              load_mode);
662255736Sdavidchstatic __noinline int bxe_nic_unload(struct bxe_softc *sc,
663255736Sdavidch                                     uint32_t         unload_mode,
664255736Sdavidch                                     uint8_t          keep_link);
665255736Sdavidch
666255736Sdavidchstatic void bxe_handle_sp_tq(void *context, int pending);
667255736Sdavidchstatic void bxe_handle_fp_tq(void *context, int pending);
668255736Sdavidch
669292639Sdavidcsstatic int bxe_add_cdev(struct bxe_softc *sc);
670292639Sdavidcsstatic void bxe_del_cdev(struct bxe_softc *sc);
671295823Sdavidcsstatic int bxe_grc_dump(struct bxe_softc *sc);
672255736Sdavidch
673255736Sdavidch/* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
674255736Sdavidchuint32_t
675255736Sdavidchcalc_crc32(uint8_t  *crc32_packet,
676255736Sdavidch           uint32_t crc32_length,
677255736Sdavidch           uint32_t crc32_seed,
678255736Sdavidch           uint8_t  complement)
679255736Sdavidch{
680255736Sdavidch   uint32_t byte         = 0;
681255736Sdavidch   uint32_t bit          = 0;
682255736Sdavidch   uint8_t  msb          = 0;
683255736Sdavidch   uint32_t temp         = 0;
684255736Sdavidch   uint32_t shft         = 0;
685255736Sdavidch   uint8_t  current_byte = 0;
686255736Sdavidch   uint32_t crc32_result = crc32_seed;
687255736Sdavidch   const uint32_t CRC32_POLY = 0x1edc6f41;
688255736Sdavidch
689255736Sdavidch   if ((crc32_packet == NULL) ||
690255736Sdavidch       (crc32_length == 0) ||
691255736Sdavidch       ((crc32_length % 8) != 0))
692255736Sdavidch    {
693255736Sdavidch        return (crc32_result);
694255736Sdavidch    }
695255736Sdavidch
696255736Sdavidch    for (byte = 0; byte < crc32_length; byte = byte + 1)
697255736Sdavidch    {
698255736Sdavidch        current_byte = crc32_packet[byte];
699255736Sdavidch        for (bit = 0; bit < 8; bit = bit + 1)
700255736Sdavidch        {
701255736Sdavidch            /* msb = crc32_result[31]; */
702255736Sdavidch            msb = (uint8_t)(crc32_result >> 31);
703255736Sdavidch
704255736Sdavidch            crc32_result = crc32_result << 1;
705255736Sdavidch
706255736Sdavidch            /* it (msb != current_byte[bit]) */
707255736Sdavidch            if (msb != (0x1 & (current_byte >> bit)))
708255736Sdavidch            {
709255736Sdavidch                crc32_result = crc32_result ^ CRC32_POLY;
710255736Sdavidch                /* crc32_result[0] = 1 */
711255736Sdavidch                crc32_result |= 1;
712255736Sdavidch            }
713255736Sdavidch        }
714255736Sdavidch    }
715255736Sdavidch
716255736Sdavidch    /* Last step is to:
717255736Sdavidch     * 1. "mirror" every bit
718255736Sdavidch     * 2. swap the 4 bytes
719255736Sdavidch     * 3. complement each bit
720255736Sdavidch     */
721255736Sdavidch
722255736Sdavidch    /* Mirror */
723255736Sdavidch    temp = crc32_result;
724255736Sdavidch    shft = sizeof(crc32_result) * 8 - 1;
725255736Sdavidch
726255736Sdavidch    for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
727255736Sdavidch    {
728255736Sdavidch        temp <<= 1;
729255736Sdavidch        temp |= crc32_result & 1;
730255736Sdavidch        shft-- ;
731255736Sdavidch    }
732255736Sdavidch
733255736Sdavidch    /* temp[31-bit] = crc32_result[bit] */
734255736Sdavidch    temp <<= shft;
735255736Sdavidch
736255736Sdavidch    /* Swap */
737255736Sdavidch    /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
738255736Sdavidch    {
739255736Sdavidch        uint32_t t0, t1, t2, t3;
740255736Sdavidch        t0 = (0x000000ff & (temp >> 24));
741255736Sdavidch        t1 = (0x0000ff00 & (temp >> 8));
742255736Sdavidch        t2 = (0x00ff0000 & (temp << 8));
743255736Sdavidch        t3 = (0xff000000 & (temp << 24));
744255736Sdavidch        crc32_result = t0 | t1 | t2 | t3;
745255736Sdavidch    }
746255736Sdavidch
747255736Sdavidch    /* Complement */
748255736Sdavidch    if (complement)
749255736Sdavidch    {
750255736Sdavidch        crc32_result = ~crc32_result;
751255736Sdavidch    }
752255736Sdavidch
753255736Sdavidch    return (crc32_result);
754255736Sdavidch}
755255736Sdavidch
756255736Sdavidchint
757255736Sdavidchbxe_test_bit(int                    nr,
758255736Sdavidch             volatile unsigned long *addr)
759255736Sdavidch{
760255736Sdavidch    return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
761255736Sdavidch}
762255736Sdavidch
763255736Sdavidchvoid
764255736Sdavidchbxe_set_bit(unsigned int           nr,
765255736Sdavidch            volatile unsigned long *addr)
766255736Sdavidch{
767255736Sdavidch    atomic_set_acq_long(addr, (1 << nr));
768255736Sdavidch}
769255736Sdavidch
770255736Sdavidchvoid
771255736Sdavidchbxe_clear_bit(int                    nr,
772255736Sdavidch              volatile unsigned long *addr)
773255736Sdavidch{
774255736Sdavidch    atomic_clear_acq_long(addr, (1 << nr));
775255736Sdavidch}
776255736Sdavidch
777255736Sdavidchint
778255736Sdavidchbxe_test_and_set_bit(int                    nr,
779255736Sdavidch                       volatile unsigned long *addr)
780255736Sdavidch{
781255736Sdavidch    unsigned long x;
782255736Sdavidch    nr = (1 << nr);
783255736Sdavidch    do {
784255736Sdavidch        x = *addr;
785255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
786255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
787255736Sdavidch    return (x & nr);
788255736Sdavidch}
789255736Sdavidch
790255736Sdavidchint
791255736Sdavidchbxe_test_and_clear_bit(int                    nr,
792255736Sdavidch                       volatile unsigned long *addr)
793255736Sdavidch{
794255736Sdavidch    unsigned long x;
795255736Sdavidch    nr = (1 << nr);
796255736Sdavidch    do {
797255736Sdavidch        x = *addr;
798255736Sdavidch    } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
799255736Sdavidch    // if (x & nr) bit_was_set; else bit_was_not_set;
800255736Sdavidch    return (x & nr);
801255736Sdavidch}
802255736Sdavidch
803255736Sdavidchint
804255736Sdavidchbxe_cmpxchg(volatile int *addr,
805255736Sdavidch            int          old,
806255736Sdavidch            int          new)
807255736Sdavidch{
808255736Sdavidch    int x;
809255736Sdavidch    do {
810255736Sdavidch        x = *addr;
811255736Sdavidch    } while (atomic_cmpset_acq_int(addr, old, new) == 0);
812255736Sdavidch    return (x);
813255736Sdavidch}
814255736Sdavidch
815255736Sdavidch/*
816255736Sdavidch * Get DMA memory from the OS.
817255736Sdavidch *
818255736Sdavidch * Validates that the OS has provided DMA buffers in response to a
819255736Sdavidch * bus_dmamap_load call and saves the physical address of those buffers.
820255736Sdavidch * When the callback is used the OS will return 0 for the mapping function
821255736Sdavidch * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
822255736Sdavidch * failures back to the caller.
823255736Sdavidch *
824255736Sdavidch * Returns:
825255736Sdavidch *   Nothing.
826255736Sdavidch */
827255736Sdavidchstatic void
828255736Sdavidchbxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
829255736Sdavidch{
830255736Sdavidch    struct bxe_dma *dma = arg;
831255736Sdavidch
832255736Sdavidch    if (error) {
833255736Sdavidch        dma->paddr = 0;
834255736Sdavidch        dma->nseg  = 0;
835255736Sdavidch        BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
836255736Sdavidch    } else {
837255736Sdavidch        dma->paddr = segs->ds_addr;
838255736Sdavidch        dma->nseg  = nseg;
839255736Sdavidch    }
840255736Sdavidch}
841255736Sdavidch
842255736Sdavidch/*
843255736Sdavidch * Allocate a block of memory and map it for DMA. No partial completions
844255736Sdavidch * allowed and release any resources acquired if we can't acquire all
845255736Sdavidch * resources.
846255736Sdavidch *
847255736Sdavidch * Returns:
848255736Sdavidch *   0 = Success, !0 = Failure
849255736Sdavidch */
850255736Sdavidchint
851255736Sdavidchbxe_dma_alloc(struct bxe_softc *sc,
852255736Sdavidch              bus_size_t       size,
853255736Sdavidch              struct bxe_dma   *dma,
854255736Sdavidch              const char       *msg)
855255736Sdavidch{
856255736Sdavidch    int rc;
857255736Sdavidch
858255736Sdavidch    if (dma->size > 0) {
859256299Sedavis        BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
860256299Sedavis              (unsigned long)dma->size);
861255736Sdavidch        return (1);
862255736Sdavidch    }
863255736Sdavidch
864255736Sdavidch    memset(dma, 0, sizeof(*dma)); /* sanity */
865255736Sdavidch    dma->sc   = sc;
866255736Sdavidch    dma->size = size;
867255736Sdavidch    snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
868255736Sdavidch
869255736Sdavidch    rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
870255736Sdavidch                            BCM_PAGE_SIZE,      /* alignment */
871255736Sdavidch                            0,                  /* boundary limit */
872255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted low */
873255736Sdavidch                            BUS_SPACE_MAXADDR,  /* restricted hi */
874255736Sdavidch                            NULL,               /* addr filter() */
875255736Sdavidch                            NULL,               /* addr filter() arg */
876255736Sdavidch                            size,               /* max map size */
877255736Sdavidch                            1,                  /* num discontinuous */
878255736Sdavidch                            size,               /* max seg size */
879255736Sdavidch                            BUS_DMA_ALLOCNOW,   /* flags */
880255736Sdavidch                            NULL,               /* lock() */
881255736Sdavidch                            NULL,               /* lock() arg */
882255736Sdavidch                            &dma->tag);         /* returned dma tag */
883255736Sdavidch    if (rc != 0) {
884255736Sdavidch        BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
885255736Sdavidch        memset(dma, 0, sizeof(*dma));
886255736Sdavidch        return (1);
887255736Sdavidch    }
888255736Sdavidch
889255736Sdavidch    rc = bus_dmamem_alloc(dma->tag,
890255736Sdavidch                          (void **)&dma->vaddr,
891255736Sdavidch                          (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
892255736Sdavidch                          &dma->map);
893255736Sdavidch    if (rc != 0) {
894255736Sdavidch        BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
895255736Sdavidch        bus_dma_tag_destroy(dma->tag);
896255736Sdavidch        memset(dma, 0, sizeof(*dma));
897255736Sdavidch        return (1);
898255736Sdavidch    }
899255736Sdavidch
900255736Sdavidch    rc = bus_dmamap_load(dma->tag,
901255736Sdavidch                         dma->map,
902255736Sdavidch                         dma->vaddr,
903255736Sdavidch                         size,
904255736Sdavidch                         bxe_dma_map_addr, /* BLOGD in here */
905255736Sdavidch                         dma,
906255736Sdavidch                         BUS_DMA_NOWAIT);
907255736Sdavidch    if (rc != 0) {
908255736Sdavidch        BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
909255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
910255736Sdavidch        bus_dma_tag_destroy(dma->tag);
911255736Sdavidch        memset(dma, 0, sizeof(*dma));
912255736Sdavidch        return (1);
913255736Sdavidch    }
914255736Sdavidch
915255736Sdavidch    return (0);
916255736Sdavidch}
917255736Sdavidch
918255736Sdavidchvoid
919255736Sdavidchbxe_dma_free(struct bxe_softc *sc,
920255736Sdavidch             struct bxe_dma   *dma)
921255736Sdavidch{
922255736Sdavidch    if (dma->size > 0) {
923255736Sdavidch        DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
924255736Sdavidch
925255736Sdavidch        bus_dmamap_sync(dma->tag, dma->map,
926255736Sdavidch                        (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
927255736Sdavidch        bus_dmamap_unload(dma->tag, dma->map);
928255736Sdavidch        bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
929255736Sdavidch        bus_dma_tag_destroy(dma->tag);
930255736Sdavidch    }
931255736Sdavidch
932255736Sdavidch    memset(dma, 0, sizeof(*dma));
933255736Sdavidch}
934255736Sdavidch
935255736Sdavidch/*
936255736Sdavidch * These indirect read and write routines are only during init.
937255736Sdavidch * The locking is handled by the MCP.
938255736Sdavidch */
939255736Sdavidch
940255736Sdavidchvoid
941255736Sdavidchbxe_reg_wr_ind(struct bxe_softc *sc,
942255736Sdavidch               uint32_t         addr,
943255736Sdavidch               uint32_t         val)
944255736Sdavidch{
945255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
946255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
947255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
948255736Sdavidch}
949255736Sdavidch
950255736Sdavidchuint32_t
951255736Sdavidchbxe_reg_rd_ind(struct bxe_softc *sc,
952255736Sdavidch               uint32_t         addr)
953255736Sdavidch{
954255736Sdavidch    uint32_t val;
955255736Sdavidch
956255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
957255736Sdavidch    val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
958255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
959255736Sdavidch
960255736Sdavidch    return (val);
961255736Sdavidch}
962255736Sdavidch
963255736Sdavidchstatic int
964255736Sdavidchbxe_acquire_hw_lock(struct bxe_softc *sc,
965255736Sdavidch                    uint32_t         resource)
966255736Sdavidch{
967255736Sdavidch    uint32_t lock_status;
968255736Sdavidch    uint32_t resource_bit = (1 << resource);
969255736Sdavidch    int func = SC_FUNC(sc);
970255736Sdavidch    uint32_t hw_lock_control_reg;
971255736Sdavidch    int cnt;
972255736Sdavidch
973255736Sdavidch    /* validate the resource is within range */
974255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
975295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
976295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
977255736Sdavidch        return (-1);
978255736Sdavidch    }
979255736Sdavidch
980255736Sdavidch    if (func <= 5) {
981255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
982255736Sdavidch    } else {
983255736Sdavidch        hw_lock_control_reg =
984255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
985255736Sdavidch    }
986255736Sdavidch
987255736Sdavidch    /* validate the resource is not already taken */
988255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
989255736Sdavidch    if (lock_status & resource_bit) {
990295830Sdavidcs        BLOGE(sc, "resource (0x%x) in use (status 0x%x bit 0x%x)\n",
991295830Sdavidcs              resource, lock_status, resource_bit);
992255736Sdavidch        return (-1);
993255736Sdavidch    }
994255736Sdavidch
995255736Sdavidch    /* try every 5ms for 5 seconds */
996255736Sdavidch    for (cnt = 0; cnt < 1000; cnt++) {
997255736Sdavidch        REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
998255736Sdavidch        lock_status = REG_RD(sc, hw_lock_control_reg);
999255736Sdavidch        if (lock_status & resource_bit) {
1000255736Sdavidch            return (0);
1001255736Sdavidch        }
1002255736Sdavidch        DELAY(5000);
1003255736Sdavidch    }
1004255736Sdavidch
1005295830Sdavidcs    BLOGE(sc, "Resource 0x%x resource_bit 0x%x lock timeout!\n",
1006295830Sdavidcs        resource, resource_bit);
1007255736Sdavidch    return (-1);
1008255736Sdavidch}
1009255736Sdavidch
1010255736Sdavidchstatic int
1011255736Sdavidchbxe_release_hw_lock(struct bxe_softc *sc,
1012255736Sdavidch                    uint32_t         resource)
1013255736Sdavidch{
1014255736Sdavidch    uint32_t lock_status;
1015255736Sdavidch    uint32_t resource_bit = (1 << resource);
1016255736Sdavidch    int func = SC_FUNC(sc);
1017255736Sdavidch    uint32_t hw_lock_control_reg;
1018255736Sdavidch
1019255736Sdavidch    /* validate the resource is within range */
1020255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1021295830Sdavidcs        BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1022295830Sdavidcs            " resource_bit 0x%x\n", resource, resource_bit);
1023255736Sdavidch        return (-1);
1024255736Sdavidch    }
1025255736Sdavidch
1026255736Sdavidch    if (func <= 5) {
1027255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1028255736Sdavidch    } else {
1029255736Sdavidch        hw_lock_control_reg =
1030255736Sdavidch                (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1031255736Sdavidch    }
1032255736Sdavidch
1033255736Sdavidch    /* validate the resource is currently taken */
1034255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
1035255736Sdavidch    if (!(lock_status & resource_bit)) {
1036295830Sdavidcs        BLOGE(sc, "resource (0x%x) not in use (status 0x%x bit 0x%x)\n",
1037295830Sdavidcs              resource, lock_status, resource_bit);
1038255736Sdavidch        return (-1);
1039255736Sdavidch    }
1040255736Sdavidch
1041255736Sdavidch    REG_WR(sc, hw_lock_control_reg, resource_bit);
1042255736Sdavidch    return (0);
1043255736Sdavidch}
1044284335Sdavidcsstatic void bxe_acquire_phy_lock(struct bxe_softc *sc)
1045284335Sdavidcs{
1046284335Sdavidcs	BXE_PHY_LOCK(sc);
1047284335Sdavidcs	bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1048284335Sdavidcs}
1049255736Sdavidch
1050284335Sdavidcsstatic void bxe_release_phy_lock(struct bxe_softc *sc)
1051284335Sdavidcs{
1052284335Sdavidcs	bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1053284335Sdavidcs	BXE_PHY_UNLOCK(sc);
1054284335Sdavidcs}
1055255736Sdavidch/*
1056255736Sdavidch * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1057255736Sdavidch * had we done things the other way around, if two pfs from the same port
1058255736Sdavidch * would attempt to access nvram at the same time, we could run into a
1059255736Sdavidch * scenario such as:
1060255736Sdavidch * pf A takes the port lock.
1061255736Sdavidch * pf B succeeds in taking the same lock since they are from the same port.
1062255736Sdavidch * pf A takes the per pf misc lock. Performs eeprom access.
1063255736Sdavidch * pf A finishes. Unlocks the per pf misc lock.
1064255736Sdavidch * Pf B takes the lock and proceeds to perform it's own access.
1065255736Sdavidch * pf A unlocks the per port lock, while pf B is still working (!).
1066255736Sdavidch * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1067255736Sdavidch * access corrupted by pf B).*
1068255736Sdavidch */
1069255736Sdavidchstatic int
1070255736Sdavidchbxe_acquire_nvram_lock(struct bxe_softc *sc)
1071255736Sdavidch{
1072255736Sdavidch    int port = SC_PORT(sc);
1073255736Sdavidch    int count, i;
1074255736Sdavidch    uint32_t val = 0;
1075255736Sdavidch
1076255736Sdavidch    /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1077255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1078255736Sdavidch
1079255736Sdavidch    /* adjust timeout for emulation/FPGA */
1080255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1081255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1082255736Sdavidch        count *= 100;
1083255736Sdavidch    }
1084255736Sdavidch
1085255736Sdavidch    /* request access to nvram interface */
1086255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1087255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1088255736Sdavidch
1089255736Sdavidch    for (i = 0; i < count*10; i++) {
1090255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1091255736Sdavidch        if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1092255736Sdavidch            break;
1093255736Sdavidch        }
1094255736Sdavidch
1095255736Sdavidch        DELAY(5);
1096255736Sdavidch    }
1097255736Sdavidch
1098255736Sdavidch    if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1099295830Sdavidcs        BLOGE(sc, "Cannot get access to nvram interface "
1100295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1101295830Sdavidcs            port, val);
1102255736Sdavidch        return (-1);
1103255736Sdavidch    }
1104255736Sdavidch
1105255736Sdavidch    return (0);
1106255736Sdavidch}
1107255736Sdavidch
1108255736Sdavidchstatic int
1109255736Sdavidchbxe_release_nvram_lock(struct bxe_softc *sc)
1110255736Sdavidch{
1111255736Sdavidch    int port = SC_PORT(sc);
1112255736Sdavidch    int count, i;
1113255736Sdavidch    uint32_t val = 0;
1114255736Sdavidch
1115255736Sdavidch    /* adjust timeout for emulation/FPGA */
1116255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1117255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1118255736Sdavidch        count *= 100;
1119255736Sdavidch    }
1120255736Sdavidch
1121255736Sdavidch    /* relinquish nvram interface */
1122255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1123255736Sdavidch           (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1124255736Sdavidch
1125255736Sdavidch    for (i = 0; i < count*10; i++) {
1126255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1127255736Sdavidch        if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1128255736Sdavidch            break;
1129255736Sdavidch        }
1130255736Sdavidch
1131255736Sdavidch        DELAY(5);
1132255736Sdavidch    }
1133255736Sdavidch
1134255736Sdavidch    if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1135295830Sdavidcs        BLOGE(sc, "Cannot free access to nvram interface "
1136295830Sdavidcs            "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1137295830Sdavidcs            port, val);
1138255736Sdavidch        return (-1);
1139255736Sdavidch    }
1140255736Sdavidch
1141255736Sdavidch    /* release HW lock: protect against other PFs in PF Direct Assignment */
1142255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1143255736Sdavidch
1144255736Sdavidch    return (0);
1145255736Sdavidch}
1146255736Sdavidch
1147255736Sdavidchstatic void
1148255736Sdavidchbxe_enable_nvram_access(struct bxe_softc *sc)
1149255736Sdavidch{
1150255736Sdavidch    uint32_t val;
1151255736Sdavidch
1152255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1153255736Sdavidch
1154255736Sdavidch    /* enable both bits, even on read */
1155255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1156255736Sdavidch           (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1157255736Sdavidch}
1158255736Sdavidch
1159255736Sdavidchstatic void
1160255736Sdavidchbxe_disable_nvram_access(struct bxe_softc *sc)
1161255736Sdavidch{
1162255736Sdavidch    uint32_t val;
1163255736Sdavidch
1164255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1165255736Sdavidch
1166255736Sdavidch    /* disable both bits, even after read */
1167255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1168255736Sdavidch           (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1169255736Sdavidch                    MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1170255736Sdavidch}
1171255736Sdavidch
1172255736Sdavidchstatic int
1173255736Sdavidchbxe_nvram_read_dword(struct bxe_softc *sc,
1174255736Sdavidch                     uint32_t         offset,
1175255736Sdavidch                     uint32_t         *ret_val,
1176255736Sdavidch                     uint32_t         cmd_flags)
1177255736Sdavidch{
1178255736Sdavidch    int count, i, rc;
1179255736Sdavidch    uint32_t val;
1180255736Sdavidch
1181255736Sdavidch    /* build the command word */
1182255736Sdavidch    cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1183255736Sdavidch
1184255736Sdavidch    /* need to clear DONE bit separately */
1185255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1186255736Sdavidch
1187255736Sdavidch    /* address of the NVRAM to read from */
1188255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1189255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1190255736Sdavidch
1191255736Sdavidch    /* issue a read command */
1192255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1193255736Sdavidch
1194255736Sdavidch    /* adjust timeout for emulation/FPGA */
1195255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1196255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1197255736Sdavidch        count *= 100;
1198255736Sdavidch    }
1199255736Sdavidch
1200255736Sdavidch    /* wait for completion */
1201255736Sdavidch    *ret_val = 0;
1202255736Sdavidch    rc = -1;
1203255736Sdavidch    for (i = 0; i < count; i++) {
1204255736Sdavidch        DELAY(5);
1205255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1206255736Sdavidch
1207255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1208255736Sdavidch            val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1209255736Sdavidch            /* we read nvram data in cpu order
1210255736Sdavidch             * but ethtool sees it as an array of bytes
1211255736Sdavidch             * converting to big-endian will do the work
1212255736Sdavidch             */
1213255736Sdavidch            *ret_val = htobe32(val);
1214255736Sdavidch            rc = 0;
1215255736Sdavidch            break;
1216255736Sdavidch        }
1217255736Sdavidch    }
1218255736Sdavidch
1219255736Sdavidch    if (rc == -1) {
1220295830Sdavidcs        BLOGE(sc, "nvram read timeout expired "
1221295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1222295830Sdavidcs            offset, cmd_flags, val);
1223255736Sdavidch    }
1224255736Sdavidch
1225255736Sdavidch    return (rc);
1226255736Sdavidch}
1227255736Sdavidch
1228255736Sdavidchstatic int
1229255736Sdavidchbxe_nvram_read(struct bxe_softc *sc,
1230255736Sdavidch               uint32_t         offset,
1231255736Sdavidch               uint8_t          *ret_buf,
1232255736Sdavidch               int              buf_size)
1233255736Sdavidch{
1234255736Sdavidch    uint32_t cmd_flags;
1235255736Sdavidch    uint32_t val;
1236255736Sdavidch    int rc;
1237255736Sdavidch
1238255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1239255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1240255736Sdavidch              offset, buf_size);
1241255736Sdavidch        return (-1);
1242255736Sdavidch    }
1243255736Sdavidch
1244255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1245255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1246255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1247255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1248255736Sdavidch        return (-1);
1249255736Sdavidch    }
1250255736Sdavidch
1251255736Sdavidch    /* request access to nvram interface */
1252255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1253255736Sdavidch    if (rc) {
1254255736Sdavidch        return (rc);
1255255736Sdavidch    }
1256255736Sdavidch
1257255736Sdavidch    /* enable access to nvram interface */
1258255736Sdavidch    bxe_enable_nvram_access(sc);
1259255736Sdavidch
1260255736Sdavidch    /* read the first word(s) */
1261255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1262255736Sdavidch    while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1263255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1264255736Sdavidch        memcpy(ret_buf, &val, 4);
1265255736Sdavidch
1266255736Sdavidch        /* advance to the next dword */
1267255736Sdavidch        offset += sizeof(uint32_t);
1268255736Sdavidch        ret_buf += sizeof(uint32_t);
1269255736Sdavidch        buf_size -= sizeof(uint32_t);
1270255736Sdavidch        cmd_flags = 0;
1271255736Sdavidch    }
1272255736Sdavidch
1273255736Sdavidch    if (rc == 0) {
1274255736Sdavidch        cmd_flags |= MCPR_NVM_COMMAND_LAST;
1275255736Sdavidch        rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1276255736Sdavidch        memcpy(ret_buf, &val, 4);
1277255736Sdavidch    }
1278255736Sdavidch
1279255736Sdavidch    /* disable access to nvram interface */
1280255736Sdavidch    bxe_disable_nvram_access(sc);
1281255736Sdavidch    bxe_release_nvram_lock(sc);
1282255736Sdavidch
1283255736Sdavidch    return (rc);
1284255736Sdavidch}
1285255736Sdavidch
1286255736Sdavidchstatic int
1287255736Sdavidchbxe_nvram_write_dword(struct bxe_softc *sc,
1288255736Sdavidch                      uint32_t         offset,
1289255736Sdavidch                      uint32_t         val,
1290255736Sdavidch                      uint32_t         cmd_flags)
1291255736Sdavidch{
1292255736Sdavidch    int count, i, rc;
1293255736Sdavidch
1294255736Sdavidch    /* build the command word */
1295255736Sdavidch    cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1296255736Sdavidch
1297255736Sdavidch    /* need to clear DONE bit separately */
1298255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1299255736Sdavidch
1300255736Sdavidch    /* write the data */
1301255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1302255736Sdavidch
1303255736Sdavidch    /* address of the NVRAM to write to */
1304255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1305255736Sdavidch           (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1306255736Sdavidch
1307255736Sdavidch    /* issue the write command */
1308255736Sdavidch    REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1309255736Sdavidch
1310255736Sdavidch    /* adjust timeout for emulation/FPGA */
1311255736Sdavidch    count = NVRAM_TIMEOUT_COUNT;
1312255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
1313255736Sdavidch        count *= 100;
1314255736Sdavidch    }
1315255736Sdavidch
1316255736Sdavidch    /* wait for completion */
1317255736Sdavidch    rc = -1;
1318255736Sdavidch    for (i = 0; i < count; i++) {
1319255736Sdavidch        DELAY(5);
1320255736Sdavidch        val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1321255736Sdavidch        if (val & MCPR_NVM_COMMAND_DONE) {
1322255736Sdavidch            rc = 0;
1323255736Sdavidch            break;
1324255736Sdavidch        }
1325255736Sdavidch    }
1326255736Sdavidch
1327255736Sdavidch    if (rc == -1) {
1328295830Sdavidcs        BLOGE(sc, "nvram write timeout expired "
1329295830Sdavidcs            "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1330295830Sdavidcs            offset, cmd_flags, val);
1331255736Sdavidch    }
1332255736Sdavidch
1333255736Sdavidch    return (rc);
1334255736Sdavidch}
1335255736Sdavidch
1336255736Sdavidch#define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1337255736Sdavidch
1338255736Sdavidchstatic int
1339255736Sdavidchbxe_nvram_write1(struct bxe_softc *sc,
1340255736Sdavidch                 uint32_t         offset,
1341255736Sdavidch                 uint8_t          *data_buf,
1342255736Sdavidch                 int              buf_size)
1343255736Sdavidch{
1344255736Sdavidch    uint32_t cmd_flags;
1345255736Sdavidch    uint32_t align_offset;
1346255736Sdavidch    uint32_t val;
1347255736Sdavidch    int rc;
1348255736Sdavidch
1349255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1350255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1351255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1352255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1353255736Sdavidch        return (-1);
1354255736Sdavidch    }
1355255736Sdavidch
1356255736Sdavidch    /* request access to nvram interface */
1357255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1358255736Sdavidch    if (rc) {
1359255736Sdavidch        return (rc);
1360255736Sdavidch    }
1361255736Sdavidch
1362255736Sdavidch    /* enable access to nvram interface */
1363255736Sdavidch    bxe_enable_nvram_access(sc);
1364255736Sdavidch
1365255736Sdavidch    cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1366255736Sdavidch    align_offset = (offset & ~0x03);
1367255736Sdavidch    rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1368255736Sdavidch
1369255736Sdavidch    if (rc == 0) {
1370255736Sdavidch        val &= ~(0xff << BYTE_OFFSET(offset));
1371255736Sdavidch        val |= (*data_buf << BYTE_OFFSET(offset));
1372255736Sdavidch
1373255736Sdavidch        /* nvram data is returned as an array of bytes
1374255736Sdavidch         * convert it back to cpu order
1375255736Sdavidch         */
1376255736Sdavidch        val = be32toh(val);
1377255736Sdavidch
1378255736Sdavidch        rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1379255736Sdavidch    }
1380255736Sdavidch
1381255736Sdavidch    /* disable access to nvram interface */
1382255736Sdavidch    bxe_disable_nvram_access(sc);
1383255736Sdavidch    bxe_release_nvram_lock(sc);
1384255736Sdavidch
1385255736Sdavidch    return (rc);
1386255736Sdavidch}
1387255736Sdavidch
1388255736Sdavidchstatic int
1389255736Sdavidchbxe_nvram_write(struct bxe_softc *sc,
1390255736Sdavidch                uint32_t         offset,
1391255736Sdavidch                uint8_t          *data_buf,
1392255736Sdavidch                int              buf_size)
1393255736Sdavidch{
1394255736Sdavidch    uint32_t cmd_flags;
1395255736Sdavidch    uint32_t val;
1396255736Sdavidch    uint32_t written_so_far;
1397255736Sdavidch    int rc;
1398255736Sdavidch
1399255736Sdavidch    if (buf_size == 1) {
1400255736Sdavidch        return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1401255736Sdavidch    }
1402255736Sdavidch
1403255736Sdavidch    if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1404255736Sdavidch        BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1405255736Sdavidch              offset, buf_size);
1406255736Sdavidch        return (-1);
1407255736Sdavidch    }
1408255736Sdavidch
1409255736Sdavidch    if (buf_size == 0) {
1410255736Sdavidch        return (0); /* nothing to do */
1411255736Sdavidch    }
1412255736Sdavidch
1413255736Sdavidch    if ((offset + buf_size) > sc->devinfo.flash_size) {
1414255736Sdavidch        BLOGE(sc, "Invalid parameter, "
1415255736Sdavidch                  "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1416255736Sdavidch              offset, buf_size, sc->devinfo.flash_size);
1417255736Sdavidch        return (-1);
1418255736Sdavidch    }
1419255736Sdavidch
1420255736Sdavidch    /* request access to nvram interface */
1421255736Sdavidch    rc = bxe_acquire_nvram_lock(sc);
1422255736Sdavidch    if (rc) {
1423255736Sdavidch        return (rc);
1424255736Sdavidch    }
1425255736Sdavidch
1426255736Sdavidch    /* enable access to nvram interface */
1427255736Sdavidch    bxe_enable_nvram_access(sc);
1428255736Sdavidch
1429255736Sdavidch    written_so_far = 0;
1430255736Sdavidch    cmd_flags = MCPR_NVM_COMMAND_FIRST;
1431255736Sdavidch    while ((written_so_far < buf_size) && (rc == 0)) {
1432255736Sdavidch        if (written_so_far == (buf_size - sizeof(uint32_t))) {
1433255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1434255736Sdavidch        } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1435255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_LAST;
1436255736Sdavidch        } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1437255736Sdavidch            cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1438255736Sdavidch        }
1439255736Sdavidch
1440255736Sdavidch        memcpy(&val, data_buf, 4);
1441255736Sdavidch
1442255736Sdavidch        rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1443255736Sdavidch
1444255736Sdavidch        /* advance to the next dword */
1445255736Sdavidch        offset += sizeof(uint32_t);
1446255736Sdavidch        data_buf += sizeof(uint32_t);
1447255736Sdavidch        written_so_far += sizeof(uint32_t);
1448255736Sdavidch        cmd_flags = 0;
1449255736Sdavidch    }
1450255736Sdavidch
1451255736Sdavidch    /* disable access to nvram interface */
1452255736Sdavidch    bxe_disable_nvram_access(sc);
1453255736Sdavidch    bxe_release_nvram_lock(sc);
1454255736Sdavidch
1455255736Sdavidch    return (rc);
1456255736Sdavidch}
1457255736Sdavidch
1458255736Sdavidch/* copy command into DMAE command memory and set DMAE command Go */
1459255736Sdavidchvoid
1460255736Sdavidchbxe_post_dmae(struct bxe_softc    *sc,
1461296071Sdavidcs              struct dmae_cmd *dmae,
1462255736Sdavidch              int                 idx)
1463255736Sdavidch{
1464255736Sdavidch    uint32_t cmd_offset;
1465255736Sdavidch    int i;
1466255736Sdavidch
1467296071Sdavidcs    cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_cmd) * idx));
1468296071Sdavidcs    for (i = 0; i < ((sizeof(struct dmae_cmd) / 4)); i++) {
1469255736Sdavidch        REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1470255736Sdavidch    }
1471255736Sdavidch
1472255736Sdavidch    REG_WR(sc, dmae_reg_go_c[idx], 1);
1473255736Sdavidch}
1474255736Sdavidch
1475255736Sdavidchuint32_t
1476255736Sdavidchbxe_dmae_opcode_add_comp(uint32_t opcode,
1477255736Sdavidch                         uint8_t  comp_type)
1478255736Sdavidch{
1479296071Sdavidcs    return (opcode | ((comp_type << DMAE_CMD_C_DST_SHIFT) |
1480296071Sdavidcs                      DMAE_CMD_C_TYPE_ENABLE));
1481255736Sdavidch}
1482255736Sdavidch
1483255736Sdavidchuint32_t
1484255736Sdavidchbxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1485255736Sdavidch{
1486296071Sdavidcs    return (opcode & ~DMAE_CMD_SRC_RESET);
1487255736Sdavidch}
1488255736Sdavidch
1489255736Sdavidchuint32_t
1490255736Sdavidchbxe_dmae_opcode(struct bxe_softc *sc,
1491255736Sdavidch                uint8_t          src_type,
1492255736Sdavidch                uint8_t          dst_type,
1493255736Sdavidch                uint8_t          with_comp,
1494255736Sdavidch                uint8_t          comp_type)
1495255736Sdavidch{
1496255736Sdavidch    uint32_t opcode = 0;
1497255736Sdavidch
1498296071Sdavidcs    opcode |= ((src_type << DMAE_CMD_SRC_SHIFT) |
1499296071Sdavidcs               (dst_type << DMAE_CMD_DST_SHIFT));
1500255736Sdavidch
1501296071Sdavidcs    opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
1502255736Sdavidch
1503255736Sdavidch    opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1504255736Sdavidch
1505296071Sdavidcs    opcode |= ((SC_VN(sc) << DMAE_CMD_E1HVN_SHIFT) |
1506296071Sdavidcs               (SC_VN(sc) << DMAE_CMD_DST_VN_SHIFT));
1507255736Sdavidch
1508296071Sdavidcs    opcode |= (DMAE_COM_SET_ERR << DMAE_CMD_ERR_POLICY_SHIFT);
1509255736Sdavidch
1510255736Sdavidch#ifdef __BIG_ENDIAN
1511255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1512255736Sdavidch#else
1513255736Sdavidch    opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1514255736Sdavidch#endif
1515255736Sdavidch
1516255736Sdavidch    if (with_comp) {
1517255736Sdavidch        opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1518255736Sdavidch    }
1519255736Sdavidch
1520255736Sdavidch    return (opcode);
1521255736Sdavidch}
1522255736Sdavidch
1523255736Sdavidchstatic void
1524255736Sdavidchbxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1525296071Sdavidcs                        struct dmae_cmd *dmae,
1526255736Sdavidch                        uint8_t             src_type,
1527255736Sdavidch                        uint8_t             dst_type)
1528255736Sdavidch{
1529296071Sdavidcs    memset(dmae, 0, sizeof(struct dmae_cmd));
1530255736Sdavidch
1531255736Sdavidch    /* set the opcode */
1532255736Sdavidch    dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1533255736Sdavidch                                   TRUE, DMAE_COMP_PCI);
1534255736Sdavidch
1535255736Sdavidch    /* fill in the completion parameters */
1536255736Sdavidch    dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1537255736Sdavidch    dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1538255736Sdavidch    dmae->comp_val     = DMAE_COMP_VAL;
1539255736Sdavidch}
1540255736Sdavidch
1541255736Sdavidch/* issue a DMAE command over the init channel and wait for completion */
1542255736Sdavidchstatic int
1543255736Sdavidchbxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1544296071Sdavidcs                         struct dmae_cmd *dmae)
1545255736Sdavidch{
1546255736Sdavidch    uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1547255736Sdavidch    int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1548255736Sdavidch
1549255736Sdavidch    BXE_DMAE_LOCK(sc);
1550255736Sdavidch
1551255736Sdavidch    /* reset completion */
1552255736Sdavidch    *wb_comp = 0;
1553255736Sdavidch
1554255736Sdavidch    /* post the command on the channel used for initializations */
1555255736Sdavidch    bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1556255736Sdavidch
1557255736Sdavidch    /* wait for completion */
1558255736Sdavidch    DELAY(5);
1559255736Sdavidch
1560255736Sdavidch    while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1561255736Sdavidch        if (!timeout ||
1562255736Sdavidch            (sc->recovery_state != BXE_RECOVERY_DONE &&
1563255736Sdavidch             sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1564295830Sdavidcs            BLOGE(sc, "DMAE timeout! *wb_comp 0x%x recovery_state 0x%x\n",
1565295830Sdavidcs                *wb_comp, sc->recovery_state);
1566255736Sdavidch            BXE_DMAE_UNLOCK(sc);
1567255736Sdavidch            return (DMAE_TIMEOUT);
1568255736Sdavidch        }
1569255736Sdavidch
1570255736Sdavidch        timeout--;
1571255736Sdavidch        DELAY(50);
1572255736Sdavidch    }
1573255736Sdavidch
1574255736Sdavidch    if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1575295830Sdavidcs        BLOGE(sc, "DMAE PCI error! *wb_comp 0x%x recovery_state 0x%x\n",
1576295830Sdavidcs                *wb_comp, sc->recovery_state);
1577255736Sdavidch        BXE_DMAE_UNLOCK(sc);
1578255736Sdavidch        return (DMAE_PCI_ERROR);
1579255736Sdavidch    }
1580255736Sdavidch
1581255736Sdavidch    BXE_DMAE_UNLOCK(sc);
1582255736Sdavidch    return (0);
1583255736Sdavidch}
1584255736Sdavidch
1585255736Sdavidchvoid
1586255736Sdavidchbxe_read_dmae(struct bxe_softc *sc,
1587255736Sdavidch              uint32_t         src_addr,
1588255736Sdavidch              uint32_t         len32)
1589255736Sdavidch{
1590296071Sdavidcs    struct dmae_cmd dmae;
1591255736Sdavidch    uint32_t *data;
1592255736Sdavidch    int i, rc;
1593255736Sdavidch
1594255736Sdavidch    DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1595255736Sdavidch
1596255736Sdavidch    if (!sc->dmae_ready) {
1597255736Sdavidch        data = BXE_SP(sc, wb_data[0]);
1598255736Sdavidch
1599255736Sdavidch        for (i = 0; i < len32; i++) {
1600255736Sdavidch            data[i] = (CHIP_IS_E1(sc)) ?
1601255736Sdavidch                          bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1602255736Sdavidch                          REG_RD(sc, (src_addr + (i * 4)));
1603255736Sdavidch        }
1604255736Sdavidch
1605255736Sdavidch        return;
1606255736Sdavidch    }
1607255736Sdavidch
1608255736Sdavidch    /* set opcode and fixed command fields */
1609255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1610255736Sdavidch
1611255736Sdavidch    /* fill in addresses and len */
1612255736Sdavidch    dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1613255736Sdavidch    dmae.src_addr_hi = 0;
1614255736Sdavidch    dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1615255736Sdavidch    dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1616255736Sdavidch    dmae.len         = len32;
1617255736Sdavidch
1618255736Sdavidch    /* issue the command and wait for completion */
1619255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1620255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1621255736Sdavidch    };
1622255736Sdavidch}
1623255736Sdavidch
1624255736Sdavidchvoid
1625255736Sdavidchbxe_write_dmae(struct bxe_softc *sc,
1626255736Sdavidch               bus_addr_t       dma_addr,
1627255736Sdavidch               uint32_t         dst_addr,
1628255736Sdavidch               uint32_t         len32)
1629255736Sdavidch{
1630296071Sdavidcs    struct dmae_cmd dmae;
1631255736Sdavidch    int rc;
1632255736Sdavidch
1633255736Sdavidch    if (!sc->dmae_ready) {
1634255736Sdavidch        DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1635255736Sdavidch
1636255736Sdavidch        if (CHIP_IS_E1(sc)) {
1637255736Sdavidch            ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1638255736Sdavidch        } else {
1639255736Sdavidch            ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1640255736Sdavidch        }
1641255736Sdavidch
1642255736Sdavidch        return;
1643255736Sdavidch    }
1644255736Sdavidch
1645255736Sdavidch    /* set opcode and fixed command fields */
1646255736Sdavidch    bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1647255736Sdavidch
1648255736Sdavidch    /* fill in addresses and len */
1649255736Sdavidch    dmae.src_addr_lo = U64_LO(dma_addr);
1650255736Sdavidch    dmae.src_addr_hi = U64_HI(dma_addr);
1651255736Sdavidch    dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1652255736Sdavidch    dmae.dst_addr_hi = 0;
1653255736Sdavidch    dmae.len         = len32;
1654255736Sdavidch
1655255736Sdavidch    /* issue the command and wait for completion */
1656255736Sdavidch    if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1657255736Sdavidch        bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1658255736Sdavidch    }
1659255736Sdavidch}
1660255736Sdavidch
1661255736Sdavidchvoid
1662255736Sdavidchbxe_write_dmae_phys_len(struct bxe_softc *sc,
1663255736Sdavidch                        bus_addr_t       phys_addr,
1664255736Sdavidch                        uint32_t         addr,
1665255736Sdavidch                        uint32_t         len)
1666255736Sdavidch{
1667255736Sdavidch    int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1668255736Sdavidch    int offset = 0;
1669255736Sdavidch
1670255736Sdavidch    while (len > dmae_wr_max) {
1671255736Sdavidch        bxe_write_dmae(sc,
1672255736Sdavidch                       (phys_addr + offset), /* src DMA address */
1673255736Sdavidch                       (addr + offset),      /* dst GRC address */
1674255736Sdavidch                       dmae_wr_max);
1675255736Sdavidch        offset += (dmae_wr_max * 4);
1676255736Sdavidch        len -= dmae_wr_max;
1677255736Sdavidch    }
1678255736Sdavidch
1679255736Sdavidch    bxe_write_dmae(sc,
1680255736Sdavidch                   (phys_addr + offset), /* src DMA address */
1681255736Sdavidch                   (addr + offset),      /* dst GRC address */
1682255736Sdavidch                   len);
1683255736Sdavidch}
1684255736Sdavidch
1685255736Sdavidchvoid
1686255736Sdavidchbxe_set_ctx_validation(struct bxe_softc   *sc,
1687255736Sdavidch                       struct eth_context *cxt,
1688255736Sdavidch                       uint32_t           cid)
1689255736Sdavidch{
1690255736Sdavidch    /* ustorm cxt validation */
1691255736Sdavidch    cxt->ustorm_ag_context.cdu_usage =
1692255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1693255736Sdavidch            CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1694255736Sdavidch    /* xcontext validation */
1695255736Sdavidch    cxt->xstorm_ag_context.cdu_reserved =
1696255736Sdavidch        CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1697255736Sdavidch            CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1698255736Sdavidch}
1699255736Sdavidch
1700255736Sdavidchstatic void
1701255736Sdavidchbxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1702255736Sdavidch                            uint8_t          port,
1703255736Sdavidch                            uint8_t          fw_sb_id,
1704255736Sdavidch                            uint8_t          sb_index,
1705255736Sdavidch                            uint8_t          ticks)
1706255736Sdavidch{
1707255736Sdavidch    uint32_t addr =
1708255736Sdavidch        (BAR_CSTRORM_INTMEM +
1709255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1710255736Sdavidch
1711255736Sdavidch    REG_WR8(sc, addr, ticks);
1712255736Sdavidch
1713255736Sdavidch    BLOGD(sc, DBG_LOAD,
1714255736Sdavidch          "port %d fw_sb_id %d sb_index %d ticks %d\n",
1715255736Sdavidch          port, fw_sb_id, sb_index, ticks);
1716255736Sdavidch}
1717255736Sdavidch
1718255736Sdavidchstatic void
1719255736Sdavidchbxe_storm_memset_hc_disable(struct bxe_softc *sc,
1720255736Sdavidch                            uint8_t          port,
1721255736Sdavidch                            uint16_t         fw_sb_id,
1722255736Sdavidch                            uint8_t          sb_index,
1723255736Sdavidch                            uint8_t          disable)
1724255736Sdavidch{
1725255736Sdavidch    uint32_t enable_flag =
1726255736Sdavidch        (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1727255736Sdavidch    uint32_t addr =
1728255736Sdavidch        (BAR_CSTRORM_INTMEM +
1729255736Sdavidch         CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1730255736Sdavidch    uint8_t flags;
1731255736Sdavidch
1732255736Sdavidch    /* clear and set */
1733255736Sdavidch    flags = REG_RD8(sc, addr);
1734255736Sdavidch    flags &= ~HC_INDEX_DATA_HC_ENABLED;
1735255736Sdavidch    flags |= enable_flag;
1736255736Sdavidch    REG_WR8(sc, addr, flags);
1737255736Sdavidch
1738255736Sdavidch    BLOGD(sc, DBG_LOAD,
1739255736Sdavidch          "port %d fw_sb_id %d sb_index %d disable %d\n",
1740255736Sdavidch          port, fw_sb_id, sb_index, disable);
1741255736Sdavidch}
1742255736Sdavidch
1743255736Sdavidchvoid
1744255736Sdavidchbxe_update_coalesce_sb_index(struct bxe_softc *sc,
1745255736Sdavidch                             uint8_t          fw_sb_id,
1746255736Sdavidch                             uint8_t          sb_index,
1747255736Sdavidch                             uint8_t          disable,
1748255736Sdavidch                             uint16_t         usec)
1749255736Sdavidch{
1750255736Sdavidch    int port = SC_PORT(sc);
1751255736Sdavidch    uint8_t ticks = (usec / 4); /* XXX ??? */
1752255736Sdavidch
1753255736Sdavidch    bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1754255736Sdavidch
1755255736Sdavidch    disable = (disable) ? 1 : ((usec) ? 0 : 1);
1756255736Sdavidch    bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1757255736Sdavidch}
1758255736Sdavidch
1759255736Sdavidchvoid
1760255736Sdavidchelink_cb_udelay(struct bxe_softc *sc,
1761255736Sdavidch                uint32_t         usecs)
1762255736Sdavidch{
1763255736Sdavidch    DELAY(usecs);
1764255736Sdavidch}
1765255736Sdavidch
1766255736Sdavidchuint32_t
1767255736Sdavidchelink_cb_reg_read(struct bxe_softc *sc,
1768255736Sdavidch                  uint32_t         reg_addr)
1769255736Sdavidch{
1770255736Sdavidch    return (REG_RD(sc, reg_addr));
1771255736Sdavidch}
1772255736Sdavidch
1773255736Sdavidchvoid
1774255736Sdavidchelink_cb_reg_write(struct bxe_softc *sc,
1775255736Sdavidch                   uint32_t         reg_addr,
1776255736Sdavidch                   uint32_t         val)
1777255736Sdavidch{
1778255736Sdavidch    REG_WR(sc, reg_addr, val);
1779255736Sdavidch}
1780255736Sdavidch
1781255736Sdavidchvoid
1782255736Sdavidchelink_cb_reg_wb_write(struct bxe_softc *sc,
1783255736Sdavidch                      uint32_t         offset,
1784255736Sdavidch                      uint32_t         *wb_write,
1785255736Sdavidch                      uint16_t         len)
1786255736Sdavidch{
1787255736Sdavidch    REG_WR_DMAE(sc, offset, wb_write, len);
1788255736Sdavidch}
1789255736Sdavidch
1790255736Sdavidchvoid
1791255736Sdavidchelink_cb_reg_wb_read(struct bxe_softc *sc,
1792255736Sdavidch                     uint32_t         offset,
1793255736Sdavidch                     uint32_t         *wb_write,
1794255736Sdavidch                     uint16_t         len)
1795255736Sdavidch{
1796255736Sdavidch    REG_RD_DMAE(sc, offset, wb_write, len);
1797255736Sdavidch}
1798255736Sdavidch
1799255736Sdavidchuint8_t
1800255736Sdavidchelink_cb_path_id(struct bxe_softc *sc)
1801255736Sdavidch{
1802255736Sdavidch    return (SC_PATH(sc));
1803255736Sdavidch}
1804255736Sdavidch
1805255736Sdavidchvoid
1806255736Sdavidchelink_cb_event_log(struct bxe_softc     *sc,
1807255736Sdavidch                   const elink_log_id_t elink_log_id,
1808255736Sdavidch                   ...)
1809255736Sdavidch{
1810255736Sdavidch    /* XXX */
1811255736Sdavidch    BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1812255736Sdavidch}
1813255736Sdavidch
1814255736Sdavidchstatic int
1815255736Sdavidchbxe_set_spio(struct bxe_softc *sc,
1816255736Sdavidch             int              spio,
1817255736Sdavidch             uint32_t         mode)
1818255736Sdavidch{
1819255736Sdavidch    uint32_t spio_reg;
1820255736Sdavidch
1821255736Sdavidch    /* Only 2 SPIOs are configurable */
1822255736Sdavidch    if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1823295830Sdavidcs        BLOGE(sc, "Invalid SPIO 0x%x mode 0x%x\n", spio, mode);
1824255736Sdavidch        return (-1);
1825255736Sdavidch    }
1826255736Sdavidch
1827255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1828255736Sdavidch
1829255736Sdavidch    /* read SPIO and mask except the float bits */
1830255736Sdavidch    spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1831255736Sdavidch
1832255736Sdavidch    switch (mode) {
1833255736Sdavidch    case MISC_SPIO_OUTPUT_LOW:
1834255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1835255736Sdavidch        /* clear FLOAT and set CLR */
1836255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1837255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1838255736Sdavidch        break;
1839255736Sdavidch
1840255736Sdavidch    case MISC_SPIO_OUTPUT_HIGH:
1841255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1842255736Sdavidch        /* clear FLOAT and set SET */
1843255736Sdavidch        spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1844255736Sdavidch        spio_reg |=  (spio << MISC_SPIO_SET_POS);
1845255736Sdavidch        break;
1846255736Sdavidch
1847255736Sdavidch    case MISC_SPIO_INPUT_HI_Z:
1848255736Sdavidch        BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1849255736Sdavidch        /* set FLOAT */
1850255736Sdavidch        spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1851255736Sdavidch        break;
1852255736Sdavidch
1853255736Sdavidch    default:
1854255736Sdavidch        break;
1855255736Sdavidch    }
1856255736Sdavidch
1857255736Sdavidch    REG_WR(sc, MISC_REG_SPIO, spio_reg);
1858255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1859255736Sdavidch
1860255736Sdavidch    return (0);
1861255736Sdavidch}
1862255736Sdavidch
1863255736Sdavidchstatic int
1864255736Sdavidchbxe_gpio_read(struct bxe_softc *sc,
1865255736Sdavidch              int              gpio_num,
1866255736Sdavidch              uint8_t          port)
1867255736Sdavidch{
1868255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1869255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1870255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1871255736Sdavidch    int gpio_shift = (gpio_num +
1872255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1873255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1874255736Sdavidch    uint32_t gpio_reg;
1875255736Sdavidch
1876255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1877295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d port 0x%x gpio_port %d gpio_shift %d"
1878295830Sdavidcs            " gpio_mask 0x%x\n", gpio_num, port, gpio_port, gpio_shift,
1879295830Sdavidcs            gpio_mask);
1880255736Sdavidch        return (-1);
1881255736Sdavidch    }
1882255736Sdavidch
1883255736Sdavidch    /* read GPIO value */
1884255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1885255736Sdavidch
1886255736Sdavidch    /* get the requested pin value */
1887255736Sdavidch    return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
1888255736Sdavidch}
1889255736Sdavidch
1890255736Sdavidchstatic int
1891255736Sdavidchbxe_gpio_write(struct bxe_softc *sc,
1892255736Sdavidch               int              gpio_num,
1893255736Sdavidch               uint32_t         mode,
1894255736Sdavidch               uint8_t          port)
1895255736Sdavidch{
1896255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
1897255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1898255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1899255736Sdavidch    int gpio_shift = (gpio_num +
1900255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1901255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
1902255736Sdavidch    uint32_t gpio_reg;
1903255736Sdavidch
1904255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
1905295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
1906295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
1907295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
1908255736Sdavidch        return (-1);
1909255736Sdavidch    }
1910255736Sdavidch
1911255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1912255736Sdavidch
1913255736Sdavidch    /* read GPIO and mask except the float bits */
1914255736Sdavidch    gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1915255736Sdavidch
1916255736Sdavidch    switch (mode) {
1917255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1918255736Sdavidch        BLOGD(sc, DBG_PHY,
1919255736Sdavidch              "Set GPIO %d (shift %d) -> output low\n",
1920255736Sdavidch              gpio_num, gpio_shift);
1921255736Sdavidch        /* clear FLOAT and set CLR */
1922255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1923255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1924255736Sdavidch        break;
1925255736Sdavidch
1926255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1927255736Sdavidch        BLOGD(sc, DBG_PHY,
1928255736Sdavidch              "Set GPIO %d (shift %d) -> output high\n",
1929255736Sdavidch              gpio_num, gpio_shift);
1930255736Sdavidch        /* clear FLOAT and set SET */
1931255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1932255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1933255736Sdavidch        break;
1934255736Sdavidch
1935255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1936255736Sdavidch        BLOGD(sc, DBG_PHY,
1937255736Sdavidch              "Set GPIO %d (shift %d) -> input\n",
1938255736Sdavidch              gpio_num, gpio_shift);
1939255736Sdavidch        /* set FLOAT */
1940255736Sdavidch        gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1941255736Sdavidch        break;
1942255736Sdavidch
1943255736Sdavidch    default:
1944255736Sdavidch        break;
1945255736Sdavidch    }
1946255736Sdavidch
1947255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1948255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1949255736Sdavidch
1950255736Sdavidch    return (0);
1951255736Sdavidch}
1952255736Sdavidch
1953255736Sdavidchstatic int
1954255736Sdavidchbxe_gpio_mult_write(struct bxe_softc *sc,
1955255736Sdavidch                    uint8_t          pins,
1956255736Sdavidch                    uint32_t         mode)
1957255736Sdavidch{
1958255736Sdavidch    uint32_t gpio_reg;
1959255736Sdavidch
1960255736Sdavidch    /* any port swapping should be handled by caller */
1961255736Sdavidch
1962255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1963255736Sdavidch
1964255736Sdavidch    /* read GPIO and mask except the float bits */
1965255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1966255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1967255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
1968255736Sdavidch    gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
1969255736Sdavidch
1970255736Sdavidch    switch (mode) {
1971255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1972255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
1973255736Sdavidch        /* set CLR */
1974255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
1975255736Sdavidch        break;
1976255736Sdavidch
1977255736Sdavidch    case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1978255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
1979255736Sdavidch        /* set SET */
1980255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
1981255736Sdavidch        break;
1982255736Sdavidch
1983255736Sdavidch    case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1984255736Sdavidch        BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
1985255736Sdavidch        /* set FLOAT */
1986255736Sdavidch        gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1987255736Sdavidch        break;
1988255736Sdavidch
1989255736Sdavidch    default:
1990295830Sdavidcs        BLOGE(sc, "Invalid GPIO mode assignment pins 0x%x mode 0x%x"
1991295830Sdavidcs            " gpio_reg 0x%x\n", pins, mode, gpio_reg);
1992255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1993255736Sdavidch        return (-1);
1994255736Sdavidch    }
1995255736Sdavidch
1996255736Sdavidch    REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1997255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1998255736Sdavidch
1999255736Sdavidch    return (0);
2000255736Sdavidch}
2001255736Sdavidch
2002255736Sdavidchstatic int
2003255736Sdavidchbxe_gpio_int_write(struct bxe_softc *sc,
2004255736Sdavidch                   int              gpio_num,
2005255736Sdavidch                   uint32_t         mode,
2006255736Sdavidch                   uint8_t          port)
2007255736Sdavidch{
2008255736Sdavidch    /* The GPIO should be swapped if swap register is set and active */
2009255736Sdavidch    int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2010255736Sdavidch                      REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2011255736Sdavidch    int gpio_shift = (gpio_num +
2012255736Sdavidch                      (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2013255736Sdavidch    uint32_t gpio_mask = (1 << gpio_shift);
2014255736Sdavidch    uint32_t gpio_reg;
2015255736Sdavidch
2016255736Sdavidch    if (gpio_num > MISC_REGISTERS_GPIO_3) {
2017295830Sdavidcs        BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
2018295830Sdavidcs            " gpio_shift %d gpio_mask 0x%x\n",
2019295830Sdavidcs            gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
2020255736Sdavidch        return (-1);
2021255736Sdavidch    }
2022255736Sdavidch
2023255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2024255736Sdavidch
2025255736Sdavidch    /* read GPIO int */
2026255736Sdavidch    gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2027255736Sdavidch
2028255736Sdavidch    switch (mode) {
2029255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2030255736Sdavidch        BLOGD(sc, DBG_PHY,
2031255736Sdavidch              "Clear GPIO INT %d (shift %d) -> output low\n",
2032255736Sdavidch              gpio_num, gpio_shift);
2033255736Sdavidch        /* clear SET and set CLR */
2034255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2035255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2036255736Sdavidch        break;
2037255736Sdavidch
2038255736Sdavidch    case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2039255736Sdavidch        BLOGD(sc, DBG_PHY,
2040255736Sdavidch              "Set GPIO INT %d (shift %d) -> output high\n",
2041255736Sdavidch              gpio_num, gpio_shift);
2042255736Sdavidch        /* clear CLR and set SET */
2043255736Sdavidch        gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2044255736Sdavidch        gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2045255736Sdavidch        break;
2046255736Sdavidch
2047255736Sdavidch    default:
2048255736Sdavidch        break;
2049255736Sdavidch    }
2050255736Sdavidch
2051255736Sdavidch    REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2052255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2053255736Sdavidch
2054255736Sdavidch    return (0);
2055255736Sdavidch}
2056255736Sdavidch
2057255736Sdavidchuint32_t
2058255736Sdavidchelink_cb_gpio_read(struct bxe_softc *sc,
2059255736Sdavidch                   uint16_t         gpio_num,
2060255736Sdavidch                   uint8_t          port)
2061255736Sdavidch{
2062255736Sdavidch    return (bxe_gpio_read(sc, gpio_num, port));
2063255736Sdavidch}
2064255736Sdavidch
2065255736Sdavidchuint8_t
2066255736Sdavidchelink_cb_gpio_write(struct bxe_softc *sc,
2067255736Sdavidch                    uint16_t         gpio_num,
2068255736Sdavidch                    uint8_t          mode, /* 0=low 1=high */
2069255736Sdavidch                    uint8_t          port)
2070255736Sdavidch{
2071255736Sdavidch    return (bxe_gpio_write(sc, gpio_num, mode, port));
2072255736Sdavidch}
2073255736Sdavidch
2074255736Sdavidchuint8_t
2075255736Sdavidchelink_cb_gpio_mult_write(struct bxe_softc *sc,
2076255736Sdavidch                         uint8_t          pins,
2077255736Sdavidch                         uint8_t          mode) /* 0=low 1=high */
2078255736Sdavidch{
2079255736Sdavidch    return (bxe_gpio_mult_write(sc, pins, mode));
2080255736Sdavidch}
2081255736Sdavidch
2082255736Sdavidchuint8_t
2083255736Sdavidchelink_cb_gpio_int_write(struct bxe_softc *sc,
2084255736Sdavidch                        uint16_t         gpio_num,
2085255736Sdavidch                        uint8_t          mode, /* 0=low 1=high */
2086255736Sdavidch                        uint8_t          port)
2087255736Sdavidch{
2088255736Sdavidch    return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2089255736Sdavidch}
2090255736Sdavidch
2091255736Sdavidchvoid
2092255736Sdavidchelink_cb_notify_link_changed(struct bxe_softc *sc)
2093255736Sdavidch{
2094255736Sdavidch    REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2095255736Sdavidch                (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2096255736Sdavidch}
2097255736Sdavidch
2098255736Sdavidch/* send the MCP a request, block until there is a reply */
2099255736Sdavidchuint32_t
2100255736Sdavidchelink_cb_fw_command(struct bxe_softc *sc,
2101255736Sdavidch                    uint32_t         command,
2102255736Sdavidch                    uint32_t         param)
2103255736Sdavidch{
2104255736Sdavidch    int mb_idx = SC_FW_MB_IDX(sc);
2105255736Sdavidch    uint32_t seq;
2106255736Sdavidch    uint32_t rc = 0;
2107255736Sdavidch    uint32_t cnt = 1;
2108255736Sdavidch    uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2109255736Sdavidch
2110255736Sdavidch    BXE_FWMB_LOCK(sc);
2111255736Sdavidch
2112255736Sdavidch    seq = ++sc->fw_seq;
2113255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2114255736Sdavidch    SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2115255736Sdavidch
2116255736Sdavidch    BLOGD(sc, DBG_PHY,
2117255736Sdavidch          "wrote command 0x%08x to FW MB param 0x%08x\n",
2118255736Sdavidch          (command | seq), param);
2119255736Sdavidch
2120255736Sdavidch    /* Let the FW do it's magic. GIve it up to 5 seconds... */
2121255736Sdavidch    do {
2122255736Sdavidch        DELAY(delay * 1000);
2123255736Sdavidch        rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2124255736Sdavidch    } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2125255736Sdavidch
2126255736Sdavidch    BLOGD(sc, DBG_PHY,
2127255736Sdavidch          "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2128255736Sdavidch          cnt*delay, rc, seq);
2129255736Sdavidch
2130255736Sdavidch    /* is this a reply to our command? */
2131255736Sdavidch    if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2132255736Sdavidch        rc &= FW_MSG_CODE_MASK;
2133255736Sdavidch    } else {
2134255736Sdavidch        /* Ruh-roh! */
2135255736Sdavidch        BLOGE(sc, "FW failed to respond!\n");
2136255736Sdavidch        // XXX bxe_fw_dump(sc);
2137255736Sdavidch        rc = 0;
2138255736Sdavidch    }
2139255736Sdavidch
2140255736Sdavidch    BXE_FWMB_UNLOCK(sc);
2141255736Sdavidch    return (rc);
2142255736Sdavidch}
2143255736Sdavidch
2144255736Sdavidchstatic uint32_t
2145255736Sdavidchbxe_fw_command(struct bxe_softc *sc,
2146255736Sdavidch               uint32_t         command,
2147255736Sdavidch               uint32_t         param)
2148255736Sdavidch{
2149255736Sdavidch    return (elink_cb_fw_command(sc, command, param));
2150255736Sdavidch}
2151255736Sdavidch
2152255736Sdavidchstatic void
2153255736Sdavidch__storm_memset_dma_mapping(struct bxe_softc *sc,
2154255736Sdavidch                           uint32_t         addr,
2155255736Sdavidch                           bus_addr_t       mapping)
2156255736Sdavidch{
2157255736Sdavidch    REG_WR(sc, addr, U64_LO(mapping));
2158255736Sdavidch    REG_WR(sc, (addr + 4), U64_HI(mapping));
2159255736Sdavidch}
2160255736Sdavidch
2161255736Sdavidchstatic void
2162255736Sdavidchstorm_memset_spq_addr(struct bxe_softc *sc,
2163255736Sdavidch                      bus_addr_t       mapping,
2164255736Sdavidch                      uint16_t         abs_fid)
2165255736Sdavidch{
2166255736Sdavidch    uint32_t addr = (XSEM_REG_FAST_MEMORY +
2167255736Sdavidch                     XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2168255736Sdavidch    __storm_memset_dma_mapping(sc, addr, mapping);
2169255736Sdavidch}
2170255736Sdavidch
2171255736Sdavidchstatic void
2172255736Sdavidchstorm_memset_vf_to_pf(struct bxe_softc *sc,
2173255736Sdavidch                      uint16_t         abs_fid,
2174255736Sdavidch                      uint16_t         pf_id)
2175255736Sdavidch{
2176255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2177255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2178255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2179255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2180255736Sdavidch}
2181255736Sdavidch
2182255736Sdavidchstatic void
2183255736Sdavidchstorm_memset_func_en(struct bxe_softc *sc,
2184255736Sdavidch                     uint16_t         abs_fid,
2185255736Sdavidch                     uint8_t          enable)
2186255736Sdavidch{
2187255736Sdavidch    REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2188255736Sdavidch    REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2189255736Sdavidch    REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2190255736Sdavidch    REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2191255736Sdavidch}
2192255736Sdavidch
2193255736Sdavidchstatic void
2194255736Sdavidchstorm_memset_eq_data(struct bxe_softc       *sc,
2195255736Sdavidch                     struct event_ring_data *eq_data,
2196255736Sdavidch                     uint16_t               pfid)
2197255736Sdavidch{
2198255736Sdavidch    uint32_t addr;
2199255736Sdavidch    size_t size;
2200255736Sdavidch
2201255736Sdavidch    addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2202255736Sdavidch    size = sizeof(struct event_ring_data);
2203255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2204255736Sdavidch}
2205255736Sdavidch
2206255736Sdavidchstatic void
2207255736Sdavidchstorm_memset_eq_prod(struct bxe_softc *sc,
2208255736Sdavidch                     uint16_t         eq_prod,
2209255736Sdavidch                     uint16_t         pfid)
2210255736Sdavidch{
2211255736Sdavidch    uint32_t addr = (BAR_CSTRORM_INTMEM +
2212255736Sdavidch                     CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2213255736Sdavidch    REG_WR16(sc, addr, eq_prod);
2214255736Sdavidch}
2215255736Sdavidch
2216255736Sdavidch/*
2217255736Sdavidch * Post a slowpath command.
2218255736Sdavidch *
2219255736Sdavidch * A slowpath command is used to propogate a configuration change through
2220255736Sdavidch * the controller in a controlled manner, allowing each STORM processor and
2221255736Sdavidch * other H/W blocks to phase in the change.  The commands sent on the
2222255736Sdavidch * slowpath are referred to as ramrods.  Depending on the ramrod used the
2223255736Sdavidch * completion of the ramrod will occur in different ways.  Here's a
2224255736Sdavidch * breakdown of ramrods and how they complete:
2225255736Sdavidch *
2226255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_SETUP
2227255736Sdavidch *   Used to setup the leading connection on a port.  Completes on the
2228255736Sdavidch *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2229255736Sdavidch *
2230255736Sdavidch * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2231255736Sdavidch *   Used to setup an additional connection on a port.  Completes on the
2232255736Sdavidch *   RCQ of the multi-queue/RSS connection being initialized.
2233255736Sdavidch *
2234255736Sdavidch * RAMROD_CMD_ID_ETH_STAT_QUERY
2235255736Sdavidch *   Used to force the storm processors to update the statistics database
2236255736Sdavidch *   in host memory.  This ramrod is send on the leading connection CID and
2237255736Sdavidch *   completes as an index increment of the CSTORM on the default status
2238255736Sdavidch *   block.
2239255736Sdavidch *
2240255736Sdavidch * RAMROD_CMD_ID_ETH_UPDATE
2241255736Sdavidch *   Used to update the state of the leading connection, usually to udpate
2242255736Sdavidch *   the RSS indirection table.  Completes on the RCQ of the leading
2243255736Sdavidch *   connection. (Not currently used under FreeBSD until OS support becomes
2244255736Sdavidch *   available.)
2245255736Sdavidch *
2246255736Sdavidch * RAMROD_CMD_ID_ETH_HALT
2247255736Sdavidch *   Used when tearing down a connection prior to driver unload.  Completes
2248255736Sdavidch *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2249255736Sdavidch *   use this on the leading connection.
2250255736Sdavidch *
2251255736Sdavidch * RAMROD_CMD_ID_ETH_SET_MAC
2252255736Sdavidch *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2253255736Sdavidch *   the RCQ of the leading connection.
2254255736Sdavidch *
2255255736Sdavidch * RAMROD_CMD_ID_ETH_CFC_DEL
2256255736Sdavidch *   Used when tearing down a conneciton prior to driver unload.  Completes
2257255736Sdavidch *   on the RCQ of the leading connection (since the current connection
2258255736Sdavidch *   has been completely removed from controller memory).
2259255736Sdavidch *
2260255736Sdavidch * RAMROD_CMD_ID_ETH_PORT_DEL
2261255736Sdavidch *   Used to tear down the leading connection prior to driver unload,
2262255736Sdavidch *   typically fp[0].  Completes as an index increment of the CSTORM on the
2263255736Sdavidch *   default status block.
2264255736Sdavidch *
2265255736Sdavidch * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2266255736Sdavidch *   Used for connection offload.  Completes on the RCQ of the multi-queue
2267255736Sdavidch *   RSS connection that is being offloaded.  (Not currently used under
2268255736Sdavidch *   FreeBSD.)
2269255736Sdavidch *
2270255736Sdavidch * There can only be one command pending per function.
2271255736Sdavidch *
2272255736Sdavidch * Returns:
2273255736Sdavidch *   0 = Success, !0 = Failure.
2274255736Sdavidch */
2275255736Sdavidch
2276255736Sdavidch/* must be called under the spq lock */
2277255736Sdavidchstatic inline
2278255736Sdavidchstruct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2279255736Sdavidch{
2280255736Sdavidch    struct eth_spe *next_spe = sc->spq_prod_bd;
2281255736Sdavidch
2282255736Sdavidch    if (sc->spq_prod_bd == sc->spq_last_bd) {
2283255736Sdavidch        /* wrap back to the first eth_spq */
2284255736Sdavidch        sc->spq_prod_bd = sc->spq;
2285255736Sdavidch        sc->spq_prod_idx = 0;
2286255736Sdavidch    } else {
2287255736Sdavidch        sc->spq_prod_bd++;
2288255736Sdavidch        sc->spq_prod_idx++;
2289255736Sdavidch    }
2290255736Sdavidch
2291255736Sdavidch    return (next_spe);
2292255736Sdavidch}
2293255736Sdavidch
2294255736Sdavidch/* must be called under the spq lock */
2295255736Sdavidchstatic inline
2296255736Sdavidchvoid bxe_sp_prod_update(struct bxe_softc *sc)
2297255736Sdavidch{
2298255736Sdavidch    int func = SC_FUNC(sc);
2299255736Sdavidch
2300255736Sdavidch    /*
2301255736Sdavidch     * Make sure that BD data is updated before writing the producer.
2302255736Sdavidch     * BD data is written to the memory, the producer is read from the
2303255736Sdavidch     * memory, thus we need a full memory barrier to ensure the ordering.
2304255736Sdavidch     */
2305255736Sdavidch    mb();
2306255736Sdavidch
2307255736Sdavidch    REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2308255736Sdavidch             sc->spq_prod_idx);
2309255736Sdavidch
2310255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2311255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
2312255736Sdavidch}
2313255736Sdavidch
2314255736Sdavidch/**
2315255736Sdavidch * bxe_is_contextless_ramrod - check if the current command ends on EQ
2316255736Sdavidch *
2317255736Sdavidch * @cmd:      command to check
2318255736Sdavidch * @cmd_type: command type
2319255736Sdavidch */
2320255736Sdavidchstatic inline
2321255736Sdavidchint bxe_is_contextless_ramrod(int cmd,
2322255736Sdavidch                              int cmd_type)
2323255736Sdavidch{
2324255736Sdavidch    if ((cmd_type == NONE_CONNECTION_TYPE) ||
2325255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2326255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2327255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2328255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2329255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2330255736Sdavidch        (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2331255736Sdavidch        return (TRUE);
2332255736Sdavidch    } else {
2333255736Sdavidch        return (FALSE);
2334255736Sdavidch    }
2335255736Sdavidch}
2336255736Sdavidch
2337255736Sdavidch/**
2338255736Sdavidch * bxe_sp_post - place a single command on an SP ring
2339255736Sdavidch *
2340255736Sdavidch * @sc:         driver handle
2341255736Sdavidch * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2342255736Sdavidch * @cid:        SW CID the command is related to
2343255736Sdavidch * @data_hi:    command private data address (high 32 bits)
2344255736Sdavidch * @data_lo:    command private data address (low 32 bits)
2345255736Sdavidch * @cmd_type:   command type (e.g. NONE, ETH)
2346255736Sdavidch *
2347255736Sdavidch * SP data is handled as if it's always an address pair, thus data fields are
2348255736Sdavidch * not swapped to little endian in upper functions. Instead this function swaps
2349255736Sdavidch * data as if it's two uint32 fields.
2350255736Sdavidch */
2351255736Sdavidchint
2352255736Sdavidchbxe_sp_post(struct bxe_softc *sc,
2353255736Sdavidch            int              command,
2354255736Sdavidch            int              cid,
2355255736Sdavidch            uint32_t         data_hi,
2356255736Sdavidch            uint32_t         data_lo,
2357255736Sdavidch            int              cmd_type)
2358255736Sdavidch{
2359255736Sdavidch    struct eth_spe *spe;
2360255736Sdavidch    uint16_t type;
2361255736Sdavidch    int common;
2362255736Sdavidch
2363255736Sdavidch    common = bxe_is_contextless_ramrod(command, cmd_type);
2364255736Sdavidch
2365255736Sdavidch    BXE_SP_LOCK(sc);
2366255736Sdavidch
2367255736Sdavidch    if (common) {
2368255736Sdavidch        if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2369255736Sdavidch            BLOGE(sc, "EQ ring is full!\n");
2370255736Sdavidch            BXE_SP_UNLOCK(sc);
2371255736Sdavidch            return (-1);
2372255736Sdavidch        }
2373255736Sdavidch    } else {
2374255736Sdavidch        if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2375255736Sdavidch            BLOGE(sc, "SPQ ring is full!\n");
2376255736Sdavidch            BXE_SP_UNLOCK(sc);
2377255736Sdavidch            return (-1);
2378255736Sdavidch        }
2379255736Sdavidch    }
2380255736Sdavidch
2381255736Sdavidch    spe = bxe_sp_get_next(sc);
2382255736Sdavidch
2383255736Sdavidch    /* CID needs port number to be encoded int it */
2384255736Sdavidch    spe->hdr.conn_and_cmd_data =
2385296071Sdavidcs        htole32((command << SPE_HDR_T_CMD_ID_SHIFT) | HW_CID(sc, cid));
2386255736Sdavidch
2387296071Sdavidcs    type = (cmd_type << SPE_HDR_T_CONN_TYPE_SHIFT) & SPE_HDR_T_CONN_TYPE;
2388255736Sdavidch
2389255736Sdavidch    /* TBD: Check if it works for VFs */
2390296071Sdavidcs    type |= ((SC_FUNC(sc) << SPE_HDR_T_FUNCTION_ID_SHIFT) &
2391296071Sdavidcs             SPE_HDR_T_FUNCTION_ID);
2392255736Sdavidch
2393255736Sdavidch    spe->hdr.type = htole16(type);
2394255736Sdavidch
2395255736Sdavidch    spe->data.update_data_addr.hi = htole32(data_hi);
2396255736Sdavidch    spe->data.update_data_addr.lo = htole32(data_lo);
2397255736Sdavidch
2398255736Sdavidch    /*
2399255736Sdavidch     * It's ok if the actual decrement is issued towards the memory
2400255736Sdavidch     * somewhere between the lock and unlock. Thus no more explict
2401255736Sdavidch     * memory barrier is needed.
2402255736Sdavidch     */
2403255736Sdavidch    if (common) {
2404255736Sdavidch        atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2405255736Sdavidch    } else {
2406255736Sdavidch        atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2407255736Sdavidch    }
2408255736Sdavidch
2409256341Sdim    BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2410256341Sdim    BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2411256341Sdim          BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2412255736Sdavidch    BLOGD(sc, DBG_SP,
2413255736Sdavidch          "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2414255736Sdavidch          sc->spq_prod_idx,
2415255736Sdavidch          (uint32_t)U64_HI(sc->spq_dma.paddr),
2416255736Sdavidch          (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2417255736Sdavidch          command,
2418255736Sdavidch          common,
2419255736Sdavidch          HW_CID(sc, cid),
2420255736Sdavidch          data_hi,
2421255736Sdavidch          data_lo,
2422255736Sdavidch          type,
2423255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left),
2424255736Sdavidch          atomic_load_acq_long(&sc->eq_spq_left));
2425255736Sdavidch
2426255736Sdavidch    bxe_sp_prod_update(sc);
2427255736Sdavidch
2428255736Sdavidch    BXE_SP_UNLOCK(sc);
2429255736Sdavidch    return (0);
2430255736Sdavidch}
2431255736Sdavidch
2432255736Sdavidch/**
2433255736Sdavidch * bxe_debug_print_ind_table - prints the indirection table configuration.
2434255736Sdavidch *
2435255736Sdavidch * @sc: driver hanlde
2436255736Sdavidch * @p:  pointer to rss configuration
2437255736Sdavidch */
2438255736Sdavidch
2439255736Sdavidch/*
2440255736Sdavidch * FreeBSD Device probe function.
2441255736Sdavidch *
2442255736Sdavidch * Compares the device found to the driver's list of supported devices and
2443255736Sdavidch * reports back to the bsd loader whether this is the right driver for the device.
2444255736Sdavidch * This is the driver entry function called from the "kldload" command.
2445255736Sdavidch *
2446255736Sdavidch * Returns:
2447255736Sdavidch *   BUS_PROBE_DEFAULT on success, positive value on failure.
2448255736Sdavidch */
2449255736Sdavidchstatic int
2450255736Sdavidchbxe_probe(device_t dev)
2451255736Sdavidch{
2452255736Sdavidch    struct bxe_softc *sc;
2453255736Sdavidch    struct bxe_device_type *t;
2454255736Sdavidch    char *descbuf;
2455255736Sdavidch    uint16_t did, sdid, svid, vid;
2456255736Sdavidch
2457255736Sdavidch    /* Find our device structure */
2458255736Sdavidch    sc = device_get_softc(dev);
2459255736Sdavidch    sc->dev = dev;
2460255736Sdavidch    t = bxe_devs;
2461255736Sdavidch
2462255736Sdavidch    /* Get the data for the device to be probed. */
2463255736Sdavidch    vid  = pci_get_vendor(dev);
2464255736Sdavidch    did  = pci_get_device(dev);
2465255736Sdavidch    svid = pci_get_subvendor(dev);
2466255736Sdavidch    sdid = pci_get_subdevice(dev);
2467255736Sdavidch
2468255736Sdavidch    BLOGD(sc, DBG_LOAD,
2469255736Sdavidch          "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
2470255736Sdavidch          "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
2471255736Sdavidch
2472255736Sdavidch    /* Look through the list of known devices for a match. */
2473255736Sdavidch    while (t->bxe_name != NULL) {
2474255736Sdavidch        if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2475255736Sdavidch            ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2476255736Sdavidch            ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2477255736Sdavidch            descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2478255736Sdavidch            if (descbuf == NULL)
2479255736Sdavidch                return (ENOMEM);
2480255736Sdavidch
2481255736Sdavidch            /* Print out the device identity. */
2482255736Sdavidch            snprintf(descbuf, BXE_DEVDESC_MAX,
2483255736Sdavidch                     "%s (%c%d) BXE v:%s\n", t->bxe_name,
2484255736Sdavidch                     (((pci_read_config(dev, PCIR_REVID, 4) &
2485255736Sdavidch                        0xf0) >> 4) + 'A'),
2486255736Sdavidch                     (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2487255736Sdavidch                     BXE_DRIVER_VERSION);
2488255736Sdavidch
2489255736Sdavidch            device_set_desc_copy(dev, descbuf);
2490255736Sdavidch            free(descbuf, M_TEMP);
2491255736Sdavidch            return (BUS_PROBE_DEFAULT);
2492255736Sdavidch        }
2493255736Sdavidch        t++;
2494255736Sdavidch    }
2495255736Sdavidch
2496255736Sdavidch    return (ENXIO);
2497255736Sdavidch}
2498255736Sdavidch
2499255736Sdavidchstatic void
2500255736Sdavidchbxe_init_mutexes(struct bxe_softc *sc)
2501255736Sdavidch{
2502255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2503255736Sdavidch    snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2504255736Sdavidch             "bxe%d_core_lock", sc->unit);
2505255736Sdavidch    sx_init(&sc->core_sx, sc->core_sx_name);
2506255736Sdavidch#else
2507255736Sdavidch    snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2508255736Sdavidch             "bxe%d_core_lock", sc->unit);
2509255736Sdavidch    mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2510255736Sdavidch#endif
2511255736Sdavidch
2512255736Sdavidch    snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2513255736Sdavidch             "bxe%d_sp_lock", sc->unit);
2514255736Sdavidch    mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2515255736Sdavidch
2516255736Sdavidch    snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2517255736Sdavidch             "bxe%d_dmae_lock", sc->unit);
2518255736Sdavidch    mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2519255736Sdavidch
2520255736Sdavidch    snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2521255736Sdavidch             "bxe%d_phy_lock", sc->unit);
2522255736Sdavidch    mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2523255736Sdavidch
2524255736Sdavidch    snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2525255736Sdavidch             "bxe%d_fwmb_lock", sc->unit);
2526255736Sdavidch    mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2527255736Sdavidch
2528255736Sdavidch    snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2529255736Sdavidch             "bxe%d_print_lock", sc->unit);
2530255736Sdavidch    mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2531255736Sdavidch
2532255736Sdavidch    snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2533255736Sdavidch             "bxe%d_stats_lock", sc->unit);
2534255736Sdavidch    mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2535255736Sdavidch
2536255736Sdavidch    snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2537255736Sdavidch             "bxe%d_mcast_lock", sc->unit);
2538255736Sdavidch    mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2539255736Sdavidch}
2540255736Sdavidch
2541255736Sdavidchstatic void
2542255736Sdavidchbxe_release_mutexes(struct bxe_softc *sc)
2543255736Sdavidch{
2544255736Sdavidch#ifdef BXE_CORE_LOCK_SX
2545255736Sdavidch    sx_destroy(&sc->core_sx);
2546255736Sdavidch#else
2547255736Sdavidch    if (mtx_initialized(&sc->core_mtx)) {
2548255736Sdavidch        mtx_destroy(&sc->core_mtx);
2549255736Sdavidch    }
2550255736Sdavidch#endif
2551255736Sdavidch
2552255736Sdavidch    if (mtx_initialized(&sc->sp_mtx)) {
2553255736Sdavidch        mtx_destroy(&sc->sp_mtx);
2554255736Sdavidch    }
2555255736Sdavidch
2556255736Sdavidch    if (mtx_initialized(&sc->dmae_mtx)) {
2557255736Sdavidch        mtx_destroy(&sc->dmae_mtx);
2558255736Sdavidch    }
2559255736Sdavidch
2560255736Sdavidch    if (mtx_initialized(&sc->port.phy_mtx)) {
2561255736Sdavidch        mtx_destroy(&sc->port.phy_mtx);
2562255736Sdavidch    }
2563255736Sdavidch
2564255736Sdavidch    if (mtx_initialized(&sc->fwmb_mtx)) {
2565255736Sdavidch        mtx_destroy(&sc->fwmb_mtx);
2566255736Sdavidch    }
2567255736Sdavidch
2568255736Sdavidch    if (mtx_initialized(&sc->print_mtx)) {
2569255736Sdavidch        mtx_destroy(&sc->print_mtx);
2570255736Sdavidch    }
2571255736Sdavidch
2572255736Sdavidch    if (mtx_initialized(&sc->stats_mtx)) {
2573255736Sdavidch        mtx_destroy(&sc->stats_mtx);
2574255736Sdavidch    }
2575255736Sdavidch
2576255736Sdavidch    if (mtx_initialized(&sc->mcast_mtx)) {
2577255736Sdavidch        mtx_destroy(&sc->mcast_mtx);
2578255736Sdavidch    }
2579255736Sdavidch}
2580255736Sdavidch
2581255736Sdavidchstatic void
2582255736Sdavidchbxe_tx_disable(struct bxe_softc* sc)
2583255736Sdavidch{
2584266979Smarcel    if_t ifp = sc->ifp;
2585255736Sdavidch
2586255736Sdavidch    /* tell the stack the driver is stopped and TX queue is full */
2587266979Smarcel    if (ifp !=  NULL) {
2588266979Smarcel        if_setdrvflags(ifp, 0);
2589255736Sdavidch    }
2590255736Sdavidch}
2591255736Sdavidch
2592255736Sdavidchstatic void
2593255736Sdavidchbxe_drv_pulse(struct bxe_softc *sc)
2594255736Sdavidch{
2595255736Sdavidch    SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2596255736Sdavidch             sc->fw_drv_pulse_wr_seq);
2597255736Sdavidch}
2598255736Sdavidch
2599255736Sdavidchstatic inline uint16_t
2600255736Sdavidchbxe_tx_avail(struct bxe_softc *sc,
2601255736Sdavidch             struct bxe_fastpath *fp)
2602255736Sdavidch{
2603255736Sdavidch    int16_t  used;
2604255736Sdavidch    uint16_t prod;
2605255736Sdavidch    uint16_t cons;
2606255736Sdavidch
2607255736Sdavidch    prod = fp->tx_bd_prod;
2608255736Sdavidch    cons = fp->tx_bd_cons;
2609255736Sdavidch
2610255736Sdavidch    used = SUB_S16(prod, cons);
2611255736Sdavidch
2612255736Sdavidch    return (int16_t)(sc->tx_ring_size) - used;
2613255736Sdavidch}
2614255736Sdavidch
2615255736Sdavidchstatic inline int
2616255736Sdavidchbxe_tx_queue_has_work(struct bxe_fastpath *fp)
2617255736Sdavidch{
2618255736Sdavidch    uint16_t hw_cons;
2619255736Sdavidch
2620255736Sdavidch    mb(); /* status block fields can change */
2621255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
2622255736Sdavidch    return (hw_cons != fp->tx_pkt_cons);
2623255736Sdavidch}
2624255736Sdavidch
2625255736Sdavidchstatic inline uint8_t
2626255736Sdavidchbxe_has_tx_work(struct bxe_fastpath *fp)
2627255736Sdavidch{
2628255736Sdavidch    /* expand this for multi-cos if ever supported */
2629255736Sdavidch    return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2630255736Sdavidch}
2631255736Sdavidch
2632255736Sdavidchstatic inline int
2633255736Sdavidchbxe_has_rx_work(struct bxe_fastpath *fp)
2634255736Sdavidch{
2635255736Sdavidch    uint16_t rx_cq_cons_sb;
2636255736Sdavidch
2637255736Sdavidch    mb(); /* status block fields can change */
2638255736Sdavidch    rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2639255736Sdavidch    if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2640255736Sdavidch        rx_cq_cons_sb++;
2641255736Sdavidch    return (fp->rx_cq_cons != rx_cq_cons_sb);
2642255736Sdavidch}
2643255736Sdavidch
2644255736Sdavidchstatic void
2645255736Sdavidchbxe_sp_event(struct bxe_softc    *sc,
2646255736Sdavidch             struct bxe_fastpath *fp,
2647255736Sdavidch             union eth_rx_cqe    *rr_cqe)
2648255736Sdavidch{
2649255736Sdavidch    int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2650255736Sdavidch    int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2651255736Sdavidch    enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2652255736Sdavidch    struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2653255736Sdavidch
2654255736Sdavidch    BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2655255736Sdavidch          fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2656255736Sdavidch
2657255736Sdavidch    switch (command) {
2658255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2659255736Sdavidch        BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2660255736Sdavidch        drv_cmd = ECORE_Q_CMD_UPDATE;
2661255736Sdavidch        break;
2662255736Sdavidch
2663255736Sdavidch    case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2664255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2665255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP;
2666255736Sdavidch        break;
2667255736Sdavidch
2668255736Sdavidch    case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2669255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2670255736Sdavidch        drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2671255736Sdavidch        break;
2672255736Sdavidch
2673255736Sdavidch    case (RAMROD_CMD_ID_ETH_HALT):
2674255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2675255736Sdavidch        drv_cmd = ECORE_Q_CMD_HALT;
2676255736Sdavidch        break;
2677255736Sdavidch
2678255736Sdavidch    case (RAMROD_CMD_ID_ETH_TERMINATE):
2679255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2680255736Sdavidch        drv_cmd = ECORE_Q_CMD_TERMINATE;
2681255736Sdavidch        break;
2682255736Sdavidch
2683255736Sdavidch    case (RAMROD_CMD_ID_ETH_EMPTY):
2684255736Sdavidch        BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2685255736Sdavidch        drv_cmd = ECORE_Q_CMD_EMPTY;
2686255736Sdavidch        break;
2687255736Sdavidch
2688255736Sdavidch    default:
2689255736Sdavidch        BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2690255736Sdavidch              command, fp->index);
2691255736Sdavidch        return;
2692255736Sdavidch    }
2693255736Sdavidch
2694255736Sdavidch    if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2695255736Sdavidch        q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2696255736Sdavidch        /*
2697255736Sdavidch         * q_obj->complete_cmd() failure means that this was
2698255736Sdavidch         * an unexpected completion.
2699255736Sdavidch         *
2700255736Sdavidch         * In this case we don't want to increase the sc->spq_left
2701255736Sdavidch         * because apparently we haven't sent this command the first
2702255736Sdavidch         * place.
2703255736Sdavidch         */
2704255736Sdavidch        // bxe_panic(sc, ("Unexpected SP completion\n"));
2705255736Sdavidch        return;
2706255736Sdavidch    }
2707255736Sdavidch
2708255736Sdavidch    atomic_add_acq_long(&sc->cq_spq_left, 1);
2709255736Sdavidch
2710255736Sdavidch    BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2711255736Sdavidch          atomic_load_acq_long(&sc->cq_spq_left));
2712255736Sdavidch}
2713255736Sdavidch
2714255736Sdavidch/*
2715255736Sdavidch * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2716255736Sdavidch * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2717255736Sdavidch * the current aggregation queue as in-progress.
2718255736Sdavidch */
2719255736Sdavidchstatic void
2720255736Sdavidchbxe_tpa_start(struct bxe_softc            *sc,
2721255736Sdavidch              struct bxe_fastpath         *fp,
2722255736Sdavidch              uint16_t                    queue,
2723255736Sdavidch              uint16_t                    cons,
2724255736Sdavidch              uint16_t                    prod,
2725255736Sdavidch              struct eth_fast_path_rx_cqe *cqe)
2726255736Sdavidch{
2727255736Sdavidch    struct bxe_sw_rx_bd tmp_bd;
2728255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
2729255736Sdavidch    struct eth_rx_bd *rx_bd;
2730255736Sdavidch    int max_agg_queues;
2731255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2732255736Sdavidch    uint16_t index;
2733255736Sdavidch
2734255736Sdavidch    BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2735255736Sdavidch                       "cons=%d prod=%d\n",
2736255736Sdavidch          fp->index, queue, cons, prod);
2737255736Sdavidch
2738255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
2739255736Sdavidch
2740255736Sdavidch    KASSERT((queue < max_agg_queues),
2741255736Sdavidch            ("fp[%02d] invalid aggr queue (%d >= %d)!",
2742255736Sdavidch             fp->index, queue, max_agg_queues));
2743255736Sdavidch
2744255736Sdavidch    KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2745255736Sdavidch            ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2746255736Sdavidch             fp->index, queue));
2747255736Sdavidch
2748255736Sdavidch    /* copy the existing mbuf and mapping from the TPA pool */
2749255736Sdavidch    tmp_bd = tpa_info->bd;
2750255736Sdavidch
2751255736Sdavidch    if (tmp_bd.m == NULL) {
2752295830Sdavidcs        uint32_t *tmp;
2753295830Sdavidcs
2754295830Sdavidcs        tmp = (uint32_t *)cqe;
2755295830Sdavidcs
2756295830Sdavidcs        BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2757295830Sdavidcs              fp->index, queue, cons, prod);
2758295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2759295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2760295830Sdavidcs
2761255736Sdavidch        /* XXX Error handling? */
2762255736Sdavidch        return;
2763255736Sdavidch    }
2764255736Sdavidch
2765255736Sdavidch    /* change the TPA queue to the start state */
2766255736Sdavidch    tpa_info->state            = BXE_TPA_STATE_START;
2767255736Sdavidch    tpa_info->placement_offset = cqe->placement_offset;
2768255736Sdavidch    tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2769255736Sdavidch    tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2770255736Sdavidch    tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2771255736Sdavidch
2772255736Sdavidch    fp->rx_tpa_queue_used |= (1 << queue);
2773255736Sdavidch
2774255736Sdavidch    /*
2775255736Sdavidch     * If all the buffer descriptors are filled with mbufs then fill in
2776255736Sdavidch     * the current consumer index with a new BD. Else if a maximum Rx
2777255736Sdavidch     * buffer limit is imposed then fill in the next producer index.
2778255736Sdavidch     */
2779255736Sdavidch    index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2780255736Sdavidch                prod : cons;
2781255736Sdavidch
2782255736Sdavidch    /* move the received mbuf and mapping to TPA pool */
2783255736Sdavidch    tpa_info->bd = fp->rx_mbuf_chain[cons];
2784255736Sdavidch
2785255736Sdavidch    /* release any existing RX BD mbuf mappings */
2786255736Sdavidch    if (cons != index) {
2787255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[cons];
2788255736Sdavidch
2789255736Sdavidch        if (rx_buf->m_map != NULL) {
2790255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2791255736Sdavidch                            BUS_DMASYNC_POSTREAD);
2792255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2793255736Sdavidch        }
2794255736Sdavidch
2795255736Sdavidch        /*
2796255736Sdavidch         * We get here when the maximum number of rx buffers is less than
2797255736Sdavidch         * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2798255736Sdavidch         * it out here without concern of a memory leak.
2799255736Sdavidch         */
2800255736Sdavidch        fp->rx_mbuf_chain[cons].m = NULL;
2801255736Sdavidch    }
2802255736Sdavidch
2803255736Sdavidch    /* update the Rx SW BD with the mbuf info from the TPA pool */
2804255736Sdavidch    fp->rx_mbuf_chain[index] = tmp_bd;
2805255736Sdavidch
2806255736Sdavidch    /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2807255736Sdavidch    rx_bd = &fp->rx_chain[index];
2808255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2809255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2810255736Sdavidch}
2811255736Sdavidch
2812255736Sdavidch/*
2813255736Sdavidch * When a TPA aggregation is completed, loop through the individual mbufs
2814255736Sdavidch * of the aggregation, combining them into a single mbuf which will be sent
2815255736Sdavidch * up the stack. Refill all freed SGEs with mbufs as we go along.
2816255736Sdavidch */
2817255736Sdavidchstatic int
2818255736Sdavidchbxe_fill_frag_mbuf(struct bxe_softc          *sc,
2819255736Sdavidch                   struct bxe_fastpath       *fp,
2820255736Sdavidch                   struct bxe_sw_tpa_info    *tpa_info,
2821255736Sdavidch                   uint16_t                  queue,
2822255736Sdavidch                   uint16_t                  pages,
2823255736Sdavidch                   struct mbuf               *m,
2824255736Sdavidch			       struct eth_end_agg_rx_cqe *cqe,
2825255736Sdavidch                   uint16_t                  cqe_idx)
2826255736Sdavidch{
2827255736Sdavidch    struct mbuf *m_frag;
2828255736Sdavidch    uint32_t frag_len, frag_size, i;
2829255736Sdavidch    uint16_t sge_idx;
2830255736Sdavidch    int rc = 0;
2831255736Sdavidch    int j;
2832255736Sdavidch
2833255736Sdavidch    frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2834255736Sdavidch
2835255736Sdavidch    BLOGD(sc, DBG_LRO,
2836255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2837255736Sdavidch          fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2838255736Sdavidch
2839255736Sdavidch    /* make sure the aggregated frame is not too big to handle */
2840255736Sdavidch    if (pages > 8 * PAGES_PER_SGE) {
2841295830Sdavidcs
2842295830Sdavidcs        uint32_t *tmp = (uint32_t *)cqe;
2843295830Sdavidcs
2844255736Sdavidch        BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2845255736Sdavidch                  "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2846255736Sdavidch              fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2847255736Sdavidch              tpa_info->len_on_bd, frag_size);
2848295830Sdavidcs
2849295830Sdavidcs        BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2850295830Sdavidcs            *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2851295830Sdavidcs
2852255736Sdavidch        bxe_panic(sc, ("sge page count error\n"));
2853255736Sdavidch        return (EINVAL);
2854255736Sdavidch    }
2855255736Sdavidch
2856255736Sdavidch    /*
2857255736Sdavidch     * Scan through the scatter gather list pulling individual mbufs into a
2858255736Sdavidch     * single mbuf for the host stack.
2859255736Sdavidch     */
2860255736Sdavidch    for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2861255736Sdavidch        sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2862255736Sdavidch
2863255736Sdavidch        /*
2864255736Sdavidch         * Firmware gives the indices of the SGE as if the ring is an array
2865255736Sdavidch         * (meaning that the "next" element will consume 2 indices).
2866255736Sdavidch         */
2867255736Sdavidch        frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2868255736Sdavidch
2869255736Sdavidch        BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2870255736Sdavidch                           "sge_idx=%d frag_size=%d frag_len=%d\n",
2871255736Sdavidch              fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2872255736Sdavidch
2873255736Sdavidch        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2874255736Sdavidch
2875255736Sdavidch        /* allocate a new mbuf for the SGE */
2876255736Sdavidch        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2877255736Sdavidch        if (rc) {
2878255736Sdavidch            /* Leave all remaining SGEs in the ring! */
2879255736Sdavidch            return (rc);
2880255736Sdavidch        }
2881255736Sdavidch
2882255736Sdavidch        /* update the fragment length */
2883255736Sdavidch        m_frag->m_len = frag_len;
2884255736Sdavidch
2885255736Sdavidch        /* concatenate the fragment to the head mbuf */
2886255736Sdavidch        m_cat(m, m_frag);
2887255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
2888255736Sdavidch
2889255736Sdavidch        /* update the TPA mbuf size and remaining fragment size */
2890255736Sdavidch        m->m_pkthdr.len += frag_len;
2891255736Sdavidch        frag_size -= frag_len;
2892255736Sdavidch    }
2893255736Sdavidch
2894255736Sdavidch    BLOGD(sc, DBG_LRO,
2895255736Sdavidch          "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2896255736Sdavidch          fp->index, queue, frag_size);
2897255736Sdavidch
2898255736Sdavidch    return (rc);
2899255736Sdavidch}
2900255736Sdavidch
2901255736Sdavidchstatic inline void
2902255736Sdavidchbxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2903255736Sdavidch{
2904255736Sdavidch    int i, j;
2905255736Sdavidch
2906255736Sdavidch    for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2907255736Sdavidch        int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2908255736Sdavidch
2909255736Sdavidch        for (j = 0; j < 2; j++) {
2910255736Sdavidch            BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2911255736Sdavidch            idx--;
2912255736Sdavidch        }
2913255736Sdavidch    }
2914255736Sdavidch}
2915255736Sdavidch
2916255736Sdavidchstatic inline void
2917255736Sdavidchbxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2918255736Sdavidch{
2919255736Sdavidch    /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2920255736Sdavidch    memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2921255736Sdavidch
2922255736Sdavidch    /*
2923255736Sdavidch     * Clear the two last indices in the page to 1. These are the indices that
2924255736Sdavidch     * correspond to the "next" element, hence will never be indicated and
2925255736Sdavidch     * should be removed from the calculations.
2926255736Sdavidch     */
2927255736Sdavidch    bxe_clear_sge_mask_next_elems(fp);
2928255736Sdavidch}
2929255736Sdavidch
2930255736Sdavidchstatic inline void
2931255736Sdavidchbxe_update_last_max_sge(struct bxe_fastpath *fp,
2932255736Sdavidch                        uint16_t            idx)
2933255736Sdavidch{
2934255736Sdavidch    uint16_t last_max = fp->last_max_sge;
2935255736Sdavidch
2936255736Sdavidch    if (SUB_S16(idx, last_max) > 0) {
2937255736Sdavidch        fp->last_max_sge = idx;
2938255736Sdavidch    }
2939255736Sdavidch}
2940255736Sdavidch
2941255736Sdavidchstatic inline void
2942255736Sdavidchbxe_update_sge_prod(struct bxe_softc          *sc,
2943255736Sdavidch                    struct bxe_fastpath       *fp,
2944255736Sdavidch                    uint16_t                  sge_len,
2945283269Sdavidcs                    union eth_sgl_or_raw_data *cqe)
2946255736Sdavidch{
2947255736Sdavidch    uint16_t last_max, last_elem, first_elem;
2948255736Sdavidch    uint16_t delta = 0;
2949255736Sdavidch    uint16_t i;
2950255736Sdavidch
2951255736Sdavidch    if (!sge_len) {
2952255736Sdavidch        return;
2953255736Sdavidch    }
2954255736Sdavidch
2955255736Sdavidch    /* first mark all used pages */
2956255736Sdavidch    for (i = 0; i < sge_len; i++) {
2957255736Sdavidch        BIT_VEC64_CLEAR_BIT(fp->sge_mask,
2958283269Sdavidcs                            RX_SGE(le16toh(cqe->sgl[i])));
2959255736Sdavidch    }
2960255736Sdavidch
2961255736Sdavidch    BLOGD(sc, DBG_LRO,
2962255736Sdavidch          "fp[%02d] fp_cqe->sgl[%d] = %d\n",
2963255736Sdavidch          fp->index, sge_len - 1,
2964283269Sdavidcs          le16toh(cqe->sgl[sge_len - 1]));
2965255736Sdavidch
2966255736Sdavidch    /* assume that the last SGE index is the biggest */
2967255736Sdavidch    bxe_update_last_max_sge(fp,
2968283269Sdavidcs                            le16toh(cqe->sgl[sge_len - 1]));
2969255736Sdavidch
2970255736Sdavidch    last_max = RX_SGE(fp->last_max_sge);
2971255736Sdavidch    last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
2972255736Sdavidch    first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
2973255736Sdavidch
2974255736Sdavidch    /* if ring is not full */
2975255736Sdavidch    if (last_elem + 1 != first_elem) {
2976255736Sdavidch        last_elem++;
2977255736Sdavidch    }
2978255736Sdavidch
2979255736Sdavidch    /* now update the prod */
2980255736Sdavidch    for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
2981255736Sdavidch        if (__predict_true(fp->sge_mask[i])) {
2982255736Sdavidch            break;
2983255736Sdavidch        }
2984255736Sdavidch
2985255736Sdavidch        fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
2986255736Sdavidch        delta += BIT_VEC64_ELEM_SZ;
2987255736Sdavidch    }
2988255736Sdavidch
2989255736Sdavidch    if (delta > 0) {
2990255736Sdavidch        fp->rx_sge_prod += delta;
2991255736Sdavidch        /* clear page-end entries */
2992255736Sdavidch        bxe_clear_sge_mask_next_elems(fp);
2993255736Sdavidch    }
2994255736Sdavidch
2995255736Sdavidch    BLOGD(sc, DBG_LRO,
2996255736Sdavidch          "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
2997255736Sdavidch          fp->index, fp->last_max_sge, fp->rx_sge_prod);
2998255736Sdavidch}
2999255736Sdavidch
3000255736Sdavidch/*
3001255736Sdavidch * The aggregation on the current TPA queue has completed. Pull the individual
3002255736Sdavidch * mbuf fragments together into a single mbuf, perform all necessary checksum
3003255736Sdavidch * calculations, and send the resuting mbuf to the stack.
3004255736Sdavidch */
3005255736Sdavidchstatic void
3006255736Sdavidchbxe_tpa_stop(struct bxe_softc          *sc,
3007255736Sdavidch             struct bxe_fastpath       *fp,
3008255736Sdavidch             struct bxe_sw_tpa_info    *tpa_info,
3009255736Sdavidch             uint16_t                  queue,
3010255736Sdavidch             uint16_t                  pages,
3011255736Sdavidch			 struct eth_end_agg_rx_cqe *cqe,
3012255736Sdavidch             uint16_t                  cqe_idx)
3013255736Sdavidch{
3014266979Smarcel    if_t ifp = sc->ifp;
3015255736Sdavidch    struct mbuf *m;
3016255736Sdavidch    int rc = 0;
3017255736Sdavidch
3018255736Sdavidch    BLOGD(sc, DBG_LRO,
3019255736Sdavidch          "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3020255736Sdavidch          fp->index, queue, tpa_info->placement_offset,
3021255736Sdavidch          le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3022255736Sdavidch
3023255736Sdavidch    m = tpa_info->bd.m;
3024255736Sdavidch
3025255736Sdavidch    /* allocate a replacement before modifying existing mbuf */
3026255736Sdavidch    rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3027255736Sdavidch    if (rc) {
3028255736Sdavidch        /* drop the frame and log an error */
3029255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3030255736Sdavidch        goto bxe_tpa_stop_exit;
3031255736Sdavidch    }
3032255736Sdavidch
3033255736Sdavidch    /* we have a replacement, fixup the current mbuf */
3034255736Sdavidch    m_adj(m, tpa_info->placement_offset);
3035255736Sdavidch    m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3036255736Sdavidch
3037255736Sdavidch    /* mark the checksums valid (taken care of by the firmware) */
3038255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3039255736Sdavidch    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3040255736Sdavidch    m->m_pkthdr.csum_data = 0xffff;
3041255736Sdavidch    m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3042255736Sdavidch                               CSUM_IP_VALID   |
3043255736Sdavidch                               CSUM_DATA_VALID |
3044255736Sdavidch                               CSUM_PSEUDO_HDR);
3045255736Sdavidch
3046255736Sdavidch    /* aggregate all of the SGEs into a single mbuf */
3047255736Sdavidch    rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3048255736Sdavidch    if (rc) {
3049255736Sdavidch        /* drop the packet and log an error */
3050255736Sdavidch        fp->eth_q_stats.rx_soft_errors++;
3051255736Sdavidch        m_freem(m);
3052255736Sdavidch    } else {
3053296071Sdavidcs        if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3054255736Sdavidch            m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3055255736Sdavidch            m->m_flags |= M_VLANTAG;
3056255736Sdavidch        }
3057255736Sdavidch
3058255736Sdavidch        /* assign packet to this interface interface */
3059266979Smarcel        if_setrcvif(m, ifp);
3060255736Sdavidch
3061255736Sdavidch#if __FreeBSD_version >= 800000
3062255736Sdavidch        /* specify what RSS queue was used for this flow */
3063255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3064275358Shselasky        M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
3065255736Sdavidch#endif
3066255736Sdavidch
3067271782Sglebius        if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3068255736Sdavidch        fp->eth_q_stats.rx_tpa_pkts++;
3069255736Sdavidch
3070255736Sdavidch        /* pass the frame to the stack */
3071266979Smarcel        if_input(ifp, m);
3072255736Sdavidch    }
3073255736Sdavidch
3074255736Sdavidch    /* we passed an mbuf up the stack or dropped the frame */
3075255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa--;
3076255736Sdavidch
3077255736Sdavidchbxe_tpa_stop_exit:
3078255736Sdavidch
3079255736Sdavidch    fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3080255736Sdavidch    fp->rx_tpa_queue_used &= ~(1 << queue);
3081255736Sdavidch}
3082255736Sdavidch
3083255736Sdavidchstatic uint8_t
3084283269Sdavidcsbxe_service_rxsgl(
3085283269Sdavidcs                 struct bxe_fastpath *fp,
3086283269Sdavidcs                 uint16_t len,
3087283269Sdavidcs                 uint16_t lenonbd,
3088283269Sdavidcs                 struct mbuf *m,
3089283269Sdavidcs                 struct eth_fast_path_rx_cqe *cqe_fp)
3090283269Sdavidcs{
3091283269Sdavidcs    struct mbuf *m_frag;
3092283269Sdavidcs    uint16_t frags, frag_len;
3093283269Sdavidcs    uint16_t sge_idx = 0;
3094283269Sdavidcs    uint16_t j;
3095283269Sdavidcs    uint8_t i, rc = 0;
3096283269Sdavidcs    uint32_t frag_size;
3097283269Sdavidcs
3098283269Sdavidcs    /* adjust the mbuf */
3099283269Sdavidcs    m->m_len = lenonbd;
3100283269Sdavidcs
3101283269Sdavidcs    frag_size =  len - lenonbd;
3102283269Sdavidcs    frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3103283269Sdavidcs
3104283269Sdavidcs    for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3105283269Sdavidcs        sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3106283269Sdavidcs
3107283269Sdavidcs        m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3108283269Sdavidcs        frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3109283269Sdavidcs        m_frag->m_len = frag_len;
3110283269Sdavidcs
3111283269Sdavidcs       /* allocate a new mbuf for the SGE */
3112283269Sdavidcs        rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3113283269Sdavidcs        if (rc) {
3114283269Sdavidcs            /* Leave all remaining SGEs in the ring! */
3115283269Sdavidcs            return (rc);
3116283269Sdavidcs        }
3117283269Sdavidcs        fp->eth_q_stats.mbuf_alloc_sge--;
3118283269Sdavidcs
3119283269Sdavidcs        /* concatenate the fragment to the head mbuf */
3120283269Sdavidcs        m_cat(m, m_frag);
3121283269Sdavidcs
3122283269Sdavidcs        frag_size -= frag_len;
3123283269Sdavidcs    }
3124283269Sdavidcs
3125283269Sdavidcs    bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3126283269Sdavidcs
3127283269Sdavidcs    return rc;
3128283269Sdavidcs}
3129283269Sdavidcs
3130283269Sdavidcsstatic uint8_t
3131255736Sdavidchbxe_rxeof(struct bxe_softc    *sc,
3132255736Sdavidch          struct bxe_fastpath *fp)
3133255736Sdavidch{
3134266979Smarcel    if_t ifp = sc->ifp;
3135255736Sdavidch    uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3136255736Sdavidch    uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3137255736Sdavidch    int rx_pkts = 0;
3138281006Sdavidcs    int rc = 0;
3139255736Sdavidch
3140255736Sdavidch    BXE_FP_RX_LOCK(fp);
3141255736Sdavidch
3142255736Sdavidch    /* CQ "next element" is of the size of the regular element */
3143255736Sdavidch    hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3144255736Sdavidch    if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3145255736Sdavidch        hw_cq_cons++;
3146255736Sdavidch    }
3147255736Sdavidch
3148255736Sdavidch    bd_cons = fp->rx_bd_cons;
3149255736Sdavidch    bd_prod = fp->rx_bd_prod;
3150255736Sdavidch    bd_prod_fw = bd_prod;
3151255736Sdavidch    sw_cq_cons = fp->rx_cq_cons;
3152255736Sdavidch    sw_cq_prod = fp->rx_cq_prod;
3153255736Sdavidch
3154255736Sdavidch    /*
3155255736Sdavidch     * Memory barrier necessary as speculative reads of the rx
3156255736Sdavidch     * buffer can be ahead of the index in the status block
3157255736Sdavidch     */
3158255736Sdavidch    rmb();
3159255736Sdavidch
3160255736Sdavidch    BLOGD(sc, DBG_RX,
3161255736Sdavidch          "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3162255736Sdavidch          fp->index, hw_cq_cons, sw_cq_cons);
3163255736Sdavidch
3164255736Sdavidch    while (sw_cq_cons != hw_cq_cons) {
3165255736Sdavidch        struct bxe_sw_rx_bd *rx_buf = NULL;
3166255736Sdavidch        union eth_rx_cqe *cqe;
3167255736Sdavidch        struct eth_fast_path_rx_cqe *cqe_fp;
3168255736Sdavidch        uint8_t cqe_fp_flags;
3169255736Sdavidch        enum eth_rx_cqe_type cqe_fp_type;
3170283269Sdavidcs        uint16_t len, lenonbd,  pad;
3171255736Sdavidch        struct mbuf *m = NULL;
3172255736Sdavidch
3173255736Sdavidch        comp_ring_cons = RCQ(sw_cq_cons);
3174255736Sdavidch        bd_prod = RX_BD(bd_prod);
3175255736Sdavidch        bd_cons = RX_BD(bd_cons);
3176255736Sdavidch
3177255736Sdavidch        cqe          = &fp->rcq_chain[comp_ring_cons];
3178255736Sdavidch        cqe_fp       = &cqe->fast_path_cqe;
3179255736Sdavidch        cqe_fp_flags = cqe_fp->type_error_flags;
3180255736Sdavidch        cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3181255736Sdavidch
3182255736Sdavidch        BLOGD(sc, DBG_RX,
3183255736Sdavidch              "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3184255736Sdavidch              "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3185283269Sdavidcs              "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3186255736Sdavidch              fp->index,
3187255736Sdavidch              hw_cq_cons,
3188255736Sdavidch              sw_cq_cons,
3189255736Sdavidch              bd_prod,
3190255736Sdavidch              bd_cons,
3191255736Sdavidch              CQE_TYPE(cqe_fp_flags),
3192255736Sdavidch              cqe_fp_flags,
3193255736Sdavidch              cqe_fp->status_flags,
3194255736Sdavidch              le32toh(cqe_fp->rss_hash_result),
3195255736Sdavidch              le16toh(cqe_fp->vlan_tag),
3196283269Sdavidcs              le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3197283269Sdavidcs              le16toh(cqe_fp->len_on_bd));
3198255736Sdavidch
3199255736Sdavidch        /* is this a slowpath msg? */
3200255736Sdavidch        if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3201255736Sdavidch            bxe_sp_event(sc, fp, cqe);
3202255736Sdavidch            goto next_cqe;
3203255736Sdavidch        }
3204255736Sdavidch
3205255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[bd_cons];
3206255736Sdavidch
3207255736Sdavidch        if (!CQE_TYPE_FAST(cqe_fp_type)) {
3208255736Sdavidch            struct bxe_sw_tpa_info *tpa_info;
3209255736Sdavidch            uint16_t frag_size, pages;
3210255736Sdavidch            uint8_t queue;
3211255736Sdavidch
3212255736Sdavidch            if (CQE_TYPE_START(cqe_fp_type)) {
3213255736Sdavidch                bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3214255736Sdavidch                              bd_cons, bd_prod, cqe_fp);
3215255736Sdavidch                m = NULL; /* packet not ready yet */
3216255736Sdavidch                goto next_rx;
3217255736Sdavidch            }
3218255736Sdavidch
3219255736Sdavidch            KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3220255736Sdavidch                    ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3221255736Sdavidch
3222255736Sdavidch            queue = cqe->end_agg_cqe.queue_index;
3223255736Sdavidch            tpa_info = &fp->rx_tpa_info[queue];
3224255736Sdavidch
3225255736Sdavidch            BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3226255736Sdavidch                  fp->index, queue);
3227255736Sdavidch
3228255736Sdavidch            frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3229255736Sdavidch                         tpa_info->len_on_bd);
3230255736Sdavidch            pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3231255736Sdavidch
3232255736Sdavidch            bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3233255736Sdavidch                         &cqe->end_agg_cqe, comp_ring_cons);
3234255736Sdavidch
3235283269Sdavidcs            bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3236255736Sdavidch
3237255736Sdavidch            goto next_cqe;
3238255736Sdavidch        }
3239255736Sdavidch
3240255736Sdavidch        /* non TPA */
3241255736Sdavidch
3242255736Sdavidch        /* is this an error packet? */
3243255736Sdavidch        if (__predict_false(cqe_fp_flags &
3244255736Sdavidch                            ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3245255736Sdavidch            BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3246255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3247255736Sdavidch            goto next_rx;
3248255736Sdavidch        }
3249255736Sdavidch
3250255736Sdavidch        len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3251283269Sdavidcs        lenonbd = le16toh(cqe_fp->len_on_bd);
3252255736Sdavidch        pad = cqe_fp->placement_offset;
3253255736Sdavidch
3254255736Sdavidch        m = rx_buf->m;
3255255736Sdavidch
3256255736Sdavidch        if (__predict_false(m == NULL)) {
3257255736Sdavidch            BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3258255736Sdavidch                  bd_cons, fp->index);
3259255736Sdavidch            goto next_rx;
3260255736Sdavidch        }
3261255736Sdavidch
3262255736Sdavidch        /* XXX double copy if packet length under a threshold */
3263255736Sdavidch
3264255736Sdavidch        /*
3265255736Sdavidch         * If all the buffer descriptors are filled with mbufs then fill in
3266255736Sdavidch         * the current consumer index with a new BD. Else if a maximum Rx
3267255736Sdavidch         * buffer limit is imposed then fill in the next producer index.
3268255736Sdavidch         */
3269255736Sdavidch        rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3270255736Sdavidch                                  (sc->max_rx_bufs != RX_BD_USABLE) ?
3271255736Sdavidch                                      bd_prod : bd_cons);
3272255736Sdavidch        if (rc != 0) {
3273281006Sdavidcs
3274281006Sdavidcs            /* we simply reuse the received mbuf and don't post it to the stack */
3275281006Sdavidcs            m = NULL;
3276281006Sdavidcs
3277255736Sdavidch            BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3278255736Sdavidch                  fp->index, rc);
3279255736Sdavidch            fp->eth_q_stats.rx_soft_errors++;
3280255736Sdavidch
3281255736Sdavidch            if (sc->max_rx_bufs != RX_BD_USABLE) {
3282255736Sdavidch                /* copy this consumer index to the producer index */
3283255736Sdavidch                memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3284255736Sdavidch                       sizeof(struct bxe_sw_rx_bd));
3285255736Sdavidch                memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3286255736Sdavidch            }
3287255736Sdavidch
3288255736Sdavidch            goto next_rx;
3289255736Sdavidch        }
3290255736Sdavidch
3291255736Sdavidch        /* current mbuf was detached from the bd */
3292255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
3293255736Sdavidch
3294255736Sdavidch        /* we allocated a replacement mbuf, fixup the current one */
3295255736Sdavidch        m_adj(m, pad);
3296255736Sdavidch        m->m_pkthdr.len = m->m_len = len;
3297255736Sdavidch
3298292638Sdavidcs        if ((len > 60) && (len > lenonbd)) {
3299292638Sdavidcs            fp->eth_q_stats.rx_bxe_service_rxsgl++;
3300283269Sdavidcs            rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3301283269Sdavidcs            if (rc)
3302283269Sdavidcs                break;
3303283274Sdavidcs            fp->eth_q_stats.rx_jumbo_sge_pkts++;
3304292638Sdavidcs        } else if (lenonbd < len) {
3305292638Sdavidcs            fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3306283269Sdavidcs        }
3307283269Sdavidcs
3308255736Sdavidch        /* assign packet to this interface interface */
3309266979Smarcel	if_setrcvif(m, ifp);
3310255736Sdavidch
3311255736Sdavidch        /* assume no hardware checksum has complated */
3312255736Sdavidch        m->m_pkthdr.csum_flags = 0;
3313255736Sdavidch
3314255736Sdavidch        /* validate checksum if offload enabled */
3315266979Smarcel        if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3316255736Sdavidch            /* check for a valid IP frame */
3317255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3318255736Sdavidch                  ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3319255736Sdavidch                m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3320255736Sdavidch                if (__predict_false(cqe_fp_flags &
3321255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3322255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3323255736Sdavidch                } else {
3324255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3325255736Sdavidch                    m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3326255736Sdavidch                }
3327255736Sdavidch            }
3328255736Sdavidch
3329255736Sdavidch            /* check for a valid TCP/UDP frame */
3330255736Sdavidch            if (!(cqe->fast_path_cqe.status_flags &
3331255736Sdavidch                  ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3332255736Sdavidch                if (__predict_false(cqe_fp_flags &
3333255736Sdavidch                                    ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3334255736Sdavidch                    fp->eth_q_stats.rx_hw_csum_errors++;
3335255736Sdavidch                } else {
3336255736Sdavidch                    fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3337255736Sdavidch                    m->m_pkthdr.csum_data = 0xFFFF;
3338255736Sdavidch                    m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3339255736Sdavidch                                               CSUM_PSEUDO_HDR);
3340255736Sdavidch                }
3341255736Sdavidch            }
3342255736Sdavidch        }
3343255736Sdavidch
3344255736Sdavidch        /* if there is a VLAN tag then flag that info */
3345296071Sdavidcs        if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3346255736Sdavidch            m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3347255736Sdavidch            m->m_flags |= M_VLANTAG;
3348255736Sdavidch        }
3349255736Sdavidch
3350255736Sdavidch#if __FreeBSD_version >= 800000
3351255736Sdavidch        /* specify what RSS queue was used for this flow */
3352255736Sdavidch        m->m_pkthdr.flowid = fp->index;
3353275358Shselasky        M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
3354255736Sdavidch#endif
3355255736Sdavidch
3356255736Sdavidchnext_rx:
3357255736Sdavidch
3358255736Sdavidch        bd_cons    = RX_BD_NEXT(bd_cons);
3359255736Sdavidch        bd_prod    = RX_BD_NEXT(bd_prod);
3360255736Sdavidch        bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3361255736Sdavidch
3362255736Sdavidch        /* pass the frame to the stack */
3363255736Sdavidch        if (__predict_true(m != NULL)) {
3364271782Sglebius            if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3365255736Sdavidch            rx_pkts++;
3366266979Smarcel            if_input(ifp, m);
3367255736Sdavidch        }
3368255736Sdavidch
3369255736Sdavidchnext_cqe:
3370255736Sdavidch
3371255736Sdavidch        sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3372255736Sdavidch        sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3373255736Sdavidch
3374255736Sdavidch        /* limit spinning on the queue */
3375281006Sdavidcs        if (rc != 0)
3376281006Sdavidcs            break;
3377281006Sdavidcs
3378255736Sdavidch        if (rx_pkts == sc->rx_budget) {
3379255736Sdavidch            fp->eth_q_stats.rx_budget_reached++;
3380255736Sdavidch            break;
3381255736Sdavidch        }
3382255736Sdavidch    } /* while work to do */
3383255736Sdavidch
3384255736Sdavidch    fp->rx_bd_cons = bd_cons;
3385255736Sdavidch    fp->rx_bd_prod = bd_prod_fw;
3386255736Sdavidch    fp->rx_cq_cons = sw_cq_cons;
3387255736Sdavidch    fp->rx_cq_prod = sw_cq_prod;
3388255736Sdavidch
3389255736Sdavidch    /* Update producers */
3390255736Sdavidch    bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3391255736Sdavidch
3392255736Sdavidch    fp->eth_q_stats.rx_pkts += rx_pkts;
3393255736Sdavidch    fp->eth_q_stats.rx_calls++;
3394255736Sdavidch
3395255736Sdavidch    BXE_FP_RX_UNLOCK(fp);
3396255736Sdavidch
3397255736Sdavidch    return (sw_cq_cons != hw_cq_cons);
3398255736Sdavidch}
3399255736Sdavidch
3400255736Sdavidchstatic uint16_t
3401255736Sdavidchbxe_free_tx_pkt(struct bxe_softc    *sc,
3402255736Sdavidch                struct bxe_fastpath *fp,
3403255736Sdavidch                uint16_t            idx)
3404255736Sdavidch{
3405255736Sdavidch    struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3406255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
3407255736Sdavidch    uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3408255736Sdavidch    uint16_t new_cons;
3409255736Sdavidch    int nbd;
3410255736Sdavidch
3411255736Sdavidch    /* unmap the mbuf from non-paged memory */
3412255736Sdavidch    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3413255736Sdavidch
3414255736Sdavidch    tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3415255736Sdavidch    nbd = le16toh(tx_start_bd->nbd) - 1;
3416255736Sdavidch
3417255736Sdavidch    new_cons = (tx_buf->first_bd + nbd);
3418255736Sdavidch
3419255736Sdavidch    /* free the mbuf */
3420255736Sdavidch    if (__predict_true(tx_buf->m != NULL)) {
3421255736Sdavidch        m_freem(tx_buf->m);
3422255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx--;
3423255736Sdavidch    } else {
3424255736Sdavidch        fp->eth_q_stats.tx_chain_lost_mbuf++;
3425255736Sdavidch    }
3426255736Sdavidch
3427255736Sdavidch    tx_buf->m = NULL;
3428255736Sdavidch    tx_buf->first_bd = 0;
3429255736Sdavidch
3430255736Sdavidch    return (new_cons);
3431255736Sdavidch}
3432255736Sdavidch
3433255736Sdavidch/* transmit timeout watchdog */
3434255736Sdavidchstatic int
3435255736Sdavidchbxe_watchdog(struct bxe_softc    *sc,
3436255736Sdavidch             struct bxe_fastpath *fp)
3437255736Sdavidch{
3438255736Sdavidch    BXE_FP_TX_LOCK(fp);
3439255736Sdavidch
3440255736Sdavidch    if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3441255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
3442255736Sdavidch        return (0);
3443255736Sdavidch    }
3444255736Sdavidch
3445258187Sedavis    BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3446258187Sedavis
3447255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
3448255736Sdavidch
3449255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_REINIT);
3450255736Sdavidch    taskqueue_enqueue(sc->chip_tq, &sc->chip_tq_task);
3451255736Sdavidch
3452255736Sdavidch    return (-1);
3453255736Sdavidch}
3454255736Sdavidch
3455255736Sdavidch/* processes transmit completions */
3456255736Sdavidchstatic uint8_t
3457255736Sdavidchbxe_txeof(struct bxe_softc    *sc,
3458255736Sdavidch          struct bxe_fastpath *fp)
3459255736Sdavidch{
3460266979Smarcel    if_t ifp = sc->ifp;
3461255736Sdavidch    uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3462255736Sdavidch    uint16_t tx_bd_avail;
3463255736Sdavidch
3464255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
3465255736Sdavidch
3466255736Sdavidch    bd_cons = fp->tx_bd_cons;
3467255736Sdavidch    hw_cons = le16toh(*fp->tx_cons_sb);
3468255736Sdavidch    sw_cons = fp->tx_pkt_cons;
3469255736Sdavidch
3470255736Sdavidch    while (sw_cons != hw_cons) {
3471255736Sdavidch        pkt_cons = TX_BD(sw_cons);
3472255736Sdavidch
3473255736Sdavidch        BLOGD(sc, DBG_TX,
3474255736Sdavidch              "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3475255736Sdavidch              fp->index, hw_cons, sw_cons, pkt_cons);
3476255736Sdavidch
3477255736Sdavidch        bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3478255736Sdavidch
3479255736Sdavidch        sw_cons++;
3480255736Sdavidch    }
3481255736Sdavidch
3482255736Sdavidch    fp->tx_pkt_cons = sw_cons;
3483255736Sdavidch    fp->tx_bd_cons  = bd_cons;
3484255736Sdavidch
3485255736Sdavidch    BLOGD(sc, DBG_TX,
3486255736Sdavidch          "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3487255736Sdavidch          fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3488255736Sdavidch
3489255736Sdavidch    mb();
3490255736Sdavidch
3491255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
3492255736Sdavidch
3493258187Sedavis    if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3494266979Smarcel        if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
3495258187Sedavis    } else {
3496266979Smarcel        if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3497258187Sedavis    }
3498255736Sdavidch
3499258187Sedavis    if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3500258187Sedavis        /* reset the watchdog timer if there are pending transmits */
3501255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
3502258187Sedavis        return (TRUE);
3503258187Sedavis    } else {
3504258187Sedavis        /* clear watchdog when there are no pending transmits */
3505258187Sedavis        fp->watchdog_timer = 0;
3506258187Sedavis        return (FALSE);
3507255736Sdavidch    }
3508255736Sdavidch}
3509255736Sdavidch
3510255736Sdavidchstatic void
3511255736Sdavidchbxe_drain_tx_queues(struct bxe_softc *sc)
3512255736Sdavidch{
3513255736Sdavidch    struct bxe_fastpath *fp;
3514255736Sdavidch    int i, count;
3515255736Sdavidch
3516255736Sdavidch    /* wait until all TX fastpath tasks have completed */
3517255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
3518255736Sdavidch        fp = &sc->fp[i];
3519255736Sdavidch
3520255736Sdavidch        count = 1000;
3521255736Sdavidch
3522255736Sdavidch        while (bxe_has_tx_work(fp)) {
3523255736Sdavidch
3524255736Sdavidch            BXE_FP_TX_LOCK(fp);
3525255736Sdavidch            bxe_txeof(sc, fp);
3526255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
3527255736Sdavidch
3528255736Sdavidch            if (count == 0) {
3529255736Sdavidch                BLOGE(sc, "Timeout waiting for fp[%d] "
3530255736Sdavidch                          "transmits to complete!\n", i);
3531255736Sdavidch                bxe_panic(sc, ("tx drain failure\n"));
3532255736Sdavidch                return;
3533255736Sdavidch            }
3534255736Sdavidch
3535255736Sdavidch            count--;
3536255736Sdavidch            DELAY(1000);
3537255736Sdavidch            rmb();
3538255736Sdavidch        }
3539255736Sdavidch    }
3540255736Sdavidch
3541255736Sdavidch    return;
3542255736Sdavidch}
3543255736Sdavidch
3544255736Sdavidchstatic int
3545255736Sdavidchbxe_del_all_macs(struct bxe_softc          *sc,
3546255736Sdavidch                 struct ecore_vlan_mac_obj *mac_obj,
3547255736Sdavidch                 int                       mac_type,
3548255736Sdavidch                 uint8_t                   wait_for_comp)
3549255736Sdavidch{
3550255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3551255736Sdavidch    int rc;
3552255736Sdavidch
3553255736Sdavidch    /* wait for completion of requested */
3554255736Sdavidch    if (wait_for_comp) {
3555255736Sdavidch        bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3556255736Sdavidch    }
3557255736Sdavidch
3558255736Sdavidch    /* Set the mac type of addresses we want to clear */
3559255736Sdavidch    bxe_set_bit(mac_type, &vlan_mac_flags);
3560255736Sdavidch
3561255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3562255736Sdavidch    if (rc < 0) {
3563295830Sdavidcs        BLOGE(sc, "Failed to delete MACs (%d) mac_type %d wait_for_comp 0x%x\n",
3564295830Sdavidcs            rc, mac_type, wait_for_comp);
3565255736Sdavidch    }
3566255736Sdavidch
3567255736Sdavidch    return (rc);
3568255736Sdavidch}
3569255736Sdavidch
3570255736Sdavidchstatic int
3571255736Sdavidchbxe_fill_accept_flags(struct bxe_softc *sc,
3572255736Sdavidch                      uint32_t         rx_mode,
3573255736Sdavidch                      unsigned long    *rx_accept_flags,
3574255736Sdavidch                      unsigned long    *tx_accept_flags)
3575255736Sdavidch{
3576255736Sdavidch    /* Clear the flags first */
3577255736Sdavidch    *rx_accept_flags = 0;
3578255736Sdavidch    *tx_accept_flags = 0;
3579255736Sdavidch
3580255736Sdavidch    switch (rx_mode) {
3581255736Sdavidch    case BXE_RX_MODE_NONE:
3582255736Sdavidch        /*
3583255736Sdavidch         * 'drop all' supersedes any accept flags that may have been
3584255736Sdavidch         * passed to the function.
3585255736Sdavidch         */
3586255736Sdavidch        break;
3587255736Sdavidch
3588255736Sdavidch    case BXE_RX_MODE_NORMAL:
3589255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3590255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3591255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3592255736Sdavidch
3593255736Sdavidch        /* internal switching mode */
3594255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3595255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3596255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3597255736Sdavidch
3598255736Sdavidch        break;
3599255736Sdavidch
3600255736Sdavidch    case BXE_RX_MODE_ALLMULTI:
3601255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3602255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3603255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3604255736Sdavidch
3605255736Sdavidch        /* internal switching mode */
3606255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3607255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3608255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3609255736Sdavidch
3610255736Sdavidch        break;
3611255736Sdavidch
3612255736Sdavidch    case BXE_RX_MODE_PROMISC:
3613255736Sdavidch        /*
3614255736Sdavidch         * According to deffinition of SI mode, iface in promisc mode
3615255736Sdavidch         * should receive matched and unmatched (in resolution of port)
3616255736Sdavidch         * unicast packets.
3617255736Sdavidch         */
3618255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3619255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3620255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3621255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3622255736Sdavidch
3623255736Sdavidch        /* internal switching mode */
3624255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3625255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3626255736Sdavidch
3627255736Sdavidch        if (IS_MF_SI(sc)) {
3628255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3629255736Sdavidch        } else {
3630255736Sdavidch            bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3631255736Sdavidch        }
3632255736Sdavidch
3633255736Sdavidch        break;
3634255736Sdavidch
3635255736Sdavidch    default:
3636295830Sdavidcs        BLOGE(sc, "Unknown rx_mode (0x%x)\n", rx_mode);
3637255736Sdavidch        return (-1);
3638255736Sdavidch    }
3639255736Sdavidch
3640255736Sdavidch    /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3641255736Sdavidch    if (rx_mode != BXE_RX_MODE_NONE) {
3642255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3643255736Sdavidch        bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3644255736Sdavidch    }
3645255736Sdavidch
3646255736Sdavidch    return (0);
3647255736Sdavidch}
3648255736Sdavidch
3649255736Sdavidchstatic int
3650255736Sdavidchbxe_set_q_rx_mode(struct bxe_softc *sc,
3651255736Sdavidch                  uint8_t          cl_id,
3652255736Sdavidch                  unsigned long    rx_mode_flags,
3653255736Sdavidch                  unsigned long    rx_accept_flags,
3654255736Sdavidch                  unsigned long    tx_accept_flags,
3655255736Sdavidch                  unsigned long    ramrod_flags)
3656255736Sdavidch{
3657255736Sdavidch    struct ecore_rx_mode_ramrod_params ramrod_param;
3658255736Sdavidch    int rc;
3659255736Sdavidch
3660255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
3661255736Sdavidch
3662255736Sdavidch    /* Prepare ramrod parameters */
3663255736Sdavidch    ramrod_param.cid = 0;
3664255736Sdavidch    ramrod_param.cl_id = cl_id;
3665255736Sdavidch    ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3666255736Sdavidch    ramrod_param.func_id = SC_FUNC(sc);
3667255736Sdavidch
3668255736Sdavidch    ramrod_param.pstate = &sc->sp_state;
3669255736Sdavidch    ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3670255736Sdavidch
3671255736Sdavidch    ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3672255736Sdavidch    ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3673255736Sdavidch
3674255736Sdavidch    bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3675255736Sdavidch
3676255736Sdavidch    ramrod_param.ramrod_flags = ramrod_flags;
3677255736Sdavidch    ramrod_param.rx_mode_flags = rx_mode_flags;
3678255736Sdavidch
3679255736Sdavidch    ramrod_param.rx_accept_flags = rx_accept_flags;
3680255736Sdavidch    ramrod_param.tx_accept_flags = tx_accept_flags;
3681255736Sdavidch
3682255736Sdavidch    rc = ecore_config_rx_mode(sc, &ramrod_param);
3683255736Sdavidch    if (rc < 0) {
3684295830Sdavidcs        BLOGE(sc, "Set rx_mode %d cli_id 0x%x rx_mode_flags 0x%x "
3685295830Sdavidcs            "rx_accept_flags 0x%x tx_accept_flags 0x%x "
3686295830Sdavidcs            "ramrod_flags 0x%x rc %d failed\n", sc->rx_mode, cl_id,
3687295830Sdavidcs            (uint32_t)rx_mode_flags, (uint32_t)rx_accept_flags,
3688295830Sdavidcs            (uint32_t)tx_accept_flags, (uint32_t)ramrod_flags, rc);
3689255736Sdavidch        return (rc);
3690255736Sdavidch    }
3691255736Sdavidch
3692255736Sdavidch    return (0);
3693255736Sdavidch}
3694255736Sdavidch
3695255736Sdavidchstatic int
3696255736Sdavidchbxe_set_storm_rx_mode(struct bxe_softc *sc)
3697255736Sdavidch{
3698255736Sdavidch    unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3699255736Sdavidch    unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3700255736Sdavidch    int rc;
3701255736Sdavidch
3702255736Sdavidch    rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3703255736Sdavidch                               &tx_accept_flags);
3704255736Sdavidch    if (rc) {
3705255736Sdavidch        return (rc);
3706255736Sdavidch    }
3707255736Sdavidch
3708255736Sdavidch    bxe_set_bit(RAMROD_RX, &ramrod_flags);
3709255736Sdavidch    bxe_set_bit(RAMROD_TX, &ramrod_flags);
3710255736Sdavidch
3711255736Sdavidch    /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3712255736Sdavidch    return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3713255736Sdavidch                              rx_accept_flags, tx_accept_flags,
3714255736Sdavidch                              ramrod_flags));
3715255736Sdavidch}
3716255736Sdavidch
3717255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3718255736Sdavidchstatic int
3719255736Sdavidchbxe_nic_load_no_mcp(struct bxe_softc *sc)
3720255736Sdavidch{
3721255736Sdavidch    int path = SC_PATH(sc);
3722255736Sdavidch    int port = SC_PORT(sc);
3723255736Sdavidch
3724255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3725255736Sdavidch          path, load_count[path][0], load_count[path][1],
3726255736Sdavidch          load_count[path][2]);
3727255736Sdavidch    load_count[path][0]++;
3728255736Sdavidch    load_count[path][1 + port]++;
3729255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3730255736Sdavidch          path, load_count[path][0], load_count[path][1],
3731255736Sdavidch          load_count[path][2]);
3732255736Sdavidch    if (load_count[path][0] == 1) {
3733255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_COMMON);
3734255736Sdavidch    } else if (load_count[path][1 + port] == 1) {
3735255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_PORT);
3736255736Sdavidch    } else {
3737255736Sdavidch        return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3738255736Sdavidch    }
3739255736Sdavidch}
3740255736Sdavidch
3741255736Sdavidch/* returns the "mcp load_code" according to global load_count array */
3742255736Sdavidchstatic int
3743255736Sdavidchbxe_nic_unload_no_mcp(struct bxe_softc *sc)
3744255736Sdavidch{
3745255736Sdavidch    int port = SC_PORT(sc);
3746255736Sdavidch    int path = SC_PATH(sc);
3747255736Sdavidch
3748255736Sdavidch    BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3749255736Sdavidch          path, load_count[path][0], load_count[path][1],
3750255736Sdavidch          load_count[path][2]);
3751255736Sdavidch    load_count[path][0]--;
3752255736Sdavidch    load_count[path][1 + port]--;
3753255736Sdavidch    BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3754255736Sdavidch          path, load_count[path][0], load_count[path][1],
3755255736Sdavidch          load_count[path][2]);
3756255736Sdavidch    if (load_count[path][0] == 0) {
3757255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3758255736Sdavidch    } else if (load_count[path][1 + port] == 0) {
3759255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3760255736Sdavidch    } else {
3761255736Sdavidch        return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3762255736Sdavidch    }
3763255736Sdavidch}
3764255736Sdavidch
3765255736Sdavidch/* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3766255736Sdavidchstatic uint32_t
3767255736Sdavidchbxe_send_unload_req(struct bxe_softc *sc,
3768255736Sdavidch                    int              unload_mode)
3769255736Sdavidch{
3770255736Sdavidch    uint32_t reset_code = 0;
3771255736Sdavidch
3772255736Sdavidch    /* Select the UNLOAD request mode */
3773255736Sdavidch    if (unload_mode == UNLOAD_NORMAL) {
3774255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3775295830Sdavidcs    } else {
3776255736Sdavidch        reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3777255736Sdavidch    }
3778255736Sdavidch
3779255736Sdavidch    /* Send the request to the MCP */
3780255736Sdavidch    if (!BXE_NOMCP(sc)) {
3781255736Sdavidch        reset_code = bxe_fw_command(sc, reset_code, 0);
3782255736Sdavidch    } else {
3783255736Sdavidch        reset_code = bxe_nic_unload_no_mcp(sc);
3784255736Sdavidch    }
3785255736Sdavidch
3786255736Sdavidch    return (reset_code);
3787255736Sdavidch}
3788255736Sdavidch
3789255736Sdavidch/* send UNLOAD_DONE command to the MCP */
3790255736Sdavidchstatic void
3791255736Sdavidchbxe_send_unload_done(struct bxe_softc *sc,
3792255736Sdavidch                     uint8_t          keep_link)
3793255736Sdavidch{
3794255736Sdavidch    uint32_t reset_param =
3795255736Sdavidch        keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
3796255736Sdavidch
3797255736Sdavidch    /* Report UNLOAD_DONE to MCP */
3798255736Sdavidch    if (!BXE_NOMCP(sc)) {
3799255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
3800255736Sdavidch    }
3801255736Sdavidch}
3802255736Sdavidch
3803255736Sdavidchstatic int
3804255736Sdavidchbxe_func_wait_started(struct bxe_softc *sc)
3805255736Sdavidch{
3806255736Sdavidch    int tout = 50;
3807255736Sdavidch
3808255736Sdavidch    if (!sc->port.pmf) {
3809255736Sdavidch        return (0);
3810255736Sdavidch    }
3811255736Sdavidch
3812255736Sdavidch    /*
3813255736Sdavidch     * (assumption: No Attention from MCP at this stage)
3814255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
3815255736Sdavidch     * 1. Sync IRS for default SB
3816255736Sdavidch     * 2. Sync SP queue - this guarantees us that attention handling started
3817255736Sdavidch     * 3. Wait, that TX disable/enable transaction completes
3818255736Sdavidch     *
3819255736Sdavidch     * 1+2 guarantee that if DCBX attention was scheduled it already changed
3820255736Sdavidch     * pending bit of transaction from STARTED-->TX_STOPPED, if we already
3821255736Sdavidch     * received completion for the transaction the state is TX_STOPPED.
3822255736Sdavidch     * State will return to STARTED after completion of TX_STOPPED-->STARTED
3823255736Sdavidch     * transaction.
3824255736Sdavidch     */
3825255736Sdavidch
3826255736Sdavidch    /* XXX make sure default SB ISR is done */
3827255736Sdavidch    /* need a way to synchronize an irq (intr_mtx?) */
3828255736Sdavidch
3829255736Sdavidch    /* XXX flush any work queues */
3830255736Sdavidch
3831255736Sdavidch    while (ecore_func_get_state(sc, &sc->func_obj) !=
3832255736Sdavidch           ECORE_F_STATE_STARTED && tout--) {
3833255736Sdavidch        DELAY(20000);
3834255736Sdavidch    }
3835255736Sdavidch
3836255736Sdavidch    if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
3837255736Sdavidch        /*
3838255736Sdavidch         * Failed to complete the transaction in a "good way"
3839255736Sdavidch         * Force both transactions with CLR bit.
3840255736Sdavidch         */
3841255736Sdavidch        struct ecore_func_state_params func_params = { NULL };
3842255736Sdavidch
3843255736Sdavidch        BLOGE(sc, "Unexpected function state! "
3844255736Sdavidch                  "Forcing STARTED-->TX_STOPPED-->STARTED\n");
3845255736Sdavidch
3846255736Sdavidch        func_params.f_obj = &sc->func_obj;
3847255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3848255736Sdavidch
3849255736Sdavidch        /* STARTED-->TX_STOPPED */
3850255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_STOP;
3851255736Sdavidch        ecore_func_state_change(sc, &func_params);
3852255736Sdavidch
3853255736Sdavidch        /* TX_STOPPED-->STARTED */
3854255736Sdavidch        func_params.cmd = ECORE_F_CMD_TX_START;
3855255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
3856255736Sdavidch    }
3857255736Sdavidch
3858255736Sdavidch    return (0);
3859255736Sdavidch}
3860255736Sdavidch
3861255736Sdavidchstatic int
3862255736Sdavidchbxe_stop_queue(struct bxe_softc *sc,
3863255736Sdavidch               int              index)
3864255736Sdavidch{
3865255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[index];
3866255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
3867255736Sdavidch    int rc;
3868255736Sdavidch
3869255736Sdavidch    BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
3870255736Sdavidch
3871255736Sdavidch    q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
3872255736Sdavidch    /* We want to wait for completion in this context */
3873255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
3874255736Sdavidch
3875255736Sdavidch    /* Stop the primary connection: */
3876255736Sdavidch
3877255736Sdavidch    /* ...halt the connection */
3878255736Sdavidch    q_params.cmd = ECORE_Q_CMD_HALT;
3879255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
3880255736Sdavidch    if (rc) {
3881255736Sdavidch        return (rc);
3882255736Sdavidch    }
3883255736Sdavidch
3884255736Sdavidch    /* ...terminate the connection */
3885255736Sdavidch    q_params.cmd = ECORE_Q_CMD_TERMINATE;
3886255736Sdavidch    memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
3887255736Sdavidch    q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
3888255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
3889255736Sdavidch    if (rc) {
3890255736Sdavidch        return (rc);
3891255736Sdavidch    }
3892255736Sdavidch
3893255736Sdavidch    /* ...delete cfc entry */
3894255736Sdavidch    q_params.cmd = ECORE_Q_CMD_CFC_DEL;
3895255736Sdavidch    memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
3896255736Sdavidch    q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
3897255736Sdavidch    return (ecore_queue_state_change(sc, &q_params));
3898255736Sdavidch}
3899255736Sdavidch
3900255736Sdavidch/* wait for the outstanding SP commands */
3901255736Sdavidchstatic inline uint8_t
3902255736Sdavidchbxe_wait_sp_comp(struct bxe_softc *sc,
3903255736Sdavidch                 unsigned long    mask)
3904255736Sdavidch{
3905255736Sdavidch    unsigned long tmp;
3906255736Sdavidch    int tout = 5000; /* wait for 5 secs tops */
3907255736Sdavidch
3908255736Sdavidch    while (tout--) {
3909255736Sdavidch        mb();
3910255736Sdavidch        if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
3911255736Sdavidch            return (TRUE);
3912255736Sdavidch        }
3913255736Sdavidch
3914255736Sdavidch        DELAY(1000);
3915255736Sdavidch    }
3916255736Sdavidch
3917255736Sdavidch    mb();
3918255736Sdavidch
3919255736Sdavidch    tmp = atomic_load_acq_long(&sc->sp_state);
3920255736Sdavidch    if (tmp & mask) {
3921255736Sdavidch        BLOGE(sc, "Filtering completion timed out: "
3922255736Sdavidch                  "sp_state 0x%lx, mask 0x%lx\n",
3923255736Sdavidch              tmp, mask);
3924255736Sdavidch        return (FALSE);
3925255736Sdavidch    }
3926255736Sdavidch
3927255736Sdavidch    return (FALSE);
3928255736Sdavidch}
3929255736Sdavidch
3930255736Sdavidchstatic int
3931255736Sdavidchbxe_func_stop(struct bxe_softc *sc)
3932255736Sdavidch{
3933255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
3934255736Sdavidch    int rc;
3935255736Sdavidch
3936255736Sdavidch    /* prepare parameters for function state transitions */
3937255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3938255736Sdavidch    func_params.f_obj = &sc->func_obj;
3939255736Sdavidch    func_params.cmd = ECORE_F_CMD_STOP;
3940255736Sdavidch
3941255736Sdavidch    /*
3942255736Sdavidch     * Try to stop the function the 'good way'. If it fails (in case
3943255736Sdavidch     * of a parity error during bxe_chip_cleanup()) and we are
3944255736Sdavidch     * not in a debug mode, perform a state transaction in order to
3945255736Sdavidch     * enable further HW_RESET transaction.
3946255736Sdavidch     */
3947255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
3948255736Sdavidch    if (rc) {
3949255736Sdavidch        BLOGE(sc, "FUNC_STOP ramrod failed. "
3950295830Sdavidcs                  "Running a dry transaction (%d)\n", rc);
3951255736Sdavidch        bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3952255736Sdavidch        return (ecore_func_state_change(sc, &func_params));
3953255736Sdavidch    }
3954255736Sdavidch
3955255736Sdavidch    return (0);
3956255736Sdavidch}
3957255736Sdavidch
3958255736Sdavidchstatic int
3959255736Sdavidchbxe_reset_hw(struct bxe_softc *sc,
3960255736Sdavidch             uint32_t         load_code)
3961255736Sdavidch{
3962255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
3963255736Sdavidch
3964255736Sdavidch    /* Prepare parameters for function state transitions */
3965255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3966255736Sdavidch
3967255736Sdavidch    func_params.f_obj = &sc->func_obj;
3968255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_RESET;
3969255736Sdavidch
3970255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
3971255736Sdavidch
3972255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
3973255736Sdavidch}
3974255736Sdavidch
3975255736Sdavidchstatic void
3976255736Sdavidchbxe_int_disable_sync(struct bxe_softc *sc,
3977255736Sdavidch                     int              disable_hw)
3978255736Sdavidch{
3979255736Sdavidch    if (disable_hw) {
3980255736Sdavidch        /* prevent the HW from sending interrupts */
3981255736Sdavidch        bxe_int_disable(sc);
3982255736Sdavidch    }
3983255736Sdavidch
3984255736Sdavidch    /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
3985255736Sdavidch    /* make sure all ISRs are done */
3986255736Sdavidch
3987255736Sdavidch    /* XXX make sure sp_task is not running */
3988255736Sdavidch    /* cancel and flush work queues */
3989255736Sdavidch}
3990255736Sdavidch
3991255736Sdavidchstatic void
3992255736Sdavidchbxe_chip_cleanup(struct bxe_softc *sc,
3993255736Sdavidch                 uint32_t         unload_mode,
3994255736Sdavidch                 uint8_t          keep_link)
3995255736Sdavidch{
3996255736Sdavidch    int port = SC_PORT(sc);
3997255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
3998255736Sdavidch    uint32_t reset_code;
3999255736Sdavidch    int i, rc = 0;
4000255736Sdavidch
4001255736Sdavidch    bxe_drain_tx_queues(sc);
4002255736Sdavidch
4003255736Sdavidch    /* give HW time to discard old tx messages */
4004255736Sdavidch    DELAY(1000);
4005255736Sdavidch
4006255736Sdavidch    /* Clean all ETH MACs */
4007255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4008255736Sdavidch    if (rc < 0) {
4009255736Sdavidch        BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4010255736Sdavidch    }
4011255736Sdavidch
4012255736Sdavidch    /* Clean up UC list  */
4013255736Sdavidch    rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4014255736Sdavidch    if (rc < 0) {
4015255736Sdavidch        BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4016255736Sdavidch    }
4017255736Sdavidch
4018255736Sdavidch    /* Disable LLH */
4019255736Sdavidch    if (!CHIP_IS_E1(sc)) {
4020255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4021255736Sdavidch    }
4022255736Sdavidch
4023255736Sdavidch    /* Set "drop all" to stop Rx */
4024255736Sdavidch
4025255736Sdavidch    /*
4026255736Sdavidch     * We need to take the BXE_MCAST_LOCK() here in order to prevent
4027255736Sdavidch     * a race between the completion code and this code.
4028255736Sdavidch     */
4029255736Sdavidch    BXE_MCAST_LOCK(sc);
4030255736Sdavidch
4031255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4032255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4033255736Sdavidch    } else {
4034255736Sdavidch        bxe_set_storm_rx_mode(sc);
4035255736Sdavidch    }
4036255736Sdavidch
4037255736Sdavidch    /* Clean up multicast configuration */
4038255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4039255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4040255736Sdavidch    if (rc < 0) {
4041255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4042255736Sdavidch    }
4043255736Sdavidch
4044255736Sdavidch    BXE_MCAST_UNLOCK(sc);
4045255736Sdavidch
4046255736Sdavidch    // XXX bxe_iov_chip_cleanup(sc);
4047255736Sdavidch
4048255736Sdavidch    /*
4049255736Sdavidch     * Send the UNLOAD_REQUEST to the MCP. This will return if
4050255736Sdavidch     * this function should perform FUNCTION, PORT, or COMMON HW
4051255736Sdavidch     * reset.
4052255736Sdavidch     */
4053255736Sdavidch    reset_code = bxe_send_unload_req(sc, unload_mode);
4054255736Sdavidch
4055255736Sdavidch    /*
4056255736Sdavidch     * (assumption: No Attention from MCP at this stage)
4057255736Sdavidch     * PMF probably in the middle of TX disable/enable transaction
4058255736Sdavidch     */
4059255736Sdavidch    rc = bxe_func_wait_started(sc);
4060255736Sdavidch    if (rc) {
4061295830Sdavidcs        BLOGE(sc, "bxe_func_wait_started failed (%d)\n", rc);
4062255736Sdavidch    }
4063255736Sdavidch
4064255736Sdavidch    /*
4065255736Sdavidch     * Close multi and leading connections
4066255736Sdavidch     * Completions for ramrods are collected in a synchronous way
4067255736Sdavidch     */
4068255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
4069255736Sdavidch        if (bxe_stop_queue(sc, i)) {
4070255736Sdavidch            goto unload_error;
4071255736Sdavidch        }
4072255736Sdavidch    }
4073255736Sdavidch
4074255736Sdavidch    /*
4075255736Sdavidch     * If SP settings didn't get completed so far - something
4076255736Sdavidch     * very wrong has happen.
4077255736Sdavidch     */
4078255736Sdavidch    if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4079295830Sdavidcs        BLOGE(sc, "Common slow path ramrods got stuck!(%d)\n", rc);
4080255736Sdavidch    }
4081255736Sdavidch
4082255736Sdavidchunload_error:
4083255736Sdavidch
4084255736Sdavidch    rc = bxe_func_stop(sc);
4085255736Sdavidch    if (rc) {
4086295830Sdavidcs        BLOGE(sc, "Function stop failed!(%d)\n", rc);
4087255736Sdavidch    }
4088255736Sdavidch
4089255736Sdavidch    /* disable HW interrupts */
4090255736Sdavidch    bxe_int_disable_sync(sc, TRUE);
4091255736Sdavidch
4092255736Sdavidch    /* detach interrupts */
4093255736Sdavidch    bxe_interrupt_detach(sc);
4094255736Sdavidch
4095255736Sdavidch    /* Reset the chip */
4096255736Sdavidch    rc = bxe_reset_hw(sc, reset_code);
4097255736Sdavidch    if (rc) {
4098295830Sdavidcs        BLOGE(sc, "Hardware reset failed(%d)\n", rc);
4099255736Sdavidch    }
4100255736Sdavidch
4101255736Sdavidch    /* Report UNLOAD_DONE to MCP */
4102255736Sdavidch    bxe_send_unload_done(sc, keep_link);
4103255736Sdavidch}
4104255736Sdavidch
4105255736Sdavidchstatic void
4106255736Sdavidchbxe_disable_close_the_gate(struct bxe_softc *sc)
4107255736Sdavidch{
4108255736Sdavidch    uint32_t val;
4109255736Sdavidch    int port = SC_PORT(sc);
4110255736Sdavidch
4111255736Sdavidch    BLOGD(sc, DBG_LOAD,
4112255736Sdavidch          "Disabling 'close the gates'\n");
4113255736Sdavidch
4114255736Sdavidch    if (CHIP_IS_E1(sc)) {
4115255736Sdavidch        uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4116255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
4117255736Sdavidch        val = REG_RD(sc, addr);
4118255736Sdavidch        val &= ~(0x300);
4119255736Sdavidch        REG_WR(sc, addr, val);
4120255736Sdavidch    } else {
4121255736Sdavidch        val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4122255736Sdavidch        val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4123255736Sdavidch                 MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4124255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4125255736Sdavidch    }
4126255736Sdavidch}
4127255736Sdavidch
4128255736Sdavidch/*
4129255736Sdavidch * Cleans the object that have internal lists without sending
4130255736Sdavidch * ramrods. Should be run when interrutps are disabled.
4131255736Sdavidch */
4132255736Sdavidchstatic void
4133255736Sdavidchbxe_squeeze_objects(struct bxe_softc *sc)
4134255736Sdavidch{
4135255736Sdavidch    unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4136255736Sdavidch    struct ecore_mcast_ramrod_params rparam = { NULL };
4137255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4138255736Sdavidch    int rc;
4139255736Sdavidch
4140255736Sdavidch    /* Cleanup MACs' object first... */
4141255736Sdavidch
4142255736Sdavidch    /* Wait for completion of requested */
4143255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4144255736Sdavidch    /* Perform a dry cleanup */
4145255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4146255736Sdavidch
4147255736Sdavidch    /* Clean ETH primary MAC */
4148255736Sdavidch    bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4149255736Sdavidch    rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4150255736Sdavidch                             &ramrod_flags);
4151255736Sdavidch    if (rc != 0) {
4152255736Sdavidch        BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4153255736Sdavidch    }
4154255736Sdavidch
4155255736Sdavidch    /* Cleanup UC list */
4156255736Sdavidch    vlan_mac_flags = 0;
4157255736Sdavidch    bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4158255736Sdavidch    rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4159255736Sdavidch                             &ramrod_flags);
4160255736Sdavidch    if (rc != 0) {
4161255736Sdavidch        BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4162255736Sdavidch    }
4163255736Sdavidch
4164255736Sdavidch    /* Now clean mcast object... */
4165255736Sdavidch
4166255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
4167255736Sdavidch    bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4168255736Sdavidch
4169255736Sdavidch    /* Add a DEL command... */
4170255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4171255736Sdavidch    if (rc < 0) {
4172255736Sdavidch        BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4173255736Sdavidch    }
4174255736Sdavidch
4175255736Sdavidch    /* now wait until all pending commands are cleared */
4176255736Sdavidch
4177255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4178255736Sdavidch    while (rc != 0) {
4179255736Sdavidch        if (rc < 0) {
4180255736Sdavidch            BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4181255736Sdavidch            return;
4182255736Sdavidch        }
4183255736Sdavidch
4184255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4185255736Sdavidch    }
4186255736Sdavidch}
4187255736Sdavidch
4188255736Sdavidch/* stop the controller */
4189255736Sdavidchstatic __noinline int
4190255736Sdavidchbxe_nic_unload(struct bxe_softc *sc,
4191255736Sdavidch               uint32_t         unload_mode,
4192255736Sdavidch               uint8_t          keep_link)
4193255736Sdavidch{
4194255736Sdavidch    uint8_t global = FALSE;
4195255736Sdavidch    uint32_t val;
4196255736Sdavidch
4197255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
4198255736Sdavidch
4199255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4200255736Sdavidch
4201255736Sdavidch    /* mark driver as unloaded in shmem2 */
4202255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4203255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4204255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4205255736Sdavidch                  val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4206255736Sdavidch    }
4207255736Sdavidch
4208255736Sdavidch    if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4209255736Sdavidch        (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4210255736Sdavidch        /*
4211255736Sdavidch         * We can get here if the driver has been unloaded
4212255736Sdavidch         * during parity error recovery and is either waiting for a
4213255736Sdavidch         * leader to complete or for other functions to unload and
4214255736Sdavidch         * then ifconfig down has been issued. In this case we want to
4215255736Sdavidch         * unload and let other functions to complete a recovery
4216255736Sdavidch         * process.
4217255736Sdavidch         */
4218255736Sdavidch        sc->recovery_state = BXE_RECOVERY_DONE;
4219255736Sdavidch        sc->is_leader = 0;
4220255736Sdavidch        bxe_release_leader_lock(sc);
4221255736Sdavidch        mb();
4222255736Sdavidch
4223255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4224295830Sdavidcs        BLOGE(sc, "Can't unload in closed or error state recover_state 0x%x"
4225295830Sdavidcs            " state = 0x%x\n", sc->recovery_state, sc->state);
4226255736Sdavidch        return (-1);
4227255736Sdavidch    }
4228255736Sdavidch
4229255736Sdavidch    /*
4230255736Sdavidch     * Nothing to do during unload if previous bxe_nic_load()
4231255736Sdavidch     * did not completed succesfully - all resourses are released.
4232255736Sdavidch     */
4233255736Sdavidch    if ((sc->state == BXE_STATE_CLOSED) ||
4234255736Sdavidch        (sc->state == BXE_STATE_ERROR)) {
4235255736Sdavidch        return (0);
4236255736Sdavidch    }
4237255736Sdavidch
4238255736Sdavidch    sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4239255736Sdavidch    mb();
4240255736Sdavidch
4241255736Sdavidch    /* stop tx */
4242255736Sdavidch    bxe_tx_disable(sc);
4243255736Sdavidch
4244255736Sdavidch    sc->rx_mode = BXE_RX_MODE_NONE;
4245255736Sdavidch    /* XXX set rx mode ??? */
4246255736Sdavidch
4247292639Sdavidcs    if (IS_PF(sc) && !sc->grcdump_done) {
4248255736Sdavidch        /* set ALWAYS_ALIVE bit in shmem */
4249255736Sdavidch        sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4250255736Sdavidch
4251255736Sdavidch        bxe_drv_pulse(sc);
4252255736Sdavidch
4253255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_STOP);
4254255736Sdavidch        bxe_save_statistics(sc);
4255255736Sdavidch    }
4256255736Sdavidch
4257255736Sdavidch    /* wait till consumers catch up with producers in all queues */
4258255736Sdavidch    bxe_drain_tx_queues(sc);
4259255736Sdavidch
4260255736Sdavidch    /* if VF indicate to PF this function is going down (PF will delete sp
4261255736Sdavidch     * elements and clear initializations
4262255736Sdavidch     */
4263255736Sdavidch    if (IS_VF(sc)) {
4264255736Sdavidch        ; /* bxe_vfpf_close_vf(sc); */
4265255736Sdavidch    } else if (unload_mode != UNLOAD_RECOVERY) {
4266255736Sdavidch        /* if this is a normal/close unload need to clean up chip */
4267292639Sdavidcs        if (!sc->grcdump_done)
4268292639Sdavidcs            bxe_chip_cleanup(sc, unload_mode, keep_link);
4269255736Sdavidch    } else {
4270255736Sdavidch        /* Send the UNLOAD_REQUEST to the MCP */
4271255736Sdavidch        bxe_send_unload_req(sc, unload_mode);
4272255736Sdavidch
4273255736Sdavidch        /*
4274255736Sdavidch         * Prevent transactions to host from the functions on the
4275255736Sdavidch         * engine that doesn't reset global blocks in case of global
4276255736Sdavidch         * attention once gloabl blocks are reset and gates are opened
4277255736Sdavidch         * (the engine which leader will perform the recovery
4278255736Sdavidch         * last).
4279255736Sdavidch         */
4280255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
4281255736Sdavidch            bxe_pf_disable(sc);
4282255736Sdavidch        }
4283255736Sdavidch
4284255736Sdavidch        /* disable HW interrupts */
4285255736Sdavidch        bxe_int_disable_sync(sc, TRUE);
4286255736Sdavidch
4287255736Sdavidch        /* detach interrupts */
4288255736Sdavidch        bxe_interrupt_detach(sc);
4289255736Sdavidch
4290255736Sdavidch        /* Report UNLOAD_DONE to MCP */
4291255736Sdavidch        bxe_send_unload_done(sc, FALSE);
4292255736Sdavidch    }
4293255736Sdavidch
4294255736Sdavidch    /*
4295255736Sdavidch     * At this stage no more interrupts will arrive so we may safely clean
4296255736Sdavidch     * the queue'able objects here in case they failed to get cleaned so far.
4297255736Sdavidch     */
4298255736Sdavidch    if (IS_PF(sc)) {
4299255736Sdavidch        bxe_squeeze_objects(sc);
4300255736Sdavidch    }
4301255736Sdavidch
4302255736Sdavidch    /* There should be no more pending SP commands at this stage */
4303255736Sdavidch    sc->sp_state = 0;
4304255736Sdavidch
4305255736Sdavidch    sc->port.pmf = 0;
4306255736Sdavidch
4307255736Sdavidch    bxe_free_fp_buffers(sc);
4308255736Sdavidch
4309255736Sdavidch    if (IS_PF(sc)) {
4310255736Sdavidch        bxe_free_mem(sc);
4311255736Sdavidch    }
4312255736Sdavidch
4313255736Sdavidch    bxe_free_fw_stats_mem(sc);
4314255736Sdavidch
4315255736Sdavidch    sc->state = BXE_STATE_CLOSED;
4316255736Sdavidch
4317255736Sdavidch    /*
4318255736Sdavidch     * Check if there are pending parity attentions. If there are - set
4319255736Sdavidch     * RECOVERY_IN_PROGRESS.
4320255736Sdavidch     */
4321255736Sdavidch    if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4322255736Sdavidch        bxe_set_reset_in_progress(sc);
4323255736Sdavidch
4324255736Sdavidch        /* Set RESET_IS_GLOBAL if needed */
4325255736Sdavidch        if (global) {
4326255736Sdavidch            bxe_set_reset_global(sc);
4327255736Sdavidch        }
4328255736Sdavidch    }
4329255736Sdavidch
4330255736Sdavidch    /*
4331255736Sdavidch     * The last driver must disable a "close the gate" if there is no
4332255736Sdavidch     * parity attention or "process kill" pending.
4333255736Sdavidch     */
4334255736Sdavidch    if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4335255736Sdavidch        bxe_reset_is_done(sc, SC_PATH(sc))) {
4336255736Sdavidch        bxe_disable_close_the_gate(sc);
4337255736Sdavidch    }
4338255736Sdavidch
4339255736Sdavidch    BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4340255736Sdavidch
4341255736Sdavidch    return (0);
4342255736Sdavidch}
4343255736Sdavidch
4344255736Sdavidch/*
4345255736Sdavidch * Called by the OS to set various media options (i.e. link, speed, etc.) when
4346255736Sdavidch * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4347255736Sdavidch */
4348255736Sdavidchstatic int
4349266979Smarcelbxe_ifmedia_update(struct ifnet  *ifp)
4350255736Sdavidch{
4351266979Smarcel    struct bxe_softc *sc = (struct bxe_softc *)if_getsoftc(ifp);
4352255736Sdavidch    struct ifmedia *ifm;
4353255736Sdavidch
4354255736Sdavidch    ifm = &sc->ifmedia;
4355255736Sdavidch
4356255736Sdavidch    /* We only support Ethernet media type. */
4357255736Sdavidch    if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4358255736Sdavidch        return (EINVAL);
4359255736Sdavidch    }
4360255736Sdavidch
4361255736Sdavidch    switch (IFM_SUBTYPE(ifm->ifm_media)) {
4362255736Sdavidch    case IFM_AUTO:
4363255736Sdavidch         break;
4364255736Sdavidch    case IFM_10G_CX4:
4365255736Sdavidch    case IFM_10G_SR:
4366255736Sdavidch    case IFM_10G_T:
4367255736Sdavidch    case IFM_10G_TWINAX:
4368255736Sdavidch    default:
4369255736Sdavidch        /* We don't support changing the media type. */
4370255736Sdavidch        BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4371255736Sdavidch              IFM_SUBTYPE(ifm->ifm_media));
4372255736Sdavidch        return (EINVAL);
4373255736Sdavidch    }
4374255736Sdavidch
4375255736Sdavidch    return (0);
4376255736Sdavidch}
4377255736Sdavidch
4378255736Sdavidch/*
4379255736Sdavidch * Called by the OS to get the current media status (i.e. link, speed, etc.).
4380255736Sdavidch */
4381255736Sdavidchstatic void
4382255736Sdavidchbxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4383255736Sdavidch{
4384266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4385255736Sdavidch
4386255736Sdavidch    /* Report link down if the driver isn't running. */
4387266979Smarcel    if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
4388255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4389255736Sdavidch        return;
4390255736Sdavidch    }
4391255736Sdavidch
4392255736Sdavidch    /* Setup the default interface info. */
4393255736Sdavidch    ifmr->ifm_status = IFM_AVALID;
4394255736Sdavidch    ifmr->ifm_active = IFM_ETHER;
4395255736Sdavidch
4396255736Sdavidch    if (sc->link_vars.link_up) {
4397255736Sdavidch        ifmr->ifm_status |= IFM_ACTIVE;
4398255736Sdavidch    } else {
4399255736Sdavidch        ifmr->ifm_active |= IFM_NONE;
4400255736Sdavidch        return;
4401255736Sdavidch    }
4402255736Sdavidch
4403255736Sdavidch    ifmr->ifm_active |= sc->media;
4404255736Sdavidch
4405255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
4406255736Sdavidch        ifmr->ifm_active |= IFM_FDX;
4407255736Sdavidch    } else {
4408255736Sdavidch        ifmr->ifm_active |= IFM_HDX;
4409255736Sdavidch    }
4410255736Sdavidch}
4411255736Sdavidch
4412255736Sdavidchstatic int
4413255736Sdavidchbxe_ioctl_nvram(struct bxe_softc *sc,
4414255736Sdavidch                uint32_t         priv_op,
4415255736Sdavidch                struct ifreq     *ifr)
4416255736Sdavidch{
4417255736Sdavidch    struct bxe_nvram_data nvdata_base;
4418255736Sdavidch    struct bxe_nvram_data *nvdata;
4419255736Sdavidch    int len;
4420255736Sdavidch    int error = 0;
4421255736Sdavidch
4422255736Sdavidch    copyin(ifr->ifr_data, &nvdata_base, sizeof(nvdata_base));
4423255736Sdavidch
4424255736Sdavidch    len = (sizeof(struct bxe_nvram_data) +
4425255736Sdavidch           nvdata_base.len -
4426255736Sdavidch           sizeof(uint32_t));
4427255736Sdavidch
4428255736Sdavidch    if (len > sizeof(struct bxe_nvram_data)) {
4429255736Sdavidch        if ((nvdata = (struct bxe_nvram_data *)
4430255736Sdavidch                 malloc(len, M_DEVBUF,
4431255736Sdavidch                        (M_NOWAIT | M_ZERO))) == NULL) {
4432295830Sdavidcs            BLOGE(sc, "BXE_IOC_RD_NVRAM malloc failed priv_op 0x%x "
4433295830Sdavidcs                " len = 0x%x\n", priv_op, len);
4434255736Sdavidch            return (1);
4435255736Sdavidch        }
4436255736Sdavidch        memcpy(nvdata, &nvdata_base, sizeof(struct bxe_nvram_data));
4437255736Sdavidch    } else {
4438255736Sdavidch        nvdata = &nvdata_base;
4439255736Sdavidch    }
4440255736Sdavidch
4441255736Sdavidch    if (priv_op == BXE_IOC_RD_NVRAM) {
4442255736Sdavidch        BLOGD(sc, DBG_IOCTL, "IOC_RD_NVRAM 0x%x %d\n",
4443255736Sdavidch              nvdata->offset, nvdata->len);
4444255736Sdavidch        error = bxe_nvram_read(sc,
4445255736Sdavidch                               nvdata->offset,
4446255736Sdavidch                               (uint8_t *)nvdata->value,
4447255736Sdavidch                               nvdata->len);
4448255736Sdavidch        copyout(nvdata, ifr->ifr_data, len);
4449255736Sdavidch    } else { /* BXE_IOC_WR_NVRAM */
4450255736Sdavidch        BLOGD(sc, DBG_IOCTL, "IOC_WR_NVRAM 0x%x %d\n",
4451255736Sdavidch              nvdata->offset, nvdata->len);
4452255736Sdavidch        copyin(ifr->ifr_data, nvdata, len);
4453255736Sdavidch        error = bxe_nvram_write(sc,
4454255736Sdavidch                                nvdata->offset,
4455255736Sdavidch                                (uint8_t *)nvdata->value,
4456255736Sdavidch                                nvdata->len);
4457255736Sdavidch    }
4458255736Sdavidch
4459255736Sdavidch    if (len > sizeof(struct bxe_nvram_data)) {
4460255736Sdavidch        free(nvdata, M_DEVBUF);
4461255736Sdavidch    }
4462255736Sdavidch
4463255736Sdavidch    return (error);
4464255736Sdavidch}
4465255736Sdavidch
4466255736Sdavidchstatic int
4467255736Sdavidchbxe_ioctl_stats_show(struct bxe_softc *sc,
4468255736Sdavidch                     uint32_t         priv_op,
4469255736Sdavidch                     struct ifreq     *ifr)
4470255736Sdavidch{
4471255736Sdavidch    const size_t str_size   = (BXE_NUM_ETH_STATS * STAT_NAME_LEN);
4472255736Sdavidch    const size_t stats_size = (BXE_NUM_ETH_STATS * sizeof(uint64_t));
4473255736Sdavidch    caddr_t p_tmp;
4474255736Sdavidch    uint32_t *offset;
4475255736Sdavidch    int i;
4476255736Sdavidch
4477255736Sdavidch    switch (priv_op)
4478255736Sdavidch    {
4479255736Sdavidch    case BXE_IOC_STATS_SHOW_NUM:
4480255736Sdavidch        memset(ifr->ifr_data, 0, sizeof(union bxe_stats_show_data));
4481255736Sdavidch        ((union bxe_stats_show_data *)ifr->ifr_data)->desc.num =
4482255736Sdavidch            BXE_NUM_ETH_STATS;
4483255736Sdavidch        ((union bxe_stats_show_data *)ifr->ifr_data)->desc.len =
4484255736Sdavidch            STAT_NAME_LEN;
4485255736Sdavidch        return (0);
4486255736Sdavidch
4487255736Sdavidch    case BXE_IOC_STATS_SHOW_STR:
4488255736Sdavidch        memset(ifr->ifr_data, 0, str_size);
4489255736Sdavidch        p_tmp = ifr->ifr_data;
4490255736Sdavidch        for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4491255736Sdavidch            strcpy(p_tmp, bxe_eth_stats_arr[i].string);
4492255736Sdavidch            p_tmp += STAT_NAME_LEN;
4493255736Sdavidch        }
4494255736Sdavidch        return (0);
4495255736Sdavidch
4496255736Sdavidch    case BXE_IOC_STATS_SHOW_CNT:
4497255736Sdavidch        memset(ifr->ifr_data, 0, stats_size);
4498255736Sdavidch        p_tmp = ifr->ifr_data;
4499255736Sdavidch        for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4500255736Sdavidch            offset = ((uint32_t *)&sc->eth_stats +
4501255736Sdavidch                      bxe_eth_stats_arr[i].offset);
4502255736Sdavidch            switch (bxe_eth_stats_arr[i].size) {
4503255736Sdavidch            case 4:
4504255736Sdavidch                *((uint64_t *)p_tmp) = (uint64_t)*offset;
4505255736Sdavidch                break;
4506255736Sdavidch            case 8:
4507255736Sdavidch                *((uint64_t *)p_tmp) = HILO_U64(*offset, *(offset + 1));
4508255736Sdavidch                break;
4509255736Sdavidch            default:
4510255736Sdavidch                *((uint64_t *)p_tmp) = 0;
4511255736Sdavidch            }
4512255736Sdavidch            p_tmp += sizeof(uint64_t);
4513255736Sdavidch        }
4514255736Sdavidch        return (0);
4515255736Sdavidch
4516255736Sdavidch    default:
4517255736Sdavidch        return (-1);
4518255736Sdavidch    }
4519255736Sdavidch}
4520255736Sdavidch
4521255736Sdavidchstatic void
4522255736Sdavidchbxe_handle_chip_tq(void *context,
4523255736Sdavidch                   int  pending)
4524255736Sdavidch{
4525255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
4526255736Sdavidch    long work = atomic_load_acq_long(&sc->chip_tq_flags);
4527255736Sdavidch
4528255736Sdavidch    switch (work)
4529255736Sdavidch    {
4530255736Sdavidch
4531255736Sdavidch    case CHIP_TQ_REINIT:
4532266979Smarcel        if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4533255736Sdavidch            /* restart the interface */
4534255736Sdavidch            BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4535255736Sdavidch            bxe_periodic_stop(sc);
4536255736Sdavidch            BXE_CORE_LOCK(sc);
4537255736Sdavidch            bxe_stop_locked(sc);
4538255736Sdavidch            bxe_init_locked(sc);
4539255736Sdavidch            BXE_CORE_UNLOCK(sc);
4540255736Sdavidch        }
4541255736Sdavidch        break;
4542255736Sdavidch
4543255736Sdavidch    default:
4544255736Sdavidch        break;
4545255736Sdavidch    }
4546255736Sdavidch}
4547255736Sdavidch
4548255736Sdavidch/*
4549255736Sdavidch * Handles any IOCTL calls from the operating system.
4550255736Sdavidch *
4551255736Sdavidch * Returns:
4552255736Sdavidch *   0 = Success, >0 Failure
4553255736Sdavidch */
4554255736Sdavidchstatic int
4555266979Smarcelbxe_ioctl(if_t ifp,
4556255736Sdavidch          u_long       command,
4557255736Sdavidch          caddr_t      data)
4558255736Sdavidch{
4559266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
4560255736Sdavidch    struct ifreq *ifr = (struct ifreq *)data;
4561255736Sdavidch    struct bxe_nvram_data *nvdata;
4562255736Sdavidch    uint32_t priv_op;
4563255736Sdavidch    int mask = 0;
4564255736Sdavidch    int reinit = 0;
4565255736Sdavidch    int error = 0;
4566255736Sdavidch
4567255736Sdavidch    int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4568255736Sdavidch    int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4569255736Sdavidch
4570255736Sdavidch    switch (command)
4571255736Sdavidch    {
4572255736Sdavidch    case SIOCSIFMTU:
4573255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4574255736Sdavidch              ifr->ifr_mtu);
4575255736Sdavidch
4576255736Sdavidch        if (sc->mtu == ifr->ifr_mtu) {
4577255736Sdavidch            /* nothing to change */
4578255736Sdavidch            break;
4579255736Sdavidch        }
4580255736Sdavidch
4581255736Sdavidch        if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4582255736Sdavidch            BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4583255736Sdavidch                  ifr->ifr_mtu, mtu_min, mtu_max);
4584255736Sdavidch            error = EINVAL;
4585255736Sdavidch            break;
4586255736Sdavidch        }
4587255736Sdavidch
4588255736Sdavidch        atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4589255736Sdavidch                             (unsigned long)ifr->ifr_mtu);
4590266979Smarcel	/*
4591266979Smarcel        atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4592255736Sdavidch                              (unsigned long)ifr->ifr_mtu);
4593266979Smarcel	XXX - Not sure why it needs to be atomic
4594266979Smarcel	*/
4595266979Smarcel	if_setmtu(ifp, ifr->ifr_mtu);
4596255736Sdavidch        reinit = 1;
4597255736Sdavidch        break;
4598255736Sdavidch
4599255736Sdavidch    case SIOCSIFFLAGS:
4600255736Sdavidch        /* toggle the interface state up or down */
4601255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4602255736Sdavidch
4603284335Sdavidcs	BXE_CORE_LOCK(sc);
4604255736Sdavidch        /* check if the interface is up */
4605266979Smarcel        if (if_getflags(ifp) & IFF_UP) {
4606266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4607255736Sdavidch                /* set the receive mode flags */
4608255736Sdavidch                bxe_set_rx_mode(sc);
4609255736Sdavidch            } else {
4610284335Sdavidcs		bxe_init_locked(sc);
4611255736Sdavidch            }
4612255736Sdavidch        } else {
4613266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4614284335Sdavidcs		bxe_periodic_stop(sc);
4615284335Sdavidcs		bxe_stop_locked(sc);
4616255736Sdavidch            }
4617255736Sdavidch        }
4618284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4619255736Sdavidch
4620255736Sdavidch        break;
4621255736Sdavidch
4622255736Sdavidch    case SIOCADDMULTI:
4623255736Sdavidch    case SIOCDELMULTI:
4624255736Sdavidch        /* add/delete multicast addresses */
4625255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4626255736Sdavidch
4627255736Sdavidch        /* check if the interface is up */
4628266979Smarcel        if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4629255736Sdavidch            /* set the receive mode flags */
4630284335Sdavidcs	    BXE_CORE_LOCK(sc);
4631255736Sdavidch            bxe_set_rx_mode(sc);
4632284335Sdavidcs	    BXE_CORE_UNLOCK(sc);
4633255736Sdavidch        }
4634255736Sdavidch
4635255736Sdavidch        break;
4636255736Sdavidch
4637255736Sdavidch    case SIOCSIFCAP:
4638255736Sdavidch        /* find out which capabilities have changed */
4639266979Smarcel        mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4640255736Sdavidch
4641255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4642255736Sdavidch              mask);
4643255736Sdavidch
4644255736Sdavidch        /* toggle the LRO capabilites enable flag */
4645255736Sdavidch        if (mask & IFCAP_LRO) {
4646266979Smarcel	    if_togglecapenable(ifp, IFCAP_LRO);
4647255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4648266979Smarcel                  (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4649255736Sdavidch            reinit = 1;
4650255736Sdavidch        }
4651255736Sdavidch
4652255736Sdavidch        /* toggle the TXCSUM checksum capabilites enable flag */
4653255736Sdavidch        if (mask & IFCAP_TXCSUM) {
4654266979Smarcel	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4655255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4656266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4657266979Smarcel            if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4658266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4659255736Sdavidch                                    CSUM_TCP      |
4660255736Sdavidch                                    CSUM_UDP      |
4661255736Sdavidch                                    CSUM_TSO      |
4662255736Sdavidch                                    CSUM_TCP_IPV6 |
4663266979Smarcel                                    CSUM_UDP_IPV6), 0);
4664255736Sdavidch            } else {
4665266979Smarcel		if_clearhwassist(ifp); /* XXX */
4666255736Sdavidch            }
4667255736Sdavidch        }
4668255736Sdavidch
4669255736Sdavidch        /* toggle the RXCSUM checksum capabilities enable flag */
4670255736Sdavidch        if (mask & IFCAP_RXCSUM) {
4671266979Smarcel	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4672255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4673266979Smarcel                  (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4674266979Smarcel            if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4675266979Smarcel                if_sethwassistbits(ifp, (CSUM_IP      |
4676255736Sdavidch                                    CSUM_TCP      |
4677255736Sdavidch                                    CSUM_UDP      |
4678255736Sdavidch                                    CSUM_TSO      |
4679255736Sdavidch                                    CSUM_TCP_IPV6 |
4680266979Smarcel                                    CSUM_UDP_IPV6), 0);
4681255736Sdavidch            } else {
4682266979Smarcel		if_clearhwassist(ifp); /* XXX */
4683255736Sdavidch            }
4684255736Sdavidch        }
4685255736Sdavidch
4686255736Sdavidch        /* toggle TSO4 capabilities enabled flag */
4687255736Sdavidch        if (mask & IFCAP_TSO4) {
4688266979Smarcel            if_togglecapenable(ifp, IFCAP_TSO4);
4689255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4690266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4691255736Sdavidch        }
4692255736Sdavidch
4693255736Sdavidch        /* toggle TSO6 capabilities enabled flag */
4694255736Sdavidch        if (mask & IFCAP_TSO6) {
4695266979Smarcel	    if_togglecapenable(ifp, IFCAP_TSO6);
4696255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4697266979Smarcel                  (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4698255736Sdavidch        }
4699255736Sdavidch
4700255736Sdavidch        /* toggle VLAN_HWTSO capabilities enabled flag */
4701255736Sdavidch        if (mask & IFCAP_VLAN_HWTSO) {
4702266979Smarcel
4703266979Smarcel	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4704255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4705266979Smarcel                  (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4706255736Sdavidch        }
4707255736Sdavidch
4708255736Sdavidch        /* toggle VLAN_HWCSUM capabilities enabled flag */
4709255736Sdavidch        if (mask & IFCAP_VLAN_HWCSUM) {
4710255736Sdavidch            /* XXX investigate this... */
4711255736Sdavidch            BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4712255736Sdavidch            error = EINVAL;
4713255736Sdavidch        }
4714255736Sdavidch
4715255736Sdavidch        /* toggle VLAN_MTU capabilities enable flag */
4716255736Sdavidch        if (mask & IFCAP_VLAN_MTU) {
4717255736Sdavidch            /* XXX investigate this... */
4718255736Sdavidch            BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4719255736Sdavidch            error = EINVAL;
4720255736Sdavidch        }
4721255736Sdavidch
4722255736Sdavidch        /* toggle VLAN_HWTAGGING capabilities enabled flag */
4723255736Sdavidch        if (mask & IFCAP_VLAN_HWTAGGING) {
4724255736Sdavidch            /* XXX investigate this... */
4725255736Sdavidch            BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4726255736Sdavidch            error = EINVAL;
4727255736Sdavidch        }
4728255736Sdavidch
4729255736Sdavidch        /* toggle VLAN_HWFILTER capabilities enabled flag */
4730255736Sdavidch        if (mask & IFCAP_VLAN_HWFILTER) {
4731255736Sdavidch            /* XXX investigate this... */
4732255736Sdavidch            BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4733255736Sdavidch            error = EINVAL;
4734255736Sdavidch        }
4735255736Sdavidch
4736255736Sdavidch        /* XXX not yet...
4737255736Sdavidch         * IFCAP_WOL_MAGIC
4738255736Sdavidch         */
4739255736Sdavidch
4740255736Sdavidch        break;
4741255736Sdavidch
4742255736Sdavidch    case SIOCSIFMEDIA:
4743255736Sdavidch    case SIOCGIFMEDIA:
4744255736Sdavidch        /* set/get interface media */
4745255736Sdavidch        BLOGD(sc, DBG_IOCTL,
4746255736Sdavidch              "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4747255736Sdavidch              (command & 0xff));
4748270876Sglebius        error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4749255736Sdavidch        break;
4750255736Sdavidch
4751255736Sdavidch    case SIOCGPRIVATE_0:
4752255736Sdavidch        copyin(ifr->ifr_data, &priv_op, sizeof(priv_op));
4753255736Sdavidch
4754255736Sdavidch        switch (priv_op)
4755255736Sdavidch        {
4756255736Sdavidch        case BXE_IOC_RD_NVRAM:
4757255736Sdavidch        case BXE_IOC_WR_NVRAM:
4758255736Sdavidch            nvdata = (struct bxe_nvram_data *)ifr->ifr_data;
4759255736Sdavidch            BLOGD(sc, DBG_IOCTL,
4760255736Sdavidch                  "Received Private NVRAM ioctl addr=0x%x size=%u\n",
4761255736Sdavidch                  nvdata->offset, nvdata->len);
4762255736Sdavidch            error = bxe_ioctl_nvram(sc, priv_op, ifr);
4763255736Sdavidch            break;
4764255736Sdavidch
4765255736Sdavidch        case BXE_IOC_STATS_SHOW_NUM:
4766255736Sdavidch        case BXE_IOC_STATS_SHOW_STR:
4767255736Sdavidch        case BXE_IOC_STATS_SHOW_CNT:
4768255736Sdavidch            BLOGD(sc, DBG_IOCTL, "Received Private Stats ioctl (%d)\n",
4769255736Sdavidch                  priv_op);
4770255736Sdavidch            error = bxe_ioctl_stats_show(sc, priv_op, ifr);
4771255736Sdavidch            break;
4772255736Sdavidch
4773255736Sdavidch        default:
4774255736Sdavidch            BLOGW(sc, "Received Private Unknown ioctl (%d)\n", priv_op);
4775255736Sdavidch            error = EINVAL;
4776255736Sdavidch            break;
4777255736Sdavidch        }
4778255736Sdavidch
4779255736Sdavidch        break;
4780255736Sdavidch
4781255736Sdavidch    default:
4782255736Sdavidch        BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4783255736Sdavidch              (command & 0xff));
4784270876Sglebius        error = ether_ioctl(ifp, command, data);
4785255736Sdavidch        break;
4786255736Sdavidch    }
4787255736Sdavidch
4788266979Smarcel    if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
4789255736Sdavidch        BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4790255736Sdavidch              "Re-initializing hardware from IOCTL change\n");
4791284335Sdavidcs	bxe_periodic_stop(sc);
4792284335Sdavidcs	BXE_CORE_LOCK(sc);
4793284335Sdavidcs	bxe_stop_locked(sc);
4794284335Sdavidcs	bxe_init_locked(sc);
4795284335Sdavidcs	BXE_CORE_UNLOCK(sc);
4796255736Sdavidch    }
4797255736Sdavidch
4798255736Sdavidch    return (error);
4799255736Sdavidch}
4800255736Sdavidch
4801255736Sdavidchstatic __noinline void
4802255736Sdavidchbxe_dump_mbuf(struct bxe_softc *sc,
4803255736Sdavidch              struct mbuf      *m,
4804255736Sdavidch              uint8_t          contents)
4805255736Sdavidch{
4806255736Sdavidch    char * type;
4807260113Sedavis    int i = 0;
4808255736Sdavidch
4809255736Sdavidch    if (!(sc->debug & DBG_MBUF)) {
4810255736Sdavidch        return;
4811255736Sdavidch    }
4812255736Sdavidch
4813255736Sdavidch    if (m == NULL) {
4814255736Sdavidch        BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4815255736Sdavidch        return;
4816255736Sdavidch    }
4817255736Sdavidch
4818255736Sdavidch    while (m) {
4819255736Sdavidch        BLOGD(sc, DBG_MBUF,
4820260113Sedavis              "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4821260113Sedavis              i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
4822255736Sdavidch
4823255736Sdavidch        if (m->m_flags & M_PKTHDR) {
4824255736Sdavidch             BLOGD(sc, DBG_MBUF,
4825260113Sedavis                   "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4826260113Sedavis                   i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
4827260113Sedavis                   (int)m->m_pkthdr.csum_flags, CSUM_BITS);
4828255736Sdavidch        }
4829255736Sdavidch
4830255736Sdavidch        if (m->m_flags & M_EXT) {
4831255736Sdavidch            switch (m->m_ext.ext_type) {
4832255736Sdavidch            case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4833255736Sdavidch            case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4834260113Sedavis            case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4835255736Sdavidch            case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4836255736Sdavidch            case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4837255736Sdavidch            case EXT_PACKET:     type = "EXT_PACKET";     break;
4838255736Sdavidch            case EXT_MBUF:       type = "EXT_MBUF";       break;
4839255736Sdavidch            case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4840255736Sdavidch            case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4841255736Sdavidch            case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4842255736Sdavidch            case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4843255736Sdavidch            default:             type = "UNKNOWN";        break;
4844255736Sdavidch            }
4845255736Sdavidch
4846255736Sdavidch            BLOGD(sc, DBG_MBUF,
4847260113Sedavis                  "%02d: - m_ext: %p ext_size=%d type=%s\n",
4848260113Sedavis                  i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4849255736Sdavidch        }
4850255736Sdavidch
4851255736Sdavidch        if (contents) {
4852255736Sdavidch            bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4853255736Sdavidch        }
4854255736Sdavidch
4855255736Sdavidch        m = m->m_next;
4856260113Sedavis        i++;
4857255736Sdavidch    }
4858255736Sdavidch}
4859255736Sdavidch
4860255736Sdavidch/*
4861255736Sdavidch * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4862255736Sdavidch * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4863255736Sdavidch * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4864255736Sdavidch * The headers comes in a seperate bd in FreeBSD so 13-3=10.
4865255736Sdavidch * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4866255736Sdavidch */
4867255736Sdavidchstatic int
4868255736Sdavidchbxe_chktso_window(struct bxe_softc  *sc,
4869255736Sdavidch                  int               nsegs,
4870255736Sdavidch                  bus_dma_segment_t *segs,
4871255736Sdavidch                  struct mbuf       *m)
4872255736Sdavidch{
4873255736Sdavidch    uint32_t num_wnds, wnd_size, wnd_sum;
4874255736Sdavidch    int32_t frag_idx, wnd_idx;
4875255736Sdavidch    unsigned short lso_mss;
4876255736Sdavidch    int defrag;
4877255736Sdavidch
4878255736Sdavidch    defrag = 0;
4879255736Sdavidch    wnd_sum = 0;
4880255736Sdavidch    wnd_size = 10;
4881255736Sdavidch    num_wnds = nsegs - wnd_size;
4882255736Sdavidch    lso_mss = htole16(m->m_pkthdr.tso_segsz);
4883255736Sdavidch
4884255736Sdavidch    /*
4885255736Sdavidch     * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4886255736Sdavidch     * first window sum of data while skipping the first assuming it is the
4887255736Sdavidch     * header in FreeBSD.
4888255736Sdavidch     */
4889255736Sdavidch    for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4890255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4891255736Sdavidch    }
4892255736Sdavidch
4893255736Sdavidch    /* check the first 10 bd window size */
4894255736Sdavidch    if (wnd_sum < lso_mss) {
4895255736Sdavidch        return (1);
4896255736Sdavidch    }
4897255736Sdavidch
4898255736Sdavidch    /* run through the windows */
4899255736Sdavidch    for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4900255736Sdavidch        /* subtract the first mbuf->m_len of the last wndw(-header) */
4901255736Sdavidch        wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4902255736Sdavidch        /* add the next mbuf len to the len of our new window */
4903255736Sdavidch        wnd_sum += htole16(segs[frag_idx].ds_len);
4904255736Sdavidch        if (wnd_sum < lso_mss) {
4905255736Sdavidch            return (1);
4906255736Sdavidch        }
4907255736Sdavidch    }
4908255736Sdavidch
4909255736Sdavidch    return (0);
4910255736Sdavidch}
4911255736Sdavidch
4912255736Sdavidchstatic uint8_t
4913255736Sdavidchbxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4914255736Sdavidch                    struct mbuf         *m,
4915255736Sdavidch                    uint32_t            *parsing_data)
4916255736Sdavidch{
4917255736Sdavidch    struct ether_vlan_header *eh = NULL;
4918255736Sdavidch    struct ip *ip4 = NULL;
4919255736Sdavidch    struct ip6_hdr *ip6 = NULL;
4920255736Sdavidch    caddr_t ip = NULL;
4921255736Sdavidch    struct tcphdr *th = NULL;
4922255736Sdavidch    int e_hlen, ip_hlen, l4_off;
4923255736Sdavidch    uint16_t proto;
4924255736Sdavidch
4925255736Sdavidch    if (m->m_pkthdr.csum_flags == CSUM_IP) {
4926255736Sdavidch        /* no L4 checksum offload needed */
4927255736Sdavidch        return (0);
4928255736Sdavidch    }
4929255736Sdavidch
4930255736Sdavidch    /* get the Ethernet header */
4931255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
4932255736Sdavidch
4933255736Sdavidch    /* handle VLAN encapsulation if present */
4934255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4935255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4936255736Sdavidch        proto  = ntohs(eh->evl_proto);
4937255736Sdavidch    } else {
4938255736Sdavidch        e_hlen = ETHER_HDR_LEN;
4939255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
4940255736Sdavidch    }
4941255736Sdavidch
4942255736Sdavidch    switch (proto) {
4943255736Sdavidch    case ETHERTYPE_IP:
4944255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
4945255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
4946255736Sdavidch                  (struct ip *)m->m_next->m_data :
4947255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
4948255736Sdavidch        /* ip_hl is number of 32-bit words */
4949255736Sdavidch        ip_hlen = (ip4->ip_hl << 2);
4950255736Sdavidch        ip = (caddr_t)ip4;
4951255736Sdavidch        break;
4952255736Sdavidch    case ETHERTYPE_IPV6:
4953255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4954255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4955255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
4956255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
4957255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
4958255736Sdavidch        ip_hlen = sizeof(struct ip6_hdr);
4959255736Sdavidch        ip = (caddr_t)ip6;
4960255736Sdavidch        break;
4961255736Sdavidch    default:
4962255736Sdavidch        /* We can't offload in this case... */
4963255736Sdavidch        /* XXX error stat ??? */
4964255736Sdavidch        return (0);
4965255736Sdavidch    }
4966255736Sdavidch
4967255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4968255736Sdavidch    l4_off = (e_hlen + ip_hlen);
4969255736Sdavidch
4970255736Sdavidch    *parsing_data |=
4971255736Sdavidch        (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
4972255736Sdavidch         ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
4973255736Sdavidch
4974255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4975255736Sdavidch                                  CSUM_TSO |
4976255736Sdavidch                                  CSUM_TCP_IPV6)) {
4977255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4978255736Sdavidch        th = (struct tcphdr *)(ip + ip_hlen);
4979255736Sdavidch        /* th_off is number of 32-bit words */
4980255736Sdavidch        *parsing_data |= ((th->th_off <<
4981255736Sdavidch                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
4982255736Sdavidch                          ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
4983255736Sdavidch        return (l4_off + (th->th_off << 2)); /* entire header length */
4984255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4985255736Sdavidch                                         CSUM_UDP_IPV6)) {
4986255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4987255736Sdavidch        return (l4_off + sizeof(struct udphdr)); /* entire header length */
4988255736Sdavidch    } else {
4989255736Sdavidch        /* XXX error stat ??? */
4990255736Sdavidch        return (0);
4991255736Sdavidch    }
4992255736Sdavidch}
4993255736Sdavidch
4994255736Sdavidchstatic uint8_t
4995255736Sdavidchbxe_set_pbd_csum(struct bxe_fastpath        *fp,
4996255736Sdavidch                 struct mbuf                *m,
4997255736Sdavidch                 struct eth_tx_parse_bd_e1x *pbd)
4998255736Sdavidch{
4999255736Sdavidch    struct ether_vlan_header *eh = NULL;
5000255736Sdavidch    struct ip *ip4 = NULL;
5001255736Sdavidch    struct ip6_hdr *ip6 = NULL;
5002255736Sdavidch    caddr_t ip = NULL;
5003255736Sdavidch    struct tcphdr *th = NULL;
5004255736Sdavidch    struct udphdr *uh = NULL;
5005255736Sdavidch    int e_hlen, ip_hlen;
5006255736Sdavidch    uint16_t proto;
5007255736Sdavidch    uint8_t hlen;
5008255736Sdavidch    uint16_t tmp_csum;
5009255736Sdavidch    uint32_t *tmp_uh;
5010255736Sdavidch
5011255736Sdavidch    /* get the Ethernet header */
5012255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5013255736Sdavidch
5014255736Sdavidch    /* handle VLAN encapsulation if present */
5015255736Sdavidch    if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
5016255736Sdavidch        e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5017255736Sdavidch        proto  = ntohs(eh->evl_proto);
5018255736Sdavidch    } else {
5019255736Sdavidch        e_hlen = ETHER_HDR_LEN;
5020255736Sdavidch        proto  = ntohs(eh->evl_encap_proto);
5021255736Sdavidch    }
5022255736Sdavidch
5023255736Sdavidch    switch (proto) {
5024255736Sdavidch    case ETHERTYPE_IP:
5025255736Sdavidch        /* get the IP header, if mbuf len < 20 then header in next mbuf */
5026255736Sdavidch        ip4 = (m->m_len < sizeof(struct ip)) ?
5027255736Sdavidch                  (struct ip *)m->m_next->m_data :
5028255736Sdavidch                  (struct ip *)(m->m_data + e_hlen);
5029255736Sdavidch        /* ip_hl is number of 32-bit words */
5030255736Sdavidch        ip_hlen = (ip4->ip_hl << 1);
5031255736Sdavidch        ip = (caddr_t)ip4;
5032255736Sdavidch        break;
5033255736Sdavidch    case ETHERTYPE_IPV6:
5034255736Sdavidch        /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
5035255736Sdavidch        ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
5036255736Sdavidch                  (struct ip6_hdr *)m->m_next->m_data :
5037255736Sdavidch                  (struct ip6_hdr *)(m->m_data + e_hlen);
5038255736Sdavidch        /* XXX cannot support offload with IPv6 extensions */
5039255736Sdavidch        ip_hlen = (sizeof(struct ip6_hdr) >> 1);
5040255736Sdavidch        ip = (caddr_t)ip6;
5041255736Sdavidch        break;
5042255736Sdavidch    default:
5043255736Sdavidch        /* We can't offload in this case... */
5044255736Sdavidch        /* XXX error stat ??? */
5045255736Sdavidch        return (0);
5046255736Sdavidch    }
5047255736Sdavidch
5048255736Sdavidch    hlen = (e_hlen >> 1);
5049255736Sdavidch
5050255736Sdavidch    /* note that rest of global_data is indirectly zeroed here */
5051255736Sdavidch    if (m->m_flags & M_VLANTAG) {
5052255736Sdavidch        pbd->global_data =
5053255736Sdavidch            htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
5054255736Sdavidch    } else {
5055255736Sdavidch        pbd->global_data = htole16(hlen);
5056255736Sdavidch    }
5057255736Sdavidch
5058255736Sdavidch    pbd->ip_hlen_w = ip_hlen;
5059255736Sdavidch
5060255736Sdavidch    hlen += pbd->ip_hlen_w;
5061255736Sdavidch
5062255736Sdavidch    /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5063255736Sdavidch
5064255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5065255736Sdavidch                                  CSUM_TSO |
5066255736Sdavidch                                  CSUM_TCP_IPV6)) {
5067255736Sdavidch        th = (struct tcphdr *)(ip + (ip_hlen << 1));
5068255736Sdavidch        /* th_off is number of 32-bit words */
5069255736Sdavidch        hlen += (uint16_t)(th->th_off << 1);
5070255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5071255736Sdavidch                                         CSUM_UDP_IPV6)) {
5072255736Sdavidch        uh = (struct udphdr *)(ip + (ip_hlen << 1));
5073255736Sdavidch        hlen += (sizeof(struct udphdr) / 2);
5074255736Sdavidch    } else {
5075255736Sdavidch        /* valid case as only CSUM_IP was set */
5076255736Sdavidch        return (0);
5077255736Sdavidch    }
5078255736Sdavidch
5079255736Sdavidch    pbd->total_hlen_w = htole16(hlen);
5080255736Sdavidch
5081255736Sdavidch    if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5082255736Sdavidch                                  CSUM_TSO |
5083255736Sdavidch                                  CSUM_TCP_IPV6)) {
5084255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5085255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5086255736Sdavidch    } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5087255736Sdavidch                                         CSUM_UDP_IPV6)) {
5088255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5089255736Sdavidch
5090255736Sdavidch        /*
5091255736Sdavidch         * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5092255736Sdavidch         * checksums and does not know anything about the UDP header and where
5093255736Sdavidch         * the checksum field is located. It only knows about TCP. Therefore
5094255736Sdavidch         * we "lie" to the hardware for outgoing UDP packets w/ checksum
5095255736Sdavidch         * offload. Since the checksum field offset for TCP is 16 bytes and
5096255736Sdavidch         * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5097255736Sdavidch         * bytes less than the start of the UDP header. This allows the
5098255736Sdavidch         * hardware to write the checksum in the correct spot. But the
5099255736Sdavidch         * hardware will compute a checksum which includes the last 10 bytes
5100255736Sdavidch         * of the IP header. To correct this we tweak the stack computed
5101255736Sdavidch         * pseudo checksum by folding in the calculation of the inverse
5102255736Sdavidch         * checksum for those final 10 bytes of the IP header. This allows
5103255736Sdavidch         * the correct checksum to be computed by the hardware.
5104255736Sdavidch         */
5105255736Sdavidch
5106255736Sdavidch        /* set pointer 10 bytes before UDP header */
5107255736Sdavidch        tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5108255736Sdavidch
5109255736Sdavidch        /* calculate a pseudo header checksum over the first 10 bytes */
5110255736Sdavidch        tmp_csum = in_pseudo(*tmp_uh,
5111255736Sdavidch                             *(tmp_uh + 1),
5112255736Sdavidch                             *(uint16_t *)(tmp_uh + 2));
5113255736Sdavidch
5114255736Sdavidch        pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5115255736Sdavidch    }
5116255736Sdavidch
5117255736Sdavidch    return (hlen * 2); /* entire header length, number of bytes */
5118255736Sdavidch}
5119255736Sdavidch
5120255736Sdavidchstatic void
5121255736Sdavidchbxe_set_pbd_lso_e2(struct mbuf *m,
5122255736Sdavidch                   uint32_t    *parsing_data)
5123255736Sdavidch{
5124255736Sdavidch    *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5125255736Sdavidch                       ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5126255736Sdavidch                      ETH_TX_PARSE_BD_E2_LSO_MSS);
5127255736Sdavidch
5128255736Sdavidch    /* XXX test for IPv6 with extension header... */
5129255736Sdavidch}
5130255736Sdavidch
5131255736Sdavidchstatic void
5132255736Sdavidchbxe_set_pbd_lso(struct mbuf                *m,
5133255736Sdavidch                struct eth_tx_parse_bd_e1x *pbd)
5134255736Sdavidch{
5135255736Sdavidch    struct ether_vlan_header *eh = NULL;
5136255736Sdavidch    struct ip *ip = NULL;
5137255736Sdavidch    struct tcphdr *th = NULL;
5138255736Sdavidch    int e_hlen;
5139255736Sdavidch
5140255736Sdavidch    /* get the Ethernet header */
5141255736Sdavidch    eh = mtod(m, struct ether_vlan_header *);
5142255736Sdavidch
5143255736Sdavidch    /* handle VLAN encapsulation if present */
5144255736Sdavidch    e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5145255736Sdavidch                 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5146255736Sdavidch
5147255736Sdavidch    /* get the IP and TCP header, with LSO entire header in first mbuf */
5148255736Sdavidch    /* XXX assuming IPv4 */
5149255736Sdavidch    ip = (struct ip *)(m->m_data + e_hlen);
5150255736Sdavidch    th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5151255736Sdavidch
5152255736Sdavidch    pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5153255736Sdavidch    pbd->tcp_send_seq = ntohl(th->th_seq);
5154255736Sdavidch    pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5155255736Sdavidch
5156255736Sdavidch#if 1
5157255736Sdavidch        /* XXX IPv4 */
5158255736Sdavidch        pbd->ip_id = ntohs(ip->ip_id);
5159255736Sdavidch        pbd->tcp_pseudo_csum =
5160255736Sdavidch            ntohs(in_pseudo(ip->ip_src.s_addr,
5161255736Sdavidch                            ip->ip_dst.s_addr,
5162255736Sdavidch                            htons(IPPROTO_TCP)));
5163255736Sdavidch#else
5164255736Sdavidch        /* XXX IPv6 */
5165255736Sdavidch        pbd->tcp_pseudo_csum =
5166255736Sdavidch            ntohs(in_pseudo(&ip6->ip6_src,
5167255736Sdavidch                            &ip6->ip6_dst,
5168255736Sdavidch                            htons(IPPROTO_TCP)));
5169255736Sdavidch#endif
5170255736Sdavidch
5171255736Sdavidch    pbd->global_data |=
5172255736Sdavidch        htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5173255736Sdavidch}
5174255736Sdavidch
5175255736Sdavidch/*
5176255736Sdavidch * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5177255736Sdavidch * visible to the controller.
5178255736Sdavidch *
5179255736Sdavidch * If an mbuf is submitted to this routine and cannot be given to the
5180255736Sdavidch * controller (e.g. it has too many fragments) then the function may free
5181255736Sdavidch * the mbuf and return to the caller.
5182255736Sdavidch *
5183255736Sdavidch * Returns:
5184255736Sdavidch *   0 = Success, !0 = Failure
5185255736Sdavidch *   Note the side effect that an mbuf may be freed if it causes a problem.
5186255736Sdavidch */
5187255736Sdavidchstatic int
5188255736Sdavidchbxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5189255736Sdavidch{
5190255736Sdavidch    bus_dma_segment_t segs[32];
5191255736Sdavidch    struct mbuf *m0;
5192255736Sdavidch    struct bxe_sw_tx_bd *tx_buf;
5193255736Sdavidch    struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5194255736Sdavidch    struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5195255736Sdavidch    /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5196255736Sdavidch    struct eth_tx_bd *tx_data_bd;
5197255736Sdavidch    struct eth_tx_bd *tx_total_pkt_size_bd;
5198255736Sdavidch    struct eth_tx_start_bd *tx_start_bd;
5199255736Sdavidch    uint16_t bd_prod, pkt_prod, total_pkt_size;
5200255736Sdavidch    uint8_t mac_type;
5201255736Sdavidch    int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5202255736Sdavidch    struct bxe_softc *sc;
5203255736Sdavidch    uint16_t tx_bd_avail;
5204255736Sdavidch    struct ether_vlan_header *eh;
5205255736Sdavidch    uint32_t pbd_e2_parsing_data = 0;
5206255736Sdavidch    uint8_t hlen = 0;
5207255736Sdavidch    int tmp_bd;
5208255736Sdavidch    int i;
5209255736Sdavidch
5210255736Sdavidch    sc = fp->sc;
5211255736Sdavidch
5212255736Sdavidch    M_ASSERTPKTHDR(*m_head);
5213255736Sdavidch
5214255736Sdavidch    m0 = *m_head;
5215255736Sdavidch    rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5216255736Sdavidch    tx_start_bd = NULL;
5217255736Sdavidch    tx_data_bd = NULL;
5218255736Sdavidch    tx_total_pkt_size_bd = NULL;
5219255736Sdavidch
5220255736Sdavidch    /* get the H/W pointer for packets and BDs */
5221255736Sdavidch    pkt_prod = fp->tx_pkt_prod;
5222255736Sdavidch    bd_prod = fp->tx_bd_prod;
5223255736Sdavidch
5224255736Sdavidch    mac_type = UNICAST_ADDRESS;
5225255736Sdavidch
5226255736Sdavidch    /* map the mbuf into the next open DMAable memory */
5227255736Sdavidch    tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5228255736Sdavidch    error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5229255736Sdavidch                                    tx_buf->m_map, m0,
5230255736Sdavidch                                    segs, &nsegs, BUS_DMA_NOWAIT);
5231255736Sdavidch
5232255736Sdavidch    /* mapping errors */
5233255736Sdavidch    if(__predict_false(error != 0)) {
5234255736Sdavidch        fp->eth_q_stats.tx_dma_mapping_failure++;
5235255736Sdavidch        if (error == ENOMEM) {
5236255736Sdavidch            /* resource issue, try again later */
5237255736Sdavidch            rc = ENOMEM;
5238255736Sdavidch        } else if (error == EFBIG) {
5239255736Sdavidch            /* possibly recoverable with defragmentation */
5240255736Sdavidch            fp->eth_q_stats.mbuf_defrag_attempts++;
5241260718Sglebius            m0 = m_defrag(*m_head, M_NOWAIT);
5242255736Sdavidch            if (m0 == NULL) {
5243255736Sdavidch                fp->eth_q_stats.mbuf_defrag_failures++;
5244255736Sdavidch                rc = ENOBUFS;
5245255736Sdavidch            } else {
5246255736Sdavidch                /* defrag successful, try mapping again */
5247255736Sdavidch                *m_head = m0;
5248255736Sdavidch                error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5249255736Sdavidch                                                tx_buf->m_map, m0,
5250255736Sdavidch                                                segs, &nsegs, BUS_DMA_NOWAIT);
5251255736Sdavidch                if (error) {
5252255736Sdavidch                    fp->eth_q_stats.tx_dma_mapping_failure++;
5253255736Sdavidch                    rc = error;
5254255736Sdavidch                }
5255255736Sdavidch            }
5256255736Sdavidch        } else {
5257255736Sdavidch            /* unknown, unrecoverable mapping error */
5258255736Sdavidch            BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5259255736Sdavidch            bxe_dump_mbuf(sc, m0, FALSE);
5260255736Sdavidch            rc = error;
5261255736Sdavidch        }
5262255736Sdavidch
5263255736Sdavidch        goto bxe_tx_encap_continue;
5264255736Sdavidch    }
5265255736Sdavidch
5266255736Sdavidch    tx_bd_avail = bxe_tx_avail(sc, fp);
5267255736Sdavidch
5268255736Sdavidch    /* make sure there is enough room in the send queue */
5269255736Sdavidch    if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5270255736Sdavidch        /* Recoverable, try again later. */
5271255736Sdavidch        fp->eth_q_stats.tx_hw_queue_full++;
5272255736Sdavidch        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5273255736Sdavidch        rc = ENOMEM;
5274255736Sdavidch        goto bxe_tx_encap_continue;
5275255736Sdavidch    }
5276255736Sdavidch
5277255736Sdavidch    /* capture the current H/W TX chain high watermark */
5278255736Sdavidch    if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5279255736Sdavidch                        (TX_BD_USABLE - tx_bd_avail))) {
5280255736Sdavidch        fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5281255736Sdavidch    }
5282255736Sdavidch
5283255736Sdavidch    /* make sure it fits in the packet window */
5284262999Sedavis    if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5285255736Sdavidch        /*
5286255736Sdavidch         * The mbuf may be to big for the controller to handle. If the frame
5287255736Sdavidch         * is a TSO frame we'll need to do an additional check.
5288255736Sdavidch         */
5289255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5290255736Sdavidch            if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5291255736Sdavidch                goto bxe_tx_encap_continue; /* OK to send */
5292255736Sdavidch            } else {
5293255736Sdavidch                fp->eth_q_stats.tx_window_violation_tso++;
5294255736Sdavidch            }
5295255736Sdavidch        } else {
5296255736Sdavidch            fp->eth_q_stats.tx_window_violation_std++;
5297255736Sdavidch        }
5298255736Sdavidch
5299262999Sedavis        /* lets try to defragment this mbuf and remap it */
5300260415Sedavis        fp->eth_q_stats.mbuf_defrag_attempts++;
5301262999Sedavis        bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5302255736Sdavidch
5303260718Sglebius        m0 = m_defrag(*m_head, M_NOWAIT);
5304260415Sedavis        if (m0 == NULL) {
5305260415Sedavis            fp->eth_q_stats.mbuf_defrag_failures++;
5306260415Sedavis            /* Ugh, just drop the frame... :( */
5307260415Sedavis            rc = ENOBUFS;
5308260415Sedavis        } else {
5309260415Sedavis            /* defrag successful, try mapping again */
5310260415Sedavis            *m_head = m0;
5311260415Sedavis            error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5312260415Sedavis                                            tx_buf->m_map, m0,
5313260415Sedavis                                            segs, &nsegs, BUS_DMA_NOWAIT);
5314260415Sedavis            if (error) {
5315260415Sedavis                fp->eth_q_stats.tx_dma_mapping_failure++;
5316260415Sedavis                /* No sense in trying to defrag/copy chain, drop it. :( */
5317260415Sedavis                rc = error;
5318260415Sedavis            }
5319262999Sedavis            else {
5320262999Sedavis                /* if the chain is still too long then drop it */
5321262999Sedavis                if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5322262999Sedavis                    bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5323262999Sedavis                    rc = ENODEV;
5324262999Sedavis                }
5325260415Sedavis            }
5326260415Sedavis        }
5327255736Sdavidch    }
5328255736Sdavidch
5329255736Sdavidchbxe_tx_encap_continue:
5330255736Sdavidch
5331255736Sdavidch    /* Check for errors */
5332255736Sdavidch    if (rc) {
5333255736Sdavidch        if (rc == ENOMEM) {
5334255736Sdavidch            /* recoverable try again later  */
5335255736Sdavidch        } else {
5336255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
5337255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tx--;
5338255736Sdavidch            m_freem(*m_head);
5339255736Sdavidch            *m_head = NULL;
5340255736Sdavidch        }
5341255736Sdavidch
5342255736Sdavidch        return (rc);
5343255736Sdavidch    }
5344255736Sdavidch
5345255736Sdavidch    /* set flag according to packet type (UNICAST_ADDRESS is default) */
5346255736Sdavidch    if (m0->m_flags & M_BCAST) {
5347255736Sdavidch        mac_type = BROADCAST_ADDRESS;
5348255736Sdavidch    } else if (m0->m_flags & M_MCAST) {
5349255736Sdavidch        mac_type = MULTICAST_ADDRESS;
5350255736Sdavidch    }
5351255736Sdavidch
5352255736Sdavidch    /* store the mbuf into the mbuf ring */
5353255736Sdavidch    tx_buf->m        = m0;
5354255736Sdavidch    tx_buf->first_bd = fp->tx_bd_prod;
5355255736Sdavidch    tx_buf->flags    = 0;
5356255736Sdavidch
5357255736Sdavidch    /* prepare the first transmit (start) BD for the mbuf */
5358255736Sdavidch    tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5359255736Sdavidch
5360255736Sdavidch    BLOGD(sc, DBG_TX,
5361255736Sdavidch          "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5362255736Sdavidch          pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5363255736Sdavidch
5364255736Sdavidch    tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5365255736Sdavidch    tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5366255736Sdavidch    tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5367255736Sdavidch    total_pkt_size += tx_start_bd->nbytes;
5368255736Sdavidch    tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5369255736Sdavidch
5370255736Sdavidch    tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5371255736Sdavidch
5372255736Sdavidch    /* all frames have at least Start BD + Parsing BD */
5373255736Sdavidch    nbds = nsegs + 1;
5374255736Sdavidch    tx_start_bd->nbd = htole16(nbds);
5375255736Sdavidch
5376255736Sdavidch    if (m0->m_flags & M_VLANTAG) {
5377255736Sdavidch        tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5378255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |=
5379255736Sdavidch            (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5380255736Sdavidch    } else {
5381255736Sdavidch        /* vf tx, start bd must hold the ethertype for fw to enforce it */
5382255736Sdavidch        if (IS_VF(sc)) {
5383255736Sdavidch            /* map ethernet header to find type and header length */
5384255736Sdavidch            eh = mtod(m0, struct ether_vlan_header *);
5385255736Sdavidch            tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5386255736Sdavidch        } else {
5387255736Sdavidch            /* used by FW for packet accounting */
5388255736Sdavidch            tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5389255736Sdavidch        }
5390255736Sdavidch    }
5391255736Sdavidch
5392255736Sdavidch    /*
5393255736Sdavidch     * add a parsing BD from the chain. The parsing BD is always added
5394255736Sdavidch     * though it is only used for TSO and chksum
5395255736Sdavidch     */
5396255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5397255736Sdavidch
5398255736Sdavidch    if (m0->m_pkthdr.csum_flags) {
5399255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5400255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5401255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5402255736Sdavidch        }
5403255736Sdavidch
5404255736Sdavidch        if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5405255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5406255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5407255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5408255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5409255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP |
5410255736Sdavidch                                                  ETH_TX_BD_FLAGS_L4_CSUM);
5411255736Sdavidch        } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5412255736Sdavidch                   (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5413255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5414255736Sdavidch        } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5415255736Sdavidch            tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5416255736Sdavidch                                                  ETH_TX_BD_FLAGS_IS_UDP);
5417255736Sdavidch        }
5418255736Sdavidch    }
5419255736Sdavidch
5420255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
5421255736Sdavidch        pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5422255736Sdavidch        memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5423255736Sdavidch
5424255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5425255736Sdavidch            hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5426255736Sdavidch        }
5427255736Sdavidch
5428255736Sdavidch        SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5429255736Sdavidch                 mac_type);
5430255736Sdavidch    } else {
5431255736Sdavidch        uint16_t global_data = 0;
5432255736Sdavidch
5433255736Sdavidch        pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5434255736Sdavidch        memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5435255736Sdavidch
5436255736Sdavidch        if (m0->m_pkthdr.csum_flags) {
5437255736Sdavidch            hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5438255736Sdavidch        }
5439255736Sdavidch
5440255736Sdavidch        SET_FLAG(global_data,
5441255736Sdavidch                 ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5442255736Sdavidch        pbd_e1x->global_data |= htole16(global_data);
5443255736Sdavidch    }
5444255736Sdavidch
5445255736Sdavidch    /* setup the parsing BD with TSO specific info */
5446255736Sdavidch    if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5447255736Sdavidch        fp->eth_q_stats.tx_ofld_frames_lso++;
5448255736Sdavidch        tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5449255736Sdavidch
5450255736Sdavidch        if (__predict_false(tx_start_bd->nbytes > hlen)) {
5451255736Sdavidch            fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5452255736Sdavidch
5453255736Sdavidch            /* split the first BD into header/data making the fw job easy */
5454255736Sdavidch            nbds++;
5455255736Sdavidch            tx_start_bd->nbd = htole16(nbds);
5456260113Sedavis            tx_start_bd->nbytes = htole16(hlen);
5457255736Sdavidch
5458255736Sdavidch            bd_prod = TX_BD_NEXT(bd_prod);
5459255736Sdavidch
5460255736Sdavidch            /* new transmit BD after the tx_parse_bd */
5461255736Sdavidch            tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5462255736Sdavidch            tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5463255736Sdavidch            tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5464255736Sdavidch            tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5465255736Sdavidch            if (tx_total_pkt_size_bd == NULL) {
5466255736Sdavidch                tx_total_pkt_size_bd = tx_data_bd;
5467255736Sdavidch            }
5468255736Sdavidch
5469255736Sdavidch            BLOGD(sc, DBG_TX,
5470255736Sdavidch                  "TSO split header size is %d (%x:%x) nbds %d\n",
5471255736Sdavidch                  le16toh(tx_start_bd->nbytes),
5472255736Sdavidch                  le32toh(tx_start_bd->addr_hi),
5473255736Sdavidch                  le32toh(tx_start_bd->addr_lo),
5474255736Sdavidch                  nbds);
5475255736Sdavidch        }
5476255736Sdavidch
5477255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
5478255736Sdavidch            bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5479255736Sdavidch        } else {
5480255736Sdavidch            bxe_set_pbd_lso(m0, pbd_e1x);
5481255736Sdavidch        }
5482255736Sdavidch    }
5483255736Sdavidch
5484255736Sdavidch    if (pbd_e2_parsing_data) {
5485255736Sdavidch        pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5486255736Sdavidch    }
5487255736Sdavidch
5488255736Sdavidch    /* prepare remaining BDs, start tx bd contains first seg/frag */
5489255736Sdavidch    for (i = 1; i < nsegs ; i++) {
5490255736Sdavidch        bd_prod = TX_BD_NEXT(bd_prod);
5491255736Sdavidch        tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5492255736Sdavidch        tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5493255736Sdavidch        tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5494255736Sdavidch        tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5495255736Sdavidch        if (tx_total_pkt_size_bd == NULL) {
5496255736Sdavidch            tx_total_pkt_size_bd = tx_data_bd;
5497255736Sdavidch        }
5498255736Sdavidch        total_pkt_size += tx_data_bd->nbytes;
5499255736Sdavidch    }
5500255736Sdavidch
5501255736Sdavidch    BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5502255736Sdavidch
5503255736Sdavidch    if (tx_total_pkt_size_bd != NULL) {
5504255736Sdavidch        tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5505255736Sdavidch    }
5506255736Sdavidch
5507255736Sdavidch    if (__predict_false(sc->debug & DBG_TX)) {
5508255736Sdavidch        tmp_bd = tx_buf->first_bd;
5509255736Sdavidch        for (i = 0; i < nbds; i++)
5510255736Sdavidch        {
5511255736Sdavidch            if (i == 0) {
5512255736Sdavidch                BLOGD(sc, DBG_TX,
5513255736Sdavidch                      "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5514255736Sdavidch                      "bd_flags=0x%x hdr_nbds=%d\n",
5515255736Sdavidch                      tx_start_bd,
5516255736Sdavidch                      tmp_bd,
5517255736Sdavidch                      le16toh(tx_start_bd->nbd),
5518255736Sdavidch                      le16toh(tx_start_bd->vlan_or_ethertype),
5519255736Sdavidch                      tx_start_bd->bd_flags.as_bitfield,
5520255736Sdavidch                      (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5521255736Sdavidch            } else if (i == 1) {
5522255736Sdavidch                if (pbd_e1x) {
5523255736Sdavidch                    BLOGD(sc, DBG_TX,
5524255736Sdavidch                          "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5525255736Sdavidch                          "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5526255736Sdavidch                          "tcp_seq=%u total_hlen_w=%u\n",
5527255736Sdavidch                          pbd_e1x,
5528255736Sdavidch                          tmp_bd,
5529255736Sdavidch                          pbd_e1x->global_data,
5530255736Sdavidch                          pbd_e1x->ip_hlen_w,
5531255736Sdavidch                          pbd_e1x->ip_id,
5532255736Sdavidch                          pbd_e1x->lso_mss,
5533255736Sdavidch                          pbd_e1x->tcp_flags,
5534255736Sdavidch                          pbd_e1x->tcp_pseudo_csum,
5535255736Sdavidch                          pbd_e1x->tcp_send_seq,
5536255736Sdavidch                          le16toh(pbd_e1x->total_hlen_w));
5537255736Sdavidch                } else { /* if (pbd_e2) */
5538255736Sdavidch                    BLOGD(sc, DBG_TX,
5539255736Sdavidch                          "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5540255736Sdavidch                          "src=%02x:%02x:%02x parsing_data=0x%x\n",
5541255736Sdavidch                          pbd_e2,
5542255736Sdavidch                          tmp_bd,
5543255736Sdavidch                          pbd_e2->data.mac_addr.dst_hi,
5544255736Sdavidch                          pbd_e2->data.mac_addr.dst_mid,
5545255736Sdavidch                          pbd_e2->data.mac_addr.dst_lo,
5546255736Sdavidch                          pbd_e2->data.mac_addr.src_hi,
5547255736Sdavidch                          pbd_e2->data.mac_addr.src_mid,
5548255736Sdavidch                          pbd_e2->data.mac_addr.src_lo,
5549255736Sdavidch                          pbd_e2->parsing_data);
5550255736Sdavidch                }
5551255736Sdavidch            }
5552255736Sdavidch
5553255736Sdavidch            if (i != 1) { /* skip parse db as it doesn't hold data */
5554255736Sdavidch                tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5555255736Sdavidch                BLOGD(sc, DBG_TX,
5556255736Sdavidch                      "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5557255736Sdavidch                      tx_data_bd,
5558255736Sdavidch                      tmp_bd,
5559255736Sdavidch                      le16toh(tx_data_bd->nbytes),
5560255736Sdavidch                      le32toh(tx_data_bd->addr_hi),
5561255736Sdavidch                      le32toh(tx_data_bd->addr_lo));
5562255736Sdavidch            }
5563255736Sdavidch
5564255736Sdavidch            tmp_bd = TX_BD_NEXT(tmp_bd);
5565255736Sdavidch        }
5566255736Sdavidch    }
5567255736Sdavidch
5568255736Sdavidch    BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5569255736Sdavidch
5570255736Sdavidch    /* update TX BD producer index value for next TX */
5571255736Sdavidch    bd_prod = TX_BD_NEXT(bd_prod);
5572255736Sdavidch
5573255736Sdavidch    /*
5574255736Sdavidch     * If the chain of tx_bd's describing this frame is adjacent to or spans
5575255736Sdavidch     * an eth_tx_next_bd element then we need to increment the nbds value.
5576255736Sdavidch     */
5577255736Sdavidch    if (TX_BD_IDX(bd_prod) < nbds) {
5578255736Sdavidch        nbds++;
5579255736Sdavidch    }
5580255736Sdavidch
5581255736Sdavidch    /* don't allow reordering of writes for nbd and packets */
5582255736Sdavidch    mb();
5583255736Sdavidch
5584255736Sdavidch    fp->tx_db.data.prod += nbds;
5585255736Sdavidch
5586255736Sdavidch    /* producer points to the next free tx_bd at this point */
5587255736Sdavidch    fp->tx_pkt_prod++;
5588255736Sdavidch    fp->tx_bd_prod = bd_prod;
5589255736Sdavidch
5590255736Sdavidch    DOORBELL(sc, fp->index, fp->tx_db.raw);
5591255736Sdavidch
5592255736Sdavidch    fp->eth_q_stats.tx_pkts++;
5593255736Sdavidch
5594255736Sdavidch    /* Prevent speculative reads from getting ahead of the status block. */
5595255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5596255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5597255736Sdavidch
5598255736Sdavidch    /* Prevent speculative reads from getting ahead of the doorbell. */
5599255736Sdavidch    bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5600255736Sdavidch                      0, 0, BUS_SPACE_BARRIER_READ);
5601255736Sdavidch
5602255736Sdavidch    return (0);
5603255736Sdavidch}
5604255736Sdavidch
5605255736Sdavidchstatic void
5606266979Smarcelbxe_tx_start_locked(struct bxe_softc *sc,
5607266979Smarcel                    if_t ifp,
5608255736Sdavidch                    struct bxe_fastpath *fp)
5609255736Sdavidch{
5610255736Sdavidch    struct mbuf *m = NULL;
5611255736Sdavidch    int tx_count = 0;
5612255736Sdavidch    uint16_t tx_bd_avail;
5613255736Sdavidch
5614255736Sdavidch    BXE_FP_TX_LOCK_ASSERT(fp);
5615255736Sdavidch
5616255736Sdavidch    /* keep adding entries while there are frames to send */
5617266979Smarcel    while (!if_sendq_empty(ifp)) {
5618255736Sdavidch
5619255736Sdavidch        /*
5620255736Sdavidch         * check for any frames to send
5621255736Sdavidch         * dequeue can still be NULL even if queue is not empty
5622255736Sdavidch         */
5623266979Smarcel        m = if_dequeue(ifp);
5624255736Sdavidch        if (__predict_false(m == NULL)) {
5625255736Sdavidch            break;
5626255736Sdavidch        }
5627255736Sdavidch
5628255736Sdavidch        /* the mbuf now belongs to us */
5629255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5630255736Sdavidch
5631255736Sdavidch        /*
5632255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5633255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5634255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5635255736Sdavidch         */
5636255736Sdavidch        if (__predict_false(bxe_tx_encap(fp, &m))) {
5637255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5638255736Sdavidch            if (m != NULL) {
5639255736Sdavidch                /* mark the TX queue as full and return the frame */
5640266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5641266979Smarcel		if_sendq_prepend(ifp, m);
5642255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5643255736Sdavidch                fp->eth_q_stats.tx_queue_xoff++;
5644255736Sdavidch            }
5645255736Sdavidch
5646255736Sdavidch            /* stop looking for more work */
5647255736Sdavidch            break;
5648255736Sdavidch        }
5649255736Sdavidch
5650255736Sdavidch        /* the frame was enqueued successfully */
5651255736Sdavidch        tx_count++;
5652255736Sdavidch
5653255736Sdavidch        /* send a copy of the frame to any BPF listeners. */
5654266979Smarcel        if_etherbpfmtap(ifp, m);
5655255736Sdavidch
5656255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
5657255736Sdavidch
5658255736Sdavidch        /* handle any completions if we're running low */
5659255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5660258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5661255736Sdavidch            bxe_txeof(sc, fp);
5662266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5663258187Sedavis                break;
5664258187Sedavis            }
5665255736Sdavidch        }
5666255736Sdavidch    }
5667255736Sdavidch
5668255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5669255736Sdavidch    if (tx_count > 0) {
5670255736Sdavidch        /* reset the TX watchdog timeout timer */
5671255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5672255736Sdavidch    }
5673255736Sdavidch}
5674255736Sdavidch
5675255736Sdavidch/* Legacy (non-RSS) dispatch routine */
5676255736Sdavidchstatic void
5677266979Smarcelbxe_tx_start(if_t ifp)
5678255736Sdavidch{
5679255736Sdavidch    struct bxe_softc *sc;
5680255736Sdavidch    struct bxe_fastpath *fp;
5681255736Sdavidch
5682266979Smarcel    sc = if_getsoftc(ifp);
5683255736Sdavidch
5684266979Smarcel    if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5685255736Sdavidch        BLOGW(sc, "Interface not running, ignoring transmit request\n");
5686255736Sdavidch        return;
5687255736Sdavidch    }
5688255736Sdavidch
5689266979Smarcel    if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5690255736Sdavidch        BLOGW(sc, "Interface TX queue is full, ignoring transmit request\n");
5691255736Sdavidch        return;
5692255736Sdavidch    }
5693255736Sdavidch
5694255736Sdavidch    if (!sc->link_vars.link_up) {
5695255736Sdavidch        BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5696255736Sdavidch        return;
5697255736Sdavidch    }
5698255736Sdavidch
5699255736Sdavidch    fp = &sc->fp[0];
5700255736Sdavidch
5701255736Sdavidch    BXE_FP_TX_LOCK(fp);
5702255736Sdavidch    bxe_tx_start_locked(sc, ifp, fp);
5703255736Sdavidch    BXE_FP_TX_UNLOCK(fp);
5704255736Sdavidch}
5705255736Sdavidch
5706255736Sdavidch#if __FreeBSD_version >= 800000
5707255736Sdavidch
5708255736Sdavidchstatic int
5709255736Sdavidchbxe_tx_mq_start_locked(struct bxe_softc    *sc,
5710266979Smarcel                       if_t                ifp,
5711255736Sdavidch                       struct bxe_fastpath *fp,
5712255736Sdavidch                       struct mbuf         *m)
5713255736Sdavidch{
5714255736Sdavidch    struct buf_ring *tx_br = fp->tx_br;
5715255736Sdavidch    struct mbuf *next;
5716255736Sdavidch    int depth, rc, tx_count;
5717255736Sdavidch    uint16_t tx_bd_avail;
5718255736Sdavidch
5719255736Sdavidch    rc = tx_count = 0;
5720255736Sdavidch
5721285973Sdavidcs    BXE_FP_TX_LOCK_ASSERT(fp);
5722285973Sdavidcs
5723255736Sdavidch    if (!tx_br) {
5724255736Sdavidch        BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5725255736Sdavidch        return (EINVAL);
5726255736Sdavidch    }
5727255736Sdavidch
5728285973Sdavidcs    if (!sc->link_vars.link_up ||
5729285973Sdavidcs        (ifp->if_drv_flags &
5730285973Sdavidcs        (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING) {
5731285973Sdavidcs        rc = drbr_enqueue_drv(ifp, tx_br, m);
5732285973Sdavidcs        goto bxe_tx_mq_start_locked_exit;
5733285973Sdavidcs    }
5734285973Sdavidcs
5735255736Sdavidch    /* fetch the depth of the driver queue */
5736266979Smarcel    depth = drbr_inuse_drv(ifp, tx_br);
5737255736Sdavidch    if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5738255736Sdavidch        fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5739255736Sdavidch    }
5740255736Sdavidch
5741255736Sdavidch    if (m == NULL) {
5742255736Sdavidch        /* no new work, check for pending frames */
5743266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
5744266979Smarcel    } else if (drbr_needs_enqueue_drv(ifp, tx_br)) {
5745255736Sdavidch        /* have both new and pending work, maintain packet order */
5746266979Smarcel        rc = drbr_enqueue_drv(ifp, tx_br, m);
5747255736Sdavidch        if (rc != 0) {
5748255736Sdavidch            fp->eth_q_stats.tx_soft_errors++;
5749255736Sdavidch            goto bxe_tx_mq_start_locked_exit;
5750255736Sdavidch        }
5751266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
5752255736Sdavidch    } else {
5753255736Sdavidch        /* new work only and nothing pending */
5754255736Sdavidch        next = m;
5755255736Sdavidch    }
5756255736Sdavidch
5757255736Sdavidch    /* keep adding entries while there are frames to send */
5758255736Sdavidch    while (next != NULL) {
5759255736Sdavidch
5760255736Sdavidch        /* the mbuf now belongs to us */
5761255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tx++;
5762255736Sdavidch
5763255736Sdavidch        /*
5764255736Sdavidch         * Put the frame into the transmit ring. If we don't have room,
5765255736Sdavidch         * place the mbuf back at the head of the TX queue, set the
5766255736Sdavidch         * OACTIVE flag, and wait for the NIC to drain the chain.
5767255736Sdavidch         */
5768255736Sdavidch        rc = bxe_tx_encap(fp, &next);
5769255736Sdavidch        if (__predict_false(rc != 0)) {
5770255736Sdavidch            fp->eth_q_stats.tx_encap_failures++;
5771255736Sdavidch            if (next != NULL) {
5772255736Sdavidch                /* mark the TX queue as full and save the frame */
5773266979Smarcel                if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5774255736Sdavidch                /* XXX this may reorder the frame */
5775266979Smarcel                rc = drbr_enqueue_drv(ifp, tx_br, next);
5776255736Sdavidch                fp->eth_q_stats.mbuf_alloc_tx--;
5777255736Sdavidch                fp->eth_q_stats.tx_frames_deferred++;
5778255736Sdavidch            }
5779255736Sdavidch
5780255736Sdavidch            /* stop looking for more work */
5781255736Sdavidch            break;
5782255736Sdavidch        }
5783255736Sdavidch
5784255736Sdavidch        /* the transmit frame was enqueued successfully */
5785255736Sdavidch        tx_count++;
5786255736Sdavidch
5787255736Sdavidch        /* send a copy of the frame to any BPF listeners */
5788266979Smarcel	if_etherbpfmtap(ifp, next);
5789255736Sdavidch
5790255736Sdavidch        tx_bd_avail = bxe_tx_avail(sc, fp);
5791255736Sdavidch
5792255736Sdavidch        /* handle any completions if we're running low */
5793255736Sdavidch        if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5794258187Sedavis            /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5795255736Sdavidch            bxe_txeof(sc, fp);
5796266979Smarcel            if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5797258187Sedavis                break;
5798258187Sedavis            }
5799255736Sdavidch        }
5800255736Sdavidch
5801266979Smarcel        next = drbr_dequeue_drv(ifp, tx_br);
5802255736Sdavidch    }
5803255736Sdavidch
5804255736Sdavidch    /* all TX packets were dequeued and/or the tx ring is full */
5805255736Sdavidch    if (tx_count > 0) {
5806255736Sdavidch        /* reset the TX watchdog timeout timer */
5807255736Sdavidch        fp->watchdog_timer = BXE_TX_TIMEOUT;
5808255736Sdavidch    }
5809255736Sdavidch
5810255736Sdavidchbxe_tx_mq_start_locked_exit:
5811255736Sdavidch
5812255736Sdavidch    return (rc);
5813255736Sdavidch}
5814255736Sdavidch
5815255736Sdavidch/* Multiqueue (TSS) dispatch routine. */
5816255736Sdavidchstatic int
5817255736Sdavidchbxe_tx_mq_start(struct ifnet *ifp,
5818255736Sdavidch                struct mbuf  *m)
5819255736Sdavidch{
5820266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5821255736Sdavidch    struct bxe_fastpath *fp;
5822255736Sdavidch    int fp_index, rc;
5823255736Sdavidch
5824255736Sdavidch    fp_index = 0; /* default is the first queue */
5825255736Sdavidch
5826275358Shselasky    /* check if flowid is set */
5827275358Shselasky    if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
5828255736Sdavidch        fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5829255736Sdavidch
5830255736Sdavidch    fp = &sc->fp[fp_index];
5831255736Sdavidch
5832285973Sdavidcs    if (BXE_FP_TX_TRYLOCK(fp)) {
5833285973Sdavidcs        rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5834285973Sdavidcs        BXE_FP_TX_UNLOCK(fp);
5835285973Sdavidcs    } else
5836285973Sdavidcs        rc = drbr_enqueue_drv(ifp, fp->tx_br, m);
5837255736Sdavidch
5838255736Sdavidch    return (rc);
5839255736Sdavidch}
5840255736Sdavidch
5841255736Sdavidchstatic void
5842255736Sdavidchbxe_mq_flush(struct ifnet *ifp)
5843255736Sdavidch{
5844266979Smarcel    struct bxe_softc *sc = if_getsoftc(ifp);
5845255736Sdavidch    struct bxe_fastpath *fp;
5846255736Sdavidch    struct mbuf *m;
5847255736Sdavidch    int i;
5848255736Sdavidch
5849255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5850255736Sdavidch        fp = &sc->fp[i];
5851255736Sdavidch
5852255736Sdavidch        if (fp->state != BXE_FP_STATE_OPEN) {
5853255736Sdavidch            BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5854255736Sdavidch                  fp->index, fp->state);
5855255736Sdavidch            continue;
5856255736Sdavidch        }
5857255736Sdavidch
5858255736Sdavidch        if (fp->tx_br != NULL) {
5859255736Sdavidch            BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5860255736Sdavidch            BXE_FP_TX_LOCK(fp);
5861255736Sdavidch            while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5862255736Sdavidch                m_freem(m);
5863255736Sdavidch            }
5864255736Sdavidch            BXE_FP_TX_UNLOCK(fp);
5865255736Sdavidch        }
5866255736Sdavidch    }
5867255736Sdavidch
5868270876Sglebius    if_qflush(ifp);
5869255736Sdavidch}
5870255736Sdavidch
5871255736Sdavidch#endif /* FreeBSD_version >= 800000 */
5872255736Sdavidch
5873255736Sdavidchstatic uint16_t
5874255736Sdavidchbxe_cid_ilt_lines(struct bxe_softc *sc)
5875255736Sdavidch{
5876255736Sdavidch    if (IS_SRIOV(sc)) {
5877255736Sdavidch        return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5878255736Sdavidch    }
5879255736Sdavidch    return (L2_ILT_LINES(sc));
5880255736Sdavidch}
5881255736Sdavidch
5882255736Sdavidchstatic void
5883255736Sdavidchbxe_ilt_set_info(struct bxe_softc *sc)
5884255736Sdavidch{
5885255736Sdavidch    struct ilt_client_info *ilt_client;
5886255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
5887255736Sdavidch    uint16_t line = 0;
5888255736Sdavidch
5889255736Sdavidch    ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5890255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5891255736Sdavidch
5892255736Sdavidch    /* CDU */
5893255736Sdavidch    ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5894255736Sdavidch    ilt_client->client_num = ILT_CLIENT_CDU;
5895255736Sdavidch    ilt_client->page_size = CDU_ILT_PAGE_SZ;
5896255736Sdavidch    ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5897255736Sdavidch    ilt_client->start = line;
5898255736Sdavidch    line += bxe_cid_ilt_lines(sc);
5899255736Sdavidch
5900255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5901255736Sdavidch        line += CNIC_ILT_LINES;
5902255736Sdavidch    }
5903255736Sdavidch
5904255736Sdavidch    ilt_client->end = (line - 1);
5905255736Sdavidch
5906255736Sdavidch    BLOGD(sc, DBG_LOAD,
5907255736Sdavidch          "ilt client[CDU]: start %d, end %d, "
5908255736Sdavidch          "psz 0x%x, flags 0x%x, hw psz %d\n",
5909255736Sdavidch          ilt_client->start, ilt_client->end,
5910255736Sdavidch          ilt_client->page_size,
5911255736Sdavidch          ilt_client->flags,
5912255736Sdavidch          ilog2(ilt_client->page_size >> 12));
5913255736Sdavidch
5914255736Sdavidch    /* QM */
5915255736Sdavidch    if (QM_INIT(sc->qm_cid_count)) {
5916255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_QM];
5917255736Sdavidch        ilt_client->client_num = ILT_CLIENT_QM;
5918255736Sdavidch        ilt_client->page_size = QM_ILT_PAGE_SZ;
5919255736Sdavidch        ilt_client->flags = 0;
5920255736Sdavidch        ilt_client->start = line;
5921255736Sdavidch
5922255736Sdavidch        /* 4 bytes for each cid */
5923255736Sdavidch        line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5924255736Sdavidch                             QM_ILT_PAGE_SZ);
5925255736Sdavidch
5926255736Sdavidch        ilt_client->end = (line - 1);
5927255736Sdavidch
5928255736Sdavidch        BLOGD(sc, DBG_LOAD,
5929255736Sdavidch              "ilt client[QM]: start %d, end %d, "
5930255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5931255736Sdavidch              ilt_client->start, ilt_client->end,
5932255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5933255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5934255736Sdavidch    }
5935255736Sdavidch
5936255736Sdavidch    if (CNIC_SUPPORT(sc)) {
5937255736Sdavidch        /* SRC */
5938255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5939255736Sdavidch        ilt_client->client_num = ILT_CLIENT_SRC;
5940255736Sdavidch        ilt_client->page_size = SRC_ILT_PAGE_SZ;
5941255736Sdavidch        ilt_client->flags = 0;
5942255736Sdavidch        ilt_client->start = line;
5943255736Sdavidch        line += SRC_ILT_LINES;
5944255736Sdavidch        ilt_client->end = (line - 1);
5945255736Sdavidch
5946255736Sdavidch        BLOGD(sc, DBG_LOAD,
5947255736Sdavidch              "ilt client[SRC]: start %d, end %d, "
5948255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5949255736Sdavidch              ilt_client->start, ilt_client->end,
5950255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5951255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5952255736Sdavidch
5953255736Sdavidch        /* TM */
5954255736Sdavidch        ilt_client = &ilt->clients[ILT_CLIENT_TM];
5955255736Sdavidch        ilt_client->client_num = ILT_CLIENT_TM;
5956255736Sdavidch        ilt_client->page_size = TM_ILT_PAGE_SZ;
5957255736Sdavidch        ilt_client->flags = 0;
5958255736Sdavidch        ilt_client->start = line;
5959255736Sdavidch        line += TM_ILT_LINES;
5960255736Sdavidch        ilt_client->end = (line - 1);
5961255736Sdavidch
5962255736Sdavidch        BLOGD(sc, DBG_LOAD,
5963255736Sdavidch              "ilt client[TM]: start %d, end %d, "
5964255736Sdavidch              "psz 0x%x, flags 0x%x, hw psz %d\n",
5965255736Sdavidch              ilt_client->start, ilt_client->end,
5966255736Sdavidch              ilt_client->page_size, ilt_client->flags,
5967255736Sdavidch              ilog2(ilt_client->page_size >> 12));
5968255736Sdavidch    }
5969255736Sdavidch
5970255736Sdavidch    KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
5971255736Sdavidch}
5972255736Sdavidch
5973255736Sdavidchstatic void
5974255736Sdavidchbxe_set_fp_rx_buf_size(struct bxe_softc *sc)
5975255736Sdavidch{
5976255736Sdavidch    int i;
5977283269Sdavidcs    uint32_t rx_buf_size;
5978255736Sdavidch
5979283269Sdavidcs    rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
5980255736Sdavidch
5981255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
5982283269Sdavidcs        if(rx_buf_size <= MCLBYTES){
5983283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5984255736Sdavidch            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5985283269Sdavidcs        }else if (rx_buf_size <= MJUMPAGESIZE){
5986283269Sdavidcs            sc->fp[i].rx_buf_size = rx_buf_size;
5987283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5988283269Sdavidcs        }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
5989283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5990283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5991283269Sdavidcs        }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
5992283269Sdavidcs            sc->fp[i].rx_buf_size = MJUMPAGESIZE;
5993283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5994283269Sdavidcs        }else {
5995283269Sdavidcs            sc->fp[i].rx_buf_size = MCLBYTES;
5996283269Sdavidcs            sc->fp[i].mbuf_alloc_size = MCLBYTES;
5997255736Sdavidch        }
5998255736Sdavidch    }
5999255736Sdavidch}
6000255736Sdavidch
6001255736Sdavidchstatic int
6002255736Sdavidchbxe_alloc_ilt_mem(struct bxe_softc *sc)
6003255736Sdavidch{
6004255736Sdavidch    int rc = 0;
6005255736Sdavidch
6006255736Sdavidch    if ((sc->ilt =
6007255736Sdavidch         (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
6008255736Sdavidch                                    M_BXE_ILT,
6009255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
6010255736Sdavidch        rc = 1;
6011255736Sdavidch    }
6012255736Sdavidch
6013255736Sdavidch    return (rc);
6014255736Sdavidch}
6015255736Sdavidch
6016255736Sdavidchstatic int
6017255736Sdavidchbxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
6018255736Sdavidch{
6019255736Sdavidch    int rc = 0;
6020255736Sdavidch
6021255736Sdavidch    if ((sc->ilt->lines =
6022255736Sdavidch         (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
6023255736Sdavidch                                    M_BXE_ILT,
6024255736Sdavidch                                    (M_NOWAIT | M_ZERO))) == NULL) {
6025255736Sdavidch        rc = 1;
6026255736Sdavidch    }
6027255736Sdavidch
6028255736Sdavidch    return (rc);
6029255736Sdavidch}
6030255736Sdavidch
6031255736Sdavidchstatic void
6032255736Sdavidchbxe_free_ilt_mem(struct bxe_softc *sc)
6033255736Sdavidch{
6034255736Sdavidch    if (sc->ilt != NULL) {
6035255736Sdavidch        free(sc->ilt, M_BXE_ILT);
6036255736Sdavidch        sc->ilt = NULL;
6037255736Sdavidch    }
6038255736Sdavidch}
6039255736Sdavidch
6040255736Sdavidchstatic void
6041255736Sdavidchbxe_free_ilt_lines_mem(struct bxe_softc *sc)
6042255736Sdavidch{
6043255736Sdavidch    if (sc->ilt->lines != NULL) {
6044255736Sdavidch        free(sc->ilt->lines, M_BXE_ILT);
6045255736Sdavidch        sc->ilt->lines = NULL;
6046255736Sdavidch    }
6047255736Sdavidch}
6048255736Sdavidch
6049255736Sdavidchstatic void
6050255736Sdavidchbxe_free_mem(struct bxe_softc *sc)
6051255736Sdavidch{
6052255736Sdavidch    int i;
6053255736Sdavidch
6054255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
6055255736Sdavidch        bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6056255736Sdavidch        sc->context[i].vcxt = NULL;
6057255736Sdavidch        sc->context[i].size = 0;
6058255736Sdavidch    }
6059255736Sdavidch
6060255736Sdavidch    ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6061255736Sdavidch
6062255736Sdavidch    bxe_free_ilt_lines_mem(sc);
6063255736Sdavidch
6064255736Sdavidch}
6065255736Sdavidch
6066255736Sdavidchstatic int
6067255736Sdavidchbxe_alloc_mem(struct bxe_softc *sc)
6068255736Sdavidch{
6069255736Sdavidch    int context_size;
6070255736Sdavidch    int allocated;
6071255736Sdavidch    int i;
6072255736Sdavidch
6073255736Sdavidch    /*
6074255736Sdavidch     * Allocate memory for CDU context:
6075255736Sdavidch     * This memory is allocated separately and not in the generic ILT
6076255736Sdavidch     * functions because CDU differs in few aspects:
6077255736Sdavidch     * 1. There can be multiple entities allocating memory for context -
6078255736Sdavidch     * regular L2, CNIC, and SRIOV drivers. Each separately controls
6079255736Sdavidch     * its own ILT lines.
6080255736Sdavidch     * 2. Since CDU page-size is not a single 4KB page (which is the case
6081255736Sdavidch     * for the other ILT clients), to be efficient we want to support
6082255736Sdavidch     * allocation of sub-page-size in the last entry.
6083255736Sdavidch     * 3. Context pointers are used by the driver to pass to FW / update
6084255736Sdavidch     * the context (for the other ILT clients the pointers are used just to
6085255736Sdavidch     * free the memory during unload).
6086255736Sdavidch     */
6087255736Sdavidch    context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6088255736Sdavidch    for (i = 0, allocated = 0; allocated < context_size; i++) {
6089255736Sdavidch        sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6090255736Sdavidch                                  (context_size - allocated));
6091255736Sdavidch
6092255736Sdavidch        if (bxe_dma_alloc(sc, sc->context[i].size,
6093255736Sdavidch                          &sc->context[i].vcxt_dma,
6094255736Sdavidch                          "cdu context") != 0) {
6095255736Sdavidch            bxe_free_mem(sc);
6096255736Sdavidch            return (-1);
6097255736Sdavidch        }
6098255736Sdavidch
6099255736Sdavidch        sc->context[i].vcxt =
6100255736Sdavidch            (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6101255736Sdavidch
6102255736Sdavidch        allocated += sc->context[i].size;
6103255736Sdavidch    }
6104255736Sdavidch
6105255736Sdavidch    bxe_alloc_ilt_lines_mem(sc);
6106255736Sdavidch
6107255736Sdavidch    BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6108255736Sdavidch          sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6109255736Sdavidch    {
6110255736Sdavidch        for (i = 0; i < 4; i++) {
6111255736Sdavidch            BLOGD(sc, DBG_LOAD,
6112255736Sdavidch                  "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6113255736Sdavidch                  i,
6114255736Sdavidch                  sc->ilt->clients[i].page_size,
6115255736Sdavidch                  sc->ilt->clients[i].start,
6116255736Sdavidch                  sc->ilt->clients[i].end,
6117255736Sdavidch                  sc->ilt->clients[i].client_num,
6118255736Sdavidch                  sc->ilt->clients[i].flags);
6119255736Sdavidch        }
6120255736Sdavidch    }
6121255736Sdavidch    if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6122255736Sdavidch        BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6123255736Sdavidch        bxe_free_mem(sc);
6124255736Sdavidch        return (-1);
6125255736Sdavidch    }
6126255736Sdavidch
6127255736Sdavidch    return (0);
6128255736Sdavidch}
6129255736Sdavidch
6130255736Sdavidchstatic void
6131255736Sdavidchbxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6132255736Sdavidch{
6133255736Sdavidch    struct bxe_softc *sc;
6134255736Sdavidch    int i;
6135255736Sdavidch
6136255736Sdavidch    sc = fp->sc;
6137255736Sdavidch
6138255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6139255736Sdavidch        return;
6140255736Sdavidch    }
6141255736Sdavidch
6142255736Sdavidch    /* free all mbufs and unload all maps */
6143255736Sdavidch    for (i = 0; i < RX_BD_TOTAL; i++) {
6144255736Sdavidch        if (fp->rx_mbuf_chain[i].m_map != NULL) {
6145255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6146255736Sdavidch                            fp->rx_mbuf_chain[i].m_map,
6147255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6148255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6149255736Sdavidch                              fp->rx_mbuf_chain[i].m_map);
6150255736Sdavidch        }
6151255736Sdavidch
6152255736Sdavidch        if (fp->rx_mbuf_chain[i].m != NULL) {
6153255736Sdavidch            m_freem(fp->rx_mbuf_chain[i].m);
6154255736Sdavidch            fp->rx_mbuf_chain[i].m = NULL;
6155255736Sdavidch            fp->eth_q_stats.mbuf_alloc_rx--;
6156255736Sdavidch        }
6157255736Sdavidch    }
6158255736Sdavidch}
6159255736Sdavidch
6160255736Sdavidchstatic void
6161255736Sdavidchbxe_free_tpa_pool(struct bxe_fastpath *fp)
6162255736Sdavidch{
6163255736Sdavidch    struct bxe_softc *sc;
6164255736Sdavidch    int i, max_agg_queues;
6165255736Sdavidch
6166255736Sdavidch    sc = fp->sc;
6167255736Sdavidch
6168255736Sdavidch    if (fp->rx_mbuf_tag == NULL) {
6169255736Sdavidch        return;
6170255736Sdavidch    }
6171255736Sdavidch
6172255736Sdavidch    max_agg_queues = MAX_AGG_QS(sc);
6173255736Sdavidch
6174255736Sdavidch    /* release all mbufs and unload all DMA maps in the TPA pool */
6175255736Sdavidch    for (i = 0; i < max_agg_queues; i++) {
6176255736Sdavidch        if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6177255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag,
6178255736Sdavidch                            fp->rx_tpa_info[i].bd.m_map,
6179255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6180255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag,
6181255736Sdavidch                              fp->rx_tpa_info[i].bd.m_map);
6182255736Sdavidch        }
6183255736Sdavidch
6184255736Sdavidch        if (fp->rx_tpa_info[i].bd.m != NULL) {
6185255736Sdavidch            m_freem(fp->rx_tpa_info[i].bd.m);
6186255736Sdavidch            fp->rx_tpa_info[i].bd.m = NULL;
6187255736Sdavidch            fp->eth_q_stats.mbuf_alloc_tpa--;
6188255736Sdavidch        }
6189255736Sdavidch    }
6190255736Sdavidch}
6191255736Sdavidch
6192255736Sdavidchstatic void
6193255736Sdavidchbxe_free_sge_chain(struct bxe_fastpath *fp)
6194255736Sdavidch{
6195255736Sdavidch    struct bxe_softc *sc;
6196255736Sdavidch    int i;
6197255736Sdavidch
6198255736Sdavidch    sc = fp->sc;
6199255736Sdavidch
6200255736Sdavidch    if (fp->rx_sge_mbuf_tag == NULL) {
6201255736Sdavidch        return;
6202255736Sdavidch    }
6203255736Sdavidch
6204255736Sdavidch    /* rree all mbufs and unload all maps */
6205255736Sdavidch    for (i = 0; i < RX_SGE_TOTAL; i++) {
6206255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6207255736Sdavidch            bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6208255736Sdavidch                            fp->rx_sge_mbuf_chain[i].m_map,
6209255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6210255736Sdavidch            bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6211255736Sdavidch                              fp->rx_sge_mbuf_chain[i].m_map);
6212255736Sdavidch        }
6213255736Sdavidch
6214255736Sdavidch        if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6215255736Sdavidch            m_freem(fp->rx_sge_mbuf_chain[i].m);
6216255736Sdavidch            fp->rx_sge_mbuf_chain[i].m = NULL;
6217255736Sdavidch            fp->eth_q_stats.mbuf_alloc_sge--;
6218255736Sdavidch        }
6219255736Sdavidch    }
6220255736Sdavidch}
6221255736Sdavidch
6222255736Sdavidchstatic void
6223255736Sdavidchbxe_free_fp_buffers(struct bxe_softc *sc)
6224255736Sdavidch{
6225255736Sdavidch    struct bxe_fastpath *fp;
6226255736Sdavidch    int i;
6227255736Sdavidch
6228255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6229255736Sdavidch        fp = &sc->fp[i];
6230255736Sdavidch
6231255736Sdavidch#if __FreeBSD_version >= 800000
6232255736Sdavidch        if (fp->tx_br != NULL) {
6233255736Sdavidch            /* just in case bxe_mq_flush() wasn't called */
6234284739Sdavidcs            if (mtx_initialized(&fp->tx_mtx)) {
6235284739Sdavidcs                struct mbuf *m;
6236284739Sdavidcs
6237284739Sdavidcs                BXE_FP_TX_LOCK(fp);
6238284739Sdavidcs                while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6239284739Sdavidcs                    m_freem(m);
6240284739Sdavidcs                BXE_FP_TX_UNLOCK(fp);
6241255736Sdavidch            }
6242255736Sdavidch            buf_ring_free(fp->tx_br, M_DEVBUF);
6243255736Sdavidch            fp->tx_br = NULL;
6244255736Sdavidch        }
6245255736Sdavidch#endif
6246255736Sdavidch
6247255736Sdavidch        /* free all RX buffers */
6248255736Sdavidch        bxe_free_rx_bd_chain(fp);
6249255736Sdavidch        bxe_free_tpa_pool(fp);
6250255736Sdavidch        bxe_free_sge_chain(fp);
6251255736Sdavidch
6252255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6253255736Sdavidch            BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6254255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_rx);
6255255736Sdavidch        }
6256255736Sdavidch
6257255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6258255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6259255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_sge);
6260255736Sdavidch        }
6261255736Sdavidch
6262255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6263255736Sdavidch            BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6264255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tpa);
6265255736Sdavidch        }
6266255736Sdavidch
6267255736Sdavidch        if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6268255736Sdavidch            BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6269255736Sdavidch                  fp->eth_q_stats.mbuf_alloc_tx);
6270255736Sdavidch        }
6271255736Sdavidch
6272255736Sdavidch        /* XXX verify all mbufs were reclaimed */
6273255736Sdavidch
6274255736Sdavidch        if (mtx_initialized(&fp->tx_mtx)) {
6275255736Sdavidch            mtx_destroy(&fp->tx_mtx);
6276255736Sdavidch        }
6277255736Sdavidch
6278255736Sdavidch        if (mtx_initialized(&fp->rx_mtx)) {
6279255736Sdavidch            mtx_destroy(&fp->rx_mtx);
6280255736Sdavidch        }
6281255736Sdavidch    }
6282255736Sdavidch}
6283255736Sdavidch
6284255736Sdavidchstatic int
6285255736Sdavidchbxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6286255736Sdavidch                     uint16_t            prev_index,
6287255736Sdavidch                     uint16_t            index)
6288255736Sdavidch{
6289255736Sdavidch    struct bxe_sw_rx_bd *rx_buf;
6290255736Sdavidch    struct eth_rx_bd *rx_bd;
6291255736Sdavidch    bus_dma_segment_t segs[1];
6292255736Sdavidch    bus_dmamap_t map;
6293255736Sdavidch    struct mbuf *m;
6294255736Sdavidch    int nsegs, rc;
6295255736Sdavidch
6296255736Sdavidch    rc = 0;
6297255736Sdavidch
6298255736Sdavidch    /* allocate the new RX BD mbuf */
6299260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6300255736Sdavidch    if (__predict_false(m == NULL)) {
6301255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6302255736Sdavidch        return (ENOBUFS);
6303255736Sdavidch    }
6304255736Sdavidch
6305255736Sdavidch    fp->eth_q_stats.mbuf_alloc_rx++;
6306255736Sdavidch
6307255736Sdavidch    /* initialize the mbuf buffer length */
6308255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6309255736Sdavidch
6310255736Sdavidch    /* map the mbuf into non-paged pool */
6311255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6312255736Sdavidch                                 fp->rx_mbuf_spare_map,
6313255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6314255736Sdavidch    if (__predict_false(rc != 0)) {
6315255736Sdavidch        fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6316255736Sdavidch        m_freem(m);
6317255736Sdavidch        fp->eth_q_stats.mbuf_alloc_rx--;
6318255736Sdavidch        return (rc);
6319255736Sdavidch    }
6320255736Sdavidch
6321255736Sdavidch    /* all mbufs must map to a single segment */
6322255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6323255736Sdavidch
6324255736Sdavidch    /* release any existing RX BD mbuf mappings */
6325255736Sdavidch
6326255736Sdavidch    if (prev_index != index) {
6327255736Sdavidch        rx_buf = &fp->rx_mbuf_chain[prev_index];
6328255736Sdavidch
6329255736Sdavidch        if (rx_buf->m_map != NULL) {
6330255736Sdavidch            bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6331255736Sdavidch                            BUS_DMASYNC_POSTREAD);
6332255736Sdavidch            bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6333255736Sdavidch        }
6334255736Sdavidch
6335255736Sdavidch        /*
6336255736Sdavidch         * We only get here from bxe_rxeof() when the maximum number
6337255736Sdavidch         * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6338255736Sdavidch         * holds the mbuf in the prev_index so it's OK to NULL it out
6339255736Sdavidch         * here without concern of a memory leak.
6340255736Sdavidch         */
6341255736Sdavidch        fp->rx_mbuf_chain[prev_index].m = NULL;
6342255736Sdavidch    }
6343255736Sdavidch
6344255736Sdavidch    rx_buf = &fp->rx_mbuf_chain[index];
6345255736Sdavidch
6346255736Sdavidch    if (rx_buf->m_map != NULL) {
6347255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6348255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6349255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6350255736Sdavidch    }
6351255736Sdavidch
6352255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6353255736Sdavidch    map = (prev_index != index) ?
6354255736Sdavidch              fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6355255736Sdavidch    rx_buf->m_map = fp->rx_mbuf_spare_map;
6356255736Sdavidch    fp->rx_mbuf_spare_map = map;
6357255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6358255736Sdavidch                    BUS_DMASYNC_PREREAD);
6359255736Sdavidch    rx_buf->m = m;
6360255736Sdavidch
6361255736Sdavidch    rx_bd = &fp->rx_chain[index];
6362255736Sdavidch    rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6363255736Sdavidch    rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6364255736Sdavidch
6365255736Sdavidch    return (rc);
6366255736Sdavidch}
6367255736Sdavidch
6368255736Sdavidchstatic int
6369255736Sdavidchbxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6370255736Sdavidch                      int                 queue)
6371255736Sdavidch{
6372255736Sdavidch    struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6373255736Sdavidch    bus_dma_segment_t segs[1];
6374255736Sdavidch    bus_dmamap_t map;
6375255736Sdavidch    struct mbuf *m;
6376255736Sdavidch    int nsegs;
6377255736Sdavidch    int rc = 0;
6378255736Sdavidch
6379255736Sdavidch    /* allocate the new TPA mbuf */
6380260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6381255736Sdavidch    if (__predict_false(m == NULL)) {
6382255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6383255736Sdavidch        return (ENOBUFS);
6384255736Sdavidch    }
6385255736Sdavidch
6386255736Sdavidch    fp->eth_q_stats.mbuf_alloc_tpa++;
6387255736Sdavidch
6388255736Sdavidch    /* initialize the mbuf buffer length */
6389255736Sdavidch    m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6390255736Sdavidch
6391255736Sdavidch    /* map the mbuf into non-paged pool */
6392255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6393255736Sdavidch                                 fp->rx_tpa_info_mbuf_spare_map,
6394255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6395255736Sdavidch    if (__predict_false(rc != 0)) {
6396255736Sdavidch        fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6397255736Sdavidch        m_free(m);
6398255736Sdavidch        fp->eth_q_stats.mbuf_alloc_tpa--;
6399255736Sdavidch        return (rc);
6400255736Sdavidch    }
6401255736Sdavidch
6402255736Sdavidch    /* all mbufs must map to a single segment */
6403255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6404255736Sdavidch
6405255736Sdavidch    /* release any existing TPA mbuf mapping */
6406255736Sdavidch    if (tpa_info->bd.m_map != NULL) {
6407255736Sdavidch        bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6408255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6409255736Sdavidch        bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6410255736Sdavidch    }
6411255736Sdavidch
6412255736Sdavidch    /* save the mbuf and mapping info for the TPA mbuf */
6413255736Sdavidch    map = tpa_info->bd.m_map;
6414255736Sdavidch    tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6415255736Sdavidch    fp->rx_tpa_info_mbuf_spare_map = map;
6416255736Sdavidch    bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6417255736Sdavidch                    BUS_DMASYNC_PREREAD);
6418255736Sdavidch    tpa_info->bd.m = m;
6419255736Sdavidch    tpa_info->seg = segs[0];
6420255736Sdavidch
6421255736Sdavidch    return (rc);
6422255736Sdavidch}
6423255736Sdavidch
6424255736Sdavidch/*
6425255736Sdavidch * Allocate an mbuf and assign it to the receive scatter gather chain. The
6426255736Sdavidch * caller must take care to save a copy of the existing mbuf in the SG mbuf
6427255736Sdavidch * chain.
6428255736Sdavidch */
6429255736Sdavidchstatic int
6430255736Sdavidchbxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6431255736Sdavidch                      uint16_t            index)
6432255736Sdavidch{
6433255736Sdavidch    struct bxe_sw_rx_bd *sge_buf;
6434255736Sdavidch    struct eth_rx_sge *sge;
6435255736Sdavidch    bus_dma_segment_t segs[1];
6436255736Sdavidch    bus_dmamap_t map;
6437255736Sdavidch    struct mbuf *m;
6438255736Sdavidch    int nsegs;
6439255736Sdavidch    int rc = 0;
6440255736Sdavidch
6441255736Sdavidch    /* allocate a new SGE mbuf */
6442260718Sglebius    m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6443255736Sdavidch    if (__predict_false(m == NULL)) {
6444255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6445255736Sdavidch        return (ENOMEM);
6446255736Sdavidch    }
6447255736Sdavidch
6448255736Sdavidch    fp->eth_q_stats.mbuf_alloc_sge++;
6449255736Sdavidch
6450255736Sdavidch    /* initialize the mbuf buffer length */
6451255736Sdavidch    m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6452255736Sdavidch
6453255736Sdavidch    /* map the SGE mbuf into non-paged pool */
6454255736Sdavidch    rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6455255736Sdavidch                                 fp->rx_sge_mbuf_spare_map,
6456255736Sdavidch                                 m, segs, &nsegs, BUS_DMA_NOWAIT);
6457255736Sdavidch    if (__predict_false(rc != 0)) {
6458255736Sdavidch        fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6459255736Sdavidch        m_freem(m);
6460255736Sdavidch        fp->eth_q_stats.mbuf_alloc_sge--;
6461255736Sdavidch        return (rc);
6462255736Sdavidch    }
6463255736Sdavidch
6464255736Sdavidch    /* all mbufs must map to a single segment */
6465255736Sdavidch    KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6466255736Sdavidch
6467255736Sdavidch    sge_buf = &fp->rx_sge_mbuf_chain[index];
6468255736Sdavidch
6469255736Sdavidch    /* release any existing SGE mbuf mapping */
6470255736Sdavidch    if (sge_buf->m_map != NULL) {
6471255736Sdavidch        bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6472255736Sdavidch                        BUS_DMASYNC_POSTREAD);
6473255736Sdavidch        bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6474255736Sdavidch    }
6475255736Sdavidch
6476255736Sdavidch    /* save the mbuf and mapping info for a future packet */
6477255736Sdavidch    map = sge_buf->m_map;
6478255736Sdavidch    sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6479255736Sdavidch    fp->rx_sge_mbuf_spare_map = map;
6480255736Sdavidch    bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6481255736Sdavidch                    BUS_DMASYNC_PREREAD);
6482255736Sdavidch    sge_buf->m = m;
6483255736Sdavidch
6484255736Sdavidch    sge = &fp->rx_sge_chain[index];
6485255736Sdavidch    sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6486255736Sdavidch    sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6487255736Sdavidch
6488255736Sdavidch    return (rc);
6489255736Sdavidch}
6490255736Sdavidch
6491255736Sdavidchstatic __noinline int
6492255736Sdavidchbxe_alloc_fp_buffers(struct bxe_softc *sc)
6493255736Sdavidch{
6494255736Sdavidch    struct bxe_fastpath *fp;
6495255736Sdavidch    int i, j, rc = 0;
6496255736Sdavidch    int ring_prod, cqe_ring_prod;
6497255736Sdavidch    int max_agg_queues;
6498255736Sdavidch
6499255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
6500255736Sdavidch        fp = &sc->fp[i];
6501255736Sdavidch
6502255736Sdavidch#if __FreeBSD_version >= 800000
6503255736Sdavidch        fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
6504260718Sglebius                                   M_NOWAIT, &fp->tx_mtx);
6505255736Sdavidch        if (fp->tx_br == NULL) {
6506255736Sdavidch            BLOGE(sc, "buf_ring alloc fail for fp[%02d]\n", i);
6507255736Sdavidch            goto bxe_alloc_fp_buffers_error;
6508255736Sdavidch        }
6509255736Sdavidch#endif
6510255736Sdavidch
6511255736Sdavidch        ring_prod = cqe_ring_prod = 0;
6512255736Sdavidch        fp->rx_bd_cons = 0;
6513255736Sdavidch        fp->rx_cq_cons = 0;
6514255736Sdavidch
6515255736Sdavidch        /* allocate buffers for the RX BDs in RX BD chain */
6516255736Sdavidch        for (j = 0; j < sc->max_rx_bufs; j++) {
6517255736Sdavidch            rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6518255736Sdavidch            if (rc != 0) {
6519255736Sdavidch                BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6520255736Sdavidch                      i, rc);
6521255736Sdavidch                goto bxe_alloc_fp_buffers_error;
6522255736Sdavidch            }
6523255736Sdavidch
6524255736Sdavidch            ring_prod     = RX_BD_NEXT(ring_prod);
6525255736Sdavidch            cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6526255736Sdavidch        }
6527255736Sdavidch
6528255736Sdavidch        fp->rx_bd_prod = ring_prod;
6529255736Sdavidch        fp->rx_cq_prod = cqe_ring_prod;
6530255736Sdavidch        fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6531255736Sdavidch
6532283274Sdavidcs        max_agg_queues = MAX_AGG_QS(sc);
6533255736Sdavidch
6534283274Sdavidcs        fp->tpa_enable = TRUE;
6535255736Sdavidch
6536283274Sdavidcs        /* fill the TPA pool */
6537283274Sdavidcs        for (j = 0; j < max_agg_queues; j++) {
6538283274Sdavidcs            rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6539283274Sdavidcs            if (rc != 0) {
6540283274Sdavidcs                BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6541283274Sdavidcs                          i, j);
6542283274Sdavidcs                fp->tpa_enable = FALSE;
6543283274Sdavidcs                goto bxe_alloc_fp_buffers_error;
6544283274Sdavidcs            }
6545283274Sdavidcs
6546283274Sdavidcs            fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6547283274Sdavidcs        }
6548283274Sdavidcs
6549283274Sdavidcs        if (fp->tpa_enable) {
6550283274Sdavidcs            /* fill the RX SGE chain */
6551283274Sdavidcs            ring_prod = 0;
6552283274Sdavidcs            for (j = 0; j < RX_SGE_USABLE; j++) {
6553283274Sdavidcs                rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6554255736Sdavidch                if (rc != 0) {
6555283274Sdavidcs                    BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6556283274Sdavidcs                              i, ring_prod);
6557255736Sdavidch                    fp->tpa_enable = FALSE;
6558283274Sdavidcs                    ring_prod = 0;
6559255736Sdavidch                    goto bxe_alloc_fp_buffers_error;
6560255736Sdavidch                }
6561255736Sdavidch
6562283274Sdavidcs                ring_prod = RX_SGE_NEXT(ring_prod);
6563255736Sdavidch            }
6564255736Sdavidch
6565283274Sdavidcs            fp->rx_sge_prod = ring_prod;
6566255736Sdavidch        }
6567255736Sdavidch    }
6568255736Sdavidch
6569255736Sdavidch    return (0);
6570255736Sdavidch
6571255736Sdavidchbxe_alloc_fp_buffers_error:
6572255736Sdavidch
6573255736Sdavidch    /* unwind what was already allocated */
6574255736Sdavidch    bxe_free_rx_bd_chain(fp);
6575255736Sdavidch    bxe_free_tpa_pool(fp);
6576255736Sdavidch    bxe_free_sge_chain(fp);
6577255736Sdavidch
6578255736Sdavidch    return (ENOBUFS);
6579255736Sdavidch}
6580255736Sdavidch
6581255736Sdavidchstatic void
6582255736Sdavidchbxe_free_fw_stats_mem(struct bxe_softc *sc)
6583255736Sdavidch{
6584255736Sdavidch    bxe_dma_free(sc, &sc->fw_stats_dma);
6585255736Sdavidch
6586255736Sdavidch    sc->fw_stats_num = 0;
6587255736Sdavidch
6588255736Sdavidch    sc->fw_stats_req_size = 0;
6589255736Sdavidch    sc->fw_stats_req = NULL;
6590255736Sdavidch    sc->fw_stats_req_mapping = 0;
6591255736Sdavidch
6592255736Sdavidch    sc->fw_stats_data_size = 0;
6593255736Sdavidch    sc->fw_stats_data = NULL;
6594255736Sdavidch    sc->fw_stats_data_mapping = 0;
6595255736Sdavidch}
6596255736Sdavidch
6597255736Sdavidchstatic int
6598255736Sdavidchbxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6599255736Sdavidch{
6600255736Sdavidch    uint8_t num_queue_stats;
6601255736Sdavidch    int num_groups;
6602255736Sdavidch
6603255736Sdavidch    /* number of queues for statistics is number of eth queues */
6604255736Sdavidch    num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6605255736Sdavidch
6606255736Sdavidch    /*
6607255736Sdavidch     * Total number of FW statistics requests =
6608255736Sdavidch     *   1 for port stats + 1 for PF stats + num of queues
6609255736Sdavidch     */
6610255736Sdavidch    sc->fw_stats_num = (2 + num_queue_stats);
6611255736Sdavidch
6612255736Sdavidch    /*
6613255736Sdavidch     * Request is built from stats_query_header and an array of
6614255736Sdavidch     * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6615255736Sdavidch     * rules. The real number or requests is configured in the
6616255736Sdavidch     * stats_query_header.
6617255736Sdavidch     */
6618255736Sdavidch    num_groups =
6619255736Sdavidch        ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6620255736Sdavidch         ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6621255736Sdavidch
6622255736Sdavidch    BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6623255736Sdavidch          sc->fw_stats_num, num_groups);
6624255736Sdavidch
6625255736Sdavidch    sc->fw_stats_req_size =
6626255736Sdavidch        (sizeof(struct stats_query_header) +
6627255736Sdavidch         (num_groups * sizeof(struct stats_query_cmd_group)));
6628255736Sdavidch
6629255736Sdavidch    /*
6630255736Sdavidch     * Data for statistics requests + stats_counter.
6631255736Sdavidch     * stats_counter holds per-STORM counters that are incremented when
6632255736Sdavidch     * STORM has finished with the current request. Memory for FCoE
6633255736Sdavidch     * offloaded statistics are counted anyway, even if they will not be sent.
6634255736Sdavidch     * VF stats are not accounted for here as the data of VF stats is stored
6635255736Sdavidch     * in memory allocated by the VF, not here.
6636255736Sdavidch     */
6637255736Sdavidch    sc->fw_stats_data_size =
6638255736Sdavidch        (sizeof(struct stats_counter) +
6639255736Sdavidch         sizeof(struct per_port_stats) +
6640255736Sdavidch         sizeof(struct per_pf_stats) +
6641255736Sdavidch         /* sizeof(struct fcoe_statistics_params) + */
6642255736Sdavidch         (sizeof(struct per_queue_stats) * num_queue_stats));
6643255736Sdavidch
6644255736Sdavidch    if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6645255736Sdavidch                      &sc->fw_stats_dma, "fw stats") != 0) {
6646255736Sdavidch        bxe_free_fw_stats_mem(sc);
6647255736Sdavidch        return (-1);
6648255736Sdavidch    }
6649255736Sdavidch
6650255736Sdavidch    /* set up the shortcuts */
6651255736Sdavidch
6652255736Sdavidch    sc->fw_stats_req =
6653255736Sdavidch        (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6654255736Sdavidch    sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6655255736Sdavidch
6656255736Sdavidch    sc->fw_stats_data =
6657255736Sdavidch        (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6658255736Sdavidch                                     sc->fw_stats_req_size);
6659255736Sdavidch    sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6660255736Sdavidch                                 sc->fw_stats_req_size);
6661255736Sdavidch
6662256341Sdim    BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6663256341Sdim          (uintmax_t)sc->fw_stats_req_mapping);
6664255736Sdavidch
6665256341Sdim    BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6666256341Sdim          (uintmax_t)sc->fw_stats_data_mapping);
6667255736Sdavidch
6668255736Sdavidch    return (0);
6669255736Sdavidch}
6670255736Sdavidch
6671255736Sdavidch/*
6672255736Sdavidch * Bits map:
6673255736Sdavidch * 0-7  - Engine0 load counter.
6674255736Sdavidch * 8-15 - Engine1 load counter.
6675255736Sdavidch * 16   - Engine0 RESET_IN_PROGRESS bit.
6676255736Sdavidch * 17   - Engine1 RESET_IN_PROGRESS bit.
6677255736Sdavidch * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6678255736Sdavidch *        function on the engine
6679255736Sdavidch * 19   - Engine1 ONE_IS_LOADED.
6680255736Sdavidch * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6681255736Sdavidch *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6682255736Sdavidch *        for just the one belonging to its engine).
6683255736Sdavidch */
6684255736Sdavidch#define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6685255736Sdavidch#define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6686255736Sdavidch#define BXE_PATH0_LOAD_CNT_SHIFT  0
6687255736Sdavidch#define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6688255736Sdavidch#define BXE_PATH1_LOAD_CNT_SHIFT  8
6689255736Sdavidch#define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6690255736Sdavidch#define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6691255736Sdavidch#define BXE_GLOBAL_RESET_BIT      0x00040000
6692255736Sdavidch
6693255736Sdavidch/* set the GLOBAL_RESET bit, should be run under rtnl lock */
6694255736Sdavidchstatic void
6695255736Sdavidchbxe_set_reset_global(struct bxe_softc *sc)
6696255736Sdavidch{
6697255736Sdavidch    uint32_t val;
6698255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6699255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6700255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6701255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6702255736Sdavidch}
6703255736Sdavidch
6704255736Sdavidch/* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6705255736Sdavidchstatic void
6706255736Sdavidchbxe_clear_reset_global(struct bxe_softc *sc)
6707255736Sdavidch{
6708255736Sdavidch    uint32_t val;
6709255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6710255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6711255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6712255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6713255736Sdavidch}
6714255736Sdavidch
6715255736Sdavidch/* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6716255736Sdavidchstatic uint8_t
6717255736Sdavidchbxe_reset_is_global(struct bxe_softc *sc)
6718255736Sdavidch{
6719255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6720255736Sdavidch    BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6721255736Sdavidch    return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6722255736Sdavidch}
6723255736Sdavidch
6724255736Sdavidch/* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6725255736Sdavidchstatic void
6726255736Sdavidchbxe_set_reset_done(struct bxe_softc *sc)
6727255736Sdavidch{
6728255736Sdavidch    uint32_t val;
6729255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6730255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6731255736Sdavidch
6732255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6733255736Sdavidch
6734255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6735255736Sdavidch    /* Clear the bit */
6736255736Sdavidch    val &= ~bit;
6737255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6738255736Sdavidch
6739255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6740255736Sdavidch}
6741255736Sdavidch
6742255736Sdavidch/* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6743255736Sdavidchstatic void
6744255736Sdavidchbxe_set_reset_in_progress(struct bxe_softc *sc)
6745255736Sdavidch{
6746255736Sdavidch    uint32_t val;
6747255736Sdavidch    uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6748255736Sdavidch                                 BXE_PATH0_RST_IN_PROG_BIT;
6749255736Sdavidch
6750255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6751255736Sdavidch
6752255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6753255736Sdavidch    /* Set the bit */
6754255736Sdavidch    val |= bit;
6755255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6756255736Sdavidch
6757255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6758255736Sdavidch}
6759255736Sdavidch
6760255736Sdavidch/* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6761255736Sdavidchstatic uint8_t
6762255736Sdavidchbxe_reset_is_done(struct bxe_softc *sc,
6763255736Sdavidch                  int              engine)
6764255736Sdavidch{
6765255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6766255736Sdavidch    uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6767255736Sdavidch                            BXE_PATH0_RST_IN_PROG_BIT;
6768255736Sdavidch
6769255736Sdavidch    /* return false if bit is set */
6770255736Sdavidch    return (val & bit) ? FALSE : TRUE;
6771255736Sdavidch}
6772255736Sdavidch
6773255736Sdavidch/* get the load status for an engine, should be run under rtnl lock */
6774255736Sdavidchstatic uint8_t
6775255736Sdavidchbxe_get_load_status(struct bxe_softc *sc,
6776255736Sdavidch                    int              engine)
6777255736Sdavidch{
6778255736Sdavidch    uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6779255736Sdavidch                             BXE_PATH0_LOAD_CNT_MASK;
6780255736Sdavidch    uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6781255736Sdavidch                              BXE_PATH0_LOAD_CNT_SHIFT;
6782255736Sdavidch    uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6783255736Sdavidch
6784255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6785255736Sdavidch
6786255736Sdavidch    val = ((val & mask) >> shift);
6787255736Sdavidch
6788255736Sdavidch    BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6789255736Sdavidch
6790255736Sdavidch    return (val != 0);
6791255736Sdavidch}
6792255736Sdavidch
6793255736Sdavidch/* set pf load mark */
6794255736Sdavidch/* XXX needs to be under rtnl lock */
6795255736Sdavidchstatic void
6796255736Sdavidchbxe_set_pf_load(struct bxe_softc *sc)
6797255736Sdavidch{
6798255736Sdavidch    uint32_t val;
6799255736Sdavidch    uint32_t val1;
6800255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6801255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6802255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6803255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6804255736Sdavidch
6805255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6806255736Sdavidch
6807255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6808255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6809255736Sdavidch
6810255736Sdavidch    /* get the current counter value */
6811255736Sdavidch    val1 = ((val & mask) >> shift);
6812255736Sdavidch
6813255736Sdavidch    /* set bit of this PF */
6814255736Sdavidch    val1 |= (1 << SC_ABS_FUNC(sc));
6815255736Sdavidch
6816255736Sdavidch    /* clear the old value */
6817255736Sdavidch    val &= ~mask;
6818255736Sdavidch
6819255736Sdavidch    /* set the new one */
6820255736Sdavidch    val |= ((val1 << shift) & mask);
6821255736Sdavidch
6822255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6823255736Sdavidch
6824255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6825255736Sdavidch}
6826255736Sdavidch
6827255736Sdavidch/* clear pf load mark */
6828255736Sdavidch/* XXX needs to be under rtnl lock */
6829255736Sdavidchstatic uint8_t
6830255736Sdavidchbxe_clear_pf_load(struct bxe_softc *sc)
6831255736Sdavidch{
6832255736Sdavidch    uint32_t val1, val;
6833255736Sdavidch    uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6834255736Sdavidch                                  BXE_PATH0_LOAD_CNT_MASK;
6835255736Sdavidch    uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6836255736Sdavidch                                   BXE_PATH0_LOAD_CNT_SHIFT;
6837255736Sdavidch
6838255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6839255736Sdavidch    val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6840255736Sdavidch    BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6841255736Sdavidch
6842255736Sdavidch    /* get the current counter value */
6843255736Sdavidch    val1 = (val & mask) >> shift;
6844255736Sdavidch
6845255736Sdavidch    /* clear bit of that PF */
6846255736Sdavidch    val1 &= ~(1 << SC_ABS_FUNC(sc));
6847255736Sdavidch
6848255736Sdavidch    /* clear the old value */
6849255736Sdavidch    val &= ~mask;
6850255736Sdavidch
6851255736Sdavidch    /* set the new one */
6852255736Sdavidch    val |= ((val1 << shift) & mask);
6853255736Sdavidch
6854255736Sdavidch    REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6855255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6856255736Sdavidch    return (val1 != 0);
6857255736Sdavidch}
6858255736Sdavidch
6859255736Sdavidch/* send load requrest to mcp and analyze response */
6860255736Sdavidchstatic int
6861255736Sdavidchbxe_nic_load_request(struct bxe_softc *sc,
6862255736Sdavidch                     uint32_t         *load_code)
6863255736Sdavidch{
6864255736Sdavidch    /* init fw_seq */
6865255736Sdavidch    sc->fw_seq =
6866255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6867255736Sdavidch         DRV_MSG_SEQ_NUMBER_MASK);
6868255736Sdavidch
6869255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6870255736Sdavidch
6871255736Sdavidch    /* get the current FW pulse sequence */
6872255736Sdavidch    sc->fw_drv_pulse_wr_seq =
6873255736Sdavidch        (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6874255736Sdavidch         DRV_PULSE_SEQ_MASK);
6875255736Sdavidch
6876255736Sdavidch    BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6877255736Sdavidch          sc->fw_drv_pulse_wr_seq);
6878255736Sdavidch
6879255736Sdavidch    /* load request */
6880255736Sdavidch    (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6881255736Sdavidch                                  DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6882255736Sdavidch
6883255736Sdavidch    /* if the MCP fails to respond we must abort */
6884255736Sdavidch    if (!(*load_code)) {
6885255736Sdavidch        BLOGE(sc, "MCP response failure!\n");
6886255736Sdavidch        return (-1);
6887255736Sdavidch    }
6888255736Sdavidch
6889255736Sdavidch    /* if MCP refused then must abort */
6890255736Sdavidch    if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6891255736Sdavidch        BLOGE(sc, "MCP refused load request\n");
6892255736Sdavidch        return (-1);
6893255736Sdavidch    }
6894255736Sdavidch
6895255736Sdavidch    return (0);
6896255736Sdavidch}
6897255736Sdavidch
6898255736Sdavidch/*
6899255736Sdavidch * Check whether another PF has already loaded FW to chip. In virtualized
6900255736Sdavidch * environments a pf from anoth VM may have already initialized the device
6901255736Sdavidch * including loading FW.
6902255736Sdavidch */
6903255736Sdavidchstatic int
6904255736Sdavidchbxe_nic_load_analyze_req(struct bxe_softc *sc,
6905255736Sdavidch                         uint32_t         load_code)
6906255736Sdavidch{
6907255736Sdavidch    uint32_t my_fw, loaded_fw;
6908255736Sdavidch
6909255736Sdavidch    /* is another pf loaded on this engine? */
6910255736Sdavidch    if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6911255736Sdavidch        (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6912255736Sdavidch        /* build my FW version dword */
6913255736Sdavidch        my_fw = (BCM_5710_FW_MAJOR_VERSION +
6914255736Sdavidch                 (BCM_5710_FW_MINOR_VERSION << 8 ) +
6915255736Sdavidch                 (BCM_5710_FW_REVISION_VERSION << 16) +
6916255736Sdavidch                 (BCM_5710_FW_ENGINEERING_VERSION << 24));
6917255736Sdavidch
6918255736Sdavidch        /* read loaded FW from chip */
6919255736Sdavidch        loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6920255736Sdavidch        BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6921255736Sdavidch              loaded_fw, my_fw);
6922255736Sdavidch
6923255736Sdavidch        /* abort nic load if version mismatch */
6924255736Sdavidch        if (my_fw != loaded_fw) {
6925255736Sdavidch            BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6926255736Sdavidch                  loaded_fw, my_fw);
6927255736Sdavidch            return (-1);
6928255736Sdavidch        }
6929255736Sdavidch    }
6930255736Sdavidch
6931255736Sdavidch    return (0);
6932255736Sdavidch}
6933255736Sdavidch
6934255736Sdavidch/* mark PMF if applicable */
6935255736Sdavidchstatic void
6936255736Sdavidchbxe_nic_load_pmf(struct bxe_softc *sc,
6937255736Sdavidch                 uint32_t         load_code)
6938255736Sdavidch{
6939255736Sdavidch    uint32_t ncsi_oem_data_addr;
6940255736Sdavidch
6941255736Sdavidch    if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6942255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6943255736Sdavidch        (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6944255736Sdavidch        /*
6945255736Sdavidch         * Barrier here for ordering between the writing to sc->port.pmf here
6946255736Sdavidch         * and reading it from the periodic task.
6947255736Sdavidch         */
6948255736Sdavidch        sc->port.pmf = 1;
6949255736Sdavidch        mb();
6950255736Sdavidch    } else {
6951255736Sdavidch        sc->port.pmf = 0;
6952255736Sdavidch    }
6953255736Sdavidch
6954255736Sdavidch    BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6955255736Sdavidch
6956255736Sdavidch    /* XXX needed? */
6957255736Sdavidch    if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6958255736Sdavidch        if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6959255736Sdavidch            ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6960255736Sdavidch            if (ncsi_oem_data_addr) {
6961255736Sdavidch                REG_WR(sc,
6962255736Sdavidch                       (ncsi_oem_data_addr +
6963255736Sdavidch                        offsetof(struct glob_ncsi_oem_data, driver_version)),
6964255736Sdavidch                       0);
6965255736Sdavidch            }
6966255736Sdavidch        }
6967255736Sdavidch    }
6968255736Sdavidch}
6969255736Sdavidch
6970255736Sdavidchstatic void
6971255736Sdavidchbxe_read_mf_cfg(struct bxe_softc *sc)
6972255736Sdavidch{
6973255736Sdavidch    int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
6974255736Sdavidch    int abs_func;
6975255736Sdavidch    int vn;
6976255736Sdavidch
6977255736Sdavidch    if (BXE_NOMCP(sc)) {
6978255736Sdavidch        return; /* what should be the default bvalue in this case */
6979255736Sdavidch    }
6980255736Sdavidch
6981255736Sdavidch    /*
6982255736Sdavidch     * The formula for computing the absolute function number is...
6983255736Sdavidch     * For 2 port configuration (4 functions per port):
6984255736Sdavidch     *   abs_func = 2 * vn + SC_PORT + SC_PATH
6985255736Sdavidch     * For 4 port configuration (2 functions per port):
6986255736Sdavidch     *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
6987255736Sdavidch     */
6988255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
6989255736Sdavidch        abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
6990255736Sdavidch        if (abs_func >= E1H_FUNC_MAX) {
6991255736Sdavidch            break;
6992255736Sdavidch        }
6993255736Sdavidch        sc->devinfo.mf_info.mf_config[vn] =
6994255736Sdavidch            MFCFG_RD(sc, func_mf_config[abs_func].config);
6995255736Sdavidch    }
6996255736Sdavidch
6997255736Sdavidch    if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
6998255736Sdavidch        FUNC_MF_CFG_FUNC_DISABLED) {
6999255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
7000255736Sdavidch        sc->flags |= BXE_MF_FUNC_DIS;
7001255736Sdavidch    } else {
7002255736Sdavidch        BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
7003255736Sdavidch        sc->flags &= ~BXE_MF_FUNC_DIS;
7004255736Sdavidch    }
7005255736Sdavidch}
7006255736Sdavidch
7007255736Sdavidch/* acquire split MCP access lock register */
7008255736Sdavidchstatic int bxe_acquire_alr(struct bxe_softc *sc)
7009255736Sdavidch{
7010255736Sdavidch    uint32_t j, val;
7011255736Sdavidch
7012255736Sdavidch    for (j = 0; j < 1000; j++) {
7013255736Sdavidch        val = (1UL << 31);
7014255736Sdavidch        REG_WR(sc, GRCBASE_MCP + 0x9c, val);
7015255736Sdavidch        val = REG_RD(sc, GRCBASE_MCP + 0x9c);
7016255736Sdavidch        if (val & (1L << 31))
7017255736Sdavidch            break;
7018255736Sdavidch
7019255736Sdavidch        DELAY(5000);
7020255736Sdavidch    }
7021255736Sdavidch
7022255736Sdavidch    if (!(val & (1L << 31))) {
7023255736Sdavidch        BLOGE(sc, "Cannot acquire MCP access lock register\n");
7024255736Sdavidch        return (-1);
7025255736Sdavidch    }
7026255736Sdavidch
7027255736Sdavidch    return (0);
7028255736Sdavidch}
7029255736Sdavidch
7030255736Sdavidch/* release split MCP access lock register */
7031255736Sdavidchstatic void bxe_release_alr(struct bxe_softc *sc)
7032255736Sdavidch{
7033255736Sdavidch    REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
7034255736Sdavidch}
7035255736Sdavidch
7036255736Sdavidchstatic void
7037255736Sdavidchbxe_fan_failure(struct bxe_softc *sc)
7038255736Sdavidch{
7039255736Sdavidch    int port = SC_PORT(sc);
7040255736Sdavidch    uint32_t ext_phy_config;
7041255736Sdavidch
7042255736Sdavidch    /* mark the failure */
7043255736Sdavidch    ext_phy_config =
7044255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7045255736Sdavidch
7046255736Sdavidch    ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7047255736Sdavidch    ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7048255736Sdavidch    SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7049255736Sdavidch             ext_phy_config);
7050255736Sdavidch
7051255736Sdavidch    /* log the failure */
7052255736Sdavidch    BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7053255736Sdavidch              "the card to prevent permanent damage. "
7054255736Sdavidch              "Please contact OEM Support for assistance\n");
7055255736Sdavidch
7056255736Sdavidch    /* XXX */
7057255736Sdavidch#if 1
7058255736Sdavidch    bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7059255736Sdavidch#else
7060255736Sdavidch    /*
7061255736Sdavidch     * Schedule device reset (unload)
7062255736Sdavidch     * This is due to some boards consuming sufficient power when driver is
7063255736Sdavidch     * up to overheat if fan fails.
7064255736Sdavidch     */
7065255736Sdavidch    bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7066255736Sdavidch    schedule_delayed_work(&sc->sp_rtnl_task, 0);
7067255736Sdavidch#endif
7068255736Sdavidch}
7069255736Sdavidch
7070255736Sdavidch/* this function is called upon a link interrupt */
7071255736Sdavidchstatic void
7072255736Sdavidchbxe_link_attn(struct bxe_softc *sc)
7073255736Sdavidch{
7074255736Sdavidch    uint32_t pause_enabled = 0;
7075255736Sdavidch    struct host_port_stats *pstats;
7076255736Sdavidch    int cmng_fns;
7077255736Sdavidch
7078255736Sdavidch    /* Make sure that we are synced with the current statistics */
7079255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_STOP);
7080255736Sdavidch
7081255736Sdavidch    elink_link_update(&sc->link_params, &sc->link_vars);
7082255736Sdavidch
7083255736Sdavidch    if (sc->link_vars.link_up) {
7084255736Sdavidch
7085255736Sdavidch        /* dropless flow control */
7086255736Sdavidch        if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7087255736Sdavidch            pause_enabled = 0;
7088255736Sdavidch
7089255736Sdavidch            if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7090255736Sdavidch                pause_enabled = 1;
7091255736Sdavidch            }
7092255736Sdavidch
7093255736Sdavidch            REG_WR(sc,
7094255736Sdavidch                   (BAR_USTRORM_INTMEM +
7095255736Sdavidch                    USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7096255736Sdavidch                   pause_enabled);
7097255736Sdavidch        }
7098255736Sdavidch
7099255736Sdavidch        if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7100255736Sdavidch            pstats = BXE_SP(sc, port_stats);
7101255736Sdavidch            /* reset old mac stats */
7102255736Sdavidch            memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7103255736Sdavidch        }
7104255736Sdavidch
7105255736Sdavidch        if (sc->state == BXE_STATE_OPEN) {
7106255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7107255736Sdavidch        }
7108255736Sdavidch    }
7109255736Sdavidch
7110255736Sdavidch    if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7111255736Sdavidch        cmng_fns = bxe_get_cmng_fns_mode(sc);
7112255736Sdavidch
7113255736Sdavidch        if (cmng_fns != CMNG_FNS_NONE) {
7114255736Sdavidch            bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7115255736Sdavidch            storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7116255736Sdavidch        } else {
7117255736Sdavidch            /* rate shaping and fairness are disabled */
7118255736Sdavidch            BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7119255736Sdavidch        }
7120255736Sdavidch    }
7121255736Sdavidch
7122255736Sdavidch    bxe_link_report_locked(sc);
7123255736Sdavidch
7124255736Sdavidch    if (IS_MF(sc)) {
7125255736Sdavidch        ; // XXX bxe_link_sync_notify(sc);
7126255736Sdavidch    }
7127255736Sdavidch}
7128255736Sdavidch
7129255736Sdavidchstatic void
7130255736Sdavidchbxe_attn_int_asserted(struct bxe_softc *sc,
7131255736Sdavidch                      uint32_t         asserted)
7132255736Sdavidch{
7133255736Sdavidch    int port = SC_PORT(sc);
7134255736Sdavidch    uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7135255736Sdavidch                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
7136255736Sdavidch    uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7137255736Sdavidch                                        NIG_REG_MASK_INTERRUPT_PORT0;
7138255736Sdavidch    uint32_t aeu_mask;
7139255736Sdavidch    uint32_t nig_mask = 0;
7140255736Sdavidch    uint32_t reg_addr;
7141255736Sdavidch    uint32_t igu_acked;
7142255736Sdavidch    uint32_t cnt;
7143255736Sdavidch
7144255736Sdavidch    if (sc->attn_state & asserted) {
7145255736Sdavidch        BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7146255736Sdavidch    }
7147255736Sdavidch
7148255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7149255736Sdavidch
7150255736Sdavidch    aeu_mask = REG_RD(sc, aeu_addr);
7151255736Sdavidch
7152255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7153255736Sdavidch          aeu_mask, asserted);
7154255736Sdavidch
7155255736Sdavidch    aeu_mask &= ~(asserted & 0x3ff);
7156255736Sdavidch
7157255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7158255736Sdavidch
7159255736Sdavidch    REG_WR(sc, aeu_addr, aeu_mask);
7160255736Sdavidch
7161255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7162255736Sdavidch
7163255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7164255736Sdavidch    sc->attn_state |= asserted;
7165255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7166255736Sdavidch
7167255736Sdavidch    if (asserted & ATTN_HARD_WIRED_MASK) {
7168255736Sdavidch        if (asserted & ATTN_NIG_FOR_FUNC) {
7169255736Sdavidch
7170284335Sdavidcs	    bxe_acquire_phy_lock(sc);
7171255736Sdavidch            /* save nig interrupt mask */
7172255736Sdavidch            nig_mask = REG_RD(sc, nig_int_mask_addr);
7173255736Sdavidch
7174255736Sdavidch            /* If nig_mask is not set, no need to call the update function */
7175255736Sdavidch            if (nig_mask) {
7176255736Sdavidch                REG_WR(sc, nig_int_mask_addr, 0);
7177255736Sdavidch
7178255736Sdavidch                bxe_link_attn(sc);
7179255736Sdavidch            }
7180255736Sdavidch
7181255736Sdavidch            /* handle unicore attn? */
7182255736Sdavidch        }
7183255736Sdavidch
7184255736Sdavidch        if (asserted & ATTN_SW_TIMER_4_FUNC) {
7185255736Sdavidch            BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7186255736Sdavidch        }
7187255736Sdavidch
7188255736Sdavidch        if (asserted & GPIO_2_FUNC) {
7189255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7190255736Sdavidch        }
7191255736Sdavidch
7192255736Sdavidch        if (asserted & GPIO_3_FUNC) {
7193255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7194255736Sdavidch        }
7195255736Sdavidch
7196255736Sdavidch        if (asserted & GPIO_4_FUNC) {
7197255736Sdavidch            BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7198255736Sdavidch        }
7199255736Sdavidch
7200255736Sdavidch        if (port == 0) {
7201255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_1) {
7202255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7203255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7204255736Sdavidch            }
7205255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_2) {
7206255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7207255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7208255736Sdavidch            }
7209255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_3) {
7210255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7211255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7212255736Sdavidch            }
7213255736Sdavidch        } else {
7214255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_4) {
7215255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7216255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7217255736Sdavidch            }
7218255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_5) {
7219255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7220255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7221255736Sdavidch            }
7222255736Sdavidch            if (asserted & ATTN_GENERAL_ATTN_6) {
7223255736Sdavidch                BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7224255736Sdavidch                REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7225255736Sdavidch            }
7226255736Sdavidch        }
7227255736Sdavidch    } /* hardwired */
7228255736Sdavidch
7229255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7230255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7231255736Sdavidch    } else {
7232255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7233255736Sdavidch    }
7234255736Sdavidch
7235255736Sdavidch    BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7236255736Sdavidch          asserted,
7237255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7238255736Sdavidch    REG_WR(sc, reg_addr, asserted);
7239255736Sdavidch
7240255736Sdavidch    /* now set back the mask */
7241255736Sdavidch    if (asserted & ATTN_NIG_FOR_FUNC) {
7242255736Sdavidch        /*
7243255736Sdavidch         * Verify that IGU ack through BAR was written before restoring
7244255736Sdavidch         * NIG mask. This loop should exit after 2-3 iterations max.
7245255736Sdavidch         */
7246255736Sdavidch        if (sc->devinfo.int_block != INT_BLOCK_HC) {
7247255736Sdavidch            cnt = 0;
7248255736Sdavidch
7249255736Sdavidch            do {
7250255736Sdavidch                igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7251255736Sdavidch            } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7252255736Sdavidch                     (++cnt < MAX_IGU_ATTN_ACK_TO));
7253255736Sdavidch
7254255736Sdavidch            if (!igu_acked) {
7255255736Sdavidch                BLOGE(sc, "Failed to verify IGU ack on time\n");
7256255736Sdavidch            }
7257255736Sdavidch
7258255736Sdavidch            mb();
7259255736Sdavidch        }
7260255736Sdavidch
7261255736Sdavidch        REG_WR(sc, nig_int_mask_addr, nig_mask);
7262255736Sdavidch
7263284335Sdavidcs	bxe_release_phy_lock(sc);
7264255736Sdavidch    }
7265255736Sdavidch}
7266255736Sdavidch
7267255736Sdavidchstatic void
7268255736Sdavidchbxe_print_next_block(struct bxe_softc *sc,
7269255736Sdavidch                     int              idx,
7270255736Sdavidch                     const char       *blk)
7271255736Sdavidch{
7272255736Sdavidch    BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7273255736Sdavidch}
7274255736Sdavidch
7275255736Sdavidchstatic int
7276255736Sdavidchbxe_check_blocks_with_parity0(struct bxe_softc *sc,
7277255736Sdavidch                              uint32_t         sig,
7278255736Sdavidch                              int              par_num,
7279255736Sdavidch                              uint8_t          print)
7280255736Sdavidch{
7281255736Sdavidch    uint32_t cur_bit = 0;
7282255736Sdavidch    int i = 0;
7283255736Sdavidch
7284255736Sdavidch    for (i = 0; sig; i++) {
7285255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7286255736Sdavidch        if (sig & cur_bit) {
7287255736Sdavidch            switch (cur_bit) {
7288255736Sdavidch            case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7289255736Sdavidch                if (print)
7290255736Sdavidch                    bxe_print_next_block(sc, par_num++, "BRB");
7291255736Sdavidch                break;
7292255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7293255736Sdavidch                if (print)
7294255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PARSER");
7295255736Sdavidch                break;
7296255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7297255736Sdavidch                if (print)
7298255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSDM");
7299255736Sdavidch                break;
7300255736Sdavidch            case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7301255736Sdavidch                if (print)
7302255736Sdavidch                    bxe_print_next_block(sc, par_num++, "SEARCHER");
7303255736Sdavidch                break;
7304255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7305255736Sdavidch                if (print)
7306255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TCM");
7307255736Sdavidch                break;
7308255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7309255736Sdavidch                if (print)
7310255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TSEMI");
7311255736Sdavidch                break;
7312255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7313255736Sdavidch                if (print)
7314255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XPB");
7315255736Sdavidch                break;
7316255736Sdavidch            }
7317255736Sdavidch
7318255736Sdavidch            /* Clear the bit */
7319255736Sdavidch            sig &= ~cur_bit;
7320255736Sdavidch        }
7321255736Sdavidch    }
7322255736Sdavidch
7323255736Sdavidch    return (par_num);
7324255736Sdavidch}
7325255736Sdavidch
7326255736Sdavidchstatic int
7327255736Sdavidchbxe_check_blocks_with_parity1(struct bxe_softc *sc,
7328255736Sdavidch                              uint32_t         sig,
7329255736Sdavidch                              int              par_num,
7330255736Sdavidch                              uint8_t          *global,
7331255736Sdavidch                              uint8_t          print)
7332255736Sdavidch{
7333255736Sdavidch    int i = 0;
7334255736Sdavidch    uint32_t cur_bit = 0;
7335255736Sdavidch    for (i = 0; sig; i++) {
7336255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7337255736Sdavidch        if (sig & cur_bit) {
7338255736Sdavidch            switch (cur_bit) {
7339255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7340255736Sdavidch                if (print)
7341255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PBF");
7342255736Sdavidch                break;
7343255736Sdavidch            case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7344255736Sdavidch                if (print)
7345255736Sdavidch                    bxe_print_next_block(sc, par_num++, "QM");
7346255736Sdavidch                break;
7347255736Sdavidch            case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7348255736Sdavidch                if (print)
7349255736Sdavidch                    bxe_print_next_block(sc, par_num++, "TM");
7350255736Sdavidch                break;
7351255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7352255736Sdavidch                if (print)
7353255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSDM");
7354255736Sdavidch                break;
7355255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7356255736Sdavidch                if (print)
7357255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XCM");
7358255736Sdavidch                break;
7359255736Sdavidch            case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7360255736Sdavidch                if (print)
7361255736Sdavidch                    bxe_print_next_block(sc, par_num++, "XSEMI");
7362255736Sdavidch                break;
7363255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7364255736Sdavidch                if (print)
7365255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7366255736Sdavidch                break;
7367255736Sdavidch            case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7368255736Sdavidch                if (print)
7369255736Sdavidch                    bxe_print_next_block(sc, par_num++, "NIG");
7370255736Sdavidch                break;
7371255736Sdavidch            case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7372255736Sdavidch                if (print)
7373255736Sdavidch                    bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7374255736Sdavidch                *global = TRUE;
7375255736Sdavidch                break;
7376255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7377255736Sdavidch                if (print)
7378255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DEBUG");
7379255736Sdavidch                break;
7380255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7381255736Sdavidch                if (print)
7382255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USDM");
7383255736Sdavidch                break;
7384255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7385255736Sdavidch                if (print)
7386255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UCM");
7387255736Sdavidch                break;
7388255736Sdavidch            case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7389255736Sdavidch                if (print)
7390255736Sdavidch                    bxe_print_next_block(sc, par_num++, "USEMI");
7391255736Sdavidch                break;
7392255736Sdavidch            case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7393255736Sdavidch                if (print)
7394255736Sdavidch                    bxe_print_next_block(sc, par_num++, "UPB");
7395255736Sdavidch                break;
7396255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7397255736Sdavidch                if (print)
7398255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSDM");
7399255736Sdavidch                break;
7400255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7401255736Sdavidch                if (print)
7402255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CCM");
7403255736Sdavidch                break;
7404255736Sdavidch            }
7405255736Sdavidch
7406255736Sdavidch            /* Clear the bit */
7407255736Sdavidch            sig &= ~cur_bit;
7408255736Sdavidch        }
7409255736Sdavidch    }
7410255736Sdavidch
7411255736Sdavidch    return (par_num);
7412255736Sdavidch}
7413255736Sdavidch
7414255736Sdavidchstatic int
7415255736Sdavidchbxe_check_blocks_with_parity2(struct bxe_softc *sc,
7416255736Sdavidch                              uint32_t         sig,
7417255736Sdavidch                              int              par_num,
7418255736Sdavidch                              uint8_t          print)
7419255736Sdavidch{
7420255736Sdavidch    uint32_t cur_bit = 0;
7421255736Sdavidch    int i = 0;
7422255736Sdavidch
7423255736Sdavidch    for (i = 0; sig; i++) {
7424255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7425255736Sdavidch        if (sig & cur_bit) {
7426255736Sdavidch            switch (cur_bit) {
7427255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7428255736Sdavidch                if (print)
7429255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CSEMI");
7430255736Sdavidch                break;
7431255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7432255736Sdavidch                if (print)
7433255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXP");
7434255736Sdavidch                break;
7435255736Sdavidch            case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7436255736Sdavidch                if (print)
7437255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7438255736Sdavidch                break;
7439255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7440255736Sdavidch                if (print)
7441255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CFC");
7442255736Sdavidch                break;
7443255736Sdavidch            case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7444255736Sdavidch                if (print)
7445255736Sdavidch                    bxe_print_next_block(sc, par_num++, "CDU");
7446255736Sdavidch                break;
7447255736Sdavidch            case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7448255736Sdavidch                if (print)
7449255736Sdavidch                    bxe_print_next_block(sc, par_num++, "DMAE");
7450255736Sdavidch                break;
7451255736Sdavidch            case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7452255736Sdavidch                if (print)
7453255736Sdavidch                    bxe_print_next_block(sc, par_num++, "IGU");
7454255736Sdavidch                break;
7455255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7456255736Sdavidch                if (print)
7457255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MISC");
7458255736Sdavidch                break;
7459255736Sdavidch            }
7460255736Sdavidch
7461255736Sdavidch            /* Clear the bit */
7462255736Sdavidch            sig &= ~cur_bit;
7463255736Sdavidch        }
7464255736Sdavidch    }
7465255736Sdavidch
7466255736Sdavidch    return (par_num);
7467255736Sdavidch}
7468255736Sdavidch
7469255736Sdavidchstatic int
7470255736Sdavidchbxe_check_blocks_with_parity3(struct bxe_softc *sc,
7471255736Sdavidch                              uint32_t         sig,
7472255736Sdavidch                              int              par_num,
7473255736Sdavidch                              uint8_t          *global,
7474255736Sdavidch                              uint8_t          print)
7475255736Sdavidch{
7476255736Sdavidch    uint32_t cur_bit = 0;
7477255736Sdavidch    int i = 0;
7478255736Sdavidch
7479255736Sdavidch    for (i = 0; sig; i++) {
7480255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7481255736Sdavidch        if (sig & cur_bit) {
7482255736Sdavidch            switch (cur_bit) {
7483255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7484255736Sdavidch                if (print)
7485255736Sdavidch                    bxe_print_next_block(sc, par_num++, "MCP ROM");
7486255736Sdavidch                *global = TRUE;
7487255736Sdavidch                break;
7488255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7489255736Sdavidch                if (print)
7490255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7491255736Sdavidch                              "MCP UMP RX");
7492255736Sdavidch                *global = TRUE;
7493255736Sdavidch                break;
7494255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7495255736Sdavidch                if (print)
7496255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7497255736Sdavidch                              "MCP UMP TX");
7498255736Sdavidch                *global = TRUE;
7499255736Sdavidch                break;
7500255736Sdavidch            case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7501255736Sdavidch                if (print)
7502255736Sdavidch                    bxe_print_next_block(sc, par_num++,
7503255736Sdavidch                              "MCP SCPAD");
7504255736Sdavidch                *global = TRUE;
7505255736Sdavidch                break;
7506255736Sdavidch            }
7507255736Sdavidch
7508255736Sdavidch            /* Clear the bit */
7509255736Sdavidch            sig &= ~cur_bit;
7510255736Sdavidch        }
7511255736Sdavidch    }
7512255736Sdavidch
7513255736Sdavidch    return (par_num);
7514255736Sdavidch}
7515255736Sdavidch
7516255736Sdavidchstatic int
7517255736Sdavidchbxe_check_blocks_with_parity4(struct bxe_softc *sc,
7518255736Sdavidch                              uint32_t         sig,
7519255736Sdavidch                              int              par_num,
7520255736Sdavidch                              uint8_t          print)
7521255736Sdavidch{
7522255736Sdavidch    uint32_t cur_bit = 0;
7523255736Sdavidch    int i = 0;
7524255736Sdavidch
7525255736Sdavidch    for (i = 0; sig; i++) {
7526255736Sdavidch        cur_bit = ((uint32_t)0x1 << i);
7527255736Sdavidch        if (sig & cur_bit) {
7528255736Sdavidch            switch (cur_bit) {
7529255736Sdavidch            case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7530255736Sdavidch                if (print)
7531255736Sdavidch                    bxe_print_next_block(sc, par_num++, "PGLUE_B");
7532255736Sdavidch                break;
7533255736Sdavidch            case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7534255736Sdavidch                if (print)
7535255736Sdavidch                    bxe_print_next_block(sc, par_num++, "ATC");
7536255736Sdavidch                break;
7537255736Sdavidch            }
7538255736Sdavidch
7539255736Sdavidch            /* Clear the bit */
7540255736Sdavidch            sig &= ~cur_bit;
7541255736Sdavidch        }
7542255736Sdavidch    }
7543255736Sdavidch
7544255736Sdavidch    return (par_num);
7545255736Sdavidch}
7546255736Sdavidch
7547255736Sdavidchstatic uint8_t
7548255736Sdavidchbxe_parity_attn(struct bxe_softc *sc,
7549255736Sdavidch                uint8_t          *global,
7550255736Sdavidch                uint8_t          print,
7551255736Sdavidch                uint32_t         *sig)
7552255736Sdavidch{
7553255736Sdavidch    int par_num = 0;
7554255736Sdavidch
7555255736Sdavidch    if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7556255736Sdavidch        (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7557255736Sdavidch        (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7558255736Sdavidch        (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7559255736Sdavidch        (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7560255736Sdavidch        BLOGE(sc, "Parity error: HW block parity attention:\n"
7561255736Sdavidch                  "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7562255736Sdavidch              (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7563255736Sdavidch              (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7564255736Sdavidch              (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7565255736Sdavidch              (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7566255736Sdavidch              (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7567255736Sdavidch
7568255736Sdavidch        if (print)
7569255736Sdavidch            BLOGI(sc, "Parity errors detected in blocks: ");
7570255736Sdavidch
7571255736Sdavidch        par_num =
7572255736Sdavidch            bxe_check_blocks_with_parity0(sc, sig[0] &
7573255736Sdavidch                                          HW_PRTY_ASSERT_SET_0,
7574255736Sdavidch                                          par_num, print);
7575255736Sdavidch        par_num =
7576255736Sdavidch            bxe_check_blocks_with_parity1(sc, sig[1] &
7577255736Sdavidch                                          HW_PRTY_ASSERT_SET_1,
7578255736Sdavidch                                          par_num, global, print);
7579255736Sdavidch        par_num =
7580255736Sdavidch            bxe_check_blocks_with_parity2(sc, sig[2] &
7581255736Sdavidch                                          HW_PRTY_ASSERT_SET_2,
7582255736Sdavidch                                          par_num, print);
7583255736Sdavidch        par_num =
7584255736Sdavidch            bxe_check_blocks_with_parity3(sc, sig[3] &
7585255736Sdavidch                                          HW_PRTY_ASSERT_SET_3,
7586255736Sdavidch                                          par_num, global, print);
7587255736Sdavidch        par_num =
7588255736Sdavidch            bxe_check_blocks_with_parity4(sc, sig[4] &
7589255736Sdavidch                                          HW_PRTY_ASSERT_SET_4,
7590255736Sdavidch                                          par_num, print);
7591255736Sdavidch
7592255736Sdavidch        if (print)
7593255736Sdavidch            BLOGI(sc, "\n");
7594255736Sdavidch
7595255736Sdavidch        return (TRUE);
7596255736Sdavidch    }
7597255736Sdavidch
7598255736Sdavidch    return (FALSE);
7599255736Sdavidch}
7600255736Sdavidch
7601255736Sdavidchstatic uint8_t
7602255736Sdavidchbxe_chk_parity_attn(struct bxe_softc *sc,
7603255736Sdavidch                    uint8_t          *global,
7604255736Sdavidch                    uint8_t          print)
7605255736Sdavidch{
7606255736Sdavidch    struct attn_route attn = { {0} };
7607255736Sdavidch    int port = SC_PORT(sc);
7608255736Sdavidch
7609255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7610255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7611255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7612255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7613255736Sdavidch
7614295823Sdavidcs    /*
7615295823Sdavidcs     * Since MCP attentions can't be disabled inside the block, we need to
7616295823Sdavidcs     * read AEU registers to see whether they're currently disabled
7617295823Sdavidcs     */
7618295823Sdavidcs    attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7619295823Sdavidcs                                      : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7620295823Sdavidcs                         MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7621295823Sdavidcs                        ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7622295823Sdavidcs
7623295823Sdavidcs
7624255736Sdavidch    if (!CHIP_IS_E1x(sc))
7625255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7626255736Sdavidch
7627255736Sdavidch    return (bxe_parity_attn(sc, global, print, attn.sig));
7628255736Sdavidch}
7629255736Sdavidch
7630255736Sdavidchstatic void
7631255736Sdavidchbxe_attn_int_deasserted4(struct bxe_softc *sc,
7632255736Sdavidch                         uint32_t         attn)
7633255736Sdavidch{
7634255736Sdavidch    uint32_t val;
7635255736Sdavidch
7636255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7637255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7638255736Sdavidch        BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7639255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7640255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7641255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7642255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7643255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7644255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7645255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7646255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7647255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7648255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7649255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7650255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7651255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7652255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7653255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7654255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7655255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7656255736Sdavidch            BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7657255736Sdavidch    }
7658255736Sdavidch
7659255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7660255736Sdavidch        val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7661255736Sdavidch        BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7662255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7663255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7664255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7665255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7666255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7667255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7668255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7669255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7670255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7671255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7672255736Sdavidch        if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7673255736Sdavidch            BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7674255736Sdavidch    }
7675255736Sdavidch
7676255736Sdavidch    if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7677255736Sdavidch                AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7678255736Sdavidch        BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7679255736Sdavidch              (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7680255736Sdavidch                                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7681255736Sdavidch    }
7682255736Sdavidch}
7683255736Sdavidch
7684255736Sdavidchstatic void
7685255736Sdavidchbxe_e1h_disable(struct bxe_softc *sc)
7686255736Sdavidch{
7687255736Sdavidch    int port = SC_PORT(sc);
7688255736Sdavidch
7689255736Sdavidch    bxe_tx_disable(sc);
7690255736Sdavidch
7691255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7692255736Sdavidch}
7693255736Sdavidch
7694255736Sdavidchstatic void
7695255736Sdavidchbxe_e1h_enable(struct bxe_softc *sc)
7696255736Sdavidch{
7697255736Sdavidch    int port = SC_PORT(sc);
7698255736Sdavidch
7699255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7700255736Sdavidch
7701255736Sdavidch    // XXX bxe_tx_enable(sc);
7702255736Sdavidch}
7703255736Sdavidch
7704255736Sdavidch/*
7705255736Sdavidch * called due to MCP event (on pmf):
7706255736Sdavidch *   reread new bandwidth configuration
7707255736Sdavidch *   configure FW
7708255736Sdavidch *   notify others function about the change
7709255736Sdavidch */
7710255736Sdavidchstatic void
7711255736Sdavidchbxe_config_mf_bw(struct bxe_softc *sc)
7712255736Sdavidch{
7713255736Sdavidch    if (sc->link_vars.link_up) {
7714255736Sdavidch        bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7715255736Sdavidch        // XXX bxe_link_sync_notify(sc);
7716255736Sdavidch    }
7717255736Sdavidch
7718255736Sdavidch    storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7719255736Sdavidch}
7720255736Sdavidch
7721255736Sdavidchstatic void
7722255736Sdavidchbxe_set_mf_bw(struct bxe_softc *sc)
7723255736Sdavidch{
7724255736Sdavidch    bxe_config_mf_bw(sc);
7725255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7726255736Sdavidch}
7727255736Sdavidch
7728255736Sdavidchstatic void
7729255736Sdavidchbxe_handle_eee_event(struct bxe_softc *sc)
7730255736Sdavidch{
7731255736Sdavidch    BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7732255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7733255736Sdavidch}
7734255736Sdavidch
7735255736Sdavidch#define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7736255736Sdavidch
7737255736Sdavidchstatic void
7738255736Sdavidchbxe_drv_info_ether_stat(struct bxe_softc *sc)
7739255736Sdavidch{
7740255736Sdavidch    struct eth_stats_info *ether_stat =
7741255736Sdavidch        &sc->sp->drv_info_to_mcp.ether_stat;
7742255736Sdavidch
7743255736Sdavidch    strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7744255736Sdavidch            ETH_STAT_INFO_VERSION_LEN);
7745255736Sdavidch
7746255736Sdavidch    /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7747255736Sdavidch    sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7748255736Sdavidch                                          DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7749255736Sdavidch                                          ether_stat->mac_local + MAC_PAD,
7750255736Sdavidch                                          MAC_PAD, ETH_ALEN);
7751255736Sdavidch
7752255736Sdavidch    ether_stat->mtu_size = sc->mtu;
7753255736Sdavidch
7754255736Sdavidch    ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7755266979Smarcel    if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
7756255736Sdavidch        ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7757255736Sdavidch    }
7758255736Sdavidch
7759255736Sdavidch    // XXX ether_stat->feature_flags |= ???;
7760255736Sdavidch
7761255736Sdavidch    ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7762255736Sdavidch
7763255736Sdavidch    ether_stat->txq_size = sc->tx_ring_size;
7764255736Sdavidch    ether_stat->rxq_size = sc->rx_ring_size;
7765255736Sdavidch}
7766255736Sdavidch
7767255736Sdavidchstatic void
7768255736Sdavidchbxe_handle_drv_info_req(struct bxe_softc *sc)
7769255736Sdavidch{
7770255736Sdavidch    enum drv_info_opcode op_code;
7771255736Sdavidch    uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7772255736Sdavidch
7773255736Sdavidch    /* if drv_info version supported by MFW doesn't match - send NACK */
7774255736Sdavidch    if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7775255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7776255736Sdavidch        return;
7777255736Sdavidch    }
7778255736Sdavidch
7779255736Sdavidch    op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7780255736Sdavidch               DRV_INFO_CONTROL_OP_CODE_SHIFT);
7781255736Sdavidch
7782255736Sdavidch    memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7783255736Sdavidch
7784255736Sdavidch    switch (op_code) {
7785255736Sdavidch    case ETH_STATS_OPCODE:
7786255736Sdavidch        bxe_drv_info_ether_stat(sc);
7787255736Sdavidch        break;
7788255736Sdavidch    case FCOE_STATS_OPCODE:
7789255736Sdavidch    case ISCSI_STATS_OPCODE:
7790255736Sdavidch    default:
7791255736Sdavidch        /* if op code isn't supported - send NACK */
7792255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7793255736Sdavidch        return;
7794255736Sdavidch    }
7795255736Sdavidch
7796255736Sdavidch    /*
7797255736Sdavidch     * If we got drv_info attn from MFW then these fields are defined in
7798255736Sdavidch     * shmem2 for sure
7799255736Sdavidch     */
7800255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_lo,
7801255736Sdavidch              U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7802255736Sdavidch    SHMEM2_WR(sc, drv_info_host_addr_hi,
7803255736Sdavidch              U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7804255736Sdavidch
7805255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7806255736Sdavidch}
7807255736Sdavidch
7808255736Sdavidchstatic void
7809255736Sdavidchbxe_dcc_event(struct bxe_softc *sc,
7810255736Sdavidch              uint32_t         dcc_event)
7811255736Sdavidch{
7812255736Sdavidch    BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7813255736Sdavidch
7814255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7815255736Sdavidch        /*
7816255736Sdavidch         * This is the only place besides the function initialization
7817255736Sdavidch         * where the sc->flags can change so it is done without any
7818255736Sdavidch         * locks
7819255736Sdavidch         */
7820255736Sdavidch        if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7821255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7822255736Sdavidch            sc->flags |= BXE_MF_FUNC_DIS;
7823255736Sdavidch            bxe_e1h_disable(sc);
7824255736Sdavidch        } else {
7825255736Sdavidch            BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7826255736Sdavidch            sc->flags &= ~BXE_MF_FUNC_DIS;
7827255736Sdavidch            bxe_e1h_enable(sc);
7828255736Sdavidch        }
7829255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7830255736Sdavidch    }
7831255736Sdavidch
7832255736Sdavidch    if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7833255736Sdavidch        bxe_config_mf_bw(sc);
7834255736Sdavidch        dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7835255736Sdavidch    }
7836255736Sdavidch
7837255736Sdavidch    /* Report results to MCP */
7838255736Sdavidch    if (dcc_event)
7839255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7840255736Sdavidch    else
7841255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7842255736Sdavidch}
7843255736Sdavidch
7844255736Sdavidchstatic void
7845255736Sdavidchbxe_pmf_update(struct bxe_softc *sc)
7846255736Sdavidch{
7847255736Sdavidch    int port = SC_PORT(sc);
7848255736Sdavidch    uint32_t val;
7849255736Sdavidch
7850255736Sdavidch    sc->port.pmf = 1;
7851255736Sdavidch    BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7852255736Sdavidch
7853255736Sdavidch    /*
7854255736Sdavidch     * We need the mb() to ensure the ordering between the writing to
7855255736Sdavidch     * sc->port.pmf here and reading it from the bxe_periodic_task().
7856255736Sdavidch     */
7857255736Sdavidch    mb();
7858255736Sdavidch
7859255736Sdavidch    /* queue a periodic task */
7860255736Sdavidch    // XXX schedule task...
7861255736Sdavidch
7862255736Sdavidch    // XXX bxe_dcbx_pmf_update(sc);
7863255736Sdavidch
7864255736Sdavidch    /* enable nig attention */
7865255736Sdavidch    val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7866255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
7867255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7868255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7869255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
7870255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7871255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7872255736Sdavidch    }
7873255736Sdavidch
7874255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_PMF);
7875255736Sdavidch}
7876255736Sdavidch
7877255736Sdavidchstatic int
7878255736Sdavidchbxe_mc_assert(struct bxe_softc *sc)
7879255736Sdavidch{
7880255736Sdavidch    char last_idx;
7881255736Sdavidch    int i, rc = 0;
7882255736Sdavidch    uint32_t row0, row1, row2, row3;
7883255736Sdavidch
7884255736Sdavidch    /* XSTORM */
7885255736Sdavidch    last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7886255736Sdavidch    if (last_idx)
7887255736Sdavidch        BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7888255736Sdavidch
7889255736Sdavidch    /* print the asserts */
7890255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7891255736Sdavidch
7892255736Sdavidch        row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7893255736Sdavidch        row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7894255736Sdavidch        row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7895255736Sdavidch        row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7896255736Sdavidch
7897255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7898255736Sdavidch            BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7899255736Sdavidch                  i, row3, row2, row1, row0);
7900255736Sdavidch            rc++;
7901255736Sdavidch        } else {
7902255736Sdavidch            break;
7903255736Sdavidch        }
7904255736Sdavidch    }
7905255736Sdavidch
7906255736Sdavidch    /* TSTORM */
7907255736Sdavidch    last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7908255736Sdavidch    if (last_idx) {
7909255736Sdavidch        BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7910255736Sdavidch    }
7911255736Sdavidch
7912255736Sdavidch    /* print the asserts */
7913255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7914255736Sdavidch
7915255736Sdavidch        row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7916255736Sdavidch        row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7917255736Sdavidch        row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7918255736Sdavidch        row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7919255736Sdavidch
7920255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7921255736Sdavidch            BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7922255736Sdavidch                  i, row3, row2, row1, row0);
7923255736Sdavidch            rc++;
7924255736Sdavidch        } else {
7925255736Sdavidch            break;
7926255736Sdavidch        }
7927255736Sdavidch    }
7928255736Sdavidch
7929255736Sdavidch    /* CSTORM */
7930255736Sdavidch    last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7931255736Sdavidch    if (last_idx) {
7932255736Sdavidch        BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7933255736Sdavidch    }
7934255736Sdavidch
7935255736Sdavidch    /* print the asserts */
7936255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7937255736Sdavidch
7938255736Sdavidch        row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7939255736Sdavidch        row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7940255736Sdavidch        row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7941255736Sdavidch        row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7942255736Sdavidch
7943255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7944255736Sdavidch            BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7945255736Sdavidch                  i, row3, row2, row1, row0);
7946255736Sdavidch            rc++;
7947255736Sdavidch        } else {
7948255736Sdavidch            break;
7949255736Sdavidch        }
7950255736Sdavidch    }
7951255736Sdavidch
7952255736Sdavidch    /* USTORM */
7953255736Sdavidch    last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7954255736Sdavidch    if (last_idx) {
7955255736Sdavidch        BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7956255736Sdavidch    }
7957255736Sdavidch
7958255736Sdavidch    /* print the asserts */
7959255736Sdavidch    for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7960255736Sdavidch
7961255736Sdavidch        row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7962255736Sdavidch        row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7963255736Sdavidch        row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7964255736Sdavidch        row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7965255736Sdavidch
7966255736Sdavidch        if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7967255736Sdavidch            BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7968255736Sdavidch                  i, row3, row2, row1, row0);
7969255736Sdavidch            rc++;
7970255736Sdavidch        } else {
7971255736Sdavidch            break;
7972255736Sdavidch        }
7973255736Sdavidch    }
7974255736Sdavidch
7975255736Sdavidch    return (rc);
7976255736Sdavidch}
7977255736Sdavidch
7978255736Sdavidchstatic void
7979255736Sdavidchbxe_attn_int_deasserted3(struct bxe_softc *sc,
7980255736Sdavidch                         uint32_t         attn)
7981255736Sdavidch{
7982255736Sdavidch    int func = SC_FUNC(sc);
7983255736Sdavidch    uint32_t val;
7984255736Sdavidch
7985255736Sdavidch    if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
7986255736Sdavidch
7987255736Sdavidch        if (attn & BXE_PMF_LINK_ASSERT(sc)) {
7988255736Sdavidch
7989255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
7990255736Sdavidch            bxe_read_mf_cfg(sc);
7991255736Sdavidch            sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
7992255736Sdavidch                MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
7993255736Sdavidch            val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
7994255736Sdavidch
7995255736Sdavidch            if (val & DRV_STATUS_DCC_EVENT_MASK)
7996255736Sdavidch                bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
7997255736Sdavidch
7998255736Sdavidch            if (val & DRV_STATUS_SET_MF_BW)
7999255736Sdavidch                bxe_set_mf_bw(sc);
8000255736Sdavidch
8001255736Sdavidch            if (val & DRV_STATUS_DRV_INFO_REQ)
8002255736Sdavidch                bxe_handle_drv_info_req(sc);
8003255736Sdavidch
8004255736Sdavidch            if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
8005255736Sdavidch                bxe_pmf_update(sc);
8006255736Sdavidch
8007255736Sdavidch            if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
8008255736Sdavidch                bxe_handle_eee_event(sc);
8009255736Sdavidch
8010255736Sdavidch            if (sc->link_vars.periodic_flags &
8011255736Sdavidch                ELINK_PERIODIC_FLAGS_LINK_EVENT) {
8012255736Sdavidch                /* sync with link */
8013284335Sdavidcs		bxe_acquire_phy_lock(sc);
8014255736Sdavidch                sc->link_vars.periodic_flags &=
8015255736Sdavidch                    ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
8016284335Sdavidcs		bxe_release_phy_lock(sc);
8017255736Sdavidch                if (IS_MF(sc))
8018255736Sdavidch                    ; // XXX bxe_link_sync_notify(sc);
8019255736Sdavidch                bxe_link_report(sc);
8020255736Sdavidch            }
8021255736Sdavidch
8022255736Sdavidch            /*
8023255736Sdavidch             * Always call it here: bxe_link_report() will
8024255736Sdavidch             * prevent the link indication duplication.
8025255736Sdavidch             */
8026255736Sdavidch            bxe_link_status_update(sc);
8027255736Sdavidch
8028255736Sdavidch        } else if (attn & BXE_MC_ASSERT_BITS) {
8029255736Sdavidch
8030255736Sdavidch            BLOGE(sc, "MC assert!\n");
8031255736Sdavidch            bxe_mc_assert(sc);
8032255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
8033255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
8034255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
8035255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
8036255736Sdavidch            bxe_panic(sc, ("MC assert!\n"));
8037255736Sdavidch
8038255736Sdavidch        } else if (attn & BXE_MCP_ASSERT) {
8039255736Sdavidch
8040255736Sdavidch            BLOGE(sc, "MCP assert!\n");
8041255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8042255736Sdavidch            // XXX bxe_fw_dump(sc);
8043255736Sdavidch
8044255736Sdavidch        } else {
8045255736Sdavidch            BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8046255736Sdavidch        }
8047255736Sdavidch    }
8048255736Sdavidch
8049255736Sdavidch    if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8050255736Sdavidch        BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8051255736Sdavidch        if (attn & BXE_GRC_TIMEOUT) {
8052255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8053255736Sdavidch            BLOGE(sc, "GRC time-out 0x%08x\n", val);
8054255736Sdavidch        }
8055255736Sdavidch        if (attn & BXE_GRC_RSV) {
8056255736Sdavidch            val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8057255736Sdavidch            BLOGE(sc, "GRC reserved 0x%08x\n", val);
8058255736Sdavidch        }
8059255736Sdavidch        REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8060255736Sdavidch    }
8061255736Sdavidch}
8062255736Sdavidch
8063255736Sdavidchstatic void
8064255736Sdavidchbxe_attn_int_deasserted2(struct bxe_softc *sc,
8065255736Sdavidch                         uint32_t         attn)
8066255736Sdavidch{
8067255736Sdavidch    int port = SC_PORT(sc);
8068255736Sdavidch    int reg_offset;
8069255736Sdavidch    uint32_t val0, mask0, val1, mask1;
8070255736Sdavidch    uint32_t val;
8071255736Sdavidch
8072255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8073255736Sdavidch        val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8074255736Sdavidch        BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8075255736Sdavidch        /* CFC error attention */
8076255736Sdavidch        if (val & 0x2) {
8077255736Sdavidch            BLOGE(sc, "FATAL error from CFC\n");
8078255736Sdavidch        }
8079255736Sdavidch    }
8080255736Sdavidch
8081255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8082255736Sdavidch        val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8083255736Sdavidch        BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8084255736Sdavidch        /* RQ_USDMDP_FIFO_OVERFLOW */
8085255736Sdavidch        if (val & 0x18000) {
8086255736Sdavidch            BLOGE(sc, "FATAL error from PXP\n");
8087255736Sdavidch        }
8088255736Sdavidch
8089255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8090255736Sdavidch            val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8091255736Sdavidch            BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8092255736Sdavidch        }
8093255736Sdavidch    }
8094255736Sdavidch
8095255736Sdavidch#define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8096255736Sdavidch#define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8097255736Sdavidch
8098255736Sdavidch    if (attn & AEU_PXP2_HW_INT_BIT) {
8099255736Sdavidch        /*  CQ47854 workaround do not panic on
8100255736Sdavidch         *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8101255736Sdavidch         */
8102255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
8103255736Sdavidch            mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8104255736Sdavidch            val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8105255736Sdavidch            mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8106255736Sdavidch            val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8107255736Sdavidch            /*
8108255736Sdavidch             * If the olny PXP2_EOP_ERROR_BIT is set in
8109255736Sdavidch             * STS0 and STS1 - clear it
8110255736Sdavidch             *
8111255736Sdavidch             * probably we lose additional attentions between
8112255736Sdavidch             * STS0 and STS_CLR0, in this case user will not
8113255736Sdavidch             * be notified about them
8114255736Sdavidch             */
8115255736Sdavidch            if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8116255736Sdavidch                !(val1 & mask1))
8117255736Sdavidch                val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8118255736Sdavidch
8119255736Sdavidch            /* print the register, since no one can restore it */
8120255736Sdavidch            BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8121255736Sdavidch
8122255736Sdavidch            /*
8123255736Sdavidch             * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8124255736Sdavidch             * then notify
8125255736Sdavidch             */
8126255736Sdavidch            if (val0 & PXP2_EOP_ERROR_BIT) {
8127255736Sdavidch                BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8128255736Sdavidch
8129255736Sdavidch                /*
8130255736Sdavidch                 * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8131255736Sdavidch                 * set then clear attention from PXP2 block without panic
8132255736Sdavidch                 */
8133255736Sdavidch                if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8134255736Sdavidch                    ((val1 & mask1) == 0))
8135255736Sdavidch                    attn &= ~AEU_PXP2_HW_INT_BIT;
8136255736Sdavidch            }
8137255736Sdavidch        }
8138255736Sdavidch    }
8139255736Sdavidch
8140255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_2) {
8141255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8142255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8143255736Sdavidch
8144255736Sdavidch        val = REG_RD(sc, reg_offset);
8145255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8146255736Sdavidch        REG_WR(sc, reg_offset, val);
8147255736Sdavidch
8148255736Sdavidch        BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8149255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8150255736Sdavidch        bxe_panic(sc, ("HW block attention set2\n"));
8151255736Sdavidch    }
8152255736Sdavidch}
8153255736Sdavidch
8154255736Sdavidchstatic void
8155255736Sdavidchbxe_attn_int_deasserted1(struct bxe_softc *sc,
8156255736Sdavidch                         uint32_t         attn)
8157255736Sdavidch{
8158255736Sdavidch    int port = SC_PORT(sc);
8159255736Sdavidch    int reg_offset;
8160255736Sdavidch    uint32_t val;
8161255736Sdavidch
8162255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8163255736Sdavidch        val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8164255736Sdavidch        BLOGE(sc, "DB hw attention 0x%08x\n", val);
8165255736Sdavidch        /* DORQ discard attention */
8166255736Sdavidch        if (val & 0x2) {
8167255736Sdavidch            BLOGE(sc, "FATAL error from DORQ\n");
8168255736Sdavidch        }
8169255736Sdavidch    }
8170255736Sdavidch
8171255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_1) {
8172255736Sdavidch        reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8173255736Sdavidch                             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8174255736Sdavidch
8175255736Sdavidch        val = REG_RD(sc, reg_offset);
8176255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8177255736Sdavidch        REG_WR(sc, reg_offset, val);
8178255736Sdavidch
8179255736Sdavidch        BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8180255736Sdavidch              (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8181255736Sdavidch        bxe_panic(sc, ("HW block attention set1\n"));
8182255736Sdavidch    }
8183255736Sdavidch}
8184255736Sdavidch
8185255736Sdavidchstatic void
8186255736Sdavidchbxe_attn_int_deasserted0(struct bxe_softc *sc,
8187255736Sdavidch                         uint32_t         attn)
8188255736Sdavidch{
8189255736Sdavidch    int port = SC_PORT(sc);
8190255736Sdavidch    int reg_offset;
8191255736Sdavidch    uint32_t val;
8192255736Sdavidch
8193255736Sdavidch    reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8194255736Sdavidch                          MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8195255736Sdavidch
8196255736Sdavidch    if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8197255736Sdavidch        val = REG_RD(sc, reg_offset);
8198255736Sdavidch        val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8199255736Sdavidch        REG_WR(sc, reg_offset, val);
8200255736Sdavidch
8201255736Sdavidch        BLOGW(sc, "SPIO5 hw attention\n");
8202255736Sdavidch
8203255736Sdavidch        /* Fan failure attention */
8204255736Sdavidch        elink_hw_reset_phy(&sc->link_params);
8205255736Sdavidch        bxe_fan_failure(sc);
8206255736Sdavidch    }
8207255736Sdavidch
8208255736Sdavidch    if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8209284335Sdavidcs	bxe_acquire_phy_lock(sc);
8210255736Sdavidch        elink_handle_module_detect_int(&sc->link_params);
8211284335Sdavidcs	bxe_release_phy_lock(sc);
8212255736Sdavidch    }
8213255736Sdavidch
8214255736Sdavidch    if (attn & HW_INTERRUT_ASSERT_SET_0) {
8215255736Sdavidch        val = REG_RD(sc, reg_offset);
8216255736Sdavidch        val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8217255736Sdavidch        REG_WR(sc, reg_offset, val);
8218255736Sdavidch
8219255736Sdavidch        bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8220255736Sdavidch                       (attn & HW_INTERRUT_ASSERT_SET_0)));
8221255736Sdavidch    }
8222255736Sdavidch}
8223255736Sdavidch
8224255736Sdavidchstatic void
8225255736Sdavidchbxe_attn_int_deasserted(struct bxe_softc *sc,
8226255736Sdavidch                        uint32_t         deasserted)
8227255736Sdavidch{
8228255736Sdavidch    struct attn_route attn;
8229255736Sdavidch    struct attn_route *group_mask;
8230255736Sdavidch    int port = SC_PORT(sc);
8231255736Sdavidch    int index;
8232255736Sdavidch    uint32_t reg_addr;
8233255736Sdavidch    uint32_t val;
8234255736Sdavidch    uint32_t aeu_mask;
8235255736Sdavidch    uint8_t global = FALSE;
8236255736Sdavidch
8237255736Sdavidch    /*
8238255736Sdavidch     * Need to take HW lock because MCP or other port might also
8239255736Sdavidch     * try to handle this event.
8240255736Sdavidch     */
8241255736Sdavidch    bxe_acquire_alr(sc);
8242255736Sdavidch
8243255736Sdavidch    if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8244255736Sdavidch        /* XXX
8245255736Sdavidch         * In case of parity errors don't handle attentions so that
8246255736Sdavidch         * other function would "see" parity errors.
8247255736Sdavidch         */
8248255736Sdavidch        sc->recovery_state = BXE_RECOVERY_INIT;
8249255736Sdavidch        // XXX schedule a recovery task...
8250255736Sdavidch        /* disable HW interrupts */
8251255736Sdavidch        bxe_int_disable(sc);
8252255736Sdavidch        bxe_release_alr(sc);
8253255736Sdavidch        return;
8254255736Sdavidch    }
8255255736Sdavidch
8256255736Sdavidch    attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8257255736Sdavidch    attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8258255736Sdavidch    attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8259255736Sdavidch    attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8260255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
8261255736Sdavidch        attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8262255736Sdavidch    } else {
8263255736Sdavidch        attn.sig[4] = 0;
8264255736Sdavidch    }
8265255736Sdavidch
8266255736Sdavidch    BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8267255736Sdavidch          attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8268255736Sdavidch
8269255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8270255736Sdavidch        if (deasserted & (1 << index)) {
8271255736Sdavidch            group_mask = &sc->attn_group[index];
8272255736Sdavidch
8273255736Sdavidch            BLOGD(sc, DBG_INTR,
8274255736Sdavidch                  "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8275255736Sdavidch                  group_mask->sig[0], group_mask->sig[1],
8276255736Sdavidch                  group_mask->sig[2], group_mask->sig[3],
8277255736Sdavidch                  group_mask->sig[4]);
8278255736Sdavidch
8279255736Sdavidch            bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8280255736Sdavidch            bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8281255736Sdavidch            bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8282255736Sdavidch            bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8283255736Sdavidch            bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8284255736Sdavidch        }
8285255736Sdavidch    }
8286255736Sdavidch
8287255736Sdavidch    bxe_release_alr(sc);
8288255736Sdavidch
8289255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
8290255736Sdavidch        reg_addr = (HC_REG_COMMAND_REG + port*32 +
8291255736Sdavidch                    COMMAND_REG_ATTN_BITS_CLR);
8292255736Sdavidch    } else {
8293255736Sdavidch        reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8294255736Sdavidch    }
8295255736Sdavidch
8296255736Sdavidch    val = ~deasserted;
8297255736Sdavidch    BLOGD(sc, DBG_INTR,
8298255736Sdavidch          "about to mask 0x%08x at %s addr 0x%08x\n", val,
8299255736Sdavidch          (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8300255736Sdavidch    REG_WR(sc, reg_addr, val);
8301255736Sdavidch
8302255736Sdavidch    if (~sc->attn_state & deasserted) {
8303255736Sdavidch        BLOGE(sc, "IGU error\n");
8304255736Sdavidch    }
8305255736Sdavidch
8306255736Sdavidch    reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8307255736Sdavidch                      MISC_REG_AEU_MASK_ATTN_FUNC_0;
8308255736Sdavidch
8309255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8310255736Sdavidch
8311255736Sdavidch    aeu_mask = REG_RD(sc, reg_addr);
8312255736Sdavidch
8313255736Sdavidch    BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8314255736Sdavidch          aeu_mask, deasserted);
8315255736Sdavidch    aeu_mask |= (deasserted & 0x3ff);
8316255736Sdavidch    BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8317255736Sdavidch
8318255736Sdavidch    REG_WR(sc, reg_addr, aeu_mask);
8319255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8320255736Sdavidch
8321255736Sdavidch    BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8322255736Sdavidch    sc->attn_state &= ~deasserted;
8323255736Sdavidch    BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8324255736Sdavidch}
8325255736Sdavidch
8326255736Sdavidchstatic void
8327255736Sdavidchbxe_attn_int(struct bxe_softc *sc)
8328255736Sdavidch{
8329255736Sdavidch    /* read local copy of bits */
8330255736Sdavidch    uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8331255736Sdavidch    uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8332255736Sdavidch    uint32_t attn_state = sc->attn_state;
8333255736Sdavidch
8334255736Sdavidch    /* look for changed bits */
8335255736Sdavidch    uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8336255736Sdavidch    uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8337255736Sdavidch
8338255736Sdavidch    BLOGD(sc, DBG_INTR,
8339255736Sdavidch          "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8340255736Sdavidch          attn_bits, attn_ack, asserted, deasserted);
8341255736Sdavidch
8342255736Sdavidch    if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8343255736Sdavidch        BLOGE(sc, "BAD attention state\n");
8344255736Sdavidch    }
8345255736Sdavidch
8346255736Sdavidch    /* handle bits that were raised */
8347255736Sdavidch    if (asserted) {
8348255736Sdavidch        bxe_attn_int_asserted(sc, asserted);
8349255736Sdavidch    }
8350255736Sdavidch
8351255736Sdavidch    if (deasserted) {
8352255736Sdavidch        bxe_attn_int_deasserted(sc, deasserted);
8353255736Sdavidch    }
8354255736Sdavidch}
8355255736Sdavidch
8356255736Sdavidchstatic uint16_t
8357255736Sdavidchbxe_update_dsb_idx(struct bxe_softc *sc)
8358255736Sdavidch{
8359255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
8360255736Sdavidch    uint16_t rc = 0;
8361255736Sdavidch
8362255736Sdavidch    mb(); /* status block is written to by the chip */
8363255736Sdavidch
8364255736Sdavidch    if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8365255736Sdavidch        sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8366255736Sdavidch        rc |= BXE_DEF_SB_ATT_IDX;
8367255736Sdavidch    }
8368255736Sdavidch
8369255736Sdavidch    if (sc->def_idx != def_sb->sp_sb.running_index) {
8370255736Sdavidch        sc->def_idx = def_sb->sp_sb.running_index;
8371255736Sdavidch        rc |= BXE_DEF_SB_IDX;
8372255736Sdavidch    }
8373255736Sdavidch
8374255736Sdavidch    mb();
8375255736Sdavidch
8376255736Sdavidch    return (rc);
8377255736Sdavidch}
8378255736Sdavidch
8379255736Sdavidchstatic inline struct ecore_queue_sp_obj *
8380255736Sdavidchbxe_cid_to_q_obj(struct bxe_softc *sc,
8381255736Sdavidch                 uint32_t         cid)
8382255736Sdavidch{
8383255736Sdavidch    BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8384255736Sdavidch    return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8385255736Sdavidch}
8386255736Sdavidch
8387255736Sdavidchstatic void
8388255736Sdavidchbxe_handle_mcast_eqe(struct bxe_softc *sc)
8389255736Sdavidch{
8390255736Sdavidch    struct ecore_mcast_ramrod_params rparam;
8391255736Sdavidch    int rc;
8392255736Sdavidch
8393255736Sdavidch    memset(&rparam, 0, sizeof(rparam));
8394255736Sdavidch
8395255736Sdavidch    rparam.mcast_obj = &sc->mcast_obj;
8396255736Sdavidch
8397255736Sdavidch    BXE_MCAST_LOCK(sc);
8398255736Sdavidch
8399255736Sdavidch    /* clear pending state for the last command */
8400255736Sdavidch    sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8401255736Sdavidch
8402255736Sdavidch    /* if there are pending mcast commands - send them */
8403255736Sdavidch    if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8404255736Sdavidch        rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8405255736Sdavidch        if (rc < 0) {
8406255736Sdavidch            BLOGD(sc, DBG_SP,
8407295830Sdavidcs                "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8408255736Sdavidch        }
8409255736Sdavidch    }
8410255736Sdavidch
8411255736Sdavidch    BXE_MCAST_UNLOCK(sc);
8412255736Sdavidch}
8413255736Sdavidch
8414255736Sdavidchstatic void
8415255736Sdavidchbxe_handle_classification_eqe(struct bxe_softc      *sc,
8416255736Sdavidch                              union event_ring_elem *elem)
8417255736Sdavidch{
8418255736Sdavidch    unsigned long ramrod_flags = 0;
8419255736Sdavidch    int rc = 0;
8420255736Sdavidch    uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8421255736Sdavidch    struct ecore_vlan_mac_obj *vlan_mac_obj;
8422255736Sdavidch
8423255736Sdavidch    /* always push next commands out, don't wait here */
8424255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
8425255736Sdavidch
8426255736Sdavidch    switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8427255736Sdavidch    case ECORE_FILTER_MAC_PENDING:
8428255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8429255736Sdavidch        vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8430255736Sdavidch        break;
8431255736Sdavidch
8432255736Sdavidch    case ECORE_FILTER_MCAST_PENDING:
8433255736Sdavidch        BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8434255736Sdavidch        /*
8435255736Sdavidch         * This is only relevant for 57710 where multicast MACs are
8436255736Sdavidch         * configured as unicast MACs using the same ramrod.
8437255736Sdavidch         */
8438255736Sdavidch        bxe_handle_mcast_eqe(sc);
8439255736Sdavidch        return;
8440255736Sdavidch
8441255736Sdavidch    default:
8442255736Sdavidch        BLOGE(sc, "Unsupported classification command: %d\n",
8443255736Sdavidch              elem->message.data.eth_event.echo);
8444255736Sdavidch        return;
8445255736Sdavidch    }
8446255736Sdavidch
8447255736Sdavidch    rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8448255736Sdavidch
8449255736Sdavidch    if (rc < 0) {
8450255736Sdavidch        BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8451255736Sdavidch    } else if (rc > 0) {
8452255736Sdavidch        BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8453255736Sdavidch    }
8454255736Sdavidch}
8455255736Sdavidch
8456255736Sdavidchstatic void
8457255736Sdavidchbxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8458255736Sdavidch                       union event_ring_elem *elem)
8459255736Sdavidch{
8460255736Sdavidch    bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8461255736Sdavidch
8462255736Sdavidch    /* send rx_mode command again if was requested */
8463255736Sdavidch    if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8464255736Sdavidch                               &sc->sp_state)) {
8465255736Sdavidch        bxe_set_storm_rx_mode(sc);
8466255736Sdavidch    }
8467255736Sdavidch}
8468255736Sdavidch
8469255736Sdavidchstatic void
8470255736Sdavidchbxe_update_eq_prod(struct bxe_softc *sc,
8471255736Sdavidch                   uint16_t         prod)
8472255736Sdavidch{
8473255736Sdavidch    storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8474255736Sdavidch    wmb(); /* keep prod updates ordered */
8475255736Sdavidch}
8476255736Sdavidch
8477255736Sdavidchstatic void
8478255736Sdavidchbxe_eq_int(struct bxe_softc *sc)
8479255736Sdavidch{
8480255736Sdavidch    uint16_t hw_cons, sw_cons, sw_prod;
8481255736Sdavidch    union event_ring_elem *elem;
8482255736Sdavidch    uint8_t echo;
8483255736Sdavidch    uint32_t cid;
8484255736Sdavidch    uint8_t opcode;
8485255736Sdavidch    int spqe_cnt = 0;
8486255736Sdavidch    struct ecore_queue_sp_obj *q_obj;
8487255736Sdavidch    struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8488255736Sdavidch    struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8489255736Sdavidch
8490255736Sdavidch    hw_cons = le16toh(*sc->eq_cons_sb);
8491255736Sdavidch
8492255736Sdavidch    /*
8493255736Sdavidch     * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8494255736Sdavidch     * when we get to the next-page we need to adjust so the loop
8495255736Sdavidch     * condition below will be met. The next element is the size of a
8496255736Sdavidch     * regular element and hence incrementing by 1
8497255736Sdavidch     */
8498255736Sdavidch    if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8499255736Sdavidch        hw_cons++;
8500255736Sdavidch    }
8501255736Sdavidch
8502255736Sdavidch    /*
8503255736Sdavidch     * This function may never run in parallel with itself for a
8504255736Sdavidch     * specific sc and no need for a read memory barrier here.
8505255736Sdavidch     */
8506255736Sdavidch    sw_cons = sc->eq_cons;
8507255736Sdavidch    sw_prod = sc->eq_prod;
8508255736Sdavidch
8509255736Sdavidch    BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8510255736Sdavidch          hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8511255736Sdavidch
8512255736Sdavidch    for (;
8513255736Sdavidch         sw_cons != hw_cons;
8514255736Sdavidch         sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8515255736Sdavidch
8516255736Sdavidch        elem = &sc->eq[EQ_DESC(sw_cons)];
8517255736Sdavidch
8518255736Sdavidch        /* elem CID originates from FW, actually LE */
8519255736Sdavidch        cid = SW_CID(elem->message.data.cfc_del_event.cid);
8520255736Sdavidch        opcode = elem->message.opcode;
8521255736Sdavidch
8522255736Sdavidch        /* handle eq element */
8523255736Sdavidch        switch (opcode) {
8524255736Sdavidch
8525255736Sdavidch        case EVENT_RING_OPCODE_STAT_QUERY:
8526255736Sdavidch            BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8527255736Sdavidch                  sc->stats_comp++);
8528255736Sdavidch            /* nothing to do with stats comp */
8529255736Sdavidch            goto next_spqe;
8530255736Sdavidch
8531255736Sdavidch        case EVENT_RING_OPCODE_CFC_DEL:
8532255736Sdavidch            /* handle according to cid range */
8533255736Sdavidch            /* we may want to verify here that the sc state is HALTING */
8534255736Sdavidch            BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8535255736Sdavidch            q_obj = bxe_cid_to_q_obj(sc, cid);
8536255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8537255736Sdavidch                break;
8538255736Sdavidch            }
8539255736Sdavidch            goto next_spqe;
8540255736Sdavidch
8541255736Sdavidch        case EVENT_RING_OPCODE_STOP_TRAFFIC:
8542255736Sdavidch            BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8543255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8544255736Sdavidch                break;
8545255736Sdavidch            }
8546255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8547255736Sdavidch            goto next_spqe;
8548255736Sdavidch
8549255736Sdavidch        case EVENT_RING_OPCODE_START_TRAFFIC:
8550255736Sdavidch            BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8551255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8552255736Sdavidch                break;
8553255736Sdavidch            }
8554255736Sdavidch            // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8555255736Sdavidch            goto next_spqe;
8556255736Sdavidch
8557255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8558255736Sdavidch            echo = elem->message.data.function_update_event.echo;
8559255736Sdavidch            if (echo == SWITCH_UPDATE) {
8560255736Sdavidch                BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8561255736Sdavidch                if (f_obj->complete_cmd(sc, f_obj,
8562255736Sdavidch                                        ECORE_F_CMD_SWITCH_UPDATE)) {
8563255736Sdavidch                    break;
8564255736Sdavidch                }
8565255736Sdavidch            }
8566255736Sdavidch            else {
8567255736Sdavidch                BLOGD(sc, DBG_SP,
8568255736Sdavidch                      "AFEX: ramrod completed FUNCTION_UPDATE\n");
8569255736Sdavidch            }
8570255736Sdavidch            goto next_spqe;
8571255736Sdavidch
8572255736Sdavidch        case EVENT_RING_OPCODE_FORWARD_SETUP:
8573255736Sdavidch            q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8574255736Sdavidch            if (q_obj->complete_cmd(sc, q_obj,
8575255736Sdavidch                                    ECORE_Q_CMD_SETUP_TX_ONLY)) {
8576255736Sdavidch                break;
8577255736Sdavidch            }
8578255736Sdavidch            goto next_spqe;
8579255736Sdavidch
8580255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_START:
8581255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8582255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8583255736Sdavidch                break;
8584255736Sdavidch            }
8585255736Sdavidch            goto next_spqe;
8586255736Sdavidch
8587255736Sdavidch        case EVENT_RING_OPCODE_FUNCTION_STOP:
8588255736Sdavidch            BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8589255736Sdavidch            if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8590255736Sdavidch                break;
8591255736Sdavidch            }
8592255736Sdavidch            goto next_spqe;
8593255736Sdavidch        }
8594255736Sdavidch
8595255736Sdavidch        switch (opcode | sc->state) {
8596255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8597255736Sdavidch        case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8598255736Sdavidch            cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8599255736Sdavidch            BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8600255736Sdavidch            rss_raw->clear_pending(rss_raw);
8601255736Sdavidch            break;
8602255736Sdavidch
8603255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8604255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8605255736Sdavidch        case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8606255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8607255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8608255736Sdavidch        case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8609255736Sdavidch            BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8610255736Sdavidch            bxe_handle_classification_eqe(sc, elem);
8611255736Sdavidch            break;
8612255736Sdavidch
8613255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8614255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8615255736Sdavidch        case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8616255736Sdavidch            BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8617255736Sdavidch            bxe_handle_mcast_eqe(sc);
8618255736Sdavidch            break;
8619255736Sdavidch
8620255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8621255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8622255736Sdavidch        case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8623255736Sdavidch            BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8624255736Sdavidch            bxe_handle_rx_mode_eqe(sc, elem);
8625255736Sdavidch            break;
8626255736Sdavidch
8627255736Sdavidch        default:
8628255736Sdavidch            /* unknown event log error and continue */
8629255736Sdavidch            BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8630255736Sdavidch                  elem->message.opcode, sc->state);
8631255736Sdavidch        }
8632255736Sdavidch
8633255736Sdavidchnext_spqe:
8634255736Sdavidch        spqe_cnt++;
8635255736Sdavidch    } /* for */
8636255736Sdavidch
8637255736Sdavidch    mb();
8638255736Sdavidch    atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8639255736Sdavidch
8640255736Sdavidch    sc->eq_cons = sw_cons;
8641255736Sdavidch    sc->eq_prod = sw_prod;
8642255736Sdavidch
8643255736Sdavidch    /* make sure that above mem writes were issued towards the memory */
8644255736Sdavidch    wmb();
8645255736Sdavidch
8646255736Sdavidch    /* update producer */
8647255736Sdavidch    bxe_update_eq_prod(sc, sc->eq_prod);
8648255736Sdavidch}
8649255736Sdavidch
8650255736Sdavidchstatic void
8651255736Sdavidchbxe_handle_sp_tq(void *context,
8652255736Sdavidch                 int  pending)
8653255736Sdavidch{
8654255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)context;
8655255736Sdavidch    uint16_t status;
8656255736Sdavidch
8657255736Sdavidch    BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8658255736Sdavidch
8659255736Sdavidch    /* what work needs to be performed? */
8660255736Sdavidch    status = bxe_update_dsb_idx(sc);
8661255736Sdavidch
8662255736Sdavidch    BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8663255736Sdavidch
8664255736Sdavidch    /* HW attentions */
8665255736Sdavidch    if (status & BXE_DEF_SB_ATT_IDX) {
8666255736Sdavidch        BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8667255736Sdavidch        bxe_attn_int(sc);
8668255736Sdavidch        status &= ~BXE_DEF_SB_ATT_IDX;
8669255736Sdavidch    }
8670255736Sdavidch
8671255736Sdavidch    /* SP events: STAT_QUERY and others */
8672255736Sdavidch    if (status & BXE_DEF_SB_IDX) {
8673255736Sdavidch        /* handle EQ completions */
8674255736Sdavidch        BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8675255736Sdavidch        bxe_eq_int(sc);
8676255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8677255736Sdavidch                   le16toh(sc->def_idx), IGU_INT_NOP, 1);
8678255736Sdavidch        status &= ~BXE_DEF_SB_IDX;
8679255736Sdavidch    }
8680255736Sdavidch
8681255736Sdavidch    /* if status is non zero then something went wrong */
8682255736Sdavidch    if (__predict_false(status)) {
8683255736Sdavidch        BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8684255736Sdavidch    }
8685255736Sdavidch
8686255736Sdavidch    /* ack status block only if something was actually handled */
8687255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8688255736Sdavidch               le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8689255736Sdavidch
8690255736Sdavidch    /*
8691255736Sdavidch     * Must be called after the EQ processing (since eq leads to sriov
8692255736Sdavidch     * ramrod completion flows).
8693255736Sdavidch     * This flow may have been scheduled by the arrival of a ramrod
8694255736Sdavidch     * completion, or by the sriov code rescheduling itself.
8695255736Sdavidch     */
8696255736Sdavidch    // XXX bxe_iov_sp_task(sc);
8697255736Sdavidch
8698255736Sdavidch}
8699255736Sdavidch
8700255736Sdavidchstatic void
8701255736Sdavidchbxe_handle_fp_tq(void *context,
8702255736Sdavidch                 int  pending)
8703255736Sdavidch{
8704255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8705255736Sdavidch    struct bxe_softc *sc = fp->sc;
8706255736Sdavidch    uint8_t more_tx = FALSE;
8707255736Sdavidch    uint8_t more_rx = FALSE;
8708255736Sdavidch
8709255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8710255736Sdavidch
8711255736Sdavidch    /* XXX
8712255736Sdavidch     * IFF_DRV_RUNNING state can't be checked here since we process
8713255736Sdavidch     * slowpath events on a client queue during setup. Instead
8714255736Sdavidch     * we need to add a "process/continue" flag here that the driver
8715255736Sdavidch     * can use to tell the task here not to do anything.
8716255736Sdavidch     */
8717255736Sdavidch#if 0
8718266979Smarcel    if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
8719255736Sdavidch        return;
8720255736Sdavidch    }
8721255736Sdavidch#endif
8722255736Sdavidch
8723255736Sdavidch    /* update the fastpath index */
8724255736Sdavidch    bxe_update_fp_sb_idx(fp);
8725255736Sdavidch
8726255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8727255736Sdavidch    /* fp->txdata[cos] */
8728255736Sdavidch    if (bxe_has_tx_work(fp)) {
8729255736Sdavidch        BXE_FP_TX_LOCK(fp);
8730255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8731255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8732255736Sdavidch    }
8733255736Sdavidch
8734255736Sdavidch    if (bxe_has_rx_work(fp)) {
8735255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8736255736Sdavidch    }
8737255736Sdavidch
8738255736Sdavidch    if (more_rx /*|| more_tx*/) {
8739255736Sdavidch        /* still more work to do */
8740255736Sdavidch        taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
8741255736Sdavidch        return;
8742255736Sdavidch    }
8743255736Sdavidch
8744255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8745255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8746255736Sdavidch}
8747255736Sdavidch
8748255736Sdavidchstatic void
8749255736Sdavidchbxe_task_fp(struct bxe_fastpath *fp)
8750255736Sdavidch{
8751255736Sdavidch    struct bxe_softc *sc = fp->sc;
8752255736Sdavidch    uint8_t more_tx = FALSE;
8753255736Sdavidch    uint8_t more_rx = FALSE;
8754255736Sdavidch
8755255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8756255736Sdavidch
8757255736Sdavidch    /* update the fastpath index */
8758255736Sdavidch    bxe_update_fp_sb_idx(fp);
8759255736Sdavidch
8760255736Sdavidch    /* XXX add loop here if ever support multiple tx CoS */
8761255736Sdavidch    /* fp->txdata[cos] */
8762255736Sdavidch    if (bxe_has_tx_work(fp)) {
8763255736Sdavidch        BXE_FP_TX_LOCK(fp);
8764255736Sdavidch        more_tx = bxe_txeof(sc, fp);
8765255736Sdavidch        BXE_FP_TX_UNLOCK(fp);
8766255736Sdavidch    }
8767255736Sdavidch
8768255736Sdavidch    if (bxe_has_rx_work(fp)) {
8769255736Sdavidch        more_rx = bxe_rxeof(sc, fp);
8770255736Sdavidch    }
8771255736Sdavidch
8772255736Sdavidch    if (more_rx /*|| more_tx*/) {
8773255736Sdavidch        /* still more work to do, bail out if this ISR and process later */
8774255736Sdavidch        taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
8775255736Sdavidch        return;
8776255736Sdavidch    }
8777255736Sdavidch
8778255736Sdavidch    /*
8779255736Sdavidch     * Here we write the fastpath index taken before doing any tx or rx work.
8780255736Sdavidch     * It is very well possible other hw events occurred up to this point and
8781255736Sdavidch     * they were actually processed accordingly above. Since we're going to
8782255736Sdavidch     * write an older fastpath index, an interrupt is coming which we might
8783255736Sdavidch     * not do any work in.
8784255736Sdavidch     */
8785255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8786255736Sdavidch               le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8787255736Sdavidch}
8788255736Sdavidch
8789255736Sdavidch/*
8790255736Sdavidch * Legacy interrupt entry point.
8791255736Sdavidch *
8792255736Sdavidch * Verifies that the controller generated the interrupt and
8793255736Sdavidch * then calls a separate routine to handle the various
8794255736Sdavidch * interrupt causes: link, RX, and TX.
8795255736Sdavidch */
8796255736Sdavidchstatic void
8797255736Sdavidchbxe_intr_legacy(void *xsc)
8798255736Sdavidch{
8799255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8800255736Sdavidch    struct bxe_fastpath *fp;
8801255736Sdavidch    uint16_t status, mask;
8802255736Sdavidch    int i;
8803255736Sdavidch
8804255736Sdavidch    BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8805255736Sdavidch
8806255736Sdavidch    /*
8807255736Sdavidch     * 0 for ustorm, 1 for cstorm
8808255736Sdavidch     * the bits returned from ack_int() are 0-15
8809255736Sdavidch     * bit 0 = attention status block
8810255736Sdavidch     * bit 1 = fast path status block
8811255736Sdavidch     * a mask of 0x2 or more = tx/rx event
8812255736Sdavidch     * a mask of 1 = slow path event
8813255736Sdavidch     */
8814255736Sdavidch
8815255736Sdavidch    status = bxe_ack_int(sc);
8816255736Sdavidch
8817255736Sdavidch    /* the interrupt is not for us */
8818255736Sdavidch    if (__predict_false(status == 0)) {
8819255736Sdavidch        BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8820255736Sdavidch        return;
8821255736Sdavidch    }
8822255736Sdavidch
8823255736Sdavidch    BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8824255736Sdavidch
8825255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
8826255736Sdavidch        fp = &sc->fp[i];
8827255736Sdavidch        mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8828255736Sdavidch        if (status & mask) {
8829255736Sdavidch            /* acknowledge and disable further fastpath interrupts */
8830255736Sdavidch            bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8831255736Sdavidch            bxe_task_fp(fp);
8832255736Sdavidch            status &= ~mask;
8833255736Sdavidch        }
8834255736Sdavidch    }
8835255736Sdavidch
8836255736Sdavidch    if (__predict_false(status & 0x1)) {
8837255736Sdavidch        /* acknowledge and disable further slowpath interrupts */
8838255736Sdavidch        bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8839255736Sdavidch
8840255736Sdavidch        /* schedule slowpath handler */
8841255736Sdavidch        taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
8842255736Sdavidch
8843255736Sdavidch        status &= ~0x1;
8844255736Sdavidch    }
8845255736Sdavidch
8846255736Sdavidch    if (__predict_false(status)) {
8847255736Sdavidch        BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8848255736Sdavidch    }
8849255736Sdavidch}
8850255736Sdavidch
8851255736Sdavidch/* slowpath interrupt entry point */
8852255736Sdavidchstatic void
8853255736Sdavidchbxe_intr_sp(void *xsc)
8854255736Sdavidch{
8855255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
8856255736Sdavidch
8857255736Sdavidch    BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8858255736Sdavidch
8859255736Sdavidch    /* acknowledge and disable further slowpath interrupts */
8860255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8861255736Sdavidch
8862255736Sdavidch    /* schedule slowpath handler */
8863255736Sdavidch    taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
8864255736Sdavidch}
8865255736Sdavidch
8866255736Sdavidch/* fastpath interrupt entry point */
8867255736Sdavidchstatic void
8868255736Sdavidchbxe_intr_fp(void *xfp)
8869255736Sdavidch{
8870255736Sdavidch    struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8871255736Sdavidch    struct bxe_softc *sc = fp->sc;
8872255736Sdavidch
8873255736Sdavidch    BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8874255736Sdavidch
8875255736Sdavidch    BLOGD(sc, DBG_INTR,
8876255736Sdavidch          "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8877255736Sdavidch          curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8878255736Sdavidch
8879255736Sdavidch    /* acknowledge and disable further fastpath interrupts */
8880255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8881255736Sdavidch
8882255736Sdavidch    bxe_task_fp(fp);
8883255736Sdavidch}
8884255736Sdavidch
8885255736Sdavidch/* Release all interrupts allocated by the driver. */
8886255736Sdavidchstatic void
8887255736Sdavidchbxe_interrupt_free(struct bxe_softc *sc)
8888255736Sdavidch{
8889255736Sdavidch    int i;
8890255736Sdavidch
8891255736Sdavidch    switch (sc->interrupt_mode) {
8892255736Sdavidch    case INTR_MODE_INTX:
8893255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8894255736Sdavidch        if (sc->intr[0].resource != NULL) {
8895255736Sdavidch            bus_release_resource(sc->dev,
8896255736Sdavidch                                 SYS_RES_IRQ,
8897255736Sdavidch                                 sc->intr[0].rid,
8898255736Sdavidch                                 sc->intr[0].resource);
8899255736Sdavidch        }
8900255736Sdavidch        break;
8901255736Sdavidch    case INTR_MODE_MSI:
8902255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8903255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
8904255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8905255736Sdavidch                bus_release_resource(sc->dev,
8906255736Sdavidch                                     SYS_RES_IRQ,
8907255736Sdavidch                                     sc->intr[i].rid,
8908255736Sdavidch                                     sc->intr[i].resource);
8909255736Sdavidch            }
8910255736Sdavidch        }
8911255736Sdavidch        pci_release_msi(sc->dev);
8912255736Sdavidch        break;
8913255736Sdavidch    case INTR_MODE_MSIX:
8914255736Sdavidch        for (i = 0; i < sc->intr_count; i++) {
8915255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8916255736Sdavidch            if (sc->intr[i].resource && sc->intr[i].rid) {
8917255736Sdavidch                bus_release_resource(sc->dev,
8918255736Sdavidch                                     SYS_RES_IRQ,
8919255736Sdavidch                                     sc->intr[i].rid,
8920255736Sdavidch                                     sc->intr[i].resource);
8921255736Sdavidch            }
8922255736Sdavidch        }
8923255736Sdavidch        pci_release_msi(sc->dev);
8924255736Sdavidch        break;
8925255736Sdavidch    default:
8926255736Sdavidch        /* nothing to do as initial allocation failed */
8927255736Sdavidch        break;
8928255736Sdavidch    }
8929255736Sdavidch}
8930255736Sdavidch
8931255736Sdavidch/*
8932255736Sdavidch * This function determines and allocates the appropriate
8933255736Sdavidch * interrupt based on system capabilites and user request.
8934255736Sdavidch *
8935255736Sdavidch * The user may force a particular interrupt mode, specify
8936255736Sdavidch * the number of receive queues, specify the method for
8937255736Sdavidch * distribuitng received frames to receive queues, or use
8938255736Sdavidch * the default settings which will automatically select the
8939255736Sdavidch * best supported combination.  In addition, the OS may or
8940255736Sdavidch * may not support certain combinations of these settings.
8941255736Sdavidch * This routine attempts to reconcile the settings requested
8942255736Sdavidch * by the user with the capabilites available from the system
8943255736Sdavidch * to select the optimal combination of features.
8944255736Sdavidch *
8945255736Sdavidch * Returns:
8946255736Sdavidch *   0 = Success, !0 = Failure.
8947255736Sdavidch */
8948255736Sdavidchstatic int
8949255736Sdavidchbxe_interrupt_alloc(struct bxe_softc *sc)
8950255736Sdavidch{
8951255736Sdavidch    int msix_count = 0;
8952255736Sdavidch    int msi_count = 0;
8953255736Sdavidch    int num_requested = 0;
8954255736Sdavidch    int num_allocated = 0;
8955255736Sdavidch    int rid, i, j;
8956255736Sdavidch    int rc;
8957255736Sdavidch
8958255736Sdavidch    /* get the number of available MSI/MSI-X interrupts from the OS */
8959255736Sdavidch    if (sc->interrupt_mode > 0) {
8960255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8961255736Sdavidch            msix_count = pci_msix_count(sc->dev);
8962255736Sdavidch        }
8963255736Sdavidch
8964255736Sdavidch        if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
8965255736Sdavidch            msi_count = pci_msi_count(sc->dev);
8966255736Sdavidch        }
8967255736Sdavidch
8968255736Sdavidch        BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
8969255736Sdavidch              msi_count, msix_count);
8970255736Sdavidch    }
8971255736Sdavidch
8972255736Sdavidch    do { /* try allocating MSI-X interrupt resources (at least 2) */
8973255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
8974255736Sdavidch            break;
8975255736Sdavidch        }
8976255736Sdavidch
8977255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
8978255736Sdavidch            (msix_count < 2)) {
8979255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8980255736Sdavidch            break;
8981255736Sdavidch        }
8982255736Sdavidch
8983255736Sdavidch        /* ask for the necessary number of MSI-X vectors */
8984255736Sdavidch        num_requested = min((sc->num_queues + 1), msix_count);
8985255736Sdavidch
8986255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
8987255736Sdavidch
8988255736Sdavidch        num_allocated = num_requested;
8989255736Sdavidch        if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
8990255736Sdavidch            BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
8991255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8992255736Sdavidch            break;
8993255736Sdavidch        }
8994255736Sdavidch
8995255736Sdavidch        if (num_allocated < 2) { /* possible? */
8996255736Sdavidch            BLOGE(sc, "MSI-X allocation less than 2!\n");
8997255736Sdavidch            sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8998255736Sdavidch            pci_release_msi(sc->dev);
8999255736Sdavidch            break;
9000255736Sdavidch        }
9001255736Sdavidch
9002255736Sdavidch        BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
9003255736Sdavidch              num_requested, num_allocated);
9004255736Sdavidch
9005255736Sdavidch        /* best effort so use the number of vectors allocated to us */
9006255736Sdavidch        sc->intr_count = num_allocated;
9007255736Sdavidch        sc->num_queues = num_allocated - 1;
9008255736Sdavidch
9009255736Sdavidch        rid = 1; /* initial resource identifier */
9010255736Sdavidch
9011255736Sdavidch        /* allocate the MSI-X vectors */
9012255736Sdavidch        for (i = 0; i < num_allocated; i++) {
9013255736Sdavidch            sc->intr[i].rid = (rid + i);
9014255736Sdavidch
9015255736Sdavidch            if ((sc->intr[i].resource =
9016255736Sdavidch                 bus_alloc_resource_any(sc->dev,
9017255736Sdavidch                                        SYS_RES_IRQ,
9018255736Sdavidch                                        &sc->intr[i].rid,
9019255736Sdavidch                                        RF_ACTIVE)) == NULL) {
9020255736Sdavidch                BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
9021255736Sdavidch                      i, (rid + i));
9022255736Sdavidch
9023255736Sdavidch                for (j = (i - 1); j >= 0; j--) {
9024255736Sdavidch                    bus_release_resource(sc->dev,
9025255736Sdavidch                                         SYS_RES_IRQ,
9026255736Sdavidch                                         sc->intr[j].rid,
9027255736Sdavidch                                         sc->intr[j].resource);
9028255736Sdavidch                }
9029255736Sdavidch
9030255736Sdavidch                sc->intr_count = 0;
9031255736Sdavidch                sc->num_queues = 0;
9032255736Sdavidch                sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9033255736Sdavidch                pci_release_msi(sc->dev);
9034255736Sdavidch                break;
9035255736Sdavidch            }
9036255736Sdavidch
9037255736Sdavidch            BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9038255736Sdavidch        }
9039255736Sdavidch    } while (0);
9040255736Sdavidch
9041255736Sdavidch    do { /* try allocating MSI vector resources (at least 2) */
9042255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSI) {
9043255736Sdavidch            break;
9044255736Sdavidch        }
9045255736Sdavidch
9046255736Sdavidch        if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9047262999Sedavis            (msi_count < 1)) {
9048255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9049255736Sdavidch            break;
9050255736Sdavidch        }
9051255736Sdavidch
9052262999Sedavis        /* ask for a single MSI vector */
9053262999Sedavis        num_requested = 1;
9054255736Sdavidch
9055255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9056255736Sdavidch
9057255736Sdavidch        num_allocated = num_requested;
9058255736Sdavidch        if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9059255736Sdavidch            BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9060255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9061255736Sdavidch            break;
9062255736Sdavidch        }
9063255736Sdavidch
9064262999Sedavis        if (num_allocated != 1) { /* possible? */
9065262999Sedavis            BLOGE(sc, "MSI allocation is not 1!\n");
9066255736Sdavidch            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9067255736Sdavidch            pci_release_msi(sc->dev);
9068255736Sdavidch            break;
9069255736Sdavidch        }
9070255736Sdavidch
9071255736Sdavidch        BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9072255736Sdavidch              num_requested, num_allocated);
9073255736Sdavidch
9074255736Sdavidch        /* best effort so use the number of vectors allocated to us */
9075255736Sdavidch        sc->intr_count = num_allocated;
9076262999Sedavis        sc->num_queues = num_allocated;
9077255736Sdavidch
9078255736Sdavidch        rid = 1; /* initial resource identifier */
9079255736Sdavidch
9080262999Sedavis        sc->intr[0].rid = rid;
9081255736Sdavidch
9082262999Sedavis        if ((sc->intr[0].resource =
9083262999Sedavis             bus_alloc_resource_any(sc->dev,
9084262999Sedavis                                    SYS_RES_IRQ,
9085262999Sedavis                                    &sc->intr[0].rid,
9086262999Sedavis                                    RF_ACTIVE)) == NULL) {
9087262999Sedavis            BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9088262999Sedavis            sc->intr_count = 0;
9089262999Sedavis            sc->num_queues = 0;
9090262999Sedavis            sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9091262999Sedavis            pci_release_msi(sc->dev);
9092262999Sedavis            break;
9093262999Sedavis        }
9094255736Sdavidch
9095262999Sedavis        BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9096255736Sdavidch    } while (0);
9097255736Sdavidch
9098255736Sdavidch    do { /* try allocating INTx vector resources */
9099255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_INTX) {
9100255736Sdavidch            break;
9101255736Sdavidch        }
9102255736Sdavidch
9103255736Sdavidch        BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9104255736Sdavidch
9105255736Sdavidch        /* only one vector for INTx */
9106255736Sdavidch        sc->intr_count = 1;
9107255736Sdavidch        sc->num_queues = 1;
9108255736Sdavidch
9109255736Sdavidch        rid = 0; /* initial resource identifier */
9110255736Sdavidch
9111255736Sdavidch        sc->intr[0].rid = rid;
9112255736Sdavidch
9113255736Sdavidch        if ((sc->intr[0].resource =
9114255736Sdavidch             bus_alloc_resource_any(sc->dev,
9115255736Sdavidch                                    SYS_RES_IRQ,
9116255736Sdavidch                                    &sc->intr[0].rid,
9117255736Sdavidch                                    (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9118255736Sdavidch            BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9119255736Sdavidch            sc->intr_count = 0;
9120255736Sdavidch            sc->num_queues = 0;
9121255736Sdavidch            sc->interrupt_mode = -1; /* Failed! */
9122255736Sdavidch            break;
9123255736Sdavidch        }
9124255736Sdavidch
9125255736Sdavidch        BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9126255736Sdavidch    } while (0);
9127255736Sdavidch
9128255736Sdavidch    if (sc->interrupt_mode == -1) {
9129255736Sdavidch        BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9130255736Sdavidch        rc = 1;
9131255736Sdavidch    } else {
9132255736Sdavidch        BLOGD(sc, DBG_LOAD,
9133255736Sdavidch              "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9134255736Sdavidch              sc->interrupt_mode, sc->num_queues);
9135255736Sdavidch        rc = 0;
9136255736Sdavidch    }
9137255736Sdavidch
9138255736Sdavidch    return (rc);
9139255736Sdavidch}
9140255736Sdavidch
9141255736Sdavidchstatic void
9142255736Sdavidchbxe_interrupt_detach(struct bxe_softc *sc)
9143255736Sdavidch{
9144255736Sdavidch    struct bxe_fastpath *fp;
9145255736Sdavidch    int i;
9146255736Sdavidch
9147255736Sdavidch    /* release interrupt resources */
9148255736Sdavidch    for (i = 0; i < sc->intr_count; i++) {
9149255736Sdavidch        if (sc->intr[i].resource && sc->intr[i].tag) {
9150255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9151255736Sdavidch            bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9152255736Sdavidch        }
9153255736Sdavidch    }
9154255736Sdavidch
9155255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9156255736Sdavidch        fp = &sc->fp[i];
9157255736Sdavidch        if (fp->tq) {
9158255736Sdavidch            taskqueue_drain(fp->tq, &fp->tq_task);
9159255736Sdavidch            taskqueue_free(fp->tq);
9160255736Sdavidch            fp->tq = NULL;
9161255736Sdavidch        }
9162255736Sdavidch    }
9163255736Sdavidch
9164255736Sdavidch
9165255736Sdavidch    if (sc->sp_tq) {
9166255736Sdavidch        taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9167255736Sdavidch        taskqueue_free(sc->sp_tq);
9168255736Sdavidch        sc->sp_tq = NULL;
9169255736Sdavidch    }
9170255736Sdavidch}
9171255736Sdavidch
9172255736Sdavidch/*
9173255736Sdavidch * Enables interrupts and attach to the ISR.
9174255736Sdavidch *
9175255736Sdavidch * When using multiple MSI/MSI-X vectors the first vector
9176255736Sdavidch * is used for slowpath operations while all remaining
9177255736Sdavidch * vectors are used for fastpath operations.  If only a
9178255736Sdavidch * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9179255736Sdavidch * ISR must look for both slowpath and fastpath completions.
9180255736Sdavidch */
9181255736Sdavidchstatic int
9182255736Sdavidchbxe_interrupt_attach(struct bxe_softc *sc)
9183255736Sdavidch{
9184255736Sdavidch    struct bxe_fastpath *fp;
9185255736Sdavidch    int rc = 0;
9186255736Sdavidch    int i;
9187255736Sdavidch
9188255736Sdavidch    snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9189255736Sdavidch             "bxe%d_sp_tq", sc->unit);
9190255736Sdavidch    TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9191255736Sdavidch    sc->sp_tq = taskqueue_create_fast(sc->sp_tq_name, M_NOWAIT,
9192255736Sdavidch                                      taskqueue_thread_enqueue,
9193255736Sdavidch                                      &sc->sp_tq);
9194255736Sdavidch    taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9195255736Sdavidch                            "%s", sc->sp_tq_name);
9196255736Sdavidch
9197255736Sdavidch
9198255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9199255736Sdavidch        fp = &sc->fp[i];
9200255736Sdavidch        snprintf(fp->tq_name, sizeof(fp->tq_name),
9201255736Sdavidch                 "bxe%d_fp%d_tq", sc->unit, i);
9202255736Sdavidch        TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9203255736Sdavidch        fp->tq = taskqueue_create_fast(fp->tq_name, M_NOWAIT,
9204255736Sdavidch                                       taskqueue_thread_enqueue,
9205255736Sdavidch                                       &fp->tq);
9206255736Sdavidch        taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9207255736Sdavidch                                "%s", fp->tq_name);
9208255736Sdavidch    }
9209255736Sdavidch
9210255736Sdavidch    /* setup interrupt handlers */
9211255736Sdavidch    if (sc->interrupt_mode == INTR_MODE_MSIX) {
9212255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9213255736Sdavidch
9214255736Sdavidch        /*
9215255736Sdavidch         * Setup the interrupt handler. Note that we pass the driver instance
9216255736Sdavidch         * to the interrupt handler for the slowpath.
9217255736Sdavidch         */
9218255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9219255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9220255736Sdavidch                                 NULL, bxe_intr_sp, sc,
9221255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9222255736Sdavidch            BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9223255736Sdavidch            goto bxe_interrupt_attach_exit;
9224255736Sdavidch        }
9225255736Sdavidch
9226255736Sdavidch        bus_describe_intr(sc->dev, sc->intr[0].resource,
9227255736Sdavidch                          sc->intr[0].tag, "sp");
9228255736Sdavidch
9229255736Sdavidch        /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9230255736Sdavidch
9231255736Sdavidch        /* initialize the fastpath vectors (note the first was used for sp) */
9232255736Sdavidch        for (i = 0; i < sc->num_queues; i++) {
9233255736Sdavidch            fp = &sc->fp[i];
9234255736Sdavidch            BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9235255736Sdavidch
9236255736Sdavidch            /*
9237255736Sdavidch             * Setup the interrupt handler. Note that we pass the
9238255736Sdavidch             * fastpath context to the interrupt handler in this
9239255736Sdavidch             * case.
9240255736Sdavidch             */
9241255736Sdavidch            if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9242255736Sdavidch                                     (INTR_TYPE_NET | INTR_MPSAFE),
9243255736Sdavidch                                     NULL, bxe_intr_fp, fp,
9244255736Sdavidch                                     &sc->intr[i + 1].tag)) != 0) {
9245255736Sdavidch                BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9246255736Sdavidch                      (i + 1), rc);
9247255736Sdavidch                goto bxe_interrupt_attach_exit;
9248255736Sdavidch            }
9249255736Sdavidch
9250255736Sdavidch            bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9251255736Sdavidch                              sc->intr[i + 1].tag, "fp%02d", i);
9252255736Sdavidch
9253255736Sdavidch            /* bind the fastpath instance to a cpu */
9254255736Sdavidch            if (sc->num_queues > 1) {
9255255736Sdavidch                bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9256255736Sdavidch            }
9257255736Sdavidch
9258255736Sdavidch            fp->state = BXE_FP_STATE_IRQ;
9259255736Sdavidch        }
9260255736Sdavidch    } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9261262999Sedavis        BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9262255736Sdavidch
9263255736Sdavidch        /*
9264262999Sedavis         * Setup the interrupt handler. Note that we pass the
9265262999Sedavis         * driver instance to the interrupt handler which
9266262999Sedavis         * will handle both the slowpath and fastpath.
9267255736Sdavidch         */
9268255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9269255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9270262999Sedavis                                 NULL, bxe_intr_legacy, sc,
9271255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9272255736Sdavidch            BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9273255736Sdavidch            goto bxe_interrupt_attach_exit;
9274255736Sdavidch        }
9275255736Sdavidch
9276255736Sdavidch    } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9277255736Sdavidch        BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9278255736Sdavidch
9279255736Sdavidch        /*
9280255736Sdavidch         * Setup the interrupt handler. Note that we pass the
9281255736Sdavidch         * driver instance to the interrupt handler which
9282255736Sdavidch         * will handle both the slowpath and fastpath.
9283255736Sdavidch         */
9284255736Sdavidch        if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9285255736Sdavidch                                 (INTR_TYPE_NET | INTR_MPSAFE),
9286255736Sdavidch                                 NULL, bxe_intr_legacy, sc,
9287255736Sdavidch                                 &sc->intr[0].tag)) != 0) {
9288255736Sdavidch            BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9289255736Sdavidch            goto bxe_interrupt_attach_exit;
9290255736Sdavidch        }
9291255736Sdavidch    }
9292255736Sdavidch
9293255736Sdavidchbxe_interrupt_attach_exit:
9294255736Sdavidch
9295255736Sdavidch    return (rc);
9296255736Sdavidch}
9297255736Sdavidch
9298255736Sdavidchstatic int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9299255736Sdavidchstatic int  bxe_init_hw_common(struct bxe_softc *sc);
9300255736Sdavidchstatic int  bxe_init_hw_port(struct bxe_softc *sc);
9301255736Sdavidchstatic int  bxe_init_hw_func(struct bxe_softc *sc);
9302255736Sdavidchstatic void bxe_reset_common(struct bxe_softc *sc);
9303255736Sdavidchstatic void bxe_reset_port(struct bxe_softc *sc);
9304255736Sdavidchstatic void bxe_reset_func(struct bxe_softc *sc);
9305255736Sdavidchstatic int  bxe_gunzip_init(struct bxe_softc *sc);
9306255736Sdavidchstatic void bxe_gunzip_end(struct bxe_softc *sc);
9307255736Sdavidchstatic int  bxe_init_firmware(struct bxe_softc *sc);
9308255736Sdavidchstatic void bxe_release_firmware(struct bxe_softc *sc);
9309255736Sdavidch
9310255736Sdavidchstatic struct
9311255736Sdavidchecore_func_sp_drv_ops bxe_func_sp_drv = {
9312255736Sdavidch    .init_hw_cmn_chip = bxe_init_hw_common_chip,
9313255736Sdavidch    .init_hw_cmn      = bxe_init_hw_common,
9314255736Sdavidch    .init_hw_port     = bxe_init_hw_port,
9315255736Sdavidch    .init_hw_func     = bxe_init_hw_func,
9316255736Sdavidch
9317255736Sdavidch    .reset_hw_cmn     = bxe_reset_common,
9318255736Sdavidch    .reset_hw_port    = bxe_reset_port,
9319255736Sdavidch    .reset_hw_func    = bxe_reset_func,
9320255736Sdavidch
9321255736Sdavidch    .gunzip_init      = bxe_gunzip_init,
9322255736Sdavidch    .gunzip_end       = bxe_gunzip_end,
9323255736Sdavidch
9324255736Sdavidch    .init_fw          = bxe_init_firmware,
9325255736Sdavidch    .release_fw       = bxe_release_firmware,
9326255736Sdavidch};
9327255736Sdavidch
9328255736Sdavidchstatic void
9329255736Sdavidchbxe_init_func_obj(struct bxe_softc *sc)
9330255736Sdavidch{
9331255736Sdavidch    sc->dmae_ready = 0;
9332255736Sdavidch
9333255736Sdavidch    ecore_init_func_obj(sc,
9334255736Sdavidch                        &sc->func_obj,
9335255736Sdavidch                        BXE_SP(sc, func_rdata),
9336255736Sdavidch                        BXE_SP_MAPPING(sc, func_rdata),
9337255736Sdavidch                        BXE_SP(sc, func_afex_rdata),
9338255736Sdavidch                        BXE_SP_MAPPING(sc, func_afex_rdata),
9339255736Sdavidch                        &bxe_func_sp_drv);
9340255736Sdavidch}
9341255736Sdavidch
9342255736Sdavidchstatic int
9343255736Sdavidchbxe_init_hw(struct bxe_softc *sc,
9344255736Sdavidch            uint32_t         load_code)
9345255736Sdavidch{
9346255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
9347255736Sdavidch    int rc;
9348255736Sdavidch
9349255736Sdavidch    /* prepare the parameters for function state transitions */
9350255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9351255736Sdavidch
9352255736Sdavidch    func_params.f_obj = &sc->func_obj;
9353255736Sdavidch    func_params.cmd = ECORE_F_CMD_HW_INIT;
9354255736Sdavidch
9355255736Sdavidch    func_params.params.hw_init.load_phase = load_code;
9356255736Sdavidch
9357255736Sdavidch    /*
9358255736Sdavidch     * Via a plethora of function pointers, we will eventually reach
9359255736Sdavidch     * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9360255736Sdavidch     */
9361255736Sdavidch    rc = ecore_func_state_change(sc, &func_params);
9362255736Sdavidch
9363255736Sdavidch    return (rc);
9364255736Sdavidch}
9365255736Sdavidch
9366255736Sdavidchstatic void
9367255736Sdavidchbxe_fill(struct bxe_softc *sc,
9368255736Sdavidch         uint32_t         addr,
9369255736Sdavidch         int              fill,
9370255736Sdavidch         uint32_t         len)
9371255736Sdavidch{
9372255736Sdavidch    uint32_t i;
9373255736Sdavidch
9374255736Sdavidch    if (!(len % 4) && !(addr % 4)) {
9375255736Sdavidch        for (i = 0; i < len; i += 4) {
9376255736Sdavidch            REG_WR(sc, (addr + i), fill);
9377255736Sdavidch        }
9378255736Sdavidch    } else {
9379255736Sdavidch        for (i = 0; i < len; i++) {
9380255736Sdavidch            REG_WR8(sc, (addr + i), fill);
9381255736Sdavidch        }
9382255736Sdavidch    }
9383255736Sdavidch}
9384255736Sdavidch
9385255736Sdavidch/* writes FP SP data to FW - data_size in dwords */
9386255736Sdavidchstatic void
9387255736Sdavidchbxe_wr_fp_sb_data(struct bxe_softc *sc,
9388255736Sdavidch                  int              fw_sb_id,
9389255736Sdavidch                  uint32_t         *sb_data_p,
9390255736Sdavidch                  uint32_t         data_size)
9391255736Sdavidch{
9392255736Sdavidch    int index;
9393255736Sdavidch
9394255736Sdavidch    for (index = 0; index < data_size; index++) {
9395255736Sdavidch        REG_WR(sc,
9396255736Sdavidch               (BAR_CSTRORM_INTMEM +
9397255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9398255736Sdavidch                (sizeof(uint32_t) * index)),
9399255736Sdavidch               *(sb_data_p + index));
9400255736Sdavidch    }
9401255736Sdavidch}
9402255736Sdavidch
9403255736Sdavidchstatic void
9404255736Sdavidchbxe_zero_fp_sb(struct bxe_softc *sc,
9405255736Sdavidch               int              fw_sb_id)
9406255736Sdavidch{
9407255736Sdavidch    struct hc_status_block_data_e2 sb_data_e2;
9408255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9409255736Sdavidch    uint32_t *sb_data_p;
9410255736Sdavidch    uint32_t data_size = 0;
9411255736Sdavidch
9412255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9413255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9414255736Sdavidch        sb_data_e2.common.state = SB_DISABLED;
9415255736Sdavidch        sb_data_e2.common.p_func.vf_valid = FALSE;
9416255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9417255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9418255736Sdavidch                     sizeof(uint32_t));
9419255736Sdavidch    } else {
9420255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9421255736Sdavidch        sb_data_e1x.common.state = SB_DISABLED;
9422255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9423255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9424255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9425255736Sdavidch                     sizeof(uint32_t));
9426255736Sdavidch    }
9427255736Sdavidch
9428255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9429255736Sdavidch
9430255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9431255736Sdavidch             0, CSTORM_STATUS_BLOCK_SIZE);
9432255736Sdavidch    bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9433255736Sdavidch             0, CSTORM_SYNC_BLOCK_SIZE);
9434255736Sdavidch}
9435255736Sdavidch
9436255736Sdavidchstatic void
9437255736Sdavidchbxe_wr_sp_sb_data(struct bxe_softc               *sc,
9438255736Sdavidch                  struct hc_sp_status_block_data *sp_sb_data)
9439255736Sdavidch{
9440255736Sdavidch    int i;
9441255736Sdavidch
9442255736Sdavidch    for (i = 0;
9443255736Sdavidch         i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9444255736Sdavidch         i++) {
9445255736Sdavidch        REG_WR(sc,
9446255736Sdavidch               (BAR_CSTRORM_INTMEM +
9447255736Sdavidch                CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9448255736Sdavidch                (i * sizeof(uint32_t))),
9449255736Sdavidch               *((uint32_t *)sp_sb_data + i));
9450255736Sdavidch    }
9451255736Sdavidch}
9452255736Sdavidch
9453255736Sdavidchstatic void
9454255736Sdavidchbxe_zero_sp_sb(struct bxe_softc *sc)
9455255736Sdavidch{
9456255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9457255736Sdavidch
9458255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9459255736Sdavidch
9460255736Sdavidch    sp_sb_data.state           = SB_DISABLED;
9461255736Sdavidch    sp_sb_data.p_func.vf_valid = FALSE;
9462255736Sdavidch
9463255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9464255736Sdavidch
9465255736Sdavidch    bxe_fill(sc,
9466255736Sdavidch             (BAR_CSTRORM_INTMEM +
9467255736Sdavidch              CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9468255736Sdavidch              0, CSTORM_SP_STATUS_BLOCK_SIZE);
9469255736Sdavidch    bxe_fill(sc,
9470255736Sdavidch             (BAR_CSTRORM_INTMEM +
9471255736Sdavidch              CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9472255736Sdavidch              0, CSTORM_SP_SYNC_BLOCK_SIZE);
9473255736Sdavidch}
9474255736Sdavidch
9475255736Sdavidchstatic void
9476255736Sdavidchbxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9477255736Sdavidch                             int                       igu_sb_id,
9478255736Sdavidch                             int                       igu_seg_id)
9479255736Sdavidch{
9480255736Sdavidch    hc_sm->igu_sb_id      = igu_sb_id;
9481255736Sdavidch    hc_sm->igu_seg_id     = igu_seg_id;
9482255736Sdavidch    hc_sm->timer_value    = 0xFF;
9483255736Sdavidch    hc_sm->time_to_expire = 0xFFFFFFFF;
9484255736Sdavidch}
9485255736Sdavidch
9486255736Sdavidchstatic void
9487255736Sdavidchbxe_map_sb_state_machines(struct hc_index_data *index_data)
9488255736Sdavidch{
9489255736Sdavidch    /* zero out state machine indices */
9490255736Sdavidch
9491255736Sdavidch    /* rx indices */
9492255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9493255736Sdavidch
9494255736Sdavidch    /* tx indices */
9495255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9496255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9497255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9498255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9499255736Sdavidch
9500255736Sdavidch    /* map indices */
9501255736Sdavidch
9502255736Sdavidch    /* rx indices */
9503255736Sdavidch    index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9504255736Sdavidch        (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9505255736Sdavidch
9506255736Sdavidch    /* tx indices */
9507255736Sdavidch    index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9508255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9509255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9510255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9511255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9512255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9513255736Sdavidch    index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9514255736Sdavidch        (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9515255736Sdavidch}
9516255736Sdavidch
9517255736Sdavidchstatic void
9518255736Sdavidchbxe_init_sb(struct bxe_softc *sc,
9519255736Sdavidch            bus_addr_t       busaddr,
9520255736Sdavidch            int              vfid,
9521255736Sdavidch            uint8_t          vf_valid,
9522255736Sdavidch            int              fw_sb_id,
9523255736Sdavidch            int              igu_sb_id)
9524255736Sdavidch{
9525255736Sdavidch    struct hc_status_block_data_e2  sb_data_e2;
9526255736Sdavidch    struct hc_status_block_data_e1x sb_data_e1x;
9527255736Sdavidch    struct hc_status_block_sm       *hc_sm_p;
9528255736Sdavidch    uint32_t *sb_data_p;
9529255736Sdavidch    int igu_seg_id;
9530255736Sdavidch    int data_size;
9531255736Sdavidch
9532255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9533255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_NORM;
9534255736Sdavidch    } else {
9535255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_NORM;
9536255736Sdavidch    }
9537255736Sdavidch
9538255736Sdavidch    bxe_zero_fp_sb(sc, fw_sb_id);
9539255736Sdavidch
9540255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9541255736Sdavidch        memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9542255736Sdavidch        sb_data_e2.common.state = SB_ENABLED;
9543255736Sdavidch        sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9544255736Sdavidch        sb_data_e2.common.p_func.vf_id = vfid;
9545255736Sdavidch        sb_data_e2.common.p_func.vf_valid = vf_valid;
9546255736Sdavidch        sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9547255736Sdavidch        sb_data_e2.common.same_igu_sb_1b = TRUE;
9548255736Sdavidch        sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9549255736Sdavidch        sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9550255736Sdavidch        hc_sm_p = sb_data_e2.common.state_machine;
9551255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e2;
9552255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e2) /
9553255736Sdavidch                     sizeof(uint32_t));
9554255736Sdavidch        bxe_map_sb_state_machines(sb_data_e2.index_data);
9555255736Sdavidch    } else {
9556255736Sdavidch        memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9557255736Sdavidch        sb_data_e1x.common.state = SB_ENABLED;
9558255736Sdavidch        sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9559255736Sdavidch        sb_data_e1x.common.p_func.vf_id = 0xff;
9560255736Sdavidch        sb_data_e1x.common.p_func.vf_valid = FALSE;
9561255736Sdavidch        sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9562255736Sdavidch        sb_data_e1x.common.same_igu_sb_1b = TRUE;
9563255736Sdavidch        sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9564255736Sdavidch        sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9565255736Sdavidch        hc_sm_p = sb_data_e1x.common.state_machine;
9566255736Sdavidch        sb_data_p = (uint32_t *)&sb_data_e1x;
9567255736Sdavidch        data_size = (sizeof(struct hc_status_block_data_e1x) /
9568255736Sdavidch                     sizeof(uint32_t));
9569255736Sdavidch        bxe_map_sb_state_machines(sb_data_e1x.index_data);
9570255736Sdavidch    }
9571255736Sdavidch
9572255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9573255736Sdavidch    bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9574255736Sdavidch
9575255736Sdavidch    BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9576255736Sdavidch
9577255736Sdavidch    /* write indices to HW - PCI guarantees endianity of regpairs */
9578255736Sdavidch    bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9579255736Sdavidch}
9580255736Sdavidch
9581255736Sdavidchstatic inline uint8_t
9582255736Sdavidchbxe_fp_qzone_id(struct bxe_fastpath *fp)
9583255736Sdavidch{
9584255736Sdavidch    if (CHIP_IS_E1x(fp->sc)) {
9585255736Sdavidch        return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9586255736Sdavidch    } else {
9587255736Sdavidch        return (fp->cl_id);
9588255736Sdavidch    }
9589255736Sdavidch}
9590255736Sdavidch
9591255736Sdavidchstatic inline uint32_t
9592255736Sdavidchbxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9593255736Sdavidch                           struct bxe_fastpath *fp)
9594255736Sdavidch{
9595255736Sdavidch    uint32_t offset = BAR_USTRORM_INTMEM;
9596255736Sdavidch
9597255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9598255736Sdavidch        offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9599255736Sdavidch    } else {
9600255736Sdavidch        offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9601255736Sdavidch    }
9602255736Sdavidch
9603255736Sdavidch    return (offset);
9604255736Sdavidch}
9605255736Sdavidch
9606255736Sdavidchstatic void
9607255736Sdavidchbxe_init_eth_fp(struct bxe_softc *sc,
9608255736Sdavidch                int              idx)
9609255736Sdavidch{
9610255736Sdavidch    struct bxe_fastpath *fp = &sc->fp[idx];
9611255736Sdavidch    uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9612255736Sdavidch    unsigned long q_type = 0;
9613255736Sdavidch    int cos;
9614255736Sdavidch
9615255736Sdavidch    fp->sc    = sc;
9616255736Sdavidch    fp->index = idx;
9617255736Sdavidch
9618255736Sdavidch    snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
9619255736Sdavidch             "bxe%d_fp%d_tx_lock", sc->unit, idx);
9620255736Sdavidch    mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
9621255736Sdavidch
9622255736Sdavidch    snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
9623255736Sdavidch             "bxe%d_fp%d_rx_lock", sc->unit, idx);
9624255736Sdavidch    mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
9625255736Sdavidch
9626255736Sdavidch    fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9627255736Sdavidch    fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9628255736Sdavidch
9629255736Sdavidch    fp->cl_id = (CHIP_IS_E1x(sc)) ?
9630255736Sdavidch                    (SC_L_ID(sc) + idx) :
9631255736Sdavidch                    /* want client ID same as IGU SB ID for non-E1 */
9632255736Sdavidch                    fp->igu_sb_id;
9633255736Sdavidch    fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9634255736Sdavidch
9635255736Sdavidch    /* setup sb indices */
9636255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9637255736Sdavidch        fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9638255736Sdavidch        fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9639255736Sdavidch    } else {
9640255736Sdavidch        fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9641255736Sdavidch        fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9642255736Sdavidch    }
9643255736Sdavidch
9644255736Sdavidch    /* init shortcut */
9645255736Sdavidch    fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9646255736Sdavidch
9647255736Sdavidch    fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9648255736Sdavidch
9649255736Sdavidch    /*
9650255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
9651255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9652255736Sdavidch     */
9653255736Sdavidch    for (cos = 0; cos < sc->max_cos; cos++) {
9654255736Sdavidch        cids[cos] = idx;
9655255736Sdavidch    }
9656255736Sdavidch    fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9657255736Sdavidch
9658255736Sdavidch    /* nothing more for a VF to do */
9659255736Sdavidch    if (IS_VF(sc)) {
9660255736Sdavidch        return;
9661255736Sdavidch    }
9662255736Sdavidch
9663255736Sdavidch    bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9664255736Sdavidch                fp->fw_sb_id, fp->igu_sb_id);
9665255736Sdavidch
9666255736Sdavidch    bxe_update_fp_sb_idx(fp);
9667255736Sdavidch
9668255736Sdavidch    /* Configure Queue State object */
9669255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9670255736Sdavidch    bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9671255736Sdavidch
9672255736Sdavidch    ecore_init_queue_obj(sc,
9673255736Sdavidch                         &sc->sp_objs[idx].q_obj,
9674255736Sdavidch                         fp->cl_id,
9675255736Sdavidch                         cids,
9676255736Sdavidch                         sc->max_cos,
9677255736Sdavidch                         SC_FUNC(sc),
9678255736Sdavidch                         BXE_SP(sc, q_rdata),
9679255736Sdavidch                         BXE_SP_MAPPING(sc, q_rdata),
9680255736Sdavidch                         q_type);
9681255736Sdavidch
9682255736Sdavidch    /* configure classification DBs */
9683255736Sdavidch    ecore_init_mac_obj(sc,
9684255736Sdavidch                       &sc->sp_objs[idx].mac_obj,
9685255736Sdavidch                       fp->cl_id,
9686255736Sdavidch                       idx,
9687255736Sdavidch                       SC_FUNC(sc),
9688255736Sdavidch                       BXE_SP(sc, mac_rdata),
9689255736Sdavidch                       BXE_SP_MAPPING(sc, mac_rdata),
9690255736Sdavidch                       ECORE_FILTER_MAC_PENDING,
9691255736Sdavidch                       &sc->sp_state,
9692255736Sdavidch                       ECORE_OBJ_TYPE_RX_TX,
9693255736Sdavidch                       &sc->macs_pool);
9694255736Sdavidch
9695255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9696255736Sdavidch          idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9697255736Sdavidch}
9698255736Sdavidch
9699255736Sdavidchstatic inline void
9700255736Sdavidchbxe_update_rx_prod(struct bxe_softc    *sc,
9701255736Sdavidch                   struct bxe_fastpath *fp,
9702255736Sdavidch                   uint16_t            rx_bd_prod,
9703255736Sdavidch                   uint16_t            rx_cq_prod,
9704255736Sdavidch                   uint16_t            rx_sge_prod)
9705255736Sdavidch{
9706255736Sdavidch    struct ustorm_eth_rx_producers rx_prods = { 0 };
9707255736Sdavidch    uint32_t i;
9708255736Sdavidch
9709255736Sdavidch    /* update producers */
9710255736Sdavidch    rx_prods.bd_prod  = rx_bd_prod;
9711255736Sdavidch    rx_prods.cqe_prod = rx_cq_prod;
9712255736Sdavidch    rx_prods.sge_prod = rx_sge_prod;
9713255736Sdavidch
9714255736Sdavidch    /*
9715255736Sdavidch     * Make sure that the BD and SGE data is updated before updating the
9716255736Sdavidch     * producers since FW might read the BD/SGE right after the producer
9717255736Sdavidch     * is updated.
9718255736Sdavidch     * This is only applicable for weak-ordered memory model archs such
9719255736Sdavidch     * as IA-64. The following barrier is also mandatory since FW will
9720255736Sdavidch     * assumes BDs must have buffers.
9721255736Sdavidch     */
9722255736Sdavidch    wmb();
9723255736Sdavidch
9724255736Sdavidch    for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9725255736Sdavidch        REG_WR(sc,
9726255736Sdavidch               (fp->ustorm_rx_prods_offset + (i * 4)),
9727255736Sdavidch               ((uint32_t *)&rx_prods)[i]);
9728255736Sdavidch    }
9729255736Sdavidch
9730255736Sdavidch    wmb(); /* keep prod updates ordered */
9731255736Sdavidch
9732255736Sdavidch    BLOGD(sc, DBG_RX,
9733255736Sdavidch          "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9734255736Sdavidch          fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9735255736Sdavidch}
9736255736Sdavidch
9737255736Sdavidchstatic void
9738255736Sdavidchbxe_init_rx_rings(struct bxe_softc *sc)
9739255736Sdavidch{
9740255736Sdavidch    struct bxe_fastpath *fp;
9741255736Sdavidch    int i;
9742255736Sdavidch
9743255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9744255736Sdavidch        fp = &sc->fp[i];
9745255736Sdavidch
9746255736Sdavidch        fp->rx_bd_cons = 0;
9747255736Sdavidch
9748255736Sdavidch        /*
9749255736Sdavidch         * Activate the BD ring...
9750255736Sdavidch         * Warning, this will generate an interrupt (to the TSTORM)
9751255736Sdavidch         * so this can only be done after the chip is initialized
9752255736Sdavidch         */
9753255736Sdavidch        bxe_update_rx_prod(sc, fp,
9754255736Sdavidch                           fp->rx_bd_prod,
9755255736Sdavidch                           fp->rx_cq_prod,
9756255736Sdavidch                           fp->rx_sge_prod);
9757255736Sdavidch
9758255736Sdavidch        if (i != 0) {
9759255736Sdavidch            continue;
9760255736Sdavidch        }
9761255736Sdavidch
9762255736Sdavidch        if (CHIP_IS_E1(sc)) {
9763255736Sdavidch            REG_WR(sc,
9764255736Sdavidch                   (BAR_USTRORM_INTMEM +
9765255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9766255736Sdavidch                   U64_LO(fp->rcq_dma.paddr));
9767255736Sdavidch            REG_WR(sc,
9768255736Sdavidch                   (BAR_USTRORM_INTMEM +
9769255736Sdavidch                    USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9770255736Sdavidch                   U64_HI(fp->rcq_dma.paddr));
9771255736Sdavidch        }
9772255736Sdavidch    }
9773255736Sdavidch}
9774255736Sdavidch
9775255736Sdavidchstatic void
9776255736Sdavidchbxe_init_tx_ring_one(struct bxe_fastpath *fp)
9777255736Sdavidch{
9778296071Sdavidcs    SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9779255736Sdavidch    fp->tx_db.data.zero_fill1 = 0;
9780255736Sdavidch    fp->tx_db.data.prod = 0;
9781255736Sdavidch
9782255736Sdavidch    fp->tx_pkt_prod = 0;
9783255736Sdavidch    fp->tx_pkt_cons = 0;
9784255736Sdavidch    fp->tx_bd_prod = 0;
9785255736Sdavidch    fp->tx_bd_cons = 0;
9786255736Sdavidch    fp->eth_q_stats.tx_pkts = 0;
9787255736Sdavidch}
9788255736Sdavidch
9789255736Sdavidchstatic inline void
9790255736Sdavidchbxe_init_tx_rings(struct bxe_softc *sc)
9791255736Sdavidch{
9792255736Sdavidch    int i;
9793255736Sdavidch
9794255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
9795255736Sdavidch        bxe_init_tx_ring_one(&sc->fp[i]);
9796255736Sdavidch    }
9797255736Sdavidch}
9798255736Sdavidch
9799255736Sdavidchstatic void
9800255736Sdavidchbxe_init_def_sb(struct bxe_softc *sc)
9801255736Sdavidch{
9802255736Sdavidch    struct host_sp_status_block *def_sb = sc->def_sb;
9803255736Sdavidch    bus_addr_t mapping = sc->def_sb_dma.paddr;
9804255736Sdavidch    int igu_sp_sb_index;
9805255736Sdavidch    int igu_seg_id;
9806255736Sdavidch    int port = SC_PORT(sc);
9807255736Sdavidch    int func = SC_FUNC(sc);
9808255736Sdavidch    int reg_offset, reg_offset_en5;
9809255736Sdavidch    uint64_t section;
9810255736Sdavidch    int index, sindex;
9811255736Sdavidch    struct hc_sp_status_block_data sp_sb_data;
9812255736Sdavidch
9813255736Sdavidch    memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9814255736Sdavidch
9815255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
9816255736Sdavidch        igu_sp_sb_index = DEF_SB_IGU_ID;
9817255736Sdavidch        igu_seg_id = HC_SEG_ACCESS_DEF;
9818255736Sdavidch    } else {
9819255736Sdavidch        igu_sp_sb_index = sc->igu_dsb_id;
9820255736Sdavidch        igu_seg_id = IGU_SEG_ACCESS_DEF;
9821255736Sdavidch    }
9822255736Sdavidch
9823255736Sdavidch    /* attentions */
9824255736Sdavidch    section = ((uint64_t)mapping +
9825255736Sdavidch               offsetof(struct host_sp_status_block, atten_status_block));
9826255736Sdavidch    def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9827255736Sdavidch    sc->attn_state = 0;
9828255736Sdavidch
9829255736Sdavidch    reg_offset = (port) ?
9830255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9831255736Sdavidch                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9832255736Sdavidch    reg_offset_en5 = (port) ?
9833255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9834255736Sdavidch                         MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9835255736Sdavidch
9836255736Sdavidch    for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9837255736Sdavidch        /* take care of sig[0]..sig[4] */
9838255736Sdavidch        for (sindex = 0; sindex < 4; sindex++) {
9839255736Sdavidch            sc->attn_group[index].sig[sindex] =
9840255736Sdavidch                REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9841255736Sdavidch        }
9842255736Sdavidch
9843255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
9844255736Sdavidch            /*
9845255736Sdavidch             * enable5 is separate from the rest of the registers,
9846255736Sdavidch             * and the address skip is 4 and not 16 between the
9847255736Sdavidch             * different groups
9848255736Sdavidch             */
9849255736Sdavidch            sc->attn_group[index].sig[4] =
9850255736Sdavidch                REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9851255736Sdavidch        } else {
9852255736Sdavidch            sc->attn_group[index].sig[4] = 0;
9853255736Sdavidch        }
9854255736Sdavidch    }
9855255736Sdavidch
9856255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
9857255736Sdavidch        reg_offset = (port) ?
9858255736Sdavidch                         HC_REG_ATTN_MSG1_ADDR_L :
9859255736Sdavidch                         HC_REG_ATTN_MSG0_ADDR_L;
9860255736Sdavidch        REG_WR(sc, reg_offset, U64_LO(section));
9861255736Sdavidch        REG_WR(sc, (reg_offset + 4), U64_HI(section));
9862255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
9863255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9864255736Sdavidch        REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9865255736Sdavidch    }
9866255736Sdavidch
9867255736Sdavidch    section = ((uint64_t)mapping +
9868255736Sdavidch               offsetof(struct host_sp_status_block, sp_sb));
9869255736Sdavidch
9870255736Sdavidch    bxe_zero_sp_sb(sc);
9871255736Sdavidch
9872255736Sdavidch    /* PCI guarantees endianity of regpair */
9873255736Sdavidch    sp_sb_data.state           = SB_ENABLED;
9874255736Sdavidch    sp_sb_data.host_sb_addr.lo = U64_LO(section);
9875255736Sdavidch    sp_sb_data.host_sb_addr.hi = U64_HI(section);
9876255736Sdavidch    sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9877255736Sdavidch    sp_sb_data.igu_seg_id      = igu_seg_id;
9878255736Sdavidch    sp_sb_data.p_func.pf_id    = func;
9879255736Sdavidch    sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9880255736Sdavidch    sp_sb_data.p_func.vf_id    = 0xff;
9881255736Sdavidch
9882255736Sdavidch    bxe_wr_sp_sb_data(sc, &sp_sb_data);
9883255736Sdavidch
9884255736Sdavidch    bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9885255736Sdavidch}
9886255736Sdavidch
9887255736Sdavidchstatic void
9888255736Sdavidchbxe_init_sp_ring(struct bxe_softc *sc)
9889255736Sdavidch{
9890255736Sdavidch    atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9891255736Sdavidch    sc->spq_prod_idx = 0;
9892255736Sdavidch    sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9893255736Sdavidch    sc->spq_prod_bd = sc->spq;
9894255736Sdavidch    sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9895255736Sdavidch}
9896255736Sdavidch
9897255736Sdavidchstatic void
9898255736Sdavidchbxe_init_eq_ring(struct bxe_softc *sc)
9899255736Sdavidch{
9900255736Sdavidch    union event_ring_elem *elem;
9901255736Sdavidch    int i;
9902255736Sdavidch
9903255736Sdavidch    for (i = 1; i <= NUM_EQ_PAGES; i++) {
9904255736Sdavidch        elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9905255736Sdavidch
9906255736Sdavidch        elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9907255736Sdavidch                                                 BCM_PAGE_SIZE *
9908255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9909255736Sdavidch        elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9910255736Sdavidch                                                 BCM_PAGE_SIZE *
9911255736Sdavidch                                                 (i % NUM_EQ_PAGES)));
9912255736Sdavidch    }
9913255736Sdavidch
9914255736Sdavidch    sc->eq_cons    = 0;
9915255736Sdavidch    sc->eq_prod    = NUM_EQ_DESC;
9916255736Sdavidch    sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9917255736Sdavidch
9918255736Sdavidch    atomic_store_rel_long(&sc->eq_spq_left,
9919255736Sdavidch                          (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9920255736Sdavidch                               NUM_EQ_DESC) - 1));
9921255736Sdavidch}
9922255736Sdavidch
9923255736Sdavidchstatic void
9924255736Sdavidchbxe_init_internal_common(struct bxe_softc *sc)
9925255736Sdavidch{
9926255736Sdavidch    int i;
9927255736Sdavidch
9928255736Sdavidch    if (IS_MF_SI(sc)) {
9929255736Sdavidch        /*
9930255736Sdavidch         * In switch independent mode, the TSTORM needs to accept
9931255736Sdavidch         * packets that failed classification, since approximate match
9932255736Sdavidch         * mac addresses aren't written to NIG LLH.
9933255736Sdavidch         */
9934255736Sdavidch        REG_WR8(sc,
9935255736Sdavidch                (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
9936255736Sdavidch                2);
9937255736Sdavidch    } else if (!CHIP_IS_E1(sc)) { /* 57710 doesn't support MF */
9938255736Sdavidch        REG_WR8(sc,
9939255736Sdavidch                (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
9940255736Sdavidch                0);
9941255736Sdavidch    }
9942255736Sdavidch
9943255736Sdavidch    /*
9944255736Sdavidch     * Zero this manually as its initialization is currently missing
9945255736Sdavidch     * in the initTool.
9946255736Sdavidch     */
9947255736Sdavidch    for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9948255736Sdavidch        REG_WR(sc,
9949255736Sdavidch               (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9950255736Sdavidch               0);
9951255736Sdavidch    }
9952255736Sdavidch
9953255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
9954255736Sdavidch        REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9955255736Sdavidch                CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9956255736Sdavidch    }
9957255736Sdavidch}
9958255736Sdavidch
9959255736Sdavidchstatic void
9960255736Sdavidchbxe_init_internal(struct bxe_softc *sc,
9961255736Sdavidch                  uint32_t         load_code)
9962255736Sdavidch{
9963255736Sdavidch    switch (load_code) {
9964255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON:
9965255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9966255736Sdavidch        bxe_init_internal_common(sc);
9967255736Sdavidch        /* no break */
9968255736Sdavidch
9969255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_PORT:
9970255736Sdavidch        /* nothing to do */
9971255736Sdavidch        /* no break */
9972255736Sdavidch
9973255736Sdavidch    case FW_MSG_CODE_DRV_LOAD_FUNCTION:
9974255736Sdavidch        /* internal memory per function is initialized inside bxe_pf_init */
9975255736Sdavidch        break;
9976255736Sdavidch
9977255736Sdavidch    default:
9978255736Sdavidch        BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
9979255736Sdavidch        break;
9980255736Sdavidch    }
9981255736Sdavidch}
9982255736Sdavidch
9983255736Sdavidchstatic void
9984255736Sdavidchstorm_memset_func_cfg(struct bxe_softc                         *sc,
9985255736Sdavidch                      struct tstorm_eth_function_common_config *tcfg,
9986255736Sdavidch                      uint16_t                                  abs_fid)
9987255736Sdavidch{
9988255736Sdavidch    uint32_t addr;
9989255736Sdavidch    size_t size;
9990255736Sdavidch
9991255736Sdavidch    addr = (BAR_TSTRORM_INTMEM +
9992255736Sdavidch            TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
9993255736Sdavidch    size = sizeof(struct tstorm_eth_function_common_config);
9994255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
9995255736Sdavidch}
9996255736Sdavidch
9997255736Sdavidchstatic void
9998255736Sdavidchbxe_func_init(struct bxe_softc            *sc,
9999255736Sdavidch              struct bxe_func_init_params *p)
10000255736Sdavidch{
10001255736Sdavidch    struct tstorm_eth_function_common_config tcfg = { 0 };
10002255736Sdavidch
10003255736Sdavidch    if (CHIP_IS_E1x(sc)) {
10004255736Sdavidch        storm_memset_func_cfg(sc, &tcfg, p->func_id);
10005255736Sdavidch    }
10006255736Sdavidch
10007255736Sdavidch    /* Enable the function in the FW */
10008255736Sdavidch    storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
10009255736Sdavidch    storm_memset_func_en(sc, p->func_id, 1);
10010255736Sdavidch
10011255736Sdavidch    /* spq */
10012255736Sdavidch    if (p->func_flgs & FUNC_FLG_SPQ) {
10013255736Sdavidch        storm_memset_spq_addr(sc, p->spq_map, p->func_id);
10014255736Sdavidch        REG_WR(sc,
10015255736Sdavidch               (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
10016255736Sdavidch               p->spq_prod);
10017255736Sdavidch    }
10018255736Sdavidch}
10019255736Sdavidch
10020255736Sdavidch/*
10021255736Sdavidch * Calculates the sum of vn_min_rates.
10022255736Sdavidch * It's needed for further normalizing of the min_rates.
10023255736Sdavidch * Returns:
10024255736Sdavidch *   sum of vn_min_rates.
10025255736Sdavidch *     or
10026255736Sdavidch *   0 - if all the min_rates are 0.
10027255736Sdavidch * In the later case fainess algorithm should be deactivated.
10028255736Sdavidch * If all min rates are not zero then those that are zeroes will be set to 1.
10029255736Sdavidch */
10030255736Sdavidchstatic void
10031255736Sdavidchbxe_calc_vn_min(struct bxe_softc       *sc,
10032255736Sdavidch                struct cmng_init_input *input)
10033255736Sdavidch{
10034255736Sdavidch    uint32_t vn_cfg;
10035255736Sdavidch    uint32_t vn_min_rate;
10036255736Sdavidch    int all_zero = 1;
10037255736Sdavidch    int vn;
10038255736Sdavidch
10039255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10040255736Sdavidch        vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10041255736Sdavidch        vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
10042255736Sdavidch                        FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
10043255736Sdavidch
10044255736Sdavidch        if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10045255736Sdavidch            /* skip hidden VNs */
10046255736Sdavidch            vn_min_rate = 0;
10047255736Sdavidch        } else if (!vn_min_rate) {
10048255736Sdavidch            /* If min rate is zero - set it to 100 */
10049255736Sdavidch            vn_min_rate = DEF_MIN_RATE;
10050255736Sdavidch        } else {
10051255736Sdavidch            all_zero = 0;
10052255736Sdavidch        }
10053255736Sdavidch
10054255736Sdavidch        input->vnic_min_rate[vn] = vn_min_rate;
10055255736Sdavidch    }
10056255736Sdavidch
10057255736Sdavidch    /* if ETS or all min rates are zeros - disable fairness */
10058255736Sdavidch    if (BXE_IS_ETS_ENABLED(sc)) {
10059255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10060255736Sdavidch        BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10061255736Sdavidch    } else if (all_zero) {
10062255736Sdavidch        input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10063255736Sdavidch        BLOGD(sc, DBG_LOAD,
10064255736Sdavidch              "Fariness disabled (all MIN values are zeroes)\n");
10065255736Sdavidch    } else {
10066255736Sdavidch        input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10067255736Sdavidch    }
10068255736Sdavidch}
10069255736Sdavidch
10070255736Sdavidchstatic inline uint16_t
10071255736Sdavidchbxe_extract_max_cfg(struct bxe_softc *sc,
10072255736Sdavidch                    uint32_t         mf_cfg)
10073255736Sdavidch{
10074255736Sdavidch    uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10075255736Sdavidch                        FUNC_MF_CFG_MAX_BW_SHIFT);
10076255736Sdavidch
10077255736Sdavidch    if (!max_cfg) {
10078255736Sdavidch        BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10079255736Sdavidch        max_cfg = 100;
10080255736Sdavidch    }
10081255736Sdavidch
10082255736Sdavidch    return (max_cfg);
10083255736Sdavidch}
10084255736Sdavidch
10085255736Sdavidchstatic void
10086255736Sdavidchbxe_calc_vn_max(struct bxe_softc       *sc,
10087255736Sdavidch                int                    vn,
10088255736Sdavidch                struct cmng_init_input *input)
10089255736Sdavidch{
10090255736Sdavidch    uint16_t vn_max_rate;
10091255736Sdavidch    uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10092255736Sdavidch    uint32_t max_cfg;
10093255736Sdavidch
10094255736Sdavidch    if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10095255736Sdavidch        vn_max_rate = 0;
10096255736Sdavidch    } else {
10097255736Sdavidch        max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10098255736Sdavidch
10099255736Sdavidch        if (IS_MF_SI(sc)) {
10100255736Sdavidch            /* max_cfg in percents of linkspeed */
10101255736Sdavidch            vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10102255736Sdavidch        } else { /* SD modes */
10103255736Sdavidch            /* max_cfg is absolute in 100Mb units */
10104255736Sdavidch            vn_max_rate = (max_cfg * 100);
10105255736Sdavidch        }
10106255736Sdavidch    }
10107255736Sdavidch
10108255736Sdavidch    BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10109255736Sdavidch
10110255736Sdavidch    input->vnic_max_rate[vn] = vn_max_rate;
10111255736Sdavidch}
10112255736Sdavidch
10113255736Sdavidchstatic void
10114255736Sdavidchbxe_cmng_fns_init(struct bxe_softc *sc,
10115255736Sdavidch                  uint8_t          read_cfg,
10116255736Sdavidch                  uint8_t          cmng_type)
10117255736Sdavidch{
10118255736Sdavidch    struct cmng_init_input input;
10119255736Sdavidch    int vn;
10120255736Sdavidch
10121255736Sdavidch    memset(&input, 0, sizeof(struct cmng_init_input));
10122255736Sdavidch
10123255736Sdavidch    input.port_rate = sc->link_vars.line_speed;
10124255736Sdavidch
10125255736Sdavidch    if (cmng_type == CMNG_FNS_MINMAX) {
10126255736Sdavidch        /* read mf conf from shmem */
10127255736Sdavidch        if (read_cfg) {
10128255736Sdavidch            bxe_read_mf_cfg(sc);
10129255736Sdavidch        }
10130255736Sdavidch
10131255736Sdavidch        /* get VN min rate and enable fairness if not 0 */
10132255736Sdavidch        bxe_calc_vn_min(sc, &input);
10133255736Sdavidch
10134255736Sdavidch        /* get VN max rate */
10135255736Sdavidch        if (sc->port.pmf) {
10136255736Sdavidch            for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10137255736Sdavidch                bxe_calc_vn_max(sc, vn, &input);
10138255736Sdavidch            }
10139255736Sdavidch        }
10140255736Sdavidch
10141255736Sdavidch        /* always enable rate shaping and fairness */
10142255736Sdavidch        input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10143255736Sdavidch
10144255736Sdavidch        ecore_init_cmng(&input, &sc->cmng);
10145255736Sdavidch        return;
10146255736Sdavidch    }
10147255736Sdavidch
10148255736Sdavidch    /* rate shaping and fairness are disabled */
10149255736Sdavidch    BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10150255736Sdavidch}
10151255736Sdavidch
10152255736Sdavidchstatic int
10153255736Sdavidchbxe_get_cmng_fns_mode(struct bxe_softc *sc)
10154255736Sdavidch{
10155255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10156255736Sdavidch        return (CMNG_FNS_NONE);
10157255736Sdavidch    }
10158255736Sdavidch
10159255736Sdavidch    if (IS_MF(sc)) {
10160255736Sdavidch        return (CMNG_FNS_MINMAX);
10161255736Sdavidch    }
10162255736Sdavidch
10163255736Sdavidch    return (CMNG_FNS_NONE);
10164255736Sdavidch}
10165255736Sdavidch
10166255736Sdavidchstatic void
10167255736Sdavidchstorm_memset_cmng(struct bxe_softc *sc,
10168255736Sdavidch                  struct cmng_init *cmng,
10169255736Sdavidch                  uint8_t          port)
10170255736Sdavidch{
10171255736Sdavidch    int vn;
10172255736Sdavidch    int func;
10173255736Sdavidch    uint32_t addr;
10174255736Sdavidch    size_t size;
10175255736Sdavidch
10176255736Sdavidch    addr = (BAR_XSTRORM_INTMEM +
10177255736Sdavidch            XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10178255736Sdavidch    size = sizeof(struct cmng_struct_per_port);
10179255736Sdavidch    ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10180255736Sdavidch
10181255736Sdavidch    for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10182255736Sdavidch        func = func_by_vn(sc, vn);
10183255736Sdavidch
10184255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10185255736Sdavidch                XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10186255736Sdavidch        size = sizeof(struct rate_shaping_vars_per_vn);
10187255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10188255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10189255736Sdavidch
10190255736Sdavidch        addr = (BAR_XSTRORM_INTMEM +
10191255736Sdavidch                XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10192255736Sdavidch        size = sizeof(struct fairness_vars_per_vn);
10193255736Sdavidch        ecore_storm_memset_struct(sc, addr, size,
10194255736Sdavidch                                  (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10195255736Sdavidch    }
10196255736Sdavidch}
10197255736Sdavidch
10198255736Sdavidchstatic void
10199255736Sdavidchbxe_pf_init(struct bxe_softc *sc)
10200255736Sdavidch{
10201255736Sdavidch    struct bxe_func_init_params func_init = { 0 };
10202255736Sdavidch    struct event_ring_data eq_data = { { 0 } };
10203255736Sdavidch    uint16_t flags;
10204255736Sdavidch
10205255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
10206255736Sdavidch        /* reset IGU PF statistics: MSIX + ATTN */
10207255736Sdavidch        /* PF */
10208255736Sdavidch        REG_WR(sc,
10209255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10210255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10211255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10212255736Sdavidch               0);
10213255736Sdavidch        /* ATTN */
10214255736Sdavidch        REG_WR(sc,
10215255736Sdavidch               (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10216255736Sdavidch                (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10217255736Sdavidch                (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10218255736Sdavidch                ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10219255736Sdavidch               0);
10220255736Sdavidch    }
10221255736Sdavidch
10222255736Sdavidch    /* function setup flags */
10223255736Sdavidch    flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10224255736Sdavidch
10225255736Sdavidch    /*
10226255736Sdavidch     * This flag is relevant for E1x only.
10227255736Sdavidch     * E2 doesn't have a TPA configuration in a function level.
10228255736Sdavidch     */
10229266979Smarcel    flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10230255736Sdavidch
10231255736Sdavidch    func_init.func_flgs = flags;
10232255736Sdavidch    func_init.pf_id     = SC_FUNC(sc);
10233255736Sdavidch    func_init.func_id   = SC_FUNC(sc);
10234255736Sdavidch    func_init.spq_map   = sc->spq_dma.paddr;
10235255736Sdavidch    func_init.spq_prod  = sc->spq_prod_idx;
10236255736Sdavidch
10237255736Sdavidch    bxe_func_init(sc, &func_init);
10238255736Sdavidch
10239255736Sdavidch    memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10240255736Sdavidch
10241255736Sdavidch    /*
10242255736Sdavidch     * Congestion management values depend on the link rate.
10243255736Sdavidch     * There is no active link so initial link rate is set to 10Gbps.
10244255736Sdavidch     * When the link comes up the congestion management values are
10245255736Sdavidch     * re-calculated according to the actual link rate.
10246255736Sdavidch     */
10247255736Sdavidch    sc->link_vars.line_speed = SPEED_10000;
10248255736Sdavidch    bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10249255736Sdavidch
10250255736Sdavidch    /* Only the PMF sets the HW */
10251255736Sdavidch    if (sc->port.pmf) {
10252255736Sdavidch        storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10253255736Sdavidch    }
10254255736Sdavidch
10255255736Sdavidch    /* init Event Queue - PCI bus guarantees correct endainity */
10256255736Sdavidch    eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10257255736Sdavidch    eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10258255736Sdavidch    eq_data.producer     = sc->eq_prod;
10259255736Sdavidch    eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10260255736Sdavidch    eq_data.sb_id        = DEF_SB_ID;
10261255736Sdavidch    storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10262255736Sdavidch}
10263255736Sdavidch
10264255736Sdavidchstatic void
10265255736Sdavidchbxe_hc_int_enable(struct bxe_softc *sc)
10266255736Sdavidch{
10267255736Sdavidch    int port = SC_PORT(sc);
10268255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10269255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10270255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10271255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10272255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10273255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10274255736Sdavidch
10275255736Sdavidch    if (msix) {
10276255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10277255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0);
10278255736Sdavidch        val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10279255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10280255736Sdavidch        if (single_msix) {
10281255736Sdavidch            val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10282255736Sdavidch        }
10283255736Sdavidch    } else if (msi) {
10284255736Sdavidch        val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10285255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10286255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10287255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10288255736Sdavidch    } else {
10289255736Sdavidch        val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10290255736Sdavidch                HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10291255736Sdavidch                HC_CONFIG_0_REG_INT_LINE_EN_0 |
10292255736Sdavidch                HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10293255736Sdavidch
10294255736Sdavidch        if (!CHIP_IS_E1(sc)) {
10295255736Sdavidch            BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10296255736Sdavidch                  val, port, addr);
10297255736Sdavidch
10298255736Sdavidch            REG_WR(sc, addr, val);
10299255736Sdavidch
10300255736Sdavidch            val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10301255736Sdavidch        }
10302255736Sdavidch    }
10303255736Sdavidch
10304255736Sdavidch    if (CHIP_IS_E1(sc)) {
10305255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10306255736Sdavidch    }
10307255736Sdavidch
10308255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10309255736Sdavidch          val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10310255736Sdavidch
10311255736Sdavidch    REG_WR(sc, addr, val);
10312255736Sdavidch
10313255736Sdavidch    /* ensure that HC_CONFIG is written before leading/trailing edge config */
10314255736Sdavidch    mb();
10315255736Sdavidch
10316255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10317255736Sdavidch        /* init leading/trailing edge */
10318255736Sdavidch        if (IS_MF(sc)) {
10319255736Sdavidch            val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10320255736Sdavidch            if (sc->port.pmf) {
10321255736Sdavidch                /* enable nig and gpio3 attention */
10322255736Sdavidch                val |= 0x1100;
10323255736Sdavidch            }
10324255736Sdavidch        } else {
10325255736Sdavidch            val = 0xffff;
10326255736Sdavidch        }
10327255736Sdavidch
10328255736Sdavidch        REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10329255736Sdavidch        REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10330255736Sdavidch    }
10331255736Sdavidch
10332255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10333255736Sdavidch    mb();
10334255736Sdavidch}
10335255736Sdavidch
10336255736Sdavidchstatic void
10337255736Sdavidchbxe_igu_int_enable(struct bxe_softc *sc)
10338255736Sdavidch{
10339255736Sdavidch    uint32_t val;
10340255736Sdavidch    uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10341255736Sdavidch    uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10342255736Sdavidch                           (sc->intr_count == 1)) ? TRUE : FALSE;
10343255736Sdavidch    uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10344255736Sdavidch
10345255736Sdavidch    val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10346255736Sdavidch
10347255736Sdavidch    if (msix) {
10348255736Sdavidch        val &= ~(IGU_PF_CONF_INT_LINE_EN |
10349255736Sdavidch                 IGU_PF_CONF_SINGLE_ISR_EN);
10350255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10351255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN);
10352255736Sdavidch        if (single_msix) {
10353255736Sdavidch            val |= IGU_PF_CONF_SINGLE_ISR_EN;
10354255736Sdavidch        }
10355255736Sdavidch    } else if (msi) {
10356255736Sdavidch        val &= ~IGU_PF_CONF_INT_LINE_EN;
10357255736Sdavidch        val |= (IGU_PF_CONF_MSI_MSIX_EN |
10358255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10359255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10360255736Sdavidch    } else {
10361255736Sdavidch        val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10362255736Sdavidch        val |= (IGU_PF_CONF_INT_LINE_EN |
10363255736Sdavidch                IGU_PF_CONF_ATTN_BIT_EN |
10364255736Sdavidch                IGU_PF_CONF_SINGLE_ISR_EN);
10365255736Sdavidch    }
10366255736Sdavidch
10367255736Sdavidch    /* clean previous status - need to configure igu prior to ack*/
10368255736Sdavidch    if ((!msix) || single_msix) {
10369255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10370255736Sdavidch        bxe_ack_int(sc);
10371255736Sdavidch    }
10372255736Sdavidch
10373255736Sdavidch    val |= IGU_PF_CONF_FUNC_EN;
10374255736Sdavidch
10375255736Sdavidch    BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10376255736Sdavidch          val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10377255736Sdavidch
10378255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10379255736Sdavidch
10380255736Sdavidch    mb();
10381255736Sdavidch
10382255736Sdavidch    /* init leading/trailing edge */
10383255736Sdavidch    if (IS_MF(sc)) {
10384255736Sdavidch        val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10385255736Sdavidch        if (sc->port.pmf) {
10386255736Sdavidch            /* enable nig and gpio3 attention */
10387255736Sdavidch            val |= 0x1100;
10388255736Sdavidch        }
10389255736Sdavidch    } else {
10390255736Sdavidch        val = 0xffff;
10391255736Sdavidch    }
10392255736Sdavidch
10393255736Sdavidch    REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10394255736Sdavidch    REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10395255736Sdavidch
10396255736Sdavidch    /* make sure that interrupts are indeed enabled from here on */
10397255736Sdavidch    mb();
10398255736Sdavidch}
10399255736Sdavidch
10400255736Sdavidchstatic void
10401255736Sdavidchbxe_int_enable(struct bxe_softc *sc)
10402255736Sdavidch{
10403255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10404255736Sdavidch        bxe_hc_int_enable(sc);
10405255736Sdavidch    } else {
10406255736Sdavidch        bxe_igu_int_enable(sc);
10407255736Sdavidch    }
10408255736Sdavidch}
10409255736Sdavidch
10410255736Sdavidchstatic void
10411255736Sdavidchbxe_hc_int_disable(struct bxe_softc *sc)
10412255736Sdavidch{
10413255736Sdavidch    int port = SC_PORT(sc);
10414255736Sdavidch    uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10415255736Sdavidch    uint32_t val = REG_RD(sc, addr);
10416255736Sdavidch
10417255736Sdavidch    /*
10418255736Sdavidch     * In E1 we must use only PCI configuration space to disable MSI/MSIX
10419255736Sdavidch     * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10420255736Sdavidch     * block
10421255736Sdavidch     */
10422255736Sdavidch    if (CHIP_IS_E1(sc)) {
10423255736Sdavidch        /*
10424255736Sdavidch         * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10425255736Sdavidch         * to prevent from HC sending interrupts after we exit the function
10426255736Sdavidch         */
10427255736Sdavidch        REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10428255736Sdavidch
10429255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10430255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10431255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10432255736Sdavidch    } else {
10433255736Sdavidch        val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10434255736Sdavidch                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10435255736Sdavidch                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10436255736Sdavidch                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10437255736Sdavidch    }
10438255736Sdavidch
10439255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10440255736Sdavidch
10441255736Sdavidch    /* flush all outstanding writes */
10442255736Sdavidch    mb();
10443255736Sdavidch
10444255736Sdavidch    REG_WR(sc, addr, val);
10445255736Sdavidch    if (REG_RD(sc, addr) != val) {
10446255736Sdavidch        BLOGE(sc, "proper val not read from HC IGU!\n");
10447255736Sdavidch    }
10448255736Sdavidch}
10449255736Sdavidch
10450255736Sdavidchstatic void
10451255736Sdavidchbxe_igu_int_disable(struct bxe_softc *sc)
10452255736Sdavidch{
10453255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10454255736Sdavidch
10455255736Sdavidch    val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10456255736Sdavidch             IGU_PF_CONF_INT_LINE_EN |
10457255736Sdavidch             IGU_PF_CONF_ATTN_BIT_EN);
10458255736Sdavidch
10459255736Sdavidch    BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10460255736Sdavidch
10461255736Sdavidch    /* flush all outstanding writes */
10462255736Sdavidch    mb();
10463255736Sdavidch
10464255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10465255736Sdavidch    if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10466255736Sdavidch        BLOGE(sc, "proper val not read from IGU!\n");
10467255736Sdavidch    }
10468255736Sdavidch}
10469255736Sdavidch
10470255736Sdavidchstatic void
10471255736Sdavidchbxe_int_disable(struct bxe_softc *sc)
10472255736Sdavidch{
10473255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
10474255736Sdavidch        bxe_hc_int_disable(sc);
10475255736Sdavidch    } else {
10476255736Sdavidch        bxe_igu_int_disable(sc);
10477255736Sdavidch    }
10478255736Sdavidch}
10479255736Sdavidch
10480255736Sdavidchstatic void
10481255736Sdavidchbxe_nic_init(struct bxe_softc *sc,
10482255736Sdavidch             int              load_code)
10483255736Sdavidch{
10484255736Sdavidch    int i;
10485255736Sdavidch
10486255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
10487255736Sdavidch        bxe_init_eth_fp(sc, i);
10488255736Sdavidch    }
10489255736Sdavidch
10490255736Sdavidch    rmb(); /* ensure status block indices were read */
10491255736Sdavidch
10492255736Sdavidch    bxe_init_rx_rings(sc);
10493255736Sdavidch    bxe_init_tx_rings(sc);
10494255736Sdavidch
10495255736Sdavidch    if (IS_VF(sc)) {
10496255736Sdavidch        return;
10497255736Sdavidch    }
10498255736Sdavidch
10499255736Sdavidch    /* initialize MOD_ABS interrupts */
10500255736Sdavidch    elink_init_mod_abs_int(sc, &sc->link_vars,
10501255736Sdavidch                           sc->devinfo.chip_id,
10502255736Sdavidch                           sc->devinfo.shmem_base,
10503255736Sdavidch                           sc->devinfo.shmem2_base,
10504255736Sdavidch                           SC_PORT(sc));
10505255736Sdavidch
10506255736Sdavidch    bxe_init_def_sb(sc);
10507255736Sdavidch    bxe_update_dsb_idx(sc);
10508255736Sdavidch    bxe_init_sp_ring(sc);
10509255736Sdavidch    bxe_init_eq_ring(sc);
10510255736Sdavidch    bxe_init_internal(sc, load_code);
10511255736Sdavidch    bxe_pf_init(sc);
10512255736Sdavidch    bxe_stats_init(sc);
10513255736Sdavidch
10514255736Sdavidch    /* flush all before enabling interrupts */
10515255736Sdavidch    mb();
10516255736Sdavidch
10517255736Sdavidch    bxe_int_enable(sc);
10518255736Sdavidch
10519255736Sdavidch    /* check for SPIO5 */
10520255736Sdavidch    bxe_attn_int_deasserted0(sc,
10521255736Sdavidch                             REG_RD(sc,
10522255736Sdavidch                                    (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10523255736Sdavidch                                     SC_PORT(sc)*4)) &
10524255736Sdavidch                             AEU_INPUTS_ATTN_BITS_SPIO5);
10525255736Sdavidch}
10526255736Sdavidch
10527255736Sdavidchstatic inline void
10528255736Sdavidchbxe_init_objs(struct bxe_softc *sc)
10529255736Sdavidch{
10530255736Sdavidch    /* mcast rules must be added to tx if tx switching is enabled */
10531255736Sdavidch    ecore_obj_type o_type =
10532255736Sdavidch        (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10533255736Sdavidch                                         ECORE_OBJ_TYPE_RX;
10534255736Sdavidch
10535255736Sdavidch    /* RX_MODE controlling object */
10536255736Sdavidch    ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10537255736Sdavidch
10538255736Sdavidch    /* multicast configuration controlling object */
10539255736Sdavidch    ecore_init_mcast_obj(sc,
10540255736Sdavidch                         &sc->mcast_obj,
10541255736Sdavidch                         sc->fp[0].cl_id,
10542255736Sdavidch                         sc->fp[0].index,
10543255736Sdavidch                         SC_FUNC(sc),
10544255736Sdavidch                         SC_FUNC(sc),
10545255736Sdavidch                         BXE_SP(sc, mcast_rdata),
10546255736Sdavidch                         BXE_SP_MAPPING(sc, mcast_rdata),
10547255736Sdavidch                         ECORE_FILTER_MCAST_PENDING,
10548255736Sdavidch                         &sc->sp_state,
10549255736Sdavidch                         o_type);
10550255736Sdavidch
10551255736Sdavidch    /* Setup CAM credit pools */
10552255736Sdavidch    ecore_init_mac_credit_pool(sc,
10553255736Sdavidch                               &sc->macs_pool,
10554255736Sdavidch                               SC_FUNC(sc),
10555255736Sdavidch                               CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10556255736Sdavidch                                                 VNICS_PER_PATH(sc));
10557255736Sdavidch
10558255736Sdavidch    ecore_init_vlan_credit_pool(sc,
10559255736Sdavidch                                &sc->vlans_pool,
10560255736Sdavidch                                SC_ABS_FUNC(sc) >> 1,
10561255736Sdavidch                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10562255736Sdavidch                                                  VNICS_PER_PATH(sc));
10563255736Sdavidch
10564255736Sdavidch    /* RSS configuration object */
10565255736Sdavidch    ecore_init_rss_config_obj(sc,
10566255736Sdavidch                              &sc->rss_conf_obj,
10567255736Sdavidch                              sc->fp[0].cl_id,
10568255736Sdavidch                              sc->fp[0].index,
10569255736Sdavidch                              SC_FUNC(sc),
10570255736Sdavidch                              SC_FUNC(sc),
10571255736Sdavidch                              BXE_SP(sc, rss_rdata),
10572255736Sdavidch                              BXE_SP_MAPPING(sc, rss_rdata),
10573255736Sdavidch                              ECORE_FILTER_RSS_CONF_PENDING,
10574255736Sdavidch                              &sc->sp_state, ECORE_OBJ_TYPE_RX);
10575255736Sdavidch}
10576255736Sdavidch
10577255736Sdavidch/*
10578255736Sdavidch * Initialize the function. This must be called before sending CLIENT_SETUP
10579255736Sdavidch * for the first client.
10580255736Sdavidch */
10581255736Sdavidchstatic inline int
10582255736Sdavidchbxe_func_start(struct bxe_softc *sc)
10583255736Sdavidch{
10584255736Sdavidch    struct ecore_func_state_params func_params = { NULL };
10585255736Sdavidch    struct ecore_func_start_params *start_params = &func_params.params.start;
10586255736Sdavidch
10587255736Sdavidch    /* Prepare parameters for function state transitions */
10588255736Sdavidch    bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10589255736Sdavidch
10590255736Sdavidch    func_params.f_obj = &sc->func_obj;
10591255736Sdavidch    func_params.cmd = ECORE_F_CMD_START;
10592255736Sdavidch
10593255736Sdavidch    /* Function parameters */
10594255736Sdavidch    start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10595255736Sdavidch    start_params->sd_vlan_tag = OVLAN(sc);
10596255736Sdavidch
10597255736Sdavidch    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10598255736Sdavidch        start_params->network_cos_mode = STATIC_COS;
10599255736Sdavidch    } else { /* CHIP_IS_E1X */
10600255736Sdavidch        start_params->network_cos_mode = FW_WRR;
10601255736Sdavidch    }
10602255736Sdavidch
10603296071Sdavidcs    //start_params->gre_tunnel_mode = 0;
10604296071Sdavidcs    //start_params->gre_tunnel_rss  = 0;
10605255736Sdavidch
10606255736Sdavidch    return (ecore_func_state_change(sc, &func_params));
10607255736Sdavidch}
10608255736Sdavidch
10609255736Sdavidchstatic int
10610255736Sdavidchbxe_set_power_state(struct bxe_softc *sc,
10611255736Sdavidch                    uint8_t          state)
10612255736Sdavidch{
10613255736Sdavidch    uint16_t pmcsr;
10614255736Sdavidch
10615255736Sdavidch    /* If there is no power capability, silently succeed */
10616255736Sdavidch    if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10617255736Sdavidch        BLOGW(sc, "No power capability\n");
10618255736Sdavidch        return (0);
10619255736Sdavidch    }
10620255736Sdavidch
10621255736Sdavidch    pmcsr = pci_read_config(sc->dev,
10622255736Sdavidch                            (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10623255736Sdavidch                            2);
10624255736Sdavidch
10625255736Sdavidch    switch (state) {
10626255736Sdavidch    case PCI_PM_D0:
10627255736Sdavidch        pci_write_config(sc->dev,
10628255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10629255736Sdavidch                         ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10630255736Sdavidch
10631255736Sdavidch        if (pmcsr & PCIM_PSTAT_DMASK) {
10632255736Sdavidch            /* delay required during transition out of D3hot */
10633255736Sdavidch            DELAY(20000);
10634255736Sdavidch        }
10635255736Sdavidch
10636255736Sdavidch        break;
10637255736Sdavidch
10638255736Sdavidch    case PCI_PM_D3hot:
10639255736Sdavidch        /* XXX if there are other clients above don't shut down the power */
10640255736Sdavidch
10641255736Sdavidch        /* don't shut down the power for emulation and FPGA */
10642255736Sdavidch        if (CHIP_REV_IS_SLOW(sc)) {
10643255736Sdavidch            return (0);
10644255736Sdavidch        }
10645255736Sdavidch
10646255736Sdavidch        pmcsr &= ~PCIM_PSTAT_DMASK;
10647255736Sdavidch        pmcsr |= PCIM_PSTAT_D3;
10648255736Sdavidch
10649255736Sdavidch        if (sc->wol) {
10650255736Sdavidch            pmcsr |= PCIM_PSTAT_PMEENABLE;
10651255736Sdavidch        }
10652255736Sdavidch
10653255736Sdavidch        pci_write_config(sc->dev,
10654255736Sdavidch                         (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10655255736Sdavidch                         pmcsr, 4);
10656255736Sdavidch
10657255736Sdavidch        /*
10658255736Sdavidch         * No more memory access after this point until device is brought back
10659255736Sdavidch         * to D0 state.
10660255736Sdavidch         */
10661255736Sdavidch        break;
10662255736Sdavidch
10663255736Sdavidch    default:
10664295830Sdavidcs        BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10665295830Sdavidcs            state, pmcsr);
10666255736Sdavidch        return (-1);
10667255736Sdavidch    }
10668255736Sdavidch
10669255736Sdavidch    return (0);
10670255736Sdavidch}
10671255736Sdavidch
10672255736Sdavidch
10673255736Sdavidch/* return true if succeeded to acquire the lock */
10674255736Sdavidchstatic uint8_t
10675255736Sdavidchbxe_trylock_hw_lock(struct bxe_softc *sc,
10676255736Sdavidch                    uint32_t         resource)
10677255736Sdavidch{
10678255736Sdavidch    uint32_t lock_status;
10679255736Sdavidch    uint32_t resource_bit = (1 << resource);
10680255736Sdavidch    int func = SC_FUNC(sc);
10681255736Sdavidch    uint32_t hw_lock_control_reg;
10682255736Sdavidch
10683255736Sdavidch    BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10684255736Sdavidch
10685255736Sdavidch    /* Validating that the resource is within range */
10686255736Sdavidch    if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10687255736Sdavidch        BLOGD(sc, DBG_LOAD,
10688255736Sdavidch              "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10689255736Sdavidch              resource, HW_LOCK_MAX_RESOURCE_VALUE);
10690255736Sdavidch        return (FALSE);
10691255736Sdavidch    }
10692255736Sdavidch
10693255736Sdavidch    if (func <= 5) {
10694255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10695255736Sdavidch    } else {
10696255736Sdavidch        hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10697255736Sdavidch    }
10698255736Sdavidch
10699255736Sdavidch    /* try to acquire the lock */
10700255736Sdavidch    REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10701255736Sdavidch    lock_status = REG_RD(sc, hw_lock_control_reg);
10702255736Sdavidch    if (lock_status & resource_bit) {
10703255736Sdavidch        return (TRUE);
10704255736Sdavidch    }
10705255736Sdavidch
10706295830Sdavidcs    BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10707295830Sdavidcs        "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10708295830Sdavidcs        lock_status, resource_bit);
10709255736Sdavidch
10710255736Sdavidch    return (FALSE);
10711255736Sdavidch}
10712255736Sdavidch
10713255736Sdavidch/*
10714255736Sdavidch * Get the recovery leader resource id according to the engine this function
10715255736Sdavidch * belongs to. Currently only only 2 engines is supported.
10716255736Sdavidch */
10717255736Sdavidchstatic int
10718255736Sdavidchbxe_get_leader_lock_resource(struct bxe_softc *sc)
10719255736Sdavidch{
10720255736Sdavidch    if (SC_PATH(sc)) {
10721255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10722255736Sdavidch    } else {
10723255736Sdavidch        return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10724255736Sdavidch    }
10725255736Sdavidch}
10726255736Sdavidch
10727255736Sdavidch/* try to acquire a leader lock for current engine */
10728255736Sdavidchstatic uint8_t
10729255736Sdavidchbxe_trylock_leader_lock(struct bxe_softc *sc)
10730255736Sdavidch{
10731255736Sdavidch    return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10732255736Sdavidch}
10733255736Sdavidch
10734255736Sdavidchstatic int
10735255736Sdavidchbxe_release_leader_lock(struct bxe_softc *sc)
10736255736Sdavidch{
10737255736Sdavidch    return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10738255736Sdavidch}
10739255736Sdavidch
10740255736Sdavidch/* close gates #2, #3 and #4 */
10741255736Sdavidchstatic void
10742255736Sdavidchbxe_set_234_gates(struct bxe_softc *sc,
10743255736Sdavidch                  uint8_t          close)
10744255736Sdavidch{
10745255736Sdavidch    uint32_t val;
10746255736Sdavidch
10747255736Sdavidch    /* gates #2 and #4a are closed/opened for "not E1" only */
10748255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10749255736Sdavidch        /* #4 */
10750255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10751255736Sdavidch        /* #2 */
10752255736Sdavidch        REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10753255736Sdavidch    }
10754255736Sdavidch
10755255736Sdavidch    /* #3 */
10756255736Sdavidch    if (CHIP_IS_E1x(sc)) {
10757255736Sdavidch        /* prevent interrupts from HC on both ports */
10758255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_1);
10759255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_1,
10760255736Sdavidch               (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10761255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10762255736Sdavidch
10763255736Sdavidch        val = REG_RD(sc, HC_REG_CONFIG_0);
10764255736Sdavidch        REG_WR(sc, HC_REG_CONFIG_0,
10765255736Sdavidch               (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10766255736Sdavidch               (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10767255736Sdavidch    } else {
10768255736Sdavidch        /* Prevent incomming interrupts in IGU */
10769255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10770255736Sdavidch
10771255736Sdavidch        REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10772255736Sdavidch               (!close) ?
10773255736Sdavidch               (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10774255736Sdavidch               (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10775255736Sdavidch    }
10776255736Sdavidch
10777255736Sdavidch    BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10778255736Sdavidch          close ? "closing" : "opening");
10779255736Sdavidch
10780255736Sdavidch    wmb();
10781255736Sdavidch}
10782255736Sdavidch
10783255736Sdavidch/* poll for pending writes bit, it should get cleared in no more than 1s */
10784255736Sdavidchstatic int
10785255736Sdavidchbxe_er_poll_igu_vq(struct bxe_softc *sc)
10786255736Sdavidch{
10787255736Sdavidch    uint32_t cnt = 1000;
10788255736Sdavidch    uint32_t pend_bits = 0;
10789255736Sdavidch
10790255736Sdavidch    do {
10791255736Sdavidch        pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10792255736Sdavidch
10793255736Sdavidch        if (pend_bits == 0) {
10794255736Sdavidch            break;
10795255736Sdavidch        }
10796255736Sdavidch
10797255736Sdavidch        DELAY(1000);
10798255736Sdavidch    } while (--cnt > 0);
10799255736Sdavidch
10800255736Sdavidch    if (cnt == 0) {
10801255736Sdavidch        BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10802255736Sdavidch        return (-1);
10803255736Sdavidch    }
10804255736Sdavidch
10805255736Sdavidch    return (0);
10806255736Sdavidch}
10807255736Sdavidch
10808255736Sdavidch#define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10809255736Sdavidch
10810255736Sdavidchstatic void
10811255736Sdavidchbxe_clp_reset_prep(struct bxe_softc *sc,
10812255736Sdavidch                   uint32_t         *magic_val)
10813255736Sdavidch{
10814255736Sdavidch    /* Do some magic... */
10815255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10816255736Sdavidch    *magic_val = val & SHARED_MF_CLP_MAGIC;
10817255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10818255736Sdavidch}
10819255736Sdavidch
10820255736Sdavidch/* restore the value of the 'magic' bit */
10821255736Sdavidchstatic void
10822255736Sdavidchbxe_clp_reset_done(struct bxe_softc *sc,
10823255736Sdavidch                   uint32_t         magic_val)
10824255736Sdavidch{
10825255736Sdavidch    /* Restore the 'magic' bit value... */
10826255736Sdavidch    uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10827255736Sdavidch    MFCFG_WR(sc, shared_mf_config.clp_mb,
10828255736Sdavidch              (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10829255736Sdavidch}
10830255736Sdavidch
10831255736Sdavidch/* prepare for MCP reset, takes care of CLP configurations */
10832255736Sdavidchstatic void
10833255736Sdavidchbxe_reset_mcp_prep(struct bxe_softc *sc,
10834255736Sdavidch                   uint32_t         *magic_val)
10835255736Sdavidch{
10836255736Sdavidch    uint32_t shmem;
10837255736Sdavidch    uint32_t validity_offset;
10838255736Sdavidch
10839255736Sdavidch    /* set `magic' bit in order to save MF config */
10840255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10841255736Sdavidch        bxe_clp_reset_prep(sc, magic_val);
10842255736Sdavidch    }
10843255736Sdavidch
10844255736Sdavidch    /* get shmem offset */
10845255736Sdavidch    shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10846255736Sdavidch    validity_offset =
10847255736Sdavidch        offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10848255736Sdavidch
10849255736Sdavidch    /* Clear validity map flags */
10850255736Sdavidch    if (shmem > 0) {
10851255736Sdavidch        REG_WR(sc, shmem + validity_offset, 0);
10852255736Sdavidch    }
10853255736Sdavidch}
10854255736Sdavidch
10855255736Sdavidch#define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10856255736Sdavidch#define MCP_ONE_TIMEOUT  100    /* 100 ms */
10857255736Sdavidch
10858255736Sdavidchstatic void
10859255736Sdavidchbxe_mcp_wait_one(struct bxe_softc *sc)
10860255736Sdavidch{
10861255736Sdavidch    /* special handling for emulation and FPGA (10 times longer) */
10862255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
10863255736Sdavidch        DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10864255736Sdavidch    } else {
10865255736Sdavidch        DELAY((MCP_ONE_TIMEOUT) * 1000);
10866255736Sdavidch    }
10867255736Sdavidch}
10868255736Sdavidch
10869255736Sdavidch/* initialize shmem_base and waits for validity signature to appear */
10870255736Sdavidchstatic int
10871255736Sdavidchbxe_init_shmem(struct bxe_softc *sc)
10872255736Sdavidch{
10873255736Sdavidch    int cnt = 0;
10874255736Sdavidch    uint32_t val = 0;
10875255736Sdavidch
10876255736Sdavidch    do {
10877255736Sdavidch        sc->devinfo.shmem_base     =
10878255736Sdavidch        sc->link_params.shmem_base =
10879255736Sdavidch            REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10880255736Sdavidch
10881255736Sdavidch        if (sc->devinfo.shmem_base) {
10882255736Sdavidch            val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10883255736Sdavidch            if (val & SHR_MEM_VALIDITY_MB)
10884255736Sdavidch                return (0);
10885255736Sdavidch        }
10886255736Sdavidch
10887255736Sdavidch        bxe_mcp_wait_one(sc);
10888255736Sdavidch
10889255736Sdavidch    } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10890255736Sdavidch
10891255736Sdavidch    BLOGE(sc, "BAD MCP validity signature\n");
10892255736Sdavidch
10893255736Sdavidch    return (-1);
10894255736Sdavidch}
10895255736Sdavidch
10896255736Sdavidchstatic int
10897255736Sdavidchbxe_reset_mcp_comp(struct bxe_softc *sc,
10898255736Sdavidch                   uint32_t         magic_val)
10899255736Sdavidch{
10900255736Sdavidch    int rc = bxe_init_shmem(sc);
10901255736Sdavidch
10902255736Sdavidch    /* Restore the `magic' bit value */
10903255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10904255736Sdavidch        bxe_clp_reset_done(sc, magic_val);
10905255736Sdavidch    }
10906255736Sdavidch
10907255736Sdavidch    return (rc);
10908255736Sdavidch}
10909255736Sdavidch
10910255736Sdavidchstatic void
10911255736Sdavidchbxe_pxp_prep(struct bxe_softc *sc)
10912255736Sdavidch{
10913255736Sdavidch    if (!CHIP_IS_E1(sc)) {
10914255736Sdavidch        REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10915255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10916255736Sdavidch        wmb();
10917255736Sdavidch    }
10918255736Sdavidch}
10919255736Sdavidch
10920255736Sdavidch/*
10921255736Sdavidch * Reset the whole chip except for:
10922255736Sdavidch *      - PCIE core
10923255736Sdavidch *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10924255736Sdavidch *      - IGU
10925255736Sdavidch *      - MISC (including AEU)
10926255736Sdavidch *      - GRC
10927255736Sdavidch *      - RBCN, RBCP
10928255736Sdavidch */
10929255736Sdavidchstatic void
10930255736Sdavidchbxe_process_kill_chip_reset(struct bxe_softc *sc,
10931255736Sdavidch                            uint8_t          global)
10932255736Sdavidch{
10933255736Sdavidch    uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10934255736Sdavidch    uint32_t global_bits2, stay_reset2;
10935255736Sdavidch
10936255736Sdavidch    /*
10937255736Sdavidch     * Bits that have to be set in reset_mask2 if we want to reset 'global'
10938255736Sdavidch     * (per chip) blocks.
10939255736Sdavidch     */
10940255736Sdavidch    global_bits2 =
10941255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10942255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10943255736Sdavidch
10944255736Sdavidch    /*
10945255736Sdavidch     * Don't reset the following blocks.
10946255736Sdavidch     * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10947255736Sdavidch     *            reset, as in 4 port device they might still be owned
10948255736Sdavidch     *            by the MCP (there is only one leader per path).
10949255736Sdavidch     */
10950255736Sdavidch    not_reset_mask1 =
10951255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_HC |
10952255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10953255736Sdavidch        MISC_REGISTERS_RESET_REG_1_RST_PXP;
10954255736Sdavidch
10955255736Sdavidch    not_reset_mask2 =
10956255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10957255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10958255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10959255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10960255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10961255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10962255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10963255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10964255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_ATC |
10965255736Sdavidch        MISC_REGISTERS_RESET_REG_2_PGLC |
10966255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10967255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10968255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10969255736Sdavidch        MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10970255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC0 |
10971255736Sdavidch        MISC_REGISTERS_RESET_REG_2_UMAC1;
10972255736Sdavidch
10973255736Sdavidch    /*
10974255736Sdavidch     * Keep the following blocks in reset:
10975255736Sdavidch     *  - all xxMACs are handled by the elink code.
10976255736Sdavidch     */
10977255736Sdavidch    stay_reset2 =
10978255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC |
10979255736Sdavidch        MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
10980255736Sdavidch
10981255736Sdavidch    /* Full reset masks according to the chip */
10982255736Sdavidch    reset_mask1 = 0xffffffff;
10983255736Sdavidch
10984255736Sdavidch    if (CHIP_IS_E1(sc))
10985255736Sdavidch        reset_mask2 = 0xffff;
10986255736Sdavidch    else if (CHIP_IS_E1H(sc))
10987255736Sdavidch        reset_mask2 = 0x1ffff;
10988255736Sdavidch    else if (CHIP_IS_E2(sc))
10989255736Sdavidch        reset_mask2 = 0xfffff;
10990255736Sdavidch    else /* CHIP_IS_E3 */
10991255736Sdavidch        reset_mask2 = 0x3ffffff;
10992255736Sdavidch
10993255736Sdavidch    /* Don't reset global blocks unless we need to */
10994255736Sdavidch    if (!global)
10995255736Sdavidch        reset_mask2 &= ~global_bits2;
10996255736Sdavidch
10997255736Sdavidch    /*
10998255736Sdavidch     * In case of attention in the QM, we need to reset PXP
10999255736Sdavidch     * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
11000255736Sdavidch     * because otherwise QM reset would release 'close the gates' shortly
11001255736Sdavidch     * before resetting the PXP, then the PSWRQ would send a write
11002255736Sdavidch     * request to PGLUE. Then when PXP is reset, PGLUE would try to
11003255736Sdavidch     * read the payload data from PSWWR, but PSWWR would not
11004255736Sdavidch     * respond. The write queue in PGLUE would stuck, dmae commands
11005255736Sdavidch     * would not return. Therefore it's important to reset the second
11006255736Sdavidch     * reset register (containing the
11007255736Sdavidch     * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
11008255736Sdavidch     * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
11009255736Sdavidch     * bit).
11010255736Sdavidch     */
11011255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
11012255736Sdavidch           reset_mask2 & (~not_reset_mask2));
11013255736Sdavidch
11014255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
11015255736Sdavidch           reset_mask1 & (~not_reset_mask1));
11016255736Sdavidch
11017255736Sdavidch    mb();
11018255736Sdavidch    wmb();
11019255736Sdavidch
11020255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
11021255736Sdavidch           reset_mask2 & (~stay_reset2));
11022255736Sdavidch
11023255736Sdavidch    mb();
11024255736Sdavidch    wmb();
11025255736Sdavidch
11026255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
11027255736Sdavidch    wmb();
11028255736Sdavidch}
11029255736Sdavidch
11030255736Sdavidchstatic int
11031255736Sdavidchbxe_process_kill(struct bxe_softc *sc,
11032255736Sdavidch                 uint8_t          global)
11033255736Sdavidch{
11034255736Sdavidch    int cnt = 1000;
11035255736Sdavidch    uint32_t val = 0;
11036255736Sdavidch    uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
11037255736Sdavidch    uint32_t tags_63_32 = 0;
11038255736Sdavidch
11039255736Sdavidch    /* Empty the Tetris buffer, wait for 1s */
11040255736Sdavidch    do {
11041255736Sdavidch        sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
11042255736Sdavidch        blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
11043255736Sdavidch        port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
11044255736Sdavidch        port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
11045255736Sdavidch        pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
11046255736Sdavidch        if (CHIP_IS_E3(sc)) {
11047255736Sdavidch            tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
11048255736Sdavidch        }
11049255736Sdavidch
11050255736Sdavidch        if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
11051255736Sdavidch            ((port_is_idle_0 & 0x1) == 0x1) &&
11052255736Sdavidch            ((port_is_idle_1 & 0x1) == 0x1) &&
11053255736Sdavidch            (pgl_exp_rom2 == 0xffffffff) &&
11054255736Sdavidch            (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11055255736Sdavidch            break;
11056255736Sdavidch        DELAY(1000);
11057255736Sdavidch    } while (cnt-- > 0);
11058255736Sdavidch
11059255736Sdavidch    if (cnt <= 0) {
11060255736Sdavidch        BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11061255736Sdavidch                  "are still outstanding read requests after 1s! "
11062255736Sdavidch                  "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11063255736Sdavidch                  "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11064255736Sdavidch              sr_cnt, blk_cnt, port_is_idle_0,
11065255736Sdavidch              port_is_idle_1, pgl_exp_rom2);
11066255736Sdavidch        return (-1);
11067255736Sdavidch    }
11068255736Sdavidch
11069255736Sdavidch    mb();
11070255736Sdavidch
11071255736Sdavidch    /* Close gates #2, #3 and #4 */
11072255736Sdavidch    bxe_set_234_gates(sc, TRUE);
11073255736Sdavidch
11074255736Sdavidch    /* Poll for IGU VQs for 57712 and newer chips */
11075255736Sdavidch    if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11076255736Sdavidch        return (-1);
11077255736Sdavidch    }
11078255736Sdavidch
11079255736Sdavidch    /* XXX indicate that "process kill" is in progress to MCP */
11080255736Sdavidch
11081255736Sdavidch    /* clear "unprepared" bit */
11082255736Sdavidch    REG_WR(sc, MISC_REG_UNPREPARED, 0);
11083255736Sdavidch    mb();
11084255736Sdavidch
11085255736Sdavidch    /* Make sure all is written to the chip before the reset */
11086255736Sdavidch    wmb();
11087255736Sdavidch
11088255736Sdavidch    /*
11089255736Sdavidch     * Wait for 1ms to empty GLUE and PCI-E core queues,
11090255736Sdavidch     * PSWHST, GRC and PSWRD Tetris buffer.
11091255736Sdavidch     */
11092255736Sdavidch    DELAY(1000);
11093255736Sdavidch
11094255736Sdavidch    /* Prepare to chip reset: */
11095255736Sdavidch    /* MCP */
11096255736Sdavidch    if (global) {
11097255736Sdavidch        bxe_reset_mcp_prep(sc, &val);
11098255736Sdavidch    }
11099255736Sdavidch
11100255736Sdavidch    /* PXP */
11101255736Sdavidch    bxe_pxp_prep(sc);
11102255736Sdavidch    mb();
11103255736Sdavidch
11104255736Sdavidch    /* reset the chip */
11105255736Sdavidch    bxe_process_kill_chip_reset(sc, global);
11106255736Sdavidch    mb();
11107255736Sdavidch
11108268854Sdavidcs    /* clear errors in PGB */
11109268854Sdavidcs    if (!CHIP_IS_E1(sc))
11110268854Sdavidcs        REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11111268854Sdavidcs
11112255736Sdavidch    /* Recover after reset: */
11113255736Sdavidch    /* MCP */
11114255736Sdavidch    if (global && bxe_reset_mcp_comp(sc, val)) {
11115255736Sdavidch        return (-1);
11116255736Sdavidch    }
11117255736Sdavidch
11118255736Sdavidch    /* XXX add resetting the NO_MCP mode DB here */
11119255736Sdavidch
11120255736Sdavidch    /* Open the gates #2, #3 and #4 */
11121255736Sdavidch    bxe_set_234_gates(sc, FALSE);
11122255736Sdavidch
11123255736Sdavidch    /* XXX
11124255736Sdavidch     * IGU/AEU preparation bring back the AEU/IGU to a reset state
11125255736Sdavidch     * re-enable attentions
11126255736Sdavidch     */
11127255736Sdavidch
11128255736Sdavidch    return (0);
11129255736Sdavidch}
11130255736Sdavidch
11131255736Sdavidchstatic int
11132255736Sdavidchbxe_leader_reset(struct bxe_softc *sc)
11133255736Sdavidch{
11134255736Sdavidch    int rc = 0;
11135255736Sdavidch    uint8_t global = bxe_reset_is_global(sc);
11136255736Sdavidch    uint32_t load_code;
11137255736Sdavidch
11138255736Sdavidch    /*
11139255736Sdavidch     * If not going to reset MCP, load "fake" driver to reset HW while
11140255736Sdavidch     * driver is owner of the HW.
11141255736Sdavidch     */
11142255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11143255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11144255736Sdavidch                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11145255736Sdavidch        if (!load_code) {
11146255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11147255736Sdavidch            rc = -1;
11148255736Sdavidch            goto exit_leader_reset;
11149255736Sdavidch        }
11150255736Sdavidch
11151255736Sdavidch        if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11152255736Sdavidch            (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11153255736Sdavidch            BLOGE(sc, "MCP unexpected response, aborting\n");
11154255736Sdavidch            rc = -1;
11155255736Sdavidch            goto exit_leader_reset2;
11156255736Sdavidch        }
11157255736Sdavidch
11158255736Sdavidch        load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11159255736Sdavidch        if (!load_code) {
11160255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
11161255736Sdavidch            rc = -1;
11162255736Sdavidch            goto exit_leader_reset2;
11163255736Sdavidch        }
11164255736Sdavidch    }
11165255736Sdavidch
11166255736Sdavidch    /* try to recover after the failure */
11167255736Sdavidch    if (bxe_process_kill(sc, global)) {
11168255736Sdavidch        BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11169255736Sdavidch        rc = -1;
11170255736Sdavidch        goto exit_leader_reset2;
11171255736Sdavidch    }
11172255736Sdavidch
11173255736Sdavidch    /*
11174255736Sdavidch     * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11175255736Sdavidch     * state.
11176255736Sdavidch     */
11177255736Sdavidch    bxe_set_reset_done(sc);
11178255736Sdavidch    if (global) {
11179255736Sdavidch        bxe_clear_reset_global(sc);
11180255736Sdavidch    }
11181255736Sdavidch
11182255736Sdavidchexit_leader_reset2:
11183255736Sdavidch
11184255736Sdavidch    /* unload "fake driver" if it was loaded */
11185255736Sdavidch    if (!global && !BXE_NOMCP(sc)) {
11186255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11187255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11188255736Sdavidch    }
11189255736Sdavidch
11190255736Sdavidchexit_leader_reset:
11191255736Sdavidch
11192255736Sdavidch    sc->is_leader = 0;
11193255736Sdavidch    bxe_release_leader_lock(sc);
11194255736Sdavidch
11195255736Sdavidch    mb();
11196255736Sdavidch    return (rc);
11197255736Sdavidch}
11198255736Sdavidch
11199255736Sdavidch/*
11200255736Sdavidch * prepare INIT transition, parameters configured:
11201255736Sdavidch *   - HC configuration
11202255736Sdavidch *   - Queue's CDU context
11203255736Sdavidch */
11204255736Sdavidchstatic void
11205255736Sdavidchbxe_pf_q_prep_init(struct bxe_softc               *sc,
11206255736Sdavidch                   struct bxe_fastpath            *fp,
11207255736Sdavidch                   struct ecore_queue_init_params *init_params)
11208255736Sdavidch{
11209255736Sdavidch    uint8_t cos;
11210255736Sdavidch    int cxt_index, cxt_offset;
11211255736Sdavidch
11212255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11213255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11214255736Sdavidch
11215255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11216255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11217255736Sdavidch
11218255736Sdavidch    /* HC rate */
11219255736Sdavidch    init_params->rx.hc_rate =
11220255736Sdavidch        sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11221255736Sdavidch    init_params->tx.hc_rate =
11222255736Sdavidch        sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11223255736Sdavidch
11224255736Sdavidch    /* FW SB ID */
11225255736Sdavidch    init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11226255736Sdavidch
11227255736Sdavidch    /* CQ index among the SB indices */
11228255736Sdavidch    init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11229255736Sdavidch    init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11230255736Sdavidch
11231255736Sdavidch    /* set maximum number of COSs supported by this queue */
11232255736Sdavidch    init_params->max_cos = sc->max_cos;
11233255736Sdavidch
11234255736Sdavidch    BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11235255736Sdavidch          fp->index, init_params->max_cos);
11236255736Sdavidch
11237255736Sdavidch    /* set the context pointers queue object */
11238255736Sdavidch    for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11239255736Sdavidch        /* XXX change index/cid here if ever support multiple tx CoS */
11240255736Sdavidch        /* fp->txdata[cos]->cid */
11241255736Sdavidch        cxt_index = fp->index / ILT_PAGE_CIDS;
11242255736Sdavidch        cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11243255736Sdavidch        init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11244255736Sdavidch    }
11245255736Sdavidch}
11246255736Sdavidch
11247255736Sdavidch/* set flags that are common for the Tx-only and not normal connections */
11248255736Sdavidchstatic unsigned long
11249255736Sdavidchbxe_get_common_flags(struct bxe_softc    *sc,
11250255736Sdavidch                     struct bxe_fastpath *fp,
11251255736Sdavidch                     uint8_t             zero_stats)
11252255736Sdavidch{
11253255736Sdavidch    unsigned long flags = 0;
11254255736Sdavidch
11255255736Sdavidch    /* PF driver will always initialize the Queue to an ACTIVE state */
11256255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11257255736Sdavidch
11258255736Sdavidch    /*
11259255736Sdavidch     * tx only connections collect statistics (on the same index as the
11260255736Sdavidch     * parent connection). The statistics are zeroed when the parent
11261255736Sdavidch     * connection is initialized.
11262255736Sdavidch     */
11263255736Sdavidch
11264255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11265255736Sdavidch    if (zero_stats) {
11266255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11267255736Sdavidch    }
11268255736Sdavidch
11269255736Sdavidch    /*
11270255736Sdavidch     * tx only connections can support tx-switching, though their
11271255736Sdavidch     * CoS-ness doesn't survive the loopback
11272255736Sdavidch     */
11273255736Sdavidch    if (sc->flags & BXE_TX_SWITCHING) {
11274255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11275255736Sdavidch    }
11276255736Sdavidch
11277255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11278255736Sdavidch
11279255736Sdavidch    return (flags);
11280255736Sdavidch}
11281255736Sdavidch
11282255736Sdavidchstatic unsigned long
11283255736Sdavidchbxe_get_q_flags(struct bxe_softc    *sc,
11284255736Sdavidch                struct bxe_fastpath *fp,
11285255736Sdavidch                uint8_t             leading)
11286255736Sdavidch{
11287255736Sdavidch    unsigned long flags = 0;
11288255736Sdavidch
11289255736Sdavidch    if (IS_MF_SD(sc)) {
11290255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11291255736Sdavidch    }
11292255736Sdavidch
11293266979Smarcel    if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11294255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11295255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11296255736Sdavidch    }
11297255736Sdavidch
11298255736Sdavidch    if (leading) {
11299255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11300255736Sdavidch        bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11301255736Sdavidch    }
11302255736Sdavidch
11303255736Sdavidch    bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11304255736Sdavidch
11305255736Sdavidch    /* merge with common flags */
11306255736Sdavidch    return (flags | bxe_get_common_flags(sc, fp, TRUE));
11307255736Sdavidch}
11308255736Sdavidch
11309255736Sdavidchstatic void
11310255736Sdavidchbxe_pf_q_prep_general(struct bxe_softc                  *sc,
11311255736Sdavidch                      struct bxe_fastpath               *fp,
11312255736Sdavidch                      struct ecore_general_setup_params *gen_init,
11313255736Sdavidch                      uint8_t                           cos)
11314255736Sdavidch{
11315255736Sdavidch    gen_init->stat_id = bxe_stats_id(fp);
11316255736Sdavidch    gen_init->spcl_id = fp->cl_id;
11317255736Sdavidch    gen_init->mtu = sc->mtu;
11318255736Sdavidch    gen_init->cos = cos;
11319255736Sdavidch}
11320255736Sdavidch
11321255736Sdavidchstatic void
11322255736Sdavidchbxe_pf_rx_q_prep(struct bxe_softc              *sc,
11323255736Sdavidch                 struct bxe_fastpath           *fp,
11324255736Sdavidch                 struct rxq_pause_params       *pause,
11325255736Sdavidch                 struct ecore_rxq_setup_params *rxq_init)
11326255736Sdavidch{
11327255736Sdavidch    uint8_t max_sge = 0;
11328255736Sdavidch    uint16_t sge_sz = 0;
11329255736Sdavidch    uint16_t tpa_agg_size = 0;
11330255736Sdavidch
11331283274Sdavidcs    pause->sge_th_lo = SGE_TH_LO(sc);
11332283274Sdavidcs    pause->sge_th_hi = SGE_TH_HI(sc);
11333255736Sdavidch
11334283274Sdavidcs    /* validate SGE ring has enough to cross high threshold */
11335283274Sdavidcs    if (sc->dropless_fc &&
11336255736Sdavidch            (pause->sge_th_hi + FW_PREFETCH_CNT) >
11337255736Sdavidch            (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11338283274Sdavidcs        BLOGW(sc, "sge ring threshold limit\n");
11339283274Sdavidcs    }
11340255736Sdavidch
11341283274Sdavidcs    /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11342283274Sdavidcs    tpa_agg_size = (2 * sc->mtu);
11343283274Sdavidcs    if (tpa_agg_size < sc->max_aggregation_size) {
11344283274Sdavidcs        tpa_agg_size = sc->max_aggregation_size;
11345283274Sdavidcs    }
11346255736Sdavidch
11347283274Sdavidcs    max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11348283274Sdavidcs    max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11349255736Sdavidch                   (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11350283274Sdavidcs    sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11351255736Sdavidch
11352255736Sdavidch    /* pause - not for e1 */
11353255736Sdavidch    if (!CHIP_IS_E1(sc)) {
11354255736Sdavidch        pause->bd_th_lo = BD_TH_LO(sc);
11355255736Sdavidch        pause->bd_th_hi = BD_TH_HI(sc);
11356255736Sdavidch
11357255736Sdavidch        pause->rcq_th_lo = RCQ_TH_LO(sc);
11358255736Sdavidch        pause->rcq_th_hi = RCQ_TH_HI(sc);
11359255736Sdavidch
11360255736Sdavidch        /* validate rings have enough entries to cross high thresholds */
11361255736Sdavidch        if (sc->dropless_fc &&
11362255736Sdavidch            pause->bd_th_hi + FW_PREFETCH_CNT >
11363255736Sdavidch            sc->rx_ring_size) {
11364255736Sdavidch            BLOGW(sc, "rx bd ring threshold limit\n");
11365255736Sdavidch        }
11366255736Sdavidch
11367255736Sdavidch        if (sc->dropless_fc &&
11368255736Sdavidch            pause->rcq_th_hi + FW_PREFETCH_CNT >
11369255736Sdavidch            RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11370255736Sdavidch            BLOGW(sc, "rcq ring threshold limit\n");
11371255736Sdavidch        }
11372255736Sdavidch
11373255736Sdavidch        pause->pri_map = 1;
11374255736Sdavidch    }
11375255736Sdavidch
11376255736Sdavidch    /* rxq setup */
11377255736Sdavidch    rxq_init->dscr_map   = fp->rx_dma.paddr;
11378255736Sdavidch    rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11379255736Sdavidch    rxq_init->rcq_map    = fp->rcq_dma.paddr;
11380255736Sdavidch    rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11381255736Sdavidch
11382255736Sdavidch    /*
11383255736Sdavidch     * This should be a maximum number of data bytes that may be
11384255736Sdavidch     * placed on the BD (not including paddings).
11385255736Sdavidch     */
11386255736Sdavidch    rxq_init->buf_sz = (fp->rx_buf_size -
11387255736Sdavidch                        IP_HEADER_ALIGNMENT_PADDING);
11388255736Sdavidch
11389255736Sdavidch    rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11390255736Sdavidch    rxq_init->tpa_agg_sz      = tpa_agg_size;
11391255736Sdavidch    rxq_init->sge_buf_sz      = sge_sz;
11392255736Sdavidch    rxq_init->max_sges_pkt    = max_sge;
11393255736Sdavidch    rxq_init->rss_engine_id   = SC_FUNC(sc);
11394255736Sdavidch    rxq_init->mcast_engine_id = SC_FUNC(sc);
11395255736Sdavidch
11396255736Sdavidch    /*
11397255736Sdavidch     * Maximum number or simultaneous TPA aggregation for this Queue.
11398255736Sdavidch     * For PF Clients it should be the maximum available number.
11399255736Sdavidch     * VF driver(s) may want to define it to a smaller value.
11400255736Sdavidch     */
11401255736Sdavidch    rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11402255736Sdavidch
11403255736Sdavidch    rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11404255736Sdavidch    rxq_init->fw_sb_id = fp->fw_sb_id;
11405255736Sdavidch
11406255736Sdavidch    rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11407255736Sdavidch
11408255736Sdavidch    /*
11409255736Sdavidch     * configure silent vlan removal
11410255736Sdavidch     * if multi function mode is afex, then mask default vlan
11411255736Sdavidch     */
11412255736Sdavidch    if (IS_MF_AFEX(sc)) {
11413255736Sdavidch        rxq_init->silent_removal_value =
11414255736Sdavidch            sc->devinfo.mf_info.afex_def_vlan_tag;
11415255736Sdavidch        rxq_init->silent_removal_mask = EVL_VLID_MASK;
11416255736Sdavidch    }
11417255736Sdavidch}
11418255736Sdavidch
11419255736Sdavidchstatic void
11420255736Sdavidchbxe_pf_tx_q_prep(struct bxe_softc              *sc,
11421255736Sdavidch                 struct bxe_fastpath           *fp,
11422255736Sdavidch                 struct ecore_txq_setup_params *txq_init,
11423255736Sdavidch                 uint8_t                       cos)
11424255736Sdavidch{
11425255736Sdavidch    /*
11426255736Sdavidch     * XXX If multiple CoS is ever supported then each fastpath structure
11427255736Sdavidch     * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11428255736Sdavidch     * fp->txdata[cos]->tx_dma.paddr;
11429255736Sdavidch     */
11430255736Sdavidch    txq_init->dscr_map     = fp->tx_dma.paddr;
11431255736Sdavidch    txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11432255736Sdavidch    txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11433255736Sdavidch    txq_init->fw_sb_id     = fp->fw_sb_id;
11434255736Sdavidch
11435255736Sdavidch    /*
11436255736Sdavidch     * set the TSS leading client id for TX classfication to the
11437255736Sdavidch     * leading RSS client id
11438255736Sdavidch     */
11439255736Sdavidch    txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11440255736Sdavidch}
11441255736Sdavidch
11442255736Sdavidch/*
11443255736Sdavidch * This function performs 2 steps in a queue state machine:
11444255736Sdavidch *   1) RESET->INIT
11445255736Sdavidch *   2) INIT->SETUP
11446255736Sdavidch */
11447255736Sdavidchstatic int
11448255736Sdavidchbxe_setup_queue(struct bxe_softc    *sc,
11449255736Sdavidch                struct bxe_fastpath *fp,
11450255736Sdavidch                uint8_t             leading)
11451255736Sdavidch{
11452255736Sdavidch    struct ecore_queue_state_params q_params = { NULL };
11453255736Sdavidch    struct ecore_queue_setup_params *setup_params =
11454255736Sdavidch                        &q_params.params.setup;
11455255736Sdavidch    int rc;
11456255736Sdavidch
11457255736Sdavidch    BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11458255736Sdavidch
11459255736Sdavidch    bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11460255736Sdavidch
11461255736Sdavidch    q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11462255736Sdavidch
11463255736Sdavidch    /* we want to wait for completion in this context */
11464255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11465255736Sdavidch
11466255736Sdavidch    /* prepare the INIT parameters */
11467255736Sdavidch    bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11468255736Sdavidch
11469255736Sdavidch    /* Set the command */
11470255736Sdavidch    q_params.cmd = ECORE_Q_CMD_INIT;
11471255736Sdavidch
11472255736Sdavidch    /* Change the state to INIT */
11473255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11474255736Sdavidch    if (rc) {
11475295830Sdavidcs        BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11476255736Sdavidch        return (rc);
11477255736Sdavidch    }
11478255736Sdavidch
11479255736Sdavidch    BLOGD(sc, DBG_LOAD, "init complete\n");
11480255736Sdavidch
11481255736Sdavidch    /* now move the Queue to the SETUP state */
11482255736Sdavidch    memset(setup_params, 0, sizeof(*setup_params));
11483255736Sdavidch
11484255736Sdavidch    /* set Queue flags */
11485255736Sdavidch    setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11486255736Sdavidch
11487255736Sdavidch    /* set general SETUP parameters */
11488255736Sdavidch    bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11489255736Sdavidch                          FIRST_TX_COS_INDEX);
11490255736Sdavidch
11491255736Sdavidch    bxe_pf_rx_q_prep(sc, fp,
11492255736Sdavidch                     &setup_params->pause_params,
11493255736Sdavidch                     &setup_params->rxq_params);
11494255736Sdavidch
11495255736Sdavidch    bxe_pf_tx_q_prep(sc, fp,
11496255736Sdavidch                     &setup_params->txq_params,
11497255736Sdavidch                     FIRST_TX_COS_INDEX);
11498255736Sdavidch
11499255736Sdavidch    /* Set the command */
11500255736Sdavidch    q_params.cmd = ECORE_Q_CMD_SETUP;
11501255736Sdavidch
11502255736Sdavidch    /* change the state to SETUP */
11503255736Sdavidch    rc = ecore_queue_state_change(sc, &q_params);
11504255736Sdavidch    if (rc) {
11505295830Sdavidcs        BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11506255736Sdavidch        return (rc);
11507255736Sdavidch    }
11508255736Sdavidch
11509255736Sdavidch    return (rc);
11510255736Sdavidch}
11511255736Sdavidch
11512255736Sdavidchstatic int
11513255736Sdavidchbxe_setup_leading(struct bxe_softc *sc)
11514255736Sdavidch{
11515255736Sdavidch    return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11516255736Sdavidch}
11517255736Sdavidch
11518255736Sdavidchstatic int
11519255736Sdavidchbxe_config_rss_pf(struct bxe_softc            *sc,
11520255736Sdavidch                  struct ecore_rss_config_obj *rss_obj,
11521255736Sdavidch                  uint8_t                     config_hash)
11522255736Sdavidch{
11523255736Sdavidch    struct ecore_config_rss_params params = { NULL };
11524255736Sdavidch    int i;
11525255736Sdavidch
11526255736Sdavidch    /*
11527255736Sdavidch     * Although RSS is meaningless when there is a single HW queue we
11528255736Sdavidch     * still need it enabled in order to have HW Rx hash generated.
11529255736Sdavidch     */
11530255736Sdavidch
11531255736Sdavidch    params.rss_obj = rss_obj;
11532255736Sdavidch
11533255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11534255736Sdavidch
11535255736Sdavidch    bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11536255736Sdavidch
11537255736Sdavidch    /* RSS configuration */
11538255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11539255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11540255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11541255736Sdavidch    bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11542255736Sdavidch    if (rss_obj->udp_rss_v4) {
11543255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11544255736Sdavidch    }
11545255736Sdavidch    if (rss_obj->udp_rss_v6) {
11546255736Sdavidch        bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11547255736Sdavidch    }
11548255736Sdavidch
11549255736Sdavidch    /* Hash bits */
11550255736Sdavidch    params.rss_result_mask = MULTI_MASK;
11551255736Sdavidch
11552255736Sdavidch    memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11553255736Sdavidch
11554255736Sdavidch    if (config_hash) {
11555255736Sdavidch        /* RSS keys */
11556255736Sdavidch        for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11557255736Sdavidch            params.rss_key[i] = arc4random();
11558255736Sdavidch        }
11559255736Sdavidch
11560255736Sdavidch        bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11561255736Sdavidch    }
11562255736Sdavidch
11563255736Sdavidch    return (ecore_config_rss(sc, &params));
11564255736Sdavidch}
11565255736Sdavidch
11566255736Sdavidchstatic int
11567255736Sdavidchbxe_config_rss_eth(struct bxe_softc *sc,
11568255736Sdavidch                   uint8_t          config_hash)
11569255736Sdavidch{
11570255736Sdavidch    return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11571255736Sdavidch}
11572255736Sdavidch
11573255736Sdavidchstatic int
11574255736Sdavidchbxe_init_rss_pf(struct bxe_softc *sc)
11575255736Sdavidch{
11576255736Sdavidch    uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11577255736Sdavidch    int i;
11578255736Sdavidch
11579255736Sdavidch    /*
11580255736Sdavidch     * Prepare the initial contents of the indirection table if
11581255736Sdavidch     * RSS is enabled
11582255736Sdavidch     */
11583255736Sdavidch    for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11584255736Sdavidch        sc->rss_conf_obj.ind_table[i] =
11585255736Sdavidch            (sc->fp->cl_id + (i % num_eth_queues));
11586255736Sdavidch    }
11587255736Sdavidch
11588255736Sdavidch    if (sc->udp_rss) {
11589255736Sdavidch        sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11590255736Sdavidch    }
11591255736Sdavidch
11592255736Sdavidch    /*
11593255736Sdavidch     * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11594255736Sdavidch     * per-port, so if explicit configuration is needed, do it only
11595255736Sdavidch     * for a PMF.
11596255736Sdavidch     *
11597255736Sdavidch     * For 57712 and newer it's a per-function configuration.
11598255736Sdavidch     */
11599255736Sdavidch    return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11600255736Sdavidch}
11601255736Sdavidch
11602255736Sdavidchstatic int
11603255736Sdavidchbxe_set_mac_one(struct bxe_softc          *sc,
11604255736Sdavidch                uint8_t                   *mac,
11605255736Sdavidch                struct ecore_vlan_mac_obj *obj,
11606255736Sdavidch                uint8_t                   set,
11607255736Sdavidch                int                       mac_type,
11608255736Sdavidch                unsigned long             *ramrod_flags)
11609255736Sdavidch{
11610255736Sdavidch    struct ecore_vlan_mac_ramrod_params ramrod_param;
11611255736Sdavidch    int rc;
11612255736Sdavidch
11613255736Sdavidch    memset(&ramrod_param, 0, sizeof(ramrod_param));
11614255736Sdavidch
11615255736Sdavidch    /* fill in general parameters */
11616255736Sdavidch    ramrod_param.vlan_mac_obj = obj;
11617255736Sdavidch    ramrod_param.ramrod_flags = *ramrod_flags;
11618255736Sdavidch
11619255736Sdavidch    /* fill a user request section if needed */
11620255736Sdavidch    if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11621255736Sdavidch        memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11622255736Sdavidch
11623255736Sdavidch        bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11624255736Sdavidch
11625255736Sdavidch        /* Set the command: ADD or DEL */
11626255736Sdavidch        ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11627255736Sdavidch                                            ECORE_VLAN_MAC_DEL;
11628255736Sdavidch    }
11629255736Sdavidch
11630255736Sdavidch    rc = ecore_config_vlan_mac(sc, &ramrod_param);
11631255736Sdavidch
11632255736Sdavidch    if (rc == ECORE_EXISTS) {
11633255736Sdavidch        BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11634255736Sdavidch        /* do not treat adding same MAC as error */
11635255736Sdavidch        rc = 0;
11636255736Sdavidch    } else if (rc < 0) {
11637255736Sdavidch        BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11638255736Sdavidch    }
11639255736Sdavidch
11640255736Sdavidch    return (rc);
11641255736Sdavidch}
11642255736Sdavidch
11643255736Sdavidchstatic int
11644255736Sdavidchbxe_set_eth_mac(struct bxe_softc *sc,
11645255736Sdavidch                uint8_t          set)
11646255736Sdavidch{
11647255736Sdavidch    unsigned long ramrod_flags = 0;
11648255736Sdavidch
11649255736Sdavidch    BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11650255736Sdavidch
11651255736Sdavidch    bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11652255736Sdavidch
11653255736Sdavidch    /* Eth MAC is set on RSS leading client (fp[0]) */
11654255736Sdavidch    return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11655255736Sdavidch                            &sc->sp_objs->mac_obj,
11656255736Sdavidch                            set, ECORE_ETH_MAC, &ramrod_flags));
11657255736Sdavidch}
11658255736Sdavidch
11659255736Sdavidchstatic int
11660255736Sdavidchbxe_get_cur_phy_idx(struct bxe_softc *sc)
11661255736Sdavidch{
11662255736Sdavidch    uint32_t sel_phy_idx = 0;
11663255736Sdavidch
11664255736Sdavidch    if (sc->link_params.num_phys <= 1) {
11665255736Sdavidch        return (ELINK_INT_PHY);
11666255736Sdavidch    }
11667255736Sdavidch
11668255736Sdavidch    if (sc->link_vars.link_up) {
11669255736Sdavidch        sel_phy_idx = ELINK_EXT_PHY1;
11670255736Sdavidch        /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11671255736Sdavidch        if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11672255736Sdavidch            (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11673255736Sdavidch             ELINK_SUPPORTED_FIBRE))
11674255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11675255736Sdavidch    } else {
11676255736Sdavidch        switch (elink_phy_selection(&sc->link_params)) {
11677255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11678255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11679255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11680255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY1;
11681255736Sdavidch               break;
11682255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11683255736Sdavidch        case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11684255736Sdavidch               sel_phy_idx = ELINK_EXT_PHY2;
11685255736Sdavidch               break;
11686255736Sdavidch        }
11687255736Sdavidch    }
11688255736Sdavidch
11689255736Sdavidch    return (sel_phy_idx);
11690255736Sdavidch}
11691255736Sdavidch
11692255736Sdavidchstatic int
11693255736Sdavidchbxe_get_link_cfg_idx(struct bxe_softc *sc)
11694255736Sdavidch{
11695255736Sdavidch    uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11696255736Sdavidch
11697255736Sdavidch    /*
11698255736Sdavidch     * The selected activated PHY is always after swapping (in case PHY
11699255736Sdavidch     * swapping is enabled). So when swapping is enabled, we need to reverse
11700255736Sdavidch     * the configuration
11701255736Sdavidch     */
11702255736Sdavidch
11703255736Sdavidch    if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11704255736Sdavidch        if (sel_phy_idx == ELINK_EXT_PHY1)
11705255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY2;
11706255736Sdavidch        else if (sel_phy_idx == ELINK_EXT_PHY2)
11707255736Sdavidch            sel_phy_idx = ELINK_EXT_PHY1;
11708255736Sdavidch    }
11709255736Sdavidch
11710255736Sdavidch    return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11711255736Sdavidch}
11712255736Sdavidch
11713255736Sdavidchstatic void
11714255736Sdavidchbxe_set_requested_fc(struct bxe_softc *sc)
11715255736Sdavidch{
11716255736Sdavidch    /*
11717255736Sdavidch     * Initialize link parameters structure variables
11718255736Sdavidch     * It is recommended to turn off RX FC for jumbo frames
11719255736Sdavidch     * for better performance
11720255736Sdavidch     */
11721255736Sdavidch    if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11722255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11723255736Sdavidch    } else {
11724255736Sdavidch        sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11725255736Sdavidch    }
11726255736Sdavidch}
11727255736Sdavidch
11728255736Sdavidchstatic void
11729255736Sdavidchbxe_calc_fc_adv(struct bxe_softc *sc)
11730255736Sdavidch{
11731255736Sdavidch    uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11732255736Sdavidch    switch (sc->link_vars.ieee_fc &
11733255736Sdavidch            MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11734255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
11735255736Sdavidch    default:
11736255736Sdavidch        sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11737255736Sdavidch                                           ADVERTISED_Pause);
11738255736Sdavidch        break;
11739255736Sdavidch
11740255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11741255736Sdavidch        sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11742255736Sdavidch                                          ADVERTISED_Pause);
11743255736Sdavidch        break;
11744255736Sdavidch
11745255736Sdavidch    case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11746255736Sdavidch        sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11747255736Sdavidch        break;
11748255736Sdavidch    }
11749255736Sdavidch}
11750255736Sdavidch
11751255736Sdavidchstatic uint16_t
11752255736Sdavidchbxe_get_mf_speed(struct bxe_softc *sc)
11753255736Sdavidch{
11754255736Sdavidch    uint16_t line_speed = sc->link_vars.line_speed;
11755255736Sdavidch    if (IS_MF(sc)) {
11756255736Sdavidch        uint16_t maxCfg =
11757255736Sdavidch            bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11758255736Sdavidch
11759255736Sdavidch        /* calculate the current MAX line speed limit for the MF devices */
11760255736Sdavidch        if (IS_MF_SI(sc)) {
11761255736Sdavidch            line_speed = (line_speed * maxCfg) / 100;
11762255736Sdavidch        } else { /* SD mode */
11763255736Sdavidch            uint16_t vn_max_rate = maxCfg * 100;
11764255736Sdavidch
11765255736Sdavidch            if (vn_max_rate < line_speed) {
11766255736Sdavidch                line_speed = vn_max_rate;
11767255736Sdavidch            }
11768255736Sdavidch        }
11769255736Sdavidch    }
11770255736Sdavidch
11771255736Sdavidch    return (line_speed);
11772255736Sdavidch}
11773255736Sdavidch
11774255736Sdavidchstatic void
11775255736Sdavidchbxe_fill_report_data(struct bxe_softc            *sc,
11776255736Sdavidch                     struct bxe_link_report_data *data)
11777255736Sdavidch{
11778255736Sdavidch    uint16_t line_speed = bxe_get_mf_speed(sc);
11779255736Sdavidch
11780255736Sdavidch    memset(data, 0, sizeof(*data));
11781255736Sdavidch
11782255736Sdavidch    /* fill the report data with the effective line speed */
11783255736Sdavidch    data->line_speed = line_speed;
11784255736Sdavidch
11785255736Sdavidch    /* Link is down */
11786255736Sdavidch    if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11787255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11788255736Sdavidch    }
11789255736Sdavidch
11790255736Sdavidch    /* Full DUPLEX */
11791255736Sdavidch    if (sc->link_vars.duplex == DUPLEX_FULL) {
11792255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11793255736Sdavidch    }
11794255736Sdavidch
11795255736Sdavidch    /* Rx Flow Control is ON */
11796255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11797255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11798255736Sdavidch    }
11799255736Sdavidch
11800255736Sdavidch    /* Tx Flow Control is ON */
11801255736Sdavidch    if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11802255736Sdavidch        bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11803255736Sdavidch    }
11804255736Sdavidch}
11805255736Sdavidch
11806255736Sdavidch/* report link status to OS, should be called under phy_lock */
11807255736Sdavidchstatic void
11808255736Sdavidchbxe_link_report_locked(struct bxe_softc *sc)
11809255736Sdavidch{
11810255736Sdavidch    struct bxe_link_report_data cur_data;
11811255736Sdavidch
11812255736Sdavidch    /* reread mf_cfg */
11813255736Sdavidch    if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11814255736Sdavidch        bxe_read_mf_cfg(sc);
11815255736Sdavidch    }
11816255736Sdavidch
11817255736Sdavidch    /* Read the current link report info */
11818255736Sdavidch    bxe_fill_report_data(sc, &cur_data);
11819255736Sdavidch
11820255736Sdavidch    /* Don't report link down or exactly the same link status twice */
11821255736Sdavidch    if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11822255736Sdavidch        (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11823255736Sdavidch                      &sc->last_reported_link.link_report_flags) &&
11824255736Sdavidch         bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11825255736Sdavidch                      &cur_data.link_report_flags))) {
11826255736Sdavidch        return;
11827255736Sdavidch    }
11828255736Sdavidch
11829255736Sdavidch    sc->link_cnt++;
11830255736Sdavidch
11831255736Sdavidch    /* report new link params and remember the state for the next time */
11832255736Sdavidch    memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11833255736Sdavidch
11834255736Sdavidch    if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11835255736Sdavidch                     &cur_data.link_report_flags)) {
11836270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_DOWN);
11837255736Sdavidch        BLOGI(sc, "NIC Link is Down\n");
11838255736Sdavidch    } else {
11839255736Sdavidch        const char *duplex;
11840255736Sdavidch        const char *flow;
11841255736Sdavidch
11842255736Sdavidch        if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11843255736Sdavidch                                   &cur_data.link_report_flags)) {
11844255736Sdavidch            duplex = "full";
11845255736Sdavidch        } else {
11846255736Sdavidch            duplex = "half";
11847255736Sdavidch        }
11848255736Sdavidch
11849255736Sdavidch        /*
11850255736Sdavidch         * Handle the FC at the end so that only these flags would be
11851255736Sdavidch         * possibly set. This way we may easily check if there is no FC
11852255736Sdavidch         * enabled.
11853255736Sdavidch         */
11854255736Sdavidch        if (cur_data.link_report_flags) {
11855255736Sdavidch            if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11856255736Sdavidch                             &cur_data.link_report_flags) &&
11857255736Sdavidch                bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11858255736Sdavidch                             &cur_data.link_report_flags)) {
11859255736Sdavidch                flow = "ON - receive & transmit";
11860255736Sdavidch            } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11861255736Sdavidch                                    &cur_data.link_report_flags) &&
11862255736Sdavidch                       !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11863255736Sdavidch                                     &cur_data.link_report_flags)) {
11864255736Sdavidch                flow = "ON - receive";
11865255736Sdavidch            } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11866255736Sdavidch                                     &cur_data.link_report_flags) &&
11867255736Sdavidch                       bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11868255736Sdavidch                                    &cur_data.link_report_flags)) {
11869255736Sdavidch                flow = "ON - transmit";
11870255736Sdavidch            } else {
11871255736Sdavidch                flow = "none"; /* possible? */
11872255736Sdavidch            }
11873255736Sdavidch        } else {
11874255736Sdavidch            flow = "none";
11875255736Sdavidch        }
11876255736Sdavidch
11877270876Sglebius        if_link_state_change(sc->ifp, LINK_STATE_UP);
11878255736Sdavidch        BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11879255736Sdavidch              cur_data.line_speed, duplex, flow);
11880255736Sdavidch    }
11881255736Sdavidch}
11882255736Sdavidch
11883255736Sdavidchstatic void
11884255736Sdavidchbxe_link_report(struct bxe_softc *sc)
11885255736Sdavidch{
11886284335Sdavidcs    bxe_acquire_phy_lock(sc);
11887255736Sdavidch    bxe_link_report_locked(sc);
11888284335Sdavidcs    bxe_release_phy_lock(sc);
11889255736Sdavidch}
11890255736Sdavidch
11891255736Sdavidchstatic void
11892255736Sdavidchbxe_link_status_update(struct bxe_softc *sc)
11893255736Sdavidch{
11894255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
11895255736Sdavidch        return;
11896255736Sdavidch    }
11897255736Sdavidch
11898255736Sdavidch    if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11899255736Sdavidch        elink_link_status_update(&sc->link_params, &sc->link_vars);
11900255736Sdavidch    } else {
11901255736Sdavidch        sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11902255736Sdavidch                                  ELINK_SUPPORTED_10baseT_Full |
11903255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Half |
11904255736Sdavidch                                  ELINK_SUPPORTED_100baseT_Full |
11905255736Sdavidch                                  ELINK_SUPPORTED_1000baseT_Full |
11906255736Sdavidch                                  ELINK_SUPPORTED_2500baseX_Full |
11907255736Sdavidch                                  ELINK_SUPPORTED_10000baseT_Full |
11908255736Sdavidch                                  ELINK_SUPPORTED_TP |
11909255736Sdavidch                                  ELINK_SUPPORTED_FIBRE |
11910255736Sdavidch                                  ELINK_SUPPORTED_Autoneg |
11911255736Sdavidch                                  ELINK_SUPPORTED_Pause |
11912255736Sdavidch                                  ELINK_SUPPORTED_Asym_Pause);
11913255736Sdavidch        sc->port.advertising[0] = sc->port.supported[0];
11914255736Sdavidch
11915255736Sdavidch        sc->link_params.sc                = sc;
11916255736Sdavidch        sc->link_params.port              = SC_PORT(sc);
11917255736Sdavidch        sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11918255736Sdavidch        sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11919255736Sdavidch        sc->link_params.req_line_speed[0] = SPEED_10000;
11920255736Sdavidch        sc->link_params.speed_cap_mask[0] = 0x7f0000;
11921255736Sdavidch        sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11922255736Sdavidch
11923255736Sdavidch        if (CHIP_REV_IS_FPGA(sc)) {
11924255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11925255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_1000;
11926255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11927255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11928255736Sdavidch        } else {
11929255736Sdavidch            sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11930255736Sdavidch            sc->link_vars.line_speed  = ELINK_SPEED_10000;
11931255736Sdavidch            sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11932255736Sdavidch                                         LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11933255736Sdavidch        }
11934255736Sdavidch
11935255736Sdavidch        sc->link_vars.link_up = 1;
11936255736Sdavidch
11937255736Sdavidch        sc->link_vars.duplex    = DUPLEX_FULL;
11938255736Sdavidch        sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11939255736Sdavidch
11940255736Sdavidch        if (IS_PF(sc)) {
11941255736Sdavidch            REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11942255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11943255736Sdavidch            bxe_link_report(sc);
11944255736Sdavidch        }
11945255736Sdavidch    }
11946255736Sdavidch
11947255736Sdavidch    if (IS_PF(sc)) {
11948255736Sdavidch        if (sc->link_vars.link_up) {
11949255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11950255736Sdavidch        } else {
11951255736Sdavidch            bxe_stats_handle(sc, STATS_EVENT_STOP);
11952255736Sdavidch        }
11953255736Sdavidch        bxe_link_report(sc);
11954255736Sdavidch    } else {
11955255736Sdavidch        bxe_link_report(sc);
11956255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11957255736Sdavidch    }
11958255736Sdavidch}
11959255736Sdavidch
11960255736Sdavidchstatic int
11961255736Sdavidchbxe_initial_phy_init(struct bxe_softc *sc,
11962255736Sdavidch                     int              load_mode)
11963255736Sdavidch{
11964255736Sdavidch    int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
11965255736Sdavidch    uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
11966255736Sdavidch    struct elink_params *lp = &sc->link_params;
11967255736Sdavidch
11968255736Sdavidch    bxe_set_requested_fc(sc);
11969255736Sdavidch
11970255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
11971255736Sdavidch        uint32_t bond = CHIP_BOND_ID(sc);
11972255736Sdavidch        uint32_t feat = 0;
11973255736Sdavidch
11974255736Sdavidch        if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
11975255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11976255736Sdavidch        } else if (bond & 0x4) {
11977255736Sdavidch            if (CHIP_IS_E3(sc)) {
11978255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
11979255736Sdavidch            } else {
11980255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11981255736Sdavidch            }
11982255736Sdavidch        } else if (bond & 0x8) {
11983255736Sdavidch            if (CHIP_IS_E3(sc)) {
11984255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
11985255736Sdavidch            } else {
11986255736Sdavidch                feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11987255736Sdavidch            }
11988255736Sdavidch        }
11989255736Sdavidch
11990255736Sdavidch        /* disable EMAC for E3 and above */
11991255736Sdavidch        if (bond & 0x2) {
11992255736Sdavidch            feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11993255736Sdavidch        }
11994255736Sdavidch
11995255736Sdavidch        sc->link_params.feature_config_flags |= feat;
11996255736Sdavidch    }
11997255736Sdavidch
11998284335Sdavidcs    bxe_acquire_phy_lock(sc);
11999255736Sdavidch
12000255736Sdavidch    if (load_mode == LOAD_DIAG) {
12001255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_XGXS;
12002255736Sdavidch        /* Prefer doing PHY loopback at 10G speed, if possible */
12003255736Sdavidch        if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
12004255736Sdavidch            if (lp->speed_cap_mask[cfg_idx] &
12005255736Sdavidch                PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
12006255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
12007255736Sdavidch            } else {
12008255736Sdavidch                lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
12009255736Sdavidch            }
12010255736Sdavidch        }
12011255736Sdavidch    }
12012255736Sdavidch
12013255736Sdavidch    if (load_mode == LOAD_LOOPBACK_EXT) {
12014255736Sdavidch        lp->loopback_mode = ELINK_LOOPBACK_EXT;
12015255736Sdavidch    }
12016255736Sdavidch
12017255736Sdavidch    rc = elink_phy_init(&sc->link_params, &sc->link_vars);
12018255736Sdavidch
12019284335Sdavidcs    bxe_release_phy_lock(sc);
12020255736Sdavidch
12021255736Sdavidch    bxe_calc_fc_adv(sc);
12022255736Sdavidch
12023255736Sdavidch    if (sc->link_vars.link_up) {
12024255736Sdavidch        bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12025255736Sdavidch        bxe_link_report(sc);
12026255736Sdavidch    }
12027255736Sdavidch
12028255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12029255736Sdavidch        bxe_periodic_start(sc);
12030255736Sdavidch    }
12031255736Sdavidch
12032255736Sdavidch    sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
12033255736Sdavidch    return (rc);
12034255736Sdavidch}
12035255736Sdavidch
12036255736Sdavidch/* must be called under IF_ADDR_LOCK */
12037284651Sdavidcs
12038255736Sdavidchstatic int
12039284651Sdavidcsbxe_set_mc_list(struct bxe_softc *sc)
12040255736Sdavidch{
12041284651Sdavidcs    struct ecore_mcast_ramrod_params rparam = { NULL };
12042284651Sdavidcs    int rc = 0;
12043255736Sdavidch    int mc_count = 0;
12044266979Smarcel    int mcnt, i;
12045284651Sdavidcs    struct ecore_mcast_list_elem *mc_mac, *mc_mac_start;
12046266979Smarcel    unsigned char *mta;
12047284651Sdavidcs    if_t ifp = sc->ifp;
12048255736Sdavidch
12049266979Smarcel    mc_count = if_multiaddr_count(ifp, -1);/* XXX they don't have a limit */
12050284651Sdavidcs    if (!mc_count)
12051255736Sdavidch        return (0);
12052255736Sdavidch
12053266979Smarcel    mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN *
12054266979Smarcel            mc_count, M_DEVBUF, M_NOWAIT);
12055266979Smarcel
12056266979Smarcel    if(mta == NULL) {
12057266979Smarcel        BLOGE(sc, "Failed to allocate temp mcast list\n");
12058266979Smarcel        return (-1);
12059266979Smarcel    }
12060284470Sdavidcs    bzero(mta, (sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count));
12061266979Smarcel
12062284651Sdavidcs    mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF, (M_NOWAIT | M_ZERO));
12063284651Sdavidcs    mc_mac_start = mc_mac;
12064284651Sdavidcs
12065255736Sdavidch    if (!mc_mac) {
12066266979Smarcel        free(mta, M_DEVBUF);
12067255736Sdavidch        BLOGE(sc, "Failed to allocate temp mcast list\n");
12068255736Sdavidch        return (-1);
12069255736Sdavidch    }
12070284470Sdavidcs    bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12071255736Sdavidch
12072284651Sdavidcs    /* mta and mcnt not expected to be  different */
12073284651Sdavidcs    if_multiaddr_array(ifp, mta, &mcnt, mc_count);
12074284651Sdavidcs
12075284651Sdavidcs
12076284651Sdavidcs    rparam.mcast_obj = &sc->mcast_obj;
12077284651Sdavidcs    ECORE_LIST_INIT(&rparam.mcast_list);
12078284651Sdavidcs
12079266979Smarcel    for(i=0; i< mcnt; i++) {
12080255736Sdavidch
12081284651Sdavidcs        mc_mac->mac = (uint8_t *)(mta + (i * ETHER_ADDR_LEN));
12082284651Sdavidcs        ECORE_LIST_PUSH_TAIL(&mc_mac->link, &rparam.mcast_list);
12083255736Sdavidch
12084255736Sdavidch        BLOGD(sc, DBG_LOAD,
12085255736Sdavidch              "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
12086255736Sdavidch              mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12087255736Sdavidch              mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
12088255736Sdavidch
12089255736Sdavidch        mc_mac++;
12090255736Sdavidch    }
12091284651Sdavidcs    rparam.mcast_list_len = mc_count;
12092255736Sdavidch
12093255736Sdavidch    BXE_MCAST_LOCK(sc);
12094255736Sdavidch
12095255736Sdavidch    /* first, clear all configured multicast MACs */
12096255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12097255736Sdavidch    if (rc < 0) {
12098255736Sdavidch        BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12099284470Sdavidcs        BXE_MCAST_UNLOCK(sc);
12100284651Sdavidcs    	free(mc_mac_start, M_DEVBUF);
12101284651Sdavidcs        free(mta, M_DEVBUF);
12102255736Sdavidch        return (rc);
12103255736Sdavidch    }
12104255736Sdavidch
12105255736Sdavidch    /* Now add the new MACs */
12106255736Sdavidch    rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12107255736Sdavidch    if (rc < 0) {
12108255736Sdavidch        BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12109255736Sdavidch    }
12110255736Sdavidch
12111255736Sdavidch    BXE_MCAST_UNLOCK(sc);
12112255736Sdavidch
12113284651Sdavidcs    free(mc_mac_start, M_DEVBUF);
12114284651Sdavidcs    free(mta, M_DEVBUF);
12115284651Sdavidcs
12116255736Sdavidch    return (rc);
12117255736Sdavidch}
12118255736Sdavidch
12119255736Sdavidchstatic int
12120255736Sdavidchbxe_set_uc_list(struct bxe_softc *sc)
12121255736Sdavidch{
12122266979Smarcel    if_t ifp = sc->ifp;
12123255736Sdavidch    struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12124255736Sdavidch    struct ifaddr *ifa;
12125255736Sdavidch    unsigned long ramrod_flags = 0;
12126255736Sdavidch    int rc;
12127255736Sdavidch
12128255736Sdavidch#if __FreeBSD_version < 800000
12129255736Sdavidch    IF_ADDR_LOCK(ifp);
12130255736Sdavidch#else
12131270876Sglebius    if_addr_rlock(ifp);
12132255736Sdavidch#endif
12133255736Sdavidch
12134255736Sdavidch    /* first schedule a cleanup up of old configuration */
12135255736Sdavidch    rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12136255736Sdavidch    if (rc < 0) {
12137255736Sdavidch        BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12138255736Sdavidch#if __FreeBSD_version < 800000
12139255736Sdavidch        IF_ADDR_UNLOCK(ifp);
12140255736Sdavidch#else
12141270876Sglebius        if_addr_runlock(ifp);
12142255736Sdavidch#endif
12143255736Sdavidch        return (rc);
12144255736Sdavidch    }
12145255736Sdavidch
12146266979Smarcel    ifa = if_getifaddr(ifp); /* XXX Is this structure */
12147255736Sdavidch    while (ifa) {
12148255736Sdavidch        if (ifa->ifa_addr->sa_family != AF_LINK) {
12149255736Sdavidch            ifa = TAILQ_NEXT(ifa, ifa_link);
12150255736Sdavidch            continue;
12151255736Sdavidch        }
12152255736Sdavidch
12153255736Sdavidch        rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12154255736Sdavidch                             mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12155255736Sdavidch        if (rc == -EEXIST) {
12156255736Sdavidch            BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12157255736Sdavidch            /* do not treat adding same MAC as an error */
12158255736Sdavidch            rc = 0;
12159255736Sdavidch        } else if (rc < 0) {
12160255736Sdavidch            BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12161255736Sdavidch#if __FreeBSD_version < 800000
12162255736Sdavidch            IF_ADDR_UNLOCK(ifp);
12163255736Sdavidch#else
12164270876Sglebius            if_addr_runlock(ifp);
12165255736Sdavidch#endif
12166255736Sdavidch            return (rc);
12167255736Sdavidch        }
12168255736Sdavidch
12169255736Sdavidch        ifa = TAILQ_NEXT(ifa, ifa_link);
12170255736Sdavidch    }
12171255736Sdavidch
12172255736Sdavidch#if __FreeBSD_version < 800000
12173255736Sdavidch    IF_ADDR_UNLOCK(ifp);
12174255736Sdavidch#else
12175270876Sglebius    if_addr_runlock(ifp);
12176255736Sdavidch#endif
12177255736Sdavidch
12178255736Sdavidch    /* Execute the pending commands */
12179255736Sdavidch    bit_set(&ramrod_flags, RAMROD_CONT);
12180255736Sdavidch    return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12181255736Sdavidch                            ECORE_UC_LIST_MAC, &ramrod_flags));
12182255736Sdavidch}
12183255736Sdavidch
12184255736Sdavidchstatic void
12185284335Sdavidcsbxe_set_rx_mode(struct bxe_softc *sc)
12186255736Sdavidch{
12187266979Smarcel    if_t ifp = sc->ifp;
12188255736Sdavidch    uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12189255736Sdavidch
12190255736Sdavidch    if (sc->state != BXE_STATE_OPEN) {
12191255736Sdavidch        BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12192255736Sdavidch        return;
12193255736Sdavidch    }
12194255736Sdavidch
12195266979Smarcel    BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12196255736Sdavidch
12197266979Smarcel    if (if_getflags(ifp) & IFF_PROMISC) {
12198255736Sdavidch        rx_mode = BXE_RX_MODE_PROMISC;
12199266979Smarcel    } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12200266979Smarcel               ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12201255736Sdavidch                CHIP_IS_E1(sc))) {
12202255736Sdavidch        rx_mode = BXE_RX_MODE_ALLMULTI;
12203255736Sdavidch    } else {
12204255736Sdavidch        if (IS_PF(sc)) {
12205255736Sdavidch            /* some multicasts */
12206255736Sdavidch            if (bxe_set_mc_list(sc) < 0) {
12207255736Sdavidch                rx_mode = BXE_RX_MODE_ALLMULTI;
12208255736Sdavidch            }
12209255736Sdavidch            if (bxe_set_uc_list(sc) < 0) {
12210255736Sdavidch                rx_mode = BXE_RX_MODE_PROMISC;
12211255736Sdavidch            }
12212255736Sdavidch        }
12213255736Sdavidch    }
12214255736Sdavidch
12215255736Sdavidch    sc->rx_mode = rx_mode;
12216255736Sdavidch
12217255736Sdavidch    /* schedule the rx_mode command */
12218255736Sdavidch    if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12219255736Sdavidch        BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12220255736Sdavidch        bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12221255736Sdavidch        return;
12222255736Sdavidch    }
12223255736Sdavidch
12224255736Sdavidch    if (IS_PF(sc)) {
12225255736Sdavidch        bxe_set_storm_rx_mode(sc);
12226255736Sdavidch    }
12227255736Sdavidch}
12228255736Sdavidch
12229255736Sdavidch
12230255736Sdavidch/* update flags in shmem */
12231255736Sdavidchstatic void
12232255736Sdavidchbxe_update_drv_flags(struct bxe_softc *sc,
12233255736Sdavidch                     uint32_t         flags,
12234255736Sdavidch                     uint32_t         set)
12235255736Sdavidch{
12236255736Sdavidch    uint32_t drv_flags;
12237255736Sdavidch
12238255736Sdavidch    if (SHMEM2_HAS(sc, drv_flags)) {
12239255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12240255736Sdavidch        drv_flags = SHMEM2_RD(sc, drv_flags);
12241255736Sdavidch
12242255736Sdavidch        if (set) {
12243255736Sdavidch            SET_FLAGS(drv_flags, flags);
12244255736Sdavidch        } else {
12245255736Sdavidch            RESET_FLAGS(drv_flags, flags);
12246255736Sdavidch        }
12247255736Sdavidch
12248255736Sdavidch        SHMEM2_WR(sc, drv_flags, drv_flags);
12249255736Sdavidch        BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12250255736Sdavidch
12251255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12252255736Sdavidch    }
12253255736Sdavidch}
12254255736Sdavidch
12255255736Sdavidch/* periodic timer callout routine, only runs when the interface is up */
12256255736Sdavidch
12257255736Sdavidchstatic void
12258255736Sdavidchbxe_periodic_callout_func(void *xsc)
12259255736Sdavidch{
12260255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12261255736Sdavidch    int i;
12262255736Sdavidch
12263255736Sdavidch    if (!BXE_CORE_TRYLOCK(sc)) {
12264255736Sdavidch        /* just bail and try again next time */
12265255736Sdavidch
12266255736Sdavidch        if ((sc->state == BXE_STATE_OPEN) &&
12267255736Sdavidch            (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12268255736Sdavidch            /* schedule the next periodic callout */
12269255736Sdavidch            callout_reset(&sc->periodic_callout, hz,
12270255736Sdavidch                          bxe_periodic_callout_func, sc);
12271255736Sdavidch        }
12272255736Sdavidch
12273255736Sdavidch        return;
12274255736Sdavidch    }
12275255736Sdavidch
12276255736Sdavidch    if ((sc->state != BXE_STATE_OPEN) ||
12277255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12278255736Sdavidch        BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12279255736Sdavidch        BXE_CORE_UNLOCK(sc);
12280255736Sdavidch        return;
12281255736Sdavidch    }
12282255736Sdavidch
12283255736Sdavidch    /* Check for TX timeouts on any fastpath. */
12284255736Sdavidch    FOR_EACH_QUEUE(sc, i) {
12285255736Sdavidch        if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12286255736Sdavidch            /* Ruh-Roh, chip was reset! */
12287255736Sdavidch            break;
12288255736Sdavidch        }
12289255736Sdavidch    }
12290255736Sdavidch
12291255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
12292255736Sdavidch        /*
12293255736Sdavidch         * This barrier is needed to ensure the ordering between the writing
12294255736Sdavidch         * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12295255736Sdavidch         * the reading here.
12296255736Sdavidch         */
12297255736Sdavidch        mb();
12298255736Sdavidch        if (sc->port.pmf) {
12299284335Sdavidcs	    bxe_acquire_phy_lock(sc);
12300255736Sdavidch            elink_period_func(&sc->link_params, &sc->link_vars);
12301284335Sdavidcs	    bxe_release_phy_lock(sc);
12302255736Sdavidch        }
12303255736Sdavidch    }
12304255736Sdavidch
12305284335Sdavidcs    if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12306255736Sdavidch        int mb_idx = SC_FW_MB_IDX(sc);
12307255736Sdavidch        uint32_t drv_pulse;
12308255736Sdavidch        uint32_t mcp_pulse;
12309255736Sdavidch
12310255736Sdavidch        ++sc->fw_drv_pulse_wr_seq;
12311255736Sdavidch        sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12312255736Sdavidch
12313255736Sdavidch        drv_pulse = sc->fw_drv_pulse_wr_seq;
12314255736Sdavidch        bxe_drv_pulse(sc);
12315255736Sdavidch
12316255736Sdavidch        mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12317255736Sdavidch                     MCP_PULSE_SEQ_MASK);
12318255736Sdavidch
12319255736Sdavidch        /*
12320255736Sdavidch         * The delta between driver pulse and mcp response should
12321255736Sdavidch         * be 1 (before mcp response) or 0 (after mcp response).
12322255736Sdavidch         */
12323255736Sdavidch        if ((drv_pulse != mcp_pulse) &&
12324255736Sdavidch            (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12325255736Sdavidch            /* someone lost a heartbeat... */
12326255736Sdavidch            BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12327255736Sdavidch                  drv_pulse, mcp_pulse);
12328255736Sdavidch        }
12329255736Sdavidch    }
12330255736Sdavidch
12331255736Sdavidch    /* state is BXE_STATE_OPEN */
12332255736Sdavidch    bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12333255736Sdavidch
12334255736Sdavidch    BXE_CORE_UNLOCK(sc);
12335255736Sdavidch
12336255736Sdavidch    if ((sc->state == BXE_STATE_OPEN) &&
12337255736Sdavidch        (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12338255736Sdavidch        /* schedule the next periodic callout */
12339255736Sdavidch        callout_reset(&sc->periodic_callout, hz,
12340255736Sdavidch                      bxe_periodic_callout_func, sc);
12341255736Sdavidch    }
12342255736Sdavidch}
12343255736Sdavidch
12344255736Sdavidchstatic void
12345255736Sdavidchbxe_periodic_start(struct bxe_softc *sc)
12346255736Sdavidch{
12347255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12348255736Sdavidch    callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12349255736Sdavidch}
12350255736Sdavidch
12351255736Sdavidchstatic void
12352255736Sdavidchbxe_periodic_stop(struct bxe_softc *sc)
12353255736Sdavidch{
12354255736Sdavidch    atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12355255736Sdavidch    callout_drain(&sc->periodic_callout);
12356255736Sdavidch}
12357255736Sdavidch
12358255736Sdavidch/* start the controller */
12359255736Sdavidchstatic __noinline int
12360255736Sdavidchbxe_nic_load(struct bxe_softc *sc,
12361255736Sdavidch             int              load_mode)
12362255736Sdavidch{
12363255736Sdavidch    uint32_t val;
12364255736Sdavidch    int load_code = 0;
12365255736Sdavidch    int i, rc = 0;
12366255736Sdavidch
12367255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12368255736Sdavidch
12369255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12370255736Sdavidch
12371255736Sdavidch    sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12372255736Sdavidch
12373255736Sdavidch    if (IS_PF(sc)) {
12374255736Sdavidch        /* must be called before memory allocation and HW init */
12375255736Sdavidch        bxe_ilt_set_info(sc);
12376255736Sdavidch    }
12377255736Sdavidch
12378255736Sdavidch    sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12379255736Sdavidch
12380255736Sdavidch    bxe_set_fp_rx_buf_size(sc);
12381255736Sdavidch
12382255736Sdavidch    if (bxe_alloc_fp_buffers(sc) != 0) {
12383255736Sdavidch        BLOGE(sc, "Failed to allocate fastpath memory\n");
12384255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12385255736Sdavidch        rc = ENOMEM;
12386255736Sdavidch        goto bxe_nic_load_error0;
12387255736Sdavidch    }
12388255736Sdavidch
12389255736Sdavidch    if (bxe_alloc_mem(sc) != 0) {
12390255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12391255736Sdavidch        rc = ENOMEM;
12392255736Sdavidch        goto bxe_nic_load_error0;
12393255736Sdavidch    }
12394255736Sdavidch
12395255736Sdavidch    if (bxe_alloc_fw_stats_mem(sc) != 0) {
12396255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12397255736Sdavidch        rc = ENOMEM;
12398255736Sdavidch        goto bxe_nic_load_error0;
12399255736Sdavidch    }
12400255736Sdavidch
12401255736Sdavidch    if (IS_PF(sc)) {
12402255736Sdavidch        /* set pf load just before approaching the MCP */
12403255736Sdavidch        bxe_set_pf_load(sc);
12404255736Sdavidch
12405255736Sdavidch        /* if MCP exists send load request and analyze response */
12406255736Sdavidch        if (!BXE_NOMCP(sc)) {
12407255736Sdavidch            /* attempt to load pf */
12408255736Sdavidch            if (bxe_nic_load_request(sc, &load_code) != 0) {
12409255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12410255736Sdavidch                rc = ENXIO;
12411255736Sdavidch                goto bxe_nic_load_error1;
12412255736Sdavidch            }
12413255736Sdavidch
12414255736Sdavidch            /* what did the MCP say? */
12415255736Sdavidch            if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12416255736Sdavidch                bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12417255736Sdavidch                sc->state = BXE_STATE_CLOSED;
12418255736Sdavidch                rc = ENXIO;
12419255736Sdavidch                goto bxe_nic_load_error2;
12420255736Sdavidch            }
12421255736Sdavidch        } else {
12422255736Sdavidch            BLOGI(sc, "Device has no MCP!\n");
12423255736Sdavidch            load_code = bxe_nic_load_no_mcp(sc);
12424255736Sdavidch        }
12425255736Sdavidch
12426255736Sdavidch        /* mark PMF if applicable */
12427255736Sdavidch        bxe_nic_load_pmf(sc, load_code);
12428255736Sdavidch
12429255736Sdavidch        /* Init Function state controlling object */
12430255736Sdavidch        bxe_init_func_obj(sc);
12431255736Sdavidch
12432255736Sdavidch        /* Initialize HW */
12433255736Sdavidch        if (bxe_init_hw(sc, load_code) != 0) {
12434255736Sdavidch            BLOGE(sc, "HW init failed\n");
12435255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12436255736Sdavidch            sc->state = BXE_STATE_CLOSED;
12437255736Sdavidch            rc = ENXIO;
12438255736Sdavidch            goto bxe_nic_load_error2;
12439255736Sdavidch        }
12440255736Sdavidch    }
12441255736Sdavidch
12442284335Sdavidcs    /* set ALWAYS_ALIVE bit in shmem */
12443284335Sdavidcs    sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12444284335Sdavidcs    bxe_drv_pulse(sc);
12445284335Sdavidcs    sc->flags |= BXE_NO_PULSE;
12446284335Sdavidcs
12447255736Sdavidch    /* attach interrupts */
12448255736Sdavidch    if (bxe_interrupt_attach(sc) != 0) {
12449255736Sdavidch        sc->state = BXE_STATE_CLOSED;
12450255736Sdavidch        rc = ENXIO;
12451255736Sdavidch        goto bxe_nic_load_error2;
12452255736Sdavidch    }
12453255736Sdavidch
12454255736Sdavidch    bxe_nic_init(sc, load_code);
12455255736Sdavidch
12456255736Sdavidch    /* Init per-function objects */
12457255736Sdavidch    if (IS_PF(sc)) {
12458255736Sdavidch        bxe_init_objs(sc);
12459255736Sdavidch        // XXX bxe_iov_nic_init(sc);
12460255736Sdavidch
12461255736Sdavidch        /* set AFEX default VLAN tag to an invalid value */
12462255736Sdavidch        sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12463255736Sdavidch        // XXX bxe_nic_load_afex_dcc(sc, load_code);
12464255736Sdavidch
12465255736Sdavidch        sc->state = BXE_STATE_OPENING_WAITING_PORT;
12466255736Sdavidch        rc = bxe_func_start(sc);
12467255736Sdavidch        if (rc) {
12468295830Sdavidcs            BLOGE(sc, "Function start failed! rc = %d\n", rc);
12469255736Sdavidch            bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12470255736Sdavidch            sc->state = BXE_STATE_ERROR;
12471255736Sdavidch            goto bxe_nic_load_error3;
12472255736Sdavidch        }
12473255736Sdavidch
12474255736Sdavidch        /* send LOAD_DONE command to MCP */
12475255736Sdavidch        if (!BXE_NOMCP(sc)) {
12476255736Sdavidch            load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12477255736Sdavidch            if (!load_code) {
12478255736Sdavidch                BLOGE(sc, "MCP response failure, aborting\n");
12479255736Sdavidch                sc->state = BXE_STATE_ERROR;
12480255736Sdavidch                rc = ENXIO;
12481255736Sdavidch                goto bxe_nic_load_error3;
12482255736Sdavidch            }
12483255736Sdavidch        }
12484255736Sdavidch
12485255736Sdavidch        rc = bxe_setup_leading(sc);
12486255736Sdavidch        if (rc) {
12487295830Sdavidcs            BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12488255736Sdavidch            sc->state = BXE_STATE_ERROR;
12489255736Sdavidch            goto bxe_nic_load_error3;
12490255736Sdavidch        }
12491255736Sdavidch
12492255736Sdavidch        FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12493255736Sdavidch            rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12494255736Sdavidch            if (rc) {
12495295830Sdavidcs                BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12496255736Sdavidch                sc->state = BXE_STATE_ERROR;
12497255736Sdavidch                goto bxe_nic_load_error3;
12498255736Sdavidch            }
12499255736Sdavidch        }
12500255736Sdavidch
12501255736Sdavidch        rc = bxe_init_rss_pf(sc);
12502255736Sdavidch        if (rc) {
12503255736Sdavidch            BLOGE(sc, "PF RSS init failed\n");
12504255736Sdavidch            sc->state = BXE_STATE_ERROR;
12505255736Sdavidch            goto bxe_nic_load_error3;
12506255736Sdavidch        }
12507255736Sdavidch    }
12508255736Sdavidch    /* XXX VF */
12509255736Sdavidch
12510255736Sdavidch    /* now when Clients are configured we are ready to work */
12511255736Sdavidch    sc->state = BXE_STATE_OPEN;
12512255736Sdavidch
12513255736Sdavidch    /* Configure a ucast MAC */
12514255736Sdavidch    if (IS_PF(sc)) {
12515255736Sdavidch        rc = bxe_set_eth_mac(sc, TRUE);
12516255736Sdavidch    }
12517255736Sdavidch    if (rc) {
12518295830Sdavidcs        BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12519255736Sdavidch        sc->state = BXE_STATE_ERROR;
12520255736Sdavidch        goto bxe_nic_load_error3;
12521255736Sdavidch    }
12522255736Sdavidch
12523255736Sdavidch    if (sc->port.pmf) {
12524255736Sdavidch        rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12525255736Sdavidch        if (rc) {
12526255736Sdavidch            sc->state = BXE_STATE_ERROR;
12527255736Sdavidch            goto bxe_nic_load_error3;
12528255736Sdavidch        }
12529255736Sdavidch    }
12530255736Sdavidch
12531255736Sdavidch    sc->link_params.feature_config_flags &=
12532255736Sdavidch        ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12533255736Sdavidch
12534255736Sdavidch    /* start fast path */
12535255736Sdavidch
12536255736Sdavidch    /* Initialize Rx filter */
12537255736Sdavidch    bxe_set_rx_mode(sc);
12538255736Sdavidch
12539255736Sdavidch    /* start the Tx */
12540255736Sdavidch    switch (/* XXX load_mode */LOAD_OPEN) {
12541255736Sdavidch    case LOAD_NORMAL:
12542255736Sdavidch    case LOAD_OPEN:
12543255736Sdavidch        break;
12544255736Sdavidch
12545255736Sdavidch    case LOAD_DIAG:
12546255736Sdavidch    case LOAD_LOOPBACK_EXT:
12547255736Sdavidch        sc->state = BXE_STATE_DIAG;
12548255736Sdavidch        break;
12549255736Sdavidch
12550255736Sdavidch    default:
12551255736Sdavidch        break;
12552255736Sdavidch    }
12553255736Sdavidch
12554255736Sdavidch    if (sc->port.pmf) {
12555255736Sdavidch        bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12556255736Sdavidch    } else {
12557255736Sdavidch        bxe_link_status_update(sc);
12558255736Sdavidch    }
12559255736Sdavidch
12560255736Sdavidch    /* start the periodic timer callout */
12561255736Sdavidch    bxe_periodic_start(sc);
12562255736Sdavidch
12563255736Sdavidch    if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12564255736Sdavidch        /* mark driver is loaded in shmem2 */
12565255736Sdavidch        val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12566255736Sdavidch        SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12567255736Sdavidch                  (val |
12568255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12569255736Sdavidch                   DRV_FLAGS_CAPABILITIES_LOADED_L2));
12570255736Sdavidch    }
12571255736Sdavidch
12572255736Sdavidch    /* wait for all pending SP commands to complete */
12573255736Sdavidch    if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12574255736Sdavidch        BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12575255736Sdavidch        bxe_periodic_stop(sc);
12576255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12577255736Sdavidch        return (ENXIO);
12578255736Sdavidch    }
12579255736Sdavidch
12580255736Sdavidch    /* Tell the stack the driver is running! */
12581266979Smarcel    if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
12582255736Sdavidch
12583255736Sdavidch    BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12584255736Sdavidch
12585255736Sdavidch    return (0);
12586255736Sdavidch
12587255736Sdavidchbxe_nic_load_error3:
12588255736Sdavidch
12589255736Sdavidch    if (IS_PF(sc)) {
12590255736Sdavidch        bxe_int_disable_sync(sc, 1);
12591255736Sdavidch
12592255736Sdavidch        /* clean out queued objects */
12593255736Sdavidch        bxe_squeeze_objects(sc);
12594255736Sdavidch    }
12595255736Sdavidch
12596255736Sdavidch    bxe_interrupt_detach(sc);
12597255736Sdavidch
12598255736Sdavidchbxe_nic_load_error2:
12599255736Sdavidch
12600255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12601255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12602255736Sdavidch        bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12603255736Sdavidch    }
12604255736Sdavidch
12605255736Sdavidch    sc->port.pmf = 0;
12606255736Sdavidch
12607255736Sdavidchbxe_nic_load_error1:
12608255736Sdavidch
12609255736Sdavidch    /* clear pf_load status, as it was already set */
12610255736Sdavidch    if (IS_PF(sc)) {
12611255736Sdavidch        bxe_clear_pf_load(sc);
12612255736Sdavidch    }
12613255736Sdavidch
12614255736Sdavidchbxe_nic_load_error0:
12615255736Sdavidch
12616255736Sdavidch    bxe_free_fw_stats_mem(sc);
12617255736Sdavidch    bxe_free_fp_buffers(sc);
12618255736Sdavidch    bxe_free_mem(sc);
12619255736Sdavidch
12620255736Sdavidch    return (rc);
12621255736Sdavidch}
12622255736Sdavidch
12623255736Sdavidchstatic int
12624255736Sdavidchbxe_init_locked(struct bxe_softc *sc)
12625255736Sdavidch{
12626255736Sdavidch    int other_engine = SC_PATH(sc) ? 0 : 1;
12627255736Sdavidch    uint8_t other_load_status, load_status;
12628255736Sdavidch    uint8_t global = FALSE;
12629255736Sdavidch    int rc;
12630255736Sdavidch
12631255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12632255736Sdavidch
12633255736Sdavidch    /* check if the driver is already running */
12634266979Smarcel    if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12635255736Sdavidch        BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12636255736Sdavidch        return (0);
12637255736Sdavidch    }
12638255736Sdavidch
12639255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
12640255736Sdavidch
12641255736Sdavidch    /*
12642255736Sdavidch     * If parity occurred during the unload, then attentions and/or
12643255736Sdavidch     * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12644255736Sdavidch     * loaded on the current engine to complete the recovery. Parity recovery
12645255736Sdavidch     * is only relevant for PF driver.
12646255736Sdavidch     */
12647255736Sdavidch    if (IS_PF(sc)) {
12648255736Sdavidch        other_load_status = bxe_get_load_status(sc, other_engine);
12649255736Sdavidch        load_status = bxe_get_load_status(sc, SC_PATH(sc));
12650255736Sdavidch
12651255736Sdavidch        if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12652255736Sdavidch            bxe_chk_parity_attn(sc, &global, TRUE)) {
12653255736Sdavidch            do {
12654255736Sdavidch                /*
12655255736Sdavidch                 * If there are attentions and they are in global blocks, set
12656255736Sdavidch                 * the GLOBAL_RESET bit regardless whether it will be this
12657255736Sdavidch                 * function that will complete the recovery or not.
12658255736Sdavidch                 */
12659255736Sdavidch                if (global) {
12660255736Sdavidch                    bxe_set_reset_global(sc);
12661255736Sdavidch                }
12662255736Sdavidch
12663255736Sdavidch                /*
12664255736Sdavidch                 * Only the first function on the current engine should try
12665255736Sdavidch                 * to recover in open. In case of attentions in global blocks
12666255736Sdavidch                 * only the first in the chip should try to recover.
12667255736Sdavidch                 */
12668255736Sdavidch                if ((!load_status && (!global || !other_load_status)) &&
12669255736Sdavidch                    bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12670255736Sdavidch                    BLOGI(sc, "Recovered during init\n");
12671255736Sdavidch                    break;
12672255736Sdavidch                }
12673255736Sdavidch
12674255736Sdavidch                /* recovery has failed... */
12675255736Sdavidch                bxe_set_power_state(sc, PCI_PM_D3hot);
12676255736Sdavidch                sc->recovery_state = BXE_RECOVERY_FAILED;
12677255736Sdavidch
12678255736Sdavidch                BLOGE(sc, "Recovery flow hasn't properly "
12679255736Sdavidch                          "completed yet, try again later. "
12680255736Sdavidch                          "If you still see this message after a "
12681255736Sdavidch                          "few retries then power cycle is required.\n");
12682255736Sdavidch
12683255736Sdavidch                rc = ENXIO;
12684255736Sdavidch                goto bxe_init_locked_done;
12685255736Sdavidch            } while (0);
12686255736Sdavidch        }
12687255736Sdavidch    }
12688255736Sdavidch
12689255736Sdavidch    sc->recovery_state = BXE_RECOVERY_DONE;
12690255736Sdavidch
12691255736Sdavidch    rc = bxe_nic_load(sc, LOAD_OPEN);
12692255736Sdavidch
12693255736Sdavidchbxe_init_locked_done:
12694255736Sdavidch
12695255736Sdavidch    if (rc) {
12696255736Sdavidch        /* Tell the stack the driver is NOT running! */
12697255736Sdavidch        BLOGE(sc, "Initialization failed, "
12698255736Sdavidch                  "stack notified driver is NOT running!\n");
12699266979Smarcel	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
12700255736Sdavidch    }
12701255736Sdavidch
12702255736Sdavidch    return (rc);
12703255736Sdavidch}
12704255736Sdavidch
12705255736Sdavidchstatic int
12706255736Sdavidchbxe_stop_locked(struct bxe_softc *sc)
12707255736Sdavidch{
12708255736Sdavidch    BXE_CORE_LOCK_ASSERT(sc);
12709255736Sdavidch    return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
12710255736Sdavidch}
12711255736Sdavidch
12712255736Sdavidch/*
12713255736Sdavidch * Handles controller initialization when called from an unlocked routine.
12714255736Sdavidch * ifconfig calls this function.
12715255736Sdavidch *
12716255736Sdavidch * Returns:
12717255736Sdavidch *   void
12718255736Sdavidch */
12719255736Sdavidchstatic void
12720255736Sdavidchbxe_init(void *xsc)
12721255736Sdavidch{
12722255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)xsc;
12723255736Sdavidch
12724255736Sdavidch    BXE_CORE_LOCK(sc);
12725255736Sdavidch    bxe_init_locked(sc);
12726255736Sdavidch    BXE_CORE_UNLOCK(sc);
12727255736Sdavidch}
12728255736Sdavidch
12729255736Sdavidchstatic int
12730255736Sdavidchbxe_init_ifnet(struct bxe_softc *sc)
12731255736Sdavidch{
12732266979Smarcel    if_t ifp;
12733266979Smarcel    int capabilities;
12734255736Sdavidch
12735255736Sdavidch    /* ifconfig entrypoint for media type/status reporting */
12736255736Sdavidch    ifmedia_init(&sc->ifmedia, IFM_IMASK,
12737255736Sdavidch                 bxe_ifmedia_update,
12738255736Sdavidch                 bxe_ifmedia_status);
12739255736Sdavidch
12740255736Sdavidch    /* set the default interface values */
12741255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
12742255736Sdavidch    ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
12743255736Sdavidch    ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
12744255736Sdavidch
12745255736Sdavidch    sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
12746255736Sdavidch
12747255736Sdavidch    /* allocate the ifnet structure */
12748266979Smarcel    if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
12749255736Sdavidch        BLOGE(sc, "Interface allocation failed!\n");
12750255736Sdavidch        return (ENXIO);
12751255736Sdavidch    }
12752255736Sdavidch
12753266979Smarcel    if_setsoftc(ifp, sc);
12754270876Sglebius    if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
12755266979Smarcel    if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
12756266979Smarcel    if_setioctlfn(ifp, bxe_ioctl);
12757266979Smarcel    if_setstartfn(ifp, bxe_tx_start);
12758271782Sglebius    if_setgetcounterfn(ifp, bxe_get_counter);
12759255736Sdavidch#if __FreeBSD_version >= 800000
12760266979Smarcel    if_settransmitfn(ifp, bxe_tx_mq_start);
12761266979Smarcel    if_setqflushfn(ifp, bxe_mq_flush);
12762255736Sdavidch#endif
12763255736Sdavidch#ifdef FreeBSD8_0
12764266979Smarcel    if_settimer(ifp, 0);
12765255736Sdavidch#endif
12766266979Smarcel    if_setinitfn(ifp, bxe_init);
12767266979Smarcel    if_setmtu(ifp, sc->mtu);
12768266979Smarcel    if_sethwassist(ifp, (CSUM_IP      |
12769255736Sdavidch                        CSUM_TCP      |
12770255736Sdavidch                        CSUM_UDP      |
12771255736Sdavidch                        CSUM_TSO      |
12772255736Sdavidch                        CSUM_TCP_IPV6 |
12773266979Smarcel                        CSUM_UDP_IPV6));
12774266979Smarcel
12775266979Smarcel    capabilities =
12776255736Sdavidch#if __FreeBSD_version < 700000
12777255736Sdavidch        (IFCAP_VLAN_MTU       |
12778255736Sdavidch         IFCAP_VLAN_HWTAGGING |
12779255736Sdavidch         IFCAP_HWCSUM         |
12780255736Sdavidch         IFCAP_JUMBO_MTU      |
12781255736Sdavidch         IFCAP_LRO);
12782255736Sdavidch#else
12783255736Sdavidch        (IFCAP_VLAN_MTU       |
12784255736Sdavidch         IFCAP_VLAN_HWTAGGING |
12785255736Sdavidch         IFCAP_VLAN_HWTSO     |
12786255736Sdavidch         IFCAP_VLAN_HWFILTER  |
12787255736Sdavidch         IFCAP_VLAN_HWCSUM    |
12788255736Sdavidch         IFCAP_HWCSUM         |
12789255736Sdavidch         IFCAP_JUMBO_MTU      |
12790255736Sdavidch         IFCAP_LRO            |
12791255736Sdavidch         IFCAP_TSO4           |
12792255736Sdavidch         IFCAP_TSO6           |
12793255736Sdavidch         IFCAP_WOL_MAGIC);
12794255736Sdavidch#endif
12795266979Smarcel    if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
12796266979Smarcel    if_setbaudrate(ifp, IF_Gbps(10));
12797266979Smarcel/* XXX */
12798266979Smarcel    if_setsendqlen(ifp, sc->tx_ring_size);
12799266979Smarcel    if_setsendqready(ifp);
12800266979Smarcel/* XXX */
12801255736Sdavidch
12802266979Smarcel    sc->ifp = ifp;
12803255736Sdavidch
12804255736Sdavidch    /* attach to the Ethernet interface list */
12805270876Sglebius    ether_ifattach(ifp, sc->link_params.mac_addr);
12806255736Sdavidch
12807255736Sdavidch    return (0);
12808255736Sdavidch}
12809255736Sdavidch
12810255736Sdavidchstatic void
12811255736Sdavidchbxe_deallocate_bars(struct bxe_softc *sc)
12812255736Sdavidch{
12813255736Sdavidch    int i;
12814255736Sdavidch
12815255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
12816255736Sdavidch        if (sc->bar[i].resource != NULL) {
12817255736Sdavidch            bus_release_resource(sc->dev,
12818255736Sdavidch                                 SYS_RES_MEMORY,
12819255736Sdavidch                                 sc->bar[i].rid,
12820255736Sdavidch                                 sc->bar[i].resource);
12821255736Sdavidch            BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
12822255736Sdavidch                  i, PCIR_BAR(i));
12823255736Sdavidch        }
12824255736Sdavidch    }
12825255736Sdavidch}
12826255736Sdavidch
12827255736Sdavidchstatic int
12828255736Sdavidchbxe_allocate_bars(struct bxe_softc *sc)
12829255736Sdavidch{
12830255736Sdavidch    u_int flags;
12831255736Sdavidch    int i;
12832255736Sdavidch
12833255736Sdavidch    memset(sc->bar, 0, sizeof(sc->bar));
12834255736Sdavidch
12835255736Sdavidch    for (i = 0; i < MAX_BARS; i++) {
12836255736Sdavidch
12837255736Sdavidch        /* memory resources reside at BARs 0, 2, 4 */
12838255736Sdavidch        /* Run `pciconf -lb` to see mappings */
12839255736Sdavidch        if ((i != 0) && (i != 2) && (i != 4)) {
12840255736Sdavidch            continue;
12841255736Sdavidch        }
12842255736Sdavidch
12843255736Sdavidch        sc->bar[i].rid = PCIR_BAR(i);
12844255736Sdavidch
12845255736Sdavidch        flags = RF_ACTIVE;
12846255736Sdavidch        if (i == 0) {
12847255736Sdavidch            flags |= RF_SHAREABLE;
12848255736Sdavidch        }
12849255736Sdavidch
12850255736Sdavidch        if ((sc->bar[i].resource =
12851255736Sdavidch             bus_alloc_resource_any(sc->dev,
12852255736Sdavidch                                    SYS_RES_MEMORY,
12853255736Sdavidch                                    &sc->bar[i].rid,
12854255736Sdavidch                                    flags)) == NULL) {
12855255736Sdavidch            return (0);
12856255736Sdavidch        }
12857255736Sdavidch
12858255736Sdavidch        sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
12859255736Sdavidch        sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
12860255736Sdavidch        sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
12861255736Sdavidch
12862255736Sdavidch        BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%ld) -> %p\n",
12863255736Sdavidch              i, PCIR_BAR(i),
12864255736Sdavidch              (void *)rman_get_start(sc->bar[i].resource),
12865255736Sdavidch              (void *)rman_get_end(sc->bar[i].resource),
12866255736Sdavidch              rman_get_size(sc->bar[i].resource),
12867255736Sdavidch              (void *)sc->bar[i].kva);
12868255736Sdavidch    }
12869255736Sdavidch
12870255736Sdavidch    return (0);
12871255736Sdavidch}
12872255736Sdavidch
12873255736Sdavidchstatic void
12874255736Sdavidchbxe_get_function_num(struct bxe_softc *sc)
12875255736Sdavidch{
12876255736Sdavidch    uint32_t val = 0;
12877255736Sdavidch
12878255736Sdavidch    /*
12879255736Sdavidch     * Read the ME register to get the function number. The ME register
12880255736Sdavidch     * holds the relative-function number and absolute-function number. The
12881255736Sdavidch     * absolute-function number appears only in E2 and above. Before that
12882255736Sdavidch     * these bits always contained zero, therefore we cannot blindly use them.
12883255736Sdavidch     */
12884255736Sdavidch
12885255736Sdavidch    val = REG_RD(sc, BAR_ME_REGISTER);
12886255736Sdavidch
12887255736Sdavidch    sc->pfunc_rel =
12888255736Sdavidch        (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
12889255736Sdavidch    sc->path_id =
12890255736Sdavidch        (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
12891255736Sdavidch
12892255736Sdavidch    if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
12893255736Sdavidch        sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
12894255736Sdavidch    } else {
12895255736Sdavidch        sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
12896255736Sdavidch    }
12897255736Sdavidch
12898255736Sdavidch    BLOGD(sc, DBG_LOAD,
12899255736Sdavidch          "Relative function %d, Absolute function %d, Path %d\n",
12900255736Sdavidch          sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
12901255736Sdavidch}
12902255736Sdavidch
12903255736Sdavidchstatic uint32_t
12904255736Sdavidchbxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
12905255736Sdavidch{
12906255736Sdavidch    uint32_t shmem2_size;
12907255736Sdavidch    uint32_t offset;
12908255736Sdavidch    uint32_t mf_cfg_offset_value;
12909255736Sdavidch
12910255736Sdavidch    /* Non 57712 */
12911255736Sdavidch    offset = (SHMEM_RD(sc, func_mb) +
12912255736Sdavidch              (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
12913255736Sdavidch
12914255736Sdavidch    /* 57712 plus */
12915255736Sdavidch    if (sc->devinfo.shmem2_base != 0) {
12916255736Sdavidch        shmem2_size = SHMEM2_RD(sc, size);
12917255736Sdavidch        if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
12918255736Sdavidch            mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
12919255736Sdavidch            if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
12920255736Sdavidch                offset = mf_cfg_offset_value;
12921255736Sdavidch            }
12922255736Sdavidch        }
12923255736Sdavidch    }
12924255736Sdavidch
12925255736Sdavidch    return (offset);
12926255736Sdavidch}
12927255736Sdavidch
12928255736Sdavidchstatic uint32_t
12929255736Sdavidchbxe_pcie_capability_read(struct bxe_softc *sc,
12930255736Sdavidch                         int    reg,
12931255736Sdavidch                         int    width)
12932255736Sdavidch{
12933255736Sdavidch    int pcie_reg;
12934255736Sdavidch
12935255736Sdavidch    /* ensure PCIe capability is enabled */
12936255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
12937255736Sdavidch        if (pcie_reg != 0) {
12938255736Sdavidch            BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
12939255736Sdavidch            return (pci_read_config(sc->dev, (pcie_reg + reg), width));
12940255736Sdavidch        }
12941255736Sdavidch    }
12942255736Sdavidch
12943255736Sdavidch    BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
12944255736Sdavidch
12945255736Sdavidch    return (0);
12946255736Sdavidch}
12947255736Sdavidch
12948255736Sdavidchstatic uint8_t
12949255736Sdavidchbxe_is_pcie_pending(struct bxe_softc *sc)
12950255736Sdavidch{
12951255736Sdavidch    return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
12952255736Sdavidch            PCIM_EXP_STA_TRANSACTION_PND);
12953255736Sdavidch}
12954255736Sdavidch
12955255736Sdavidch/*
12956255736Sdavidch * Walk the PCI capabiites list for the device to find what features are
12957255736Sdavidch * supported. These capabilites may be enabled/disabled by firmware so it's
12958255736Sdavidch * best to walk the list rather than make assumptions.
12959255736Sdavidch */
12960255736Sdavidchstatic void
12961255736Sdavidchbxe_probe_pci_caps(struct bxe_softc *sc)
12962255736Sdavidch{
12963255736Sdavidch    uint16_t link_status;
12964255736Sdavidch    int reg;
12965255736Sdavidch
12966255736Sdavidch    /* check if PCI Power Management is enabled */
12967255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
12968255736Sdavidch        if (reg != 0) {
12969255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
12970255736Sdavidch
12971255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
12972255736Sdavidch            sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
12973255736Sdavidch        }
12974255736Sdavidch    }
12975255736Sdavidch
12976255736Sdavidch    link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
12977255736Sdavidch
12978255736Sdavidch    /* handle PCIe 2.0 workarounds for 57710 */
12979255736Sdavidch    if (CHIP_IS_E1(sc)) {
12980255736Sdavidch        /* workaround for 57710 errata E4_57710_27462 */
12981255736Sdavidch        sc->devinfo.pcie_link_speed =
12982255736Sdavidch            (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
12983255736Sdavidch
12984255736Sdavidch        /* workaround for 57710 errata E4_57710_27488 */
12985255736Sdavidch        sc->devinfo.pcie_link_width =
12986255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12987255736Sdavidch        if (sc->devinfo.pcie_link_speed > 1) {
12988255736Sdavidch            sc->devinfo.pcie_link_width =
12989255736Sdavidch                ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
12990255736Sdavidch        }
12991255736Sdavidch    } else {
12992255736Sdavidch        sc->devinfo.pcie_link_speed =
12993255736Sdavidch            (link_status & PCIM_LINK_STA_SPEED);
12994255736Sdavidch        sc->devinfo.pcie_link_width =
12995255736Sdavidch            ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12996255736Sdavidch    }
12997255736Sdavidch
12998255736Sdavidch    BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
12999255736Sdavidch          sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
13000255736Sdavidch
13001255736Sdavidch    sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
13002255736Sdavidch    sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
13003255736Sdavidch
13004255736Sdavidch    /* check if MSI capability is enabled */
13005255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
13006255736Sdavidch        if (reg != 0) {
13007255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
13008255736Sdavidch
13009255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
13010255736Sdavidch            sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
13011255736Sdavidch        }
13012255736Sdavidch    }
13013255736Sdavidch
13014255736Sdavidch    /* check if MSI-X capability is enabled */
13015255736Sdavidch    if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
13016255736Sdavidch        if (reg != 0) {
13017255736Sdavidch            BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
13018255736Sdavidch
13019255736Sdavidch            sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
13020255736Sdavidch            sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
13021255736Sdavidch        }
13022255736Sdavidch    }
13023255736Sdavidch}
13024255736Sdavidch
13025255736Sdavidchstatic int
13026255736Sdavidchbxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
13027255736Sdavidch{
13028255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13029255736Sdavidch    uint32_t val;
13030255736Sdavidch
13031255736Sdavidch    /* get the outer vlan if we're in switch-dependent mode */
13032255736Sdavidch
13033255736Sdavidch    val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13034255736Sdavidch    mf_info->ext_id = (uint16_t)val;
13035255736Sdavidch
13036255736Sdavidch    mf_info->multi_vnics_mode = 1;
13037255736Sdavidch
13038255736Sdavidch    if (!VALID_OVLAN(mf_info->ext_id)) {
13039255736Sdavidch        BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13040255736Sdavidch        return (1);
13041255736Sdavidch    }
13042255736Sdavidch
13043255736Sdavidch    /* get the capabilities */
13044255736Sdavidch    if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13045255736Sdavidch        FUNC_MF_CFG_PROTOCOL_ISCSI) {
13046255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13047255736Sdavidch    } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13048255736Sdavidch               FUNC_MF_CFG_PROTOCOL_FCOE) {
13049255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13050255736Sdavidch    } else {
13051255736Sdavidch        mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13052255736Sdavidch    }
13053255736Sdavidch
13054255736Sdavidch    mf_info->vnics_per_port =
13055255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13056255736Sdavidch
13057255736Sdavidch    return (0);
13058255736Sdavidch}
13059255736Sdavidch
13060255736Sdavidchstatic uint32_t
13061255736Sdavidchbxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13062255736Sdavidch{
13063255736Sdavidch    uint32_t retval = 0;
13064255736Sdavidch    uint32_t val;
13065255736Sdavidch
13066255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13067255736Sdavidch
13068255736Sdavidch    if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13069255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13070255736Sdavidch            retval |= MF_PROTO_SUPPORT_ETHERNET;
13071255736Sdavidch        }
13072255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13073255736Sdavidch            retval |= MF_PROTO_SUPPORT_ISCSI;
13074255736Sdavidch        }
13075255736Sdavidch        if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13076255736Sdavidch            retval |= MF_PROTO_SUPPORT_FCOE;
13077255736Sdavidch        }
13078255736Sdavidch    }
13079255736Sdavidch
13080255736Sdavidch    return (retval);
13081255736Sdavidch}
13082255736Sdavidch
13083255736Sdavidchstatic int
13084255736Sdavidchbxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13085255736Sdavidch{
13086255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13087255736Sdavidch    uint32_t val;
13088255736Sdavidch
13089255736Sdavidch    /*
13090255736Sdavidch     * There is no outer vlan if we're in switch-independent mode.
13091255736Sdavidch     * If the mac is valid then assume multi-function.
13092255736Sdavidch     */
13093255736Sdavidch
13094255736Sdavidch    val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13095255736Sdavidch
13096255736Sdavidch    mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13097255736Sdavidch
13098255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13099255736Sdavidch
13100255736Sdavidch    mf_info->vnics_per_port =
13101255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13102255736Sdavidch
13103255736Sdavidch    return (0);
13104255736Sdavidch}
13105255736Sdavidch
13106255736Sdavidchstatic int
13107255736Sdavidchbxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13108255736Sdavidch{
13109255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13110255736Sdavidch    uint32_t e1hov_tag;
13111255736Sdavidch    uint32_t func_config;
13112255736Sdavidch    uint32_t niv_config;
13113255736Sdavidch
13114255736Sdavidch    mf_info->multi_vnics_mode = 1;
13115255736Sdavidch
13116255736Sdavidch    e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13117255736Sdavidch    func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13118255736Sdavidch    niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13119255736Sdavidch
13120255736Sdavidch    mf_info->ext_id =
13121255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13122255736Sdavidch                   FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13123255736Sdavidch
13124255736Sdavidch    mf_info->default_vlan =
13125255736Sdavidch        (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13126255736Sdavidch                   FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13127255736Sdavidch
13128255736Sdavidch    mf_info->niv_allowed_priorities =
13129255736Sdavidch        (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13130255736Sdavidch                  FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13131255736Sdavidch
13132255736Sdavidch    mf_info->niv_default_cos =
13133255736Sdavidch        (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13134255736Sdavidch                  FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13135255736Sdavidch
13136255736Sdavidch    mf_info->afex_vlan_mode =
13137255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13138255736Sdavidch         FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13139255736Sdavidch
13140255736Sdavidch    mf_info->niv_mba_enabled =
13141255736Sdavidch        ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13142255736Sdavidch         FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13143255736Sdavidch
13144255736Sdavidch    mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13145255736Sdavidch
13146255736Sdavidch    mf_info->vnics_per_port =
13147255736Sdavidch        (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13148255736Sdavidch
13149255736Sdavidch    return (0);
13150255736Sdavidch}
13151255736Sdavidch
13152255736Sdavidchstatic int
13153255736Sdavidchbxe_check_valid_mf_cfg(struct bxe_softc *sc)
13154255736Sdavidch{
13155255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13156255736Sdavidch    uint32_t mf_cfg1;
13157255736Sdavidch    uint32_t mf_cfg2;
13158255736Sdavidch    uint32_t ovlan1;
13159255736Sdavidch    uint32_t ovlan2;
13160255736Sdavidch    uint8_t i, j;
13161255736Sdavidch
13162255736Sdavidch    BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13163255736Sdavidch          SC_PORT(sc));
13164255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13165255736Sdavidch          mf_info->mf_config[SC_VN(sc)]);
13166255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13167255736Sdavidch          mf_info->multi_vnics_mode);
13168255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13169255736Sdavidch          mf_info->vnics_per_port);
13170255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13171255736Sdavidch          mf_info->ext_id);
13172255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13173255736Sdavidch          mf_info->min_bw[0], mf_info->min_bw[1],
13174255736Sdavidch          mf_info->min_bw[2], mf_info->min_bw[3]);
13175255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13176255736Sdavidch          mf_info->max_bw[0], mf_info->max_bw[1],
13177255736Sdavidch          mf_info->max_bw[2], mf_info->max_bw[3]);
13178255736Sdavidch    BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13179255736Sdavidch          sc->mac_addr_str);
13180255736Sdavidch
13181255736Sdavidch    /* various MF mode sanity checks... */
13182255736Sdavidch
13183255736Sdavidch    if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13184255736Sdavidch        BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13185255736Sdavidch              SC_PORT(sc));
13186255736Sdavidch        return (1);
13187255736Sdavidch    }
13188255736Sdavidch
13189255736Sdavidch    if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13190255736Sdavidch        BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13191255736Sdavidch              mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13192255736Sdavidch        return (1);
13193255736Sdavidch    }
13194255736Sdavidch
13195255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13196255736Sdavidch        /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13197255736Sdavidch        if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13198255736Sdavidch            BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13199255736Sdavidch                  SC_VN(sc), OVLAN(sc));
13200255736Sdavidch            return (1);
13201255736Sdavidch        }
13202255736Sdavidch
13203255736Sdavidch        if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13204255736Sdavidch            BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13205255736Sdavidch                  mf_info->multi_vnics_mode, OVLAN(sc));
13206255736Sdavidch            return (1);
13207255736Sdavidch        }
13208255736Sdavidch
13209255736Sdavidch        /*
13210255736Sdavidch         * Verify all functions are either MF or SF mode. If MF, make sure
13211255736Sdavidch         * sure that all non-hidden functions have a valid ovlan. If SF,
13212255736Sdavidch         * make sure that all non-hidden functions have an invalid ovlan.
13213255736Sdavidch         */
13214255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13215255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13216255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13217255736Sdavidch            if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13218255736Sdavidch                (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13219255736Sdavidch                 ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13220255736Sdavidch                BLOGE(sc, "mf_mode=SD function %d MF config "
13221255736Sdavidch                          "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13222255736Sdavidch                      i, mf_info->multi_vnics_mode, ovlan1);
13223255736Sdavidch                return (1);
13224255736Sdavidch            }
13225255736Sdavidch        }
13226255736Sdavidch
13227255736Sdavidch        /* Verify all funcs on the same port each have a different ovlan. */
13228255736Sdavidch        FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13229255736Sdavidch            mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13230255736Sdavidch            ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13231255736Sdavidch            /* iterate from the next function on the port to the max func */
13232255736Sdavidch            for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13233255736Sdavidch                mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13234255736Sdavidch                ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13235255736Sdavidch                if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13236255736Sdavidch                    VALID_OVLAN(ovlan1) &&
13237255736Sdavidch                    !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13238255736Sdavidch                    VALID_OVLAN(ovlan2) &&
13239255736Sdavidch                    (ovlan1 == ovlan2)) {
13240255736Sdavidch                    BLOGE(sc, "mf_mode=SD functions %d and %d "
13241255736Sdavidch                              "have the same ovlan (%d)\n",
13242255736Sdavidch                          i, j, ovlan1);
13243255736Sdavidch                    return (1);
13244255736Sdavidch                }
13245255736Sdavidch            }
13246255736Sdavidch        }
13247255736Sdavidch    } /* MULTI_FUNCTION_SD */
13248255736Sdavidch
13249255736Sdavidch    return (0);
13250255736Sdavidch}
13251255736Sdavidch
13252255736Sdavidchstatic int
13253255736Sdavidchbxe_get_mf_cfg_info(struct bxe_softc *sc)
13254255736Sdavidch{
13255255736Sdavidch    struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13256255736Sdavidch    uint32_t val, mac_upper;
13257255736Sdavidch    uint8_t i, vnic;
13258255736Sdavidch
13259255736Sdavidch    /* initialize mf_info defaults */
13260255736Sdavidch    mf_info->vnics_per_port   = 1;
13261255736Sdavidch    mf_info->multi_vnics_mode = FALSE;
13262255736Sdavidch    mf_info->path_has_ovlan   = FALSE;
13263255736Sdavidch    mf_info->mf_mode          = SINGLE_FUNCTION;
13264255736Sdavidch
13265255736Sdavidch    if (!CHIP_IS_MF_CAP(sc)) {
13266255736Sdavidch        return (0);
13267255736Sdavidch    }
13268255736Sdavidch
13269255736Sdavidch    if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13270255736Sdavidch        BLOGE(sc, "Invalid mf_cfg_base!\n");
13271255736Sdavidch        return (1);
13272255736Sdavidch    }
13273255736Sdavidch
13274255736Sdavidch    /* get the MF mode (switch dependent / independent / single-function) */
13275255736Sdavidch
13276255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13277255736Sdavidch
13278255736Sdavidch    switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13279255736Sdavidch    {
13280255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13281255736Sdavidch
13282255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13283255736Sdavidch
13284255736Sdavidch        /* check for legal upper mac bytes */
13285255736Sdavidch        if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13286255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SI;
13287255736Sdavidch        } else {
13288255736Sdavidch            BLOGE(sc, "Invalid config for Switch Independent mode\n");
13289255736Sdavidch        }
13290255736Sdavidch
13291255736Sdavidch        break;
13292255736Sdavidch
13293255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13294255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13295255736Sdavidch
13296255736Sdavidch        /* get outer vlan configuration */
13297255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13298255736Sdavidch
13299255736Sdavidch        if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13300255736Sdavidch            FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13301255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_SD;
13302255736Sdavidch        } else {
13303255736Sdavidch            BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13304255736Sdavidch        }
13305255736Sdavidch
13306255736Sdavidch        break;
13307255736Sdavidch
13308255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13309255736Sdavidch
13310255736Sdavidch        /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13311255736Sdavidch        return (0);
13312255736Sdavidch
13313255736Sdavidch    case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13314255736Sdavidch
13315255736Sdavidch        /*
13316255736Sdavidch         * Mark MF mode as NIV if MCP version includes NPAR-SD support
13317255736Sdavidch         * and the MAC address is valid.
13318255736Sdavidch         */
13319255736Sdavidch        mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13320255736Sdavidch
13321255736Sdavidch        if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13322255736Sdavidch            (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13323255736Sdavidch            mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13324255736Sdavidch        } else {
13325255736Sdavidch            BLOGE(sc, "Invalid config for AFEX mode\n");
13326255736Sdavidch        }
13327255736Sdavidch
13328255736Sdavidch        break;
13329255736Sdavidch
13330255736Sdavidch    default:
13331255736Sdavidch
13332255736Sdavidch        BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13333255736Sdavidch              (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13334255736Sdavidch
13335255736Sdavidch        return (1);
13336255736Sdavidch    }
13337255736Sdavidch
13338255736Sdavidch    /* set path mf_mode (which could be different than function mf_mode) */
13339255736Sdavidch    if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13340255736Sdavidch        mf_info->path_has_ovlan = TRUE;
13341255736Sdavidch    } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13342255736Sdavidch        /*
13343255736Sdavidch         * Decide on path multi vnics mode. If we're not in MF mode and in
13344255736Sdavidch         * 4-port mode, this is good enough to check vnic-0 of the other port
13345255736Sdavidch         * on the same path
13346255736Sdavidch         */
13347255736Sdavidch        if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13348255736Sdavidch            uint8_t other_port = !(PORT_ID(sc) & 1);
13349255736Sdavidch            uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13350255736Sdavidch
13351255736Sdavidch            val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13352255736Sdavidch
13353255736Sdavidch            mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13354255736Sdavidch        }
13355255736Sdavidch    }
13356255736Sdavidch
13357255736Sdavidch    if (mf_info->mf_mode == SINGLE_FUNCTION) {
13358255736Sdavidch        /* invalid MF config */
13359255736Sdavidch        if (SC_VN(sc) >= 1) {
13360255736Sdavidch            BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13361255736Sdavidch            return (1);
13362255736Sdavidch        }
13363255736Sdavidch
13364255736Sdavidch        return (0);
13365255736Sdavidch    }
13366255736Sdavidch
13367255736Sdavidch    /* get the MF configuration */
13368255736Sdavidch    mf_info->mf_config[SC_VN(sc)] =
13369255736Sdavidch        MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13370255736Sdavidch
13371255736Sdavidch    switch(mf_info->mf_mode)
13372255736Sdavidch    {
13373255736Sdavidch    case MULTI_FUNCTION_SD:
13374255736Sdavidch
13375255736Sdavidch        bxe_get_shmem_mf_cfg_info_sd(sc);
13376255736Sdavidch        break;
13377255736Sdavidch
13378255736Sdavidch    case MULTI_FUNCTION_SI:
13379255736Sdavidch
13380255736Sdavidch        bxe_get_shmem_mf_cfg_info_si(sc);
13381255736Sdavidch        break;
13382255736Sdavidch
13383255736Sdavidch    case MULTI_FUNCTION_AFEX:
13384255736Sdavidch
13385255736Sdavidch        bxe_get_shmem_mf_cfg_info_niv(sc);
13386255736Sdavidch        break;
13387255736Sdavidch
13388255736Sdavidch    default:
13389255736Sdavidch
13390255736Sdavidch        BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13391255736Sdavidch              mf_info->mf_mode);
13392255736Sdavidch        return (1);
13393255736Sdavidch    }
13394255736Sdavidch
13395255736Sdavidch    /* get the congestion management parameters */
13396255736Sdavidch
13397255736Sdavidch    vnic = 0;
13398255736Sdavidch    FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13399255736Sdavidch        /* get min/max bw */
13400255736Sdavidch        val = MFCFG_RD(sc, func_mf_config[i].config);
13401255736Sdavidch        mf_info->min_bw[vnic] =
13402255736Sdavidch            ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13403255736Sdavidch        mf_info->max_bw[vnic] =
13404255736Sdavidch            ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13405255736Sdavidch        vnic++;
13406255736Sdavidch    }
13407255736Sdavidch
13408255736Sdavidch    return (bxe_check_valid_mf_cfg(sc));
13409255736Sdavidch}
13410255736Sdavidch
13411255736Sdavidchstatic int
13412255736Sdavidchbxe_get_shmem_info(struct bxe_softc *sc)
13413255736Sdavidch{
13414255736Sdavidch    int port;
13415255736Sdavidch    uint32_t mac_hi, mac_lo, val;
13416255736Sdavidch
13417255736Sdavidch    port = SC_PORT(sc);
13418255736Sdavidch    mac_hi = mac_lo = 0;
13419255736Sdavidch
13420255736Sdavidch    sc->link_params.sc   = sc;
13421255736Sdavidch    sc->link_params.port = port;
13422255736Sdavidch
13423255736Sdavidch    /* get the hardware config info */
13424255736Sdavidch    sc->devinfo.hw_config =
13425255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config);
13426255736Sdavidch    sc->devinfo.hw_config2 =
13427255736Sdavidch        SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13428255736Sdavidch
13429255736Sdavidch    sc->link_params.hw_led_mode =
13430255736Sdavidch        ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13431255736Sdavidch         SHARED_HW_CFG_LED_MODE_SHIFT);
13432255736Sdavidch
13433255736Sdavidch    /* get the port feature config */
13434255736Sdavidch    sc->port.config =
13435255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].config),
13436255736Sdavidch
13437255736Sdavidch    /* get the link params */
13438255736Sdavidch    sc->link_params.speed_cap_mask[0] =
13439255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13440255736Sdavidch    sc->link_params.speed_cap_mask[1] =
13441255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13442255736Sdavidch
13443255736Sdavidch    /* get the lane config */
13444255736Sdavidch    sc->link_params.lane_config =
13445255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13446255736Sdavidch
13447255736Sdavidch    /* get the link config */
13448255736Sdavidch    val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13449255736Sdavidch    sc->port.link_config[ELINK_INT_PHY] = val;
13450255736Sdavidch    sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13451255736Sdavidch    sc->port.link_config[ELINK_EXT_PHY1] =
13452255736Sdavidch        SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13453255736Sdavidch
13454255736Sdavidch    /* get the override preemphasis flag and enable it or turn it off */
13455255736Sdavidch    val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13456255736Sdavidch    if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13457255736Sdavidch        sc->link_params.feature_config_flags |=
13458255736Sdavidch            ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13459255736Sdavidch    } else {
13460255736Sdavidch        sc->link_params.feature_config_flags &=
13461255736Sdavidch            ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13462255736Sdavidch    }
13463255736Sdavidch
13464255736Sdavidch    /* get the initial value of the link params */
13465255736Sdavidch    sc->link_params.multi_phy_config =
13466255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13467255736Sdavidch
13468255736Sdavidch    /* get external phy info */
13469255736Sdavidch    sc->port.ext_phy_config =
13470255736Sdavidch        SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13471255736Sdavidch
13472255736Sdavidch    /* get the multifunction configuration */
13473255736Sdavidch    bxe_get_mf_cfg_info(sc);
13474255736Sdavidch
13475255736Sdavidch    /* get the mac address */
13476255736Sdavidch    if (IS_MF(sc)) {
13477255736Sdavidch        mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13478255736Sdavidch        mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13479255736Sdavidch    } else {
13480255736Sdavidch        mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13481255736Sdavidch        mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13482255736Sdavidch    }
13483255736Sdavidch
13484255736Sdavidch    if ((mac_lo == 0) && (mac_hi == 0)) {
13485255736Sdavidch        *sc->mac_addr_str = 0;
13486255736Sdavidch        BLOGE(sc, "No Ethernet address programmed!\n");
13487255736Sdavidch    } else {
13488255736Sdavidch        sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13489255736Sdavidch        sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13490255736Sdavidch        sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13491255736Sdavidch        sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13492255736Sdavidch        sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13493255736Sdavidch        sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13494255736Sdavidch        snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13495255736Sdavidch                 "%02x:%02x:%02x:%02x:%02x:%02x",
13496255736Sdavidch                 sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13497255736Sdavidch                 sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13498255736Sdavidch                 sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13499255736Sdavidch        BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13500255736Sdavidch    }
13501255736Sdavidch
13502255736Sdavidch    return (0);
13503255736Sdavidch}
13504255736Sdavidch
13505255736Sdavidchstatic void
13506255736Sdavidchbxe_get_tunable_params(struct bxe_softc *sc)
13507255736Sdavidch{
13508255736Sdavidch    /* sanity checks */
13509255736Sdavidch
13510255736Sdavidch    if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13511255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13512255736Sdavidch        (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13513255736Sdavidch        BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13514255736Sdavidch        bxe_interrupt_mode = INTR_MODE_MSIX;
13515255736Sdavidch    }
13516255736Sdavidch
13517255736Sdavidch    if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13518255736Sdavidch        BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13519255736Sdavidch        bxe_queue_count = 0;
13520255736Sdavidch    }
13521255736Sdavidch
13522255736Sdavidch    if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13523255736Sdavidch        if (bxe_max_rx_bufs == 0) {
13524255736Sdavidch            bxe_max_rx_bufs = RX_BD_USABLE;
13525255736Sdavidch        } else {
13526255736Sdavidch            BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13527255736Sdavidch            bxe_max_rx_bufs = 2048;
13528255736Sdavidch        }
13529255736Sdavidch    }
13530255736Sdavidch
13531255736Sdavidch    if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13532255736Sdavidch        BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13533255736Sdavidch        bxe_hc_rx_ticks = 25;
13534255736Sdavidch    }
13535255736Sdavidch
13536255736Sdavidch    if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13537255736Sdavidch        BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13538255736Sdavidch        bxe_hc_tx_ticks = 50;
13539255736Sdavidch    }
13540255736Sdavidch
13541255736Sdavidch    if (bxe_max_aggregation_size == 0) {
13542255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13543255736Sdavidch    }
13544255736Sdavidch
13545255736Sdavidch    if (bxe_max_aggregation_size > 0xffff) {
13546255736Sdavidch        BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13547255736Sdavidch              bxe_max_aggregation_size);
13548255736Sdavidch        bxe_max_aggregation_size = TPA_AGG_SIZE;
13549255736Sdavidch    }
13550255736Sdavidch
13551255736Sdavidch    if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13552255736Sdavidch        BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13553255736Sdavidch        bxe_mrrs = -1;
13554255736Sdavidch    }
13555255736Sdavidch
13556255736Sdavidch    if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13557255736Sdavidch        BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13558255736Sdavidch        bxe_autogreeen = 0;
13559255736Sdavidch    }
13560255736Sdavidch
13561255736Sdavidch    if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13562255736Sdavidch        BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13563255736Sdavidch        bxe_udp_rss = 0;
13564255736Sdavidch    }
13565255736Sdavidch
13566255736Sdavidch    /* pull in user settings */
13567255736Sdavidch
13568255736Sdavidch    sc->interrupt_mode       = bxe_interrupt_mode;
13569255736Sdavidch    sc->max_rx_bufs          = bxe_max_rx_bufs;
13570255736Sdavidch    sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13571255736Sdavidch    sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13572255736Sdavidch    sc->max_aggregation_size = bxe_max_aggregation_size;
13573255736Sdavidch    sc->mrrs                 = bxe_mrrs;
13574255736Sdavidch    sc->autogreeen           = bxe_autogreeen;
13575255736Sdavidch    sc->udp_rss              = bxe_udp_rss;
13576255736Sdavidch
13577255736Sdavidch    if (bxe_interrupt_mode == INTR_MODE_INTX) {
13578255736Sdavidch        sc->num_queues = 1;
13579255736Sdavidch    } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13580255736Sdavidch        sc->num_queues =
13581255736Sdavidch            min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13582255736Sdavidch                MAX_RSS_CHAINS);
13583255736Sdavidch        if (sc->num_queues > mp_ncpus) {
13584255736Sdavidch            sc->num_queues = mp_ncpus;
13585255736Sdavidch        }
13586255736Sdavidch    }
13587255736Sdavidch
13588255736Sdavidch    BLOGD(sc, DBG_LOAD,
13589255736Sdavidch          "User Config: "
13590258187Sedavis          "debug=0x%lx "
13591255736Sdavidch          "interrupt_mode=%d "
13592255736Sdavidch          "queue_count=%d "
13593255736Sdavidch          "hc_rx_ticks=%d "
13594255736Sdavidch          "hc_tx_ticks=%d "
13595255736Sdavidch          "rx_budget=%d "
13596255736Sdavidch          "max_aggregation_size=%d "
13597255736Sdavidch          "mrrs=%d "
13598255736Sdavidch          "autogreeen=%d "
13599255736Sdavidch          "udp_rss=%d\n",
13600255736Sdavidch          bxe_debug,
13601255736Sdavidch          sc->interrupt_mode,
13602255736Sdavidch          sc->num_queues,
13603255736Sdavidch          sc->hc_rx_ticks,
13604255736Sdavidch          sc->hc_tx_ticks,
13605255736Sdavidch          bxe_rx_budget,
13606255736Sdavidch          sc->max_aggregation_size,
13607255736Sdavidch          sc->mrrs,
13608255736Sdavidch          sc->autogreeen,
13609255736Sdavidch          sc->udp_rss);
13610255736Sdavidch}
13611255736Sdavidch
13612255736Sdavidchstatic void
13613255736Sdavidchbxe_media_detect(struct bxe_softc *sc)
13614255736Sdavidch{
13615255736Sdavidch    uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13616255736Sdavidch    switch (sc->link_params.phy[phy_idx].media_type) {
13617255736Sdavidch    case ELINK_ETH_PHY_SFPP_10G_FIBER:
13618256299Sedavis    case ELINK_ETH_PHY_XFP_FIBER:
13619256299Sedavis        BLOGI(sc, "Found 10Gb Fiber media.\n");
13620256299Sedavis        sc->media = IFM_10G_SR;
13621256299Sedavis        break;
13622255736Sdavidch    case ELINK_ETH_PHY_SFP_1G_FIBER:
13623256299Sedavis        BLOGI(sc, "Found 1Gb Fiber media.\n");
13624256299Sedavis        sc->media = IFM_1000_SX;
13625256299Sedavis        break;
13626255736Sdavidch    case ELINK_ETH_PHY_KR:
13627255736Sdavidch    case ELINK_ETH_PHY_CX4:
13628255736Sdavidch        BLOGI(sc, "Found 10GBase-CX4 media.\n");
13629255736Sdavidch        sc->media = IFM_10G_CX4;
13630255736Sdavidch        break;
13631255736Sdavidch    case ELINK_ETH_PHY_DA_TWINAX:
13632255736Sdavidch        BLOGI(sc, "Found 10Gb Twinax media.\n");
13633255736Sdavidch        sc->media = IFM_10G_TWINAX;
13634255736Sdavidch        break;
13635255736Sdavidch    case ELINK_ETH_PHY_BASE_T:
13636256299Sedavis        if (sc->link_params.speed_cap_mask[0] &
13637256299Sedavis            PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13638256299Sedavis            BLOGI(sc, "Found 10GBase-T media.\n");
13639256299Sedavis            sc->media = IFM_10G_T;
13640256299Sedavis        } else {
13641256299Sedavis            BLOGI(sc, "Found 1000Base-T media.\n");
13642256299Sedavis            sc->media = IFM_1000_T;
13643256299Sedavis        }
13644255736Sdavidch        break;
13645255736Sdavidch    case ELINK_ETH_PHY_NOT_PRESENT:
13646255736Sdavidch        BLOGI(sc, "Media not present.\n");
13647255736Sdavidch        sc->media = 0;
13648255736Sdavidch        break;
13649255736Sdavidch    case ELINK_ETH_PHY_UNSPECIFIED:
13650255736Sdavidch    default:
13651255736Sdavidch        BLOGI(sc, "Unknown media!\n");
13652255736Sdavidch        sc->media = 0;
13653255736Sdavidch        break;
13654255736Sdavidch    }
13655255736Sdavidch}
13656255736Sdavidch
13657255736Sdavidch#define GET_FIELD(value, fname)                     \
13658255736Sdavidch    (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13659255736Sdavidch#define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13660255736Sdavidch#define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
13661255736Sdavidch
13662255736Sdavidchstatic int
13663255736Sdavidchbxe_get_igu_cam_info(struct bxe_softc *sc)
13664255736Sdavidch{
13665255736Sdavidch    int pfid = SC_FUNC(sc);
13666255736Sdavidch    int igu_sb_id;
13667255736Sdavidch    uint32_t val;
13668255736Sdavidch    uint8_t fid, igu_sb_cnt = 0;
13669255736Sdavidch
13670255736Sdavidch    sc->igu_base_sb = 0xff;
13671255736Sdavidch
13672255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
13673255736Sdavidch        int vn = SC_VN(sc);
13674255736Sdavidch        igu_sb_cnt = sc->igu_sb_cnt;
13675255736Sdavidch        sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
13676255736Sdavidch                           FP_SB_MAX_E1x);
13677255736Sdavidch        sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
13678255736Sdavidch                          (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
13679255736Sdavidch        return (0);
13680255736Sdavidch    }
13681255736Sdavidch
13682255736Sdavidch    /* IGU in normal mode - read CAM */
13683255736Sdavidch    for (igu_sb_id = 0;
13684255736Sdavidch         igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
13685255736Sdavidch         igu_sb_id++) {
13686255736Sdavidch        val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
13687255736Sdavidch        if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
13688255736Sdavidch            continue;
13689255736Sdavidch        }
13690255736Sdavidch        fid = IGU_FID(val);
13691255736Sdavidch        if ((fid & IGU_FID_ENCODE_IS_PF)) {
13692255736Sdavidch            if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
13693255736Sdavidch                continue;
13694255736Sdavidch            }
13695255736Sdavidch            if (IGU_VEC(val) == 0) {
13696255736Sdavidch                /* default status block */
13697255736Sdavidch                sc->igu_dsb_id = igu_sb_id;
13698255736Sdavidch            } else {
13699255736Sdavidch                if (sc->igu_base_sb == 0xff) {
13700255736Sdavidch                    sc->igu_base_sb = igu_sb_id;
13701255736Sdavidch                }
13702255736Sdavidch                igu_sb_cnt++;
13703255736Sdavidch            }
13704255736Sdavidch        }
13705255736Sdavidch    }
13706255736Sdavidch
13707255736Sdavidch    /*
13708255736Sdavidch     * Due to new PF resource allocation by MFW T7.4 and above, it's optional
13709255736Sdavidch     * that number of CAM entries will not be equal to the value advertised in
13710255736Sdavidch     * PCI. Driver should use the minimal value of both as the actual status
13711255736Sdavidch     * block count
13712255736Sdavidch     */
13713255736Sdavidch    sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
13714255736Sdavidch
13715255736Sdavidch    if (igu_sb_cnt == 0) {
13716255736Sdavidch        BLOGE(sc, "CAM configuration error\n");
13717255736Sdavidch        return (-1);
13718255736Sdavidch    }
13719255736Sdavidch
13720255736Sdavidch    return (0);
13721255736Sdavidch}
13722255736Sdavidch
13723255736Sdavidch/*
13724255736Sdavidch * Gather various information from the device config space, the device itself,
13725255736Sdavidch * shmem, and the user input.
13726255736Sdavidch */
13727255736Sdavidchstatic int
13728255736Sdavidchbxe_get_device_info(struct bxe_softc *sc)
13729255736Sdavidch{
13730255736Sdavidch    uint32_t val;
13731255736Sdavidch    int rc;
13732255736Sdavidch
13733255736Sdavidch    /* Get the data for the device */
13734255736Sdavidch    sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
13735255736Sdavidch    sc->devinfo.device_id    = pci_get_device(sc->dev);
13736255736Sdavidch    sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
13737255736Sdavidch    sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
13738255736Sdavidch
13739255736Sdavidch    /* get the chip revision (chip metal comes from pci config space) */
13740255736Sdavidch    sc->devinfo.chip_id     =
13741255736Sdavidch    sc->link_params.chip_id =
13742255736Sdavidch        (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
13743255736Sdavidch         ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
13744255736Sdavidch         (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
13745255736Sdavidch         ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
13746255736Sdavidch
13747255736Sdavidch    /* force 57811 according to MISC register */
13748255736Sdavidch    if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
13749255736Sdavidch        if (CHIP_IS_57810(sc)) {
13750255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
13751255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
13752255736Sdavidch        } else if (CHIP_IS_57810_MF(sc)) {
13753255736Sdavidch            sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
13754255736Sdavidch                                   (sc->devinfo.chip_id & 0x0000ffff));
13755255736Sdavidch        }
13756255736Sdavidch        sc->devinfo.chip_id |= 0x1;
13757255736Sdavidch    }
13758255736Sdavidch
13759255736Sdavidch    BLOGD(sc, DBG_LOAD,
13760255736Sdavidch          "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
13761255736Sdavidch          sc->devinfo.chip_id,
13762255736Sdavidch          ((sc->devinfo.chip_id >> 16) & 0xffff),
13763255736Sdavidch          ((sc->devinfo.chip_id >> 12) & 0xf),
13764255736Sdavidch          ((sc->devinfo.chip_id >>  4) & 0xff),
13765255736Sdavidch          ((sc->devinfo.chip_id >>  0) & 0xf));
13766255736Sdavidch
13767255736Sdavidch    val = (REG_RD(sc, 0x2874) & 0x55);
13768255736Sdavidch    if ((sc->devinfo.chip_id & 0x1) ||
13769255736Sdavidch        (CHIP_IS_E1(sc) && val) ||
13770255736Sdavidch        (CHIP_IS_E1H(sc) && (val == 0x55))) {
13771255736Sdavidch        sc->flags |= BXE_ONE_PORT_FLAG;
13772255736Sdavidch        BLOGD(sc, DBG_LOAD, "single port device\n");
13773255736Sdavidch    }
13774255736Sdavidch
13775255736Sdavidch    /* set the doorbell size */
13776255736Sdavidch    sc->doorbell_size = (1 << BXE_DB_SHIFT);
13777255736Sdavidch
13778255736Sdavidch    /* determine whether the device is in 2 port or 4 port mode */
13779255736Sdavidch    sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
13780255736Sdavidch    if (CHIP_IS_E2E3(sc)) {
13781255736Sdavidch        /*
13782255736Sdavidch         * Read port4mode_en_ovwr[0]:
13783255736Sdavidch         *   If 1, four port mode is in port4mode_en_ovwr[1].
13784255736Sdavidch         *   If 0, four port mode is in port4mode_en[0].
13785255736Sdavidch         */
13786255736Sdavidch        val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
13787255736Sdavidch        if (val & 1) {
13788255736Sdavidch            val = ((val >> 1) & 1);
13789255736Sdavidch        } else {
13790255736Sdavidch            val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
13791255736Sdavidch        }
13792255736Sdavidch
13793255736Sdavidch        sc->devinfo.chip_port_mode =
13794255736Sdavidch            (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
13795255736Sdavidch
13796255736Sdavidch        BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
13797255736Sdavidch    }
13798255736Sdavidch
13799255736Sdavidch    /* get the function and path info for the device */
13800255736Sdavidch    bxe_get_function_num(sc);
13801255736Sdavidch
13802255736Sdavidch    /* get the shared memory base address */
13803255736Sdavidch    sc->devinfo.shmem_base     =
13804255736Sdavidch    sc->link_params.shmem_base =
13805255736Sdavidch        REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
13806255736Sdavidch    sc->devinfo.shmem2_base =
13807255736Sdavidch        REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
13808255736Sdavidch                                  MISC_REG_GENERIC_CR_0));
13809255736Sdavidch
13810255736Sdavidch    BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
13811255736Sdavidch          sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
13812255736Sdavidch
13813255736Sdavidch    if (!sc->devinfo.shmem_base) {
13814255736Sdavidch        /* this should ONLY prevent upcoming shmem reads */
13815255736Sdavidch        BLOGI(sc, "MCP not active\n");
13816255736Sdavidch        sc->flags |= BXE_NO_MCP_FLAG;
13817255736Sdavidch        return (0);
13818255736Sdavidch    }
13819255736Sdavidch
13820255736Sdavidch    /* make sure the shared memory contents are valid */
13821255736Sdavidch    val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
13822255736Sdavidch    if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
13823255736Sdavidch        (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
13824255736Sdavidch        BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
13825255736Sdavidch        return (0);
13826255736Sdavidch    }
13827255736Sdavidch    BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
13828255736Sdavidch
13829255736Sdavidch    /* get the bootcode version */
13830255736Sdavidch    sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
13831255736Sdavidch    snprintf(sc->devinfo.bc_ver_str,
13832255736Sdavidch             sizeof(sc->devinfo.bc_ver_str),
13833255736Sdavidch             "%d.%d.%d",
13834255736Sdavidch             ((sc->devinfo.bc_ver >> 24) & 0xff),
13835255736Sdavidch             ((sc->devinfo.bc_ver >> 16) & 0xff),
13836255736Sdavidch             ((sc->devinfo.bc_ver >>  8) & 0xff));
13837255736Sdavidch    BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
13838255736Sdavidch
13839255736Sdavidch    /* get the bootcode shmem address */
13840255736Sdavidch    sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
13841255736Sdavidch    BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
13842255736Sdavidch
13843255736Sdavidch    /* clean indirect addresses as they're not used */
13844255736Sdavidch    pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
13845255736Sdavidch    if (IS_PF(sc)) {
13846255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
13847255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
13848255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
13849255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
13850255736Sdavidch        if (CHIP_IS_E1x(sc)) {
13851255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
13852255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
13853255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
13854255736Sdavidch            REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
13855255736Sdavidch        }
13856255736Sdavidch
13857255736Sdavidch        /*
13858255736Sdavidch         * Enable internal target-read (in case we are probed after PF
13859255736Sdavidch         * FLR). Must be done prior to any BAR read access. Only for
13860255736Sdavidch         * 57712 and up
13861255736Sdavidch         */
13862255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
13863255736Sdavidch            REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
13864255736Sdavidch        }
13865255736Sdavidch    }
13866255736Sdavidch
13867255736Sdavidch    /* get the nvram size */
13868255736Sdavidch    val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
13869255736Sdavidch    sc->devinfo.flash_size =
13870255736Sdavidch        (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
13871255736Sdavidch    BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
13872255736Sdavidch
13873255736Sdavidch    /* get PCI capabilites */
13874255736Sdavidch    bxe_probe_pci_caps(sc);
13875255736Sdavidch
13876255736Sdavidch    bxe_set_power_state(sc, PCI_PM_D0);
13877255736Sdavidch
13878255736Sdavidch    /* get various configuration parameters from shmem */
13879255736Sdavidch    bxe_get_shmem_info(sc);
13880255736Sdavidch
13881255736Sdavidch    if (sc->devinfo.pcie_msix_cap_reg != 0) {
13882255736Sdavidch        val = pci_read_config(sc->dev,
13883255736Sdavidch                              (sc->devinfo.pcie_msix_cap_reg +
13884255736Sdavidch                               PCIR_MSIX_CTRL),
13885255736Sdavidch                              2);
13886255736Sdavidch        sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
13887255736Sdavidch    } else {
13888255736Sdavidch        sc->igu_sb_cnt = 1;
13889255736Sdavidch    }
13890255736Sdavidch
13891255736Sdavidch    sc->igu_base_addr = BAR_IGU_INTMEM;
13892255736Sdavidch
13893255736Sdavidch    /* initialize IGU parameters */
13894255736Sdavidch    if (CHIP_IS_E1x(sc)) {
13895255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_HC;
13896255736Sdavidch        sc->igu_dsb_id = DEF_SB_IGU_ID;
13897255736Sdavidch        sc->igu_base_sb = 0;
13898255736Sdavidch    } else {
13899255736Sdavidch        sc->devinfo.int_block = INT_BLOCK_IGU;
13900255736Sdavidch
13901255736Sdavidch        /* do not allow device reset during IGU info preocessing */
13902255736Sdavidch        bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13903255736Sdavidch
13904255736Sdavidch        val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
13905255736Sdavidch
13906255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13907255736Sdavidch            int tout = 5000;
13908255736Sdavidch
13909255736Sdavidch            BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
13910255736Sdavidch
13911255736Sdavidch            val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
13912255736Sdavidch            REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
13913255736Sdavidch            REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
13914255736Sdavidch
13915255736Sdavidch            while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13916255736Sdavidch                tout--;
13917255736Sdavidch                DELAY(1000);
13918255736Sdavidch            }
13919255736Sdavidch
13920255736Sdavidch            if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13921255736Sdavidch                BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
13922255736Sdavidch                bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13923255736Sdavidch                return (-1);
13924255736Sdavidch            }
13925255736Sdavidch        }
13926255736Sdavidch
13927255736Sdavidch        if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13928255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
13929255736Sdavidch            sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
13930255736Sdavidch        } else {
13931255736Sdavidch            BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
13932255736Sdavidch        }
13933255736Sdavidch
13934255736Sdavidch        rc = bxe_get_igu_cam_info(sc);
13935255736Sdavidch
13936255736Sdavidch        bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13937255736Sdavidch
13938255736Sdavidch        if (rc) {
13939255736Sdavidch            return (rc);
13940255736Sdavidch        }
13941255736Sdavidch    }
13942255736Sdavidch
13943255736Sdavidch    /*
13944255736Sdavidch     * Get base FW non-default (fast path) status block ID. This value is
13945255736Sdavidch     * used to initialize the fw_sb_id saved on the fp/queue structure to
13946255736Sdavidch     * determine the id used by the FW.
13947255736Sdavidch     */
13948255736Sdavidch    if (CHIP_IS_E1x(sc)) {
13949255736Sdavidch        sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
13950255736Sdavidch    } else {
13951255736Sdavidch        /*
13952255736Sdavidch         * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
13953255736Sdavidch         * the same queue are indicated on the same IGU SB). So we prefer
13954255736Sdavidch         * FW and IGU SBs to be the same value.
13955255736Sdavidch         */
13956255736Sdavidch        sc->base_fw_ndsb = sc->igu_base_sb;
13957255736Sdavidch    }
13958255736Sdavidch
13959255736Sdavidch    BLOGD(sc, DBG_LOAD,
13960255736Sdavidch          "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
13961255736Sdavidch          sc->igu_dsb_id, sc->igu_base_sb,
13962255736Sdavidch          sc->igu_sb_cnt, sc->base_fw_ndsb);
13963255736Sdavidch
13964255736Sdavidch    elink_phy_probe(&sc->link_params);
13965255736Sdavidch
13966255736Sdavidch    return (0);
13967255736Sdavidch}
13968255736Sdavidch
13969255736Sdavidchstatic void
13970255736Sdavidchbxe_link_settings_supported(struct bxe_softc *sc,
13971255736Sdavidch                            uint32_t         switch_cfg)
13972255736Sdavidch{
13973255736Sdavidch    uint32_t cfg_size = 0;
13974255736Sdavidch    uint32_t idx;
13975255736Sdavidch    uint8_t port = SC_PORT(sc);
13976255736Sdavidch
13977255736Sdavidch    /* aggregation of supported attributes of all external phys */
13978255736Sdavidch    sc->port.supported[0] = 0;
13979255736Sdavidch    sc->port.supported[1] = 0;
13980255736Sdavidch
13981255736Sdavidch    switch (sc->link_params.num_phys) {
13982255736Sdavidch    case 1:
13983255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
13984255736Sdavidch        cfg_size = 1;
13985255736Sdavidch        break;
13986255736Sdavidch    case 2:
13987255736Sdavidch        sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
13988255736Sdavidch        cfg_size = 1;
13989255736Sdavidch        break;
13990255736Sdavidch    case 3:
13991255736Sdavidch        if (sc->link_params.multi_phy_config &
13992255736Sdavidch            PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
13993255736Sdavidch            sc->port.supported[1] =
13994255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
13995255736Sdavidch            sc->port.supported[0] =
13996255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
13997255736Sdavidch        } else {
13998255736Sdavidch            sc->port.supported[0] =
13999255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY1].supported;
14000255736Sdavidch            sc->port.supported[1] =
14001255736Sdavidch                sc->link_params.phy[ELINK_EXT_PHY2].supported;
14002255736Sdavidch        }
14003255736Sdavidch        cfg_size = 2;
14004255736Sdavidch        break;
14005255736Sdavidch    }
14006255736Sdavidch
14007255736Sdavidch    if (!(sc->port.supported[0] || sc->port.supported[1])) {
14008255736Sdavidch        BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
14009255736Sdavidch              SHMEM_RD(sc,
14010255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config),
14011255736Sdavidch              SHMEM_RD(sc,
14012255736Sdavidch                       dev_info.port_hw_config[port].external_phy_config2));
14013255736Sdavidch        return;
14014255736Sdavidch    }
14015255736Sdavidch
14016255736Sdavidch    if (CHIP_IS_E3(sc))
14017255736Sdavidch        sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
14018255736Sdavidch    else {
14019255736Sdavidch        switch (switch_cfg) {
14020255736Sdavidch        case ELINK_SWITCH_CFG_1G:
14021255736Sdavidch            sc->port.phy_addr =
14022255736Sdavidch                REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
14023255736Sdavidch            break;
14024255736Sdavidch        case ELINK_SWITCH_CFG_10G:
14025255736Sdavidch            sc->port.phy_addr =
14026255736Sdavidch                REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
14027255736Sdavidch            break;
14028255736Sdavidch        default:
14029255736Sdavidch            BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14030255736Sdavidch                  sc->port.link_config[0]);
14031255736Sdavidch            return;
14032255736Sdavidch        }
14033255736Sdavidch    }
14034255736Sdavidch
14035255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14036255736Sdavidch
14037255736Sdavidch    /* mask what we support according to speed_cap_mask per configuration */
14038255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14039255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14040255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14041255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14042255736Sdavidch        }
14043255736Sdavidch
14044255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14045255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14046255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14047255736Sdavidch        }
14048255736Sdavidch
14049255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14050255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14051255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14052255736Sdavidch        }
14053255736Sdavidch
14054255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14055255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14056255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14057255736Sdavidch        }
14058255736Sdavidch
14059255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14060255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14061255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14062255736Sdavidch        }
14063255736Sdavidch
14064255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14065255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14066255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14067255736Sdavidch        }
14068255736Sdavidch
14069255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14070255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14071255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14072255736Sdavidch        }
14073255736Sdavidch
14074255736Sdavidch        if (!(sc->link_params.speed_cap_mask[idx] &
14075255736Sdavidch              PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14076255736Sdavidch            sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14077255736Sdavidch        }
14078255736Sdavidch    }
14079255736Sdavidch
14080255736Sdavidch    BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14081255736Sdavidch          sc->port.supported[0], sc->port.supported[1]);
14082255736Sdavidch}
14083255736Sdavidch
14084255736Sdavidchstatic void
14085255736Sdavidchbxe_link_settings_requested(struct bxe_softc *sc)
14086255736Sdavidch{
14087255736Sdavidch    uint32_t link_config;
14088255736Sdavidch    uint32_t idx;
14089255736Sdavidch    uint32_t cfg_size = 0;
14090255736Sdavidch
14091255736Sdavidch    sc->port.advertising[0] = 0;
14092255736Sdavidch    sc->port.advertising[1] = 0;
14093255736Sdavidch
14094255736Sdavidch    switch (sc->link_params.num_phys) {
14095255736Sdavidch    case 1:
14096255736Sdavidch    case 2:
14097255736Sdavidch        cfg_size = 1;
14098255736Sdavidch        break;
14099255736Sdavidch    case 3:
14100255736Sdavidch        cfg_size = 2;
14101255736Sdavidch        break;
14102255736Sdavidch    }
14103255736Sdavidch
14104255736Sdavidch    for (idx = 0; idx < cfg_size; idx++) {
14105255736Sdavidch        sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14106255736Sdavidch        link_config = sc->port.link_config[idx];
14107255736Sdavidch
14108255736Sdavidch        switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14109255736Sdavidch        case PORT_FEATURE_LINK_SPEED_AUTO:
14110255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14111255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14112255736Sdavidch                sc->port.advertising[idx] |= sc->port.supported[idx];
14113255736Sdavidch                if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14114255736Sdavidch                    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14115255736Sdavidch                    sc->port.advertising[idx] |=
14116255736Sdavidch                        (ELINK_SUPPORTED_100baseT_Half |
14117255736Sdavidch                         ELINK_SUPPORTED_100baseT_Full);
14118255736Sdavidch            } else {
14119255736Sdavidch                /* force 10G, no AN */
14120255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14121255736Sdavidch                sc->port.advertising[idx] |=
14122255736Sdavidch                    (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14123255736Sdavidch                continue;
14124255736Sdavidch            }
14125255736Sdavidch            break;
14126255736Sdavidch
14127255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_FULL:
14128255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14129255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14130255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14131255736Sdavidch                                              ADVERTISED_TP);
14132255736Sdavidch            } else {
14133255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14134255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14135255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14136255736Sdavidch                return;
14137255736Sdavidch            }
14138255736Sdavidch            break;
14139255736Sdavidch
14140255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10M_HALF:
14141255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14142255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14143255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14144255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14145255736Sdavidch                                              ADVERTISED_TP);
14146255736Sdavidch            } else {
14147255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14148255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14149255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14150255736Sdavidch                return;
14151255736Sdavidch            }
14152255736Sdavidch            break;
14153255736Sdavidch
14154255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_FULL:
14155255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14156255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14157255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14158255736Sdavidch                                              ADVERTISED_TP);
14159255736Sdavidch            } else {
14160255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14161255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14162255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14163255736Sdavidch                return;
14164255736Sdavidch            }
14165255736Sdavidch            break;
14166255736Sdavidch
14167255736Sdavidch        case PORT_FEATURE_LINK_SPEED_100M_HALF:
14168255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14169255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14170255736Sdavidch                sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14171255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14172255736Sdavidch                                              ADVERTISED_TP);
14173255736Sdavidch            } else {
14174255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14175255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14176255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14177255736Sdavidch                return;
14178255736Sdavidch            }
14179255736Sdavidch            break;
14180255736Sdavidch
14181255736Sdavidch        case PORT_FEATURE_LINK_SPEED_1G:
14182255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14183255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14184255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14185255736Sdavidch                                              ADVERTISED_TP);
14186255736Sdavidch            } else {
14187255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14188255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14189255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14190255736Sdavidch                return;
14191255736Sdavidch            }
14192255736Sdavidch            break;
14193255736Sdavidch
14194255736Sdavidch        case PORT_FEATURE_LINK_SPEED_2_5G:
14195255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14196255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14197255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14198255736Sdavidch                                              ADVERTISED_TP);
14199255736Sdavidch            } else {
14200255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14201255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14202255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14203255736Sdavidch                return;
14204255736Sdavidch            }
14205255736Sdavidch            break;
14206255736Sdavidch
14207255736Sdavidch        case PORT_FEATURE_LINK_SPEED_10G_CX4:
14208255736Sdavidch            if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14209255736Sdavidch                sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14210255736Sdavidch                sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14211255736Sdavidch                                              ADVERTISED_FIBRE);
14212255736Sdavidch            } else {
14213255736Sdavidch                BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14214255736Sdavidch                          "speed_cap_mask=0x%08x\n",
14215255736Sdavidch                      link_config, sc->link_params.speed_cap_mask[idx]);
14216255736Sdavidch                return;
14217255736Sdavidch            }
14218255736Sdavidch            break;
14219255736Sdavidch
14220255736Sdavidch        case PORT_FEATURE_LINK_SPEED_20G:
14221255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14222255736Sdavidch            break;
14223255736Sdavidch
14224255736Sdavidch        default:
14225255736Sdavidch            BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14226255736Sdavidch                      "speed_cap_mask=0x%08x\n",
14227255736Sdavidch                  link_config, sc->link_params.speed_cap_mask[idx]);
14228255736Sdavidch            sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14229255736Sdavidch            sc->port.advertising[idx] = sc->port.supported[idx];
14230255736Sdavidch            break;
14231255736Sdavidch        }
14232255736Sdavidch
14233255736Sdavidch        sc->link_params.req_flow_ctrl[idx] =
14234255736Sdavidch            (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14235255736Sdavidch
14236255736Sdavidch        if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14237255736Sdavidch            if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14238255736Sdavidch                sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14239255736Sdavidch            } else {
14240255736Sdavidch                bxe_set_requested_fc(sc);
14241255736Sdavidch            }
14242255736Sdavidch        }
14243255736Sdavidch
14244255736Sdavidch        BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14245255736Sdavidch                            "req_flow_ctrl=0x%x advertising=0x%x\n",
14246255736Sdavidch              sc->link_params.req_line_speed[idx],
14247255736Sdavidch              sc->link_params.req_duplex[idx],
14248255736Sdavidch              sc->link_params.req_flow_ctrl[idx],
14249255736Sdavidch              sc->port.advertising[idx]);
14250255736Sdavidch    }
14251255736Sdavidch}
14252255736Sdavidch
14253255736Sdavidchstatic void
14254255736Sdavidchbxe_get_phy_info(struct bxe_softc *sc)
14255255736Sdavidch{
14256255736Sdavidch    uint8_t port = SC_PORT(sc);
14257255736Sdavidch    uint32_t config = sc->port.config;
14258255736Sdavidch    uint32_t eee_mode;
14259255736Sdavidch
14260255736Sdavidch    /* shmem data already read in bxe_get_shmem_info() */
14261255736Sdavidch
14262255736Sdavidch    BLOGD(sc, DBG_LOAD, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14263255736Sdavidch                        "link_config0=0x%08x\n",
14264255736Sdavidch               sc->link_params.lane_config,
14265255736Sdavidch               sc->link_params.speed_cap_mask[0],
14266255736Sdavidch               sc->port.link_config[0]);
14267255736Sdavidch
14268255736Sdavidch    bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14269255736Sdavidch    bxe_link_settings_requested(sc);
14270255736Sdavidch
14271255736Sdavidch    if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14272255736Sdavidch        sc->link_params.feature_config_flags |=
14273255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14274255736Sdavidch    } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14275255736Sdavidch        sc->link_params.feature_config_flags &=
14276255736Sdavidch            ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14277255736Sdavidch    } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14278255736Sdavidch        sc->link_params.feature_config_flags |=
14279255736Sdavidch            ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14280255736Sdavidch    }
14281255736Sdavidch
14282255736Sdavidch    /* configure link feature according to nvram value */
14283255736Sdavidch    eee_mode =
14284255736Sdavidch        (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14285255736Sdavidch          PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14286255736Sdavidch         PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14287255736Sdavidch    if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14288255736Sdavidch        sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14289255736Sdavidch                                    ELINK_EEE_MODE_ENABLE_LPI |
14290255736Sdavidch                                    ELINK_EEE_MODE_OUTPUT_TIME);
14291255736Sdavidch    } else {
14292255736Sdavidch        sc->link_params.eee_mode = 0;
14293255736Sdavidch    }
14294255736Sdavidch
14295255736Sdavidch    /* get the media type */
14296255736Sdavidch    bxe_media_detect(sc);
14297255736Sdavidch}
14298255736Sdavidch
14299255736Sdavidchstatic void
14300255736Sdavidchbxe_get_params(struct bxe_softc *sc)
14301255736Sdavidch{
14302255736Sdavidch    /* get user tunable params */
14303255736Sdavidch    bxe_get_tunable_params(sc);
14304255736Sdavidch
14305255736Sdavidch    /* select the RX and TX ring sizes */
14306255736Sdavidch    sc->tx_ring_size = TX_BD_USABLE;
14307255736Sdavidch    sc->rx_ring_size = RX_BD_USABLE;
14308255736Sdavidch
14309255736Sdavidch    /* XXX disable WoL */
14310255736Sdavidch    sc->wol = 0;
14311255736Sdavidch}
14312255736Sdavidch
14313255736Sdavidchstatic void
14314255736Sdavidchbxe_set_modes_bitmap(struct bxe_softc *sc)
14315255736Sdavidch{
14316255736Sdavidch    uint32_t flags = 0;
14317255736Sdavidch
14318255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
14319255736Sdavidch        SET_FLAGS(flags, MODE_FPGA);
14320255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
14321255736Sdavidch        SET_FLAGS(flags, MODE_EMUL);
14322255736Sdavidch    } else {
14323255736Sdavidch        SET_FLAGS(flags, MODE_ASIC);
14324255736Sdavidch    }
14325255736Sdavidch
14326255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
14327255736Sdavidch        SET_FLAGS(flags, MODE_PORT4);
14328255736Sdavidch    } else {
14329255736Sdavidch        SET_FLAGS(flags, MODE_PORT2);
14330255736Sdavidch    }
14331255736Sdavidch
14332255736Sdavidch    if (CHIP_IS_E2(sc)) {
14333255736Sdavidch        SET_FLAGS(flags, MODE_E2);
14334255736Sdavidch    } else if (CHIP_IS_E3(sc)) {
14335255736Sdavidch        SET_FLAGS(flags, MODE_E3);
14336255736Sdavidch        if (CHIP_REV(sc) == CHIP_REV_Ax) {
14337255736Sdavidch            SET_FLAGS(flags, MODE_E3_A0);
14338255736Sdavidch        } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14339255736Sdavidch            SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14340255736Sdavidch        }
14341255736Sdavidch    }
14342255736Sdavidch
14343255736Sdavidch    if (IS_MF(sc)) {
14344255736Sdavidch        SET_FLAGS(flags, MODE_MF);
14345255736Sdavidch        switch (sc->devinfo.mf_info.mf_mode) {
14346255736Sdavidch        case MULTI_FUNCTION_SD:
14347255736Sdavidch            SET_FLAGS(flags, MODE_MF_SD);
14348255736Sdavidch            break;
14349255736Sdavidch        case MULTI_FUNCTION_SI:
14350255736Sdavidch            SET_FLAGS(flags, MODE_MF_SI);
14351255736Sdavidch            break;
14352255736Sdavidch        case MULTI_FUNCTION_AFEX:
14353255736Sdavidch            SET_FLAGS(flags, MODE_MF_AFEX);
14354255736Sdavidch            break;
14355255736Sdavidch        }
14356255736Sdavidch    } else {
14357255736Sdavidch        SET_FLAGS(flags, MODE_SF);
14358255736Sdavidch    }
14359255736Sdavidch
14360255736Sdavidch#if defined(__LITTLE_ENDIAN)
14361255736Sdavidch    SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14362255736Sdavidch#else /* __BIG_ENDIAN */
14363255736Sdavidch    SET_FLAGS(flags, MODE_BIG_ENDIAN);
14364255736Sdavidch#endif
14365255736Sdavidch
14366255736Sdavidch    INIT_MODE_FLAGS(sc) = flags;
14367255736Sdavidch}
14368255736Sdavidch
14369255736Sdavidchstatic int
14370255736Sdavidchbxe_alloc_hsi_mem(struct bxe_softc *sc)
14371255736Sdavidch{
14372255736Sdavidch    struct bxe_fastpath *fp;
14373255736Sdavidch    bus_addr_t busaddr;
14374255736Sdavidch    int max_agg_queues;
14375255736Sdavidch    int max_segments;
14376255736Sdavidch    bus_size_t max_size;
14377255736Sdavidch    bus_size_t max_seg_size;
14378255736Sdavidch    char buf[32];
14379255736Sdavidch    int rc;
14380255736Sdavidch    int i, j;
14381255736Sdavidch
14382255736Sdavidch    /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14383255736Sdavidch
14384255736Sdavidch    /* allocate the parent bus DMA tag */
14385255736Sdavidch    rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14386255736Sdavidch                            1,                        /* alignment */
14387255736Sdavidch                            0,                        /* boundary limit */
14388255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted low */
14389255736Sdavidch                            BUS_SPACE_MAXADDR,        /* restricted hi */
14390255736Sdavidch                            NULL,                     /* addr filter() */
14391255736Sdavidch                            NULL,                     /* addr filter() arg */
14392255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14393255736Sdavidch                            BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14394255736Sdavidch                            BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14395255736Sdavidch                            0,                        /* flags */
14396255736Sdavidch                            NULL,                     /* lock() */
14397255736Sdavidch                            NULL,                     /* lock() arg */
14398255736Sdavidch                            &sc->parent_dma_tag);     /* returned dma tag */
14399255736Sdavidch    if (rc != 0) {
14400255736Sdavidch        BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14401255736Sdavidch        return (1);
14402255736Sdavidch    }
14403255736Sdavidch
14404255736Sdavidch    /************************/
14405255736Sdavidch    /* DEFAULT STATUS BLOCK */
14406255736Sdavidch    /************************/
14407255736Sdavidch
14408255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14409255736Sdavidch                      &sc->def_sb_dma, "default status block") != 0) {
14410255736Sdavidch        /* XXX */
14411255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14412255736Sdavidch        return (1);
14413255736Sdavidch    }
14414255736Sdavidch
14415255736Sdavidch    sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14416255736Sdavidch
14417255736Sdavidch    /***************/
14418255736Sdavidch    /* EVENT QUEUE */
14419255736Sdavidch    /***************/
14420255736Sdavidch
14421255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14422255736Sdavidch                      &sc->eq_dma, "event queue") != 0) {
14423255736Sdavidch        /* XXX */
14424255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14425255736Sdavidch        sc->def_sb = NULL;
14426255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14427255736Sdavidch        return (1);
14428255736Sdavidch    }
14429255736Sdavidch
14430255736Sdavidch    sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14431255736Sdavidch
14432255736Sdavidch    /*************/
14433255736Sdavidch    /* SLOW PATH */
14434255736Sdavidch    /*************/
14435255736Sdavidch
14436255736Sdavidch    if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14437255736Sdavidch                      &sc->sp_dma, "slow path") != 0) {
14438255736Sdavidch        /* XXX */
14439255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14440255736Sdavidch        sc->eq = NULL;
14441255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14442255736Sdavidch        sc->def_sb = NULL;
14443255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14444255736Sdavidch        return (1);
14445255736Sdavidch    }
14446255736Sdavidch
14447255736Sdavidch    sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14448255736Sdavidch
14449255736Sdavidch    /*******************/
14450255736Sdavidch    /* SLOW PATH QUEUE */
14451255736Sdavidch    /*******************/
14452255736Sdavidch
14453255736Sdavidch    if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14454255736Sdavidch                      &sc->spq_dma, "slow path queue") != 0) {
14455255736Sdavidch        /* XXX */
14456255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14457255736Sdavidch        sc->sp = NULL;
14458255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14459255736Sdavidch        sc->eq = NULL;
14460255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14461255736Sdavidch        sc->def_sb = NULL;
14462255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14463255736Sdavidch        return (1);
14464255736Sdavidch    }
14465255736Sdavidch
14466255736Sdavidch    sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14467255736Sdavidch
14468255736Sdavidch    /***************************/
14469255736Sdavidch    /* FW DECOMPRESSION BUFFER */
14470255736Sdavidch    /***************************/
14471255736Sdavidch
14472255736Sdavidch    if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14473255736Sdavidch                      "fw decompression buffer") != 0) {
14474255736Sdavidch        /* XXX */
14475255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14476255736Sdavidch        sc->spq = NULL;
14477255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14478255736Sdavidch        sc->sp = NULL;
14479255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14480255736Sdavidch        sc->eq = NULL;
14481255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14482255736Sdavidch        sc->def_sb = NULL;
14483255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14484255736Sdavidch        return (1);
14485255736Sdavidch    }
14486255736Sdavidch
14487255736Sdavidch    sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14488255736Sdavidch
14489255736Sdavidch    if ((sc->gz_strm =
14490255736Sdavidch         malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14491255736Sdavidch        /* XXX */
14492255736Sdavidch        bxe_dma_free(sc, &sc->gz_buf_dma);
14493255736Sdavidch        sc->gz_buf = NULL;
14494255736Sdavidch        bxe_dma_free(sc, &sc->spq_dma);
14495255736Sdavidch        sc->spq = NULL;
14496255736Sdavidch        bxe_dma_free(sc, &sc->sp_dma);
14497255736Sdavidch        sc->sp = NULL;
14498255736Sdavidch        bxe_dma_free(sc, &sc->eq_dma);
14499255736Sdavidch        sc->eq = NULL;
14500255736Sdavidch        bxe_dma_free(sc, &sc->def_sb_dma);
14501255736Sdavidch        sc->def_sb = NULL;
14502255736Sdavidch        bus_dma_tag_destroy(sc->parent_dma_tag);
14503255736Sdavidch        return (1);
14504255736Sdavidch    }
14505255736Sdavidch
14506255736Sdavidch    /*************/
14507255736Sdavidch    /* FASTPATHS */
14508255736Sdavidch    /*************/
14509255736Sdavidch
14510255736Sdavidch    /* allocate DMA memory for each fastpath structure */
14511255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
14512255736Sdavidch        fp = &sc->fp[i];
14513255736Sdavidch        fp->sc    = sc;
14514255736Sdavidch        fp->index = i;
14515255736Sdavidch
14516255736Sdavidch        /*******************/
14517255736Sdavidch        /* FP STATUS BLOCK */
14518255736Sdavidch        /*******************/
14519255736Sdavidch
14520255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d status block", i);
14521255736Sdavidch        if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14522255736Sdavidch                          &fp->sb_dma, buf) != 0) {
14523255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14524255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14525255736Sdavidch            return (1);
14526255736Sdavidch        } else {
14527255736Sdavidch            if (CHIP_IS_E2E3(sc)) {
14528255736Sdavidch                fp->status_block.e2_sb =
14529255736Sdavidch                    (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14530255736Sdavidch            } else {
14531255736Sdavidch                fp->status_block.e1x_sb =
14532255736Sdavidch                    (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14533255736Sdavidch            }
14534255736Sdavidch        }
14535255736Sdavidch
14536255736Sdavidch        /******************/
14537255736Sdavidch        /* FP TX BD CHAIN */
14538255736Sdavidch        /******************/
14539255736Sdavidch
14540255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14541255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14542255736Sdavidch                          &fp->tx_dma, buf) != 0) {
14543255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14544255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14545255736Sdavidch            return (1);
14546255736Sdavidch        } else {
14547255736Sdavidch            fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14548255736Sdavidch        }
14549255736Sdavidch
14550255736Sdavidch        /* link together the tx bd chain pages */
14551255736Sdavidch        for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14552255736Sdavidch            /* index into the tx bd chain array to last entry per page */
14553255736Sdavidch            struct eth_tx_next_bd *tx_next_bd =
14554255736Sdavidch                &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14555255736Sdavidch            /* point to the next page and wrap from last page */
14556255736Sdavidch            busaddr = (fp->tx_dma.paddr +
14557255736Sdavidch                       (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14558255736Sdavidch            tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14559255736Sdavidch            tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14560255736Sdavidch        }
14561255736Sdavidch
14562255736Sdavidch        /******************/
14563255736Sdavidch        /* FP RX BD CHAIN */
14564255736Sdavidch        /******************/
14565255736Sdavidch
14566255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14567255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14568255736Sdavidch                          &fp->rx_dma, buf) != 0) {
14569255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14570255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14571255736Sdavidch            return (1);
14572255736Sdavidch        } else {
14573255736Sdavidch            fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14574255736Sdavidch        }
14575255736Sdavidch
14576255736Sdavidch        /* link together the rx bd chain pages */
14577255736Sdavidch        for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14578255736Sdavidch            /* index into the rx bd chain array to last entry per page */
14579255736Sdavidch            struct eth_rx_bd *rx_bd =
14580255736Sdavidch                &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14581255736Sdavidch            /* point to the next page and wrap from last page */
14582255736Sdavidch            busaddr = (fp->rx_dma.paddr +
14583255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14584255736Sdavidch            rx_bd->addr_hi = htole32(U64_HI(busaddr));
14585255736Sdavidch            rx_bd->addr_lo = htole32(U64_LO(busaddr));
14586255736Sdavidch        }
14587255736Sdavidch
14588255736Sdavidch        /*******************/
14589255736Sdavidch        /* FP RX RCQ CHAIN */
14590255736Sdavidch        /*******************/
14591255736Sdavidch
14592255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14593255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14594255736Sdavidch                          &fp->rcq_dma, buf) != 0) {
14595255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14596255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14597255736Sdavidch            return (1);
14598255736Sdavidch        } else {
14599255736Sdavidch            fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14600255736Sdavidch        }
14601255736Sdavidch
14602255736Sdavidch        /* link together the rcq chain pages */
14603255736Sdavidch        for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14604255736Sdavidch            /* index into the rcq chain array to last entry per page */
14605255736Sdavidch            struct eth_rx_cqe_next_page *rx_cqe_next =
14606255736Sdavidch                (struct eth_rx_cqe_next_page *)
14607255736Sdavidch                &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14608255736Sdavidch            /* point to the next page and wrap from last page */
14609255736Sdavidch            busaddr = (fp->rcq_dma.paddr +
14610255736Sdavidch                       (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14611255736Sdavidch            rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14612255736Sdavidch            rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14613255736Sdavidch        }
14614255736Sdavidch
14615255736Sdavidch        /*******************/
14616255736Sdavidch        /* FP RX SGE CHAIN */
14617255736Sdavidch        /*******************/
14618255736Sdavidch
14619255736Sdavidch        snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14620255736Sdavidch        if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14621255736Sdavidch                          &fp->rx_sge_dma, buf) != 0) {
14622255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14623255736Sdavidch            BLOGE(sc, "Failed to alloc %s\n", buf);
14624255736Sdavidch            return (1);
14625255736Sdavidch        } else {
14626255736Sdavidch            fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14627255736Sdavidch        }
14628255736Sdavidch
14629255736Sdavidch        /* link together the sge chain pages */
14630255736Sdavidch        for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14631255736Sdavidch            /* index into the rcq chain array to last entry per page */
14632255736Sdavidch            struct eth_rx_sge *rx_sge =
14633255736Sdavidch                &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14634255736Sdavidch            /* point to the next page and wrap from last page */
14635255736Sdavidch            busaddr = (fp->rx_sge_dma.paddr +
14636255736Sdavidch                       (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14637255736Sdavidch            rx_sge->addr_hi = htole32(U64_HI(busaddr));
14638255736Sdavidch            rx_sge->addr_lo = htole32(U64_LO(busaddr));
14639255736Sdavidch        }
14640255736Sdavidch
14641255736Sdavidch        /***********************/
14642255736Sdavidch        /* FP TX MBUF DMA MAPS */
14643255736Sdavidch        /***********************/
14644255736Sdavidch
14645255736Sdavidch        /* set required sizes before mapping to conserve resources */
14646266979Smarcel        if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
14647255736Sdavidch            max_size     = BXE_TSO_MAX_SIZE;
14648255736Sdavidch            max_segments = BXE_TSO_MAX_SEGMENTS;
14649255736Sdavidch            max_seg_size = BXE_TSO_MAX_SEG_SIZE;
14650255736Sdavidch        } else {
14651255736Sdavidch            max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
14652255736Sdavidch            max_segments = BXE_MAX_SEGMENTS;
14653255736Sdavidch            max_seg_size = MCLBYTES;
14654255736Sdavidch        }
14655255736Sdavidch
14656255736Sdavidch        /* create a dma tag for the tx mbufs */
14657255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14658255736Sdavidch                                1,                  /* alignment */
14659255736Sdavidch                                0,                  /* boundary limit */
14660255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14661255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14662255736Sdavidch                                NULL,               /* addr filter() */
14663255736Sdavidch                                NULL,               /* addr filter() arg */
14664255736Sdavidch                                max_size,           /* max map size */
14665255736Sdavidch                                max_segments,       /* num discontinuous */
14666255736Sdavidch                                max_seg_size,       /* max seg size */
14667255736Sdavidch                                0,                  /* flags */
14668255736Sdavidch                                NULL,               /* lock() */
14669255736Sdavidch                                NULL,               /* lock() arg */
14670255736Sdavidch                                &fp->tx_mbuf_tag);  /* returned dma tag */
14671255736Sdavidch        if (rc != 0) {
14672255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14673255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14674295830Sdavidcs                      "'fp %d tx mbufs' (%d)\n", i, rc);
14675255736Sdavidch            return (1);
14676255736Sdavidch        }
14677255736Sdavidch
14678255736Sdavidch        /* create dma maps for each of the tx mbuf clusters */
14679255736Sdavidch        for (j = 0; j < TX_BD_TOTAL; j++) {
14680255736Sdavidch            if (bus_dmamap_create(fp->tx_mbuf_tag,
14681255736Sdavidch                                  BUS_DMA_NOWAIT,
14682255736Sdavidch                                  &fp->tx_mbuf_chain[j].m_map)) {
14683255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14684255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14685295830Sdavidcs                          "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
14686255736Sdavidch                return (1);
14687255736Sdavidch            }
14688255736Sdavidch        }
14689255736Sdavidch
14690255736Sdavidch        /***********************/
14691255736Sdavidch        /* FP RX MBUF DMA MAPS */
14692255736Sdavidch        /***********************/
14693255736Sdavidch
14694255736Sdavidch        /* create a dma tag for the rx mbufs */
14695255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14696255736Sdavidch                                1,                  /* alignment */
14697255736Sdavidch                                0,                  /* boundary limit */
14698255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14699255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14700255736Sdavidch                                NULL,               /* addr filter() */
14701255736Sdavidch                                NULL,               /* addr filter() arg */
14702255736Sdavidch                                MJUM9BYTES,         /* max map size */
14703255736Sdavidch                                1,                  /* num discontinuous */
14704255736Sdavidch                                MJUM9BYTES,         /* max seg size */
14705255736Sdavidch                                0,                  /* flags */
14706255736Sdavidch                                NULL,               /* lock() */
14707255736Sdavidch                                NULL,               /* lock() arg */
14708255736Sdavidch                                &fp->rx_mbuf_tag);  /* returned dma tag */
14709255736Sdavidch        if (rc != 0) {
14710255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14711255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14712295830Sdavidcs                      "'fp %d rx mbufs' (%d)\n", i, rc);
14713255736Sdavidch            return (1);
14714255736Sdavidch        }
14715255736Sdavidch
14716255736Sdavidch        /* create dma maps for each of the rx mbuf clusters */
14717255736Sdavidch        for (j = 0; j < RX_BD_TOTAL; j++) {
14718255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
14719255736Sdavidch                                  BUS_DMA_NOWAIT,
14720255736Sdavidch                                  &fp->rx_mbuf_chain[j].m_map)) {
14721255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14722255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14723295830Sdavidcs                          "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
14724255736Sdavidch                return (1);
14725255736Sdavidch            }
14726255736Sdavidch        }
14727255736Sdavidch
14728255736Sdavidch        /* create dma map for the spare rx mbuf cluster */
14729255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
14730255736Sdavidch                              BUS_DMA_NOWAIT,
14731255736Sdavidch                              &fp->rx_mbuf_spare_map)) {
14732255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14733255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14734295830Sdavidcs                      "'fp %d spare rx mbuf' (%d)\n", i, rc);
14735255736Sdavidch            return (1);
14736255736Sdavidch        }
14737255736Sdavidch
14738255736Sdavidch        /***************************/
14739255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
14740255736Sdavidch        /***************************/
14741255736Sdavidch
14742255736Sdavidch        /* create a dma tag for the rx sge mbufs */
14743255736Sdavidch        rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14744255736Sdavidch                                1,                  /* alignment */
14745255736Sdavidch                                0,                  /* boundary limit */
14746255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted low */
14747255736Sdavidch                                BUS_SPACE_MAXADDR,  /* restricted hi */
14748255736Sdavidch                                NULL,               /* addr filter() */
14749255736Sdavidch                                NULL,               /* addr filter() arg */
14750255736Sdavidch                                BCM_PAGE_SIZE,      /* max map size */
14751255736Sdavidch                                1,                  /* num discontinuous */
14752255736Sdavidch                                BCM_PAGE_SIZE,      /* max seg size */
14753255736Sdavidch                                0,                  /* flags */
14754255736Sdavidch                                NULL,               /* lock() */
14755255736Sdavidch                                NULL,               /* lock() arg */
14756255736Sdavidch                                &fp->rx_sge_mbuf_tag); /* returned dma tag */
14757255736Sdavidch        if (rc != 0) {
14758255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14759255736Sdavidch            BLOGE(sc, "Failed to create dma tag for "
14760295830Sdavidcs                      "'fp %d rx sge mbufs' (%d)\n", i, rc);
14761255736Sdavidch            return (1);
14762255736Sdavidch        }
14763255736Sdavidch
14764255736Sdavidch        /* create dma maps for the rx sge mbuf clusters */
14765255736Sdavidch        for (j = 0; j < RX_SGE_TOTAL; j++) {
14766255736Sdavidch            if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14767255736Sdavidch                                  BUS_DMA_NOWAIT,
14768255736Sdavidch                                  &fp->rx_sge_mbuf_chain[j].m_map)) {
14769255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14770255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14771295830Sdavidcs                          "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
14772255736Sdavidch                return (1);
14773255736Sdavidch            }
14774255736Sdavidch        }
14775255736Sdavidch
14776255736Sdavidch        /* create dma map for the spare rx sge mbuf cluster */
14777255736Sdavidch        if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14778255736Sdavidch                              BUS_DMA_NOWAIT,
14779255736Sdavidch                              &fp->rx_sge_mbuf_spare_map)) {
14780255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14781255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14782295830Sdavidcs                      "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
14783255736Sdavidch            return (1);
14784255736Sdavidch        }
14785255736Sdavidch
14786255736Sdavidch        /***************************/
14787255736Sdavidch        /* FP RX TPA MBUF DMA MAPS */
14788255736Sdavidch        /***************************/
14789255736Sdavidch
14790255736Sdavidch        /* create dma maps for the rx tpa mbuf clusters */
14791255736Sdavidch        max_agg_queues = MAX_AGG_QS(sc);
14792255736Sdavidch
14793255736Sdavidch        for (j = 0; j < max_agg_queues; j++) {
14794255736Sdavidch            if (bus_dmamap_create(fp->rx_mbuf_tag,
14795255736Sdavidch                                  BUS_DMA_NOWAIT,
14796255736Sdavidch                                  &fp->rx_tpa_info[j].bd.m_map)) {
14797255736Sdavidch                /* XXX unwind and free previous fastpath allocations */
14798255736Sdavidch                BLOGE(sc, "Failed to create dma map for "
14799295830Sdavidcs                          "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
14800255736Sdavidch                return (1);
14801255736Sdavidch            }
14802255736Sdavidch        }
14803255736Sdavidch
14804255736Sdavidch        /* create dma map for the spare rx tpa mbuf cluster */
14805255736Sdavidch        if (bus_dmamap_create(fp->rx_mbuf_tag,
14806255736Sdavidch                              BUS_DMA_NOWAIT,
14807255736Sdavidch                              &fp->rx_tpa_info_mbuf_spare_map)) {
14808255736Sdavidch            /* XXX unwind and free previous fastpath allocations */
14809255736Sdavidch            BLOGE(sc, "Failed to create dma map for "
14810295830Sdavidcs                      "'fp %d spare rx tpa mbuf' (%d)\n", i, rc);
14811255736Sdavidch            return (1);
14812255736Sdavidch        }
14813255736Sdavidch
14814255736Sdavidch        bxe_init_sge_ring_bit_mask(fp);
14815255736Sdavidch    }
14816255736Sdavidch
14817255736Sdavidch    return (0);
14818255736Sdavidch}
14819255736Sdavidch
14820255736Sdavidchstatic void
14821255736Sdavidchbxe_free_hsi_mem(struct bxe_softc *sc)
14822255736Sdavidch{
14823255736Sdavidch    struct bxe_fastpath *fp;
14824255736Sdavidch    int max_agg_queues;
14825255736Sdavidch    int i, j;
14826255736Sdavidch
14827255736Sdavidch    if (sc->parent_dma_tag == NULL) {
14828255736Sdavidch        return; /* assume nothing was allocated */
14829255736Sdavidch    }
14830255736Sdavidch
14831255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
14832255736Sdavidch        fp = &sc->fp[i];
14833255736Sdavidch
14834255736Sdavidch        /*******************/
14835255736Sdavidch        /* FP STATUS BLOCK */
14836255736Sdavidch        /*******************/
14837255736Sdavidch
14838255736Sdavidch        bxe_dma_free(sc, &fp->sb_dma);
14839255736Sdavidch        memset(&fp->status_block, 0, sizeof(fp->status_block));
14840255736Sdavidch
14841255736Sdavidch        /******************/
14842255736Sdavidch        /* FP TX BD CHAIN */
14843255736Sdavidch        /******************/
14844255736Sdavidch
14845255736Sdavidch        bxe_dma_free(sc, &fp->tx_dma);
14846255736Sdavidch        fp->tx_chain = NULL;
14847255736Sdavidch
14848255736Sdavidch        /******************/
14849255736Sdavidch        /* FP RX BD CHAIN */
14850255736Sdavidch        /******************/
14851255736Sdavidch
14852255736Sdavidch        bxe_dma_free(sc, &fp->rx_dma);
14853255736Sdavidch        fp->rx_chain = NULL;
14854255736Sdavidch
14855255736Sdavidch        /*******************/
14856255736Sdavidch        /* FP RX RCQ CHAIN */
14857255736Sdavidch        /*******************/
14858255736Sdavidch
14859255736Sdavidch        bxe_dma_free(sc, &fp->rcq_dma);
14860255736Sdavidch        fp->rcq_chain = NULL;
14861255736Sdavidch
14862255736Sdavidch        /*******************/
14863255736Sdavidch        /* FP RX SGE CHAIN */
14864255736Sdavidch        /*******************/
14865255736Sdavidch
14866255736Sdavidch        bxe_dma_free(sc, &fp->rx_sge_dma);
14867255736Sdavidch        fp->rx_sge_chain = NULL;
14868255736Sdavidch
14869255736Sdavidch        /***********************/
14870255736Sdavidch        /* FP TX MBUF DMA MAPS */
14871255736Sdavidch        /***********************/
14872255736Sdavidch
14873255736Sdavidch        if (fp->tx_mbuf_tag != NULL) {
14874255736Sdavidch            for (j = 0; j < TX_BD_TOTAL; j++) {
14875255736Sdavidch                if (fp->tx_mbuf_chain[j].m_map != NULL) {
14876255736Sdavidch                    bus_dmamap_unload(fp->tx_mbuf_tag,
14877255736Sdavidch                                      fp->tx_mbuf_chain[j].m_map);
14878255736Sdavidch                    bus_dmamap_destroy(fp->tx_mbuf_tag,
14879255736Sdavidch                                       fp->tx_mbuf_chain[j].m_map);
14880255736Sdavidch                }
14881255736Sdavidch            }
14882255736Sdavidch
14883255736Sdavidch            bus_dma_tag_destroy(fp->tx_mbuf_tag);
14884255736Sdavidch            fp->tx_mbuf_tag = NULL;
14885255736Sdavidch        }
14886255736Sdavidch
14887255736Sdavidch        /***********************/
14888255736Sdavidch        /* FP RX MBUF DMA MAPS */
14889255736Sdavidch        /***********************/
14890255736Sdavidch
14891255736Sdavidch        if (fp->rx_mbuf_tag != NULL) {
14892255736Sdavidch            for (j = 0; j < RX_BD_TOTAL; j++) {
14893255736Sdavidch                if (fp->rx_mbuf_chain[j].m_map != NULL) {
14894255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
14895255736Sdavidch                                      fp->rx_mbuf_chain[j].m_map);
14896255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
14897255736Sdavidch                                       fp->rx_mbuf_chain[j].m_map);
14898255736Sdavidch                }
14899255736Sdavidch            }
14900255736Sdavidch
14901255736Sdavidch            if (fp->rx_mbuf_spare_map != NULL) {
14902255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14903255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14904255736Sdavidch            }
14905255736Sdavidch
14906255736Sdavidch            /***************************/
14907255736Sdavidch            /* FP RX TPA MBUF DMA MAPS */
14908255736Sdavidch            /***************************/
14909255736Sdavidch
14910255736Sdavidch            max_agg_queues = MAX_AGG_QS(sc);
14911255736Sdavidch
14912255736Sdavidch            for (j = 0; j < max_agg_queues; j++) {
14913255736Sdavidch                if (fp->rx_tpa_info[j].bd.m_map != NULL) {
14914255736Sdavidch                    bus_dmamap_unload(fp->rx_mbuf_tag,
14915255736Sdavidch                                      fp->rx_tpa_info[j].bd.m_map);
14916255736Sdavidch                    bus_dmamap_destroy(fp->rx_mbuf_tag,
14917255736Sdavidch                                       fp->rx_tpa_info[j].bd.m_map);
14918255736Sdavidch                }
14919255736Sdavidch            }
14920255736Sdavidch
14921255736Sdavidch            if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
14922255736Sdavidch                bus_dmamap_unload(fp->rx_mbuf_tag,
14923255736Sdavidch                                  fp->rx_tpa_info_mbuf_spare_map);
14924255736Sdavidch                bus_dmamap_destroy(fp->rx_mbuf_tag,
14925255736Sdavidch                                   fp->rx_tpa_info_mbuf_spare_map);
14926255736Sdavidch            }
14927255736Sdavidch
14928255736Sdavidch            bus_dma_tag_destroy(fp->rx_mbuf_tag);
14929255736Sdavidch            fp->rx_mbuf_tag = NULL;
14930255736Sdavidch        }
14931255736Sdavidch
14932255736Sdavidch        /***************************/
14933255736Sdavidch        /* FP RX SGE MBUF DMA MAPS */
14934255736Sdavidch        /***************************/
14935255736Sdavidch
14936255736Sdavidch        if (fp->rx_sge_mbuf_tag != NULL) {
14937255736Sdavidch            for (j = 0; j < RX_SGE_TOTAL; j++) {
14938255736Sdavidch                if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
14939255736Sdavidch                    bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14940255736Sdavidch                                      fp->rx_sge_mbuf_chain[j].m_map);
14941255736Sdavidch                    bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14942255736Sdavidch                                       fp->rx_sge_mbuf_chain[j].m_map);
14943255736Sdavidch                }
14944255736Sdavidch            }
14945255736Sdavidch
14946255736Sdavidch            if (fp->rx_sge_mbuf_spare_map != NULL) {
14947255736Sdavidch                bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14948255736Sdavidch                                  fp->rx_sge_mbuf_spare_map);
14949255736Sdavidch                bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14950255736Sdavidch                                   fp->rx_sge_mbuf_spare_map);
14951255736Sdavidch            }
14952255736Sdavidch
14953255736Sdavidch            bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
14954255736Sdavidch            fp->rx_sge_mbuf_tag = NULL;
14955255736Sdavidch        }
14956255736Sdavidch    }
14957255736Sdavidch
14958255736Sdavidch    /***************************/
14959255736Sdavidch    /* FW DECOMPRESSION BUFFER */
14960255736Sdavidch    /***************************/
14961255736Sdavidch
14962255736Sdavidch    bxe_dma_free(sc, &sc->gz_buf_dma);
14963255736Sdavidch    sc->gz_buf = NULL;
14964255736Sdavidch    free(sc->gz_strm, M_DEVBUF);
14965255736Sdavidch    sc->gz_strm = NULL;
14966255736Sdavidch
14967255736Sdavidch    /*******************/
14968255736Sdavidch    /* SLOW PATH QUEUE */
14969255736Sdavidch    /*******************/
14970255736Sdavidch
14971255736Sdavidch    bxe_dma_free(sc, &sc->spq_dma);
14972255736Sdavidch    sc->spq = NULL;
14973255736Sdavidch
14974255736Sdavidch    /*************/
14975255736Sdavidch    /* SLOW PATH */
14976255736Sdavidch    /*************/
14977255736Sdavidch
14978255736Sdavidch    bxe_dma_free(sc, &sc->sp_dma);
14979255736Sdavidch    sc->sp = NULL;
14980255736Sdavidch
14981255736Sdavidch    /***************/
14982255736Sdavidch    /* EVENT QUEUE */
14983255736Sdavidch    /***************/
14984255736Sdavidch
14985255736Sdavidch    bxe_dma_free(sc, &sc->eq_dma);
14986255736Sdavidch    sc->eq = NULL;
14987255736Sdavidch
14988255736Sdavidch    /************************/
14989255736Sdavidch    /* DEFAULT STATUS BLOCK */
14990255736Sdavidch    /************************/
14991255736Sdavidch
14992255736Sdavidch    bxe_dma_free(sc, &sc->def_sb_dma);
14993255736Sdavidch    sc->def_sb = NULL;
14994255736Sdavidch
14995255736Sdavidch    bus_dma_tag_destroy(sc->parent_dma_tag);
14996255736Sdavidch    sc->parent_dma_tag = NULL;
14997255736Sdavidch}
14998255736Sdavidch
14999255736Sdavidch/*
15000255736Sdavidch * Previous driver DMAE transaction may have occurred when pre-boot stage
15001255736Sdavidch * ended and boot began. This would invalidate the addresses of the
15002255736Sdavidch * transaction, resulting in was-error bit set in the PCI causing all
15003255736Sdavidch * hw-to-host PCIe transactions to timeout. If this happened we want to clear
15004255736Sdavidch * the interrupt which detected this from the pglueb and the was-done bit
15005255736Sdavidch */
15006255736Sdavidchstatic void
15007255736Sdavidchbxe_prev_interrupted_dmae(struct bxe_softc *sc)
15008255736Sdavidch{
15009255736Sdavidch    uint32_t val;
15010255736Sdavidch
15011255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
15012255736Sdavidch        val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
15013255736Sdavidch        if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
15014255736Sdavidch            BLOGD(sc, DBG_LOAD,
15015255736Sdavidch                  "Clearing 'was-error' bit that was set in pglueb");
15016255736Sdavidch            REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
15017255736Sdavidch        }
15018255736Sdavidch    }
15019255736Sdavidch}
15020255736Sdavidch
15021255736Sdavidchstatic int
15022255736Sdavidchbxe_prev_mcp_done(struct bxe_softc *sc)
15023255736Sdavidch{
15024255736Sdavidch    uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15025255736Sdavidch                                 DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15026255736Sdavidch    if (!rc) {
15027255736Sdavidch        BLOGE(sc, "MCP response failure, aborting\n");
15028255736Sdavidch        return (-1);
15029255736Sdavidch    }
15030255736Sdavidch
15031255736Sdavidch    return (0);
15032255736Sdavidch}
15033255736Sdavidch
15034255736Sdavidchstatic struct bxe_prev_list_node *
15035255736Sdavidchbxe_prev_path_get_entry(struct bxe_softc *sc)
15036255736Sdavidch{
15037255736Sdavidch    struct bxe_prev_list_node *tmp;
15038255736Sdavidch
15039255736Sdavidch    LIST_FOREACH(tmp, &bxe_prev_list, node) {
15040255736Sdavidch        if ((sc->pcie_bus == tmp->bus) &&
15041255736Sdavidch            (sc->pcie_device == tmp->slot) &&
15042255736Sdavidch            (SC_PATH(sc) == tmp->path)) {
15043255736Sdavidch            return (tmp);
15044255736Sdavidch        }
15045255736Sdavidch    }
15046255736Sdavidch
15047255736Sdavidch    return (NULL);
15048255736Sdavidch}
15049255736Sdavidch
15050255736Sdavidchstatic uint8_t
15051255736Sdavidchbxe_prev_is_path_marked(struct bxe_softc *sc)
15052255736Sdavidch{
15053255736Sdavidch    struct bxe_prev_list_node *tmp;
15054255736Sdavidch    int rc = FALSE;
15055255736Sdavidch
15056255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15057255736Sdavidch
15058255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15059255736Sdavidch    if (tmp) {
15060255736Sdavidch        if (tmp->aer) {
15061255736Sdavidch            BLOGD(sc, DBG_LOAD,
15062255736Sdavidch                  "Path %d/%d/%d was marked by AER\n",
15063255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15064255736Sdavidch        } else {
15065255736Sdavidch            rc = TRUE;
15066255736Sdavidch            BLOGD(sc, DBG_LOAD,
15067255736Sdavidch                  "Path %d/%d/%d was already cleaned from previous drivers\n",
15068255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15069255736Sdavidch        }
15070255736Sdavidch    }
15071255736Sdavidch
15072255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15073255736Sdavidch
15074255736Sdavidch    return (rc);
15075255736Sdavidch}
15076255736Sdavidch
15077255736Sdavidchstatic int
15078255736Sdavidchbxe_prev_mark_path(struct bxe_softc *sc,
15079255736Sdavidch                   uint8_t          after_undi)
15080255736Sdavidch{
15081255736Sdavidch    struct bxe_prev_list_node *tmp;
15082255736Sdavidch
15083255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15084255736Sdavidch
15085255736Sdavidch    /* Check whether the entry for this path already exists */
15086255736Sdavidch    tmp = bxe_prev_path_get_entry(sc);
15087255736Sdavidch    if (tmp) {
15088255736Sdavidch        if (!tmp->aer) {
15089255736Sdavidch            BLOGD(sc, DBG_LOAD,
15090255736Sdavidch                  "Re-marking AER in path %d/%d/%d\n",
15091255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15092255736Sdavidch        } else {
15093255736Sdavidch            BLOGD(sc, DBG_LOAD,
15094255736Sdavidch                  "Removing AER indication from path %d/%d/%d\n",
15095255736Sdavidch                  sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15096255736Sdavidch            tmp->aer = 0;
15097255736Sdavidch        }
15098255736Sdavidch
15099255736Sdavidch        mtx_unlock(&bxe_prev_mtx);
15100255736Sdavidch        return (0);
15101255736Sdavidch    }
15102255736Sdavidch
15103255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15104255736Sdavidch
15105255736Sdavidch    /* Create an entry for this path and add it */
15106255736Sdavidch    tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15107255736Sdavidch                 (M_NOWAIT | M_ZERO));
15108255736Sdavidch    if (!tmp) {
15109255736Sdavidch        BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15110255736Sdavidch        return (-1);
15111255736Sdavidch    }
15112255736Sdavidch
15113255736Sdavidch    tmp->bus  = sc->pcie_bus;
15114255736Sdavidch    tmp->slot = sc->pcie_device;
15115255736Sdavidch    tmp->path = SC_PATH(sc);
15116255736Sdavidch    tmp->aer  = 0;
15117255736Sdavidch    tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15118255736Sdavidch
15119255736Sdavidch    mtx_lock(&bxe_prev_mtx);
15120255736Sdavidch
15121255736Sdavidch    BLOGD(sc, DBG_LOAD,
15122255736Sdavidch          "Marked path %d/%d/%d - finished previous unload\n",
15123255736Sdavidch          sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15124255736Sdavidch    LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15125255736Sdavidch
15126255736Sdavidch    mtx_unlock(&bxe_prev_mtx);
15127255736Sdavidch
15128255736Sdavidch    return (0);
15129255736Sdavidch}
15130255736Sdavidch
15131255736Sdavidchstatic int
15132255736Sdavidchbxe_do_flr(struct bxe_softc *sc)
15133255736Sdavidch{
15134255736Sdavidch    int i;
15135255736Sdavidch
15136255736Sdavidch    /* only E2 and onwards support FLR */
15137255736Sdavidch    if (CHIP_IS_E1x(sc)) {
15138255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15139255736Sdavidch        return (-1);
15140255736Sdavidch    }
15141255736Sdavidch
15142255736Sdavidch    /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15143255736Sdavidch    if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15144255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15145255736Sdavidch              sc->devinfo.bc_ver);
15146255736Sdavidch        return (-1);
15147255736Sdavidch    }
15148255736Sdavidch
15149255736Sdavidch    /* Wait for Transaction Pending bit clean */
15150255736Sdavidch    for (i = 0; i < 4; i++) {
15151255736Sdavidch        if (i) {
15152255736Sdavidch            DELAY(((1 << (i - 1)) * 100) * 1000);
15153255736Sdavidch        }
15154255736Sdavidch
15155255736Sdavidch        if (!bxe_is_pcie_pending(sc)) {
15156255736Sdavidch            goto clear;
15157255736Sdavidch        }
15158255736Sdavidch    }
15159255736Sdavidch
15160255736Sdavidch    BLOGE(sc, "PCIE transaction is not cleared, "
15161255736Sdavidch              "proceeding with reset anyway\n");
15162255736Sdavidch
15163255736Sdavidchclear:
15164255736Sdavidch
15165255736Sdavidch    BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15166255736Sdavidch    bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15167255736Sdavidch
15168255736Sdavidch    return (0);
15169255736Sdavidch}
15170255736Sdavidch
15171255736Sdavidchstruct bxe_mac_vals {
15172255736Sdavidch    uint32_t xmac_addr;
15173255736Sdavidch    uint32_t xmac_val;
15174255736Sdavidch    uint32_t emac_addr;
15175255736Sdavidch    uint32_t emac_val;
15176255736Sdavidch    uint32_t umac_addr;
15177255736Sdavidch    uint32_t umac_val;
15178255736Sdavidch    uint32_t bmac_addr;
15179255736Sdavidch    uint32_t bmac_val[2];
15180255736Sdavidch};
15181255736Sdavidch
15182255736Sdavidchstatic void
15183255736Sdavidchbxe_prev_unload_close_mac(struct bxe_softc *sc,
15184255736Sdavidch                          struct bxe_mac_vals *vals)
15185255736Sdavidch{
15186255736Sdavidch    uint32_t val, base_addr, offset, mask, reset_reg;
15187255736Sdavidch    uint8_t mac_stopped = FALSE;
15188255736Sdavidch    uint8_t port = SC_PORT(sc);
15189255736Sdavidch    uint32_t wb_data[2];
15190255736Sdavidch
15191255736Sdavidch    /* reset addresses as they also mark which values were changed */
15192255736Sdavidch    vals->bmac_addr = 0;
15193255736Sdavidch    vals->umac_addr = 0;
15194255736Sdavidch    vals->xmac_addr = 0;
15195255736Sdavidch    vals->emac_addr = 0;
15196255736Sdavidch
15197255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15198255736Sdavidch
15199255736Sdavidch    if (!CHIP_IS_E3(sc)) {
15200255736Sdavidch        val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15201255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15202255736Sdavidch        if ((mask & reset_reg) && val) {
15203255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15204255736Sdavidch            base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15205255736Sdavidch                                    : NIG_REG_INGRESS_BMAC0_MEM;
15206255736Sdavidch            offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15207255736Sdavidch                                    : BIGMAC_REGISTER_BMAC_CONTROL;
15208255736Sdavidch
15209255736Sdavidch            /*
15210255736Sdavidch             * use rd/wr since we cannot use dmae. This is safe
15211255736Sdavidch             * since MCP won't access the bus due to the request
15212255736Sdavidch             * to unload, and no function on the path can be
15213255736Sdavidch             * loaded at this time.
15214255736Sdavidch             */
15215255736Sdavidch            wb_data[0] = REG_RD(sc, base_addr + offset);
15216255736Sdavidch            wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15217255736Sdavidch            vals->bmac_addr = base_addr + offset;
15218255736Sdavidch            vals->bmac_val[0] = wb_data[0];
15219255736Sdavidch            vals->bmac_val[1] = wb_data[1];
15220255736Sdavidch            wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15221255736Sdavidch            REG_WR(sc, vals->bmac_addr, wb_data[0]);
15222255736Sdavidch            REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15223255736Sdavidch        }
15224255736Sdavidch
15225255736Sdavidch        BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15226255736Sdavidch        vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15227255736Sdavidch        vals->emac_val = REG_RD(sc, vals->emac_addr);
15228255736Sdavidch        REG_WR(sc, vals->emac_addr, 0);
15229255736Sdavidch        mac_stopped = TRUE;
15230255736Sdavidch    } else {
15231255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15232255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15233255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15234255736Sdavidch            val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15235255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15236255736Sdavidch            REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15237255736Sdavidch            vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15238255736Sdavidch            vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15239255736Sdavidch            REG_WR(sc, vals->xmac_addr, 0);
15240255736Sdavidch            mac_stopped = TRUE;
15241255736Sdavidch        }
15242255736Sdavidch
15243255736Sdavidch        mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15244255736Sdavidch        if (mask & reset_reg) {
15245255736Sdavidch            BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15246255736Sdavidch            base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15247255736Sdavidch            vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15248255736Sdavidch            vals->umac_val = REG_RD(sc, vals->umac_addr);
15249255736Sdavidch            REG_WR(sc, vals->umac_addr, 0);
15250255736Sdavidch            mac_stopped = TRUE;
15251255736Sdavidch        }
15252255736Sdavidch    }
15253255736Sdavidch
15254255736Sdavidch    if (mac_stopped) {
15255255736Sdavidch        DELAY(20000);
15256255736Sdavidch    }
15257255736Sdavidch}
15258255736Sdavidch
15259255736Sdavidch#define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15260255736Sdavidch#define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15261255736Sdavidch#define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15262255736Sdavidch#define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15263255736Sdavidch
15264255736Sdavidchstatic void
15265255736Sdavidchbxe_prev_unload_undi_inc(struct bxe_softc *sc,
15266255736Sdavidch                         uint8_t          port,
15267255736Sdavidch                         uint8_t          inc)
15268255736Sdavidch{
15269255736Sdavidch    uint16_t rcq, bd;
15270255736Sdavidch    uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15271255736Sdavidch
15272255736Sdavidch    rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15273255736Sdavidch    bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15274255736Sdavidch
15275255736Sdavidch    tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15276255736Sdavidch    REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15277255736Sdavidch
15278255736Sdavidch    BLOGD(sc, DBG_LOAD,
15279255736Sdavidch          "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15280255736Sdavidch          port, bd, rcq);
15281255736Sdavidch}
15282255736Sdavidch
15283255736Sdavidchstatic int
15284255736Sdavidchbxe_prev_unload_common(struct bxe_softc *sc)
15285255736Sdavidch{
15286255736Sdavidch    uint32_t reset_reg, tmp_reg = 0, rc;
15287255736Sdavidch    uint8_t prev_undi = FALSE;
15288255736Sdavidch    struct bxe_mac_vals mac_vals;
15289255736Sdavidch    uint32_t timer_count = 1000;
15290255736Sdavidch    uint32_t prev_brb;
15291255736Sdavidch
15292255736Sdavidch    /*
15293255736Sdavidch     * It is possible a previous function received 'common' answer,
15294255736Sdavidch     * but hasn't loaded yet, therefore creating a scenario of
15295255736Sdavidch     * multiple functions receiving 'common' on the same path.
15296255736Sdavidch     */
15297255736Sdavidch    BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15298255736Sdavidch
15299255736Sdavidch    memset(&mac_vals, 0, sizeof(mac_vals));
15300255736Sdavidch
15301255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15302255736Sdavidch        return (bxe_prev_mcp_done(sc));
15303255736Sdavidch    }
15304255736Sdavidch
15305255736Sdavidch    reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15306255736Sdavidch
15307255736Sdavidch    /* Reset should be performed after BRB is emptied */
15308255736Sdavidch    if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15309255736Sdavidch        /* Close the MAC Rx to prevent BRB from filling up */
15310255736Sdavidch        bxe_prev_unload_close_mac(sc, &mac_vals);
15311255736Sdavidch
15312255736Sdavidch        /* close LLH filters towards the BRB */
15313255736Sdavidch        elink_set_rx_filter(&sc->link_params, 0);
15314255736Sdavidch
15315255736Sdavidch        /*
15316255736Sdavidch         * Check if the UNDI driver was previously loaded.
15317255736Sdavidch         * UNDI driver initializes CID offset for normal bell to 0x7
15318255736Sdavidch         */
15319255736Sdavidch        if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15320255736Sdavidch            tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15321255736Sdavidch            if (tmp_reg == 0x7) {
15322255736Sdavidch                BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15323255736Sdavidch                prev_undi = TRUE;
15324255736Sdavidch                /* clear the UNDI indication */
15325255736Sdavidch                REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15326255736Sdavidch                /* clear possible idle check errors */
15327255736Sdavidch                REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15328255736Sdavidch            }
15329255736Sdavidch        }
15330255736Sdavidch
15331255736Sdavidch        /* wait until BRB is empty */
15332255736Sdavidch        tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15333255736Sdavidch        while (timer_count) {
15334255736Sdavidch            prev_brb = tmp_reg;
15335255736Sdavidch
15336255736Sdavidch            tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15337255736Sdavidch            if (!tmp_reg) {
15338255736Sdavidch                break;
15339255736Sdavidch            }
15340255736Sdavidch
15341255736Sdavidch            BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15342255736Sdavidch
15343255736Sdavidch            /* reset timer as long as BRB actually gets emptied */
15344255736Sdavidch            if (prev_brb > tmp_reg) {
15345255736Sdavidch                timer_count = 1000;
15346255736Sdavidch            } else {
15347255736Sdavidch                timer_count--;
15348255736Sdavidch            }
15349255736Sdavidch
15350255736Sdavidch            /* If UNDI resides in memory, manually increment it */
15351255736Sdavidch            if (prev_undi) {
15352255736Sdavidch                bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15353255736Sdavidch            }
15354255736Sdavidch
15355255736Sdavidch            DELAY(10);
15356255736Sdavidch        }
15357255736Sdavidch
15358255736Sdavidch        if (!timer_count) {
15359255736Sdavidch            BLOGE(sc, "Failed to empty BRB\n");
15360255736Sdavidch        }
15361255736Sdavidch    }
15362255736Sdavidch
15363255736Sdavidch    /* No packets are in the pipeline, path is ready for reset */
15364255736Sdavidch    bxe_reset_common(sc);
15365255736Sdavidch
15366255736Sdavidch    if (mac_vals.xmac_addr) {
15367255736Sdavidch        REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15368255736Sdavidch    }
15369255736Sdavidch    if (mac_vals.umac_addr) {
15370255736Sdavidch        REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15371255736Sdavidch    }
15372255736Sdavidch    if (mac_vals.emac_addr) {
15373255736Sdavidch        REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15374255736Sdavidch    }
15375255736Sdavidch    if (mac_vals.bmac_addr) {
15376255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15377255736Sdavidch        REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15378255736Sdavidch    }
15379255736Sdavidch
15380255736Sdavidch    rc = bxe_prev_mark_path(sc, prev_undi);
15381255736Sdavidch    if (rc) {
15382255736Sdavidch        bxe_prev_mcp_done(sc);
15383255736Sdavidch        return (rc);
15384255736Sdavidch    }
15385255736Sdavidch
15386255736Sdavidch    return (bxe_prev_mcp_done(sc));
15387255736Sdavidch}
15388255736Sdavidch
15389255736Sdavidchstatic int
15390255736Sdavidchbxe_prev_unload_uncommon(struct bxe_softc *sc)
15391255736Sdavidch{
15392255736Sdavidch    int rc;
15393255736Sdavidch
15394255736Sdavidch    BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15395255736Sdavidch
15396255736Sdavidch    /* Test if previous unload process was already finished for this path */
15397255736Sdavidch    if (bxe_prev_is_path_marked(sc)) {
15398255736Sdavidch        return (bxe_prev_mcp_done(sc));
15399255736Sdavidch    }
15400255736Sdavidch
15401255736Sdavidch    BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15402255736Sdavidch
15403255736Sdavidch    /*
15404255736Sdavidch     * If function has FLR capabilities, and existing FW version matches
15405255736Sdavidch     * the one required, then FLR will be sufficient to clean any residue
15406255736Sdavidch     * left by previous driver
15407255736Sdavidch     */
15408255736Sdavidch    rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15409255736Sdavidch    if (!rc) {
15410255736Sdavidch        /* fw version is good */
15411255736Sdavidch        BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15412255736Sdavidch        rc = bxe_do_flr(sc);
15413255736Sdavidch    }
15414255736Sdavidch
15415255736Sdavidch    if (!rc) {
15416255736Sdavidch        /* FLR was performed */
15417255736Sdavidch        BLOGD(sc, DBG_LOAD, "FLR successful\n");
15418255736Sdavidch        return (0);
15419255736Sdavidch    }
15420255736Sdavidch
15421255736Sdavidch    BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15422255736Sdavidch
15423255736Sdavidch    /* Close the MCP request, return failure*/
15424255736Sdavidch    rc = bxe_prev_mcp_done(sc);
15425255736Sdavidch    if (!rc) {
15426255736Sdavidch        rc = BXE_PREV_WAIT_NEEDED;
15427255736Sdavidch    }
15428255736Sdavidch
15429255736Sdavidch    return (rc);
15430255736Sdavidch}
15431255736Sdavidch
15432255736Sdavidchstatic int
15433255736Sdavidchbxe_prev_unload(struct bxe_softc *sc)
15434255736Sdavidch{
15435255736Sdavidch    int time_counter = 10;
15436255736Sdavidch    uint32_t fw, hw_lock_reg, hw_lock_val;
15437255736Sdavidch    uint32_t rc = 0;
15438255736Sdavidch
15439255736Sdavidch    /*
15440255736Sdavidch     * Clear HW from errors which may have resulted from an interrupted
15441255736Sdavidch     * DMAE transaction.
15442255736Sdavidch     */
15443255736Sdavidch    bxe_prev_interrupted_dmae(sc);
15444255736Sdavidch
15445255736Sdavidch    /* Release previously held locks */
15446255736Sdavidch    hw_lock_reg =
15447255736Sdavidch        (SC_FUNC(sc) <= 5) ?
15448255736Sdavidch            (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15449255736Sdavidch            (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15450255736Sdavidch
15451255736Sdavidch    hw_lock_val = (REG_RD(sc, hw_lock_reg));
15452255736Sdavidch    if (hw_lock_val) {
15453255736Sdavidch        if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15454255736Sdavidch            BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15455255736Sdavidch            REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15456255736Sdavidch                   (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15457255736Sdavidch        }
15458255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15459255736Sdavidch        REG_WR(sc, hw_lock_reg, 0xffffffff);
15460255736Sdavidch    } else {
15461255736Sdavidch        BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15462255736Sdavidch    }
15463255736Sdavidch
15464255736Sdavidch    if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15465255736Sdavidch        BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15466255736Sdavidch        REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15467255736Sdavidch    }
15468255736Sdavidch
15469255736Sdavidch    do {
15470255736Sdavidch        /* Lock MCP using an unload request */
15471255736Sdavidch        fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15472255736Sdavidch        if (!fw) {
15473255736Sdavidch            BLOGE(sc, "MCP response failure, aborting\n");
15474255736Sdavidch            rc = -1;
15475255736Sdavidch            break;
15476255736Sdavidch        }
15477255736Sdavidch
15478255736Sdavidch        if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15479255736Sdavidch            rc = bxe_prev_unload_common(sc);
15480255736Sdavidch            break;
15481255736Sdavidch        }
15482255736Sdavidch
15483255736Sdavidch        /* non-common reply from MCP night require looping */
15484255736Sdavidch        rc = bxe_prev_unload_uncommon(sc);
15485255736Sdavidch        if (rc != BXE_PREV_WAIT_NEEDED) {
15486255736Sdavidch            break;
15487255736Sdavidch        }
15488255736Sdavidch
15489255736Sdavidch        DELAY(20000);
15490255736Sdavidch    } while (--time_counter);
15491255736Sdavidch
15492255736Sdavidch    if (!time_counter || rc) {
15493295830Sdavidcs        BLOGE(sc, "Failed to unload previous driver!"
15494295830Sdavidcs            " time_counter %d rc %d\n", time_counter, rc);
15495255736Sdavidch        rc = -1;
15496255736Sdavidch    }
15497255736Sdavidch
15498255736Sdavidch    return (rc);
15499255736Sdavidch}
15500255736Sdavidch
15501255736Sdavidchvoid
15502255736Sdavidchbxe_dcbx_set_state(struct bxe_softc *sc,
15503255736Sdavidch                   uint8_t          dcb_on,
15504255736Sdavidch                   uint32_t         dcbx_enabled)
15505255736Sdavidch{
15506255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
15507255736Sdavidch        sc->dcb_state = dcb_on;
15508255736Sdavidch        sc->dcbx_enabled = dcbx_enabled;
15509255736Sdavidch    } else {
15510255736Sdavidch        sc->dcb_state = FALSE;
15511255736Sdavidch        sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15512255736Sdavidch    }
15513255736Sdavidch    BLOGD(sc, DBG_LOAD,
15514255736Sdavidch          "DCB state [%s:%s]\n",
15515255736Sdavidch          dcb_on ? "ON" : "OFF",
15516255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15517255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15518255736Sdavidch          (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15519255736Sdavidch          "on-chip with negotiation" : "invalid");
15520255736Sdavidch}
15521255736Sdavidch
15522255736Sdavidch/* must be called after sriov-enable */
15523255736Sdavidchstatic int
15524255736Sdavidchbxe_set_qm_cid_count(struct bxe_softc *sc)
15525255736Sdavidch{
15526255736Sdavidch    int cid_count = BXE_L2_MAX_CID(sc);
15527255736Sdavidch
15528255736Sdavidch    if (IS_SRIOV(sc)) {
15529255736Sdavidch        cid_count += BXE_VF_CIDS;
15530255736Sdavidch    }
15531255736Sdavidch
15532255736Sdavidch    if (CNIC_SUPPORT(sc)) {
15533255736Sdavidch        cid_count += CNIC_CID_MAX;
15534255736Sdavidch    }
15535255736Sdavidch
15536255736Sdavidch    return (roundup(cid_count, QM_CID_ROUND));
15537255736Sdavidch}
15538255736Sdavidch
15539255736Sdavidchstatic void
15540255736Sdavidchbxe_init_multi_cos(struct bxe_softc *sc)
15541255736Sdavidch{
15542255736Sdavidch    int pri, cos;
15543255736Sdavidch
15544255736Sdavidch    uint32_t pri_map = 0; /* XXX change to user config */
15545255736Sdavidch
15546255736Sdavidch    for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15547255736Sdavidch        cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15548255736Sdavidch        if (cos < sc->max_cos) {
15549255736Sdavidch            sc->prio_to_cos[pri] = cos;
15550255736Sdavidch        } else {
15551255736Sdavidch            BLOGW(sc, "Invalid COS %d for priority %d "
15552255736Sdavidch                      "(max COS is %d), setting to 0\n",
15553255736Sdavidch                  cos, pri, (sc->max_cos - 1));
15554255736Sdavidch            sc->prio_to_cos[pri] = 0;
15555255736Sdavidch        }
15556255736Sdavidch    }
15557255736Sdavidch}
15558255736Sdavidch
15559255736Sdavidchstatic int
15560255736Sdavidchbxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15561255736Sdavidch{
15562255736Sdavidch    struct bxe_softc *sc;
15563255736Sdavidch    int error, result;
15564255736Sdavidch
15565255736Sdavidch    result = 0;
15566255736Sdavidch    error = sysctl_handle_int(oidp, &result, 0, req);
15567255736Sdavidch
15568255736Sdavidch    if (error || !req->newptr) {
15569255736Sdavidch        return (error);
15570255736Sdavidch    }
15571255736Sdavidch
15572255736Sdavidch    if (result == 1) {
15573292638Sdavidcs        uint32_t  temp;
15574255736Sdavidch        sc = (struct bxe_softc *)arg1;
15575289199Sdavidcs
15576255736Sdavidch        BLOGI(sc, "... dumping driver state ...\n");
15577292638Sdavidcs        temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15578292638Sdavidcs        BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15579255736Sdavidch    }
15580255736Sdavidch
15581255736Sdavidch    return (error);
15582255736Sdavidch}
15583255736Sdavidch
15584255736Sdavidchstatic int
15585295823Sdavidcsbxe_sysctl_trigger_grcdump(SYSCTL_HANDLER_ARGS)
15586295823Sdavidcs{
15587295823Sdavidcs    struct bxe_softc *sc;
15588295823Sdavidcs    int error, result;
15589295823Sdavidcs
15590295823Sdavidcs    result = 0;
15591295823Sdavidcs    error = sysctl_handle_int(oidp, &result, 0, req);
15592295823Sdavidcs
15593295823Sdavidcs    if (error || !req->newptr) {
15594295823Sdavidcs        return (error);
15595295823Sdavidcs    }
15596295823Sdavidcs
15597295823Sdavidcs    if (result == 1) {
15598295823Sdavidcs        sc = (struct bxe_softc *)arg1;
15599295823Sdavidcs
15600295823Sdavidcs        BLOGI(sc, "... grcdump start ...\n");
15601295823Sdavidcs        bxe_grc_dump(sc);
15602295823Sdavidcs        BLOGI(sc, "... grcdump done ...\n");
15603295823Sdavidcs    }
15604295823Sdavidcs
15605295823Sdavidcs    return (error);
15606295823Sdavidcs}
15607295823Sdavidcs
15608295823Sdavidcsstatic int
15609255736Sdavidchbxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15610255736Sdavidch{
15611255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15612255736Sdavidch    uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15613255736Sdavidch    uint32_t *offset;
15614255736Sdavidch    uint64_t value = 0;
15615255736Sdavidch    int index = (int)arg2;
15616255736Sdavidch
15617255736Sdavidch    if (index >= BXE_NUM_ETH_STATS) {
15618255736Sdavidch        BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15619255736Sdavidch        return (-1);
15620255736Sdavidch    }
15621255736Sdavidch
15622255736Sdavidch    offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15623255736Sdavidch
15624255736Sdavidch    switch (bxe_eth_stats_arr[index].size) {
15625255736Sdavidch    case 4:
15626255736Sdavidch        value = (uint64_t)*offset;
15627255736Sdavidch        break;
15628255736Sdavidch    case 8:
15629255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15630255736Sdavidch        break;
15631255736Sdavidch    default:
15632255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15633255736Sdavidch              index, bxe_eth_stats_arr[index].size);
15634255736Sdavidch        return (-1);
15635255736Sdavidch    }
15636255736Sdavidch
15637255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15638255736Sdavidch}
15639255736Sdavidch
15640255736Sdavidchstatic int
15641255736Sdavidchbxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15642255736Sdavidch{
15643255736Sdavidch    struct bxe_softc *sc = (struct bxe_softc *)arg1;
15644255736Sdavidch    uint32_t *eth_stats;
15645255736Sdavidch    uint32_t *offset;
15646255736Sdavidch    uint64_t value = 0;
15647255736Sdavidch    uint32_t q_stat = (uint32_t)arg2;
15648255736Sdavidch    uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15649255736Sdavidch    uint32_t index = (q_stat & 0xffff);
15650255736Sdavidch
15651255736Sdavidch    eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15652255736Sdavidch
15653255736Sdavidch    if (index >= BXE_NUM_ETH_Q_STATS) {
15654255736Sdavidch        BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15655255736Sdavidch        return (-1);
15656255736Sdavidch    }
15657255736Sdavidch
15658255736Sdavidch    offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15659255736Sdavidch
15660255736Sdavidch    switch (bxe_eth_q_stats_arr[index].size) {
15661255736Sdavidch    case 4:
15662255736Sdavidch        value = (uint64_t)*offset;
15663255736Sdavidch        break;
15664255736Sdavidch    case 8:
15665255736Sdavidch        value = HILO_U64(*offset, *(offset + 1));
15666255736Sdavidch        break;
15667255736Sdavidch    default:
15668255736Sdavidch        BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15669255736Sdavidch              index, bxe_eth_q_stats_arr[index].size);
15670255736Sdavidch        return (-1);
15671255736Sdavidch    }
15672255736Sdavidch
15673255736Sdavidch    return (sysctl_handle_64(oidp, &value, 0, req));
15674255736Sdavidch}
15675255736Sdavidch
15676255736Sdavidchstatic void
15677255736Sdavidchbxe_add_sysctls(struct bxe_softc *sc)
15678255736Sdavidch{
15679255736Sdavidch    struct sysctl_ctx_list *ctx;
15680255736Sdavidch    struct sysctl_oid_list *children;
15681255736Sdavidch    struct sysctl_oid *queue_top, *queue;
15682255736Sdavidch    struct sysctl_oid_list *queue_top_children, *queue_children;
15683255736Sdavidch    char queue_num_buf[32];
15684255736Sdavidch    uint32_t q_stat;
15685255736Sdavidch    int i, j;
15686255736Sdavidch
15687255736Sdavidch    ctx = device_get_sysctl_ctx(sc->dev);
15688255736Sdavidch    children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15689255736Sdavidch
15690255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
15691255736Sdavidch                      CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
15692255736Sdavidch                      "version");
15693255736Sdavidch
15694255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15695273377Shselasky                      CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
15696255736Sdavidch                      "bootcode version");
15697255736Sdavidch
15698255736Sdavidch    snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
15699255736Sdavidch             BCM_5710_FW_MAJOR_VERSION,
15700255736Sdavidch             BCM_5710_FW_MINOR_VERSION,
15701255736Sdavidch             BCM_5710_FW_REVISION_VERSION,
15702255736Sdavidch             BCM_5710_FW_ENGINEERING_VERSION);
15703255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15704273377Shselasky                      CTLFLAG_RD, sc->fw_ver_str, 0,
15705255736Sdavidch                      "firmware version");
15706255736Sdavidch
15707255736Sdavidch    snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
15708255736Sdavidch        ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
15709255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
15710255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
15711255736Sdavidch         (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
15712255736Sdavidch                                                                "Unknown"));
15713255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15714273377Shselasky                      CTLFLAG_RD, sc->mf_mode_str, 0,
15715255736Sdavidch                      "multifunction mode");
15716255736Sdavidch
15717255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
15718255736Sdavidch                    CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
15719255736Sdavidch                    "multifunction vnics per port");
15720255736Sdavidch
15721255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15722273377Shselasky                      CTLFLAG_RD, sc->mac_addr_str, 0,
15723255736Sdavidch                      "mac address");
15724255736Sdavidch
15725255736Sdavidch    snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
15726255736Sdavidch        ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
15727255736Sdavidch         (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
15728255736Sdavidch         (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
15729255736Sdavidch                                              "???GT/s"),
15730255736Sdavidch        sc->devinfo.pcie_link_width);
15731255736Sdavidch    SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15732273377Shselasky                      CTLFLAG_RD, sc->pci_link_str, 0,
15733255736Sdavidch                      "pci link status");
15734255736Sdavidch
15735255736Sdavidch    sc->debug = bxe_debug;
15736273377Shselasky    SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
15737273377Shselasky                    CTLFLAG_RW, &sc->debug,
15738255736Sdavidch                    "debug logging mode");
15739255736Sdavidch
15740295823Sdavidcs    SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "trigger_grcdump",
15741295823Sdavidcs                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15742295823Sdavidcs                    bxe_sysctl_trigger_grcdump, "IU",
15743292639Sdavidcs                    "set by driver when a grcdump is needed");
15744292639Sdavidcs
15745295823Sdavidcs    sc->grcdump_done = 0;
15746295823Sdavidcs    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
15747295823Sdavidcs                   CTLFLAG_RW, &sc->grcdump_done, 0,
15748295823Sdavidcs                   "set by driver when grcdump is done");
15749292639Sdavidcs
15750255736Sdavidch    sc->rx_budget = bxe_rx_budget;
15751255736Sdavidch    SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
15752255736Sdavidch                    CTLFLAG_RW, &sc->rx_budget, 0,
15753255736Sdavidch                    "rx processing budget");
15754255736Sdavidch
15755255736Sdavidch    SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
15756255736Sdavidch                    CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15757255736Sdavidch                    bxe_sysctl_state, "IU", "dump driver state");
15758255736Sdavidch
15759255736Sdavidch    for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
15760255736Sdavidch        SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
15761255736Sdavidch                        bxe_eth_stats_arr[i].string,
15762255736Sdavidch                        CTLTYPE_U64 | CTLFLAG_RD, sc, i,
15763255736Sdavidch                        bxe_sysctl_eth_stat, "LU",
15764255736Sdavidch                        bxe_eth_stats_arr[i].string);
15765255736Sdavidch    }
15766255736Sdavidch
15767255736Sdavidch    /* add a new parent node for all queues "dev.bxe.#.queue" */
15768255736Sdavidch    queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
15769255736Sdavidch                                CTLFLAG_RD, NULL, "queue");
15770255736Sdavidch    queue_top_children = SYSCTL_CHILDREN(queue_top);
15771255736Sdavidch
15772255736Sdavidch    for (i = 0; i < sc->num_queues; i++) {
15773255736Sdavidch        /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
15774255736Sdavidch        snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
15775255736Sdavidch        queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
15776255736Sdavidch                                queue_num_buf, CTLFLAG_RD, NULL,
15777255736Sdavidch                                "single queue");
15778255736Sdavidch        queue_children = SYSCTL_CHILDREN(queue);
15779255736Sdavidch
15780255736Sdavidch        for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
15781255736Sdavidch            q_stat = ((i << 16) | j);
15782255736Sdavidch            SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
15783255736Sdavidch                            bxe_eth_q_stats_arr[j].string,
15784255736Sdavidch                            CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
15785255736Sdavidch                            bxe_sysctl_eth_q_stat, "LU",
15786255736Sdavidch                            bxe_eth_q_stats_arr[j].string);
15787255736Sdavidch        }
15788255736Sdavidch    }
15789255736Sdavidch}
15790255736Sdavidch
15791255736Sdavidch/*
15792255736Sdavidch * Device attach function.
15793255736Sdavidch *
15794255736Sdavidch * Allocates device resources, performs secondary chip identification, and
15795255736Sdavidch * initializes driver instance variables. This function is called from driver
15796255736Sdavidch * load after a successful probe.
15797255736Sdavidch *
15798255736Sdavidch * Returns:
15799255736Sdavidch *   0 = Success, >0 = Failure
15800255736Sdavidch */
15801255736Sdavidchstatic int
15802255736Sdavidchbxe_attach(device_t dev)
15803255736Sdavidch{
15804255736Sdavidch    struct bxe_softc *sc;
15805255736Sdavidch
15806255736Sdavidch    sc = device_get_softc(dev);
15807255736Sdavidch
15808255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting attach...\n");
15809255736Sdavidch
15810255736Sdavidch    sc->state = BXE_STATE_CLOSED;
15811255736Sdavidch
15812255736Sdavidch    sc->dev  = dev;
15813255736Sdavidch    sc->unit = device_get_unit(dev);
15814255736Sdavidch
15815255736Sdavidch    BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
15816255736Sdavidch
15817255736Sdavidch    sc->pcie_bus    = pci_get_bus(dev);
15818255736Sdavidch    sc->pcie_device = pci_get_slot(dev);
15819255736Sdavidch    sc->pcie_func   = pci_get_function(dev);
15820255736Sdavidch
15821255736Sdavidch    /* enable bus master capability */
15822255736Sdavidch    pci_enable_busmaster(dev);
15823255736Sdavidch
15824255736Sdavidch    /* get the BARs */
15825255736Sdavidch    if (bxe_allocate_bars(sc) != 0) {
15826255736Sdavidch        return (ENXIO);
15827255736Sdavidch    }
15828255736Sdavidch
15829255736Sdavidch    /* initialize the mutexes */
15830255736Sdavidch    bxe_init_mutexes(sc);
15831255736Sdavidch
15832255736Sdavidch    /* prepare the periodic callout */
15833255736Sdavidch    callout_init(&sc->periodic_callout, 0);
15834255736Sdavidch
15835255736Sdavidch    /* prepare the chip taskqueue */
15836255736Sdavidch    sc->chip_tq_flags = CHIP_TQ_NONE;
15837255736Sdavidch    snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
15838255736Sdavidch             "bxe%d_chip_tq", sc->unit);
15839255736Sdavidch    TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
15840255736Sdavidch    sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
15841255736Sdavidch                                   taskqueue_thread_enqueue,
15842255736Sdavidch                                   &sc->chip_tq);
15843255736Sdavidch    taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
15844255736Sdavidch                            "%s", sc->chip_tq_name);
15845255736Sdavidch
15846255736Sdavidch    /* get device info and set params */
15847255736Sdavidch    if (bxe_get_device_info(sc) != 0) {
15848255736Sdavidch        BLOGE(sc, "getting device info\n");
15849255736Sdavidch        bxe_deallocate_bars(sc);
15850255736Sdavidch        pci_disable_busmaster(dev);
15851255736Sdavidch        return (ENXIO);
15852255736Sdavidch    }
15853255736Sdavidch
15854255736Sdavidch    /* get final misc params */
15855255736Sdavidch    bxe_get_params(sc);
15856255736Sdavidch
15857255736Sdavidch    /* set the default MTU (changed via ifconfig) */
15858255736Sdavidch    sc->mtu = ETHERMTU;
15859255736Sdavidch
15860255736Sdavidch    bxe_set_modes_bitmap(sc);
15861255736Sdavidch
15862255736Sdavidch    /* XXX
15863255736Sdavidch     * If in AFEX mode and the function is configured for FCoE
15864255736Sdavidch     * then bail... no L2 allowed.
15865255736Sdavidch     */
15866255736Sdavidch
15867255736Sdavidch    /* get phy settings from shmem and 'and' against admin settings */
15868255736Sdavidch    bxe_get_phy_info(sc);
15869255736Sdavidch
15870255736Sdavidch    /* initialize the FreeBSD ifnet interface */
15871255736Sdavidch    if (bxe_init_ifnet(sc) != 0) {
15872255736Sdavidch        bxe_release_mutexes(sc);
15873255736Sdavidch        bxe_deallocate_bars(sc);
15874255736Sdavidch        pci_disable_busmaster(dev);
15875255736Sdavidch        return (ENXIO);
15876255736Sdavidch    }
15877255736Sdavidch
15878292639Sdavidcs    if (bxe_add_cdev(sc) != 0) {
15879292639Sdavidcs        if (sc->ifp != NULL) {
15880292639Sdavidcs            ether_ifdetach(sc->ifp);
15881292639Sdavidcs        }
15882292639Sdavidcs        ifmedia_removeall(&sc->ifmedia);
15883292639Sdavidcs        bxe_release_mutexes(sc);
15884292639Sdavidcs        bxe_deallocate_bars(sc);
15885292639Sdavidcs        pci_disable_busmaster(dev);
15886292639Sdavidcs        return (ENXIO);
15887292639Sdavidcs    }
15888292639Sdavidcs
15889255736Sdavidch    /* allocate device interrupts */
15890255736Sdavidch    if (bxe_interrupt_alloc(sc) != 0) {
15891292639Sdavidcs        bxe_del_cdev(sc);
15892266979Smarcel        if (sc->ifp != NULL) {
15893270876Sglebius            ether_ifdetach(sc->ifp);
15894255736Sdavidch        }
15895255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
15896255736Sdavidch        bxe_release_mutexes(sc);
15897255736Sdavidch        bxe_deallocate_bars(sc);
15898255736Sdavidch        pci_disable_busmaster(dev);
15899255736Sdavidch        return (ENXIO);
15900255736Sdavidch    }
15901255736Sdavidch
15902255736Sdavidch    /* allocate ilt */
15903255736Sdavidch    if (bxe_alloc_ilt_mem(sc) != 0) {
15904255736Sdavidch        bxe_interrupt_free(sc);
15905292639Sdavidcs        bxe_del_cdev(sc);
15906266979Smarcel        if (sc->ifp != NULL) {
15907270876Sglebius            ether_ifdetach(sc->ifp);
15908255736Sdavidch        }
15909255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
15910255736Sdavidch        bxe_release_mutexes(sc);
15911255736Sdavidch        bxe_deallocate_bars(sc);
15912255736Sdavidch        pci_disable_busmaster(dev);
15913255736Sdavidch        return (ENXIO);
15914255736Sdavidch    }
15915255736Sdavidch
15916255736Sdavidch    /* allocate the host hardware/software hsi structures */
15917255736Sdavidch    if (bxe_alloc_hsi_mem(sc) != 0) {
15918255736Sdavidch        bxe_free_ilt_mem(sc);
15919255736Sdavidch        bxe_interrupt_free(sc);
15920292639Sdavidcs        bxe_del_cdev(sc);
15921266979Smarcel        if (sc->ifp != NULL) {
15922270876Sglebius            ether_ifdetach(sc->ifp);
15923255736Sdavidch        }
15924255736Sdavidch        ifmedia_removeall(&sc->ifmedia);
15925255736Sdavidch        bxe_release_mutexes(sc);
15926255736Sdavidch        bxe_deallocate_bars(sc);
15927255736Sdavidch        pci_disable_busmaster(dev);
15928255736Sdavidch        return (ENXIO);
15929255736Sdavidch    }
15930255736Sdavidch
15931255736Sdavidch    /* need to reset chip if UNDI was active */
15932255736Sdavidch    if (IS_PF(sc) && !BXE_NOMCP(sc)) {
15933255736Sdavidch        /* init fw_seq */
15934255736Sdavidch        sc->fw_seq =
15935255736Sdavidch            (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
15936255736Sdavidch             DRV_MSG_SEQ_NUMBER_MASK);
15937255736Sdavidch        BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
15938255736Sdavidch        bxe_prev_unload(sc);
15939255736Sdavidch    }
15940255736Sdavidch
15941255736Sdavidch#if 1
15942255736Sdavidch    /* XXX */
15943255736Sdavidch    bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
15944255736Sdavidch#else
15945255736Sdavidch    if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
15946255736Sdavidch        SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
15947255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
15948255736Sdavidch        SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
15949255736Sdavidch        bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
15950255736Sdavidch        bxe_dcbx_init_params(sc);
15951255736Sdavidch    } else {
15952255736Sdavidch        bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
15953255736Sdavidch    }
15954255736Sdavidch#endif
15955255736Sdavidch
15956255736Sdavidch    /* calculate qm_cid_count */
15957255736Sdavidch    sc->qm_cid_count = bxe_set_qm_cid_count(sc);
15958255736Sdavidch    BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
15959255736Sdavidch
15960255736Sdavidch    sc->max_cos = 1;
15961255736Sdavidch    bxe_init_multi_cos(sc);
15962255736Sdavidch
15963255736Sdavidch    bxe_add_sysctls(sc);
15964255736Sdavidch
15965255736Sdavidch    return (0);
15966255736Sdavidch}
15967255736Sdavidch
15968255736Sdavidch/*
15969255736Sdavidch * Device detach function.
15970255736Sdavidch *
15971255736Sdavidch * Stops the controller, resets the controller, and releases resources.
15972255736Sdavidch *
15973255736Sdavidch * Returns:
15974255736Sdavidch *   0 = Success, >0 = Failure
15975255736Sdavidch */
15976255736Sdavidchstatic int
15977255736Sdavidchbxe_detach(device_t dev)
15978255736Sdavidch{
15979255736Sdavidch    struct bxe_softc *sc;
15980266979Smarcel    if_t ifp;
15981255736Sdavidch
15982255736Sdavidch    sc = device_get_softc(dev);
15983255736Sdavidch
15984255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting detach...\n");
15985255736Sdavidch
15986266979Smarcel    ifp = sc->ifp;
15987266979Smarcel    if (ifp != NULL && if_vlantrunkinuse(ifp)) {
15988255736Sdavidch        BLOGE(sc, "Cannot detach while VLANs are in use.\n");
15989255736Sdavidch        return(EBUSY);
15990255736Sdavidch    }
15991255736Sdavidch
15992292639Sdavidcs    bxe_del_cdev(sc);
15993292639Sdavidcs
15994255736Sdavidch    /* stop the periodic callout */
15995255736Sdavidch    bxe_periodic_stop(sc);
15996255736Sdavidch
15997255736Sdavidch    /* stop the chip taskqueue */
15998255736Sdavidch    atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
15999255736Sdavidch    if (sc->chip_tq) {
16000255736Sdavidch        taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16001255736Sdavidch        taskqueue_free(sc->chip_tq);
16002255736Sdavidch        sc->chip_tq = NULL;
16003255736Sdavidch    }
16004255736Sdavidch
16005255736Sdavidch    /* stop and reset the controller if it was open */
16006255736Sdavidch    if (sc->state != BXE_STATE_CLOSED) {
16007255736Sdavidch        BXE_CORE_LOCK(sc);
16008255736Sdavidch        bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16009255736Sdavidch        BXE_CORE_UNLOCK(sc);
16010255736Sdavidch    }
16011255736Sdavidch
16012255736Sdavidch    /* release the network interface */
16013255736Sdavidch    if (ifp != NULL) {
16014270876Sglebius        ether_ifdetach(ifp);
16015255736Sdavidch    }
16016255736Sdavidch    ifmedia_removeall(&sc->ifmedia);
16017255736Sdavidch
16018255736Sdavidch    /* XXX do the following based on driver state... */
16019255736Sdavidch
16020255736Sdavidch    /* free the host hardware/software hsi structures */
16021255736Sdavidch    bxe_free_hsi_mem(sc);
16022255736Sdavidch
16023255736Sdavidch    /* free ilt */
16024255736Sdavidch    bxe_free_ilt_mem(sc);
16025255736Sdavidch
16026255736Sdavidch    /* release the interrupts */
16027255736Sdavidch    bxe_interrupt_free(sc);
16028255736Sdavidch
16029255736Sdavidch    /* Release the mutexes*/
16030255736Sdavidch    bxe_release_mutexes(sc);
16031255736Sdavidch
16032255736Sdavidch    /* Release the PCIe BAR mapped memory */
16033255736Sdavidch    bxe_deallocate_bars(sc);
16034255736Sdavidch
16035255736Sdavidch    /* Release the FreeBSD interface. */
16036266979Smarcel    if (sc->ifp != NULL) {
16037270876Sglebius        if_free(sc->ifp);
16038255736Sdavidch    }
16039255736Sdavidch
16040255736Sdavidch    pci_disable_busmaster(dev);
16041255736Sdavidch
16042255736Sdavidch    return (0);
16043255736Sdavidch}
16044255736Sdavidch
16045255736Sdavidch/*
16046255736Sdavidch * Device shutdown function.
16047255736Sdavidch *
16048255736Sdavidch * Stops and resets the controller.
16049255736Sdavidch *
16050255736Sdavidch * Returns:
16051255736Sdavidch *   Nothing
16052255736Sdavidch */
16053255736Sdavidchstatic int
16054255736Sdavidchbxe_shutdown(device_t dev)
16055255736Sdavidch{
16056255736Sdavidch    struct bxe_softc *sc;
16057255736Sdavidch
16058255736Sdavidch    sc = device_get_softc(dev);
16059255736Sdavidch
16060255736Sdavidch    BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16061255736Sdavidch
16062255736Sdavidch    /* stop the periodic callout */
16063255736Sdavidch    bxe_periodic_stop(sc);
16064255736Sdavidch
16065255736Sdavidch    BXE_CORE_LOCK(sc);
16066255736Sdavidch    bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16067255736Sdavidch    BXE_CORE_UNLOCK(sc);
16068255736Sdavidch
16069255736Sdavidch    return (0);
16070255736Sdavidch}
16071255736Sdavidch
16072255736Sdavidchvoid
16073255736Sdavidchbxe_igu_ack_sb(struct bxe_softc *sc,
16074255736Sdavidch               uint8_t          igu_sb_id,
16075255736Sdavidch               uint8_t          segment,
16076255736Sdavidch               uint16_t         index,
16077255736Sdavidch               uint8_t          op,
16078255736Sdavidch               uint8_t          update)
16079255736Sdavidch{
16080255736Sdavidch    uint32_t igu_addr = sc->igu_base_addr;
16081255736Sdavidch    igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16082255736Sdavidch    bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16083255736Sdavidch}
16084255736Sdavidch
16085255736Sdavidchstatic void
16086255736Sdavidchbxe_igu_clear_sb_gen(struct bxe_softc *sc,
16087255736Sdavidch                     uint8_t          func,
16088255736Sdavidch                     uint8_t          idu_sb_id,
16089255736Sdavidch                     uint8_t          is_pf)
16090255736Sdavidch{
16091255736Sdavidch    uint32_t data, ctl, cnt = 100;
16092255736Sdavidch    uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16093255736Sdavidch    uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16094255736Sdavidch    uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16095255736Sdavidch    uint32_t sb_bit =  1 << (idu_sb_id%32);
16096255736Sdavidch    uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16097255736Sdavidch    uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16098255736Sdavidch
16099255736Sdavidch    /* Not supported in BC mode */
16100255736Sdavidch    if (CHIP_INT_MODE_IS_BC(sc)) {
16101255736Sdavidch        return;
16102255736Sdavidch    }
16103255736Sdavidch
16104255736Sdavidch    data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16105255736Sdavidch             IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16106255736Sdavidch            IGU_REGULAR_CLEANUP_SET |
16107255736Sdavidch            IGU_REGULAR_BCLEANUP);
16108255736Sdavidch
16109255736Sdavidch    ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16110255736Sdavidch           (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16111255736Sdavidch           (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16112255736Sdavidch
16113255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16114255736Sdavidch            data, igu_addr_data);
16115255736Sdavidch    REG_WR(sc, igu_addr_data, data);
16116255736Sdavidch
16117255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16118255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16119255736Sdavidch    mb();
16120255736Sdavidch
16121255736Sdavidch    BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16122255736Sdavidch            ctl, igu_addr_ctl);
16123255736Sdavidch    REG_WR(sc, igu_addr_ctl, ctl);
16124255736Sdavidch
16125255736Sdavidch    bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16126255736Sdavidch                      BUS_SPACE_BARRIER_WRITE);
16127255736Sdavidch    mb();
16128255736Sdavidch
16129255736Sdavidch    /* wait for clean up to finish */
16130255736Sdavidch    while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16131255736Sdavidch        DELAY(20000);
16132255736Sdavidch    }
16133255736Sdavidch
16134255736Sdavidch    if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16135255736Sdavidch        BLOGD(sc, DBG_LOAD,
16136255736Sdavidch              "Unable to finish IGU cleanup: "
16137255736Sdavidch              "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16138255736Sdavidch              idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16139255736Sdavidch    }
16140255736Sdavidch}
16141255736Sdavidch
16142255736Sdavidchstatic void
16143255736Sdavidchbxe_igu_clear_sb(struct bxe_softc *sc,
16144255736Sdavidch                 uint8_t          idu_sb_id)
16145255736Sdavidch{
16146255736Sdavidch    bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16147255736Sdavidch}
16148255736Sdavidch
16149255736Sdavidch
16150255736Sdavidch
16151255736Sdavidch
16152255736Sdavidch
16153255736Sdavidch
16154255736Sdavidch
16155255736Sdavidch/*******************/
16156255736Sdavidch/* ECORE CALLBACKS */
16157255736Sdavidch/*******************/
16158255736Sdavidch
16159255736Sdavidchstatic void
16160255736Sdavidchbxe_reset_common(struct bxe_softc *sc)
16161255736Sdavidch{
16162255736Sdavidch    uint32_t val = 0x1400;
16163255736Sdavidch
16164255736Sdavidch    /* reset_common */
16165255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16166255736Sdavidch
16167255736Sdavidch    if (CHIP_IS_E3(sc)) {
16168255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16169255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16170255736Sdavidch    }
16171255736Sdavidch
16172255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16173255736Sdavidch}
16174255736Sdavidch
16175255736Sdavidchstatic void
16176255736Sdavidchbxe_common_init_phy(struct bxe_softc *sc)
16177255736Sdavidch{
16178255736Sdavidch    uint32_t shmem_base[2];
16179255736Sdavidch    uint32_t shmem2_base[2];
16180255736Sdavidch
16181255736Sdavidch    /* Avoid common init in case MFW supports LFA */
16182255736Sdavidch    if (SHMEM2_RD(sc, size) >
16183255736Sdavidch        (uint32_t)offsetof(struct shmem2_region,
16184255736Sdavidch                           lfa_host_addr[SC_PORT(sc)])) {
16185255736Sdavidch        return;
16186255736Sdavidch    }
16187255736Sdavidch
16188255736Sdavidch    shmem_base[0]  = sc->devinfo.shmem_base;
16189255736Sdavidch    shmem2_base[0] = sc->devinfo.shmem2_base;
16190255736Sdavidch
16191255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16192255736Sdavidch        shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16193255736Sdavidch        shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16194255736Sdavidch    }
16195255736Sdavidch
16196284335Sdavidcs    bxe_acquire_phy_lock(sc);
16197255736Sdavidch    elink_common_init_phy(sc, shmem_base, shmem2_base,
16198255736Sdavidch                          sc->devinfo.chip_id, 0);
16199284335Sdavidcs    bxe_release_phy_lock(sc);
16200255736Sdavidch}
16201255736Sdavidch
16202255736Sdavidchstatic void
16203255736Sdavidchbxe_pf_disable(struct bxe_softc *sc)
16204255736Sdavidch{
16205255736Sdavidch    uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16206255736Sdavidch
16207255736Sdavidch    val &= ~IGU_PF_CONF_FUNC_EN;
16208255736Sdavidch
16209255736Sdavidch    REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16210255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16211255736Sdavidch    REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16212255736Sdavidch}
16213255736Sdavidch
16214255736Sdavidchstatic void
16215255736Sdavidchbxe_init_pxp(struct bxe_softc *sc)
16216255736Sdavidch{
16217255736Sdavidch    uint16_t devctl;
16218255736Sdavidch    int r_order, w_order;
16219255736Sdavidch
16220255736Sdavidch    devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16221255736Sdavidch
16222255736Sdavidch    BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16223255736Sdavidch
16224255736Sdavidch    w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16225255736Sdavidch
16226255736Sdavidch    if (sc->mrrs == -1) {
16227255736Sdavidch        r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16228255736Sdavidch    } else {
16229255736Sdavidch        BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16230255736Sdavidch        r_order = sc->mrrs;
16231255736Sdavidch    }
16232255736Sdavidch
16233255736Sdavidch    ecore_init_pxp_arb(sc, r_order, w_order);
16234255736Sdavidch}
16235255736Sdavidch
16236255736Sdavidchstatic uint32_t
16237255736Sdavidchbxe_get_pretend_reg(struct bxe_softc *sc)
16238255736Sdavidch{
16239255736Sdavidch    uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16240255736Sdavidch    uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16241255736Sdavidch    return (base + (SC_ABS_FUNC(sc)) * stride);
16242255736Sdavidch}
16243255736Sdavidch
16244255736Sdavidch/*
16245255736Sdavidch * Called only on E1H or E2.
16246255736Sdavidch * When pretending to be PF, the pretend value is the function number 0..7.
16247255736Sdavidch * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16248255736Sdavidch * combination.
16249255736Sdavidch */
16250255736Sdavidchstatic int
16251255736Sdavidchbxe_pretend_func(struct bxe_softc *sc,
16252255736Sdavidch                 uint16_t         pretend_func_val)
16253255736Sdavidch{
16254255736Sdavidch    uint32_t pretend_reg;
16255255736Sdavidch
16256255736Sdavidch    if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16257255736Sdavidch        return (-1);
16258255736Sdavidch    }
16259255736Sdavidch
16260255736Sdavidch    /* get my own pretend register */
16261255736Sdavidch    pretend_reg = bxe_get_pretend_reg(sc);
16262255736Sdavidch    REG_WR(sc, pretend_reg, pretend_func_val);
16263255736Sdavidch    REG_RD(sc, pretend_reg);
16264255736Sdavidch    return (0);
16265255736Sdavidch}
16266255736Sdavidch
16267255736Sdavidchstatic void
16268255736Sdavidchbxe_iov_init_dmae(struct bxe_softc *sc)
16269255736Sdavidch{
16270255736Sdavidch    return;
16271255736Sdavidch}
16272255736Sdavidch
16273255736Sdavidchstatic void
16274255736Sdavidchbxe_iov_init_dq(struct bxe_softc *sc)
16275255736Sdavidch{
16276255736Sdavidch    return;
16277255736Sdavidch}
16278255736Sdavidch
16279255736Sdavidch/* send a NIG loopback debug packet */
16280255736Sdavidchstatic void
16281255736Sdavidchbxe_lb_pckt(struct bxe_softc *sc)
16282255736Sdavidch{
16283255736Sdavidch    uint32_t wb_write[3];
16284255736Sdavidch
16285255736Sdavidch    /* Ethernet source and destination addresses */
16286255736Sdavidch    wb_write[0] = 0x55555555;
16287255736Sdavidch    wb_write[1] = 0x55555555;
16288255736Sdavidch    wb_write[2] = 0x20;     /* SOP */
16289255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16290255736Sdavidch
16291255736Sdavidch    /* NON-IP protocol */
16292255736Sdavidch    wb_write[0] = 0x09000000;
16293255736Sdavidch    wb_write[1] = 0x55555555;
16294255736Sdavidch    wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16295255736Sdavidch    REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16296255736Sdavidch}
16297255736Sdavidch
16298255736Sdavidch/*
16299255736Sdavidch * Some of the internal memories are not directly readable from the driver.
16300255736Sdavidch * To test them we send debug packets.
16301255736Sdavidch */
16302255736Sdavidchstatic int
16303255736Sdavidchbxe_int_mem_test(struct bxe_softc *sc)
16304255736Sdavidch{
16305255736Sdavidch    int factor;
16306255736Sdavidch    int count, i;
16307255736Sdavidch    uint32_t val = 0;
16308255736Sdavidch
16309255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
16310255736Sdavidch        factor = 120;
16311255736Sdavidch    } else if (CHIP_REV_IS_EMUL(sc)) {
16312255736Sdavidch        factor = 200;
16313255736Sdavidch    } else {
16314255736Sdavidch        factor = 1;
16315255736Sdavidch    }
16316255736Sdavidch
16317255736Sdavidch    /* disable inputs of parser neighbor blocks */
16318255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16319255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16320255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16321255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16322255736Sdavidch
16323255736Sdavidch    /*  write 0 to parser credits for CFC search request */
16324255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16325255736Sdavidch
16326255736Sdavidch    /* send Ethernet packet */
16327255736Sdavidch    bxe_lb_pckt(sc);
16328255736Sdavidch
16329255736Sdavidch    /* TODO do i reset NIG statistic? */
16330255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16331255736Sdavidch    count = 1000 * factor;
16332255736Sdavidch    while (count) {
16333255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16334255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16335255736Sdavidch        if (val == 0x10) {
16336255736Sdavidch            break;
16337255736Sdavidch        }
16338255736Sdavidch
16339255736Sdavidch        DELAY(10000);
16340255736Sdavidch        count--;
16341255736Sdavidch    }
16342255736Sdavidch
16343255736Sdavidch    if (val != 0x10) {
16344255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16345255736Sdavidch        return (-1);
16346255736Sdavidch    }
16347255736Sdavidch
16348255736Sdavidch    /* wait until PRS register shows 1 packet */
16349255736Sdavidch    count = (1000 * factor);
16350255736Sdavidch    while (count) {
16351255736Sdavidch        val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16352255736Sdavidch        if (val == 1) {
16353255736Sdavidch            break;
16354255736Sdavidch        }
16355255736Sdavidch
16356255736Sdavidch        DELAY(10000);
16357255736Sdavidch        count--;
16358255736Sdavidch    }
16359255736Sdavidch
16360255736Sdavidch    if (val != 0x1) {
16361255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16362255736Sdavidch        return (-2);
16363255736Sdavidch    }
16364255736Sdavidch
16365255736Sdavidch    /* Reset and init BRB, PRS */
16366255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16367255736Sdavidch    DELAY(50000);
16368255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16369255736Sdavidch    DELAY(50000);
16370255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16371255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16372255736Sdavidch
16373255736Sdavidch    /* Disable inputs of parser neighbor blocks */
16374255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16375255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16376255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16377255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16378255736Sdavidch
16379255736Sdavidch    /* Write 0 to parser credits for CFC search request */
16380255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16381255736Sdavidch
16382255736Sdavidch    /* send 10 Ethernet packets */
16383255736Sdavidch    for (i = 0; i < 10; i++) {
16384255736Sdavidch        bxe_lb_pckt(sc);
16385255736Sdavidch    }
16386255736Sdavidch
16387255736Sdavidch    /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16388255736Sdavidch    count = (1000 * factor);
16389255736Sdavidch    while (count) {
16390255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16391255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
16392255736Sdavidch        if (val == 0xb0) {
16393255736Sdavidch            break;
16394255736Sdavidch        }
16395255736Sdavidch
16396255736Sdavidch        DELAY(10000);
16397255736Sdavidch        count--;
16398255736Sdavidch    }
16399255736Sdavidch
16400255736Sdavidch    if (val != 0xb0) {
16401255736Sdavidch        BLOGE(sc, "NIG timeout val=0x%x\n", val);
16402255736Sdavidch        return (-3);
16403255736Sdavidch    }
16404255736Sdavidch
16405255736Sdavidch    /* Wait until PRS register shows 2 packets */
16406255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16407255736Sdavidch    if (val != 2) {
16408255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16409255736Sdavidch    }
16410255736Sdavidch
16411255736Sdavidch    /* Write 1 to parser credits for CFC search request */
16412255736Sdavidch    REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16413255736Sdavidch
16414255736Sdavidch    /* Wait until PRS register shows 3 packets */
16415255736Sdavidch    DELAY(10000 * factor);
16416255736Sdavidch
16417255736Sdavidch    /* Wait until NIG register shows 1 packet of size 0x10 */
16418255736Sdavidch    val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16419255736Sdavidch    if (val != 3) {
16420255736Sdavidch        BLOGE(sc, "PRS timeout val=0x%x\n", val);
16421255736Sdavidch    }
16422255736Sdavidch
16423255736Sdavidch    /* clear NIG EOP FIFO */
16424255736Sdavidch    for (i = 0; i < 11; i++) {
16425255736Sdavidch        REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16426255736Sdavidch    }
16427255736Sdavidch
16428255736Sdavidch    val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16429255736Sdavidch    if (val != 1) {
16430295830Sdavidcs        BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16431255736Sdavidch        return (-4);
16432255736Sdavidch    }
16433255736Sdavidch
16434255736Sdavidch    /* Reset and init BRB, PRS, NIG */
16435255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16436255736Sdavidch    DELAY(50000);
16437255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16438255736Sdavidch    DELAY(50000);
16439255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16440255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16441255736Sdavidch    if (!CNIC_SUPPORT(sc)) {
16442255736Sdavidch        /* set NIC mode */
16443255736Sdavidch        REG_WR(sc, PRS_REG_NIC_MODE, 1);
16444255736Sdavidch    }
16445255736Sdavidch
16446255736Sdavidch    /* Enable inputs of parser neighbor blocks */
16447255736Sdavidch    REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16448255736Sdavidch    REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16449255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16450255736Sdavidch    REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16451255736Sdavidch
16452255736Sdavidch    return (0);
16453255736Sdavidch}
16454255736Sdavidch
16455255736Sdavidchstatic void
16456255736Sdavidchbxe_setup_fan_failure_detection(struct bxe_softc *sc)
16457255736Sdavidch{
16458255736Sdavidch    int is_required;
16459255736Sdavidch    uint32_t val;
16460255736Sdavidch    int port;
16461255736Sdavidch
16462255736Sdavidch    is_required = 0;
16463255736Sdavidch    val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
16464255736Sdavidch           SHARED_HW_CFG_FAN_FAILURE_MASK);
16465255736Sdavidch
16466255736Sdavidch    if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
16467255736Sdavidch        is_required = 1;
16468255736Sdavidch    }
16469255736Sdavidch    /*
16470255736Sdavidch     * The fan failure mechanism is usually related to the PHY type since
16471255736Sdavidch     * the power consumption of the board is affected by the PHY. Currently,
16472255736Sdavidch     * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
16473255736Sdavidch     */
16474255736Sdavidch    else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
16475255736Sdavidch        for (port = PORT_0; port < PORT_MAX; port++) {
16476255736Sdavidch            is_required |= elink_fan_failure_det_req(sc,
16477255736Sdavidch                                                     sc->devinfo.shmem_base,
16478255736Sdavidch                                                     sc->devinfo.shmem2_base,
16479255736Sdavidch                                                     port);
16480255736Sdavidch        }
16481255736Sdavidch    }
16482255736Sdavidch
16483255736Sdavidch    BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
16484255736Sdavidch
16485255736Sdavidch    if (is_required == 0) {
16486255736Sdavidch        return;
16487255736Sdavidch    }
16488255736Sdavidch
16489255736Sdavidch    /* Fan failure is indicated by SPIO 5 */
16490255736Sdavidch    bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
16491255736Sdavidch
16492255736Sdavidch    /* set to active low mode */
16493255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_INT);
16494255736Sdavidch    val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
16495255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_INT, val);
16496255736Sdavidch
16497255736Sdavidch    /* enable interrupt to signal the IGU */
16498255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
16499255736Sdavidch    val |= MISC_SPIO_SPIO5;
16500255736Sdavidch    REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
16501255736Sdavidch}
16502255736Sdavidch
16503255736Sdavidchstatic void
16504255736Sdavidchbxe_enable_blocks_attention(struct bxe_softc *sc)
16505255736Sdavidch{
16506255736Sdavidch    uint32_t val;
16507255736Sdavidch
16508255736Sdavidch    REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16509255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16510255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
16511255736Sdavidch    } else {
16512255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
16513255736Sdavidch    }
16514255736Sdavidch    REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16515255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16516255736Sdavidch    /*
16517255736Sdavidch     * mask read length error interrupts in brb for parser
16518255736Sdavidch     * (parsing unit and 'checksum and crc' unit)
16519255736Sdavidch     * these errors are legal (PU reads fixed length and CAC can cause
16520255736Sdavidch     * read length error on truncated packets)
16521255736Sdavidch     */
16522255736Sdavidch    REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
16523255736Sdavidch    REG_WR(sc, QM_REG_QM_INT_MASK, 0);
16524255736Sdavidch    REG_WR(sc, TM_REG_TM_INT_MASK, 0);
16525255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
16526255736Sdavidch    REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
16527255736Sdavidch    REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
16528255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
16529255736Sdavidch/*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
16530255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
16531255736Sdavidch    REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
16532255736Sdavidch    REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
16533255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
16534255736Sdavidch/*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
16535255736Sdavidch    REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
16536255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
16537255736Sdavidch    REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
16538255736Sdavidch    REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
16539255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
16540255736Sdavidch/*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
16541255736Sdavidch
16542255736Sdavidch    val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
16543255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
16544255736Sdavidch           PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
16545255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16546255736Sdavidch        val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
16547255736Sdavidch                PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
16548255736Sdavidch    }
16549255736Sdavidch    REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
16550255736Sdavidch
16551255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
16552255736Sdavidch    REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
16553255736Sdavidch    REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
16554255736Sdavidch/*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
16555255736Sdavidch
16556255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16557255736Sdavidch        /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
16558255736Sdavidch        REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
16559255736Sdavidch    }
16560255736Sdavidch
16561255736Sdavidch    REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
16562255736Sdavidch    REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
16563255736Sdavidch/*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
16564255736Sdavidch    REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
16565255736Sdavidch}
16566255736Sdavidch
16567255736Sdavidch/**
16568255736Sdavidch * bxe_init_hw_common - initialize the HW at the COMMON phase.
16569255736Sdavidch *
16570255736Sdavidch * @sc:     driver handle
16571255736Sdavidch */
16572255736Sdavidchstatic int
16573255736Sdavidchbxe_init_hw_common(struct bxe_softc *sc)
16574255736Sdavidch{
16575255736Sdavidch    uint8_t abs_func_id;
16576255736Sdavidch    uint32_t val;
16577255736Sdavidch
16578255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
16579255736Sdavidch          SC_ABS_FUNC(sc));
16580255736Sdavidch
16581255736Sdavidch    /*
16582255736Sdavidch     * take the RESET lock to protect undi_unload flow from accessing
16583255736Sdavidch     * registers while we are resetting the chip
16584255736Sdavidch     */
16585255736Sdavidch    bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16586255736Sdavidch
16587255736Sdavidch    bxe_reset_common(sc);
16588255736Sdavidch
16589255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
16590255736Sdavidch
16591255736Sdavidch    val = 0xfffc;
16592255736Sdavidch    if (CHIP_IS_E3(sc)) {
16593255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16594255736Sdavidch        val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16595255736Sdavidch    }
16596255736Sdavidch
16597255736Sdavidch    REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
16598255736Sdavidch
16599255736Sdavidch    bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16600255736Sdavidch
16601255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
16602255736Sdavidch    BLOGD(sc, DBG_LOAD, "after misc block init\n");
16603255736Sdavidch
16604255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16605255736Sdavidch        /*
16606255736Sdavidch         * 4-port mode or 2-port mode we need to turn off master-enable for
16607255736Sdavidch         * everyone. After that we turn it back on for self. So, we disregard
16608255736Sdavidch         * multi-function, and always disable all functions on the given path,
16609255736Sdavidch         * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
16610255736Sdavidch         */
16611255736Sdavidch        for (abs_func_id = SC_PATH(sc);
16612255736Sdavidch             abs_func_id < (E2_FUNC_MAX * 2);
16613255736Sdavidch             abs_func_id += 2) {
16614255736Sdavidch            if (abs_func_id == SC_ABS_FUNC(sc)) {
16615255736Sdavidch                REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
16616255736Sdavidch                continue;
16617255736Sdavidch            }
16618255736Sdavidch
16619255736Sdavidch            bxe_pretend_func(sc, abs_func_id);
16620255736Sdavidch
16621255736Sdavidch            /* clear pf enable */
16622255736Sdavidch            bxe_pf_disable(sc);
16623255736Sdavidch
16624255736Sdavidch            bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16625255736Sdavidch        }
16626255736Sdavidch    }
16627255736Sdavidch
16628255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pf disable\n");
16629255736Sdavidch
16630255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
16631255736Sdavidch
16632255736Sdavidch    if (CHIP_IS_E1(sc)) {
16633255736Sdavidch        /*
16634255736Sdavidch         * enable HW interrupt from PXP on USDM overflow
16635255736Sdavidch         * bit 16 on INT_MASK_0
16636255736Sdavidch         */
16637255736Sdavidch        REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16638255736Sdavidch    }
16639255736Sdavidch
16640255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
16641255736Sdavidch    bxe_init_pxp(sc);
16642255736Sdavidch
16643255736Sdavidch#ifdef __BIG_ENDIAN
16644255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
16645255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
16646255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
16647255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
16648255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
16649255736Sdavidch    /* make sure this value is 0 */
16650255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
16651255736Sdavidch
16652255736Sdavidch    //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
16653255736Sdavidch    REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
16654255736Sdavidch    REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
16655255736Sdavidch    REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
16656255736Sdavidch    REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
16657255736Sdavidch#endif
16658255736Sdavidch
16659255736Sdavidch    ecore_ilt_init_page_size(sc, INITOP_SET);
16660255736Sdavidch
16661255736Sdavidch    if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
16662255736Sdavidch        REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
16663255736Sdavidch    }
16664255736Sdavidch
16665255736Sdavidch    /* let the HW do it's magic... */
16666255736Sdavidch    DELAY(100000);
16667255736Sdavidch
16668255736Sdavidch    /* finish PXP init */
16669255736Sdavidch    val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
16670255736Sdavidch    if (val != 1) {
16671295830Sdavidcs        BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
16672295830Sdavidcs            val);
16673255736Sdavidch        return (-1);
16674255736Sdavidch    }
16675255736Sdavidch    val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
16676255736Sdavidch    if (val != 1) {
16677295830Sdavidcs        BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
16678255736Sdavidch        return (-1);
16679255736Sdavidch    }
16680255736Sdavidch
16681255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pxp init\n");
16682255736Sdavidch
16683255736Sdavidch    /*
16684255736Sdavidch     * Timer bug workaround for E2 only. We need to set the entire ILT to have
16685255736Sdavidch     * entries with value "0" and valid bit on. This needs to be done by the
16686255736Sdavidch     * first PF that is loaded in a path (i.e. common phase)
16687255736Sdavidch     */
16688255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16689255736Sdavidch/*
16690255736Sdavidch * In E2 there is a bug in the timers block that can cause function 6 / 7
16691255736Sdavidch * (i.e. vnic3) to start even if it is marked as "scan-off".
16692255736Sdavidch * This occurs when a different function (func2,3) is being marked
16693255736Sdavidch * as "scan-off". Real-life scenario for example: if a driver is being
16694255736Sdavidch * load-unloaded while func6,7 are down. This will cause the timer to access
16695255736Sdavidch * the ilt, translate to a logical address and send a request to read/write.
16696255736Sdavidch * Since the ilt for the function that is down is not valid, this will cause
16697255736Sdavidch * a translation error which is unrecoverable.
16698255736Sdavidch * The Workaround is intended to make sure that when this happens nothing
16699255736Sdavidch * fatal will occur. The workaround:
16700255736Sdavidch *  1.  First PF driver which loads on a path will:
16701255736Sdavidch *      a.  After taking the chip out of reset, by using pretend,
16702255736Sdavidch *          it will write "0" to the following registers of
16703255736Sdavidch *          the other vnics.
16704255736Sdavidch *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16705255736Sdavidch *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
16706255736Sdavidch *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
16707255736Sdavidch *          And for itself it will write '1' to
16708255736Sdavidch *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
16709255736Sdavidch *          dmae-operations (writing to pram for example.)
16710255736Sdavidch *          note: can be done for only function 6,7 but cleaner this
16711255736Sdavidch *            way.
16712255736Sdavidch *      b.  Write zero+valid to the entire ILT.
16713255736Sdavidch *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
16714255736Sdavidch *          VNIC3 (of that port). The range allocated will be the
16715255736Sdavidch *          entire ILT. This is needed to prevent  ILT range error.
16716255736Sdavidch *  2.  Any PF driver load flow:
16717255736Sdavidch *      a.  ILT update with the physical addresses of the allocated
16718255736Sdavidch *          logical pages.
16719255736Sdavidch *      b.  Wait 20msec. - note that this timeout is needed to make
16720255736Sdavidch *          sure there are no requests in one of the PXP internal
16721255736Sdavidch *          queues with "old" ILT addresses.
16722255736Sdavidch *      c.  PF enable in the PGLC.
16723255736Sdavidch *      d.  Clear the was_error of the PF in the PGLC. (could have
16724255736Sdavidch *          occurred while driver was down)
16725255736Sdavidch *      e.  PF enable in the CFC (WEAK + STRONG)
16726255736Sdavidch *      f.  Timers scan enable
16727255736Sdavidch *  3.  PF driver unload flow:
16728255736Sdavidch *      a.  Clear the Timers scan_en.
16729255736Sdavidch *      b.  Polling for scan_on=0 for that PF.
16730255736Sdavidch *      c.  Clear the PF enable bit in the PXP.
16731255736Sdavidch *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
16732255736Sdavidch *      e.  Write zero+valid to all ILT entries (The valid bit must
16733255736Sdavidch *          stay set)
16734255736Sdavidch *      f.  If this is VNIC 3 of a port then also init
16735255736Sdavidch *          first_timers_ilt_entry to zero and last_timers_ilt_entry
16736255736Sdavidch *          to the last enrty in the ILT.
16737255736Sdavidch *
16738255736Sdavidch *      Notes:
16739255736Sdavidch *      Currently the PF error in the PGLC is non recoverable.
16740255736Sdavidch *      In the future the there will be a recovery routine for this error.
16741255736Sdavidch *      Currently attention is masked.
16742255736Sdavidch *      Having an MCP lock on the load/unload process does not guarantee that
16743255736Sdavidch *      there is no Timer disable during Func6/7 enable. This is because the
16744255736Sdavidch *      Timers scan is currently being cleared by the MCP on FLR.
16745255736Sdavidch *      Step 2.d can be done only for PF6/7 and the driver can also check if
16746255736Sdavidch *      there is error before clearing it. But the flow above is simpler and
16747255736Sdavidch *      more general.
16748255736Sdavidch *      All ILT entries are written by zero+valid and not just PF6/7
16749255736Sdavidch *      ILT entries since in the future the ILT entries allocation for
16750255736Sdavidch *      PF-s might be dynamic.
16751255736Sdavidch */
16752255736Sdavidch        struct ilt_client_info ilt_cli;
16753255736Sdavidch        struct ecore_ilt ilt;
16754255736Sdavidch
16755255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
16756255736Sdavidch        memset(&ilt, 0, sizeof(struct ecore_ilt));
16757255736Sdavidch
16758255736Sdavidch        /* initialize dummy TM client */
16759255736Sdavidch        ilt_cli.start      = 0;
16760255736Sdavidch        ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
16761255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
16762255736Sdavidch
16763255736Sdavidch        /*
16764255736Sdavidch         * Step 1: set zeroes to all ilt page entries with valid bit on
16765255736Sdavidch         * Step 2: set the timers first/last ilt entry to point
16766255736Sdavidch         * to the entire range to prevent ILT range error for 3rd/4th
16767255736Sdavidch         * vnic (this code assumes existence of the vnic)
16768255736Sdavidch         *
16769255736Sdavidch         * both steps performed by call to ecore_ilt_client_init_op()
16770255736Sdavidch         * with dummy TM client
16771255736Sdavidch         *
16772255736Sdavidch         * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
16773255736Sdavidch         * and his brother are split registers
16774255736Sdavidch         */
16775255736Sdavidch
16776255736Sdavidch        bxe_pretend_func(sc, (SC_PATH(sc) + 6));
16777255736Sdavidch        ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
16778255736Sdavidch        bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16779255736Sdavidch
16780255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
16781255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
16782255736Sdavidch        REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
16783255736Sdavidch    }
16784255736Sdavidch
16785255736Sdavidch    REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
16786255736Sdavidch    REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
16787255736Sdavidch
16788255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16789255736Sdavidch        int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
16790255736Sdavidch                     (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
16791255736Sdavidch
16792255736Sdavidch        ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
16793255736Sdavidch        ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
16794255736Sdavidch
16795255736Sdavidch        /* let the HW do it's magic... */
16796255736Sdavidch        do {
16797255736Sdavidch            DELAY(200000);
16798255736Sdavidch            val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
16799255736Sdavidch        } while (factor-- && (val != 1));
16800255736Sdavidch
16801255736Sdavidch        if (val != 1) {
16802295830Sdavidcs            BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
16803255736Sdavidch            return (-1);
16804255736Sdavidch        }
16805255736Sdavidch    }
16806255736Sdavidch
16807255736Sdavidch    BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
16808255736Sdavidch
16809255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
16810255736Sdavidch
16811255736Sdavidch    bxe_iov_init_dmae(sc);
16812255736Sdavidch
16813255736Sdavidch    /* clean the DMAE memory */
16814255736Sdavidch    sc->dmae_ready = 1;
16815255736Sdavidch    ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
16816255736Sdavidch
16817255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
16818255736Sdavidch
16819255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
16820255736Sdavidch
16821255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
16822255736Sdavidch
16823255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
16824255736Sdavidch
16825255736Sdavidch    bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
16826255736Sdavidch    bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
16827255736Sdavidch    bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
16828255736Sdavidch    bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
16829255736Sdavidch
16830255736Sdavidch    ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
16831255736Sdavidch
16832255736Sdavidch    /* QM queues pointers table */
16833255736Sdavidch    ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
16834255736Sdavidch
16835255736Sdavidch    /* soft reset pulse */
16836255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 1);
16837255736Sdavidch    REG_WR(sc, QM_REG_SOFT_RESET, 0);
16838255736Sdavidch
16839255736Sdavidch    if (CNIC_SUPPORT(sc))
16840255736Sdavidch        ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
16841255736Sdavidch
16842255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
16843255736Sdavidch    REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
16844255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
16845255736Sdavidch        /* enable hw interrupt from doorbell Q */
16846255736Sdavidch        REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16847255736Sdavidch    }
16848255736Sdavidch
16849255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16850255736Sdavidch
16851255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16852255736Sdavidch    REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
16853255736Sdavidch
16854255736Sdavidch    if (!CHIP_IS_E1(sc)) {
16855255736Sdavidch        REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
16856255736Sdavidch    }
16857255736Sdavidch
16858255736Sdavidch    if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
16859255736Sdavidch        if (IS_MF_AFEX(sc)) {
16860255736Sdavidch            /*
16861255736Sdavidch             * configure that AFEX and VLAN headers must be
16862255736Sdavidch             * received in AFEX mode
16863255736Sdavidch             */
16864255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
16865255736Sdavidch            REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
16866255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
16867255736Sdavidch            REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
16868255736Sdavidch            REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
16869255736Sdavidch        } else {
16870255736Sdavidch            /*
16871255736Sdavidch             * Bit-map indicating which L2 hdrs may appear
16872255736Sdavidch             * after the basic Ethernet header
16873255736Sdavidch             */
16874255736Sdavidch            REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
16875255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
16876255736Sdavidch        }
16877255736Sdavidch    }
16878255736Sdavidch
16879255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
16880255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
16881255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
16882255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
16883255736Sdavidch
16884255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16885255736Sdavidch        /* reset VFC memories */
16886255736Sdavidch        REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16887255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
16888255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
16889255736Sdavidch        REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16890255736Sdavidch               VFC_MEMORIES_RST_REG_CAM_RST |
16891255736Sdavidch               VFC_MEMORIES_RST_REG_RAM_RST);
16892255736Sdavidch
16893255736Sdavidch        DELAY(20000);
16894255736Sdavidch    }
16895255736Sdavidch
16896255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
16897255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
16898255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
16899255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
16900255736Sdavidch
16901255736Sdavidch    /* sync semi rtc */
16902255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
16903255736Sdavidch           0x80000000);
16904255736Sdavidch    REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
16905255736Sdavidch           0x80000000);
16906255736Sdavidch
16907255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
16908255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
16909255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
16910255736Sdavidch
16911255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16912255736Sdavidch        if (IS_MF_AFEX(sc)) {
16913255736Sdavidch            /*
16914255736Sdavidch             * configure that AFEX and VLAN headers must be
16915255736Sdavidch             * sent in AFEX mode
16916255736Sdavidch             */
16917255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
16918255736Sdavidch            REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
16919255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
16920255736Sdavidch            REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
16921255736Sdavidch            REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
16922255736Sdavidch        } else {
16923255736Sdavidch            REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
16924255736Sdavidch                   sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
16925255736Sdavidch        }
16926255736Sdavidch    }
16927255736Sdavidch
16928255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 1);
16929255736Sdavidch
16930255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
16931255736Sdavidch
16932255736Sdavidch    if (CNIC_SUPPORT(sc)) {
16933255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
16934255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
16935255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
16936255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
16937255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
16938255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
16939255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
16940255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
16941255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
16942255736Sdavidch        REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
16943255736Sdavidch    }
16944255736Sdavidch    REG_WR(sc, SRC_REG_SOFT_RST, 0);
16945255736Sdavidch
16946255736Sdavidch    if (sizeof(union cdu_context) != 1024) {
16947255736Sdavidch        /* we currently assume that a context is 1024 bytes */
16948255736Sdavidch        BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
16949255736Sdavidch              (long)sizeof(union cdu_context));
16950255736Sdavidch    }
16951255736Sdavidch
16952255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
16953255736Sdavidch    val = (4 << 24) + (0 << 12) + 1024;
16954255736Sdavidch    REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
16955255736Sdavidch
16956255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
16957255736Sdavidch
16958255736Sdavidch    REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
16959255736Sdavidch    /* enable context validation interrupt from CFC */
16960255736Sdavidch    REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16961255736Sdavidch
16962255736Sdavidch    /* set the thresholds to prevent CFC/CDU race */
16963255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
16964255736Sdavidch    ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
16965255736Sdavidch
16966255736Sdavidch    if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
16967255736Sdavidch        REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
16968255736Sdavidch    }
16969255736Sdavidch
16970255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
16971255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
16972255736Sdavidch
16973255736Sdavidch    /* Reset PCIE errors for debug */
16974255736Sdavidch    REG_WR(sc, 0x2814, 0xffffffff);
16975255736Sdavidch    REG_WR(sc, 0x3820, 0xffffffff);
16976255736Sdavidch
16977255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
16978255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
16979255736Sdavidch               (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
16980255736Sdavidch                PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
16981255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
16982255736Sdavidch               (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
16983255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
16984255736Sdavidch                PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
16985255736Sdavidch        REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
16986255736Sdavidch               (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
16987255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
16988255736Sdavidch                PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
16989255736Sdavidch    }
16990255736Sdavidch
16991255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
16992255736Sdavidch
16993255736Sdavidch    if (!CHIP_IS_E1(sc)) {
16994255736Sdavidch        /* in E3 this done in per-port section */
16995255736Sdavidch        if (!CHIP_IS_E3(sc))
16996255736Sdavidch            REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
16997255736Sdavidch    }
16998255736Sdavidch
16999255736Sdavidch    if (CHIP_IS_E1H(sc)) {
17000255736Sdavidch        /* not applicable for E2 (and above ...) */
17001255736Sdavidch        REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17002255736Sdavidch    }
17003255736Sdavidch
17004255736Sdavidch    if (CHIP_REV_IS_SLOW(sc)) {
17005255736Sdavidch        DELAY(200000);
17006255736Sdavidch    }
17007255736Sdavidch
17008255736Sdavidch    /* finish CFC init */
17009255736Sdavidch    val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17010255736Sdavidch    if (val != 1) {
17011295830Sdavidcs        BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17012255736Sdavidch        return (-1);
17013255736Sdavidch    }
17014255736Sdavidch    val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17015255736Sdavidch    if (val != 1) {
17016295830Sdavidcs        BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17017255736Sdavidch        return (-1);
17018255736Sdavidch    }
17019255736Sdavidch    val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17020255736Sdavidch    if (val != 1) {
17021295830Sdavidcs        BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17022255736Sdavidch        return (-1);
17023255736Sdavidch    }
17024255736Sdavidch    REG_WR(sc, CFC_REG_DEBUG0, 0);
17025255736Sdavidch
17026255736Sdavidch    if (CHIP_IS_E1(sc)) {
17027255736Sdavidch        /* read NIG statistic to see if this is our first up since powerup */
17028255736Sdavidch        bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17029255736Sdavidch        val = *BXE_SP(sc, wb_data[0]);
17030255736Sdavidch
17031255736Sdavidch        /* do internal memory self test */
17032255736Sdavidch        if ((val == 0) && bxe_int_mem_test(sc)) {
17033295830Sdavidcs            BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17034255736Sdavidch            return (-1);
17035255736Sdavidch        }
17036255736Sdavidch    }
17037255736Sdavidch
17038255736Sdavidch    bxe_setup_fan_failure_detection(sc);
17039255736Sdavidch
17040255736Sdavidch    /* clear PXP2 attentions */
17041255736Sdavidch    REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17042255736Sdavidch
17043255736Sdavidch    bxe_enable_blocks_attention(sc);
17044255736Sdavidch
17045255736Sdavidch    if (!CHIP_REV_IS_SLOW(sc)) {
17046255736Sdavidch        ecore_enable_blocks_parity(sc);
17047255736Sdavidch    }
17048255736Sdavidch
17049255736Sdavidch    if (!BXE_NOMCP(sc)) {
17050255736Sdavidch        if (CHIP_IS_E1x(sc)) {
17051255736Sdavidch            bxe_common_init_phy(sc);
17052255736Sdavidch        }
17053255736Sdavidch    }
17054255736Sdavidch
17055255736Sdavidch    return (0);
17056255736Sdavidch}
17057255736Sdavidch
17058255736Sdavidch/**
17059255736Sdavidch * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17060255736Sdavidch *
17061255736Sdavidch * @sc:     driver handle
17062255736Sdavidch */
17063255736Sdavidchstatic int
17064255736Sdavidchbxe_init_hw_common_chip(struct bxe_softc *sc)
17065255736Sdavidch{
17066255736Sdavidch    int rc = bxe_init_hw_common(sc);
17067255736Sdavidch
17068255736Sdavidch    if (rc) {
17069295830Sdavidcs        BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17070255736Sdavidch        return (rc);
17071255736Sdavidch    }
17072255736Sdavidch
17073255736Sdavidch    /* In E2 2-PORT mode, same ext phy is used for the two paths */
17074255736Sdavidch    if (!BXE_NOMCP(sc)) {
17075255736Sdavidch        bxe_common_init_phy(sc);
17076255736Sdavidch    }
17077255736Sdavidch
17078255736Sdavidch    return (0);
17079255736Sdavidch}
17080255736Sdavidch
17081255736Sdavidchstatic int
17082255736Sdavidchbxe_init_hw_port(struct bxe_softc *sc)
17083255736Sdavidch{
17084255736Sdavidch    int port = SC_PORT(sc);
17085255736Sdavidch    int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17086255736Sdavidch    uint32_t low, high;
17087255736Sdavidch    uint32_t val;
17088255736Sdavidch
17089255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17090255736Sdavidch
17091255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17092255736Sdavidch
17093255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17094255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17095255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17096255736Sdavidch
17097255736Sdavidch    /*
17098255736Sdavidch     * Timers bug workaround: disables the pf_master bit in pglue at
17099255736Sdavidch     * common phase, we need to enable it here before any dmae access are
17100255736Sdavidch     * attempted. Therefore we manually added the enable-master to the
17101255736Sdavidch     * port phase (it also happens in the function phase)
17102255736Sdavidch     */
17103255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17104255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17105255736Sdavidch    }
17106255736Sdavidch
17107255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17108255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17109255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17110255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17111255736Sdavidch
17112255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17113255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17114255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17115255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17116255736Sdavidch
17117255736Sdavidch    /* QM cid (connection) count */
17118255736Sdavidch    ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17119255736Sdavidch
17120255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17121255736Sdavidch        ecore_init_block(sc, BLOCK_TM, init_phase);
17122255736Sdavidch        REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17123255736Sdavidch        REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17124255736Sdavidch    }
17125255736Sdavidch
17126255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17127255736Sdavidch
17128255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17129255736Sdavidch
17130255736Sdavidch    if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17131255736Sdavidch        if (IS_MF(sc)) {
17132255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 160 : 246);
17133255736Sdavidch        } else if (sc->mtu > 4096) {
17134255736Sdavidch            if (BXE_ONE_PORT(sc)) {
17135255736Sdavidch                low = 160;
17136255736Sdavidch            } else {
17137255736Sdavidch                val = sc->mtu;
17138255736Sdavidch                /* (24*1024 + val*4)/256 */
17139255736Sdavidch                low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17140255736Sdavidch            }
17141255736Sdavidch        } else {
17142255736Sdavidch            low = (BXE_ONE_PORT(sc) ? 80 : 160);
17143255736Sdavidch        }
17144255736Sdavidch        high = (low + 56); /* 14*1024/256 */
17145255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17146255736Sdavidch        REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17147255736Sdavidch    }
17148255736Sdavidch
17149255736Sdavidch    if (CHIP_IS_MODE_4_PORT(sc)) {
17150255736Sdavidch        REG_WR(sc, SC_PORT(sc) ?
17151255736Sdavidch               BRB1_REG_MAC_GUARANTIED_1 :
17152255736Sdavidch               BRB1_REG_MAC_GUARANTIED_0, 40);
17153255736Sdavidch    }
17154255736Sdavidch
17155255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17156255736Sdavidch    if (CHIP_IS_E3B0(sc)) {
17157255736Sdavidch        if (IS_MF_AFEX(sc)) {
17158255736Sdavidch            /* configure headers for AFEX mode */
17159255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17160255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17161255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17162255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17163255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17164255736Sdavidch                   PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17165255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17166255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17167255736Sdavidch                   PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17168255736Sdavidch        } else {
17169255736Sdavidch            /* Ovlan exists only if we are in multi-function +
17170255736Sdavidch             * switch-dependent mode, in switch-independent there
17171255736Sdavidch             * is no ovlan headers
17172255736Sdavidch             */
17173255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17174255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17175255736Sdavidch                   PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17176255736Sdavidch                   (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17177255736Sdavidch        }
17178255736Sdavidch    }
17179255736Sdavidch
17180255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17181255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17182255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17183255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17184255736Sdavidch
17185255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17186255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17187255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17188255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17189255736Sdavidch
17190255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17191255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17192255736Sdavidch
17193255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17194255736Sdavidch
17195255736Sdavidch    if (CHIP_IS_E1x(sc)) {
17196255736Sdavidch        /* configure PBF to work without PAUSE mtu 9000 */
17197255736Sdavidch        REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17198255736Sdavidch
17199255736Sdavidch        /* update threshold */
17200255736Sdavidch        REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17201255736Sdavidch        /* update init credit */
17202255736Sdavidch        REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17203255736Sdavidch
17204255736Sdavidch        /* probe changes */
17205255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17206255736Sdavidch        DELAY(50);
17207255736Sdavidch        REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17208255736Sdavidch    }
17209255736Sdavidch
17210255736Sdavidch    if (CNIC_SUPPORT(sc)) {
17211255736Sdavidch        ecore_init_block(sc, BLOCK_SRC, init_phase);
17212255736Sdavidch    }
17213255736Sdavidch
17214255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17215255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17216255736Sdavidch
17217255736Sdavidch    if (CHIP_IS_E1(sc)) {
17218255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17219255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17220255736Sdavidch    }
17221255736Sdavidch    ecore_init_block(sc, BLOCK_HC, init_phase);
17222255736Sdavidch
17223255736Sdavidch    ecore_init_block(sc, BLOCK_IGU, init_phase);
17224255736Sdavidch
17225255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17226255736Sdavidch    /* init aeu_mask_attn_func_0/1:
17227255736Sdavidch     *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17228255736Sdavidch     *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17229255736Sdavidch     *             bits 4-7 are used for "per vn group attention" */
17230255736Sdavidch    val = IS_MF(sc) ? 0xF7 : 0x7;
17231255736Sdavidch    /* Enable DCBX attention for all but E1 */
17232255736Sdavidch    val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17233255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17234255736Sdavidch
17235255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17236255736Sdavidch
17237255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17238255736Sdavidch        /* Bit-map indicating which L2 hdrs may appear after the
17239255736Sdavidch         * basic Ethernet header
17240255736Sdavidch         */
17241255736Sdavidch        if (IS_MF_AFEX(sc)) {
17242255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17243255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17244255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17245255736Sdavidch        } else {
17246255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17247255736Sdavidch                   NIG_REG_P1_HDRS_AFTER_BASIC :
17248255736Sdavidch                   NIG_REG_P0_HDRS_AFTER_BASIC,
17249255736Sdavidch                   IS_MF_SD(sc) ? 7 : 6);
17250255736Sdavidch        }
17251255736Sdavidch
17252255736Sdavidch        if (CHIP_IS_E3(sc)) {
17253255736Sdavidch            REG_WR(sc, SC_PORT(sc) ?
17254255736Sdavidch                   NIG_REG_LLH1_MF_MODE :
17255255736Sdavidch                   NIG_REG_LLH_MF_MODE, IS_MF(sc));
17256255736Sdavidch        }
17257255736Sdavidch    }
17258255736Sdavidch    if (!CHIP_IS_E3(sc)) {
17259255736Sdavidch        REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17260255736Sdavidch    }
17261255736Sdavidch
17262255736Sdavidch    if (!CHIP_IS_E1(sc)) {
17263255736Sdavidch        /* 0x2 disable mf_ov, 0x1 enable */
17264255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17265255736Sdavidch               (IS_MF_SD(sc) ? 0x1 : 0x2));
17266255736Sdavidch
17267255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17268255736Sdavidch            val = 0;
17269255736Sdavidch            switch (sc->devinfo.mf_info.mf_mode) {
17270255736Sdavidch            case MULTI_FUNCTION_SD:
17271255736Sdavidch                val = 1;
17272255736Sdavidch                break;
17273255736Sdavidch            case MULTI_FUNCTION_SI:
17274255736Sdavidch            case MULTI_FUNCTION_AFEX:
17275255736Sdavidch                val = 2;
17276255736Sdavidch                break;
17277255736Sdavidch            }
17278255736Sdavidch
17279255736Sdavidch            REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17280255736Sdavidch                        NIG_REG_LLH0_CLS_TYPE), val);
17281255736Sdavidch        }
17282255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17283255736Sdavidch        REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17284255736Sdavidch        REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17285255736Sdavidch    }
17286255736Sdavidch
17287255736Sdavidch    /* If SPIO5 is set to generate interrupts, enable it for this port */
17288255736Sdavidch    val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17289255736Sdavidch    if (val & MISC_SPIO_SPIO5) {
17290255736Sdavidch        uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17291255736Sdavidch                                    MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17292255736Sdavidch        val = REG_RD(sc, reg_addr);
17293255736Sdavidch        val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17294255736Sdavidch        REG_WR(sc, reg_addr, val);
17295255736Sdavidch    }
17296255736Sdavidch
17297255736Sdavidch    return (0);
17298255736Sdavidch}
17299255736Sdavidch
17300255736Sdavidchstatic uint32_t
17301255736Sdavidchbxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17302255736Sdavidch                       uint32_t         reg,
17303255736Sdavidch                       uint32_t         expected,
17304255736Sdavidch                       uint32_t         poll_count)
17305255736Sdavidch{
17306255736Sdavidch    uint32_t cur_cnt = poll_count;
17307255736Sdavidch    uint32_t val;
17308255736Sdavidch
17309255736Sdavidch    while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17310255736Sdavidch        DELAY(FLR_WAIT_INTERVAL);
17311255736Sdavidch    }
17312255736Sdavidch
17313255736Sdavidch    return (val);
17314255736Sdavidch}
17315255736Sdavidch
17316255736Sdavidchstatic int
17317255736Sdavidchbxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17318255736Sdavidch                              uint32_t         reg,
17319255736Sdavidch                              char             *msg,
17320255736Sdavidch                              uint32_t         poll_cnt)
17321255736Sdavidch{
17322255736Sdavidch    uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17323255736Sdavidch
17324255736Sdavidch    if (val != 0) {
17325255736Sdavidch        BLOGE(sc, "%s usage count=%d\n", msg, val);
17326255736Sdavidch        return (1);
17327255736Sdavidch    }
17328255736Sdavidch
17329255736Sdavidch    return (0);
17330255736Sdavidch}
17331255736Sdavidch
17332255736Sdavidch/* Common routines with VF FLR cleanup */
17333255736Sdavidchstatic uint32_t
17334255736Sdavidchbxe_flr_clnup_poll_count(struct bxe_softc *sc)
17335255736Sdavidch{
17336255736Sdavidch    /* adjust polling timeout */
17337255736Sdavidch    if (CHIP_REV_IS_EMUL(sc)) {
17338255736Sdavidch        return (FLR_POLL_CNT * 2000);
17339255736Sdavidch    }
17340255736Sdavidch
17341255736Sdavidch    if (CHIP_REV_IS_FPGA(sc)) {
17342255736Sdavidch        return (FLR_POLL_CNT * 120);
17343255736Sdavidch    }
17344255736Sdavidch
17345255736Sdavidch    return (FLR_POLL_CNT);
17346255736Sdavidch}
17347255736Sdavidch
17348255736Sdavidchstatic int
17349255736Sdavidchbxe_poll_hw_usage_counters(struct bxe_softc *sc,
17350255736Sdavidch                           uint32_t         poll_cnt)
17351255736Sdavidch{
17352255736Sdavidch    /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17353255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17354255736Sdavidch                                      CFC_REG_NUM_LCIDS_INSIDE_PF,
17355255736Sdavidch                                      "CFC PF usage counter timed out",
17356255736Sdavidch                                      poll_cnt)) {
17357255736Sdavidch        return (1);
17358255736Sdavidch    }
17359255736Sdavidch
17360255736Sdavidch    /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17361255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17362255736Sdavidch                                      DORQ_REG_PF_USAGE_CNT,
17363255736Sdavidch                                      "DQ PF usage counter timed out",
17364255736Sdavidch                                      poll_cnt)) {
17365255736Sdavidch        return (1);
17366255736Sdavidch    }
17367255736Sdavidch
17368255736Sdavidch    /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17369255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17370255736Sdavidch                                      QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17371255736Sdavidch                                      "QM PF usage counter timed out",
17372255736Sdavidch                                      poll_cnt)) {
17373255736Sdavidch        return (1);
17374255736Sdavidch    }
17375255736Sdavidch
17376255736Sdavidch    /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17377255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17378255736Sdavidch                                      TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17379255736Sdavidch                                      "Timers VNIC usage counter timed out",
17380255736Sdavidch                                      poll_cnt)) {
17381255736Sdavidch        return (1);
17382255736Sdavidch    }
17383255736Sdavidch
17384255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17385255736Sdavidch                                      TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17386255736Sdavidch                                      "Timers NUM_SCANS usage counter timed out",
17387255736Sdavidch                                      poll_cnt)) {
17388255736Sdavidch        return (1);
17389255736Sdavidch    }
17390255736Sdavidch
17391255736Sdavidch    /* Wait DMAE PF usage counter to zero */
17392255736Sdavidch    if (bxe_flr_clnup_poll_hw_counter(sc,
17393255736Sdavidch                                      dmae_reg_go_c[INIT_DMAE_C(sc)],
17394255736Sdavidch                                      "DMAE dommand register timed out",
17395255736Sdavidch                                      poll_cnt)) {
17396255736Sdavidch        return (1);
17397255736Sdavidch    }
17398255736Sdavidch
17399255736Sdavidch    return (0);
17400255736Sdavidch}
17401255736Sdavidch
17402255736Sdavidch#define OP_GEN_PARAM(param)                                            \
17403255736Sdavidch    (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17404255736Sdavidch#define OP_GEN_TYPE(type)                                           \
17405255736Sdavidch    (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17406255736Sdavidch#define OP_GEN_AGG_VECT(index)                                             \
17407255736Sdavidch    (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17408255736Sdavidch
17409255736Sdavidchstatic int
17410255736Sdavidchbxe_send_final_clnup(struct bxe_softc *sc,
17411255736Sdavidch                     uint8_t          clnup_func,
17412255736Sdavidch                     uint32_t         poll_cnt)
17413255736Sdavidch{
17414255736Sdavidch    uint32_t op_gen_command = 0;
17415255736Sdavidch    uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17416255736Sdavidch                          CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17417255736Sdavidch    int ret = 0;
17418255736Sdavidch
17419255736Sdavidch    if (REG_RD(sc, comp_addr)) {
17420255736Sdavidch        BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17421255736Sdavidch        return (1);
17422255736Sdavidch    }
17423255736Sdavidch
17424255736Sdavidch    op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17425255736Sdavidch    op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17426255736Sdavidch    op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17427255736Sdavidch    op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17428255736Sdavidch
17429255736Sdavidch    BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17430255736Sdavidch    REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17431255736Sdavidch
17432255736Sdavidch    if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17433255736Sdavidch        BLOGE(sc, "FW final cleanup did not succeed\n");
17434255736Sdavidch        BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17435255736Sdavidch              (REG_RD(sc, comp_addr)));
17436255736Sdavidch        bxe_panic(sc, ("FLR cleanup failed\n"));
17437255736Sdavidch        return (1);
17438255736Sdavidch    }
17439255736Sdavidch
17440255736Sdavidch    /* Zero completion for nxt FLR */
17441255736Sdavidch    REG_WR(sc, comp_addr, 0);
17442255736Sdavidch
17443255736Sdavidch    return (ret);
17444255736Sdavidch}
17445255736Sdavidch
17446255736Sdavidchstatic void
17447255736Sdavidchbxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17448255736Sdavidch                       struct pbf_pN_buf_regs *regs,
17449255736Sdavidch                       uint32_t               poll_count)
17450255736Sdavidch{
17451255736Sdavidch    uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17452255736Sdavidch    uint32_t cur_cnt = poll_count;
17453255736Sdavidch
17454255736Sdavidch    crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
17455255736Sdavidch    crd = crd_start = REG_RD(sc, regs->crd);
17456255736Sdavidch    init_crd = REG_RD(sc, regs->init_crd);
17457255736Sdavidch
17458255736Sdavidch    BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
17459255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
17460255736Sdavidch    BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
17461255736Sdavidch
17462255736Sdavidch    while ((crd != init_crd) &&
17463255736Sdavidch           ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
17464255736Sdavidch            (init_crd - crd_start))) {
17465255736Sdavidch        if (cur_cnt--) {
17466255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
17467255736Sdavidch            crd = REG_RD(sc, regs->crd);
17468255736Sdavidch            crd_freed = REG_RD(sc, regs->crd_freed);
17469255736Sdavidch        } else {
17470255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
17471255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
17472255736Sdavidch            BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
17473255736Sdavidch            break;
17474255736Sdavidch        }
17475255736Sdavidch    }
17476255736Sdavidch
17477255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
17478255736Sdavidch          poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17479255736Sdavidch}
17480255736Sdavidch
17481255736Sdavidchstatic void
17482255736Sdavidchbxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
17483255736Sdavidch                       struct pbf_pN_cmd_regs *regs,
17484255736Sdavidch                       uint32_t               poll_count)
17485255736Sdavidch{
17486255736Sdavidch    uint32_t occup, to_free, freed, freed_start;
17487255736Sdavidch    uint32_t cur_cnt = poll_count;
17488255736Sdavidch
17489255736Sdavidch    occup = to_free = REG_RD(sc, regs->lines_occup);
17490255736Sdavidch    freed = freed_start = REG_RD(sc, regs->lines_freed);
17491255736Sdavidch
17492255736Sdavidch    BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17493255736Sdavidch    BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17494255736Sdavidch
17495255736Sdavidch    while (occup &&
17496255736Sdavidch           ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
17497255736Sdavidch        if (cur_cnt--) {
17498255736Sdavidch            DELAY(FLR_WAIT_INTERVAL);
17499255736Sdavidch            occup = REG_RD(sc, regs->lines_occup);
17500255736Sdavidch            freed = REG_RD(sc, regs->lines_freed);
17501255736Sdavidch        } else {
17502255736Sdavidch            BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
17503255736Sdavidch            BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17504255736Sdavidch            BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17505255736Sdavidch            break;
17506255736Sdavidch        }
17507255736Sdavidch    }
17508255736Sdavidch
17509255736Sdavidch    BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
17510255736Sdavidch          poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17511255736Sdavidch}
17512255736Sdavidch
17513255736Sdavidchstatic void
17514255736Sdavidchbxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
17515255736Sdavidch{
17516255736Sdavidch    struct pbf_pN_cmd_regs cmd_regs[] = {
17517255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
17518255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q0 :
17519255736Sdavidch            PBF_REG_P0_TQ_OCCUPANCY,
17520255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17521255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q0 :
17522255736Sdavidch            PBF_REG_P0_TQ_LINES_FREED_CNT},
17523255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
17524255736Sdavidch            PBF_REG_TQ_OCCUPANCY_Q1 :
17525255736Sdavidch            PBF_REG_P1_TQ_OCCUPANCY,
17526255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17527255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_Q1 :
17528255736Sdavidch            PBF_REG_P1_TQ_LINES_FREED_CNT},
17529255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
17530255736Sdavidch            PBF_REG_TQ_OCCUPANCY_LB_Q :
17531255736Sdavidch            PBF_REG_P4_TQ_OCCUPANCY,
17532255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17533255736Sdavidch            PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
17534255736Sdavidch            PBF_REG_P4_TQ_LINES_FREED_CNT}
17535255736Sdavidch    };
17536255736Sdavidch
17537255736Sdavidch    struct pbf_pN_buf_regs buf_regs[] = {
17538255736Sdavidch        {0, (CHIP_IS_E3B0(sc)) ?
17539255736Sdavidch            PBF_REG_INIT_CRD_Q0 :
17540255736Sdavidch            PBF_REG_P0_INIT_CRD ,
17541255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17542255736Sdavidch            PBF_REG_CREDIT_Q0 :
17543255736Sdavidch            PBF_REG_P0_CREDIT,
17544255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17545255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
17546255736Sdavidch            PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
17547255736Sdavidch        {1, (CHIP_IS_E3B0(sc)) ?
17548255736Sdavidch            PBF_REG_INIT_CRD_Q1 :
17549255736Sdavidch            PBF_REG_P1_INIT_CRD,
17550255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17551255736Sdavidch            PBF_REG_CREDIT_Q1 :
17552255736Sdavidch            PBF_REG_P1_CREDIT,
17553255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17554255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
17555255736Sdavidch            PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
17556255736Sdavidch        {4, (CHIP_IS_E3B0(sc)) ?
17557255736Sdavidch            PBF_REG_INIT_CRD_LB_Q :
17558255736Sdavidch            PBF_REG_P4_INIT_CRD,
17559255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17560255736Sdavidch            PBF_REG_CREDIT_LB_Q :
17561255736Sdavidch            PBF_REG_P4_CREDIT,
17562255736Sdavidch            (CHIP_IS_E3B0(sc)) ?
17563255736Sdavidch            PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
17564255736Sdavidch            PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
17565255736Sdavidch    };
17566255736Sdavidch
17567255736Sdavidch    int i;
17568255736Sdavidch
17569255736Sdavidch    /* Verify the command queues are flushed P0, P1, P4 */
17570255736Sdavidch    for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
17571255736Sdavidch        bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
17572255736Sdavidch    }
17573255736Sdavidch
17574255736Sdavidch    /* Verify the transmission buffers are flushed P0, P1, P4 */
17575255736Sdavidch    for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
17576255736Sdavidch        bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
17577255736Sdavidch    }
17578255736Sdavidch}
17579255736Sdavidch
17580255736Sdavidchstatic void
17581255736Sdavidchbxe_hw_enable_status(struct bxe_softc *sc)
17582255736Sdavidch{
17583255736Sdavidch    uint32_t val;
17584255736Sdavidch
17585255736Sdavidch    val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
17586255736Sdavidch    BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
17587255736Sdavidch
17588255736Sdavidch    val = REG_RD(sc, PBF_REG_DISABLE_PF);
17589255736Sdavidch    BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
17590255736Sdavidch
17591255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
17592255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
17593255736Sdavidch
17594255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
17595255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
17596255736Sdavidch
17597255736Sdavidch    val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
17598255736Sdavidch    BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
17599255736Sdavidch
17600255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
17601255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
17602255736Sdavidch
17603255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
17604255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
17605255736Sdavidch
17606255736Sdavidch    val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
17607255736Sdavidch    BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
17608255736Sdavidch}
17609255736Sdavidch
17610255736Sdavidchstatic int
17611255736Sdavidchbxe_pf_flr_clnup(struct bxe_softc *sc)
17612255736Sdavidch{
17613255736Sdavidch    uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
17614255736Sdavidch
17615255736Sdavidch    BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
17616255736Sdavidch
17617255736Sdavidch    /* Re-enable PF target read access */
17618255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
17619255736Sdavidch
17620255736Sdavidch    /* Poll HW usage counters */
17621255736Sdavidch    BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
17622255736Sdavidch    if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
17623255736Sdavidch        return (-1);
17624255736Sdavidch    }
17625255736Sdavidch
17626255736Sdavidch    /* Zero the igu 'trailing edge' and 'leading edge' */
17627255736Sdavidch
17628255736Sdavidch    /* Send the FW cleanup command */
17629255736Sdavidch    if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
17630255736Sdavidch        return (-1);
17631255736Sdavidch    }
17632255736Sdavidch
17633255736Sdavidch    /* ATC cleanup */
17634255736Sdavidch
17635255736Sdavidch    /* Verify TX hw is flushed */
17636255736Sdavidch    bxe_tx_hw_flushed(sc, poll_cnt);
17637255736Sdavidch
17638255736Sdavidch    /* Wait 100ms (not adjusted according to platform) */
17639255736Sdavidch    DELAY(100000);
17640255736Sdavidch
17641255736Sdavidch    /* Verify no pending pci transactions */
17642255736Sdavidch    if (bxe_is_pcie_pending(sc)) {
17643255736Sdavidch        BLOGE(sc, "PCIE Transactions still pending\n");
17644255736Sdavidch    }
17645255736Sdavidch
17646255736Sdavidch    /* Debug */
17647255736Sdavidch    bxe_hw_enable_status(sc);
17648255736Sdavidch
17649255736Sdavidch    /*
17650255736Sdavidch     * Master enable - Due to WB DMAE writes performed before this
17651255736Sdavidch     * register is re-initialized as part of the regular function init
17652255736Sdavidch     */
17653255736Sdavidch    REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17654255736Sdavidch
17655255736Sdavidch    return (0);
17656255736Sdavidch}
17657255736Sdavidch
17658255736Sdavidchstatic int
17659255736Sdavidchbxe_init_hw_func(struct bxe_softc *sc)
17660255736Sdavidch{
17661255736Sdavidch    int port = SC_PORT(sc);
17662255736Sdavidch    int func = SC_FUNC(sc);
17663255736Sdavidch    int init_phase = PHASE_PF0 + func;
17664255736Sdavidch    struct ecore_ilt *ilt = sc->ilt;
17665255736Sdavidch    uint16_t cdu_ilt_start;
17666255736Sdavidch    uint32_t addr, val;
17667255736Sdavidch    uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
17668255736Sdavidch    int i, main_mem_width, rc;
17669255736Sdavidch
17670255736Sdavidch    BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
17671255736Sdavidch
17672255736Sdavidch    /* FLR cleanup */
17673255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17674255736Sdavidch        rc = bxe_pf_flr_clnup(sc);
17675255736Sdavidch        if (rc) {
17676255736Sdavidch            BLOGE(sc, "FLR cleanup failed!\n");
17677255736Sdavidch            // XXX bxe_fw_dump(sc);
17678255736Sdavidch            // XXX bxe_idle_chk(sc);
17679255736Sdavidch            return (rc);
17680255736Sdavidch        }
17681255736Sdavidch    }
17682255736Sdavidch
17683255736Sdavidch    /* set MSI reconfigure capability */
17684255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
17685255736Sdavidch        addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
17686255736Sdavidch        val = REG_RD(sc, addr);
17687255736Sdavidch        val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
17688255736Sdavidch        REG_WR(sc, addr, val);
17689255736Sdavidch    }
17690255736Sdavidch
17691255736Sdavidch    ecore_init_block(sc, BLOCK_PXP, init_phase);
17692255736Sdavidch    ecore_init_block(sc, BLOCK_PXP2, init_phase);
17693255736Sdavidch
17694255736Sdavidch    ilt = sc->ilt;
17695255736Sdavidch    cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
17696255736Sdavidch
17697255736Sdavidch    for (i = 0; i < L2_ILT_LINES(sc); i++) {
17698255736Sdavidch        ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
17699255736Sdavidch        ilt->lines[cdu_ilt_start + i].page_mapping =
17700255736Sdavidch            sc->context[i].vcxt_dma.paddr;
17701255736Sdavidch        ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
17702255736Sdavidch    }
17703255736Sdavidch    ecore_ilt_init_op(sc, INITOP_SET);
17704255736Sdavidch
17705295830Sdavidcs    /* Set NIC mode */
17706295830Sdavidcs    REG_WR(sc, PRS_REG_NIC_MODE, 1);
17707295830Sdavidcs    BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
17708255736Sdavidch
17709255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17710255736Sdavidch        uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
17711255736Sdavidch
17712255736Sdavidch        /* Turn on a single ISR mode in IGU if driver is going to use
17713255736Sdavidch         * INT#x or MSI
17714255736Sdavidch         */
17715255736Sdavidch        if (sc->interrupt_mode != INTR_MODE_MSIX) {
17716255736Sdavidch            pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
17717255736Sdavidch        }
17718255736Sdavidch
17719255736Sdavidch        /*
17720255736Sdavidch         * Timers workaround bug: function init part.
17721255736Sdavidch         * Need to wait 20msec after initializing ILT,
17722255736Sdavidch         * needed to make sure there are no requests in
17723255736Sdavidch         * one of the PXP internal queues with "old" ILT addresses
17724255736Sdavidch         */
17725255736Sdavidch        DELAY(20000);
17726255736Sdavidch
17727255736Sdavidch        /*
17728255736Sdavidch         * Master enable - Due to WB DMAE writes performed before this
17729255736Sdavidch         * register is re-initialized as part of the regular function
17730255736Sdavidch         * init
17731255736Sdavidch         */
17732255736Sdavidch        REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17733255736Sdavidch        /* Enable the function in IGU */
17734255736Sdavidch        REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
17735255736Sdavidch    }
17736255736Sdavidch
17737255736Sdavidch    sc->dmae_ready = 1;
17738255736Sdavidch
17739255736Sdavidch    ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17740255736Sdavidch
17741255736Sdavidch    if (!CHIP_IS_E1x(sc))
17742255736Sdavidch        REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
17743255736Sdavidch
17744255736Sdavidch    ecore_init_block(sc, BLOCK_ATC, init_phase);
17745255736Sdavidch    ecore_init_block(sc, BLOCK_DMAE, init_phase);
17746255736Sdavidch    ecore_init_block(sc, BLOCK_NIG, init_phase);
17747255736Sdavidch    ecore_init_block(sc, BLOCK_SRC, init_phase);
17748255736Sdavidch    ecore_init_block(sc, BLOCK_MISC, init_phase);
17749255736Sdavidch    ecore_init_block(sc, BLOCK_TCM, init_phase);
17750255736Sdavidch    ecore_init_block(sc, BLOCK_UCM, init_phase);
17751255736Sdavidch    ecore_init_block(sc, BLOCK_CCM, init_phase);
17752255736Sdavidch    ecore_init_block(sc, BLOCK_XCM, init_phase);
17753255736Sdavidch    ecore_init_block(sc, BLOCK_TSEM, init_phase);
17754255736Sdavidch    ecore_init_block(sc, BLOCK_USEM, init_phase);
17755255736Sdavidch    ecore_init_block(sc, BLOCK_CSEM, init_phase);
17756255736Sdavidch    ecore_init_block(sc, BLOCK_XSEM, init_phase);
17757255736Sdavidch
17758255736Sdavidch    if (!CHIP_IS_E1x(sc))
17759255736Sdavidch        REG_WR(sc, QM_REG_PF_EN, 1);
17760255736Sdavidch
17761255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
17762255736Sdavidch        REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17763255736Sdavidch        REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17764255736Sdavidch        REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17765255736Sdavidch        REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17766255736Sdavidch    }
17767255736Sdavidch    ecore_init_block(sc, BLOCK_QM, init_phase);
17768255736Sdavidch
17769255736Sdavidch    ecore_init_block(sc, BLOCK_TM, init_phase);
17770255736Sdavidch    ecore_init_block(sc, BLOCK_DORQ, init_phase);
17771255736Sdavidch
17772255736Sdavidch    bxe_iov_init_dq(sc);
17773255736Sdavidch
17774255736Sdavidch    ecore_init_block(sc, BLOCK_BRB1, init_phase);
17775255736Sdavidch    ecore_init_block(sc, BLOCK_PRS, init_phase);
17776255736Sdavidch    ecore_init_block(sc, BLOCK_TSDM, init_phase);
17777255736Sdavidch    ecore_init_block(sc, BLOCK_CSDM, init_phase);
17778255736Sdavidch    ecore_init_block(sc, BLOCK_USDM, init_phase);
17779255736Sdavidch    ecore_init_block(sc, BLOCK_XSDM, init_phase);
17780255736Sdavidch    ecore_init_block(sc, BLOCK_UPB, init_phase);
17781255736Sdavidch    ecore_init_block(sc, BLOCK_XPB, init_phase);
17782255736Sdavidch    ecore_init_block(sc, BLOCK_PBF, init_phase);
17783255736Sdavidch    if (!CHIP_IS_E1x(sc))
17784255736Sdavidch        REG_WR(sc, PBF_REG_DISABLE_PF, 0);
17785255736Sdavidch
17786255736Sdavidch    ecore_init_block(sc, BLOCK_CDU, init_phase);
17787255736Sdavidch
17788255736Sdavidch    ecore_init_block(sc, BLOCK_CFC, init_phase);
17789255736Sdavidch
17790255736Sdavidch    if (!CHIP_IS_E1x(sc))
17791255736Sdavidch        REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
17792255736Sdavidch
17793255736Sdavidch    if (IS_MF(sc)) {
17794255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
17795255736Sdavidch        REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
17796255736Sdavidch    }
17797255736Sdavidch
17798255736Sdavidch    ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17799255736Sdavidch
17800255736Sdavidch    /* HC init per function */
17801255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
17802255736Sdavidch        if (CHIP_IS_E1H(sc)) {
17803255736Sdavidch            REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17804255736Sdavidch
17805255736Sdavidch            REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17806255736Sdavidch            REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17807255736Sdavidch        }
17808255736Sdavidch        ecore_init_block(sc, BLOCK_HC, init_phase);
17809255736Sdavidch
17810255736Sdavidch    } else {
17811255736Sdavidch        int num_segs, sb_idx, prod_offset;
17812255736Sdavidch
17813255736Sdavidch        REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17814255736Sdavidch
17815255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17816255736Sdavidch            REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
17817255736Sdavidch            REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
17818255736Sdavidch        }
17819255736Sdavidch
17820255736Sdavidch        ecore_init_block(sc, BLOCK_IGU, init_phase);
17821255736Sdavidch
17822255736Sdavidch        if (!CHIP_IS_E1x(sc)) {
17823255736Sdavidch            int dsb_idx = 0;
17824255736Sdavidch            /**
17825255736Sdavidch             * Producer memory:
17826255736Sdavidch             * E2 mode: address 0-135 match to the mapping memory;
17827255736Sdavidch             * 136 - PF0 default prod; 137 - PF1 default prod;
17828255736Sdavidch             * 138 - PF2 default prod; 139 - PF3 default prod;
17829255736Sdavidch             * 140 - PF0 attn prod;    141 - PF1 attn prod;
17830255736Sdavidch             * 142 - PF2 attn prod;    143 - PF3 attn prod;
17831255736Sdavidch             * 144-147 reserved.
17832255736Sdavidch             *
17833255736Sdavidch             * E1.5 mode - In backward compatible mode;
17834255736Sdavidch             * for non default SB; each even line in the memory
17835255736Sdavidch             * holds the U producer and each odd line hold
17836255736Sdavidch             * the C producer. The first 128 producers are for
17837255736Sdavidch             * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
17838255736Sdavidch             * producers are for the DSB for each PF.
17839255736Sdavidch             * Each PF has five segments: (the order inside each
17840255736Sdavidch             * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
17841255736Sdavidch             * 132-135 C prods; 136-139 X prods; 140-143 T prods;
17842255736Sdavidch             * 144-147 attn prods;
17843255736Sdavidch             */
17844255736Sdavidch            /* non-default-status-blocks */
17845255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17846255736Sdavidch                IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
17847255736Sdavidch            for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
17848255736Sdavidch                prod_offset = (sc->igu_base_sb + sb_idx) *
17849255736Sdavidch                    num_segs;
17850255736Sdavidch
17851255736Sdavidch                for (i = 0; i < num_segs; i++) {
17852255736Sdavidch                    addr = IGU_REG_PROD_CONS_MEMORY +
17853255736Sdavidch                            (prod_offset + i) * 4;
17854255736Sdavidch                    REG_WR(sc, addr, 0);
17855255736Sdavidch                }
17856255736Sdavidch                /* send consumer update with value 0 */
17857255736Sdavidch                bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
17858255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
17859255736Sdavidch                bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
17860255736Sdavidch            }
17861255736Sdavidch
17862255736Sdavidch            /* default-status-blocks */
17863255736Sdavidch            num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17864255736Sdavidch                IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
17865255736Sdavidch
17866255736Sdavidch            if (CHIP_IS_MODE_4_PORT(sc))
17867255736Sdavidch                dsb_idx = SC_FUNC(sc);
17868255736Sdavidch            else
17869255736Sdavidch                dsb_idx = SC_VN(sc);
17870255736Sdavidch
17871255736Sdavidch            prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
17872255736Sdavidch                       IGU_BC_BASE_DSB_PROD + dsb_idx :
17873255736Sdavidch                       IGU_NORM_BASE_DSB_PROD + dsb_idx);
17874255736Sdavidch
17875255736Sdavidch            /*
17876255736Sdavidch             * igu prods come in chunks of E1HVN_MAX (4) -
17877255736Sdavidch             * does not matters what is the current chip mode
17878255736Sdavidch             */
17879255736Sdavidch            for (i = 0; i < (num_segs * E1HVN_MAX);
17880255736Sdavidch                 i += E1HVN_MAX) {
17881255736Sdavidch                addr = IGU_REG_PROD_CONS_MEMORY +
17882255736Sdavidch                            (prod_offset + i)*4;
17883255736Sdavidch                REG_WR(sc, addr, 0);
17884255736Sdavidch            }
17885255736Sdavidch            /* send consumer update with 0 */
17886255736Sdavidch            if (CHIP_INT_MODE_IS_BC(sc)) {
17887255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17888255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
17889255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17890255736Sdavidch                           CSTORM_ID, 0, IGU_INT_NOP, 1);
17891255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17892255736Sdavidch                           XSTORM_ID, 0, IGU_INT_NOP, 1);
17893255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17894255736Sdavidch                           TSTORM_ID, 0, IGU_INT_NOP, 1);
17895255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17896255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
17897255736Sdavidch            } else {
17898255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17899255736Sdavidch                           USTORM_ID, 0, IGU_INT_NOP, 1);
17900255736Sdavidch                bxe_ack_sb(sc, sc->igu_dsb_id,
17901255736Sdavidch                           ATTENTION_ID, 0, IGU_INT_NOP, 1);
17902255736Sdavidch            }
17903255736Sdavidch            bxe_igu_clear_sb(sc, sc->igu_dsb_id);
17904255736Sdavidch
17905255736Sdavidch            /* !!! these should become driver const once
17906255736Sdavidch               rf-tool supports split-68 const */
17907255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
17908255736Sdavidch            REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
17909255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
17910255736Sdavidch            REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
17911255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
17912255736Sdavidch            REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
17913255736Sdavidch        }
17914255736Sdavidch    }
17915255736Sdavidch
17916255736Sdavidch    /* Reset PCIE errors for debug */
17917255736Sdavidch    REG_WR(sc, 0x2114, 0xffffffff);
17918255736Sdavidch    REG_WR(sc, 0x2120, 0xffffffff);
17919255736Sdavidch
17920255736Sdavidch    if (CHIP_IS_E1x(sc)) {
17921255736Sdavidch        main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
17922255736Sdavidch        main_mem_base = HC_REG_MAIN_MEMORY +
17923255736Sdavidch                SC_PORT(sc) * (main_mem_size * 4);
17924255736Sdavidch        main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
17925255736Sdavidch        main_mem_width = 8;
17926255736Sdavidch
17927255736Sdavidch        val = REG_RD(sc, main_mem_prty_clr);
17928255736Sdavidch        if (val) {
17929255736Sdavidch            BLOGD(sc, DBG_LOAD,
17930255736Sdavidch                  "Parity errors in HC block during function init (0x%x)!\n",
17931255736Sdavidch                  val);
17932255736Sdavidch        }
17933255736Sdavidch
17934255736Sdavidch        /* Clear "false" parity errors in MSI-X table */
17935255736Sdavidch        for (i = main_mem_base;
17936255736Sdavidch             i < main_mem_base + main_mem_size * 4;
17937255736Sdavidch             i += main_mem_width) {
17938255736Sdavidch            bxe_read_dmae(sc, i, main_mem_width / 4);
17939255736Sdavidch            bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
17940255736Sdavidch                           i, main_mem_width / 4);
17941255736Sdavidch        }
17942255736Sdavidch        /* Clear HC parity attention */
17943255736Sdavidch        REG_RD(sc, main_mem_prty_clr);
17944255736Sdavidch    }
17945255736Sdavidch
17946255736Sdavidch#if 1
17947255736Sdavidch    /* Enable STORMs SP logging */
17948255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM +
17949255736Sdavidch           USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17950255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM +
17951255736Sdavidch           TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17952255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
17953255736Sdavidch           CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17954255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM +
17955255736Sdavidch           XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17956255736Sdavidch#endif
17957255736Sdavidch
17958255736Sdavidch    elink_phy_probe(&sc->link_params);
17959255736Sdavidch
17960255736Sdavidch    return (0);
17961255736Sdavidch}
17962255736Sdavidch
17963255736Sdavidchstatic void
17964255736Sdavidchbxe_link_reset(struct bxe_softc *sc)
17965255736Sdavidch{
17966255736Sdavidch    if (!BXE_NOMCP(sc)) {
17967284335Sdavidcs	bxe_acquire_phy_lock(sc);
17968255736Sdavidch        elink_lfa_reset(&sc->link_params, &sc->link_vars);
17969284335Sdavidcs	bxe_release_phy_lock(sc);
17970255736Sdavidch    } else {
17971255736Sdavidch        if (!CHIP_REV_IS_SLOW(sc)) {
17972255736Sdavidch            BLOGW(sc, "Bootcode is missing - cannot reset link\n");
17973255736Sdavidch        }
17974255736Sdavidch    }
17975255736Sdavidch}
17976255736Sdavidch
17977255736Sdavidchstatic void
17978255736Sdavidchbxe_reset_port(struct bxe_softc *sc)
17979255736Sdavidch{
17980255736Sdavidch    int port = SC_PORT(sc);
17981255736Sdavidch    uint32_t val;
17982255736Sdavidch
17983255736Sdavidch    /* reset physical Link */
17984255736Sdavidch    bxe_link_reset(sc);
17985255736Sdavidch
17986255736Sdavidch    REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17987255736Sdavidch
17988255736Sdavidch    /* Do not rcv packets to BRB */
17989255736Sdavidch    REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
17990255736Sdavidch    /* Do not direct rcv packets that are not for MCP to the BRB */
17991255736Sdavidch    REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
17992255736Sdavidch               NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
17993255736Sdavidch
17994255736Sdavidch    /* Configure AEU */
17995255736Sdavidch    REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
17996255736Sdavidch
17997255736Sdavidch    DELAY(100000);
17998255736Sdavidch
17999255736Sdavidch    /* Check for BRB port occupancy */
18000255736Sdavidch    val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18001255736Sdavidch    if (val) {
18002255736Sdavidch        BLOGD(sc, DBG_LOAD,
18003255736Sdavidch              "BRB1 is not empty, %d blocks are occupied\n", val);
18004255736Sdavidch    }
18005255736Sdavidch
18006255736Sdavidch    /* TODO: Close Doorbell port? */
18007255736Sdavidch}
18008255736Sdavidch
18009255736Sdavidchstatic void
18010255736Sdavidchbxe_ilt_wr(struct bxe_softc *sc,
18011255736Sdavidch           uint32_t         index,
18012255736Sdavidch           bus_addr_t       addr)
18013255736Sdavidch{
18014255736Sdavidch    int reg;
18015255736Sdavidch    uint32_t wb_write[2];
18016255736Sdavidch
18017255736Sdavidch    if (CHIP_IS_E1(sc)) {
18018255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18019255736Sdavidch    } else {
18020255736Sdavidch        reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18021255736Sdavidch    }
18022255736Sdavidch
18023255736Sdavidch    wb_write[0] = ONCHIP_ADDR1(addr);
18024255736Sdavidch    wb_write[1] = ONCHIP_ADDR2(addr);
18025255736Sdavidch    REG_WR_DMAE(sc, reg, wb_write, 2);
18026255736Sdavidch}
18027255736Sdavidch
18028255736Sdavidchstatic void
18029255736Sdavidchbxe_clear_func_ilt(struct bxe_softc *sc,
18030255736Sdavidch                   uint32_t         func)
18031255736Sdavidch{
18032255736Sdavidch    uint32_t i, base = FUNC_ILT_BASE(func);
18033255736Sdavidch    for (i = base; i < base + ILT_PER_FUNC; i++) {
18034255736Sdavidch        bxe_ilt_wr(sc, i, 0);
18035255736Sdavidch    }
18036255736Sdavidch}
18037255736Sdavidch
18038255736Sdavidchstatic void
18039255736Sdavidchbxe_reset_func(struct bxe_softc *sc)
18040255736Sdavidch{
18041255736Sdavidch    struct bxe_fastpath *fp;
18042255736Sdavidch    int port = SC_PORT(sc);
18043255736Sdavidch    int func = SC_FUNC(sc);
18044255736Sdavidch    int i;
18045255736Sdavidch
18046255736Sdavidch    /* Disable the function in the FW */
18047255736Sdavidch    REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18048255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18049255736Sdavidch    REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18050255736Sdavidch    REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18051255736Sdavidch
18052255736Sdavidch    /* FP SBs */
18053255736Sdavidch    FOR_EACH_ETH_QUEUE(sc, i) {
18054255736Sdavidch        fp = &sc->fp[i];
18055255736Sdavidch        REG_WR8(sc, BAR_CSTRORM_INTMEM +
18056255736Sdavidch                CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18057255736Sdavidch                SB_DISABLED);
18058255736Sdavidch    }
18059255736Sdavidch
18060255736Sdavidch    /* SP SB */
18061255736Sdavidch    REG_WR8(sc, BAR_CSTRORM_INTMEM +
18062255736Sdavidch            CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18063255736Sdavidch            SB_DISABLED);
18064255736Sdavidch
18065255736Sdavidch    for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18066255736Sdavidch        REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18067255736Sdavidch    }
18068255736Sdavidch
18069255736Sdavidch    /* Configure IGU */
18070255736Sdavidch    if (sc->devinfo.int_block == INT_BLOCK_HC) {
18071255736Sdavidch        REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18072255736Sdavidch        REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18073255736Sdavidch    } else {
18074255736Sdavidch        REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18075255736Sdavidch        REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18076255736Sdavidch    }
18077255736Sdavidch
18078255736Sdavidch    if (CNIC_LOADED(sc)) {
18079255736Sdavidch        /* Disable Timer scan */
18080255736Sdavidch        REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18081255736Sdavidch        /*
18082255736Sdavidch         * Wait for at least 10ms and up to 2 second for the timers
18083255736Sdavidch         * scan to complete
18084255736Sdavidch         */
18085255736Sdavidch        for (i = 0; i < 200; i++) {
18086255736Sdavidch            DELAY(10000);
18087255736Sdavidch            if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18088255736Sdavidch                break;
18089255736Sdavidch        }
18090255736Sdavidch    }
18091255736Sdavidch
18092255736Sdavidch    /* Clear ILT */
18093255736Sdavidch    bxe_clear_func_ilt(sc, func);
18094255736Sdavidch
18095255736Sdavidch    /*
18096255736Sdavidch     * Timers workaround bug for E2: if this is vnic-3,
18097255736Sdavidch     * we need to set the entire ilt range for this timers.
18098255736Sdavidch     */
18099255736Sdavidch    if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18100255736Sdavidch        struct ilt_client_info ilt_cli;
18101255736Sdavidch        /* use dummy TM client */
18102255736Sdavidch        memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18103255736Sdavidch        ilt_cli.start = 0;
18104255736Sdavidch        ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18105255736Sdavidch        ilt_cli.client_num = ILT_CLIENT_TM;
18106255736Sdavidch
18107255736Sdavidch        ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18108255736Sdavidch    }
18109255736Sdavidch
18110255736Sdavidch    /* this assumes that reset_port() called before reset_func()*/
18111255736Sdavidch    if (!CHIP_IS_E1x(sc)) {
18112255736Sdavidch        bxe_pf_disable(sc);
18113255736Sdavidch    }
18114255736Sdavidch
18115255736Sdavidch    sc->dmae_ready = 0;
18116255736Sdavidch}
18117255736Sdavidch
18118255736Sdavidchstatic int
18119255736Sdavidchbxe_gunzip_init(struct bxe_softc *sc)
18120255736Sdavidch{
18121255736Sdavidch    return (0);
18122255736Sdavidch}
18123255736Sdavidch
18124255736Sdavidchstatic void
18125255736Sdavidchbxe_gunzip_end(struct bxe_softc *sc)
18126255736Sdavidch{
18127255736Sdavidch    return;
18128255736Sdavidch}
18129255736Sdavidch
18130255736Sdavidchstatic int
18131255736Sdavidchbxe_init_firmware(struct bxe_softc *sc)
18132255736Sdavidch{
18133255736Sdavidch    if (CHIP_IS_E1(sc)) {
18134255736Sdavidch        ecore_init_e1_firmware(sc);
18135255736Sdavidch        sc->iro_array = e1_iro_arr;
18136255736Sdavidch    } else if (CHIP_IS_E1H(sc)) {
18137255736Sdavidch        ecore_init_e1h_firmware(sc);
18138255736Sdavidch        sc->iro_array = e1h_iro_arr;
18139255736Sdavidch    } else if (!CHIP_IS_E1x(sc)) {
18140255736Sdavidch        ecore_init_e2_firmware(sc);
18141255736Sdavidch        sc->iro_array = e2_iro_arr;
18142255736Sdavidch    } else {
18143255736Sdavidch        BLOGE(sc, "Unsupported chip revision\n");
18144255736Sdavidch        return (-1);
18145255736Sdavidch    }
18146255736Sdavidch
18147255736Sdavidch    return (0);
18148255736Sdavidch}
18149255736Sdavidch
18150255736Sdavidchstatic void
18151255736Sdavidchbxe_release_firmware(struct bxe_softc *sc)
18152255736Sdavidch{
18153255736Sdavidch    /* Do nothing */
18154255736Sdavidch    return;
18155255736Sdavidch}
18156255736Sdavidch
18157255736Sdavidchstatic int
18158255736Sdavidchecore_gunzip(struct bxe_softc *sc,
18159255736Sdavidch             const uint8_t    *zbuf,
18160255736Sdavidch             int              len)
18161255736Sdavidch{
18162255736Sdavidch    /* XXX : Implement... */
18163255736Sdavidch    BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18164255736Sdavidch    return (FALSE);
18165255736Sdavidch}
18166255736Sdavidch
18167255736Sdavidchstatic void
18168255736Sdavidchecore_reg_wr_ind(struct bxe_softc *sc,
18169255736Sdavidch                 uint32_t         addr,
18170255736Sdavidch                 uint32_t         val)
18171255736Sdavidch{
18172255736Sdavidch    bxe_reg_wr_ind(sc, addr, val);
18173255736Sdavidch}
18174255736Sdavidch
18175255736Sdavidchstatic void
18176255736Sdavidchecore_write_dmae_phys_len(struct bxe_softc *sc,
18177255736Sdavidch                          bus_addr_t       phys_addr,
18178255736Sdavidch                          uint32_t         addr,
18179255736Sdavidch                          uint32_t         len)
18180255736Sdavidch{
18181255736Sdavidch    bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18182255736Sdavidch}
18183255736Sdavidch
18184255736Sdavidchvoid
18185255736Sdavidchecore_storm_memset_struct(struct bxe_softc *sc,
18186255736Sdavidch                          uint32_t         addr,
18187255736Sdavidch                          size_t           size,
18188255736Sdavidch                          uint32_t         *data)
18189255736Sdavidch{
18190255736Sdavidch    uint8_t i;
18191255736Sdavidch    for (i = 0; i < size/4; i++) {
18192255736Sdavidch        REG_WR(sc, addr + (i * 4), data[i]);
18193255736Sdavidch    }
18194255736Sdavidch}
18195255736Sdavidch
18196292639Sdavidcs
18197292639Sdavidcs/*
18198292639Sdavidcs * character device - ioctl interface definitions
18199292639Sdavidcs */
18200292639Sdavidcs
18201292639Sdavidcs
18202292639Sdavidcs#include "bxe_dump.h"
18203292639Sdavidcs#include "bxe_ioctl.h"
18204292639Sdavidcs#include <sys/conf.h>
18205292639Sdavidcs
18206292639Sdavidcsstatic int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18207292639Sdavidcs                struct thread *td);
18208292639Sdavidcs
18209292639Sdavidcsstatic struct cdevsw bxe_cdevsw = {
18210292639Sdavidcs    .d_version = D_VERSION,
18211292639Sdavidcs    .d_ioctl = bxe_eioctl,
18212292639Sdavidcs    .d_name = "bxecnic",
18213292639Sdavidcs};
18214292639Sdavidcs
18215292639Sdavidcs#define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18216292639Sdavidcs
18217292639Sdavidcs
18218292639Sdavidcs#define DUMP_ALL_PRESETS        0x1FFF
18219292639Sdavidcs#define DUMP_MAX_PRESETS        13
18220292639Sdavidcs#define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18221292639Sdavidcs#define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18222292639Sdavidcs#define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18223292639Sdavidcs#define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18224292639Sdavidcs#define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18225292639Sdavidcs
18226292639Sdavidcs#define IS_REG_IN_PRESET(presets, idx)  \
18227292639Sdavidcs                ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18228292639Sdavidcs
18229292639Sdavidcs
18230292639Sdavidcsstatic int
18231292639Sdavidcsbxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18232292639Sdavidcs{
18233292639Sdavidcs    if (CHIP_IS_E1(sc))
18234292639Sdavidcs        return dump_num_registers[0][preset-1];
18235292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18236292639Sdavidcs        return dump_num_registers[1][preset-1];
18237292639Sdavidcs    else if (CHIP_IS_E2(sc))
18238292639Sdavidcs        return dump_num_registers[2][preset-1];
18239292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18240292639Sdavidcs        return dump_num_registers[3][preset-1];
18241292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18242292639Sdavidcs        return dump_num_registers[4][preset-1];
18243292639Sdavidcs    else
18244292639Sdavidcs        return 0;
18245292639Sdavidcs}
18246292639Sdavidcs
18247292639Sdavidcsstatic int
18248292639Sdavidcsbxe_get_total_regs_len32(struct bxe_softc *sc)
18249292639Sdavidcs{
18250292639Sdavidcs    uint32_t preset_idx;
18251292639Sdavidcs    int regdump_len32 = 0;
18252292639Sdavidcs
18253292639Sdavidcs
18254292639Sdavidcs    /* Calculate the total preset regs length */
18255292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18256292639Sdavidcs        regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18257292639Sdavidcs    }
18258292639Sdavidcs
18259292639Sdavidcs    return regdump_len32;
18260292639Sdavidcs}
18261292639Sdavidcs
18262292639Sdavidcsstatic const uint32_t *
18263292639Sdavidcs__bxe_get_page_addr_ar(struct bxe_softc *sc)
18264292639Sdavidcs{
18265292639Sdavidcs    if (CHIP_IS_E2(sc))
18266292639Sdavidcs        return page_vals_e2;
18267292639Sdavidcs    else if (CHIP_IS_E3(sc))
18268292639Sdavidcs        return page_vals_e3;
18269292639Sdavidcs    else
18270292639Sdavidcs        return NULL;
18271292639Sdavidcs}
18272292639Sdavidcs
18273292639Sdavidcsstatic uint32_t
18274292639Sdavidcs__bxe_get_page_reg_num(struct bxe_softc *sc)
18275292639Sdavidcs{
18276292639Sdavidcs    if (CHIP_IS_E2(sc))
18277292639Sdavidcs        return PAGE_MODE_VALUES_E2;
18278292639Sdavidcs    else if (CHIP_IS_E3(sc))
18279292639Sdavidcs        return PAGE_MODE_VALUES_E3;
18280292639Sdavidcs    else
18281292639Sdavidcs        return 0;
18282292639Sdavidcs}
18283292639Sdavidcs
18284292639Sdavidcsstatic const uint32_t *
18285292639Sdavidcs__bxe_get_page_write_ar(struct bxe_softc *sc)
18286292639Sdavidcs{
18287292639Sdavidcs    if (CHIP_IS_E2(sc))
18288292639Sdavidcs        return page_write_regs_e2;
18289292639Sdavidcs    else if (CHIP_IS_E3(sc))
18290292639Sdavidcs        return page_write_regs_e3;
18291292639Sdavidcs    else
18292292639Sdavidcs        return NULL;
18293292639Sdavidcs}
18294292639Sdavidcs
18295292639Sdavidcsstatic uint32_t
18296292639Sdavidcs__bxe_get_page_write_num(struct bxe_softc *sc)
18297292639Sdavidcs{
18298292639Sdavidcs    if (CHIP_IS_E2(sc))
18299292639Sdavidcs        return PAGE_WRITE_REGS_E2;
18300292639Sdavidcs    else if (CHIP_IS_E3(sc))
18301292639Sdavidcs        return PAGE_WRITE_REGS_E3;
18302292639Sdavidcs    else
18303292639Sdavidcs        return 0;
18304292639Sdavidcs}
18305292639Sdavidcs
18306292639Sdavidcsstatic const struct reg_addr *
18307292639Sdavidcs__bxe_get_page_read_ar(struct bxe_softc *sc)
18308292639Sdavidcs{
18309292639Sdavidcs    if (CHIP_IS_E2(sc))
18310292639Sdavidcs        return page_read_regs_e2;
18311292639Sdavidcs    else if (CHIP_IS_E3(sc))
18312292639Sdavidcs        return page_read_regs_e3;
18313292639Sdavidcs    else
18314292639Sdavidcs        return NULL;
18315292639Sdavidcs}
18316292639Sdavidcs
18317292639Sdavidcsstatic uint32_t
18318292639Sdavidcs__bxe_get_page_read_num(struct bxe_softc *sc)
18319292639Sdavidcs{
18320292639Sdavidcs    if (CHIP_IS_E2(sc))
18321292639Sdavidcs        return PAGE_READ_REGS_E2;
18322292639Sdavidcs    else if (CHIP_IS_E3(sc))
18323292639Sdavidcs        return PAGE_READ_REGS_E3;
18324292639Sdavidcs    else
18325292639Sdavidcs        return 0;
18326292639Sdavidcs}
18327292639Sdavidcs
18328292639Sdavidcsstatic bool
18329292639Sdavidcsbxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18330292639Sdavidcs{
18331292639Sdavidcs    if (CHIP_IS_E1(sc))
18332292639Sdavidcs        return IS_E1_REG(reg_info->chips);
18333292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18334292639Sdavidcs        return IS_E1H_REG(reg_info->chips);
18335292639Sdavidcs    else if (CHIP_IS_E2(sc))
18336292639Sdavidcs        return IS_E2_REG(reg_info->chips);
18337292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18338292639Sdavidcs        return IS_E3A0_REG(reg_info->chips);
18339292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18340292639Sdavidcs        return IS_E3B0_REG(reg_info->chips);
18341292639Sdavidcs    else
18342292639Sdavidcs        return 0;
18343292639Sdavidcs}
18344292639Sdavidcs
18345292639Sdavidcsstatic bool
18346292639Sdavidcsbxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18347292639Sdavidcs{
18348292639Sdavidcs    if (CHIP_IS_E1(sc))
18349292639Sdavidcs        return IS_E1_REG(wreg_info->chips);
18350292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18351292639Sdavidcs        return IS_E1H_REG(wreg_info->chips);
18352292639Sdavidcs    else if (CHIP_IS_E2(sc))
18353292639Sdavidcs        return IS_E2_REG(wreg_info->chips);
18354292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18355292639Sdavidcs        return IS_E3A0_REG(wreg_info->chips);
18356292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18357292639Sdavidcs        return IS_E3B0_REG(wreg_info->chips);
18358292639Sdavidcs    else
18359292639Sdavidcs        return 0;
18360292639Sdavidcs}
18361292639Sdavidcs
18362292639Sdavidcs/**
18363292639Sdavidcs * bxe_read_pages_regs - read "paged" registers
18364292639Sdavidcs *
18365292639Sdavidcs * @bp          device handle
18366292639Sdavidcs * @p           output buffer
18367292639Sdavidcs *
18368292639Sdavidcs * Reads "paged" memories: memories that may only be read by first writing to a
18369292639Sdavidcs * specific address ("write address") and then reading from a specific address
18370292639Sdavidcs * ("read address"). There may be more than one write address per "page" and
18371292639Sdavidcs * more than one read address per write address.
18372292639Sdavidcs */
18373292639Sdavidcsstatic void
18374292639Sdavidcsbxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18375292639Sdavidcs{
18376292639Sdavidcs    uint32_t i, j, k, n;
18377292639Sdavidcs
18378292639Sdavidcs    /* addresses of the paged registers */
18379292639Sdavidcs    const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18380292639Sdavidcs    /* number of paged registers */
18381292639Sdavidcs    int num_pages = __bxe_get_page_reg_num(sc);
18382292639Sdavidcs    /* write addresses */
18383292639Sdavidcs    const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18384292639Sdavidcs    /* number of write addresses */
18385292639Sdavidcs    int write_num = __bxe_get_page_write_num(sc);
18386292639Sdavidcs    /* read addresses info */
18387292639Sdavidcs    const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18388292639Sdavidcs    /* number of read addresses */
18389292639Sdavidcs    int read_num = __bxe_get_page_read_num(sc);
18390292639Sdavidcs    uint32_t addr, size;
18391292639Sdavidcs
18392292639Sdavidcs    for (i = 0; i < num_pages; i++) {
18393292639Sdavidcs        for (j = 0; j < write_num; j++) {
18394292639Sdavidcs            REG_WR(sc, write_addr[j], page_addr[i]);
18395292639Sdavidcs
18396292639Sdavidcs            for (k = 0; k < read_num; k++) {
18397292639Sdavidcs                if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18398292639Sdavidcs                    size = read_addr[k].size;
18399292639Sdavidcs                    for (n = 0; n < size; n++) {
18400292639Sdavidcs                        addr = read_addr[k].addr + n*4;
18401292639Sdavidcs                        *p++ = REG_RD(sc, addr);
18402292639Sdavidcs                    }
18403292639Sdavidcs                }
18404292639Sdavidcs            }
18405292639Sdavidcs        }
18406292639Sdavidcs    }
18407292639Sdavidcs    return;
18408292639Sdavidcs}
18409292639Sdavidcs
18410292639Sdavidcs
18411292639Sdavidcsstatic int
18412292639Sdavidcsbxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18413292639Sdavidcs{
18414292639Sdavidcs    uint32_t i, j, addr;
18415292639Sdavidcs    const struct wreg_addr *wreg_addr_p = NULL;
18416292639Sdavidcs
18417292639Sdavidcs    if (CHIP_IS_E1(sc))
18418292639Sdavidcs        wreg_addr_p = &wreg_addr_e1;
18419292639Sdavidcs    else if (CHIP_IS_E1H(sc))
18420292639Sdavidcs        wreg_addr_p = &wreg_addr_e1h;
18421292639Sdavidcs    else if (CHIP_IS_E2(sc))
18422292639Sdavidcs        wreg_addr_p = &wreg_addr_e2;
18423292639Sdavidcs    else if (CHIP_IS_E3A0(sc))
18424292639Sdavidcs        wreg_addr_p = &wreg_addr_e3;
18425292639Sdavidcs    else if (CHIP_IS_E3B0(sc))
18426292639Sdavidcs        wreg_addr_p = &wreg_addr_e3b0;
18427292639Sdavidcs    else
18428292639Sdavidcs        return (-1);
18429292639Sdavidcs
18430292639Sdavidcs    /* Read the idle_chk registers */
18431292639Sdavidcs    for (i = 0; i < IDLE_REGS_COUNT; i++) {
18432292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18433292639Sdavidcs            IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18434292639Sdavidcs            for (j = 0; j < idle_reg_addrs[i].size; j++)
18435292639Sdavidcs                *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18436292639Sdavidcs        }
18437292639Sdavidcs    }
18438292639Sdavidcs
18439292639Sdavidcs    /* Read the regular registers */
18440292639Sdavidcs    for (i = 0; i < REGS_COUNT; i++) {
18441292639Sdavidcs        if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18442292639Sdavidcs            IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18443292639Sdavidcs            for (j = 0; j < reg_addrs[i].size; j++)
18444292639Sdavidcs                *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18445292639Sdavidcs        }
18446292639Sdavidcs    }
18447292639Sdavidcs
18448292639Sdavidcs    /* Read the CAM registers */
18449292639Sdavidcs    if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18450292639Sdavidcs        IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18451292639Sdavidcs        for (i = 0; i < wreg_addr_p->size; i++) {
18452292639Sdavidcs            *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
18453292639Sdavidcs
18454292639Sdavidcs            /* In case of wreg_addr register, read additional
18455292639Sdavidcs               registers from read_regs array
18456292639Sdavidcs             */
18457292639Sdavidcs            for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
18458292639Sdavidcs                addr = *(wreg_addr_p->read_regs);
18459292639Sdavidcs                *p++ = REG_RD(sc, addr + j*4);
18460292639Sdavidcs            }
18461292639Sdavidcs        }
18462292639Sdavidcs    }
18463292639Sdavidcs
18464292639Sdavidcs    /* Paged registers are supported in E2 & E3 only */
18465292639Sdavidcs    if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
18466292639Sdavidcs        /* Read "paged" registers */
18467292639Sdavidcs        bxe_read_pages_regs(sc, p, preset);
18468292639Sdavidcs    }
18469292639Sdavidcs
18470292639Sdavidcs    return 0;
18471292639Sdavidcs}
18472292639Sdavidcs
18473292639Sdavidcsstatic int
18474295823Sdavidcsbxe_grc_dump(struct bxe_softc *sc)
18475292639Sdavidcs{
18476292639Sdavidcs    int rval = 0;
18477292639Sdavidcs    uint32_t preset_idx;
18478292639Sdavidcs    uint8_t *buf;
18479292639Sdavidcs    uint32_t size;
18480292639Sdavidcs    struct  dump_header *d_hdr;
18481295823Sdavidcs
18482295823Sdavidcs    if (sc->grcdump_done)
18483295823Sdavidcs	return (rval);
18484292639Sdavidcs
18485292639Sdavidcs    ecore_disable_blocks_parity(sc);
18486292639Sdavidcs
18487295823Sdavidcs    buf = sc->grc_dump;
18488295823Sdavidcs    d_hdr = sc->grc_dump;
18489292639Sdavidcs
18490292639Sdavidcs    d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
18491292639Sdavidcs    d_hdr->version = BNX2X_DUMP_VERSION;
18492292639Sdavidcs    d_hdr->preset = DUMP_ALL_PRESETS;
18493292639Sdavidcs
18494292639Sdavidcs    if (CHIP_IS_E1(sc)) {
18495292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1;
18496292639Sdavidcs    } else if (CHIP_IS_E1H(sc)) {
18497292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E1H;
18498292639Sdavidcs    } else if (CHIP_IS_E2(sc)) {
18499292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E2 |
18500292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18501292639Sdavidcs    } else if (CHIP_IS_E3A0(sc)) {
18502292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
18503292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18504292639Sdavidcs    } else if (CHIP_IS_E3B0(sc)) {
18505292639Sdavidcs        d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
18506292639Sdavidcs                (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18507292639Sdavidcs    }
18508292639Sdavidcs
18509292639Sdavidcs    buf += sizeof(struct  dump_header);
18510292639Sdavidcs
18511292639Sdavidcs    for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18512292639Sdavidcs
18513292639Sdavidcs        /* Skip presets with IOR */
18514292639Sdavidcs        if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
18515292639Sdavidcs            (preset_idx == 11))
18516292639Sdavidcs            continue;
18517292639Sdavidcs
18518292639Sdavidcs        rval = bxe_get_preset_regs(sc, sc->grc_dump, preset_idx);
18519292639Sdavidcs
18520292639Sdavidcs	if (rval)
18521292639Sdavidcs            break;
18522292639Sdavidcs
18523292639Sdavidcs        size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
18524292639Sdavidcs
18525292639Sdavidcs        buf += size;
18526292639Sdavidcs    }
18527292639Sdavidcs
18528292639Sdavidcs    ecore_clear_blocks_parity(sc);
18529292639Sdavidcs    ecore_enable_blocks_parity(sc);
18530292639Sdavidcs
18531292639Sdavidcs    sc->grcdump_done = 1;
18532292639Sdavidcs    return(rval);
18533292639Sdavidcs}
18534292639Sdavidcs
18535292639Sdavidcsstatic int
18536292639Sdavidcsbxe_add_cdev(struct bxe_softc *sc)
18537292639Sdavidcs{
18538295823Sdavidcs    int grc_dump_size;
18539292639Sdavidcs
18540295823Sdavidcs    grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18541295823Sdavidcs				sizeof(struct  dump_header);
18542292639Sdavidcs
18543295823Sdavidcs    sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
18544292639Sdavidcs
18545292639Sdavidcs    if (sc->grc_dump == NULL)
18546292639Sdavidcs        return (-1);
18547292639Sdavidcs
18548292639Sdavidcs    sc->ioctl_dev = make_dev(&bxe_cdevsw,
18549292639Sdavidcs                            sc->ifp->if_dunit,
18550292639Sdavidcs                            UID_ROOT,
18551292639Sdavidcs                            GID_WHEEL,
18552292639Sdavidcs                            0600,
18553292639Sdavidcs                            "%s",
18554292639Sdavidcs                            if_name(sc->ifp));
18555292639Sdavidcs
18556292639Sdavidcs    if (sc->ioctl_dev == NULL) {
18557292639Sdavidcs
18558292639Sdavidcs        free(sc->grc_dump, M_DEVBUF);
18559292639Sdavidcs
18560292639Sdavidcs        return (-1);
18561292639Sdavidcs    }
18562292639Sdavidcs
18563292639Sdavidcs    sc->ioctl_dev->si_drv1 = sc;
18564292639Sdavidcs
18565292639Sdavidcs    return (0);
18566292639Sdavidcs}
18567292639Sdavidcs
18568292639Sdavidcsstatic void
18569292639Sdavidcsbxe_del_cdev(struct bxe_softc *sc)
18570292639Sdavidcs{
18571292639Sdavidcs    if (sc->ioctl_dev != NULL)
18572292639Sdavidcs        destroy_dev(sc->ioctl_dev);
18573292639Sdavidcs
18574292639Sdavidcs    if (sc->grc_dump == NULL)
18575292639Sdavidcs        free(sc->grc_dump, M_DEVBUF);
18576292639Sdavidcs
18577292639Sdavidcs    return;
18578292639Sdavidcs}
18579292639Sdavidcs
18580292639Sdavidcsstatic int
18581292639Sdavidcsbxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18582292639Sdavidcs        struct thread *td)
18583292639Sdavidcs{
18584292639Sdavidcs    struct bxe_softc    *sc;
18585292639Sdavidcs    int                 rval = 0;
18586292639Sdavidcs    device_t            pci_dev;
18587292639Sdavidcs    bxe_grcdump_t       *dump = NULL;
18588292639Sdavidcs    int grc_dump_size;
18589292639Sdavidcs
18590292639Sdavidcs    if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
18591292639Sdavidcs        return ENXIO;
18592292639Sdavidcs
18593292639Sdavidcs    pci_dev= sc->dev;
18594292639Sdavidcs
18595292639Sdavidcs    dump = (bxe_grcdump_t *)data;
18596292639Sdavidcs
18597292639Sdavidcs    switch(cmd) {
18598292639Sdavidcs
18599292639Sdavidcs        case BXE_GRC_DUMP_SIZE:
18600292639Sdavidcs            dump->pci_func = sc->pcie_func;
18601292639Sdavidcs            dump->grcdump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18602292639Sdavidcs					sizeof(struct  dump_header);
18603292639Sdavidcs            break;
18604292639Sdavidcs
18605292639Sdavidcs        case BXE_GRC_DUMP:
18606292639Sdavidcs
18607292639Sdavidcs            grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18608292639Sdavidcs				sizeof(struct  dump_header);
18609292639Sdavidcs
18610292639Sdavidcs            if ((sc->grc_dump == NULL) || (dump->grcdump == NULL) ||
18611295823Sdavidcs                (dump->grcdump_size < grc_dump_size) || (!sc->grcdump_done)) {
18612292639Sdavidcs                rval = EINVAL;
18613292639Sdavidcs                break;
18614292639Sdavidcs            }
18615295823Sdavidcs	    dump->grcdump_dwords = grc_dump_size >> 2;
18616295823Sdavidcs            rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
18617295823Sdavidcs            sc->grcdump_done = 0;
18618292639Sdavidcs
18619292639Sdavidcs            break;
18620292639Sdavidcs
18621292639Sdavidcs        default:
18622292639Sdavidcs            break;
18623292639Sdavidcs    }
18624292639Sdavidcs
18625292639Sdavidcs    return (rval);
18626292639Sdavidcs}
18627