xref: /freebsd/sys/dev/bxe/bxe.c (revision acc1a9ef8333c798c210fa94be6af4d5fe2dd794)
1 /*-
2  * Copyright (c) 2007-2014 QLogic Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #define BXE_DRIVER_VERSION "1.78.81"
31 
32 #include "bxe.h"
33 #include "ecore_sp.h"
34 #include "ecore_init.h"
35 #include "ecore_init_ops.h"
36 
37 #include "57710_int_offsets.h"
38 #include "57711_int_offsets.h"
39 #include "57712_int_offsets.h"
40 
41 /*
42  * CTLTYPE_U64 and sysctl_handle_64 were added in r217616. Define these
43  * explicitly here for older kernels that don't include this changeset.
44  */
45 #ifndef CTLTYPE_U64
46 #define CTLTYPE_U64      CTLTYPE_QUAD
47 #define sysctl_handle_64 sysctl_handle_quad
48 #endif
49 
50 /*
51  * CSUM_TCP_IPV6 and CSUM_UDP_IPV6 were added in r236170. Define these
52  * here as zero(0) for older kernels that don't include this changeset
53  * thereby masking the functionality.
54  */
55 #ifndef CSUM_TCP_IPV6
56 #define CSUM_TCP_IPV6 0
57 #define CSUM_UDP_IPV6 0
58 #endif
59 
60 /*
61  * pci_find_cap was added in r219865. Re-define this at pci_find_extcap
62  * for older kernels that don't include this changeset.
63  */
64 #if __FreeBSD_version < 900035
65 #define pci_find_cap pci_find_extcap
66 #endif
67 
68 #define BXE_DEF_SB_ATT_IDX 0x0001
69 #define BXE_DEF_SB_IDX     0x0002
70 
71 /*
72  * FLR Support - bxe_pf_flr_clnup() is called during nic_load in the per
73  * function HW initialization.
74  */
75 #define FLR_WAIT_USEC     10000 /* 10 msecs */
76 #define FLR_WAIT_INTERVAL 50    /* usecs */
77 #define FLR_POLL_CNT      (FLR_WAIT_USEC / FLR_WAIT_INTERVAL) /* 200 */
78 
79 struct pbf_pN_buf_regs {
80     int pN;
81     uint32_t init_crd;
82     uint32_t crd;
83     uint32_t crd_freed;
84 };
85 
86 struct pbf_pN_cmd_regs {
87     int pN;
88     uint32_t lines_occup;
89     uint32_t lines_freed;
90 };
91 
92 /*
93  * PCI Device ID Table used by bxe_probe().
94  */
95 #define BXE_DEVDESC_MAX 64
96 static struct bxe_device_type bxe_devs[] = {
97     {
98         BRCM_VENDORID,
99         CHIP_NUM_57710,
100         PCI_ANY_ID, PCI_ANY_ID,
101         "QLogic NetXtreme II BCM57710 10GbE"
102     },
103     {
104         BRCM_VENDORID,
105         CHIP_NUM_57711,
106         PCI_ANY_ID, PCI_ANY_ID,
107         "QLogic NetXtreme II BCM57711 10GbE"
108     },
109     {
110         BRCM_VENDORID,
111         CHIP_NUM_57711E,
112         PCI_ANY_ID, PCI_ANY_ID,
113         "QLogic NetXtreme II BCM57711E 10GbE"
114     },
115     {
116         BRCM_VENDORID,
117         CHIP_NUM_57712,
118         PCI_ANY_ID, PCI_ANY_ID,
119         "QLogic NetXtreme II BCM57712 10GbE"
120     },
121     {
122         BRCM_VENDORID,
123         CHIP_NUM_57712_MF,
124         PCI_ANY_ID, PCI_ANY_ID,
125         "QLogic NetXtreme II BCM57712 MF 10GbE"
126     },
127     {
128         BRCM_VENDORID,
129         CHIP_NUM_57800,
130         PCI_ANY_ID, PCI_ANY_ID,
131         "QLogic NetXtreme II BCM57800 10GbE"
132     },
133     {
134         BRCM_VENDORID,
135         CHIP_NUM_57800_MF,
136         PCI_ANY_ID, PCI_ANY_ID,
137         "QLogic NetXtreme II BCM57800 MF 10GbE"
138     },
139     {
140         BRCM_VENDORID,
141         CHIP_NUM_57810,
142         PCI_ANY_ID, PCI_ANY_ID,
143         "QLogic NetXtreme II BCM57810 10GbE"
144     },
145     {
146         BRCM_VENDORID,
147         CHIP_NUM_57810_MF,
148         PCI_ANY_ID, PCI_ANY_ID,
149         "QLogic NetXtreme II BCM57810 MF 10GbE"
150     },
151     {
152         BRCM_VENDORID,
153         CHIP_NUM_57811,
154         PCI_ANY_ID, PCI_ANY_ID,
155         "QLogic NetXtreme II BCM57811 10GbE"
156     },
157     {
158         BRCM_VENDORID,
159         CHIP_NUM_57811_MF,
160         PCI_ANY_ID, PCI_ANY_ID,
161         "QLogic NetXtreme II BCM57811 MF 10GbE"
162     },
163     {
164         BRCM_VENDORID,
165         CHIP_NUM_57840_4_10,
166         PCI_ANY_ID, PCI_ANY_ID,
167         "QLogic NetXtreme II BCM57840 4x10GbE"
168     },
169     {
170         BRCM_VENDORID,
171         CHIP_NUM_57840_MF,
172         PCI_ANY_ID, PCI_ANY_ID,
173         "QLogic NetXtreme II BCM57840 MF 10GbE"
174     },
175     {
176         0, 0, 0, 0, NULL
177     }
178 };
179 
180 MALLOC_DECLARE(M_BXE_ILT);
181 MALLOC_DEFINE(M_BXE_ILT, "bxe_ilt", "bxe ILT pointer");
182 
183 /*
184  * FreeBSD device entry points.
185  */
186 static int bxe_probe(device_t);
187 static int bxe_attach(device_t);
188 static int bxe_detach(device_t);
189 static int bxe_shutdown(device_t);
190 
191 /*
192  * FreeBSD KLD module/device interface event handler method.
193  */
194 static device_method_t bxe_methods[] = {
195     /* Device interface (device_if.h) */
196     DEVMETHOD(device_probe,     bxe_probe),
197     DEVMETHOD(device_attach,    bxe_attach),
198     DEVMETHOD(device_detach,    bxe_detach),
199     DEVMETHOD(device_shutdown,  bxe_shutdown),
200     /* Bus interface (bus_if.h) */
201     DEVMETHOD(bus_print_child,  bus_generic_print_child),
202     DEVMETHOD(bus_driver_added, bus_generic_driver_added),
203     KOBJMETHOD_END
204 };
205 
206 /*
207  * FreeBSD KLD Module data declaration
208  */
209 static driver_t bxe_driver = {
210     "bxe",                   /* module name */
211     bxe_methods,             /* event handler */
212     sizeof(struct bxe_softc) /* extra data */
213 };
214 
215 /*
216  * FreeBSD dev class is needed to manage dev instances and
217  * to associate with a bus type
218  */
219 static devclass_t bxe_devclass;
220 
221 MODULE_DEPEND(bxe, pci, 1, 1, 1);
222 MODULE_DEPEND(bxe, ether, 1, 1, 1);
223 DRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
224 
225 /* resources needed for unloading a previously loaded device */
226 
227 #define BXE_PREV_WAIT_NEEDED 1
228 struct mtx bxe_prev_mtx;
229 MTX_SYSINIT(bxe_prev_mtx, &bxe_prev_mtx, "bxe_prev_lock", MTX_DEF);
230 struct bxe_prev_list_node {
231     LIST_ENTRY(bxe_prev_list_node) node;
232     uint8_t bus;
233     uint8_t slot;
234     uint8_t path;
235     uint8_t aer; /* XXX automatic error recovery */
236     uint8_t undi;
237 };
238 static LIST_HEAD(, bxe_prev_list_node) bxe_prev_list = LIST_HEAD_INITIALIZER(bxe_prev_list);
239 
240 static int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
241 
242 /* Tunable device values... */
243 
244 SYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
245 
246 /* Debug */
247 unsigned long bxe_debug = 0;
248 SYSCTL_ULONG(_hw_bxe, OID_AUTO, debug, CTLFLAG_RDTUN,
249              &bxe_debug, 0, "Debug logging mode");
250 
251 /* Interrupt Mode: 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
252 static int bxe_interrupt_mode = INTR_MODE_MSIX;
253 SYSCTL_INT(_hw_bxe, OID_AUTO, interrupt_mode, CTLFLAG_RDTUN,
254            &bxe_interrupt_mode, 0, "Interrupt (MSI-X/MSI/INTx) mode");
255 
256 /* Number of Queues: 0 (Auto) or 1 to 16 (fixed queue number) */
257 static int bxe_queue_count = 4;
258 SYSCTL_INT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN,
259            &bxe_queue_count, 0, "Multi-Queue queue count");
260 
261 /* max number of buffers per queue (default RX_BD_USABLE) */
262 static int bxe_max_rx_bufs = 0;
263 SYSCTL_INT(_hw_bxe, OID_AUTO, max_rx_bufs, CTLFLAG_RDTUN,
264            &bxe_max_rx_bufs, 0, "Maximum Number of Rx Buffers Per Queue");
265 
266 /* Host interrupt coalescing RX tick timer (usecs) */
267 static int bxe_hc_rx_ticks = 25;
268 SYSCTL_INT(_hw_bxe, OID_AUTO, hc_rx_ticks, CTLFLAG_RDTUN,
269            &bxe_hc_rx_ticks, 0, "Host Coalescing Rx ticks");
270 
271 /* Host interrupt coalescing TX tick timer (usecs) */
272 static int bxe_hc_tx_ticks = 50;
273 SYSCTL_INT(_hw_bxe, OID_AUTO, hc_tx_ticks, CTLFLAG_RDTUN,
274            &bxe_hc_tx_ticks, 0, "Host Coalescing Tx ticks");
275 
276 /* Maximum number of Rx packets to process at a time */
277 static int bxe_rx_budget = 0xffffffff;
278 SYSCTL_INT(_hw_bxe, OID_AUTO, rx_budget, CTLFLAG_TUN,
279            &bxe_rx_budget, 0, "Rx processing budget");
280 
281 /* Maximum LRO aggregation size */
282 static int bxe_max_aggregation_size = 0;
283 SYSCTL_INT(_hw_bxe, OID_AUTO, max_aggregation_size, CTLFLAG_TUN,
284            &bxe_max_aggregation_size, 0, "max aggregation size");
285 
286 /* PCI MRRS: -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB) */
287 static int bxe_mrrs = -1;
288 SYSCTL_INT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN,
289            &bxe_mrrs, 0, "PCIe maximum read request size");
290 
291 /* AutoGrEEEn: 0 (hardware default), 1 (force on), 2 (force off) */
292 static int bxe_autogreeen = 0;
293 SYSCTL_INT(_hw_bxe, OID_AUTO, autogreeen, CTLFLAG_RDTUN,
294            &bxe_autogreeen, 0, "AutoGrEEEn support");
295 
296 /* 4-tuple RSS support for UDP: 0 (disabled), 1 (enabled) */
297 static int bxe_udp_rss = 0;
298 SYSCTL_INT(_hw_bxe, OID_AUTO, udp_rss, CTLFLAG_RDTUN,
299            &bxe_udp_rss, 0, "UDP RSS support");
300 
301 
302 #define STAT_NAME_LEN 32 /* no stat names below can be longer than this */
303 
304 #define STATS_OFFSET32(stat_name)                   \
305     (offsetof(struct bxe_eth_stats, stat_name) / 4)
306 
307 #define Q_STATS_OFFSET32(stat_name)                   \
308     (offsetof(struct bxe_eth_q_stats, stat_name) / 4)
309 
310 static const struct {
311     uint32_t offset;
312     uint32_t size;
313     uint32_t flags;
314 #define STATS_FLAGS_PORT  1
315 #define STATS_FLAGS_FUNC  2 /* MF only cares about function stats */
316 #define STATS_FLAGS_BOTH  (STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
317     char string[STAT_NAME_LEN];
318 } bxe_eth_stats_arr[] = {
319     { STATS_OFFSET32(total_bytes_received_hi),
320                 8, STATS_FLAGS_BOTH, "rx_bytes" },
321     { STATS_OFFSET32(error_bytes_received_hi),
322                 8, STATS_FLAGS_BOTH, "rx_error_bytes" },
323     { STATS_OFFSET32(total_unicast_packets_received_hi),
324                 8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
325     { STATS_OFFSET32(total_multicast_packets_received_hi),
326                 8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
327     { STATS_OFFSET32(total_broadcast_packets_received_hi),
328                 8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
329     { STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
330                 8, STATS_FLAGS_PORT, "rx_crc_errors" },
331     { STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
332                 8, STATS_FLAGS_PORT, "rx_align_errors" },
333     { STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
334                 8, STATS_FLAGS_PORT, "rx_undersize_packets" },
335     { STATS_OFFSET32(etherstatsoverrsizepkts_hi),
336                 8, STATS_FLAGS_PORT, "rx_oversize_packets" },
337     { STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
338                 8, STATS_FLAGS_PORT, "rx_fragments" },
339     { STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
340                 8, STATS_FLAGS_PORT, "rx_jabbers" },
341     { STATS_OFFSET32(no_buff_discard_hi),
342                 8, STATS_FLAGS_BOTH, "rx_discards" },
343     { STATS_OFFSET32(mac_filter_discard),
344                 4, STATS_FLAGS_PORT, "rx_filtered_packets" },
345     { STATS_OFFSET32(mf_tag_discard),
346                 4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
347     { STATS_OFFSET32(pfc_frames_received_hi),
348                 8, STATS_FLAGS_PORT, "pfc_frames_received" },
349     { STATS_OFFSET32(pfc_frames_sent_hi),
350                 8, STATS_FLAGS_PORT, "pfc_frames_sent" },
351     { STATS_OFFSET32(brb_drop_hi),
352                 8, STATS_FLAGS_PORT, "rx_brb_discard" },
353     { STATS_OFFSET32(brb_truncate_hi),
354                 8, STATS_FLAGS_PORT, "rx_brb_truncate" },
355     { STATS_OFFSET32(pause_frames_received_hi),
356                 8, STATS_FLAGS_PORT, "rx_pause_frames" },
357     { STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
358                 8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
359     { STATS_OFFSET32(nig_timer_max),
360                 4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
361     { STATS_OFFSET32(total_bytes_transmitted_hi),
362                 8, STATS_FLAGS_BOTH, "tx_bytes" },
363     { STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
364                 8, STATS_FLAGS_PORT, "tx_error_bytes" },
365     { STATS_OFFSET32(total_unicast_packets_transmitted_hi),
366                 8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
367     { STATS_OFFSET32(total_multicast_packets_transmitted_hi),
368                 8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
369     { STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
370                 8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
371     { STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
372                 8, STATS_FLAGS_PORT, "tx_mac_errors" },
373     { STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
374                 8, STATS_FLAGS_PORT, "tx_carrier_errors" },
375     { STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
376                 8, STATS_FLAGS_PORT, "tx_single_collisions" },
377     { STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
378                 8, STATS_FLAGS_PORT, "tx_multi_collisions" },
379     { STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
380                 8, STATS_FLAGS_PORT, "tx_deferred" },
381     { STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
382                 8, STATS_FLAGS_PORT, "tx_excess_collisions" },
383     { STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
384                 8, STATS_FLAGS_PORT, "tx_late_collisions" },
385     { STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
386                 8, STATS_FLAGS_PORT, "tx_total_collisions" },
387     { STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
388                 8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
389     { STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
390                 8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
391     { STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
392                 8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
393     { STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
394                 8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
395     { STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
396                 8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
397     { STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
398                 8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
399     { STATS_OFFSET32(etherstatspktsover1522octets_hi),
400                 8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
401     { STATS_OFFSET32(pause_frames_sent_hi),
402                 8, STATS_FLAGS_PORT, "tx_pause_frames" },
403     { STATS_OFFSET32(total_tpa_aggregations_hi),
404                 8, STATS_FLAGS_FUNC, "tpa_aggregations" },
405     { STATS_OFFSET32(total_tpa_aggregated_frames_hi),
406                 8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
407     { STATS_OFFSET32(total_tpa_bytes_hi),
408                 8, STATS_FLAGS_FUNC, "tpa_bytes"},
409     { STATS_OFFSET32(eee_tx_lpi),
410                 4, STATS_FLAGS_PORT, "eee_tx_lpi"},
411     { STATS_OFFSET32(rx_calls),
412                 4, STATS_FLAGS_FUNC, "rx_calls"},
413     { STATS_OFFSET32(rx_pkts),
414                 4, STATS_FLAGS_FUNC, "rx_pkts"},
415     { STATS_OFFSET32(rx_tpa_pkts),
416                 4, STATS_FLAGS_FUNC, "rx_tpa_pkts"},
417     { STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
418                 4, STATS_FLAGS_FUNC, "rx_erroneous_jumbo_sge_pkts"},
419     { STATS_OFFSET32(rx_bxe_service_rxsgl),
420                 4, STATS_FLAGS_FUNC, "rx_bxe_service_rxsgl"},
421     { STATS_OFFSET32(rx_jumbo_sge_pkts),
422                 4, STATS_FLAGS_FUNC, "rx_jumbo_sge_pkts"},
423     { STATS_OFFSET32(rx_soft_errors),
424                 4, STATS_FLAGS_FUNC, "rx_soft_errors"},
425     { STATS_OFFSET32(rx_hw_csum_errors),
426                 4, STATS_FLAGS_FUNC, "rx_hw_csum_errors"},
427     { STATS_OFFSET32(rx_ofld_frames_csum_ip),
428                 4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_ip"},
429     { STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
430                 4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_tcp_udp"},
431     { STATS_OFFSET32(rx_budget_reached),
432                 4, STATS_FLAGS_FUNC, "rx_budget_reached"},
433     { STATS_OFFSET32(tx_pkts),
434                 4, STATS_FLAGS_FUNC, "tx_pkts"},
435     { STATS_OFFSET32(tx_soft_errors),
436                 4, STATS_FLAGS_FUNC, "tx_soft_errors"},
437     { STATS_OFFSET32(tx_ofld_frames_csum_ip),
438                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_ip"},
439     { STATS_OFFSET32(tx_ofld_frames_csum_tcp),
440                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_tcp"},
441     { STATS_OFFSET32(tx_ofld_frames_csum_udp),
442                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_udp"},
443     { STATS_OFFSET32(tx_ofld_frames_lso),
444                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso"},
445     { STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
446                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso_hdr_splits"},
447     { STATS_OFFSET32(tx_encap_failures),
448                 4, STATS_FLAGS_FUNC, "tx_encap_failures"},
449     { STATS_OFFSET32(tx_hw_queue_full),
450                 4, STATS_FLAGS_FUNC, "tx_hw_queue_full"},
451     { STATS_OFFSET32(tx_hw_max_queue_depth),
452                 4, STATS_FLAGS_FUNC, "tx_hw_max_queue_depth"},
453     { STATS_OFFSET32(tx_dma_mapping_failure),
454                 4, STATS_FLAGS_FUNC, "tx_dma_mapping_failure"},
455     { STATS_OFFSET32(tx_max_drbr_queue_depth),
456                 4, STATS_FLAGS_FUNC, "tx_max_drbr_queue_depth"},
457     { STATS_OFFSET32(tx_window_violation_std),
458                 4, STATS_FLAGS_FUNC, "tx_window_violation_std"},
459     { STATS_OFFSET32(tx_window_violation_tso),
460                 4, STATS_FLAGS_FUNC, "tx_window_violation_tso"},
461     { STATS_OFFSET32(tx_chain_lost_mbuf),
462                 4, STATS_FLAGS_FUNC, "tx_chain_lost_mbuf"},
463     { STATS_OFFSET32(tx_frames_deferred),
464                 4, STATS_FLAGS_FUNC, "tx_frames_deferred"},
465     { STATS_OFFSET32(tx_queue_xoff),
466                 4, STATS_FLAGS_FUNC, "tx_queue_xoff"},
467     { STATS_OFFSET32(mbuf_defrag_attempts),
468                 4, STATS_FLAGS_FUNC, "mbuf_defrag_attempts"},
469     { STATS_OFFSET32(mbuf_defrag_failures),
470                 4, STATS_FLAGS_FUNC, "mbuf_defrag_failures"},
471     { STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
472                 4, STATS_FLAGS_FUNC, "mbuf_rx_bd_alloc_failed"},
473     { STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
474                 4, STATS_FLAGS_FUNC, "mbuf_rx_bd_mapping_failed"},
475     { STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
476                 4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_alloc_failed"},
477     { STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
478                 4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_mapping_failed"},
479     { STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
480                 4, STATS_FLAGS_FUNC, "mbuf_rx_sge_alloc_failed"},
481     { STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
482                 4, STATS_FLAGS_FUNC, "mbuf_rx_sge_mapping_failed"},
483     { STATS_OFFSET32(mbuf_alloc_tx),
484                 4, STATS_FLAGS_FUNC, "mbuf_alloc_tx"},
485     { STATS_OFFSET32(mbuf_alloc_rx),
486                 4, STATS_FLAGS_FUNC, "mbuf_alloc_rx"},
487     { STATS_OFFSET32(mbuf_alloc_sge),
488                 4, STATS_FLAGS_FUNC, "mbuf_alloc_sge"},
489     { STATS_OFFSET32(mbuf_alloc_tpa),
490                 4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"}
491 };
492 
493 static const struct {
494     uint32_t offset;
495     uint32_t size;
496     char string[STAT_NAME_LEN];
497 } bxe_eth_q_stats_arr[] = {
498     { Q_STATS_OFFSET32(total_bytes_received_hi),
499                 8, "rx_bytes" },
500     { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
501                 8, "rx_ucast_packets" },
502     { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
503                 8, "rx_mcast_packets" },
504     { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
505                 8, "rx_bcast_packets" },
506     { Q_STATS_OFFSET32(no_buff_discard_hi),
507                 8, "rx_discards" },
508     { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
509                 8, "tx_bytes" },
510     { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
511                 8, "tx_ucast_packets" },
512     { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
513                 8, "tx_mcast_packets" },
514     { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
515                 8, "tx_bcast_packets" },
516     { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
517                 8, "tpa_aggregations" },
518     { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
519                 8, "tpa_aggregated_frames"},
520     { Q_STATS_OFFSET32(total_tpa_bytes_hi),
521                 8, "tpa_bytes"},
522     { Q_STATS_OFFSET32(rx_calls),
523                 4, "rx_calls"},
524     { Q_STATS_OFFSET32(rx_pkts),
525                 4, "rx_pkts"},
526     { Q_STATS_OFFSET32(rx_tpa_pkts),
527                 4, "rx_tpa_pkts"},
528     { Q_STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
529                 4, "rx_erroneous_jumbo_sge_pkts"},
530     { Q_STATS_OFFSET32(rx_bxe_service_rxsgl),
531                 4, "rx_bxe_service_rxsgl"},
532     { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
533                 4, "rx_jumbo_sge_pkts"},
534     { Q_STATS_OFFSET32(rx_soft_errors),
535                 4, "rx_soft_errors"},
536     { Q_STATS_OFFSET32(rx_hw_csum_errors),
537                 4, "rx_hw_csum_errors"},
538     { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
539                 4, "rx_ofld_frames_csum_ip"},
540     { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
541                 4, "rx_ofld_frames_csum_tcp_udp"},
542     { Q_STATS_OFFSET32(rx_budget_reached),
543                 4, "rx_budget_reached"},
544     { Q_STATS_OFFSET32(tx_pkts),
545                 4, "tx_pkts"},
546     { Q_STATS_OFFSET32(tx_soft_errors),
547                 4, "tx_soft_errors"},
548     { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
549                 4, "tx_ofld_frames_csum_ip"},
550     { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
551                 4, "tx_ofld_frames_csum_tcp"},
552     { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
553                 4, "tx_ofld_frames_csum_udp"},
554     { Q_STATS_OFFSET32(tx_ofld_frames_lso),
555                 4, "tx_ofld_frames_lso"},
556     { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
557                 4, "tx_ofld_frames_lso_hdr_splits"},
558     { Q_STATS_OFFSET32(tx_encap_failures),
559                 4, "tx_encap_failures"},
560     { Q_STATS_OFFSET32(tx_hw_queue_full),
561                 4, "tx_hw_queue_full"},
562     { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
563                 4, "tx_hw_max_queue_depth"},
564     { Q_STATS_OFFSET32(tx_dma_mapping_failure),
565                 4, "tx_dma_mapping_failure"},
566     { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
567                 4, "tx_max_drbr_queue_depth"},
568     { Q_STATS_OFFSET32(tx_window_violation_std),
569                 4, "tx_window_violation_std"},
570     { Q_STATS_OFFSET32(tx_window_violation_tso),
571                 4, "tx_window_violation_tso"},
572     { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
573                 4, "tx_chain_lost_mbuf"},
574     { Q_STATS_OFFSET32(tx_frames_deferred),
575                 4, "tx_frames_deferred"},
576     { Q_STATS_OFFSET32(tx_queue_xoff),
577                 4, "tx_queue_xoff"},
578     { Q_STATS_OFFSET32(mbuf_defrag_attempts),
579                 4, "mbuf_defrag_attempts"},
580     { Q_STATS_OFFSET32(mbuf_defrag_failures),
581                 4, "mbuf_defrag_failures"},
582     { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
583                 4, "mbuf_rx_bd_alloc_failed"},
584     { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
585                 4, "mbuf_rx_bd_mapping_failed"},
586     { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
587                 4, "mbuf_rx_tpa_alloc_failed"},
588     { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
589                 4, "mbuf_rx_tpa_mapping_failed"},
590     { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
591                 4, "mbuf_rx_sge_alloc_failed"},
592     { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
593                 4, "mbuf_rx_sge_mapping_failed"},
594     { Q_STATS_OFFSET32(mbuf_alloc_tx),
595                 4, "mbuf_alloc_tx"},
596     { Q_STATS_OFFSET32(mbuf_alloc_rx),
597                 4, "mbuf_alloc_rx"},
598     { Q_STATS_OFFSET32(mbuf_alloc_sge),
599                 4, "mbuf_alloc_sge"},
600     { Q_STATS_OFFSET32(mbuf_alloc_tpa),
601                 4, "mbuf_alloc_tpa"}
602 };
603 
604 #define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
605 #define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
606 
607 
608 static void    bxe_cmng_fns_init(struct bxe_softc *sc,
609                                  uint8_t          read_cfg,
610                                  uint8_t          cmng_type);
611 static int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
612 static void    storm_memset_cmng(struct bxe_softc *sc,
613                                  struct cmng_init *cmng,
614                                  uint8_t          port);
615 static void    bxe_set_reset_global(struct bxe_softc *sc);
616 static void    bxe_set_reset_in_progress(struct bxe_softc *sc);
617 static uint8_t bxe_reset_is_done(struct bxe_softc *sc,
618                                  int              engine);
619 static uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
620 static uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
621                                    uint8_t          *global,
622                                    uint8_t          print);
623 static void    bxe_int_disable(struct bxe_softc *sc);
624 static int     bxe_release_leader_lock(struct bxe_softc *sc);
625 static void    bxe_pf_disable(struct bxe_softc *sc);
626 static void    bxe_free_fp_buffers(struct bxe_softc *sc);
627 static inline void bxe_update_rx_prod(struct bxe_softc    *sc,
628                                       struct bxe_fastpath *fp,
629                                       uint16_t            rx_bd_prod,
630                                       uint16_t            rx_cq_prod,
631                                       uint16_t            rx_sge_prod);
632 static void    bxe_link_report_locked(struct bxe_softc *sc);
633 static void    bxe_link_report(struct bxe_softc *sc);
634 static void    bxe_link_status_update(struct bxe_softc *sc);
635 static void    bxe_periodic_callout_func(void *xsc);
636 static void    bxe_periodic_start(struct bxe_softc *sc);
637 static void    bxe_periodic_stop(struct bxe_softc *sc);
638 static int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
639                                     uint16_t prev_index,
640                                     uint16_t index);
641 static int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
642                                      int                 queue);
643 static int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
644                                      uint16_t            index);
645 static uint8_t bxe_txeof(struct bxe_softc *sc,
646                          struct bxe_fastpath *fp);
647 static void    bxe_task_fp(struct bxe_fastpath *fp);
648 static __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
649                                      struct mbuf      *m,
650                                      uint8_t          contents);
651 static int     bxe_alloc_mem(struct bxe_softc *sc);
652 static void    bxe_free_mem(struct bxe_softc *sc);
653 static int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
654 static void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
655 static int     bxe_interrupt_attach(struct bxe_softc *sc);
656 static void    bxe_interrupt_detach(struct bxe_softc *sc);
657 static void    bxe_set_rx_mode(struct bxe_softc *sc);
658 static int     bxe_init_locked(struct bxe_softc *sc);
659 static int     bxe_stop_locked(struct bxe_softc *sc);
660 static __noinline int bxe_nic_load(struct bxe_softc *sc,
661                                    int              load_mode);
662 static __noinline int bxe_nic_unload(struct bxe_softc *sc,
663                                      uint32_t         unload_mode,
664                                      uint8_t          keep_link);
665 
666 static void bxe_handle_sp_tq(void *context, int pending);
667 static void bxe_handle_fp_tq(void *context, int pending);
668 
669 static int bxe_add_cdev(struct bxe_softc *sc);
670 static void bxe_del_cdev(struct bxe_softc *sc);
671 static int bxe_grc_dump(struct bxe_softc *sc);
672 static int bxe_alloc_buf_rings(struct bxe_softc *sc);
673 static void bxe_free_buf_rings(struct bxe_softc *sc);
674 
675 /* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
676 uint32_t
677 calc_crc32(uint8_t  *crc32_packet,
678            uint32_t crc32_length,
679            uint32_t crc32_seed,
680            uint8_t  complement)
681 {
682    uint32_t byte         = 0;
683    uint32_t bit          = 0;
684    uint8_t  msb          = 0;
685    uint32_t temp         = 0;
686    uint32_t shft         = 0;
687    uint8_t  current_byte = 0;
688    uint32_t crc32_result = crc32_seed;
689    const uint32_t CRC32_POLY = 0x1edc6f41;
690 
691    if ((crc32_packet == NULL) ||
692        (crc32_length == 0) ||
693        ((crc32_length % 8) != 0))
694     {
695         return (crc32_result);
696     }
697 
698     for (byte = 0; byte < crc32_length; byte = byte + 1)
699     {
700         current_byte = crc32_packet[byte];
701         for (bit = 0; bit < 8; bit = bit + 1)
702         {
703             /* msb = crc32_result[31]; */
704             msb = (uint8_t)(crc32_result >> 31);
705 
706             crc32_result = crc32_result << 1;
707 
708             /* it (msb != current_byte[bit]) */
709             if (msb != (0x1 & (current_byte >> bit)))
710             {
711                 crc32_result = crc32_result ^ CRC32_POLY;
712                 /* crc32_result[0] = 1 */
713                 crc32_result |= 1;
714             }
715         }
716     }
717 
718     /* Last step is to:
719      * 1. "mirror" every bit
720      * 2. swap the 4 bytes
721      * 3. complement each bit
722      */
723 
724     /* Mirror */
725     temp = crc32_result;
726     shft = sizeof(crc32_result) * 8 - 1;
727 
728     for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
729     {
730         temp <<= 1;
731         temp |= crc32_result & 1;
732         shft-- ;
733     }
734 
735     /* temp[31-bit] = crc32_result[bit] */
736     temp <<= shft;
737 
738     /* Swap */
739     /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
740     {
741         uint32_t t0, t1, t2, t3;
742         t0 = (0x000000ff & (temp >> 24));
743         t1 = (0x0000ff00 & (temp >> 8));
744         t2 = (0x00ff0000 & (temp << 8));
745         t3 = (0xff000000 & (temp << 24));
746         crc32_result = t0 | t1 | t2 | t3;
747     }
748 
749     /* Complement */
750     if (complement)
751     {
752         crc32_result = ~crc32_result;
753     }
754 
755     return (crc32_result);
756 }
757 
758 int
759 bxe_test_bit(int                    nr,
760              volatile unsigned long *addr)
761 {
762     return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
763 }
764 
765 void
766 bxe_set_bit(unsigned int           nr,
767             volatile unsigned long *addr)
768 {
769     atomic_set_acq_long(addr, (1 << nr));
770 }
771 
772 void
773 bxe_clear_bit(int                    nr,
774               volatile unsigned long *addr)
775 {
776     atomic_clear_acq_long(addr, (1 << nr));
777 }
778 
779 int
780 bxe_test_and_set_bit(int                    nr,
781                        volatile unsigned long *addr)
782 {
783     unsigned long x;
784     nr = (1 << nr);
785     do {
786         x = *addr;
787     } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
788     // if (x & nr) bit_was_set; else bit_was_not_set;
789     return (x & nr);
790 }
791 
792 int
793 bxe_test_and_clear_bit(int                    nr,
794                        volatile unsigned long *addr)
795 {
796     unsigned long x;
797     nr = (1 << nr);
798     do {
799         x = *addr;
800     } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
801     // if (x & nr) bit_was_set; else bit_was_not_set;
802     return (x & nr);
803 }
804 
805 int
806 bxe_cmpxchg(volatile int *addr,
807             int          old,
808             int          new)
809 {
810     int x;
811     do {
812         x = *addr;
813     } while (atomic_cmpset_acq_int(addr, old, new) == 0);
814     return (x);
815 }
816 
817 /*
818  * Get DMA memory from the OS.
819  *
820  * Validates that the OS has provided DMA buffers in response to a
821  * bus_dmamap_load call and saves the physical address of those buffers.
822  * When the callback is used the OS will return 0 for the mapping function
823  * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
824  * failures back to the caller.
825  *
826  * Returns:
827  *   Nothing.
828  */
829 static void
830 bxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
831 {
832     struct bxe_dma *dma = arg;
833 
834     if (error) {
835         dma->paddr = 0;
836         dma->nseg  = 0;
837         BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
838     } else {
839         dma->paddr = segs->ds_addr;
840         dma->nseg  = nseg;
841     }
842 }
843 
844 /*
845  * Allocate a block of memory and map it for DMA. No partial completions
846  * allowed and release any resources acquired if we can't acquire all
847  * resources.
848  *
849  * Returns:
850  *   0 = Success, !0 = Failure
851  */
852 int
853 bxe_dma_alloc(struct bxe_softc *sc,
854               bus_size_t       size,
855               struct bxe_dma   *dma,
856               const char       *msg)
857 {
858     int rc;
859 
860     if (dma->size > 0) {
861         BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
862               (unsigned long)dma->size);
863         return (1);
864     }
865 
866     memset(dma, 0, sizeof(*dma)); /* sanity */
867     dma->sc   = sc;
868     dma->size = size;
869     snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
870 
871     rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
872                             BCM_PAGE_SIZE,      /* alignment */
873                             0,                  /* boundary limit */
874                             BUS_SPACE_MAXADDR,  /* restricted low */
875                             BUS_SPACE_MAXADDR,  /* restricted hi */
876                             NULL,               /* addr filter() */
877                             NULL,               /* addr filter() arg */
878                             size,               /* max map size */
879                             1,                  /* num discontinuous */
880                             size,               /* max seg size */
881                             BUS_DMA_ALLOCNOW,   /* flags */
882                             NULL,               /* lock() */
883                             NULL,               /* lock() arg */
884                             &dma->tag);         /* returned dma tag */
885     if (rc != 0) {
886         BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
887         memset(dma, 0, sizeof(*dma));
888         return (1);
889     }
890 
891     rc = bus_dmamem_alloc(dma->tag,
892                           (void **)&dma->vaddr,
893                           (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
894                           &dma->map);
895     if (rc != 0) {
896         BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
897         bus_dma_tag_destroy(dma->tag);
898         memset(dma, 0, sizeof(*dma));
899         return (1);
900     }
901 
902     rc = bus_dmamap_load(dma->tag,
903                          dma->map,
904                          dma->vaddr,
905                          size,
906                          bxe_dma_map_addr, /* BLOGD in here */
907                          dma,
908                          BUS_DMA_NOWAIT);
909     if (rc != 0) {
910         BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
911         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
912         bus_dma_tag_destroy(dma->tag);
913         memset(dma, 0, sizeof(*dma));
914         return (1);
915     }
916 
917     return (0);
918 }
919 
920 void
921 bxe_dma_free(struct bxe_softc *sc,
922              struct bxe_dma   *dma)
923 {
924     if (dma->size > 0) {
925         DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
926 
927         bus_dmamap_sync(dma->tag, dma->map,
928                         (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
929         bus_dmamap_unload(dma->tag, dma->map);
930         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
931         bus_dma_tag_destroy(dma->tag);
932     }
933 
934     memset(dma, 0, sizeof(*dma));
935 }
936 
937 /*
938  * These indirect read and write routines are only during init.
939  * The locking is handled by the MCP.
940  */
941 
942 void
943 bxe_reg_wr_ind(struct bxe_softc *sc,
944                uint32_t         addr,
945                uint32_t         val)
946 {
947     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
948     pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
949     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
950 }
951 
952 uint32_t
953 bxe_reg_rd_ind(struct bxe_softc *sc,
954                uint32_t         addr)
955 {
956     uint32_t val;
957 
958     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
959     val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
960     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
961 
962     return (val);
963 }
964 
965 static int
966 bxe_acquire_hw_lock(struct bxe_softc *sc,
967                     uint32_t         resource)
968 {
969     uint32_t lock_status;
970     uint32_t resource_bit = (1 << resource);
971     int func = SC_FUNC(sc);
972     uint32_t hw_lock_control_reg;
973     int cnt;
974 
975     /* validate the resource is within range */
976     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
977         BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
978             " resource_bit 0x%x\n", resource, resource_bit);
979         return (-1);
980     }
981 
982     if (func <= 5) {
983         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
984     } else {
985         hw_lock_control_reg =
986                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
987     }
988 
989     /* validate the resource is not already taken */
990     lock_status = REG_RD(sc, hw_lock_control_reg);
991     if (lock_status & resource_bit) {
992         BLOGE(sc, "resource (0x%x) in use (status 0x%x bit 0x%x)\n",
993               resource, lock_status, resource_bit);
994         return (-1);
995     }
996 
997     /* try every 5ms for 5 seconds */
998     for (cnt = 0; cnt < 1000; cnt++) {
999         REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1000         lock_status = REG_RD(sc, hw_lock_control_reg);
1001         if (lock_status & resource_bit) {
1002             return (0);
1003         }
1004         DELAY(5000);
1005     }
1006 
1007     BLOGE(sc, "Resource 0x%x resource_bit 0x%x lock timeout!\n",
1008         resource, resource_bit);
1009     return (-1);
1010 }
1011 
1012 static int
1013 bxe_release_hw_lock(struct bxe_softc *sc,
1014                     uint32_t         resource)
1015 {
1016     uint32_t lock_status;
1017     uint32_t resource_bit = (1 << resource);
1018     int func = SC_FUNC(sc);
1019     uint32_t hw_lock_control_reg;
1020 
1021     /* validate the resource is within range */
1022     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1023         BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1024             " resource_bit 0x%x\n", resource, resource_bit);
1025         return (-1);
1026     }
1027 
1028     if (func <= 5) {
1029         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1030     } else {
1031         hw_lock_control_reg =
1032                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1033     }
1034 
1035     /* validate the resource is currently taken */
1036     lock_status = REG_RD(sc, hw_lock_control_reg);
1037     if (!(lock_status & resource_bit)) {
1038         BLOGE(sc, "resource (0x%x) not in use (status 0x%x bit 0x%x)\n",
1039               resource, lock_status, resource_bit);
1040         return (-1);
1041     }
1042 
1043     REG_WR(sc, hw_lock_control_reg, resource_bit);
1044     return (0);
1045 }
1046 static void bxe_acquire_phy_lock(struct bxe_softc *sc)
1047 {
1048 	BXE_PHY_LOCK(sc);
1049 	bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1050 }
1051 
1052 static void bxe_release_phy_lock(struct bxe_softc *sc)
1053 {
1054 	bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1055 	BXE_PHY_UNLOCK(sc);
1056 }
1057 /*
1058  * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1059  * had we done things the other way around, if two pfs from the same port
1060  * would attempt to access nvram at the same time, we could run into a
1061  * scenario such as:
1062  * pf A takes the port lock.
1063  * pf B succeeds in taking the same lock since they are from the same port.
1064  * pf A takes the per pf misc lock. Performs eeprom access.
1065  * pf A finishes. Unlocks the per pf misc lock.
1066  * Pf B takes the lock and proceeds to perform it's own access.
1067  * pf A unlocks the per port lock, while pf B is still working (!).
1068  * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1069  * access corrupted by pf B).*
1070  */
1071 static int
1072 bxe_acquire_nvram_lock(struct bxe_softc *sc)
1073 {
1074     int port = SC_PORT(sc);
1075     int count, i;
1076     uint32_t val = 0;
1077 
1078     /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1079     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1080 
1081     /* adjust timeout for emulation/FPGA */
1082     count = NVRAM_TIMEOUT_COUNT;
1083     if (CHIP_REV_IS_SLOW(sc)) {
1084         count *= 100;
1085     }
1086 
1087     /* request access to nvram interface */
1088     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1089            (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1090 
1091     for (i = 0; i < count*10; i++) {
1092         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1093         if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1094             break;
1095         }
1096 
1097         DELAY(5);
1098     }
1099 
1100     if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1101         BLOGE(sc, "Cannot get access to nvram interface "
1102             "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1103             port, val);
1104         return (-1);
1105     }
1106 
1107     return (0);
1108 }
1109 
1110 static int
1111 bxe_release_nvram_lock(struct bxe_softc *sc)
1112 {
1113     int port = SC_PORT(sc);
1114     int count, i;
1115     uint32_t val = 0;
1116 
1117     /* adjust timeout for emulation/FPGA */
1118     count = NVRAM_TIMEOUT_COUNT;
1119     if (CHIP_REV_IS_SLOW(sc)) {
1120         count *= 100;
1121     }
1122 
1123     /* relinquish nvram interface */
1124     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1125            (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1126 
1127     for (i = 0; i < count*10; i++) {
1128         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1129         if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1130             break;
1131         }
1132 
1133         DELAY(5);
1134     }
1135 
1136     if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1137         BLOGE(sc, "Cannot free access to nvram interface "
1138             "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1139             port, val);
1140         return (-1);
1141     }
1142 
1143     /* release HW lock: protect against other PFs in PF Direct Assignment */
1144     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1145 
1146     return (0);
1147 }
1148 
1149 static void
1150 bxe_enable_nvram_access(struct bxe_softc *sc)
1151 {
1152     uint32_t val;
1153 
1154     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1155 
1156     /* enable both bits, even on read */
1157     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1158            (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1159 }
1160 
1161 static void
1162 bxe_disable_nvram_access(struct bxe_softc *sc)
1163 {
1164     uint32_t val;
1165 
1166     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1167 
1168     /* disable both bits, even after read */
1169     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1170            (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1171                     MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1172 }
1173 
1174 static int
1175 bxe_nvram_read_dword(struct bxe_softc *sc,
1176                      uint32_t         offset,
1177                      uint32_t         *ret_val,
1178                      uint32_t         cmd_flags)
1179 {
1180     int count, i, rc;
1181     uint32_t val;
1182 
1183     /* build the command word */
1184     cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1185 
1186     /* need to clear DONE bit separately */
1187     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1188 
1189     /* address of the NVRAM to read from */
1190     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1191            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1192 
1193     /* issue a read command */
1194     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1195 
1196     /* adjust timeout for emulation/FPGA */
1197     count = NVRAM_TIMEOUT_COUNT;
1198     if (CHIP_REV_IS_SLOW(sc)) {
1199         count *= 100;
1200     }
1201 
1202     /* wait for completion */
1203     *ret_val = 0;
1204     rc = -1;
1205     for (i = 0; i < count; i++) {
1206         DELAY(5);
1207         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1208 
1209         if (val & MCPR_NVM_COMMAND_DONE) {
1210             val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1211             /* we read nvram data in cpu order
1212              * but ethtool sees it as an array of bytes
1213              * converting to big-endian will do the work
1214              */
1215             *ret_val = htobe32(val);
1216             rc = 0;
1217             break;
1218         }
1219     }
1220 
1221     if (rc == -1) {
1222         BLOGE(sc, "nvram read timeout expired "
1223             "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1224             offset, cmd_flags, val);
1225     }
1226 
1227     return (rc);
1228 }
1229 
1230 static int
1231 bxe_nvram_read(struct bxe_softc *sc,
1232                uint32_t         offset,
1233                uint8_t          *ret_buf,
1234                int              buf_size)
1235 {
1236     uint32_t cmd_flags;
1237     uint32_t val;
1238     int rc;
1239 
1240     if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1241         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1242               offset, buf_size);
1243         return (-1);
1244     }
1245 
1246     if ((offset + buf_size) > sc->devinfo.flash_size) {
1247         BLOGE(sc, "Invalid parameter, "
1248                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1249               offset, buf_size, sc->devinfo.flash_size);
1250         return (-1);
1251     }
1252 
1253     /* request access to nvram interface */
1254     rc = bxe_acquire_nvram_lock(sc);
1255     if (rc) {
1256         return (rc);
1257     }
1258 
1259     /* enable access to nvram interface */
1260     bxe_enable_nvram_access(sc);
1261 
1262     /* read the first word(s) */
1263     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1264     while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1265         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1266         memcpy(ret_buf, &val, 4);
1267 
1268         /* advance to the next dword */
1269         offset += sizeof(uint32_t);
1270         ret_buf += sizeof(uint32_t);
1271         buf_size -= sizeof(uint32_t);
1272         cmd_flags = 0;
1273     }
1274 
1275     if (rc == 0) {
1276         cmd_flags |= MCPR_NVM_COMMAND_LAST;
1277         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1278         memcpy(ret_buf, &val, 4);
1279     }
1280 
1281     /* disable access to nvram interface */
1282     bxe_disable_nvram_access(sc);
1283     bxe_release_nvram_lock(sc);
1284 
1285     return (rc);
1286 }
1287 
1288 static int
1289 bxe_nvram_write_dword(struct bxe_softc *sc,
1290                       uint32_t         offset,
1291                       uint32_t         val,
1292                       uint32_t         cmd_flags)
1293 {
1294     int count, i, rc;
1295 
1296     /* build the command word */
1297     cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1298 
1299     /* need to clear DONE bit separately */
1300     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1301 
1302     /* write the data */
1303     REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1304 
1305     /* address of the NVRAM to write to */
1306     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1307            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1308 
1309     /* issue the write command */
1310     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1311 
1312     /* adjust timeout for emulation/FPGA */
1313     count = NVRAM_TIMEOUT_COUNT;
1314     if (CHIP_REV_IS_SLOW(sc)) {
1315         count *= 100;
1316     }
1317 
1318     /* wait for completion */
1319     rc = -1;
1320     for (i = 0; i < count; i++) {
1321         DELAY(5);
1322         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1323         if (val & MCPR_NVM_COMMAND_DONE) {
1324             rc = 0;
1325             break;
1326         }
1327     }
1328 
1329     if (rc == -1) {
1330         BLOGE(sc, "nvram write timeout expired "
1331             "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1332             offset, cmd_flags, val);
1333     }
1334 
1335     return (rc);
1336 }
1337 
1338 #define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1339 
1340 static int
1341 bxe_nvram_write1(struct bxe_softc *sc,
1342                  uint32_t         offset,
1343                  uint8_t          *data_buf,
1344                  int              buf_size)
1345 {
1346     uint32_t cmd_flags;
1347     uint32_t align_offset;
1348     uint32_t val;
1349     int rc;
1350 
1351     if ((offset + buf_size) > sc->devinfo.flash_size) {
1352         BLOGE(sc, "Invalid parameter, "
1353                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1354               offset, buf_size, sc->devinfo.flash_size);
1355         return (-1);
1356     }
1357 
1358     /* request access to nvram interface */
1359     rc = bxe_acquire_nvram_lock(sc);
1360     if (rc) {
1361         return (rc);
1362     }
1363 
1364     /* enable access to nvram interface */
1365     bxe_enable_nvram_access(sc);
1366 
1367     cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1368     align_offset = (offset & ~0x03);
1369     rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1370 
1371     if (rc == 0) {
1372         val &= ~(0xff << BYTE_OFFSET(offset));
1373         val |= (*data_buf << BYTE_OFFSET(offset));
1374 
1375         /* nvram data is returned as an array of bytes
1376          * convert it back to cpu order
1377          */
1378         val = be32toh(val);
1379 
1380         rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1381     }
1382 
1383     /* disable access to nvram interface */
1384     bxe_disable_nvram_access(sc);
1385     bxe_release_nvram_lock(sc);
1386 
1387     return (rc);
1388 }
1389 
1390 static int
1391 bxe_nvram_write(struct bxe_softc *sc,
1392                 uint32_t         offset,
1393                 uint8_t          *data_buf,
1394                 int              buf_size)
1395 {
1396     uint32_t cmd_flags;
1397     uint32_t val;
1398     uint32_t written_so_far;
1399     int rc;
1400 
1401     if (buf_size == 1) {
1402         return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1403     }
1404 
1405     if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1406         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1407               offset, buf_size);
1408         return (-1);
1409     }
1410 
1411     if (buf_size == 0) {
1412         return (0); /* nothing to do */
1413     }
1414 
1415     if ((offset + buf_size) > sc->devinfo.flash_size) {
1416         BLOGE(sc, "Invalid parameter, "
1417                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1418               offset, buf_size, sc->devinfo.flash_size);
1419         return (-1);
1420     }
1421 
1422     /* request access to nvram interface */
1423     rc = bxe_acquire_nvram_lock(sc);
1424     if (rc) {
1425         return (rc);
1426     }
1427 
1428     /* enable access to nvram interface */
1429     bxe_enable_nvram_access(sc);
1430 
1431     written_so_far = 0;
1432     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1433     while ((written_so_far < buf_size) && (rc == 0)) {
1434         if (written_so_far == (buf_size - sizeof(uint32_t))) {
1435             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1436         } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1437             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1438         } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1439             cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1440         }
1441 
1442         memcpy(&val, data_buf, 4);
1443 
1444         rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1445 
1446         /* advance to the next dword */
1447         offset += sizeof(uint32_t);
1448         data_buf += sizeof(uint32_t);
1449         written_so_far += sizeof(uint32_t);
1450         cmd_flags = 0;
1451     }
1452 
1453     /* disable access to nvram interface */
1454     bxe_disable_nvram_access(sc);
1455     bxe_release_nvram_lock(sc);
1456 
1457     return (rc);
1458 }
1459 
1460 /* copy command into DMAE command memory and set DMAE command Go */
1461 void
1462 bxe_post_dmae(struct bxe_softc    *sc,
1463               struct dmae_cmd *dmae,
1464               int                 idx)
1465 {
1466     uint32_t cmd_offset;
1467     int i;
1468 
1469     cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_cmd) * idx));
1470     for (i = 0; i < ((sizeof(struct dmae_cmd) / 4)); i++) {
1471         REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1472     }
1473 
1474     REG_WR(sc, dmae_reg_go_c[idx], 1);
1475 }
1476 
1477 uint32_t
1478 bxe_dmae_opcode_add_comp(uint32_t opcode,
1479                          uint8_t  comp_type)
1480 {
1481     return (opcode | ((comp_type << DMAE_CMD_C_DST_SHIFT) |
1482                       DMAE_CMD_C_TYPE_ENABLE));
1483 }
1484 
1485 uint32_t
1486 bxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1487 {
1488     return (opcode & ~DMAE_CMD_SRC_RESET);
1489 }
1490 
1491 uint32_t
1492 bxe_dmae_opcode(struct bxe_softc *sc,
1493                 uint8_t          src_type,
1494                 uint8_t          dst_type,
1495                 uint8_t          with_comp,
1496                 uint8_t          comp_type)
1497 {
1498     uint32_t opcode = 0;
1499 
1500     opcode |= ((src_type << DMAE_CMD_SRC_SHIFT) |
1501                (dst_type << DMAE_CMD_DST_SHIFT));
1502 
1503     opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
1504 
1505     opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1506 
1507     opcode |= ((SC_VN(sc) << DMAE_CMD_E1HVN_SHIFT) |
1508                (SC_VN(sc) << DMAE_CMD_DST_VN_SHIFT));
1509 
1510     opcode |= (DMAE_COM_SET_ERR << DMAE_CMD_ERR_POLICY_SHIFT);
1511 
1512 #ifdef __BIG_ENDIAN
1513     opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1514 #else
1515     opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1516 #endif
1517 
1518     if (with_comp) {
1519         opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1520     }
1521 
1522     return (opcode);
1523 }
1524 
1525 static void
1526 bxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1527                         struct dmae_cmd *dmae,
1528                         uint8_t             src_type,
1529                         uint8_t             dst_type)
1530 {
1531     memset(dmae, 0, sizeof(struct dmae_cmd));
1532 
1533     /* set the opcode */
1534     dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1535                                    TRUE, DMAE_COMP_PCI);
1536 
1537     /* fill in the completion parameters */
1538     dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1539     dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1540     dmae->comp_val     = DMAE_COMP_VAL;
1541 }
1542 
1543 /* issue a DMAE command over the init channel and wait for completion */
1544 static int
1545 bxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1546                          struct dmae_cmd *dmae)
1547 {
1548     uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1549     int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1550 
1551     BXE_DMAE_LOCK(sc);
1552 
1553     /* reset completion */
1554     *wb_comp = 0;
1555 
1556     /* post the command on the channel used for initializations */
1557     bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1558 
1559     /* wait for completion */
1560     DELAY(5);
1561 
1562     while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1563         if (!timeout ||
1564             (sc->recovery_state != BXE_RECOVERY_DONE &&
1565              sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1566             BLOGE(sc, "DMAE timeout! *wb_comp 0x%x recovery_state 0x%x\n",
1567                 *wb_comp, sc->recovery_state);
1568             BXE_DMAE_UNLOCK(sc);
1569             return (DMAE_TIMEOUT);
1570         }
1571 
1572         timeout--;
1573         DELAY(50);
1574     }
1575 
1576     if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1577         BLOGE(sc, "DMAE PCI error! *wb_comp 0x%x recovery_state 0x%x\n",
1578                 *wb_comp, sc->recovery_state);
1579         BXE_DMAE_UNLOCK(sc);
1580         return (DMAE_PCI_ERROR);
1581     }
1582 
1583     BXE_DMAE_UNLOCK(sc);
1584     return (0);
1585 }
1586 
1587 void
1588 bxe_read_dmae(struct bxe_softc *sc,
1589               uint32_t         src_addr,
1590               uint32_t         len32)
1591 {
1592     struct dmae_cmd dmae;
1593     uint32_t *data;
1594     int i, rc;
1595 
1596     DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1597 
1598     if (!sc->dmae_ready) {
1599         data = BXE_SP(sc, wb_data[0]);
1600 
1601         for (i = 0; i < len32; i++) {
1602             data[i] = (CHIP_IS_E1(sc)) ?
1603                           bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1604                           REG_RD(sc, (src_addr + (i * 4)));
1605         }
1606 
1607         return;
1608     }
1609 
1610     /* set opcode and fixed command fields */
1611     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1612 
1613     /* fill in addresses and len */
1614     dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1615     dmae.src_addr_hi = 0;
1616     dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1617     dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1618     dmae.len         = len32;
1619 
1620     /* issue the command and wait for completion */
1621     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1622         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1623     };
1624 }
1625 
1626 void
1627 bxe_write_dmae(struct bxe_softc *sc,
1628                bus_addr_t       dma_addr,
1629                uint32_t         dst_addr,
1630                uint32_t         len32)
1631 {
1632     struct dmae_cmd dmae;
1633     int rc;
1634 
1635     if (!sc->dmae_ready) {
1636         DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1637 
1638         if (CHIP_IS_E1(sc)) {
1639             ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1640         } else {
1641             ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1642         }
1643 
1644         return;
1645     }
1646 
1647     /* set opcode and fixed command fields */
1648     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1649 
1650     /* fill in addresses and len */
1651     dmae.src_addr_lo = U64_LO(dma_addr);
1652     dmae.src_addr_hi = U64_HI(dma_addr);
1653     dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1654     dmae.dst_addr_hi = 0;
1655     dmae.len         = len32;
1656 
1657     /* issue the command and wait for completion */
1658     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1659         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1660     }
1661 }
1662 
1663 void
1664 bxe_write_dmae_phys_len(struct bxe_softc *sc,
1665                         bus_addr_t       phys_addr,
1666                         uint32_t         addr,
1667                         uint32_t         len)
1668 {
1669     int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1670     int offset = 0;
1671 
1672     while (len > dmae_wr_max) {
1673         bxe_write_dmae(sc,
1674                        (phys_addr + offset), /* src DMA address */
1675                        (addr + offset),      /* dst GRC address */
1676                        dmae_wr_max);
1677         offset += (dmae_wr_max * 4);
1678         len -= dmae_wr_max;
1679     }
1680 
1681     bxe_write_dmae(sc,
1682                    (phys_addr + offset), /* src DMA address */
1683                    (addr + offset),      /* dst GRC address */
1684                    len);
1685 }
1686 
1687 void
1688 bxe_set_ctx_validation(struct bxe_softc   *sc,
1689                        struct eth_context *cxt,
1690                        uint32_t           cid)
1691 {
1692     /* ustorm cxt validation */
1693     cxt->ustorm_ag_context.cdu_usage =
1694         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1695             CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1696     /* xcontext validation */
1697     cxt->xstorm_ag_context.cdu_reserved =
1698         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1699             CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1700 }
1701 
1702 static void
1703 bxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1704                             uint8_t          port,
1705                             uint8_t          fw_sb_id,
1706                             uint8_t          sb_index,
1707                             uint8_t          ticks)
1708 {
1709     uint32_t addr =
1710         (BAR_CSTRORM_INTMEM +
1711          CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1712 
1713     REG_WR8(sc, addr, ticks);
1714 
1715     BLOGD(sc, DBG_LOAD,
1716           "port %d fw_sb_id %d sb_index %d ticks %d\n",
1717           port, fw_sb_id, sb_index, ticks);
1718 }
1719 
1720 static void
1721 bxe_storm_memset_hc_disable(struct bxe_softc *sc,
1722                             uint8_t          port,
1723                             uint16_t         fw_sb_id,
1724                             uint8_t          sb_index,
1725                             uint8_t          disable)
1726 {
1727     uint32_t enable_flag =
1728         (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1729     uint32_t addr =
1730         (BAR_CSTRORM_INTMEM +
1731          CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1732     uint8_t flags;
1733 
1734     /* clear and set */
1735     flags = REG_RD8(sc, addr);
1736     flags &= ~HC_INDEX_DATA_HC_ENABLED;
1737     flags |= enable_flag;
1738     REG_WR8(sc, addr, flags);
1739 
1740     BLOGD(sc, DBG_LOAD,
1741           "port %d fw_sb_id %d sb_index %d disable %d\n",
1742           port, fw_sb_id, sb_index, disable);
1743 }
1744 
1745 void
1746 bxe_update_coalesce_sb_index(struct bxe_softc *sc,
1747                              uint8_t          fw_sb_id,
1748                              uint8_t          sb_index,
1749                              uint8_t          disable,
1750                              uint16_t         usec)
1751 {
1752     int port = SC_PORT(sc);
1753     uint8_t ticks = (usec / 4); /* XXX ??? */
1754 
1755     bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1756 
1757     disable = (disable) ? 1 : ((usec) ? 0 : 1);
1758     bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1759 }
1760 
1761 void
1762 elink_cb_udelay(struct bxe_softc *sc,
1763                 uint32_t         usecs)
1764 {
1765     DELAY(usecs);
1766 }
1767 
1768 uint32_t
1769 elink_cb_reg_read(struct bxe_softc *sc,
1770                   uint32_t         reg_addr)
1771 {
1772     return (REG_RD(sc, reg_addr));
1773 }
1774 
1775 void
1776 elink_cb_reg_write(struct bxe_softc *sc,
1777                    uint32_t         reg_addr,
1778                    uint32_t         val)
1779 {
1780     REG_WR(sc, reg_addr, val);
1781 }
1782 
1783 void
1784 elink_cb_reg_wb_write(struct bxe_softc *sc,
1785                       uint32_t         offset,
1786                       uint32_t         *wb_write,
1787                       uint16_t         len)
1788 {
1789     REG_WR_DMAE(sc, offset, wb_write, len);
1790 }
1791 
1792 void
1793 elink_cb_reg_wb_read(struct bxe_softc *sc,
1794                      uint32_t         offset,
1795                      uint32_t         *wb_write,
1796                      uint16_t         len)
1797 {
1798     REG_RD_DMAE(sc, offset, wb_write, len);
1799 }
1800 
1801 uint8_t
1802 elink_cb_path_id(struct bxe_softc *sc)
1803 {
1804     return (SC_PATH(sc));
1805 }
1806 
1807 void
1808 elink_cb_event_log(struct bxe_softc     *sc,
1809                    const elink_log_id_t elink_log_id,
1810                    ...)
1811 {
1812     /* XXX */
1813     BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1814 }
1815 
1816 static int
1817 bxe_set_spio(struct bxe_softc *sc,
1818              int              spio,
1819              uint32_t         mode)
1820 {
1821     uint32_t spio_reg;
1822 
1823     /* Only 2 SPIOs are configurable */
1824     if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1825         BLOGE(sc, "Invalid SPIO 0x%x mode 0x%x\n", spio, mode);
1826         return (-1);
1827     }
1828 
1829     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1830 
1831     /* read SPIO and mask except the float bits */
1832     spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1833 
1834     switch (mode) {
1835     case MISC_SPIO_OUTPUT_LOW:
1836         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1837         /* clear FLOAT and set CLR */
1838         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1839         spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1840         break;
1841 
1842     case MISC_SPIO_OUTPUT_HIGH:
1843         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1844         /* clear FLOAT and set SET */
1845         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1846         spio_reg |=  (spio << MISC_SPIO_SET_POS);
1847         break;
1848 
1849     case MISC_SPIO_INPUT_HI_Z:
1850         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1851         /* set FLOAT */
1852         spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1853         break;
1854 
1855     default:
1856         break;
1857     }
1858 
1859     REG_WR(sc, MISC_REG_SPIO, spio_reg);
1860     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1861 
1862     return (0);
1863 }
1864 
1865 static int
1866 bxe_gpio_read(struct bxe_softc *sc,
1867               int              gpio_num,
1868               uint8_t          port)
1869 {
1870     /* The GPIO should be swapped if swap register is set and active */
1871     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1872                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1873     int gpio_shift = (gpio_num +
1874                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1875     uint32_t gpio_mask = (1 << gpio_shift);
1876     uint32_t gpio_reg;
1877 
1878     if (gpio_num > MISC_REGISTERS_GPIO_3) {
1879         BLOGE(sc, "Invalid GPIO %d port 0x%x gpio_port %d gpio_shift %d"
1880             " gpio_mask 0x%x\n", gpio_num, port, gpio_port, gpio_shift,
1881             gpio_mask);
1882         return (-1);
1883     }
1884 
1885     /* read GPIO value */
1886     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1887 
1888     /* get the requested pin value */
1889     return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
1890 }
1891 
1892 static int
1893 bxe_gpio_write(struct bxe_softc *sc,
1894                int              gpio_num,
1895                uint32_t         mode,
1896                uint8_t          port)
1897 {
1898     /* The GPIO should be swapped if swap register is set and active */
1899     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1900                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1901     int gpio_shift = (gpio_num +
1902                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1903     uint32_t gpio_mask = (1 << gpio_shift);
1904     uint32_t gpio_reg;
1905 
1906     if (gpio_num > MISC_REGISTERS_GPIO_3) {
1907         BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
1908             " gpio_shift %d gpio_mask 0x%x\n",
1909             gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
1910         return (-1);
1911     }
1912 
1913     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1914 
1915     /* read GPIO and mask except the float bits */
1916     gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1917 
1918     switch (mode) {
1919     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1920         BLOGD(sc, DBG_PHY,
1921               "Set GPIO %d (shift %d) -> output low\n",
1922               gpio_num, gpio_shift);
1923         /* clear FLOAT and set CLR */
1924         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1925         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1926         break;
1927 
1928     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1929         BLOGD(sc, DBG_PHY,
1930               "Set GPIO %d (shift %d) -> output high\n",
1931               gpio_num, gpio_shift);
1932         /* clear FLOAT and set SET */
1933         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1934         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1935         break;
1936 
1937     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1938         BLOGD(sc, DBG_PHY,
1939               "Set GPIO %d (shift %d) -> input\n",
1940               gpio_num, gpio_shift);
1941         /* set FLOAT */
1942         gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1943         break;
1944 
1945     default:
1946         break;
1947     }
1948 
1949     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1950     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1951 
1952     return (0);
1953 }
1954 
1955 static int
1956 bxe_gpio_mult_write(struct bxe_softc *sc,
1957                     uint8_t          pins,
1958                     uint32_t         mode)
1959 {
1960     uint32_t gpio_reg;
1961 
1962     /* any port swapping should be handled by caller */
1963 
1964     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1965 
1966     /* read GPIO and mask except the float bits */
1967     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1968     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1969     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
1970     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
1971 
1972     switch (mode) {
1973     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1974         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
1975         /* set CLR */
1976         gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
1977         break;
1978 
1979     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1980         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
1981         /* set SET */
1982         gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
1983         break;
1984 
1985     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1986         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
1987         /* set FLOAT */
1988         gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1989         break;
1990 
1991     default:
1992         BLOGE(sc, "Invalid GPIO mode assignment pins 0x%x mode 0x%x"
1993             " gpio_reg 0x%x\n", pins, mode, gpio_reg);
1994         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1995         return (-1);
1996     }
1997 
1998     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1999     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2000 
2001     return (0);
2002 }
2003 
2004 static int
2005 bxe_gpio_int_write(struct bxe_softc *sc,
2006                    int              gpio_num,
2007                    uint32_t         mode,
2008                    uint8_t          port)
2009 {
2010     /* The GPIO should be swapped if swap register is set and active */
2011     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2012                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2013     int gpio_shift = (gpio_num +
2014                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2015     uint32_t gpio_mask = (1 << gpio_shift);
2016     uint32_t gpio_reg;
2017 
2018     if (gpio_num > MISC_REGISTERS_GPIO_3) {
2019         BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
2020             " gpio_shift %d gpio_mask 0x%x\n",
2021             gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
2022         return (-1);
2023     }
2024 
2025     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2026 
2027     /* read GPIO int */
2028     gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2029 
2030     switch (mode) {
2031     case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2032         BLOGD(sc, DBG_PHY,
2033               "Clear GPIO INT %d (shift %d) -> output low\n",
2034               gpio_num, gpio_shift);
2035         /* clear SET and set CLR */
2036         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2037         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2038         break;
2039 
2040     case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2041         BLOGD(sc, DBG_PHY,
2042               "Set GPIO INT %d (shift %d) -> output high\n",
2043               gpio_num, gpio_shift);
2044         /* clear CLR and set SET */
2045         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2046         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2047         break;
2048 
2049     default:
2050         break;
2051     }
2052 
2053     REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2054     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2055 
2056     return (0);
2057 }
2058 
2059 uint32_t
2060 elink_cb_gpio_read(struct bxe_softc *sc,
2061                    uint16_t         gpio_num,
2062                    uint8_t          port)
2063 {
2064     return (bxe_gpio_read(sc, gpio_num, port));
2065 }
2066 
2067 uint8_t
2068 elink_cb_gpio_write(struct bxe_softc *sc,
2069                     uint16_t         gpio_num,
2070                     uint8_t          mode, /* 0=low 1=high */
2071                     uint8_t          port)
2072 {
2073     return (bxe_gpio_write(sc, gpio_num, mode, port));
2074 }
2075 
2076 uint8_t
2077 elink_cb_gpio_mult_write(struct bxe_softc *sc,
2078                          uint8_t          pins,
2079                          uint8_t          mode) /* 0=low 1=high */
2080 {
2081     return (bxe_gpio_mult_write(sc, pins, mode));
2082 }
2083 
2084 uint8_t
2085 elink_cb_gpio_int_write(struct bxe_softc *sc,
2086                         uint16_t         gpio_num,
2087                         uint8_t          mode, /* 0=low 1=high */
2088                         uint8_t          port)
2089 {
2090     return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2091 }
2092 
2093 void
2094 elink_cb_notify_link_changed(struct bxe_softc *sc)
2095 {
2096     REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2097                 (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2098 }
2099 
2100 /* send the MCP a request, block until there is a reply */
2101 uint32_t
2102 elink_cb_fw_command(struct bxe_softc *sc,
2103                     uint32_t         command,
2104                     uint32_t         param)
2105 {
2106     int mb_idx = SC_FW_MB_IDX(sc);
2107     uint32_t seq;
2108     uint32_t rc = 0;
2109     uint32_t cnt = 1;
2110     uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2111 
2112     BXE_FWMB_LOCK(sc);
2113 
2114     seq = ++sc->fw_seq;
2115     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2116     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2117 
2118     BLOGD(sc, DBG_PHY,
2119           "wrote command 0x%08x to FW MB param 0x%08x\n",
2120           (command | seq), param);
2121 
2122     /* Let the FW do it's magic. GIve it up to 5 seconds... */
2123     do {
2124         DELAY(delay * 1000);
2125         rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2126     } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2127 
2128     BLOGD(sc, DBG_PHY,
2129           "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2130           cnt*delay, rc, seq);
2131 
2132     /* is this a reply to our command? */
2133     if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2134         rc &= FW_MSG_CODE_MASK;
2135     } else {
2136         /* Ruh-roh! */
2137         BLOGE(sc, "FW failed to respond!\n");
2138         // XXX bxe_fw_dump(sc);
2139         rc = 0;
2140     }
2141 
2142     BXE_FWMB_UNLOCK(sc);
2143     return (rc);
2144 }
2145 
2146 static uint32_t
2147 bxe_fw_command(struct bxe_softc *sc,
2148                uint32_t         command,
2149                uint32_t         param)
2150 {
2151     return (elink_cb_fw_command(sc, command, param));
2152 }
2153 
2154 static void
2155 __storm_memset_dma_mapping(struct bxe_softc *sc,
2156                            uint32_t         addr,
2157                            bus_addr_t       mapping)
2158 {
2159     REG_WR(sc, addr, U64_LO(mapping));
2160     REG_WR(sc, (addr + 4), U64_HI(mapping));
2161 }
2162 
2163 static void
2164 storm_memset_spq_addr(struct bxe_softc *sc,
2165                       bus_addr_t       mapping,
2166                       uint16_t         abs_fid)
2167 {
2168     uint32_t addr = (XSEM_REG_FAST_MEMORY +
2169                      XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2170     __storm_memset_dma_mapping(sc, addr, mapping);
2171 }
2172 
2173 static void
2174 storm_memset_vf_to_pf(struct bxe_softc *sc,
2175                       uint16_t         abs_fid,
2176                       uint16_t         pf_id)
2177 {
2178     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2179     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2180     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2181     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2182 }
2183 
2184 static void
2185 storm_memset_func_en(struct bxe_softc *sc,
2186                      uint16_t         abs_fid,
2187                      uint8_t          enable)
2188 {
2189     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2190     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2191     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2192     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2193 }
2194 
2195 static void
2196 storm_memset_eq_data(struct bxe_softc       *sc,
2197                      struct event_ring_data *eq_data,
2198                      uint16_t               pfid)
2199 {
2200     uint32_t addr;
2201     size_t size;
2202 
2203     addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2204     size = sizeof(struct event_ring_data);
2205     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2206 }
2207 
2208 static void
2209 storm_memset_eq_prod(struct bxe_softc *sc,
2210                      uint16_t         eq_prod,
2211                      uint16_t         pfid)
2212 {
2213     uint32_t addr = (BAR_CSTRORM_INTMEM +
2214                      CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2215     REG_WR16(sc, addr, eq_prod);
2216 }
2217 
2218 /*
2219  * Post a slowpath command.
2220  *
2221  * A slowpath command is used to propogate a configuration change through
2222  * the controller in a controlled manner, allowing each STORM processor and
2223  * other H/W blocks to phase in the change.  The commands sent on the
2224  * slowpath are referred to as ramrods.  Depending on the ramrod used the
2225  * completion of the ramrod will occur in different ways.  Here's a
2226  * breakdown of ramrods and how they complete:
2227  *
2228  * RAMROD_CMD_ID_ETH_PORT_SETUP
2229  *   Used to setup the leading connection on a port.  Completes on the
2230  *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2231  *
2232  * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2233  *   Used to setup an additional connection on a port.  Completes on the
2234  *   RCQ of the multi-queue/RSS connection being initialized.
2235  *
2236  * RAMROD_CMD_ID_ETH_STAT_QUERY
2237  *   Used to force the storm processors to update the statistics database
2238  *   in host memory.  This ramrod is send on the leading connection CID and
2239  *   completes as an index increment of the CSTORM on the default status
2240  *   block.
2241  *
2242  * RAMROD_CMD_ID_ETH_UPDATE
2243  *   Used to update the state of the leading connection, usually to udpate
2244  *   the RSS indirection table.  Completes on the RCQ of the leading
2245  *   connection. (Not currently used under FreeBSD until OS support becomes
2246  *   available.)
2247  *
2248  * RAMROD_CMD_ID_ETH_HALT
2249  *   Used when tearing down a connection prior to driver unload.  Completes
2250  *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2251  *   use this on the leading connection.
2252  *
2253  * RAMROD_CMD_ID_ETH_SET_MAC
2254  *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2255  *   the RCQ of the leading connection.
2256  *
2257  * RAMROD_CMD_ID_ETH_CFC_DEL
2258  *   Used when tearing down a conneciton prior to driver unload.  Completes
2259  *   on the RCQ of the leading connection (since the current connection
2260  *   has been completely removed from controller memory).
2261  *
2262  * RAMROD_CMD_ID_ETH_PORT_DEL
2263  *   Used to tear down the leading connection prior to driver unload,
2264  *   typically fp[0].  Completes as an index increment of the CSTORM on the
2265  *   default status block.
2266  *
2267  * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2268  *   Used for connection offload.  Completes on the RCQ of the multi-queue
2269  *   RSS connection that is being offloaded.  (Not currently used under
2270  *   FreeBSD.)
2271  *
2272  * There can only be one command pending per function.
2273  *
2274  * Returns:
2275  *   0 = Success, !0 = Failure.
2276  */
2277 
2278 /* must be called under the spq lock */
2279 static inline
2280 struct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2281 {
2282     struct eth_spe *next_spe = sc->spq_prod_bd;
2283 
2284     if (sc->spq_prod_bd == sc->spq_last_bd) {
2285         /* wrap back to the first eth_spq */
2286         sc->spq_prod_bd = sc->spq;
2287         sc->spq_prod_idx = 0;
2288     } else {
2289         sc->spq_prod_bd++;
2290         sc->spq_prod_idx++;
2291     }
2292 
2293     return (next_spe);
2294 }
2295 
2296 /* must be called under the spq lock */
2297 static inline
2298 void bxe_sp_prod_update(struct bxe_softc *sc)
2299 {
2300     int func = SC_FUNC(sc);
2301 
2302     /*
2303      * Make sure that BD data is updated before writing the producer.
2304      * BD data is written to the memory, the producer is read from the
2305      * memory, thus we need a full memory barrier to ensure the ordering.
2306      */
2307     mb();
2308 
2309     REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2310              sc->spq_prod_idx);
2311 
2312     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2313                       BUS_SPACE_BARRIER_WRITE);
2314 }
2315 
2316 /**
2317  * bxe_is_contextless_ramrod - check if the current command ends on EQ
2318  *
2319  * @cmd:      command to check
2320  * @cmd_type: command type
2321  */
2322 static inline
2323 int bxe_is_contextless_ramrod(int cmd,
2324                               int cmd_type)
2325 {
2326     if ((cmd_type == NONE_CONNECTION_TYPE) ||
2327         (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2328         (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2329         (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2330         (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2331         (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2332         (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2333         return (TRUE);
2334     } else {
2335         return (FALSE);
2336     }
2337 }
2338 
2339 /**
2340  * bxe_sp_post - place a single command on an SP ring
2341  *
2342  * @sc:         driver handle
2343  * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2344  * @cid:        SW CID the command is related to
2345  * @data_hi:    command private data address (high 32 bits)
2346  * @data_lo:    command private data address (low 32 bits)
2347  * @cmd_type:   command type (e.g. NONE, ETH)
2348  *
2349  * SP data is handled as if it's always an address pair, thus data fields are
2350  * not swapped to little endian in upper functions. Instead this function swaps
2351  * data as if it's two uint32 fields.
2352  */
2353 int
2354 bxe_sp_post(struct bxe_softc *sc,
2355             int              command,
2356             int              cid,
2357             uint32_t         data_hi,
2358             uint32_t         data_lo,
2359             int              cmd_type)
2360 {
2361     struct eth_spe *spe;
2362     uint16_t type;
2363     int common;
2364 
2365     common = bxe_is_contextless_ramrod(command, cmd_type);
2366 
2367     BXE_SP_LOCK(sc);
2368 
2369     if (common) {
2370         if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2371             BLOGE(sc, "EQ ring is full!\n");
2372             BXE_SP_UNLOCK(sc);
2373             return (-1);
2374         }
2375     } else {
2376         if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2377             BLOGE(sc, "SPQ ring is full!\n");
2378             BXE_SP_UNLOCK(sc);
2379             return (-1);
2380         }
2381     }
2382 
2383     spe = bxe_sp_get_next(sc);
2384 
2385     /* CID needs port number to be encoded int it */
2386     spe->hdr.conn_and_cmd_data =
2387         htole32((command << SPE_HDR_T_CMD_ID_SHIFT) | HW_CID(sc, cid));
2388 
2389     type = (cmd_type << SPE_HDR_T_CONN_TYPE_SHIFT) & SPE_HDR_T_CONN_TYPE;
2390 
2391     /* TBD: Check if it works for VFs */
2392     type |= ((SC_FUNC(sc) << SPE_HDR_T_FUNCTION_ID_SHIFT) &
2393              SPE_HDR_T_FUNCTION_ID);
2394 
2395     spe->hdr.type = htole16(type);
2396 
2397     spe->data.update_data_addr.hi = htole32(data_hi);
2398     spe->data.update_data_addr.lo = htole32(data_lo);
2399 
2400     /*
2401      * It's ok if the actual decrement is issued towards the memory
2402      * somewhere between the lock and unlock. Thus no more explict
2403      * memory barrier is needed.
2404      */
2405     if (common) {
2406         atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2407     } else {
2408         atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2409     }
2410 
2411     BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2412     BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2413           BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2414     BLOGD(sc, DBG_SP,
2415           "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2416           sc->spq_prod_idx,
2417           (uint32_t)U64_HI(sc->spq_dma.paddr),
2418           (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2419           command,
2420           common,
2421           HW_CID(sc, cid),
2422           data_hi,
2423           data_lo,
2424           type,
2425           atomic_load_acq_long(&sc->cq_spq_left),
2426           atomic_load_acq_long(&sc->eq_spq_left));
2427 
2428     bxe_sp_prod_update(sc);
2429 
2430     BXE_SP_UNLOCK(sc);
2431     return (0);
2432 }
2433 
2434 /**
2435  * bxe_debug_print_ind_table - prints the indirection table configuration.
2436  *
2437  * @sc: driver hanlde
2438  * @p:  pointer to rss configuration
2439  */
2440 
2441 /*
2442  * FreeBSD Device probe function.
2443  *
2444  * Compares the device found to the driver's list of supported devices and
2445  * reports back to the bsd loader whether this is the right driver for the device.
2446  * This is the driver entry function called from the "kldload" command.
2447  *
2448  * Returns:
2449  *   BUS_PROBE_DEFAULT on success, positive value on failure.
2450  */
2451 static int
2452 bxe_probe(device_t dev)
2453 {
2454     struct bxe_softc *sc;
2455     struct bxe_device_type *t;
2456     char *descbuf;
2457     uint16_t did, sdid, svid, vid;
2458 
2459     /* Find our device structure */
2460     sc = device_get_softc(dev);
2461     sc->dev = dev;
2462     t = bxe_devs;
2463 
2464     /* Get the data for the device to be probed. */
2465     vid  = pci_get_vendor(dev);
2466     did  = pci_get_device(dev);
2467     svid = pci_get_subvendor(dev);
2468     sdid = pci_get_subdevice(dev);
2469 
2470     BLOGD(sc, DBG_LOAD,
2471           "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
2472           "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
2473 
2474     /* Look through the list of known devices for a match. */
2475     while (t->bxe_name != NULL) {
2476         if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2477             ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2478             ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2479             descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2480             if (descbuf == NULL)
2481                 return (ENOMEM);
2482 
2483             /* Print out the device identity. */
2484             snprintf(descbuf, BXE_DEVDESC_MAX,
2485                      "%s (%c%d) BXE v:%s\n", t->bxe_name,
2486                      (((pci_read_config(dev, PCIR_REVID, 4) &
2487                         0xf0) >> 4) + 'A'),
2488                      (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2489                      BXE_DRIVER_VERSION);
2490 
2491             device_set_desc_copy(dev, descbuf);
2492             free(descbuf, M_TEMP);
2493             return (BUS_PROBE_DEFAULT);
2494         }
2495         t++;
2496     }
2497 
2498     return (ENXIO);
2499 }
2500 
2501 static void
2502 bxe_init_mutexes(struct bxe_softc *sc)
2503 {
2504 #ifdef BXE_CORE_LOCK_SX
2505     snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2506              "bxe%d_core_lock", sc->unit);
2507     sx_init(&sc->core_sx, sc->core_sx_name);
2508 #else
2509     snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2510              "bxe%d_core_lock", sc->unit);
2511     mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2512 #endif
2513 
2514     snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2515              "bxe%d_sp_lock", sc->unit);
2516     mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2517 
2518     snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2519              "bxe%d_dmae_lock", sc->unit);
2520     mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2521 
2522     snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2523              "bxe%d_phy_lock", sc->unit);
2524     mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2525 
2526     snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2527              "bxe%d_fwmb_lock", sc->unit);
2528     mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2529 
2530     snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2531              "bxe%d_print_lock", sc->unit);
2532     mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2533 
2534     snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2535              "bxe%d_stats_lock", sc->unit);
2536     mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2537 
2538     snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2539              "bxe%d_mcast_lock", sc->unit);
2540     mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2541 }
2542 
2543 static void
2544 bxe_release_mutexes(struct bxe_softc *sc)
2545 {
2546 #ifdef BXE_CORE_LOCK_SX
2547     sx_destroy(&sc->core_sx);
2548 #else
2549     if (mtx_initialized(&sc->core_mtx)) {
2550         mtx_destroy(&sc->core_mtx);
2551     }
2552 #endif
2553 
2554     if (mtx_initialized(&sc->sp_mtx)) {
2555         mtx_destroy(&sc->sp_mtx);
2556     }
2557 
2558     if (mtx_initialized(&sc->dmae_mtx)) {
2559         mtx_destroy(&sc->dmae_mtx);
2560     }
2561 
2562     if (mtx_initialized(&sc->port.phy_mtx)) {
2563         mtx_destroy(&sc->port.phy_mtx);
2564     }
2565 
2566     if (mtx_initialized(&sc->fwmb_mtx)) {
2567         mtx_destroy(&sc->fwmb_mtx);
2568     }
2569 
2570     if (mtx_initialized(&sc->print_mtx)) {
2571         mtx_destroy(&sc->print_mtx);
2572     }
2573 
2574     if (mtx_initialized(&sc->stats_mtx)) {
2575         mtx_destroy(&sc->stats_mtx);
2576     }
2577 
2578     if (mtx_initialized(&sc->mcast_mtx)) {
2579         mtx_destroy(&sc->mcast_mtx);
2580     }
2581 }
2582 
2583 static void
2584 bxe_tx_disable(struct bxe_softc* sc)
2585 {
2586     if_t ifp = sc->ifp;
2587 
2588     /* tell the stack the driver is stopped and TX queue is full */
2589     if (ifp !=  NULL) {
2590         if_setdrvflags(ifp, 0);
2591     }
2592 }
2593 
2594 static void
2595 bxe_drv_pulse(struct bxe_softc *sc)
2596 {
2597     SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2598              sc->fw_drv_pulse_wr_seq);
2599 }
2600 
2601 static inline uint16_t
2602 bxe_tx_avail(struct bxe_softc *sc,
2603              struct bxe_fastpath *fp)
2604 {
2605     int16_t  used;
2606     uint16_t prod;
2607     uint16_t cons;
2608 
2609     prod = fp->tx_bd_prod;
2610     cons = fp->tx_bd_cons;
2611 
2612     used = SUB_S16(prod, cons);
2613 
2614     return (int16_t)(sc->tx_ring_size) - used;
2615 }
2616 
2617 static inline int
2618 bxe_tx_queue_has_work(struct bxe_fastpath *fp)
2619 {
2620     uint16_t hw_cons;
2621 
2622     mb(); /* status block fields can change */
2623     hw_cons = le16toh(*fp->tx_cons_sb);
2624     return (hw_cons != fp->tx_pkt_cons);
2625 }
2626 
2627 static inline uint8_t
2628 bxe_has_tx_work(struct bxe_fastpath *fp)
2629 {
2630     /* expand this for multi-cos if ever supported */
2631     return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2632 }
2633 
2634 static inline int
2635 bxe_has_rx_work(struct bxe_fastpath *fp)
2636 {
2637     uint16_t rx_cq_cons_sb;
2638 
2639     mb(); /* status block fields can change */
2640     rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2641     if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2642         rx_cq_cons_sb++;
2643     return (fp->rx_cq_cons != rx_cq_cons_sb);
2644 }
2645 
2646 static void
2647 bxe_sp_event(struct bxe_softc    *sc,
2648              struct bxe_fastpath *fp,
2649              union eth_rx_cqe    *rr_cqe)
2650 {
2651     int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2652     int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2653     enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2654     struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2655 
2656     BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2657           fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2658 
2659     switch (command) {
2660     case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2661         BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2662         drv_cmd = ECORE_Q_CMD_UPDATE;
2663         break;
2664 
2665     case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2666         BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2667         drv_cmd = ECORE_Q_CMD_SETUP;
2668         break;
2669 
2670     case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2671         BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2672         drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2673         break;
2674 
2675     case (RAMROD_CMD_ID_ETH_HALT):
2676         BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2677         drv_cmd = ECORE_Q_CMD_HALT;
2678         break;
2679 
2680     case (RAMROD_CMD_ID_ETH_TERMINATE):
2681         BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2682         drv_cmd = ECORE_Q_CMD_TERMINATE;
2683         break;
2684 
2685     case (RAMROD_CMD_ID_ETH_EMPTY):
2686         BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2687         drv_cmd = ECORE_Q_CMD_EMPTY;
2688         break;
2689 
2690     default:
2691         BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2692               command, fp->index);
2693         return;
2694     }
2695 
2696     if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2697         q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2698         /*
2699          * q_obj->complete_cmd() failure means that this was
2700          * an unexpected completion.
2701          *
2702          * In this case we don't want to increase the sc->spq_left
2703          * because apparently we haven't sent this command the first
2704          * place.
2705          */
2706         // bxe_panic(sc, ("Unexpected SP completion\n"));
2707         return;
2708     }
2709 
2710     atomic_add_acq_long(&sc->cq_spq_left, 1);
2711 
2712     BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2713           atomic_load_acq_long(&sc->cq_spq_left));
2714 }
2715 
2716 /*
2717  * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2718  * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2719  * the current aggregation queue as in-progress.
2720  */
2721 static void
2722 bxe_tpa_start(struct bxe_softc            *sc,
2723               struct bxe_fastpath         *fp,
2724               uint16_t                    queue,
2725               uint16_t                    cons,
2726               uint16_t                    prod,
2727               struct eth_fast_path_rx_cqe *cqe)
2728 {
2729     struct bxe_sw_rx_bd tmp_bd;
2730     struct bxe_sw_rx_bd *rx_buf;
2731     struct eth_rx_bd *rx_bd;
2732     int max_agg_queues;
2733     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2734     uint16_t index;
2735 
2736     BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2737                        "cons=%d prod=%d\n",
2738           fp->index, queue, cons, prod);
2739 
2740     max_agg_queues = MAX_AGG_QS(sc);
2741 
2742     KASSERT((queue < max_agg_queues),
2743             ("fp[%02d] invalid aggr queue (%d >= %d)!",
2744              fp->index, queue, max_agg_queues));
2745 
2746     KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2747             ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2748              fp->index, queue));
2749 
2750     /* copy the existing mbuf and mapping from the TPA pool */
2751     tmp_bd = tpa_info->bd;
2752 
2753     if (tmp_bd.m == NULL) {
2754         uint32_t *tmp;
2755 
2756         tmp = (uint32_t *)cqe;
2757 
2758         BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2759               fp->index, queue, cons, prod);
2760         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2761             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2762 
2763         /* XXX Error handling? */
2764         return;
2765     }
2766 
2767     /* change the TPA queue to the start state */
2768     tpa_info->state            = BXE_TPA_STATE_START;
2769     tpa_info->placement_offset = cqe->placement_offset;
2770     tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2771     tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2772     tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2773 
2774     fp->rx_tpa_queue_used |= (1 << queue);
2775 
2776     /*
2777      * If all the buffer descriptors are filled with mbufs then fill in
2778      * the current consumer index with a new BD. Else if a maximum Rx
2779      * buffer limit is imposed then fill in the next producer index.
2780      */
2781     index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2782                 prod : cons;
2783 
2784     /* move the received mbuf and mapping to TPA pool */
2785     tpa_info->bd = fp->rx_mbuf_chain[cons];
2786 
2787     /* release any existing RX BD mbuf mappings */
2788     if (cons != index) {
2789         rx_buf = &fp->rx_mbuf_chain[cons];
2790 
2791         if (rx_buf->m_map != NULL) {
2792             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2793                             BUS_DMASYNC_POSTREAD);
2794             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2795         }
2796 
2797         /*
2798          * We get here when the maximum number of rx buffers is less than
2799          * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2800          * it out here without concern of a memory leak.
2801          */
2802         fp->rx_mbuf_chain[cons].m = NULL;
2803     }
2804 
2805     /* update the Rx SW BD with the mbuf info from the TPA pool */
2806     fp->rx_mbuf_chain[index] = tmp_bd;
2807 
2808     /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2809     rx_bd = &fp->rx_chain[index];
2810     rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2811     rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2812 }
2813 
2814 /*
2815  * When a TPA aggregation is completed, loop through the individual mbufs
2816  * of the aggregation, combining them into a single mbuf which will be sent
2817  * up the stack. Refill all freed SGEs with mbufs as we go along.
2818  */
2819 static int
2820 bxe_fill_frag_mbuf(struct bxe_softc          *sc,
2821                    struct bxe_fastpath       *fp,
2822                    struct bxe_sw_tpa_info    *tpa_info,
2823                    uint16_t                  queue,
2824                    uint16_t                  pages,
2825                    struct mbuf               *m,
2826 			       struct eth_end_agg_rx_cqe *cqe,
2827                    uint16_t                  cqe_idx)
2828 {
2829     struct mbuf *m_frag;
2830     uint32_t frag_len, frag_size, i;
2831     uint16_t sge_idx;
2832     int rc = 0;
2833     int j;
2834 
2835     frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2836 
2837     BLOGD(sc, DBG_LRO,
2838           "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2839           fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2840 
2841     /* make sure the aggregated frame is not too big to handle */
2842     if (pages > 8 * PAGES_PER_SGE) {
2843 
2844         uint32_t *tmp = (uint32_t *)cqe;
2845 
2846         BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2847                   "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2848               fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2849               tpa_info->len_on_bd, frag_size);
2850 
2851         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2852             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2853 
2854         bxe_panic(sc, ("sge page count error\n"));
2855         return (EINVAL);
2856     }
2857 
2858     /*
2859      * Scan through the scatter gather list pulling individual mbufs into a
2860      * single mbuf for the host stack.
2861      */
2862     for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2863         sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2864 
2865         /*
2866          * Firmware gives the indices of the SGE as if the ring is an array
2867          * (meaning that the "next" element will consume 2 indices).
2868          */
2869         frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2870 
2871         BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2872                            "sge_idx=%d frag_size=%d frag_len=%d\n",
2873               fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2874 
2875         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2876 
2877         /* allocate a new mbuf for the SGE */
2878         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2879         if (rc) {
2880             /* Leave all remaining SGEs in the ring! */
2881             return (rc);
2882         }
2883 
2884         /* update the fragment length */
2885         m_frag->m_len = frag_len;
2886 
2887         /* concatenate the fragment to the head mbuf */
2888         m_cat(m, m_frag);
2889         fp->eth_q_stats.mbuf_alloc_sge--;
2890 
2891         /* update the TPA mbuf size and remaining fragment size */
2892         m->m_pkthdr.len += frag_len;
2893         frag_size -= frag_len;
2894     }
2895 
2896     BLOGD(sc, DBG_LRO,
2897           "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2898           fp->index, queue, frag_size);
2899 
2900     return (rc);
2901 }
2902 
2903 static inline void
2904 bxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2905 {
2906     int i, j;
2907 
2908     for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2909         int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2910 
2911         for (j = 0; j < 2; j++) {
2912             BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2913             idx--;
2914         }
2915     }
2916 }
2917 
2918 static inline void
2919 bxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2920 {
2921     /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2922     memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2923 
2924     /*
2925      * Clear the two last indices in the page to 1. These are the indices that
2926      * correspond to the "next" element, hence will never be indicated and
2927      * should be removed from the calculations.
2928      */
2929     bxe_clear_sge_mask_next_elems(fp);
2930 }
2931 
2932 static inline void
2933 bxe_update_last_max_sge(struct bxe_fastpath *fp,
2934                         uint16_t            idx)
2935 {
2936     uint16_t last_max = fp->last_max_sge;
2937 
2938     if (SUB_S16(idx, last_max) > 0) {
2939         fp->last_max_sge = idx;
2940     }
2941 }
2942 
2943 static inline void
2944 bxe_update_sge_prod(struct bxe_softc          *sc,
2945                     struct bxe_fastpath       *fp,
2946                     uint16_t                  sge_len,
2947                     union eth_sgl_or_raw_data *cqe)
2948 {
2949     uint16_t last_max, last_elem, first_elem;
2950     uint16_t delta = 0;
2951     uint16_t i;
2952 
2953     if (!sge_len) {
2954         return;
2955     }
2956 
2957     /* first mark all used pages */
2958     for (i = 0; i < sge_len; i++) {
2959         BIT_VEC64_CLEAR_BIT(fp->sge_mask,
2960                             RX_SGE(le16toh(cqe->sgl[i])));
2961     }
2962 
2963     BLOGD(sc, DBG_LRO,
2964           "fp[%02d] fp_cqe->sgl[%d] = %d\n",
2965           fp->index, sge_len - 1,
2966           le16toh(cqe->sgl[sge_len - 1]));
2967 
2968     /* assume that the last SGE index is the biggest */
2969     bxe_update_last_max_sge(fp,
2970                             le16toh(cqe->sgl[sge_len - 1]));
2971 
2972     last_max = RX_SGE(fp->last_max_sge);
2973     last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
2974     first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
2975 
2976     /* if ring is not full */
2977     if (last_elem + 1 != first_elem) {
2978         last_elem++;
2979     }
2980 
2981     /* now update the prod */
2982     for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
2983         if (__predict_true(fp->sge_mask[i])) {
2984             break;
2985         }
2986 
2987         fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
2988         delta += BIT_VEC64_ELEM_SZ;
2989     }
2990 
2991     if (delta > 0) {
2992         fp->rx_sge_prod += delta;
2993         /* clear page-end entries */
2994         bxe_clear_sge_mask_next_elems(fp);
2995     }
2996 
2997     BLOGD(sc, DBG_LRO,
2998           "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
2999           fp->index, fp->last_max_sge, fp->rx_sge_prod);
3000 }
3001 
3002 /*
3003  * The aggregation on the current TPA queue has completed. Pull the individual
3004  * mbuf fragments together into a single mbuf, perform all necessary checksum
3005  * calculations, and send the resuting mbuf to the stack.
3006  */
3007 static void
3008 bxe_tpa_stop(struct bxe_softc          *sc,
3009              struct bxe_fastpath       *fp,
3010              struct bxe_sw_tpa_info    *tpa_info,
3011              uint16_t                  queue,
3012              uint16_t                  pages,
3013 			 struct eth_end_agg_rx_cqe *cqe,
3014              uint16_t                  cqe_idx)
3015 {
3016     if_t ifp = sc->ifp;
3017     struct mbuf *m;
3018     int rc = 0;
3019 
3020     BLOGD(sc, DBG_LRO,
3021           "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3022           fp->index, queue, tpa_info->placement_offset,
3023           le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3024 
3025     m = tpa_info->bd.m;
3026 
3027     /* allocate a replacement before modifying existing mbuf */
3028     rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3029     if (rc) {
3030         /* drop the frame and log an error */
3031         fp->eth_q_stats.rx_soft_errors++;
3032         goto bxe_tpa_stop_exit;
3033     }
3034 
3035     /* we have a replacement, fixup the current mbuf */
3036     m_adj(m, tpa_info->placement_offset);
3037     m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3038 
3039     /* mark the checksums valid (taken care of by the firmware) */
3040     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3041     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3042     m->m_pkthdr.csum_data = 0xffff;
3043     m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3044                                CSUM_IP_VALID   |
3045                                CSUM_DATA_VALID |
3046                                CSUM_PSEUDO_HDR);
3047 
3048     /* aggregate all of the SGEs into a single mbuf */
3049     rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3050     if (rc) {
3051         /* drop the packet and log an error */
3052         fp->eth_q_stats.rx_soft_errors++;
3053         m_freem(m);
3054     } else {
3055         if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3056             m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3057             m->m_flags |= M_VLANTAG;
3058         }
3059 
3060         /* assign packet to this interface interface */
3061         if_setrcvif(m, ifp);
3062 
3063 #if __FreeBSD_version >= 800000
3064         /* specify what RSS queue was used for this flow */
3065         m->m_pkthdr.flowid = fp->index;
3066         M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
3067 #endif
3068 
3069         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3070         fp->eth_q_stats.rx_tpa_pkts++;
3071 
3072         /* pass the frame to the stack */
3073         if_input(ifp, m);
3074     }
3075 
3076     /* we passed an mbuf up the stack or dropped the frame */
3077     fp->eth_q_stats.mbuf_alloc_tpa--;
3078 
3079 bxe_tpa_stop_exit:
3080 
3081     fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3082     fp->rx_tpa_queue_used &= ~(1 << queue);
3083 }
3084 
3085 static uint8_t
3086 bxe_service_rxsgl(
3087                  struct bxe_fastpath *fp,
3088                  uint16_t len,
3089                  uint16_t lenonbd,
3090                  struct mbuf *m,
3091                  struct eth_fast_path_rx_cqe *cqe_fp)
3092 {
3093     struct mbuf *m_frag;
3094     uint16_t frags, frag_len;
3095     uint16_t sge_idx = 0;
3096     uint16_t j;
3097     uint8_t i, rc = 0;
3098     uint32_t frag_size;
3099 
3100     /* adjust the mbuf */
3101     m->m_len = lenonbd;
3102 
3103     frag_size =  len - lenonbd;
3104     frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3105 
3106     for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3107         sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3108 
3109         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3110         frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3111         m_frag->m_len = frag_len;
3112 
3113        /* allocate a new mbuf for the SGE */
3114         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3115         if (rc) {
3116             /* Leave all remaining SGEs in the ring! */
3117             return (rc);
3118         }
3119         fp->eth_q_stats.mbuf_alloc_sge--;
3120 
3121         /* concatenate the fragment to the head mbuf */
3122         m_cat(m, m_frag);
3123 
3124         frag_size -= frag_len;
3125     }
3126 
3127     bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3128 
3129     return rc;
3130 }
3131 
3132 static uint8_t
3133 bxe_rxeof(struct bxe_softc    *sc,
3134           struct bxe_fastpath *fp)
3135 {
3136     if_t ifp = sc->ifp;
3137     uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3138     uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3139     int rx_pkts = 0;
3140     int rc = 0;
3141 
3142     BXE_FP_RX_LOCK(fp);
3143 
3144     /* CQ "next element" is of the size of the regular element */
3145     hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3146     if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3147         hw_cq_cons++;
3148     }
3149 
3150     bd_cons = fp->rx_bd_cons;
3151     bd_prod = fp->rx_bd_prod;
3152     bd_prod_fw = bd_prod;
3153     sw_cq_cons = fp->rx_cq_cons;
3154     sw_cq_prod = fp->rx_cq_prod;
3155 
3156     /*
3157      * Memory barrier necessary as speculative reads of the rx
3158      * buffer can be ahead of the index in the status block
3159      */
3160     rmb();
3161 
3162     BLOGD(sc, DBG_RX,
3163           "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3164           fp->index, hw_cq_cons, sw_cq_cons);
3165 
3166     while (sw_cq_cons != hw_cq_cons) {
3167         struct bxe_sw_rx_bd *rx_buf = NULL;
3168         union eth_rx_cqe *cqe;
3169         struct eth_fast_path_rx_cqe *cqe_fp;
3170         uint8_t cqe_fp_flags;
3171         enum eth_rx_cqe_type cqe_fp_type;
3172         uint16_t len, lenonbd,  pad;
3173         struct mbuf *m = NULL;
3174 
3175         comp_ring_cons = RCQ(sw_cq_cons);
3176         bd_prod = RX_BD(bd_prod);
3177         bd_cons = RX_BD(bd_cons);
3178 
3179         cqe          = &fp->rcq_chain[comp_ring_cons];
3180         cqe_fp       = &cqe->fast_path_cqe;
3181         cqe_fp_flags = cqe_fp->type_error_flags;
3182         cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3183 
3184         BLOGD(sc, DBG_RX,
3185               "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3186               "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3187               "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3188               fp->index,
3189               hw_cq_cons,
3190               sw_cq_cons,
3191               bd_prod,
3192               bd_cons,
3193               CQE_TYPE(cqe_fp_flags),
3194               cqe_fp_flags,
3195               cqe_fp->status_flags,
3196               le32toh(cqe_fp->rss_hash_result),
3197               le16toh(cqe_fp->vlan_tag),
3198               le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3199               le16toh(cqe_fp->len_on_bd));
3200 
3201         /* is this a slowpath msg? */
3202         if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3203             bxe_sp_event(sc, fp, cqe);
3204             goto next_cqe;
3205         }
3206 
3207         rx_buf = &fp->rx_mbuf_chain[bd_cons];
3208 
3209         if (!CQE_TYPE_FAST(cqe_fp_type)) {
3210             struct bxe_sw_tpa_info *tpa_info;
3211             uint16_t frag_size, pages;
3212             uint8_t queue;
3213 
3214             if (CQE_TYPE_START(cqe_fp_type)) {
3215                 bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3216                               bd_cons, bd_prod, cqe_fp);
3217                 m = NULL; /* packet not ready yet */
3218                 goto next_rx;
3219             }
3220 
3221             KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3222                     ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3223 
3224             queue = cqe->end_agg_cqe.queue_index;
3225             tpa_info = &fp->rx_tpa_info[queue];
3226 
3227             BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3228                   fp->index, queue);
3229 
3230             frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3231                          tpa_info->len_on_bd);
3232             pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3233 
3234             bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3235                          &cqe->end_agg_cqe, comp_ring_cons);
3236 
3237             bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3238 
3239             goto next_cqe;
3240         }
3241 
3242         /* non TPA */
3243 
3244         /* is this an error packet? */
3245         if (__predict_false(cqe_fp_flags &
3246                             ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3247             BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3248             fp->eth_q_stats.rx_soft_errors++;
3249             goto next_rx;
3250         }
3251 
3252         len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3253         lenonbd = le16toh(cqe_fp->len_on_bd);
3254         pad = cqe_fp->placement_offset;
3255 
3256         m = rx_buf->m;
3257 
3258         if (__predict_false(m == NULL)) {
3259             BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3260                   bd_cons, fp->index);
3261             goto next_rx;
3262         }
3263 
3264         /* XXX double copy if packet length under a threshold */
3265 
3266         /*
3267          * If all the buffer descriptors are filled with mbufs then fill in
3268          * the current consumer index with a new BD. Else if a maximum Rx
3269          * buffer limit is imposed then fill in the next producer index.
3270          */
3271         rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3272                                   (sc->max_rx_bufs != RX_BD_USABLE) ?
3273                                       bd_prod : bd_cons);
3274         if (rc != 0) {
3275 
3276             /* we simply reuse the received mbuf and don't post it to the stack */
3277             m = NULL;
3278 
3279             BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3280                   fp->index, rc);
3281             fp->eth_q_stats.rx_soft_errors++;
3282 
3283             if (sc->max_rx_bufs != RX_BD_USABLE) {
3284                 /* copy this consumer index to the producer index */
3285                 memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3286                        sizeof(struct bxe_sw_rx_bd));
3287                 memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3288             }
3289 
3290             goto next_rx;
3291         }
3292 
3293         /* current mbuf was detached from the bd */
3294         fp->eth_q_stats.mbuf_alloc_rx--;
3295 
3296         /* we allocated a replacement mbuf, fixup the current one */
3297         m_adj(m, pad);
3298         m->m_pkthdr.len = m->m_len = len;
3299 
3300         if ((len > 60) && (len > lenonbd)) {
3301             fp->eth_q_stats.rx_bxe_service_rxsgl++;
3302             rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3303             if (rc)
3304                 break;
3305             fp->eth_q_stats.rx_jumbo_sge_pkts++;
3306         } else if (lenonbd < len) {
3307             fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3308         }
3309 
3310         /* assign packet to this interface interface */
3311 	if_setrcvif(m, ifp);
3312 
3313         /* assume no hardware checksum has complated */
3314         m->m_pkthdr.csum_flags = 0;
3315 
3316         /* validate checksum if offload enabled */
3317         if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3318             /* check for a valid IP frame */
3319             if (!(cqe->fast_path_cqe.status_flags &
3320                   ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3321                 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3322                 if (__predict_false(cqe_fp_flags &
3323                                     ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3324                     fp->eth_q_stats.rx_hw_csum_errors++;
3325                 } else {
3326                     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3327                     m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3328                 }
3329             }
3330 
3331             /* check for a valid TCP/UDP frame */
3332             if (!(cqe->fast_path_cqe.status_flags &
3333                   ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3334                 if (__predict_false(cqe_fp_flags &
3335                                     ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3336                     fp->eth_q_stats.rx_hw_csum_errors++;
3337                 } else {
3338                     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3339                     m->m_pkthdr.csum_data = 0xFFFF;
3340                     m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3341                                                CSUM_PSEUDO_HDR);
3342                 }
3343             }
3344         }
3345 
3346         /* if there is a VLAN tag then flag that info */
3347         if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3348             m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3349             m->m_flags |= M_VLANTAG;
3350         }
3351 
3352 #if __FreeBSD_version >= 800000
3353         /* specify what RSS queue was used for this flow */
3354         m->m_pkthdr.flowid = fp->index;
3355         M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
3356 #endif
3357 
3358 next_rx:
3359 
3360         bd_cons    = RX_BD_NEXT(bd_cons);
3361         bd_prod    = RX_BD_NEXT(bd_prod);
3362         bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3363 
3364         /* pass the frame to the stack */
3365         if (__predict_true(m != NULL)) {
3366             if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3367             rx_pkts++;
3368             if_input(ifp, m);
3369         }
3370 
3371 next_cqe:
3372 
3373         sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3374         sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3375 
3376         /* limit spinning on the queue */
3377         if (rc != 0)
3378             break;
3379 
3380         if (rx_pkts == sc->rx_budget) {
3381             fp->eth_q_stats.rx_budget_reached++;
3382             break;
3383         }
3384     } /* while work to do */
3385 
3386     fp->rx_bd_cons = bd_cons;
3387     fp->rx_bd_prod = bd_prod_fw;
3388     fp->rx_cq_cons = sw_cq_cons;
3389     fp->rx_cq_prod = sw_cq_prod;
3390 
3391     /* Update producers */
3392     bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3393 
3394     fp->eth_q_stats.rx_pkts += rx_pkts;
3395     fp->eth_q_stats.rx_calls++;
3396 
3397     BXE_FP_RX_UNLOCK(fp);
3398 
3399     return (sw_cq_cons != hw_cq_cons);
3400 }
3401 
3402 static uint16_t
3403 bxe_free_tx_pkt(struct bxe_softc    *sc,
3404                 struct bxe_fastpath *fp,
3405                 uint16_t            idx)
3406 {
3407     struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3408     struct eth_tx_start_bd *tx_start_bd;
3409     uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3410     uint16_t new_cons;
3411     int nbd;
3412 
3413     /* unmap the mbuf from non-paged memory */
3414     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3415 
3416     tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3417     nbd = le16toh(tx_start_bd->nbd) - 1;
3418 
3419     new_cons = (tx_buf->first_bd + nbd);
3420 
3421     /* free the mbuf */
3422     if (__predict_true(tx_buf->m != NULL)) {
3423         m_freem(tx_buf->m);
3424         fp->eth_q_stats.mbuf_alloc_tx--;
3425     } else {
3426         fp->eth_q_stats.tx_chain_lost_mbuf++;
3427     }
3428 
3429     tx_buf->m = NULL;
3430     tx_buf->first_bd = 0;
3431 
3432     return (new_cons);
3433 }
3434 
3435 /* transmit timeout watchdog */
3436 static int
3437 bxe_watchdog(struct bxe_softc    *sc,
3438              struct bxe_fastpath *fp)
3439 {
3440     BXE_FP_TX_LOCK(fp);
3441 
3442     if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3443         BXE_FP_TX_UNLOCK(fp);
3444         return (0);
3445     }
3446 
3447     BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3448 
3449     BXE_FP_TX_UNLOCK(fp);
3450 
3451     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_REINIT);
3452     taskqueue_enqueue(sc->chip_tq, &sc->chip_tq_task);
3453 
3454     return (-1);
3455 }
3456 
3457 /* processes transmit completions */
3458 static uint8_t
3459 bxe_txeof(struct bxe_softc    *sc,
3460           struct bxe_fastpath *fp)
3461 {
3462     if_t ifp = sc->ifp;
3463     uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3464     uint16_t tx_bd_avail;
3465 
3466     BXE_FP_TX_LOCK_ASSERT(fp);
3467 
3468     bd_cons = fp->tx_bd_cons;
3469     hw_cons = le16toh(*fp->tx_cons_sb);
3470     sw_cons = fp->tx_pkt_cons;
3471 
3472     while (sw_cons != hw_cons) {
3473         pkt_cons = TX_BD(sw_cons);
3474 
3475         BLOGD(sc, DBG_TX,
3476               "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3477               fp->index, hw_cons, sw_cons, pkt_cons);
3478 
3479         bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3480 
3481         sw_cons++;
3482     }
3483 
3484     fp->tx_pkt_cons = sw_cons;
3485     fp->tx_bd_cons  = bd_cons;
3486 
3487     BLOGD(sc, DBG_TX,
3488           "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3489           fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3490 
3491     mb();
3492 
3493     tx_bd_avail = bxe_tx_avail(sc, fp);
3494 
3495     if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3496         if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
3497     } else {
3498         if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3499     }
3500 
3501     if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3502         /* reset the watchdog timer if there are pending transmits */
3503         fp->watchdog_timer = BXE_TX_TIMEOUT;
3504         return (TRUE);
3505     } else {
3506         /* clear watchdog when there are no pending transmits */
3507         fp->watchdog_timer = 0;
3508         return (FALSE);
3509     }
3510 }
3511 
3512 static void
3513 bxe_drain_tx_queues(struct bxe_softc *sc)
3514 {
3515     struct bxe_fastpath *fp;
3516     int i, count;
3517 
3518     /* wait until all TX fastpath tasks have completed */
3519     for (i = 0; i < sc->num_queues; i++) {
3520         fp = &sc->fp[i];
3521 
3522         count = 1000;
3523 
3524         while (bxe_has_tx_work(fp)) {
3525 
3526             BXE_FP_TX_LOCK(fp);
3527             bxe_txeof(sc, fp);
3528             BXE_FP_TX_UNLOCK(fp);
3529 
3530             if (count == 0) {
3531                 BLOGE(sc, "Timeout waiting for fp[%d] "
3532                           "transmits to complete!\n", i);
3533                 bxe_panic(sc, ("tx drain failure\n"));
3534                 return;
3535             }
3536 
3537             count--;
3538             DELAY(1000);
3539             rmb();
3540         }
3541     }
3542 
3543     return;
3544 }
3545 
3546 static int
3547 bxe_del_all_macs(struct bxe_softc          *sc,
3548                  struct ecore_vlan_mac_obj *mac_obj,
3549                  int                       mac_type,
3550                  uint8_t                   wait_for_comp)
3551 {
3552     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3553     int rc;
3554 
3555     /* wait for completion of requested */
3556     if (wait_for_comp) {
3557         bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3558     }
3559 
3560     /* Set the mac type of addresses we want to clear */
3561     bxe_set_bit(mac_type, &vlan_mac_flags);
3562 
3563     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3564     if (rc < 0) {
3565         BLOGE(sc, "Failed to delete MACs (%d) mac_type %d wait_for_comp 0x%x\n",
3566             rc, mac_type, wait_for_comp);
3567     }
3568 
3569     return (rc);
3570 }
3571 
3572 static int
3573 bxe_fill_accept_flags(struct bxe_softc *sc,
3574                       uint32_t         rx_mode,
3575                       unsigned long    *rx_accept_flags,
3576                       unsigned long    *tx_accept_flags)
3577 {
3578     /* Clear the flags first */
3579     *rx_accept_flags = 0;
3580     *tx_accept_flags = 0;
3581 
3582     switch (rx_mode) {
3583     case BXE_RX_MODE_NONE:
3584         /*
3585          * 'drop all' supersedes any accept flags that may have been
3586          * passed to the function.
3587          */
3588         break;
3589 
3590     case BXE_RX_MODE_NORMAL:
3591         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3592         bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3593         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3594 
3595         /* internal switching mode */
3596         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3597         bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3598         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3599 
3600         break;
3601 
3602     case BXE_RX_MODE_ALLMULTI:
3603         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3604         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3605         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3606 
3607         /* internal switching mode */
3608         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3609         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3610         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3611 
3612         break;
3613 
3614     case BXE_RX_MODE_PROMISC:
3615         /*
3616          * According to deffinition of SI mode, iface in promisc mode
3617          * should receive matched and unmatched (in resolution of port)
3618          * unicast packets.
3619          */
3620         bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3621         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3622         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3623         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3624 
3625         /* internal switching mode */
3626         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3627         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3628 
3629         if (IS_MF_SI(sc)) {
3630             bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3631         } else {
3632             bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3633         }
3634 
3635         break;
3636 
3637     default:
3638         BLOGE(sc, "Unknown rx_mode (0x%x)\n", rx_mode);
3639         return (-1);
3640     }
3641 
3642     /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3643     if (rx_mode != BXE_RX_MODE_NONE) {
3644         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3645         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3646     }
3647 
3648     return (0);
3649 }
3650 
3651 static int
3652 bxe_set_q_rx_mode(struct bxe_softc *sc,
3653                   uint8_t          cl_id,
3654                   unsigned long    rx_mode_flags,
3655                   unsigned long    rx_accept_flags,
3656                   unsigned long    tx_accept_flags,
3657                   unsigned long    ramrod_flags)
3658 {
3659     struct ecore_rx_mode_ramrod_params ramrod_param;
3660     int rc;
3661 
3662     memset(&ramrod_param, 0, sizeof(ramrod_param));
3663 
3664     /* Prepare ramrod parameters */
3665     ramrod_param.cid = 0;
3666     ramrod_param.cl_id = cl_id;
3667     ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3668     ramrod_param.func_id = SC_FUNC(sc);
3669 
3670     ramrod_param.pstate = &sc->sp_state;
3671     ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3672 
3673     ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3674     ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3675 
3676     bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3677 
3678     ramrod_param.ramrod_flags = ramrod_flags;
3679     ramrod_param.rx_mode_flags = rx_mode_flags;
3680 
3681     ramrod_param.rx_accept_flags = rx_accept_flags;
3682     ramrod_param.tx_accept_flags = tx_accept_flags;
3683 
3684     rc = ecore_config_rx_mode(sc, &ramrod_param);
3685     if (rc < 0) {
3686         BLOGE(sc, "Set rx_mode %d cli_id 0x%x rx_mode_flags 0x%x "
3687             "rx_accept_flags 0x%x tx_accept_flags 0x%x "
3688             "ramrod_flags 0x%x rc %d failed\n", sc->rx_mode, cl_id,
3689             (uint32_t)rx_mode_flags, (uint32_t)rx_accept_flags,
3690             (uint32_t)tx_accept_flags, (uint32_t)ramrod_flags, rc);
3691         return (rc);
3692     }
3693 
3694     return (0);
3695 }
3696 
3697 static int
3698 bxe_set_storm_rx_mode(struct bxe_softc *sc)
3699 {
3700     unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3701     unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3702     int rc;
3703 
3704     rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3705                                &tx_accept_flags);
3706     if (rc) {
3707         return (rc);
3708     }
3709 
3710     bxe_set_bit(RAMROD_RX, &ramrod_flags);
3711     bxe_set_bit(RAMROD_TX, &ramrod_flags);
3712 
3713     /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3714     return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3715                               rx_accept_flags, tx_accept_flags,
3716                               ramrod_flags));
3717 }
3718 
3719 /* returns the "mcp load_code" according to global load_count array */
3720 static int
3721 bxe_nic_load_no_mcp(struct bxe_softc *sc)
3722 {
3723     int path = SC_PATH(sc);
3724     int port = SC_PORT(sc);
3725 
3726     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3727           path, load_count[path][0], load_count[path][1],
3728           load_count[path][2]);
3729     load_count[path][0]++;
3730     load_count[path][1 + port]++;
3731     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3732           path, load_count[path][0], load_count[path][1],
3733           load_count[path][2]);
3734     if (load_count[path][0] == 1) {
3735         return (FW_MSG_CODE_DRV_LOAD_COMMON);
3736     } else if (load_count[path][1 + port] == 1) {
3737         return (FW_MSG_CODE_DRV_LOAD_PORT);
3738     } else {
3739         return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3740     }
3741 }
3742 
3743 /* returns the "mcp load_code" according to global load_count array */
3744 static int
3745 bxe_nic_unload_no_mcp(struct bxe_softc *sc)
3746 {
3747     int port = SC_PORT(sc);
3748     int path = SC_PATH(sc);
3749 
3750     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3751           path, load_count[path][0], load_count[path][1],
3752           load_count[path][2]);
3753     load_count[path][0]--;
3754     load_count[path][1 + port]--;
3755     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3756           path, load_count[path][0], load_count[path][1],
3757           load_count[path][2]);
3758     if (load_count[path][0] == 0) {
3759         return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3760     } else if (load_count[path][1 + port] == 0) {
3761         return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3762     } else {
3763         return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3764     }
3765 }
3766 
3767 /* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3768 static uint32_t
3769 bxe_send_unload_req(struct bxe_softc *sc,
3770                     int              unload_mode)
3771 {
3772     uint32_t reset_code = 0;
3773 
3774     /* Select the UNLOAD request mode */
3775     if (unload_mode == UNLOAD_NORMAL) {
3776         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3777     } else {
3778         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3779     }
3780 
3781     /* Send the request to the MCP */
3782     if (!BXE_NOMCP(sc)) {
3783         reset_code = bxe_fw_command(sc, reset_code, 0);
3784     } else {
3785         reset_code = bxe_nic_unload_no_mcp(sc);
3786     }
3787 
3788     return (reset_code);
3789 }
3790 
3791 /* send UNLOAD_DONE command to the MCP */
3792 static void
3793 bxe_send_unload_done(struct bxe_softc *sc,
3794                      uint8_t          keep_link)
3795 {
3796     uint32_t reset_param =
3797         keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
3798 
3799     /* Report UNLOAD_DONE to MCP */
3800     if (!BXE_NOMCP(sc)) {
3801         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
3802     }
3803 }
3804 
3805 static int
3806 bxe_func_wait_started(struct bxe_softc *sc)
3807 {
3808     int tout = 50;
3809 
3810     if (!sc->port.pmf) {
3811         return (0);
3812     }
3813 
3814     /*
3815      * (assumption: No Attention from MCP at this stage)
3816      * PMF probably in the middle of TX disable/enable transaction
3817      * 1. Sync IRS for default SB
3818      * 2. Sync SP queue - this guarantees us that attention handling started
3819      * 3. Wait, that TX disable/enable transaction completes
3820      *
3821      * 1+2 guarantee that if DCBX attention was scheduled it already changed
3822      * pending bit of transaction from STARTED-->TX_STOPPED, if we already
3823      * received completion for the transaction the state is TX_STOPPED.
3824      * State will return to STARTED after completion of TX_STOPPED-->STARTED
3825      * transaction.
3826      */
3827 
3828     /* XXX make sure default SB ISR is done */
3829     /* need a way to synchronize an irq (intr_mtx?) */
3830 
3831     /* XXX flush any work queues */
3832 
3833     while (ecore_func_get_state(sc, &sc->func_obj) !=
3834            ECORE_F_STATE_STARTED && tout--) {
3835         DELAY(20000);
3836     }
3837 
3838     if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
3839         /*
3840          * Failed to complete the transaction in a "good way"
3841          * Force both transactions with CLR bit.
3842          */
3843         struct ecore_func_state_params func_params = { NULL };
3844 
3845         BLOGE(sc, "Unexpected function state! "
3846                   "Forcing STARTED-->TX_STOPPED-->STARTED\n");
3847 
3848         func_params.f_obj = &sc->func_obj;
3849         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3850 
3851         /* STARTED-->TX_STOPPED */
3852         func_params.cmd = ECORE_F_CMD_TX_STOP;
3853         ecore_func_state_change(sc, &func_params);
3854 
3855         /* TX_STOPPED-->STARTED */
3856         func_params.cmd = ECORE_F_CMD_TX_START;
3857         return (ecore_func_state_change(sc, &func_params));
3858     }
3859 
3860     return (0);
3861 }
3862 
3863 static int
3864 bxe_stop_queue(struct bxe_softc *sc,
3865                int              index)
3866 {
3867     struct bxe_fastpath *fp = &sc->fp[index];
3868     struct ecore_queue_state_params q_params = { NULL };
3869     int rc;
3870 
3871     BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
3872 
3873     q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
3874     /* We want to wait for completion in this context */
3875     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
3876 
3877     /* Stop the primary connection: */
3878 
3879     /* ...halt the connection */
3880     q_params.cmd = ECORE_Q_CMD_HALT;
3881     rc = ecore_queue_state_change(sc, &q_params);
3882     if (rc) {
3883         return (rc);
3884     }
3885 
3886     /* ...terminate the connection */
3887     q_params.cmd = ECORE_Q_CMD_TERMINATE;
3888     memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
3889     q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
3890     rc = ecore_queue_state_change(sc, &q_params);
3891     if (rc) {
3892         return (rc);
3893     }
3894 
3895     /* ...delete cfc entry */
3896     q_params.cmd = ECORE_Q_CMD_CFC_DEL;
3897     memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
3898     q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
3899     return (ecore_queue_state_change(sc, &q_params));
3900 }
3901 
3902 /* wait for the outstanding SP commands */
3903 static inline uint8_t
3904 bxe_wait_sp_comp(struct bxe_softc *sc,
3905                  unsigned long    mask)
3906 {
3907     unsigned long tmp;
3908     int tout = 5000; /* wait for 5 secs tops */
3909 
3910     while (tout--) {
3911         mb();
3912         if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
3913             return (TRUE);
3914         }
3915 
3916         DELAY(1000);
3917     }
3918 
3919     mb();
3920 
3921     tmp = atomic_load_acq_long(&sc->sp_state);
3922     if (tmp & mask) {
3923         BLOGE(sc, "Filtering completion timed out: "
3924                   "sp_state 0x%lx, mask 0x%lx\n",
3925               tmp, mask);
3926         return (FALSE);
3927     }
3928 
3929     return (FALSE);
3930 }
3931 
3932 static int
3933 bxe_func_stop(struct bxe_softc *sc)
3934 {
3935     struct ecore_func_state_params func_params = { NULL };
3936     int rc;
3937 
3938     /* prepare parameters for function state transitions */
3939     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3940     func_params.f_obj = &sc->func_obj;
3941     func_params.cmd = ECORE_F_CMD_STOP;
3942 
3943     /*
3944      * Try to stop the function the 'good way'. If it fails (in case
3945      * of a parity error during bxe_chip_cleanup()) and we are
3946      * not in a debug mode, perform a state transaction in order to
3947      * enable further HW_RESET transaction.
3948      */
3949     rc = ecore_func_state_change(sc, &func_params);
3950     if (rc) {
3951         BLOGE(sc, "FUNC_STOP ramrod failed. "
3952                   "Running a dry transaction (%d)\n", rc);
3953         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3954         return (ecore_func_state_change(sc, &func_params));
3955     }
3956 
3957     return (0);
3958 }
3959 
3960 static int
3961 bxe_reset_hw(struct bxe_softc *sc,
3962              uint32_t         load_code)
3963 {
3964     struct ecore_func_state_params func_params = { NULL };
3965 
3966     /* Prepare parameters for function state transitions */
3967     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3968 
3969     func_params.f_obj = &sc->func_obj;
3970     func_params.cmd = ECORE_F_CMD_HW_RESET;
3971 
3972     func_params.params.hw_init.load_phase = load_code;
3973 
3974     return (ecore_func_state_change(sc, &func_params));
3975 }
3976 
3977 static void
3978 bxe_int_disable_sync(struct bxe_softc *sc,
3979                      int              disable_hw)
3980 {
3981     if (disable_hw) {
3982         /* prevent the HW from sending interrupts */
3983         bxe_int_disable(sc);
3984     }
3985 
3986     /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
3987     /* make sure all ISRs are done */
3988 
3989     /* XXX make sure sp_task is not running */
3990     /* cancel and flush work queues */
3991 }
3992 
3993 static void
3994 bxe_chip_cleanup(struct bxe_softc *sc,
3995                  uint32_t         unload_mode,
3996                  uint8_t          keep_link)
3997 {
3998     int port = SC_PORT(sc);
3999     struct ecore_mcast_ramrod_params rparam = { NULL };
4000     uint32_t reset_code;
4001     int i, rc = 0;
4002 
4003     bxe_drain_tx_queues(sc);
4004 
4005     /* give HW time to discard old tx messages */
4006     DELAY(1000);
4007 
4008     /* Clean all ETH MACs */
4009     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4010     if (rc < 0) {
4011         BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4012     }
4013 
4014     /* Clean up UC list  */
4015     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4016     if (rc < 0) {
4017         BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4018     }
4019 
4020     /* Disable LLH */
4021     if (!CHIP_IS_E1(sc)) {
4022         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4023     }
4024 
4025     /* Set "drop all" to stop Rx */
4026 
4027     /*
4028      * We need to take the BXE_MCAST_LOCK() here in order to prevent
4029      * a race between the completion code and this code.
4030      */
4031     BXE_MCAST_LOCK(sc);
4032 
4033     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4034         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4035     } else {
4036         bxe_set_storm_rx_mode(sc);
4037     }
4038 
4039     /* Clean up multicast configuration */
4040     rparam.mcast_obj = &sc->mcast_obj;
4041     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4042     if (rc < 0) {
4043         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4044     }
4045 
4046     BXE_MCAST_UNLOCK(sc);
4047 
4048     // XXX bxe_iov_chip_cleanup(sc);
4049 
4050     /*
4051      * Send the UNLOAD_REQUEST to the MCP. This will return if
4052      * this function should perform FUNCTION, PORT, or COMMON HW
4053      * reset.
4054      */
4055     reset_code = bxe_send_unload_req(sc, unload_mode);
4056 
4057     /*
4058      * (assumption: No Attention from MCP at this stage)
4059      * PMF probably in the middle of TX disable/enable transaction
4060      */
4061     rc = bxe_func_wait_started(sc);
4062     if (rc) {
4063         BLOGE(sc, "bxe_func_wait_started failed (%d)\n", rc);
4064     }
4065 
4066     /*
4067      * Close multi and leading connections
4068      * Completions for ramrods are collected in a synchronous way
4069      */
4070     for (i = 0; i < sc->num_queues; i++) {
4071         if (bxe_stop_queue(sc, i)) {
4072             goto unload_error;
4073         }
4074     }
4075 
4076     /*
4077      * If SP settings didn't get completed so far - something
4078      * very wrong has happen.
4079      */
4080     if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4081         BLOGE(sc, "Common slow path ramrods got stuck!(%d)\n", rc);
4082     }
4083 
4084 unload_error:
4085 
4086     rc = bxe_func_stop(sc);
4087     if (rc) {
4088         BLOGE(sc, "Function stop failed!(%d)\n", rc);
4089     }
4090 
4091     /* disable HW interrupts */
4092     bxe_int_disable_sync(sc, TRUE);
4093 
4094     /* detach interrupts */
4095     bxe_interrupt_detach(sc);
4096 
4097     /* Reset the chip */
4098     rc = bxe_reset_hw(sc, reset_code);
4099     if (rc) {
4100         BLOGE(sc, "Hardware reset failed(%d)\n", rc);
4101     }
4102 
4103     /* Report UNLOAD_DONE to MCP */
4104     bxe_send_unload_done(sc, keep_link);
4105 }
4106 
4107 static void
4108 bxe_disable_close_the_gate(struct bxe_softc *sc)
4109 {
4110     uint32_t val;
4111     int port = SC_PORT(sc);
4112 
4113     BLOGD(sc, DBG_LOAD,
4114           "Disabling 'close the gates'\n");
4115 
4116     if (CHIP_IS_E1(sc)) {
4117         uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4118                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
4119         val = REG_RD(sc, addr);
4120         val &= ~(0x300);
4121         REG_WR(sc, addr, val);
4122     } else {
4123         val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4124         val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4125                  MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4126         REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4127     }
4128 }
4129 
4130 /*
4131  * Cleans the object that have internal lists without sending
4132  * ramrods. Should be run when interrutps are disabled.
4133  */
4134 static void
4135 bxe_squeeze_objects(struct bxe_softc *sc)
4136 {
4137     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4138     struct ecore_mcast_ramrod_params rparam = { NULL };
4139     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4140     int rc;
4141 
4142     /* Cleanup MACs' object first... */
4143 
4144     /* Wait for completion of requested */
4145     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4146     /* Perform a dry cleanup */
4147     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4148 
4149     /* Clean ETH primary MAC */
4150     bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4151     rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4152                              &ramrod_flags);
4153     if (rc != 0) {
4154         BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4155     }
4156 
4157     /* Cleanup UC list */
4158     vlan_mac_flags = 0;
4159     bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4160     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4161                              &ramrod_flags);
4162     if (rc != 0) {
4163         BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4164     }
4165 
4166     /* Now clean mcast object... */
4167 
4168     rparam.mcast_obj = &sc->mcast_obj;
4169     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4170 
4171     /* Add a DEL command... */
4172     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4173     if (rc < 0) {
4174         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4175     }
4176 
4177     /* now wait until all pending commands are cleared */
4178 
4179     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4180     while (rc != 0) {
4181         if (rc < 0) {
4182             BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4183             return;
4184         }
4185 
4186         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4187     }
4188 }
4189 
4190 /* stop the controller */
4191 static __noinline int
4192 bxe_nic_unload(struct bxe_softc *sc,
4193                uint32_t         unload_mode,
4194                uint8_t          keep_link)
4195 {
4196     uint8_t global = FALSE;
4197     uint32_t val;
4198     int i;
4199 
4200     BXE_CORE_LOCK_ASSERT(sc);
4201 
4202     if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
4203 
4204     for (i = 0; i < sc->num_queues; i++) {
4205         struct bxe_fastpath *fp;
4206 
4207         fp = &sc->fp[i];
4208         BXE_FP_TX_LOCK(fp);
4209         BXE_FP_TX_UNLOCK(fp);
4210     }
4211 
4212     BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4213 
4214     /* mark driver as unloaded in shmem2 */
4215     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4216         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4217         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4218                   val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4219     }
4220 
4221     if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4222         (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4223         /*
4224          * We can get here if the driver has been unloaded
4225          * during parity error recovery and is either waiting for a
4226          * leader to complete or for other functions to unload and
4227          * then ifconfig down has been issued. In this case we want to
4228          * unload and let other functions to complete a recovery
4229          * process.
4230          */
4231         sc->recovery_state = BXE_RECOVERY_DONE;
4232         sc->is_leader = 0;
4233         bxe_release_leader_lock(sc);
4234         mb();
4235 
4236         BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4237         BLOGE(sc, "Can't unload in closed or error state recover_state 0x%x"
4238             " state = 0x%x\n", sc->recovery_state, sc->state);
4239         return (-1);
4240     }
4241 
4242     /*
4243      * Nothing to do during unload if previous bxe_nic_load()
4244      * did not completed succesfully - all resourses are released.
4245      */
4246     if ((sc->state == BXE_STATE_CLOSED) ||
4247         (sc->state == BXE_STATE_ERROR)) {
4248         return (0);
4249     }
4250 
4251     sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4252     mb();
4253 
4254     /* stop tx */
4255     bxe_tx_disable(sc);
4256 
4257     sc->rx_mode = BXE_RX_MODE_NONE;
4258     /* XXX set rx mode ??? */
4259 
4260     if (IS_PF(sc) && !sc->grcdump_done) {
4261         /* set ALWAYS_ALIVE bit in shmem */
4262         sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4263 
4264         bxe_drv_pulse(sc);
4265 
4266         bxe_stats_handle(sc, STATS_EVENT_STOP);
4267         bxe_save_statistics(sc);
4268     }
4269 
4270     /* wait till consumers catch up with producers in all queues */
4271     bxe_drain_tx_queues(sc);
4272 
4273     /* if VF indicate to PF this function is going down (PF will delete sp
4274      * elements and clear initializations
4275      */
4276     if (IS_VF(sc)) {
4277         ; /* bxe_vfpf_close_vf(sc); */
4278     } else if (unload_mode != UNLOAD_RECOVERY) {
4279         /* if this is a normal/close unload need to clean up chip */
4280         if (!sc->grcdump_done)
4281             bxe_chip_cleanup(sc, unload_mode, keep_link);
4282     } else {
4283         /* Send the UNLOAD_REQUEST to the MCP */
4284         bxe_send_unload_req(sc, unload_mode);
4285 
4286         /*
4287          * Prevent transactions to host from the functions on the
4288          * engine that doesn't reset global blocks in case of global
4289          * attention once gloabl blocks are reset and gates are opened
4290          * (the engine which leader will perform the recovery
4291          * last).
4292          */
4293         if (!CHIP_IS_E1x(sc)) {
4294             bxe_pf_disable(sc);
4295         }
4296 
4297         /* disable HW interrupts */
4298         bxe_int_disable_sync(sc, TRUE);
4299 
4300         /* detach interrupts */
4301         bxe_interrupt_detach(sc);
4302 
4303         /* Report UNLOAD_DONE to MCP */
4304         bxe_send_unload_done(sc, FALSE);
4305     }
4306 
4307     /*
4308      * At this stage no more interrupts will arrive so we may safely clean
4309      * the queue'able objects here in case they failed to get cleaned so far.
4310      */
4311     if (IS_PF(sc)) {
4312         bxe_squeeze_objects(sc);
4313     }
4314 
4315     /* There should be no more pending SP commands at this stage */
4316     sc->sp_state = 0;
4317 
4318     sc->port.pmf = 0;
4319 
4320     bxe_free_fp_buffers(sc);
4321 
4322     if (IS_PF(sc)) {
4323         bxe_free_mem(sc);
4324     }
4325 
4326     bxe_free_fw_stats_mem(sc);
4327 
4328     sc->state = BXE_STATE_CLOSED;
4329 
4330     /*
4331      * Check if there are pending parity attentions. If there are - set
4332      * RECOVERY_IN_PROGRESS.
4333      */
4334     if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4335         bxe_set_reset_in_progress(sc);
4336 
4337         /* Set RESET_IS_GLOBAL if needed */
4338         if (global) {
4339             bxe_set_reset_global(sc);
4340         }
4341     }
4342 
4343     /*
4344      * The last driver must disable a "close the gate" if there is no
4345      * parity attention or "process kill" pending.
4346      */
4347     if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4348         bxe_reset_is_done(sc, SC_PATH(sc))) {
4349         bxe_disable_close_the_gate(sc);
4350     }
4351 
4352     BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4353 
4354     return (0);
4355 }
4356 
4357 /*
4358  * Called by the OS to set various media options (i.e. link, speed, etc.) when
4359  * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4360  */
4361 static int
4362 bxe_ifmedia_update(struct ifnet  *ifp)
4363 {
4364     struct bxe_softc *sc = (struct bxe_softc *)if_getsoftc(ifp);
4365     struct ifmedia *ifm;
4366 
4367     ifm = &sc->ifmedia;
4368 
4369     /* We only support Ethernet media type. */
4370     if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4371         return (EINVAL);
4372     }
4373 
4374     switch (IFM_SUBTYPE(ifm->ifm_media)) {
4375     case IFM_AUTO:
4376          break;
4377     case IFM_10G_CX4:
4378     case IFM_10G_SR:
4379     case IFM_10G_T:
4380     case IFM_10G_TWINAX:
4381     default:
4382         /* We don't support changing the media type. */
4383         BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4384               IFM_SUBTYPE(ifm->ifm_media));
4385         return (EINVAL);
4386     }
4387 
4388     return (0);
4389 }
4390 
4391 /*
4392  * Called by the OS to get the current media status (i.e. link, speed, etc.).
4393  */
4394 static void
4395 bxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4396 {
4397     struct bxe_softc *sc = if_getsoftc(ifp);
4398 
4399     /* Report link down if the driver isn't running. */
4400     if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
4401         ifmr->ifm_active |= IFM_NONE;
4402         return;
4403     }
4404 
4405     /* Setup the default interface info. */
4406     ifmr->ifm_status = IFM_AVALID;
4407     ifmr->ifm_active = IFM_ETHER;
4408 
4409     if (sc->link_vars.link_up) {
4410         ifmr->ifm_status |= IFM_ACTIVE;
4411     } else {
4412         ifmr->ifm_active |= IFM_NONE;
4413         return;
4414     }
4415 
4416     ifmr->ifm_active |= sc->media;
4417 
4418     if (sc->link_vars.duplex == DUPLEX_FULL) {
4419         ifmr->ifm_active |= IFM_FDX;
4420     } else {
4421         ifmr->ifm_active |= IFM_HDX;
4422     }
4423 }
4424 
4425 static int
4426 bxe_ioctl_nvram(struct bxe_softc *sc,
4427                 uint32_t         priv_op,
4428                 struct ifreq     *ifr)
4429 {
4430     struct bxe_nvram_data nvdata_base;
4431     struct bxe_nvram_data *nvdata;
4432     int len;
4433     int error = 0;
4434 
4435     copyin(ifr->ifr_data, &nvdata_base, sizeof(nvdata_base));
4436 
4437     len = (sizeof(struct bxe_nvram_data) +
4438            nvdata_base.len -
4439            sizeof(uint32_t));
4440 
4441     if (len > sizeof(struct bxe_nvram_data)) {
4442         if ((nvdata = (struct bxe_nvram_data *)
4443                  malloc(len, M_DEVBUF,
4444                         (M_NOWAIT | M_ZERO))) == NULL) {
4445             BLOGE(sc, "BXE_IOC_RD_NVRAM malloc failed priv_op 0x%x "
4446                 " len = 0x%x\n", priv_op, len);
4447             return (1);
4448         }
4449         memcpy(nvdata, &nvdata_base, sizeof(struct bxe_nvram_data));
4450     } else {
4451         nvdata = &nvdata_base;
4452     }
4453 
4454     if (priv_op == BXE_IOC_RD_NVRAM) {
4455         BLOGD(sc, DBG_IOCTL, "IOC_RD_NVRAM 0x%x %d\n",
4456               nvdata->offset, nvdata->len);
4457         error = bxe_nvram_read(sc,
4458                                nvdata->offset,
4459                                (uint8_t *)nvdata->value,
4460                                nvdata->len);
4461         copyout(nvdata, ifr->ifr_data, len);
4462     } else { /* BXE_IOC_WR_NVRAM */
4463         BLOGD(sc, DBG_IOCTL, "IOC_WR_NVRAM 0x%x %d\n",
4464               nvdata->offset, nvdata->len);
4465         copyin(ifr->ifr_data, nvdata, len);
4466         error = bxe_nvram_write(sc,
4467                                 nvdata->offset,
4468                                 (uint8_t *)nvdata->value,
4469                                 nvdata->len);
4470     }
4471 
4472     if (len > sizeof(struct bxe_nvram_data)) {
4473         free(nvdata, M_DEVBUF);
4474     }
4475 
4476     return (error);
4477 }
4478 
4479 static int
4480 bxe_ioctl_stats_show(struct bxe_softc *sc,
4481                      uint32_t         priv_op,
4482                      struct ifreq     *ifr)
4483 {
4484     const size_t str_size   = (BXE_NUM_ETH_STATS * STAT_NAME_LEN);
4485     const size_t stats_size = (BXE_NUM_ETH_STATS * sizeof(uint64_t));
4486     caddr_t p_tmp;
4487     uint32_t *offset;
4488     int i;
4489 
4490     switch (priv_op)
4491     {
4492     case BXE_IOC_STATS_SHOW_NUM:
4493         memset(ifr->ifr_data, 0, sizeof(union bxe_stats_show_data));
4494         ((union bxe_stats_show_data *)ifr->ifr_data)->desc.num =
4495             BXE_NUM_ETH_STATS;
4496         ((union bxe_stats_show_data *)ifr->ifr_data)->desc.len =
4497             STAT_NAME_LEN;
4498         return (0);
4499 
4500     case BXE_IOC_STATS_SHOW_STR:
4501         memset(ifr->ifr_data, 0, str_size);
4502         p_tmp = ifr->ifr_data;
4503         for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4504             strcpy(p_tmp, bxe_eth_stats_arr[i].string);
4505             p_tmp += STAT_NAME_LEN;
4506         }
4507         return (0);
4508 
4509     case BXE_IOC_STATS_SHOW_CNT:
4510         memset(ifr->ifr_data, 0, stats_size);
4511         p_tmp = ifr->ifr_data;
4512         for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4513             offset = ((uint32_t *)&sc->eth_stats +
4514                       bxe_eth_stats_arr[i].offset);
4515             switch (bxe_eth_stats_arr[i].size) {
4516             case 4:
4517                 *((uint64_t *)p_tmp) = (uint64_t)*offset;
4518                 break;
4519             case 8:
4520                 *((uint64_t *)p_tmp) = HILO_U64(*offset, *(offset + 1));
4521                 break;
4522             default:
4523                 *((uint64_t *)p_tmp) = 0;
4524             }
4525             p_tmp += sizeof(uint64_t);
4526         }
4527         return (0);
4528 
4529     default:
4530         return (-1);
4531     }
4532 }
4533 
4534 static void
4535 bxe_handle_chip_tq(void *context,
4536                    int  pending)
4537 {
4538     struct bxe_softc *sc = (struct bxe_softc *)context;
4539     long work = atomic_load_acq_long(&sc->chip_tq_flags);
4540 
4541     switch (work)
4542     {
4543 
4544     case CHIP_TQ_REINIT:
4545         if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4546             /* restart the interface */
4547             BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4548             bxe_periodic_stop(sc);
4549             BXE_CORE_LOCK(sc);
4550             bxe_stop_locked(sc);
4551             bxe_init_locked(sc);
4552             BXE_CORE_UNLOCK(sc);
4553         }
4554         break;
4555 
4556     default:
4557         break;
4558     }
4559 }
4560 
4561 /*
4562  * Handles any IOCTL calls from the operating system.
4563  *
4564  * Returns:
4565  *   0 = Success, >0 Failure
4566  */
4567 static int
4568 bxe_ioctl(if_t ifp,
4569           u_long       command,
4570           caddr_t      data)
4571 {
4572     struct bxe_softc *sc = if_getsoftc(ifp);
4573     struct ifreq *ifr = (struct ifreq *)data;
4574     struct bxe_nvram_data *nvdata;
4575     uint32_t priv_op;
4576     int mask = 0;
4577     int reinit = 0;
4578     int error = 0;
4579 
4580     int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4581     int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4582 
4583     switch (command)
4584     {
4585     case SIOCSIFMTU:
4586         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4587               ifr->ifr_mtu);
4588 
4589         if (sc->mtu == ifr->ifr_mtu) {
4590             /* nothing to change */
4591             break;
4592         }
4593 
4594         if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4595             BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4596                   ifr->ifr_mtu, mtu_min, mtu_max);
4597             error = EINVAL;
4598             break;
4599         }
4600 
4601         atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4602                              (unsigned long)ifr->ifr_mtu);
4603 	/*
4604         atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4605                               (unsigned long)ifr->ifr_mtu);
4606 	XXX - Not sure why it needs to be atomic
4607 	*/
4608 	if_setmtu(ifp, ifr->ifr_mtu);
4609         reinit = 1;
4610         break;
4611 
4612     case SIOCSIFFLAGS:
4613         /* toggle the interface state up or down */
4614         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4615 
4616 	BXE_CORE_LOCK(sc);
4617         /* check if the interface is up */
4618         if (if_getflags(ifp) & IFF_UP) {
4619             if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4620                 /* set the receive mode flags */
4621                 bxe_set_rx_mode(sc);
4622             } else {
4623 		bxe_init_locked(sc);
4624             }
4625         } else {
4626             if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4627 		bxe_periodic_stop(sc);
4628 		bxe_stop_locked(sc);
4629             }
4630         }
4631 	BXE_CORE_UNLOCK(sc);
4632 
4633         break;
4634 
4635     case SIOCADDMULTI:
4636     case SIOCDELMULTI:
4637         /* add/delete multicast addresses */
4638         BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4639 
4640         /* check if the interface is up */
4641         if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4642             /* set the receive mode flags */
4643 	    BXE_CORE_LOCK(sc);
4644             bxe_set_rx_mode(sc);
4645 	    BXE_CORE_UNLOCK(sc);
4646         }
4647 
4648         break;
4649 
4650     case SIOCSIFCAP:
4651         /* find out which capabilities have changed */
4652         mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4653 
4654         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4655               mask);
4656 
4657         /* toggle the LRO capabilites enable flag */
4658         if (mask & IFCAP_LRO) {
4659 	    if_togglecapenable(ifp, IFCAP_LRO);
4660             BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4661                   (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4662             reinit = 1;
4663         }
4664 
4665         /* toggle the TXCSUM checksum capabilites enable flag */
4666         if (mask & IFCAP_TXCSUM) {
4667 	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4668             BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4669                   (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4670             if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4671                 if_sethwassistbits(ifp, (CSUM_IP      |
4672                                     CSUM_TCP      |
4673                                     CSUM_UDP      |
4674                                     CSUM_TSO      |
4675                                     CSUM_TCP_IPV6 |
4676                                     CSUM_UDP_IPV6), 0);
4677             } else {
4678 		if_clearhwassist(ifp); /* XXX */
4679             }
4680         }
4681 
4682         /* toggle the RXCSUM checksum capabilities enable flag */
4683         if (mask & IFCAP_RXCSUM) {
4684 	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4685             BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4686                   (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4687             if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4688                 if_sethwassistbits(ifp, (CSUM_IP      |
4689                                     CSUM_TCP      |
4690                                     CSUM_UDP      |
4691                                     CSUM_TSO      |
4692                                     CSUM_TCP_IPV6 |
4693                                     CSUM_UDP_IPV6), 0);
4694             } else {
4695 		if_clearhwassist(ifp); /* XXX */
4696             }
4697         }
4698 
4699         /* toggle TSO4 capabilities enabled flag */
4700         if (mask & IFCAP_TSO4) {
4701             if_togglecapenable(ifp, IFCAP_TSO4);
4702             BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4703                   (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4704         }
4705 
4706         /* toggle TSO6 capabilities enabled flag */
4707         if (mask & IFCAP_TSO6) {
4708 	    if_togglecapenable(ifp, IFCAP_TSO6);
4709             BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4710                   (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4711         }
4712 
4713         /* toggle VLAN_HWTSO capabilities enabled flag */
4714         if (mask & IFCAP_VLAN_HWTSO) {
4715 
4716 	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4717             BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4718                   (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4719         }
4720 
4721         /* toggle VLAN_HWCSUM capabilities enabled flag */
4722         if (mask & IFCAP_VLAN_HWCSUM) {
4723             /* XXX investigate this... */
4724             BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4725             error = EINVAL;
4726         }
4727 
4728         /* toggle VLAN_MTU capabilities enable flag */
4729         if (mask & IFCAP_VLAN_MTU) {
4730             /* XXX investigate this... */
4731             BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4732             error = EINVAL;
4733         }
4734 
4735         /* toggle VLAN_HWTAGGING capabilities enabled flag */
4736         if (mask & IFCAP_VLAN_HWTAGGING) {
4737             /* XXX investigate this... */
4738             BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4739             error = EINVAL;
4740         }
4741 
4742         /* toggle VLAN_HWFILTER capabilities enabled flag */
4743         if (mask & IFCAP_VLAN_HWFILTER) {
4744             /* XXX investigate this... */
4745             BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4746             error = EINVAL;
4747         }
4748 
4749         /* XXX not yet...
4750          * IFCAP_WOL_MAGIC
4751          */
4752 
4753         break;
4754 
4755     case SIOCSIFMEDIA:
4756     case SIOCGIFMEDIA:
4757         /* set/get interface media */
4758         BLOGD(sc, DBG_IOCTL,
4759               "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4760               (command & 0xff));
4761         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4762         break;
4763 
4764     case SIOCGPRIVATE_0:
4765         copyin(ifr->ifr_data, &priv_op, sizeof(priv_op));
4766 
4767         switch (priv_op)
4768         {
4769         case BXE_IOC_RD_NVRAM:
4770         case BXE_IOC_WR_NVRAM:
4771             nvdata = (struct bxe_nvram_data *)ifr->ifr_data;
4772             BLOGD(sc, DBG_IOCTL,
4773                   "Received Private NVRAM ioctl addr=0x%x size=%u\n",
4774                   nvdata->offset, nvdata->len);
4775             error = bxe_ioctl_nvram(sc, priv_op, ifr);
4776             break;
4777 
4778         case BXE_IOC_STATS_SHOW_NUM:
4779         case BXE_IOC_STATS_SHOW_STR:
4780         case BXE_IOC_STATS_SHOW_CNT:
4781             BLOGD(sc, DBG_IOCTL, "Received Private Stats ioctl (%d)\n",
4782                   priv_op);
4783             error = bxe_ioctl_stats_show(sc, priv_op, ifr);
4784             break;
4785 
4786         default:
4787             BLOGW(sc, "Received Private Unknown ioctl (%d)\n", priv_op);
4788             error = EINVAL;
4789             break;
4790         }
4791 
4792         break;
4793 
4794     default:
4795         BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4796               (command & 0xff));
4797         error = ether_ioctl(ifp, command, data);
4798         break;
4799     }
4800 
4801     if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
4802         BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4803               "Re-initializing hardware from IOCTL change\n");
4804 	bxe_periodic_stop(sc);
4805 	BXE_CORE_LOCK(sc);
4806 	bxe_stop_locked(sc);
4807 	bxe_init_locked(sc);
4808 	BXE_CORE_UNLOCK(sc);
4809     }
4810 
4811     return (error);
4812 }
4813 
4814 static __noinline void
4815 bxe_dump_mbuf(struct bxe_softc *sc,
4816               struct mbuf      *m,
4817               uint8_t          contents)
4818 {
4819     char * type;
4820     int i = 0;
4821 
4822     if (!(sc->debug & DBG_MBUF)) {
4823         return;
4824     }
4825 
4826     if (m == NULL) {
4827         BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4828         return;
4829     }
4830 
4831     while (m) {
4832         BLOGD(sc, DBG_MBUF,
4833               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4834               i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
4835 
4836         if (m->m_flags & M_PKTHDR) {
4837              BLOGD(sc, DBG_MBUF,
4838                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4839                    i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
4840                    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
4841         }
4842 
4843         if (m->m_flags & M_EXT) {
4844             switch (m->m_ext.ext_type) {
4845             case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4846             case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4847             case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4848             case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4849             case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4850             case EXT_PACKET:     type = "EXT_PACKET";     break;
4851             case EXT_MBUF:       type = "EXT_MBUF";       break;
4852             case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4853             case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4854             case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4855             case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4856             default:             type = "UNKNOWN";        break;
4857             }
4858 
4859             BLOGD(sc, DBG_MBUF,
4860                   "%02d: - m_ext: %p ext_size=%d type=%s\n",
4861                   i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4862         }
4863 
4864         if (contents) {
4865             bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4866         }
4867 
4868         m = m->m_next;
4869         i++;
4870     }
4871 }
4872 
4873 /*
4874  * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4875  * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4876  * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4877  * The headers comes in a seperate bd in FreeBSD so 13-3=10.
4878  * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4879  */
4880 static int
4881 bxe_chktso_window(struct bxe_softc  *sc,
4882                   int               nsegs,
4883                   bus_dma_segment_t *segs,
4884                   struct mbuf       *m)
4885 {
4886     uint32_t num_wnds, wnd_size, wnd_sum;
4887     int32_t frag_idx, wnd_idx;
4888     unsigned short lso_mss;
4889     int defrag;
4890 
4891     defrag = 0;
4892     wnd_sum = 0;
4893     wnd_size = 10;
4894     num_wnds = nsegs - wnd_size;
4895     lso_mss = htole16(m->m_pkthdr.tso_segsz);
4896 
4897     /*
4898      * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4899      * first window sum of data while skipping the first assuming it is the
4900      * header in FreeBSD.
4901      */
4902     for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4903         wnd_sum += htole16(segs[frag_idx].ds_len);
4904     }
4905 
4906     /* check the first 10 bd window size */
4907     if (wnd_sum < lso_mss) {
4908         return (1);
4909     }
4910 
4911     /* run through the windows */
4912     for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4913         /* subtract the first mbuf->m_len of the last wndw(-header) */
4914         wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4915         /* add the next mbuf len to the len of our new window */
4916         wnd_sum += htole16(segs[frag_idx].ds_len);
4917         if (wnd_sum < lso_mss) {
4918             return (1);
4919         }
4920     }
4921 
4922     return (0);
4923 }
4924 
4925 static uint8_t
4926 bxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4927                     struct mbuf         *m,
4928                     uint32_t            *parsing_data)
4929 {
4930     struct ether_vlan_header *eh = NULL;
4931     struct ip *ip4 = NULL;
4932     struct ip6_hdr *ip6 = NULL;
4933     caddr_t ip = NULL;
4934     struct tcphdr *th = NULL;
4935     int e_hlen, ip_hlen, l4_off;
4936     uint16_t proto;
4937 
4938     if (m->m_pkthdr.csum_flags == CSUM_IP) {
4939         /* no L4 checksum offload needed */
4940         return (0);
4941     }
4942 
4943     /* get the Ethernet header */
4944     eh = mtod(m, struct ether_vlan_header *);
4945 
4946     /* handle VLAN encapsulation if present */
4947     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4948         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4949         proto  = ntohs(eh->evl_proto);
4950     } else {
4951         e_hlen = ETHER_HDR_LEN;
4952         proto  = ntohs(eh->evl_encap_proto);
4953     }
4954 
4955     switch (proto) {
4956     case ETHERTYPE_IP:
4957         /* get the IP header, if mbuf len < 20 then header in next mbuf */
4958         ip4 = (m->m_len < sizeof(struct ip)) ?
4959                   (struct ip *)m->m_next->m_data :
4960                   (struct ip *)(m->m_data + e_hlen);
4961         /* ip_hl is number of 32-bit words */
4962         ip_hlen = (ip4->ip_hl << 2);
4963         ip = (caddr_t)ip4;
4964         break;
4965     case ETHERTYPE_IPV6:
4966         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4967         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4968                   (struct ip6_hdr *)m->m_next->m_data :
4969                   (struct ip6_hdr *)(m->m_data + e_hlen);
4970         /* XXX cannot support offload with IPv6 extensions */
4971         ip_hlen = sizeof(struct ip6_hdr);
4972         ip = (caddr_t)ip6;
4973         break;
4974     default:
4975         /* We can't offload in this case... */
4976         /* XXX error stat ??? */
4977         return (0);
4978     }
4979 
4980     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4981     l4_off = (e_hlen + ip_hlen);
4982 
4983     *parsing_data |=
4984         (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
4985          ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
4986 
4987     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4988                                   CSUM_TSO |
4989                                   CSUM_TCP_IPV6)) {
4990         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4991         th = (struct tcphdr *)(ip + ip_hlen);
4992         /* th_off is number of 32-bit words */
4993         *parsing_data |= ((th->th_off <<
4994                            ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
4995                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
4996         return (l4_off + (th->th_off << 2)); /* entire header length */
4997     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4998                                          CSUM_UDP_IPV6)) {
4999         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5000         return (l4_off + sizeof(struct udphdr)); /* entire header length */
5001     } else {
5002         /* XXX error stat ??? */
5003         return (0);
5004     }
5005 }
5006 
5007 static uint8_t
5008 bxe_set_pbd_csum(struct bxe_fastpath        *fp,
5009                  struct mbuf                *m,
5010                  struct eth_tx_parse_bd_e1x *pbd)
5011 {
5012     struct ether_vlan_header *eh = NULL;
5013     struct ip *ip4 = NULL;
5014     struct ip6_hdr *ip6 = NULL;
5015     caddr_t ip = NULL;
5016     struct tcphdr *th = NULL;
5017     struct udphdr *uh = NULL;
5018     int e_hlen, ip_hlen;
5019     uint16_t proto;
5020     uint8_t hlen;
5021     uint16_t tmp_csum;
5022     uint32_t *tmp_uh;
5023 
5024     /* get the Ethernet header */
5025     eh = mtod(m, struct ether_vlan_header *);
5026 
5027     /* handle VLAN encapsulation if present */
5028     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
5029         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5030         proto  = ntohs(eh->evl_proto);
5031     } else {
5032         e_hlen = ETHER_HDR_LEN;
5033         proto  = ntohs(eh->evl_encap_proto);
5034     }
5035 
5036     switch (proto) {
5037     case ETHERTYPE_IP:
5038         /* get the IP header, if mbuf len < 20 then header in next mbuf */
5039         ip4 = (m->m_len < sizeof(struct ip)) ?
5040                   (struct ip *)m->m_next->m_data :
5041                   (struct ip *)(m->m_data + e_hlen);
5042         /* ip_hl is number of 32-bit words */
5043         ip_hlen = (ip4->ip_hl << 1);
5044         ip = (caddr_t)ip4;
5045         break;
5046     case ETHERTYPE_IPV6:
5047         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
5048         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
5049                   (struct ip6_hdr *)m->m_next->m_data :
5050                   (struct ip6_hdr *)(m->m_data + e_hlen);
5051         /* XXX cannot support offload with IPv6 extensions */
5052         ip_hlen = (sizeof(struct ip6_hdr) >> 1);
5053         ip = (caddr_t)ip6;
5054         break;
5055     default:
5056         /* We can't offload in this case... */
5057         /* XXX error stat ??? */
5058         return (0);
5059     }
5060 
5061     hlen = (e_hlen >> 1);
5062 
5063     /* note that rest of global_data is indirectly zeroed here */
5064     if (m->m_flags & M_VLANTAG) {
5065         pbd->global_data =
5066             htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
5067     } else {
5068         pbd->global_data = htole16(hlen);
5069     }
5070 
5071     pbd->ip_hlen_w = ip_hlen;
5072 
5073     hlen += pbd->ip_hlen_w;
5074 
5075     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5076 
5077     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5078                                   CSUM_TSO |
5079                                   CSUM_TCP_IPV6)) {
5080         th = (struct tcphdr *)(ip + (ip_hlen << 1));
5081         /* th_off is number of 32-bit words */
5082         hlen += (uint16_t)(th->th_off << 1);
5083     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5084                                          CSUM_UDP_IPV6)) {
5085         uh = (struct udphdr *)(ip + (ip_hlen << 1));
5086         hlen += (sizeof(struct udphdr) / 2);
5087     } else {
5088         /* valid case as only CSUM_IP was set */
5089         return (0);
5090     }
5091 
5092     pbd->total_hlen_w = htole16(hlen);
5093 
5094     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5095                                   CSUM_TSO |
5096                                   CSUM_TCP_IPV6)) {
5097         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5098         pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5099     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5100                                          CSUM_UDP_IPV6)) {
5101         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5102 
5103         /*
5104          * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5105          * checksums and does not know anything about the UDP header and where
5106          * the checksum field is located. It only knows about TCP. Therefore
5107          * we "lie" to the hardware for outgoing UDP packets w/ checksum
5108          * offload. Since the checksum field offset for TCP is 16 bytes and
5109          * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5110          * bytes less than the start of the UDP header. This allows the
5111          * hardware to write the checksum in the correct spot. But the
5112          * hardware will compute a checksum which includes the last 10 bytes
5113          * of the IP header. To correct this we tweak the stack computed
5114          * pseudo checksum by folding in the calculation of the inverse
5115          * checksum for those final 10 bytes of the IP header. This allows
5116          * the correct checksum to be computed by the hardware.
5117          */
5118 
5119         /* set pointer 10 bytes before UDP header */
5120         tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5121 
5122         /* calculate a pseudo header checksum over the first 10 bytes */
5123         tmp_csum = in_pseudo(*tmp_uh,
5124                              *(tmp_uh + 1),
5125                              *(uint16_t *)(tmp_uh + 2));
5126 
5127         pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5128     }
5129 
5130     return (hlen * 2); /* entire header length, number of bytes */
5131 }
5132 
5133 static void
5134 bxe_set_pbd_lso_e2(struct mbuf *m,
5135                    uint32_t    *parsing_data)
5136 {
5137     *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5138                        ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5139                       ETH_TX_PARSE_BD_E2_LSO_MSS);
5140 
5141     /* XXX test for IPv6 with extension header... */
5142 }
5143 
5144 static void
5145 bxe_set_pbd_lso(struct mbuf                *m,
5146                 struct eth_tx_parse_bd_e1x *pbd)
5147 {
5148     struct ether_vlan_header *eh = NULL;
5149     struct ip *ip = NULL;
5150     struct tcphdr *th = NULL;
5151     int e_hlen;
5152 
5153     /* get the Ethernet header */
5154     eh = mtod(m, struct ether_vlan_header *);
5155 
5156     /* handle VLAN encapsulation if present */
5157     e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5158                  (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5159 
5160     /* get the IP and TCP header, with LSO entire header in first mbuf */
5161     /* XXX assuming IPv4 */
5162     ip = (struct ip *)(m->m_data + e_hlen);
5163     th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5164 
5165     pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5166     pbd->tcp_send_seq = ntohl(th->th_seq);
5167     pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5168 
5169 #if 1
5170         /* XXX IPv4 */
5171         pbd->ip_id = ntohs(ip->ip_id);
5172         pbd->tcp_pseudo_csum =
5173             ntohs(in_pseudo(ip->ip_src.s_addr,
5174                             ip->ip_dst.s_addr,
5175                             htons(IPPROTO_TCP)));
5176 #else
5177         /* XXX IPv6 */
5178         pbd->tcp_pseudo_csum =
5179             ntohs(in_pseudo(&ip6->ip6_src,
5180                             &ip6->ip6_dst,
5181                             htons(IPPROTO_TCP)));
5182 #endif
5183 
5184     pbd->global_data |=
5185         htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5186 }
5187 
5188 /*
5189  * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5190  * visible to the controller.
5191  *
5192  * If an mbuf is submitted to this routine and cannot be given to the
5193  * controller (e.g. it has too many fragments) then the function may free
5194  * the mbuf and return to the caller.
5195  *
5196  * Returns:
5197  *   0 = Success, !0 = Failure
5198  *   Note the side effect that an mbuf may be freed if it causes a problem.
5199  */
5200 static int
5201 bxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5202 {
5203     bus_dma_segment_t segs[32];
5204     struct mbuf *m0;
5205     struct bxe_sw_tx_bd *tx_buf;
5206     struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5207     struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5208     /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5209     struct eth_tx_bd *tx_data_bd;
5210     struct eth_tx_bd *tx_total_pkt_size_bd;
5211     struct eth_tx_start_bd *tx_start_bd;
5212     uint16_t bd_prod, pkt_prod, total_pkt_size;
5213     uint8_t mac_type;
5214     int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5215     struct bxe_softc *sc;
5216     uint16_t tx_bd_avail;
5217     struct ether_vlan_header *eh;
5218     uint32_t pbd_e2_parsing_data = 0;
5219     uint8_t hlen = 0;
5220     int tmp_bd;
5221     int i;
5222 
5223     sc = fp->sc;
5224 
5225     M_ASSERTPKTHDR(*m_head);
5226 
5227     m0 = *m_head;
5228     rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5229     tx_start_bd = NULL;
5230     tx_data_bd = NULL;
5231     tx_total_pkt_size_bd = NULL;
5232 
5233     /* get the H/W pointer for packets and BDs */
5234     pkt_prod = fp->tx_pkt_prod;
5235     bd_prod = fp->tx_bd_prod;
5236 
5237     mac_type = UNICAST_ADDRESS;
5238 
5239     /* map the mbuf into the next open DMAable memory */
5240     tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5241     error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5242                                     tx_buf->m_map, m0,
5243                                     segs, &nsegs, BUS_DMA_NOWAIT);
5244 
5245     /* mapping errors */
5246     if(__predict_false(error != 0)) {
5247         fp->eth_q_stats.tx_dma_mapping_failure++;
5248         if (error == ENOMEM) {
5249             /* resource issue, try again later */
5250             rc = ENOMEM;
5251         } else if (error == EFBIG) {
5252             /* possibly recoverable with defragmentation */
5253             fp->eth_q_stats.mbuf_defrag_attempts++;
5254             m0 = m_defrag(*m_head, M_NOWAIT);
5255             if (m0 == NULL) {
5256                 fp->eth_q_stats.mbuf_defrag_failures++;
5257                 rc = ENOBUFS;
5258             } else {
5259                 /* defrag successful, try mapping again */
5260                 *m_head = m0;
5261                 error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5262                                                 tx_buf->m_map, m0,
5263                                                 segs, &nsegs, BUS_DMA_NOWAIT);
5264                 if (error) {
5265                     fp->eth_q_stats.tx_dma_mapping_failure++;
5266                     rc = error;
5267                 }
5268             }
5269         } else {
5270             /* unknown, unrecoverable mapping error */
5271             BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5272             bxe_dump_mbuf(sc, m0, FALSE);
5273             rc = error;
5274         }
5275 
5276         goto bxe_tx_encap_continue;
5277     }
5278 
5279     tx_bd_avail = bxe_tx_avail(sc, fp);
5280 
5281     /* make sure there is enough room in the send queue */
5282     if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5283         /* Recoverable, try again later. */
5284         fp->eth_q_stats.tx_hw_queue_full++;
5285         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5286         rc = ENOMEM;
5287         goto bxe_tx_encap_continue;
5288     }
5289 
5290     /* capture the current H/W TX chain high watermark */
5291     if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5292                         (TX_BD_USABLE - tx_bd_avail))) {
5293         fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5294     }
5295 
5296     /* make sure it fits in the packet window */
5297     if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5298         /*
5299          * The mbuf may be to big for the controller to handle. If the frame
5300          * is a TSO frame we'll need to do an additional check.
5301          */
5302         if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5303             if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5304                 goto bxe_tx_encap_continue; /* OK to send */
5305             } else {
5306                 fp->eth_q_stats.tx_window_violation_tso++;
5307             }
5308         } else {
5309             fp->eth_q_stats.tx_window_violation_std++;
5310         }
5311 
5312         /* lets try to defragment this mbuf and remap it */
5313         fp->eth_q_stats.mbuf_defrag_attempts++;
5314         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5315 
5316         m0 = m_defrag(*m_head, M_NOWAIT);
5317         if (m0 == NULL) {
5318             fp->eth_q_stats.mbuf_defrag_failures++;
5319             /* Ugh, just drop the frame... :( */
5320             rc = ENOBUFS;
5321         } else {
5322             /* defrag successful, try mapping again */
5323             *m_head = m0;
5324             error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5325                                             tx_buf->m_map, m0,
5326                                             segs, &nsegs, BUS_DMA_NOWAIT);
5327             if (error) {
5328                 fp->eth_q_stats.tx_dma_mapping_failure++;
5329                 /* No sense in trying to defrag/copy chain, drop it. :( */
5330                 rc = error;
5331             }
5332             else {
5333                 /* if the chain is still too long then drop it */
5334                 if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5335                     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5336                     rc = ENODEV;
5337                 }
5338             }
5339         }
5340     }
5341 
5342 bxe_tx_encap_continue:
5343 
5344     /* Check for errors */
5345     if (rc) {
5346         if (rc == ENOMEM) {
5347             /* recoverable try again later  */
5348         } else {
5349             fp->eth_q_stats.tx_soft_errors++;
5350             fp->eth_q_stats.mbuf_alloc_tx--;
5351             m_freem(*m_head);
5352             *m_head = NULL;
5353         }
5354 
5355         return (rc);
5356     }
5357 
5358     /* set flag according to packet type (UNICAST_ADDRESS is default) */
5359     if (m0->m_flags & M_BCAST) {
5360         mac_type = BROADCAST_ADDRESS;
5361     } else if (m0->m_flags & M_MCAST) {
5362         mac_type = MULTICAST_ADDRESS;
5363     }
5364 
5365     /* store the mbuf into the mbuf ring */
5366     tx_buf->m        = m0;
5367     tx_buf->first_bd = fp->tx_bd_prod;
5368     tx_buf->flags    = 0;
5369 
5370     /* prepare the first transmit (start) BD for the mbuf */
5371     tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5372 
5373     BLOGD(sc, DBG_TX,
5374           "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5375           pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5376 
5377     tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5378     tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5379     tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5380     total_pkt_size += tx_start_bd->nbytes;
5381     tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5382 
5383     tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5384 
5385     /* all frames have at least Start BD + Parsing BD */
5386     nbds = nsegs + 1;
5387     tx_start_bd->nbd = htole16(nbds);
5388 
5389     if (m0->m_flags & M_VLANTAG) {
5390         tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5391         tx_start_bd->bd_flags.as_bitfield |=
5392             (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5393     } else {
5394         /* vf tx, start bd must hold the ethertype for fw to enforce it */
5395         if (IS_VF(sc)) {
5396             /* map ethernet header to find type and header length */
5397             eh = mtod(m0, struct ether_vlan_header *);
5398             tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5399         } else {
5400             /* used by FW for packet accounting */
5401             tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5402         }
5403     }
5404 
5405     /*
5406      * add a parsing BD from the chain. The parsing BD is always added
5407      * though it is only used for TSO and chksum
5408      */
5409     bd_prod = TX_BD_NEXT(bd_prod);
5410 
5411     if (m0->m_pkthdr.csum_flags) {
5412         if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5413             fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5414             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5415         }
5416 
5417         if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5418             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5419                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5420         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5421             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5422                                                   ETH_TX_BD_FLAGS_IS_UDP |
5423                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5424         } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5425                    (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5426             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5427         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5428             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5429                                                   ETH_TX_BD_FLAGS_IS_UDP);
5430         }
5431     }
5432 
5433     if (!CHIP_IS_E1x(sc)) {
5434         pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5435         memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5436 
5437         if (m0->m_pkthdr.csum_flags) {
5438             hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5439         }
5440 
5441         SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5442                  mac_type);
5443     } else {
5444         uint16_t global_data = 0;
5445 
5446         pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5447         memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5448 
5449         if (m0->m_pkthdr.csum_flags) {
5450             hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5451         }
5452 
5453         SET_FLAG(global_data,
5454                  ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5455         pbd_e1x->global_data |= htole16(global_data);
5456     }
5457 
5458     /* setup the parsing BD with TSO specific info */
5459     if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5460         fp->eth_q_stats.tx_ofld_frames_lso++;
5461         tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5462 
5463         if (__predict_false(tx_start_bd->nbytes > hlen)) {
5464             fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5465 
5466             /* split the first BD into header/data making the fw job easy */
5467             nbds++;
5468             tx_start_bd->nbd = htole16(nbds);
5469             tx_start_bd->nbytes = htole16(hlen);
5470 
5471             bd_prod = TX_BD_NEXT(bd_prod);
5472 
5473             /* new transmit BD after the tx_parse_bd */
5474             tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5475             tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5476             tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5477             tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5478             if (tx_total_pkt_size_bd == NULL) {
5479                 tx_total_pkt_size_bd = tx_data_bd;
5480             }
5481 
5482             BLOGD(sc, DBG_TX,
5483                   "TSO split header size is %d (%x:%x) nbds %d\n",
5484                   le16toh(tx_start_bd->nbytes),
5485                   le32toh(tx_start_bd->addr_hi),
5486                   le32toh(tx_start_bd->addr_lo),
5487                   nbds);
5488         }
5489 
5490         if (!CHIP_IS_E1x(sc)) {
5491             bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5492         } else {
5493             bxe_set_pbd_lso(m0, pbd_e1x);
5494         }
5495     }
5496 
5497     if (pbd_e2_parsing_data) {
5498         pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5499     }
5500 
5501     /* prepare remaining BDs, start tx bd contains first seg/frag */
5502     for (i = 1; i < nsegs ; i++) {
5503         bd_prod = TX_BD_NEXT(bd_prod);
5504         tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5505         tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5506         tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5507         tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5508         if (tx_total_pkt_size_bd == NULL) {
5509             tx_total_pkt_size_bd = tx_data_bd;
5510         }
5511         total_pkt_size += tx_data_bd->nbytes;
5512     }
5513 
5514     BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5515 
5516     if (tx_total_pkt_size_bd != NULL) {
5517         tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5518     }
5519 
5520     if (__predict_false(sc->debug & DBG_TX)) {
5521         tmp_bd = tx_buf->first_bd;
5522         for (i = 0; i < nbds; i++)
5523         {
5524             if (i == 0) {
5525                 BLOGD(sc, DBG_TX,
5526                       "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5527                       "bd_flags=0x%x hdr_nbds=%d\n",
5528                       tx_start_bd,
5529                       tmp_bd,
5530                       le16toh(tx_start_bd->nbd),
5531                       le16toh(tx_start_bd->vlan_or_ethertype),
5532                       tx_start_bd->bd_flags.as_bitfield,
5533                       (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5534             } else if (i == 1) {
5535                 if (pbd_e1x) {
5536                     BLOGD(sc, DBG_TX,
5537                           "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5538                           "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5539                           "tcp_seq=%u total_hlen_w=%u\n",
5540                           pbd_e1x,
5541                           tmp_bd,
5542                           pbd_e1x->global_data,
5543                           pbd_e1x->ip_hlen_w,
5544                           pbd_e1x->ip_id,
5545                           pbd_e1x->lso_mss,
5546                           pbd_e1x->tcp_flags,
5547                           pbd_e1x->tcp_pseudo_csum,
5548                           pbd_e1x->tcp_send_seq,
5549                           le16toh(pbd_e1x->total_hlen_w));
5550                 } else { /* if (pbd_e2) */
5551                     BLOGD(sc, DBG_TX,
5552                           "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5553                           "src=%02x:%02x:%02x parsing_data=0x%x\n",
5554                           pbd_e2,
5555                           tmp_bd,
5556                           pbd_e2->data.mac_addr.dst_hi,
5557                           pbd_e2->data.mac_addr.dst_mid,
5558                           pbd_e2->data.mac_addr.dst_lo,
5559                           pbd_e2->data.mac_addr.src_hi,
5560                           pbd_e2->data.mac_addr.src_mid,
5561                           pbd_e2->data.mac_addr.src_lo,
5562                           pbd_e2->parsing_data);
5563                 }
5564             }
5565 
5566             if (i != 1) { /* skip parse db as it doesn't hold data */
5567                 tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5568                 BLOGD(sc, DBG_TX,
5569                       "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5570                       tx_data_bd,
5571                       tmp_bd,
5572                       le16toh(tx_data_bd->nbytes),
5573                       le32toh(tx_data_bd->addr_hi),
5574                       le32toh(tx_data_bd->addr_lo));
5575             }
5576 
5577             tmp_bd = TX_BD_NEXT(tmp_bd);
5578         }
5579     }
5580 
5581     BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5582 
5583     /* update TX BD producer index value for next TX */
5584     bd_prod = TX_BD_NEXT(bd_prod);
5585 
5586     /*
5587      * If the chain of tx_bd's describing this frame is adjacent to or spans
5588      * an eth_tx_next_bd element then we need to increment the nbds value.
5589      */
5590     if (TX_BD_IDX(bd_prod) < nbds) {
5591         nbds++;
5592     }
5593 
5594     /* don't allow reordering of writes for nbd and packets */
5595     mb();
5596 
5597     fp->tx_db.data.prod += nbds;
5598 
5599     /* producer points to the next free tx_bd at this point */
5600     fp->tx_pkt_prod++;
5601     fp->tx_bd_prod = bd_prod;
5602 
5603     DOORBELL(sc, fp->index, fp->tx_db.raw);
5604 
5605     fp->eth_q_stats.tx_pkts++;
5606 
5607     /* Prevent speculative reads from getting ahead of the status block. */
5608     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5609                       0, 0, BUS_SPACE_BARRIER_READ);
5610 
5611     /* Prevent speculative reads from getting ahead of the doorbell. */
5612     bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5613                       0, 0, BUS_SPACE_BARRIER_READ);
5614 
5615     return (0);
5616 }
5617 
5618 static void
5619 bxe_tx_start_locked(struct bxe_softc *sc,
5620                     if_t ifp,
5621                     struct bxe_fastpath *fp)
5622 {
5623     struct mbuf *m = NULL;
5624     int tx_count = 0;
5625     uint16_t tx_bd_avail;
5626 
5627     BXE_FP_TX_LOCK_ASSERT(fp);
5628 
5629     /* keep adding entries while there are frames to send */
5630     while (!if_sendq_empty(ifp)) {
5631 
5632         /*
5633          * check for any frames to send
5634          * dequeue can still be NULL even if queue is not empty
5635          */
5636         m = if_dequeue(ifp);
5637         if (__predict_false(m == NULL)) {
5638             break;
5639         }
5640 
5641         /* the mbuf now belongs to us */
5642         fp->eth_q_stats.mbuf_alloc_tx++;
5643 
5644         /*
5645          * Put the frame into the transmit ring. If we don't have room,
5646          * place the mbuf back at the head of the TX queue, set the
5647          * OACTIVE flag, and wait for the NIC to drain the chain.
5648          */
5649         if (__predict_false(bxe_tx_encap(fp, &m))) {
5650             fp->eth_q_stats.tx_encap_failures++;
5651             if (m != NULL) {
5652                 /* mark the TX queue as full and return the frame */
5653                 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5654 		if_sendq_prepend(ifp, m);
5655                 fp->eth_q_stats.mbuf_alloc_tx--;
5656                 fp->eth_q_stats.tx_queue_xoff++;
5657             }
5658 
5659             /* stop looking for more work */
5660             break;
5661         }
5662 
5663         /* the frame was enqueued successfully */
5664         tx_count++;
5665 
5666         /* send a copy of the frame to any BPF listeners. */
5667         if_etherbpfmtap(ifp, m);
5668 
5669         tx_bd_avail = bxe_tx_avail(sc, fp);
5670 
5671         /* handle any completions if we're running low */
5672         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5673             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5674             bxe_txeof(sc, fp);
5675             if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5676                 break;
5677             }
5678         }
5679     }
5680 
5681     /* all TX packets were dequeued and/or the tx ring is full */
5682     if (tx_count > 0) {
5683         /* reset the TX watchdog timeout timer */
5684         fp->watchdog_timer = BXE_TX_TIMEOUT;
5685     }
5686 }
5687 
5688 /* Legacy (non-RSS) dispatch routine */
5689 static void
5690 bxe_tx_start(if_t ifp)
5691 {
5692     struct bxe_softc *sc;
5693     struct bxe_fastpath *fp;
5694 
5695     sc = if_getsoftc(ifp);
5696 
5697     if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5698         BLOGW(sc, "Interface not running, ignoring transmit request\n");
5699         return;
5700     }
5701 
5702     if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5703         BLOGW(sc, "Interface TX queue is full, ignoring transmit request\n");
5704         return;
5705     }
5706 
5707     if (!sc->link_vars.link_up) {
5708         BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5709         return;
5710     }
5711 
5712     fp = &sc->fp[0];
5713 
5714     BXE_FP_TX_LOCK(fp);
5715     bxe_tx_start_locked(sc, ifp, fp);
5716     BXE_FP_TX_UNLOCK(fp);
5717 }
5718 
5719 #if __FreeBSD_version >= 800000
5720 
5721 static int
5722 bxe_tx_mq_start_locked(struct bxe_softc    *sc,
5723                        if_t                ifp,
5724                        struct bxe_fastpath *fp,
5725                        struct mbuf         *m)
5726 {
5727     struct buf_ring *tx_br = fp->tx_br;
5728     struct mbuf *next;
5729     int depth, rc, tx_count;
5730     uint16_t tx_bd_avail;
5731 
5732     rc = tx_count = 0;
5733 
5734     BXE_FP_TX_LOCK_ASSERT(fp);
5735 
5736     if (!tx_br) {
5737         BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5738         return (EINVAL);
5739     }
5740 
5741     if (!sc->link_vars.link_up ||
5742         (if_getdrvflags(ifp) &
5743         (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING) {
5744         rc = drbr_enqueue_drv(ifp, tx_br, m);
5745         goto bxe_tx_mq_start_locked_exit;
5746     }
5747 
5748     /* fetch the depth of the driver queue */
5749     depth = drbr_inuse_drv(ifp, tx_br);
5750     if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5751         fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5752     }
5753 
5754     if (m == NULL) {
5755         /* no new work, check for pending frames */
5756         next = drbr_dequeue_drv(ifp, tx_br);
5757     } else if (drbr_needs_enqueue_drv(ifp, tx_br)) {
5758         /* have both new and pending work, maintain packet order */
5759         rc = drbr_enqueue_drv(ifp, tx_br, m);
5760         if (rc != 0) {
5761             fp->eth_q_stats.tx_soft_errors++;
5762             goto bxe_tx_mq_start_locked_exit;
5763         }
5764         next = drbr_dequeue_drv(ifp, tx_br);
5765     } else {
5766         /* new work only and nothing pending */
5767         next = m;
5768     }
5769 
5770     /* keep adding entries while there are frames to send */
5771     while (next != NULL) {
5772 
5773         /* the mbuf now belongs to us */
5774         fp->eth_q_stats.mbuf_alloc_tx++;
5775 
5776         /*
5777          * Put the frame into the transmit ring. If we don't have room,
5778          * place the mbuf back at the head of the TX queue, set the
5779          * OACTIVE flag, and wait for the NIC to drain the chain.
5780          */
5781         rc = bxe_tx_encap(fp, &next);
5782         if (__predict_false(rc != 0)) {
5783             fp->eth_q_stats.tx_encap_failures++;
5784             if (next != NULL) {
5785                 /* mark the TX queue as full and save the frame */
5786                 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5787                 /* XXX this may reorder the frame */
5788                 rc = drbr_enqueue_drv(ifp, tx_br, next);
5789                 fp->eth_q_stats.mbuf_alloc_tx--;
5790                 fp->eth_q_stats.tx_frames_deferred++;
5791             }
5792 
5793             /* stop looking for more work */
5794             break;
5795         }
5796 
5797         /* the transmit frame was enqueued successfully */
5798         tx_count++;
5799 
5800         /* send a copy of the frame to any BPF listeners */
5801 	if_etherbpfmtap(ifp, next);
5802 
5803         tx_bd_avail = bxe_tx_avail(sc, fp);
5804 
5805         /* handle any completions if we're running low */
5806         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5807             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5808             bxe_txeof(sc, fp);
5809             if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5810                 break;
5811             }
5812         }
5813 
5814         next = drbr_dequeue_drv(ifp, tx_br);
5815     }
5816 
5817     /* all TX packets were dequeued and/or the tx ring is full */
5818     if (tx_count > 0) {
5819         /* reset the TX watchdog timeout timer */
5820         fp->watchdog_timer = BXE_TX_TIMEOUT;
5821     }
5822 
5823 bxe_tx_mq_start_locked_exit:
5824 
5825     return (rc);
5826 }
5827 
5828 /* Multiqueue (TSS) dispatch routine. */
5829 static int
5830 bxe_tx_mq_start(struct ifnet *ifp,
5831                 struct mbuf  *m)
5832 {
5833     struct bxe_softc *sc = if_getsoftc(ifp);
5834     struct bxe_fastpath *fp;
5835     int fp_index, rc;
5836 
5837     fp_index = 0; /* default is the first queue */
5838 
5839     /* check if flowid is set */
5840     if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
5841         fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5842 
5843     fp = &sc->fp[fp_index];
5844 
5845     if (BXE_FP_TX_TRYLOCK(fp)) {
5846         rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5847         BXE_FP_TX_UNLOCK(fp);
5848     } else
5849         rc = drbr_enqueue_drv(ifp, fp->tx_br, m);
5850 
5851     return (rc);
5852 }
5853 
5854 static void
5855 bxe_mq_flush(struct ifnet *ifp)
5856 {
5857     struct bxe_softc *sc = if_getsoftc(ifp);
5858     struct bxe_fastpath *fp;
5859     struct mbuf *m;
5860     int i;
5861 
5862     for (i = 0; i < sc->num_queues; i++) {
5863         fp = &sc->fp[i];
5864 
5865         if (fp->state != BXE_FP_STATE_OPEN) {
5866             BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5867                   fp->index, fp->state);
5868             continue;
5869         }
5870 
5871         if (fp->tx_br != NULL) {
5872             BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5873             BXE_FP_TX_LOCK(fp);
5874             while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5875                 m_freem(m);
5876             }
5877             BXE_FP_TX_UNLOCK(fp);
5878         }
5879     }
5880 
5881     if_qflush(ifp);
5882 }
5883 
5884 #endif /* FreeBSD_version >= 800000 */
5885 
5886 static uint16_t
5887 bxe_cid_ilt_lines(struct bxe_softc *sc)
5888 {
5889     if (IS_SRIOV(sc)) {
5890         return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5891     }
5892     return (L2_ILT_LINES(sc));
5893 }
5894 
5895 static void
5896 bxe_ilt_set_info(struct bxe_softc *sc)
5897 {
5898     struct ilt_client_info *ilt_client;
5899     struct ecore_ilt *ilt = sc->ilt;
5900     uint16_t line = 0;
5901 
5902     ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5903     BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5904 
5905     /* CDU */
5906     ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5907     ilt_client->client_num = ILT_CLIENT_CDU;
5908     ilt_client->page_size = CDU_ILT_PAGE_SZ;
5909     ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5910     ilt_client->start = line;
5911     line += bxe_cid_ilt_lines(sc);
5912 
5913     if (CNIC_SUPPORT(sc)) {
5914         line += CNIC_ILT_LINES;
5915     }
5916 
5917     ilt_client->end = (line - 1);
5918 
5919     BLOGD(sc, DBG_LOAD,
5920           "ilt client[CDU]: start %d, end %d, "
5921           "psz 0x%x, flags 0x%x, hw psz %d\n",
5922           ilt_client->start, ilt_client->end,
5923           ilt_client->page_size,
5924           ilt_client->flags,
5925           ilog2(ilt_client->page_size >> 12));
5926 
5927     /* QM */
5928     if (QM_INIT(sc->qm_cid_count)) {
5929         ilt_client = &ilt->clients[ILT_CLIENT_QM];
5930         ilt_client->client_num = ILT_CLIENT_QM;
5931         ilt_client->page_size = QM_ILT_PAGE_SZ;
5932         ilt_client->flags = 0;
5933         ilt_client->start = line;
5934 
5935         /* 4 bytes for each cid */
5936         line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5937                              QM_ILT_PAGE_SZ);
5938 
5939         ilt_client->end = (line - 1);
5940 
5941         BLOGD(sc, DBG_LOAD,
5942               "ilt client[QM]: start %d, end %d, "
5943               "psz 0x%x, flags 0x%x, hw psz %d\n",
5944               ilt_client->start, ilt_client->end,
5945               ilt_client->page_size, ilt_client->flags,
5946               ilog2(ilt_client->page_size >> 12));
5947     }
5948 
5949     if (CNIC_SUPPORT(sc)) {
5950         /* SRC */
5951         ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5952         ilt_client->client_num = ILT_CLIENT_SRC;
5953         ilt_client->page_size = SRC_ILT_PAGE_SZ;
5954         ilt_client->flags = 0;
5955         ilt_client->start = line;
5956         line += SRC_ILT_LINES;
5957         ilt_client->end = (line - 1);
5958 
5959         BLOGD(sc, DBG_LOAD,
5960               "ilt client[SRC]: start %d, end %d, "
5961               "psz 0x%x, flags 0x%x, hw psz %d\n",
5962               ilt_client->start, ilt_client->end,
5963               ilt_client->page_size, ilt_client->flags,
5964               ilog2(ilt_client->page_size >> 12));
5965 
5966         /* TM */
5967         ilt_client = &ilt->clients[ILT_CLIENT_TM];
5968         ilt_client->client_num = ILT_CLIENT_TM;
5969         ilt_client->page_size = TM_ILT_PAGE_SZ;
5970         ilt_client->flags = 0;
5971         ilt_client->start = line;
5972         line += TM_ILT_LINES;
5973         ilt_client->end = (line - 1);
5974 
5975         BLOGD(sc, DBG_LOAD,
5976               "ilt client[TM]: start %d, end %d, "
5977               "psz 0x%x, flags 0x%x, hw psz %d\n",
5978               ilt_client->start, ilt_client->end,
5979               ilt_client->page_size, ilt_client->flags,
5980               ilog2(ilt_client->page_size >> 12));
5981     }
5982 
5983     KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
5984 }
5985 
5986 static void
5987 bxe_set_fp_rx_buf_size(struct bxe_softc *sc)
5988 {
5989     int i;
5990     uint32_t rx_buf_size;
5991 
5992     rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
5993 
5994     for (i = 0; i < sc->num_queues; i++) {
5995         if(rx_buf_size <= MCLBYTES){
5996             sc->fp[i].rx_buf_size = rx_buf_size;
5997             sc->fp[i].mbuf_alloc_size = MCLBYTES;
5998         }else if (rx_buf_size <= MJUMPAGESIZE){
5999             sc->fp[i].rx_buf_size = rx_buf_size;
6000             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6001         }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
6002             sc->fp[i].rx_buf_size = MCLBYTES;
6003             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6004         }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
6005             sc->fp[i].rx_buf_size = MJUMPAGESIZE;
6006             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6007         }else {
6008             sc->fp[i].rx_buf_size = MCLBYTES;
6009             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6010         }
6011     }
6012 }
6013 
6014 static int
6015 bxe_alloc_ilt_mem(struct bxe_softc *sc)
6016 {
6017     int rc = 0;
6018 
6019     if ((sc->ilt =
6020          (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
6021                                     M_BXE_ILT,
6022                                     (M_NOWAIT | M_ZERO))) == NULL) {
6023         rc = 1;
6024     }
6025 
6026     return (rc);
6027 }
6028 
6029 static int
6030 bxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
6031 {
6032     int rc = 0;
6033 
6034     if ((sc->ilt->lines =
6035          (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
6036                                     M_BXE_ILT,
6037                                     (M_NOWAIT | M_ZERO))) == NULL) {
6038         rc = 1;
6039     }
6040 
6041     return (rc);
6042 }
6043 
6044 static void
6045 bxe_free_ilt_mem(struct bxe_softc *sc)
6046 {
6047     if (sc->ilt != NULL) {
6048         free(sc->ilt, M_BXE_ILT);
6049         sc->ilt = NULL;
6050     }
6051 }
6052 
6053 static void
6054 bxe_free_ilt_lines_mem(struct bxe_softc *sc)
6055 {
6056     if (sc->ilt->lines != NULL) {
6057         free(sc->ilt->lines, M_BXE_ILT);
6058         sc->ilt->lines = NULL;
6059     }
6060 }
6061 
6062 static void
6063 bxe_free_mem(struct bxe_softc *sc)
6064 {
6065     int i;
6066 
6067     for (i = 0; i < L2_ILT_LINES(sc); i++) {
6068         bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6069         sc->context[i].vcxt = NULL;
6070         sc->context[i].size = 0;
6071     }
6072 
6073     ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6074 
6075     bxe_free_ilt_lines_mem(sc);
6076 
6077 }
6078 
6079 static int
6080 bxe_alloc_mem(struct bxe_softc *sc)
6081 {
6082     int context_size;
6083     int allocated;
6084     int i;
6085 
6086     /*
6087      * Allocate memory for CDU context:
6088      * This memory is allocated separately and not in the generic ILT
6089      * functions because CDU differs in few aspects:
6090      * 1. There can be multiple entities allocating memory for context -
6091      * regular L2, CNIC, and SRIOV drivers. Each separately controls
6092      * its own ILT lines.
6093      * 2. Since CDU page-size is not a single 4KB page (which is the case
6094      * for the other ILT clients), to be efficient we want to support
6095      * allocation of sub-page-size in the last entry.
6096      * 3. Context pointers are used by the driver to pass to FW / update
6097      * the context (for the other ILT clients the pointers are used just to
6098      * free the memory during unload).
6099      */
6100     context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6101     for (i = 0, allocated = 0; allocated < context_size; i++) {
6102         sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6103                                   (context_size - allocated));
6104 
6105         if (bxe_dma_alloc(sc, sc->context[i].size,
6106                           &sc->context[i].vcxt_dma,
6107                           "cdu context") != 0) {
6108             bxe_free_mem(sc);
6109             return (-1);
6110         }
6111 
6112         sc->context[i].vcxt =
6113             (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6114 
6115         allocated += sc->context[i].size;
6116     }
6117 
6118     bxe_alloc_ilt_lines_mem(sc);
6119 
6120     BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6121           sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6122     {
6123         for (i = 0; i < 4; i++) {
6124             BLOGD(sc, DBG_LOAD,
6125                   "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6126                   i,
6127                   sc->ilt->clients[i].page_size,
6128                   sc->ilt->clients[i].start,
6129                   sc->ilt->clients[i].end,
6130                   sc->ilt->clients[i].client_num,
6131                   sc->ilt->clients[i].flags);
6132         }
6133     }
6134     if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6135         BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6136         bxe_free_mem(sc);
6137         return (-1);
6138     }
6139 
6140     return (0);
6141 }
6142 
6143 static void
6144 bxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6145 {
6146     struct bxe_softc *sc;
6147     int i;
6148 
6149     sc = fp->sc;
6150 
6151     if (fp->rx_mbuf_tag == NULL) {
6152         return;
6153     }
6154 
6155     /* free all mbufs and unload all maps */
6156     for (i = 0; i < RX_BD_TOTAL; i++) {
6157         if (fp->rx_mbuf_chain[i].m_map != NULL) {
6158             bus_dmamap_sync(fp->rx_mbuf_tag,
6159                             fp->rx_mbuf_chain[i].m_map,
6160                             BUS_DMASYNC_POSTREAD);
6161             bus_dmamap_unload(fp->rx_mbuf_tag,
6162                               fp->rx_mbuf_chain[i].m_map);
6163         }
6164 
6165         if (fp->rx_mbuf_chain[i].m != NULL) {
6166             m_freem(fp->rx_mbuf_chain[i].m);
6167             fp->rx_mbuf_chain[i].m = NULL;
6168             fp->eth_q_stats.mbuf_alloc_rx--;
6169         }
6170     }
6171 }
6172 
6173 static void
6174 bxe_free_tpa_pool(struct bxe_fastpath *fp)
6175 {
6176     struct bxe_softc *sc;
6177     int i, max_agg_queues;
6178 
6179     sc = fp->sc;
6180 
6181     if (fp->rx_mbuf_tag == NULL) {
6182         return;
6183     }
6184 
6185     max_agg_queues = MAX_AGG_QS(sc);
6186 
6187     /* release all mbufs and unload all DMA maps in the TPA pool */
6188     for (i = 0; i < max_agg_queues; i++) {
6189         if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6190             bus_dmamap_sync(fp->rx_mbuf_tag,
6191                             fp->rx_tpa_info[i].bd.m_map,
6192                             BUS_DMASYNC_POSTREAD);
6193             bus_dmamap_unload(fp->rx_mbuf_tag,
6194                               fp->rx_tpa_info[i].bd.m_map);
6195         }
6196 
6197         if (fp->rx_tpa_info[i].bd.m != NULL) {
6198             m_freem(fp->rx_tpa_info[i].bd.m);
6199             fp->rx_tpa_info[i].bd.m = NULL;
6200             fp->eth_q_stats.mbuf_alloc_tpa--;
6201         }
6202     }
6203 }
6204 
6205 static void
6206 bxe_free_sge_chain(struct bxe_fastpath *fp)
6207 {
6208     struct bxe_softc *sc;
6209     int i;
6210 
6211     sc = fp->sc;
6212 
6213     if (fp->rx_sge_mbuf_tag == NULL) {
6214         return;
6215     }
6216 
6217     /* rree all mbufs and unload all maps */
6218     for (i = 0; i < RX_SGE_TOTAL; i++) {
6219         if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6220             bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6221                             fp->rx_sge_mbuf_chain[i].m_map,
6222                             BUS_DMASYNC_POSTREAD);
6223             bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6224                               fp->rx_sge_mbuf_chain[i].m_map);
6225         }
6226 
6227         if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6228             m_freem(fp->rx_sge_mbuf_chain[i].m);
6229             fp->rx_sge_mbuf_chain[i].m = NULL;
6230             fp->eth_q_stats.mbuf_alloc_sge--;
6231         }
6232     }
6233 }
6234 
6235 static void
6236 bxe_free_fp_buffers(struct bxe_softc *sc)
6237 {
6238     struct bxe_fastpath *fp;
6239     int i;
6240 
6241     for (i = 0; i < sc->num_queues; i++) {
6242         fp = &sc->fp[i];
6243 
6244 #if __FreeBSD_version >= 800000
6245         if (fp->tx_br != NULL) {
6246             /* just in case bxe_mq_flush() wasn't called */
6247             if (mtx_initialized(&fp->tx_mtx)) {
6248                 struct mbuf *m;
6249 
6250                 BXE_FP_TX_LOCK(fp);
6251                 while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6252                     m_freem(m);
6253                 BXE_FP_TX_UNLOCK(fp);
6254             }
6255         }
6256 #endif
6257 
6258         /* free all RX buffers */
6259         bxe_free_rx_bd_chain(fp);
6260         bxe_free_tpa_pool(fp);
6261         bxe_free_sge_chain(fp);
6262 
6263         if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6264             BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6265                   fp->eth_q_stats.mbuf_alloc_rx);
6266         }
6267 
6268         if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6269             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6270                   fp->eth_q_stats.mbuf_alloc_sge);
6271         }
6272 
6273         if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6274             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6275                   fp->eth_q_stats.mbuf_alloc_tpa);
6276         }
6277 
6278         if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6279             BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6280                   fp->eth_q_stats.mbuf_alloc_tx);
6281         }
6282 
6283         /* XXX verify all mbufs were reclaimed */
6284     }
6285 }
6286 
6287 static int
6288 bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6289                      uint16_t            prev_index,
6290                      uint16_t            index)
6291 {
6292     struct bxe_sw_rx_bd *rx_buf;
6293     struct eth_rx_bd *rx_bd;
6294     bus_dma_segment_t segs[1];
6295     bus_dmamap_t map;
6296     struct mbuf *m;
6297     int nsegs, rc;
6298 
6299     rc = 0;
6300 
6301     /* allocate the new RX BD mbuf */
6302     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6303     if (__predict_false(m == NULL)) {
6304         fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6305         return (ENOBUFS);
6306     }
6307 
6308     fp->eth_q_stats.mbuf_alloc_rx++;
6309 
6310     /* initialize the mbuf buffer length */
6311     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6312 
6313     /* map the mbuf into non-paged pool */
6314     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6315                                  fp->rx_mbuf_spare_map,
6316                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6317     if (__predict_false(rc != 0)) {
6318         fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6319         m_freem(m);
6320         fp->eth_q_stats.mbuf_alloc_rx--;
6321         return (rc);
6322     }
6323 
6324     /* all mbufs must map to a single segment */
6325     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6326 
6327     /* release any existing RX BD mbuf mappings */
6328 
6329     if (prev_index != index) {
6330         rx_buf = &fp->rx_mbuf_chain[prev_index];
6331 
6332         if (rx_buf->m_map != NULL) {
6333             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6334                             BUS_DMASYNC_POSTREAD);
6335             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6336         }
6337 
6338         /*
6339          * We only get here from bxe_rxeof() when the maximum number
6340          * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6341          * holds the mbuf in the prev_index so it's OK to NULL it out
6342          * here without concern of a memory leak.
6343          */
6344         fp->rx_mbuf_chain[prev_index].m = NULL;
6345     }
6346 
6347     rx_buf = &fp->rx_mbuf_chain[index];
6348 
6349     if (rx_buf->m_map != NULL) {
6350         bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6351                         BUS_DMASYNC_POSTREAD);
6352         bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6353     }
6354 
6355     /* save the mbuf and mapping info for a future packet */
6356     map = (prev_index != index) ?
6357               fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6358     rx_buf->m_map = fp->rx_mbuf_spare_map;
6359     fp->rx_mbuf_spare_map = map;
6360     bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6361                     BUS_DMASYNC_PREREAD);
6362     rx_buf->m = m;
6363 
6364     rx_bd = &fp->rx_chain[index];
6365     rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6366     rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6367 
6368     return (rc);
6369 }
6370 
6371 static int
6372 bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6373                       int                 queue)
6374 {
6375     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6376     bus_dma_segment_t segs[1];
6377     bus_dmamap_t map;
6378     struct mbuf *m;
6379     int nsegs;
6380     int rc = 0;
6381 
6382     /* allocate the new TPA mbuf */
6383     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6384     if (__predict_false(m == NULL)) {
6385         fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6386         return (ENOBUFS);
6387     }
6388 
6389     fp->eth_q_stats.mbuf_alloc_tpa++;
6390 
6391     /* initialize the mbuf buffer length */
6392     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6393 
6394     /* map the mbuf into non-paged pool */
6395     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6396                                  fp->rx_tpa_info_mbuf_spare_map,
6397                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6398     if (__predict_false(rc != 0)) {
6399         fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6400         m_free(m);
6401         fp->eth_q_stats.mbuf_alloc_tpa--;
6402         return (rc);
6403     }
6404 
6405     /* all mbufs must map to a single segment */
6406     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6407 
6408     /* release any existing TPA mbuf mapping */
6409     if (tpa_info->bd.m_map != NULL) {
6410         bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6411                         BUS_DMASYNC_POSTREAD);
6412         bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6413     }
6414 
6415     /* save the mbuf and mapping info for the TPA mbuf */
6416     map = tpa_info->bd.m_map;
6417     tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6418     fp->rx_tpa_info_mbuf_spare_map = map;
6419     bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6420                     BUS_DMASYNC_PREREAD);
6421     tpa_info->bd.m = m;
6422     tpa_info->seg = segs[0];
6423 
6424     return (rc);
6425 }
6426 
6427 /*
6428  * Allocate an mbuf and assign it to the receive scatter gather chain. The
6429  * caller must take care to save a copy of the existing mbuf in the SG mbuf
6430  * chain.
6431  */
6432 static int
6433 bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6434                       uint16_t            index)
6435 {
6436     struct bxe_sw_rx_bd *sge_buf;
6437     struct eth_rx_sge *sge;
6438     bus_dma_segment_t segs[1];
6439     bus_dmamap_t map;
6440     struct mbuf *m;
6441     int nsegs;
6442     int rc = 0;
6443 
6444     /* allocate a new SGE mbuf */
6445     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6446     if (__predict_false(m == NULL)) {
6447         fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6448         return (ENOMEM);
6449     }
6450 
6451     fp->eth_q_stats.mbuf_alloc_sge++;
6452 
6453     /* initialize the mbuf buffer length */
6454     m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6455 
6456     /* map the SGE mbuf into non-paged pool */
6457     rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6458                                  fp->rx_sge_mbuf_spare_map,
6459                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6460     if (__predict_false(rc != 0)) {
6461         fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6462         m_freem(m);
6463         fp->eth_q_stats.mbuf_alloc_sge--;
6464         return (rc);
6465     }
6466 
6467     /* all mbufs must map to a single segment */
6468     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6469 
6470     sge_buf = &fp->rx_sge_mbuf_chain[index];
6471 
6472     /* release any existing SGE mbuf mapping */
6473     if (sge_buf->m_map != NULL) {
6474         bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6475                         BUS_DMASYNC_POSTREAD);
6476         bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6477     }
6478 
6479     /* save the mbuf and mapping info for a future packet */
6480     map = sge_buf->m_map;
6481     sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6482     fp->rx_sge_mbuf_spare_map = map;
6483     bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6484                     BUS_DMASYNC_PREREAD);
6485     sge_buf->m = m;
6486 
6487     sge = &fp->rx_sge_chain[index];
6488     sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6489     sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6490 
6491     return (rc);
6492 }
6493 
6494 static __noinline int
6495 bxe_alloc_fp_buffers(struct bxe_softc *sc)
6496 {
6497     struct bxe_fastpath *fp;
6498     int i, j, rc = 0;
6499     int ring_prod, cqe_ring_prod;
6500     int max_agg_queues;
6501 
6502     for (i = 0; i < sc->num_queues; i++) {
6503         fp = &sc->fp[i];
6504 
6505         ring_prod = cqe_ring_prod = 0;
6506         fp->rx_bd_cons = 0;
6507         fp->rx_cq_cons = 0;
6508 
6509         /* allocate buffers for the RX BDs in RX BD chain */
6510         for (j = 0; j < sc->max_rx_bufs; j++) {
6511             rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6512             if (rc != 0) {
6513                 BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6514                       i, rc);
6515                 goto bxe_alloc_fp_buffers_error;
6516             }
6517 
6518             ring_prod     = RX_BD_NEXT(ring_prod);
6519             cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6520         }
6521 
6522         fp->rx_bd_prod = ring_prod;
6523         fp->rx_cq_prod = cqe_ring_prod;
6524         fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6525 
6526         max_agg_queues = MAX_AGG_QS(sc);
6527 
6528         fp->tpa_enable = TRUE;
6529 
6530         /* fill the TPA pool */
6531         for (j = 0; j < max_agg_queues; j++) {
6532             rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6533             if (rc != 0) {
6534                 BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6535                           i, j);
6536                 fp->tpa_enable = FALSE;
6537                 goto bxe_alloc_fp_buffers_error;
6538             }
6539 
6540             fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6541         }
6542 
6543         if (fp->tpa_enable) {
6544             /* fill the RX SGE chain */
6545             ring_prod = 0;
6546             for (j = 0; j < RX_SGE_USABLE; j++) {
6547                 rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6548                 if (rc != 0) {
6549                     BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6550                               i, ring_prod);
6551                     fp->tpa_enable = FALSE;
6552                     ring_prod = 0;
6553                     goto bxe_alloc_fp_buffers_error;
6554                 }
6555 
6556                 ring_prod = RX_SGE_NEXT(ring_prod);
6557             }
6558 
6559             fp->rx_sge_prod = ring_prod;
6560         }
6561     }
6562 
6563     return (0);
6564 
6565 bxe_alloc_fp_buffers_error:
6566 
6567     /* unwind what was already allocated */
6568     bxe_free_rx_bd_chain(fp);
6569     bxe_free_tpa_pool(fp);
6570     bxe_free_sge_chain(fp);
6571 
6572     return (ENOBUFS);
6573 }
6574 
6575 static void
6576 bxe_free_fw_stats_mem(struct bxe_softc *sc)
6577 {
6578     bxe_dma_free(sc, &sc->fw_stats_dma);
6579 
6580     sc->fw_stats_num = 0;
6581 
6582     sc->fw_stats_req_size = 0;
6583     sc->fw_stats_req = NULL;
6584     sc->fw_stats_req_mapping = 0;
6585 
6586     sc->fw_stats_data_size = 0;
6587     sc->fw_stats_data = NULL;
6588     sc->fw_stats_data_mapping = 0;
6589 }
6590 
6591 static int
6592 bxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6593 {
6594     uint8_t num_queue_stats;
6595     int num_groups;
6596 
6597     /* number of queues for statistics is number of eth queues */
6598     num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6599 
6600     /*
6601      * Total number of FW statistics requests =
6602      *   1 for port stats + 1 for PF stats + num of queues
6603      */
6604     sc->fw_stats_num = (2 + num_queue_stats);
6605 
6606     /*
6607      * Request is built from stats_query_header and an array of
6608      * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6609      * rules. The real number or requests is configured in the
6610      * stats_query_header.
6611      */
6612     num_groups =
6613         ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6614          ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6615 
6616     BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6617           sc->fw_stats_num, num_groups);
6618 
6619     sc->fw_stats_req_size =
6620         (sizeof(struct stats_query_header) +
6621          (num_groups * sizeof(struct stats_query_cmd_group)));
6622 
6623     /*
6624      * Data for statistics requests + stats_counter.
6625      * stats_counter holds per-STORM counters that are incremented when
6626      * STORM has finished with the current request. Memory for FCoE
6627      * offloaded statistics are counted anyway, even if they will not be sent.
6628      * VF stats are not accounted for here as the data of VF stats is stored
6629      * in memory allocated by the VF, not here.
6630      */
6631     sc->fw_stats_data_size =
6632         (sizeof(struct stats_counter) +
6633          sizeof(struct per_port_stats) +
6634          sizeof(struct per_pf_stats) +
6635          /* sizeof(struct fcoe_statistics_params) + */
6636          (sizeof(struct per_queue_stats) * num_queue_stats));
6637 
6638     if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6639                       &sc->fw_stats_dma, "fw stats") != 0) {
6640         bxe_free_fw_stats_mem(sc);
6641         return (-1);
6642     }
6643 
6644     /* set up the shortcuts */
6645 
6646     sc->fw_stats_req =
6647         (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6648     sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6649 
6650     sc->fw_stats_data =
6651         (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6652                                      sc->fw_stats_req_size);
6653     sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6654                                  sc->fw_stats_req_size);
6655 
6656     BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6657           (uintmax_t)sc->fw_stats_req_mapping);
6658 
6659     BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6660           (uintmax_t)sc->fw_stats_data_mapping);
6661 
6662     return (0);
6663 }
6664 
6665 /*
6666  * Bits map:
6667  * 0-7  - Engine0 load counter.
6668  * 8-15 - Engine1 load counter.
6669  * 16   - Engine0 RESET_IN_PROGRESS bit.
6670  * 17   - Engine1 RESET_IN_PROGRESS bit.
6671  * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6672  *        function on the engine
6673  * 19   - Engine1 ONE_IS_LOADED.
6674  * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6675  *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6676  *        for just the one belonging to its engine).
6677  */
6678 #define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6679 #define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6680 #define BXE_PATH0_LOAD_CNT_SHIFT  0
6681 #define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6682 #define BXE_PATH1_LOAD_CNT_SHIFT  8
6683 #define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6684 #define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6685 #define BXE_GLOBAL_RESET_BIT      0x00040000
6686 
6687 /* set the GLOBAL_RESET bit, should be run under rtnl lock */
6688 static void
6689 bxe_set_reset_global(struct bxe_softc *sc)
6690 {
6691     uint32_t val;
6692     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6693     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6694     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6695     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6696 }
6697 
6698 /* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6699 static void
6700 bxe_clear_reset_global(struct bxe_softc *sc)
6701 {
6702     uint32_t val;
6703     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6704     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6705     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6706     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6707 }
6708 
6709 /* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6710 static uint8_t
6711 bxe_reset_is_global(struct bxe_softc *sc)
6712 {
6713     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6714     BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6715     return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6716 }
6717 
6718 /* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6719 static void
6720 bxe_set_reset_done(struct bxe_softc *sc)
6721 {
6722     uint32_t val;
6723     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6724                                  BXE_PATH0_RST_IN_PROG_BIT;
6725 
6726     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6727 
6728     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6729     /* Clear the bit */
6730     val &= ~bit;
6731     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6732 
6733     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6734 }
6735 
6736 /* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6737 static void
6738 bxe_set_reset_in_progress(struct bxe_softc *sc)
6739 {
6740     uint32_t val;
6741     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6742                                  BXE_PATH0_RST_IN_PROG_BIT;
6743 
6744     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6745 
6746     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6747     /* Set the bit */
6748     val |= bit;
6749     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6750 
6751     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6752 }
6753 
6754 /* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6755 static uint8_t
6756 bxe_reset_is_done(struct bxe_softc *sc,
6757                   int              engine)
6758 {
6759     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6760     uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6761                             BXE_PATH0_RST_IN_PROG_BIT;
6762 
6763     /* return false if bit is set */
6764     return (val & bit) ? FALSE : TRUE;
6765 }
6766 
6767 /* get the load status for an engine, should be run under rtnl lock */
6768 static uint8_t
6769 bxe_get_load_status(struct bxe_softc *sc,
6770                     int              engine)
6771 {
6772     uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6773                              BXE_PATH0_LOAD_CNT_MASK;
6774     uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6775                               BXE_PATH0_LOAD_CNT_SHIFT;
6776     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6777 
6778     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6779 
6780     val = ((val & mask) >> shift);
6781 
6782     BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6783 
6784     return (val != 0);
6785 }
6786 
6787 /* set pf load mark */
6788 /* XXX needs to be under rtnl lock */
6789 static void
6790 bxe_set_pf_load(struct bxe_softc *sc)
6791 {
6792     uint32_t val;
6793     uint32_t val1;
6794     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6795                                   BXE_PATH0_LOAD_CNT_MASK;
6796     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6797                                    BXE_PATH0_LOAD_CNT_SHIFT;
6798 
6799     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6800 
6801     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6802     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6803 
6804     /* get the current counter value */
6805     val1 = ((val & mask) >> shift);
6806 
6807     /* set bit of this PF */
6808     val1 |= (1 << SC_ABS_FUNC(sc));
6809 
6810     /* clear the old value */
6811     val &= ~mask;
6812 
6813     /* set the new one */
6814     val |= ((val1 << shift) & mask);
6815 
6816     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6817 
6818     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6819 }
6820 
6821 /* clear pf load mark */
6822 /* XXX needs to be under rtnl lock */
6823 static uint8_t
6824 bxe_clear_pf_load(struct bxe_softc *sc)
6825 {
6826     uint32_t val1, val;
6827     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6828                                   BXE_PATH0_LOAD_CNT_MASK;
6829     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6830                                    BXE_PATH0_LOAD_CNT_SHIFT;
6831 
6832     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6833     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6834     BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6835 
6836     /* get the current counter value */
6837     val1 = (val & mask) >> shift;
6838 
6839     /* clear bit of that PF */
6840     val1 &= ~(1 << SC_ABS_FUNC(sc));
6841 
6842     /* clear the old value */
6843     val &= ~mask;
6844 
6845     /* set the new one */
6846     val |= ((val1 << shift) & mask);
6847 
6848     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6849     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6850     return (val1 != 0);
6851 }
6852 
6853 /* send load requrest to mcp and analyze response */
6854 static int
6855 bxe_nic_load_request(struct bxe_softc *sc,
6856                      uint32_t         *load_code)
6857 {
6858     /* init fw_seq */
6859     sc->fw_seq =
6860         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6861          DRV_MSG_SEQ_NUMBER_MASK);
6862 
6863     BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6864 
6865     /* get the current FW pulse sequence */
6866     sc->fw_drv_pulse_wr_seq =
6867         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6868          DRV_PULSE_SEQ_MASK);
6869 
6870     BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6871           sc->fw_drv_pulse_wr_seq);
6872 
6873     /* load request */
6874     (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6875                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6876 
6877     /* if the MCP fails to respond we must abort */
6878     if (!(*load_code)) {
6879         BLOGE(sc, "MCP response failure!\n");
6880         return (-1);
6881     }
6882 
6883     /* if MCP refused then must abort */
6884     if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6885         BLOGE(sc, "MCP refused load request\n");
6886         return (-1);
6887     }
6888 
6889     return (0);
6890 }
6891 
6892 /*
6893  * Check whether another PF has already loaded FW to chip. In virtualized
6894  * environments a pf from anoth VM may have already initialized the device
6895  * including loading FW.
6896  */
6897 static int
6898 bxe_nic_load_analyze_req(struct bxe_softc *sc,
6899                          uint32_t         load_code)
6900 {
6901     uint32_t my_fw, loaded_fw;
6902 
6903     /* is another pf loaded on this engine? */
6904     if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6905         (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6906         /* build my FW version dword */
6907         my_fw = (BCM_5710_FW_MAJOR_VERSION +
6908                  (BCM_5710_FW_MINOR_VERSION << 8 ) +
6909                  (BCM_5710_FW_REVISION_VERSION << 16) +
6910                  (BCM_5710_FW_ENGINEERING_VERSION << 24));
6911 
6912         /* read loaded FW from chip */
6913         loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6914         BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6915               loaded_fw, my_fw);
6916 
6917         /* abort nic load if version mismatch */
6918         if (my_fw != loaded_fw) {
6919             BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6920                   loaded_fw, my_fw);
6921             return (-1);
6922         }
6923     }
6924 
6925     return (0);
6926 }
6927 
6928 /* mark PMF if applicable */
6929 static void
6930 bxe_nic_load_pmf(struct bxe_softc *sc,
6931                  uint32_t         load_code)
6932 {
6933     uint32_t ncsi_oem_data_addr;
6934 
6935     if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6936         (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6937         (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6938         /*
6939          * Barrier here for ordering between the writing to sc->port.pmf here
6940          * and reading it from the periodic task.
6941          */
6942         sc->port.pmf = 1;
6943         mb();
6944     } else {
6945         sc->port.pmf = 0;
6946     }
6947 
6948     BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6949 
6950     /* XXX needed? */
6951     if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6952         if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6953             ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6954             if (ncsi_oem_data_addr) {
6955                 REG_WR(sc,
6956                        (ncsi_oem_data_addr +
6957                         offsetof(struct glob_ncsi_oem_data, driver_version)),
6958                        0);
6959             }
6960         }
6961     }
6962 }
6963 
6964 static void
6965 bxe_read_mf_cfg(struct bxe_softc *sc)
6966 {
6967     int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
6968     int abs_func;
6969     int vn;
6970 
6971     if (BXE_NOMCP(sc)) {
6972         return; /* what should be the default bvalue in this case */
6973     }
6974 
6975     /*
6976      * The formula for computing the absolute function number is...
6977      * For 2 port configuration (4 functions per port):
6978      *   abs_func = 2 * vn + SC_PORT + SC_PATH
6979      * For 4 port configuration (2 functions per port):
6980      *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
6981      */
6982     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
6983         abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
6984         if (abs_func >= E1H_FUNC_MAX) {
6985             break;
6986         }
6987         sc->devinfo.mf_info.mf_config[vn] =
6988             MFCFG_RD(sc, func_mf_config[abs_func].config);
6989     }
6990 
6991     if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
6992         FUNC_MF_CFG_FUNC_DISABLED) {
6993         BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
6994         sc->flags |= BXE_MF_FUNC_DIS;
6995     } else {
6996         BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
6997         sc->flags &= ~BXE_MF_FUNC_DIS;
6998     }
6999 }
7000 
7001 /* acquire split MCP access lock register */
7002 static int bxe_acquire_alr(struct bxe_softc *sc)
7003 {
7004     uint32_t j, val;
7005 
7006     for (j = 0; j < 1000; j++) {
7007         val = (1UL << 31);
7008         REG_WR(sc, GRCBASE_MCP + 0x9c, val);
7009         val = REG_RD(sc, GRCBASE_MCP + 0x9c);
7010         if (val & (1L << 31))
7011             break;
7012 
7013         DELAY(5000);
7014     }
7015 
7016     if (!(val & (1L << 31))) {
7017         BLOGE(sc, "Cannot acquire MCP access lock register\n");
7018         return (-1);
7019     }
7020 
7021     return (0);
7022 }
7023 
7024 /* release split MCP access lock register */
7025 static void bxe_release_alr(struct bxe_softc *sc)
7026 {
7027     REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
7028 }
7029 
7030 static void
7031 bxe_fan_failure(struct bxe_softc *sc)
7032 {
7033     int port = SC_PORT(sc);
7034     uint32_t ext_phy_config;
7035 
7036     /* mark the failure */
7037     ext_phy_config =
7038         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7039 
7040     ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7041     ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7042     SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7043              ext_phy_config);
7044 
7045     /* log the failure */
7046     BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7047               "the card to prevent permanent damage. "
7048               "Please contact OEM Support for assistance\n");
7049 
7050     /* XXX */
7051 #if 1
7052     bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7053 #else
7054     /*
7055      * Schedule device reset (unload)
7056      * This is due to some boards consuming sufficient power when driver is
7057      * up to overheat if fan fails.
7058      */
7059     bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7060     schedule_delayed_work(&sc->sp_rtnl_task, 0);
7061 #endif
7062 }
7063 
7064 /* this function is called upon a link interrupt */
7065 static void
7066 bxe_link_attn(struct bxe_softc *sc)
7067 {
7068     uint32_t pause_enabled = 0;
7069     struct host_port_stats *pstats;
7070     int cmng_fns;
7071 
7072     /* Make sure that we are synced with the current statistics */
7073     bxe_stats_handle(sc, STATS_EVENT_STOP);
7074 
7075     elink_link_update(&sc->link_params, &sc->link_vars);
7076 
7077     if (sc->link_vars.link_up) {
7078 
7079         /* dropless flow control */
7080         if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7081             pause_enabled = 0;
7082 
7083             if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7084                 pause_enabled = 1;
7085             }
7086 
7087             REG_WR(sc,
7088                    (BAR_USTRORM_INTMEM +
7089                     USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7090                    pause_enabled);
7091         }
7092 
7093         if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7094             pstats = BXE_SP(sc, port_stats);
7095             /* reset old mac stats */
7096             memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7097         }
7098 
7099         if (sc->state == BXE_STATE_OPEN) {
7100             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7101         }
7102     }
7103 
7104     if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7105         cmng_fns = bxe_get_cmng_fns_mode(sc);
7106 
7107         if (cmng_fns != CMNG_FNS_NONE) {
7108             bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7109             storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7110         } else {
7111             /* rate shaping and fairness are disabled */
7112             BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7113         }
7114     }
7115 
7116     bxe_link_report_locked(sc);
7117 
7118     if (IS_MF(sc)) {
7119         ; // XXX bxe_link_sync_notify(sc);
7120     }
7121 }
7122 
7123 static void
7124 bxe_attn_int_asserted(struct bxe_softc *sc,
7125                       uint32_t         asserted)
7126 {
7127     int port = SC_PORT(sc);
7128     uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7129                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
7130     uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7131                                         NIG_REG_MASK_INTERRUPT_PORT0;
7132     uint32_t aeu_mask;
7133     uint32_t nig_mask = 0;
7134     uint32_t reg_addr;
7135     uint32_t igu_acked;
7136     uint32_t cnt;
7137 
7138     if (sc->attn_state & asserted) {
7139         BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7140     }
7141 
7142     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7143 
7144     aeu_mask = REG_RD(sc, aeu_addr);
7145 
7146     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7147           aeu_mask, asserted);
7148 
7149     aeu_mask &= ~(asserted & 0x3ff);
7150 
7151     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7152 
7153     REG_WR(sc, aeu_addr, aeu_mask);
7154 
7155     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7156 
7157     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7158     sc->attn_state |= asserted;
7159     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7160 
7161     if (asserted & ATTN_HARD_WIRED_MASK) {
7162         if (asserted & ATTN_NIG_FOR_FUNC) {
7163 
7164 	    bxe_acquire_phy_lock(sc);
7165             /* save nig interrupt mask */
7166             nig_mask = REG_RD(sc, nig_int_mask_addr);
7167 
7168             /* If nig_mask is not set, no need to call the update function */
7169             if (nig_mask) {
7170                 REG_WR(sc, nig_int_mask_addr, 0);
7171 
7172                 bxe_link_attn(sc);
7173             }
7174 
7175             /* handle unicore attn? */
7176         }
7177 
7178         if (asserted & ATTN_SW_TIMER_4_FUNC) {
7179             BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7180         }
7181 
7182         if (asserted & GPIO_2_FUNC) {
7183             BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7184         }
7185 
7186         if (asserted & GPIO_3_FUNC) {
7187             BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7188         }
7189 
7190         if (asserted & GPIO_4_FUNC) {
7191             BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7192         }
7193 
7194         if (port == 0) {
7195             if (asserted & ATTN_GENERAL_ATTN_1) {
7196                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7197                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7198             }
7199             if (asserted & ATTN_GENERAL_ATTN_2) {
7200                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7201                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7202             }
7203             if (asserted & ATTN_GENERAL_ATTN_3) {
7204                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7205                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7206             }
7207         } else {
7208             if (asserted & ATTN_GENERAL_ATTN_4) {
7209                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7210                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7211             }
7212             if (asserted & ATTN_GENERAL_ATTN_5) {
7213                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7214                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7215             }
7216             if (asserted & ATTN_GENERAL_ATTN_6) {
7217                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7218                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7219             }
7220         }
7221     } /* hardwired */
7222 
7223     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7224         reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7225     } else {
7226         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7227     }
7228 
7229     BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7230           asserted,
7231           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7232     REG_WR(sc, reg_addr, asserted);
7233 
7234     /* now set back the mask */
7235     if (asserted & ATTN_NIG_FOR_FUNC) {
7236         /*
7237          * Verify that IGU ack through BAR was written before restoring
7238          * NIG mask. This loop should exit after 2-3 iterations max.
7239          */
7240         if (sc->devinfo.int_block != INT_BLOCK_HC) {
7241             cnt = 0;
7242 
7243             do {
7244                 igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7245             } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7246                      (++cnt < MAX_IGU_ATTN_ACK_TO));
7247 
7248             if (!igu_acked) {
7249                 BLOGE(sc, "Failed to verify IGU ack on time\n");
7250             }
7251 
7252             mb();
7253         }
7254 
7255         REG_WR(sc, nig_int_mask_addr, nig_mask);
7256 
7257 	bxe_release_phy_lock(sc);
7258     }
7259 }
7260 
7261 static void
7262 bxe_print_next_block(struct bxe_softc *sc,
7263                      int              idx,
7264                      const char       *blk)
7265 {
7266     BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7267 }
7268 
7269 static int
7270 bxe_check_blocks_with_parity0(struct bxe_softc *sc,
7271                               uint32_t         sig,
7272                               int              par_num,
7273                               uint8_t          print)
7274 {
7275     uint32_t cur_bit = 0;
7276     int i = 0;
7277 
7278     for (i = 0; sig; i++) {
7279         cur_bit = ((uint32_t)0x1 << i);
7280         if (sig & cur_bit) {
7281             switch (cur_bit) {
7282             case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7283                 if (print)
7284                     bxe_print_next_block(sc, par_num++, "BRB");
7285                 break;
7286             case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7287                 if (print)
7288                     bxe_print_next_block(sc, par_num++, "PARSER");
7289                 break;
7290             case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7291                 if (print)
7292                     bxe_print_next_block(sc, par_num++, "TSDM");
7293                 break;
7294             case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7295                 if (print)
7296                     bxe_print_next_block(sc, par_num++, "SEARCHER");
7297                 break;
7298             case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7299                 if (print)
7300                     bxe_print_next_block(sc, par_num++, "TCM");
7301                 break;
7302             case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7303                 if (print)
7304                     bxe_print_next_block(sc, par_num++, "TSEMI");
7305                 break;
7306             case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7307                 if (print)
7308                     bxe_print_next_block(sc, par_num++, "XPB");
7309                 break;
7310             }
7311 
7312             /* Clear the bit */
7313             sig &= ~cur_bit;
7314         }
7315     }
7316 
7317     return (par_num);
7318 }
7319 
7320 static int
7321 bxe_check_blocks_with_parity1(struct bxe_softc *sc,
7322                               uint32_t         sig,
7323                               int              par_num,
7324                               uint8_t          *global,
7325                               uint8_t          print)
7326 {
7327     int i = 0;
7328     uint32_t cur_bit = 0;
7329     for (i = 0; sig; i++) {
7330         cur_bit = ((uint32_t)0x1 << i);
7331         if (sig & cur_bit) {
7332             switch (cur_bit) {
7333             case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7334                 if (print)
7335                     bxe_print_next_block(sc, par_num++, "PBF");
7336                 break;
7337             case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7338                 if (print)
7339                     bxe_print_next_block(sc, par_num++, "QM");
7340                 break;
7341             case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7342                 if (print)
7343                     bxe_print_next_block(sc, par_num++, "TM");
7344                 break;
7345             case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7346                 if (print)
7347                     bxe_print_next_block(sc, par_num++, "XSDM");
7348                 break;
7349             case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7350                 if (print)
7351                     bxe_print_next_block(sc, par_num++, "XCM");
7352                 break;
7353             case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7354                 if (print)
7355                     bxe_print_next_block(sc, par_num++, "XSEMI");
7356                 break;
7357             case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7358                 if (print)
7359                     bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7360                 break;
7361             case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7362                 if (print)
7363                     bxe_print_next_block(sc, par_num++, "NIG");
7364                 break;
7365             case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7366                 if (print)
7367                     bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7368                 *global = TRUE;
7369                 break;
7370             case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7371                 if (print)
7372                     bxe_print_next_block(sc, par_num++, "DEBUG");
7373                 break;
7374             case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7375                 if (print)
7376                     bxe_print_next_block(sc, par_num++, "USDM");
7377                 break;
7378             case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7379                 if (print)
7380                     bxe_print_next_block(sc, par_num++, "UCM");
7381                 break;
7382             case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7383                 if (print)
7384                     bxe_print_next_block(sc, par_num++, "USEMI");
7385                 break;
7386             case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7387                 if (print)
7388                     bxe_print_next_block(sc, par_num++, "UPB");
7389                 break;
7390             case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7391                 if (print)
7392                     bxe_print_next_block(sc, par_num++, "CSDM");
7393                 break;
7394             case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7395                 if (print)
7396                     bxe_print_next_block(sc, par_num++, "CCM");
7397                 break;
7398             }
7399 
7400             /* Clear the bit */
7401             sig &= ~cur_bit;
7402         }
7403     }
7404 
7405     return (par_num);
7406 }
7407 
7408 static int
7409 bxe_check_blocks_with_parity2(struct bxe_softc *sc,
7410                               uint32_t         sig,
7411                               int              par_num,
7412                               uint8_t          print)
7413 {
7414     uint32_t cur_bit = 0;
7415     int i = 0;
7416 
7417     for (i = 0; sig; i++) {
7418         cur_bit = ((uint32_t)0x1 << i);
7419         if (sig & cur_bit) {
7420             switch (cur_bit) {
7421             case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7422                 if (print)
7423                     bxe_print_next_block(sc, par_num++, "CSEMI");
7424                 break;
7425             case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7426                 if (print)
7427                     bxe_print_next_block(sc, par_num++, "PXP");
7428                 break;
7429             case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7430                 if (print)
7431                     bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7432                 break;
7433             case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7434                 if (print)
7435                     bxe_print_next_block(sc, par_num++, "CFC");
7436                 break;
7437             case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7438                 if (print)
7439                     bxe_print_next_block(sc, par_num++, "CDU");
7440                 break;
7441             case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7442                 if (print)
7443                     bxe_print_next_block(sc, par_num++, "DMAE");
7444                 break;
7445             case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7446                 if (print)
7447                     bxe_print_next_block(sc, par_num++, "IGU");
7448                 break;
7449             case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7450                 if (print)
7451                     bxe_print_next_block(sc, par_num++, "MISC");
7452                 break;
7453             }
7454 
7455             /* Clear the bit */
7456             sig &= ~cur_bit;
7457         }
7458     }
7459 
7460     return (par_num);
7461 }
7462 
7463 static int
7464 bxe_check_blocks_with_parity3(struct bxe_softc *sc,
7465                               uint32_t         sig,
7466                               int              par_num,
7467                               uint8_t          *global,
7468                               uint8_t          print)
7469 {
7470     uint32_t cur_bit = 0;
7471     int i = 0;
7472 
7473     for (i = 0; sig; i++) {
7474         cur_bit = ((uint32_t)0x1 << i);
7475         if (sig & cur_bit) {
7476             switch (cur_bit) {
7477             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7478                 if (print)
7479                     bxe_print_next_block(sc, par_num++, "MCP ROM");
7480                 *global = TRUE;
7481                 break;
7482             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7483                 if (print)
7484                     bxe_print_next_block(sc, par_num++,
7485                               "MCP UMP RX");
7486                 *global = TRUE;
7487                 break;
7488             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7489                 if (print)
7490                     bxe_print_next_block(sc, par_num++,
7491                               "MCP UMP TX");
7492                 *global = TRUE;
7493                 break;
7494             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7495                 if (print)
7496                     bxe_print_next_block(sc, par_num++,
7497                               "MCP SCPAD");
7498                 *global = TRUE;
7499                 break;
7500             }
7501 
7502             /* Clear the bit */
7503             sig &= ~cur_bit;
7504         }
7505     }
7506 
7507     return (par_num);
7508 }
7509 
7510 static int
7511 bxe_check_blocks_with_parity4(struct bxe_softc *sc,
7512                               uint32_t         sig,
7513                               int              par_num,
7514                               uint8_t          print)
7515 {
7516     uint32_t cur_bit = 0;
7517     int i = 0;
7518 
7519     for (i = 0; sig; i++) {
7520         cur_bit = ((uint32_t)0x1 << i);
7521         if (sig & cur_bit) {
7522             switch (cur_bit) {
7523             case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7524                 if (print)
7525                     bxe_print_next_block(sc, par_num++, "PGLUE_B");
7526                 break;
7527             case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7528                 if (print)
7529                     bxe_print_next_block(sc, par_num++, "ATC");
7530                 break;
7531             }
7532 
7533             /* Clear the bit */
7534             sig &= ~cur_bit;
7535         }
7536     }
7537 
7538     return (par_num);
7539 }
7540 
7541 static uint8_t
7542 bxe_parity_attn(struct bxe_softc *sc,
7543                 uint8_t          *global,
7544                 uint8_t          print,
7545                 uint32_t         *sig)
7546 {
7547     int par_num = 0;
7548 
7549     if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7550         (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7551         (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7552         (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7553         (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7554         BLOGE(sc, "Parity error: HW block parity attention:\n"
7555                   "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7556               (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7557               (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7558               (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7559               (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7560               (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7561 
7562         if (print)
7563             BLOGI(sc, "Parity errors detected in blocks: ");
7564 
7565         par_num =
7566             bxe_check_blocks_with_parity0(sc, sig[0] &
7567                                           HW_PRTY_ASSERT_SET_0,
7568                                           par_num, print);
7569         par_num =
7570             bxe_check_blocks_with_parity1(sc, sig[1] &
7571                                           HW_PRTY_ASSERT_SET_1,
7572                                           par_num, global, print);
7573         par_num =
7574             bxe_check_blocks_with_parity2(sc, sig[2] &
7575                                           HW_PRTY_ASSERT_SET_2,
7576                                           par_num, print);
7577         par_num =
7578             bxe_check_blocks_with_parity3(sc, sig[3] &
7579                                           HW_PRTY_ASSERT_SET_3,
7580                                           par_num, global, print);
7581         par_num =
7582             bxe_check_blocks_with_parity4(sc, sig[4] &
7583                                           HW_PRTY_ASSERT_SET_4,
7584                                           par_num, print);
7585 
7586         if (print)
7587             BLOGI(sc, "\n");
7588 
7589         return (TRUE);
7590     }
7591 
7592     return (FALSE);
7593 }
7594 
7595 static uint8_t
7596 bxe_chk_parity_attn(struct bxe_softc *sc,
7597                     uint8_t          *global,
7598                     uint8_t          print)
7599 {
7600     struct attn_route attn = { {0} };
7601     int port = SC_PORT(sc);
7602 
7603     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7604     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7605     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7606     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7607 
7608     /*
7609      * Since MCP attentions can't be disabled inside the block, we need to
7610      * read AEU registers to see whether they're currently disabled
7611      */
7612     attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7613                                       : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7614                          MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7615                         ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7616 
7617 
7618     if (!CHIP_IS_E1x(sc))
7619         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7620 
7621     return (bxe_parity_attn(sc, global, print, attn.sig));
7622 }
7623 
7624 static void
7625 bxe_attn_int_deasserted4(struct bxe_softc *sc,
7626                          uint32_t         attn)
7627 {
7628     uint32_t val;
7629 
7630     if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7631         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7632         BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7633         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7634             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7635         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7636             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7637         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7638             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7639         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7640             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7641         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7642             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7643         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7644             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7645         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7646             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7647         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7648             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7649         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7650             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7651     }
7652 
7653     if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7654         val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7655         BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7656         if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7657             BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7658         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7659             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7660         if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7661             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7662         if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7663             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7664         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7665             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7666         if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7667             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7668     }
7669 
7670     if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7671                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7672         BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7673               (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7674                                  AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7675     }
7676 }
7677 
7678 static void
7679 bxe_e1h_disable(struct bxe_softc *sc)
7680 {
7681     int port = SC_PORT(sc);
7682 
7683     bxe_tx_disable(sc);
7684 
7685     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7686 }
7687 
7688 static void
7689 bxe_e1h_enable(struct bxe_softc *sc)
7690 {
7691     int port = SC_PORT(sc);
7692 
7693     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7694 
7695     // XXX bxe_tx_enable(sc);
7696 }
7697 
7698 /*
7699  * called due to MCP event (on pmf):
7700  *   reread new bandwidth configuration
7701  *   configure FW
7702  *   notify others function about the change
7703  */
7704 static void
7705 bxe_config_mf_bw(struct bxe_softc *sc)
7706 {
7707     if (sc->link_vars.link_up) {
7708         bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7709         // XXX bxe_link_sync_notify(sc);
7710     }
7711 
7712     storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7713 }
7714 
7715 static void
7716 bxe_set_mf_bw(struct bxe_softc *sc)
7717 {
7718     bxe_config_mf_bw(sc);
7719     bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7720 }
7721 
7722 static void
7723 bxe_handle_eee_event(struct bxe_softc *sc)
7724 {
7725     BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7726     bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7727 }
7728 
7729 #define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7730 
7731 static void
7732 bxe_drv_info_ether_stat(struct bxe_softc *sc)
7733 {
7734     struct eth_stats_info *ether_stat =
7735         &sc->sp->drv_info_to_mcp.ether_stat;
7736 
7737     strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7738             ETH_STAT_INFO_VERSION_LEN);
7739 
7740     /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7741     sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7742                                           DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7743                                           ether_stat->mac_local + MAC_PAD,
7744                                           MAC_PAD, ETH_ALEN);
7745 
7746     ether_stat->mtu_size = sc->mtu;
7747 
7748     ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7749     if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
7750         ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7751     }
7752 
7753     // XXX ether_stat->feature_flags |= ???;
7754 
7755     ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7756 
7757     ether_stat->txq_size = sc->tx_ring_size;
7758     ether_stat->rxq_size = sc->rx_ring_size;
7759 }
7760 
7761 static void
7762 bxe_handle_drv_info_req(struct bxe_softc *sc)
7763 {
7764     enum drv_info_opcode op_code;
7765     uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7766 
7767     /* if drv_info version supported by MFW doesn't match - send NACK */
7768     if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7769         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7770         return;
7771     }
7772 
7773     op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7774                DRV_INFO_CONTROL_OP_CODE_SHIFT);
7775 
7776     memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7777 
7778     switch (op_code) {
7779     case ETH_STATS_OPCODE:
7780         bxe_drv_info_ether_stat(sc);
7781         break;
7782     case FCOE_STATS_OPCODE:
7783     case ISCSI_STATS_OPCODE:
7784     default:
7785         /* if op code isn't supported - send NACK */
7786         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7787         return;
7788     }
7789 
7790     /*
7791      * If we got drv_info attn from MFW then these fields are defined in
7792      * shmem2 for sure
7793      */
7794     SHMEM2_WR(sc, drv_info_host_addr_lo,
7795               U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7796     SHMEM2_WR(sc, drv_info_host_addr_hi,
7797               U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7798 
7799     bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7800 }
7801 
7802 static void
7803 bxe_dcc_event(struct bxe_softc *sc,
7804               uint32_t         dcc_event)
7805 {
7806     BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7807 
7808     if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7809         /*
7810          * This is the only place besides the function initialization
7811          * where the sc->flags can change so it is done without any
7812          * locks
7813          */
7814         if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7815             BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7816             sc->flags |= BXE_MF_FUNC_DIS;
7817             bxe_e1h_disable(sc);
7818         } else {
7819             BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7820             sc->flags &= ~BXE_MF_FUNC_DIS;
7821             bxe_e1h_enable(sc);
7822         }
7823         dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7824     }
7825 
7826     if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7827         bxe_config_mf_bw(sc);
7828         dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7829     }
7830 
7831     /* Report results to MCP */
7832     if (dcc_event)
7833         bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7834     else
7835         bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7836 }
7837 
7838 static void
7839 bxe_pmf_update(struct bxe_softc *sc)
7840 {
7841     int port = SC_PORT(sc);
7842     uint32_t val;
7843 
7844     sc->port.pmf = 1;
7845     BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7846 
7847     /*
7848      * We need the mb() to ensure the ordering between the writing to
7849      * sc->port.pmf here and reading it from the bxe_periodic_task().
7850      */
7851     mb();
7852 
7853     /* queue a periodic task */
7854     // XXX schedule task...
7855 
7856     // XXX bxe_dcbx_pmf_update(sc);
7857 
7858     /* enable nig attention */
7859     val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7860     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7861         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7862         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7863     } else if (!CHIP_IS_E1x(sc)) {
7864         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7865         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7866     }
7867 
7868     bxe_stats_handle(sc, STATS_EVENT_PMF);
7869 }
7870 
7871 static int
7872 bxe_mc_assert(struct bxe_softc *sc)
7873 {
7874     char last_idx;
7875     int i, rc = 0;
7876     uint32_t row0, row1, row2, row3;
7877 
7878     /* XSTORM */
7879     last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7880     if (last_idx)
7881         BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7882 
7883     /* print the asserts */
7884     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7885 
7886         row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7887         row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7888         row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7889         row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7890 
7891         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7892             BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7893                   i, row3, row2, row1, row0);
7894             rc++;
7895         } else {
7896             break;
7897         }
7898     }
7899 
7900     /* TSTORM */
7901     last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7902     if (last_idx) {
7903         BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7904     }
7905 
7906     /* print the asserts */
7907     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7908 
7909         row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7910         row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7911         row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7912         row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7913 
7914         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7915             BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7916                   i, row3, row2, row1, row0);
7917             rc++;
7918         } else {
7919             break;
7920         }
7921     }
7922 
7923     /* CSTORM */
7924     last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7925     if (last_idx) {
7926         BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7927     }
7928 
7929     /* print the asserts */
7930     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7931 
7932         row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7933         row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7934         row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7935         row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7936 
7937         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7938             BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7939                   i, row3, row2, row1, row0);
7940             rc++;
7941         } else {
7942             break;
7943         }
7944     }
7945 
7946     /* USTORM */
7947     last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7948     if (last_idx) {
7949         BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7950     }
7951 
7952     /* print the asserts */
7953     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7954 
7955         row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7956         row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7957         row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7958         row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7959 
7960         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7961             BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7962                   i, row3, row2, row1, row0);
7963             rc++;
7964         } else {
7965             break;
7966         }
7967     }
7968 
7969     return (rc);
7970 }
7971 
7972 static void
7973 bxe_attn_int_deasserted3(struct bxe_softc *sc,
7974                          uint32_t         attn)
7975 {
7976     int func = SC_FUNC(sc);
7977     uint32_t val;
7978 
7979     if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
7980 
7981         if (attn & BXE_PMF_LINK_ASSERT(sc)) {
7982 
7983             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
7984             bxe_read_mf_cfg(sc);
7985             sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
7986                 MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
7987             val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
7988 
7989             if (val & DRV_STATUS_DCC_EVENT_MASK)
7990                 bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
7991 
7992             if (val & DRV_STATUS_SET_MF_BW)
7993                 bxe_set_mf_bw(sc);
7994 
7995             if (val & DRV_STATUS_DRV_INFO_REQ)
7996                 bxe_handle_drv_info_req(sc);
7997 
7998             if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
7999                 bxe_pmf_update(sc);
8000 
8001             if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
8002                 bxe_handle_eee_event(sc);
8003 
8004             if (sc->link_vars.periodic_flags &
8005                 ELINK_PERIODIC_FLAGS_LINK_EVENT) {
8006                 /* sync with link */
8007 		bxe_acquire_phy_lock(sc);
8008                 sc->link_vars.periodic_flags &=
8009                     ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
8010 		bxe_release_phy_lock(sc);
8011                 if (IS_MF(sc))
8012                     ; // XXX bxe_link_sync_notify(sc);
8013                 bxe_link_report(sc);
8014             }
8015 
8016             /*
8017              * Always call it here: bxe_link_report() will
8018              * prevent the link indication duplication.
8019              */
8020             bxe_link_status_update(sc);
8021 
8022         } else if (attn & BXE_MC_ASSERT_BITS) {
8023 
8024             BLOGE(sc, "MC assert!\n");
8025             bxe_mc_assert(sc);
8026             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
8027             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
8028             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
8029             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
8030             bxe_panic(sc, ("MC assert!\n"));
8031 
8032         } else if (attn & BXE_MCP_ASSERT) {
8033 
8034             BLOGE(sc, "MCP assert!\n");
8035             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8036             // XXX bxe_fw_dump(sc);
8037 
8038         } else {
8039             BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8040         }
8041     }
8042 
8043     if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8044         BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8045         if (attn & BXE_GRC_TIMEOUT) {
8046             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8047             BLOGE(sc, "GRC time-out 0x%08x\n", val);
8048         }
8049         if (attn & BXE_GRC_RSV) {
8050             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8051             BLOGE(sc, "GRC reserved 0x%08x\n", val);
8052         }
8053         REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8054     }
8055 }
8056 
8057 static void
8058 bxe_attn_int_deasserted2(struct bxe_softc *sc,
8059                          uint32_t         attn)
8060 {
8061     int port = SC_PORT(sc);
8062     int reg_offset;
8063     uint32_t val0, mask0, val1, mask1;
8064     uint32_t val;
8065 
8066     if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8067         val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8068         BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8069         /* CFC error attention */
8070         if (val & 0x2) {
8071             BLOGE(sc, "FATAL error from CFC\n");
8072         }
8073     }
8074 
8075     if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8076         val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8077         BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8078         /* RQ_USDMDP_FIFO_OVERFLOW */
8079         if (val & 0x18000) {
8080             BLOGE(sc, "FATAL error from PXP\n");
8081         }
8082 
8083         if (!CHIP_IS_E1x(sc)) {
8084             val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8085             BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8086         }
8087     }
8088 
8089 #define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8090 #define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8091 
8092     if (attn & AEU_PXP2_HW_INT_BIT) {
8093         /*  CQ47854 workaround do not panic on
8094          *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8095          */
8096         if (!CHIP_IS_E1x(sc)) {
8097             mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8098             val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8099             mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8100             val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8101             /*
8102              * If the olny PXP2_EOP_ERROR_BIT is set in
8103              * STS0 and STS1 - clear it
8104              *
8105              * probably we lose additional attentions between
8106              * STS0 and STS_CLR0, in this case user will not
8107              * be notified about them
8108              */
8109             if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8110                 !(val1 & mask1))
8111                 val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8112 
8113             /* print the register, since no one can restore it */
8114             BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8115 
8116             /*
8117              * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8118              * then notify
8119              */
8120             if (val0 & PXP2_EOP_ERROR_BIT) {
8121                 BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8122 
8123                 /*
8124                  * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8125                  * set then clear attention from PXP2 block without panic
8126                  */
8127                 if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8128                     ((val1 & mask1) == 0))
8129                     attn &= ~AEU_PXP2_HW_INT_BIT;
8130             }
8131         }
8132     }
8133 
8134     if (attn & HW_INTERRUT_ASSERT_SET_2) {
8135         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8136                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8137 
8138         val = REG_RD(sc, reg_offset);
8139         val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8140         REG_WR(sc, reg_offset, val);
8141 
8142         BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8143               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8144         bxe_panic(sc, ("HW block attention set2\n"));
8145     }
8146 }
8147 
8148 static void
8149 bxe_attn_int_deasserted1(struct bxe_softc *sc,
8150                          uint32_t         attn)
8151 {
8152     int port = SC_PORT(sc);
8153     int reg_offset;
8154     uint32_t val;
8155 
8156     if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8157         val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8158         BLOGE(sc, "DB hw attention 0x%08x\n", val);
8159         /* DORQ discard attention */
8160         if (val & 0x2) {
8161             BLOGE(sc, "FATAL error from DORQ\n");
8162         }
8163     }
8164 
8165     if (attn & HW_INTERRUT_ASSERT_SET_1) {
8166         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8167                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8168 
8169         val = REG_RD(sc, reg_offset);
8170         val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8171         REG_WR(sc, reg_offset, val);
8172 
8173         BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8174               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8175         bxe_panic(sc, ("HW block attention set1\n"));
8176     }
8177 }
8178 
8179 static void
8180 bxe_attn_int_deasserted0(struct bxe_softc *sc,
8181                          uint32_t         attn)
8182 {
8183     int port = SC_PORT(sc);
8184     int reg_offset;
8185     uint32_t val;
8186 
8187     reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8188                           MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8189 
8190     if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8191         val = REG_RD(sc, reg_offset);
8192         val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8193         REG_WR(sc, reg_offset, val);
8194 
8195         BLOGW(sc, "SPIO5 hw attention\n");
8196 
8197         /* Fan failure attention */
8198         elink_hw_reset_phy(&sc->link_params);
8199         bxe_fan_failure(sc);
8200     }
8201 
8202     if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8203 	bxe_acquire_phy_lock(sc);
8204         elink_handle_module_detect_int(&sc->link_params);
8205 	bxe_release_phy_lock(sc);
8206     }
8207 
8208     if (attn & HW_INTERRUT_ASSERT_SET_0) {
8209         val = REG_RD(sc, reg_offset);
8210         val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8211         REG_WR(sc, reg_offset, val);
8212 
8213         bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8214                        (attn & HW_INTERRUT_ASSERT_SET_0)));
8215     }
8216 }
8217 
8218 static void
8219 bxe_attn_int_deasserted(struct bxe_softc *sc,
8220                         uint32_t         deasserted)
8221 {
8222     struct attn_route attn;
8223     struct attn_route *group_mask;
8224     int port = SC_PORT(sc);
8225     int index;
8226     uint32_t reg_addr;
8227     uint32_t val;
8228     uint32_t aeu_mask;
8229     uint8_t global = FALSE;
8230 
8231     /*
8232      * Need to take HW lock because MCP or other port might also
8233      * try to handle this event.
8234      */
8235     bxe_acquire_alr(sc);
8236 
8237     if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8238         /* XXX
8239          * In case of parity errors don't handle attentions so that
8240          * other function would "see" parity errors.
8241          */
8242         sc->recovery_state = BXE_RECOVERY_INIT;
8243         // XXX schedule a recovery task...
8244         /* disable HW interrupts */
8245         bxe_int_disable(sc);
8246         bxe_release_alr(sc);
8247         return;
8248     }
8249 
8250     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8251     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8252     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8253     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8254     if (!CHIP_IS_E1x(sc)) {
8255         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8256     } else {
8257         attn.sig[4] = 0;
8258     }
8259 
8260     BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8261           attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8262 
8263     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8264         if (deasserted & (1 << index)) {
8265             group_mask = &sc->attn_group[index];
8266 
8267             BLOGD(sc, DBG_INTR,
8268                   "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8269                   group_mask->sig[0], group_mask->sig[1],
8270                   group_mask->sig[2], group_mask->sig[3],
8271                   group_mask->sig[4]);
8272 
8273             bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8274             bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8275             bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8276             bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8277             bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8278         }
8279     }
8280 
8281     bxe_release_alr(sc);
8282 
8283     if (sc->devinfo.int_block == INT_BLOCK_HC) {
8284         reg_addr = (HC_REG_COMMAND_REG + port*32 +
8285                     COMMAND_REG_ATTN_BITS_CLR);
8286     } else {
8287         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8288     }
8289 
8290     val = ~deasserted;
8291     BLOGD(sc, DBG_INTR,
8292           "about to mask 0x%08x at %s addr 0x%08x\n", val,
8293           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8294     REG_WR(sc, reg_addr, val);
8295 
8296     if (~sc->attn_state & deasserted) {
8297         BLOGE(sc, "IGU error\n");
8298     }
8299 
8300     reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8301                       MISC_REG_AEU_MASK_ATTN_FUNC_0;
8302 
8303     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8304 
8305     aeu_mask = REG_RD(sc, reg_addr);
8306 
8307     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8308           aeu_mask, deasserted);
8309     aeu_mask |= (deasserted & 0x3ff);
8310     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8311 
8312     REG_WR(sc, reg_addr, aeu_mask);
8313     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8314 
8315     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8316     sc->attn_state &= ~deasserted;
8317     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8318 }
8319 
8320 static void
8321 bxe_attn_int(struct bxe_softc *sc)
8322 {
8323     /* read local copy of bits */
8324     uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8325     uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8326     uint32_t attn_state = sc->attn_state;
8327 
8328     /* look for changed bits */
8329     uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8330     uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8331 
8332     BLOGD(sc, DBG_INTR,
8333           "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8334           attn_bits, attn_ack, asserted, deasserted);
8335 
8336     if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8337         BLOGE(sc, "BAD attention state\n");
8338     }
8339 
8340     /* handle bits that were raised */
8341     if (asserted) {
8342         bxe_attn_int_asserted(sc, asserted);
8343     }
8344 
8345     if (deasserted) {
8346         bxe_attn_int_deasserted(sc, deasserted);
8347     }
8348 }
8349 
8350 static uint16_t
8351 bxe_update_dsb_idx(struct bxe_softc *sc)
8352 {
8353     struct host_sp_status_block *def_sb = sc->def_sb;
8354     uint16_t rc = 0;
8355 
8356     mb(); /* status block is written to by the chip */
8357 
8358     if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8359         sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8360         rc |= BXE_DEF_SB_ATT_IDX;
8361     }
8362 
8363     if (sc->def_idx != def_sb->sp_sb.running_index) {
8364         sc->def_idx = def_sb->sp_sb.running_index;
8365         rc |= BXE_DEF_SB_IDX;
8366     }
8367 
8368     mb();
8369 
8370     return (rc);
8371 }
8372 
8373 static inline struct ecore_queue_sp_obj *
8374 bxe_cid_to_q_obj(struct bxe_softc *sc,
8375                  uint32_t         cid)
8376 {
8377     BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8378     return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8379 }
8380 
8381 static void
8382 bxe_handle_mcast_eqe(struct bxe_softc *sc)
8383 {
8384     struct ecore_mcast_ramrod_params rparam;
8385     int rc;
8386 
8387     memset(&rparam, 0, sizeof(rparam));
8388 
8389     rparam.mcast_obj = &sc->mcast_obj;
8390 
8391     BXE_MCAST_LOCK(sc);
8392 
8393     /* clear pending state for the last command */
8394     sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8395 
8396     /* if there are pending mcast commands - send them */
8397     if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8398         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8399         if (rc < 0) {
8400             BLOGD(sc, DBG_SP,
8401                 "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8402         }
8403     }
8404 
8405     BXE_MCAST_UNLOCK(sc);
8406 }
8407 
8408 static void
8409 bxe_handle_classification_eqe(struct bxe_softc      *sc,
8410                               union event_ring_elem *elem)
8411 {
8412     unsigned long ramrod_flags = 0;
8413     int rc = 0;
8414     uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8415     struct ecore_vlan_mac_obj *vlan_mac_obj;
8416 
8417     /* always push next commands out, don't wait here */
8418     bit_set(&ramrod_flags, RAMROD_CONT);
8419 
8420     switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8421     case ECORE_FILTER_MAC_PENDING:
8422         BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8423         vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8424         break;
8425 
8426     case ECORE_FILTER_MCAST_PENDING:
8427         BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8428         /*
8429          * This is only relevant for 57710 where multicast MACs are
8430          * configured as unicast MACs using the same ramrod.
8431          */
8432         bxe_handle_mcast_eqe(sc);
8433         return;
8434 
8435     default:
8436         BLOGE(sc, "Unsupported classification command: %d\n",
8437               elem->message.data.eth_event.echo);
8438         return;
8439     }
8440 
8441     rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8442 
8443     if (rc < 0) {
8444         BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8445     } else if (rc > 0) {
8446         BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8447     }
8448 }
8449 
8450 static void
8451 bxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8452                        union event_ring_elem *elem)
8453 {
8454     bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8455 
8456     /* send rx_mode command again if was requested */
8457     if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8458                                &sc->sp_state)) {
8459         bxe_set_storm_rx_mode(sc);
8460     }
8461 }
8462 
8463 static void
8464 bxe_update_eq_prod(struct bxe_softc *sc,
8465                    uint16_t         prod)
8466 {
8467     storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8468     wmb(); /* keep prod updates ordered */
8469 }
8470 
8471 static void
8472 bxe_eq_int(struct bxe_softc *sc)
8473 {
8474     uint16_t hw_cons, sw_cons, sw_prod;
8475     union event_ring_elem *elem;
8476     uint8_t echo;
8477     uint32_t cid;
8478     uint8_t opcode;
8479     int spqe_cnt = 0;
8480     struct ecore_queue_sp_obj *q_obj;
8481     struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8482     struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8483 
8484     hw_cons = le16toh(*sc->eq_cons_sb);
8485 
8486     /*
8487      * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8488      * when we get to the next-page we need to adjust so the loop
8489      * condition below will be met. The next element is the size of a
8490      * regular element and hence incrementing by 1
8491      */
8492     if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8493         hw_cons++;
8494     }
8495 
8496     /*
8497      * This function may never run in parallel with itself for a
8498      * specific sc and no need for a read memory barrier here.
8499      */
8500     sw_cons = sc->eq_cons;
8501     sw_prod = sc->eq_prod;
8502 
8503     BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8504           hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8505 
8506     for (;
8507          sw_cons != hw_cons;
8508          sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8509 
8510         elem = &sc->eq[EQ_DESC(sw_cons)];
8511 
8512         /* elem CID originates from FW, actually LE */
8513         cid = SW_CID(elem->message.data.cfc_del_event.cid);
8514         opcode = elem->message.opcode;
8515 
8516         /* handle eq element */
8517         switch (opcode) {
8518 
8519         case EVENT_RING_OPCODE_STAT_QUERY:
8520             BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8521                   sc->stats_comp++);
8522             /* nothing to do with stats comp */
8523             goto next_spqe;
8524 
8525         case EVENT_RING_OPCODE_CFC_DEL:
8526             /* handle according to cid range */
8527             /* we may want to verify here that the sc state is HALTING */
8528             BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8529             q_obj = bxe_cid_to_q_obj(sc, cid);
8530             if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8531                 break;
8532             }
8533             goto next_spqe;
8534 
8535         case EVENT_RING_OPCODE_STOP_TRAFFIC:
8536             BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8537             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8538                 break;
8539             }
8540             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8541             goto next_spqe;
8542 
8543         case EVENT_RING_OPCODE_START_TRAFFIC:
8544             BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8545             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8546                 break;
8547             }
8548             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8549             goto next_spqe;
8550 
8551         case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8552             echo = elem->message.data.function_update_event.echo;
8553             if (echo == SWITCH_UPDATE) {
8554                 BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8555                 if (f_obj->complete_cmd(sc, f_obj,
8556                                         ECORE_F_CMD_SWITCH_UPDATE)) {
8557                     break;
8558                 }
8559             }
8560             else {
8561                 BLOGD(sc, DBG_SP,
8562                       "AFEX: ramrod completed FUNCTION_UPDATE\n");
8563             }
8564             goto next_spqe;
8565 
8566         case EVENT_RING_OPCODE_FORWARD_SETUP:
8567             q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8568             if (q_obj->complete_cmd(sc, q_obj,
8569                                     ECORE_Q_CMD_SETUP_TX_ONLY)) {
8570                 break;
8571             }
8572             goto next_spqe;
8573 
8574         case EVENT_RING_OPCODE_FUNCTION_START:
8575             BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8576             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8577                 break;
8578             }
8579             goto next_spqe;
8580 
8581         case EVENT_RING_OPCODE_FUNCTION_STOP:
8582             BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8583             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8584                 break;
8585             }
8586             goto next_spqe;
8587         }
8588 
8589         switch (opcode | sc->state) {
8590         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8591         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8592             cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8593             BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8594             rss_raw->clear_pending(rss_raw);
8595             break;
8596 
8597         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8598         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8599         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8600         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8601         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8602         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8603             BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8604             bxe_handle_classification_eqe(sc, elem);
8605             break;
8606 
8607         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8608         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8609         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8610             BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8611             bxe_handle_mcast_eqe(sc);
8612             break;
8613 
8614         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8615         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8616         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8617             BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8618             bxe_handle_rx_mode_eqe(sc, elem);
8619             break;
8620 
8621         default:
8622             /* unknown event log error and continue */
8623             BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8624                   elem->message.opcode, sc->state);
8625         }
8626 
8627 next_spqe:
8628         spqe_cnt++;
8629     } /* for */
8630 
8631     mb();
8632     atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8633 
8634     sc->eq_cons = sw_cons;
8635     sc->eq_prod = sw_prod;
8636 
8637     /* make sure that above mem writes were issued towards the memory */
8638     wmb();
8639 
8640     /* update producer */
8641     bxe_update_eq_prod(sc, sc->eq_prod);
8642 }
8643 
8644 static void
8645 bxe_handle_sp_tq(void *context,
8646                  int  pending)
8647 {
8648     struct bxe_softc *sc = (struct bxe_softc *)context;
8649     uint16_t status;
8650 
8651     BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8652 
8653     /* what work needs to be performed? */
8654     status = bxe_update_dsb_idx(sc);
8655 
8656     BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8657 
8658     /* HW attentions */
8659     if (status & BXE_DEF_SB_ATT_IDX) {
8660         BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8661         bxe_attn_int(sc);
8662         status &= ~BXE_DEF_SB_ATT_IDX;
8663     }
8664 
8665     /* SP events: STAT_QUERY and others */
8666     if (status & BXE_DEF_SB_IDX) {
8667         /* handle EQ completions */
8668         BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8669         bxe_eq_int(sc);
8670         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8671                    le16toh(sc->def_idx), IGU_INT_NOP, 1);
8672         status &= ~BXE_DEF_SB_IDX;
8673     }
8674 
8675     /* if status is non zero then something went wrong */
8676     if (__predict_false(status)) {
8677         BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8678     }
8679 
8680     /* ack status block only if something was actually handled */
8681     bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8682                le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8683 
8684     /*
8685      * Must be called after the EQ processing (since eq leads to sriov
8686      * ramrod completion flows).
8687      * This flow may have been scheduled by the arrival of a ramrod
8688      * completion, or by the sriov code rescheduling itself.
8689      */
8690     // XXX bxe_iov_sp_task(sc);
8691 
8692 }
8693 
8694 static void
8695 bxe_handle_fp_tq(void *context,
8696                  int  pending)
8697 {
8698     struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8699     struct bxe_softc *sc = fp->sc;
8700     uint8_t more_tx = FALSE;
8701     uint8_t more_rx = FALSE;
8702 
8703     BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8704 
8705     /* XXX
8706      * IFF_DRV_RUNNING state can't be checked here since we process
8707      * slowpath events on a client queue during setup. Instead
8708      * we need to add a "process/continue" flag here that the driver
8709      * can use to tell the task here not to do anything.
8710      */
8711 #if 0
8712     if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
8713         return;
8714     }
8715 #endif
8716 
8717     /* update the fastpath index */
8718     bxe_update_fp_sb_idx(fp);
8719 
8720     /* XXX add loop here if ever support multiple tx CoS */
8721     /* fp->txdata[cos] */
8722     if (bxe_has_tx_work(fp)) {
8723         BXE_FP_TX_LOCK(fp);
8724         more_tx = bxe_txeof(sc, fp);
8725         BXE_FP_TX_UNLOCK(fp);
8726     }
8727 
8728     if (bxe_has_rx_work(fp)) {
8729         more_rx = bxe_rxeof(sc, fp);
8730     }
8731 
8732     if (more_rx /*|| more_tx*/) {
8733         /* still more work to do */
8734         taskqueue_enqueue(fp->tq, &fp->tq_task);
8735         return;
8736     }
8737 
8738     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8739                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8740 }
8741 
8742 static void
8743 bxe_task_fp(struct bxe_fastpath *fp)
8744 {
8745     struct bxe_softc *sc = fp->sc;
8746     uint8_t more_tx = FALSE;
8747     uint8_t more_rx = FALSE;
8748 
8749     BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8750 
8751     /* update the fastpath index */
8752     bxe_update_fp_sb_idx(fp);
8753 
8754     /* XXX add loop here if ever support multiple tx CoS */
8755     /* fp->txdata[cos] */
8756     if (bxe_has_tx_work(fp)) {
8757         BXE_FP_TX_LOCK(fp);
8758         more_tx = bxe_txeof(sc, fp);
8759         BXE_FP_TX_UNLOCK(fp);
8760     }
8761 
8762     if (bxe_has_rx_work(fp)) {
8763         more_rx = bxe_rxeof(sc, fp);
8764     }
8765 
8766     if (more_rx /*|| more_tx*/) {
8767         /* still more work to do, bail out if this ISR and process later */
8768         taskqueue_enqueue(fp->tq, &fp->tq_task);
8769         return;
8770     }
8771 
8772     /*
8773      * Here we write the fastpath index taken before doing any tx or rx work.
8774      * It is very well possible other hw events occurred up to this point and
8775      * they were actually processed accordingly above. Since we're going to
8776      * write an older fastpath index, an interrupt is coming which we might
8777      * not do any work in.
8778      */
8779     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8780                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8781 }
8782 
8783 /*
8784  * Legacy interrupt entry point.
8785  *
8786  * Verifies that the controller generated the interrupt and
8787  * then calls a separate routine to handle the various
8788  * interrupt causes: link, RX, and TX.
8789  */
8790 static void
8791 bxe_intr_legacy(void *xsc)
8792 {
8793     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8794     struct bxe_fastpath *fp;
8795     uint16_t status, mask;
8796     int i;
8797 
8798     BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8799 
8800     /*
8801      * 0 for ustorm, 1 for cstorm
8802      * the bits returned from ack_int() are 0-15
8803      * bit 0 = attention status block
8804      * bit 1 = fast path status block
8805      * a mask of 0x2 or more = tx/rx event
8806      * a mask of 1 = slow path event
8807      */
8808 
8809     status = bxe_ack_int(sc);
8810 
8811     /* the interrupt is not for us */
8812     if (__predict_false(status == 0)) {
8813         BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8814         return;
8815     }
8816 
8817     BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8818 
8819     FOR_EACH_ETH_QUEUE(sc, i) {
8820         fp = &sc->fp[i];
8821         mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8822         if (status & mask) {
8823             /* acknowledge and disable further fastpath interrupts */
8824             bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8825             bxe_task_fp(fp);
8826             status &= ~mask;
8827         }
8828     }
8829 
8830     if (__predict_false(status & 0x1)) {
8831         /* acknowledge and disable further slowpath interrupts */
8832         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8833 
8834         /* schedule slowpath handler */
8835         taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8836 
8837         status &= ~0x1;
8838     }
8839 
8840     if (__predict_false(status)) {
8841         BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8842     }
8843 }
8844 
8845 /* slowpath interrupt entry point */
8846 static void
8847 bxe_intr_sp(void *xsc)
8848 {
8849     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8850 
8851     BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8852 
8853     /* acknowledge and disable further slowpath interrupts */
8854     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8855 
8856     /* schedule slowpath handler */
8857     taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8858 }
8859 
8860 /* fastpath interrupt entry point */
8861 static void
8862 bxe_intr_fp(void *xfp)
8863 {
8864     struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8865     struct bxe_softc *sc = fp->sc;
8866 
8867     BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8868 
8869     BLOGD(sc, DBG_INTR,
8870           "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8871           curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8872 
8873     /* acknowledge and disable further fastpath interrupts */
8874     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8875 
8876     bxe_task_fp(fp);
8877 }
8878 
8879 /* Release all interrupts allocated by the driver. */
8880 static void
8881 bxe_interrupt_free(struct bxe_softc *sc)
8882 {
8883     int i;
8884 
8885     switch (sc->interrupt_mode) {
8886     case INTR_MODE_INTX:
8887         BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8888         if (sc->intr[0].resource != NULL) {
8889             bus_release_resource(sc->dev,
8890                                  SYS_RES_IRQ,
8891                                  sc->intr[0].rid,
8892                                  sc->intr[0].resource);
8893         }
8894         break;
8895     case INTR_MODE_MSI:
8896         for (i = 0; i < sc->intr_count; i++) {
8897             BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
8898             if (sc->intr[i].resource && sc->intr[i].rid) {
8899                 bus_release_resource(sc->dev,
8900                                      SYS_RES_IRQ,
8901                                      sc->intr[i].rid,
8902                                      sc->intr[i].resource);
8903             }
8904         }
8905         pci_release_msi(sc->dev);
8906         break;
8907     case INTR_MODE_MSIX:
8908         for (i = 0; i < sc->intr_count; i++) {
8909             BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8910             if (sc->intr[i].resource && sc->intr[i].rid) {
8911                 bus_release_resource(sc->dev,
8912                                      SYS_RES_IRQ,
8913                                      sc->intr[i].rid,
8914                                      sc->intr[i].resource);
8915             }
8916         }
8917         pci_release_msi(sc->dev);
8918         break;
8919     default:
8920         /* nothing to do as initial allocation failed */
8921         break;
8922     }
8923 }
8924 
8925 /*
8926  * This function determines and allocates the appropriate
8927  * interrupt based on system capabilites and user request.
8928  *
8929  * The user may force a particular interrupt mode, specify
8930  * the number of receive queues, specify the method for
8931  * distribuitng received frames to receive queues, or use
8932  * the default settings which will automatically select the
8933  * best supported combination.  In addition, the OS may or
8934  * may not support certain combinations of these settings.
8935  * This routine attempts to reconcile the settings requested
8936  * by the user with the capabilites available from the system
8937  * to select the optimal combination of features.
8938  *
8939  * Returns:
8940  *   0 = Success, !0 = Failure.
8941  */
8942 static int
8943 bxe_interrupt_alloc(struct bxe_softc *sc)
8944 {
8945     int msix_count = 0;
8946     int msi_count = 0;
8947     int num_requested = 0;
8948     int num_allocated = 0;
8949     int rid, i, j;
8950     int rc;
8951 
8952     /* get the number of available MSI/MSI-X interrupts from the OS */
8953     if (sc->interrupt_mode > 0) {
8954         if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8955             msix_count = pci_msix_count(sc->dev);
8956         }
8957 
8958         if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
8959             msi_count = pci_msi_count(sc->dev);
8960         }
8961 
8962         BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
8963               msi_count, msix_count);
8964     }
8965 
8966     do { /* try allocating MSI-X interrupt resources (at least 2) */
8967         if (sc->interrupt_mode != INTR_MODE_MSIX) {
8968             break;
8969         }
8970 
8971         if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
8972             (msix_count < 2)) {
8973             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8974             break;
8975         }
8976 
8977         /* ask for the necessary number of MSI-X vectors */
8978         num_requested = min((sc->num_queues + 1), msix_count);
8979 
8980         BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
8981 
8982         num_allocated = num_requested;
8983         if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
8984             BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
8985             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8986             break;
8987         }
8988 
8989         if (num_allocated < 2) { /* possible? */
8990             BLOGE(sc, "MSI-X allocation less than 2!\n");
8991             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8992             pci_release_msi(sc->dev);
8993             break;
8994         }
8995 
8996         BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
8997               num_requested, num_allocated);
8998 
8999         /* best effort so use the number of vectors allocated to us */
9000         sc->intr_count = num_allocated;
9001         sc->num_queues = num_allocated - 1;
9002 
9003         rid = 1; /* initial resource identifier */
9004 
9005         /* allocate the MSI-X vectors */
9006         for (i = 0; i < num_allocated; i++) {
9007             sc->intr[i].rid = (rid + i);
9008 
9009             if ((sc->intr[i].resource =
9010                  bus_alloc_resource_any(sc->dev,
9011                                         SYS_RES_IRQ,
9012                                         &sc->intr[i].rid,
9013                                         RF_ACTIVE)) == NULL) {
9014                 BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
9015                       i, (rid + i));
9016 
9017                 for (j = (i - 1); j >= 0; j--) {
9018                     bus_release_resource(sc->dev,
9019                                          SYS_RES_IRQ,
9020                                          sc->intr[j].rid,
9021                                          sc->intr[j].resource);
9022                 }
9023 
9024                 sc->intr_count = 0;
9025                 sc->num_queues = 0;
9026                 sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9027                 pci_release_msi(sc->dev);
9028                 break;
9029             }
9030 
9031             BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9032         }
9033     } while (0);
9034 
9035     do { /* try allocating MSI vector resources (at least 2) */
9036         if (sc->interrupt_mode != INTR_MODE_MSI) {
9037             break;
9038         }
9039 
9040         if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9041             (msi_count < 1)) {
9042             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9043             break;
9044         }
9045 
9046         /* ask for a single MSI vector */
9047         num_requested = 1;
9048 
9049         BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9050 
9051         num_allocated = num_requested;
9052         if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9053             BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9054             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9055             break;
9056         }
9057 
9058         if (num_allocated != 1) { /* possible? */
9059             BLOGE(sc, "MSI allocation is not 1!\n");
9060             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9061             pci_release_msi(sc->dev);
9062             break;
9063         }
9064 
9065         BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9066               num_requested, num_allocated);
9067 
9068         /* best effort so use the number of vectors allocated to us */
9069         sc->intr_count = num_allocated;
9070         sc->num_queues = num_allocated;
9071 
9072         rid = 1; /* initial resource identifier */
9073 
9074         sc->intr[0].rid = rid;
9075 
9076         if ((sc->intr[0].resource =
9077              bus_alloc_resource_any(sc->dev,
9078                                     SYS_RES_IRQ,
9079                                     &sc->intr[0].rid,
9080                                     RF_ACTIVE)) == NULL) {
9081             BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9082             sc->intr_count = 0;
9083             sc->num_queues = 0;
9084             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9085             pci_release_msi(sc->dev);
9086             break;
9087         }
9088 
9089         BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9090     } while (0);
9091 
9092     do { /* try allocating INTx vector resources */
9093         if (sc->interrupt_mode != INTR_MODE_INTX) {
9094             break;
9095         }
9096 
9097         BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9098 
9099         /* only one vector for INTx */
9100         sc->intr_count = 1;
9101         sc->num_queues = 1;
9102 
9103         rid = 0; /* initial resource identifier */
9104 
9105         sc->intr[0].rid = rid;
9106 
9107         if ((sc->intr[0].resource =
9108              bus_alloc_resource_any(sc->dev,
9109                                     SYS_RES_IRQ,
9110                                     &sc->intr[0].rid,
9111                                     (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9112             BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9113             sc->intr_count = 0;
9114             sc->num_queues = 0;
9115             sc->interrupt_mode = -1; /* Failed! */
9116             break;
9117         }
9118 
9119         BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9120     } while (0);
9121 
9122     if (sc->interrupt_mode == -1) {
9123         BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9124         rc = 1;
9125     } else {
9126         BLOGD(sc, DBG_LOAD,
9127               "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9128               sc->interrupt_mode, sc->num_queues);
9129         rc = 0;
9130     }
9131 
9132     return (rc);
9133 }
9134 
9135 static void
9136 bxe_interrupt_detach(struct bxe_softc *sc)
9137 {
9138     struct bxe_fastpath *fp;
9139     int i;
9140 
9141     /* release interrupt resources */
9142     for (i = 0; i < sc->intr_count; i++) {
9143         if (sc->intr[i].resource && sc->intr[i].tag) {
9144             BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9145             bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9146         }
9147     }
9148 
9149     for (i = 0; i < sc->num_queues; i++) {
9150         fp = &sc->fp[i];
9151         if (fp->tq) {
9152             taskqueue_drain(fp->tq, &fp->tq_task);
9153             taskqueue_free(fp->tq);
9154             fp->tq = NULL;
9155         }
9156     }
9157 
9158 
9159     if (sc->sp_tq) {
9160         taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9161         taskqueue_free(sc->sp_tq);
9162         sc->sp_tq = NULL;
9163     }
9164 }
9165 
9166 /*
9167  * Enables interrupts and attach to the ISR.
9168  *
9169  * When using multiple MSI/MSI-X vectors the first vector
9170  * is used for slowpath operations while all remaining
9171  * vectors are used for fastpath operations.  If only a
9172  * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9173  * ISR must look for both slowpath and fastpath completions.
9174  */
9175 static int
9176 bxe_interrupt_attach(struct bxe_softc *sc)
9177 {
9178     struct bxe_fastpath *fp;
9179     int rc = 0;
9180     int i;
9181 
9182     snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9183              "bxe%d_sp_tq", sc->unit);
9184     TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9185     sc->sp_tq = taskqueue_create_fast(sc->sp_tq_name, M_NOWAIT,
9186                                       taskqueue_thread_enqueue,
9187                                       &sc->sp_tq);
9188     taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9189                             "%s", sc->sp_tq_name);
9190 
9191 
9192     for (i = 0; i < sc->num_queues; i++) {
9193         fp = &sc->fp[i];
9194         snprintf(fp->tq_name, sizeof(fp->tq_name),
9195                  "bxe%d_fp%d_tq", sc->unit, i);
9196         TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9197         fp->tq = taskqueue_create_fast(fp->tq_name, M_NOWAIT,
9198                                        taskqueue_thread_enqueue,
9199                                        &fp->tq);
9200         taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9201                                 "%s", fp->tq_name);
9202     }
9203 
9204     /* setup interrupt handlers */
9205     if (sc->interrupt_mode == INTR_MODE_MSIX) {
9206         BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9207 
9208         /*
9209          * Setup the interrupt handler. Note that we pass the driver instance
9210          * to the interrupt handler for the slowpath.
9211          */
9212         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9213                                  (INTR_TYPE_NET | INTR_MPSAFE),
9214                                  NULL, bxe_intr_sp, sc,
9215                                  &sc->intr[0].tag)) != 0) {
9216             BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9217             goto bxe_interrupt_attach_exit;
9218         }
9219 
9220         bus_describe_intr(sc->dev, sc->intr[0].resource,
9221                           sc->intr[0].tag, "sp");
9222 
9223         /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9224 
9225         /* initialize the fastpath vectors (note the first was used for sp) */
9226         for (i = 0; i < sc->num_queues; i++) {
9227             fp = &sc->fp[i];
9228             BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9229 
9230             /*
9231              * Setup the interrupt handler. Note that we pass the
9232              * fastpath context to the interrupt handler in this
9233              * case.
9234              */
9235             if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9236                                      (INTR_TYPE_NET | INTR_MPSAFE),
9237                                      NULL, bxe_intr_fp, fp,
9238                                      &sc->intr[i + 1].tag)) != 0) {
9239                 BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9240                       (i + 1), rc);
9241                 goto bxe_interrupt_attach_exit;
9242             }
9243 
9244             bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9245                               sc->intr[i + 1].tag, "fp%02d", i);
9246 
9247             /* bind the fastpath instance to a cpu */
9248             if (sc->num_queues > 1) {
9249                 bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9250             }
9251 
9252             fp->state = BXE_FP_STATE_IRQ;
9253         }
9254     } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9255         BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9256 
9257         /*
9258          * Setup the interrupt handler. Note that we pass the
9259          * driver instance to the interrupt handler which
9260          * will handle both the slowpath and fastpath.
9261          */
9262         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9263                                  (INTR_TYPE_NET | INTR_MPSAFE),
9264                                  NULL, bxe_intr_legacy, sc,
9265                                  &sc->intr[0].tag)) != 0) {
9266             BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9267             goto bxe_interrupt_attach_exit;
9268         }
9269 
9270     } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9271         BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9272 
9273         /*
9274          * Setup the interrupt handler. Note that we pass the
9275          * driver instance to the interrupt handler which
9276          * will handle both the slowpath and fastpath.
9277          */
9278         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9279                                  (INTR_TYPE_NET | INTR_MPSAFE),
9280                                  NULL, bxe_intr_legacy, sc,
9281                                  &sc->intr[0].tag)) != 0) {
9282             BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9283             goto bxe_interrupt_attach_exit;
9284         }
9285     }
9286 
9287 bxe_interrupt_attach_exit:
9288 
9289     return (rc);
9290 }
9291 
9292 static int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9293 static int  bxe_init_hw_common(struct bxe_softc *sc);
9294 static int  bxe_init_hw_port(struct bxe_softc *sc);
9295 static int  bxe_init_hw_func(struct bxe_softc *sc);
9296 static void bxe_reset_common(struct bxe_softc *sc);
9297 static void bxe_reset_port(struct bxe_softc *sc);
9298 static void bxe_reset_func(struct bxe_softc *sc);
9299 static int  bxe_gunzip_init(struct bxe_softc *sc);
9300 static void bxe_gunzip_end(struct bxe_softc *sc);
9301 static int  bxe_init_firmware(struct bxe_softc *sc);
9302 static void bxe_release_firmware(struct bxe_softc *sc);
9303 
9304 static struct
9305 ecore_func_sp_drv_ops bxe_func_sp_drv = {
9306     .init_hw_cmn_chip = bxe_init_hw_common_chip,
9307     .init_hw_cmn      = bxe_init_hw_common,
9308     .init_hw_port     = bxe_init_hw_port,
9309     .init_hw_func     = bxe_init_hw_func,
9310 
9311     .reset_hw_cmn     = bxe_reset_common,
9312     .reset_hw_port    = bxe_reset_port,
9313     .reset_hw_func    = bxe_reset_func,
9314 
9315     .gunzip_init      = bxe_gunzip_init,
9316     .gunzip_end       = bxe_gunzip_end,
9317 
9318     .init_fw          = bxe_init_firmware,
9319     .release_fw       = bxe_release_firmware,
9320 };
9321 
9322 static void
9323 bxe_init_func_obj(struct bxe_softc *sc)
9324 {
9325     sc->dmae_ready = 0;
9326 
9327     ecore_init_func_obj(sc,
9328                         &sc->func_obj,
9329                         BXE_SP(sc, func_rdata),
9330                         BXE_SP_MAPPING(sc, func_rdata),
9331                         BXE_SP(sc, func_afex_rdata),
9332                         BXE_SP_MAPPING(sc, func_afex_rdata),
9333                         &bxe_func_sp_drv);
9334 }
9335 
9336 static int
9337 bxe_init_hw(struct bxe_softc *sc,
9338             uint32_t         load_code)
9339 {
9340     struct ecore_func_state_params func_params = { NULL };
9341     int rc;
9342 
9343     /* prepare the parameters for function state transitions */
9344     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9345 
9346     func_params.f_obj = &sc->func_obj;
9347     func_params.cmd = ECORE_F_CMD_HW_INIT;
9348 
9349     func_params.params.hw_init.load_phase = load_code;
9350 
9351     /*
9352      * Via a plethora of function pointers, we will eventually reach
9353      * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9354      */
9355     rc = ecore_func_state_change(sc, &func_params);
9356 
9357     return (rc);
9358 }
9359 
9360 static void
9361 bxe_fill(struct bxe_softc *sc,
9362          uint32_t         addr,
9363          int              fill,
9364          uint32_t         len)
9365 {
9366     uint32_t i;
9367 
9368     if (!(len % 4) && !(addr % 4)) {
9369         for (i = 0; i < len; i += 4) {
9370             REG_WR(sc, (addr + i), fill);
9371         }
9372     } else {
9373         for (i = 0; i < len; i++) {
9374             REG_WR8(sc, (addr + i), fill);
9375         }
9376     }
9377 }
9378 
9379 /* writes FP SP data to FW - data_size in dwords */
9380 static void
9381 bxe_wr_fp_sb_data(struct bxe_softc *sc,
9382                   int              fw_sb_id,
9383                   uint32_t         *sb_data_p,
9384                   uint32_t         data_size)
9385 {
9386     int index;
9387 
9388     for (index = 0; index < data_size; index++) {
9389         REG_WR(sc,
9390                (BAR_CSTRORM_INTMEM +
9391                 CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9392                 (sizeof(uint32_t) * index)),
9393                *(sb_data_p + index));
9394     }
9395 }
9396 
9397 static void
9398 bxe_zero_fp_sb(struct bxe_softc *sc,
9399                int              fw_sb_id)
9400 {
9401     struct hc_status_block_data_e2 sb_data_e2;
9402     struct hc_status_block_data_e1x sb_data_e1x;
9403     uint32_t *sb_data_p;
9404     uint32_t data_size = 0;
9405 
9406     if (!CHIP_IS_E1x(sc)) {
9407         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9408         sb_data_e2.common.state = SB_DISABLED;
9409         sb_data_e2.common.p_func.vf_valid = FALSE;
9410         sb_data_p = (uint32_t *)&sb_data_e2;
9411         data_size = (sizeof(struct hc_status_block_data_e2) /
9412                      sizeof(uint32_t));
9413     } else {
9414         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9415         sb_data_e1x.common.state = SB_DISABLED;
9416         sb_data_e1x.common.p_func.vf_valid = FALSE;
9417         sb_data_p = (uint32_t *)&sb_data_e1x;
9418         data_size = (sizeof(struct hc_status_block_data_e1x) /
9419                      sizeof(uint32_t));
9420     }
9421 
9422     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9423 
9424     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9425              0, CSTORM_STATUS_BLOCK_SIZE);
9426     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9427              0, CSTORM_SYNC_BLOCK_SIZE);
9428 }
9429 
9430 static void
9431 bxe_wr_sp_sb_data(struct bxe_softc               *sc,
9432                   struct hc_sp_status_block_data *sp_sb_data)
9433 {
9434     int i;
9435 
9436     for (i = 0;
9437          i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9438          i++) {
9439         REG_WR(sc,
9440                (BAR_CSTRORM_INTMEM +
9441                 CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9442                 (i * sizeof(uint32_t))),
9443                *((uint32_t *)sp_sb_data + i));
9444     }
9445 }
9446 
9447 static void
9448 bxe_zero_sp_sb(struct bxe_softc *sc)
9449 {
9450     struct hc_sp_status_block_data sp_sb_data;
9451 
9452     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9453 
9454     sp_sb_data.state           = SB_DISABLED;
9455     sp_sb_data.p_func.vf_valid = FALSE;
9456 
9457     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9458 
9459     bxe_fill(sc,
9460              (BAR_CSTRORM_INTMEM +
9461               CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9462               0, CSTORM_SP_STATUS_BLOCK_SIZE);
9463     bxe_fill(sc,
9464              (BAR_CSTRORM_INTMEM +
9465               CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9466               0, CSTORM_SP_SYNC_BLOCK_SIZE);
9467 }
9468 
9469 static void
9470 bxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9471                              int                       igu_sb_id,
9472                              int                       igu_seg_id)
9473 {
9474     hc_sm->igu_sb_id      = igu_sb_id;
9475     hc_sm->igu_seg_id     = igu_seg_id;
9476     hc_sm->timer_value    = 0xFF;
9477     hc_sm->time_to_expire = 0xFFFFFFFF;
9478 }
9479 
9480 static void
9481 bxe_map_sb_state_machines(struct hc_index_data *index_data)
9482 {
9483     /* zero out state machine indices */
9484 
9485     /* rx indices */
9486     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9487 
9488     /* tx indices */
9489     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9490     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9491     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9492     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9493 
9494     /* map indices */
9495 
9496     /* rx indices */
9497     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9498         (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9499 
9500     /* tx indices */
9501     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9502         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9503     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9504         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9505     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9506         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9507     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9508         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9509 }
9510 
9511 static void
9512 bxe_init_sb(struct bxe_softc *sc,
9513             bus_addr_t       busaddr,
9514             int              vfid,
9515             uint8_t          vf_valid,
9516             int              fw_sb_id,
9517             int              igu_sb_id)
9518 {
9519     struct hc_status_block_data_e2  sb_data_e2;
9520     struct hc_status_block_data_e1x sb_data_e1x;
9521     struct hc_status_block_sm       *hc_sm_p;
9522     uint32_t *sb_data_p;
9523     int igu_seg_id;
9524     int data_size;
9525 
9526     if (CHIP_INT_MODE_IS_BC(sc)) {
9527         igu_seg_id = HC_SEG_ACCESS_NORM;
9528     } else {
9529         igu_seg_id = IGU_SEG_ACCESS_NORM;
9530     }
9531 
9532     bxe_zero_fp_sb(sc, fw_sb_id);
9533 
9534     if (!CHIP_IS_E1x(sc)) {
9535         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9536         sb_data_e2.common.state = SB_ENABLED;
9537         sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9538         sb_data_e2.common.p_func.vf_id = vfid;
9539         sb_data_e2.common.p_func.vf_valid = vf_valid;
9540         sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9541         sb_data_e2.common.same_igu_sb_1b = TRUE;
9542         sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9543         sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9544         hc_sm_p = sb_data_e2.common.state_machine;
9545         sb_data_p = (uint32_t *)&sb_data_e2;
9546         data_size = (sizeof(struct hc_status_block_data_e2) /
9547                      sizeof(uint32_t));
9548         bxe_map_sb_state_machines(sb_data_e2.index_data);
9549     } else {
9550         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9551         sb_data_e1x.common.state = SB_ENABLED;
9552         sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9553         sb_data_e1x.common.p_func.vf_id = 0xff;
9554         sb_data_e1x.common.p_func.vf_valid = FALSE;
9555         sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9556         sb_data_e1x.common.same_igu_sb_1b = TRUE;
9557         sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9558         sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9559         hc_sm_p = sb_data_e1x.common.state_machine;
9560         sb_data_p = (uint32_t *)&sb_data_e1x;
9561         data_size = (sizeof(struct hc_status_block_data_e1x) /
9562                      sizeof(uint32_t));
9563         bxe_map_sb_state_machines(sb_data_e1x.index_data);
9564     }
9565 
9566     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9567     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9568 
9569     BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9570 
9571     /* write indices to HW - PCI guarantees endianity of regpairs */
9572     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9573 }
9574 
9575 static inline uint8_t
9576 bxe_fp_qzone_id(struct bxe_fastpath *fp)
9577 {
9578     if (CHIP_IS_E1x(fp->sc)) {
9579         return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9580     } else {
9581         return (fp->cl_id);
9582     }
9583 }
9584 
9585 static inline uint32_t
9586 bxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9587                            struct bxe_fastpath *fp)
9588 {
9589     uint32_t offset = BAR_USTRORM_INTMEM;
9590 
9591     if (!CHIP_IS_E1x(sc)) {
9592         offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9593     } else {
9594         offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9595     }
9596 
9597     return (offset);
9598 }
9599 
9600 static void
9601 bxe_init_eth_fp(struct bxe_softc *sc,
9602                 int              idx)
9603 {
9604     struct bxe_fastpath *fp = &sc->fp[idx];
9605     uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9606     unsigned long q_type = 0;
9607     int cos;
9608 
9609     fp->sc    = sc;
9610     fp->index = idx;
9611 
9612     fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9613     fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9614 
9615     fp->cl_id = (CHIP_IS_E1x(sc)) ?
9616                     (SC_L_ID(sc) + idx) :
9617                     /* want client ID same as IGU SB ID for non-E1 */
9618                     fp->igu_sb_id;
9619     fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9620 
9621     /* setup sb indices */
9622     if (!CHIP_IS_E1x(sc)) {
9623         fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9624         fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9625     } else {
9626         fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9627         fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9628     }
9629 
9630     /* init shortcut */
9631     fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9632 
9633     fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9634 
9635     /*
9636      * XXX If multiple CoS is ever supported then each fastpath structure
9637      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9638      */
9639     for (cos = 0; cos < sc->max_cos; cos++) {
9640         cids[cos] = idx;
9641     }
9642     fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9643 
9644     /* nothing more for a VF to do */
9645     if (IS_VF(sc)) {
9646         return;
9647     }
9648 
9649     bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9650                 fp->fw_sb_id, fp->igu_sb_id);
9651 
9652     bxe_update_fp_sb_idx(fp);
9653 
9654     /* Configure Queue State object */
9655     bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9656     bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9657 
9658     ecore_init_queue_obj(sc,
9659                          &sc->sp_objs[idx].q_obj,
9660                          fp->cl_id,
9661                          cids,
9662                          sc->max_cos,
9663                          SC_FUNC(sc),
9664                          BXE_SP(sc, q_rdata),
9665                          BXE_SP_MAPPING(sc, q_rdata),
9666                          q_type);
9667 
9668     /* configure classification DBs */
9669     ecore_init_mac_obj(sc,
9670                        &sc->sp_objs[idx].mac_obj,
9671                        fp->cl_id,
9672                        idx,
9673                        SC_FUNC(sc),
9674                        BXE_SP(sc, mac_rdata),
9675                        BXE_SP_MAPPING(sc, mac_rdata),
9676                        ECORE_FILTER_MAC_PENDING,
9677                        &sc->sp_state,
9678                        ECORE_OBJ_TYPE_RX_TX,
9679                        &sc->macs_pool);
9680 
9681     BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9682           idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9683 }
9684 
9685 static inline void
9686 bxe_update_rx_prod(struct bxe_softc    *sc,
9687                    struct bxe_fastpath *fp,
9688                    uint16_t            rx_bd_prod,
9689                    uint16_t            rx_cq_prod,
9690                    uint16_t            rx_sge_prod)
9691 {
9692     struct ustorm_eth_rx_producers rx_prods = { 0 };
9693     uint32_t i;
9694 
9695     /* update producers */
9696     rx_prods.bd_prod  = rx_bd_prod;
9697     rx_prods.cqe_prod = rx_cq_prod;
9698     rx_prods.sge_prod = rx_sge_prod;
9699 
9700     /*
9701      * Make sure that the BD and SGE data is updated before updating the
9702      * producers since FW might read the BD/SGE right after the producer
9703      * is updated.
9704      * This is only applicable for weak-ordered memory model archs such
9705      * as IA-64. The following barrier is also mandatory since FW will
9706      * assumes BDs must have buffers.
9707      */
9708     wmb();
9709 
9710     for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9711         REG_WR(sc,
9712                (fp->ustorm_rx_prods_offset + (i * 4)),
9713                ((uint32_t *)&rx_prods)[i]);
9714     }
9715 
9716     wmb(); /* keep prod updates ordered */
9717 
9718     BLOGD(sc, DBG_RX,
9719           "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9720           fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9721 }
9722 
9723 static void
9724 bxe_init_rx_rings(struct bxe_softc *sc)
9725 {
9726     struct bxe_fastpath *fp;
9727     int i;
9728 
9729     for (i = 0; i < sc->num_queues; i++) {
9730         fp = &sc->fp[i];
9731 
9732         fp->rx_bd_cons = 0;
9733 
9734         /*
9735          * Activate the BD ring...
9736          * Warning, this will generate an interrupt (to the TSTORM)
9737          * so this can only be done after the chip is initialized
9738          */
9739         bxe_update_rx_prod(sc, fp,
9740                            fp->rx_bd_prod,
9741                            fp->rx_cq_prod,
9742                            fp->rx_sge_prod);
9743 
9744         if (i != 0) {
9745             continue;
9746         }
9747 
9748         if (CHIP_IS_E1(sc)) {
9749             REG_WR(sc,
9750                    (BAR_USTRORM_INTMEM +
9751                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9752                    U64_LO(fp->rcq_dma.paddr));
9753             REG_WR(sc,
9754                    (BAR_USTRORM_INTMEM +
9755                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9756                    U64_HI(fp->rcq_dma.paddr));
9757         }
9758     }
9759 }
9760 
9761 static void
9762 bxe_init_tx_ring_one(struct bxe_fastpath *fp)
9763 {
9764     SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9765     fp->tx_db.data.zero_fill1 = 0;
9766     fp->tx_db.data.prod = 0;
9767 
9768     fp->tx_pkt_prod = 0;
9769     fp->tx_pkt_cons = 0;
9770     fp->tx_bd_prod = 0;
9771     fp->tx_bd_cons = 0;
9772     fp->eth_q_stats.tx_pkts = 0;
9773 }
9774 
9775 static inline void
9776 bxe_init_tx_rings(struct bxe_softc *sc)
9777 {
9778     int i;
9779 
9780     for (i = 0; i < sc->num_queues; i++) {
9781         bxe_init_tx_ring_one(&sc->fp[i]);
9782     }
9783 }
9784 
9785 static void
9786 bxe_init_def_sb(struct bxe_softc *sc)
9787 {
9788     struct host_sp_status_block *def_sb = sc->def_sb;
9789     bus_addr_t mapping = sc->def_sb_dma.paddr;
9790     int igu_sp_sb_index;
9791     int igu_seg_id;
9792     int port = SC_PORT(sc);
9793     int func = SC_FUNC(sc);
9794     int reg_offset, reg_offset_en5;
9795     uint64_t section;
9796     int index, sindex;
9797     struct hc_sp_status_block_data sp_sb_data;
9798 
9799     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9800 
9801     if (CHIP_INT_MODE_IS_BC(sc)) {
9802         igu_sp_sb_index = DEF_SB_IGU_ID;
9803         igu_seg_id = HC_SEG_ACCESS_DEF;
9804     } else {
9805         igu_sp_sb_index = sc->igu_dsb_id;
9806         igu_seg_id = IGU_SEG_ACCESS_DEF;
9807     }
9808 
9809     /* attentions */
9810     section = ((uint64_t)mapping +
9811                offsetof(struct host_sp_status_block, atten_status_block));
9812     def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9813     sc->attn_state = 0;
9814 
9815     reg_offset = (port) ?
9816                      MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9817                      MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9818     reg_offset_en5 = (port) ?
9819                          MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9820                          MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9821 
9822     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9823         /* take care of sig[0]..sig[4] */
9824         for (sindex = 0; sindex < 4; sindex++) {
9825             sc->attn_group[index].sig[sindex] =
9826                 REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9827         }
9828 
9829         if (!CHIP_IS_E1x(sc)) {
9830             /*
9831              * enable5 is separate from the rest of the registers,
9832              * and the address skip is 4 and not 16 between the
9833              * different groups
9834              */
9835             sc->attn_group[index].sig[4] =
9836                 REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9837         } else {
9838             sc->attn_group[index].sig[4] = 0;
9839         }
9840     }
9841 
9842     if (sc->devinfo.int_block == INT_BLOCK_HC) {
9843         reg_offset = (port) ?
9844                          HC_REG_ATTN_MSG1_ADDR_L :
9845                          HC_REG_ATTN_MSG0_ADDR_L;
9846         REG_WR(sc, reg_offset, U64_LO(section));
9847         REG_WR(sc, (reg_offset + 4), U64_HI(section));
9848     } else if (!CHIP_IS_E1x(sc)) {
9849         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9850         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9851     }
9852 
9853     section = ((uint64_t)mapping +
9854                offsetof(struct host_sp_status_block, sp_sb));
9855 
9856     bxe_zero_sp_sb(sc);
9857 
9858     /* PCI guarantees endianity of regpair */
9859     sp_sb_data.state           = SB_ENABLED;
9860     sp_sb_data.host_sb_addr.lo = U64_LO(section);
9861     sp_sb_data.host_sb_addr.hi = U64_HI(section);
9862     sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9863     sp_sb_data.igu_seg_id      = igu_seg_id;
9864     sp_sb_data.p_func.pf_id    = func;
9865     sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9866     sp_sb_data.p_func.vf_id    = 0xff;
9867 
9868     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9869 
9870     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9871 }
9872 
9873 static void
9874 bxe_init_sp_ring(struct bxe_softc *sc)
9875 {
9876     atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9877     sc->spq_prod_idx = 0;
9878     sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9879     sc->spq_prod_bd = sc->spq;
9880     sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9881 }
9882 
9883 static void
9884 bxe_init_eq_ring(struct bxe_softc *sc)
9885 {
9886     union event_ring_elem *elem;
9887     int i;
9888 
9889     for (i = 1; i <= NUM_EQ_PAGES; i++) {
9890         elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9891 
9892         elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9893                                                  BCM_PAGE_SIZE *
9894                                                  (i % NUM_EQ_PAGES)));
9895         elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9896                                                  BCM_PAGE_SIZE *
9897                                                  (i % NUM_EQ_PAGES)));
9898     }
9899 
9900     sc->eq_cons    = 0;
9901     sc->eq_prod    = NUM_EQ_DESC;
9902     sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9903 
9904     atomic_store_rel_long(&sc->eq_spq_left,
9905                           (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9906                                NUM_EQ_DESC) - 1));
9907 }
9908 
9909 static void
9910 bxe_init_internal_common(struct bxe_softc *sc)
9911 {
9912     int i;
9913 
9914     if (IS_MF_SI(sc)) {
9915         /*
9916          * In switch independent mode, the TSTORM needs to accept
9917          * packets that failed classification, since approximate match
9918          * mac addresses aren't written to NIG LLH.
9919          */
9920         REG_WR8(sc,
9921                 (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
9922                 2);
9923     } else if (!CHIP_IS_E1(sc)) { /* 57710 doesn't support MF */
9924         REG_WR8(sc,
9925                 (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
9926                 0);
9927     }
9928 
9929     /*
9930      * Zero this manually as its initialization is currently missing
9931      * in the initTool.
9932      */
9933     for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9934         REG_WR(sc,
9935                (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9936                0);
9937     }
9938 
9939     if (!CHIP_IS_E1x(sc)) {
9940         REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9941                 CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9942     }
9943 }
9944 
9945 static void
9946 bxe_init_internal(struct bxe_softc *sc,
9947                   uint32_t         load_code)
9948 {
9949     switch (load_code) {
9950     case FW_MSG_CODE_DRV_LOAD_COMMON:
9951     case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9952         bxe_init_internal_common(sc);
9953         /* no break */
9954 
9955     case FW_MSG_CODE_DRV_LOAD_PORT:
9956         /* nothing to do */
9957         /* no break */
9958 
9959     case FW_MSG_CODE_DRV_LOAD_FUNCTION:
9960         /* internal memory per function is initialized inside bxe_pf_init */
9961         break;
9962 
9963     default:
9964         BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
9965         break;
9966     }
9967 }
9968 
9969 static void
9970 storm_memset_func_cfg(struct bxe_softc                         *sc,
9971                       struct tstorm_eth_function_common_config *tcfg,
9972                       uint16_t                                  abs_fid)
9973 {
9974     uint32_t addr;
9975     size_t size;
9976 
9977     addr = (BAR_TSTRORM_INTMEM +
9978             TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
9979     size = sizeof(struct tstorm_eth_function_common_config);
9980     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
9981 }
9982 
9983 static void
9984 bxe_func_init(struct bxe_softc            *sc,
9985               struct bxe_func_init_params *p)
9986 {
9987     struct tstorm_eth_function_common_config tcfg = { 0 };
9988 
9989     if (CHIP_IS_E1x(sc)) {
9990         storm_memset_func_cfg(sc, &tcfg, p->func_id);
9991     }
9992 
9993     /* Enable the function in the FW */
9994     storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
9995     storm_memset_func_en(sc, p->func_id, 1);
9996 
9997     /* spq */
9998     if (p->func_flgs & FUNC_FLG_SPQ) {
9999         storm_memset_spq_addr(sc, p->spq_map, p->func_id);
10000         REG_WR(sc,
10001                (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
10002                p->spq_prod);
10003     }
10004 }
10005 
10006 /*
10007  * Calculates the sum of vn_min_rates.
10008  * It's needed for further normalizing of the min_rates.
10009  * Returns:
10010  *   sum of vn_min_rates.
10011  *     or
10012  *   0 - if all the min_rates are 0.
10013  * In the later case fainess algorithm should be deactivated.
10014  * If all min rates are not zero then those that are zeroes will be set to 1.
10015  */
10016 static void
10017 bxe_calc_vn_min(struct bxe_softc       *sc,
10018                 struct cmng_init_input *input)
10019 {
10020     uint32_t vn_cfg;
10021     uint32_t vn_min_rate;
10022     int all_zero = 1;
10023     int vn;
10024 
10025     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10026         vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10027         vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
10028                         FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
10029 
10030         if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10031             /* skip hidden VNs */
10032             vn_min_rate = 0;
10033         } else if (!vn_min_rate) {
10034             /* If min rate is zero - set it to 100 */
10035             vn_min_rate = DEF_MIN_RATE;
10036         } else {
10037             all_zero = 0;
10038         }
10039 
10040         input->vnic_min_rate[vn] = vn_min_rate;
10041     }
10042 
10043     /* if ETS or all min rates are zeros - disable fairness */
10044     if (BXE_IS_ETS_ENABLED(sc)) {
10045         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10046         BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10047     } else if (all_zero) {
10048         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10049         BLOGD(sc, DBG_LOAD,
10050               "Fariness disabled (all MIN values are zeroes)\n");
10051     } else {
10052         input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10053     }
10054 }
10055 
10056 static inline uint16_t
10057 bxe_extract_max_cfg(struct bxe_softc *sc,
10058                     uint32_t         mf_cfg)
10059 {
10060     uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10061                         FUNC_MF_CFG_MAX_BW_SHIFT);
10062 
10063     if (!max_cfg) {
10064         BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10065         max_cfg = 100;
10066     }
10067 
10068     return (max_cfg);
10069 }
10070 
10071 static void
10072 bxe_calc_vn_max(struct bxe_softc       *sc,
10073                 int                    vn,
10074                 struct cmng_init_input *input)
10075 {
10076     uint16_t vn_max_rate;
10077     uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10078     uint32_t max_cfg;
10079 
10080     if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10081         vn_max_rate = 0;
10082     } else {
10083         max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10084 
10085         if (IS_MF_SI(sc)) {
10086             /* max_cfg in percents of linkspeed */
10087             vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10088         } else { /* SD modes */
10089             /* max_cfg is absolute in 100Mb units */
10090             vn_max_rate = (max_cfg * 100);
10091         }
10092     }
10093 
10094     BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10095 
10096     input->vnic_max_rate[vn] = vn_max_rate;
10097 }
10098 
10099 static void
10100 bxe_cmng_fns_init(struct bxe_softc *sc,
10101                   uint8_t          read_cfg,
10102                   uint8_t          cmng_type)
10103 {
10104     struct cmng_init_input input;
10105     int vn;
10106 
10107     memset(&input, 0, sizeof(struct cmng_init_input));
10108 
10109     input.port_rate = sc->link_vars.line_speed;
10110 
10111     if (cmng_type == CMNG_FNS_MINMAX) {
10112         /* read mf conf from shmem */
10113         if (read_cfg) {
10114             bxe_read_mf_cfg(sc);
10115         }
10116 
10117         /* get VN min rate and enable fairness if not 0 */
10118         bxe_calc_vn_min(sc, &input);
10119 
10120         /* get VN max rate */
10121         if (sc->port.pmf) {
10122             for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10123                 bxe_calc_vn_max(sc, vn, &input);
10124             }
10125         }
10126 
10127         /* always enable rate shaping and fairness */
10128         input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10129 
10130         ecore_init_cmng(&input, &sc->cmng);
10131         return;
10132     }
10133 
10134     /* rate shaping and fairness are disabled */
10135     BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10136 }
10137 
10138 static int
10139 bxe_get_cmng_fns_mode(struct bxe_softc *sc)
10140 {
10141     if (CHIP_REV_IS_SLOW(sc)) {
10142         return (CMNG_FNS_NONE);
10143     }
10144 
10145     if (IS_MF(sc)) {
10146         return (CMNG_FNS_MINMAX);
10147     }
10148 
10149     return (CMNG_FNS_NONE);
10150 }
10151 
10152 static void
10153 storm_memset_cmng(struct bxe_softc *sc,
10154                   struct cmng_init *cmng,
10155                   uint8_t          port)
10156 {
10157     int vn;
10158     int func;
10159     uint32_t addr;
10160     size_t size;
10161 
10162     addr = (BAR_XSTRORM_INTMEM +
10163             XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10164     size = sizeof(struct cmng_struct_per_port);
10165     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10166 
10167     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10168         func = func_by_vn(sc, vn);
10169 
10170         addr = (BAR_XSTRORM_INTMEM +
10171                 XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10172         size = sizeof(struct rate_shaping_vars_per_vn);
10173         ecore_storm_memset_struct(sc, addr, size,
10174                                   (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10175 
10176         addr = (BAR_XSTRORM_INTMEM +
10177                 XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10178         size = sizeof(struct fairness_vars_per_vn);
10179         ecore_storm_memset_struct(sc, addr, size,
10180                                   (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10181     }
10182 }
10183 
10184 static void
10185 bxe_pf_init(struct bxe_softc *sc)
10186 {
10187     struct bxe_func_init_params func_init = { 0 };
10188     struct event_ring_data eq_data = { { 0 } };
10189     uint16_t flags;
10190 
10191     if (!CHIP_IS_E1x(sc)) {
10192         /* reset IGU PF statistics: MSIX + ATTN */
10193         /* PF */
10194         REG_WR(sc,
10195                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10196                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10197                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10198                0);
10199         /* ATTN */
10200         REG_WR(sc,
10201                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10202                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10203                 (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10204                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10205                0);
10206     }
10207 
10208     /* function setup flags */
10209     flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10210 
10211     /*
10212      * This flag is relevant for E1x only.
10213      * E2 doesn't have a TPA configuration in a function level.
10214      */
10215     flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10216 
10217     func_init.func_flgs = flags;
10218     func_init.pf_id     = SC_FUNC(sc);
10219     func_init.func_id   = SC_FUNC(sc);
10220     func_init.spq_map   = sc->spq_dma.paddr;
10221     func_init.spq_prod  = sc->spq_prod_idx;
10222 
10223     bxe_func_init(sc, &func_init);
10224 
10225     memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10226 
10227     /*
10228      * Congestion management values depend on the link rate.
10229      * There is no active link so initial link rate is set to 10Gbps.
10230      * When the link comes up the congestion management values are
10231      * re-calculated according to the actual link rate.
10232      */
10233     sc->link_vars.line_speed = SPEED_10000;
10234     bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10235 
10236     /* Only the PMF sets the HW */
10237     if (sc->port.pmf) {
10238         storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10239     }
10240 
10241     /* init Event Queue - PCI bus guarantees correct endainity */
10242     eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10243     eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10244     eq_data.producer     = sc->eq_prod;
10245     eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10246     eq_data.sb_id        = DEF_SB_ID;
10247     storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10248 }
10249 
10250 static void
10251 bxe_hc_int_enable(struct bxe_softc *sc)
10252 {
10253     int port = SC_PORT(sc);
10254     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10255     uint32_t val = REG_RD(sc, addr);
10256     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10257     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10258                            (sc->intr_count == 1)) ? TRUE : FALSE;
10259     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10260 
10261     if (msix) {
10262         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10263                  HC_CONFIG_0_REG_INT_LINE_EN_0);
10264         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10265                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10266         if (single_msix) {
10267             val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10268         }
10269     } else if (msi) {
10270         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10271         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10272                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10273                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10274     } else {
10275         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10276                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10277                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10278                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10279 
10280         if (!CHIP_IS_E1(sc)) {
10281             BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10282                   val, port, addr);
10283 
10284             REG_WR(sc, addr, val);
10285 
10286             val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10287         }
10288     }
10289 
10290     if (CHIP_IS_E1(sc)) {
10291         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10292     }
10293 
10294     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10295           val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10296 
10297     REG_WR(sc, addr, val);
10298 
10299     /* ensure that HC_CONFIG is written before leading/trailing edge config */
10300     mb();
10301 
10302     if (!CHIP_IS_E1(sc)) {
10303         /* init leading/trailing edge */
10304         if (IS_MF(sc)) {
10305             val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10306             if (sc->port.pmf) {
10307                 /* enable nig and gpio3 attention */
10308                 val |= 0x1100;
10309             }
10310         } else {
10311             val = 0xffff;
10312         }
10313 
10314         REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10315         REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10316     }
10317 
10318     /* make sure that interrupts are indeed enabled from here on */
10319     mb();
10320 }
10321 
10322 static void
10323 bxe_igu_int_enable(struct bxe_softc *sc)
10324 {
10325     uint32_t val;
10326     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10327     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10328                            (sc->intr_count == 1)) ? TRUE : FALSE;
10329     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10330 
10331     val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10332 
10333     if (msix) {
10334         val &= ~(IGU_PF_CONF_INT_LINE_EN |
10335                  IGU_PF_CONF_SINGLE_ISR_EN);
10336         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10337                 IGU_PF_CONF_ATTN_BIT_EN);
10338         if (single_msix) {
10339             val |= IGU_PF_CONF_SINGLE_ISR_EN;
10340         }
10341     } else if (msi) {
10342         val &= ~IGU_PF_CONF_INT_LINE_EN;
10343         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10344                 IGU_PF_CONF_ATTN_BIT_EN |
10345                 IGU_PF_CONF_SINGLE_ISR_EN);
10346     } else {
10347         val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10348         val |= (IGU_PF_CONF_INT_LINE_EN |
10349                 IGU_PF_CONF_ATTN_BIT_EN |
10350                 IGU_PF_CONF_SINGLE_ISR_EN);
10351     }
10352 
10353     /* clean previous status - need to configure igu prior to ack*/
10354     if ((!msix) || single_msix) {
10355         REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10356         bxe_ack_int(sc);
10357     }
10358 
10359     val |= IGU_PF_CONF_FUNC_EN;
10360 
10361     BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10362           val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10363 
10364     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10365 
10366     mb();
10367 
10368     /* init leading/trailing edge */
10369     if (IS_MF(sc)) {
10370         val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10371         if (sc->port.pmf) {
10372             /* enable nig and gpio3 attention */
10373             val |= 0x1100;
10374         }
10375     } else {
10376         val = 0xffff;
10377     }
10378 
10379     REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10380     REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10381 
10382     /* make sure that interrupts are indeed enabled from here on */
10383     mb();
10384 }
10385 
10386 static void
10387 bxe_int_enable(struct bxe_softc *sc)
10388 {
10389     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10390         bxe_hc_int_enable(sc);
10391     } else {
10392         bxe_igu_int_enable(sc);
10393     }
10394 }
10395 
10396 static void
10397 bxe_hc_int_disable(struct bxe_softc *sc)
10398 {
10399     int port = SC_PORT(sc);
10400     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10401     uint32_t val = REG_RD(sc, addr);
10402 
10403     /*
10404      * In E1 we must use only PCI configuration space to disable MSI/MSIX
10405      * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10406      * block
10407      */
10408     if (CHIP_IS_E1(sc)) {
10409         /*
10410          * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10411          * to prevent from HC sending interrupts after we exit the function
10412          */
10413         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10414 
10415         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10416                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10417                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10418     } else {
10419         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10420                  HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10421                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10422                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10423     }
10424 
10425     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10426 
10427     /* flush all outstanding writes */
10428     mb();
10429 
10430     REG_WR(sc, addr, val);
10431     if (REG_RD(sc, addr) != val) {
10432         BLOGE(sc, "proper val not read from HC IGU!\n");
10433     }
10434 }
10435 
10436 static void
10437 bxe_igu_int_disable(struct bxe_softc *sc)
10438 {
10439     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10440 
10441     val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10442              IGU_PF_CONF_INT_LINE_EN |
10443              IGU_PF_CONF_ATTN_BIT_EN);
10444 
10445     BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10446 
10447     /* flush all outstanding writes */
10448     mb();
10449 
10450     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10451     if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10452         BLOGE(sc, "proper val not read from IGU!\n");
10453     }
10454 }
10455 
10456 static void
10457 bxe_int_disable(struct bxe_softc *sc)
10458 {
10459     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10460         bxe_hc_int_disable(sc);
10461     } else {
10462         bxe_igu_int_disable(sc);
10463     }
10464 }
10465 
10466 static void
10467 bxe_nic_init(struct bxe_softc *sc,
10468              int              load_code)
10469 {
10470     int i;
10471 
10472     for (i = 0; i < sc->num_queues; i++) {
10473         bxe_init_eth_fp(sc, i);
10474     }
10475 
10476     rmb(); /* ensure status block indices were read */
10477 
10478     bxe_init_rx_rings(sc);
10479     bxe_init_tx_rings(sc);
10480 
10481     if (IS_VF(sc)) {
10482         return;
10483     }
10484 
10485     /* initialize MOD_ABS interrupts */
10486     elink_init_mod_abs_int(sc, &sc->link_vars,
10487                            sc->devinfo.chip_id,
10488                            sc->devinfo.shmem_base,
10489                            sc->devinfo.shmem2_base,
10490                            SC_PORT(sc));
10491 
10492     bxe_init_def_sb(sc);
10493     bxe_update_dsb_idx(sc);
10494     bxe_init_sp_ring(sc);
10495     bxe_init_eq_ring(sc);
10496     bxe_init_internal(sc, load_code);
10497     bxe_pf_init(sc);
10498     bxe_stats_init(sc);
10499 
10500     /* flush all before enabling interrupts */
10501     mb();
10502 
10503     bxe_int_enable(sc);
10504 
10505     /* check for SPIO5 */
10506     bxe_attn_int_deasserted0(sc,
10507                              REG_RD(sc,
10508                                     (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10509                                      SC_PORT(sc)*4)) &
10510                              AEU_INPUTS_ATTN_BITS_SPIO5);
10511 }
10512 
10513 static inline void
10514 bxe_init_objs(struct bxe_softc *sc)
10515 {
10516     /* mcast rules must be added to tx if tx switching is enabled */
10517     ecore_obj_type o_type =
10518         (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10519                                          ECORE_OBJ_TYPE_RX;
10520 
10521     /* RX_MODE controlling object */
10522     ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10523 
10524     /* multicast configuration controlling object */
10525     ecore_init_mcast_obj(sc,
10526                          &sc->mcast_obj,
10527                          sc->fp[0].cl_id,
10528                          sc->fp[0].index,
10529                          SC_FUNC(sc),
10530                          SC_FUNC(sc),
10531                          BXE_SP(sc, mcast_rdata),
10532                          BXE_SP_MAPPING(sc, mcast_rdata),
10533                          ECORE_FILTER_MCAST_PENDING,
10534                          &sc->sp_state,
10535                          o_type);
10536 
10537     /* Setup CAM credit pools */
10538     ecore_init_mac_credit_pool(sc,
10539                                &sc->macs_pool,
10540                                SC_FUNC(sc),
10541                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10542                                                  VNICS_PER_PATH(sc));
10543 
10544     ecore_init_vlan_credit_pool(sc,
10545                                 &sc->vlans_pool,
10546                                 SC_ABS_FUNC(sc) >> 1,
10547                                 CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10548                                                   VNICS_PER_PATH(sc));
10549 
10550     /* RSS configuration object */
10551     ecore_init_rss_config_obj(sc,
10552                               &sc->rss_conf_obj,
10553                               sc->fp[0].cl_id,
10554                               sc->fp[0].index,
10555                               SC_FUNC(sc),
10556                               SC_FUNC(sc),
10557                               BXE_SP(sc, rss_rdata),
10558                               BXE_SP_MAPPING(sc, rss_rdata),
10559                               ECORE_FILTER_RSS_CONF_PENDING,
10560                               &sc->sp_state, ECORE_OBJ_TYPE_RX);
10561 }
10562 
10563 /*
10564  * Initialize the function. This must be called before sending CLIENT_SETUP
10565  * for the first client.
10566  */
10567 static inline int
10568 bxe_func_start(struct bxe_softc *sc)
10569 {
10570     struct ecore_func_state_params func_params = { NULL };
10571     struct ecore_func_start_params *start_params = &func_params.params.start;
10572 
10573     /* Prepare parameters for function state transitions */
10574     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10575 
10576     func_params.f_obj = &sc->func_obj;
10577     func_params.cmd = ECORE_F_CMD_START;
10578 
10579     /* Function parameters */
10580     start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10581     start_params->sd_vlan_tag = OVLAN(sc);
10582 
10583     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10584         start_params->network_cos_mode = STATIC_COS;
10585     } else { /* CHIP_IS_E1X */
10586         start_params->network_cos_mode = FW_WRR;
10587     }
10588 
10589     //start_params->gre_tunnel_mode = 0;
10590     //start_params->gre_tunnel_rss  = 0;
10591 
10592     return (ecore_func_state_change(sc, &func_params));
10593 }
10594 
10595 static int
10596 bxe_set_power_state(struct bxe_softc *sc,
10597                     uint8_t          state)
10598 {
10599     uint16_t pmcsr;
10600 
10601     /* If there is no power capability, silently succeed */
10602     if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10603         BLOGW(sc, "No power capability\n");
10604         return (0);
10605     }
10606 
10607     pmcsr = pci_read_config(sc->dev,
10608                             (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10609                             2);
10610 
10611     switch (state) {
10612     case PCI_PM_D0:
10613         pci_write_config(sc->dev,
10614                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10615                          ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10616 
10617         if (pmcsr & PCIM_PSTAT_DMASK) {
10618             /* delay required during transition out of D3hot */
10619             DELAY(20000);
10620         }
10621 
10622         break;
10623 
10624     case PCI_PM_D3hot:
10625         /* XXX if there are other clients above don't shut down the power */
10626 
10627         /* don't shut down the power for emulation and FPGA */
10628         if (CHIP_REV_IS_SLOW(sc)) {
10629             return (0);
10630         }
10631 
10632         pmcsr &= ~PCIM_PSTAT_DMASK;
10633         pmcsr |= PCIM_PSTAT_D3;
10634 
10635         if (sc->wol) {
10636             pmcsr |= PCIM_PSTAT_PMEENABLE;
10637         }
10638 
10639         pci_write_config(sc->dev,
10640                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10641                          pmcsr, 4);
10642 
10643         /*
10644          * No more memory access after this point until device is brought back
10645          * to D0 state.
10646          */
10647         break;
10648 
10649     default:
10650         BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10651             state, pmcsr);
10652         return (-1);
10653     }
10654 
10655     return (0);
10656 }
10657 
10658 
10659 /* return true if succeeded to acquire the lock */
10660 static uint8_t
10661 bxe_trylock_hw_lock(struct bxe_softc *sc,
10662                     uint32_t         resource)
10663 {
10664     uint32_t lock_status;
10665     uint32_t resource_bit = (1 << resource);
10666     int func = SC_FUNC(sc);
10667     uint32_t hw_lock_control_reg;
10668 
10669     BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10670 
10671     /* Validating that the resource is within range */
10672     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10673         BLOGD(sc, DBG_LOAD,
10674               "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10675               resource, HW_LOCK_MAX_RESOURCE_VALUE);
10676         return (FALSE);
10677     }
10678 
10679     if (func <= 5) {
10680         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10681     } else {
10682         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10683     }
10684 
10685     /* try to acquire the lock */
10686     REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10687     lock_status = REG_RD(sc, hw_lock_control_reg);
10688     if (lock_status & resource_bit) {
10689         return (TRUE);
10690     }
10691 
10692     BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10693         "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10694         lock_status, resource_bit);
10695 
10696     return (FALSE);
10697 }
10698 
10699 /*
10700  * Get the recovery leader resource id according to the engine this function
10701  * belongs to. Currently only only 2 engines is supported.
10702  */
10703 static int
10704 bxe_get_leader_lock_resource(struct bxe_softc *sc)
10705 {
10706     if (SC_PATH(sc)) {
10707         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10708     } else {
10709         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10710     }
10711 }
10712 
10713 /* try to acquire a leader lock for current engine */
10714 static uint8_t
10715 bxe_trylock_leader_lock(struct bxe_softc *sc)
10716 {
10717     return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10718 }
10719 
10720 static int
10721 bxe_release_leader_lock(struct bxe_softc *sc)
10722 {
10723     return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10724 }
10725 
10726 /* close gates #2, #3 and #4 */
10727 static void
10728 bxe_set_234_gates(struct bxe_softc *sc,
10729                   uint8_t          close)
10730 {
10731     uint32_t val;
10732 
10733     /* gates #2 and #4a are closed/opened for "not E1" only */
10734     if (!CHIP_IS_E1(sc)) {
10735         /* #4 */
10736         REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10737         /* #2 */
10738         REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10739     }
10740 
10741     /* #3 */
10742     if (CHIP_IS_E1x(sc)) {
10743         /* prevent interrupts from HC on both ports */
10744         val = REG_RD(sc, HC_REG_CONFIG_1);
10745         REG_WR(sc, HC_REG_CONFIG_1,
10746                (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10747                (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10748 
10749         val = REG_RD(sc, HC_REG_CONFIG_0);
10750         REG_WR(sc, HC_REG_CONFIG_0,
10751                (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10752                (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10753     } else {
10754         /* Prevent incomming interrupts in IGU */
10755         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10756 
10757         REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10758                (!close) ?
10759                (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10760                (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10761     }
10762 
10763     BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10764           close ? "closing" : "opening");
10765 
10766     wmb();
10767 }
10768 
10769 /* poll for pending writes bit, it should get cleared in no more than 1s */
10770 static int
10771 bxe_er_poll_igu_vq(struct bxe_softc *sc)
10772 {
10773     uint32_t cnt = 1000;
10774     uint32_t pend_bits = 0;
10775 
10776     do {
10777         pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10778 
10779         if (pend_bits == 0) {
10780             break;
10781         }
10782 
10783         DELAY(1000);
10784     } while (--cnt > 0);
10785 
10786     if (cnt == 0) {
10787         BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10788         return (-1);
10789     }
10790 
10791     return (0);
10792 }
10793 
10794 #define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10795 
10796 static void
10797 bxe_clp_reset_prep(struct bxe_softc *sc,
10798                    uint32_t         *magic_val)
10799 {
10800     /* Do some magic... */
10801     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10802     *magic_val = val & SHARED_MF_CLP_MAGIC;
10803     MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10804 }
10805 
10806 /* restore the value of the 'magic' bit */
10807 static void
10808 bxe_clp_reset_done(struct bxe_softc *sc,
10809                    uint32_t         magic_val)
10810 {
10811     /* Restore the 'magic' bit value... */
10812     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10813     MFCFG_WR(sc, shared_mf_config.clp_mb,
10814               (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10815 }
10816 
10817 /* prepare for MCP reset, takes care of CLP configurations */
10818 static void
10819 bxe_reset_mcp_prep(struct bxe_softc *sc,
10820                    uint32_t         *magic_val)
10821 {
10822     uint32_t shmem;
10823     uint32_t validity_offset;
10824 
10825     /* set `magic' bit in order to save MF config */
10826     if (!CHIP_IS_E1(sc)) {
10827         bxe_clp_reset_prep(sc, magic_val);
10828     }
10829 
10830     /* get shmem offset */
10831     shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10832     validity_offset =
10833         offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10834 
10835     /* Clear validity map flags */
10836     if (shmem > 0) {
10837         REG_WR(sc, shmem + validity_offset, 0);
10838     }
10839 }
10840 
10841 #define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10842 #define MCP_ONE_TIMEOUT  100    /* 100 ms */
10843 
10844 static void
10845 bxe_mcp_wait_one(struct bxe_softc *sc)
10846 {
10847     /* special handling for emulation and FPGA (10 times longer) */
10848     if (CHIP_REV_IS_SLOW(sc)) {
10849         DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10850     } else {
10851         DELAY((MCP_ONE_TIMEOUT) * 1000);
10852     }
10853 }
10854 
10855 /* initialize shmem_base and waits for validity signature to appear */
10856 static int
10857 bxe_init_shmem(struct bxe_softc *sc)
10858 {
10859     int cnt = 0;
10860     uint32_t val = 0;
10861 
10862     do {
10863         sc->devinfo.shmem_base     =
10864         sc->link_params.shmem_base =
10865             REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10866 
10867         if (sc->devinfo.shmem_base) {
10868             val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10869             if (val & SHR_MEM_VALIDITY_MB)
10870                 return (0);
10871         }
10872 
10873         bxe_mcp_wait_one(sc);
10874 
10875     } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10876 
10877     BLOGE(sc, "BAD MCP validity signature\n");
10878 
10879     return (-1);
10880 }
10881 
10882 static int
10883 bxe_reset_mcp_comp(struct bxe_softc *sc,
10884                    uint32_t         magic_val)
10885 {
10886     int rc = bxe_init_shmem(sc);
10887 
10888     /* Restore the `magic' bit value */
10889     if (!CHIP_IS_E1(sc)) {
10890         bxe_clp_reset_done(sc, magic_val);
10891     }
10892 
10893     return (rc);
10894 }
10895 
10896 static void
10897 bxe_pxp_prep(struct bxe_softc *sc)
10898 {
10899     if (!CHIP_IS_E1(sc)) {
10900         REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10901         REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10902         wmb();
10903     }
10904 }
10905 
10906 /*
10907  * Reset the whole chip except for:
10908  *      - PCIE core
10909  *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10910  *      - IGU
10911  *      - MISC (including AEU)
10912  *      - GRC
10913  *      - RBCN, RBCP
10914  */
10915 static void
10916 bxe_process_kill_chip_reset(struct bxe_softc *sc,
10917                             uint8_t          global)
10918 {
10919     uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10920     uint32_t global_bits2, stay_reset2;
10921 
10922     /*
10923      * Bits that have to be set in reset_mask2 if we want to reset 'global'
10924      * (per chip) blocks.
10925      */
10926     global_bits2 =
10927         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10928         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10929 
10930     /*
10931      * Don't reset the following blocks.
10932      * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10933      *            reset, as in 4 port device they might still be owned
10934      *            by the MCP (there is only one leader per path).
10935      */
10936     not_reset_mask1 =
10937         MISC_REGISTERS_RESET_REG_1_RST_HC |
10938         MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10939         MISC_REGISTERS_RESET_REG_1_RST_PXP;
10940 
10941     not_reset_mask2 =
10942         MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10943         MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10944         MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10945         MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10946         MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10947         MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10948         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10949         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10950         MISC_REGISTERS_RESET_REG_2_RST_ATC |
10951         MISC_REGISTERS_RESET_REG_2_PGLC |
10952         MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10953         MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10954         MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10955         MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10956         MISC_REGISTERS_RESET_REG_2_UMAC0 |
10957         MISC_REGISTERS_RESET_REG_2_UMAC1;
10958 
10959     /*
10960      * Keep the following blocks in reset:
10961      *  - all xxMACs are handled by the elink code.
10962      */
10963     stay_reset2 =
10964         MISC_REGISTERS_RESET_REG_2_XMAC |
10965         MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
10966 
10967     /* Full reset masks according to the chip */
10968     reset_mask1 = 0xffffffff;
10969 
10970     if (CHIP_IS_E1(sc))
10971         reset_mask2 = 0xffff;
10972     else if (CHIP_IS_E1H(sc))
10973         reset_mask2 = 0x1ffff;
10974     else if (CHIP_IS_E2(sc))
10975         reset_mask2 = 0xfffff;
10976     else /* CHIP_IS_E3 */
10977         reset_mask2 = 0x3ffffff;
10978 
10979     /* Don't reset global blocks unless we need to */
10980     if (!global)
10981         reset_mask2 &= ~global_bits2;
10982 
10983     /*
10984      * In case of attention in the QM, we need to reset PXP
10985      * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
10986      * because otherwise QM reset would release 'close the gates' shortly
10987      * before resetting the PXP, then the PSWRQ would send a write
10988      * request to PGLUE. Then when PXP is reset, PGLUE would try to
10989      * read the payload data from PSWWR, but PSWWR would not
10990      * respond. The write queue in PGLUE would stuck, dmae commands
10991      * would not return. Therefore it's important to reset the second
10992      * reset register (containing the
10993      * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
10994      * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
10995      * bit).
10996      */
10997     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
10998            reset_mask2 & (~not_reset_mask2));
10999 
11000     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
11001            reset_mask1 & (~not_reset_mask1));
11002 
11003     mb();
11004     wmb();
11005 
11006     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
11007            reset_mask2 & (~stay_reset2));
11008 
11009     mb();
11010     wmb();
11011 
11012     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
11013     wmb();
11014 }
11015 
11016 static int
11017 bxe_process_kill(struct bxe_softc *sc,
11018                  uint8_t          global)
11019 {
11020     int cnt = 1000;
11021     uint32_t val = 0;
11022     uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
11023     uint32_t tags_63_32 = 0;
11024 
11025     /* Empty the Tetris buffer, wait for 1s */
11026     do {
11027         sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
11028         blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
11029         port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
11030         port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
11031         pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
11032         if (CHIP_IS_E3(sc)) {
11033             tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
11034         }
11035 
11036         if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
11037             ((port_is_idle_0 & 0x1) == 0x1) &&
11038             ((port_is_idle_1 & 0x1) == 0x1) &&
11039             (pgl_exp_rom2 == 0xffffffff) &&
11040             (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11041             break;
11042         DELAY(1000);
11043     } while (cnt-- > 0);
11044 
11045     if (cnt <= 0) {
11046         BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11047                   "are still outstanding read requests after 1s! "
11048                   "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11049                   "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11050               sr_cnt, blk_cnt, port_is_idle_0,
11051               port_is_idle_1, pgl_exp_rom2);
11052         return (-1);
11053     }
11054 
11055     mb();
11056 
11057     /* Close gates #2, #3 and #4 */
11058     bxe_set_234_gates(sc, TRUE);
11059 
11060     /* Poll for IGU VQs for 57712 and newer chips */
11061     if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11062         return (-1);
11063     }
11064 
11065     /* XXX indicate that "process kill" is in progress to MCP */
11066 
11067     /* clear "unprepared" bit */
11068     REG_WR(sc, MISC_REG_UNPREPARED, 0);
11069     mb();
11070 
11071     /* Make sure all is written to the chip before the reset */
11072     wmb();
11073 
11074     /*
11075      * Wait for 1ms to empty GLUE and PCI-E core queues,
11076      * PSWHST, GRC and PSWRD Tetris buffer.
11077      */
11078     DELAY(1000);
11079 
11080     /* Prepare to chip reset: */
11081     /* MCP */
11082     if (global) {
11083         bxe_reset_mcp_prep(sc, &val);
11084     }
11085 
11086     /* PXP */
11087     bxe_pxp_prep(sc);
11088     mb();
11089 
11090     /* reset the chip */
11091     bxe_process_kill_chip_reset(sc, global);
11092     mb();
11093 
11094     /* clear errors in PGB */
11095     if (!CHIP_IS_E1(sc))
11096         REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11097 
11098     /* Recover after reset: */
11099     /* MCP */
11100     if (global && bxe_reset_mcp_comp(sc, val)) {
11101         return (-1);
11102     }
11103 
11104     /* XXX add resetting the NO_MCP mode DB here */
11105 
11106     /* Open the gates #2, #3 and #4 */
11107     bxe_set_234_gates(sc, FALSE);
11108 
11109     /* XXX
11110      * IGU/AEU preparation bring back the AEU/IGU to a reset state
11111      * re-enable attentions
11112      */
11113 
11114     return (0);
11115 }
11116 
11117 static int
11118 bxe_leader_reset(struct bxe_softc *sc)
11119 {
11120     int rc = 0;
11121     uint8_t global = bxe_reset_is_global(sc);
11122     uint32_t load_code;
11123 
11124     /*
11125      * If not going to reset MCP, load "fake" driver to reset HW while
11126      * driver is owner of the HW.
11127      */
11128     if (!global && !BXE_NOMCP(sc)) {
11129         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11130                                    DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11131         if (!load_code) {
11132             BLOGE(sc, "MCP response failure, aborting\n");
11133             rc = -1;
11134             goto exit_leader_reset;
11135         }
11136 
11137         if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11138             (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11139             BLOGE(sc, "MCP unexpected response, aborting\n");
11140             rc = -1;
11141             goto exit_leader_reset2;
11142         }
11143 
11144         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11145         if (!load_code) {
11146             BLOGE(sc, "MCP response failure, aborting\n");
11147             rc = -1;
11148             goto exit_leader_reset2;
11149         }
11150     }
11151 
11152     /* try to recover after the failure */
11153     if (bxe_process_kill(sc, global)) {
11154         BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11155         rc = -1;
11156         goto exit_leader_reset2;
11157     }
11158 
11159     /*
11160      * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11161      * state.
11162      */
11163     bxe_set_reset_done(sc);
11164     if (global) {
11165         bxe_clear_reset_global(sc);
11166     }
11167 
11168 exit_leader_reset2:
11169 
11170     /* unload "fake driver" if it was loaded */
11171     if (!global && !BXE_NOMCP(sc)) {
11172         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11173         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11174     }
11175 
11176 exit_leader_reset:
11177 
11178     sc->is_leader = 0;
11179     bxe_release_leader_lock(sc);
11180 
11181     mb();
11182     return (rc);
11183 }
11184 
11185 /*
11186  * prepare INIT transition, parameters configured:
11187  *   - HC configuration
11188  *   - Queue's CDU context
11189  */
11190 static void
11191 bxe_pf_q_prep_init(struct bxe_softc               *sc,
11192                    struct bxe_fastpath            *fp,
11193                    struct ecore_queue_init_params *init_params)
11194 {
11195     uint8_t cos;
11196     int cxt_index, cxt_offset;
11197 
11198     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11199     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11200 
11201     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11202     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11203 
11204     /* HC rate */
11205     init_params->rx.hc_rate =
11206         sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11207     init_params->tx.hc_rate =
11208         sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11209 
11210     /* FW SB ID */
11211     init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11212 
11213     /* CQ index among the SB indices */
11214     init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11215     init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11216 
11217     /* set maximum number of COSs supported by this queue */
11218     init_params->max_cos = sc->max_cos;
11219 
11220     BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11221           fp->index, init_params->max_cos);
11222 
11223     /* set the context pointers queue object */
11224     for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11225         /* XXX change index/cid here if ever support multiple tx CoS */
11226         /* fp->txdata[cos]->cid */
11227         cxt_index = fp->index / ILT_PAGE_CIDS;
11228         cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11229         init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11230     }
11231 }
11232 
11233 /* set flags that are common for the Tx-only and not normal connections */
11234 static unsigned long
11235 bxe_get_common_flags(struct bxe_softc    *sc,
11236                      struct bxe_fastpath *fp,
11237                      uint8_t             zero_stats)
11238 {
11239     unsigned long flags = 0;
11240 
11241     /* PF driver will always initialize the Queue to an ACTIVE state */
11242     bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11243 
11244     /*
11245      * tx only connections collect statistics (on the same index as the
11246      * parent connection). The statistics are zeroed when the parent
11247      * connection is initialized.
11248      */
11249 
11250     bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11251     if (zero_stats) {
11252         bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11253     }
11254 
11255     /*
11256      * tx only connections can support tx-switching, though their
11257      * CoS-ness doesn't survive the loopback
11258      */
11259     if (sc->flags & BXE_TX_SWITCHING) {
11260         bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11261     }
11262 
11263     bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11264 
11265     return (flags);
11266 }
11267 
11268 static unsigned long
11269 bxe_get_q_flags(struct bxe_softc    *sc,
11270                 struct bxe_fastpath *fp,
11271                 uint8_t             leading)
11272 {
11273     unsigned long flags = 0;
11274 
11275     if (IS_MF_SD(sc)) {
11276         bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11277     }
11278 
11279     if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11280         bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11281         bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11282     }
11283 
11284     if (leading) {
11285         bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11286         bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11287     }
11288 
11289     bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11290 
11291     /* merge with common flags */
11292     return (flags | bxe_get_common_flags(sc, fp, TRUE));
11293 }
11294 
11295 static void
11296 bxe_pf_q_prep_general(struct bxe_softc                  *sc,
11297                       struct bxe_fastpath               *fp,
11298                       struct ecore_general_setup_params *gen_init,
11299                       uint8_t                           cos)
11300 {
11301     gen_init->stat_id = bxe_stats_id(fp);
11302     gen_init->spcl_id = fp->cl_id;
11303     gen_init->mtu = sc->mtu;
11304     gen_init->cos = cos;
11305 }
11306 
11307 static void
11308 bxe_pf_rx_q_prep(struct bxe_softc              *sc,
11309                  struct bxe_fastpath           *fp,
11310                  struct rxq_pause_params       *pause,
11311                  struct ecore_rxq_setup_params *rxq_init)
11312 {
11313     uint8_t max_sge = 0;
11314     uint16_t sge_sz = 0;
11315     uint16_t tpa_agg_size = 0;
11316 
11317     pause->sge_th_lo = SGE_TH_LO(sc);
11318     pause->sge_th_hi = SGE_TH_HI(sc);
11319 
11320     /* validate SGE ring has enough to cross high threshold */
11321     if (sc->dropless_fc &&
11322             (pause->sge_th_hi + FW_PREFETCH_CNT) >
11323             (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11324         BLOGW(sc, "sge ring threshold limit\n");
11325     }
11326 
11327     /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11328     tpa_agg_size = (2 * sc->mtu);
11329     if (tpa_agg_size < sc->max_aggregation_size) {
11330         tpa_agg_size = sc->max_aggregation_size;
11331     }
11332 
11333     max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11334     max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11335                    (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11336     sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11337 
11338     /* pause - not for e1 */
11339     if (!CHIP_IS_E1(sc)) {
11340         pause->bd_th_lo = BD_TH_LO(sc);
11341         pause->bd_th_hi = BD_TH_HI(sc);
11342 
11343         pause->rcq_th_lo = RCQ_TH_LO(sc);
11344         pause->rcq_th_hi = RCQ_TH_HI(sc);
11345 
11346         /* validate rings have enough entries to cross high thresholds */
11347         if (sc->dropless_fc &&
11348             pause->bd_th_hi + FW_PREFETCH_CNT >
11349             sc->rx_ring_size) {
11350             BLOGW(sc, "rx bd ring threshold limit\n");
11351         }
11352 
11353         if (sc->dropless_fc &&
11354             pause->rcq_th_hi + FW_PREFETCH_CNT >
11355             RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11356             BLOGW(sc, "rcq ring threshold limit\n");
11357         }
11358 
11359         pause->pri_map = 1;
11360     }
11361 
11362     /* rxq setup */
11363     rxq_init->dscr_map   = fp->rx_dma.paddr;
11364     rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11365     rxq_init->rcq_map    = fp->rcq_dma.paddr;
11366     rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11367 
11368     /*
11369      * This should be a maximum number of data bytes that may be
11370      * placed on the BD (not including paddings).
11371      */
11372     rxq_init->buf_sz = (fp->rx_buf_size -
11373                         IP_HEADER_ALIGNMENT_PADDING);
11374 
11375     rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11376     rxq_init->tpa_agg_sz      = tpa_agg_size;
11377     rxq_init->sge_buf_sz      = sge_sz;
11378     rxq_init->max_sges_pkt    = max_sge;
11379     rxq_init->rss_engine_id   = SC_FUNC(sc);
11380     rxq_init->mcast_engine_id = SC_FUNC(sc);
11381 
11382     /*
11383      * Maximum number or simultaneous TPA aggregation for this Queue.
11384      * For PF Clients it should be the maximum available number.
11385      * VF driver(s) may want to define it to a smaller value.
11386      */
11387     rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11388 
11389     rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11390     rxq_init->fw_sb_id = fp->fw_sb_id;
11391 
11392     rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11393 
11394     /*
11395      * configure silent vlan removal
11396      * if multi function mode is afex, then mask default vlan
11397      */
11398     if (IS_MF_AFEX(sc)) {
11399         rxq_init->silent_removal_value =
11400             sc->devinfo.mf_info.afex_def_vlan_tag;
11401         rxq_init->silent_removal_mask = EVL_VLID_MASK;
11402     }
11403 }
11404 
11405 static void
11406 bxe_pf_tx_q_prep(struct bxe_softc              *sc,
11407                  struct bxe_fastpath           *fp,
11408                  struct ecore_txq_setup_params *txq_init,
11409                  uint8_t                       cos)
11410 {
11411     /*
11412      * XXX If multiple CoS is ever supported then each fastpath structure
11413      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11414      * fp->txdata[cos]->tx_dma.paddr;
11415      */
11416     txq_init->dscr_map     = fp->tx_dma.paddr;
11417     txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11418     txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11419     txq_init->fw_sb_id     = fp->fw_sb_id;
11420 
11421     /*
11422      * set the TSS leading client id for TX classfication to the
11423      * leading RSS client id
11424      */
11425     txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11426 }
11427 
11428 /*
11429  * This function performs 2 steps in a queue state machine:
11430  *   1) RESET->INIT
11431  *   2) INIT->SETUP
11432  */
11433 static int
11434 bxe_setup_queue(struct bxe_softc    *sc,
11435                 struct bxe_fastpath *fp,
11436                 uint8_t             leading)
11437 {
11438     struct ecore_queue_state_params q_params = { NULL };
11439     struct ecore_queue_setup_params *setup_params =
11440                         &q_params.params.setup;
11441     int rc;
11442 
11443     BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11444 
11445     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11446 
11447     q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11448 
11449     /* we want to wait for completion in this context */
11450     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11451 
11452     /* prepare the INIT parameters */
11453     bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11454 
11455     /* Set the command */
11456     q_params.cmd = ECORE_Q_CMD_INIT;
11457 
11458     /* Change the state to INIT */
11459     rc = ecore_queue_state_change(sc, &q_params);
11460     if (rc) {
11461         BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11462         return (rc);
11463     }
11464 
11465     BLOGD(sc, DBG_LOAD, "init complete\n");
11466 
11467     /* now move the Queue to the SETUP state */
11468     memset(setup_params, 0, sizeof(*setup_params));
11469 
11470     /* set Queue flags */
11471     setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11472 
11473     /* set general SETUP parameters */
11474     bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11475                           FIRST_TX_COS_INDEX);
11476 
11477     bxe_pf_rx_q_prep(sc, fp,
11478                      &setup_params->pause_params,
11479                      &setup_params->rxq_params);
11480 
11481     bxe_pf_tx_q_prep(sc, fp,
11482                      &setup_params->txq_params,
11483                      FIRST_TX_COS_INDEX);
11484 
11485     /* Set the command */
11486     q_params.cmd = ECORE_Q_CMD_SETUP;
11487 
11488     /* change the state to SETUP */
11489     rc = ecore_queue_state_change(sc, &q_params);
11490     if (rc) {
11491         BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11492         return (rc);
11493     }
11494 
11495     return (rc);
11496 }
11497 
11498 static int
11499 bxe_setup_leading(struct bxe_softc *sc)
11500 {
11501     return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11502 }
11503 
11504 static int
11505 bxe_config_rss_pf(struct bxe_softc            *sc,
11506                   struct ecore_rss_config_obj *rss_obj,
11507                   uint8_t                     config_hash)
11508 {
11509     struct ecore_config_rss_params params = { NULL };
11510     int i;
11511 
11512     /*
11513      * Although RSS is meaningless when there is a single HW queue we
11514      * still need it enabled in order to have HW Rx hash generated.
11515      */
11516 
11517     params.rss_obj = rss_obj;
11518 
11519     bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11520 
11521     bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11522 
11523     /* RSS configuration */
11524     bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11525     bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11526     bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11527     bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11528     if (rss_obj->udp_rss_v4) {
11529         bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11530     }
11531     if (rss_obj->udp_rss_v6) {
11532         bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11533     }
11534 
11535     /* Hash bits */
11536     params.rss_result_mask = MULTI_MASK;
11537 
11538     memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11539 
11540     if (config_hash) {
11541         /* RSS keys */
11542         for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11543             params.rss_key[i] = arc4random();
11544         }
11545 
11546         bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11547     }
11548 
11549     return (ecore_config_rss(sc, &params));
11550 }
11551 
11552 static int
11553 bxe_config_rss_eth(struct bxe_softc *sc,
11554                    uint8_t          config_hash)
11555 {
11556     return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11557 }
11558 
11559 static int
11560 bxe_init_rss_pf(struct bxe_softc *sc)
11561 {
11562     uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11563     int i;
11564 
11565     /*
11566      * Prepare the initial contents of the indirection table if
11567      * RSS is enabled
11568      */
11569     for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11570         sc->rss_conf_obj.ind_table[i] =
11571             (sc->fp->cl_id + (i % num_eth_queues));
11572     }
11573 
11574     if (sc->udp_rss) {
11575         sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11576     }
11577 
11578     /*
11579      * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11580      * per-port, so if explicit configuration is needed, do it only
11581      * for a PMF.
11582      *
11583      * For 57712 and newer it's a per-function configuration.
11584      */
11585     return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11586 }
11587 
11588 static int
11589 bxe_set_mac_one(struct bxe_softc          *sc,
11590                 uint8_t                   *mac,
11591                 struct ecore_vlan_mac_obj *obj,
11592                 uint8_t                   set,
11593                 int                       mac_type,
11594                 unsigned long             *ramrod_flags)
11595 {
11596     struct ecore_vlan_mac_ramrod_params ramrod_param;
11597     int rc;
11598 
11599     memset(&ramrod_param, 0, sizeof(ramrod_param));
11600 
11601     /* fill in general parameters */
11602     ramrod_param.vlan_mac_obj = obj;
11603     ramrod_param.ramrod_flags = *ramrod_flags;
11604 
11605     /* fill a user request section if needed */
11606     if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11607         memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11608 
11609         bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11610 
11611         /* Set the command: ADD or DEL */
11612         ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11613                                             ECORE_VLAN_MAC_DEL;
11614     }
11615 
11616     rc = ecore_config_vlan_mac(sc, &ramrod_param);
11617 
11618     if (rc == ECORE_EXISTS) {
11619         BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11620         /* do not treat adding same MAC as error */
11621         rc = 0;
11622     } else if (rc < 0) {
11623         BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11624     }
11625 
11626     return (rc);
11627 }
11628 
11629 static int
11630 bxe_set_eth_mac(struct bxe_softc *sc,
11631                 uint8_t          set)
11632 {
11633     unsigned long ramrod_flags = 0;
11634 
11635     BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11636 
11637     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11638 
11639     /* Eth MAC is set on RSS leading client (fp[0]) */
11640     return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11641                             &sc->sp_objs->mac_obj,
11642                             set, ECORE_ETH_MAC, &ramrod_flags));
11643 }
11644 
11645 static int
11646 bxe_get_cur_phy_idx(struct bxe_softc *sc)
11647 {
11648     uint32_t sel_phy_idx = 0;
11649 
11650     if (sc->link_params.num_phys <= 1) {
11651         return (ELINK_INT_PHY);
11652     }
11653 
11654     if (sc->link_vars.link_up) {
11655         sel_phy_idx = ELINK_EXT_PHY1;
11656         /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11657         if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11658             (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11659              ELINK_SUPPORTED_FIBRE))
11660             sel_phy_idx = ELINK_EXT_PHY2;
11661     } else {
11662         switch (elink_phy_selection(&sc->link_params)) {
11663         case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11664         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11665         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11666                sel_phy_idx = ELINK_EXT_PHY1;
11667                break;
11668         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11669         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11670                sel_phy_idx = ELINK_EXT_PHY2;
11671                break;
11672         }
11673     }
11674 
11675     return (sel_phy_idx);
11676 }
11677 
11678 static int
11679 bxe_get_link_cfg_idx(struct bxe_softc *sc)
11680 {
11681     uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11682 
11683     /*
11684      * The selected activated PHY is always after swapping (in case PHY
11685      * swapping is enabled). So when swapping is enabled, we need to reverse
11686      * the configuration
11687      */
11688 
11689     if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11690         if (sel_phy_idx == ELINK_EXT_PHY1)
11691             sel_phy_idx = ELINK_EXT_PHY2;
11692         else if (sel_phy_idx == ELINK_EXT_PHY2)
11693             sel_phy_idx = ELINK_EXT_PHY1;
11694     }
11695 
11696     return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11697 }
11698 
11699 static void
11700 bxe_set_requested_fc(struct bxe_softc *sc)
11701 {
11702     /*
11703      * Initialize link parameters structure variables
11704      * It is recommended to turn off RX FC for jumbo frames
11705      * for better performance
11706      */
11707     if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11708         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11709     } else {
11710         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11711     }
11712 }
11713 
11714 static void
11715 bxe_calc_fc_adv(struct bxe_softc *sc)
11716 {
11717     uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11718     switch (sc->link_vars.ieee_fc &
11719             MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11720     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
11721     default:
11722         sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11723                                            ADVERTISED_Pause);
11724         break;
11725 
11726     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11727         sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11728                                           ADVERTISED_Pause);
11729         break;
11730 
11731     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11732         sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11733         break;
11734     }
11735 }
11736 
11737 static uint16_t
11738 bxe_get_mf_speed(struct bxe_softc *sc)
11739 {
11740     uint16_t line_speed = sc->link_vars.line_speed;
11741     if (IS_MF(sc)) {
11742         uint16_t maxCfg =
11743             bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11744 
11745         /* calculate the current MAX line speed limit for the MF devices */
11746         if (IS_MF_SI(sc)) {
11747             line_speed = (line_speed * maxCfg) / 100;
11748         } else { /* SD mode */
11749             uint16_t vn_max_rate = maxCfg * 100;
11750 
11751             if (vn_max_rate < line_speed) {
11752                 line_speed = vn_max_rate;
11753             }
11754         }
11755     }
11756 
11757     return (line_speed);
11758 }
11759 
11760 static void
11761 bxe_fill_report_data(struct bxe_softc            *sc,
11762                      struct bxe_link_report_data *data)
11763 {
11764     uint16_t line_speed = bxe_get_mf_speed(sc);
11765 
11766     memset(data, 0, sizeof(*data));
11767 
11768     /* fill the report data with the effective line speed */
11769     data->line_speed = line_speed;
11770 
11771     /* Link is down */
11772     if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11773         bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11774     }
11775 
11776     /* Full DUPLEX */
11777     if (sc->link_vars.duplex == DUPLEX_FULL) {
11778         bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11779     }
11780 
11781     /* Rx Flow Control is ON */
11782     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11783         bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11784     }
11785 
11786     /* Tx Flow Control is ON */
11787     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11788         bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11789     }
11790 }
11791 
11792 /* report link status to OS, should be called under phy_lock */
11793 static void
11794 bxe_link_report_locked(struct bxe_softc *sc)
11795 {
11796     struct bxe_link_report_data cur_data;
11797 
11798     /* reread mf_cfg */
11799     if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11800         bxe_read_mf_cfg(sc);
11801     }
11802 
11803     /* Read the current link report info */
11804     bxe_fill_report_data(sc, &cur_data);
11805 
11806     /* Don't report link down or exactly the same link status twice */
11807     if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11808         (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11809                       &sc->last_reported_link.link_report_flags) &&
11810          bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11811                       &cur_data.link_report_flags))) {
11812         return;
11813     }
11814 
11815     sc->link_cnt++;
11816 
11817     /* report new link params and remember the state for the next time */
11818     memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11819 
11820     if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11821                      &cur_data.link_report_flags)) {
11822         if_link_state_change(sc->ifp, LINK_STATE_DOWN);
11823         BLOGI(sc, "NIC Link is Down\n");
11824     } else {
11825         const char *duplex;
11826         const char *flow;
11827 
11828         if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11829                                    &cur_data.link_report_flags)) {
11830             duplex = "full";
11831         } else {
11832             duplex = "half";
11833         }
11834 
11835         /*
11836          * Handle the FC at the end so that only these flags would be
11837          * possibly set. This way we may easily check if there is no FC
11838          * enabled.
11839          */
11840         if (cur_data.link_report_flags) {
11841             if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11842                              &cur_data.link_report_flags) &&
11843                 bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11844                              &cur_data.link_report_flags)) {
11845                 flow = "ON - receive & transmit";
11846             } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11847                                     &cur_data.link_report_flags) &&
11848                        !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11849                                      &cur_data.link_report_flags)) {
11850                 flow = "ON - receive";
11851             } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11852                                      &cur_data.link_report_flags) &&
11853                        bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11854                                     &cur_data.link_report_flags)) {
11855                 flow = "ON - transmit";
11856             } else {
11857                 flow = "none"; /* possible? */
11858             }
11859         } else {
11860             flow = "none";
11861         }
11862 
11863         if_link_state_change(sc->ifp, LINK_STATE_UP);
11864         BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11865               cur_data.line_speed, duplex, flow);
11866     }
11867 }
11868 
11869 static void
11870 bxe_link_report(struct bxe_softc *sc)
11871 {
11872     bxe_acquire_phy_lock(sc);
11873     bxe_link_report_locked(sc);
11874     bxe_release_phy_lock(sc);
11875 }
11876 
11877 static void
11878 bxe_link_status_update(struct bxe_softc *sc)
11879 {
11880     if (sc->state != BXE_STATE_OPEN) {
11881         return;
11882     }
11883 
11884     if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11885         elink_link_status_update(&sc->link_params, &sc->link_vars);
11886     } else {
11887         sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11888                                   ELINK_SUPPORTED_10baseT_Full |
11889                                   ELINK_SUPPORTED_100baseT_Half |
11890                                   ELINK_SUPPORTED_100baseT_Full |
11891                                   ELINK_SUPPORTED_1000baseT_Full |
11892                                   ELINK_SUPPORTED_2500baseX_Full |
11893                                   ELINK_SUPPORTED_10000baseT_Full |
11894                                   ELINK_SUPPORTED_TP |
11895                                   ELINK_SUPPORTED_FIBRE |
11896                                   ELINK_SUPPORTED_Autoneg |
11897                                   ELINK_SUPPORTED_Pause |
11898                                   ELINK_SUPPORTED_Asym_Pause);
11899         sc->port.advertising[0] = sc->port.supported[0];
11900 
11901         sc->link_params.sc                = sc;
11902         sc->link_params.port              = SC_PORT(sc);
11903         sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11904         sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11905         sc->link_params.req_line_speed[0] = SPEED_10000;
11906         sc->link_params.speed_cap_mask[0] = 0x7f0000;
11907         sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11908 
11909         if (CHIP_REV_IS_FPGA(sc)) {
11910             sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11911             sc->link_vars.line_speed  = ELINK_SPEED_1000;
11912             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11913                                          LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11914         } else {
11915             sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11916             sc->link_vars.line_speed  = ELINK_SPEED_10000;
11917             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11918                                          LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11919         }
11920 
11921         sc->link_vars.link_up = 1;
11922 
11923         sc->link_vars.duplex    = DUPLEX_FULL;
11924         sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11925 
11926         if (IS_PF(sc)) {
11927             REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11928             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11929             bxe_link_report(sc);
11930         }
11931     }
11932 
11933     if (IS_PF(sc)) {
11934         if (sc->link_vars.link_up) {
11935             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11936         } else {
11937             bxe_stats_handle(sc, STATS_EVENT_STOP);
11938         }
11939         bxe_link_report(sc);
11940     } else {
11941         bxe_link_report(sc);
11942         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11943     }
11944 }
11945 
11946 static int
11947 bxe_initial_phy_init(struct bxe_softc *sc,
11948                      int              load_mode)
11949 {
11950     int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
11951     uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
11952     struct elink_params *lp = &sc->link_params;
11953 
11954     bxe_set_requested_fc(sc);
11955 
11956     if (CHIP_REV_IS_SLOW(sc)) {
11957         uint32_t bond = CHIP_BOND_ID(sc);
11958         uint32_t feat = 0;
11959 
11960         if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
11961             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11962         } else if (bond & 0x4) {
11963             if (CHIP_IS_E3(sc)) {
11964                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
11965             } else {
11966                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11967             }
11968         } else if (bond & 0x8) {
11969             if (CHIP_IS_E3(sc)) {
11970                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
11971             } else {
11972                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11973             }
11974         }
11975 
11976         /* disable EMAC for E3 and above */
11977         if (bond & 0x2) {
11978             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11979         }
11980 
11981         sc->link_params.feature_config_flags |= feat;
11982     }
11983 
11984     bxe_acquire_phy_lock(sc);
11985 
11986     if (load_mode == LOAD_DIAG) {
11987         lp->loopback_mode = ELINK_LOOPBACK_XGXS;
11988         /* Prefer doing PHY loopback at 10G speed, if possible */
11989         if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
11990             if (lp->speed_cap_mask[cfg_idx] &
11991                 PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
11992                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
11993             } else {
11994                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
11995             }
11996         }
11997     }
11998 
11999     if (load_mode == LOAD_LOOPBACK_EXT) {
12000         lp->loopback_mode = ELINK_LOOPBACK_EXT;
12001     }
12002 
12003     rc = elink_phy_init(&sc->link_params, &sc->link_vars);
12004 
12005     bxe_release_phy_lock(sc);
12006 
12007     bxe_calc_fc_adv(sc);
12008 
12009     if (sc->link_vars.link_up) {
12010         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12011         bxe_link_report(sc);
12012     }
12013 
12014     if (!CHIP_REV_IS_SLOW(sc)) {
12015         bxe_periodic_start(sc);
12016     }
12017 
12018     sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
12019     return (rc);
12020 }
12021 
12022 /* must be called under IF_ADDR_LOCK */
12023 
12024 static int
12025 bxe_set_mc_list(struct bxe_softc *sc)
12026 {
12027     struct ecore_mcast_ramrod_params rparam = { NULL };
12028     int rc = 0;
12029     int mc_count = 0;
12030     int mcnt, i;
12031     struct ecore_mcast_list_elem *mc_mac, *mc_mac_start;
12032     unsigned char *mta;
12033     if_t ifp = sc->ifp;
12034 
12035     mc_count = if_multiaddr_count(ifp, -1);/* XXX they don't have a limit */
12036     if (!mc_count)
12037         return (0);
12038 
12039     mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN *
12040             mc_count, M_DEVBUF, M_NOWAIT);
12041 
12042     if(mta == NULL) {
12043         BLOGE(sc, "Failed to allocate temp mcast list\n");
12044         return (-1);
12045     }
12046     bzero(mta, (sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count));
12047 
12048     mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF, (M_NOWAIT | M_ZERO));
12049     mc_mac_start = mc_mac;
12050 
12051     if (!mc_mac) {
12052         free(mta, M_DEVBUF);
12053         BLOGE(sc, "Failed to allocate temp mcast list\n");
12054         return (-1);
12055     }
12056     bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12057 
12058     /* mta and mcnt not expected to be  different */
12059     if_multiaddr_array(ifp, mta, &mcnt, mc_count);
12060 
12061 
12062     rparam.mcast_obj = &sc->mcast_obj;
12063     ECORE_LIST_INIT(&rparam.mcast_list);
12064 
12065     for(i=0; i< mcnt; i++) {
12066 
12067         mc_mac->mac = (uint8_t *)(mta + (i * ETHER_ADDR_LEN));
12068         ECORE_LIST_PUSH_TAIL(&mc_mac->link, &rparam.mcast_list);
12069 
12070         BLOGD(sc, DBG_LOAD,
12071               "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
12072               mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12073               mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
12074 
12075         mc_mac++;
12076     }
12077     rparam.mcast_list_len = mc_count;
12078 
12079     BXE_MCAST_LOCK(sc);
12080 
12081     /* first, clear all configured multicast MACs */
12082     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12083     if (rc < 0) {
12084         BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12085         BXE_MCAST_UNLOCK(sc);
12086     	free(mc_mac_start, M_DEVBUF);
12087         free(mta, M_DEVBUF);
12088         return (rc);
12089     }
12090 
12091     /* Now add the new MACs */
12092     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12093     if (rc < 0) {
12094         BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12095     }
12096 
12097     BXE_MCAST_UNLOCK(sc);
12098 
12099     free(mc_mac_start, M_DEVBUF);
12100     free(mta, M_DEVBUF);
12101 
12102     return (rc);
12103 }
12104 
12105 static int
12106 bxe_set_uc_list(struct bxe_softc *sc)
12107 {
12108     if_t ifp = sc->ifp;
12109     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12110     struct ifaddr *ifa;
12111     unsigned long ramrod_flags = 0;
12112     int rc;
12113 
12114 #if __FreeBSD_version < 800000
12115     IF_ADDR_LOCK(ifp);
12116 #else
12117     if_addr_rlock(ifp);
12118 #endif
12119 
12120     /* first schedule a cleanup up of old configuration */
12121     rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12122     if (rc < 0) {
12123         BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12124 #if __FreeBSD_version < 800000
12125         IF_ADDR_UNLOCK(ifp);
12126 #else
12127         if_addr_runlock(ifp);
12128 #endif
12129         return (rc);
12130     }
12131 
12132     ifa = if_getifaddr(ifp); /* XXX Is this structure */
12133     while (ifa) {
12134         if (ifa->ifa_addr->sa_family != AF_LINK) {
12135             ifa = TAILQ_NEXT(ifa, ifa_link);
12136             continue;
12137         }
12138 
12139         rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12140                              mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12141         if (rc == -EEXIST) {
12142             BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12143             /* do not treat adding same MAC as an error */
12144             rc = 0;
12145         } else if (rc < 0) {
12146             BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12147 #if __FreeBSD_version < 800000
12148             IF_ADDR_UNLOCK(ifp);
12149 #else
12150             if_addr_runlock(ifp);
12151 #endif
12152             return (rc);
12153         }
12154 
12155         ifa = TAILQ_NEXT(ifa, ifa_link);
12156     }
12157 
12158 #if __FreeBSD_version < 800000
12159     IF_ADDR_UNLOCK(ifp);
12160 #else
12161     if_addr_runlock(ifp);
12162 #endif
12163 
12164     /* Execute the pending commands */
12165     bit_set(&ramrod_flags, RAMROD_CONT);
12166     return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12167                             ECORE_UC_LIST_MAC, &ramrod_flags));
12168 }
12169 
12170 static void
12171 bxe_set_rx_mode(struct bxe_softc *sc)
12172 {
12173     if_t ifp = sc->ifp;
12174     uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12175 
12176     if (sc->state != BXE_STATE_OPEN) {
12177         BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12178         return;
12179     }
12180 
12181     BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12182 
12183     if (if_getflags(ifp) & IFF_PROMISC) {
12184         rx_mode = BXE_RX_MODE_PROMISC;
12185     } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12186                ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12187                 CHIP_IS_E1(sc))) {
12188         rx_mode = BXE_RX_MODE_ALLMULTI;
12189     } else {
12190         if (IS_PF(sc)) {
12191             /* some multicasts */
12192             if (bxe_set_mc_list(sc) < 0) {
12193                 rx_mode = BXE_RX_MODE_ALLMULTI;
12194             }
12195             if (bxe_set_uc_list(sc) < 0) {
12196                 rx_mode = BXE_RX_MODE_PROMISC;
12197             }
12198         }
12199     }
12200 
12201     sc->rx_mode = rx_mode;
12202 
12203     /* schedule the rx_mode command */
12204     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12205         BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12206         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12207         return;
12208     }
12209 
12210     if (IS_PF(sc)) {
12211         bxe_set_storm_rx_mode(sc);
12212     }
12213 }
12214 
12215 
12216 /* update flags in shmem */
12217 static void
12218 bxe_update_drv_flags(struct bxe_softc *sc,
12219                      uint32_t         flags,
12220                      uint32_t         set)
12221 {
12222     uint32_t drv_flags;
12223 
12224     if (SHMEM2_HAS(sc, drv_flags)) {
12225         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12226         drv_flags = SHMEM2_RD(sc, drv_flags);
12227 
12228         if (set) {
12229             SET_FLAGS(drv_flags, flags);
12230         } else {
12231             RESET_FLAGS(drv_flags, flags);
12232         }
12233 
12234         SHMEM2_WR(sc, drv_flags, drv_flags);
12235         BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12236 
12237         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12238     }
12239 }
12240 
12241 /* periodic timer callout routine, only runs when the interface is up */
12242 
12243 static void
12244 bxe_periodic_callout_func(void *xsc)
12245 {
12246     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12247     int i;
12248 
12249     if (!BXE_CORE_TRYLOCK(sc)) {
12250         /* just bail and try again next time */
12251 
12252         if ((sc->state == BXE_STATE_OPEN) &&
12253             (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12254             /* schedule the next periodic callout */
12255             callout_reset(&sc->periodic_callout, hz,
12256                           bxe_periodic_callout_func, sc);
12257         }
12258 
12259         return;
12260     }
12261 
12262     if ((sc->state != BXE_STATE_OPEN) ||
12263         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12264         BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12265         BXE_CORE_UNLOCK(sc);
12266         return;
12267     }
12268 
12269     /* Check for TX timeouts on any fastpath. */
12270     FOR_EACH_QUEUE(sc, i) {
12271         if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12272             /* Ruh-Roh, chip was reset! */
12273             break;
12274         }
12275     }
12276 
12277     if (!CHIP_REV_IS_SLOW(sc)) {
12278         /*
12279          * This barrier is needed to ensure the ordering between the writing
12280          * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12281          * the reading here.
12282          */
12283         mb();
12284         if (sc->port.pmf) {
12285 	    bxe_acquire_phy_lock(sc);
12286             elink_period_func(&sc->link_params, &sc->link_vars);
12287 	    bxe_release_phy_lock(sc);
12288         }
12289     }
12290 
12291     if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12292         int mb_idx = SC_FW_MB_IDX(sc);
12293         uint32_t drv_pulse;
12294         uint32_t mcp_pulse;
12295 
12296         ++sc->fw_drv_pulse_wr_seq;
12297         sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12298 
12299         drv_pulse = sc->fw_drv_pulse_wr_seq;
12300         bxe_drv_pulse(sc);
12301 
12302         mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12303                      MCP_PULSE_SEQ_MASK);
12304 
12305         /*
12306          * The delta between driver pulse and mcp response should
12307          * be 1 (before mcp response) or 0 (after mcp response).
12308          */
12309         if ((drv_pulse != mcp_pulse) &&
12310             (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12311             /* someone lost a heartbeat... */
12312             BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12313                   drv_pulse, mcp_pulse);
12314         }
12315     }
12316 
12317     /* state is BXE_STATE_OPEN */
12318     bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12319 
12320     BXE_CORE_UNLOCK(sc);
12321 
12322     if ((sc->state == BXE_STATE_OPEN) &&
12323         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12324         /* schedule the next periodic callout */
12325         callout_reset(&sc->periodic_callout, hz,
12326                       bxe_periodic_callout_func, sc);
12327     }
12328 }
12329 
12330 static void
12331 bxe_periodic_start(struct bxe_softc *sc)
12332 {
12333     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12334     callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12335 }
12336 
12337 static void
12338 bxe_periodic_stop(struct bxe_softc *sc)
12339 {
12340     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12341     callout_drain(&sc->periodic_callout);
12342 }
12343 
12344 /* start the controller */
12345 static __noinline int
12346 bxe_nic_load(struct bxe_softc *sc,
12347              int              load_mode)
12348 {
12349     uint32_t val;
12350     int load_code = 0;
12351     int i, rc = 0;
12352 
12353     BXE_CORE_LOCK_ASSERT(sc);
12354 
12355     BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12356 
12357     sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12358 
12359     if (IS_PF(sc)) {
12360         /* must be called before memory allocation and HW init */
12361         bxe_ilt_set_info(sc);
12362     }
12363 
12364     sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12365 
12366     bxe_set_fp_rx_buf_size(sc);
12367 
12368     if (bxe_alloc_fp_buffers(sc) != 0) {
12369         BLOGE(sc, "Failed to allocate fastpath memory\n");
12370         sc->state = BXE_STATE_CLOSED;
12371         rc = ENOMEM;
12372         goto bxe_nic_load_error0;
12373     }
12374 
12375     if (bxe_alloc_mem(sc) != 0) {
12376         sc->state = BXE_STATE_CLOSED;
12377         rc = ENOMEM;
12378         goto bxe_nic_load_error0;
12379     }
12380 
12381     if (bxe_alloc_fw_stats_mem(sc) != 0) {
12382         sc->state = BXE_STATE_CLOSED;
12383         rc = ENOMEM;
12384         goto bxe_nic_load_error0;
12385     }
12386 
12387     if (IS_PF(sc)) {
12388         /* set pf load just before approaching the MCP */
12389         bxe_set_pf_load(sc);
12390 
12391         /* if MCP exists send load request and analyze response */
12392         if (!BXE_NOMCP(sc)) {
12393             /* attempt to load pf */
12394             if (bxe_nic_load_request(sc, &load_code) != 0) {
12395                 sc->state = BXE_STATE_CLOSED;
12396                 rc = ENXIO;
12397                 goto bxe_nic_load_error1;
12398             }
12399 
12400             /* what did the MCP say? */
12401             if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12402                 bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12403                 sc->state = BXE_STATE_CLOSED;
12404                 rc = ENXIO;
12405                 goto bxe_nic_load_error2;
12406             }
12407         } else {
12408             BLOGI(sc, "Device has no MCP!\n");
12409             load_code = bxe_nic_load_no_mcp(sc);
12410         }
12411 
12412         /* mark PMF if applicable */
12413         bxe_nic_load_pmf(sc, load_code);
12414 
12415         /* Init Function state controlling object */
12416         bxe_init_func_obj(sc);
12417 
12418         /* Initialize HW */
12419         if (bxe_init_hw(sc, load_code) != 0) {
12420             BLOGE(sc, "HW init failed\n");
12421             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12422             sc->state = BXE_STATE_CLOSED;
12423             rc = ENXIO;
12424             goto bxe_nic_load_error2;
12425         }
12426     }
12427 
12428     /* set ALWAYS_ALIVE bit in shmem */
12429     sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12430     bxe_drv_pulse(sc);
12431     sc->flags |= BXE_NO_PULSE;
12432 
12433     /* attach interrupts */
12434     if (bxe_interrupt_attach(sc) != 0) {
12435         sc->state = BXE_STATE_CLOSED;
12436         rc = ENXIO;
12437         goto bxe_nic_load_error2;
12438     }
12439 
12440     bxe_nic_init(sc, load_code);
12441 
12442     /* Init per-function objects */
12443     if (IS_PF(sc)) {
12444         bxe_init_objs(sc);
12445         // XXX bxe_iov_nic_init(sc);
12446 
12447         /* set AFEX default VLAN tag to an invalid value */
12448         sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12449         // XXX bxe_nic_load_afex_dcc(sc, load_code);
12450 
12451         sc->state = BXE_STATE_OPENING_WAITING_PORT;
12452         rc = bxe_func_start(sc);
12453         if (rc) {
12454             BLOGE(sc, "Function start failed! rc = %d\n", rc);
12455             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12456             sc->state = BXE_STATE_ERROR;
12457             goto bxe_nic_load_error3;
12458         }
12459 
12460         /* send LOAD_DONE command to MCP */
12461         if (!BXE_NOMCP(sc)) {
12462             load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12463             if (!load_code) {
12464                 BLOGE(sc, "MCP response failure, aborting\n");
12465                 sc->state = BXE_STATE_ERROR;
12466                 rc = ENXIO;
12467                 goto bxe_nic_load_error3;
12468             }
12469         }
12470 
12471         rc = bxe_setup_leading(sc);
12472         if (rc) {
12473             BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12474             sc->state = BXE_STATE_ERROR;
12475             goto bxe_nic_load_error3;
12476         }
12477 
12478         FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12479             rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12480             if (rc) {
12481                 BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12482                 sc->state = BXE_STATE_ERROR;
12483                 goto bxe_nic_load_error3;
12484             }
12485         }
12486 
12487         rc = bxe_init_rss_pf(sc);
12488         if (rc) {
12489             BLOGE(sc, "PF RSS init failed\n");
12490             sc->state = BXE_STATE_ERROR;
12491             goto bxe_nic_load_error3;
12492         }
12493     }
12494     /* XXX VF */
12495 
12496     /* now when Clients are configured we are ready to work */
12497     sc->state = BXE_STATE_OPEN;
12498 
12499     /* Configure a ucast MAC */
12500     if (IS_PF(sc)) {
12501         rc = bxe_set_eth_mac(sc, TRUE);
12502     }
12503     if (rc) {
12504         BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12505         sc->state = BXE_STATE_ERROR;
12506         goto bxe_nic_load_error3;
12507     }
12508 
12509     if (sc->port.pmf) {
12510         rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12511         if (rc) {
12512             sc->state = BXE_STATE_ERROR;
12513             goto bxe_nic_load_error3;
12514         }
12515     }
12516 
12517     sc->link_params.feature_config_flags &=
12518         ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12519 
12520     /* start fast path */
12521 
12522     /* Initialize Rx filter */
12523     bxe_set_rx_mode(sc);
12524 
12525     /* start the Tx */
12526     switch (/* XXX load_mode */LOAD_OPEN) {
12527     case LOAD_NORMAL:
12528     case LOAD_OPEN:
12529         break;
12530 
12531     case LOAD_DIAG:
12532     case LOAD_LOOPBACK_EXT:
12533         sc->state = BXE_STATE_DIAG;
12534         break;
12535 
12536     default:
12537         break;
12538     }
12539 
12540     if (sc->port.pmf) {
12541         bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12542     } else {
12543         bxe_link_status_update(sc);
12544     }
12545 
12546     /* start the periodic timer callout */
12547     bxe_periodic_start(sc);
12548 
12549     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12550         /* mark driver is loaded in shmem2 */
12551         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12552         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12553                   (val |
12554                    DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12555                    DRV_FLAGS_CAPABILITIES_LOADED_L2));
12556     }
12557 
12558     /* wait for all pending SP commands to complete */
12559     if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12560         BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12561         bxe_periodic_stop(sc);
12562         bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12563         return (ENXIO);
12564     }
12565 
12566     /* Tell the stack the driver is running! */
12567     if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
12568 
12569     BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12570 
12571     return (0);
12572 
12573 bxe_nic_load_error3:
12574 
12575     if (IS_PF(sc)) {
12576         bxe_int_disable_sync(sc, 1);
12577 
12578         /* clean out queued objects */
12579         bxe_squeeze_objects(sc);
12580     }
12581 
12582     bxe_interrupt_detach(sc);
12583 
12584 bxe_nic_load_error2:
12585 
12586     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12587         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12588         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12589     }
12590 
12591     sc->port.pmf = 0;
12592 
12593 bxe_nic_load_error1:
12594 
12595     /* clear pf_load status, as it was already set */
12596     if (IS_PF(sc)) {
12597         bxe_clear_pf_load(sc);
12598     }
12599 
12600 bxe_nic_load_error0:
12601 
12602     bxe_free_fw_stats_mem(sc);
12603     bxe_free_fp_buffers(sc);
12604     bxe_free_mem(sc);
12605 
12606     return (rc);
12607 }
12608 
12609 static int
12610 bxe_init_locked(struct bxe_softc *sc)
12611 {
12612     int other_engine = SC_PATH(sc) ? 0 : 1;
12613     uint8_t other_load_status, load_status;
12614     uint8_t global = FALSE;
12615     int rc;
12616 
12617     BXE_CORE_LOCK_ASSERT(sc);
12618 
12619     /* check if the driver is already running */
12620     if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12621         BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12622         return (0);
12623     }
12624 
12625     bxe_set_power_state(sc, PCI_PM_D0);
12626 
12627     /*
12628      * If parity occurred during the unload, then attentions and/or
12629      * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12630      * loaded on the current engine to complete the recovery. Parity recovery
12631      * is only relevant for PF driver.
12632      */
12633     if (IS_PF(sc)) {
12634         other_load_status = bxe_get_load_status(sc, other_engine);
12635         load_status = bxe_get_load_status(sc, SC_PATH(sc));
12636 
12637         if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12638             bxe_chk_parity_attn(sc, &global, TRUE)) {
12639             do {
12640                 /*
12641                  * If there are attentions and they are in global blocks, set
12642                  * the GLOBAL_RESET bit regardless whether it will be this
12643                  * function that will complete the recovery or not.
12644                  */
12645                 if (global) {
12646                     bxe_set_reset_global(sc);
12647                 }
12648 
12649                 /*
12650                  * Only the first function on the current engine should try
12651                  * to recover in open. In case of attentions in global blocks
12652                  * only the first in the chip should try to recover.
12653                  */
12654                 if ((!load_status && (!global || !other_load_status)) &&
12655                     bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12656                     BLOGI(sc, "Recovered during init\n");
12657                     break;
12658                 }
12659 
12660                 /* recovery has failed... */
12661                 bxe_set_power_state(sc, PCI_PM_D3hot);
12662                 sc->recovery_state = BXE_RECOVERY_FAILED;
12663 
12664                 BLOGE(sc, "Recovery flow hasn't properly "
12665                           "completed yet, try again later. "
12666                           "If you still see this message after a "
12667                           "few retries then power cycle is required.\n");
12668 
12669                 rc = ENXIO;
12670                 goto bxe_init_locked_done;
12671             } while (0);
12672         }
12673     }
12674 
12675     sc->recovery_state = BXE_RECOVERY_DONE;
12676 
12677     rc = bxe_nic_load(sc, LOAD_OPEN);
12678 
12679 bxe_init_locked_done:
12680 
12681     if (rc) {
12682         /* Tell the stack the driver is NOT running! */
12683         BLOGE(sc, "Initialization failed, "
12684                   "stack notified driver is NOT running!\n");
12685 	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
12686     }
12687 
12688     return (rc);
12689 }
12690 
12691 static int
12692 bxe_stop_locked(struct bxe_softc *sc)
12693 {
12694     BXE_CORE_LOCK_ASSERT(sc);
12695     return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
12696 }
12697 
12698 /*
12699  * Handles controller initialization when called from an unlocked routine.
12700  * ifconfig calls this function.
12701  *
12702  * Returns:
12703  *   void
12704  */
12705 static void
12706 bxe_init(void *xsc)
12707 {
12708     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12709 
12710     BXE_CORE_LOCK(sc);
12711     bxe_init_locked(sc);
12712     BXE_CORE_UNLOCK(sc);
12713 }
12714 
12715 static int
12716 bxe_init_ifnet(struct bxe_softc *sc)
12717 {
12718     if_t ifp;
12719     int capabilities;
12720 
12721     /* ifconfig entrypoint for media type/status reporting */
12722     ifmedia_init(&sc->ifmedia, IFM_IMASK,
12723                  bxe_ifmedia_update,
12724                  bxe_ifmedia_status);
12725 
12726     /* set the default interface values */
12727     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
12728     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
12729     ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
12730 
12731     sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
12732 
12733     /* allocate the ifnet structure */
12734     if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
12735         BLOGE(sc, "Interface allocation failed!\n");
12736         return (ENXIO);
12737     }
12738 
12739     if_setsoftc(ifp, sc);
12740     if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
12741     if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
12742     if_setioctlfn(ifp, bxe_ioctl);
12743     if_setstartfn(ifp, bxe_tx_start);
12744     if_setgetcounterfn(ifp, bxe_get_counter);
12745 #if __FreeBSD_version >= 800000
12746     if_settransmitfn(ifp, bxe_tx_mq_start);
12747     if_setqflushfn(ifp, bxe_mq_flush);
12748 #endif
12749 #ifdef FreeBSD8_0
12750     if_settimer(ifp, 0);
12751 #endif
12752     if_setinitfn(ifp, bxe_init);
12753     if_setmtu(ifp, sc->mtu);
12754     if_sethwassist(ifp, (CSUM_IP      |
12755                         CSUM_TCP      |
12756                         CSUM_UDP      |
12757                         CSUM_TSO      |
12758                         CSUM_TCP_IPV6 |
12759                         CSUM_UDP_IPV6));
12760 
12761     capabilities =
12762 #if __FreeBSD_version < 700000
12763         (IFCAP_VLAN_MTU       |
12764          IFCAP_VLAN_HWTAGGING |
12765          IFCAP_HWCSUM         |
12766          IFCAP_JUMBO_MTU      |
12767          IFCAP_LRO);
12768 #else
12769         (IFCAP_VLAN_MTU       |
12770          IFCAP_VLAN_HWTAGGING |
12771          IFCAP_VLAN_HWTSO     |
12772          IFCAP_VLAN_HWFILTER  |
12773          IFCAP_VLAN_HWCSUM    |
12774          IFCAP_HWCSUM         |
12775          IFCAP_JUMBO_MTU      |
12776          IFCAP_LRO            |
12777          IFCAP_TSO4           |
12778          IFCAP_TSO6           |
12779          IFCAP_WOL_MAGIC);
12780 #endif
12781     if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
12782     if_setbaudrate(ifp, IF_Gbps(10));
12783 /* XXX */
12784     if_setsendqlen(ifp, sc->tx_ring_size);
12785     if_setsendqready(ifp);
12786 /* XXX */
12787 
12788     sc->ifp = ifp;
12789 
12790     /* attach to the Ethernet interface list */
12791     ether_ifattach(ifp, sc->link_params.mac_addr);
12792 
12793     return (0);
12794 }
12795 
12796 static void
12797 bxe_deallocate_bars(struct bxe_softc *sc)
12798 {
12799     int i;
12800 
12801     for (i = 0; i < MAX_BARS; i++) {
12802         if (sc->bar[i].resource != NULL) {
12803             bus_release_resource(sc->dev,
12804                                  SYS_RES_MEMORY,
12805                                  sc->bar[i].rid,
12806                                  sc->bar[i].resource);
12807             BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
12808                   i, PCIR_BAR(i));
12809         }
12810     }
12811 }
12812 
12813 static int
12814 bxe_allocate_bars(struct bxe_softc *sc)
12815 {
12816     u_int flags;
12817     int i;
12818 
12819     memset(sc->bar, 0, sizeof(sc->bar));
12820 
12821     for (i = 0; i < MAX_BARS; i++) {
12822 
12823         /* memory resources reside at BARs 0, 2, 4 */
12824         /* Run `pciconf -lb` to see mappings */
12825         if ((i != 0) && (i != 2) && (i != 4)) {
12826             continue;
12827         }
12828 
12829         sc->bar[i].rid = PCIR_BAR(i);
12830 
12831         flags = RF_ACTIVE;
12832         if (i == 0) {
12833             flags |= RF_SHAREABLE;
12834         }
12835 
12836         if ((sc->bar[i].resource =
12837              bus_alloc_resource_any(sc->dev,
12838                                     SYS_RES_MEMORY,
12839                                     &sc->bar[i].rid,
12840                                     flags)) == NULL) {
12841             return (0);
12842         }
12843 
12844         sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
12845         sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
12846         sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
12847 
12848         BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%ld) -> %p\n",
12849               i, PCIR_BAR(i),
12850               (void *)rman_get_start(sc->bar[i].resource),
12851               (void *)rman_get_end(sc->bar[i].resource),
12852               rman_get_size(sc->bar[i].resource),
12853               (void *)sc->bar[i].kva);
12854     }
12855 
12856     return (0);
12857 }
12858 
12859 static void
12860 bxe_get_function_num(struct bxe_softc *sc)
12861 {
12862     uint32_t val = 0;
12863 
12864     /*
12865      * Read the ME register to get the function number. The ME register
12866      * holds the relative-function number and absolute-function number. The
12867      * absolute-function number appears only in E2 and above. Before that
12868      * these bits always contained zero, therefore we cannot blindly use them.
12869      */
12870 
12871     val = REG_RD(sc, BAR_ME_REGISTER);
12872 
12873     sc->pfunc_rel =
12874         (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
12875     sc->path_id =
12876         (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
12877 
12878     if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
12879         sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
12880     } else {
12881         sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
12882     }
12883 
12884     BLOGD(sc, DBG_LOAD,
12885           "Relative function %d, Absolute function %d, Path %d\n",
12886           sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
12887 }
12888 
12889 static uint32_t
12890 bxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
12891 {
12892     uint32_t shmem2_size;
12893     uint32_t offset;
12894     uint32_t mf_cfg_offset_value;
12895 
12896     /* Non 57712 */
12897     offset = (SHMEM_RD(sc, func_mb) +
12898               (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
12899 
12900     /* 57712 plus */
12901     if (sc->devinfo.shmem2_base != 0) {
12902         shmem2_size = SHMEM2_RD(sc, size);
12903         if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
12904             mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
12905             if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
12906                 offset = mf_cfg_offset_value;
12907             }
12908         }
12909     }
12910 
12911     return (offset);
12912 }
12913 
12914 static uint32_t
12915 bxe_pcie_capability_read(struct bxe_softc *sc,
12916                          int    reg,
12917                          int    width)
12918 {
12919     int pcie_reg;
12920 
12921     /* ensure PCIe capability is enabled */
12922     if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
12923         if (pcie_reg != 0) {
12924             BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
12925             return (pci_read_config(sc->dev, (pcie_reg + reg), width));
12926         }
12927     }
12928 
12929     BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
12930 
12931     return (0);
12932 }
12933 
12934 static uint8_t
12935 bxe_is_pcie_pending(struct bxe_softc *sc)
12936 {
12937     return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
12938             PCIM_EXP_STA_TRANSACTION_PND);
12939 }
12940 
12941 /*
12942  * Walk the PCI capabiites list for the device to find what features are
12943  * supported. These capabilites may be enabled/disabled by firmware so it's
12944  * best to walk the list rather than make assumptions.
12945  */
12946 static void
12947 bxe_probe_pci_caps(struct bxe_softc *sc)
12948 {
12949     uint16_t link_status;
12950     int reg;
12951 
12952     /* check if PCI Power Management is enabled */
12953     if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
12954         if (reg != 0) {
12955             BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
12956 
12957             sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
12958             sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
12959         }
12960     }
12961 
12962     link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
12963 
12964     /* handle PCIe 2.0 workarounds for 57710 */
12965     if (CHIP_IS_E1(sc)) {
12966         /* workaround for 57710 errata E4_57710_27462 */
12967         sc->devinfo.pcie_link_speed =
12968             (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
12969 
12970         /* workaround for 57710 errata E4_57710_27488 */
12971         sc->devinfo.pcie_link_width =
12972             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12973         if (sc->devinfo.pcie_link_speed > 1) {
12974             sc->devinfo.pcie_link_width =
12975                 ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
12976         }
12977     } else {
12978         sc->devinfo.pcie_link_speed =
12979             (link_status & PCIM_LINK_STA_SPEED);
12980         sc->devinfo.pcie_link_width =
12981             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12982     }
12983 
12984     BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
12985           sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
12986 
12987     sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
12988     sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
12989 
12990     /* check if MSI capability is enabled */
12991     if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
12992         if (reg != 0) {
12993             BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
12994 
12995             sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
12996             sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
12997         }
12998     }
12999 
13000     /* check if MSI-X capability is enabled */
13001     if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
13002         if (reg != 0) {
13003             BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
13004 
13005             sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
13006             sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
13007         }
13008     }
13009 }
13010 
13011 static int
13012 bxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
13013 {
13014     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13015     uint32_t val;
13016 
13017     /* get the outer vlan if we're in switch-dependent mode */
13018 
13019     val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13020     mf_info->ext_id = (uint16_t)val;
13021 
13022     mf_info->multi_vnics_mode = 1;
13023 
13024     if (!VALID_OVLAN(mf_info->ext_id)) {
13025         BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13026         return (1);
13027     }
13028 
13029     /* get the capabilities */
13030     if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13031         FUNC_MF_CFG_PROTOCOL_ISCSI) {
13032         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13033     } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13034                FUNC_MF_CFG_PROTOCOL_FCOE) {
13035         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13036     } else {
13037         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13038     }
13039 
13040     mf_info->vnics_per_port =
13041         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13042 
13043     return (0);
13044 }
13045 
13046 static uint32_t
13047 bxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13048 {
13049     uint32_t retval = 0;
13050     uint32_t val;
13051 
13052     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13053 
13054     if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13055         if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13056             retval |= MF_PROTO_SUPPORT_ETHERNET;
13057         }
13058         if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13059             retval |= MF_PROTO_SUPPORT_ISCSI;
13060         }
13061         if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13062             retval |= MF_PROTO_SUPPORT_FCOE;
13063         }
13064     }
13065 
13066     return (retval);
13067 }
13068 
13069 static int
13070 bxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13071 {
13072     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13073     uint32_t val;
13074 
13075     /*
13076      * There is no outer vlan if we're in switch-independent mode.
13077      * If the mac is valid then assume multi-function.
13078      */
13079 
13080     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13081 
13082     mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13083 
13084     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13085 
13086     mf_info->vnics_per_port =
13087         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13088 
13089     return (0);
13090 }
13091 
13092 static int
13093 bxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13094 {
13095     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13096     uint32_t e1hov_tag;
13097     uint32_t func_config;
13098     uint32_t niv_config;
13099 
13100     mf_info->multi_vnics_mode = 1;
13101 
13102     e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13103     func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13104     niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13105 
13106     mf_info->ext_id =
13107         (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13108                    FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13109 
13110     mf_info->default_vlan =
13111         (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13112                    FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13113 
13114     mf_info->niv_allowed_priorities =
13115         (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13116                   FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13117 
13118     mf_info->niv_default_cos =
13119         (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13120                   FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13121 
13122     mf_info->afex_vlan_mode =
13123         ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13124          FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13125 
13126     mf_info->niv_mba_enabled =
13127         ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13128          FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13129 
13130     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13131 
13132     mf_info->vnics_per_port =
13133         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13134 
13135     return (0);
13136 }
13137 
13138 static int
13139 bxe_check_valid_mf_cfg(struct bxe_softc *sc)
13140 {
13141     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13142     uint32_t mf_cfg1;
13143     uint32_t mf_cfg2;
13144     uint32_t ovlan1;
13145     uint32_t ovlan2;
13146     uint8_t i, j;
13147 
13148     BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13149           SC_PORT(sc));
13150     BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13151           mf_info->mf_config[SC_VN(sc)]);
13152     BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13153           mf_info->multi_vnics_mode);
13154     BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13155           mf_info->vnics_per_port);
13156     BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13157           mf_info->ext_id);
13158     BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13159           mf_info->min_bw[0], mf_info->min_bw[1],
13160           mf_info->min_bw[2], mf_info->min_bw[3]);
13161     BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13162           mf_info->max_bw[0], mf_info->max_bw[1],
13163           mf_info->max_bw[2], mf_info->max_bw[3]);
13164     BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13165           sc->mac_addr_str);
13166 
13167     /* various MF mode sanity checks... */
13168 
13169     if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13170         BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13171               SC_PORT(sc));
13172         return (1);
13173     }
13174 
13175     if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13176         BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13177               mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13178         return (1);
13179     }
13180 
13181     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13182         /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13183         if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13184             BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13185                   SC_VN(sc), OVLAN(sc));
13186             return (1);
13187         }
13188 
13189         if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13190             BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13191                   mf_info->multi_vnics_mode, OVLAN(sc));
13192             return (1);
13193         }
13194 
13195         /*
13196          * Verify all functions are either MF or SF mode. If MF, make sure
13197          * sure that all non-hidden functions have a valid ovlan. If SF,
13198          * make sure that all non-hidden functions have an invalid ovlan.
13199          */
13200         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13201             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13202             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13203             if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13204                 (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13205                  ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13206                 BLOGE(sc, "mf_mode=SD function %d MF config "
13207                           "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13208                       i, mf_info->multi_vnics_mode, ovlan1);
13209                 return (1);
13210             }
13211         }
13212 
13213         /* Verify all funcs on the same port each have a different ovlan. */
13214         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13215             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13216             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13217             /* iterate from the next function on the port to the max func */
13218             for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13219                 mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13220                 ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13221                 if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13222                     VALID_OVLAN(ovlan1) &&
13223                     !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13224                     VALID_OVLAN(ovlan2) &&
13225                     (ovlan1 == ovlan2)) {
13226                     BLOGE(sc, "mf_mode=SD functions %d and %d "
13227                               "have the same ovlan (%d)\n",
13228                           i, j, ovlan1);
13229                     return (1);
13230                 }
13231             }
13232         }
13233     } /* MULTI_FUNCTION_SD */
13234 
13235     return (0);
13236 }
13237 
13238 static int
13239 bxe_get_mf_cfg_info(struct bxe_softc *sc)
13240 {
13241     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13242     uint32_t val, mac_upper;
13243     uint8_t i, vnic;
13244 
13245     /* initialize mf_info defaults */
13246     mf_info->vnics_per_port   = 1;
13247     mf_info->multi_vnics_mode = FALSE;
13248     mf_info->path_has_ovlan   = FALSE;
13249     mf_info->mf_mode          = SINGLE_FUNCTION;
13250 
13251     if (!CHIP_IS_MF_CAP(sc)) {
13252         return (0);
13253     }
13254 
13255     if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13256         BLOGE(sc, "Invalid mf_cfg_base!\n");
13257         return (1);
13258     }
13259 
13260     /* get the MF mode (switch dependent / independent / single-function) */
13261 
13262     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13263 
13264     switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13265     {
13266     case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13267 
13268         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13269 
13270         /* check for legal upper mac bytes */
13271         if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13272             mf_info->mf_mode = MULTI_FUNCTION_SI;
13273         } else {
13274             BLOGE(sc, "Invalid config for Switch Independent mode\n");
13275         }
13276 
13277         break;
13278 
13279     case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13280     case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13281 
13282         /* get outer vlan configuration */
13283         val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13284 
13285         if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13286             FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13287             mf_info->mf_mode = MULTI_FUNCTION_SD;
13288         } else {
13289             BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13290         }
13291 
13292         break;
13293 
13294     case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13295 
13296         /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13297         return (0);
13298 
13299     case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13300 
13301         /*
13302          * Mark MF mode as NIV if MCP version includes NPAR-SD support
13303          * and the MAC address is valid.
13304          */
13305         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13306 
13307         if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13308             (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13309             mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13310         } else {
13311             BLOGE(sc, "Invalid config for AFEX mode\n");
13312         }
13313 
13314         break;
13315 
13316     default:
13317 
13318         BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13319               (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13320 
13321         return (1);
13322     }
13323 
13324     /* set path mf_mode (which could be different than function mf_mode) */
13325     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13326         mf_info->path_has_ovlan = TRUE;
13327     } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13328         /*
13329          * Decide on path multi vnics mode. If we're not in MF mode and in
13330          * 4-port mode, this is good enough to check vnic-0 of the other port
13331          * on the same path
13332          */
13333         if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13334             uint8_t other_port = !(PORT_ID(sc) & 1);
13335             uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13336 
13337             val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13338 
13339             mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13340         }
13341     }
13342 
13343     if (mf_info->mf_mode == SINGLE_FUNCTION) {
13344         /* invalid MF config */
13345         if (SC_VN(sc) >= 1) {
13346             BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13347             return (1);
13348         }
13349 
13350         return (0);
13351     }
13352 
13353     /* get the MF configuration */
13354     mf_info->mf_config[SC_VN(sc)] =
13355         MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13356 
13357     switch(mf_info->mf_mode)
13358     {
13359     case MULTI_FUNCTION_SD:
13360 
13361         bxe_get_shmem_mf_cfg_info_sd(sc);
13362         break;
13363 
13364     case MULTI_FUNCTION_SI:
13365 
13366         bxe_get_shmem_mf_cfg_info_si(sc);
13367         break;
13368 
13369     case MULTI_FUNCTION_AFEX:
13370 
13371         bxe_get_shmem_mf_cfg_info_niv(sc);
13372         break;
13373 
13374     default:
13375 
13376         BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13377               mf_info->mf_mode);
13378         return (1);
13379     }
13380 
13381     /* get the congestion management parameters */
13382 
13383     vnic = 0;
13384     FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13385         /* get min/max bw */
13386         val = MFCFG_RD(sc, func_mf_config[i].config);
13387         mf_info->min_bw[vnic] =
13388             ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13389         mf_info->max_bw[vnic] =
13390             ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13391         vnic++;
13392     }
13393 
13394     return (bxe_check_valid_mf_cfg(sc));
13395 }
13396 
13397 static int
13398 bxe_get_shmem_info(struct bxe_softc *sc)
13399 {
13400     int port;
13401     uint32_t mac_hi, mac_lo, val;
13402 
13403     port = SC_PORT(sc);
13404     mac_hi = mac_lo = 0;
13405 
13406     sc->link_params.sc   = sc;
13407     sc->link_params.port = port;
13408 
13409     /* get the hardware config info */
13410     sc->devinfo.hw_config =
13411         SHMEM_RD(sc, dev_info.shared_hw_config.config);
13412     sc->devinfo.hw_config2 =
13413         SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13414 
13415     sc->link_params.hw_led_mode =
13416         ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13417          SHARED_HW_CFG_LED_MODE_SHIFT);
13418 
13419     /* get the port feature config */
13420     sc->port.config =
13421         SHMEM_RD(sc, dev_info.port_feature_config[port].config),
13422 
13423     /* get the link params */
13424     sc->link_params.speed_cap_mask[0] =
13425         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13426     sc->link_params.speed_cap_mask[1] =
13427         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13428 
13429     /* get the lane config */
13430     sc->link_params.lane_config =
13431         SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13432 
13433     /* get the link config */
13434     val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13435     sc->port.link_config[ELINK_INT_PHY] = val;
13436     sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13437     sc->port.link_config[ELINK_EXT_PHY1] =
13438         SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13439 
13440     /* get the override preemphasis flag and enable it or turn it off */
13441     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13442     if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13443         sc->link_params.feature_config_flags |=
13444             ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13445     } else {
13446         sc->link_params.feature_config_flags &=
13447             ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13448     }
13449 
13450     /* get the initial value of the link params */
13451     sc->link_params.multi_phy_config =
13452         SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13453 
13454     /* get external phy info */
13455     sc->port.ext_phy_config =
13456         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13457 
13458     /* get the multifunction configuration */
13459     bxe_get_mf_cfg_info(sc);
13460 
13461     /* get the mac address */
13462     if (IS_MF(sc)) {
13463         mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13464         mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13465     } else {
13466         mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13467         mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13468     }
13469 
13470     if ((mac_lo == 0) && (mac_hi == 0)) {
13471         *sc->mac_addr_str = 0;
13472         BLOGE(sc, "No Ethernet address programmed!\n");
13473     } else {
13474         sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13475         sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13476         sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13477         sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13478         sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13479         sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13480         snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13481                  "%02x:%02x:%02x:%02x:%02x:%02x",
13482                  sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13483                  sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13484                  sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13485         BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13486     }
13487 
13488     return (0);
13489 }
13490 
13491 static void
13492 bxe_get_tunable_params(struct bxe_softc *sc)
13493 {
13494     /* sanity checks */
13495 
13496     if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13497         (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13498         (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13499         BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13500         bxe_interrupt_mode = INTR_MODE_MSIX;
13501     }
13502 
13503     if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13504         BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13505         bxe_queue_count = 0;
13506     }
13507 
13508     if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13509         if (bxe_max_rx_bufs == 0) {
13510             bxe_max_rx_bufs = RX_BD_USABLE;
13511         } else {
13512             BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13513             bxe_max_rx_bufs = 2048;
13514         }
13515     }
13516 
13517     if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13518         BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13519         bxe_hc_rx_ticks = 25;
13520     }
13521 
13522     if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13523         BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13524         bxe_hc_tx_ticks = 50;
13525     }
13526 
13527     if (bxe_max_aggregation_size == 0) {
13528         bxe_max_aggregation_size = TPA_AGG_SIZE;
13529     }
13530 
13531     if (bxe_max_aggregation_size > 0xffff) {
13532         BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13533               bxe_max_aggregation_size);
13534         bxe_max_aggregation_size = TPA_AGG_SIZE;
13535     }
13536 
13537     if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13538         BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13539         bxe_mrrs = -1;
13540     }
13541 
13542     if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13543         BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13544         bxe_autogreeen = 0;
13545     }
13546 
13547     if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13548         BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13549         bxe_udp_rss = 0;
13550     }
13551 
13552     /* pull in user settings */
13553 
13554     sc->interrupt_mode       = bxe_interrupt_mode;
13555     sc->max_rx_bufs          = bxe_max_rx_bufs;
13556     sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13557     sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13558     sc->max_aggregation_size = bxe_max_aggregation_size;
13559     sc->mrrs                 = bxe_mrrs;
13560     sc->autogreeen           = bxe_autogreeen;
13561     sc->udp_rss              = bxe_udp_rss;
13562 
13563     if (bxe_interrupt_mode == INTR_MODE_INTX) {
13564         sc->num_queues = 1;
13565     } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13566         sc->num_queues =
13567             min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13568                 MAX_RSS_CHAINS);
13569         if (sc->num_queues > mp_ncpus) {
13570             sc->num_queues = mp_ncpus;
13571         }
13572     }
13573 
13574     BLOGD(sc, DBG_LOAD,
13575           "User Config: "
13576           "debug=0x%lx "
13577           "interrupt_mode=%d "
13578           "queue_count=%d "
13579           "hc_rx_ticks=%d "
13580           "hc_tx_ticks=%d "
13581           "rx_budget=%d "
13582           "max_aggregation_size=%d "
13583           "mrrs=%d "
13584           "autogreeen=%d "
13585           "udp_rss=%d\n",
13586           bxe_debug,
13587           sc->interrupt_mode,
13588           sc->num_queues,
13589           sc->hc_rx_ticks,
13590           sc->hc_tx_ticks,
13591           bxe_rx_budget,
13592           sc->max_aggregation_size,
13593           sc->mrrs,
13594           sc->autogreeen,
13595           sc->udp_rss);
13596 }
13597 
13598 static void
13599 bxe_media_detect(struct bxe_softc *sc)
13600 {
13601     uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13602     switch (sc->link_params.phy[phy_idx].media_type) {
13603     case ELINK_ETH_PHY_SFPP_10G_FIBER:
13604     case ELINK_ETH_PHY_XFP_FIBER:
13605         BLOGI(sc, "Found 10Gb Fiber media.\n");
13606         sc->media = IFM_10G_SR;
13607         break;
13608     case ELINK_ETH_PHY_SFP_1G_FIBER:
13609         BLOGI(sc, "Found 1Gb Fiber media.\n");
13610         sc->media = IFM_1000_SX;
13611         break;
13612     case ELINK_ETH_PHY_KR:
13613     case ELINK_ETH_PHY_CX4:
13614         BLOGI(sc, "Found 10GBase-CX4 media.\n");
13615         sc->media = IFM_10G_CX4;
13616         break;
13617     case ELINK_ETH_PHY_DA_TWINAX:
13618         BLOGI(sc, "Found 10Gb Twinax media.\n");
13619         sc->media = IFM_10G_TWINAX;
13620         break;
13621     case ELINK_ETH_PHY_BASE_T:
13622         if (sc->link_params.speed_cap_mask[0] &
13623             PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13624             BLOGI(sc, "Found 10GBase-T media.\n");
13625             sc->media = IFM_10G_T;
13626         } else {
13627             BLOGI(sc, "Found 1000Base-T media.\n");
13628             sc->media = IFM_1000_T;
13629         }
13630         break;
13631     case ELINK_ETH_PHY_NOT_PRESENT:
13632         BLOGI(sc, "Media not present.\n");
13633         sc->media = 0;
13634         break;
13635     case ELINK_ETH_PHY_UNSPECIFIED:
13636     default:
13637         BLOGI(sc, "Unknown media!\n");
13638         sc->media = 0;
13639         break;
13640     }
13641 }
13642 
13643 #define GET_FIELD(value, fname)                     \
13644     (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13645 #define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13646 #define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
13647 
13648 static int
13649 bxe_get_igu_cam_info(struct bxe_softc *sc)
13650 {
13651     int pfid = SC_FUNC(sc);
13652     int igu_sb_id;
13653     uint32_t val;
13654     uint8_t fid, igu_sb_cnt = 0;
13655 
13656     sc->igu_base_sb = 0xff;
13657 
13658     if (CHIP_INT_MODE_IS_BC(sc)) {
13659         int vn = SC_VN(sc);
13660         igu_sb_cnt = sc->igu_sb_cnt;
13661         sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
13662                            FP_SB_MAX_E1x);
13663         sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
13664                           (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
13665         return (0);
13666     }
13667 
13668     /* IGU in normal mode - read CAM */
13669     for (igu_sb_id = 0;
13670          igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
13671          igu_sb_id++) {
13672         val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
13673         if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
13674             continue;
13675         }
13676         fid = IGU_FID(val);
13677         if ((fid & IGU_FID_ENCODE_IS_PF)) {
13678             if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
13679                 continue;
13680             }
13681             if (IGU_VEC(val) == 0) {
13682                 /* default status block */
13683                 sc->igu_dsb_id = igu_sb_id;
13684             } else {
13685                 if (sc->igu_base_sb == 0xff) {
13686                     sc->igu_base_sb = igu_sb_id;
13687                 }
13688                 igu_sb_cnt++;
13689             }
13690         }
13691     }
13692 
13693     /*
13694      * Due to new PF resource allocation by MFW T7.4 and above, it's optional
13695      * that number of CAM entries will not be equal to the value advertised in
13696      * PCI. Driver should use the minimal value of both as the actual status
13697      * block count
13698      */
13699     sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
13700 
13701     if (igu_sb_cnt == 0) {
13702         BLOGE(sc, "CAM configuration error\n");
13703         return (-1);
13704     }
13705 
13706     return (0);
13707 }
13708 
13709 /*
13710  * Gather various information from the device config space, the device itself,
13711  * shmem, and the user input.
13712  */
13713 static int
13714 bxe_get_device_info(struct bxe_softc *sc)
13715 {
13716     uint32_t val;
13717     int rc;
13718 
13719     /* Get the data for the device */
13720     sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
13721     sc->devinfo.device_id    = pci_get_device(sc->dev);
13722     sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
13723     sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
13724 
13725     /* get the chip revision (chip metal comes from pci config space) */
13726     sc->devinfo.chip_id     =
13727     sc->link_params.chip_id =
13728         (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
13729          ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
13730          (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
13731          ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
13732 
13733     /* force 57811 according to MISC register */
13734     if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
13735         if (CHIP_IS_57810(sc)) {
13736             sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
13737                                    (sc->devinfo.chip_id & 0x0000ffff));
13738         } else if (CHIP_IS_57810_MF(sc)) {
13739             sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
13740                                    (sc->devinfo.chip_id & 0x0000ffff));
13741         }
13742         sc->devinfo.chip_id |= 0x1;
13743     }
13744 
13745     BLOGD(sc, DBG_LOAD,
13746           "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
13747           sc->devinfo.chip_id,
13748           ((sc->devinfo.chip_id >> 16) & 0xffff),
13749           ((sc->devinfo.chip_id >> 12) & 0xf),
13750           ((sc->devinfo.chip_id >>  4) & 0xff),
13751           ((sc->devinfo.chip_id >>  0) & 0xf));
13752 
13753     val = (REG_RD(sc, 0x2874) & 0x55);
13754     if ((sc->devinfo.chip_id & 0x1) ||
13755         (CHIP_IS_E1(sc) && val) ||
13756         (CHIP_IS_E1H(sc) && (val == 0x55))) {
13757         sc->flags |= BXE_ONE_PORT_FLAG;
13758         BLOGD(sc, DBG_LOAD, "single port device\n");
13759     }
13760 
13761     /* set the doorbell size */
13762     sc->doorbell_size = (1 << BXE_DB_SHIFT);
13763 
13764     /* determine whether the device is in 2 port or 4 port mode */
13765     sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
13766     if (CHIP_IS_E2E3(sc)) {
13767         /*
13768          * Read port4mode_en_ovwr[0]:
13769          *   If 1, four port mode is in port4mode_en_ovwr[1].
13770          *   If 0, four port mode is in port4mode_en[0].
13771          */
13772         val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
13773         if (val & 1) {
13774             val = ((val >> 1) & 1);
13775         } else {
13776             val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
13777         }
13778 
13779         sc->devinfo.chip_port_mode =
13780             (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
13781 
13782         BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
13783     }
13784 
13785     /* get the function and path info for the device */
13786     bxe_get_function_num(sc);
13787 
13788     /* get the shared memory base address */
13789     sc->devinfo.shmem_base     =
13790     sc->link_params.shmem_base =
13791         REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
13792     sc->devinfo.shmem2_base =
13793         REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
13794                                   MISC_REG_GENERIC_CR_0));
13795 
13796     BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
13797           sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
13798 
13799     if (!sc->devinfo.shmem_base) {
13800         /* this should ONLY prevent upcoming shmem reads */
13801         BLOGI(sc, "MCP not active\n");
13802         sc->flags |= BXE_NO_MCP_FLAG;
13803         return (0);
13804     }
13805 
13806     /* make sure the shared memory contents are valid */
13807     val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
13808     if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
13809         (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
13810         BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
13811         return (0);
13812     }
13813     BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
13814 
13815     /* get the bootcode version */
13816     sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
13817     snprintf(sc->devinfo.bc_ver_str,
13818              sizeof(sc->devinfo.bc_ver_str),
13819              "%d.%d.%d",
13820              ((sc->devinfo.bc_ver >> 24) & 0xff),
13821              ((sc->devinfo.bc_ver >> 16) & 0xff),
13822              ((sc->devinfo.bc_ver >>  8) & 0xff));
13823     BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
13824 
13825     /* get the bootcode shmem address */
13826     sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
13827     BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
13828 
13829     /* clean indirect addresses as they're not used */
13830     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
13831     if (IS_PF(sc)) {
13832         REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
13833         REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
13834         REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
13835         REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
13836         if (CHIP_IS_E1x(sc)) {
13837             REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
13838             REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
13839             REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
13840             REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
13841         }
13842 
13843         /*
13844          * Enable internal target-read (in case we are probed after PF
13845          * FLR). Must be done prior to any BAR read access. Only for
13846          * 57712 and up
13847          */
13848         if (!CHIP_IS_E1x(sc)) {
13849             REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
13850         }
13851     }
13852 
13853     /* get the nvram size */
13854     val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
13855     sc->devinfo.flash_size =
13856         (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
13857     BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
13858 
13859     /* get PCI capabilites */
13860     bxe_probe_pci_caps(sc);
13861 
13862     bxe_set_power_state(sc, PCI_PM_D0);
13863 
13864     /* get various configuration parameters from shmem */
13865     bxe_get_shmem_info(sc);
13866 
13867     if (sc->devinfo.pcie_msix_cap_reg != 0) {
13868         val = pci_read_config(sc->dev,
13869                               (sc->devinfo.pcie_msix_cap_reg +
13870                                PCIR_MSIX_CTRL),
13871                               2);
13872         sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
13873     } else {
13874         sc->igu_sb_cnt = 1;
13875     }
13876 
13877     sc->igu_base_addr = BAR_IGU_INTMEM;
13878 
13879     /* initialize IGU parameters */
13880     if (CHIP_IS_E1x(sc)) {
13881         sc->devinfo.int_block = INT_BLOCK_HC;
13882         sc->igu_dsb_id = DEF_SB_IGU_ID;
13883         sc->igu_base_sb = 0;
13884     } else {
13885         sc->devinfo.int_block = INT_BLOCK_IGU;
13886 
13887         /* do not allow device reset during IGU info preocessing */
13888         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13889 
13890         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
13891 
13892         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13893             int tout = 5000;
13894 
13895             BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
13896 
13897             val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
13898             REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
13899             REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
13900 
13901             while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13902                 tout--;
13903                 DELAY(1000);
13904             }
13905 
13906             if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13907                 BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
13908                 bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13909                 return (-1);
13910             }
13911         }
13912 
13913         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13914             BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
13915             sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
13916         } else {
13917             BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
13918         }
13919 
13920         rc = bxe_get_igu_cam_info(sc);
13921 
13922         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13923 
13924         if (rc) {
13925             return (rc);
13926         }
13927     }
13928 
13929     /*
13930      * Get base FW non-default (fast path) status block ID. This value is
13931      * used to initialize the fw_sb_id saved on the fp/queue structure to
13932      * determine the id used by the FW.
13933      */
13934     if (CHIP_IS_E1x(sc)) {
13935         sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
13936     } else {
13937         /*
13938          * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
13939          * the same queue are indicated on the same IGU SB). So we prefer
13940          * FW and IGU SBs to be the same value.
13941          */
13942         sc->base_fw_ndsb = sc->igu_base_sb;
13943     }
13944 
13945     BLOGD(sc, DBG_LOAD,
13946           "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
13947           sc->igu_dsb_id, sc->igu_base_sb,
13948           sc->igu_sb_cnt, sc->base_fw_ndsb);
13949 
13950     elink_phy_probe(&sc->link_params);
13951 
13952     return (0);
13953 }
13954 
13955 static void
13956 bxe_link_settings_supported(struct bxe_softc *sc,
13957                             uint32_t         switch_cfg)
13958 {
13959     uint32_t cfg_size = 0;
13960     uint32_t idx;
13961     uint8_t port = SC_PORT(sc);
13962 
13963     /* aggregation of supported attributes of all external phys */
13964     sc->port.supported[0] = 0;
13965     sc->port.supported[1] = 0;
13966 
13967     switch (sc->link_params.num_phys) {
13968     case 1:
13969         sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
13970         cfg_size = 1;
13971         break;
13972     case 2:
13973         sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
13974         cfg_size = 1;
13975         break;
13976     case 3:
13977         if (sc->link_params.multi_phy_config &
13978             PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
13979             sc->port.supported[1] =
13980                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
13981             sc->port.supported[0] =
13982                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
13983         } else {
13984             sc->port.supported[0] =
13985                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
13986             sc->port.supported[1] =
13987                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
13988         }
13989         cfg_size = 2;
13990         break;
13991     }
13992 
13993     if (!(sc->port.supported[0] || sc->port.supported[1])) {
13994         BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
13995               SHMEM_RD(sc,
13996                        dev_info.port_hw_config[port].external_phy_config),
13997               SHMEM_RD(sc,
13998                        dev_info.port_hw_config[port].external_phy_config2));
13999         return;
14000     }
14001 
14002     if (CHIP_IS_E3(sc))
14003         sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
14004     else {
14005         switch (switch_cfg) {
14006         case ELINK_SWITCH_CFG_1G:
14007             sc->port.phy_addr =
14008                 REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
14009             break;
14010         case ELINK_SWITCH_CFG_10G:
14011             sc->port.phy_addr =
14012                 REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
14013             break;
14014         default:
14015             BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14016                   sc->port.link_config[0]);
14017             return;
14018         }
14019     }
14020 
14021     BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14022 
14023     /* mask what we support according to speed_cap_mask per configuration */
14024     for (idx = 0; idx < cfg_size; idx++) {
14025         if (!(sc->link_params.speed_cap_mask[idx] &
14026               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14027             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14028         }
14029 
14030         if (!(sc->link_params.speed_cap_mask[idx] &
14031               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14032             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14033         }
14034 
14035         if (!(sc->link_params.speed_cap_mask[idx] &
14036               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14037             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14038         }
14039 
14040         if (!(sc->link_params.speed_cap_mask[idx] &
14041               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14042             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14043         }
14044 
14045         if (!(sc->link_params.speed_cap_mask[idx] &
14046               PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14047             sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14048         }
14049 
14050         if (!(sc->link_params.speed_cap_mask[idx] &
14051               PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14052             sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14053         }
14054 
14055         if (!(sc->link_params.speed_cap_mask[idx] &
14056               PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14057             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14058         }
14059 
14060         if (!(sc->link_params.speed_cap_mask[idx] &
14061               PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14062             sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14063         }
14064     }
14065 
14066     BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14067           sc->port.supported[0], sc->port.supported[1]);
14068 }
14069 
14070 static void
14071 bxe_link_settings_requested(struct bxe_softc *sc)
14072 {
14073     uint32_t link_config;
14074     uint32_t idx;
14075     uint32_t cfg_size = 0;
14076 
14077     sc->port.advertising[0] = 0;
14078     sc->port.advertising[1] = 0;
14079 
14080     switch (sc->link_params.num_phys) {
14081     case 1:
14082     case 2:
14083         cfg_size = 1;
14084         break;
14085     case 3:
14086         cfg_size = 2;
14087         break;
14088     }
14089 
14090     for (idx = 0; idx < cfg_size; idx++) {
14091         sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14092         link_config = sc->port.link_config[idx];
14093 
14094         switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14095         case PORT_FEATURE_LINK_SPEED_AUTO:
14096             if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14097                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14098                 sc->port.advertising[idx] |= sc->port.supported[idx];
14099                 if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14100                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14101                     sc->port.advertising[idx] |=
14102                         (ELINK_SUPPORTED_100baseT_Half |
14103                          ELINK_SUPPORTED_100baseT_Full);
14104             } else {
14105                 /* force 10G, no AN */
14106                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14107                 sc->port.advertising[idx] |=
14108                     (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14109                 continue;
14110             }
14111             break;
14112 
14113         case PORT_FEATURE_LINK_SPEED_10M_FULL:
14114             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14115                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14116                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14117                                               ADVERTISED_TP);
14118             } else {
14119                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14120                           "speed_cap_mask=0x%08x\n",
14121                       link_config, sc->link_params.speed_cap_mask[idx]);
14122                 return;
14123             }
14124             break;
14125 
14126         case PORT_FEATURE_LINK_SPEED_10M_HALF:
14127             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14128                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14129                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14130                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14131                                               ADVERTISED_TP);
14132             } else {
14133                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14134                           "speed_cap_mask=0x%08x\n",
14135                       link_config, sc->link_params.speed_cap_mask[idx]);
14136                 return;
14137             }
14138             break;
14139 
14140         case PORT_FEATURE_LINK_SPEED_100M_FULL:
14141             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14142                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14143                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14144                                               ADVERTISED_TP);
14145             } else {
14146                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14147                           "speed_cap_mask=0x%08x\n",
14148                       link_config, sc->link_params.speed_cap_mask[idx]);
14149                 return;
14150             }
14151             break;
14152 
14153         case PORT_FEATURE_LINK_SPEED_100M_HALF:
14154             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14155                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14156                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14157                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14158                                               ADVERTISED_TP);
14159             } else {
14160                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14161                           "speed_cap_mask=0x%08x\n",
14162                       link_config, sc->link_params.speed_cap_mask[idx]);
14163                 return;
14164             }
14165             break;
14166 
14167         case PORT_FEATURE_LINK_SPEED_1G:
14168             if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14169                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14170                 sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14171                                               ADVERTISED_TP);
14172             } else {
14173                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14174                           "speed_cap_mask=0x%08x\n",
14175                       link_config, sc->link_params.speed_cap_mask[idx]);
14176                 return;
14177             }
14178             break;
14179 
14180         case PORT_FEATURE_LINK_SPEED_2_5G:
14181             if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14182                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14183                 sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14184                                               ADVERTISED_TP);
14185             } else {
14186                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14187                           "speed_cap_mask=0x%08x\n",
14188                       link_config, sc->link_params.speed_cap_mask[idx]);
14189                 return;
14190             }
14191             break;
14192 
14193         case PORT_FEATURE_LINK_SPEED_10G_CX4:
14194             if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14195                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14196                 sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14197                                               ADVERTISED_FIBRE);
14198             } else {
14199                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14200                           "speed_cap_mask=0x%08x\n",
14201                       link_config, sc->link_params.speed_cap_mask[idx]);
14202                 return;
14203             }
14204             break;
14205 
14206         case PORT_FEATURE_LINK_SPEED_20G:
14207             sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14208             break;
14209 
14210         default:
14211             BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14212                       "speed_cap_mask=0x%08x\n",
14213                   link_config, sc->link_params.speed_cap_mask[idx]);
14214             sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14215             sc->port.advertising[idx] = sc->port.supported[idx];
14216             break;
14217         }
14218 
14219         sc->link_params.req_flow_ctrl[idx] =
14220             (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14221 
14222         if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14223             if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14224                 sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14225             } else {
14226                 bxe_set_requested_fc(sc);
14227             }
14228         }
14229 
14230         BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14231                             "req_flow_ctrl=0x%x advertising=0x%x\n",
14232               sc->link_params.req_line_speed[idx],
14233               sc->link_params.req_duplex[idx],
14234               sc->link_params.req_flow_ctrl[idx],
14235               sc->port.advertising[idx]);
14236     }
14237 }
14238 
14239 static void
14240 bxe_get_phy_info(struct bxe_softc *sc)
14241 {
14242     uint8_t port = SC_PORT(sc);
14243     uint32_t config = sc->port.config;
14244     uint32_t eee_mode;
14245 
14246     /* shmem data already read in bxe_get_shmem_info() */
14247 
14248     BLOGD(sc, DBG_LOAD, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14249                         "link_config0=0x%08x\n",
14250                sc->link_params.lane_config,
14251                sc->link_params.speed_cap_mask[0],
14252                sc->port.link_config[0]);
14253 
14254     bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14255     bxe_link_settings_requested(sc);
14256 
14257     if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14258         sc->link_params.feature_config_flags |=
14259             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14260     } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14261         sc->link_params.feature_config_flags &=
14262             ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14263     } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14264         sc->link_params.feature_config_flags |=
14265             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14266     }
14267 
14268     /* configure link feature according to nvram value */
14269     eee_mode =
14270         (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14271           PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14272          PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14273     if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14274         sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14275                                     ELINK_EEE_MODE_ENABLE_LPI |
14276                                     ELINK_EEE_MODE_OUTPUT_TIME);
14277     } else {
14278         sc->link_params.eee_mode = 0;
14279     }
14280 
14281     /* get the media type */
14282     bxe_media_detect(sc);
14283 }
14284 
14285 static void
14286 bxe_get_params(struct bxe_softc *sc)
14287 {
14288     /* get user tunable params */
14289     bxe_get_tunable_params(sc);
14290 
14291     /* select the RX and TX ring sizes */
14292     sc->tx_ring_size = TX_BD_USABLE;
14293     sc->rx_ring_size = RX_BD_USABLE;
14294 
14295     /* XXX disable WoL */
14296     sc->wol = 0;
14297 }
14298 
14299 static void
14300 bxe_set_modes_bitmap(struct bxe_softc *sc)
14301 {
14302     uint32_t flags = 0;
14303 
14304     if (CHIP_REV_IS_FPGA(sc)) {
14305         SET_FLAGS(flags, MODE_FPGA);
14306     } else if (CHIP_REV_IS_EMUL(sc)) {
14307         SET_FLAGS(flags, MODE_EMUL);
14308     } else {
14309         SET_FLAGS(flags, MODE_ASIC);
14310     }
14311 
14312     if (CHIP_IS_MODE_4_PORT(sc)) {
14313         SET_FLAGS(flags, MODE_PORT4);
14314     } else {
14315         SET_FLAGS(flags, MODE_PORT2);
14316     }
14317 
14318     if (CHIP_IS_E2(sc)) {
14319         SET_FLAGS(flags, MODE_E2);
14320     } else if (CHIP_IS_E3(sc)) {
14321         SET_FLAGS(flags, MODE_E3);
14322         if (CHIP_REV(sc) == CHIP_REV_Ax) {
14323             SET_FLAGS(flags, MODE_E3_A0);
14324         } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14325             SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14326         }
14327     }
14328 
14329     if (IS_MF(sc)) {
14330         SET_FLAGS(flags, MODE_MF);
14331         switch (sc->devinfo.mf_info.mf_mode) {
14332         case MULTI_FUNCTION_SD:
14333             SET_FLAGS(flags, MODE_MF_SD);
14334             break;
14335         case MULTI_FUNCTION_SI:
14336             SET_FLAGS(flags, MODE_MF_SI);
14337             break;
14338         case MULTI_FUNCTION_AFEX:
14339             SET_FLAGS(flags, MODE_MF_AFEX);
14340             break;
14341         }
14342     } else {
14343         SET_FLAGS(flags, MODE_SF);
14344     }
14345 
14346 #if defined(__LITTLE_ENDIAN)
14347     SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14348 #else /* __BIG_ENDIAN */
14349     SET_FLAGS(flags, MODE_BIG_ENDIAN);
14350 #endif
14351 
14352     INIT_MODE_FLAGS(sc) = flags;
14353 }
14354 
14355 static int
14356 bxe_alloc_hsi_mem(struct bxe_softc *sc)
14357 {
14358     struct bxe_fastpath *fp;
14359     bus_addr_t busaddr;
14360     int max_agg_queues;
14361     int max_segments;
14362     bus_size_t max_size;
14363     bus_size_t max_seg_size;
14364     char buf[32];
14365     int rc;
14366     int i, j;
14367 
14368     /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14369 
14370     /* allocate the parent bus DMA tag */
14371     rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14372                             1,                        /* alignment */
14373                             0,                        /* boundary limit */
14374                             BUS_SPACE_MAXADDR,        /* restricted low */
14375                             BUS_SPACE_MAXADDR,        /* restricted hi */
14376                             NULL,                     /* addr filter() */
14377                             NULL,                     /* addr filter() arg */
14378                             BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14379                             BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14380                             BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14381                             0,                        /* flags */
14382                             NULL,                     /* lock() */
14383                             NULL,                     /* lock() arg */
14384                             &sc->parent_dma_tag);     /* returned dma tag */
14385     if (rc != 0) {
14386         BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14387         return (1);
14388     }
14389 
14390     /************************/
14391     /* DEFAULT STATUS BLOCK */
14392     /************************/
14393 
14394     if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14395                       &sc->def_sb_dma, "default status block") != 0) {
14396         /* XXX */
14397         bus_dma_tag_destroy(sc->parent_dma_tag);
14398         return (1);
14399     }
14400 
14401     sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14402 
14403     /***************/
14404     /* EVENT QUEUE */
14405     /***************/
14406 
14407     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14408                       &sc->eq_dma, "event queue") != 0) {
14409         /* XXX */
14410         bxe_dma_free(sc, &sc->def_sb_dma);
14411         sc->def_sb = NULL;
14412         bus_dma_tag_destroy(sc->parent_dma_tag);
14413         return (1);
14414     }
14415 
14416     sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14417 
14418     /*************/
14419     /* SLOW PATH */
14420     /*************/
14421 
14422     if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14423                       &sc->sp_dma, "slow path") != 0) {
14424         /* XXX */
14425         bxe_dma_free(sc, &sc->eq_dma);
14426         sc->eq = NULL;
14427         bxe_dma_free(sc, &sc->def_sb_dma);
14428         sc->def_sb = NULL;
14429         bus_dma_tag_destroy(sc->parent_dma_tag);
14430         return (1);
14431     }
14432 
14433     sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14434 
14435     /*******************/
14436     /* SLOW PATH QUEUE */
14437     /*******************/
14438 
14439     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14440                       &sc->spq_dma, "slow path queue") != 0) {
14441         /* XXX */
14442         bxe_dma_free(sc, &sc->sp_dma);
14443         sc->sp = NULL;
14444         bxe_dma_free(sc, &sc->eq_dma);
14445         sc->eq = NULL;
14446         bxe_dma_free(sc, &sc->def_sb_dma);
14447         sc->def_sb = NULL;
14448         bus_dma_tag_destroy(sc->parent_dma_tag);
14449         return (1);
14450     }
14451 
14452     sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14453 
14454     /***************************/
14455     /* FW DECOMPRESSION BUFFER */
14456     /***************************/
14457 
14458     if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14459                       "fw decompression buffer") != 0) {
14460         /* XXX */
14461         bxe_dma_free(sc, &sc->spq_dma);
14462         sc->spq = NULL;
14463         bxe_dma_free(sc, &sc->sp_dma);
14464         sc->sp = NULL;
14465         bxe_dma_free(sc, &sc->eq_dma);
14466         sc->eq = NULL;
14467         bxe_dma_free(sc, &sc->def_sb_dma);
14468         sc->def_sb = NULL;
14469         bus_dma_tag_destroy(sc->parent_dma_tag);
14470         return (1);
14471     }
14472 
14473     sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14474 
14475     if ((sc->gz_strm =
14476          malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14477         /* XXX */
14478         bxe_dma_free(sc, &sc->gz_buf_dma);
14479         sc->gz_buf = NULL;
14480         bxe_dma_free(sc, &sc->spq_dma);
14481         sc->spq = NULL;
14482         bxe_dma_free(sc, &sc->sp_dma);
14483         sc->sp = NULL;
14484         bxe_dma_free(sc, &sc->eq_dma);
14485         sc->eq = NULL;
14486         bxe_dma_free(sc, &sc->def_sb_dma);
14487         sc->def_sb = NULL;
14488         bus_dma_tag_destroy(sc->parent_dma_tag);
14489         return (1);
14490     }
14491 
14492     /*************/
14493     /* FASTPATHS */
14494     /*************/
14495 
14496     /* allocate DMA memory for each fastpath structure */
14497     for (i = 0; i < sc->num_queues; i++) {
14498         fp = &sc->fp[i];
14499         fp->sc    = sc;
14500         fp->index = i;
14501 
14502         /*******************/
14503         /* FP STATUS BLOCK */
14504         /*******************/
14505 
14506         snprintf(buf, sizeof(buf), "fp %d status block", i);
14507         if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14508                           &fp->sb_dma, buf) != 0) {
14509             /* XXX unwind and free previous fastpath allocations */
14510             BLOGE(sc, "Failed to alloc %s\n", buf);
14511             return (1);
14512         } else {
14513             if (CHIP_IS_E2E3(sc)) {
14514                 fp->status_block.e2_sb =
14515                     (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14516             } else {
14517                 fp->status_block.e1x_sb =
14518                     (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14519             }
14520         }
14521 
14522         /******************/
14523         /* FP TX BD CHAIN */
14524         /******************/
14525 
14526         snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14527         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14528                           &fp->tx_dma, buf) != 0) {
14529             /* XXX unwind and free previous fastpath allocations */
14530             BLOGE(sc, "Failed to alloc %s\n", buf);
14531             return (1);
14532         } else {
14533             fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14534         }
14535 
14536         /* link together the tx bd chain pages */
14537         for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14538             /* index into the tx bd chain array to last entry per page */
14539             struct eth_tx_next_bd *tx_next_bd =
14540                 &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14541             /* point to the next page and wrap from last page */
14542             busaddr = (fp->tx_dma.paddr +
14543                        (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14544             tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14545             tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14546         }
14547 
14548         /******************/
14549         /* FP RX BD CHAIN */
14550         /******************/
14551 
14552         snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14553         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14554                           &fp->rx_dma, buf) != 0) {
14555             /* XXX unwind and free previous fastpath allocations */
14556             BLOGE(sc, "Failed to alloc %s\n", buf);
14557             return (1);
14558         } else {
14559             fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14560         }
14561 
14562         /* link together the rx bd chain pages */
14563         for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14564             /* index into the rx bd chain array to last entry per page */
14565             struct eth_rx_bd *rx_bd =
14566                 &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14567             /* point to the next page and wrap from last page */
14568             busaddr = (fp->rx_dma.paddr +
14569                        (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14570             rx_bd->addr_hi = htole32(U64_HI(busaddr));
14571             rx_bd->addr_lo = htole32(U64_LO(busaddr));
14572         }
14573 
14574         /*******************/
14575         /* FP RX RCQ CHAIN */
14576         /*******************/
14577 
14578         snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14579         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14580                           &fp->rcq_dma, buf) != 0) {
14581             /* XXX unwind and free previous fastpath allocations */
14582             BLOGE(sc, "Failed to alloc %s\n", buf);
14583             return (1);
14584         } else {
14585             fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14586         }
14587 
14588         /* link together the rcq chain pages */
14589         for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14590             /* index into the rcq chain array to last entry per page */
14591             struct eth_rx_cqe_next_page *rx_cqe_next =
14592                 (struct eth_rx_cqe_next_page *)
14593                 &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14594             /* point to the next page and wrap from last page */
14595             busaddr = (fp->rcq_dma.paddr +
14596                        (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14597             rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14598             rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14599         }
14600 
14601         /*******************/
14602         /* FP RX SGE CHAIN */
14603         /*******************/
14604 
14605         snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14606         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14607                           &fp->rx_sge_dma, buf) != 0) {
14608             /* XXX unwind and free previous fastpath allocations */
14609             BLOGE(sc, "Failed to alloc %s\n", buf);
14610             return (1);
14611         } else {
14612             fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14613         }
14614 
14615         /* link together the sge chain pages */
14616         for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14617             /* index into the rcq chain array to last entry per page */
14618             struct eth_rx_sge *rx_sge =
14619                 &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14620             /* point to the next page and wrap from last page */
14621             busaddr = (fp->rx_sge_dma.paddr +
14622                        (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14623             rx_sge->addr_hi = htole32(U64_HI(busaddr));
14624             rx_sge->addr_lo = htole32(U64_LO(busaddr));
14625         }
14626 
14627         /***********************/
14628         /* FP TX MBUF DMA MAPS */
14629         /***********************/
14630 
14631         /* set required sizes before mapping to conserve resources */
14632         if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
14633             max_size     = BXE_TSO_MAX_SIZE;
14634             max_segments = BXE_TSO_MAX_SEGMENTS;
14635             max_seg_size = BXE_TSO_MAX_SEG_SIZE;
14636         } else {
14637             max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
14638             max_segments = BXE_MAX_SEGMENTS;
14639             max_seg_size = MCLBYTES;
14640         }
14641 
14642         /* create a dma tag for the tx mbufs */
14643         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14644                                 1,                  /* alignment */
14645                                 0,                  /* boundary limit */
14646                                 BUS_SPACE_MAXADDR,  /* restricted low */
14647                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14648                                 NULL,               /* addr filter() */
14649                                 NULL,               /* addr filter() arg */
14650                                 max_size,           /* max map size */
14651                                 max_segments,       /* num discontinuous */
14652                                 max_seg_size,       /* max seg size */
14653                                 0,                  /* flags */
14654                                 NULL,               /* lock() */
14655                                 NULL,               /* lock() arg */
14656                                 &fp->tx_mbuf_tag);  /* returned dma tag */
14657         if (rc != 0) {
14658             /* XXX unwind and free previous fastpath allocations */
14659             BLOGE(sc, "Failed to create dma tag for "
14660                       "'fp %d tx mbufs' (%d)\n", i, rc);
14661             return (1);
14662         }
14663 
14664         /* create dma maps for each of the tx mbuf clusters */
14665         for (j = 0; j < TX_BD_TOTAL; j++) {
14666             if (bus_dmamap_create(fp->tx_mbuf_tag,
14667                                   BUS_DMA_NOWAIT,
14668                                   &fp->tx_mbuf_chain[j].m_map)) {
14669                 /* XXX unwind and free previous fastpath allocations */
14670                 BLOGE(sc, "Failed to create dma map for "
14671                           "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
14672                 return (1);
14673             }
14674         }
14675 
14676         /***********************/
14677         /* FP RX MBUF DMA MAPS */
14678         /***********************/
14679 
14680         /* create a dma tag for the rx mbufs */
14681         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14682                                 1,                  /* alignment */
14683                                 0,                  /* boundary limit */
14684                                 BUS_SPACE_MAXADDR,  /* restricted low */
14685                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14686                                 NULL,               /* addr filter() */
14687                                 NULL,               /* addr filter() arg */
14688                                 MJUM9BYTES,         /* max map size */
14689                                 1,                  /* num discontinuous */
14690                                 MJUM9BYTES,         /* max seg size */
14691                                 0,                  /* flags */
14692                                 NULL,               /* lock() */
14693                                 NULL,               /* lock() arg */
14694                                 &fp->rx_mbuf_tag);  /* returned dma tag */
14695         if (rc != 0) {
14696             /* XXX unwind and free previous fastpath allocations */
14697             BLOGE(sc, "Failed to create dma tag for "
14698                       "'fp %d rx mbufs' (%d)\n", i, rc);
14699             return (1);
14700         }
14701 
14702         /* create dma maps for each of the rx mbuf clusters */
14703         for (j = 0; j < RX_BD_TOTAL; j++) {
14704             if (bus_dmamap_create(fp->rx_mbuf_tag,
14705                                   BUS_DMA_NOWAIT,
14706                                   &fp->rx_mbuf_chain[j].m_map)) {
14707                 /* XXX unwind and free previous fastpath allocations */
14708                 BLOGE(sc, "Failed to create dma map for "
14709                           "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
14710                 return (1);
14711             }
14712         }
14713 
14714         /* create dma map for the spare rx mbuf cluster */
14715         if (bus_dmamap_create(fp->rx_mbuf_tag,
14716                               BUS_DMA_NOWAIT,
14717                               &fp->rx_mbuf_spare_map)) {
14718             /* XXX unwind and free previous fastpath allocations */
14719             BLOGE(sc, "Failed to create dma map for "
14720                       "'fp %d spare rx mbuf' (%d)\n", i, rc);
14721             return (1);
14722         }
14723 
14724         /***************************/
14725         /* FP RX SGE MBUF DMA MAPS */
14726         /***************************/
14727 
14728         /* create a dma tag for the rx sge mbufs */
14729         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14730                                 1,                  /* alignment */
14731                                 0,                  /* boundary limit */
14732                                 BUS_SPACE_MAXADDR,  /* restricted low */
14733                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14734                                 NULL,               /* addr filter() */
14735                                 NULL,               /* addr filter() arg */
14736                                 BCM_PAGE_SIZE,      /* max map size */
14737                                 1,                  /* num discontinuous */
14738                                 BCM_PAGE_SIZE,      /* max seg size */
14739                                 0,                  /* flags */
14740                                 NULL,               /* lock() */
14741                                 NULL,               /* lock() arg */
14742                                 &fp->rx_sge_mbuf_tag); /* returned dma tag */
14743         if (rc != 0) {
14744             /* XXX unwind and free previous fastpath allocations */
14745             BLOGE(sc, "Failed to create dma tag for "
14746                       "'fp %d rx sge mbufs' (%d)\n", i, rc);
14747             return (1);
14748         }
14749 
14750         /* create dma maps for the rx sge mbuf clusters */
14751         for (j = 0; j < RX_SGE_TOTAL; j++) {
14752             if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14753                                   BUS_DMA_NOWAIT,
14754                                   &fp->rx_sge_mbuf_chain[j].m_map)) {
14755                 /* XXX unwind and free previous fastpath allocations */
14756                 BLOGE(sc, "Failed to create dma map for "
14757                           "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
14758                 return (1);
14759             }
14760         }
14761 
14762         /* create dma map for the spare rx sge mbuf cluster */
14763         if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14764                               BUS_DMA_NOWAIT,
14765                               &fp->rx_sge_mbuf_spare_map)) {
14766             /* XXX unwind and free previous fastpath allocations */
14767             BLOGE(sc, "Failed to create dma map for "
14768                       "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
14769             return (1);
14770         }
14771 
14772         /***************************/
14773         /* FP RX TPA MBUF DMA MAPS */
14774         /***************************/
14775 
14776         /* create dma maps for the rx tpa mbuf clusters */
14777         max_agg_queues = MAX_AGG_QS(sc);
14778 
14779         for (j = 0; j < max_agg_queues; j++) {
14780             if (bus_dmamap_create(fp->rx_mbuf_tag,
14781                                   BUS_DMA_NOWAIT,
14782                                   &fp->rx_tpa_info[j].bd.m_map)) {
14783                 /* XXX unwind and free previous fastpath allocations */
14784                 BLOGE(sc, "Failed to create dma map for "
14785                           "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
14786                 return (1);
14787             }
14788         }
14789 
14790         /* create dma map for the spare rx tpa mbuf cluster */
14791         if (bus_dmamap_create(fp->rx_mbuf_tag,
14792                               BUS_DMA_NOWAIT,
14793                               &fp->rx_tpa_info_mbuf_spare_map)) {
14794             /* XXX unwind and free previous fastpath allocations */
14795             BLOGE(sc, "Failed to create dma map for "
14796                       "'fp %d spare rx tpa mbuf' (%d)\n", i, rc);
14797             return (1);
14798         }
14799 
14800         bxe_init_sge_ring_bit_mask(fp);
14801     }
14802 
14803     return (0);
14804 }
14805 
14806 static void
14807 bxe_free_hsi_mem(struct bxe_softc *sc)
14808 {
14809     struct bxe_fastpath *fp;
14810     int max_agg_queues;
14811     int i, j;
14812 
14813     if (sc->parent_dma_tag == NULL) {
14814         return; /* assume nothing was allocated */
14815     }
14816 
14817     for (i = 0; i < sc->num_queues; i++) {
14818         fp = &sc->fp[i];
14819 
14820         /*******************/
14821         /* FP STATUS BLOCK */
14822         /*******************/
14823 
14824         bxe_dma_free(sc, &fp->sb_dma);
14825         memset(&fp->status_block, 0, sizeof(fp->status_block));
14826 
14827         /******************/
14828         /* FP TX BD CHAIN */
14829         /******************/
14830 
14831         bxe_dma_free(sc, &fp->tx_dma);
14832         fp->tx_chain = NULL;
14833 
14834         /******************/
14835         /* FP RX BD CHAIN */
14836         /******************/
14837 
14838         bxe_dma_free(sc, &fp->rx_dma);
14839         fp->rx_chain = NULL;
14840 
14841         /*******************/
14842         /* FP RX RCQ CHAIN */
14843         /*******************/
14844 
14845         bxe_dma_free(sc, &fp->rcq_dma);
14846         fp->rcq_chain = NULL;
14847 
14848         /*******************/
14849         /* FP RX SGE CHAIN */
14850         /*******************/
14851 
14852         bxe_dma_free(sc, &fp->rx_sge_dma);
14853         fp->rx_sge_chain = NULL;
14854 
14855         /***********************/
14856         /* FP TX MBUF DMA MAPS */
14857         /***********************/
14858 
14859         if (fp->tx_mbuf_tag != NULL) {
14860             for (j = 0; j < TX_BD_TOTAL; j++) {
14861                 if (fp->tx_mbuf_chain[j].m_map != NULL) {
14862                     bus_dmamap_unload(fp->tx_mbuf_tag,
14863                                       fp->tx_mbuf_chain[j].m_map);
14864                     bus_dmamap_destroy(fp->tx_mbuf_tag,
14865                                        fp->tx_mbuf_chain[j].m_map);
14866                 }
14867             }
14868 
14869             bus_dma_tag_destroy(fp->tx_mbuf_tag);
14870             fp->tx_mbuf_tag = NULL;
14871         }
14872 
14873         /***********************/
14874         /* FP RX MBUF DMA MAPS */
14875         /***********************/
14876 
14877         if (fp->rx_mbuf_tag != NULL) {
14878             for (j = 0; j < RX_BD_TOTAL; j++) {
14879                 if (fp->rx_mbuf_chain[j].m_map != NULL) {
14880                     bus_dmamap_unload(fp->rx_mbuf_tag,
14881                                       fp->rx_mbuf_chain[j].m_map);
14882                     bus_dmamap_destroy(fp->rx_mbuf_tag,
14883                                        fp->rx_mbuf_chain[j].m_map);
14884                 }
14885             }
14886 
14887             if (fp->rx_mbuf_spare_map != NULL) {
14888                 bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14889                 bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14890             }
14891 
14892             /***************************/
14893             /* FP RX TPA MBUF DMA MAPS */
14894             /***************************/
14895 
14896             max_agg_queues = MAX_AGG_QS(sc);
14897 
14898             for (j = 0; j < max_agg_queues; j++) {
14899                 if (fp->rx_tpa_info[j].bd.m_map != NULL) {
14900                     bus_dmamap_unload(fp->rx_mbuf_tag,
14901                                       fp->rx_tpa_info[j].bd.m_map);
14902                     bus_dmamap_destroy(fp->rx_mbuf_tag,
14903                                        fp->rx_tpa_info[j].bd.m_map);
14904                 }
14905             }
14906 
14907             if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
14908                 bus_dmamap_unload(fp->rx_mbuf_tag,
14909                                   fp->rx_tpa_info_mbuf_spare_map);
14910                 bus_dmamap_destroy(fp->rx_mbuf_tag,
14911                                    fp->rx_tpa_info_mbuf_spare_map);
14912             }
14913 
14914             bus_dma_tag_destroy(fp->rx_mbuf_tag);
14915             fp->rx_mbuf_tag = NULL;
14916         }
14917 
14918         /***************************/
14919         /* FP RX SGE MBUF DMA MAPS */
14920         /***************************/
14921 
14922         if (fp->rx_sge_mbuf_tag != NULL) {
14923             for (j = 0; j < RX_SGE_TOTAL; j++) {
14924                 if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
14925                     bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14926                                       fp->rx_sge_mbuf_chain[j].m_map);
14927                     bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14928                                        fp->rx_sge_mbuf_chain[j].m_map);
14929                 }
14930             }
14931 
14932             if (fp->rx_sge_mbuf_spare_map != NULL) {
14933                 bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14934                                   fp->rx_sge_mbuf_spare_map);
14935                 bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14936                                    fp->rx_sge_mbuf_spare_map);
14937             }
14938 
14939             bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
14940             fp->rx_sge_mbuf_tag = NULL;
14941         }
14942     }
14943 
14944     /***************************/
14945     /* FW DECOMPRESSION BUFFER */
14946     /***************************/
14947 
14948     bxe_dma_free(sc, &sc->gz_buf_dma);
14949     sc->gz_buf = NULL;
14950     free(sc->gz_strm, M_DEVBUF);
14951     sc->gz_strm = NULL;
14952 
14953     /*******************/
14954     /* SLOW PATH QUEUE */
14955     /*******************/
14956 
14957     bxe_dma_free(sc, &sc->spq_dma);
14958     sc->spq = NULL;
14959 
14960     /*************/
14961     /* SLOW PATH */
14962     /*************/
14963 
14964     bxe_dma_free(sc, &sc->sp_dma);
14965     sc->sp = NULL;
14966 
14967     /***************/
14968     /* EVENT QUEUE */
14969     /***************/
14970 
14971     bxe_dma_free(sc, &sc->eq_dma);
14972     sc->eq = NULL;
14973 
14974     /************************/
14975     /* DEFAULT STATUS BLOCK */
14976     /************************/
14977 
14978     bxe_dma_free(sc, &sc->def_sb_dma);
14979     sc->def_sb = NULL;
14980 
14981     bus_dma_tag_destroy(sc->parent_dma_tag);
14982     sc->parent_dma_tag = NULL;
14983 }
14984 
14985 /*
14986  * Previous driver DMAE transaction may have occurred when pre-boot stage
14987  * ended and boot began. This would invalidate the addresses of the
14988  * transaction, resulting in was-error bit set in the PCI causing all
14989  * hw-to-host PCIe transactions to timeout. If this happened we want to clear
14990  * the interrupt which detected this from the pglueb and the was-done bit
14991  */
14992 static void
14993 bxe_prev_interrupted_dmae(struct bxe_softc *sc)
14994 {
14995     uint32_t val;
14996 
14997     if (!CHIP_IS_E1x(sc)) {
14998         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
14999         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
15000             BLOGD(sc, DBG_LOAD,
15001                   "Clearing 'was-error' bit that was set in pglueb");
15002             REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
15003         }
15004     }
15005 }
15006 
15007 static int
15008 bxe_prev_mcp_done(struct bxe_softc *sc)
15009 {
15010     uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15011                                  DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15012     if (!rc) {
15013         BLOGE(sc, "MCP response failure, aborting\n");
15014         return (-1);
15015     }
15016 
15017     return (0);
15018 }
15019 
15020 static struct bxe_prev_list_node *
15021 bxe_prev_path_get_entry(struct bxe_softc *sc)
15022 {
15023     struct bxe_prev_list_node *tmp;
15024 
15025     LIST_FOREACH(tmp, &bxe_prev_list, node) {
15026         if ((sc->pcie_bus == tmp->bus) &&
15027             (sc->pcie_device == tmp->slot) &&
15028             (SC_PATH(sc) == tmp->path)) {
15029             return (tmp);
15030         }
15031     }
15032 
15033     return (NULL);
15034 }
15035 
15036 static uint8_t
15037 bxe_prev_is_path_marked(struct bxe_softc *sc)
15038 {
15039     struct bxe_prev_list_node *tmp;
15040     int rc = FALSE;
15041 
15042     mtx_lock(&bxe_prev_mtx);
15043 
15044     tmp = bxe_prev_path_get_entry(sc);
15045     if (tmp) {
15046         if (tmp->aer) {
15047             BLOGD(sc, DBG_LOAD,
15048                   "Path %d/%d/%d was marked by AER\n",
15049                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15050         } else {
15051             rc = TRUE;
15052             BLOGD(sc, DBG_LOAD,
15053                   "Path %d/%d/%d was already cleaned from previous drivers\n",
15054                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15055         }
15056     }
15057 
15058     mtx_unlock(&bxe_prev_mtx);
15059 
15060     return (rc);
15061 }
15062 
15063 static int
15064 bxe_prev_mark_path(struct bxe_softc *sc,
15065                    uint8_t          after_undi)
15066 {
15067     struct bxe_prev_list_node *tmp;
15068 
15069     mtx_lock(&bxe_prev_mtx);
15070 
15071     /* Check whether the entry for this path already exists */
15072     tmp = bxe_prev_path_get_entry(sc);
15073     if (tmp) {
15074         if (!tmp->aer) {
15075             BLOGD(sc, DBG_LOAD,
15076                   "Re-marking AER in path %d/%d/%d\n",
15077                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15078         } else {
15079             BLOGD(sc, DBG_LOAD,
15080                   "Removing AER indication from path %d/%d/%d\n",
15081                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15082             tmp->aer = 0;
15083         }
15084 
15085         mtx_unlock(&bxe_prev_mtx);
15086         return (0);
15087     }
15088 
15089     mtx_unlock(&bxe_prev_mtx);
15090 
15091     /* Create an entry for this path and add it */
15092     tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15093                  (M_NOWAIT | M_ZERO));
15094     if (!tmp) {
15095         BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15096         return (-1);
15097     }
15098 
15099     tmp->bus  = sc->pcie_bus;
15100     tmp->slot = sc->pcie_device;
15101     tmp->path = SC_PATH(sc);
15102     tmp->aer  = 0;
15103     tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15104 
15105     mtx_lock(&bxe_prev_mtx);
15106 
15107     BLOGD(sc, DBG_LOAD,
15108           "Marked path %d/%d/%d - finished previous unload\n",
15109           sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15110     LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15111 
15112     mtx_unlock(&bxe_prev_mtx);
15113 
15114     return (0);
15115 }
15116 
15117 static int
15118 bxe_do_flr(struct bxe_softc *sc)
15119 {
15120     int i;
15121 
15122     /* only E2 and onwards support FLR */
15123     if (CHIP_IS_E1x(sc)) {
15124         BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15125         return (-1);
15126     }
15127 
15128     /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15129     if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15130         BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15131               sc->devinfo.bc_ver);
15132         return (-1);
15133     }
15134 
15135     /* Wait for Transaction Pending bit clean */
15136     for (i = 0; i < 4; i++) {
15137         if (i) {
15138             DELAY(((1 << (i - 1)) * 100) * 1000);
15139         }
15140 
15141         if (!bxe_is_pcie_pending(sc)) {
15142             goto clear;
15143         }
15144     }
15145 
15146     BLOGE(sc, "PCIE transaction is not cleared, "
15147               "proceeding with reset anyway\n");
15148 
15149 clear:
15150 
15151     BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15152     bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15153 
15154     return (0);
15155 }
15156 
15157 struct bxe_mac_vals {
15158     uint32_t xmac_addr;
15159     uint32_t xmac_val;
15160     uint32_t emac_addr;
15161     uint32_t emac_val;
15162     uint32_t umac_addr;
15163     uint32_t umac_val;
15164     uint32_t bmac_addr;
15165     uint32_t bmac_val[2];
15166 };
15167 
15168 static void
15169 bxe_prev_unload_close_mac(struct bxe_softc *sc,
15170                           struct bxe_mac_vals *vals)
15171 {
15172     uint32_t val, base_addr, offset, mask, reset_reg;
15173     uint8_t mac_stopped = FALSE;
15174     uint8_t port = SC_PORT(sc);
15175     uint32_t wb_data[2];
15176 
15177     /* reset addresses as they also mark which values were changed */
15178     vals->bmac_addr = 0;
15179     vals->umac_addr = 0;
15180     vals->xmac_addr = 0;
15181     vals->emac_addr = 0;
15182 
15183     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15184 
15185     if (!CHIP_IS_E3(sc)) {
15186         val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15187         mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15188         if ((mask & reset_reg) && val) {
15189             BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15190             base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15191                                     : NIG_REG_INGRESS_BMAC0_MEM;
15192             offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15193                                     : BIGMAC_REGISTER_BMAC_CONTROL;
15194 
15195             /*
15196              * use rd/wr since we cannot use dmae. This is safe
15197              * since MCP won't access the bus due to the request
15198              * to unload, and no function on the path can be
15199              * loaded at this time.
15200              */
15201             wb_data[0] = REG_RD(sc, base_addr + offset);
15202             wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15203             vals->bmac_addr = base_addr + offset;
15204             vals->bmac_val[0] = wb_data[0];
15205             vals->bmac_val[1] = wb_data[1];
15206             wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15207             REG_WR(sc, vals->bmac_addr, wb_data[0]);
15208             REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15209         }
15210 
15211         BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15212         vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15213         vals->emac_val = REG_RD(sc, vals->emac_addr);
15214         REG_WR(sc, vals->emac_addr, 0);
15215         mac_stopped = TRUE;
15216     } else {
15217         if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15218             BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15219             base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15220             val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15221             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15222             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15223             vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15224             vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15225             REG_WR(sc, vals->xmac_addr, 0);
15226             mac_stopped = TRUE;
15227         }
15228 
15229         mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15230         if (mask & reset_reg) {
15231             BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15232             base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15233             vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15234             vals->umac_val = REG_RD(sc, vals->umac_addr);
15235             REG_WR(sc, vals->umac_addr, 0);
15236             mac_stopped = TRUE;
15237         }
15238     }
15239 
15240     if (mac_stopped) {
15241         DELAY(20000);
15242     }
15243 }
15244 
15245 #define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15246 #define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15247 #define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15248 #define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15249 
15250 static void
15251 bxe_prev_unload_undi_inc(struct bxe_softc *sc,
15252                          uint8_t          port,
15253                          uint8_t          inc)
15254 {
15255     uint16_t rcq, bd;
15256     uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15257 
15258     rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15259     bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15260 
15261     tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15262     REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15263 
15264     BLOGD(sc, DBG_LOAD,
15265           "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15266           port, bd, rcq);
15267 }
15268 
15269 static int
15270 bxe_prev_unload_common(struct bxe_softc *sc)
15271 {
15272     uint32_t reset_reg, tmp_reg = 0, rc;
15273     uint8_t prev_undi = FALSE;
15274     struct bxe_mac_vals mac_vals;
15275     uint32_t timer_count = 1000;
15276     uint32_t prev_brb;
15277 
15278     /*
15279      * It is possible a previous function received 'common' answer,
15280      * but hasn't loaded yet, therefore creating a scenario of
15281      * multiple functions receiving 'common' on the same path.
15282      */
15283     BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15284 
15285     memset(&mac_vals, 0, sizeof(mac_vals));
15286 
15287     if (bxe_prev_is_path_marked(sc)) {
15288         return (bxe_prev_mcp_done(sc));
15289     }
15290 
15291     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15292 
15293     /* Reset should be performed after BRB is emptied */
15294     if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15295         /* Close the MAC Rx to prevent BRB from filling up */
15296         bxe_prev_unload_close_mac(sc, &mac_vals);
15297 
15298         /* close LLH filters towards the BRB */
15299         elink_set_rx_filter(&sc->link_params, 0);
15300 
15301         /*
15302          * Check if the UNDI driver was previously loaded.
15303          * UNDI driver initializes CID offset for normal bell to 0x7
15304          */
15305         if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15306             tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15307             if (tmp_reg == 0x7) {
15308                 BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15309                 prev_undi = TRUE;
15310                 /* clear the UNDI indication */
15311                 REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15312                 /* clear possible idle check errors */
15313                 REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15314             }
15315         }
15316 
15317         /* wait until BRB is empty */
15318         tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15319         while (timer_count) {
15320             prev_brb = tmp_reg;
15321 
15322             tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15323             if (!tmp_reg) {
15324                 break;
15325             }
15326 
15327             BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15328 
15329             /* reset timer as long as BRB actually gets emptied */
15330             if (prev_brb > tmp_reg) {
15331                 timer_count = 1000;
15332             } else {
15333                 timer_count--;
15334             }
15335 
15336             /* If UNDI resides in memory, manually increment it */
15337             if (prev_undi) {
15338                 bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15339             }
15340 
15341             DELAY(10);
15342         }
15343 
15344         if (!timer_count) {
15345             BLOGE(sc, "Failed to empty BRB\n");
15346         }
15347     }
15348 
15349     /* No packets are in the pipeline, path is ready for reset */
15350     bxe_reset_common(sc);
15351 
15352     if (mac_vals.xmac_addr) {
15353         REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15354     }
15355     if (mac_vals.umac_addr) {
15356         REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15357     }
15358     if (mac_vals.emac_addr) {
15359         REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15360     }
15361     if (mac_vals.bmac_addr) {
15362         REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15363         REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15364     }
15365 
15366     rc = bxe_prev_mark_path(sc, prev_undi);
15367     if (rc) {
15368         bxe_prev_mcp_done(sc);
15369         return (rc);
15370     }
15371 
15372     return (bxe_prev_mcp_done(sc));
15373 }
15374 
15375 static int
15376 bxe_prev_unload_uncommon(struct bxe_softc *sc)
15377 {
15378     int rc;
15379 
15380     BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15381 
15382     /* Test if previous unload process was already finished for this path */
15383     if (bxe_prev_is_path_marked(sc)) {
15384         return (bxe_prev_mcp_done(sc));
15385     }
15386 
15387     BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15388 
15389     /*
15390      * If function has FLR capabilities, and existing FW version matches
15391      * the one required, then FLR will be sufficient to clean any residue
15392      * left by previous driver
15393      */
15394     rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15395     if (!rc) {
15396         /* fw version is good */
15397         BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15398         rc = bxe_do_flr(sc);
15399     }
15400 
15401     if (!rc) {
15402         /* FLR was performed */
15403         BLOGD(sc, DBG_LOAD, "FLR successful\n");
15404         return (0);
15405     }
15406 
15407     BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15408 
15409     /* Close the MCP request, return failure*/
15410     rc = bxe_prev_mcp_done(sc);
15411     if (!rc) {
15412         rc = BXE_PREV_WAIT_NEEDED;
15413     }
15414 
15415     return (rc);
15416 }
15417 
15418 static int
15419 bxe_prev_unload(struct bxe_softc *sc)
15420 {
15421     int time_counter = 10;
15422     uint32_t fw, hw_lock_reg, hw_lock_val;
15423     uint32_t rc = 0;
15424 
15425     /*
15426      * Clear HW from errors which may have resulted from an interrupted
15427      * DMAE transaction.
15428      */
15429     bxe_prev_interrupted_dmae(sc);
15430 
15431     /* Release previously held locks */
15432     hw_lock_reg =
15433         (SC_FUNC(sc) <= 5) ?
15434             (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15435             (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15436 
15437     hw_lock_val = (REG_RD(sc, hw_lock_reg));
15438     if (hw_lock_val) {
15439         if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15440             BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15441             REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15442                    (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15443         }
15444         BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15445         REG_WR(sc, hw_lock_reg, 0xffffffff);
15446     } else {
15447         BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15448     }
15449 
15450     if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15451         BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15452         REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15453     }
15454 
15455     do {
15456         /* Lock MCP using an unload request */
15457         fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15458         if (!fw) {
15459             BLOGE(sc, "MCP response failure, aborting\n");
15460             rc = -1;
15461             break;
15462         }
15463 
15464         if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15465             rc = bxe_prev_unload_common(sc);
15466             break;
15467         }
15468 
15469         /* non-common reply from MCP night require looping */
15470         rc = bxe_prev_unload_uncommon(sc);
15471         if (rc != BXE_PREV_WAIT_NEEDED) {
15472             break;
15473         }
15474 
15475         DELAY(20000);
15476     } while (--time_counter);
15477 
15478     if (!time_counter || rc) {
15479         BLOGE(sc, "Failed to unload previous driver!"
15480             " time_counter %d rc %d\n", time_counter, rc);
15481         rc = -1;
15482     }
15483 
15484     return (rc);
15485 }
15486 
15487 void
15488 bxe_dcbx_set_state(struct bxe_softc *sc,
15489                    uint8_t          dcb_on,
15490                    uint32_t         dcbx_enabled)
15491 {
15492     if (!CHIP_IS_E1x(sc)) {
15493         sc->dcb_state = dcb_on;
15494         sc->dcbx_enabled = dcbx_enabled;
15495     } else {
15496         sc->dcb_state = FALSE;
15497         sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15498     }
15499     BLOGD(sc, DBG_LOAD,
15500           "DCB state [%s:%s]\n",
15501           dcb_on ? "ON" : "OFF",
15502           (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15503           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15504           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15505           "on-chip with negotiation" : "invalid");
15506 }
15507 
15508 /* must be called after sriov-enable */
15509 static int
15510 bxe_set_qm_cid_count(struct bxe_softc *sc)
15511 {
15512     int cid_count = BXE_L2_MAX_CID(sc);
15513 
15514     if (IS_SRIOV(sc)) {
15515         cid_count += BXE_VF_CIDS;
15516     }
15517 
15518     if (CNIC_SUPPORT(sc)) {
15519         cid_count += CNIC_CID_MAX;
15520     }
15521 
15522     return (roundup(cid_count, QM_CID_ROUND));
15523 }
15524 
15525 static void
15526 bxe_init_multi_cos(struct bxe_softc *sc)
15527 {
15528     int pri, cos;
15529 
15530     uint32_t pri_map = 0; /* XXX change to user config */
15531 
15532     for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15533         cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15534         if (cos < sc->max_cos) {
15535             sc->prio_to_cos[pri] = cos;
15536         } else {
15537             BLOGW(sc, "Invalid COS %d for priority %d "
15538                       "(max COS is %d), setting to 0\n",
15539                   cos, pri, (sc->max_cos - 1));
15540             sc->prio_to_cos[pri] = 0;
15541         }
15542     }
15543 }
15544 
15545 static int
15546 bxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15547 {
15548     struct bxe_softc *sc;
15549     int error, result;
15550 
15551     result = 0;
15552     error = sysctl_handle_int(oidp, &result, 0, req);
15553 
15554     if (error || !req->newptr) {
15555         return (error);
15556     }
15557 
15558     if (result == 1) {
15559         uint32_t  temp;
15560         sc = (struct bxe_softc *)arg1;
15561 
15562         BLOGI(sc, "... dumping driver state ...\n");
15563         temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15564         BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15565     }
15566 
15567     return (error);
15568 }
15569 
15570 static int
15571 bxe_sysctl_trigger_grcdump(SYSCTL_HANDLER_ARGS)
15572 {
15573     struct bxe_softc *sc;
15574     int error, result;
15575 
15576     result = 0;
15577     error = sysctl_handle_int(oidp, &result, 0, req);
15578 
15579     if (error || !req->newptr) {
15580         return (error);
15581     }
15582 
15583     if (result == 1) {
15584         sc = (struct bxe_softc *)arg1;
15585 
15586         BLOGI(sc, "... grcdump start ...\n");
15587         bxe_grc_dump(sc);
15588         BLOGI(sc, "... grcdump done ...\n");
15589     }
15590 
15591     return (error);
15592 }
15593 
15594 static int
15595 bxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15596 {
15597     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15598     uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15599     uint32_t *offset;
15600     uint64_t value = 0;
15601     int index = (int)arg2;
15602 
15603     if (index >= BXE_NUM_ETH_STATS) {
15604         BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15605         return (-1);
15606     }
15607 
15608     offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15609 
15610     switch (bxe_eth_stats_arr[index].size) {
15611     case 4:
15612         value = (uint64_t)*offset;
15613         break;
15614     case 8:
15615         value = HILO_U64(*offset, *(offset + 1));
15616         break;
15617     default:
15618         BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15619               index, bxe_eth_stats_arr[index].size);
15620         return (-1);
15621     }
15622 
15623     return (sysctl_handle_64(oidp, &value, 0, req));
15624 }
15625 
15626 static int
15627 bxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15628 {
15629     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15630     uint32_t *eth_stats;
15631     uint32_t *offset;
15632     uint64_t value = 0;
15633     uint32_t q_stat = (uint32_t)arg2;
15634     uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15635     uint32_t index = (q_stat & 0xffff);
15636 
15637     eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15638 
15639     if (index >= BXE_NUM_ETH_Q_STATS) {
15640         BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15641         return (-1);
15642     }
15643 
15644     offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15645 
15646     switch (bxe_eth_q_stats_arr[index].size) {
15647     case 4:
15648         value = (uint64_t)*offset;
15649         break;
15650     case 8:
15651         value = HILO_U64(*offset, *(offset + 1));
15652         break;
15653     default:
15654         BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15655               index, bxe_eth_q_stats_arr[index].size);
15656         return (-1);
15657     }
15658 
15659     return (sysctl_handle_64(oidp, &value, 0, req));
15660 }
15661 
15662 static void
15663 bxe_add_sysctls(struct bxe_softc *sc)
15664 {
15665     struct sysctl_ctx_list *ctx;
15666     struct sysctl_oid_list *children;
15667     struct sysctl_oid *queue_top, *queue;
15668     struct sysctl_oid_list *queue_top_children, *queue_children;
15669     char queue_num_buf[32];
15670     uint32_t q_stat;
15671     int i, j;
15672 
15673     ctx = device_get_sysctl_ctx(sc->dev);
15674     children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15675 
15676     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
15677                       CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
15678                       "version");
15679 
15680     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15681                       CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
15682                       "bootcode version");
15683 
15684     snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
15685              BCM_5710_FW_MAJOR_VERSION,
15686              BCM_5710_FW_MINOR_VERSION,
15687              BCM_5710_FW_REVISION_VERSION,
15688              BCM_5710_FW_ENGINEERING_VERSION);
15689     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15690                       CTLFLAG_RD, sc->fw_ver_str, 0,
15691                       "firmware version");
15692 
15693     snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
15694         ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
15695          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
15696          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
15697          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
15698                                                                 "Unknown"));
15699     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15700                       CTLFLAG_RD, sc->mf_mode_str, 0,
15701                       "multifunction mode");
15702 
15703     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
15704                     CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
15705                     "multifunction vnics per port");
15706 
15707     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15708                       CTLFLAG_RD, sc->mac_addr_str, 0,
15709                       "mac address");
15710 
15711     snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
15712         ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
15713          (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
15714          (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
15715                                               "???GT/s"),
15716         sc->devinfo.pcie_link_width);
15717     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15718                       CTLFLAG_RD, sc->pci_link_str, 0,
15719                       "pci link status");
15720 
15721     sc->debug = bxe_debug;
15722     SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
15723                     CTLFLAG_RW, &sc->debug,
15724                     "debug logging mode");
15725 
15726     SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "trigger_grcdump",
15727                     CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15728                     bxe_sysctl_trigger_grcdump, "IU",
15729                     "set by driver when a grcdump is needed");
15730 
15731     sc->grcdump_done = 0;
15732     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
15733                    CTLFLAG_RW, &sc->grcdump_done, 0,
15734                    "set by driver when grcdump is done");
15735 
15736     sc->rx_budget = bxe_rx_budget;
15737     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
15738                     CTLFLAG_RW, &sc->rx_budget, 0,
15739                     "rx processing budget");
15740 
15741     SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
15742                     CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15743                     bxe_sysctl_state, "IU", "dump driver state");
15744 
15745     for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
15746         SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
15747                         bxe_eth_stats_arr[i].string,
15748                         CTLTYPE_U64 | CTLFLAG_RD, sc, i,
15749                         bxe_sysctl_eth_stat, "LU",
15750                         bxe_eth_stats_arr[i].string);
15751     }
15752 
15753     /* add a new parent node for all queues "dev.bxe.#.queue" */
15754     queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
15755                                 CTLFLAG_RD, NULL, "queue");
15756     queue_top_children = SYSCTL_CHILDREN(queue_top);
15757 
15758     for (i = 0; i < sc->num_queues; i++) {
15759         /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
15760         snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
15761         queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
15762                                 queue_num_buf, CTLFLAG_RD, NULL,
15763                                 "single queue");
15764         queue_children = SYSCTL_CHILDREN(queue);
15765 
15766         for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
15767             q_stat = ((i << 16) | j);
15768             SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
15769                             bxe_eth_q_stats_arr[j].string,
15770                             CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
15771                             bxe_sysctl_eth_q_stat, "LU",
15772                             bxe_eth_q_stats_arr[j].string);
15773         }
15774     }
15775 }
15776 
15777 static int
15778 bxe_alloc_buf_rings(struct bxe_softc *sc)
15779 {
15780 #if __FreeBSD_version >= 800000
15781 
15782     int i;
15783     struct bxe_fastpath *fp;
15784 
15785     for (i = 0; i < sc->num_queues; i++) {
15786 
15787         fp = &sc->fp[i];
15788 
15789         fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
15790                                    M_NOWAIT, &fp->tx_mtx);
15791         if (fp->tx_br == NULL)
15792             return (-1);
15793     }
15794 #endif
15795     return (0);
15796 }
15797 
15798 static void
15799 bxe_free_buf_rings(struct bxe_softc *sc)
15800 {
15801 #if __FreeBSD_version >= 800000
15802 
15803     int i;
15804     struct bxe_fastpath *fp;
15805 
15806     for (i = 0; i < sc->num_queues; i++) {
15807 
15808         fp = &sc->fp[i];
15809 
15810         if (fp->tx_br) {
15811             buf_ring_free(fp->tx_br, M_DEVBUF);
15812             fp->tx_br = NULL;
15813         }
15814     }
15815 
15816 #endif
15817 }
15818 
15819 static void
15820 bxe_init_fp_mutexs(struct bxe_softc *sc)
15821 {
15822     int i;
15823     struct bxe_fastpath *fp;
15824 
15825     for (i = 0; i < sc->num_queues; i++) {
15826 
15827         fp = &sc->fp[i];
15828 
15829         snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
15830             "bxe%d_fp%d_tx_lock", sc->unit, i);
15831         mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
15832 
15833         snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
15834             "bxe%d_fp%d_rx_lock", sc->unit, i);
15835         mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
15836     }
15837 }
15838 
15839 static void
15840 bxe_destroy_fp_mutexs(struct bxe_softc *sc)
15841 {
15842     int i;
15843     struct bxe_fastpath *fp;
15844 
15845     for (i = 0; i < sc->num_queues; i++) {
15846 
15847         fp = &sc->fp[i];
15848 
15849         if (mtx_initialized(&fp->tx_mtx)) {
15850             mtx_destroy(&fp->tx_mtx);
15851         }
15852 
15853         if (mtx_initialized(&fp->rx_mtx)) {
15854             mtx_destroy(&fp->rx_mtx);
15855         }
15856     }
15857 }
15858 
15859 
15860 /*
15861  * Device attach function.
15862  *
15863  * Allocates device resources, performs secondary chip identification, and
15864  * initializes driver instance variables. This function is called from driver
15865  * load after a successful probe.
15866  *
15867  * Returns:
15868  *   0 = Success, >0 = Failure
15869  */
15870 static int
15871 bxe_attach(device_t dev)
15872 {
15873     struct bxe_softc *sc;
15874 
15875     sc = device_get_softc(dev);
15876 
15877     BLOGD(sc, DBG_LOAD, "Starting attach...\n");
15878 
15879     sc->state = BXE_STATE_CLOSED;
15880 
15881     sc->dev  = dev;
15882     sc->unit = device_get_unit(dev);
15883 
15884     BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
15885 
15886     sc->pcie_bus    = pci_get_bus(dev);
15887     sc->pcie_device = pci_get_slot(dev);
15888     sc->pcie_func   = pci_get_function(dev);
15889 
15890     /* enable bus master capability */
15891     pci_enable_busmaster(dev);
15892 
15893     /* get the BARs */
15894     if (bxe_allocate_bars(sc) != 0) {
15895         return (ENXIO);
15896     }
15897 
15898     /* initialize the mutexes */
15899     bxe_init_mutexes(sc);
15900 
15901     /* prepare the periodic callout */
15902     callout_init(&sc->periodic_callout, 0);
15903 
15904     /* prepare the chip taskqueue */
15905     sc->chip_tq_flags = CHIP_TQ_NONE;
15906     snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
15907              "bxe%d_chip_tq", sc->unit);
15908     TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
15909     sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
15910                                    taskqueue_thread_enqueue,
15911                                    &sc->chip_tq);
15912     taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
15913                             "%s", sc->chip_tq_name);
15914 
15915     /* get device info and set params */
15916     if (bxe_get_device_info(sc) != 0) {
15917         BLOGE(sc, "getting device info\n");
15918         bxe_deallocate_bars(sc);
15919         pci_disable_busmaster(dev);
15920         return (ENXIO);
15921     }
15922 
15923     /* get final misc params */
15924     bxe_get_params(sc);
15925 
15926     /* set the default MTU (changed via ifconfig) */
15927     sc->mtu = ETHERMTU;
15928 
15929     bxe_set_modes_bitmap(sc);
15930 
15931     /* XXX
15932      * If in AFEX mode and the function is configured for FCoE
15933      * then bail... no L2 allowed.
15934      */
15935 
15936     /* get phy settings from shmem and 'and' against admin settings */
15937     bxe_get_phy_info(sc);
15938 
15939     /* initialize the FreeBSD ifnet interface */
15940     if (bxe_init_ifnet(sc) != 0) {
15941         bxe_release_mutexes(sc);
15942         bxe_deallocate_bars(sc);
15943         pci_disable_busmaster(dev);
15944         return (ENXIO);
15945     }
15946 
15947     if (bxe_add_cdev(sc) != 0) {
15948         if (sc->ifp != NULL) {
15949             ether_ifdetach(sc->ifp);
15950         }
15951         ifmedia_removeall(&sc->ifmedia);
15952         bxe_release_mutexes(sc);
15953         bxe_deallocate_bars(sc);
15954         pci_disable_busmaster(dev);
15955         return (ENXIO);
15956     }
15957 
15958     /* allocate device interrupts */
15959     if (bxe_interrupt_alloc(sc) != 0) {
15960         bxe_del_cdev(sc);
15961         if (sc->ifp != NULL) {
15962             ether_ifdetach(sc->ifp);
15963         }
15964         ifmedia_removeall(&sc->ifmedia);
15965         bxe_release_mutexes(sc);
15966         bxe_deallocate_bars(sc);
15967         pci_disable_busmaster(dev);
15968         return (ENXIO);
15969     }
15970 
15971     bxe_init_fp_mutexs(sc);
15972 
15973     if (bxe_alloc_buf_rings(sc) != 0) {
15974 	bxe_free_buf_rings(sc);
15975         bxe_interrupt_free(sc);
15976         bxe_del_cdev(sc);
15977         if (sc->ifp != NULL) {
15978             ether_ifdetach(sc->ifp);
15979         }
15980         ifmedia_removeall(&sc->ifmedia);
15981         bxe_release_mutexes(sc);
15982         bxe_deallocate_bars(sc);
15983         pci_disable_busmaster(dev);
15984         return (ENXIO);
15985     }
15986 
15987     /* allocate ilt */
15988     if (bxe_alloc_ilt_mem(sc) != 0) {
15989 	bxe_free_buf_rings(sc);
15990         bxe_interrupt_free(sc);
15991         bxe_del_cdev(sc);
15992         if (sc->ifp != NULL) {
15993             ether_ifdetach(sc->ifp);
15994         }
15995         ifmedia_removeall(&sc->ifmedia);
15996         bxe_release_mutexes(sc);
15997         bxe_deallocate_bars(sc);
15998         pci_disable_busmaster(dev);
15999         return (ENXIO);
16000     }
16001 
16002     /* allocate the host hardware/software hsi structures */
16003     if (bxe_alloc_hsi_mem(sc) != 0) {
16004         bxe_free_ilt_mem(sc);
16005 	bxe_free_buf_rings(sc);
16006         bxe_interrupt_free(sc);
16007         bxe_del_cdev(sc);
16008         if (sc->ifp != NULL) {
16009             ether_ifdetach(sc->ifp);
16010         }
16011         ifmedia_removeall(&sc->ifmedia);
16012         bxe_release_mutexes(sc);
16013         bxe_deallocate_bars(sc);
16014         pci_disable_busmaster(dev);
16015         return (ENXIO);
16016     }
16017 
16018     /* need to reset chip if UNDI was active */
16019     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
16020         /* init fw_seq */
16021         sc->fw_seq =
16022             (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
16023              DRV_MSG_SEQ_NUMBER_MASK);
16024         BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
16025         bxe_prev_unload(sc);
16026     }
16027 
16028 #if 1
16029     /* XXX */
16030     bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16031 #else
16032     if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
16033         SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
16034         SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
16035         SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
16036         bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
16037         bxe_dcbx_init_params(sc);
16038     } else {
16039         bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16040     }
16041 #endif
16042 
16043     /* calculate qm_cid_count */
16044     sc->qm_cid_count = bxe_set_qm_cid_count(sc);
16045     BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
16046 
16047     sc->max_cos = 1;
16048     bxe_init_multi_cos(sc);
16049 
16050     bxe_add_sysctls(sc);
16051 
16052     return (0);
16053 }
16054 
16055 /*
16056  * Device detach function.
16057  *
16058  * Stops the controller, resets the controller, and releases resources.
16059  *
16060  * Returns:
16061  *   0 = Success, >0 = Failure
16062  */
16063 static int
16064 bxe_detach(device_t dev)
16065 {
16066     struct bxe_softc *sc;
16067     if_t ifp;
16068 
16069     sc = device_get_softc(dev);
16070 
16071     BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16072 
16073     ifp = sc->ifp;
16074     if (ifp != NULL && if_vlantrunkinuse(ifp)) {
16075         BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16076         return(EBUSY);
16077     }
16078 
16079     bxe_del_cdev(sc);
16080 
16081     /* stop the periodic callout */
16082     bxe_periodic_stop(sc);
16083 
16084     /* stop the chip taskqueue */
16085     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16086     if (sc->chip_tq) {
16087         taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16088         taskqueue_free(sc->chip_tq);
16089         sc->chip_tq = NULL;
16090     }
16091 
16092     /* stop and reset the controller if it was open */
16093     if (sc->state != BXE_STATE_CLOSED) {
16094         BXE_CORE_LOCK(sc);
16095         bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16096         BXE_CORE_UNLOCK(sc);
16097     }
16098 
16099     /* release the network interface */
16100     if (ifp != NULL) {
16101         ether_ifdetach(ifp);
16102     }
16103     ifmedia_removeall(&sc->ifmedia);
16104 
16105     /* XXX do the following based on driver state... */
16106 
16107     /* free the host hardware/software hsi structures */
16108     bxe_free_hsi_mem(sc);
16109 
16110     /* free ilt */
16111     bxe_free_ilt_mem(sc);
16112 
16113     bxe_free_buf_rings(sc);
16114 
16115     /* release the interrupts */
16116     bxe_interrupt_free(sc);
16117 
16118     /* Release the mutexes*/
16119     bxe_destroy_fp_mutexs(sc);
16120     bxe_release_mutexes(sc);
16121 
16122 
16123     /* Release the PCIe BAR mapped memory */
16124     bxe_deallocate_bars(sc);
16125 
16126     /* Release the FreeBSD interface. */
16127     if (sc->ifp != NULL) {
16128         if_free(sc->ifp);
16129     }
16130 
16131     pci_disable_busmaster(dev);
16132 
16133     return (0);
16134 }
16135 
16136 /*
16137  * Device shutdown function.
16138  *
16139  * Stops and resets the controller.
16140  *
16141  * Returns:
16142  *   Nothing
16143  */
16144 static int
16145 bxe_shutdown(device_t dev)
16146 {
16147     struct bxe_softc *sc;
16148 
16149     sc = device_get_softc(dev);
16150 
16151     BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16152 
16153     /* stop the periodic callout */
16154     bxe_periodic_stop(sc);
16155 
16156     BXE_CORE_LOCK(sc);
16157     bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16158     BXE_CORE_UNLOCK(sc);
16159 
16160     return (0);
16161 }
16162 
16163 void
16164 bxe_igu_ack_sb(struct bxe_softc *sc,
16165                uint8_t          igu_sb_id,
16166                uint8_t          segment,
16167                uint16_t         index,
16168                uint8_t          op,
16169                uint8_t          update)
16170 {
16171     uint32_t igu_addr = sc->igu_base_addr;
16172     igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16173     bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16174 }
16175 
16176 static void
16177 bxe_igu_clear_sb_gen(struct bxe_softc *sc,
16178                      uint8_t          func,
16179                      uint8_t          idu_sb_id,
16180                      uint8_t          is_pf)
16181 {
16182     uint32_t data, ctl, cnt = 100;
16183     uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16184     uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16185     uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16186     uint32_t sb_bit =  1 << (idu_sb_id%32);
16187     uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16188     uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16189 
16190     /* Not supported in BC mode */
16191     if (CHIP_INT_MODE_IS_BC(sc)) {
16192         return;
16193     }
16194 
16195     data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16196              IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16197             IGU_REGULAR_CLEANUP_SET |
16198             IGU_REGULAR_BCLEANUP);
16199 
16200     ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16201            (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16202            (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16203 
16204     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16205             data, igu_addr_data);
16206     REG_WR(sc, igu_addr_data, data);
16207 
16208     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16209                       BUS_SPACE_BARRIER_WRITE);
16210     mb();
16211 
16212     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16213             ctl, igu_addr_ctl);
16214     REG_WR(sc, igu_addr_ctl, ctl);
16215 
16216     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16217                       BUS_SPACE_BARRIER_WRITE);
16218     mb();
16219 
16220     /* wait for clean up to finish */
16221     while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16222         DELAY(20000);
16223     }
16224 
16225     if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16226         BLOGD(sc, DBG_LOAD,
16227               "Unable to finish IGU cleanup: "
16228               "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16229               idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16230     }
16231 }
16232 
16233 static void
16234 bxe_igu_clear_sb(struct bxe_softc *sc,
16235                  uint8_t          idu_sb_id)
16236 {
16237     bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16238 }
16239 
16240 
16241 
16242 
16243 
16244 
16245 
16246 /*******************/
16247 /* ECORE CALLBACKS */
16248 /*******************/
16249 
16250 static void
16251 bxe_reset_common(struct bxe_softc *sc)
16252 {
16253     uint32_t val = 0x1400;
16254 
16255     /* reset_common */
16256     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16257 
16258     if (CHIP_IS_E3(sc)) {
16259         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16260         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16261     }
16262 
16263     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16264 }
16265 
16266 static void
16267 bxe_common_init_phy(struct bxe_softc *sc)
16268 {
16269     uint32_t shmem_base[2];
16270     uint32_t shmem2_base[2];
16271 
16272     /* Avoid common init in case MFW supports LFA */
16273     if (SHMEM2_RD(sc, size) >
16274         (uint32_t)offsetof(struct shmem2_region,
16275                            lfa_host_addr[SC_PORT(sc)])) {
16276         return;
16277     }
16278 
16279     shmem_base[0]  = sc->devinfo.shmem_base;
16280     shmem2_base[0] = sc->devinfo.shmem2_base;
16281 
16282     if (!CHIP_IS_E1x(sc)) {
16283         shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16284         shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16285     }
16286 
16287     bxe_acquire_phy_lock(sc);
16288     elink_common_init_phy(sc, shmem_base, shmem2_base,
16289                           sc->devinfo.chip_id, 0);
16290     bxe_release_phy_lock(sc);
16291 }
16292 
16293 static void
16294 bxe_pf_disable(struct bxe_softc *sc)
16295 {
16296     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16297 
16298     val &= ~IGU_PF_CONF_FUNC_EN;
16299 
16300     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16301     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16302     REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16303 }
16304 
16305 static void
16306 bxe_init_pxp(struct bxe_softc *sc)
16307 {
16308     uint16_t devctl;
16309     int r_order, w_order;
16310 
16311     devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16312 
16313     BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16314 
16315     w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16316 
16317     if (sc->mrrs == -1) {
16318         r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16319     } else {
16320         BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16321         r_order = sc->mrrs;
16322     }
16323 
16324     ecore_init_pxp_arb(sc, r_order, w_order);
16325 }
16326 
16327 static uint32_t
16328 bxe_get_pretend_reg(struct bxe_softc *sc)
16329 {
16330     uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16331     uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16332     return (base + (SC_ABS_FUNC(sc)) * stride);
16333 }
16334 
16335 /*
16336  * Called only on E1H or E2.
16337  * When pretending to be PF, the pretend value is the function number 0..7.
16338  * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16339  * combination.
16340  */
16341 static int
16342 bxe_pretend_func(struct bxe_softc *sc,
16343                  uint16_t         pretend_func_val)
16344 {
16345     uint32_t pretend_reg;
16346 
16347     if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16348         return (-1);
16349     }
16350 
16351     /* get my own pretend register */
16352     pretend_reg = bxe_get_pretend_reg(sc);
16353     REG_WR(sc, pretend_reg, pretend_func_val);
16354     REG_RD(sc, pretend_reg);
16355     return (0);
16356 }
16357 
16358 static void
16359 bxe_iov_init_dmae(struct bxe_softc *sc)
16360 {
16361     return;
16362 }
16363 
16364 static void
16365 bxe_iov_init_dq(struct bxe_softc *sc)
16366 {
16367     return;
16368 }
16369 
16370 /* send a NIG loopback debug packet */
16371 static void
16372 bxe_lb_pckt(struct bxe_softc *sc)
16373 {
16374     uint32_t wb_write[3];
16375 
16376     /* Ethernet source and destination addresses */
16377     wb_write[0] = 0x55555555;
16378     wb_write[1] = 0x55555555;
16379     wb_write[2] = 0x20;     /* SOP */
16380     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16381 
16382     /* NON-IP protocol */
16383     wb_write[0] = 0x09000000;
16384     wb_write[1] = 0x55555555;
16385     wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16386     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16387 }
16388 
16389 /*
16390  * Some of the internal memories are not directly readable from the driver.
16391  * To test them we send debug packets.
16392  */
16393 static int
16394 bxe_int_mem_test(struct bxe_softc *sc)
16395 {
16396     int factor;
16397     int count, i;
16398     uint32_t val = 0;
16399 
16400     if (CHIP_REV_IS_FPGA(sc)) {
16401         factor = 120;
16402     } else if (CHIP_REV_IS_EMUL(sc)) {
16403         factor = 200;
16404     } else {
16405         factor = 1;
16406     }
16407 
16408     /* disable inputs of parser neighbor blocks */
16409     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16410     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16411     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16412     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16413 
16414     /*  write 0 to parser credits for CFC search request */
16415     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16416 
16417     /* send Ethernet packet */
16418     bxe_lb_pckt(sc);
16419 
16420     /* TODO do i reset NIG statistic? */
16421     /* Wait until NIG register shows 1 packet of size 0x10 */
16422     count = 1000 * factor;
16423     while (count) {
16424         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16425         val = *BXE_SP(sc, wb_data[0]);
16426         if (val == 0x10) {
16427             break;
16428         }
16429 
16430         DELAY(10000);
16431         count--;
16432     }
16433 
16434     if (val != 0x10) {
16435         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16436         return (-1);
16437     }
16438 
16439     /* wait until PRS register shows 1 packet */
16440     count = (1000 * factor);
16441     while (count) {
16442         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16443         if (val == 1) {
16444             break;
16445         }
16446 
16447         DELAY(10000);
16448         count--;
16449     }
16450 
16451     if (val != 0x1) {
16452         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16453         return (-2);
16454     }
16455 
16456     /* Reset and init BRB, PRS */
16457     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16458     DELAY(50000);
16459     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16460     DELAY(50000);
16461     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16462     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16463 
16464     /* Disable inputs of parser neighbor blocks */
16465     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16466     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16467     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16468     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16469 
16470     /* Write 0 to parser credits for CFC search request */
16471     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16472 
16473     /* send 10 Ethernet packets */
16474     for (i = 0; i < 10; i++) {
16475         bxe_lb_pckt(sc);
16476     }
16477 
16478     /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16479     count = (1000 * factor);
16480     while (count) {
16481         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16482         val = *BXE_SP(sc, wb_data[0]);
16483         if (val == 0xb0) {
16484             break;
16485         }
16486 
16487         DELAY(10000);
16488         count--;
16489     }
16490 
16491     if (val != 0xb0) {
16492         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16493         return (-3);
16494     }
16495 
16496     /* Wait until PRS register shows 2 packets */
16497     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16498     if (val != 2) {
16499         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16500     }
16501 
16502     /* Write 1 to parser credits for CFC search request */
16503     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16504 
16505     /* Wait until PRS register shows 3 packets */
16506     DELAY(10000 * factor);
16507 
16508     /* Wait until NIG register shows 1 packet of size 0x10 */
16509     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16510     if (val != 3) {
16511         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16512     }
16513 
16514     /* clear NIG EOP FIFO */
16515     for (i = 0; i < 11; i++) {
16516         REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16517     }
16518 
16519     val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16520     if (val != 1) {
16521         BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16522         return (-4);
16523     }
16524 
16525     /* Reset and init BRB, PRS, NIG */
16526     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16527     DELAY(50000);
16528     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16529     DELAY(50000);
16530     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16531     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16532     if (!CNIC_SUPPORT(sc)) {
16533         /* set NIC mode */
16534         REG_WR(sc, PRS_REG_NIC_MODE, 1);
16535     }
16536 
16537     /* Enable inputs of parser neighbor blocks */
16538     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16539     REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16540     REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16541     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16542 
16543     return (0);
16544 }
16545 
16546 static void
16547 bxe_setup_fan_failure_detection(struct bxe_softc *sc)
16548 {
16549     int is_required;
16550     uint32_t val;
16551     int port;
16552 
16553     is_required = 0;
16554     val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
16555            SHARED_HW_CFG_FAN_FAILURE_MASK);
16556 
16557     if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
16558         is_required = 1;
16559     }
16560     /*
16561      * The fan failure mechanism is usually related to the PHY type since
16562      * the power consumption of the board is affected by the PHY. Currently,
16563      * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
16564      */
16565     else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
16566         for (port = PORT_0; port < PORT_MAX; port++) {
16567             is_required |= elink_fan_failure_det_req(sc,
16568                                                      sc->devinfo.shmem_base,
16569                                                      sc->devinfo.shmem2_base,
16570                                                      port);
16571         }
16572     }
16573 
16574     BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
16575 
16576     if (is_required == 0) {
16577         return;
16578     }
16579 
16580     /* Fan failure is indicated by SPIO 5 */
16581     bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
16582 
16583     /* set to active low mode */
16584     val = REG_RD(sc, MISC_REG_SPIO_INT);
16585     val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
16586     REG_WR(sc, MISC_REG_SPIO_INT, val);
16587 
16588     /* enable interrupt to signal the IGU */
16589     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
16590     val |= MISC_SPIO_SPIO5;
16591     REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
16592 }
16593 
16594 static void
16595 bxe_enable_blocks_attention(struct bxe_softc *sc)
16596 {
16597     uint32_t val;
16598 
16599     REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16600     if (!CHIP_IS_E1x(sc)) {
16601         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
16602     } else {
16603         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
16604     }
16605     REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16606     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16607     /*
16608      * mask read length error interrupts in brb for parser
16609      * (parsing unit and 'checksum and crc' unit)
16610      * these errors are legal (PU reads fixed length and CAC can cause
16611      * read length error on truncated packets)
16612      */
16613     REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
16614     REG_WR(sc, QM_REG_QM_INT_MASK, 0);
16615     REG_WR(sc, TM_REG_TM_INT_MASK, 0);
16616     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
16617     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
16618     REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
16619 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
16620 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
16621     REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
16622     REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
16623     REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
16624 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
16625 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
16626     REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
16627     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
16628     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
16629     REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
16630 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
16631 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
16632 
16633     val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
16634            PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
16635            PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
16636     if (!CHIP_IS_E1x(sc)) {
16637         val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
16638                 PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
16639     }
16640     REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
16641 
16642     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
16643     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
16644     REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
16645 /*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
16646 
16647     if (!CHIP_IS_E1x(sc)) {
16648         /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
16649         REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
16650     }
16651 
16652     REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
16653     REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
16654 /*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
16655     REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
16656 }
16657 
16658 /**
16659  * bxe_init_hw_common - initialize the HW at the COMMON phase.
16660  *
16661  * @sc:     driver handle
16662  */
16663 static int
16664 bxe_init_hw_common(struct bxe_softc *sc)
16665 {
16666     uint8_t abs_func_id;
16667     uint32_t val;
16668 
16669     BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
16670           SC_ABS_FUNC(sc));
16671 
16672     /*
16673      * take the RESET lock to protect undi_unload flow from accessing
16674      * registers while we are resetting the chip
16675      */
16676     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16677 
16678     bxe_reset_common(sc);
16679 
16680     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
16681 
16682     val = 0xfffc;
16683     if (CHIP_IS_E3(sc)) {
16684         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16685         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16686     }
16687 
16688     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
16689 
16690     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16691 
16692     ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
16693     BLOGD(sc, DBG_LOAD, "after misc block init\n");
16694 
16695     if (!CHIP_IS_E1x(sc)) {
16696         /*
16697          * 4-port mode or 2-port mode we need to turn off master-enable for
16698          * everyone. After that we turn it back on for self. So, we disregard
16699          * multi-function, and always disable all functions on the given path,
16700          * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
16701          */
16702         for (abs_func_id = SC_PATH(sc);
16703              abs_func_id < (E2_FUNC_MAX * 2);
16704              abs_func_id += 2) {
16705             if (abs_func_id == SC_ABS_FUNC(sc)) {
16706                 REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
16707                 continue;
16708             }
16709 
16710             bxe_pretend_func(sc, abs_func_id);
16711 
16712             /* clear pf enable */
16713             bxe_pf_disable(sc);
16714 
16715             bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16716         }
16717     }
16718 
16719     BLOGD(sc, DBG_LOAD, "after pf disable\n");
16720 
16721     ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
16722 
16723     if (CHIP_IS_E1(sc)) {
16724         /*
16725          * enable HW interrupt from PXP on USDM overflow
16726          * bit 16 on INT_MASK_0
16727          */
16728         REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16729     }
16730 
16731     ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
16732     bxe_init_pxp(sc);
16733 
16734 #ifdef __BIG_ENDIAN
16735     REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
16736     REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
16737     REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
16738     REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
16739     REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
16740     /* make sure this value is 0 */
16741     REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
16742 
16743     //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
16744     REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
16745     REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
16746     REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
16747     REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
16748 #endif
16749 
16750     ecore_ilt_init_page_size(sc, INITOP_SET);
16751 
16752     if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
16753         REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
16754     }
16755 
16756     /* let the HW do it's magic... */
16757     DELAY(100000);
16758 
16759     /* finish PXP init */
16760     val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
16761     if (val != 1) {
16762         BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
16763             val);
16764         return (-1);
16765     }
16766     val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
16767     if (val != 1) {
16768         BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
16769         return (-1);
16770     }
16771 
16772     BLOGD(sc, DBG_LOAD, "after pxp init\n");
16773 
16774     /*
16775      * Timer bug workaround for E2 only. We need to set the entire ILT to have
16776      * entries with value "0" and valid bit on. This needs to be done by the
16777      * first PF that is loaded in a path (i.e. common phase)
16778      */
16779     if (!CHIP_IS_E1x(sc)) {
16780 /*
16781  * In E2 there is a bug in the timers block that can cause function 6 / 7
16782  * (i.e. vnic3) to start even if it is marked as "scan-off".
16783  * This occurs when a different function (func2,3) is being marked
16784  * as "scan-off". Real-life scenario for example: if a driver is being
16785  * load-unloaded while func6,7 are down. This will cause the timer to access
16786  * the ilt, translate to a logical address and send a request to read/write.
16787  * Since the ilt for the function that is down is not valid, this will cause
16788  * a translation error which is unrecoverable.
16789  * The Workaround is intended to make sure that when this happens nothing
16790  * fatal will occur. The workaround:
16791  *  1.  First PF driver which loads on a path will:
16792  *      a.  After taking the chip out of reset, by using pretend,
16793  *          it will write "0" to the following registers of
16794  *          the other vnics.
16795  *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16796  *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
16797  *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
16798  *          And for itself it will write '1' to
16799  *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
16800  *          dmae-operations (writing to pram for example.)
16801  *          note: can be done for only function 6,7 but cleaner this
16802  *            way.
16803  *      b.  Write zero+valid to the entire ILT.
16804  *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
16805  *          VNIC3 (of that port). The range allocated will be the
16806  *          entire ILT. This is needed to prevent  ILT range error.
16807  *  2.  Any PF driver load flow:
16808  *      a.  ILT update with the physical addresses of the allocated
16809  *          logical pages.
16810  *      b.  Wait 20msec. - note that this timeout is needed to make
16811  *          sure there are no requests in one of the PXP internal
16812  *          queues with "old" ILT addresses.
16813  *      c.  PF enable in the PGLC.
16814  *      d.  Clear the was_error of the PF in the PGLC. (could have
16815  *          occurred while driver was down)
16816  *      e.  PF enable in the CFC (WEAK + STRONG)
16817  *      f.  Timers scan enable
16818  *  3.  PF driver unload flow:
16819  *      a.  Clear the Timers scan_en.
16820  *      b.  Polling for scan_on=0 for that PF.
16821  *      c.  Clear the PF enable bit in the PXP.
16822  *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
16823  *      e.  Write zero+valid to all ILT entries (The valid bit must
16824  *          stay set)
16825  *      f.  If this is VNIC 3 of a port then also init
16826  *          first_timers_ilt_entry to zero and last_timers_ilt_entry
16827  *          to the last enrty in the ILT.
16828  *
16829  *      Notes:
16830  *      Currently the PF error in the PGLC is non recoverable.
16831  *      In the future the there will be a recovery routine for this error.
16832  *      Currently attention is masked.
16833  *      Having an MCP lock on the load/unload process does not guarantee that
16834  *      there is no Timer disable during Func6/7 enable. This is because the
16835  *      Timers scan is currently being cleared by the MCP on FLR.
16836  *      Step 2.d can be done only for PF6/7 and the driver can also check if
16837  *      there is error before clearing it. But the flow above is simpler and
16838  *      more general.
16839  *      All ILT entries are written by zero+valid and not just PF6/7
16840  *      ILT entries since in the future the ILT entries allocation for
16841  *      PF-s might be dynamic.
16842  */
16843         struct ilt_client_info ilt_cli;
16844         struct ecore_ilt ilt;
16845 
16846         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
16847         memset(&ilt, 0, sizeof(struct ecore_ilt));
16848 
16849         /* initialize dummy TM client */
16850         ilt_cli.start      = 0;
16851         ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
16852         ilt_cli.client_num = ILT_CLIENT_TM;
16853 
16854         /*
16855          * Step 1: set zeroes to all ilt page entries with valid bit on
16856          * Step 2: set the timers first/last ilt entry to point
16857          * to the entire range to prevent ILT range error for 3rd/4th
16858          * vnic (this code assumes existence of the vnic)
16859          *
16860          * both steps performed by call to ecore_ilt_client_init_op()
16861          * with dummy TM client
16862          *
16863          * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
16864          * and his brother are split registers
16865          */
16866 
16867         bxe_pretend_func(sc, (SC_PATH(sc) + 6));
16868         ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
16869         bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16870 
16871         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
16872         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
16873         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
16874     }
16875 
16876     REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
16877     REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
16878 
16879     if (!CHIP_IS_E1x(sc)) {
16880         int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
16881                      (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
16882 
16883         ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
16884         ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
16885 
16886         /* let the HW do it's magic... */
16887         do {
16888             DELAY(200000);
16889             val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
16890         } while (factor-- && (val != 1));
16891 
16892         if (val != 1) {
16893             BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
16894             return (-1);
16895         }
16896     }
16897 
16898     BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
16899 
16900     ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
16901 
16902     bxe_iov_init_dmae(sc);
16903 
16904     /* clean the DMAE memory */
16905     sc->dmae_ready = 1;
16906     ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
16907 
16908     ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
16909 
16910     ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
16911 
16912     ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
16913 
16914     ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
16915 
16916     bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
16917     bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
16918     bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
16919     bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
16920 
16921     ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
16922 
16923     /* QM queues pointers table */
16924     ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
16925 
16926     /* soft reset pulse */
16927     REG_WR(sc, QM_REG_SOFT_RESET, 1);
16928     REG_WR(sc, QM_REG_SOFT_RESET, 0);
16929 
16930     if (CNIC_SUPPORT(sc))
16931         ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
16932 
16933     ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
16934     REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
16935     if (!CHIP_REV_IS_SLOW(sc)) {
16936         /* enable hw interrupt from doorbell Q */
16937         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16938     }
16939 
16940     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16941 
16942     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16943     REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
16944 
16945     if (!CHIP_IS_E1(sc)) {
16946         REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
16947     }
16948 
16949     if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
16950         if (IS_MF_AFEX(sc)) {
16951             /*
16952              * configure that AFEX and VLAN headers must be
16953              * received in AFEX mode
16954              */
16955             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
16956             REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
16957             REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
16958             REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
16959             REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
16960         } else {
16961             /*
16962              * Bit-map indicating which L2 hdrs may appear
16963              * after the basic Ethernet header
16964              */
16965             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
16966                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
16967         }
16968     }
16969 
16970     ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
16971     ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
16972     ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
16973     ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
16974 
16975     if (!CHIP_IS_E1x(sc)) {
16976         /* reset VFC memories */
16977         REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16978                VFC_MEMORIES_RST_REG_CAM_RST |
16979                VFC_MEMORIES_RST_REG_RAM_RST);
16980         REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16981                VFC_MEMORIES_RST_REG_CAM_RST |
16982                VFC_MEMORIES_RST_REG_RAM_RST);
16983 
16984         DELAY(20000);
16985     }
16986 
16987     ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
16988     ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
16989     ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
16990     ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
16991 
16992     /* sync semi rtc */
16993     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
16994            0x80000000);
16995     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
16996            0x80000000);
16997 
16998     ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
16999     ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
17000     ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
17001 
17002     if (!CHIP_IS_E1x(sc)) {
17003         if (IS_MF_AFEX(sc)) {
17004             /*
17005              * configure that AFEX and VLAN headers must be
17006              * sent in AFEX mode
17007              */
17008             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
17009             REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
17010             REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
17011             REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
17012             REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
17013         } else {
17014             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
17015                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17016         }
17017     }
17018 
17019     REG_WR(sc, SRC_REG_SOFT_RST, 1);
17020 
17021     ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
17022 
17023     if (CNIC_SUPPORT(sc)) {
17024         REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
17025         REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
17026         REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
17027         REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
17028         REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
17029         REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
17030         REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
17031         REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
17032         REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
17033         REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
17034     }
17035     REG_WR(sc, SRC_REG_SOFT_RST, 0);
17036 
17037     if (sizeof(union cdu_context) != 1024) {
17038         /* we currently assume that a context is 1024 bytes */
17039         BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
17040               (long)sizeof(union cdu_context));
17041     }
17042 
17043     ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
17044     val = (4 << 24) + (0 << 12) + 1024;
17045     REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
17046 
17047     ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
17048 
17049     REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
17050     /* enable context validation interrupt from CFC */
17051     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17052 
17053     /* set the thresholds to prevent CFC/CDU race */
17054     REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
17055     ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
17056 
17057     if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
17058         REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
17059     }
17060 
17061     ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
17062     ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
17063 
17064     /* Reset PCIE errors for debug */
17065     REG_WR(sc, 0x2814, 0xffffffff);
17066     REG_WR(sc, 0x3820, 0xffffffff);
17067 
17068     if (!CHIP_IS_E1x(sc)) {
17069         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
17070                (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17071                 PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17072         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17073                (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17074                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17075                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17076         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17077                (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17078                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17079                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17080     }
17081 
17082     ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17083 
17084     if (!CHIP_IS_E1(sc)) {
17085         /* in E3 this done in per-port section */
17086         if (!CHIP_IS_E3(sc))
17087             REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17088     }
17089 
17090     if (CHIP_IS_E1H(sc)) {
17091         /* not applicable for E2 (and above ...) */
17092         REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17093     }
17094 
17095     if (CHIP_REV_IS_SLOW(sc)) {
17096         DELAY(200000);
17097     }
17098 
17099     /* finish CFC init */
17100     val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17101     if (val != 1) {
17102         BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17103         return (-1);
17104     }
17105     val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17106     if (val != 1) {
17107         BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17108         return (-1);
17109     }
17110     val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17111     if (val != 1) {
17112         BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17113         return (-1);
17114     }
17115     REG_WR(sc, CFC_REG_DEBUG0, 0);
17116 
17117     if (CHIP_IS_E1(sc)) {
17118         /* read NIG statistic to see if this is our first up since powerup */
17119         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17120         val = *BXE_SP(sc, wb_data[0]);
17121 
17122         /* do internal memory self test */
17123         if ((val == 0) && bxe_int_mem_test(sc)) {
17124             BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17125             return (-1);
17126         }
17127     }
17128 
17129     bxe_setup_fan_failure_detection(sc);
17130 
17131     /* clear PXP2 attentions */
17132     REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17133 
17134     bxe_enable_blocks_attention(sc);
17135 
17136     if (!CHIP_REV_IS_SLOW(sc)) {
17137         ecore_enable_blocks_parity(sc);
17138     }
17139 
17140     if (!BXE_NOMCP(sc)) {
17141         if (CHIP_IS_E1x(sc)) {
17142             bxe_common_init_phy(sc);
17143         }
17144     }
17145 
17146     return (0);
17147 }
17148 
17149 /**
17150  * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17151  *
17152  * @sc:     driver handle
17153  */
17154 static int
17155 bxe_init_hw_common_chip(struct bxe_softc *sc)
17156 {
17157     int rc = bxe_init_hw_common(sc);
17158 
17159     if (rc) {
17160         BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17161         return (rc);
17162     }
17163 
17164     /* In E2 2-PORT mode, same ext phy is used for the two paths */
17165     if (!BXE_NOMCP(sc)) {
17166         bxe_common_init_phy(sc);
17167     }
17168 
17169     return (0);
17170 }
17171 
17172 static int
17173 bxe_init_hw_port(struct bxe_softc *sc)
17174 {
17175     int port = SC_PORT(sc);
17176     int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17177     uint32_t low, high;
17178     uint32_t val;
17179 
17180     BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17181 
17182     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17183 
17184     ecore_init_block(sc, BLOCK_MISC, init_phase);
17185     ecore_init_block(sc, BLOCK_PXP, init_phase);
17186     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17187 
17188     /*
17189      * Timers bug workaround: disables the pf_master bit in pglue at
17190      * common phase, we need to enable it here before any dmae access are
17191      * attempted. Therefore we manually added the enable-master to the
17192      * port phase (it also happens in the function phase)
17193      */
17194     if (!CHIP_IS_E1x(sc)) {
17195         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17196     }
17197 
17198     ecore_init_block(sc, BLOCK_ATC, init_phase);
17199     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17200     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17201     ecore_init_block(sc, BLOCK_QM, init_phase);
17202 
17203     ecore_init_block(sc, BLOCK_TCM, init_phase);
17204     ecore_init_block(sc, BLOCK_UCM, init_phase);
17205     ecore_init_block(sc, BLOCK_CCM, init_phase);
17206     ecore_init_block(sc, BLOCK_XCM, init_phase);
17207 
17208     /* QM cid (connection) count */
17209     ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17210 
17211     if (CNIC_SUPPORT(sc)) {
17212         ecore_init_block(sc, BLOCK_TM, init_phase);
17213         REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17214         REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17215     }
17216 
17217     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17218 
17219     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17220 
17221     if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17222         if (IS_MF(sc)) {
17223             low = (BXE_ONE_PORT(sc) ? 160 : 246);
17224         } else if (sc->mtu > 4096) {
17225             if (BXE_ONE_PORT(sc)) {
17226                 low = 160;
17227             } else {
17228                 val = sc->mtu;
17229                 /* (24*1024 + val*4)/256 */
17230                 low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17231             }
17232         } else {
17233             low = (BXE_ONE_PORT(sc) ? 80 : 160);
17234         }
17235         high = (low + 56); /* 14*1024/256 */
17236         REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17237         REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17238     }
17239 
17240     if (CHIP_IS_MODE_4_PORT(sc)) {
17241         REG_WR(sc, SC_PORT(sc) ?
17242                BRB1_REG_MAC_GUARANTIED_1 :
17243                BRB1_REG_MAC_GUARANTIED_0, 40);
17244     }
17245 
17246     ecore_init_block(sc, BLOCK_PRS, init_phase);
17247     if (CHIP_IS_E3B0(sc)) {
17248         if (IS_MF_AFEX(sc)) {
17249             /* configure headers for AFEX mode */
17250             REG_WR(sc, SC_PORT(sc) ?
17251                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17252                    PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17253             REG_WR(sc, SC_PORT(sc) ?
17254                    PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17255                    PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17256             REG_WR(sc, SC_PORT(sc) ?
17257                    PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17258                    PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17259         } else {
17260             /* Ovlan exists only if we are in multi-function +
17261              * switch-dependent mode, in switch-independent there
17262              * is no ovlan headers
17263              */
17264             REG_WR(sc, SC_PORT(sc) ?
17265                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17266                    PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17267                    (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17268         }
17269     }
17270 
17271     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17272     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17273     ecore_init_block(sc, BLOCK_USDM, init_phase);
17274     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17275 
17276     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17277     ecore_init_block(sc, BLOCK_USEM, init_phase);
17278     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17279     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17280 
17281     ecore_init_block(sc, BLOCK_UPB, init_phase);
17282     ecore_init_block(sc, BLOCK_XPB, init_phase);
17283 
17284     ecore_init_block(sc, BLOCK_PBF, init_phase);
17285 
17286     if (CHIP_IS_E1x(sc)) {
17287         /* configure PBF to work without PAUSE mtu 9000 */
17288         REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17289 
17290         /* update threshold */
17291         REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17292         /* update init credit */
17293         REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17294 
17295         /* probe changes */
17296         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17297         DELAY(50);
17298         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17299     }
17300 
17301     if (CNIC_SUPPORT(sc)) {
17302         ecore_init_block(sc, BLOCK_SRC, init_phase);
17303     }
17304 
17305     ecore_init_block(sc, BLOCK_CDU, init_phase);
17306     ecore_init_block(sc, BLOCK_CFC, init_phase);
17307 
17308     if (CHIP_IS_E1(sc)) {
17309         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17310         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17311     }
17312     ecore_init_block(sc, BLOCK_HC, init_phase);
17313 
17314     ecore_init_block(sc, BLOCK_IGU, init_phase);
17315 
17316     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17317     /* init aeu_mask_attn_func_0/1:
17318      *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17319      *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17320      *             bits 4-7 are used for "per vn group attention" */
17321     val = IS_MF(sc) ? 0xF7 : 0x7;
17322     /* Enable DCBX attention for all but E1 */
17323     val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17324     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17325 
17326     ecore_init_block(sc, BLOCK_NIG, init_phase);
17327 
17328     if (!CHIP_IS_E1x(sc)) {
17329         /* Bit-map indicating which L2 hdrs may appear after the
17330          * basic Ethernet header
17331          */
17332         if (IS_MF_AFEX(sc)) {
17333             REG_WR(sc, SC_PORT(sc) ?
17334                    NIG_REG_P1_HDRS_AFTER_BASIC :
17335                    NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17336         } else {
17337             REG_WR(sc, SC_PORT(sc) ?
17338                    NIG_REG_P1_HDRS_AFTER_BASIC :
17339                    NIG_REG_P0_HDRS_AFTER_BASIC,
17340                    IS_MF_SD(sc) ? 7 : 6);
17341         }
17342 
17343         if (CHIP_IS_E3(sc)) {
17344             REG_WR(sc, SC_PORT(sc) ?
17345                    NIG_REG_LLH1_MF_MODE :
17346                    NIG_REG_LLH_MF_MODE, IS_MF(sc));
17347         }
17348     }
17349     if (!CHIP_IS_E3(sc)) {
17350         REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17351     }
17352 
17353     if (!CHIP_IS_E1(sc)) {
17354         /* 0x2 disable mf_ov, 0x1 enable */
17355         REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17356                (IS_MF_SD(sc) ? 0x1 : 0x2));
17357 
17358         if (!CHIP_IS_E1x(sc)) {
17359             val = 0;
17360             switch (sc->devinfo.mf_info.mf_mode) {
17361             case MULTI_FUNCTION_SD:
17362                 val = 1;
17363                 break;
17364             case MULTI_FUNCTION_SI:
17365             case MULTI_FUNCTION_AFEX:
17366                 val = 2;
17367                 break;
17368             }
17369 
17370             REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17371                         NIG_REG_LLH0_CLS_TYPE), val);
17372         }
17373         REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17374         REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17375         REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17376     }
17377 
17378     /* If SPIO5 is set to generate interrupts, enable it for this port */
17379     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17380     if (val & MISC_SPIO_SPIO5) {
17381         uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17382                                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17383         val = REG_RD(sc, reg_addr);
17384         val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17385         REG_WR(sc, reg_addr, val);
17386     }
17387 
17388     return (0);
17389 }
17390 
17391 static uint32_t
17392 bxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17393                        uint32_t         reg,
17394                        uint32_t         expected,
17395                        uint32_t         poll_count)
17396 {
17397     uint32_t cur_cnt = poll_count;
17398     uint32_t val;
17399 
17400     while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17401         DELAY(FLR_WAIT_INTERVAL);
17402     }
17403 
17404     return (val);
17405 }
17406 
17407 static int
17408 bxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17409                               uint32_t         reg,
17410                               char             *msg,
17411                               uint32_t         poll_cnt)
17412 {
17413     uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17414 
17415     if (val != 0) {
17416         BLOGE(sc, "%s usage count=%d\n", msg, val);
17417         return (1);
17418     }
17419 
17420     return (0);
17421 }
17422 
17423 /* Common routines with VF FLR cleanup */
17424 static uint32_t
17425 bxe_flr_clnup_poll_count(struct bxe_softc *sc)
17426 {
17427     /* adjust polling timeout */
17428     if (CHIP_REV_IS_EMUL(sc)) {
17429         return (FLR_POLL_CNT * 2000);
17430     }
17431 
17432     if (CHIP_REV_IS_FPGA(sc)) {
17433         return (FLR_POLL_CNT * 120);
17434     }
17435 
17436     return (FLR_POLL_CNT);
17437 }
17438 
17439 static int
17440 bxe_poll_hw_usage_counters(struct bxe_softc *sc,
17441                            uint32_t         poll_cnt)
17442 {
17443     /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17444     if (bxe_flr_clnup_poll_hw_counter(sc,
17445                                       CFC_REG_NUM_LCIDS_INSIDE_PF,
17446                                       "CFC PF usage counter timed out",
17447                                       poll_cnt)) {
17448         return (1);
17449     }
17450 
17451     /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17452     if (bxe_flr_clnup_poll_hw_counter(sc,
17453                                       DORQ_REG_PF_USAGE_CNT,
17454                                       "DQ PF usage counter timed out",
17455                                       poll_cnt)) {
17456         return (1);
17457     }
17458 
17459     /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17460     if (bxe_flr_clnup_poll_hw_counter(sc,
17461                                       QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17462                                       "QM PF usage counter timed out",
17463                                       poll_cnt)) {
17464         return (1);
17465     }
17466 
17467     /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17468     if (bxe_flr_clnup_poll_hw_counter(sc,
17469                                       TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17470                                       "Timers VNIC usage counter timed out",
17471                                       poll_cnt)) {
17472         return (1);
17473     }
17474 
17475     if (bxe_flr_clnup_poll_hw_counter(sc,
17476                                       TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17477                                       "Timers NUM_SCANS usage counter timed out",
17478                                       poll_cnt)) {
17479         return (1);
17480     }
17481 
17482     /* Wait DMAE PF usage counter to zero */
17483     if (bxe_flr_clnup_poll_hw_counter(sc,
17484                                       dmae_reg_go_c[INIT_DMAE_C(sc)],
17485                                       "DMAE dommand register timed out",
17486                                       poll_cnt)) {
17487         return (1);
17488     }
17489 
17490     return (0);
17491 }
17492 
17493 #define OP_GEN_PARAM(param)                                            \
17494     (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17495 #define OP_GEN_TYPE(type)                                           \
17496     (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17497 #define OP_GEN_AGG_VECT(index)                                             \
17498     (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17499 
17500 static int
17501 bxe_send_final_clnup(struct bxe_softc *sc,
17502                      uint8_t          clnup_func,
17503                      uint32_t         poll_cnt)
17504 {
17505     uint32_t op_gen_command = 0;
17506     uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17507                           CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17508     int ret = 0;
17509 
17510     if (REG_RD(sc, comp_addr)) {
17511         BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17512         return (1);
17513     }
17514 
17515     op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17516     op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17517     op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17518     op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17519 
17520     BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17521     REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17522 
17523     if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17524         BLOGE(sc, "FW final cleanup did not succeed\n");
17525         BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17526               (REG_RD(sc, comp_addr)));
17527         bxe_panic(sc, ("FLR cleanup failed\n"));
17528         return (1);
17529     }
17530 
17531     /* Zero completion for nxt FLR */
17532     REG_WR(sc, comp_addr, 0);
17533 
17534     return (ret);
17535 }
17536 
17537 static void
17538 bxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17539                        struct pbf_pN_buf_regs *regs,
17540                        uint32_t               poll_count)
17541 {
17542     uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17543     uint32_t cur_cnt = poll_count;
17544 
17545     crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
17546     crd = crd_start = REG_RD(sc, regs->crd);
17547     init_crd = REG_RD(sc, regs->init_crd);
17548 
17549     BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
17550     BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
17551     BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
17552 
17553     while ((crd != init_crd) &&
17554            ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
17555             (init_crd - crd_start))) {
17556         if (cur_cnt--) {
17557             DELAY(FLR_WAIT_INTERVAL);
17558             crd = REG_RD(sc, regs->crd);
17559             crd_freed = REG_RD(sc, regs->crd_freed);
17560         } else {
17561             BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
17562             BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
17563             BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
17564             break;
17565         }
17566     }
17567 
17568     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
17569           poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17570 }
17571 
17572 static void
17573 bxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
17574                        struct pbf_pN_cmd_regs *regs,
17575                        uint32_t               poll_count)
17576 {
17577     uint32_t occup, to_free, freed, freed_start;
17578     uint32_t cur_cnt = poll_count;
17579 
17580     occup = to_free = REG_RD(sc, regs->lines_occup);
17581     freed = freed_start = REG_RD(sc, regs->lines_freed);
17582 
17583     BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17584     BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17585 
17586     while (occup &&
17587            ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
17588         if (cur_cnt--) {
17589             DELAY(FLR_WAIT_INTERVAL);
17590             occup = REG_RD(sc, regs->lines_occup);
17591             freed = REG_RD(sc, regs->lines_freed);
17592         } else {
17593             BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
17594             BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17595             BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17596             break;
17597         }
17598     }
17599 
17600     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
17601           poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17602 }
17603 
17604 static void
17605 bxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
17606 {
17607     struct pbf_pN_cmd_regs cmd_regs[] = {
17608         {0, (CHIP_IS_E3B0(sc)) ?
17609             PBF_REG_TQ_OCCUPANCY_Q0 :
17610             PBF_REG_P0_TQ_OCCUPANCY,
17611             (CHIP_IS_E3B0(sc)) ?
17612             PBF_REG_TQ_LINES_FREED_CNT_Q0 :
17613             PBF_REG_P0_TQ_LINES_FREED_CNT},
17614         {1, (CHIP_IS_E3B0(sc)) ?
17615             PBF_REG_TQ_OCCUPANCY_Q1 :
17616             PBF_REG_P1_TQ_OCCUPANCY,
17617             (CHIP_IS_E3B0(sc)) ?
17618             PBF_REG_TQ_LINES_FREED_CNT_Q1 :
17619             PBF_REG_P1_TQ_LINES_FREED_CNT},
17620         {4, (CHIP_IS_E3B0(sc)) ?
17621             PBF_REG_TQ_OCCUPANCY_LB_Q :
17622             PBF_REG_P4_TQ_OCCUPANCY,
17623             (CHIP_IS_E3B0(sc)) ?
17624             PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
17625             PBF_REG_P4_TQ_LINES_FREED_CNT}
17626     };
17627 
17628     struct pbf_pN_buf_regs buf_regs[] = {
17629         {0, (CHIP_IS_E3B0(sc)) ?
17630             PBF_REG_INIT_CRD_Q0 :
17631             PBF_REG_P0_INIT_CRD ,
17632             (CHIP_IS_E3B0(sc)) ?
17633             PBF_REG_CREDIT_Q0 :
17634             PBF_REG_P0_CREDIT,
17635             (CHIP_IS_E3B0(sc)) ?
17636             PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
17637             PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
17638         {1, (CHIP_IS_E3B0(sc)) ?
17639             PBF_REG_INIT_CRD_Q1 :
17640             PBF_REG_P1_INIT_CRD,
17641             (CHIP_IS_E3B0(sc)) ?
17642             PBF_REG_CREDIT_Q1 :
17643             PBF_REG_P1_CREDIT,
17644             (CHIP_IS_E3B0(sc)) ?
17645             PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
17646             PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
17647         {4, (CHIP_IS_E3B0(sc)) ?
17648             PBF_REG_INIT_CRD_LB_Q :
17649             PBF_REG_P4_INIT_CRD,
17650             (CHIP_IS_E3B0(sc)) ?
17651             PBF_REG_CREDIT_LB_Q :
17652             PBF_REG_P4_CREDIT,
17653             (CHIP_IS_E3B0(sc)) ?
17654             PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
17655             PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
17656     };
17657 
17658     int i;
17659 
17660     /* Verify the command queues are flushed P0, P1, P4 */
17661     for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
17662         bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
17663     }
17664 
17665     /* Verify the transmission buffers are flushed P0, P1, P4 */
17666     for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
17667         bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
17668     }
17669 }
17670 
17671 static void
17672 bxe_hw_enable_status(struct bxe_softc *sc)
17673 {
17674     uint32_t val;
17675 
17676     val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
17677     BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
17678 
17679     val = REG_RD(sc, PBF_REG_DISABLE_PF);
17680     BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
17681 
17682     val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
17683     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
17684 
17685     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
17686     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
17687 
17688     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
17689     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
17690 
17691     val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
17692     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
17693 
17694     val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
17695     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
17696 
17697     val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
17698     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
17699 }
17700 
17701 static int
17702 bxe_pf_flr_clnup(struct bxe_softc *sc)
17703 {
17704     uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
17705 
17706     BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
17707 
17708     /* Re-enable PF target read access */
17709     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
17710 
17711     /* Poll HW usage counters */
17712     BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
17713     if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
17714         return (-1);
17715     }
17716 
17717     /* Zero the igu 'trailing edge' and 'leading edge' */
17718 
17719     /* Send the FW cleanup command */
17720     if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
17721         return (-1);
17722     }
17723 
17724     /* ATC cleanup */
17725 
17726     /* Verify TX hw is flushed */
17727     bxe_tx_hw_flushed(sc, poll_cnt);
17728 
17729     /* Wait 100ms (not adjusted according to platform) */
17730     DELAY(100000);
17731 
17732     /* Verify no pending pci transactions */
17733     if (bxe_is_pcie_pending(sc)) {
17734         BLOGE(sc, "PCIE Transactions still pending\n");
17735     }
17736 
17737     /* Debug */
17738     bxe_hw_enable_status(sc);
17739 
17740     /*
17741      * Master enable - Due to WB DMAE writes performed before this
17742      * register is re-initialized as part of the regular function init
17743      */
17744     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17745 
17746     return (0);
17747 }
17748 
17749 static int
17750 bxe_init_hw_func(struct bxe_softc *sc)
17751 {
17752     int port = SC_PORT(sc);
17753     int func = SC_FUNC(sc);
17754     int init_phase = PHASE_PF0 + func;
17755     struct ecore_ilt *ilt = sc->ilt;
17756     uint16_t cdu_ilt_start;
17757     uint32_t addr, val;
17758     uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
17759     int i, main_mem_width, rc;
17760 
17761     BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
17762 
17763     /* FLR cleanup */
17764     if (!CHIP_IS_E1x(sc)) {
17765         rc = bxe_pf_flr_clnup(sc);
17766         if (rc) {
17767             BLOGE(sc, "FLR cleanup failed!\n");
17768             // XXX bxe_fw_dump(sc);
17769             // XXX bxe_idle_chk(sc);
17770             return (rc);
17771         }
17772     }
17773 
17774     /* set MSI reconfigure capability */
17775     if (sc->devinfo.int_block == INT_BLOCK_HC) {
17776         addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
17777         val = REG_RD(sc, addr);
17778         val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
17779         REG_WR(sc, addr, val);
17780     }
17781 
17782     ecore_init_block(sc, BLOCK_PXP, init_phase);
17783     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17784 
17785     ilt = sc->ilt;
17786     cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
17787 
17788     for (i = 0; i < L2_ILT_LINES(sc); i++) {
17789         ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
17790         ilt->lines[cdu_ilt_start + i].page_mapping =
17791             sc->context[i].vcxt_dma.paddr;
17792         ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
17793     }
17794     ecore_ilt_init_op(sc, INITOP_SET);
17795 
17796     /* Set NIC mode */
17797     REG_WR(sc, PRS_REG_NIC_MODE, 1);
17798     BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
17799 
17800     if (!CHIP_IS_E1x(sc)) {
17801         uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
17802 
17803         /* Turn on a single ISR mode in IGU if driver is going to use
17804          * INT#x or MSI
17805          */
17806         if (sc->interrupt_mode != INTR_MODE_MSIX) {
17807             pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
17808         }
17809 
17810         /*
17811          * Timers workaround bug: function init part.
17812          * Need to wait 20msec after initializing ILT,
17813          * needed to make sure there are no requests in
17814          * one of the PXP internal queues with "old" ILT addresses
17815          */
17816         DELAY(20000);
17817 
17818         /*
17819          * Master enable - Due to WB DMAE writes performed before this
17820          * register is re-initialized as part of the regular function
17821          * init
17822          */
17823         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17824         /* Enable the function in IGU */
17825         REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
17826     }
17827 
17828     sc->dmae_ready = 1;
17829 
17830     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17831 
17832     if (!CHIP_IS_E1x(sc))
17833         REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
17834 
17835     ecore_init_block(sc, BLOCK_ATC, init_phase);
17836     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17837     ecore_init_block(sc, BLOCK_NIG, init_phase);
17838     ecore_init_block(sc, BLOCK_SRC, init_phase);
17839     ecore_init_block(sc, BLOCK_MISC, init_phase);
17840     ecore_init_block(sc, BLOCK_TCM, init_phase);
17841     ecore_init_block(sc, BLOCK_UCM, init_phase);
17842     ecore_init_block(sc, BLOCK_CCM, init_phase);
17843     ecore_init_block(sc, BLOCK_XCM, init_phase);
17844     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17845     ecore_init_block(sc, BLOCK_USEM, init_phase);
17846     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17847     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17848 
17849     if (!CHIP_IS_E1x(sc))
17850         REG_WR(sc, QM_REG_PF_EN, 1);
17851 
17852     if (!CHIP_IS_E1x(sc)) {
17853         REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17854         REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17855         REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17856         REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17857     }
17858     ecore_init_block(sc, BLOCK_QM, init_phase);
17859 
17860     ecore_init_block(sc, BLOCK_TM, init_phase);
17861     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17862 
17863     bxe_iov_init_dq(sc);
17864 
17865     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17866     ecore_init_block(sc, BLOCK_PRS, init_phase);
17867     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17868     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17869     ecore_init_block(sc, BLOCK_USDM, init_phase);
17870     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17871     ecore_init_block(sc, BLOCK_UPB, init_phase);
17872     ecore_init_block(sc, BLOCK_XPB, init_phase);
17873     ecore_init_block(sc, BLOCK_PBF, init_phase);
17874     if (!CHIP_IS_E1x(sc))
17875         REG_WR(sc, PBF_REG_DISABLE_PF, 0);
17876 
17877     ecore_init_block(sc, BLOCK_CDU, init_phase);
17878 
17879     ecore_init_block(sc, BLOCK_CFC, init_phase);
17880 
17881     if (!CHIP_IS_E1x(sc))
17882         REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
17883 
17884     if (IS_MF(sc)) {
17885         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
17886         REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
17887     }
17888 
17889     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17890 
17891     /* HC init per function */
17892     if (sc->devinfo.int_block == INT_BLOCK_HC) {
17893         if (CHIP_IS_E1H(sc)) {
17894             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17895 
17896             REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17897             REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17898         }
17899         ecore_init_block(sc, BLOCK_HC, init_phase);
17900 
17901     } else {
17902         int num_segs, sb_idx, prod_offset;
17903 
17904         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17905 
17906         if (!CHIP_IS_E1x(sc)) {
17907             REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
17908             REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
17909         }
17910 
17911         ecore_init_block(sc, BLOCK_IGU, init_phase);
17912 
17913         if (!CHIP_IS_E1x(sc)) {
17914             int dsb_idx = 0;
17915             /**
17916              * Producer memory:
17917              * E2 mode: address 0-135 match to the mapping memory;
17918              * 136 - PF0 default prod; 137 - PF1 default prod;
17919              * 138 - PF2 default prod; 139 - PF3 default prod;
17920              * 140 - PF0 attn prod;    141 - PF1 attn prod;
17921              * 142 - PF2 attn prod;    143 - PF3 attn prod;
17922              * 144-147 reserved.
17923              *
17924              * E1.5 mode - In backward compatible mode;
17925              * for non default SB; each even line in the memory
17926              * holds the U producer and each odd line hold
17927              * the C producer. The first 128 producers are for
17928              * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
17929              * producers are for the DSB for each PF.
17930              * Each PF has five segments: (the order inside each
17931              * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
17932              * 132-135 C prods; 136-139 X prods; 140-143 T prods;
17933              * 144-147 attn prods;
17934              */
17935             /* non-default-status-blocks */
17936             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17937                 IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
17938             for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
17939                 prod_offset = (sc->igu_base_sb + sb_idx) *
17940                     num_segs;
17941 
17942                 for (i = 0; i < num_segs; i++) {
17943                     addr = IGU_REG_PROD_CONS_MEMORY +
17944                             (prod_offset + i) * 4;
17945                     REG_WR(sc, addr, 0);
17946                 }
17947                 /* send consumer update with value 0 */
17948                 bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
17949                            USTORM_ID, 0, IGU_INT_NOP, 1);
17950                 bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
17951             }
17952 
17953             /* default-status-blocks */
17954             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17955                 IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
17956 
17957             if (CHIP_IS_MODE_4_PORT(sc))
17958                 dsb_idx = SC_FUNC(sc);
17959             else
17960                 dsb_idx = SC_VN(sc);
17961 
17962             prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
17963                        IGU_BC_BASE_DSB_PROD + dsb_idx :
17964                        IGU_NORM_BASE_DSB_PROD + dsb_idx);
17965 
17966             /*
17967              * igu prods come in chunks of E1HVN_MAX (4) -
17968              * does not matters what is the current chip mode
17969              */
17970             for (i = 0; i < (num_segs * E1HVN_MAX);
17971                  i += E1HVN_MAX) {
17972                 addr = IGU_REG_PROD_CONS_MEMORY +
17973                             (prod_offset + i)*4;
17974                 REG_WR(sc, addr, 0);
17975             }
17976             /* send consumer update with 0 */
17977             if (CHIP_INT_MODE_IS_BC(sc)) {
17978                 bxe_ack_sb(sc, sc->igu_dsb_id,
17979                            USTORM_ID, 0, IGU_INT_NOP, 1);
17980                 bxe_ack_sb(sc, sc->igu_dsb_id,
17981                            CSTORM_ID, 0, IGU_INT_NOP, 1);
17982                 bxe_ack_sb(sc, sc->igu_dsb_id,
17983                            XSTORM_ID, 0, IGU_INT_NOP, 1);
17984                 bxe_ack_sb(sc, sc->igu_dsb_id,
17985                            TSTORM_ID, 0, IGU_INT_NOP, 1);
17986                 bxe_ack_sb(sc, sc->igu_dsb_id,
17987                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
17988             } else {
17989                 bxe_ack_sb(sc, sc->igu_dsb_id,
17990                            USTORM_ID, 0, IGU_INT_NOP, 1);
17991                 bxe_ack_sb(sc, sc->igu_dsb_id,
17992                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
17993             }
17994             bxe_igu_clear_sb(sc, sc->igu_dsb_id);
17995 
17996             /* !!! these should become driver const once
17997                rf-tool supports split-68 const */
17998             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
17999             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
18000             REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
18001             REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
18002             REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
18003             REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
18004         }
18005     }
18006 
18007     /* Reset PCIE errors for debug */
18008     REG_WR(sc, 0x2114, 0xffffffff);
18009     REG_WR(sc, 0x2120, 0xffffffff);
18010 
18011     if (CHIP_IS_E1x(sc)) {
18012         main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
18013         main_mem_base = HC_REG_MAIN_MEMORY +
18014                 SC_PORT(sc) * (main_mem_size * 4);
18015         main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
18016         main_mem_width = 8;
18017 
18018         val = REG_RD(sc, main_mem_prty_clr);
18019         if (val) {
18020             BLOGD(sc, DBG_LOAD,
18021                   "Parity errors in HC block during function init (0x%x)!\n",
18022                   val);
18023         }
18024 
18025         /* Clear "false" parity errors in MSI-X table */
18026         for (i = main_mem_base;
18027              i < main_mem_base + main_mem_size * 4;
18028              i += main_mem_width) {
18029             bxe_read_dmae(sc, i, main_mem_width / 4);
18030             bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
18031                            i, main_mem_width / 4);
18032         }
18033         /* Clear HC parity attention */
18034         REG_RD(sc, main_mem_prty_clr);
18035     }
18036 
18037 #if 1
18038     /* Enable STORMs SP logging */
18039     REG_WR8(sc, BAR_USTRORM_INTMEM +
18040            USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18041     REG_WR8(sc, BAR_TSTRORM_INTMEM +
18042            TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18043     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18044            CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18045     REG_WR8(sc, BAR_XSTRORM_INTMEM +
18046            XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18047 #endif
18048 
18049     elink_phy_probe(&sc->link_params);
18050 
18051     return (0);
18052 }
18053 
18054 static void
18055 bxe_link_reset(struct bxe_softc *sc)
18056 {
18057     if (!BXE_NOMCP(sc)) {
18058 	bxe_acquire_phy_lock(sc);
18059         elink_lfa_reset(&sc->link_params, &sc->link_vars);
18060 	bxe_release_phy_lock(sc);
18061     } else {
18062         if (!CHIP_REV_IS_SLOW(sc)) {
18063             BLOGW(sc, "Bootcode is missing - cannot reset link\n");
18064         }
18065     }
18066 }
18067 
18068 static void
18069 bxe_reset_port(struct bxe_softc *sc)
18070 {
18071     int port = SC_PORT(sc);
18072     uint32_t val;
18073 
18074     /* reset physical Link */
18075     bxe_link_reset(sc);
18076 
18077     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18078 
18079     /* Do not rcv packets to BRB */
18080     REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18081     /* Do not direct rcv packets that are not for MCP to the BRB */
18082     REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18083                NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18084 
18085     /* Configure AEU */
18086     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18087 
18088     DELAY(100000);
18089 
18090     /* Check for BRB port occupancy */
18091     val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18092     if (val) {
18093         BLOGD(sc, DBG_LOAD,
18094               "BRB1 is not empty, %d blocks are occupied\n", val);
18095     }
18096 
18097     /* TODO: Close Doorbell port? */
18098 }
18099 
18100 static void
18101 bxe_ilt_wr(struct bxe_softc *sc,
18102            uint32_t         index,
18103            bus_addr_t       addr)
18104 {
18105     int reg;
18106     uint32_t wb_write[2];
18107 
18108     if (CHIP_IS_E1(sc)) {
18109         reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18110     } else {
18111         reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18112     }
18113 
18114     wb_write[0] = ONCHIP_ADDR1(addr);
18115     wb_write[1] = ONCHIP_ADDR2(addr);
18116     REG_WR_DMAE(sc, reg, wb_write, 2);
18117 }
18118 
18119 static void
18120 bxe_clear_func_ilt(struct bxe_softc *sc,
18121                    uint32_t         func)
18122 {
18123     uint32_t i, base = FUNC_ILT_BASE(func);
18124     for (i = base; i < base + ILT_PER_FUNC; i++) {
18125         bxe_ilt_wr(sc, i, 0);
18126     }
18127 }
18128 
18129 static void
18130 bxe_reset_func(struct bxe_softc *sc)
18131 {
18132     struct bxe_fastpath *fp;
18133     int port = SC_PORT(sc);
18134     int func = SC_FUNC(sc);
18135     int i;
18136 
18137     /* Disable the function in the FW */
18138     REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18139     REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18140     REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18141     REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18142 
18143     /* FP SBs */
18144     FOR_EACH_ETH_QUEUE(sc, i) {
18145         fp = &sc->fp[i];
18146         REG_WR8(sc, BAR_CSTRORM_INTMEM +
18147                 CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18148                 SB_DISABLED);
18149     }
18150 
18151     /* SP SB */
18152     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18153             CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18154             SB_DISABLED);
18155 
18156     for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18157         REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18158     }
18159 
18160     /* Configure IGU */
18161     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18162         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18163         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18164     } else {
18165         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18166         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18167     }
18168 
18169     if (CNIC_LOADED(sc)) {
18170         /* Disable Timer scan */
18171         REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18172         /*
18173          * Wait for at least 10ms and up to 2 second for the timers
18174          * scan to complete
18175          */
18176         for (i = 0; i < 200; i++) {
18177             DELAY(10000);
18178             if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18179                 break;
18180         }
18181     }
18182 
18183     /* Clear ILT */
18184     bxe_clear_func_ilt(sc, func);
18185 
18186     /*
18187      * Timers workaround bug for E2: if this is vnic-3,
18188      * we need to set the entire ilt range for this timers.
18189      */
18190     if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18191         struct ilt_client_info ilt_cli;
18192         /* use dummy TM client */
18193         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18194         ilt_cli.start = 0;
18195         ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18196         ilt_cli.client_num = ILT_CLIENT_TM;
18197 
18198         ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18199     }
18200 
18201     /* this assumes that reset_port() called before reset_func()*/
18202     if (!CHIP_IS_E1x(sc)) {
18203         bxe_pf_disable(sc);
18204     }
18205 
18206     sc->dmae_ready = 0;
18207 }
18208 
18209 static int
18210 bxe_gunzip_init(struct bxe_softc *sc)
18211 {
18212     return (0);
18213 }
18214 
18215 static void
18216 bxe_gunzip_end(struct bxe_softc *sc)
18217 {
18218     return;
18219 }
18220 
18221 static int
18222 bxe_init_firmware(struct bxe_softc *sc)
18223 {
18224     if (CHIP_IS_E1(sc)) {
18225         ecore_init_e1_firmware(sc);
18226         sc->iro_array = e1_iro_arr;
18227     } else if (CHIP_IS_E1H(sc)) {
18228         ecore_init_e1h_firmware(sc);
18229         sc->iro_array = e1h_iro_arr;
18230     } else if (!CHIP_IS_E1x(sc)) {
18231         ecore_init_e2_firmware(sc);
18232         sc->iro_array = e2_iro_arr;
18233     } else {
18234         BLOGE(sc, "Unsupported chip revision\n");
18235         return (-1);
18236     }
18237 
18238     return (0);
18239 }
18240 
18241 static void
18242 bxe_release_firmware(struct bxe_softc *sc)
18243 {
18244     /* Do nothing */
18245     return;
18246 }
18247 
18248 static int
18249 ecore_gunzip(struct bxe_softc *sc,
18250              const uint8_t    *zbuf,
18251              int              len)
18252 {
18253     /* XXX : Implement... */
18254     BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18255     return (FALSE);
18256 }
18257 
18258 static void
18259 ecore_reg_wr_ind(struct bxe_softc *sc,
18260                  uint32_t         addr,
18261                  uint32_t         val)
18262 {
18263     bxe_reg_wr_ind(sc, addr, val);
18264 }
18265 
18266 static void
18267 ecore_write_dmae_phys_len(struct bxe_softc *sc,
18268                           bus_addr_t       phys_addr,
18269                           uint32_t         addr,
18270                           uint32_t         len)
18271 {
18272     bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18273 }
18274 
18275 void
18276 ecore_storm_memset_struct(struct bxe_softc *sc,
18277                           uint32_t         addr,
18278                           size_t           size,
18279                           uint32_t         *data)
18280 {
18281     uint8_t i;
18282     for (i = 0; i < size/4; i++) {
18283         REG_WR(sc, addr + (i * 4), data[i]);
18284     }
18285 }
18286 
18287 
18288 /*
18289  * character device - ioctl interface definitions
18290  */
18291 
18292 
18293 #include "bxe_dump.h"
18294 #include "bxe_ioctl.h"
18295 #include <sys/conf.h>
18296 
18297 static int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18298                 struct thread *td);
18299 
18300 static struct cdevsw bxe_cdevsw = {
18301     .d_version = D_VERSION,
18302     .d_ioctl = bxe_eioctl,
18303     .d_name = "bxecnic",
18304 };
18305 
18306 #define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18307 
18308 
18309 #define DUMP_ALL_PRESETS        0x1FFF
18310 #define DUMP_MAX_PRESETS        13
18311 #define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18312 #define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18313 #define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18314 #define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18315 #define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18316 
18317 #define IS_REG_IN_PRESET(presets, idx)  \
18318                 ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18319 
18320 
18321 static int
18322 bxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18323 {
18324     if (CHIP_IS_E1(sc))
18325         return dump_num_registers[0][preset-1];
18326     else if (CHIP_IS_E1H(sc))
18327         return dump_num_registers[1][preset-1];
18328     else if (CHIP_IS_E2(sc))
18329         return dump_num_registers[2][preset-1];
18330     else if (CHIP_IS_E3A0(sc))
18331         return dump_num_registers[3][preset-1];
18332     else if (CHIP_IS_E3B0(sc))
18333         return dump_num_registers[4][preset-1];
18334     else
18335         return 0;
18336 }
18337 
18338 static int
18339 bxe_get_total_regs_len32(struct bxe_softc *sc)
18340 {
18341     uint32_t preset_idx;
18342     int regdump_len32 = 0;
18343 
18344 
18345     /* Calculate the total preset regs length */
18346     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18347         regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18348     }
18349 
18350     return regdump_len32;
18351 }
18352 
18353 static const uint32_t *
18354 __bxe_get_page_addr_ar(struct bxe_softc *sc)
18355 {
18356     if (CHIP_IS_E2(sc))
18357         return page_vals_e2;
18358     else if (CHIP_IS_E3(sc))
18359         return page_vals_e3;
18360     else
18361         return NULL;
18362 }
18363 
18364 static uint32_t
18365 __bxe_get_page_reg_num(struct bxe_softc *sc)
18366 {
18367     if (CHIP_IS_E2(sc))
18368         return PAGE_MODE_VALUES_E2;
18369     else if (CHIP_IS_E3(sc))
18370         return PAGE_MODE_VALUES_E3;
18371     else
18372         return 0;
18373 }
18374 
18375 static const uint32_t *
18376 __bxe_get_page_write_ar(struct bxe_softc *sc)
18377 {
18378     if (CHIP_IS_E2(sc))
18379         return page_write_regs_e2;
18380     else if (CHIP_IS_E3(sc))
18381         return page_write_regs_e3;
18382     else
18383         return NULL;
18384 }
18385 
18386 static uint32_t
18387 __bxe_get_page_write_num(struct bxe_softc *sc)
18388 {
18389     if (CHIP_IS_E2(sc))
18390         return PAGE_WRITE_REGS_E2;
18391     else if (CHIP_IS_E3(sc))
18392         return PAGE_WRITE_REGS_E3;
18393     else
18394         return 0;
18395 }
18396 
18397 static const struct reg_addr *
18398 __bxe_get_page_read_ar(struct bxe_softc *sc)
18399 {
18400     if (CHIP_IS_E2(sc))
18401         return page_read_regs_e2;
18402     else if (CHIP_IS_E3(sc))
18403         return page_read_regs_e3;
18404     else
18405         return NULL;
18406 }
18407 
18408 static uint32_t
18409 __bxe_get_page_read_num(struct bxe_softc *sc)
18410 {
18411     if (CHIP_IS_E2(sc))
18412         return PAGE_READ_REGS_E2;
18413     else if (CHIP_IS_E3(sc))
18414         return PAGE_READ_REGS_E3;
18415     else
18416         return 0;
18417 }
18418 
18419 static bool
18420 bxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18421 {
18422     if (CHIP_IS_E1(sc))
18423         return IS_E1_REG(reg_info->chips);
18424     else if (CHIP_IS_E1H(sc))
18425         return IS_E1H_REG(reg_info->chips);
18426     else if (CHIP_IS_E2(sc))
18427         return IS_E2_REG(reg_info->chips);
18428     else if (CHIP_IS_E3A0(sc))
18429         return IS_E3A0_REG(reg_info->chips);
18430     else if (CHIP_IS_E3B0(sc))
18431         return IS_E3B0_REG(reg_info->chips);
18432     else
18433         return 0;
18434 }
18435 
18436 static bool
18437 bxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18438 {
18439     if (CHIP_IS_E1(sc))
18440         return IS_E1_REG(wreg_info->chips);
18441     else if (CHIP_IS_E1H(sc))
18442         return IS_E1H_REG(wreg_info->chips);
18443     else if (CHIP_IS_E2(sc))
18444         return IS_E2_REG(wreg_info->chips);
18445     else if (CHIP_IS_E3A0(sc))
18446         return IS_E3A0_REG(wreg_info->chips);
18447     else if (CHIP_IS_E3B0(sc))
18448         return IS_E3B0_REG(wreg_info->chips);
18449     else
18450         return 0;
18451 }
18452 
18453 /**
18454  * bxe_read_pages_regs - read "paged" registers
18455  *
18456  * @bp          device handle
18457  * @p           output buffer
18458  *
18459  * Reads "paged" memories: memories that may only be read by first writing to a
18460  * specific address ("write address") and then reading from a specific address
18461  * ("read address"). There may be more than one write address per "page" and
18462  * more than one read address per write address.
18463  */
18464 static void
18465 bxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18466 {
18467     uint32_t i, j, k, n;
18468 
18469     /* addresses of the paged registers */
18470     const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18471     /* number of paged registers */
18472     int num_pages = __bxe_get_page_reg_num(sc);
18473     /* write addresses */
18474     const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18475     /* number of write addresses */
18476     int write_num = __bxe_get_page_write_num(sc);
18477     /* read addresses info */
18478     const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18479     /* number of read addresses */
18480     int read_num = __bxe_get_page_read_num(sc);
18481     uint32_t addr, size;
18482 
18483     for (i = 0; i < num_pages; i++) {
18484         for (j = 0; j < write_num; j++) {
18485             REG_WR(sc, write_addr[j], page_addr[i]);
18486 
18487             for (k = 0; k < read_num; k++) {
18488                 if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18489                     size = read_addr[k].size;
18490                     for (n = 0; n < size; n++) {
18491                         addr = read_addr[k].addr + n*4;
18492                         *p++ = REG_RD(sc, addr);
18493                     }
18494                 }
18495             }
18496         }
18497     }
18498     return;
18499 }
18500 
18501 
18502 static int
18503 bxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18504 {
18505     uint32_t i, j, addr;
18506     const struct wreg_addr *wreg_addr_p = NULL;
18507 
18508     if (CHIP_IS_E1(sc))
18509         wreg_addr_p = &wreg_addr_e1;
18510     else if (CHIP_IS_E1H(sc))
18511         wreg_addr_p = &wreg_addr_e1h;
18512     else if (CHIP_IS_E2(sc))
18513         wreg_addr_p = &wreg_addr_e2;
18514     else if (CHIP_IS_E3A0(sc))
18515         wreg_addr_p = &wreg_addr_e3;
18516     else if (CHIP_IS_E3B0(sc))
18517         wreg_addr_p = &wreg_addr_e3b0;
18518     else
18519         return (-1);
18520 
18521     /* Read the idle_chk registers */
18522     for (i = 0; i < IDLE_REGS_COUNT; i++) {
18523         if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18524             IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18525             for (j = 0; j < idle_reg_addrs[i].size; j++)
18526                 *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18527         }
18528     }
18529 
18530     /* Read the regular registers */
18531     for (i = 0; i < REGS_COUNT; i++) {
18532         if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18533             IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18534             for (j = 0; j < reg_addrs[i].size; j++)
18535                 *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18536         }
18537     }
18538 
18539     /* Read the CAM registers */
18540     if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18541         IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18542         for (i = 0; i < wreg_addr_p->size; i++) {
18543             *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
18544 
18545             /* In case of wreg_addr register, read additional
18546                registers from read_regs array
18547              */
18548             for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
18549                 addr = *(wreg_addr_p->read_regs);
18550                 *p++ = REG_RD(sc, addr + j*4);
18551             }
18552         }
18553     }
18554 
18555     /* Paged registers are supported in E2 & E3 only */
18556     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
18557         /* Read "paged" registers */
18558         bxe_read_pages_regs(sc, p, preset);
18559     }
18560 
18561     return 0;
18562 }
18563 
18564 static int
18565 bxe_grc_dump(struct bxe_softc *sc)
18566 {
18567     int rval = 0;
18568     uint32_t preset_idx;
18569     uint8_t *buf;
18570     uint32_t size;
18571     struct  dump_header *d_hdr;
18572 
18573     if (sc->grcdump_done)
18574 	return (rval);
18575 
18576     ecore_disable_blocks_parity(sc);
18577 
18578     buf = sc->grc_dump;
18579     d_hdr = sc->grc_dump;
18580 
18581     d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
18582     d_hdr->version = BNX2X_DUMP_VERSION;
18583     d_hdr->preset = DUMP_ALL_PRESETS;
18584 
18585     if (CHIP_IS_E1(sc)) {
18586         d_hdr->dump_meta_data = DUMP_CHIP_E1;
18587     } else if (CHIP_IS_E1H(sc)) {
18588         d_hdr->dump_meta_data = DUMP_CHIP_E1H;
18589     } else if (CHIP_IS_E2(sc)) {
18590         d_hdr->dump_meta_data = DUMP_CHIP_E2 |
18591                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18592     } else if (CHIP_IS_E3A0(sc)) {
18593         d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
18594                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18595     } else if (CHIP_IS_E3B0(sc)) {
18596         d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
18597                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18598     }
18599 
18600     buf += sizeof(struct  dump_header);
18601 
18602     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18603 
18604         /* Skip presets with IOR */
18605         if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
18606             (preset_idx == 11))
18607             continue;
18608 
18609         rval = bxe_get_preset_regs(sc, sc->grc_dump, preset_idx);
18610 
18611 	if (rval)
18612             break;
18613 
18614         size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
18615 
18616         buf += size;
18617     }
18618 
18619     ecore_clear_blocks_parity(sc);
18620     ecore_enable_blocks_parity(sc);
18621 
18622     sc->grcdump_done = 1;
18623     return(rval);
18624 }
18625 
18626 static int
18627 bxe_add_cdev(struct bxe_softc *sc)
18628 {
18629     int grc_dump_size;
18630 
18631     grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18632 				sizeof(struct  dump_header);
18633 
18634     sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
18635 
18636     if (sc->grc_dump == NULL)
18637         return (-1);
18638 
18639     sc->ioctl_dev = make_dev(&bxe_cdevsw,
18640                             sc->ifp->if_dunit,
18641                             UID_ROOT,
18642                             GID_WHEEL,
18643                             0600,
18644                             "%s",
18645                             if_name(sc->ifp));
18646 
18647     if (sc->ioctl_dev == NULL) {
18648 
18649         free(sc->grc_dump, M_DEVBUF);
18650 
18651         return (-1);
18652     }
18653 
18654     sc->ioctl_dev->si_drv1 = sc;
18655 
18656     return (0);
18657 }
18658 
18659 static void
18660 bxe_del_cdev(struct bxe_softc *sc)
18661 {
18662     if (sc->ioctl_dev != NULL)
18663         destroy_dev(sc->ioctl_dev);
18664 
18665     if (sc->grc_dump == NULL)
18666         free(sc->grc_dump, M_DEVBUF);
18667 
18668     return;
18669 }
18670 
18671 static int
18672 bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18673         struct thread *td)
18674 {
18675     struct bxe_softc    *sc;
18676     int                 rval = 0;
18677     device_t            pci_dev;
18678     bxe_grcdump_t       *dump = NULL;
18679     int grc_dump_size;
18680 
18681     if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
18682         return ENXIO;
18683 
18684     pci_dev= sc->dev;
18685 
18686     dump = (bxe_grcdump_t *)data;
18687 
18688     switch(cmd) {
18689 
18690         case BXE_GRC_DUMP_SIZE:
18691             dump->pci_func = sc->pcie_func;
18692             dump->grcdump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18693 					sizeof(struct  dump_header);
18694             break;
18695 
18696         case BXE_GRC_DUMP:
18697 
18698             grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18699 				sizeof(struct  dump_header);
18700 
18701             if ((sc->grc_dump == NULL) || (dump->grcdump == NULL) ||
18702                 (dump->grcdump_size < grc_dump_size) || (!sc->grcdump_done)) {
18703                 rval = EINVAL;
18704                 break;
18705             }
18706 	    dump->grcdump_dwords = grc_dump_size >> 2;
18707             rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
18708             sc->grcdump_done = 0;
18709 
18710             break;
18711 
18712         default:
18713             break;
18714     }
18715 
18716     return (rval);
18717 }
18718