xref: /freebsd/sys/dev/bxe/bxe.c (revision f061a2215f9bf0bea98ac601a34750f89428db67)
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     { STATS_OFFSET32(tx_queue_full_return),
492                 4, STATS_FLAGS_FUNC, "tx_queue_full_return"}
493 };
494 
495 static const struct {
496     uint32_t offset;
497     uint32_t size;
498     char string[STAT_NAME_LEN];
499 } bxe_eth_q_stats_arr[] = {
500     { Q_STATS_OFFSET32(total_bytes_received_hi),
501                 8, "rx_bytes" },
502     { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
503                 8, "rx_ucast_packets" },
504     { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
505                 8, "rx_mcast_packets" },
506     { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
507                 8, "rx_bcast_packets" },
508     { Q_STATS_OFFSET32(no_buff_discard_hi),
509                 8, "rx_discards" },
510     { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
511                 8, "tx_bytes" },
512     { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
513                 8, "tx_ucast_packets" },
514     { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
515                 8, "tx_mcast_packets" },
516     { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
517                 8, "tx_bcast_packets" },
518     { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
519                 8, "tpa_aggregations" },
520     { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
521                 8, "tpa_aggregated_frames"},
522     { Q_STATS_OFFSET32(total_tpa_bytes_hi),
523                 8, "tpa_bytes"},
524     { Q_STATS_OFFSET32(rx_calls),
525                 4, "rx_calls"},
526     { Q_STATS_OFFSET32(rx_pkts),
527                 4, "rx_pkts"},
528     { Q_STATS_OFFSET32(rx_tpa_pkts),
529                 4, "rx_tpa_pkts"},
530     { Q_STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
531                 4, "rx_erroneous_jumbo_sge_pkts"},
532     { Q_STATS_OFFSET32(rx_bxe_service_rxsgl),
533                 4, "rx_bxe_service_rxsgl"},
534     { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
535                 4, "rx_jumbo_sge_pkts"},
536     { Q_STATS_OFFSET32(rx_soft_errors),
537                 4, "rx_soft_errors"},
538     { Q_STATS_OFFSET32(rx_hw_csum_errors),
539                 4, "rx_hw_csum_errors"},
540     { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
541                 4, "rx_ofld_frames_csum_ip"},
542     { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
543                 4, "rx_ofld_frames_csum_tcp_udp"},
544     { Q_STATS_OFFSET32(rx_budget_reached),
545                 4, "rx_budget_reached"},
546     { Q_STATS_OFFSET32(tx_pkts),
547                 4, "tx_pkts"},
548     { Q_STATS_OFFSET32(tx_soft_errors),
549                 4, "tx_soft_errors"},
550     { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
551                 4, "tx_ofld_frames_csum_ip"},
552     { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
553                 4, "tx_ofld_frames_csum_tcp"},
554     { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
555                 4, "tx_ofld_frames_csum_udp"},
556     { Q_STATS_OFFSET32(tx_ofld_frames_lso),
557                 4, "tx_ofld_frames_lso"},
558     { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
559                 4, "tx_ofld_frames_lso_hdr_splits"},
560     { Q_STATS_OFFSET32(tx_encap_failures),
561                 4, "tx_encap_failures"},
562     { Q_STATS_OFFSET32(tx_hw_queue_full),
563                 4, "tx_hw_queue_full"},
564     { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
565                 4, "tx_hw_max_queue_depth"},
566     { Q_STATS_OFFSET32(tx_dma_mapping_failure),
567                 4, "tx_dma_mapping_failure"},
568     { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
569                 4, "tx_max_drbr_queue_depth"},
570     { Q_STATS_OFFSET32(tx_window_violation_std),
571                 4, "tx_window_violation_std"},
572     { Q_STATS_OFFSET32(tx_window_violation_tso),
573                 4, "tx_window_violation_tso"},
574     { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
575                 4, "tx_chain_lost_mbuf"},
576     { Q_STATS_OFFSET32(tx_frames_deferred),
577                 4, "tx_frames_deferred"},
578     { Q_STATS_OFFSET32(tx_queue_xoff),
579                 4, "tx_queue_xoff"},
580     { Q_STATS_OFFSET32(mbuf_defrag_attempts),
581                 4, "mbuf_defrag_attempts"},
582     { Q_STATS_OFFSET32(mbuf_defrag_failures),
583                 4, "mbuf_defrag_failures"},
584     { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
585                 4, "mbuf_rx_bd_alloc_failed"},
586     { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
587                 4, "mbuf_rx_bd_mapping_failed"},
588     { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
589                 4, "mbuf_rx_tpa_alloc_failed"},
590     { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
591                 4, "mbuf_rx_tpa_mapping_failed"},
592     { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
593                 4, "mbuf_rx_sge_alloc_failed"},
594     { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
595                 4, "mbuf_rx_sge_mapping_failed"},
596     { Q_STATS_OFFSET32(mbuf_alloc_tx),
597                 4, "mbuf_alloc_tx"},
598     { Q_STATS_OFFSET32(mbuf_alloc_rx),
599                 4, "mbuf_alloc_rx"},
600     { Q_STATS_OFFSET32(mbuf_alloc_sge),
601                 4, "mbuf_alloc_sge"},
602     { Q_STATS_OFFSET32(mbuf_alloc_tpa),
603                 4, "mbuf_alloc_tpa"},
604     { Q_STATS_OFFSET32(tx_queue_full_return),
605                 4, "tx_queue_full_return"}
606 };
607 
608 #define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
609 #define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
610 
611 
612 static void    bxe_cmng_fns_init(struct bxe_softc *sc,
613                                  uint8_t          read_cfg,
614                                  uint8_t          cmng_type);
615 static int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
616 static void    storm_memset_cmng(struct bxe_softc *sc,
617                                  struct cmng_init *cmng,
618                                  uint8_t          port);
619 static void    bxe_set_reset_global(struct bxe_softc *sc);
620 static void    bxe_set_reset_in_progress(struct bxe_softc *sc);
621 static uint8_t bxe_reset_is_done(struct bxe_softc *sc,
622                                  int              engine);
623 static uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
624 static uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
625                                    uint8_t          *global,
626                                    uint8_t          print);
627 static void    bxe_int_disable(struct bxe_softc *sc);
628 static int     bxe_release_leader_lock(struct bxe_softc *sc);
629 static void    bxe_pf_disable(struct bxe_softc *sc);
630 static void    bxe_free_fp_buffers(struct bxe_softc *sc);
631 static inline void bxe_update_rx_prod(struct bxe_softc    *sc,
632                                       struct bxe_fastpath *fp,
633                                       uint16_t            rx_bd_prod,
634                                       uint16_t            rx_cq_prod,
635                                       uint16_t            rx_sge_prod);
636 static void    bxe_link_report_locked(struct bxe_softc *sc);
637 static void    bxe_link_report(struct bxe_softc *sc);
638 static void    bxe_link_status_update(struct bxe_softc *sc);
639 static void    bxe_periodic_callout_func(void *xsc);
640 static void    bxe_periodic_start(struct bxe_softc *sc);
641 static void    bxe_periodic_stop(struct bxe_softc *sc);
642 static int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
643                                     uint16_t prev_index,
644                                     uint16_t index);
645 static int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
646                                      int                 queue);
647 static int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
648                                      uint16_t            index);
649 static uint8_t bxe_txeof(struct bxe_softc *sc,
650                          struct bxe_fastpath *fp);
651 static void    bxe_task_fp(struct bxe_fastpath *fp);
652 static __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
653                                      struct mbuf      *m,
654                                      uint8_t          contents);
655 static int     bxe_alloc_mem(struct bxe_softc *sc);
656 static void    bxe_free_mem(struct bxe_softc *sc);
657 static int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
658 static void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
659 static int     bxe_interrupt_attach(struct bxe_softc *sc);
660 static void    bxe_interrupt_detach(struct bxe_softc *sc);
661 static void    bxe_set_rx_mode(struct bxe_softc *sc);
662 static int     bxe_init_locked(struct bxe_softc *sc);
663 static int     bxe_stop_locked(struct bxe_softc *sc);
664 static __noinline int bxe_nic_load(struct bxe_softc *sc,
665                                    int              load_mode);
666 static __noinline int bxe_nic_unload(struct bxe_softc *sc,
667                                      uint32_t         unload_mode,
668                                      uint8_t          keep_link);
669 
670 static void bxe_handle_sp_tq(void *context, int pending);
671 static void bxe_handle_fp_tq(void *context, int pending);
672 
673 static int bxe_add_cdev(struct bxe_softc *sc);
674 static void bxe_del_cdev(struct bxe_softc *sc);
675 static int bxe_alloc_buf_rings(struct bxe_softc *sc);
676 static void bxe_free_buf_rings(struct bxe_softc *sc);
677 
678 /* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
679 uint32_t
680 calc_crc32(uint8_t  *crc32_packet,
681            uint32_t crc32_length,
682            uint32_t crc32_seed,
683            uint8_t  complement)
684 {
685    uint32_t byte         = 0;
686    uint32_t bit          = 0;
687    uint8_t  msb          = 0;
688    uint32_t temp         = 0;
689    uint32_t shft         = 0;
690    uint8_t  current_byte = 0;
691    uint32_t crc32_result = crc32_seed;
692    const uint32_t CRC32_POLY = 0x1edc6f41;
693 
694    if ((crc32_packet == NULL) ||
695        (crc32_length == 0) ||
696        ((crc32_length % 8) != 0))
697     {
698         return (crc32_result);
699     }
700 
701     for (byte = 0; byte < crc32_length; byte = byte + 1)
702     {
703         current_byte = crc32_packet[byte];
704         for (bit = 0; bit < 8; bit = bit + 1)
705         {
706             /* msb = crc32_result[31]; */
707             msb = (uint8_t)(crc32_result >> 31);
708 
709             crc32_result = crc32_result << 1;
710 
711             /* it (msb != current_byte[bit]) */
712             if (msb != (0x1 & (current_byte >> bit)))
713             {
714                 crc32_result = crc32_result ^ CRC32_POLY;
715                 /* crc32_result[0] = 1 */
716                 crc32_result |= 1;
717             }
718         }
719     }
720 
721     /* Last step is to:
722      * 1. "mirror" every bit
723      * 2. swap the 4 bytes
724      * 3. complement each bit
725      */
726 
727     /* Mirror */
728     temp = crc32_result;
729     shft = sizeof(crc32_result) * 8 - 1;
730 
731     for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
732     {
733         temp <<= 1;
734         temp |= crc32_result & 1;
735         shft-- ;
736     }
737 
738     /* temp[31-bit] = crc32_result[bit] */
739     temp <<= shft;
740 
741     /* Swap */
742     /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
743     {
744         uint32_t t0, t1, t2, t3;
745         t0 = (0x000000ff & (temp >> 24));
746         t1 = (0x0000ff00 & (temp >> 8));
747         t2 = (0x00ff0000 & (temp << 8));
748         t3 = (0xff000000 & (temp << 24));
749         crc32_result = t0 | t1 | t2 | t3;
750     }
751 
752     /* Complement */
753     if (complement)
754     {
755         crc32_result = ~crc32_result;
756     }
757 
758     return (crc32_result);
759 }
760 
761 int
762 bxe_test_bit(int                    nr,
763              volatile unsigned long *addr)
764 {
765     return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
766 }
767 
768 void
769 bxe_set_bit(unsigned int           nr,
770             volatile unsigned long *addr)
771 {
772     atomic_set_acq_long(addr, (1 << nr));
773 }
774 
775 void
776 bxe_clear_bit(int                    nr,
777               volatile unsigned long *addr)
778 {
779     atomic_clear_acq_long(addr, (1 << nr));
780 }
781 
782 int
783 bxe_test_and_set_bit(int                    nr,
784                        volatile unsigned long *addr)
785 {
786     unsigned long x;
787     nr = (1 << nr);
788     do {
789         x = *addr;
790     } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
791     // if (x & nr) bit_was_set; else bit_was_not_set;
792     return (x & nr);
793 }
794 
795 int
796 bxe_test_and_clear_bit(int                    nr,
797                        volatile unsigned long *addr)
798 {
799     unsigned long x;
800     nr = (1 << nr);
801     do {
802         x = *addr;
803     } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
804     // if (x & nr) bit_was_set; else bit_was_not_set;
805     return (x & nr);
806 }
807 
808 int
809 bxe_cmpxchg(volatile int *addr,
810             int          old,
811             int          new)
812 {
813     int x;
814     do {
815         x = *addr;
816     } while (atomic_cmpset_acq_int(addr, old, new) == 0);
817     return (x);
818 }
819 
820 /*
821  * Get DMA memory from the OS.
822  *
823  * Validates that the OS has provided DMA buffers in response to a
824  * bus_dmamap_load call and saves the physical address of those buffers.
825  * When the callback is used the OS will return 0 for the mapping function
826  * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
827  * failures back to the caller.
828  *
829  * Returns:
830  *   Nothing.
831  */
832 static void
833 bxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
834 {
835     struct bxe_dma *dma = arg;
836 
837     if (error) {
838         dma->paddr = 0;
839         dma->nseg  = 0;
840         BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
841     } else {
842         dma->paddr = segs->ds_addr;
843         dma->nseg  = nseg;
844     }
845 }
846 
847 /*
848  * Allocate a block of memory and map it for DMA. No partial completions
849  * allowed and release any resources acquired if we can't acquire all
850  * resources.
851  *
852  * Returns:
853  *   0 = Success, !0 = Failure
854  */
855 int
856 bxe_dma_alloc(struct bxe_softc *sc,
857               bus_size_t       size,
858               struct bxe_dma   *dma,
859               const char       *msg)
860 {
861     int rc;
862 
863     if (dma->size > 0) {
864         BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
865               (unsigned long)dma->size);
866         return (1);
867     }
868 
869     memset(dma, 0, sizeof(*dma)); /* sanity */
870     dma->sc   = sc;
871     dma->size = size;
872     snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
873 
874     rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
875                             BCM_PAGE_SIZE,      /* alignment */
876                             0,                  /* boundary limit */
877                             BUS_SPACE_MAXADDR,  /* restricted low */
878                             BUS_SPACE_MAXADDR,  /* restricted hi */
879                             NULL,               /* addr filter() */
880                             NULL,               /* addr filter() arg */
881                             size,               /* max map size */
882                             1,                  /* num discontinuous */
883                             size,               /* max seg size */
884                             BUS_DMA_ALLOCNOW,   /* flags */
885                             NULL,               /* lock() */
886                             NULL,               /* lock() arg */
887                             &dma->tag);         /* returned dma tag */
888     if (rc != 0) {
889         BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
890         memset(dma, 0, sizeof(*dma));
891         return (1);
892     }
893 
894     rc = bus_dmamem_alloc(dma->tag,
895                           (void **)&dma->vaddr,
896                           (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
897                           &dma->map);
898     if (rc != 0) {
899         BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
900         bus_dma_tag_destroy(dma->tag);
901         memset(dma, 0, sizeof(*dma));
902         return (1);
903     }
904 
905     rc = bus_dmamap_load(dma->tag,
906                          dma->map,
907                          dma->vaddr,
908                          size,
909                          bxe_dma_map_addr, /* BLOGD in here */
910                          dma,
911                          BUS_DMA_NOWAIT);
912     if (rc != 0) {
913         BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
914         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
915         bus_dma_tag_destroy(dma->tag);
916         memset(dma, 0, sizeof(*dma));
917         return (1);
918     }
919 
920     return (0);
921 }
922 
923 void
924 bxe_dma_free(struct bxe_softc *sc,
925              struct bxe_dma   *dma)
926 {
927     if (dma->size > 0) {
928         DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
929 
930         bus_dmamap_sync(dma->tag, dma->map,
931                         (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
932         bus_dmamap_unload(dma->tag, dma->map);
933         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
934         bus_dma_tag_destroy(dma->tag);
935     }
936 
937     memset(dma, 0, sizeof(*dma));
938 }
939 
940 /*
941  * These indirect read and write routines are only during init.
942  * The locking is handled by the MCP.
943  */
944 
945 void
946 bxe_reg_wr_ind(struct bxe_softc *sc,
947                uint32_t         addr,
948                uint32_t         val)
949 {
950     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
951     pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
952     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
953 }
954 
955 uint32_t
956 bxe_reg_rd_ind(struct bxe_softc *sc,
957                uint32_t         addr)
958 {
959     uint32_t val;
960 
961     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
962     val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
963     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
964 
965     return (val);
966 }
967 
968 static int
969 bxe_acquire_hw_lock(struct bxe_softc *sc,
970                     uint32_t         resource)
971 {
972     uint32_t lock_status;
973     uint32_t resource_bit = (1 << resource);
974     int func = SC_FUNC(sc);
975     uint32_t hw_lock_control_reg;
976     int cnt;
977 
978     /* validate the resource is within range */
979     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
980         BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
981             " resource_bit 0x%x\n", resource, resource_bit);
982         return (-1);
983     }
984 
985     if (func <= 5) {
986         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
987     } else {
988         hw_lock_control_reg =
989                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
990     }
991 
992     /* validate the resource is not already taken */
993     lock_status = REG_RD(sc, hw_lock_control_reg);
994     if (lock_status & resource_bit) {
995         BLOGE(sc, "resource (0x%x) in use (status 0x%x bit 0x%x)\n",
996               resource, lock_status, resource_bit);
997         return (-1);
998     }
999 
1000     /* try every 5ms for 5 seconds */
1001     for (cnt = 0; cnt < 1000; cnt++) {
1002         REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1003         lock_status = REG_RD(sc, hw_lock_control_reg);
1004         if (lock_status & resource_bit) {
1005             return (0);
1006         }
1007         DELAY(5000);
1008     }
1009 
1010     BLOGE(sc, "Resource 0x%x resource_bit 0x%x lock timeout!\n",
1011         resource, resource_bit);
1012     return (-1);
1013 }
1014 
1015 static int
1016 bxe_release_hw_lock(struct bxe_softc *sc,
1017                     uint32_t         resource)
1018 {
1019     uint32_t lock_status;
1020     uint32_t resource_bit = (1 << resource);
1021     int func = SC_FUNC(sc);
1022     uint32_t hw_lock_control_reg;
1023 
1024     /* validate the resource is within range */
1025     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1026         BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1027             " resource_bit 0x%x\n", resource, resource_bit);
1028         return (-1);
1029     }
1030 
1031     if (func <= 5) {
1032         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1033     } else {
1034         hw_lock_control_reg =
1035                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1036     }
1037 
1038     /* validate the resource is currently taken */
1039     lock_status = REG_RD(sc, hw_lock_control_reg);
1040     if (!(lock_status & resource_bit)) {
1041         BLOGE(sc, "resource (0x%x) not in use (status 0x%x bit 0x%x)\n",
1042               resource, lock_status, resource_bit);
1043         return (-1);
1044     }
1045 
1046     REG_WR(sc, hw_lock_control_reg, resource_bit);
1047     return (0);
1048 }
1049 static void bxe_acquire_phy_lock(struct bxe_softc *sc)
1050 {
1051 	BXE_PHY_LOCK(sc);
1052 	bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1053 }
1054 
1055 static void bxe_release_phy_lock(struct bxe_softc *sc)
1056 {
1057 	bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO);
1058 	BXE_PHY_UNLOCK(sc);
1059 }
1060 /*
1061  * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1062  * had we done things the other way around, if two pfs from the same port
1063  * would attempt to access nvram at the same time, we could run into a
1064  * scenario such as:
1065  * pf A takes the port lock.
1066  * pf B succeeds in taking the same lock since they are from the same port.
1067  * pf A takes the per pf misc lock. Performs eeprom access.
1068  * pf A finishes. Unlocks the per pf misc lock.
1069  * Pf B takes the lock and proceeds to perform it's own access.
1070  * pf A unlocks the per port lock, while pf B is still working (!).
1071  * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1072  * access corrupted by pf B).*
1073  */
1074 static int
1075 bxe_acquire_nvram_lock(struct bxe_softc *sc)
1076 {
1077     int port = SC_PORT(sc);
1078     int count, i;
1079     uint32_t val = 0;
1080 
1081     /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1082     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1083 
1084     /* adjust timeout for emulation/FPGA */
1085     count = NVRAM_TIMEOUT_COUNT;
1086     if (CHIP_REV_IS_SLOW(sc)) {
1087         count *= 100;
1088     }
1089 
1090     /* request access to nvram interface */
1091     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1092            (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1093 
1094     for (i = 0; i < count*10; i++) {
1095         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1096         if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1097             break;
1098         }
1099 
1100         DELAY(5);
1101     }
1102 
1103     if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1104         BLOGE(sc, "Cannot get access to nvram interface "
1105             "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1106             port, val);
1107         return (-1);
1108     }
1109 
1110     return (0);
1111 }
1112 
1113 static int
1114 bxe_release_nvram_lock(struct bxe_softc *sc)
1115 {
1116     int port = SC_PORT(sc);
1117     int count, i;
1118     uint32_t val = 0;
1119 
1120     /* adjust timeout for emulation/FPGA */
1121     count = NVRAM_TIMEOUT_COUNT;
1122     if (CHIP_REV_IS_SLOW(sc)) {
1123         count *= 100;
1124     }
1125 
1126     /* relinquish nvram interface */
1127     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1128            (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1129 
1130     for (i = 0; i < count*10; i++) {
1131         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1132         if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1133             break;
1134         }
1135 
1136         DELAY(5);
1137     }
1138 
1139     if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1140         BLOGE(sc, "Cannot free access to nvram interface "
1141             "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1142             port, val);
1143         return (-1);
1144     }
1145 
1146     /* release HW lock: protect against other PFs in PF Direct Assignment */
1147     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1148 
1149     return (0);
1150 }
1151 
1152 static void
1153 bxe_enable_nvram_access(struct bxe_softc *sc)
1154 {
1155     uint32_t val;
1156 
1157     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1158 
1159     /* enable both bits, even on read */
1160     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1161            (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1162 }
1163 
1164 static void
1165 bxe_disable_nvram_access(struct bxe_softc *sc)
1166 {
1167     uint32_t val;
1168 
1169     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1170 
1171     /* disable both bits, even after read */
1172     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1173            (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1174                     MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1175 }
1176 
1177 static int
1178 bxe_nvram_read_dword(struct bxe_softc *sc,
1179                      uint32_t         offset,
1180                      uint32_t         *ret_val,
1181                      uint32_t         cmd_flags)
1182 {
1183     int count, i, rc;
1184     uint32_t val;
1185 
1186     /* build the command word */
1187     cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1188 
1189     /* need to clear DONE bit separately */
1190     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1191 
1192     /* address of the NVRAM to read from */
1193     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1194            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1195 
1196     /* issue a read command */
1197     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1198 
1199     /* adjust timeout for emulation/FPGA */
1200     count = NVRAM_TIMEOUT_COUNT;
1201     if (CHIP_REV_IS_SLOW(sc)) {
1202         count *= 100;
1203     }
1204 
1205     /* wait for completion */
1206     *ret_val = 0;
1207     rc = -1;
1208     for (i = 0; i < count; i++) {
1209         DELAY(5);
1210         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1211 
1212         if (val & MCPR_NVM_COMMAND_DONE) {
1213             val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1214             /* we read nvram data in cpu order
1215              * but ethtool sees it as an array of bytes
1216              * converting to big-endian will do the work
1217              */
1218             *ret_val = htobe32(val);
1219             rc = 0;
1220             break;
1221         }
1222     }
1223 
1224     if (rc == -1) {
1225         BLOGE(sc, "nvram read timeout expired "
1226             "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1227             offset, cmd_flags, val);
1228     }
1229 
1230     return (rc);
1231 }
1232 
1233 static int
1234 bxe_nvram_read(struct bxe_softc *sc,
1235                uint32_t         offset,
1236                uint8_t          *ret_buf,
1237                int              buf_size)
1238 {
1239     uint32_t cmd_flags;
1240     uint32_t val;
1241     int rc;
1242 
1243     if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1244         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1245               offset, buf_size);
1246         return (-1);
1247     }
1248 
1249     if ((offset + buf_size) > sc->devinfo.flash_size) {
1250         BLOGE(sc, "Invalid parameter, "
1251                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1252               offset, buf_size, sc->devinfo.flash_size);
1253         return (-1);
1254     }
1255 
1256     /* request access to nvram interface */
1257     rc = bxe_acquire_nvram_lock(sc);
1258     if (rc) {
1259         return (rc);
1260     }
1261 
1262     /* enable access to nvram interface */
1263     bxe_enable_nvram_access(sc);
1264 
1265     /* read the first word(s) */
1266     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1267     while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1268         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1269         memcpy(ret_buf, &val, 4);
1270 
1271         /* advance to the next dword */
1272         offset += sizeof(uint32_t);
1273         ret_buf += sizeof(uint32_t);
1274         buf_size -= sizeof(uint32_t);
1275         cmd_flags = 0;
1276     }
1277 
1278     if (rc == 0) {
1279         cmd_flags |= MCPR_NVM_COMMAND_LAST;
1280         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1281         memcpy(ret_buf, &val, 4);
1282     }
1283 
1284     /* disable access to nvram interface */
1285     bxe_disable_nvram_access(sc);
1286     bxe_release_nvram_lock(sc);
1287 
1288     return (rc);
1289 }
1290 
1291 static int
1292 bxe_nvram_write_dword(struct bxe_softc *sc,
1293                       uint32_t         offset,
1294                       uint32_t         val,
1295                       uint32_t         cmd_flags)
1296 {
1297     int count, i, rc;
1298 
1299     /* build the command word */
1300     cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1301 
1302     /* need to clear DONE bit separately */
1303     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1304 
1305     /* write the data */
1306     REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1307 
1308     /* address of the NVRAM to write to */
1309     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1310            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1311 
1312     /* issue the write command */
1313     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1314 
1315     /* adjust timeout for emulation/FPGA */
1316     count = NVRAM_TIMEOUT_COUNT;
1317     if (CHIP_REV_IS_SLOW(sc)) {
1318         count *= 100;
1319     }
1320 
1321     /* wait for completion */
1322     rc = -1;
1323     for (i = 0; i < count; i++) {
1324         DELAY(5);
1325         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1326         if (val & MCPR_NVM_COMMAND_DONE) {
1327             rc = 0;
1328             break;
1329         }
1330     }
1331 
1332     if (rc == -1) {
1333         BLOGE(sc, "nvram write timeout expired "
1334             "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1335             offset, cmd_flags, val);
1336     }
1337 
1338     return (rc);
1339 }
1340 
1341 #define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1342 
1343 static int
1344 bxe_nvram_write1(struct bxe_softc *sc,
1345                  uint32_t         offset,
1346                  uint8_t          *data_buf,
1347                  int              buf_size)
1348 {
1349     uint32_t cmd_flags;
1350     uint32_t align_offset;
1351     uint32_t val;
1352     int rc;
1353 
1354     if ((offset + buf_size) > sc->devinfo.flash_size) {
1355         BLOGE(sc, "Invalid parameter, "
1356                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1357               offset, buf_size, sc->devinfo.flash_size);
1358         return (-1);
1359     }
1360 
1361     /* request access to nvram interface */
1362     rc = bxe_acquire_nvram_lock(sc);
1363     if (rc) {
1364         return (rc);
1365     }
1366 
1367     /* enable access to nvram interface */
1368     bxe_enable_nvram_access(sc);
1369 
1370     cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1371     align_offset = (offset & ~0x03);
1372     rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1373 
1374     if (rc == 0) {
1375         val &= ~(0xff << BYTE_OFFSET(offset));
1376         val |= (*data_buf << BYTE_OFFSET(offset));
1377 
1378         /* nvram data is returned as an array of bytes
1379          * convert it back to cpu order
1380          */
1381         val = be32toh(val);
1382 
1383         rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1384     }
1385 
1386     /* disable access to nvram interface */
1387     bxe_disable_nvram_access(sc);
1388     bxe_release_nvram_lock(sc);
1389 
1390     return (rc);
1391 }
1392 
1393 static int
1394 bxe_nvram_write(struct bxe_softc *sc,
1395                 uint32_t         offset,
1396                 uint8_t          *data_buf,
1397                 int              buf_size)
1398 {
1399     uint32_t cmd_flags;
1400     uint32_t val;
1401     uint32_t written_so_far;
1402     int rc;
1403 
1404     if (buf_size == 1) {
1405         return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1406     }
1407 
1408     if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1409         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1410               offset, buf_size);
1411         return (-1);
1412     }
1413 
1414     if (buf_size == 0) {
1415         return (0); /* nothing to do */
1416     }
1417 
1418     if ((offset + buf_size) > sc->devinfo.flash_size) {
1419         BLOGE(sc, "Invalid parameter, "
1420                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1421               offset, buf_size, sc->devinfo.flash_size);
1422         return (-1);
1423     }
1424 
1425     /* request access to nvram interface */
1426     rc = bxe_acquire_nvram_lock(sc);
1427     if (rc) {
1428         return (rc);
1429     }
1430 
1431     /* enable access to nvram interface */
1432     bxe_enable_nvram_access(sc);
1433 
1434     written_so_far = 0;
1435     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1436     while ((written_so_far < buf_size) && (rc == 0)) {
1437         if (written_so_far == (buf_size - sizeof(uint32_t))) {
1438             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1439         } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1440             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1441         } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1442             cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1443         }
1444 
1445         memcpy(&val, data_buf, 4);
1446 
1447         rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1448 
1449         /* advance to the next dword */
1450         offset += sizeof(uint32_t);
1451         data_buf += sizeof(uint32_t);
1452         written_so_far += sizeof(uint32_t);
1453         cmd_flags = 0;
1454     }
1455 
1456     /* disable access to nvram interface */
1457     bxe_disable_nvram_access(sc);
1458     bxe_release_nvram_lock(sc);
1459 
1460     return (rc);
1461 }
1462 
1463 /* copy command into DMAE command memory and set DMAE command Go */
1464 void
1465 bxe_post_dmae(struct bxe_softc    *sc,
1466               struct dmae_cmd *dmae,
1467               int                 idx)
1468 {
1469     uint32_t cmd_offset;
1470     int i;
1471 
1472     cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_cmd) * idx));
1473     for (i = 0; i < ((sizeof(struct dmae_cmd) / 4)); i++) {
1474         REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1475     }
1476 
1477     REG_WR(sc, dmae_reg_go_c[idx], 1);
1478 }
1479 
1480 uint32_t
1481 bxe_dmae_opcode_add_comp(uint32_t opcode,
1482                          uint8_t  comp_type)
1483 {
1484     return (opcode | ((comp_type << DMAE_CMD_C_DST_SHIFT) |
1485                       DMAE_CMD_C_TYPE_ENABLE));
1486 }
1487 
1488 uint32_t
1489 bxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1490 {
1491     return (opcode & ~DMAE_CMD_SRC_RESET);
1492 }
1493 
1494 uint32_t
1495 bxe_dmae_opcode(struct bxe_softc *sc,
1496                 uint8_t          src_type,
1497                 uint8_t          dst_type,
1498                 uint8_t          with_comp,
1499                 uint8_t          comp_type)
1500 {
1501     uint32_t opcode = 0;
1502 
1503     opcode |= ((src_type << DMAE_CMD_SRC_SHIFT) |
1504                (dst_type << DMAE_CMD_DST_SHIFT));
1505 
1506     opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
1507 
1508     opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1509 
1510     opcode |= ((SC_VN(sc) << DMAE_CMD_E1HVN_SHIFT) |
1511                (SC_VN(sc) << DMAE_CMD_DST_VN_SHIFT));
1512 
1513     opcode |= (DMAE_COM_SET_ERR << DMAE_CMD_ERR_POLICY_SHIFT);
1514 
1515 #ifdef __BIG_ENDIAN
1516     opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1517 #else
1518     opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1519 #endif
1520 
1521     if (with_comp) {
1522         opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1523     }
1524 
1525     return (opcode);
1526 }
1527 
1528 static void
1529 bxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1530                         struct dmae_cmd *dmae,
1531                         uint8_t             src_type,
1532                         uint8_t             dst_type)
1533 {
1534     memset(dmae, 0, sizeof(struct dmae_cmd));
1535 
1536     /* set the opcode */
1537     dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1538                                    TRUE, DMAE_COMP_PCI);
1539 
1540     /* fill in the completion parameters */
1541     dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1542     dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1543     dmae->comp_val     = DMAE_COMP_VAL;
1544 }
1545 
1546 /* issue a DMAE command over the init channel and wait for completion */
1547 static int
1548 bxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1549                          struct dmae_cmd *dmae)
1550 {
1551     uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1552     int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1553 
1554     BXE_DMAE_LOCK(sc);
1555 
1556     /* reset completion */
1557     *wb_comp = 0;
1558 
1559     /* post the command on the channel used for initializations */
1560     bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1561 
1562     /* wait for completion */
1563     DELAY(5);
1564 
1565     while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1566         if (!timeout ||
1567             (sc->recovery_state != BXE_RECOVERY_DONE &&
1568              sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1569             BLOGE(sc, "DMAE timeout! *wb_comp 0x%x recovery_state 0x%x\n",
1570                 *wb_comp, sc->recovery_state);
1571             BXE_DMAE_UNLOCK(sc);
1572             return (DMAE_TIMEOUT);
1573         }
1574 
1575         timeout--;
1576         DELAY(50);
1577     }
1578 
1579     if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1580         BLOGE(sc, "DMAE PCI error! *wb_comp 0x%x recovery_state 0x%x\n",
1581                 *wb_comp, sc->recovery_state);
1582         BXE_DMAE_UNLOCK(sc);
1583         return (DMAE_PCI_ERROR);
1584     }
1585 
1586     BXE_DMAE_UNLOCK(sc);
1587     return (0);
1588 }
1589 
1590 void
1591 bxe_read_dmae(struct bxe_softc *sc,
1592               uint32_t         src_addr,
1593               uint32_t         len32)
1594 {
1595     struct dmae_cmd dmae;
1596     uint32_t *data;
1597     int i, rc;
1598 
1599     DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1600 
1601     if (!sc->dmae_ready) {
1602         data = BXE_SP(sc, wb_data[0]);
1603 
1604         for (i = 0; i < len32; i++) {
1605             data[i] = (CHIP_IS_E1(sc)) ?
1606                           bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1607                           REG_RD(sc, (src_addr + (i * 4)));
1608         }
1609 
1610         return;
1611     }
1612 
1613     /* set opcode and fixed command fields */
1614     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1615 
1616     /* fill in addresses and len */
1617     dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1618     dmae.src_addr_hi = 0;
1619     dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1620     dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1621     dmae.len         = len32;
1622 
1623     /* issue the command and wait for completion */
1624     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1625         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1626     }
1627 }
1628 
1629 void
1630 bxe_write_dmae(struct bxe_softc *sc,
1631                bus_addr_t       dma_addr,
1632                uint32_t         dst_addr,
1633                uint32_t         len32)
1634 {
1635     struct dmae_cmd dmae;
1636     int rc;
1637 
1638     if (!sc->dmae_ready) {
1639         DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1640 
1641         if (CHIP_IS_E1(sc)) {
1642             ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1643         } else {
1644             ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1645         }
1646 
1647         return;
1648     }
1649 
1650     /* set opcode and fixed command fields */
1651     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1652 
1653     /* fill in addresses and len */
1654     dmae.src_addr_lo = U64_LO(dma_addr);
1655     dmae.src_addr_hi = U64_HI(dma_addr);
1656     dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1657     dmae.dst_addr_hi = 0;
1658     dmae.len         = len32;
1659 
1660     /* issue the command and wait for completion */
1661     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1662         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1663     }
1664 }
1665 
1666 void
1667 bxe_write_dmae_phys_len(struct bxe_softc *sc,
1668                         bus_addr_t       phys_addr,
1669                         uint32_t         addr,
1670                         uint32_t         len)
1671 {
1672     int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1673     int offset = 0;
1674 
1675     while (len > dmae_wr_max) {
1676         bxe_write_dmae(sc,
1677                        (phys_addr + offset), /* src DMA address */
1678                        (addr + offset),      /* dst GRC address */
1679                        dmae_wr_max);
1680         offset += (dmae_wr_max * 4);
1681         len -= dmae_wr_max;
1682     }
1683 
1684     bxe_write_dmae(sc,
1685                    (phys_addr + offset), /* src DMA address */
1686                    (addr + offset),      /* dst GRC address */
1687                    len);
1688 }
1689 
1690 void
1691 bxe_set_ctx_validation(struct bxe_softc   *sc,
1692                        struct eth_context *cxt,
1693                        uint32_t           cid)
1694 {
1695     /* ustorm cxt validation */
1696     cxt->ustorm_ag_context.cdu_usage =
1697         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1698             CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1699     /* xcontext validation */
1700     cxt->xstorm_ag_context.cdu_reserved =
1701         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1702             CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1703 }
1704 
1705 static void
1706 bxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1707                             uint8_t          port,
1708                             uint8_t          fw_sb_id,
1709                             uint8_t          sb_index,
1710                             uint8_t          ticks)
1711 {
1712     uint32_t addr =
1713         (BAR_CSTRORM_INTMEM +
1714          CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1715 
1716     REG_WR8(sc, addr, ticks);
1717 
1718     BLOGD(sc, DBG_LOAD,
1719           "port %d fw_sb_id %d sb_index %d ticks %d\n",
1720           port, fw_sb_id, sb_index, ticks);
1721 }
1722 
1723 static void
1724 bxe_storm_memset_hc_disable(struct bxe_softc *sc,
1725                             uint8_t          port,
1726                             uint16_t         fw_sb_id,
1727                             uint8_t          sb_index,
1728                             uint8_t          disable)
1729 {
1730     uint32_t enable_flag =
1731         (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1732     uint32_t addr =
1733         (BAR_CSTRORM_INTMEM +
1734          CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1735     uint8_t flags;
1736 
1737     /* clear and set */
1738     flags = REG_RD8(sc, addr);
1739     flags &= ~HC_INDEX_DATA_HC_ENABLED;
1740     flags |= enable_flag;
1741     REG_WR8(sc, addr, flags);
1742 
1743     BLOGD(sc, DBG_LOAD,
1744           "port %d fw_sb_id %d sb_index %d disable %d\n",
1745           port, fw_sb_id, sb_index, disable);
1746 }
1747 
1748 void
1749 bxe_update_coalesce_sb_index(struct bxe_softc *sc,
1750                              uint8_t          fw_sb_id,
1751                              uint8_t          sb_index,
1752                              uint8_t          disable,
1753                              uint16_t         usec)
1754 {
1755     int port = SC_PORT(sc);
1756     uint8_t ticks = (usec / 4); /* XXX ??? */
1757 
1758     bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1759 
1760     disable = (disable) ? 1 : ((usec) ? 0 : 1);
1761     bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1762 }
1763 
1764 void
1765 elink_cb_udelay(struct bxe_softc *sc,
1766                 uint32_t         usecs)
1767 {
1768     DELAY(usecs);
1769 }
1770 
1771 uint32_t
1772 elink_cb_reg_read(struct bxe_softc *sc,
1773                   uint32_t         reg_addr)
1774 {
1775     return (REG_RD(sc, reg_addr));
1776 }
1777 
1778 void
1779 elink_cb_reg_write(struct bxe_softc *sc,
1780                    uint32_t         reg_addr,
1781                    uint32_t         val)
1782 {
1783     REG_WR(sc, reg_addr, val);
1784 }
1785 
1786 void
1787 elink_cb_reg_wb_write(struct bxe_softc *sc,
1788                       uint32_t         offset,
1789                       uint32_t         *wb_write,
1790                       uint16_t         len)
1791 {
1792     REG_WR_DMAE(sc, offset, wb_write, len);
1793 }
1794 
1795 void
1796 elink_cb_reg_wb_read(struct bxe_softc *sc,
1797                      uint32_t         offset,
1798                      uint32_t         *wb_write,
1799                      uint16_t         len)
1800 {
1801     REG_RD_DMAE(sc, offset, wb_write, len);
1802 }
1803 
1804 uint8_t
1805 elink_cb_path_id(struct bxe_softc *sc)
1806 {
1807     return (SC_PATH(sc));
1808 }
1809 
1810 void
1811 elink_cb_event_log(struct bxe_softc     *sc,
1812                    const elink_log_id_t elink_log_id,
1813                    ...)
1814 {
1815     /* XXX */
1816     BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1817 }
1818 
1819 static int
1820 bxe_set_spio(struct bxe_softc *sc,
1821              int              spio,
1822              uint32_t         mode)
1823 {
1824     uint32_t spio_reg;
1825 
1826     /* Only 2 SPIOs are configurable */
1827     if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1828         BLOGE(sc, "Invalid SPIO 0x%x mode 0x%x\n", spio, mode);
1829         return (-1);
1830     }
1831 
1832     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1833 
1834     /* read SPIO and mask except the float bits */
1835     spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1836 
1837     switch (mode) {
1838     case MISC_SPIO_OUTPUT_LOW:
1839         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1840         /* clear FLOAT and set CLR */
1841         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1842         spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1843         break;
1844 
1845     case MISC_SPIO_OUTPUT_HIGH:
1846         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1847         /* clear FLOAT and set SET */
1848         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1849         spio_reg |=  (spio << MISC_SPIO_SET_POS);
1850         break;
1851 
1852     case MISC_SPIO_INPUT_HI_Z:
1853         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1854         /* set FLOAT */
1855         spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1856         break;
1857 
1858     default:
1859         break;
1860     }
1861 
1862     REG_WR(sc, MISC_REG_SPIO, spio_reg);
1863     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1864 
1865     return (0);
1866 }
1867 
1868 static int
1869 bxe_gpio_read(struct bxe_softc *sc,
1870               int              gpio_num,
1871               uint8_t          port)
1872 {
1873     /* The GPIO should be swapped if swap register is set and active */
1874     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1875                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1876     int gpio_shift = (gpio_num +
1877                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1878     uint32_t gpio_mask = (1 << gpio_shift);
1879     uint32_t gpio_reg;
1880 
1881     if (gpio_num > MISC_REGISTERS_GPIO_3) {
1882         BLOGE(sc, "Invalid GPIO %d port 0x%x gpio_port %d gpio_shift %d"
1883             " gpio_mask 0x%x\n", gpio_num, port, gpio_port, gpio_shift,
1884             gpio_mask);
1885         return (-1);
1886     }
1887 
1888     /* read GPIO value */
1889     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1890 
1891     /* get the requested pin value */
1892     return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
1893 }
1894 
1895 static int
1896 bxe_gpio_write(struct bxe_softc *sc,
1897                int              gpio_num,
1898                uint32_t         mode,
1899                uint8_t          port)
1900 {
1901     /* The GPIO should be swapped if swap register is set and active */
1902     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1903                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1904     int gpio_shift = (gpio_num +
1905                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1906     uint32_t gpio_mask = (1 << gpio_shift);
1907     uint32_t gpio_reg;
1908 
1909     if (gpio_num > MISC_REGISTERS_GPIO_3) {
1910         BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
1911             " gpio_shift %d gpio_mask 0x%x\n",
1912             gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
1913         return (-1);
1914     }
1915 
1916     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1917 
1918     /* read GPIO and mask except the float bits */
1919     gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1920 
1921     switch (mode) {
1922     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1923         BLOGD(sc, DBG_PHY,
1924               "Set GPIO %d (shift %d) -> output low\n",
1925               gpio_num, gpio_shift);
1926         /* clear FLOAT and set CLR */
1927         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1928         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1929         break;
1930 
1931     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1932         BLOGD(sc, DBG_PHY,
1933               "Set GPIO %d (shift %d) -> output high\n",
1934               gpio_num, gpio_shift);
1935         /* clear FLOAT and set SET */
1936         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1937         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1938         break;
1939 
1940     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1941         BLOGD(sc, DBG_PHY,
1942               "Set GPIO %d (shift %d) -> input\n",
1943               gpio_num, gpio_shift);
1944         /* set FLOAT */
1945         gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1946         break;
1947 
1948     default:
1949         break;
1950     }
1951 
1952     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1953     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1954 
1955     return (0);
1956 }
1957 
1958 static int
1959 bxe_gpio_mult_write(struct bxe_softc *sc,
1960                     uint8_t          pins,
1961                     uint32_t         mode)
1962 {
1963     uint32_t gpio_reg;
1964 
1965     /* any port swapping should be handled by caller */
1966 
1967     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1968 
1969     /* read GPIO and mask except the float bits */
1970     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1971     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1972     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
1973     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
1974 
1975     switch (mode) {
1976     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1977         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
1978         /* set CLR */
1979         gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
1980         break;
1981 
1982     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1983         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
1984         /* set SET */
1985         gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
1986         break;
1987 
1988     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1989         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
1990         /* set FLOAT */
1991         gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1992         break;
1993 
1994     default:
1995         BLOGE(sc, "Invalid GPIO mode assignment pins 0x%x mode 0x%x"
1996             " gpio_reg 0x%x\n", pins, mode, gpio_reg);
1997         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1998         return (-1);
1999     }
2000 
2001     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2002     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2003 
2004     return (0);
2005 }
2006 
2007 static int
2008 bxe_gpio_int_write(struct bxe_softc *sc,
2009                    int              gpio_num,
2010                    uint32_t         mode,
2011                    uint8_t          port)
2012 {
2013     /* The GPIO should be swapped if swap register is set and active */
2014     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2015                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2016     int gpio_shift = (gpio_num +
2017                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2018     uint32_t gpio_mask = (1 << gpio_shift);
2019     uint32_t gpio_reg;
2020 
2021     if (gpio_num > MISC_REGISTERS_GPIO_3) {
2022         BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
2023             " gpio_shift %d gpio_mask 0x%x\n",
2024             gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
2025         return (-1);
2026     }
2027 
2028     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2029 
2030     /* read GPIO int */
2031     gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2032 
2033     switch (mode) {
2034     case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2035         BLOGD(sc, DBG_PHY,
2036               "Clear GPIO INT %d (shift %d) -> output low\n",
2037               gpio_num, gpio_shift);
2038         /* clear SET and set CLR */
2039         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2040         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2041         break;
2042 
2043     case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2044         BLOGD(sc, DBG_PHY,
2045               "Set GPIO INT %d (shift %d) -> output high\n",
2046               gpio_num, gpio_shift);
2047         /* clear CLR and set SET */
2048         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2049         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2050         break;
2051 
2052     default:
2053         break;
2054     }
2055 
2056     REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2057     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2058 
2059     return (0);
2060 }
2061 
2062 uint32_t
2063 elink_cb_gpio_read(struct bxe_softc *sc,
2064                    uint16_t         gpio_num,
2065                    uint8_t          port)
2066 {
2067     return (bxe_gpio_read(sc, gpio_num, port));
2068 }
2069 
2070 uint8_t
2071 elink_cb_gpio_write(struct bxe_softc *sc,
2072                     uint16_t         gpio_num,
2073                     uint8_t          mode, /* 0=low 1=high */
2074                     uint8_t          port)
2075 {
2076     return (bxe_gpio_write(sc, gpio_num, mode, port));
2077 }
2078 
2079 uint8_t
2080 elink_cb_gpio_mult_write(struct bxe_softc *sc,
2081                          uint8_t          pins,
2082                          uint8_t          mode) /* 0=low 1=high */
2083 {
2084     return (bxe_gpio_mult_write(sc, pins, mode));
2085 }
2086 
2087 uint8_t
2088 elink_cb_gpio_int_write(struct bxe_softc *sc,
2089                         uint16_t         gpio_num,
2090                         uint8_t          mode, /* 0=low 1=high */
2091                         uint8_t          port)
2092 {
2093     return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2094 }
2095 
2096 void
2097 elink_cb_notify_link_changed(struct bxe_softc *sc)
2098 {
2099     REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2100                 (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2101 }
2102 
2103 /* send the MCP a request, block until there is a reply */
2104 uint32_t
2105 elink_cb_fw_command(struct bxe_softc *sc,
2106                     uint32_t         command,
2107                     uint32_t         param)
2108 {
2109     int mb_idx = SC_FW_MB_IDX(sc);
2110     uint32_t seq;
2111     uint32_t rc = 0;
2112     uint32_t cnt = 1;
2113     uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2114 
2115     BXE_FWMB_LOCK(sc);
2116 
2117     seq = ++sc->fw_seq;
2118     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2119     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2120 
2121     BLOGD(sc, DBG_PHY,
2122           "wrote command 0x%08x to FW MB param 0x%08x\n",
2123           (command | seq), param);
2124 
2125     /* Let the FW do it's magic. GIve it up to 5 seconds... */
2126     do {
2127         DELAY(delay * 1000);
2128         rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2129     } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2130 
2131     BLOGD(sc, DBG_PHY,
2132           "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2133           cnt*delay, rc, seq);
2134 
2135     /* is this a reply to our command? */
2136     if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2137         rc &= FW_MSG_CODE_MASK;
2138     } else {
2139         /* Ruh-roh! */
2140         BLOGE(sc, "FW failed to respond!\n");
2141         // XXX bxe_fw_dump(sc);
2142         rc = 0;
2143     }
2144 
2145     BXE_FWMB_UNLOCK(sc);
2146     return (rc);
2147 }
2148 
2149 static uint32_t
2150 bxe_fw_command(struct bxe_softc *sc,
2151                uint32_t         command,
2152                uint32_t         param)
2153 {
2154     return (elink_cb_fw_command(sc, command, param));
2155 }
2156 
2157 static void
2158 __storm_memset_dma_mapping(struct bxe_softc *sc,
2159                            uint32_t         addr,
2160                            bus_addr_t       mapping)
2161 {
2162     REG_WR(sc, addr, U64_LO(mapping));
2163     REG_WR(sc, (addr + 4), U64_HI(mapping));
2164 }
2165 
2166 static void
2167 storm_memset_spq_addr(struct bxe_softc *sc,
2168                       bus_addr_t       mapping,
2169                       uint16_t         abs_fid)
2170 {
2171     uint32_t addr = (XSEM_REG_FAST_MEMORY +
2172                      XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2173     __storm_memset_dma_mapping(sc, addr, mapping);
2174 }
2175 
2176 static void
2177 storm_memset_vf_to_pf(struct bxe_softc *sc,
2178                       uint16_t         abs_fid,
2179                       uint16_t         pf_id)
2180 {
2181     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2182     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2183     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2184     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2185 }
2186 
2187 static void
2188 storm_memset_func_en(struct bxe_softc *sc,
2189                      uint16_t         abs_fid,
2190                      uint8_t          enable)
2191 {
2192     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2193     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2194     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2195     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2196 }
2197 
2198 static void
2199 storm_memset_eq_data(struct bxe_softc       *sc,
2200                      struct event_ring_data *eq_data,
2201                      uint16_t               pfid)
2202 {
2203     uint32_t addr;
2204     size_t size;
2205 
2206     addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2207     size = sizeof(struct event_ring_data);
2208     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2209 }
2210 
2211 static void
2212 storm_memset_eq_prod(struct bxe_softc *sc,
2213                      uint16_t         eq_prod,
2214                      uint16_t         pfid)
2215 {
2216     uint32_t addr = (BAR_CSTRORM_INTMEM +
2217                      CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2218     REG_WR16(sc, addr, eq_prod);
2219 }
2220 
2221 /*
2222  * Post a slowpath command.
2223  *
2224  * A slowpath command is used to propagate a configuration change through
2225  * the controller in a controlled manner, allowing each STORM processor and
2226  * other H/W blocks to phase in the change.  The commands sent on the
2227  * slowpath are referred to as ramrods.  Depending on the ramrod used the
2228  * completion of the ramrod will occur in different ways.  Here's a
2229  * breakdown of ramrods and how they complete:
2230  *
2231  * RAMROD_CMD_ID_ETH_PORT_SETUP
2232  *   Used to setup the leading connection on a port.  Completes on the
2233  *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2234  *
2235  * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2236  *   Used to setup an additional connection on a port.  Completes on the
2237  *   RCQ of the multi-queue/RSS connection being initialized.
2238  *
2239  * RAMROD_CMD_ID_ETH_STAT_QUERY
2240  *   Used to force the storm processors to update the statistics database
2241  *   in host memory.  This ramrod is send on the leading connection CID and
2242  *   completes as an index increment of the CSTORM on the default status
2243  *   block.
2244  *
2245  * RAMROD_CMD_ID_ETH_UPDATE
2246  *   Used to update the state of the leading connection, usually to udpate
2247  *   the RSS indirection table.  Completes on the RCQ of the leading
2248  *   connection. (Not currently used under FreeBSD until OS support becomes
2249  *   available.)
2250  *
2251  * RAMROD_CMD_ID_ETH_HALT
2252  *   Used when tearing down a connection prior to driver unload.  Completes
2253  *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2254  *   use this on the leading connection.
2255  *
2256  * RAMROD_CMD_ID_ETH_SET_MAC
2257  *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2258  *   the RCQ of the leading connection.
2259  *
2260  * RAMROD_CMD_ID_ETH_CFC_DEL
2261  *   Used when tearing down a conneciton prior to driver unload.  Completes
2262  *   on the RCQ of the leading connection (since the current connection
2263  *   has been completely removed from controller memory).
2264  *
2265  * RAMROD_CMD_ID_ETH_PORT_DEL
2266  *   Used to tear down the leading connection prior to driver unload,
2267  *   typically fp[0].  Completes as an index increment of the CSTORM on the
2268  *   default status block.
2269  *
2270  * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2271  *   Used for connection offload.  Completes on the RCQ of the multi-queue
2272  *   RSS connection that is being offloaded.  (Not currently used under
2273  *   FreeBSD.)
2274  *
2275  * There can only be one command pending per function.
2276  *
2277  * Returns:
2278  *   0 = Success, !0 = Failure.
2279  */
2280 
2281 /* must be called under the spq lock */
2282 static inline
2283 struct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2284 {
2285     struct eth_spe *next_spe = sc->spq_prod_bd;
2286 
2287     if (sc->spq_prod_bd == sc->spq_last_bd) {
2288         /* wrap back to the first eth_spq */
2289         sc->spq_prod_bd = sc->spq;
2290         sc->spq_prod_idx = 0;
2291     } else {
2292         sc->spq_prod_bd++;
2293         sc->spq_prod_idx++;
2294     }
2295 
2296     return (next_spe);
2297 }
2298 
2299 /* must be called under the spq lock */
2300 static inline
2301 void bxe_sp_prod_update(struct bxe_softc *sc)
2302 {
2303     int func = SC_FUNC(sc);
2304 
2305     /*
2306      * Make sure that BD data is updated before writing the producer.
2307      * BD data is written to the memory, the producer is read from the
2308      * memory, thus we need a full memory barrier to ensure the ordering.
2309      */
2310     mb();
2311 
2312     REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2313              sc->spq_prod_idx);
2314 
2315     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2316                       BUS_SPACE_BARRIER_WRITE);
2317 }
2318 
2319 /**
2320  * bxe_is_contextless_ramrod - check if the current command ends on EQ
2321  *
2322  * @cmd:      command to check
2323  * @cmd_type: command type
2324  */
2325 static inline
2326 int bxe_is_contextless_ramrod(int cmd,
2327                               int cmd_type)
2328 {
2329     if ((cmd_type == NONE_CONNECTION_TYPE) ||
2330         (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2331         (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2332         (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2333         (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2334         (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2335         (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2336         return (TRUE);
2337     } else {
2338         return (FALSE);
2339     }
2340 }
2341 
2342 /**
2343  * bxe_sp_post - place a single command on an SP ring
2344  *
2345  * @sc:         driver handle
2346  * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2347  * @cid:        SW CID the command is related to
2348  * @data_hi:    command private data address (high 32 bits)
2349  * @data_lo:    command private data address (low 32 bits)
2350  * @cmd_type:   command type (e.g. NONE, ETH)
2351  *
2352  * SP data is handled as if it's always an address pair, thus data fields are
2353  * not swapped to little endian in upper functions. Instead this function swaps
2354  * data as if it's two uint32 fields.
2355  */
2356 int
2357 bxe_sp_post(struct bxe_softc *sc,
2358             int              command,
2359             int              cid,
2360             uint32_t         data_hi,
2361             uint32_t         data_lo,
2362             int              cmd_type)
2363 {
2364     struct eth_spe *spe;
2365     uint16_t type;
2366     int common;
2367 
2368     common = bxe_is_contextless_ramrod(command, cmd_type);
2369 
2370     BXE_SP_LOCK(sc);
2371 
2372     if (common) {
2373         if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2374             BLOGE(sc, "EQ ring is full!\n");
2375             BXE_SP_UNLOCK(sc);
2376             return (-1);
2377         }
2378     } else {
2379         if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2380             BLOGE(sc, "SPQ ring is full!\n");
2381             BXE_SP_UNLOCK(sc);
2382             return (-1);
2383         }
2384     }
2385 
2386     spe = bxe_sp_get_next(sc);
2387 
2388     /* CID needs port number to be encoded int it */
2389     spe->hdr.conn_and_cmd_data =
2390         htole32((command << SPE_HDR_T_CMD_ID_SHIFT) | HW_CID(sc, cid));
2391 
2392     type = (cmd_type << SPE_HDR_T_CONN_TYPE_SHIFT) & SPE_HDR_T_CONN_TYPE;
2393 
2394     /* TBD: Check if it works for VFs */
2395     type |= ((SC_FUNC(sc) << SPE_HDR_T_FUNCTION_ID_SHIFT) &
2396              SPE_HDR_T_FUNCTION_ID);
2397 
2398     spe->hdr.type = htole16(type);
2399 
2400     spe->data.update_data_addr.hi = htole32(data_hi);
2401     spe->data.update_data_addr.lo = htole32(data_lo);
2402 
2403     /*
2404      * It's ok if the actual decrement is issued towards the memory
2405      * somewhere between the lock and unlock. Thus no more explict
2406      * memory barrier is needed.
2407      */
2408     if (common) {
2409         atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2410     } else {
2411         atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2412     }
2413 
2414     BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2415     BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2416           BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2417     BLOGD(sc, DBG_SP,
2418           "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2419           sc->spq_prod_idx,
2420           (uint32_t)U64_HI(sc->spq_dma.paddr),
2421           (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2422           command,
2423           common,
2424           HW_CID(sc, cid),
2425           data_hi,
2426           data_lo,
2427           type,
2428           atomic_load_acq_long(&sc->cq_spq_left),
2429           atomic_load_acq_long(&sc->eq_spq_left));
2430 
2431     bxe_sp_prod_update(sc);
2432 
2433     BXE_SP_UNLOCK(sc);
2434     return (0);
2435 }
2436 
2437 /**
2438  * bxe_debug_print_ind_table - prints the indirection table configuration.
2439  *
2440  * @sc: driver hanlde
2441  * @p:  pointer to rss configuration
2442  */
2443 
2444 /*
2445  * FreeBSD Device probe function.
2446  *
2447  * Compares the device found to the driver's list of supported devices and
2448  * reports back to the bsd loader whether this is the right driver for the device.
2449  * This is the driver entry function called from the "kldload" command.
2450  *
2451  * Returns:
2452  *   BUS_PROBE_DEFAULT on success, positive value on failure.
2453  */
2454 static int
2455 bxe_probe(device_t dev)
2456 {
2457     struct bxe_device_type *t;
2458     char *descbuf;
2459     uint16_t did, sdid, svid, vid;
2460 
2461     /* Find our device structure */
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     /* Look through the list of known devices for a match. */
2471     while (t->bxe_name != NULL) {
2472         if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2473             ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2474             ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2475             descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2476             if (descbuf == NULL)
2477                 return (ENOMEM);
2478 
2479             /* Print out the device identity. */
2480             snprintf(descbuf, BXE_DEVDESC_MAX,
2481                      "%s (%c%d) BXE v:%s\n", t->bxe_name,
2482                      (((pci_read_config(dev, PCIR_REVID, 4) &
2483                         0xf0) >> 4) + 'A'),
2484                      (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2485                      BXE_DRIVER_VERSION);
2486 
2487             device_set_desc_copy(dev, descbuf);
2488             free(descbuf, M_TEMP);
2489             return (BUS_PROBE_DEFAULT);
2490         }
2491         t++;
2492     }
2493 
2494     return (ENXIO);
2495 }
2496 
2497 static void
2498 bxe_init_mutexes(struct bxe_softc *sc)
2499 {
2500 #ifdef BXE_CORE_LOCK_SX
2501     snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2502              "bxe%d_core_lock", sc->unit);
2503     sx_init(&sc->core_sx, sc->core_sx_name);
2504 #else
2505     snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2506              "bxe%d_core_lock", sc->unit);
2507     mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2508 #endif
2509 
2510     snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2511              "bxe%d_sp_lock", sc->unit);
2512     mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2513 
2514     snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2515              "bxe%d_dmae_lock", sc->unit);
2516     mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2517 
2518     snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2519              "bxe%d_phy_lock", sc->unit);
2520     mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2521 
2522     snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2523              "bxe%d_fwmb_lock", sc->unit);
2524     mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2525 
2526     snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2527              "bxe%d_print_lock", sc->unit);
2528     mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2529 
2530     snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2531              "bxe%d_stats_lock", sc->unit);
2532     mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2533 
2534     snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2535              "bxe%d_mcast_lock", sc->unit);
2536     mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2537 }
2538 
2539 static void
2540 bxe_release_mutexes(struct bxe_softc *sc)
2541 {
2542 #ifdef BXE_CORE_LOCK_SX
2543     sx_destroy(&sc->core_sx);
2544 #else
2545     if (mtx_initialized(&sc->core_mtx)) {
2546         mtx_destroy(&sc->core_mtx);
2547     }
2548 #endif
2549 
2550     if (mtx_initialized(&sc->sp_mtx)) {
2551         mtx_destroy(&sc->sp_mtx);
2552     }
2553 
2554     if (mtx_initialized(&sc->dmae_mtx)) {
2555         mtx_destroy(&sc->dmae_mtx);
2556     }
2557 
2558     if (mtx_initialized(&sc->port.phy_mtx)) {
2559         mtx_destroy(&sc->port.phy_mtx);
2560     }
2561 
2562     if (mtx_initialized(&sc->fwmb_mtx)) {
2563         mtx_destroy(&sc->fwmb_mtx);
2564     }
2565 
2566     if (mtx_initialized(&sc->print_mtx)) {
2567         mtx_destroy(&sc->print_mtx);
2568     }
2569 
2570     if (mtx_initialized(&sc->stats_mtx)) {
2571         mtx_destroy(&sc->stats_mtx);
2572     }
2573 
2574     if (mtx_initialized(&sc->mcast_mtx)) {
2575         mtx_destroy(&sc->mcast_mtx);
2576     }
2577 }
2578 
2579 static void
2580 bxe_tx_disable(struct bxe_softc* sc)
2581 {
2582     if_t ifp = sc->ifp;
2583 
2584     /* tell the stack the driver is stopped and TX queue is full */
2585     if (ifp !=  NULL) {
2586         if_setdrvflags(ifp, 0);
2587     }
2588 }
2589 
2590 static void
2591 bxe_drv_pulse(struct bxe_softc *sc)
2592 {
2593     SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2594              sc->fw_drv_pulse_wr_seq);
2595 }
2596 
2597 static inline uint16_t
2598 bxe_tx_avail(struct bxe_softc *sc,
2599              struct bxe_fastpath *fp)
2600 {
2601     int16_t  used;
2602     uint16_t prod;
2603     uint16_t cons;
2604 
2605     prod = fp->tx_bd_prod;
2606     cons = fp->tx_bd_cons;
2607 
2608     used = SUB_S16(prod, cons);
2609 
2610     return (int16_t)(sc->tx_ring_size) - used;
2611 }
2612 
2613 static inline int
2614 bxe_tx_queue_has_work(struct bxe_fastpath *fp)
2615 {
2616     uint16_t hw_cons;
2617 
2618     mb(); /* status block fields can change */
2619     hw_cons = le16toh(*fp->tx_cons_sb);
2620     return (hw_cons != fp->tx_pkt_cons);
2621 }
2622 
2623 static inline uint8_t
2624 bxe_has_tx_work(struct bxe_fastpath *fp)
2625 {
2626     /* expand this for multi-cos if ever supported */
2627     return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2628 }
2629 
2630 static inline int
2631 bxe_has_rx_work(struct bxe_fastpath *fp)
2632 {
2633     uint16_t rx_cq_cons_sb;
2634 
2635     mb(); /* status block fields can change */
2636     rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2637     if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2638         rx_cq_cons_sb++;
2639     return (fp->rx_cq_cons != rx_cq_cons_sb);
2640 }
2641 
2642 static void
2643 bxe_sp_event(struct bxe_softc    *sc,
2644              struct bxe_fastpath *fp,
2645              union eth_rx_cqe    *rr_cqe)
2646 {
2647     int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2648     int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2649     enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2650     struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2651 
2652     BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2653           fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2654 
2655     switch (command) {
2656     case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2657         BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2658         drv_cmd = ECORE_Q_CMD_UPDATE;
2659         break;
2660 
2661     case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2662         BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2663         drv_cmd = ECORE_Q_CMD_SETUP;
2664         break;
2665 
2666     case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2667         BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2668         drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2669         break;
2670 
2671     case (RAMROD_CMD_ID_ETH_HALT):
2672         BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2673         drv_cmd = ECORE_Q_CMD_HALT;
2674         break;
2675 
2676     case (RAMROD_CMD_ID_ETH_TERMINATE):
2677         BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2678         drv_cmd = ECORE_Q_CMD_TERMINATE;
2679         break;
2680 
2681     case (RAMROD_CMD_ID_ETH_EMPTY):
2682         BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2683         drv_cmd = ECORE_Q_CMD_EMPTY;
2684         break;
2685 
2686     default:
2687         BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2688               command, fp->index);
2689         return;
2690     }
2691 
2692     if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2693         q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2694         /*
2695          * q_obj->complete_cmd() failure means that this was
2696          * an unexpected completion.
2697          *
2698          * In this case we don't want to increase the sc->spq_left
2699          * because apparently we haven't sent this command the first
2700          * place.
2701          */
2702         // bxe_panic(sc, ("Unexpected SP completion\n"));
2703         return;
2704     }
2705 
2706     atomic_add_acq_long(&sc->cq_spq_left, 1);
2707 
2708     BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2709           atomic_load_acq_long(&sc->cq_spq_left));
2710 }
2711 
2712 /*
2713  * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2714  * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2715  * the current aggregation queue as in-progress.
2716  */
2717 static void
2718 bxe_tpa_start(struct bxe_softc            *sc,
2719               struct bxe_fastpath         *fp,
2720               uint16_t                    queue,
2721               uint16_t                    cons,
2722               uint16_t                    prod,
2723               struct eth_fast_path_rx_cqe *cqe)
2724 {
2725     struct bxe_sw_rx_bd tmp_bd;
2726     struct bxe_sw_rx_bd *rx_buf;
2727     struct eth_rx_bd *rx_bd;
2728     int max_agg_queues;
2729     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2730     uint16_t index;
2731 
2732     BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2733                        "cons=%d prod=%d\n",
2734           fp->index, queue, cons, prod);
2735 
2736     max_agg_queues = MAX_AGG_QS(sc);
2737 
2738     KASSERT((queue < max_agg_queues),
2739             ("fp[%02d] invalid aggr queue (%d >= %d)!",
2740              fp->index, queue, max_agg_queues));
2741 
2742     KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2743             ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2744              fp->index, queue));
2745 
2746     /* copy the existing mbuf and mapping from the TPA pool */
2747     tmp_bd = tpa_info->bd;
2748 
2749     if (tmp_bd.m == NULL) {
2750         uint32_t *tmp;
2751 
2752         tmp = (uint32_t *)cqe;
2753 
2754         BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2755               fp->index, queue, cons, prod);
2756         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2757             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2758 
2759         /* XXX Error handling? */
2760         return;
2761     }
2762 
2763     /* change the TPA queue to the start state */
2764     tpa_info->state            = BXE_TPA_STATE_START;
2765     tpa_info->placement_offset = cqe->placement_offset;
2766     tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2767     tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2768     tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2769 
2770     fp->rx_tpa_queue_used |= (1 << queue);
2771 
2772     /*
2773      * If all the buffer descriptors are filled with mbufs then fill in
2774      * the current consumer index with a new BD. Else if a maximum Rx
2775      * buffer limit is imposed then fill in the next producer index.
2776      */
2777     index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2778                 prod : cons;
2779 
2780     /* move the received mbuf and mapping to TPA pool */
2781     tpa_info->bd = fp->rx_mbuf_chain[cons];
2782 
2783     /* release any existing RX BD mbuf mappings */
2784     if (cons != index) {
2785         rx_buf = &fp->rx_mbuf_chain[cons];
2786 
2787         if (rx_buf->m_map != NULL) {
2788             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2789                             BUS_DMASYNC_POSTREAD);
2790             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2791         }
2792 
2793         /*
2794          * We get here when the maximum number of rx buffers is less than
2795          * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2796          * it out here without concern of a memory leak.
2797          */
2798         fp->rx_mbuf_chain[cons].m = NULL;
2799     }
2800 
2801     /* update the Rx SW BD with the mbuf info from the TPA pool */
2802     fp->rx_mbuf_chain[index] = tmp_bd;
2803 
2804     /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2805     rx_bd = &fp->rx_chain[index];
2806     rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2807     rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2808 }
2809 
2810 /*
2811  * When a TPA aggregation is completed, loop through the individual mbufs
2812  * of the aggregation, combining them into a single mbuf which will be sent
2813  * up the stack. Refill all freed SGEs with mbufs as we go along.
2814  */
2815 static int
2816 bxe_fill_frag_mbuf(struct bxe_softc          *sc,
2817                    struct bxe_fastpath       *fp,
2818                    struct bxe_sw_tpa_info    *tpa_info,
2819                    uint16_t                  queue,
2820                    uint16_t                  pages,
2821                    struct mbuf               *m,
2822 			       struct eth_end_agg_rx_cqe *cqe,
2823                    uint16_t                  cqe_idx)
2824 {
2825     struct mbuf *m_frag;
2826     uint32_t frag_len, frag_size, i;
2827     uint16_t sge_idx;
2828     int rc = 0;
2829     int j;
2830 
2831     frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2832 
2833     BLOGD(sc, DBG_LRO,
2834           "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2835           fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2836 
2837     /* make sure the aggregated frame is not too big to handle */
2838     if (pages > 8 * PAGES_PER_SGE) {
2839 
2840         uint32_t *tmp = (uint32_t *)cqe;
2841 
2842         BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2843                   "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2844               fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2845               tpa_info->len_on_bd, frag_size);
2846 
2847         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2848             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2849 
2850         bxe_panic(sc, ("sge page count error\n"));
2851         return (EINVAL);
2852     }
2853 
2854     /*
2855      * Scan through the scatter gather list pulling individual mbufs into a
2856      * single mbuf for the host stack.
2857      */
2858     for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2859         sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2860 
2861         /*
2862          * Firmware gives the indices of the SGE as if the ring is an array
2863          * (meaning that the "next" element will consume 2 indices).
2864          */
2865         frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2866 
2867         BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2868                            "sge_idx=%d frag_size=%d frag_len=%d\n",
2869               fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2870 
2871         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2872 
2873         /* allocate a new mbuf for the SGE */
2874         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2875         if (rc) {
2876             /* Leave all remaining SGEs in the ring! */
2877             return (rc);
2878         }
2879 
2880         /* update the fragment length */
2881         m_frag->m_len = frag_len;
2882 
2883         /* concatenate the fragment to the head mbuf */
2884         m_cat(m, m_frag);
2885         fp->eth_q_stats.mbuf_alloc_sge--;
2886 
2887         /* update the TPA mbuf size and remaining fragment size */
2888         m->m_pkthdr.len += frag_len;
2889         frag_size -= frag_len;
2890     }
2891 
2892     BLOGD(sc, DBG_LRO,
2893           "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2894           fp->index, queue, frag_size);
2895 
2896     return (rc);
2897 }
2898 
2899 static inline void
2900 bxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2901 {
2902     int i, j;
2903 
2904     for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2905         int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2906 
2907         for (j = 0; j < 2; j++) {
2908             BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2909             idx--;
2910         }
2911     }
2912 }
2913 
2914 static inline void
2915 bxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2916 {
2917     /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2918     memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2919 
2920     /*
2921      * Clear the two last indices in the page to 1. These are the indices that
2922      * correspond to the "next" element, hence will never be indicated and
2923      * should be removed from the calculations.
2924      */
2925     bxe_clear_sge_mask_next_elems(fp);
2926 }
2927 
2928 static inline void
2929 bxe_update_last_max_sge(struct bxe_fastpath *fp,
2930                         uint16_t            idx)
2931 {
2932     uint16_t last_max = fp->last_max_sge;
2933 
2934     if (SUB_S16(idx, last_max) > 0) {
2935         fp->last_max_sge = idx;
2936     }
2937 }
2938 
2939 static inline void
2940 bxe_update_sge_prod(struct bxe_softc          *sc,
2941                     struct bxe_fastpath       *fp,
2942                     uint16_t                  sge_len,
2943                     union eth_sgl_or_raw_data *cqe)
2944 {
2945     uint16_t last_max, last_elem, first_elem;
2946     uint16_t delta = 0;
2947     uint16_t i;
2948 
2949     if (!sge_len) {
2950         return;
2951     }
2952 
2953     /* first mark all used pages */
2954     for (i = 0; i < sge_len; i++) {
2955         BIT_VEC64_CLEAR_BIT(fp->sge_mask,
2956                             RX_SGE(le16toh(cqe->sgl[i])));
2957     }
2958 
2959     BLOGD(sc, DBG_LRO,
2960           "fp[%02d] fp_cqe->sgl[%d] = %d\n",
2961           fp->index, sge_len - 1,
2962           le16toh(cqe->sgl[sge_len - 1]));
2963 
2964     /* assume that the last SGE index is the biggest */
2965     bxe_update_last_max_sge(fp,
2966                             le16toh(cqe->sgl[sge_len - 1]));
2967 
2968     last_max = RX_SGE(fp->last_max_sge);
2969     last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
2970     first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
2971 
2972     /* if ring is not full */
2973     if (last_elem + 1 != first_elem) {
2974         last_elem++;
2975     }
2976 
2977     /* now update the prod */
2978     for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
2979         if (__predict_true(fp->sge_mask[i])) {
2980             break;
2981         }
2982 
2983         fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
2984         delta += BIT_VEC64_ELEM_SZ;
2985     }
2986 
2987     if (delta > 0) {
2988         fp->rx_sge_prod += delta;
2989         /* clear page-end entries */
2990         bxe_clear_sge_mask_next_elems(fp);
2991     }
2992 
2993     BLOGD(sc, DBG_LRO,
2994           "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
2995           fp->index, fp->last_max_sge, fp->rx_sge_prod);
2996 }
2997 
2998 /*
2999  * The aggregation on the current TPA queue has completed. Pull the individual
3000  * mbuf fragments together into a single mbuf, perform all necessary checksum
3001  * calculations, and send the resuting mbuf to the stack.
3002  */
3003 static void
3004 bxe_tpa_stop(struct bxe_softc          *sc,
3005              struct bxe_fastpath       *fp,
3006              struct bxe_sw_tpa_info    *tpa_info,
3007              uint16_t                  queue,
3008              uint16_t                  pages,
3009 			 struct eth_end_agg_rx_cqe *cqe,
3010              uint16_t                  cqe_idx)
3011 {
3012     if_t ifp = sc->ifp;
3013     struct mbuf *m;
3014     int rc = 0;
3015 
3016     BLOGD(sc, DBG_LRO,
3017           "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3018           fp->index, queue, tpa_info->placement_offset,
3019           le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3020 
3021     m = tpa_info->bd.m;
3022 
3023     /* allocate a replacement before modifying existing mbuf */
3024     rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3025     if (rc) {
3026         /* drop the frame and log an error */
3027         fp->eth_q_stats.rx_soft_errors++;
3028         goto bxe_tpa_stop_exit;
3029     }
3030 
3031     /* we have a replacement, fixup the current mbuf */
3032     m_adj(m, tpa_info->placement_offset);
3033     m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3034 
3035     /* mark the checksums valid (taken care of by the firmware) */
3036     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3037     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3038     m->m_pkthdr.csum_data = 0xffff;
3039     m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3040                                CSUM_IP_VALID   |
3041                                CSUM_DATA_VALID |
3042                                CSUM_PSEUDO_HDR);
3043 
3044     /* aggregate all of the SGEs into a single mbuf */
3045     rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3046     if (rc) {
3047         /* drop the packet and log an error */
3048         fp->eth_q_stats.rx_soft_errors++;
3049         m_freem(m);
3050     } else {
3051         if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3052             m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3053             m->m_flags |= M_VLANTAG;
3054         }
3055 
3056         /* assign packet to this interface interface */
3057         if_setrcvif(m, ifp);
3058 
3059 #if __FreeBSD_version >= 800000
3060         /* specify what RSS queue was used for this flow */
3061         m->m_pkthdr.flowid = fp->index;
3062         BXE_SET_FLOWID(m);
3063 #endif
3064 
3065         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3066         fp->eth_q_stats.rx_tpa_pkts++;
3067 
3068         /* pass the frame to the stack */
3069         if_input(ifp, m);
3070     }
3071 
3072     /* we passed an mbuf up the stack or dropped the frame */
3073     fp->eth_q_stats.mbuf_alloc_tpa--;
3074 
3075 bxe_tpa_stop_exit:
3076 
3077     fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3078     fp->rx_tpa_queue_used &= ~(1 << queue);
3079 }
3080 
3081 static uint8_t
3082 bxe_service_rxsgl(
3083                  struct bxe_fastpath *fp,
3084                  uint16_t len,
3085                  uint16_t lenonbd,
3086                  struct mbuf *m,
3087                  struct eth_fast_path_rx_cqe *cqe_fp)
3088 {
3089     struct mbuf *m_frag;
3090     uint16_t frags, frag_len;
3091     uint16_t sge_idx = 0;
3092     uint16_t j;
3093     uint8_t i, rc = 0;
3094     uint32_t frag_size;
3095 
3096     /* adjust the mbuf */
3097     m->m_len = lenonbd;
3098 
3099     frag_size =  len - lenonbd;
3100     frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3101 
3102     for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3103         sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3104 
3105         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3106         frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3107         m_frag->m_len = frag_len;
3108 
3109        /* allocate a new mbuf for the SGE */
3110         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3111         if (rc) {
3112             /* Leave all remaining SGEs in the ring! */
3113             return (rc);
3114         }
3115         fp->eth_q_stats.mbuf_alloc_sge--;
3116 
3117         /* concatenate the fragment to the head mbuf */
3118         m_cat(m, m_frag);
3119 
3120         frag_size -= frag_len;
3121     }
3122 
3123     bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3124 
3125     return rc;
3126 }
3127 
3128 static uint8_t
3129 bxe_rxeof(struct bxe_softc    *sc,
3130           struct bxe_fastpath *fp)
3131 {
3132     if_t ifp = sc->ifp;
3133     uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3134     uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3135     int rx_pkts = 0;
3136     int rc = 0;
3137 
3138     BXE_FP_RX_LOCK(fp);
3139 
3140     /* CQ "next element" is of the size of the regular element */
3141     hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3142     if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3143         hw_cq_cons++;
3144     }
3145 
3146     bd_cons = fp->rx_bd_cons;
3147     bd_prod = fp->rx_bd_prod;
3148     bd_prod_fw = bd_prod;
3149     sw_cq_cons = fp->rx_cq_cons;
3150     sw_cq_prod = fp->rx_cq_prod;
3151 
3152     /*
3153      * Memory barrier necessary as speculative reads of the rx
3154      * buffer can be ahead of the index in the status block
3155      */
3156     rmb();
3157 
3158     BLOGD(sc, DBG_RX,
3159           "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3160           fp->index, hw_cq_cons, sw_cq_cons);
3161 
3162     while (sw_cq_cons != hw_cq_cons) {
3163         struct bxe_sw_rx_bd *rx_buf = NULL;
3164         union eth_rx_cqe *cqe;
3165         struct eth_fast_path_rx_cqe *cqe_fp;
3166         uint8_t cqe_fp_flags;
3167         enum eth_rx_cqe_type cqe_fp_type;
3168         uint16_t len, lenonbd,  pad;
3169         struct mbuf *m = NULL;
3170 
3171         comp_ring_cons = RCQ(sw_cq_cons);
3172         bd_prod = RX_BD(bd_prod);
3173         bd_cons = RX_BD(bd_cons);
3174 
3175         cqe          = &fp->rcq_chain[comp_ring_cons];
3176         cqe_fp       = &cqe->fast_path_cqe;
3177         cqe_fp_flags = cqe_fp->type_error_flags;
3178         cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3179 
3180         BLOGD(sc, DBG_RX,
3181               "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3182               "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3183               "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3184               fp->index,
3185               hw_cq_cons,
3186               sw_cq_cons,
3187               bd_prod,
3188               bd_cons,
3189               CQE_TYPE(cqe_fp_flags),
3190               cqe_fp_flags,
3191               cqe_fp->status_flags,
3192               le32toh(cqe_fp->rss_hash_result),
3193               le16toh(cqe_fp->vlan_tag),
3194               le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3195               le16toh(cqe_fp->len_on_bd));
3196 
3197         /* is this a slowpath msg? */
3198         if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3199             bxe_sp_event(sc, fp, cqe);
3200             goto next_cqe;
3201         }
3202 
3203         rx_buf = &fp->rx_mbuf_chain[bd_cons];
3204 
3205         if (!CQE_TYPE_FAST(cqe_fp_type)) {
3206             struct bxe_sw_tpa_info *tpa_info;
3207             uint16_t frag_size, pages;
3208             uint8_t queue;
3209 
3210             if (CQE_TYPE_START(cqe_fp_type)) {
3211                 bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3212                               bd_cons, bd_prod, cqe_fp);
3213                 m = NULL; /* packet not ready yet */
3214                 goto next_rx;
3215             }
3216 
3217             KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3218                     ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3219 
3220             queue = cqe->end_agg_cqe.queue_index;
3221             tpa_info = &fp->rx_tpa_info[queue];
3222 
3223             BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3224                   fp->index, queue);
3225 
3226             frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3227                          tpa_info->len_on_bd);
3228             pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3229 
3230             bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3231                          &cqe->end_agg_cqe, comp_ring_cons);
3232 
3233             bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3234 
3235             goto next_cqe;
3236         }
3237 
3238         /* non TPA */
3239 
3240         /* is this an error packet? */
3241         if (__predict_false(cqe_fp_flags &
3242                             ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3243             BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3244             fp->eth_q_stats.rx_soft_errors++;
3245             goto next_rx;
3246         }
3247 
3248         len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3249         lenonbd = le16toh(cqe_fp->len_on_bd);
3250         pad = cqe_fp->placement_offset;
3251 
3252         m = rx_buf->m;
3253 
3254         if (__predict_false(m == NULL)) {
3255             BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3256                   bd_cons, fp->index);
3257             goto next_rx;
3258         }
3259 
3260         /* XXX double copy if packet length under a threshold */
3261 
3262         /*
3263          * If all the buffer descriptors are filled with mbufs then fill in
3264          * the current consumer index with a new BD. Else if a maximum Rx
3265          * buffer limit is imposed then fill in the next producer index.
3266          */
3267         rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3268                                   (sc->max_rx_bufs != RX_BD_USABLE) ?
3269                                       bd_prod : bd_cons);
3270         if (rc != 0) {
3271 
3272             /* we simply reuse the received mbuf and don't post it to the stack */
3273             m = NULL;
3274 
3275             BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3276                   fp->index, rc);
3277             fp->eth_q_stats.rx_soft_errors++;
3278 
3279             if (sc->max_rx_bufs != RX_BD_USABLE) {
3280                 /* copy this consumer index to the producer index */
3281                 memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3282                        sizeof(struct bxe_sw_rx_bd));
3283                 memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3284             }
3285 
3286             goto next_rx;
3287         }
3288 
3289         /* current mbuf was detached from the bd */
3290         fp->eth_q_stats.mbuf_alloc_rx--;
3291 
3292         /* we allocated a replacement mbuf, fixup the current one */
3293         m_adj(m, pad);
3294         m->m_pkthdr.len = m->m_len = len;
3295 
3296         if ((len > 60) && (len > lenonbd)) {
3297             fp->eth_q_stats.rx_bxe_service_rxsgl++;
3298             rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3299             if (rc)
3300                 break;
3301             fp->eth_q_stats.rx_jumbo_sge_pkts++;
3302         } else if (lenonbd < len) {
3303             fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3304         }
3305 
3306         /* assign packet to this interface interface */
3307 	if_setrcvif(m, ifp);
3308 
3309         /* assume no hardware checksum has complated */
3310         m->m_pkthdr.csum_flags = 0;
3311 
3312         /* validate checksum if offload enabled */
3313         if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3314             /* check for a valid IP frame */
3315             if (!(cqe->fast_path_cqe.status_flags &
3316                   ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3317                 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3318                 if (__predict_false(cqe_fp_flags &
3319                                     ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3320                     fp->eth_q_stats.rx_hw_csum_errors++;
3321                 } else {
3322                     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3323                     m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3324                 }
3325             }
3326 
3327             /* check for a valid TCP/UDP frame */
3328             if (!(cqe->fast_path_cqe.status_flags &
3329                   ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3330                 if (__predict_false(cqe_fp_flags &
3331                                     ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3332                     fp->eth_q_stats.rx_hw_csum_errors++;
3333                 } else {
3334                     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3335                     m->m_pkthdr.csum_data = 0xFFFF;
3336                     m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3337                                                CSUM_PSEUDO_HDR);
3338                 }
3339             }
3340         }
3341 
3342         /* if there is a VLAN tag then flag that info */
3343         if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3344             m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3345             m->m_flags |= M_VLANTAG;
3346         }
3347 
3348 #if __FreeBSD_version >= 800000
3349         /* specify what RSS queue was used for this flow */
3350         m->m_pkthdr.flowid = fp->index;
3351         BXE_SET_FLOWID(m);
3352 #endif
3353 
3354 next_rx:
3355 
3356         bd_cons    = RX_BD_NEXT(bd_cons);
3357         bd_prod    = RX_BD_NEXT(bd_prod);
3358         bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3359 
3360         /* pass the frame to the stack */
3361         if (__predict_true(m != NULL)) {
3362             if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3363             rx_pkts++;
3364             if_input(ifp, m);
3365         }
3366 
3367 next_cqe:
3368 
3369         sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3370         sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3371 
3372         /* limit spinning on the queue */
3373         if (rc != 0)
3374             break;
3375 
3376         if (rx_pkts == sc->rx_budget) {
3377             fp->eth_q_stats.rx_budget_reached++;
3378             break;
3379         }
3380     } /* while work to do */
3381 
3382     fp->rx_bd_cons = bd_cons;
3383     fp->rx_bd_prod = bd_prod_fw;
3384     fp->rx_cq_cons = sw_cq_cons;
3385     fp->rx_cq_prod = sw_cq_prod;
3386 
3387     /* Update producers */
3388     bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3389 
3390     fp->eth_q_stats.rx_pkts += rx_pkts;
3391     fp->eth_q_stats.rx_calls++;
3392 
3393     BXE_FP_RX_UNLOCK(fp);
3394 
3395     return (sw_cq_cons != hw_cq_cons);
3396 }
3397 
3398 static uint16_t
3399 bxe_free_tx_pkt(struct bxe_softc    *sc,
3400                 struct bxe_fastpath *fp,
3401                 uint16_t            idx)
3402 {
3403     struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3404     struct eth_tx_start_bd *tx_start_bd;
3405     uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3406     uint16_t new_cons;
3407     int nbd;
3408 
3409     /* unmap the mbuf from non-paged memory */
3410     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3411 
3412     tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3413     nbd = le16toh(tx_start_bd->nbd) - 1;
3414 
3415     new_cons = (tx_buf->first_bd + nbd);
3416 
3417     /* free the mbuf */
3418     if (__predict_true(tx_buf->m != NULL)) {
3419         m_freem(tx_buf->m);
3420         fp->eth_q_stats.mbuf_alloc_tx--;
3421     } else {
3422         fp->eth_q_stats.tx_chain_lost_mbuf++;
3423     }
3424 
3425     tx_buf->m = NULL;
3426     tx_buf->first_bd = 0;
3427 
3428     return (new_cons);
3429 }
3430 
3431 /* transmit timeout watchdog */
3432 static int
3433 bxe_watchdog(struct bxe_softc    *sc,
3434              struct bxe_fastpath *fp)
3435 {
3436     BXE_FP_TX_LOCK(fp);
3437 
3438     if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3439         BXE_FP_TX_UNLOCK(fp);
3440         return (0);
3441     }
3442 
3443     BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3444     if(sc->trigger_grcdump) {
3445          /* taking grcdump */
3446          bxe_grc_dump(sc);
3447     }
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 successfully - 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 void
4426 bxe_handle_chip_tq(void *context,
4427                    int  pending)
4428 {
4429     struct bxe_softc *sc = (struct bxe_softc *)context;
4430     long work = atomic_load_acq_long(&sc->chip_tq_flags);
4431 
4432     switch (work)
4433     {
4434 
4435     case CHIP_TQ_REINIT:
4436         if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4437             /* restart the interface */
4438             BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4439             bxe_periodic_stop(sc);
4440             BXE_CORE_LOCK(sc);
4441             bxe_stop_locked(sc);
4442             bxe_init_locked(sc);
4443             BXE_CORE_UNLOCK(sc);
4444         }
4445         break;
4446 
4447     default:
4448         break;
4449     }
4450 }
4451 
4452 /*
4453  * Handles any IOCTL calls from the operating system.
4454  *
4455  * Returns:
4456  *   0 = Success, >0 Failure
4457  */
4458 static int
4459 bxe_ioctl(if_t ifp,
4460           u_long       command,
4461           caddr_t      data)
4462 {
4463     struct bxe_softc *sc = if_getsoftc(ifp);
4464     struct ifreq *ifr = (struct ifreq *)data;
4465     int mask = 0;
4466     int reinit = 0;
4467     int error = 0;
4468 
4469     int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4470     int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4471 
4472     switch (command)
4473     {
4474     case SIOCSIFMTU:
4475         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4476               ifr->ifr_mtu);
4477 
4478         if (sc->mtu == ifr->ifr_mtu) {
4479             /* nothing to change */
4480             break;
4481         }
4482 
4483         if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4484             BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4485                   ifr->ifr_mtu, mtu_min, mtu_max);
4486             error = EINVAL;
4487             break;
4488         }
4489 
4490         atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4491                              (unsigned long)ifr->ifr_mtu);
4492 	/*
4493         atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4494                               (unsigned long)ifr->ifr_mtu);
4495 	XXX - Not sure why it needs to be atomic
4496 	*/
4497 	if_setmtu(ifp, ifr->ifr_mtu);
4498         reinit = 1;
4499         break;
4500 
4501     case SIOCSIFFLAGS:
4502         /* toggle the interface state up or down */
4503         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4504 
4505 	BXE_CORE_LOCK(sc);
4506         /* check if the interface is up */
4507         if (if_getflags(ifp) & IFF_UP) {
4508             if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4509                 /* set the receive mode flags */
4510                 bxe_set_rx_mode(sc);
4511             } else if(sc->state != BXE_STATE_DISABLED) {
4512 		bxe_init_locked(sc);
4513             }
4514         } else {
4515             if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4516 		bxe_periodic_stop(sc);
4517 		bxe_stop_locked(sc);
4518             }
4519         }
4520 	BXE_CORE_UNLOCK(sc);
4521 
4522         break;
4523 
4524     case SIOCADDMULTI:
4525     case SIOCDELMULTI:
4526         /* add/delete multicast addresses */
4527         BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4528 
4529         /* check if the interface is up */
4530         if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4531             /* set the receive mode flags */
4532 	    BXE_CORE_LOCK(sc);
4533             bxe_set_rx_mode(sc);
4534 	    BXE_CORE_UNLOCK(sc);
4535         }
4536 
4537         break;
4538 
4539     case SIOCSIFCAP:
4540         /* find out which capabilities have changed */
4541         mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4542 
4543         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4544               mask);
4545 
4546         /* toggle the LRO capabilites enable flag */
4547         if (mask & IFCAP_LRO) {
4548 	    if_togglecapenable(ifp, IFCAP_LRO);
4549             BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4550                   (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4551             reinit = 1;
4552         }
4553 
4554         /* toggle the TXCSUM checksum capabilites enable flag */
4555         if (mask & IFCAP_TXCSUM) {
4556 	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4557             BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4558                   (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4559             if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4560                 if_sethwassistbits(ifp, (CSUM_IP      |
4561                                     CSUM_TCP      |
4562                                     CSUM_UDP      |
4563                                     CSUM_TSO      |
4564                                     CSUM_TCP_IPV6 |
4565                                     CSUM_UDP_IPV6), 0);
4566             } else {
4567 		if_clearhwassist(ifp); /* XXX */
4568             }
4569         }
4570 
4571         /* toggle the RXCSUM checksum capabilities enable flag */
4572         if (mask & IFCAP_RXCSUM) {
4573 	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4574             BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4575                   (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4576             if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4577                 if_sethwassistbits(ifp, (CSUM_IP      |
4578                                     CSUM_TCP      |
4579                                     CSUM_UDP      |
4580                                     CSUM_TSO      |
4581                                     CSUM_TCP_IPV6 |
4582                                     CSUM_UDP_IPV6), 0);
4583             } else {
4584 		if_clearhwassist(ifp); /* XXX */
4585             }
4586         }
4587 
4588         /* toggle TSO4 capabilities enabled flag */
4589         if (mask & IFCAP_TSO4) {
4590             if_togglecapenable(ifp, IFCAP_TSO4);
4591             BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4592                   (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4593         }
4594 
4595         /* toggle TSO6 capabilities enabled flag */
4596         if (mask & IFCAP_TSO6) {
4597 	    if_togglecapenable(ifp, IFCAP_TSO6);
4598             BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4599                   (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4600         }
4601 
4602         /* toggle VLAN_HWTSO capabilities enabled flag */
4603         if (mask & IFCAP_VLAN_HWTSO) {
4604 
4605 	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4606             BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4607                   (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4608         }
4609 
4610         /* toggle VLAN_HWCSUM capabilities enabled flag */
4611         if (mask & IFCAP_VLAN_HWCSUM) {
4612             /* XXX investigate this... */
4613             BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4614             error = EINVAL;
4615         }
4616 
4617         /* toggle VLAN_MTU capabilities enable flag */
4618         if (mask & IFCAP_VLAN_MTU) {
4619             /* XXX investigate this... */
4620             BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4621             error = EINVAL;
4622         }
4623 
4624         /* toggle VLAN_HWTAGGING capabilities enabled flag */
4625         if (mask & IFCAP_VLAN_HWTAGGING) {
4626             /* XXX investigate this... */
4627             BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4628             error = EINVAL;
4629         }
4630 
4631         /* toggle VLAN_HWFILTER capabilities enabled flag */
4632         if (mask & IFCAP_VLAN_HWFILTER) {
4633             /* XXX investigate this... */
4634             BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4635             error = EINVAL;
4636         }
4637 
4638         /* XXX not yet...
4639          * IFCAP_WOL_MAGIC
4640          */
4641 
4642         break;
4643 
4644     case SIOCSIFMEDIA:
4645     case SIOCGIFMEDIA:
4646         /* set/get interface media */
4647         BLOGD(sc, DBG_IOCTL,
4648               "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4649               (command & 0xff));
4650         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4651         break;
4652 
4653     default:
4654         BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4655               (command & 0xff));
4656         error = ether_ioctl(ifp, command, data);
4657         break;
4658     }
4659 
4660     if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
4661         BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4662               "Re-initializing hardware from IOCTL change\n");
4663 	bxe_periodic_stop(sc);
4664 	BXE_CORE_LOCK(sc);
4665 	bxe_stop_locked(sc);
4666 	bxe_init_locked(sc);
4667 	BXE_CORE_UNLOCK(sc);
4668     }
4669 
4670     return (error);
4671 }
4672 
4673 static __noinline void
4674 bxe_dump_mbuf(struct bxe_softc *sc,
4675               struct mbuf      *m,
4676               uint8_t          contents)
4677 {
4678     char * type;
4679     int i = 0;
4680 
4681     if (!(sc->debug & DBG_MBUF)) {
4682         return;
4683     }
4684 
4685     if (m == NULL) {
4686         BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4687         return;
4688     }
4689 
4690     while (m) {
4691 
4692 #if __FreeBSD_version >= 1000000
4693         BLOGD(sc, DBG_MBUF,
4694               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4695               i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
4696 
4697         if (m->m_flags & M_PKTHDR) {
4698              BLOGD(sc, DBG_MBUF,
4699                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4700                    i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
4701                    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
4702         }
4703 #else
4704         BLOGD(sc, DBG_MBUF,
4705               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4706               i, m, m->m_len, m->m_flags,
4707               "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
4708 
4709         if (m->m_flags & M_PKTHDR) {
4710              BLOGD(sc, DBG_MBUF,
4711                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4712                    i, m->m_pkthdr.len, m->m_flags,
4713                    "\20\12M_BCAST\13M_MCAST\14M_FRAG"
4714                    "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
4715                    "\22M_PROMISC\23M_NOFREE",
4716                    (int)m->m_pkthdr.csum_flags,
4717                    "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
4718                    "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
4719                    "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
4720                    "\14CSUM_PSEUDO_HDR");
4721         }
4722 #endif /* #if __FreeBSD_version >= 1000000 */
4723 
4724         if (m->m_flags & M_EXT) {
4725             switch (m->m_ext.ext_type) {
4726             case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4727             case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4728             case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4729             case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4730             case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4731             case EXT_PACKET:     type = "EXT_PACKET";     break;
4732             case EXT_MBUF:       type = "EXT_MBUF";       break;
4733             case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4734             case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4735             case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4736             case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4737             default:             type = "UNKNOWN";        break;
4738             }
4739 
4740             BLOGD(sc, DBG_MBUF,
4741                   "%02d: - m_ext: %p ext_size=%d type=%s\n",
4742                   i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4743         }
4744 
4745         if (contents) {
4746             bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4747         }
4748 
4749         m = m->m_next;
4750         i++;
4751     }
4752 }
4753 
4754 /*
4755  * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4756  * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4757  * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4758  * The headers comes in a separate bd in FreeBSD so 13-3=10.
4759  * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4760  */
4761 static int
4762 bxe_chktso_window(struct bxe_softc  *sc,
4763                   int               nsegs,
4764                   bus_dma_segment_t *segs,
4765                   struct mbuf       *m)
4766 {
4767     uint32_t num_wnds, wnd_size, wnd_sum;
4768     int32_t frag_idx, wnd_idx;
4769     unsigned short lso_mss;
4770     int defrag;
4771 
4772     defrag = 0;
4773     wnd_sum = 0;
4774     wnd_size = 10;
4775     num_wnds = nsegs - wnd_size;
4776     lso_mss = htole16(m->m_pkthdr.tso_segsz);
4777 
4778     /*
4779      * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4780      * first window sum of data while skipping the first assuming it is the
4781      * header in FreeBSD.
4782      */
4783     for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4784         wnd_sum += htole16(segs[frag_idx].ds_len);
4785     }
4786 
4787     /* check the first 10 bd window size */
4788     if (wnd_sum < lso_mss) {
4789         return (1);
4790     }
4791 
4792     /* run through the windows */
4793     for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4794         /* subtract the first mbuf->m_len of the last wndw(-header) */
4795         wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4796         /* add the next mbuf len to the len of our new window */
4797         wnd_sum += htole16(segs[frag_idx].ds_len);
4798         if (wnd_sum < lso_mss) {
4799             return (1);
4800         }
4801     }
4802 
4803     return (0);
4804 }
4805 
4806 static uint8_t
4807 bxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4808                     struct mbuf         *m,
4809                     uint32_t            *parsing_data)
4810 {
4811     struct ether_vlan_header *eh = NULL;
4812     struct ip *ip4 = NULL;
4813     struct ip6_hdr *ip6 = NULL;
4814     caddr_t ip = NULL;
4815     struct tcphdr *th = NULL;
4816     int e_hlen, ip_hlen, l4_off;
4817     uint16_t proto;
4818 
4819     if (m->m_pkthdr.csum_flags == CSUM_IP) {
4820         /* no L4 checksum offload needed */
4821         return (0);
4822     }
4823 
4824     /* get the Ethernet header */
4825     eh = mtod(m, struct ether_vlan_header *);
4826 
4827     /* handle VLAN encapsulation if present */
4828     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4829         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4830         proto  = ntohs(eh->evl_proto);
4831     } else {
4832         e_hlen = ETHER_HDR_LEN;
4833         proto  = ntohs(eh->evl_encap_proto);
4834     }
4835 
4836     switch (proto) {
4837     case ETHERTYPE_IP:
4838         /* get the IP header, if mbuf len < 20 then header in next mbuf */
4839         ip4 = (m->m_len < sizeof(struct ip)) ?
4840                   (struct ip *)m->m_next->m_data :
4841                   (struct ip *)(m->m_data + e_hlen);
4842         /* ip_hl is number of 32-bit words */
4843         ip_hlen = (ip4->ip_hl << 2);
4844         ip = (caddr_t)ip4;
4845         break;
4846     case ETHERTYPE_IPV6:
4847         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4848         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4849                   (struct ip6_hdr *)m->m_next->m_data :
4850                   (struct ip6_hdr *)(m->m_data + e_hlen);
4851         /* XXX cannot support offload with IPv6 extensions */
4852         ip_hlen = sizeof(struct ip6_hdr);
4853         ip = (caddr_t)ip6;
4854         break;
4855     default:
4856         /* We can't offload in this case... */
4857         /* XXX error stat ??? */
4858         return (0);
4859     }
4860 
4861     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4862     l4_off = (e_hlen + ip_hlen);
4863 
4864     *parsing_data |=
4865         (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
4866          ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
4867 
4868     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4869                                   CSUM_TSO |
4870                                   CSUM_TCP_IPV6)) {
4871         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4872         th = (struct tcphdr *)(ip + ip_hlen);
4873         /* th_off is number of 32-bit words */
4874         *parsing_data |= ((th->th_off <<
4875                            ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
4876                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
4877         return (l4_off + (th->th_off << 2)); /* entire header length */
4878     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4879                                          CSUM_UDP_IPV6)) {
4880         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4881         return (l4_off + sizeof(struct udphdr)); /* entire header length */
4882     } else {
4883         /* XXX error stat ??? */
4884         return (0);
4885     }
4886 }
4887 
4888 static uint8_t
4889 bxe_set_pbd_csum(struct bxe_fastpath        *fp,
4890                  struct mbuf                *m,
4891                  struct eth_tx_parse_bd_e1x *pbd)
4892 {
4893     struct ether_vlan_header *eh = NULL;
4894     struct ip *ip4 = NULL;
4895     struct ip6_hdr *ip6 = NULL;
4896     caddr_t ip = NULL;
4897     struct tcphdr *th = NULL;
4898     struct udphdr *uh = NULL;
4899     int e_hlen, ip_hlen;
4900     uint16_t proto;
4901     uint8_t hlen;
4902     uint16_t tmp_csum;
4903     uint32_t *tmp_uh;
4904 
4905     /* get the Ethernet header */
4906     eh = mtod(m, struct ether_vlan_header *);
4907 
4908     /* handle VLAN encapsulation if present */
4909     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4910         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4911         proto  = ntohs(eh->evl_proto);
4912     } else {
4913         e_hlen = ETHER_HDR_LEN;
4914         proto  = ntohs(eh->evl_encap_proto);
4915     }
4916 
4917     switch (proto) {
4918     case ETHERTYPE_IP:
4919         /* get the IP header, if mbuf len < 20 then header in next mbuf */
4920         ip4 = (m->m_len < sizeof(struct ip)) ?
4921                   (struct ip *)m->m_next->m_data :
4922                   (struct ip *)(m->m_data + e_hlen);
4923         /* ip_hl is number of 32-bit words */
4924         ip_hlen = (ip4->ip_hl << 1);
4925         ip = (caddr_t)ip4;
4926         break;
4927     case ETHERTYPE_IPV6:
4928         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4929         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4930                   (struct ip6_hdr *)m->m_next->m_data :
4931                   (struct ip6_hdr *)(m->m_data + e_hlen);
4932         /* XXX cannot support offload with IPv6 extensions */
4933         ip_hlen = (sizeof(struct ip6_hdr) >> 1);
4934         ip = (caddr_t)ip6;
4935         break;
4936     default:
4937         /* We can't offload in this case... */
4938         /* XXX error stat ??? */
4939         return (0);
4940     }
4941 
4942     hlen = (e_hlen >> 1);
4943 
4944     /* note that rest of global_data is indirectly zeroed here */
4945     if (m->m_flags & M_VLANTAG) {
4946         pbd->global_data =
4947             htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
4948     } else {
4949         pbd->global_data = htole16(hlen);
4950     }
4951 
4952     pbd->ip_hlen_w = ip_hlen;
4953 
4954     hlen += pbd->ip_hlen_w;
4955 
4956     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4957 
4958     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4959                                   CSUM_TSO |
4960                                   CSUM_TCP_IPV6)) {
4961         th = (struct tcphdr *)(ip + (ip_hlen << 1));
4962         /* th_off is number of 32-bit words */
4963         hlen += (uint16_t)(th->th_off << 1);
4964     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4965                                          CSUM_UDP_IPV6)) {
4966         uh = (struct udphdr *)(ip + (ip_hlen << 1));
4967         hlen += (sizeof(struct udphdr) / 2);
4968     } else {
4969         /* valid case as only CSUM_IP was set */
4970         return (0);
4971     }
4972 
4973     pbd->total_hlen_w = htole16(hlen);
4974 
4975     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4976                                   CSUM_TSO |
4977                                   CSUM_TCP_IPV6)) {
4978         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4979         pbd->tcp_pseudo_csum = ntohs(th->th_sum);
4980     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4981                                          CSUM_UDP_IPV6)) {
4982         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4983 
4984         /*
4985          * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
4986          * checksums and does not know anything about the UDP header and where
4987          * the checksum field is located. It only knows about TCP. Therefore
4988          * we "lie" to the hardware for outgoing UDP packets w/ checksum
4989          * offload. Since the checksum field offset for TCP is 16 bytes and
4990          * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
4991          * bytes less than the start of the UDP header. This allows the
4992          * hardware to write the checksum in the correct spot. But the
4993          * hardware will compute a checksum which includes the last 10 bytes
4994          * of the IP header. To correct this we tweak the stack computed
4995          * pseudo checksum by folding in the calculation of the inverse
4996          * checksum for those final 10 bytes of the IP header. This allows
4997          * the correct checksum to be computed by the hardware.
4998          */
4999 
5000         /* set pointer 10 bytes before UDP header */
5001         tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5002 
5003         /* calculate a pseudo header checksum over the first 10 bytes */
5004         tmp_csum = in_pseudo(*tmp_uh,
5005                              *(tmp_uh + 1),
5006                              *(uint16_t *)(tmp_uh + 2));
5007 
5008         pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5009     }
5010 
5011     return (hlen * 2); /* entire header length, number of bytes */
5012 }
5013 
5014 static void
5015 bxe_set_pbd_lso_e2(struct mbuf *m,
5016                    uint32_t    *parsing_data)
5017 {
5018     *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5019                        ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5020                       ETH_TX_PARSE_BD_E2_LSO_MSS);
5021 
5022     /* XXX test for IPv6 with extension header... */
5023 }
5024 
5025 static void
5026 bxe_set_pbd_lso(struct mbuf                *m,
5027                 struct eth_tx_parse_bd_e1x *pbd)
5028 {
5029     struct ether_vlan_header *eh = NULL;
5030     struct ip *ip = NULL;
5031     struct tcphdr *th = NULL;
5032     int e_hlen;
5033 
5034     /* get the Ethernet header */
5035     eh = mtod(m, struct ether_vlan_header *);
5036 
5037     /* handle VLAN encapsulation if present */
5038     e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5039                  (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5040 
5041     /* get the IP and TCP header, with LSO entire header in first mbuf */
5042     /* XXX assuming IPv4 */
5043     ip = (struct ip *)(m->m_data + e_hlen);
5044     th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5045 
5046     pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5047     pbd->tcp_send_seq = ntohl(th->th_seq);
5048     pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5049 
5050 #if 1
5051         /* XXX IPv4 */
5052         pbd->ip_id = ntohs(ip->ip_id);
5053         pbd->tcp_pseudo_csum =
5054             ntohs(in_pseudo(ip->ip_src.s_addr,
5055                             ip->ip_dst.s_addr,
5056                             htons(IPPROTO_TCP)));
5057 #else
5058         /* XXX IPv6 */
5059         pbd->tcp_pseudo_csum =
5060             ntohs(in_pseudo(&ip6->ip6_src,
5061                             &ip6->ip6_dst,
5062                             htons(IPPROTO_TCP)));
5063 #endif
5064 
5065     pbd->global_data |=
5066         htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5067 }
5068 
5069 /*
5070  * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5071  * visible to the controller.
5072  *
5073  * If an mbuf is submitted to this routine and cannot be given to the
5074  * controller (e.g. it has too many fragments) then the function may free
5075  * the mbuf and return to the caller.
5076  *
5077  * Returns:
5078  *   0 = Success, !0 = Failure
5079  *   Note the side effect that an mbuf may be freed if it causes a problem.
5080  */
5081 static int
5082 bxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5083 {
5084     bus_dma_segment_t segs[32];
5085     struct mbuf *m0;
5086     struct bxe_sw_tx_bd *tx_buf;
5087     struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5088     struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5089     /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5090     struct eth_tx_bd *tx_data_bd;
5091     struct eth_tx_bd *tx_total_pkt_size_bd;
5092     struct eth_tx_start_bd *tx_start_bd;
5093     uint16_t bd_prod, pkt_prod, total_pkt_size;
5094     uint8_t mac_type;
5095     int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5096     struct bxe_softc *sc;
5097     uint16_t tx_bd_avail;
5098     struct ether_vlan_header *eh;
5099     uint32_t pbd_e2_parsing_data = 0;
5100     uint8_t hlen = 0;
5101     int tmp_bd;
5102     int i;
5103 
5104     sc = fp->sc;
5105 
5106 #if __FreeBSD_version >= 800000
5107     M_ASSERTPKTHDR(*m_head);
5108 #endif /* #if __FreeBSD_version >= 800000 */
5109 
5110     m0 = *m_head;
5111     rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5112     tx_start_bd = NULL;
5113     tx_data_bd = NULL;
5114     tx_total_pkt_size_bd = NULL;
5115 
5116     /* get the H/W pointer for packets and BDs */
5117     pkt_prod = fp->tx_pkt_prod;
5118     bd_prod = fp->tx_bd_prod;
5119 
5120     mac_type = UNICAST_ADDRESS;
5121 
5122     /* map the mbuf into the next open DMAable memory */
5123     tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5124     error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5125                                     tx_buf->m_map, m0,
5126                                     segs, &nsegs, BUS_DMA_NOWAIT);
5127 
5128     /* mapping errors */
5129     if(__predict_false(error != 0)) {
5130         fp->eth_q_stats.tx_dma_mapping_failure++;
5131         if (error == ENOMEM) {
5132             /* resource issue, try again later */
5133             rc = ENOMEM;
5134         } else if (error == EFBIG) {
5135             /* possibly recoverable with defragmentation */
5136             fp->eth_q_stats.mbuf_defrag_attempts++;
5137             m0 = m_defrag(*m_head, M_NOWAIT);
5138             if (m0 == NULL) {
5139                 fp->eth_q_stats.mbuf_defrag_failures++;
5140                 rc = ENOBUFS;
5141             } else {
5142                 /* defrag successful, try mapping again */
5143                 *m_head = m0;
5144                 error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5145                                                 tx_buf->m_map, m0,
5146                                                 segs, &nsegs, BUS_DMA_NOWAIT);
5147                 if (error) {
5148                     fp->eth_q_stats.tx_dma_mapping_failure++;
5149                     rc = error;
5150                 }
5151             }
5152         } else {
5153             /* unknown, unrecoverable mapping error */
5154             BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5155             bxe_dump_mbuf(sc, m0, FALSE);
5156             rc = error;
5157         }
5158 
5159         goto bxe_tx_encap_continue;
5160     }
5161 
5162     tx_bd_avail = bxe_tx_avail(sc, fp);
5163 
5164     /* make sure there is enough room in the send queue */
5165     if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5166         /* Recoverable, try again later. */
5167         fp->eth_q_stats.tx_hw_queue_full++;
5168         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5169         rc = ENOMEM;
5170         goto bxe_tx_encap_continue;
5171     }
5172 
5173     /* capture the current H/W TX chain high watermark */
5174     if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5175                         (TX_BD_USABLE - tx_bd_avail))) {
5176         fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5177     }
5178 
5179     /* make sure it fits in the packet window */
5180     if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5181         /*
5182          * The mbuf may be to big for the controller to handle. If the frame
5183          * is a TSO frame we'll need to do an additional check.
5184          */
5185         if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5186             if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5187                 goto bxe_tx_encap_continue; /* OK to send */
5188             } else {
5189                 fp->eth_q_stats.tx_window_violation_tso++;
5190             }
5191         } else {
5192             fp->eth_q_stats.tx_window_violation_std++;
5193         }
5194 
5195         /* lets try to defragment this mbuf and remap it */
5196         fp->eth_q_stats.mbuf_defrag_attempts++;
5197         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5198 
5199         m0 = m_defrag(*m_head, M_NOWAIT);
5200         if (m0 == NULL) {
5201             fp->eth_q_stats.mbuf_defrag_failures++;
5202             /* Ugh, just drop the frame... :( */
5203             rc = ENOBUFS;
5204         } else {
5205             /* defrag successful, try mapping again */
5206             *m_head = m0;
5207             error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5208                                             tx_buf->m_map, m0,
5209                                             segs, &nsegs, BUS_DMA_NOWAIT);
5210             if (error) {
5211                 fp->eth_q_stats.tx_dma_mapping_failure++;
5212                 /* No sense in trying to defrag/copy chain, drop it. :( */
5213                 rc = error;
5214             }
5215             else {
5216                 /* if the chain is still too long then drop it */
5217                 if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5218                     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5219                     rc = ENODEV;
5220                 }
5221             }
5222         }
5223     }
5224 
5225 bxe_tx_encap_continue:
5226 
5227     /* Check for errors */
5228     if (rc) {
5229         if (rc == ENOMEM) {
5230             /* recoverable try again later  */
5231         } else {
5232             fp->eth_q_stats.tx_soft_errors++;
5233             fp->eth_q_stats.mbuf_alloc_tx--;
5234             m_freem(*m_head);
5235             *m_head = NULL;
5236         }
5237 
5238         return (rc);
5239     }
5240 
5241     /* set flag according to packet type (UNICAST_ADDRESS is default) */
5242     if (m0->m_flags & M_BCAST) {
5243         mac_type = BROADCAST_ADDRESS;
5244     } else if (m0->m_flags & M_MCAST) {
5245         mac_type = MULTICAST_ADDRESS;
5246     }
5247 
5248     /* store the mbuf into the mbuf ring */
5249     tx_buf->m        = m0;
5250     tx_buf->first_bd = fp->tx_bd_prod;
5251     tx_buf->flags    = 0;
5252 
5253     /* prepare the first transmit (start) BD for the mbuf */
5254     tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5255 
5256     BLOGD(sc, DBG_TX,
5257           "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5258           pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5259 
5260     tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5261     tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5262     tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5263     total_pkt_size += tx_start_bd->nbytes;
5264     tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5265 
5266     tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5267 
5268     /* all frames have at least Start BD + Parsing BD */
5269     nbds = nsegs + 1;
5270     tx_start_bd->nbd = htole16(nbds);
5271 
5272     if (m0->m_flags & M_VLANTAG) {
5273         tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5274         tx_start_bd->bd_flags.as_bitfield |=
5275             (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5276     } else {
5277         /* vf tx, start bd must hold the ethertype for fw to enforce it */
5278         if (IS_VF(sc)) {
5279             /* map ethernet header to find type and header length */
5280             eh = mtod(m0, struct ether_vlan_header *);
5281             tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5282         } else {
5283             /* used by FW for packet accounting */
5284             tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5285         }
5286     }
5287 
5288     /*
5289      * add a parsing BD from the chain. The parsing BD is always added
5290      * though it is only used for TSO and chksum
5291      */
5292     bd_prod = TX_BD_NEXT(bd_prod);
5293 
5294     if (m0->m_pkthdr.csum_flags) {
5295         if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5296             fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5297             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5298         }
5299 
5300         if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5301             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5302                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5303         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5304             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5305                                                   ETH_TX_BD_FLAGS_IS_UDP |
5306                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5307         } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5308                    (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5309             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5310         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5311             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5312                                                   ETH_TX_BD_FLAGS_IS_UDP);
5313         }
5314     }
5315 
5316     if (!CHIP_IS_E1x(sc)) {
5317         pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5318         memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5319 
5320         if (m0->m_pkthdr.csum_flags) {
5321             hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5322         }
5323 
5324         SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5325                  mac_type);
5326     } else {
5327         uint16_t global_data = 0;
5328 
5329         pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5330         memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5331 
5332         if (m0->m_pkthdr.csum_flags) {
5333             hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5334         }
5335 
5336         SET_FLAG(global_data,
5337                  ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5338         pbd_e1x->global_data |= htole16(global_data);
5339     }
5340 
5341     /* setup the parsing BD with TSO specific info */
5342     if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5343         fp->eth_q_stats.tx_ofld_frames_lso++;
5344         tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5345 
5346         if (__predict_false(tx_start_bd->nbytes > hlen)) {
5347             fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5348 
5349             /* split the first BD into header/data making the fw job easy */
5350             nbds++;
5351             tx_start_bd->nbd = htole16(nbds);
5352             tx_start_bd->nbytes = htole16(hlen);
5353 
5354             bd_prod = TX_BD_NEXT(bd_prod);
5355 
5356             /* new transmit BD after the tx_parse_bd */
5357             tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5358             tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5359             tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5360             tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5361             if (tx_total_pkt_size_bd == NULL) {
5362                 tx_total_pkt_size_bd = tx_data_bd;
5363             }
5364 
5365             BLOGD(sc, DBG_TX,
5366                   "TSO split header size is %d (%x:%x) nbds %d\n",
5367                   le16toh(tx_start_bd->nbytes),
5368                   le32toh(tx_start_bd->addr_hi),
5369                   le32toh(tx_start_bd->addr_lo),
5370                   nbds);
5371         }
5372 
5373         if (!CHIP_IS_E1x(sc)) {
5374             bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5375         } else {
5376             bxe_set_pbd_lso(m0, pbd_e1x);
5377         }
5378     }
5379 
5380     if (pbd_e2_parsing_data) {
5381         pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5382     }
5383 
5384     /* prepare remaining BDs, start tx bd contains first seg/frag */
5385     for (i = 1; i < nsegs ; i++) {
5386         bd_prod = TX_BD_NEXT(bd_prod);
5387         tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5388         tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5389         tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5390         tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5391         if (tx_total_pkt_size_bd == NULL) {
5392             tx_total_pkt_size_bd = tx_data_bd;
5393         }
5394         total_pkt_size += tx_data_bd->nbytes;
5395     }
5396 
5397     BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5398 
5399     if (tx_total_pkt_size_bd != NULL) {
5400         tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5401     }
5402 
5403     if (__predict_false(sc->debug & DBG_TX)) {
5404         tmp_bd = tx_buf->first_bd;
5405         for (i = 0; i < nbds; i++)
5406         {
5407             if (i == 0) {
5408                 BLOGD(sc, DBG_TX,
5409                       "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5410                       "bd_flags=0x%x hdr_nbds=%d\n",
5411                       tx_start_bd,
5412                       tmp_bd,
5413                       le16toh(tx_start_bd->nbd),
5414                       le16toh(tx_start_bd->vlan_or_ethertype),
5415                       tx_start_bd->bd_flags.as_bitfield,
5416                       (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5417             } else if (i == 1) {
5418                 if (pbd_e1x) {
5419                     BLOGD(sc, DBG_TX,
5420                           "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5421                           "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5422                           "tcp_seq=%u total_hlen_w=%u\n",
5423                           pbd_e1x,
5424                           tmp_bd,
5425                           pbd_e1x->global_data,
5426                           pbd_e1x->ip_hlen_w,
5427                           pbd_e1x->ip_id,
5428                           pbd_e1x->lso_mss,
5429                           pbd_e1x->tcp_flags,
5430                           pbd_e1x->tcp_pseudo_csum,
5431                           pbd_e1x->tcp_send_seq,
5432                           le16toh(pbd_e1x->total_hlen_w));
5433                 } else { /* if (pbd_e2) */
5434                     BLOGD(sc, DBG_TX,
5435                           "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5436                           "src=%02x:%02x:%02x parsing_data=0x%x\n",
5437                           pbd_e2,
5438                           tmp_bd,
5439                           pbd_e2->data.mac_addr.dst_hi,
5440                           pbd_e2->data.mac_addr.dst_mid,
5441                           pbd_e2->data.mac_addr.dst_lo,
5442                           pbd_e2->data.mac_addr.src_hi,
5443                           pbd_e2->data.mac_addr.src_mid,
5444                           pbd_e2->data.mac_addr.src_lo,
5445                           pbd_e2->parsing_data);
5446                 }
5447             }
5448 
5449             if (i != 1) { /* skip parse db as it doesn't hold data */
5450                 tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5451                 BLOGD(sc, DBG_TX,
5452                       "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5453                       tx_data_bd,
5454                       tmp_bd,
5455                       le16toh(tx_data_bd->nbytes),
5456                       le32toh(tx_data_bd->addr_hi),
5457                       le32toh(tx_data_bd->addr_lo));
5458             }
5459 
5460             tmp_bd = TX_BD_NEXT(tmp_bd);
5461         }
5462     }
5463 
5464     BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5465 
5466     /* update TX BD producer index value for next TX */
5467     bd_prod = TX_BD_NEXT(bd_prod);
5468 
5469     /*
5470      * If the chain of tx_bd's describing this frame is adjacent to or spans
5471      * an eth_tx_next_bd element then we need to increment the nbds value.
5472      */
5473     if (TX_BD_IDX(bd_prod) < nbds) {
5474         nbds++;
5475     }
5476 
5477     /* don't allow reordering of writes for nbd and packets */
5478     mb();
5479 
5480     fp->tx_db.data.prod += nbds;
5481 
5482     /* producer points to the next free tx_bd at this point */
5483     fp->tx_pkt_prod++;
5484     fp->tx_bd_prod = bd_prod;
5485 
5486     DOORBELL(sc, fp->index, fp->tx_db.raw);
5487 
5488     fp->eth_q_stats.tx_pkts++;
5489 
5490     /* Prevent speculative reads from getting ahead of the status block. */
5491     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5492                       0, 0, BUS_SPACE_BARRIER_READ);
5493 
5494     /* Prevent speculative reads from getting ahead of the doorbell. */
5495     bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5496                       0, 0, BUS_SPACE_BARRIER_READ);
5497 
5498     return (0);
5499 }
5500 
5501 static void
5502 bxe_tx_start_locked(struct bxe_softc *sc,
5503                     if_t ifp,
5504                     struct bxe_fastpath *fp)
5505 {
5506     struct mbuf *m = NULL;
5507     int tx_count = 0;
5508     uint16_t tx_bd_avail;
5509 
5510     BXE_FP_TX_LOCK_ASSERT(fp);
5511 
5512     /* keep adding entries while there are frames to send */
5513     while (!if_sendq_empty(ifp)) {
5514 
5515         /*
5516          * check for any frames to send
5517          * dequeue can still be NULL even if queue is not empty
5518          */
5519         m = if_dequeue(ifp);
5520         if (__predict_false(m == NULL)) {
5521             break;
5522         }
5523 
5524         /* the mbuf now belongs to us */
5525         fp->eth_q_stats.mbuf_alloc_tx++;
5526 
5527         /*
5528          * Put the frame into the transmit ring. If we don't have room,
5529          * place the mbuf back at the head of the TX queue, set the
5530          * OACTIVE flag, and wait for the NIC to drain the chain.
5531          */
5532         if (__predict_false(bxe_tx_encap(fp, &m))) {
5533             fp->eth_q_stats.tx_encap_failures++;
5534             if (m != NULL) {
5535                 /* mark the TX queue as full and return the frame */
5536                 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5537 		if_sendq_prepend(ifp, m);
5538                 fp->eth_q_stats.mbuf_alloc_tx--;
5539                 fp->eth_q_stats.tx_queue_xoff++;
5540             }
5541 
5542             /* stop looking for more work */
5543             break;
5544         }
5545 
5546         /* the frame was enqueued successfully */
5547         tx_count++;
5548 
5549         /* send a copy of the frame to any BPF listeners. */
5550         if_etherbpfmtap(ifp, m);
5551 
5552         tx_bd_avail = bxe_tx_avail(sc, fp);
5553 
5554         /* handle any completions if we're running low */
5555         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5556             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5557             bxe_txeof(sc, fp);
5558             if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5559                 break;
5560             }
5561         }
5562     }
5563 
5564     /* all TX packets were dequeued and/or the tx ring is full */
5565     if (tx_count > 0) {
5566         /* reset the TX watchdog timeout timer */
5567         fp->watchdog_timer = BXE_TX_TIMEOUT;
5568     }
5569 }
5570 
5571 /* Legacy (non-RSS) dispatch routine */
5572 static void
5573 bxe_tx_start(if_t ifp)
5574 {
5575     struct bxe_softc *sc;
5576     struct bxe_fastpath *fp;
5577 
5578     sc = if_getsoftc(ifp);
5579 
5580     if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5581         BLOGW(sc, "Interface not running, ignoring transmit request\n");
5582         return;
5583     }
5584 
5585     if (!sc->link_vars.link_up) {
5586         BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5587         return;
5588     }
5589 
5590     fp = &sc->fp[0];
5591 
5592     if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5593         fp->eth_q_stats.tx_queue_full_return++;
5594         return;
5595     }
5596 
5597     BXE_FP_TX_LOCK(fp);
5598     bxe_tx_start_locked(sc, ifp, fp);
5599     BXE_FP_TX_UNLOCK(fp);
5600 }
5601 
5602 #if __FreeBSD_version >= 800000
5603 
5604 static int
5605 bxe_tx_mq_start_locked(struct bxe_softc    *sc,
5606                        if_t                ifp,
5607                        struct bxe_fastpath *fp,
5608                        struct mbuf         *m)
5609 {
5610     struct buf_ring *tx_br = fp->tx_br;
5611     struct mbuf *next;
5612     int depth, rc, tx_count;
5613     uint16_t tx_bd_avail;
5614 
5615     rc = tx_count = 0;
5616 
5617     BXE_FP_TX_LOCK_ASSERT(fp);
5618 
5619     if (!tx_br) {
5620         BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5621         return (EINVAL);
5622     }
5623 
5624     if (!sc->link_vars.link_up ||
5625         (if_getdrvflags(ifp) &
5626         (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING) {
5627         rc = drbr_enqueue(ifp, tx_br, m);
5628         goto bxe_tx_mq_start_locked_exit;
5629     }
5630 
5631     /* fetch the depth of the driver queue */
5632     depth = drbr_inuse_drv(ifp, tx_br);
5633     if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5634         fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5635     }
5636 
5637     if (m == NULL) {
5638         /* no new work, check for pending frames */
5639         next = drbr_dequeue_drv(ifp, tx_br);
5640     } else if (drbr_needs_enqueue_drv(ifp, tx_br)) {
5641         /* have both new and pending work, maintain packet order */
5642         rc = drbr_enqueue(ifp, tx_br, m);
5643         if (rc != 0) {
5644             fp->eth_q_stats.tx_soft_errors++;
5645             goto bxe_tx_mq_start_locked_exit;
5646         }
5647         next = drbr_dequeue_drv(ifp, tx_br);
5648     } else {
5649         /* new work only and nothing pending */
5650         next = m;
5651     }
5652 
5653     /* keep adding entries while there are frames to send */
5654     while (next != NULL) {
5655 
5656         /* the mbuf now belongs to us */
5657         fp->eth_q_stats.mbuf_alloc_tx++;
5658 
5659         /*
5660          * Put the frame into the transmit ring. If we don't have room,
5661          * place the mbuf back at the head of the TX queue, set the
5662          * OACTIVE flag, and wait for the NIC to drain the chain.
5663          */
5664         rc = bxe_tx_encap(fp, &next);
5665         if (__predict_false(rc != 0)) {
5666             fp->eth_q_stats.tx_encap_failures++;
5667             if (next != NULL) {
5668                 /* mark the TX queue as full and save the frame */
5669                 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5670                 /* XXX this may reorder the frame */
5671                 rc = drbr_enqueue(ifp, tx_br, next);
5672                 fp->eth_q_stats.mbuf_alloc_tx--;
5673                 fp->eth_q_stats.tx_frames_deferred++;
5674             }
5675 
5676             /* stop looking for more work */
5677             break;
5678         }
5679 
5680         /* the transmit frame was enqueued successfully */
5681         tx_count++;
5682 
5683         /* send a copy of the frame to any BPF listeners */
5684 	if_etherbpfmtap(ifp, next);
5685 
5686         tx_bd_avail = bxe_tx_avail(sc, fp);
5687 
5688         /* handle any completions if we're running low */
5689         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5690             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5691             bxe_txeof(sc, fp);
5692             if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5693                 break;
5694             }
5695         }
5696 
5697         next = drbr_dequeue_drv(ifp, tx_br);
5698     }
5699 
5700     /* all TX packets were dequeued and/or the tx ring is full */
5701     if (tx_count > 0) {
5702         /* reset the TX watchdog timeout timer */
5703         fp->watchdog_timer = BXE_TX_TIMEOUT;
5704     }
5705 
5706 bxe_tx_mq_start_locked_exit:
5707 
5708     return (rc);
5709 }
5710 
5711 /* Multiqueue (TSS) dispatch routine. */
5712 static int
5713 bxe_tx_mq_start(struct ifnet *ifp,
5714                 struct mbuf  *m)
5715 {
5716     struct bxe_softc *sc = if_getsoftc(ifp);
5717     struct bxe_fastpath *fp;
5718     int fp_index, rc;
5719 
5720     fp_index = 0; /* default is the first queue */
5721 
5722     /* check if flowid is set */
5723 
5724     if (BXE_VALID_FLOWID(m))
5725         fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5726 
5727     fp = &sc->fp[fp_index];
5728 
5729     if (BXE_FP_TX_TRYLOCK(fp)) {
5730         rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5731         BXE_FP_TX_UNLOCK(fp);
5732     } else
5733         rc = drbr_enqueue(ifp, fp->tx_br, m);
5734 
5735     return (rc);
5736 }
5737 
5738 static void
5739 bxe_mq_flush(struct ifnet *ifp)
5740 {
5741     struct bxe_softc *sc = if_getsoftc(ifp);
5742     struct bxe_fastpath *fp;
5743     struct mbuf *m;
5744     int i;
5745 
5746     for (i = 0; i < sc->num_queues; i++) {
5747         fp = &sc->fp[i];
5748 
5749         if (fp->state != BXE_FP_STATE_OPEN) {
5750             BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5751                   fp->index, fp->state);
5752             continue;
5753         }
5754 
5755         if (fp->tx_br != NULL) {
5756             BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5757             BXE_FP_TX_LOCK(fp);
5758             while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5759                 m_freem(m);
5760             }
5761             BXE_FP_TX_UNLOCK(fp);
5762         }
5763     }
5764 
5765     if_qflush(ifp);
5766 }
5767 
5768 #endif /* FreeBSD_version >= 800000 */
5769 
5770 static uint16_t
5771 bxe_cid_ilt_lines(struct bxe_softc *sc)
5772 {
5773     if (IS_SRIOV(sc)) {
5774         return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5775     }
5776     return (L2_ILT_LINES(sc));
5777 }
5778 
5779 static void
5780 bxe_ilt_set_info(struct bxe_softc *sc)
5781 {
5782     struct ilt_client_info *ilt_client;
5783     struct ecore_ilt *ilt = sc->ilt;
5784     uint16_t line = 0;
5785 
5786     ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5787     BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5788 
5789     /* CDU */
5790     ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5791     ilt_client->client_num = ILT_CLIENT_CDU;
5792     ilt_client->page_size = CDU_ILT_PAGE_SZ;
5793     ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5794     ilt_client->start = line;
5795     line += bxe_cid_ilt_lines(sc);
5796 
5797     if (CNIC_SUPPORT(sc)) {
5798         line += CNIC_ILT_LINES;
5799     }
5800 
5801     ilt_client->end = (line - 1);
5802 
5803     BLOGD(sc, DBG_LOAD,
5804           "ilt client[CDU]: start %d, end %d, "
5805           "psz 0x%x, flags 0x%x, hw psz %d\n",
5806           ilt_client->start, ilt_client->end,
5807           ilt_client->page_size,
5808           ilt_client->flags,
5809           ilog2(ilt_client->page_size >> 12));
5810 
5811     /* QM */
5812     if (QM_INIT(sc->qm_cid_count)) {
5813         ilt_client = &ilt->clients[ILT_CLIENT_QM];
5814         ilt_client->client_num = ILT_CLIENT_QM;
5815         ilt_client->page_size = QM_ILT_PAGE_SZ;
5816         ilt_client->flags = 0;
5817         ilt_client->start = line;
5818 
5819         /* 4 bytes for each cid */
5820         line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5821                              QM_ILT_PAGE_SZ);
5822 
5823         ilt_client->end = (line - 1);
5824 
5825         BLOGD(sc, DBG_LOAD,
5826               "ilt client[QM]: start %d, end %d, "
5827               "psz 0x%x, flags 0x%x, hw psz %d\n",
5828               ilt_client->start, ilt_client->end,
5829               ilt_client->page_size, ilt_client->flags,
5830               ilog2(ilt_client->page_size >> 12));
5831     }
5832 
5833     if (CNIC_SUPPORT(sc)) {
5834         /* SRC */
5835         ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5836         ilt_client->client_num = ILT_CLIENT_SRC;
5837         ilt_client->page_size = SRC_ILT_PAGE_SZ;
5838         ilt_client->flags = 0;
5839         ilt_client->start = line;
5840         line += SRC_ILT_LINES;
5841         ilt_client->end = (line - 1);
5842 
5843         BLOGD(sc, DBG_LOAD,
5844               "ilt client[SRC]: start %d, end %d, "
5845               "psz 0x%x, flags 0x%x, hw psz %d\n",
5846               ilt_client->start, ilt_client->end,
5847               ilt_client->page_size, ilt_client->flags,
5848               ilog2(ilt_client->page_size >> 12));
5849 
5850         /* TM */
5851         ilt_client = &ilt->clients[ILT_CLIENT_TM];
5852         ilt_client->client_num = ILT_CLIENT_TM;
5853         ilt_client->page_size = TM_ILT_PAGE_SZ;
5854         ilt_client->flags = 0;
5855         ilt_client->start = line;
5856         line += TM_ILT_LINES;
5857         ilt_client->end = (line - 1);
5858 
5859         BLOGD(sc, DBG_LOAD,
5860               "ilt client[TM]: start %d, end %d, "
5861               "psz 0x%x, flags 0x%x, hw psz %d\n",
5862               ilt_client->start, ilt_client->end,
5863               ilt_client->page_size, ilt_client->flags,
5864               ilog2(ilt_client->page_size >> 12));
5865     }
5866 
5867     KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
5868 }
5869 
5870 static void
5871 bxe_set_fp_rx_buf_size(struct bxe_softc *sc)
5872 {
5873     int i;
5874     uint32_t rx_buf_size;
5875 
5876     rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
5877 
5878     for (i = 0; i < sc->num_queues; i++) {
5879         if(rx_buf_size <= MCLBYTES){
5880             sc->fp[i].rx_buf_size = rx_buf_size;
5881             sc->fp[i].mbuf_alloc_size = MCLBYTES;
5882         }else if (rx_buf_size <= MJUMPAGESIZE){
5883             sc->fp[i].rx_buf_size = rx_buf_size;
5884             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5885         }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
5886             sc->fp[i].rx_buf_size = MCLBYTES;
5887             sc->fp[i].mbuf_alloc_size = MCLBYTES;
5888         }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
5889             sc->fp[i].rx_buf_size = MJUMPAGESIZE;
5890             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5891         }else {
5892             sc->fp[i].rx_buf_size = MCLBYTES;
5893             sc->fp[i].mbuf_alloc_size = MCLBYTES;
5894         }
5895     }
5896 }
5897 
5898 static int
5899 bxe_alloc_ilt_mem(struct bxe_softc *sc)
5900 {
5901     int rc = 0;
5902 
5903     if ((sc->ilt =
5904          (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
5905                                     M_BXE_ILT,
5906                                     (M_NOWAIT | M_ZERO))) == NULL) {
5907         rc = 1;
5908     }
5909 
5910     return (rc);
5911 }
5912 
5913 static int
5914 bxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
5915 {
5916     int rc = 0;
5917 
5918     if ((sc->ilt->lines =
5919          (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
5920                                     M_BXE_ILT,
5921                                     (M_NOWAIT | M_ZERO))) == NULL) {
5922         rc = 1;
5923     }
5924 
5925     return (rc);
5926 }
5927 
5928 static void
5929 bxe_free_ilt_mem(struct bxe_softc *sc)
5930 {
5931     if (sc->ilt != NULL) {
5932         free(sc->ilt, M_BXE_ILT);
5933         sc->ilt = NULL;
5934     }
5935 }
5936 
5937 static void
5938 bxe_free_ilt_lines_mem(struct bxe_softc *sc)
5939 {
5940     if (sc->ilt->lines != NULL) {
5941         free(sc->ilt->lines, M_BXE_ILT);
5942         sc->ilt->lines = NULL;
5943     }
5944 }
5945 
5946 static void
5947 bxe_free_mem(struct bxe_softc *sc)
5948 {
5949     int i;
5950 
5951     for (i = 0; i < L2_ILT_LINES(sc); i++) {
5952         bxe_dma_free(sc, &sc->context[i].vcxt_dma);
5953         sc->context[i].vcxt = NULL;
5954         sc->context[i].size = 0;
5955     }
5956 
5957     ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
5958 
5959     bxe_free_ilt_lines_mem(sc);
5960 
5961 }
5962 
5963 static int
5964 bxe_alloc_mem(struct bxe_softc *sc)
5965 {
5966     int context_size;
5967     int allocated;
5968     int i;
5969 
5970     /*
5971      * Allocate memory for CDU context:
5972      * This memory is allocated separately and not in the generic ILT
5973      * functions because CDU differs in few aspects:
5974      * 1. There can be multiple entities allocating memory for context -
5975      * regular L2, CNIC, and SRIOV drivers. Each separately controls
5976      * its own ILT lines.
5977      * 2. Since CDU page-size is not a single 4KB page (which is the case
5978      * for the other ILT clients), to be efficient we want to support
5979      * allocation of sub-page-size in the last entry.
5980      * 3. Context pointers are used by the driver to pass to FW / update
5981      * the context (for the other ILT clients the pointers are used just to
5982      * free the memory during unload).
5983      */
5984     context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
5985     for (i = 0, allocated = 0; allocated < context_size; i++) {
5986         sc->context[i].size = min(CDU_ILT_PAGE_SZ,
5987                                   (context_size - allocated));
5988 
5989         if (bxe_dma_alloc(sc, sc->context[i].size,
5990                           &sc->context[i].vcxt_dma,
5991                           "cdu context") != 0) {
5992             bxe_free_mem(sc);
5993             return (-1);
5994         }
5995 
5996         sc->context[i].vcxt =
5997             (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
5998 
5999         allocated += sc->context[i].size;
6000     }
6001 
6002     bxe_alloc_ilt_lines_mem(sc);
6003 
6004     BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6005           sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6006     {
6007         for (i = 0; i < 4; i++) {
6008             BLOGD(sc, DBG_LOAD,
6009                   "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6010                   i,
6011                   sc->ilt->clients[i].page_size,
6012                   sc->ilt->clients[i].start,
6013                   sc->ilt->clients[i].end,
6014                   sc->ilt->clients[i].client_num,
6015                   sc->ilt->clients[i].flags);
6016         }
6017     }
6018     if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6019         BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6020         bxe_free_mem(sc);
6021         return (-1);
6022     }
6023 
6024     return (0);
6025 }
6026 
6027 static void
6028 bxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6029 {
6030     struct bxe_softc *sc;
6031     int i;
6032 
6033     sc = fp->sc;
6034 
6035     if (fp->rx_mbuf_tag == NULL) {
6036         return;
6037     }
6038 
6039     /* free all mbufs and unload all maps */
6040     for (i = 0; i < RX_BD_TOTAL; i++) {
6041         if (fp->rx_mbuf_chain[i].m_map != NULL) {
6042             bus_dmamap_sync(fp->rx_mbuf_tag,
6043                             fp->rx_mbuf_chain[i].m_map,
6044                             BUS_DMASYNC_POSTREAD);
6045             bus_dmamap_unload(fp->rx_mbuf_tag,
6046                               fp->rx_mbuf_chain[i].m_map);
6047         }
6048 
6049         if (fp->rx_mbuf_chain[i].m != NULL) {
6050             m_freem(fp->rx_mbuf_chain[i].m);
6051             fp->rx_mbuf_chain[i].m = NULL;
6052             fp->eth_q_stats.mbuf_alloc_rx--;
6053         }
6054     }
6055 }
6056 
6057 static void
6058 bxe_free_tpa_pool(struct bxe_fastpath *fp)
6059 {
6060     struct bxe_softc *sc;
6061     int i, max_agg_queues;
6062 
6063     sc = fp->sc;
6064 
6065     if (fp->rx_mbuf_tag == NULL) {
6066         return;
6067     }
6068 
6069     max_agg_queues = MAX_AGG_QS(sc);
6070 
6071     /* release all mbufs and unload all DMA maps in the TPA pool */
6072     for (i = 0; i < max_agg_queues; i++) {
6073         if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6074             bus_dmamap_sync(fp->rx_mbuf_tag,
6075                             fp->rx_tpa_info[i].bd.m_map,
6076                             BUS_DMASYNC_POSTREAD);
6077             bus_dmamap_unload(fp->rx_mbuf_tag,
6078                               fp->rx_tpa_info[i].bd.m_map);
6079         }
6080 
6081         if (fp->rx_tpa_info[i].bd.m != NULL) {
6082             m_freem(fp->rx_tpa_info[i].bd.m);
6083             fp->rx_tpa_info[i].bd.m = NULL;
6084             fp->eth_q_stats.mbuf_alloc_tpa--;
6085         }
6086     }
6087 }
6088 
6089 static void
6090 bxe_free_sge_chain(struct bxe_fastpath *fp)
6091 {
6092     struct bxe_softc *sc;
6093     int i;
6094 
6095     sc = fp->sc;
6096 
6097     if (fp->rx_sge_mbuf_tag == NULL) {
6098         return;
6099     }
6100 
6101     /* rree all mbufs and unload all maps */
6102     for (i = 0; i < RX_SGE_TOTAL; i++) {
6103         if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6104             bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6105                             fp->rx_sge_mbuf_chain[i].m_map,
6106                             BUS_DMASYNC_POSTREAD);
6107             bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6108                               fp->rx_sge_mbuf_chain[i].m_map);
6109         }
6110 
6111         if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6112             m_freem(fp->rx_sge_mbuf_chain[i].m);
6113             fp->rx_sge_mbuf_chain[i].m = NULL;
6114             fp->eth_q_stats.mbuf_alloc_sge--;
6115         }
6116     }
6117 }
6118 
6119 static void
6120 bxe_free_fp_buffers(struct bxe_softc *sc)
6121 {
6122     struct bxe_fastpath *fp;
6123     int i;
6124 
6125     for (i = 0; i < sc->num_queues; i++) {
6126         fp = &sc->fp[i];
6127 
6128 #if __FreeBSD_version >= 800000
6129         if (fp->tx_br != NULL) {
6130             /* just in case bxe_mq_flush() wasn't called */
6131             if (mtx_initialized(&fp->tx_mtx)) {
6132                 struct mbuf *m;
6133 
6134                 BXE_FP_TX_LOCK(fp);
6135                 while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6136                     m_freem(m);
6137                 BXE_FP_TX_UNLOCK(fp);
6138             }
6139         }
6140 #endif
6141 
6142         /* free all RX buffers */
6143         bxe_free_rx_bd_chain(fp);
6144         bxe_free_tpa_pool(fp);
6145         bxe_free_sge_chain(fp);
6146 
6147         if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6148             BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6149                   fp->eth_q_stats.mbuf_alloc_rx);
6150         }
6151 
6152         if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6153             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6154                   fp->eth_q_stats.mbuf_alloc_sge);
6155         }
6156 
6157         if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6158             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6159                   fp->eth_q_stats.mbuf_alloc_tpa);
6160         }
6161 
6162         if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6163             BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6164                   fp->eth_q_stats.mbuf_alloc_tx);
6165         }
6166 
6167         /* XXX verify all mbufs were reclaimed */
6168     }
6169 }
6170 
6171 static int
6172 bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6173                      uint16_t            prev_index,
6174                      uint16_t            index)
6175 {
6176     struct bxe_sw_rx_bd *rx_buf;
6177     struct eth_rx_bd *rx_bd;
6178     bus_dma_segment_t segs[1];
6179     bus_dmamap_t map;
6180     struct mbuf *m;
6181     int nsegs, rc;
6182 
6183     rc = 0;
6184 
6185     /* allocate the new RX BD mbuf */
6186     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6187     if (__predict_false(m == NULL)) {
6188         fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6189         return (ENOBUFS);
6190     }
6191 
6192     fp->eth_q_stats.mbuf_alloc_rx++;
6193 
6194     /* initialize the mbuf buffer length */
6195     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6196 
6197     /* map the mbuf into non-paged pool */
6198     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6199                                  fp->rx_mbuf_spare_map,
6200                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6201     if (__predict_false(rc != 0)) {
6202         fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6203         m_freem(m);
6204         fp->eth_q_stats.mbuf_alloc_rx--;
6205         return (rc);
6206     }
6207 
6208     /* all mbufs must map to a single segment */
6209     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6210 
6211     /* release any existing RX BD mbuf mappings */
6212 
6213     if (prev_index != index) {
6214         rx_buf = &fp->rx_mbuf_chain[prev_index];
6215 
6216         if (rx_buf->m_map != NULL) {
6217             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6218                             BUS_DMASYNC_POSTREAD);
6219             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6220         }
6221 
6222         /*
6223          * We only get here from bxe_rxeof() when the maximum number
6224          * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6225          * holds the mbuf in the prev_index so it's OK to NULL it out
6226          * here without concern of a memory leak.
6227          */
6228         fp->rx_mbuf_chain[prev_index].m = NULL;
6229     }
6230 
6231     rx_buf = &fp->rx_mbuf_chain[index];
6232 
6233     if (rx_buf->m_map != NULL) {
6234         bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6235                         BUS_DMASYNC_POSTREAD);
6236         bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6237     }
6238 
6239     /* save the mbuf and mapping info for a future packet */
6240     map = (prev_index != index) ?
6241               fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6242     rx_buf->m_map = fp->rx_mbuf_spare_map;
6243     fp->rx_mbuf_spare_map = map;
6244     bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6245                     BUS_DMASYNC_PREREAD);
6246     rx_buf->m = m;
6247 
6248     rx_bd = &fp->rx_chain[index];
6249     rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6250     rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6251 
6252     return (rc);
6253 }
6254 
6255 static int
6256 bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6257                       int                 queue)
6258 {
6259     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6260     bus_dma_segment_t segs[1];
6261     bus_dmamap_t map;
6262     struct mbuf *m;
6263     int nsegs;
6264     int rc = 0;
6265 
6266     /* allocate the new TPA mbuf */
6267     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6268     if (__predict_false(m == NULL)) {
6269         fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6270         return (ENOBUFS);
6271     }
6272 
6273     fp->eth_q_stats.mbuf_alloc_tpa++;
6274 
6275     /* initialize the mbuf buffer length */
6276     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6277 
6278     /* map the mbuf into non-paged pool */
6279     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6280                                  fp->rx_tpa_info_mbuf_spare_map,
6281                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6282     if (__predict_false(rc != 0)) {
6283         fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6284         m_free(m);
6285         fp->eth_q_stats.mbuf_alloc_tpa--;
6286         return (rc);
6287     }
6288 
6289     /* all mbufs must map to a single segment */
6290     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6291 
6292     /* release any existing TPA mbuf mapping */
6293     if (tpa_info->bd.m_map != NULL) {
6294         bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6295                         BUS_DMASYNC_POSTREAD);
6296         bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6297     }
6298 
6299     /* save the mbuf and mapping info for the TPA mbuf */
6300     map = tpa_info->bd.m_map;
6301     tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6302     fp->rx_tpa_info_mbuf_spare_map = map;
6303     bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6304                     BUS_DMASYNC_PREREAD);
6305     tpa_info->bd.m = m;
6306     tpa_info->seg = segs[0];
6307 
6308     return (rc);
6309 }
6310 
6311 /*
6312  * Allocate an mbuf and assign it to the receive scatter gather chain. The
6313  * caller must take care to save a copy of the existing mbuf in the SG mbuf
6314  * chain.
6315  */
6316 static int
6317 bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6318                       uint16_t            index)
6319 {
6320     struct bxe_sw_rx_bd *sge_buf;
6321     struct eth_rx_sge *sge;
6322     bus_dma_segment_t segs[1];
6323     bus_dmamap_t map;
6324     struct mbuf *m;
6325     int nsegs;
6326     int rc = 0;
6327 
6328     /* allocate a new SGE mbuf */
6329     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6330     if (__predict_false(m == NULL)) {
6331         fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6332         return (ENOMEM);
6333     }
6334 
6335     fp->eth_q_stats.mbuf_alloc_sge++;
6336 
6337     /* initialize the mbuf buffer length */
6338     m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6339 
6340     /* map the SGE mbuf into non-paged pool */
6341     rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6342                                  fp->rx_sge_mbuf_spare_map,
6343                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6344     if (__predict_false(rc != 0)) {
6345         fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6346         m_freem(m);
6347         fp->eth_q_stats.mbuf_alloc_sge--;
6348         return (rc);
6349     }
6350 
6351     /* all mbufs must map to a single segment */
6352     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6353 
6354     sge_buf = &fp->rx_sge_mbuf_chain[index];
6355 
6356     /* release any existing SGE mbuf mapping */
6357     if (sge_buf->m_map != NULL) {
6358         bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6359                         BUS_DMASYNC_POSTREAD);
6360         bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6361     }
6362 
6363     /* save the mbuf and mapping info for a future packet */
6364     map = sge_buf->m_map;
6365     sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6366     fp->rx_sge_mbuf_spare_map = map;
6367     bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6368                     BUS_DMASYNC_PREREAD);
6369     sge_buf->m = m;
6370 
6371     sge = &fp->rx_sge_chain[index];
6372     sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6373     sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6374 
6375     return (rc);
6376 }
6377 
6378 static __noinline int
6379 bxe_alloc_fp_buffers(struct bxe_softc *sc)
6380 {
6381     struct bxe_fastpath *fp;
6382     int i, j, rc = 0;
6383     int ring_prod, cqe_ring_prod;
6384     int max_agg_queues;
6385 
6386     for (i = 0; i < sc->num_queues; i++) {
6387         fp = &sc->fp[i];
6388 
6389         ring_prod = cqe_ring_prod = 0;
6390         fp->rx_bd_cons = 0;
6391         fp->rx_cq_cons = 0;
6392 
6393         /* allocate buffers for the RX BDs in RX BD chain */
6394         for (j = 0; j < sc->max_rx_bufs; j++) {
6395             rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6396             if (rc != 0) {
6397                 BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6398                       i, rc);
6399                 goto bxe_alloc_fp_buffers_error;
6400             }
6401 
6402             ring_prod     = RX_BD_NEXT(ring_prod);
6403             cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6404         }
6405 
6406         fp->rx_bd_prod = ring_prod;
6407         fp->rx_cq_prod = cqe_ring_prod;
6408         fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6409 
6410         max_agg_queues = MAX_AGG_QS(sc);
6411 
6412         fp->tpa_enable = TRUE;
6413 
6414         /* fill the TPA pool */
6415         for (j = 0; j < max_agg_queues; j++) {
6416             rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6417             if (rc != 0) {
6418                 BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6419                           i, j);
6420                 fp->tpa_enable = FALSE;
6421                 goto bxe_alloc_fp_buffers_error;
6422             }
6423 
6424             fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6425         }
6426 
6427         if (fp->tpa_enable) {
6428             /* fill the RX SGE chain */
6429             ring_prod = 0;
6430             for (j = 0; j < RX_SGE_USABLE; j++) {
6431                 rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6432                 if (rc != 0) {
6433                     BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6434                               i, ring_prod);
6435                     fp->tpa_enable = FALSE;
6436                     ring_prod = 0;
6437                     goto bxe_alloc_fp_buffers_error;
6438                 }
6439 
6440                 ring_prod = RX_SGE_NEXT(ring_prod);
6441             }
6442 
6443             fp->rx_sge_prod = ring_prod;
6444         }
6445     }
6446 
6447     return (0);
6448 
6449 bxe_alloc_fp_buffers_error:
6450 
6451     /* unwind what was already allocated */
6452     bxe_free_rx_bd_chain(fp);
6453     bxe_free_tpa_pool(fp);
6454     bxe_free_sge_chain(fp);
6455 
6456     return (ENOBUFS);
6457 }
6458 
6459 static void
6460 bxe_free_fw_stats_mem(struct bxe_softc *sc)
6461 {
6462     bxe_dma_free(sc, &sc->fw_stats_dma);
6463 
6464     sc->fw_stats_num = 0;
6465 
6466     sc->fw_stats_req_size = 0;
6467     sc->fw_stats_req = NULL;
6468     sc->fw_stats_req_mapping = 0;
6469 
6470     sc->fw_stats_data_size = 0;
6471     sc->fw_stats_data = NULL;
6472     sc->fw_stats_data_mapping = 0;
6473 }
6474 
6475 static int
6476 bxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6477 {
6478     uint8_t num_queue_stats;
6479     int num_groups;
6480 
6481     /* number of queues for statistics is number of eth queues */
6482     num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6483 
6484     /*
6485      * Total number of FW statistics requests =
6486      *   1 for port stats + 1 for PF stats + num of queues
6487      */
6488     sc->fw_stats_num = (2 + num_queue_stats);
6489 
6490     /*
6491      * Request is built from stats_query_header and an array of
6492      * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6493      * rules. The real number or requests is configured in the
6494      * stats_query_header.
6495      */
6496     num_groups =
6497         ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6498          ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6499 
6500     BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6501           sc->fw_stats_num, num_groups);
6502 
6503     sc->fw_stats_req_size =
6504         (sizeof(struct stats_query_header) +
6505          (num_groups * sizeof(struct stats_query_cmd_group)));
6506 
6507     /*
6508      * Data for statistics requests + stats_counter.
6509      * stats_counter holds per-STORM counters that are incremented when
6510      * STORM has finished with the current request. Memory for FCoE
6511      * offloaded statistics are counted anyway, even if they will not be sent.
6512      * VF stats are not accounted for here as the data of VF stats is stored
6513      * in memory allocated by the VF, not here.
6514      */
6515     sc->fw_stats_data_size =
6516         (sizeof(struct stats_counter) +
6517          sizeof(struct per_port_stats) +
6518          sizeof(struct per_pf_stats) +
6519          /* sizeof(struct fcoe_statistics_params) + */
6520          (sizeof(struct per_queue_stats) * num_queue_stats));
6521 
6522     if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6523                       &sc->fw_stats_dma, "fw stats") != 0) {
6524         bxe_free_fw_stats_mem(sc);
6525         return (-1);
6526     }
6527 
6528     /* set up the shortcuts */
6529 
6530     sc->fw_stats_req =
6531         (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6532     sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6533 
6534     sc->fw_stats_data =
6535         (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6536                                      sc->fw_stats_req_size);
6537     sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6538                                  sc->fw_stats_req_size);
6539 
6540     BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6541           (uintmax_t)sc->fw_stats_req_mapping);
6542 
6543     BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6544           (uintmax_t)sc->fw_stats_data_mapping);
6545 
6546     return (0);
6547 }
6548 
6549 /*
6550  * Bits map:
6551  * 0-7  - Engine0 load counter.
6552  * 8-15 - Engine1 load counter.
6553  * 16   - Engine0 RESET_IN_PROGRESS bit.
6554  * 17   - Engine1 RESET_IN_PROGRESS bit.
6555  * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6556  *        function on the engine
6557  * 19   - Engine1 ONE_IS_LOADED.
6558  * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6559  *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6560  *        for just the one belonging to its engine).
6561  */
6562 #define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6563 #define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6564 #define BXE_PATH0_LOAD_CNT_SHIFT  0
6565 #define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6566 #define BXE_PATH1_LOAD_CNT_SHIFT  8
6567 #define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6568 #define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6569 #define BXE_GLOBAL_RESET_BIT      0x00040000
6570 
6571 /* set the GLOBAL_RESET bit, should be run under rtnl lock */
6572 static void
6573 bxe_set_reset_global(struct bxe_softc *sc)
6574 {
6575     uint32_t val;
6576     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6577     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6578     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6579     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6580 }
6581 
6582 /* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6583 static void
6584 bxe_clear_reset_global(struct bxe_softc *sc)
6585 {
6586     uint32_t val;
6587     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6588     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6589     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6590     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6591 }
6592 
6593 /* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6594 static uint8_t
6595 bxe_reset_is_global(struct bxe_softc *sc)
6596 {
6597     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6598     BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6599     return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6600 }
6601 
6602 /* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6603 static void
6604 bxe_set_reset_done(struct bxe_softc *sc)
6605 {
6606     uint32_t val;
6607     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6608                                  BXE_PATH0_RST_IN_PROG_BIT;
6609 
6610     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6611 
6612     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6613     /* Clear the bit */
6614     val &= ~bit;
6615     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6616 
6617     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6618 }
6619 
6620 /* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6621 static void
6622 bxe_set_reset_in_progress(struct bxe_softc *sc)
6623 {
6624     uint32_t val;
6625     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6626                                  BXE_PATH0_RST_IN_PROG_BIT;
6627 
6628     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6629 
6630     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6631     /* Set the bit */
6632     val |= bit;
6633     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6634 
6635     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6636 }
6637 
6638 /* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6639 static uint8_t
6640 bxe_reset_is_done(struct bxe_softc *sc,
6641                   int              engine)
6642 {
6643     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6644     uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6645                             BXE_PATH0_RST_IN_PROG_BIT;
6646 
6647     /* return false if bit is set */
6648     return (val & bit) ? FALSE : TRUE;
6649 }
6650 
6651 /* get the load status for an engine, should be run under rtnl lock */
6652 static uint8_t
6653 bxe_get_load_status(struct bxe_softc *sc,
6654                     int              engine)
6655 {
6656     uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6657                              BXE_PATH0_LOAD_CNT_MASK;
6658     uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6659                               BXE_PATH0_LOAD_CNT_SHIFT;
6660     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6661 
6662     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6663 
6664     val = ((val & mask) >> shift);
6665 
6666     BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6667 
6668     return (val != 0);
6669 }
6670 
6671 /* set pf load mark */
6672 /* XXX needs to be under rtnl lock */
6673 static void
6674 bxe_set_pf_load(struct bxe_softc *sc)
6675 {
6676     uint32_t val;
6677     uint32_t val1;
6678     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6679                                   BXE_PATH0_LOAD_CNT_MASK;
6680     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6681                                    BXE_PATH0_LOAD_CNT_SHIFT;
6682 
6683     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6684 
6685     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6686     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6687 
6688     /* get the current counter value */
6689     val1 = ((val & mask) >> shift);
6690 
6691     /* set bit of this PF */
6692     val1 |= (1 << SC_ABS_FUNC(sc));
6693 
6694     /* clear the old value */
6695     val &= ~mask;
6696 
6697     /* set the new one */
6698     val |= ((val1 << shift) & mask);
6699 
6700     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6701 
6702     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6703 }
6704 
6705 /* clear pf load mark */
6706 /* XXX needs to be under rtnl lock */
6707 static uint8_t
6708 bxe_clear_pf_load(struct bxe_softc *sc)
6709 {
6710     uint32_t val1, val;
6711     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6712                                   BXE_PATH0_LOAD_CNT_MASK;
6713     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6714                                    BXE_PATH0_LOAD_CNT_SHIFT;
6715 
6716     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6717     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6718     BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6719 
6720     /* get the current counter value */
6721     val1 = (val & mask) >> shift;
6722 
6723     /* clear bit of that PF */
6724     val1 &= ~(1 << SC_ABS_FUNC(sc));
6725 
6726     /* clear the old value */
6727     val &= ~mask;
6728 
6729     /* set the new one */
6730     val |= ((val1 << shift) & mask);
6731 
6732     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6733     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6734     return (val1 != 0);
6735 }
6736 
6737 /* send load requrest to mcp and analyze response */
6738 static int
6739 bxe_nic_load_request(struct bxe_softc *sc,
6740                      uint32_t         *load_code)
6741 {
6742     /* init fw_seq */
6743     sc->fw_seq =
6744         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6745          DRV_MSG_SEQ_NUMBER_MASK);
6746 
6747     BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6748 
6749     /* get the current FW pulse sequence */
6750     sc->fw_drv_pulse_wr_seq =
6751         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6752          DRV_PULSE_SEQ_MASK);
6753 
6754     BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6755           sc->fw_drv_pulse_wr_seq);
6756 
6757     /* load request */
6758     (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6759                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6760 
6761     /* if the MCP fails to respond we must abort */
6762     if (!(*load_code)) {
6763         BLOGE(sc, "MCP response failure!\n");
6764         return (-1);
6765     }
6766 
6767     /* if MCP refused then must abort */
6768     if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6769         BLOGE(sc, "MCP refused load request\n");
6770         return (-1);
6771     }
6772 
6773     return (0);
6774 }
6775 
6776 /*
6777  * Check whether another PF has already loaded FW to chip. In virtualized
6778  * environments a pf from anoth VM may have already initialized the device
6779  * including loading FW.
6780  */
6781 static int
6782 bxe_nic_load_analyze_req(struct bxe_softc *sc,
6783                          uint32_t         load_code)
6784 {
6785     uint32_t my_fw, loaded_fw;
6786 
6787     /* is another pf loaded on this engine? */
6788     if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6789         (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6790         /* build my FW version dword */
6791         my_fw = (BCM_5710_FW_MAJOR_VERSION +
6792                  (BCM_5710_FW_MINOR_VERSION << 8 ) +
6793                  (BCM_5710_FW_REVISION_VERSION << 16) +
6794                  (BCM_5710_FW_ENGINEERING_VERSION << 24));
6795 
6796         /* read loaded FW from chip */
6797         loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6798         BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6799               loaded_fw, my_fw);
6800 
6801         /* abort nic load if version mismatch */
6802         if (my_fw != loaded_fw) {
6803             BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6804                   loaded_fw, my_fw);
6805             return (-1);
6806         }
6807     }
6808 
6809     return (0);
6810 }
6811 
6812 /* mark PMF if applicable */
6813 static void
6814 bxe_nic_load_pmf(struct bxe_softc *sc,
6815                  uint32_t         load_code)
6816 {
6817     uint32_t ncsi_oem_data_addr;
6818 
6819     if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6820         (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6821         (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6822         /*
6823          * Barrier here for ordering between the writing to sc->port.pmf here
6824          * and reading it from the periodic task.
6825          */
6826         sc->port.pmf = 1;
6827         mb();
6828     } else {
6829         sc->port.pmf = 0;
6830     }
6831 
6832     BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6833 
6834     /* XXX needed? */
6835     if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6836         if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6837             ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6838             if (ncsi_oem_data_addr) {
6839                 REG_WR(sc,
6840                        (ncsi_oem_data_addr +
6841                         offsetof(struct glob_ncsi_oem_data, driver_version)),
6842                        0);
6843             }
6844         }
6845     }
6846 }
6847 
6848 static void
6849 bxe_read_mf_cfg(struct bxe_softc *sc)
6850 {
6851     int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
6852     int abs_func;
6853     int vn;
6854 
6855     if (BXE_NOMCP(sc)) {
6856         return; /* what should be the default bvalue in this case */
6857     }
6858 
6859     /*
6860      * The formula for computing the absolute function number is...
6861      * For 2 port configuration (4 functions per port):
6862      *   abs_func = 2 * vn + SC_PORT + SC_PATH
6863      * For 4 port configuration (2 functions per port):
6864      *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
6865      */
6866     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
6867         abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
6868         if (abs_func >= E1H_FUNC_MAX) {
6869             break;
6870         }
6871         sc->devinfo.mf_info.mf_config[vn] =
6872             MFCFG_RD(sc, func_mf_config[abs_func].config);
6873     }
6874 
6875     if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
6876         FUNC_MF_CFG_FUNC_DISABLED) {
6877         BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
6878         sc->flags |= BXE_MF_FUNC_DIS;
6879     } else {
6880         BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
6881         sc->flags &= ~BXE_MF_FUNC_DIS;
6882     }
6883 }
6884 
6885 /* acquire split MCP access lock register */
6886 static int bxe_acquire_alr(struct bxe_softc *sc)
6887 {
6888     uint32_t j, val;
6889 
6890     for (j = 0; j < 1000; j++) {
6891         val = (1UL << 31);
6892         REG_WR(sc, GRCBASE_MCP + 0x9c, val);
6893         val = REG_RD(sc, GRCBASE_MCP + 0x9c);
6894         if (val & (1L << 31))
6895             break;
6896 
6897         DELAY(5000);
6898     }
6899 
6900     if (!(val & (1L << 31))) {
6901         BLOGE(sc, "Cannot acquire MCP access lock register\n");
6902         return (-1);
6903     }
6904 
6905     return (0);
6906 }
6907 
6908 /* release split MCP access lock register */
6909 static void bxe_release_alr(struct bxe_softc *sc)
6910 {
6911     REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
6912 }
6913 
6914 static void
6915 bxe_fan_failure(struct bxe_softc *sc)
6916 {
6917     int port = SC_PORT(sc);
6918     uint32_t ext_phy_config;
6919 
6920     /* mark the failure */
6921     ext_phy_config =
6922         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
6923 
6924     ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
6925     ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
6926     SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
6927              ext_phy_config);
6928 
6929     /* log the failure */
6930     BLOGW(sc, "Fan Failure has caused the driver to shutdown "
6931               "the card to prevent permanent damage. "
6932               "Please contact OEM Support for assistance\n");
6933 
6934     /* XXX */
6935 #if 1
6936     bxe_panic(sc, ("Schedule task to handle fan failure\n"));
6937 #else
6938     /*
6939      * Schedule device reset (unload)
6940      * This is due to some boards consuming sufficient power when driver is
6941      * up to overheat if fan fails.
6942      */
6943     bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
6944     schedule_delayed_work(&sc->sp_rtnl_task, 0);
6945 #endif
6946 }
6947 
6948 /* this function is called upon a link interrupt */
6949 static void
6950 bxe_link_attn(struct bxe_softc *sc)
6951 {
6952     uint32_t pause_enabled = 0;
6953     struct host_port_stats *pstats;
6954     int cmng_fns;
6955 
6956     /* Make sure that we are synced with the current statistics */
6957     bxe_stats_handle(sc, STATS_EVENT_STOP);
6958 
6959     elink_link_update(&sc->link_params, &sc->link_vars);
6960 
6961     if (sc->link_vars.link_up) {
6962 
6963         /* dropless flow control */
6964         if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
6965             pause_enabled = 0;
6966 
6967             if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
6968                 pause_enabled = 1;
6969             }
6970 
6971             REG_WR(sc,
6972                    (BAR_USTRORM_INTMEM +
6973                     USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
6974                    pause_enabled);
6975         }
6976 
6977         if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
6978             pstats = BXE_SP(sc, port_stats);
6979             /* reset old mac stats */
6980             memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
6981         }
6982 
6983         if (sc->state == BXE_STATE_OPEN) {
6984             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
6985         }
6986     }
6987 
6988     if (sc->link_vars.link_up && sc->link_vars.line_speed) {
6989         cmng_fns = bxe_get_cmng_fns_mode(sc);
6990 
6991         if (cmng_fns != CMNG_FNS_NONE) {
6992             bxe_cmng_fns_init(sc, FALSE, cmng_fns);
6993             storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
6994         } else {
6995             /* rate shaping and fairness are disabled */
6996             BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
6997         }
6998     }
6999 
7000     bxe_link_report_locked(sc);
7001 
7002     if (IS_MF(sc)) {
7003         ; // XXX bxe_link_sync_notify(sc);
7004     }
7005 }
7006 
7007 static void
7008 bxe_attn_int_asserted(struct bxe_softc *sc,
7009                       uint32_t         asserted)
7010 {
7011     int port = SC_PORT(sc);
7012     uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7013                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
7014     uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7015                                         NIG_REG_MASK_INTERRUPT_PORT0;
7016     uint32_t aeu_mask;
7017     uint32_t nig_mask = 0;
7018     uint32_t reg_addr;
7019     uint32_t igu_acked;
7020     uint32_t cnt;
7021 
7022     if (sc->attn_state & asserted) {
7023         BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7024     }
7025 
7026     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7027 
7028     aeu_mask = REG_RD(sc, aeu_addr);
7029 
7030     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7031           aeu_mask, asserted);
7032 
7033     aeu_mask &= ~(asserted & 0x3ff);
7034 
7035     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7036 
7037     REG_WR(sc, aeu_addr, aeu_mask);
7038 
7039     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7040 
7041     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7042     sc->attn_state |= asserted;
7043     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7044 
7045     if (asserted & ATTN_HARD_WIRED_MASK) {
7046         if (asserted & ATTN_NIG_FOR_FUNC) {
7047 
7048 	    bxe_acquire_phy_lock(sc);
7049             /* save nig interrupt mask */
7050             nig_mask = REG_RD(sc, nig_int_mask_addr);
7051 
7052             /* If nig_mask is not set, no need to call the update function */
7053             if (nig_mask) {
7054                 REG_WR(sc, nig_int_mask_addr, 0);
7055 
7056                 bxe_link_attn(sc);
7057             }
7058 
7059             /* handle unicore attn? */
7060         }
7061 
7062         if (asserted & ATTN_SW_TIMER_4_FUNC) {
7063             BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7064         }
7065 
7066         if (asserted & GPIO_2_FUNC) {
7067             BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7068         }
7069 
7070         if (asserted & GPIO_3_FUNC) {
7071             BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7072         }
7073 
7074         if (asserted & GPIO_4_FUNC) {
7075             BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7076         }
7077 
7078         if (port == 0) {
7079             if (asserted & ATTN_GENERAL_ATTN_1) {
7080                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7081                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7082             }
7083             if (asserted & ATTN_GENERAL_ATTN_2) {
7084                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7085                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7086             }
7087             if (asserted & ATTN_GENERAL_ATTN_3) {
7088                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7089                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7090             }
7091         } else {
7092             if (asserted & ATTN_GENERAL_ATTN_4) {
7093                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7094                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7095             }
7096             if (asserted & ATTN_GENERAL_ATTN_5) {
7097                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7098                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7099             }
7100             if (asserted & ATTN_GENERAL_ATTN_6) {
7101                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7102                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7103             }
7104         }
7105     } /* hardwired */
7106 
7107     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7108         reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7109     } else {
7110         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7111     }
7112 
7113     BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7114           asserted,
7115           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7116     REG_WR(sc, reg_addr, asserted);
7117 
7118     /* now set back the mask */
7119     if (asserted & ATTN_NIG_FOR_FUNC) {
7120         /*
7121          * Verify that IGU ack through BAR was written before restoring
7122          * NIG mask. This loop should exit after 2-3 iterations max.
7123          */
7124         if (sc->devinfo.int_block != INT_BLOCK_HC) {
7125             cnt = 0;
7126 
7127             do {
7128                 igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7129             } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7130                      (++cnt < MAX_IGU_ATTN_ACK_TO));
7131 
7132             if (!igu_acked) {
7133                 BLOGE(sc, "Failed to verify IGU ack on time\n");
7134             }
7135 
7136             mb();
7137         }
7138 
7139         REG_WR(sc, nig_int_mask_addr, nig_mask);
7140 
7141 	bxe_release_phy_lock(sc);
7142     }
7143 }
7144 
7145 static void
7146 bxe_print_next_block(struct bxe_softc *sc,
7147                      int              idx,
7148                      const char       *blk)
7149 {
7150     BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7151 }
7152 
7153 static int
7154 bxe_check_blocks_with_parity0(struct bxe_softc *sc,
7155                               uint32_t         sig,
7156                               int              par_num,
7157                               uint8_t          print)
7158 {
7159     uint32_t cur_bit = 0;
7160     int i = 0;
7161 
7162     for (i = 0; sig; i++) {
7163         cur_bit = ((uint32_t)0x1 << i);
7164         if (sig & cur_bit) {
7165             switch (cur_bit) {
7166             case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7167                 if (print)
7168                     bxe_print_next_block(sc, par_num++, "BRB");
7169                 break;
7170             case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7171                 if (print)
7172                     bxe_print_next_block(sc, par_num++, "PARSER");
7173                 break;
7174             case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7175                 if (print)
7176                     bxe_print_next_block(sc, par_num++, "TSDM");
7177                 break;
7178             case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7179                 if (print)
7180                     bxe_print_next_block(sc, par_num++, "SEARCHER");
7181                 break;
7182             case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7183                 if (print)
7184                     bxe_print_next_block(sc, par_num++, "TCM");
7185                 break;
7186             case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7187                 if (print)
7188                     bxe_print_next_block(sc, par_num++, "TSEMI");
7189                 break;
7190             case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7191                 if (print)
7192                     bxe_print_next_block(sc, par_num++, "XPB");
7193                 break;
7194             }
7195 
7196             /* Clear the bit */
7197             sig &= ~cur_bit;
7198         }
7199     }
7200 
7201     return (par_num);
7202 }
7203 
7204 static int
7205 bxe_check_blocks_with_parity1(struct bxe_softc *sc,
7206                               uint32_t         sig,
7207                               int              par_num,
7208                               uint8_t          *global,
7209                               uint8_t          print)
7210 {
7211     int i = 0;
7212     uint32_t cur_bit = 0;
7213     for (i = 0; sig; i++) {
7214         cur_bit = ((uint32_t)0x1 << i);
7215         if (sig & cur_bit) {
7216             switch (cur_bit) {
7217             case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7218                 if (print)
7219                     bxe_print_next_block(sc, par_num++, "PBF");
7220                 break;
7221             case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7222                 if (print)
7223                     bxe_print_next_block(sc, par_num++, "QM");
7224                 break;
7225             case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7226                 if (print)
7227                     bxe_print_next_block(sc, par_num++, "TM");
7228                 break;
7229             case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7230                 if (print)
7231                     bxe_print_next_block(sc, par_num++, "XSDM");
7232                 break;
7233             case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7234                 if (print)
7235                     bxe_print_next_block(sc, par_num++, "XCM");
7236                 break;
7237             case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7238                 if (print)
7239                     bxe_print_next_block(sc, par_num++, "XSEMI");
7240                 break;
7241             case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7242                 if (print)
7243                     bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7244                 break;
7245             case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7246                 if (print)
7247                     bxe_print_next_block(sc, par_num++, "NIG");
7248                 break;
7249             case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7250                 if (print)
7251                     bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7252                 *global = TRUE;
7253                 break;
7254             case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7255                 if (print)
7256                     bxe_print_next_block(sc, par_num++, "DEBUG");
7257                 break;
7258             case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7259                 if (print)
7260                     bxe_print_next_block(sc, par_num++, "USDM");
7261                 break;
7262             case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7263                 if (print)
7264                     bxe_print_next_block(sc, par_num++, "UCM");
7265                 break;
7266             case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7267                 if (print)
7268                     bxe_print_next_block(sc, par_num++, "USEMI");
7269                 break;
7270             case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7271                 if (print)
7272                     bxe_print_next_block(sc, par_num++, "UPB");
7273                 break;
7274             case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7275                 if (print)
7276                     bxe_print_next_block(sc, par_num++, "CSDM");
7277                 break;
7278             case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7279                 if (print)
7280                     bxe_print_next_block(sc, par_num++, "CCM");
7281                 break;
7282             }
7283 
7284             /* Clear the bit */
7285             sig &= ~cur_bit;
7286         }
7287     }
7288 
7289     return (par_num);
7290 }
7291 
7292 static int
7293 bxe_check_blocks_with_parity2(struct bxe_softc *sc,
7294                               uint32_t         sig,
7295                               int              par_num,
7296                               uint8_t          print)
7297 {
7298     uint32_t cur_bit = 0;
7299     int i = 0;
7300 
7301     for (i = 0; sig; i++) {
7302         cur_bit = ((uint32_t)0x1 << i);
7303         if (sig & cur_bit) {
7304             switch (cur_bit) {
7305             case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7306                 if (print)
7307                     bxe_print_next_block(sc, par_num++, "CSEMI");
7308                 break;
7309             case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7310                 if (print)
7311                     bxe_print_next_block(sc, par_num++, "PXP");
7312                 break;
7313             case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7314                 if (print)
7315                     bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7316                 break;
7317             case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7318                 if (print)
7319                     bxe_print_next_block(sc, par_num++, "CFC");
7320                 break;
7321             case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7322                 if (print)
7323                     bxe_print_next_block(sc, par_num++, "CDU");
7324                 break;
7325             case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7326                 if (print)
7327                     bxe_print_next_block(sc, par_num++, "DMAE");
7328                 break;
7329             case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7330                 if (print)
7331                     bxe_print_next_block(sc, par_num++, "IGU");
7332                 break;
7333             case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7334                 if (print)
7335                     bxe_print_next_block(sc, par_num++, "MISC");
7336                 break;
7337             }
7338 
7339             /* Clear the bit */
7340             sig &= ~cur_bit;
7341         }
7342     }
7343 
7344     return (par_num);
7345 }
7346 
7347 static int
7348 bxe_check_blocks_with_parity3(struct bxe_softc *sc,
7349                               uint32_t         sig,
7350                               int              par_num,
7351                               uint8_t          *global,
7352                               uint8_t          print)
7353 {
7354     uint32_t cur_bit = 0;
7355     int i = 0;
7356 
7357     for (i = 0; sig; i++) {
7358         cur_bit = ((uint32_t)0x1 << i);
7359         if (sig & cur_bit) {
7360             switch (cur_bit) {
7361             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7362                 if (print)
7363                     bxe_print_next_block(sc, par_num++, "MCP ROM");
7364                 *global = TRUE;
7365                 break;
7366             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7367                 if (print)
7368                     bxe_print_next_block(sc, par_num++,
7369                               "MCP UMP RX");
7370                 *global = TRUE;
7371                 break;
7372             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7373                 if (print)
7374                     bxe_print_next_block(sc, par_num++,
7375                               "MCP UMP TX");
7376                 *global = TRUE;
7377                 break;
7378             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7379                 if (print)
7380                     bxe_print_next_block(sc, par_num++,
7381                               "MCP SCPAD");
7382                 *global = TRUE;
7383                 break;
7384             }
7385 
7386             /* Clear the bit */
7387             sig &= ~cur_bit;
7388         }
7389     }
7390 
7391     return (par_num);
7392 }
7393 
7394 static int
7395 bxe_check_blocks_with_parity4(struct bxe_softc *sc,
7396                               uint32_t         sig,
7397                               int              par_num,
7398                               uint8_t          print)
7399 {
7400     uint32_t cur_bit = 0;
7401     int i = 0;
7402 
7403     for (i = 0; sig; i++) {
7404         cur_bit = ((uint32_t)0x1 << i);
7405         if (sig & cur_bit) {
7406             switch (cur_bit) {
7407             case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7408                 if (print)
7409                     bxe_print_next_block(sc, par_num++, "PGLUE_B");
7410                 break;
7411             case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7412                 if (print)
7413                     bxe_print_next_block(sc, par_num++, "ATC");
7414                 break;
7415             }
7416 
7417             /* Clear the bit */
7418             sig &= ~cur_bit;
7419         }
7420     }
7421 
7422     return (par_num);
7423 }
7424 
7425 static uint8_t
7426 bxe_parity_attn(struct bxe_softc *sc,
7427                 uint8_t          *global,
7428                 uint8_t          print,
7429                 uint32_t         *sig)
7430 {
7431     int par_num = 0;
7432 
7433     if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7434         (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7435         (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7436         (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7437         (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7438         BLOGE(sc, "Parity error: HW block parity attention:\n"
7439                   "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7440               (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7441               (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7442               (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7443               (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7444               (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7445 
7446         if (print)
7447             BLOGI(sc, "Parity errors detected in blocks: ");
7448 
7449         par_num =
7450             bxe_check_blocks_with_parity0(sc, sig[0] &
7451                                           HW_PRTY_ASSERT_SET_0,
7452                                           par_num, print);
7453         par_num =
7454             bxe_check_blocks_with_parity1(sc, sig[1] &
7455                                           HW_PRTY_ASSERT_SET_1,
7456                                           par_num, global, print);
7457         par_num =
7458             bxe_check_blocks_with_parity2(sc, sig[2] &
7459                                           HW_PRTY_ASSERT_SET_2,
7460                                           par_num, print);
7461         par_num =
7462             bxe_check_blocks_with_parity3(sc, sig[3] &
7463                                           HW_PRTY_ASSERT_SET_3,
7464                                           par_num, global, print);
7465         par_num =
7466             bxe_check_blocks_with_parity4(sc, sig[4] &
7467                                           HW_PRTY_ASSERT_SET_4,
7468                                           par_num, print);
7469 
7470         if (print)
7471             BLOGI(sc, "\n");
7472 
7473         return (TRUE);
7474     }
7475 
7476     return (FALSE);
7477 }
7478 
7479 static uint8_t
7480 bxe_chk_parity_attn(struct bxe_softc *sc,
7481                     uint8_t          *global,
7482                     uint8_t          print)
7483 {
7484     struct attn_route attn = { {0} };
7485     int port = SC_PORT(sc);
7486 
7487     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7488     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7489     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7490     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7491 
7492     /*
7493      * Since MCP attentions can't be disabled inside the block, we need to
7494      * read AEU registers to see whether they're currently disabled
7495      */
7496     attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7497                                       : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7498                          MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7499                         ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7500 
7501 
7502     if (!CHIP_IS_E1x(sc))
7503         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7504 
7505     return (bxe_parity_attn(sc, global, print, attn.sig));
7506 }
7507 
7508 static void
7509 bxe_attn_int_deasserted4(struct bxe_softc *sc,
7510                          uint32_t         attn)
7511 {
7512     uint32_t val;
7513 
7514     if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7515         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7516         BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7517         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7518             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7519         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7520             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7521         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7522             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7523         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7524             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7525         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7526             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7527         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7528             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7529         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7530             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7531         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7532             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7533         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7534             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7535     }
7536 
7537     if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7538         val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7539         BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7540         if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7541             BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7542         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7543             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7544         if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7545             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7546         if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7547             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7548         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7549             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7550         if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7551             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7552     }
7553 
7554     if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7555                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7556         BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7557               (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7558                                  AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7559     }
7560 }
7561 
7562 static void
7563 bxe_e1h_disable(struct bxe_softc *sc)
7564 {
7565     int port = SC_PORT(sc);
7566 
7567     bxe_tx_disable(sc);
7568 
7569     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7570 }
7571 
7572 static void
7573 bxe_e1h_enable(struct bxe_softc *sc)
7574 {
7575     int port = SC_PORT(sc);
7576 
7577     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7578 
7579     // XXX bxe_tx_enable(sc);
7580 }
7581 
7582 /*
7583  * called due to MCP event (on pmf):
7584  *   reread new bandwidth configuration
7585  *   configure FW
7586  *   notify others function about the change
7587  */
7588 static void
7589 bxe_config_mf_bw(struct bxe_softc *sc)
7590 {
7591     if (sc->link_vars.link_up) {
7592         bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7593         // XXX bxe_link_sync_notify(sc);
7594     }
7595 
7596     storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7597 }
7598 
7599 static void
7600 bxe_set_mf_bw(struct bxe_softc *sc)
7601 {
7602     bxe_config_mf_bw(sc);
7603     bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7604 }
7605 
7606 static void
7607 bxe_handle_eee_event(struct bxe_softc *sc)
7608 {
7609     BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7610     bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7611 }
7612 
7613 #define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7614 
7615 static void
7616 bxe_drv_info_ether_stat(struct bxe_softc *sc)
7617 {
7618     struct eth_stats_info *ether_stat =
7619         &sc->sp->drv_info_to_mcp.ether_stat;
7620 
7621     strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7622             ETH_STAT_INFO_VERSION_LEN);
7623 
7624     /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7625     sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7626                                           DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7627                                           ether_stat->mac_local + MAC_PAD,
7628                                           MAC_PAD, ETH_ALEN);
7629 
7630     ether_stat->mtu_size = sc->mtu;
7631 
7632     ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7633     if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
7634         ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7635     }
7636 
7637     // XXX ether_stat->feature_flags |= ???;
7638 
7639     ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7640 
7641     ether_stat->txq_size = sc->tx_ring_size;
7642     ether_stat->rxq_size = sc->rx_ring_size;
7643 }
7644 
7645 static void
7646 bxe_handle_drv_info_req(struct bxe_softc *sc)
7647 {
7648     enum drv_info_opcode op_code;
7649     uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7650 
7651     /* if drv_info version supported by MFW doesn't match - send NACK */
7652     if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7653         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7654         return;
7655     }
7656 
7657     op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7658                DRV_INFO_CONTROL_OP_CODE_SHIFT);
7659 
7660     memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7661 
7662     switch (op_code) {
7663     case ETH_STATS_OPCODE:
7664         bxe_drv_info_ether_stat(sc);
7665         break;
7666     case FCOE_STATS_OPCODE:
7667     case ISCSI_STATS_OPCODE:
7668     default:
7669         /* if op code isn't supported - send NACK */
7670         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7671         return;
7672     }
7673 
7674     /*
7675      * If we got drv_info attn from MFW then these fields are defined in
7676      * shmem2 for sure
7677      */
7678     SHMEM2_WR(sc, drv_info_host_addr_lo,
7679               U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7680     SHMEM2_WR(sc, drv_info_host_addr_hi,
7681               U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7682 
7683     bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7684 }
7685 
7686 static void
7687 bxe_dcc_event(struct bxe_softc *sc,
7688               uint32_t         dcc_event)
7689 {
7690     BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7691 
7692     if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7693         /*
7694          * This is the only place besides the function initialization
7695          * where the sc->flags can change so it is done without any
7696          * locks
7697          */
7698         if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7699             BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7700             sc->flags |= BXE_MF_FUNC_DIS;
7701             bxe_e1h_disable(sc);
7702         } else {
7703             BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7704             sc->flags &= ~BXE_MF_FUNC_DIS;
7705             bxe_e1h_enable(sc);
7706         }
7707         dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7708     }
7709 
7710     if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7711         bxe_config_mf_bw(sc);
7712         dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7713     }
7714 
7715     /* Report results to MCP */
7716     if (dcc_event)
7717         bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7718     else
7719         bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7720 }
7721 
7722 static void
7723 bxe_pmf_update(struct bxe_softc *sc)
7724 {
7725     int port = SC_PORT(sc);
7726     uint32_t val;
7727 
7728     sc->port.pmf = 1;
7729     BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7730 
7731     /*
7732      * We need the mb() to ensure the ordering between the writing to
7733      * sc->port.pmf here and reading it from the bxe_periodic_task().
7734      */
7735     mb();
7736 
7737     /* queue a periodic task */
7738     // XXX schedule task...
7739 
7740     // XXX bxe_dcbx_pmf_update(sc);
7741 
7742     /* enable nig attention */
7743     val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7744     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7745         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7746         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7747     } else if (!CHIP_IS_E1x(sc)) {
7748         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7749         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7750     }
7751 
7752     bxe_stats_handle(sc, STATS_EVENT_PMF);
7753 }
7754 
7755 static int
7756 bxe_mc_assert(struct bxe_softc *sc)
7757 {
7758     char last_idx;
7759     int i, rc = 0;
7760     uint32_t row0, row1, row2, row3;
7761 
7762     /* XSTORM */
7763     last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7764     if (last_idx)
7765         BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7766 
7767     /* print the asserts */
7768     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7769 
7770         row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7771         row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7772         row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7773         row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7774 
7775         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7776             BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7777                   i, row3, row2, row1, row0);
7778             rc++;
7779         } else {
7780             break;
7781         }
7782     }
7783 
7784     /* TSTORM */
7785     last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7786     if (last_idx) {
7787         BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7788     }
7789 
7790     /* print the asserts */
7791     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7792 
7793         row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7794         row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7795         row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7796         row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7797 
7798         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7799             BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7800                   i, row3, row2, row1, row0);
7801             rc++;
7802         } else {
7803             break;
7804         }
7805     }
7806 
7807     /* CSTORM */
7808     last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7809     if (last_idx) {
7810         BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7811     }
7812 
7813     /* print the asserts */
7814     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7815 
7816         row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7817         row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7818         row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7819         row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7820 
7821         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7822             BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7823                   i, row3, row2, row1, row0);
7824             rc++;
7825         } else {
7826             break;
7827         }
7828     }
7829 
7830     /* USTORM */
7831     last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7832     if (last_idx) {
7833         BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7834     }
7835 
7836     /* print the asserts */
7837     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7838 
7839         row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7840         row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7841         row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7842         row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7843 
7844         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7845             BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7846                   i, row3, row2, row1, row0);
7847             rc++;
7848         } else {
7849             break;
7850         }
7851     }
7852 
7853     return (rc);
7854 }
7855 
7856 static void
7857 bxe_attn_int_deasserted3(struct bxe_softc *sc,
7858                          uint32_t         attn)
7859 {
7860     int func = SC_FUNC(sc);
7861     uint32_t val;
7862 
7863     if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
7864 
7865         if (attn & BXE_PMF_LINK_ASSERT(sc)) {
7866 
7867             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
7868             bxe_read_mf_cfg(sc);
7869             sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
7870                 MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
7871             val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
7872 
7873             if (val & DRV_STATUS_DCC_EVENT_MASK)
7874                 bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
7875 
7876             if (val & DRV_STATUS_SET_MF_BW)
7877                 bxe_set_mf_bw(sc);
7878 
7879             if (val & DRV_STATUS_DRV_INFO_REQ)
7880                 bxe_handle_drv_info_req(sc);
7881 
7882             if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
7883                 bxe_pmf_update(sc);
7884 
7885             if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
7886                 bxe_handle_eee_event(sc);
7887 
7888             if (sc->link_vars.periodic_flags &
7889                 ELINK_PERIODIC_FLAGS_LINK_EVENT) {
7890                 /* sync with link */
7891 		bxe_acquire_phy_lock(sc);
7892                 sc->link_vars.periodic_flags &=
7893                     ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
7894 		bxe_release_phy_lock(sc);
7895                 if (IS_MF(sc))
7896                     ; // XXX bxe_link_sync_notify(sc);
7897                 bxe_link_report(sc);
7898             }
7899 
7900             /*
7901              * Always call it here: bxe_link_report() will
7902              * prevent the link indication duplication.
7903              */
7904             bxe_link_status_update(sc);
7905 
7906         } else if (attn & BXE_MC_ASSERT_BITS) {
7907 
7908             BLOGE(sc, "MC assert!\n");
7909             bxe_mc_assert(sc);
7910             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
7911             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
7912             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
7913             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
7914             bxe_panic(sc, ("MC assert!\n"));
7915 
7916         } else if (attn & BXE_MCP_ASSERT) {
7917 
7918             BLOGE(sc, "MCP assert!\n");
7919             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
7920             // XXX bxe_fw_dump(sc);
7921 
7922         } else {
7923             BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
7924         }
7925     }
7926 
7927     if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
7928         BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
7929         if (attn & BXE_GRC_TIMEOUT) {
7930             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
7931             BLOGE(sc, "GRC time-out 0x%08x\n", val);
7932         }
7933         if (attn & BXE_GRC_RSV) {
7934             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
7935             BLOGE(sc, "GRC reserved 0x%08x\n", val);
7936         }
7937         REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
7938     }
7939 }
7940 
7941 static void
7942 bxe_attn_int_deasserted2(struct bxe_softc *sc,
7943                          uint32_t         attn)
7944 {
7945     int port = SC_PORT(sc);
7946     int reg_offset;
7947     uint32_t val0, mask0, val1, mask1;
7948     uint32_t val;
7949 
7950     if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
7951         val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
7952         BLOGE(sc, "CFC hw attention 0x%08x\n", val);
7953         /* CFC error attention */
7954         if (val & 0x2) {
7955             BLOGE(sc, "FATAL error from CFC\n");
7956         }
7957     }
7958 
7959     if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
7960         val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
7961         BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
7962         /* RQ_USDMDP_FIFO_OVERFLOW */
7963         if (val & 0x18000) {
7964             BLOGE(sc, "FATAL error from PXP\n");
7965         }
7966 
7967         if (!CHIP_IS_E1x(sc)) {
7968             val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
7969             BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
7970         }
7971     }
7972 
7973 #define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
7974 #define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
7975 
7976     if (attn & AEU_PXP2_HW_INT_BIT) {
7977         /*  CQ47854 workaround do not panic on
7978          *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
7979          */
7980         if (!CHIP_IS_E1x(sc)) {
7981             mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
7982             val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
7983             mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
7984             val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
7985             /*
7986              * If the only PXP2_EOP_ERROR_BIT is set in
7987              * STS0 and STS1 - clear it
7988              *
7989              * probably we lose additional attentions between
7990              * STS0 and STS_CLR0, in this case user will not
7991              * be notified about them
7992              */
7993             if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
7994                 !(val1 & mask1))
7995                 val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
7996 
7997             /* print the register, since no one can restore it */
7998             BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
7999 
8000             /*
8001              * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8002              * then notify
8003              */
8004             if (val0 & PXP2_EOP_ERROR_BIT) {
8005                 BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8006 
8007                 /*
8008                  * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8009                  * set then clear attention from PXP2 block without panic
8010                  */
8011                 if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8012                     ((val1 & mask1) == 0))
8013                     attn &= ~AEU_PXP2_HW_INT_BIT;
8014             }
8015         }
8016     }
8017 
8018     if (attn & HW_INTERRUT_ASSERT_SET_2) {
8019         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8020                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8021 
8022         val = REG_RD(sc, reg_offset);
8023         val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8024         REG_WR(sc, reg_offset, val);
8025 
8026         BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8027               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8028         bxe_panic(sc, ("HW block attention set2\n"));
8029     }
8030 }
8031 
8032 static void
8033 bxe_attn_int_deasserted1(struct bxe_softc *sc,
8034                          uint32_t         attn)
8035 {
8036     int port = SC_PORT(sc);
8037     int reg_offset;
8038     uint32_t val;
8039 
8040     if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8041         val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8042         BLOGE(sc, "DB hw attention 0x%08x\n", val);
8043         /* DORQ discard attention */
8044         if (val & 0x2) {
8045             BLOGE(sc, "FATAL error from DORQ\n");
8046         }
8047     }
8048 
8049     if (attn & HW_INTERRUT_ASSERT_SET_1) {
8050         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8051                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8052 
8053         val = REG_RD(sc, reg_offset);
8054         val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8055         REG_WR(sc, reg_offset, val);
8056 
8057         BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8058               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8059         bxe_panic(sc, ("HW block attention set1\n"));
8060     }
8061 }
8062 
8063 static void
8064 bxe_attn_int_deasserted0(struct bxe_softc *sc,
8065                          uint32_t         attn)
8066 {
8067     int port = SC_PORT(sc);
8068     int reg_offset;
8069     uint32_t val;
8070 
8071     reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8072                           MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8073 
8074     if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8075         val = REG_RD(sc, reg_offset);
8076         val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8077         REG_WR(sc, reg_offset, val);
8078 
8079         BLOGW(sc, "SPIO5 hw attention\n");
8080 
8081         /* Fan failure attention */
8082         elink_hw_reset_phy(&sc->link_params);
8083         bxe_fan_failure(sc);
8084     }
8085 
8086     if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8087 	bxe_acquire_phy_lock(sc);
8088         elink_handle_module_detect_int(&sc->link_params);
8089 	bxe_release_phy_lock(sc);
8090     }
8091 
8092     if (attn & HW_INTERRUT_ASSERT_SET_0) {
8093         val = REG_RD(sc, reg_offset);
8094         val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8095         REG_WR(sc, reg_offset, val);
8096 
8097         bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8098                        (attn & HW_INTERRUT_ASSERT_SET_0)));
8099     }
8100 }
8101 
8102 static void
8103 bxe_attn_int_deasserted(struct bxe_softc *sc,
8104                         uint32_t         deasserted)
8105 {
8106     struct attn_route attn;
8107     struct attn_route *group_mask;
8108     int port = SC_PORT(sc);
8109     int index;
8110     uint32_t reg_addr;
8111     uint32_t val;
8112     uint32_t aeu_mask;
8113     uint8_t global = FALSE;
8114 
8115     /*
8116      * Need to take HW lock because MCP or other port might also
8117      * try to handle this event.
8118      */
8119     bxe_acquire_alr(sc);
8120 
8121     if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8122         /* XXX
8123          * In case of parity errors don't handle attentions so that
8124          * other function would "see" parity errors.
8125          */
8126         sc->recovery_state = BXE_RECOVERY_INIT;
8127         // XXX schedule a recovery task...
8128         /* disable HW interrupts */
8129         bxe_int_disable(sc);
8130         bxe_release_alr(sc);
8131         return;
8132     }
8133 
8134     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8135     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8136     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8137     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8138     if (!CHIP_IS_E1x(sc)) {
8139         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8140     } else {
8141         attn.sig[4] = 0;
8142     }
8143 
8144     BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8145           attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8146 
8147     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8148         if (deasserted & (1 << index)) {
8149             group_mask = &sc->attn_group[index];
8150 
8151             BLOGD(sc, DBG_INTR,
8152                   "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8153                   group_mask->sig[0], group_mask->sig[1],
8154                   group_mask->sig[2], group_mask->sig[3],
8155                   group_mask->sig[4]);
8156 
8157             bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8158             bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8159             bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8160             bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8161             bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8162         }
8163     }
8164 
8165     bxe_release_alr(sc);
8166 
8167     if (sc->devinfo.int_block == INT_BLOCK_HC) {
8168         reg_addr = (HC_REG_COMMAND_REG + port*32 +
8169                     COMMAND_REG_ATTN_BITS_CLR);
8170     } else {
8171         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8172     }
8173 
8174     val = ~deasserted;
8175     BLOGD(sc, DBG_INTR,
8176           "about to mask 0x%08x at %s addr 0x%08x\n", val,
8177           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8178     REG_WR(sc, reg_addr, val);
8179 
8180     if (~sc->attn_state & deasserted) {
8181         BLOGE(sc, "IGU error\n");
8182     }
8183 
8184     reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8185                       MISC_REG_AEU_MASK_ATTN_FUNC_0;
8186 
8187     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8188 
8189     aeu_mask = REG_RD(sc, reg_addr);
8190 
8191     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8192           aeu_mask, deasserted);
8193     aeu_mask |= (deasserted & 0x3ff);
8194     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8195 
8196     REG_WR(sc, reg_addr, aeu_mask);
8197     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8198 
8199     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8200     sc->attn_state &= ~deasserted;
8201     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8202 }
8203 
8204 static void
8205 bxe_attn_int(struct bxe_softc *sc)
8206 {
8207     /* read local copy of bits */
8208     uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8209     uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8210     uint32_t attn_state = sc->attn_state;
8211 
8212     /* look for changed bits */
8213     uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8214     uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8215 
8216     BLOGD(sc, DBG_INTR,
8217           "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8218           attn_bits, attn_ack, asserted, deasserted);
8219 
8220     if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8221         BLOGE(sc, "BAD attention state\n");
8222     }
8223 
8224     /* handle bits that were raised */
8225     if (asserted) {
8226         bxe_attn_int_asserted(sc, asserted);
8227     }
8228 
8229     if (deasserted) {
8230         bxe_attn_int_deasserted(sc, deasserted);
8231     }
8232 }
8233 
8234 static uint16_t
8235 bxe_update_dsb_idx(struct bxe_softc *sc)
8236 {
8237     struct host_sp_status_block *def_sb = sc->def_sb;
8238     uint16_t rc = 0;
8239 
8240     mb(); /* status block is written to by the chip */
8241 
8242     if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8243         sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8244         rc |= BXE_DEF_SB_ATT_IDX;
8245     }
8246 
8247     if (sc->def_idx != def_sb->sp_sb.running_index) {
8248         sc->def_idx = def_sb->sp_sb.running_index;
8249         rc |= BXE_DEF_SB_IDX;
8250     }
8251 
8252     mb();
8253 
8254     return (rc);
8255 }
8256 
8257 static inline struct ecore_queue_sp_obj *
8258 bxe_cid_to_q_obj(struct bxe_softc *sc,
8259                  uint32_t         cid)
8260 {
8261     BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8262     return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8263 }
8264 
8265 static void
8266 bxe_handle_mcast_eqe(struct bxe_softc *sc)
8267 {
8268     struct ecore_mcast_ramrod_params rparam;
8269     int rc;
8270 
8271     memset(&rparam, 0, sizeof(rparam));
8272 
8273     rparam.mcast_obj = &sc->mcast_obj;
8274 
8275     BXE_MCAST_LOCK(sc);
8276 
8277     /* clear pending state for the last command */
8278     sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8279 
8280     /* if there are pending mcast commands - send them */
8281     if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8282         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8283         if (rc < 0) {
8284             BLOGD(sc, DBG_SP,
8285                 "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8286         }
8287     }
8288 
8289     BXE_MCAST_UNLOCK(sc);
8290 }
8291 
8292 static void
8293 bxe_handle_classification_eqe(struct bxe_softc      *sc,
8294                               union event_ring_elem *elem)
8295 {
8296     unsigned long ramrod_flags = 0;
8297     int rc = 0;
8298     uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8299     struct ecore_vlan_mac_obj *vlan_mac_obj;
8300 
8301     /* always push next commands out, don't wait here */
8302     bit_set(&ramrod_flags, RAMROD_CONT);
8303 
8304     switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8305     case ECORE_FILTER_MAC_PENDING:
8306         BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8307         vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8308         break;
8309 
8310     case ECORE_FILTER_MCAST_PENDING:
8311         BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8312         /*
8313          * This is only relevant for 57710 where multicast MACs are
8314          * configured as unicast MACs using the same ramrod.
8315          */
8316         bxe_handle_mcast_eqe(sc);
8317         return;
8318 
8319     default:
8320         BLOGE(sc, "Unsupported classification command: %d\n",
8321               elem->message.data.eth_event.echo);
8322         return;
8323     }
8324 
8325     rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8326 
8327     if (rc < 0) {
8328         BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8329     } else if (rc > 0) {
8330         BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8331     }
8332 }
8333 
8334 static void
8335 bxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8336                        union event_ring_elem *elem)
8337 {
8338     bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8339 
8340     /* send rx_mode command again if was requested */
8341     if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8342                                &sc->sp_state)) {
8343         bxe_set_storm_rx_mode(sc);
8344     }
8345 }
8346 
8347 static void
8348 bxe_update_eq_prod(struct bxe_softc *sc,
8349                    uint16_t         prod)
8350 {
8351     storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8352     wmb(); /* keep prod updates ordered */
8353 }
8354 
8355 static void
8356 bxe_eq_int(struct bxe_softc *sc)
8357 {
8358     uint16_t hw_cons, sw_cons, sw_prod;
8359     union event_ring_elem *elem;
8360     uint8_t echo;
8361     uint32_t cid;
8362     uint8_t opcode;
8363     int spqe_cnt = 0;
8364     struct ecore_queue_sp_obj *q_obj;
8365     struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8366     struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8367 
8368     hw_cons = le16toh(*sc->eq_cons_sb);
8369 
8370     /*
8371      * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8372      * when we get to the next-page we need to adjust so the loop
8373      * condition below will be met. The next element is the size of a
8374      * regular element and hence incrementing by 1
8375      */
8376     if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8377         hw_cons++;
8378     }
8379 
8380     /*
8381      * This function may never run in parallel with itself for a
8382      * specific sc and no need for a read memory barrier here.
8383      */
8384     sw_cons = sc->eq_cons;
8385     sw_prod = sc->eq_prod;
8386 
8387     BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8388           hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8389 
8390     for (;
8391          sw_cons != hw_cons;
8392          sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8393 
8394         elem = &sc->eq[EQ_DESC(sw_cons)];
8395 
8396         /* elem CID originates from FW, actually LE */
8397         cid = SW_CID(elem->message.data.cfc_del_event.cid);
8398         opcode = elem->message.opcode;
8399 
8400         /* handle eq element */
8401         switch (opcode) {
8402 
8403         case EVENT_RING_OPCODE_STAT_QUERY:
8404             BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8405                   sc->stats_comp++);
8406             /* nothing to do with stats comp */
8407             goto next_spqe;
8408 
8409         case EVENT_RING_OPCODE_CFC_DEL:
8410             /* handle according to cid range */
8411             /* we may want to verify here that the sc state is HALTING */
8412             BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8413             q_obj = bxe_cid_to_q_obj(sc, cid);
8414             if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8415                 break;
8416             }
8417             goto next_spqe;
8418 
8419         case EVENT_RING_OPCODE_STOP_TRAFFIC:
8420             BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8421             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8422                 break;
8423             }
8424             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8425             goto next_spqe;
8426 
8427         case EVENT_RING_OPCODE_START_TRAFFIC:
8428             BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8429             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8430                 break;
8431             }
8432             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8433             goto next_spqe;
8434 
8435         case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8436             echo = elem->message.data.function_update_event.echo;
8437             if (echo == SWITCH_UPDATE) {
8438                 BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8439                 if (f_obj->complete_cmd(sc, f_obj,
8440                                         ECORE_F_CMD_SWITCH_UPDATE)) {
8441                     break;
8442                 }
8443             }
8444             else {
8445                 BLOGD(sc, DBG_SP,
8446                       "AFEX: ramrod completed FUNCTION_UPDATE\n");
8447             }
8448             goto next_spqe;
8449 
8450         case EVENT_RING_OPCODE_FORWARD_SETUP:
8451             q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8452             if (q_obj->complete_cmd(sc, q_obj,
8453                                     ECORE_Q_CMD_SETUP_TX_ONLY)) {
8454                 break;
8455             }
8456             goto next_spqe;
8457 
8458         case EVENT_RING_OPCODE_FUNCTION_START:
8459             BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8460             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8461                 break;
8462             }
8463             goto next_spqe;
8464 
8465         case EVENT_RING_OPCODE_FUNCTION_STOP:
8466             BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8467             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8468                 break;
8469             }
8470             goto next_spqe;
8471         }
8472 
8473         switch (opcode | sc->state) {
8474         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8475         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8476             cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8477             BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8478             rss_raw->clear_pending(rss_raw);
8479             break;
8480 
8481         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8482         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8483         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8484         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8485         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8486         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8487             BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8488             bxe_handle_classification_eqe(sc, elem);
8489             break;
8490 
8491         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8492         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8493         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8494             BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8495             bxe_handle_mcast_eqe(sc);
8496             break;
8497 
8498         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8499         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8500         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8501             BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8502             bxe_handle_rx_mode_eqe(sc, elem);
8503             break;
8504 
8505         default:
8506             /* unknown event log error and continue */
8507             BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8508                   elem->message.opcode, sc->state);
8509         }
8510 
8511 next_spqe:
8512         spqe_cnt++;
8513     } /* for */
8514 
8515     mb();
8516     atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8517 
8518     sc->eq_cons = sw_cons;
8519     sc->eq_prod = sw_prod;
8520 
8521     /* make sure that above mem writes were issued towards the memory */
8522     wmb();
8523 
8524     /* update producer */
8525     bxe_update_eq_prod(sc, sc->eq_prod);
8526 }
8527 
8528 static void
8529 bxe_handle_sp_tq(void *context,
8530                  int  pending)
8531 {
8532     struct bxe_softc *sc = (struct bxe_softc *)context;
8533     uint16_t status;
8534 
8535     BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8536 
8537     /* what work needs to be performed? */
8538     status = bxe_update_dsb_idx(sc);
8539 
8540     BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8541 
8542     /* HW attentions */
8543     if (status & BXE_DEF_SB_ATT_IDX) {
8544         BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8545         bxe_attn_int(sc);
8546         status &= ~BXE_DEF_SB_ATT_IDX;
8547     }
8548 
8549     /* SP events: STAT_QUERY and others */
8550     if (status & BXE_DEF_SB_IDX) {
8551         /* handle EQ completions */
8552         BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8553         bxe_eq_int(sc);
8554         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8555                    le16toh(sc->def_idx), IGU_INT_NOP, 1);
8556         status &= ~BXE_DEF_SB_IDX;
8557     }
8558 
8559     /* if status is non zero then something went wrong */
8560     if (__predict_false(status)) {
8561         BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8562     }
8563 
8564     /* ack status block only if something was actually handled */
8565     bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8566                le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8567 
8568     /*
8569      * Must be called after the EQ processing (since eq leads to sriov
8570      * ramrod completion flows).
8571      * This flow may have been scheduled by the arrival of a ramrod
8572      * completion, or by the sriov code rescheduling itself.
8573      */
8574     // XXX bxe_iov_sp_task(sc);
8575 
8576 }
8577 
8578 static void
8579 bxe_handle_fp_tq(void *context,
8580                  int  pending)
8581 {
8582     struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8583     struct bxe_softc *sc = fp->sc;
8584     uint8_t more_tx = FALSE;
8585     uint8_t more_rx = FALSE;
8586 
8587     BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8588 
8589     /* XXX
8590      * IFF_DRV_RUNNING state can't be checked here since we process
8591      * slowpath events on a client queue during setup. Instead
8592      * we need to add a "process/continue" flag here that the driver
8593      * can use to tell the task here not to do anything.
8594      */
8595 #if 0
8596     if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
8597         return;
8598     }
8599 #endif
8600 
8601     /* update the fastpath index */
8602     bxe_update_fp_sb_idx(fp);
8603 
8604     /* XXX add loop here if ever support multiple tx CoS */
8605     /* fp->txdata[cos] */
8606     if (bxe_has_tx_work(fp)) {
8607         BXE_FP_TX_LOCK(fp);
8608         more_tx = bxe_txeof(sc, fp);
8609         BXE_FP_TX_UNLOCK(fp);
8610     }
8611 
8612     if (bxe_has_rx_work(fp)) {
8613         more_rx = bxe_rxeof(sc, fp);
8614     }
8615 
8616     if (more_rx /*|| more_tx*/) {
8617         /* still more work to do */
8618         taskqueue_enqueue(fp->tq, &fp->tq_task);
8619         return;
8620     }
8621 
8622     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8623                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8624 }
8625 
8626 static void
8627 bxe_task_fp(struct bxe_fastpath *fp)
8628 {
8629     struct bxe_softc *sc = fp->sc;
8630     uint8_t more_tx = FALSE;
8631     uint8_t more_rx = FALSE;
8632 
8633     BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8634 
8635     /* update the fastpath index */
8636     bxe_update_fp_sb_idx(fp);
8637 
8638     /* XXX add loop here if ever support multiple tx CoS */
8639     /* fp->txdata[cos] */
8640     if (bxe_has_tx_work(fp)) {
8641         BXE_FP_TX_LOCK(fp);
8642         more_tx = bxe_txeof(sc, fp);
8643         BXE_FP_TX_UNLOCK(fp);
8644     }
8645 
8646     if (bxe_has_rx_work(fp)) {
8647         more_rx = bxe_rxeof(sc, fp);
8648     }
8649 
8650     if (more_rx /*|| more_tx*/) {
8651         /* still more work to do, bail out if this ISR and process later */
8652         taskqueue_enqueue(fp->tq, &fp->tq_task);
8653         return;
8654     }
8655 
8656     /*
8657      * Here we write the fastpath index taken before doing any tx or rx work.
8658      * It is very well possible other hw events occurred up to this point and
8659      * they were actually processed accordingly above. Since we're going to
8660      * write an older fastpath index, an interrupt is coming which we might
8661      * not do any work in.
8662      */
8663     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8664                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8665 }
8666 
8667 /*
8668  * Legacy interrupt entry point.
8669  *
8670  * Verifies that the controller generated the interrupt and
8671  * then calls a separate routine to handle the various
8672  * interrupt causes: link, RX, and TX.
8673  */
8674 static void
8675 bxe_intr_legacy(void *xsc)
8676 {
8677     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8678     struct bxe_fastpath *fp;
8679     uint16_t status, mask;
8680     int i;
8681 
8682     BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8683 
8684     /*
8685      * 0 for ustorm, 1 for cstorm
8686      * the bits returned from ack_int() are 0-15
8687      * bit 0 = attention status block
8688      * bit 1 = fast path status block
8689      * a mask of 0x2 or more = tx/rx event
8690      * a mask of 1 = slow path event
8691      */
8692 
8693     status = bxe_ack_int(sc);
8694 
8695     /* the interrupt is not for us */
8696     if (__predict_false(status == 0)) {
8697         BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8698         return;
8699     }
8700 
8701     BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8702 
8703     FOR_EACH_ETH_QUEUE(sc, i) {
8704         fp = &sc->fp[i];
8705         mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8706         if (status & mask) {
8707             /* acknowledge and disable further fastpath interrupts */
8708             bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8709             bxe_task_fp(fp);
8710             status &= ~mask;
8711         }
8712     }
8713 
8714     if (__predict_false(status & 0x1)) {
8715         /* acknowledge and disable further slowpath interrupts */
8716         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8717 
8718         /* schedule slowpath handler */
8719         taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8720 
8721         status &= ~0x1;
8722     }
8723 
8724     if (__predict_false(status)) {
8725         BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8726     }
8727 }
8728 
8729 /* slowpath interrupt entry point */
8730 static void
8731 bxe_intr_sp(void *xsc)
8732 {
8733     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8734 
8735     BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8736 
8737     /* acknowledge and disable further slowpath interrupts */
8738     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8739 
8740     /* schedule slowpath handler */
8741     taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8742 }
8743 
8744 /* fastpath interrupt entry point */
8745 static void
8746 bxe_intr_fp(void *xfp)
8747 {
8748     struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8749     struct bxe_softc *sc = fp->sc;
8750 
8751     BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8752 
8753     BLOGD(sc, DBG_INTR,
8754           "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8755           curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8756 
8757     /* acknowledge and disable further fastpath interrupts */
8758     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8759 
8760     bxe_task_fp(fp);
8761 }
8762 
8763 /* Release all interrupts allocated by the driver. */
8764 static void
8765 bxe_interrupt_free(struct bxe_softc *sc)
8766 {
8767     int i;
8768 
8769     switch (sc->interrupt_mode) {
8770     case INTR_MODE_INTX:
8771         BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8772         if (sc->intr[0].resource != NULL) {
8773             bus_release_resource(sc->dev,
8774                                  SYS_RES_IRQ,
8775                                  sc->intr[0].rid,
8776                                  sc->intr[0].resource);
8777         }
8778         break;
8779     case INTR_MODE_MSI:
8780         for (i = 0; i < sc->intr_count; i++) {
8781             BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
8782             if (sc->intr[i].resource && sc->intr[i].rid) {
8783                 bus_release_resource(sc->dev,
8784                                      SYS_RES_IRQ,
8785                                      sc->intr[i].rid,
8786                                      sc->intr[i].resource);
8787             }
8788         }
8789         pci_release_msi(sc->dev);
8790         break;
8791     case INTR_MODE_MSIX:
8792         for (i = 0; i < sc->intr_count; i++) {
8793             BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8794             if (sc->intr[i].resource && sc->intr[i].rid) {
8795                 bus_release_resource(sc->dev,
8796                                      SYS_RES_IRQ,
8797                                      sc->intr[i].rid,
8798                                      sc->intr[i].resource);
8799             }
8800         }
8801         pci_release_msi(sc->dev);
8802         break;
8803     default:
8804         /* nothing to do as initial allocation failed */
8805         break;
8806     }
8807 }
8808 
8809 /*
8810  * This function determines and allocates the appropriate
8811  * interrupt based on system capabilites and user request.
8812  *
8813  * The user may force a particular interrupt mode, specify
8814  * the number of receive queues, specify the method for
8815  * distribuitng received frames to receive queues, or use
8816  * the default settings which will automatically select the
8817  * best supported combination.  In addition, the OS may or
8818  * may not support certain combinations of these settings.
8819  * This routine attempts to reconcile the settings requested
8820  * by the user with the capabilites available from the system
8821  * to select the optimal combination of features.
8822  *
8823  * Returns:
8824  *   0 = Success, !0 = Failure.
8825  */
8826 static int
8827 bxe_interrupt_alloc(struct bxe_softc *sc)
8828 {
8829     int msix_count = 0;
8830     int msi_count = 0;
8831     int num_requested = 0;
8832     int num_allocated = 0;
8833     int rid, i, j;
8834     int rc;
8835 
8836     /* get the number of available MSI/MSI-X interrupts from the OS */
8837     if (sc->interrupt_mode > 0) {
8838         if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8839             msix_count = pci_msix_count(sc->dev);
8840         }
8841 
8842         if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
8843             msi_count = pci_msi_count(sc->dev);
8844         }
8845 
8846         BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
8847               msi_count, msix_count);
8848     }
8849 
8850     do { /* try allocating MSI-X interrupt resources (at least 2) */
8851         if (sc->interrupt_mode != INTR_MODE_MSIX) {
8852             break;
8853         }
8854 
8855         if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
8856             (msix_count < 2)) {
8857             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8858             break;
8859         }
8860 
8861         /* ask for the necessary number of MSI-X vectors */
8862         num_requested = min((sc->num_queues + 1), msix_count);
8863 
8864         BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
8865 
8866         num_allocated = num_requested;
8867         if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
8868             BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
8869             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8870             break;
8871         }
8872 
8873         if (num_allocated < 2) { /* possible? */
8874             BLOGE(sc, "MSI-X allocation less than 2!\n");
8875             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8876             pci_release_msi(sc->dev);
8877             break;
8878         }
8879 
8880         BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
8881               num_requested, num_allocated);
8882 
8883         /* best effort so use the number of vectors allocated to us */
8884         sc->intr_count = num_allocated;
8885         sc->num_queues = num_allocated - 1;
8886 
8887         rid = 1; /* initial resource identifier */
8888 
8889         /* allocate the MSI-X vectors */
8890         for (i = 0; i < num_allocated; i++) {
8891             sc->intr[i].rid = (rid + i);
8892 
8893             if ((sc->intr[i].resource =
8894                  bus_alloc_resource_any(sc->dev,
8895                                         SYS_RES_IRQ,
8896                                         &sc->intr[i].rid,
8897                                         RF_ACTIVE)) == NULL) {
8898                 BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
8899                       i, (rid + i));
8900 
8901                 for (j = (i - 1); j >= 0; j--) {
8902                     bus_release_resource(sc->dev,
8903                                          SYS_RES_IRQ,
8904                                          sc->intr[j].rid,
8905                                          sc->intr[j].resource);
8906                 }
8907 
8908                 sc->intr_count = 0;
8909                 sc->num_queues = 0;
8910                 sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
8911                 pci_release_msi(sc->dev);
8912                 break;
8913             }
8914 
8915             BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
8916         }
8917     } while (0);
8918 
8919     do { /* try allocating MSI vector resources (at least 2) */
8920         if (sc->interrupt_mode != INTR_MODE_MSI) {
8921             break;
8922         }
8923 
8924         if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
8925             (msi_count < 1)) {
8926             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8927             break;
8928         }
8929 
8930         /* ask for a single MSI vector */
8931         num_requested = 1;
8932 
8933         BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
8934 
8935         num_allocated = num_requested;
8936         if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
8937             BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
8938             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8939             break;
8940         }
8941 
8942         if (num_allocated != 1) { /* possible? */
8943             BLOGE(sc, "MSI allocation is not 1!\n");
8944             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8945             pci_release_msi(sc->dev);
8946             break;
8947         }
8948 
8949         BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
8950               num_requested, num_allocated);
8951 
8952         /* best effort so use the number of vectors allocated to us */
8953         sc->intr_count = num_allocated;
8954         sc->num_queues = num_allocated;
8955 
8956         rid = 1; /* initial resource identifier */
8957 
8958         sc->intr[0].rid = rid;
8959 
8960         if ((sc->intr[0].resource =
8961              bus_alloc_resource_any(sc->dev,
8962                                     SYS_RES_IRQ,
8963                                     &sc->intr[0].rid,
8964                                     RF_ACTIVE)) == NULL) {
8965             BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
8966             sc->intr_count = 0;
8967             sc->num_queues = 0;
8968             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
8969             pci_release_msi(sc->dev);
8970             break;
8971         }
8972 
8973         BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
8974     } while (0);
8975 
8976     do { /* try allocating INTx vector resources */
8977         if (sc->interrupt_mode != INTR_MODE_INTX) {
8978             break;
8979         }
8980 
8981         BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
8982 
8983         /* only one vector for INTx */
8984         sc->intr_count = 1;
8985         sc->num_queues = 1;
8986 
8987         rid = 0; /* initial resource identifier */
8988 
8989         sc->intr[0].rid = rid;
8990 
8991         if ((sc->intr[0].resource =
8992              bus_alloc_resource_any(sc->dev,
8993                                     SYS_RES_IRQ,
8994                                     &sc->intr[0].rid,
8995                                     (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
8996             BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
8997             sc->intr_count = 0;
8998             sc->num_queues = 0;
8999             sc->interrupt_mode = -1; /* Failed! */
9000             break;
9001         }
9002 
9003         BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9004     } while (0);
9005 
9006     if (sc->interrupt_mode == -1) {
9007         BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9008         rc = 1;
9009     } else {
9010         BLOGD(sc, DBG_LOAD,
9011               "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9012               sc->interrupt_mode, sc->num_queues);
9013         rc = 0;
9014     }
9015 
9016     return (rc);
9017 }
9018 
9019 static void
9020 bxe_interrupt_detach(struct bxe_softc *sc)
9021 {
9022     struct bxe_fastpath *fp;
9023     int i;
9024 
9025     /* release interrupt resources */
9026     for (i = 0; i < sc->intr_count; i++) {
9027         if (sc->intr[i].resource && sc->intr[i].tag) {
9028             BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9029             bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9030         }
9031     }
9032 
9033     for (i = 0; i < sc->num_queues; i++) {
9034         fp = &sc->fp[i];
9035         if (fp->tq) {
9036             taskqueue_drain(fp->tq, &fp->tq_task);
9037             taskqueue_free(fp->tq);
9038             fp->tq = NULL;
9039         }
9040     }
9041 
9042 
9043     if (sc->sp_tq) {
9044         taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9045         taskqueue_free(sc->sp_tq);
9046         sc->sp_tq = NULL;
9047     }
9048 }
9049 
9050 /*
9051  * Enables interrupts and attach to the ISR.
9052  *
9053  * When using multiple MSI/MSI-X vectors the first vector
9054  * is used for slowpath operations while all remaining
9055  * vectors are used for fastpath operations.  If only a
9056  * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9057  * ISR must look for both slowpath and fastpath completions.
9058  */
9059 static int
9060 bxe_interrupt_attach(struct bxe_softc *sc)
9061 {
9062     struct bxe_fastpath *fp;
9063     int rc = 0;
9064     int i;
9065 
9066     snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9067              "bxe%d_sp_tq", sc->unit);
9068     TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9069     sc->sp_tq = taskqueue_create_fast(sc->sp_tq_name, M_NOWAIT,
9070                                       taskqueue_thread_enqueue,
9071                                       &sc->sp_tq);
9072     taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9073                             "%s", sc->sp_tq_name);
9074 
9075 
9076     for (i = 0; i < sc->num_queues; i++) {
9077         fp = &sc->fp[i];
9078         snprintf(fp->tq_name, sizeof(fp->tq_name),
9079                  "bxe%d_fp%d_tq", sc->unit, i);
9080         TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9081         fp->tq = taskqueue_create_fast(fp->tq_name, M_NOWAIT,
9082                                        taskqueue_thread_enqueue,
9083                                        &fp->tq);
9084         taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9085                                 "%s", fp->tq_name);
9086     }
9087 
9088     /* setup interrupt handlers */
9089     if (sc->interrupt_mode == INTR_MODE_MSIX) {
9090         BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9091 
9092         /*
9093          * Setup the interrupt handler. Note that we pass the driver instance
9094          * to the interrupt handler for the slowpath.
9095          */
9096         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9097                                  (INTR_TYPE_NET | INTR_MPSAFE),
9098                                  NULL, bxe_intr_sp, sc,
9099                                  &sc->intr[0].tag)) != 0) {
9100             BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9101             goto bxe_interrupt_attach_exit;
9102         }
9103 
9104         bus_describe_intr(sc->dev, sc->intr[0].resource,
9105                           sc->intr[0].tag, "sp");
9106 
9107         /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9108 
9109         /* initialize the fastpath vectors (note the first was used for sp) */
9110         for (i = 0; i < sc->num_queues; i++) {
9111             fp = &sc->fp[i];
9112             BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9113 
9114             /*
9115              * Setup the interrupt handler. Note that we pass the
9116              * fastpath context to the interrupt handler in this
9117              * case.
9118              */
9119             if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9120                                      (INTR_TYPE_NET | INTR_MPSAFE),
9121                                      NULL, bxe_intr_fp, fp,
9122                                      &sc->intr[i + 1].tag)) != 0) {
9123                 BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9124                       (i + 1), rc);
9125                 goto bxe_interrupt_attach_exit;
9126             }
9127 
9128             bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9129                               sc->intr[i + 1].tag, "fp%02d", i);
9130 
9131             /* bind the fastpath instance to a cpu */
9132             if (sc->num_queues > 1) {
9133                 bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9134             }
9135 
9136             fp->state = BXE_FP_STATE_IRQ;
9137         }
9138     } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9139         BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9140 
9141         /*
9142          * Setup the interrupt handler. Note that we pass the
9143          * driver instance to the interrupt handler which
9144          * will handle both the slowpath and fastpath.
9145          */
9146         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9147                                  (INTR_TYPE_NET | INTR_MPSAFE),
9148                                  NULL, bxe_intr_legacy, sc,
9149                                  &sc->intr[0].tag)) != 0) {
9150             BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9151             goto bxe_interrupt_attach_exit;
9152         }
9153 
9154     } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9155         BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9156 
9157         /*
9158          * Setup the interrupt handler. Note that we pass the
9159          * driver instance to the interrupt handler which
9160          * will handle both the slowpath and fastpath.
9161          */
9162         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9163                                  (INTR_TYPE_NET | INTR_MPSAFE),
9164                                  NULL, bxe_intr_legacy, sc,
9165                                  &sc->intr[0].tag)) != 0) {
9166             BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9167             goto bxe_interrupt_attach_exit;
9168         }
9169     }
9170 
9171 bxe_interrupt_attach_exit:
9172 
9173     return (rc);
9174 }
9175 
9176 static int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9177 static int  bxe_init_hw_common(struct bxe_softc *sc);
9178 static int  bxe_init_hw_port(struct bxe_softc *sc);
9179 static int  bxe_init_hw_func(struct bxe_softc *sc);
9180 static void bxe_reset_common(struct bxe_softc *sc);
9181 static void bxe_reset_port(struct bxe_softc *sc);
9182 static void bxe_reset_func(struct bxe_softc *sc);
9183 static int  bxe_gunzip_init(struct bxe_softc *sc);
9184 static void bxe_gunzip_end(struct bxe_softc *sc);
9185 static int  bxe_init_firmware(struct bxe_softc *sc);
9186 static void bxe_release_firmware(struct bxe_softc *sc);
9187 
9188 static struct
9189 ecore_func_sp_drv_ops bxe_func_sp_drv = {
9190     .init_hw_cmn_chip = bxe_init_hw_common_chip,
9191     .init_hw_cmn      = bxe_init_hw_common,
9192     .init_hw_port     = bxe_init_hw_port,
9193     .init_hw_func     = bxe_init_hw_func,
9194 
9195     .reset_hw_cmn     = bxe_reset_common,
9196     .reset_hw_port    = bxe_reset_port,
9197     .reset_hw_func    = bxe_reset_func,
9198 
9199     .gunzip_init      = bxe_gunzip_init,
9200     .gunzip_end       = bxe_gunzip_end,
9201 
9202     .init_fw          = bxe_init_firmware,
9203     .release_fw       = bxe_release_firmware,
9204 };
9205 
9206 static void
9207 bxe_init_func_obj(struct bxe_softc *sc)
9208 {
9209     sc->dmae_ready = 0;
9210 
9211     ecore_init_func_obj(sc,
9212                         &sc->func_obj,
9213                         BXE_SP(sc, func_rdata),
9214                         BXE_SP_MAPPING(sc, func_rdata),
9215                         BXE_SP(sc, func_afex_rdata),
9216                         BXE_SP_MAPPING(sc, func_afex_rdata),
9217                         &bxe_func_sp_drv);
9218 }
9219 
9220 static int
9221 bxe_init_hw(struct bxe_softc *sc,
9222             uint32_t         load_code)
9223 {
9224     struct ecore_func_state_params func_params = { NULL };
9225     int rc;
9226 
9227     /* prepare the parameters for function state transitions */
9228     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9229 
9230     func_params.f_obj = &sc->func_obj;
9231     func_params.cmd = ECORE_F_CMD_HW_INIT;
9232 
9233     func_params.params.hw_init.load_phase = load_code;
9234 
9235     /*
9236      * Via a plethora of function pointers, we will eventually reach
9237      * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9238      */
9239     rc = ecore_func_state_change(sc, &func_params);
9240 
9241     return (rc);
9242 }
9243 
9244 static void
9245 bxe_fill(struct bxe_softc *sc,
9246          uint32_t         addr,
9247          int              fill,
9248          uint32_t         len)
9249 {
9250     uint32_t i;
9251 
9252     if (!(len % 4) && !(addr % 4)) {
9253         for (i = 0; i < len; i += 4) {
9254             REG_WR(sc, (addr + i), fill);
9255         }
9256     } else {
9257         for (i = 0; i < len; i++) {
9258             REG_WR8(sc, (addr + i), fill);
9259         }
9260     }
9261 }
9262 
9263 /* writes FP SP data to FW - data_size in dwords */
9264 static void
9265 bxe_wr_fp_sb_data(struct bxe_softc *sc,
9266                   int              fw_sb_id,
9267                   uint32_t         *sb_data_p,
9268                   uint32_t         data_size)
9269 {
9270     int index;
9271 
9272     for (index = 0; index < data_size; index++) {
9273         REG_WR(sc,
9274                (BAR_CSTRORM_INTMEM +
9275                 CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9276                 (sizeof(uint32_t) * index)),
9277                *(sb_data_p + index));
9278     }
9279 }
9280 
9281 static void
9282 bxe_zero_fp_sb(struct bxe_softc *sc,
9283                int              fw_sb_id)
9284 {
9285     struct hc_status_block_data_e2 sb_data_e2;
9286     struct hc_status_block_data_e1x sb_data_e1x;
9287     uint32_t *sb_data_p;
9288     uint32_t data_size = 0;
9289 
9290     if (!CHIP_IS_E1x(sc)) {
9291         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9292         sb_data_e2.common.state = SB_DISABLED;
9293         sb_data_e2.common.p_func.vf_valid = FALSE;
9294         sb_data_p = (uint32_t *)&sb_data_e2;
9295         data_size = (sizeof(struct hc_status_block_data_e2) /
9296                      sizeof(uint32_t));
9297     } else {
9298         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9299         sb_data_e1x.common.state = SB_DISABLED;
9300         sb_data_e1x.common.p_func.vf_valid = FALSE;
9301         sb_data_p = (uint32_t *)&sb_data_e1x;
9302         data_size = (sizeof(struct hc_status_block_data_e1x) /
9303                      sizeof(uint32_t));
9304     }
9305 
9306     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9307 
9308     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9309              0, CSTORM_STATUS_BLOCK_SIZE);
9310     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9311              0, CSTORM_SYNC_BLOCK_SIZE);
9312 }
9313 
9314 static void
9315 bxe_wr_sp_sb_data(struct bxe_softc               *sc,
9316                   struct hc_sp_status_block_data *sp_sb_data)
9317 {
9318     int i;
9319 
9320     for (i = 0;
9321          i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9322          i++) {
9323         REG_WR(sc,
9324                (BAR_CSTRORM_INTMEM +
9325                 CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9326                 (i * sizeof(uint32_t))),
9327                *((uint32_t *)sp_sb_data + i));
9328     }
9329 }
9330 
9331 static void
9332 bxe_zero_sp_sb(struct bxe_softc *sc)
9333 {
9334     struct hc_sp_status_block_data sp_sb_data;
9335 
9336     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9337 
9338     sp_sb_data.state           = SB_DISABLED;
9339     sp_sb_data.p_func.vf_valid = FALSE;
9340 
9341     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9342 
9343     bxe_fill(sc,
9344              (BAR_CSTRORM_INTMEM +
9345               CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9346               0, CSTORM_SP_STATUS_BLOCK_SIZE);
9347     bxe_fill(sc,
9348              (BAR_CSTRORM_INTMEM +
9349               CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9350               0, CSTORM_SP_SYNC_BLOCK_SIZE);
9351 }
9352 
9353 static void
9354 bxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9355                              int                       igu_sb_id,
9356                              int                       igu_seg_id)
9357 {
9358     hc_sm->igu_sb_id      = igu_sb_id;
9359     hc_sm->igu_seg_id     = igu_seg_id;
9360     hc_sm->timer_value    = 0xFF;
9361     hc_sm->time_to_expire = 0xFFFFFFFF;
9362 }
9363 
9364 static void
9365 bxe_map_sb_state_machines(struct hc_index_data *index_data)
9366 {
9367     /* zero out state machine indices */
9368 
9369     /* rx indices */
9370     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9371 
9372     /* tx indices */
9373     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9374     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9375     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9376     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9377 
9378     /* map indices */
9379 
9380     /* rx indices */
9381     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9382         (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9383 
9384     /* tx indices */
9385     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9386         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9387     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9388         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9389     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9390         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9391     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9392         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9393 }
9394 
9395 static void
9396 bxe_init_sb(struct bxe_softc *sc,
9397             bus_addr_t       busaddr,
9398             int              vfid,
9399             uint8_t          vf_valid,
9400             int              fw_sb_id,
9401             int              igu_sb_id)
9402 {
9403     struct hc_status_block_data_e2  sb_data_e2;
9404     struct hc_status_block_data_e1x sb_data_e1x;
9405     struct hc_status_block_sm       *hc_sm_p;
9406     uint32_t *sb_data_p;
9407     int igu_seg_id;
9408     int data_size;
9409 
9410     if (CHIP_INT_MODE_IS_BC(sc)) {
9411         igu_seg_id = HC_SEG_ACCESS_NORM;
9412     } else {
9413         igu_seg_id = IGU_SEG_ACCESS_NORM;
9414     }
9415 
9416     bxe_zero_fp_sb(sc, fw_sb_id);
9417 
9418     if (!CHIP_IS_E1x(sc)) {
9419         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9420         sb_data_e2.common.state = SB_ENABLED;
9421         sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9422         sb_data_e2.common.p_func.vf_id = vfid;
9423         sb_data_e2.common.p_func.vf_valid = vf_valid;
9424         sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9425         sb_data_e2.common.same_igu_sb_1b = TRUE;
9426         sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9427         sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9428         hc_sm_p = sb_data_e2.common.state_machine;
9429         sb_data_p = (uint32_t *)&sb_data_e2;
9430         data_size = (sizeof(struct hc_status_block_data_e2) /
9431                      sizeof(uint32_t));
9432         bxe_map_sb_state_machines(sb_data_e2.index_data);
9433     } else {
9434         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9435         sb_data_e1x.common.state = SB_ENABLED;
9436         sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9437         sb_data_e1x.common.p_func.vf_id = 0xff;
9438         sb_data_e1x.common.p_func.vf_valid = FALSE;
9439         sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9440         sb_data_e1x.common.same_igu_sb_1b = TRUE;
9441         sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9442         sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9443         hc_sm_p = sb_data_e1x.common.state_machine;
9444         sb_data_p = (uint32_t *)&sb_data_e1x;
9445         data_size = (sizeof(struct hc_status_block_data_e1x) /
9446                      sizeof(uint32_t));
9447         bxe_map_sb_state_machines(sb_data_e1x.index_data);
9448     }
9449 
9450     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9451     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9452 
9453     BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9454 
9455     /* write indices to HW - PCI guarantees endianity of regpairs */
9456     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9457 }
9458 
9459 static inline uint8_t
9460 bxe_fp_qzone_id(struct bxe_fastpath *fp)
9461 {
9462     if (CHIP_IS_E1x(fp->sc)) {
9463         return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9464     } else {
9465         return (fp->cl_id);
9466     }
9467 }
9468 
9469 static inline uint32_t
9470 bxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9471                            struct bxe_fastpath *fp)
9472 {
9473     uint32_t offset = BAR_USTRORM_INTMEM;
9474 
9475     if (!CHIP_IS_E1x(sc)) {
9476         offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9477     } else {
9478         offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9479     }
9480 
9481     return (offset);
9482 }
9483 
9484 static void
9485 bxe_init_eth_fp(struct bxe_softc *sc,
9486                 int              idx)
9487 {
9488     struct bxe_fastpath *fp = &sc->fp[idx];
9489     uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9490     unsigned long q_type = 0;
9491     int cos;
9492 
9493     fp->sc    = sc;
9494     fp->index = idx;
9495 
9496     fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9497     fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9498 
9499     fp->cl_id = (CHIP_IS_E1x(sc)) ?
9500                     (SC_L_ID(sc) + idx) :
9501                     /* want client ID same as IGU SB ID for non-E1 */
9502                     fp->igu_sb_id;
9503     fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9504 
9505     /* setup sb indices */
9506     if (!CHIP_IS_E1x(sc)) {
9507         fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9508         fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9509     } else {
9510         fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9511         fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9512     }
9513 
9514     /* init shortcut */
9515     fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9516 
9517     fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9518 
9519     /*
9520      * XXX If multiple CoS is ever supported then each fastpath structure
9521      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9522      */
9523     for (cos = 0; cos < sc->max_cos; cos++) {
9524         cids[cos] = idx;
9525     }
9526     fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9527 
9528     /* nothing more for a VF to do */
9529     if (IS_VF(sc)) {
9530         return;
9531     }
9532 
9533     bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9534                 fp->fw_sb_id, fp->igu_sb_id);
9535 
9536     bxe_update_fp_sb_idx(fp);
9537 
9538     /* Configure Queue State object */
9539     bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9540     bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9541 
9542     ecore_init_queue_obj(sc,
9543                          &sc->sp_objs[idx].q_obj,
9544                          fp->cl_id,
9545                          cids,
9546                          sc->max_cos,
9547                          SC_FUNC(sc),
9548                          BXE_SP(sc, q_rdata),
9549                          BXE_SP_MAPPING(sc, q_rdata),
9550                          q_type);
9551 
9552     /* configure classification DBs */
9553     ecore_init_mac_obj(sc,
9554                        &sc->sp_objs[idx].mac_obj,
9555                        fp->cl_id,
9556                        idx,
9557                        SC_FUNC(sc),
9558                        BXE_SP(sc, mac_rdata),
9559                        BXE_SP_MAPPING(sc, mac_rdata),
9560                        ECORE_FILTER_MAC_PENDING,
9561                        &sc->sp_state,
9562                        ECORE_OBJ_TYPE_RX_TX,
9563                        &sc->macs_pool);
9564 
9565     BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9566           idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9567 }
9568 
9569 static inline void
9570 bxe_update_rx_prod(struct bxe_softc    *sc,
9571                    struct bxe_fastpath *fp,
9572                    uint16_t            rx_bd_prod,
9573                    uint16_t            rx_cq_prod,
9574                    uint16_t            rx_sge_prod)
9575 {
9576     struct ustorm_eth_rx_producers rx_prods = { 0 };
9577     uint32_t i;
9578 
9579     /* update producers */
9580     rx_prods.bd_prod  = rx_bd_prod;
9581     rx_prods.cqe_prod = rx_cq_prod;
9582     rx_prods.sge_prod = rx_sge_prod;
9583 
9584     /*
9585      * Make sure that the BD and SGE data is updated before updating the
9586      * producers since FW might read the BD/SGE right after the producer
9587      * is updated.
9588      * This is only applicable for weak-ordered memory model archs such
9589      * as IA-64. The following barrier is also mandatory since FW will
9590      * assumes BDs must have buffers.
9591      */
9592     wmb();
9593 
9594     for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9595         REG_WR(sc,
9596                (fp->ustorm_rx_prods_offset + (i * 4)),
9597                ((uint32_t *)&rx_prods)[i]);
9598     }
9599 
9600     wmb(); /* keep prod updates ordered */
9601 
9602     BLOGD(sc, DBG_RX,
9603           "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9604           fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9605 }
9606 
9607 static void
9608 bxe_init_rx_rings(struct bxe_softc *sc)
9609 {
9610     struct bxe_fastpath *fp;
9611     int i;
9612 
9613     for (i = 0; i < sc->num_queues; i++) {
9614         fp = &sc->fp[i];
9615 
9616         fp->rx_bd_cons = 0;
9617 
9618         /*
9619          * Activate the BD ring...
9620          * Warning, this will generate an interrupt (to the TSTORM)
9621          * so this can only be done after the chip is initialized
9622          */
9623         bxe_update_rx_prod(sc, fp,
9624                            fp->rx_bd_prod,
9625                            fp->rx_cq_prod,
9626                            fp->rx_sge_prod);
9627 
9628         if (i != 0) {
9629             continue;
9630         }
9631 
9632         if (CHIP_IS_E1(sc)) {
9633             REG_WR(sc,
9634                    (BAR_USTRORM_INTMEM +
9635                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9636                    U64_LO(fp->rcq_dma.paddr));
9637             REG_WR(sc,
9638                    (BAR_USTRORM_INTMEM +
9639                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9640                    U64_HI(fp->rcq_dma.paddr));
9641         }
9642     }
9643 }
9644 
9645 static void
9646 bxe_init_tx_ring_one(struct bxe_fastpath *fp)
9647 {
9648     SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9649     fp->tx_db.data.zero_fill1 = 0;
9650     fp->tx_db.data.prod = 0;
9651 
9652     fp->tx_pkt_prod = 0;
9653     fp->tx_pkt_cons = 0;
9654     fp->tx_bd_prod = 0;
9655     fp->tx_bd_cons = 0;
9656     fp->eth_q_stats.tx_pkts = 0;
9657 }
9658 
9659 static inline void
9660 bxe_init_tx_rings(struct bxe_softc *sc)
9661 {
9662     int i;
9663 
9664     for (i = 0; i < sc->num_queues; i++) {
9665         bxe_init_tx_ring_one(&sc->fp[i]);
9666     }
9667 }
9668 
9669 static void
9670 bxe_init_def_sb(struct bxe_softc *sc)
9671 {
9672     struct host_sp_status_block *def_sb = sc->def_sb;
9673     bus_addr_t mapping = sc->def_sb_dma.paddr;
9674     int igu_sp_sb_index;
9675     int igu_seg_id;
9676     int port = SC_PORT(sc);
9677     int func = SC_FUNC(sc);
9678     int reg_offset, reg_offset_en5;
9679     uint64_t section;
9680     int index, sindex;
9681     struct hc_sp_status_block_data sp_sb_data;
9682 
9683     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9684 
9685     if (CHIP_INT_MODE_IS_BC(sc)) {
9686         igu_sp_sb_index = DEF_SB_IGU_ID;
9687         igu_seg_id = HC_SEG_ACCESS_DEF;
9688     } else {
9689         igu_sp_sb_index = sc->igu_dsb_id;
9690         igu_seg_id = IGU_SEG_ACCESS_DEF;
9691     }
9692 
9693     /* attentions */
9694     section = ((uint64_t)mapping +
9695                offsetof(struct host_sp_status_block, atten_status_block));
9696     def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9697     sc->attn_state = 0;
9698 
9699     reg_offset = (port) ?
9700                      MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9701                      MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9702     reg_offset_en5 = (port) ?
9703                          MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9704                          MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9705 
9706     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9707         /* take care of sig[0]..sig[4] */
9708         for (sindex = 0; sindex < 4; sindex++) {
9709             sc->attn_group[index].sig[sindex] =
9710                 REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9711         }
9712 
9713         if (!CHIP_IS_E1x(sc)) {
9714             /*
9715              * enable5 is separate from the rest of the registers,
9716              * and the address skip is 4 and not 16 between the
9717              * different groups
9718              */
9719             sc->attn_group[index].sig[4] =
9720                 REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9721         } else {
9722             sc->attn_group[index].sig[4] = 0;
9723         }
9724     }
9725 
9726     if (sc->devinfo.int_block == INT_BLOCK_HC) {
9727         reg_offset = (port) ?
9728                          HC_REG_ATTN_MSG1_ADDR_L :
9729                          HC_REG_ATTN_MSG0_ADDR_L;
9730         REG_WR(sc, reg_offset, U64_LO(section));
9731         REG_WR(sc, (reg_offset + 4), U64_HI(section));
9732     } else if (!CHIP_IS_E1x(sc)) {
9733         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9734         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9735     }
9736 
9737     section = ((uint64_t)mapping +
9738                offsetof(struct host_sp_status_block, sp_sb));
9739 
9740     bxe_zero_sp_sb(sc);
9741 
9742     /* PCI guarantees endianity of regpair */
9743     sp_sb_data.state           = SB_ENABLED;
9744     sp_sb_data.host_sb_addr.lo = U64_LO(section);
9745     sp_sb_data.host_sb_addr.hi = U64_HI(section);
9746     sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9747     sp_sb_data.igu_seg_id      = igu_seg_id;
9748     sp_sb_data.p_func.pf_id    = func;
9749     sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9750     sp_sb_data.p_func.vf_id    = 0xff;
9751 
9752     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9753 
9754     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9755 }
9756 
9757 static void
9758 bxe_init_sp_ring(struct bxe_softc *sc)
9759 {
9760     atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9761     sc->spq_prod_idx = 0;
9762     sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9763     sc->spq_prod_bd = sc->spq;
9764     sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9765 }
9766 
9767 static void
9768 bxe_init_eq_ring(struct bxe_softc *sc)
9769 {
9770     union event_ring_elem *elem;
9771     int i;
9772 
9773     for (i = 1; i <= NUM_EQ_PAGES; i++) {
9774         elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9775 
9776         elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9777                                                  BCM_PAGE_SIZE *
9778                                                  (i % NUM_EQ_PAGES)));
9779         elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9780                                                  BCM_PAGE_SIZE *
9781                                                  (i % NUM_EQ_PAGES)));
9782     }
9783 
9784     sc->eq_cons    = 0;
9785     sc->eq_prod    = NUM_EQ_DESC;
9786     sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9787 
9788     atomic_store_rel_long(&sc->eq_spq_left,
9789                           (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9790                                NUM_EQ_DESC) - 1));
9791 }
9792 
9793 static void
9794 bxe_init_internal_common(struct bxe_softc *sc)
9795 {
9796     int i;
9797 
9798     /*
9799      * Zero this manually as its initialization is currently missing
9800      * in the initTool.
9801      */
9802     for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9803         REG_WR(sc,
9804                (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9805                0);
9806     }
9807 
9808     if (!CHIP_IS_E1x(sc)) {
9809         REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9810                 CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9811     }
9812 }
9813 
9814 static void
9815 bxe_init_internal(struct bxe_softc *sc,
9816                   uint32_t         load_code)
9817 {
9818     switch (load_code) {
9819     case FW_MSG_CODE_DRV_LOAD_COMMON:
9820     case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9821         bxe_init_internal_common(sc);
9822         /* no break */
9823 
9824     case FW_MSG_CODE_DRV_LOAD_PORT:
9825         /* nothing to do */
9826         /* no break */
9827 
9828     case FW_MSG_CODE_DRV_LOAD_FUNCTION:
9829         /* internal memory per function is initialized inside bxe_pf_init */
9830         break;
9831 
9832     default:
9833         BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
9834         break;
9835     }
9836 }
9837 
9838 static void
9839 storm_memset_func_cfg(struct bxe_softc                         *sc,
9840                       struct tstorm_eth_function_common_config *tcfg,
9841                       uint16_t                                  abs_fid)
9842 {
9843     uint32_t addr;
9844     size_t size;
9845 
9846     addr = (BAR_TSTRORM_INTMEM +
9847             TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
9848     size = sizeof(struct tstorm_eth_function_common_config);
9849     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
9850 }
9851 
9852 static void
9853 bxe_func_init(struct bxe_softc            *sc,
9854               struct bxe_func_init_params *p)
9855 {
9856     struct tstorm_eth_function_common_config tcfg = { 0 };
9857 
9858     if (CHIP_IS_E1x(sc)) {
9859         storm_memset_func_cfg(sc, &tcfg, p->func_id);
9860     }
9861 
9862     /* Enable the function in the FW */
9863     storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
9864     storm_memset_func_en(sc, p->func_id, 1);
9865 
9866     /* spq */
9867     if (p->func_flgs & FUNC_FLG_SPQ) {
9868         storm_memset_spq_addr(sc, p->spq_map, p->func_id);
9869         REG_WR(sc,
9870                (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
9871                p->spq_prod);
9872     }
9873 }
9874 
9875 /*
9876  * Calculates the sum of vn_min_rates.
9877  * It's needed for further normalizing of the min_rates.
9878  * Returns:
9879  *   sum of vn_min_rates.
9880  *     or
9881  *   0 - if all the min_rates are 0.
9882  * In the later case fainess algorithm should be deactivated.
9883  * If all min rates are not zero then those that are zeroes will be set to 1.
9884  */
9885 static void
9886 bxe_calc_vn_min(struct bxe_softc       *sc,
9887                 struct cmng_init_input *input)
9888 {
9889     uint32_t vn_cfg;
9890     uint32_t vn_min_rate;
9891     int all_zero = 1;
9892     int vn;
9893 
9894     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
9895         vn_cfg = sc->devinfo.mf_info.mf_config[vn];
9896         vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
9897                         FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
9898 
9899         if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
9900             /* skip hidden VNs */
9901             vn_min_rate = 0;
9902         } else if (!vn_min_rate) {
9903             /* If min rate is zero - set it to 100 */
9904             vn_min_rate = DEF_MIN_RATE;
9905         } else {
9906             all_zero = 0;
9907         }
9908 
9909         input->vnic_min_rate[vn] = vn_min_rate;
9910     }
9911 
9912     /* if ETS or all min rates are zeros - disable fairness */
9913     if (BXE_IS_ETS_ENABLED(sc)) {
9914         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
9915         BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
9916     } else if (all_zero) {
9917         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
9918         BLOGD(sc, DBG_LOAD,
9919               "Fariness disabled (all MIN values are zeroes)\n");
9920     } else {
9921         input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
9922     }
9923 }
9924 
9925 static inline uint16_t
9926 bxe_extract_max_cfg(struct bxe_softc *sc,
9927                     uint32_t         mf_cfg)
9928 {
9929     uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
9930                         FUNC_MF_CFG_MAX_BW_SHIFT);
9931 
9932     if (!max_cfg) {
9933         BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
9934         max_cfg = 100;
9935     }
9936 
9937     return (max_cfg);
9938 }
9939 
9940 static void
9941 bxe_calc_vn_max(struct bxe_softc       *sc,
9942                 int                    vn,
9943                 struct cmng_init_input *input)
9944 {
9945     uint16_t vn_max_rate;
9946     uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
9947     uint32_t max_cfg;
9948 
9949     if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
9950         vn_max_rate = 0;
9951     } else {
9952         max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
9953 
9954         if (IS_MF_SI(sc)) {
9955             /* max_cfg in percents of linkspeed */
9956             vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
9957         } else { /* SD modes */
9958             /* max_cfg is absolute in 100Mb units */
9959             vn_max_rate = (max_cfg * 100);
9960         }
9961     }
9962 
9963     BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
9964 
9965     input->vnic_max_rate[vn] = vn_max_rate;
9966 }
9967 
9968 static void
9969 bxe_cmng_fns_init(struct bxe_softc *sc,
9970                   uint8_t          read_cfg,
9971                   uint8_t          cmng_type)
9972 {
9973     struct cmng_init_input input;
9974     int vn;
9975 
9976     memset(&input, 0, sizeof(struct cmng_init_input));
9977 
9978     input.port_rate = sc->link_vars.line_speed;
9979 
9980     if (cmng_type == CMNG_FNS_MINMAX) {
9981         /* read mf conf from shmem */
9982         if (read_cfg) {
9983             bxe_read_mf_cfg(sc);
9984         }
9985 
9986         /* get VN min rate and enable fairness if not 0 */
9987         bxe_calc_vn_min(sc, &input);
9988 
9989         /* get VN max rate */
9990         if (sc->port.pmf) {
9991             for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
9992                 bxe_calc_vn_max(sc, vn, &input);
9993             }
9994         }
9995 
9996         /* always enable rate shaping and fairness */
9997         input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
9998 
9999         ecore_init_cmng(&input, &sc->cmng);
10000         return;
10001     }
10002 
10003     /* rate shaping and fairness are disabled */
10004     BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10005 }
10006 
10007 static int
10008 bxe_get_cmng_fns_mode(struct bxe_softc *sc)
10009 {
10010     if (CHIP_REV_IS_SLOW(sc)) {
10011         return (CMNG_FNS_NONE);
10012     }
10013 
10014     if (IS_MF(sc)) {
10015         return (CMNG_FNS_MINMAX);
10016     }
10017 
10018     return (CMNG_FNS_NONE);
10019 }
10020 
10021 static void
10022 storm_memset_cmng(struct bxe_softc *sc,
10023                   struct cmng_init *cmng,
10024                   uint8_t          port)
10025 {
10026     int vn;
10027     int func;
10028     uint32_t addr;
10029     size_t size;
10030 
10031     addr = (BAR_XSTRORM_INTMEM +
10032             XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10033     size = sizeof(struct cmng_struct_per_port);
10034     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10035 
10036     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10037         func = func_by_vn(sc, vn);
10038 
10039         addr = (BAR_XSTRORM_INTMEM +
10040                 XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10041         size = sizeof(struct rate_shaping_vars_per_vn);
10042         ecore_storm_memset_struct(sc, addr, size,
10043                                   (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10044 
10045         addr = (BAR_XSTRORM_INTMEM +
10046                 XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10047         size = sizeof(struct fairness_vars_per_vn);
10048         ecore_storm_memset_struct(sc, addr, size,
10049                                   (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10050     }
10051 }
10052 
10053 static void
10054 bxe_pf_init(struct bxe_softc *sc)
10055 {
10056     struct bxe_func_init_params func_init = { 0 };
10057     struct event_ring_data eq_data = { { 0 } };
10058     uint16_t flags;
10059 
10060     if (!CHIP_IS_E1x(sc)) {
10061         /* reset IGU PF statistics: MSIX + ATTN */
10062         /* PF */
10063         REG_WR(sc,
10064                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10065                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10066                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10067                0);
10068         /* ATTN */
10069         REG_WR(sc,
10070                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10071                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10072                 (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10073                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10074                0);
10075     }
10076 
10077     /* function setup flags */
10078     flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10079 
10080     /*
10081      * This flag is relevant for E1x only.
10082      * E2 doesn't have a TPA configuration in a function level.
10083      */
10084     flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10085 
10086     func_init.func_flgs = flags;
10087     func_init.pf_id     = SC_FUNC(sc);
10088     func_init.func_id   = SC_FUNC(sc);
10089     func_init.spq_map   = sc->spq_dma.paddr;
10090     func_init.spq_prod  = sc->spq_prod_idx;
10091 
10092     bxe_func_init(sc, &func_init);
10093 
10094     memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10095 
10096     /*
10097      * Congestion management values depend on the link rate.
10098      * There is no active link so initial link rate is set to 10Gbps.
10099      * When the link comes up the congestion management values are
10100      * re-calculated according to the actual link rate.
10101      */
10102     sc->link_vars.line_speed = SPEED_10000;
10103     bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10104 
10105     /* Only the PMF sets the HW */
10106     if (sc->port.pmf) {
10107         storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10108     }
10109 
10110     /* init Event Queue - PCI bus guarantees correct endainity */
10111     eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10112     eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10113     eq_data.producer     = sc->eq_prod;
10114     eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10115     eq_data.sb_id        = DEF_SB_ID;
10116     storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10117 }
10118 
10119 static void
10120 bxe_hc_int_enable(struct bxe_softc *sc)
10121 {
10122     int port = SC_PORT(sc);
10123     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10124     uint32_t val = REG_RD(sc, addr);
10125     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10126     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10127                            (sc->intr_count == 1)) ? TRUE : FALSE;
10128     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10129 
10130     if (msix) {
10131         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10132                  HC_CONFIG_0_REG_INT_LINE_EN_0);
10133         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10134                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10135         if (single_msix) {
10136             val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10137         }
10138     } else if (msi) {
10139         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10140         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10141                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10142                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10143     } else {
10144         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10145                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10146                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10147                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10148 
10149         if (!CHIP_IS_E1(sc)) {
10150             BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10151                   val, port, addr);
10152 
10153             REG_WR(sc, addr, val);
10154 
10155             val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10156         }
10157     }
10158 
10159     if (CHIP_IS_E1(sc)) {
10160         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10161     }
10162 
10163     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10164           val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10165 
10166     REG_WR(sc, addr, val);
10167 
10168     /* ensure that HC_CONFIG is written before leading/trailing edge config */
10169     mb();
10170 
10171     if (!CHIP_IS_E1(sc)) {
10172         /* init leading/trailing edge */
10173         if (IS_MF(sc)) {
10174             val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10175             if (sc->port.pmf) {
10176                 /* enable nig and gpio3 attention */
10177                 val |= 0x1100;
10178             }
10179         } else {
10180             val = 0xffff;
10181         }
10182 
10183         REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10184         REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10185     }
10186 
10187     /* make sure that interrupts are indeed enabled from here on */
10188     mb();
10189 }
10190 
10191 static void
10192 bxe_igu_int_enable(struct bxe_softc *sc)
10193 {
10194     uint32_t val;
10195     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10196     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10197                            (sc->intr_count == 1)) ? TRUE : FALSE;
10198     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10199 
10200     val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10201 
10202     if (msix) {
10203         val &= ~(IGU_PF_CONF_INT_LINE_EN |
10204                  IGU_PF_CONF_SINGLE_ISR_EN);
10205         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10206                 IGU_PF_CONF_ATTN_BIT_EN);
10207         if (single_msix) {
10208             val |= IGU_PF_CONF_SINGLE_ISR_EN;
10209         }
10210     } else if (msi) {
10211         val &= ~IGU_PF_CONF_INT_LINE_EN;
10212         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10213                 IGU_PF_CONF_ATTN_BIT_EN |
10214                 IGU_PF_CONF_SINGLE_ISR_EN);
10215     } else {
10216         val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10217         val |= (IGU_PF_CONF_INT_LINE_EN |
10218                 IGU_PF_CONF_ATTN_BIT_EN |
10219                 IGU_PF_CONF_SINGLE_ISR_EN);
10220     }
10221 
10222     /* clean previous status - need to configure igu prior to ack*/
10223     if ((!msix) || single_msix) {
10224         REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10225         bxe_ack_int(sc);
10226     }
10227 
10228     val |= IGU_PF_CONF_FUNC_EN;
10229 
10230     BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10231           val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10232 
10233     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10234 
10235     mb();
10236 
10237     /* init leading/trailing edge */
10238     if (IS_MF(sc)) {
10239         val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10240         if (sc->port.pmf) {
10241             /* enable nig and gpio3 attention */
10242             val |= 0x1100;
10243         }
10244     } else {
10245         val = 0xffff;
10246     }
10247 
10248     REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10249     REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10250 
10251     /* make sure that interrupts are indeed enabled from here on */
10252     mb();
10253 }
10254 
10255 static void
10256 bxe_int_enable(struct bxe_softc *sc)
10257 {
10258     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10259         bxe_hc_int_enable(sc);
10260     } else {
10261         bxe_igu_int_enable(sc);
10262     }
10263 }
10264 
10265 static void
10266 bxe_hc_int_disable(struct bxe_softc *sc)
10267 {
10268     int port = SC_PORT(sc);
10269     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10270     uint32_t val = REG_RD(sc, addr);
10271 
10272     /*
10273      * In E1 we must use only PCI configuration space to disable MSI/MSIX
10274      * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10275      * block
10276      */
10277     if (CHIP_IS_E1(sc)) {
10278         /*
10279          * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10280          * to prevent from HC sending interrupts after we exit the function
10281          */
10282         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10283 
10284         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10285                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10286                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10287     } else {
10288         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10289                  HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10290                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10291                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10292     }
10293 
10294     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10295 
10296     /* flush all outstanding writes */
10297     mb();
10298 
10299     REG_WR(sc, addr, val);
10300     if (REG_RD(sc, addr) != val) {
10301         BLOGE(sc, "proper val not read from HC IGU!\n");
10302     }
10303 }
10304 
10305 static void
10306 bxe_igu_int_disable(struct bxe_softc *sc)
10307 {
10308     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10309 
10310     val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10311              IGU_PF_CONF_INT_LINE_EN |
10312              IGU_PF_CONF_ATTN_BIT_EN);
10313 
10314     BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10315 
10316     /* flush all outstanding writes */
10317     mb();
10318 
10319     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10320     if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10321         BLOGE(sc, "proper val not read from IGU!\n");
10322     }
10323 }
10324 
10325 static void
10326 bxe_int_disable(struct bxe_softc *sc)
10327 {
10328     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10329         bxe_hc_int_disable(sc);
10330     } else {
10331         bxe_igu_int_disable(sc);
10332     }
10333 }
10334 
10335 static void
10336 bxe_nic_init(struct bxe_softc *sc,
10337              int              load_code)
10338 {
10339     int i;
10340 
10341     for (i = 0; i < sc->num_queues; i++) {
10342         bxe_init_eth_fp(sc, i);
10343     }
10344 
10345     rmb(); /* ensure status block indices were read */
10346 
10347     bxe_init_rx_rings(sc);
10348     bxe_init_tx_rings(sc);
10349 
10350     if (IS_VF(sc)) {
10351         return;
10352     }
10353 
10354     /* initialize MOD_ABS interrupts */
10355     elink_init_mod_abs_int(sc, &sc->link_vars,
10356                            sc->devinfo.chip_id,
10357                            sc->devinfo.shmem_base,
10358                            sc->devinfo.shmem2_base,
10359                            SC_PORT(sc));
10360 
10361     bxe_init_def_sb(sc);
10362     bxe_update_dsb_idx(sc);
10363     bxe_init_sp_ring(sc);
10364     bxe_init_eq_ring(sc);
10365     bxe_init_internal(sc, load_code);
10366     bxe_pf_init(sc);
10367     bxe_stats_init(sc);
10368 
10369     /* flush all before enabling interrupts */
10370     mb();
10371 
10372     bxe_int_enable(sc);
10373 
10374     /* check for SPIO5 */
10375     bxe_attn_int_deasserted0(sc,
10376                              REG_RD(sc,
10377                                     (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10378                                      SC_PORT(sc)*4)) &
10379                              AEU_INPUTS_ATTN_BITS_SPIO5);
10380 }
10381 
10382 static inline void
10383 bxe_init_objs(struct bxe_softc *sc)
10384 {
10385     /* mcast rules must be added to tx if tx switching is enabled */
10386     ecore_obj_type o_type =
10387         (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10388                                          ECORE_OBJ_TYPE_RX;
10389 
10390     /* RX_MODE controlling object */
10391     ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10392 
10393     /* multicast configuration controlling object */
10394     ecore_init_mcast_obj(sc,
10395                          &sc->mcast_obj,
10396                          sc->fp[0].cl_id,
10397                          sc->fp[0].index,
10398                          SC_FUNC(sc),
10399                          SC_FUNC(sc),
10400                          BXE_SP(sc, mcast_rdata),
10401                          BXE_SP_MAPPING(sc, mcast_rdata),
10402                          ECORE_FILTER_MCAST_PENDING,
10403                          &sc->sp_state,
10404                          o_type);
10405 
10406     /* Setup CAM credit pools */
10407     ecore_init_mac_credit_pool(sc,
10408                                &sc->macs_pool,
10409                                SC_FUNC(sc),
10410                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10411                                                  VNICS_PER_PATH(sc));
10412 
10413     ecore_init_vlan_credit_pool(sc,
10414                                 &sc->vlans_pool,
10415                                 SC_ABS_FUNC(sc) >> 1,
10416                                 CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10417                                                   VNICS_PER_PATH(sc));
10418 
10419     /* RSS configuration object */
10420     ecore_init_rss_config_obj(sc,
10421                               &sc->rss_conf_obj,
10422                               sc->fp[0].cl_id,
10423                               sc->fp[0].index,
10424                               SC_FUNC(sc),
10425                               SC_FUNC(sc),
10426                               BXE_SP(sc, rss_rdata),
10427                               BXE_SP_MAPPING(sc, rss_rdata),
10428                               ECORE_FILTER_RSS_CONF_PENDING,
10429                               &sc->sp_state, ECORE_OBJ_TYPE_RX);
10430 }
10431 
10432 /*
10433  * Initialize the function. This must be called before sending CLIENT_SETUP
10434  * for the first client.
10435  */
10436 static inline int
10437 bxe_func_start(struct bxe_softc *sc)
10438 {
10439     struct ecore_func_state_params func_params = { NULL };
10440     struct ecore_func_start_params *start_params = &func_params.params.start;
10441 
10442     /* Prepare parameters for function state transitions */
10443     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10444 
10445     func_params.f_obj = &sc->func_obj;
10446     func_params.cmd = ECORE_F_CMD_START;
10447 
10448     /* Function parameters */
10449     start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10450     start_params->sd_vlan_tag = OVLAN(sc);
10451 
10452     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10453         start_params->network_cos_mode = STATIC_COS;
10454     } else { /* CHIP_IS_E1X */
10455         start_params->network_cos_mode = FW_WRR;
10456     }
10457 
10458     //start_params->gre_tunnel_mode = 0;
10459     //start_params->gre_tunnel_rss  = 0;
10460 
10461     return (ecore_func_state_change(sc, &func_params));
10462 }
10463 
10464 static int
10465 bxe_set_power_state(struct bxe_softc *sc,
10466                     uint8_t          state)
10467 {
10468     uint16_t pmcsr;
10469 
10470     /* If there is no power capability, silently succeed */
10471     if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10472         BLOGW(sc, "No power capability\n");
10473         return (0);
10474     }
10475 
10476     pmcsr = pci_read_config(sc->dev,
10477                             (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10478                             2);
10479 
10480     switch (state) {
10481     case PCI_PM_D0:
10482         pci_write_config(sc->dev,
10483                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10484                          ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10485 
10486         if (pmcsr & PCIM_PSTAT_DMASK) {
10487             /* delay required during transition out of D3hot */
10488             DELAY(20000);
10489         }
10490 
10491         break;
10492 
10493     case PCI_PM_D3hot:
10494         /* XXX if there are other clients above don't shut down the power */
10495 
10496         /* don't shut down the power for emulation and FPGA */
10497         if (CHIP_REV_IS_SLOW(sc)) {
10498             return (0);
10499         }
10500 
10501         pmcsr &= ~PCIM_PSTAT_DMASK;
10502         pmcsr |= PCIM_PSTAT_D3;
10503 
10504         if (sc->wol) {
10505             pmcsr |= PCIM_PSTAT_PMEENABLE;
10506         }
10507 
10508         pci_write_config(sc->dev,
10509                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10510                          pmcsr, 4);
10511 
10512         /*
10513          * No more memory access after this point until device is brought back
10514          * to D0 state.
10515          */
10516         break;
10517 
10518     default:
10519         BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10520             state, pmcsr);
10521         return (-1);
10522     }
10523 
10524     return (0);
10525 }
10526 
10527 
10528 /* return true if succeeded to acquire the lock */
10529 static uint8_t
10530 bxe_trylock_hw_lock(struct bxe_softc *sc,
10531                     uint32_t         resource)
10532 {
10533     uint32_t lock_status;
10534     uint32_t resource_bit = (1 << resource);
10535     int func = SC_FUNC(sc);
10536     uint32_t hw_lock_control_reg;
10537 
10538     BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10539 
10540     /* Validating that the resource is within range */
10541     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10542         BLOGD(sc, DBG_LOAD,
10543               "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10544               resource, HW_LOCK_MAX_RESOURCE_VALUE);
10545         return (FALSE);
10546     }
10547 
10548     if (func <= 5) {
10549         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10550     } else {
10551         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10552     }
10553 
10554     /* try to acquire the lock */
10555     REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10556     lock_status = REG_RD(sc, hw_lock_control_reg);
10557     if (lock_status & resource_bit) {
10558         return (TRUE);
10559     }
10560 
10561     BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10562         "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10563         lock_status, resource_bit);
10564 
10565     return (FALSE);
10566 }
10567 
10568 /*
10569  * Get the recovery leader resource id according to the engine this function
10570  * belongs to. Currently only only 2 engines is supported.
10571  */
10572 static int
10573 bxe_get_leader_lock_resource(struct bxe_softc *sc)
10574 {
10575     if (SC_PATH(sc)) {
10576         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10577     } else {
10578         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10579     }
10580 }
10581 
10582 /* try to acquire a leader lock for current engine */
10583 static uint8_t
10584 bxe_trylock_leader_lock(struct bxe_softc *sc)
10585 {
10586     return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10587 }
10588 
10589 static int
10590 bxe_release_leader_lock(struct bxe_softc *sc)
10591 {
10592     return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10593 }
10594 
10595 /* close gates #2, #3 and #4 */
10596 static void
10597 bxe_set_234_gates(struct bxe_softc *sc,
10598                   uint8_t          close)
10599 {
10600     uint32_t val;
10601 
10602     /* gates #2 and #4a are closed/opened for "not E1" only */
10603     if (!CHIP_IS_E1(sc)) {
10604         /* #4 */
10605         REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10606         /* #2 */
10607         REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10608     }
10609 
10610     /* #3 */
10611     if (CHIP_IS_E1x(sc)) {
10612         /* prevent interrupts from HC on both ports */
10613         val = REG_RD(sc, HC_REG_CONFIG_1);
10614         REG_WR(sc, HC_REG_CONFIG_1,
10615                (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10616                (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10617 
10618         val = REG_RD(sc, HC_REG_CONFIG_0);
10619         REG_WR(sc, HC_REG_CONFIG_0,
10620                (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10621                (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10622     } else {
10623         /* Prevent incoming interrupts in IGU */
10624         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10625 
10626         REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10627                (!close) ?
10628                (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10629                (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10630     }
10631 
10632     BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10633           close ? "closing" : "opening");
10634 
10635     wmb();
10636 }
10637 
10638 /* poll for pending writes bit, it should get cleared in no more than 1s */
10639 static int
10640 bxe_er_poll_igu_vq(struct bxe_softc *sc)
10641 {
10642     uint32_t cnt = 1000;
10643     uint32_t pend_bits = 0;
10644 
10645     do {
10646         pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10647 
10648         if (pend_bits == 0) {
10649             break;
10650         }
10651 
10652         DELAY(1000);
10653     } while (--cnt > 0);
10654 
10655     if (cnt == 0) {
10656         BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10657         return (-1);
10658     }
10659 
10660     return (0);
10661 }
10662 
10663 #define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10664 
10665 static void
10666 bxe_clp_reset_prep(struct bxe_softc *sc,
10667                    uint32_t         *magic_val)
10668 {
10669     /* Do some magic... */
10670     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10671     *magic_val = val & SHARED_MF_CLP_MAGIC;
10672     MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10673 }
10674 
10675 /* restore the value of the 'magic' bit */
10676 static void
10677 bxe_clp_reset_done(struct bxe_softc *sc,
10678                    uint32_t         magic_val)
10679 {
10680     /* Restore the 'magic' bit value... */
10681     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10682     MFCFG_WR(sc, shared_mf_config.clp_mb,
10683               (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10684 }
10685 
10686 /* prepare for MCP reset, takes care of CLP configurations */
10687 static void
10688 bxe_reset_mcp_prep(struct bxe_softc *sc,
10689                    uint32_t         *magic_val)
10690 {
10691     uint32_t shmem;
10692     uint32_t validity_offset;
10693 
10694     /* set `magic' bit in order to save MF config */
10695     if (!CHIP_IS_E1(sc)) {
10696         bxe_clp_reset_prep(sc, magic_val);
10697     }
10698 
10699     /* get shmem offset */
10700     shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10701     validity_offset =
10702         offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10703 
10704     /* Clear validity map flags */
10705     if (shmem > 0) {
10706         REG_WR(sc, shmem + validity_offset, 0);
10707     }
10708 }
10709 
10710 #define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10711 #define MCP_ONE_TIMEOUT  100    /* 100 ms */
10712 
10713 static void
10714 bxe_mcp_wait_one(struct bxe_softc *sc)
10715 {
10716     /* special handling for emulation and FPGA (10 times longer) */
10717     if (CHIP_REV_IS_SLOW(sc)) {
10718         DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10719     } else {
10720         DELAY((MCP_ONE_TIMEOUT) * 1000);
10721     }
10722 }
10723 
10724 /* initialize shmem_base and waits for validity signature to appear */
10725 static int
10726 bxe_init_shmem(struct bxe_softc *sc)
10727 {
10728     int cnt = 0;
10729     uint32_t val = 0;
10730 
10731     do {
10732         sc->devinfo.shmem_base     =
10733         sc->link_params.shmem_base =
10734             REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10735 
10736         if (sc->devinfo.shmem_base) {
10737             val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10738             if (val & SHR_MEM_VALIDITY_MB)
10739                 return (0);
10740         }
10741 
10742         bxe_mcp_wait_one(sc);
10743 
10744     } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10745 
10746     BLOGE(sc, "BAD MCP validity signature\n");
10747 
10748     return (-1);
10749 }
10750 
10751 static int
10752 bxe_reset_mcp_comp(struct bxe_softc *sc,
10753                    uint32_t         magic_val)
10754 {
10755     int rc = bxe_init_shmem(sc);
10756 
10757     /* Restore the `magic' bit value */
10758     if (!CHIP_IS_E1(sc)) {
10759         bxe_clp_reset_done(sc, magic_val);
10760     }
10761 
10762     return (rc);
10763 }
10764 
10765 static void
10766 bxe_pxp_prep(struct bxe_softc *sc)
10767 {
10768     if (!CHIP_IS_E1(sc)) {
10769         REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10770         REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10771         wmb();
10772     }
10773 }
10774 
10775 /*
10776  * Reset the whole chip except for:
10777  *      - PCIE core
10778  *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10779  *      - IGU
10780  *      - MISC (including AEU)
10781  *      - GRC
10782  *      - RBCN, RBCP
10783  */
10784 static void
10785 bxe_process_kill_chip_reset(struct bxe_softc *sc,
10786                             uint8_t          global)
10787 {
10788     uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10789     uint32_t global_bits2, stay_reset2;
10790 
10791     /*
10792      * Bits that have to be set in reset_mask2 if we want to reset 'global'
10793      * (per chip) blocks.
10794      */
10795     global_bits2 =
10796         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10797         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10798 
10799     /*
10800      * Don't reset the following blocks.
10801      * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10802      *            reset, as in 4 port device they might still be owned
10803      *            by the MCP (there is only one leader per path).
10804      */
10805     not_reset_mask1 =
10806         MISC_REGISTERS_RESET_REG_1_RST_HC |
10807         MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10808         MISC_REGISTERS_RESET_REG_1_RST_PXP;
10809 
10810     not_reset_mask2 =
10811         MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10812         MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10813         MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10814         MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10815         MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10816         MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10817         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10818         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10819         MISC_REGISTERS_RESET_REG_2_RST_ATC |
10820         MISC_REGISTERS_RESET_REG_2_PGLC |
10821         MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10822         MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10823         MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10824         MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10825         MISC_REGISTERS_RESET_REG_2_UMAC0 |
10826         MISC_REGISTERS_RESET_REG_2_UMAC1;
10827 
10828     /*
10829      * Keep the following blocks in reset:
10830      *  - all xxMACs are handled by the elink code.
10831      */
10832     stay_reset2 =
10833         MISC_REGISTERS_RESET_REG_2_XMAC |
10834         MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
10835 
10836     /* Full reset masks according to the chip */
10837     reset_mask1 = 0xffffffff;
10838 
10839     if (CHIP_IS_E1(sc))
10840         reset_mask2 = 0xffff;
10841     else if (CHIP_IS_E1H(sc))
10842         reset_mask2 = 0x1ffff;
10843     else if (CHIP_IS_E2(sc))
10844         reset_mask2 = 0xfffff;
10845     else /* CHIP_IS_E3 */
10846         reset_mask2 = 0x3ffffff;
10847 
10848     /* Don't reset global blocks unless we need to */
10849     if (!global)
10850         reset_mask2 &= ~global_bits2;
10851 
10852     /*
10853      * In case of attention in the QM, we need to reset PXP
10854      * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
10855      * because otherwise QM reset would release 'close the gates' shortly
10856      * before resetting the PXP, then the PSWRQ would send a write
10857      * request to PGLUE. Then when PXP is reset, PGLUE would try to
10858      * read the payload data from PSWWR, but PSWWR would not
10859      * respond. The write queue in PGLUE would stuck, dmae commands
10860      * would not return. Therefore it's important to reset the second
10861      * reset register (containing the
10862      * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
10863      * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
10864      * bit).
10865      */
10866     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
10867            reset_mask2 & (~not_reset_mask2));
10868 
10869     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
10870            reset_mask1 & (~not_reset_mask1));
10871 
10872     mb();
10873     wmb();
10874 
10875     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
10876            reset_mask2 & (~stay_reset2));
10877 
10878     mb();
10879     wmb();
10880 
10881     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
10882     wmb();
10883 }
10884 
10885 static int
10886 bxe_process_kill(struct bxe_softc *sc,
10887                  uint8_t          global)
10888 {
10889     int cnt = 1000;
10890     uint32_t val = 0;
10891     uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
10892     uint32_t tags_63_32 = 0;
10893 
10894     /* Empty the Tetris buffer, wait for 1s */
10895     do {
10896         sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
10897         blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
10898         port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
10899         port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
10900         pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
10901         if (CHIP_IS_E3(sc)) {
10902             tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
10903         }
10904 
10905         if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
10906             ((port_is_idle_0 & 0x1) == 0x1) &&
10907             ((port_is_idle_1 & 0x1) == 0x1) &&
10908             (pgl_exp_rom2 == 0xffffffff) &&
10909             (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
10910             break;
10911         DELAY(1000);
10912     } while (cnt-- > 0);
10913 
10914     if (cnt <= 0) {
10915         BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
10916                   "are still outstanding read requests after 1s! "
10917                   "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
10918                   "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
10919               sr_cnt, blk_cnt, port_is_idle_0,
10920               port_is_idle_1, pgl_exp_rom2);
10921         return (-1);
10922     }
10923 
10924     mb();
10925 
10926     /* Close gates #2, #3 and #4 */
10927     bxe_set_234_gates(sc, TRUE);
10928 
10929     /* Poll for IGU VQs for 57712 and newer chips */
10930     if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
10931         return (-1);
10932     }
10933 
10934     /* XXX indicate that "process kill" is in progress to MCP */
10935 
10936     /* clear "unprepared" bit */
10937     REG_WR(sc, MISC_REG_UNPREPARED, 0);
10938     mb();
10939 
10940     /* Make sure all is written to the chip before the reset */
10941     wmb();
10942 
10943     /*
10944      * Wait for 1ms to empty GLUE and PCI-E core queues,
10945      * PSWHST, GRC and PSWRD Tetris buffer.
10946      */
10947     DELAY(1000);
10948 
10949     /* Prepare to chip reset: */
10950     /* MCP */
10951     if (global) {
10952         bxe_reset_mcp_prep(sc, &val);
10953     }
10954 
10955     /* PXP */
10956     bxe_pxp_prep(sc);
10957     mb();
10958 
10959     /* reset the chip */
10960     bxe_process_kill_chip_reset(sc, global);
10961     mb();
10962 
10963     /* clear errors in PGB */
10964     if (!CHIP_IS_E1(sc))
10965         REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
10966 
10967     /* Recover after reset: */
10968     /* MCP */
10969     if (global && bxe_reset_mcp_comp(sc, val)) {
10970         return (-1);
10971     }
10972 
10973     /* XXX add resetting the NO_MCP mode DB here */
10974 
10975     /* Open the gates #2, #3 and #4 */
10976     bxe_set_234_gates(sc, FALSE);
10977 
10978     /* XXX
10979      * IGU/AEU preparation bring back the AEU/IGU to a reset state
10980      * re-enable attentions
10981      */
10982 
10983     return (0);
10984 }
10985 
10986 static int
10987 bxe_leader_reset(struct bxe_softc *sc)
10988 {
10989     int rc = 0;
10990     uint8_t global = bxe_reset_is_global(sc);
10991     uint32_t load_code;
10992 
10993     /*
10994      * If not going to reset MCP, load "fake" driver to reset HW while
10995      * driver is owner of the HW.
10996      */
10997     if (!global && !BXE_NOMCP(sc)) {
10998         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
10999                                    DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11000         if (!load_code) {
11001             BLOGE(sc, "MCP response failure, aborting\n");
11002             rc = -1;
11003             goto exit_leader_reset;
11004         }
11005 
11006         if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11007             (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11008             BLOGE(sc, "MCP unexpected response, aborting\n");
11009             rc = -1;
11010             goto exit_leader_reset2;
11011         }
11012 
11013         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11014         if (!load_code) {
11015             BLOGE(sc, "MCP response failure, aborting\n");
11016             rc = -1;
11017             goto exit_leader_reset2;
11018         }
11019     }
11020 
11021     /* try to recover after the failure */
11022     if (bxe_process_kill(sc, global)) {
11023         BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11024         rc = -1;
11025         goto exit_leader_reset2;
11026     }
11027 
11028     /*
11029      * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11030      * state.
11031      */
11032     bxe_set_reset_done(sc);
11033     if (global) {
11034         bxe_clear_reset_global(sc);
11035     }
11036 
11037 exit_leader_reset2:
11038 
11039     /* unload "fake driver" if it was loaded */
11040     if (!global && !BXE_NOMCP(sc)) {
11041         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11042         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11043     }
11044 
11045 exit_leader_reset:
11046 
11047     sc->is_leader = 0;
11048     bxe_release_leader_lock(sc);
11049 
11050     mb();
11051     return (rc);
11052 }
11053 
11054 /*
11055  * prepare INIT transition, parameters configured:
11056  *   - HC configuration
11057  *   - Queue's CDU context
11058  */
11059 static void
11060 bxe_pf_q_prep_init(struct bxe_softc               *sc,
11061                    struct bxe_fastpath            *fp,
11062                    struct ecore_queue_init_params *init_params)
11063 {
11064     uint8_t cos;
11065     int cxt_index, cxt_offset;
11066 
11067     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11068     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11069 
11070     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11071     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11072 
11073     /* HC rate */
11074     init_params->rx.hc_rate =
11075         sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11076     init_params->tx.hc_rate =
11077         sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11078 
11079     /* FW SB ID */
11080     init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11081 
11082     /* CQ index among the SB indices */
11083     init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11084     init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11085 
11086     /* set maximum number of COSs supported by this queue */
11087     init_params->max_cos = sc->max_cos;
11088 
11089     BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11090           fp->index, init_params->max_cos);
11091 
11092     /* set the context pointers queue object */
11093     for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11094         /* XXX change index/cid here if ever support multiple tx CoS */
11095         /* fp->txdata[cos]->cid */
11096         cxt_index = fp->index / ILT_PAGE_CIDS;
11097         cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11098         init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11099     }
11100 }
11101 
11102 /* set flags that are common for the Tx-only and not normal connections */
11103 static unsigned long
11104 bxe_get_common_flags(struct bxe_softc    *sc,
11105                      struct bxe_fastpath *fp,
11106                      uint8_t             zero_stats)
11107 {
11108     unsigned long flags = 0;
11109 
11110     /* PF driver will always initialize the Queue to an ACTIVE state */
11111     bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11112 
11113     /*
11114      * tx only connections collect statistics (on the same index as the
11115      * parent connection). The statistics are zeroed when the parent
11116      * connection is initialized.
11117      */
11118 
11119     bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11120     if (zero_stats) {
11121         bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11122     }
11123 
11124     /*
11125      * tx only connections can support tx-switching, though their
11126      * CoS-ness doesn't survive the loopback
11127      */
11128     if (sc->flags & BXE_TX_SWITCHING) {
11129         bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11130     }
11131 
11132     bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11133 
11134     return (flags);
11135 }
11136 
11137 static unsigned long
11138 bxe_get_q_flags(struct bxe_softc    *sc,
11139                 struct bxe_fastpath *fp,
11140                 uint8_t             leading)
11141 {
11142     unsigned long flags = 0;
11143 
11144     if (IS_MF_SD(sc)) {
11145         bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11146     }
11147 
11148     if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11149         bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11150         bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11151     }
11152 
11153     if (leading) {
11154         bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11155         bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11156     }
11157 
11158     bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11159 
11160     /* merge with common flags */
11161     return (flags | bxe_get_common_flags(sc, fp, TRUE));
11162 }
11163 
11164 static void
11165 bxe_pf_q_prep_general(struct bxe_softc                  *sc,
11166                       struct bxe_fastpath               *fp,
11167                       struct ecore_general_setup_params *gen_init,
11168                       uint8_t                           cos)
11169 {
11170     gen_init->stat_id = bxe_stats_id(fp);
11171     gen_init->spcl_id = fp->cl_id;
11172     gen_init->mtu = sc->mtu;
11173     gen_init->cos = cos;
11174 }
11175 
11176 static void
11177 bxe_pf_rx_q_prep(struct bxe_softc              *sc,
11178                  struct bxe_fastpath           *fp,
11179                  struct rxq_pause_params       *pause,
11180                  struct ecore_rxq_setup_params *rxq_init)
11181 {
11182     uint8_t max_sge = 0;
11183     uint16_t sge_sz = 0;
11184     uint16_t tpa_agg_size = 0;
11185 
11186     pause->sge_th_lo = SGE_TH_LO(sc);
11187     pause->sge_th_hi = SGE_TH_HI(sc);
11188 
11189     /* validate SGE ring has enough to cross high threshold */
11190     if (sc->dropless_fc &&
11191             (pause->sge_th_hi + FW_PREFETCH_CNT) >
11192             (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11193         BLOGW(sc, "sge ring threshold limit\n");
11194     }
11195 
11196     /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11197     tpa_agg_size = (2 * sc->mtu);
11198     if (tpa_agg_size < sc->max_aggregation_size) {
11199         tpa_agg_size = sc->max_aggregation_size;
11200     }
11201 
11202     max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11203     max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11204                    (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11205     sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11206 
11207     /* pause - not for e1 */
11208     if (!CHIP_IS_E1(sc)) {
11209         pause->bd_th_lo = BD_TH_LO(sc);
11210         pause->bd_th_hi = BD_TH_HI(sc);
11211 
11212         pause->rcq_th_lo = RCQ_TH_LO(sc);
11213         pause->rcq_th_hi = RCQ_TH_HI(sc);
11214 
11215         /* validate rings have enough entries to cross high thresholds */
11216         if (sc->dropless_fc &&
11217             pause->bd_th_hi + FW_PREFETCH_CNT >
11218             sc->rx_ring_size) {
11219             BLOGW(sc, "rx bd ring threshold limit\n");
11220         }
11221 
11222         if (sc->dropless_fc &&
11223             pause->rcq_th_hi + FW_PREFETCH_CNT >
11224             RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11225             BLOGW(sc, "rcq ring threshold limit\n");
11226         }
11227 
11228         pause->pri_map = 1;
11229     }
11230 
11231     /* rxq setup */
11232     rxq_init->dscr_map   = fp->rx_dma.paddr;
11233     rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11234     rxq_init->rcq_map    = fp->rcq_dma.paddr;
11235     rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11236 
11237     /*
11238      * This should be a maximum number of data bytes that may be
11239      * placed on the BD (not including paddings).
11240      */
11241     rxq_init->buf_sz = (fp->rx_buf_size -
11242                         IP_HEADER_ALIGNMENT_PADDING);
11243 
11244     rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11245     rxq_init->tpa_agg_sz      = tpa_agg_size;
11246     rxq_init->sge_buf_sz      = sge_sz;
11247     rxq_init->max_sges_pkt    = max_sge;
11248     rxq_init->rss_engine_id   = SC_FUNC(sc);
11249     rxq_init->mcast_engine_id = SC_FUNC(sc);
11250 
11251     /*
11252      * Maximum number or simultaneous TPA aggregation for this Queue.
11253      * For PF Clients it should be the maximum available number.
11254      * VF driver(s) may want to define it to a smaller value.
11255      */
11256     rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11257 
11258     rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11259     rxq_init->fw_sb_id = fp->fw_sb_id;
11260 
11261     rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11262 
11263     /*
11264      * configure silent vlan removal
11265      * if multi function mode is afex, then mask default vlan
11266      */
11267     if (IS_MF_AFEX(sc)) {
11268         rxq_init->silent_removal_value =
11269             sc->devinfo.mf_info.afex_def_vlan_tag;
11270         rxq_init->silent_removal_mask = EVL_VLID_MASK;
11271     }
11272 }
11273 
11274 static void
11275 bxe_pf_tx_q_prep(struct bxe_softc              *sc,
11276                  struct bxe_fastpath           *fp,
11277                  struct ecore_txq_setup_params *txq_init,
11278                  uint8_t                       cos)
11279 {
11280     /*
11281      * XXX If multiple CoS is ever supported then each fastpath structure
11282      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11283      * fp->txdata[cos]->tx_dma.paddr;
11284      */
11285     txq_init->dscr_map     = fp->tx_dma.paddr;
11286     txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11287     txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11288     txq_init->fw_sb_id     = fp->fw_sb_id;
11289 
11290     /*
11291      * set the TSS leading client id for TX classfication to the
11292      * leading RSS client id
11293      */
11294     txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11295 }
11296 
11297 /*
11298  * This function performs 2 steps in a queue state machine:
11299  *   1) RESET->INIT
11300  *   2) INIT->SETUP
11301  */
11302 static int
11303 bxe_setup_queue(struct bxe_softc    *sc,
11304                 struct bxe_fastpath *fp,
11305                 uint8_t             leading)
11306 {
11307     struct ecore_queue_state_params q_params = { NULL };
11308     struct ecore_queue_setup_params *setup_params =
11309                         &q_params.params.setup;
11310     int rc;
11311 
11312     BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11313 
11314     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11315 
11316     q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11317 
11318     /* we want to wait for completion in this context */
11319     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11320 
11321     /* prepare the INIT parameters */
11322     bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11323 
11324     /* Set the command */
11325     q_params.cmd = ECORE_Q_CMD_INIT;
11326 
11327     /* Change the state to INIT */
11328     rc = ecore_queue_state_change(sc, &q_params);
11329     if (rc) {
11330         BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11331         return (rc);
11332     }
11333 
11334     BLOGD(sc, DBG_LOAD, "init complete\n");
11335 
11336     /* now move the Queue to the SETUP state */
11337     memset(setup_params, 0, sizeof(*setup_params));
11338 
11339     /* set Queue flags */
11340     setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11341 
11342     /* set general SETUP parameters */
11343     bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11344                           FIRST_TX_COS_INDEX);
11345 
11346     bxe_pf_rx_q_prep(sc, fp,
11347                      &setup_params->pause_params,
11348                      &setup_params->rxq_params);
11349 
11350     bxe_pf_tx_q_prep(sc, fp,
11351                      &setup_params->txq_params,
11352                      FIRST_TX_COS_INDEX);
11353 
11354     /* Set the command */
11355     q_params.cmd = ECORE_Q_CMD_SETUP;
11356 
11357     /* change the state to SETUP */
11358     rc = ecore_queue_state_change(sc, &q_params);
11359     if (rc) {
11360         BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11361         return (rc);
11362     }
11363 
11364     return (rc);
11365 }
11366 
11367 static int
11368 bxe_setup_leading(struct bxe_softc *sc)
11369 {
11370     return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11371 }
11372 
11373 static int
11374 bxe_config_rss_pf(struct bxe_softc            *sc,
11375                   struct ecore_rss_config_obj *rss_obj,
11376                   uint8_t                     config_hash)
11377 {
11378     struct ecore_config_rss_params params = { NULL };
11379     int i;
11380 
11381     /*
11382      * Although RSS is meaningless when there is a single HW queue we
11383      * still need it enabled in order to have HW Rx hash generated.
11384      */
11385 
11386     params.rss_obj = rss_obj;
11387 
11388     bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11389 
11390     bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11391 
11392     /* RSS configuration */
11393     bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11394     bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11395     bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11396     bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11397     if (rss_obj->udp_rss_v4) {
11398         bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11399     }
11400     if (rss_obj->udp_rss_v6) {
11401         bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11402     }
11403 
11404     /* Hash bits */
11405     params.rss_result_mask = MULTI_MASK;
11406 
11407     memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11408 
11409     if (config_hash) {
11410         /* RSS keys */
11411         for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11412             params.rss_key[i] = arc4random();
11413         }
11414 
11415         bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11416     }
11417 
11418     return (ecore_config_rss(sc, &params));
11419 }
11420 
11421 static int
11422 bxe_config_rss_eth(struct bxe_softc *sc,
11423                    uint8_t          config_hash)
11424 {
11425     return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11426 }
11427 
11428 static int
11429 bxe_init_rss_pf(struct bxe_softc *sc)
11430 {
11431     uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11432     int i;
11433 
11434     /*
11435      * Prepare the initial contents of the indirection table if
11436      * RSS is enabled
11437      */
11438     for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11439         sc->rss_conf_obj.ind_table[i] =
11440             (sc->fp->cl_id + (i % num_eth_queues));
11441     }
11442 
11443     if (sc->udp_rss) {
11444         sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11445     }
11446 
11447     /*
11448      * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11449      * per-port, so if explicit configuration is needed, do it only
11450      * for a PMF.
11451      *
11452      * For 57712 and newer it's a per-function configuration.
11453      */
11454     return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11455 }
11456 
11457 static int
11458 bxe_set_mac_one(struct bxe_softc          *sc,
11459                 uint8_t                   *mac,
11460                 struct ecore_vlan_mac_obj *obj,
11461                 uint8_t                   set,
11462                 int                       mac_type,
11463                 unsigned long             *ramrod_flags)
11464 {
11465     struct ecore_vlan_mac_ramrod_params ramrod_param;
11466     int rc;
11467 
11468     memset(&ramrod_param, 0, sizeof(ramrod_param));
11469 
11470     /* fill in general parameters */
11471     ramrod_param.vlan_mac_obj = obj;
11472     ramrod_param.ramrod_flags = *ramrod_flags;
11473 
11474     /* fill a user request section if needed */
11475     if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11476         memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11477 
11478         bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11479 
11480         /* Set the command: ADD or DEL */
11481         ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11482                                             ECORE_VLAN_MAC_DEL;
11483     }
11484 
11485     rc = ecore_config_vlan_mac(sc, &ramrod_param);
11486 
11487     if (rc == ECORE_EXISTS) {
11488         BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11489         /* do not treat adding same MAC as error */
11490         rc = 0;
11491     } else if (rc < 0) {
11492         BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11493     }
11494 
11495     return (rc);
11496 }
11497 
11498 static int
11499 bxe_set_eth_mac(struct bxe_softc *sc,
11500                 uint8_t          set)
11501 {
11502     unsigned long ramrod_flags = 0;
11503 
11504     BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11505 
11506     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11507 
11508     /* Eth MAC is set on RSS leading client (fp[0]) */
11509     return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11510                             &sc->sp_objs->mac_obj,
11511                             set, ECORE_ETH_MAC, &ramrod_flags));
11512 }
11513 
11514 static int
11515 bxe_get_cur_phy_idx(struct bxe_softc *sc)
11516 {
11517     uint32_t sel_phy_idx = 0;
11518 
11519     if (sc->link_params.num_phys <= 1) {
11520         return (ELINK_INT_PHY);
11521     }
11522 
11523     if (sc->link_vars.link_up) {
11524         sel_phy_idx = ELINK_EXT_PHY1;
11525         /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11526         if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11527             (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11528              ELINK_SUPPORTED_FIBRE))
11529             sel_phy_idx = ELINK_EXT_PHY2;
11530     } else {
11531         switch (elink_phy_selection(&sc->link_params)) {
11532         case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11533         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11534         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11535                sel_phy_idx = ELINK_EXT_PHY1;
11536                break;
11537         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11538         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11539                sel_phy_idx = ELINK_EXT_PHY2;
11540                break;
11541         }
11542     }
11543 
11544     return (sel_phy_idx);
11545 }
11546 
11547 static int
11548 bxe_get_link_cfg_idx(struct bxe_softc *sc)
11549 {
11550     uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11551 
11552     /*
11553      * The selected activated PHY is always after swapping (in case PHY
11554      * swapping is enabled). So when swapping is enabled, we need to reverse
11555      * the configuration
11556      */
11557 
11558     if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11559         if (sel_phy_idx == ELINK_EXT_PHY1)
11560             sel_phy_idx = ELINK_EXT_PHY2;
11561         else if (sel_phy_idx == ELINK_EXT_PHY2)
11562             sel_phy_idx = ELINK_EXT_PHY1;
11563     }
11564 
11565     return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11566 }
11567 
11568 static void
11569 bxe_set_requested_fc(struct bxe_softc *sc)
11570 {
11571     /*
11572      * Initialize link parameters structure variables
11573      * It is recommended to turn off RX FC for jumbo frames
11574      * for better performance
11575      */
11576     if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11577         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11578     } else {
11579         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11580     }
11581 }
11582 
11583 static void
11584 bxe_calc_fc_adv(struct bxe_softc *sc)
11585 {
11586     uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11587     switch (sc->link_vars.ieee_fc &
11588             MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11589     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
11590     default:
11591         sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11592                                            ADVERTISED_Pause);
11593         break;
11594 
11595     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11596         sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11597                                           ADVERTISED_Pause);
11598         break;
11599 
11600     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11601         sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11602         break;
11603     }
11604 }
11605 
11606 static uint16_t
11607 bxe_get_mf_speed(struct bxe_softc *sc)
11608 {
11609     uint16_t line_speed = sc->link_vars.line_speed;
11610     if (IS_MF(sc)) {
11611         uint16_t maxCfg =
11612             bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11613 
11614         /* calculate the current MAX line speed limit for the MF devices */
11615         if (IS_MF_SI(sc)) {
11616             line_speed = (line_speed * maxCfg) / 100;
11617         } else { /* SD mode */
11618             uint16_t vn_max_rate = maxCfg * 100;
11619 
11620             if (vn_max_rate < line_speed) {
11621                 line_speed = vn_max_rate;
11622             }
11623         }
11624     }
11625 
11626     return (line_speed);
11627 }
11628 
11629 static void
11630 bxe_fill_report_data(struct bxe_softc            *sc,
11631                      struct bxe_link_report_data *data)
11632 {
11633     uint16_t line_speed = bxe_get_mf_speed(sc);
11634 
11635     memset(data, 0, sizeof(*data));
11636 
11637     /* fill the report data with the effective line speed */
11638     data->line_speed = line_speed;
11639 
11640     /* Link is down */
11641     if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11642         bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11643     }
11644 
11645     /* Full DUPLEX */
11646     if (sc->link_vars.duplex == DUPLEX_FULL) {
11647         bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11648     }
11649 
11650     /* Rx Flow Control is ON */
11651     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11652         bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11653     }
11654 
11655     /* Tx Flow Control is ON */
11656     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11657         bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11658     }
11659 }
11660 
11661 /* report link status to OS, should be called under phy_lock */
11662 static void
11663 bxe_link_report_locked(struct bxe_softc *sc)
11664 {
11665     struct bxe_link_report_data cur_data;
11666 
11667     /* reread mf_cfg */
11668     if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11669         bxe_read_mf_cfg(sc);
11670     }
11671 
11672     /* Read the current link report info */
11673     bxe_fill_report_data(sc, &cur_data);
11674 
11675     /* Don't report link down or exactly the same link status twice */
11676     if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11677         (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11678                       &sc->last_reported_link.link_report_flags) &&
11679          bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11680                       &cur_data.link_report_flags))) {
11681         return;
11682     }
11683 
11684     sc->link_cnt++;
11685 
11686     /* report new link params and remember the state for the next time */
11687     memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11688 
11689     if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11690                      &cur_data.link_report_flags)) {
11691         if_link_state_change(sc->ifp, LINK_STATE_DOWN);
11692         BLOGI(sc, "NIC Link is Down\n");
11693     } else {
11694         const char *duplex;
11695         const char *flow;
11696 
11697         if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11698                                    &cur_data.link_report_flags)) {
11699             duplex = "full";
11700         } else {
11701             duplex = "half";
11702         }
11703 
11704         /*
11705          * Handle the FC at the end so that only these flags would be
11706          * possibly set. This way we may easily check if there is no FC
11707          * enabled.
11708          */
11709         if (cur_data.link_report_flags) {
11710             if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11711                              &cur_data.link_report_flags) &&
11712                 bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11713                              &cur_data.link_report_flags)) {
11714                 flow = "ON - receive & transmit";
11715             } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11716                                     &cur_data.link_report_flags) &&
11717                        !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11718                                      &cur_data.link_report_flags)) {
11719                 flow = "ON - receive";
11720             } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11721                                      &cur_data.link_report_flags) &&
11722                        bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11723                                     &cur_data.link_report_flags)) {
11724                 flow = "ON - transmit";
11725             } else {
11726                 flow = "none"; /* possible? */
11727             }
11728         } else {
11729             flow = "none";
11730         }
11731 
11732         if_link_state_change(sc->ifp, LINK_STATE_UP);
11733         BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11734               cur_data.line_speed, duplex, flow);
11735     }
11736 }
11737 
11738 static void
11739 bxe_link_report(struct bxe_softc *sc)
11740 {
11741     bxe_acquire_phy_lock(sc);
11742     bxe_link_report_locked(sc);
11743     bxe_release_phy_lock(sc);
11744 }
11745 
11746 static void
11747 bxe_link_status_update(struct bxe_softc *sc)
11748 {
11749     if (sc->state != BXE_STATE_OPEN) {
11750         return;
11751     }
11752 
11753     if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11754         elink_link_status_update(&sc->link_params, &sc->link_vars);
11755     } else {
11756         sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11757                                   ELINK_SUPPORTED_10baseT_Full |
11758                                   ELINK_SUPPORTED_100baseT_Half |
11759                                   ELINK_SUPPORTED_100baseT_Full |
11760                                   ELINK_SUPPORTED_1000baseT_Full |
11761                                   ELINK_SUPPORTED_2500baseX_Full |
11762                                   ELINK_SUPPORTED_10000baseT_Full |
11763                                   ELINK_SUPPORTED_TP |
11764                                   ELINK_SUPPORTED_FIBRE |
11765                                   ELINK_SUPPORTED_Autoneg |
11766                                   ELINK_SUPPORTED_Pause |
11767                                   ELINK_SUPPORTED_Asym_Pause);
11768         sc->port.advertising[0] = sc->port.supported[0];
11769 
11770         sc->link_params.sc                = sc;
11771         sc->link_params.port              = SC_PORT(sc);
11772         sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11773         sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11774         sc->link_params.req_line_speed[0] = SPEED_10000;
11775         sc->link_params.speed_cap_mask[0] = 0x7f0000;
11776         sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11777 
11778         if (CHIP_REV_IS_FPGA(sc)) {
11779             sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11780             sc->link_vars.line_speed  = ELINK_SPEED_1000;
11781             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11782                                          LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11783         } else {
11784             sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11785             sc->link_vars.line_speed  = ELINK_SPEED_10000;
11786             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11787                                          LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11788         }
11789 
11790         sc->link_vars.link_up = 1;
11791 
11792         sc->link_vars.duplex    = DUPLEX_FULL;
11793         sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11794 
11795         if (IS_PF(sc)) {
11796             REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11797             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11798             bxe_link_report(sc);
11799         }
11800     }
11801 
11802     if (IS_PF(sc)) {
11803         if (sc->link_vars.link_up) {
11804             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11805         } else {
11806             bxe_stats_handle(sc, STATS_EVENT_STOP);
11807         }
11808         bxe_link_report(sc);
11809     } else {
11810         bxe_link_report(sc);
11811         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11812     }
11813 }
11814 
11815 static int
11816 bxe_initial_phy_init(struct bxe_softc *sc,
11817                      int              load_mode)
11818 {
11819     int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
11820     uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
11821     struct elink_params *lp = &sc->link_params;
11822 
11823     bxe_set_requested_fc(sc);
11824 
11825     if (CHIP_REV_IS_SLOW(sc)) {
11826         uint32_t bond = CHIP_BOND_ID(sc);
11827         uint32_t feat = 0;
11828 
11829         if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
11830             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11831         } else if (bond & 0x4) {
11832             if (CHIP_IS_E3(sc)) {
11833                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
11834             } else {
11835                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11836             }
11837         } else if (bond & 0x8) {
11838             if (CHIP_IS_E3(sc)) {
11839                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
11840             } else {
11841                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11842             }
11843         }
11844 
11845         /* disable EMAC for E3 and above */
11846         if (bond & 0x2) {
11847             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11848         }
11849 
11850         sc->link_params.feature_config_flags |= feat;
11851     }
11852 
11853     bxe_acquire_phy_lock(sc);
11854 
11855     if (load_mode == LOAD_DIAG) {
11856         lp->loopback_mode = ELINK_LOOPBACK_XGXS;
11857         /* Prefer doing PHY loopback at 10G speed, if possible */
11858         if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
11859             if (lp->speed_cap_mask[cfg_idx] &
11860                 PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
11861                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
11862             } else {
11863                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
11864             }
11865         }
11866     }
11867 
11868     if (load_mode == LOAD_LOOPBACK_EXT) {
11869         lp->loopback_mode = ELINK_LOOPBACK_EXT;
11870     }
11871 
11872     rc = elink_phy_init(&sc->link_params, &sc->link_vars);
11873 
11874     bxe_release_phy_lock(sc);
11875 
11876     bxe_calc_fc_adv(sc);
11877 
11878     if (sc->link_vars.link_up) {
11879         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11880         bxe_link_report(sc);
11881     }
11882 
11883     if (!CHIP_REV_IS_SLOW(sc)) {
11884         bxe_periodic_start(sc);
11885     }
11886 
11887     sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
11888     return (rc);
11889 }
11890 
11891 /* must be called under IF_ADDR_LOCK */
11892 
11893 static int
11894 bxe_set_mc_list(struct bxe_softc *sc)
11895 {
11896     struct ecore_mcast_ramrod_params rparam = { NULL };
11897     int rc = 0;
11898     int mc_count = 0;
11899     int mcnt, i;
11900     struct ecore_mcast_list_elem *mc_mac, *mc_mac_start;
11901     unsigned char *mta;
11902     if_t ifp = sc->ifp;
11903 
11904     mc_count = if_multiaddr_count(ifp, -1);/* XXX they don't have a limit */
11905     if (!mc_count)
11906         return (0);
11907 
11908     mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN *
11909             mc_count, M_DEVBUF, M_NOWAIT);
11910 
11911     if(mta == NULL) {
11912         BLOGE(sc, "Failed to allocate temp mcast list\n");
11913         return (-1);
11914     }
11915     bzero(mta, (sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count));
11916 
11917     mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF, (M_NOWAIT | M_ZERO));
11918     mc_mac_start = mc_mac;
11919 
11920     if (!mc_mac) {
11921         free(mta, M_DEVBUF);
11922         BLOGE(sc, "Failed to allocate temp mcast list\n");
11923         return (-1);
11924     }
11925     bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
11926 
11927     /* mta and mcnt not expected to be  different */
11928     if_multiaddr_array(ifp, mta, &mcnt, mc_count);
11929 
11930 
11931     rparam.mcast_obj = &sc->mcast_obj;
11932     ECORE_LIST_INIT(&rparam.mcast_list);
11933 
11934     for(i=0; i< mcnt; i++) {
11935 
11936         mc_mac->mac = (uint8_t *)(mta + (i * ETHER_ADDR_LEN));
11937         ECORE_LIST_PUSH_TAIL(&mc_mac->link, &rparam.mcast_list);
11938 
11939         BLOGD(sc, DBG_LOAD,
11940               "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
11941               mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
11942               mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
11943 
11944         mc_mac++;
11945     }
11946     rparam.mcast_list_len = mc_count;
11947 
11948     BXE_MCAST_LOCK(sc);
11949 
11950     /* first, clear all configured multicast MACs */
11951     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
11952     if (rc < 0) {
11953         BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
11954         BXE_MCAST_UNLOCK(sc);
11955     	free(mc_mac_start, M_DEVBUF);
11956         free(mta, M_DEVBUF);
11957         return (rc);
11958     }
11959 
11960     /* Now add the new MACs */
11961     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
11962     if (rc < 0) {
11963         BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
11964     }
11965 
11966     BXE_MCAST_UNLOCK(sc);
11967 
11968     free(mc_mac_start, M_DEVBUF);
11969     free(mta, M_DEVBUF);
11970 
11971     return (rc);
11972 }
11973 
11974 static int
11975 bxe_set_uc_list(struct bxe_softc *sc)
11976 {
11977     if_t ifp = sc->ifp;
11978     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
11979     struct ifaddr *ifa;
11980     unsigned long ramrod_flags = 0;
11981     int rc;
11982 
11983 #if __FreeBSD_version < 800000
11984     IF_ADDR_LOCK(ifp);
11985 #else
11986     if_addr_rlock(ifp);
11987 #endif
11988 
11989     /* first schedule a cleanup up of old configuration */
11990     rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
11991     if (rc < 0) {
11992         BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
11993 #if __FreeBSD_version < 800000
11994         IF_ADDR_UNLOCK(ifp);
11995 #else
11996         if_addr_runlock(ifp);
11997 #endif
11998         return (rc);
11999     }
12000 
12001     ifa = if_getifaddr(ifp); /* XXX Is this structure */
12002     while (ifa) {
12003         if (ifa->ifa_addr->sa_family != AF_LINK) {
12004             ifa = TAILQ_NEXT(ifa, ifa_link);
12005             continue;
12006         }
12007 
12008         rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12009                              mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12010         if (rc == -EEXIST) {
12011             BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12012             /* do not treat adding same MAC as an error */
12013             rc = 0;
12014         } else if (rc < 0) {
12015             BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12016 #if __FreeBSD_version < 800000
12017             IF_ADDR_UNLOCK(ifp);
12018 #else
12019             if_addr_runlock(ifp);
12020 #endif
12021             return (rc);
12022         }
12023 
12024         ifa = TAILQ_NEXT(ifa, ifa_link);
12025     }
12026 
12027 #if __FreeBSD_version < 800000
12028     IF_ADDR_UNLOCK(ifp);
12029 #else
12030     if_addr_runlock(ifp);
12031 #endif
12032 
12033     /* Execute the pending commands */
12034     bit_set(&ramrod_flags, RAMROD_CONT);
12035     return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12036                             ECORE_UC_LIST_MAC, &ramrod_flags));
12037 }
12038 
12039 static void
12040 bxe_set_rx_mode(struct bxe_softc *sc)
12041 {
12042     if_t ifp = sc->ifp;
12043     uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12044 
12045     if (sc->state != BXE_STATE_OPEN) {
12046         BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12047         return;
12048     }
12049 
12050     BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12051 
12052     if (if_getflags(ifp) & IFF_PROMISC) {
12053         rx_mode = BXE_RX_MODE_PROMISC;
12054     } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12055                ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12056                 CHIP_IS_E1(sc))) {
12057         rx_mode = BXE_RX_MODE_ALLMULTI;
12058     } else {
12059         if (IS_PF(sc)) {
12060             /* some multicasts */
12061             if (bxe_set_mc_list(sc) < 0) {
12062                 rx_mode = BXE_RX_MODE_ALLMULTI;
12063             }
12064             if (bxe_set_uc_list(sc) < 0) {
12065                 rx_mode = BXE_RX_MODE_PROMISC;
12066             }
12067         }
12068     }
12069 
12070     sc->rx_mode = rx_mode;
12071 
12072     /* schedule the rx_mode command */
12073     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12074         BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12075         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12076         return;
12077     }
12078 
12079     if (IS_PF(sc)) {
12080         bxe_set_storm_rx_mode(sc);
12081     }
12082 }
12083 
12084 
12085 /* update flags in shmem */
12086 static void
12087 bxe_update_drv_flags(struct bxe_softc *sc,
12088                      uint32_t         flags,
12089                      uint32_t         set)
12090 {
12091     uint32_t drv_flags;
12092 
12093     if (SHMEM2_HAS(sc, drv_flags)) {
12094         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12095         drv_flags = SHMEM2_RD(sc, drv_flags);
12096 
12097         if (set) {
12098             SET_FLAGS(drv_flags, flags);
12099         } else {
12100             RESET_FLAGS(drv_flags, flags);
12101         }
12102 
12103         SHMEM2_WR(sc, drv_flags, drv_flags);
12104         BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12105 
12106         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12107     }
12108 }
12109 
12110 /* periodic timer callout routine, only runs when the interface is up */
12111 
12112 static void
12113 bxe_periodic_callout_func(void *xsc)
12114 {
12115     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12116     struct bxe_fastpath *fp;
12117     uint16_t tx_bd_avail;
12118     int i;
12119 
12120     if (!BXE_CORE_TRYLOCK(sc)) {
12121         /* just bail and try again next time */
12122 
12123         if ((sc->state == BXE_STATE_OPEN) &&
12124             (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12125             /* schedule the next periodic callout */
12126             callout_reset(&sc->periodic_callout, hz,
12127                           bxe_periodic_callout_func, sc);
12128         }
12129 
12130         return;
12131     }
12132 
12133     if ((sc->state != BXE_STATE_OPEN) ||
12134         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12135         BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12136         BXE_CORE_UNLOCK(sc);
12137         return;
12138     }
12139 
12140 #if __FreeBSD_version >= 800000
12141 
12142     FOR_EACH_QUEUE(sc, i) {
12143         fp = &sc->fp[i];
12144 
12145         if (BXE_FP_TX_TRYLOCK(fp)) {
12146             if_t ifp = sc->ifp;
12147             /*
12148              * If interface was stopped due to unavailable
12149              * bds, try to process some tx completions
12150              */
12151             (void) bxe_txeof(sc, fp);
12152 
12153             tx_bd_avail = bxe_tx_avail(sc, fp);
12154             if (tx_bd_avail >= BXE_TX_CLEANUP_THRESHOLD) {
12155                 bxe_tx_mq_start_locked(sc, ifp, fp, NULL);
12156             }
12157             BXE_FP_TX_UNLOCK(fp);
12158         }
12159     }
12160 
12161 #else
12162 
12163     fp = &sc->fp[0];
12164     if (BXE_FP_TX_TRYLOCK(fp)) {
12165         struct ifnet *ifp = sc->ifnet;
12166         /*
12167          * If interface was stopped due to unavailable
12168          * bds, try to process some tx completions
12169          */
12170         (void) bxe_txeof(sc, fp);
12171 
12172         tx_bd_avail = bxe_tx_avail(sc, fp);
12173         if (tx_bd_avail >= BXE_TX_CLEANUP_THRESHOLD) {
12174             bxe_tx_start_locked(sc, ifp, fp);
12175         }
12176 
12177         BXE_FP_TX_UNLOCK(fp);
12178     }
12179 
12180 #endif /* #if __FreeBSD_version >= 800000 */
12181 
12182     /* Check for TX timeouts on any fastpath. */
12183     FOR_EACH_QUEUE(sc, i) {
12184         if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12185             /* Ruh-Roh, chip was reset! */
12186             break;
12187         }
12188     }
12189 
12190     if (!CHIP_REV_IS_SLOW(sc)) {
12191         /*
12192          * This barrier is needed to ensure the ordering between the writing
12193          * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12194          * the reading here.
12195          */
12196         mb();
12197         if (sc->port.pmf) {
12198 	    bxe_acquire_phy_lock(sc);
12199             elink_period_func(&sc->link_params, &sc->link_vars);
12200 	    bxe_release_phy_lock(sc);
12201         }
12202     }
12203 
12204     if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12205         int mb_idx = SC_FW_MB_IDX(sc);
12206         uint32_t drv_pulse;
12207         uint32_t mcp_pulse;
12208 
12209         ++sc->fw_drv_pulse_wr_seq;
12210         sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12211 
12212         drv_pulse = sc->fw_drv_pulse_wr_seq;
12213         bxe_drv_pulse(sc);
12214 
12215         mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12216                      MCP_PULSE_SEQ_MASK);
12217 
12218         /*
12219          * The delta between driver pulse and mcp response should
12220          * be 1 (before mcp response) or 0 (after mcp response).
12221          */
12222         if ((drv_pulse != mcp_pulse) &&
12223             (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12224             /* someone lost a heartbeat... */
12225             BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12226                   drv_pulse, mcp_pulse);
12227         }
12228     }
12229 
12230     /* state is BXE_STATE_OPEN */
12231     bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12232 
12233     BXE_CORE_UNLOCK(sc);
12234 
12235     if ((sc->state == BXE_STATE_OPEN) &&
12236         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12237         /* schedule the next periodic callout */
12238         callout_reset(&sc->periodic_callout, hz,
12239                       bxe_periodic_callout_func, sc);
12240     }
12241 }
12242 
12243 static void
12244 bxe_periodic_start(struct bxe_softc *sc)
12245 {
12246     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12247     callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12248 }
12249 
12250 static void
12251 bxe_periodic_stop(struct bxe_softc *sc)
12252 {
12253     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12254     callout_drain(&sc->periodic_callout);
12255 }
12256 
12257 /* start the controller */
12258 static __noinline int
12259 bxe_nic_load(struct bxe_softc *sc,
12260              int              load_mode)
12261 {
12262     uint32_t val;
12263     int load_code = 0;
12264     int i, rc = 0;
12265 
12266     BXE_CORE_LOCK_ASSERT(sc);
12267 
12268     BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12269 
12270     sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12271 
12272     if (IS_PF(sc)) {
12273         /* must be called before memory allocation and HW init */
12274         bxe_ilt_set_info(sc);
12275     }
12276 
12277     sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12278 
12279     bxe_set_fp_rx_buf_size(sc);
12280 
12281     if (bxe_alloc_fp_buffers(sc) != 0) {
12282         BLOGE(sc, "Failed to allocate fastpath memory\n");
12283         sc->state = BXE_STATE_CLOSED;
12284         rc = ENOMEM;
12285         goto bxe_nic_load_error0;
12286     }
12287 
12288     if (bxe_alloc_mem(sc) != 0) {
12289         sc->state = BXE_STATE_CLOSED;
12290         rc = ENOMEM;
12291         goto bxe_nic_load_error0;
12292     }
12293 
12294     if (bxe_alloc_fw_stats_mem(sc) != 0) {
12295         sc->state = BXE_STATE_CLOSED;
12296         rc = ENOMEM;
12297         goto bxe_nic_load_error0;
12298     }
12299 
12300     if (IS_PF(sc)) {
12301         /* set pf load just before approaching the MCP */
12302         bxe_set_pf_load(sc);
12303 
12304         /* if MCP exists send load request and analyze response */
12305         if (!BXE_NOMCP(sc)) {
12306             /* attempt to load pf */
12307             if (bxe_nic_load_request(sc, &load_code) != 0) {
12308                 sc->state = BXE_STATE_CLOSED;
12309                 rc = ENXIO;
12310                 goto bxe_nic_load_error1;
12311             }
12312 
12313             /* what did the MCP say? */
12314             if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12315                 bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12316                 sc->state = BXE_STATE_CLOSED;
12317                 rc = ENXIO;
12318                 goto bxe_nic_load_error2;
12319             }
12320         } else {
12321             BLOGI(sc, "Device has no MCP!\n");
12322             load_code = bxe_nic_load_no_mcp(sc);
12323         }
12324 
12325         /* mark PMF if applicable */
12326         bxe_nic_load_pmf(sc, load_code);
12327 
12328         /* Init Function state controlling object */
12329         bxe_init_func_obj(sc);
12330 
12331         /* Initialize HW */
12332         if (bxe_init_hw(sc, load_code) != 0) {
12333             BLOGE(sc, "HW init failed\n");
12334             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12335             sc->state = BXE_STATE_CLOSED;
12336             rc = ENXIO;
12337             goto bxe_nic_load_error2;
12338         }
12339     }
12340 
12341     /* set ALWAYS_ALIVE bit in shmem */
12342     sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12343     bxe_drv_pulse(sc);
12344     sc->flags |= BXE_NO_PULSE;
12345 
12346     /* attach interrupts */
12347     if (bxe_interrupt_attach(sc) != 0) {
12348         sc->state = BXE_STATE_CLOSED;
12349         rc = ENXIO;
12350         goto bxe_nic_load_error2;
12351     }
12352 
12353     bxe_nic_init(sc, load_code);
12354 
12355     /* Init per-function objects */
12356     if (IS_PF(sc)) {
12357         bxe_init_objs(sc);
12358         // XXX bxe_iov_nic_init(sc);
12359 
12360         /* set AFEX default VLAN tag to an invalid value */
12361         sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12362         // XXX bxe_nic_load_afex_dcc(sc, load_code);
12363 
12364         sc->state = BXE_STATE_OPENING_WAITING_PORT;
12365         rc = bxe_func_start(sc);
12366         if (rc) {
12367             BLOGE(sc, "Function start failed! rc = %d\n", rc);
12368             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12369             sc->state = BXE_STATE_ERROR;
12370             goto bxe_nic_load_error3;
12371         }
12372 
12373         /* send LOAD_DONE command to MCP */
12374         if (!BXE_NOMCP(sc)) {
12375             load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12376             if (!load_code) {
12377                 BLOGE(sc, "MCP response failure, aborting\n");
12378                 sc->state = BXE_STATE_ERROR;
12379                 rc = ENXIO;
12380                 goto bxe_nic_load_error3;
12381             }
12382         }
12383 
12384         rc = bxe_setup_leading(sc);
12385         if (rc) {
12386             BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12387             sc->state = BXE_STATE_ERROR;
12388             goto bxe_nic_load_error3;
12389         }
12390 
12391         FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12392             rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12393             if (rc) {
12394                 BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12395                 sc->state = BXE_STATE_ERROR;
12396                 goto bxe_nic_load_error3;
12397             }
12398         }
12399 
12400         rc = bxe_init_rss_pf(sc);
12401         if (rc) {
12402             BLOGE(sc, "PF RSS init failed\n");
12403             sc->state = BXE_STATE_ERROR;
12404             goto bxe_nic_load_error3;
12405         }
12406     }
12407     /* XXX VF */
12408 
12409     /* now when Clients are configured we are ready to work */
12410     sc->state = BXE_STATE_OPEN;
12411 
12412     /* Configure a ucast MAC */
12413     if (IS_PF(sc)) {
12414         rc = bxe_set_eth_mac(sc, TRUE);
12415     }
12416     if (rc) {
12417         BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12418         sc->state = BXE_STATE_ERROR;
12419         goto bxe_nic_load_error3;
12420     }
12421 
12422     if (sc->port.pmf) {
12423         rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12424         if (rc) {
12425             sc->state = BXE_STATE_ERROR;
12426             goto bxe_nic_load_error3;
12427         }
12428     }
12429 
12430     sc->link_params.feature_config_flags &=
12431         ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12432 
12433     /* start fast path */
12434 
12435     /* Initialize Rx filter */
12436     bxe_set_rx_mode(sc);
12437 
12438     /* start the Tx */
12439     switch (/* XXX load_mode */LOAD_OPEN) {
12440     case LOAD_NORMAL:
12441     case LOAD_OPEN:
12442         break;
12443 
12444     case LOAD_DIAG:
12445     case LOAD_LOOPBACK_EXT:
12446         sc->state = BXE_STATE_DIAG;
12447         break;
12448 
12449     default:
12450         break;
12451     }
12452 
12453     if (sc->port.pmf) {
12454         bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12455     } else {
12456         bxe_link_status_update(sc);
12457     }
12458 
12459     /* start the periodic timer callout */
12460     bxe_periodic_start(sc);
12461 
12462     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12463         /* mark driver is loaded in shmem2 */
12464         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12465         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12466                   (val |
12467                    DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12468                    DRV_FLAGS_CAPABILITIES_LOADED_L2));
12469     }
12470 
12471     /* wait for all pending SP commands to complete */
12472     if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12473         BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12474         bxe_periodic_stop(sc);
12475         bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12476         return (ENXIO);
12477     }
12478 
12479     /* Tell the stack the driver is running! */
12480     if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
12481 
12482     BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12483 
12484     return (0);
12485 
12486 bxe_nic_load_error3:
12487 
12488     if (IS_PF(sc)) {
12489         bxe_int_disable_sync(sc, 1);
12490 
12491         /* clean out queued objects */
12492         bxe_squeeze_objects(sc);
12493     }
12494 
12495     bxe_interrupt_detach(sc);
12496 
12497 bxe_nic_load_error2:
12498 
12499     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12500         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12501         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12502     }
12503 
12504     sc->port.pmf = 0;
12505 
12506 bxe_nic_load_error1:
12507 
12508     /* clear pf_load status, as it was already set */
12509     if (IS_PF(sc)) {
12510         bxe_clear_pf_load(sc);
12511     }
12512 
12513 bxe_nic_load_error0:
12514 
12515     bxe_free_fw_stats_mem(sc);
12516     bxe_free_fp_buffers(sc);
12517     bxe_free_mem(sc);
12518 
12519     return (rc);
12520 }
12521 
12522 static int
12523 bxe_init_locked(struct bxe_softc *sc)
12524 {
12525     int other_engine = SC_PATH(sc) ? 0 : 1;
12526     uint8_t other_load_status, load_status;
12527     uint8_t global = FALSE;
12528     int rc;
12529 
12530     BXE_CORE_LOCK_ASSERT(sc);
12531 
12532     /* check if the driver is already running */
12533     if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12534         BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12535         return (0);
12536     }
12537 
12538     bxe_set_power_state(sc, PCI_PM_D0);
12539 
12540     /*
12541      * If parity occurred during the unload, then attentions and/or
12542      * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12543      * loaded on the current engine to complete the recovery. Parity recovery
12544      * is only relevant for PF driver.
12545      */
12546     if (IS_PF(sc)) {
12547         other_load_status = bxe_get_load_status(sc, other_engine);
12548         load_status = bxe_get_load_status(sc, SC_PATH(sc));
12549 
12550         if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12551             bxe_chk_parity_attn(sc, &global, TRUE)) {
12552             do {
12553                 /*
12554                  * If there are attentions and they are in global blocks, set
12555                  * the GLOBAL_RESET bit regardless whether it will be this
12556                  * function that will complete the recovery or not.
12557                  */
12558                 if (global) {
12559                     bxe_set_reset_global(sc);
12560                 }
12561 
12562                 /*
12563                  * Only the first function on the current engine should try
12564                  * to recover in open. In case of attentions in global blocks
12565                  * only the first in the chip should try to recover.
12566                  */
12567                 if ((!load_status && (!global || !other_load_status)) &&
12568                     bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12569                     BLOGI(sc, "Recovered during init\n");
12570                     break;
12571                 }
12572 
12573                 /* recovery has failed... */
12574                 bxe_set_power_state(sc, PCI_PM_D3hot);
12575                 sc->recovery_state = BXE_RECOVERY_FAILED;
12576 
12577                 BLOGE(sc, "Recovery flow hasn't properly "
12578                           "completed yet, try again later. "
12579                           "If you still see this message after a "
12580                           "few retries then power cycle is required.\n");
12581 
12582                 rc = ENXIO;
12583                 goto bxe_init_locked_done;
12584             } while (0);
12585         }
12586     }
12587 
12588     sc->recovery_state = BXE_RECOVERY_DONE;
12589 
12590     rc = bxe_nic_load(sc, LOAD_OPEN);
12591 
12592 bxe_init_locked_done:
12593 
12594     if (rc) {
12595         /* Tell the stack the driver is NOT running! */
12596         BLOGE(sc, "Initialization failed, "
12597                   "stack notified driver is NOT running!\n");
12598 	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
12599     }
12600 
12601     return (rc);
12602 }
12603 
12604 static int
12605 bxe_stop_locked(struct bxe_softc *sc)
12606 {
12607     BXE_CORE_LOCK_ASSERT(sc);
12608     return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
12609 }
12610 
12611 /*
12612  * Handles controller initialization when called from an unlocked routine.
12613  * ifconfig calls this function.
12614  *
12615  * Returns:
12616  *   void
12617  */
12618 static void
12619 bxe_init(void *xsc)
12620 {
12621     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12622 
12623     BXE_CORE_LOCK(sc);
12624     bxe_init_locked(sc);
12625     BXE_CORE_UNLOCK(sc);
12626 }
12627 
12628 static int
12629 bxe_init_ifnet(struct bxe_softc *sc)
12630 {
12631     if_t ifp;
12632     int capabilities;
12633 
12634     /* ifconfig entrypoint for media type/status reporting */
12635     ifmedia_init(&sc->ifmedia, IFM_IMASK,
12636                  bxe_ifmedia_update,
12637                  bxe_ifmedia_status);
12638 
12639     /* set the default interface values */
12640     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
12641     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
12642     ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
12643 
12644     sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
12645 
12646     /* allocate the ifnet structure */
12647     if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
12648         BLOGE(sc, "Interface allocation failed!\n");
12649         return (ENXIO);
12650     }
12651 
12652     if_setsoftc(ifp, sc);
12653     if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
12654     if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
12655     if_setioctlfn(ifp, bxe_ioctl);
12656     if_setstartfn(ifp, bxe_tx_start);
12657     if_setgetcounterfn(ifp, bxe_get_counter);
12658 #if __FreeBSD_version >= 800000
12659     if_settransmitfn(ifp, bxe_tx_mq_start);
12660     if_setqflushfn(ifp, bxe_mq_flush);
12661 #endif
12662 #ifdef FreeBSD8_0
12663     if_settimer(ifp, 0);
12664 #endif
12665     if_setinitfn(ifp, bxe_init);
12666     if_setmtu(ifp, sc->mtu);
12667     if_sethwassist(ifp, (CSUM_IP      |
12668                         CSUM_TCP      |
12669                         CSUM_UDP      |
12670                         CSUM_TSO      |
12671                         CSUM_TCP_IPV6 |
12672                         CSUM_UDP_IPV6));
12673 
12674     capabilities =
12675 #if __FreeBSD_version < 700000
12676         (IFCAP_VLAN_MTU       |
12677          IFCAP_VLAN_HWTAGGING |
12678          IFCAP_HWCSUM         |
12679          IFCAP_JUMBO_MTU      |
12680          IFCAP_LRO);
12681 #else
12682         (IFCAP_VLAN_MTU       |
12683          IFCAP_VLAN_HWTAGGING |
12684          IFCAP_VLAN_HWTSO     |
12685          IFCAP_VLAN_HWFILTER  |
12686          IFCAP_VLAN_HWCSUM    |
12687          IFCAP_HWCSUM         |
12688          IFCAP_JUMBO_MTU      |
12689          IFCAP_LRO            |
12690          IFCAP_TSO4           |
12691          IFCAP_TSO6           |
12692          IFCAP_WOL_MAGIC);
12693 #endif
12694     if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
12695     if_setbaudrate(ifp, IF_Gbps(10));
12696 /* XXX */
12697     if_setsendqlen(ifp, sc->tx_ring_size);
12698     if_setsendqready(ifp);
12699 /* XXX */
12700 
12701     sc->ifp = ifp;
12702 
12703     /* attach to the Ethernet interface list */
12704     ether_ifattach(ifp, sc->link_params.mac_addr);
12705 
12706     return (0);
12707 }
12708 
12709 static void
12710 bxe_deallocate_bars(struct bxe_softc *sc)
12711 {
12712     int i;
12713 
12714     for (i = 0; i < MAX_BARS; i++) {
12715         if (sc->bar[i].resource != NULL) {
12716             bus_release_resource(sc->dev,
12717                                  SYS_RES_MEMORY,
12718                                  sc->bar[i].rid,
12719                                  sc->bar[i].resource);
12720             BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
12721                   i, PCIR_BAR(i));
12722         }
12723     }
12724 }
12725 
12726 static int
12727 bxe_allocate_bars(struct bxe_softc *sc)
12728 {
12729     u_int flags;
12730     int i;
12731 
12732     memset(sc->bar, 0, sizeof(sc->bar));
12733 
12734     for (i = 0; i < MAX_BARS; i++) {
12735 
12736         /* memory resources reside at BARs 0, 2, 4 */
12737         /* Run `pciconf -lb` to see mappings */
12738         if ((i != 0) && (i != 2) && (i != 4)) {
12739             continue;
12740         }
12741 
12742         sc->bar[i].rid = PCIR_BAR(i);
12743 
12744         flags = RF_ACTIVE;
12745         if (i == 0) {
12746             flags |= RF_SHAREABLE;
12747         }
12748 
12749         if ((sc->bar[i].resource =
12750              bus_alloc_resource_any(sc->dev,
12751                                     SYS_RES_MEMORY,
12752                                     &sc->bar[i].rid,
12753                                     flags)) == NULL) {
12754             return (0);
12755         }
12756 
12757         sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
12758         sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
12759         sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
12760 
12761         BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%jd) -> %p\n",
12762               i, PCIR_BAR(i),
12763               (void *)rman_get_start(sc->bar[i].resource),
12764               (void *)rman_get_end(sc->bar[i].resource),
12765               rman_get_size(sc->bar[i].resource),
12766               (void *)sc->bar[i].kva);
12767     }
12768 
12769     return (0);
12770 }
12771 
12772 static void
12773 bxe_get_function_num(struct bxe_softc *sc)
12774 {
12775     uint32_t val = 0;
12776 
12777     /*
12778      * Read the ME register to get the function number. The ME register
12779      * holds the relative-function number and absolute-function number. The
12780      * absolute-function number appears only in E2 and above. Before that
12781      * these bits always contained zero, therefore we cannot blindly use them.
12782      */
12783 
12784     val = REG_RD(sc, BAR_ME_REGISTER);
12785 
12786     sc->pfunc_rel =
12787         (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
12788     sc->path_id =
12789         (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
12790 
12791     if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
12792         sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
12793     } else {
12794         sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
12795     }
12796 
12797     BLOGD(sc, DBG_LOAD,
12798           "Relative function %d, Absolute function %d, Path %d\n",
12799           sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
12800 }
12801 
12802 static uint32_t
12803 bxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
12804 {
12805     uint32_t shmem2_size;
12806     uint32_t offset;
12807     uint32_t mf_cfg_offset_value;
12808 
12809     /* Non 57712 */
12810     offset = (SHMEM_RD(sc, func_mb) +
12811               (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
12812 
12813     /* 57712 plus */
12814     if (sc->devinfo.shmem2_base != 0) {
12815         shmem2_size = SHMEM2_RD(sc, size);
12816         if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
12817             mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
12818             if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
12819                 offset = mf_cfg_offset_value;
12820             }
12821         }
12822     }
12823 
12824     return (offset);
12825 }
12826 
12827 static uint32_t
12828 bxe_pcie_capability_read(struct bxe_softc *sc,
12829                          int    reg,
12830                          int    width)
12831 {
12832     int pcie_reg;
12833 
12834     /* ensure PCIe capability is enabled */
12835     if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
12836         if (pcie_reg != 0) {
12837             BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
12838             return (pci_read_config(sc->dev, (pcie_reg + reg), width));
12839         }
12840     }
12841 
12842     BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
12843 
12844     return (0);
12845 }
12846 
12847 static uint8_t
12848 bxe_is_pcie_pending(struct bxe_softc *sc)
12849 {
12850     return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
12851             PCIM_EXP_STA_TRANSACTION_PND);
12852 }
12853 
12854 /*
12855  * Walk the PCI capabiites list for the device to find what features are
12856  * supported. These capabilites may be enabled/disabled by firmware so it's
12857  * best to walk the list rather than make assumptions.
12858  */
12859 static void
12860 bxe_probe_pci_caps(struct bxe_softc *sc)
12861 {
12862     uint16_t link_status;
12863     int reg;
12864 
12865     /* check if PCI Power Management is enabled */
12866     if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
12867         if (reg != 0) {
12868             BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
12869 
12870             sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
12871             sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
12872         }
12873     }
12874 
12875     link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
12876 
12877     /* handle PCIe 2.0 workarounds for 57710 */
12878     if (CHIP_IS_E1(sc)) {
12879         /* workaround for 57710 errata E4_57710_27462 */
12880         sc->devinfo.pcie_link_speed =
12881             (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
12882 
12883         /* workaround for 57710 errata E4_57710_27488 */
12884         sc->devinfo.pcie_link_width =
12885             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12886         if (sc->devinfo.pcie_link_speed > 1) {
12887             sc->devinfo.pcie_link_width =
12888                 ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
12889         }
12890     } else {
12891         sc->devinfo.pcie_link_speed =
12892             (link_status & PCIM_LINK_STA_SPEED);
12893         sc->devinfo.pcie_link_width =
12894             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
12895     }
12896 
12897     BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
12898           sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
12899 
12900     sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
12901     sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
12902 
12903     /* check if MSI capability is enabled */
12904     if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
12905         if (reg != 0) {
12906             BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
12907 
12908             sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
12909             sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
12910         }
12911     }
12912 
12913     /* check if MSI-X capability is enabled */
12914     if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
12915         if (reg != 0) {
12916             BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
12917 
12918             sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
12919             sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
12920         }
12921     }
12922 }
12923 
12924 static int
12925 bxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
12926 {
12927     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
12928     uint32_t val;
12929 
12930     /* get the outer vlan if we're in switch-dependent mode */
12931 
12932     val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
12933     mf_info->ext_id = (uint16_t)val;
12934 
12935     mf_info->multi_vnics_mode = 1;
12936 
12937     if (!VALID_OVLAN(mf_info->ext_id)) {
12938         BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
12939         return (1);
12940     }
12941 
12942     /* get the capabilities */
12943     if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
12944         FUNC_MF_CFG_PROTOCOL_ISCSI) {
12945         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
12946     } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
12947                FUNC_MF_CFG_PROTOCOL_FCOE) {
12948         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
12949     } else {
12950         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
12951     }
12952 
12953     mf_info->vnics_per_port =
12954         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
12955 
12956     return (0);
12957 }
12958 
12959 static uint32_t
12960 bxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
12961 {
12962     uint32_t retval = 0;
12963     uint32_t val;
12964 
12965     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
12966 
12967     if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
12968         if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
12969             retval |= MF_PROTO_SUPPORT_ETHERNET;
12970         }
12971         if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
12972             retval |= MF_PROTO_SUPPORT_ISCSI;
12973         }
12974         if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
12975             retval |= MF_PROTO_SUPPORT_FCOE;
12976         }
12977     }
12978 
12979     return (retval);
12980 }
12981 
12982 static int
12983 bxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
12984 {
12985     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
12986     uint32_t val;
12987 
12988     /*
12989      * There is no outer vlan if we're in switch-independent mode.
12990      * If the mac is valid then assume multi-function.
12991      */
12992 
12993     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
12994 
12995     mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
12996 
12997     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
12998 
12999     mf_info->vnics_per_port =
13000         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13001 
13002     return (0);
13003 }
13004 
13005 static int
13006 bxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13007 {
13008     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13009     uint32_t e1hov_tag;
13010     uint32_t func_config;
13011     uint32_t niv_config;
13012 
13013     mf_info->multi_vnics_mode = 1;
13014 
13015     e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13016     func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13017     niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13018 
13019     mf_info->ext_id =
13020         (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13021                    FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13022 
13023     mf_info->default_vlan =
13024         (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13025                    FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13026 
13027     mf_info->niv_allowed_priorities =
13028         (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13029                   FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13030 
13031     mf_info->niv_default_cos =
13032         (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13033                   FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13034 
13035     mf_info->afex_vlan_mode =
13036         ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13037          FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13038 
13039     mf_info->niv_mba_enabled =
13040         ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13041          FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13042 
13043     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13044 
13045     mf_info->vnics_per_port =
13046         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13047 
13048     return (0);
13049 }
13050 
13051 static int
13052 bxe_check_valid_mf_cfg(struct bxe_softc *sc)
13053 {
13054     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13055     uint32_t mf_cfg1;
13056     uint32_t mf_cfg2;
13057     uint32_t ovlan1;
13058     uint32_t ovlan2;
13059     uint8_t i, j;
13060 
13061     BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13062           SC_PORT(sc));
13063     BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13064           mf_info->mf_config[SC_VN(sc)]);
13065     BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13066           mf_info->multi_vnics_mode);
13067     BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13068           mf_info->vnics_per_port);
13069     BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13070           mf_info->ext_id);
13071     BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13072           mf_info->min_bw[0], mf_info->min_bw[1],
13073           mf_info->min_bw[2], mf_info->min_bw[3]);
13074     BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13075           mf_info->max_bw[0], mf_info->max_bw[1],
13076           mf_info->max_bw[2], mf_info->max_bw[3]);
13077     BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13078           sc->mac_addr_str);
13079 
13080     /* various MF mode sanity checks... */
13081 
13082     if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13083         BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13084               SC_PORT(sc));
13085         return (1);
13086     }
13087 
13088     if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13089         BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13090               mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13091         return (1);
13092     }
13093 
13094     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13095         /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13096         if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13097             BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13098                   SC_VN(sc), OVLAN(sc));
13099             return (1);
13100         }
13101 
13102         if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13103             BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13104                   mf_info->multi_vnics_mode, OVLAN(sc));
13105             return (1);
13106         }
13107 
13108         /*
13109          * Verify all functions are either MF or SF mode. If MF, make sure
13110          * sure that all non-hidden functions have a valid ovlan. If SF,
13111          * make sure that all non-hidden functions have an invalid ovlan.
13112          */
13113         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13114             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13115             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13116             if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13117                 (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13118                  ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13119                 BLOGE(sc, "mf_mode=SD function %d MF config "
13120                           "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13121                       i, mf_info->multi_vnics_mode, ovlan1);
13122                 return (1);
13123             }
13124         }
13125 
13126         /* Verify all funcs on the same port each have a different ovlan. */
13127         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13128             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13129             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13130             /* iterate from the next function on the port to the max func */
13131             for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13132                 mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13133                 ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13134                 if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13135                     VALID_OVLAN(ovlan1) &&
13136                     !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13137                     VALID_OVLAN(ovlan2) &&
13138                     (ovlan1 == ovlan2)) {
13139                     BLOGE(sc, "mf_mode=SD functions %d and %d "
13140                               "have the same ovlan (%d)\n",
13141                           i, j, ovlan1);
13142                     return (1);
13143                 }
13144             }
13145         }
13146     } /* MULTI_FUNCTION_SD */
13147 
13148     return (0);
13149 }
13150 
13151 static int
13152 bxe_get_mf_cfg_info(struct bxe_softc *sc)
13153 {
13154     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13155     uint32_t val, mac_upper;
13156     uint8_t i, vnic;
13157 
13158     /* initialize mf_info defaults */
13159     mf_info->vnics_per_port   = 1;
13160     mf_info->multi_vnics_mode = FALSE;
13161     mf_info->path_has_ovlan   = FALSE;
13162     mf_info->mf_mode          = SINGLE_FUNCTION;
13163 
13164     if (!CHIP_IS_MF_CAP(sc)) {
13165         return (0);
13166     }
13167 
13168     if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13169         BLOGE(sc, "Invalid mf_cfg_base!\n");
13170         return (1);
13171     }
13172 
13173     /* get the MF mode (switch dependent / independent / single-function) */
13174 
13175     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13176 
13177     switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13178     {
13179     case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13180 
13181         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13182 
13183         /* check for legal upper mac bytes */
13184         if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13185             mf_info->mf_mode = MULTI_FUNCTION_SI;
13186         } else {
13187             BLOGE(sc, "Invalid config for Switch Independent mode\n");
13188         }
13189 
13190         break;
13191 
13192     case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13193     case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13194 
13195         /* get outer vlan configuration */
13196         val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13197 
13198         if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13199             FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13200             mf_info->mf_mode = MULTI_FUNCTION_SD;
13201         } else {
13202             BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13203         }
13204 
13205         break;
13206 
13207     case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13208 
13209         /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13210         return (0);
13211 
13212     case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13213 
13214         /*
13215          * Mark MF mode as NIV if MCP version includes NPAR-SD support
13216          * and the MAC address is valid.
13217          */
13218         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13219 
13220         if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13221             (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13222             mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13223         } else {
13224             BLOGE(sc, "Invalid config for AFEX mode\n");
13225         }
13226 
13227         break;
13228 
13229     default:
13230 
13231         BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13232               (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13233 
13234         return (1);
13235     }
13236 
13237     /* set path mf_mode (which could be different than function mf_mode) */
13238     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13239         mf_info->path_has_ovlan = TRUE;
13240     } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13241         /*
13242          * Decide on path multi vnics mode. If we're not in MF mode and in
13243          * 4-port mode, this is good enough to check vnic-0 of the other port
13244          * on the same path
13245          */
13246         if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13247             uint8_t other_port = !(PORT_ID(sc) & 1);
13248             uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13249 
13250             val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13251 
13252             mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13253         }
13254     }
13255 
13256     if (mf_info->mf_mode == SINGLE_FUNCTION) {
13257         /* invalid MF config */
13258         if (SC_VN(sc) >= 1) {
13259             BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13260             return (1);
13261         }
13262 
13263         return (0);
13264     }
13265 
13266     /* get the MF configuration */
13267     mf_info->mf_config[SC_VN(sc)] =
13268         MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13269 
13270     switch(mf_info->mf_mode)
13271     {
13272     case MULTI_FUNCTION_SD:
13273 
13274         bxe_get_shmem_mf_cfg_info_sd(sc);
13275         break;
13276 
13277     case MULTI_FUNCTION_SI:
13278 
13279         bxe_get_shmem_mf_cfg_info_si(sc);
13280         break;
13281 
13282     case MULTI_FUNCTION_AFEX:
13283 
13284         bxe_get_shmem_mf_cfg_info_niv(sc);
13285         break;
13286 
13287     default:
13288 
13289         BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13290               mf_info->mf_mode);
13291         return (1);
13292     }
13293 
13294     /* get the congestion management parameters */
13295 
13296     vnic = 0;
13297     FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13298         /* get min/max bw */
13299         val = MFCFG_RD(sc, func_mf_config[i].config);
13300         mf_info->min_bw[vnic] =
13301             ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13302         mf_info->max_bw[vnic] =
13303             ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13304         vnic++;
13305     }
13306 
13307     return (bxe_check_valid_mf_cfg(sc));
13308 }
13309 
13310 static int
13311 bxe_get_shmem_info(struct bxe_softc *sc)
13312 {
13313     int port;
13314     uint32_t mac_hi, mac_lo, val;
13315 
13316     port = SC_PORT(sc);
13317     mac_hi = mac_lo = 0;
13318 
13319     sc->link_params.sc   = sc;
13320     sc->link_params.port = port;
13321 
13322     /* get the hardware config info */
13323     sc->devinfo.hw_config =
13324         SHMEM_RD(sc, dev_info.shared_hw_config.config);
13325     sc->devinfo.hw_config2 =
13326         SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13327 
13328     sc->link_params.hw_led_mode =
13329         ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13330          SHARED_HW_CFG_LED_MODE_SHIFT);
13331 
13332     /* get the port feature config */
13333     sc->port.config =
13334         SHMEM_RD(sc, dev_info.port_feature_config[port].config),
13335 
13336     /* get the link params */
13337     sc->link_params.speed_cap_mask[0] =
13338         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13339     sc->link_params.speed_cap_mask[1] =
13340         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13341 
13342     /* get the lane config */
13343     sc->link_params.lane_config =
13344         SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13345 
13346     /* get the link config */
13347     val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13348     sc->port.link_config[ELINK_INT_PHY] = val;
13349     sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13350     sc->port.link_config[ELINK_EXT_PHY1] =
13351         SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13352 
13353     /* get the override preemphasis flag and enable it or turn it off */
13354     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13355     if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13356         sc->link_params.feature_config_flags |=
13357             ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13358     } else {
13359         sc->link_params.feature_config_flags &=
13360             ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13361     }
13362 
13363     /* get the initial value of the link params */
13364     sc->link_params.multi_phy_config =
13365         SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13366 
13367     /* get external phy info */
13368     sc->port.ext_phy_config =
13369         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13370 
13371     /* get the multifunction configuration */
13372     bxe_get_mf_cfg_info(sc);
13373 
13374     /* get the mac address */
13375     if (IS_MF(sc)) {
13376         mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13377         mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13378     } else {
13379         mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13380         mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13381     }
13382 
13383     if ((mac_lo == 0) && (mac_hi == 0)) {
13384         *sc->mac_addr_str = 0;
13385         BLOGE(sc, "No Ethernet address programmed!\n");
13386     } else {
13387         sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13388         sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13389         sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13390         sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13391         sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13392         sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13393         snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13394                  "%02x:%02x:%02x:%02x:%02x:%02x",
13395                  sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13396                  sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13397                  sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13398         BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13399     }
13400 
13401     return (0);
13402 }
13403 
13404 static void
13405 bxe_get_tunable_params(struct bxe_softc *sc)
13406 {
13407     /* sanity checks */
13408 
13409     if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13410         (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13411         (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13412         BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13413         bxe_interrupt_mode = INTR_MODE_MSIX;
13414     }
13415 
13416     if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13417         BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13418         bxe_queue_count = 0;
13419     }
13420 
13421     if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13422         if (bxe_max_rx_bufs == 0) {
13423             bxe_max_rx_bufs = RX_BD_USABLE;
13424         } else {
13425             BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13426             bxe_max_rx_bufs = 2048;
13427         }
13428     }
13429 
13430     if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13431         BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13432         bxe_hc_rx_ticks = 25;
13433     }
13434 
13435     if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13436         BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13437         bxe_hc_tx_ticks = 50;
13438     }
13439 
13440     if (bxe_max_aggregation_size == 0) {
13441         bxe_max_aggregation_size = TPA_AGG_SIZE;
13442     }
13443 
13444     if (bxe_max_aggregation_size > 0xffff) {
13445         BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13446               bxe_max_aggregation_size);
13447         bxe_max_aggregation_size = TPA_AGG_SIZE;
13448     }
13449 
13450     if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13451         BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13452         bxe_mrrs = -1;
13453     }
13454 
13455     if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13456         BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13457         bxe_autogreeen = 0;
13458     }
13459 
13460     if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13461         BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13462         bxe_udp_rss = 0;
13463     }
13464 
13465     /* pull in user settings */
13466 
13467     sc->interrupt_mode       = bxe_interrupt_mode;
13468     sc->max_rx_bufs          = bxe_max_rx_bufs;
13469     sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13470     sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13471     sc->max_aggregation_size = bxe_max_aggregation_size;
13472     sc->mrrs                 = bxe_mrrs;
13473     sc->autogreeen           = bxe_autogreeen;
13474     sc->udp_rss              = bxe_udp_rss;
13475 
13476     if (bxe_interrupt_mode == INTR_MODE_INTX) {
13477         sc->num_queues = 1;
13478     } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13479         sc->num_queues =
13480             min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13481                 MAX_RSS_CHAINS);
13482         if (sc->num_queues > mp_ncpus) {
13483             sc->num_queues = mp_ncpus;
13484         }
13485     }
13486 
13487     BLOGD(sc, DBG_LOAD,
13488           "User Config: "
13489           "debug=0x%lx "
13490           "interrupt_mode=%d "
13491           "queue_count=%d "
13492           "hc_rx_ticks=%d "
13493           "hc_tx_ticks=%d "
13494           "rx_budget=%d "
13495           "max_aggregation_size=%d "
13496           "mrrs=%d "
13497           "autogreeen=%d "
13498           "udp_rss=%d\n",
13499           bxe_debug,
13500           sc->interrupt_mode,
13501           sc->num_queues,
13502           sc->hc_rx_ticks,
13503           sc->hc_tx_ticks,
13504           bxe_rx_budget,
13505           sc->max_aggregation_size,
13506           sc->mrrs,
13507           sc->autogreeen,
13508           sc->udp_rss);
13509 }
13510 
13511 static int
13512 bxe_media_detect(struct bxe_softc *sc)
13513 {
13514     int port_type;
13515     uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13516 
13517     switch (sc->link_params.phy[phy_idx].media_type) {
13518     case ELINK_ETH_PHY_SFPP_10G_FIBER:
13519     case ELINK_ETH_PHY_XFP_FIBER:
13520         BLOGI(sc, "Found 10Gb Fiber media.\n");
13521         sc->media = IFM_10G_SR;
13522         port_type = PORT_FIBRE;
13523         break;
13524     case ELINK_ETH_PHY_SFP_1G_FIBER:
13525         BLOGI(sc, "Found 1Gb Fiber media.\n");
13526         sc->media = IFM_1000_SX;
13527         port_type = PORT_FIBRE;
13528         break;
13529     case ELINK_ETH_PHY_KR:
13530     case ELINK_ETH_PHY_CX4:
13531         BLOGI(sc, "Found 10GBase-CX4 media.\n");
13532         sc->media = IFM_10G_CX4;
13533         port_type = PORT_FIBRE;
13534         break;
13535     case ELINK_ETH_PHY_DA_TWINAX:
13536         BLOGI(sc, "Found 10Gb Twinax media.\n");
13537         sc->media = IFM_10G_TWINAX;
13538         port_type = PORT_DA;
13539         break;
13540     case ELINK_ETH_PHY_BASE_T:
13541         if (sc->link_params.speed_cap_mask[0] &
13542             PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13543             BLOGI(sc, "Found 10GBase-T media.\n");
13544             sc->media = IFM_10G_T;
13545             port_type = PORT_TP;
13546         } else {
13547             BLOGI(sc, "Found 1000Base-T media.\n");
13548             sc->media = IFM_1000_T;
13549             port_type = PORT_TP;
13550         }
13551         break;
13552     case ELINK_ETH_PHY_NOT_PRESENT:
13553         BLOGI(sc, "Media not present.\n");
13554         sc->media = 0;
13555         port_type = PORT_OTHER;
13556         break;
13557     case ELINK_ETH_PHY_UNSPECIFIED:
13558     default:
13559         BLOGI(sc, "Unknown media!\n");
13560         sc->media = 0;
13561         port_type = PORT_OTHER;
13562         break;
13563     }
13564     return port_type;
13565 }
13566 
13567 #define GET_FIELD(value, fname)                     \
13568     (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13569 #define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13570 #define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
13571 
13572 static int
13573 bxe_get_igu_cam_info(struct bxe_softc *sc)
13574 {
13575     int pfid = SC_FUNC(sc);
13576     int igu_sb_id;
13577     uint32_t val;
13578     uint8_t fid, igu_sb_cnt = 0;
13579 
13580     sc->igu_base_sb = 0xff;
13581 
13582     if (CHIP_INT_MODE_IS_BC(sc)) {
13583         int vn = SC_VN(sc);
13584         igu_sb_cnt = sc->igu_sb_cnt;
13585         sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
13586                            FP_SB_MAX_E1x);
13587         sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
13588                           (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
13589         return (0);
13590     }
13591 
13592     /* IGU in normal mode - read CAM */
13593     for (igu_sb_id = 0;
13594          igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
13595          igu_sb_id++) {
13596         val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
13597         if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
13598             continue;
13599         }
13600         fid = IGU_FID(val);
13601         if ((fid & IGU_FID_ENCODE_IS_PF)) {
13602             if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
13603                 continue;
13604             }
13605             if (IGU_VEC(val) == 0) {
13606                 /* default status block */
13607                 sc->igu_dsb_id = igu_sb_id;
13608             } else {
13609                 if (sc->igu_base_sb == 0xff) {
13610                     sc->igu_base_sb = igu_sb_id;
13611                 }
13612                 igu_sb_cnt++;
13613             }
13614         }
13615     }
13616 
13617     /*
13618      * Due to new PF resource allocation by MFW T7.4 and above, it's optional
13619      * that number of CAM entries will not be equal to the value advertised in
13620      * PCI. Driver should use the minimal value of both as the actual status
13621      * block count
13622      */
13623     sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
13624 
13625     if (igu_sb_cnt == 0) {
13626         BLOGE(sc, "CAM configuration error\n");
13627         return (-1);
13628     }
13629 
13630     return (0);
13631 }
13632 
13633 /*
13634  * Gather various information from the device config space, the device itself,
13635  * shmem, and the user input.
13636  */
13637 static int
13638 bxe_get_device_info(struct bxe_softc *sc)
13639 {
13640     uint32_t val;
13641     int rc;
13642 
13643     /* Get the data for the device */
13644     sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
13645     sc->devinfo.device_id    = pci_get_device(sc->dev);
13646     sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
13647     sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
13648 
13649     /* get the chip revision (chip metal comes from pci config space) */
13650     sc->devinfo.chip_id     =
13651     sc->link_params.chip_id =
13652         (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
13653          ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
13654          (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
13655          ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
13656 
13657     /* force 57811 according to MISC register */
13658     if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
13659         if (CHIP_IS_57810(sc)) {
13660             sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
13661                                    (sc->devinfo.chip_id & 0x0000ffff));
13662         } else if (CHIP_IS_57810_MF(sc)) {
13663             sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
13664                                    (sc->devinfo.chip_id & 0x0000ffff));
13665         }
13666         sc->devinfo.chip_id |= 0x1;
13667     }
13668 
13669     BLOGD(sc, DBG_LOAD,
13670           "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
13671           sc->devinfo.chip_id,
13672           ((sc->devinfo.chip_id >> 16) & 0xffff),
13673           ((sc->devinfo.chip_id >> 12) & 0xf),
13674           ((sc->devinfo.chip_id >>  4) & 0xff),
13675           ((sc->devinfo.chip_id >>  0) & 0xf));
13676 
13677     val = (REG_RD(sc, 0x2874) & 0x55);
13678     if ((sc->devinfo.chip_id & 0x1) ||
13679         (CHIP_IS_E1(sc) && val) ||
13680         (CHIP_IS_E1H(sc) && (val == 0x55))) {
13681         sc->flags |= BXE_ONE_PORT_FLAG;
13682         BLOGD(sc, DBG_LOAD, "single port device\n");
13683     }
13684 
13685     /* set the doorbell size */
13686     sc->doorbell_size = (1 << BXE_DB_SHIFT);
13687 
13688     /* determine whether the device is in 2 port or 4 port mode */
13689     sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
13690     if (CHIP_IS_E2E3(sc)) {
13691         /*
13692          * Read port4mode_en_ovwr[0]:
13693          *   If 1, four port mode is in port4mode_en_ovwr[1].
13694          *   If 0, four port mode is in port4mode_en[0].
13695          */
13696         val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
13697         if (val & 1) {
13698             val = ((val >> 1) & 1);
13699         } else {
13700             val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
13701         }
13702 
13703         sc->devinfo.chip_port_mode =
13704             (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
13705 
13706         BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
13707     }
13708 
13709     /* get the function and path info for the device */
13710     bxe_get_function_num(sc);
13711 
13712     /* get the shared memory base address */
13713     sc->devinfo.shmem_base     =
13714     sc->link_params.shmem_base =
13715         REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
13716     sc->devinfo.shmem2_base =
13717         REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
13718                                   MISC_REG_GENERIC_CR_0));
13719 
13720     BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
13721           sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
13722 
13723     if (!sc->devinfo.shmem_base) {
13724         /* this should ONLY prevent upcoming shmem reads */
13725         BLOGI(sc, "MCP not active\n");
13726         sc->flags |= BXE_NO_MCP_FLAG;
13727         return (0);
13728     }
13729 
13730     /* make sure the shared memory contents are valid */
13731     val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
13732     if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
13733         (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
13734         BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
13735         return (0);
13736     }
13737     BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
13738 
13739     /* get the bootcode version */
13740     sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
13741     snprintf(sc->devinfo.bc_ver_str,
13742              sizeof(sc->devinfo.bc_ver_str),
13743              "%d.%d.%d",
13744              ((sc->devinfo.bc_ver >> 24) & 0xff),
13745              ((sc->devinfo.bc_ver >> 16) & 0xff),
13746              ((sc->devinfo.bc_ver >>  8) & 0xff));
13747     BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
13748 
13749     /* get the bootcode shmem address */
13750     sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
13751     BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
13752 
13753     /* clean indirect addresses as they're not used */
13754     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
13755     if (IS_PF(sc)) {
13756         REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
13757         REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
13758         REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
13759         REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
13760         if (CHIP_IS_E1x(sc)) {
13761             REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
13762             REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
13763             REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
13764             REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
13765         }
13766 
13767         /*
13768          * Enable internal target-read (in case we are probed after PF
13769          * FLR). Must be done prior to any BAR read access. Only for
13770          * 57712 and up
13771          */
13772         if (!CHIP_IS_E1x(sc)) {
13773             REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
13774         }
13775     }
13776 
13777     /* get the nvram size */
13778     val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
13779     sc->devinfo.flash_size =
13780         (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
13781     BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
13782 
13783     /* get PCI capabilites */
13784     bxe_probe_pci_caps(sc);
13785 
13786     bxe_set_power_state(sc, PCI_PM_D0);
13787 
13788     /* get various configuration parameters from shmem */
13789     bxe_get_shmem_info(sc);
13790 
13791     if (sc->devinfo.pcie_msix_cap_reg != 0) {
13792         val = pci_read_config(sc->dev,
13793                               (sc->devinfo.pcie_msix_cap_reg +
13794                                PCIR_MSIX_CTRL),
13795                               2);
13796         sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
13797     } else {
13798         sc->igu_sb_cnt = 1;
13799     }
13800 
13801     sc->igu_base_addr = BAR_IGU_INTMEM;
13802 
13803     /* initialize IGU parameters */
13804     if (CHIP_IS_E1x(sc)) {
13805         sc->devinfo.int_block = INT_BLOCK_HC;
13806         sc->igu_dsb_id = DEF_SB_IGU_ID;
13807         sc->igu_base_sb = 0;
13808     } else {
13809         sc->devinfo.int_block = INT_BLOCK_IGU;
13810 
13811         /* do not allow device reset during IGU info preocessing */
13812         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13813 
13814         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
13815 
13816         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13817             int tout = 5000;
13818 
13819             BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
13820 
13821             val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
13822             REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
13823             REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
13824 
13825             while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13826                 tout--;
13827                 DELAY(1000);
13828             }
13829 
13830             if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13831                 BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
13832                 bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13833                 return (-1);
13834             }
13835         }
13836 
13837         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13838             BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
13839             sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
13840         } else {
13841             BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
13842         }
13843 
13844         rc = bxe_get_igu_cam_info(sc);
13845 
13846         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13847 
13848         if (rc) {
13849             return (rc);
13850         }
13851     }
13852 
13853     /*
13854      * Get base FW non-default (fast path) status block ID. This value is
13855      * used to initialize the fw_sb_id saved on the fp/queue structure to
13856      * determine the id used by the FW.
13857      */
13858     if (CHIP_IS_E1x(sc)) {
13859         sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
13860     } else {
13861         /*
13862          * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
13863          * the same queue are indicated on the same IGU SB). So we prefer
13864          * FW and IGU SBs to be the same value.
13865          */
13866         sc->base_fw_ndsb = sc->igu_base_sb;
13867     }
13868 
13869     BLOGD(sc, DBG_LOAD,
13870           "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
13871           sc->igu_dsb_id, sc->igu_base_sb,
13872           sc->igu_sb_cnt, sc->base_fw_ndsb);
13873 
13874     elink_phy_probe(&sc->link_params);
13875 
13876     return (0);
13877 }
13878 
13879 static void
13880 bxe_link_settings_supported(struct bxe_softc *sc,
13881                             uint32_t         switch_cfg)
13882 {
13883     uint32_t cfg_size = 0;
13884     uint32_t idx;
13885     uint8_t port = SC_PORT(sc);
13886 
13887     /* aggregation of supported attributes of all external phys */
13888     sc->port.supported[0] = 0;
13889     sc->port.supported[1] = 0;
13890 
13891     switch (sc->link_params.num_phys) {
13892     case 1:
13893         sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
13894         cfg_size = 1;
13895         break;
13896     case 2:
13897         sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
13898         cfg_size = 1;
13899         break;
13900     case 3:
13901         if (sc->link_params.multi_phy_config &
13902             PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
13903             sc->port.supported[1] =
13904                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
13905             sc->port.supported[0] =
13906                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
13907         } else {
13908             sc->port.supported[0] =
13909                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
13910             sc->port.supported[1] =
13911                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
13912         }
13913         cfg_size = 2;
13914         break;
13915     }
13916 
13917     if (!(sc->port.supported[0] || sc->port.supported[1])) {
13918         BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
13919               SHMEM_RD(sc,
13920                        dev_info.port_hw_config[port].external_phy_config),
13921               SHMEM_RD(sc,
13922                        dev_info.port_hw_config[port].external_phy_config2));
13923         return;
13924     }
13925 
13926     if (CHIP_IS_E3(sc))
13927         sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
13928     else {
13929         switch (switch_cfg) {
13930         case ELINK_SWITCH_CFG_1G:
13931             sc->port.phy_addr =
13932                 REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
13933             break;
13934         case ELINK_SWITCH_CFG_10G:
13935             sc->port.phy_addr =
13936                 REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
13937             break;
13938         default:
13939             BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
13940                   sc->port.link_config[0]);
13941             return;
13942         }
13943     }
13944 
13945     BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
13946 
13947     /* mask what we support according to speed_cap_mask per configuration */
13948     for (idx = 0; idx < cfg_size; idx++) {
13949         if (!(sc->link_params.speed_cap_mask[idx] &
13950               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
13951             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
13952         }
13953 
13954         if (!(sc->link_params.speed_cap_mask[idx] &
13955               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
13956             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
13957         }
13958 
13959         if (!(sc->link_params.speed_cap_mask[idx] &
13960               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
13961             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
13962         }
13963 
13964         if (!(sc->link_params.speed_cap_mask[idx] &
13965               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
13966             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
13967         }
13968 
13969         if (!(sc->link_params.speed_cap_mask[idx] &
13970               PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
13971             sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
13972         }
13973 
13974         if (!(sc->link_params.speed_cap_mask[idx] &
13975               PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
13976             sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
13977         }
13978 
13979         if (!(sc->link_params.speed_cap_mask[idx] &
13980               PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
13981             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
13982         }
13983 
13984         if (!(sc->link_params.speed_cap_mask[idx] &
13985               PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
13986             sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
13987         }
13988     }
13989 
13990     BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
13991           sc->port.supported[0], sc->port.supported[1]);
13992 }
13993 
13994 static void
13995 bxe_link_settings_requested(struct bxe_softc *sc)
13996 {
13997     uint32_t link_config;
13998     uint32_t idx;
13999     uint32_t cfg_size = 0;
14000 
14001     sc->port.advertising[0] = 0;
14002     sc->port.advertising[1] = 0;
14003 
14004     switch (sc->link_params.num_phys) {
14005     case 1:
14006     case 2:
14007         cfg_size = 1;
14008         break;
14009     case 3:
14010         cfg_size = 2;
14011         break;
14012     }
14013 
14014     for (idx = 0; idx < cfg_size; idx++) {
14015         sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14016         link_config = sc->port.link_config[idx];
14017 
14018         switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14019         case PORT_FEATURE_LINK_SPEED_AUTO:
14020             if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14021                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14022                 sc->port.advertising[idx] |= sc->port.supported[idx];
14023                 if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14024                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14025                     sc->port.advertising[idx] |=
14026                         (ELINK_SUPPORTED_100baseT_Half |
14027                          ELINK_SUPPORTED_100baseT_Full);
14028             } else {
14029                 /* force 10G, no AN */
14030                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14031                 sc->port.advertising[idx] |=
14032                     (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14033                 continue;
14034             }
14035             break;
14036 
14037         case PORT_FEATURE_LINK_SPEED_10M_FULL:
14038             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14039                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14040                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14041                                               ADVERTISED_TP);
14042             } else {
14043                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14044                           "speed_cap_mask=0x%08x\n",
14045                       link_config, sc->link_params.speed_cap_mask[idx]);
14046                 return;
14047             }
14048             break;
14049 
14050         case PORT_FEATURE_LINK_SPEED_10M_HALF:
14051             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14052                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14053                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14054                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14055                                               ADVERTISED_TP);
14056             } else {
14057                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14058                           "speed_cap_mask=0x%08x\n",
14059                       link_config, sc->link_params.speed_cap_mask[idx]);
14060                 return;
14061             }
14062             break;
14063 
14064         case PORT_FEATURE_LINK_SPEED_100M_FULL:
14065             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14066                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14067                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14068                                               ADVERTISED_TP);
14069             } else {
14070                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14071                           "speed_cap_mask=0x%08x\n",
14072                       link_config, sc->link_params.speed_cap_mask[idx]);
14073                 return;
14074             }
14075             break;
14076 
14077         case PORT_FEATURE_LINK_SPEED_100M_HALF:
14078             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14079                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14080                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14081                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14082                                               ADVERTISED_TP);
14083             } else {
14084                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14085                           "speed_cap_mask=0x%08x\n",
14086                       link_config, sc->link_params.speed_cap_mask[idx]);
14087                 return;
14088             }
14089             break;
14090 
14091         case PORT_FEATURE_LINK_SPEED_1G:
14092             if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14093                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14094                 sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14095                                               ADVERTISED_TP);
14096             } else {
14097                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14098                           "speed_cap_mask=0x%08x\n",
14099                       link_config, sc->link_params.speed_cap_mask[idx]);
14100                 return;
14101             }
14102             break;
14103 
14104         case PORT_FEATURE_LINK_SPEED_2_5G:
14105             if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14106                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14107                 sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14108                                               ADVERTISED_TP);
14109             } else {
14110                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14111                           "speed_cap_mask=0x%08x\n",
14112                       link_config, sc->link_params.speed_cap_mask[idx]);
14113                 return;
14114             }
14115             break;
14116 
14117         case PORT_FEATURE_LINK_SPEED_10G_CX4:
14118             if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14119                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14120                 sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14121                                               ADVERTISED_FIBRE);
14122             } else {
14123                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14124                           "speed_cap_mask=0x%08x\n",
14125                       link_config, sc->link_params.speed_cap_mask[idx]);
14126                 return;
14127             }
14128             break;
14129 
14130         case PORT_FEATURE_LINK_SPEED_20G:
14131             sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14132             break;
14133 
14134         default:
14135             BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14136                       "speed_cap_mask=0x%08x\n",
14137                   link_config, sc->link_params.speed_cap_mask[idx]);
14138             sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14139             sc->port.advertising[idx] = sc->port.supported[idx];
14140             break;
14141         }
14142 
14143         sc->link_params.req_flow_ctrl[idx] =
14144             (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14145 
14146         if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14147             if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14148                 sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14149             } else {
14150                 bxe_set_requested_fc(sc);
14151             }
14152         }
14153 
14154         BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14155                             "req_flow_ctrl=0x%x advertising=0x%x\n",
14156               sc->link_params.req_line_speed[idx],
14157               sc->link_params.req_duplex[idx],
14158               sc->link_params.req_flow_ctrl[idx],
14159               sc->port.advertising[idx]);
14160     }
14161 }
14162 
14163 static void
14164 bxe_get_phy_info(struct bxe_softc *sc)
14165 {
14166     uint8_t port = SC_PORT(sc);
14167     uint32_t config = sc->port.config;
14168     uint32_t eee_mode;
14169 
14170     /* shmem data already read in bxe_get_shmem_info() */
14171 
14172     BLOGD(sc, DBG_LOAD, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14173                         "link_config0=0x%08x\n",
14174                sc->link_params.lane_config,
14175                sc->link_params.speed_cap_mask[0],
14176                sc->port.link_config[0]);
14177 
14178     bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14179     bxe_link_settings_requested(sc);
14180 
14181     if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14182         sc->link_params.feature_config_flags |=
14183             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14184     } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14185         sc->link_params.feature_config_flags &=
14186             ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14187     } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14188         sc->link_params.feature_config_flags |=
14189             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14190     }
14191 
14192     /* configure link feature according to nvram value */
14193     eee_mode =
14194         (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14195           PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14196          PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14197     if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14198         sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14199                                     ELINK_EEE_MODE_ENABLE_LPI |
14200                                     ELINK_EEE_MODE_OUTPUT_TIME);
14201     } else {
14202         sc->link_params.eee_mode = 0;
14203     }
14204 
14205     /* get the media type */
14206     bxe_media_detect(sc);
14207 }
14208 
14209 static void
14210 bxe_get_params(struct bxe_softc *sc)
14211 {
14212     /* get user tunable params */
14213     bxe_get_tunable_params(sc);
14214 
14215     /* select the RX and TX ring sizes */
14216     sc->tx_ring_size = TX_BD_USABLE;
14217     sc->rx_ring_size = RX_BD_USABLE;
14218 
14219     /* XXX disable WoL */
14220     sc->wol = 0;
14221 }
14222 
14223 static void
14224 bxe_set_modes_bitmap(struct bxe_softc *sc)
14225 {
14226     uint32_t flags = 0;
14227 
14228     if (CHIP_REV_IS_FPGA(sc)) {
14229         SET_FLAGS(flags, MODE_FPGA);
14230     } else if (CHIP_REV_IS_EMUL(sc)) {
14231         SET_FLAGS(flags, MODE_EMUL);
14232     } else {
14233         SET_FLAGS(flags, MODE_ASIC);
14234     }
14235 
14236     if (CHIP_IS_MODE_4_PORT(sc)) {
14237         SET_FLAGS(flags, MODE_PORT4);
14238     } else {
14239         SET_FLAGS(flags, MODE_PORT2);
14240     }
14241 
14242     if (CHIP_IS_E2(sc)) {
14243         SET_FLAGS(flags, MODE_E2);
14244     } else if (CHIP_IS_E3(sc)) {
14245         SET_FLAGS(flags, MODE_E3);
14246         if (CHIP_REV(sc) == CHIP_REV_Ax) {
14247             SET_FLAGS(flags, MODE_E3_A0);
14248         } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14249             SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14250         }
14251     }
14252 
14253     if (IS_MF(sc)) {
14254         SET_FLAGS(flags, MODE_MF);
14255         switch (sc->devinfo.mf_info.mf_mode) {
14256         case MULTI_FUNCTION_SD:
14257             SET_FLAGS(flags, MODE_MF_SD);
14258             break;
14259         case MULTI_FUNCTION_SI:
14260             SET_FLAGS(flags, MODE_MF_SI);
14261             break;
14262         case MULTI_FUNCTION_AFEX:
14263             SET_FLAGS(flags, MODE_MF_AFEX);
14264             break;
14265         }
14266     } else {
14267         SET_FLAGS(flags, MODE_SF);
14268     }
14269 
14270 #if defined(__LITTLE_ENDIAN)
14271     SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14272 #else /* __BIG_ENDIAN */
14273     SET_FLAGS(flags, MODE_BIG_ENDIAN);
14274 #endif
14275 
14276     INIT_MODE_FLAGS(sc) = flags;
14277 }
14278 
14279 static int
14280 bxe_alloc_hsi_mem(struct bxe_softc *sc)
14281 {
14282     struct bxe_fastpath *fp;
14283     bus_addr_t busaddr;
14284     int max_agg_queues;
14285     int max_segments;
14286     bus_size_t max_size;
14287     bus_size_t max_seg_size;
14288     char buf[32];
14289     int rc;
14290     int i, j;
14291 
14292     /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14293 
14294     /* allocate the parent bus DMA tag */
14295     rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14296                             1,                        /* alignment */
14297                             0,                        /* boundary limit */
14298                             BUS_SPACE_MAXADDR,        /* restricted low */
14299                             BUS_SPACE_MAXADDR,        /* restricted hi */
14300                             NULL,                     /* addr filter() */
14301                             NULL,                     /* addr filter() arg */
14302                             BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14303                             BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14304                             BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14305                             0,                        /* flags */
14306                             NULL,                     /* lock() */
14307                             NULL,                     /* lock() arg */
14308                             &sc->parent_dma_tag);     /* returned dma tag */
14309     if (rc != 0) {
14310         BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14311         return (1);
14312     }
14313 
14314     /************************/
14315     /* DEFAULT STATUS BLOCK */
14316     /************************/
14317 
14318     if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14319                       &sc->def_sb_dma, "default status block") != 0) {
14320         /* XXX */
14321         bus_dma_tag_destroy(sc->parent_dma_tag);
14322         return (1);
14323     }
14324 
14325     sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14326 
14327     /***************/
14328     /* EVENT QUEUE */
14329     /***************/
14330 
14331     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14332                       &sc->eq_dma, "event queue") != 0) {
14333         /* XXX */
14334         bxe_dma_free(sc, &sc->def_sb_dma);
14335         sc->def_sb = NULL;
14336         bus_dma_tag_destroy(sc->parent_dma_tag);
14337         return (1);
14338     }
14339 
14340     sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14341 
14342     /*************/
14343     /* SLOW PATH */
14344     /*************/
14345 
14346     if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14347                       &sc->sp_dma, "slow path") != 0) {
14348         /* XXX */
14349         bxe_dma_free(sc, &sc->eq_dma);
14350         sc->eq = NULL;
14351         bxe_dma_free(sc, &sc->def_sb_dma);
14352         sc->def_sb = NULL;
14353         bus_dma_tag_destroy(sc->parent_dma_tag);
14354         return (1);
14355     }
14356 
14357     sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14358 
14359     /*******************/
14360     /* SLOW PATH QUEUE */
14361     /*******************/
14362 
14363     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14364                       &sc->spq_dma, "slow path queue") != 0) {
14365         /* XXX */
14366         bxe_dma_free(sc, &sc->sp_dma);
14367         sc->sp = NULL;
14368         bxe_dma_free(sc, &sc->eq_dma);
14369         sc->eq = NULL;
14370         bxe_dma_free(sc, &sc->def_sb_dma);
14371         sc->def_sb = NULL;
14372         bus_dma_tag_destroy(sc->parent_dma_tag);
14373         return (1);
14374     }
14375 
14376     sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14377 
14378     /***************************/
14379     /* FW DECOMPRESSION BUFFER */
14380     /***************************/
14381 
14382     if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14383                       "fw decompression buffer") != 0) {
14384         /* XXX */
14385         bxe_dma_free(sc, &sc->spq_dma);
14386         sc->spq = NULL;
14387         bxe_dma_free(sc, &sc->sp_dma);
14388         sc->sp = NULL;
14389         bxe_dma_free(sc, &sc->eq_dma);
14390         sc->eq = NULL;
14391         bxe_dma_free(sc, &sc->def_sb_dma);
14392         sc->def_sb = NULL;
14393         bus_dma_tag_destroy(sc->parent_dma_tag);
14394         return (1);
14395     }
14396 
14397     sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14398 
14399     if ((sc->gz_strm =
14400          malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14401         /* XXX */
14402         bxe_dma_free(sc, &sc->gz_buf_dma);
14403         sc->gz_buf = NULL;
14404         bxe_dma_free(sc, &sc->spq_dma);
14405         sc->spq = NULL;
14406         bxe_dma_free(sc, &sc->sp_dma);
14407         sc->sp = NULL;
14408         bxe_dma_free(sc, &sc->eq_dma);
14409         sc->eq = NULL;
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     /*************/
14417     /* FASTPATHS */
14418     /*************/
14419 
14420     /* allocate DMA memory for each fastpath structure */
14421     for (i = 0; i < sc->num_queues; i++) {
14422         fp = &sc->fp[i];
14423         fp->sc    = sc;
14424         fp->index = i;
14425 
14426         /*******************/
14427         /* FP STATUS BLOCK */
14428         /*******************/
14429 
14430         snprintf(buf, sizeof(buf), "fp %d status block", i);
14431         if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14432                           &fp->sb_dma, buf) != 0) {
14433             /* XXX unwind and free previous fastpath allocations */
14434             BLOGE(sc, "Failed to alloc %s\n", buf);
14435             return (1);
14436         } else {
14437             if (CHIP_IS_E2E3(sc)) {
14438                 fp->status_block.e2_sb =
14439                     (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14440             } else {
14441                 fp->status_block.e1x_sb =
14442                     (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14443             }
14444         }
14445 
14446         /******************/
14447         /* FP TX BD CHAIN */
14448         /******************/
14449 
14450         snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14451         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14452                           &fp->tx_dma, buf) != 0) {
14453             /* XXX unwind and free previous fastpath allocations */
14454             BLOGE(sc, "Failed to alloc %s\n", buf);
14455             return (1);
14456         } else {
14457             fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14458         }
14459 
14460         /* link together the tx bd chain pages */
14461         for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14462             /* index into the tx bd chain array to last entry per page */
14463             struct eth_tx_next_bd *tx_next_bd =
14464                 &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14465             /* point to the next page and wrap from last page */
14466             busaddr = (fp->tx_dma.paddr +
14467                        (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14468             tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14469             tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14470         }
14471 
14472         /******************/
14473         /* FP RX BD CHAIN */
14474         /******************/
14475 
14476         snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14477         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14478                           &fp->rx_dma, buf) != 0) {
14479             /* XXX unwind and free previous fastpath allocations */
14480             BLOGE(sc, "Failed to alloc %s\n", buf);
14481             return (1);
14482         } else {
14483             fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14484         }
14485 
14486         /* link together the rx bd chain pages */
14487         for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14488             /* index into the rx bd chain array to last entry per page */
14489             struct eth_rx_bd *rx_bd =
14490                 &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14491             /* point to the next page and wrap from last page */
14492             busaddr = (fp->rx_dma.paddr +
14493                        (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14494             rx_bd->addr_hi = htole32(U64_HI(busaddr));
14495             rx_bd->addr_lo = htole32(U64_LO(busaddr));
14496         }
14497 
14498         /*******************/
14499         /* FP RX RCQ CHAIN */
14500         /*******************/
14501 
14502         snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14503         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14504                           &fp->rcq_dma, buf) != 0) {
14505             /* XXX unwind and free previous fastpath allocations */
14506             BLOGE(sc, "Failed to alloc %s\n", buf);
14507             return (1);
14508         } else {
14509             fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14510         }
14511 
14512         /* link together the rcq chain pages */
14513         for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14514             /* index into the rcq chain array to last entry per page */
14515             struct eth_rx_cqe_next_page *rx_cqe_next =
14516                 (struct eth_rx_cqe_next_page *)
14517                 &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14518             /* point to the next page and wrap from last page */
14519             busaddr = (fp->rcq_dma.paddr +
14520                        (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14521             rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14522             rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14523         }
14524 
14525         /*******************/
14526         /* FP RX SGE CHAIN */
14527         /*******************/
14528 
14529         snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14530         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14531                           &fp->rx_sge_dma, buf) != 0) {
14532             /* XXX unwind and free previous fastpath allocations */
14533             BLOGE(sc, "Failed to alloc %s\n", buf);
14534             return (1);
14535         } else {
14536             fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14537         }
14538 
14539         /* link together the sge chain pages */
14540         for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14541             /* index into the rcq chain array to last entry per page */
14542             struct eth_rx_sge *rx_sge =
14543                 &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14544             /* point to the next page and wrap from last page */
14545             busaddr = (fp->rx_sge_dma.paddr +
14546                        (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14547             rx_sge->addr_hi = htole32(U64_HI(busaddr));
14548             rx_sge->addr_lo = htole32(U64_LO(busaddr));
14549         }
14550 
14551         /***********************/
14552         /* FP TX MBUF DMA MAPS */
14553         /***********************/
14554 
14555         /* set required sizes before mapping to conserve resources */
14556         if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
14557             max_size     = BXE_TSO_MAX_SIZE;
14558             max_segments = BXE_TSO_MAX_SEGMENTS;
14559             max_seg_size = BXE_TSO_MAX_SEG_SIZE;
14560         } else {
14561             max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
14562             max_segments = BXE_MAX_SEGMENTS;
14563             max_seg_size = MCLBYTES;
14564         }
14565 
14566         /* create a dma tag for the tx mbufs */
14567         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14568                                 1,                  /* alignment */
14569                                 0,                  /* boundary limit */
14570                                 BUS_SPACE_MAXADDR,  /* restricted low */
14571                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14572                                 NULL,               /* addr filter() */
14573                                 NULL,               /* addr filter() arg */
14574                                 max_size,           /* max map size */
14575                                 max_segments,       /* num discontinuous */
14576                                 max_seg_size,       /* max seg size */
14577                                 0,                  /* flags */
14578                                 NULL,               /* lock() */
14579                                 NULL,               /* lock() arg */
14580                                 &fp->tx_mbuf_tag);  /* returned dma tag */
14581         if (rc != 0) {
14582             /* XXX unwind and free previous fastpath allocations */
14583             BLOGE(sc, "Failed to create dma tag for "
14584                       "'fp %d tx mbufs' (%d)\n", i, rc);
14585             return (1);
14586         }
14587 
14588         /* create dma maps for each of the tx mbuf clusters */
14589         for (j = 0; j < TX_BD_TOTAL; j++) {
14590             if (bus_dmamap_create(fp->tx_mbuf_tag,
14591                                   BUS_DMA_NOWAIT,
14592                                   &fp->tx_mbuf_chain[j].m_map)) {
14593                 /* XXX unwind and free previous fastpath allocations */
14594                 BLOGE(sc, "Failed to create dma map for "
14595                           "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
14596                 return (1);
14597             }
14598         }
14599 
14600         /***********************/
14601         /* FP RX MBUF DMA MAPS */
14602         /***********************/
14603 
14604         /* create a dma tag for the rx mbufs */
14605         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14606                                 1,                  /* alignment */
14607                                 0,                  /* boundary limit */
14608                                 BUS_SPACE_MAXADDR,  /* restricted low */
14609                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14610                                 NULL,               /* addr filter() */
14611                                 NULL,               /* addr filter() arg */
14612                                 MJUM9BYTES,         /* max map size */
14613                                 1,                  /* num discontinuous */
14614                                 MJUM9BYTES,         /* max seg size */
14615                                 0,                  /* flags */
14616                                 NULL,               /* lock() */
14617                                 NULL,               /* lock() arg */
14618                                 &fp->rx_mbuf_tag);  /* returned dma tag */
14619         if (rc != 0) {
14620             /* XXX unwind and free previous fastpath allocations */
14621             BLOGE(sc, "Failed to create dma tag for "
14622                       "'fp %d rx mbufs' (%d)\n", i, rc);
14623             return (1);
14624         }
14625 
14626         /* create dma maps for each of the rx mbuf clusters */
14627         for (j = 0; j < RX_BD_TOTAL; j++) {
14628             if (bus_dmamap_create(fp->rx_mbuf_tag,
14629                                   BUS_DMA_NOWAIT,
14630                                   &fp->rx_mbuf_chain[j].m_map)) {
14631                 /* XXX unwind and free previous fastpath allocations */
14632                 BLOGE(sc, "Failed to create dma map for "
14633                           "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
14634                 return (1);
14635             }
14636         }
14637 
14638         /* create dma map for the spare rx mbuf cluster */
14639         if (bus_dmamap_create(fp->rx_mbuf_tag,
14640                               BUS_DMA_NOWAIT,
14641                               &fp->rx_mbuf_spare_map)) {
14642             /* XXX unwind and free previous fastpath allocations */
14643             BLOGE(sc, "Failed to create dma map for "
14644                       "'fp %d spare rx mbuf' (%d)\n", i, rc);
14645             return (1);
14646         }
14647 
14648         /***************************/
14649         /* FP RX SGE MBUF DMA MAPS */
14650         /***************************/
14651 
14652         /* create a dma tag for the rx sge mbufs */
14653         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14654                                 1,                  /* alignment */
14655                                 0,                  /* boundary limit */
14656                                 BUS_SPACE_MAXADDR,  /* restricted low */
14657                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14658                                 NULL,               /* addr filter() */
14659                                 NULL,               /* addr filter() arg */
14660                                 BCM_PAGE_SIZE,      /* max map size */
14661                                 1,                  /* num discontinuous */
14662                                 BCM_PAGE_SIZE,      /* max seg size */
14663                                 0,                  /* flags */
14664                                 NULL,               /* lock() */
14665                                 NULL,               /* lock() arg */
14666                                 &fp->rx_sge_mbuf_tag); /* returned dma tag */
14667         if (rc != 0) {
14668             /* XXX unwind and free previous fastpath allocations */
14669             BLOGE(sc, "Failed to create dma tag for "
14670                       "'fp %d rx sge mbufs' (%d)\n", i, rc);
14671             return (1);
14672         }
14673 
14674         /* create dma maps for the rx sge mbuf clusters */
14675         for (j = 0; j < RX_SGE_TOTAL; j++) {
14676             if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14677                                   BUS_DMA_NOWAIT,
14678                                   &fp->rx_sge_mbuf_chain[j].m_map)) {
14679                 /* XXX unwind and free previous fastpath allocations */
14680                 BLOGE(sc, "Failed to create dma map for "
14681                           "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
14682                 return (1);
14683             }
14684         }
14685 
14686         /* create dma map for the spare rx sge mbuf cluster */
14687         if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14688                               BUS_DMA_NOWAIT,
14689                               &fp->rx_sge_mbuf_spare_map)) {
14690             /* XXX unwind and free previous fastpath allocations */
14691             BLOGE(sc, "Failed to create dma map for "
14692                       "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
14693             return (1);
14694         }
14695 
14696         /***************************/
14697         /* FP RX TPA MBUF DMA MAPS */
14698         /***************************/
14699 
14700         /* create dma maps for the rx tpa mbuf clusters */
14701         max_agg_queues = MAX_AGG_QS(sc);
14702 
14703         for (j = 0; j < max_agg_queues; j++) {
14704             if (bus_dmamap_create(fp->rx_mbuf_tag,
14705                                   BUS_DMA_NOWAIT,
14706                                   &fp->rx_tpa_info[j].bd.m_map)) {
14707                 /* XXX unwind and free previous fastpath allocations */
14708                 BLOGE(sc, "Failed to create dma map for "
14709                           "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
14710                 return (1);
14711             }
14712         }
14713 
14714         /* create dma map for the spare rx tpa mbuf cluster */
14715         if (bus_dmamap_create(fp->rx_mbuf_tag,
14716                               BUS_DMA_NOWAIT,
14717                               &fp->rx_tpa_info_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 tpa mbuf' (%d)\n", i, rc);
14721             return (1);
14722         }
14723 
14724         bxe_init_sge_ring_bit_mask(fp);
14725     }
14726 
14727     return (0);
14728 }
14729 
14730 static void
14731 bxe_free_hsi_mem(struct bxe_softc *sc)
14732 {
14733     struct bxe_fastpath *fp;
14734     int max_agg_queues;
14735     int i, j;
14736 
14737     if (sc->parent_dma_tag == NULL) {
14738         return; /* assume nothing was allocated */
14739     }
14740 
14741     for (i = 0; i < sc->num_queues; i++) {
14742         fp = &sc->fp[i];
14743 
14744         /*******************/
14745         /* FP STATUS BLOCK */
14746         /*******************/
14747 
14748         bxe_dma_free(sc, &fp->sb_dma);
14749         memset(&fp->status_block, 0, sizeof(fp->status_block));
14750 
14751         /******************/
14752         /* FP TX BD CHAIN */
14753         /******************/
14754 
14755         bxe_dma_free(sc, &fp->tx_dma);
14756         fp->tx_chain = NULL;
14757 
14758         /******************/
14759         /* FP RX BD CHAIN */
14760         /******************/
14761 
14762         bxe_dma_free(sc, &fp->rx_dma);
14763         fp->rx_chain = NULL;
14764 
14765         /*******************/
14766         /* FP RX RCQ CHAIN */
14767         /*******************/
14768 
14769         bxe_dma_free(sc, &fp->rcq_dma);
14770         fp->rcq_chain = NULL;
14771 
14772         /*******************/
14773         /* FP RX SGE CHAIN */
14774         /*******************/
14775 
14776         bxe_dma_free(sc, &fp->rx_sge_dma);
14777         fp->rx_sge_chain = NULL;
14778 
14779         /***********************/
14780         /* FP TX MBUF DMA MAPS */
14781         /***********************/
14782 
14783         if (fp->tx_mbuf_tag != NULL) {
14784             for (j = 0; j < TX_BD_TOTAL; j++) {
14785                 if (fp->tx_mbuf_chain[j].m_map != NULL) {
14786                     bus_dmamap_unload(fp->tx_mbuf_tag,
14787                                       fp->tx_mbuf_chain[j].m_map);
14788                     bus_dmamap_destroy(fp->tx_mbuf_tag,
14789                                        fp->tx_mbuf_chain[j].m_map);
14790                 }
14791             }
14792 
14793             bus_dma_tag_destroy(fp->tx_mbuf_tag);
14794             fp->tx_mbuf_tag = NULL;
14795         }
14796 
14797         /***********************/
14798         /* FP RX MBUF DMA MAPS */
14799         /***********************/
14800 
14801         if (fp->rx_mbuf_tag != NULL) {
14802             for (j = 0; j < RX_BD_TOTAL; j++) {
14803                 if (fp->rx_mbuf_chain[j].m_map != NULL) {
14804                     bus_dmamap_unload(fp->rx_mbuf_tag,
14805                                       fp->rx_mbuf_chain[j].m_map);
14806                     bus_dmamap_destroy(fp->rx_mbuf_tag,
14807                                        fp->rx_mbuf_chain[j].m_map);
14808                 }
14809             }
14810 
14811             if (fp->rx_mbuf_spare_map != NULL) {
14812                 bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14813                 bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14814             }
14815 
14816             /***************************/
14817             /* FP RX TPA MBUF DMA MAPS */
14818             /***************************/
14819 
14820             max_agg_queues = MAX_AGG_QS(sc);
14821 
14822             for (j = 0; j < max_agg_queues; j++) {
14823                 if (fp->rx_tpa_info[j].bd.m_map != NULL) {
14824                     bus_dmamap_unload(fp->rx_mbuf_tag,
14825                                       fp->rx_tpa_info[j].bd.m_map);
14826                     bus_dmamap_destroy(fp->rx_mbuf_tag,
14827                                        fp->rx_tpa_info[j].bd.m_map);
14828                 }
14829             }
14830 
14831             if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
14832                 bus_dmamap_unload(fp->rx_mbuf_tag,
14833                                   fp->rx_tpa_info_mbuf_spare_map);
14834                 bus_dmamap_destroy(fp->rx_mbuf_tag,
14835                                    fp->rx_tpa_info_mbuf_spare_map);
14836             }
14837 
14838             bus_dma_tag_destroy(fp->rx_mbuf_tag);
14839             fp->rx_mbuf_tag = NULL;
14840         }
14841 
14842         /***************************/
14843         /* FP RX SGE MBUF DMA MAPS */
14844         /***************************/
14845 
14846         if (fp->rx_sge_mbuf_tag != NULL) {
14847             for (j = 0; j < RX_SGE_TOTAL; j++) {
14848                 if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
14849                     bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14850                                       fp->rx_sge_mbuf_chain[j].m_map);
14851                     bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14852                                        fp->rx_sge_mbuf_chain[j].m_map);
14853                 }
14854             }
14855 
14856             if (fp->rx_sge_mbuf_spare_map != NULL) {
14857                 bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14858                                   fp->rx_sge_mbuf_spare_map);
14859                 bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
14860                                    fp->rx_sge_mbuf_spare_map);
14861             }
14862 
14863             bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
14864             fp->rx_sge_mbuf_tag = NULL;
14865         }
14866     }
14867 
14868     /***************************/
14869     /* FW DECOMPRESSION BUFFER */
14870     /***************************/
14871 
14872     bxe_dma_free(sc, &sc->gz_buf_dma);
14873     sc->gz_buf = NULL;
14874     free(sc->gz_strm, M_DEVBUF);
14875     sc->gz_strm = NULL;
14876 
14877     /*******************/
14878     /* SLOW PATH QUEUE */
14879     /*******************/
14880 
14881     bxe_dma_free(sc, &sc->spq_dma);
14882     sc->spq = NULL;
14883 
14884     /*************/
14885     /* SLOW PATH */
14886     /*************/
14887 
14888     bxe_dma_free(sc, &sc->sp_dma);
14889     sc->sp = NULL;
14890 
14891     /***************/
14892     /* EVENT QUEUE */
14893     /***************/
14894 
14895     bxe_dma_free(sc, &sc->eq_dma);
14896     sc->eq = NULL;
14897 
14898     /************************/
14899     /* DEFAULT STATUS BLOCK */
14900     /************************/
14901 
14902     bxe_dma_free(sc, &sc->def_sb_dma);
14903     sc->def_sb = NULL;
14904 
14905     bus_dma_tag_destroy(sc->parent_dma_tag);
14906     sc->parent_dma_tag = NULL;
14907 }
14908 
14909 /*
14910  * Previous driver DMAE transaction may have occurred when pre-boot stage
14911  * ended and boot began. This would invalidate the addresses of the
14912  * transaction, resulting in was-error bit set in the PCI causing all
14913  * hw-to-host PCIe transactions to timeout. If this happened we want to clear
14914  * the interrupt which detected this from the pglueb and the was-done bit
14915  */
14916 static void
14917 bxe_prev_interrupted_dmae(struct bxe_softc *sc)
14918 {
14919     uint32_t val;
14920 
14921     if (!CHIP_IS_E1x(sc)) {
14922         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
14923         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
14924             BLOGD(sc, DBG_LOAD,
14925                   "Clearing 'was-error' bit that was set in pglueb");
14926             REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
14927         }
14928     }
14929 }
14930 
14931 static int
14932 bxe_prev_mcp_done(struct bxe_softc *sc)
14933 {
14934     uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
14935                                  DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
14936     if (!rc) {
14937         BLOGE(sc, "MCP response failure, aborting\n");
14938         return (-1);
14939     }
14940 
14941     return (0);
14942 }
14943 
14944 static struct bxe_prev_list_node *
14945 bxe_prev_path_get_entry(struct bxe_softc *sc)
14946 {
14947     struct bxe_prev_list_node *tmp;
14948 
14949     LIST_FOREACH(tmp, &bxe_prev_list, node) {
14950         if ((sc->pcie_bus == tmp->bus) &&
14951             (sc->pcie_device == tmp->slot) &&
14952             (SC_PATH(sc) == tmp->path)) {
14953             return (tmp);
14954         }
14955     }
14956 
14957     return (NULL);
14958 }
14959 
14960 static uint8_t
14961 bxe_prev_is_path_marked(struct bxe_softc *sc)
14962 {
14963     struct bxe_prev_list_node *tmp;
14964     int rc = FALSE;
14965 
14966     mtx_lock(&bxe_prev_mtx);
14967 
14968     tmp = bxe_prev_path_get_entry(sc);
14969     if (tmp) {
14970         if (tmp->aer) {
14971             BLOGD(sc, DBG_LOAD,
14972                   "Path %d/%d/%d was marked by AER\n",
14973                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
14974         } else {
14975             rc = TRUE;
14976             BLOGD(sc, DBG_LOAD,
14977                   "Path %d/%d/%d was already cleaned from previous drivers\n",
14978                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
14979         }
14980     }
14981 
14982     mtx_unlock(&bxe_prev_mtx);
14983 
14984     return (rc);
14985 }
14986 
14987 static int
14988 bxe_prev_mark_path(struct bxe_softc *sc,
14989                    uint8_t          after_undi)
14990 {
14991     struct bxe_prev_list_node *tmp;
14992 
14993     mtx_lock(&bxe_prev_mtx);
14994 
14995     /* Check whether the entry for this path already exists */
14996     tmp = bxe_prev_path_get_entry(sc);
14997     if (tmp) {
14998         if (!tmp->aer) {
14999             BLOGD(sc, DBG_LOAD,
15000                   "Re-marking AER in path %d/%d/%d\n",
15001                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15002         } else {
15003             BLOGD(sc, DBG_LOAD,
15004                   "Removing AER indication from path %d/%d/%d\n",
15005                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15006             tmp->aer = 0;
15007         }
15008 
15009         mtx_unlock(&bxe_prev_mtx);
15010         return (0);
15011     }
15012 
15013     mtx_unlock(&bxe_prev_mtx);
15014 
15015     /* Create an entry for this path and add it */
15016     tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15017                  (M_NOWAIT | M_ZERO));
15018     if (!tmp) {
15019         BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15020         return (-1);
15021     }
15022 
15023     tmp->bus  = sc->pcie_bus;
15024     tmp->slot = sc->pcie_device;
15025     tmp->path = SC_PATH(sc);
15026     tmp->aer  = 0;
15027     tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15028 
15029     mtx_lock(&bxe_prev_mtx);
15030 
15031     BLOGD(sc, DBG_LOAD,
15032           "Marked path %d/%d/%d - finished previous unload\n",
15033           sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15034     LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15035 
15036     mtx_unlock(&bxe_prev_mtx);
15037 
15038     return (0);
15039 }
15040 
15041 static int
15042 bxe_do_flr(struct bxe_softc *sc)
15043 {
15044     int i;
15045 
15046     /* only E2 and onwards support FLR */
15047     if (CHIP_IS_E1x(sc)) {
15048         BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15049         return (-1);
15050     }
15051 
15052     /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15053     if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15054         BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15055               sc->devinfo.bc_ver);
15056         return (-1);
15057     }
15058 
15059     /* Wait for Transaction Pending bit clean */
15060     for (i = 0; i < 4; i++) {
15061         if (i) {
15062             DELAY(((1 << (i - 1)) * 100) * 1000);
15063         }
15064 
15065         if (!bxe_is_pcie_pending(sc)) {
15066             goto clear;
15067         }
15068     }
15069 
15070     BLOGE(sc, "PCIE transaction is not cleared, "
15071               "proceeding with reset anyway\n");
15072 
15073 clear:
15074 
15075     BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15076     bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15077 
15078     return (0);
15079 }
15080 
15081 struct bxe_mac_vals {
15082     uint32_t xmac_addr;
15083     uint32_t xmac_val;
15084     uint32_t emac_addr;
15085     uint32_t emac_val;
15086     uint32_t umac_addr;
15087     uint32_t umac_val;
15088     uint32_t bmac_addr;
15089     uint32_t bmac_val[2];
15090 };
15091 
15092 static void
15093 bxe_prev_unload_close_mac(struct bxe_softc *sc,
15094                           struct bxe_mac_vals *vals)
15095 {
15096     uint32_t val, base_addr, offset, mask, reset_reg;
15097     uint8_t mac_stopped = FALSE;
15098     uint8_t port = SC_PORT(sc);
15099     uint32_t wb_data[2];
15100 
15101     /* reset addresses as they also mark which values were changed */
15102     vals->bmac_addr = 0;
15103     vals->umac_addr = 0;
15104     vals->xmac_addr = 0;
15105     vals->emac_addr = 0;
15106 
15107     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15108 
15109     if (!CHIP_IS_E3(sc)) {
15110         val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15111         mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15112         if ((mask & reset_reg) && val) {
15113             BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15114             base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15115                                     : NIG_REG_INGRESS_BMAC0_MEM;
15116             offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15117                                     : BIGMAC_REGISTER_BMAC_CONTROL;
15118 
15119             /*
15120              * use rd/wr since we cannot use dmae. This is safe
15121              * since MCP won't access the bus due to the request
15122              * to unload, and no function on the path can be
15123              * loaded at this time.
15124              */
15125             wb_data[0] = REG_RD(sc, base_addr + offset);
15126             wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15127             vals->bmac_addr = base_addr + offset;
15128             vals->bmac_val[0] = wb_data[0];
15129             vals->bmac_val[1] = wb_data[1];
15130             wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15131             REG_WR(sc, vals->bmac_addr, wb_data[0]);
15132             REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15133         }
15134 
15135         BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15136         vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15137         vals->emac_val = REG_RD(sc, vals->emac_addr);
15138         REG_WR(sc, vals->emac_addr, 0);
15139         mac_stopped = TRUE;
15140     } else {
15141         if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15142             BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15143             base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15144             val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15145             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15146             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15147             vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15148             vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15149             REG_WR(sc, vals->xmac_addr, 0);
15150             mac_stopped = TRUE;
15151         }
15152 
15153         mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15154         if (mask & reset_reg) {
15155             BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15156             base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15157             vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15158             vals->umac_val = REG_RD(sc, vals->umac_addr);
15159             REG_WR(sc, vals->umac_addr, 0);
15160             mac_stopped = TRUE;
15161         }
15162     }
15163 
15164     if (mac_stopped) {
15165         DELAY(20000);
15166     }
15167 }
15168 
15169 #define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15170 #define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15171 #define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15172 #define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15173 
15174 static void
15175 bxe_prev_unload_undi_inc(struct bxe_softc *sc,
15176                          uint8_t          port,
15177                          uint8_t          inc)
15178 {
15179     uint16_t rcq, bd;
15180     uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15181 
15182     rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15183     bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15184 
15185     tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15186     REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15187 
15188     BLOGD(sc, DBG_LOAD,
15189           "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15190           port, bd, rcq);
15191 }
15192 
15193 static int
15194 bxe_prev_unload_common(struct bxe_softc *sc)
15195 {
15196     uint32_t reset_reg, tmp_reg = 0, rc;
15197     uint8_t prev_undi = FALSE;
15198     struct bxe_mac_vals mac_vals;
15199     uint32_t timer_count = 1000;
15200     uint32_t prev_brb;
15201 
15202     /*
15203      * It is possible a previous function received 'common' answer,
15204      * but hasn't loaded yet, therefore creating a scenario of
15205      * multiple functions receiving 'common' on the same path.
15206      */
15207     BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15208 
15209     memset(&mac_vals, 0, sizeof(mac_vals));
15210 
15211     if (bxe_prev_is_path_marked(sc)) {
15212         return (bxe_prev_mcp_done(sc));
15213     }
15214 
15215     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15216 
15217     /* Reset should be performed after BRB is emptied */
15218     if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15219         /* Close the MAC Rx to prevent BRB from filling up */
15220         bxe_prev_unload_close_mac(sc, &mac_vals);
15221 
15222         /* close LLH filters towards the BRB */
15223         elink_set_rx_filter(&sc->link_params, 0);
15224 
15225         /*
15226          * Check if the UNDI driver was previously loaded.
15227          * UNDI driver initializes CID offset for normal bell to 0x7
15228          */
15229         if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15230             tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15231             if (tmp_reg == 0x7) {
15232                 BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15233                 prev_undi = TRUE;
15234                 /* clear the UNDI indication */
15235                 REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15236                 /* clear possible idle check errors */
15237                 REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15238             }
15239         }
15240 
15241         /* wait until BRB is empty */
15242         tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15243         while (timer_count) {
15244             prev_brb = tmp_reg;
15245 
15246             tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15247             if (!tmp_reg) {
15248                 break;
15249             }
15250 
15251             BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15252 
15253             /* reset timer as long as BRB actually gets emptied */
15254             if (prev_brb > tmp_reg) {
15255                 timer_count = 1000;
15256             } else {
15257                 timer_count--;
15258             }
15259 
15260             /* If UNDI resides in memory, manually increment it */
15261             if (prev_undi) {
15262                 bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15263             }
15264 
15265             DELAY(10);
15266         }
15267 
15268         if (!timer_count) {
15269             BLOGE(sc, "Failed to empty BRB\n");
15270         }
15271     }
15272 
15273     /* No packets are in the pipeline, path is ready for reset */
15274     bxe_reset_common(sc);
15275 
15276     if (mac_vals.xmac_addr) {
15277         REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15278     }
15279     if (mac_vals.umac_addr) {
15280         REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15281     }
15282     if (mac_vals.emac_addr) {
15283         REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15284     }
15285     if (mac_vals.bmac_addr) {
15286         REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15287         REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15288     }
15289 
15290     rc = bxe_prev_mark_path(sc, prev_undi);
15291     if (rc) {
15292         bxe_prev_mcp_done(sc);
15293         return (rc);
15294     }
15295 
15296     return (bxe_prev_mcp_done(sc));
15297 }
15298 
15299 static int
15300 bxe_prev_unload_uncommon(struct bxe_softc *sc)
15301 {
15302     int rc;
15303 
15304     BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15305 
15306     /* Test if previous unload process was already finished for this path */
15307     if (bxe_prev_is_path_marked(sc)) {
15308         return (bxe_prev_mcp_done(sc));
15309     }
15310 
15311     BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15312 
15313     /*
15314      * If function has FLR capabilities, and existing FW version matches
15315      * the one required, then FLR will be sufficient to clean any residue
15316      * left by previous driver
15317      */
15318     rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15319     if (!rc) {
15320         /* fw version is good */
15321         BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15322         rc = bxe_do_flr(sc);
15323     }
15324 
15325     if (!rc) {
15326         /* FLR was performed */
15327         BLOGD(sc, DBG_LOAD, "FLR successful\n");
15328         return (0);
15329     }
15330 
15331     BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15332 
15333     /* Close the MCP request, return failure*/
15334     rc = bxe_prev_mcp_done(sc);
15335     if (!rc) {
15336         rc = BXE_PREV_WAIT_NEEDED;
15337     }
15338 
15339     return (rc);
15340 }
15341 
15342 static int
15343 bxe_prev_unload(struct bxe_softc *sc)
15344 {
15345     int time_counter = 10;
15346     uint32_t fw, hw_lock_reg, hw_lock_val;
15347     uint32_t rc = 0;
15348 
15349     /*
15350      * Clear HW from errors which may have resulted from an interrupted
15351      * DMAE transaction.
15352      */
15353     bxe_prev_interrupted_dmae(sc);
15354 
15355     /* Release previously held locks */
15356     hw_lock_reg =
15357         (SC_FUNC(sc) <= 5) ?
15358             (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15359             (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15360 
15361     hw_lock_val = (REG_RD(sc, hw_lock_reg));
15362     if (hw_lock_val) {
15363         if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15364             BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15365             REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15366                    (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15367         }
15368         BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15369         REG_WR(sc, hw_lock_reg, 0xffffffff);
15370     } else {
15371         BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15372     }
15373 
15374     if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15375         BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15376         REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15377     }
15378 
15379     do {
15380         /* Lock MCP using an unload request */
15381         fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15382         if (!fw) {
15383             BLOGE(sc, "MCP response failure, aborting\n");
15384             rc = -1;
15385             break;
15386         }
15387 
15388         if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15389             rc = bxe_prev_unload_common(sc);
15390             break;
15391         }
15392 
15393         /* non-common reply from MCP night require looping */
15394         rc = bxe_prev_unload_uncommon(sc);
15395         if (rc != BXE_PREV_WAIT_NEEDED) {
15396             break;
15397         }
15398 
15399         DELAY(20000);
15400     } while (--time_counter);
15401 
15402     if (!time_counter || rc) {
15403         BLOGE(sc, "Failed to unload previous driver!"
15404             " time_counter %d rc %d\n", time_counter, rc);
15405         rc = -1;
15406     }
15407 
15408     return (rc);
15409 }
15410 
15411 void
15412 bxe_dcbx_set_state(struct bxe_softc *sc,
15413                    uint8_t          dcb_on,
15414                    uint32_t         dcbx_enabled)
15415 {
15416     if (!CHIP_IS_E1x(sc)) {
15417         sc->dcb_state = dcb_on;
15418         sc->dcbx_enabled = dcbx_enabled;
15419     } else {
15420         sc->dcb_state = FALSE;
15421         sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15422     }
15423     BLOGD(sc, DBG_LOAD,
15424           "DCB state [%s:%s]\n",
15425           dcb_on ? "ON" : "OFF",
15426           (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15427           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15428           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15429           "on-chip with negotiation" : "invalid");
15430 }
15431 
15432 /* must be called after sriov-enable */
15433 static int
15434 bxe_set_qm_cid_count(struct bxe_softc *sc)
15435 {
15436     int cid_count = BXE_L2_MAX_CID(sc);
15437 
15438     if (IS_SRIOV(sc)) {
15439         cid_count += BXE_VF_CIDS;
15440     }
15441 
15442     if (CNIC_SUPPORT(sc)) {
15443         cid_count += CNIC_CID_MAX;
15444     }
15445 
15446     return (roundup(cid_count, QM_CID_ROUND));
15447 }
15448 
15449 static void
15450 bxe_init_multi_cos(struct bxe_softc *sc)
15451 {
15452     int pri, cos;
15453 
15454     uint32_t pri_map = 0; /* XXX change to user config */
15455 
15456     for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15457         cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15458         if (cos < sc->max_cos) {
15459             sc->prio_to_cos[pri] = cos;
15460         } else {
15461             BLOGW(sc, "Invalid COS %d for priority %d "
15462                       "(max COS is %d), setting to 0\n",
15463                   cos, pri, (sc->max_cos - 1));
15464             sc->prio_to_cos[pri] = 0;
15465         }
15466     }
15467 }
15468 
15469 static int
15470 bxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15471 {
15472     struct bxe_softc *sc;
15473     int error, result;
15474 
15475     result = 0;
15476     error = sysctl_handle_int(oidp, &result, 0, req);
15477 
15478     if (error || !req->newptr) {
15479         return (error);
15480     }
15481 
15482     if (result == 1) {
15483         uint32_t  temp;
15484         sc = (struct bxe_softc *)arg1;
15485 
15486         BLOGI(sc, "... dumping driver state ...\n");
15487         temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15488         BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15489     }
15490 
15491     return (error);
15492 }
15493 
15494 static int
15495 bxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15496 {
15497     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15498     uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15499     uint32_t *offset;
15500     uint64_t value = 0;
15501     int index = (int)arg2;
15502 
15503     if (index >= BXE_NUM_ETH_STATS) {
15504         BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15505         return (-1);
15506     }
15507 
15508     offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15509 
15510     switch (bxe_eth_stats_arr[index].size) {
15511     case 4:
15512         value = (uint64_t)*offset;
15513         break;
15514     case 8:
15515         value = HILO_U64(*offset, *(offset + 1));
15516         break;
15517     default:
15518         BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15519               index, bxe_eth_stats_arr[index].size);
15520         return (-1);
15521     }
15522 
15523     return (sysctl_handle_64(oidp, &value, 0, req));
15524 }
15525 
15526 static int
15527 bxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15528 {
15529     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15530     uint32_t *eth_stats;
15531     uint32_t *offset;
15532     uint64_t value = 0;
15533     uint32_t q_stat = (uint32_t)arg2;
15534     uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15535     uint32_t index = (q_stat & 0xffff);
15536 
15537     eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15538 
15539     if (index >= BXE_NUM_ETH_Q_STATS) {
15540         BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15541         return (-1);
15542     }
15543 
15544     offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15545 
15546     switch (bxe_eth_q_stats_arr[index].size) {
15547     case 4:
15548         value = (uint64_t)*offset;
15549         break;
15550     case 8:
15551         value = HILO_U64(*offset, *(offset + 1));
15552         break;
15553     default:
15554         BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15555               index, bxe_eth_q_stats_arr[index].size);
15556         return (-1);
15557     }
15558 
15559     return (sysctl_handle_64(oidp, &value, 0, req));
15560 }
15561 
15562 static void
15563 bxe_add_sysctls(struct bxe_softc *sc)
15564 {
15565     struct sysctl_ctx_list *ctx;
15566     struct sysctl_oid_list *children;
15567     struct sysctl_oid *queue_top, *queue;
15568     struct sysctl_oid_list *queue_top_children, *queue_children;
15569     char queue_num_buf[32];
15570     uint32_t q_stat;
15571     int i, j;
15572 
15573     ctx = device_get_sysctl_ctx(sc->dev);
15574     children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15575 
15576     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
15577                       CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
15578                       "version");
15579 
15580     snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
15581              BCM_5710_FW_MAJOR_VERSION,
15582              BCM_5710_FW_MINOR_VERSION,
15583              BCM_5710_FW_REVISION_VERSION,
15584              BCM_5710_FW_ENGINEERING_VERSION);
15585 
15586     snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
15587         ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
15588          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
15589          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
15590          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
15591                                                                 "Unknown"));
15592     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
15593                     CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
15594                     "multifunction vnics per port");
15595 
15596     snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
15597         ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
15598          (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
15599          (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
15600                                               "???GT/s"),
15601         sc->devinfo.pcie_link_width);
15602 
15603     sc->debug = bxe_debug;
15604 
15605 #if __FreeBSD_version >= 900000
15606     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15607                       CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
15608                       "bootcode version");
15609     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15610                       CTLFLAG_RD, sc->fw_ver_str, 0,
15611                       "firmware version");
15612     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15613                       CTLFLAG_RD, sc->mf_mode_str, 0,
15614                       "multifunction mode");
15615     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15616                       CTLFLAG_RD, sc->mac_addr_str, 0,
15617                       "mac address");
15618     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15619                       CTLFLAG_RD, sc->pci_link_str, 0,
15620                       "pci link status");
15621     SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
15622                     CTLFLAG_RW, &sc->debug,
15623                     "debug logging mode");
15624 #else
15625     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15626                       CTLFLAG_RD, &sc->devinfo.bc_ver_str, 0,
15627                       "bootcode version");
15628     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15629                       CTLFLAG_RD, &sc->fw_ver_str, 0,
15630                       "firmware version");
15631     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15632                       CTLFLAG_RD, &sc->mf_mode_str, 0,
15633                       "multifunction mode");
15634     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15635                       CTLFLAG_RD, &sc->mac_addr_str, 0,
15636                       "mac address");
15637     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15638                       CTLFLAG_RD, &sc->pci_link_str, 0,
15639                       "pci link status");
15640     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "debug",
15641                     CTLFLAG_RW, &sc->debug, 0,
15642                     "debug logging mode");
15643 #endif /* #if __FreeBSD_version >= 900000 */
15644 
15645     sc->trigger_grcdump = 0;
15646     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "trigger_grcdump",
15647                    CTLFLAG_RW, &sc->trigger_grcdump, 0,
15648                    "trigger grcdump should be invoked"
15649                    "  before collecting grcdump");
15650 
15651     sc->grcdump_started = 0;
15652     sc->grcdump_done = 0;
15653     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
15654                    CTLFLAG_RD, &sc->grcdump_done, 0,
15655                    "set by driver when grcdump is done");
15656 
15657     sc->rx_budget = bxe_rx_budget;
15658     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
15659                     CTLFLAG_RW, &sc->rx_budget, 0,
15660                     "rx processing budget");
15661 
15662     SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
15663                     CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15664                     bxe_sysctl_state, "IU", "dump driver state");
15665 
15666     for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
15667         SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
15668                         bxe_eth_stats_arr[i].string,
15669                         CTLTYPE_U64 | CTLFLAG_RD, sc, i,
15670                         bxe_sysctl_eth_stat, "LU",
15671                         bxe_eth_stats_arr[i].string);
15672     }
15673 
15674     /* add a new parent node for all queues "dev.bxe.#.queue" */
15675     queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
15676                                 CTLFLAG_RD, NULL, "queue");
15677     queue_top_children = SYSCTL_CHILDREN(queue_top);
15678 
15679     for (i = 0; i < sc->num_queues; i++) {
15680         /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
15681         snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
15682         queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
15683                                 queue_num_buf, CTLFLAG_RD, NULL,
15684                                 "single queue");
15685         queue_children = SYSCTL_CHILDREN(queue);
15686 
15687         for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
15688             q_stat = ((i << 16) | j);
15689             SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
15690                             bxe_eth_q_stats_arr[j].string,
15691                             CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
15692                             bxe_sysctl_eth_q_stat, "LU",
15693                             bxe_eth_q_stats_arr[j].string);
15694         }
15695     }
15696 }
15697 
15698 static int
15699 bxe_alloc_buf_rings(struct bxe_softc *sc)
15700 {
15701 #if __FreeBSD_version >= 800000
15702 
15703     int i;
15704     struct bxe_fastpath *fp;
15705 
15706     for (i = 0; i < sc->num_queues; i++) {
15707 
15708         fp = &sc->fp[i];
15709 
15710         fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
15711                                    M_NOWAIT, &fp->tx_mtx);
15712         if (fp->tx_br == NULL)
15713             return (-1);
15714     }
15715 #endif
15716     return (0);
15717 }
15718 
15719 static void
15720 bxe_free_buf_rings(struct bxe_softc *sc)
15721 {
15722 #if __FreeBSD_version >= 800000
15723 
15724     int i;
15725     struct bxe_fastpath *fp;
15726 
15727     for (i = 0; i < sc->num_queues; i++) {
15728 
15729         fp = &sc->fp[i];
15730 
15731         if (fp->tx_br) {
15732             buf_ring_free(fp->tx_br, M_DEVBUF);
15733             fp->tx_br = NULL;
15734         }
15735     }
15736 
15737 #endif
15738 }
15739 
15740 static void
15741 bxe_init_fp_mutexs(struct bxe_softc *sc)
15742 {
15743     int i;
15744     struct bxe_fastpath *fp;
15745 
15746     for (i = 0; i < sc->num_queues; i++) {
15747 
15748         fp = &sc->fp[i];
15749 
15750         snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
15751             "bxe%d_fp%d_tx_lock", sc->unit, i);
15752         mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
15753 
15754         snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
15755             "bxe%d_fp%d_rx_lock", sc->unit, i);
15756         mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
15757     }
15758 }
15759 
15760 static void
15761 bxe_destroy_fp_mutexs(struct bxe_softc *sc)
15762 {
15763     int i;
15764     struct bxe_fastpath *fp;
15765 
15766     for (i = 0; i < sc->num_queues; i++) {
15767 
15768         fp = &sc->fp[i];
15769 
15770         if (mtx_initialized(&fp->tx_mtx)) {
15771             mtx_destroy(&fp->tx_mtx);
15772         }
15773 
15774         if (mtx_initialized(&fp->rx_mtx)) {
15775             mtx_destroy(&fp->rx_mtx);
15776         }
15777     }
15778 }
15779 
15780 
15781 /*
15782  * Device attach function.
15783  *
15784  * Allocates device resources, performs secondary chip identification, and
15785  * initializes driver instance variables. This function is called from driver
15786  * load after a successful probe.
15787  *
15788  * Returns:
15789  *   0 = Success, >0 = Failure
15790  */
15791 static int
15792 bxe_attach(device_t dev)
15793 {
15794     struct bxe_softc *sc;
15795 
15796     sc = device_get_softc(dev);
15797 
15798     BLOGD(sc, DBG_LOAD, "Starting attach...\n");
15799 
15800     sc->state = BXE_STATE_CLOSED;
15801 
15802     sc->dev  = dev;
15803     sc->unit = device_get_unit(dev);
15804 
15805     BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
15806 
15807     sc->pcie_bus    = pci_get_bus(dev);
15808     sc->pcie_device = pci_get_slot(dev);
15809     sc->pcie_func   = pci_get_function(dev);
15810 
15811     /* enable bus master capability */
15812     pci_enable_busmaster(dev);
15813 
15814     /* get the BARs */
15815     if (bxe_allocate_bars(sc) != 0) {
15816         return (ENXIO);
15817     }
15818 
15819     /* initialize the mutexes */
15820     bxe_init_mutexes(sc);
15821 
15822     /* prepare the periodic callout */
15823     callout_init(&sc->periodic_callout, 0);
15824 
15825     /* prepare the chip taskqueue */
15826     sc->chip_tq_flags = CHIP_TQ_NONE;
15827     snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
15828              "bxe%d_chip_tq", sc->unit);
15829     TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
15830     sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
15831                                    taskqueue_thread_enqueue,
15832                                    &sc->chip_tq);
15833     taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
15834                             "%s", sc->chip_tq_name);
15835 
15836     /* get device info and set params */
15837     if (bxe_get_device_info(sc) != 0) {
15838         BLOGE(sc, "getting device info\n");
15839         bxe_deallocate_bars(sc);
15840         pci_disable_busmaster(dev);
15841         return (ENXIO);
15842     }
15843 
15844     /* get final misc params */
15845     bxe_get_params(sc);
15846 
15847     /* set the default MTU (changed via ifconfig) */
15848     sc->mtu = ETHERMTU;
15849 
15850     bxe_set_modes_bitmap(sc);
15851 
15852     /* XXX
15853      * If in AFEX mode and the function is configured for FCoE
15854      * then bail... no L2 allowed.
15855      */
15856 
15857     /* get phy settings from shmem and 'and' against admin settings */
15858     bxe_get_phy_info(sc);
15859 
15860     /* initialize the FreeBSD ifnet interface */
15861     if (bxe_init_ifnet(sc) != 0) {
15862         bxe_release_mutexes(sc);
15863         bxe_deallocate_bars(sc);
15864         pci_disable_busmaster(dev);
15865         return (ENXIO);
15866     }
15867 
15868     if (bxe_add_cdev(sc) != 0) {
15869         if (sc->ifp != NULL) {
15870             ether_ifdetach(sc->ifp);
15871         }
15872         ifmedia_removeall(&sc->ifmedia);
15873         bxe_release_mutexes(sc);
15874         bxe_deallocate_bars(sc);
15875         pci_disable_busmaster(dev);
15876         return (ENXIO);
15877     }
15878 
15879     /* allocate device interrupts */
15880     if (bxe_interrupt_alloc(sc) != 0) {
15881         bxe_del_cdev(sc);
15882         if (sc->ifp != NULL) {
15883             ether_ifdetach(sc->ifp);
15884         }
15885         ifmedia_removeall(&sc->ifmedia);
15886         bxe_release_mutexes(sc);
15887         bxe_deallocate_bars(sc);
15888         pci_disable_busmaster(dev);
15889         return (ENXIO);
15890     }
15891 
15892     bxe_init_fp_mutexs(sc);
15893 
15894     if (bxe_alloc_buf_rings(sc) != 0) {
15895 	bxe_free_buf_rings(sc);
15896         bxe_interrupt_free(sc);
15897         bxe_del_cdev(sc);
15898         if (sc->ifp != NULL) {
15899             ether_ifdetach(sc->ifp);
15900         }
15901         ifmedia_removeall(&sc->ifmedia);
15902         bxe_release_mutexes(sc);
15903         bxe_deallocate_bars(sc);
15904         pci_disable_busmaster(dev);
15905         return (ENXIO);
15906     }
15907 
15908     /* allocate ilt */
15909     if (bxe_alloc_ilt_mem(sc) != 0) {
15910 	bxe_free_buf_rings(sc);
15911         bxe_interrupt_free(sc);
15912         bxe_del_cdev(sc);
15913         if (sc->ifp != NULL) {
15914             ether_ifdetach(sc->ifp);
15915         }
15916         ifmedia_removeall(&sc->ifmedia);
15917         bxe_release_mutexes(sc);
15918         bxe_deallocate_bars(sc);
15919         pci_disable_busmaster(dev);
15920         return (ENXIO);
15921     }
15922 
15923     /* allocate the host hardware/software hsi structures */
15924     if (bxe_alloc_hsi_mem(sc) != 0) {
15925         bxe_free_ilt_mem(sc);
15926 	bxe_free_buf_rings(sc);
15927         bxe_interrupt_free(sc);
15928         bxe_del_cdev(sc);
15929         if (sc->ifp != NULL) {
15930             ether_ifdetach(sc->ifp);
15931         }
15932         ifmedia_removeall(&sc->ifmedia);
15933         bxe_release_mutexes(sc);
15934         bxe_deallocate_bars(sc);
15935         pci_disable_busmaster(dev);
15936         return (ENXIO);
15937     }
15938 
15939     /* need to reset chip if UNDI was active */
15940     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
15941         /* init fw_seq */
15942         sc->fw_seq =
15943             (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
15944              DRV_MSG_SEQ_NUMBER_MASK);
15945         BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
15946         bxe_prev_unload(sc);
15947     }
15948 
15949 #if 1
15950     /* XXX */
15951     bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
15952 #else
15953     if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
15954         SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
15955         SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
15956         SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
15957         bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
15958         bxe_dcbx_init_params(sc);
15959     } else {
15960         bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
15961     }
15962 #endif
15963 
15964     /* calculate qm_cid_count */
15965     sc->qm_cid_count = bxe_set_qm_cid_count(sc);
15966     BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
15967 
15968     sc->max_cos = 1;
15969     bxe_init_multi_cos(sc);
15970 
15971     bxe_add_sysctls(sc);
15972 
15973     return (0);
15974 }
15975 
15976 /*
15977  * Device detach function.
15978  *
15979  * Stops the controller, resets the controller, and releases resources.
15980  *
15981  * Returns:
15982  *   0 = Success, >0 = Failure
15983  */
15984 static int
15985 bxe_detach(device_t dev)
15986 {
15987     struct bxe_softc *sc;
15988     if_t ifp;
15989 
15990     sc = device_get_softc(dev);
15991 
15992     BLOGD(sc, DBG_LOAD, "Starting detach...\n");
15993 
15994     ifp = sc->ifp;
15995     if (ifp != NULL && if_vlantrunkinuse(ifp)) {
15996         BLOGE(sc, "Cannot detach while VLANs are in use.\n");
15997         return(EBUSY);
15998     }
15999 
16000     bxe_del_cdev(sc);
16001 
16002     /* stop the periodic callout */
16003     bxe_periodic_stop(sc);
16004 
16005     /* stop the chip taskqueue */
16006     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16007     if (sc->chip_tq) {
16008         taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16009         taskqueue_free(sc->chip_tq);
16010         sc->chip_tq = NULL;
16011     }
16012 
16013     /* stop and reset the controller if it was open */
16014     if (sc->state != BXE_STATE_CLOSED) {
16015         BXE_CORE_LOCK(sc);
16016         bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16017         sc->state = BXE_STATE_DISABLED;
16018         BXE_CORE_UNLOCK(sc);
16019     }
16020 
16021     /* release the network interface */
16022     if (ifp != NULL) {
16023         ether_ifdetach(ifp);
16024     }
16025     ifmedia_removeall(&sc->ifmedia);
16026 
16027     /* XXX do the following based on driver state... */
16028 
16029     /* free the host hardware/software hsi structures */
16030     bxe_free_hsi_mem(sc);
16031 
16032     /* free ilt */
16033     bxe_free_ilt_mem(sc);
16034 
16035     bxe_free_buf_rings(sc);
16036 
16037     /* release the interrupts */
16038     bxe_interrupt_free(sc);
16039 
16040     /* Release the mutexes*/
16041     bxe_destroy_fp_mutexs(sc);
16042     bxe_release_mutexes(sc);
16043 
16044 
16045     /* Release the PCIe BAR mapped memory */
16046     bxe_deallocate_bars(sc);
16047 
16048     /* Release the FreeBSD interface. */
16049     if (sc->ifp != NULL) {
16050         if_free(sc->ifp);
16051     }
16052 
16053     pci_disable_busmaster(dev);
16054 
16055     return (0);
16056 }
16057 
16058 /*
16059  * Device shutdown function.
16060  *
16061  * Stops and resets the controller.
16062  *
16063  * Returns:
16064  *   Nothing
16065  */
16066 static int
16067 bxe_shutdown(device_t dev)
16068 {
16069     struct bxe_softc *sc;
16070 
16071     sc = device_get_softc(dev);
16072 
16073     BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16074 
16075     /* stop the periodic callout */
16076     bxe_periodic_stop(sc);
16077 
16078     BXE_CORE_LOCK(sc);
16079     bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16080     BXE_CORE_UNLOCK(sc);
16081 
16082     return (0);
16083 }
16084 
16085 void
16086 bxe_igu_ack_sb(struct bxe_softc *sc,
16087                uint8_t          igu_sb_id,
16088                uint8_t          segment,
16089                uint16_t         index,
16090                uint8_t          op,
16091                uint8_t          update)
16092 {
16093     uint32_t igu_addr = sc->igu_base_addr;
16094     igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16095     bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16096 }
16097 
16098 static void
16099 bxe_igu_clear_sb_gen(struct bxe_softc *sc,
16100                      uint8_t          func,
16101                      uint8_t          idu_sb_id,
16102                      uint8_t          is_pf)
16103 {
16104     uint32_t data, ctl, cnt = 100;
16105     uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16106     uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16107     uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16108     uint32_t sb_bit =  1 << (idu_sb_id%32);
16109     uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16110     uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16111 
16112     /* Not supported in BC mode */
16113     if (CHIP_INT_MODE_IS_BC(sc)) {
16114         return;
16115     }
16116 
16117     data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16118              IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16119             IGU_REGULAR_CLEANUP_SET |
16120             IGU_REGULAR_BCLEANUP);
16121 
16122     ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16123            (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16124            (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16125 
16126     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16127             data, igu_addr_data);
16128     REG_WR(sc, igu_addr_data, data);
16129 
16130     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16131                       BUS_SPACE_BARRIER_WRITE);
16132     mb();
16133 
16134     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16135             ctl, igu_addr_ctl);
16136     REG_WR(sc, igu_addr_ctl, ctl);
16137 
16138     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16139                       BUS_SPACE_BARRIER_WRITE);
16140     mb();
16141 
16142     /* wait for clean up to finish */
16143     while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16144         DELAY(20000);
16145     }
16146 
16147     if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16148         BLOGD(sc, DBG_LOAD,
16149               "Unable to finish IGU cleanup: "
16150               "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16151               idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16152     }
16153 }
16154 
16155 static void
16156 bxe_igu_clear_sb(struct bxe_softc *sc,
16157                  uint8_t          idu_sb_id)
16158 {
16159     bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16160 }
16161 
16162 
16163 
16164 
16165 
16166 
16167 
16168 /*******************/
16169 /* ECORE CALLBACKS */
16170 /*******************/
16171 
16172 static void
16173 bxe_reset_common(struct bxe_softc *sc)
16174 {
16175     uint32_t val = 0x1400;
16176 
16177     /* reset_common */
16178     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16179 
16180     if (CHIP_IS_E3(sc)) {
16181         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16182         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16183     }
16184 
16185     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16186 }
16187 
16188 static void
16189 bxe_common_init_phy(struct bxe_softc *sc)
16190 {
16191     uint32_t shmem_base[2];
16192     uint32_t shmem2_base[2];
16193 
16194     /* Avoid common init in case MFW supports LFA */
16195     if (SHMEM2_RD(sc, size) >
16196         (uint32_t)offsetof(struct shmem2_region,
16197                            lfa_host_addr[SC_PORT(sc)])) {
16198         return;
16199     }
16200 
16201     shmem_base[0]  = sc->devinfo.shmem_base;
16202     shmem2_base[0] = sc->devinfo.shmem2_base;
16203 
16204     if (!CHIP_IS_E1x(sc)) {
16205         shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16206         shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16207     }
16208 
16209     bxe_acquire_phy_lock(sc);
16210     elink_common_init_phy(sc, shmem_base, shmem2_base,
16211                           sc->devinfo.chip_id, 0);
16212     bxe_release_phy_lock(sc);
16213 }
16214 
16215 static void
16216 bxe_pf_disable(struct bxe_softc *sc)
16217 {
16218     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16219 
16220     val &= ~IGU_PF_CONF_FUNC_EN;
16221 
16222     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16223     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16224     REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16225 }
16226 
16227 static void
16228 bxe_init_pxp(struct bxe_softc *sc)
16229 {
16230     uint16_t devctl;
16231     int r_order, w_order;
16232 
16233     devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16234 
16235     BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16236 
16237     w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16238 
16239     if (sc->mrrs == -1) {
16240         r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16241     } else {
16242         BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16243         r_order = sc->mrrs;
16244     }
16245 
16246     ecore_init_pxp_arb(sc, r_order, w_order);
16247 }
16248 
16249 static uint32_t
16250 bxe_get_pretend_reg(struct bxe_softc *sc)
16251 {
16252     uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16253     uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16254     return (base + (SC_ABS_FUNC(sc)) * stride);
16255 }
16256 
16257 /*
16258  * Called only on E1H or E2.
16259  * When pretending to be PF, the pretend value is the function number 0..7.
16260  * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16261  * combination.
16262  */
16263 static int
16264 bxe_pretend_func(struct bxe_softc *sc,
16265                  uint16_t         pretend_func_val)
16266 {
16267     uint32_t pretend_reg;
16268 
16269     if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16270         return (-1);
16271     }
16272 
16273     /* get my own pretend register */
16274     pretend_reg = bxe_get_pretend_reg(sc);
16275     REG_WR(sc, pretend_reg, pretend_func_val);
16276     REG_RD(sc, pretend_reg);
16277     return (0);
16278 }
16279 
16280 static void
16281 bxe_iov_init_dmae(struct bxe_softc *sc)
16282 {
16283     return;
16284 }
16285 
16286 static void
16287 bxe_iov_init_dq(struct bxe_softc *sc)
16288 {
16289     return;
16290 }
16291 
16292 /* send a NIG loopback debug packet */
16293 static void
16294 bxe_lb_pckt(struct bxe_softc *sc)
16295 {
16296     uint32_t wb_write[3];
16297 
16298     /* Ethernet source and destination addresses */
16299     wb_write[0] = 0x55555555;
16300     wb_write[1] = 0x55555555;
16301     wb_write[2] = 0x20;     /* SOP */
16302     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16303 
16304     /* NON-IP protocol */
16305     wb_write[0] = 0x09000000;
16306     wb_write[1] = 0x55555555;
16307     wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16308     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16309 }
16310 
16311 /*
16312  * Some of the internal memories are not directly readable from the driver.
16313  * To test them we send debug packets.
16314  */
16315 static int
16316 bxe_int_mem_test(struct bxe_softc *sc)
16317 {
16318     int factor;
16319     int count, i;
16320     uint32_t val = 0;
16321 
16322     if (CHIP_REV_IS_FPGA(sc)) {
16323         factor = 120;
16324     } else if (CHIP_REV_IS_EMUL(sc)) {
16325         factor = 200;
16326     } else {
16327         factor = 1;
16328     }
16329 
16330     /* disable inputs of parser neighbor blocks */
16331     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16332     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16333     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16334     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16335 
16336     /*  write 0 to parser credits for CFC search request */
16337     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16338 
16339     /* send Ethernet packet */
16340     bxe_lb_pckt(sc);
16341 
16342     /* TODO do i reset NIG statistic? */
16343     /* Wait until NIG register shows 1 packet of size 0x10 */
16344     count = 1000 * factor;
16345     while (count) {
16346         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16347         val = *BXE_SP(sc, wb_data[0]);
16348         if (val == 0x10) {
16349             break;
16350         }
16351 
16352         DELAY(10000);
16353         count--;
16354     }
16355 
16356     if (val != 0x10) {
16357         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16358         return (-1);
16359     }
16360 
16361     /* wait until PRS register shows 1 packet */
16362     count = (1000 * factor);
16363     while (count) {
16364         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16365         if (val == 1) {
16366             break;
16367         }
16368 
16369         DELAY(10000);
16370         count--;
16371     }
16372 
16373     if (val != 0x1) {
16374         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16375         return (-2);
16376     }
16377 
16378     /* Reset and init BRB, PRS */
16379     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16380     DELAY(50000);
16381     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16382     DELAY(50000);
16383     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16384     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16385 
16386     /* Disable inputs of parser neighbor blocks */
16387     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16388     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16389     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16390     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16391 
16392     /* Write 0 to parser credits for CFC search request */
16393     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16394 
16395     /* send 10 Ethernet packets */
16396     for (i = 0; i < 10; i++) {
16397         bxe_lb_pckt(sc);
16398     }
16399 
16400     /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16401     count = (1000 * factor);
16402     while (count) {
16403         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16404         val = *BXE_SP(sc, wb_data[0]);
16405         if (val == 0xb0) {
16406             break;
16407         }
16408 
16409         DELAY(10000);
16410         count--;
16411     }
16412 
16413     if (val != 0xb0) {
16414         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16415         return (-3);
16416     }
16417 
16418     /* Wait until PRS register shows 2 packets */
16419     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16420     if (val != 2) {
16421         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16422     }
16423 
16424     /* Write 1 to parser credits for CFC search request */
16425     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16426 
16427     /* Wait until PRS register shows 3 packets */
16428     DELAY(10000 * factor);
16429 
16430     /* Wait until NIG register shows 1 packet of size 0x10 */
16431     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16432     if (val != 3) {
16433         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16434     }
16435 
16436     /* clear NIG EOP FIFO */
16437     for (i = 0; i < 11; i++) {
16438         REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16439     }
16440 
16441     val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16442     if (val != 1) {
16443         BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16444         return (-4);
16445     }
16446 
16447     /* Reset and init BRB, PRS, NIG */
16448     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16449     DELAY(50000);
16450     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16451     DELAY(50000);
16452     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16453     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16454     if (!CNIC_SUPPORT(sc)) {
16455         /* set NIC mode */
16456         REG_WR(sc, PRS_REG_NIC_MODE, 1);
16457     }
16458 
16459     /* Enable inputs of parser neighbor blocks */
16460     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16461     REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16462     REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16463     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16464 
16465     return (0);
16466 }
16467 
16468 static void
16469 bxe_setup_fan_failure_detection(struct bxe_softc *sc)
16470 {
16471     int is_required;
16472     uint32_t val;
16473     int port;
16474 
16475     is_required = 0;
16476     val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
16477            SHARED_HW_CFG_FAN_FAILURE_MASK);
16478 
16479     if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
16480         is_required = 1;
16481     }
16482     /*
16483      * The fan failure mechanism is usually related to the PHY type since
16484      * the power consumption of the board is affected by the PHY. Currently,
16485      * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
16486      */
16487     else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
16488         for (port = PORT_0; port < PORT_MAX; port++) {
16489             is_required |= elink_fan_failure_det_req(sc,
16490                                                      sc->devinfo.shmem_base,
16491                                                      sc->devinfo.shmem2_base,
16492                                                      port);
16493         }
16494     }
16495 
16496     BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
16497 
16498     if (is_required == 0) {
16499         return;
16500     }
16501 
16502     /* Fan failure is indicated by SPIO 5 */
16503     bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
16504 
16505     /* set to active low mode */
16506     val = REG_RD(sc, MISC_REG_SPIO_INT);
16507     val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
16508     REG_WR(sc, MISC_REG_SPIO_INT, val);
16509 
16510     /* enable interrupt to signal the IGU */
16511     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
16512     val |= MISC_SPIO_SPIO5;
16513     REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
16514 }
16515 
16516 static void
16517 bxe_enable_blocks_attention(struct bxe_softc *sc)
16518 {
16519     uint32_t val;
16520 
16521     REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16522     if (!CHIP_IS_E1x(sc)) {
16523         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
16524     } else {
16525         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
16526     }
16527     REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16528     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16529     /*
16530      * mask read length error interrupts in brb for parser
16531      * (parsing unit and 'checksum and crc' unit)
16532      * these errors are legal (PU reads fixed length and CAC can cause
16533      * read length error on truncated packets)
16534      */
16535     REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
16536     REG_WR(sc, QM_REG_QM_INT_MASK, 0);
16537     REG_WR(sc, TM_REG_TM_INT_MASK, 0);
16538     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
16539     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
16540     REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
16541 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
16542 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
16543     REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
16544     REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
16545     REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
16546 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
16547 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
16548     REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
16549     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
16550     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
16551     REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
16552 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
16553 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
16554 
16555     val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
16556            PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
16557            PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
16558     if (!CHIP_IS_E1x(sc)) {
16559         val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
16560                 PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
16561     }
16562     REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
16563 
16564     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
16565     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
16566     REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
16567 /*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
16568 
16569     if (!CHIP_IS_E1x(sc)) {
16570         /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
16571         REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
16572     }
16573 
16574     REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
16575     REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
16576 /*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
16577     REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
16578 }
16579 
16580 /**
16581  * bxe_init_hw_common - initialize the HW at the COMMON phase.
16582  *
16583  * @sc:     driver handle
16584  */
16585 static int
16586 bxe_init_hw_common(struct bxe_softc *sc)
16587 {
16588     uint8_t abs_func_id;
16589     uint32_t val;
16590 
16591     BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
16592           SC_ABS_FUNC(sc));
16593 
16594     /*
16595      * take the RESET lock to protect undi_unload flow from accessing
16596      * registers while we are resetting the chip
16597      */
16598     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16599 
16600     bxe_reset_common(sc);
16601 
16602     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
16603 
16604     val = 0xfffc;
16605     if (CHIP_IS_E3(sc)) {
16606         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16607         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16608     }
16609 
16610     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
16611 
16612     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16613 
16614     ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
16615     BLOGD(sc, DBG_LOAD, "after misc block init\n");
16616 
16617     if (!CHIP_IS_E1x(sc)) {
16618         /*
16619          * 4-port mode or 2-port mode we need to turn off master-enable for
16620          * everyone. After that we turn it back on for self. So, we disregard
16621          * multi-function, and always disable all functions on the given path,
16622          * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
16623          */
16624         for (abs_func_id = SC_PATH(sc);
16625              abs_func_id < (E2_FUNC_MAX * 2);
16626              abs_func_id += 2) {
16627             if (abs_func_id == SC_ABS_FUNC(sc)) {
16628                 REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
16629                 continue;
16630             }
16631 
16632             bxe_pretend_func(sc, abs_func_id);
16633 
16634             /* clear pf enable */
16635             bxe_pf_disable(sc);
16636 
16637             bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16638         }
16639     }
16640 
16641     BLOGD(sc, DBG_LOAD, "after pf disable\n");
16642 
16643     ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
16644 
16645     if (CHIP_IS_E1(sc)) {
16646         /*
16647          * enable HW interrupt from PXP on USDM overflow
16648          * bit 16 on INT_MASK_0
16649          */
16650         REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16651     }
16652 
16653     ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
16654     bxe_init_pxp(sc);
16655 
16656 #ifdef __BIG_ENDIAN
16657     REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
16658     REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
16659     REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
16660     REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
16661     REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
16662     /* make sure this value is 0 */
16663     REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
16664 
16665     //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
16666     REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
16667     REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
16668     REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
16669     REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
16670 #endif
16671 
16672     ecore_ilt_init_page_size(sc, INITOP_SET);
16673 
16674     if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
16675         REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
16676     }
16677 
16678     /* let the HW do it's magic... */
16679     DELAY(100000);
16680 
16681     /* finish PXP init */
16682     val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
16683     if (val != 1) {
16684         BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
16685             val);
16686         return (-1);
16687     }
16688     val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
16689     if (val != 1) {
16690         BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
16691         return (-1);
16692     }
16693 
16694     BLOGD(sc, DBG_LOAD, "after pxp init\n");
16695 
16696     /*
16697      * Timer bug workaround for E2 only. We need to set the entire ILT to have
16698      * entries with value "0" and valid bit on. This needs to be done by the
16699      * first PF that is loaded in a path (i.e. common phase)
16700      */
16701     if (!CHIP_IS_E1x(sc)) {
16702 /*
16703  * In E2 there is a bug in the timers block that can cause function 6 / 7
16704  * (i.e. vnic3) to start even if it is marked as "scan-off".
16705  * This occurs when a different function (func2,3) is being marked
16706  * as "scan-off". Real-life scenario for example: if a driver is being
16707  * load-unloaded while func6,7 are down. This will cause the timer to access
16708  * the ilt, translate to a logical address and send a request to read/write.
16709  * Since the ilt for the function that is down is not valid, this will cause
16710  * a translation error which is unrecoverable.
16711  * The Workaround is intended to make sure that when this happens nothing
16712  * fatal will occur. The workaround:
16713  *  1.  First PF driver which loads on a path will:
16714  *      a.  After taking the chip out of reset, by using pretend,
16715  *          it will write "0" to the following registers of
16716  *          the other vnics.
16717  *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16718  *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
16719  *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
16720  *          And for itself it will write '1' to
16721  *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
16722  *          dmae-operations (writing to pram for example.)
16723  *          note: can be done for only function 6,7 but cleaner this
16724  *            way.
16725  *      b.  Write zero+valid to the entire ILT.
16726  *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
16727  *          VNIC3 (of that port). The range allocated will be the
16728  *          entire ILT. This is needed to prevent  ILT range error.
16729  *  2.  Any PF driver load flow:
16730  *      a.  ILT update with the physical addresses of the allocated
16731  *          logical pages.
16732  *      b.  Wait 20msec. - note that this timeout is needed to make
16733  *          sure there are no requests in one of the PXP internal
16734  *          queues with "old" ILT addresses.
16735  *      c.  PF enable in the PGLC.
16736  *      d.  Clear the was_error of the PF in the PGLC. (could have
16737  *          occurred while driver was down)
16738  *      e.  PF enable in the CFC (WEAK + STRONG)
16739  *      f.  Timers scan enable
16740  *  3.  PF driver unload flow:
16741  *      a.  Clear the Timers scan_en.
16742  *      b.  Polling for scan_on=0 for that PF.
16743  *      c.  Clear the PF enable bit in the PXP.
16744  *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
16745  *      e.  Write zero+valid to all ILT entries (The valid bit must
16746  *          stay set)
16747  *      f.  If this is VNIC 3 of a port then also init
16748  *          first_timers_ilt_entry to zero and last_timers_ilt_entry
16749  *          to the last enrty in the ILT.
16750  *
16751  *      Notes:
16752  *      Currently the PF error in the PGLC is non recoverable.
16753  *      In the future the there will be a recovery routine for this error.
16754  *      Currently attention is masked.
16755  *      Having an MCP lock on the load/unload process does not guarantee that
16756  *      there is no Timer disable during Func6/7 enable. This is because the
16757  *      Timers scan is currently being cleared by the MCP on FLR.
16758  *      Step 2.d can be done only for PF6/7 and the driver can also check if
16759  *      there is error before clearing it. But the flow above is simpler and
16760  *      more general.
16761  *      All ILT entries are written by zero+valid and not just PF6/7
16762  *      ILT entries since in the future the ILT entries allocation for
16763  *      PF-s might be dynamic.
16764  */
16765         struct ilt_client_info ilt_cli;
16766         struct ecore_ilt ilt;
16767 
16768         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
16769         memset(&ilt, 0, sizeof(struct ecore_ilt));
16770 
16771         /* initialize dummy TM client */
16772         ilt_cli.start      = 0;
16773         ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
16774         ilt_cli.client_num = ILT_CLIENT_TM;
16775 
16776         /*
16777          * Step 1: set zeroes to all ilt page entries with valid bit on
16778          * Step 2: set the timers first/last ilt entry to point
16779          * to the entire range to prevent ILT range error for 3rd/4th
16780          * vnic (this code assumes existence of the vnic)
16781          *
16782          * both steps performed by call to ecore_ilt_client_init_op()
16783          * with dummy TM client
16784          *
16785          * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
16786          * and his brother are split registers
16787          */
16788 
16789         bxe_pretend_func(sc, (SC_PATH(sc) + 6));
16790         ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
16791         bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16792 
16793         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
16794         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
16795         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
16796     }
16797 
16798     REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
16799     REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
16800 
16801     if (!CHIP_IS_E1x(sc)) {
16802         int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
16803                      (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
16804 
16805         ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
16806         ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
16807 
16808         /* let the HW do it's magic... */
16809         do {
16810             DELAY(200000);
16811             val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
16812         } while (factor-- && (val != 1));
16813 
16814         if (val != 1) {
16815             BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
16816             return (-1);
16817         }
16818     }
16819 
16820     BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
16821 
16822     ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
16823 
16824     bxe_iov_init_dmae(sc);
16825 
16826     /* clean the DMAE memory */
16827     sc->dmae_ready = 1;
16828     ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
16829 
16830     ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
16831 
16832     ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
16833 
16834     ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
16835 
16836     ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
16837 
16838     bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
16839     bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
16840     bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
16841     bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
16842 
16843     ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
16844 
16845     /* QM queues pointers table */
16846     ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
16847 
16848     /* soft reset pulse */
16849     REG_WR(sc, QM_REG_SOFT_RESET, 1);
16850     REG_WR(sc, QM_REG_SOFT_RESET, 0);
16851 
16852     if (CNIC_SUPPORT(sc))
16853         ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
16854 
16855     ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
16856     REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
16857     if (!CHIP_REV_IS_SLOW(sc)) {
16858         /* enable hw interrupt from doorbell Q */
16859         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16860     }
16861 
16862     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16863 
16864     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16865     REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
16866 
16867     if (!CHIP_IS_E1(sc)) {
16868         REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
16869     }
16870 
16871     if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
16872         if (IS_MF_AFEX(sc)) {
16873             /*
16874              * configure that AFEX and VLAN headers must be
16875              * received in AFEX mode
16876              */
16877             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
16878             REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
16879             REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
16880             REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
16881             REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
16882         } else {
16883             /*
16884              * Bit-map indicating which L2 hdrs may appear
16885              * after the basic Ethernet header
16886              */
16887             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
16888                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
16889         }
16890     }
16891 
16892     ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
16893     ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
16894     ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
16895     ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
16896 
16897     if (!CHIP_IS_E1x(sc)) {
16898         /* reset VFC memories */
16899         REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16900                VFC_MEMORIES_RST_REG_CAM_RST |
16901                VFC_MEMORIES_RST_REG_RAM_RST);
16902         REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
16903                VFC_MEMORIES_RST_REG_CAM_RST |
16904                VFC_MEMORIES_RST_REG_RAM_RST);
16905 
16906         DELAY(20000);
16907     }
16908 
16909     ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
16910     ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
16911     ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
16912     ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
16913 
16914     /* sync semi rtc */
16915     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
16916            0x80000000);
16917     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
16918            0x80000000);
16919 
16920     ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
16921     ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
16922     ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
16923 
16924     if (!CHIP_IS_E1x(sc)) {
16925         if (IS_MF_AFEX(sc)) {
16926             /*
16927              * configure that AFEX and VLAN headers must be
16928              * sent in AFEX mode
16929              */
16930             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
16931             REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
16932             REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
16933             REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
16934             REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
16935         } else {
16936             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
16937                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
16938         }
16939     }
16940 
16941     REG_WR(sc, SRC_REG_SOFT_RST, 1);
16942 
16943     ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
16944 
16945     if (CNIC_SUPPORT(sc)) {
16946         REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
16947         REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
16948         REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
16949         REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
16950         REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
16951         REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
16952         REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
16953         REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
16954         REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
16955         REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
16956     }
16957     REG_WR(sc, SRC_REG_SOFT_RST, 0);
16958 
16959     if (sizeof(union cdu_context) != 1024) {
16960         /* we currently assume that a context is 1024 bytes */
16961         BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
16962               (long)sizeof(union cdu_context));
16963     }
16964 
16965     ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
16966     val = (4 << 24) + (0 << 12) + 1024;
16967     REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
16968 
16969     ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
16970 
16971     REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
16972     /* enable context validation interrupt from CFC */
16973     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16974 
16975     /* set the thresholds to prevent CFC/CDU race */
16976     REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
16977     ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
16978 
16979     if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
16980         REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
16981     }
16982 
16983     ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
16984     ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
16985 
16986     /* Reset PCIE errors for debug */
16987     REG_WR(sc, 0x2814, 0xffffffff);
16988     REG_WR(sc, 0x3820, 0xffffffff);
16989 
16990     if (!CHIP_IS_E1x(sc)) {
16991         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
16992                (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
16993                 PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
16994         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
16995                (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
16996                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
16997                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
16998         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
16999                (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17000                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17001                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17002     }
17003 
17004     ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17005 
17006     if (!CHIP_IS_E1(sc)) {
17007         /* in E3 this done in per-port section */
17008         if (!CHIP_IS_E3(sc))
17009             REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17010     }
17011 
17012     if (CHIP_IS_E1H(sc)) {
17013         /* not applicable for E2 (and above ...) */
17014         REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17015     }
17016 
17017     if (CHIP_REV_IS_SLOW(sc)) {
17018         DELAY(200000);
17019     }
17020 
17021     /* finish CFC init */
17022     val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17023     if (val != 1) {
17024         BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17025         return (-1);
17026     }
17027     val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17028     if (val != 1) {
17029         BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17030         return (-1);
17031     }
17032     val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17033     if (val != 1) {
17034         BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17035         return (-1);
17036     }
17037     REG_WR(sc, CFC_REG_DEBUG0, 0);
17038 
17039     if (CHIP_IS_E1(sc)) {
17040         /* read NIG statistic to see if this is our first up since powerup */
17041         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17042         val = *BXE_SP(sc, wb_data[0]);
17043 
17044         /* do internal memory self test */
17045         if ((val == 0) && bxe_int_mem_test(sc)) {
17046             BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17047             return (-1);
17048         }
17049     }
17050 
17051     bxe_setup_fan_failure_detection(sc);
17052 
17053     /* clear PXP2 attentions */
17054     REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17055 
17056     bxe_enable_blocks_attention(sc);
17057 
17058     if (!CHIP_REV_IS_SLOW(sc)) {
17059         ecore_enable_blocks_parity(sc);
17060     }
17061 
17062     if (!BXE_NOMCP(sc)) {
17063         if (CHIP_IS_E1x(sc)) {
17064             bxe_common_init_phy(sc);
17065         }
17066     }
17067 
17068     return (0);
17069 }
17070 
17071 /**
17072  * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17073  *
17074  * @sc:     driver handle
17075  */
17076 static int
17077 bxe_init_hw_common_chip(struct bxe_softc *sc)
17078 {
17079     int rc = bxe_init_hw_common(sc);
17080 
17081     if (rc) {
17082         BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17083         return (rc);
17084     }
17085 
17086     /* In E2 2-PORT mode, same ext phy is used for the two paths */
17087     if (!BXE_NOMCP(sc)) {
17088         bxe_common_init_phy(sc);
17089     }
17090 
17091     return (0);
17092 }
17093 
17094 static int
17095 bxe_init_hw_port(struct bxe_softc *sc)
17096 {
17097     int port = SC_PORT(sc);
17098     int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17099     uint32_t low, high;
17100     uint32_t val;
17101 
17102     BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17103 
17104     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17105 
17106     ecore_init_block(sc, BLOCK_MISC, init_phase);
17107     ecore_init_block(sc, BLOCK_PXP, init_phase);
17108     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17109 
17110     /*
17111      * Timers bug workaround: disables the pf_master bit in pglue at
17112      * common phase, we need to enable it here before any dmae access are
17113      * attempted. Therefore we manually added the enable-master to the
17114      * port phase (it also happens in the function phase)
17115      */
17116     if (!CHIP_IS_E1x(sc)) {
17117         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17118     }
17119 
17120     ecore_init_block(sc, BLOCK_ATC, init_phase);
17121     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17122     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17123     ecore_init_block(sc, BLOCK_QM, init_phase);
17124 
17125     ecore_init_block(sc, BLOCK_TCM, init_phase);
17126     ecore_init_block(sc, BLOCK_UCM, init_phase);
17127     ecore_init_block(sc, BLOCK_CCM, init_phase);
17128     ecore_init_block(sc, BLOCK_XCM, init_phase);
17129 
17130     /* QM cid (connection) count */
17131     ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17132 
17133     if (CNIC_SUPPORT(sc)) {
17134         ecore_init_block(sc, BLOCK_TM, init_phase);
17135         REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17136         REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17137     }
17138 
17139     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17140 
17141     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17142 
17143     if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17144         if (IS_MF(sc)) {
17145             low = (BXE_ONE_PORT(sc) ? 160 : 246);
17146         } else if (sc->mtu > 4096) {
17147             if (BXE_ONE_PORT(sc)) {
17148                 low = 160;
17149             } else {
17150                 val = sc->mtu;
17151                 /* (24*1024 + val*4)/256 */
17152                 low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17153             }
17154         } else {
17155             low = (BXE_ONE_PORT(sc) ? 80 : 160);
17156         }
17157         high = (low + 56); /* 14*1024/256 */
17158         REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17159         REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17160     }
17161 
17162     if (CHIP_IS_MODE_4_PORT(sc)) {
17163         REG_WR(sc, SC_PORT(sc) ?
17164                BRB1_REG_MAC_GUARANTIED_1 :
17165                BRB1_REG_MAC_GUARANTIED_0, 40);
17166     }
17167 
17168     ecore_init_block(sc, BLOCK_PRS, init_phase);
17169     if (CHIP_IS_E3B0(sc)) {
17170         if (IS_MF_AFEX(sc)) {
17171             /* configure headers for AFEX mode */
17172             REG_WR(sc, SC_PORT(sc) ?
17173                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17174                    PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17175             REG_WR(sc, SC_PORT(sc) ?
17176                    PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17177                    PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17178             REG_WR(sc, SC_PORT(sc) ?
17179                    PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17180                    PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17181         } else {
17182             /* Ovlan exists only if we are in multi-function +
17183              * switch-dependent mode, in switch-independent there
17184              * is no ovlan headers
17185              */
17186             REG_WR(sc, SC_PORT(sc) ?
17187                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17188                    PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17189                    (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17190         }
17191     }
17192 
17193     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17194     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17195     ecore_init_block(sc, BLOCK_USDM, init_phase);
17196     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17197 
17198     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17199     ecore_init_block(sc, BLOCK_USEM, init_phase);
17200     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17201     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17202 
17203     ecore_init_block(sc, BLOCK_UPB, init_phase);
17204     ecore_init_block(sc, BLOCK_XPB, init_phase);
17205 
17206     ecore_init_block(sc, BLOCK_PBF, init_phase);
17207 
17208     if (CHIP_IS_E1x(sc)) {
17209         /* configure PBF to work without PAUSE mtu 9000 */
17210         REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17211 
17212         /* update threshold */
17213         REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17214         /* update init credit */
17215         REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17216 
17217         /* probe changes */
17218         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17219         DELAY(50);
17220         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17221     }
17222 
17223     if (CNIC_SUPPORT(sc)) {
17224         ecore_init_block(sc, BLOCK_SRC, init_phase);
17225     }
17226 
17227     ecore_init_block(sc, BLOCK_CDU, init_phase);
17228     ecore_init_block(sc, BLOCK_CFC, init_phase);
17229 
17230     if (CHIP_IS_E1(sc)) {
17231         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17232         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17233     }
17234     ecore_init_block(sc, BLOCK_HC, init_phase);
17235 
17236     ecore_init_block(sc, BLOCK_IGU, init_phase);
17237 
17238     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17239     /* init aeu_mask_attn_func_0/1:
17240      *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17241      *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17242      *             bits 4-7 are used for "per vn group attention" */
17243     val = IS_MF(sc) ? 0xF7 : 0x7;
17244     /* Enable DCBX attention for all but E1 */
17245     val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17246     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17247 
17248     ecore_init_block(sc, BLOCK_NIG, init_phase);
17249 
17250     if (!CHIP_IS_E1x(sc)) {
17251         /* Bit-map indicating which L2 hdrs may appear after the
17252          * basic Ethernet header
17253          */
17254         if (IS_MF_AFEX(sc)) {
17255             REG_WR(sc, SC_PORT(sc) ?
17256                    NIG_REG_P1_HDRS_AFTER_BASIC :
17257                    NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17258         } else {
17259             REG_WR(sc, SC_PORT(sc) ?
17260                    NIG_REG_P1_HDRS_AFTER_BASIC :
17261                    NIG_REG_P0_HDRS_AFTER_BASIC,
17262                    IS_MF_SD(sc) ? 7 : 6);
17263         }
17264 
17265         if (CHIP_IS_E3(sc)) {
17266             REG_WR(sc, SC_PORT(sc) ?
17267                    NIG_REG_LLH1_MF_MODE :
17268                    NIG_REG_LLH_MF_MODE, IS_MF(sc));
17269         }
17270     }
17271     if (!CHIP_IS_E3(sc)) {
17272         REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17273     }
17274 
17275     if (!CHIP_IS_E1(sc)) {
17276         /* 0x2 disable mf_ov, 0x1 enable */
17277         REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17278                (IS_MF_SD(sc) ? 0x1 : 0x2));
17279 
17280         if (!CHIP_IS_E1x(sc)) {
17281             val = 0;
17282             switch (sc->devinfo.mf_info.mf_mode) {
17283             case MULTI_FUNCTION_SD:
17284                 val = 1;
17285                 break;
17286             case MULTI_FUNCTION_SI:
17287             case MULTI_FUNCTION_AFEX:
17288                 val = 2;
17289                 break;
17290             }
17291 
17292             REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17293                         NIG_REG_LLH0_CLS_TYPE), val);
17294         }
17295         REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17296         REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17297         REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17298     }
17299 
17300     /* If SPIO5 is set to generate interrupts, enable it for this port */
17301     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17302     if (val & MISC_SPIO_SPIO5) {
17303         uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17304                                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17305         val = REG_RD(sc, reg_addr);
17306         val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17307         REG_WR(sc, reg_addr, val);
17308     }
17309 
17310     return (0);
17311 }
17312 
17313 static uint32_t
17314 bxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17315                        uint32_t         reg,
17316                        uint32_t         expected,
17317                        uint32_t         poll_count)
17318 {
17319     uint32_t cur_cnt = poll_count;
17320     uint32_t val;
17321 
17322     while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17323         DELAY(FLR_WAIT_INTERVAL);
17324     }
17325 
17326     return (val);
17327 }
17328 
17329 static int
17330 bxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17331                               uint32_t         reg,
17332                               char             *msg,
17333                               uint32_t         poll_cnt)
17334 {
17335     uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17336 
17337     if (val != 0) {
17338         BLOGE(sc, "%s usage count=%d\n", msg, val);
17339         return (1);
17340     }
17341 
17342     return (0);
17343 }
17344 
17345 /* Common routines with VF FLR cleanup */
17346 static uint32_t
17347 bxe_flr_clnup_poll_count(struct bxe_softc *sc)
17348 {
17349     /* adjust polling timeout */
17350     if (CHIP_REV_IS_EMUL(sc)) {
17351         return (FLR_POLL_CNT * 2000);
17352     }
17353 
17354     if (CHIP_REV_IS_FPGA(sc)) {
17355         return (FLR_POLL_CNT * 120);
17356     }
17357 
17358     return (FLR_POLL_CNT);
17359 }
17360 
17361 static int
17362 bxe_poll_hw_usage_counters(struct bxe_softc *sc,
17363                            uint32_t         poll_cnt)
17364 {
17365     /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17366     if (bxe_flr_clnup_poll_hw_counter(sc,
17367                                       CFC_REG_NUM_LCIDS_INSIDE_PF,
17368                                       "CFC PF usage counter timed out",
17369                                       poll_cnt)) {
17370         return (1);
17371     }
17372 
17373     /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17374     if (bxe_flr_clnup_poll_hw_counter(sc,
17375                                       DORQ_REG_PF_USAGE_CNT,
17376                                       "DQ PF usage counter timed out",
17377                                       poll_cnt)) {
17378         return (1);
17379     }
17380 
17381     /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17382     if (bxe_flr_clnup_poll_hw_counter(sc,
17383                                       QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17384                                       "QM PF usage counter timed out",
17385                                       poll_cnt)) {
17386         return (1);
17387     }
17388 
17389     /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17390     if (bxe_flr_clnup_poll_hw_counter(sc,
17391                                       TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17392                                       "Timers VNIC usage counter timed out",
17393                                       poll_cnt)) {
17394         return (1);
17395     }
17396 
17397     if (bxe_flr_clnup_poll_hw_counter(sc,
17398                                       TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17399                                       "Timers NUM_SCANS usage counter timed out",
17400                                       poll_cnt)) {
17401         return (1);
17402     }
17403 
17404     /* Wait DMAE PF usage counter to zero */
17405     if (bxe_flr_clnup_poll_hw_counter(sc,
17406                                       dmae_reg_go_c[INIT_DMAE_C(sc)],
17407                                       "DMAE dommand register timed out",
17408                                       poll_cnt)) {
17409         return (1);
17410     }
17411 
17412     return (0);
17413 }
17414 
17415 #define OP_GEN_PARAM(param)                                            \
17416     (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17417 #define OP_GEN_TYPE(type)                                           \
17418     (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17419 #define OP_GEN_AGG_VECT(index)                                             \
17420     (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17421 
17422 static int
17423 bxe_send_final_clnup(struct bxe_softc *sc,
17424                      uint8_t          clnup_func,
17425                      uint32_t         poll_cnt)
17426 {
17427     uint32_t op_gen_command = 0;
17428     uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17429                           CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17430     int ret = 0;
17431 
17432     if (REG_RD(sc, comp_addr)) {
17433         BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17434         return (1);
17435     }
17436 
17437     op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17438     op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17439     op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17440     op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17441 
17442     BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17443     REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17444 
17445     if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17446         BLOGE(sc, "FW final cleanup did not succeed\n");
17447         BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17448               (REG_RD(sc, comp_addr)));
17449         bxe_panic(sc, ("FLR cleanup failed\n"));
17450         return (1);
17451     }
17452 
17453     /* Zero completion for nxt FLR */
17454     REG_WR(sc, comp_addr, 0);
17455 
17456     return (ret);
17457 }
17458 
17459 static void
17460 bxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17461                        struct pbf_pN_buf_regs *regs,
17462                        uint32_t               poll_count)
17463 {
17464     uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17465     uint32_t cur_cnt = poll_count;
17466 
17467     crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
17468     crd = crd_start = REG_RD(sc, regs->crd);
17469     init_crd = REG_RD(sc, regs->init_crd);
17470 
17471     BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
17472     BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
17473     BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
17474 
17475     while ((crd != init_crd) &&
17476            ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
17477             (init_crd - crd_start))) {
17478         if (cur_cnt--) {
17479             DELAY(FLR_WAIT_INTERVAL);
17480             crd = REG_RD(sc, regs->crd);
17481             crd_freed = REG_RD(sc, regs->crd_freed);
17482         } else {
17483             BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
17484             BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
17485             BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
17486             break;
17487         }
17488     }
17489 
17490     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
17491           poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17492 }
17493 
17494 static void
17495 bxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
17496                        struct pbf_pN_cmd_regs *regs,
17497                        uint32_t               poll_count)
17498 {
17499     uint32_t occup, to_free, freed, freed_start;
17500     uint32_t cur_cnt = poll_count;
17501 
17502     occup = to_free = REG_RD(sc, regs->lines_occup);
17503     freed = freed_start = REG_RD(sc, regs->lines_freed);
17504 
17505     BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17506     BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17507 
17508     while (occup &&
17509            ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
17510         if (cur_cnt--) {
17511             DELAY(FLR_WAIT_INTERVAL);
17512             occup = REG_RD(sc, regs->lines_occup);
17513             freed = REG_RD(sc, regs->lines_freed);
17514         } else {
17515             BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
17516             BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17517             BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17518             break;
17519         }
17520     }
17521 
17522     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
17523           poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17524 }
17525 
17526 static void
17527 bxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
17528 {
17529     struct pbf_pN_cmd_regs cmd_regs[] = {
17530         {0, (CHIP_IS_E3B0(sc)) ?
17531             PBF_REG_TQ_OCCUPANCY_Q0 :
17532             PBF_REG_P0_TQ_OCCUPANCY,
17533             (CHIP_IS_E3B0(sc)) ?
17534             PBF_REG_TQ_LINES_FREED_CNT_Q0 :
17535             PBF_REG_P0_TQ_LINES_FREED_CNT},
17536         {1, (CHIP_IS_E3B0(sc)) ?
17537             PBF_REG_TQ_OCCUPANCY_Q1 :
17538             PBF_REG_P1_TQ_OCCUPANCY,
17539             (CHIP_IS_E3B0(sc)) ?
17540             PBF_REG_TQ_LINES_FREED_CNT_Q1 :
17541             PBF_REG_P1_TQ_LINES_FREED_CNT},
17542         {4, (CHIP_IS_E3B0(sc)) ?
17543             PBF_REG_TQ_OCCUPANCY_LB_Q :
17544             PBF_REG_P4_TQ_OCCUPANCY,
17545             (CHIP_IS_E3B0(sc)) ?
17546             PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
17547             PBF_REG_P4_TQ_LINES_FREED_CNT}
17548     };
17549 
17550     struct pbf_pN_buf_regs buf_regs[] = {
17551         {0, (CHIP_IS_E3B0(sc)) ?
17552             PBF_REG_INIT_CRD_Q0 :
17553             PBF_REG_P0_INIT_CRD ,
17554             (CHIP_IS_E3B0(sc)) ?
17555             PBF_REG_CREDIT_Q0 :
17556             PBF_REG_P0_CREDIT,
17557             (CHIP_IS_E3B0(sc)) ?
17558             PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
17559             PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
17560         {1, (CHIP_IS_E3B0(sc)) ?
17561             PBF_REG_INIT_CRD_Q1 :
17562             PBF_REG_P1_INIT_CRD,
17563             (CHIP_IS_E3B0(sc)) ?
17564             PBF_REG_CREDIT_Q1 :
17565             PBF_REG_P1_CREDIT,
17566             (CHIP_IS_E3B0(sc)) ?
17567             PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
17568             PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
17569         {4, (CHIP_IS_E3B0(sc)) ?
17570             PBF_REG_INIT_CRD_LB_Q :
17571             PBF_REG_P4_INIT_CRD,
17572             (CHIP_IS_E3B0(sc)) ?
17573             PBF_REG_CREDIT_LB_Q :
17574             PBF_REG_P4_CREDIT,
17575             (CHIP_IS_E3B0(sc)) ?
17576             PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
17577             PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
17578     };
17579 
17580     int i;
17581 
17582     /* Verify the command queues are flushed P0, P1, P4 */
17583     for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
17584         bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
17585     }
17586 
17587     /* Verify the transmission buffers are flushed P0, P1, P4 */
17588     for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
17589         bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
17590     }
17591 }
17592 
17593 static void
17594 bxe_hw_enable_status(struct bxe_softc *sc)
17595 {
17596     uint32_t val;
17597 
17598     val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
17599     BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
17600 
17601     val = REG_RD(sc, PBF_REG_DISABLE_PF);
17602     BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
17603 
17604     val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
17605     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
17606 
17607     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
17608     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
17609 
17610     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
17611     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
17612 
17613     val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
17614     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
17615 
17616     val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
17617     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
17618 
17619     val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
17620     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
17621 }
17622 
17623 static int
17624 bxe_pf_flr_clnup(struct bxe_softc *sc)
17625 {
17626     uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
17627 
17628     BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
17629 
17630     /* Re-enable PF target read access */
17631     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
17632 
17633     /* Poll HW usage counters */
17634     BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
17635     if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
17636         return (-1);
17637     }
17638 
17639     /* Zero the igu 'trailing edge' and 'leading edge' */
17640 
17641     /* Send the FW cleanup command */
17642     if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
17643         return (-1);
17644     }
17645 
17646     /* ATC cleanup */
17647 
17648     /* Verify TX hw is flushed */
17649     bxe_tx_hw_flushed(sc, poll_cnt);
17650 
17651     /* Wait 100ms (not adjusted according to platform) */
17652     DELAY(100000);
17653 
17654     /* Verify no pending pci transactions */
17655     if (bxe_is_pcie_pending(sc)) {
17656         BLOGE(sc, "PCIE Transactions still pending\n");
17657     }
17658 
17659     /* Debug */
17660     bxe_hw_enable_status(sc);
17661 
17662     /*
17663      * Master enable - Due to WB DMAE writes performed before this
17664      * register is re-initialized as part of the regular function init
17665      */
17666     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17667 
17668     return (0);
17669 }
17670 
17671 static int
17672 bxe_init_hw_func(struct bxe_softc *sc)
17673 {
17674     int port = SC_PORT(sc);
17675     int func = SC_FUNC(sc);
17676     int init_phase = PHASE_PF0 + func;
17677     struct ecore_ilt *ilt = sc->ilt;
17678     uint16_t cdu_ilt_start;
17679     uint32_t addr, val;
17680     uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
17681     int i, main_mem_width, rc;
17682 
17683     BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
17684 
17685     /* FLR cleanup */
17686     if (!CHIP_IS_E1x(sc)) {
17687         rc = bxe_pf_flr_clnup(sc);
17688         if (rc) {
17689             BLOGE(sc, "FLR cleanup failed!\n");
17690             // XXX bxe_fw_dump(sc);
17691             // XXX bxe_idle_chk(sc);
17692             return (rc);
17693         }
17694     }
17695 
17696     /* set MSI reconfigure capability */
17697     if (sc->devinfo.int_block == INT_BLOCK_HC) {
17698         addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
17699         val = REG_RD(sc, addr);
17700         val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
17701         REG_WR(sc, addr, val);
17702     }
17703 
17704     ecore_init_block(sc, BLOCK_PXP, init_phase);
17705     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17706 
17707     ilt = sc->ilt;
17708     cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
17709 
17710     for (i = 0; i < L2_ILT_LINES(sc); i++) {
17711         ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
17712         ilt->lines[cdu_ilt_start + i].page_mapping =
17713             sc->context[i].vcxt_dma.paddr;
17714         ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
17715     }
17716     ecore_ilt_init_op(sc, INITOP_SET);
17717 
17718     /* Set NIC mode */
17719     REG_WR(sc, PRS_REG_NIC_MODE, 1);
17720     BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
17721 
17722     if (!CHIP_IS_E1x(sc)) {
17723         uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
17724 
17725         /* Turn on a single ISR mode in IGU if driver is going to use
17726          * INT#x or MSI
17727          */
17728         if (sc->interrupt_mode != INTR_MODE_MSIX) {
17729             pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
17730         }
17731 
17732         /*
17733          * Timers workaround bug: function init part.
17734          * Need to wait 20msec after initializing ILT,
17735          * needed to make sure there are no requests in
17736          * one of the PXP internal queues with "old" ILT addresses
17737          */
17738         DELAY(20000);
17739 
17740         /*
17741          * Master enable - Due to WB DMAE writes performed before this
17742          * register is re-initialized as part of the regular function
17743          * init
17744          */
17745         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17746         /* Enable the function in IGU */
17747         REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
17748     }
17749 
17750     sc->dmae_ready = 1;
17751 
17752     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17753 
17754     if (!CHIP_IS_E1x(sc))
17755         REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
17756 
17757     ecore_init_block(sc, BLOCK_ATC, init_phase);
17758     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17759     ecore_init_block(sc, BLOCK_NIG, init_phase);
17760     ecore_init_block(sc, BLOCK_SRC, init_phase);
17761     ecore_init_block(sc, BLOCK_MISC, init_phase);
17762     ecore_init_block(sc, BLOCK_TCM, init_phase);
17763     ecore_init_block(sc, BLOCK_UCM, init_phase);
17764     ecore_init_block(sc, BLOCK_CCM, init_phase);
17765     ecore_init_block(sc, BLOCK_XCM, init_phase);
17766     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17767     ecore_init_block(sc, BLOCK_USEM, init_phase);
17768     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17769     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17770 
17771     if (!CHIP_IS_E1x(sc))
17772         REG_WR(sc, QM_REG_PF_EN, 1);
17773 
17774     if (!CHIP_IS_E1x(sc)) {
17775         REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17776         REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17777         REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17778         REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17779     }
17780     ecore_init_block(sc, BLOCK_QM, init_phase);
17781 
17782     ecore_init_block(sc, BLOCK_TM, init_phase);
17783     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17784 
17785     bxe_iov_init_dq(sc);
17786 
17787     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17788     ecore_init_block(sc, BLOCK_PRS, init_phase);
17789     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17790     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17791     ecore_init_block(sc, BLOCK_USDM, init_phase);
17792     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17793     ecore_init_block(sc, BLOCK_UPB, init_phase);
17794     ecore_init_block(sc, BLOCK_XPB, init_phase);
17795     ecore_init_block(sc, BLOCK_PBF, init_phase);
17796     if (!CHIP_IS_E1x(sc))
17797         REG_WR(sc, PBF_REG_DISABLE_PF, 0);
17798 
17799     ecore_init_block(sc, BLOCK_CDU, init_phase);
17800 
17801     ecore_init_block(sc, BLOCK_CFC, init_phase);
17802 
17803     if (!CHIP_IS_E1x(sc))
17804         REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
17805 
17806     if (IS_MF(sc)) {
17807         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
17808         REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
17809     }
17810 
17811     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17812 
17813     /* HC init per function */
17814     if (sc->devinfo.int_block == INT_BLOCK_HC) {
17815         if (CHIP_IS_E1H(sc)) {
17816             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17817 
17818             REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17819             REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17820         }
17821         ecore_init_block(sc, BLOCK_HC, init_phase);
17822 
17823     } else {
17824         int num_segs, sb_idx, prod_offset;
17825 
17826         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17827 
17828         if (!CHIP_IS_E1x(sc)) {
17829             REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
17830             REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
17831         }
17832 
17833         ecore_init_block(sc, BLOCK_IGU, init_phase);
17834 
17835         if (!CHIP_IS_E1x(sc)) {
17836             int dsb_idx = 0;
17837             /**
17838              * Producer memory:
17839              * E2 mode: address 0-135 match to the mapping memory;
17840              * 136 - PF0 default prod; 137 - PF1 default prod;
17841              * 138 - PF2 default prod; 139 - PF3 default prod;
17842              * 140 - PF0 attn prod;    141 - PF1 attn prod;
17843              * 142 - PF2 attn prod;    143 - PF3 attn prod;
17844              * 144-147 reserved.
17845              *
17846              * E1.5 mode - In backward compatible mode;
17847              * for non default SB; each even line in the memory
17848              * holds the U producer and each odd line hold
17849              * the C producer. The first 128 producers are for
17850              * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
17851              * producers are for the DSB for each PF.
17852              * Each PF has five segments: (the order inside each
17853              * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
17854              * 132-135 C prods; 136-139 X prods; 140-143 T prods;
17855              * 144-147 attn prods;
17856              */
17857             /* non-default-status-blocks */
17858             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17859                 IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
17860             for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
17861                 prod_offset = (sc->igu_base_sb + sb_idx) *
17862                     num_segs;
17863 
17864                 for (i = 0; i < num_segs; i++) {
17865                     addr = IGU_REG_PROD_CONS_MEMORY +
17866                             (prod_offset + i) * 4;
17867                     REG_WR(sc, addr, 0);
17868                 }
17869                 /* send consumer update with value 0 */
17870                 bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
17871                            USTORM_ID, 0, IGU_INT_NOP, 1);
17872                 bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
17873             }
17874 
17875             /* default-status-blocks */
17876             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
17877                 IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
17878 
17879             if (CHIP_IS_MODE_4_PORT(sc))
17880                 dsb_idx = SC_FUNC(sc);
17881             else
17882                 dsb_idx = SC_VN(sc);
17883 
17884             prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
17885                        IGU_BC_BASE_DSB_PROD + dsb_idx :
17886                        IGU_NORM_BASE_DSB_PROD + dsb_idx);
17887 
17888             /*
17889              * igu prods come in chunks of E1HVN_MAX (4) -
17890              * does not matters what is the current chip mode
17891              */
17892             for (i = 0; i < (num_segs * E1HVN_MAX);
17893                  i += E1HVN_MAX) {
17894                 addr = IGU_REG_PROD_CONS_MEMORY +
17895                             (prod_offset + i)*4;
17896                 REG_WR(sc, addr, 0);
17897             }
17898             /* send consumer update with 0 */
17899             if (CHIP_INT_MODE_IS_BC(sc)) {
17900                 bxe_ack_sb(sc, sc->igu_dsb_id,
17901                            USTORM_ID, 0, IGU_INT_NOP, 1);
17902                 bxe_ack_sb(sc, sc->igu_dsb_id,
17903                            CSTORM_ID, 0, IGU_INT_NOP, 1);
17904                 bxe_ack_sb(sc, sc->igu_dsb_id,
17905                            XSTORM_ID, 0, IGU_INT_NOP, 1);
17906                 bxe_ack_sb(sc, sc->igu_dsb_id,
17907                            TSTORM_ID, 0, IGU_INT_NOP, 1);
17908                 bxe_ack_sb(sc, sc->igu_dsb_id,
17909                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
17910             } else {
17911                 bxe_ack_sb(sc, sc->igu_dsb_id,
17912                            USTORM_ID, 0, IGU_INT_NOP, 1);
17913                 bxe_ack_sb(sc, sc->igu_dsb_id,
17914                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
17915             }
17916             bxe_igu_clear_sb(sc, sc->igu_dsb_id);
17917 
17918             /* !!! these should become driver const once
17919                rf-tool supports split-68 const */
17920             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
17921             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
17922             REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
17923             REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
17924             REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
17925             REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
17926         }
17927     }
17928 
17929     /* Reset PCIE errors for debug */
17930     REG_WR(sc, 0x2114, 0xffffffff);
17931     REG_WR(sc, 0x2120, 0xffffffff);
17932 
17933     if (CHIP_IS_E1x(sc)) {
17934         main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
17935         main_mem_base = HC_REG_MAIN_MEMORY +
17936                 SC_PORT(sc) * (main_mem_size * 4);
17937         main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
17938         main_mem_width = 8;
17939 
17940         val = REG_RD(sc, main_mem_prty_clr);
17941         if (val) {
17942             BLOGD(sc, DBG_LOAD,
17943                   "Parity errors in HC block during function init (0x%x)!\n",
17944                   val);
17945         }
17946 
17947         /* Clear "false" parity errors in MSI-X table */
17948         for (i = main_mem_base;
17949              i < main_mem_base + main_mem_size * 4;
17950              i += main_mem_width) {
17951             bxe_read_dmae(sc, i, main_mem_width / 4);
17952             bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
17953                            i, main_mem_width / 4);
17954         }
17955         /* Clear HC parity attention */
17956         REG_RD(sc, main_mem_prty_clr);
17957     }
17958 
17959 #if 1
17960     /* Enable STORMs SP logging */
17961     REG_WR8(sc, BAR_USTRORM_INTMEM +
17962            USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17963     REG_WR8(sc, BAR_TSTRORM_INTMEM +
17964            TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17965     REG_WR8(sc, BAR_CSTRORM_INTMEM +
17966            CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17967     REG_WR8(sc, BAR_XSTRORM_INTMEM +
17968            XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
17969 #endif
17970 
17971     elink_phy_probe(&sc->link_params);
17972 
17973     return (0);
17974 }
17975 
17976 static void
17977 bxe_link_reset(struct bxe_softc *sc)
17978 {
17979     if (!BXE_NOMCP(sc)) {
17980 	bxe_acquire_phy_lock(sc);
17981         elink_lfa_reset(&sc->link_params, &sc->link_vars);
17982 	bxe_release_phy_lock(sc);
17983     } else {
17984         if (!CHIP_REV_IS_SLOW(sc)) {
17985             BLOGW(sc, "Bootcode is missing - cannot reset link\n");
17986         }
17987     }
17988 }
17989 
17990 static void
17991 bxe_reset_port(struct bxe_softc *sc)
17992 {
17993     int port = SC_PORT(sc);
17994     uint32_t val;
17995 
17996     /* reset physical Link */
17997     bxe_link_reset(sc);
17998 
17999     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18000 
18001     /* Do not rcv packets to BRB */
18002     REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18003     /* Do not direct rcv packets that are not for MCP to the BRB */
18004     REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18005                NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18006 
18007     /* Configure AEU */
18008     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18009 
18010     DELAY(100000);
18011 
18012     /* Check for BRB port occupancy */
18013     val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18014     if (val) {
18015         BLOGD(sc, DBG_LOAD,
18016               "BRB1 is not empty, %d blocks are occupied\n", val);
18017     }
18018 
18019     /* TODO: Close Doorbell port? */
18020 }
18021 
18022 static void
18023 bxe_ilt_wr(struct bxe_softc *sc,
18024            uint32_t         index,
18025            bus_addr_t       addr)
18026 {
18027     int reg;
18028     uint32_t wb_write[2];
18029 
18030     if (CHIP_IS_E1(sc)) {
18031         reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18032     } else {
18033         reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18034     }
18035 
18036     wb_write[0] = ONCHIP_ADDR1(addr);
18037     wb_write[1] = ONCHIP_ADDR2(addr);
18038     REG_WR_DMAE(sc, reg, wb_write, 2);
18039 }
18040 
18041 static void
18042 bxe_clear_func_ilt(struct bxe_softc *sc,
18043                    uint32_t         func)
18044 {
18045     uint32_t i, base = FUNC_ILT_BASE(func);
18046     for (i = base; i < base + ILT_PER_FUNC; i++) {
18047         bxe_ilt_wr(sc, i, 0);
18048     }
18049 }
18050 
18051 static void
18052 bxe_reset_func(struct bxe_softc *sc)
18053 {
18054     struct bxe_fastpath *fp;
18055     int port = SC_PORT(sc);
18056     int func = SC_FUNC(sc);
18057     int i;
18058 
18059     /* Disable the function in the FW */
18060     REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18061     REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18062     REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18063     REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18064 
18065     /* FP SBs */
18066     FOR_EACH_ETH_QUEUE(sc, i) {
18067         fp = &sc->fp[i];
18068         REG_WR8(sc, BAR_CSTRORM_INTMEM +
18069                 CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18070                 SB_DISABLED);
18071     }
18072 
18073     /* SP SB */
18074     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18075             CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18076             SB_DISABLED);
18077 
18078     for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18079         REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18080     }
18081 
18082     /* Configure IGU */
18083     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18084         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18085         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18086     } else {
18087         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18088         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18089     }
18090 
18091     if (CNIC_LOADED(sc)) {
18092         /* Disable Timer scan */
18093         REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18094         /*
18095          * Wait for at least 10ms and up to 2 second for the timers
18096          * scan to complete
18097          */
18098         for (i = 0; i < 200; i++) {
18099             DELAY(10000);
18100             if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18101                 break;
18102         }
18103     }
18104 
18105     /* Clear ILT */
18106     bxe_clear_func_ilt(sc, func);
18107 
18108     /*
18109      * Timers workaround bug for E2: if this is vnic-3,
18110      * we need to set the entire ilt range for this timers.
18111      */
18112     if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18113         struct ilt_client_info ilt_cli;
18114         /* use dummy TM client */
18115         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18116         ilt_cli.start = 0;
18117         ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18118         ilt_cli.client_num = ILT_CLIENT_TM;
18119 
18120         ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18121     }
18122 
18123     /* this assumes that reset_port() called before reset_func()*/
18124     if (!CHIP_IS_E1x(sc)) {
18125         bxe_pf_disable(sc);
18126     }
18127 
18128     sc->dmae_ready = 0;
18129 }
18130 
18131 static int
18132 bxe_gunzip_init(struct bxe_softc *sc)
18133 {
18134     return (0);
18135 }
18136 
18137 static void
18138 bxe_gunzip_end(struct bxe_softc *sc)
18139 {
18140     return;
18141 }
18142 
18143 static int
18144 bxe_init_firmware(struct bxe_softc *sc)
18145 {
18146     if (CHIP_IS_E1(sc)) {
18147         ecore_init_e1_firmware(sc);
18148         sc->iro_array = e1_iro_arr;
18149     } else if (CHIP_IS_E1H(sc)) {
18150         ecore_init_e1h_firmware(sc);
18151         sc->iro_array = e1h_iro_arr;
18152     } else if (!CHIP_IS_E1x(sc)) {
18153         ecore_init_e2_firmware(sc);
18154         sc->iro_array = e2_iro_arr;
18155     } else {
18156         BLOGE(sc, "Unsupported chip revision\n");
18157         return (-1);
18158     }
18159 
18160     return (0);
18161 }
18162 
18163 static void
18164 bxe_release_firmware(struct bxe_softc *sc)
18165 {
18166     /* Do nothing */
18167     return;
18168 }
18169 
18170 static int
18171 ecore_gunzip(struct bxe_softc *sc,
18172              const uint8_t    *zbuf,
18173              int              len)
18174 {
18175     /* XXX : Implement... */
18176     BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18177     return (FALSE);
18178 }
18179 
18180 static void
18181 ecore_reg_wr_ind(struct bxe_softc *sc,
18182                  uint32_t         addr,
18183                  uint32_t         val)
18184 {
18185     bxe_reg_wr_ind(sc, addr, val);
18186 }
18187 
18188 static void
18189 ecore_write_dmae_phys_len(struct bxe_softc *sc,
18190                           bus_addr_t       phys_addr,
18191                           uint32_t         addr,
18192                           uint32_t         len)
18193 {
18194     bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18195 }
18196 
18197 void
18198 ecore_storm_memset_struct(struct bxe_softc *sc,
18199                           uint32_t         addr,
18200                           size_t           size,
18201                           uint32_t         *data)
18202 {
18203     uint8_t i;
18204     for (i = 0; i < size/4; i++) {
18205         REG_WR(sc, addr + (i * 4), data[i]);
18206     }
18207 }
18208 
18209 
18210 /*
18211  * character device - ioctl interface definitions
18212  */
18213 
18214 
18215 #include "bxe_dump.h"
18216 #include "bxe_ioctl.h"
18217 #include <sys/conf.h>
18218 
18219 static int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18220                 struct thread *td);
18221 
18222 static struct cdevsw bxe_cdevsw = {
18223     .d_version = D_VERSION,
18224     .d_ioctl = bxe_eioctl,
18225     .d_name = "bxecnic",
18226 };
18227 
18228 #define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18229 
18230 
18231 #define DUMP_ALL_PRESETS        0x1FFF
18232 #define DUMP_MAX_PRESETS        13
18233 #define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18234 #define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18235 #define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18236 #define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18237 #define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18238 
18239 #define IS_REG_IN_PRESET(presets, idx)  \
18240                 ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18241 
18242 
18243 static int
18244 bxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18245 {
18246     if (CHIP_IS_E1(sc))
18247         return dump_num_registers[0][preset-1];
18248     else if (CHIP_IS_E1H(sc))
18249         return dump_num_registers[1][preset-1];
18250     else if (CHIP_IS_E2(sc))
18251         return dump_num_registers[2][preset-1];
18252     else if (CHIP_IS_E3A0(sc))
18253         return dump_num_registers[3][preset-1];
18254     else if (CHIP_IS_E3B0(sc))
18255         return dump_num_registers[4][preset-1];
18256     else
18257         return 0;
18258 }
18259 
18260 static int
18261 bxe_get_total_regs_len32(struct bxe_softc *sc)
18262 {
18263     uint32_t preset_idx;
18264     int regdump_len32 = 0;
18265 
18266 
18267     /* Calculate the total preset regs length */
18268     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18269         regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18270     }
18271 
18272     return regdump_len32;
18273 }
18274 
18275 static const uint32_t *
18276 __bxe_get_page_addr_ar(struct bxe_softc *sc)
18277 {
18278     if (CHIP_IS_E2(sc))
18279         return page_vals_e2;
18280     else if (CHIP_IS_E3(sc))
18281         return page_vals_e3;
18282     else
18283         return NULL;
18284 }
18285 
18286 static uint32_t
18287 __bxe_get_page_reg_num(struct bxe_softc *sc)
18288 {
18289     if (CHIP_IS_E2(sc))
18290         return PAGE_MODE_VALUES_E2;
18291     else if (CHIP_IS_E3(sc))
18292         return PAGE_MODE_VALUES_E3;
18293     else
18294         return 0;
18295 }
18296 
18297 static const uint32_t *
18298 __bxe_get_page_write_ar(struct bxe_softc *sc)
18299 {
18300     if (CHIP_IS_E2(sc))
18301         return page_write_regs_e2;
18302     else if (CHIP_IS_E3(sc))
18303         return page_write_regs_e3;
18304     else
18305         return NULL;
18306 }
18307 
18308 static uint32_t
18309 __bxe_get_page_write_num(struct bxe_softc *sc)
18310 {
18311     if (CHIP_IS_E2(sc))
18312         return PAGE_WRITE_REGS_E2;
18313     else if (CHIP_IS_E3(sc))
18314         return PAGE_WRITE_REGS_E3;
18315     else
18316         return 0;
18317 }
18318 
18319 static const struct reg_addr *
18320 __bxe_get_page_read_ar(struct bxe_softc *sc)
18321 {
18322     if (CHIP_IS_E2(sc))
18323         return page_read_regs_e2;
18324     else if (CHIP_IS_E3(sc))
18325         return page_read_regs_e3;
18326     else
18327         return NULL;
18328 }
18329 
18330 static uint32_t
18331 __bxe_get_page_read_num(struct bxe_softc *sc)
18332 {
18333     if (CHIP_IS_E2(sc))
18334         return PAGE_READ_REGS_E2;
18335     else if (CHIP_IS_E3(sc))
18336         return PAGE_READ_REGS_E3;
18337     else
18338         return 0;
18339 }
18340 
18341 static bool
18342 bxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18343 {
18344     if (CHIP_IS_E1(sc))
18345         return IS_E1_REG(reg_info->chips);
18346     else if (CHIP_IS_E1H(sc))
18347         return IS_E1H_REG(reg_info->chips);
18348     else if (CHIP_IS_E2(sc))
18349         return IS_E2_REG(reg_info->chips);
18350     else if (CHIP_IS_E3A0(sc))
18351         return IS_E3A0_REG(reg_info->chips);
18352     else if (CHIP_IS_E3B0(sc))
18353         return IS_E3B0_REG(reg_info->chips);
18354     else
18355         return 0;
18356 }
18357 
18358 static bool
18359 bxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18360 {
18361     if (CHIP_IS_E1(sc))
18362         return IS_E1_REG(wreg_info->chips);
18363     else if (CHIP_IS_E1H(sc))
18364         return IS_E1H_REG(wreg_info->chips);
18365     else if (CHIP_IS_E2(sc))
18366         return IS_E2_REG(wreg_info->chips);
18367     else if (CHIP_IS_E3A0(sc))
18368         return IS_E3A0_REG(wreg_info->chips);
18369     else if (CHIP_IS_E3B0(sc))
18370         return IS_E3B0_REG(wreg_info->chips);
18371     else
18372         return 0;
18373 }
18374 
18375 /**
18376  * bxe_read_pages_regs - read "paged" registers
18377  *
18378  * @bp          device handle
18379  * @p           output buffer
18380  *
18381  * Reads "paged" memories: memories that may only be read by first writing to a
18382  * specific address ("write address") and then reading from a specific address
18383  * ("read address"). There may be more than one write address per "page" and
18384  * more than one read address per write address.
18385  */
18386 static void
18387 bxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18388 {
18389     uint32_t i, j, k, n;
18390 
18391     /* addresses of the paged registers */
18392     const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18393     /* number of paged registers */
18394     int num_pages = __bxe_get_page_reg_num(sc);
18395     /* write addresses */
18396     const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18397     /* number of write addresses */
18398     int write_num = __bxe_get_page_write_num(sc);
18399     /* read addresses info */
18400     const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18401     /* number of read addresses */
18402     int read_num = __bxe_get_page_read_num(sc);
18403     uint32_t addr, size;
18404 
18405     for (i = 0; i < num_pages; i++) {
18406         for (j = 0; j < write_num; j++) {
18407             REG_WR(sc, write_addr[j], page_addr[i]);
18408 
18409             for (k = 0; k < read_num; k++) {
18410                 if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18411                     size = read_addr[k].size;
18412                     for (n = 0; n < size; n++) {
18413                         addr = read_addr[k].addr + n*4;
18414                         *p++ = REG_RD(sc, addr);
18415                     }
18416                 }
18417             }
18418         }
18419     }
18420     return;
18421 }
18422 
18423 
18424 static int
18425 bxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18426 {
18427     uint32_t i, j, addr;
18428     const struct wreg_addr *wreg_addr_p = NULL;
18429 
18430     if (CHIP_IS_E1(sc))
18431         wreg_addr_p = &wreg_addr_e1;
18432     else if (CHIP_IS_E1H(sc))
18433         wreg_addr_p = &wreg_addr_e1h;
18434     else if (CHIP_IS_E2(sc))
18435         wreg_addr_p = &wreg_addr_e2;
18436     else if (CHIP_IS_E3A0(sc))
18437         wreg_addr_p = &wreg_addr_e3;
18438     else if (CHIP_IS_E3B0(sc))
18439         wreg_addr_p = &wreg_addr_e3b0;
18440     else
18441         return (-1);
18442 
18443     /* Read the idle_chk registers */
18444     for (i = 0; i < IDLE_REGS_COUNT; i++) {
18445         if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18446             IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18447             for (j = 0; j < idle_reg_addrs[i].size; j++)
18448                 *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18449         }
18450     }
18451 
18452     /* Read the regular registers */
18453     for (i = 0; i < REGS_COUNT; i++) {
18454         if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18455             IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18456             for (j = 0; j < reg_addrs[i].size; j++)
18457                 *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18458         }
18459     }
18460 
18461     /* Read the CAM registers */
18462     if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18463         IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18464         for (i = 0; i < wreg_addr_p->size; i++) {
18465             *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
18466 
18467             /* In case of wreg_addr register, read additional
18468                registers from read_regs array
18469              */
18470             for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
18471                 addr = *(wreg_addr_p->read_regs);
18472                 *p++ = REG_RD(sc, addr + j*4);
18473             }
18474         }
18475     }
18476 
18477     /* Paged registers are supported in E2 & E3 only */
18478     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
18479         /* Read "paged" registers */
18480         bxe_read_pages_regs(sc, p, preset);
18481     }
18482 
18483     return 0;
18484 }
18485 
18486 int
18487 bxe_grc_dump(struct bxe_softc *sc)
18488 {
18489     int rval = 0;
18490     uint32_t preset_idx;
18491     uint8_t *buf;
18492     uint32_t size;
18493     struct  dump_header *d_hdr;
18494     uint32_t i;
18495     uint32_t reg_val;
18496     uint32_t reg_addr;
18497     uint32_t cmd_offset;
18498     int context_size;
18499     int allocated;
18500     struct ecore_ilt *ilt = SC_ILT(sc);
18501     struct bxe_fastpath *fp;
18502     struct ilt_client_info *ilt_cli;
18503     int grc_dump_size;
18504 
18505 
18506     if (sc->grcdump_done || sc->grcdump_started)
18507 	return (rval);
18508 
18509     sc->grcdump_started = 1;
18510     BLOGI(sc, "Started collecting grcdump\n");
18511 
18512     grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18513                 sizeof(struct  dump_header);
18514 
18515     sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
18516 
18517     if (sc->grc_dump == NULL) {
18518         BLOGW(sc, "Unable to allocate memory for grcdump collection\n");
18519         return(ENOMEM);
18520     }
18521 
18522 
18523 
18524     /* Disable parity attentions as long as following dump may
18525      * cause false alarms by reading never written registers. We
18526      * will re-enable parity attentions right after the dump.
18527      */
18528 
18529     /* Disable parity on path 0 */
18530     bxe_pretend_func(sc, 0);
18531 
18532     ecore_disable_blocks_parity(sc);
18533 
18534     /* Disable parity on path 1 */
18535     bxe_pretend_func(sc, 1);
18536     ecore_disable_blocks_parity(sc);
18537 
18538     /* Return to current function */
18539     bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18540 
18541     buf = sc->grc_dump;
18542     d_hdr = sc->grc_dump;
18543 
18544     d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
18545     d_hdr->version = BNX2X_DUMP_VERSION;
18546     d_hdr->preset = DUMP_ALL_PRESETS;
18547 
18548     if (CHIP_IS_E1(sc)) {
18549         d_hdr->dump_meta_data = DUMP_CHIP_E1;
18550     } else if (CHIP_IS_E1H(sc)) {
18551         d_hdr->dump_meta_data = DUMP_CHIP_E1H;
18552     } else if (CHIP_IS_E2(sc)) {
18553         d_hdr->dump_meta_data = DUMP_CHIP_E2 |
18554                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18555     } else if (CHIP_IS_E3A0(sc)) {
18556         d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
18557                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18558     } else if (CHIP_IS_E3B0(sc)) {
18559         d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
18560                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18561     }
18562 
18563     buf += sizeof(struct  dump_header);
18564 
18565     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18566 
18567         /* Skip presets with IOR */
18568         if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
18569             (preset_idx == 11))
18570             continue;
18571 
18572         rval = bxe_get_preset_regs(sc, (uint32_t *)buf, preset_idx);
18573 
18574 	if (rval)
18575             break;
18576 
18577         size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
18578 
18579         buf += size;
18580     }
18581 
18582     bxe_pretend_func(sc, 0);
18583     ecore_clear_blocks_parity(sc);
18584     ecore_enable_blocks_parity(sc);
18585 
18586     bxe_pretend_func(sc, 1);
18587     ecore_clear_blocks_parity(sc);
18588     ecore_enable_blocks_parity(sc);
18589 
18590     /* Return to current function */
18591     bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18592 
18593 
18594     context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
18595     for (i = 0, allocated = 0; allocated < context_size; i++) {
18596 
18597         BLOGI(sc, "cdu_context i %d paddr %#jx vaddr %p size 0x%zx\n", i,
18598             (uintmax_t)sc->context[i].vcxt_dma.paddr,
18599             sc->context[i].vcxt_dma.vaddr,
18600             sc->context[i].size);
18601         allocated += sc->context[i].size;
18602     }
18603     BLOGI(sc, "fw stats start_paddr %#jx end_paddr %#jx vaddr %p size 0x%x\n",
18604         (uintmax_t)sc->fw_stats_req_mapping,
18605         (uintmax_t)sc->fw_stats_data_mapping,
18606         sc->fw_stats_req, (sc->fw_stats_req_size + sc->fw_stats_data_size));
18607     BLOGI(sc, "def_status_block paddr %p vaddr %p size 0x%zx\n",
18608         (void *)sc->def_sb_dma.paddr, sc->def_sb,
18609         sizeof(struct host_sp_status_block));
18610     BLOGI(sc, "event_queue paddr %#jx vaddr %p size 0x%x\n",
18611         (uintmax_t)sc->eq_dma.paddr, sc->eq_dma.vaddr, BCM_PAGE_SIZE);
18612     BLOGI(sc, "slow path paddr %#jx vaddr %p size 0x%zx\n",
18613         (uintmax_t)sc->sp_dma.paddr, sc->sp_dma.vaddr,
18614         sizeof(struct bxe_slowpath));
18615     BLOGI(sc, "slow path queue paddr %#jx vaddr %p size 0x%x\n",
18616         (uintmax_t)sc->spq_dma.paddr, sc->spq_dma.vaddr, BCM_PAGE_SIZE);
18617     BLOGI(sc, "fw_buf paddr %#jx vaddr %p size 0x%x\n",
18618         (uintmax_t)sc->gz_buf_dma.paddr, sc->gz_buf_dma.vaddr,
18619         FW_BUF_SIZE);
18620     for (i = 0; i < sc->num_queues; i++) {
18621         fp = &sc->fp[i];
18622         BLOGI(sc, "FP status block fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18623             (uintmax_t)fp->sb_dma.paddr, fp->sb_dma.vaddr,
18624             sizeof(union bxe_host_hc_status_block));
18625         BLOGI(sc, "TX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18626             (uintmax_t)fp->tx_dma.paddr, fp->tx_dma.vaddr,
18627             (BCM_PAGE_SIZE * TX_BD_NUM_PAGES));
18628         BLOGI(sc, "RX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18629             (uintmax_t)fp->rx_dma.paddr, fp->rx_dma.vaddr,
18630             (BCM_PAGE_SIZE * RX_BD_NUM_PAGES));
18631         BLOGI(sc, "RX RCQ CHAIN fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18632             (uintmax_t)fp->rcq_dma.paddr, fp->rcq_dma.vaddr,
18633             (BCM_PAGE_SIZE * RCQ_NUM_PAGES));
18634         BLOGI(sc, "RX SGE CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18635             (uintmax_t)fp->rx_sge_dma.paddr, fp->rx_sge_dma.vaddr,
18636             (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES));
18637     }
18638 
18639     ilt_cli = &ilt->clients[1];
18640     for (i = ilt_cli->start; i <= ilt_cli->end; i++) {
18641         BLOGI(sc, "ECORE_ILT paddr %#jx vaddr %p size 0x%x\n",
18642             (uintmax_t)(((struct bxe_dma *)((&ilt->lines[i])->page))->paddr),
18643             ((struct bxe_dma *)((&ilt->lines[i])->page))->vaddr, BCM_PAGE_SIZE);
18644     }
18645 
18646 
18647     cmd_offset = DMAE_REG_CMD_MEM;
18648     for (i = 0; i < 224; i++) {
18649         reg_addr = (cmd_offset +(i * 4));
18650         reg_val = REG_RD(sc, reg_addr);
18651         BLOGI(sc, "DMAE_REG_CMD_MEM i=%d reg_addr 0x%x reg_val 0x%08x\n",i,
18652             reg_addr, reg_val);
18653     }
18654 
18655 
18656     BLOGI(sc, "Collection of grcdump done\n");
18657     sc->grcdump_done = 1;
18658     return(rval);
18659 }
18660 
18661 static int
18662 bxe_add_cdev(struct bxe_softc *sc)
18663 {
18664     sc->eeprom = malloc(BXE_EEPROM_MAX_DATA_LEN, M_DEVBUF, M_NOWAIT);
18665 
18666     if (sc->eeprom == NULL) {
18667         BLOGW(sc, "Unable to alloc for eeprom size buffer\n");
18668         return (-1);
18669     }
18670 
18671     sc->ioctl_dev = make_dev(&bxe_cdevsw,
18672                             sc->ifp->if_dunit,
18673                             UID_ROOT,
18674                             GID_WHEEL,
18675                             0600,
18676                             "%s",
18677                             if_name(sc->ifp));
18678 
18679     if (sc->ioctl_dev == NULL) {
18680         free(sc->eeprom, M_DEVBUF);
18681         sc->eeprom = NULL;
18682         return (-1);
18683     }
18684 
18685     sc->ioctl_dev->si_drv1 = sc;
18686 
18687     return (0);
18688 }
18689 
18690 static void
18691 bxe_del_cdev(struct bxe_softc *sc)
18692 {
18693     if (sc->ioctl_dev != NULL)
18694         destroy_dev(sc->ioctl_dev);
18695 
18696     if (sc->eeprom != NULL) {
18697         free(sc->eeprom, M_DEVBUF);
18698         sc->eeprom = NULL;
18699     }
18700     sc->ioctl_dev = NULL;
18701 
18702     return;
18703 }
18704 
18705 static bool bxe_is_nvram_accessible(struct bxe_softc *sc)
18706 {
18707 
18708     if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
18709         return FALSE;
18710 
18711     return TRUE;
18712 }
18713 
18714 
18715 static int
18716 bxe_wr_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18717 {
18718     int rval = 0;
18719 
18720     if(!bxe_is_nvram_accessible(sc)) {
18721         BLOGW(sc, "Cannot access eeprom when interface is down\n");
18722         return (-EAGAIN);
18723     }
18724     rval = bxe_nvram_write(sc, offset, (uint8_t *)data, len);
18725 
18726 
18727    return (rval);
18728 }
18729 
18730 static int
18731 bxe_rd_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18732 {
18733     int rval = 0;
18734 
18735     if(!bxe_is_nvram_accessible(sc)) {
18736         BLOGW(sc, "Cannot access eeprom when interface is down\n");
18737         return (-EAGAIN);
18738     }
18739     rval = bxe_nvram_read(sc, offset, (uint8_t *)data, len);
18740 
18741    return (rval);
18742 }
18743 
18744 static int
18745 bxe_eeprom_rd_wr(struct bxe_softc *sc, bxe_eeprom_t *eeprom)
18746 {
18747     int rval = 0;
18748 
18749     switch (eeprom->eeprom_cmd) {
18750 
18751     case BXE_EEPROM_CMD_SET_EEPROM:
18752 
18753         rval = copyin(eeprom->eeprom_data, sc->eeprom,
18754                        eeprom->eeprom_data_len);
18755 
18756         if (rval)
18757             break;
18758 
18759         rval = bxe_wr_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18760                        eeprom->eeprom_data_len);
18761         break;
18762 
18763     case BXE_EEPROM_CMD_GET_EEPROM:
18764 
18765         rval = bxe_rd_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18766                        eeprom->eeprom_data_len);
18767 
18768         if (rval) {
18769             break;
18770         }
18771 
18772         rval = copyout(sc->eeprom, eeprom->eeprom_data,
18773                        eeprom->eeprom_data_len);
18774         break;
18775 
18776     default:
18777             rval = EINVAL;
18778             break;
18779     }
18780 
18781     if (rval) {
18782         BLOGW(sc, "ioctl cmd %d  failed rval %d\n", eeprom->eeprom_cmd, rval);
18783     }
18784 
18785     return (rval);
18786 }
18787 
18788 static int
18789 bxe_get_settings(struct bxe_softc *sc, bxe_dev_setting_t *dev_p)
18790 {
18791     uint32_t ext_phy_config;
18792     int port = SC_PORT(sc);
18793     int cfg_idx = bxe_get_link_cfg_idx(sc);
18794 
18795     dev_p->supported = sc->port.supported[cfg_idx] |
18796             (sc->port.supported[cfg_idx ^ 1] &
18797             (ELINK_SUPPORTED_TP | ELINK_SUPPORTED_FIBRE));
18798     dev_p->advertising = sc->port.advertising[cfg_idx];
18799     if(sc->link_params.phy[bxe_get_cur_phy_idx(sc)].media_type ==
18800         ELINK_ETH_PHY_SFP_1G_FIBER) {
18801         dev_p->supported = ~(ELINK_SUPPORTED_10000baseT_Full);
18802         dev_p->advertising &= ~(ADVERTISED_10000baseT_Full);
18803     }
18804     if ((sc->state == BXE_STATE_OPEN) && sc->link_vars.link_up &&
18805         !(sc->flags & BXE_MF_FUNC_DIS)) {
18806         dev_p->duplex = sc->link_vars.duplex;
18807         if (IS_MF(sc) && !BXE_NOMCP(sc))
18808             dev_p->speed = bxe_get_mf_speed(sc);
18809         else
18810             dev_p->speed = sc->link_vars.line_speed;
18811     } else {
18812         dev_p->duplex = DUPLEX_UNKNOWN;
18813         dev_p->speed = SPEED_UNKNOWN;
18814     }
18815 
18816     dev_p->port = bxe_media_detect(sc);
18817 
18818     ext_phy_config = SHMEM_RD(sc,
18819                          dev_info.port_hw_config[port].external_phy_config);
18820     if((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) ==
18821         PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
18822         dev_p->phy_address =  sc->port.phy_addr;
18823     else if(((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18824             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
18825         ((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18826             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
18827         dev_p->phy_address = ELINK_XGXS_EXT_PHY_ADDR(ext_phy_config);
18828     else
18829         dev_p->phy_address = 0;
18830 
18831     if(sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG)
18832         dev_p->autoneg = AUTONEG_ENABLE;
18833     else
18834        dev_p->autoneg = AUTONEG_DISABLE;
18835 
18836 
18837     return 0;
18838 }
18839 
18840 static int
18841 bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18842         struct thread *td)
18843 {
18844     struct bxe_softc    *sc;
18845     int                 rval = 0;
18846     device_t            pci_dev;
18847     bxe_grcdump_t       *dump = NULL;
18848     int grc_dump_size;
18849     bxe_drvinfo_t   *drv_infop = NULL;
18850     bxe_dev_setting_t  *dev_p;
18851     bxe_dev_setting_t  dev_set;
18852     bxe_get_regs_t  *reg_p;
18853     bxe_reg_rdw_t *reg_rdw_p;
18854     bxe_pcicfg_rdw_t *cfg_rdw_p;
18855     bxe_perm_mac_addr_t *mac_addr_p;
18856 
18857 
18858     if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
18859         return ENXIO;
18860 
18861     pci_dev= sc->dev;
18862 
18863     dump = (bxe_grcdump_t *)data;
18864 
18865     switch(cmd) {
18866 
18867         case BXE_GRC_DUMP_SIZE:
18868             dump->pci_func = sc->pcie_func;
18869             dump->grcdump_size =
18870                 (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18871                      sizeof(struct  dump_header);
18872             break;
18873 
18874         case BXE_GRC_DUMP:
18875 
18876             grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18877                                 sizeof(struct  dump_header);
18878             if ((!sc->trigger_grcdump) || (dump->grcdump == NULL) ||
18879                 (dump->grcdump_size < grc_dump_size)) {
18880                 rval = EINVAL;
18881                 break;
18882             }
18883 
18884             if((sc->trigger_grcdump) && (!sc->grcdump_done) &&
18885                 (!sc->grcdump_started)) {
18886                 rval =  bxe_grc_dump(sc);
18887             }
18888 
18889             if((!rval) && (sc->grcdump_done) && (sc->grcdump_started) &&
18890                 (sc->grc_dump != NULL))  {
18891                 dump->grcdump_dwords = grc_dump_size >> 2;
18892                 rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
18893                 free(sc->grc_dump, M_DEVBUF);
18894                 sc->grc_dump = NULL;
18895                 sc->grcdump_started = 0;
18896                 sc->grcdump_done = 0;
18897             }
18898 
18899             break;
18900 
18901         case BXE_DRV_INFO:
18902             drv_infop = (bxe_drvinfo_t *)data;
18903             snprintf(drv_infop->drv_name, BXE_DRV_NAME_LENGTH, "%s", "bxe");
18904             snprintf(drv_infop->drv_version, BXE_DRV_VERSION_LENGTH, "v:%s",
18905                 BXE_DRIVER_VERSION);
18906             snprintf(drv_infop->mfw_version, BXE_MFW_VERSION_LENGTH, "%s",
18907                 sc->devinfo.bc_ver_str);
18908             snprintf(drv_infop->stormfw_version, BXE_STORMFW_VERSION_LENGTH,
18909                 "%s", sc->fw_ver_str);
18910             drv_infop->eeprom_dump_len = sc->devinfo.flash_size;
18911             drv_infop->reg_dump_len =
18912                 (bxe_get_total_regs_len32(sc) * sizeof(uint32_t))
18913                     + sizeof(struct  dump_header);
18914             snprintf(drv_infop->bus_info, BXE_BUS_INFO_LENGTH, "%d:%d:%d",
18915                 sc->pcie_bus, sc->pcie_device, sc->pcie_func);
18916             break;
18917 
18918         case BXE_DEV_SETTING:
18919             dev_p = (bxe_dev_setting_t *)data;
18920             bxe_get_settings(sc, &dev_set);
18921             dev_p->supported = dev_set.supported;
18922             dev_p->advertising = dev_set.advertising;
18923             dev_p->speed = dev_set.speed;
18924             dev_p->duplex = dev_set.duplex;
18925             dev_p->port = dev_set.port;
18926             dev_p->phy_address = dev_set.phy_address;
18927             dev_p->autoneg = dev_set.autoneg;
18928 
18929             break;
18930 
18931         case BXE_GET_REGS:
18932 
18933             reg_p = (bxe_get_regs_t *)data;
18934             grc_dump_size = reg_p->reg_buf_len;
18935 
18936             if((!sc->grcdump_done) && (!sc->grcdump_started)) {
18937                 bxe_grc_dump(sc);
18938             }
18939             if((sc->grcdump_done) && (sc->grcdump_started) &&
18940                 (sc->grc_dump != NULL))  {
18941                 rval = copyout(sc->grc_dump, reg_p->reg_buf, grc_dump_size);
18942                 free(sc->grc_dump, M_DEVBUF);
18943                 sc->grc_dump = NULL;
18944                 sc->grcdump_started = 0;
18945                 sc->grcdump_done = 0;
18946             }
18947 
18948             break;
18949 
18950         case BXE_RDW_REG:
18951             reg_rdw_p = (bxe_reg_rdw_t *)data;
18952             if((reg_rdw_p->reg_cmd == BXE_READ_REG_CMD) &&
18953                 (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
18954                 reg_rdw_p->reg_val = REG_RD(sc, reg_rdw_p->reg_id);
18955 
18956             if((reg_rdw_p->reg_cmd == BXE_WRITE_REG_CMD) &&
18957                 (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
18958                 REG_WR(sc, reg_rdw_p->reg_id, reg_rdw_p->reg_val);
18959 
18960             break;
18961 
18962         case BXE_RDW_PCICFG:
18963             cfg_rdw_p = (bxe_pcicfg_rdw_t *)data;
18964             if(cfg_rdw_p->cfg_cmd == BXE_READ_PCICFG) {
18965 
18966                 cfg_rdw_p->cfg_val = pci_read_config(sc->dev, cfg_rdw_p->cfg_id,
18967                                          cfg_rdw_p->cfg_width);
18968 
18969             } else if(cfg_rdw_p->cfg_cmd == BXE_WRITE_PCICFG) {
18970                 pci_write_config(sc->dev, cfg_rdw_p->cfg_id, cfg_rdw_p->cfg_val,
18971                             cfg_rdw_p->cfg_width);
18972             } else {
18973                 BLOGW(sc, "BXE_RDW_PCICFG ioctl wrong cmd passed\n");
18974             }
18975             break;
18976 
18977         case BXE_MAC_ADDR:
18978             mac_addr_p = (bxe_perm_mac_addr_t *)data;
18979             snprintf(mac_addr_p->mac_addr_str, sizeof(sc->mac_addr_str), "%s",
18980                 sc->mac_addr_str);
18981             break;
18982 
18983         case BXE_EEPROM:
18984             rval = bxe_eeprom_rd_wr(sc, (bxe_eeprom_t *)data);
18985             break;
18986 
18987 
18988         default:
18989             break;
18990     }
18991 
18992     return (rval);
18993 }
18994