xref: /freebsd/sys/dev/bxe/bxe.c (revision a5ff72cb0e51a7675d4e2b5810a2b6dad5b91960)
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 propogate 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_softc *sc;
2458     struct bxe_device_type *t;
2459     char *descbuf;
2460     uint16_t did, sdid, svid, vid;
2461 
2462     /* Find our device structure */
2463     sc = device_get_softc(dev);
2464     sc->dev = dev;
2465     t = bxe_devs;
2466 
2467     /* Get the data for the device to be probed. */
2468     vid  = pci_get_vendor(dev);
2469     did  = pci_get_device(dev);
2470     svid = pci_get_subvendor(dev);
2471     sdid = pci_get_subdevice(dev);
2472 
2473     BLOGD(sc, DBG_LOAD,
2474           "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
2475           "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
2476 
2477     /* Look through the list of known devices for a match. */
2478     while (t->bxe_name != NULL) {
2479         if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2480             ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2481             ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2482             descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2483             if (descbuf == NULL)
2484                 return (ENOMEM);
2485 
2486             /* Print out the device identity. */
2487             snprintf(descbuf, BXE_DEVDESC_MAX,
2488                      "%s (%c%d) BXE v:%s\n", t->bxe_name,
2489                      (((pci_read_config(dev, PCIR_REVID, 4) &
2490                         0xf0) >> 4) + 'A'),
2491                      (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2492                      BXE_DRIVER_VERSION);
2493 
2494             device_set_desc_copy(dev, descbuf);
2495             free(descbuf, M_TEMP);
2496             return (BUS_PROBE_DEFAULT);
2497         }
2498         t++;
2499     }
2500 
2501     return (ENXIO);
2502 }
2503 
2504 static void
2505 bxe_init_mutexes(struct bxe_softc *sc)
2506 {
2507 #ifdef BXE_CORE_LOCK_SX
2508     snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2509              "bxe%d_core_lock", sc->unit);
2510     sx_init(&sc->core_sx, sc->core_sx_name);
2511 #else
2512     snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2513              "bxe%d_core_lock", sc->unit);
2514     mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2515 #endif
2516 
2517     snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2518              "bxe%d_sp_lock", sc->unit);
2519     mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2520 
2521     snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2522              "bxe%d_dmae_lock", sc->unit);
2523     mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2524 
2525     snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2526              "bxe%d_phy_lock", sc->unit);
2527     mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2528 
2529     snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2530              "bxe%d_fwmb_lock", sc->unit);
2531     mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2532 
2533     snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2534              "bxe%d_print_lock", sc->unit);
2535     mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2536 
2537     snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2538              "bxe%d_stats_lock", sc->unit);
2539     mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2540 
2541     snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2542              "bxe%d_mcast_lock", sc->unit);
2543     mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2544 }
2545 
2546 static void
2547 bxe_release_mutexes(struct bxe_softc *sc)
2548 {
2549 #ifdef BXE_CORE_LOCK_SX
2550     sx_destroy(&sc->core_sx);
2551 #else
2552     if (mtx_initialized(&sc->core_mtx)) {
2553         mtx_destroy(&sc->core_mtx);
2554     }
2555 #endif
2556 
2557     if (mtx_initialized(&sc->sp_mtx)) {
2558         mtx_destroy(&sc->sp_mtx);
2559     }
2560 
2561     if (mtx_initialized(&sc->dmae_mtx)) {
2562         mtx_destroy(&sc->dmae_mtx);
2563     }
2564 
2565     if (mtx_initialized(&sc->port.phy_mtx)) {
2566         mtx_destroy(&sc->port.phy_mtx);
2567     }
2568 
2569     if (mtx_initialized(&sc->fwmb_mtx)) {
2570         mtx_destroy(&sc->fwmb_mtx);
2571     }
2572 
2573     if (mtx_initialized(&sc->print_mtx)) {
2574         mtx_destroy(&sc->print_mtx);
2575     }
2576 
2577     if (mtx_initialized(&sc->stats_mtx)) {
2578         mtx_destroy(&sc->stats_mtx);
2579     }
2580 
2581     if (mtx_initialized(&sc->mcast_mtx)) {
2582         mtx_destroy(&sc->mcast_mtx);
2583     }
2584 }
2585 
2586 static void
2587 bxe_tx_disable(struct bxe_softc* sc)
2588 {
2589     if_t ifp = sc->ifp;
2590 
2591     /* tell the stack the driver is stopped and TX queue is full */
2592     if (ifp !=  NULL) {
2593         if_setdrvflags(ifp, 0);
2594     }
2595 }
2596 
2597 static void
2598 bxe_drv_pulse(struct bxe_softc *sc)
2599 {
2600     SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2601              sc->fw_drv_pulse_wr_seq);
2602 }
2603 
2604 static inline uint16_t
2605 bxe_tx_avail(struct bxe_softc *sc,
2606              struct bxe_fastpath *fp)
2607 {
2608     int16_t  used;
2609     uint16_t prod;
2610     uint16_t cons;
2611 
2612     prod = fp->tx_bd_prod;
2613     cons = fp->tx_bd_cons;
2614 
2615     used = SUB_S16(prod, cons);
2616 
2617     return (int16_t)(sc->tx_ring_size) - used;
2618 }
2619 
2620 static inline int
2621 bxe_tx_queue_has_work(struct bxe_fastpath *fp)
2622 {
2623     uint16_t hw_cons;
2624 
2625     mb(); /* status block fields can change */
2626     hw_cons = le16toh(*fp->tx_cons_sb);
2627     return (hw_cons != fp->tx_pkt_cons);
2628 }
2629 
2630 static inline uint8_t
2631 bxe_has_tx_work(struct bxe_fastpath *fp)
2632 {
2633     /* expand this for multi-cos if ever supported */
2634     return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2635 }
2636 
2637 static inline int
2638 bxe_has_rx_work(struct bxe_fastpath *fp)
2639 {
2640     uint16_t rx_cq_cons_sb;
2641 
2642     mb(); /* status block fields can change */
2643     rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2644     if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2645         rx_cq_cons_sb++;
2646     return (fp->rx_cq_cons != rx_cq_cons_sb);
2647 }
2648 
2649 static void
2650 bxe_sp_event(struct bxe_softc    *sc,
2651              struct bxe_fastpath *fp,
2652              union eth_rx_cqe    *rr_cqe)
2653 {
2654     int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2655     int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2656     enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2657     struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2658 
2659     BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2660           fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2661 
2662     switch (command) {
2663     case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2664         BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2665         drv_cmd = ECORE_Q_CMD_UPDATE;
2666         break;
2667 
2668     case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2669         BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2670         drv_cmd = ECORE_Q_CMD_SETUP;
2671         break;
2672 
2673     case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2674         BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2675         drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2676         break;
2677 
2678     case (RAMROD_CMD_ID_ETH_HALT):
2679         BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2680         drv_cmd = ECORE_Q_CMD_HALT;
2681         break;
2682 
2683     case (RAMROD_CMD_ID_ETH_TERMINATE):
2684         BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2685         drv_cmd = ECORE_Q_CMD_TERMINATE;
2686         break;
2687 
2688     case (RAMROD_CMD_ID_ETH_EMPTY):
2689         BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2690         drv_cmd = ECORE_Q_CMD_EMPTY;
2691         break;
2692 
2693     default:
2694         BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2695               command, fp->index);
2696         return;
2697     }
2698 
2699     if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2700         q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2701         /*
2702          * q_obj->complete_cmd() failure means that this was
2703          * an unexpected completion.
2704          *
2705          * In this case we don't want to increase the sc->spq_left
2706          * because apparently we haven't sent this command the first
2707          * place.
2708          */
2709         // bxe_panic(sc, ("Unexpected SP completion\n"));
2710         return;
2711     }
2712 
2713     atomic_add_acq_long(&sc->cq_spq_left, 1);
2714 
2715     BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2716           atomic_load_acq_long(&sc->cq_spq_left));
2717 }
2718 
2719 /*
2720  * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2721  * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2722  * the current aggregation queue as in-progress.
2723  */
2724 static void
2725 bxe_tpa_start(struct bxe_softc            *sc,
2726               struct bxe_fastpath         *fp,
2727               uint16_t                    queue,
2728               uint16_t                    cons,
2729               uint16_t                    prod,
2730               struct eth_fast_path_rx_cqe *cqe)
2731 {
2732     struct bxe_sw_rx_bd tmp_bd;
2733     struct bxe_sw_rx_bd *rx_buf;
2734     struct eth_rx_bd *rx_bd;
2735     int max_agg_queues;
2736     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2737     uint16_t index;
2738 
2739     BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2740                        "cons=%d prod=%d\n",
2741           fp->index, queue, cons, prod);
2742 
2743     max_agg_queues = MAX_AGG_QS(sc);
2744 
2745     KASSERT((queue < max_agg_queues),
2746             ("fp[%02d] invalid aggr queue (%d >= %d)!",
2747              fp->index, queue, max_agg_queues));
2748 
2749     KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2750             ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2751              fp->index, queue));
2752 
2753     /* copy the existing mbuf and mapping from the TPA pool */
2754     tmp_bd = tpa_info->bd;
2755 
2756     if (tmp_bd.m == NULL) {
2757         uint32_t *tmp;
2758 
2759         tmp = (uint32_t *)cqe;
2760 
2761         BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2762               fp->index, queue, cons, prod);
2763         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2764             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2765 
2766         /* XXX Error handling? */
2767         return;
2768     }
2769 
2770     /* change the TPA queue to the start state */
2771     tpa_info->state            = BXE_TPA_STATE_START;
2772     tpa_info->placement_offset = cqe->placement_offset;
2773     tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2774     tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2775     tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2776 
2777     fp->rx_tpa_queue_used |= (1 << queue);
2778 
2779     /*
2780      * If all the buffer descriptors are filled with mbufs then fill in
2781      * the current consumer index with a new BD. Else if a maximum Rx
2782      * buffer limit is imposed then fill in the next producer index.
2783      */
2784     index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2785                 prod : cons;
2786 
2787     /* move the received mbuf and mapping to TPA pool */
2788     tpa_info->bd = fp->rx_mbuf_chain[cons];
2789 
2790     /* release any existing RX BD mbuf mappings */
2791     if (cons != index) {
2792         rx_buf = &fp->rx_mbuf_chain[cons];
2793 
2794         if (rx_buf->m_map != NULL) {
2795             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2796                             BUS_DMASYNC_POSTREAD);
2797             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2798         }
2799 
2800         /*
2801          * We get here when the maximum number of rx buffers is less than
2802          * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2803          * it out here without concern of a memory leak.
2804          */
2805         fp->rx_mbuf_chain[cons].m = NULL;
2806     }
2807 
2808     /* update the Rx SW BD with the mbuf info from the TPA pool */
2809     fp->rx_mbuf_chain[index] = tmp_bd;
2810 
2811     /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2812     rx_bd = &fp->rx_chain[index];
2813     rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2814     rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2815 }
2816 
2817 /*
2818  * When a TPA aggregation is completed, loop through the individual mbufs
2819  * of the aggregation, combining them into a single mbuf which will be sent
2820  * up the stack. Refill all freed SGEs with mbufs as we go along.
2821  */
2822 static int
2823 bxe_fill_frag_mbuf(struct bxe_softc          *sc,
2824                    struct bxe_fastpath       *fp,
2825                    struct bxe_sw_tpa_info    *tpa_info,
2826                    uint16_t                  queue,
2827                    uint16_t                  pages,
2828                    struct mbuf               *m,
2829 			       struct eth_end_agg_rx_cqe *cqe,
2830                    uint16_t                  cqe_idx)
2831 {
2832     struct mbuf *m_frag;
2833     uint32_t frag_len, frag_size, i;
2834     uint16_t sge_idx;
2835     int rc = 0;
2836     int j;
2837 
2838     frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2839 
2840     BLOGD(sc, DBG_LRO,
2841           "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2842           fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2843 
2844     /* make sure the aggregated frame is not too big to handle */
2845     if (pages > 8 * PAGES_PER_SGE) {
2846 
2847         uint32_t *tmp = (uint32_t *)cqe;
2848 
2849         BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2850                   "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2851               fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2852               tpa_info->len_on_bd, frag_size);
2853 
2854         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2855             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7));
2856 
2857         bxe_panic(sc, ("sge page count error\n"));
2858         return (EINVAL);
2859     }
2860 
2861     /*
2862      * Scan through the scatter gather list pulling individual mbufs into a
2863      * single mbuf for the host stack.
2864      */
2865     for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2866         sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2867 
2868         /*
2869          * Firmware gives the indices of the SGE as if the ring is an array
2870          * (meaning that the "next" element will consume 2 indices).
2871          */
2872         frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2873 
2874         BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2875                            "sge_idx=%d frag_size=%d frag_len=%d\n",
2876               fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2877 
2878         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2879 
2880         /* allocate a new mbuf for the SGE */
2881         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2882         if (rc) {
2883             /* Leave all remaining SGEs in the ring! */
2884             return (rc);
2885         }
2886 
2887         /* update the fragment length */
2888         m_frag->m_len = frag_len;
2889 
2890         /* concatenate the fragment to the head mbuf */
2891         m_cat(m, m_frag);
2892         fp->eth_q_stats.mbuf_alloc_sge--;
2893 
2894         /* update the TPA mbuf size and remaining fragment size */
2895         m->m_pkthdr.len += frag_len;
2896         frag_size -= frag_len;
2897     }
2898 
2899     BLOGD(sc, DBG_LRO,
2900           "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2901           fp->index, queue, frag_size);
2902 
2903     return (rc);
2904 }
2905 
2906 static inline void
2907 bxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2908 {
2909     int i, j;
2910 
2911     for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2912         int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2913 
2914         for (j = 0; j < 2; j++) {
2915             BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2916             idx--;
2917         }
2918     }
2919 }
2920 
2921 static inline void
2922 bxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2923 {
2924     /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2925     memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2926 
2927     /*
2928      * Clear the two last indices in the page to 1. These are the indices that
2929      * correspond to the "next" element, hence will never be indicated and
2930      * should be removed from the calculations.
2931      */
2932     bxe_clear_sge_mask_next_elems(fp);
2933 }
2934 
2935 static inline void
2936 bxe_update_last_max_sge(struct bxe_fastpath *fp,
2937                         uint16_t            idx)
2938 {
2939     uint16_t last_max = fp->last_max_sge;
2940 
2941     if (SUB_S16(idx, last_max) > 0) {
2942         fp->last_max_sge = idx;
2943     }
2944 }
2945 
2946 static inline void
2947 bxe_update_sge_prod(struct bxe_softc          *sc,
2948                     struct bxe_fastpath       *fp,
2949                     uint16_t                  sge_len,
2950                     union eth_sgl_or_raw_data *cqe)
2951 {
2952     uint16_t last_max, last_elem, first_elem;
2953     uint16_t delta = 0;
2954     uint16_t i;
2955 
2956     if (!sge_len) {
2957         return;
2958     }
2959 
2960     /* first mark all used pages */
2961     for (i = 0; i < sge_len; i++) {
2962         BIT_VEC64_CLEAR_BIT(fp->sge_mask,
2963                             RX_SGE(le16toh(cqe->sgl[i])));
2964     }
2965 
2966     BLOGD(sc, DBG_LRO,
2967           "fp[%02d] fp_cqe->sgl[%d] = %d\n",
2968           fp->index, sge_len - 1,
2969           le16toh(cqe->sgl[sge_len - 1]));
2970 
2971     /* assume that the last SGE index is the biggest */
2972     bxe_update_last_max_sge(fp,
2973                             le16toh(cqe->sgl[sge_len - 1]));
2974 
2975     last_max = RX_SGE(fp->last_max_sge);
2976     last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
2977     first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
2978 
2979     /* if ring is not full */
2980     if (last_elem + 1 != first_elem) {
2981         last_elem++;
2982     }
2983 
2984     /* now update the prod */
2985     for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
2986         if (__predict_true(fp->sge_mask[i])) {
2987             break;
2988         }
2989 
2990         fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
2991         delta += BIT_VEC64_ELEM_SZ;
2992     }
2993 
2994     if (delta > 0) {
2995         fp->rx_sge_prod += delta;
2996         /* clear page-end entries */
2997         bxe_clear_sge_mask_next_elems(fp);
2998     }
2999 
3000     BLOGD(sc, DBG_LRO,
3001           "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
3002           fp->index, fp->last_max_sge, fp->rx_sge_prod);
3003 }
3004 
3005 /*
3006  * The aggregation on the current TPA queue has completed. Pull the individual
3007  * mbuf fragments together into a single mbuf, perform all necessary checksum
3008  * calculations, and send the resuting mbuf to the stack.
3009  */
3010 static void
3011 bxe_tpa_stop(struct bxe_softc          *sc,
3012              struct bxe_fastpath       *fp,
3013              struct bxe_sw_tpa_info    *tpa_info,
3014              uint16_t                  queue,
3015              uint16_t                  pages,
3016 			 struct eth_end_agg_rx_cqe *cqe,
3017              uint16_t                  cqe_idx)
3018 {
3019     if_t ifp = sc->ifp;
3020     struct mbuf *m;
3021     int rc = 0;
3022 
3023     BLOGD(sc, DBG_LRO,
3024           "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3025           fp->index, queue, tpa_info->placement_offset,
3026           le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3027 
3028     m = tpa_info->bd.m;
3029 
3030     /* allocate a replacement before modifying existing mbuf */
3031     rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3032     if (rc) {
3033         /* drop the frame and log an error */
3034         fp->eth_q_stats.rx_soft_errors++;
3035         goto bxe_tpa_stop_exit;
3036     }
3037 
3038     /* we have a replacement, fixup the current mbuf */
3039     m_adj(m, tpa_info->placement_offset);
3040     m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3041 
3042     /* mark the checksums valid (taken care of by the firmware) */
3043     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3044     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3045     m->m_pkthdr.csum_data = 0xffff;
3046     m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3047                                CSUM_IP_VALID   |
3048                                CSUM_DATA_VALID |
3049                                CSUM_PSEUDO_HDR);
3050 
3051     /* aggregate all of the SGEs into a single mbuf */
3052     rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3053     if (rc) {
3054         /* drop the packet and log an error */
3055         fp->eth_q_stats.rx_soft_errors++;
3056         m_freem(m);
3057     } else {
3058         if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3059             m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3060             m->m_flags |= M_VLANTAG;
3061         }
3062 
3063         /* assign packet to this interface interface */
3064         if_setrcvif(m, ifp);
3065 
3066 #if __FreeBSD_version >= 800000
3067         /* specify what RSS queue was used for this flow */
3068         m->m_pkthdr.flowid = fp->index;
3069         BXE_SET_FLOWID(m);
3070 #endif
3071 
3072         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3073         fp->eth_q_stats.rx_tpa_pkts++;
3074 
3075         /* pass the frame to the stack */
3076         if_input(ifp, m);
3077     }
3078 
3079     /* we passed an mbuf up the stack or dropped the frame */
3080     fp->eth_q_stats.mbuf_alloc_tpa--;
3081 
3082 bxe_tpa_stop_exit:
3083 
3084     fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3085     fp->rx_tpa_queue_used &= ~(1 << queue);
3086 }
3087 
3088 static uint8_t
3089 bxe_service_rxsgl(
3090                  struct bxe_fastpath *fp,
3091                  uint16_t len,
3092                  uint16_t lenonbd,
3093                  struct mbuf *m,
3094                  struct eth_fast_path_rx_cqe *cqe_fp)
3095 {
3096     struct mbuf *m_frag;
3097     uint16_t frags, frag_len;
3098     uint16_t sge_idx = 0;
3099     uint16_t j;
3100     uint8_t i, rc = 0;
3101     uint32_t frag_size;
3102 
3103     /* adjust the mbuf */
3104     m->m_len = lenonbd;
3105 
3106     frag_size =  len - lenonbd;
3107     frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3108 
3109     for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3110         sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3111 
3112         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3113         frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3114         m_frag->m_len = frag_len;
3115 
3116        /* allocate a new mbuf for the SGE */
3117         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3118         if (rc) {
3119             /* Leave all remaining SGEs in the ring! */
3120             return (rc);
3121         }
3122         fp->eth_q_stats.mbuf_alloc_sge--;
3123 
3124         /* concatenate the fragment to the head mbuf */
3125         m_cat(m, m_frag);
3126 
3127         frag_size -= frag_len;
3128     }
3129 
3130     bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3131 
3132     return rc;
3133 }
3134 
3135 static uint8_t
3136 bxe_rxeof(struct bxe_softc    *sc,
3137           struct bxe_fastpath *fp)
3138 {
3139     if_t ifp = sc->ifp;
3140     uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3141     uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3142     int rx_pkts = 0;
3143     int rc = 0;
3144 
3145     BXE_FP_RX_LOCK(fp);
3146 
3147     /* CQ "next element" is of the size of the regular element */
3148     hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3149     if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3150         hw_cq_cons++;
3151     }
3152 
3153     bd_cons = fp->rx_bd_cons;
3154     bd_prod = fp->rx_bd_prod;
3155     bd_prod_fw = bd_prod;
3156     sw_cq_cons = fp->rx_cq_cons;
3157     sw_cq_prod = fp->rx_cq_prod;
3158 
3159     /*
3160      * Memory barrier necessary as speculative reads of the rx
3161      * buffer can be ahead of the index in the status block
3162      */
3163     rmb();
3164 
3165     BLOGD(sc, DBG_RX,
3166           "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3167           fp->index, hw_cq_cons, sw_cq_cons);
3168 
3169     while (sw_cq_cons != hw_cq_cons) {
3170         struct bxe_sw_rx_bd *rx_buf = NULL;
3171         union eth_rx_cqe *cqe;
3172         struct eth_fast_path_rx_cqe *cqe_fp;
3173         uint8_t cqe_fp_flags;
3174         enum eth_rx_cqe_type cqe_fp_type;
3175         uint16_t len, lenonbd,  pad;
3176         struct mbuf *m = NULL;
3177 
3178         comp_ring_cons = RCQ(sw_cq_cons);
3179         bd_prod = RX_BD(bd_prod);
3180         bd_cons = RX_BD(bd_cons);
3181 
3182         cqe          = &fp->rcq_chain[comp_ring_cons];
3183         cqe_fp       = &cqe->fast_path_cqe;
3184         cqe_fp_flags = cqe_fp->type_error_flags;
3185         cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3186 
3187         BLOGD(sc, DBG_RX,
3188               "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3189               "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3190               "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3191               fp->index,
3192               hw_cq_cons,
3193               sw_cq_cons,
3194               bd_prod,
3195               bd_cons,
3196               CQE_TYPE(cqe_fp_flags),
3197               cqe_fp_flags,
3198               cqe_fp->status_flags,
3199               le32toh(cqe_fp->rss_hash_result),
3200               le16toh(cqe_fp->vlan_tag),
3201               le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3202               le16toh(cqe_fp->len_on_bd));
3203 
3204         /* is this a slowpath msg? */
3205         if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3206             bxe_sp_event(sc, fp, cqe);
3207             goto next_cqe;
3208         }
3209 
3210         rx_buf = &fp->rx_mbuf_chain[bd_cons];
3211 
3212         if (!CQE_TYPE_FAST(cqe_fp_type)) {
3213             struct bxe_sw_tpa_info *tpa_info;
3214             uint16_t frag_size, pages;
3215             uint8_t queue;
3216 
3217             if (CQE_TYPE_START(cqe_fp_type)) {
3218                 bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3219                               bd_cons, bd_prod, cqe_fp);
3220                 m = NULL; /* packet not ready yet */
3221                 goto next_rx;
3222             }
3223 
3224             KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3225                     ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3226 
3227             queue = cqe->end_agg_cqe.queue_index;
3228             tpa_info = &fp->rx_tpa_info[queue];
3229 
3230             BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3231                   fp->index, queue);
3232 
3233             frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3234                          tpa_info->len_on_bd);
3235             pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3236 
3237             bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3238                          &cqe->end_agg_cqe, comp_ring_cons);
3239 
3240             bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3241 
3242             goto next_cqe;
3243         }
3244 
3245         /* non TPA */
3246 
3247         /* is this an error packet? */
3248         if (__predict_false(cqe_fp_flags &
3249                             ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3250             BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3251             fp->eth_q_stats.rx_soft_errors++;
3252             goto next_rx;
3253         }
3254 
3255         len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3256         lenonbd = le16toh(cqe_fp->len_on_bd);
3257         pad = cqe_fp->placement_offset;
3258 
3259         m = rx_buf->m;
3260 
3261         if (__predict_false(m == NULL)) {
3262             BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3263                   bd_cons, fp->index);
3264             goto next_rx;
3265         }
3266 
3267         /* XXX double copy if packet length under a threshold */
3268 
3269         /*
3270          * If all the buffer descriptors are filled with mbufs then fill in
3271          * the current consumer index with a new BD. Else if a maximum Rx
3272          * buffer limit is imposed then fill in the next producer index.
3273          */
3274         rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3275                                   (sc->max_rx_bufs != RX_BD_USABLE) ?
3276                                       bd_prod : bd_cons);
3277         if (rc != 0) {
3278 
3279             /* we simply reuse the received mbuf and don't post it to the stack */
3280             m = NULL;
3281 
3282             BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3283                   fp->index, rc);
3284             fp->eth_q_stats.rx_soft_errors++;
3285 
3286             if (sc->max_rx_bufs != RX_BD_USABLE) {
3287                 /* copy this consumer index to the producer index */
3288                 memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3289                        sizeof(struct bxe_sw_rx_bd));
3290                 memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3291             }
3292 
3293             goto next_rx;
3294         }
3295 
3296         /* current mbuf was detached from the bd */
3297         fp->eth_q_stats.mbuf_alloc_rx--;
3298 
3299         /* we allocated a replacement mbuf, fixup the current one */
3300         m_adj(m, pad);
3301         m->m_pkthdr.len = m->m_len = len;
3302 
3303         if ((len > 60) && (len > lenonbd)) {
3304             fp->eth_q_stats.rx_bxe_service_rxsgl++;
3305             rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3306             if (rc)
3307                 break;
3308             fp->eth_q_stats.rx_jumbo_sge_pkts++;
3309         } else if (lenonbd < len) {
3310             fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3311         }
3312 
3313         /* assign packet to this interface interface */
3314 	if_setrcvif(m, ifp);
3315 
3316         /* assume no hardware checksum has complated */
3317         m->m_pkthdr.csum_flags = 0;
3318 
3319         /* validate checksum if offload enabled */
3320         if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3321             /* check for a valid IP frame */
3322             if (!(cqe->fast_path_cqe.status_flags &
3323                   ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3324                 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3325                 if (__predict_false(cqe_fp_flags &
3326                                     ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3327                     fp->eth_q_stats.rx_hw_csum_errors++;
3328                 } else {
3329                     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3330                     m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3331                 }
3332             }
3333 
3334             /* check for a valid TCP/UDP frame */
3335             if (!(cqe->fast_path_cqe.status_flags &
3336                   ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3337                 if (__predict_false(cqe_fp_flags &
3338                                     ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3339                     fp->eth_q_stats.rx_hw_csum_errors++;
3340                 } else {
3341                     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3342                     m->m_pkthdr.csum_data = 0xFFFF;
3343                     m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3344                                                CSUM_PSEUDO_HDR);
3345                 }
3346             }
3347         }
3348 
3349         /* if there is a VLAN tag then flag that info */
3350         if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3351             m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3352             m->m_flags |= M_VLANTAG;
3353         }
3354 
3355 #if __FreeBSD_version >= 800000
3356         /* specify what RSS queue was used for this flow */
3357         m->m_pkthdr.flowid = fp->index;
3358         BXE_SET_FLOWID(m);
3359 #endif
3360 
3361 next_rx:
3362 
3363         bd_cons    = RX_BD_NEXT(bd_cons);
3364         bd_prod    = RX_BD_NEXT(bd_prod);
3365         bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3366 
3367         /* pass the frame to the stack */
3368         if (__predict_true(m != NULL)) {
3369             if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3370             rx_pkts++;
3371             if_input(ifp, m);
3372         }
3373 
3374 next_cqe:
3375 
3376         sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3377         sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3378 
3379         /* limit spinning on the queue */
3380         if (rc != 0)
3381             break;
3382 
3383         if (rx_pkts == sc->rx_budget) {
3384             fp->eth_q_stats.rx_budget_reached++;
3385             break;
3386         }
3387     } /* while work to do */
3388 
3389     fp->rx_bd_cons = bd_cons;
3390     fp->rx_bd_prod = bd_prod_fw;
3391     fp->rx_cq_cons = sw_cq_cons;
3392     fp->rx_cq_prod = sw_cq_prod;
3393 
3394     /* Update producers */
3395     bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3396 
3397     fp->eth_q_stats.rx_pkts += rx_pkts;
3398     fp->eth_q_stats.rx_calls++;
3399 
3400     BXE_FP_RX_UNLOCK(fp);
3401 
3402     return (sw_cq_cons != hw_cq_cons);
3403 }
3404 
3405 static uint16_t
3406 bxe_free_tx_pkt(struct bxe_softc    *sc,
3407                 struct bxe_fastpath *fp,
3408                 uint16_t            idx)
3409 {
3410     struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3411     struct eth_tx_start_bd *tx_start_bd;
3412     uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3413     uint16_t new_cons;
3414     int nbd;
3415 
3416     /* unmap the mbuf from non-paged memory */
3417     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3418 
3419     tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3420     nbd = le16toh(tx_start_bd->nbd) - 1;
3421 
3422     new_cons = (tx_buf->first_bd + nbd);
3423 
3424     /* free the mbuf */
3425     if (__predict_true(tx_buf->m != NULL)) {
3426         m_freem(tx_buf->m);
3427         fp->eth_q_stats.mbuf_alloc_tx--;
3428     } else {
3429         fp->eth_q_stats.tx_chain_lost_mbuf++;
3430     }
3431 
3432     tx_buf->m = NULL;
3433     tx_buf->first_bd = 0;
3434 
3435     return (new_cons);
3436 }
3437 
3438 /* transmit timeout watchdog */
3439 static int
3440 bxe_watchdog(struct bxe_softc    *sc,
3441              struct bxe_fastpath *fp)
3442 {
3443     BXE_FP_TX_LOCK(fp);
3444 
3445     if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3446         BXE_FP_TX_UNLOCK(fp);
3447         return (0);
3448     }
3449 
3450     BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3451     if(sc->trigger_grcdump) {
3452          /* taking grcdump */
3453          bxe_grc_dump(sc);
3454     }
3455 
3456     BXE_FP_TX_UNLOCK(fp);
3457 
3458     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_REINIT);
3459     taskqueue_enqueue(sc->chip_tq, &sc->chip_tq_task);
3460 
3461     return (-1);
3462 }
3463 
3464 /* processes transmit completions */
3465 static uint8_t
3466 bxe_txeof(struct bxe_softc    *sc,
3467           struct bxe_fastpath *fp)
3468 {
3469     if_t ifp = sc->ifp;
3470     uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3471     uint16_t tx_bd_avail;
3472 
3473     BXE_FP_TX_LOCK_ASSERT(fp);
3474 
3475     bd_cons = fp->tx_bd_cons;
3476     hw_cons = le16toh(*fp->tx_cons_sb);
3477     sw_cons = fp->tx_pkt_cons;
3478 
3479     while (sw_cons != hw_cons) {
3480         pkt_cons = TX_BD(sw_cons);
3481 
3482         BLOGD(sc, DBG_TX,
3483               "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3484               fp->index, hw_cons, sw_cons, pkt_cons);
3485 
3486         bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3487 
3488         sw_cons++;
3489     }
3490 
3491     fp->tx_pkt_cons = sw_cons;
3492     fp->tx_bd_cons  = bd_cons;
3493 
3494     BLOGD(sc, DBG_TX,
3495           "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3496           fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3497 
3498     mb();
3499 
3500     tx_bd_avail = bxe_tx_avail(sc, fp);
3501 
3502     if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3503         if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
3504     } else {
3505         if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3506     }
3507 
3508     if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3509         /* reset the watchdog timer if there are pending transmits */
3510         fp->watchdog_timer = BXE_TX_TIMEOUT;
3511         return (TRUE);
3512     } else {
3513         /* clear watchdog when there are no pending transmits */
3514         fp->watchdog_timer = 0;
3515         return (FALSE);
3516     }
3517 }
3518 
3519 static void
3520 bxe_drain_tx_queues(struct bxe_softc *sc)
3521 {
3522     struct bxe_fastpath *fp;
3523     int i, count;
3524 
3525     /* wait until all TX fastpath tasks have completed */
3526     for (i = 0; i < sc->num_queues; i++) {
3527         fp = &sc->fp[i];
3528 
3529         count = 1000;
3530 
3531         while (bxe_has_tx_work(fp)) {
3532 
3533             BXE_FP_TX_LOCK(fp);
3534             bxe_txeof(sc, fp);
3535             BXE_FP_TX_UNLOCK(fp);
3536 
3537             if (count == 0) {
3538                 BLOGE(sc, "Timeout waiting for fp[%d] "
3539                           "transmits to complete!\n", i);
3540                 bxe_panic(sc, ("tx drain failure\n"));
3541                 return;
3542             }
3543 
3544             count--;
3545             DELAY(1000);
3546             rmb();
3547         }
3548     }
3549 
3550     return;
3551 }
3552 
3553 static int
3554 bxe_del_all_macs(struct bxe_softc          *sc,
3555                  struct ecore_vlan_mac_obj *mac_obj,
3556                  int                       mac_type,
3557                  uint8_t                   wait_for_comp)
3558 {
3559     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3560     int rc;
3561 
3562     /* wait for completion of requested */
3563     if (wait_for_comp) {
3564         bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3565     }
3566 
3567     /* Set the mac type of addresses we want to clear */
3568     bxe_set_bit(mac_type, &vlan_mac_flags);
3569 
3570     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3571     if (rc < 0) {
3572         BLOGE(sc, "Failed to delete MACs (%d) mac_type %d wait_for_comp 0x%x\n",
3573             rc, mac_type, wait_for_comp);
3574     }
3575 
3576     return (rc);
3577 }
3578 
3579 static int
3580 bxe_fill_accept_flags(struct bxe_softc *sc,
3581                       uint32_t         rx_mode,
3582                       unsigned long    *rx_accept_flags,
3583                       unsigned long    *tx_accept_flags)
3584 {
3585     /* Clear the flags first */
3586     *rx_accept_flags = 0;
3587     *tx_accept_flags = 0;
3588 
3589     switch (rx_mode) {
3590     case BXE_RX_MODE_NONE:
3591         /*
3592          * 'drop all' supersedes any accept flags that may have been
3593          * passed to the function.
3594          */
3595         break;
3596 
3597     case BXE_RX_MODE_NORMAL:
3598         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3599         bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3600         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3601 
3602         /* internal switching mode */
3603         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3604         bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3605         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3606 
3607         break;
3608 
3609     case BXE_RX_MODE_ALLMULTI:
3610         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3611         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3612         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3613 
3614         /* internal switching mode */
3615         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3616         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3617         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3618 
3619         break;
3620 
3621     case BXE_RX_MODE_PROMISC:
3622         /*
3623          * According to deffinition of SI mode, iface in promisc mode
3624          * should receive matched and unmatched (in resolution of port)
3625          * unicast packets.
3626          */
3627         bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3628         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3629         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3630         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3631 
3632         /* internal switching mode */
3633         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3634         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3635 
3636         if (IS_MF_SI(sc)) {
3637             bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3638         } else {
3639             bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3640         }
3641 
3642         break;
3643 
3644     default:
3645         BLOGE(sc, "Unknown rx_mode (0x%x)\n", rx_mode);
3646         return (-1);
3647     }
3648 
3649     /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3650     if (rx_mode != BXE_RX_MODE_NONE) {
3651         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3652         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3653     }
3654 
3655     return (0);
3656 }
3657 
3658 static int
3659 bxe_set_q_rx_mode(struct bxe_softc *sc,
3660                   uint8_t          cl_id,
3661                   unsigned long    rx_mode_flags,
3662                   unsigned long    rx_accept_flags,
3663                   unsigned long    tx_accept_flags,
3664                   unsigned long    ramrod_flags)
3665 {
3666     struct ecore_rx_mode_ramrod_params ramrod_param;
3667     int rc;
3668 
3669     memset(&ramrod_param, 0, sizeof(ramrod_param));
3670 
3671     /* Prepare ramrod parameters */
3672     ramrod_param.cid = 0;
3673     ramrod_param.cl_id = cl_id;
3674     ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3675     ramrod_param.func_id = SC_FUNC(sc);
3676 
3677     ramrod_param.pstate = &sc->sp_state;
3678     ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3679 
3680     ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3681     ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3682 
3683     bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3684 
3685     ramrod_param.ramrod_flags = ramrod_flags;
3686     ramrod_param.rx_mode_flags = rx_mode_flags;
3687 
3688     ramrod_param.rx_accept_flags = rx_accept_flags;
3689     ramrod_param.tx_accept_flags = tx_accept_flags;
3690 
3691     rc = ecore_config_rx_mode(sc, &ramrod_param);
3692     if (rc < 0) {
3693         BLOGE(sc, "Set rx_mode %d cli_id 0x%x rx_mode_flags 0x%x "
3694             "rx_accept_flags 0x%x tx_accept_flags 0x%x "
3695             "ramrod_flags 0x%x rc %d failed\n", sc->rx_mode, cl_id,
3696             (uint32_t)rx_mode_flags, (uint32_t)rx_accept_flags,
3697             (uint32_t)tx_accept_flags, (uint32_t)ramrod_flags, rc);
3698         return (rc);
3699     }
3700 
3701     return (0);
3702 }
3703 
3704 static int
3705 bxe_set_storm_rx_mode(struct bxe_softc *sc)
3706 {
3707     unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3708     unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3709     int rc;
3710 
3711     rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3712                                &tx_accept_flags);
3713     if (rc) {
3714         return (rc);
3715     }
3716 
3717     bxe_set_bit(RAMROD_RX, &ramrod_flags);
3718     bxe_set_bit(RAMROD_TX, &ramrod_flags);
3719 
3720     /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3721     return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3722                               rx_accept_flags, tx_accept_flags,
3723                               ramrod_flags));
3724 }
3725 
3726 /* returns the "mcp load_code" according to global load_count array */
3727 static int
3728 bxe_nic_load_no_mcp(struct bxe_softc *sc)
3729 {
3730     int path = SC_PATH(sc);
3731     int port = SC_PORT(sc);
3732 
3733     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3734           path, load_count[path][0], load_count[path][1],
3735           load_count[path][2]);
3736     load_count[path][0]++;
3737     load_count[path][1 + port]++;
3738     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3739           path, load_count[path][0], load_count[path][1],
3740           load_count[path][2]);
3741     if (load_count[path][0] == 1) {
3742         return (FW_MSG_CODE_DRV_LOAD_COMMON);
3743     } else if (load_count[path][1 + port] == 1) {
3744         return (FW_MSG_CODE_DRV_LOAD_PORT);
3745     } else {
3746         return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3747     }
3748 }
3749 
3750 /* returns the "mcp load_code" according to global load_count array */
3751 static int
3752 bxe_nic_unload_no_mcp(struct bxe_softc *sc)
3753 {
3754     int port = SC_PORT(sc);
3755     int path = SC_PATH(sc);
3756 
3757     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3758           path, load_count[path][0], load_count[path][1],
3759           load_count[path][2]);
3760     load_count[path][0]--;
3761     load_count[path][1 + port]--;
3762     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3763           path, load_count[path][0], load_count[path][1],
3764           load_count[path][2]);
3765     if (load_count[path][0] == 0) {
3766         return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3767     } else if (load_count[path][1 + port] == 0) {
3768         return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3769     } else {
3770         return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3771     }
3772 }
3773 
3774 /* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3775 static uint32_t
3776 bxe_send_unload_req(struct bxe_softc *sc,
3777                     int              unload_mode)
3778 {
3779     uint32_t reset_code = 0;
3780 
3781     /* Select the UNLOAD request mode */
3782     if (unload_mode == UNLOAD_NORMAL) {
3783         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3784     } else {
3785         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3786     }
3787 
3788     /* Send the request to the MCP */
3789     if (!BXE_NOMCP(sc)) {
3790         reset_code = bxe_fw_command(sc, reset_code, 0);
3791     } else {
3792         reset_code = bxe_nic_unload_no_mcp(sc);
3793     }
3794 
3795     return (reset_code);
3796 }
3797 
3798 /* send UNLOAD_DONE command to the MCP */
3799 static void
3800 bxe_send_unload_done(struct bxe_softc *sc,
3801                      uint8_t          keep_link)
3802 {
3803     uint32_t reset_param =
3804         keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
3805 
3806     /* Report UNLOAD_DONE to MCP */
3807     if (!BXE_NOMCP(sc)) {
3808         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
3809     }
3810 }
3811 
3812 static int
3813 bxe_func_wait_started(struct bxe_softc *sc)
3814 {
3815     int tout = 50;
3816 
3817     if (!sc->port.pmf) {
3818         return (0);
3819     }
3820 
3821     /*
3822      * (assumption: No Attention from MCP at this stage)
3823      * PMF probably in the middle of TX disable/enable transaction
3824      * 1. Sync IRS for default SB
3825      * 2. Sync SP queue - this guarantees us that attention handling started
3826      * 3. Wait, that TX disable/enable transaction completes
3827      *
3828      * 1+2 guarantee that if DCBX attention was scheduled it already changed
3829      * pending bit of transaction from STARTED-->TX_STOPPED, if we already
3830      * received completion for the transaction the state is TX_STOPPED.
3831      * State will return to STARTED after completion of TX_STOPPED-->STARTED
3832      * transaction.
3833      */
3834 
3835     /* XXX make sure default SB ISR is done */
3836     /* need a way to synchronize an irq (intr_mtx?) */
3837 
3838     /* XXX flush any work queues */
3839 
3840     while (ecore_func_get_state(sc, &sc->func_obj) !=
3841            ECORE_F_STATE_STARTED && tout--) {
3842         DELAY(20000);
3843     }
3844 
3845     if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
3846         /*
3847          * Failed to complete the transaction in a "good way"
3848          * Force both transactions with CLR bit.
3849          */
3850         struct ecore_func_state_params func_params = { NULL };
3851 
3852         BLOGE(sc, "Unexpected function state! "
3853                   "Forcing STARTED-->TX_STOPPED-->STARTED\n");
3854 
3855         func_params.f_obj = &sc->func_obj;
3856         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3857 
3858         /* STARTED-->TX_STOPPED */
3859         func_params.cmd = ECORE_F_CMD_TX_STOP;
3860         ecore_func_state_change(sc, &func_params);
3861 
3862         /* TX_STOPPED-->STARTED */
3863         func_params.cmd = ECORE_F_CMD_TX_START;
3864         return (ecore_func_state_change(sc, &func_params));
3865     }
3866 
3867     return (0);
3868 }
3869 
3870 static int
3871 bxe_stop_queue(struct bxe_softc *sc,
3872                int              index)
3873 {
3874     struct bxe_fastpath *fp = &sc->fp[index];
3875     struct ecore_queue_state_params q_params = { NULL };
3876     int rc;
3877 
3878     BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
3879 
3880     q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
3881     /* We want to wait for completion in this context */
3882     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
3883 
3884     /* Stop the primary connection: */
3885 
3886     /* ...halt the connection */
3887     q_params.cmd = ECORE_Q_CMD_HALT;
3888     rc = ecore_queue_state_change(sc, &q_params);
3889     if (rc) {
3890         return (rc);
3891     }
3892 
3893     /* ...terminate the connection */
3894     q_params.cmd = ECORE_Q_CMD_TERMINATE;
3895     memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
3896     q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
3897     rc = ecore_queue_state_change(sc, &q_params);
3898     if (rc) {
3899         return (rc);
3900     }
3901 
3902     /* ...delete cfc entry */
3903     q_params.cmd = ECORE_Q_CMD_CFC_DEL;
3904     memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
3905     q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
3906     return (ecore_queue_state_change(sc, &q_params));
3907 }
3908 
3909 /* wait for the outstanding SP commands */
3910 static inline uint8_t
3911 bxe_wait_sp_comp(struct bxe_softc *sc,
3912                  unsigned long    mask)
3913 {
3914     unsigned long tmp;
3915     int tout = 5000; /* wait for 5 secs tops */
3916 
3917     while (tout--) {
3918         mb();
3919         if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
3920             return (TRUE);
3921         }
3922 
3923         DELAY(1000);
3924     }
3925 
3926     mb();
3927 
3928     tmp = atomic_load_acq_long(&sc->sp_state);
3929     if (tmp & mask) {
3930         BLOGE(sc, "Filtering completion timed out: "
3931                   "sp_state 0x%lx, mask 0x%lx\n",
3932               tmp, mask);
3933         return (FALSE);
3934     }
3935 
3936     return (FALSE);
3937 }
3938 
3939 static int
3940 bxe_func_stop(struct bxe_softc *sc)
3941 {
3942     struct ecore_func_state_params func_params = { NULL };
3943     int rc;
3944 
3945     /* prepare parameters for function state transitions */
3946     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3947     func_params.f_obj = &sc->func_obj;
3948     func_params.cmd = ECORE_F_CMD_STOP;
3949 
3950     /*
3951      * Try to stop the function the 'good way'. If it fails (in case
3952      * of a parity error during bxe_chip_cleanup()) and we are
3953      * not in a debug mode, perform a state transaction in order to
3954      * enable further HW_RESET transaction.
3955      */
3956     rc = ecore_func_state_change(sc, &func_params);
3957     if (rc) {
3958         BLOGE(sc, "FUNC_STOP ramrod failed. "
3959                   "Running a dry transaction (%d)\n", rc);
3960         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3961         return (ecore_func_state_change(sc, &func_params));
3962     }
3963 
3964     return (0);
3965 }
3966 
3967 static int
3968 bxe_reset_hw(struct bxe_softc *sc,
3969              uint32_t         load_code)
3970 {
3971     struct ecore_func_state_params func_params = { NULL };
3972 
3973     /* Prepare parameters for function state transitions */
3974     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3975 
3976     func_params.f_obj = &sc->func_obj;
3977     func_params.cmd = ECORE_F_CMD_HW_RESET;
3978 
3979     func_params.params.hw_init.load_phase = load_code;
3980 
3981     return (ecore_func_state_change(sc, &func_params));
3982 }
3983 
3984 static void
3985 bxe_int_disable_sync(struct bxe_softc *sc,
3986                      int              disable_hw)
3987 {
3988     if (disable_hw) {
3989         /* prevent the HW from sending interrupts */
3990         bxe_int_disable(sc);
3991     }
3992 
3993     /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
3994     /* make sure all ISRs are done */
3995 
3996     /* XXX make sure sp_task is not running */
3997     /* cancel and flush work queues */
3998 }
3999 
4000 static void
4001 bxe_chip_cleanup(struct bxe_softc *sc,
4002                  uint32_t         unload_mode,
4003                  uint8_t          keep_link)
4004 {
4005     int port = SC_PORT(sc);
4006     struct ecore_mcast_ramrod_params rparam = { NULL };
4007     uint32_t reset_code;
4008     int i, rc = 0;
4009 
4010     bxe_drain_tx_queues(sc);
4011 
4012     /* give HW time to discard old tx messages */
4013     DELAY(1000);
4014 
4015     /* Clean all ETH MACs */
4016     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4017     if (rc < 0) {
4018         BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4019     }
4020 
4021     /* Clean up UC list  */
4022     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4023     if (rc < 0) {
4024         BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4025     }
4026 
4027     /* Disable LLH */
4028     if (!CHIP_IS_E1(sc)) {
4029         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4030     }
4031 
4032     /* Set "drop all" to stop Rx */
4033 
4034     /*
4035      * We need to take the BXE_MCAST_LOCK() here in order to prevent
4036      * a race between the completion code and this code.
4037      */
4038     BXE_MCAST_LOCK(sc);
4039 
4040     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4041         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4042     } else {
4043         bxe_set_storm_rx_mode(sc);
4044     }
4045 
4046     /* Clean up multicast configuration */
4047     rparam.mcast_obj = &sc->mcast_obj;
4048     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4049     if (rc < 0) {
4050         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4051     }
4052 
4053     BXE_MCAST_UNLOCK(sc);
4054 
4055     // XXX bxe_iov_chip_cleanup(sc);
4056 
4057     /*
4058      * Send the UNLOAD_REQUEST to the MCP. This will return if
4059      * this function should perform FUNCTION, PORT, or COMMON HW
4060      * reset.
4061      */
4062     reset_code = bxe_send_unload_req(sc, unload_mode);
4063 
4064     /*
4065      * (assumption: No Attention from MCP at this stage)
4066      * PMF probably in the middle of TX disable/enable transaction
4067      */
4068     rc = bxe_func_wait_started(sc);
4069     if (rc) {
4070         BLOGE(sc, "bxe_func_wait_started failed (%d)\n", rc);
4071     }
4072 
4073     /*
4074      * Close multi and leading connections
4075      * Completions for ramrods are collected in a synchronous way
4076      */
4077     for (i = 0; i < sc->num_queues; i++) {
4078         if (bxe_stop_queue(sc, i)) {
4079             goto unload_error;
4080         }
4081     }
4082 
4083     /*
4084      * If SP settings didn't get completed so far - something
4085      * very wrong has happen.
4086      */
4087     if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4088         BLOGE(sc, "Common slow path ramrods got stuck!(%d)\n", rc);
4089     }
4090 
4091 unload_error:
4092 
4093     rc = bxe_func_stop(sc);
4094     if (rc) {
4095         BLOGE(sc, "Function stop failed!(%d)\n", rc);
4096     }
4097 
4098     /* disable HW interrupts */
4099     bxe_int_disable_sync(sc, TRUE);
4100 
4101     /* detach interrupts */
4102     bxe_interrupt_detach(sc);
4103 
4104     /* Reset the chip */
4105     rc = bxe_reset_hw(sc, reset_code);
4106     if (rc) {
4107         BLOGE(sc, "Hardware reset failed(%d)\n", rc);
4108     }
4109 
4110     /* Report UNLOAD_DONE to MCP */
4111     bxe_send_unload_done(sc, keep_link);
4112 }
4113 
4114 static void
4115 bxe_disable_close_the_gate(struct bxe_softc *sc)
4116 {
4117     uint32_t val;
4118     int port = SC_PORT(sc);
4119 
4120     BLOGD(sc, DBG_LOAD,
4121           "Disabling 'close the gates'\n");
4122 
4123     if (CHIP_IS_E1(sc)) {
4124         uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4125                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
4126         val = REG_RD(sc, addr);
4127         val &= ~(0x300);
4128         REG_WR(sc, addr, val);
4129     } else {
4130         val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4131         val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4132                  MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4133         REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4134     }
4135 }
4136 
4137 /*
4138  * Cleans the object that have internal lists without sending
4139  * ramrods. Should be run when interrutps are disabled.
4140  */
4141 static void
4142 bxe_squeeze_objects(struct bxe_softc *sc)
4143 {
4144     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4145     struct ecore_mcast_ramrod_params rparam = { NULL };
4146     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4147     int rc;
4148 
4149     /* Cleanup MACs' object first... */
4150 
4151     /* Wait for completion of requested */
4152     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4153     /* Perform a dry cleanup */
4154     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4155 
4156     /* Clean ETH primary MAC */
4157     bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4158     rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4159                              &ramrod_flags);
4160     if (rc != 0) {
4161         BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4162     }
4163 
4164     /* Cleanup UC list */
4165     vlan_mac_flags = 0;
4166     bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4167     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4168                              &ramrod_flags);
4169     if (rc != 0) {
4170         BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4171     }
4172 
4173     /* Now clean mcast object... */
4174 
4175     rparam.mcast_obj = &sc->mcast_obj;
4176     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4177 
4178     /* Add a DEL command... */
4179     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4180     if (rc < 0) {
4181         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4182     }
4183 
4184     /* now wait until all pending commands are cleared */
4185 
4186     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4187     while (rc != 0) {
4188         if (rc < 0) {
4189             BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4190             return;
4191         }
4192 
4193         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4194     }
4195 }
4196 
4197 /* stop the controller */
4198 static __noinline int
4199 bxe_nic_unload(struct bxe_softc *sc,
4200                uint32_t         unload_mode,
4201                uint8_t          keep_link)
4202 {
4203     uint8_t global = FALSE;
4204     uint32_t val;
4205     int i;
4206 
4207     BXE_CORE_LOCK_ASSERT(sc);
4208 
4209     if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
4210 
4211     for (i = 0; i < sc->num_queues; i++) {
4212         struct bxe_fastpath *fp;
4213 
4214         fp = &sc->fp[i];
4215         BXE_FP_TX_LOCK(fp);
4216         BXE_FP_TX_UNLOCK(fp);
4217     }
4218 
4219     BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4220 
4221     /* mark driver as unloaded in shmem2 */
4222     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4223         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4224         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4225                   val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4226     }
4227 
4228     if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4229         (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4230         /*
4231          * We can get here if the driver has been unloaded
4232          * during parity error recovery and is either waiting for a
4233          * leader to complete or for other functions to unload and
4234          * then ifconfig down has been issued. In this case we want to
4235          * unload and let other functions to complete a recovery
4236          * process.
4237          */
4238         sc->recovery_state = BXE_RECOVERY_DONE;
4239         sc->is_leader = 0;
4240         bxe_release_leader_lock(sc);
4241         mb();
4242 
4243         BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4244         BLOGE(sc, "Can't unload in closed or error state recover_state 0x%x"
4245             " state = 0x%x\n", sc->recovery_state, sc->state);
4246         return (-1);
4247     }
4248 
4249     /*
4250      * Nothing to do during unload if previous bxe_nic_load()
4251      * did not completed succesfully - all resourses are released.
4252      */
4253     if ((sc->state == BXE_STATE_CLOSED) ||
4254         (sc->state == BXE_STATE_ERROR)) {
4255         return (0);
4256     }
4257 
4258     sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4259     mb();
4260 
4261     /* stop tx */
4262     bxe_tx_disable(sc);
4263 
4264     sc->rx_mode = BXE_RX_MODE_NONE;
4265     /* XXX set rx mode ??? */
4266 
4267     if (IS_PF(sc) && !sc->grcdump_done) {
4268         /* set ALWAYS_ALIVE bit in shmem */
4269         sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4270 
4271         bxe_drv_pulse(sc);
4272 
4273         bxe_stats_handle(sc, STATS_EVENT_STOP);
4274         bxe_save_statistics(sc);
4275     }
4276 
4277     /* wait till consumers catch up with producers in all queues */
4278     bxe_drain_tx_queues(sc);
4279 
4280     /* if VF indicate to PF this function is going down (PF will delete sp
4281      * elements and clear initializations
4282      */
4283     if (IS_VF(sc)) {
4284         ; /* bxe_vfpf_close_vf(sc); */
4285     } else if (unload_mode != UNLOAD_RECOVERY) {
4286         /* if this is a normal/close unload need to clean up chip */
4287         if (!sc->grcdump_done)
4288             bxe_chip_cleanup(sc, unload_mode, keep_link);
4289     } else {
4290         /* Send the UNLOAD_REQUEST to the MCP */
4291         bxe_send_unload_req(sc, unload_mode);
4292 
4293         /*
4294          * Prevent transactions to host from the functions on the
4295          * engine that doesn't reset global blocks in case of global
4296          * attention once gloabl blocks are reset and gates are opened
4297          * (the engine which leader will perform the recovery
4298          * last).
4299          */
4300         if (!CHIP_IS_E1x(sc)) {
4301             bxe_pf_disable(sc);
4302         }
4303 
4304         /* disable HW interrupts */
4305         bxe_int_disable_sync(sc, TRUE);
4306 
4307         /* detach interrupts */
4308         bxe_interrupt_detach(sc);
4309 
4310         /* Report UNLOAD_DONE to MCP */
4311         bxe_send_unload_done(sc, FALSE);
4312     }
4313 
4314     /*
4315      * At this stage no more interrupts will arrive so we may safely clean
4316      * the queue'able objects here in case they failed to get cleaned so far.
4317      */
4318     if (IS_PF(sc)) {
4319         bxe_squeeze_objects(sc);
4320     }
4321 
4322     /* There should be no more pending SP commands at this stage */
4323     sc->sp_state = 0;
4324 
4325     sc->port.pmf = 0;
4326 
4327     bxe_free_fp_buffers(sc);
4328 
4329     if (IS_PF(sc)) {
4330         bxe_free_mem(sc);
4331     }
4332 
4333     bxe_free_fw_stats_mem(sc);
4334 
4335     sc->state = BXE_STATE_CLOSED;
4336 
4337     /*
4338      * Check if there are pending parity attentions. If there are - set
4339      * RECOVERY_IN_PROGRESS.
4340      */
4341     if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4342         bxe_set_reset_in_progress(sc);
4343 
4344         /* Set RESET_IS_GLOBAL if needed */
4345         if (global) {
4346             bxe_set_reset_global(sc);
4347         }
4348     }
4349 
4350     /*
4351      * The last driver must disable a "close the gate" if there is no
4352      * parity attention or "process kill" pending.
4353      */
4354     if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4355         bxe_reset_is_done(sc, SC_PATH(sc))) {
4356         bxe_disable_close_the_gate(sc);
4357     }
4358 
4359     BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4360 
4361     return (0);
4362 }
4363 
4364 /*
4365  * Called by the OS to set various media options (i.e. link, speed, etc.) when
4366  * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4367  */
4368 static int
4369 bxe_ifmedia_update(struct ifnet  *ifp)
4370 {
4371     struct bxe_softc *sc = (struct bxe_softc *)if_getsoftc(ifp);
4372     struct ifmedia *ifm;
4373 
4374     ifm = &sc->ifmedia;
4375 
4376     /* We only support Ethernet media type. */
4377     if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4378         return (EINVAL);
4379     }
4380 
4381     switch (IFM_SUBTYPE(ifm->ifm_media)) {
4382     case IFM_AUTO:
4383          break;
4384     case IFM_10G_CX4:
4385     case IFM_10G_SR:
4386     case IFM_10G_T:
4387     case IFM_10G_TWINAX:
4388     default:
4389         /* We don't support changing the media type. */
4390         BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4391               IFM_SUBTYPE(ifm->ifm_media));
4392         return (EINVAL);
4393     }
4394 
4395     return (0);
4396 }
4397 
4398 /*
4399  * Called by the OS to get the current media status (i.e. link, speed, etc.).
4400  */
4401 static void
4402 bxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4403 {
4404     struct bxe_softc *sc = if_getsoftc(ifp);
4405 
4406     /* Report link down if the driver isn't running. */
4407     if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
4408         ifmr->ifm_active |= IFM_NONE;
4409         return;
4410     }
4411 
4412     /* Setup the default interface info. */
4413     ifmr->ifm_status = IFM_AVALID;
4414     ifmr->ifm_active = IFM_ETHER;
4415 
4416     if (sc->link_vars.link_up) {
4417         ifmr->ifm_status |= IFM_ACTIVE;
4418     } else {
4419         ifmr->ifm_active |= IFM_NONE;
4420         return;
4421     }
4422 
4423     ifmr->ifm_active |= sc->media;
4424 
4425     if (sc->link_vars.duplex == DUPLEX_FULL) {
4426         ifmr->ifm_active |= IFM_FDX;
4427     } else {
4428         ifmr->ifm_active |= IFM_HDX;
4429     }
4430 }
4431 
4432 static int
4433 bxe_ioctl_nvram(struct bxe_softc *sc,
4434                 uint32_t         priv_op,
4435                 struct ifreq     *ifr)
4436 {
4437     struct bxe_nvram_data nvdata_base;
4438     struct bxe_nvram_data *nvdata;
4439     int len;
4440     int error = 0;
4441 
4442     copyin(ifr->ifr_data, &nvdata_base, sizeof(nvdata_base));
4443 
4444     len = (sizeof(struct bxe_nvram_data) +
4445            nvdata_base.len -
4446            sizeof(uint32_t));
4447 
4448     if (len > sizeof(struct bxe_nvram_data)) {
4449         if ((nvdata = (struct bxe_nvram_data *)
4450                  malloc(len, M_DEVBUF,
4451                         (M_NOWAIT | M_ZERO))) == NULL) {
4452             BLOGE(sc, "BXE_IOC_RD_NVRAM malloc failed priv_op 0x%x "
4453                 " len = 0x%x\n", priv_op, len);
4454             return (1);
4455         }
4456         memcpy(nvdata, &nvdata_base, sizeof(struct bxe_nvram_data));
4457     } else {
4458         nvdata = &nvdata_base;
4459     }
4460 
4461     if (priv_op == BXE_IOC_RD_NVRAM) {
4462         BLOGD(sc, DBG_IOCTL, "IOC_RD_NVRAM 0x%x %d\n",
4463               nvdata->offset, nvdata->len);
4464         error = bxe_nvram_read(sc,
4465                                nvdata->offset,
4466                                (uint8_t *)nvdata->value,
4467                                nvdata->len);
4468         copyout(nvdata, ifr->ifr_data, len);
4469     } else { /* BXE_IOC_WR_NVRAM */
4470         BLOGD(sc, DBG_IOCTL, "IOC_WR_NVRAM 0x%x %d\n",
4471               nvdata->offset, nvdata->len);
4472         copyin(ifr->ifr_data, nvdata, len);
4473         error = bxe_nvram_write(sc,
4474                                 nvdata->offset,
4475                                 (uint8_t *)nvdata->value,
4476                                 nvdata->len);
4477     }
4478 
4479     if (len > sizeof(struct bxe_nvram_data)) {
4480         free(nvdata, M_DEVBUF);
4481     }
4482 
4483     return (error);
4484 }
4485 
4486 static int
4487 bxe_ioctl_stats_show(struct bxe_softc *sc,
4488                      uint32_t         priv_op,
4489                      struct ifreq     *ifr)
4490 {
4491     const size_t str_size   = (BXE_NUM_ETH_STATS * STAT_NAME_LEN);
4492     const size_t stats_size = (BXE_NUM_ETH_STATS * sizeof(uint64_t));
4493     caddr_t p_tmp;
4494     uint32_t *offset;
4495     int i;
4496 
4497     switch (priv_op)
4498     {
4499     case BXE_IOC_STATS_SHOW_NUM:
4500         memset(ifr->ifr_data, 0, sizeof(union bxe_stats_show_data));
4501         ((union bxe_stats_show_data *)ifr->ifr_data)->desc.num =
4502             BXE_NUM_ETH_STATS;
4503         ((union bxe_stats_show_data *)ifr->ifr_data)->desc.len =
4504             STAT_NAME_LEN;
4505         return (0);
4506 
4507     case BXE_IOC_STATS_SHOW_STR:
4508         memset(ifr->ifr_data, 0, str_size);
4509         p_tmp = ifr->ifr_data;
4510         for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4511             strcpy(p_tmp, bxe_eth_stats_arr[i].string);
4512             p_tmp += STAT_NAME_LEN;
4513         }
4514         return (0);
4515 
4516     case BXE_IOC_STATS_SHOW_CNT:
4517         memset(ifr->ifr_data, 0, stats_size);
4518         p_tmp = ifr->ifr_data;
4519         for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4520             offset = ((uint32_t *)&sc->eth_stats +
4521                       bxe_eth_stats_arr[i].offset);
4522             switch (bxe_eth_stats_arr[i].size) {
4523             case 4:
4524                 *((uint64_t *)p_tmp) = (uint64_t)*offset;
4525                 break;
4526             case 8:
4527                 *((uint64_t *)p_tmp) = HILO_U64(*offset, *(offset + 1));
4528                 break;
4529             default:
4530                 *((uint64_t *)p_tmp) = 0;
4531             }
4532             p_tmp += sizeof(uint64_t);
4533         }
4534         return (0);
4535 
4536     default:
4537         return (-1);
4538     }
4539 }
4540 
4541 static void
4542 bxe_handle_chip_tq(void *context,
4543                    int  pending)
4544 {
4545     struct bxe_softc *sc = (struct bxe_softc *)context;
4546     long work = atomic_load_acq_long(&sc->chip_tq_flags);
4547 
4548     switch (work)
4549     {
4550 
4551     case CHIP_TQ_REINIT:
4552         if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
4553             /* restart the interface */
4554             BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4555             bxe_periodic_stop(sc);
4556             BXE_CORE_LOCK(sc);
4557             bxe_stop_locked(sc);
4558             bxe_init_locked(sc);
4559             BXE_CORE_UNLOCK(sc);
4560         }
4561         break;
4562 
4563     default:
4564         break;
4565     }
4566 }
4567 
4568 /*
4569  * Handles any IOCTL calls from the operating system.
4570  *
4571  * Returns:
4572  *   0 = Success, >0 Failure
4573  */
4574 static int
4575 bxe_ioctl(if_t ifp,
4576           u_long       command,
4577           caddr_t      data)
4578 {
4579     struct bxe_softc *sc = if_getsoftc(ifp);
4580     struct ifreq *ifr = (struct ifreq *)data;
4581     struct bxe_nvram_data *nvdata;
4582     uint32_t priv_op;
4583     int mask = 0;
4584     int reinit = 0;
4585     int error = 0;
4586 
4587     int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4588     int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4589 
4590     switch (command)
4591     {
4592     case SIOCSIFMTU:
4593         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4594               ifr->ifr_mtu);
4595 
4596         if (sc->mtu == ifr->ifr_mtu) {
4597             /* nothing to change */
4598             break;
4599         }
4600 
4601         if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4602             BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4603                   ifr->ifr_mtu, mtu_min, mtu_max);
4604             error = EINVAL;
4605             break;
4606         }
4607 
4608         atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4609                              (unsigned long)ifr->ifr_mtu);
4610 	/*
4611         atomic_store_rel_long((volatile unsigned long *)&if_getmtu(ifp),
4612                               (unsigned long)ifr->ifr_mtu);
4613 	XXX - Not sure why it needs to be atomic
4614 	*/
4615 	if_setmtu(ifp, ifr->ifr_mtu);
4616         reinit = 1;
4617         break;
4618 
4619     case SIOCSIFFLAGS:
4620         /* toggle the interface state up or down */
4621         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4622 
4623 	BXE_CORE_LOCK(sc);
4624         /* check if the interface is up */
4625         if (if_getflags(ifp) & IFF_UP) {
4626             if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4627                 /* set the receive mode flags */
4628                 bxe_set_rx_mode(sc);
4629             } else if(sc->state != BXE_STATE_DISABLED) {
4630 		bxe_init_locked(sc);
4631             }
4632         } else {
4633             if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4634 		bxe_periodic_stop(sc);
4635 		bxe_stop_locked(sc);
4636             }
4637         }
4638 	BXE_CORE_UNLOCK(sc);
4639 
4640         break;
4641 
4642     case SIOCADDMULTI:
4643     case SIOCDELMULTI:
4644         /* add/delete multicast addresses */
4645         BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4646 
4647         /* check if the interface is up */
4648         if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4649             /* set the receive mode flags */
4650 	    BXE_CORE_LOCK(sc);
4651             bxe_set_rx_mode(sc);
4652 	    BXE_CORE_UNLOCK(sc);
4653         }
4654 
4655         break;
4656 
4657     case SIOCSIFCAP:
4658         /* find out which capabilities have changed */
4659         mask = (ifr->ifr_reqcap ^ if_getcapenable(ifp));
4660 
4661         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4662               mask);
4663 
4664         /* toggle the LRO capabilites enable flag */
4665         if (mask & IFCAP_LRO) {
4666 	    if_togglecapenable(ifp, IFCAP_LRO);
4667             BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4668                   (if_getcapenable(ifp) & IFCAP_LRO) ? "ON" : "OFF");
4669             reinit = 1;
4670         }
4671 
4672         /* toggle the TXCSUM checksum capabilites enable flag */
4673         if (mask & IFCAP_TXCSUM) {
4674 	    if_togglecapenable(ifp, IFCAP_TXCSUM);
4675             BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4676                   (if_getcapenable(ifp) & IFCAP_TXCSUM) ? "ON" : "OFF");
4677             if (if_getcapenable(ifp) & IFCAP_TXCSUM) {
4678                 if_sethwassistbits(ifp, (CSUM_IP      |
4679                                     CSUM_TCP      |
4680                                     CSUM_UDP      |
4681                                     CSUM_TSO      |
4682                                     CSUM_TCP_IPV6 |
4683                                     CSUM_UDP_IPV6), 0);
4684             } else {
4685 		if_clearhwassist(ifp); /* XXX */
4686             }
4687         }
4688 
4689         /* toggle the RXCSUM checksum capabilities enable flag */
4690         if (mask & IFCAP_RXCSUM) {
4691 	    if_togglecapenable(ifp, IFCAP_RXCSUM);
4692             BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4693                   (if_getcapenable(ifp) & IFCAP_RXCSUM) ? "ON" : "OFF");
4694             if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
4695                 if_sethwassistbits(ifp, (CSUM_IP      |
4696                                     CSUM_TCP      |
4697                                     CSUM_UDP      |
4698                                     CSUM_TSO      |
4699                                     CSUM_TCP_IPV6 |
4700                                     CSUM_UDP_IPV6), 0);
4701             } else {
4702 		if_clearhwassist(ifp); /* XXX */
4703             }
4704         }
4705 
4706         /* toggle TSO4 capabilities enabled flag */
4707         if (mask & IFCAP_TSO4) {
4708             if_togglecapenable(ifp, IFCAP_TSO4);
4709             BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4710                   (if_getcapenable(ifp) & IFCAP_TSO4) ? "ON" : "OFF");
4711         }
4712 
4713         /* toggle TSO6 capabilities enabled flag */
4714         if (mask & IFCAP_TSO6) {
4715 	    if_togglecapenable(ifp, IFCAP_TSO6);
4716             BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4717                   (if_getcapenable(ifp) & IFCAP_TSO6) ? "ON" : "OFF");
4718         }
4719 
4720         /* toggle VLAN_HWTSO capabilities enabled flag */
4721         if (mask & IFCAP_VLAN_HWTSO) {
4722 
4723 	    if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
4724             BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4725                   (if_getcapenable(ifp) & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4726         }
4727 
4728         /* toggle VLAN_HWCSUM capabilities enabled flag */
4729         if (mask & IFCAP_VLAN_HWCSUM) {
4730             /* XXX investigate this... */
4731             BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4732             error = EINVAL;
4733         }
4734 
4735         /* toggle VLAN_MTU capabilities enable flag */
4736         if (mask & IFCAP_VLAN_MTU) {
4737             /* XXX investigate this... */
4738             BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4739             error = EINVAL;
4740         }
4741 
4742         /* toggle VLAN_HWTAGGING capabilities enabled flag */
4743         if (mask & IFCAP_VLAN_HWTAGGING) {
4744             /* XXX investigate this... */
4745             BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4746             error = EINVAL;
4747         }
4748 
4749         /* toggle VLAN_HWFILTER capabilities enabled flag */
4750         if (mask & IFCAP_VLAN_HWFILTER) {
4751             /* XXX investigate this... */
4752             BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4753             error = EINVAL;
4754         }
4755 
4756         /* XXX not yet...
4757          * IFCAP_WOL_MAGIC
4758          */
4759 
4760         break;
4761 
4762     case SIOCSIFMEDIA:
4763     case SIOCGIFMEDIA:
4764         /* set/get interface media */
4765         BLOGD(sc, DBG_IOCTL,
4766               "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4767               (command & 0xff));
4768         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4769         break;
4770 
4771     case SIOCGPRIVATE_0:
4772         copyin(ifr->ifr_data, &priv_op, sizeof(priv_op));
4773 
4774         switch (priv_op)
4775         {
4776         case BXE_IOC_RD_NVRAM:
4777         case BXE_IOC_WR_NVRAM:
4778             nvdata = (struct bxe_nvram_data *)ifr->ifr_data;
4779             BLOGD(sc, DBG_IOCTL,
4780                   "Received Private NVRAM ioctl addr=0x%x size=%u\n",
4781                   nvdata->offset, nvdata->len);
4782             error = bxe_ioctl_nvram(sc, priv_op, ifr);
4783             break;
4784 
4785         case BXE_IOC_STATS_SHOW_NUM:
4786         case BXE_IOC_STATS_SHOW_STR:
4787         case BXE_IOC_STATS_SHOW_CNT:
4788             BLOGD(sc, DBG_IOCTL, "Received Private Stats ioctl (%d)\n",
4789                   priv_op);
4790             error = bxe_ioctl_stats_show(sc, priv_op, ifr);
4791             break;
4792 
4793         default:
4794             BLOGW(sc, "Received Private Unknown ioctl (%d)\n", priv_op);
4795             error = EINVAL;
4796             break;
4797         }
4798 
4799         break;
4800 
4801     default:
4802         BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4803               (command & 0xff));
4804         error = ether_ioctl(ifp, command, data);
4805         break;
4806     }
4807 
4808     if (reinit && (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
4809         BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4810               "Re-initializing hardware from IOCTL change\n");
4811 	bxe_periodic_stop(sc);
4812 	BXE_CORE_LOCK(sc);
4813 	bxe_stop_locked(sc);
4814 	bxe_init_locked(sc);
4815 	BXE_CORE_UNLOCK(sc);
4816     }
4817 
4818     return (error);
4819 }
4820 
4821 static __noinline void
4822 bxe_dump_mbuf(struct bxe_softc *sc,
4823               struct mbuf      *m,
4824               uint8_t          contents)
4825 {
4826     char * type;
4827     int i = 0;
4828 
4829     if (!(sc->debug & DBG_MBUF)) {
4830         return;
4831     }
4832 
4833     if (m == NULL) {
4834         BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4835         return;
4836     }
4837 
4838     while (m) {
4839 
4840 #if __FreeBSD_version >= 1000000
4841         BLOGD(sc, DBG_MBUF,
4842               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4843               i, m, m->m_len, m->m_flags, M_FLAG_BITS, m->m_data);
4844 
4845         if (m->m_flags & M_PKTHDR) {
4846              BLOGD(sc, DBG_MBUF,
4847                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4848                    i, m->m_pkthdr.len, m->m_flags, M_FLAG_BITS,
4849                    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
4850         }
4851 #else
4852         BLOGD(sc, DBG_MBUF,
4853               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4854               i, m, m->m_len, m->m_flags,
4855               "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
4856 
4857         if (m->m_flags & M_PKTHDR) {
4858              BLOGD(sc, DBG_MBUF,
4859                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4860                    i, m->m_pkthdr.len, m->m_flags,
4861                    "\20\12M_BCAST\13M_MCAST\14M_FRAG"
4862                    "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
4863                    "\22M_PROMISC\23M_NOFREE",
4864                    (int)m->m_pkthdr.csum_flags,
4865                    "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
4866                    "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
4867                    "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
4868                    "\14CSUM_PSEUDO_HDR");
4869         }
4870 #endif /* #if __FreeBSD_version >= 1000000 */
4871 
4872         if (m->m_flags & M_EXT) {
4873             switch (m->m_ext.ext_type) {
4874             case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4875             case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4876             case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4877             case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4878             case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4879             case EXT_PACKET:     type = "EXT_PACKET";     break;
4880             case EXT_MBUF:       type = "EXT_MBUF";       break;
4881             case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4882             case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4883             case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4884             case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4885             default:             type = "UNKNOWN";        break;
4886             }
4887 
4888             BLOGD(sc, DBG_MBUF,
4889                   "%02d: - m_ext: %p ext_size=%d type=%s\n",
4890                   i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4891         }
4892 
4893         if (contents) {
4894             bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4895         }
4896 
4897         m = m->m_next;
4898         i++;
4899     }
4900 }
4901 
4902 /*
4903  * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4904  * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4905  * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4906  * The headers comes in a seperate bd in FreeBSD so 13-3=10.
4907  * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4908  */
4909 static int
4910 bxe_chktso_window(struct bxe_softc  *sc,
4911                   int               nsegs,
4912                   bus_dma_segment_t *segs,
4913                   struct mbuf       *m)
4914 {
4915     uint32_t num_wnds, wnd_size, wnd_sum;
4916     int32_t frag_idx, wnd_idx;
4917     unsigned short lso_mss;
4918     int defrag;
4919 
4920     defrag = 0;
4921     wnd_sum = 0;
4922     wnd_size = 10;
4923     num_wnds = nsegs - wnd_size;
4924     lso_mss = htole16(m->m_pkthdr.tso_segsz);
4925 
4926     /*
4927      * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4928      * first window sum of data while skipping the first assuming it is the
4929      * header in FreeBSD.
4930      */
4931     for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4932         wnd_sum += htole16(segs[frag_idx].ds_len);
4933     }
4934 
4935     /* check the first 10 bd window size */
4936     if (wnd_sum < lso_mss) {
4937         return (1);
4938     }
4939 
4940     /* run through the windows */
4941     for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4942         /* subtract the first mbuf->m_len of the last wndw(-header) */
4943         wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4944         /* add the next mbuf len to the len of our new window */
4945         wnd_sum += htole16(segs[frag_idx].ds_len);
4946         if (wnd_sum < lso_mss) {
4947             return (1);
4948         }
4949     }
4950 
4951     return (0);
4952 }
4953 
4954 static uint8_t
4955 bxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4956                     struct mbuf         *m,
4957                     uint32_t            *parsing_data)
4958 {
4959     struct ether_vlan_header *eh = NULL;
4960     struct ip *ip4 = NULL;
4961     struct ip6_hdr *ip6 = NULL;
4962     caddr_t ip = NULL;
4963     struct tcphdr *th = NULL;
4964     int e_hlen, ip_hlen, l4_off;
4965     uint16_t proto;
4966 
4967     if (m->m_pkthdr.csum_flags == CSUM_IP) {
4968         /* no L4 checksum offload needed */
4969         return (0);
4970     }
4971 
4972     /* get the Ethernet header */
4973     eh = mtod(m, struct ether_vlan_header *);
4974 
4975     /* handle VLAN encapsulation if present */
4976     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4977         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4978         proto  = ntohs(eh->evl_proto);
4979     } else {
4980         e_hlen = ETHER_HDR_LEN;
4981         proto  = ntohs(eh->evl_encap_proto);
4982     }
4983 
4984     switch (proto) {
4985     case ETHERTYPE_IP:
4986         /* get the IP header, if mbuf len < 20 then header in next mbuf */
4987         ip4 = (m->m_len < sizeof(struct ip)) ?
4988                   (struct ip *)m->m_next->m_data :
4989                   (struct ip *)(m->m_data + e_hlen);
4990         /* ip_hl is number of 32-bit words */
4991         ip_hlen = (ip4->ip_hl << 2);
4992         ip = (caddr_t)ip4;
4993         break;
4994     case ETHERTYPE_IPV6:
4995         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4996         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4997                   (struct ip6_hdr *)m->m_next->m_data :
4998                   (struct ip6_hdr *)(m->m_data + e_hlen);
4999         /* XXX cannot support offload with IPv6 extensions */
5000         ip_hlen = sizeof(struct ip6_hdr);
5001         ip = (caddr_t)ip6;
5002         break;
5003     default:
5004         /* We can't offload in this case... */
5005         /* XXX error stat ??? */
5006         return (0);
5007     }
5008 
5009     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5010     l4_off = (e_hlen + ip_hlen);
5011 
5012     *parsing_data |=
5013         (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
5014          ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
5015 
5016     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5017                                   CSUM_TSO |
5018                                   CSUM_TCP_IPV6)) {
5019         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5020         th = (struct tcphdr *)(ip + ip_hlen);
5021         /* th_off is number of 32-bit words */
5022         *parsing_data |= ((th->th_off <<
5023                            ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
5024                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
5025         return (l4_off + (th->th_off << 2)); /* entire header length */
5026     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5027                                          CSUM_UDP_IPV6)) {
5028         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5029         return (l4_off + sizeof(struct udphdr)); /* entire header length */
5030     } else {
5031         /* XXX error stat ??? */
5032         return (0);
5033     }
5034 }
5035 
5036 static uint8_t
5037 bxe_set_pbd_csum(struct bxe_fastpath        *fp,
5038                  struct mbuf                *m,
5039                  struct eth_tx_parse_bd_e1x *pbd)
5040 {
5041     struct ether_vlan_header *eh = NULL;
5042     struct ip *ip4 = NULL;
5043     struct ip6_hdr *ip6 = NULL;
5044     caddr_t ip = NULL;
5045     struct tcphdr *th = NULL;
5046     struct udphdr *uh = NULL;
5047     int e_hlen, ip_hlen;
5048     uint16_t proto;
5049     uint8_t hlen;
5050     uint16_t tmp_csum;
5051     uint32_t *tmp_uh;
5052 
5053     /* get the Ethernet header */
5054     eh = mtod(m, struct ether_vlan_header *);
5055 
5056     /* handle VLAN encapsulation if present */
5057     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
5058         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5059         proto  = ntohs(eh->evl_proto);
5060     } else {
5061         e_hlen = ETHER_HDR_LEN;
5062         proto  = ntohs(eh->evl_encap_proto);
5063     }
5064 
5065     switch (proto) {
5066     case ETHERTYPE_IP:
5067         /* get the IP header, if mbuf len < 20 then header in next mbuf */
5068         ip4 = (m->m_len < sizeof(struct ip)) ?
5069                   (struct ip *)m->m_next->m_data :
5070                   (struct ip *)(m->m_data + e_hlen);
5071         /* ip_hl is number of 32-bit words */
5072         ip_hlen = (ip4->ip_hl << 1);
5073         ip = (caddr_t)ip4;
5074         break;
5075     case ETHERTYPE_IPV6:
5076         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
5077         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
5078                   (struct ip6_hdr *)m->m_next->m_data :
5079                   (struct ip6_hdr *)(m->m_data + e_hlen);
5080         /* XXX cannot support offload with IPv6 extensions */
5081         ip_hlen = (sizeof(struct ip6_hdr) >> 1);
5082         ip = (caddr_t)ip6;
5083         break;
5084     default:
5085         /* We can't offload in this case... */
5086         /* XXX error stat ??? */
5087         return (0);
5088     }
5089 
5090     hlen = (e_hlen >> 1);
5091 
5092     /* note that rest of global_data is indirectly zeroed here */
5093     if (m->m_flags & M_VLANTAG) {
5094         pbd->global_data =
5095             htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
5096     } else {
5097         pbd->global_data = htole16(hlen);
5098     }
5099 
5100     pbd->ip_hlen_w = ip_hlen;
5101 
5102     hlen += pbd->ip_hlen_w;
5103 
5104     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5105 
5106     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5107                                   CSUM_TSO |
5108                                   CSUM_TCP_IPV6)) {
5109         th = (struct tcphdr *)(ip + (ip_hlen << 1));
5110         /* th_off is number of 32-bit words */
5111         hlen += (uint16_t)(th->th_off << 1);
5112     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5113                                          CSUM_UDP_IPV6)) {
5114         uh = (struct udphdr *)(ip + (ip_hlen << 1));
5115         hlen += (sizeof(struct udphdr) / 2);
5116     } else {
5117         /* valid case as only CSUM_IP was set */
5118         return (0);
5119     }
5120 
5121     pbd->total_hlen_w = htole16(hlen);
5122 
5123     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5124                                   CSUM_TSO |
5125                                   CSUM_TCP_IPV6)) {
5126         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5127         pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5128     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5129                                          CSUM_UDP_IPV6)) {
5130         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5131 
5132         /*
5133          * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5134          * checksums and does not know anything about the UDP header and where
5135          * the checksum field is located. It only knows about TCP. Therefore
5136          * we "lie" to the hardware for outgoing UDP packets w/ checksum
5137          * offload. Since the checksum field offset for TCP is 16 bytes and
5138          * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5139          * bytes less than the start of the UDP header. This allows the
5140          * hardware to write the checksum in the correct spot. But the
5141          * hardware will compute a checksum which includes the last 10 bytes
5142          * of the IP header. To correct this we tweak the stack computed
5143          * pseudo checksum by folding in the calculation of the inverse
5144          * checksum for those final 10 bytes of the IP header. This allows
5145          * the correct checksum to be computed by the hardware.
5146          */
5147 
5148         /* set pointer 10 bytes before UDP header */
5149         tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5150 
5151         /* calculate a pseudo header checksum over the first 10 bytes */
5152         tmp_csum = in_pseudo(*tmp_uh,
5153                              *(tmp_uh + 1),
5154                              *(uint16_t *)(tmp_uh + 2));
5155 
5156         pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5157     }
5158 
5159     return (hlen * 2); /* entire header length, number of bytes */
5160 }
5161 
5162 static void
5163 bxe_set_pbd_lso_e2(struct mbuf *m,
5164                    uint32_t    *parsing_data)
5165 {
5166     *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5167                        ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5168                       ETH_TX_PARSE_BD_E2_LSO_MSS);
5169 
5170     /* XXX test for IPv6 with extension header... */
5171 }
5172 
5173 static void
5174 bxe_set_pbd_lso(struct mbuf                *m,
5175                 struct eth_tx_parse_bd_e1x *pbd)
5176 {
5177     struct ether_vlan_header *eh = NULL;
5178     struct ip *ip = NULL;
5179     struct tcphdr *th = NULL;
5180     int e_hlen;
5181 
5182     /* get the Ethernet header */
5183     eh = mtod(m, struct ether_vlan_header *);
5184 
5185     /* handle VLAN encapsulation if present */
5186     e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5187                  (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5188 
5189     /* get the IP and TCP header, with LSO entire header in first mbuf */
5190     /* XXX assuming IPv4 */
5191     ip = (struct ip *)(m->m_data + e_hlen);
5192     th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5193 
5194     pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5195     pbd->tcp_send_seq = ntohl(th->th_seq);
5196     pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5197 
5198 #if 1
5199         /* XXX IPv4 */
5200         pbd->ip_id = ntohs(ip->ip_id);
5201         pbd->tcp_pseudo_csum =
5202             ntohs(in_pseudo(ip->ip_src.s_addr,
5203                             ip->ip_dst.s_addr,
5204                             htons(IPPROTO_TCP)));
5205 #else
5206         /* XXX IPv6 */
5207         pbd->tcp_pseudo_csum =
5208             ntohs(in_pseudo(&ip6->ip6_src,
5209                             &ip6->ip6_dst,
5210                             htons(IPPROTO_TCP)));
5211 #endif
5212 
5213     pbd->global_data |=
5214         htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5215 }
5216 
5217 /*
5218  * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5219  * visible to the controller.
5220  *
5221  * If an mbuf is submitted to this routine and cannot be given to the
5222  * controller (e.g. it has too many fragments) then the function may free
5223  * the mbuf and return to the caller.
5224  *
5225  * Returns:
5226  *   0 = Success, !0 = Failure
5227  *   Note the side effect that an mbuf may be freed if it causes a problem.
5228  */
5229 static int
5230 bxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5231 {
5232     bus_dma_segment_t segs[32];
5233     struct mbuf *m0;
5234     struct bxe_sw_tx_bd *tx_buf;
5235     struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5236     struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5237     /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5238     struct eth_tx_bd *tx_data_bd;
5239     struct eth_tx_bd *tx_total_pkt_size_bd;
5240     struct eth_tx_start_bd *tx_start_bd;
5241     uint16_t bd_prod, pkt_prod, total_pkt_size;
5242     uint8_t mac_type;
5243     int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5244     struct bxe_softc *sc;
5245     uint16_t tx_bd_avail;
5246     struct ether_vlan_header *eh;
5247     uint32_t pbd_e2_parsing_data = 0;
5248     uint8_t hlen = 0;
5249     int tmp_bd;
5250     int i;
5251 
5252     sc = fp->sc;
5253 
5254 #if __FreeBSD_version >= 800000
5255     M_ASSERTPKTHDR(*m_head);
5256 #endif /* #if __FreeBSD_version >= 800000 */
5257 
5258     m0 = *m_head;
5259     rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5260     tx_start_bd = NULL;
5261     tx_data_bd = NULL;
5262     tx_total_pkt_size_bd = NULL;
5263 
5264     /* get the H/W pointer for packets and BDs */
5265     pkt_prod = fp->tx_pkt_prod;
5266     bd_prod = fp->tx_bd_prod;
5267 
5268     mac_type = UNICAST_ADDRESS;
5269 
5270     /* map the mbuf into the next open DMAable memory */
5271     tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5272     error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5273                                     tx_buf->m_map, m0,
5274                                     segs, &nsegs, BUS_DMA_NOWAIT);
5275 
5276     /* mapping errors */
5277     if(__predict_false(error != 0)) {
5278         fp->eth_q_stats.tx_dma_mapping_failure++;
5279         if (error == ENOMEM) {
5280             /* resource issue, try again later */
5281             rc = ENOMEM;
5282         } else if (error == EFBIG) {
5283             /* possibly recoverable with defragmentation */
5284             fp->eth_q_stats.mbuf_defrag_attempts++;
5285             m0 = m_defrag(*m_head, M_NOWAIT);
5286             if (m0 == NULL) {
5287                 fp->eth_q_stats.mbuf_defrag_failures++;
5288                 rc = ENOBUFS;
5289             } else {
5290                 /* defrag successful, try mapping again */
5291                 *m_head = m0;
5292                 error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5293                                                 tx_buf->m_map, m0,
5294                                                 segs, &nsegs, BUS_DMA_NOWAIT);
5295                 if (error) {
5296                     fp->eth_q_stats.tx_dma_mapping_failure++;
5297                     rc = error;
5298                 }
5299             }
5300         } else {
5301             /* unknown, unrecoverable mapping error */
5302             BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5303             bxe_dump_mbuf(sc, m0, FALSE);
5304             rc = error;
5305         }
5306 
5307         goto bxe_tx_encap_continue;
5308     }
5309 
5310     tx_bd_avail = bxe_tx_avail(sc, fp);
5311 
5312     /* make sure there is enough room in the send queue */
5313     if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5314         /* Recoverable, try again later. */
5315         fp->eth_q_stats.tx_hw_queue_full++;
5316         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5317         rc = ENOMEM;
5318         goto bxe_tx_encap_continue;
5319     }
5320 
5321     /* capture the current H/W TX chain high watermark */
5322     if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5323                         (TX_BD_USABLE - tx_bd_avail))) {
5324         fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5325     }
5326 
5327     /* make sure it fits in the packet window */
5328     if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5329         /*
5330          * The mbuf may be to big for the controller to handle. If the frame
5331          * is a TSO frame we'll need to do an additional check.
5332          */
5333         if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5334             if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5335                 goto bxe_tx_encap_continue; /* OK to send */
5336             } else {
5337                 fp->eth_q_stats.tx_window_violation_tso++;
5338             }
5339         } else {
5340             fp->eth_q_stats.tx_window_violation_std++;
5341         }
5342 
5343         /* lets try to defragment this mbuf and remap it */
5344         fp->eth_q_stats.mbuf_defrag_attempts++;
5345         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5346 
5347         m0 = m_defrag(*m_head, M_NOWAIT);
5348         if (m0 == NULL) {
5349             fp->eth_q_stats.mbuf_defrag_failures++;
5350             /* Ugh, just drop the frame... :( */
5351             rc = ENOBUFS;
5352         } else {
5353             /* defrag successful, try mapping again */
5354             *m_head = m0;
5355             error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5356                                             tx_buf->m_map, m0,
5357                                             segs, &nsegs, BUS_DMA_NOWAIT);
5358             if (error) {
5359                 fp->eth_q_stats.tx_dma_mapping_failure++;
5360                 /* No sense in trying to defrag/copy chain, drop it. :( */
5361                 rc = error;
5362             }
5363             else {
5364                 /* if the chain is still too long then drop it */
5365                 if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5366                     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5367                     rc = ENODEV;
5368                 }
5369             }
5370         }
5371     }
5372 
5373 bxe_tx_encap_continue:
5374 
5375     /* Check for errors */
5376     if (rc) {
5377         if (rc == ENOMEM) {
5378             /* recoverable try again later  */
5379         } else {
5380             fp->eth_q_stats.tx_soft_errors++;
5381             fp->eth_q_stats.mbuf_alloc_tx--;
5382             m_freem(*m_head);
5383             *m_head = NULL;
5384         }
5385 
5386         return (rc);
5387     }
5388 
5389     /* set flag according to packet type (UNICAST_ADDRESS is default) */
5390     if (m0->m_flags & M_BCAST) {
5391         mac_type = BROADCAST_ADDRESS;
5392     } else if (m0->m_flags & M_MCAST) {
5393         mac_type = MULTICAST_ADDRESS;
5394     }
5395 
5396     /* store the mbuf into the mbuf ring */
5397     tx_buf->m        = m0;
5398     tx_buf->first_bd = fp->tx_bd_prod;
5399     tx_buf->flags    = 0;
5400 
5401     /* prepare the first transmit (start) BD for the mbuf */
5402     tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5403 
5404     BLOGD(sc, DBG_TX,
5405           "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5406           pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5407 
5408     tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5409     tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5410     tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5411     total_pkt_size += tx_start_bd->nbytes;
5412     tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5413 
5414     tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5415 
5416     /* all frames have at least Start BD + Parsing BD */
5417     nbds = nsegs + 1;
5418     tx_start_bd->nbd = htole16(nbds);
5419 
5420     if (m0->m_flags & M_VLANTAG) {
5421         tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5422         tx_start_bd->bd_flags.as_bitfield |=
5423             (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5424     } else {
5425         /* vf tx, start bd must hold the ethertype for fw to enforce it */
5426         if (IS_VF(sc)) {
5427             /* map ethernet header to find type and header length */
5428             eh = mtod(m0, struct ether_vlan_header *);
5429             tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5430         } else {
5431             /* used by FW for packet accounting */
5432             tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5433         }
5434     }
5435 
5436     /*
5437      * add a parsing BD from the chain. The parsing BD is always added
5438      * though it is only used for TSO and chksum
5439      */
5440     bd_prod = TX_BD_NEXT(bd_prod);
5441 
5442     if (m0->m_pkthdr.csum_flags) {
5443         if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5444             fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5445             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5446         }
5447 
5448         if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5449             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5450                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5451         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5452             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5453                                                   ETH_TX_BD_FLAGS_IS_UDP |
5454                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5455         } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5456                    (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5457             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5458         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5459             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5460                                                   ETH_TX_BD_FLAGS_IS_UDP);
5461         }
5462     }
5463 
5464     if (!CHIP_IS_E1x(sc)) {
5465         pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5466         memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5467 
5468         if (m0->m_pkthdr.csum_flags) {
5469             hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5470         }
5471 
5472         SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5473                  mac_type);
5474     } else {
5475         uint16_t global_data = 0;
5476 
5477         pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5478         memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5479 
5480         if (m0->m_pkthdr.csum_flags) {
5481             hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5482         }
5483 
5484         SET_FLAG(global_data,
5485                  ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5486         pbd_e1x->global_data |= htole16(global_data);
5487     }
5488 
5489     /* setup the parsing BD with TSO specific info */
5490     if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5491         fp->eth_q_stats.tx_ofld_frames_lso++;
5492         tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5493 
5494         if (__predict_false(tx_start_bd->nbytes > hlen)) {
5495             fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5496 
5497             /* split the first BD into header/data making the fw job easy */
5498             nbds++;
5499             tx_start_bd->nbd = htole16(nbds);
5500             tx_start_bd->nbytes = htole16(hlen);
5501 
5502             bd_prod = TX_BD_NEXT(bd_prod);
5503 
5504             /* new transmit BD after the tx_parse_bd */
5505             tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5506             tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5507             tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5508             tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5509             if (tx_total_pkt_size_bd == NULL) {
5510                 tx_total_pkt_size_bd = tx_data_bd;
5511             }
5512 
5513             BLOGD(sc, DBG_TX,
5514                   "TSO split header size is %d (%x:%x) nbds %d\n",
5515                   le16toh(tx_start_bd->nbytes),
5516                   le32toh(tx_start_bd->addr_hi),
5517                   le32toh(tx_start_bd->addr_lo),
5518                   nbds);
5519         }
5520 
5521         if (!CHIP_IS_E1x(sc)) {
5522             bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5523         } else {
5524             bxe_set_pbd_lso(m0, pbd_e1x);
5525         }
5526     }
5527 
5528     if (pbd_e2_parsing_data) {
5529         pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5530     }
5531 
5532     /* prepare remaining BDs, start tx bd contains first seg/frag */
5533     for (i = 1; i < nsegs ; i++) {
5534         bd_prod = TX_BD_NEXT(bd_prod);
5535         tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5536         tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5537         tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5538         tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5539         if (tx_total_pkt_size_bd == NULL) {
5540             tx_total_pkt_size_bd = tx_data_bd;
5541         }
5542         total_pkt_size += tx_data_bd->nbytes;
5543     }
5544 
5545     BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5546 
5547     if (tx_total_pkt_size_bd != NULL) {
5548         tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5549     }
5550 
5551     if (__predict_false(sc->debug & DBG_TX)) {
5552         tmp_bd = tx_buf->first_bd;
5553         for (i = 0; i < nbds; i++)
5554         {
5555             if (i == 0) {
5556                 BLOGD(sc, DBG_TX,
5557                       "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5558                       "bd_flags=0x%x hdr_nbds=%d\n",
5559                       tx_start_bd,
5560                       tmp_bd,
5561                       le16toh(tx_start_bd->nbd),
5562                       le16toh(tx_start_bd->vlan_or_ethertype),
5563                       tx_start_bd->bd_flags.as_bitfield,
5564                       (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5565             } else if (i == 1) {
5566                 if (pbd_e1x) {
5567                     BLOGD(sc, DBG_TX,
5568                           "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5569                           "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5570                           "tcp_seq=%u total_hlen_w=%u\n",
5571                           pbd_e1x,
5572                           tmp_bd,
5573                           pbd_e1x->global_data,
5574                           pbd_e1x->ip_hlen_w,
5575                           pbd_e1x->ip_id,
5576                           pbd_e1x->lso_mss,
5577                           pbd_e1x->tcp_flags,
5578                           pbd_e1x->tcp_pseudo_csum,
5579                           pbd_e1x->tcp_send_seq,
5580                           le16toh(pbd_e1x->total_hlen_w));
5581                 } else { /* if (pbd_e2) */
5582                     BLOGD(sc, DBG_TX,
5583                           "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5584                           "src=%02x:%02x:%02x parsing_data=0x%x\n",
5585                           pbd_e2,
5586                           tmp_bd,
5587                           pbd_e2->data.mac_addr.dst_hi,
5588                           pbd_e2->data.mac_addr.dst_mid,
5589                           pbd_e2->data.mac_addr.dst_lo,
5590                           pbd_e2->data.mac_addr.src_hi,
5591                           pbd_e2->data.mac_addr.src_mid,
5592                           pbd_e2->data.mac_addr.src_lo,
5593                           pbd_e2->parsing_data);
5594                 }
5595             }
5596 
5597             if (i != 1) { /* skip parse db as it doesn't hold data */
5598                 tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5599                 BLOGD(sc, DBG_TX,
5600                       "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5601                       tx_data_bd,
5602                       tmp_bd,
5603                       le16toh(tx_data_bd->nbytes),
5604                       le32toh(tx_data_bd->addr_hi),
5605                       le32toh(tx_data_bd->addr_lo));
5606             }
5607 
5608             tmp_bd = TX_BD_NEXT(tmp_bd);
5609         }
5610     }
5611 
5612     BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5613 
5614     /* update TX BD producer index value for next TX */
5615     bd_prod = TX_BD_NEXT(bd_prod);
5616 
5617     /*
5618      * If the chain of tx_bd's describing this frame is adjacent to or spans
5619      * an eth_tx_next_bd element then we need to increment the nbds value.
5620      */
5621     if (TX_BD_IDX(bd_prod) < nbds) {
5622         nbds++;
5623     }
5624 
5625     /* don't allow reordering of writes for nbd and packets */
5626     mb();
5627 
5628     fp->tx_db.data.prod += nbds;
5629 
5630     /* producer points to the next free tx_bd at this point */
5631     fp->tx_pkt_prod++;
5632     fp->tx_bd_prod = bd_prod;
5633 
5634     DOORBELL(sc, fp->index, fp->tx_db.raw);
5635 
5636     fp->eth_q_stats.tx_pkts++;
5637 
5638     /* Prevent speculative reads from getting ahead of the status block. */
5639     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5640                       0, 0, BUS_SPACE_BARRIER_READ);
5641 
5642     /* Prevent speculative reads from getting ahead of the doorbell. */
5643     bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5644                       0, 0, BUS_SPACE_BARRIER_READ);
5645 
5646     return (0);
5647 }
5648 
5649 static void
5650 bxe_tx_start_locked(struct bxe_softc *sc,
5651                     if_t ifp,
5652                     struct bxe_fastpath *fp)
5653 {
5654     struct mbuf *m = NULL;
5655     int tx_count = 0;
5656     uint16_t tx_bd_avail;
5657 
5658     BXE_FP_TX_LOCK_ASSERT(fp);
5659 
5660     /* keep adding entries while there are frames to send */
5661     while (!if_sendq_empty(ifp)) {
5662 
5663         /*
5664          * check for any frames to send
5665          * dequeue can still be NULL even if queue is not empty
5666          */
5667         m = if_dequeue(ifp);
5668         if (__predict_false(m == NULL)) {
5669             break;
5670         }
5671 
5672         /* the mbuf now belongs to us */
5673         fp->eth_q_stats.mbuf_alloc_tx++;
5674 
5675         /*
5676          * Put the frame into the transmit ring. If we don't have room,
5677          * place the mbuf back at the head of the TX queue, set the
5678          * OACTIVE flag, and wait for the NIC to drain the chain.
5679          */
5680         if (__predict_false(bxe_tx_encap(fp, &m))) {
5681             fp->eth_q_stats.tx_encap_failures++;
5682             if (m != NULL) {
5683                 /* mark the TX queue as full and return the frame */
5684                 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5685 		if_sendq_prepend(ifp, m);
5686                 fp->eth_q_stats.mbuf_alloc_tx--;
5687                 fp->eth_q_stats.tx_queue_xoff++;
5688             }
5689 
5690             /* stop looking for more work */
5691             break;
5692         }
5693 
5694         /* the frame was enqueued successfully */
5695         tx_count++;
5696 
5697         /* send a copy of the frame to any BPF listeners. */
5698         if_etherbpfmtap(ifp, m);
5699 
5700         tx_bd_avail = bxe_tx_avail(sc, fp);
5701 
5702         /* handle any completions if we're running low */
5703         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5704             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5705             bxe_txeof(sc, fp);
5706             if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5707                 break;
5708             }
5709         }
5710     }
5711 
5712     /* all TX packets were dequeued and/or the tx ring is full */
5713     if (tx_count > 0) {
5714         /* reset the TX watchdog timeout timer */
5715         fp->watchdog_timer = BXE_TX_TIMEOUT;
5716     }
5717 }
5718 
5719 /* Legacy (non-RSS) dispatch routine */
5720 static void
5721 bxe_tx_start(if_t ifp)
5722 {
5723     struct bxe_softc *sc;
5724     struct bxe_fastpath *fp;
5725 
5726     sc = if_getsoftc(ifp);
5727 
5728     if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
5729         BLOGW(sc, "Interface not running, ignoring transmit request\n");
5730         return;
5731     }
5732 
5733     if (!sc->link_vars.link_up) {
5734         BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5735         return;
5736     }
5737 
5738     fp = &sc->fp[0];
5739 
5740     if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5741         fp->eth_q_stats.tx_queue_full_return++;
5742         return;
5743     }
5744 
5745     BXE_FP_TX_LOCK(fp);
5746     bxe_tx_start_locked(sc, ifp, fp);
5747     BXE_FP_TX_UNLOCK(fp);
5748 }
5749 
5750 #if __FreeBSD_version >= 800000
5751 
5752 static int
5753 bxe_tx_mq_start_locked(struct bxe_softc    *sc,
5754                        if_t                ifp,
5755                        struct bxe_fastpath *fp,
5756                        struct mbuf         *m)
5757 {
5758     struct buf_ring *tx_br = fp->tx_br;
5759     struct mbuf *next;
5760     int depth, rc, tx_count;
5761     uint16_t tx_bd_avail;
5762 
5763     rc = tx_count = 0;
5764 
5765     BXE_FP_TX_LOCK_ASSERT(fp);
5766 
5767     if (!tx_br) {
5768         BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5769         return (EINVAL);
5770     }
5771 
5772     if (!sc->link_vars.link_up ||
5773         (if_getdrvflags(ifp) &
5774         (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING) {
5775         rc = drbr_enqueue(ifp, tx_br, m);
5776         goto bxe_tx_mq_start_locked_exit;
5777     }
5778 
5779     /* fetch the depth of the driver queue */
5780     depth = drbr_inuse_drv(ifp, tx_br);
5781     if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5782         fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5783     }
5784 
5785     if (m == NULL) {
5786         /* no new work, check for pending frames */
5787         next = drbr_dequeue_drv(ifp, tx_br);
5788     } else if (drbr_needs_enqueue_drv(ifp, tx_br)) {
5789         /* have both new and pending work, maintain packet order */
5790         rc = drbr_enqueue(ifp, tx_br, m);
5791         if (rc != 0) {
5792             fp->eth_q_stats.tx_soft_errors++;
5793             goto bxe_tx_mq_start_locked_exit;
5794         }
5795         next = drbr_dequeue_drv(ifp, tx_br);
5796     } else {
5797         /* new work only and nothing pending */
5798         next = m;
5799     }
5800 
5801     /* keep adding entries while there are frames to send */
5802     while (next != NULL) {
5803 
5804         /* the mbuf now belongs to us */
5805         fp->eth_q_stats.mbuf_alloc_tx++;
5806 
5807         /*
5808          * Put the frame into the transmit ring. If we don't have room,
5809          * place the mbuf back at the head of the TX queue, set the
5810          * OACTIVE flag, and wait for the NIC to drain the chain.
5811          */
5812         rc = bxe_tx_encap(fp, &next);
5813         if (__predict_false(rc != 0)) {
5814             fp->eth_q_stats.tx_encap_failures++;
5815             if (next != NULL) {
5816                 /* mark the TX queue as full and save the frame */
5817                 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5818                 /* XXX this may reorder the frame */
5819                 rc = drbr_enqueue(ifp, tx_br, next);
5820                 fp->eth_q_stats.mbuf_alloc_tx--;
5821                 fp->eth_q_stats.tx_frames_deferred++;
5822             }
5823 
5824             /* stop looking for more work */
5825             break;
5826         }
5827 
5828         /* the transmit frame was enqueued successfully */
5829         tx_count++;
5830 
5831         /* send a copy of the frame to any BPF listeners */
5832 	if_etherbpfmtap(ifp, next);
5833 
5834         tx_bd_avail = bxe_tx_avail(sc, fp);
5835 
5836         /* handle any completions if we're running low */
5837         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5838             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5839             bxe_txeof(sc, fp);
5840             if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE) {
5841                 break;
5842             }
5843         }
5844 
5845         next = drbr_dequeue_drv(ifp, tx_br);
5846     }
5847 
5848     /* all TX packets were dequeued and/or the tx ring is full */
5849     if (tx_count > 0) {
5850         /* reset the TX watchdog timeout timer */
5851         fp->watchdog_timer = BXE_TX_TIMEOUT;
5852     }
5853 
5854 bxe_tx_mq_start_locked_exit:
5855 
5856     return (rc);
5857 }
5858 
5859 /* Multiqueue (TSS) dispatch routine. */
5860 static int
5861 bxe_tx_mq_start(struct ifnet *ifp,
5862                 struct mbuf  *m)
5863 {
5864     struct bxe_softc *sc = if_getsoftc(ifp);
5865     struct bxe_fastpath *fp;
5866     int fp_index, rc;
5867 
5868     fp_index = 0; /* default is the first queue */
5869 
5870     /* check if flowid is set */
5871 
5872     if (BXE_VALID_FLOWID(m))
5873         fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5874 
5875     fp = &sc->fp[fp_index];
5876 
5877     if (BXE_FP_TX_TRYLOCK(fp)) {
5878         rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5879         BXE_FP_TX_UNLOCK(fp);
5880     } else
5881         rc = drbr_enqueue(ifp, fp->tx_br, m);
5882 
5883     return (rc);
5884 }
5885 
5886 static void
5887 bxe_mq_flush(struct ifnet *ifp)
5888 {
5889     struct bxe_softc *sc = if_getsoftc(ifp);
5890     struct bxe_fastpath *fp;
5891     struct mbuf *m;
5892     int i;
5893 
5894     for (i = 0; i < sc->num_queues; i++) {
5895         fp = &sc->fp[i];
5896 
5897         if (fp->state != BXE_FP_STATE_OPEN) {
5898             BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5899                   fp->index, fp->state);
5900             continue;
5901         }
5902 
5903         if (fp->tx_br != NULL) {
5904             BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5905             BXE_FP_TX_LOCK(fp);
5906             while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5907                 m_freem(m);
5908             }
5909             BXE_FP_TX_UNLOCK(fp);
5910         }
5911     }
5912 
5913     if_qflush(ifp);
5914 }
5915 
5916 #endif /* FreeBSD_version >= 800000 */
5917 
5918 static uint16_t
5919 bxe_cid_ilt_lines(struct bxe_softc *sc)
5920 {
5921     if (IS_SRIOV(sc)) {
5922         return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5923     }
5924     return (L2_ILT_LINES(sc));
5925 }
5926 
5927 static void
5928 bxe_ilt_set_info(struct bxe_softc *sc)
5929 {
5930     struct ilt_client_info *ilt_client;
5931     struct ecore_ilt *ilt = sc->ilt;
5932     uint16_t line = 0;
5933 
5934     ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5935     BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5936 
5937     /* CDU */
5938     ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5939     ilt_client->client_num = ILT_CLIENT_CDU;
5940     ilt_client->page_size = CDU_ILT_PAGE_SZ;
5941     ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5942     ilt_client->start = line;
5943     line += bxe_cid_ilt_lines(sc);
5944 
5945     if (CNIC_SUPPORT(sc)) {
5946         line += CNIC_ILT_LINES;
5947     }
5948 
5949     ilt_client->end = (line - 1);
5950 
5951     BLOGD(sc, DBG_LOAD,
5952           "ilt client[CDU]: start %d, end %d, "
5953           "psz 0x%x, flags 0x%x, hw psz %d\n",
5954           ilt_client->start, ilt_client->end,
5955           ilt_client->page_size,
5956           ilt_client->flags,
5957           ilog2(ilt_client->page_size >> 12));
5958 
5959     /* QM */
5960     if (QM_INIT(sc->qm_cid_count)) {
5961         ilt_client = &ilt->clients[ILT_CLIENT_QM];
5962         ilt_client->client_num = ILT_CLIENT_QM;
5963         ilt_client->page_size = QM_ILT_PAGE_SZ;
5964         ilt_client->flags = 0;
5965         ilt_client->start = line;
5966 
5967         /* 4 bytes for each cid */
5968         line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5969                              QM_ILT_PAGE_SZ);
5970 
5971         ilt_client->end = (line - 1);
5972 
5973         BLOGD(sc, DBG_LOAD,
5974               "ilt client[QM]: start %d, end %d, "
5975               "psz 0x%x, flags 0x%x, hw psz %d\n",
5976               ilt_client->start, ilt_client->end,
5977               ilt_client->page_size, ilt_client->flags,
5978               ilog2(ilt_client->page_size >> 12));
5979     }
5980 
5981     if (CNIC_SUPPORT(sc)) {
5982         /* SRC */
5983         ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5984         ilt_client->client_num = ILT_CLIENT_SRC;
5985         ilt_client->page_size = SRC_ILT_PAGE_SZ;
5986         ilt_client->flags = 0;
5987         ilt_client->start = line;
5988         line += SRC_ILT_LINES;
5989         ilt_client->end = (line - 1);
5990 
5991         BLOGD(sc, DBG_LOAD,
5992               "ilt client[SRC]: start %d, end %d, "
5993               "psz 0x%x, flags 0x%x, hw psz %d\n",
5994               ilt_client->start, ilt_client->end,
5995               ilt_client->page_size, ilt_client->flags,
5996               ilog2(ilt_client->page_size >> 12));
5997 
5998         /* TM */
5999         ilt_client = &ilt->clients[ILT_CLIENT_TM];
6000         ilt_client->client_num = ILT_CLIENT_TM;
6001         ilt_client->page_size = TM_ILT_PAGE_SZ;
6002         ilt_client->flags = 0;
6003         ilt_client->start = line;
6004         line += TM_ILT_LINES;
6005         ilt_client->end = (line - 1);
6006 
6007         BLOGD(sc, DBG_LOAD,
6008               "ilt client[TM]: start %d, end %d, "
6009               "psz 0x%x, flags 0x%x, hw psz %d\n",
6010               ilt_client->start, ilt_client->end,
6011               ilt_client->page_size, ilt_client->flags,
6012               ilog2(ilt_client->page_size >> 12));
6013     }
6014 
6015     KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
6016 }
6017 
6018 static void
6019 bxe_set_fp_rx_buf_size(struct bxe_softc *sc)
6020 {
6021     int i;
6022     uint32_t rx_buf_size;
6023 
6024     rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
6025 
6026     for (i = 0; i < sc->num_queues; i++) {
6027         if(rx_buf_size <= MCLBYTES){
6028             sc->fp[i].rx_buf_size = rx_buf_size;
6029             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6030         }else if (rx_buf_size <= MJUMPAGESIZE){
6031             sc->fp[i].rx_buf_size = rx_buf_size;
6032             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6033         }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
6034             sc->fp[i].rx_buf_size = MCLBYTES;
6035             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6036         }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
6037             sc->fp[i].rx_buf_size = MJUMPAGESIZE;
6038             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6039         }else {
6040             sc->fp[i].rx_buf_size = MCLBYTES;
6041             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6042         }
6043     }
6044 }
6045 
6046 static int
6047 bxe_alloc_ilt_mem(struct bxe_softc *sc)
6048 {
6049     int rc = 0;
6050 
6051     if ((sc->ilt =
6052          (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
6053                                     M_BXE_ILT,
6054                                     (M_NOWAIT | M_ZERO))) == NULL) {
6055         rc = 1;
6056     }
6057 
6058     return (rc);
6059 }
6060 
6061 static int
6062 bxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
6063 {
6064     int rc = 0;
6065 
6066     if ((sc->ilt->lines =
6067          (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
6068                                     M_BXE_ILT,
6069                                     (M_NOWAIT | M_ZERO))) == NULL) {
6070         rc = 1;
6071     }
6072 
6073     return (rc);
6074 }
6075 
6076 static void
6077 bxe_free_ilt_mem(struct bxe_softc *sc)
6078 {
6079     if (sc->ilt != NULL) {
6080         free(sc->ilt, M_BXE_ILT);
6081         sc->ilt = NULL;
6082     }
6083 }
6084 
6085 static void
6086 bxe_free_ilt_lines_mem(struct bxe_softc *sc)
6087 {
6088     if (sc->ilt->lines != NULL) {
6089         free(sc->ilt->lines, M_BXE_ILT);
6090         sc->ilt->lines = NULL;
6091     }
6092 }
6093 
6094 static void
6095 bxe_free_mem(struct bxe_softc *sc)
6096 {
6097     int i;
6098 
6099     for (i = 0; i < L2_ILT_LINES(sc); i++) {
6100         bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6101         sc->context[i].vcxt = NULL;
6102         sc->context[i].size = 0;
6103     }
6104 
6105     ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6106 
6107     bxe_free_ilt_lines_mem(sc);
6108 
6109 }
6110 
6111 static int
6112 bxe_alloc_mem(struct bxe_softc *sc)
6113 {
6114     int context_size;
6115     int allocated;
6116     int i;
6117 
6118     /*
6119      * Allocate memory for CDU context:
6120      * This memory is allocated separately and not in the generic ILT
6121      * functions because CDU differs in few aspects:
6122      * 1. There can be multiple entities allocating memory for context -
6123      * regular L2, CNIC, and SRIOV drivers. Each separately controls
6124      * its own ILT lines.
6125      * 2. Since CDU page-size is not a single 4KB page (which is the case
6126      * for the other ILT clients), to be efficient we want to support
6127      * allocation of sub-page-size in the last entry.
6128      * 3. Context pointers are used by the driver to pass to FW / update
6129      * the context (for the other ILT clients the pointers are used just to
6130      * free the memory during unload).
6131      */
6132     context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6133     for (i = 0, allocated = 0; allocated < context_size; i++) {
6134         sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6135                                   (context_size - allocated));
6136 
6137         if (bxe_dma_alloc(sc, sc->context[i].size,
6138                           &sc->context[i].vcxt_dma,
6139                           "cdu context") != 0) {
6140             bxe_free_mem(sc);
6141             return (-1);
6142         }
6143 
6144         sc->context[i].vcxt =
6145             (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6146 
6147         allocated += sc->context[i].size;
6148     }
6149 
6150     bxe_alloc_ilt_lines_mem(sc);
6151 
6152     BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6153           sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6154     {
6155         for (i = 0; i < 4; i++) {
6156             BLOGD(sc, DBG_LOAD,
6157                   "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6158                   i,
6159                   sc->ilt->clients[i].page_size,
6160                   sc->ilt->clients[i].start,
6161                   sc->ilt->clients[i].end,
6162                   sc->ilt->clients[i].client_num,
6163                   sc->ilt->clients[i].flags);
6164         }
6165     }
6166     if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6167         BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6168         bxe_free_mem(sc);
6169         return (-1);
6170     }
6171 
6172     return (0);
6173 }
6174 
6175 static void
6176 bxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6177 {
6178     struct bxe_softc *sc;
6179     int i;
6180 
6181     sc = fp->sc;
6182 
6183     if (fp->rx_mbuf_tag == NULL) {
6184         return;
6185     }
6186 
6187     /* free all mbufs and unload all maps */
6188     for (i = 0; i < RX_BD_TOTAL; i++) {
6189         if (fp->rx_mbuf_chain[i].m_map != NULL) {
6190             bus_dmamap_sync(fp->rx_mbuf_tag,
6191                             fp->rx_mbuf_chain[i].m_map,
6192                             BUS_DMASYNC_POSTREAD);
6193             bus_dmamap_unload(fp->rx_mbuf_tag,
6194                               fp->rx_mbuf_chain[i].m_map);
6195         }
6196 
6197         if (fp->rx_mbuf_chain[i].m != NULL) {
6198             m_freem(fp->rx_mbuf_chain[i].m);
6199             fp->rx_mbuf_chain[i].m = NULL;
6200             fp->eth_q_stats.mbuf_alloc_rx--;
6201         }
6202     }
6203 }
6204 
6205 static void
6206 bxe_free_tpa_pool(struct bxe_fastpath *fp)
6207 {
6208     struct bxe_softc *sc;
6209     int i, max_agg_queues;
6210 
6211     sc = fp->sc;
6212 
6213     if (fp->rx_mbuf_tag == NULL) {
6214         return;
6215     }
6216 
6217     max_agg_queues = MAX_AGG_QS(sc);
6218 
6219     /* release all mbufs and unload all DMA maps in the TPA pool */
6220     for (i = 0; i < max_agg_queues; i++) {
6221         if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6222             bus_dmamap_sync(fp->rx_mbuf_tag,
6223                             fp->rx_tpa_info[i].bd.m_map,
6224                             BUS_DMASYNC_POSTREAD);
6225             bus_dmamap_unload(fp->rx_mbuf_tag,
6226                               fp->rx_tpa_info[i].bd.m_map);
6227         }
6228 
6229         if (fp->rx_tpa_info[i].bd.m != NULL) {
6230             m_freem(fp->rx_tpa_info[i].bd.m);
6231             fp->rx_tpa_info[i].bd.m = NULL;
6232             fp->eth_q_stats.mbuf_alloc_tpa--;
6233         }
6234     }
6235 }
6236 
6237 static void
6238 bxe_free_sge_chain(struct bxe_fastpath *fp)
6239 {
6240     struct bxe_softc *sc;
6241     int i;
6242 
6243     sc = fp->sc;
6244 
6245     if (fp->rx_sge_mbuf_tag == NULL) {
6246         return;
6247     }
6248 
6249     /* rree all mbufs and unload all maps */
6250     for (i = 0; i < RX_SGE_TOTAL; i++) {
6251         if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6252             bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6253                             fp->rx_sge_mbuf_chain[i].m_map,
6254                             BUS_DMASYNC_POSTREAD);
6255             bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6256                               fp->rx_sge_mbuf_chain[i].m_map);
6257         }
6258 
6259         if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6260             m_freem(fp->rx_sge_mbuf_chain[i].m);
6261             fp->rx_sge_mbuf_chain[i].m = NULL;
6262             fp->eth_q_stats.mbuf_alloc_sge--;
6263         }
6264     }
6265 }
6266 
6267 static void
6268 bxe_free_fp_buffers(struct bxe_softc *sc)
6269 {
6270     struct bxe_fastpath *fp;
6271     int i;
6272 
6273     for (i = 0; i < sc->num_queues; i++) {
6274         fp = &sc->fp[i];
6275 
6276 #if __FreeBSD_version >= 800000
6277         if (fp->tx_br != NULL) {
6278             /* just in case bxe_mq_flush() wasn't called */
6279             if (mtx_initialized(&fp->tx_mtx)) {
6280                 struct mbuf *m;
6281 
6282                 BXE_FP_TX_LOCK(fp);
6283                 while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6284                     m_freem(m);
6285                 BXE_FP_TX_UNLOCK(fp);
6286             }
6287         }
6288 #endif
6289 
6290         /* free all RX buffers */
6291         bxe_free_rx_bd_chain(fp);
6292         bxe_free_tpa_pool(fp);
6293         bxe_free_sge_chain(fp);
6294 
6295         if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6296             BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6297                   fp->eth_q_stats.mbuf_alloc_rx);
6298         }
6299 
6300         if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6301             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6302                   fp->eth_q_stats.mbuf_alloc_sge);
6303         }
6304 
6305         if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6306             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6307                   fp->eth_q_stats.mbuf_alloc_tpa);
6308         }
6309 
6310         if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6311             BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6312                   fp->eth_q_stats.mbuf_alloc_tx);
6313         }
6314 
6315         /* XXX verify all mbufs were reclaimed */
6316     }
6317 }
6318 
6319 static int
6320 bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6321                      uint16_t            prev_index,
6322                      uint16_t            index)
6323 {
6324     struct bxe_sw_rx_bd *rx_buf;
6325     struct eth_rx_bd *rx_bd;
6326     bus_dma_segment_t segs[1];
6327     bus_dmamap_t map;
6328     struct mbuf *m;
6329     int nsegs, rc;
6330 
6331     rc = 0;
6332 
6333     /* allocate the new RX BD mbuf */
6334     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6335     if (__predict_false(m == NULL)) {
6336         fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6337         return (ENOBUFS);
6338     }
6339 
6340     fp->eth_q_stats.mbuf_alloc_rx++;
6341 
6342     /* initialize the mbuf buffer length */
6343     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6344 
6345     /* map the mbuf into non-paged pool */
6346     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6347                                  fp->rx_mbuf_spare_map,
6348                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6349     if (__predict_false(rc != 0)) {
6350         fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6351         m_freem(m);
6352         fp->eth_q_stats.mbuf_alloc_rx--;
6353         return (rc);
6354     }
6355 
6356     /* all mbufs must map to a single segment */
6357     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6358 
6359     /* release any existing RX BD mbuf mappings */
6360 
6361     if (prev_index != index) {
6362         rx_buf = &fp->rx_mbuf_chain[prev_index];
6363 
6364         if (rx_buf->m_map != NULL) {
6365             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6366                             BUS_DMASYNC_POSTREAD);
6367             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6368         }
6369 
6370         /*
6371          * We only get here from bxe_rxeof() when the maximum number
6372          * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6373          * holds the mbuf in the prev_index so it's OK to NULL it out
6374          * here without concern of a memory leak.
6375          */
6376         fp->rx_mbuf_chain[prev_index].m = NULL;
6377     }
6378 
6379     rx_buf = &fp->rx_mbuf_chain[index];
6380 
6381     if (rx_buf->m_map != NULL) {
6382         bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6383                         BUS_DMASYNC_POSTREAD);
6384         bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6385     }
6386 
6387     /* save the mbuf and mapping info for a future packet */
6388     map = (prev_index != index) ?
6389               fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6390     rx_buf->m_map = fp->rx_mbuf_spare_map;
6391     fp->rx_mbuf_spare_map = map;
6392     bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6393                     BUS_DMASYNC_PREREAD);
6394     rx_buf->m = m;
6395 
6396     rx_bd = &fp->rx_chain[index];
6397     rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6398     rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6399 
6400     return (rc);
6401 }
6402 
6403 static int
6404 bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6405                       int                 queue)
6406 {
6407     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6408     bus_dma_segment_t segs[1];
6409     bus_dmamap_t map;
6410     struct mbuf *m;
6411     int nsegs;
6412     int rc = 0;
6413 
6414     /* allocate the new TPA mbuf */
6415     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6416     if (__predict_false(m == NULL)) {
6417         fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6418         return (ENOBUFS);
6419     }
6420 
6421     fp->eth_q_stats.mbuf_alloc_tpa++;
6422 
6423     /* initialize the mbuf buffer length */
6424     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6425 
6426     /* map the mbuf into non-paged pool */
6427     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6428                                  fp->rx_tpa_info_mbuf_spare_map,
6429                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6430     if (__predict_false(rc != 0)) {
6431         fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6432         m_free(m);
6433         fp->eth_q_stats.mbuf_alloc_tpa--;
6434         return (rc);
6435     }
6436 
6437     /* all mbufs must map to a single segment */
6438     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6439 
6440     /* release any existing TPA mbuf mapping */
6441     if (tpa_info->bd.m_map != NULL) {
6442         bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6443                         BUS_DMASYNC_POSTREAD);
6444         bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6445     }
6446 
6447     /* save the mbuf and mapping info for the TPA mbuf */
6448     map = tpa_info->bd.m_map;
6449     tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6450     fp->rx_tpa_info_mbuf_spare_map = map;
6451     bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6452                     BUS_DMASYNC_PREREAD);
6453     tpa_info->bd.m = m;
6454     tpa_info->seg = segs[0];
6455 
6456     return (rc);
6457 }
6458 
6459 /*
6460  * Allocate an mbuf and assign it to the receive scatter gather chain. The
6461  * caller must take care to save a copy of the existing mbuf in the SG mbuf
6462  * chain.
6463  */
6464 static int
6465 bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6466                       uint16_t            index)
6467 {
6468     struct bxe_sw_rx_bd *sge_buf;
6469     struct eth_rx_sge *sge;
6470     bus_dma_segment_t segs[1];
6471     bus_dmamap_t map;
6472     struct mbuf *m;
6473     int nsegs;
6474     int rc = 0;
6475 
6476     /* allocate a new SGE mbuf */
6477     m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6478     if (__predict_false(m == NULL)) {
6479         fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6480         return (ENOMEM);
6481     }
6482 
6483     fp->eth_q_stats.mbuf_alloc_sge++;
6484 
6485     /* initialize the mbuf buffer length */
6486     m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6487 
6488     /* map the SGE mbuf into non-paged pool */
6489     rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6490                                  fp->rx_sge_mbuf_spare_map,
6491                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6492     if (__predict_false(rc != 0)) {
6493         fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6494         m_freem(m);
6495         fp->eth_q_stats.mbuf_alloc_sge--;
6496         return (rc);
6497     }
6498 
6499     /* all mbufs must map to a single segment */
6500     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6501 
6502     sge_buf = &fp->rx_sge_mbuf_chain[index];
6503 
6504     /* release any existing SGE mbuf mapping */
6505     if (sge_buf->m_map != NULL) {
6506         bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6507                         BUS_DMASYNC_POSTREAD);
6508         bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6509     }
6510 
6511     /* save the mbuf and mapping info for a future packet */
6512     map = sge_buf->m_map;
6513     sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6514     fp->rx_sge_mbuf_spare_map = map;
6515     bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6516                     BUS_DMASYNC_PREREAD);
6517     sge_buf->m = m;
6518 
6519     sge = &fp->rx_sge_chain[index];
6520     sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6521     sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6522 
6523     return (rc);
6524 }
6525 
6526 static __noinline int
6527 bxe_alloc_fp_buffers(struct bxe_softc *sc)
6528 {
6529     struct bxe_fastpath *fp;
6530     int i, j, rc = 0;
6531     int ring_prod, cqe_ring_prod;
6532     int max_agg_queues;
6533 
6534     for (i = 0; i < sc->num_queues; i++) {
6535         fp = &sc->fp[i];
6536 
6537         ring_prod = cqe_ring_prod = 0;
6538         fp->rx_bd_cons = 0;
6539         fp->rx_cq_cons = 0;
6540 
6541         /* allocate buffers for the RX BDs in RX BD chain */
6542         for (j = 0; j < sc->max_rx_bufs; j++) {
6543             rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6544             if (rc != 0) {
6545                 BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6546                       i, rc);
6547                 goto bxe_alloc_fp_buffers_error;
6548             }
6549 
6550             ring_prod     = RX_BD_NEXT(ring_prod);
6551             cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6552         }
6553 
6554         fp->rx_bd_prod = ring_prod;
6555         fp->rx_cq_prod = cqe_ring_prod;
6556         fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6557 
6558         max_agg_queues = MAX_AGG_QS(sc);
6559 
6560         fp->tpa_enable = TRUE;
6561 
6562         /* fill the TPA pool */
6563         for (j = 0; j < max_agg_queues; j++) {
6564             rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6565             if (rc != 0) {
6566                 BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6567                           i, j);
6568                 fp->tpa_enable = FALSE;
6569                 goto bxe_alloc_fp_buffers_error;
6570             }
6571 
6572             fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6573         }
6574 
6575         if (fp->tpa_enable) {
6576             /* fill the RX SGE chain */
6577             ring_prod = 0;
6578             for (j = 0; j < RX_SGE_USABLE; j++) {
6579                 rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6580                 if (rc != 0) {
6581                     BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6582                               i, ring_prod);
6583                     fp->tpa_enable = FALSE;
6584                     ring_prod = 0;
6585                     goto bxe_alloc_fp_buffers_error;
6586                 }
6587 
6588                 ring_prod = RX_SGE_NEXT(ring_prod);
6589             }
6590 
6591             fp->rx_sge_prod = ring_prod;
6592         }
6593     }
6594 
6595     return (0);
6596 
6597 bxe_alloc_fp_buffers_error:
6598 
6599     /* unwind what was already allocated */
6600     bxe_free_rx_bd_chain(fp);
6601     bxe_free_tpa_pool(fp);
6602     bxe_free_sge_chain(fp);
6603 
6604     return (ENOBUFS);
6605 }
6606 
6607 static void
6608 bxe_free_fw_stats_mem(struct bxe_softc *sc)
6609 {
6610     bxe_dma_free(sc, &sc->fw_stats_dma);
6611 
6612     sc->fw_stats_num = 0;
6613 
6614     sc->fw_stats_req_size = 0;
6615     sc->fw_stats_req = NULL;
6616     sc->fw_stats_req_mapping = 0;
6617 
6618     sc->fw_stats_data_size = 0;
6619     sc->fw_stats_data = NULL;
6620     sc->fw_stats_data_mapping = 0;
6621 }
6622 
6623 static int
6624 bxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6625 {
6626     uint8_t num_queue_stats;
6627     int num_groups;
6628 
6629     /* number of queues for statistics is number of eth queues */
6630     num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6631 
6632     /*
6633      * Total number of FW statistics requests =
6634      *   1 for port stats + 1 for PF stats + num of queues
6635      */
6636     sc->fw_stats_num = (2 + num_queue_stats);
6637 
6638     /*
6639      * Request is built from stats_query_header and an array of
6640      * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6641      * rules. The real number or requests is configured in the
6642      * stats_query_header.
6643      */
6644     num_groups =
6645         ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6646          ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6647 
6648     BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6649           sc->fw_stats_num, num_groups);
6650 
6651     sc->fw_stats_req_size =
6652         (sizeof(struct stats_query_header) +
6653          (num_groups * sizeof(struct stats_query_cmd_group)));
6654 
6655     /*
6656      * Data for statistics requests + stats_counter.
6657      * stats_counter holds per-STORM counters that are incremented when
6658      * STORM has finished with the current request. Memory for FCoE
6659      * offloaded statistics are counted anyway, even if they will not be sent.
6660      * VF stats are not accounted for here as the data of VF stats is stored
6661      * in memory allocated by the VF, not here.
6662      */
6663     sc->fw_stats_data_size =
6664         (sizeof(struct stats_counter) +
6665          sizeof(struct per_port_stats) +
6666          sizeof(struct per_pf_stats) +
6667          /* sizeof(struct fcoe_statistics_params) + */
6668          (sizeof(struct per_queue_stats) * num_queue_stats));
6669 
6670     if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6671                       &sc->fw_stats_dma, "fw stats") != 0) {
6672         bxe_free_fw_stats_mem(sc);
6673         return (-1);
6674     }
6675 
6676     /* set up the shortcuts */
6677 
6678     sc->fw_stats_req =
6679         (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6680     sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6681 
6682     sc->fw_stats_data =
6683         (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6684                                      sc->fw_stats_req_size);
6685     sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6686                                  sc->fw_stats_req_size);
6687 
6688     BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6689           (uintmax_t)sc->fw_stats_req_mapping);
6690 
6691     BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6692           (uintmax_t)sc->fw_stats_data_mapping);
6693 
6694     return (0);
6695 }
6696 
6697 /*
6698  * Bits map:
6699  * 0-7  - Engine0 load counter.
6700  * 8-15 - Engine1 load counter.
6701  * 16   - Engine0 RESET_IN_PROGRESS bit.
6702  * 17   - Engine1 RESET_IN_PROGRESS bit.
6703  * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6704  *        function on the engine
6705  * 19   - Engine1 ONE_IS_LOADED.
6706  * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6707  *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6708  *        for just the one belonging to its engine).
6709  */
6710 #define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6711 #define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6712 #define BXE_PATH0_LOAD_CNT_SHIFT  0
6713 #define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6714 #define BXE_PATH1_LOAD_CNT_SHIFT  8
6715 #define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6716 #define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6717 #define BXE_GLOBAL_RESET_BIT      0x00040000
6718 
6719 /* set the GLOBAL_RESET bit, should be run under rtnl lock */
6720 static void
6721 bxe_set_reset_global(struct bxe_softc *sc)
6722 {
6723     uint32_t val;
6724     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6725     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6726     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6727     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6728 }
6729 
6730 /* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6731 static void
6732 bxe_clear_reset_global(struct bxe_softc *sc)
6733 {
6734     uint32_t val;
6735     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6736     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6737     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6738     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6739 }
6740 
6741 /* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6742 static uint8_t
6743 bxe_reset_is_global(struct bxe_softc *sc)
6744 {
6745     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6746     BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6747     return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6748 }
6749 
6750 /* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6751 static void
6752 bxe_set_reset_done(struct bxe_softc *sc)
6753 {
6754     uint32_t val;
6755     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6756                                  BXE_PATH0_RST_IN_PROG_BIT;
6757 
6758     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6759 
6760     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6761     /* Clear the bit */
6762     val &= ~bit;
6763     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6764 
6765     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6766 }
6767 
6768 /* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6769 static void
6770 bxe_set_reset_in_progress(struct bxe_softc *sc)
6771 {
6772     uint32_t val;
6773     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6774                                  BXE_PATH0_RST_IN_PROG_BIT;
6775 
6776     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6777 
6778     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6779     /* Set the bit */
6780     val |= bit;
6781     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6782 
6783     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6784 }
6785 
6786 /* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6787 static uint8_t
6788 bxe_reset_is_done(struct bxe_softc *sc,
6789                   int              engine)
6790 {
6791     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6792     uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6793                             BXE_PATH0_RST_IN_PROG_BIT;
6794 
6795     /* return false if bit is set */
6796     return (val & bit) ? FALSE : TRUE;
6797 }
6798 
6799 /* get the load status for an engine, should be run under rtnl lock */
6800 static uint8_t
6801 bxe_get_load_status(struct bxe_softc *sc,
6802                     int              engine)
6803 {
6804     uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6805                              BXE_PATH0_LOAD_CNT_MASK;
6806     uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6807                               BXE_PATH0_LOAD_CNT_SHIFT;
6808     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6809 
6810     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6811 
6812     val = ((val & mask) >> shift);
6813 
6814     BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6815 
6816     return (val != 0);
6817 }
6818 
6819 /* set pf load mark */
6820 /* XXX needs to be under rtnl lock */
6821 static void
6822 bxe_set_pf_load(struct bxe_softc *sc)
6823 {
6824     uint32_t val;
6825     uint32_t val1;
6826     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6827                                   BXE_PATH0_LOAD_CNT_MASK;
6828     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6829                                    BXE_PATH0_LOAD_CNT_SHIFT;
6830 
6831     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6832 
6833     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6834     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6835 
6836     /* get the current counter value */
6837     val1 = ((val & mask) >> shift);
6838 
6839     /* set bit of this PF */
6840     val1 |= (1 << SC_ABS_FUNC(sc));
6841 
6842     /* clear the old value */
6843     val &= ~mask;
6844 
6845     /* set the new one */
6846     val |= ((val1 << shift) & mask);
6847 
6848     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6849 
6850     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6851 }
6852 
6853 /* clear pf load mark */
6854 /* XXX needs to be under rtnl lock */
6855 static uint8_t
6856 bxe_clear_pf_load(struct bxe_softc *sc)
6857 {
6858     uint32_t val1, val;
6859     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6860                                   BXE_PATH0_LOAD_CNT_MASK;
6861     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6862                                    BXE_PATH0_LOAD_CNT_SHIFT;
6863 
6864     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6865     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6866     BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6867 
6868     /* get the current counter value */
6869     val1 = (val & mask) >> shift;
6870 
6871     /* clear bit of that PF */
6872     val1 &= ~(1 << SC_ABS_FUNC(sc));
6873 
6874     /* clear the old value */
6875     val &= ~mask;
6876 
6877     /* set the new one */
6878     val |= ((val1 << shift) & mask);
6879 
6880     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6881     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6882     return (val1 != 0);
6883 }
6884 
6885 /* send load requrest to mcp and analyze response */
6886 static int
6887 bxe_nic_load_request(struct bxe_softc *sc,
6888                      uint32_t         *load_code)
6889 {
6890     /* init fw_seq */
6891     sc->fw_seq =
6892         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6893          DRV_MSG_SEQ_NUMBER_MASK);
6894 
6895     BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6896 
6897     /* get the current FW pulse sequence */
6898     sc->fw_drv_pulse_wr_seq =
6899         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6900          DRV_PULSE_SEQ_MASK);
6901 
6902     BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6903           sc->fw_drv_pulse_wr_seq);
6904 
6905     /* load request */
6906     (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6907                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6908 
6909     /* if the MCP fails to respond we must abort */
6910     if (!(*load_code)) {
6911         BLOGE(sc, "MCP response failure!\n");
6912         return (-1);
6913     }
6914 
6915     /* if MCP refused then must abort */
6916     if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6917         BLOGE(sc, "MCP refused load request\n");
6918         return (-1);
6919     }
6920 
6921     return (0);
6922 }
6923 
6924 /*
6925  * Check whether another PF has already loaded FW to chip. In virtualized
6926  * environments a pf from anoth VM may have already initialized the device
6927  * including loading FW.
6928  */
6929 static int
6930 bxe_nic_load_analyze_req(struct bxe_softc *sc,
6931                          uint32_t         load_code)
6932 {
6933     uint32_t my_fw, loaded_fw;
6934 
6935     /* is another pf loaded on this engine? */
6936     if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6937         (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6938         /* build my FW version dword */
6939         my_fw = (BCM_5710_FW_MAJOR_VERSION +
6940                  (BCM_5710_FW_MINOR_VERSION << 8 ) +
6941                  (BCM_5710_FW_REVISION_VERSION << 16) +
6942                  (BCM_5710_FW_ENGINEERING_VERSION << 24));
6943 
6944         /* read loaded FW from chip */
6945         loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6946         BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6947               loaded_fw, my_fw);
6948 
6949         /* abort nic load if version mismatch */
6950         if (my_fw != loaded_fw) {
6951             BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6952                   loaded_fw, my_fw);
6953             return (-1);
6954         }
6955     }
6956 
6957     return (0);
6958 }
6959 
6960 /* mark PMF if applicable */
6961 static void
6962 bxe_nic_load_pmf(struct bxe_softc *sc,
6963                  uint32_t         load_code)
6964 {
6965     uint32_t ncsi_oem_data_addr;
6966 
6967     if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6968         (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6969         (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6970         /*
6971          * Barrier here for ordering between the writing to sc->port.pmf here
6972          * and reading it from the periodic task.
6973          */
6974         sc->port.pmf = 1;
6975         mb();
6976     } else {
6977         sc->port.pmf = 0;
6978     }
6979 
6980     BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6981 
6982     /* XXX needed? */
6983     if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6984         if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6985             ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6986             if (ncsi_oem_data_addr) {
6987                 REG_WR(sc,
6988                        (ncsi_oem_data_addr +
6989                         offsetof(struct glob_ncsi_oem_data, driver_version)),
6990                        0);
6991             }
6992         }
6993     }
6994 }
6995 
6996 static void
6997 bxe_read_mf_cfg(struct bxe_softc *sc)
6998 {
6999     int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
7000     int abs_func;
7001     int vn;
7002 
7003     if (BXE_NOMCP(sc)) {
7004         return; /* what should be the default bvalue in this case */
7005     }
7006 
7007     /*
7008      * The formula for computing the absolute function number is...
7009      * For 2 port configuration (4 functions per port):
7010      *   abs_func = 2 * vn + SC_PORT + SC_PATH
7011      * For 4 port configuration (2 functions per port):
7012      *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
7013      */
7014     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
7015         abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
7016         if (abs_func >= E1H_FUNC_MAX) {
7017             break;
7018         }
7019         sc->devinfo.mf_info.mf_config[vn] =
7020             MFCFG_RD(sc, func_mf_config[abs_func].config);
7021     }
7022 
7023     if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
7024         FUNC_MF_CFG_FUNC_DISABLED) {
7025         BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
7026         sc->flags |= BXE_MF_FUNC_DIS;
7027     } else {
7028         BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
7029         sc->flags &= ~BXE_MF_FUNC_DIS;
7030     }
7031 }
7032 
7033 /* acquire split MCP access lock register */
7034 static int bxe_acquire_alr(struct bxe_softc *sc)
7035 {
7036     uint32_t j, val;
7037 
7038     for (j = 0; j < 1000; j++) {
7039         val = (1UL << 31);
7040         REG_WR(sc, GRCBASE_MCP + 0x9c, val);
7041         val = REG_RD(sc, GRCBASE_MCP + 0x9c);
7042         if (val & (1L << 31))
7043             break;
7044 
7045         DELAY(5000);
7046     }
7047 
7048     if (!(val & (1L << 31))) {
7049         BLOGE(sc, "Cannot acquire MCP access lock register\n");
7050         return (-1);
7051     }
7052 
7053     return (0);
7054 }
7055 
7056 /* release split MCP access lock register */
7057 static void bxe_release_alr(struct bxe_softc *sc)
7058 {
7059     REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
7060 }
7061 
7062 static void
7063 bxe_fan_failure(struct bxe_softc *sc)
7064 {
7065     int port = SC_PORT(sc);
7066     uint32_t ext_phy_config;
7067 
7068     /* mark the failure */
7069     ext_phy_config =
7070         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7071 
7072     ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7073     ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7074     SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7075              ext_phy_config);
7076 
7077     /* log the failure */
7078     BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7079               "the card to prevent permanent damage. "
7080               "Please contact OEM Support for assistance\n");
7081 
7082     /* XXX */
7083 #if 1
7084     bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7085 #else
7086     /*
7087      * Schedule device reset (unload)
7088      * This is due to some boards consuming sufficient power when driver is
7089      * up to overheat if fan fails.
7090      */
7091     bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7092     schedule_delayed_work(&sc->sp_rtnl_task, 0);
7093 #endif
7094 }
7095 
7096 /* this function is called upon a link interrupt */
7097 static void
7098 bxe_link_attn(struct bxe_softc *sc)
7099 {
7100     uint32_t pause_enabled = 0;
7101     struct host_port_stats *pstats;
7102     int cmng_fns;
7103 
7104     /* Make sure that we are synced with the current statistics */
7105     bxe_stats_handle(sc, STATS_EVENT_STOP);
7106 
7107     elink_link_update(&sc->link_params, &sc->link_vars);
7108 
7109     if (sc->link_vars.link_up) {
7110 
7111         /* dropless flow control */
7112         if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7113             pause_enabled = 0;
7114 
7115             if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7116                 pause_enabled = 1;
7117             }
7118 
7119             REG_WR(sc,
7120                    (BAR_USTRORM_INTMEM +
7121                     USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7122                    pause_enabled);
7123         }
7124 
7125         if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7126             pstats = BXE_SP(sc, port_stats);
7127             /* reset old mac stats */
7128             memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7129         }
7130 
7131         if (sc->state == BXE_STATE_OPEN) {
7132             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7133         }
7134     }
7135 
7136     if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7137         cmng_fns = bxe_get_cmng_fns_mode(sc);
7138 
7139         if (cmng_fns != CMNG_FNS_NONE) {
7140             bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7141             storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7142         } else {
7143             /* rate shaping and fairness are disabled */
7144             BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7145         }
7146     }
7147 
7148     bxe_link_report_locked(sc);
7149 
7150     if (IS_MF(sc)) {
7151         ; // XXX bxe_link_sync_notify(sc);
7152     }
7153 }
7154 
7155 static void
7156 bxe_attn_int_asserted(struct bxe_softc *sc,
7157                       uint32_t         asserted)
7158 {
7159     int port = SC_PORT(sc);
7160     uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7161                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
7162     uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7163                                         NIG_REG_MASK_INTERRUPT_PORT0;
7164     uint32_t aeu_mask;
7165     uint32_t nig_mask = 0;
7166     uint32_t reg_addr;
7167     uint32_t igu_acked;
7168     uint32_t cnt;
7169 
7170     if (sc->attn_state & asserted) {
7171         BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7172     }
7173 
7174     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7175 
7176     aeu_mask = REG_RD(sc, aeu_addr);
7177 
7178     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7179           aeu_mask, asserted);
7180 
7181     aeu_mask &= ~(asserted & 0x3ff);
7182 
7183     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7184 
7185     REG_WR(sc, aeu_addr, aeu_mask);
7186 
7187     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7188 
7189     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7190     sc->attn_state |= asserted;
7191     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7192 
7193     if (asserted & ATTN_HARD_WIRED_MASK) {
7194         if (asserted & ATTN_NIG_FOR_FUNC) {
7195 
7196 	    bxe_acquire_phy_lock(sc);
7197             /* save nig interrupt mask */
7198             nig_mask = REG_RD(sc, nig_int_mask_addr);
7199 
7200             /* If nig_mask is not set, no need to call the update function */
7201             if (nig_mask) {
7202                 REG_WR(sc, nig_int_mask_addr, 0);
7203 
7204                 bxe_link_attn(sc);
7205             }
7206 
7207             /* handle unicore attn? */
7208         }
7209 
7210         if (asserted & ATTN_SW_TIMER_4_FUNC) {
7211             BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7212         }
7213 
7214         if (asserted & GPIO_2_FUNC) {
7215             BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7216         }
7217 
7218         if (asserted & GPIO_3_FUNC) {
7219             BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7220         }
7221 
7222         if (asserted & GPIO_4_FUNC) {
7223             BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7224         }
7225 
7226         if (port == 0) {
7227             if (asserted & ATTN_GENERAL_ATTN_1) {
7228                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7229                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7230             }
7231             if (asserted & ATTN_GENERAL_ATTN_2) {
7232                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7233                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7234             }
7235             if (asserted & ATTN_GENERAL_ATTN_3) {
7236                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7237                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7238             }
7239         } else {
7240             if (asserted & ATTN_GENERAL_ATTN_4) {
7241                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7242                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7243             }
7244             if (asserted & ATTN_GENERAL_ATTN_5) {
7245                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7246                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7247             }
7248             if (asserted & ATTN_GENERAL_ATTN_6) {
7249                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7250                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7251             }
7252         }
7253     } /* hardwired */
7254 
7255     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7256         reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7257     } else {
7258         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7259     }
7260 
7261     BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7262           asserted,
7263           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7264     REG_WR(sc, reg_addr, asserted);
7265 
7266     /* now set back the mask */
7267     if (asserted & ATTN_NIG_FOR_FUNC) {
7268         /*
7269          * Verify that IGU ack through BAR was written before restoring
7270          * NIG mask. This loop should exit after 2-3 iterations max.
7271          */
7272         if (sc->devinfo.int_block != INT_BLOCK_HC) {
7273             cnt = 0;
7274 
7275             do {
7276                 igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7277             } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7278                      (++cnt < MAX_IGU_ATTN_ACK_TO));
7279 
7280             if (!igu_acked) {
7281                 BLOGE(sc, "Failed to verify IGU ack on time\n");
7282             }
7283 
7284             mb();
7285         }
7286 
7287         REG_WR(sc, nig_int_mask_addr, nig_mask);
7288 
7289 	bxe_release_phy_lock(sc);
7290     }
7291 }
7292 
7293 static void
7294 bxe_print_next_block(struct bxe_softc *sc,
7295                      int              idx,
7296                      const char       *blk)
7297 {
7298     BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7299 }
7300 
7301 static int
7302 bxe_check_blocks_with_parity0(struct bxe_softc *sc,
7303                               uint32_t         sig,
7304                               int              par_num,
7305                               uint8_t          print)
7306 {
7307     uint32_t cur_bit = 0;
7308     int i = 0;
7309 
7310     for (i = 0; sig; i++) {
7311         cur_bit = ((uint32_t)0x1 << i);
7312         if (sig & cur_bit) {
7313             switch (cur_bit) {
7314             case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7315                 if (print)
7316                     bxe_print_next_block(sc, par_num++, "BRB");
7317                 break;
7318             case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7319                 if (print)
7320                     bxe_print_next_block(sc, par_num++, "PARSER");
7321                 break;
7322             case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7323                 if (print)
7324                     bxe_print_next_block(sc, par_num++, "TSDM");
7325                 break;
7326             case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7327                 if (print)
7328                     bxe_print_next_block(sc, par_num++, "SEARCHER");
7329                 break;
7330             case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7331                 if (print)
7332                     bxe_print_next_block(sc, par_num++, "TCM");
7333                 break;
7334             case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7335                 if (print)
7336                     bxe_print_next_block(sc, par_num++, "TSEMI");
7337                 break;
7338             case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7339                 if (print)
7340                     bxe_print_next_block(sc, par_num++, "XPB");
7341                 break;
7342             }
7343 
7344             /* Clear the bit */
7345             sig &= ~cur_bit;
7346         }
7347     }
7348 
7349     return (par_num);
7350 }
7351 
7352 static int
7353 bxe_check_blocks_with_parity1(struct bxe_softc *sc,
7354                               uint32_t         sig,
7355                               int              par_num,
7356                               uint8_t          *global,
7357                               uint8_t          print)
7358 {
7359     int i = 0;
7360     uint32_t cur_bit = 0;
7361     for (i = 0; sig; i++) {
7362         cur_bit = ((uint32_t)0x1 << i);
7363         if (sig & cur_bit) {
7364             switch (cur_bit) {
7365             case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7366                 if (print)
7367                     bxe_print_next_block(sc, par_num++, "PBF");
7368                 break;
7369             case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7370                 if (print)
7371                     bxe_print_next_block(sc, par_num++, "QM");
7372                 break;
7373             case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7374                 if (print)
7375                     bxe_print_next_block(sc, par_num++, "TM");
7376                 break;
7377             case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7378                 if (print)
7379                     bxe_print_next_block(sc, par_num++, "XSDM");
7380                 break;
7381             case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7382                 if (print)
7383                     bxe_print_next_block(sc, par_num++, "XCM");
7384                 break;
7385             case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7386                 if (print)
7387                     bxe_print_next_block(sc, par_num++, "XSEMI");
7388                 break;
7389             case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7390                 if (print)
7391                     bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7392                 break;
7393             case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7394                 if (print)
7395                     bxe_print_next_block(sc, par_num++, "NIG");
7396                 break;
7397             case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7398                 if (print)
7399                     bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7400                 *global = TRUE;
7401                 break;
7402             case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7403                 if (print)
7404                     bxe_print_next_block(sc, par_num++, "DEBUG");
7405                 break;
7406             case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7407                 if (print)
7408                     bxe_print_next_block(sc, par_num++, "USDM");
7409                 break;
7410             case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7411                 if (print)
7412                     bxe_print_next_block(sc, par_num++, "UCM");
7413                 break;
7414             case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7415                 if (print)
7416                     bxe_print_next_block(sc, par_num++, "USEMI");
7417                 break;
7418             case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7419                 if (print)
7420                     bxe_print_next_block(sc, par_num++, "UPB");
7421                 break;
7422             case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7423                 if (print)
7424                     bxe_print_next_block(sc, par_num++, "CSDM");
7425                 break;
7426             case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7427                 if (print)
7428                     bxe_print_next_block(sc, par_num++, "CCM");
7429                 break;
7430             }
7431 
7432             /* Clear the bit */
7433             sig &= ~cur_bit;
7434         }
7435     }
7436 
7437     return (par_num);
7438 }
7439 
7440 static int
7441 bxe_check_blocks_with_parity2(struct bxe_softc *sc,
7442                               uint32_t         sig,
7443                               int              par_num,
7444                               uint8_t          print)
7445 {
7446     uint32_t cur_bit = 0;
7447     int i = 0;
7448 
7449     for (i = 0; sig; i++) {
7450         cur_bit = ((uint32_t)0x1 << i);
7451         if (sig & cur_bit) {
7452             switch (cur_bit) {
7453             case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7454                 if (print)
7455                     bxe_print_next_block(sc, par_num++, "CSEMI");
7456                 break;
7457             case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7458                 if (print)
7459                     bxe_print_next_block(sc, par_num++, "PXP");
7460                 break;
7461             case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7462                 if (print)
7463                     bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7464                 break;
7465             case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7466                 if (print)
7467                     bxe_print_next_block(sc, par_num++, "CFC");
7468                 break;
7469             case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7470                 if (print)
7471                     bxe_print_next_block(sc, par_num++, "CDU");
7472                 break;
7473             case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7474                 if (print)
7475                     bxe_print_next_block(sc, par_num++, "DMAE");
7476                 break;
7477             case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7478                 if (print)
7479                     bxe_print_next_block(sc, par_num++, "IGU");
7480                 break;
7481             case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7482                 if (print)
7483                     bxe_print_next_block(sc, par_num++, "MISC");
7484                 break;
7485             }
7486 
7487             /* Clear the bit */
7488             sig &= ~cur_bit;
7489         }
7490     }
7491 
7492     return (par_num);
7493 }
7494 
7495 static int
7496 bxe_check_blocks_with_parity3(struct bxe_softc *sc,
7497                               uint32_t         sig,
7498                               int              par_num,
7499                               uint8_t          *global,
7500                               uint8_t          print)
7501 {
7502     uint32_t cur_bit = 0;
7503     int i = 0;
7504 
7505     for (i = 0; sig; i++) {
7506         cur_bit = ((uint32_t)0x1 << i);
7507         if (sig & cur_bit) {
7508             switch (cur_bit) {
7509             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7510                 if (print)
7511                     bxe_print_next_block(sc, par_num++, "MCP ROM");
7512                 *global = TRUE;
7513                 break;
7514             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7515                 if (print)
7516                     bxe_print_next_block(sc, par_num++,
7517                               "MCP UMP RX");
7518                 *global = TRUE;
7519                 break;
7520             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7521                 if (print)
7522                     bxe_print_next_block(sc, par_num++,
7523                               "MCP UMP TX");
7524                 *global = TRUE;
7525                 break;
7526             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7527                 if (print)
7528                     bxe_print_next_block(sc, par_num++,
7529                               "MCP SCPAD");
7530                 *global = TRUE;
7531                 break;
7532             }
7533 
7534             /* Clear the bit */
7535             sig &= ~cur_bit;
7536         }
7537     }
7538 
7539     return (par_num);
7540 }
7541 
7542 static int
7543 bxe_check_blocks_with_parity4(struct bxe_softc *sc,
7544                               uint32_t         sig,
7545                               int              par_num,
7546                               uint8_t          print)
7547 {
7548     uint32_t cur_bit = 0;
7549     int i = 0;
7550 
7551     for (i = 0; sig; i++) {
7552         cur_bit = ((uint32_t)0x1 << i);
7553         if (sig & cur_bit) {
7554             switch (cur_bit) {
7555             case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7556                 if (print)
7557                     bxe_print_next_block(sc, par_num++, "PGLUE_B");
7558                 break;
7559             case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7560                 if (print)
7561                     bxe_print_next_block(sc, par_num++, "ATC");
7562                 break;
7563             }
7564 
7565             /* Clear the bit */
7566             sig &= ~cur_bit;
7567         }
7568     }
7569 
7570     return (par_num);
7571 }
7572 
7573 static uint8_t
7574 bxe_parity_attn(struct bxe_softc *sc,
7575                 uint8_t          *global,
7576                 uint8_t          print,
7577                 uint32_t         *sig)
7578 {
7579     int par_num = 0;
7580 
7581     if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7582         (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7583         (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7584         (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7585         (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7586         BLOGE(sc, "Parity error: HW block parity attention:\n"
7587                   "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7588               (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7589               (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7590               (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7591               (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7592               (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7593 
7594         if (print)
7595             BLOGI(sc, "Parity errors detected in blocks: ");
7596 
7597         par_num =
7598             bxe_check_blocks_with_parity0(sc, sig[0] &
7599                                           HW_PRTY_ASSERT_SET_0,
7600                                           par_num, print);
7601         par_num =
7602             bxe_check_blocks_with_parity1(sc, sig[1] &
7603                                           HW_PRTY_ASSERT_SET_1,
7604                                           par_num, global, print);
7605         par_num =
7606             bxe_check_blocks_with_parity2(sc, sig[2] &
7607                                           HW_PRTY_ASSERT_SET_2,
7608                                           par_num, print);
7609         par_num =
7610             bxe_check_blocks_with_parity3(sc, sig[3] &
7611                                           HW_PRTY_ASSERT_SET_3,
7612                                           par_num, global, print);
7613         par_num =
7614             bxe_check_blocks_with_parity4(sc, sig[4] &
7615                                           HW_PRTY_ASSERT_SET_4,
7616                                           par_num, print);
7617 
7618         if (print)
7619             BLOGI(sc, "\n");
7620 
7621         return (TRUE);
7622     }
7623 
7624     return (FALSE);
7625 }
7626 
7627 static uint8_t
7628 bxe_chk_parity_attn(struct bxe_softc *sc,
7629                     uint8_t          *global,
7630                     uint8_t          print)
7631 {
7632     struct attn_route attn = { {0} };
7633     int port = SC_PORT(sc);
7634 
7635     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7636     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7637     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7638     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7639 
7640     /*
7641      * Since MCP attentions can't be disabled inside the block, we need to
7642      * read AEU registers to see whether they're currently disabled
7643      */
7644     attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7645                                       : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7646                          MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7647                         ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7648 
7649 
7650     if (!CHIP_IS_E1x(sc))
7651         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7652 
7653     return (bxe_parity_attn(sc, global, print, attn.sig));
7654 }
7655 
7656 static void
7657 bxe_attn_int_deasserted4(struct bxe_softc *sc,
7658                          uint32_t         attn)
7659 {
7660     uint32_t val;
7661 
7662     if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7663         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7664         BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7665         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7666             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7667         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7668             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7669         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7670             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7671         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7672             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7673         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7674             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7675         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7676             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7677         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7678             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7679         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7680             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7681         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7682             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7683     }
7684 
7685     if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7686         val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7687         BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7688         if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7689             BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7690         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7691             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7692         if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7693             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7694         if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7695             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7696         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7697             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7698         if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7699             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7700     }
7701 
7702     if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7703                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7704         BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7705               (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7706                                  AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7707     }
7708 }
7709 
7710 static void
7711 bxe_e1h_disable(struct bxe_softc *sc)
7712 {
7713     int port = SC_PORT(sc);
7714 
7715     bxe_tx_disable(sc);
7716 
7717     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7718 }
7719 
7720 static void
7721 bxe_e1h_enable(struct bxe_softc *sc)
7722 {
7723     int port = SC_PORT(sc);
7724 
7725     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7726 
7727     // XXX bxe_tx_enable(sc);
7728 }
7729 
7730 /*
7731  * called due to MCP event (on pmf):
7732  *   reread new bandwidth configuration
7733  *   configure FW
7734  *   notify others function about the change
7735  */
7736 static void
7737 bxe_config_mf_bw(struct bxe_softc *sc)
7738 {
7739     if (sc->link_vars.link_up) {
7740         bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7741         // XXX bxe_link_sync_notify(sc);
7742     }
7743 
7744     storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7745 }
7746 
7747 static void
7748 bxe_set_mf_bw(struct bxe_softc *sc)
7749 {
7750     bxe_config_mf_bw(sc);
7751     bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7752 }
7753 
7754 static void
7755 bxe_handle_eee_event(struct bxe_softc *sc)
7756 {
7757     BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7758     bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7759 }
7760 
7761 #define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7762 
7763 static void
7764 bxe_drv_info_ether_stat(struct bxe_softc *sc)
7765 {
7766     struct eth_stats_info *ether_stat =
7767         &sc->sp->drv_info_to_mcp.ether_stat;
7768 
7769     strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7770             ETH_STAT_INFO_VERSION_LEN);
7771 
7772     /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7773     sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7774                                           DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7775                                           ether_stat->mac_local + MAC_PAD,
7776                                           MAC_PAD, ETH_ALEN);
7777 
7778     ether_stat->mtu_size = sc->mtu;
7779 
7780     ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7781     if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
7782         ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7783     }
7784 
7785     // XXX ether_stat->feature_flags |= ???;
7786 
7787     ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7788 
7789     ether_stat->txq_size = sc->tx_ring_size;
7790     ether_stat->rxq_size = sc->rx_ring_size;
7791 }
7792 
7793 static void
7794 bxe_handle_drv_info_req(struct bxe_softc *sc)
7795 {
7796     enum drv_info_opcode op_code;
7797     uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7798 
7799     /* if drv_info version supported by MFW doesn't match - send NACK */
7800     if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7801         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7802         return;
7803     }
7804 
7805     op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7806                DRV_INFO_CONTROL_OP_CODE_SHIFT);
7807 
7808     memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7809 
7810     switch (op_code) {
7811     case ETH_STATS_OPCODE:
7812         bxe_drv_info_ether_stat(sc);
7813         break;
7814     case FCOE_STATS_OPCODE:
7815     case ISCSI_STATS_OPCODE:
7816     default:
7817         /* if op code isn't supported - send NACK */
7818         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7819         return;
7820     }
7821 
7822     /*
7823      * If we got drv_info attn from MFW then these fields are defined in
7824      * shmem2 for sure
7825      */
7826     SHMEM2_WR(sc, drv_info_host_addr_lo,
7827               U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7828     SHMEM2_WR(sc, drv_info_host_addr_hi,
7829               U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7830 
7831     bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7832 }
7833 
7834 static void
7835 bxe_dcc_event(struct bxe_softc *sc,
7836               uint32_t         dcc_event)
7837 {
7838     BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7839 
7840     if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7841         /*
7842          * This is the only place besides the function initialization
7843          * where the sc->flags can change so it is done without any
7844          * locks
7845          */
7846         if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7847             BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7848             sc->flags |= BXE_MF_FUNC_DIS;
7849             bxe_e1h_disable(sc);
7850         } else {
7851             BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7852             sc->flags &= ~BXE_MF_FUNC_DIS;
7853             bxe_e1h_enable(sc);
7854         }
7855         dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7856     }
7857 
7858     if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7859         bxe_config_mf_bw(sc);
7860         dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7861     }
7862 
7863     /* Report results to MCP */
7864     if (dcc_event)
7865         bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7866     else
7867         bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7868 }
7869 
7870 static void
7871 bxe_pmf_update(struct bxe_softc *sc)
7872 {
7873     int port = SC_PORT(sc);
7874     uint32_t val;
7875 
7876     sc->port.pmf = 1;
7877     BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7878 
7879     /*
7880      * We need the mb() to ensure the ordering between the writing to
7881      * sc->port.pmf here and reading it from the bxe_periodic_task().
7882      */
7883     mb();
7884 
7885     /* queue a periodic task */
7886     // XXX schedule task...
7887 
7888     // XXX bxe_dcbx_pmf_update(sc);
7889 
7890     /* enable nig attention */
7891     val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7892     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7893         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7894         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7895     } else if (!CHIP_IS_E1x(sc)) {
7896         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7897         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7898     }
7899 
7900     bxe_stats_handle(sc, STATS_EVENT_PMF);
7901 }
7902 
7903 static int
7904 bxe_mc_assert(struct bxe_softc *sc)
7905 {
7906     char last_idx;
7907     int i, rc = 0;
7908     uint32_t row0, row1, row2, row3;
7909 
7910     /* XSTORM */
7911     last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7912     if (last_idx)
7913         BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7914 
7915     /* print the asserts */
7916     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7917 
7918         row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7919         row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7920         row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7921         row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7922 
7923         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7924             BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7925                   i, row3, row2, row1, row0);
7926             rc++;
7927         } else {
7928             break;
7929         }
7930     }
7931 
7932     /* TSTORM */
7933     last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7934     if (last_idx) {
7935         BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7936     }
7937 
7938     /* print the asserts */
7939     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7940 
7941         row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7942         row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7943         row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7944         row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7945 
7946         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7947             BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7948                   i, row3, row2, row1, row0);
7949             rc++;
7950         } else {
7951             break;
7952         }
7953     }
7954 
7955     /* CSTORM */
7956     last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7957     if (last_idx) {
7958         BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7959     }
7960 
7961     /* print the asserts */
7962     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7963 
7964         row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7965         row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7966         row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7967         row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7968 
7969         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7970             BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7971                   i, row3, row2, row1, row0);
7972             rc++;
7973         } else {
7974             break;
7975         }
7976     }
7977 
7978     /* USTORM */
7979     last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7980     if (last_idx) {
7981         BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7982     }
7983 
7984     /* print the asserts */
7985     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7986 
7987         row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7988         row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7989         row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7990         row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7991 
7992         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7993             BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7994                   i, row3, row2, row1, row0);
7995             rc++;
7996         } else {
7997             break;
7998         }
7999     }
8000 
8001     return (rc);
8002 }
8003 
8004 static void
8005 bxe_attn_int_deasserted3(struct bxe_softc *sc,
8006                          uint32_t         attn)
8007 {
8008     int func = SC_FUNC(sc);
8009     uint32_t val;
8010 
8011     if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
8012 
8013         if (attn & BXE_PMF_LINK_ASSERT(sc)) {
8014 
8015             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
8016             bxe_read_mf_cfg(sc);
8017             sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
8018                 MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
8019             val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
8020 
8021             if (val & DRV_STATUS_DCC_EVENT_MASK)
8022                 bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
8023 
8024             if (val & DRV_STATUS_SET_MF_BW)
8025                 bxe_set_mf_bw(sc);
8026 
8027             if (val & DRV_STATUS_DRV_INFO_REQ)
8028                 bxe_handle_drv_info_req(sc);
8029 
8030             if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
8031                 bxe_pmf_update(sc);
8032 
8033             if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
8034                 bxe_handle_eee_event(sc);
8035 
8036             if (sc->link_vars.periodic_flags &
8037                 ELINK_PERIODIC_FLAGS_LINK_EVENT) {
8038                 /* sync with link */
8039 		bxe_acquire_phy_lock(sc);
8040                 sc->link_vars.periodic_flags &=
8041                     ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
8042 		bxe_release_phy_lock(sc);
8043                 if (IS_MF(sc))
8044                     ; // XXX bxe_link_sync_notify(sc);
8045                 bxe_link_report(sc);
8046             }
8047 
8048             /*
8049              * Always call it here: bxe_link_report() will
8050              * prevent the link indication duplication.
8051              */
8052             bxe_link_status_update(sc);
8053 
8054         } else if (attn & BXE_MC_ASSERT_BITS) {
8055 
8056             BLOGE(sc, "MC assert!\n");
8057             bxe_mc_assert(sc);
8058             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
8059             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
8060             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
8061             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
8062             bxe_panic(sc, ("MC assert!\n"));
8063 
8064         } else if (attn & BXE_MCP_ASSERT) {
8065 
8066             BLOGE(sc, "MCP assert!\n");
8067             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8068             // XXX bxe_fw_dump(sc);
8069 
8070         } else {
8071             BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8072         }
8073     }
8074 
8075     if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8076         BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8077         if (attn & BXE_GRC_TIMEOUT) {
8078             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8079             BLOGE(sc, "GRC time-out 0x%08x\n", val);
8080         }
8081         if (attn & BXE_GRC_RSV) {
8082             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8083             BLOGE(sc, "GRC reserved 0x%08x\n", val);
8084         }
8085         REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8086     }
8087 }
8088 
8089 static void
8090 bxe_attn_int_deasserted2(struct bxe_softc *sc,
8091                          uint32_t         attn)
8092 {
8093     int port = SC_PORT(sc);
8094     int reg_offset;
8095     uint32_t val0, mask0, val1, mask1;
8096     uint32_t val;
8097 
8098     if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8099         val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8100         BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8101         /* CFC error attention */
8102         if (val & 0x2) {
8103             BLOGE(sc, "FATAL error from CFC\n");
8104         }
8105     }
8106 
8107     if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8108         val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8109         BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8110         /* RQ_USDMDP_FIFO_OVERFLOW */
8111         if (val & 0x18000) {
8112             BLOGE(sc, "FATAL error from PXP\n");
8113         }
8114 
8115         if (!CHIP_IS_E1x(sc)) {
8116             val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8117             BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8118         }
8119     }
8120 
8121 #define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8122 #define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8123 
8124     if (attn & AEU_PXP2_HW_INT_BIT) {
8125         /*  CQ47854 workaround do not panic on
8126          *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8127          */
8128         if (!CHIP_IS_E1x(sc)) {
8129             mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8130             val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8131             mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8132             val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8133             /*
8134              * If the olny PXP2_EOP_ERROR_BIT is set in
8135              * STS0 and STS1 - clear it
8136              *
8137              * probably we lose additional attentions between
8138              * STS0 and STS_CLR0, in this case user will not
8139              * be notified about them
8140              */
8141             if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8142                 !(val1 & mask1))
8143                 val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8144 
8145             /* print the register, since no one can restore it */
8146             BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8147 
8148             /*
8149              * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8150              * then notify
8151              */
8152             if (val0 & PXP2_EOP_ERROR_BIT) {
8153                 BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8154 
8155                 /*
8156                  * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8157                  * set then clear attention from PXP2 block without panic
8158                  */
8159                 if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8160                     ((val1 & mask1) == 0))
8161                     attn &= ~AEU_PXP2_HW_INT_BIT;
8162             }
8163         }
8164     }
8165 
8166     if (attn & HW_INTERRUT_ASSERT_SET_2) {
8167         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8168                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8169 
8170         val = REG_RD(sc, reg_offset);
8171         val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8172         REG_WR(sc, reg_offset, val);
8173 
8174         BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8175               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8176         bxe_panic(sc, ("HW block attention set2\n"));
8177     }
8178 }
8179 
8180 static void
8181 bxe_attn_int_deasserted1(struct bxe_softc *sc,
8182                          uint32_t         attn)
8183 {
8184     int port = SC_PORT(sc);
8185     int reg_offset;
8186     uint32_t val;
8187 
8188     if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8189         val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8190         BLOGE(sc, "DB hw attention 0x%08x\n", val);
8191         /* DORQ discard attention */
8192         if (val & 0x2) {
8193             BLOGE(sc, "FATAL error from DORQ\n");
8194         }
8195     }
8196 
8197     if (attn & HW_INTERRUT_ASSERT_SET_1) {
8198         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8199                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8200 
8201         val = REG_RD(sc, reg_offset);
8202         val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8203         REG_WR(sc, reg_offset, val);
8204 
8205         BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8206               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8207         bxe_panic(sc, ("HW block attention set1\n"));
8208     }
8209 }
8210 
8211 static void
8212 bxe_attn_int_deasserted0(struct bxe_softc *sc,
8213                          uint32_t         attn)
8214 {
8215     int port = SC_PORT(sc);
8216     int reg_offset;
8217     uint32_t val;
8218 
8219     reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8220                           MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8221 
8222     if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8223         val = REG_RD(sc, reg_offset);
8224         val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8225         REG_WR(sc, reg_offset, val);
8226 
8227         BLOGW(sc, "SPIO5 hw attention\n");
8228 
8229         /* Fan failure attention */
8230         elink_hw_reset_phy(&sc->link_params);
8231         bxe_fan_failure(sc);
8232     }
8233 
8234     if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8235 	bxe_acquire_phy_lock(sc);
8236         elink_handle_module_detect_int(&sc->link_params);
8237 	bxe_release_phy_lock(sc);
8238     }
8239 
8240     if (attn & HW_INTERRUT_ASSERT_SET_0) {
8241         val = REG_RD(sc, reg_offset);
8242         val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8243         REG_WR(sc, reg_offset, val);
8244 
8245         bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8246                        (attn & HW_INTERRUT_ASSERT_SET_0)));
8247     }
8248 }
8249 
8250 static void
8251 bxe_attn_int_deasserted(struct bxe_softc *sc,
8252                         uint32_t         deasserted)
8253 {
8254     struct attn_route attn;
8255     struct attn_route *group_mask;
8256     int port = SC_PORT(sc);
8257     int index;
8258     uint32_t reg_addr;
8259     uint32_t val;
8260     uint32_t aeu_mask;
8261     uint8_t global = FALSE;
8262 
8263     /*
8264      * Need to take HW lock because MCP or other port might also
8265      * try to handle this event.
8266      */
8267     bxe_acquire_alr(sc);
8268 
8269     if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8270         /* XXX
8271          * In case of parity errors don't handle attentions so that
8272          * other function would "see" parity errors.
8273          */
8274         sc->recovery_state = BXE_RECOVERY_INIT;
8275         // XXX schedule a recovery task...
8276         /* disable HW interrupts */
8277         bxe_int_disable(sc);
8278         bxe_release_alr(sc);
8279         return;
8280     }
8281 
8282     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8283     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8284     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8285     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8286     if (!CHIP_IS_E1x(sc)) {
8287         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8288     } else {
8289         attn.sig[4] = 0;
8290     }
8291 
8292     BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8293           attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8294 
8295     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8296         if (deasserted & (1 << index)) {
8297             group_mask = &sc->attn_group[index];
8298 
8299             BLOGD(sc, DBG_INTR,
8300                   "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8301                   group_mask->sig[0], group_mask->sig[1],
8302                   group_mask->sig[2], group_mask->sig[3],
8303                   group_mask->sig[4]);
8304 
8305             bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8306             bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8307             bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8308             bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8309             bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8310         }
8311     }
8312 
8313     bxe_release_alr(sc);
8314 
8315     if (sc->devinfo.int_block == INT_BLOCK_HC) {
8316         reg_addr = (HC_REG_COMMAND_REG + port*32 +
8317                     COMMAND_REG_ATTN_BITS_CLR);
8318     } else {
8319         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8320     }
8321 
8322     val = ~deasserted;
8323     BLOGD(sc, DBG_INTR,
8324           "about to mask 0x%08x at %s addr 0x%08x\n", val,
8325           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8326     REG_WR(sc, reg_addr, val);
8327 
8328     if (~sc->attn_state & deasserted) {
8329         BLOGE(sc, "IGU error\n");
8330     }
8331 
8332     reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8333                       MISC_REG_AEU_MASK_ATTN_FUNC_0;
8334 
8335     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8336 
8337     aeu_mask = REG_RD(sc, reg_addr);
8338 
8339     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8340           aeu_mask, deasserted);
8341     aeu_mask |= (deasserted & 0x3ff);
8342     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8343 
8344     REG_WR(sc, reg_addr, aeu_mask);
8345     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8346 
8347     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8348     sc->attn_state &= ~deasserted;
8349     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8350 }
8351 
8352 static void
8353 bxe_attn_int(struct bxe_softc *sc)
8354 {
8355     /* read local copy of bits */
8356     uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8357     uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8358     uint32_t attn_state = sc->attn_state;
8359 
8360     /* look for changed bits */
8361     uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8362     uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8363 
8364     BLOGD(sc, DBG_INTR,
8365           "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8366           attn_bits, attn_ack, asserted, deasserted);
8367 
8368     if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8369         BLOGE(sc, "BAD attention state\n");
8370     }
8371 
8372     /* handle bits that were raised */
8373     if (asserted) {
8374         bxe_attn_int_asserted(sc, asserted);
8375     }
8376 
8377     if (deasserted) {
8378         bxe_attn_int_deasserted(sc, deasserted);
8379     }
8380 }
8381 
8382 static uint16_t
8383 bxe_update_dsb_idx(struct bxe_softc *sc)
8384 {
8385     struct host_sp_status_block *def_sb = sc->def_sb;
8386     uint16_t rc = 0;
8387 
8388     mb(); /* status block is written to by the chip */
8389 
8390     if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8391         sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8392         rc |= BXE_DEF_SB_ATT_IDX;
8393     }
8394 
8395     if (sc->def_idx != def_sb->sp_sb.running_index) {
8396         sc->def_idx = def_sb->sp_sb.running_index;
8397         rc |= BXE_DEF_SB_IDX;
8398     }
8399 
8400     mb();
8401 
8402     return (rc);
8403 }
8404 
8405 static inline struct ecore_queue_sp_obj *
8406 bxe_cid_to_q_obj(struct bxe_softc *sc,
8407                  uint32_t         cid)
8408 {
8409     BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8410     return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8411 }
8412 
8413 static void
8414 bxe_handle_mcast_eqe(struct bxe_softc *sc)
8415 {
8416     struct ecore_mcast_ramrod_params rparam;
8417     int rc;
8418 
8419     memset(&rparam, 0, sizeof(rparam));
8420 
8421     rparam.mcast_obj = &sc->mcast_obj;
8422 
8423     BXE_MCAST_LOCK(sc);
8424 
8425     /* clear pending state for the last command */
8426     sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8427 
8428     /* if there are pending mcast commands - send them */
8429     if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8430         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8431         if (rc < 0) {
8432             BLOGD(sc, DBG_SP,
8433                 "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8434         }
8435     }
8436 
8437     BXE_MCAST_UNLOCK(sc);
8438 }
8439 
8440 static void
8441 bxe_handle_classification_eqe(struct bxe_softc      *sc,
8442                               union event_ring_elem *elem)
8443 {
8444     unsigned long ramrod_flags = 0;
8445     int rc = 0;
8446     uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8447     struct ecore_vlan_mac_obj *vlan_mac_obj;
8448 
8449     /* always push next commands out, don't wait here */
8450     bit_set(&ramrod_flags, RAMROD_CONT);
8451 
8452     switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8453     case ECORE_FILTER_MAC_PENDING:
8454         BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8455         vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8456         break;
8457 
8458     case ECORE_FILTER_MCAST_PENDING:
8459         BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8460         /*
8461          * This is only relevant for 57710 where multicast MACs are
8462          * configured as unicast MACs using the same ramrod.
8463          */
8464         bxe_handle_mcast_eqe(sc);
8465         return;
8466 
8467     default:
8468         BLOGE(sc, "Unsupported classification command: %d\n",
8469               elem->message.data.eth_event.echo);
8470         return;
8471     }
8472 
8473     rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8474 
8475     if (rc < 0) {
8476         BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8477     } else if (rc > 0) {
8478         BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8479     }
8480 }
8481 
8482 static void
8483 bxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8484                        union event_ring_elem *elem)
8485 {
8486     bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8487 
8488     /* send rx_mode command again if was requested */
8489     if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8490                                &sc->sp_state)) {
8491         bxe_set_storm_rx_mode(sc);
8492     }
8493 }
8494 
8495 static void
8496 bxe_update_eq_prod(struct bxe_softc *sc,
8497                    uint16_t         prod)
8498 {
8499     storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8500     wmb(); /* keep prod updates ordered */
8501 }
8502 
8503 static void
8504 bxe_eq_int(struct bxe_softc *sc)
8505 {
8506     uint16_t hw_cons, sw_cons, sw_prod;
8507     union event_ring_elem *elem;
8508     uint8_t echo;
8509     uint32_t cid;
8510     uint8_t opcode;
8511     int spqe_cnt = 0;
8512     struct ecore_queue_sp_obj *q_obj;
8513     struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8514     struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8515 
8516     hw_cons = le16toh(*sc->eq_cons_sb);
8517 
8518     /*
8519      * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8520      * when we get to the next-page we need to adjust so the loop
8521      * condition below will be met. The next element is the size of a
8522      * regular element and hence incrementing by 1
8523      */
8524     if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8525         hw_cons++;
8526     }
8527 
8528     /*
8529      * This function may never run in parallel with itself for a
8530      * specific sc and no need for a read memory barrier here.
8531      */
8532     sw_cons = sc->eq_cons;
8533     sw_prod = sc->eq_prod;
8534 
8535     BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8536           hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8537 
8538     for (;
8539          sw_cons != hw_cons;
8540          sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8541 
8542         elem = &sc->eq[EQ_DESC(sw_cons)];
8543 
8544         /* elem CID originates from FW, actually LE */
8545         cid = SW_CID(elem->message.data.cfc_del_event.cid);
8546         opcode = elem->message.opcode;
8547 
8548         /* handle eq element */
8549         switch (opcode) {
8550 
8551         case EVENT_RING_OPCODE_STAT_QUERY:
8552             BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8553                   sc->stats_comp++);
8554             /* nothing to do with stats comp */
8555             goto next_spqe;
8556 
8557         case EVENT_RING_OPCODE_CFC_DEL:
8558             /* handle according to cid range */
8559             /* we may want to verify here that the sc state is HALTING */
8560             BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8561             q_obj = bxe_cid_to_q_obj(sc, cid);
8562             if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8563                 break;
8564             }
8565             goto next_spqe;
8566 
8567         case EVENT_RING_OPCODE_STOP_TRAFFIC:
8568             BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8569             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8570                 break;
8571             }
8572             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8573             goto next_spqe;
8574 
8575         case EVENT_RING_OPCODE_START_TRAFFIC:
8576             BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8577             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8578                 break;
8579             }
8580             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8581             goto next_spqe;
8582 
8583         case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8584             echo = elem->message.data.function_update_event.echo;
8585             if (echo == SWITCH_UPDATE) {
8586                 BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8587                 if (f_obj->complete_cmd(sc, f_obj,
8588                                         ECORE_F_CMD_SWITCH_UPDATE)) {
8589                     break;
8590                 }
8591             }
8592             else {
8593                 BLOGD(sc, DBG_SP,
8594                       "AFEX: ramrod completed FUNCTION_UPDATE\n");
8595             }
8596             goto next_spqe;
8597 
8598         case EVENT_RING_OPCODE_FORWARD_SETUP:
8599             q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8600             if (q_obj->complete_cmd(sc, q_obj,
8601                                     ECORE_Q_CMD_SETUP_TX_ONLY)) {
8602                 break;
8603             }
8604             goto next_spqe;
8605 
8606         case EVENT_RING_OPCODE_FUNCTION_START:
8607             BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8608             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8609                 break;
8610             }
8611             goto next_spqe;
8612 
8613         case EVENT_RING_OPCODE_FUNCTION_STOP:
8614             BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8615             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8616                 break;
8617             }
8618             goto next_spqe;
8619         }
8620 
8621         switch (opcode | sc->state) {
8622         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8623         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8624             cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8625             BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8626             rss_raw->clear_pending(rss_raw);
8627             break;
8628 
8629         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8630         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8631         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8632         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8633         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8634         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8635             BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8636             bxe_handle_classification_eqe(sc, elem);
8637             break;
8638 
8639         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8640         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8641         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8642             BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8643             bxe_handle_mcast_eqe(sc);
8644             break;
8645 
8646         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8647         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8648         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8649             BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8650             bxe_handle_rx_mode_eqe(sc, elem);
8651             break;
8652 
8653         default:
8654             /* unknown event log error and continue */
8655             BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8656                   elem->message.opcode, sc->state);
8657         }
8658 
8659 next_spqe:
8660         spqe_cnt++;
8661     } /* for */
8662 
8663     mb();
8664     atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8665 
8666     sc->eq_cons = sw_cons;
8667     sc->eq_prod = sw_prod;
8668 
8669     /* make sure that above mem writes were issued towards the memory */
8670     wmb();
8671 
8672     /* update producer */
8673     bxe_update_eq_prod(sc, sc->eq_prod);
8674 }
8675 
8676 static void
8677 bxe_handle_sp_tq(void *context,
8678                  int  pending)
8679 {
8680     struct bxe_softc *sc = (struct bxe_softc *)context;
8681     uint16_t status;
8682 
8683     BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8684 
8685     /* what work needs to be performed? */
8686     status = bxe_update_dsb_idx(sc);
8687 
8688     BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8689 
8690     /* HW attentions */
8691     if (status & BXE_DEF_SB_ATT_IDX) {
8692         BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8693         bxe_attn_int(sc);
8694         status &= ~BXE_DEF_SB_ATT_IDX;
8695     }
8696 
8697     /* SP events: STAT_QUERY and others */
8698     if (status & BXE_DEF_SB_IDX) {
8699         /* handle EQ completions */
8700         BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8701         bxe_eq_int(sc);
8702         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8703                    le16toh(sc->def_idx), IGU_INT_NOP, 1);
8704         status &= ~BXE_DEF_SB_IDX;
8705     }
8706 
8707     /* if status is non zero then something went wrong */
8708     if (__predict_false(status)) {
8709         BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8710     }
8711 
8712     /* ack status block only if something was actually handled */
8713     bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8714                le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8715 
8716     /*
8717      * Must be called after the EQ processing (since eq leads to sriov
8718      * ramrod completion flows).
8719      * This flow may have been scheduled by the arrival of a ramrod
8720      * completion, or by the sriov code rescheduling itself.
8721      */
8722     // XXX bxe_iov_sp_task(sc);
8723 
8724 }
8725 
8726 static void
8727 bxe_handle_fp_tq(void *context,
8728                  int  pending)
8729 {
8730     struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8731     struct bxe_softc *sc = fp->sc;
8732     uint8_t more_tx = FALSE;
8733     uint8_t more_rx = FALSE;
8734 
8735     BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8736 
8737     /* XXX
8738      * IFF_DRV_RUNNING state can't be checked here since we process
8739      * slowpath events on a client queue during setup. Instead
8740      * we need to add a "process/continue" flag here that the driver
8741      * can use to tell the task here not to do anything.
8742      */
8743 #if 0
8744     if (!(if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING)) {
8745         return;
8746     }
8747 #endif
8748 
8749     /* update the fastpath index */
8750     bxe_update_fp_sb_idx(fp);
8751 
8752     /* XXX add loop here if ever support multiple tx CoS */
8753     /* fp->txdata[cos] */
8754     if (bxe_has_tx_work(fp)) {
8755         BXE_FP_TX_LOCK(fp);
8756         more_tx = bxe_txeof(sc, fp);
8757         BXE_FP_TX_UNLOCK(fp);
8758     }
8759 
8760     if (bxe_has_rx_work(fp)) {
8761         more_rx = bxe_rxeof(sc, fp);
8762     }
8763 
8764     if (more_rx /*|| more_tx*/) {
8765         /* still more work to do */
8766         taskqueue_enqueue(fp->tq, &fp->tq_task);
8767         return;
8768     }
8769 
8770     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8771                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8772 }
8773 
8774 static void
8775 bxe_task_fp(struct bxe_fastpath *fp)
8776 {
8777     struct bxe_softc *sc = fp->sc;
8778     uint8_t more_tx = FALSE;
8779     uint8_t more_rx = FALSE;
8780 
8781     BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8782 
8783     /* update the fastpath index */
8784     bxe_update_fp_sb_idx(fp);
8785 
8786     /* XXX add loop here if ever support multiple tx CoS */
8787     /* fp->txdata[cos] */
8788     if (bxe_has_tx_work(fp)) {
8789         BXE_FP_TX_LOCK(fp);
8790         more_tx = bxe_txeof(sc, fp);
8791         BXE_FP_TX_UNLOCK(fp);
8792     }
8793 
8794     if (bxe_has_rx_work(fp)) {
8795         more_rx = bxe_rxeof(sc, fp);
8796     }
8797 
8798     if (more_rx /*|| more_tx*/) {
8799         /* still more work to do, bail out if this ISR and process later */
8800         taskqueue_enqueue(fp->tq, &fp->tq_task);
8801         return;
8802     }
8803 
8804     /*
8805      * Here we write the fastpath index taken before doing any tx or rx work.
8806      * It is very well possible other hw events occurred up to this point and
8807      * they were actually processed accordingly above. Since we're going to
8808      * write an older fastpath index, an interrupt is coming which we might
8809      * not do any work in.
8810      */
8811     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8812                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8813 }
8814 
8815 /*
8816  * Legacy interrupt entry point.
8817  *
8818  * Verifies that the controller generated the interrupt and
8819  * then calls a separate routine to handle the various
8820  * interrupt causes: link, RX, and TX.
8821  */
8822 static void
8823 bxe_intr_legacy(void *xsc)
8824 {
8825     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8826     struct bxe_fastpath *fp;
8827     uint16_t status, mask;
8828     int i;
8829 
8830     BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8831 
8832     /*
8833      * 0 for ustorm, 1 for cstorm
8834      * the bits returned from ack_int() are 0-15
8835      * bit 0 = attention status block
8836      * bit 1 = fast path status block
8837      * a mask of 0x2 or more = tx/rx event
8838      * a mask of 1 = slow path event
8839      */
8840 
8841     status = bxe_ack_int(sc);
8842 
8843     /* the interrupt is not for us */
8844     if (__predict_false(status == 0)) {
8845         BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8846         return;
8847     }
8848 
8849     BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8850 
8851     FOR_EACH_ETH_QUEUE(sc, i) {
8852         fp = &sc->fp[i];
8853         mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8854         if (status & mask) {
8855             /* acknowledge and disable further fastpath interrupts */
8856             bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8857             bxe_task_fp(fp);
8858             status &= ~mask;
8859         }
8860     }
8861 
8862     if (__predict_false(status & 0x1)) {
8863         /* acknowledge and disable further slowpath interrupts */
8864         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8865 
8866         /* schedule slowpath handler */
8867         taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8868 
8869         status &= ~0x1;
8870     }
8871 
8872     if (__predict_false(status)) {
8873         BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8874     }
8875 }
8876 
8877 /* slowpath interrupt entry point */
8878 static void
8879 bxe_intr_sp(void *xsc)
8880 {
8881     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8882 
8883     BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8884 
8885     /* acknowledge and disable further slowpath interrupts */
8886     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8887 
8888     /* schedule slowpath handler */
8889     taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8890 }
8891 
8892 /* fastpath interrupt entry point */
8893 static void
8894 bxe_intr_fp(void *xfp)
8895 {
8896     struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8897     struct bxe_softc *sc = fp->sc;
8898 
8899     BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8900 
8901     BLOGD(sc, DBG_INTR,
8902           "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8903           curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8904 
8905     /* acknowledge and disable further fastpath interrupts */
8906     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8907 
8908     bxe_task_fp(fp);
8909 }
8910 
8911 /* Release all interrupts allocated by the driver. */
8912 static void
8913 bxe_interrupt_free(struct bxe_softc *sc)
8914 {
8915     int i;
8916 
8917     switch (sc->interrupt_mode) {
8918     case INTR_MODE_INTX:
8919         BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8920         if (sc->intr[0].resource != NULL) {
8921             bus_release_resource(sc->dev,
8922                                  SYS_RES_IRQ,
8923                                  sc->intr[0].rid,
8924                                  sc->intr[0].resource);
8925         }
8926         break;
8927     case INTR_MODE_MSI:
8928         for (i = 0; i < sc->intr_count; i++) {
8929             BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
8930             if (sc->intr[i].resource && sc->intr[i].rid) {
8931                 bus_release_resource(sc->dev,
8932                                      SYS_RES_IRQ,
8933                                      sc->intr[i].rid,
8934                                      sc->intr[i].resource);
8935             }
8936         }
8937         pci_release_msi(sc->dev);
8938         break;
8939     case INTR_MODE_MSIX:
8940         for (i = 0; i < sc->intr_count; i++) {
8941             BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8942             if (sc->intr[i].resource && sc->intr[i].rid) {
8943                 bus_release_resource(sc->dev,
8944                                      SYS_RES_IRQ,
8945                                      sc->intr[i].rid,
8946                                      sc->intr[i].resource);
8947             }
8948         }
8949         pci_release_msi(sc->dev);
8950         break;
8951     default:
8952         /* nothing to do as initial allocation failed */
8953         break;
8954     }
8955 }
8956 
8957 /*
8958  * This function determines and allocates the appropriate
8959  * interrupt based on system capabilites and user request.
8960  *
8961  * The user may force a particular interrupt mode, specify
8962  * the number of receive queues, specify the method for
8963  * distribuitng received frames to receive queues, or use
8964  * the default settings which will automatically select the
8965  * best supported combination.  In addition, the OS may or
8966  * may not support certain combinations of these settings.
8967  * This routine attempts to reconcile the settings requested
8968  * by the user with the capabilites available from the system
8969  * to select the optimal combination of features.
8970  *
8971  * Returns:
8972  *   0 = Success, !0 = Failure.
8973  */
8974 static int
8975 bxe_interrupt_alloc(struct bxe_softc *sc)
8976 {
8977     int msix_count = 0;
8978     int msi_count = 0;
8979     int num_requested = 0;
8980     int num_allocated = 0;
8981     int rid, i, j;
8982     int rc;
8983 
8984     /* get the number of available MSI/MSI-X interrupts from the OS */
8985     if (sc->interrupt_mode > 0) {
8986         if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8987             msix_count = pci_msix_count(sc->dev);
8988         }
8989 
8990         if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
8991             msi_count = pci_msi_count(sc->dev);
8992         }
8993 
8994         BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
8995               msi_count, msix_count);
8996     }
8997 
8998     do { /* try allocating MSI-X interrupt resources (at least 2) */
8999         if (sc->interrupt_mode != INTR_MODE_MSIX) {
9000             break;
9001         }
9002 
9003         if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
9004             (msix_count < 2)) {
9005             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9006             break;
9007         }
9008 
9009         /* ask for the necessary number of MSI-X vectors */
9010         num_requested = min((sc->num_queues + 1), msix_count);
9011 
9012         BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
9013 
9014         num_allocated = num_requested;
9015         if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
9016             BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
9017             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9018             break;
9019         }
9020 
9021         if (num_allocated < 2) { /* possible? */
9022             BLOGE(sc, "MSI-X allocation less than 2!\n");
9023             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9024             pci_release_msi(sc->dev);
9025             break;
9026         }
9027 
9028         BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
9029               num_requested, num_allocated);
9030 
9031         /* best effort so use the number of vectors allocated to us */
9032         sc->intr_count = num_allocated;
9033         sc->num_queues = num_allocated - 1;
9034 
9035         rid = 1; /* initial resource identifier */
9036 
9037         /* allocate the MSI-X vectors */
9038         for (i = 0; i < num_allocated; i++) {
9039             sc->intr[i].rid = (rid + i);
9040 
9041             if ((sc->intr[i].resource =
9042                  bus_alloc_resource_any(sc->dev,
9043                                         SYS_RES_IRQ,
9044                                         &sc->intr[i].rid,
9045                                         RF_ACTIVE)) == NULL) {
9046                 BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
9047                       i, (rid + i));
9048 
9049                 for (j = (i - 1); j >= 0; j--) {
9050                     bus_release_resource(sc->dev,
9051                                          SYS_RES_IRQ,
9052                                          sc->intr[j].rid,
9053                                          sc->intr[j].resource);
9054                 }
9055 
9056                 sc->intr_count = 0;
9057                 sc->num_queues = 0;
9058                 sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9059                 pci_release_msi(sc->dev);
9060                 break;
9061             }
9062 
9063             BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9064         }
9065     } while (0);
9066 
9067     do { /* try allocating MSI vector resources (at least 2) */
9068         if (sc->interrupt_mode != INTR_MODE_MSI) {
9069             break;
9070         }
9071 
9072         if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9073             (msi_count < 1)) {
9074             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9075             break;
9076         }
9077 
9078         /* ask for a single MSI vector */
9079         num_requested = 1;
9080 
9081         BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9082 
9083         num_allocated = num_requested;
9084         if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9085             BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9086             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9087             break;
9088         }
9089 
9090         if (num_allocated != 1) { /* possible? */
9091             BLOGE(sc, "MSI allocation is not 1!\n");
9092             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9093             pci_release_msi(sc->dev);
9094             break;
9095         }
9096 
9097         BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9098               num_requested, num_allocated);
9099 
9100         /* best effort so use the number of vectors allocated to us */
9101         sc->intr_count = num_allocated;
9102         sc->num_queues = num_allocated;
9103 
9104         rid = 1; /* initial resource identifier */
9105 
9106         sc->intr[0].rid = rid;
9107 
9108         if ((sc->intr[0].resource =
9109              bus_alloc_resource_any(sc->dev,
9110                                     SYS_RES_IRQ,
9111                                     &sc->intr[0].rid,
9112                                     RF_ACTIVE)) == NULL) {
9113             BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9114             sc->intr_count = 0;
9115             sc->num_queues = 0;
9116             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9117             pci_release_msi(sc->dev);
9118             break;
9119         }
9120 
9121         BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9122     } while (0);
9123 
9124     do { /* try allocating INTx vector resources */
9125         if (sc->interrupt_mode != INTR_MODE_INTX) {
9126             break;
9127         }
9128 
9129         BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9130 
9131         /* only one vector for INTx */
9132         sc->intr_count = 1;
9133         sc->num_queues = 1;
9134 
9135         rid = 0; /* initial resource identifier */
9136 
9137         sc->intr[0].rid = rid;
9138 
9139         if ((sc->intr[0].resource =
9140              bus_alloc_resource_any(sc->dev,
9141                                     SYS_RES_IRQ,
9142                                     &sc->intr[0].rid,
9143                                     (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9144             BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9145             sc->intr_count = 0;
9146             sc->num_queues = 0;
9147             sc->interrupt_mode = -1; /* Failed! */
9148             break;
9149         }
9150 
9151         BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9152     } while (0);
9153 
9154     if (sc->interrupt_mode == -1) {
9155         BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9156         rc = 1;
9157     } else {
9158         BLOGD(sc, DBG_LOAD,
9159               "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9160               sc->interrupt_mode, sc->num_queues);
9161         rc = 0;
9162     }
9163 
9164     return (rc);
9165 }
9166 
9167 static void
9168 bxe_interrupt_detach(struct bxe_softc *sc)
9169 {
9170     struct bxe_fastpath *fp;
9171     int i;
9172 
9173     /* release interrupt resources */
9174     for (i = 0; i < sc->intr_count; i++) {
9175         if (sc->intr[i].resource && sc->intr[i].tag) {
9176             BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9177             bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9178         }
9179     }
9180 
9181     for (i = 0; i < sc->num_queues; i++) {
9182         fp = &sc->fp[i];
9183         if (fp->tq) {
9184             taskqueue_drain(fp->tq, &fp->tq_task);
9185             taskqueue_free(fp->tq);
9186             fp->tq = NULL;
9187         }
9188     }
9189 
9190 
9191     if (sc->sp_tq) {
9192         taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9193         taskqueue_free(sc->sp_tq);
9194         sc->sp_tq = NULL;
9195     }
9196 }
9197 
9198 /*
9199  * Enables interrupts and attach to the ISR.
9200  *
9201  * When using multiple MSI/MSI-X vectors the first vector
9202  * is used for slowpath operations while all remaining
9203  * vectors are used for fastpath operations.  If only a
9204  * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9205  * ISR must look for both slowpath and fastpath completions.
9206  */
9207 static int
9208 bxe_interrupt_attach(struct bxe_softc *sc)
9209 {
9210     struct bxe_fastpath *fp;
9211     int rc = 0;
9212     int i;
9213 
9214     snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9215              "bxe%d_sp_tq", sc->unit);
9216     TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9217     sc->sp_tq = taskqueue_create_fast(sc->sp_tq_name, M_NOWAIT,
9218                                       taskqueue_thread_enqueue,
9219                                       &sc->sp_tq);
9220     taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9221                             "%s", sc->sp_tq_name);
9222 
9223 
9224     for (i = 0; i < sc->num_queues; i++) {
9225         fp = &sc->fp[i];
9226         snprintf(fp->tq_name, sizeof(fp->tq_name),
9227                  "bxe%d_fp%d_tq", sc->unit, i);
9228         TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9229         fp->tq = taskqueue_create_fast(fp->tq_name, M_NOWAIT,
9230                                        taskqueue_thread_enqueue,
9231                                        &fp->tq);
9232         taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9233                                 "%s", fp->tq_name);
9234     }
9235 
9236     /* setup interrupt handlers */
9237     if (sc->interrupt_mode == INTR_MODE_MSIX) {
9238         BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9239 
9240         /*
9241          * Setup the interrupt handler. Note that we pass the driver instance
9242          * to the interrupt handler for the slowpath.
9243          */
9244         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9245                                  (INTR_TYPE_NET | INTR_MPSAFE),
9246                                  NULL, bxe_intr_sp, sc,
9247                                  &sc->intr[0].tag)) != 0) {
9248             BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9249             goto bxe_interrupt_attach_exit;
9250         }
9251 
9252         bus_describe_intr(sc->dev, sc->intr[0].resource,
9253                           sc->intr[0].tag, "sp");
9254 
9255         /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9256 
9257         /* initialize the fastpath vectors (note the first was used for sp) */
9258         for (i = 0; i < sc->num_queues; i++) {
9259             fp = &sc->fp[i];
9260             BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9261 
9262             /*
9263              * Setup the interrupt handler. Note that we pass the
9264              * fastpath context to the interrupt handler in this
9265              * case.
9266              */
9267             if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9268                                      (INTR_TYPE_NET | INTR_MPSAFE),
9269                                      NULL, bxe_intr_fp, fp,
9270                                      &sc->intr[i + 1].tag)) != 0) {
9271                 BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9272                       (i + 1), rc);
9273                 goto bxe_interrupt_attach_exit;
9274             }
9275 
9276             bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9277                               sc->intr[i + 1].tag, "fp%02d", i);
9278 
9279             /* bind the fastpath instance to a cpu */
9280             if (sc->num_queues > 1) {
9281                 bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9282             }
9283 
9284             fp->state = BXE_FP_STATE_IRQ;
9285         }
9286     } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9287         BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9288 
9289         /*
9290          * Setup the interrupt handler. Note that we pass the
9291          * driver instance to the interrupt handler which
9292          * will handle both the slowpath and fastpath.
9293          */
9294         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9295                                  (INTR_TYPE_NET | INTR_MPSAFE),
9296                                  NULL, bxe_intr_legacy, sc,
9297                                  &sc->intr[0].tag)) != 0) {
9298             BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9299             goto bxe_interrupt_attach_exit;
9300         }
9301 
9302     } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9303         BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9304 
9305         /*
9306          * Setup the interrupt handler. Note that we pass the
9307          * driver instance to the interrupt handler which
9308          * will handle both the slowpath and fastpath.
9309          */
9310         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9311                                  (INTR_TYPE_NET | INTR_MPSAFE),
9312                                  NULL, bxe_intr_legacy, sc,
9313                                  &sc->intr[0].tag)) != 0) {
9314             BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9315             goto bxe_interrupt_attach_exit;
9316         }
9317     }
9318 
9319 bxe_interrupt_attach_exit:
9320 
9321     return (rc);
9322 }
9323 
9324 static int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9325 static int  bxe_init_hw_common(struct bxe_softc *sc);
9326 static int  bxe_init_hw_port(struct bxe_softc *sc);
9327 static int  bxe_init_hw_func(struct bxe_softc *sc);
9328 static void bxe_reset_common(struct bxe_softc *sc);
9329 static void bxe_reset_port(struct bxe_softc *sc);
9330 static void bxe_reset_func(struct bxe_softc *sc);
9331 static int  bxe_gunzip_init(struct bxe_softc *sc);
9332 static void bxe_gunzip_end(struct bxe_softc *sc);
9333 static int  bxe_init_firmware(struct bxe_softc *sc);
9334 static void bxe_release_firmware(struct bxe_softc *sc);
9335 
9336 static struct
9337 ecore_func_sp_drv_ops bxe_func_sp_drv = {
9338     .init_hw_cmn_chip = bxe_init_hw_common_chip,
9339     .init_hw_cmn      = bxe_init_hw_common,
9340     .init_hw_port     = bxe_init_hw_port,
9341     .init_hw_func     = bxe_init_hw_func,
9342 
9343     .reset_hw_cmn     = bxe_reset_common,
9344     .reset_hw_port    = bxe_reset_port,
9345     .reset_hw_func    = bxe_reset_func,
9346 
9347     .gunzip_init      = bxe_gunzip_init,
9348     .gunzip_end       = bxe_gunzip_end,
9349 
9350     .init_fw          = bxe_init_firmware,
9351     .release_fw       = bxe_release_firmware,
9352 };
9353 
9354 static void
9355 bxe_init_func_obj(struct bxe_softc *sc)
9356 {
9357     sc->dmae_ready = 0;
9358 
9359     ecore_init_func_obj(sc,
9360                         &sc->func_obj,
9361                         BXE_SP(sc, func_rdata),
9362                         BXE_SP_MAPPING(sc, func_rdata),
9363                         BXE_SP(sc, func_afex_rdata),
9364                         BXE_SP_MAPPING(sc, func_afex_rdata),
9365                         &bxe_func_sp_drv);
9366 }
9367 
9368 static int
9369 bxe_init_hw(struct bxe_softc *sc,
9370             uint32_t         load_code)
9371 {
9372     struct ecore_func_state_params func_params = { NULL };
9373     int rc;
9374 
9375     /* prepare the parameters for function state transitions */
9376     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9377 
9378     func_params.f_obj = &sc->func_obj;
9379     func_params.cmd = ECORE_F_CMD_HW_INIT;
9380 
9381     func_params.params.hw_init.load_phase = load_code;
9382 
9383     /*
9384      * Via a plethora of function pointers, we will eventually reach
9385      * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9386      */
9387     rc = ecore_func_state_change(sc, &func_params);
9388 
9389     return (rc);
9390 }
9391 
9392 static void
9393 bxe_fill(struct bxe_softc *sc,
9394          uint32_t         addr,
9395          int              fill,
9396          uint32_t         len)
9397 {
9398     uint32_t i;
9399 
9400     if (!(len % 4) && !(addr % 4)) {
9401         for (i = 0; i < len; i += 4) {
9402             REG_WR(sc, (addr + i), fill);
9403         }
9404     } else {
9405         for (i = 0; i < len; i++) {
9406             REG_WR8(sc, (addr + i), fill);
9407         }
9408     }
9409 }
9410 
9411 /* writes FP SP data to FW - data_size in dwords */
9412 static void
9413 bxe_wr_fp_sb_data(struct bxe_softc *sc,
9414                   int              fw_sb_id,
9415                   uint32_t         *sb_data_p,
9416                   uint32_t         data_size)
9417 {
9418     int index;
9419 
9420     for (index = 0; index < data_size; index++) {
9421         REG_WR(sc,
9422                (BAR_CSTRORM_INTMEM +
9423                 CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9424                 (sizeof(uint32_t) * index)),
9425                *(sb_data_p + index));
9426     }
9427 }
9428 
9429 static void
9430 bxe_zero_fp_sb(struct bxe_softc *sc,
9431                int              fw_sb_id)
9432 {
9433     struct hc_status_block_data_e2 sb_data_e2;
9434     struct hc_status_block_data_e1x sb_data_e1x;
9435     uint32_t *sb_data_p;
9436     uint32_t data_size = 0;
9437 
9438     if (!CHIP_IS_E1x(sc)) {
9439         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9440         sb_data_e2.common.state = SB_DISABLED;
9441         sb_data_e2.common.p_func.vf_valid = FALSE;
9442         sb_data_p = (uint32_t *)&sb_data_e2;
9443         data_size = (sizeof(struct hc_status_block_data_e2) /
9444                      sizeof(uint32_t));
9445     } else {
9446         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9447         sb_data_e1x.common.state = SB_DISABLED;
9448         sb_data_e1x.common.p_func.vf_valid = FALSE;
9449         sb_data_p = (uint32_t *)&sb_data_e1x;
9450         data_size = (sizeof(struct hc_status_block_data_e1x) /
9451                      sizeof(uint32_t));
9452     }
9453 
9454     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9455 
9456     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9457              0, CSTORM_STATUS_BLOCK_SIZE);
9458     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9459              0, CSTORM_SYNC_BLOCK_SIZE);
9460 }
9461 
9462 static void
9463 bxe_wr_sp_sb_data(struct bxe_softc               *sc,
9464                   struct hc_sp_status_block_data *sp_sb_data)
9465 {
9466     int i;
9467 
9468     for (i = 0;
9469          i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9470          i++) {
9471         REG_WR(sc,
9472                (BAR_CSTRORM_INTMEM +
9473                 CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9474                 (i * sizeof(uint32_t))),
9475                *((uint32_t *)sp_sb_data + i));
9476     }
9477 }
9478 
9479 static void
9480 bxe_zero_sp_sb(struct bxe_softc *sc)
9481 {
9482     struct hc_sp_status_block_data sp_sb_data;
9483 
9484     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9485 
9486     sp_sb_data.state           = SB_DISABLED;
9487     sp_sb_data.p_func.vf_valid = FALSE;
9488 
9489     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9490 
9491     bxe_fill(sc,
9492              (BAR_CSTRORM_INTMEM +
9493               CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9494               0, CSTORM_SP_STATUS_BLOCK_SIZE);
9495     bxe_fill(sc,
9496              (BAR_CSTRORM_INTMEM +
9497               CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9498               0, CSTORM_SP_SYNC_BLOCK_SIZE);
9499 }
9500 
9501 static void
9502 bxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9503                              int                       igu_sb_id,
9504                              int                       igu_seg_id)
9505 {
9506     hc_sm->igu_sb_id      = igu_sb_id;
9507     hc_sm->igu_seg_id     = igu_seg_id;
9508     hc_sm->timer_value    = 0xFF;
9509     hc_sm->time_to_expire = 0xFFFFFFFF;
9510 }
9511 
9512 static void
9513 bxe_map_sb_state_machines(struct hc_index_data *index_data)
9514 {
9515     /* zero out state machine indices */
9516 
9517     /* rx indices */
9518     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9519 
9520     /* tx indices */
9521     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9522     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9523     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9524     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9525 
9526     /* map indices */
9527 
9528     /* rx indices */
9529     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9530         (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9531 
9532     /* tx indices */
9533     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9534         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9535     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9536         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9537     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9538         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9539     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9540         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9541 }
9542 
9543 static void
9544 bxe_init_sb(struct bxe_softc *sc,
9545             bus_addr_t       busaddr,
9546             int              vfid,
9547             uint8_t          vf_valid,
9548             int              fw_sb_id,
9549             int              igu_sb_id)
9550 {
9551     struct hc_status_block_data_e2  sb_data_e2;
9552     struct hc_status_block_data_e1x sb_data_e1x;
9553     struct hc_status_block_sm       *hc_sm_p;
9554     uint32_t *sb_data_p;
9555     int igu_seg_id;
9556     int data_size;
9557 
9558     if (CHIP_INT_MODE_IS_BC(sc)) {
9559         igu_seg_id = HC_SEG_ACCESS_NORM;
9560     } else {
9561         igu_seg_id = IGU_SEG_ACCESS_NORM;
9562     }
9563 
9564     bxe_zero_fp_sb(sc, fw_sb_id);
9565 
9566     if (!CHIP_IS_E1x(sc)) {
9567         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9568         sb_data_e2.common.state = SB_ENABLED;
9569         sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9570         sb_data_e2.common.p_func.vf_id = vfid;
9571         sb_data_e2.common.p_func.vf_valid = vf_valid;
9572         sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9573         sb_data_e2.common.same_igu_sb_1b = TRUE;
9574         sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9575         sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9576         hc_sm_p = sb_data_e2.common.state_machine;
9577         sb_data_p = (uint32_t *)&sb_data_e2;
9578         data_size = (sizeof(struct hc_status_block_data_e2) /
9579                      sizeof(uint32_t));
9580         bxe_map_sb_state_machines(sb_data_e2.index_data);
9581     } else {
9582         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9583         sb_data_e1x.common.state = SB_ENABLED;
9584         sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9585         sb_data_e1x.common.p_func.vf_id = 0xff;
9586         sb_data_e1x.common.p_func.vf_valid = FALSE;
9587         sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9588         sb_data_e1x.common.same_igu_sb_1b = TRUE;
9589         sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9590         sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9591         hc_sm_p = sb_data_e1x.common.state_machine;
9592         sb_data_p = (uint32_t *)&sb_data_e1x;
9593         data_size = (sizeof(struct hc_status_block_data_e1x) /
9594                      sizeof(uint32_t));
9595         bxe_map_sb_state_machines(sb_data_e1x.index_data);
9596     }
9597 
9598     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9599     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9600 
9601     BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9602 
9603     /* write indices to HW - PCI guarantees endianity of regpairs */
9604     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9605 }
9606 
9607 static inline uint8_t
9608 bxe_fp_qzone_id(struct bxe_fastpath *fp)
9609 {
9610     if (CHIP_IS_E1x(fp->sc)) {
9611         return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9612     } else {
9613         return (fp->cl_id);
9614     }
9615 }
9616 
9617 static inline uint32_t
9618 bxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9619                            struct bxe_fastpath *fp)
9620 {
9621     uint32_t offset = BAR_USTRORM_INTMEM;
9622 
9623     if (!CHIP_IS_E1x(sc)) {
9624         offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9625     } else {
9626         offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9627     }
9628 
9629     return (offset);
9630 }
9631 
9632 static void
9633 bxe_init_eth_fp(struct bxe_softc *sc,
9634                 int              idx)
9635 {
9636     struct bxe_fastpath *fp = &sc->fp[idx];
9637     uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9638     unsigned long q_type = 0;
9639     int cos;
9640 
9641     fp->sc    = sc;
9642     fp->index = idx;
9643 
9644     fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9645     fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9646 
9647     fp->cl_id = (CHIP_IS_E1x(sc)) ?
9648                     (SC_L_ID(sc) + idx) :
9649                     /* want client ID same as IGU SB ID for non-E1 */
9650                     fp->igu_sb_id;
9651     fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9652 
9653     /* setup sb indices */
9654     if (!CHIP_IS_E1x(sc)) {
9655         fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9656         fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9657     } else {
9658         fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9659         fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9660     }
9661 
9662     /* init shortcut */
9663     fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9664 
9665     fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9666 
9667     /*
9668      * XXX If multiple CoS is ever supported then each fastpath structure
9669      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9670      */
9671     for (cos = 0; cos < sc->max_cos; cos++) {
9672         cids[cos] = idx;
9673     }
9674     fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9675 
9676     /* nothing more for a VF to do */
9677     if (IS_VF(sc)) {
9678         return;
9679     }
9680 
9681     bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9682                 fp->fw_sb_id, fp->igu_sb_id);
9683 
9684     bxe_update_fp_sb_idx(fp);
9685 
9686     /* Configure Queue State object */
9687     bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9688     bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9689 
9690     ecore_init_queue_obj(sc,
9691                          &sc->sp_objs[idx].q_obj,
9692                          fp->cl_id,
9693                          cids,
9694                          sc->max_cos,
9695                          SC_FUNC(sc),
9696                          BXE_SP(sc, q_rdata),
9697                          BXE_SP_MAPPING(sc, q_rdata),
9698                          q_type);
9699 
9700     /* configure classification DBs */
9701     ecore_init_mac_obj(sc,
9702                        &sc->sp_objs[idx].mac_obj,
9703                        fp->cl_id,
9704                        idx,
9705                        SC_FUNC(sc),
9706                        BXE_SP(sc, mac_rdata),
9707                        BXE_SP_MAPPING(sc, mac_rdata),
9708                        ECORE_FILTER_MAC_PENDING,
9709                        &sc->sp_state,
9710                        ECORE_OBJ_TYPE_RX_TX,
9711                        &sc->macs_pool);
9712 
9713     BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9714           idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9715 }
9716 
9717 static inline void
9718 bxe_update_rx_prod(struct bxe_softc    *sc,
9719                    struct bxe_fastpath *fp,
9720                    uint16_t            rx_bd_prod,
9721                    uint16_t            rx_cq_prod,
9722                    uint16_t            rx_sge_prod)
9723 {
9724     struct ustorm_eth_rx_producers rx_prods = { 0 };
9725     uint32_t i;
9726 
9727     /* update producers */
9728     rx_prods.bd_prod  = rx_bd_prod;
9729     rx_prods.cqe_prod = rx_cq_prod;
9730     rx_prods.sge_prod = rx_sge_prod;
9731 
9732     /*
9733      * Make sure that the BD and SGE data is updated before updating the
9734      * producers since FW might read the BD/SGE right after the producer
9735      * is updated.
9736      * This is only applicable for weak-ordered memory model archs such
9737      * as IA-64. The following barrier is also mandatory since FW will
9738      * assumes BDs must have buffers.
9739      */
9740     wmb();
9741 
9742     for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9743         REG_WR(sc,
9744                (fp->ustorm_rx_prods_offset + (i * 4)),
9745                ((uint32_t *)&rx_prods)[i]);
9746     }
9747 
9748     wmb(); /* keep prod updates ordered */
9749 
9750     BLOGD(sc, DBG_RX,
9751           "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9752           fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9753 }
9754 
9755 static void
9756 bxe_init_rx_rings(struct bxe_softc *sc)
9757 {
9758     struct bxe_fastpath *fp;
9759     int i;
9760 
9761     for (i = 0; i < sc->num_queues; i++) {
9762         fp = &sc->fp[i];
9763 
9764         fp->rx_bd_cons = 0;
9765 
9766         /*
9767          * Activate the BD ring...
9768          * Warning, this will generate an interrupt (to the TSTORM)
9769          * so this can only be done after the chip is initialized
9770          */
9771         bxe_update_rx_prod(sc, fp,
9772                            fp->rx_bd_prod,
9773                            fp->rx_cq_prod,
9774                            fp->rx_sge_prod);
9775 
9776         if (i != 0) {
9777             continue;
9778         }
9779 
9780         if (CHIP_IS_E1(sc)) {
9781             REG_WR(sc,
9782                    (BAR_USTRORM_INTMEM +
9783                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9784                    U64_LO(fp->rcq_dma.paddr));
9785             REG_WR(sc,
9786                    (BAR_USTRORM_INTMEM +
9787                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9788                    U64_HI(fp->rcq_dma.paddr));
9789         }
9790     }
9791 }
9792 
9793 static void
9794 bxe_init_tx_ring_one(struct bxe_fastpath *fp)
9795 {
9796     SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9797     fp->tx_db.data.zero_fill1 = 0;
9798     fp->tx_db.data.prod = 0;
9799 
9800     fp->tx_pkt_prod = 0;
9801     fp->tx_pkt_cons = 0;
9802     fp->tx_bd_prod = 0;
9803     fp->tx_bd_cons = 0;
9804     fp->eth_q_stats.tx_pkts = 0;
9805 }
9806 
9807 static inline void
9808 bxe_init_tx_rings(struct bxe_softc *sc)
9809 {
9810     int i;
9811 
9812     for (i = 0; i < sc->num_queues; i++) {
9813         bxe_init_tx_ring_one(&sc->fp[i]);
9814     }
9815 }
9816 
9817 static void
9818 bxe_init_def_sb(struct bxe_softc *sc)
9819 {
9820     struct host_sp_status_block *def_sb = sc->def_sb;
9821     bus_addr_t mapping = sc->def_sb_dma.paddr;
9822     int igu_sp_sb_index;
9823     int igu_seg_id;
9824     int port = SC_PORT(sc);
9825     int func = SC_FUNC(sc);
9826     int reg_offset, reg_offset_en5;
9827     uint64_t section;
9828     int index, sindex;
9829     struct hc_sp_status_block_data sp_sb_data;
9830 
9831     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9832 
9833     if (CHIP_INT_MODE_IS_BC(sc)) {
9834         igu_sp_sb_index = DEF_SB_IGU_ID;
9835         igu_seg_id = HC_SEG_ACCESS_DEF;
9836     } else {
9837         igu_sp_sb_index = sc->igu_dsb_id;
9838         igu_seg_id = IGU_SEG_ACCESS_DEF;
9839     }
9840 
9841     /* attentions */
9842     section = ((uint64_t)mapping +
9843                offsetof(struct host_sp_status_block, atten_status_block));
9844     def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9845     sc->attn_state = 0;
9846 
9847     reg_offset = (port) ?
9848                      MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9849                      MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9850     reg_offset_en5 = (port) ?
9851                          MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9852                          MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9853 
9854     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9855         /* take care of sig[0]..sig[4] */
9856         for (sindex = 0; sindex < 4; sindex++) {
9857             sc->attn_group[index].sig[sindex] =
9858                 REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9859         }
9860 
9861         if (!CHIP_IS_E1x(sc)) {
9862             /*
9863              * enable5 is separate from the rest of the registers,
9864              * and the address skip is 4 and not 16 between the
9865              * different groups
9866              */
9867             sc->attn_group[index].sig[4] =
9868                 REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9869         } else {
9870             sc->attn_group[index].sig[4] = 0;
9871         }
9872     }
9873 
9874     if (sc->devinfo.int_block == INT_BLOCK_HC) {
9875         reg_offset = (port) ?
9876                          HC_REG_ATTN_MSG1_ADDR_L :
9877                          HC_REG_ATTN_MSG0_ADDR_L;
9878         REG_WR(sc, reg_offset, U64_LO(section));
9879         REG_WR(sc, (reg_offset + 4), U64_HI(section));
9880     } else if (!CHIP_IS_E1x(sc)) {
9881         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9882         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9883     }
9884 
9885     section = ((uint64_t)mapping +
9886                offsetof(struct host_sp_status_block, sp_sb));
9887 
9888     bxe_zero_sp_sb(sc);
9889 
9890     /* PCI guarantees endianity of regpair */
9891     sp_sb_data.state           = SB_ENABLED;
9892     sp_sb_data.host_sb_addr.lo = U64_LO(section);
9893     sp_sb_data.host_sb_addr.hi = U64_HI(section);
9894     sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9895     sp_sb_data.igu_seg_id      = igu_seg_id;
9896     sp_sb_data.p_func.pf_id    = func;
9897     sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9898     sp_sb_data.p_func.vf_id    = 0xff;
9899 
9900     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9901 
9902     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9903 }
9904 
9905 static void
9906 bxe_init_sp_ring(struct bxe_softc *sc)
9907 {
9908     atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9909     sc->spq_prod_idx = 0;
9910     sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9911     sc->spq_prod_bd = sc->spq;
9912     sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9913 }
9914 
9915 static void
9916 bxe_init_eq_ring(struct bxe_softc *sc)
9917 {
9918     union event_ring_elem *elem;
9919     int i;
9920 
9921     for (i = 1; i <= NUM_EQ_PAGES; i++) {
9922         elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9923 
9924         elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9925                                                  BCM_PAGE_SIZE *
9926                                                  (i % NUM_EQ_PAGES)));
9927         elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9928                                                  BCM_PAGE_SIZE *
9929                                                  (i % NUM_EQ_PAGES)));
9930     }
9931 
9932     sc->eq_cons    = 0;
9933     sc->eq_prod    = NUM_EQ_DESC;
9934     sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9935 
9936     atomic_store_rel_long(&sc->eq_spq_left,
9937                           (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9938                                NUM_EQ_DESC) - 1));
9939 }
9940 
9941 static void
9942 bxe_init_internal_common(struct bxe_softc *sc)
9943 {
9944     int i;
9945 
9946     /*
9947      * Zero this manually as its initialization is currently missing
9948      * in the initTool.
9949      */
9950     for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9951         REG_WR(sc,
9952                (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9953                0);
9954     }
9955 
9956     if (!CHIP_IS_E1x(sc)) {
9957         REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9958                 CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9959     }
9960 }
9961 
9962 static void
9963 bxe_init_internal(struct bxe_softc *sc,
9964                   uint32_t         load_code)
9965 {
9966     switch (load_code) {
9967     case FW_MSG_CODE_DRV_LOAD_COMMON:
9968     case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9969         bxe_init_internal_common(sc);
9970         /* no break */
9971 
9972     case FW_MSG_CODE_DRV_LOAD_PORT:
9973         /* nothing to do */
9974         /* no break */
9975 
9976     case FW_MSG_CODE_DRV_LOAD_FUNCTION:
9977         /* internal memory per function is initialized inside bxe_pf_init */
9978         break;
9979 
9980     default:
9981         BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
9982         break;
9983     }
9984 }
9985 
9986 static void
9987 storm_memset_func_cfg(struct bxe_softc                         *sc,
9988                       struct tstorm_eth_function_common_config *tcfg,
9989                       uint16_t                                  abs_fid)
9990 {
9991     uint32_t addr;
9992     size_t size;
9993 
9994     addr = (BAR_TSTRORM_INTMEM +
9995             TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
9996     size = sizeof(struct tstorm_eth_function_common_config);
9997     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
9998 }
9999 
10000 static void
10001 bxe_func_init(struct bxe_softc            *sc,
10002               struct bxe_func_init_params *p)
10003 {
10004     struct tstorm_eth_function_common_config tcfg = { 0 };
10005 
10006     if (CHIP_IS_E1x(sc)) {
10007         storm_memset_func_cfg(sc, &tcfg, p->func_id);
10008     }
10009 
10010     /* Enable the function in the FW */
10011     storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
10012     storm_memset_func_en(sc, p->func_id, 1);
10013 
10014     /* spq */
10015     if (p->func_flgs & FUNC_FLG_SPQ) {
10016         storm_memset_spq_addr(sc, p->spq_map, p->func_id);
10017         REG_WR(sc,
10018                (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
10019                p->spq_prod);
10020     }
10021 }
10022 
10023 /*
10024  * Calculates the sum of vn_min_rates.
10025  * It's needed for further normalizing of the min_rates.
10026  * Returns:
10027  *   sum of vn_min_rates.
10028  *     or
10029  *   0 - if all the min_rates are 0.
10030  * In the later case fainess algorithm should be deactivated.
10031  * If all min rates are not zero then those that are zeroes will be set to 1.
10032  */
10033 static void
10034 bxe_calc_vn_min(struct bxe_softc       *sc,
10035                 struct cmng_init_input *input)
10036 {
10037     uint32_t vn_cfg;
10038     uint32_t vn_min_rate;
10039     int all_zero = 1;
10040     int vn;
10041 
10042     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10043         vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10044         vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
10045                         FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
10046 
10047         if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10048             /* skip hidden VNs */
10049             vn_min_rate = 0;
10050         } else if (!vn_min_rate) {
10051             /* If min rate is zero - set it to 100 */
10052             vn_min_rate = DEF_MIN_RATE;
10053         } else {
10054             all_zero = 0;
10055         }
10056 
10057         input->vnic_min_rate[vn] = vn_min_rate;
10058     }
10059 
10060     /* if ETS or all min rates are zeros - disable fairness */
10061     if (BXE_IS_ETS_ENABLED(sc)) {
10062         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10063         BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10064     } else if (all_zero) {
10065         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10066         BLOGD(sc, DBG_LOAD,
10067               "Fariness disabled (all MIN values are zeroes)\n");
10068     } else {
10069         input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10070     }
10071 }
10072 
10073 static inline uint16_t
10074 bxe_extract_max_cfg(struct bxe_softc *sc,
10075                     uint32_t         mf_cfg)
10076 {
10077     uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10078                         FUNC_MF_CFG_MAX_BW_SHIFT);
10079 
10080     if (!max_cfg) {
10081         BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10082         max_cfg = 100;
10083     }
10084 
10085     return (max_cfg);
10086 }
10087 
10088 static void
10089 bxe_calc_vn_max(struct bxe_softc       *sc,
10090                 int                    vn,
10091                 struct cmng_init_input *input)
10092 {
10093     uint16_t vn_max_rate;
10094     uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10095     uint32_t max_cfg;
10096 
10097     if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10098         vn_max_rate = 0;
10099     } else {
10100         max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10101 
10102         if (IS_MF_SI(sc)) {
10103             /* max_cfg in percents of linkspeed */
10104             vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10105         } else { /* SD modes */
10106             /* max_cfg is absolute in 100Mb units */
10107             vn_max_rate = (max_cfg * 100);
10108         }
10109     }
10110 
10111     BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10112 
10113     input->vnic_max_rate[vn] = vn_max_rate;
10114 }
10115 
10116 static void
10117 bxe_cmng_fns_init(struct bxe_softc *sc,
10118                   uint8_t          read_cfg,
10119                   uint8_t          cmng_type)
10120 {
10121     struct cmng_init_input input;
10122     int vn;
10123 
10124     memset(&input, 0, sizeof(struct cmng_init_input));
10125 
10126     input.port_rate = sc->link_vars.line_speed;
10127 
10128     if (cmng_type == CMNG_FNS_MINMAX) {
10129         /* read mf conf from shmem */
10130         if (read_cfg) {
10131             bxe_read_mf_cfg(sc);
10132         }
10133 
10134         /* get VN min rate and enable fairness if not 0 */
10135         bxe_calc_vn_min(sc, &input);
10136 
10137         /* get VN max rate */
10138         if (sc->port.pmf) {
10139             for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10140                 bxe_calc_vn_max(sc, vn, &input);
10141             }
10142         }
10143 
10144         /* always enable rate shaping and fairness */
10145         input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10146 
10147         ecore_init_cmng(&input, &sc->cmng);
10148         return;
10149     }
10150 
10151     /* rate shaping and fairness are disabled */
10152     BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10153 }
10154 
10155 static int
10156 bxe_get_cmng_fns_mode(struct bxe_softc *sc)
10157 {
10158     if (CHIP_REV_IS_SLOW(sc)) {
10159         return (CMNG_FNS_NONE);
10160     }
10161 
10162     if (IS_MF(sc)) {
10163         return (CMNG_FNS_MINMAX);
10164     }
10165 
10166     return (CMNG_FNS_NONE);
10167 }
10168 
10169 static void
10170 storm_memset_cmng(struct bxe_softc *sc,
10171                   struct cmng_init *cmng,
10172                   uint8_t          port)
10173 {
10174     int vn;
10175     int func;
10176     uint32_t addr;
10177     size_t size;
10178 
10179     addr = (BAR_XSTRORM_INTMEM +
10180             XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10181     size = sizeof(struct cmng_struct_per_port);
10182     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10183 
10184     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10185         func = func_by_vn(sc, vn);
10186 
10187         addr = (BAR_XSTRORM_INTMEM +
10188                 XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10189         size = sizeof(struct rate_shaping_vars_per_vn);
10190         ecore_storm_memset_struct(sc, addr, size,
10191                                   (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10192 
10193         addr = (BAR_XSTRORM_INTMEM +
10194                 XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10195         size = sizeof(struct fairness_vars_per_vn);
10196         ecore_storm_memset_struct(sc, addr, size,
10197                                   (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10198     }
10199 }
10200 
10201 static void
10202 bxe_pf_init(struct bxe_softc *sc)
10203 {
10204     struct bxe_func_init_params func_init = { 0 };
10205     struct event_ring_data eq_data = { { 0 } };
10206     uint16_t flags;
10207 
10208     if (!CHIP_IS_E1x(sc)) {
10209         /* reset IGU PF statistics: MSIX + ATTN */
10210         /* PF */
10211         REG_WR(sc,
10212                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10213                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10214                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10215                0);
10216         /* ATTN */
10217         REG_WR(sc,
10218                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10219                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10220                 (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10221                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10222                0);
10223     }
10224 
10225     /* function setup flags */
10226     flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10227 
10228     /*
10229      * This flag is relevant for E1x only.
10230      * E2 doesn't have a TPA configuration in a function level.
10231      */
10232     flags |= (if_getcapenable(sc->ifp) & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10233 
10234     func_init.func_flgs = flags;
10235     func_init.pf_id     = SC_FUNC(sc);
10236     func_init.func_id   = SC_FUNC(sc);
10237     func_init.spq_map   = sc->spq_dma.paddr;
10238     func_init.spq_prod  = sc->spq_prod_idx;
10239 
10240     bxe_func_init(sc, &func_init);
10241 
10242     memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10243 
10244     /*
10245      * Congestion management values depend on the link rate.
10246      * There is no active link so initial link rate is set to 10Gbps.
10247      * When the link comes up the congestion management values are
10248      * re-calculated according to the actual link rate.
10249      */
10250     sc->link_vars.line_speed = SPEED_10000;
10251     bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10252 
10253     /* Only the PMF sets the HW */
10254     if (sc->port.pmf) {
10255         storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10256     }
10257 
10258     /* init Event Queue - PCI bus guarantees correct endainity */
10259     eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10260     eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10261     eq_data.producer     = sc->eq_prod;
10262     eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10263     eq_data.sb_id        = DEF_SB_ID;
10264     storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10265 }
10266 
10267 static void
10268 bxe_hc_int_enable(struct bxe_softc *sc)
10269 {
10270     int port = SC_PORT(sc);
10271     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10272     uint32_t val = REG_RD(sc, addr);
10273     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10274     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10275                            (sc->intr_count == 1)) ? TRUE : FALSE;
10276     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10277 
10278     if (msix) {
10279         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10280                  HC_CONFIG_0_REG_INT_LINE_EN_0);
10281         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10282                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10283         if (single_msix) {
10284             val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10285         }
10286     } else if (msi) {
10287         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
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_ATTN_BIT_EN_0);
10291     } else {
10292         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10293                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10294                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10295                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10296 
10297         if (!CHIP_IS_E1(sc)) {
10298             BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10299                   val, port, addr);
10300 
10301             REG_WR(sc, addr, val);
10302 
10303             val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10304         }
10305     }
10306 
10307     if (CHIP_IS_E1(sc)) {
10308         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10309     }
10310 
10311     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10312           val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10313 
10314     REG_WR(sc, addr, val);
10315 
10316     /* ensure that HC_CONFIG is written before leading/trailing edge config */
10317     mb();
10318 
10319     if (!CHIP_IS_E1(sc)) {
10320         /* init leading/trailing edge */
10321         if (IS_MF(sc)) {
10322             val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10323             if (sc->port.pmf) {
10324                 /* enable nig and gpio3 attention */
10325                 val |= 0x1100;
10326             }
10327         } else {
10328             val = 0xffff;
10329         }
10330 
10331         REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10332         REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10333     }
10334 
10335     /* make sure that interrupts are indeed enabled from here on */
10336     mb();
10337 }
10338 
10339 static void
10340 bxe_igu_int_enable(struct bxe_softc *sc)
10341 {
10342     uint32_t val;
10343     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10344     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10345                            (sc->intr_count == 1)) ? TRUE : FALSE;
10346     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10347 
10348     val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10349 
10350     if (msix) {
10351         val &= ~(IGU_PF_CONF_INT_LINE_EN |
10352                  IGU_PF_CONF_SINGLE_ISR_EN);
10353         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10354                 IGU_PF_CONF_ATTN_BIT_EN);
10355         if (single_msix) {
10356             val |= IGU_PF_CONF_SINGLE_ISR_EN;
10357         }
10358     } else if (msi) {
10359         val &= ~IGU_PF_CONF_INT_LINE_EN;
10360         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10361                 IGU_PF_CONF_ATTN_BIT_EN |
10362                 IGU_PF_CONF_SINGLE_ISR_EN);
10363     } else {
10364         val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10365         val |= (IGU_PF_CONF_INT_LINE_EN |
10366                 IGU_PF_CONF_ATTN_BIT_EN |
10367                 IGU_PF_CONF_SINGLE_ISR_EN);
10368     }
10369 
10370     /* clean previous status - need to configure igu prior to ack*/
10371     if ((!msix) || single_msix) {
10372         REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10373         bxe_ack_int(sc);
10374     }
10375 
10376     val |= IGU_PF_CONF_FUNC_EN;
10377 
10378     BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10379           val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10380 
10381     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10382 
10383     mb();
10384 
10385     /* init leading/trailing edge */
10386     if (IS_MF(sc)) {
10387         val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10388         if (sc->port.pmf) {
10389             /* enable nig and gpio3 attention */
10390             val |= 0x1100;
10391         }
10392     } else {
10393         val = 0xffff;
10394     }
10395 
10396     REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10397     REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10398 
10399     /* make sure that interrupts are indeed enabled from here on */
10400     mb();
10401 }
10402 
10403 static void
10404 bxe_int_enable(struct bxe_softc *sc)
10405 {
10406     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10407         bxe_hc_int_enable(sc);
10408     } else {
10409         bxe_igu_int_enable(sc);
10410     }
10411 }
10412 
10413 static void
10414 bxe_hc_int_disable(struct bxe_softc *sc)
10415 {
10416     int port = SC_PORT(sc);
10417     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10418     uint32_t val = REG_RD(sc, addr);
10419 
10420     /*
10421      * In E1 we must use only PCI configuration space to disable MSI/MSIX
10422      * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10423      * block
10424      */
10425     if (CHIP_IS_E1(sc)) {
10426         /*
10427          * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10428          * to prevent from HC sending interrupts after we exit the function
10429          */
10430         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10431 
10432         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10433                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10434                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10435     } else {
10436         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10437                  HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10438                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10439                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10440     }
10441 
10442     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10443 
10444     /* flush all outstanding writes */
10445     mb();
10446 
10447     REG_WR(sc, addr, val);
10448     if (REG_RD(sc, addr) != val) {
10449         BLOGE(sc, "proper val not read from HC IGU!\n");
10450     }
10451 }
10452 
10453 static void
10454 bxe_igu_int_disable(struct bxe_softc *sc)
10455 {
10456     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10457 
10458     val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10459              IGU_PF_CONF_INT_LINE_EN |
10460              IGU_PF_CONF_ATTN_BIT_EN);
10461 
10462     BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10463 
10464     /* flush all outstanding writes */
10465     mb();
10466 
10467     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10468     if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10469         BLOGE(sc, "proper val not read from IGU!\n");
10470     }
10471 }
10472 
10473 static void
10474 bxe_int_disable(struct bxe_softc *sc)
10475 {
10476     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10477         bxe_hc_int_disable(sc);
10478     } else {
10479         bxe_igu_int_disable(sc);
10480     }
10481 }
10482 
10483 static void
10484 bxe_nic_init(struct bxe_softc *sc,
10485              int              load_code)
10486 {
10487     int i;
10488 
10489     for (i = 0; i < sc->num_queues; i++) {
10490         bxe_init_eth_fp(sc, i);
10491     }
10492 
10493     rmb(); /* ensure status block indices were read */
10494 
10495     bxe_init_rx_rings(sc);
10496     bxe_init_tx_rings(sc);
10497 
10498     if (IS_VF(sc)) {
10499         return;
10500     }
10501 
10502     /* initialize MOD_ABS interrupts */
10503     elink_init_mod_abs_int(sc, &sc->link_vars,
10504                            sc->devinfo.chip_id,
10505                            sc->devinfo.shmem_base,
10506                            sc->devinfo.shmem2_base,
10507                            SC_PORT(sc));
10508 
10509     bxe_init_def_sb(sc);
10510     bxe_update_dsb_idx(sc);
10511     bxe_init_sp_ring(sc);
10512     bxe_init_eq_ring(sc);
10513     bxe_init_internal(sc, load_code);
10514     bxe_pf_init(sc);
10515     bxe_stats_init(sc);
10516 
10517     /* flush all before enabling interrupts */
10518     mb();
10519 
10520     bxe_int_enable(sc);
10521 
10522     /* check for SPIO5 */
10523     bxe_attn_int_deasserted0(sc,
10524                              REG_RD(sc,
10525                                     (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10526                                      SC_PORT(sc)*4)) &
10527                              AEU_INPUTS_ATTN_BITS_SPIO5);
10528 }
10529 
10530 static inline void
10531 bxe_init_objs(struct bxe_softc *sc)
10532 {
10533     /* mcast rules must be added to tx if tx switching is enabled */
10534     ecore_obj_type o_type =
10535         (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10536                                          ECORE_OBJ_TYPE_RX;
10537 
10538     /* RX_MODE controlling object */
10539     ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10540 
10541     /* multicast configuration controlling object */
10542     ecore_init_mcast_obj(sc,
10543                          &sc->mcast_obj,
10544                          sc->fp[0].cl_id,
10545                          sc->fp[0].index,
10546                          SC_FUNC(sc),
10547                          SC_FUNC(sc),
10548                          BXE_SP(sc, mcast_rdata),
10549                          BXE_SP_MAPPING(sc, mcast_rdata),
10550                          ECORE_FILTER_MCAST_PENDING,
10551                          &sc->sp_state,
10552                          o_type);
10553 
10554     /* Setup CAM credit pools */
10555     ecore_init_mac_credit_pool(sc,
10556                                &sc->macs_pool,
10557                                SC_FUNC(sc),
10558                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10559                                                  VNICS_PER_PATH(sc));
10560 
10561     ecore_init_vlan_credit_pool(sc,
10562                                 &sc->vlans_pool,
10563                                 SC_ABS_FUNC(sc) >> 1,
10564                                 CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10565                                                   VNICS_PER_PATH(sc));
10566 
10567     /* RSS configuration object */
10568     ecore_init_rss_config_obj(sc,
10569                               &sc->rss_conf_obj,
10570                               sc->fp[0].cl_id,
10571                               sc->fp[0].index,
10572                               SC_FUNC(sc),
10573                               SC_FUNC(sc),
10574                               BXE_SP(sc, rss_rdata),
10575                               BXE_SP_MAPPING(sc, rss_rdata),
10576                               ECORE_FILTER_RSS_CONF_PENDING,
10577                               &sc->sp_state, ECORE_OBJ_TYPE_RX);
10578 }
10579 
10580 /*
10581  * Initialize the function. This must be called before sending CLIENT_SETUP
10582  * for the first client.
10583  */
10584 static inline int
10585 bxe_func_start(struct bxe_softc *sc)
10586 {
10587     struct ecore_func_state_params func_params = { NULL };
10588     struct ecore_func_start_params *start_params = &func_params.params.start;
10589 
10590     /* Prepare parameters for function state transitions */
10591     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10592 
10593     func_params.f_obj = &sc->func_obj;
10594     func_params.cmd = ECORE_F_CMD_START;
10595 
10596     /* Function parameters */
10597     start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10598     start_params->sd_vlan_tag = OVLAN(sc);
10599 
10600     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10601         start_params->network_cos_mode = STATIC_COS;
10602     } else { /* CHIP_IS_E1X */
10603         start_params->network_cos_mode = FW_WRR;
10604     }
10605 
10606     //start_params->gre_tunnel_mode = 0;
10607     //start_params->gre_tunnel_rss  = 0;
10608 
10609     return (ecore_func_state_change(sc, &func_params));
10610 }
10611 
10612 static int
10613 bxe_set_power_state(struct bxe_softc *sc,
10614                     uint8_t          state)
10615 {
10616     uint16_t pmcsr;
10617 
10618     /* If there is no power capability, silently succeed */
10619     if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10620         BLOGW(sc, "No power capability\n");
10621         return (0);
10622     }
10623 
10624     pmcsr = pci_read_config(sc->dev,
10625                             (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10626                             2);
10627 
10628     switch (state) {
10629     case PCI_PM_D0:
10630         pci_write_config(sc->dev,
10631                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10632                          ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10633 
10634         if (pmcsr & PCIM_PSTAT_DMASK) {
10635             /* delay required during transition out of D3hot */
10636             DELAY(20000);
10637         }
10638 
10639         break;
10640 
10641     case PCI_PM_D3hot:
10642         /* XXX if there are other clients above don't shut down the power */
10643 
10644         /* don't shut down the power for emulation and FPGA */
10645         if (CHIP_REV_IS_SLOW(sc)) {
10646             return (0);
10647         }
10648 
10649         pmcsr &= ~PCIM_PSTAT_DMASK;
10650         pmcsr |= PCIM_PSTAT_D3;
10651 
10652         if (sc->wol) {
10653             pmcsr |= PCIM_PSTAT_PMEENABLE;
10654         }
10655 
10656         pci_write_config(sc->dev,
10657                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10658                          pmcsr, 4);
10659 
10660         /*
10661          * No more memory access after this point until device is brought back
10662          * to D0 state.
10663          */
10664         break;
10665 
10666     default:
10667         BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10668             state, pmcsr);
10669         return (-1);
10670     }
10671 
10672     return (0);
10673 }
10674 
10675 
10676 /* return true if succeeded to acquire the lock */
10677 static uint8_t
10678 bxe_trylock_hw_lock(struct bxe_softc *sc,
10679                     uint32_t         resource)
10680 {
10681     uint32_t lock_status;
10682     uint32_t resource_bit = (1 << resource);
10683     int func = SC_FUNC(sc);
10684     uint32_t hw_lock_control_reg;
10685 
10686     BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10687 
10688     /* Validating that the resource is within range */
10689     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10690         BLOGD(sc, DBG_LOAD,
10691               "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10692               resource, HW_LOCK_MAX_RESOURCE_VALUE);
10693         return (FALSE);
10694     }
10695 
10696     if (func <= 5) {
10697         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10698     } else {
10699         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10700     }
10701 
10702     /* try to acquire the lock */
10703     REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10704     lock_status = REG_RD(sc, hw_lock_control_reg);
10705     if (lock_status & resource_bit) {
10706         return (TRUE);
10707     }
10708 
10709     BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10710         "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10711         lock_status, resource_bit);
10712 
10713     return (FALSE);
10714 }
10715 
10716 /*
10717  * Get the recovery leader resource id according to the engine this function
10718  * belongs to. Currently only only 2 engines is supported.
10719  */
10720 static int
10721 bxe_get_leader_lock_resource(struct bxe_softc *sc)
10722 {
10723     if (SC_PATH(sc)) {
10724         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10725     } else {
10726         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10727     }
10728 }
10729 
10730 /* try to acquire a leader lock for current engine */
10731 static uint8_t
10732 bxe_trylock_leader_lock(struct bxe_softc *sc)
10733 {
10734     return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10735 }
10736 
10737 static int
10738 bxe_release_leader_lock(struct bxe_softc *sc)
10739 {
10740     return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10741 }
10742 
10743 /* close gates #2, #3 and #4 */
10744 static void
10745 bxe_set_234_gates(struct bxe_softc *sc,
10746                   uint8_t          close)
10747 {
10748     uint32_t val;
10749 
10750     /* gates #2 and #4a are closed/opened for "not E1" only */
10751     if (!CHIP_IS_E1(sc)) {
10752         /* #4 */
10753         REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10754         /* #2 */
10755         REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10756     }
10757 
10758     /* #3 */
10759     if (CHIP_IS_E1x(sc)) {
10760         /* prevent interrupts from HC on both ports */
10761         val = REG_RD(sc, HC_REG_CONFIG_1);
10762         REG_WR(sc, HC_REG_CONFIG_1,
10763                (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10764                (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10765 
10766         val = REG_RD(sc, HC_REG_CONFIG_0);
10767         REG_WR(sc, HC_REG_CONFIG_0,
10768                (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10769                (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10770     } else {
10771         /* Prevent incomming interrupts in IGU */
10772         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10773 
10774         REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10775                (!close) ?
10776                (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10777                (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10778     }
10779 
10780     BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10781           close ? "closing" : "opening");
10782 
10783     wmb();
10784 }
10785 
10786 /* poll for pending writes bit, it should get cleared in no more than 1s */
10787 static int
10788 bxe_er_poll_igu_vq(struct bxe_softc *sc)
10789 {
10790     uint32_t cnt = 1000;
10791     uint32_t pend_bits = 0;
10792 
10793     do {
10794         pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10795 
10796         if (pend_bits == 0) {
10797             break;
10798         }
10799 
10800         DELAY(1000);
10801     } while (--cnt > 0);
10802 
10803     if (cnt == 0) {
10804         BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10805         return (-1);
10806     }
10807 
10808     return (0);
10809 }
10810 
10811 #define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10812 
10813 static void
10814 bxe_clp_reset_prep(struct bxe_softc *sc,
10815                    uint32_t         *magic_val)
10816 {
10817     /* Do some magic... */
10818     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10819     *magic_val = val & SHARED_MF_CLP_MAGIC;
10820     MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10821 }
10822 
10823 /* restore the value of the 'magic' bit */
10824 static void
10825 bxe_clp_reset_done(struct bxe_softc *sc,
10826                    uint32_t         magic_val)
10827 {
10828     /* Restore the 'magic' bit value... */
10829     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10830     MFCFG_WR(sc, shared_mf_config.clp_mb,
10831               (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10832 }
10833 
10834 /* prepare for MCP reset, takes care of CLP configurations */
10835 static void
10836 bxe_reset_mcp_prep(struct bxe_softc *sc,
10837                    uint32_t         *magic_val)
10838 {
10839     uint32_t shmem;
10840     uint32_t validity_offset;
10841 
10842     /* set `magic' bit in order to save MF config */
10843     if (!CHIP_IS_E1(sc)) {
10844         bxe_clp_reset_prep(sc, magic_val);
10845     }
10846 
10847     /* get shmem offset */
10848     shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10849     validity_offset =
10850         offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10851 
10852     /* Clear validity map flags */
10853     if (shmem > 0) {
10854         REG_WR(sc, shmem + validity_offset, 0);
10855     }
10856 }
10857 
10858 #define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10859 #define MCP_ONE_TIMEOUT  100    /* 100 ms */
10860 
10861 static void
10862 bxe_mcp_wait_one(struct bxe_softc *sc)
10863 {
10864     /* special handling for emulation and FPGA (10 times longer) */
10865     if (CHIP_REV_IS_SLOW(sc)) {
10866         DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10867     } else {
10868         DELAY((MCP_ONE_TIMEOUT) * 1000);
10869     }
10870 }
10871 
10872 /* initialize shmem_base and waits for validity signature to appear */
10873 static int
10874 bxe_init_shmem(struct bxe_softc *sc)
10875 {
10876     int cnt = 0;
10877     uint32_t val = 0;
10878 
10879     do {
10880         sc->devinfo.shmem_base     =
10881         sc->link_params.shmem_base =
10882             REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10883 
10884         if (sc->devinfo.shmem_base) {
10885             val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10886             if (val & SHR_MEM_VALIDITY_MB)
10887                 return (0);
10888         }
10889 
10890         bxe_mcp_wait_one(sc);
10891 
10892     } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10893 
10894     BLOGE(sc, "BAD MCP validity signature\n");
10895 
10896     return (-1);
10897 }
10898 
10899 static int
10900 bxe_reset_mcp_comp(struct bxe_softc *sc,
10901                    uint32_t         magic_val)
10902 {
10903     int rc = bxe_init_shmem(sc);
10904 
10905     /* Restore the `magic' bit value */
10906     if (!CHIP_IS_E1(sc)) {
10907         bxe_clp_reset_done(sc, magic_val);
10908     }
10909 
10910     return (rc);
10911 }
10912 
10913 static void
10914 bxe_pxp_prep(struct bxe_softc *sc)
10915 {
10916     if (!CHIP_IS_E1(sc)) {
10917         REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10918         REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10919         wmb();
10920     }
10921 }
10922 
10923 /*
10924  * Reset the whole chip except for:
10925  *      - PCIE core
10926  *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10927  *      - IGU
10928  *      - MISC (including AEU)
10929  *      - GRC
10930  *      - RBCN, RBCP
10931  */
10932 static void
10933 bxe_process_kill_chip_reset(struct bxe_softc *sc,
10934                             uint8_t          global)
10935 {
10936     uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10937     uint32_t global_bits2, stay_reset2;
10938 
10939     /*
10940      * Bits that have to be set in reset_mask2 if we want to reset 'global'
10941      * (per chip) blocks.
10942      */
10943     global_bits2 =
10944         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10945         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10946 
10947     /*
10948      * Don't reset the following blocks.
10949      * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10950      *            reset, as in 4 port device they might still be owned
10951      *            by the MCP (there is only one leader per path).
10952      */
10953     not_reset_mask1 =
10954         MISC_REGISTERS_RESET_REG_1_RST_HC |
10955         MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10956         MISC_REGISTERS_RESET_REG_1_RST_PXP;
10957 
10958     not_reset_mask2 =
10959         MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10960         MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10961         MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10962         MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10963         MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10964         MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10965         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10966         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10967         MISC_REGISTERS_RESET_REG_2_RST_ATC |
10968         MISC_REGISTERS_RESET_REG_2_PGLC |
10969         MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10970         MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10971         MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10972         MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10973         MISC_REGISTERS_RESET_REG_2_UMAC0 |
10974         MISC_REGISTERS_RESET_REG_2_UMAC1;
10975 
10976     /*
10977      * Keep the following blocks in reset:
10978      *  - all xxMACs are handled by the elink code.
10979      */
10980     stay_reset2 =
10981         MISC_REGISTERS_RESET_REG_2_XMAC |
10982         MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
10983 
10984     /* Full reset masks according to the chip */
10985     reset_mask1 = 0xffffffff;
10986 
10987     if (CHIP_IS_E1(sc))
10988         reset_mask2 = 0xffff;
10989     else if (CHIP_IS_E1H(sc))
10990         reset_mask2 = 0x1ffff;
10991     else if (CHIP_IS_E2(sc))
10992         reset_mask2 = 0xfffff;
10993     else /* CHIP_IS_E3 */
10994         reset_mask2 = 0x3ffffff;
10995 
10996     /* Don't reset global blocks unless we need to */
10997     if (!global)
10998         reset_mask2 &= ~global_bits2;
10999 
11000     /*
11001      * In case of attention in the QM, we need to reset PXP
11002      * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
11003      * because otherwise QM reset would release 'close the gates' shortly
11004      * before resetting the PXP, then the PSWRQ would send a write
11005      * request to PGLUE. Then when PXP is reset, PGLUE would try to
11006      * read the payload data from PSWWR, but PSWWR would not
11007      * respond. The write queue in PGLUE would stuck, dmae commands
11008      * would not return. Therefore it's important to reset the second
11009      * reset register (containing the
11010      * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
11011      * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
11012      * bit).
11013      */
11014     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
11015            reset_mask2 & (~not_reset_mask2));
11016 
11017     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
11018            reset_mask1 & (~not_reset_mask1));
11019 
11020     mb();
11021     wmb();
11022 
11023     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
11024            reset_mask2 & (~stay_reset2));
11025 
11026     mb();
11027     wmb();
11028 
11029     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
11030     wmb();
11031 }
11032 
11033 static int
11034 bxe_process_kill(struct bxe_softc *sc,
11035                  uint8_t          global)
11036 {
11037     int cnt = 1000;
11038     uint32_t val = 0;
11039     uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
11040     uint32_t tags_63_32 = 0;
11041 
11042     /* Empty the Tetris buffer, wait for 1s */
11043     do {
11044         sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
11045         blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
11046         port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
11047         port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
11048         pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
11049         if (CHIP_IS_E3(sc)) {
11050             tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
11051         }
11052 
11053         if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
11054             ((port_is_idle_0 & 0x1) == 0x1) &&
11055             ((port_is_idle_1 & 0x1) == 0x1) &&
11056             (pgl_exp_rom2 == 0xffffffff) &&
11057             (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11058             break;
11059         DELAY(1000);
11060     } while (cnt-- > 0);
11061 
11062     if (cnt <= 0) {
11063         BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11064                   "are still outstanding read requests after 1s! "
11065                   "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11066                   "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11067               sr_cnt, blk_cnt, port_is_idle_0,
11068               port_is_idle_1, pgl_exp_rom2);
11069         return (-1);
11070     }
11071 
11072     mb();
11073 
11074     /* Close gates #2, #3 and #4 */
11075     bxe_set_234_gates(sc, TRUE);
11076 
11077     /* Poll for IGU VQs for 57712 and newer chips */
11078     if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11079         return (-1);
11080     }
11081 
11082     /* XXX indicate that "process kill" is in progress to MCP */
11083 
11084     /* clear "unprepared" bit */
11085     REG_WR(sc, MISC_REG_UNPREPARED, 0);
11086     mb();
11087 
11088     /* Make sure all is written to the chip before the reset */
11089     wmb();
11090 
11091     /*
11092      * Wait for 1ms to empty GLUE and PCI-E core queues,
11093      * PSWHST, GRC and PSWRD Tetris buffer.
11094      */
11095     DELAY(1000);
11096 
11097     /* Prepare to chip reset: */
11098     /* MCP */
11099     if (global) {
11100         bxe_reset_mcp_prep(sc, &val);
11101     }
11102 
11103     /* PXP */
11104     bxe_pxp_prep(sc);
11105     mb();
11106 
11107     /* reset the chip */
11108     bxe_process_kill_chip_reset(sc, global);
11109     mb();
11110 
11111     /* clear errors in PGB */
11112     if (!CHIP_IS_E1(sc))
11113         REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11114 
11115     /* Recover after reset: */
11116     /* MCP */
11117     if (global && bxe_reset_mcp_comp(sc, val)) {
11118         return (-1);
11119     }
11120 
11121     /* XXX add resetting the NO_MCP mode DB here */
11122 
11123     /* Open the gates #2, #3 and #4 */
11124     bxe_set_234_gates(sc, FALSE);
11125 
11126     /* XXX
11127      * IGU/AEU preparation bring back the AEU/IGU to a reset state
11128      * re-enable attentions
11129      */
11130 
11131     return (0);
11132 }
11133 
11134 static int
11135 bxe_leader_reset(struct bxe_softc *sc)
11136 {
11137     int rc = 0;
11138     uint8_t global = bxe_reset_is_global(sc);
11139     uint32_t load_code;
11140 
11141     /*
11142      * If not going to reset MCP, load "fake" driver to reset HW while
11143      * driver is owner of the HW.
11144      */
11145     if (!global && !BXE_NOMCP(sc)) {
11146         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11147                                    DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11148         if (!load_code) {
11149             BLOGE(sc, "MCP response failure, aborting\n");
11150             rc = -1;
11151             goto exit_leader_reset;
11152         }
11153 
11154         if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11155             (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11156             BLOGE(sc, "MCP unexpected response, aborting\n");
11157             rc = -1;
11158             goto exit_leader_reset2;
11159         }
11160 
11161         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11162         if (!load_code) {
11163             BLOGE(sc, "MCP response failure, aborting\n");
11164             rc = -1;
11165             goto exit_leader_reset2;
11166         }
11167     }
11168 
11169     /* try to recover after the failure */
11170     if (bxe_process_kill(sc, global)) {
11171         BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11172         rc = -1;
11173         goto exit_leader_reset2;
11174     }
11175 
11176     /*
11177      * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11178      * state.
11179      */
11180     bxe_set_reset_done(sc);
11181     if (global) {
11182         bxe_clear_reset_global(sc);
11183     }
11184 
11185 exit_leader_reset2:
11186 
11187     /* unload "fake driver" if it was loaded */
11188     if (!global && !BXE_NOMCP(sc)) {
11189         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11190         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11191     }
11192 
11193 exit_leader_reset:
11194 
11195     sc->is_leader = 0;
11196     bxe_release_leader_lock(sc);
11197 
11198     mb();
11199     return (rc);
11200 }
11201 
11202 /*
11203  * prepare INIT transition, parameters configured:
11204  *   - HC configuration
11205  *   - Queue's CDU context
11206  */
11207 static void
11208 bxe_pf_q_prep_init(struct bxe_softc               *sc,
11209                    struct bxe_fastpath            *fp,
11210                    struct ecore_queue_init_params *init_params)
11211 {
11212     uint8_t cos;
11213     int cxt_index, cxt_offset;
11214 
11215     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11216     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11217 
11218     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11219     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11220 
11221     /* HC rate */
11222     init_params->rx.hc_rate =
11223         sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11224     init_params->tx.hc_rate =
11225         sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11226 
11227     /* FW SB ID */
11228     init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11229 
11230     /* CQ index among the SB indices */
11231     init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11232     init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11233 
11234     /* set maximum number of COSs supported by this queue */
11235     init_params->max_cos = sc->max_cos;
11236 
11237     BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11238           fp->index, init_params->max_cos);
11239 
11240     /* set the context pointers queue object */
11241     for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11242         /* XXX change index/cid here if ever support multiple tx CoS */
11243         /* fp->txdata[cos]->cid */
11244         cxt_index = fp->index / ILT_PAGE_CIDS;
11245         cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11246         init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11247     }
11248 }
11249 
11250 /* set flags that are common for the Tx-only and not normal connections */
11251 static unsigned long
11252 bxe_get_common_flags(struct bxe_softc    *sc,
11253                      struct bxe_fastpath *fp,
11254                      uint8_t             zero_stats)
11255 {
11256     unsigned long flags = 0;
11257 
11258     /* PF driver will always initialize the Queue to an ACTIVE state */
11259     bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11260 
11261     /*
11262      * tx only connections collect statistics (on the same index as the
11263      * parent connection). The statistics are zeroed when the parent
11264      * connection is initialized.
11265      */
11266 
11267     bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11268     if (zero_stats) {
11269         bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11270     }
11271 
11272     /*
11273      * tx only connections can support tx-switching, though their
11274      * CoS-ness doesn't survive the loopback
11275      */
11276     if (sc->flags & BXE_TX_SWITCHING) {
11277         bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11278     }
11279 
11280     bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11281 
11282     return (flags);
11283 }
11284 
11285 static unsigned long
11286 bxe_get_q_flags(struct bxe_softc    *sc,
11287                 struct bxe_fastpath *fp,
11288                 uint8_t             leading)
11289 {
11290     unsigned long flags = 0;
11291 
11292     if (IS_MF_SD(sc)) {
11293         bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11294     }
11295 
11296     if (if_getcapenable(sc->ifp) & IFCAP_LRO) {
11297         bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11298         bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11299     }
11300 
11301     if (leading) {
11302         bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11303         bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11304     }
11305 
11306     bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11307 
11308     /* merge with common flags */
11309     return (flags | bxe_get_common_flags(sc, fp, TRUE));
11310 }
11311 
11312 static void
11313 bxe_pf_q_prep_general(struct bxe_softc                  *sc,
11314                       struct bxe_fastpath               *fp,
11315                       struct ecore_general_setup_params *gen_init,
11316                       uint8_t                           cos)
11317 {
11318     gen_init->stat_id = bxe_stats_id(fp);
11319     gen_init->spcl_id = fp->cl_id;
11320     gen_init->mtu = sc->mtu;
11321     gen_init->cos = cos;
11322 }
11323 
11324 static void
11325 bxe_pf_rx_q_prep(struct bxe_softc              *sc,
11326                  struct bxe_fastpath           *fp,
11327                  struct rxq_pause_params       *pause,
11328                  struct ecore_rxq_setup_params *rxq_init)
11329 {
11330     uint8_t max_sge = 0;
11331     uint16_t sge_sz = 0;
11332     uint16_t tpa_agg_size = 0;
11333 
11334     pause->sge_th_lo = SGE_TH_LO(sc);
11335     pause->sge_th_hi = SGE_TH_HI(sc);
11336 
11337     /* validate SGE ring has enough to cross high threshold */
11338     if (sc->dropless_fc &&
11339             (pause->sge_th_hi + FW_PREFETCH_CNT) >
11340             (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11341         BLOGW(sc, "sge ring threshold limit\n");
11342     }
11343 
11344     /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11345     tpa_agg_size = (2 * sc->mtu);
11346     if (tpa_agg_size < sc->max_aggregation_size) {
11347         tpa_agg_size = sc->max_aggregation_size;
11348     }
11349 
11350     max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11351     max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11352                    (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11353     sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11354 
11355     /* pause - not for e1 */
11356     if (!CHIP_IS_E1(sc)) {
11357         pause->bd_th_lo = BD_TH_LO(sc);
11358         pause->bd_th_hi = BD_TH_HI(sc);
11359 
11360         pause->rcq_th_lo = RCQ_TH_LO(sc);
11361         pause->rcq_th_hi = RCQ_TH_HI(sc);
11362 
11363         /* validate rings have enough entries to cross high thresholds */
11364         if (sc->dropless_fc &&
11365             pause->bd_th_hi + FW_PREFETCH_CNT >
11366             sc->rx_ring_size) {
11367             BLOGW(sc, "rx bd ring threshold limit\n");
11368         }
11369 
11370         if (sc->dropless_fc &&
11371             pause->rcq_th_hi + FW_PREFETCH_CNT >
11372             RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11373             BLOGW(sc, "rcq ring threshold limit\n");
11374         }
11375 
11376         pause->pri_map = 1;
11377     }
11378 
11379     /* rxq setup */
11380     rxq_init->dscr_map   = fp->rx_dma.paddr;
11381     rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11382     rxq_init->rcq_map    = fp->rcq_dma.paddr;
11383     rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11384 
11385     /*
11386      * This should be a maximum number of data bytes that may be
11387      * placed on the BD (not including paddings).
11388      */
11389     rxq_init->buf_sz = (fp->rx_buf_size -
11390                         IP_HEADER_ALIGNMENT_PADDING);
11391 
11392     rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11393     rxq_init->tpa_agg_sz      = tpa_agg_size;
11394     rxq_init->sge_buf_sz      = sge_sz;
11395     rxq_init->max_sges_pkt    = max_sge;
11396     rxq_init->rss_engine_id   = SC_FUNC(sc);
11397     rxq_init->mcast_engine_id = SC_FUNC(sc);
11398 
11399     /*
11400      * Maximum number or simultaneous TPA aggregation for this Queue.
11401      * For PF Clients it should be the maximum available number.
11402      * VF driver(s) may want to define it to a smaller value.
11403      */
11404     rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11405 
11406     rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11407     rxq_init->fw_sb_id = fp->fw_sb_id;
11408 
11409     rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11410 
11411     /*
11412      * configure silent vlan removal
11413      * if multi function mode is afex, then mask default vlan
11414      */
11415     if (IS_MF_AFEX(sc)) {
11416         rxq_init->silent_removal_value =
11417             sc->devinfo.mf_info.afex_def_vlan_tag;
11418         rxq_init->silent_removal_mask = EVL_VLID_MASK;
11419     }
11420 }
11421 
11422 static void
11423 bxe_pf_tx_q_prep(struct bxe_softc              *sc,
11424                  struct bxe_fastpath           *fp,
11425                  struct ecore_txq_setup_params *txq_init,
11426                  uint8_t                       cos)
11427 {
11428     /*
11429      * XXX If multiple CoS is ever supported then each fastpath structure
11430      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11431      * fp->txdata[cos]->tx_dma.paddr;
11432      */
11433     txq_init->dscr_map     = fp->tx_dma.paddr;
11434     txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11435     txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11436     txq_init->fw_sb_id     = fp->fw_sb_id;
11437 
11438     /*
11439      * set the TSS leading client id for TX classfication to the
11440      * leading RSS client id
11441      */
11442     txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11443 }
11444 
11445 /*
11446  * This function performs 2 steps in a queue state machine:
11447  *   1) RESET->INIT
11448  *   2) INIT->SETUP
11449  */
11450 static int
11451 bxe_setup_queue(struct bxe_softc    *sc,
11452                 struct bxe_fastpath *fp,
11453                 uint8_t             leading)
11454 {
11455     struct ecore_queue_state_params q_params = { NULL };
11456     struct ecore_queue_setup_params *setup_params =
11457                         &q_params.params.setup;
11458     int rc;
11459 
11460     BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11461 
11462     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11463 
11464     q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11465 
11466     /* we want to wait for completion in this context */
11467     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11468 
11469     /* prepare the INIT parameters */
11470     bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11471 
11472     /* Set the command */
11473     q_params.cmd = ECORE_Q_CMD_INIT;
11474 
11475     /* Change the state to INIT */
11476     rc = ecore_queue_state_change(sc, &q_params);
11477     if (rc) {
11478         BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11479         return (rc);
11480     }
11481 
11482     BLOGD(sc, DBG_LOAD, "init complete\n");
11483 
11484     /* now move the Queue to the SETUP state */
11485     memset(setup_params, 0, sizeof(*setup_params));
11486 
11487     /* set Queue flags */
11488     setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11489 
11490     /* set general SETUP parameters */
11491     bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11492                           FIRST_TX_COS_INDEX);
11493 
11494     bxe_pf_rx_q_prep(sc, fp,
11495                      &setup_params->pause_params,
11496                      &setup_params->rxq_params);
11497 
11498     bxe_pf_tx_q_prep(sc, fp,
11499                      &setup_params->txq_params,
11500                      FIRST_TX_COS_INDEX);
11501 
11502     /* Set the command */
11503     q_params.cmd = ECORE_Q_CMD_SETUP;
11504 
11505     /* change the state to SETUP */
11506     rc = ecore_queue_state_change(sc, &q_params);
11507     if (rc) {
11508         BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11509         return (rc);
11510     }
11511 
11512     return (rc);
11513 }
11514 
11515 static int
11516 bxe_setup_leading(struct bxe_softc *sc)
11517 {
11518     return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11519 }
11520 
11521 static int
11522 bxe_config_rss_pf(struct bxe_softc            *sc,
11523                   struct ecore_rss_config_obj *rss_obj,
11524                   uint8_t                     config_hash)
11525 {
11526     struct ecore_config_rss_params params = { NULL };
11527     int i;
11528 
11529     /*
11530      * Although RSS is meaningless when there is a single HW queue we
11531      * still need it enabled in order to have HW Rx hash generated.
11532      */
11533 
11534     params.rss_obj = rss_obj;
11535 
11536     bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11537 
11538     bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11539 
11540     /* RSS configuration */
11541     bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11542     bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11543     bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11544     bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11545     if (rss_obj->udp_rss_v4) {
11546         bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11547     }
11548     if (rss_obj->udp_rss_v6) {
11549         bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11550     }
11551 
11552     /* Hash bits */
11553     params.rss_result_mask = MULTI_MASK;
11554 
11555     memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11556 
11557     if (config_hash) {
11558         /* RSS keys */
11559         for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11560             params.rss_key[i] = arc4random();
11561         }
11562 
11563         bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11564     }
11565 
11566     return (ecore_config_rss(sc, &params));
11567 }
11568 
11569 static int
11570 bxe_config_rss_eth(struct bxe_softc *sc,
11571                    uint8_t          config_hash)
11572 {
11573     return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11574 }
11575 
11576 static int
11577 bxe_init_rss_pf(struct bxe_softc *sc)
11578 {
11579     uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11580     int i;
11581 
11582     /*
11583      * Prepare the initial contents of the indirection table if
11584      * RSS is enabled
11585      */
11586     for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11587         sc->rss_conf_obj.ind_table[i] =
11588             (sc->fp->cl_id + (i % num_eth_queues));
11589     }
11590 
11591     if (sc->udp_rss) {
11592         sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11593     }
11594 
11595     /*
11596      * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11597      * per-port, so if explicit configuration is needed, do it only
11598      * for a PMF.
11599      *
11600      * For 57712 and newer it's a per-function configuration.
11601      */
11602     return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11603 }
11604 
11605 static int
11606 bxe_set_mac_one(struct bxe_softc          *sc,
11607                 uint8_t                   *mac,
11608                 struct ecore_vlan_mac_obj *obj,
11609                 uint8_t                   set,
11610                 int                       mac_type,
11611                 unsigned long             *ramrod_flags)
11612 {
11613     struct ecore_vlan_mac_ramrod_params ramrod_param;
11614     int rc;
11615 
11616     memset(&ramrod_param, 0, sizeof(ramrod_param));
11617 
11618     /* fill in general parameters */
11619     ramrod_param.vlan_mac_obj = obj;
11620     ramrod_param.ramrod_flags = *ramrod_flags;
11621 
11622     /* fill a user request section if needed */
11623     if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11624         memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11625 
11626         bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11627 
11628         /* Set the command: ADD or DEL */
11629         ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11630                                             ECORE_VLAN_MAC_DEL;
11631     }
11632 
11633     rc = ecore_config_vlan_mac(sc, &ramrod_param);
11634 
11635     if (rc == ECORE_EXISTS) {
11636         BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11637         /* do not treat adding same MAC as error */
11638         rc = 0;
11639     } else if (rc < 0) {
11640         BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11641     }
11642 
11643     return (rc);
11644 }
11645 
11646 static int
11647 bxe_set_eth_mac(struct bxe_softc *sc,
11648                 uint8_t          set)
11649 {
11650     unsigned long ramrod_flags = 0;
11651 
11652     BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11653 
11654     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11655 
11656     /* Eth MAC is set on RSS leading client (fp[0]) */
11657     return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11658                             &sc->sp_objs->mac_obj,
11659                             set, ECORE_ETH_MAC, &ramrod_flags));
11660 }
11661 
11662 static int
11663 bxe_get_cur_phy_idx(struct bxe_softc *sc)
11664 {
11665     uint32_t sel_phy_idx = 0;
11666 
11667     if (sc->link_params.num_phys <= 1) {
11668         return (ELINK_INT_PHY);
11669     }
11670 
11671     if (sc->link_vars.link_up) {
11672         sel_phy_idx = ELINK_EXT_PHY1;
11673         /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11674         if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11675             (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11676              ELINK_SUPPORTED_FIBRE))
11677             sel_phy_idx = ELINK_EXT_PHY2;
11678     } else {
11679         switch (elink_phy_selection(&sc->link_params)) {
11680         case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11681         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11682         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11683                sel_phy_idx = ELINK_EXT_PHY1;
11684                break;
11685         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11686         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11687                sel_phy_idx = ELINK_EXT_PHY2;
11688                break;
11689         }
11690     }
11691 
11692     return (sel_phy_idx);
11693 }
11694 
11695 static int
11696 bxe_get_link_cfg_idx(struct bxe_softc *sc)
11697 {
11698     uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11699 
11700     /*
11701      * The selected activated PHY is always after swapping (in case PHY
11702      * swapping is enabled). So when swapping is enabled, we need to reverse
11703      * the configuration
11704      */
11705 
11706     if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11707         if (sel_phy_idx == ELINK_EXT_PHY1)
11708             sel_phy_idx = ELINK_EXT_PHY2;
11709         else if (sel_phy_idx == ELINK_EXT_PHY2)
11710             sel_phy_idx = ELINK_EXT_PHY1;
11711     }
11712 
11713     return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11714 }
11715 
11716 static void
11717 bxe_set_requested_fc(struct bxe_softc *sc)
11718 {
11719     /*
11720      * Initialize link parameters structure variables
11721      * It is recommended to turn off RX FC for jumbo frames
11722      * for better performance
11723      */
11724     if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11725         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11726     } else {
11727         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11728     }
11729 }
11730 
11731 static void
11732 bxe_calc_fc_adv(struct bxe_softc *sc)
11733 {
11734     uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11735     switch (sc->link_vars.ieee_fc &
11736             MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11737     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
11738     default:
11739         sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11740                                            ADVERTISED_Pause);
11741         break;
11742 
11743     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11744         sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11745                                           ADVERTISED_Pause);
11746         break;
11747 
11748     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11749         sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11750         break;
11751     }
11752 }
11753 
11754 static uint16_t
11755 bxe_get_mf_speed(struct bxe_softc *sc)
11756 {
11757     uint16_t line_speed = sc->link_vars.line_speed;
11758     if (IS_MF(sc)) {
11759         uint16_t maxCfg =
11760             bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11761 
11762         /* calculate the current MAX line speed limit for the MF devices */
11763         if (IS_MF_SI(sc)) {
11764             line_speed = (line_speed * maxCfg) / 100;
11765         } else { /* SD mode */
11766             uint16_t vn_max_rate = maxCfg * 100;
11767 
11768             if (vn_max_rate < line_speed) {
11769                 line_speed = vn_max_rate;
11770             }
11771         }
11772     }
11773 
11774     return (line_speed);
11775 }
11776 
11777 static void
11778 bxe_fill_report_data(struct bxe_softc            *sc,
11779                      struct bxe_link_report_data *data)
11780 {
11781     uint16_t line_speed = bxe_get_mf_speed(sc);
11782 
11783     memset(data, 0, sizeof(*data));
11784 
11785     /* fill the report data with the effective line speed */
11786     data->line_speed = line_speed;
11787 
11788     /* Link is down */
11789     if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11790         bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11791     }
11792 
11793     /* Full DUPLEX */
11794     if (sc->link_vars.duplex == DUPLEX_FULL) {
11795         bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11796     }
11797 
11798     /* Rx Flow Control is ON */
11799     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11800         bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11801     }
11802 
11803     /* Tx Flow Control is ON */
11804     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11805         bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11806     }
11807 }
11808 
11809 /* report link status to OS, should be called under phy_lock */
11810 static void
11811 bxe_link_report_locked(struct bxe_softc *sc)
11812 {
11813     struct bxe_link_report_data cur_data;
11814 
11815     /* reread mf_cfg */
11816     if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11817         bxe_read_mf_cfg(sc);
11818     }
11819 
11820     /* Read the current link report info */
11821     bxe_fill_report_data(sc, &cur_data);
11822 
11823     /* Don't report link down or exactly the same link status twice */
11824     if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11825         (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11826                       &sc->last_reported_link.link_report_flags) &&
11827          bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11828                       &cur_data.link_report_flags))) {
11829         return;
11830     }
11831 
11832     sc->link_cnt++;
11833 
11834     /* report new link params and remember the state for the next time */
11835     memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11836 
11837     if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11838                      &cur_data.link_report_flags)) {
11839         if_link_state_change(sc->ifp, LINK_STATE_DOWN);
11840         BLOGI(sc, "NIC Link is Down\n");
11841     } else {
11842         const char *duplex;
11843         const char *flow;
11844 
11845         if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11846                                    &cur_data.link_report_flags)) {
11847             duplex = "full";
11848         } else {
11849             duplex = "half";
11850         }
11851 
11852         /*
11853          * Handle the FC at the end so that only these flags would be
11854          * possibly set. This way we may easily check if there is no FC
11855          * enabled.
11856          */
11857         if (cur_data.link_report_flags) {
11858             if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11859                              &cur_data.link_report_flags) &&
11860                 bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11861                              &cur_data.link_report_flags)) {
11862                 flow = "ON - receive & transmit";
11863             } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11864                                     &cur_data.link_report_flags) &&
11865                        !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11866                                      &cur_data.link_report_flags)) {
11867                 flow = "ON - receive";
11868             } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11869                                      &cur_data.link_report_flags) &&
11870                        bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11871                                     &cur_data.link_report_flags)) {
11872                 flow = "ON - transmit";
11873             } else {
11874                 flow = "none"; /* possible? */
11875             }
11876         } else {
11877             flow = "none";
11878         }
11879 
11880         if_link_state_change(sc->ifp, LINK_STATE_UP);
11881         BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11882               cur_data.line_speed, duplex, flow);
11883     }
11884 }
11885 
11886 static void
11887 bxe_link_report(struct bxe_softc *sc)
11888 {
11889     bxe_acquire_phy_lock(sc);
11890     bxe_link_report_locked(sc);
11891     bxe_release_phy_lock(sc);
11892 }
11893 
11894 static void
11895 bxe_link_status_update(struct bxe_softc *sc)
11896 {
11897     if (sc->state != BXE_STATE_OPEN) {
11898         return;
11899     }
11900 
11901     if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11902         elink_link_status_update(&sc->link_params, &sc->link_vars);
11903     } else {
11904         sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11905                                   ELINK_SUPPORTED_10baseT_Full |
11906                                   ELINK_SUPPORTED_100baseT_Half |
11907                                   ELINK_SUPPORTED_100baseT_Full |
11908                                   ELINK_SUPPORTED_1000baseT_Full |
11909                                   ELINK_SUPPORTED_2500baseX_Full |
11910                                   ELINK_SUPPORTED_10000baseT_Full |
11911                                   ELINK_SUPPORTED_TP |
11912                                   ELINK_SUPPORTED_FIBRE |
11913                                   ELINK_SUPPORTED_Autoneg |
11914                                   ELINK_SUPPORTED_Pause |
11915                                   ELINK_SUPPORTED_Asym_Pause);
11916         sc->port.advertising[0] = sc->port.supported[0];
11917 
11918         sc->link_params.sc                = sc;
11919         sc->link_params.port              = SC_PORT(sc);
11920         sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11921         sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11922         sc->link_params.req_line_speed[0] = SPEED_10000;
11923         sc->link_params.speed_cap_mask[0] = 0x7f0000;
11924         sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11925 
11926         if (CHIP_REV_IS_FPGA(sc)) {
11927             sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11928             sc->link_vars.line_speed  = ELINK_SPEED_1000;
11929             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11930                                          LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11931         } else {
11932             sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11933             sc->link_vars.line_speed  = ELINK_SPEED_10000;
11934             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11935                                          LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11936         }
11937 
11938         sc->link_vars.link_up = 1;
11939 
11940         sc->link_vars.duplex    = DUPLEX_FULL;
11941         sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11942 
11943         if (IS_PF(sc)) {
11944             REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11945             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11946             bxe_link_report(sc);
11947         }
11948     }
11949 
11950     if (IS_PF(sc)) {
11951         if (sc->link_vars.link_up) {
11952             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11953         } else {
11954             bxe_stats_handle(sc, STATS_EVENT_STOP);
11955         }
11956         bxe_link_report(sc);
11957     } else {
11958         bxe_link_report(sc);
11959         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11960     }
11961 }
11962 
11963 static int
11964 bxe_initial_phy_init(struct bxe_softc *sc,
11965                      int              load_mode)
11966 {
11967     int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
11968     uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
11969     struct elink_params *lp = &sc->link_params;
11970 
11971     bxe_set_requested_fc(sc);
11972 
11973     if (CHIP_REV_IS_SLOW(sc)) {
11974         uint32_t bond = CHIP_BOND_ID(sc);
11975         uint32_t feat = 0;
11976 
11977         if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
11978             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11979         } else if (bond & 0x4) {
11980             if (CHIP_IS_E3(sc)) {
11981                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
11982             } else {
11983                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
11984             }
11985         } else if (bond & 0x8) {
11986             if (CHIP_IS_E3(sc)) {
11987                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
11988             } else {
11989                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11990             }
11991         }
11992 
11993         /* disable EMAC for E3 and above */
11994         if (bond & 0x2) {
11995             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
11996         }
11997 
11998         sc->link_params.feature_config_flags |= feat;
11999     }
12000 
12001     bxe_acquire_phy_lock(sc);
12002 
12003     if (load_mode == LOAD_DIAG) {
12004         lp->loopback_mode = ELINK_LOOPBACK_XGXS;
12005         /* Prefer doing PHY loopback at 10G speed, if possible */
12006         if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
12007             if (lp->speed_cap_mask[cfg_idx] &
12008                 PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
12009                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
12010             } else {
12011                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
12012             }
12013         }
12014     }
12015 
12016     if (load_mode == LOAD_LOOPBACK_EXT) {
12017         lp->loopback_mode = ELINK_LOOPBACK_EXT;
12018     }
12019 
12020     rc = elink_phy_init(&sc->link_params, &sc->link_vars);
12021 
12022     bxe_release_phy_lock(sc);
12023 
12024     bxe_calc_fc_adv(sc);
12025 
12026     if (sc->link_vars.link_up) {
12027         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12028         bxe_link_report(sc);
12029     }
12030 
12031     if (!CHIP_REV_IS_SLOW(sc)) {
12032         bxe_periodic_start(sc);
12033     }
12034 
12035     sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
12036     return (rc);
12037 }
12038 
12039 /* must be called under IF_ADDR_LOCK */
12040 
12041 static int
12042 bxe_set_mc_list(struct bxe_softc *sc)
12043 {
12044     struct ecore_mcast_ramrod_params rparam = { NULL };
12045     int rc = 0;
12046     int mc_count = 0;
12047     int mcnt, i;
12048     struct ecore_mcast_list_elem *mc_mac, *mc_mac_start;
12049     unsigned char *mta;
12050     if_t ifp = sc->ifp;
12051 
12052     mc_count = if_multiaddr_count(ifp, -1);/* XXX they don't have a limit */
12053     if (!mc_count)
12054         return (0);
12055 
12056     mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN *
12057             mc_count, M_DEVBUF, M_NOWAIT);
12058 
12059     if(mta == NULL) {
12060         BLOGE(sc, "Failed to allocate temp mcast list\n");
12061         return (-1);
12062     }
12063     bzero(mta, (sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count));
12064 
12065     mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF, (M_NOWAIT | M_ZERO));
12066     mc_mac_start = mc_mac;
12067 
12068     if (!mc_mac) {
12069         free(mta, M_DEVBUF);
12070         BLOGE(sc, "Failed to allocate temp mcast list\n");
12071         return (-1);
12072     }
12073     bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12074 
12075     /* mta and mcnt not expected to be  different */
12076     if_multiaddr_array(ifp, mta, &mcnt, mc_count);
12077 
12078 
12079     rparam.mcast_obj = &sc->mcast_obj;
12080     ECORE_LIST_INIT(&rparam.mcast_list);
12081 
12082     for(i=0; i< mcnt; i++) {
12083 
12084         mc_mac->mac = (uint8_t *)(mta + (i * ETHER_ADDR_LEN));
12085         ECORE_LIST_PUSH_TAIL(&mc_mac->link, &rparam.mcast_list);
12086 
12087         BLOGD(sc, DBG_LOAD,
12088               "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
12089               mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12090               mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
12091 
12092         mc_mac++;
12093     }
12094     rparam.mcast_list_len = mc_count;
12095 
12096     BXE_MCAST_LOCK(sc);
12097 
12098     /* first, clear all configured multicast MACs */
12099     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12100     if (rc < 0) {
12101         BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12102         BXE_MCAST_UNLOCK(sc);
12103     	free(mc_mac_start, M_DEVBUF);
12104         free(mta, M_DEVBUF);
12105         return (rc);
12106     }
12107 
12108     /* Now add the new MACs */
12109     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12110     if (rc < 0) {
12111         BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12112     }
12113 
12114     BXE_MCAST_UNLOCK(sc);
12115 
12116     free(mc_mac_start, M_DEVBUF);
12117     free(mta, M_DEVBUF);
12118 
12119     return (rc);
12120 }
12121 
12122 static int
12123 bxe_set_uc_list(struct bxe_softc *sc)
12124 {
12125     if_t ifp = sc->ifp;
12126     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12127     struct ifaddr *ifa;
12128     unsigned long ramrod_flags = 0;
12129     int rc;
12130 
12131 #if __FreeBSD_version < 800000
12132     IF_ADDR_LOCK(ifp);
12133 #else
12134     if_addr_rlock(ifp);
12135 #endif
12136 
12137     /* first schedule a cleanup up of old configuration */
12138     rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12139     if (rc < 0) {
12140         BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12141 #if __FreeBSD_version < 800000
12142         IF_ADDR_UNLOCK(ifp);
12143 #else
12144         if_addr_runlock(ifp);
12145 #endif
12146         return (rc);
12147     }
12148 
12149     ifa = if_getifaddr(ifp); /* XXX Is this structure */
12150     while (ifa) {
12151         if (ifa->ifa_addr->sa_family != AF_LINK) {
12152             ifa = TAILQ_NEXT(ifa, ifa_link);
12153             continue;
12154         }
12155 
12156         rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12157                              mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12158         if (rc == -EEXIST) {
12159             BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12160             /* do not treat adding same MAC as an error */
12161             rc = 0;
12162         } else if (rc < 0) {
12163             BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12164 #if __FreeBSD_version < 800000
12165             IF_ADDR_UNLOCK(ifp);
12166 #else
12167             if_addr_runlock(ifp);
12168 #endif
12169             return (rc);
12170         }
12171 
12172         ifa = TAILQ_NEXT(ifa, ifa_link);
12173     }
12174 
12175 #if __FreeBSD_version < 800000
12176     IF_ADDR_UNLOCK(ifp);
12177 #else
12178     if_addr_runlock(ifp);
12179 #endif
12180 
12181     /* Execute the pending commands */
12182     bit_set(&ramrod_flags, RAMROD_CONT);
12183     return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12184                             ECORE_UC_LIST_MAC, &ramrod_flags));
12185 }
12186 
12187 static void
12188 bxe_set_rx_mode(struct bxe_softc *sc)
12189 {
12190     if_t ifp = sc->ifp;
12191     uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12192 
12193     if (sc->state != BXE_STATE_OPEN) {
12194         BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12195         return;
12196     }
12197 
12198     BLOGD(sc, DBG_SP, "if_flags(ifp)=0x%x\n", if_getflags(sc->ifp));
12199 
12200     if (if_getflags(ifp) & IFF_PROMISC) {
12201         rx_mode = BXE_RX_MODE_PROMISC;
12202     } else if ((if_getflags(ifp) & IFF_ALLMULTI) ||
12203                ((if_getamcount(ifp) > BXE_MAX_MULTICAST) &&
12204                 CHIP_IS_E1(sc))) {
12205         rx_mode = BXE_RX_MODE_ALLMULTI;
12206     } else {
12207         if (IS_PF(sc)) {
12208             /* some multicasts */
12209             if (bxe_set_mc_list(sc) < 0) {
12210                 rx_mode = BXE_RX_MODE_ALLMULTI;
12211             }
12212             if (bxe_set_uc_list(sc) < 0) {
12213                 rx_mode = BXE_RX_MODE_PROMISC;
12214             }
12215         }
12216     }
12217 
12218     sc->rx_mode = rx_mode;
12219 
12220     /* schedule the rx_mode command */
12221     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12222         BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12223         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12224         return;
12225     }
12226 
12227     if (IS_PF(sc)) {
12228         bxe_set_storm_rx_mode(sc);
12229     }
12230 }
12231 
12232 
12233 /* update flags in shmem */
12234 static void
12235 bxe_update_drv_flags(struct bxe_softc *sc,
12236                      uint32_t         flags,
12237                      uint32_t         set)
12238 {
12239     uint32_t drv_flags;
12240 
12241     if (SHMEM2_HAS(sc, drv_flags)) {
12242         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12243         drv_flags = SHMEM2_RD(sc, drv_flags);
12244 
12245         if (set) {
12246             SET_FLAGS(drv_flags, flags);
12247         } else {
12248             RESET_FLAGS(drv_flags, flags);
12249         }
12250 
12251         SHMEM2_WR(sc, drv_flags, drv_flags);
12252         BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12253 
12254         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12255     }
12256 }
12257 
12258 /* periodic timer callout routine, only runs when the interface is up */
12259 
12260 static void
12261 bxe_periodic_callout_func(void *xsc)
12262 {
12263     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12264     struct bxe_fastpath *fp;
12265     uint16_t tx_bd_avail;
12266     int i;
12267 
12268     if (!BXE_CORE_TRYLOCK(sc)) {
12269         /* just bail and try again next time */
12270 
12271         if ((sc->state == BXE_STATE_OPEN) &&
12272             (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12273             /* schedule the next periodic callout */
12274             callout_reset(&sc->periodic_callout, hz,
12275                           bxe_periodic_callout_func, sc);
12276         }
12277 
12278         return;
12279     }
12280 
12281     if ((sc->state != BXE_STATE_OPEN) ||
12282         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12283         BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12284         BXE_CORE_UNLOCK(sc);
12285         return;
12286     }
12287 
12288 #if __FreeBSD_version >= 800000
12289 
12290     FOR_EACH_QUEUE(sc, i) {
12291         fp = &sc->fp[i];
12292 
12293         if (BXE_FP_TX_TRYLOCK(fp)) {
12294             if_t ifp = sc->ifp;
12295             /*
12296              * If interface was stopped due to unavailable
12297              * bds, try to process some tx completions
12298              */
12299             (void) bxe_txeof(sc, fp);
12300 
12301             tx_bd_avail = bxe_tx_avail(sc, fp);
12302             if (tx_bd_avail >= BXE_TX_CLEANUP_THRESHOLD) {
12303                 bxe_tx_mq_start_locked(sc, ifp, fp, NULL);
12304             }
12305             BXE_FP_TX_UNLOCK(fp);
12306         }
12307     }
12308 
12309 #else
12310 
12311     fp = &sc->fp[0];
12312     if (BXE_FP_TX_TRYLOCK(fp)) {
12313         struct ifnet *ifp = sc->ifnet;
12314         /*
12315          * If interface was stopped due to unavailable
12316          * bds, try to process some tx completions
12317          */
12318         (void) bxe_txeof(sc, fp);
12319 
12320         tx_bd_avail = bxe_tx_avail(sc, fp);
12321         if (tx_bd_avail >= BXE_TX_CLEANUP_THRESHOLD) {
12322             bxe_tx_start_locked(sc, ifp, fp);
12323         }
12324 
12325         BXE_FP_TX_UNLOCK(fp);
12326     }
12327 
12328 #endif /* #if __FreeBSD_version >= 800000 */
12329 
12330     /* Check for TX timeouts on any fastpath. */
12331     FOR_EACH_QUEUE(sc, i) {
12332         if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12333             /* Ruh-Roh, chip was reset! */
12334             break;
12335         }
12336     }
12337 
12338     if (!CHIP_REV_IS_SLOW(sc)) {
12339         /*
12340          * This barrier is needed to ensure the ordering between the writing
12341          * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12342          * the reading here.
12343          */
12344         mb();
12345         if (sc->port.pmf) {
12346 	    bxe_acquire_phy_lock(sc);
12347             elink_period_func(&sc->link_params, &sc->link_vars);
12348 	    bxe_release_phy_lock(sc);
12349         }
12350     }
12351 
12352     if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12353         int mb_idx = SC_FW_MB_IDX(sc);
12354         uint32_t drv_pulse;
12355         uint32_t mcp_pulse;
12356 
12357         ++sc->fw_drv_pulse_wr_seq;
12358         sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12359 
12360         drv_pulse = sc->fw_drv_pulse_wr_seq;
12361         bxe_drv_pulse(sc);
12362 
12363         mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12364                      MCP_PULSE_SEQ_MASK);
12365 
12366         /*
12367          * The delta between driver pulse and mcp response should
12368          * be 1 (before mcp response) or 0 (after mcp response).
12369          */
12370         if ((drv_pulse != mcp_pulse) &&
12371             (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12372             /* someone lost a heartbeat... */
12373             BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12374                   drv_pulse, mcp_pulse);
12375         }
12376     }
12377 
12378     /* state is BXE_STATE_OPEN */
12379     bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12380 
12381     BXE_CORE_UNLOCK(sc);
12382 
12383     if ((sc->state == BXE_STATE_OPEN) &&
12384         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12385         /* schedule the next periodic callout */
12386         callout_reset(&sc->periodic_callout, hz,
12387                       bxe_periodic_callout_func, sc);
12388     }
12389 }
12390 
12391 static void
12392 bxe_periodic_start(struct bxe_softc *sc)
12393 {
12394     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12395     callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12396 }
12397 
12398 static void
12399 bxe_periodic_stop(struct bxe_softc *sc)
12400 {
12401     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12402     callout_drain(&sc->periodic_callout);
12403 }
12404 
12405 /* start the controller */
12406 static __noinline int
12407 bxe_nic_load(struct bxe_softc *sc,
12408              int              load_mode)
12409 {
12410     uint32_t val;
12411     int load_code = 0;
12412     int i, rc = 0;
12413 
12414     BXE_CORE_LOCK_ASSERT(sc);
12415 
12416     BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12417 
12418     sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12419 
12420     if (IS_PF(sc)) {
12421         /* must be called before memory allocation and HW init */
12422         bxe_ilt_set_info(sc);
12423     }
12424 
12425     sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12426 
12427     bxe_set_fp_rx_buf_size(sc);
12428 
12429     if (bxe_alloc_fp_buffers(sc) != 0) {
12430         BLOGE(sc, "Failed to allocate fastpath memory\n");
12431         sc->state = BXE_STATE_CLOSED;
12432         rc = ENOMEM;
12433         goto bxe_nic_load_error0;
12434     }
12435 
12436     if (bxe_alloc_mem(sc) != 0) {
12437         sc->state = BXE_STATE_CLOSED;
12438         rc = ENOMEM;
12439         goto bxe_nic_load_error0;
12440     }
12441 
12442     if (bxe_alloc_fw_stats_mem(sc) != 0) {
12443         sc->state = BXE_STATE_CLOSED;
12444         rc = ENOMEM;
12445         goto bxe_nic_load_error0;
12446     }
12447 
12448     if (IS_PF(sc)) {
12449         /* set pf load just before approaching the MCP */
12450         bxe_set_pf_load(sc);
12451 
12452         /* if MCP exists send load request and analyze response */
12453         if (!BXE_NOMCP(sc)) {
12454             /* attempt to load pf */
12455             if (bxe_nic_load_request(sc, &load_code) != 0) {
12456                 sc->state = BXE_STATE_CLOSED;
12457                 rc = ENXIO;
12458                 goto bxe_nic_load_error1;
12459             }
12460 
12461             /* what did the MCP say? */
12462             if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12463                 bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12464                 sc->state = BXE_STATE_CLOSED;
12465                 rc = ENXIO;
12466                 goto bxe_nic_load_error2;
12467             }
12468         } else {
12469             BLOGI(sc, "Device has no MCP!\n");
12470             load_code = bxe_nic_load_no_mcp(sc);
12471         }
12472 
12473         /* mark PMF if applicable */
12474         bxe_nic_load_pmf(sc, load_code);
12475 
12476         /* Init Function state controlling object */
12477         bxe_init_func_obj(sc);
12478 
12479         /* Initialize HW */
12480         if (bxe_init_hw(sc, load_code) != 0) {
12481             BLOGE(sc, "HW init failed\n");
12482             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12483             sc->state = BXE_STATE_CLOSED;
12484             rc = ENXIO;
12485             goto bxe_nic_load_error2;
12486         }
12487     }
12488 
12489     /* set ALWAYS_ALIVE bit in shmem */
12490     sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12491     bxe_drv_pulse(sc);
12492     sc->flags |= BXE_NO_PULSE;
12493 
12494     /* attach interrupts */
12495     if (bxe_interrupt_attach(sc) != 0) {
12496         sc->state = BXE_STATE_CLOSED;
12497         rc = ENXIO;
12498         goto bxe_nic_load_error2;
12499     }
12500 
12501     bxe_nic_init(sc, load_code);
12502 
12503     /* Init per-function objects */
12504     if (IS_PF(sc)) {
12505         bxe_init_objs(sc);
12506         // XXX bxe_iov_nic_init(sc);
12507 
12508         /* set AFEX default VLAN tag to an invalid value */
12509         sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12510         // XXX bxe_nic_load_afex_dcc(sc, load_code);
12511 
12512         sc->state = BXE_STATE_OPENING_WAITING_PORT;
12513         rc = bxe_func_start(sc);
12514         if (rc) {
12515             BLOGE(sc, "Function start failed! rc = %d\n", rc);
12516             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12517             sc->state = BXE_STATE_ERROR;
12518             goto bxe_nic_load_error3;
12519         }
12520 
12521         /* send LOAD_DONE command to MCP */
12522         if (!BXE_NOMCP(sc)) {
12523             load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12524             if (!load_code) {
12525                 BLOGE(sc, "MCP response failure, aborting\n");
12526                 sc->state = BXE_STATE_ERROR;
12527                 rc = ENXIO;
12528                 goto bxe_nic_load_error3;
12529             }
12530         }
12531 
12532         rc = bxe_setup_leading(sc);
12533         if (rc) {
12534             BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12535             sc->state = BXE_STATE_ERROR;
12536             goto bxe_nic_load_error3;
12537         }
12538 
12539         FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12540             rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12541             if (rc) {
12542                 BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12543                 sc->state = BXE_STATE_ERROR;
12544                 goto bxe_nic_load_error3;
12545             }
12546         }
12547 
12548         rc = bxe_init_rss_pf(sc);
12549         if (rc) {
12550             BLOGE(sc, "PF RSS init failed\n");
12551             sc->state = BXE_STATE_ERROR;
12552             goto bxe_nic_load_error3;
12553         }
12554     }
12555     /* XXX VF */
12556 
12557     /* now when Clients are configured we are ready to work */
12558     sc->state = BXE_STATE_OPEN;
12559 
12560     /* Configure a ucast MAC */
12561     if (IS_PF(sc)) {
12562         rc = bxe_set_eth_mac(sc, TRUE);
12563     }
12564     if (rc) {
12565         BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12566         sc->state = BXE_STATE_ERROR;
12567         goto bxe_nic_load_error3;
12568     }
12569 
12570     if (sc->port.pmf) {
12571         rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12572         if (rc) {
12573             sc->state = BXE_STATE_ERROR;
12574             goto bxe_nic_load_error3;
12575         }
12576     }
12577 
12578     sc->link_params.feature_config_flags &=
12579         ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12580 
12581     /* start fast path */
12582 
12583     /* Initialize Rx filter */
12584     bxe_set_rx_mode(sc);
12585 
12586     /* start the Tx */
12587     switch (/* XXX load_mode */LOAD_OPEN) {
12588     case LOAD_NORMAL:
12589     case LOAD_OPEN:
12590         break;
12591 
12592     case LOAD_DIAG:
12593     case LOAD_LOOPBACK_EXT:
12594         sc->state = BXE_STATE_DIAG;
12595         break;
12596 
12597     default:
12598         break;
12599     }
12600 
12601     if (sc->port.pmf) {
12602         bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12603     } else {
12604         bxe_link_status_update(sc);
12605     }
12606 
12607     /* start the periodic timer callout */
12608     bxe_periodic_start(sc);
12609 
12610     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12611         /* mark driver is loaded in shmem2 */
12612         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12613         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12614                   (val |
12615                    DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12616                    DRV_FLAGS_CAPABILITIES_LOADED_L2));
12617     }
12618 
12619     /* wait for all pending SP commands to complete */
12620     if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12621         BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12622         bxe_periodic_stop(sc);
12623         bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12624         return (ENXIO);
12625     }
12626 
12627     /* Tell the stack the driver is running! */
12628     if_setdrvflags(sc->ifp, IFF_DRV_RUNNING);
12629 
12630     BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12631 
12632     return (0);
12633 
12634 bxe_nic_load_error3:
12635 
12636     if (IS_PF(sc)) {
12637         bxe_int_disable_sync(sc, 1);
12638 
12639         /* clean out queued objects */
12640         bxe_squeeze_objects(sc);
12641     }
12642 
12643     bxe_interrupt_detach(sc);
12644 
12645 bxe_nic_load_error2:
12646 
12647     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12648         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12649         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12650     }
12651 
12652     sc->port.pmf = 0;
12653 
12654 bxe_nic_load_error1:
12655 
12656     /* clear pf_load status, as it was already set */
12657     if (IS_PF(sc)) {
12658         bxe_clear_pf_load(sc);
12659     }
12660 
12661 bxe_nic_load_error0:
12662 
12663     bxe_free_fw_stats_mem(sc);
12664     bxe_free_fp_buffers(sc);
12665     bxe_free_mem(sc);
12666 
12667     return (rc);
12668 }
12669 
12670 static int
12671 bxe_init_locked(struct bxe_softc *sc)
12672 {
12673     int other_engine = SC_PATH(sc) ? 0 : 1;
12674     uint8_t other_load_status, load_status;
12675     uint8_t global = FALSE;
12676     int rc;
12677 
12678     BXE_CORE_LOCK_ASSERT(sc);
12679 
12680     /* check if the driver is already running */
12681     if (if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) {
12682         BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12683         return (0);
12684     }
12685 
12686     bxe_set_power_state(sc, PCI_PM_D0);
12687 
12688     /*
12689      * If parity occurred during the unload, then attentions and/or
12690      * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12691      * loaded on the current engine to complete the recovery. Parity recovery
12692      * is only relevant for PF driver.
12693      */
12694     if (IS_PF(sc)) {
12695         other_load_status = bxe_get_load_status(sc, other_engine);
12696         load_status = bxe_get_load_status(sc, SC_PATH(sc));
12697 
12698         if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12699             bxe_chk_parity_attn(sc, &global, TRUE)) {
12700             do {
12701                 /*
12702                  * If there are attentions and they are in global blocks, set
12703                  * the GLOBAL_RESET bit regardless whether it will be this
12704                  * function that will complete the recovery or not.
12705                  */
12706                 if (global) {
12707                     bxe_set_reset_global(sc);
12708                 }
12709 
12710                 /*
12711                  * Only the first function on the current engine should try
12712                  * to recover in open. In case of attentions in global blocks
12713                  * only the first in the chip should try to recover.
12714                  */
12715                 if ((!load_status && (!global || !other_load_status)) &&
12716                     bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12717                     BLOGI(sc, "Recovered during init\n");
12718                     break;
12719                 }
12720 
12721                 /* recovery has failed... */
12722                 bxe_set_power_state(sc, PCI_PM_D3hot);
12723                 sc->recovery_state = BXE_RECOVERY_FAILED;
12724 
12725                 BLOGE(sc, "Recovery flow hasn't properly "
12726                           "completed yet, try again later. "
12727                           "If you still see this message after a "
12728                           "few retries then power cycle is required.\n");
12729 
12730                 rc = ENXIO;
12731                 goto bxe_init_locked_done;
12732             } while (0);
12733         }
12734     }
12735 
12736     sc->recovery_state = BXE_RECOVERY_DONE;
12737 
12738     rc = bxe_nic_load(sc, LOAD_OPEN);
12739 
12740 bxe_init_locked_done:
12741 
12742     if (rc) {
12743         /* Tell the stack the driver is NOT running! */
12744         BLOGE(sc, "Initialization failed, "
12745                   "stack notified driver is NOT running!\n");
12746 	if_setdrvflagbits(sc->ifp, 0, IFF_DRV_RUNNING);
12747     }
12748 
12749     return (rc);
12750 }
12751 
12752 static int
12753 bxe_stop_locked(struct bxe_softc *sc)
12754 {
12755     BXE_CORE_LOCK_ASSERT(sc);
12756     return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
12757 }
12758 
12759 /*
12760  * Handles controller initialization when called from an unlocked routine.
12761  * ifconfig calls this function.
12762  *
12763  * Returns:
12764  *   void
12765  */
12766 static void
12767 bxe_init(void *xsc)
12768 {
12769     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12770 
12771     BXE_CORE_LOCK(sc);
12772     bxe_init_locked(sc);
12773     BXE_CORE_UNLOCK(sc);
12774 }
12775 
12776 static int
12777 bxe_init_ifnet(struct bxe_softc *sc)
12778 {
12779     if_t ifp;
12780     int capabilities;
12781 
12782     /* ifconfig entrypoint for media type/status reporting */
12783     ifmedia_init(&sc->ifmedia, IFM_IMASK,
12784                  bxe_ifmedia_update,
12785                  bxe_ifmedia_status);
12786 
12787     /* set the default interface values */
12788     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
12789     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
12790     ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
12791 
12792     sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
12793 
12794     /* allocate the ifnet structure */
12795     if ((ifp = if_gethandle(IFT_ETHER)) == NULL) {
12796         BLOGE(sc, "Interface allocation failed!\n");
12797         return (ENXIO);
12798     }
12799 
12800     if_setsoftc(ifp, sc);
12801     if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
12802     if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
12803     if_setioctlfn(ifp, bxe_ioctl);
12804     if_setstartfn(ifp, bxe_tx_start);
12805     if_setgetcounterfn(ifp, bxe_get_counter);
12806 #if __FreeBSD_version >= 800000
12807     if_settransmitfn(ifp, bxe_tx_mq_start);
12808     if_setqflushfn(ifp, bxe_mq_flush);
12809 #endif
12810 #ifdef FreeBSD8_0
12811     if_settimer(ifp, 0);
12812 #endif
12813     if_setinitfn(ifp, bxe_init);
12814     if_setmtu(ifp, sc->mtu);
12815     if_sethwassist(ifp, (CSUM_IP      |
12816                         CSUM_TCP      |
12817                         CSUM_UDP      |
12818                         CSUM_TSO      |
12819                         CSUM_TCP_IPV6 |
12820                         CSUM_UDP_IPV6));
12821 
12822     capabilities =
12823 #if __FreeBSD_version < 700000
12824         (IFCAP_VLAN_MTU       |
12825          IFCAP_VLAN_HWTAGGING |
12826          IFCAP_HWCSUM         |
12827          IFCAP_JUMBO_MTU      |
12828          IFCAP_LRO);
12829 #else
12830         (IFCAP_VLAN_MTU       |
12831          IFCAP_VLAN_HWTAGGING |
12832          IFCAP_VLAN_HWTSO     |
12833          IFCAP_VLAN_HWFILTER  |
12834          IFCAP_VLAN_HWCSUM    |
12835          IFCAP_HWCSUM         |
12836          IFCAP_JUMBO_MTU      |
12837          IFCAP_LRO            |
12838          IFCAP_TSO4           |
12839          IFCAP_TSO6           |
12840          IFCAP_WOL_MAGIC);
12841 #endif
12842     if_setcapabilitiesbit(ifp, capabilities, 0); /* XXX */
12843     if_setbaudrate(ifp, IF_Gbps(10));
12844 /* XXX */
12845     if_setsendqlen(ifp, sc->tx_ring_size);
12846     if_setsendqready(ifp);
12847 /* XXX */
12848 
12849     sc->ifp = ifp;
12850 
12851     /* attach to the Ethernet interface list */
12852     ether_ifattach(ifp, sc->link_params.mac_addr);
12853 
12854     return (0);
12855 }
12856 
12857 static void
12858 bxe_deallocate_bars(struct bxe_softc *sc)
12859 {
12860     int i;
12861 
12862     for (i = 0; i < MAX_BARS; i++) {
12863         if (sc->bar[i].resource != NULL) {
12864             bus_release_resource(sc->dev,
12865                                  SYS_RES_MEMORY,
12866                                  sc->bar[i].rid,
12867                                  sc->bar[i].resource);
12868             BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
12869                   i, PCIR_BAR(i));
12870         }
12871     }
12872 }
12873 
12874 static int
12875 bxe_allocate_bars(struct bxe_softc *sc)
12876 {
12877     u_int flags;
12878     int i;
12879 
12880     memset(sc->bar, 0, sizeof(sc->bar));
12881 
12882     for (i = 0; i < MAX_BARS; i++) {
12883 
12884         /* memory resources reside at BARs 0, 2, 4 */
12885         /* Run `pciconf -lb` to see mappings */
12886         if ((i != 0) && (i != 2) && (i != 4)) {
12887             continue;
12888         }
12889 
12890         sc->bar[i].rid = PCIR_BAR(i);
12891 
12892         flags = RF_ACTIVE;
12893         if (i == 0) {
12894             flags |= RF_SHAREABLE;
12895         }
12896 
12897         if ((sc->bar[i].resource =
12898              bus_alloc_resource_any(sc->dev,
12899                                     SYS_RES_MEMORY,
12900                                     &sc->bar[i].rid,
12901                                     flags)) == NULL) {
12902             return (0);
12903         }
12904 
12905         sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
12906         sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
12907         sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
12908 
12909         BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%jd) -> %p\n",
12910               i, PCIR_BAR(i),
12911               (void *)rman_get_start(sc->bar[i].resource),
12912               (void *)rman_get_end(sc->bar[i].resource),
12913               rman_get_size(sc->bar[i].resource),
12914               (void *)sc->bar[i].kva);
12915     }
12916 
12917     return (0);
12918 }
12919 
12920 static void
12921 bxe_get_function_num(struct bxe_softc *sc)
12922 {
12923     uint32_t val = 0;
12924 
12925     /*
12926      * Read the ME register to get the function number. The ME register
12927      * holds the relative-function number and absolute-function number. The
12928      * absolute-function number appears only in E2 and above. Before that
12929      * these bits always contained zero, therefore we cannot blindly use them.
12930      */
12931 
12932     val = REG_RD(sc, BAR_ME_REGISTER);
12933 
12934     sc->pfunc_rel =
12935         (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
12936     sc->path_id =
12937         (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
12938 
12939     if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
12940         sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
12941     } else {
12942         sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
12943     }
12944 
12945     BLOGD(sc, DBG_LOAD,
12946           "Relative function %d, Absolute function %d, Path %d\n",
12947           sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
12948 }
12949 
12950 static uint32_t
12951 bxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
12952 {
12953     uint32_t shmem2_size;
12954     uint32_t offset;
12955     uint32_t mf_cfg_offset_value;
12956 
12957     /* Non 57712 */
12958     offset = (SHMEM_RD(sc, func_mb) +
12959               (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
12960 
12961     /* 57712 plus */
12962     if (sc->devinfo.shmem2_base != 0) {
12963         shmem2_size = SHMEM2_RD(sc, size);
12964         if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
12965             mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
12966             if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
12967                 offset = mf_cfg_offset_value;
12968             }
12969         }
12970     }
12971 
12972     return (offset);
12973 }
12974 
12975 static uint32_t
12976 bxe_pcie_capability_read(struct bxe_softc *sc,
12977                          int    reg,
12978                          int    width)
12979 {
12980     int pcie_reg;
12981 
12982     /* ensure PCIe capability is enabled */
12983     if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
12984         if (pcie_reg != 0) {
12985             BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
12986             return (pci_read_config(sc->dev, (pcie_reg + reg), width));
12987         }
12988     }
12989 
12990     BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
12991 
12992     return (0);
12993 }
12994 
12995 static uint8_t
12996 bxe_is_pcie_pending(struct bxe_softc *sc)
12997 {
12998     return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
12999             PCIM_EXP_STA_TRANSACTION_PND);
13000 }
13001 
13002 /*
13003  * Walk the PCI capabiites list for the device to find what features are
13004  * supported. These capabilites may be enabled/disabled by firmware so it's
13005  * best to walk the list rather than make assumptions.
13006  */
13007 static void
13008 bxe_probe_pci_caps(struct bxe_softc *sc)
13009 {
13010     uint16_t link_status;
13011     int reg;
13012 
13013     /* check if PCI Power Management is enabled */
13014     if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
13015         if (reg != 0) {
13016             BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
13017 
13018             sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
13019             sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
13020         }
13021     }
13022 
13023     link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
13024 
13025     /* handle PCIe 2.0 workarounds for 57710 */
13026     if (CHIP_IS_E1(sc)) {
13027         /* workaround for 57710 errata E4_57710_27462 */
13028         sc->devinfo.pcie_link_speed =
13029             (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
13030 
13031         /* workaround for 57710 errata E4_57710_27488 */
13032         sc->devinfo.pcie_link_width =
13033             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13034         if (sc->devinfo.pcie_link_speed > 1) {
13035             sc->devinfo.pcie_link_width =
13036                 ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
13037         }
13038     } else {
13039         sc->devinfo.pcie_link_speed =
13040             (link_status & PCIM_LINK_STA_SPEED);
13041         sc->devinfo.pcie_link_width =
13042             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13043     }
13044 
13045     BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
13046           sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
13047 
13048     sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
13049     sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
13050 
13051     /* check if MSI capability is enabled */
13052     if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
13053         if (reg != 0) {
13054             BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
13055 
13056             sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
13057             sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
13058         }
13059     }
13060 
13061     /* check if MSI-X capability is enabled */
13062     if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
13063         if (reg != 0) {
13064             BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
13065 
13066             sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
13067             sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
13068         }
13069     }
13070 }
13071 
13072 static int
13073 bxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
13074 {
13075     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13076     uint32_t val;
13077 
13078     /* get the outer vlan if we're in switch-dependent mode */
13079 
13080     val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13081     mf_info->ext_id = (uint16_t)val;
13082 
13083     mf_info->multi_vnics_mode = 1;
13084 
13085     if (!VALID_OVLAN(mf_info->ext_id)) {
13086         BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13087         return (1);
13088     }
13089 
13090     /* get the capabilities */
13091     if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13092         FUNC_MF_CFG_PROTOCOL_ISCSI) {
13093         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13094     } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13095                FUNC_MF_CFG_PROTOCOL_FCOE) {
13096         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13097     } else {
13098         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13099     }
13100 
13101     mf_info->vnics_per_port =
13102         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13103 
13104     return (0);
13105 }
13106 
13107 static uint32_t
13108 bxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13109 {
13110     uint32_t retval = 0;
13111     uint32_t val;
13112 
13113     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13114 
13115     if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13116         if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13117             retval |= MF_PROTO_SUPPORT_ETHERNET;
13118         }
13119         if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13120             retval |= MF_PROTO_SUPPORT_ISCSI;
13121         }
13122         if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13123             retval |= MF_PROTO_SUPPORT_FCOE;
13124         }
13125     }
13126 
13127     return (retval);
13128 }
13129 
13130 static int
13131 bxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13132 {
13133     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13134     uint32_t val;
13135 
13136     /*
13137      * There is no outer vlan if we're in switch-independent mode.
13138      * If the mac is valid then assume multi-function.
13139      */
13140 
13141     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13142 
13143     mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13144 
13145     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13146 
13147     mf_info->vnics_per_port =
13148         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13149 
13150     return (0);
13151 }
13152 
13153 static int
13154 bxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13155 {
13156     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13157     uint32_t e1hov_tag;
13158     uint32_t func_config;
13159     uint32_t niv_config;
13160 
13161     mf_info->multi_vnics_mode = 1;
13162 
13163     e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13164     func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13165     niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13166 
13167     mf_info->ext_id =
13168         (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13169                    FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13170 
13171     mf_info->default_vlan =
13172         (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13173                    FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13174 
13175     mf_info->niv_allowed_priorities =
13176         (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13177                   FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13178 
13179     mf_info->niv_default_cos =
13180         (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13181                   FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13182 
13183     mf_info->afex_vlan_mode =
13184         ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13185          FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13186 
13187     mf_info->niv_mba_enabled =
13188         ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13189          FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13190 
13191     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13192 
13193     mf_info->vnics_per_port =
13194         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13195 
13196     return (0);
13197 }
13198 
13199 static int
13200 bxe_check_valid_mf_cfg(struct bxe_softc *sc)
13201 {
13202     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13203     uint32_t mf_cfg1;
13204     uint32_t mf_cfg2;
13205     uint32_t ovlan1;
13206     uint32_t ovlan2;
13207     uint8_t i, j;
13208 
13209     BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13210           SC_PORT(sc));
13211     BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13212           mf_info->mf_config[SC_VN(sc)]);
13213     BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13214           mf_info->multi_vnics_mode);
13215     BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13216           mf_info->vnics_per_port);
13217     BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13218           mf_info->ext_id);
13219     BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13220           mf_info->min_bw[0], mf_info->min_bw[1],
13221           mf_info->min_bw[2], mf_info->min_bw[3]);
13222     BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13223           mf_info->max_bw[0], mf_info->max_bw[1],
13224           mf_info->max_bw[2], mf_info->max_bw[3]);
13225     BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13226           sc->mac_addr_str);
13227 
13228     /* various MF mode sanity checks... */
13229 
13230     if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13231         BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13232               SC_PORT(sc));
13233         return (1);
13234     }
13235 
13236     if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13237         BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13238               mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13239         return (1);
13240     }
13241 
13242     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13243         /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13244         if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13245             BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13246                   SC_VN(sc), OVLAN(sc));
13247             return (1);
13248         }
13249 
13250         if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13251             BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13252                   mf_info->multi_vnics_mode, OVLAN(sc));
13253             return (1);
13254         }
13255 
13256         /*
13257          * Verify all functions are either MF or SF mode. If MF, make sure
13258          * sure that all non-hidden functions have a valid ovlan. If SF,
13259          * make sure that all non-hidden functions have an invalid ovlan.
13260          */
13261         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13262             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13263             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13264             if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13265                 (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13266                  ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13267                 BLOGE(sc, "mf_mode=SD function %d MF config "
13268                           "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13269                       i, mf_info->multi_vnics_mode, ovlan1);
13270                 return (1);
13271             }
13272         }
13273 
13274         /* Verify all funcs on the same port each have a different ovlan. */
13275         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13276             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13277             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13278             /* iterate from the next function on the port to the max func */
13279             for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13280                 mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13281                 ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13282                 if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13283                     VALID_OVLAN(ovlan1) &&
13284                     !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13285                     VALID_OVLAN(ovlan2) &&
13286                     (ovlan1 == ovlan2)) {
13287                     BLOGE(sc, "mf_mode=SD functions %d and %d "
13288                               "have the same ovlan (%d)\n",
13289                           i, j, ovlan1);
13290                     return (1);
13291                 }
13292             }
13293         }
13294     } /* MULTI_FUNCTION_SD */
13295 
13296     return (0);
13297 }
13298 
13299 static int
13300 bxe_get_mf_cfg_info(struct bxe_softc *sc)
13301 {
13302     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13303     uint32_t val, mac_upper;
13304     uint8_t i, vnic;
13305 
13306     /* initialize mf_info defaults */
13307     mf_info->vnics_per_port   = 1;
13308     mf_info->multi_vnics_mode = FALSE;
13309     mf_info->path_has_ovlan   = FALSE;
13310     mf_info->mf_mode          = SINGLE_FUNCTION;
13311 
13312     if (!CHIP_IS_MF_CAP(sc)) {
13313         return (0);
13314     }
13315 
13316     if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13317         BLOGE(sc, "Invalid mf_cfg_base!\n");
13318         return (1);
13319     }
13320 
13321     /* get the MF mode (switch dependent / independent / single-function) */
13322 
13323     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13324 
13325     switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13326     {
13327     case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13328 
13329         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13330 
13331         /* check for legal upper mac bytes */
13332         if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13333             mf_info->mf_mode = MULTI_FUNCTION_SI;
13334         } else {
13335             BLOGE(sc, "Invalid config for Switch Independent mode\n");
13336         }
13337 
13338         break;
13339 
13340     case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13341     case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13342 
13343         /* get outer vlan configuration */
13344         val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13345 
13346         if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13347             FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13348             mf_info->mf_mode = MULTI_FUNCTION_SD;
13349         } else {
13350             BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13351         }
13352 
13353         break;
13354 
13355     case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13356 
13357         /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13358         return (0);
13359 
13360     case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13361 
13362         /*
13363          * Mark MF mode as NIV if MCP version includes NPAR-SD support
13364          * and the MAC address is valid.
13365          */
13366         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13367 
13368         if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13369             (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13370             mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13371         } else {
13372             BLOGE(sc, "Invalid config for AFEX mode\n");
13373         }
13374 
13375         break;
13376 
13377     default:
13378 
13379         BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13380               (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13381 
13382         return (1);
13383     }
13384 
13385     /* set path mf_mode (which could be different than function mf_mode) */
13386     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13387         mf_info->path_has_ovlan = TRUE;
13388     } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13389         /*
13390          * Decide on path multi vnics mode. If we're not in MF mode and in
13391          * 4-port mode, this is good enough to check vnic-0 of the other port
13392          * on the same path
13393          */
13394         if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13395             uint8_t other_port = !(PORT_ID(sc) & 1);
13396             uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13397 
13398             val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13399 
13400             mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13401         }
13402     }
13403 
13404     if (mf_info->mf_mode == SINGLE_FUNCTION) {
13405         /* invalid MF config */
13406         if (SC_VN(sc) >= 1) {
13407             BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13408             return (1);
13409         }
13410 
13411         return (0);
13412     }
13413 
13414     /* get the MF configuration */
13415     mf_info->mf_config[SC_VN(sc)] =
13416         MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13417 
13418     switch(mf_info->mf_mode)
13419     {
13420     case MULTI_FUNCTION_SD:
13421 
13422         bxe_get_shmem_mf_cfg_info_sd(sc);
13423         break;
13424 
13425     case MULTI_FUNCTION_SI:
13426 
13427         bxe_get_shmem_mf_cfg_info_si(sc);
13428         break;
13429 
13430     case MULTI_FUNCTION_AFEX:
13431 
13432         bxe_get_shmem_mf_cfg_info_niv(sc);
13433         break;
13434 
13435     default:
13436 
13437         BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13438               mf_info->mf_mode);
13439         return (1);
13440     }
13441 
13442     /* get the congestion management parameters */
13443 
13444     vnic = 0;
13445     FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13446         /* get min/max bw */
13447         val = MFCFG_RD(sc, func_mf_config[i].config);
13448         mf_info->min_bw[vnic] =
13449             ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13450         mf_info->max_bw[vnic] =
13451             ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13452         vnic++;
13453     }
13454 
13455     return (bxe_check_valid_mf_cfg(sc));
13456 }
13457 
13458 static int
13459 bxe_get_shmem_info(struct bxe_softc *sc)
13460 {
13461     int port;
13462     uint32_t mac_hi, mac_lo, val;
13463 
13464     port = SC_PORT(sc);
13465     mac_hi = mac_lo = 0;
13466 
13467     sc->link_params.sc   = sc;
13468     sc->link_params.port = port;
13469 
13470     /* get the hardware config info */
13471     sc->devinfo.hw_config =
13472         SHMEM_RD(sc, dev_info.shared_hw_config.config);
13473     sc->devinfo.hw_config2 =
13474         SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13475 
13476     sc->link_params.hw_led_mode =
13477         ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13478          SHARED_HW_CFG_LED_MODE_SHIFT);
13479 
13480     /* get the port feature config */
13481     sc->port.config =
13482         SHMEM_RD(sc, dev_info.port_feature_config[port].config),
13483 
13484     /* get the link params */
13485     sc->link_params.speed_cap_mask[0] =
13486         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13487     sc->link_params.speed_cap_mask[1] =
13488         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13489 
13490     /* get the lane config */
13491     sc->link_params.lane_config =
13492         SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13493 
13494     /* get the link config */
13495     val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13496     sc->port.link_config[ELINK_INT_PHY] = val;
13497     sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13498     sc->port.link_config[ELINK_EXT_PHY1] =
13499         SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13500 
13501     /* get the override preemphasis flag and enable it or turn it off */
13502     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13503     if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13504         sc->link_params.feature_config_flags |=
13505             ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13506     } else {
13507         sc->link_params.feature_config_flags &=
13508             ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13509     }
13510 
13511     /* get the initial value of the link params */
13512     sc->link_params.multi_phy_config =
13513         SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13514 
13515     /* get external phy info */
13516     sc->port.ext_phy_config =
13517         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13518 
13519     /* get the multifunction configuration */
13520     bxe_get_mf_cfg_info(sc);
13521 
13522     /* get the mac address */
13523     if (IS_MF(sc)) {
13524         mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13525         mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13526     } else {
13527         mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13528         mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13529     }
13530 
13531     if ((mac_lo == 0) && (mac_hi == 0)) {
13532         *sc->mac_addr_str = 0;
13533         BLOGE(sc, "No Ethernet address programmed!\n");
13534     } else {
13535         sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13536         sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13537         sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13538         sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13539         sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13540         sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13541         snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13542                  "%02x:%02x:%02x:%02x:%02x:%02x",
13543                  sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13544                  sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13545                  sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13546         BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13547     }
13548 
13549     return (0);
13550 }
13551 
13552 static void
13553 bxe_get_tunable_params(struct bxe_softc *sc)
13554 {
13555     /* sanity checks */
13556 
13557     if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13558         (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13559         (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13560         BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13561         bxe_interrupt_mode = INTR_MODE_MSIX;
13562     }
13563 
13564     if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13565         BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13566         bxe_queue_count = 0;
13567     }
13568 
13569     if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13570         if (bxe_max_rx_bufs == 0) {
13571             bxe_max_rx_bufs = RX_BD_USABLE;
13572         } else {
13573             BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13574             bxe_max_rx_bufs = 2048;
13575         }
13576     }
13577 
13578     if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13579         BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13580         bxe_hc_rx_ticks = 25;
13581     }
13582 
13583     if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13584         BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13585         bxe_hc_tx_ticks = 50;
13586     }
13587 
13588     if (bxe_max_aggregation_size == 0) {
13589         bxe_max_aggregation_size = TPA_AGG_SIZE;
13590     }
13591 
13592     if (bxe_max_aggregation_size > 0xffff) {
13593         BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13594               bxe_max_aggregation_size);
13595         bxe_max_aggregation_size = TPA_AGG_SIZE;
13596     }
13597 
13598     if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13599         BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13600         bxe_mrrs = -1;
13601     }
13602 
13603     if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13604         BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13605         bxe_autogreeen = 0;
13606     }
13607 
13608     if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13609         BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13610         bxe_udp_rss = 0;
13611     }
13612 
13613     /* pull in user settings */
13614 
13615     sc->interrupt_mode       = bxe_interrupt_mode;
13616     sc->max_rx_bufs          = bxe_max_rx_bufs;
13617     sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13618     sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13619     sc->max_aggregation_size = bxe_max_aggregation_size;
13620     sc->mrrs                 = bxe_mrrs;
13621     sc->autogreeen           = bxe_autogreeen;
13622     sc->udp_rss              = bxe_udp_rss;
13623 
13624     if (bxe_interrupt_mode == INTR_MODE_INTX) {
13625         sc->num_queues = 1;
13626     } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13627         sc->num_queues =
13628             min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13629                 MAX_RSS_CHAINS);
13630         if (sc->num_queues > mp_ncpus) {
13631             sc->num_queues = mp_ncpus;
13632         }
13633     }
13634 
13635     BLOGD(sc, DBG_LOAD,
13636           "User Config: "
13637           "debug=0x%lx "
13638           "interrupt_mode=%d "
13639           "queue_count=%d "
13640           "hc_rx_ticks=%d "
13641           "hc_tx_ticks=%d "
13642           "rx_budget=%d "
13643           "max_aggregation_size=%d "
13644           "mrrs=%d "
13645           "autogreeen=%d "
13646           "udp_rss=%d\n",
13647           bxe_debug,
13648           sc->interrupt_mode,
13649           sc->num_queues,
13650           sc->hc_rx_ticks,
13651           sc->hc_tx_ticks,
13652           bxe_rx_budget,
13653           sc->max_aggregation_size,
13654           sc->mrrs,
13655           sc->autogreeen,
13656           sc->udp_rss);
13657 }
13658 
13659 static int
13660 bxe_media_detect(struct bxe_softc *sc)
13661 {
13662     int port_type;
13663     uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13664 
13665     switch (sc->link_params.phy[phy_idx].media_type) {
13666     case ELINK_ETH_PHY_SFPP_10G_FIBER:
13667     case ELINK_ETH_PHY_XFP_FIBER:
13668         BLOGI(sc, "Found 10Gb Fiber media.\n");
13669         sc->media = IFM_10G_SR;
13670         port_type = PORT_FIBRE;
13671         break;
13672     case ELINK_ETH_PHY_SFP_1G_FIBER:
13673         BLOGI(sc, "Found 1Gb Fiber media.\n");
13674         sc->media = IFM_1000_SX;
13675         port_type = PORT_FIBRE;
13676         break;
13677     case ELINK_ETH_PHY_KR:
13678     case ELINK_ETH_PHY_CX4:
13679         BLOGI(sc, "Found 10GBase-CX4 media.\n");
13680         sc->media = IFM_10G_CX4;
13681         port_type = PORT_FIBRE;
13682         break;
13683     case ELINK_ETH_PHY_DA_TWINAX:
13684         BLOGI(sc, "Found 10Gb Twinax media.\n");
13685         sc->media = IFM_10G_TWINAX;
13686         port_type = PORT_DA;
13687         break;
13688     case ELINK_ETH_PHY_BASE_T:
13689         if (sc->link_params.speed_cap_mask[0] &
13690             PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13691             BLOGI(sc, "Found 10GBase-T media.\n");
13692             sc->media = IFM_10G_T;
13693             port_type = PORT_TP;
13694         } else {
13695             BLOGI(sc, "Found 1000Base-T media.\n");
13696             sc->media = IFM_1000_T;
13697             port_type = PORT_TP;
13698         }
13699         break;
13700     case ELINK_ETH_PHY_NOT_PRESENT:
13701         BLOGI(sc, "Media not present.\n");
13702         sc->media = 0;
13703         port_type = PORT_OTHER;
13704         break;
13705     case ELINK_ETH_PHY_UNSPECIFIED:
13706     default:
13707         BLOGI(sc, "Unknown media!\n");
13708         sc->media = 0;
13709         port_type = PORT_OTHER;
13710         break;
13711     }
13712     return port_type;
13713 }
13714 
13715 #define GET_FIELD(value, fname)                     \
13716     (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13717 #define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13718 #define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
13719 
13720 static int
13721 bxe_get_igu_cam_info(struct bxe_softc *sc)
13722 {
13723     int pfid = SC_FUNC(sc);
13724     int igu_sb_id;
13725     uint32_t val;
13726     uint8_t fid, igu_sb_cnt = 0;
13727 
13728     sc->igu_base_sb = 0xff;
13729 
13730     if (CHIP_INT_MODE_IS_BC(sc)) {
13731         int vn = SC_VN(sc);
13732         igu_sb_cnt = sc->igu_sb_cnt;
13733         sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
13734                            FP_SB_MAX_E1x);
13735         sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
13736                           (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
13737         return (0);
13738     }
13739 
13740     /* IGU in normal mode - read CAM */
13741     for (igu_sb_id = 0;
13742          igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
13743          igu_sb_id++) {
13744         val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
13745         if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
13746             continue;
13747         }
13748         fid = IGU_FID(val);
13749         if ((fid & IGU_FID_ENCODE_IS_PF)) {
13750             if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
13751                 continue;
13752             }
13753             if (IGU_VEC(val) == 0) {
13754                 /* default status block */
13755                 sc->igu_dsb_id = igu_sb_id;
13756             } else {
13757                 if (sc->igu_base_sb == 0xff) {
13758                     sc->igu_base_sb = igu_sb_id;
13759                 }
13760                 igu_sb_cnt++;
13761             }
13762         }
13763     }
13764 
13765     /*
13766      * Due to new PF resource allocation by MFW T7.4 and above, it's optional
13767      * that number of CAM entries will not be equal to the value advertised in
13768      * PCI. Driver should use the minimal value of both as the actual status
13769      * block count
13770      */
13771     sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
13772 
13773     if (igu_sb_cnt == 0) {
13774         BLOGE(sc, "CAM configuration error\n");
13775         return (-1);
13776     }
13777 
13778     return (0);
13779 }
13780 
13781 /*
13782  * Gather various information from the device config space, the device itself,
13783  * shmem, and the user input.
13784  */
13785 static int
13786 bxe_get_device_info(struct bxe_softc *sc)
13787 {
13788     uint32_t val;
13789     int rc;
13790 
13791     /* Get the data for the device */
13792     sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
13793     sc->devinfo.device_id    = pci_get_device(sc->dev);
13794     sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
13795     sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
13796 
13797     /* get the chip revision (chip metal comes from pci config space) */
13798     sc->devinfo.chip_id     =
13799     sc->link_params.chip_id =
13800         (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
13801          ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
13802          (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
13803          ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
13804 
13805     /* force 57811 according to MISC register */
13806     if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
13807         if (CHIP_IS_57810(sc)) {
13808             sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
13809                                    (sc->devinfo.chip_id & 0x0000ffff));
13810         } else if (CHIP_IS_57810_MF(sc)) {
13811             sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
13812                                    (sc->devinfo.chip_id & 0x0000ffff));
13813         }
13814         sc->devinfo.chip_id |= 0x1;
13815     }
13816 
13817     BLOGD(sc, DBG_LOAD,
13818           "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
13819           sc->devinfo.chip_id,
13820           ((sc->devinfo.chip_id >> 16) & 0xffff),
13821           ((sc->devinfo.chip_id >> 12) & 0xf),
13822           ((sc->devinfo.chip_id >>  4) & 0xff),
13823           ((sc->devinfo.chip_id >>  0) & 0xf));
13824 
13825     val = (REG_RD(sc, 0x2874) & 0x55);
13826     if ((sc->devinfo.chip_id & 0x1) ||
13827         (CHIP_IS_E1(sc) && val) ||
13828         (CHIP_IS_E1H(sc) && (val == 0x55))) {
13829         sc->flags |= BXE_ONE_PORT_FLAG;
13830         BLOGD(sc, DBG_LOAD, "single port device\n");
13831     }
13832 
13833     /* set the doorbell size */
13834     sc->doorbell_size = (1 << BXE_DB_SHIFT);
13835 
13836     /* determine whether the device is in 2 port or 4 port mode */
13837     sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
13838     if (CHIP_IS_E2E3(sc)) {
13839         /*
13840          * Read port4mode_en_ovwr[0]:
13841          *   If 1, four port mode is in port4mode_en_ovwr[1].
13842          *   If 0, four port mode is in port4mode_en[0].
13843          */
13844         val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
13845         if (val & 1) {
13846             val = ((val >> 1) & 1);
13847         } else {
13848             val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
13849         }
13850 
13851         sc->devinfo.chip_port_mode =
13852             (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
13853 
13854         BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
13855     }
13856 
13857     /* get the function and path info for the device */
13858     bxe_get_function_num(sc);
13859 
13860     /* get the shared memory base address */
13861     sc->devinfo.shmem_base     =
13862     sc->link_params.shmem_base =
13863         REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
13864     sc->devinfo.shmem2_base =
13865         REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
13866                                   MISC_REG_GENERIC_CR_0));
13867 
13868     BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
13869           sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
13870 
13871     if (!sc->devinfo.shmem_base) {
13872         /* this should ONLY prevent upcoming shmem reads */
13873         BLOGI(sc, "MCP not active\n");
13874         sc->flags |= BXE_NO_MCP_FLAG;
13875         return (0);
13876     }
13877 
13878     /* make sure the shared memory contents are valid */
13879     val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
13880     if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
13881         (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
13882         BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
13883         return (0);
13884     }
13885     BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
13886 
13887     /* get the bootcode version */
13888     sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
13889     snprintf(sc->devinfo.bc_ver_str,
13890              sizeof(sc->devinfo.bc_ver_str),
13891              "%d.%d.%d",
13892              ((sc->devinfo.bc_ver >> 24) & 0xff),
13893              ((sc->devinfo.bc_ver >> 16) & 0xff),
13894              ((sc->devinfo.bc_ver >>  8) & 0xff));
13895     BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
13896 
13897     /* get the bootcode shmem address */
13898     sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
13899     BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
13900 
13901     /* clean indirect addresses as they're not used */
13902     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
13903     if (IS_PF(sc)) {
13904         REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
13905         REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
13906         REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
13907         REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
13908         if (CHIP_IS_E1x(sc)) {
13909             REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
13910             REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
13911             REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
13912             REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
13913         }
13914 
13915         /*
13916          * Enable internal target-read (in case we are probed after PF
13917          * FLR). Must be done prior to any BAR read access. Only for
13918          * 57712 and up
13919          */
13920         if (!CHIP_IS_E1x(sc)) {
13921             REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
13922         }
13923     }
13924 
13925     /* get the nvram size */
13926     val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
13927     sc->devinfo.flash_size =
13928         (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
13929     BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
13930 
13931     /* get PCI capabilites */
13932     bxe_probe_pci_caps(sc);
13933 
13934     bxe_set_power_state(sc, PCI_PM_D0);
13935 
13936     /* get various configuration parameters from shmem */
13937     bxe_get_shmem_info(sc);
13938 
13939     if (sc->devinfo.pcie_msix_cap_reg != 0) {
13940         val = pci_read_config(sc->dev,
13941                               (sc->devinfo.pcie_msix_cap_reg +
13942                                PCIR_MSIX_CTRL),
13943                               2);
13944         sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
13945     } else {
13946         sc->igu_sb_cnt = 1;
13947     }
13948 
13949     sc->igu_base_addr = BAR_IGU_INTMEM;
13950 
13951     /* initialize IGU parameters */
13952     if (CHIP_IS_E1x(sc)) {
13953         sc->devinfo.int_block = INT_BLOCK_HC;
13954         sc->igu_dsb_id = DEF_SB_IGU_ID;
13955         sc->igu_base_sb = 0;
13956     } else {
13957         sc->devinfo.int_block = INT_BLOCK_IGU;
13958 
13959         /* do not allow device reset during IGU info preocessing */
13960         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13961 
13962         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
13963 
13964         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13965             int tout = 5000;
13966 
13967             BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
13968 
13969             val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
13970             REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
13971             REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
13972 
13973             while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13974                 tout--;
13975                 DELAY(1000);
13976             }
13977 
13978             if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
13979                 BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
13980                 bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13981                 return (-1);
13982             }
13983         }
13984 
13985         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
13986             BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
13987             sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
13988         } else {
13989             BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
13990         }
13991 
13992         rc = bxe_get_igu_cam_info(sc);
13993 
13994         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
13995 
13996         if (rc) {
13997             return (rc);
13998         }
13999     }
14000 
14001     /*
14002      * Get base FW non-default (fast path) status block ID. This value is
14003      * used to initialize the fw_sb_id saved on the fp/queue structure to
14004      * determine the id used by the FW.
14005      */
14006     if (CHIP_IS_E1x(sc)) {
14007         sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
14008     } else {
14009         /*
14010          * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
14011          * the same queue are indicated on the same IGU SB). So we prefer
14012          * FW and IGU SBs to be the same value.
14013          */
14014         sc->base_fw_ndsb = sc->igu_base_sb;
14015     }
14016 
14017     BLOGD(sc, DBG_LOAD,
14018           "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
14019           sc->igu_dsb_id, sc->igu_base_sb,
14020           sc->igu_sb_cnt, sc->base_fw_ndsb);
14021 
14022     elink_phy_probe(&sc->link_params);
14023 
14024     return (0);
14025 }
14026 
14027 static void
14028 bxe_link_settings_supported(struct bxe_softc *sc,
14029                             uint32_t         switch_cfg)
14030 {
14031     uint32_t cfg_size = 0;
14032     uint32_t idx;
14033     uint8_t port = SC_PORT(sc);
14034 
14035     /* aggregation of supported attributes of all external phys */
14036     sc->port.supported[0] = 0;
14037     sc->port.supported[1] = 0;
14038 
14039     switch (sc->link_params.num_phys) {
14040     case 1:
14041         sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
14042         cfg_size = 1;
14043         break;
14044     case 2:
14045         sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
14046         cfg_size = 1;
14047         break;
14048     case 3:
14049         if (sc->link_params.multi_phy_config &
14050             PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
14051             sc->port.supported[1] =
14052                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
14053             sc->port.supported[0] =
14054                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
14055         } else {
14056             sc->port.supported[0] =
14057                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
14058             sc->port.supported[1] =
14059                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
14060         }
14061         cfg_size = 2;
14062         break;
14063     }
14064 
14065     if (!(sc->port.supported[0] || sc->port.supported[1])) {
14066         BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
14067               SHMEM_RD(sc,
14068                        dev_info.port_hw_config[port].external_phy_config),
14069               SHMEM_RD(sc,
14070                        dev_info.port_hw_config[port].external_phy_config2));
14071         return;
14072     }
14073 
14074     if (CHIP_IS_E3(sc))
14075         sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
14076     else {
14077         switch (switch_cfg) {
14078         case ELINK_SWITCH_CFG_1G:
14079             sc->port.phy_addr =
14080                 REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
14081             break;
14082         case ELINK_SWITCH_CFG_10G:
14083             sc->port.phy_addr =
14084                 REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
14085             break;
14086         default:
14087             BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14088                   sc->port.link_config[0]);
14089             return;
14090         }
14091     }
14092 
14093     BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14094 
14095     /* mask what we support according to speed_cap_mask per configuration */
14096     for (idx = 0; idx < cfg_size; idx++) {
14097         if (!(sc->link_params.speed_cap_mask[idx] &
14098               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14099             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14100         }
14101 
14102         if (!(sc->link_params.speed_cap_mask[idx] &
14103               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14104             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14105         }
14106 
14107         if (!(sc->link_params.speed_cap_mask[idx] &
14108               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14109             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14110         }
14111 
14112         if (!(sc->link_params.speed_cap_mask[idx] &
14113               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14114             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14115         }
14116 
14117         if (!(sc->link_params.speed_cap_mask[idx] &
14118               PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14119             sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14120         }
14121 
14122         if (!(sc->link_params.speed_cap_mask[idx] &
14123               PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14124             sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14125         }
14126 
14127         if (!(sc->link_params.speed_cap_mask[idx] &
14128               PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14129             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14130         }
14131 
14132         if (!(sc->link_params.speed_cap_mask[idx] &
14133               PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14134             sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14135         }
14136     }
14137 
14138     BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14139           sc->port.supported[0], sc->port.supported[1]);
14140 }
14141 
14142 static void
14143 bxe_link_settings_requested(struct bxe_softc *sc)
14144 {
14145     uint32_t link_config;
14146     uint32_t idx;
14147     uint32_t cfg_size = 0;
14148 
14149     sc->port.advertising[0] = 0;
14150     sc->port.advertising[1] = 0;
14151 
14152     switch (sc->link_params.num_phys) {
14153     case 1:
14154     case 2:
14155         cfg_size = 1;
14156         break;
14157     case 3:
14158         cfg_size = 2;
14159         break;
14160     }
14161 
14162     for (idx = 0; idx < cfg_size; idx++) {
14163         sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14164         link_config = sc->port.link_config[idx];
14165 
14166         switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14167         case PORT_FEATURE_LINK_SPEED_AUTO:
14168             if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14169                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14170                 sc->port.advertising[idx] |= sc->port.supported[idx];
14171                 if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14172                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14173                     sc->port.advertising[idx] |=
14174                         (ELINK_SUPPORTED_100baseT_Half |
14175                          ELINK_SUPPORTED_100baseT_Full);
14176             } else {
14177                 /* force 10G, no AN */
14178                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14179                 sc->port.advertising[idx] |=
14180                     (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14181                 continue;
14182             }
14183             break;
14184 
14185         case PORT_FEATURE_LINK_SPEED_10M_FULL:
14186             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14187                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14188                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14189                                               ADVERTISED_TP);
14190             } else {
14191                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14192                           "speed_cap_mask=0x%08x\n",
14193                       link_config, sc->link_params.speed_cap_mask[idx]);
14194                 return;
14195             }
14196             break;
14197 
14198         case PORT_FEATURE_LINK_SPEED_10M_HALF:
14199             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14200                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14201                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14202                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14203                                               ADVERTISED_TP);
14204             } else {
14205                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14206                           "speed_cap_mask=0x%08x\n",
14207                       link_config, sc->link_params.speed_cap_mask[idx]);
14208                 return;
14209             }
14210             break;
14211 
14212         case PORT_FEATURE_LINK_SPEED_100M_FULL:
14213             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14214                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14215                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14216                                               ADVERTISED_TP);
14217             } else {
14218                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14219                           "speed_cap_mask=0x%08x\n",
14220                       link_config, sc->link_params.speed_cap_mask[idx]);
14221                 return;
14222             }
14223             break;
14224 
14225         case PORT_FEATURE_LINK_SPEED_100M_HALF:
14226             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14227                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14228                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14229                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14230                                               ADVERTISED_TP);
14231             } else {
14232                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14233                           "speed_cap_mask=0x%08x\n",
14234                       link_config, sc->link_params.speed_cap_mask[idx]);
14235                 return;
14236             }
14237             break;
14238 
14239         case PORT_FEATURE_LINK_SPEED_1G:
14240             if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14241                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14242                 sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14243                                               ADVERTISED_TP);
14244             } else {
14245                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14246                           "speed_cap_mask=0x%08x\n",
14247                       link_config, sc->link_params.speed_cap_mask[idx]);
14248                 return;
14249             }
14250             break;
14251 
14252         case PORT_FEATURE_LINK_SPEED_2_5G:
14253             if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14254                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14255                 sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14256                                               ADVERTISED_TP);
14257             } else {
14258                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14259                           "speed_cap_mask=0x%08x\n",
14260                       link_config, sc->link_params.speed_cap_mask[idx]);
14261                 return;
14262             }
14263             break;
14264 
14265         case PORT_FEATURE_LINK_SPEED_10G_CX4:
14266             if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14267                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14268                 sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14269                                               ADVERTISED_FIBRE);
14270             } else {
14271                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14272                           "speed_cap_mask=0x%08x\n",
14273                       link_config, sc->link_params.speed_cap_mask[idx]);
14274                 return;
14275             }
14276             break;
14277 
14278         case PORT_FEATURE_LINK_SPEED_20G:
14279             sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14280             break;
14281 
14282         default:
14283             BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14284                       "speed_cap_mask=0x%08x\n",
14285                   link_config, sc->link_params.speed_cap_mask[idx]);
14286             sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14287             sc->port.advertising[idx] = sc->port.supported[idx];
14288             break;
14289         }
14290 
14291         sc->link_params.req_flow_ctrl[idx] =
14292             (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14293 
14294         if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14295             if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14296                 sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14297             } else {
14298                 bxe_set_requested_fc(sc);
14299             }
14300         }
14301 
14302         BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14303                             "req_flow_ctrl=0x%x advertising=0x%x\n",
14304               sc->link_params.req_line_speed[idx],
14305               sc->link_params.req_duplex[idx],
14306               sc->link_params.req_flow_ctrl[idx],
14307               sc->port.advertising[idx]);
14308     }
14309 }
14310 
14311 static void
14312 bxe_get_phy_info(struct bxe_softc *sc)
14313 {
14314     uint8_t port = SC_PORT(sc);
14315     uint32_t config = sc->port.config;
14316     uint32_t eee_mode;
14317 
14318     /* shmem data already read in bxe_get_shmem_info() */
14319 
14320     BLOGD(sc, DBG_LOAD, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14321                         "link_config0=0x%08x\n",
14322                sc->link_params.lane_config,
14323                sc->link_params.speed_cap_mask[0],
14324                sc->port.link_config[0]);
14325 
14326     bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14327     bxe_link_settings_requested(sc);
14328 
14329     if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14330         sc->link_params.feature_config_flags |=
14331             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14332     } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14333         sc->link_params.feature_config_flags &=
14334             ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14335     } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14336         sc->link_params.feature_config_flags |=
14337             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14338     }
14339 
14340     /* configure link feature according to nvram value */
14341     eee_mode =
14342         (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14343           PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14344          PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14345     if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14346         sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14347                                     ELINK_EEE_MODE_ENABLE_LPI |
14348                                     ELINK_EEE_MODE_OUTPUT_TIME);
14349     } else {
14350         sc->link_params.eee_mode = 0;
14351     }
14352 
14353     /* get the media type */
14354     bxe_media_detect(sc);
14355 }
14356 
14357 static void
14358 bxe_get_params(struct bxe_softc *sc)
14359 {
14360     /* get user tunable params */
14361     bxe_get_tunable_params(sc);
14362 
14363     /* select the RX and TX ring sizes */
14364     sc->tx_ring_size = TX_BD_USABLE;
14365     sc->rx_ring_size = RX_BD_USABLE;
14366 
14367     /* XXX disable WoL */
14368     sc->wol = 0;
14369 }
14370 
14371 static void
14372 bxe_set_modes_bitmap(struct bxe_softc *sc)
14373 {
14374     uint32_t flags = 0;
14375 
14376     if (CHIP_REV_IS_FPGA(sc)) {
14377         SET_FLAGS(flags, MODE_FPGA);
14378     } else if (CHIP_REV_IS_EMUL(sc)) {
14379         SET_FLAGS(flags, MODE_EMUL);
14380     } else {
14381         SET_FLAGS(flags, MODE_ASIC);
14382     }
14383 
14384     if (CHIP_IS_MODE_4_PORT(sc)) {
14385         SET_FLAGS(flags, MODE_PORT4);
14386     } else {
14387         SET_FLAGS(flags, MODE_PORT2);
14388     }
14389 
14390     if (CHIP_IS_E2(sc)) {
14391         SET_FLAGS(flags, MODE_E2);
14392     } else if (CHIP_IS_E3(sc)) {
14393         SET_FLAGS(flags, MODE_E3);
14394         if (CHIP_REV(sc) == CHIP_REV_Ax) {
14395             SET_FLAGS(flags, MODE_E3_A0);
14396         } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14397             SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14398         }
14399     }
14400 
14401     if (IS_MF(sc)) {
14402         SET_FLAGS(flags, MODE_MF);
14403         switch (sc->devinfo.mf_info.mf_mode) {
14404         case MULTI_FUNCTION_SD:
14405             SET_FLAGS(flags, MODE_MF_SD);
14406             break;
14407         case MULTI_FUNCTION_SI:
14408             SET_FLAGS(flags, MODE_MF_SI);
14409             break;
14410         case MULTI_FUNCTION_AFEX:
14411             SET_FLAGS(flags, MODE_MF_AFEX);
14412             break;
14413         }
14414     } else {
14415         SET_FLAGS(flags, MODE_SF);
14416     }
14417 
14418 #if defined(__LITTLE_ENDIAN)
14419     SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14420 #else /* __BIG_ENDIAN */
14421     SET_FLAGS(flags, MODE_BIG_ENDIAN);
14422 #endif
14423 
14424     INIT_MODE_FLAGS(sc) = flags;
14425 }
14426 
14427 static int
14428 bxe_alloc_hsi_mem(struct bxe_softc *sc)
14429 {
14430     struct bxe_fastpath *fp;
14431     bus_addr_t busaddr;
14432     int max_agg_queues;
14433     int max_segments;
14434     bus_size_t max_size;
14435     bus_size_t max_seg_size;
14436     char buf[32];
14437     int rc;
14438     int i, j;
14439 
14440     /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14441 
14442     /* allocate the parent bus DMA tag */
14443     rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14444                             1,                        /* alignment */
14445                             0,                        /* boundary limit */
14446                             BUS_SPACE_MAXADDR,        /* restricted low */
14447                             BUS_SPACE_MAXADDR,        /* restricted hi */
14448                             NULL,                     /* addr filter() */
14449                             NULL,                     /* addr filter() arg */
14450                             BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14451                             BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14452                             BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14453                             0,                        /* flags */
14454                             NULL,                     /* lock() */
14455                             NULL,                     /* lock() arg */
14456                             &sc->parent_dma_tag);     /* returned dma tag */
14457     if (rc != 0) {
14458         BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14459         return (1);
14460     }
14461 
14462     /************************/
14463     /* DEFAULT STATUS BLOCK */
14464     /************************/
14465 
14466     if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14467                       &sc->def_sb_dma, "default status block") != 0) {
14468         /* XXX */
14469         bus_dma_tag_destroy(sc->parent_dma_tag);
14470         return (1);
14471     }
14472 
14473     sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14474 
14475     /***************/
14476     /* EVENT QUEUE */
14477     /***************/
14478 
14479     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14480                       &sc->eq_dma, "event queue") != 0) {
14481         /* XXX */
14482         bxe_dma_free(sc, &sc->def_sb_dma);
14483         sc->def_sb = NULL;
14484         bus_dma_tag_destroy(sc->parent_dma_tag);
14485         return (1);
14486     }
14487 
14488     sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14489 
14490     /*************/
14491     /* SLOW PATH */
14492     /*************/
14493 
14494     if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14495                       &sc->sp_dma, "slow path") != 0) {
14496         /* XXX */
14497         bxe_dma_free(sc, &sc->eq_dma);
14498         sc->eq = NULL;
14499         bxe_dma_free(sc, &sc->def_sb_dma);
14500         sc->def_sb = NULL;
14501         bus_dma_tag_destroy(sc->parent_dma_tag);
14502         return (1);
14503     }
14504 
14505     sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14506 
14507     /*******************/
14508     /* SLOW PATH QUEUE */
14509     /*******************/
14510 
14511     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14512                       &sc->spq_dma, "slow path queue") != 0) {
14513         /* XXX */
14514         bxe_dma_free(sc, &sc->sp_dma);
14515         sc->sp = NULL;
14516         bxe_dma_free(sc, &sc->eq_dma);
14517         sc->eq = NULL;
14518         bxe_dma_free(sc, &sc->def_sb_dma);
14519         sc->def_sb = NULL;
14520         bus_dma_tag_destroy(sc->parent_dma_tag);
14521         return (1);
14522     }
14523 
14524     sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14525 
14526     /***************************/
14527     /* FW DECOMPRESSION BUFFER */
14528     /***************************/
14529 
14530     if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14531                       "fw decompression buffer") != 0) {
14532         /* XXX */
14533         bxe_dma_free(sc, &sc->spq_dma);
14534         sc->spq = NULL;
14535         bxe_dma_free(sc, &sc->sp_dma);
14536         sc->sp = NULL;
14537         bxe_dma_free(sc, &sc->eq_dma);
14538         sc->eq = NULL;
14539         bxe_dma_free(sc, &sc->def_sb_dma);
14540         sc->def_sb = NULL;
14541         bus_dma_tag_destroy(sc->parent_dma_tag);
14542         return (1);
14543     }
14544 
14545     sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14546 
14547     if ((sc->gz_strm =
14548          malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14549         /* XXX */
14550         bxe_dma_free(sc, &sc->gz_buf_dma);
14551         sc->gz_buf = NULL;
14552         bxe_dma_free(sc, &sc->spq_dma);
14553         sc->spq = NULL;
14554         bxe_dma_free(sc, &sc->sp_dma);
14555         sc->sp = NULL;
14556         bxe_dma_free(sc, &sc->eq_dma);
14557         sc->eq = NULL;
14558         bxe_dma_free(sc, &sc->def_sb_dma);
14559         sc->def_sb = NULL;
14560         bus_dma_tag_destroy(sc->parent_dma_tag);
14561         return (1);
14562     }
14563 
14564     /*************/
14565     /* FASTPATHS */
14566     /*************/
14567 
14568     /* allocate DMA memory for each fastpath structure */
14569     for (i = 0; i < sc->num_queues; i++) {
14570         fp = &sc->fp[i];
14571         fp->sc    = sc;
14572         fp->index = i;
14573 
14574         /*******************/
14575         /* FP STATUS BLOCK */
14576         /*******************/
14577 
14578         snprintf(buf, sizeof(buf), "fp %d status block", i);
14579         if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14580                           &fp->sb_dma, buf) != 0) {
14581             /* XXX unwind and free previous fastpath allocations */
14582             BLOGE(sc, "Failed to alloc %s\n", buf);
14583             return (1);
14584         } else {
14585             if (CHIP_IS_E2E3(sc)) {
14586                 fp->status_block.e2_sb =
14587                     (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14588             } else {
14589                 fp->status_block.e1x_sb =
14590                     (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14591             }
14592         }
14593 
14594         /******************/
14595         /* FP TX BD CHAIN */
14596         /******************/
14597 
14598         snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14599         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14600                           &fp->tx_dma, buf) != 0) {
14601             /* XXX unwind and free previous fastpath allocations */
14602             BLOGE(sc, "Failed to alloc %s\n", buf);
14603             return (1);
14604         } else {
14605             fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14606         }
14607 
14608         /* link together the tx bd chain pages */
14609         for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14610             /* index into the tx bd chain array to last entry per page */
14611             struct eth_tx_next_bd *tx_next_bd =
14612                 &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14613             /* point to the next page and wrap from last page */
14614             busaddr = (fp->tx_dma.paddr +
14615                        (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14616             tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14617             tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14618         }
14619 
14620         /******************/
14621         /* FP RX BD CHAIN */
14622         /******************/
14623 
14624         snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14625         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14626                           &fp->rx_dma, buf) != 0) {
14627             /* XXX unwind and free previous fastpath allocations */
14628             BLOGE(sc, "Failed to alloc %s\n", buf);
14629             return (1);
14630         } else {
14631             fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14632         }
14633 
14634         /* link together the rx bd chain pages */
14635         for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14636             /* index into the rx bd chain array to last entry per page */
14637             struct eth_rx_bd *rx_bd =
14638                 &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14639             /* point to the next page and wrap from last page */
14640             busaddr = (fp->rx_dma.paddr +
14641                        (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14642             rx_bd->addr_hi = htole32(U64_HI(busaddr));
14643             rx_bd->addr_lo = htole32(U64_LO(busaddr));
14644         }
14645 
14646         /*******************/
14647         /* FP RX RCQ CHAIN */
14648         /*******************/
14649 
14650         snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14651         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14652                           &fp->rcq_dma, buf) != 0) {
14653             /* XXX unwind and free previous fastpath allocations */
14654             BLOGE(sc, "Failed to alloc %s\n", buf);
14655             return (1);
14656         } else {
14657             fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14658         }
14659 
14660         /* link together the rcq chain pages */
14661         for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14662             /* index into the rcq chain array to last entry per page */
14663             struct eth_rx_cqe_next_page *rx_cqe_next =
14664                 (struct eth_rx_cqe_next_page *)
14665                 &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14666             /* point to the next page and wrap from last page */
14667             busaddr = (fp->rcq_dma.paddr +
14668                        (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14669             rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14670             rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14671         }
14672 
14673         /*******************/
14674         /* FP RX SGE CHAIN */
14675         /*******************/
14676 
14677         snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14678         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14679                           &fp->rx_sge_dma, buf) != 0) {
14680             /* XXX unwind and free previous fastpath allocations */
14681             BLOGE(sc, "Failed to alloc %s\n", buf);
14682             return (1);
14683         } else {
14684             fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14685         }
14686 
14687         /* link together the sge chain pages */
14688         for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14689             /* index into the rcq chain array to last entry per page */
14690             struct eth_rx_sge *rx_sge =
14691                 &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14692             /* point to the next page and wrap from last page */
14693             busaddr = (fp->rx_sge_dma.paddr +
14694                        (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14695             rx_sge->addr_hi = htole32(U64_HI(busaddr));
14696             rx_sge->addr_lo = htole32(U64_LO(busaddr));
14697         }
14698 
14699         /***********************/
14700         /* FP TX MBUF DMA MAPS */
14701         /***********************/
14702 
14703         /* set required sizes before mapping to conserve resources */
14704         if (if_getcapenable(sc->ifp) & (IFCAP_TSO4 | IFCAP_TSO6)) {
14705             max_size     = BXE_TSO_MAX_SIZE;
14706             max_segments = BXE_TSO_MAX_SEGMENTS;
14707             max_seg_size = BXE_TSO_MAX_SEG_SIZE;
14708         } else {
14709             max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
14710             max_segments = BXE_MAX_SEGMENTS;
14711             max_seg_size = MCLBYTES;
14712         }
14713 
14714         /* create a dma tag for the tx mbufs */
14715         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14716                                 1,                  /* alignment */
14717                                 0,                  /* boundary limit */
14718                                 BUS_SPACE_MAXADDR,  /* restricted low */
14719                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14720                                 NULL,               /* addr filter() */
14721                                 NULL,               /* addr filter() arg */
14722                                 max_size,           /* max map size */
14723                                 max_segments,       /* num discontinuous */
14724                                 max_seg_size,       /* max seg size */
14725                                 0,                  /* flags */
14726                                 NULL,               /* lock() */
14727                                 NULL,               /* lock() arg */
14728                                 &fp->tx_mbuf_tag);  /* returned dma tag */
14729         if (rc != 0) {
14730             /* XXX unwind and free previous fastpath allocations */
14731             BLOGE(sc, "Failed to create dma tag for "
14732                       "'fp %d tx mbufs' (%d)\n", i, rc);
14733             return (1);
14734         }
14735 
14736         /* create dma maps for each of the tx mbuf clusters */
14737         for (j = 0; j < TX_BD_TOTAL; j++) {
14738             if (bus_dmamap_create(fp->tx_mbuf_tag,
14739                                   BUS_DMA_NOWAIT,
14740                                   &fp->tx_mbuf_chain[j].m_map)) {
14741                 /* XXX unwind and free previous fastpath allocations */
14742                 BLOGE(sc, "Failed to create dma map for "
14743                           "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
14744                 return (1);
14745             }
14746         }
14747 
14748         /***********************/
14749         /* FP RX MBUF DMA MAPS */
14750         /***********************/
14751 
14752         /* create a dma tag for the rx mbufs */
14753         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14754                                 1,                  /* alignment */
14755                                 0,                  /* boundary limit */
14756                                 BUS_SPACE_MAXADDR,  /* restricted low */
14757                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14758                                 NULL,               /* addr filter() */
14759                                 NULL,               /* addr filter() arg */
14760                                 MJUM9BYTES,         /* max map size */
14761                                 1,                  /* num discontinuous */
14762                                 MJUM9BYTES,         /* max seg size */
14763                                 0,                  /* flags */
14764                                 NULL,               /* lock() */
14765                                 NULL,               /* lock() arg */
14766                                 &fp->rx_mbuf_tag);  /* returned dma tag */
14767         if (rc != 0) {
14768             /* XXX unwind and free previous fastpath allocations */
14769             BLOGE(sc, "Failed to create dma tag for "
14770                       "'fp %d rx mbufs' (%d)\n", i, rc);
14771             return (1);
14772         }
14773 
14774         /* create dma maps for each of the rx mbuf clusters */
14775         for (j = 0; j < RX_BD_TOTAL; j++) {
14776             if (bus_dmamap_create(fp->rx_mbuf_tag,
14777                                   BUS_DMA_NOWAIT,
14778                                   &fp->rx_mbuf_chain[j].m_map)) {
14779                 /* XXX unwind and free previous fastpath allocations */
14780                 BLOGE(sc, "Failed to create dma map for "
14781                           "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
14782                 return (1);
14783             }
14784         }
14785 
14786         /* create dma map for the spare rx mbuf cluster */
14787         if (bus_dmamap_create(fp->rx_mbuf_tag,
14788                               BUS_DMA_NOWAIT,
14789                               &fp->rx_mbuf_spare_map)) {
14790             /* XXX unwind and free previous fastpath allocations */
14791             BLOGE(sc, "Failed to create dma map for "
14792                       "'fp %d spare rx mbuf' (%d)\n", i, rc);
14793             return (1);
14794         }
14795 
14796         /***************************/
14797         /* FP RX SGE MBUF DMA MAPS */
14798         /***************************/
14799 
14800         /* create a dma tag for the rx sge mbufs */
14801         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
14802                                 1,                  /* alignment */
14803                                 0,                  /* boundary limit */
14804                                 BUS_SPACE_MAXADDR,  /* restricted low */
14805                                 BUS_SPACE_MAXADDR,  /* restricted hi */
14806                                 NULL,               /* addr filter() */
14807                                 NULL,               /* addr filter() arg */
14808                                 BCM_PAGE_SIZE,      /* max map size */
14809                                 1,                  /* num discontinuous */
14810                                 BCM_PAGE_SIZE,      /* max seg size */
14811                                 0,                  /* flags */
14812                                 NULL,               /* lock() */
14813                                 NULL,               /* lock() arg */
14814                                 &fp->rx_sge_mbuf_tag); /* returned dma tag */
14815         if (rc != 0) {
14816             /* XXX unwind and free previous fastpath allocations */
14817             BLOGE(sc, "Failed to create dma tag for "
14818                       "'fp %d rx sge mbufs' (%d)\n", i, rc);
14819             return (1);
14820         }
14821 
14822         /* create dma maps for the rx sge mbuf clusters */
14823         for (j = 0; j < RX_SGE_TOTAL; j++) {
14824             if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14825                                   BUS_DMA_NOWAIT,
14826                                   &fp->rx_sge_mbuf_chain[j].m_map)) {
14827                 /* XXX unwind and free previous fastpath allocations */
14828                 BLOGE(sc, "Failed to create dma map for "
14829                           "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
14830                 return (1);
14831             }
14832         }
14833 
14834         /* create dma map for the spare rx sge mbuf cluster */
14835         if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
14836                               BUS_DMA_NOWAIT,
14837                               &fp->rx_sge_mbuf_spare_map)) {
14838             /* XXX unwind and free previous fastpath allocations */
14839             BLOGE(sc, "Failed to create dma map for "
14840                       "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
14841             return (1);
14842         }
14843 
14844         /***************************/
14845         /* FP RX TPA MBUF DMA MAPS */
14846         /***************************/
14847 
14848         /* create dma maps for the rx tpa mbuf clusters */
14849         max_agg_queues = MAX_AGG_QS(sc);
14850 
14851         for (j = 0; j < max_agg_queues; j++) {
14852             if (bus_dmamap_create(fp->rx_mbuf_tag,
14853                                   BUS_DMA_NOWAIT,
14854                                   &fp->rx_tpa_info[j].bd.m_map)) {
14855                 /* XXX unwind and free previous fastpath allocations */
14856                 BLOGE(sc, "Failed to create dma map for "
14857                           "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
14858                 return (1);
14859             }
14860         }
14861 
14862         /* create dma map for the spare rx tpa mbuf cluster */
14863         if (bus_dmamap_create(fp->rx_mbuf_tag,
14864                               BUS_DMA_NOWAIT,
14865                               &fp->rx_tpa_info_mbuf_spare_map)) {
14866             /* XXX unwind and free previous fastpath allocations */
14867             BLOGE(sc, "Failed to create dma map for "
14868                       "'fp %d spare rx tpa mbuf' (%d)\n", i, rc);
14869             return (1);
14870         }
14871 
14872         bxe_init_sge_ring_bit_mask(fp);
14873     }
14874 
14875     return (0);
14876 }
14877 
14878 static void
14879 bxe_free_hsi_mem(struct bxe_softc *sc)
14880 {
14881     struct bxe_fastpath *fp;
14882     int max_agg_queues;
14883     int i, j;
14884 
14885     if (sc->parent_dma_tag == NULL) {
14886         return; /* assume nothing was allocated */
14887     }
14888 
14889     for (i = 0; i < sc->num_queues; i++) {
14890         fp = &sc->fp[i];
14891 
14892         /*******************/
14893         /* FP STATUS BLOCK */
14894         /*******************/
14895 
14896         bxe_dma_free(sc, &fp->sb_dma);
14897         memset(&fp->status_block, 0, sizeof(fp->status_block));
14898 
14899         /******************/
14900         /* FP TX BD CHAIN */
14901         /******************/
14902 
14903         bxe_dma_free(sc, &fp->tx_dma);
14904         fp->tx_chain = NULL;
14905 
14906         /******************/
14907         /* FP RX BD CHAIN */
14908         /******************/
14909 
14910         bxe_dma_free(sc, &fp->rx_dma);
14911         fp->rx_chain = NULL;
14912 
14913         /*******************/
14914         /* FP RX RCQ CHAIN */
14915         /*******************/
14916 
14917         bxe_dma_free(sc, &fp->rcq_dma);
14918         fp->rcq_chain = NULL;
14919 
14920         /*******************/
14921         /* FP RX SGE CHAIN */
14922         /*******************/
14923 
14924         bxe_dma_free(sc, &fp->rx_sge_dma);
14925         fp->rx_sge_chain = NULL;
14926 
14927         /***********************/
14928         /* FP TX MBUF DMA MAPS */
14929         /***********************/
14930 
14931         if (fp->tx_mbuf_tag != NULL) {
14932             for (j = 0; j < TX_BD_TOTAL; j++) {
14933                 if (fp->tx_mbuf_chain[j].m_map != NULL) {
14934                     bus_dmamap_unload(fp->tx_mbuf_tag,
14935                                       fp->tx_mbuf_chain[j].m_map);
14936                     bus_dmamap_destroy(fp->tx_mbuf_tag,
14937                                        fp->tx_mbuf_chain[j].m_map);
14938                 }
14939             }
14940 
14941             bus_dma_tag_destroy(fp->tx_mbuf_tag);
14942             fp->tx_mbuf_tag = NULL;
14943         }
14944 
14945         /***********************/
14946         /* FP RX MBUF DMA MAPS */
14947         /***********************/
14948 
14949         if (fp->rx_mbuf_tag != NULL) {
14950             for (j = 0; j < RX_BD_TOTAL; j++) {
14951                 if (fp->rx_mbuf_chain[j].m_map != NULL) {
14952                     bus_dmamap_unload(fp->rx_mbuf_tag,
14953                                       fp->rx_mbuf_chain[j].m_map);
14954                     bus_dmamap_destroy(fp->rx_mbuf_tag,
14955                                        fp->rx_mbuf_chain[j].m_map);
14956                 }
14957             }
14958 
14959             if (fp->rx_mbuf_spare_map != NULL) {
14960                 bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14961                 bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
14962             }
14963 
14964             /***************************/
14965             /* FP RX TPA MBUF DMA MAPS */
14966             /***************************/
14967 
14968             max_agg_queues = MAX_AGG_QS(sc);
14969 
14970             for (j = 0; j < max_agg_queues; j++) {
14971                 if (fp->rx_tpa_info[j].bd.m_map != NULL) {
14972                     bus_dmamap_unload(fp->rx_mbuf_tag,
14973                                       fp->rx_tpa_info[j].bd.m_map);
14974                     bus_dmamap_destroy(fp->rx_mbuf_tag,
14975                                        fp->rx_tpa_info[j].bd.m_map);
14976                 }
14977             }
14978 
14979             if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
14980                 bus_dmamap_unload(fp->rx_mbuf_tag,
14981                                   fp->rx_tpa_info_mbuf_spare_map);
14982                 bus_dmamap_destroy(fp->rx_mbuf_tag,
14983                                    fp->rx_tpa_info_mbuf_spare_map);
14984             }
14985 
14986             bus_dma_tag_destroy(fp->rx_mbuf_tag);
14987             fp->rx_mbuf_tag = NULL;
14988         }
14989 
14990         /***************************/
14991         /* FP RX SGE MBUF DMA MAPS */
14992         /***************************/
14993 
14994         if (fp->rx_sge_mbuf_tag != NULL) {
14995             for (j = 0; j < RX_SGE_TOTAL; j++) {
14996                 if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
14997                     bus_dmamap_unload(fp->rx_sge_mbuf_tag,
14998                                       fp->rx_sge_mbuf_chain[j].m_map);
14999                     bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15000                                        fp->rx_sge_mbuf_chain[j].m_map);
15001                 }
15002             }
15003 
15004             if (fp->rx_sge_mbuf_spare_map != NULL) {
15005                 bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15006                                   fp->rx_sge_mbuf_spare_map);
15007                 bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15008                                    fp->rx_sge_mbuf_spare_map);
15009             }
15010 
15011             bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
15012             fp->rx_sge_mbuf_tag = NULL;
15013         }
15014     }
15015 
15016     /***************************/
15017     /* FW DECOMPRESSION BUFFER */
15018     /***************************/
15019 
15020     bxe_dma_free(sc, &sc->gz_buf_dma);
15021     sc->gz_buf = NULL;
15022     free(sc->gz_strm, M_DEVBUF);
15023     sc->gz_strm = NULL;
15024 
15025     /*******************/
15026     /* SLOW PATH QUEUE */
15027     /*******************/
15028 
15029     bxe_dma_free(sc, &sc->spq_dma);
15030     sc->spq = NULL;
15031 
15032     /*************/
15033     /* SLOW PATH */
15034     /*************/
15035 
15036     bxe_dma_free(sc, &sc->sp_dma);
15037     sc->sp = NULL;
15038 
15039     /***************/
15040     /* EVENT QUEUE */
15041     /***************/
15042 
15043     bxe_dma_free(sc, &sc->eq_dma);
15044     sc->eq = NULL;
15045 
15046     /************************/
15047     /* DEFAULT STATUS BLOCK */
15048     /************************/
15049 
15050     bxe_dma_free(sc, &sc->def_sb_dma);
15051     sc->def_sb = NULL;
15052 
15053     bus_dma_tag_destroy(sc->parent_dma_tag);
15054     sc->parent_dma_tag = NULL;
15055 }
15056 
15057 /*
15058  * Previous driver DMAE transaction may have occurred when pre-boot stage
15059  * ended and boot began. This would invalidate the addresses of the
15060  * transaction, resulting in was-error bit set in the PCI causing all
15061  * hw-to-host PCIe transactions to timeout. If this happened we want to clear
15062  * the interrupt which detected this from the pglueb and the was-done bit
15063  */
15064 static void
15065 bxe_prev_interrupted_dmae(struct bxe_softc *sc)
15066 {
15067     uint32_t val;
15068 
15069     if (!CHIP_IS_E1x(sc)) {
15070         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
15071         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
15072             BLOGD(sc, DBG_LOAD,
15073                   "Clearing 'was-error' bit that was set in pglueb");
15074             REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
15075         }
15076     }
15077 }
15078 
15079 static int
15080 bxe_prev_mcp_done(struct bxe_softc *sc)
15081 {
15082     uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15083                                  DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15084     if (!rc) {
15085         BLOGE(sc, "MCP response failure, aborting\n");
15086         return (-1);
15087     }
15088 
15089     return (0);
15090 }
15091 
15092 static struct bxe_prev_list_node *
15093 bxe_prev_path_get_entry(struct bxe_softc *sc)
15094 {
15095     struct bxe_prev_list_node *tmp;
15096 
15097     LIST_FOREACH(tmp, &bxe_prev_list, node) {
15098         if ((sc->pcie_bus == tmp->bus) &&
15099             (sc->pcie_device == tmp->slot) &&
15100             (SC_PATH(sc) == tmp->path)) {
15101             return (tmp);
15102         }
15103     }
15104 
15105     return (NULL);
15106 }
15107 
15108 static uint8_t
15109 bxe_prev_is_path_marked(struct bxe_softc *sc)
15110 {
15111     struct bxe_prev_list_node *tmp;
15112     int rc = FALSE;
15113 
15114     mtx_lock(&bxe_prev_mtx);
15115 
15116     tmp = bxe_prev_path_get_entry(sc);
15117     if (tmp) {
15118         if (tmp->aer) {
15119             BLOGD(sc, DBG_LOAD,
15120                   "Path %d/%d/%d was marked by AER\n",
15121                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15122         } else {
15123             rc = TRUE;
15124             BLOGD(sc, DBG_LOAD,
15125                   "Path %d/%d/%d was already cleaned from previous drivers\n",
15126                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15127         }
15128     }
15129 
15130     mtx_unlock(&bxe_prev_mtx);
15131 
15132     return (rc);
15133 }
15134 
15135 static int
15136 bxe_prev_mark_path(struct bxe_softc *sc,
15137                    uint8_t          after_undi)
15138 {
15139     struct bxe_prev_list_node *tmp;
15140 
15141     mtx_lock(&bxe_prev_mtx);
15142 
15143     /* Check whether the entry for this path already exists */
15144     tmp = bxe_prev_path_get_entry(sc);
15145     if (tmp) {
15146         if (!tmp->aer) {
15147             BLOGD(sc, DBG_LOAD,
15148                   "Re-marking AER in path %d/%d/%d\n",
15149                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15150         } else {
15151             BLOGD(sc, DBG_LOAD,
15152                   "Removing AER indication from path %d/%d/%d\n",
15153                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15154             tmp->aer = 0;
15155         }
15156 
15157         mtx_unlock(&bxe_prev_mtx);
15158         return (0);
15159     }
15160 
15161     mtx_unlock(&bxe_prev_mtx);
15162 
15163     /* Create an entry for this path and add it */
15164     tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15165                  (M_NOWAIT | M_ZERO));
15166     if (!tmp) {
15167         BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15168         return (-1);
15169     }
15170 
15171     tmp->bus  = sc->pcie_bus;
15172     tmp->slot = sc->pcie_device;
15173     tmp->path = SC_PATH(sc);
15174     tmp->aer  = 0;
15175     tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15176 
15177     mtx_lock(&bxe_prev_mtx);
15178 
15179     BLOGD(sc, DBG_LOAD,
15180           "Marked path %d/%d/%d - finished previous unload\n",
15181           sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15182     LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15183 
15184     mtx_unlock(&bxe_prev_mtx);
15185 
15186     return (0);
15187 }
15188 
15189 static int
15190 bxe_do_flr(struct bxe_softc *sc)
15191 {
15192     int i;
15193 
15194     /* only E2 and onwards support FLR */
15195     if (CHIP_IS_E1x(sc)) {
15196         BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15197         return (-1);
15198     }
15199 
15200     /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15201     if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15202         BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15203               sc->devinfo.bc_ver);
15204         return (-1);
15205     }
15206 
15207     /* Wait for Transaction Pending bit clean */
15208     for (i = 0; i < 4; i++) {
15209         if (i) {
15210             DELAY(((1 << (i - 1)) * 100) * 1000);
15211         }
15212 
15213         if (!bxe_is_pcie_pending(sc)) {
15214             goto clear;
15215         }
15216     }
15217 
15218     BLOGE(sc, "PCIE transaction is not cleared, "
15219               "proceeding with reset anyway\n");
15220 
15221 clear:
15222 
15223     BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15224     bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15225 
15226     return (0);
15227 }
15228 
15229 struct bxe_mac_vals {
15230     uint32_t xmac_addr;
15231     uint32_t xmac_val;
15232     uint32_t emac_addr;
15233     uint32_t emac_val;
15234     uint32_t umac_addr;
15235     uint32_t umac_val;
15236     uint32_t bmac_addr;
15237     uint32_t bmac_val[2];
15238 };
15239 
15240 static void
15241 bxe_prev_unload_close_mac(struct bxe_softc *sc,
15242                           struct bxe_mac_vals *vals)
15243 {
15244     uint32_t val, base_addr, offset, mask, reset_reg;
15245     uint8_t mac_stopped = FALSE;
15246     uint8_t port = SC_PORT(sc);
15247     uint32_t wb_data[2];
15248 
15249     /* reset addresses as they also mark which values were changed */
15250     vals->bmac_addr = 0;
15251     vals->umac_addr = 0;
15252     vals->xmac_addr = 0;
15253     vals->emac_addr = 0;
15254 
15255     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15256 
15257     if (!CHIP_IS_E3(sc)) {
15258         val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15259         mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15260         if ((mask & reset_reg) && val) {
15261             BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15262             base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15263                                     : NIG_REG_INGRESS_BMAC0_MEM;
15264             offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15265                                     : BIGMAC_REGISTER_BMAC_CONTROL;
15266 
15267             /*
15268              * use rd/wr since we cannot use dmae. This is safe
15269              * since MCP won't access the bus due to the request
15270              * to unload, and no function on the path can be
15271              * loaded at this time.
15272              */
15273             wb_data[0] = REG_RD(sc, base_addr + offset);
15274             wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15275             vals->bmac_addr = base_addr + offset;
15276             vals->bmac_val[0] = wb_data[0];
15277             vals->bmac_val[1] = wb_data[1];
15278             wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15279             REG_WR(sc, vals->bmac_addr, wb_data[0]);
15280             REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15281         }
15282 
15283         BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15284         vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15285         vals->emac_val = REG_RD(sc, vals->emac_addr);
15286         REG_WR(sc, vals->emac_addr, 0);
15287         mac_stopped = TRUE;
15288     } else {
15289         if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15290             BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15291             base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15292             val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15293             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15294             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15295             vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15296             vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15297             REG_WR(sc, vals->xmac_addr, 0);
15298             mac_stopped = TRUE;
15299         }
15300 
15301         mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15302         if (mask & reset_reg) {
15303             BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15304             base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15305             vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15306             vals->umac_val = REG_RD(sc, vals->umac_addr);
15307             REG_WR(sc, vals->umac_addr, 0);
15308             mac_stopped = TRUE;
15309         }
15310     }
15311 
15312     if (mac_stopped) {
15313         DELAY(20000);
15314     }
15315 }
15316 
15317 #define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15318 #define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15319 #define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15320 #define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15321 
15322 static void
15323 bxe_prev_unload_undi_inc(struct bxe_softc *sc,
15324                          uint8_t          port,
15325                          uint8_t          inc)
15326 {
15327     uint16_t rcq, bd;
15328     uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15329 
15330     rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15331     bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15332 
15333     tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15334     REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15335 
15336     BLOGD(sc, DBG_LOAD,
15337           "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15338           port, bd, rcq);
15339 }
15340 
15341 static int
15342 bxe_prev_unload_common(struct bxe_softc *sc)
15343 {
15344     uint32_t reset_reg, tmp_reg = 0, rc;
15345     uint8_t prev_undi = FALSE;
15346     struct bxe_mac_vals mac_vals;
15347     uint32_t timer_count = 1000;
15348     uint32_t prev_brb;
15349 
15350     /*
15351      * It is possible a previous function received 'common' answer,
15352      * but hasn't loaded yet, therefore creating a scenario of
15353      * multiple functions receiving 'common' on the same path.
15354      */
15355     BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15356 
15357     memset(&mac_vals, 0, sizeof(mac_vals));
15358 
15359     if (bxe_prev_is_path_marked(sc)) {
15360         return (bxe_prev_mcp_done(sc));
15361     }
15362 
15363     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15364 
15365     /* Reset should be performed after BRB is emptied */
15366     if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15367         /* Close the MAC Rx to prevent BRB from filling up */
15368         bxe_prev_unload_close_mac(sc, &mac_vals);
15369 
15370         /* close LLH filters towards the BRB */
15371         elink_set_rx_filter(&sc->link_params, 0);
15372 
15373         /*
15374          * Check if the UNDI driver was previously loaded.
15375          * UNDI driver initializes CID offset for normal bell to 0x7
15376          */
15377         if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15378             tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15379             if (tmp_reg == 0x7) {
15380                 BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15381                 prev_undi = TRUE;
15382                 /* clear the UNDI indication */
15383                 REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15384                 /* clear possible idle check errors */
15385                 REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15386             }
15387         }
15388 
15389         /* wait until BRB is empty */
15390         tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15391         while (timer_count) {
15392             prev_brb = tmp_reg;
15393 
15394             tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15395             if (!tmp_reg) {
15396                 break;
15397             }
15398 
15399             BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15400 
15401             /* reset timer as long as BRB actually gets emptied */
15402             if (prev_brb > tmp_reg) {
15403                 timer_count = 1000;
15404             } else {
15405                 timer_count--;
15406             }
15407 
15408             /* If UNDI resides in memory, manually increment it */
15409             if (prev_undi) {
15410                 bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15411             }
15412 
15413             DELAY(10);
15414         }
15415 
15416         if (!timer_count) {
15417             BLOGE(sc, "Failed to empty BRB\n");
15418         }
15419     }
15420 
15421     /* No packets are in the pipeline, path is ready for reset */
15422     bxe_reset_common(sc);
15423 
15424     if (mac_vals.xmac_addr) {
15425         REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15426     }
15427     if (mac_vals.umac_addr) {
15428         REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15429     }
15430     if (mac_vals.emac_addr) {
15431         REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15432     }
15433     if (mac_vals.bmac_addr) {
15434         REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15435         REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15436     }
15437 
15438     rc = bxe_prev_mark_path(sc, prev_undi);
15439     if (rc) {
15440         bxe_prev_mcp_done(sc);
15441         return (rc);
15442     }
15443 
15444     return (bxe_prev_mcp_done(sc));
15445 }
15446 
15447 static int
15448 bxe_prev_unload_uncommon(struct bxe_softc *sc)
15449 {
15450     int rc;
15451 
15452     BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15453 
15454     /* Test if previous unload process was already finished for this path */
15455     if (bxe_prev_is_path_marked(sc)) {
15456         return (bxe_prev_mcp_done(sc));
15457     }
15458 
15459     BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15460 
15461     /*
15462      * If function has FLR capabilities, and existing FW version matches
15463      * the one required, then FLR will be sufficient to clean any residue
15464      * left by previous driver
15465      */
15466     rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15467     if (!rc) {
15468         /* fw version is good */
15469         BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15470         rc = bxe_do_flr(sc);
15471     }
15472 
15473     if (!rc) {
15474         /* FLR was performed */
15475         BLOGD(sc, DBG_LOAD, "FLR successful\n");
15476         return (0);
15477     }
15478 
15479     BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15480 
15481     /* Close the MCP request, return failure*/
15482     rc = bxe_prev_mcp_done(sc);
15483     if (!rc) {
15484         rc = BXE_PREV_WAIT_NEEDED;
15485     }
15486 
15487     return (rc);
15488 }
15489 
15490 static int
15491 bxe_prev_unload(struct bxe_softc *sc)
15492 {
15493     int time_counter = 10;
15494     uint32_t fw, hw_lock_reg, hw_lock_val;
15495     uint32_t rc = 0;
15496 
15497     /*
15498      * Clear HW from errors which may have resulted from an interrupted
15499      * DMAE transaction.
15500      */
15501     bxe_prev_interrupted_dmae(sc);
15502 
15503     /* Release previously held locks */
15504     hw_lock_reg =
15505         (SC_FUNC(sc) <= 5) ?
15506             (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15507             (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15508 
15509     hw_lock_val = (REG_RD(sc, hw_lock_reg));
15510     if (hw_lock_val) {
15511         if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15512             BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15513             REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15514                    (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15515         }
15516         BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15517         REG_WR(sc, hw_lock_reg, 0xffffffff);
15518     } else {
15519         BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15520     }
15521 
15522     if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15523         BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15524         REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15525     }
15526 
15527     do {
15528         /* Lock MCP using an unload request */
15529         fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15530         if (!fw) {
15531             BLOGE(sc, "MCP response failure, aborting\n");
15532             rc = -1;
15533             break;
15534         }
15535 
15536         if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15537             rc = bxe_prev_unload_common(sc);
15538             break;
15539         }
15540 
15541         /* non-common reply from MCP night require looping */
15542         rc = bxe_prev_unload_uncommon(sc);
15543         if (rc != BXE_PREV_WAIT_NEEDED) {
15544             break;
15545         }
15546 
15547         DELAY(20000);
15548     } while (--time_counter);
15549 
15550     if (!time_counter || rc) {
15551         BLOGE(sc, "Failed to unload previous driver!"
15552             " time_counter %d rc %d\n", time_counter, rc);
15553         rc = -1;
15554     }
15555 
15556     return (rc);
15557 }
15558 
15559 void
15560 bxe_dcbx_set_state(struct bxe_softc *sc,
15561                    uint8_t          dcb_on,
15562                    uint32_t         dcbx_enabled)
15563 {
15564     if (!CHIP_IS_E1x(sc)) {
15565         sc->dcb_state = dcb_on;
15566         sc->dcbx_enabled = dcbx_enabled;
15567     } else {
15568         sc->dcb_state = FALSE;
15569         sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15570     }
15571     BLOGD(sc, DBG_LOAD,
15572           "DCB state [%s:%s]\n",
15573           dcb_on ? "ON" : "OFF",
15574           (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15575           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15576           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15577           "on-chip with negotiation" : "invalid");
15578 }
15579 
15580 /* must be called after sriov-enable */
15581 static int
15582 bxe_set_qm_cid_count(struct bxe_softc *sc)
15583 {
15584     int cid_count = BXE_L2_MAX_CID(sc);
15585 
15586     if (IS_SRIOV(sc)) {
15587         cid_count += BXE_VF_CIDS;
15588     }
15589 
15590     if (CNIC_SUPPORT(sc)) {
15591         cid_count += CNIC_CID_MAX;
15592     }
15593 
15594     return (roundup(cid_count, QM_CID_ROUND));
15595 }
15596 
15597 static void
15598 bxe_init_multi_cos(struct bxe_softc *sc)
15599 {
15600     int pri, cos;
15601 
15602     uint32_t pri_map = 0; /* XXX change to user config */
15603 
15604     for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15605         cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15606         if (cos < sc->max_cos) {
15607             sc->prio_to_cos[pri] = cos;
15608         } else {
15609             BLOGW(sc, "Invalid COS %d for priority %d "
15610                       "(max COS is %d), setting to 0\n",
15611                   cos, pri, (sc->max_cos - 1));
15612             sc->prio_to_cos[pri] = 0;
15613         }
15614     }
15615 }
15616 
15617 static int
15618 bxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15619 {
15620     struct bxe_softc *sc;
15621     int error, result;
15622 
15623     result = 0;
15624     error = sysctl_handle_int(oidp, &result, 0, req);
15625 
15626     if (error || !req->newptr) {
15627         return (error);
15628     }
15629 
15630     if (result == 1) {
15631         uint32_t  temp;
15632         sc = (struct bxe_softc *)arg1;
15633 
15634         BLOGI(sc, "... dumping driver state ...\n");
15635         temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15636         BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15637     }
15638 
15639     return (error);
15640 }
15641 
15642 static int
15643 bxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15644 {
15645     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15646     uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15647     uint32_t *offset;
15648     uint64_t value = 0;
15649     int index = (int)arg2;
15650 
15651     if (index >= BXE_NUM_ETH_STATS) {
15652         BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15653         return (-1);
15654     }
15655 
15656     offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15657 
15658     switch (bxe_eth_stats_arr[index].size) {
15659     case 4:
15660         value = (uint64_t)*offset;
15661         break;
15662     case 8:
15663         value = HILO_U64(*offset, *(offset + 1));
15664         break;
15665     default:
15666         BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15667               index, bxe_eth_stats_arr[index].size);
15668         return (-1);
15669     }
15670 
15671     return (sysctl_handle_64(oidp, &value, 0, req));
15672 }
15673 
15674 static int
15675 bxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15676 {
15677     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15678     uint32_t *eth_stats;
15679     uint32_t *offset;
15680     uint64_t value = 0;
15681     uint32_t q_stat = (uint32_t)arg2;
15682     uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15683     uint32_t index = (q_stat & 0xffff);
15684 
15685     eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15686 
15687     if (index >= BXE_NUM_ETH_Q_STATS) {
15688         BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15689         return (-1);
15690     }
15691 
15692     offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15693 
15694     switch (bxe_eth_q_stats_arr[index].size) {
15695     case 4:
15696         value = (uint64_t)*offset;
15697         break;
15698     case 8:
15699         value = HILO_U64(*offset, *(offset + 1));
15700         break;
15701     default:
15702         BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15703               index, bxe_eth_q_stats_arr[index].size);
15704         return (-1);
15705     }
15706 
15707     return (sysctl_handle_64(oidp, &value, 0, req));
15708 }
15709 
15710 static void
15711 bxe_add_sysctls(struct bxe_softc *sc)
15712 {
15713     struct sysctl_ctx_list *ctx;
15714     struct sysctl_oid_list *children;
15715     struct sysctl_oid *queue_top, *queue;
15716     struct sysctl_oid_list *queue_top_children, *queue_children;
15717     char queue_num_buf[32];
15718     uint32_t q_stat;
15719     int i, j;
15720 
15721     ctx = device_get_sysctl_ctx(sc->dev);
15722     children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15723 
15724     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
15725                       CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
15726                       "version");
15727 
15728     snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
15729              BCM_5710_FW_MAJOR_VERSION,
15730              BCM_5710_FW_MINOR_VERSION,
15731              BCM_5710_FW_REVISION_VERSION,
15732              BCM_5710_FW_ENGINEERING_VERSION);
15733 
15734     snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
15735         ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
15736          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
15737          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
15738          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
15739                                                                 "Unknown"));
15740     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
15741                     CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
15742                     "multifunction vnics per port");
15743 
15744     snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
15745         ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
15746          (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
15747          (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
15748                                               "???GT/s"),
15749         sc->devinfo.pcie_link_width);
15750 
15751     sc->debug = bxe_debug;
15752 
15753 #if __FreeBSD_version >= 900000
15754     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15755                       CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
15756                       "bootcode version");
15757     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15758                       CTLFLAG_RD, sc->fw_ver_str, 0,
15759                       "firmware version");
15760     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15761                       CTLFLAG_RD, sc->mf_mode_str, 0,
15762                       "multifunction mode");
15763     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15764                       CTLFLAG_RD, sc->mac_addr_str, 0,
15765                       "mac address");
15766     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15767                       CTLFLAG_RD, sc->pci_link_str, 0,
15768                       "pci link status");
15769     SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
15770                     CTLFLAG_RW, &sc->debug,
15771                     "debug logging mode");
15772 #else
15773     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
15774                       CTLFLAG_RD, &sc->devinfo.bc_ver_str, 0,
15775                       "bootcode version");
15776     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
15777                       CTLFLAG_RD, &sc->fw_ver_str, 0,
15778                       "firmware version");
15779     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
15780                       CTLFLAG_RD, &sc->mf_mode_str, 0,
15781                       "multifunction mode");
15782     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
15783                       CTLFLAG_RD, &sc->mac_addr_str, 0,
15784                       "mac address");
15785     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
15786                       CTLFLAG_RD, &sc->pci_link_str, 0,
15787                       "pci link status");
15788     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "debug",
15789                     CTLFLAG_RW, &sc->debug, 0,
15790                     "debug logging mode");
15791 #endif /* #if __FreeBSD_version >= 900000 */
15792 
15793     sc->trigger_grcdump = 0;
15794     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "trigger_grcdump",
15795                    CTLFLAG_RW, &sc->trigger_grcdump, 0,
15796                    "trigger grcdump should be invoked"
15797                    "  before collecting grcdump");
15798 
15799     sc->grcdump_started = 0;
15800     sc->grcdump_done = 0;
15801     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
15802                    CTLFLAG_RD, &sc->grcdump_done, 0,
15803                    "set by driver when grcdump is done");
15804 
15805     sc->rx_budget = bxe_rx_budget;
15806     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
15807                     CTLFLAG_RW, &sc->rx_budget, 0,
15808                     "rx processing budget");
15809 
15810     SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
15811                     CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
15812                     bxe_sysctl_state, "IU", "dump driver state");
15813 
15814     for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
15815         SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
15816                         bxe_eth_stats_arr[i].string,
15817                         CTLTYPE_U64 | CTLFLAG_RD, sc, i,
15818                         bxe_sysctl_eth_stat, "LU",
15819                         bxe_eth_stats_arr[i].string);
15820     }
15821 
15822     /* add a new parent node for all queues "dev.bxe.#.queue" */
15823     queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
15824                                 CTLFLAG_RD, NULL, "queue");
15825     queue_top_children = SYSCTL_CHILDREN(queue_top);
15826 
15827     for (i = 0; i < sc->num_queues; i++) {
15828         /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
15829         snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
15830         queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
15831                                 queue_num_buf, CTLFLAG_RD, NULL,
15832                                 "single queue");
15833         queue_children = SYSCTL_CHILDREN(queue);
15834 
15835         for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
15836             q_stat = ((i << 16) | j);
15837             SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
15838                             bxe_eth_q_stats_arr[j].string,
15839                             CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
15840                             bxe_sysctl_eth_q_stat, "LU",
15841                             bxe_eth_q_stats_arr[j].string);
15842         }
15843     }
15844 }
15845 
15846 static int
15847 bxe_alloc_buf_rings(struct bxe_softc *sc)
15848 {
15849 #if __FreeBSD_version >= 800000
15850 
15851     int i;
15852     struct bxe_fastpath *fp;
15853 
15854     for (i = 0; i < sc->num_queues; i++) {
15855 
15856         fp = &sc->fp[i];
15857 
15858         fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
15859                                    M_NOWAIT, &fp->tx_mtx);
15860         if (fp->tx_br == NULL)
15861             return (-1);
15862     }
15863 #endif
15864     return (0);
15865 }
15866 
15867 static void
15868 bxe_free_buf_rings(struct bxe_softc *sc)
15869 {
15870 #if __FreeBSD_version >= 800000
15871 
15872     int i;
15873     struct bxe_fastpath *fp;
15874 
15875     for (i = 0; i < sc->num_queues; i++) {
15876 
15877         fp = &sc->fp[i];
15878 
15879         if (fp->tx_br) {
15880             buf_ring_free(fp->tx_br, M_DEVBUF);
15881             fp->tx_br = NULL;
15882         }
15883     }
15884 
15885 #endif
15886 }
15887 
15888 static void
15889 bxe_init_fp_mutexs(struct bxe_softc *sc)
15890 {
15891     int i;
15892     struct bxe_fastpath *fp;
15893 
15894     for (i = 0; i < sc->num_queues; i++) {
15895 
15896         fp = &sc->fp[i];
15897 
15898         snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
15899             "bxe%d_fp%d_tx_lock", sc->unit, i);
15900         mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
15901 
15902         snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
15903             "bxe%d_fp%d_rx_lock", sc->unit, i);
15904         mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
15905     }
15906 }
15907 
15908 static void
15909 bxe_destroy_fp_mutexs(struct bxe_softc *sc)
15910 {
15911     int i;
15912     struct bxe_fastpath *fp;
15913 
15914     for (i = 0; i < sc->num_queues; i++) {
15915 
15916         fp = &sc->fp[i];
15917 
15918         if (mtx_initialized(&fp->tx_mtx)) {
15919             mtx_destroy(&fp->tx_mtx);
15920         }
15921 
15922         if (mtx_initialized(&fp->rx_mtx)) {
15923             mtx_destroy(&fp->rx_mtx);
15924         }
15925     }
15926 }
15927 
15928 
15929 /*
15930  * Device attach function.
15931  *
15932  * Allocates device resources, performs secondary chip identification, and
15933  * initializes driver instance variables. This function is called from driver
15934  * load after a successful probe.
15935  *
15936  * Returns:
15937  *   0 = Success, >0 = Failure
15938  */
15939 static int
15940 bxe_attach(device_t dev)
15941 {
15942     struct bxe_softc *sc;
15943 
15944     sc = device_get_softc(dev);
15945 
15946     BLOGD(sc, DBG_LOAD, "Starting attach...\n");
15947 
15948     sc->state = BXE_STATE_CLOSED;
15949 
15950     sc->dev  = dev;
15951     sc->unit = device_get_unit(dev);
15952 
15953     BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
15954 
15955     sc->pcie_bus    = pci_get_bus(dev);
15956     sc->pcie_device = pci_get_slot(dev);
15957     sc->pcie_func   = pci_get_function(dev);
15958 
15959     /* enable bus master capability */
15960     pci_enable_busmaster(dev);
15961 
15962     /* get the BARs */
15963     if (bxe_allocate_bars(sc) != 0) {
15964         return (ENXIO);
15965     }
15966 
15967     /* initialize the mutexes */
15968     bxe_init_mutexes(sc);
15969 
15970     /* prepare the periodic callout */
15971     callout_init(&sc->periodic_callout, 0);
15972 
15973     /* prepare the chip taskqueue */
15974     sc->chip_tq_flags = CHIP_TQ_NONE;
15975     snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
15976              "bxe%d_chip_tq", sc->unit);
15977     TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
15978     sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
15979                                    taskqueue_thread_enqueue,
15980                                    &sc->chip_tq);
15981     taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
15982                             "%s", sc->chip_tq_name);
15983 
15984     /* get device info and set params */
15985     if (bxe_get_device_info(sc) != 0) {
15986         BLOGE(sc, "getting device info\n");
15987         bxe_deallocate_bars(sc);
15988         pci_disable_busmaster(dev);
15989         return (ENXIO);
15990     }
15991 
15992     /* get final misc params */
15993     bxe_get_params(sc);
15994 
15995     /* set the default MTU (changed via ifconfig) */
15996     sc->mtu = ETHERMTU;
15997 
15998     bxe_set_modes_bitmap(sc);
15999 
16000     /* XXX
16001      * If in AFEX mode and the function is configured for FCoE
16002      * then bail... no L2 allowed.
16003      */
16004 
16005     /* get phy settings from shmem and 'and' against admin settings */
16006     bxe_get_phy_info(sc);
16007 
16008     /* initialize the FreeBSD ifnet interface */
16009     if (bxe_init_ifnet(sc) != 0) {
16010         bxe_release_mutexes(sc);
16011         bxe_deallocate_bars(sc);
16012         pci_disable_busmaster(dev);
16013         return (ENXIO);
16014     }
16015 
16016     if (bxe_add_cdev(sc) != 0) {
16017         if (sc->ifp != NULL) {
16018             ether_ifdetach(sc->ifp);
16019         }
16020         ifmedia_removeall(&sc->ifmedia);
16021         bxe_release_mutexes(sc);
16022         bxe_deallocate_bars(sc);
16023         pci_disable_busmaster(dev);
16024         return (ENXIO);
16025     }
16026 
16027     /* allocate device interrupts */
16028     if (bxe_interrupt_alloc(sc) != 0) {
16029         bxe_del_cdev(sc);
16030         if (sc->ifp != NULL) {
16031             ether_ifdetach(sc->ifp);
16032         }
16033         ifmedia_removeall(&sc->ifmedia);
16034         bxe_release_mutexes(sc);
16035         bxe_deallocate_bars(sc);
16036         pci_disable_busmaster(dev);
16037         return (ENXIO);
16038     }
16039 
16040     bxe_init_fp_mutexs(sc);
16041 
16042     if (bxe_alloc_buf_rings(sc) != 0) {
16043 	bxe_free_buf_rings(sc);
16044         bxe_interrupt_free(sc);
16045         bxe_del_cdev(sc);
16046         if (sc->ifp != NULL) {
16047             ether_ifdetach(sc->ifp);
16048         }
16049         ifmedia_removeall(&sc->ifmedia);
16050         bxe_release_mutexes(sc);
16051         bxe_deallocate_bars(sc);
16052         pci_disable_busmaster(dev);
16053         return (ENXIO);
16054     }
16055 
16056     /* allocate ilt */
16057     if (bxe_alloc_ilt_mem(sc) != 0) {
16058 	bxe_free_buf_rings(sc);
16059         bxe_interrupt_free(sc);
16060         bxe_del_cdev(sc);
16061         if (sc->ifp != NULL) {
16062             ether_ifdetach(sc->ifp);
16063         }
16064         ifmedia_removeall(&sc->ifmedia);
16065         bxe_release_mutexes(sc);
16066         bxe_deallocate_bars(sc);
16067         pci_disable_busmaster(dev);
16068         return (ENXIO);
16069     }
16070 
16071     /* allocate the host hardware/software hsi structures */
16072     if (bxe_alloc_hsi_mem(sc) != 0) {
16073         bxe_free_ilt_mem(sc);
16074 	bxe_free_buf_rings(sc);
16075         bxe_interrupt_free(sc);
16076         bxe_del_cdev(sc);
16077         if (sc->ifp != NULL) {
16078             ether_ifdetach(sc->ifp);
16079         }
16080         ifmedia_removeall(&sc->ifmedia);
16081         bxe_release_mutexes(sc);
16082         bxe_deallocate_bars(sc);
16083         pci_disable_busmaster(dev);
16084         return (ENXIO);
16085     }
16086 
16087     /* need to reset chip if UNDI was active */
16088     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
16089         /* init fw_seq */
16090         sc->fw_seq =
16091             (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
16092              DRV_MSG_SEQ_NUMBER_MASK);
16093         BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
16094         bxe_prev_unload(sc);
16095     }
16096 
16097 #if 1
16098     /* XXX */
16099     bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16100 #else
16101     if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
16102         SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
16103         SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
16104         SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
16105         bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
16106         bxe_dcbx_init_params(sc);
16107     } else {
16108         bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16109     }
16110 #endif
16111 
16112     /* calculate qm_cid_count */
16113     sc->qm_cid_count = bxe_set_qm_cid_count(sc);
16114     BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
16115 
16116     sc->max_cos = 1;
16117     bxe_init_multi_cos(sc);
16118 
16119     bxe_add_sysctls(sc);
16120 
16121     return (0);
16122 }
16123 
16124 /*
16125  * Device detach function.
16126  *
16127  * Stops the controller, resets the controller, and releases resources.
16128  *
16129  * Returns:
16130  *   0 = Success, >0 = Failure
16131  */
16132 static int
16133 bxe_detach(device_t dev)
16134 {
16135     struct bxe_softc *sc;
16136     if_t ifp;
16137 
16138     sc = device_get_softc(dev);
16139 
16140     BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16141 
16142     ifp = sc->ifp;
16143     if (ifp != NULL && if_vlantrunkinuse(ifp)) {
16144         BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16145         return(EBUSY);
16146     }
16147 
16148     bxe_del_cdev(sc);
16149 
16150     /* stop the periodic callout */
16151     bxe_periodic_stop(sc);
16152 
16153     /* stop the chip taskqueue */
16154     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16155     if (sc->chip_tq) {
16156         taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16157         taskqueue_free(sc->chip_tq);
16158         sc->chip_tq = NULL;
16159     }
16160 
16161     /* stop and reset the controller if it was open */
16162     if (sc->state != BXE_STATE_CLOSED) {
16163         BXE_CORE_LOCK(sc);
16164         bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16165         sc->state = BXE_STATE_DISABLED;
16166         BXE_CORE_UNLOCK(sc);
16167     }
16168 
16169     /* release the network interface */
16170     if (ifp != NULL) {
16171         ether_ifdetach(ifp);
16172     }
16173     ifmedia_removeall(&sc->ifmedia);
16174 
16175     /* XXX do the following based on driver state... */
16176 
16177     /* free the host hardware/software hsi structures */
16178     bxe_free_hsi_mem(sc);
16179 
16180     /* free ilt */
16181     bxe_free_ilt_mem(sc);
16182 
16183     bxe_free_buf_rings(sc);
16184 
16185     /* release the interrupts */
16186     bxe_interrupt_free(sc);
16187 
16188     /* Release the mutexes*/
16189     bxe_destroy_fp_mutexs(sc);
16190     bxe_release_mutexes(sc);
16191 
16192 
16193     /* Release the PCIe BAR mapped memory */
16194     bxe_deallocate_bars(sc);
16195 
16196     /* Release the FreeBSD interface. */
16197     if (sc->ifp != NULL) {
16198         if_free(sc->ifp);
16199     }
16200 
16201     pci_disable_busmaster(dev);
16202 
16203     return (0);
16204 }
16205 
16206 /*
16207  * Device shutdown function.
16208  *
16209  * Stops and resets the controller.
16210  *
16211  * Returns:
16212  *   Nothing
16213  */
16214 static int
16215 bxe_shutdown(device_t dev)
16216 {
16217     struct bxe_softc *sc;
16218 
16219     sc = device_get_softc(dev);
16220 
16221     BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16222 
16223     /* stop the periodic callout */
16224     bxe_periodic_stop(sc);
16225 
16226     BXE_CORE_LOCK(sc);
16227     bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16228     BXE_CORE_UNLOCK(sc);
16229 
16230     return (0);
16231 }
16232 
16233 void
16234 bxe_igu_ack_sb(struct bxe_softc *sc,
16235                uint8_t          igu_sb_id,
16236                uint8_t          segment,
16237                uint16_t         index,
16238                uint8_t          op,
16239                uint8_t          update)
16240 {
16241     uint32_t igu_addr = sc->igu_base_addr;
16242     igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16243     bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16244 }
16245 
16246 static void
16247 bxe_igu_clear_sb_gen(struct bxe_softc *sc,
16248                      uint8_t          func,
16249                      uint8_t          idu_sb_id,
16250                      uint8_t          is_pf)
16251 {
16252     uint32_t data, ctl, cnt = 100;
16253     uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16254     uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16255     uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16256     uint32_t sb_bit =  1 << (idu_sb_id%32);
16257     uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16258     uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16259 
16260     /* Not supported in BC mode */
16261     if (CHIP_INT_MODE_IS_BC(sc)) {
16262         return;
16263     }
16264 
16265     data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16266              IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16267             IGU_REGULAR_CLEANUP_SET |
16268             IGU_REGULAR_BCLEANUP);
16269 
16270     ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16271            (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16272            (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16273 
16274     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16275             data, igu_addr_data);
16276     REG_WR(sc, igu_addr_data, data);
16277 
16278     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16279                       BUS_SPACE_BARRIER_WRITE);
16280     mb();
16281 
16282     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16283             ctl, igu_addr_ctl);
16284     REG_WR(sc, igu_addr_ctl, ctl);
16285 
16286     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16287                       BUS_SPACE_BARRIER_WRITE);
16288     mb();
16289 
16290     /* wait for clean up to finish */
16291     while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16292         DELAY(20000);
16293     }
16294 
16295     if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16296         BLOGD(sc, DBG_LOAD,
16297               "Unable to finish IGU cleanup: "
16298               "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16299               idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16300     }
16301 }
16302 
16303 static void
16304 bxe_igu_clear_sb(struct bxe_softc *sc,
16305                  uint8_t          idu_sb_id)
16306 {
16307     bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16308 }
16309 
16310 
16311 
16312 
16313 
16314 
16315 
16316 /*******************/
16317 /* ECORE CALLBACKS */
16318 /*******************/
16319 
16320 static void
16321 bxe_reset_common(struct bxe_softc *sc)
16322 {
16323     uint32_t val = 0x1400;
16324 
16325     /* reset_common */
16326     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16327 
16328     if (CHIP_IS_E3(sc)) {
16329         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16330         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16331     }
16332 
16333     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16334 }
16335 
16336 static void
16337 bxe_common_init_phy(struct bxe_softc *sc)
16338 {
16339     uint32_t shmem_base[2];
16340     uint32_t shmem2_base[2];
16341 
16342     /* Avoid common init in case MFW supports LFA */
16343     if (SHMEM2_RD(sc, size) >
16344         (uint32_t)offsetof(struct shmem2_region,
16345                            lfa_host_addr[SC_PORT(sc)])) {
16346         return;
16347     }
16348 
16349     shmem_base[0]  = sc->devinfo.shmem_base;
16350     shmem2_base[0] = sc->devinfo.shmem2_base;
16351 
16352     if (!CHIP_IS_E1x(sc)) {
16353         shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16354         shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16355     }
16356 
16357     bxe_acquire_phy_lock(sc);
16358     elink_common_init_phy(sc, shmem_base, shmem2_base,
16359                           sc->devinfo.chip_id, 0);
16360     bxe_release_phy_lock(sc);
16361 }
16362 
16363 static void
16364 bxe_pf_disable(struct bxe_softc *sc)
16365 {
16366     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16367 
16368     val &= ~IGU_PF_CONF_FUNC_EN;
16369 
16370     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16371     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16372     REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16373 }
16374 
16375 static void
16376 bxe_init_pxp(struct bxe_softc *sc)
16377 {
16378     uint16_t devctl;
16379     int r_order, w_order;
16380 
16381     devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16382 
16383     BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16384 
16385     w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16386 
16387     if (sc->mrrs == -1) {
16388         r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16389     } else {
16390         BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16391         r_order = sc->mrrs;
16392     }
16393 
16394     ecore_init_pxp_arb(sc, r_order, w_order);
16395 }
16396 
16397 static uint32_t
16398 bxe_get_pretend_reg(struct bxe_softc *sc)
16399 {
16400     uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16401     uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16402     return (base + (SC_ABS_FUNC(sc)) * stride);
16403 }
16404 
16405 /*
16406  * Called only on E1H or E2.
16407  * When pretending to be PF, the pretend value is the function number 0..7.
16408  * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16409  * combination.
16410  */
16411 static int
16412 bxe_pretend_func(struct bxe_softc *sc,
16413                  uint16_t         pretend_func_val)
16414 {
16415     uint32_t pretend_reg;
16416 
16417     if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16418         return (-1);
16419     }
16420 
16421     /* get my own pretend register */
16422     pretend_reg = bxe_get_pretend_reg(sc);
16423     REG_WR(sc, pretend_reg, pretend_func_val);
16424     REG_RD(sc, pretend_reg);
16425     return (0);
16426 }
16427 
16428 static void
16429 bxe_iov_init_dmae(struct bxe_softc *sc)
16430 {
16431     return;
16432 }
16433 
16434 static void
16435 bxe_iov_init_dq(struct bxe_softc *sc)
16436 {
16437     return;
16438 }
16439 
16440 /* send a NIG loopback debug packet */
16441 static void
16442 bxe_lb_pckt(struct bxe_softc *sc)
16443 {
16444     uint32_t wb_write[3];
16445 
16446     /* Ethernet source and destination addresses */
16447     wb_write[0] = 0x55555555;
16448     wb_write[1] = 0x55555555;
16449     wb_write[2] = 0x20;     /* SOP */
16450     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16451 
16452     /* NON-IP protocol */
16453     wb_write[0] = 0x09000000;
16454     wb_write[1] = 0x55555555;
16455     wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16456     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16457 }
16458 
16459 /*
16460  * Some of the internal memories are not directly readable from the driver.
16461  * To test them we send debug packets.
16462  */
16463 static int
16464 bxe_int_mem_test(struct bxe_softc *sc)
16465 {
16466     int factor;
16467     int count, i;
16468     uint32_t val = 0;
16469 
16470     if (CHIP_REV_IS_FPGA(sc)) {
16471         factor = 120;
16472     } else if (CHIP_REV_IS_EMUL(sc)) {
16473         factor = 200;
16474     } else {
16475         factor = 1;
16476     }
16477 
16478     /* disable inputs of parser neighbor blocks */
16479     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16480     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16481     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16482     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16483 
16484     /*  write 0 to parser credits for CFC search request */
16485     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16486 
16487     /* send Ethernet packet */
16488     bxe_lb_pckt(sc);
16489 
16490     /* TODO do i reset NIG statistic? */
16491     /* Wait until NIG register shows 1 packet of size 0x10 */
16492     count = 1000 * factor;
16493     while (count) {
16494         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16495         val = *BXE_SP(sc, wb_data[0]);
16496         if (val == 0x10) {
16497             break;
16498         }
16499 
16500         DELAY(10000);
16501         count--;
16502     }
16503 
16504     if (val != 0x10) {
16505         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16506         return (-1);
16507     }
16508 
16509     /* wait until PRS register shows 1 packet */
16510     count = (1000 * factor);
16511     while (count) {
16512         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16513         if (val == 1) {
16514             break;
16515         }
16516 
16517         DELAY(10000);
16518         count--;
16519     }
16520 
16521     if (val != 0x1) {
16522         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16523         return (-2);
16524     }
16525 
16526     /* Reset and init BRB, PRS */
16527     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16528     DELAY(50000);
16529     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16530     DELAY(50000);
16531     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16532     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16533 
16534     /* Disable inputs of parser neighbor blocks */
16535     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16536     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16537     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16538     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16539 
16540     /* Write 0 to parser credits for CFC search request */
16541     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16542 
16543     /* send 10 Ethernet packets */
16544     for (i = 0; i < 10; i++) {
16545         bxe_lb_pckt(sc);
16546     }
16547 
16548     /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16549     count = (1000 * factor);
16550     while (count) {
16551         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16552         val = *BXE_SP(sc, wb_data[0]);
16553         if (val == 0xb0) {
16554             break;
16555         }
16556 
16557         DELAY(10000);
16558         count--;
16559     }
16560 
16561     if (val != 0xb0) {
16562         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16563         return (-3);
16564     }
16565 
16566     /* Wait until PRS register shows 2 packets */
16567     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16568     if (val != 2) {
16569         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16570     }
16571 
16572     /* Write 1 to parser credits for CFC search request */
16573     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16574 
16575     /* Wait until PRS register shows 3 packets */
16576     DELAY(10000 * factor);
16577 
16578     /* Wait until NIG register shows 1 packet of size 0x10 */
16579     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16580     if (val != 3) {
16581         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16582     }
16583 
16584     /* clear NIG EOP FIFO */
16585     for (i = 0; i < 11; i++) {
16586         REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16587     }
16588 
16589     val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16590     if (val != 1) {
16591         BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16592         return (-4);
16593     }
16594 
16595     /* Reset and init BRB, PRS, NIG */
16596     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16597     DELAY(50000);
16598     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16599     DELAY(50000);
16600     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16601     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16602     if (!CNIC_SUPPORT(sc)) {
16603         /* set NIC mode */
16604         REG_WR(sc, PRS_REG_NIC_MODE, 1);
16605     }
16606 
16607     /* Enable inputs of parser neighbor blocks */
16608     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16609     REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16610     REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16611     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16612 
16613     return (0);
16614 }
16615 
16616 static void
16617 bxe_setup_fan_failure_detection(struct bxe_softc *sc)
16618 {
16619     int is_required;
16620     uint32_t val;
16621     int port;
16622 
16623     is_required = 0;
16624     val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
16625            SHARED_HW_CFG_FAN_FAILURE_MASK);
16626 
16627     if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
16628         is_required = 1;
16629     }
16630     /*
16631      * The fan failure mechanism is usually related to the PHY type since
16632      * the power consumption of the board is affected by the PHY. Currently,
16633      * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
16634      */
16635     else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
16636         for (port = PORT_0; port < PORT_MAX; port++) {
16637             is_required |= elink_fan_failure_det_req(sc,
16638                                                      sc->devinfo.shmem_base,
16639                                                      sc->devinfo.shmem2_base,
16640                                                      port);
16641         }
16642     }
16643 
16644     BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
16645 
16646     if (is_required == 0) {
16647         return;
16648     }
16649 
16650     /* Fan failure is indicated by SPIO 5 */
16651     bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
16652 
16653     /* set to active low mode */
16654     val = REG_RD(sc, MISC_REG_SPIO_INT);
16655     val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
16656     REG_WR(sc, MISC_REG_SPIO_INT, val);
16657 
16658     /* enable interrupt to signal the IGU */
16659     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
16660     val |= MISC_SPIO_SPIO5;
16661     REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
16662 }
16663 
16664 static void
16665 bxe_enable_blocks_attention(struct bxe_softc *sc)
16666 {
16667     uint32_t val;
16668 
16669     REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16670     if (!CHIP_IS_E1x(sc)) {
16671         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
16672     } else {
16673         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
16674     }
16675     REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
16676     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
16677     /*
16678      * mask read length error interrupts in brb for parser
16679      * (parsing unit and 'checksum and crc' unit)
16680      * these errors are legal (PU reads fixed length and CAC can cause
16681      * read length error on truncated packets)
16682      */
16683     REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
16684     REG_WR(sc, QM_REG_QM_INT_MASK, 0);
16685     REG_WR(sc, TM_REG_TM_INT_MASK, 0);
16686     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
16687     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
16688     REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
16689 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
16690 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
16691     REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
16692     REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
16693     REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
16694 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
16695 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
16696     REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
16697     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
16698     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
16699     REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
16700 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
16701 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
16702 
16703     val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
16704            PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
16705            PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
16706     if (!CHIP_IS_E1x(sc)) {
16707         val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
16708                 PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
16709     }
16710     REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
16711 
16712     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
16713     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
16714     REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
16715 /*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
16716 
16717     if (!CHIP_IS_E1x(sc)) {
16718         /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
16719         REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
16720     }
16721 
16722     REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
16723     REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
16724 /*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
16725     REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
16726 }
16727 
16728 /**
16729  * bxe_init_hw_common - initialize the HW at the COMMON phase.
16730  *
16731  * @sc:     driver handle
16732  */
16733 static int
16734 bxe_init_hw_common(struct bxe_softc *sc)
16735 {
16736     uint8_t abs_func_id;
16737     uint32_t val;
16738 
16739     BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
16740           SC_ABS_FUNC(sc));
16741 
16742     /*
16743      * take the RESET lock to protect undi_unload flow from accessing
16744      * registers while we are resetting the chip
16745      */
16746     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16747 
16748     bxe_reset_common(sc);
16749 
16750     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
16751 
16752     val = 0xfffc;
16753     if (CHIP_IS_E3(sc)) {
16754         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16755         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16756     }
16757 
16758     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
16759 
16760     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
16761 
16762     ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
16763     BLOGD(sc, DBG_LOAD, "after misc block init\n");
16764 
16765     if (!CHIP_IS_E1x(sc)) {
16766         /*
16767          * 4-port mode or 2-port mode we need to turn off master-enable for
16768          * everyone. After that we turn it back on for self. So, we disregard
16769          * multi-function, and always disable all functions on the given path,
16770          * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
16771          */
16772         for (abs_func_id = SC_PATH(sc);
16773              abs_func_id < (E2_FUNC_MAX * 2);
16774              abs_func_id += 2) {
16775             if (abs_func_id == SC_ABS_FUNC(sc)) {
16776                 REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
16777                 continue;
16778             }
16779 
16780             bxe_pretend_func(sc, abs_func_id);
16781 
16782             /* clear pf enable */
16783             bxe_pf_disable(sc);
16784 
16785             bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16786         }
16787     }
16788 
16789     BLOGD(sc, DBG_LOAD, "after pf disable\n");
16790 
16791     ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
16792 
16793     if (CHIP_IS_E1(sc)) {
16794         /*
16795          * enable HW interrupt from PXP on USDM overflow
16796          * bit 16 on INT_MASK_0
16797          */
16798         REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
16799     }
16800 
16801     ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
16802     bxe_init_pxp(sc);
16803 
16804 #ifdef __BIG_ENDIAN
16805     REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
16806     REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
16807     REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
16808     REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
16809     REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
16810     /* make sure this value is 0 */
16811     REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
16812 
16813     //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
16814     REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
16815     REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
16816     REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
16817     REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
16818 #endif
16819 
16820     ecore_ilt_init_page_size(sc, INITOP_SET);
16821 
16822     if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
16823         REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
16824     }
16825 
16826     /* let the HW do it's magic... */
16827     DELAY(100000);
16828 
16829     /* finish PXP init */
16830     val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
16831     if (val != 1) {
16832         BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
16833             val);
16834         return (-1);
16835     }
16836     val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
16837     if (val != 1) {
16838         BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
16839         return (-1);
16840     }
16841 
16842     BLOGD(sc, DBG_LOAD, "after pxp init\n");
16843 
16844     /*
16845      * Timer bug workaround for E2 only. We need to set the entire ILT to have
16846      * entries with value "0" and valid bit on. This needs to be done by the
16847      * first PF that is loaded in a path (i.e. common phase)
16848      */
16849     if (!CHIP_IS_E1x(sc)) {
16850 /*
16851  * In E2 there is a bug in the timers block that can cause function 6 / 7
16852  * (i.e. vnic3) to start even if it is marked as "scan-off".
16853  * This occurs when a different function (func2,3) is being marked
16854  * as "scan-off". Real-life scenario for example: if a driver is being
16855  * load-unloaded while func6,7 are down. This will cause the timer to access
16856  * the ilt, translate to a logical address and send a request to read/write.
16857  * Since the ilt for the function that is down is not valid, this will cause
16858  * a translation error which is unrecoverable.
16859  * The Workaround is intended to make sure that when this happens nothing
16860  * fatal will occur. The workaround:
16861  *  1.  First PF driver which loads on a path will:
16862  *      a.  After taking the chip out of reset, by using pretend,
16863  *          it will write "0" to the following registers of
16864  *          the other vnics.
16865  *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16866  *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
16867  *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
16868  *          And for itself it will write '1' to
16869  *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
16870  *          dmae-operations (writing to pram for example.)
16871  *          note: can be done for only function 6,7 but cleaner this
16872  *            way.
16873  *      b.  Write zero+valid to the entire ILT.
16874  *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
16875  *          VNIC3 (of that port). The range allocated will be the
16876  *          entire ILT. This is needed to prevent  ILT range error.
16877  *  2.  Any PF driver load flow:
16878  *      a.  ILT update with the physical addresses of the allocated
16879  *          logical pages.
16880  *      b.  Wait 20msec. - note that this timeout is needed to make
16881  *          sure there are no requests in one of the PXP internal
16882  *          queues with "old" ILT addresses.
16883  *      c.  PF enable in the PGLC.
16884  *      d.  Clear the was_error of the PF in the PGLC. (could have
16885  *          occurred while driver was down)
16886  *      e.  PF enable in the CFC (WEAK + STRONG)
16887  *      f.  Timers scan enable
16888  *  3.  PF driver unload flow:
16889  *      a.  Clear the Timers scan_en.
16890  *      b.  Polling for scan_on=0 for that PF.
16891  *      c.  Clear the PF enable bit in the PXP.
16892  *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
16893  *      e.  Write zero+valid to all ILT entries (The valid bit must
16894  *          stay set)
16895  *      f.  If this is VNIC 3 of a port then also init
16896  *          first_timers_ilt_entry to zero and last_timers_ilt_entry
16897  *          to the last enrty in the ILT.
16898  *
16899  *      Notes:
16900  *      Currently the PF error in the PGLC is non recoverable.
16901  *      In the future the there will be a recovery routine for this error.
16902  *      Currently attention is masked.
16903  *      Having an MCP lock on the load/unload process does not guarantee that
16904  *      there is no Timer disable during Func6/7 enable. This is because the
16905  *      Timers scan is currently being cleared by the MCP on FLR.
16906  *      Step 2.d can be done only for PF6/7 and the driver can also check if
16907  *      there is error before clearing it. But the flow above is simpler and
16908  *      more general.
16909  *      All ILT entries are written by zero+valid and not just PF6/7
16910  *      ILT entries since in the future the ILT entries allocation for
16911  *      PF-s might be dynamic.
16912  */
16913         struct ilt_client_info ilt_cli;
16914         struct ecore_ilt ilt;
16915 
16916         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
16917         memset(&ilt, 0, sizeof(struct ecore_ilt));
16918 
16919         /* initialize dummy TM client */
16920         ilt_cli.start      = 0;
16921         ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
16922         ilt_cli.client_num = ILT_CLIENT_TM;
16923 
16924         /*
16925          * Step 1: set zeroes to all ilt page entries with valid bit on
16926          * Step 2: set the timers first/last ilt entry to point
16927          * to the entire range to prevent ILT range error for 3rd/4th
16928          * vnic (this code assumes existence of the vnic)
16929          *
16930          * both steps performed by call to ecore_ilt_client_init_op()
16931          * with dummy TM client
16932          *
16933          * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
16934          * and his brother are split registers
16935          */
16936 
16937         bxe_pretend_func(sc, (SC_PATH(sc) + 6));
16938         ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
16939         bxe_pretend_func(sc, SC_ABS_FUNC(sc));
16940 
16941         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
16942         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
16943         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
16944     }
16945 
16946     REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
16947     REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
16948 
16949     if (!CHIP_IS_E1x(sc)) {
16950         int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
16951                      (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
16952 
16953         ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
16954         ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
16955 
16956         /* let the HW do it's magic... */
16957         do {
16958             DELAY(200000);
16959             val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
16960         } while (factor-- && (val != 1));
16961 
16962         if (val != 1) {
16963             BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
16964             return (-1);
16965         }
16966     }
16967 
16968     BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
16969 
16970     ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
16971 
16972     bxe_iov_init_dmae(sc);
16973 
16974     /* clean the DMAE memory */
16975     sc->dmae_ready = 1;
16976     ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
16977 
16978     ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
16979 
16980     ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
16981 
16982     ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
16983 
16984     ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
16985 
16986     bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
16987     bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
16988     bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
16989     bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
16990 
16991     ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
16992 
16993     /* QM queues pointers table */
16994     ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
16995 
16996     /* soft reset pulse */
16997     REG_WR(sc, QM_REG_SOFT_RESET, 1);
16998     REG_WR(sc, QM_REG_SOFT_RESET, 0);
16999 
17000     if (CNIC_SUPPORT(sc))
17001         ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
17002 
17003     ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
17004     REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
17005     if (!CHIP_REV_IS_SLOW(sc)) {
17006         /* enable hw interrupt from doorbell Q */
17007         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17008     }
17009 
17010     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17011 
17012     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17013     REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
17014 
17015     if (!CHIP_IS_E1(sc)) {
17016         REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
17017     }
17018 
17019     if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
17020         if (IS_MF_AFEX(sc)) {
17021             /*
17022              * configure that AFEX and VLAN headers must be
17023              * received in AFEX mode
17024              */
17025             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
17026             REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
17027             REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
17028             REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
17029             REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
17030         } else {
17031             /*
17032              * Bit-map indicating which L2 hdrs may appear
17033              * after the basic Ethernet header
17034              */
17035             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
17036                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17037         }
17038     }
17039 
17040     ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
17041     ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
17042     ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
17043     ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
17044 
17045     if (!CHIP_IS_E1x(sc)) {
17046         /* reset VFC memories */
17047         REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17048                VFC_MEMORIES_RST_REG_CAM_RST |
17049                VFC_MEMORIES_RST_REG_RAM_RST);
17050         REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17051                VFC_MEMORIES_RST_REG_CAM_RST |
17052                VFC_MEMORIES_RST_REG_RAM_RST);
17053 
17054         DELAY(20000);
17055     }
17056 
17057     ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
17058     ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
17059     ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
17060     ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
17061 
17062     /* sync semi rtc */
17063     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
17064            0x80000000);
17065     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
17066            0x80000000);
17067 
17068     ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
17069     ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
17070     ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
17071 
17072     if (!CHIP_IS_E1x(sc)) {
17073         if (IS_MF_AFEX(sc)) {
17074             /*
17075              * configure that AFEX and VLAN headers must be
17076              * sent in AFEX mode
17077              */
17078             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
17079             REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
17080             REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
17081             REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
17082             REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
17083         } else {
17084             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
17085                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17086         }
17087     }
17088 
17089     REG_WR(sc, SRC_REG_SOFT_RST, 1);
17090 
17091     ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
17092 
17093     if (CNIC_SUPPORT(sc)) {
17094         REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
17095         REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
17096         REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
17097         REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
17098         REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
17099         REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
17100         REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
17101         REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
17102         REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
17103         REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
17104     }
17105     REG_WR(sc, SRC_REG_SOFT_RST, 0);
17106 
17107     if (sizeof(union cdu_context) != 1024) {
17108         /* we currently assume that a context is 1024 bytes */
17109         BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
17110               (long)sizeof(union cdu_context));
17111     }
17112 
17113     ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
17114     val = (4 << 24) + (0 << 12) + 1024;
17115     REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
17116 
17117     ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
17118 
17119     REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
17120     /* enable context validation interrupt from CFC */
17121     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17122 
17123     /* set the thresholds to prevent CFC/CDU race */
17124     REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
17125     ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
17126 
17127     if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
17128         REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
17129     }
17130 
17131     ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
17132     ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
17133 
17134     /* Reset PCIE errors for debug */
17135     REG_WR(sc, 0x2814, 0xffffffff);
17136     REG_WR(sc, 0x3820, 0xffffffff);
17137 
17138     if (!CHIP_IS_E1x(sc)) {
17139         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
17140                (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17141                 PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17142         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17143                (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17144                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17145                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17146         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17147                (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17148                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17149                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17150     }
17151 
17152     ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17153 
17154     if (!CHIP_IS_E1(sc)) {
17155         /* in E3 this done in per-port section */
17156         if (!CHIP_IS_E3(sc))
17157             REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17158     }
17159 
17160     if (CHIP_IS_E1H(sc)) {
17161         /* not applicable for E2 (and above ...) */
17162         REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17163     }
17164 
17165     if (CHIP_REV_IS_SLOW(sc)) {
17166         DELAY(200000);
17167     }
17168 
17169     /* finish CFC init */
17170     val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17171     if (val != 1) {
17172         BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17173         return (-1);
17174     }
17175     val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17176     if (val != 1) {
17177         BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17178         return (-1);
17179     }
17180     val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17181     if (val != 1) {
17182         BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17183         return (-1);
17184     }
17185     REG_WR(sc, CFC_REG_DEBUG0, 0);
17186 
17187     if (CHIP_IS_E1(sc)) {
17188         /* read NIG statistic to see if this is our first up since powerup */
17189         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17190         val = *BXE_SP(sc, wb_data[0]);
17191 
17192         /* do internal memory self test */
17193         if ((val == 0) && bxe_int_mem_test(sc)) {
17194             BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17195             return (-1);
17196         }
17197     }
17198 
17199     bxe_setup_fan_failure_detection(sc);
17200 
17201     /* clear PXP2 attentions */
17202     REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17203 
17204     bxe_enable_blocks_attention(sc);
17205 
17206     if (!CHIP_REV_IS_SLOW(sc)) {
17207         ecore_enable_blocks_parity(sc);
17208     }
17209 
17210     if (!BXE_NOMCP(sc)) {
17211         if (CHIP_IS_E1x(sc)) {
17212             bxe_common_init_phy(sc);
17213         }
17214     }
17215 
17216     return (0);
17217 }
17218 
17219 /**
17220  * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17221  *
17222  * @sc:     driver handle
17223  */
17224 static int
17225 bxe_init_hw_common_chip(struct bxe_softc *sc)
17226 {
17227     int rc = bxe_init_hw_common(sc);
17228 
17229     if (rc) {
17230         BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17231         return (rc);
17232     }
17233 
17234     /* In E2 2-PORT mode, same ext phy is used for the two paths */
17235     if (!BXE_NOMCP(sc)) {
17236         bxe_common_init_phy(sc);
17237     }
17238 
17239     return (0);
17240 }
17241 
17242 static int
17243 bxe_init_hw_port(struct bxe_softc *sc)
17244 {
17245     int port = SC_PORT(sc);
17246     int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17247     uint32_t low, high;
17248     uint32_t val;
17249 
17250     BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17251 
17252     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17253 
17254     ecore_init_block(sc, BLOCK_MISC, init_phase);
17255     ecore_init_block(sc, BLOCK_PXP, init_phase);
17256     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17257 
17258     /*
17259      * Timers bug workaround: disables the pf_master bit in pglue at
17260      * common phase, we need to enable it here before any dmae access are
17261      * attempted. Therefore we manually added the enable-master to the
17262      * port phase (it also happens in the function phase)
17263      */
17264     if (!CHIP_IS_E1x(sc)) {
17265         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17266     }
17267 
17268     ecore_init_block(sc, BLOCK_ATC, init_phase);
17269     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17270     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17271     ecore_init_block(sc, BLOCK_QM, init_phase);
17272 
17273     ecore_init_block(sc, BLOCK_TCM, init_phase);
17274     ecore_init_block(sc, BLOCK_UCM, init_phase);
17275     ecore_init_block(sc, BLOCK_CCM, init_phase);
17276     ecore_init_block(sc, BLOCK_XCM, init_phase);
17277 
17278     /* QM cid (connection) count */
17279     ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17280 
17281     if (CNIC_SUPPORT(sc)) {
17282         ecore_init_block(sc, BLOCK_TM, init_phase);
17283         REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17284         REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17285     }
17286 
17287     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17288 
17289     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17290 
17291     if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17292         if (IS_MF(sc)) {
17293             low = (BXE_ONE_PORT(sc) ? 160 : 246);
17294         } else if (sc->mtu > 4096) {
17295             if (BXE_ONE_PORT(sc)) {
17296                 low = 160;
17297             } else {
17298                 val = sc->mtu;
17299                 /* (24*1024 + val*4)/256 */
17300                 low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17301             }
17302         } else {
17303             low = (BXE_ONE_PORT(sc) ? 80 : 160);
17304         }
17305         high = (low + 56); /* 14*1024/256 */
17306         REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17307         REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17308     }
17309 
17310     if (CHIP_IS_MODE_4_PORT(sc)) {
17311         REG_WR(sc, SC_PORT(sc) ?
17312                BRB1_REG_MAC_GUARANTIED_1 :
17313                BRB1_REG_MAC_GUARANTIED_0, 40);
17314     }
17315 
17316     ecore_init_block(sc, BLOCK_PRS, init_phase);
17317     if (CHIP_IS_E3B0(sc)) {
17318         if (IS_MF_AFEX(sc)) {
17319             /* configure headers for AFEX mode */
17320             REG_WR(sc, SC_PORT(sc) ?
17321                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17322                    PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17323             REG_WR(sc, SC_PORT(sc) ?
17324                    PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17325                    PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17326             REG_WR(sc, SC_PORT(sc) ?
17327                    PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17328                    PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17329         } else {
17330             /* Ovlan exists only if we are in multi-function +
17331              * switch-dependent mode, in switch-independent there
17332              * is no ovlan headers
17333              */
17334             REG_WR(sc, SC_PORT(sc) ?
17335                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17336                    PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17337                    (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17338         }
17339     }
17340 
17341     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17342     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17343     ecore_init_block(sc, BLOCK_USDM, init_phase);
17344     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17345 
17346     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17347     ecore_init_block(sc, BLOCK_USEM, init_phase);
17348     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17349     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17350 
17351     ecore_init_block(sc, BLOCK_UPB, init_phase);
17352     ecore_init_block(sc, BLOCK_XPB, init_phase);
17353 
17354     ecore_init_block(sc, BLOCK_PBF, init_phase);
17355 
17356     if (CHIP_IS_E1x(sc)) {
17357         /* configure PBF to work without PAUSE mtu 9000 */
17358         REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17359 
17360         /* update threshold */
17361         REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17362         /* update init credit */
17363         REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17364 
17365         /* probe changes */
17366         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17367         DELAY(50);
17368         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17369     }
17370 
17371     if (CNIC_SUPPORT(sc)) {
17372         ecore_init_block(sc, BLOCK_SRC, init_phase);
17373     }
17374 
17375     ecore_init_block(sc, BLOCK_CDU, init_phase);
17376     ecore_init_block(sc, BLOCK_CFC, init_phase);
17377 
17378     if (CHIP_IS_E1(sc)) {
17379         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17380         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17381     }
17382     ecore_init_block(sc, BLOCK_HC, init_phase);
17383 
17384     ecore_init_block(sc, BLOCK_IGU, init_phase);
17385 
17386     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17387     /* init aeu_mask_attn_func_0/1:
17388      *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17389      *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17390      *             bits 4-7 are used for "per vn group attention" */
17391     val = IS_MF(sc) ? 0xF7 : 0x7;
17392     /* Enable DCBX attention for all but E1 */
17393     val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17394     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17395 
17396     ecore_init_block(sc, BLOCK_NIG, init_phase);
17397 
17398     if (!CHIP_IS_E1x(sc)) {
17399         /* Bit-map indicating which L2 hdrs may appear after the
17400          * basic Ethernet header
17401          */
17402         if (IS_MF_AFEX(sc)) {
17403             REG_WR(sc, SC_PORT(sc) ?
17404                    NIG_REG_P1_HDRS_AFTER_BASIC :
17405                    NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17406         } else {
17407             REG_WR(sc, SC_PORT(sc) ?
17408                    NIG_REG_P1_HDRS_AFTER_BASIC :
17409                    NIG_REG_P0_HDRS_AFTER_BASIC,
17410                    IS_MF_SD(sc) ? 7 : 6);
17411         }
17412 
17413         if (CHIP_IS_E3(sc)) {
17414             REG_WR(sc, SC_PORT(sc) ?
17415                    NIG_REG_LLH1_MF_MODE :
17416                    NIG_REG_LLH_MF_MODE, IS_MF(sc));
17417         }
17418     }
17419     if (!CHIP_IS_E3(sc)) {
17420         REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17421     }
17422 
17423     if (!CHIP_IS_E1(sc)) {
17424         /* 0x2 disable mf_ov, 0x1 enable */
17425         REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17426                (IS_MF_SD(sc) ? 0x1 : 0x2));
17427 
17428         if (!CHIP_IS_E1x(sc)) {
17429             val = 0;
17430             switch (sc->devinfo.mf_info.mf_mode) {
17431             case MULTI_FUNCTION_SD:
17432                 val = 1;
17433                 break;
17434             case MULTI_FUNCTION_SI:
17435             case MULTI_FUNCTION_AFEX:
17436                 val = 2;
17437                 break;
17438             }
17439 
17440             REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17441                         NIG_REG_LLH0_CLS_TYPE), val);
17442         }
17443         REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17444         REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17445         REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17446     }
17447 
17448     /* If SPIO5 is set to generate interrupts, enable it for this port */
17449     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17450     if (val & MISC_SPIO_SPIO5) {
17451         uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17452                                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17453         val = REG_RD(sc, reg_addr);
17454         val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17455         REG_WR(sc, reg_addr, val);
17456     }
17457 
17458     return (0);
17459 }
17460 
17461 static uint32_t
17462 bxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17463                        uint32_t         reg,
17464                        uint32_t         expected,
17465                        uint32_t         poll_count)
17466 {
17467     uint32_t cur_cnt = poll_count;
17468     uint32_t val;
17469 
17470     while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17471         DELAY(FLR_WAIT_INTERVAL);
17472     }
17473 
17474     return (val);
17475 }
17476 
17477 static int
17478 bxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17479                               uint32_t         reg,
17480                               char             *msg,
17481                               uint32_t         poll_cnt)
17482 {
17483     uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17484 
17485     if (val != 0) {
17486         BLOGE(sc, "%s usage count=%d\n", msg, val);
17487         return (1);
17488     }
17489 
17490     return (0);
17491 }
17492 
17493 /* Common routines with VF FLR cleanup */
17494 static uint32_t
17495 bxe_flr_clnup_poll_count(struct bxe_softc *sc)
17496 {
17497     /* adjust polling timeout */
17498     if (CHIP_REV_IS_EMUL(sc)) {
17499         return (FLR_POLL_CNT * 2000);
17500     }
17501 
17502     if (CHIP_REV_IS_FPGA(sc)) {
17503         return (FLR_POLL_CNT * 120);
17504     }
17505 
17506     return (FLR_POLL_CNT);
17507 }
17508 
17509 static int
17510 bxe_poll_hw_usage_counters(struct bxe_softc *sc,
17511                            uint32_t         poll_cnt)
17512 {
17513     /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17514     if (bxe_flr_clnup_poll_hw_counter(sc,
17515                                       CFC_REG_NUM_LCIDS_INSIDE_PF,
17516                                       "CFC PF usage counter timed out",
17517                                       poll_cnt)) {
17518         return (1);
17519     }
17520 
17521     /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17522     if (bxe_flr_clnup_poll_hw_counter(sc,
17523                                       DORQ_REG_PF_USAGE_CNT,
17524                                       "DQ PF usage counter timed out",
17525                                       poll_cnt)) {
17526         return (1);
17527     }
17528 
17529     /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17530     if (bxe_flr_clnup_poll_hw_counter(sc,
17531                                       QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17532                                       "QM PF usage counter timed out",
17533                                       poll_cnt)) {
17534         return (1);
17535     }
17536 
17537     /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17538     if (bxe_flr_clnup_poll_hw_counter(sc,
17539                                       TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17540                                       "Timers VNIC usage counter timed out",
17541                                       poll_cnt)) {
17542         return (1);
17543     }
17544 
17545     if (bxe_flr_clnup_poll_hw_counter(sc,
17546                                       TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17547                                       "Timers NUM_SCANS usage counter timed out",
17548                                       poll_cnt)) {
17549         return (1);
17550     }
17551 
17552     /* Wait DMAE PF usage counter to zero */
17553     if (bxe_flr_clnup_poll_hw_counter(sc,
17554                                       dmae_reg_go_c[INIT_DMAE_C(sc)],
17555                                       "DMAE dommand register timed out",
17556                                       poll_cnt)) {
17557         return (1);
17558     }
17559 
17560     return (0);
17561 }
17562 
17563 #define OP_GEN_PARAM(param)                                            \
17564     (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17565 #define OP_GEN_TYPE(type)                                           \
17566     (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17567 #define OP_GEN_AGG_VECT(index)                                             \
17568     (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17569 
17570 static int
17571 bxe_send_final_clnup(struct bxe_softc *sc,
17572                      uint8_t          clnup_func,
17573                      uint32_t         poll_cnt)
17574 {
17575     uint32_t op_gen_command = 0;
17576     uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17577                           CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17578     int ret = 0;
17579 
17580     if (REG_RD(sc, comp_addr)) {
17581         BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17582         return (1);
17583     }
17584 
17585     op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17586     op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17587     op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17588     op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17589 
17590     BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17591     REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17592 
17593     if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17594         BLOGE(sc, "FW final cleanup did not succeed\n");
17595         BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17596               (REG_RD(sc, comp_addr)));
17597         bxe_panic(sc, ("FLR cleanup failed\n"));
17598         return (1);
17599     }
17600 
17601     /* Zero completion for nxt FLR */
17602     REG_WR(sc, comp_addr, 0);
17603 
17604     return (ret);
17605 }
17606 
17607 static void
17608 bxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17609                        struct pbf_pN_buf_regs *regs,
17610                        uint32_t               poll_count)
17611 {
17612     uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17613     uint32_t cur_cnt = poll_count;
17614 
17615     crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
17616     crd = crd_start = REG_RD(sc, regs->crd);
17617     init_crd = REG_RD(sc, regs->init_crd);
17618 
17619     BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
17620     BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
17621     BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
17622 
17623     while ((crd != init_crd) &&
17624            ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
17625             (init_crd - crd_start))) {
17626         if (cur_cnt--) {
17627             DELAY(FLR_WAIT_INTERVAL);
17628             crd = REG_RD(sc, regs->crd);
17629             crd_freed = REG_RD(sc, regs->crd_freed);
17630         } else {
17631             BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
17632             BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
17633             BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
17634             break;
17635         }
17636     }
17637 
17638     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
17639           poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17640 }
17641 
17642 static void
17643 bxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
17644                        struct pbf_pN_cmd_regs *regs,
17645                        uint32_t               poll_count)
17646 {
17647     uint32_t occup, to_free, freed, freed_start;
17648     uint32_t cur_cnt = poll_count;
17649 
17650     occup = to_free = REG_RD(sc, regs->lines_occup);
17651     freed = freed_start = REG_RD(sc, regs->lines_freed);
17652 
17653     BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17654     BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17655 
17656     while (occup &&
17657            ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
17658         if (cur_cnt--) {
17659             DELAY(FLR_WAIT_INTERVAL);
17660             occup = REG_RD(sc, regs->lines_occup);
17661             freed = REG_RD(sc, regs->lines_freed);
17662         } else {
17663             BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
17664             BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
17665             BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
17666             break;
17667         }
17668     }
17669 
17670     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
17671           poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
17672 }
17673 
17674 static void
17675 bxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
17676 {
17677     struct pbf_pN_cmd_regs cmd_regs[] = {
17678         {0, (CHIP_IS_E3B0(sc)) ?
17679             PBF_REG_TQ_OCCUPANCY_Q0 :
17680             PBF_REG_P0_TQ_OCCUPANCY,
17681             (CHIP_IS_E3B0(sc)) ?
17682             PBF_REG_TQ_LINES_FREED_CNT_Q0 :
17683             PBF_REG_P0_TQ_LINES_FREED_CNT},
17684         {1, (CHIP_IS_E3B0(sc)) ?
17685             PBF_REG_TQ_OCCUPANCY_Q1 :
17686             PBF_REG_P1_TQ_OCCUPANCY,
17687             (CHIP_IS_E3B0(sc)) ?
17688             PBF_REG_TQ_LINES_FREED_CNT_Q1 :
17689             PBF_REG_P1_TQ_LINES_FREED_CNT},
17690         {4, (CHIP_IS_E3B0(sc)) ?
17691             PBF_REG_TQ_OCCUPANCY_LB_Q :
17692             PBF_REG_P4_TQ_OCCUPANCY,
17693             (CHIP_IS_E3B0(sc)) ?
17694             PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
17695             PBF_REG_P4_TQ_LINES_FREED_CNT}
17696     };
17697 
17698     struct pbf_pN_buf_regs buf_regs[] = {
17699         {0, (CHIP_IS_E3B0(sc)) ?
17700             PBF_REG_INIT_CRD_Q0 :
17701             PBF_REG_P0_INIT_CRD ,
17702             (CHIP_IS_E3B0(sc)) ?
17703             PBF_REG_CREDIT_Q0 :
17704             PBF_REG_P0_CREDIT,
17705             (CHIP_IS_E3B0(sc)) ?
17706             PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
17707             PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
17708         {1, (CHIP_IS_E3B0(sc)) ?
17709             PBF_REG_INIT_CRD_Q1 :
17710             PBF_REG_P1_INIT_CRD,
17711             (CHIP_IS_E3B0(sc)) ?
17712             PBF_REG_CREDIT_Q1 :
17713             PBF_REG_P1_CREDIT,
17714             (CHIP_IS_E3B0(sc)) ?
17715             PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
17716             PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
17717         {4, (CHIP_IS_E3B0(sc)) ?
17718             PBF_REG_INIT_CRD_LB_Q :
17719             PBF_REG_P4_INIT_CRD,
17720             (CHIP_IS_E3B0(sc)) ?
17721             PBF_REG_CREDIT_LB_Q :
17722             PBF_REG_P4_CREDIT,
17723             (CHIP_IS_E3B0(sc)) ?
17724             PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
17725             PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
17726     };
17727 
17728     int i;
17729 
17730     /* Verify the command queues are flushed P0, P1, P4 */
17731     for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
17732         bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
17733     }
17734 
17735     /* Verify the transmission buffers are flushed P0, P1, P4 */
17736     for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
17737         bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
17738     }
17739 }
17740 
17741 static void
17742 bxe_hw_enable_status(struct bxe_softc *sc)
17743 {
17744     uint32_t val;
17745 
17746     val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
17747     BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
17748 
17749     val = REG_RD(sc, PBF_REG_DISABLE_PF);
17750     BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
17751 
17752     val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
17753     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
17754 
17755     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
17756     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
17757 
17758     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
17759     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
17760 
17761     val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
17762     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
17763 
17764     val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
17765     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
17766 
17767     val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
17768     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
17769 }
17770 
17771 static int
17772 bxe_pf_flr_clnup(struct bxe_softc *sc)
17773 {
17774     uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
17775 
17776     BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
17777 
17778     /* Re-enable PF target read access */
17779     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
17780 
17781     /* Poll HW usage counters */
17782     BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
17783     if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
17784         return (-1);
17785     }
17786 
17787     /* Zero the igu 'trailing edge' and 'leading edge' */
17788 
17789     /* Send the FW cleanup command */
17790     if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
17791         return (-1);
17792     }
17793 
17794     /* ATC cleanup */
17795 
17796     /* Verify TX hw is flushed */
17797     bxe_tx_hw_flushed(sc, poll_cnt);
17798 
17799     /* Wait 100ms (not adjusted according to platform) */
17800     DELAY(100000);
17801 
17802     /* Verify no pending pci transactions */
17803     if (bxe_is_pcie_pending(sc)) {
17804         BLOGE(sc, "PCIE Transactions still pending\n");
17805     }
17806 
17807     /* Debug */
17808     bxe_hw_enable_status(sc);
17809 
17810     /*
17811      * Master enable - Due to WB DMAE writes performed before this
17812      * register is re-initialized as part of the regular function init
17813      */
17814     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17815 
17816     return (0);
17817 }
17818 
17819 static int
17820 bxe_init_hw_func(struct bxe_softc *sc)
17821 {
17822     int port = SC_PORT(sc);
17823     int func = SC_FUNC(sc);
17824     int init_phase = PHASE_PF0 + func;
17825     struct ecore_ilt *ilt = sc->ilt;
17826     uint16_t cdu_ilt_start;
17827     uint32_t addr, val;
17828     uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
17829     int i, main_mem_width, rc;
17830 
17831     BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
17832 
17833     /* FLR cleanup */
17834     if (!CHIP_IS_E1x(sc)) {
17835         rc = bxe_pf_flr_clnup(sc);
17836         if (rc) {
17837             BLOGE(sc, "FLR cleanup failed!\n");
17838             // XXX bxe_fw_dump(sc);
17839             // XXX bxe_idle_chk(sc);
17840             return (rc);
17841         }
17842     }
17843 
17844     /* set MSI reconfigure capability */
17845     if (sc->devinfo.int_block == INT_BLOCK_HC) {
17846         addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
17847         val = REG_RD(sc, addr);
17848         val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
17849         REG_WR(sc, addr, val);
17850     }
17851 
17852     ecore_init_block(sc, BLOCK_PXP, init_phase);
17853     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17854 
17855     ilt = sc->ilt;
17856     cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
17857 
17858     for (i = 0; i < L2_ILT_LINES(sc); i++) {
17859         ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
17860         ilt->lines[cdu_ilt_start + i].page_mapping =
17861             sc->context[i].vcxt_dma.paddr;
17862         ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
17863     }
17864     ecore_ilt_init_op(sc, INITOP_SET);
17865 
17866     /* Set NIC mode */
17867     REG_WR(sc, PRS_REG_NIC_MODE, 1);
17868     BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
17869 
17870     if (!CHIP_IS_E1x(sc)) {
17871         uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
17872 
17873         /* Turn on a single ISR mode in IGU if driver is going to use
17874          * INT#x or MSI
17875          */
17876         if (sc->interrupt_mode != INTR_MODE_MSIX) {
17877             pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
17878         }
17879 
17880         /*
17881          * Timers workaround bug: function init part.
17882          * Need to wait 20msec after initializing ILT,
17883          * needed to make sure there are no requests in
17884          * one of the PXP internal queues with "old" ILT addresses
17885          */
17886         DELAY(20000);
17887 
17888         /*
17889          * Master enable - Due to WB DMAE writes performed before this
17890          * register is re-initialized as part of the regular function
17891          * init
17892          */
17893         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17894         /* Enable the function in IGU */
17895         REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
17896     }
17897 
17898     sc->dmae_ready = 1;
17899 
17900     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17901 
17902     if (!CHIP_IS_E1x(sc))
17903         REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
17904 
17905     ecore_init_block(sc, BLOCK_ATC, init_phase);
17906     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17907     ecore_init_block(sc, BLOCK_NIG, init_phase);
17908     ecore_init_block(sc, BLOCK_SRC, init_phase);
17909     ecore_init_block(sc, BLOCK_MISC, init_phase);
17910     ecore_init_block(sc, BLOCK_TCM, init_phase);
17911     ecore_init_block(sc, BLOCK_UCM, init_phase);
17912     ecore_init_block(sc, BLOCK_CCM, init_phase);
17913     ecore_init_block(sc, BLOCK_XCM, init_phase);
17914     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17915     ecore_init_block(sc, BLOCK_USEM, init_phase);
17916     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17917     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17918 
17919     if (!CHIP_IS_E1x(sc))
17920         REG_WR(sc, QM_REG_PF_EN, 1);
17921 
17922     if (!CHIP_IS_E1x(sc)) {
17923         REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17924         REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17925         REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17926         REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
17927     }
17928     ecore_init_block(sc, BLOCK_QM, init_phase);
17929 
17930     ecore_init_block(sc, BLOCK_TM, init_phase);
17931     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17932 
17933     bxe_iov_init_dq(sc);
17934 
17935     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17936     ecore_init_block(sc, BLOCK_PRS, init_phase);
17937     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17938     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17939     ecore_init_block(sc, BLOCK_USDM, init_phase);
17940     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17941     ecore_init_block(sc, BLOCK_UPB, init_phase);
17942     ecore_init_block(sc, BLOCK_XPB, init_phase);
17943     ecore_init_block(sc, BLOCK_PBF, init_phase);
17944     if (!CHIP_IS_E1x(sc))
17945         REG_WR(sc, PBF_REG_DISABLE_PF, 0);
17946 
17947     ecore_init_block(sc, BLOCK_CDU, init_phase);
17948 
17949     ecore_init_block(sc, BLOCK_CFC, init_phase);
17950 
17951     if (!CHIP_IS_E1x(sc))
17952         REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
17953 
17954     if (IS_MF(sc)) {
17955         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
17956         REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
17957     }
17958 
17959     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17960 
17961     /* HC init per function */
17962     if (sc->devinfo.int_block == INT_BLOCK_HC) {
17963         if (CHIP_IS_E1H(sc)) {
17964             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17965 
17966             REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17967             REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17968         }
17969         ecore_init_block(sc, BLOCK_HC, init_phase);
17970 
17971     } else {
17972         int num_segs, sb_idx, prod_offset;
17973 
17974         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
17975 
17976         if (!CHIP_IS_E1x(sc)) {
17977             REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
17978             REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
17979         }
17980 
17981         ecore_init_block(sc, BLOCK_IGU, init_phase);
17982 
17983         if (!CHIP_IS_E1x(sc)) {
17984             int dsb_idx = 0;
17985             /**
17986              * Producer memory:
17987              * E2 mode: address 0-135 match to the mapping memory;
17988              * 136 - PF0 default prod; 137 - PF1 default prod;
17989              * 138 - PF2 default prod; 139 - PF3 default prod;
17990              * 140 - PF0 attn prod;    141 - PF1 attn prod;
17991              * 142 - PF2 attn prod;    143 - PF3 attn prod;
17992              * 144-147 reserved.
17993              *
17994              * E1.5 mode - In backward compatible mode;
17995              * for non default SB; each even line in the memory
17996              * holds the U producer and each odd line hold
17997              * the C producer. The first 128 producers are for
17998              * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
17999              * producers are for the DSB for each PF.
18000              * Each PF has five segments: (the order inside each
18001              * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
18002              * 132-135 C prods; 136-139 X prods; 140-143 T prods;
18003              * 144-147 attn prods;
18004              */
18005             /* non-default-status-blocks */
18006             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18007                 IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
18008             for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
18009                 prod_offset = (sc->igu_base_sb + sb_idx) *
18010                     num_segs;
18011 
18012                 for (i = 0; i < num_segs; i++) {
18013                     addr = IGU_REG_PROD_CONS_MEMORY +
18014                             (prod_offset + i) * 4;
18015                     REG_WR(sc, addr, 0);
18016                 }
18017                 /* send consumer update with value 0 */
18018                 bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
18019                            USTORM_ID, 0, IGU_INT_NOP, 1);
18020                 bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
18021             }
18022 
18023             /* default-status-blocks */
18024             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18025                 IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
18026 
18027             if (CHIP_IS_MODE_4_PORT(sc))
18028                 dsb_idx = SC_FUNC(sc);
18029             else
18030                 dsb_idx = SC_VN(sc);
18031 
18032             prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
18033                        IGU_BC_BASE_DSB_PROD + dsb_idx :
18034                        IGU_NORM_BASE_DSB_PROD + dsb_idx);
18035 
18036             /*
18037              * igu prods come in chunks of E1HVN_MAX (4) -
18038              * does not matters what is the current chip mode
18039              */
18040             for (i = 0; i < (num_segs * E1HVN_MAX);
18041                  i += E1HVN_MAX) {
18042                 addr = IGU_REG_PROD_CONS_MEMORY +
18043                             (prod_offset + i)*4;
18044                 REG_WR(sc, addr, 0);
18045             }
18046             /* send consumer update with 0 */
18047             if (CHIP_INT_MODE_IS_BC(sc)) {
18048                 bxe_ack_sb(sc, sc->igu_dsb_id,
18049                            USTORM_ID, 0, IGU_INT_NOP, 1);
18050                 bxe_ack_sb(sc, sc->igu_dsb_id,
18051                            CSTORM_ID, 0, IGU_INT_NOP, 1);
18052                 bxe_ack_sb(sc, sc->igu_dsb_id,
18053                            XSTORM_ID, 0, IGU_INT_NOP, 1);
18054                 bxe_ack_sb(sc, sc->igu_dsb_id,
18055                            TSTORM_ID, 0, IGU_INT_NOP, 1);
18056                 bxe_ack_sb(sc, sc->igu_dsb_id,
18057                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
18058             } else {
18059                 bxe_ack_sb(sc, sc->igu_dsb_id,
18060                            USTORM_ID, 0, IGU_INT_NOP, 1);
18061                 bxe_ack_sb(sc, sc->igu_dsb_id,
18062                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
18063             }
18064             bxe_igu_clear_sb(sc, sc->igu_dsb_id);
18065 
18066             /* !!! these should become driver const once
18067                rf-tool supports split-68 const */
18068             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
18069             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
18070             REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
18071             REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
18072             REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
18073             REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
18074         }
18075     }
18076 
18077     /* Reset PCIE errors for debug */
18078     REG_WR(sc, 0x2114, 0xffffffff);
18079     REG_WR(sc, 0x2120, 0xffffffff);
18080 
18081     if (CHIP_IS_E1x(sc)) {
18082         main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
18083         main_mem_base = HC_REG_MAIN_MEMORY +
18084                 SC_PORT(sc) * (main_mem_size * 4);
18085         main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
18086         main_mem_width = 8;
18087 
18088         val = REG_RD(sc, main_mem_prty_clr);
18089         if (val) {
18090             BLOGD(sc, DBG_LOAD,
18091                   "Parity errors in HC block during function init (0x%x)!\n",
18092                   val);
18093         }
18094 
18095         /* Clear "false" parity errors in MSI-X table */
18096         for (i = main_mem_base;
18097              i < main_mem_base + main_mem_size * 4;
18098              i += main_mem_width) {
18099             bxe_read_dmae(sc, i, main_mem_width / 4);
18100             bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
18101                            i, main_mem_width / 4);
18102         }
18103         /* Clear HC parity attention */
18104         REG_RD(sc, main_mem_prty_clr);
18105     }
18106 
18107 #if 1
18108     /* Enable STORMs SP logging */
18109     REG_WR8(sc, BAR_USTRORM_INTMEM +
18110            USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18111     REG_WR8(sc, BAR_TSTRORM_INTMEM +
18112            TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18113     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18114            CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18115     REG_WR8(sc, BAR_XSTRORM_INTMEM +
18116            XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18117 #endif
18118 
18119     elink_phy_probe(&sc->link_params);
18120 
18121     return (0);
18122 }
18123 
18124 static void
18125 bxe_link_reset(struct bxe_softc *sc)
18126 {
18127     if (!BXE_NOMCP(sc)) {
18128 	bxe_acquire_phy_lock(sc);
18129         elink_lfa_reset(&sc->link_params, &sc->link_vars);
18130 	bxe_release_phy_lock(sc);
18131     } else {
18132         if (!CHIP_REV_IS_SLOW(sc)) {
18133             BLOGW(sc, "Bootcode is missing - cannot reset link\n");
18134         }
18135     }
18136 }
18137 
18138 static void
18139 bxe_reset_port(struct bxe_softc *sc)
18140 {
18141     int port = SC_PORT(sc);
18142     uint32_t val;
18143 
18144     /* reset physical Link */
18145     bxe_link_reset(sc);
18146 
18147     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18148 
18149     /* Do not rcv packets to BRB */
18150     REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18151     /* Do not direct rcv packets that are not for MCP to the BRB */
18152     REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18153                NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18154 
18155     /* Configure AEU */
18156     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18157 
18158     DELAY(100000);
18159 
18160     /* Check for BRB port occupancy */
18161     val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18162     if (val) {
18163         BLOGD(sc, DBG_LOAD,
18164               "BRB1 is not empty, %d blocks are occupied\n", val);
18165     }
18166 
18167     /* TODO: Close Doorbell port? */
18168 }
18169 
18170 static void
18171 bxe_ilt_wr(struct bxe_softc *sc,
18172            uint32_t         index,
18173            bus_addr_t       addr)
18174 {
18175     int reg;
18176     uint32_t wb_write[2];
18177 
18178     if (CHIP_IS_E1(sc)) {
18179         reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18180     } else {
18181         reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18182     }
18183 
18184     wb_write[0] = ONCHIP_ADDR1(addr);
18185     wb_write[1] = ONCHIP_ADDR2(addr);
18186     REG_WR_DMAE(sc, reg, wb_write, 2);
18187 }
18188 
18189 static void
18190 bxe_clear_func_ilt(struct bxe_softc *sc,
18191                    uint32_t         func)
18192 {
18193     uint32_t i, base = FUNC_ILT_BASE(func);
18194     for (i = base; i < base + ILT_PER_FUNC; i++) {
18195         bxe_ilt_wr(sc, i, 0);
18196     }
18197 }
18198 
18199 static void
18200 bxe_reset_func(struct bxe_softc *sc)
18201 {
18202     struct bxe_fastpath *fp;
18203     int port = SC_PORT(sc);
18204     int func = SC_FUNC(sc);
18205     int i;
18206 
18207     /* Disable the function in the FW */
18208     REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18209     REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18210     REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18211     REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18212 
18213     /* FP SBs */
18214     FOR_EACH_ETH_QUEUE(sc, i) {
18215         fp = &sc->fp[i];
18216         REG_WR8(sc, BAR_CSTRORM_INTMEM +
18217                 CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18218                 SB_DISABLED);
18219     }
18220 
18221     /* SP SB */
18222     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18223             CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18224             SB_DISABLED);
18225 
18226     for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18227         REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18228     }
18229 
18230     /* Configure IGU */
18231     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18232         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18233         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18234     } else {
18235         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18236         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18237     }
18238 
18239     if (CNIC_LOADED(sc)) {
18240         /* Disable Timer scan */
18241         REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18242         /*
18243          * Wait for at least 10ms and up to 2 second for the timers
18244          * scan to complete
18245          */
18246         for (i = 0; i < 200; i++) {
18247             DELAY(10000);
18248             if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18249                 break;
18250         }
18251     }
18252 
18253     /* Clear ILT */
18254     bxe_clear_func_ilt(sc, func);
18255 
18256     /*
18257      * Timers workaround bug for E2: if this is vnic-3,
18258      * we need to set the entire ilt range for this timers.
18259      */
18260     if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18261         struct ilt_client_info ilt_cli;
18262         /* use dummy TM client */
18263         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18264         ilt_cli.start = 0;
18265         ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18266         ilt_cli.client_num = ILT_CLIENT_TM;
18267 
18268         ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18269     }
18270 
18271     /* this assumes that reset_port() called before reset_func()*/
18272     if (!CHIP_IS_E1x(sc)) {
18273         bxe_pf_disable(sc);
18274     }
18275 
18276     sc->dmae_ready = 0;
18277 }
18278 
18279 static int
18280 bxe_gunzip_init(struct bxe_softc *sc)
18281 {
18282     return (0);
18283 }
18284 
18285 static void
18286 bxe_gunzip_end(struct bxe_softc *sc)
18287 {
18288     return;
18289 }
18290 
18291 static int
18292 bxe_init_firmware(struct bxe_softc *sc)
18293 {
18294     if (CHIP_IS_E1(sc)) {
18295         ecore_init_e1_firmware(sc);
18296         sc->iro_array = e1_iro_arr;
18297     } else if (CHIP_IS_E1H(sc)) {
18298         ecore_init_e1h_firmware(sc);
18299         sc->iro_array = e1h_iro_arr;
18300     } else if (!CHIP_IS_E1x(sc)) {
18301         ecore_init_e2_firmware(sc);
18302         sc->iro_array = e2_iro_arr;
18303     } else {
18304         BLOGE(sc, "Unsupported chip revision\n");
18305         return (-1);
18306     }
18307 
18308     return (0);
18309 }
18310 
18311 static void
18312 bxe_release_firmware(struct bxe_softc *sc)
18313 {
18314     /* Do nothing */
18315     return;
18316 }
18317 
18318 static int
18319 ecore_gunzip(struct bxe_softc *sc,
18320              const uint8_t    *zbuf,
18321              int              len)
18322 {
18323     /* XXX : Implement... */
18324     BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18325     return (FALSE);
18326 }
18327 
18328 static void
18329 ecore_reg_wr_ind(struct bxe_softc *sc,
18330                  uint32_t         addr,
18331                  uint32_t         val)
18332 {
18333     bxe_reg_wr_ind(sc, addr, val);
18334 }
18335 
18336 static void
18337 ecore_write_dmae_phys_len(struct bxe_softc *sc,
18338                           bus_addr_t       phys_addr,
18339                           uint32_t         addr,
18340                           uint32_t         len)
18341 {
18342     bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18343 }
18344 
18345 void
18346 ecore_storm_memset_struct(struct bxe_softc *sc,
18347                           uint32_t         addr,
18348                           size_t           size,
18349                           uint32_t         *data)
18350 {
18351     uint8_t i;
18352     for (i = 0; i < size/4; i++) {
18353         REG_WR(sc, addr + (i * 4), data[i]);
18354     }
18355 }
18356 
18357 
18358 /*
18359  * character device - ioctl interface definitions
18360  */
18361 
18362 
18363 #include "bxe_dump.h"
18364 #include "bxe_ioctl.h"
18365 #include <sys/conf.h>
18366 
18367 static int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18368                 struct thread *td);
18369 
18370 static struct cdevsw bxe_cdevsw = {
18371     .d_version = D_VERSION,
18372     .d_ioctl = bxe_eioctl,
18373     .d_name = "bxecnic",
18374 };
18375 
18376 #define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18377 
18378 
18379 #define DUMP_ALL_PRESETS        0x1FFF
18380 #define DUMP_MAX_PRESETS        13
18381 #define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18382 #define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18383 #define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18384 #define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18385 #define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18386 
18387 #define IS_REG_IN_PRESET(presets, idx)  \
18388                 ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18389 
18390 
18391 static int
18392 bxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18393 {
18394     if (CHIP_IS_E1(sc))
18395         return dump_num_registers[0][preset-1];
18396     else if (CHIP_IS_E1H(sc))
18397         return dump_num_registers[1][preset-1];
18398     else if (CHIP_IS_E2(sc))
18399         return dump_num_registers[2][preset-1];
18400     else if (CHIP_IS_E3A0(sc))
18401         return dump_num_registers[3][preset-1];
18402     else if (CHIP_IS_E3B0(sc))
18403         return dump_num_registers[4][preset-1];
18404     else
18405         return 0;
18406 }
18407 
18408 static int
18409 bxe_get_total_regs_len32(struct bxe_softc *sc)
18410 {
18411     uint32_t preset_idx;
18412     int regdump_len32 = 0;
18413 
18414 
18415     /* Calculate the total preset regs length */
18416     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18417         regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18418     }
18419 
18420     return regdump_len32;
18421 }
18422 
18423 static const uint32_t *
18424 __bxe_get_page_addr_ar(struct bxe_softc *sc)
18425 {
18426     if (CHIP_IS_E2(sc))
18427         return page_vals_e2;
18428     else if (CHIP_IS_E3(sc))
18429         return page_vals_e3;
18430     else
18431         return NULL;
18432 }
18433 
18434 static uint32_t
18435 __bxe_get_page_reg_num(struct bxe_softc *sc)
18436 {
18437     if (CHIP_IS_E2(sc))
18438         return PAGE_MODE_VALUES_E2;
18439     else if (CHIP_IS_E3(sc))
18440         return PAGE_MODE_VALUES_E3;
18441     else
18442         return 0;
18443 }
18444 
18445 static const uint32_t *
18446 __bxe_get_page_write_ar(struct bxe_softc *sc)
18447 {
18448     if (CHIP_IS_E2(sc))
18449         return page_write_regs_e2;
18450     else if (CHIP_IS_E3(sc))
18451         return page_write_regs_e3;
18452     else
18453         return NULL;
18454 }
18455 
18456 static uint32_t
18457 __bxe_get_page_write_num(struct bxe_softc *sc)
18458 {
18459     if (CHIP_IS_E2(sc))
18460         return PAGE_WRITE_REGS_E2;
18461     else if (CHIP_IS_E3(sc))
18462         return PAGE_WRITE_REGS_E3;
18463     else
18464         return 0;
18465 }
18466 
18467 static const struct reg_addr *
18468 __bxe_get_page_read_ar(struct bxe_softc *sc)
18469 {
18470     if (CHIP_IS_E2(sc))
18471         return page_read_regs_e2;
18472     else if (CHIP_IS_E3(sc))
18473         return page_read_regs_e3;
18474     else
18475         return NULL;
18476 }
18477 
18478 static uint32_t
18479 __bxe_get_page_read_num(struct bxe_softc *sc)
18480 {
18481     if (CHIP_IS_E2(sc))
18482         return PAGE_READ_REGS_E2;
18483     else if (CHIP_IS_E3(sc))
18484         return PAGE_READ_REGS_E3;
18485     else
18486         return 0;
18487 }
18488 
18489 static bool
18490 bxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18491 {
18492     if (CHIP_IS_E1(sc))
18493         return IS_E1_REG(reg_info->chips);
18494     else if (CHIP_IS_E1H(sc))
18495         return IS_E1H_REG(reg_info->chips);
18496     else if (CHIP_IS_E2(sc))
18497         return IS_E2_REG(reg_info->chips);
18498     else if (CHIP_IS_E3A0(sc))
18499         return IS_E3A0_REG(reg_info->chips);
18500     else if (CHIP_IS_E3B0(sc))
18501         return IS_E3B0_REG(reg_info->chips);
18502     else
18503         return 0;
18504 }
18505 
18506 static bool
18507 bxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18508 {
18509     if (CHIP_IS_E1(sc))
18510         return IS_E1_REG(wreg_info->chips);
18511     else if (CHIP_IS_E1H(sc))
18512         return IS_E1H_REG(wreg_info->chips);
18513     else if (CHIP_IS_E2(sc))
18514         return IS_E2_REG(wreg_info->chips);
18515     else if (CHIP_IS_E3A0(sc))
18516         return IS_E3A0_REG(wreg_info->chips);
18517     else if (CHIP_IS_E3B0(sc))
18518         return IS_E3B0_REG(wreg_info->chips);
18519     else
18520         return 0;
18521 }
18522 
18523 /**
18524  * bxe_read_pages_regs - read "paged" registers
18525  *
18526  * @bp          device handle
18527  * @p           output buffer
18528  *
18529  * Reads "paged" memories: memories that may only be read by first writing to a
18530  * specific address ("write address") and then reading from a specific address
18531  * ("read address"). There may be more than one write address per "page" and
18532  * more than one read address per write address.
18533  */
18534 static void
18535 bxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18536 {
18537     uint32_t i, j, k, n;
18538 
18539     /* addresses of the paged registers */
18540     const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18541     /* number of paged registers */
18542     int num_pages = __bxe_get_page_reg_num(sc);
18543     /* write addresses */
18544     const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18545     /* number of write addresses */
18546     int write_num = __bxe_get_page_write_num(sc);
18547     /* read addresses info */
18548     const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18549     /* number of read addresses */
18550     int read_num = __bxe_get_page_read_num(sc);
18551     uint32_t addr, size;
18552 
18553     for (i = 0; i < num_pages; i++) {
18554         for (j = 0; j < write_num; j++) {
18555             REG_WR(sc, write_addr[j], page_addr[i]);
18556 
18557             for (k = 0; k < read_num; k++) {
18558                 if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18559                     size = read_addr[k].size;
18560                     for (n = 0; n < size; n++) {
18561                         addr = read_addr[k].addr + n*4;
18562                         *p++ = REG_RD(sc, addr);
18563                     }
18564                 }
18565             }
18566         }
18567     }
18568     return;
18569 }
18570 
18571 
18572 static int
18573 bxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18574 {
18575     uint32_t i, j, addr;
18576     const struct wreg_addr *wreg_addr_p = NULL;
18577 
18578     if (CHIP_IS_E1(sc))
18579         wreg_addr_p = &wreg_addr_e1;
18580     else if (CHIP_IS_E1H(sc))
18581         wreg_addr_p = &wreg_addr_e1h;
18582     else if (CHIP_IS_E2(sc))
18583         wreg_addr_p = &wreg_addr_e2;
18584     else if (CHIP_IS_E3A0(sc))
18585         wreg_addr_p = &wreg_addr_e3;
18586     else if (CHIP_IS_E3B0(sc))
18587         wreg_addr_p = &wreg_addr_e3b0;
18588     else
18589         return (-1);
18590 
18591     /* Read the idle_chk registers */
18592     for (i = 0; i < IDLE_REGS_COUNT; i++) {
18593         if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18594             IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18595             for (j = 0; j < idle_reg_addrs[i].size; j++)
18596                 *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18597         }
18598     }
18599 
18600     /* Read the regular registers */
18601     for (i = 0; i < REGS_COUNT; i++) {
18602         if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18603             IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18604             for (j = 0; j < reg_addrs[i].size; j++)
18605                 *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18606         }
18607     }
18608 
18609     /* Read the CAM registers */
18610     if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18611         IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18612         for (i = 0; i < wreg_addr_p->size; i++) {
18613             *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
18614 
18615             /* In case of wreg_addr register, read additional
18616                registers from read_regs array
18617              */
18618             for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
18619                 addr = *(wreg_addr_p->read_regs);
18620                 *p++ = REG_RD(sc, addr + j*4);
18621             }
18622         }
18623     }
18624 
18625     /* Paged registers are supported in E2 & E3 only */
18626     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
18627         /* Read "paged" registers */
18628         bxe_read_pages_regs(sc, p, preset);
18629     }
18630 
18631     return 0;
18632 }
18633 
18634 int
18635 bxe_grc_dump(struct bxe_softc *sc)
18636 {
18637     int rval = 0;
18638     uint32_t preset_idx;
18639     uint8_t *buf;
18640     uint32_t size;
18641     struct  dump_header *d_hdr;
18642     uint32_t i;
18643     uint32_t reg_val;
18644     uint32_t reg_addr;
18645     uint32_t cmd_offset;
18646     int context_size;
18647     int allocated;
18648     struct ecore_ilt *ilt = SC_ILT(sc);
18649     struct bxe_fastpath *fp;
18650     struct ilt_client_info *ilt_cli;
18651     int grc_dump_size;
18652 
18653 
18654     if (sc->grcdump_done || sc->grcdump_started)
18655 	return (rval);
18656 
18657     sc->grcdump_started = 1;
18658     BLOGI(sc, "Started collecting grcdump\n");
18659 
18660     grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
18661                 sizeof(struct  dump_header);
18662 
18663     sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
18664 
18665     if (sc->grc_dump == NULL) {
18666         BLOGW(sc, "Unable to allocate memory for grcdump collection\n");
18667         return(ENOMEM);
18668     }
18669 
18670 
18671 
18672     /* Disable parity attentions as long as following dump may
18673      * cause false alarms by reading never written registers. We
18674      * will re-enable parity attentions right after the dump.
18675      */
18676 
18677     /* Disable parity on path 0 */
18678     bxe_pretend_func(sc, 0);
18679 
18680     ecore_disable_blocks_parity(sc);
18681 
18682     /* Disable parity on path 1 */
18683     bxe_pretend_func(sc, 1);
18684     ecore_disable_blocks_parity(sc);
18685 
18686     /* Return to current function */
18687     bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18688 
18689     buf = sc->grc_dump;
18690     d_hdr = sc->grc_dump;
18691 
18692     d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
18693     d_hdr->version = BNX2X_DUMP_VERSION;
18694     d_hdr->preset = DUMP_ALL_PRESETS;
18695 
18696     if (CHIP_IS_E1(sc)) {
18697         d_hdr->dump_meta_data = DUMP_CHIP_E1;
18698     } else if (CHIP_IS_E1H(sc)) {
18699         d_hdr->dump_meta_data = DUMP_CHIP_E1H;
18700     } else if (CHIP_IS_E2(sc)) {
18701         d_hdr->dump_meta_data = DUMP_CHIP_E2 |
18702                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18703     } else if (CHIP_IS_E3A0(sc)) {
18704         d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
18705                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18706     } else if (CHIP_IS_E3B0(sc)) {
18707         d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
18708                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
18709     }
18710 
18711     buf += sizeof(struct  dump_header);
18712 
18713     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18714 
18715         /* Skip presets with IOR */
18716         if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
18717             (preset_idx == 11))
18718             continue;
18719 
18720         rval = bxe_get_preset_regs(sc, (uint32_t *)buf, preset_idx);
18721 
18722 	if (rval)
18723             break;
18724 
18725         size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
18726 
18727         buf += size;
18728     }
18729 
18730     bxe_pretend_func(sc, 0);
18731     ecore_clear_blocks_parity(sc);
18732     ecore_enable_blocks_parity(sc);
18733 
18734     bxe_pretend_func(sc, 1);
18735     ecore_clear_blocks_parity(sc);
18736     ecore_enable_blocks_parity(sc);
18737 
18738     /* Return to current function */
18739     bxe_pretend_func(sc, SC_ABS_FUNC(sc));
18740 
18741 
18742     context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
18743     for (i = 0, allocated = 0; allocated < context_size; i++) {
18744 
18745         BLOGI(sc, "cdu_context i %d paddr %#jx vaddr %p size 0x%zx\n", i,
18746             (uintmax_t)sc->context[i].vcxt_dma.paddr,
18747             sc->context[i].vcxt_dma.vaddr,
18748             sc->context[i].size);
18749         allocated += sc->context[i].size;
18750     }
18751     BLOGI(sc, "fw stats start_paddr %#jx end_paddr %#jx vaddr %p size 0x%x\n",
18752         (uintmax_t)sc->fw_stats_req_mapping,
18753         (uintmax_t)sc->fw_stats_data_mapping,
18754         sc->fw_stats_req, (sc->fw_stats_req_size + sc->fw_stats_data_size));
18755     BLOGI(sc, "def_status_block paddr %p vaddr %p size 0x%zx\n",
18756         (void *)sc->def_sb_dma.paddr, sc->def_sb,
18757         sizeof(struct host_sp_status_block));
18758     BLOGI(sc, "event_queue paddr %#jx vaddr %p size 0x%x\n",
18759         (uintmax_t)sc->eq_dma.paddr, sc->eq_dma.vaddr, BCM_PAGE_SIZE);
18760     BLOGI(sc, "slow path paddr %#jx vaddr %p size 0x%zx\n",
18761         (uintmax_t)sc->sp_dma.paddr, sc->sp_dma.vaddr,
18762         sizeof(struct bxe_slowpath));
18763     BLOGI(sc, "slow path queue paddr %#jx vaddr %p size 0x%x\n",
18764         (uintmax_t)sc->spq_dma.paddr, sc->spq_dma.vaddr, BCM_PAGE_SIZE);
18765     BLOGI(sc, "fw_buf paddr %#jx vaddr %p size 0x%x\n",
18766         (uintmax_t)sc->gz_buf_dma.paddr, sc->gz_buf_dma.vaddr,
18767         FW_BUF_SIZE);
18768     for (i = 0; i < sc->num_queues; i++) {
18769         fp = &sc->fp[i];
18770         BLOGI(sc, "FP status block fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18771             (uintmax_t)fp->sb_dma.paddr, fp->sb_dma.vaddr,
18772             sizeof(union bxe_host_hc_status_block));
18773         BLOGI(sc, "TX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18774             (uintmax_t)fp->tx_dma.paddr, fp->tx_dma.vaddr,
18775             (BCM_PAGE_SIZE * TX_BD_NUM_PAGES));
18776         BLOGI(sc, "RX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18777             (uintmax_t)fp->rx_dma.paddr, fp->rx_dma.vaddr,
18778             (BCM_PAGE_SIZE * RX_BD_NUM_PAGES));
18779         BLOGI(sc, "RX RCQ CHAIN fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
18780             (uintmax_t)fp->rcq_dma.paddr, fp->rcq_dma.vaddr,
18781             (BCM_PAGE_SIZE * RCQ_NUM_PAGES));
18782         BLOGI(sc, "RX SGE CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
18783             (uintmax_t)fp->rx_sge_dma.paddr, fp->rx_sge_dma.vaddr,
18784             (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES));
18785     }
18786 
18787     ilt_cli = &ilt->clients[1];
18788     for (i = ilt_cli->start; i <= ilt_cli->end; i++) {
18789         BLOGI(sc, "ECORE_ILT paddr %#jx vaddr %p size 0x%x\n",
18790             (uintmax_t)(((struct bxe_dma *)((&ilt->lines[i])->page))->paddr),
18791             ((struct bxe_dma *)((&ilt->lines[i])->page))->vaddr, BCM_PAGE_SIZE);
18792     }
18793 
18794 
18795     cmd_offset = DMAE_REG_CMD_MEM;
18796     for (i = 0; i < 224; i++) {
18797         reg_addr = (cmd_offset +(i * 4));
18798         reg_val = REG_RD(sc, reg_addr);
18799         BLOGI(sc, "DMAE_REG_CMD_MEM i=%d reg_addr 0x%x reg_val 0x%08x\n",i,
18800             reg_addr, reg_val);
18801     }
18802 
18803 
18804     BLOGI(sc, "Collection of grcdump done\n");
18805     sc->grcdump_done = 1;
18806     return(rval);
18807 }
18808 
18809 static int
18810 bxe_add_cdev(struct bxe_softc *sc)
18811 {
18812     sc->eeprom = malloc(BXE_EEPROM_MAX_DATA_LEN, M_DEVBUF, M_NOWAIT);
18813 
18814     if (sc->eeprom == NULL) {
18815         BLOGW(sc, "Unable to alloc for eeprom size buffer\n");
18816         return (-1);
18817     }
18818 
18819     sc->ioctl_dev = make_dev(&bxe_cdevsw,
18820                             sc->ifp->if_dunit,
18821                             UID_ROOT,
18822                             GID_WHEEL,
18823                             0600,
18824                             "%s",
18825                             if_name(sc->ifp));
18826 
18827     if (sc->ioctl_dev == NULL) {
18828         free(sc->eeprom, M_DEVBUF);
18829         sc->eeprom = NULL;
18830         return (-1);
18831     }
18832 
18833     sc->ioctl_dev->si_drv1 = sc;
18834 
18835     return (0);
18836 }
18837 
18838 static void
18839 bxe_del_cdev(struct bxe_softc *sc)
18840 {
18841     if (sc->ioctl_dev != NULL)
18842         destroy_dev(sc->ioctl_dev);
18843 
18844     if (sc->eeprom != NULL) {
18845         free(sc->eeprom, M_DEVBUF);
18846         sc->eeprom = NULL;
18847     }
18848     sc->ioctl_dev = NULL;
18849 
18850     return;
18851 }
18852 
18853 static bool bxe_is_nvram_accessible(struct bxe_softc *sc)
18854 {
18855 
18856     if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
18857         return FALSE;
18858 
18859     return TRUE;
18860 }
18861 
18862 
18863 static int
18864 bxe_wr_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18865 {
18866     int rval = 0;
18867 
18868     if(!bxe_is_nvram_accessible(sc)) {
18869         BLOGW(sc, "Cannot access eeprom when interface is down\n");
18870         return (-EAGAIN);
18871     }
18872     rval = bxe_nvram_write(sc, offset, (uint8_t *)data, len);
18873 
18874 
18875    return (rval);
18876 }
18877 
18878 static int
18879 bxe_rd_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
18880 {
18881     int rval = 0;
18882 
18883     if(!bxe_is_nvram_accessible(sc)) {
18884         BLOGW(sc, "Cannot access eeprom when interface is down\n");
18885         return (-EAGAIN);
18886     }
18887     rval = bxe_nvram_read(sc, offset, (uint8_t *)data, len);
18888 
18889    return (rval);
18890 }
18891 
18892 static int
18893 bxe_eeprom_rd_wr(struct bxe_softc *sc, bxe_eeprom_t *eeprom)
18894 {
18895     int rval = 0;
18896 
18897     switch (eeprom->eeprom_cmd) {
18898 
18899     case BXE_EEPROM_CMD_SET_EEPROM:
18900 
18901         rval = copyin(eeprom->eeprom_data, sc->eeprom,
18902                        eeprom->eeprom_data_len);
18903 
18904         if (rval)
18905             break;
18906 
18907         rval = bxe_wr_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18908                        eeprom->eeprom_data_len);
18909         break;
18910 
18911     case BXE_EEPROM_CMD_GET_EEPROM:
18912 
18913         rval = bxe_rd_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
18914                        eeprom->eeprom_data_len);
18915 
18916         if (rval) {
18917             break;
18918         }
18919 
18920         rval = copyout(sc->eeprom, eeprom->eeprom_data,
18921                        eeprom->eeprom_data_len);
18922         break;
18923 
18924     default:
18925             rval = EINVAL;
18926             break;
18927     }
18928 
18929     if (rval) {
18930         BLOGW(sc, "ioctl cmd %d  failed rval %d\n", eeprom->eeprom_cmd, rval);
18931     }
18932 
18933     return (rval);
18934 }
18935 
18936 static int
18937 bxe_get_settings(struct bxe_softc *sc, bxe_dev_setting_t *dev_p)
18938 {
18939     uint32_t ext_phy_config;
18940     int port = SC_PORT(sc);
18941     int cfg_idx = bxe_get_link_cfg_idx(sc);
18942 
18943     dev_p->supported = sc->port.supported[cfg_idx] |
18944             (sc->port.supported[cfg_idx ^ 1] &
18945             (ELINK_SUPPORTED_TP | ELINK_SUPPORTED_FIBRE));
18946     dev_p->advertising = sc->port.advertising[cfg_idx];
18947     if(sc->link_params.phy[bxe_get_cur_phy_idx(sc)].media_type ==
18948         ELINK_ETH_PHY_SFP_1G_FIBER) {
18949         dev_p->supported = ~(ELINK_SUPPORTED_10000baseT_Full);
18950         dev_p->advertising &= ~(ADVERTISED_10000baseT_Full);
18951     }
18952     if ((sc->state == BXE_STATE_OPEN) && sc->link_vars.link_up &&
18953         !(sc->flags & BXE_MF_FUNC_DIS)) {
18954         dev_p->duplex = sc->link_vars.duplex;
18955         if (IS_MF(sc) && !BXE_NOMCP(sc))
18956             dev_p->speed = bxe_get_mf_speed(sc);
18957         else
18958             dev_p->speed = sc->link_vars.line_speed;
18959     } else {
18960         dev_p->duplex = DUPLEX_UNKNOWN;
18961         dev_p->speed = SPEED_UNKNOWN;
18962     }
18963 
18964     dev_p->port = bxe_media_detect(sc);
18965 
18966     ext_phy_config = SHMEM_RD(sc,
18967                          dev_info.port_hw_config[port].external_phy_config);
18968     if((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) ==
18969         PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
18970         dev_p->phy_address =  sc->port.phy_addr;
18971     else if(((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18972             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
18973         ((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
18974             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
18975         dev_p->phy_address = ELINK_XGXS_EXT_PHY_ADDR(ext_phy_config);
18976     else
18977         dev_p->phy_address = 0;
18978 
18979     if(sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG)
18980         dev_p->autoneg = AUTONEG_ENABLE;
18981     else
18982        dev_p->autoneg = AUTONEG_DISABLE;
18983 
18984 
18985     return 0;
18986 }
18987 
18988 static int
18989 bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18990         struct thread *td)
18991 {
18992     struct bxe_softc    *sc;
18993     int                 rval = 0;
18994     device_t            pci_dev;
18995     bxe_grcdump_t       *dump = NULL;
18996     int grc_dump_size;
18997     bxe_drvinfo_t   *drv_infop = NULL;
18998     bxe_dev_setting_t  *dev_p;
18999     bxe_dev_setting_t  dev_set;
19000     bxe_get_regs_t  *reg_p;
19001     bxe_reg_rdw_t *reg_rdw_p;
19002     bxe_pcicfg_rdw_t *cfg_rdw_p;
19003     bxe_perm_mac_addr_t *mac_addr_p;
19004 
19005 
19006     if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
19007         return ENXIO;
19008 
19009     pci_dev= sc->dev;
19010 
19011     dump = (bxe_grcdump_t *)data;
19012 
19013     switch(cmd) {
19014 
19015         case BXE_GRC_DUMP_SIZE:
19016             dump->pci_func = sc->pcie_func;
19017             dump->grcdump_size =
19018                 (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19019                      sizeof(struct  dump_header);
19020             break;
19021 
19022         case BXE_GRC_DUMP:
19023 
19024             grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19025                                 sizeof(struct  dump_header);
19026             if ((!sc->trigger_grcdump) || (dump->grcdump == NULL) ||
19027                 (dump->grcdump_size < grc_dump_size)) {
19028                 rval = EINVAL;
19029                 break;
19030             }
19031 
19032             if((sc->trigger_grcdump) && (!sc->grcdump_done) &&
19033                 (!sc->grcdump_started)) {
19034                 rval =  bxe_grc_dump(sc);
19035             }
19036 
19037             if((!rval) && (sc->grcdump_done) && (sc->grcdump_started) &&
19038                 (sc->grc_dump != NULL))  {
19039                 dump->grcdump_dwords = grc_dump_size >> 2;
19040                 rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
19041                 free(sc->grc_dump, M_DEVBUF);
19042                 sc->grc_dump = NULL;
19043                 sc->grcdump_started = 0;
19044                 sc->grcdump_done = 0;
19045             }
19046 
19047             break;
19048 
19049         case BXE_DRV_INFO:
19050             drv_infop = (bxe_drvinfo_t *)data;
19051             snprintf(drv_infop->drv_name, BXE_DRV_NAME_LENGTH, "%s", "bxe");
19052             snprintf(drv_infop->drv_version, BXE_DRV_VERSION_LENGTH, "v:%s",
19053                 BXE_DRIVER_VERSION);
19054             snprintf(drv_infop->mfw_version, BXE_MFW_VERSION_LENGTH, "%s",
19055                 sc->devinfo.bc_ver_str);
19056             snprintf(drv_infop->stormfw_version, BXE_STORMFW_VERSION_LENGTH,
19057                 "%s", sc->fw_ver_str);
19058             drv_infop->eeprom_dump_len = sc->devinfo.flash_size;
19059             drv_infop->reg_dump_len =
19060                 (bxe_get_total_regs_len32(sc) * sizeof(uint32_t))
19061                     + sizeof(struct  dump_header);
19062             snprintf(drv_infop->bus_info, BXE_BUS_INFO_LENGTH, "%d:%d:%d",
19063                 sc->pcie_bus, sc->pcie_device, sc->pcie_func);
19064             break;
19065 
19066         case BXE_DEV_SETTING:
19067             dev_p = (bxe_dev_setting_t *)data;
19068             bxe_get_settings(sc, &dev_set);
19069             dev_p->supported = dev_set.supported;
19070             dev_p->advertising = dev_set.advertising;
19071             dev_p->speed = dev_set.speed;
19072             dev_p->duplex = dev_set.duplex;
19073             dev_p->port = dev_set.port;
19074             dev_p->phy_address = dev_set.phy_address;
19075             dev_p->autoneg = dev_set.autoneg;
19076 
19077             break;
19078 
19079         case BXE_GET_REGS:
19080 
19081             reg_p = (bxe_get_regs_t *)data;
19082             grc_dump_size = reg_p->reg_buf_len;
19083 
19084             if((!sc->grcdump_done) && (!sc->grcdump_started)) {
19085                 bxe_grc_dump(sc);
19086             }
19087             if((sc->grcdump_done) && (sc->grcdump_started) &&
19088                 (sc->grc_dump != NULL))  {
19089                 rval = copyout(sc->grc_dump, reg_p->reg_buf, grc_dump_size);
19090                 free(sc->grc_dump, M_DEVBUF);
19091                 sc->grc_dump = NULL;
19092                 sc->grcdump_started = 0;
19093                 sc->grcdump_done = 0;
19094             }
19095 
19096             break;
19097 
19098         case BXE_RDW_REG:
19099             reg_rdw_p = (bxe_reg_rdw_t *)data;
19100             if((reg_rdw_p->reg_cmd == BXE_READ_REG_CMD) &&
19101                 (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19102                 reg_rdw_p->reg_val = REG_RD(sc, reg_rdw_p->reg_id);
19103 
19104             if((reg_rdw_p->reg_cmd == BXE_WRITE_REG_CMD) &&
19105                 (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19106                 REG_WR(sc, reg_rdw_p->reg_id, reg_rdw_p->reg_val);
19107 
19108             break;
19109 
19110         case BXE_RDW_PCICFG:
19111             cfg_rdw_p = (bxe_pcicfg_rdw_t *)data;
19112             if(cfg_rdw_p->cfg_cmd == BXE_READ_PCICFG) {
19113 
19114                 cfg_rdw_p->cfg_val = pci_read_config(sc->dev, cfg_rdw_p->cfg_id,
19115                                          cfg_rdw_p->cfg_width);
19116 
19117             } else if(cfg_rdw_p->cfg_cmd == BXE_WRITE_PCICFG) {
19118                 pci_write_config(sc->dev, cfg_rdw_p->cfg_id, cfg_rdw_p->cfg_val,
19119                             cfg_rdw_p->cfg_width);
19120             } else {
19121                 BLOGW(sc, "BXE_RDW_PCICFG ioctl wrong cmd passed\n");
19122             }
19123             break;
19124 
19125         case BXE_MAC_ADDR:
19126             mac_addr_p = (bxe_perm_mac_addr_t *)data;
19127             snprintf(mac_addr_p->mac_addr_str, sizeof(sc->mac_addr_str), "%s",
19128                 sc->mac_addr_str);
19129             break;
19130 
19131         case BXE_EEPROM:
19132             rval = bxe_eeprom_rd_wr(sc, (bxe_eeprom_t *)data);
19133             break;
19134 
19135 
19136         default:
19137             break;
19138     }
19139 
19140     return (rval);
19141 }
19142