]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/bxe/bxe.c
MFC r339366
[FreeBSD/stable/9.git] / sys / dev / bxe / bxe.c
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.91"
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 /*
193  * FreeBSD KLD module/device interface event handler method.
194  */
195 static device_method_t bxe_methods[] = {
196     /* Device interface (device_if.h) */
197     DEVMETHOD(device_probe,     bxe_probe),
198     DEVMETHOD(device_attach,    bxe_attach),
199     DEVMETHOD(device_detach,    bxe_detach),
200     DEVMETHOD(device_shutdown,  bxe_shutdown),
201     /* Bus interface (bus_if.h) */
202     DEVMETHOD(bus_print_child,  bus_generic_print_child),
203     DEVMETHOD(bus_driver_added, bus_generic_driver_added),
204     KOBJMETHOD_END
205 };
206
207 /*
208  * FreeBSD KLD Module data declaration
209  */
210 static driver_t bxe_driver = {
211     "bxe",                   /* module name */
212     bxe_methods,             /* event handler */
213     sizeof(struct bxe_softc) /* extra data */
214 };
215
216 /*
217  * FreeBSD dev class is needed to manage dev instances and
218  * to associate with a bus type
219  */
220 static devclass_t bxe_devclass;
221
222 MODULE_DEPEND(bxe, pci, 1, 1, 1);
223 MODULE_DEPEND(bxe, ether, 1, 1, 1);
224 DRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
225
226 /* resources needed for unloading a previously loaded device */
227
228 #define BXE_PREV_WAIT_NEEDED 1
229 struct mtx bxe_prev_mtx;
230 MTX_SYSINIT(bxe_prev_mtx, &bxe_prev_mtx, "bxe_prev_lock", MTX_DEF);
231 struct bxe_prev_list_node {
232     LIST_ENTRY(bxe_prev_list_node) node;
233     uint8_t bus;
234     uint8_t slot;
235     uint8_t path;
236     uint8_t aer; /* XXX automatic error recovery */
237     uint8_t undi;
238 };
239 static LIST_HEAD(, bxe_prev_list_node) bxe_prev_list = LIST_HEAD_INITIALIZER(bxe_prev_list);
240
241 static int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
242
243 /* Tunable device values... */
244
245 SYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
246
247 /* Debug */
248 unsigned long bxe_debug = 0;
249 TUNABLE_ULONG("hw.bxe.debug", &bxe_debug);
250 SYSCTL_ULONG(_hw_bxe, OID_AUTO, debug, (CTLFLAG_RDTUN),
251              &bxe_debug, 0, "Debug logging mode");
252
253 /* Interrupt Mode: 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
254 static int bxe_interrupt_mode = INTR_MODE_MSIX;
255 TUNABLE_INT("hw.bxe.interrupt_mode", &bxe_interrupt_mode);
256 SYSCTL_INT(_hw_bxe, OID_AUTO, interrupt_mode, CTLFLAG_RDTUN,
257            &bxe_interrupt_mode, 0, "Interrupt (MSI-X/MSI/INTx) mode");
258
259 /* Number of Queues: 0 (Auto) or 1 to 16 (fixed queue number) */
260 static int bxe_queue_count = 4;
261 TUNABLE_INT("hw.bxe.queue_count", &bxe_queue_count);
262 SYSCTL_INT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN,
263            &bxe_queue_count, 0, "Multi-Queue queue count");
264
265 /* max number of buffers per queue (default RX_BD_USABLE) */
266 static int bxe_max_rx_bufs = 0;
267 TUNABLE_INT("hw.bxe.max_rx_bufs", &bxe_max_rx_bufs);
268 SYSCTL_INT(_hw_bxe, OID_AUTO, max_rx_bufs, CTLFLAG_RDTUN,
269            &bxe_max_rx_bufs, 0, "Maximum Number of Rx Buffers Per Queue");
270
271 /* Host interrupt coalescing RX tick timer (usecs) */
272 static int bxe_hc_rx_ticks = 25;
273 TUNABLE_INT("hw.bxe.hc_rx_ticks", &bxe_hc_rx_ticks);
274 SYSCTL_INT(_hw_bxe, OID_AUTO, hc_rx_ticks, CTLFLAG_RDTUN,
275            &bxe_hc_rx_ticks, 0, "Host Coalescing Rx ticks");
276
277 /* Host interrupt coalescing TX tick timer (usecs) */
278 static int bxe_hc_tx_ticks = 50;
279 TUNABLE_INT("hw.bxe.hc_tx_ticks", &bxe_hc_tx_ticks);
280 SYSCTL_INT(_hw_bxe, OID_AUTO, hc_tx_ticks, CTLFLAG_RDTUN,
281            &bxe_hc_tx_ticks, 0, "Host Coalescing Tx ticks");
282
283 /* Maximum number of Rx packets to process at a time */
284 static int bxe_rx_budget = 0xffffffff;
285 TUNABLE_INT("hw.bxe.rx_budget", &bxe_rx_budget);
286 SYSCTL_INT(_hw_bxe, OID_AUTO, rx_budget, CTLFLAG_TUN,
287            &bxe_rx_budget, 0, "Rx processing budget");
288
289 /* Maximum LRO aggregation size */
290 static int bxe_max_aggregation_size = 0;
291 TUNABLE_INT("hw.bxe.max_aggregation_size", &bxe_max_aggregation_size);
292 SYSCTL_INT(_hw_bxe, OID_AUTO, max_aggregation_size, CTLFLAG_TUN,
293            &bxe_max_aggregation_size, 0, "max aggregation size");
294
295 /* PCI MRRS: -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB) */
296 static int bxe_mrrs = -1;
297 TUNABLE_INT("hw.bxe.mrrs", &bxe_mrrs);
298 SYSCTL_INT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN,
299            &bxe_mrrs, 0, "PCIe maximum read request size");
300
301 /* AutoGrEEEn: 0 (hardware default), 1 (force on), 2 (force off) */
302 static int bxe_autogreeen = 0;
303 TUNABLE_INT("hw.bxe.autogreeen", &bxe_autogreeen);
304 SYSCTL_INT(_hw_bxe, OID_AUTO, autogreeen, CTLFLAG_RDTUN,
305            &bxe_autogreeen, 0, "AutoGrEEEn support");
306
307 /* 4-tuple RSS support for UDP: 0 (disabled), 1 (enabled) */
308 static int bxe_udp_rss = 0;
309 TUNABLE_INT("hw.bxe.udp_rss", &bxe_udp_rss);
310 SYSCTL_INT(_hw_bxe, OID_AUTO, udp_rss, CTLFLAG_RDTUN,
311            &bxe_udp_rss, 0, "UDP RSS support");
312
313
314 #define STAT_NAME_LEN 32 /* no stat names below can be longer than this */
315
316 #define STATS_OFFSET32(stat_name)                   \
317     (offsetof(struct bxe_eth_stats, stat_name) / 4)
318
319 #define Q_STATS_OFFSET32(stat_name)                   \
320     (offsetof(struct bxe_eth_q_stats, stat_name) / 4)
321
322 static const struct {
323     uint32_t offset;
324     uint32_t size;
325     uint32_t flags;
326 #define STATS_FLAGS_PORT  1
327 #define STATS_FLAGS_FUNC  2 /* MF only cares about function stats */
328 #define STATS_FLAGS_BOTH  (STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
329     char string[STAT_NAME_LEN];
330 } bxe_eth_stats_arr[] = {
331     { STATS_OFFSET32(total_bytes_received_hi),
332                 8, STATS_FLAGS_BOTH, "rx_bytes" },
333     { STATS_OFFSET32(error_bytes_received_hi),
334                 8, STATS_FLAGS_BOTH, "rx_error_bytes" },
335     { STATS_OFFSET32(total_unicast_packets_received_hi),
336                 8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
337     { STATS_OFFSET32(total_multicast_packets_received_hi),
338                 8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
339     { STATS_OFFSET32(total_broadcast_packets_received_hi),
340                 8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
341     { STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
342                 8, STATS_FLAGS_PORT, "rx_crc_errors" },
343     { STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
344                 8, STATS_FLAGS_PORT, "rx_align_errors" },
345     { STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
346                 8, STATS_FLAGS_PORT, "rx_undersize_packets" },
347     { STATS_OFFSET32(etherstatsoverrsizepkts_hi),
348                 8, STATS_FLAGS_PORT, "rx_oversize_packets" },
349     { STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
350                 8, STATS_FLAGS_PORT, "rx_fragments" },
351     { STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
352                 8, STATS_FLAGS_PORT, "rx_jabbers" },
353     { STATS_OFFSET32(no_buff_discard_hi),
354                 8, STATS_FLAGS_BOTH, "rx_discards" },
355     { STATS_OFFSET32(mac_filter_discard),
356                 4, STATS_FLAGS_PORT, "rx_filtered_packets" },
357     { STATS_OFFSET32(mf_tag_discard),
358                 4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
359     { STATS_OFFSET32(pfc_frames_received_hi),
360                 8, STATS_FLAGS_PORT, "pfc_frames_received" },
361     { STATS_OFFSET32(pfc_frames_sent_hi),
362                 8, STATS_FLAGS_PORT, "pfc_frames_sent" },
363     { STATS_OFFSET32(brb_drop_hi),
364                 8, STATS_FLAGS_PORT, "rx_brb_discard" },
365     { STATS_OFFSET32(brb_truncate_hi),
366                 8, STATS_FLAGS_PORT, "rx_brb_truncate" },
367     { STATS_OFFSET32(pause_frames_received_hi),
368                 8, STATS_FLAGS_PORT, "rx_pause_frames" },
369     { STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
370                 8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
371     { STATS_OFFSET32(nig_timer_max),
372                 4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
373     { STATS_OFFSET32(total_bytes_transmitted_hi),
374                 8, STATS_FLAGS_BOTH, "tx_bytes" },
375     { STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
376                 8, STATS_FLAGS_PORT, "tx_error_bytes" },
377     { STATS_OFFSET32(total_unicast_packets_transmitted_hi),
378                 8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
379     { STATS_OFFSET32(total_multicast_packets_transmitted_hi),
380                 8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
381     { STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
382                 8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
383     { STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
384                 8, STATS_FLAGS_PORT, "tx_mac_errors" },
385     { STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
386                 8, STATS_FLAGS_PORT, "tx_carrier_errors" },
387     { STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
388                 8, STATS_FLAGS_PORT, "tx_single_collisions" },
389     { STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
390                 8, STATS_FLAGS_PORT, "tx_multi_collisions" },
391     { STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
392                 8, STATS_FLAGS_PORT, "tx_deferred" },
393     { STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
394                 8, STATS_FLAGS_PORT, "tx_excess_collisions" },
395     { STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
396                 8, STATS_FLAGS_PORT, "tx_late_collisions" },
397     { STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
398                 8, STATS_FLAGS_PORT, "tx_total_collisions" },
399     { STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
400                 8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
401     { STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
402                 8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
403     { STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
404                 8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
405     { STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
406                 8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
407     { STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
408                 8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
409     { STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
410                 8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
411     { STATS_OFFSET32(etherstatspktsover1522octets_hi),
412                 8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
413     { STATS_OFFSET32(pause_frames_sent_hi),
414                 8, STATS_FLAGS_PORT, "tx_pause_frames" },
415     { STATS_OFFSET32(total_tpa_aggregations_hi),
416                 8, STATS_FLAGS_FUNC, "tpa_aggregations" },
417     { STATS_OFFSET32(total_tpa_aggregated_frames_hi),
418                 8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
419     { STATS_OFFSET32(total_tpa_bytes_hi),
420                 8, STATS_FLAGS_FUNC, "tpa_bytes"},
421     { STATS_OFFSET32(eee_tx_lpi),
422                 4, STATS_FLAGS_PORT, "eee_tx_lpi"},
423     { STATS_OFFSET32(rx_calls),
424                 4, STATS_FLAGS_FUNC, "rx_calls"},
425     { STATS_OFFSET32(rx_pkts),
426                 4, STATS_FLAGS_FUNC, "rx_pkts"},
427     { STATS_OFFSET32(rx_tpa_pkts),
428                 4, STATS_FLAGS_FUNC, "rx_tpa_pkts"},
429     { STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
430                 4, STATS_FLAGS_FUNC, "rx_erroneous_jumbo_sge_pkts"},
431     { STATS_OFFSET32(rx_bxe_service_rxsgl),
432                 4, STATS_FLAGS_FUNC, "rx_bxe_service_rxsgl"},
433     { STATS_OFFSET32(rx_jumbo_sge_pkts),
434                 4, STATS_FLAGS_FUNC, "rx_jumbo_sge_pkts"},
435     { STATS_OFFSET32(rx_soft_errors),
436                 4, STATS_FLAGS_FUNC, "rx_soft_errors"},
437     { STATS_OFFSET32(rx_hw_csum_errors),
438                 4, STATS_FLAGS_FUNC, "rx_hw_csum_errors"},
439     { STATS_OFFSET32(rx_ofld_frames_csum_ip),
440                 4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_ip"},
441     { STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
442                 4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_tcp_udp"},
443     { STATS_OFFSET32(rx_budget_reached),
444                 4, STATS_FLAGS_FUNC, "rx_budget_reached"},
445     { STATS_OFFSET32(tx_pkts),
446                 4, STATS_FLAGS_FUNC, "tx_pkts"},
447     { STATS_OFFSET32(tx_soft_errors),
448                 4, STATS_FLAGS_FUNC, "tx_soft_errors"},
449     { STATS_OFFSET32(tx_ofld_frames_csum_ip),
450                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_ip"},
451     { STATS_OFFSET32(tx_ofld_frames_csum_tcp),
452                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_tcp"},
453     { STATS_OFFSET32(tx_ofld_frames_csum_udp),
454                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_udp"},
455     { STATS_OFFSET32(tx_ofld_frames_lso),
456                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso"},
457     { STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
458                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso_hdr_splits"},
459     { STATS_OFFSET32(tx_encap_failures),
460                 4, STATS_FLAGS_FUNC, "tx_encap_failures"},
461     { STATS_OFFSET32(tx_hw_queue_full),
462                 4, STATS_FLAGS_FUNC, "tx_hw_queue_full"},
463     { STATS_OFFSET32(tx_hw_max_queue_depth),
464                 4, STATS_FLAGS_FUNC, "tx_hw_max_queue_depth"},
465     { STATS_OFFSET32(tx_dma_mapping_failure),
466                 4, STATS_FLAGS_FUNC, "tx_dma_mapping_failure"},
467     { STATS_OFFSET32(tx_max_drbr_queue_depth),
468                 4, STATS_FLAGS_FUNC, "tx_max_drbr_queue_depth"},
469     { STATS_OFFSET32(tx_window_violation_std),
470                 4, STATS_FLAGS_FUNC, "tx_window_violation_std"},
471     { STATS_OFFSET32(tx_window_violation_tso),
472                 4, STATS_FLAGS_FUNC, "tx_window_violation_tso"},
473     { STATS_OFFSET32(tx_chain_lost_mbuf),
474                 4, STATS_FLAGS_FUNC, "tx_chain_lost_mbuf"},
475     { STATS_OFFSET32(tx_frames_deferred),
476                 4, STATS_FLAGS_FUNC, "tx_frames_deferred"},
477     { STATS_OFFSET32(tx_queue_xoff),
478                 4, STATS_FLAGS_FUNC, "tx_queue_xoff"},
479     { STATS_OFFSET32(mbuf_defrag_attempts),
480                 4, STATS_FLAGS_FUNC, "mbuf_defrag_attempts"},
481     { STATS_OFFSET32(mbuf_defrag_failures),
482                 4, STATS_FLAGS_FUNC, "mbuf_defrag_failures"},
483     { STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
484                 4, STATS_FLAGS_FUNC, "mbuf_rx_bd_alloc_failed"},
485     { STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
486                 4, STATS_FLAGS_FUNC, "mbuf_rx_bd_mapping_failed"},
487     { STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
488                 4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_alloc_failed"},
489     { STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
490                 4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_mapping_failed"},
491     { STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
492                 4, STATS_FLAGS_FUNC, "mbuf_rx_sge_alloc_failed"},
493     { STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
494                 4, STATS_FLAGS_FUNC, "mbuf_rx_sge_mapping_failed"},
495     { STATS_OFFSET32(mbuf_alloc_tx),
496                 4, STATS_FLAGS_FUNC, "mbuf_alloc_tx"},
497     { STATS_OFFSET32(mbuf_alloc_rx),
498                 4, STATS_FLAGS_FUNC, "mbuf_alloc_rx"},
499     { STATS_OFFSET32(mbuf_alloc_sge),
500                 4, STATS_FLAGS_FUNC, "mbuf_alloc_sge"},
501     { STATS_OFFSET32(mbuf_alloc_tpa),
502                 4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"},
503     { STATS_OFFSET32(tx_queue_full_return),
504                 4, STATS_FLAGS_FUNC, "tx_queue_full_return"},
505     { STATS_OFFSET32(bxe_tx_mq_sc_state_failures),
506                 4, STATS_FLAGS_FUNC, "bxe_tx_mq_sc_state_failures"},
507     { STATS_OFFSET32(tx_request_link_down_failures),
508                 4, STATS_FLAGS_FUNC, "tx_request_link_down_failures"},
509     { STATS_OFFSET32(bd_avail_too_less_failures),
510                 4, STATS_FLAGS_FUNC, "bd_avail_too_less_failures"},
511     { STATS_OFFSET32(tx_mq_not_empty),
512                 4, STATS_FLAGS_FUNC, "tx_mq_not_empty"},
513     { STATS_OFFSET32(nsegs_path1_errors),
514                 4, STATS_FLAGS_FUNC, "nsegs_path1_errors"},
515     { STATS_OFFSET32(nsegs_path2_errors),
516                 4, STATS_FLAGS_FUNC, "nsegs_path2_errors"}
517
518
519 };
520
521 static const struct {
522     uint32_t offset;
523     uint32_t size;
524     char string[STAT_NAME_LEN];
525 } bxe_eth_q_stats_arr[] = {
526     { Q_STATS_OFFSET32(total_bytes_received_hi),
527                 8, "rx_bytes" },
528     { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
529                 8, "rx_ucast_packets" },
530     { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
531                 8, "rx_mcast_packets" },
532     { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
533                 8, "rx_bcast_packets" },
534     { Q_STATS_OFFSET32(no_buff_discard_hi),
535                 8, "rx_discards" },
536     { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
537                 8, "tx_bytes" },
538     { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
539                 8, "tx_ucast_packets" },
540     { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
541                 8, "tx_mcast_packets" },
542     { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
543                 8, "tx_bcast_packets" },
544     { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
545                 8, "tpa_aggregations" },
546     { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
547                 8, "tpa_aggregated_frames"},
548     { Q_STATS_OFFSET32(total_tpa_bytes_hi),
549                 8, "tpa_bytes"},
550     { Q_STATS_OFFSET32(rx_calls),
551                 4, "rx_calls"},
552     { Q_STATS_OFFSET32(rx_pkts),
553                 4, "rx_pkts"},
554     { Q_STATS_OFFSET32(rx_tpa_pkts),
555                 4, "rx_tpa_pkts"},
556     { Q_STATS_OFFSET32(rx_erroneous_jumbo_sge_pkts),
557                 4, "rx_erroneous_jumbo_sge_pkts"},
558     { Q_STATS_OFFSET32(rx_bxe_service_rxsgl),
559                 4, "rx_bxe_service_rxsgl"},
560     { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
561                 4, "rx_jumbo_sge_pkts"},
562     { Q_STATS_OFFSET32(rx_soft_errors),
563                 4, "rx_soft_errors"},
564     { Q_STATS_OFFSET32(rx_hw_csum_errors),
565                 4, "rx_hw_csum_errors"},
566     { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
567                 4, "rx_ofld_frames_csum_ip"},
568     { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
569                 4, "rx_ofld_frames_csum_tcp_udp"},
570     { Q_STATS_OFFSET32(rx_budget_reached),
571                 4, "rx_budget_reached"},
572     { Q_STATS_OFFSET32(tx_pkts),
573                 4, "tx_pkts"},
574     { Q_STATS_OFFSET32(tx_soft_errors),
575                 4, "tx_soft_errors"},
576     { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
577                 4, "tx_ofld_frames_csum_ip"},
578     { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
579                 4, "tx_ofld_frames_csum_tcp"},
580     { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
581                 4, "tx_ofld_frames_csum_udp"},
582     { Q_STATS_OFFSET32(tx_ofld_frames_lso),
583                 4, "tx_ofld_frames_lso"},
584     { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
585                 4, "tx_ofld_frames_lso_hdr_splits"},
586     { Q_STATS_OFFSET32(tx_encap_failures),
587                 4, "tx_encap_failures"},
588     { Q_STATS_OFFSET32(tx_hw_queue_full),
589                 4, "tx_hw_queue_full"},
590     { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
591                 4, "tx_hw_max_queue_depth"},
592     { Q_STATS_OFFSET32(tx_dma_mapping_failure),
593                 4, "tx_dma_mapping_failure"},
594     { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
595                 4, "tx_max_drbr_queue_depth"},
596     { Q_STATS_OFFSET32(tx_window_violation_std),
597                 4, "tx_window_violation_std"},
598     { Q_STATS_OFFSET32(tx_window_violation_tso),
599                 4, "tx_window_violation_tso"},
600     { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
601                 4, "tx_chain_lost_mbuf"},
602     { Q_STATS_OFFSET32(tx_frames_deferred),
603                 4, "tx_frames_deferred"},
604     { Q_STATS_OFFSET32(tx_queue_xoff),
605                 4, "tx_queue_xoff"},
606     { Q_STATS_OFFSET32(mbuf_defrag_attempts),
607                 4, "mbuf_defrag_attempts"},
608     { Q_STATS_OFFSET32(mbuf_defrag_failures),
609                 4, "mbuf_defrag_failures"},
610     { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
611                 4, "mbuf_rx_bd_alloc_failed"},
612     { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
613                 4, "mbuf_rx_bd_mapping_failed"},
614     { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
615                 4, "mbuf_rx_tpa_alloc_failed"},
616     { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
617                 4, "mbuf_rx_tpa_mapping_failed"},
618     { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
619                 4, "mbuf_rx_sge_alloc_failed"},
620     { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
621                 4, "mbuf_rx_sge_mapping_failed"},
622     { Q_STATS_OFFSET32(mbuf_alloc_tx),
623                 4, "mbuf_alloc_tx"},
624     { Q_STATS_OFFSET32(mbuf_alloc_rx),
625                 4, "mbuf_alloc_rx"},
626     { Q_STATS_OFFSET32(mbuf_alloc_sge),
627                 4, "mbuf_alloc_sge"},
628     { Q_STATS_OFFSET32(mbuf_alloc_tpa),
629                 4, "mbuf_alloc_tpa"},
630     { Q_STATS_OFFSET32(tx_queue_full_return),
631                 4, "tx_queue_full_return"},
632     { Q_STATS_OFFSET32(bxe_tx_mq_sc_state_failures),
633                 4, "bxe_tx_mq_sc_state_failures"},
634     { Q_STATS_OFFSET32(tx_request_link_down_failures),
635                 4, "tx_request_link_down_failures"},
636     { Q_STATS_OFFSET32(bd_avail_too_less_failures),
637                 4, "bd_avail_too_less_failures"},
638     { Q_STATS_OFFSET32(tx_mq_not_empty),
639                 4, "tx_mq_not_empty"},
640     { Q_STATS_OFFSET32(nsegs_path1_errors),
641                 4, "nsegs_path1_errors"},
642     { Q_STATS_OFFSET32(nsegs_path2_errors),
643                 4, "nsegs_path2_errors"}
644
645
646 };
647
648 #define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
649 #define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
650
651
652 static void    bxe_cmng_fns_init(struct bxe_softc *sc,
653                                  uint8_t          read_cfg,
654                                  uint8_t          cmng_type);
655 static int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
656 static void    storm_memset_cmng(struct bxe_softc *sc,
657                                  struct cmng_init *cmng,
658                                  uint8_t          port);
659 static void    bxe_set_reset_global(struct bxe_softc *sc);
660 static void    bxe_set_reset_in_progress(struct bxe_softc *sc);
661 static uint8_t bxe_reset_is_done(struct bxe_softc *sc,
662                                  int              engine);
663 static uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
664 static uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
665                                    uint8_t          *global,
666                                    uint8_t          print);
667 static void    bxe_int_disable(struct bxe_softc *sc);
668 static int     bxe_release_leader_lock(struct bxe_softc *sc);
669 static void    bxe_pf_disable(struct bxe_softc *sc);
670 static void    bxe_free_fp_buffers(struct bxe_softc *sc);
671 static inline void bxe_update_rx_prod(struct bxe_softc    *sc,
672                                       struct bxe_fastpath *fp,
673                                       uint16_t            rx_bd_prod,
674                                       uint16_t            rx_cq_prod,
675                                       uint16_t            rx_sge_prod);
676 static void    bxe_link_report_locked(struct bxe_softc *sc);
677 static void    bxe_link_report(struct bxe_softc *sc);
678 static void    bxe_link_status_update(struct bxe_softc *sc);
679 static void    bxe_periodic_callout_func(void *xsc);
680 static void    bxe_periodic_start(struct bxe_softc *sc);
681 static void    bxe_periodic_stop(struct bxe_softc *sc);
682 static int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
683                                     uint16_t prev_index,
684                                     uint16_t index);
685 static int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
686                                      int                 queue);
687 static int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
688                                      uint16_t            index);
689 static uint8_t bxe_txeof(struct bxe_softc *sc,
690                          struct bxe_fastpath *fp);
691 static void    bxe_task_fp(struct bxe_fastpath *fp);
692 static __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
693                                      struct mbuf      *m,
694                                      uint8_t          contents);
695 static int     bxe_alloc_mem(struct bxe_softc *sc);
696 static void    bxe_free_mem(struct bxe_softc *sc);
697 static int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
698 static void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
699 static int     bxe_interrupt_attach(struct bxe_softc *sc);
700 static void    bxe_interrupt_detach(struct bxe_softc *sc);
701 static void    bxe_set_rx_mode(struct bxe_softc *sc);
702 static int     bxe_init_locked(struct bxe_softc *sc);
703 static int     bxe_stop_locked(struct bxe_softc *sc);
704 static void    bxe_sp_err_timeout_task(void *arg, int pending);
705 void           bxe_parity_recover(struct bxe_softc *sc);
706 void           bxe_handle_error(struct bxe_softc *sc);
707 static __noinline int bxe_nic_load(struct bxe_softc *sc,
708                                    int              load_mode);
709 static __noinline int bxe_nic_unload(struct bxe_softc *sc,
710                                      uint32_t         unload_mode,
711                                      uint8_t          keep_link);
712
713 static void bxe_handle_sp_tq(void *context, int pending);
714 static void bxe_handle_fp_tq(void *context, int pending);
715
716 static int bxe_add_cdev(struct bxe_softc *sc);
717 static void bxe_del_cdev(struct bxe_softc *sc);
718 int bxe_grc_dump(struct bxe_softc *sc);
719 static int bxe_alloc_buf_rings(struct bxe_softc *sc);
720 static void bxe_free_buf_rings(struct bxe_softc *sc);
721
722 /* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
723 uint32_t
724 calc_crc32(uint8_t  *crc32_packet,
725            uint32_t crc32_length,
726            uint32_t crc32_seed,
727            uint8_t  complement)
728 {
729    uint32_t byte         = 0;
730    uint32_t bit          = 0;
731    uint8_t  msb          = 0;
732    uint32_t temp         = 0;
733    uint32_t shft         = 0;
734    uint8_t  current_byte = 0;
735    uint32_t crc32_result = crc32_seed;
736    const uint32_t CRC32_POLY = 0x1edc6f41;
737
738    if ((crc32_packet == NULL) ||
739        (crc32_length == 0) ||
740        ((crc32_length % 8) != 0))
741     {
742         return (crc32_result);
743     }
744
745     for (byte = 0; byte < crc32_length; byte = byte + 1)
746     {
747         current_byte = crc32_packet[byte];
748         for (bit = 0; bit < 8; bit = bit + 1)
749         {
750             /* msb = crc32_result[31]; */
751             msb = (uint8_t)(crc32_result >> 31);
752
753             crc32_result = crc32_result << 1;
754
755             /* it (msb != current_byte[bit]) */
756             if (msb != (0x1 & (current_byte >> bit)))
757             {
758                 crc32_result = crc32_result ^ CRC32_POLY;
759                 /* crc32_result[0] = 1 */
760                 crc32_result |= 1;
761             }
762         }
763     }
764
765     /* Last step is to:
766      * 1. "mirror" every bit
767      * 2. swap the 4 bytes
768      * 3. complement each bit
769      */
770
771     /* Mirror */
772     temp = crc32_result;
773     shft = sizeof(crc32_result) * 8 - 1;
774
775     for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
776     {
777         temp <<= 1;
778         temp |= crc32_result & 1;
779         shft-- ;
780     }
781
782     /* temp[31-bit] = crc32_result[bit] */
783     temp <<= shft;
784
785     /* Swap */
786     /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
787     {
788         uint32_t t0, t1, t2, t3;
789         t0 = (0x000000ff & (temp >> 24));
790         t1 = (0x0000ff00 & (temp >> 8));
791         t2 = (0x00ff0000 & (temp << 8));
792         t3 = (0xff000000 & (temp << 24));
793         crc32_result = t0 | t1 | t2 | t3;
794     }
795
796     /* Complement */
797     if (complement)
798     {
799         crc32_result = ~crc32_result;
800     }
801
802     return (crc32_result);
803 }
804
805 int
806 bxe_test_bit(int                    nr,
807              volatile unsigned long *addr)
808 {
809     return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
810 }
811
812 void
813 bxe_set_bit(unsigned int           nr,
814             volatile unsigned long *addr)
815 {
816     atomic_set_acq_long(addr, (1 << nr));
817 }
818
819 void
820 bxe_clear_bit(int                    nr,
821               volatile unsigned long *addr)
822 {
823     atomic_clear_acq_long(addr, (1 << nr));
824 }
825
826 int
827 bxe_test_and_set_bit(int                    nr,
828                        volatile unsigned long *addr)
829 {
830     unsigned long x;
831     nr = (1 << nr);
832     do {
833         x = *addr;
834     } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
835     // if (x & nr) bit_was_set; else bit_was_not_set;
836     return (x & nr);
837 }
838
839 int
840 bxe_test_and_clear_bit(int                    nr,
841                        volatile unsigned long *addr)
842 {
843     unsigned long x;
844     nr = (1 << nr);
845     do {
846         x = *addr;
847     } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
848     // if (x & nr) bit_was_set; else bit_was_not_set;
849     return (x & nr);
850 }
851
852 int
853 bxe_cmpxchg(volatile int *addr,
854             int          old,
855             int          new)
856 {
857     int x;
858     do {
859         x = *addr;
860     } while (atomic_cmpset_acq_int(addr, old, new) == 0);
861     return (x);
862 }
863
864 /*
865  * Get DMA memory from the OS.
866  *
867  * Validates that the OS has provided DMA buffers in response to a
868  * bus_dmamap_load call and saves the physical address of those buffers.
869  * When the callback is used the OS will return 0 for the mapping function
870  * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
871  * failures back to the caller.
872  *
873  * Returns:
874  *   Nothing.
875  */
876 static void
877 bxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
878 {
879     struct bxe_dma *dma = arg;
880
881     if (error) {
882         dma->paddr = 0;
883         dma->nseg  = 0;
884         BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
885     } else {
886         dma->paddr = segs->ds_addr;
887         dma->nseg  = nseg;
888     }
889 }
890
891 /*
892  * Allocate a block of memory and map it for DMA. No partial completions
893  * allowed and release any resources acquired if we can't acquire all
894  * resources.
895  *
896  * Returns:
897  *   0 = Success, !0 = Failure
898  */
899 int
900 bxe_dma_alloc(struct bxe_softc *sc,
901               bus_size_t       size,
902               struct bxe_dma   *dma,
903               const char       *msg)
904 {
905     int rc;
906
907     if (dma->size > 0) {
908         BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
909               (unsigned long)dma->size);
910         return (1);
911     }
912
913     memset(dma, 0, sizeof(*dma)); /* sanity */
914     dma->sc   = sc;
915     dma->size = size;
916     snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
917
918     rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
919                             BCM_PAGE_SIZE,      /* alignment */
920                             0,                  /* boundary limit */
921                             BUS_SPACE_MAXADDR,  /* restricted low */
922                             BUS_SPACE_MAXADDR,  /* restricted hi */
923                             NULL,               /* addr filter() */
924                             NULL,               /* addr filter() arg */
925                             size,               /* max map size */
926                             1,                  /* num discontinuous */
927                             size,               /* max seg size */
928                             BUS_DMA_ALLOCNOW,   /* flags */
929                             NULL,               /* lock() */
930                             NULL,               /* lock() arg */
931                             &dma->tag);         /* returned dma tag */
932     if (rc != 0) {
933         BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
934         memset(dma, 0, sizeof(*dma));
935         return (1);
936     }
937
938     rc = bus_dmamem_alloc(dma->tag,
939                           (void **)&dma->vaddr,
940                           (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
941                           &dma->map);
942     if (rc != 0) {
943         BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
944         bus_dma_tag_destroy(dma->tag);
945         memset(dma, 0, sizeof(*dma));
946         return (1);
947     }
948
949     rc = bus_dmamap_load(dma->tag,
950                          dma->map,
951                          dma->vaddr,
952                          size,
953                          bxe_dma_map_addr, /* BLOGD in here */
954                          dma,
955                          BUS_DMA_NOWAIT);
956     if (rc != 0) {
957         BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
958         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
959         bus_dma_tag_destroy(dma->tag);
960         memset(dma, 0, sizeof(*dma));
961         return (1);
962     }
963
964     return (0);
965 }
966
967 void
968 bxe_dma_free(struct bxe_softc *sc,
969              struct bxe_dma   *dma)
970 {
971     if (dma->size > 0) {
972         DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
973
974         bus_dmamap_sync(dma->tag, dma->map,
975                         (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
976         bus_dmamap_unload(dma->tag, dma->map);
977         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
978         bus_dma_tag_destroy(dma->tag);
979     }
980
981     memset(dma, 0, sizeof(*dma));
982 }
983
984 /*
985  * These indirect read and write routines are only during init.
986  * The locking is handled by the MCP.
987  */
988
989 void
990 bxe_reg_wr_ind(struct bxe_softc *sc,
991                uint32_t         addr,
992                uint32_t         val)
993 {
994     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
995     pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
996     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
997 }
998
999 uint32_t
1000 bxe_reg_rd_ind(struct bxe_softc *sc,
1001                uint32_t         addr)
1002 {
1003     uint32_t val;
1004
1005     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
1006     val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
1007     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
1008
1009     return (val);
1010 }
1011
1012 static int
1013 bxe_acquire_hw_lock(struct bxe_softc *sc,
1014                     uint32_t         resource)
1015 {
1016     uint32_t lock_status;
1017     uint32_t resource_bit = (1 << resource);
1018     int func = SC_FUNC(sc);
1019     uint32_t hw_lock_control_reg;
1020     int cnt;
1021
1022     /* validate the resource is within range */
1023     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1024         BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1025             " resource_bit 0x%x\n", resource, resource_bit);
1026         return (-1);
1027     }
1028
1029     if (func <= 5) {
1030         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1031     } else {
1032         hw_lock_control_reg =
1033                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1034     }
1035
1036     /* validate the resource is not already taken */
1037     lock_status = REG_RD(sc, hw_lock_control_reg);
1038     if (lock_status & resource_bit) {
1039         BLOGE(sc, "resource (0x%x) in use (status 0x%x bit 0x%x)\n",
1040               resource, lock_status, resource_bit);
1041         return (-1);
1042     }
1043
1044     /* try every 5ms for 5 seconds */
1045     for (cnt = 0; cnt < 1000; cnt++) {
1046         REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1047         lock_status = REG_RD(sc, hw_lock_control_reg);
1048         if (lock_status & resource_bit) {
1049             return (0);
1050         }
1051         DELAY(5000);
1052     }
1053
1054     BLOGE(sc, "Resource 0x%x resource_bit 0x%x lock timeout!\n",
1055         resource, resource_bit);
1056     return (-1);
1057 }
1058
1059 static int
1060 bxe_release_hw_lock(struct bxe_softc *sc,
1061                     uint32_t         resource)
1062 {
1063     uint32_t lock_status;
1064     uint32_t resource_bit = (1 << resource);
1065     int func = SC_FUNC(sc);
1066     uint32_t hw_lock_control_reg;
1067
1068     /* validate the resource is within range */
1069     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1070         BLOGE(sc, "(resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE)"
1071             " resource_bit 0x%x\n", resource, resource_bit);
1072         return (-1);
1073     }
1074
1075     if (func <= 5) {
1076         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1077     } else {
1078         hw_lock_control_reg =
1079                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1080     }
1081
1082     /* validate the resource is currently taken */
1083     lock_status = REG_RD(sc, hw_lock_control_reg);
1084     if (!(lock_status & resource_bit)) {
1085         BLOGE(sc, "resource (0x%x) not in use (status 0x%x bit 0x%x)\n",
1086               resource, lock_status, resource_bit);
1087         return (-1);
1088     }
1089
1090     REG_WR(sc, hw_lock_control_reg, resource_bit);
1091     return (0);
1092 }
1093 static void bxe_acquire_phy_lock(struct bxe_softc *sc)
1094 {
1095         BXE_PHY_LOCK(sc);
1096         bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO); 
1097 }
1098
1099 static void bxe_release_phy_lock(struct bxe_softc *sc)
1100 {
1101         bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO); 
1102         BXE_PHY_UNLOCK(sc);
1103 }
1104 /*
1105  * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1106  * had we done things the other way around, if two pfs from the same port
1107  * would attempt to access nvram at the same time, we could run into a
1108  * scenario such as:
1109  * pf A takes the port lock.
1110  * pf B succeeds in taking the same lock since they are from the same port.
1111  * pf A takes the per pf misc lock. Performs eeprom access.
1112  * pf A finishes. Unlocks the per pf misc lock.
1113  * Pf B takes the lock and proceeds to perform it's own access.
1114  * pf A unlocks the per port lock, while pf B is still working (!).
1115  * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1116  * access corrupted by pf B).*
1117  */
1118 static int
1119 bxe_acquire_nvram_lock(struct bxe_softc *sc)
1120 {
1121     int port = SC_PORT(sc);
1122     int count, i;
1123     uint32_t val = 0;
1124
1125     /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1126     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1127
1128     /* adjust timeout for emulation/FPGA */
1129     count = NVRAM_TIMEOUT_COUNT;
1130     if (CHIP_REV_IS_SLOW(sc)) {
1131         count *= 100;
1132     }
1133
1134     /* request access to nvram interface */
1135     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1136            (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1137
1138     for (i = 0; i < count*10; i++) {
1139         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1140         if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1141             break;
1142         }
1143
1144         DELAY(5);
1145     }
1146
1147     if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1148         BLOGE(sc, "Cannot get access to nvram interface "
1149             "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1150             port, val);
1151         return (-1);
1152     }
1153
1154     return (0);
1155 }
1156
1157 static int
1158 bxe_release_nvram_lock(struct bxe_softc *sc)
1159 {
1160     int port = SC_PORT(sc);
1161     int count, i;
1162     uint32_t val = 0;
1163
1164     /* adjust timeout for emulation/FPGA */
1165     count = NVRAM_TIMEOUT_COUNT;
1166     if (CHIP_REV_IS_SLOW(sc)) {
1167         count *= 100;
1168     }
1169
1170     /* relinquish nvram interface */
1171     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1172            (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1173
1174     for (i = 0; i < count*10; i++) {
1175         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1176         if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1177             break;
1178         }
1179
1180         DELAY(5);
1181     }
1182
1183     if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1184         BLOGE(sc, "Cannot free access to nvram interface "
1185             "port %d val 0x%x (MCPR_NVM_SW_ARB_ARB_ARB1 << port)\n",
1186             port, val);
1187         return (-1);
1188     }
1189
1190     /* release HW lock: protect against other PFs in PF Direct Assignment */
1191     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1192
1193     return (0);
1194 }
1195
1196 static void
1197 bxe_enable_nvram_access(struct bxe_softc *sc)
1198 {
1199     uint32_t val;
1200
1201     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1202
1203     /* enable both bits, even on read */
1204     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1205            (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1206 }
1207
1208 static void
1209 bxe_disable_nvram_access(struct bxe_softc *sc)
1210 {
1211     uint32_t val;
1212
1213     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1214
1215     /* disable both bits, even after read */
1216     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1217            (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1218                     MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1219 }
1220
1221 static int
1222 bxe_nvram_read_dword(struct bxe_softc *sc,
1223                      uint32_t         offset,
1224                      uint32_t         *ret_val,
1225                      uint32_t         cmd_flags)
1226 {
1227     int count, i, rc;
1228     uint32_t val;
1229
1230     /* build the command word */
1231     cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1232
1233     /* need to clear DONE bit separately */
1234     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1235
1236     /* address of the NVRAM to read from */
1237     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1238            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1239
1240     /* issue a read command */
1241     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1242
1243     /* adjust timeout for emulation/FPGA */
1244     count = NVRAM_TIMEOUT_COUNT;
1245     if (CHIP_REV_IS_SLOW(sc)) {
1246         count *= 100;
1247     }
1248
1249     /* wait for completion */
1250     *ret_val = 0;
1251     rc = -1;
1252     for (i = 0; i < count; i++) {
1253         DELAY(5);
1254         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1255
1256         if (val & MCPR_NVM_COMMAND_DONE) {
1257             val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1258             /* we read nvram data in cpu order
1259              * but ethtool sees it as an array of bytes
1260              * converting to big-endian will do the work
1261              */
1262             *ret_val = htobe32(val);
1263             rc = 0;
1264             break;
1265         }
1266     }
1267
1268     if (rc == -1) {
1269         BLOGE(sc, "nvram read timeout expired "
1270             "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1271             offset, cmd_flags, val);
1272     }
1273
1274     return (rc);
1275 }
1276
1277 static int
1278 bxe_nvram_read(struct bxe_softc *sc,
1279                uint32_t         offset,
1280                uint8_t          *ret_buf,
1281                int              buf_size)
1282 {
1283     uint32_t cmd_flags;
1284     uint32_t val;
1285     int rc;
1286
1287     if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1288         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1289               offset, buf_size);
1290         return (-1);
1291     }
1292
1293     if ((offset + buf_size) > sc->devinfo.flash_size) {
1294         BLOGE(sc, "Invalid parameter, "
1295                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1296               offset, buf_size, sc->devinfo.flash_size);
1297         return (-1);
1298     }
1299
1300     /* request access to nvram interface */
1301     rc = bxe_acquire_nvram_lock(sc);
1302     if (rc) {
1303         return (rc);
1304     }
1305
1306     /* enable access to nvram interface */
1307     bxe_enable_nvram_access(sc);
1308
1309     /* read the first word(s) */
1310     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1311     while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1312         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1313         memcpy(ret_buf, &val, 4);
1314
1315         /* advance to the next dword */
1316         offset += sizeof(uint32_t);
1317         ret_buf += sizeof(uint32_t);
1318         buf_size -= sizeof(uint32_t);
1319         cmd_flags = 0;
1320     }
1321
1322     if (rc == 0) {
1323         cmd_flags |= MCPR_NVM_COMMAND_LAST;
1324         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1325         memcpy(ret_buf, &val, 4);
1326     }
1327
1328     /* disable access to nvram interface */
1329     bxe_disable_nvram_access(sc);
1330     bxe_release_nvram_lock(sc);
1331
1332     return (rc);
1333 }
1334
1335 static int
1336 bxe_nvram_write_dword(struct bxe_softc *sc,
1337                       uint32_t         offset,
1338                       uint32_t         val,
1339                       uint32_t         cmd_flags)
1340 {
1341     int count, i, rc;
1342
1343     /* build the command word */
1344     cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1345
1346     /* need to clear DONE bit separately */
1347     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1348
1349     /* write the data */
1350     REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1351
1352     /* address of the NVRAM to write to */
1353     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1354            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1355
1356     /* issue the write command */
1357     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1358
1359     /* adjust timeout for emulation/FPGA */
1360     count = NVRAM_TIMEOUT_COUNT;
1361     if (CHIP_REV_IS_SLOW(sc)) {
1362         count *= 100;
1363     }
1364
1365     /* wait for completion */
1366     rc = -1;
1367     for (i = 0; i < count; i++) {
1368         DELAY(5);
1369         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1370         if (val & MCPR_NVM_COMMAND_DONE) {
1371             rc = 0;
1372             break;
1373         }
1374     }
1375
1376     if (rc == -1) {
1377         BLOGE(sc, "nvram write timeout expired "
1378             "(offset 0x%x cmd_flags 0x%x val 0x%x)\n",
1379             offset, cmd_flags, val);
1380     }
1381
1382     return (rc);
1383 }
1384
1385 #define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1386
1387 static int
1388 bxe_nvram_write1(struct bxe_softc *sc,
1389                  uint32_t         offset,
1390                  uint8_t          *data_buf,
1391                  int              buf_size)
1392 {
1393     uint32_t cmd_flags;
1394     uint32_t align_offset;
1395     uint32_t val;
1396     int rc;
1397
1398     if ((offset + buf_size) > sc->devinfo.flash_size) {
1399         BLOGE(sc, "Invalid parameter, "
1400                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1401               offset, buf_size, sc->devinfo.flash_size);
1402         return (-1);
1403     }
1404
1405     /* request access to nvram interface */
1406     rc = bxe_acquire_nvram_lock(sc);
1407     if (rc) {
1408         return (rc);
1409     }
1410
1411     /* enable access to nvram interface */
1412     bxe_enable_nvram_access(sc);
1413
1414     cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1415     align_offset = (offset & ~0x03);
1416     rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1417
1418     if (rc == 0) {
1419         val &= ~(0xff << BYTE_OFFSET(offset));
1420         val |= (*data_buf << BYTE_OFFSET(offset));
1421
1422         /* nvram data is returned as an array of bytes
1423          * convert it back to cpu order
1424          */
1425         val = be32toh(val);
1426
1427         rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1428     }
1429
1430     /* disable access to nvram interface */
1431     bxe_disable_nvram_access(sc);
1432     bxe_release_nvram_lock(sc);
1433
1434     return (rc);
1435 }
1436
1437 static int
1438 bxe_nvram_write(struct bxe_softc *sc,
1439                 uint32_t         offset,
1440                 uint8_t          *data_buf,
1441                 int              buf_size)
1442 {
1443     uint32_t cmd_flags;
1444     uint32_t val;
1445     uint32_t written_so_far;
1446     int rc;
1447
1448     if (buf_size == 1) {
1449         return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1450     }
1451
1452     if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1453         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1454               offset, buf_size);
1455         return (-1);
1456     }
1457
1458     if (buf_size == 0) {
1459         return (0); /* nothing to do */
1460     }
1461
1462     if ((offset + buf_size) > sc->devinfo.flash_size) {
1463         BLOGE(sc, "Invalid parameter, "
1464                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1465               offset, buf_size, sc->devinfo.flash_size);
1466         return (-1);
1467     }
1468
1469     /* request access to nvram interface */
1470     rc = bxe_acquire_nvram_lock(sc);
1471     if (rc) {
1472         return (rc);
1473     }
1474
1475     /* enable access to nvram interface */
1476     bxe_enable_nvram_access(sc);
1477
1478     written_so_far = 0;
1479     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1480     while ((written_so_far < buf_size) && (rc == 0)) {
1481         if (written_so_far == (buf_size - sizeof(uint32_t))) {
1482             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1483         } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1484             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1485         } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1486             cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1487         }
1488
1489         memcpy(&val, data_buf, 4);
1490
1491         rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1492
1493         /* advance to the next dword */
1494         offset += sizeof(uint32_t);
1495         data_buf += sizeof(uint32_t);
1496         written_so_far += sizeof(uint32_t);
1497         cmd_flags = 0;
1498     }
1499
1500     /* disable access to nvram interface */
1501     bxe_disable_nvram_access(sc);
1502     bxe_release_nvram_lock(sc);
1503
1504     return (rc);
1505 }
1506
1507 /* copy command into DMAE command memory and set DMAE command Go */
1508 void
1509 bxe_post_dmae(struct bxe_softc    *sc,
1510               struct dmae_cmd *dmae,
1511               int                 idx)
1512 {
1513     uint32_t cmd_offset;
1514     int i;
1515
1516     cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_cmd) * idx));
1517     for (i = 0; i < ((sizeof(struct dmae_cmd) / 4)); i++) {
1518         REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1519     }
1520
1521     REG_WR(sc, dmae_reg_go_c[idx], 1);
1522 }
1523
1524 uint32_t
1525 bxe_dmae_opcode_add_comp(uint32_t opcode,
1526                          uint8_t  comp_type)
1527 {
1528     return (opcode | ((comp_type << DMAE_CMD_C_DST_SHIFT) |
1529                       DMAE_CMD_C_TYPE_ENABLE));
1530 }
1531
1532 uint32_t
1533 bxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1534 {
1535     return (opcode & ~DMAE_CMD_SRC_RESET);
1536 }
1537
1538 uint32_t
1539 bxe_dmae_opcode(struct bxe_softc *sc,
1540                 uint8_t          src_type,
1541                 uint8_t          dst_type,
1542                 uint8_t          with_comp,
1543                 uint8_t          comp_type)
1544 {
1545     uint32_t opcode = 0;
1546
1547     opcode |= ((src_type << DMAE_CMD_SRC_SHIFT) |
1548                (dst_type << DMAE_CMD_DST_SHIFT));
1549
1550     opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
1551
1552     opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1553
1554     opcode |= ((SC_VN(sc) << DMAE_CMD_E1HVN_SHIFT) |
1555                (SC_VN(sc) << DMAE_CMD_DST_VN_SHIFT));
1556
1557     opcode |= (DMAE_COM_SET_ERR << DMAE_CMD_ERR_POLICY_SHIFT);
1558
1559 #ifdef __BIG_ENDIAN
1560     opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1561 #else
1562     opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1563 #endif
1564
1565     if (with_comp) {
1566         opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1567     }
1568
1569     return (opcode);
1570 }
1571
1572 static void
1573 bxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1574                         struct dmae_cmd *dmae,
1575                         uint8_t             src_type,
1576                         uint8_t             dst_type)
1577 {
1578     memset(dmae, 0, sizeof(struct dmae_cmd));
1579
1580     /* set the opcode */
1581     dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1582                                    TRUE, DMAE_COMP_PCI);
1583
1584     /* fill in the completion parameters */
1585     dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1586     dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1587     dmae->comp_val     = DMAE_COMP_VAL;
1588 }
1589
1590 /* issue a DMAE command over the init channel and wait for completion */
1591 static int
1592 bxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1593                          struct dmae_cmd *dmae)
1594 {
1595     uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1596     int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1597
1598     BXE_DMAE_LOCK(sc);
1599
1600     /* reset completion */
1601     *wb_comp = 0;
1602
1603     /* post the command on the channel used for initializations */
1604     bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1605
1606     /* wait for completion */
1607     DELAY(5);
1608
1609     while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1610         if (!timeout ||
1611             (sc->recovery_state != BXE_RECOVERY_DONE &&
1612              sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1613             BLOGE(sc, "DMAE timeout! *wb_comp 0x%x recovery_state 0x%x\n",
1614                 *wb_comp, sc->recovery_state);
1615             BXE_DMAE_UNLOCK(sc);
1616             return (DMAE_TIMEOUT);
1617         }
1618
1619         timeout--;
1620         DELAY(50);
1621     }
1622
1623     if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1624         BLOGE(sc, "DMAE PCI error! *wb_comp 0x%x recovery_state 0x%x\n",
1625                 *wb_comp, sc->recovery_state);
1626         BXE_DMAE_UNLOCK(sc);
1627         return (DMAE_PCI_ERROR);
1628     }
1629
1630     BXE_DMAE_UNLOCK(sc);
1631     return (0);
1632 }
1633
1634 void
1635 bxe_read_dmae(struct bxe_softc *sc,
1636               uint32_t         src_addr,
1637               uint32_t         len32)
1638 {
1639     struct dmae_cmd dmae;
1640     uint32_t *data;
1641     int i, rc;
1642
1643     DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1644
1645     if (!sc->dmae_ready) {
1646         data = BXE_SP(sc, wb_data[0]);
1647
1648         for (i = 0; i < len32; i++) {
1649             data[i] = (CHIP_IS_E1(sc)) ?
1650                           bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1651                           REG_RD(sc, (src_addr + (i * 4)));
1652         }
1653
1654         return;
1655     }
1656
1657     /* set opcode and fixed command fields */
1658     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1659
1660     /* fill in addresses and len */
1661     dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1662     dmae.src_addr_hi = 0;
1663     dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1664     dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1665     dmae.len         = len32;
1666
1667     /* issue the command and wait for completion */
1668     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1669         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1670     };
1671 }
1672
1673 void
1674 bxe_write_dmae(struct bxe_softc *sc,
1675                bus_addr_t       dma_addr,
1676                uint32_t         dst_addr,
1677                uint32_t         len32)
1678 {
1679     struct dmae_cmd dmae;
1680     int rc;
1681
1682     if (!sc->dmae_ready) {
1683         DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1684
1685         if (CHIP_IS_E1(sc)) {
1686             ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1687         } else {
1688             ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1689         }
1690
1691         return;
1692     }
1693
1694     /* set opcode and fixed command fields */
1695     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1696
1697     /* fill in addresses and len */
1698     dmae.src_addr_lo = U64_LO(dma_addr);
1699     dmae.src_addr_hi = U64_HI(dma_addr);
1700     dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1701     dmae.dst_addr_hi = 0;
1702     dmae.len         = len32;
1703
1704     /* issue the command and wait for completion */
1705     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1706         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1707     }
1708 }
1709
1710 void
1711 bxe_write_dmae_phys_len(struct bxe_softc *sc,
1712                         bus_addr_t       phys_addr,
1713                         uint32_t         addr,
1714                         uint32_t         len)
1715 {
1716     int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1717     int offset = 0;
1718
1719     while (len > dmae_wr_max) {
1720         bxe_write_dmae(sc,
1721                        (phys_addr + offset), /* src DMA address */
1722                        (addr + offset),      /* dst GRC address */
1723                        dmae_wr_max);
1724         offset += (dmae_wr_max * 4);
1725         len -= dmae_wr_max;
1726     }
1727
1728     bxe_write_dmae(sc,
1729                    (phys_addr + offset), /* src DMA address */
1730                    (addr + offset),      /* dst GRC address */
1731                    len);
1732 }
1733
1734 void
1735 bxe_set_ctx_validation(struct bxe_softc   *sc,
1736                        struct eth_context *cxt,
1737                        uint32_t           cid)
1738 {
1739     /* ustorm cxt validation */
1740     cxt->ustorm_ag_context.cdu_usage =
1741         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1742             CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1743     /* xcontext validation */
1744     cxt->xstorm_ag_context.cdu_reserved =
1745         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1746             CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1747 }
1748
1749 static void
1750 bxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1751                             uint8_t          port,
1752                             uint8_t          fw_sb_id,
1753                             uint8_t          sb_index,
1754                             uint8_t          ticks)
1755 {
1756     uint32_t addr =
1757         (BAR_CSTRORM_INTMEM +
1758          CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1759
1760     REG_WR8(sc, addr, ticks);
1761
1762     BLOGD(sc, DBG_LOAD,
1763           "port %d fw_sb_id %d sb_index %d ticks %d\n",
1764           port, fw_sb_id, sb_index, ticks);
1765 }
1766
1767 static void
1768 bxe_storm_memset_hc_disable(struct bxe_softc *sc,
1769                             uint8_t          port,
1770                             uint16_t         fw_sb_id,
1771                             uint8_t          sb_index,
1772                             uint8_t          disable)
1773 {
1774     uint32_t enable_flag =
1775         (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1776     uint32_t addr =
1777         (BAR_CSTRORM_INTMEM +
1778          CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1779     uint8_t flags;
1780
1781     /* clear and set */
1782     flags = REG_RD8(sc, addr);
1783     flags &= ~HC_INDEX_DATA_HC_ENABLED;
1784     flags |= enable_flag;
1785     REG_WR8(sc, addr, flags);
1786
1787     BLOGD(sc, DBG_LOAD,
1788           "port %d fw_sb_id %d sb_index %d disable %d\n",
1789           port, fw_sb_id, sb_index, disable);
1790 }
1791
1792 void
1793 bxe_update_coalesce_sb_index(struct bxe_softc *sc,
1794                              uint8_t          fw_sb_id,
1795                              uint8_t          sb_index,
1796                              uint8_t          disable,
1797                              uint16_t         usec)
1798 {
1799     int port = SC_PORT(sc);
1800     uint8_t ticks = (usec / 4); /* XXX ??? */
1801
1802     bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1803
1804     disable = (disable) ? 1 : ((usec) ? 0 : 1);
1805     bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1806 }
1807
1808 void
1809 elink_cb_udelay(struct bxe_softc *sc,
1810                 uint32_t         usecs)
1811 {
1812     DELAY(usecs);
1813 }
1814
1815 uint32_t
1816 elink_cb_reg_read(struct bxe_softc *sc,
1817                   uint32_t         reg_addr)
1818 {
1819     return (REG_RD(sc, reg_addr));
1820 }
1821
1822 void
1823 elink_cb_reg_write(struct bxe_softc *sc,
1824                    uint32_t         reg_addr,
1825                    uint32_t         val)
1826 {
1827     REG_WR(sc, reg_addr, val);
1828 }
1829
1830 void
1831 elink_cb_reg_wb_write(struct bxe_softc *sc,
1832                       uint32_t         offset,
1833                       uint32_t         *wb_write,
1834                       uint16_t         len)
1835 {
1836     REG_WR_DMAE(sc, offset, wb_write, len);
1837 }
1838
1839 void
1840 elink_cb_reg_wb_read(struct bxe_softc *sc,
1841                      uint32_t         offset,
1842                      uint32_t         *wb_write,
1843                      uint16_t         len)
1844 {
1845     REG_RD_DMAE(sc, offset, wb_write, len);
1846 }
1847
1848 uint8_t
1849 elink_cb_path_id(struct bxe_softc *sc)
1850 {
1851     return (SC_PATH(sc));
1852 }
1853
1854 void
1855 elink_cb_event_log(struct bxe_softc     *sc,
1856                    const elink_log_id_t elink_log_id,
1857                    ...)
1858 {
1859     /* XXX */
1860     BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1861 }
1862
1863 static int
1864 bxe_set_spio(struct bxe_softc *sc,
1865              int              spio,
1866              uint32_t         mode)
1867 {
1868     uint32_t spio_reg;
1869
1870     /* Only 2 SPIOs are configurable */
1871     if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1872         BLOGE(sc, "Invalid SPIO 0x%x mode 0x%x\n", spio, mode);
1873         return (-1);
1874     }
1875
1876     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1877
1878     /* read SPIO and mask except the float bits */
1879     spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1880
1881     switch (mode) {
1882     case MISC_SPIO_OUTPUT_LOW:
1883         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1884         /* clear FLOAT and set CLR */
1885         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1886         spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1887         break;
1888
1889     case MISC_SPIO_OUTPUT_HIGH:
1890         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1891         /* clear FLOAT and set SET */
1892         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1893         spio_reg |=  (spio << MISC_SPIO_SET_POS);
1894         break;
1895
1896     case MISC_SPIO_INPUT_HI_Z:
1897         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1898         /* set FLOAT */
1899         spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1900         break;
1901
1902     default:
1903         break;
1904     }
1905
1906     REG_WR(sc, MISC_REG_SPIO, spio_reg);
1907     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1908
1909     return (0);
1910 }
1911
1912 static int
1913 bxe_gpio_read(struct bxe_softc *sc,
1914               int              gpio_num,
1915               uint8_t          port)
1916 {
1917     /* The GPIO should be swapped if swap register is set and active */
1918     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1919                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1920     int gpio_shift = (gpio_num +
1921                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1922     uint32_t gpio_mask = (1 << gpio_shift);
1923     uint32_t gpio_reg;
1924
1925     if (gpio_num > MISC_REGISTERS_GPIO_3) {
1926         BLOGE(sc, "Invalid GPIO %d port 0x%x gpio_port %d gpio_shift %d"
1927             " gpio_mask 0x%x\n", gpio_num, port, gpio_port, gpio_shift,
1928             gpio_mask);
1929         return (-1);
1930     }
1931
1932     /* read GPIO value */
1933     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
1934
1935     /* get the requested pin value */
1936     return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
1937 }
1938
1939 static int
1940 bxe_gpio_write(struct bxe_softc *sc,
1941                int              gpio_num,
1942                uint32_t         mode,
1943                uint8_t          port)
1944 {
1945     /* The GPIO should be swapped if swap register is set and active */
1946     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
1947                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
1948     int gpio_shift = (gpio_num +
1949                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
1950     uint32_t gpio_mask = (1 << gpio_shift);
1951     uint32_t gpio_reg;
1952
1953     if (gpio_num > MISC_REGISTERS_GPIO_3) {
1954         BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
1955             " gpio_shift %d gpio_mask 0x%x\n",
1956             gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
1957         return (-1);
1958     }
1959
1960     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1961
1962     /* read GPIO and mask except the float bits */
1963     gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1964
1965     switch (mode) {
1966     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1967         BLOGD(sc, DBG_PHY,
1968               "Set GPIO %d (shift %d) -> output low\n",
1969               gpio_num, gpio_shift);
1970         /* clear FLOAT and set CLR */
1971         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1972         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1973         break;
1974
1975     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1976         BLOGD(sc, DBG_PHY,
1977               "Set GPIO %d (shift %d) -> output high\n",
1978               gpio_num, gpio_shift);
1979         /* clear FLOAT and set SET */
1980         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1981         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1982         break;
1983
1984     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1985         BLOGD(sc, DBG_PHY,
1986               "Set GPIO %d (shift %d) -> input\n",
1987               gpio_num, gpio_shift);
1988         /* set FLOAT */
1989         gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1990         break;
1991
1992     default:
1993         break;
1994     }
1995
1996     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
1997     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
1998
1999     return (0);
2000 }
2001
2002 static int
2003 bxe_gpio_mult_write(struct bxe_softc *sc,
2004                     uint8_t          pins,
2005                     uint32_t         mode)
2006 {
2007     uint32_t gpio_reg;
2008
2009     /* any port swapping should be handled by caller */
2010
2011     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2012
2013     /* read GPIO and mask except the float bits */
2014     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
2015     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2016     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
2017     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
2018
2019     switch (mode) {
2020     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2021         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
2022         /* set CLR */
2023         gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
2024         break;
2025
2026     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2027         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
2028         /* set SET */
2029         gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
2030         break;
2031
2032     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2033         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
2034         /* set FLOAT */
2035         gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2036         break;
2037
2038     default:
2039         BLOGE(sc, "Invalid GPIO mode assignment pins 0x%x mode 0x%x"
2040             " gpio_reg 0x%x\n", pins, mode, gpio_reg);
2041         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2042         return (-1);
2043     }
2044
2045     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2046     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2047
2048     return (0);
2049 }
2050
2051 static int
2052 bxe_gpio_int_write(struct bxe_softc *sc,
2053                    int              gpio_num,
2054                    uint32_t         mode,
2055                    uint8_t          port)
2056 {
2057     /* The GPIO should be swapped if swap register is set and active */
2058     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2059                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2060     int gpio_shift = (gpio_num +
2061                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2062     uint32_t gpio_mask = (1 << gpio_shift);
2063     uint32_t gpio_reg;
2064
2065     if (gpio_num > MISC_REGISTERS_GPIO_3) {
2066         BLOGE(sc, "Invalid GPIO %d mode 0x%x port 0x%x gpio_port %d"
2067             " gpio_shift %d gpio_mask 0x%x\n",
2068             gpio_num, mode, port, gpio_port, gpio_shift, gpio_mask);
2069         return (-1);
2070     }
2071
2072     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2073
2074     /* read GPIO int */
2075     gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2076
2077     switch (mode) {
2078     case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2079         BLOGD(sc, DBG_PHY,
2080               "Clear GPIO INT %d (shift %d) -> output low\n",
2081               gpio_num, gpio_shift);
2082         /* clear SET and set CLR */
2083         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2084         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2085         break;
2086
2087     case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2088         BLOGD(sc, DBG_PHY,
2089               "Set GPIO INT %d (shift %d) -> output high\n",
2090               gpio_num, gpio_shift);
2091         /* clear CLR and set SET */
2092         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2093         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2094         break;
2095
2096     default:
2097         break;
2098     }
2099
2100     REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2101     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2102
2103     return (0);
2104 }
2105
2106 uint32_t
2107 elink_cb_gpio_read(struct bxe_softc *sc,
2108                    uint16_t         gpio_num,
2109                    uint8_t          port)
2110 {
2111     return (bxe_gpio_read(sc, gpio_num, port));
2112 }
2113
2114 uint8_t
2115 elink_cb_gpio_write(struct bxe_softc *sc,
2116                     uint16_t         gpio_num,
2117                     uint8_t          mode, /* 0=low 1=high */
2118                     uint8_t          port)
2119 {
2120     return (bxe_gpio_write(sc, gpio_num, mode, port));
2121 }
2122
2123 uint8_t
2124 elink_cb_gpio_mult_write(struct bxe_softc *sc,
2125                          uint8_t          pins,
2126                          uint8_t          mode) /* 0=low 1=high */
2127 {
2128     return (bxe_gpio_mult_write(sc, pins, mode));
2129 }
2130
2131 uint8_t
2132 elink_cb_gpio_int_write(struct bxe_softc *sc,
2133                         uint16_t         gpio_num,
2134                         uint8_t          mode, /* 0=low 1=high */
2135                         uint8_t          port)
2136 {
2137     return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2138 }
2139
2140 void
2141 elink_cb_notify_link_changed(struct bxe_softc *sc)
2142 {
2143     REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2144                 (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2145 }
2146
2147 /* send the MCP a request, block until there is a reply */
2148 uint32_t
2149 elink_cb_fw_command(struct bxe_softc *sc,
2150                     uint32_t         command,
2151                     uint32_t         param)
2152 {
2153     int mb_idx = SC_FW_MB_IDX(sc);
2154     uint32_t seq;
2155     uint32_t rc = 0;
2156     uint32_t cnt = 1;
2157     uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2158
2159     BXE_FWMB_LOCK(sc);
2160
2161     seq = ++sc->fw_seq;
2162     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2163     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2164
2165     BLOGD(sc, DBG_PHY,
2166           "wrote command 0x%08x to FW MB param 0x%08x\n",
2167           (command | seq), param);
2168
2169     /* Let the FW do it's magic. GIve it up to 5 seconds... */
2170     do {
2171         DELAY(delay * 1000);
2172         rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2173     } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2174
2175     BLOGD(sc, DBG_PHY,
2176           "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2177           cnt*delay, rc, seq);
2178
2179     /* is this a reply to our command? */
2180     if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2181         rc &= FW_MSG_CODE_MASK;
2182     } else {
2183         /* Ruh-roh! */
2184         BLOGE(sc, "FW failed to respond!\n");
2185         // XXX bxe_fw_dump(sc);
2186         rc = 0;
2187     }
2188
2189     BXE_FWMB_UNLOCK(sc);
2190     return (rc);
2191 }
2192
2193 static uint32_t
2194 bxe_fw_command(struct bxe_softc *sc,
2195                uint32_t         command,
2196                uint32_t         param)
2197 {
2198     return (elink_cb_fw_command(sc, command, param));
2199 }
2200
2201 static void
2202 __storm_memset_dma_mapping(struct bxe_softc *sc,
2203                            uint32_t         addr,
2204                            bus_addr_t       mapping)
2205 {
2206     REG_WR(sc, addr, U64_LO(mapping));
2207     REG_WR(sc, (addr + 4), U64_HI(mapping));
2208 }
2209
2210 static void
2211 storm_memset_spq_addr(struct bxe_softc *sc,
2212                       bus_addr_t       mapping,
2213                       uint16_t         abs_fid)
2214 {
2215     uint32_t addr = (XSEM_REG_FAST_MEMORY +
2216                      XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2217     __storm_memset_dma_mapping(sc, addr, mapping);
2218 }
2219
2220 static void
2221 storm_memset_vf_to_pf(struct bxe_softc *sc,
2222                       uint16_t         abs_fid,
2223                       uint16_t         pf_id)
2224 {
2225     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2226     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2227     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2228     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2229 }
2230
2231 static void
2232 storm_memset_func_en(struct bxe_softc *sc,
2233                      uint16_t         abs_fid,
2234                      uint8_t          enable)
2235 {
2236     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2237     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2238     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2239     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2240 }
2241
2242 static void
2243 storm_memset_eq_data(struct bxe_softc       *sc,
2244                      struct event_ring_data *eq_data,
2245                      uint16_t               pfid)
2246 {
2247     uint32_t addr;
2248     size_t size;
2249
2250     addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2251     size = sizeof(struct event_ring_data);
2252     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2253 }
2254
2255 static void
2256 storm_memset_eq_prod(struct bxe_softc *sc,
2257                      uint16_t         eq_prod,
2258                      uint16_t         pfid)
2259 {
2260     uint32_t addr = (BAR_CSTRORM_INTMEM +
2261                      CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2262     REG_WR16(sc, addr, eq_prod);
2263 }
2264
2265 /*
2266  * Post a slowpath command.
2267  *
2268  * A slowpath command is used to propogate a configuration change through
2269  * the controller in a controlled manner, allowing each STORM processor and
2270  * other H/W blocks to phase in the change.  The commands sent on the
2271  * slowpath are referred to as ramrods.  Depending on the ramrod used the
2272  * completion of the ramrod will occur in different ways.  Here's a
2273  * breakdown of ramrods and how they complete:
2274  *
2275  * RAMROD_CMD_ID_ETH_PORT_SETUP
2276  *   Used to setup the leading connection on a port.  Completes on the
2277  *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2278  *
2279  * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2280  *   Used to setup an additional connection on a port.  Completes on the
2281  *   RCQ of the multi-queue/RSS connection being initialized.
2282  *
2283  * RAMROD_CMD_ID_ETH_STAT_QUERY
2284  *   Used to force the storm processors to update the statistics database
2285  *   in host memory.  This ramrod is send on the leading connection CID and
2286  *   completes as an index increment of the CSTORM on the default status
2287  *   block.
2288  *
2289  * RAMROD_CMD_ID_ETH_UPDATE
2290  *   Used to update the state of the leading connection, usually to udpate
2291  *   the RSS indirection table.  Completes on the RCQ of the leading
2292  *   connection. (Not currently used under FreeBSD until OS support becomes
2293  *   available.)
2294  *
2295  * RAMROD_CMD_ID_ETH_HALT
2296  *   Used when tearing down a connection prior to driver unload.  Completes
2297  *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2298  *   use this on the leading connection.
2299  *
2300  * RAMROD_CMD_ID_ETH_SET_MAC
2301  *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2302  *   the RCQ of the leading connection.
2303  *
2304  * RAMROD_CMD_ID_ETH_CFC_DEL
2305  *   Used when tearing down a conneciton prior to driver unload.  Completes
2306  *   on the RCQ of the leading connection (since the current connection
2307  *   has been completely removed from controller memory).
2308  *
2309  * RAMROD_CMD_ID_ETH_PORT_DEL
2310  *   Used to tear down the leading connection prior to driver unload,
2311  *   typically fp[0].  Completes as an index increment of the CSTORM on the
2312  *   default status block.
2313  *
2314  * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2315  *   Used for connection offload.  Completes on the RCQ of the multi-queue
2316  *   RSS connection that is being offloaded.  (Not currently used under
2317  *   FreeBSD.)
2318  *
2319  * There can only be one command pending per function.
2320  *
2321  * Returns:
2322  *   0 = Success, !0 = Failure.
2323  */
2324
2325 /* must be called under the spq lock */
2326 static inline
2327 struct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2328 {
2329     struct eth_spe *next_spe = sc->spq_prod_bd;
2330
2331     if (sc->spq_prod_bd == sc->spq_last_bd) {
2332         /* wrap back to the first eth_spq */
2333         sc->spq_prod_bd = sc->spq;
2334         sc->spq_prod_idx = 0;
2335     } else {
2336         sc->spq_prod_bd++;
2337         sc->spq_prod_idx++;
2338     }
2339
2340     return (next_spe);
2341 }
2342
2343 /* must be called under the spq lock */
2344 static inline
2345 void bxe_sp_prod_update(struct bxe_softc *sc)
2346 {
2347     int func = SC_FUNC(sc);
2348
2349     /*
2350      * Make sure that BD data is updated before writing the producer.
2351      * BD data is written to the memory, the producer is read from the
2352      * memory, thus we need a full memory barrier to ensure the ordering.
2353      */
2354     mb();
2355
2356     REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2357              sc->spq_prod_idx);
2358
2359     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2360                       BUS_SPACE_BARRIER_WRITE);
2361 }
2362
2363 /**
2364  * bxe_is_contextless_ramrod - check if the current command ends on EQ
2365  *
2366  * @cmd:      command to check
2367  * @cmd_type: command type
2368  */
2369 static inline
2370 int bxe_is_contextless_ramrod(int cmd,
2371                               int cmd_type)
2372 {
2373     if ((cmd_type == NONE_CONNECTION_TYPE) ||
2374         (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2375         (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2376         (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2377         (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2378         (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2379         (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2380         return (TRUE);
2381     } else {
2382         return (FALSE);
2383     }
2384 }
2385
2386 /**
2387  * bxe_sp_post - place a single command on an SP ring
2388  *
2389  * @sc:         driver handle
2390  * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2391  * @cid:        SW CID the command is related to
2392  * @data_hi:    command private data address (high 32 bits)
2393  * @data_lo:    command private data address (low 32 bits)
2394  * @cmd_type:   command type (e.g. NONE, ETH)
2395  *
2396  * SP data is handled as if it's always an address pair, thus data fields are
2397  * not swapped to little endian in upper functions. Instead this function swaps
2398  * data as if it's two uint32 fields.
2399  */
2400 int
2401 bxe_sp_post(struct bxe_softc *sc,
2402             int              command,
2403             int              cid,
2404             uint32_t         data_hi,
2405             uint32_t         data_lo,
2406             int              cmd_type)
2407 {
2408     struct eth_spe *spe;
2409     uint16_t type;
2410     int common;
2411
2412     common = bxe_is_contextless_ramrod(command, cmd_type);
2413
2414     BXE_SP_LOCK(sc);
2415
2416     if (common) {
2417         if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2418             BLOGE(sc, "EQ ring is full!\n");
2419             BXE_SP_UNLOCK(sc);
2420             return (-1);
2421         }
2422     } else {
2423         if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2424             BLOGE(sc, "SPQ ring is full!\n");
2425             BXE_SP_UNLOCK(sc);
2426             return (-1);
2427         }
2428     }
2429
2430     spe = bxe_sp_get_next(sc);
2431
2432     /* CID needs port number to be encoded int it */
2433     spe->hdr.conn_and_cmd_data =
2434         htole32((command << SPE_HDR_T_CMD_ID_SHIFT) | HW_CID(sc, cid));
2435
2436     type = (cmd_type << SPE_HDR_T_CONN_TYPE_SHIFT) & SPE_HDR_T_CONN_TYPE;
2437
2438     /* TBD: Check if it works for VFs */
2439     type |= ((SC_FUNC(sc) << SPE_HDR_T_FUNCTION_ID_SHIFT) &
2440              SPE_HDR_T_FUNCTION_ID);
2441
2442     spe->hdr.type = htole16(type);
2443
2444     spe->data.update_data_addr.hi = htole32(data_hi);
2445     spe->data.update_data_addr.lo = htole32(data_lo);
2446
2447     /*
2448      * It's ok if the actual decrement is issued towards the memory
2449      * somewhere between the lock and unlock. Thus no more explict
2450      * memory barrier is needed.
2451      */
2452     if (common) {
2453         atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2454     } else {
2455         atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2456     }
2457
2458     BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2459     BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2460           BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2461     BLOGD(sc, DBG_SP,
2462           "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2463           sc->spq_prod_idx,
2464           (uint32_t)U64_HI(sc->spq_dma.paddr),
2465           (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2466           command,
2467           common,
2468           HW_CID(sc, cid),
2469           data_hi,
2470           data_lo,
2471           type,
2472           atomic_load_acq_long(&sc->cq_spq_left),
2473           atomic_load_acq_long(&sc->eq_spq_left));
2474
2475     bxe_sp_prod_update(sc);
2476
2477     BXE_SP_UNLOCK(sc);
2478     return (0);
2479 }
2480
2481 /**
2482  * bxe_debug_print_ind_table - prints the indirection table configuration.
2483  *
2484  * @sc: driver hanlde
2485  * @p:  pointer to rss configuration
2486  */
2487
2488 /*
2489  * FreeBSD Device probe function.
2490  *
2491  * Compares the device found to the driver's list of supported devices and
2492  * reports back to the bsd loader whether this is the right driver for the device.
2493  * This is the driver entry function called from the "kldload" command.
2494  *
2495  * Returns:
2496  *   BUS_PROBE_DEFAULT on success, positive value on failure.
2497  */
2498 static int
2499 bxe_probe(device_t dev)
2500 {
2501     struct bxe_softc *sc;
2502     struct bxe_device_type *t;
2503     char *descbuf;
2504     uint16_t did, sdid, svid, vid;
2505
2506     /* Find our device structure */
2507     sc = device_get_softc(dev);
2508     sc->dev = dev;
2509     t = bxe_devs;
2510
2511     /* Get the data for the device to be probed. */
2512     vid  = pci_get_vendor(dev);
2513     did  = pci_get_device(dev);
2514     svid = pci_get_subvendor(dev);
2515     sdid = pci_get_subdevice(dev);
2516
2517     BLOGD(sc, DBG_LOAD,
2518           "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
2519           "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
2520
2521     /* Look through the list of known devices for a match. */
2522     while (t->bxe_name != NULL) {
2523         if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2524             ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2525             ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2526             descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2527             if (descbuf == NULL)
2528                 return (ENOMEM);
2529
2530             /* Print out the device identity. */
2531             snprintf(descbuf, BXE_DEVDESC_MAX,
2532                      "%s (%c%d) BXE v:%s\n", t->bxe_name,
2533                      (((pci_read_config(dev, PCIR_REVID, 4) &
2534                         0xf0) >> 4) + 'A'),
2535                      (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2536                      BXE_DRIVER_VERSION);
2537
2538             device_set_desc_copy(dev, descbuf);
2539             free(descbuf, M_TEMP);
2540             return (BUS_PROBE_DEFAULT);
2541         }
2542         t++;
2543     }
2544
2545     return (ENXIO);
2546 }
2547
2548 static void
2549 bxe_init_mutexes(struct bxe_softc *sc)
2550 {
2551 #ifdef BXE_CORE_LOCK_SX
2552     snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2553              "bxe%d_core_lock", sc->unit);
2554     sx_init(&sc->core_sx, sc->core_sx_name);
2555 #else
2556     snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2557              "bxe%d_core_lock", sc->unit);
2558     mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2559 #endif
2560
2561     snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2562              "bxe%d_sp_lock", sc->unit);
2563     mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2564
2565     snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2566              "bxe%d_dmae_lock", sc->unit);
2567     mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2568
2569     snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2570              "bxe%d_phy_lock", sc->unit);
2571     mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2572
2573     snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2574              "bxe%d_fwmb_lock", sc->unit);
2575     mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2576
2577     snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2578              "bxe%d_print_lock", sc->unit);
2579     mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2580
2581     snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2582              "bxe%d_stats_lock", sc->unit);
2583     mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2584
2585     snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2586              "bxe%d_mcast_lock", sc->unit);
2587     mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2588 }
2589
2590 static void
2591 bxe_release_mutexes(struct bxe_softc *sc)
2592 {
2593 #ifdef BXE_CORE_LOCK_SX
2594     sx_destroy(&sc->core_sx);
2595 #else
2596     if (mtx_initialized(&sc->core_mtx)) {
2597         mtx_destroy(&sc->core_mtx);
2598     }
2599 #endif
2600
2601     if (mtx_initialized(&sc->sp_mtx)) {
2602         mtx_destroy(&sc->sp_mtx);
2603     }
2604
2605     if (mtx_initialized(&sc->dmae_mtx)) {
2606         mtx_destroy(&sc->dmae_mtx);
2607     }
2608
2609     if (mtx_initialized(&sc->port.phy_mtx)) {
2610         mtx_destroy(&sc->port.phy_mtx);
2611     }
2612
2613     if (mtx_initialized(&sc->fwmb_mtx)) {
2614         mtx_destroy(&sc->fwmb_mtx);
2615     }
2616
2617     if (mtx_initialized(&sc->print_mtx)) {
2618         mtx_destroy(&sc->print_mtx);
2619     }
2620
2621     if (mtx_initialized(&sc->stats_mtx)) {
2622         mtx_destroy(&sc->stats_mtx);
2623     }
2624
2625     if (mtx_initialized(&sc->mcast_mtx)) {
2626         mtx_destroy(&sc->mcast_mtx);
2627     }
2628 }
2629
2630 static void
2631 bxe_tx_disable(struct bxe_softc* sc)
2632 {
2633     struct ifnet *ifp = sc->ifnet;
2634
2635     /* tell the stack the driver is stopped and TX queue is full */
2636     if (ifp != NULL) {
2637         ifp->if_drv_flags = 0;
2638     }
2639 }
2640
2641 static void
2642 bxe_drv_pulse(struct bxe_softc *sc)
2643 {
2644     SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2645              sc->fw_drv_pulse_wr_seq);
2646 }
2647
2648 static inline uint16_t
2649 bxe_tx_avail(struct bxe_softc *sc,
2650              struct bxe_fastpath *fp)
2651 {
2652     int16_t  used;
2653     uint16_t prod;
2654     uint16_t cons;
2655
2656     prod = fp->tx_bd_prod;
2657     cons = fp->tx_bd_cons;
2658
2659     used = SUB_S16(prod, cons);
2660
2661     return (int16_t)(sc->tx_ring_size) - used;
2662 }
2663
2664 static inline int
2665 bxe_tx_queue_has_work(struct bxe_fastpath *fp)
2666 {
2667     uint16_t hw_cons;
2668
2669     mb(); /* status block fields can change */
2670     hw_cons = le16toh(*fp->tx_cons_sb);
2671     return (hw_cons != fp->tx_pkt_cons);
2672 }
2673
2674 static inline uint8_t
2675 bxe_has_tx_work(struct bxe_fastpath *fp)
2676 {
2677     /* expand this for multi-cos if ever supported */
2678     return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2679 }
2680
2681 static inline int
2682 bxe_has_rx_work(struct bxe_fastpath *fp)
2683 {
2684     uint16_t rx_cq_cons_sb;
2685
2686     mb(); /* status block fields can change */
2687     rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2688     if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2689         rx_cq_cons_sb++;
2690     return (fp->rx_cq_cons != rx_cq_cons_sb);
2691 }
2692
2693 static void
2694 bxe_sp_event(struct bxe_softc    *sc,
2695              struct bxe_fastpath *fp,
2696              union eth_rx_cqe    *rr_cqe)
2697 {
2698     int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2699     int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2700     enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2701     struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2702
2703     BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2704           fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2705
2706     switch (command) {
2707     case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2708         BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2709         drv_cmd = ECORE_Q_CMD_UPDATE;
2710         break;
2711
2712     case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2713         BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2714         drv_cmd = ECORE_Q_CMD_SETUP;
2715         break;
2716
2717     case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2718         BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2719         drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2720         break;
2721
2722     case (RAMROD_CMD_ID_ETH_HALT):
2723         BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2724         drv_cmd = ECORE_Q_CMD_HALT;
2725         break;
2726
2727     case (RAMROD_CMD_ID_ETH_TERMINATE):
2728         BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2729         drv_cmd = ECORE_Q_CMD_TERMINATE;
2730         break;
2731
2732     case (RAMROD_CMD_ID_ETH_EMPTY):
2733         BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2734         drv_cmd = ECORE_Q_CMD_EMPTY;
2735         break;
2736
2737     default:
2738         BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2739               command, fp->index);
2740         return;
2741     }
2742
2743     if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2744         q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2745         /*
2746          * q_obj->complete_cmd() failure means that this was
2747          * an unexpected completion.
2748          *
2749          * In this case we don't want to increase the sc->spq_left
2750          * because apparently we haven't sent this command the first
2751          * place.
2752          */
2753         // bxe_panic(sc, ("Unexpected SP completion\n"));
2754         return;
2755     }
2756
2757     atomic_add_acq_long(&sc->cq_spq_left, 1);
2758
2759     BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2760           atomic_load_acq_long(&sc->cq_spq_left));
2761 }
2762
2763 /*
2764  * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2765  * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2766  * the current aggregation queue as in-progress.
2767  */
2768 static void
2769 bxe_tpa_start(struct bxe_softc            *sc,
2770               struct bxe_fastpath         *fp,
2771               uint16_t                    queue,
2772               uint16_t                    cons,
2773               uint16_t                    prod,
2774               struct eth_fast_path_rx_cqe *cqe)
2775 {
2776     struct bxe_sw_rx_bd tmp_bd;
2777     struct bxe_sw_rx_bd *rx_buf;
2778     struct eth_rx_bd *rx_bd;
2779     int max_agg_queues;
2780     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2781     uint16_t index;
2782
2783     BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2784                        "cons=%d prod=%d\n",
2785           fp->index, queue, cons, prod);
2786
2787     max_agg_queues = MAX_AGG_QS(sc);
2788
2789     KASSERT((queue < max_agg_queues),
2790             ("fp[%02d] invalid aggr queue (%d >= %d)!",
2791              fp->index, queue, max_agg_queues));
2792
2793     KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2794             ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2795              fp->index, queue));
2796
2797     /* copy the existing mbuf and mapping from the TPA pool */
2798     tmp_bd = tpa_info->bd;
2799
2800     if (tmp_bd.m == NULL) {
2801         uint32_t *tmp;
2802
2803         tmp = (uint32_t *)cqe;
2804
2805         BLOGE(sc, "fp[%02d].tpa[%02d] cons[%d] prod[%d]mbuf not allocated!\n",
2806               fp->index, queue, cons, prod);
2807         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2808             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7)); 
2809             
2810         /* XXX Error handling? */
2811         return;
2812     }
2813
2814     /* change the TPA queue to the start state */
2815     tpa_info->state            = BXE_TPA_STATE_START;
2816     tpa_info->placement_offset = cqe->placement_offset;
2817     tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2818     tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2819     tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2820
2821     fp->rx_tpa_queue_used |= (1 << queue);
2822
2823     /*
2824      * If all the buffer descriptors are filled with mbufs then fill in
2825      * the current consumer index with a new BD. Else if a maximum Rx
2826      * buffer limit is imposed then fill in the next producer index.
2827      */
2828     index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2829                 prod : cons;
2830
2831     /* move the received mbuf and mapping to TPA pool */
2832     tpa_info->bd = fp->rx_mbuf_chain[cons];
2833
2834     /* release any existing RX BD mbuf mappings */
2835     if (cons != index) {
2836         rx_buf = &fp->rx_mbuf_chain[cons];
2837
2838         if (rx_buf->m_map != NULL) {
2839             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2840                             BUS_DMASYNC_POSTREAD);
2841             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2842         }
2843
2844         /*
2845          * We get here when the maximum number of rx buffers is less than
2846          * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2847          * it out here without concern of a memory leak.
2848          */
2849         fp->rx_mbuf_chain[cons].m = NULL;
2850     }
2851
2852     /* update the Rx SW BD with the mbuf info from the TPA pool */
2853     fp->rx_mbuf_chain[index] = tmp_bd;
2854
2855     /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2856     rx_bd = &fp->rx_chain[index];
2857     rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2858     rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2859 }
2860
2861 /*
2862  * When a TPA aggregation is completed, loop through the individual mbufs
2863  * of the aggregation, combining them into a single mbuf which will be sent
2864  * up the stack. Refill all freed SGEs with mbufs as we go along.
2865  */
2866 static int
2867 bxe_fill_frag_mbuf(struct bxe_softc          *sc,
2868                    struct bxe_fastpath       *fp,
2869                    struct bxe_sw_tpa_info    *tpa_info,
2870                    uint16_t                  queue,
2871                    uint16_t                  pages,
2872                    struct mbuf               *m,
2873                                struct eth_end_agg_rx_cqe *cqe,
2874                    uint16_t                  cqe_idx)
2875 {
2876     struct mbuf *m_frag;
2877     uint32_t frag_len, frag_size, i;
2878     uint16_t sge_idx;
2879     int rc = 0;
2880     int j;
2881
2882     frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
2883
2884     BLOGD(sc, DBG_LRO,
2885           "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
2886           fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
2887
2888     /* make sure the aggregated frame is not too big to handle */
2889     if (pages > 8 * PAGES_PER_SGE) {
2890
2891         uint32_t *tmp = (uint32_t *)cqe;
2892
2893         BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
2894                   "pkt_len=%d len_on_bd=%d frag_size=%d\n",
2895               fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
2896               tpa_info->len_on_bd, frag_size);
2897
2898         BLOGE(sc, "cqe [0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x]\n",
2899             *tmp, *(tmp+1), *(tmp+2), *(tmp+3), *(tmp+4), *(tmp+5), *(tmp+6), *(tmp+7)); 
2900
2901         bxe_panic(sc, ("sge page count error\n"));
2902         return (EINVAL);
2903     }
2904
2905     /*
2906      * Scan through the scatter gather list pulling individual mbufs into a
2907      * single mbuf for the host stack.
2908      */
2909     for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
2910         sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
2911
2912         /*
2913          * Firmware gives the indices of the SGE as if the ring is an array
2914          * (meaning that the "next" element will consume 2 indices).
2915          */
2916         frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
2917
2918         BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
2919                            "sge_idx=%d frag_size=%d frag_len=%d\n",
2920               fp->index, queue, i, j, sge_idx, frag_size, frag_len);
2921
2922         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
2923
2924         /* allocate a new mbuf for the SGE */
2925         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
2926         if (rc) {
2927             /* Leave all remaining SGEs in the ring! */
2928             return (rc);
2929         }
2930
2931         /* update the fragment length */
2932         m_frag->m_len = frag_len;
2933
2934         /* concatenate the fragment to the head mbuf */
2935         m_cat(m, m_frag);
2936         fp->eth_q_stats.mbuf_alloc_sge--;
2937
2938         /* update the TPA mbuf size and remaining fragment size */
2939         m->m_pkthdr.len += frag_len;
2940         frag_size -= frag_len;
2941     }
2942
2943     BLOGD(sc, DBG_LRO,
2944           "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
2945           fp->index, queue, frag_size);
2946
2947     return (rc);
2948 }
2949
2950 static inline void
2951 bxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
2952 {
2953     int i, j;
2954
2955     for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
2956         int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
2957
2958         for (j = 0; j < 2; j++) {
2959             BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
2960             idx--;
2961         }
2962     }
2963 }
2964
2965 static inline void
2966 bxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
2967 {
2968     /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
2969     memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
2970
2971     /*
2972      * Clear the two last indices in the page to 1. These are the indices that
2973      * correspond to the "next" element, hence will never be indicated and
2974      * should be removed from the calculations.
2975      */
2976     bxe_clear_sge_mask_next_elems(fp);
2977 }
2978
2979 static inline void
2980 bxe_update_last_max_sge(struct bxe_fastpath *fp,
2981                         uint16_t            idx)
2982 {
2983     uint16_t last_max = fp->last_max_sge;
2984
2985     if (SUB_S16(idx, last_max) > 0) {
2986         fp->last_max_sge = idx;
2987     }
2988 }
2989
2990 static inline void
2991 bxe_update_sge_prod(struct bxe_softc          *sc,
2992                     struct bxe_fastpath       *fp,
2993                     uint16_t                  sge_len,
2994                     union eth_sgl_or_raw_data *cqe)
2995 {
2996     uint16_t last_max, last_elem, first_elem;
2997     uint16_t delta = 0;
2998     uint16_t i;
2999
3000     if (!sge_len) {
3001         return;
3002     }
3003
3004     /* first mark all used pages */
3005     for (i = 0; i < sge_len; i++) {
3006         BIT_VEC64_CLEAR_BIT(fp->sge_mask,
3007                             RX_SGE(le16toh(cqe->sgl[i])));
3008     }
3009
3010     BLOGD(sc, DBG_LRO,
3011           "fp[%02d] fp_cqe->sgl[%d] = %d\n",
3012           fp->index, sge_len - 1,
3013           le16toh(cqe->sgl[sge_len - 1]));
3014
3015     /* assume that the last SGE index is the biggest */
3016     bxe_update_last_max_sge(fp,
3017                             le16toh(cqe->sgl[sge_len - 1]));
3018
3019     last_max = RX_SGE(fp->last_max_sge);
3020     last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
3021     first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
3022
3023     /* if ring is not full */
3024     if (last_elem + 1 != first_elem) {
3025         last_elem++;
3026     }
3027
3028     /* now update the prod */
3029     for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
3030         if (__predict_true(fp->sge_mask[i])) {
3031             break;
3032         }
3033
3034         fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
3035         delta += BIT_VEC64_ELEM_SZ;
3036     }
3037
3038     if (delta > 0) {
3039         fp->rx_sge_prod += delta;
3040         /* clear page-end entries */
3041         bxe_clear_sge_mask_next_elems(fp);
3042     }
3043
3044     BLOGD(sc, DBG_LRO,
3045           "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
3046           fp->index, fp->last_max_sge, fp->rx_sge_prod);
3047 }
3048
3049 /*
3050  * The aggregation on the current TPA queue has completed. Pull the individual
3051  * mbuf fragments together into a single mbuf, perform all necessary checksum
3052  * calculations, and send the resuting mbuf to the stack.
3053  */
3054 static void
3055 bxe_tpa_stop(struct bxe_softc          *sc,
3056              struct bxe_fastpath       *fp,
3057              struct bxe_sw_tpa_info    *tpa_info,
3058              uint16_t                  queue,
3059              uint16_t                  pages,
3060                          struct eth_end_agg_rx_cqe *cqe,
3061              uint16_t                  cqe_idx)
3062 {
3063     struct ifnet *ifp = sc->ifnet;
3064     struct mbuf *m;
3065     int rc = 0;
3066
3067     BLOGD(sc, DBG_LRO,
3068           "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3069           fp->index, queue, tpa_info->placement_offset,
3070           le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3071
3072     m = tpa_info->bd.m;
3073
3074     /* allocate a replacement before modifying existing mbuf */
3075     rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3076     if (rc) {
3077         /* drop the frame and log an error */
3078         fp->eth_q_stats.rx_soft_errors++;
3079         goto bxe_tpa_stop_exit;
3080     }
3081
3082     /* we have a replacement, fixup the current mbuf */
3083     m_adj(m, tpa_info->placement_offset);
3084     m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3085
3086     /* mark the checksums valid (taken care of by the firmware) */
3087     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3088     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3089     m->m_pkthdr.csum_data = 0xffff;
3090     m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3091                                CSUM_IP_VALID   |
3092                                CSUM_DATA_VALID |
3093                                CSUM_PSEUDO_HDR);
3094
3095     /* aggregate all of the SGEs into a single mbuf */
3096     rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3097     if (rc) {
3098         /* drop the packet and log an error */
3099         fp->eth_q_stats.rx_soft_errors++;
3100         m_freem(m);
3101     } else {
3102         if (tpa_info->parsing_flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3103             m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3104             m->m_flags |= M_VLANTAG;
3105         }
3106
3107         /* assign packet to this interface interface */
3108         m->m_pkthdr.rcvif = ifp;
3109
3110 #if __FreeBSD_version >= 800000
3111         /* specify what RSS queue was used for this flow */
3112         m->m_pkthdr.flowid = fp->index;
3113         BXE_SET_FLOWID(m);
3114 #endif
3115
3116         ifp->if_ipackets++;
3117         fp->eth_q_stats.rx_tpa_pkts++;
3118
3119         /* pass the frame to the stack */
3120         (*ifp->if_input)(ifp, m);
3121     }
3122
3123     /* we passed an mbuf up the stack or dropped the frame */
3124     fp->eth_q_stats.mbuf_alloc_tpa--;
3125
3126 bxe_tpa_stop_exit:
3127
3128     fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3129     fp->rx_tpa_queue_used &= ~(1 << queue);
3130 }
3131
3132 static uint8_t
3133 bxe_service_rxsgl(
3134                  struct bxe_fastpath *fp,
3135                  uint16_t len,
3136                  uint16_t lenonbd,
3137                  struct mbuf *m,
3138                  struct eth_fast_path_rx_cqe *cqe_fp)
3139 {
3140     struct mbuf *m_frag;
3141     uint16_t frags, frag_len;
3142     uint16_t sge_idx = 0;
3143     uint16_t j;
3144     uint8_t i, rc = 0;
3145     uint32_t frag_size;
3146
3147     /* adjust the mbuf */
3148     m->m_len = lenonbd;
3149
3150     frag_size =  len - lenonbd;
3151     frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3152
3153     for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3154         sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3155
3156         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3157         frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3158         m_frag->m_len = frag_len;
3159
3160        /* allocate a new mbuf for the SGE */
3161         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3162         if (rc) {
3163             /* Leave all remaining SGEs in the ring! */
3164             return (rc);
3165         }
3166         fp->eth_q_stats.mbuf_alloc_sge--;
3167
3168         /* concatenate the fragment to the head mbuf */
3169         m_cat(m, m_frag);
3170
3171         frag_size -= frag_len;
3172     }
3173
3174     bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3175
3176     return rc;
3177 }
3178
3179 static uint8_t
3180 bxe_rxeof(struct bxe_softc    *sc,
3181           struct bxe_fastpath *fp)
3182 {
3183     struct ifnet *ifp = sc->ifnet;
3184     uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3185     uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3186     int rx_pkts = 0;
3187     int rc = 0;
3188
3189     BXE_FP_RX_LOCK(fp);
3190
3191     /* CQ "next element" is of the size of the regular element */
3192     hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3193     if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3194         hw_cq_cons++;
3195     }
3196
3197     bd_cons = fp->rx_bd_cons;
3198     bd_prod = fp->rx_bd_prod;
3199     bd_prod_fw = bd_prod;
3200     sw_cq_cons = fp->rx_cq_cons;
3201     sw_cq_prod = fp->rx_cq_prod;
3202
3203     /*
3204      * Memory barrier necessary as speculative reads of the rx
3205      * buffer can be ahead of the index in the status block
3206      */
3207     rmb();
3208
3209     BLOGD(sc, DBG_RX,
3210           "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3211           fp->index, hw_cq_cons, sw_cq_cons);
3212
3213     while (sw_cq_cons != hw_cq_cons) {
3214         struct bxe_sw_rx_bd *rx_buf = NULL;
3215         union eth_rx_cqe *cqe;
3216         struct eth_fast_path_rx_cqe *cqe_fp;
3217         uint8_t cqe_fp_flags;
3218         enum eth_rx_cqe_type cqe_fp_type;
3219         uint16_t len, lenonbd,  pad;
3220         struct mbuf *m = NULL;
3221
3222         comp_ring_cons = RCQ(sw_cq_cons);
3223         bd_prod = RX_BD(bd_prod);
3224         bd_cons = RX_BD(bd_cons);
3225
3226         cqe          = &fp->rcq_chain[comp_ring_cons];
3227         cqe_fp       = &cqe->fast_path_cqe;
3228         cqe_fp_flags = cqe_fp->type_error_flags;
3229         cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3230
3231         BLOGD(sc, DBG_RX,
3232               "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3233               "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3234               "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3235               fp->index,
3236               hw_cq_cons,
3237               sw_cq_cons,
3238               bd_prod,
3239               bd_cons,
3240               CQE_TYPE(cqe_fp_flags),
3241               cqe_fp_flags,
3242               cqe_fp->status_flags,
3243               le32toh(cqe_fp->rss_hash_result),
3244               le16toh(cqe_fp->vlan_tag),
3245               le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3246               le16toh(cqe_fp->len_on_bd));
3247
3248         /* is this a slowpath msg? */
3249         if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3250             bxe_sp_event(sc, fp, cqe);
3251             goto next_cqe;
3252         }
3253
3254         rx_buf = &fp->rx_mbuf_chain[bd_cons];
3255
3256         if (!CQE_TYPE_FAST(cqe_fp_type)) {
3257             struct bxe_sw_tpa_info *tpa_info;
3258             uint16_t frag_size, pages;
3259             uint8_t queue;
3260
3261             if (CQE_TYPE_START(cqe_fp_type)) {
3262                 bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3263                               bd_cons, bd_prod, cqe_fp);
3264                 m = NULL; /* packet not ready yet */
3265                 goto next_rx;
3266             }
3267
3268             KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3269                     ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3270
3271             queue = cqe->end_agg_cqe.queue_index;
3272             tpa_info = &fp->rx_tpa_info[queue];
3273
3274             BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3275                   fp->index, queue);
3276
3277             frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3278                          tpa_info->len_on_bd);
3279             pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3280
3281             bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3282                          &cqe->end_agg_cqe, comp_ring_cons);
3283
3284             bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3285
3286             goto next_cqe;
3287         }
3288
3289         /* non TPA */
3290
3291         /* is this an error packet? */
3292         if (__predict_false(cqe_fp_flags &
3293                             ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3294             BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3295             fp->eth_q_stats.rx_soft_errors++;
3296             goto next_rx;
3297         }
3298
3299         len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3300         lenonbd = le16toh(cqe_fp->len_on_bd);
3301         pad = cqe_fp->placement_offset;
3302
3303         m = rx_buf->m;
3304
3305         if (__predict_false(m == NULL)) {
3306             BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3307                   bd_cons, fp->index);
3308             goto next_rx;
3309         }
3310
3311         /* XXX double copy if packet length under a threshold */
3312
3313         /*
3314          * If all the buffer descriptors are filled with mbufs then fill in
3315          * the current consumer index with a new BD. Else if a maximum Rx
3316          * buffer limit is imposed then fill in the next producer index.
3317          */
3318         rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3319                                   (sc->max_rx_bufs != RX_BD_USABLE) ?
3320                                       bd_prod : bd_cons);
3321         if (rc != 0) {
3322
3323             /* we simply reuse the received mbuf and don't post it to the stack */
3324             m = NULL;
3325
3326             BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3327                   fp->index, rc);
3328             fp->eth_q_stats.rx_soft_errors++;
3329
3330             if (sc->max_rx_bufs != RX_BD_USABLE) {
3331                 /* copy this consumer index to the producer index */
3332                 memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3333                        sizeof(struct bxe_sw_rx_bd));
3334                 memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3335             }
3336
3337             goto next_rx;
3338         }
3339
3340         /* current mbuf was detached from the bd */
3341         fp->eth_q_stats.mbuf_alloc_rx--;
3342
3343         /* we allocated a replacement mbuf, fixup the current one */
3344         m_adj(m, pad);
3345         m->m_pkthdr.len = m->m_len = len;
3346
3347         if ((len > 60) && (len > lenonbd)) {
3348             fp->eth_q_stats.rx_bxe_service_rxsgl++;
3349             rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3350             if (rc)
3351                 break;
3352             fp->eth_q_stats.rx_jumbo_sge_pkts++;
3353         } else if (lenonbd < len) {
3354             fp->eth_q_stats.rx_erroneous_jumbo_sge_pkts++;
3355         }
3356
3357         /* assign packet to this interface interface */
3358         m->m_pkthdr.rcvif = ifp;
3359
3360         /* assume no hardware checksum has complated */
3361         m->m_pkthdr.csum_flags = 0;
3362
3363         /* validate checksum if offload enabled */
3364         if (ifp->if_capenable & IFCAP_RXCSUM) {
3365             /* check for a valid IP frame */
3366             if (!(cqe->fast_path_cqe.status_flags &
3367                   ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3368                 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3369                 if (__predict_false(cqe_fp_flags &
3370                                     ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3371                     fp->eth_q_stats.rx_hw_csum_errors++;
3372                 } else {
3373                     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3374                     m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3375                 }
3376             }
3377
3378             /* check for a valid TCP/UDP frame */
3379             if (!(cqe->fast_path_cqe.status_flags &
3380                   ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3381                 if (__predict_false(cqe_fp_flags &
3382                                     ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3383                     fp->eth_q_stats.rx_hw_csum_errors++;
3384                 } else {
3385                     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3386                     m->m_pkthdr.csum_data = 0xFFFF;
3387                     m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3388                                                CSUM_PSEUDO_HDR);
3389                 }
3390             }
3391         }
3392
3393         /* if there is a VLAN tag then flag that info */
3394         if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_INNER_VLAN_EXIST) {
3395             m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3396             m->m_flags |= M_VLANTAG;
3397         }
3398
3399 #if __FreeBSD_version >= 800000
3400         /* specify what RSS queue was used for this flow */
3401         m->m_pkthdr.flowid = fp->index;
3402         BXE_SET_FLOWID(m);
3403 #endif
3404
3405 next_rx:
3406
3407         bd_cons    = RX_BD_NEXT(bd_cons);
3408         bd_prod    = RX_BD_NEXT(bd_prod);
3409         bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3410
3411         /* pass the frame to the stack */
3412         if (__predict_true(m != NULL)) {
3413             ifp->if_ipackets++;
3414             rx_pkts++;
3415             (*ifp->if_input)(ifp, m);
3416         }
3417
3418 next_cqe:
3419
3420         sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3421         sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3422
3423         /* limit spinning on the queue */
3424         if (rc != 0)
3425             break;
3426
3427         if (rx_pkts == sc->rx_budget) {
3428             fp->eth_q_stats.rx_budget_reached++;
3429             break;
3430         }
3431     } /* while work to do */
3432
3433     fp->rx_bd_cons = bd_cons;
3434     fp->rx_bd_prod = bd_prod_fw;
3435     fp->rx_cq_cons = sw_cq_cons;
3436     fp->rx_cq_prod = sw_cq_prod;
3437
3438     /* Update producers */
3439     bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3440
3441     fp->eth_q_stats.rx_pkts += rx_pkts;
3442     fp->eth_q_stats.rx_calls++;
3443
3444     BXE_FP_RX_UNLOCK(fp);
3445
3446     return (sw_cq_cons != hw_cq_cons);
3447 }
3448
3449 static uint16_t
3450 bxe_free_tx_pkt(struct bxe_softc    *sc,
3451                 struct bxe_fastpath *fp,
3452                 uint16_t            idx)
3453 {
3454     struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3455     struct eth_tx_start_bd *tx_start_bd;
3456     uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3457     uint16_t new_cons;
3458     int nbd;
3459
3460     /* unmap the mbuf from non-paged memory */
3461     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3462
3463     tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3464     nbd = le16toh(tx_start_bd->nbd) - 1;
3465
3466     new_cons = (tx_buf->first_bd + nbd);
3467
3468     /* free the mbuf */
3469     if (__predict_true(tx_buf->m != NULL)) {
3470         m_freem(tx_buf->m);
3471         fp->eth_q_stats.mbuf_alloc_tx--;
3472     } else {
3473         fp->eth_q_stats.tx_chain_lost_mbuf++;
3474     }
3475
3476     tx_buf->m = NULL;
3477     tx_buf->first_bd = 0;
3478
3479     return (new_cons);
3480 }
3481
3482 /* transmit timeout watchdog */
3483 static int
3484 bxe_watchdog(struct bxe_softc    *sc,
3485              struct bxe_fastpath *fp)
3486 {
3487     BXE_FP_TX_LOCK(fp);
3488
3489     if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3490         BXE_FP_TX_UNLOCK(fp);
3491         return (0);
3492     }
3493
3494     BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3495
3496     BXE_FP_TX_UNLOCK(fp);
3497     BXE_SET_ERROR_BIT(sc, BXE_ERR_TXQ_STUCK);
3498     taskqueue_enqueue_timeout(taskqueue_thread,
3499         &sc->sp_err_timeout_task, hz/10);
3500
3501     return (-1);
3502 }
3503
3504 /* processes transmit completions */
3505 static uint8_t
3506 bxe_txeof(struct bxe_softc    *sc,
3507           struct bxe_fastpath *fp)
3508 {
3509     struct ifnet *ifp = sc->ifnet;
3510     uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3511     uint16_t tx_bd_avail;
3512
3513     BXE_FP_TX_LOCK_ASSERT(fp);
3514
3515     bd_cons = fp->tx_bd_cons;
3516     hw_cons = le16toh(*fp->tx_cons_sb);
3517     sw_cons = fp->tx_pkt_cons;
3518
3519     while (sw_cons != hw_cons) {
3520         pkt_cons = TX_BD(sw_cons);
3521
3522         BLOGD(sc, DBG_TX,
3523               "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3524               fp->index, hw_cons, sw_cons, pkt_cons);
3525
3526         bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3527
3528         sw_cons++;
3529     }
3530
3531     fp->tx_pkt_cons = sw_cons;
3532     fp->tx_bd_cons  = bd_cons;
3533
3534     BLOGD(sc, DBG_TX,
3535           "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3536           fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3537
3538     mb();
3539
3540     tx_bd_avail = bxe_tx_avail(sc, fp);
3541
3542     if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3543         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3544     } else {
3545         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3546     }
3547
3548     if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3549         /* reset the watchdog timer if there are pending transmits */
3550         fp->watchdog_timer = BXE_TX_TIMEOUT;
3551         return (TRUE);
3552     } else {
3553         /* clear watchdog when there are no pending transmits */
3554         fp->watchdog_timer = 0;
3555         return (FALSE);
3556     }
3557 }
3558
3559 static void
3560 bxe_drain_tx_queues(struct bxe_softc *sc)
3561 {
3562     struct bxe_fastpath *fp;
3563     int i, count;
3564
3565     /* wait until all TX fastpath tasks have completed */
3566     for (i = 0; i < sc->num_queues; i++) {
3567         fp = &sc->fp[i];
3568
3569         count = 1000;
3570
3571         while (bxe_has_tx_work(fp)) {
3572
3573             BXE_FP_TX_LOCK(fp);
3574             bxe_txeof(sc, fp);
3575             BXE_FP_TX_UNLOCK(fp);
3576
3577             if (count == 0) {
3578                 BLOGE(sc, "Timeout waiting for fp[%d] "
3579                           "transmits to complete!\n", i);
3580                 bxe_panic(sc, ("tx drain failure\n"));
3581                 return;
3582             }
3583
3584             count--;
3585             DELAY(1000);
3586             rmb();
3587         }
3588     }
3589
3590     return;
3591 }
3592
3593 static int
3594 bxe_del_all_macs(struct bxe_softc          *sc,
3595                  struct ecore_vlan_mac_obj *mac_obj,
3596                  int                       mac_type,
3597                  uint8_t                   wait_for_comp)
3598 {
3599     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3600     int rc;
3601
3602     /* wait for completion of requested */
3603     if (wait_for_comp) {
3604         bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3605     }
3606
3607     /* Set the mac type of addresses we want to clear */
3608     bxe_set_bit(mac_type, &vlan_mac_flags);
3609
3610     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3611     if (rc < 0) {
3612         BLOGE(sc, "Failed to delete MACs (%d) mac_type %d wait_for_comp 0x%x\n",
3613             rc, mac_type, wait_for_comp);
3614     }
3615
3616     return (rc);
3617 }
3618
3619 static int
3620 bxe_fill_accept_flags(struct bxe_softc *sc,
3621                       uint32_t         rx_mode,
3622                       unsigned long    *rx_accept_flags,
3623                       unsigned long    *tx_accept_flags)
3624 {
3625     /* Clear the flags first */
3626     *rx_accept_flags = 0;
3627     *tx_accept_flags = 0;
3628
3629     switch (rx_mode) {
3630     case BXE_RX_MODE_NONE:
3631         /*
3632          * 'drop all' supersedes any accept flags that may have been
3633          * passed to the function.
3634          */
3635         break;
3636
3637     case BXE_RX_MODE_NORMAL:
3638         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3639         bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3640         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3641
3642         /* internal switching mode */
3643         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3644         bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3645         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3646
3647         break;
3648
3649     case BXE_RX_MODE_ALLMULTI:
3650         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3651         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3652         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3653
3654         /* internal switching mode */
3655         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3656         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3657         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3658
3659         break;
3660
3661     case BXE_RX_MODE_PROMISC:
3662         /*
3663          * According to deffinition of SI mode, iface in promisc mode
3664          * should receive matched and unmatched (in resolution of port)
3665          * unicast packets.
3666          */
3667         bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3668         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3669         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3670         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3671
3672         /* internal switching mode */
3673         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3674         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3675
3676         if (IS_MF_SI(sc)) {
3677             bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3678         } else {
3679             bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3680         }
3681
3682         break;
3683
3684     default:
3685         BLOGE(sc, "Unknown rx_mode (0x%x)\n", rx_mode);
3686         return (-1);
3687     }
3688
3689     /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3690     if (rx_mode != BXE_RX_MODE_NONE) {
3691         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3692         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3693     }
3694
3695     return (0);
3696 }
3697
3698 static int
3699 bxe_set_q_rx_mode(struct bxe_softc *sc,
3700                   uint8_t          cl_id,
3701                   unsigned long    rx_mode_flags,
3702                   unsigned long    rx_accept_flags,
3703                   unsigned long    tx_accept_flags,
3704                   unsigned long    ramrod_flags)
3705 {
3706     struct ecore_rx_mode_ramrod_params ramrod_param;
3707     int rc;
3708
3709     memset(&ramrod_param, 0, sizeof(ramrod_param));
3710
3711     /* Prepare ramrod parameters */
3712     ramrod_param.cid = 0;
3713     ramrod_param.cl_id = cl_id;
3714     ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3715     ramrod_param.func_id = SC_FUNC(sc);
3716
3717     ramrod_param.pstate = &sc->sp_state;
3718     ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3719
3720     ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3721     ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3722
3723     bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3724
3725     ramrod_param.ramrod_flags = ramrod_flags;
3726     ramrod_param.rx_mode_flags = rx_mode_flags;
3727
3728     ramrod_param.rx_accept_flags = rx_accept_flags;
3729     ramrod_param.tx_accept_flags = tx_accept_flags;
3730
3731     rc = ecore_config_rx_mode(sc, &ramrod_param);
3732     if (rc < 0) {
3733         BLOGE(sc, "Set rx_mode %d cli_id 0x%x rx_mode_flags 0x%x "
3734             "rx_accept_flags 0x%x tx_accept_flags 0x%x "
3735             "ramrod_flags 0x%x rc %d failed\n", sc->rx_mode, cl_id,
3736             (uint32_t)rx_mode_flags, (uint32_t)rx_accept_flags,
3737             (uint32_t)tx_accept_flags, (uint32_t)ramrod_flags, rc);
3738         return (rc);
3739     }
3740
3741     return (0);
3742 }
3743
3744 static int
3745 bxe_set_storm_rx_mode(struct bxe_softc *sc)
3746 {
3747     unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3748     unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3749     int rc;
3750
3751     rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3752                                &tx_accept_flags);
3753     if (rc) {
3754         return (rc);
3755     }
3756
3757     bxe_set_bit(RAMROD_RX, &ramrod_flags);
3758     bxe_set_bit(RAMROD_TX, &ramrod_flags);
3759
3760     /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3761     return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3762                               rx_accept_flags, tx_accept_flags,
3763                               ramrod_flags));
3764 }
3765
3766 /* returns the "mcp load_code" according to global load_count array */
3767 static int
3768 bxe_nic_load_no_mcp(struct bxe_softc *sc)
3769 {
3770     int path = SC_PATH(sc);
3771     int port = SC_PORT(sc);
3772
3773     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3774           path, load_count[path][0], load_count[path][1],
3775           load_count[path][2]);
3776     load_count[path][0]++;
3777     load_count[path][1 + port]++;
3778     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3779           path, load_count[path][0], load_count[path][1],
3780           load_count[path][2]);
3781     if (load_count[path][0] == 1) {
3782         return (FW_MSG_CODE_DRV_LOAD_COMMON);
3783     } else if (load_count[path][1 + port] == 1) {
3784         return (FW_MSG_CODE_DRV_LOAD_PORT);
3785     } else {
3786         return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3787     }
3788 }
3789
3790 /* returns the "mcp load_code" according to global load_count array */
3791 static int
3792 bxe_nic_unload_no_mcp(struct bxe_softc *sc)
3793 {
3794     int port = SC_PORT(sc);
3795     int path = SC_PATH(sc);
3796
3797     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3798           path, load_count[path][0], load_count[path][1],
3799           load_count[path][2]);
3800     load_count[path][0]--;
3801     load_count[path][1 + port]--;
3802     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3803           path, load_count[path][0], load_count[path][1],
3804           load_count[path][2]);
3805     if (load_count[path][0] == 0) {
3806         return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3807     } else if (load_count[path][1 + port] == 0) {
3808         return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3809     } else {
3810         return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3811     }
3812 }
3813
3814 /* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3815 static uint32_t
3816 bxe_send_unload_req(struct bxe_softc *sc,
3817                     int              unload_mode)
3818 {
3819     uint32_t reset_code = 0;
3820
3821     /* Select the UNLOAD request mode */
3822     if (unload_mode == UNLOAD_NORMAL) {
3823         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3824     } else {
3825         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3826     }
3827
3828     /* Send the request to the MCP */
3829     if (!BXE_NOMCP(sc)) {
3830         reset_code = bxe_fw_command(sc, reset_code, 0);
3831     } else {
3832         reset_code = bxe_nic_unload_no_mcp(sc);
3833     }
3834
3835     return (reset_code);
3836 }
3837
3838 /* send UNLOAD_DONE command to the MCP */
3839 static void
3840 bxe_send_unload_done(struct bxe_softc *sc,
3841                      uint8_t          keep_link)
3842 {
3843     uint32_t reset_param =
3844         keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
3845
3846     /* Report UNLOAD_DONE to MCP */
3847     if (!BXE_NOMCP(sc)) {
3848         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
3849     }
3850 }
3851
3852 static int
3853 bxe_func_wait_started(struct bxe_softc *sc)
3854 {
3855     int tout = 50;
3856
3857     if (!sc->port.pmf) {
3858         return (0);
3859     }
3860
3861     /*
3862      * (assumption: No Attention from MCP at this stage)
3863      * PMF probably in the middle of TX disable/enable transaction
3864      * 1. Sync IRS for default SB
3865      * 2. Sync SP queue - this guarantees us that attention handling started
3866      * 3. Wait, that TX disable/enable transaction completes
3867      *
3868      * 1+2 guarantee that if DCBX attention was scheduled it already changed
3869      * pending bit of transaction from STARTED-->TX_STOPPED, if we already
3870      * received completion for the transaction the state is TX_STOPPED.
3871      * State will return to STARTED after completion of TX_STOPPED-->STARTED
3872      * transaction.
3873      */
3874
3875     /* XXX make sure default SB ISR is done */
3876     /* need a way to synchronize an irq (intr_mtx?) */
3877
3878     /* XXX flush any work queues */
3879
3880     while (ecore_func_get_state(sc, &sc->func_obj) !=
3881            ECORE_F_STATE_STARTED && tout--) {
3882         DELAY(20000);
3883     }
3884
3885     if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
3886         /*
3887          * Failed to complete the transaction in a "good way"
3888          * Force both transactions with CLR bit.
3889          */
3890         struct ecore_func_state_params func_params = { NULL };
3891
3892         BLOGE(sc, "Unexpected function state! "
3893                   "Forcing STARTED-->TX_STOPPED-->STARTED\n");
3894
3895         func_params.f_obj = &sc->func_obj;
3896         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
3897
3898         /* STARTED-->TX_STOPPED */
3899         func_params.cmd = ECORE_F_CMD_TX_STOP;
3900         ecore_func_state_change(sc, &func_params);
3901
3902         /* TX_STOPPED-->STARTED */
3903         func_params.cmd = ECORE_F_CMD_TX_START;
3904         return (ecore_func_state_change(sc, &func_params));
3905     }
3906
3907     return (0);
3908 }
3909
3910 static int
3911 bxe_stop_queue(struct bxe_softc *sc,
3912                int              index)
3913 {
3914     struct bxe_fastpath *fp = &sc->fp[index];
3915     struct ecore_queue_state_params q_params = { NULL };
3916     int rc;
3917
3918     BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
3919
3920     q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
3921     /* We want to wait for completion in this context */
3922     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
3923
3924     /* Stop the primary connection: */
3925
3926     /* ...halt the connection */
3927     q_params.cmd = ECORE_Q_CMD_HALT;
3928     rc = ecore_queue_state_change(sc, &q_params);
3929     if (rc) {
3930         return (rc);
3931     }
3932
3933     /* ...terminate the connection */
3934     q_params.cmd = ECORE_Q_CMD_TERMINATE;
3935     memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
3936     q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
3937     rc = ecore_queue_state_change(sc, &q_params);
3938     if (rc) {
3939         return (rc);
3940     }
3941
3942     /* ...delete cfc entry */
3943     q_params.cmd = ECORE_Q_CMD_CFC_DEL;
3944     memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
3945     q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
3946     return (ecore_queue_state_change(sc, &q_params));
3947 }
3948
3949 /* wait for the outstanding SP commands */
3950 static inline uint8_t
3951 bxe_wait_sp_comp(struct bxe_softc *sc,
3952                  unsigned long    mask)
3953 {
3954     unsigned long tmp;
3955     int tout = 5000; /* wait for 5 secs tops */
3956
3957     while (tout--) {
3958         mb();
3959         if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
3960             return (TRUE);
3961         }
3962
3963         DELAY(1000);
3964     }
3965
3966     mb();
3967
3968     tmp = atomic_load_acq_long(&sc->sp_state);
3969     if (tmp & mask) {
3970         BLOGE(sc, "Filtering completion timed out: "
3971                   "sp_state 0x%lx, mask 0x%lx\n",
3972               tmp, mask);
3973         return (FALSE);
3974     }
3975
3976     return (FALSE);
3977 }
3978
3979 static int
3980 bxe_func_stop(struct bxe_softc *sc)
3981 {
3982     struct ecore_func_state_params func_params = { NULL };
3983     int rc;
3984
3985     /* prepare parameters for function state transitions */
3986     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
3987     func_params.f_obj = &sc->func_obj;
3988     func_params.cmd = ECORE_F_CMD_STOP;
3989
3990     /*
3991      * Try to stop the function the 'good way'. If it fails (in case
3992      * of a parity error during bxe_chip_cleanup()) and we are
3993      * not in a debug mode, perform a state transaction in order to
3994      * enable further HW_RESET transaction.
3995      */
3996     rc = ecore_func_state_change(sc, &func_params);
3997     if (rc) {
3998         BLOGE(sc, "FUNC_STOP ramrod failed. "
3999                   "Running a dry transaction (%d)\n", rc);
4000         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
4001         return (ecore_func_state_change(sc, &func_params));
4002     }
4003
4004     return (0);
4005 }
4006
4007 static int
4008 bxe_reset_hw(struct bxe_softc *sc,
4009              uint32_t         load_code)
4010 {
4011     struct ecore_func_state_params func_params = { NULL };
4012
4013     /* Prepare parameters for function state transitions */
4014     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
4015
4016     func_params.f_obj = &sc->func_obj;
4017     func_params.cmd = ECORE_F_CMD_HW_RESET;
4018
4019     func_params.params.hw_init.load_phase = load_code;
4020
4021     return (ecore_func_state_change(sc, &func_params));
4022 }
4023
4024 static void
4025 bxe_int_disable_sync(struct bxe_softc *sc,
4026                      int              disable_hw)
4027 {
4028     if (disable_hw) {
4029         /* prevent the HW from sending interrupts */
4030         bxe_int_disable(sc);
4031     }
4032
4033     /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
4034     /* make sure all ISRs are done */
4035
4036     /* XXX make sure sp_task is not running */
4037     /* cancel and flush work queues */
4038 }
4039
4040 static void
4041 bxe_chip_cleanup(struct bxe_softc *sc,
4042                  uint32_t         unload_mode,
4043                  uint8_t          keep_link)
4044 {
4045     int port = SC_PORT(sc);
4046     struct ecore_mcast_ramrod_params rparam = { NULL };
4047     uint32_t reset_code;
4048     int i, rc = 0;
4049
4050     bxe_drain_tx_queues(sc);
4051
4052     /* give HW time to discard old tx messages */
4053     DELAY(1000);
4054
4055     /* Clean all ETH MACs */
4056     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4057     if (rc < 0) {
4058         BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4059     }
4060
4061     /* Clean up UC list  */
4062     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4063     if (rc < 0) {
4064         BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4065     }
4066
4067     /* Disable LLH */
4068     if (!CHIP_IS_E1(sc)) {
4069         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4070     }
4071
4072     /* Set "drop all" to stop Rx */
4073
4074     /*
4075      * We need to take the BXE_MCAST_LOCK() here in order to prevent
4076      * a race between the completion code and this code.
4077      */
4078     BXE_MCAST_LOCK(sc);
4079
4080     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4081         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4082     } else {
4083         bxe_set_storm_rx_mode(sc);
4084     }
4085
4086     /* Clean up multicast configuration */
4087     rparam.mcast_obj = &sc->mcast_obj;
4088     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4089     if (rc < 0) {
4090         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4091     }
4092
4093     BXE_MCAST_UNLOCK(sc);
4094
4095     // XXX bxe_iov_chip_cleanup(sc);
4096
4097     /*
4098      * Send the UNLOAD_REQUEST to the MCP. This will return if
4099      * this function should perform FUNCTION, PORT, or COMMON HW
4100      * reset.
4101      */
4102     reset_code = bxe_send_unload_req(sc, unload_mode);
4103
4104     /*
4105      * (assumption: No Attention from MCP at this stage)
4106      * PMF probably in the middle of TX disable/enable transaction
4107      */
4108     rc = bxe_func_wait_started(sc);
4109     if (rc) {
4110         BLOGE(sc, "bxe_func_wait_started failed (%d)\n", rc);
4111     }
4112
4113     /*
4114      * Close multi and leading connections
4115      * Completions for ramrods are collected in a synchronous way
4116      */
4117     for (i = 0; i < sc->num_queues; i++) {
4118         if (bxe_stop_queue(sc, i)) {
4119             goto unload_error;
4120         }
4121     }
4122
4123     /*
4124      * If SP settings didn't get completed so far - something
4125      * very wrong has happen.
4126      */
4127     if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4128         BLOGE(sc, "Common slow path ramrods got stuck!(%d)\n", rc);
4129     }
4130
4131 unload_error:
4132
4133     rc = bxe_func_stop(sc);
4134     if (rc) {
4135         BLOGE(sc, "Function stop failed!(%d)\n", rc);
4136     }
4137
4138     /* disable HW interrupts */
4139     bxe_int_disable_sync(sc, TRUE);
4140
4141     /* detach interrupts */
4142     bxe_interrupt_detach(sc);
4143
4144     /* Reset the chip */
4145     rc = bxe_reset_hw(sc, reset_code);
4146     if (rc) {
4147         BLOGE(sc, "Hardware reset failed(%d)\n", rc);
4148     }
4149
4150     /* Report UNLOAD_DONE to MCP */
4151     bxe_send_unload_done(sc, keep_link);
4152 }
4153
4154 static void
4155 bxe_disable_close_the_gate(struct bxe_softc *sc)
4156 {
4157     uint32_t val;
4158     int port = SC_PORT(sc);
4159
4160     BLOGD(sc, DBG_LOAD,
4161           "Disabling 'close the gates'\n");
4162
4163     if (CHIP_IS_E1(sc)) {
4164         uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4165                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
4166         val = REG_RD(sc, addr);
4167         val &= ~(0x300);
4168         REG_WR(sc, addr, val);
4169     } else {
4170         val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4171         val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4172                  MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4173         REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4174     }
4175 }
4176
4177 /*
4178  * Cleans the object that have internal lists without sending
4179  * ramrods. Should be run when interrutps are disabled.
4180  */
4181 static void
4182 bxe_squeeze_objects(struct bxe_softc *sc)
4183 {
4184     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4185     struct ecore_mcast_ramrod_params rparam = { NULL };
4186     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4187     int rc;
4188
4189     /* Cleanup MACs' object first... */
4190
4191     /* Wait for completion of requested */
4192     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4193     /* Perform a dry cleanup */
4194     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4195
4196     /* Clean ETH primary MAC */
4197     bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4198     rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4199                              &ramrod_flags);
4200     if (rc != 0) {
4201         BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4202     }
4203
4204     /* Cleanup UC list */
4205     vlan_mac_flags = 0;
4206     bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4207     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4208                              &ramrod_flags);
4209     if (rc != 0) {
4210         BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4211     }
4212
4213     /* Now clean mcast object... */
4214
4215     rparam.mcast_obj = &sc->mcast_obj;
4216     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4217
4218     /* Add a DEL command... */
4219     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4220     if (rc < 0) {
4221         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4222     }
4223
4224     /* now wait until all pending commands are cleared */
4225
4226     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4227     while (rc != 0) {
4228         if (rc < 0) {
4229             BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4230             return;
4231         }
4232
4233         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4234     }
4235 }
4236
4237 /* stop the controller */
4238 static __noinline int
4239 bxe_nic_unload(struct bxe_softc *sc,
4240                uint32_t         unload_mode,
4241                uint8_t          keep_link)
4242 {
4243     uint8_t global = FALSE;
4244     uint32_t val;
4245     int i;
4246
4247     BXE_CORE_LOCK_ASSERT(sc);
4248
4249     sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
4250
4251     for (i = 0; i < sc->num_queues; i++) {
4252         struct bxe_fastpath *fp;
4253
4254         fp = &sc->fp[i];
4255         fp->watchdog_timer = 0;
4256         BXE_FP_TX_LOCK(fp);
4257         BXE_FP_TX_UNLOCK(fp);
4258     }
4259
4260     BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4261
4262     /* mark driver as unloaded in shmem2 */
4263     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4264         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4265         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4266                   val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4267     }
4268
4269     if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4270         (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4271
4272         if(CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
4273             /*
4274              * We can get here if the driver has been unloaded
4275              * during parity error recovery and is either waiting for a
4276              * leader to complete or for other functions to unload and
4277              * then ifconfig down has been issued. In this case we want to
4278              * unload and let other functions to complete a recovery
4279              * process.
4280              */
4281             sc->recovery_state = BXE_RECOVERY_DONE;
4282             sc->is_leader = 0;
4283             bxe_release_leader_lock(sc);
4284             mb();
4285             BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4286         }
4287         BLOGE(sc, "Can't unload in closed or error state recover_state 0x%x"
4288             " state = 0x%x\n", sc->recovery_state, sc->state);
4289         return (-1);
4290     }
4291
4292     /*
4293      * Nothing to do during unload if previous bxe_nic_load()
4294      * did not completed succesfully - all resourses are released.
4295      */
4296     if ((sc->state == BXE_STATE_CLOSED) ||
4297         (sc->state == BXE_STATE_ERROR)) {
4298         return (0);
4299     }
4300
4301     sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4302     mb();
4303
4304     /* stop tx */
4305     bxe_tx_disable(sc);
4306
4307     sc->rx_mode = BXE_RX_MODE_NONE;
4308     /* XXX set rx mode ??? */
4309
4310     if (IS_PF(sc) && !sc->grcdump_done) {
4311         /* set ALWAYS_ALIVE bit in shmem */
4312         sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4313
4314         bxe_drv_pulse(sc);
4315
4316         bxe_stats_handle(sc, STATS_EVENT_STOP);
4317         bxe_save_statistics(sc);
4318     }
4319
4320     /* wait till consumers catch up with producers in all queues */
4321     bxe_drain_tx_queues(sc);
4322
4323     /* if VF indicate to PF this function is going down (PF will delete sp
4324      * elements and clear initializations
4325      */
4326     if (IS_VF(sc)) {
4327         ; /* bxe_vfpf_close_vf(sc); */
4328     } else if (unload_mode != UNLOAD_RECOVERY) {
4329         /* if this is a normal/close unload need to clean up chip */
4330         if (!sc->grcdump_done)
4331             bxe_chip_cleanup(sc, unload_mode, keep_link);
4332     } else {
4333         /* Send the UNLOAD_REQUEST to the MCP */
4334         bxe_send_unload_req(sc, unload_mode);
4335
4336         /*
4337          * Prevent transactions to host from the functions on the
4338          * engine that doesn't reset global blocks in case of global
4339          * attention once gloabl blocks are reset and gates are opened
4340          * (the engine which leader will perform the recovery
4341          * last).
4342          */
4343         if (!CHIP_IS_E1x(sc)) {
4344             bxe_pf_disable(sc);
4345         }
4346
4347         /* disable HW interrupts */
4348         bxe_int_disable_sync(sc, TRUE);
4349
4350         /* detach interrupts */
4351         bxe_interrupt_detach(sc);
4352
4353         /* Report UNLOAD_DONE to MCP */
4354         bxe_send_unload_done(sc, FALSE);
4355     }
4356
4357     /*
4358      * At this stage no more interrupts will arrive so we may safely clean
4359      * the queue'able objects here in case they failed to get cleaned so far.
4360      */
4361     if (IS_PF(sc)) {
4362         bxe_squeeze_objects(sc);
4363     }
4364
4365     /* There should be no more pending SP commands at this stage */
4366     sc->sp_state = 0;
4367
4368     sc->port.pmf = 0;
4369
4370     bxe_free_fp_buffers(sc);
4371
4372     if (IS_PF(sc)) {
4373         bxe_free_mem(sc);
4374     }
4375
4376     bxe_free_fw_stats_mem(sc);
4377
4378     sc->state = BXE_STATE_CLOSED;
4379
4380     /*
4381      * Check if there are pending parity attentions. If there are - set
4382      * RECOVERY_IN_PROGRESS.
4383      */
4384     if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4385         bxe_set_reset_in_progress(sc);
4386
4387         /* Set RESET_IS_GLOBAL if needed */
4388         if (global) {
4389             bxe_set_reset_global(sc);
4390         }
4391     }
4392
4393     /*
4394      * The last driver must disable a "close the gate" if there is no
4395      * parity attention or "process kill" pending.
4396      */
4397     if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4398         bxe_reset_is_done(sc, SC_PATH(sc))) {
4399         bxe_disable_close_the_gate(sc);
4400     }
4401
4402     BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4403
4404     bxe_link_report(sc);
4405
4406     return (0);
4407 }
4408
4409 /*
4410  * Called by the OS to set various media options (i.e. link, speed, etc.) when
4411  * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4412  */
4413 static int
4414 bxe_ifmedia_update(struct ifnet *ifp)
4415 {
4416     struct bxe_softc *sc = (struct bxe_softc *)ifp->if_softc;
4417     struct ifmedia *ifm;
4418
4419     ifm = &sc->ifmedia;
4420
4421     /* We only support Ethernet media type. */
4422     if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4423         return (EINVAL);
4424     }
4425
4426     switch (IFM_SUBTYPE(ifm->ifm_media)) {
4427     case IFM_AUTO:
4428          break;
4429     case IFM_10G_CX4:
4430     case IFM_10G_SR:
4431     case IFM_10G_T:
4432     case IFM_10G_TWINAX:
4433     default:
4434         /* We don't support changing the media type. */
4435         BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4436               IFM_SUBTYPE(ifm->ifm_media));
4437         return (EINVAL);
4438     }
4439
4440     return (0);
4441 }
4442
4443 /*
4444  * Called by the OS to get the current media status (i.e. link, speed, etc.).
4445  */
4446 static void
4447 bxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4448 {
4449     struct bxe_softc *sc = ifp->if_softc;
4450
4451     /* Bug 165447: the 'ifconfig' tool skips printing of the "status: ..."
4452        line if the IFM_AVALID flag is *NOT* set. So we need to set this
4453        flag unconditionally (irrespective of the admininistrative
4454        'up/down' state of the interface) to ensure that that line is always
4455        displayed.
4456     */
4457     ifmr->ifm_status = IFM_AVALID;
4458
4459     /* Setup the default interface info. */
4460     ifmr->ifm_active = IFM_ETHER;
4461
4462     /* Report link down if the driver isn't running. */
4463     if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
4464         ifmr->ifm_active |= IFM_NONE;
4465         BLOGD(sc, DBG_PHY, "in %s : nic still not loaded fully\n", __func__);
4466         BLOGD(sc, DBG_PHY, "in %s : link_up (1) : %d\n",
4467                 __func__, sc->link_vars.link_up);
4468         return;
4469     }
4470
4471
4472     if (sc->link_vars.link_up) {
4473         ifmr->ifm_status |= IFM_ACTIVE;
4474         ifmr->ifm_active |= IFM_FDX;
4475     } else {
4476         ifmr->ifm_active |= IFM_NONE;
4477         BLOGD(sc, DBG_PHY, "in %s : setting IFM_NONE\n",
4478                 __func__);
4479         return;
4480     }
4481
4482     ifmr->ifm_active |= sc->media;
4483     return;
4484 }
4485
4486 static void
4487 bxe_handle_chip_tq(void *context,
4488                    int  pending)
4489 {
4490     struct bxe_softc *sc = (struct bxe_softc *)context;
4491     long work = atomic_load_acq_long(&sc->chip_tq_flags);
4492
4493     switch (work)
4494     {
4495     case CHIP_TQ_REINIT:
4496         if (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) {
4497             /* restart the interface */
4498             BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4499             bxe_periodic_stop(sc);
4500             BXE_CORE_LOCK(sc);
4501             bxe_stop_locked(sc);
4502             bxe_init_locked(sc);
4503             BXE_CORE_UNLOCK(sc);
4504         }
4505         break;
4506
4507     default:
4508         break;
4509     }
4510 }
4511
4512 /*
4513  * Handles any IOCTL calls from the operating system.
4514  *
4515  * Returns:
4516  *   0 = Success, >0 Failure
4517  */
4518 static int
4519 bxe_ioctl(struct ifnet *ifp,
4520           u_long       command,
4521           caddr_t      data)
4522 {
4523     struct bxe_softc *sc = ifp->if_softc;
4524     struct ifreq *ifr = (struct ifreq *)data;
4525     int mask = 0;
4526     int reinit = 0;
4527     int error = 0;
4528
4529     int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4530     int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4531
4532     switch (command)
4533     {
4534     case SIOCSIFMTU:
4535         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4536               ifr->ifr_mtu);
4537
4538         if (sc->mtu == ifr->ifr_mtu) {
4539             /* nothing to change */
4540             break;
4541         }
4542
4543         if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4544             BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4545                   ifr->ifr_mtu, mtu_min, mtu_max);
4546             error = EINVAL;
4547             break;
4548         }
4549
4550         atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4551                              (unsigned long)ifr->ifr_mtu);
4552         atomic_store_rel_long((volatile unsigned long *)&ifp->if_mtu,
4553                               (unsigned long)ifr->ifr_mtu);
4554
4555         reinit = 1;
4556         break;
4557
4558     case SIOCSIFFLAGS:
4559         /* toggle the interface state up or down */
4560         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4561
4562         BXE_CORE_LOCK(sc);
4563         /* check if the interface is up */
4564         if (ifp->if_flags & IFF_UP) {
4565             if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4566                 /* set the receive mode flags */
4567                 bxe_set_rx_mode(sc);
4568             } else if(sc->state != BXE_STATE_DISABLED) {
4569                 bxe_init_locked(sc);
4570             }
4571         } else {
4572             if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4573                 bxe_periodic_stop(sc);
4574                 bxe_stop_locked(sc);
4575             }
4576         }
4577         BXE_CORE_UNLOCK(sc);
4578
4579         break;
4580
4581     case SIOCADDMULTI:
4582     case SIOCDELMULTI:
4583         /* add/delete multicast addresses */
4584         BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4585
4586         /* check if the interface is up */
4587         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4588             /* set the receive mode flags */
4589             BXE_CORE_LOCK(sc);
4590             bxe_set_rx_mode(sc);
4591             BXE_CORE_UNLOCK(sc); 
4592         }
4593
4594         break;
4595
4596     case SIOCSIFCAP:
4597         /* find out which capabilities have changed */
4598         mask = (ifr->ifr_reqcap ^ ifp->if_capenable);
4599
4600         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4601               mask);
4602
4603         /* toggle the LRO capabilites enable flag */
4604         if (mask & IFCAP_LRO) {
4605             ifp->if_capenable ^= IFCAP_LRO;
4606             BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4607                   (ifp->if_capenable & IFCAP_LRO) ? "ON" : "OFF");
4608             reinit = 1;
4609         }
4610
4611         /* toggle the TXCSUM checksum capabilites enable flag */
4612         if (mask & IFCAP_TXCSUM) {
4613             ifp->if_capenable ^= IFCAP_TXCSUM;
4614             BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4615                   (ifp->if_capenable & IFCAP_TXCSUM) ? "ON" : "OFF");
4616             if (ifp->if_capenable & IFCAP_TXCSUM) {
4617                 ifp->if_hwassist = (CSUM_IP       |
4618                                     CSUM_TCP      |
4619                                     CSUM_UDP      |
4620                                     CSUM_TSO      |
4621                                     CSUM_TCP_IPV6 |
4622                                     CSUM_UDP_IPV6);
4623             } else {
4624                 ifp->if_hwassist = 0;
4625             }
4626         }
4627
4628         /* toggle the RXCSUM checksum capabilities enable flag */
4629         if (mask & IFCAP_RXCSUM) {
4630             ifp->if_capenable ^= IFCAP_RXCSUM;
4631             BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4632                   (ifp->if_capenable & IFCAP_RXCSUM) ? "ON" : "OFF");
4633             if (ifp->if_capenable & IFCAP_RXCSUM) {
4634                 ifp->if_hwassist = (CSUM_IP       |
4635                                     CSUM_TCP      |
4636                                     CSUM_UDP      |
4637                                     CSUM_TSO      |
4638                                     CSUM_TCP_IPV6 |
4639                                     CSUM_UDP_IPV6);
4640             } else {
4641                 ifp->if_hwassist = 0;
4642             }
4643         }
4644
4645         /* toggle TSO4 capabilities enabled flag */
4646         if (mask & IFCAP_TSO4) {
4647             ifp->if_capenable ^= IFCAP_TSO4;
4648             BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4649                   (ifp->if_capenable & IFCAP_TSO4) ? "ON" : "OFF");
4650         }
4651
4652         /* toggle TSO6 capabilities enabled flag */
4653         if (mask & IFCAP_TSO6) {
4654             ifp->if_capenable ^= IFCAP_TSO6;
4655             BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4656                   (ifp->if_capenable & IFCAP_TSO6) ? "ON" : "OFF");
4657         }
4658
4659         /* toggle VLAN_HWTSO capabilities enabled flag */
4660         if (mask & IFCAP_VLAN_HWTSO) {
4661             ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
4662             BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4663                   (ifp->if_capenable & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4664         }
4665
4666         /* toggle VLAN_HWCSUM capabilities enabled flag */
4667         if (mask & IFCAP_VLAN_HWCSUM) {
4668             /* XXX investigate this... */
4669             BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4670             error = EINVAL;
4671         }
4672
4673         /* toggle VLAN_MTU capabilities enable flag */
4674         if (mask & IFCAP_VLAN_MTU) {
4675             /* XXX investigate this... */
4676             BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4677             error = EINVAL;
4678         }
4679
4680         /* toggle VLAN_HWTAGGING capabilities enabled flag */
4681         if (mask & IFCAP_VLAN_HWTAGGING) {
4682             /* XXX investigate this... */
4683             BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4684             error = EINVAL;
4685         }
4686
4687         /* toggle VLAN_HWFILTER capabilities enabled flag */
4688         if (mask & IFCAP_VLAN_HWFILTER) {
4689             /* XXX investigate this... */
4690             BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4691             error = EINVAL;
4692         }
4693
4694         /* XXX not yet...
4695          * IFCAP_WOL_MAGIC
4696          */
4697
4698         break;
4699
4700     case SIOCSIFMEDIA:
4701     case SIOCGIFMEDIA:
4702         /* set/get interface media */
4703         BLOGD(sc, DBG_IOCTL,
4704               "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4705               (command & 0xff));
4706         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
4707         break;
4708
4709     default:
4710         BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
4711               (command & 0xff));
4712         error = ether_ioctl(ifp, command, data);
4713         break;
4714     }
4715
4716     if (reinit && (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING)) {
4717         BLOGD(sc, DBG_LOAD | DBG_IOCTL,
4718               "Re-initializing hardware from IOCTL change\n");
4719         bxe_periodic_stop(sc);
4720         BXE_CORE_LOCK(sc);
4721         bxe_stop_locked(sc);
4722         bxe_init_locked(sc);
4723         BXE_CORE_UNLOCK(sc);
4724     }
4725
4726     return (error);
4727 }
4728
4729 static __noinline void
4730 bxe_dump_mbuf(struct bxe_softc *sc,
4731               struct mbuf      *m,
4732               uint8_t          contents)
4733 {
4734     char * type;
4735     int i = 0;
4736
4737     if (!(sc->debug & DBG_MBUF)) {
4738         return;
4739     }
4740
4741     if (m == NULL) {
4742         BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
4743         return;
4744     }
4745
4746     while (m) {
4747
4748 #if __FreeBSD_version >= 1000000
4749         BLOGD(sc, DBG_MBUF,
4750               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4751               i, m, m->m_len, m->m_flags,
4752               "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
4753
4754         if (m->m_flags & M_PKTHDR) {
4755              BLOGD(sc, DBG_MBUF,
4756                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4757                    i, m->m_pkthdr.len, m->m_flags,
4758                    "\20\12M_BCAST\13M_MCAST\14M_FRAG"
4759                    "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
4760                    "\22M_PROMISC\23M_NOFREE",
4761                    (int)m->m_pkthdr.csum_flags,
4762                    "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
4763                    "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
4764                    "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
4765                    "\14CSUM_PSEUDO_HDR");
4766         }
4767 #else
4768         BLOGD(sc, DBG_MBUF,
4769               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
4770               i, m, m->m_len, m->m_flags,
4771               "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
4772
4773         if (m->m_flags & M_PKTHDR) {
4774              BLOGD(sc, DBG_MBUF,
4775                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
4776                    i, m->m_pkthdr.len, m->m_flags,
4777                    "\20\12M_BCAST\13M_MCAST\14M_FRAG"
4778                    "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
4779                    "\22M_PROMISC\23M_NOFREE",
4780                    (int)m->m_pkthdr.csum_flags,
4781                    "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
4782                    "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
4783                    "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
4784                    "\14CSUM_PSEUDO_HDR");
4785         }
4786 #endif /* #if __FreeBSD_version >= 1000000 */
4787
4788         if (m->m_flags & M_EXT) {
4789             switch (m->m_ext.ext_type) {
4790             case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
4791             case EXT_SFBUF:      type = "EXT_SFBUF";      break;
4792             case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
4793             case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
4794             case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
4795             case EXT_PACKET:     type = "EXT_PACKET";     break;
4796             case EXT_MBUF:       type = "EXT_MBUF";       break;
4797             case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
4798             case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
4799             case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
4800             case EXT_EXTREF:     type = "EXT_EXTREF";     break;
4801             default:             type = "UNKNOWN";        break;
4802             }
4803
4804             BLOGD(sc, DBG_MBUF,
4805                   "%02d: - m_ext: %p ext_size=%d type=%s\n",
4806                   i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
4807         }
4808
4809         if (contents) {
4810             bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
4811         }
4812
4813         m = m->m_next;
4814         i++;
4815     }
4816 }
4817
4818 /*
4819  * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
4820  * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
4821  * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
4822  * The headers comes in a seperate bd in FreeBSD so 13-3=10.
4823  * Returns: 0 if OK to send, 1 if packet needs further defragmentation
4824  */
4825 static int
4826 bxe_chktso_window(struct bxe_softc  *sc,
4827                   int               nsegs,
4828                   bus_dma_segment_t *segs,
4829                   struct mbuf       *m)
4830 {
4831     uint32_t num_wnds, wnd_size, wnd_sum;
4832     int32_t frag_idx, wnd_idx;
4833     unsigned short lso_mss;
4834     int defrag;
4835
4836     defrag = 0;
4837     wnd_sum = 0;
4838     wnd_size = 10;
4839     num_wnds = nsegs - wnd_size;
4840     lso_mss = htole16(m->m_pkthdr.tso_segsz);
4841
4842     /*
4843      * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
4844      * first window sum of data while skipping the first assuming it is the
4845      * header in FreeBSD.
4846      */
4847     for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
4848         wnd_sum += htole16(segs[frag_idx].ds_len);
4849     }
4850
4851     /* check the first 10 bd window size */
4852     if (wnd_sum < lso_mss) {
4853         return (1);
4854     }
4855
4856     /* run through the windows */
4857     for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
4858         /* subtract the first mbuf->m_len of the last wndw(-header) */
4859         wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
4860         /* add the next mbuf len to the len of our new window */
4861         wnd_sum += htole16(segs[frag_idx].ds_len);
4862         if (wnd_sum < lso_mss) {
4863             return (1);
4864         }
4865     }
4866
4867     return (0);
4868 }
4869
4870 static uint8_t
4871 bxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
4872                     struct mbuf         *m,
4873                     uint32_t            *parsing_data)
4874 {
4875     struct ether_vlan_header *eh = NULL;
4876     struct ip *ip4 = NULL;
4877     struct ip6_hdr *ip6 = NULL;
4878     caddr_t ip = NULL;
4879     struct tcphdr *th = NULL;
4880     int e_hlen, ip_hlen, l4_off;
4881     uint16_t proto;
4882
4883     if (m->m_pkthdr.csum_flags == CSUM_IP) {
4884         /* no L4 checksum offload needed */
4885         return (0);
4886     }
4887
4888     /* get the Ethernet header */
4889     eh = mtod(m, struct ether_vlan_header *);
4890
4891     /* handle VLAN encapsulation if present */
4892     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4893         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4894         proto  = ntohs(eh->evl_proto);
4895     } else {
4896         e_hlen = ETHER_HDR_LEN;
4897         proto  = ntohs(eh->evl_encap_proto);
4898     }
4899
4900     switch (proto) {
4901     case ETHERTYPE_IP:
4902         /* get the IP header, if mbuf len < 20 then header in next mbuf */
4903         ip4 = (m->m_len < sizeof(struct ip)) ?
4904                   (struct ip *)m->m_next->m_data :
4905                   (struct ip *)(m->m_data + e_hlen);
4906         /* ip_hl is number of 32-bit words */
4907         ip_hlen = (ip4->ip_hl << 2);
4908         ip = (caddr_t)ip4;
4909         break;
4910     case ETHERTYPE_IPV6:
4911         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4912         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4913                   (struct ip6_hdr *)m->m_next->m_data :
4914                   (struct ip6_hdr *)(m->m_data + e_hlen);
4915         /* XXX cannot support offload with IPv6 extensions */
4916         ip_hlen = sizeof(struct ip6_hdr);
4917         ip = (caddr_t)ip6;
4918         break;
4919     default:
4920         /* We can't offload in this case... */
4921         /* XXX error stat ??? */
4922         return (0);
4923     }
4924
4925     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
4926     l4_off = (e_hlen + ip_hlen);
4927
4928     *parsing_data |=
4929         (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
4930          ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
4931
4932     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
4933                                   CSUM_TSO |
4934                                   CSUM_TCP_IPV6)) {
4935         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
4936         th = (struct tcphdr *)(ip + ip_hlen);
4937         /* th_off is number of 32-bit words */
4938         *parsing_data |= ((th->th_off <<
4939                            ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
4940                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
4941         return (l4_off + (th->th_off << 2)); /* entire header length */
4942     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
4943                                          CSUM_UDP_IPV6)) {
4944         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
4945         return (l4_off + sizeof(struct udphdr)); /* entire header length */
4946     } else {
4947         /* XXX error stat ??? */
4948         return (0);
4949     }
4950 }
4951
4952 static uint8_t
4953 bxe_set_pbd_csum(struct bxe_fastpath        *fp,
4954                  struct mbuf                *m,
4955                  struct eth_tx_parse_bd_e1x *pbd)
4956 {
4957     struct ether_vlan_header *eh = NULL;
4958     struct ip *ip4 = NULL;
4959     struct ip6_hdr *ip6 = NULL;
4960     caddr_t ip = NULL;
4961     struct tcphdr *th = NULL;
4962     struct udphdr *uh = NULL;
4963     int e_hlen, ip_hlen;
4964     uint16_t proto;
4965     uint8_t hlen;
4966     uint16_t tmp_csum;
4967     uint32_t *tmp_uh;
4968
4969     /* get the Ethernet header */
4970     eh = mtod(m, struct ether_vlan_header *);
4971
4972     /* handle VLAN encapsulation if present */
4973     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
4974         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
4975         proto  = ntohs(eh->evl_proto);
4976     } else {
4977         e_hlen = ETHER_HDR_LEN;
4978         proto  = ntohs(eh->evl_encap_proto);
4979     }
4980
4981     switch (proto) {
4982     case ETHERTYPE_IP:
4983         /* get the IP header, if mbuf len < 20 then header in next mbuf */
4984         ip4 = (m->m_len < sizeof(struct ip)) ?
4985                   (struct ip *)m->m_next->m_data :
4986                   (struct ip *)(m->m_data + e_hlen);
4987         /* ip_hl is number of 32-bit words */
4988         ip_hlen = (ip4->ip_hl << 1);
4989         ip = (caddr_t)ip4;
4990         break;
4991     case ETHERTYPE_IPV6:
4992         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
4993         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
4994                   (struct ip6_hdr *)m->m_next->m_data :
4995                   (struct ip6_hdr *)(m->m_data + e_hlen);
4996         /* XXX cannot support offload with IPv6 extensions */
4997         ip_hlen = (sizeof(struct ip6_hdr) >> 1);
4998         ip = (caddr_t)ip6;
4999         break;
5000     default:
5001         /* We can't offload in this case... */
5002         /* XXX error stat ??? */
5003         return (0);
5004     }
5005
5006     hlen = (e_hlen >> 1);
5007
5008     /* note that rest of global_data is indirectly zeroed here */
5009     if (m->m_flags & M_VLANTAG) {
5010         pbd->global_data =
5011             htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
5012     } else {
5013         pbd->global_data = htole16(hlen);
5014     }
5015
5016     pbd->ip_hlen_w = ip_hlen;
5017
5018     hlen += pbd->ip_hlen_w;
5019
5020     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5021
5022     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5023                                   CSUM_TSO |
5024                                   CSUM_TCP_IPV6)) {
5025         th = (struct tcphdr *)(ip + (ip_hlen << 1));
5026         /* th_off is number of 32-bit words */
5027         hlen += (uint16_t)(th->th_off << 1);
5028     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5029                                          CSUM_UDP_IPV6)) {
5030         uh = (struct udphdr *)(ip + (ip_hlen << 1));
5031         hlen += (sizeof(struct udphdr) / 2);
5032     } else {
5033         /* valid case as only CSUM_IP was set */
5034         return (0);
5035     }
5036
5037     pbd->total_hlen_w = htole16(hlen);
5038
5039     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5040                                   CSUM_TSO |
5041                                   CSUM_TCP_IPV6)) {
5042         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5043         pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5044     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5045                                          CSUM_UDP_IPV6)) {
5046         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5047
5048         /*
5049          * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5050          * checksums and does not know anything about the UDP header and where
5051          * the checksum field is located. It only knows about TCP. Therefore
5052          * we "lie" to the hardware for outgoing UDP packets w/ checksum
5053          * offload. Since the checksum field offset for TCP is 16 bytes and
5054          * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5055          * bytes less than the start of the UDP header. This allows the
5056          * hardware to write the checksum in the correct spot. But the
5057          * hardware will compute a checksum which includes the last 10 bytes
5058          * of the IP header. To correct this we tweak the stack computed
5059          * pseudo checksum by folding in the calculation of the inverse
5060          * checksum for those final 10 bytes of the IP header. This allows
5061          * the correct checksum to be computed by the hardware.
5062          */
5063
5064         /* set pointer 10 bytes before UDP header */
5065         tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5066
5067         /* calculate a pseudo header checksum over the first 10 bytes */
5068         tmp_csum = in_pseudo(*tmp_uh,
5069                              *(tmp_uh + 1),
5070                              *(uint16_t *)(tmp_uh + 2));
5071
5072         pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5073     }
5074
5075     return (hlen * 2); /* entire header length, number of bytes */
5076 }
5077
5078 static void
5079 bxe_set_pbd_lso_e2(struct mbuf *m,
5080                    uint32_t    *parsing_data)
5081 {
5082     *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5083                        ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5084                       ETH_TX_PARSE_BD_E2_LSO_MSS);
5085
5086     /* XXX test for IPv6 with extension header... */
5087 }
5088
5089 static void
5090 bxe_set_pbd_lso(struct mbuf                *m,
5091                 struct eth_tx_parse_bd_e1x *pbd)
5092 {
5093     struct ether_vlan_header *eh = NULL;
5094     struct ip *ip = NULL;
5095     struct tcphdr *th = NULL;
5096     int e_hlen;
5097
5098     /* get the Ethernet header */
5099     eh = mtod(m, struct ether_vlan_header *);
5100
5101     /* handle VLAN encapsulation if present */
5102     e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5103                  (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5104
5105     /* get the IP and TCP header, with LSO entire header in first mbuf */
5106     /* XXX assuming IPv4 */
5107     ip = (struct ip *)(m->m_data + e_hlen);
5108     th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5109
5110     pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5111     pbd->tcp_send_seq = ntohl(th->th_seq);
5112     pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5113
5114 #if 1
5115         /* XXX IPv4 */
5116         pbd->ip_id = ntohs(ip->ip_id);
5117         pbd->tcp_pseudo_csum =
5118             ntohs(in_pseudo(ip->ip_src.s_addr,
5119                             ip->ip_dst.s_addr,
5120                             htons(IPPROTO_TCP)));
5121 #else
5122         /* XXX IPv6 */
5123         pbd->tcp_pseudo_csum =
5124             ntohs(in_pseudo(&ip6->ip6_src,
5125                             &ip6->ip6_dst,
5126                             htons(IPPROTO_TCP)));
5127 #endif
5128
5129     pbd->global_data |=
5130         htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5131 }
5132
5133 /*
5134  * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5135  * visible to the controller.
5136  *
5137  * If an mbuf is submitted to this routine and cannot be given to the
5138  * controller (e.g. it has too many fragments) then the function may free
5139  * the mbuf and return to the caller.
5140  *
5141  * Returns:
5142  *   0 = Success, !0 = Failure
5143  *   Note the side effect that an mbuf may be freed if it causes a problem.
5144  */
5145 static int
5146 bxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5147 {
5148     bus_dma_segment_t segs[32];
5149     struct mbuf *m0;
5150     struct bxe_sw_tx_bd *tx_buf;
5151     struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5152     struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5153     /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5154     struct eth_tx_bd *tx_data_bd;
5155     struct eth_tx_bd *tx_total_pkt_size_bd;
5156     struct eth_tx_start_bd *tx_start_bd;
5157     uint16_t bd_prod, pkt_prod, total_pkt_size;
5158     uint8_t mac_type;
5159     int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5160     struct bxe_softc *sc;
5161     uint16_t tx_bd_avail;
5162     struct ether_vlan_header *eh;
5163     uint32_t pbd_e2_parsing_data = 0;
5164     uint8_t hlen = 0;
5165     int tmp_bd;
5166     int i;
5167
5168     sc = fp->sc;
5169
5170 #if __FreeBSD_version >= 800000
5171     M_ASSERTPKTHDR(*m_head);
5172 #endif /* #if __FreeBSD_version >= 800000 */
5173
5174     m0 = *m_head;
5175     rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5176     tx_start_bd = NULL;
5177     tx_data_bd = NULL;
5178     tx_total_pkt_size_bd = NULL;
5179
5180     /* get the H/W pointer for packets and BDs */
5181     pkt_prod = fp->tx_pkt_prod;
5182     bd_prod = fp->tx_bd_prod;
5183
5184     mac_type = UNICAST_ADDRESS;
5185
5186     /* map the mbuf into the next open DMAable memory */
5187     tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5188     error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5189                                     tx_buf->m_map, m0,
5190                                     segs, &nsegs, BUS_DMA_NOWAIT);
5191
5192     /* mapping errors */
5193     if(__predict_false(error != 0)) {
5194         fp->eth_q_stats.tx_dma_mapping_failure++;
5195         if (error == ENOMEM) {
5196             /* resource issue, try again later */
5197             rc = ENOMEM;
5198         } else if (error == EFBIG) {
5199             /* possibly recoverable with defragmentation */
5200             fp->eth_q_stats.mbuf_defrag_attempts++;
5201             m0 = m_defrag(*m_head, M_DONTWAIT);
5202             if (m0 == NULL) {
5203                 fp->eth_q_stats.mbuf_defrag_failures++;
5204                 rc = ENOBUFS;
5205             } else {
5206                 /* defrag successful, try mapping again */
5207                 *m_head = m0;
5208                 error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5209                                                 tx_buf->m_map, m0,
5210                                                 segs, &nsegs, BUS_DMA_NOWAIT);
5211                 if (error) {
5212                     fp->eth_q_stats.tx_dma_mapping_failure++;
5213                     rc = error;
5214                 }
5215             }
5216         } else {
5217             /* unknown, unrecoverable mapping error */
5218             BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5219             bxe_dump_mbuf(sc, m0, FALSE);
5220             rc = error;
5221         }
5222
5223         goto bxe_tx_encap_continue;
5224     }
5225
5226     tx_bd_avail = bxe_tx_avail(sc, fp);
5227
5228     /* make sure there is enough room in the send queue */
5229     if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5230         /* Recoverable, try again later. */
5231         fp->eth_q_stats.tx_hw_queue_full++;
5232         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5233         rc = ENOMEM;
5234         goto bxe_tx_encap_continue;
5235     }
5236
5237     /* capture the current H/W TX chain high watermark */
5238     if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5239                         (TX_BD_USABLE - tx_bd_avail))) {
5240         fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5241     }
5242
5243     /* make sure it fits in the packet window */
5244     if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5245         /*
5246          * The mbuf may be to big for the controller to handle. If the frame
5247          * is a TSO frame we'll need to do an additional check.
5248          */
5249         if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5250             if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5251                 goto bxe_tx_encap_continue; /* OK to send */
5252             } else {
5253                 fp->eth_q_stats.tx_window_violation_tso++;
5254             }
5255         } else {
5256             fp->eth_q_stats.tx_window_violation_std++;
5257         }
5258
5259         /* lets try to defragment this mbuf and remap it */
5260         fp->eth_q_stats.mbuf_defrag_attempts++;
5261         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5262
5263         m0 = m_defrag(*m_head, M_DONTWAIT);
5264         if (m0 == NULL) {
5265             fp->eth_q_stats.mbuf_defrag_failures++;
5266             /* Ugh, just drop the frame... :( */
5267             rc = ENOBUFS;
5268         } else {
5269             /* defrag successful, try mapping again */
5270             *m_head = m0;
5271             error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5272                                             tx_buf->m_map, m0,
5273                                             segs, &nsegs, BUS_DMA_NOWAIT);
5274             if (error) {
5275                 fp->eth_q_stats.tx_dma_mapping_failure++;
5276                 /* No sense in trying to defrag/copy chain, drop it. :( */
5277                 rc = error;
5278             } else {
5279                /* if the chain is still too long then drop it */
5280                 if(m0->m_pkthdr.csum_flags & CSUM_TSO) {
5281                     /*
5282                      * in case TSO is enabled nsegs should be checked against
5283                      * BXE_TSO_MAX_SEGMENTS
5284                      */
5285                     if (__predict_false(nsegs > BXE_TSO_MAX_SEGMENTS)) {
5286                         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5287                         fp->eth_q_stats.nsegs_path1_errors++;
5288                         rc = ENODEV;
5289                     }
5290                 } else {
5291                     if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5292                         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5293                         fp->eth_q_stats.nsegs_path2_errors++;
5294                         rc = ENODEV;
5295                     }
5296                 }
5297             }
5298         }
5299     }
5300
5301 bxe_tx_encap_continue:
5302
5303     /* Check for errors */
5304     if (rc) {
5305         if (rc == ENOMEM) {
5306             /* recoverable try again later  */
5307         } else {
5308             fp->eth_q_stats.tx_soft_errors++;
5309             fp->eth_q_stats.mbuf_alloc_tx--;
5310             m_freem(*m_head);
5311             *m_head = NULL;
5312         }
5313
5314         return (rc);
5315     }
5316
5317     /* set flag according to packet type (UNICAST_ADDRESS is default) */
5318     if (m0->m_flags & M_BCAST) {
5319         mac_type = BROADCAST_ADDRESS;
5320     } else if (m0->m_flags & M_MCAST) {
5321         mac_type = MULTICAST_ADDRESS;
5322     }
5323
5324     /* store the mbuf into the mbuf ring */
5325     tx_buf->m        = m0;
5326     tx_buf->first_bd = fp->tx_bd_prod;
5327     tx_buf->flags    = 0;
5328
5329     /* prepare the first transmit (start) BD for the mbuf */
5330     tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5331
5332     BLOGD(sc, DBG_TX,
5333           "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5334           pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5335
5336     tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5337     tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5338     tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5339     total_pkt_size += tx_start_bd->nbytes;
5340     tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5341
5342     tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5343
5344     /* all frames have at least Start BD + Parsing BD */
5345     nbds = nsegs + 1;
5346     tx_start_bd->nbd = htole16(nbds);
5347
5348     if (m0->m_flags & M_VLANTAG) {
5349         tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5350         tx_start_bd->bd_flags.as_bitfield |=
5351             (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5352     } else {
5353         /* vf tx, start bd must hold the ethertype for fw to enforce it */
5354         if (IS_VF(sc)) {
5355             /* map ethernet header to find type and header length */
5356             eh = mtod(m0, struct ether_vlan_header *);
5357             tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5358         } else {
5359             /* used by FW for packet accounting */
5360             tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5361         }
5362     }
5363
5364     /*
5365      * add a parsing BD from the chain. The parsing BD is always added
5366      * though it is only used for TSO and chksum
5367      */
5368     bd_prod = TX_BD_NEXT(bd_prod);
5369
5370     if (m0->m_pkthdr.csum_flags) {
5371         if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5372             fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5373             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5374         }
5375
5376         if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5377             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5378                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5379         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5380             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5381                                                   ETH_TX_BD_FLAGS_IS_UDP |
5382                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5383         } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5384                    (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5385             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5386         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5387             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5388                                                   ETH_TX_BD_FLAGS_IS_UDP);
5389         }
5390     }
5391
5392     if (!CHIP_IS_E1x(sc)) {
5393         pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5394         memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5395
5396         if (m0->m_pkthdr.csum_flags) {
5397             hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5398         }
5399
5400         SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5401                  mac_type);
5402     } else {
5403         uint16_t global_data = 0;
5404
5405         pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5406         memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5407
5408         if (m0->m_pkthdr.csum_flags) {
5409             hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5410         }
5411
5412         SET_FLAG(global_data,
5413                  ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5414         pbd_e1x->global_data |= htole16(global_data);
5415     }
5416
5417     /* setup the parsing BD with TSO specific info */
5418     if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5419         fp->eth_q_stats.tx_ofld_frames_lso++;
5420         tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5421
5422         if (__predict_false(tx_start_bd->nbytes > hlen)) {
5423             fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5424
5425             /* split the first BD into header/data making the fw job easy */
5426             nbds++;
5427             tx_start_bd->nbd = htole16(nbds);
5428             tx_start_bd->nbytes = htole16(hlen);
5429
5430             bd_prod = TX_BD_NEXT(bd_prod);
5431
5432             /* new transmit BD after the tx_parse_bd */
5433             tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5434             tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5435             tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5436             tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5437             if (tx_total_pkt_size_bd == NULL) {
5438                 tx_total_pkt_size_bd = tx_data_bd;
5439             }
5440
5441             BLOGD(sc, DBG_TX,
5442                   "TSO split header size is %d (%x:%x) nbds %d\n",
5443                   le16toh(tx_start_bd->nbytes),
5444                   le32toh(tx_start_bd->addr_hi),
5445                   le32toh(tx_start_bd->addr_lo),
5446                   nbds);
5447         }
5448
5449         if (!CHIP_IS_E1x(sc)) {
5450             bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5451         } else {
5452             bxe_set_pbd_lso(m0, pbd_e1x);
5453         }
5454     }
5455
5456     if (pbd_e2_parsing_data) {
5457         pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5458     }
5459
5460     /* prepare remaining BDs, start tx bd contains first seg/frag */
5461     for (i = 1; i < nsegs ; i++) {
5462         bd_prod = TX_BD_NEXT(bd_prod);
5463         tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5464         tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5465         tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5466         tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5467         if (tx_total_pkt_size_bd == NULL) {
5468             tx_total_pkt_size_bd = tx_data_bd;
5469         }
5470         total_pkt_size += tx_data_bd->nbytes;
5471     }
5472
5473     BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5474
5475     if (tx_total_pkt_size_bd != NULL) {
5476         tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5477     }
5478
5479     if (__predict_false(sc->debug & DBG_TX)) {
5480         tmp_bd = tx_buf->first_bd;
5481         for (i = 0; i < nbds; i++)
5482         {
5483             if (i == 0) {
5484                 BLOGD(sc, DBG_TX,
5485                       "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5486                       "bd_flags=0x%x hdr_nbds=%d\n",
5487                       tx_start_bd,
5488                       tmp_bd,
5489                       le16toh(tx_start_bd->nbd),
5490                       le16toh(tx_start_bd->vlan_or_ethertype),
5491                       tx_start_bd->bd_flags.as_bitfield,
5492                       (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5493             } else if (i == 1) {
5494                 if (pbd_e1x) {
5495                     BLOGD(sc, DBG_TX,
5496                           "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5497                           "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5498                           "tcp_seq=%u total_hlen_w=%u\n",
5499                           pbd_e1x,
5500                           tmp_bd,
5501                           pbd_e1x->global_data,
5502                           pbd_e1x->ip_hlen_w,
5503                           pbd_e1x->ip_id,
5504                           pbd_e1x->lso_mss,
5505                           pbd_e1x->tcp_flags,
5506                           pbd_e1x->tcp_pseudo_csum,
5507                           pbd_e1x->tcp_send_seq,
5508                           le16toh(pbd_e1x->total_hlen_w));
5509                 } else { /* if (pbd_e2) */
5510                     BLOGD(sc, DBG_TX,
5511                           "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5512                           "src=%02x:%02x:%02x parsing_data=0x%x\n",
5513                           pbd_e2,
5514                           tmp_bd,
5515                           pbd_e2->data.mac_addr.dst_hi,
5516                           pbd_e2->data.mac_addr.dst_mid,
5517                           pbd_e2->data.mac_addr.dst_lo,
5518                           pbd_e2->data.mac_addr.src_hi,
5519                           pbd_e2->data.mac_addr.src_mid,
5520                           pbd_e2->data.mac_addr.src_lo,
5521                           pbd_e2->parsing_data);
5522                 }
5523             }
5524
5525             if (i != 1) { /* skip parse db as it doesn't hold data */
5526                 tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5527                 BLOGD(sc, DBG_TX,
5528                       "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5529                       tx_data_bd,
5530                       tmp_bd,
5531                       le16toh(tx_data_bd->nbytes),
5532                       le32toh(tx_data_bd->addr_hi),
5533                       le32toh(tx_data_bd->addr_lo));
5534             }
5535
5536             tmp_bd = TX_BD_NEXT(tmp_bd);
5537         }
5538     }
5539
5540     BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5541
5542     /* update TX BD producer index value for next TX */
5543     bd_prod = TX_BD_NEXT(bd_prod);
5544
5545     /*
5546      * If the chain of tx_bd's describing this frame is adjacent to or spans
5547      * an eth_tx_next_bd element then we need to increment the nbds value.
5548      */
5549     if (TX_BD_IDX(bd_prod) < nbds) {
5550         nbds++;
5551     }
5552
5553     /* don't allow reordering of writes for nbd and packets */
5554     mb();
5555
5556     fp->tx_db.data.prod += nbds;
5557
5558     /* producer points to the next free tx_bd at this point */
5559     fp->tx_pkt_prod++;
5560     fp->tx_bd_prod = bd_prod;
5561
5562     DOORBELL(sc, fp->index, fp->tx_db.raw);
5563
5564     fp->eth_q_stats.tx_pkts++;
5565
5566     /* Prevent speculative reads from getting ahead of the status block. */
5567     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5568                       0, 0, BUS_SPACE_BARRIER_READ);
5569
5570     /* Prevent speculative reads from getting ahead of the doorbell. */
5571     bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5572                       0, 0, BUS_SPACE_BARRIER_READ);
5573
5574     return (0);
5575 }
5576
5577 static void
5578 bxe_tx_start_locked(struct bxe_softc    *sc,
5579                     struct ifnet        *ifp,
5580                     struct bxe_fastpath *fp)
5581 {
5582     struct mbuf *m = NULL;
5583     int tx_count = 0;
5584     uint16_t tx_bd_avail;
5585
5586     BXE_FP_TX_LOCK_ASSERT(fp);
5587
5588     /* keep adding entries while there are frames to send */
5589     while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
5590
5591         /*
5592          * check for any frames to send
5593          * dequeue can still be NULL even if queue is not empty
5594          */
5595         IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
5596         if (__predict_false(m == NULL)) {
5597             break;
5598         }
5599
5600         /* the mbuf now belongs to us */
5601         fp->eth_q_stats.mbuf_alloc_tx++;
5602
5603         /*
5604          * Put the frame into the transmit ring. If we don't have room,
5605          * place the mbuf back at the head of the TX queue, set the
5606          * OACTIVE flag, and wait for the NIC to drain the chain.
5607          */
5608         if (__predict_false(bxe_tx_encap(fp, &m))) {
5609             fp->eth_q_stats.tx_encap_failures++;
5610             if (m != NULL) {
5611                 /* mark the TX queue as full and return the frame */
5612                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5613                 IFQ_DRV_PREPEND(&ifp->if_snd, m);
5614                 fp->eth_q_stats.mbuf_alloc_tx--;
5615                 fp->eth_q_stats.tx_queue_xoff++;
5616             }
5617
5618             /* stop looking for more work */
5619             break;
5620         }
5621
5622         /* the frame was enqueued successfully */
5623         tx_count++;
5624
5625         /* send a copy of the frame to any BPF listeners. */
5626         BPF_MTAP(ifp, m);
5627
5628         tx_bd_avail = bxe_tx_avail(sc, fp);
5629
5630         /* handle any completions if we're running low */
5631         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5632             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5633             bxe_txeof(sc, fp);
5634             if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5635                 break;
5636             }
5637         }
5638     }
5639
5640     /* all TX packets were dequeued and/or the tx ring is full */
5641     if (tx_count > 0) {
5642         /* reset the TX watchdog timeout timer */
5643         fp->watchdog_timer = BXE_TX_TIMEOUT;
5644     }
5645 }
5646
5647 /* Legacy (non-RSS) dispatch routine */
5648 static void
5649 bxe_tx_start(struct ifnet *ifp)
5650 {
5651     struct bxe_softc *sc;
5652     struct bxe_fastpath *fp;
5653
5654     sc = ifp->if_softc;
5655
5656     if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
5657         BLOGW(sc, "Interface not running, ignoring transmit request\n");
5658         return;
5659     }
5660
5661     if (!sc->link_vars.link_up) {
5662         BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5663         return;
5664     }
5665
5666     fp = &sc->fp[0];
5667
5668     if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5669         fp->eth_q_stats.tx_queue_full_return++;
5670         return;
5671     }
5672
5673     BXE_FP_TX_LOCK(fp);
5674     bxe_tx_start_locked(sc, ifp, fp);
5675     BXE_FP_TX_UNLOCK(fp);
5676 }
5677
5678 #if __FreeBSD_version >= 901504
5679
5680 static int
5681 bxe_tx_mq_start_locked(struct bxe_softc    *sc,
5682                        struct ifnet        *ifp,
5683                        struct bxe_fastpath *fp,
5684                        struct mbuf         *m)
5685 {
5686     struct buf_ring *tx_br = fp->tx_br;
5687     struct mbuf *next;
5688     int depth, rc, tx_count;
5689     uint16_t tx_bd_avail;
5690
5691     rc = tx_count = 0;
5692
5693     BXE_FP_TX_LOCK_ASSERT(fp);
5694
5695     if (sc->state != BXE_STATE_OPEN)  {
5696         fp->eth_q_stats.bxe_tx_mq_sc_state_failures++;
5697         return ENETDOWN;
5698     }
5699
5700     if (!tx_br) {
5701         BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
5702         return (EINVAL);
5703     }
5704
5705     if (m != NULL) {
5706         rc = drbr_enqueue(ifp, tx_br, m);
5707         if (rc != 0) {
5708             fp->eth_q_stats.tx_soft_errors++;
5709             goto bxe_tx_mq_start_locked_exit;
5710         }
5711     }
5712
5713     if (!sc->link_vars.link_up || !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
5714         fp->eth_q_stats.tx_request_link_down_failures++;
5715         goto bxe_tx_mq_start_locked_exit;
5716     }
5717
5718     /* fetch the depth of the driver queue */
5719     depth = drbr_inuse(ifp, tx_br);
5720     if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
5721         fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
5722     }
5723
5724     /* keep adding entries while there are frames to send */
5725     while ((next = drbr_peek(ifp, tx_br)) != NULL) {
5726         /* handle any completions if we're running low */
5727         tx_bd_avail = bxe_tx_avail(sc, fp);
5728         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5729             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5730             bxe_txeof(sc, fp);
5731             tx_bd_avail = bxe_tx_avail(sc, fp);
5732             if (tx_bd_avail < (BXE_TSO_MAX_SEGMENTS + 1)) {
5733                 fp->eth_q_stats.bd_avail_too_less_failures++;
5734                 m_freem(next);
5735                 drbr_advance(ifp, tx_br);
5736                 rc = ENOBUFS;
5737                 break;
5738             }
5739         }
5740
5741         /* the mbuf now belongs to us */
5742         fp->eth_q_stats.mbuf_alloc_tx++;
5743
5744         /*
5745          * Put the frame into the transmit ring. If we don't have room,
5746          * place the mbuf back at the head of the TX queue, set the
5747          * OACTIVE flag, and wait for the NIC to drain the chain.
5748          */
5749         rc = bxe_tx_encap(fp, &next);
5750         if (__predict_false(rc != 0)) {
5751             fp->eth_q_stats.tx_encap_failures++;
5752             if (next != NULL) {
5753                 /* mark the TX queue as full and save the frame */
5754                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5755                 drbr_putback(ifp, tx_br, next);
5756                 fp->eth_q_stats.mbuf_alloc_tx--;
5757                 fp->eth_q_stats.tx_frames_deferred++;
5758             } else
5759                 drbr_advance(ifp, tx_br);
5760
5761             /* stop looking for more work */
5762             break;
5763         }
5764
5765         /* the transmit frame was enqueued successfully */
5766         tx_count++;
5767
5768         /* send a copy of the frame to any BPF listeners */
5769         BPF_MTAP(ifp, next);
5770
5771         drbr_advance(ifp, tx_br);
5772     }
5773
5774     /* all TX packets were dequeued and/or the tx ring is full */
5775     if (tx_count > 0) {
5776         /* reset the TX watchdog timeout timer */
5777         fp->watchdog_timer = BXE_TX_TIMEOUT;
5778     }
5779
5780 bxe_tx_mq_start_locked_exit:
5781     /* If we didn't drain the drbr, enqueue a task in the future to do it. */
5782     if (!drbr_empty(ifp, tx_br)) {
5783         fp->eth_q_stats.tx_mq_not_empty++;
5784         taskqueue_enqueue_timeout(fp->tq, &fp->tx_timeout_task, 1);
5785     }
5786
5787     return (rc);
5788 }
5789
5790 static void
5791 bxe_tx_mq_start_deferred(void *arg,
5792                          int pending)
5793 {
5794     struct bxe_fastpath *fp = (struct bxe_fastpath *)arg;
5795     struct bxe_softc *sc = fp->sc;
5796     struct ifnet *ifp = sc->ifnet;
5797
5798     BXE_FP_TX_LOCK(fp);
5799     bxe_tx_mq_start_locked(sc, ifp, fp, NULL);
5800     BXE_FP_TX_UNLOCK(fp);
5801 }
5802
5803 /* Multiqueue (TSS) dispatch routine. */
5804 static int
5805 bxe_tx_mq_start(struct ifnet *ifp,
5806                 struct mbuf  *m)
5807 {
5808     struct bxe_softc *sc = ifp->if_softc;
5809     struct bxe_fastpath *fp;
5810     int fp_index, rc;
5811
5812     fp_index = 0; /* default is the first queue */
5813
5814     /* check if flowid is set */
5815
5816     if (BXE_VALID_FLOWID(m))
5817         fp_index = (m->m_pkthdr.flowid % sc->num_queues);
5818
5819     fp = &sc->fp[fp_index];
5820
5821     if (sc->state != BXE_STATE_OPEN)  {
5822         fp->eth_q_stats.bxe_tx_mq_sc_state_failures++;
5823         return ENETDOWN;
5824     }
5825
5826     if (BXE_FP_TX_TRYLOCK(fp)) {
5827         rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
5828         BXE_FP_TX_UNLOCK(fp);
5829     } else {
5830         rc = drbr_enqueue(ifp, fp->tx_br, m);
5831         taskqueue_enqueue(fp->tq, &fp->tx_task);
5832     }
5833
5834     return (rc);
5835 }
5836
5837 static void
5838 bxe_mq_flush(struct ifnet *ifp)
5839 {
5840     struct bxe_softc *sc = ifp->if_softc;
5841     struct bxe_fastpath *fp;
5842     struct mbuf *m;
5843     int i;
5844
5845     for (i = 0; i < sc->num_queues; i++) {
5846         fp = &sc->fp[i];
5847
5848         if (fp->state != BXE_FP_STATE_IRQ) {
5849             BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
5850                   fp->index, fp->state);
5851             continue;
5852         }
5853
5854         if (fp->tx_br != NULL) {
5855             BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
5856             BXE_FP_TX_LOCK(fp);
5857             while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
5858                 m_freem(m);
5859             }
5860             BXE_FP_TX_UNLOCK(fp);
5861         }
5862     }
5863
5864     if_qflush(ifp);
5865 }
5866
5867 #endif /* FreeBSD_version >= 901504 */
5868
5869 static uint16_t
5870 bxe_cid_ilt_lines(struct bxe_softc *sc)
5871 {
5872     if (IS_SRIOV(sc)) {
5873         return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
5874     }
5875     return (L2_ILT_LINES(sc));
5876 }
5877
5878 static void
5879 bxe_ilt_set_info(struct bxe_softc *sc)
5880 {
5881     struct ilt_client_info *ilt_client;
5882     struct ecore_ilt *ilt = sc->ilt;
5883     uint16_t line = 0;
5884
5885     ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
5886     BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
5887
5888     /* CDU */
5889     ilt_client = &ilt->clients[ILT_CLIENT_CDU];
5890     ilt_client->client_num = ILT_CLIENT_CDU;
5891     ilt_client->page_size = CDU_ILT_PAGE_SZ;
5892     ilt_client->flags = ILT_CLIENT_SKIP_MEM;
5893     ilt_client->start = line;
5894     line += bxe_cid_ilt_lines(sc);
5895
5896     if (CNIC_SUPPORT(sc)) {
5897         line += CNIC_ILT_LINES;
5898     }
5899
5900     ilt_client->end = (line - 1);
5901
5902     BLOGD(sc, DBG_LOAD,
5903           "ilt client[CDU]: start %d, end %d, "
5904           "psz 0x%x, flags 0x%x, hw psz %d\n",
5905           ilt_client->start, ilt_client->end,
5906           ilt_client->page_size,
5907           ilt_client->flags,
5908           ilog2(ilt_client->page_size >> 12));
5909
5910     /* QM */
5911     if (QM_INIT(sc->qm_cid_count)) {
5912         ilt_client = &ilt->clients[ILT_CLIENT_QM];
5913         ilt_client->client_num = ILT_CLIENT_QM;
5914         ilt_client->page_size = QM_ILT_PAGE_SZ;
5915         ilt_client->flags = 0;
5916         ilt_client->start = line;
5917
5918         /* 4 bytes for each cid */
5919         line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
5920                              QM_ILT_PAGE_SZ);
5921
5922         ilt_client->end = (line - 1);
5923
5924         BLOGD(sc, DBG_LOAD,
5925               "ilt client[QM]: start %d, end %d, "
5926               "psz 0x%x, flags 0x%x, hw psz %d\n",
5927               ilt_client->start, ilt_client->end,
5928               ilt_client->page_size, ilt_client->flags,
5929               ilog2(ilt_client->page_size >> 12));
5930     }
5931
5932     if (CNIC_SUPPORT(sc)) {
5933         /* SRC */
5934         ilt_client = &ilt->clients[ILT_CLIENT_SRC];
5935         ilt_client->client_num = ILT_CLIENT_SRC;
5936         ilt_client->page_size = SRC_ILT_PAGE_SZ;
5937         ilt_client->flags = 0;
5938         ilt_client->start = line;
5939         line += SRC_ILT_LINES;
5940         ilt_client->end = (line - 1);
5941
5942         BLOGD(sc, DBG_LOAD,
5943               "ilt client[SRC]: start %d, end %d, "
5944               "psz 0x%x, flags 0x%x, hw psz %d\n",
5945               ilt_client->start, ilt_client->end,
5946               ilt_client->page_size, ilt_client->flags,
5947               ilog2(ilt_client->page_size >> 12));
5948
5949         /* TM */
5950         ilt_client = &ilt->clients[ILT_CLIENT_TM];
5951         ilt_client->client_num = ILT_CLIENT_TM;
5952         ilt_client->page_size = TM_ILT_PAGE_SZ;
5953         ilt_client->flags = 0;
5954         ilt_client->start = line;
5955         line += TM_ILT_LINES;
5956         ilt_client->end = (line - 1);
5957
5958         BLOGD(sc, DBG_LOAD,
5959               "ilt client[TM]: start %d, end %d, "
5960               "psz 0x%x, flags 0x%x, hw psz %d\n",
5961               ilt_client->start, ilt_client->end,
5962               ilt_client->page_size, ilt_client->flags,
5963               ilog2(ilt_client->page_size >> 12));
5964     }
5965
5966     KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
5967 }
5968
5969 static void
5970 bxe_set_fp_rx_buf_size(struct bxe_softc *sc)
5971 {
5972     int i;
5973     uint32_t rx_buf_size;
5974
5975     rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
5976
5977     for (i = 0; i < sc->num_queues; i++) {
5978         if(rx_buf_size <= MCLBYTES){
5979             sc->fp[i].rx_buf_size = rx_buf_size;
5980             sc->fp[i].mbuf_alloc_size = MCLBYTES;
5981         }else if (rx_buf_size <= MJUMPAGESIZE){
5982             sc->fp[i].rx_buf_size = rx_buf_size;
5983             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5984         }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
5985             sc->fp[i].rx_buf_size = MCLBYTES;
5986             sc->fp[i].mbuf_alloc_size = MCLBYTES;
5987         }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
5988             sc->fp[i].rx_buf_size = MJUMPAGESIZE;
5989             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
5990         }else {
5991             sc->fp[i].rx_buf_size = MCLBYTES;
5992             sc->fp[i].mbuf_alloc_size = MCLBYTES;
5993         }
5994     }
5995 }
5996
5997 static int
5998 bxe_alloc_ilt_mem(struct bxe_softc *sc)
5999 {
6000     int rc = 0;
6001
6002     if ((sc->ilt =
6003          (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
6004                                     M_BXE_ILT,
6005                                     (M_NOWAIT | M_ZERO))) == NULL) {
6006         rc = 1;
6007     }
6008
6009     return (rc);
6010 }
6011
6012 static int
6013 bxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
6014 {
6015     int rc = 0;
6016
6017     if ((sc->ilt->lines =
6018          (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
6019                                     M_BXE_ILT,
6020                                     (M_NOWAIT | M_ZERO))) == NULL) {
6021         rc = 1;
6022     }
6023
6024     return (rc);
6025 }
6026
6027 static void
6028 bxe_free_ilt_mem(struct bxe_softc *sc)
6029 {
6030     if (sc->ilt != NULL) {
6031         free(sc->ilt, M_BXE_ILT);
6032         sc->ilt = NULL;
6033     }
6034 }
6035
6036 static void
6037 bxe_free_ilt_lines_mem(struct bxe_softc *sc)
6038 {
6039     if (sc->ilt->lines != NULL) {
6040         free(sc->ilt->lines, M_BXE_ILT);
6041         sc->ilt->lines = NULL;
6042     }
6043 }
6044
6045 static void
6046 bxe_free_mem(struct bxe_softc *sc)
6047 {
6048     int i;
6049
6050     for (i = 0; i < L2_ILT_LINES(sc); i++) {
6051         bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6052         sc->context[i].vcxt = NULL;
6053         sc->context[i].size = 0;
6054     }
6055
6056     ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6057
6058     bxe_free_ilt_lines_mem(sc);
6059
6060 }
6061
6062 static int
6063 bxe_alloc_mem(struct bxe_softc *sc)
6064 {
6065
6066     int context_size;
6067     int allocated;
6068     int i;
6069
6070     /*
6071      * Allocate memory for CDU context:
6072      * This memory is allocated separately and not in the generic ILT
6073      * functions because CDU differs in few aspects:
6074      * 1. There can be multiple entities allocating memory for context -
6075      * regular L2, CNIC, and SRIOV drivers. Each separately controls
6076      * its own ILT lines.
6077      * 2. Since CDU page-size is not a single 4KB page (which is the case
6078      * for the other ILT clients), to be efficient we want to support
6079      * allocation of sub-page-size in the last entry.
6080      * 3. Context pointers are used by the driver to pass to FW / update
6081      * the context (for the other ILT clients the pointers are used just to
6082      * free the memory during unload).
6083      */
6084     context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6085     for (i = 0, allocated = 0; allocated < context_size; i++) {
6086         sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6087                                   (context_size - allocated));
6088
6089         if (bxe_dma_alloc(sc, sc->context[i].size,
6090                           &sc->context[i].vcxt_dma,
6091                           "cdu context") != 0) {
6092             bxe_free_mem(sc);
6093             return (-1);
6094         }
6095
6096         sc->context[i].vcxt =
6097             (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6098
6099         allocated += sc->context[i].size;
6100     }
6101
6102     bxe_alloc_ilt_lines_mem(sc);
6103
6104     BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6105           sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6106     {
6107         for (i = 0; i < 4; i++) {
6108             BLOGD(sc, DBG_LOAD,
6109                   "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6110                   i,
6111                   sc->ilt->clients[i].page_size,
6112                   sc->ilt->clients[i].start,
6113                   sc->ilt->clients[i].end,
6114                   sc->ilt->clients[i].client_num,
6115                   sc->ilt->clients[i].flags);
6116         }
6117     }
6118     if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6119         BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6120         bxe_free_mem(sc);
6121         return (-1);
6122     }
6123
6124     return (0);
6125 }
6126
6127 static void
6128 bxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6129 {
6130     struct bxe_softc *sc;
6131     int i;
6132
6133     sc = fp->sc;
6134
6135     if (fp->rx_mbuf_tag == NULL) {
6136         return;
6137     }
6138
6139     /* free all mbufs and unload all maps */
6140     for (i = 0; i < RX_BD_TOTAL; i++) {
6141         if (fp->rx_mbuf_chain[i].m_map != NULL) {
6142             bus_dmamap_sync(fp->rx_mbuf_tag,
6143                             fp->rx_mbuf_chain[i].m_map,
6144                             BUS_DMASYNC_POSTREAD);
6145             bus_dmamap_unload(fp->rx_mbuf_tag,
6146                               fp->rx_mbuf_chain[i].m_map);
6147         }
6148
6149         if (fp->rx_mbuf_chain[i].m != NULL) {
6150             m_freem(fp->rx_mbuf_chain[i].m);
6151             fp->rx_mbuf_chain[i].m = NULL;
6152             fp->eth_q_stats.mbuf_alloc_rx--;
6153         }
6154     }
6155 }
6156
6157 static void
6158 bxe_free_tpa_pool(struct bxe_fastpath *fp)
6159 {
6160     struct bxe_softc *sc;
6161     int i, max_agg_queues;
6162
6163     sc = fp->sc;
6164
6165     if (fp->rx_mbuf_tag == NULL) {
6166         return;
6167     }
6168
6169     max_agg_queues = MAX_AGG_QS(sc);
6170
6171     /* release all mbufs and unload all DMA maps in the TPA pool */
6172     for (i = 0; i < max_agg_queues; i++) {
6173         if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6174             bus_dmamap_sync(fp->rx_mbuf_tag,
6175                             fp->rx_tpa_info[i].bd.m_map,
6176                             BUS_DMASYNC_POSTREAD);
6177             bus_dmamap_unload(fp->rx_mbuf_tag,
6178                               fp->rx_tpa_info[i].bd.m_map);
6179         }
6180
6181         if (fp->rx_tpa_info[i].bd.m != NULL) {
6182             m_freem(fp->rx_tpa_info[i].bd.m);
6183             fp->rx_tpa_info[i].bd.m = NULL;
6184             fp->eth_q_stats.mbuf_alloc_tpa--;
6185         }
6186     }
6187 }
6188
6189 static void
6190 bxe_free_sge_chain(struct bxe_fastpath *fp)
6191 {
6192     struct bxe_softc *sc;
6193     int i;
6194
6195     sc = fp->sc;
6196
6197     if (fp->rx_sge_mbuf_tag == NULL) {
6198         return;
6199     }
6200
6201     /* rree all mbufs and unload all maps */
6202     for (i = 0; i < RX_SGE_TOTAL; i++) {
6203         if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6204             bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6205                             fp->rx_sge_mbuf_chain[i].m_map,
6206                             BUS_DMASYNC_POSTREAD);
6207             bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6208                               fp->rx_sge_mbuf_chain[i].m_map);
6209         }
6210
6211         if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6212             m_freem(fp->rx_sge_mbuf_chain[i].m);
6213             fp->rx_sge_mbuf_chain[i].m = NULL;
6214             fp->eth_q_stats.mbuf_alloc_sge--;
6215         }
6216     }
6217 }
6218
6219 static void
6220 bxe_free_fp_buffers(struct bxe_softc *sc)
6221 {
6222     struct bxe_fastpath *fp;
6223     int i;
6224
6225     for (i = 0; i < sc->num_queues; i++) {
6226         fp = &sc->fp[i];
6227
6228 #if __FreeBSD_version >= 901504
6229         if (fp->tx_br != NULL) {
6230             /* just in case bxe_mq_flush() wasn't called */
6231             if (mtx_initialized(&fp->tx_mtx)) {
6232                 struct mbuf *m;
6233
6234                 BXE_FP_TX_LOCK(fp);
6235                 while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6236                     m_freem(m);
6237                 BXE_FP_TX_UNLOCK(fp);
6238             }
6239         }
6240 #endif
6241
6242         /* free all RX buffers */
6243         bxe_free_rx_bd_chain(fp);
6244         bxe_free_tpa_pool(fp);
6245         bxe_free_sge_chain(fp);
6246
6247         if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6248             BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6249                   fp->eth_q_stats.mbuf_alloc_rx);
6250         }
6251
6252         if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6253             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6254                   fp->eth_q_stats.mbuf_alloc_sge);
6255         }
6256
6257         if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6258             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6259                   fp->eth_q_stats.mbuf_alloc_tpa);
6260         }
6261
6262         if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6263             BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6264                   fp->eth_q_stats.mbuf_alloc_tx);
6265         }
6266
6267         /* XXX verify all mbufs were reclaimed */
6268     }
6269 }
6270
6271 static int
6272 bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6273                      uint16_t            prev_index,
6274                      uint16_t            index)
6275 {
6276     struct bxe_sw_rx_bd *rx_buf;
6277     struct eth_rx_bd *rx_bd;
6278     bus_dma_segment_t segs[1];
6279     bus_dmamap_t map;
6280     struct mbuf *m;
6281     int nsegs, rc;
6282
6283     rc = 0;
6284
6285     /* allocate the new RX BD mbuf */
6286     m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6287     if (__predict_false(m == NULL)) {
6288         fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6289         return (ENOBUFS);
6290     }
6291
6292     fp->eth_q_stats.mbuf_alloc_rx++;
6293
6294     /* initialize the mbuf buffer length */
6295     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6296
6297     /* map the mbuf into non-paged pool */
6298     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6299                                  fp->rx_mbuf_spare_map,
6300                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6301     if (__predict_false(rc != 0)) {
6302         fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6303         m_freem(m);
6304         fp->eth_q_stats.mbuf_alloc_rx--;
6305         return (rc);
6306     }
6307
6308     /* all mbufs must map to a single segment */
6309     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6310
6311     /* release any existing RX BD mbuf mappings */
6312
6313     if (prev_index != index) {
6314         rx_buf = &fp->rx_mbuf_chain[prev_index];
6315
6316         if (rx_buf->m_map != NULL) {
6317             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6318                             BUS_DMASYNC_POSTREAD);
6319             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6320         }
6321
6322         /*
6323          * We only get here from bxe_rxeof() when the maximum number
6324          * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6325          * holds the mbuf in the prev_index so it's OK to NULL it out
6326          * here without concern of a memory leak.
6327          */
6328         fp->rx_mbuf_chain[prev_index].m = NULL;
6329     }
6330
6331     rx_buf = &fp->rx_mbuf_chain[index];
6332
6333     if (rx_buf->m_map != NULL) {
6334         bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6335                         BUS_DMASYNC_POSTREAD);
6336         bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6337     }
6338
6339     /* save the mbuf and mapping info for a future packet */
6340     map = (prev_index != index) ?
6341               fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6342     rx_buf->m_map = fp->rx_mbuf_spare_map;
6343     fp->rx_mbuf_spare_map = map;
6344     bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6345                     BUS_DMASYNC_PREREAD);
6346     rx_buf->m = m;
6347
6348     rx_bd = &fp->rx_chain[index];
6349     rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6350     rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6351
6352     return (rc);
6353 }
6354
6355 static int
6356 bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6357                       int                 queue)
6358 {
6359     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6360     bus_dma_segment_t segs[1];
6361     bus_dmamap_t map;
6362     struct mbuf *m;
6363     int nsegs;
6364     int rc = 0;
6365
6366     /* allocate the new TPA mbuf */
6367     m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6368     if (__predict_false(m == NULL)) {
6369         fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6370         return (ENOBUFS);
6371     }
6372
6373     fp->eth_q_stats.mbuf_alloc_tpa++;
6374
6375     /* initialize the mbuf buffer length */
6376     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6377
6378     /* map the mbuf into non-paged pool */
6379     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6380                                  fp->rx_tpa_info_mbuf_spare_map,
6381                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6382     if (__predict_false(rc != 0)) {
6383         fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6384         m_free(m);
6385         fp->eth_q_stats.mbuf_alloc_tpa--;
6386         return (rc);
6387     }
6388
6389     /* all mbufs must map to a single segment */
6390     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6391
6392     /* release any existing TPA mbuf mapping */
6393     if (tpa_info->bd.m_map != NULL) {
6394         bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6395                         BUS_DMASYNC_POSTREAD);
6396         bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6397     }
6398
6399     /* save the mbuf and mapping info for the TPA mbuf */
6400     map = tpa_info->bd.m_map;
6401     tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6402     fp->rx_tpa_info_mbuf_spare_map = map;
6403     bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6404                     BUS_DMASYNC_PREREAD);
6405     tpa_info->bd.m = m;
6406     tpa_info->seg = segs[0];
6407
6408     return (rc);
6409 }
6410
6411 /*
6412  * Allocate an mbuf and assign it to the receive scatter gather chain. The
6413  * caller must take care to save a copy of the existing mbuf in the SG mbuf
6414  * chain.
6415  */
6416 static int
6417 bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6418                       uint16_t            index)
6419 {
6420     struct bxe_sw_rx_bd *sge_buf;
6421     struct eth_rx_sge *sge;
6422     bus_dma_segment_t segs[1];
6423     bus_dmamap_t map;
6424     struct mbuf *m;
6425     int nsegs;
6426     int rc = 0;
6427
6428     /* allocate a new SGE mbuf */
6429     m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6430     if (__predict_false(m == NULL)) {
6431         fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6432         return (ENOMEM);
6433     }
6434
6435     fp->eth_q_stats.mbuf_alloc_sge++;
6436
6437     /* initialize the mbuf buffer length */
6438     m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6439
6440     /* map the SGE mbuf into non-paged pool */
6441     rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6442                                  fp->rx_sge_mbuf_spare_map,
6443                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6444     if (__predict_false(rc != 0)) {
6445         fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6446         m_freem(m);
6447         fp->eth_q_stats.mbuf_alloc_sge--;
6448         return (rc);
6449     }
6450
6451     /* all mbufs must map to a single segment */
6452     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6453
6454     sge_buf = &fp->rx_sge_mbuf_chain[index];
6455
6456     /* release any existing SGE mbuf mapping */
6457     if (sge_buf->m_map != NULL) {
6458         bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6459                         BUS_DMASYNC_POSTREAD);
6460         bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6461     }
6462
6463     /* save the mbuf and mapping info for a future packet */
6464     map = sge_buf->m_map;
6465     sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6466     fp->rx_sge_mbuf_spare_map = map;
6467     bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6468                     BUS_DMASYNC_PREREAD);
6469     sge_buf->m = m;
6470
6471     sge = &fp->rx_sge_chain[index];
6472     sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6473     sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6474
6475     return (rc);
6476 }
6477
6478 static __noinline int
6479 bxe_alloc_fp_buffers(struct bxe_softc *sc)
6480 {
6481     struct bxe_fastpath *fp;
6482     int i, j, rc = 0;
6483     int ring_prod, cqe_ring_prod;
6484     int max_agg_queues;
6485
6486     for (i = 0; i < sc->num_queues; i++) {
6487         fp = &sc->fp[i];
6488
6489         ring_prod = cqe_ring_prod = 0;
6490         fp->rx_bd_cons = 0;
6491         fp->rx_cq_cons = 0;
6492
6493         /* allocate buffers for the RX BDs in RX BD chain */
6494         for (j = 0; j < sc->max_rx_bufs; j++) {
6495             rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6496             if (rc != 0) {
6497                 BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6498                       i, rc);
6499                 goto bxe_alloc_fp_buffers_error;
6500             }
6501
6502             ring_prod     = RX_BD_NEXT(ring_prod);
6503             cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6504         }
6505
6506         fp->rx_bd_prod = ring_prod;
6507         fp->rx_cq_prod = cqe_ring_prod;
6508         fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6509
6510         max_agg_queues = MAX_AGG_QS(sc);
6511
6512         fp->tpa_enable = TRUE;
6513
6514         /* fill the TPA pool */
6515         for (j = 0; j < max_agg_queues; j++) {
6516             rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6517             if (rc != 0) {
6518                 BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6519                           i, j);
6520                 fp->tpa_enable = FALSE;
6521                 goto bxe_alloc_fp_buffers_error;
6522             }
6523
6524             fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6525         }
6526
6527         if (fp->tpa_enable) {
6528             /* fill the RX SGE chain */
6529             ring_prod = 0;
6530             for (j = 0; j < RX_SGE_USABLE; j++) {
6531                 rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6532                 if (rc != 0) {
6533                     BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6534                               i, ring_prod);
6535                     fp->tpa_enable = FALSE;
6536                     ring_prod = 0;
6537                     goto bxe_alloc_fp_buffers_error;
6538                 }
6539
6540                 ring_prod = RX_SGE_NEXT(ring_prod);
6541             }
6542
6543             fp->rx_sge_prod = ring_prod;
6544         }
6545     }
6546
6547     return (0);
6548
6549 bxe_alloc_fp_buffers_error:
6550
6551     /* unwind what was already allocated */
6552     bxe_free_rx_bd_chain(fp);
6553     bxe_free_tpa_pool(fp);
6554     bxe_free_sge_chain(fp);
6555
6556     return (ENOBUFS);
6557 }
6558
6559 static void
6560 bxe_free_fw_stats_mem(struct bxe_softc *sc)
6561 {
6562     bxe_dma_free(sc, &sc->fw_stats_dma);
6563
6564     sc->fw_stats_num = 0;
6565
6566     sc->fw_stats_req_size = 0;
6567     sc->fw_stats_req = NULL;
6568     sc->fw_stats_req_mapping = 0;
6569
6570     sc->fw_stats_data_size = 0;
6571     sc->fw_stats_data = NULL;
6572     sc->fw_stats_data_mapping = 0;
6573 }
6574
6575 static int
6576 bxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6577 {
6578     uint8_t num_queue_stats;
6579     int num_groups;
6580
6581     /* number of queues for statistics is number of eth queues */
6582     num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6583
6584     /*
6585      * Total number of FW statistics requests =
6586      *   1 for port stats + 1 for PF stats + num of queues
6587      */
6588     sc->fw_stats_num = (2 + num_queue_stats);
6589
6590     /*
6591      * Request is built from stats_query_header and an array of
6592      * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6593      * rules. The real number or requests is configured in the
6594      * stats_query_header.
6595      */
6596     num_groups =
6597         ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6598          ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6599
6600     BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6601           sc->fw_stats_num, num_groups);
6602
6603     sc->fw_stats_req_size =
6604         (sizeof(struct stats_query_header) +
6605          (num_groups * sizeof(struct stats_query_cmd_group)));
6606
6607     /*
6608      * Data for statistics requests + stats_counter.
6609      * stats_counter holds per-STORM counters that are incremented when
6610      * STORM has finished with the current request. Memory for FCoE
6611      * offloaded statistics are counted anyway, even if they will not be sent.
6612      * VF stats are not accounted for here as the data of VF stats is stored
6613      * in memory allocated by the VF, not here.
6614      */
6615     sc->fw_stats_data_size =
6616         (sizeof(struct stats_counter) +
6617          sizeof(struct per_port_stats) +
6618          sizeof(struct per_pf_stats) +
6619          /* sizeof(struct fcoe_statistics_params) + */
6620          (sizeof(struct per_queue_stats) * num_queue_stats));
6621
6622     if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6623                       &sc->fw_stats_dma, "fw stats") != 0) {
6624         bxe_free_fw_stats_mem(sc);
6625         return (-1);
6626     }
6627
6628     /* set up the shortcuts */
6629
6630     sc->fw_stats_req =
6631         (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6632     sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6633
6634     sc->fw_stats_data =
6635         (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6636                                      sc->fw_stats_req_size);
6637     sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6638                                  sc->fw_stats_req_size);
6639
6640     BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6641           (uintmax_t)sc->fw_stats_req_mapping);
6642
6643     BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6644           (uintmax_t)sc->fw_stats_data_mapping);
6645
6646     return (0);
6647 }
6648
6649 /*
6650  * Bits map:
6651  * 0-7  - Engine0 load counter.
6652  * 8-15 - Engine1 load counter.
6653  * 16   - Engine0 RESET_IN_PROGRESS bit.
6654  * 17   - Engine1 RESET_IN_PROGRESS bit.
6655  * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
6656  *        function on the engine
6657  * 19   - Engine1 ONE_IS_LOADED.
6658  * 20   - Chip reset flow bit. When set none-leader must wait for both engines
6659  *        leader to complete (check for both RESET_IN_PROGRESS bits and not
6660  *        for just the one belonging to its engine).
6661  */
6662 #define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
6663 #define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
6664 #define BXE_PATH0_LOAD_CNT_SHIFT  0
6665 #define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
6666 #define BXE_PATH1_LOAD_CNT_SHIFT  8
6667 #define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
6668 #define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
6669 #define BXE_GLOBAL_RESET_BIT      0x00040000
6670
6671 /* set the GLOBAL_RESET bit, should be run under rtnl lock */
6672 static void
6673 bxe_set_reset_global(struct bxe_softc *sc)
6674 {
6675     uint32_t val;
6676     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6677     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6678     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
6679     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6680 }
6681
6682 /* clear the GLOBAL_RESET bit, should be run under rtnl lock */
6683 static void
6684 bxe_clear_reset_global(struct bxe_softc *sc)
6685 {
6686     uint32_t val;
6687     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6688     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6689     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
6690     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6691 }
6692
6693 /* checks the GLOBAL_RESET bit, should be run under rtnl lock */
6694 static uint8_t
6695 bxe_reset_is_global(struct bxe_softc *sc)
6696 {
6697     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6698     BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
6699     return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
6700 }
6701
6702 /* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
6703 static void
6704 bxe_set_reset_done(struct bxe_softc *sc)
6705 {
6706     uint32_t val;
6707     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6708                                  BXE_PATH0_RST_IN_PROG_BIT;
6709
6710     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6711
6712     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6713     /* Clear the bit */
6714     val &= ~bit;
6715     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6716
6717     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6718 }
6719
6720 /* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
6721 static void
6722 bxe_set_reset_in_progress(struct bxe_softc *sc)
6723 {
6724     uint32_t val;
6725     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
6726                                  BXE_PATH0_RST_IN_PROG_BIT;
6727
6728     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6729
6730     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6731     /* Set the bit */
6732     val |= bit;
6733     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6734
6735     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6736 }
6737
6738 /* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
6739 static uint8_t
6740 bxe_reset_is_done(struct bxe_softc *sc,
6741                   int              engine)
6742 {
6743     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6744     uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
6745                             BXE_PATH0_RST_IN_PROG_BIT;
6746
6747     /* return false if bit is set */
6748     return (val & bit) ? FALSE : TRUE;
6749 }
6750
6751 /* get the load status for an engine, should be run under rtnl lock */
6752 static uint8_t
6753 bxe_get_load_status(struct bxe_softc *sc,
6754                     int              engine)
6755 {
6756     uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
6757                              BXE_PATH0_LOAD_CNT_MASK;
6758     uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
6759                               BXE_PATH0_LOAD_CNT_SHIFT;
6760     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6761
6762     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6763
6764     val = ((val & mask) >> shift);
6765
6766     BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
6767
6768     return (val != 0);
6769 }
6770
6771 /* set pf load mark */
6772 /* XXX needs to be under rtnl lock */
6773 static void
6774 bxe_set_pf_load(struct bxe_softc *sc)
6775 {
6776     uint32_t val;
6777     uint32_t val1;
6778     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6779                                   BXE_PATH0_LOAD_CNT_MASK;
6780     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6781                                    BXE_PATH0_LOAD_CNT_SHIFT;
6782
6783     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6784
6785     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6786     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
6787
6788     /* get the current counter value */
6789     val1 = ((val & mask) >> shift);
6790
6791     /* set bit of this PF */
6792     val1 |= (1 << SC_ABS_FUNC(sc));
6793
6794     /* clear the old value */
6795     val &= ~mask;
6796
6797     /* set the new one */
6798     val |= ((val1 << shift) & mask);
6799
6800     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6801
6802     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6803 }
6804
6805 /* clear pf load mark */
6806 /* XXX needs to be under rtnl lock */
6807 static uint8_t
6808 bxe_clear_pf_load(struct bxe_softc *sc)
6809 {
6810     uint32_t val1, val;
6811     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
6812                                   BXE_PATH0_LOAD_CNT_MASK;
6813     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
6814                                    BXE_PATH0_LOAD_CNT_SHIFT;
6815
6816     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6817     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
6818     BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
6819
6820     /* get the current counter value */
6821     val1 = (val & mask) >> shift;
6822
6823     /* clear bit of that PF */
6824     val1 &= ~(1 << SC_ABS_FUNC(sc));
6825
6826     /* clear the old value */
6827     val &= ~mask;
6828
6829     /* set the new one */
6830     val |= ((val1 << shift) & mask);
6831
6832     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
6833     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
6834     return (val1 != 0);
6835 }
6836
6837 /* send load requrest to mcp and analyze response */
6838 static int
6839 bxe_nic_load_request(struct bxe_softc *sc,
6840                      uint32_t         *load_code)
6841 {
6842     /* init fw_seq */
6843     sc->fw_seq =
6844         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
6845          DRV_MSG_SEQ_NUMBER_MASK);
6846
6847     BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
6848
6849     /* get the current FW pulse sequence */
6850     sc->fw_drv_pulse_wr_seq =
6851         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
6852          DRV_PULSE_SEQ_MASK);
6853
6854     BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
6855           sc->fw_drv_pulse_wr_seq);
6856
6857     /* load request */
6858     (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
6859                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
6860
6861     /* if the MCP fails to respond we must abort */
6862     if (!(*load_code)) {
6863         BLOGE(sc, "MCP response failure!\n");
6864         return (-1);
6865     }
6866
6867     /* if MCP refused then must abort */
6868     if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
6869         BLOGE(sc, "MCP refused load request\n");
6870         return (-1);
6871     }
6872
6873     return (0);
6874 }
6875
6876 /*
6877  * Check whether another PF has already loaded FW to chip. In virtualized
6878  * environments a pf from anoth VM may have already initialized the device
6879  * including loading FW.
6880  */
6881 static int
6882 bxe_nic_load_analyze_req(struct bxe_softc *sc,
6883                          uint32_t         load_code)
6884 {
6885     uint32_t my_fw, loaded_fw;
6886
6887     /* is another pf loaded on this engine? */
6888     if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
6889         (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
6890         /* build my FW version dword */
6891         my_fw = (BCM_5710_FW_MAJOR_VERSION +
6892                  (BCM_5710_FW_MINOR_VERSION << 8 ) +
6893                  (BCM_5710_FW_REVISION_VERSION << 16) +
6894                  (BCM_5710_FW_ENGINEERING_VERSION << 24));
6895
6896         /* read loaded FW from chip */
6897         loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
6898         BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
6899               loaded_fw, my_fw);
6900
6901         /* abort nic load if version mismatch */
6902         if (my_fw != loaded_fw) {
6903             BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
6904                   loaded_fw, my_fw);
6905             return (-1);
6906         }
6907     }
6908
6909     return (0);
6910 }
6911
6912 /* mark PMF if applicable */
6913 static void
6914 bxe_nic_load_pmf(struct bxe_softc *sc,
6915                  uint32_t         load_code)
6916 {
6917     uint32_t ncsi_oem_data_addr;
6918
6919     if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
6920         (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
6921         (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
6922         /*
6923          * Barrier here for ordering between the writing to sc->port.pmf here
6924          * and reading it from the periodic task.
6925          */
6926         sc->port.pmf = 1;
6927         mb();
6928     } else {
6929         sc->port.pmf = 0;
6930     }
6931
6932     BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
6933
6934     /* XXX needed? */
6935     if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
6936         if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
6937             ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
6938             if (ncsi_oem_data_addr) {
6939                 REG_WR(sc,
6940                        (ncsi_oem_data_addr +
6941                         offsetof(struct glob_ncsi_oem_data, driver_version)),
6942                        0);
6943             }
6944         }
6945     }
6946 }
6947
6948 static void
6949 bxe_read_mf_cfg(struct bxe_softc *sc)
6950 {
6951     int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
6952     int abs_func;
6953     int vn;
6954
6955     if (BXE_NOMCP(sc)) {
6956         return; /* what should be the default bvalue in this case */
6957     }
6958
6959     /*
6960      * The formula for computing the absolute function number is...
6961      * For 2 port configuration (4 functions per port):
6962      *   abs_func = 2 * vn + SC_PORT + SC_PATH
6963      * For 4 port configuration (2 functions per port):
6964      *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
6965      */
6966     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
6967         abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
6968         if (abs_func >= E1H_FUNC_MAX) {
6969             break;
6970         }
6971         sc->devinfo.mf_info.mf_config[vn] =
6972             MFCFG_RD(sc, func_mf_config[abs_func].config);
6973     }
6974
6975     if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
6976         FUNC_MF_CFG_FUNC_DISABLED) {
6977         BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
6978         sc->flags |= BXE_MF_FUNC_DIS;
6979     } else {
6980         BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
6981         sc->flags &= ~BXE_MF_FUNC_DIS;
6982     }
6983 }
6984
6985 /* acquire split MCP access lock register */
6986 static int bxe_acquire_alr(struct bxe_softc *sc)
6987 {
6988     uint32_t j, val;
6989
6990     for (j = 0; j < 1000; j++) {
6991         val = (1UL << 31);
6992         REG_WR(sc, GRCBASE_MCP + 0x9c, val);
6993         val = REG_RD(sc, GRCBASE_MCP + 0x9c);
6994         if (val & (1L << 31))
6995             break;
6996
6997         DELAY(5000);
6998     }
6999
7000     if (!(val & (1L << 31))) {
7001         BLOGE(sc, "Cannot acquire MCP access lock register\n");
7002         return (-1);
7003     }
7004
7005     return (0);
7006 }
7007
7008 /* release split MCP access lock register */
7009 static void bxe_release_alr(struct bxe_softc *sc)
7010 {
7011     REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
7012 }
7013
7014 static void
7015 bxe_fan_failure(struct bxe_softc *sc)
7016 {
7017     int port = SC_PORT(sc);
7018     uint32_t ext_phy_config;
7019
7020     /* mark the failure */
7021     ext_phy_config =
7022         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7023
7024     ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7025     ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7026     SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7027              ext_phy_config);
7028
7029     /* log the failure */
7030     BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7031               "the card to prevent permanent damage. "
7032               "Please contact OEM Support for assistance\n");
7033
7034     /* XXX */
7035 #if 1
7036     bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7037 #else
7038     /*
7039      * Schedule device reset (unload)
7040      * This is due to some boards consuming sufficient power when driver is
7041      * up to overheat if fan fails.
7042      */
7043     bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7044     schedule_delayed_work(&sc->sp_rtnl_task, 0);
7045 #endif
7046 }
7047
7048 /* this function is called upon a link interrupt */
7049 static void
7050 bxe_link_attn(struct bxe_softc *sc)
7051 {
7052     uint32_t pause_enabled = 0;
7053     struct host_port_stats *pstats;
7054     int cmng_fns;
7055     struct bxe_fastpath *fp;
7056     int i;
7057
7058     /* Make sure that we are synced with the current statistics */
7059     bxe_stats_handle(sc, STATS_EVENT_STOP);
7060     BLOGD(sc, DBG_LOAD, "link_vars phy_flags : %x\n", sc->link_vars.phy_flags);
7061     elink_link_update(&sc->link_params, &sc->link_vars);
7062
7063     if (sc->link_vars.link_up) {
7064
7065         /* dropless flow control */
7066         if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7067             pause_enabled = 0;
7068
7069             if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7070                 pause_enabled = 1;
7071             }
7072
7073             REG_WR(sc,
7074                    (BAR_USTRORM_INTMEM +
7075                     USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7076                    pause_enabled);
7077         }
7078
7079         if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7080             pstats = BXE_SP(sc, port_stats);
7081             /* reset old mac stats */
7082             memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7083         }
7084
7085         if (sc->state == BXE_STATE_OPEN) {
7086             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7087             /* Restart tx when the link comes back. */
7088             FOR_EACH_ETH_QUEUE(sc, i) {
7089                 fp = &sc->fp[i];
7090                 taskqueue_enqueue(fp->tq, &fp->tx_task);
7091             }
7092         }
7093
7094     }
7095
7096     if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7097         cmng_fns = bxe_get_cmng_fns_mode(sc);
7098
7099         if (cmng_fns != CMNG_FNS_NONE) {
7100             bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7101             storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7102         } else {
7103             /* rate shaping and fairness are disabled */
7104             BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7105         }
7106     }
7107
7108     bxe_link_report_locked(sc);
7109
7110     if (IS_MF(sc)) {
7111         ; // XXX bxe_link_sync_notify(sc);
7112     }
7113 }
7114
7115 static void
7116 bxe_attn_int_asserted(struct bxe_softc *sc,
7117                       uint32_t         asserted)
7118 {
7119     int port = SC_PORT(sc);
7120     uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7121                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
7122     uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7123                                         NIG_REG_MASK_INTERRUPT_PORT0;
7124     uint32_t aeu_mask;
7125     uint32_t nig_mask = 0;
7126     uint32_t reg_addr;
7127     uint32_t igu_acked;
7128     uint32_t cnt;
7129
7130     if (sc->attn_state & asserted) {
7131         BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7132     }
7133
7134     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7135
7136     aeu_mask = REG_RD(sc, aeu_addr);
7137
7138     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7139           aeu_mask, asserted);
7140
7141     aeu_mask &= ~(asserted & 0x3ff);
7142
7143     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7144
7145     REG_WR(sc, aeu_addr, aeu_mask);
7146
7147     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7148
7149     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7150     sc->attn_state |= asserted;
7151     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7152
7153     if (asserted & ATTN_HARD_WIRED_MASK) {
7154         if (asserted & ATTN_NIG_FOR_FUNC) {
7155
7156             bxe_acquire_phy_lock(sc);
7157             /* save nig interrupt mask */
7158             nig_mask = REG_RD(sc, nig_int_mask_addr);
7159
7160             /* If nig_mask is not set, no need to call the update function */
7161             if (nig_mask) {
7162                 REG_WR(sc, nig_int_mask_addr, 0);
7163
7164                 bxe_link_attn(sc);
7165             }
7166
7167             /* handle unicore attn? */
7168         }
7169
7170         if (asserted & ATTN_SW_TIMER_4_FUNC) {
7171             BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7172         }
7173
7174         if (asserted & GPIO_2_FUNC) {
7175             BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7176         }
7177
7178         if (asserted & GPIO_3_FUNC) {
7179             BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7180         }
7181
7182         if (asserted & GPIO_4_FUNC) {
7183             BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7184         }
7185
7186         if (port == 0) {
7187             if (asserted & ATTN_GENERAL_ATTN_1) {
7188                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7189                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7190             }
7191             if (asserted & ATTN_GENERAL_ATTN_2) {
7192                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7193                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7194             }
7195             if (asserted & ATTN_GENERAL_ATTN_3) {
7196                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7197                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7198             }
7199         } else {
7200             if (asserted & ATTN_GENERAL_ATTN_4) {
7201                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7202                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7203             }
7204             if (asserted & ATTN_GENERAL_ATTN_5) {
7205                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7206                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7207             }
7208             if (asserted & ATTN_GENERAL_ATTN_6) {
7209                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7210                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7211             }
7212         }
7213     } /* hardwired */
7214
7215     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7216         reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7217     } else {
7218         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7219     }
7220
7221     BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7222           asserted,
7223           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7224     REG_WR(sc, reg_addr, asserted);
7225
7226     /* now set back the mask */
7227     if (asserted & ATTN_NIG_FOR_FUNC) {
7228         /*
7229          * Verify that IGU ack through BAR was written before restoring
7230          * NIG mask. This loop should exit after 2-3 iterations max.
7231          */
7232         if (sc->devinfo.int_block != INT_BLOCK_HC) {
7233             cnt = 0;
7234
7235             do {
7236                 igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7237             } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7238                      (++cnt < MAX_IGU_ATTN_ACK_TO));
7239
7240             if (!igu_acked) {
7241                 BLOGE(sc, "Failed to verify IGU ack on time\n");
7242             }
7243
7244             mb();
7245         }
7246
7247         REG_WR(sc, nig_int_mask_addr, nig_mask);
7248
7249         bxe_release_phy_lock(sc);
7250     }
7251 }
7252
7253 static void
7254 bxe_print_next_block(struct bxe_softc *sc,
7255                      int              idx,
7256                      const char       *blk)
7257 {
7258     BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7259 }
7260
7261 static int
7262 bxe_check_blocks_with_parity0(struct bxe_softc *sc,
7263                               uint32_t         sig,
7264                               int              par_num,
7265                               uint8_t          print)
7266 {
7267     uint32_t cur_bit = 0;
7268     int i = 0;
7269
7270     for (i = 0; sig; i++) {
7271         cur_bit = ((uint32_t)0x1 << i);
7272         if (sig & cur_bit) {
7273             switch (cur_bit) {
7274             case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7275                 if (print)
7276                     bxe_print_next_block(sc, par_num++, "BRB");
7277                 break;
7278             case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7279                 if (print)
7280                     bxe_print_next_block(sc, par_num++, "PARSER");
7281                 break;
7282             case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7283                 if (print)
7284                     bxe_print_next_block(sc, par_num++, "TSDM");
7285                 break;
7286             case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7287                 if (print)
7288                     bxe_print_next_block(sc, par_num++, "SEARCHER");
7289                 break;
7290             case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7291                 if (print)
7292                     bxe_print_next_block(sc, par_num++, "TCM");
7293                 break;
7294             case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7295                 if (print)
7296                     bxe_print_next_block(sc, par_num++, "TSEMI");
7297                 break;
7298             case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7299                 if (print)
7300                     bxe_print_next_block(sc, par_num++, "XPB");
7301                 break;
7302             }
7303
7304             /* Clear the bit */
7305             sig &= ~cur_bit;
7306         }
7307     }
7308
7309     return (par_num);
7310 }
7311
7312 static int
7313 bxe_check_blocks_with_parity1(struct bxe_softc *sc,
7314                               uint32_t         sig,
7315                               int              par_num,
7316                               uint8_t          *global,
7317                               uint8_t          print)
7318 {
7319     int i = 0;
7320     uint32_t cur_bit = 0;
7321     for (i = 0; sig; i++) {
7322         cur_bit = ((uint32_t)0x1 << i);
7323         if (sig & cur_bit) {
7324             switch (cur_bit) {
7325             case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7326                 if (print)
7327                     bxe_print_next_block(sc, par_num++, "PBF");
7328                 break;
7329             case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7330                 if (print)
7331                     bxe_print_next_block(sc, par_num++, "QM");
7332                 break;
7333             case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7334                 if (print)
7335                     bxe_print_next_block(sc, par_num++, "TM");
7336                 break;
7337             case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7338                 if (print)
7339                     bxe_print_next_block(sc, par_num++, "XSDM");
7340                 break;
7341             case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7342                 if (print)
7343                     bxe_print_next_block(sc, par_num++, "XCM");
7344                 break;
7345             case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7346                 if (print)
7347                     bxe_print_next_block(sc, par_num++, "XSEMI");
7348                 break;
7349             case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7350                 if (print)
7351                     bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7352                 break;
7353             case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7354                 if (print)
7355                     bxe_print_next_block(sc, par_num++, "NIG");
7356                 break;
7357             case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7358                 if (print)
7359                     bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7360                 *global = TRUE;
7361                 break;
7362             case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7363                 if (print)
7364                     bxe_print_next_block(sc, par_num++, "DEBUG");
7365                 break;
7366             case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7367                 if (print)
7368                     bxe_print_next_block(sc, par_num++, "USDM");
7369                 break;
7370             case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7371                 if (print)
7372                     bxe_print_next_block(sc, par_num++, "UCM");
7373                 break;
7374             case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7375                 if (print)
7376                     bxe_print_next_block(sc, par_num++, "USEMI");
7377                 break;
7378             case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7379                 if (print)
7380                     bxe_print_next_block(sc, par_num++, "UPB");
7381                 break;
7382             case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7383                 if (print)
7384                     bxe_print_next_block(sc, par_num++, "CSDM");
7385                 break;
7386             case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7387                 if (print)
7388                     bxe_print_next_block(sc, par_num++, "CCM");
7389                 break;
7390             }
7391
7392             /* Clear the bit */
7393             sig &= ~cur_bit;
7394         }
7395     }
7396
7397     return (par_num);
7398 }
7399
7400 static int
7401 bxe_check_blocks_with_parity2(struct bxe_softc *sc,
7402                               uint32_t         sig,
7403                               int              par_num,
7404                               uint8_t          print)
7405 {
7406     uint32_t cur_bit = 0;
7407     int i = 0;
7408
7409     for (i = 0; sig; i++) {
7410         cur_bit = ((uint32_t)0x1 << i);
7411         if (sig & cur_bit) {
7412             switch (cur_bit) {
7413             case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7414                 if (print)
7415                     bxe_print_next_block(sc, par_num++, "CSEMI");
7416                 break;
7417             case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7418                 if (print)
7419                     bxe_print_next_block(sc, par_num++, "PXP");
7420                 break;
7421             case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7422                 if (print)
7423                     bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7424                 break;
7425             case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7426                 if (print)
7427                     bxe_print_next_block(sc, par_num++, "CFC");
7428                 break;
7429             case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7430                 if (print)
7431                     bxe_print_next_block(sc, par_num++, "CDU");
7432                 break;
7433             case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7434                 if (print)
7435                     bxe_print_next_block(sc, par_num++, "DMAE");
7436                 break;
7437             case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7438                 if (print)
7439                     bxe_print_next_block(sc, par_num++, "IGU");
7440                 break;
7441             case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7442                 if (print)
7443                     bxe_print_next_block(sc, par_num++, "MISC");
7444                 break;
7445             }
7446
7447             /* Clear the bit */
7448             sig &= ~cur_bit;
7449         }
7450     }
7451
7452     return (par_num);
7453 }
7454
7455 static int
7456 bxe_check_blocks_with_parity3(struct bxe_softc *sc,
7457                               uint32_t         sig,
7458                               int              par_num,
7459                               uint8_t          *global,
7460                               uint8_t          print)
7461 {
7462     uint32_t cur_bit = 0;
7463     int i = 0;
7464
7465     for (i = 0; sig; i++) {
7466         cur_bit = ((uint32_t)0x1 << i);
7467         if (sig & cur_bit) {
7468             switch (cur_bit) {
7469             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7470                 if (print)
7471                     bxe_print_next_block(sc, par_num++, "MCP ROM");
7472                 *global = TRUE;
7473                 break;
7474             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7475                 if (print)
7476                     bxe_print_next_block(sc, par_num++,
7477                               "MCP UMP RX");
7478                 *global = TRUE;
7479                 break;
7480             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7481                 if (print)
7482                     bxe_print_next_block(sc, par_num++,
7483                               "MCP UMP TX");
7484                 *global = TRUE;
7485                 break;
7486             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7487                 if (print)
7488                     bxe_print_next_block(sc, par_num++,
7489                               "MCP SCPAD");
7490                 *global = TRUE;
7491                 break;
7492             }
7493
7494             /* Clear the bit */
7495             sig &= ~cur_bit;
7496         }
7497     }
7498
7499     return (par_num);
7500 }
7501
7502 static int
7503 bxe_check_blocks_with_parity4(struct bxe_softc *sc,
7504                               uint32_t         sig,
7505                               int              par_num,
7506                               uint8_t          print)
7507 {
7508     uint32_t cur_bit = 0;
7509     int i = 0;
7510
7511     for (i = 0; sig; i++) {
7512         cur_bit = ((uint32_t)0x1 << i);
7513         if (sig & cur_bit) {
7514             switch (cur_bit) {
7515             case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7516                 if (print)
7517                     bxe_print_next_block(sc, par_num++, "PGLUE_B");
7518                 break;
7519             case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7520                 if (print)
7521                     bxe_print_next_block(sc, par_num++, "ATC");
7522                 break;
7523             }
7524
7525             /* Clear the bit */
7526             sig &= ~cur_bit;
7527         }
7528     }
7529
7530     return (par_num);
7531 }
7532
7533 static uint8_t
7534 bxe_parity_attn(struct bxe_softc *sc,
7535                 uint8_t          *global,
7536                 uint8_t          print,
7537                 uint32_t         *sig)
7538 {
7539     int par_num = 0;
7540
7541     if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7542         (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7543         (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7544         (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7545         (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7546         BLOGE(sc, "Parity error: HW block parity attention:\n"
7547                   "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7548               (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7549               (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7550               (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7551               (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7552               (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7553
7554         if (print)
7555             BLOGI(sc, "Parity errors detected in blocks: ");
7556
7557         par_num =
7558             bxe_check_blocks_with_parity0(sc, sig[0] &
7559                                           HW_PRTY_ASSERT_SET_0,
7560                                           par_num, print);
7561         par_num =
7562             bxe_check_blocks_with_parity1(sc, sig[1] &
7563                                           HW_PRTY_ASSERT_SET_1,
7564                                           par_num, global, print);
7565         par_num =
7566             bxe_check_blocks_with_parity2(sc, sig[2] &
7567                                           HW_PRTY_ASSERT_SET_2,
7568                                           par_num, print);
7569         par_num =
7570             bxe_check_blocks_with_parity3(sc, sig[3] &
7571                                           HW_PRTY_ASSERT_SET_3,
7572                                           par_num, global, print);
7573         par_num =
7574             bxe_check_blocks_with_parity4(sc, sig[4] &
7575                                           HW_PRTY_ASSERT_SET_4,
7576                                           par_num, print);
7577
7578         if (print)
7579             BLOGI(sc, "\n");
7580
7581         if( *global == TRUE ) {
7582                 BXE_SET_ERROR_BIT(sc, BXE_ERR_GLOBAL);
7583         }
7584
7585         return (TRUE);
7586     }
7587
7588     return (FALSE);
7589 }
7590
7591 static uint8_t
7592 bxe_chk_parity_attn(struct bxe_softc *sc,
7593                     uint8_t          *global,
7594                     uint8_t          print)
7595 {
7596     struct attn_route attn = { {0} };
7597     int port = SC_PORT(sc);
7598
7599     if(sc->state != BXE_STATE_OPEN)
7600         return FALSE;
7601
7602     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7603     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7604     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7605     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7606
7607     /*
7608      * Since MCP attentions can't be disabled inside the block, we need to
7609      * read AEU registers to see whether they're currently disabled
7610      */
7611     attn.sig[3] &= ((REG_RD(sc, (!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
7612                                       : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0)) &
7613                          MISC_AEU_ENABLE_MCP_PRTY_BITS) |
7614                         ~MISC_AEU_ENABLE_MCP_PRTY_BITS);
7615
7616
7617     if (!CHIP_IS_E1x(sc))
7618         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7619
7620     return (bxe_parity_attn(sc, global, print, attn.sig));
7621 }
7622
7623 static void
7624 bxe_attn_int_deasserted4(struct bxe_softc *sc,
7625                          uint32_t         attn)
7626 {
7627     uint32_t val;
7628     boolean_t err_flg = FALSE;
7629
7630     if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7631         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7632         BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7633         err_flg = TRUE;
7634         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7635             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7636         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7637             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7638         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7639             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7640         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7641             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7642         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7643             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7644         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7645             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7646         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7647             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7648         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7649             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7650         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7651             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7652     }
7653
7654     if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7655         val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7656         BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7657         err_flg = TRUE;
7658         if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7659             BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7660         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7661             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7662         if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7663             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7664         if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7665             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7666         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7667             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7668         if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7669             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7670     }
7671
7672     if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7673                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
7674         BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
7675               (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7676                                  AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
7677         err_flg = TRUE;
7678     }
7679     if (err_flg) {
7680         BXE_SET_ERROR_BIT(sc, BXE_ERR_MISC);
7681         taskqueue_enqueue_timeout(taskqueue_thread,
7682             &sc->sp_err_timeout_task, hz/10);
7683     }
7684
7685 }
7686
7687 static void
7688 bxe_e1h_disable(struct bxe_softc *sc)
7689 {
7690     int port = SC_PORT(sc);
7691
7692     bxe_tx_disable(sc);
7693
7694     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7695 }
7696
7697 static void
7698 bxe_e1h_enable(struct bxe_softc *sc)
7699 {
7700     int port = SC_PORT(sc);
7701
7702     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
7703
7704     // XXX bxe_tx_enable(sc);
7705 }
7706
7707 /*
7708  * called due to MCP event (on pmf):
7709  *   reread new bandwidth configuration
7710  *   configure FW
7711  *   notify others function about the change
7712  */
7713 static void
7714 bxe_config_mf_bw(struct bxe_softc *sc)
7715 {
7716     if (sc->link_vars.link_up) {
7717         bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
7718         // XXX bxe_link_sync_notify(sc);
7719     }
7720
7721     storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7722 }
7723
7724 static void
7725 bxe_set_mf_bw(struct bxe_softc *sc)
7726 {
7727     bxe_config_mf_bw(sc);
7728     bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
7729 }
7730
7731 static void
7732 bxe_handle_eee_event(struct bxe_softc *sc)
7733 {
7734     BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
7735     bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
7736 }
7737
7738 #define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
7739
7740 static void
7741 bxe_drv_info_ether_stat(struct bxe_softc *sc)
7742 {
7743     struct eth_stats_info *ether_stat =
7744         &sc->sp->drv_info_to_mcp.ether_stat;
7745
7746     strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
7747             ETH_STAT_INFO_VERSION_LEN);
7748
7749     /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
7750     sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
7751                                           DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
7752                                           ether_stat->mac_local + MAC_PAD,
7753                                           MAC_PAD, ETH_ALEN);
7754
7755     ether_stat->mtu_size = sc->mtu;
7756
7757     ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
7758     if (sc->ifnet->if_capenable & (IFCAP_TSO4 | IFCAP_TSO6)) {
7759         ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
7760     }
7761
7762     // XXX ether_stat->feature_flags |= ???;
7763
7764     ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
7765
7766     ether_stat->txq_size = sc->tx_ring_size;
7767     ether_stat->rxq_size = sc->rx_ring_size;
7768 }
7769
7770 static void
7771 bxe_handle_drv_info_req(struct bxe_softc *sc)
7772 {
7773     enum drv_info_opcode op_code;
7774     uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
7775
7776     /* if drv_info version supported by MFW doesn't match - send NACK */
7777     if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
7778         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7779         return;
7780     }
7781
7782     op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
7783                DRV_INFO_CONTROL_OP_CODE_SHIFT);
7784
7785     memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
7786
7787     switch (op_code) {
7788     case ETH_STATS_OPCODE:
7789         bxe_drv_info_ether_stat(sc);
7790         break;
7791     case FCOE_STATS_OPCODE:
7792     case ISCSI_STATS_OPCODE:
7793     default:
7794         /* if op code isn't supported - send NACK */
7795         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
7796         return;
7797     }
7798
7799     /*
7800      * If we got drv_info attn from MFW then these fields are defined in
7801      * shmem2 for sure
7802      */
7803     SHMEM2_WR(sc, drv_info_host_addr_lo,
7804               U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7805     SHMEM2_WR(sc, drv_info_host_addr_hi,
7806               U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
7807
7808     bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
7809 }
7810
7811 static void
7812 bxe_dcc_event(struct bxe_softc *sc,
7813               uint32_t         dcc_event)
7814 {
7815     BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
7816
7817     if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
7818         /*
7819          * This is the only place besides the function initialization
7820          * where the sc->flags can change so it is done without any
7821          * locks
7822          */
7823         if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
7824             BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
7825             sc->flags |= BXE_MF_FUNC_DIS;
7826             bxe_e1h_disable(sc);
7827         } else {
7828             BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
7829             sc->flags &= ~BXE_MF_FUNC_DIS;
7830             bxe_e1h_enable(sc);
7831         }
7832         dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
7833     }
7834
7835     if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
7836         bxe_config_mf_bw(sc);
7837         dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
7838     }
7839
7840     /* Report results to MCP */
7841     if (dcc_event)
7842         bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
7843     else
7844         bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
7845 }
7846
7847 static void
7848 bxe_pmf_update(struct bxe_softc *sc)
7849 {
7850     int port = SC_PORT(sc);
7851     uint32_t val;
7852
7853     sc->port.pmf = 1;
7854     BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
7855
7856     /*
7857      * We need the mb() to ensure the ordering between the writing to
7858      * sc->port.pmf here and reading it from the bxe_periodic_task().
7859      */
7860     mb();
7861
7862     /* queue a periodic task */
7863     // XXX schedule task...
7864
7865     // XXX bxe_dcbx_pmf_update(sc);
7866
7867     /* enable nig attention */
7868     val = (0xff0f | (1 << (SC_VN(sc) + 4)));
7869     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7870         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
7871         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
7872     } else if (!CHIP_IS_E1x(sc)) {
7873         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
7874         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
7875     }
7876
7877     bxe_stats_handle(sc, STATS_EVENT_PMF);
7878 }
7879
7880 static int
7881 bxe_mc_assert(struct bxe_softc *sc)
7882 {
7883     char last_idx;
7884     int i, rc = 0;
7885     uint32_t row0, row1, row2, row3;
7886
7887     /* XSTORM */
7888     last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
7889     if (last_idx)
7890         BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7891
7892     /* print the asserts */
7893     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7894
7895         row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
7896         row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
7897         row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
7898         row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
7899
7900         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7901             BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
7902                   i, row3, row2, row1, row0);
7903             rc++;
7904         } else {
7905             break;
7906         }
7907     }
7908
7909     /* TSTORM */
7910     last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
7911     if (last_idx) {
7912         BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
7913     }
7914
7915     /* print the asserts */
7916     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
7917
7918         row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
7919         row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
7920         row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
7921         row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
7922
7923         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7924             BLOGE(sc, "TSTORM_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     /* CSTORM */
7933     last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
7934     if (last_idx) {
7935         BLOGE(sc, "CSTORM_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_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
7942         row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
7943         row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
7944         row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
7945
7946         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7947             BLOGE(sc, "CSTORM_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     /* USTORM */
7956     last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
7957     if (last_idx) {
7958         BLOGE(sc, "USTORM_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_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
7965         row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
7966         row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
7967         row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
7968
7969         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
7970             BLOGE(sc, "USTORM_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     return (rc);
7979 }
7980
7981 static void
7982 bxe_attn_int_deasserted3(struct bxe_softc *sc,
7983                          uint32_t         attn)
7984 {
7985     int func = SC_FUNC(sc);
7986     uint32_t val;
7987
7988     if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
7989
7990         if (attn & BXE_PMF_LINK_ASSERT(sc)) {
7991
7992             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
7993             bxe_read_mf_cfg(sc);
7994             sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
7995                 MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
7996             val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
7997
7998             if (val & DRV_STATUS_DCC_EVENT_MASK)
7999                 bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
8000
8001             if (val & DRV_STATUS_SET_MF_BW)
8002                 bxe_set_mf_bw(sc);
8003
8004             if (val & DRV_STATUS_DRV_INFO_REQ)
8005                 bxe_handle_drv_info_req(sc);
8006
8007             if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
8008                 bxe_pmf_update(sc);
8009
8010             if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
8011                 bxe_handle_eee_event(sc);
8012
8013             if (sc->link_vars.periodic_flags &
8014                 ELINK_PERIODIC_FLAGS_LINK_EVENT) {
8015                 /* sync with link */
8016                 bxe_acquire_phy_lock(sc);
8017                 sc->link_vars.periodic_flags &=
8018                     ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
8019                 bxe_release_phy_lock(sc);
8020                 if (IS_MF(sc))
8021                     ; // XXX bxe_link_sync_notify(sc);
8022                 bxe_link_report(sc);
8023             }
8024
8025             /*
8026              * Always call it here: bxe_link_report() will
8027              * prevent the link indication duplication.
8028              */
8029             bxe_link_status_update(sc);
8030
8031         } else if (attn & BXE_MC_ASSERT_BITS) {
8032
8033             BLOGE(sc, "MC assert!\n");
8034             bxe_mc_assert(sc);
8035             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
8036             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
8037             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
8038             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
8039             bxe_int_disable(sc);
8040             BXE_SET_ERROR_BIT(sc, BXE_ERR_MC_ASSERT);
8041             taskqueue_enqueue_timeout(taskqueue_thread,
8042                 &sc->sp_err_timeout_task, hz/10);
8043         
8044         } else if (attn & BXE_MCP_ASSERT) {
8045
8046             BLOGE(sc, "MCP assert!\n");
8047             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8048             BXE_SET_ERROR_BIT(sc, BXE_ERR_MCP_ASSERT);
8049             taskqueue_enqueue_timeout(taskqueue_thread,
8050                 &sc->sp_err_timeout_task, hz/10);
8051             bxe_int_disable(sc);  /*avoid repetive assert alert */
8052
8053
8054         } else {
8055             BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8056         }
8057     }
8058
8059     if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8060         BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8061         if (attn & BXE_GRC_TIMEOUT) {
8062             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8063             BLOGE(sc, "GRC time-out 0x%08x\n", val);
8064         }
8065         if (attn & BXE_GRC_RSV) {
8066             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8067             BLOGE(sc, "GRC reserved 0x%08x\n", val);
8068         }
8069         REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8070     }
8071 }
8072
8073 static void
8074 bxe_attn_int_deasserted2(struct bxe_softc *sc,
8075                          uint32_t         attn)
8076 {
8077     int port = SC_PORT(sc);
8078     int reg_offset;
8079     uint32_t val0, mask0, val1, mask1;
8080     uint32_t val;
8081     boolean_t err_flg = FALSE;
8082
8083     if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8084         val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8085         BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8086         /* CFC error attention */
8087         if (val & 0x2) {
8088             BLOGE(sc, "FATAL error from CFC\n");
8089             err_flg = TRUE;
8090         }
8091     }
8092
8093     if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8094         val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8095         BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8096         /* RQ_USDMDP_FIFO_OVERFLOW */
8097         if (val & 0x18000) {
8098             BLOGE(sc, "FATAL error from PXP\n");
8099             err_flg = TRUE;
8100         }
8101
8102         if (!CHIP_IS_E1x(sc)) {
8103             val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8104             BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8105             err_flg = TRUE;
8106         }
8107     }
8108
8109 #define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8110 #define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8111
8112     if (attn & AEU_PXP2_HW_INT_BIT) {
8113         /*  CQ47854 workaround do not panic on
8114          *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8115          */
8116         if (!CHIP_IS_E1x(sc)) {
8117             mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8118             val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8119             mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8120             val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8121             /*
8122              * If the olny PXP2_EOP_ERROR_BIT is set in
8123              * STS0 and STS1 - clear it
8124              *
8125              * probably we lose additional attentions between
8126              * STS0 and STS_CLR0, in this case user will not
8127              * be notified about them
8128              */
8129             if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8130                 !(val1 & mask1))
8131                 val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8132
8133             /* print the register, since no one can restore it */
8134             BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8135
8136             /*
8137              * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8138              * then notify
8139              */
8140             if (val0 & PXP2_EOP_ERROR_BIT) {
8141                 BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8142                 err_flg = TRUE;
8143
8144                 /*
8145                  * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8146                  * set then clear attention from PXP2 block without panic
8147                  */
8148                 if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8149                     ((val1 & mask1) == 0))
8150                     attn &= ~AEU_PXP2_HW_INT_BIT;
8151             }
8152         }
8153     }
8154
8155     if (attn & HW_INTERRUT_ASSERT_SET_2) {
8156         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8157                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8158
8159         val = REG_RD(sc, reg_offset);
8160         val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8161         REG_WR(sc, reg_offset, val);
8162
8163         BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8164               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8165         err_flg = TRUE;
8166         bxe_panic(sc, ("HW block attention set2\n"));
8167     }
8168     if(err_flg) {
8169         BXE_SET_ERROR_BIT(sc, BXE_ERR_GLOBAL);
8170         taskqueue_enqueue_timeout(taskqueue_thread,
8171            &sc->sp_err_timeout_task, hz/10);
8172     }
8173
8174 }
8175
8176 static void
8177 bxe_attn_int_deasserted1(struct bxe_softc *sc,
8178                          uint32_t         attn)
8179 {
8180     int port = SC_PORT(sc);
8181     int reg_offset;
8182     uint32_t val;
8183     boolean_t err_flg = FALSE;
8184
8185     if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8186         val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8187         BLOGE(sc, "DB hw attention 0x%08x\n", val);
8188         /* DORQ discard attention */
8189         if (val & 0x2) {
8190             BLOGE(sc, "FATAL error from DORQ\n");
8191             err_flg = TRUE;
8192         }
8193     }
8194
8195     if (attn & HW_INTERRUT_ASSERT_SET_1) {
8196         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8197                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8198
8199         val = REG_RD(sc, reg_offset);
8200         val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8201         REG_WR(sc, reg_offset, val);
8202
8203         BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8204               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8205         err_flg = TRUE;
8206         bxe_panic(sc, ("HW block attention set1\n"));
8207     }
8208     if(err_flg) {
8209         BXE_SET_ERROR_BIT(sc, BXE_ERR_MISC);
8210         taskqueue_enqueue_timeout(taskqueue_thread,
8211            &sc->sp_err_timeout_task, hz/10);
8212     }
8213
8214 }
8215
8216 static void
8217 bxe_attn_int_deasserted0(struct bxe_softc *sc,
8218                          uint32_t         attn)
8219 {
8220     int port = SC_PORT(sc);
8221     int reg_offset;
8222     uint32_t val;
8223
8224     reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8225                           MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8226
8227     if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8228         val = REG_RD(sc, reg_offset);
8229         val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8230         REG_WR(sc, reg_offset, val);
8231
8232         BLOGW(sc, "SPIO5 hw attention\n");
8233
8234         /* Fan failure attention */
8235         elink_hw_reset_phy(&sc->link_params);
8236         bxe_fan_failure(sc);
8237     }
8238
8239     if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8240         bxe_acquire_phy_lock(sc);
8241         elink_handle_module_detect_int(&sc->link_params);
8242         bxe_release_phy_lock(sc);
8243     }
8244
8245     if (attn & HW_INTERRUT_ASSERT_SET_0) {
8246         val = REG_RD(sc, reg_offset);
8247         val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8248         REG_WR(sc, reg_offset, val);
8249
8250
8251         BXE_SET_ERROR_BIT(sc, BXE_ERR_MISC);
8252         taskqueue_enqueue_timeout(taskqueue_thread,
8253            &sc->sp_err_timeout_task, hz/10);
8254
8255         bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8256                        (attn & HW_INTERRUT_ASSERT_SET_0)));
8257     }
8258 }
8259
8260 static void
8261 bxe_attn_int_deasserted(struct bxe_softc *sc,
8262                         uint32_t         deasserted)
8263 {
8264     struct attn_route attn;
8265     struct attn_route *group_mask;
8266     int port = SC_PORT(sc);
8267     int index;
8268     uint32_t reg_addr;
8269     uint32_t val;
8270     uint32_t aeu_mask;
8271     uint8_t global = FALSE;
8272
8273     /*
8274      * Need to take HW lock because MCP or other port might also
8275      * try to handle this event.
8276      */
8277     bxe_acquire_alr(sc);
8278
8279     if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8280         /* XXX
8281          * In case of parity errors don't handle attentions so that
8282          * other function would "see" parity errors.
8283          */
8284         // XXX schedule a recovery task...
8285         /* disable HW interrupts */
8286         bxe_int_disable(sc);
8287         BXE_SET_ERROR_BIT(sc, BXE_ERR_PARITY);
8288         taskqueue_enqueue_timeout(taskqueue_thread,
8289            &sc->sp_err_timeout_task, hz/10);
8290         bxe_release_alr(sc);
8291         return;
8292     }
8293
8294     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8295     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8296     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8297     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8298     if (!CHIP_IS_E1x(sc)) {
8299         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8300     } else {
8301         attn.sig[4] = 0;
8302     }
8303
8304     BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8305           attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8306
8307     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8308         if (deasserted & (1 << index)) {
8309             group_mask = &sc->attn_group[index];
8310
8311             BLOGD(sc, DBG_INTR,
8312                   "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8313                   group_mask->sig[0], group_mask->sig[1],
8314                   group_mask->sig[2], group_mask->sig[3],
8315                   group_mask->sig[4]);
8316
8317             bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8318             bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8319             bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8320             bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8321             bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8322         }
8323     }
8324
8325     bxe_release_alr(sc);
8326
8327     if (sc->devinfo.int_block == INT_BLOCK_HC) {
8328         reg_addr = (HC_REG_COMMAND_REG + port*32 +
8329                     COMMAND_REG_ATTN_BITS_CLR);
8330     } else {
8331         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8332     }
8333
8334     val = ~deasserted;
8335     BLOGD(sc, DBG_INTR,
8336           "about to mask 0x%08x at %s addr 0x%08x\n", val,
8337           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8338     REG_WR(sc, reg_addr, val);
8339
8340     if (~sc->attn_state & deasserted) {
8341         BLOGE(sc, "IGU error\n");
8342     }
8343
8344     reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8345                       MISC_REG_AEU_MASK_ATTN_FUNC_0;
8346
8347     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8348
8349     aeu_mask = REG_RD(sc, reg_addr);
8350
8351     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8352           aeu_mask, deasserted);
8353     aeu_mask |= (deasserted & 0x3ff);
8354     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8355
8356     REG_WR(sc, reg_addr, aeu_mask);
8357     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8358
8359     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8360     sc->attn_state &= ~deasserted;
8361     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8362 }
8363
8364 static void
8365 bxe_attn_int(struct bxe_softc *sc)
8366 {
8367     /* read local copy of bits */
8368     uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8369     uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8370     uint32_t attn_state = sc->attn_state;
8371
8372     /* look for changed bits */
8373     uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8374     uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8375
8376     BLOGD(sc, DBG_INTR,
8377           "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8378           attn_bits, attn_ack, asserted, deasserted);
8379
8380     if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8381         BLOGE(sc, "BAD attention state\n");
8382     }
8383
8384     /* handle bits that were raised */
8385     if (asserted) {
8386         bxe_attn_int_asserted(sc, asserted);
8387     }
8388
8389     if (deasserted) {
8390         bxe_attn_int_deasserted(sc, deasserted);
8391     }
8392 }
8393
8394 static uint16_t
8395 bxe_update_dsb_idx(struct bxe_softc *sc)
8396 {
8397     struct host_sp_status_block *def_sb = sc->def_sb;
8398     uint16_t rc = 0;
8399
8400     mb(); /* status block is written to by the chip */
8401
8402     if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8403         sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8404         rc |= BXE_DEF_SB_ATT_IDX;
8405     }
8406
8407     if (sc->def_idx != def_sb->sp_sb.running_index) {
8408         sc->def_idx = def_sb->sp_sb.running_index;
8409         rc |= BXE_DEF_SB_IDX;
8410     }
8411
8412     mb();
8413
8414     return (rc);
8415 }
8416
8417 static inline struct ecore_queue_sp_obj *
8418 bxe_cid_to_q_obj(struct bxe_softc *sc,
8419                  uint32_t         cid)
8420 {
8421     BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8422     return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8423 }
8424
8425 static void
8426 bxe_handle_mcast_eqe(struct bxe_softc *sc)
8427 {
8428     struct ecore_mcast_ramrod_params rparam;
8429     int rc;
8430
8431     memset(&rparam, 0, sizeof(rparam));
8432
8433     rparam.mcast_obj = &sc->mcast_obj;
8434
8435     BXE_MCAST_LOCK(sc);
8436
8437     /* clear pending state for the last command */
8438     sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8439
8440     /* if there are pending mcast commands - send them */
8441     if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8442         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8443         if (rc < 0) {
8444             BLOGD(sc, DBG_SP,
8445                 "ERROR: Failed to send pending mcast commands (%d)\n", rc);
8446         }
8447     }
8448
8449     BXE_MCAST_UNLOCK(sc);
8450 }
8451
8452 static void
8453 bxe_handle_classification_eqe(struct bxe_softc      *sc,
8454                               union event_ring_elem *elem)
8455 {
8456     unsigned long ramrod_flags = 0;
8457     int rc = 0;
8458     uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8459     struct ecore_vlan_mac_obj *vlan_mac_obj;
8460
8461     /* always push next commands out, don't wait here */
8462     bit_set(&ramrod_flags, RAMROD_CONT);
8463
8464     switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8465     case ECORE_FILTER_MAC_PENDING:
8466         BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8467         vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8468         break;
8469
8470     case ECORE_FILTER_MCAST_PENDING:
8471         BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8472         /*
8473          * This is only relevant for 57710 where multicast MACs are
8474          * configured as unicast MACs using the same ramrod.
8475          */
8476         bxe_handle_mcast_eqe(sc);
8477         return;
8478
8479     default:
8480         BLOGE(sc, "Unsupported classification command: %d\n",
8481               elem->message.data.eth_event.echo);
8482         return;
8483     }
8484
8485     rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8486
8487     if (rc < 0) {
8488         BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8489     } else if (rc > 0) {
8490         BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8491     }
8492 }
8493
8494 static void
8495 bxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8496                        union event_ring_elem *elem)
8497 {
8498     bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8499
8500     /* send rx_mode command again if was requested */
8501     if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8502                                &sc->sp_state)) {
8503         bxe_set_storm_rx_mode(sc);
8504     }
8505 }
8506
8507 static void
8508 bxe_update_eq_prod(struct bxe_softc *sc,
8509                    uint16_t         prod)
8510 {
8511     storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8512     wmb(); /* keep prod updates ordered */
8513 }
8514
8515 static void
8516 bxe_eq_int(struct bxe_softc *sc)
8517 {
8518     uint16_t hw_cons, sw_cons, sw_prod;
8519     union event_ring_elem *elem;
8520     uint8_t echo;
8521     uint32_t cid;
8522     uint8_t opcode;
8523     int spqe_cnt = 0;
8524     struct ecore_queue_sp_obj *q_obj;
8525     struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8526     struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8527
8528     hw_cons = le16toh(*sc->eq_cons_sb);
8529
8530     /*
8531      * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8532      * when we get to the next-page we need to adjust so the loop
8533      * condition below will be met. The next element is the size of a
8534      * regular element and hence incrementing by 1
8535      */
8536     if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8537         hw_cons++;
8538     }
8539
8540     /*
8541      * This function may never run in parallel with itself for a
8542      * specific sc and no need for a read memory barrier here.
8543      */
8544     sw_cons = sc->eq_cons;
8545     sw_prod = sc->eq_prod;
8546
8547     BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8548           hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8549
8550     for (;
8551          sw_cons != hw_cons;
8552          sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8553
8554         elem = &sc->eq[EQ_DESC(sw_cons)];
8555
8556         /* elem CID originates from FW, actually LE */
8557         cid = SW_CID(elem->message.data.cfc_del_event.cid);
8558         opcode = elem->message.opcode;
8559
8560         /* handle eq element */
8561         switch (opcode) {
8562
8563         case EVENT_RING_OPCODE_STAT_QUERY:
8564             BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8565                   sc->stats_comp++);
8566             /* nothing to do with stats comp */
8567             goto next_spqe;
8568
8569         case EVENT_RING_OPCODE_CFC_DEL:
8570             /* handle according to cid range */
8571             /* we may want to verify here that the sc state is HALTING */
8572             BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8573             q_obj = bxe_cid_to_q_obj(sc, cid);
8574             if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8575                 break;
8576             }
8577             goto next_spqe;
8578
8579         case EVENT_RING_OPCODE_STOP_TRAFFIC:
8580             BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8581             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8582                 break;
8583             }
8584             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8585             goto next_spqe;
8586
8587         case EVENT_RING_OPCODE_START_TRAFFIC:
8588             BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8589             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8590                 break;
8591             }
8592             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8593             goto next_spqe;
8594
8595         case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8596             echo = elem->message.data.function_update_event.echo;
8597             if (echo == SWITCH_UPDATE) {
8598                 BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8599                 if (f_obj->complete_cmd(sc, f_obj,
8600                                         ECORE_F_CMD_SWITCH_UPDATE)) {
8601                     break;
8602                 }
8603             }
8604             else {
8605                 BLOGD(sc, DBG_SP,
8606                       "AFEX: ramrod completed FUNCTION_UPDATE\n");
8607             }
8608             goto next_spqe;
8609
8610         case EVENT_RING_OPCODE_FORWARD_SETUP:
8611             q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8612             if (q_obj->complete_cmd(sc, q_obj,
8613                                     ECORE_Q_CMD_SETUP_TX_ONLY)) {
8614                 break;
8615             }
8616             goto next_spqe;
8617
8618         case EVENT_RING_OPCODE_FUNCTION_START:
8619             BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8620             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8621                 break;
8622             }
8623             goto next_spqe;
8624
8625         case EVENT_RING_OPCODE_FUNCTION_STOP:
8626             BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8627             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8628                 break;
8629             }
8630             goto next_spqe;
8631         }
8632
8633         switch (opcode | sc->state) {
8634         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8635         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8636             cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8637             BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8638             rss_raw->clear_pending(rss_raw);
8639             break;
8640
8641         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8642         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8643         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8644         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8645         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8646         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8647             BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8648             bxe_handle_classification_eqe(sc, elem);
8649             break;
8650
8651         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8652         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8653         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8654             BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8655             bxe_handle_mcast_eqe(sc);
8656             break;
8657
8658         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
8659         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
8660         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8661             BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
8662             bxe_handle_rx_mode_eqe(sc, elem);
8663             break;
8664
8665         default:
8666             /* unknown event log error and continue */
8667             BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
8668                   elem->message.opcode, sc->state);
8669         }
8670
8671 next_spqe:
8672         spqe_cnt++;
8673     } /* for */
8674
8675     mb();
8676     atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
8677
8678     sc->eq_cons = sw_cons;
8679     sc->eq_prod = sw_prod;
8680
8681     /* make sure that above mem writes were issued towards the memory */
8682     wmb();
8683
8684     /* update producer */
8685     bxe_update_eq_prod(sc, sc->eq_prod);
8686 }
8687
8688 static void
8689 bxe_handle_sp_tq(void *context,
8690                  int  pending)
8691 {
8692     struct bxe_softc *sc = (struct bxe_softc *)context;
8693     uint16_t status;
8694
8695     BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
8696
8697     /* what work needs to be performed? */
8698     status = bxe_update_dsb_idx(sc);
8699
8700     BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
8701
8702     /* HW attentions */
8703     if (status & BXE_DEF_SB_ATT_IDX) {
8704         BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
8705         bxe_attn_int(sc);
8706         status &= ~BXE_DEF_SB_ATT_IDX;
8707     }
8708
8709     /* SP events: STAT_QUERY and others */
8710     if (status & BXE_DEF_SB_IDX) {
8711         /* handle EQ completions */
8712         BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
8713         bxe_eq_int(sc);
8714         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
8715                    le16toh(sc->def_idx), IGU_INT_NOP, 1);
8716         status &= ~BXE_DEF_SB_IDX;
8717     }
8718
8719     /* if status is non zero then something went wrong */
8720     if (__predict_false(status)) {
8721         BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
8722     }
8723
8724     /* ack status block only if something was actually handled */
8725     bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
8726                le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
8727
8728     /*
8729      * Must be called after the EQ processing (since eq leads to sriov
8730      * ramrod completion flows).
8731      * This flow may have been scheduled by the arrival of a ramrod
8732      * completion, or by the sriov code rescheduling itself.
8733      */
8734     // XXX bxe_iov_sp_task(sc);
8735
8736 }
8737
8738 static void
8739 bxe_handle_fp_tq(void *context,
8740                  int  pending)
8741 {
8742     struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
8743     struct bxe_softc *sc = fp->sc;
8744     uint8_t more_tx = FALSE;
8745     uint8_t more_rx = FALSE;
8746
8747     BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
8748
8749     /* XXX
8750      * IFF_DRV_RUNNING state can't be checked here since we process
8751      * slowpath events on a client queue during setup. Instead
8752      * we need to add a "process/continue" flag here that the driver
8753      * can use to tell the task here not to do anything.
8754      */
8755 #if 0
8756     if (!(sc->ifnet->if_drv_flags & IFF_DRV_RUNNING)) {
8757         return;
8758     }
8759 #endif
8760
8761     /* update the fastpath index */
8762     bxe_update_fp_sb_idx(fp);
8763
8764     /* XXX add loop here if ever support multiple tx CoS */
8765     /* fp->txdata[cos] */
8766     if (bxe_has_tx_work(fp)) {
8767         BXE_FP_TX_LOCK(fp);
8768         more_tx = bxe_txeof(sc, fp);
8769         BXE_FP_TX_UNLOCK(fp);
8770     }
8771
8772     if (bxe_has_rx_work(fp)) {
8773         more_rx = bxe_rxeof(sc, fp);
8774     }
8775
8776     if (more_rx /*|| more_tx*/) {
8777         /* still more work to do */
8778         taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
8779         return;
8780     }
8781
8782     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8783                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8784 }
8785
8786 static void
8787 bxe_task_fp(struct bxe_fastpath *fp)
8788 {
8789     struct bxe_softc *sc = fp->sc;
8790     uint8_t more_tx = FALSE;
8791     uint8_t more_rx = FALSE;
8792
8793     BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
8794
8795     /* update the fastpath index */
8796     bxe_update_fp_sb_idx(fp);
8797
8798     /* XXX add loop here if ever support multiple tx CoS */
8799     /* fp->txdata[cos] */
8800     if (bxe_has_tx_work(fp)) {
8801         BXE_FP_TX_LOCK(fp);
8802         more_tx = bxe_txeof(sc, fp);
8803         BXE_FP_TX_UNLOCK(fp);
8804     }
8805
8806     if (bxe_has_rx_work(fp)) {
8807         more_rx = bxe_rxeof(sc, fp);
8808     }
8809
8810     if (more_rx /*|| more_tx*/) {
8811         /* still more work to do, bail out if this ISR and process later */
8812         taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
8813         return;
8814     }
8815
8816     /*
8817      * Here we write the fastpath index taken before doing any tx or rx work.
8818      * It is very well possible other hw events occurred up to this point and
8819      * they were actually processed accordingly above. Since we're going to
8820      * write an older fastpath index, an interrupt is coming which we might
8821      * not do any work in.
8822      */
8823     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
8824                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
8825 }
8826
8827 /*
8828  * Legacy interrupt entry point.
8829  *
8830  * Verifies that the controller generated the interrupt and
8831  * then calls a separate routine to handle the various
8832  * interrupt causes: link, RX, and TX.
8833  */
8834 static void
8835 bxe_intr_legacy(void *xsc)
8836 {
8837     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8838     struct bxe_fastpath *fp;
8839     uint16_t status, mask;
8840     int i;
8841
8842     BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
8843
8844     /*
8845      * 0 for ustorm, 1 for cstorm
8846      * the bits returned from ack_int() are 0-15
8847      * bit 0 = attention status block
8848      * bit 1 = fast path status block
8849      * a mask of 0x2 or more = tx/rx event
8850      * a mask of 1 = slow path event
8851      */
8852
8853     status = bxe_ack_int(sc);
8854
8855     /* the interrupt is not for us */
8856     if (__predict_false(status == 0)) {
8857         BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
8858         return;
8859     }
8860
8861     BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
8862
8863     FOR_EACH_ETH_QUEUE(sc, i) {
8864         fp = &sc->fp[i];
8865         mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
8866         if (status & mask) {
8867             /* acknowledge and disable further fastpath interrupts */
8868             bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8869             bxe_task_fp(fp);
8870             status &= ~mask;
8871         }
8872     }
8873
8874     if (__predict_false(status & 0x1)) {
8875         /* acknowledge and disable further slowpath interrupts */
8876         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8877
8878         /* schedule slowpath handler */
8879         taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
8880
8881         status &= ~0x1;
8882     }
8883
8884     if (__predict_false(status)) {
8885         BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
8886     }
8887 }
8888
8889 /* slowpath interrupt entry point */
8890 static void
8891 bxe_intr_sp(void *xsc)
8892 {
8893     struct bxe_softc *sc = (struct bxe_softc *)xsc;
8894
8895     BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
8896
8897     /* acknowledge and disable further slowpath interrupts */
8898     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8899
8900     /* schedule slowpath handler */
8901     taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
8902 }
8903
8904 /* fastpath interrupt entry point */
8905 static void
8906 bxe_intr_fp(void *xfp)
8907 {
8908     struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
8909     struct bxe_softc *sc = fp->sc;
8910
8911     BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
8912
8913     BLOGD(sc, DBG_INTR,
8914           "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
8915           curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
8916
8917     /* acknowledge and disable further fastpath interrupts */
8918     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
8919
8920     bxe_task_fp(fp);
8921 }
8922
8923 /* Release all interrupts allocated by the driver. */
8924 static void
8925 bxe_interrupt_free(struct bxe_softc *sc)
8926 {
8927     int i;
8928
8929     switch (sc->interrupt_mode) {
8930     case INTR_MODE_INTX:
8931         BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
8932         if (sc->intr[0].resource != NULL) {
8933             bus_release_resource(sc->dev,
8934                                  SYS_RES_IRQ,
8935                                  sc->intr[0].rid,
8936                                  sc->intr[0].resource);
8937         }
8938         break;
8939     case INTR_MODE_MSI:
8940         for (i = 0; i < sc->intr_count; i++) {
8941             BLOGD(sc, DBG_LOAD, "Releasing MSI 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     case INTR_MODE_MSIX:
8952         for (i = 0; i < sc->intr_count; i++) {
8953             BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
8954             if (sc->intr[i].resource && sc->intr[i].rid) {
8955                 bus_release_resource(sc->dev,
8956                                      SYS_RES_IRQ,
8957                                      sc->intr[i].rid,
8958                                      sc->intr[i].resource);
8959             }
8960         }
8961         pci_release_msi(sc->dev);
8962         break;
8963     default:
8964         /* nothing to do as initial allocation failed */
8965         break;
8966     }
8967 }
8968
8969 /*
8970  * This function determines and allocates the appropriate
8971  * interrupt based on system capabilites and user request.
8972  *
8973  * The user may force a particular interrupt mode, specify
8974  * the number of receive queues, specify the method for
8975  * distribuitng received frames to receive queues, or use
8976  * the default settings which will automatically select the
8977  * best supported combination.  In addition, the OS may or
8978  * may not support certain combinations of these settings.
8979  * This routine attempts to reconcile the settings requested
8980  * by the user with the capabilites available from the system
8981  * to select the optimal combination of features.
8982  *
8983  * Returns:
8984  *   0 = Success, !0 = Failure.
8985  */
8986 static int
8987 bxe_interrupt_alloc(struct bxe_softc *sc)
8988 {
8989     int msix_count = 0;
8990     int msi_count = 0;
8991     int num_requested = 0;
8992     int num_allocated = 0;
8993     int rid, i, j;
8994     int rc;
8995
8996     /* get the number of available MSI/MSI-X interrupts from the OS */
8997     if (sc->interrupt_mode > 0) {
8998         if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
8999             msix_count = pci_msix_count(sc->dev);
9000         }
9001
9002         if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
9003             msi_count = pci_msi_count(sc->dev);
9004         }
9005
9006         BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
9007               msi_count, msix_count);
9008     }
9009
9010     do { /* try allocating MSI-X interrupt resources (at least 2) */
9011         if (sc->interrupt_mode != INTR_MODE_MSIX) {
9012             break;
9013         }
9014
9015         if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
9016             (msix_count < 2)) {
9017             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9018             break;
9019         }
9020
9021         /* ask for the necessary number of MSI-X vectors */
9022         num_requested = min((sc->num_queues + 1), msix_count);
9023
9024         BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
9025
9026         num_allocated = num_requested;
9027         if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
9028             BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
9029             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9030             break;
9031         }
9032
9033         if (num_allocated < 2) { /* possible? */
9034             BLOGE(sc, "MSI-X allocation less than 2!\n");
9035             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9036             pci_release_msi(sc->dev);
9037             break;
9038         }
9039
9040         BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
9041               num_requested, num_allocated);
9042
9043         /* best effort so use the number of vectors allocated to us */
9044         sc->intr_count = num_allocated;
9045         sc->num_queues = num_allocated - 1;
9046
9047         rid = 1; /* initial resource identifier */
9048
9049         /* allocate the MSI-X vectors */
9050         for (i = 0; i < num_allocated; i++) {
9051             sc->intr[i].rid = (rid + i);
9052
9053             if ((sc->intr[i].resource =
9054                  bus_alloc_resource_any(sc->dev,
9055                                         SYS_RES_IRQ,
9056                                         &sc->intr[i].rid,
9057                                         RF_ACTIVE)) == NULL) {
9058                 BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
9059                       i, (rid + i));
9060
9061                 for (j = (i - 1); j >= 0; j--) {
9062                     bus_release_resource(sc->dev,
9063                                          SYS_RES_IRQ,
9064                                          sc->intr[j].rid,
9065                                          sc->intr[j].resource);
9066                 }
9067
9068                 sc->intr_count = 0;
9069                 sc->num_queues = 0;
9070                 sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9071                 pci_release_msi(sc->dev);
9072                 break;
9073             }
9074
9075             BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9076         }
9077     } while (0);
9078
9079     do { /* try allocating MSI vector resources (at least 2) */
9080         if (sc->interrupt_mode != INTR_MODE_MSI) {
9081             break;
9082         }
9083
9084         if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9085             (msi_count < 1)) {
9086             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9087             break;
9088         }
9089
9090         /* ask for a single MSI vector */
9091         num_requested = 1;
9092
9093         BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9094
9095         num_allocated = num_requested;
9096         if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9097             BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9098             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9099             break;
9100         }
9101
9102         if (num_allocated != 1) { /* possible? */
9103             BLOGE(sc, "MSI allocation is not 1!\n");
9104             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9105             pci_release_msi(sc->dev);
9106             break;
9107         }
9108
9109         BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9110               num_requested, num_allocated);
9111
9112         /* best effort so use the number of vectors allocated to us */
9113         sc->intr_count = num_allocated;
9114         sc->num_queues = num_allocated;
9115
9116         rid = 1; /* initial resource identifier */
9117
9118         sc->intr[0].rid = rid;
9119
9120         if ((sc->intr[0].resource =
9121              bus_alloc_resource_any(sc->dev,
9122                                     SYS_RES_IRQ,
9123                                     &sc->intr[0].rid,
9124                                     RF_ACTIVE)) == NULL) {
9125             BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9126             sc->intr_count = 0;
9127             sc->num_queues = 0;
9128             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9129             pci_release_msi(sc->dev);
9130             break;
9131         }
9132
9133         BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9134     } while (0);
9135
9136     do { /* try allocating INTx vector resources */
9137         if (sc->interrupt_mode != INTR_MODE_INTX) {
9138             break;
9139         }
9140
9141         BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9142
9143         /* only one vector for INTx */
9144         sc->intr_count = 1;
9145         sc->num_queues = 1;
9146
9147         rid = 0; /* initial resource identifier */
9148
9149         sc->intr[0].rid = rid;
9150
9151         if ((sc->intr[0].resource =
9152              bus_alloc_resource_any(sc->dev,
9153                                     SYS_RES_IRQ,
9154                                     &sc->intr[0].rid,
9155                                     (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9156             BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9157             sc->intr_count = 0;
9158             sc->num_queues = 0;
9159             sc->interrupt_mode = -1; /* Failed! */
9160             break;
9161         }
9162
9163         BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9164     } while (0);
9165
9166     if (sc->interrupt_mode == -1) {
9167         BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9168         rc = 1;
9169     } else {
9170         BLOGD(sc, DBG_LOAD,
9171               "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9172               sc->interrupt_mode, sc->num_queues);
9173         rc = 0;
9174     }
9175
9176     return (rc);
9177 }
9178
9179 static void
9180 bxe_interrupt_detach(struct bxe_softc *sc)
9181 {
9182     struct bxe_fastpath *fp;
9183     int i;
9184
9185     /* release interrupt resources */
9186     for (i = 0; i < sc->intr_count; i++) {
9187         if (sc->intr[i].resource && sc->intr[i].tag) {
9188             BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9189             bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9190         }
9191     }
9192
9193     for (i = 0; i < sc->num_queues; i++) {
9194         fp = &sc->fp[i];
9195         if (fp->tq) {
9196             taskqueue_drain(fp->tq, &fp->tq_task);
9197             taskqueue_drain(fp->tq, &fp->tx_task);
9198             while (taskqueue_cancel_timeout(fp->tq, &fp->tx_timeout_task,
9199                 NULL))
9200                 taskqueue_drain_timeout(fp->tq, &fp->tx_timeout_task);
9201         }
9202
9203         for (i = 0; i < sc->num_queues; i++) {
9204             fp = &sc->fp[i];
9205             if (fp->tq != NULL) {
9206                 taskqueue_free(fp->tq);
9207                 fp->tq = NULL;
9208             }
9209         }
9210     }
9211
9212     if (sc->sp_tq) {
9213         taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9214         taskqueue_free(sc->sp_tq);
9215         sc->sp_tq = NULL;
9216     }
9217 }
9218
9219 /*
9220  * Enables interrupts and attach to the ISR.
9221  *
9222  * When using multiple MSI/MSI-X vectors the first vector
9223  * is used for slowpath operations while all remaining
9224  * vectors are used for fastpath operations.  If only a
9225  * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9226  * ISR must look for both slowpath and fastpath completions.
9227  */
9228 static int
9229 bxe_interrupt_attach(struct bxe_softc *sc)
9230 {
9231     struct bxe_fastpath *fp;
9232     int rc = 0;
9233     int i;
9234
9235     snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9236              "bxe%d_sp_tq", sc->unit);
9237     TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9238     sc->sp_tq = taskqueue_create(sc->sp_tq_name, M_NOWAIT,
9239                                  taskqueue_thread_enqueue,
9240                                  &sc->sp_tq);
9241     taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9242                             "%s", sc->sp_tq_name);
9243
9244
9245     for (i = 0; i < sc->num_queues; i++) {
9246         fp = &sc->fp[i];
9247         snprintf(fp->tq_name, sizeof(fp->tq_name),
9248                  "bxe%d_fp%d_tq", sc->unit, i);
9249         TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9250         TASK_INIT(&fp->tx_task, 0, bxe_tx_mq_start_deferred, fp);
9251         fp->tq = taskqueue_create(fp->tq_name, M_NOWAIT,
9252                                   taskqueue_thread_enqueue,
9253                                   &fp->tq);
9254         TIMEOUT_TASK_INIT(fp->tq, &fp->tx_timeout_task, 0,
9255                           bxe_tx_mq_start_deferred, fp);
9256         taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9257                                 "%s", fp->tq_name);
9258     }
9259
9260     /* setup interrupt handlers */
9261     if (sc->interrupt_mode == INTR_MODE_MSIX) {
9262         BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9263
9264         /*
9265          * Setup the interrupt handler. Note that we pass the driver instance
9266          * to the interrupt handler for the slowpath.
9267          */
9268         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9269                                  (INTR_TYPE_NET | INTR_MPSAFE),
9270                                  NULL, bxe_intr_sp, sc,
9271                                  &sc->intr[0].tag)) != 0) {
9272             BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9273             goto bxe_interrupt_attach_exit;
9274         }
9275
9276         bus_describe_intr(sc->dev, sc->intr[0].resource,
9277                           sc->intr[0].tag, "sp");
9278
9279         /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9280
9281         /* initialize the fastpath vectors (note the first was used for sp) */
9282         for (i = 0; i < sc->num_queues; i++) {
9283             fp = &sc->fp[i];
9284             BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9285
9286             /*
9287              * Setup the interrupt handler. Note that we pass the
9288              * fastpath context to the interrupt handler in this
9289              * case.
9290              */
9291             if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9292                                      (INTR_TYPE_NET | INTR_MPSAFE),
9293                                      NULL, bxe_intr_fp, fp,
9294                                      &sc->intr[i + 1].tag)) != 0) {
9295                 BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9296                       (i + 1), rc);
9297                 goto bxe_interrupt_attach_exit;
9298             }
9299
9300             bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9301                               sc->intr[i + 1].tag, "fp%02d", i);
9302
9303             /* bind the fastpath instance to a cpu */
9304             if (sc->num_queues > 1) {
9305                 bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9306             }
9307
9308             fp->state = BXE_FP_STATE_IRQ;
9309         }
9310     } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9311         BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9312
9313         /*
9314          * Setup the interrupt handler. Note that we pass the
9315          * driver instance to the interrupt handler which
9316          * will handle both the slowpath and fastpath.
9317          */
9318         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9319                                  (INTR_TYPE_NET | INTR_MPSAFE),
9320                                  NULL, bxe_intr_legacy, sc,
9321                                  &sc->intr[0].tag)) != 0) {
9322             BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9323             goto bxe_interrupt_attach_exit;
9324         }
9325
9326     } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9327         BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9328
9329         /*
9330          * Setup the interrupt handler. Note that we pass the
9331          * driver instance to the interrupt handler which
9332          * will handle both the slowpath and fastpath.
9333          */
9334         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9335                                  (INTR_TYPE_NET | INTR_MPSAFE),
9336                                  NULL, bxe_intr_legacy, sc,
9337                                  &sc->intr[0].tag)) != 0) {
9338             BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9339             goto bxe_interrupt_attach_exit;
9340         }
9341     }
9342
9343 bxe_interrupt_attach_exit:
9344
9345     return (rc);
9346 }
9347
9348 static int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9349 static int  bxe_init_hw_common(struct bxe_softc *sc);
9350 static int  bxe_init_hw_port(struct bxe_softc *sc);
9351 static int  bxe_init_hw_func(struct bxe_softc *sc);
9352 static void bxe_reset_common(struct bxe_softc *sc);
9353 static void bxe_reset_port(struct bxe_softc *sc);
9354 static void bxe_reset_func(struct bxe_softc *sc);
9355 static int  bxe_gunzip_init(struct bxe_softc *sc);
9356 static void bxe_gunzip_end(struct bxe_softc *sc);
9357 static int  bxe_init_firmware(struct bxe_softc *sc);
9358 static void bxe_release_firmware(struct bxe_softc *sc);
9359
9360 static struct
9361 ecore_func_sp_drv_ops bxe_func_sp_drv = {
9362     .init_hw_cmn_chip = bxe_init_hw_common_chip,
9363     .init_hw_cmn      = bxe_init_hw_common,
9364     .init_hw_port     = bxe_init_hw_port,
9365     .init_hw_func     = bxe_init_hw_func,
9366
9367     .reset_hw_cmn     = bxe_reset_common,
9368     .reset_hw_port    = bxe_reset_port,
9369     .reset_hw_func    = bxe_reset_func,
9370
9371     .gunzip_init      = bxe_gunzip_init,
9372     .gunzip_end       = bxe_gunzip_end,
9373
9374     .init_fw          = bxe_init_firmware,
9375     .release_fw       = bxe_release_firmware,
9376 };
9377
9378 static void
9379 bxe_init_func_obj(struct bxe_softc *sc)
9380 {
9381     sc->dmae_ready = 0;
9382
9383     ecore_init_func_obj(sc,
9384                         &sc->func_obj,
9385                         BXE_SP(sc, func_rdata),
9386                         BXE_SP_MAPPING(sc, func_rdata),
9387                         BXE_SP(sc, func_afex_rdata),
9388                         BXE_SP_MAPPING(sc, func_afex_rdata),
9389                         &bxe_func_sp_drv);
9390 }
9391
9392 static int
9393 bxe_init_hw(struct bxe_softc *sc,
9394             uint32_t         load_code)
9395 {
9396     struct ecore_func_state_params func_params = { NULL };
9397     int rc;
9398
9399     /* prepare the parameters for function state transitions */
9400     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9401
9402     func_params.f_obj = &sc->func_obj;
9403     func_params.cmd = ECORE_F_CMD_HW_INIT;
9404
9405     func_params.params.hw_init.load_phase = load_code;
9406
9407     /*
9408      * Via a plethora of function pointers, we will eventually reach
9409      * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9410      */
9411     rc = ecore_func_state_change(sc, &func_params);
9412
9413     return (rc);
9414 }
9415
9416 static void
9417 bxe_fill(struct bxe_softc *sc,
9418          uint32_t         addr,
9419          int              fill,
9420          uint32_t         len)
9421 {
9422     uint32_t i;
9423
9424     if (!(len % 4) && !(addr % 4)) {
9425         for (i = 0; i < len; i += 4) {
9426             REG_WR(sc, (addr + i), fill);
9427         }
9428     } else {
9429         for (i = 0; i < len; i++) {
9430             REG_WR8(sc, (addr + i), fill);
9431         }
9432     }
9433 }
9434
9435 /* writes FP SP data to FW - data_size in dwords */
9436 static void
9437 bxe_wr_fp_sb_data(struct bxe_softc *sc,
9438                   int              fw_sb_id,
9439                   uint32_t         *sb_data_p,
9440                   uint32_t         data_size)
9441 {
9442     int index;
9443
9444     for (index = 0; index < data_size; index++) {
9445         REG_WR(sc,
9446                (BAR_CSTRORM_INTMEM +
9447                 CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9448                 (sizeof(uint32_t) * index)),
9449                *(sb_data_p + index));
9450     }
9451 }
9452
9453 static void
9454 bxe_zero_fp_sb(struct bxe_softc *sc,
9455                int              fw_sb_id)
9456 {
9457     struct hc_status_block_data_e2 sb_data_e2;
9458     struct hc_status_block_data_e1x sb_data_e1x;
9459     uint32_t *sb_data_p;
9460     uint32_t data_size = 0;
9461
9462     if (!CHIP_IS_E1x(sc)) {
9463         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9464         sb_data_e2.common.state = SB_DISABLED;
9465         sb_data_e2.common.p_func.vf_valid = FALSE;
9466         sb_data_p = (uint32_t *)&sb_data_e2;
9467         data_size = (sizeof(struct hc_status_block_data_e2) /
9468                      sizeof(uint32_t));
9469     } else {
9470         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9471         sb_data_e1x.common.state = SB_DISABLED;
9472         sb_data_e1x.common.p_func.vf_valid = FALSE;
9473         sb_data_p = (uint32_t *)&sb_data_e1x;
9474         data_size = (sizeof(struct hc_status_block_data_e1x) /
9475                      sizeof(uint32_t));
9476     }
9477
9478     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9479
9480     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9481              0, CSTORM_STATUS_BLOCK_SIZE);
9482     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9483              0, CSTORM_SYNC_BLOCK_SIZE);
9484 }
9485
9486 static void
9487 bxe_wr_sp_sb_data(struct bxe_softc               *sc,
9488                   struct hc_sp_status_block_data *sp_sb_data)
9489 {
9490     int i;
9491
9492     for (i = 0;
9493          i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9494          i++) {
9495         REG_WR(sc,
9496                (BAR_CSTRORM_INTMEM +
9497                 CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9498                 (i * sizeof(uint32_t))),
9499                *((uint32_t *)sp_sb_data + i));
9500     }
9501 }
9502
9503 static void
9504 bxe_zero_sp_sb(struct bxe_softc *sc)
9505 {
9506     struct hc_sp_status_block_data sp_sb_data;
9507
9508     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9509
9510     sp_sb_data.state           = SB_DISABLED;
9511     sp_sb_data.p_func.vf_valid = FALSE;
9512
9513     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9514
9515     bxe_fill(sc,
9516              (BAR_CSTRORM_INTMEM +
9517               CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9518               0, CSTORM_SP_STATUS_BLOCK_SIZE);
9519     bxe_fill(sc,
9520              (BAR_CSTRORM_INTMEM +
9521               CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9522               0, CSTORM_SP_SYNC_BLOCK_SIZE);
9523 }
9524
9525 static void
9526 bxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9527                              int                       igu_sb_id,
9528                              int                       igu_seg_id)
9529 {
9530     hc_sm->igu_sb_id      = igu_sb_id;
9531     hc_sm->igu_seg_id     = igu_seg_id;
9532     hc_sm->timer_value    = 0xFF;
9533     hc_sm->time_to_expire = 0xFFFFFFFF;
9534 }
9535
9536 static void
9537 bxe_map_sb_state_machines(struct hc_index_data *index_data)
9538 {
9539     /* zero out state machine indices */
9540
9541     /* rx indices */
9542     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9543
9544     /* tx indices */
9545     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9546     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9547     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9548     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9549
9550     /* map indices */
9551
9552     /* rx indices */
9553     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9554         (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9555
9556     /* tx indices */
9557     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9558         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9559     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9560         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9561     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9562         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9563     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9564         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9565 }
9566
9567 static void
9568 bxe_init_sb(struct bxe_softc *sc,
9569             bus_addr_t       busaddr,
9570             int              vfid,
9571             uint8_t          vf_valid,
9572             int              fw_sb_id,
9573             int              igu_sb_id)
9574 {
9575     struct hc_status_block_data_e2  sb_data_e2;
9576     struct hc_status_block_data_e1x sb_data_e1x;
9577     struct hc_status_block_sm       *hc_sm_p;
9578     uint32_t *sb_data_p;
9579     int igu_seg_id;
9580     int data_size;
9581
9582     if (CHIP_INT_MODE_IS_BC(sc)) {
9583         igu_seg_id = HC_SEG_ACCESS_NORM;
9584     } else {
9585         igu_seg_id = IGU_SEG_ACCESS_NORM;
9586     }
9587
9588     bxe_zero_fp_sb(sc, fw_sb_id);
9589
9590     if (!CHIP_IS_E1x(sc)) {
9591         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9592         sb_data_e2.common.state = SB_ENABLED;
9593         sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9594         sb_data_e2.common.p_func.vf_id = vfid;
9595         sb_data_e2.common.p_func.vf_valid = vf_valid;
9596         sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9597         sb_data_e2.common.same_igu_sb_1b = TRUE;
9598         sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9599         sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9600         hc_sm_p = sb_data_e2.common.state_machine;
9601         sb_data_p = (uint32_t *)&sb_data_e2;
9602         data_size = (sizeof(struct hc_status_block_data_e2) /
9603                      sizeof(uint32_t));
9604         bxe_map_sb_state_machines(sb_data_e2.index_data);
9605     } else {
9606         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9607         sb_data_e1x.common.state = SB_ENABLED;
9608         sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9609         sb_data_e1x.common.p_func.vf_id = 0xff;
9610         sb_data_e1x.common.p_func.vf_valid = FALSE;
9611         sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9612         sb_data_e1x.common.same_igu_sb_1b = TRUE;
9613         sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9614         sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9615         hc_sm_p = sb_data_e1x.common.state_machine;
9616         sb_data_p = (uint32_t *)&sb_data_e1x;
9617         data_size = (sizeof(struct hc_status_block_data_e1x) /
9618                      sizeof(uint32_t));
9619         bxe_map_sb_state_machines(sb_data_e1x.index_data);
9620     }
9621
9622     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9623     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9624
9625     BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9626
9627     /* write indices to HW - PCI guarantees endianity of regpairs */
9628     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9629 }
9630
9631 static inline uint8_t
9632 bxe_fp_qzone_id(struct bxe_fastpath *fp)
9633 {
9634     if (CHIP_IS_E1x(fp->sc)) {
9635         return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
9636     } else {
9637         return (fp->cl_id);
9638     }
9639 }
9640
9641 static inline uint32_t
9642 bxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
9643                            struct bxe_fastpath *fp)
9644 {
9645     uint32_t offset = BAR_USTRORM_INTMEM;
9646
9647     if (!CHIP_IS_E1x(sc)) {
9648         offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
9649     } else {
9650         offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
9651     }
9652
9653     return (offset);
9654 }
9655
9656 static void
9657 bxe_init_eth_fp(struct bxe_softc *sc,
9658                 int              idx)
9659 {
9660     struct bxe_fastpath *fp = &sc->fp[idx];
9661     uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
9662     unsigned long q_type = 0;
9663     int cos;
9664
9665     fp->sc    = sc;
9666     fp->index = idx;
9667
9668     fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
9669     fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
9670
9671     fp->cl_id = (CHIP_IS_E1x(sc)) ?
9672                     (SC_L_ID(sc) + idx) :
9673                     /* want client ID same as IGU SB ID for non-E1 */
9674                     fp->igu_sb_id;
9675     fp->cl_qzone_id = bxe_fp_qzone_id(fp);
9676
9677     /* setup sb indices */
9678     if (!CHIP_IS_E1x(sc)) {
9679         fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
9680         fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
9681     } else {
9682         fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
9683         fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
9684     }
9685
9686     /* init shortcut */
9687     fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
9688
9689     fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
9690
9691     /*
9692      * XXX If multiple CoS is ever supported then each fastpath structure
9693      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
9694      */
9695     for (cos = 0; cos < sc->max_cos; cos++) {
9696         cids[cos] = idx;
9697     }
9698     fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
9699
9700     /* nothing more for a VF to do */
9701     if (IS_VF(sc)) {
9702         return;
9703     }
9704
9705     bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
9706                 fp->fw_sb_id, fp->igu_sb_id);
9707
9708     bxe_update_fp_sb_idx(fp);
9709
9710     /* Configure Queue State object */
9711     bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
9712     bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
9713
9714     ecore_init_queue_obj(sc,
9715                          &sc->sp_objs[idx].q_obj,
9716                          fp->cl_id,
9717                          cids,
9718                          sc->max_cos,
9719                          SC_FUNC(sc),
9720                          BXE_SP(sc, q_rdata),
9721                          BXE_SP_MAPPING(sc, q_rdata),
9722                          q_type);
9723
9724     /* configure classification DBs */
9725     ecore_init_mac_obj(sc,
9726                        &sc->sp_objs[idx].mac_obj,
9727                        fp->cl_id,
9728                        idx,
9729                        SC_FUNC(sc),
9730                        BXE_SP(sc, mac_rdata),
9731                        BXE_SP_MAPPING(sc, mac_rdata),
9732                        ECORE_FILTER_MAC_PENDING,
9733                        &sc->sp_state,
9734                        ECORE_OBJ_TYPE_RX_TX,
9735                        &sc->macs_pool);
9736
9737     BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
9738           idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
9739 }
9740
9741 static inline void
9742 bxe_update_rx_prod(struct bxe_softc    *sc,
9743                    struct bxe_fastpath *fp,
9744                    uint16_t            rx_bd_prod,
9745                    uint16_t            rx_cq_prod,
9746                    uint16_t            rx_sge_prod)
9747 {
9748     struct ustorm_eth_rx_producers rx_prods = { 0 };
9749     uint32_t i;
9750
9751     /* update producers */
9752     rx_prods.bd_prod  = rx_bd_prod;
9753     rx_prods.cqe_prod = rx_cq_prod;
9754     rx_prods.sge_prod = rx_sge_prod;
9755
9756     /*
9757      * Make sure that the BD and SGE data is updated before updating the
9758      * producers since FW might read the BD/SGE right after the producer
9759      * is updated.
9760      * This is only applicable for weak-ordered memory model archs such
9761      * as IA-64. The following barrier is also mandatory since FW will
9762      * assumes BDs must have buffers.
9763      */
9764     wmb();
9765
9766     for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
9767         REG_WR(sc,
9768                (fp->ustorm_rx_prods_offset + (i * 4)),
9769                ((uint32_t *)&rx_prods)[i]);
9770     }
9771
9772     wmb(); /* keep prod updates ordered */
9773
9774     BLOGD(sc, DBG_RX,
9775           "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
9776           fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
9777 }
9778
9779 static void
9780 bxe_init_rx_rings(struct bxe_softc *sc)
9781 {
9782     struct bxe_fastpath *fp;
9783     int i;
9784
9785     for (i = 0; i < sc->num_queues; i++) {
9786         fp = &sc->fp[i];
9787
9788         fp->rx_bd_cons = 0;
9789
9790         /*
9791          * Activate the BD ring...
9792          * Warning, this will generate an interrupt (to the TSTORM)
9793          * so this can only be done after the chip is initialized
9794          */
9795         bxe_update_rx_prod(sc, fp,
9796                            fp->rx_bd_prod,
9797                            fp->rx_cq_prod,
9798                            fp->rx_sge_prod);
9799
9800         if (i != 0) {
9801             continue;
9802         }
9803
9804         if (CHIP_IS_E1(sc)) {
9805             REG_WR(sc,
9806                    (BAR_USTRORM_INTMEM +
9807                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
9808                    U64_LO(fp->rcq_dma.paddr));
9809             REG_WR(sc,
9810                    (BAR_USTRORM_INTMEM +
9811                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
9812                    U64_HI(fp->rcq_dma.paddr));
9813         }
9814     }
9815 }
9816
9817 static void
9818 bxe_init_tx_ring_one(struct bxe_fastpath *fp)
9819 {
9820     SET_FLAG(fp->tx_db.data.header.data, DOORBELL_HDR_T_DB_TYPE, 1);
9821     fp->tx_db.data.zero_fill1 = 0;
9822     fp->tx_db.data.prod = 0;
9823
9824     fp->tx_pkt_prod = 0;
9825     fp->tx_pkt_cons = 0;
9826     fp->tx_bd_prod = 0;
9827     fp->tx_bd_cons = 0;
9828     fp->eth_q_stats.tx_pkts = 0;
9829 }
9830
9831 static inline void
9832 bxe_init_tx_rings(struct bxe_softc *sc)
9833 {
9834     int i;
9835
9836     for (i = 0; i < sc->num_queues; i++) {
9837         bxe_init_tx_ring_one(&sc->fp[i]);
9838     }
9839 }
9840
9841 static void
9842 bxe_init_def_sb(struct bxe_softc *sc)
9843 {
9844     struct host_sp_status_block *def_sb = sc->def_sb;
9845     bus_addr_t mapping = sc->def_sb_dma.paddr;
9846     int igu_sp_sb_index;
9847     int igu_seg_id;
9848     int port = SC_PORT(sc);
9849     int func = SC_FUNC(sc);
9850     int reg_offset, reg_offset_en5;
9851     uint64_t section;
9852     int index, sindex;
9853     struct hc_sp_status_block_data sp_sb_data;
9854
9855     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9856
9857     if (CHIP_INT_MODE_IS_BC(sc)) {
9858         igu_sp_sb_index = DEF_SB_IGU_ID;
9859         igu_seg_id = HC_SEG_ACCESS_DEF;
9860     } else {
9861         igu_sp_sb_index = sc->igu_dsb_id;
9862         igu_seg_id = IGU_SEG_ACCESS_DEF;
9863     }
9864
9865     /* attentions */
9866     section = ((uint64_t)mapping +
9867                offsetof(struct host_sp_status_block, atten_status_block));
9868     def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
9869     sc->attn_state = 0;
9870
9871     reg_offset = (port) ?
9872                      MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
9873                      MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
9874     reg_offset_en5 = (port) ?
9875                          MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
9876                          MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
9877
9878     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
9879         /* take care of sig[0]..sig[4] */
9880         for (sindex = 0; sindex < 4; sindex++) {
9881             sc->attn_group[index].sig[sindex] =
9882                 REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
9883         }
9884
9885         if (!CHIP_IS_E1x(sc)) {
9886             /*
9887              * enable5 is separate from the rest of the registers,
9888              * and the address skip is 4 and not 16 between the
9889              * different groups
9890              */
9891             sc->attn_group[index].sig[4] =
9892                 REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
9893         } else {
9894             sc->attn_group[index].sig[4] = 0;
9895         }
9896     }
9897
9898     if (sc->devinfo.int_block == INT_BLOCK_HC) {
9899         reg_offset = (port) ?
9900                          HC_REG_ATTN_MSG1_ADDR_L :
9901                          HC_REG_ATTN_MSG0_ADDR_L;
9902         REG_WR(sc, reg_offset, U64_LO(section));
9903         REG_WR(sc, (reg_offset + 4), U64_HI(section));
9904     } else if (!CHIP_IS_E1x(sc)) {
9905         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
9906         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
9907     }
9908
9909     section = ((uint64_t)mapping +
9910                offsetof(struct host_sp_status_block, sp_sb));
9911
9912     bxe_zero_sp_sb(sc);
9913
9914     /* PCI guarantees endianity of regpair */
9915     sp_sb_data.state           = SB_ENABLED;
9916     sp_sb_data.host_sb_addr.lo = U64_LO(section);
9917     sp_sb_data.host_sb_addr.hi = U64_HI(section);
9918     sp_sb_data.igu_sb_id       = igu_sp_sb_index;
9919     sp_sb_data.igu_seg_id      = igu_seg_id;
9920     sp_sb_data.p_func.pf_id    = func;
9921     sp_sb_data.p_func.vnic_id  = SC_VN(sc);
9922     sp_sb_data.p_func.vf_id    = 0xff;
9923
9924     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9925
9926     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
9927 }
9928
9929 static void
9930 bxe_init_sp_ring(struct bxe_softc *sc)
9931 {
9932     atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
9933     sc->spq_prod_idx = 0;
9934     sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
9935     sc->spq_prod_bd = sc->spq;
9936     sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
9937 }
9938
9939 static void
9940 bxe_init_eq_ring(struct bxe_softc *sc)
9941 {
9942     union event_ring_elem *elem;
9943     int i;
9944
9945     for (i = 1; i <= NUM_EQ_PAGES; i++) {
9946         elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
9947
9948         elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
9949                                                  BCM_PAGE_SIZE *
9950                                                  (i % NUM_EQ_PAGES)));
9951         elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
9952                                                  BCM_PAGE_SIZE *
9953                                                  (i % NUM_EQ_PAGES)));
9954     }
9955
9956     sc->eq_cons    = 0;
9957     sc->eq_prod    = NUM_EQ_DESC;
9958     sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
9959
9960     atomic_store_rel_long(&sc->eq_spq_left,
9961                           (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
9962                                NUM_EQ_DESC) - 1));
9963 }
9964
9965 static void
9966 bxe_init_internal_common(struct bxe_softc *sc)
9967 {
9968     int i;
9969
9970     /*
9971      * Zero this manually as its initialization is currently missing
9972      * in the initTool.
9973      */
9974     for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
9975         REG_WR(sc,
9976                (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
9977                0);
9978     }
9979
9980     if (!CHIP_IS_E1x(sc)) {
9981         REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
9982                 CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
9983     }
9984 }
9985
9986 static void
9987 bxe_init_internal(struct bxe_softc *sc,
9988                   uint32_t         load_code)
9989 {
9990     switch (load_code) {
9991     case FW_MSG_CODE_DRV_LOAD_COMMON:
9992     case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
9993         bxe_init_internal_common(sc);
9994         /* no break */
9995
9996     case FW_MSG_CODE_DRV_LOAD_PORT:
9997         /* nothing to do */
9998         /* no break */
9999
10000     case FW_MSG_CODE_DRV_LOAD_FUNCTION:
10001         /* internal memory per function is initialized inside bxe_pf_init */
10002         break;
10003
10004     default:
10005         BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
10006         break;
10007     }
10008 }
10009
10010 static void
10011 storm_memset_func_cfg(struct bxe_softc                         *sc,
10012                       struct tstorm_eth_function_common_config *tcfg,
10013                       uint16_t                                  abs_fid)
10014 {
10015     uint32_t addr;
10016     size_t size;
10017
10018     addr = (BAR_TSTRORM_INTMEM +
10019             TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
10020     size = sizeof(struct tstorm_eth_function_common_config);
10021     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
10022 }
10023
10024 static void
10025 bxe_func_init(struct bxe_softc            *sc,
10026               struct bxe_func_init_params *p)
10027 {
10028     struct tstorm_eth_function_common_config tcfg = { 0 };
10029
10030     if (CHIP_IS_E1x(sc)) {
10031         storm_memset_func_cfg(sc, &tcfg, p->func_id);
10032     }
10033
10034     /* Enable the function in the FW */
10035     storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
10036     storm_memset_func_en(sc, p->func_id, 1);
10037
10038     /* spq */
10039     if (p->func_flgs & FUNC_FLG_SPQ) {
10040         storm_memset_spq_addr(sc, p->spq_map, p->func_id);
10041         REG_WR(sc,
10042                (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
10043                p->spq_prod);
10044     }
10045 }
10046
10047 /*
10048  * Calculates the sum of vn_min_rates.
10049  * It's needed for further normalizing of the min_rates.
10050  * Returns:
10051  *   sum of vn_min_rates.
10052  *     or
10053  *   0 - if all the min_rates are 0.
10054  * In the later case fainess algorithm should be deactivated.
10055  * If all min rates are not zero then those that are zeroes will be set to 1.
10056  */
10057 static void
10058 bxe_calc_vn_min(struct bxe_softc       *sc,
10059                 struct cmng_init_input *input)
10060 {
10061     uint32_t vn_cfg;
10062     uint32_t vn_min_rate;
10063     int all_zero = 1;
10064     int vn;
10065
10066     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10067         vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10068         vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
10069                         FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
10070
10071         if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10072             /* skip hidden VNs */
10073             vn_min_rate = 0;
10074         } else if (!vn_min_rate) {
10075             /* If min rate is zero - set it to 100 */
10076             vn_min_rate = DEF_MIN_RATE;
10077         } else {
10078             all_zero = 0;
10079         }
10080
10081         input->vnic_min_rate[vn] = vn_min_rate;
10082     }
10083
10084     /* if ETS or all min rates are zeros - disable fairness */
10085     if (BXE_IS_ETS_ENABLED(sc)) {
10086         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10087         BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10088     } else if (all_zero) {
10089         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10090         BLOGD(sc, DBG_LOAD,
10091               "Fariness disabled (all MIN values are zeroes)\n");
10092     } else {
10093         input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10094     }
10095 }
10096
10097 static inline uint16_t
10098 bxe_extract_max_cfg(struct bxe_softc *sc,
10099                     uint32_t         mf_cfg)
10100 {
10101     uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10102                         FUNC_MF_CFG_MAX_BW_SHIFT);
10103
10104     if (!max_cfg) {
10105         BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10106         max_cfg = 100;
10107     }
10108
10109     return (max_cfg);
10110 }
10111
10112 static void
10113 bxe_calc_vn_max(struct bxe_softc       *sc,
10114                 int                    vn,
10115                 struct cmng_init_input *input)
10116 {
10117     uint16_t vn_max_rate;
10118     uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10119     uint32_t max_cfg;
10120
10121     if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10122         vn_max_rate = 0;
10123     } else {
10124         max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10125
10126         if (IS_MF_SI(sc)) {
10127             /* max_cfg in percents of linkspeed */
10128             vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10129         } else { /* SD modes */
10130             /* max_cfg is absolute in 100Mb units */
10131             vn_max_rate = (max_cfg * 100);
10132         }
10133     }
10134
10135     BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10136
10137     input->vnic_max_rate[vn] = vn_max_rate;
10138 }
10139
10140 static void
10141 bxe_cmng_fns_init(struct bxe_softc *sc,
10142                   uint8_t          read_cfg,
10143                   uint8_t          cmng_type)
10144 {
10145     struct cmng_init_input input;
10146     int vn;
10147
10148     memset(&input, 0, sizeof(struct cmng_init_input));
10149
10150     input.port_rate = sc->link_vars.line_speed;
10151
10152     if (cmng_type == CMNG_FNS_MINMAX) {
10153         /* read mf conf from shmem */
10154         if (read_cfg) {
10155             bxe_read_mf_cfg(sc);
10156         }
10157
10158         /* get VN min rate and enable fairness if not 0 */
10159         bxe_calc_vn_min(sc, &input);
10160
10161         /* get VN max rate */
10162         if (sc->port.pmf) {
10163             for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10164                 bxe_calc_vn_max(sc, vn, &input);
10165             }
10166         }
10167
10168         /* always enable rate shaping and fairness */
10169         input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10170
10171         ecore_init_cmng(&input, &sc->cmng);
10172         return;
10173     }
10174
10175     /* rate shaping and fairness are disabled */
10176     BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10177 }
10178
10179 static int
10180 bxe_get_cmng_fns_mode(struct bxe_softc *sc)
10181 {
10182     if (CHIP_REV_IS_SLOW(sc)) {
10183         return (CMNG_FNS_NONE);
10184     }
10185
10186     if (IS_MF(sc)) {
10187         return (CMNG_FNS_MINMAX);
10188     }
10189
10190     return (CMNG_FNS_NONE);
10191 }
10192
10193 static void
10194 storm_memset_cmng(struct bxe_softc *sc,
10195                   struct cmng_init *cmng,
10196                   uint8_t          port)
10197 {
10198     int vn;
10199     int func;
10200     uint32_t addr;
10201     size_t size;
10202
10203     addr = (BAR_XSTRORM_INTMEM +
10204             XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10205     size = sizeof(struct cmng_struct_per_port);
10206     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10207
10208     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10209         func = func_by_vn(sc, vn);
10210
10211         addr = (BAR_XSTRORM_INTMEM +
10212                 XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10213         size = sizeof(struct rate_shaping_vars_per_vn);
10214         ecore_storm_memset_struct(sc, addr, size,
10215                                   (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10216
10217         addr = (BAR_XSTRORM_INTMEM +
10218                 XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10219         size = sizeof(struct fairness_vars_per_vn);
10220         ecore_storm_memset_struct(sc, addr, size,
10221                                   (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10222     }
10223 }
10224
10225 static void
10226 bxe_pf_init(struct bxe_softc *sc)
10227 {
10228     struct bxe_func_init_params func_init = { 0 };
10229     struct event_ring_data eq_data = { { 0 } };
10230     uint16_t flags;
10231
10232     if (!CHIP_IS_E1x(sc)) {
10233         /* reset IGU PF statistics: MSIX + ATTN */
10234         /* PF */
10235         REG_WR(sc,
10236                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10237                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10238                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10239                0);
10240         /* ATTN */
10241         REG_WR(sc,
10242                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10243                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10244                 (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10245                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10246                0);
10247     }
10248
10249     /* function setup flags */
10250     flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10251
10252     /*
10253      * This flag is relevant for E1x only.
10254      * E2 doesn't have a TPA configuration in a function level.
10255      */
10256     flags |= (sc->ifnet->if_capenable & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10257
10258     func_init.func_flgs = flags;
10259     func_init.pf_id     = SC_FUNC(sc);
10260     func_init.func_id   = SC_FUNC(sc);
10261     func_init.spq_map   = sc->spq_dma.paddr;
10262     func_init.spq_prod  = sc->spq_prod_idx;
10263
10264     bxe_func_init(sc, &func_init);
10265
10266     memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10267
10268     /*
10269      * Congestion management values depend on the link rate.
10270      * There is no active link so initial link rate is set to 10Gbps.
10271      * When the link comes up the congestion management values are
10272      * re-calculated according to the actual link rate.
10273      */
10274     sc->link_vars.line_speed = SPEED_10000;
10275     bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10276
10277     /* Only the PMF sets the HW */
10278     if (sc->port.pmf) {
10279         storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10280     }
10281
10282     /* init Event Queue - PCI bus guarantees correct endainity */
10283     eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10284     eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10285     eq_data.producer     = sc->eq_prod;
10286     eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10287     eq_data.sb_id        = DEF_SB_ID;
10288     storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10289 }
10290
10291 static void
10292 bxe_hc_int_enable(struct bxe_softc *sc)
10293 {
10294     int port = SC_PORT(sc);
10295     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10296     uint32_t val = REG_RD(sc, addr);
10297     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10298     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10299                            (sc->intr_count == 1)) ? TRUE : FALSE;
10300     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10301
10302     if (msix) {
10303         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10304                  HC_CONFIG_0_REG_INT_LINE_EN_0);
10305         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10306                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10307         if (single_msix) {
10308             val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10309         }
10310     } else if (msi) {
10311         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10312         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10313                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10314                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10315     } else {
10316         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10317                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10318                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10319                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10320
10321         if (!CHIP_IS_E1(sc)) {
10322             BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10323                   val, port, addr);
10324
10325             REG_WR(sc, addr, val);
10326
10327             val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10328         }
10329     }
10330
10331     if (CHIP_IS_E1(sc)) {
10332         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10333     }
10334
10335     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10336           val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10337
10338     REG_WR(sc, addr, val);
10339
10340     /* ensure that HC_CONFIG is written before leading/trailing edge config */
10341     mb();
10342
10343     if (!CHIP_IS_E1(sc)) {
10344         /* init leading/trailing edge */
10345         if (IS_MF(sc)) {
10346             val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10347             if (sc->port.pmf) {
10348                 /* enable nig and gpio3 attention */
10349                 val |= 0x1100;
10350             }
10351         } else {
10352             val = 0xffff;
10353         }
10354
10355         REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10356         REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10357     }
10358
10359     /* make sure that interrupts are indeed enabled from here on */
10360     mb();
10361 }
10362
10363 static void
10364 bxe_igu_int_enable(struct bxe_softc *sc)
10365 {
10366     uint32_t val;
10367     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10368     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10369                            (sc->intr_count == 1)) ? TRUE : FALSE;
10370     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10371
10372     val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10373
10374     if (msix) {
10375         val &= ~(IGU_PF_CONF_INT_LINE_EN |
10376                  IGU_PF_CONF_SINGLE_ISR_EN);
10377         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10378                 IGU_PF_CONF_ATTN_BIT_EN);
10379         if (single_msix) {
10380             val |= IGU_PF_CONF_SINGLE_ISR_EN;
10381         }
10382     } else if (msi) {
10383         val &= ~IGU_PF_CONF_INT_LINE_EN;
10384         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10385                 IGU_PF_CONF_ATTN_BIT_EN |
10386                 IGU_PF_CONF_SINGLE_ISR_EN);
10387     } else {
10388         val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10389         val |= (IGU_PF_CONF_INT_LINE_EN |
10390                 IGU_PF_CONF_ATTN_BIT_EN |
10391                 IGU_PF_CONF_SINGLE_ISR_EN);
10392     }
10393
10394     /* clean previous status - need to configure igu prior to ack*/
10395     if ((!msix) || single_msix) {
10396         REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10397         bxe_ack_int(sc);
10398     }
10399
10400     val |= IGU_PF_CONF_FUNC_EN;
10401
10402     BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10403           val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10404
10405     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10406
10407     mb();
10408
10409     /* init leading/trailing edge */
10410     if (IS_MF(sc)) {
10411         val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10412         if (sc->port.pmf) {
10413             /* enable nig and gpio3 attention */
10414             val |= 0x1100;
10415         }
10416     } else {
10417         val = 0xffff;
10418     }
10419
10420     REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10421     REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10422
10423     /* make sure that interrupts are indeed enabled from here on */
10424     mb();
10425 }
10426
10427 static void
10428 bxe_int_enable(struct bxe_softc *sc)
10429 {
10430     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10431         bxe_hc_int_enable(sc);
10432     } else {
10433         bxe_igu_int_enable(sc);
10434     }
10435 }
10436
10437 static void
10438 bxe_hc_int_disable(struct bxe_softc *sc)
10439 {
10440     int port = SC_PORT(sc);
10441     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10442     uint32_t val = REG_RD(sc, addr);
10443
10444     /*
10445      * In E1 we must use only PCI configuration space to disable MSI/MSIX
10446      * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10447      * block
10448      */
10449     if (CHIP_IS_E1(sc)) {
10450         /*
10451          * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10452          * to prevent from HC sending interrupts after we exit the function
10453          */
10454         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10455
10456         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10457                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10458                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10459     } else {
10460         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10461                  HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10462                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10463                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10464     }
10465
10466     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10467
10468     /* flush all outstanding writes */
10469     mb();
10470
10471     REG_WR(sc, addr, val);
10472     if (REG_RD(sc, addr) != val) {
10473         BLOGE(sc, "proper val not read from HC IGU!\n");
10474     }
10475 }
10476
10477 static void
10478 bxe_igu_int_disable(struct bxe_softc *sc)
10479 {
10480     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10481
10482     val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10483              IGU_PF_CONF_INT_LINE_EN |
10484              IGU_PF_CONF_ATTN_BIT_EN);
10485
10486     BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10487
10488     /* flush all outstanding writes */
10489     mb();
10490
10491     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10492     if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10493         BLOGE(sc, "proper val not read from IGU!\n");
10494     }
10495 }
10496
10497 static void
10498 bxe_int_disable(struct bxe_softc *sc)
10499 {
10500     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10501         bxe_hc_int_disable(sc);
10502     } else {
10503         bxe_igu_int_disable(sc);
10504     }
10505 }
10506
10507 static void
10508 bxe_nic_init(struct bxe_softc *sc,
10509              int              load_code)
10510 {
10511     int i;
10512
10513     for (i = 0; i < sc->num_queues; i++) {
10514         bxe_init_eth_fp(sc, i);
10515     }
10516
10517     rmb(); /* ensure status block indices were read */
10518
10519     bxe_init_rx_rings(sc);
10520     bxe_init_tx_rings(sc);
10521
10522     if (IS_VF(sc)) {
10523         return;
10524     }
10525
10526     /* initialize MOD_ABS interrupts */
10527     elink_init_mod_abs_int(sc, &sc->link_vars,
10528                            sc->devinfo.chip_id,
10529                            sc->devinfo.shmem_base,
10530                            sc->devinfo.shmem2_base,
10531                            SC_PORT(sc));
10532
10533     bxe_init_def_sb(sc);
10534     bxe_update_dsb_idx(sc);
10535     bxe_init_sp_ring(sc);
10536     bxe_init_eq_ring(sc);
10537     bxe_init_internal(sc, load_code);
10538     bxe_pf_init(sc);
10539     bxe_stats_init(sc);
10540
10541     /* flush all before enabling interrupts */
10542     mb();
10543
10544     bxe_int_enable(sc);
10545
10546     /* check for SPIO5 */
10547     bxe_attn_int_deasserted0(sc,
10548                              REG_RD(sc,
10549                                     (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10550                                      SC_PORT(sc)*4)) &
10551                              AEU_INPUTS_ATTN_BITS_SPIO5);
10552 }
10553
10554 static inline void
10555 bxe_init_objs(struct bxe_softc *sc)
10556 {
10557     /* mcast rules must be added to tx if tx switching is enabled */
10558     ecore_obj_type o_type =
10559         (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10560                                          ECORE_OBJ_TYPE_RX;
10561
10562     /* RX_MODE controlling object */
10563     ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10564
10565     /* multicast configuration controlling object */
10566     ecore_init_mcast_obj(sc,
10567                          &sc->mcast_obj,
10568                          sc->fp[0].cl_id,
10569                          sc->fp[0].index,
10570                          SC_FUNC(sc),
10571                          SC_FUNC(sc),
10572                          BXE_SP(sc, mcast_rdata),
10573                          BXE_SP_MAPPING(sc, mcast_rdata),
10574                          ECORE_FILTER_MCAST_PENDING,
10575                          &sc->sp_state,
10576                          o_type);
10577
10578     /* Setup CAM credit pools */
10579     ecore_init_mac_credit_pool(sc,
10580                                &sc->macs_pool,
10581                                SC_FUNC(sc),
10582                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10583                                                  VNICS_PER_PATH(sc));
10584
10585     ecore_init_vlan_credit_pool(sc,
10586                                 &sc->vlans_pool,
10587                                 SC_ABS_FUNC(sc) >> 1,
10588                                 CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10589                                                   VNICS_PER_PATH(sc));
10590
10591     /* RSS configuration object */
10592     ecore_init_rss_config_obj(sc,
10593                               &sc->rss_conf_obj,
10594                               sc->fp[0].cl_id,
10595                               sc->fp[0].index,
10596                               SC_FUNC(sc),
10597                               SC_FUNC(sc),
10598                               BXE_SP(sc, rss_rdata),
10599                               BXE_SP_MAPPING(sc, rss_rdata),
10600                               ECORE_FILTER_RSS_CONF_PENDING,
10601                               &sc->sp_state, ECORE_OBJ_TYPE_RX);
10602 }
10603
10604 /*
10605  * Initialize the function. This must be called before sending CLIENT_SETUP
10606  * for the first client.
10607  */
10608 static inline int
10609 bxe_func_start(struct bxe_softc *sc)
10610 {
10611     struct ecore_func_state_params func_params = { NULL };
10612     struct ecore_func_start_params *start_params = &func_params.params.start;
10613
10614     /* Prepare parameters for function state transitions */
10615     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
10616
10617     func_params.f_obj = &sc->func_obj;
10618     func_params.cmd = ECORE_F_CMD_START;
10619
10620     /* Function parameters */
10621     start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
10622     start_params->sd_vlan_tag = OVLAN(sc);
10623
10624     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
10625         start_params->network_cos_mode = STATIC_COS;
10626     } else { /* CHIP_IS_E1X */
10627         start_params->network_cos_mode = FW_WRR;
10628     }
10629
10630     //start_params->gre_tunnel_mode = 0;
10631     //start_params->gre_tunnel_rss  = 0;
10632
10633     return (ecore_func_state_change(sc, &func_params));
10634 }
10635
10636 static int
10637 bxe_set_power_state(struct bxe_softc *sc,
10638                     uint8_t          state)
10639 {
10640     uint16_t pmcsr;
10641
10642     /* If there is no power capability, silently succeed */
10643     if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
10644         BLOGW(sc, "No power capability\n");
10645         return (0);
10646     }
10647
10648     pmcsr = pci_read_config(sc->dev,
10649                             (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10650                             2);
10651
10652     switch (state) {
10653     case PCI_PM_D0:
10654         pci_write_config(sc->dev,
10655                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10656                          ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
10657
10658         if (pmcsr & PCIM_PSTAT_DMASK) {
10659             /* delay required during transition out of D3hot */
10660             DELAY(20000);
10661         }
10662
10663         break;
10664
10665     case PCI_PM_D3hot:
10666         /* XXX if there are other clients above don't shut down the power */
10667
10668         /* don't shut down the power for emulation and FPGA */
10669         if (CHIP_REV_IS_SLOW(sc)) {
10670             return (0);
10671         }
10672
10673         pmcsr &= ~PCIM_PSTAT_DMASK;
10674         pmcsr |= PCIM_PSTAT_D3;
10675
10676         if (sc->wol) {
10677             pmcsr |= PCIM_PSTAT_PMEENABLE;
10678         }
10679
10680         pci_write_config(sc->dev,
10681                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
10682                          pmcsr, 4);
10683
10684         /*
10685          * No more memory access after this point until device is brought back
10686          * to D0 state.
10687          */
10688         break;
10689
10690     default:
10691         BLOGE(sc, "Can't support PCI power state = 0x%x pmcsr 0x%x\n",
10692             state, pmcsr);
10693         return (-1);
10694     }
10695
10696     return (0);
10697 }
10698
10699
10700 /* return true if succeeded to acquire the lock */
10701 static uint8_t
10702 bxe_trylock_hw_lock(struct bxe_softc *sc,
10703                     uint32_t         resource)
10704 {
10705     uint32_t lock_status;
10706     uint32_t resource_bit = (1 << resource);
10707     int func = SC_FUNC(sc);
10708     uint32_t hw_lock_control_reg;
10709
10710     BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
10711
10712     /* Validating that the resource is within range */
10713     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
10714         BLOGD(sc, DBG_LOAD,
10715               "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
10716               resource, HW_LOCK_MAX_RESOURCE_VALUE);
10717         return (FALSE);
10718     }
10719
10720     if (func <= 5) {
10721         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
10722     } else {
10723         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
10724     }
10725
10726     /* try to acquire the lock */
10727     REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
10728     lock_status = REG_RD(sc, hw_lock_control_reg);
10729     if (lock_status & resource_bit) {
10730         return (TRUE);
10731     }
10732
10733     BLOGE(sc, "Failed to get a resource lock 0x%x func %d "
10734         "lock_status 0x%x resource_bit 0x%x\n", resource, func,
10735         lock_status, resource_bit);
10736
10737     return (FALSE);
10738 }
10739
10740 /*
10741  * Get the recovery leader resource id according to the engine this function
10742  * belongs to. Currently only only 2 engines is supported.
10743  */
10744 static int
10745 bxe_get_leader_lock_resource(struct bxe_softc *sc)
10746 {
10747     if (SC_PATH(sc)) {
10748         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
10749     } else {
10750         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
10751     }
10752 }
10753
10754 /* try to acquire a leader lock for current engine */
10755 static uint8_t
10756 bxe_trylock_leader_lock(struct bxe_softc *sc)
10757 {
10758     return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10759 }
10760
10761 static int
10762 bxe_release_leader_lock(struct bxe_softc *sc)
10763 {
10764     return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
10765 }
10766
10767 /* close gates #2, #3 and #4 */
10768 static void
10769 bxe_set_234_gates(struct bxe_softc *sc,
10770                   uint8_t          close)
10771 {
10772     uint32_t val;
10773
10774     /* gates #2 and #4a are closed/opened for "not E1" only */
10775     if (!CHIP_IS_E1(sc)) {
10776         /* #4 */
10777         REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
10778         /* #2 */
10779         REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
10780     }
10781
10782     /* #3 */
10783     if (CHIP_IS_E1x(sc)) {
10784         /* prevent interrupts from HC on both ports */
10785         val = REG_RD(sc, HC_REG_CONFIG_1);
10786         REG_WR(sc, HC_REG_CONFIG_1,
10787                (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
10788                (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
10789
10790         val = REG_RD(sc, HC_REG_CONFIG_0);
10791         REG_WR(sc, HC_REG_CONFIG_0,
10792                (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
10793                (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
10794     } else {
10795         /* Prevent incomming interrupts in IGU */
10796         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
10797
10798         REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
10799                (!close) ?
10800                (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
10801                (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
10802     }
10803
10804     BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
10805           close ? "closing" : "opening");
10806
10807     wmb();
10808 }
10809
10810 /* poll for pending writes bit, it should get cleared in no more than 1s */
10811 static int
10812 bxe_er_poll_igu_vq(struct bxe_softc *sc)
10813 {
10814     uint32_t cnt = 1000;
10815     uint32_t pend_bits = 0;
10816
10817     do {
10818         pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
10819
10820         if (pend_bits == 0) {
10821             break;
10822         }
10823
10824         DELAY(1000);
10825     } while (--cnt > 0);
10826
10827     if (cnt == 0) {
10828         BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
10829         return (-1);
10830     }
10831
10832     return (0);
10833 }
10834
10835 #define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
10836
10837 static void
10838 bxe_clp_reset_prep(struct bxe_softc *sc,
10839                    uint32_t         *magic_val)
10840 {
10841     /* Do some magic... */
10842     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10843     *magic_val = val & SHARED_MF_CLP_MAGIC;
10844     MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
10845 }
10846
10847 /* restore the value of the 'magic' bit */
10848 static void
10849 bxe_clp_reset_done(struct bxe_softc *sc,
10850                    uint32_t         magic_val)
10851 {
10852     /* Restore the 'magic' bit value... */
10853     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
10854     MFCFG_WR(sc, shared_mf_config.clp_mb,
10855               (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
10856 }
10857
10858 /* prepare for MCP reset, takes care of CLP configurations */
10859 static void
10860 bxe_reset_mcp_prep(struct bxe_softc *sc,
10861                    uint32_t         *magic_val)
10862 {
10863     uint32_t shmem;
10864     uint32_t validity_offset;
10865
10866     /* set `magic' bit in order to save MF config */
10867     if (!CHIP_IS_E1(sc)) {
10868         bxe_clp_reset_prep(sc, magic_val);
10869     }
10870
10871     /* get shmem offset */
10872     shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10873     validity_offset =
10874         offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
10875
10876     /* Clear validity map flags */
10877     if (shmem > 0) {
10878         REG_WR(sc, shmem + validity_offset, 0);
10879     }
10880 }
10881
10882 #define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
10883 #define MCP_ONE_TIMEOUT  100    /* 100 ms */
10884
10885 static void
10886 bxe_mcp_wait_one(struct bxe_softc *sc)
10887 {
10888     /* special handling for emulation and FPGA (10 times longer) */
10889     if (CHIP_REV_IS_SLOW(sc)) {
10890         DELAY((MCP_ONE_TIMEOUT*10) * 1000);
10891     } else {
10892         DELAY((MCP_ONE_TIMEOUT) * 1000);
10893     }
10894 }
10895
10896 /* initialize shmem_base and waits for validity signature to appear */
10897 static int
10898 bxe_init_shmem(struct bxe_softc *sc)
10899 {
10900     int cnt = 0;
10901     uint32_t val = 0;
10902
10903     do {
10904         sc->devinfo.shmem_base     =
10905         sc->link_params.shmem_base =
10906             REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
10907
10908         if (sc->devinfo.shmem_base) {
10909             val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
10910             if (val & SHR_MEM_VALIDITY_MB)
10911                 return (0);
10912         }
10913
10914         bxe_mcp_wait_one(sc);
10915
10916     } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
10917
10918     BLOGE(sc, "BAD MCP validity signature\n");
10919
10920     return (-1);
10921 }
10922
10923 static int
10924 bxe_reset_mcp_comp(struct bxe_softc *sc,
10925                    uint32_t         magic_val)
10926 {
10927     int rc = bxe_init_shmem(sc);
10928
10929     /* Restore the `magic' bit value */
10930     if (!CHIP_IS_E1(sc)) {
10931         bxe_clp_reset_done(sc, magic_val);
10932     }
10933
10934     return (rc);
10935 }
10936
10937 static void
10938 bxe_pxp_prep(struct bxe_softc *sc)
10939 {
10940     if (!CHIP_IS_E1(sc)) {
10941         REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
10942         REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
10943         wmb();
10944     }
10945 }
10946
10947 /*
10948  * Reset the whole chip except for:
10949  *      - PCIE core
10950  *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
10951  *      - IGU
10952  *      - MISC (including AEU)
10953  *      - GRC
10954  *      - RBCN, RBCP
10955  */
10956 static void
10957 bxe_process_kill_chip_reset(struct bxe_softc *sc,
10958                             uint8_t          global)
10959 {
10960     uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
10961     uint32_t global_bits2, stay_reset2;
10962
10963     /*
10964      * Bits that have to be set in reset_mask2 if we want to reset 'global'
10965      * (per chip) blocks.
10966      */
10967     global_bits2 =
10968         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
10969         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
10970
10971     /*
10972      * Don't reset the following blocks.
10973      * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
10974      *            reset, as in 4 port device they might still be owned
10975      *            by the MCP (there is only one leader per path).
10976      */
10977     not_reset_mask1 =
10978         MISC_REGISTERS_RESET_REG_1_RST_HC |
10979         MISC_REGISTERS_RESET_REG_1_RST_PXPV |
10980         MISC_REGISTERS_RESET_REG_1_RST_PXP;
10981
10982     not_reset_mask2 =
10983         MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
10984         MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
10985         MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
10986         MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
10987         MISC_REGISTERS_RESET_REG_2_RST_RBCN |
10988         MISC_REGISTERS_RESET_REG_2_RST_GRC  |
10989         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
10990         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
10991         MISC_REGISTERS_RESET_REG_2_RST_ATC |
10992         MISC_REGISTERS_RESET_REG_2_PGLC |
10993         MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
10994         MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
10995         MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
10996         MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
10997         MISC_REGISTERS_RESET_REG_2_UMAC0 |
10998         MISC_REGISTERS_RESET_REG_2_UMAC1;
10999
11000     /*
11001      * Keep the following blocks in reset:
11002      *  - all xxMACs are handled by the elink code.
11003      */
11004     stay_reset2 =
11005         MISC_REGISTERS_RESET_REG_2_XMAC |
11006         MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
11007
11008     /* Full reset masks according to the chip */
11009     reset_mask1 = 0xffffffff;
11010
11011     if (CHIP_IS_E1(sc))
11012         reset_mask2 = 0xffff;
11013     else if (CHIP_IS_E1H(sc))
11014         reset_mask2 = 0x1ffff;
11015     else if (CHIP_IS_E2(sc))
11016         reset_mask2 = 0xfffff;
11017     else /* CHIP_IS_E3 */
11018         reset_mask2 = 0x3ffffff;
11019
11020     /* Don't reset global blocks unless we need to */
11021     if (!global)
11022         reset_mask2 &= ~global_bits2;
11023
11024     /*
11025      * In case of attention in the QM, we need to reset PXP
11026      * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
11027      * because otherwise QM reset would release 'close the gates' shortly
11028      * before resetting the PXP, then the PSWRQ would send a write
11029      * request to PGLUE. Then when PXP is reset, PGLUE would try to
11030      * read the payload data from PSWWR, but PSWWR would not
11031      * respond. The write queue in PGLUE would stuck, dmae commands
11032      * would not return. Therefore it's important to reset the second
11033      * reset register (containing the
11034      * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
11035      * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
11036      * bit).
11037      */
11038     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
11039            reset_mask2 & (~not_reset_mask2));
11040
11041     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
11042            reset_mask1 & (~not_reset_mask1));
11043
11044     mb();
11045     wmb();
11046
11047     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
11048            reset_mask2 & (~stay_reset2));
11049
11050     mb();
11051     wmb();
11052
11053     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
11054     wmb();
11055 }
11056
11057 static int
11058 bxe_process_kill(struct bxe_softc *sc,
11059                  uint8_t          global)
11060 {
11061     int cnt = 1000;
11062     uint32_t val = 0;
11063     uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
11064     uint32_t tags_63_32 = 0;
11065
11066     /* Empty the Tetris buffer, wait for 1s */
11067     do {
11068         sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
11069         blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
11070         port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
11071         port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
11072         pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
11073         if (CHIP_IS_E3(sc)) {
11074             tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
11075         }
11076
11077         if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
11078             ((port_is_idle_0 & 0x1) == 0x1) &&
11079             ((port_is_idle_1 & 0x1) == 0x1) &&
11080             (pgl_exp_rom2 == 0xffffffff) &&
11081             (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11082             break;
11083         DELAY(1000);
11084     } while (cnt-- > 0);
11085
11086     if (cnt <= 0) {
11087         BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11088                   "are still outstanding read requests after 1s! "
11089                   "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11090                   "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11091               sr_cnt, blk_cnt, port_is_idle_0,
11092               port_is_idle_1, pgl_exp_rom2);
11093         return (-1);
11094     }
11095
11096     mb();
11097
11098     /* Close gates #2, #3 and #4 */
11099     bxe_set_234_gates(sc, TRUE);
11100
11101     /* Poll for IGU VQs for 57712 and newer chips */
11102     if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11103         return (-1);
11104     }
11105
11106     /* XXX indicate that "process kill" is in progress to MCP */
11107
11108     /* clear "unprepared" bit */
11109     REG_WR(sc, MISC_REG_UNPREPARED, 0);
11110     mb();
11111
11112     /* Make sure all is written to the chip before the reset */
11113     wmb();
11114
11115     /*
11116      * Wait for 1ms to empty GLUE and PCI-E core queues,
11117      * PSWHST, GRC and PSWRD Tetris buffer.
11118      */
11119     DELAY(1000);
11120
11121     /* Prepare to chip reset: */
11122     /* MCP */
11123     if (global) {
11124         bxe_reset_mcp_prep(sc, &val);
11125     }
11126
11127     /* PXP */
11128     bxe_pxp_prep(sc);
11129     mb();
11130
11131     /* reset the chip */
11132     bxe_process_kill_chip_reset(sc, global);
11133     mb();
11134
11135     /* clear errors in PGB */
11136     if (!CHIP_IS_E1(sc))
11137         REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11138
11139     /* Recover after reset: */
11140     /* MCP */
11141     if (global && bxe_reset_mcp_comp(sc, val)) {
11142         return (-1);
11143     }
11144
11145     /* XXX add resetting the NO_MCP mode DB here */
11146
11147     /* Open the gates #2, #3 and #4 */
11148     bxe_set_234_gates(sc, FALSE);
11149
11150     /* XXX
11151      * IGU/AEU preparation bring back the AEU/IGU to a reset state
11152      * re-enable attentions
11153      */
11154
11155     return (0);
11156 }
11157
11158 static int
11159 bxe_leader_reset(struct bxe_softc *sc)
11160 {
11161     int rc = 0;
11162     uint8_t global = bxe_reset_is_global(sc);
11163     uint32_t load_code;
11164
11165     /*
11166      * If not going to reset MCP, load "fake" driver to reset HW while
11167      * driver is owner of the HW.
11168      */
11169     if (!global && !BXE_NOMCP(sc)) {
11170         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11171                                    DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11172         if (!load_code) {
11173             BLOGE(sc, "MCP response failure, aborting\n");
11174             rc = -1;
11175             goto exit_leader_reset;
11176         }
11177
11178         if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11179             (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11180             BLOGE(sc, "MCP unexpected response, aborting\n");
11181             rc = -1;
11182             goto exit_leader_reset2;
11183         }
11184
11185         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11186         if (!load_code) {
11187             BLOGE(sc, "MCP response failure, aborting\n");
11188             rc = -1;
11189             goto exit_leader_reset2;
11190         }
11191     }
11192
11193     /* try to recover after the failure */
11194     if (bxe_process_kill(sc, global)) {
11195         BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11196         rc = -1;
11197         goto exit_leader_reset2;
11198     }
11199
11200     /*
11201      * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11202      * state.
11203      */
11204     bxe_set_reset_done(sc);
11205     if (global) {
11206         bxe_clear_reset_global(sc);
11207     }
11208
11209 exit_leader_reset2:
11210
11211     /* unload "fake driver" if it was loaded */
11212     if (!global && !BXE_NOMCP(sc)) {
11213         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11214         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11215     }
11216
11217 exit_leader_reset:
11218
11219     sc->is_leader = 0;
11220     bxe_release_leader_lock(sc);
11221
11222     mb();
11223     return (rc);
11224 }
11225
11226 /*
11227  * prepare INIT transition, parameters configured:
11228  *   - HC configuration
11229  *   - Queue's CDU context
11230  */
11231 static void
11232 bxe_pf_q_prep_init(struct bxe_softc               *sc,
11233                    struct bxe_fastpath            *fp,
11234                    struct ecore_queue_init_params *init_params)
11235 {
11236     uint8_t cos;
11237     int cxt_index, cxt_offset;
11238
11239     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11240     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11241
11242     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11243     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11244
11245     /* HC rate */
11246     init_params->rx.hc_rate =
11247         sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11248     init_params->tx.hc_rate =
11249         sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11250
11251     /* FW SB ID */
11252     init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11253
11254     /* CQ index among the SB indices */
11255     init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11256     init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11257
11258     /* set maximum number of COSs supported by this queue */
11259     init_params->max_cos = sc->max_cos;
11260
11261     BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11262           fp->index, init_params->max_cos);
11263
11264     /* set the context pointers queue object */
11265     for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11266         /* XXX change index/cid here if ever support multiple tx CoS */
11267         /* fp->txdata[cos]->cid */
11268         cxt_index = fp->index / ILT_PAGE_CIDS;
11269         cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11270         init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11271     }
11272 }
11273
11274 /* set flags that are common for the Tx-only and not normal connections */
11275 static unsigned long
11276 bxe_get_common_flags(struct bxe_softc    *sc,
11277                      struct bxe_fastpath *fp,
11278                      uint8_t             zero_stats)
11279 {
11280     unsigned long flags = 0;
11281
11282     /* PF driver will always initialize the Queue to an ACTIVE state */
11283     bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11284
11285     /*
11286      * tx only connections collect statistics (on the same index as the
11287      * parent connection). The statistics are zeroed when the parent
11288      * connection is initialized.
11289      */
11290
11291     bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11292     if (zero_stats) {
11293         bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11294     }
11295
11296     /*
11297      * tx only connections can support tx-switching, though their
11298      * CoS-ness doesn't survive the loopback
11299      */
11300     if (sc->flags & BXE_TX_SWITCHING) {
11301         bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11302     }
11303
11304     bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11305
11306     return (flags);
11307 }
11308
11309 static unsigned long
11310 bxe_get_q_flags(struct bxe_softc    *sc,
11311                 struct bxe_fastpath *fp,
11312                 uint8_t             leading)
11313 {
11314     unsigned long flags = 0;
11315
11316     if (IS_MF_SD(sc)) {
11317         bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11318     }
11319
11320     if (sc->ifnet->if_capenable & IFCAP_LRO) {
11321         bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11322 #if __FreeBSD_version >= 800000
11323         bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11324 #endif
11325     }
11326
11327     if (leading) {
11328         bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11329         bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11330     }
11331
11332     bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11333
11334     /* merge with common flags */
11335     return (flags | bxe_get_common_flags(sc, fp, TRUE));
11336 }
11337
11338 static void
11339 bxe_pf_q_prep_general(struct bxe_softc                  *sc,
11340                       struct bxe_fastpath               *fp,
11341                       struct ecore_general_setup_params *gen_init,
11342                       uint8_t                           cos)
11343 {
11344     gen_init->stat_id = bxe_stats_id(fp);
11345     gen_init->spcl_id = fp->cl_id;
11346     gen_init->mtu = sc->mtu;
11347     gen_init->cos = cos;
11348 }
11349
11350 static void
11351 bxe_pf_rx_q_prep(struct bxe_softc              *sc,
11352                  struct bxe_fastpath           *fp,
11353                  struct rxq_pause_params       *pause,
11354                  struct ecore_rxq_setup_params *rxq_init)
11355 {
11356     uint8_t max_sge = 0;
11357     uint16_t sge_sz = 0;
11358     uint16_t tpa_agg_size = 0;
11359
11360     pause->sge_th_lo = SGE_TH_LO(sc);
11361     pause->sge_th_hi = SGE_TH_HI(sc);
11362
11363     /* validate SGE ring has enough to cross high threshold */
11364     if (sc->dropless_fc &&
11365             (pause->sge_th_hi + FW_PREFETCH_CNT) >
11366             (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11367         BLOGW(sc, "sge ring threshold limit\n");
11368     }
11369
11370     /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11371     tpa_agg_size = (2 * sc->mtu);
11372     if (tpa_agg_size < sc->max_aggregation_size) {
11373         tpa_agg_size = sc->max_aggregation_size;
11374     }
11375
11376     max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11377     max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11378                    (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11379     sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11380
11381     /* pause - not for e1 */
11382     if (!CHIP_IS_E1(sc)) {
11383         pause->bd_th_lo = BD_TH_LO(sc);
11384         pause->bd_th_hi = BD_TH_HI(sc);
11385
11386         pause->rcq_th_lo = RCQ_TH_LO(sc);
11387         pause->rcq_th_hi = RCQ_TH_HI(sc);
11388
11389         /* validate rings have enough entries to cross high thresholds */
11390         if (sc->dropless_fc &&
11391             pause->bd_th_hi + FW_PREFETCH_CNT >
11392             sc->rx_ring_size) {
11393             BLOGW(sc, "rx bd ring threshold limit\n");
11394         }
11395
11396         if (sc->dropless_fc &&
11397             pause->rcq_th_hi + FW_PREFETCH_CNT >
11398             RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11399             BLOGW(sc, "rcq ring threshold limit\n");
11400         }
11401
11402         pause->pri_map = 1;
11403     }
11404
11405     /* rxq setup */
11406     rxq_init->dscr_map   = fp->rx_dma.paddr;
11407     rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11408     rxq_init->rcq_map    = fp->rcq_dma.paddr;
11409     rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11410
11411     /*
11412      * This should be a maximum number of data bytes that may be
11413      * placed on the BD (not including paddings).
11414      */
11415     rxq_init->buf_sz = (fp->rx_buf_size -
11416                         IP_HEADER_ALIGNMENT_PADDING);
11417
11418     rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11419     rxq_init->tpa_agg_sz      = tpa_agg_size;
11420     rxq_init->sge_buf_sz      = sge_sz;
11421     rxq_init->max_sges_pkt    = max_sge;
11422     rxq_init->rss_engine_id   = SC_FUNC(sc);
11423     rxq_init->mcast_engine_id = SC_FUNC(sc);
11424
11425     /*
11426      * Maximum number or simultaneous TPA aggregation for this Queue.
11427      * For PF Clients it should be the maximum available number.
11428      * VF driver(s) may want to define it to a smaller value.
11429      */
11430     rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11431
11432     rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11433     rxq_init->fw_sb_id = fp->fw_sb_id;
11434
11435     rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11436
11437     /*
11438      * configure silent vlan removal
11439      * if multi function mode is afex, then mask default vlan
11440      */
11441     if (IS_MF_AFEX(sc)) {
11442         rxq_init->silent_removal_value =
11443             sc->devinfo.mf_info.afex_def_vlan_tag;
11444         rxq_init->silent_removal_mask = EVL_VLID_MASK;
11445     }
11446 }
11447
11448 static void
11449 bxe_pf_tx_q_prep(struct bxe_softc              *sc,
11450                  struct bxe_fastpath           *fp,
11451                  struct ecore_txq_setup_params *txq_init,
11452                  uint8_t                       cos)
11453 {
11454     /*
11455      * XXX If multiple CoS is ever supported then each fastpath structure
11456      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11457      * fp->txdata[cos]->tx_dma.paddr;
11458      */
11459     txq_init->dscr_map     = fp->tx_dma.paddr;
11460     txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11461     txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11462     txq_init->fw_sb_id     = fp->fw_sb_id;
11463
11464     /*
11465      * set the TSS leading client id for TX classfication to the
11466      * leading RSS client id
11467      */
11468     txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11469 }
11470
11471 /*
11472  * This function performs 2 steps in a queue state machine:
11473  *   1) RESET->INIT
11474  *   2) INIT->SETUP
11475  */
11476 static int
11477 bxe_setup_queue(struct bxe_softc    *sc,
11478                 struct bxe_fastpath *fp,
11479                 uint8_t             leading)
11480 {
11481     struct ecore_queue_state_params q_params = { NULL };
11482     struct ecore_queue_setup_params *setup_params =
11483                         &q_params.params.setup;
11484     int rc;
11485
11486     BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11487
11488     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11489
11490     q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11491
11492     /* we want to wait for completion in this context */
11493     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11494
11495     /* prepare the INIT parameters */
11496     bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11497
11498     /* Set the command */
11499     q_params.cmd = ECORE_Q_CMD_INIT;
11500
11501     /* Change the state to INIT */
11502     rc = ecore_queue_state_change(sc, &q_params);
11503     if (rc) {
11504         BLOGE(sc, "Queue(%d) INIT failed rc = %d\n", fp->index, rc);
11505         return (rc);
11506     }
11507
11508     BLOGD(sc, DBG_LOAD, "init complete\n");
11509
11510     /* now move the Queue to the SETUP state */
11511     memset(setup_params, 0, sizeof(*setup_params));
11512
11513     /* set Queue flags */
11514     setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11515
11516     /* set general SETUP parameters */
11517     bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11518                           FIRST_TX_COS_INDEX);
11519
11520     bxe_pf_rx_q_prep(sc, fp,
11521                      &setup_params->pause_params,
11522                      &setup_params->rxq_params);
11523
11524     bxe_pf_tx_q_prep(sc, fp,
11525                      &setup_params->txq_params,
11526                      FIRST_TX_COS_INDEX);
11527
11528     /* Set the command */
11529     q_params.cmd = ECORE_Q_CMD_SETUP;
11530
11531     /* change the state to SETUP */
11532     rc = ecore_queue_state_change(sc, &q_params);
11533     if (rc) {
11534         BLOGE(sc, "Queue(%d) SETUP failed (rc = %d)\n", fp->index, rc);
11535         return (rc);
11536     }
11537
11538     return (rc);
11539 }
11540
11541 static int
11542 bxe_setup_leading(struct bxe_softc *sc)
11543 {
11544     return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11545 }
11546
11547 static int
11548 bxe_config_rss_pf(struct bxe_softc            *sc,
11549                   struct ecore_rss_config_obj *rss_obj,
11550                   uint8_t                     config_hash)
11551 {
11552     struct ecore_config_rss_params params = { NULL };
11553     int i;
11554
11555     /*
11556      * Although RSS is meaningless when there is a single HW queue we
11557      * still need it enabled in order to have HW Rx hash generated.
11558      */
11559
11560     params.rss_obj = rss_obj;
11561
11562     bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11563
11564     bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11565
11566     /* RSS configuration */
11567     bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11568     bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11569     bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11570     bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11571     if (rss_obj->udp_rss_v4) {
11572         bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
11573     }
11574     if (rss_obj->udp_rss_v6) {
11575         bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
11576     }
11577
11578     /* Hash bits */
11579     params.rss_result_mask = MULTI_MASK;
11580
11581     memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
11582
11583     if (config_hash) {
11584         /* RSS keys */
11585         for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
11586             params.rss_key[i] = arc4random();
11587         }
11588
11589         bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
11590     }
11591
11592     return (ecore_config_rss(sc, &params));
11593 }
11594
11595 static int
11596 bxe_config_rss_eth(struct bxe_softc *sc,
11597                    uint8_t          config_hash)
11598 {
11599     return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
11600 }
11601
11602 static int
11603 bxe_init_rss_pf(struct bxe_softc *sc)
11604 {
11605     uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
11606     int i;
11607
11608     /*
11609      * Prepare the initial contents of the indirection table if
11610      * RSS is enabled
11611      */
11612     for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
11613         sc->rss_conf_obj.ind_table[i] =
11614             (sc->fp->cl_id + (i % num_eth_queues));
11615     }
11616
11617     if (sc->udp_rss) {
11618         sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
11619     }
11620
11621     /*
11622      * For 57710 and 57711 SEARCHER configuration (rss_keys) is
11623      * per-port, so if explicit configuration is needed, do it only
11624      * for a PMF.
11625      *
11626      * For 57712 and newer it's a per-function configuration.
11627      */
11628     return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
11629 }
11630
11631 static int
11632 bxe_set_mac_one(struct bxe_softc          *sc,
11633                 uint8_t                   *mac,
11634                 struct ecore_vlan_mac_obj *obj,
11635                 uint8_t                   set,
11636                 int                       mac_type,
11637                 unsigned long             *ramrod_flags)
11638 {
11639     struct ecore_vlan_mac_ramrod_params ramrod_param;
11640     int rc;
11641
11642     memset(&ramrod_param, 0, sizeof(ramrod_param));
11643
11644     /* fill in general parameters */
11645     ramrod_param.vlan_mac_obj = obj;
11646     ramrod_param.ramrod_flags = *ramrod_flags;
11647
11648     /* fill a user request section if needed */
11649     if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
11650         memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
11651
11652         bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
11653
11654         /* Set the command: ADD or DEL */
11655         ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
11656                                             ECORE_VLAN_MAC_DEL;
11657     }
11658
11659     rc = ecore_config_vlan_mac(sc, &ramrod_param);
11660
11661     if (rc == ECORE_EXISTS) {
11662         BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
11663         /* do not treat adding same MAC as error */
11664         rc = 0;
11665     } else if (rc < 0) {
11666         BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
11667     }
11668
11669     return (rc);
11670 }
11671
11672 static int
11673 bxe_set_eth_mac(struct bxe_softc *sc,
11674                 uint8_t          set)
11675 {
11676     unsigned long ramrod_flags = 0;
11677
11678     BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
11679
11680     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
11681
11682     /* Eth MAC is set on RSS leading client (fp[0]) */
11683     return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
11684                             &sc->sp_objs->mac_obj,
11685                             set, ECORE_ETH_MAC, &ramrod_flags));
11686 }
11687
11688 static int
11689 bxe_get_cur_phy_idx(struct bxe_softc *sc)
11690 {
11691     uint32_t sel_phy_idx = 0;
11692
11693     if (sc->link_params.num_phys <= 1) {
11694         return (ELINK_INT_PHY);
11695     }
11696
11697     if (sc->link_vars.link_up) {
11698         sel_phy_idx = ELINK_EXT_PHY1;
11699         /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
11700         if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
11701             (sc->link_params.phy[ELINK_EXT_PHY2].supported &
11702              ELINK_SUPPORTED_FIBRE))
11703             sel_phy_idx = ELINK_EXT_PHY2;
11704     } else {
11705         switch (elink_phy_selection(&sc->link_params)) {
11706         case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
11707         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
11708         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
11709                sel_phy_idx = ELINK_EXT_PHY1;
11710                break;
11711         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
11712         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
11713                sel_phy_idx = ELINK_EXT_PHY2;
11714                break;
11715         }
11716     }
11717
11718     return (sel_phy_idx);
11719 }
11720
11721 static int
11722 bxe_get_link_cfg_idx(struct bxe_softc *sc)
11723 {
11724     uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
11725
11726     /*
11727      * The selected activated PHY is always after swapping (in case PHY
11728      * swapping is enabled). So when swapping is enabled, we need to reverse
11729      * the configuration
11730      */
11731
11732     if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11733         if (sel_phy_idx == ELINK_EXT_PHY1)
11734             sel_phy_idx = ELINK_EXT_PHY2;
11735         else if (sel_phy_idx == ELINK_EXT_PHY2)
11736             sel_phy_idx = ELINK_EXT_PHY1;
11737     }
11738
11739     return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
11740 }
11741
11742 static void
11743 bxe_set_requested_fc(struct bxe_softc *sc)
11744 {
11745     /*
11746      * Initialize link parameters structure variables
11747      * It is recommended to turn off RX FC for jumbo frames
11748      * for better performance
11749      */
11750     if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
11751         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
11752     } else {
11753         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
11754     }
11755 }
11756
11757 static void
11758 bxe_calc_fc_adv(struct bxe_softc *sc)
11759 {
11760     uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
11761
11762
11763     sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
11764                                            ADVERTISED_Pause);
11765
11766     switch (sc->link_vars.ieee_fc &
11767             MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
11768
11769     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
11770         sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
11771                                           ADVERTISED_Pause);
11772         break;
11773
11774     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
11775         sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
11776         break;
11777
11778     default:
11779         break;
11780
11781     }
11782 }
11783
11784 static uint16_t
11785 bxe_get_mf_speed(struct bxe_softc *sc)
11786 {
11787     uint16_t line_speed = sc->link_vars.line_speed;
11788     if (IS_MF(sc)) {
11789         uint16_t maxCfg =
11790             bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
11791
11792         /* calculate the current MAX line speed limit for the MF devices */
11793         if (IS_MF_SI(sc)) {
11794             line_speed = (line_speed * maxCfg) / 100;
11795         } else { /* SD mode */
11796             uint16_t vn_max_rate = maxCfg * 100;
11797
11798             if (vn_max_rate < line_speed) {
11799                 line_speed = vn_max_rate;
11800             }
11801         }
11802     }
11803
11804     return (line_speed);
11805 }
11806
11807 static void
11808 bxe_fill_report_data(struct bxe_softc            *sc,
11809                      struct bxe_link_report_data *data)
11810 {
11811     uint16_t line_speed = bxe_get_mf_speed(sc);
11812
11813     memset(data, 0, sizeof(*data));
11814
11815     /* fill the report data with the effective line speed */
11816     data->line_speed = line_speed;
11817
11818     /* Link is down */
11819     if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
11820         bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
11821     }
11822
11823     /* Full DUPLEX */
11824     if (sc->link_vars.duplex == DUPLEX_FULL) {
11825         bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
11826     }
11827
11828     /* Rx Flow Control is ON */
11829     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
11830         bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
11831     }
11832
11833     /* Tx Flow Control is ON */
11834     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
11835         bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
11836     }
11837 }
11838
11839 /* report link status to OS, should be called under phy_lock */
11840 static void
11841 bxe_link_report_locked(struct bxe_softc *sc)
11842 {
11843     struct bxe_link_report_data cur_data;
11844
11845     /* reread mf_cfg */
11846     if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
11847         bxe_read_mf_cfg(sc);
11848     }
11849
11850     /* Read the current link report info */
11851     bxe_fill_report_data(sc, &cur_data);
11852
11853     /* Don't report link down or exactly the same link status twice */
11854     if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
11855         (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11856                       &sc->last_reported_link.link_report_flags) &&
11857          bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11858                       &cur_data.link_report_flags))) {
11859         return;
11860     }
11861
11862         ELINK_DEBUG_P2(sc, "Change in link status : cur_data = %x, last_reported_link = %x\n",
11863                                         cur_data.link_report_flags, sc->last_reported_link.link_report_flags);
11864     sc->link_cnt++;
11865
11866         ELINK_DEBUG_P1(sc, "link status change count = %x\n", sc->link_cnt);
11867     /* report new link params and remember the state for the next time */
11868     memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
11869
11870     if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
11871                      &cur_data.link_report_flags)) {
11872         if_link_state_change(sc->ifnet, LINK_STATE_DOWN);
11873     } else {
11874         const char *duplex;
11875         const char *flow;
11876
11877         if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
11878                                    &cur_data.link_report_flags)) {
11879             duplex = "full";
11880                         ELINK_DEBUG_P0(sc, "link set to full duplex\n");
11881         } else {
11882             duplex = "half";
11883                         ELINK_DEBUG_P0(sc, "link set to half duplex\n");
11884         }
11885
11886         /*
11887          * Handle the FC at the end so that only these flags would be
11888          * possibly set. This way we may easily check if there is no FC
11889          * enabled.
11890          */
11891         if (cur_data.link_report_flags) {
11892             if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11893                              &cur_data.link_report_flags) &&
11894                 bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11895                              &cur_data.link_report_flags)) {
11896                 flow = "ON - receive & transmit";
11897             } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11898                                     &cur_data.link_report_flags) &&
11899                        !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11900                                      &cur_data.link_report_flags)) {
11901                 flow = "ON - receive";
11902             } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
11903                                      &cur_data.link_report_flags) &&
11904                        bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
11905                                     &cur_data.link_report_flags)) {
11906                 flow = "ON - transmit";
11907             } else {
11908                 flow = "none"; /* possible? */
11909             }
11910         } else {
11911             flow = "none";
11912         }
11913
11914         if_link_state_change(sc->ifnet, LINK_STATE_UP);
11915         BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
11916               cur_data.line_speed, duplex, flow);
11917     }
11918 }
11919
11920 static void
11921 bxe_link_report(struct bxe_softc *sc)
11922 {
11923     bxe_acquire_phy_lock(sc);
11924     bxe_link_report_locked(sc);
11925     bxe_release_phy_lock(sc);
11926 }
11927
11928 static void
11929 bxe_link_status_update(struct bxe_softc *sc)
11930 {
11931     if (sc->state != BXE_STATE_OPEN) {
11932         return;
11933     }
11934
11935     if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
11936         elink_link_status_update(&sc->link_params, &sc->link_vars);
11937     } else {
11938         sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
11939                                   ELINK_SUPPORTED_10baseT_Full |
11940                                   ELINK_SUPPORTED_100baseT_Half |
11941                                   ELINK_SUPPORTED_100baseT_Full |
11942                                   ELINK_SUPPORTED_1000baseT_Full |
11943                                   ELINK_SUPPORTED_2500baseX_Full |
11944                                   ELINK_SUPPORTED_10000baseT_Full |
11945                                   ELINK_SUPPORTED_TP |
11946                                   ELINK_SUPPORTED_FIBRE |
11947                                   ELINK_SUPPORTED_Autoneg |
11948                                   ELINK_SUPPORTED_Pause |
11949                                   ELINK_SUPPORTED_Asym_Pause);
11950         sc->port.advertising[0] = sc->port.supported[0];
11951
11952         sc->link_params.sc                = sc;
11953         sc->link_params.port              = SC_PORT(sc);
11954         sc->link_params.req_duplex[0]     = DUPLEX_FULL;
11955         sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
11956         sc->link_params.req_line_speed[0] = SPEED_10000;
11957         sc->link_params.speed_cap_mask[0] = 0x7f0000;
11958         sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
11959
11960         if (CHIP_REV_IS_FPGA(sc)) {
11961             sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
11962             sc->link_vars.line_speed  = ELINK_SPEED_1000;
11963             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11964                                          LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
11965         } else {
11966             sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
11967             sc->link_vars.line_speed  = ELINK_SPEED_10000;
11968             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
11969                                          LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
11970         }
11971
11972         sc->link_vars.link_up = 1;
11973
11974         sc->link_vars.duplex    = DUPLEX_FULL;
11975         sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
11976
11977         if (IS_PF(sc)) {
11978             REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
11979             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11980             bxe_link_report(sc);
11981         }
11982     }
11983
11984     if (IS_PF(sc)) {
11985         if (sc->link_vars.link_up) {
11986             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11987         } else {
11988             bxe_stats_handle(sc, STATS_EVENT_STOP);
11989         }
11990         bxe_link_report(sc);
11991     } else {
11992         bxe_link_report(sc);
11993         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
11994     }
11995 }
11996
11997 static int
11998 bxe_initial_phy_init(struct bxe_softc *sc,
11999                      int              load_mode)
12000 {
12001     int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
12002     uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
12003     struct elink_params *lp = &sc->link_params;
12004
12005     bxe_set_requested_fc(sc);
12006
12007     if (CHIP_REV_IS_SLOW(sc)) {
12008         uint32_t bond = CHIP_BOND_ID(sc);
12009         uint32_t feat = 0;
12010
12011         if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
12012             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12013         } else if (bond & 0x4) {
12014             if (CHIP_IS_E3(sc)) {
12015                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
12016             } else {
12017                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12018             }
12019         } else if (bond & 0x8) {
12020             if (CHIP_IS_E3(sc)) {
12021                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
12022             } else {
12023                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12024             }
12025         }
12026
12027         /* disable EMAC for E3 and above */
12028         if (bond & 0x2) {
12029             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12030         }
12031
12032         sc->link_params.feature_config_flags |= feat;
12033     }
12034
12035     bxe_acquire_phy_lock(sc);
12036
12037     if (load_mode == LOAD_DIAG) {
12038         lp->loopback_mode = ELINK_LOOPBACK_XGXS;
12039         /* Prefer doing PHY loopback at 10G speed, if possible */
12040         if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
12041             if (lp->speed_cap_mask[cfg_idx] &
12042                 PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
12043                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
12044             } else {
12045                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
12046             }
12047         }
12048     }
12049
12050     if (load_mode == LOAD_LOOPBACK_EXT) {
12051         lp->loopback_mode = ELINK_LOOPBACK_EXT;
12052     }
12053
12054     rc = elink_phy_init(&sc->link_params, &sc->link_vars);
12055
12056     bxe_release_phy_lock(sc);
12057
12058     bxe_calc_fc_adv(sc);
12059
12060     if (sc->link_vars.link_up) {
12061         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12062         bxe_link_report(sc);
12063     }
12064
12065     if (!CHIP_REV_IS_SLOW(sc)) {
12066         bxe_periodic_start(sc);
12067     }
12068
12069     sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
12070     return (rc);
12071 }
12072
12073 /* must be called under IF_ADDR_LOCK */
12074 static int
12075 bxe_init_mcast_macs_list(struct bxe_softc                 *sc,
12076                          struct ecore_mcast_ramrod_params *p)
12077 {
12078     struct ifnet *ifp = sc->ifnet;
12079     int mc_count = 0;
12080     struct ifmultiaddr *ifma;
12081     struct ecore_mcast_list_elem *mc_mac;
12082
12083     TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
12084         if (ifma->ifma_addr->sa_family != AF_LINK) {
12085             continue;
12086         }
12087
12088         mc_count++;
12089     }
12090
12091     ECORE_LIST_INIT(&p->mcast_list);
12092     p->mcast_list_len = 0;
12093
12094     if (!mc_count) {
12095         return (0);
12096     }
12097
12098     mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF,
12099                     (M_NOWAIT | M_ZERO));
12100     if (!mc_mac) {
12101         BLOGE(sc, "Failed to allocate temp mcast list\n");
12102         return (-1);
12103     }
12104     bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12105
12106     TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
12107         if (ifma->ifma_addr->sa_family != AF_LINK) {
12108             continue;
12109         }
12110
12111         mc_mac->mac = (uint8_t *)LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
12112         ECORE_LIST_PUSH_TAIL(&mc_mac->link, &p->mcast_list);
12113
12114         BLOGD(sc, DBG_LOAD,
12115               "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X and mc_count %d\n",
12116               mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12117               mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5], mc_count);
12118        mc_mac++;
12119     }
12120
12121     p->mcast_list_len = mc_count;
12122
12123     return (0);
12124 }
12125
12126
12127 static void
12128 bxe_free_mcast_macs_list(struct ecore_mcast_ramrod_params *p)
12129 {
12130     struct ecore_mcast_list_elem *mc_mac =
12131         ECORE_LIST_FIRST_ENTRY(&p->mcast_list,
12132                                struct ecore_mcast_list_elem,
12133                                link);
12134
12135     if (mc_mac) {
12136         /* only a single free as all mc_macs are in the same heap array */
12137         free(mc_mac, M_DEVBUF);
12138     }
12139 }
12140
12141 static int
12142 bxe_set_mc_list(struct bxe_softc *sc)
12143 {
12144     struct ecore_mcast_ramrod_params rparam = { NULL };
12145     int rc = 0;
12146
12147     rparam.mcast_obj = &sc->mcast_obj;
12148
12149     BXE_MCAST_LOCK(sc);
12150
12151     /* first, clear all configured multicast MACs */
12152     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12153     if (rc < 0) {
12154         BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12155         /* Manual backport parts of FreeBSD upstream r284470. */
12156         BXE_MCAST_UNLOCK(sc);
12157         return (rc);
12158     }
12159
12160     /* configure a new MACs list */
12161     rc = bxe_init_mcast_macs_list(sc, &rparam);
12162     if (rc) {
12163         BLOGE(sc, "Failed to create mcast MACs list (%d)\n", rc);
12164         BXE_MCAST_UNLOCK(sc);
12165         return (rc);
12166     }
12167
12168     /* Now add the new MACs */
12169     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12170     if (rc < 0) {
12171         BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12172     }
12173
12174     bxe_free_mcast_macs_list(&rparam);
12175
12176     BXE_MCAST_UNLOCK(sc);
12177
12178     return (rc);
12179 }
12180
12181 static int
12182 bxe_set_uc_list(struct bxe_softc *sc)
12183 {
12184     struct ifnet *ifp = sc->ifnet;
12185     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12186     struct ifaddr *ifa;
12187     unsigned long ramrod_flags = 0;
12188     int rc;
12189
12190 #if __FreeBSD_version < 800000
12191     IF_ADDR_LOCK(ifp);
12192 #else
12193     if_addr_rlock(ifp);
12194 #endif
12195
12196     /* first schedule a cleanup up of old configuration */
12197     rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12198     if (rc < 0) {
12199         BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12200 #if __FreeBSD_version < 800000
12201         IF_ADDR_UNLOCK(ifp);
12202 #else
12203         if_addr_runlock(ifp);
12204 #endif
12205         return (rc);
12206     }
12207
12208     ifa = ifp->if_addr;
12209     while (ifa) {
12210         if (ifa->ifa_addr->sa_family != AF_LINK) {
12211             ifa = TAILQ_NEXT(ifa, ifa_link);
12212             continue;
12213         }
12214
12215         rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12216                              mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12217         if (rc == -EEXIST) {
12218             BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12219             /* do not treat adding same MAC as an error */
12220             rc = 0;
12221         } else if (rc < 0) {
12222             BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12223 #if __FreeBSD_version < 800000
12224             IF_ADDR_UNLOCK(ifp);
12225 #else
12226             if_addr_runlock(ifp);
12227 #endif
12228             return (rc);
12229         }
12230
12231         ifa = TAILQ_NEXT(ifa, ifa_link);
12232     }
12233
12234 #if __FreeBSD_version < 800000
12235     IF_ADDR_UNLOCK(ifp);
12236 #else
12237     if_addr_runlock(ifp);
12238 #endif
12239
12240     /* Execute the pending commands */
12241     bit_set(&ramrod_flags, RAMROD_CONT);
12242     return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12243                             ECORE_UC_LIST_MAC, &ramrod_flags));
12244 }
12245
12246 static void
12247 bxe_set_rx_mode(struct bxe_softc *sc)
12248 {
12249     struct ifnet *ifp = sc->ifnet;
12250     uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12251
12252     if (sc->state != BXE_STATE_OPEN) {
12253         BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12254         return;
12255     }
12256
12257     BLOGD(sc, DBG_SP, "ifp->if_flags=0x%x\n", ifp->if_flags);
12258
12259     if (ifp->if_flags & IFF_PROMISC) {
12260         rx_mode = BXE_RX_MODE_PROMISC;
12261     } else if ((ifp->if_flags & IFF_ALLMULTI) ||
12262                ((ifp->if_amcount > BXE_MAX_MULTICAST) &&
12263                 CHIP_IS_E1(sc))) {
12264         rx_mode = BXE_RX_MODE_ALLMULTI;
12265     } else {
12266         if (IS_PF(sc)) {
12267             /* some multicasts */
12268             if (bxe_set_mc_list(sc) < 0) {
12269                 rx_mode = BXE_RX_MODE_ALLMULTI;
12270             }
12271             if (bxe_set_uc_list(sc) < 0) {
12272                 rx_mode = BXE_RX_MODE_PROMISC;
12273             }
12274         }
12275     }
12276
12277     sc->rx_mode = rx_mode;
12278
12279     /* schedule the rx_mode command */
12280     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12281         BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12282         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12283         return;
12284     }
12285
12286     if (IS_PF(sc)) {
12287         bxe_set_storm_rx_mode(sc);
12288     }
12289 }
12290
12291
12292 /* update flags in shmem */
12293 static void
12294 bxe_update_drv_flags(struct bxe_softc *sc,
12295                      uint32_t         flags,
12296                      uint32_t         set)
12297 {
12298     uint32_t drv_flags;
12299
12300     if (SHMEM2_HAS(sc, drv_flags)) {
12301         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12302         drv_flags = SHMEM2_RD(sc, drv_flags);
12303
12304         if (set) {
12305             SET_FLAGS(drv_flags, flags);
12306         } else {
12307             RESET_FLAGS(drv_flags, flags);
12308         }
12309
12310         SHMEM2_WR(sc, drv_flags, drv_flags);
12311         BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12312
12313         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12314     }
12315 }
12316
12317 /* periodic timer callout routine, only runs when the interface is up */
12318
12319 static void
12320 bxe_periodic_callout_func(void *xsc)
12321 {
12322     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12323     int i;
12324
12325     if (!BXE_CORE_TRYLOCK(sc)) {
12326         /* just bail and try again next time */
12327
12328         if ((sc->state == BXE_STATE_OPEN) &&
12329             (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12330             /* schedule the next periodic callout */
12331             callout_reset(&sc->periodic_callout, hz,
12332                           bxe_periodic_callout_func, sc);
12333         }
12334
12335         return;
12336     }
12337
12338     if ((sc->state != BXE_STATE_OPEN) ||
12339         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12340         BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12341         BXE_CORE_UNLOCK(sc);
12342         return;
12343     }
12344
12345
12346     /* Check for TX timeouts on any fastpath. */
12347     FOR_EACH_QUEUE(sc, i) {
12348         if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12349             /* Ruh-Roh, chip was reset! */
12350             break;
12351         }
12352     }
12353
12354     if (!CHIP_REV_IS_SLOW(sc)) {
12355         /*
12356          * This barrier is needed to ensure the ordering between the writing
12357          * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12358          * the reading here.
12359          */
12360         mb();
12361         if (sc->port.pmf) {
12362             bxe_acquire_phy_lock(sc);
12363             elink_period_func(&sc->link_params, &sc->link_vars);
12364             bxe_release_phy_lock(sc);
12365         }
12366     }
12367
12368     if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12369         int mb_idx = SC_FW_MB_IDX(sc);
12370         uint32_t drv_pulse;
12371         uint32_t mcp_pulse;
12372
12373         ++sc->fw_drv_pulse_wr_seq;
12374         sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12375
12376         drv_pulse = sc->fw_drv_pulse_wr_seq;
12377         bxe_drv_pulse(sc);
12378
12379         mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12380                      MCP_PULSE_SEQ_MASK);
12381
12382         /*
12383          * The delta between driver pulse and mcp response should
12384          * be 1 (before mcp response) or 0 (after mcp response).
12385          */
12386         if ((drv_pulse != mcp_pulse) &&
12387             (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12388             /* someone lost a heartbeat... */
12389             BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12390                   drv_pulse, mcp_pulse);
12391         }
12392     }
12393
12394     /* state is BXE_STATE_OPEN */
12395     bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12396
12397     BXE_CORE_UNLOCK(sc);
12398
12399     if ((sc->state == BXE_STATE_OPEN) &&
12400         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12401         /* schedule the next periodic callout */
12402         callout_reset(&sc->periodic_callout, hz,
12403                       bxe_periodic_callout_func, sc);
12404     }
12405 }
12406
12407 static void
12408 bxe_periodic_start(struct bxe_softc *sc)
12409 {
12410     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12411     callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12412 }
12413
12414 static void
12415 bxe_periodic_stop(struct bxe_softc *sc)
12416 {
12417     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12418     callout_drain(&sc->periodic_callout);
12419 }
12420
12421 void
12422 bxe_parity_recover(struct bxe_softc *sc)
12423 {
12424     uint8_t global = FALSE;
12425     uint32_t error_recovered, error_unrecovered;
12426     bool is_parity;
12427
12428
12429     if ((sc->recovery_state == BXE_RECOVERY_FAILED) &&
12430         (sc->state == BXE_STATE_ERROR)) {
12431         BLOGE(sc, "RECOVERY failed, "
12432             "stack notified driver is NOT running! "
12433             "Please reboot/power cycle the system.\n");
12434         return;
12435     }
12436
12437     while (1) {
12438         BLOGD(sc, DBG_SP,
12439            "%s sc=%p state=0x%x rec_state=0x%x error_status=%x\n",
12440             __func__, sc, sc->state, sc->recovery_state, sc->error_status);
12441
12442         switch(sc->recovery_state) {
12443
12444         case BXE_RECOVERY_INIT:
12445             is_parity = bxe_chk_parity_attn(sc, &global, FALSE);
12446
12447             if ((CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ||
12448                 (sc->error_status & BXE_ERR_MCP_ASSERT) ||
12449                 (sc->error_status & BXE_ERR_GLOBAL)) {
12450
12451                 BXE_CORE_LOCK(sc);
12452                 if (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) {
12453                     bxe_periodic_stop(sc);
12454                 }
12455                 bxe_nic_unload(sc, UNLOAD_RECOVERY, false);
12456                 sc->state = BXE_STATE_ERROR;
12457                 sc->recovery_state = BXE_RECOVERY_FAILED;
12458                 BLOGE(sc, " No Recovery tried for error 0x%x"
12459                     " stack notified driver is NOT running!"
12460                     " Please reboot/power cycle the system.\n",
12461                     sc->error_status);
12462                 BXE_CORE_UNLOCK(sc);
12463                 return;
12464             }
12465
12466
12467            /* Try to get a LEADER_LOCK HW lock */
12468             if (bxe_trylock_leader_lock(sc)) {
12469
12470                 bxe_set_reset_in_progress(sc);
12471                 /*
12472                  * Check if there is a global attention and if
12473                  * there was a global attention, set the global
12474                  * reset bit.
12475                  */
12476                 if (global) {
12477                     bxe_set_reset_global(sc);
12478                 }
12479                 sc->is_leader = 1;
12480             }
12481
12482             /* If interface has been removed - break */
12483
12484             if (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) {
12485                 bxe_periodic_stop(sc);
12486             }
12487
12488             BXE_CORE_LOCK(sc);
12489             bxe_nic_unload(sc,UNLOAD_RECOVERY, false);
12490             sc->recovery_state = BXE_RECOVERY_WAIT;
12491             BXE_CORE_UNLOCK(sc);
12492
12493             /*
12494              * Ensure "is_leader", MCP command sequence and
12495              * "recovery_state" update values are seen on other
12496              * CPUs.
12497              */
12498             mb();
12499             break;
12500         case BXE_RECOVERY_WAIT:
12501
12502             if (sc->is_leader) {
12503                 int other_engine = SC_PATH(sc) ? 0 : 1;
12504                 bool other_load_status =
12505                     bxe_get_load_status(sc, other_engine);
12506                 bool load_status =
12507                     bxe_get_load_status(sc, SC_PATH(sc));
12508                 global = bxe_reset_is_global(sc);
12509
12510                 /*
12511                  * In case of a parity in a global block, let
12512                  * the first leader that performs a
12513                  * leader_reset() reset the global blocks in
12514                  * order to clear global attentions. Otherwise
12515                  * the gates will remain closed for that
12516                  * engine.
12517                  */
12518                 if (load_status ||
12519                     (global && other_load_status)) {
12520                     /*
12521                      * Wait until all other functions get
12522                      * down.
12523                      */
12524                     taskqueue_enqueue_timeout(taskqueue_thread,
12525                         &sc->sp_err_timeout_task, hz/10);
12526                     return;
12527                 } else {
12528                     /*
12529                      * If all other functions got down
12530                      * try to bring the chip back to
12531                      * normal. In any case it's an exit
12532                      * point for a leader.
12533                      */
12534                     if (bxe_leader_reset(sc)) {
12535                         BLOGE(sc, "RECOVERY failed, "
12536                             "stack notified driver is NOT running!\n");
12537                         sc->recovery_state = BXE_RECOVERY_FAILED;
12538                         sc->state = BXE_STATE_ERROR;
12539                         mb();
12540                         return;
12541                     }
12542
12543                     /*
12544                      * If we are here, means that the
12545                      * leader has succeeded and doesn't
12546                      * want to be a leader any more. Try
12547                      * to continue as a none-leader.
12548                      */
12549                 break;
12550                 }
12551
12552             } else { /* non-leader */
12553                 if (!bxe_reset_is_done(sc, SC_PATH(sc))) {
12554                     /*
12555                      * Try to get a LEADER_LOCK HW lock as
12556                      * long as a former leader may have
12557                      * been unloaded by the user or
12558                      * released a leadership by another
12559                      * reason.
12560                      */
12561                     if (bxe_trylock_leader_lock(sc)) {
12562                         /*
12563                          * I'm a leader now! Restart a
12564                          * switch case.
12565                          */
12566                         sc->is_leader = 1;
12567                         break;
12568                     }
12569
12570                     taskqueue_enqueue_timeout(taskqueue_thread,
12571                         &sc->sp_err_timeout_task, hz/10);
12572                     return;
12573
12574                 } else {
12575                     /*
12576                      * If there was a global attention, wait
12577                      * for it to be cleared.
12578                      */
12579                     if (bxe_reset_is_global(sc)) {
12580                         taskqueue_enqueue_timeout(taskqueue_thread,
12581                             &sc->sp_err_timeout_task, hz/10);
12582                         return;
12583                      }
12584
12585                      error_recovered =
12586                          sc->eth_stats.recoverable_error;
12587                      error_unrecovered =
12588                          sc->eth_stats.unrecoverable_error;
12589                      BXE_CORE_LOCK(sc);
12590                      sc->recovery_state =
12591                          BXE_RECOVERY_NIC_LOADING;
12592                      if (bxe_nic_load(sc, LOAD_NORMAL)) {
12593                          error_unrecovered++;
12594                          sc->recovery_state = BXE_RECOVERY_FAILED;
12595                          sc->state = BXE_STATE_ERROR;
12596                          BLOGE(sc, "Recovery is NOT successfull, "
12597                             " state=0x%x recovery_state=0x%x error=%x\n",
12598                             sc->state, sc->recovery_state, sc->error_status);
12599                          sc->error_status = 0;
12600                      } else {
12601                          sc->recovery_state =
12602                              BXE_RECOVERY_DONE;
12603                          error_recovered++;
12604                          BLOGI(sc, "Recovery is successfull from errors %x,"
12605                             " state=0x%x"
12606                             " recovery_state=0x%x \n", sc->error_status,
12607                             sc->state, sc->recovery_state);
12608                          mb();
12609                      }
12610                      sc->error_status = 0;
12611                      BXE_CORE_UNLOCK(sc);
12612                      sc->eth_stats.recoverable_error =
12613                          error_recovered;
12614                      sc->eth_stats.unrecoverable_error =
12615                          error_unrecovered;
12616
12617                      return;
12618                  }
12619              }
12620          default:
12621              return;
12622          }
12623     }
12624 }
12625 void
12626 bxe_handle_error(struct bxe_softc * sc)
12627 {
12628
12629     if(sc->recovery_state == BXE_RECOVERY_WAIT) {
12630         return;
12631     }
12632     if(sc->error_status) {
12633         if (sc->state == BXE_STATE_OPEN)  {
12634             bxe_int_disable(sc);
12635         }
12636         if (sc->link_vars.link_up) {
12637             if_link_state_change(sc->ifnet, LINK_STATE_DOWN);
12638         }
12639         sc->recovery_state = BXE_RECOVERY_INIT;
12640         BLOGI(sc, "bxe%d: Recovery started errors 0x%x recovery state 0x%x\n",
12641             sc->unit, sc->error_status, sc->recovery_state);
12642         bxe_parity_recover(sc);
12643    }
12644 }
12645
12646 static void
12647 bxe_sp_err_timeout_task(void *arg, int pending)
12648 {
12649
12650     struct bxe_softc *sc = (struct bxe_softc *)arg;
12651
12652     BLOGD(sc, DBG_SP,
12653         "%s state = 0x%x rec state=0x%x error_status=%x\n",
12654         __func__, sc->state, sc->recovery_state, sc->error_status);
12655
12656     if((sc->recovery_state == BXE_RECOVERY_FAILED) &&
12657        (sc->state == BXE_STATE_ERROR)) {
12658         return;
12659     }
12660     /* if can be taken */
12661     if ((sc->error_status) && (sc->trigger_grcdump)) {
12662         bxe_grc_dump(sc);
12663     }
12664     if (sc->recovery_state != BXE_RECOVERY_DONE) {
12665         bxe_handle_error(sc);
12666         bxe_parity_recover(sc);
12667     } else if (sc->error_status) {
12668         bxe_handle_error(sc);
12669     }
12670
12671     return;
12672 }
12673
12674 /* start the controller */
12675 static __noinline int
12676 bxe_nic_load(struct bxe_softc *sc,
12677              int              load_mode)
12678 {
12679     uint32_t val;
12680     int load_code = 0;
12681     int i, rc = 0;
12682
12683     BXE_CORE_LOCK_ASSERT(sc);
12684
12685     BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12686
12687     sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12688
12689     if (IS_PF(sc)) {
12690         /* must be called before memory allocation and HW init */
12691         bxe_ilt_set_info(sc);
12692     }
12693
12694     sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12695
12696     bxe_set_fp_rx_buf_size(sc);
12697
12698     if (bxe_alloc_fp_buffers(sc) != 0) {
12699         BLOGE(sc, "Failed to allocate fastpath memory\n");
12700         sc->state = BXE_STATE_CLOSED;
12701         rc = ENOMEM;
12702         goto bxe_nic_load_error0;
12703     }
12704
12705     if (bxe_alloc_mem(sc) != 0) {
12706         sc->state = BXE_STATE_CLOSED;
12707         rc = ENOMEM;
12708         goto bxe_nic_load_error0;
12709     }
12710
12711     if (bxe_alloc_fw_stats_mem(sc) != 0) {
12712         sc->state = BXE_STATE_CLOSED;
12713         rc = ENOMEM;
12714         goto bxe_nic_load_error0;
12715     }
12716
12717     if (IS_PF(sc)) {
12718         /* set pf load just before approaching the MCP */
12719         bxe_set_pf_load(sc);
12720
12721         /* if MCP exists send load request and analyze response */
12722         if (!BXE_NOMCP(sc)) {
12723             /* attempt to load pf */
12724             if (bxe_nic_load_request(sc, &load_code) != 0) {
12725                 sc->state = BXE_STATE_CLOSED;
12726                 rc = ENXIO;
12727                 goto bxe_nic_load_error1;
12728             }
12729
12730             /* what did the MCP say? */
12731             if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12732                 bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12733                 sc->state = BXE_STATE_CLOSED;
12734                 rc = ENXIO;
12735                 goto bxe_nic_load_error2;
12736             }
12737         } else {
12738             BLOGI(sc, "Device has no MCP!\n");
12739             load_code = bxe_nic_load_no_mcp(sc);
12740         }
12741
12742         /* mark PMF if applicable */
12743         bxe_nic_load_pmf(sc, load_code);
12744
12745         /* Init Function state controlling object */
12746         bxe_init_func_obj(sc);
12747
12748         /* Initialize HW */
12749         if (bxe_init_hw(sc, load_code) != 0) {
12750             BLOGE(sc, "HW init failed\n");
12751             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12752             sc->state = BXE_STATE_CLOSED;
12753             rc = ENXIO;
12754             goto bxe_nic_load_error2;
12755         }
12756     }
12757
12758     /* set ALWAYS_ALIVE bit in shmem */
12759     sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12760     bxe_drv_pulse(sc);
12761     sc->flags |= BXE_NO_PULSE;
12762
12763     /* attach interrupts */
12764     if (bxe_interrupt_attach(sc) != 0) {
12765         sc->state = BXE_STATE_CLOSED;
12766         rc = ENXIO;
12767         goto bxe_nic_load_error2;
12768     }
12769
12770     bxe_nic_init(sc, load_code);
12771
12772     /* Init per-function objects */
12773     if (IS_PF(sc)) {
12774         bxe_init_objs(sc);
12775         // XXX bxe_iov_nic_init(sc);
12776
12777         /* set AFEX default VLAN tag to an invalid value */
12778         sc->devinfo.mf_info.afex_def_vlan_tag = -1;
12779         // XXX bxe_nic_load_afex_dcc(sc, load_code);
12780
12781         sc->state = BXE_STATE_OPENING_WAITING_PORT;
12782         rc = bxe_func_start(sc);
12783         if (rc) {
12784             BLOGE(sc, "Function start failed! rc = %d\n", rc);
12785             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12786             sc->state = BXE_STATE_ERROR;
12787             goto bxe_nic_load_error3;
12788         }
12789
12790         /* send LOAD_DONE command to MCP */
12791         if (!BXE_NOMCP(sc)) {
12792             load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12793             if (!load_code) {
12794                 BLOGE(sc, "MCP response failure, aborting\n");
12795                 sc->state = BXE_STATE_ERROR;
12796                 rc = ENXIO;
12797                 goto bxe_nic_load_error3;
12798             }
12799         }
12800
12801         rc = bxe_setup_leading(sc);
12802         if (rc) {
12803             BLOGE(sc, "Setup leading failed! rc = %d\n", rc);
12804             sc->state = BXE_STATE_ERROR;
12805             goto bxe_nic_load_error3;
12806         }
12807
12808         FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
12809             rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
12810             if (rc) {
12811                 BLOGE(sc, "Queue(%d) setup failed rc = %d\n", i, rc);
12812                 sc->state = BXE_STATE_ERROR;
12813                 goto bxe_nic_load_error3;
12814             }
12815         }
12816
12817         rc = bxe_init_rss_pf(sc);
12818         if (rc) {
12819             BLOGE(sc, "PF RSS init failed\n");
12820             sc->state = BXE_STATE_ERROR;
12821             goto bxe_nic_load_error3;
12822         }
12823     }
12824     /* XXX VF */
12825
12826     /* now when Clients are configured we are ready to work */
12827     sc->state = BXE_STATE_OPEN;
12828
12829     /* Configure a ucast MAC */
12830     if (IS_PF(sc)) {
12831         rc = bxe_set_eth_mac(sc, TRUE);
12832     }
12833     if (rc) {
12834         BLOGE(sc, "Setting Ethernet MAC failed rc = %d\n", rc);
12835         sc->state = BXE_STATE_ERROR;
12836         goto bxe_nic_load_error3;
12837     }
12838
12839     if (sc->port.pmf) {
12840         rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
12841         if (rc) {
12842             sc->state = BXE_STATE_ERROR;
12843             goto bxe_nic_load_error3;
12844         }
12845     }
12846
12847     sc->link_params.feature_config_flags &=
12848         ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
12849
12850     /* start fast path */
12851
12852     /* Initialize Rx filter */
12853     bxe_set_rx_mode(sc);
12854
12855     /* start the Tx */
12856     switch (/* XXX load_mode */LOAD_OPEN) {
12857     case LOAD_NORMAL:
12858     case LOAD_OPEN:
12859         break;
12860
12861     case LOAD_DIAG:
12862     case LOAD_LOOPBACK_EXT:
12863         sc->state = BXE_STATE_DIAG;
12864         break;
12865
12866     default:
12867         break;
12868     }
12869
12870     if (sc->port.pmf) {
12871         bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
12872     } else {
12873         bxe_link_status_update(sc);
12874     }
12875
12876     /* start the periodic timer callout */
12877     bxe_periodic_start(sc);
12878
12879     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
12880         /* mark driver is loaded in shmem2 */
12881         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
12882         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
12883                   (val |
12884                    DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
12885                    DRV_FLAGS_CAPABILITIES_LOADED_L2));
12886     }
12887
12888     /* wait for all pending SP commands to complete */
12889     if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
12890         BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
12891         bxe_periodic_stop(sc);
12892         bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
12893         return (ENXIO);
12894     }
12895
12896     /* Tell the stack the driver is running! */
12897     sc->ifnet->if_drv_flags = IFF_DRV_RUNNING;
12898
12899     BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
12900
12901     return (0);
12902
12903 bxe_nic_load_error3:
12904
12905     if (IS_PF(sc)) {
12906         bxe_int_disable_sync(sc, 1);
12907
12908         /* clean out queued objects */
12909         bxe_squeeze_objects(sc);
12910     }
12911
12912     bxe_interrupt_detach(sc);
12913
12914 bxe_nic_load_error2:
12915
12916     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
12917         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
12918         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
12919     }
12920
12921     sc->port.pmf = 0;
12922
12923 bxe_nic_load_error1:
12924
12925     /* clear pf_load status, as it was already set */
12926     if (IS_PF(sc)) {
12927         bxe_clear_pf_load(sc);
12928     }
12929
12930 bxe_nic_load_error0:
12931
12932     bxe_free_fw_stats_mem(sc);
12933     bxe_free_fp_buffers(sc);
12934     bxe_free_mem(sc);
12935
12936     return (rc);
12937 }
12938
12939 static int
12940 bxe_init_locked(struct bxe_softc *sc)
12941 {
12942     int other_engine = SC_PATH(sc) ? 0 : 1;
12943     uint8_t other_load_status, load_status;
12944     uint8_t global = FALSE;
12945     int rc;
12946
12947     BXE_CORE_LOCK_ASSERT(sc);
12948
12949     /* check if the driver is already running */
12950     if (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) {
12951         BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
12952         return (0);
12953     }
12954
12955     if((sc->state == BXE_STATE_ERROR) &&
12956         (sc->recovery_state == BXE_RECOVERY_FAILED)) {
12957         BLOGE(sc, "Initialization not done, "
12958                   "as previous recovery failed."
12959                   "Reboot/Power-cycle the system\n" );
12960         return (ENXIO);
12961     }
12962
12963
12964     bxe_set_power_state(sc, PCI_PM_D0);
12965
12966     /*
12967      * If parity occurred during the unload, then attentions and/or
12968      * RECOVERY_IN_PROGRES may still be set. If so we want the first function
12969      * loaded on the current engine to complete the recovery. Parity recovery
12970      * is only relevant for PF driver.
12971      */
12972     if (IS_PF(sc)) {
12973         other_load_status = bxe_get_load_status(sc, other_engine);
12974         load_status = bxe_get_load_status(sc, SC_PATH(sc));
12975
12976         if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
12977             bxe_chk_parity_attn(sc, &global, TRUE)) {
12978             do {
12979                 /*
12980                  * If there are attentions and they are in global blocks, set
12981                  * the GLOBAL_RESET bit regardless whether it will be this
12982                  * function that will complete the recovery or not.
12983                  */
12984                 if (global) {
12985                     bxe_set_reset_global(sc);
12986                 }
12987
12988                 /*
12989                  * Only the first function on the current engine should try
12990                  * to recover in open. In case of attentions in global blocks
12991                  * only the first in the chip should try to recover.
12992                  */
12993                 if ((!load_status && (!global || !other_load_status)) &&
12994                     bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
12995                     BLOGI(sc, "Recovered during init\n");
12996                     break;
12997                 }
12998
12999                 /* recovery has failed... */
13000                 bxe_set_power_state(sc, PCI_PM_D3hot);
13001                 sc->recovery_state = BXE_RECOVERY_FAILED;
13002
13003                 BLOGE(sc, "Recovery flow hasn't properly "
13004                           "completed yet, try again later. "
13005                           "If you still see this message after a "
13006                           "few retries then power cycle is required.\n");
13007
13008                 rc = ENXIO;
13009                 goto bxe_init_locked_done;
13010             } while (0);
13011         }
13012     }
13013
13014     sc->recovery_state = BXE_RECOVERY_DONE;
13015
13016     rc = bxe_nic_load(sc, LOAD_OPEN);
13017
13018 bxe_init_locked_done:
13019
13020     if (rc) {
13021         /* Tell the stack the driver is NOT running! */
13022         BLOGE(sc, "Initialization failed, "
13023                   "stack notified driver is NOT running!\n");
13024         sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
13025     }
13026
13027     return (rc);
13028 }
13029
13030 static int
13031 bxe_stop_locked(struct bxe_softc *sc)
13032 {
13033     BXE_CORE_LOCK_ASSERT(sc);
13034     return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
13035 }
13036
13037 /*
13038  * Handles controller initialization when called from an unlocked routine.
13039  * ifconfig calls this function.
13040  *
13041  * Returns:
13042  *   void
13043  */
13044 static void
13045 bxe_init(void *xsc)
13046 {
13047     struct bxe_softc *sc = (struct bxe_softc *)xsc;
13048
13049     BXE_CORE_LOCK(sc);
13050     bxe_init_locked(sc);
13051     BXE_CORE_UNLOCK(sc);
13052 }
13053
13054 static int
13055 bxe_init_ifnet(struct bxe_softc *sc)
13056 {
13057     struct ifnet *ifp;
13058
13059     /* ifconfig entrypoint for media type/status reporting */
13060     ifmedia_init(&sc->ifmedia, IFM_IMASK,
13061                  bxe_ifmedia_update,
13062                  bxe_ifmedia_status);
13063
13064     /* set the default interface values */
13065     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
13066     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
13067     ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
13068
13069     sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
13070         BLOGI(sc, "IFMEDIA flags : %x\n", sc->ifmedia.ifm_media);
13071
13072     /* allocate the ifnet structure */
13073     if ((ifp = if_alloc(IFT_ETHER)) == NULL) {
13074         BLOGE(sc, "Interface allocation failed!\n");
13075         return (ENXIO);
13076     }
13077
13078     ifp->if_softc = sc;
13079     if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
13080     ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
13081     ifp->if_ioctl = bxe_ioctl;
13082     ifp->if_start = bxe_tx_start;
13083 #if __FreeBSD_version >= 901504
13084     ifp->if_transmit = bxe_tx_mq_start;
13085     ifp->if_qflush = bxe_mq_flush;
13086 #endif
13087 #ifdef FreeBSD8_0
13088     ifp->if_timer = 0;
13089 #endif
13090     ifp->if_init = bxe_init;
13091     ifp->if_mtu = sc->mtu;
13092     ifp->if_hwassist = (CSUM_IP       |
13093                         CSUM_TCP      |
13094                         CSUM_UDP      |
13095                         CSUM_TSO      |
13096                         CSUM_TCP_IPV6 |
13097                         CSUM_UDP_IPV6);
13098     ifp->if_capabilities =
13099 #if __FreeBSD_version < 700000
13100         (IFCAP_VLAN_MTU       |
13101          IFCAP_VLAN_HWTAGGING |
13102          IFCAP_HWCSUM         |
13103          IFCAP_JUMBO_MTU      |
13104          IFCAP_LRO);
13105 #else
13106         (IFCAP_VLAN_MTU       |
13107          IFCAP_VLAN_HWTAGGING |
13108          IFCAP_VLAN_HWTSO     |
13109          IFCAP_VLAN_HWFILTER  |
13110          IFCAP_VLAN_HWCSUM    |
13111          IFCAP_HWCSUM         |
13112          IFCAP_JUMBO_MTU      |
13113          IFCAP_LRO            |
13114          IFCAP_TSO4           |
13115          IFCAP_TSO6           |
13116          IFCAP_WOL_MAGIC);
13117 #endif
13118     ifp->if_capenable = ifp->if_capabilities;
13119     ifp->if_capenable &= ~IFCAP_WOL_MAGIC; /* XXX not yet... */
13120 #if __FreeBSD_version < 1000025
13121     ifp->if_baudrate = 1000000000;
13122 #else
13123     if_initbaudrate(ifp, IF_Gbps(10));
13124 #endif
13125     ifp->if_snd.ifq_drv_maxlen = sc->tx_ring_size;
13126
13127     IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
13128     IFQ_SET_READY(&ifp->if_snd);
13129
13130     sc->ifnet = ifp;
13131
13132     /* attach to the Ethernet interface list */
13133     ether_ifattach(ifp, sc->link_params.mac_addr);
13134
13135     return (0);
13136 }
13137
13138 static void
13139 bxe_deallocate_bars(struct bxe_softc *sc)
13140 {
13141     int i;
13142
13143     for (i = 0; i < MAX_BARS; i++) {
13144         if (sc->bar[i].resource != NULL) {
13145             bus_release_resource(sc->dev,
13146                                  SYS_RES_MEMORY,
13147                                  sc->bar[i].rid,
13148                                  sc->bar[i].resource);
13149             BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
13150                   i, PCIR_BAR(i));
13151         }
13152     }
13153 }
13154
13155 static int
13156 bxe_allocate_bars(struct bxe_softc *sc)
13157 {
13158     u_int flags;
13159     int i;
13160
13161     memset(sc->bar, 0, sizeof(sc->bar));
13162
13163     for (i = 0; i < MAX_BARS; i++) {
13164
13165         /* memory resources reside at BARs 0, 2, 4 */
13166         /* Run `pciconf -lb` to see mappings */
13167         if ((i != 0) && (i != 2) && (i != 4)) {
13168             continue;
13169         }
13170
13171         sc->bar[i].rid = PCIR_BAR(i);
13172
13173         flags = RF_ACTIVE;
13174         if (i == 0) {
13175             flags |= RF_SHAREABLE;
13176         }
13177
13178         if ((sc->bar[i].resource =
13179              bus_alloc_resource_any(sc->dev,
13180                                     SYS_RES_MEMORY,
13181                                     &sc->bar[i].rid,
13182                                     flags)) == NULL) {
13183             return (0);
13184         }
13185
13186         sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
13187         sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
13188         sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
13189
13190         BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%ld) -> %p\n",
13191               i, PCIR_BAR(i),
13192               (void *)rman_get_start(sc->bar[i].resource),
13193               (void *)rman_get_end(sc->bar[i].resource),
13194               rman_get_size(sc->bar[i].resource),
13195               (void *)sc->bar[i].kva);
13196     }
13197
13198     return (0);
13199 }
13200
13201 static void
13202 bxe_get_function_num(struct bxe_softc *sc)
13203 {
13204     uint32_t val = 0;
13205
13206     /*
13207      * Read the ME register to get the function number. The ME register
13208      * holds the relative-function number and absolute-function number. The
13209      * absolute-function number appears only in E2 and above. Before that
13210      * these bits always contained zero, therefore we cannot blindly use them.
13211      */
13212
13213     val = REG_RD(sc, BAR_ME_REGISTER);
13214
13215     sc->pfunc_rel =
13216         (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
13217     sc->path_id =
13218         (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
13219
13220     if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13221         sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
13222     } else {
13223         sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
13224     }
13225
13226     BLOGD(sc, DBG_LOAD,
13227           "Relative function %d, Absolute function %d, Path %d\n",
13228           sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
13229 }
13230
13231 static uint32_t
13232 bxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
13233 {
13234     uint32_t shmem2_size;
13235     uint32_t offset;
13236     uint32_t mf_cfg_offset_value;
13237
13238     /* Non 57712 */
13239     offset = (SHMEM_RD(sc, func_mb) +
13240               (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
13241
13242     /* 57712 plus */
13243     if (sc->devinfo.shmem2_base != 0) {
13244         shmem2_size = SHMEM2_RD(sc, size);
13245         if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
13246             mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
13247             if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
13248                 offset = mf_cfg_offset_value;
13249             }
13250         }
13251     }
13252
13253     return (offset);
13254 }
13255
13256 static uint32_t
13257 bxe_pcie_capability_read(struct bxe_softc *sc,
13258                          int    reg,
13259                          int    width)
13260 {
13261     int pcie_reg;
13262
13263     /* ensure PCIe capability is enabled */
13264     if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
13265         if (pcie_reg != 0) {
13266             BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
13267             return (pci_read_config(sc->dev, (pcie_reg + reg), width));
13268         }
13269     }
13270
13271     BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
13272
13273     return (0);
13274 }
13275
13276 static uint8_t
13277 bxe_is_pcie_pending(struct bxe_softc *sc)
13278 {
13279     return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
13280             PCIM_EXP_STA_TRANSACTION_PND);
13281 }
13282
13283 /*
13284  * Walk the PCI capabiites list for the device to find what features are
13285  * supported. These capabilites may be enabled/disabled by firmware so it's
13286  * best to walk the list rather than make assumptions.
13287  */
13288 static void
13289 bxe_probe_pci_caps(struct bxe_softc *sc)
13290 {
13291     uint16_t link_status;
13292     int reg;
13293
13294     /* check if PCI Power Management is enabled */
13295     if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
13296         if (reg != 0) {
13297             BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
13298
13299             sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
13300             sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
13301         }
13302     }
13303
13304     link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
13305
13306     /* handle PCIe 2.0 workarounds for 57710 */
13307     if (CHIP_IS_E1(sc)) {
13308         /* workaround for 57710 errata E4_57710_27462 */
13309         sc->devinfo.pcie_link_speed =
13310             (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
13311
13312         /* workaround for 57710 errata E4_57710_27488 */
13313         sc->devinfo.pcie_link_width =
13314             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13315         if (sc->devinfo.pcie_link_speed > 1) {
13316             sc->devinfo.pcie_link_width =
13317                 ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
13318         }
13319     } else {
13320         sc->devinfo.pcie_link_speed =
13321             (link_status & PCIM_LINK_STA_SPEED);
13322         sc->devinfo.pcie_link_width =
13323             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13324     }
13325
13326     BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
13327           sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
13328
13329     sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
13330     sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
13331
13332     /* check if MSI capability is enabled */
13333     if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
13334         if (reg != 0) {
13335             BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
13336
13337             sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
13338             sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
13339         }
13340     }
13341
13342     /* check if MSI-X capability is enabled */
13343     if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
13344         if (reg != 0) {
13345             BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
13346
13347             sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
13348             sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
13349         }
13350     }
13351 }
13352
13353 static int
13354 bxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
13355 {
13356     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13357     uint32_t val;
13358
13359     /* get the outer vlan if we're in switch-dependent mode */
13360
13361     val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13362     mf_info->ext_id = (uint16_t)val;
13363
13364     mf_info->multi_vnics_mode = 1;
13365
13366     if (!VALID_OVLAN(mf_info->ext_id)) {
13367         BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13368         return (1);
13369     }
13370
13371     /* get the capabilities */
13372     if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13373         FUNC_MF_CFG_PROTOCOL_ISCSI) {
13374         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13375     } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13376                FUNC_MF_CFG_PROTOCOL_FCOE) {
13377         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13378     } else {
13379         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13380     }
13381
13382     mf_info->vnics_per_port =
13383         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13384
13385     return (0);
13386 }
13387
13388 static uint32_t
13389 bxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13390 {
13391     uint32_t retval = 0;
13392     uint32_t val;
13393
13394     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13395
13396     if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13397         if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13398             retval |= MF_PROTO_SUPPORT_ETHERNET;
13399         }
13400         if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13401             retval |= MF_PROTO_SUPPORT_ISCSI;
13402         }
13403         if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13404             retval |= MF_PROTO_SUPPORT_FCOE;
13405         }
13406     }
13407
13408     return (retval);
13409 }
13410
13411 static int
13412 bxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13413 {
13414     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13415     uint32_t val;
13416
13417     /*
13418      * There is no outer vlan if we're in switch-independent mode.
13419      * If the mac is valid then assume multi-function.
13420      */
13421
13422     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13423
13424     mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13425
13426     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13427
13428     mf_info->vnics_per_port =
13429         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13430
13431     return (0);
13432 }
13433
13434 static int
13435 bxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13436 {
13437     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13438     uint32_t e1hov_tag;
13439     uint32_t func_config;
13440     uint32_t niv_config;
13441
13442     mf_info->multi_vnics_mode = 1;
13443
13444     e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13445     func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13446     niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13447
13448     mf_info->ext_id =
13449         (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13450                    FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13451
13452     mf_info->default_vlan =
13453         (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13454                    FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13455
13456     mf_info->niv_allowed_priorities =
13457         (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13458                   FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13459
13460     mf_info->niv_default_cos =
13461         (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13462                   FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13463
13464     mf_info->afex_vlan_mode =
13465         ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13466          FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13467
13468     mf_info->niv_mba_enabled =
13469         ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13470          FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13471
13472     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13473
13474     mf_info->vnics_per_port =
13475         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13476
13477     return (0);
13478 }
13479
13480 static int
13481 bxe_check_valid_mf_cfg(struct bxe_softc *sc)
13482 {
13483     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13484     uint32_t mf_cfg1;
13485     uint32_t mf_cfg2;
13486     uint32_t ovlan1;
13487     uint32_t ovlan2;
13488     uint8_t i, j;
13489
13490     BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13491           SC_PORT(sc));
13492     BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13493           mf_info->mf_config[SC_VN(sc)]);
13494     BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13495           mf_info->multi_vnics_mode);
13496     BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13497           mf_info->vnics_per_port);
13498     BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13499           mf_info->ext_id);
13500     BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13501           mf_info->min_bw[0], mf_info->min_bw[1],
13502           mf_info->min_bw[2], mf_info->min_bw[3]);
13503     BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13504           mf_info->max_bw[0], mf_info->max_bw[1],
13505           mf_info->max_bw[2], mf_info->max_bw[3]);
13506     BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13507           sc->mac_addr_str);
13508
13509     /* various MF mode sanity checks... */
13510
13511     if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13512         BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13513               SC_PORT(sc));
13514         return (1);
13515     }
13516
13517     if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13518         BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13519               mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13520         return (1);
13521     }
13522
13523     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13524         /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13525         if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13526             BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13527                   SC_VN(sc), OVLAN(sc));
13528             return (1);
13529         }
13530
13531         if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13532             BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13533                   mf_info->multi_vnics_mode, OVLAN(sc));
13534             return (1);
13535         }
13536
13537         /*
13538          * Verify all functions are either MF or SF mode. If MF, make sure
13539          * sure that all non-hidden functions have a valid ovlan. If SF,
13540          * make sure that all non-hidden functions have an invalid ovlan.
13541          */
13542         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13543             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13544             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13545             if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13546                 (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13547                  ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13548                 BLOGE(sc, "mf_mode=SD function %d MF config "
13549                           "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13550                       i, mf_info->multi_vnics_mode, ovlan1);
13551                 return (1);
13552             }
13553         }
13554
13555         /* Verify all funcs on the same port each have a different ovlan. */
13556         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13557             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13558             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13559             /* iterate from the next function on the port to the max func */
13560             for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13561                 mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13562                 ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13563                 if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13564                     VALID_OVLAN(ovlan1) &&
13565                     !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13566                     VALID_OVLAN(ovlan2) &&
13567                     (ovlan1 == ovlan2)) {
13568                     BLOGE(sc, "mf_mode=SD functions %d and %d "
13569                               "have the same ovlan (%d)\n",
13570                           i, j, ovlan1);
13571                     return (1);
13572                 }
13573             }
13574         }
13575     } /* MULTI_FUNCTION_SD */
13576
13577     return (0);
13578 }
13579
13580 static int
13581 bxe_get_mf_cfg_info(struct bxe_softc *sc)
13582 {
13583     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13584     uint32_t val, mac_upper;
13585     uint8_t i, vnic;
13586
13587     /* initialize mf_info defaults */
13588     mf_info->vnics_per_port   = 1;
13589     mf_info->multi_vnics_mode = FALSE;
13590     mf_info->path_has_ovlan   = FALSE;
13591     mf_info->mf_mode          = SINGLE_FUNCTION;
13592
13593     if (!CHIP_IS_MF_CAP(sc)) {
13594         return (0);
13595     }
13596
13597     if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13598         BLOGE(sc, "Invalid mf_cfg_base!\n");
13599         return (1);
13600     }
13601
13602     /* get the MF mode (switch dependent / independent / single-function) */
13603
13604     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13605
13606     switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13607     {
13608     case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13609
13610         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13611
13612         /* check for legal upper mac bytes */
13613         if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13614             mf_info->mf_mode = MULTI_FUNCTION_SI;
13615         } else {
13616             BLOGE(sc, "Invalid config for Switch Independent mode\n");
13617         }
13618
13619         break;
13620
13621     case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13622     case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13623
13624         /* get outer vlan configuration */
13625         val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13626
13627         if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13628             FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13629             mf_info->mf_mode = MULTI_FUNCTION_SD;
13630         } else {
13631             BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13632         }
13633
13634         break;
13635
13636     case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13637
13638         /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13639         return (0);
13640
13641     case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13642
13643         /*
13644          * Mark MF mode as NIV if MCP version includes NPAR-SD support
13645          * and the MAC address is valid.
13646          */
13647         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13648
13649         if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13650             (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13651             mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13652         } else {
13653             BLOGE(sc, "Invalid config for AFEX mode\n");
13654         }
13655
13656         break;
13657
13658     default:
13659
13660         BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13661               (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13662
13663         return (1);
13664     }
13665
13666     /* set path mf_mode (which could be different than function mf_mode) */
13667     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13668         mf_info->path_has_ovlan = TRUE;
13669     } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13670         /*
13671          * Decide on path multi vnics mode. If we're not in MF mode and in
13672          * 4-port mode, this is good enough to check vnic-0 of the other port
13673          * on the same path
13674          */
13675         if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13676             uint8_t other_port = !(PORT_ID(sc) & 1);
13677             uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13678
13679             val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13680
13681             mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13682         }
13683     }
13684
13685     if (mf_info->mf_mode == SINGLE_FUNCTION) {
13686         /* invalid MF config */
13687         if (SC_VN(sc) >= 1) {
13688             BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13689             return (1);
13690         }
13691
13692         return (0);
13693     }
13694
13695     /* get the MF configuration */
13696     mf_info->mf_config[SC_VN(sc)] =
13697         MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13698
13699     switch(mf_info->mf_mode)
13700     {
13701     case MULTI_FUNCTION_SD:
13702
13703         bxe_get_shmem_mf_cfg_info_sd(sc);
13704         break;
13705
13706     case MULTI_FUNCTION_SI:
13707
13708         bxe_get_shmem_mf_cfg_info_si(sc);
13709         break;
13710
13711     case MULTI_FUNCTION_AFEX:
13712
13713         bxe_get_shmem_mf_cfg_info_niv(sc);
13714         break;
13715
13716     default:
13717
13718         BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13719               mf_info->mf_mode);
13720         return (1);
13721     }
13722
13723     /* get the congestion management parameters */
13724
13725     vnic = 0;
13726     FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13727         /* get min/max bw */
13728         val = MFCFG_RD(sc, func_mf_config[i].config);
13729         mf_info->min_bw[vnic] =
13730             ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13731         mf_info->max_bw[vnic] =
13732             ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13733         vnic++;
13734     }
13735
13736     return (bxe_check_valid_mf_cfg(sc));
13737 }
13738
13739 static int
13740 bxe_get_shmem_info(struct bxe_softc *sc)
13741 {
13742     int port;
13743     uint32_t mac_hi, mac_lo, val;
13744
13745     port = SC_PORT(sc);
13746     mac_hi = mac_lo = 0;
13747
13748     sc->link_params.sc   = sc;
13749     sc->link_params.port = port;
13750
13751     /* get the hardware config info */
13752     sc->devinfo.hw_config =
13753         SHMEM_RD(sc, dev_info.shared_hw_config.config);
13754     sc->devinfo.hw_config2 =
13755         SHMEM_RD(sc, dev_info.shared_hw_config.config2);
13756
13757     sc->link_params.hw_led_mode =
13758         ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
13759          SHARED_HW_CFG_LED_MODE_SHIFT);
13760
13761     /* get the port feature config */
13762     sc->port.config =
13763         SHMEM_RD(sc, dev_info.port_feature_config[port].config),
13764
13765     /* get the link params */
13766     sc->link_params.speed_cap_mask[0] =
13767         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
13768     sc->link_params.speed_cap_mask[1] =
13769         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
13770
13771     /* get the lane config */
13772     sc->link_params.lane_config =
13773         SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
13774
13775     /* get the link config */
13776     val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
13777     sc->port.link_config[ELINK_INT_PHY] = val;
13778     sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
13779     sc->port.link_config[ELINK_EXT_PHY1] =
13780         SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
13781
13782     /* get the override preemphasis flag and enable it or turn it off */
13783     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13784     if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
13785         sc->link_params.feature_config_flags |=
13786             ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13787     } else {
13788         sc->link_params.feature_config_flags &=
13789             ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
13790     }
13791
13792     /* get the initial value of the link params */
13793     sc->link_params.multi_phy_config =
13794         SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
13795
13796     /* get external phy info */
13797     sc->port.ext_phy_config =
13798         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
13799
13800     /* get the multifunction configuration */
13801     bxe_get_mf_cfg_info(sc);
13802
13803     /* get the mac address */
13804     if (IS_MF(sc)) {
13805         mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13806         mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
13807     } else {
13808         mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
13809         mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
13810     }
13811
13812     if ((mac_lo == 0) && (mac_hi == 0)) {
13813         *sc->mac_addr_str = 0;
13814         BLOGE(sc, "No Ethernet address programmed!\n");
13815     } else {
13816         sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
13817         sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
13818         sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
13819         sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
13820         sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
13821         sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
13822         snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
13823                  "%02x:%02x:%02x:%02x:%02x:%02x",
13824                  sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
13825                  sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
13826                  sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
13827         BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
13828     }
13829
13830     return (0);
13831 }
13832
13833 static void
13834 bxe_get_tunable_params(struct bxe_softc *sc)
13835 {
13836     /* sanity checks */
13837
13838     if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
13839         (bxe_interrupt_mode != INTR_MODE_MSI)  &&
13840         (bxe_interrupt_mode != INTR_MODE_MSIX)) {
13841         BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
13842         bxe_interrupt_mode = INTR_MODE_MSIX;
13843     }
13844
13845     if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
13846         BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
13847         bxe_queue_count = 0;
13848     }
13849
13850     if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
13851         if (bxe_max_rx_bufs == 0) {
13852             bxe_max_rx_bufs = RX_BD_USABLE;
13853         } else {
13854             BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
13855             bxe_max_rx_bufs = 2048;
13856         }
13857     }
13858
13859     if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
13860         BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
13861         bxe_hc_rx_ticks = 25;
13862     }
13863
13864     if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
13865         BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
13866         bxe_hc_tx_ticks = 50;
13867     }
13868
13869     if (bxe_max_aggregation_size == 0) {
13870         bxe_max_aggregation_size = TPA_AGG_SIZE;
13871     }
13872
13873     if (bxe_max_aggregation_size > 0xffff) {
13874         BLOGW(sc, "invalid max_aggregation_size (%d)\n",
13875               bxe_max_aggregation_size);
13876         bxe_max_aggregation_size = TPA_AGG_SIZE;
13877     }
13878
13879     if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
13880         BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
13881         bxe_mrrs = -1;
13882     }
13883
13884     if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
13885         BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
13886         bxe_autogreeen = 0;
13887     }
13888
13889     if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
13890         BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
13891         bxe_udp_rss = 0;
13892     }
13893
13894     /* pull in user settings */
13895
13896     sc->interrupt_mode       = bxe_interrupt_mode;
13897     sc->max_rx_bufs          = bxe_max_rx_bufs;
13898     sc->hc_rx_ticks          = bxe_hc_rx_ticks;
13899     sc->hc_tx_ticks          = bxe_hc_tx_ticks;
13900     sc->max_aggregation_size = bxe_max_aggregation_size;
13901     sc->mrrs                 = bxe_mrrs;
13902     sc->autogreeen           = bxe_autogreeen;
13903     sc->udp_rss              = bxe_udp_rss;
13904
13905     if (bxe_interrupt_mode == INTR_MODE_INTX) {
13906         sc->num_queues = 1;
13907     } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
13908         sc->num_queues =
13909             min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
13910                 MAX_RSS_CHAINS);
13911         if (sc->num_queues > mp_ncpus) {
13912             sc->num_queues = mp_ncpus;
13913         }
13914     }
13915
13916     BLOGD(sc, DBG_LOAD,
13917           "User Config: "
13918           "debug=0x%lx "
13919           "interrupt_mode=%d "
13920           "queue_count=%d "
13921           "hc_rx_ticks=%d "
13922           "hc_tx_ticks=%d "
13923           "rx_budget=%d "
13924           "max_aggregation_size=%d "
13925           "mrrs=%d "
13926           "autogreeen=%d "
13927           "udp_rss=%d\n",
13928           bxe_debug,
13929           sc->interrupt_mode,
13930           sc->num_queues,
13931           sc->hc_rx_ticks,
13932           sc->hc_tx_ticks,
13933           bxe_rx_budget,
13934           sc->max_aggregation_size,
13935           sc->mrrs,
13936           sc->autogreeen,
13937           sc->udp_rss);
13938 }
13939
13940 static int
13941 bxe_media_detect(struct bxe_softc *sc)
13942 {
13943     int port_type;
13944     uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
13945
13946     switch (sc->link_params.phy[phy_idx].media_type) {
13947     case ELINK_ETH_PHY_SFPP_10G_FIBER:
13948     case ELINK_ETH_PHY_XFP_FIBER:
13949         BLOGI(sc, "Found 10Gb Fiber media.\n");
13950         sc->media = IFM_10G_SR;
13951         port_type = PORT_FIBRE;
13952         break;
13953     case ELINK_ETH_PHY_SFP_1G_FIBER:
13954         BLOGI(sc, "Found 1Gb Fiber media.\n");
13955         sc->media = IFM_1000_SX;
13956         port_type = PORT_FIBRE;
13957         break;
13958     case ELINK_ETH_PHY_KR:
13959     case ELINK_ETH_PHY_CX4:
13960         BLOGI(sc, "Found 10GBase-CX4 media.\n");
13961         sc->media = IFM_10G_CX4;
13962         port_type = PORT_FIBRE;
13963         break;
13964     case ELINK_ETH_PHY_DA_TWINAX:
13965         BLOGI(sc, "Found 10Gb Twinax media.\n");
13966         sc->media = IFM_10G_TWINAX;
13967         port_type = PORT_DA;
13968         break;
13969     case ELINK_ETH_PHY_BASE_T:
13970         if (sc->link_params.speed_cap_mask[0] &
13971             PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
13972             BLOGI(sc, "Found 10GBase-T media.\n");
13973             sc->media = IFM_10G_T;
13974             port_type = PORT_TP;
13975         } else {
13976             BLOGI(sc, "Found 1000Base-T media.\n");
13977             sc->media = IFM_1000_T;
13978             port_type = PORT_TP;
13979         }
13980         break;
13981     case ELINK_ETH_PHY_NOT_PRESENT:
13982         BLOGI(sc, "Media not present.\n");
13983         sc->media = 0;
13984         port_type = PORT_OTHER;
13985         break;
13986     case ELINK_ETH_PHY_UNSPECIFIED:
13987     default:
13988         BLOGI(sc, "Unknown media!\n");
13989         sc->media = 0;
13990         port_type = PORT_OTHER;
13991         break;
13992     }
13993     return port_type;
13994 }
13995
13996 #define GET_FIELD(value, fname)                     \
13997     (((value) & (fname##_MASK)) >> (fname##_SHIFT))
13998 #define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
13999 #define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
14000
14001 static int
14002 bxe_get_igu_cam_info(struct bxe_softc *sc)
14003 {
14004     int pfid = SC_FUNC(sc);
14005     int igu_sb_id;
14006     uint32_t val;
14007     uint8_t fid, igu_sb_cnt = 0;
14008
14009     sc->igu_base_sb = 0xff;
14010
14011     if (CHIP_INT_MODE_IS_BC(sc)) {
14012         int vn = SC_VN(sc);
14013         igu_sb_cnt = sc->igu_sb_cnt;
14014         sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
14015                            FP_SB_MAX_E1x);
14016         sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
14017                           (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
14018         return (0);
14019     }
14020
14021     /* IGU in normal mode - read CAM */
14022     for (igu_sb_id = 0;
14023          igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
14024          igu_sb_id++) {
14025         val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
14026         if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
14027             continue;
14028         }
14029         fid = IGU_FID(val);
14030         if ((fid & IGU_FID_ENCODE_IS_PF)) {
14031             if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
14032                 continue;
14033             }
14034             if (IGU_VEC(val) == 0) {
14035                 /* default status block */
14036                 sc->igu_dsb_id = igu_sb_id;
14037             } else {
14038                 if (sc->igu_base_sb == 0xff) {
14039                     sc->igu_base_sb = igu_sb_id;
14040                 }
14041                 igu_sb_cnt++;
14042             }
14043         }
14044     }
14045
14046     /*
14047      * Due to new PF resource allocation by MFW T7.4 and above, it's optional
14048      * that number of CAM entries will not be equal to the value advertised in
14049      * PCI. Driver should use the minimal value of both as the actual status
14050      * block count
14051      */
14052     sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
14053
14054     if (igu_sb_cnt == 0) {
14055         BLOGE(sc, "CAM configuration error\n");
14056         return (-1);
14057     }
14058
14059     return (0);
14060 }
14061
14062 /*
14063  * Gather various information from the device config space, the device itself,
14064  * shmem, and the user input.
14065  */
14066 static int
14067 bxe_get_device_info(struct bxe_softc *sc)
14068 {
14069     uint32_t val;
14070     int rc;
14071
14072     /* Get the data for the device */
14073     sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
14074     sc->devinfo.device_id    = pci_get_device(sc->dev);
14075     sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
14076     sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
14077
14078     /* get the chip revision (chip metal comes from pci config space) */
14079     sc->devinfo.chip_id     =
14080     sc->link_params.chip_id =
14081         (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
14082          ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
14083          (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
14084          ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
14085
14086     /* force 57811 according to MISC register */
14087     if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
14088         if (CHIP_IS_57810(sc)) {
14089             sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
14090                                    (sc->devinfo.chip_id & 0x0000ffff));
14091         } else if (CHIP_IS_57810_MF(sc)) {
14092             sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
14093                                    (sc->devinfo.chip_id & 0x0000ffff));
14094         }
14095         sc->devinfo.chip_id |= 0x1;
14096     }
14097
14098     BLOGD(sc, DBG_LOAD,
14099           "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
14100           sc->devinfo.chip_id,
14101           ((sc->devinfo.chip_id >> 16) & 0xffff),
14102           ((sc->devinfo.chip_id >> 12) & 0xf),
14103           ((sc->devinfo.chip_id >>  4) & 0xff),
14104           ((sc->devinfo.chip_id >>  0) & 0xf));
14105
14106     val = (REG_RD(sc, 0x2874) & 0x55);
14107     if ((sc->devinfo.chip_id & 0x1) ||
14108         (CHIP_IS_E1(sc) && val) ||
14109         (CHIP_IS_E1H(sc) && (val == 0x55))) {
14110         sc->flags |= BXE_ONE_PORT_FLAG;
14111         BLOGD(sc, DBG_LOAD, "single port device\n");
14112     }
14113
14114     /* set the doorbell size */
14115     sc->doorbell_size = (1 << BXE_DB_SHIFT);
14116
14117     /* determine whether the device is in 2 port or 4 port mode */
14118     sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
14119     if (CHIP_IS_E2E3(sc)) {
14120         /*
14121          * Read port4mode_en_ovwr[0]:
14122          *   If 1, four port mode is in port4mode_en_ovwr[1].
14123          *   If 0, four port mode is in port4mode_en[0].
14124          */
14125         val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
14126         if (val & 1) {
14127             val = ((val >> 1) & 1);
14128         } else {
14129             val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
14130         }
14131
14132         sc->devinfo.chip_port_mode =
14133             (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
14134
14135         BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
14136     }
14137
14138     /* get the function and path info for the device */
14139     bxe_get_function_num(sc);
14140
14141     /* get the shared memory base address */
14142     sc->devinfo.shmem_base     =
14143     sc->link_params.shmem_base =
14144         REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
14145     sc->devinfo.shmem2_base =
14146         REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
14147                                   MISC_REG_GENERIC_CR_0));
14148
14149     BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
14150           sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
14151
14152     if (!sc->devinfo.shmem_base) {
14153         /* this should ONLY prevent upcoming shmem reads */
14154         BLOGI(sc, "MCP not active\n");
14155         sc->flags |= BXE_NO_MCP_FLAG;
14156         return (0);
14157     }
14158
14159     /* make sure the shared memory contents are valid */
14160     val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
14161     if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
14162         (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
14163         BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
14164         return (0);
14165     }
14166     BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
14167
14168     /* get the bootcode version */
14169     sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
14170     snprintf(sc->devinfo.bc_ver_str,
14171              sizeof(sc->devinfo.bc_ver_str),
14172              "%d.%d.%d",
14173              ((sc->devinfo.bc_ver >> 24) & 0xff),
14174              ((sc->devinfo.bc_ver >> 16) & 0xff),
14175              ((sc->devinfo.bc_ver >>  8) & 0xff));
14176     BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
14177
14178     /* get the bootcode shmem address */
14179     sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
14180     BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
14181
14182     /* clean indirect addresses as they're not used */
14183     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
14184     if (IS_PF(sc)) {
14185         REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
14186         REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
14187         REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
14188         REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
14189         if (CHIP_IS_E1x(sc)) {
14190             REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
14191             REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
14192             REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
14193             REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
14194         }
14195
14196         /*
14197          * Enable internal target-read (in case we are probed after PF
14198          * FLR). Must be done prior to any BAR read access. Only for
14199          * 57712 and up
14200          */
14201         if (!CHIP_IS_E1x(sc)) {
14202             REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
14203         }
14204     }
14205
14206     /* get the nvram size */
14207     val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
14208     sc->devinfo.flash_size =
14209         (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
14210     BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
14211
14212     /* get PCI capabilites */
14213     bxe_probe_pci_caps(sc);
14214
14215     bxe_set_power_state(sc, PCI_PM_D0);
14216
14217     /* get various configuration parameters from shmem */
14218     bxe_get_shmem_info(sc);
14219
14220     if (sc->devinfo.pcie_msix_cap_reg != 0) {
14221         val = pci_read_config(sc->dev,
14222                               (sc->devinfo.pcie_msix_cap_reg +
14223                                PCIR_MSIX_CTRL),
14224                               2);
14225         sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
14226     } else {
14227         sc->igu_sb_cnt = 1;
14228     }
14229
14230     sc->igu_base_addr = BAR_IGU_INTMEM;
14231
14232     /* initialize IGU parameters */
14233     if (CHIP_IS_E1x(sc)) {
14234         sc->devinfo.int_block = INT_BLOCK_HC;
14235         sc->igu_dsb_id = DEF_SB_IGU_ID;
14236         sc->igu_base_sb = 0;
14237     } else {
14238         sc->devinfo.int_block = INT_BLOCK_IGU;
14239
14240         /* do not allow device reset during IGU info preocessing */
14241         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14242
14243         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
14244
14245         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14246             int tout = 5000;
14247
14248             BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
14249
14250             val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
14251             REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
14252             REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
14253
14254             while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14255                 tout--;
14256                 DELAY(1000);
14257             }
14258
14259             if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14260                 BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
14261                 bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14262                 return (-1);
14263             }
14264         }
14265
14266         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14267             BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
14268             sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
14269         } else {
14270             BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
14271         }
14272
14273         rc = bxe_get_igu_cam_info(sc);
14274
14275         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14276
14277         if (rc) {
14278             return (rc);
14279         }
14280     }
14281
14282     /*
14283      * Get base FW non-default (fast path) status block ID. This value is
14284      * used to initialize the fw_sb_id saved on the fp/queue structure to
14285      * determine the id used by the FW.
14286      */
14287     if (CHIP_IS_E1x(sc)) {
14288         sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
14289     } else {
14290         /*
14291          * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
14292          * the same queue are indicated on the same IGU SB). So we prefer
14293          * FW and IGU SBs to be the same value.
14294          */
14295         sc->base_fw_ndsb = sc->igu_base_sb;
14296     }
14297
14298     BLOGD(sc, DBG_LOAD,
14299           "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
14300           sc->igu_dsb_id, sc->igu_base_sb,
14301           sc->igu_sb_cnt, sc->base_fw_ndsb);
14302
14303     elink_phy_probe(&sc->link_params);
14304
14305     return (0);
14306 }
14307
14308 static void
14309 bxe_link_settings_supported(struct bxe_softc *sc,
14310                             uint32_t         switch_cfg)
14311 {
14312     uint32_t cfg_size = 0;
14313     uint32_t idx;
14314     uint8_t port = SC_PORT(sc);
14315
14316     /* aggregation of supported attributes of all external phys */
14317     sc->port.supported[0] = 0;
14318     sc->port.supported[1] = 0;
14319
14320     switch (sc->link_params.num_phys) {
14321     case 1:
14322         sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
14323         cfg_size = 1;
14324         break;
14325     case 2:
14326         sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
14327         cfg_size = 1;
14328         break;
14329     case 3:
14330         if (sc->link_params.multi_phy_config &
14331             PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
14332             sc->port.supported[1] =
14333                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
14334             sc->port.supported[0] =
14335                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
14336         } else {
14337             sc->port.supported[0] =
14338                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
14339             sc->port.supported[1] =
14340                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
14341         }
14342         cfg_size = 2;
14343         break;
14344     }
14345
14346     if (!(sc->port.supported[0] || sc->port.supported[1])) {
14347         BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
14348               SHMEM_RD(sc,
14349                        dev_info.port_hw_config[port].external_phy_config),
14350               SHMEM_RD(sc,
14351                        dev_info.port_hw_config[port].external_phy_config2));
14352         return;
14353     }
14354
14355     if (CHIP_IS_E3(sc))
14356         sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
14357     else {
14358         switch (switch_cfg) {
14359         case ELINK_SWITCH_CFG_1G:
14360             sc->port.phy_addr =
14361                 REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
14362             break;
14363         case ELINK_SWITCH_CFG_10G:
14364             sc->port.phy_addr =
14365                 REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
14366             break;
14367         default:
14368             BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14369                   sc->port.link_config[0]);
14370             return;
14371         }
14372     }
14373
14374     BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14375
14376     /* mask what we support according to speed_cap_mask per configuration */
14377     for (idx = 0; idx < cfg_size; idx++) {
14378         if (!(sc->link_params.speed_cap_mask[idx] &
14379               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14380             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14381         }
14382
14383         if (!(sc->link_params.speed_cap_mask[idx] &
14384               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14385             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14386         }
14387
14388         if (!(sc->link_params.speed_cap_mask[idx] &
14389               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14390             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14391         }
14392
14393         if (!(sc->link_params.speed_cap_mask[idx] &
14394               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14395             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14396         }
14397
14398         if (!(sc->link_params.speed_cap_mask[idx] &
14399               PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14400             sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14401         }
14402
14403         if (!(sc->link_params.speed_cap_mask[idx] &
14404               PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14405             sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14406         }
14407
14408         if (!(sc->link_params.speed_cap_mask[idx] &
14409               PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14410             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14411         }
14412
14413         if (!(sc->link_params.speed_cap_mask[idx] &
14414               PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14415             sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14416         }
14417     }
14418
14419     BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14420           sc->port.supported[0], sc->port.supported[1]);
14421         ELINK_DEBUG_P2(sc, "PHY supported 0=0x%08x 1=0x%08x\n",
14422                                         sc->port.supported[0], sc->port.supported[1]);
14423 }
14424
14425 static void
14426 bxe_link_settings_requested(struct bxe_softc *sc)
14427 {
14428     uint32_t link_config;
14429     uint32_t idx;
14430     uint32_t cfg_size = 0;
14431
14432     sc->port.advertising[0] = 0;
14433     sc->port.advertising[1] = 0;
14434
14435     switch (sc->link_params.num_phys) {
14436     case 1:
14437     case 2:
14438         cfg_size = 1;
14439         break;
14440     case 3:
14441         cfg_size = 2;
14442         break;
14443     }
14444
14445     for (idx = 0; idx < cfg_size; idx++) {
14446         sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14447         link_config = sc->port.link_config[idx];
14448
14449         switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14450         case PORT_FEATURE_LINK_SPEED_AUTO:
14451             if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14452                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14453                 sc->port.advertising[idx] |= sc->port.supported[idx];
14454                 if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14455                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14456                     sc->port.advertising[idx] |=
14457                         (ELINK_SUPPORTED_100baseT_Half |
14458                          ELINK_SUPPORTED_100baseT_Full);
14459             } else {
14460                 /* force 10G, no AN */
14461                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14462                 sc->port.advertising[idx] |=
14463                     (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14464                 continue;
14465             }
14466             break;
14467
14468         case PORT_FEATURE_LINK_SPEED_10M_FULL:
14469             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14470                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14471                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14472                                               ADVERTISED_TP);
14473             } else {
14474                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14475                           "speed_cap_mask=0x%08x\n",
14476                       link_config, sc->link_params.speed_cap_mask[idx]);
14477                 return;
14478             }
14479             break;
14480
14481         case PORT_FEATURE_LINK_SPEED_10M_HALF:
14482             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14483                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14484                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14485                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14486                                               ADVERTISED_TP);
14487                                 ELINK_DEBUG_P1(sc, "driver requesting DUPLEX_HALF req_duplex = %x!\n",
14488                                                                 sc->link_params.req_duplex[idx]);
14489             } else {
14490                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14491                           "speed_cap_mask=0x%08x\n",
14492                       link_config, sc->link_params.speed_cap_mask[idx]);
14493                 return;
14494             }
14495             break;
14496
14497         case PORT_FEATURE_LINK_SPEED_100M_FULL:
14498             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14499                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14500                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14501                                               ADVERTISED_TP);
14502             } else {
14503                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14504                           "speed_cap_mask=0x%08x\n",
14505                       link_config, sc->link_params.speed_cap_mask[idx]);
14506                 return;
14507             }
14508             break;
14509
14510         case PORT_FEATURE_LINK_SPEED_100M_HALF:
14511             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14512                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14513                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14514                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14515                                               ADVERTISED_TP);
14516             } else {
14517                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14518                           "speed_cap_mask=0x%08x\n",
14519                       link_config, sc->link_params.speed_cap_mask[idx]);
14520                 return;
14521             }
14522             break;
14523
14524         case PORT_FEATURE_LINK_SPEED_1G:
14525             if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14526                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14527                 sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14528                                               ADVERTISED_TP);
14529             } else {
14530                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14531                           "speed_cap_mask=0x%08x\n",
14532                       link_config, sc->link_params.speed_cap_mask[idx]);
14533                 return;
14534             }
14535             break;
14536
14537         case PORT_FEATURE_LINK_SPEED_2_5G:
14538             if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14539                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14540                 sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14541                                               ADVERTISED_TP);
14542             } else {
14543                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14544                           "speed_cap_mask=0x%08x\n",
14545                       link_config, sc->link_params.speed_cap_mask[idx]);
14546                 return;
14547             }
14548             break;
14549
14550         case PORT_FEATURE_LINK_SPEED_10G_CX4:
14551             if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14552                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14553                 sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14554                                               ADVERTISED_FIBRE);
14555             } else {
14556                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14557                           "speed_cap_mask=0x%08x\n",
14558                       link_config, sc->link_params.speed_cap_mask[idx]);
14559                 return;
14560             }
14561             break;
14562
14563         case PORT_FEATURE_LINK_SPEED_20G:
14564             sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14565             break;
14566
14567         default:
14568             BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14569                       "speed_cap_mask=0x%08x\n",
14570                   link_config, sc->link_params.speed_cap_mask[idx]);
14571             sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14572             sc->port.advertising[idx] = sc->port.supported[idx];
14573             break;
14574         }
14575
14576         sc->link_params.req_flow_ctrl[idx] =
14577             (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14578
14579         if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14580             if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14581                 sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14582             } else {
14583                 bxe_set_requested_fc(sc);
14584             }
14585         }
14586
14587         BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14588                             "req_flow_ctrl=0x%x advertising=0x%x\n",
14589               sc->link_params.req_line_speed[idx],
14590               sc->link_params.req_duplex[idx],
14591               sc->link_params.req_flow_ctrl[idx],
14592               sc->port.advertising[idx]);
14593                 ELINK_DEBUG_P3(sc, "req_line_speed=%d req_duplex=%d "
14594                                                 "advertising=0x%x\n",
14595                                                 sc->link_params.req_line_speed[idx],
14596                                                 sc->link_params.req_duplex[idx],
14597                                                 sc->port.advertising[idx]);
14598     }
14599 }
14600
14601 static void
14602 bxe_get_phy_info(struct bxe_softc *sc)
14603 {
14604     uint8_t port = SC_PORT(sc);
14605     uint32_t config = sc->port.config;
14606     uint32_t eee_mode;
14607
14608     /* shmem data already read in bxe_get_shmem_info() */
14609
14610     ELINK_DEBUG_P3(sc, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14611                         "link_config0=0x%08x\n",
14612                sc->link_params.lane_config,
14613                sc->link_params.speed_cap_mask[0],
14614                sc->port.link_config[0]);
14615      
14616
14617     bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14618     bxe_link_settings_requested(sc);
14619
14620     if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14621         sc->link_params.feature_config_flags |=
14622             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14623     } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14624         sc->link_params.feature_config_flags &=
14625             ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14626     } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14627         sc->link_params.feature_config_flags |=
14628             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14629     }
14630
14631     /* configure link feature according to nvram value */
14632     eee_mode =
14633         (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14634           PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14635          PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14636     if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14637         sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14638                                     ELINK_EEE_MODE_ENABLE_LPI |
14639                                     ELINK_EEE_MODE_OUTPUT_TIME);
14640     } else {
14641         sc->link_params.eee_mode = 0;
14642     }
14643
14644     /* get the media type */
14645     bxe_media_detect(sc);
14646         ELINK_DEBUG_P1(sc, "detected media type\n", sc->media);
14647 }
14648
14649 static void
14650 bxe_get_params(struct bxe_softc *sc)
14651 {
14652     /* get user tunable params */
14653     bxe_get_tunable_params(sc);
14654
14655     /* select the RX and TX ring sizes */
14656     sc->tx_ring_size = TX_BD_USABLE;
14657     sc->rx_ring_size = RX_BD_USABLE;
14658
14659     /* XXX disable WoL */
14660     sc->wol = 0;
14661 }
14662
14663 static void
14664 bxe_set_modes_bitmap(struct bxe_softc *sc)
14665 {
14666     uint32_t flags = 0;
14667
14668     if (CHIP_REV_IS_FPGA(sc)) {
14669         SET_FLAGS(flags, MODE_FPGA);
14670     } else if (CHIP_REV_IS_EMUL(sc)) {
14671         SET_FLAGS(flags, MODE_EMUL);
14672     } else {
14673         SET_FLAGS(flags, MODE_ASIC);
14674     }
14675
14676     if (CHIP_IS_MODE_4_PORT(sc)) {
14677         SET_FLAGS(flags, MODE_PORT4);
14678     } else {
14679         SET_FLAGS(flags, MODE_PORT2);
14680     }
14681
14682     if (CHIP_IS_E2(sc)) {
14683         SET_FLAGS(flags, MODE_E2);
14684     } else if (CHIP_IS_E3(sc)) {
14685         SET_FLAGS(flags, MODE_E3);
14686         if (CHIP_REV(sc) == CHIP_REV_Ax) {
14687             SET_FLAGS(flags, MODE_E3_A0);
14688         } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14689             SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14690         }
14691     }
14692
14693     if (IS_MF(sc)) {
14694         SET_FLAGS(flags, MODE_MF);
14695         switch (sc->devinfo.mf_info.mf_mode) {
14696         case MULTI_FUNCTION_SD:
14697             SET_FLAGS(flags, MODE_MF_SD);
14698             break;
14699         case MULTI_FUNCTION_SI:
14700             SET_FLAGS(flags, MODE_MF_SI);
14701             break;
14702         case MULTI_FUNCTION_AFEX:
14703             SET_FLAGS(flags, MODE_MF_AFEX);
14704             break;
14705         }
14706     } else {
14707         SET_FLAGS(flags, MODE_SF);
14708     }
14709
14710 #if defined(__LITTLE_ENDIAN)
14711     SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14712 #else /* __BIG_ENDIAN */
14713     SET_FLAGS(flags, MODE_BIG_ENDIAN);
14714 #endif
14715
14716     INIT_MODE_FLAGS(sc) = flags;
14717 }
14718
14719 static int
14720 bxe_alloc_hsi_mem(struct bxe_softc *sc)
14721 {
14722     struct bxe_fastpath *fp;
14723     bus_addr_t busaddr;
14724     int max_agg_queues;
14725     int max_segments;
14726     bus_size_t max_size;
14727     bus_size_t max_seg_size;
14728     char buf[32];
14729     int rc;
14730     int i, j;
14731
14732     /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14733
14734     /* allocate the parent bus DMA tag */
14735     rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14736                             1,                        /* alignment */
14737                             0,                        /* boundary limit */
14738                             BUS_SPACE_MAXADDR,        /* restricted low */
14739                             BUS_SPACE_MAXADDR,        /* restricted hi */
14740                             NULL,                     /* addr filter() */
14741                             NULL,                     /* addr filter() arg */
14742                             BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14743                             BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14744                             BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14745                             0,                        /* flags */
14746                             NULL,                     /* lock() */
14747                             NULL,                     /* lock() arg */
14748                             &sc->parent_dma_tag);     /* returned dma tag */
14749     if (rc != 0) {
14750         BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14751         return (1);
14752     }
14753
14754     /************************/
14755     /* DEFAULT STATUS BLOCK */
14756     /************************/
14757
14758     if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
14759                       &sc->def_sb_dma, "default status block") != 0) {
14760         /* XXX */
14761         bus_dma_tag_destroy(sc->parent_dma_tag);
14762         return (1);
14763     }
14764
14765     sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
14766
14767     /***************/
14768     /* EVENT QUEUE */
14769     /***************/
14770
14771     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14772                       &sc->eq_dma, "event queue") != 0) {
14773         /* XXX */
14774         bxe_dma_free(sc, &sc->def_sb_dma);
14775         sc->def_sb = NULL;
14776         bus_dma_tag_destroy(sc->parent_dma_tag);
14777         return (1);
14778     }
14779
14780     sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
14781
14782     /*************/
14783     /* SLOW PATH */
14784     /*************/
14785
14786     if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
14787                       &sc->sp_dma, "slow path") != 0) {
14788         /* XXX */
14789         bxe_dma_free(sc, &sc->eq_dma);
14790         sc->eq = NULL;
14791         bxe_dma_free(sc, &sc->def_sb_dma);
14792         sc->def_sb = NULL;
14793         bus_dma_tag_destroy(sc->parent_dma_tag);
14794         return (1);
14795     }
14796
14797     sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
14798
14799     /*******************/
14800     /* SLOW PATH QUEUE */
14801     /*******************/
14802
14803     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
14804                       &sc->spq_dma, "slow path queue") != 0) {
14805         /* XXX */
14806         bxe_dma_free(sc, &sc->sp_dma);
14807         sc->sp = NULL;
14808         bxe_dma_free(sc, &sc->eq_dma);
14809         sc->eq = NULL;
14810         bxe_dma_free(sc, &sc->def_sb_dma);
14811         sc->def_sb = NULL;
14812         bus_dma_tag_destroy(sc->parent_dma_tag);
14813         return (1);
14814     }
14815
14816     sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
14817
14818     /***************************/
14819     /* FW DECOMPRESSION BUFFER */
14820     /***************************/
14821
14822     if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
14823                       "fw decompression buffer") != 0) {
14824         /* XXX */
14825         bxe_dma_free(sc, &sc->spq_dma);
14826         sc->spq = NULL;
14827         bxe_dma_free(sc, &sc->sp_dma);
14828         sc->sp = NULL;
14829         bxe_dma_free(sc, &sc->eq_dma);
14830         sc->eq = NULL;
14831         bxe_dma_free(sc, &sc->def_sb_dma);
14832         sc->def_sb = NULL;
14833         bus_dma_tag_destroy(sc->parent_dma_tag);
14834         return (1);
14835     }
14836
14837     sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
14838
14839     if ((sc->gz_strm =
14840          malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
14841         /* XXX */
14842         bxe_dma_free(sc, &sc->gz_buf_dma);
14843         sc->gz_buf = NULL;
14844         bxe_dma_free(sc, &sc->spq_dma);
14845         sc->spq = NULL;
14846         bxe_dma_free(sc, &sc->sp_dma);
14847         sc->sp = NULL;
14848         bxe_dma_free(sc, &sc->eq_dma);
14849         sc->eq = NULL;
14850         bxe_dma_free(sc, &sc->def_sb_dma);
14851         sc->def_sb = NULL;
14852         bus_dma_tag_destroy(sc->parent_dma_tag);
14853         return (1);
14854     }
14855
14856     /*************/
14857     /* FASTPATHS */
14858     /*************/
14859
14860     /* allocate DMA memory for each fastpath structure */
14861     for (i = 0; i < sc->num_queues; i++) {
14862         fp = &sc->fp[i];
14863         fp->sc    = sc;
14864         fp->index = i;
14865
14866         /*******************/
14867         /* FP STATUS BLOCK */
14868         /*******************/
14869
14870         snprintf(buf, sizeof(buf), "fp %d status block", i);
14871         if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
14872                           &fp->sb_dma, buf) != 0) {
14873             /* XXX unwind and free previous fastpath allocations */
14874             BLOGE(sc, "Failed to alloc %s\n", buf);
14875             return (1);
14876         } else {
14877             if (CHIP_IS_E2E3(sc)) {
14878                 fp->status_block.e2_sb =
14879                     (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
14880             } else {
14881                 fp->status_block.e1x_sb =
14882                     (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
14883             }
14884         }
14885
14886         /******************/
14887         /* FP TX BD CHAIN */
14888         /******************/
14889
14890         snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
14891         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
14892                           &fp->tx_dma, buf) != 0) {
14893             /* XXX unwind and free previous fastpath allocations */
14894             BLOGE(sc, "Failed to alloc %s\n", buf);
14895             return (1);
14896         } else {
14897             fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
14898         }
14899
14900         /* link together the tx bd chain pages */
14901         for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
14902             /* index into the tx bd chain array to last entry per page */
14903             struct eth_tx_next_bd *tx_next_bd =
14904                 &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
14905             /* point to the next page and wrap from last page */
14906             busaddr = (fp->tx_dma.paddr +
14907                        (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
14908             tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
14909             tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
14910         }
14911
14912         /******************/
14913         /* FP RX BD CHAIN */
14914         /******************/
14915
14916         snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
14917         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
14918                           &fp->rx_dma, buf) != 0) {
14919             /* XXX unwind and free previous fastpath allocations */
14920             BLOGE(sc, "Failed to alloc %s\n", buf);
14921             return (1);
14922         } else {
14923             fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
14924         }
14925
14926         /* link together the rx bd chain pages */
14927         for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
14928             /* index into the rx bd chain array to last entry per page */
14929             struct eth_rx_bd *rx_bd =
14930                 &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
14931             /* point to the next page and wrap from last page */
14932             busaddr = (fp->rx_dma.paddr +
14933                        (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
14934             rx_bd->addr_hi = htole32(U64_HI(busaddr));
14935             rx_bd->addr_lo = htole32(U64_LO(busaddr));
14936         }
14937
14938         /*******************/
14939         /* FP RX RCQ CHAIN */
14940         /*******************/
14941
14942         snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
14943         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
14944                           &fp->rcq_dma, buf) != 0) {
14945             /* XXX unwind and free previous fastpath allocations */
14946             BLOGE(sc, "Failed to alloc %s\n", buf);
14947             return (1);
14948         } else {
14949             fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
14950         }
14951
14952         /* link together the rcq chain pages */
14953         for (j = 1; j <= RCQ_NUM_PAGES; j++) {
14954             /* index into the rcq chain array to last entry per page */
14955             struct eth_rx_cqe_next_page *rx_cqe_next =
14956                 (struct eth_rx_cqe_next_page *)
14957                 &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
14958             /* point to the next page and wrap from last page */
14959             busaddr = (fp->rcq_dma.paddr +
14960                        (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
14961             rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
14962             rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
14963         }
14964
14965         /*******************/
14966         /* FP RX SGE CHAIN */
14967         /*******************/
14968
14969         snprintf(buf, sizeof(buf), "fp %d sge chain", i);
14970         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
14971                           &fp->rx_sge_dma, buf) != 0) {
14972             /* XXX unwind and free previous fastpath allocations */
14973             BLOGE(sc, "Failed to alloc %s\n", buf);
14974             return (1);
14975         } else {
14976             fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
14977         }
14978
14979         /* link together the sge chain pages */
14980         for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
14981             /* index into the rcq chain array to last entry per page */
14982             struct eth_rx_sge *rx_sge =
14983                 &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
14984             /* point to the next page and wrap from last page */
14985             busaddr = (fp->rx_sge_dma.paddr +
14986                        (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
14987             rx_sge->addr_hi = htole32(U64_HI(busaddr));
14988             rx_sge->addr_lo = htole32(U64_LO(busaddr));
14989         }
14990
14991         /***********************/
14992         /* FP TX MBUF DMA MAPS */
14993         /***********************/
14994
14995         /* set required sizes before mapping to conserve resources */
14996         if (sc->ifnet->if_capenable & (IFCAP_TSO4 | IFCAP_TSO6)) {
14997             max_size     = BXE_TSO_MAX_SIZE;
14998             max_segments = BXE_TSO_MAX_SEGMENTS;
14999             max_seg_size = BXE_TSO_MAX_SEG_SIZE;
15000         } else {
15001             max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
15002             max_segments = BXE_MAX_SEGMENTS;
15003             max_seg_size = MCLBYTES;
15004         }
15005
15006         /* create a dma tag for the tx mbufs */
15007         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15008                                 1,                  /* alignment */
15009                                 0,                  /* boundary limit */
15010                                 BUS_SPACE_MAXADDR,  /* restricted low */
15011                                 BUS_SPACE_MAXADDR,  /* restricted hi */
15012                                 NULL,               /* addr filter() */
15013                                 NULL,               /* addr filter() arg */
15014                                 max_size,           /* max map size */
15015                                 max_segments,       /* num discontinuous */
15016                                 max_seg_size,       /* max seg size */
15017                                 0,                  /* flags */
15018                                 NULL,               /* lock() */
15019                                 NULL,               /* lock() arg */
15020                                 &fp->tx_mbuf_tag);  /* returned dma tag */
15021         if (rc != 0) {
15022             /* XXX unwind and free previous fastpath allocations */
15023             BLOGE(sc, "Failed to create dma tag for "
15024                       "'fp %d tx mbufs' (%d)\n", i, rc);
15025             return (1);
15026         }
15027
15028         /* create dma maps for each of the tx mbuf clusters */
15029         for (j = 0; j < TX_BD_TOTAL; j++) {
15030             if (bus_dmamap_create(fp->tx_mbuf_tag,
15031                                   BUS_DMA_NOWAIT,
15032                                   &fp->tx_mbuf_chain[j].m_map)) {
15033                 /* XXX unwind and free previous fastpath allocations */
15034                 BLOGE(sc, "Failed to create dma map for "
15035                           "'fp %d tx mbuf %d' (%d)\n", i, j, rc);
15036                 return (1);
15037             }
15038         }
15039
15040         /***********************/
15041         /* FP RX MBUF DMA MAPS */
15042         /***********************/
15043
15044         /* create a dma tag for the rx mbufs */
15045         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15046                                 1,                  /* alignment */
15047                                 0,                  /* boundary limit */
15048                                 BUS_SPACE_MAXADDR,  /* restricted low */
15049                                 BUS_SPACE_MAXADDR,  /* restricted hi */
15050                                 NULL,               /* addr filter() */
15051                                 NULL,               /* addr filter() arg */
15052                                 MJUM9BYTES,         /* max map size */
15053                                 1,                  /* num discontinuous */
15054                                 MJUM9BYTES,         /* max seg size */
15055                                 0,                  /* flags */
15056                                 NULL,               /* lock() */
15057                                 NULL,               /* lock() arg */
15058                                 &fp->rx_mbuf_tag);  /* returned dma tag */
15059         if (rc != 0) {
15060             /* XXX unwind and free previous fastpath allocations */
15061             BLOGE(sc, "Failed to create dma tag for "
15062                       "'fp %d rx mbufs' (%d)\n", i, rc);
15063             return (1);
15064         }
15065
15066         /* create dma maps for each of the rx mbuf clusters */
15067         for (j = 0; j < RX_BD_TOTAL; j++) {
15068             if (bus_dmamap_create(fp->rx_mbuf_tag,
15069                                   BUS_DMA_NOWAIT,
15070                                   &fp->rx_mbuf_chain[j].m_map)) {
15071                 /* XXX unwind and free previous fastpath allocations */
15072                 BLOGE(sc, "Failed to create dma map for "
15073                           "'fp %d rx mbuf %d' (%d)\n", i, j, rc);
15074                 return (1);
15075             }
15076         }
15077
15078         /* create dma map for the spare rx mbuf cluster */
15079         if (bus_dmamap_create(fp->rx_mbuf_tag,
15080                               BUS_DMA_NOWAIT,
15081                               &fp->rx_mbuf_spare_map)) {
15082             /* XXX unwind and free previous fastpath allocations */
15083             BLOGE(sc, "Failed to create dma map for "
15084                       "'fp %d spare rx mbuf' (%d)\n", i, rc);
15085             return (1);
15086         }
15087
15088         /***************************/
15089         /* FP RX SGE MBUF DMA MAPS */
15090         /***************************/
15091
15092         /* create a dma tag for the rx sge mbufs */
15093         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15094                                 1,                  /* alignment */
15095                                 0,                  /* boundary limit */
15096                                 BUS_SPACE_MAXADDR,  /* restricted low */
15097                                 BUS_SPACE_MAXADDR,  /* restricted hi */
15098                                 NULL,               /* addr filter() */
15099                                 NULL,               /* addr filter() arg */
15100                                 BCM_PAGE_SIZE,      /* max map size */
15101                                 1,                  /* num discontinuous */
15102                                 BCM_PAGE_SIZE,      /* max seg size */
15103                                 0,                  /* flags */
15104                                 NULL,               /* lock() */
15105                                 NULL,               /* lock() arg */
15106                                 &fp->rx_sge_mbuf_tag); /* returned dma tag */
15107         if (rc != 0) {
15108             /* XXX unwind and free previous fastpath allocations */
15109             BLOGE(sc, "Failed to create dma tag for "
15110                       "'fp %d rx sge mbufs' (%d)\n", i, rc);
15111             return (1);
15112         }
15113
15114         /* create dma maps for the rx sge mbuf clusters */
15115         for (j = 0; j < RX_SGE_TOTAL; j++) {
15116             if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15117                                   BUS_DMA_NOWAIT,
15118                                   &fp->rx_sge_mbuf_chain[j].m_map)) {
15119                 /* XXX unwind and free previous fastpath allocations */
15120                 BLOGE(sc, "Failed to create dma map for "
15121                           "'fp %d rx sge mbuf %d' (%d)\n", i, j, rc);
15122                 return (1);
15123             }
15124         }
15125
15126         /* create dma map for the spare rx sge mbuf cluster */
15127         if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15128                               BUS_DMA_NOWAIT,
15129                               &fp->rx_sge_mbuf_spare_map)) {
15130             /* XXX unwind and free previous fastpath allocations */
15131             BLOGE(sc, "Failed to create dma map for "
15132                       "'fp %d spare rx sge mbuf' (%d)\n", i, rc);
15133             return (1);
15134         }
15135
15136         /***************************/
15137         /* FP RX TPA MBUF DMA MAPS */
15138         /***************************/
15139
15140         /* create dma maps for the rx tpa mbuf clusters */
15141         max_agg_queues = MAX_AGG_QS(sc);
15142
15143         for (j = 0; j < max_agg_queues; j++) {
15144             if (bus_dmamap_create(fp->rx_mbuf_tag,
15145                                   BUS_DMA_NOWAIT,
15146                                   &fp->rx_tpa_info[j].bd.m_map)) {
15147                 /* XXX unwind and free previous fastpath allocations */
15148                 BLOGE(sc, "Failed to create dma map for "
15149                           "'fp %d rx tpa mbuf %d' (%d)\n", i, j, rc);
15150                 return (1);
15151             }
15152         }
15153
15154         /* create dma map for the spare rx tpa mbuf cluster */
15155         if (bus_dmamap_create(fp->rx_mbuf_tag,
15156                               BUS_DMA_NOWAIT,
15157                               &fp->rx_tpa_info_mbuf_spare_map)) {
15158             /* XXX unwind and free previous fastpath allocations */
15159             BLOGE(sc, "Failed to create dma map for "
15160                       "'fp %d spare rx tpa mbuf' (%d)\n", i, rc);
15161             return (1);
15162         }
15163
15164         bxe_init_sge_ring_bit_mask(fp);
15165     }
15166
15167     return (0);
15168 }
15169
15170 static void
15171 bxe_free_hsi_mem(struct bxe_softc *sc)
15172 {
15173     struct bxe_fastpath *fp;
15174     int max_agg_queues;
15175     int i, j;
15176
15177     if (sc->parent_dma_tag == NULL) {
15178         return; /* assume nothing was allocated */
15179     }
15180
15181     for (i = 0; i < sc->num_queues; i++) {
15182         fp = &sc->fp[i];
15183
15184         /*******************/
15185         /* FP STATUS BLOCK */
15186         /*******************/
15187
15188         bxe_dma_free(sc, &fp->sb_dma);
15189         memset(&fp->status_block, 0, sizeof(fp->status_block));
15190
15191         /******************/
15192         /* FP TX BD CHAIN */
15193         /******************/
15194
15195         bxe_dma_free(sc, &fp->tx_dma);
15196         fp->tx_chain = NULL;
15197
15198         /******************/
15199         /* FP RX BD CHAIN */
15200         /******************/
15201
15202         bxe_dma_free(sc, &fp->rx_dma);
15203         fp->rx_chain = NULL;
15204
15205         /*******************/
15206         /* FP RX RCQ CHAIN */
15207         /*******************/
15208
15209         bxe_dma_free(sc, &fp->rcq_dma);
15210         fp->rcq_chain = NULL;
15211
15212         /*******************/
15213         /* FP RX SGE CHAIN */
15214         /*******************/
15215
15216         bxe_dma_free(sc, &fp->rx_sge_dma);
15217         fp->rx_sge_chain = NULL;
15218
15219         /***********************/
15220         /* FP TX MBUF DMA MAPS */
15221         /***********************/
15222
15223         if (fp->tx_mbuf_tag != NULL) {
15224             for (j = 0; j < TX_BD_TOTAL; j++) {
15225                 if (fp->tx_mbuf_chain[j].m_map != NULL) {
15226                     bus_dmamap_unload(fp->tx_mbuf_tag,
15227                                       fp->tx_mbuf_chain[j].m_map);
15228                     bus_dmamap_destroy(fp->tx_mbuf_tag,
15229                                        fp->tx_mbuf_chain[j].m_map);
15230                 }
15231             }
15232
15233             bus_dma_tag_destroy(fp->tx_mbuf_tag);
15234             fp->tx_mbuf_tag = NULL;
15235         }
15236
15237         /***********************/
15238         /* FP RX MBUF DMA MAPS */
15239         /***********************/
15240
15241         if (fp->rx_mbuf_tag != NULL) {
15242             for (j = 0; j < RX_BD_TOTAL; j++) {
15243                 if (fp->rx_mbuf_chain[j].m_map != NULL) {
15244                     bus_dmamap_unload(fp->rx_mbuf_tag,
15245                                       fp->rx_mbuf_chain[j].m_map);
15246                     bus_dmamap_destroy(fp->rx_mbuf_tag,
15247                                        fp->rx_mbuf_chain[j].m_map);
15248                 }
15249             }
15250
15251             if (fp->rx_mbuf_spare_map != NULL) {
15252                 bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15253                 bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15254             }
15255
15256             /***************************/
15257             /* FP RX TPA MBUF DMA MAPS */
15258             /***************************/
15259
15260             max_agg_queues = MAX_AGG_QS(sc);
15261
15262             for (j = 0; j < max_agg_queues; j++) {
15263                 if (fp->rx_tpa_info[j].bd.m_map != NULL) {
15264                     bus_dmamap_unload(fp->rx_mbuf_tag,
15265                                       fp->rx_tpa_info[j].bd.m_map);
15266                     bus_dmamap_destroy(fp->rx_mbuf_tag,
15267                                        fp->rx_tpa_info[j].bd.m_map);
15268                 }
15269             }
15270
15271             if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
15272                 bus_dmamap_unload(fp->rx_mbuf_tag,
15273                                   fp->rx_tpa_info_mbuf_spare_map);
15274                 bus_dmamap_destroy(fp->rx_mbuf_tag,
15275                                    fp->rx_tpa_info_mbuf_spare_map);
15276             }
15277
15278             bus_dma_tag_destroy(fp->rx_mbuf_tag);
15279             fp->rx_mbuf_tag = NULL;
15280         }
15281
15282         /***************************/
15283         /* FP RX SGE MBUF DMA MAPS */
15284         /***************************/
15285
15286         if (fp->rx_sge_mbuf_tag != NULL) {
15287             for (j = 0; j < RX_SGE_TOTAL; j++) {
15288                 if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
15289                     bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15290                                       fp->rx_sge_mbuf_chain[j].m_map);
15291                     bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15292                                        fp->rx_sge_mbuf_chain[j].m_map);
15293                 }
15294             }
15295
15296             if (fp->rx_sge_mbuf_spare_map != NULL) {
15297                 bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15298                                   fp->rx_sge_mbuf_spare_map);
15299                 bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15300                                    fp->rx_sge_mbuf_spare_map);
15301             }
15302
15303             bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
15304             fp->rx_sge_mbuf_tag = NULL;
15305         }
15306     }
15307
15308     /***************************/
15309     /* FW DECOMPRESSION BUFFER */
15310     /***************************/
15311
15312     bxe_dma_free(sc, &sc->gz_buf_dma);
15313     sc->gz_buf = NULL;
15314     free(sc->gz_strm, M_DEVBUF);
15315     sc->gz_strm = NULL;
15316
15317     /*******************/
15318     /* SLOW PATH QUEUE */
15319     /*******************/
15320
15321     bxe_dma_free(sc, &sc->spq_dma);
15322     sc->spq = NULL;
15323
15324     /*************/
15325     /* SLOW PATH */
15326     /*************/
15327
15328     bxe_dma_free(sc, &sc->sp_dma);
15329     sc->sp = NULL;
15330
15331     /***************/
15332     /* EVENT QUEUE */
15333     /***************/
15334
15335     bxe_dma_free(sc, &sc->eq_dma);
15336     sc->eq = NULL;
15337
15338     /************************/
15339     /* DEFAULT STATUS BLOCK */
15340     /************************/
15341
15342     bxe_dma_free(sc, &sc->def_sb_dma);
15343     sc->def_sb = NULL;
15344
15345     bus_dma_tag_destroy(sc->parent_dma_tag);
15346     sc->parent_dma_tag = NULL;
15347 }
15348
15349 /*
15350  * Previous driver DMAE transaction may have occurred when pre-boot stage
15351  * ended and boot began. This would invalidate the addresses of the
15352  * transaction, resulting in was-error bit set in the PCI causing all
15353  * hw-to-host PCIe transactions to timeout. If this happened we want to clear
15354  * the interrupt which detected this from the pglueb and the was-done bit
15355  */
15356 static void
15357 bxe_prev_interrupted_dmae(struct bxe_softc *sc)
15358 {
15359     uint32_t val;
15360
15361     if (!CHIP_IS_E1x(sc)) {
15362         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
15363         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
15364             BLOGD(sc, DBG_LOAD,
15365                   "Clearing 'was-error' bit that was set in pglueb");
15366             REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
15367         }
15368     }
15369 }
15370
15371 static int
15372 bxe_prev_mcp_done(struct bxe_softc *sc)
15373 {
15374     uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15375                                  DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15376     if (!rc) {
15377         BLOGE(sc, "MCP response failure, aborting\n");
15378         return (-1);
15379     }
15380
15381     return (0);
15382 }
15383
15384 static struct bxe_prev_list_node *
15385 bxe_prev_path_get_entry(struct bxe_softc *sc)
15386 {
15387     struct bxe_prev_list_node *tmp;
15388
15389     LIST_FOREACH(tmp, &bxe_prev_list, node) {
15390         if ((sc->pcie_bus == tmp->bus) &&
15391             (sc->pcie_device == tmp->slot) &&
15392             (SC_PATH(sc) == tmp->path)) {
15393             return (tmp);
15394         }
15395     }
15396
15397     return (NULL);
15398 }
15399
15400 static uint8_t
15401 bxe_prev_is_path_marked(struct bxe_softc *sc)
15402 {
15403     struct bxe_prev_list_node *tmp;
15404     int rc = FALSE;
15405
15406     mtx_lock(&bxe_prev_mtx);
15407
15408     tmp = bxe_prev_path_get_entry(sc);
15409     if (tmp) {
15410         if (tmp->aer) {
15411             BLOGD(sc, DBG_LOAD,
15412                   "Path %d/%d/%d was marked by AER\n",
15413                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15414         } else {
15415             rc = TRUE;
15416             BLOGD(sc, DBG_LOAD,
15417                   "Path %d/%d/%d was already cleaned from previous drivers\n",
15418                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15419         }
15420     }
15421
15422     mtx_unlock(&bxe_prev_mtx);
15423
15424     return (rc);
15425 }
15426
15427 static int
15428 bxe_prev_mark_path(struct bxe_softc *sc,
15429                    uint8_t          after_undi)
15430 {
15431     struct bxe_prev_list_node *tmp;
15432
15433     mtx_lock(&bxe_prev_mtx);
15434
15435     /* Check whether the entry for this path already exists */
15436     tmp = bxe_prev_path_get_entry(sc);
15437     if (tmp) {
15438         if (!tmp->aer) {
15439             BLOGD(sc, DBG_LOAD,
15440                   "Re-marking AER in path %d/%d/%d\n",
15441                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15442         } else {
15443             BLOGD(sc, DBG_LOAD,
15444                   "Removing AER indication from path %d/%d/%d\n",
15445                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15446             tmp->aer = 0;
15447         }
15448
15449         mtx_unlock(&bxe_prev_mtx);
15450         return (0);
15451     }
15452
15453     mtx_unlock(&bxe_prev_mtx);
15454
15455     /* Create an entry for this path and add it */
15456     tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15457                  (M_NOWAIT | M_ZERO));
15458     if (!tmp) {
15459         BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15460         return (-1);
15461     }
15462
15463     tmp->bus  = sc->pcie_bus;
15464     tmp->slot = sc->pcie_device;
15465     tmp->path = SC_PATH(sc);
15466     tmp->aer  = 0;
15467     tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15468
15469     mtx_lock(&bxe_prev_mtx);
15470
15471     BLOGD(sc, DBG_LOAD,
15472           "Marked path %d/%d/%d - finished previous unload\n",
15473           sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15474     LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15475
15476     mtx_unlock(&bxe_prev_mtx);
15477
15478     return (0);
15479 }
15480
15481 static int
15482 bxe_do_flr(struct bxe_softc *sc)
15483 {
15484     int i;
15485
15486     /* only E2 and onwards support FLR */
15487     if (CHIP_IS_E1x(sc)) {
15488         BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15489         return (-1);
15490     }
15491
15492     /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15493     if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15494         BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15495               sc->devinfo.bc_ver);
15496         return (-1);
15497     }
15498
15499     /* Wait for Transaction Pending bit clean */
15500     for (i = 0; i < 4; i++) {
15501         if (i) {
15502             DELAY(((1 << (i - 1)) * 100) * 1000);
15503         }
15504
15505         if (!bxe_is_pcie_pending(sc)) {
15506             goto clear;
15507         }
15508     }
15509
15510     BLOGE(sc, "PCIE transaction is not cleared, "
15511               "proceeding with reset anyway\n");
15512
15513 clear:
15514
15515     BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15516     bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15517
15518     return (0);
15519 }
15520
15521 struct bxe_mac_vals {
15522     uint32_t xmac_addr;
15523     uint32_t xmac_val;
15524     uint32_t emac_addr;
15525     uint32_t emac_val;
15526     uint32_t umac_addr;
15527     uint32_t umac_val;
15528     uint32_t bmac_addr;
15529     uint32_t bmac_val[2];
15530 };
15531
15532 static void
15533 bxe_prev_unload_close_mac(struct bxe_softc *sc,
15534                           struct bxe_mac_vals *vals)
15535 {
15536     uint32_t val, base_addr, offset, mask, reset_reg;
15537     uint8_t mac_stopped = FALSE;
15538     uint8_t port = SC_PORT(sc);
15539     uint32_t wb_data[2];
15540
15541     /* reset addresses as they also mark which values were changed */
15542     vals->bmac_addr = 0;
15543     vals->umac_addr = 0;
15544     vals->xmac_addr = 0;
15545     vals->emac_addr = 0;
15546
15547     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15548
15549     if (!CHIP_IS_E3(sc)) {
15550         val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15551         mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15552         if ((mask & reset_reg) && val) {
15553             BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15554             base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15555                                     : NIG_REG_INGRESS_BMAC0_MEM;
15556             offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15557                                     : BIGMAC_REGISTER_BMAC_CONTROL;
15558
15559             /*
15560              * use rd/wr since we cannot use dmae. This is safe
15561              * since MCP won't access the bus due to the request
15562              * to unload, and no function on the path can be
15563              * loaded at this time.
15564              */
15565             wb_data[0] = REG_RD(sc, base_addr + offset);
15566             wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15567             vals->bmac_addr = base_addr + offset;
15568             vals->bmac_val[0] = wb_data[0];
15569             vals->bmac_val[1] = wb_data[1];
15570             wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15571             REG_WR(sc, vals->bmac_addr, wb_data[0]);
15572             REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15573         }
15574
15575         BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15576         vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15577         vals->emac_val = REG_RD(sc, vals->emac_addr);
15578         REG_WR(sc, vals->emac_addr, 0);
15579         mac_stopped = TRUE;
15580     } else {
15581         if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15582             BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15583             base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15584             val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15585             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15586             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15587             vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15588             vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15589             REG_WR(sc, vals->xmac_addr, 0);
15590             mac_stopped = TRUE;
15591         }
15592
15593         mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15594         if (mask & reset_reg) {
15595             BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15596             base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15597             vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15598             vals->umac_val = REG_RD(sc, vals->umac_addr);
15599             REG_WR(sc, vals->umac_addr, 0);
15600             mac_stopped = TRUE;
15601         }
15602     }
15603
15604     if (mac_stopped) {
15605         DELAY(20000);
15606     }
15607 }
15608
15609 #define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15610 #define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15611 #define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15612 #define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15613
15614 static void
15615 bxe_prev_unload_undi_inc(struct bxe_softc *sc,
15616                          uint8_t          port,
15617                          uint8_t          inc)
15618 {
15619     uint16_t rcq, bd;
15620     uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15621
15622     rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15623     bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15624
15625     tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15626     REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15627
15628     BLOGD(sc, DBG_LOAD,
15629           "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15630           port, bd, rcq);
15631 }
15632
15633 static int
15634 bxe_prev_unload_common(struct bxe_softc *sc)
15635 {
15636     uint32_t reset_reg, tmp_reg = 0, rc;
15637     uint8_t prev_undi = FALSE;
15638     struct bxe_mac_vals mac_vals;
15639     uint32_t timer_count = 1000;
15640     uint32_t prev_brb;
15641
15642     /*
15643      * It is possible a previous function received 'common' answer,
15644      * but hasn't loaded yet, therefore creating a scenario of
15645      * multiple functions receiving 'common' on the same path.
15646      */
15647     BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15648
15649     memset(&mac_vals, 0, sizeof(mac_vals));
15650
15651     if (bxe_prev_is_path_marked(sc)) {
15652         return (bxe_prev_mcp_done(sc));
15653     }
15654
15655     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15656
15657     /* Reset should be performed after BRB is emptied */
15658     if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15659         /* Close the MAC Rx to prevent BRB from filling up */
15660         bxe_prev_unload_close_mac(sc, &mac_vals);
15661
15662         /* close LLH filters towards the BRB */
15663         elink_set_rx_filter(&sc->link_params, 0);
15664
15665         /*
15666          * Check if the UNDI driver was previously loaded.
15667          * UNDI driver initializes CID offset for normal bell to 0x7
15668          */
15669         if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15670             tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15671             if (tmp_reg == 0x7) {
15672                 BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15673                 prev_undi = TRUE;
15674                 /* clear the UNDI indication */
15675                 REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15676                 /* clear possible idle check errors */
15677                 REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15678             }
15679         }
15680
15681         /* wait until BRB is empty */
15682         tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15683         while (timer_count) {
15684             prev_brb = tmp_reg;
15685
15686             tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15687             if (!tmp_reg) {
15688                 break;
15689             }
15690
15691             BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15692
15693             /* reset timer as long as BRB actually gets emptied */
15694             if (prev_brb > tmp_reg) {
15695                 timer_count = 1000;
15696             } else {
15697                 timer_count--;
15698             }
15699
15700             /* If UNDI resides in memory, manually increment it */
15701             if (prev_undi) {
15702                 bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15703             }
15704
15705             DELAY(10);
15706         }
15707
15708         if (!timer_count) {
15709             BLOGE(sc, "Failed to empty BRB\n");
15710         }
15711     }
15712
15713     /* No packets are in the pipeline, path is ready for reset */
15714     bxe_reset_common(sc);
15715
15716     if (mac_vals.xmac_addr) {
15717         REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15718     }
15719     if (mac_vals.umac_addr) {
15720         REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15721     }
15722     if (mac_vals.emac_addr) {
15723         REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15724     }
15725     if (mac_vals.bmac_addr) {
15726         REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15727         REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15728     }
15729
15730     rc = bxe_prev_mark_path(sc, prev_undi);
15731     if (rc) {
15732         bxe_prev_mcp_done(sc);
15733         return (rc);
15734     }
15735
15736     return (bxe_prev_mcp_done(sc));
15737 }
15738
15739 static int
15740 bxe_prev_unload_uncommon(struct bxe_softc *sc)
15741 {
15742     int rc;
15743
15744     BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15745
15746     /* Test if previous unload process was already finished for this path */
15747     if (bxe_prev_is_path_marked(sc)) {
15748         return (bxe_prev_mcp_done(sc));
15749     }
15750
15751     BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
15752
15753     /*
15754      * If function has FLR capabilities, and existing FW version matches
15755      * the one required, then FLR will be sufficient to clean any residue
15756      * left by previous driver
15757      */
15758     rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
15759     if (!rc) {
15760         /* fw version is good */
15761         BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
15762         rc = bxe_do_flr(sc);
15763     }
15764
15765     if (!rc) {
15766         /* FLR was performed */
15767         BLOGD(sc, DBG_LOAD, "FLR successful\n");
15768         return (0);
15769     }
15770
15771     BLOGD(sc, DBG_LOAD, "Could not FLR\n");
15772
15773     /* Close the MCP request, return failure*/
15774     rc = bxe_prev_mcp_done(sc);
15775     if (!rc) {
15776         rc = BXE_PREV_WAIT_NEEDED;
15777     }
15778
15779     return (rc);
15780 }
15781
15782 static int
15783 bxe_prev_unload(struct bxe_softc *sc)
15784 {
15785     int time_counter = 10;
15786     uint32_t fw, hw_lock_reg, hw_lock_val;
15787     uint32_t rc = 0;
15788
15789     /*
15790      * Clear HW from errors which may have resulted from an interrupted
15791      * DMAE transaction.
15792      */
15793     bxe_prev_interrupted_dmae(sc);
15794
15795     /* Release previously held locks */
15796     hw_lock_reg =
15797         (SC_FUNC(sc) <= 5) ?
15798             (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
15799             (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
15800
15801     hw_lock_val = (REG_RD(sc, hw_lock_reg));
15802     if (hw_lock_val) {
15803         if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
15804             BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
15805             REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
15806                    (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
15807         }
15808         BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
15809         REG_WR(sc, hw_lock_reg, 0xffffffff);
15810     } else {
15811         BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
15812     }
15813
15814     if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
15815         BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
15816         REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
15817     }
15818
15819     do {
15820         /* Lock MCP using an unload request */
15821         fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
15822         if (!fw) {
15823             BLOGE(sc, "MCP response failure, aborting\n");
15824             rc = -1;
15825             break;
15826         }
15827
15828         if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
15829             rc = bxe_prev_unload_common(sc);
15830             break;
15831         }
15832
15833         /* non-common reply from MCP night require looping */
15834         rc = bxe_prev_unload_uncommon(sc);
15835         if (rc != BXE_PREV_WAIT_NEEDED) {
15836             break;
15837         }
15838
15839         DELAY(20000);
15840     } while (--time_counter);
15841
15842     if (!time_counter || rc) {
15843         BLOGE(sc, "Failed to unload previous driver!"
15844             " time_counter %d rc %d\n", time_counter, rc);
15845         rc = -1;
15846     }
15847
15848     return (rc);
15849 }
15850
15851 void
15852 bxe_dcbx_set_state(struct bxe_softc *sc,
15853                    uint8_t          dcb_on,
15854                    uint32_t         dcbx_enabled)
15855 {
15856     if (!CHIP_IS_E1x(sc)) {
15857         sc->dcb_state = dcb_on;
15858         sc->dcbx_enabled = dcbx_enabled;
15859     } else {
15860         sc->dcb_state = FALSE;
15861         sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
15862     }
15863     BLOGD(sc, DBG_LOAD,
15864           "DCB state [%s:%s]\n",
15865           dcb_on ? "ON" : "OFF",
15866           (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
15867           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
15868           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
15869           "on-chip with negotiation" : "invalid");
15870 }
15871
15872 /* must be called after sriov-enable */
15873 static int
15874 bxe_set_qm_cid_count(struct bxe_softc *sc)
15875 {
15876     int cid_count = BXE_L2_MAX_CID(sc);
15877
15878     if (IS_SRIOV(sc)) {
15879         cid_count += BXE_VF_CIDS;
15880     }
15881
15882     if (CNIC_SUPPORT(sc)) {
15883         cid_count += CNIC_CID_MAX;
15884     }
15885
15886     return (roundup(cid_count, QM_CID_ROUND));
15887 }
15888
15889 static void
15890 bxe_init_multi_cos(struct bxe_softc *sc)
15891 {
15892     int pri, cos;
15893
15894     uint32_t pri_map = 0; /* XXX change to user config */
15895
15896     for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
15897         cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
15898         if (cos < sc->max_cos) {
15899             sc->prio_to_cos[pri] = cos;
15900         } else {
15901             BLOGW(sc, "Invalid COS %d for priority %d "
15902                       "(max COS is %d), setting to 0\n",
15903                   cos, pri, (sc->max_cos - 1));
15904             sc->prio_to_cos[pri] = 0;
15905         }
15906     }
15907 }
15908
15909 static int
15910 bxe_sysctl_state(SYSCTL_HANDLER_ARGS)
15911 {
15912     struct bxe_softc *sc;
15913     int error, result;
15914
15915     result = 0;
15916     error = sysctl_handle_int(oidp, &result, 0, req);
15917
15918     if (error || !req->newptr) {
15919         return (error);
15920     }
15921
15922     if (result == 1) {
15923         uint32_t  temp;
15924         sc = (struct bxe_softc *)arg1;
15925
15926         BLOGI(sc, "... dumping driver state ...\n");
15927         temp = SHMEM2_RD(sc, temperature_in_half_celsius);
15928         BLOGI(sc, "\t Device Temperature = %d Celsius\n", (temp/2));
15929     }
15930
15931     return (error);
15932 }
15933
15934 static int
15935 bxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
15936 {
15937     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15938     uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
15939     uint32_t *offset;
15940     uint64_t value = 0;
15941     int index = (int)arg2;
15942
15943     if (index >= BXE_NUM_ETH_STATS) {
15944         BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
15945         return (-1);
15946     }
15947
15948     offset = (eth_stats + bxe_eth_stats_arr[index].offset);
15949
15950     switch (bxe_eth_stats_arr[index].size) {
15951     case 4:
15952         value = (uint64_t)*offset;
15953         break;
15954     case 8:
15955         value = HILO_U64(*offset, *(offset + 1));
15956         break;
15957     default:
15958         BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
15959               index, bxe_eth_stats_arr[index].size);
15960         return (-1);
15961     }
15962
15963     return (sysctl_handle_64(oidp, &value, 0, req));
15964 }
15965
15966 static int
15967 bxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
15968 {
15969     struct bxe_softc *sc = (struct bxe_softc *)arg1;
15970     uint32_t *eth_stats;
15971     uint32_t *offset;
15972     uint64_t value = 0;
15973     uint32_t q_stat = (uint32_t)arg2;
15974     uint32_t fp_index = ((q_stat >> 16) & 0xffff);
15975     uint32_t index = (q_stat & 0xffff);
15976
15977     eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
15978
15979     if (index >= BXE_NUM_ETH_Q_STATS) {
15980         BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
15981         return (-1);
15982     }
15983
15984     offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
15985
15986     switch (bxe_eth_q_stats_arr[index].size) {
15987     case 4:
15988         value = (uint64_t)*offset;
15989         break;
15990     case 8:
15991         value = HILO_U64(*offset, *(offset + 1));
15992         break;
15993     default:
15994         BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
15995               index, bxe_eth_q_stats_arr[index].size);
15996         return (-1);
15997     }
15998
15999     return (sysctl_handle_64(oidp, &value, 0, req));
16000 }
16001
16002 static void bxe_force_link_reset(struct bxe_softc *sc)
16003 {
16004
16005         bxe_acquire_phy_lock(sc);
16006         elink_link_reset(&sc->link_params, &sc->link_vars, 1);
16007         bxe_release_phy_lock(sc);
16008 }
16009
16010 static int
16011 bxe_sysctl_pauseparam(SYSCTL_HANDLER_ARGS)
16012 {
16013         struct bxe_softc *sc = (struct bxe_softc *)arg1;;
16014         uint32_t cfg_idx = bxe_get_link_cfg_idx(sc);
16015         int rc = 0;
16016         int error;
16017         int result;
16018
16019
16020         error = sysctl_handle_int(oidp, &sc->bxe_pause_param, 0, req);
16021
16022         if (error || !req->newptr) {
16023                 return (error);
16024         }
16025         if ((sc->bxe_pause_param < 0) ||  (sc->bxe_pause_param > 8)) {
16026                 BLOGW(sc, "invalid pause param (%d) - use intergers between 1 & 8\n",sc->bxe_pause_param);
16027                 sc->bxe_pause_param = 8;
16028         }
16029
16030         result = (sc->bxe_pause_param << PORT_FEATURE_FLOW_CONTROL_SHIFT);
16031
16032
16033         if((result & 0x400) && !(sc->port.supported[cfg_idx] & ELINK_SUPPORTED_Autoneg))  {
16034                         BLOGW(sc, "Does not support Autoneg pause_param %d\n", sc->bxe_pause_param);
16035                         return -EINVAL;
16036         }
16037
16038         if(IS_MF(sc))
16039                 return 0;
16040        sc->link_params.req_flow_ctrl[cfg_idx] = ELINK_FLOW_CTRL_AUTO;
16041         if(result & ELINK_FLOW_CTRL_RX)
16042                 sc->link_params.req_flow_ctrl[cfg_idx] |= ELINK_FLOW_CTRL_RX;
16043
16044         if(result & ELINK_FLOW_CTRL_TX)
16045                 sc->link_params.req_flow_ctrl[cfg_idx] |= ELINK_FLOW_CTRL_TX;
16046         if(sc->link_params.req_flow_ctrl[cfg_idx] == ELINK_FLOW_CTRL_AUTO)
16047                 sc->link_params.req_flow_ctrl[cfg_idx] = ELINK_FLOW_CTRL_NONE;
16048
16049         if(result & 0x400) {
16050                 if (sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG) {
16051                         sc->link_params.req_flow_ctrl[cfg_idx] =
16052                                 ELINK_FLOW_CTRL_AUTO;
16053                 }
16054                 sc->link_params.req_fc_auto_adv = 0;
16055                 if (result & ELINK_FLOW_CTRL_RX)
16056                         sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_RX;
16057
16058                 if (result & ELINK_FLOW_CTRL_TX)
16059                         sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_TX;
16060                 if (!sc->link_params.req_fc_auto_adv)
16061                         sc->link_params.req_fc_auto_adv |= ELINK_FLOW_CTRL_NONE;
16062         }
16063          if (IS_PF(sc)) {
16064                         if (sc->link_vars.link_up) {
16065                                 bxe_stats_handle(sc, STATS_EVENT_STOP);
16066                         }
16067                         if (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) {
16068                         bxe_force_link_reset(sc);
16069                         bxe_acquire_phy_lock(sc);
16070
16071                         rc = elink_phy_init(&sc->link_params, &sc->link_vars);
16072
16073                         bxe_release_phy_lock(sc);
16074
16075                         bxe_calc_fc_adv(sc);
16076                         }
16077         }
16078         return rc;
16079 }
16080
16081
16082 static void
16083 bxe_add_sysctls(struct bxe_softc *sc)
16084 {
16085     struct sysctl_ctx_list *ctx;
16086     struct sysctl_oid_list *children;
16087     struct sysctl_oid *queue_top, *queue;
16088     struct sysctl_oid_list *queue_top_children, *queue_children;
16089     char queue_num_buf[32];
16090     uint32_t q_stat;
16091     int i, j;
16092
16093     ctx = device_get_sysctl_ctx(sc->dev);
16094     children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
16095
16096     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
16097                       CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
16098                       "version");
16099
16100     snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
16101              BCM_5710_FW_MAJOR_VERSION,
16102              BCM_5710_FW_MINOR_VERSION,
16103              BCM_5710_FW_REVISION_VERSION,
16104              BCM_5710_FW_ENGINEERING_VERSION);
16105
16106     snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
16107         ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
16108          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
16109          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
16110          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
16111                                                                 "Unknown"));
16112     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
16113                     CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
16114                     "multifunction vnics per port");
16115
16116     snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
16117         ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
16118          (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
16119          (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
16120                                               "???GT/s"),
16121         sc->devinfo.pcie_link_width);
16122
16123     sc->debug = bxe_debug;
16124
16125 #if __FreeBSD_version >= 900000
16126     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
16127                       CTLFLAG_RD, sc->devinfo.bc_ver_str, 0,
16128                       "bootcode version");
16129     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
16130                       CTLFLAG_RD, sc->fw_ver_str, 0,
16131                       "firmware version");
16132     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
16133                       CTLFLAG_RD, sc->mf_mode_str, 0,
16134                       "multifunction mode");
16135     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
16136                       CTLFLAG_RD, sc->mac_addr_str, 0,
16137                       "mac address");
16138     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
16139                       CTLFLAG_RD, sc->pci_link_str, 0,
16140                       "pci link status");
16141     SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "debug",
16142                     CTLFLAG_RW, &sc->debug,
16143                     "debug logging mode");
16144 #else
16145     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
16146                       CTLFLAG_RD, &sc->devinfo.bc_ver_str, 0,
16147                       "bootcode version");
16148     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
16149                       CTLFLAG_RD, &sc->fw_ver_str, 0,
16150                       "firmware version");
16151     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
16152                       CTLFLAG_RD, &sc->mf_mode_str, 0,
16153                       "multifunction mode");
16154     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
16155                       CTLFLAG_RD, &sc->mac_addr_str, 0,
16156                       "mac address");
16157     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
16158                       CTLFLAG_RD, &sc->pci_link_str, 0,
16159                       "pci link status");
16160     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "debug",
16161                     CTLFLAG_RW, &sc->debug, 0,
16162                     "debug logging mode");
16163 #endif /* #if __FreeBSD_version >= 900000 */
16164
16165     sc->trigger_grcdump = 0;
16166     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "trigger_grcdump",
16167                    CTLFLAG_RW, &sc->trigger_grcdump, 0,
16168                    "trigger grcdump should be invoked"
16169                    "  before collecting grcdump");
16170
16171     sc->grcdump_started = 0;
16172     sc->grcdump_done = 0;
16173     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "grcdump_done",
16174                    CTLFLAG_RD, &sc->grcdump_done, 0,
16175                    "set by driver when grcdump is done");
16176
16177     sc->rx_budget = bxe_rx_budget;
16178     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
16179                     CTLFLAG_RW, &sc->rx_budget, 0,
16180                     "rx processing budget");
16181
16182    SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_param",
16183                     CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
16184                     bxe_sysctl_pauseparam, "IU",
16185                     "need pause frames- DEF:0/TX:1/RX:2/BOTH:3/AUTO:4/AUTOTX:5/AUTORX:6/AUTORXTX:7/NONE:8");
16186
16187
16188     SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
16189                     CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
16190                     bxe_sysctl_state, "IU", "dump driver state");
16191
16192     for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
16193         SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
16194                         bxe_eth_stats_arr[i].string,
16195                         CTLTYPE_U64 | CTLFLAG_RD, sc, i,
16196                         bxe_sysctl_eth_stat, "LU",
16197                         bxe_eth_stats_arr[i].string);
16198     }
16199
16200     /* add a new parent node for all queues "dev.bxe.#.queue" */
16201     queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
16202                                 CTLFLAG_RD, NULL, "queue");
16203     queue_top_children = SYSCTL_CHILDREN(queue_top);
16204
16205     for (i = 0; i < sc->num_queues; i++) {
16206         /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
16207         snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
16208         queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
16209                                 queue_num_buf, CTLFLAG_RD, NULL,
16210                                 "single queue");
16211         queue_children = SYSCTL_CHILDREN(queue);
16212
16213         for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
16214             q_stat = ((i << 16) | j);
16215             SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
16216                             bxe_eth_q_stats_arr[j].string,
16217                             CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
16218                             bxe_sysctl_eth_q_stat, "LU",
16219                             bxe_eth_q_stats_arr[j].string);
16220         }
16221     }
16222 }
16223
16224 static int
16225 bxe_alloc_buf_rings(struct bxe_softc *sc)
16226 {
16227 #if __FreeBSD_version >= 901504
16228
16229     int i;
16230     struct bxe_fastpath *fp;
16231
16232     for (i = 0; i < sc->num_queues; i++) {
16233
16234         fp = &sc->fp[i];
16235
16236         fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
16237                                    M_NOWAIT, &fp->tx_mtx);
16238         if (fp->tx_br == NULL)
16239             return (-1);
16240     }
16241 #endif
16242     return (0);
16243 }
16244
16245 static void
16246 bxe_free_buf_rings(struct bxe_softc *sc)
16247 {
16248 #if __FreeBSD_version >= 901504
16249
16250     int i;
16251     struct bxe_fastpath *fp;
16252
16253     for (i = 0; i < sc->num_queues; i++) {
16254
16255         fp = &sc->fp[i];
16256
16257         if (fp->tx_br) {
16258             buf_ring_free(fp->tx_br, M_DEVBUF);
16259             fp->tx_br = NULL;
16260         }
16261     }
16262
16263 #endif
16264 }
16265
16266 static void
16267 bxe_init_fp_mutexs(struct bxe_softc *sc)
16268 {
16269     int i;
16270     struct bxe_fastpath *fp;
16271
16272     for (i = 0; i < sc->num_queues; i++) {
16273
16274         fp = &sc->fp[i];
16275
16276         snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
16277             "bxe%d_fp%d_tx_lock", sc->unit, i);
16278         mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
16279
16280         snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
16281             "bxe%d_fp%d_rx_lock", sc->unit, i);
16282         mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
16283     }
16284 }
16285
16286 static void
16287 bxe_destroy_fp_mutexs(struct bxe_softc *sc)
16288 {
16289     int i;
16290     struct bxe_fastpath *fp;
16291
16292     for (i = 0; i < sc->num_queues; i++) {
16293
16294         fp = &sc->fp[i];
16295
16296         if (mtx_initialized(&fp->tx_mtx)) {
16297             mtx_destroy(&fp->tx_mtx);
16298         }
16299
16300         if (mtx_initialized(&fp->rx_mtx)) {
16301             mtx_destroy(&fp->rx_mtx);
16302         }
16303     }
16304 }
16305
16306
16307 /*
16308  * Device attach function.
16309  *
16310  * Allocates device resources, performs secondary chip identification, and
16311  * initializes driver instance variables. This function is called from driver
16312  * load after a successful probe.
16313  *
16314  * Returns:
16315  *   0 = Success, >0 = Failure
16316  */
16317 static int
16318 bxe_attach(device_t dev)
16319 {
16320     struct bxe_softc *sc;
16321
16322     sc = device_get_softc(dev);
16323
16324     BLOGD(sc, DBG_LOAD, "Starting attach...\n");
16325
16326     sc->state = BXE_STATE_CLOSED;
16327
16328     sc->dev  = dev;
16329     sc->unit = device_get_unit(dev);
16330
16331     BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
16332
16333     sc->pcie_bus    = pci_get_bus(dev);
16334     sc->pcie_device = pci_get_slot(dev);
16335     sc->pcie_func   = pci_get_function(dev);
16336
16337     /* enable bus master capability */
16338     pci_enable_busmaster(dev);
16339
16340     /* get the BARs */
16341     if (bxe_allocate_bars(sc) != 0) {
16342         return (ENXIO);
16343     }
16344
16345     /* initialize the mutexes */
16346     bxe_init_mutexes(sc);
16347
16348     /* prepare the periodic callout */
16349     callout_init(&sc->periodic_callout, 0);
16350
16351     /* prepare the chip taskqueue */
16352     sc->chip_tq_flags = CHIP_TQ_NONE;
16353     snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
16354              "bxe%d_chip_tq", sc->unit);
16355     TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
16356     sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
16357                                    taskqueue_thread_enqueue,
16358                                    &sc->chip_tq);
16359     taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
16360                             "%s", sc->chip_tq_name);
16361
16362     TIMEOUT_TASK_INIT(taskqueue_thread,
16363         &sc->sp_err_timeout_task, 0, bxe_sp_err_timeout_task,  sc);
16364
16365
16366     /* get device info and set params */
16367     if (bxe_get_device_info(sc) != 0) {
16368         BLOGE(sc, "getting device info\n");
16369         bxe_deallocate_bars(sc);
16370         pci_disable_busmaster(dev);
16371         return (ENXIO);
16372     }
16373
16374     /* get final misc params */
16375     bxe_get_params(sc);
16376
16377     /* set the default MTU (changed via ifconfig) */
16378     sc->mtu = ETHERMTU;
16379
16380     bxe_set_modes_bitmap(sc);
16381
16382     /* XXX
16383      * If in AFEX mode and the function is configured for FCoE
16384      * then bail... no L2 allowed.
16385      */
16386
16387     /* get phy settings from shmem and 'and' against admin settings */
16388     bxe_get_phy_info(sc);
16389
16390     /* initialize the FreeBSD ifnet interface */
16391     if (bxe_init_ifnet(sc) != 0) {
16392         bxe_release_mutexes(sc);
16393         bxe_deallocate_bars(sc);
16394         pci_disable_busmaster(dev);
16395         return (ENXIO);
16396     }
16397
16398     if (bxe_add_cdev(sc) != 0) {
16399         if (sc->ifnet != NULL) {
16400             ether_ifdetach(sc->ifnet);
16401         }
16402         ifmedia_removeall(&sc->ifmedia);
16403         bxe_release_mutexes(sc);
16404         bxe_deallocate_bars(sc);
16405         pci_disable_busmaster(dev);
16406         return (ENXIO);
16407     }
16408
16409     /* allocate device interrupts */
16410     if (bxe_interrupt_alloc(sc) != 0) {
16411         bxe_del_cdev(sc);
16412         if (sc->ifnet != NULL) {
16413             ether_ifdetach(sc->ifnet);
16414         }
16415         ifmedia_removeall(&sc->ifmedia);
16416         bxe_release_mutexes(sc);
16417         bxe_deallocate_bars(sc);
16418         pci_disable_busmaster(dev);
16419         return (ENXIO);
16420     }
16421
16422     bxe_init_fp_mutexs(sc);
16423
16424     if (bxe_alloc_buf_rings(sc) != 0) {
16425         bxe_free_buf_rings(sc);
16426         bxe_interrupt_free(sc);
16427         bxe_del_cdev(sc);
16428         if (sc->ifnet != NULL) {
16429             ether_ifdetach(sc->ifnet);
16430         }
16431         ifmedia_removeall(&sc->ifmedia);
16432         bxe_release_mutexes(sc);
16433         bxe_deallocate_bars(sc);
16434         pci_disable_busmaster(dev);
16435         return (ENXIO);
16436     }
16437
16438     /* allocate ilt */
16439     if (bxe_alloc_ilt_mem(sc) != 0) {
16440         bxe_free_buf_rings(sc);
16441         bxe_interrupt_free(sc);
16442         bxe_del_cdev(sc);
16443         if (sc->ifnet != NULL) {
16444             ether_ifdetach(sc->ifnet);
16445         }
16446         ifmedia_removeall(&sc->ifmedia);
16447         bxe_release_mutexes(sc);
16448         bxe_deallocate_bars(sc);
16449         pci_disable_busmaster(dev);
16450         return (ENXIO);
16451     }
16452
16453     /* allocate the host hardware/software hsi structures */
16454     if (bxe_alloc_hsi_mem(sc) != 0) {
16455         bxe_free_ilt_mem(sc);
16456         bxe_free_buf_rings(sc);
16457         bxe_interrupt_free(sc);
16458         bxe_del_cdev(sc);
16459         if (sc->ifnet != NULL) {
16460             ether_ifdetach(sc->ifnet);
16461         }
16462         ifmedia_removeall(&sc->ifmedia);
16463         bxe_release_mutexes(sc);
16464         bxe_deallocate_bars(sc);
16465         pci_disable_busmaster(dev);
16466         return (ENXIO);
16467     }
16468
16469     /* need to reset chip if UNDI was active */
16470     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
16471         /* init fw_seq */
16472         sc->fw_seq =
16473             (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
16474              DRV_MSG_SEQ_NUMBER_MASK);
16475         BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
16476         bxe_prev_unload(sc);
16477     }
16478
16479 #if 1
16480     /* XXX */
16481     bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16482 #else
16483     if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
16484         SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
16485         SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
16486         SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
16487         bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
16488         bxe_dcbx_init_params(sc);
16489     } else {
16490         bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16491     }
16492 #endif
16493
16494     /* calculate qm_cid_count */
16495     sc->qm_cid_count = bxe_set_qm_cid_count(sc);
16496     BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
16497
16498     sc->max_cos = 1;
16499     bxe_init_multi_cos(sc);
16500
16501     bxe_add_sysctls(sc);
16502
16503     return (0);
16504 }
16505
16506 /*
16507  * Device detach function.
16508  *
16509  * Stops the controller, resets the controller, and releases resources.
16510  *
16511  * Returns:
16512  *   0 = Success, >0 = Failure
16513  */
16514 static int
16515 bxe_detach(device_t dev)
16516 {
16517     struct bxe_softc *sc;
16518     struct ifnet *ifp;
16519
16520     sc = device_get_softc(dev);
16521
16522     BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16523
16524     ifp = sc->ifnet;
16525     if (ifp != NULL && ifp->if_vlantrunk != NULL) {
16526         BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16527         return(EBUSY);
16528     }
16529
16530     bxe_del_cdev(sc);
16531
16532     /* stop the periodic callout */
16533     bxe_periodic_stop(sc);
16534
16535     /* stop the chip taskqueue */
16536     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16537     if (sc->chip_tq) {
16538         taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16539         taskqueue_free(sc->chip_tq);
16540         sc->chip_tq = NULL;
16541         taskqueue_drain_timeout(taskqueue_thread,
16542             &sc->sp_err_timeout_task);
16543     }
16544
16545     /* stop and reset the controller if it was open */
16546     if (sc->state != BXE_STATE_CLOSED) {
16547         BXE_CORE_LOCK(sc);
16548         bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16549         sc->state = BXE_STATE_DISABLED;
16550         BXE_CORE_UNLOCK(sc);
16551     }
16552
16553     /* release the network interface */
16554     if (ifp != NULL) {
16555         ether_ifdetach(ifp);
16556     }
16557     ifmedia_removeall(&sc->ifmedia);
16558
16559     /* XXX do the following based on driver state... */
16560
16561     /* free the host hardware/software hsi structures */
16562     bxe_free_hsi_mem(sc);
16563
16564     /* free ilt */
16565     bxe_free_ilt_mem(sc);
16566
16567     bxe_free_buf_rings(sc);
16568
16569     /* release the interrupts */
16570     bxe_interrupt_free(sc);
16571
16572     /* Release the mutexes*/
16573     bxe_destroy_fp_mutexs(sc);
16574     bxe_release_mutexes(sc);
16575
16576
16577     /* Release the PCIe BAR mapped memory */
16578     bxe_deallocate_bars(sc);
16579
16580     /* Release the FreeBSD interface. */
16581     if (sc->ifnet != NULL) {
16582         if_free(sc->ifnet);
16583     }
16584
16585     pci_disable_busmaster(dev);
16586
16587     return (0);
16588 }
16589
16590 /*
16591  * Device shutdown function.
16592  *
16593  * Stops and resets the controller.
16594  *
16595  * Returns:
16596  *   Nothing
16597  */
16598 static int
16599 bxe_shutdown(device_t dev)
16600 {
16601     struct bxe_softc *sc;
16602
16603     sc = device_get_softc(dev);
16604
16605     BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16606
16607     /* stop the periodic callout */
16608     bxe_periodic_stop(sc);
16609
16610     if (sc->state != BXE_STATE_CLOSED) {
16611         BXE_CORE_LOCK(sc);
16612         bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16613         BXE_CORE_UNLOCK(sc);
16614     }
16615
16616     return (0);
16617 }
16618
16619 void
16620 bxe_igu_ack_sb(struct bxe_softc *sc,
16621                uint8_t          igu_sb_id,
16622                uint8_t          segment,
16623                uint16_t         index,
16624                uint8_t          op,
16625                uint8_t          update)
16626 {
16627     uint32_t igu_addr = sc->igu_base_addr;
16628     igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16629     bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16630 }
16631
16632 static void
16633 bxe_igu_clear_sb_gen(struct bxe_softc *sc,
16634                      uint8_t          func,
16635                      uint8_t          idu_sb_id,
16636                      uint8_t          is_pf)
16637 {
16638     uint32_t data, ctl, cnt = 100;
16639     uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16640     uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16641     uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16642     uint32_t sb_bit =  1 << (idu_sb_id%32);
16643     uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16644     uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16645
16646     /* Not supported in BC mode */
16647     if (CHIP_INT_MODE_IS_BC(sc)) {
16648         return;
16649     }
16650
16651     data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16652              IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16653             IGU_REGULAR_CLEANUP_SET |
16654             IGU_REGULAR_BCLEANUP);
16655
16656     ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16657            (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16658            (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16659
16660     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16661             data, igu_addr_data);
16662     REG_WR(sc, igu_addr_data, data);
16663
16664     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16665                       BUS_SPACE_BARRIER_WRITE);
16666     mb();
16667
16668     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16669             ctl, igu_addr_ctl);
16670     REG_WR(sc, igu_addr_ctl, ctl);
16671
16672     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16673                       BUS_SPACE_BARRIER_WRITE);
16674     mb();
16675
16676     /* wait for clean up to finish */
16677     while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16678         DELAY(20000);
16679     }
16680
16681     if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16682         BLOGD(sc, DBG_LOAD,
16683               "Unable to finish IGU cleanup: "
16684               "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16685               idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16686     }
16687 }
16688
16689 static void
16690 bxe_igu_clear_sb(struct bxe_softc *sc,
16691                  uint8_t          idu_sb_id)
16692 {
16693     bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16694 }
16695
16696
16697
16698
16699
16700
16701
16702 /*******************/
16703 /* ECORE CALLBACKS */
16704 /*******************/
16705
16706 static void
16707 bxe_reset_common(struct bxe_softc *sc)
16708 {
16709     uint32_t val = 0x1400;
16710
16711     /* reset_common */
16712     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16713
16714     if (CHIP_IS_E3(sc)) {
16715         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16716         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16717     }
16718
16719     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16720 }
16721
16722 static void
16723 bxe_common_init_phy(struct bxe_softc *sc)
16724 {
16725     uint32_t shmem_base[2];
16726     uint32_t shmem2_base[2];
16727
16728     /* Avoid common init in case MFW supports LFA */
16729     if (SHMEM2_RD(sc, size) >
16730         (uint32_t)offsetof(struct shmem2_region,
16731                            lfa_host_addr[SC_PORT(sc)])) {
16732         return;
16733     }
16734
16735     shmem_base[0]  = sc->devinfo.shmem_base;
16736     shmem2_base[0] = sc->devinfo.shmem2_base;
16737
16738     if (!CHIP_IS_E1x(sc)) {
16739         shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16740         shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16741     }
16742
16743     bxe_acquire_phy_lock(sc);
16744     elink_common_init_phy(sc, shmem_base, shmem2_base,
16745                           sc->devinfo.chip_id, 0);
16746     bxe_release_phy_lock(sc);
16747 }
16748
16749 static void
16750 bxe_pf_disable(struct bxe_softc *sc)
16751 {
16752     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16753
16754     val &= ~IGU_PF_CONF_FUNC_EN;
16755
16756     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16757     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16758     REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16759 }
16760
16761 static void
16762 bxe_init_pxp(struct bxe_softc *sc)
16763 {
16764     uint16_t devctl;
16765     int r_order, w_order;
16766
16767     devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16768
16769     BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16770
16771     w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16772
16773     if (sc->mrrs == -1) {
16774         r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16775     } else {
16776         BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16777         r_order = sc->mrrs;
16778     }
16779
16780     ecore_init_pxp_arb(sc, r_order, w_order);
16781 }
16782
16783 static uint32_t
16784 bxe_get_pretend_reg(struct bxe_softc *sc)
16785 {
16786     uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16787     uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16788     return (base + (SC_ABS_FUNC(sc)) * stride);
16789 }
16790
16791 /*
16792  * Called only on E1H or E2.
16793  * When pretending to be PF, the pretend value is the function number 0..7.
16794  * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16795  * combination.
16796  */
16797 static int
16798 bxe_pretend_func(struct bxe_softc *sc,
16799                  uint16_t         pretend_func_val)
16800 {
16801     uint32_t pretend_reg;
16802
16803     if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16804         return (-1);
16805     }
16806
16807     /* get my own pretend register */
16808     pretend_reg = bxe_get_pretend_reg(sc);
16809     REG_WR(sc, pretend_reg, pretend_func_val);
16810     REG_RD(sc, pretend_reg);
16811     return (0);
16812 }
16813
16814 static void
16815 bxe_iov_init_dmae(struct bxe_softc *sc)
16816 {
16817     return;
16818 }
16819
16820 static void
16821 bxe_iov_init_dq(struct bxe_softc *sc)
16822 {
16823     return;
16824 }
16825
16826 /* send a NIG loopback debug packet */
16827 static void
16828 bxe_lb_pckt(struct bxe_softc *sc)
16829 {
16830     uint32_t wb_write[3];
16831
16832     /* Ethernet source and destination addresses */
16833     wb_write[0] = 0x55555555;
16834     wb_write[1] = 0x55555555;
16835     wb_write[2] = 0x20;     /* SOP */
16836     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16837
16838     /* NON-IP protocol */
16839     wb_write[0] = 0x09000000;
16840     wb_write[1] = 0x55555555;
16841     wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16842     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16843 }
16844
16845 /*
16846  * Some of the internal memories are not directly readable from the driver.
16847  * To test them we send debug packets.
16848  */
16849 static int
16850 bxe_int_mem_test(struct bxe_softc *sc)
16851 {
16852     int factor;
16853     int count, i;
16854     uint32_t val = 0;
16855
16856     if (CHIP_REV_IS_FPGA(sc)) {
16857         factor = 120;
16858     } else if (CHIP_REV_IS_EMUL(sc)) {
16859         factor = 200;
16860     } else {
16861         factor = 1;
16862     }
16863
16864     /* disable inputs of parser neighbor blocks */
16865     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16866     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16867     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16868     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16869
16870     /*  write 0 to parser credits for CFC search request */
16871     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16872
16873     /* send Ethernet packet */
16874     bxe_lb_pckt(sc);
16875
16876     /* TODO do i reset NIG statistic? */
16877     /* Wait until NIG register shows 1 packet of size 0x10 */
16878     count = 1000 * factor;
16879     while (count) {
16880         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16881         val = *BXE_SP(sc, wb_data[0]);
16882         if (val == 0x10) {
16883             break;
16884         }
16885
16886         DELAY(10000);
16887         count--;
16888     }
16889
16890     if (val != 0x10) {
16891         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16892         return (-1);
16893     }
16894
16895     /* wait until PRS register shows 1 packet */
16896     count = (1000 * factor);
16897     while (count) {
16898         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16899         if (val == 1) {
16900             break;
16901         }
16902
16903         DELAY(10000);
16904         count--;
16905     }
16906
16907     if (val != 0x1) {
16908         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16909         return (-2);
16910     }
16911
16912     /* Reset and init BRB, PRS */
16913     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16914     DELAY(50000);
16915     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16916     DELAY(50000);
16917     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16918     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16919
16920     /* Disable inputs of parser neighbor blocks */
16921     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16922     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16923     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16924     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16925
16926     /* Write 0 to parser credits for CFC search request */
16927     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16928
16929     /* send 10 Ethernet packets */
16930     for (i = 0; i < 10; i++) {
16931         bxe_lb_pckt(sc);
16932     }
16933
16934     /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
16935     count = (1000 * factor);
16936     while (count) {
16937         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16938         val = *BXE_SP(sc, wb_data[0]);
16939         if (val == 0xb0) {
16940             break;
16941         }
16942
16943         DELAY(10000);
16944         count--;
16945     }
16946
16947     if (val != 0xb0) {
16948         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16949         return (-3);
16950     }
16951
16952     /* Wait until PRS register shows 2 packets */
16953     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16954     if (val != 2) {
16955         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16956     }
16957
16958     /* Write 1 to parser credits for CFC search request */
16959     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
16960
16961     /* Wait until PRS register shows 3 packets */
16962     DELAY(10000 * factor);
16963
16964     /* Wait until NIG register shows 1 packet of size 0x10 */
16965     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16966     if (val != 3) {
16967         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16968     }
16969
16970     /* clear NIG EOP FIFO */
16971     for (i = 0; i < 11; i++) {
16972         REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
16973     }
16974
16975     val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
16976     if (val != 1) {
16977         BLOGE(sc, "clear of NIG failed val=0x%x\n", val);
16978         return (-4);
16979     }
16980
16981     /* Reset and init BRB, PRS, NIG */
16982     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16983     DELAY(50000);
16984     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16985     DELAY(50000);
16986     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16987     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16988     if (!CNIC_SUPPORT(sc)) {
16989         /* set NIC mode */
16990         REG_WR(sc, PRS_REG_NIC_MODE, 1);
16991     }
16992
16993     /* Enable inputs of parser neighbor blocks */
16994     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
16995     REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
16996     REG_WR(sc, CFC_REG_DEBUG0, 0x0);
16997     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
16998
16999     return (0);
17000 }
17001
17002 static void
17003 bxe_setup_fan_failure_detection(struct bxe_softc *sc)
17004 {
17005     int is_required;
17006     uint32_t val;
17007     int port;
17008
17009     is_required = 0;
17010     val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
17011            SHARED_HW_CFG_FAN_FAILURE_MASK);
17012
17013     if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
17014         is_required = 1;
17015     }
17016     /*
17017      * The fan failure mechanism is usually related to the PHY type since
17018      * the power consumption of the board is affected by the PHY. Currently,
17019      * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
17020      */
17021     else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
17022         for (port = PORT_0; port < PORT_MAX; port++) {
17023             is_required |= elink_fan_failure_det_req(sc,
17024                                                      sc->devinfo.shmem_base,
17025                                                      sc->devinfo.shmem2_base,
17026                                                      port);
17027         }
17028     }
17029
17030     BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
17031
17032     if (is_required == 0) {
17033         return;
17034     }
17035
17036     /* Fan failure is indicated by SPIO 5 */
17037     bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
17038
17039     /* set to active low mode */
17040     val = REG_RD(sc, MISC_REG_SPIO_INT);
17041     val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
17042     REG_WR(sc, MISC_REG_SPIO_INT, val);
17043
17044     /* enable interrupt to signal the IGU */
17045     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17046     val |= MISC_SPIO_SPIO5;
17047     REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
17048 }
17049
17050 static void
17051 bxe_enable_blocks_attention(struct bxe_softc *sc)
17052 {
17053     uint32_t val;
17054
17055     REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17056     if (!CHIP_IS_E1x(sc)) {
17057         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
17058     } else {
17059         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
17060     }
17061     REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17062     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17063     /*
17064      * mask read length error interrupts in brb for parser
17065      * (parsing unit and 'checksum and crc' unit)
17066      * these errors are legal (PU reads fixed length and CAC can cause
17067      * read length error on truncated packets)
17068      */
17069     REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
17070     REG_WR(sc, QM_REG_QM_INT_MASK, 0);
17071     REG_WR(sc, TM_REG_TM_INT_MASK, 0);
17072     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
17073     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
17074     REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
17075 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
17076 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
17077     REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
17078     REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
17079     REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
17080 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
17081 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
17082     REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
17083     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
17084     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
17085     REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
17086 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
17087 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
17088
17089     val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
17090            PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
17091            PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
17092     if (!CHIP_IS_E1x(sc)) {
17093         val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
17094                 PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
17095     }
17096     REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
17097
17098     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
17099     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
17100     REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
17101 /*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
17102
17103     if (!CHIP_IS_E1x(sc)) {
17104         /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
17105         REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
17106     }
17107
17108     REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
17109     REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
17110 /*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
17111     REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
17112 }
17113
17114 /**
17115  * bxe_init_hw_common - initialize the HW at the COMMON phase.
17116  *
17117  * @sc:     driver handle
17118  */
17119 static int
17120 bxe_init_hw_common(struct bxe_softc *sc)
17121 {
17122     uint8_t abs_func_id;
17123     uint32_t val;
17124
17125     BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
17126           SC_ABS_FUNC(sc));
17127
17128     /*
17129      * take the RESET lock to protect undi_unload flow from accessing
17130      * registers while we are resetting the chip
17131      */
17132     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17133
17134     bxe_reset_common(sc);
17135
17136     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
17137
17138     val = 0xfffc;
17139     if (CHIP_IS_E3(sc)) {
17140         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
17141         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
17142     }
17143
17144     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
17145
17146     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17147
17148     ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
17149     BLOGD(sc, DBG_LOAD, "after misc block init\n");
17150
17151     if (!CHIP_IS_E1x(sc)) {
17152         /*
17153          * 4-port mode or 2-port mode we need to turn off master-enable for
17154          * everyone. After that we turn it back on for self. So, we disregard
17155          * multi-function, and always disable all functions on the given path,
17156          * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
17157          */
17158         for (abs_func_id = SC_PATH(sc);
17159              abs_func_id < (E2_FUNC_MAX * 2);
17160              abs_func_id += 2) {
17161             if (abs_func_id == SC_ABS_FUNC(sc)) {
17162                 REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17163                 continue;
17164             }
17165
17166             bxe_pretend_func(sc, abs_func_id);
17167
17168             /* clear pf enable */
17169             bxe_pf_disable(sc);
17170
17171             bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17172         }
17173     }
17174
17175     BLOGD(sc, DBG_LOAD, "after pf disable\n");
17176
17177     ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
17178
17179     if (CHIP_IS_E1(sc)) {
17180         /*
17181          * enable HW interrupt from PXP on USDM overflow
17182          * bit 16 on INT_MASK_0
17183          */
17184         REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17185     }
17186
17187     ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
17188     bxe_init_pxp(sc);
17189
17190 #ifdef __BIG_ENDIAN
17191     REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
17192     REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
17193     REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
17194     REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
17195     REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
17196     /* make sure this value is 0 */
17197     REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
17198
17199     //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
17200     REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
17201     REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
17202     REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
17203     REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
17204 #endif
17205
17206     ecore_ilt_init_page_size(sc, INITOP_SET);
17207
17208     if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
17209         REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
17210     }
17211
17212     /* let the HW do it's magic... */
17213     DELAY(100000);
17214
17215     /* finish PXP init */
17216     val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
17217     if (val != 1) {
17218         BLOGE(sc, "PXP2 CFG failed PXP2_REG_RQ_CFG_DONE val = 0x%x\n",
17219             val);
17220         return (-1);
17221     }
17222     val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
17223     if (val != 1) {
17224         BLOGE(sc, "PXP2 RD_INIT failed val = 0x%x\n", val);
17225         return (-1);
17226     }
17227
17228     BLOGD(sc, DBG_LOAD, "after pxp init\n");
17229
17230     /*
17231      * Timer bug workaround for E2 only. We need to set the entire ILT to have
17232      * entries with value "0" and valid bit on. This needs to be done by the
17233      * first PF that is loaded in a path (i.e. common phase)
17234      */
17235     if (!CHIP_IS_E1x(sc)) {
17236 /*
17237  * In E2 there is a bug in the timers block that can cause function 6 / 7
17238  * (i.e. vnic3) to start even if it is marked as "scan-off".
17239  * This occurs when a different function (func2,3) is being marked
17240  * as "scan-off". Real-life scenario for example: if a driver is being
17241  * load-unloaded while func6,7 are down. This will cause the timer to access
17242  * the ilt, translate to a logical address and send a request to read/write.
17243  * Since the ilt for the function that is down is not valid, this will cause
17244  * a translation error which is unrecoverable.
17245  * The Workaround is intended to make sure that when this happens nothing
17246  * fatal will occur. The workaround:
17247  *  1.  First PF driver which loads on a path will:
17248  *      a.  After taking the chip out of reset, by using pretend,
17249  *          it will write "0" to the following registers of
17250  *          the other vnics.
17251  *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
17252  *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
17253  *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
17254  *          And for itself it will write '1' to
17255  *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
17256  *          dmae-operations (writing to pram for example.)
17257  *          note: can be done for only function 6,7 but cleaner this
17258  *            way.
17259  *      b.  Write zero+valid to the entire ILT.
17260  *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
17261  *          VNIC3 (of that port). The range allocated will be the
17262  *          entire ILT. This is needed to prevent  ILT range error.
17263  *  2.  Any PF driver load flow:
17264  *      a.  ILT update with the physical addresses of the allocated
17265  *          logical pages.
17266  *      b.  Wait 20msec. - note that this timeout is needed to make
17267  *          sure there are no requests in one of the PXP internal
17268  *          queues with "old" ILT addresses.
17269  *      c.  PF enable in the PGLC.
17270  *      d.  Clear the was_error of the PF in the PGLC. (could have
17271  *          occurred while driver was down)
17272  *      e.  PF enable in the CFC (WEAK + STRONG)
17273  *      f.  Timers scan enable
17274  *  3.  PF driver unload flow:
17275  *      a.  Clear the Timers scan_en.
17276  *      b.  Polling for scan_on=0 for that PF.
17277  *      c.  Clear the PF enable bit in the PXP.
17278  *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
17279  *      e.  Write zero+valid to all ILT entries (The valid bit must
17280  *          stay set)
17281  *      f.  If this is VNIC 3 of a port then also init
17282  *          first_timers_ilt_entry to zero and last_timers_ilt_entry
17283  *          to the last enrty in the ILT.
17284  *
17285  *      Notes:
17286  *      Currently the PF error in the PGLC is non recoverable.
17287  *      In the future the there will be a recovery routine for this error.
17288  *      Currently attention is masked.
17289  *      Having an MCP lock on the load/unload process does not guarantee that
17290  *      there is no Timer disable during Func6/7 enable. This is because the
17291  *      Timers scan is currently being cleared by the MCP on FLR.
17292  *      Step 2.d can be done only for PF6/7 and the driver can also check if
17293  *      there is error before clearing it. But the flow above is simpler and
17294  *      more general.
17295  *      All ILT entries are written by zero+valid and not just PF6/7
17296  *      ILT entries since in the future the ILT entries allocation for
17297  *      PF-s might be dynamic.
17298  */
17299         struct ilt_client_info ilt_cli;
17300         struct ecore_ilt ilt;
17301
17302         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
17303         memset(&ilt, 0, sizeof(struct ecore_ilt));
17304
17305         /* initialize dummy TM client */
17306         ilt_cli.start      = 0;
17307         ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
17308         ilt_cli.client_num = ILT_CLIENT_TM;
17309
17310         /*
17311          * Step 1: set zeroes to all ilt page entries with valid bit on
17312          * Step 2: set the timers first/last ilt entry to point
17313          * to the entire range to prevent ILT range error for 3rd/4th
17314          * vnic (this code assumes existence of the vnic)
17315          *
17316          * both steps performed by call to ecore_ilt_client_init_op()
17317          * with dummy TM client
17318          *
17319          * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
17320          * and his brother are split registers
17321          */
17322
17323         bxe_pretend_func(sc, (SC_PATH(sc) + 6));
17324         ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
17325         bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17326
17327         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
17328         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
17329         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
17330     }
17331
17332     REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
17333     REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
17334
17335     if (!CHIP_IS_E1x(sc)) {
17336         int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
17337                      (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
17338
17339         ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
17340         ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
17341
17342         /* let the HW do it's magic... */
17343         do {
17344             DELAY(200000);
17345             val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
17346         } while (factor-- && (val != 1));
17347
17348         if (val != 1) {
17349             BLOGE(sc, "ATC_INIT failed val = 0x%x\n", val);
17350             return (-1);
17351         }
17352     }
17353
17354     BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
17355
17356     ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
17357
17358     bxe_iov_init_dmae(sc);
17359
17360     /* clean the DMAE memory */
17361     sc->dmae_ready = 1;
17362     ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
17363
17364     ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
17365
17366     ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
17367
17368     ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
17369
17370     ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
17371
17372     bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
17373     bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
17374     bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
17375     bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
17376
17377     ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
17378
17379     /* QM queues pointers table */
17380     ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
17381
17382     /* soft reset pulse */
17383     REG_WR(sc, QM_REG_SOFT_RESET, 1);
17384     REG_WR(sc, QM_REG_SOFT_RESET, 0);
17385
17386     if (CNIC_SUPPORT(sc))
17387         ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
17388
17389     ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
17390     REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
17391     if (!CHIP_REV_IS_SLOW(sc)) {
17392         /* enable hw interrupt from doorbell Q */
17393         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17394     }
17395
17396     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17397
17398     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17399     REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
17400
17401     if (!CHIP_IS_E1(sc)) {
17402         REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
17403     }
17404
17405     if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
17406         if (IS_MF_AFEX(sc)) {
17407             /*
17408              * configure that AFEX and VLAN headers must be
17409              * received in AFEX mode
17410              */
17411             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
17412             REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
17413             REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
17414             REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
17415             REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
17416         } else {
17417             /*
17418              * Bit-map indicating which L2 hdrs may appear
17419              * after the basic Ethernet header
17420              */
17421             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
17422                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17423         }
17424     }
17425
17426     ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
17427     ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
17428     ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
17429     ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
17430
17431     if (!CHIP_IS_E1x(sc)) {
17432         /* reset VFC memories */
17433         REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17434                VFC_MEMORIES_RST_REG_CAM_RST |
17435                VFC_MEMORIES_RST_REG_RAM_RST);
17436         REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17437                VFC_MEMORIES_RST_REG_CAM_RST |
17438                VFC_MEMORIES_RST_REG_RAM_RST);
17439
17440         DELAY(20000);
17441     }
17442
17443     ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
17444     ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
17445     ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
17446     ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
17447
17448     /* sync semi rtc */
17449     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
17450            0x80000000);
17451     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
17452            0x80000000);
17453
17454     ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
17455     ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
17456     ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
17457
17458     if (!CHIP_IS_E1x(sc)) {
17459         if (IS_MF_AFEX(sc)) {
17460             /*
17461              * configure that AFEX and VLAN headers must be
17462              * sent in AFEX mode
17463              */
17464             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
17465             REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
17466             REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
17467             REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
17468             REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
17469         } else {
17470             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
17471                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17472         }
17473     }
17474
17475     REG_WR(sc, SRC_REG_SOFT_RST, 1);
17476
17477     ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
17478
17479     if (CNIC_SUPPORT(sc)) {
17480         REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
17481         REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
17482         REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
17483         REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
17484         REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
17485         REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
17486         REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
17487         REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
17488         REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
17489         REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
17490     }
17491     REG_WR(sc, SRC_REG_SOFT_RST, 0);
17492
17493     if (sizeof(union cdu_context) != 1024) {
17494         /* we currently assume that a context is 1024 bytes */
17495         BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
17496               (long)sizeof(union cdu_context));
17497     }
17498
17499     ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
17500     val = (4 << 24) + (0 << 12) + 1024;
17501     REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
17502
17503     ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
17504
17505     REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
17506     /* enable context validation interrupt from CFC */
17507     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17508
17509     /* set the thresholds to prevent CFC/CDU race */
17510     REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
17511     ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
17512
17513     if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
17514         REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
17515     }
17516
17517     ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
17518     ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
17519
17520     /* Reset PCIE errors for debug */
17521     REG_WR(sc, 0x2814, 0xffffffff);
17522     REG_WR(sc, 0x3820, 0xffffffff);
17523
17524     if (!CHIP_IS_E1x(sc)) {
17525         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
17526                (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17527                 PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17528         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17529                (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17530                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17531                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17532         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17533                (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17534                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17535                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17536     }
17537
17538     ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17539
17540     if (!CHIP_IS_E1(sc)) {
17541         /* in E3 this done in per-port section */
17542         if (!CHIP_IS_E3(sc))
17543             REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17544     }
17545
17546     if (CHIP_IS_E1H(sc)) {
17547         /* not applicable for E2 (and above ...) */
17548         REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17549     }
17550
17551     if (CHIP_REV_IS_SLOW(sc)) {
17552         DELAY(200000);
17553     }
17554
17555     /* finish CFC init */
17556     val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17557     if (val != 1) {
17558         BLOGE(sc, "CFC LL_INIT failed val=0x%x\n", val);
17559         return (-1);
17560     }
17561     val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17562     if (val != 1) {
17563         BLOGE(sc, "CFC AC_INIT failed val=0x%x\n", val);
17564         return (-1);
17565     }
17566     val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17567     if (val != 1) {
17568         BLOGE(sc, "CFC CAM_INIT failed val=0x%x\n", val);
17569         return (-1);
17570     }
17571     REG_WR(sc, CFC_REG_DEBUG0, 0);
17572
17573     if (CHIP_IS_E1(sc)) {
17574         /* read NIG statistic to see if this is our first up since powerup */
17575         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17576         val = *BXE_SP(sc, wb_data[0]);
17577
17578         /* do internal memory self test */
17579         if ((val == 0) && bxe_int_mem_test(sc)) {
17580             BLOGE(sc, "internal mem self test failed val=0x%x\n", val);
17581             return (-1);
17582         }
17583     }
17584
17585     bxe_setup_fan_failure_detection(sc);
17586
17587     /* clear PXP2 attentions */
17588     REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17589
17590     bxe_enable_blocks_attention(sc);
17591
17592     if (!CHIP_REV_IS_SLOW(sc)) {
17593         ecore_enable_blocks_parity(sc);
17594     }
17595
17596     if (!BXE_NOMCP(sc)) {
17597         if (CHIP_IS_E1x(sc)) {
17598             bxe_common_init_phy(sc);
17599         }
17600     }
17601
17602     return (0);
17603 }
17604
17605 /**
17606  * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17607  *
17608  * @sc:     driver handle
17609  */
17610 static int
17611 bxe_init_hw_common_chip(struct bxe_softc *sc)
17612 {
17613     int rc = bxe_init_hw_common(sc);
17614
17615     if (rc) {
17616         BLOGE(sc, "bxe_init_hw_common failed rc=%d\n", rc);
17617         return (rc);
17618     }
17619
17620     /* In E2 2-PORT mode, same ext phy is used for the two paths */
17621     if (!BXE_NOMCP(sc)) {
17622         bxe_common_init_phy(sc);
17623     }
17624
17625     return (0);
17626 }
17627
17628 static int
17629 bxe_init_hw_port(struct bxe_softc *sc)
17630 {
17631     int port = SC_PORT(sc);
17632     int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17633     uint32_t low, high;
17634     uint32_t val;
17635
17636     BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17637
17638     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17639
17640     ecore_init_block(sc, BLOCK_MISC, init_phase);
17641     ecore_init_block(sc, BLOCK_PXP, init_phase);
17642     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17643
17644     /*
17645      * Timers bug workaround: disables the pf_master bit in pglue at
17646      * common phase, we need to enable it here before any dmae access are
17647      * attempted. Therefore we manually added the enable-master to the
17648      * port phase (it also happens in the function phase)
17649      */
17650     if (!CHIP_IS_E1x(sc)) {
17651         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17652     }
17653
17654     ecore_init_block(sc, BLOCK_ATC, init_phase);
17655     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17656     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17657     ecore_init_block(sc, BLOCK_QM, init_phase);
17658
17659     ecore_init_block(sc, BLOCK_TCM, init_phase);
17660     ecore_init_block(sc, BLOCK_UCM, init_phase);
17661     ecore_init_block(sc, BLOCK_CCM, init_phase);
17662     ecore_init_block(sc, BLOCK_XCM, init_phase);
17663
17664     /* QM cid (connection) count */
17665     ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17666
17667     if (CNIC_SUPPORT(sc)) {
17668         ecore_init_block(sc, BLOCK_TM, init_phase);
17669         REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17670         REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17671     }
17672
17673     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17674
17675     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17676
17677     if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17678         if (IS_MF(sc)) {
17679             low = (BXE_ONE_PORT(sc) ? 160 : 246);
17680         } else if (sc->mtu > 4096) {
17681             if (BXE_ONE_PORT(sc)) {
17682                 low = 160;
17683             } else {
17684                 val = sc->mtu;
17685                 /* (24*1024 + val*4)/256 */
17686                 low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17687             }
17688         } else {
17689             low = (BXE_ONE_PORT(sc) ? 80 : 160);
17690         }
17691         high = (low + 56); /* 14*1024/256 */
17692         REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17693         REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17694     }
17695
17696     if (CHIP_IS_MODE_4_PORT(sc)) {
17697         REG_WR(sc, SC_PORT(sc) ?
17698                BRB1_REG_MAC_GUARANTIED_1 :
17699                BRB1_REG_MAC_GUARANTIED_0, 40);
17700     }
17701
17702     ecore_init_block(sc, BLOCK_PRS, init_phase);
17703     if (CHIP_IS_E3B0(sc)) {
17704         if (IS_MF_AFEX(sc)) {
17705             /* configure headers for AFEX mode */
17706             REG_WR(sc, SC_PORT(sc) ?
17707                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17708                    PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17709             REG_WR(sc, SC_PORT(sc) ?
17710                    PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17711                    PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17712             REG_WR(sc, SC_PORT(sc) ?
17713                    PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17714                    PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17715         } else {
17716             /* Ovlan exists only if we are in multi-function +
17717              * switch-dependent mode, in switch-independent there
17718              * is no ovlan headers
17719              */
17720             REG_WR(sc, SC_PORT(sc) ?
17721                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17722                    PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17723                    (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17724         }
17725     }
17726
17727     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17728     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17729     ecore_init_block(sc, BLOCK_USDM, init_phase);
17730     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17731
17732     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17733     ecore_init_block(sc, BLOCK_USEM, init_phase);
17734     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17735     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17736
17737     ecore_init_block(sc, BLOCK_UPB, init_phase);
17738     ecore_init_block(sc, BLOCK_XPB, init_phase);
17739
17740     ecore_init_block(sc, BLOCK_PBF, init_phase);
17741
17742     if (CHIP_IS_E1x(sc)) {
17743         /* configure PBF to work without PAUSE mtu 9000 */
17744         REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17745
17746         /* update threshold */
17747         REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17748         /* update init credit */
17749         REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17750
17751         /* probe changes */
17752         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17753         DELAY(50);
17754         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17755     }
17756
17757     if (CNIC_SUPPORT(sc)) {
17758         ecore_init_block(sc, BLOCK_SRC, init_phase);
17759     }
17760
17761     ecore_init_block(sc, BLOCK_CDU, init_phase);
17762     ecore_init_block(sc, BLOCK_CFC, init_phase);
17763
17764     if (CHIP_IS_E1(sc)) {
17765         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17766         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17767     }
17768     ecore_init_block(sc, BLOCK_HC, init_phase);
17769
17770     ecore_init_block(sc, BLOCK_IGU, init_phase);
17771
17772     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17773     /* init aeu_mask_attn_func_0/1:
17774      *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17775      *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17776      *             bits 4-7 are used for "per vn group attention" */
17777     val = IS_MF(sc) ? 0xF7 : 0x7;
17778     /* Enable DCBX attention for all but E1 */
17779     val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17780     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17781
17782     ecore_init_block(sc, BLOCK_NIG, init_phase);
17783
17784     if (!CHIP_IS_E1x(sc)) {
17785         /* Bit-map indicating which L2 hdrs may appear after the
17786          * basic Ethernet header
17787          */
17788         if (IS_MF_AFEX(sc)) {
17789             REG_WR(sc, SC_PORT(sc) ?
17790                    NIG_REG_P1_HDRS_AFTER_BASIC :
17791                    NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17792         } else {
17793             REG_WR(sc, SC_PORT(sc) ?
17794                    NIG_REG_P1_HDRS_AFTER_BASIC :
17795                    NIG_REG_P0_HDRS_AFTER_BASIC,
17796                    IS_MF_SD(sc) ? 7 : 6);
17797         }
17798
17799         if (CHIP_IS_E3(sc)) {
17800             REG_WR(sc, SC_PORT(sc) ?
17801                    NIG_REG_LLH1_MF_MODE :
17802                    NIG_REG_LLH_MF_MODE, IS_MF(sc));
17803         }
17804     }
17805     if (!CHIP_IS_E3(sc)) {
17806         REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17807     }
17808
17809     if (!CHIP_IS_E1(sc)) {
17810         /* 0x2 disable mf_ov, 0x1 enable */
17811         REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17812                (IS_MF_SD(sc) ? 0x1 : 0x2));
17813
17814         if (!CHIP_IS_E1x(sc)) {
17815             val = 0;
17816             switch (sc->devinfo.mf_info.mf_mode) {
17817             case MULTI_FUNCTION_SD:
17818                 val = 1;
17819                 break;
17820             case MULTI_FUNCTION_SI:
17821             case MULTI_FUNCTION_AFEX:
17822                 val = 2;
17823                 break;
17824             }
17825
17826             REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17827                         NIG_REG_LLH0_CLS_TYPE), val);
17828         }
17829         REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17830         REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17831         REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17832     }
17833
17834     /* If SPIO5 is set to generate interrupts, enable it for this port */
17835     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17836     if (val & MISC_SPIO_SPIO5) {
17837         uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17838                                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17839         val = REG_RD(sc, reg_addr);
17840         val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17841         REG_WR(sc, reg_addr, val);
17842     }
17843
17844     return (0);
17845 }
17846
17847 static uint32_t
17848 bxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17849                        uint32_t         reg,
17850                        uint32_t         expected,
17851                        uint32_t         poll_count)
17852 {
17853     uint32_t cur_cnt = poll_count;
17854     uint32_t val;
17855
17856     while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17857         DELAY(FLR_WAIT_INTERVAL);
17858     }
17859
17860     return (val);
17861 }
17862
17863 static int
17864 bxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17865                               uint32_t         reg,
17866                               char             *msg,
17867                               uint32_t         poll_cnt)
17868 {
17869     uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17870
17871     if (val != 0) {
17872         BLOGE(sc, "%s usage count=%d\n", msg, val);
17873         return (1);
17874     }
17875
17876     return (0);
17877 }
17878
17879 /* Common routines with VF FLR cleanup */
17880 static uint32_t
17881 bxe_flr_clnup_poll_count(struct bxe_softc *sc)
17882 {
17883     /* adjust polling timeout */
17884     if (CHIP_REV_IS_EMUL(sc)) {
17885         return (FLR_POLL_CNT * 2000);
17886     }
17887
17888     if (CHIP_REV_IS_FPGA(sc)) {
17889         return (FLR_POLL_CNT * 120);
17890     }
17891
17892     return (FLR_POLL_CNT);
17893 }
17894
17895 static int
17896 bxe_poll_hw_usage_counters(struct bxe_softc *sc,
17897                            uint32_t         poll_cnt)
17898 {
17899     /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17900     if (bxe_flr_clnup_poll_hw_counter(sc,
17901                                       CFC_REG_NUM_LCIDS_INSIDE_PF,
17902                                       "CFC PF usage counter timed out",
17903                                       poll_cnt)) {
17904         return (1);
17905     }
17906
17907     /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17908     if (bxe_flr_clnup_poll_hw_counter(sc,
17909                                       DORQ_REG_PF_USAGE_CNT,
17910                                       "DQ PF usage counter timed out",
17911                                       poll_cnt)) {
17912         return (1);
17913     }
17914
17915     /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17916     if (bxe_flr_clnup_poll_hw_counter(sc,
17917                                       QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17918                                       "QM PF usage counter timed out",
17919                                       poll_cnt)) {
17920         return (1);
17921     }
17922
17923     /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17924     if (bxe_flr_clnup_poll_hw_counter(sc,
17925                                       TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
17926                                       "Timers VNIC usage counter timed out",
17927                                       poll_cnt)) {
17928         return (1);
17929     }
17930
17931     if (bxe_flr_clnup_poll_hw_counter(sc,
17932                                       TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
17933                                       "Timers NUM_SCANS usage counter timed out",
17934                                       poll_cnt)) {
17935         return (1);
17936     }
17937
17938     /* Wait DMAE PF usage counter to zero */
17939     if (bxe_flr_clnup_poll_hw_counter(sc,
17940                                       dmae_reg_go_c[INIT_DMAE_C(sc)],
17941                                       "DMAE dommand register timed out",
17942                                       poll_cnt)) {
17943         return (1);
17944     }
17945
17946     return (0);
17947 }
17948
17949 #define OP_GEN_PARAM(param)                                            \
17950     (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
17951 #define OP_GEN_TYPE(type)                                           \
17952     (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
17953 #define OP_GEN_AGG_VECT(index)                                             \
17954     (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
17955
17956 static int
17957 bxe_send_final_clnup(struct bxe_softc *sc,
17958                      uint8_t          clnup_func,
17959                      uint32_t         poll_cnt)
17960 {
17961     uint32_t op_gen_command = 0;
17962     uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
17963                           CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
17964     int ret = 0;
17965
17966     if (REG_RD(sc, comp_addr)) {
17967         BLOGE(sc, "Cleanup complete was not 0 before sending\n");
17968         return (1);
17969     }
17970
17971     op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
17972     op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
17973     op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
17974     op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
17975
17976     BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
17977     REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
17978
17979     if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
17980         BLOGE(sc, "FW final cleanup did not succeed\n");
17981         BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
17982               (REG_RD(sc, comp_addr)));
17983         bxe_panic(sc, ("FLR cleanup failed\n"));
17984         return (1);
17985     }
17986
17987     /* Zero completion for nxt FLR */
17988     REG_WR(sc, comp_addr, 0);
17989
17990     return (ret);
17991 }
17992
17993 static void
17994 bxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
17995                        struct pbf_pN_buf_regs *regs,
17996                        uint32_t               poll_count)
17997 {
17998     uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
17999     uint32_t cur_cnt = poll_count;
18000
18001     crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
18002     crd = crd_start = REG_RD(sc, regs->crd);
18003     init_crd = REG_RD(sc, regs->init_crd);
18004
18005     BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
18006     BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
18007     BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
18008
18009     while ((crd != init_crd) &&
18010            ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
18011             (init_crd - crd_start))) {
18012         if (cur_cnt--) {
18013             DELAY(FLR_WAIT_INTERVAL);
18014             crd = REG_RD(sc, regs->crd);
18015             crd_freed = REG_RD(sc, regs->crd_freed);
18016         } else {
18017             BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
18018             BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
18019             BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
18020             break;
18021         }
18022     }
18023
18024     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
18025           poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18026 }
18027
18028 static void
18029 bxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
18030                        struct pbf_pN_cmd_regs *regs,
18031                        uint32_t               poll_count)
18032 {
18033     uint32_t occup, to_free, freed, freed_start;
18034     uint32_t cur_cnt = poll_count;
18035
18036     occup = to_free = REG_RD(sc, regs->lines_occup);
18037     freed = freed_start = REG_RD(sc, regs->lines_freed);
18038
18039     BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18040     BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18041
18042     while (occup &&
18043            ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
18044         if (cur_cnt--) {
18045             DELAY(FLR_WAIT_INTERVAL);
18046             occup = REG_RD(sc, regs->lines_occup);
18047             freed = REG_RD(sc, regs->lines_freed);
18048         } else {
18049             BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
18050             BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18051             BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18052             break;
18053         }
18054     }
18055
18056     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
18057           poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18058 }
18059
18060 static void
18061 bxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
18062 {
18063     struct pbf_pN_cmd_regs cmd_regs[] = {
18064         {0, (CHIP_IS_E3B0(sc)) ?
18065             PBF_REG_TQ_OCCUPANCY_Q0 :
18066             PBF_REG_P0_TQ_OCCUPANCY,
18067             (CHIP_IS_E3B0(sc)) ?
18068             PBF_REG_TQ_LINES_FREED_CNT_Q0 :
18069             PBF_REG_P0_TQ_LINES_FREED_CNT},
18070         {1, (CHIP_IS_E3B0(sc)) ?
18071             PBF_REG_TQ_OCCUPANCY_Q1 :
18072             PBF_REG_P1_TQ_OCCUPANCY,
18073             (CHIP_IS_E3B0(sc)) ?
18074             PBF_REG_TQ_LINES_FREED_CNT_Q1 :
18075             PBF_REG_P1_TQ_LINES_FREED_CNT},
18076         {4, (CHIP_IS_E3B0(sc)) ?
18077             PBF_REG_TQ_OCCUPANCY_LB_Q :
18078             PBF_REG_P4_TQ_OCCUPANCY,
18079             (CHIP_IS_E3B0(sc)) ?
18080             PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
18081             PBF_REG_P4_TQ_LINES_FREED_CNT}
18082     };
18083
18084     struct pbf_pN_buf_regs buf_regs[] = {
18085         {0, (CHIP_IS_E3B0(sc)) ?
18086             PBF_REG_INIT_CRD_Q0 :
18087             PBF_REG_P0_INIT_CRD ,
18088             (CHIP_IS_E3B0(sc)) ?
18089             PBF_REG_CREDIT_Q0 :
18090             PBF_REG_P0_CREDIT,
18091             (CHIP_IS_E3B0(sc)) ?
18092             PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
18093             PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
18094         {1, (CHIP_IS_E3B0(sc)) ?
18095             PBF_REG_INIT_CRD_Q1 :
18096             PBF_REG_P1_INIT_CRD,
18097             (CHIP_IS_E3B0(sc)) ?
18098             PBF_REG_CREDIT_Q1 :
18099             PBF_REG_P1_CREDIT,
18100             (CHIP_IS_E3B0(sc)) ?
18101             PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
18102             PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
18103         {4, (CHIP_IS_E3B0(sc)) ?
18104             PBF_REG_INIT_CRD_LB_Q :
18105             PBF_REG_P4_INIT_CRD,
18106             (CHIP_IS_E3B0(sc)) ?
18107             PBF_REG_CREDIT_LB_Q :
18108             PBF_REG_P4_CREDIT,
18109             (CHIP_IS_E3B0(sc)) ?
18110             PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
18111             PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
18112     };
18113
18114     int i;
18115
18116     /* Verify the command queues are flushed P0, P1, P4 */
18117     for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
18118         bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
18119     }
18120
18121     /* Verify the transmission buffers are flushed P0, P1, P4 */
18122     for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
18123         bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
18124     }
18125 }
18126
18127 static void
18128 bxe_hw_enable_status(struct bxe_softc *sc)
18129 {
18130     uint32_t val;
18131
18132     val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
18133     BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
18134
18135     val = REG_RD(sc, PBF_REG_DISABLE_PF);
18136     BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
18137
18138     val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
18139     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
18140
18141     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
18142     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
18143
18144     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
18145     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
18146
18147     val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
18148     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
18149
18150     val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
18151     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
18152
18153     val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
18154     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
18155 }
18156
18157 static int
18158 bxe_pf_flr_clnup(struct bxe_softc *sc)
18159 {
18160     uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
18161
18162     BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
18163
18164     /* Re-enable PF target read access */
18165     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
18166
18167     /* Poll HW usage counters */
18168     BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
18169     if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
18170         return (-1);
18171     }
18172
18173     /* Zero the igu 'trailing edge' and 'leading edge' */
18174
18175     /* Send the FW cleanup command */
18176     if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
18177         return (-1);
18178     }
18179
18180     /* ATC cleanup */
18181
18182     /* Verify TX hw is flushed */
18183     bxe_tx_hw_flushed(sc, poll_cnt);
18184
18185     /* Wait 100ms (not adjusted according to platform) */
18186     DELAY(100000);
18187
18188     /* Verify no pending pci transactions */
18189     if (bxe_is_pcie_pending(sc)) {
18190         BLOGE(sc, "PCIE Transactions still pending\n");
18191     }
18192
18193     /* Debug */
18194     bxe_hw_enable_status(sc);
18195
18196     /*
18197      * Master enable - Due to WB DMAE writes performed before this
18198      * register is re-initialized as part of the regular function init
18199      */
18200     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18201
18202     return (0);
18203 }
18204
18205 static int
18206 bxe_init_hw_func(struct bxe_softc *sc)
18207 {
18208     int port = SC_PORT(sc);
18209     int func = SC_FUNC(sc);
18210     int init_phase = PHASE_PF0 + func;
18211     struct ecore_ilt *ilt = sc->ilt;
18212     uint16_t cdu_ilt_start;
18213     uint32_t addr, val;
18214     uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
18215     int i, main_mem_width, rc;
18216
18217     BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
18218
18219     /* FLR cleanup */
18220     if (!CHIP_IS_E1x(sc)) {
18221         rc = bxe_pf_flr_clnup(sc);
18222         if (rc) {
18223             BLOGE(sc, "FLR cleanup failed!\n");
18224             // XXX bxe_fw_dump(sc);
18225             // XXX bxe_idle_chk(sc);
18226             return (rc);
18227         }
18228     }
18229
18230     /* set MSI reconfigure capability */
18231     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18232         addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
18233         val = REG_RD(sc, addr);
18234         val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
18235         REG_WR(sc, addr, val);
18236     }
18237
18238     ecore_init_block(sc, BLOCK_PXP, init_phase);
18239     ecore_init_block(sc, BLOCK_PXP2, init_phase);
18240
18241     ilt = sc->ilt;
18242     cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
18243
18244     for (i = 0; i < L2_ILT_LINES(sc); i++) {
18245         ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
18246         ilt->lines[cdu_ilt_start + i].page_mapping =
18247             sc->context[i].vcxt_dma.paddr;
18248         ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
18249     }
18250     ecore_ilt_init_op(sc, INITOP_SET);
18251
18252     /* Set NIC mode */
18253     REG_WR(sc, PRS_REG_NIC_MODE, 1);
18254     BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
18255
18256     if (!CHIP_IS_E1x(sc)) {
18257         uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
18258
18259         /* Turn on a single ISR mode in IGU if driver is going to use
18260          * INT#x or MSI
18261          */
18262         if (sc->interrupt_mode != INTR_MODE_MSIX) {
18263             pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
18264         }
18265
18266         /*
18267          * Timers workaround bug: function init part.
18268          * Need to wait 20msec after initializing ILT,
18269          * needed to make sure there are no requests in
18270          * one of the PXP internal queues with "old" ILT addresses
18271          */
18272         DELAY(20000);
18273
18274         /*
18275          * Master enable - Due to WB DMAE writes performed before this
18276          * register is re-initialized as part of the regular function
18277          * init
18278          */
18279         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18280         /* Enable the function in IGU */
18281         REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
18282     }
18283
18284     sc->dmae_ready = 1;
18285
18286     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
18287
18288     if (!CHIP_IS_E1x(sc))
18289         REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
18290
18291     ecore_init_block(sc, BLOCK_ATC, init_phase);
18292     ecore_init_block(sc, BLOCK_DMAE, init_phase);
18293     ecore_init_block(sc, BLOCK_NIG, init_phase);
18294     ecore_init_block(sc, BLOCK_SRC, init_phase);
18295     ecore_init_block(sc, BLOCK_MISC, init_phase);
18296     ecore_init_block(sc, BLOCK_TCM, init_phase);
18297     ecore_init_block(sc, BLOCK_UCM, init_phase);
18298     ecore_init_block(sc, BLOCK_CCM, init_phase);
18299     ecore_init_block(sc, BLOCK_XCM, init_phase);
18300     ecore_init_block(sc, BLOCK_TSEM, init_phase);
18301     ecore_init_block(sc, BLOCK_USEM, init_phase);
18302     ecore_init_block(sc, BLOCK_CSEM, init_phase);
18303     ecore_init_block(sc, BLOCK_XSEM, init_phase);
18304
18305     if (!CHIP_IS_E1x(sc))
18306         REG_WR(sc, QM_REG_PF_EN, 1);
18307
18308     if (!CHIP_IS_E1x(sc)) {
18309         REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18310         REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18311         REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18312         REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18313     }
18314     ecore_init_block(sc, BLOCK_QM, init_phase);
18315
18316     ecore_init_block(sc, BLOCK_TM, init_phase);
18317     ecore_init_block(sc, BLOCK_DORQ, init_phase);
18318
18319     bxe_iov_init_dq(sc);
18320
18321     ecore_init_block(sc, BLOCK_BRB1, init_phase);
18322     ecore_init_block(sc, BLOCK_PRS, init_phase);
18323     ecore_init_block(sc, BLOCK_TSDM, init_phase);
18324     ecore_init_block(sc, BLOCK_CSDM, init_phase);
18325     ecore_init_block(sc, BLOCK_USDM, init_phase);
18326     ecore_init_block(sc, BLOCK_XSDM, init_phase);
18327     ecore_init_block(sc, BLOCK_UPB, init_phase);
18328     ecore_init_block(sc, BLOCK_XPB, init_phase);
18329     ecore_init_block(sc, BLOCK_PBF, init_phase);
18330     if (!CHIP_IS_E1x(sc))
18331         REG_WR(sc, PBF_REG_DISABLE_PF, 0);
18332
18333     ecore_init_block(sc, BLOCK_CDU, init_phase);
18334
18335     ecore_init_block(sc, BLOCK_CFC, init_phase);
18336
18337     if (!CHIP_IS_E1x(sc))
18338         REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
18339
18340     if (IS_MF(sc)) {
18341         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
18342         REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
18343     }
18344
18345     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
18346
18347     /* HC init per function */
18348     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18349         if (CHIP_IS_E1H(sc)) {
18350             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18351
18352             REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18353             REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18354         }
18355         ecore_init_block(sc, BLOCK_HC, init_phase);
18356
18357     } else {
18358         int num_segs, sb_idx, prod_offset;
18359
18360         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18361
18362         if (!CHIP_IS_E1x(sc)) {
18363             REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18364             REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18365         }
18366
18367         ecore_init_block(sc, BLOCK_IGU, init_phase);
18368
18369         if (!CHIP_IS_E1x(sc)) {
18370             int dsb_idx = 0;
18371             /**
18372              * Producer memory:
18373              * E2 mode: address 0-135 match to the mapping memory;
18374              * 136 - PF0 default prod; 137 - PF1 default prod;
18375              * 138 - PF2 default prod; 139 - PF3 default prod;
18376              * 140 - PF0 attn prod;    141 - PF1 attn prod;
18377              * 142 - PF2 attn prod;    143 - PF3 attn prod;
18378              * 144-147 reserved.
18379              *
18380              * E1.5 mode - In backward compatible mode;
18381              * for non default SB; each even line in the memory
18382              * holds the U producer and each odd line hold
18383              * the C producer. The first 128 producers are for
18384              * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
18385              * producers are for the DSB for each PF.
18386              * Each PF has five segments: (the order inside each
18387              * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
18388              * 132-135 C prods; 136-139 X prods; 140-143 T prods;
18389              * 144-147 attn prods;
18390              */
18391             /* non-default-status-blocks */
18392             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18393                 IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
18394             for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
18395                 prod_offset = (sc->igu_base_sb + sb_idx) *
18396                     num_segs;
18397
18398                 for (i = 0; i < num_segs; i++) {
18399                     addr = IGU_REG_PROD_CONS_MEMORY +
18400                             (prod_offset + i) * 4;
18401                     REG_WR(sc, addr, 0);
18402                 }
18403                 /* send consumer update with value 0 */
18404                 bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
18405                            USTORM_ID, 0, IGU_INT_NOP, 1);
18406                 bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
18407             }
18408
18409             /* default-status-blocks */
18410             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18411                 IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
18412
18413             if (CHIP_IS_MODE_4_PORT(sc))
18414                 dsb_idx = SC_FUNC(sc);
18415             else
18416                 dsb_idx = SC_VN(sc);
18417
18418             prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
18419                        IGU_BC_BASE_DSB_PROD + dsb_idx :
18420                        IGU_NORM_BASE_DSB_PROD + dsb_idx);
18421
18422             /*
18423              * igu prods come in chunks of E1HVN_MAX (4) -
18424              * does not matters what is the current chip mode
18425              */
18426             for (i = 0; i < (num_segs * E1HVN_MAX);
18427                  i += E1HVN_MAX) {
18428                 addr = IGU_REG_PROD_CONS_MEMORY +
18429                             (prod_offset + i)*4;
18430                 REG_WR(sc, addr, 0);
18431             }
18432             /* send consumer update with 0 */
18433             if (CHIP_INT_MODE_IS_BC(sc)) {
18434                 bxe_ack_sb(sc, sc->igu_dsb_id,
18435                            USTORM_ID, 0, IGU_INT_NOP, 1);
18436                 bxe_ack_sb(sc, sc->igu_dsb_id,
18437                            CSTORM_ID, 0, IGU_INT_NOP, 1);
18438                 bxe_ack_sb(sc, sc->igu_dsb_id,
18439                            XSTORM_ID, 0, IGU_INT_NOP, 1);
18440                 bxe_ack_sb(sc, sc->igu_dsb_id,
18441                            TSTORM_ID, 0, IGU_INT_NOP, 1);
18442                 bxe_ack_sb(sc, sc->igu_dsb_id,
18443                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
18444             } else {
18445                 bxe_ack_sb(sc, sc->igu_dsb_id,
18446                            USTORM_ID, 0, IGU_INT_NOP, 1);
18447                 bxe_ack_sb(sc, sc->igu_dsb_id,
18448                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
18449             }
18450             bxe_igu_clear_sb(sc, sc->igu_dsb_id);
18451
18452             /* !!! these should become driver const once
18453                rf-tool supports split-68 const */
18454             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
18455             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
18456             REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
18457             REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
18458             REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
18459             REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
18460         }
18461     }
18462
18463     /* Reset PCIE errors for debug */
18464     REG_WR(sc, 0x2114, 0xffffffff);
18465     REG_WR(sc, 0x2120, 0xffffffff);
18466
18467     if (CHIP_IS_E1x(sc)) {
18468         main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
18469         main_mem_base = HC_REG_MAIN_MEMORY +
18470                 SC_PORT(sc) * (main_mem_size * 4);
18471         main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
18472         main_mem_width = 8;
18473
18474         val = REG_RD(sc, main_mem_prty_clr);
18475         if (val) {
18476             BLOGD(sc, DBG_LOAD,
18477                   "Parity errors in HC block during function init (0x%x)!\n",
18478                   val);
18479         }
18480
18481         /* Clear "false" parity errors in MSI-X table */
18482         for (i = main_mem_base;
18483              i < main_mem_base + main_mem_size * 4;
18484              i += main_mem_width) {
18485             bxe_read_dmae(sc, i, main_mem_width / 4);
18486             bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
18487                            i, main_mem_width / 4);
18488         }
18489         /* Clear HC parity attention */
18490         REG_RD(sc, main_mem_prty_clr);
18491     }
18492
18493 #if 1
18494     /* Enable STORMs SP logging */
18495     REG_WR8(sc, BAR_USTRORM_INTMEM +
18496            USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18497     REG_WR8(sc, BAR_TSTRORM_INTMEM +
18498            TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18499     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18500            CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18501     REG_WR8(sc, BAR_XSTRORM_INTMEM +
18502            XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18503 #endif
18504
18505     elink_phy_probe(&sc->link_params);
18506
18507     return (0);
18508 }
18509
18510 static void
18511 bxe_link_reset(struct bxe_softc *sc)
18512 {
18513     if (!BXE_NOMCP(sc)) {
18514         bxe_acquire_phy_lock(sc);
18515         elink_lfa_reset(&sc->link_params, &sc->link_vars);
18516         bxe_release_phy_lock(sc);
18517     } else {
18518         if (!CHIP_REV_IS_SLOW(sc)) {
18519             BLOGW(sc, "Bootcode is missing - cannot reset link\n");
18520         }
18521     }
18522 }
18523
18524 static void
18525 bxe_reset_port(struct bxe_softc *sc)
18526 {
18527     int port = SC_PORT(sc);
18528     uint32_t val;
18529
18530         ELINK_DEBUG_P0(sc, "bxe_reset_port called\n");
18531     /* reset physical Link */
18532     bxe_link_reset(sc);
18533
18534     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18535
18536     /* Do not rcv packets to BRB */
18537     REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18538     /* Do not direct rcv packets that are not for MCP to the BRB */
18539     REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18540                NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18541
18542     /* Configure AEU */
18543     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18544
18545     DELAY(100000);
18546
18547     /* Check for BRB port occupancy */
18548     val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18549     if (val) {
18550         BLOGD(sc, DBG_LOAD,
18551               "BRB1 is not empty, %d blocks are occupied\n", val);
18552     }
18553
18554     /* TODO: Close Doorbell port? */
18555 }
18556
18557 static void
18558 bxe_ilt_wr(struct bxe_softc *sc,
18559            uint32_t         index,
18560            bus_addr_t       addr)
18561 {
18562     int reg;
18563     uint32_t wb_write[2];
18564
18565     if (CHIP_IS_E1(sc)) {
18566         reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18567     } else {
18568         reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18569     }
18570
18571     wb_write[0] = ONCHIP_ADDR1(addr);
18572     wb_write[1] = ONCHIP_ADDR2(addr);
18573     REG_WR_DMAE(sc, reg, wb_write, 2);
18574 }
18575
18576 static void
18577 bxe_clear_func_ilt(struct bxe_softc *sc,
18578                    uint32_t         func)
18579 {
18580     uint32_t i, base = FUNC_ILT_BASE(func);
18581     for (i = base; i < base + ILT_PER_FUNC; i++) {
18582         bxe_ilt_wr(sc, i, 0);
18583     }
18584 }
18585
18586 static void
18587 bxe_reset_func(struct bxe_softc *sc)
18588 {
18589     struct bxe_fastpath *fp;
18590     int port = SC_PORT(sc);
18591     int func = SC_FUNC(sc);
18592     int i;
18593
18594     /* Disable the function in the FW */
18595     REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18596     REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18597     REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18598     REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18599
18600     /* FP SBs */
18601     FOR_EACH_ETH_QUEUE(sc, i) {
18602         fp = &sc->fp[i];
18603         REG_WR8(sc, BAR_CSTRORM_INTMEM +
18604                 CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18605                 SB_DISABLED);
18606     }
18607
18608     /* SP SB */
18609     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18610             CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18611             SB_DISABLED);
18612
18613     for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18614         REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18615     }
18616
18617     /* Configure IGU */
18618     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18619         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18620         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18621     } else {
18622         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18623         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18624     }
18625
18626     if (CNIC_LOADED(sc)) {
18627         /* Disable Timer scan */
18628         REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18629         /*
18630          * Wait for at least 10ms and up to 2 second for the timers
18631          * scan to complete
18632          */
18633         for (i = 0; i < 200; i++) {
18634             DELAY(10000);
18635             if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18636                 break;
18637         }
18638     }
18639
18640     /* Clear ILT */
18641     bxe_clear_func_ilt(sc, func);
18642
18643     /*
18644      * Timers workaround bug for E2: if this is vnic-3,
18645      * we need to set the entire ilt range for this timers.
18646      */
18647     if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18648         struct ilt_client_info ilt_cli;
18649         /* use dummy TM client */
18650         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18651         ilt_cli.start = 0;
18652         ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18653         ilt_cli.client_num = ILT_CLIENT_TM;
18654
18655         ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18656     }
18657
18658     /* this assumes that reset_port() called before reset_func()*/
18659     if (!CHIP_IS_E1x(sc)) {
18660         bxe_pf_disable(sc);
18661     }
18662
18663     sc->dmae_ready = 0;
18664 }
18665
18666 static int
18667 bxe_gunzip_init(struct bxe_softc *sc)
18668 {
18669     return (0);
18670 }
18671
18672 static void
18673 bxe_gunzip_end(struct bxe_softc *sc)
18674 {
18675     return;
18676 }
18677
18678 static int
18679 bxe_init_firmware(struct bxe_softc *sc)
18680 {
18681     if (CHIP_IS_E1(sc)) {
18682         ecore_init_e1_firmware(sc);
18683         sc->iro_array = e1_iro_arr;
18684     } else if (CHIP_IS_E1H(sc)) {
18685         ecore_init_e1h_firmware(sc);
18686         sc->iro_array = e1h_iro_arr;
18687     } else if (!CHIP_IS_E1x(sc)) {
18688         ecore_init_e2_firmware(sc);
18689         sc->iro_array = e2_iro_arr;
18690     } else {
18691         BLOGE(sc, "Unsupported chip revision\n");
18692         return (-1);
18693     }
18694
18695     return (0);
18696 }
18697
18698 static void
18699 bxe_release_firmware(struct bxe_softc *sc)
18700 {
18701     /* Do nothing */
18702     return;
18703 }
18704
18705 static int
18706 ecore_gunzip(struct bxe_softc *sc,
18707              const uint8_t    *zbuf,
18708              int              len)
18709 {
18710     /* XXX : Implement... */
18711     BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18712     return (FALSE);
18713 }
18714
18715 static void
18716 ecore_reg_wr_ind(struct bxe_softc *sc,
18717                  uint32_t         addr,
18718                  uint32_t         val)
18719 {
18720     bxe_reg_wr_ind(sc, addr, val);
18721 }
18722
18723 static void
18724 ecore_write_dmae_phys_len(struct bxe_softc *sc,
18725                           bus_addr_t       phys_addr,
18726                           uint32_t         addr,
18727                           uint32_t         len)
18728 {
18729     bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18730 }
18731
18732 void
18733 ecore_storm_memset_struct(struct bxe_softc *sc,
18734                           uint32_t         addr,
18735                           size_t           size,
18736                           uint32_t         *data)
18737 {
18738     uint8_t i;
18739     for (i = 0; i < size/4; i++) {
18740         REG_WR(sc, addr + (i * 4), data[i]);
18741     }
18742 }
18743
18744
18745 /*
18746  * character device - ioctl interface definitions
18747  */
18748
18749
18750 #include "bxe_dump.h"
18751 #include "bxe_ioctl.h"
18752 #include <sys/conf.h>
18753
18754 static int bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
18755                 struct thread *td);
18756
18757 static struct cdevsw bxe_cdevsw = {
18758     .d_version = D_VERSION,
18759     .d_ioctl = bxe_eioctl,
18760     .d_name = "bxecnic",
18761 };
18762
18763 #define BXE_PATH(sc)    (CHIP_IS_E1x(sc) ? 0 : (sc->pcie_func & 1))
18764
18765
18766 #define DUMP_ALL_PRESETS        0x1FFF
18767 #define DUMP_MAX_PRESETS        13
18768 #define IS_E1_REG(chips)        ((chips & DUMP_CHIP_E1) == DUMP_CHIP_E1)
18769 #define IS_E1H_REG(chips)       ((chips & DUMP_CHIP_E1H) == DUMP_CHIP_E1H)
18770 #define IS_E2_REG(chips)        ((chips & DUMP_CHIP_E2) == DUMP_CHIP_E2)
18771 #define IS_E3A0_REG(chips)      ((chips & DUMP_CHIP_E3A0) == DUMP_CHIP_E3A0)
18772 #define IS_E3B0_REG(chips)      ((chips & DUMP_CHIP_E3B0) == DUMP_CHIP_E3B0)
18773
18774 #define IS_REG_IN_PRESET(presets, idx)  \
18775                 ((presets & (1 << (idx-1))) == (1 << (idx-1)))
18776
18777
18778 static int
18779 bxe_get_preset_regs_len(struct bxe_softc *sc, uint32_t preset)
18780 {
18781     if (CHIP_IS_E1(sc))
18782         return dump_num_registers[0][preset-1];
18783     else if (CHIP_IS_E1H(sc))
18784         return dump_num_registers[1][preset-1];
18785     else if (CHIP_IS_E2(sc))
18786         return dump_num_registers[2][preset-1];
18787     else if (CHIP_IS_E3A0(sc))
18788         return dump_num_registers[3][preset-1];
18789     else if (CHIP_IS_E3B0(sc))
18790         return dump_num_registers[4][preset-1];
18791     else
18792         return 0;
18793 }
18794
18795 static int
18796 bxe_get_total_regs_len32(struct bxe_softc *sc)
18797 {
18798     uint32_t preset_idx;
18799     int regdump_len32 = 0;
18800
18801
18802     /* Calculate the total preset regs length */
18803     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
18804         regdump_len32 += bxe_get_preset_regs_len(sc, preset_idx);
18805     }
18806
18807     return regdump_len32;
18808 }
18809
18810 static const uint32_t *
18811 __bxe_get_page_addr_ar(struct bxe_softc *sc)
18812 {
18813     if (CHIP_IS_E2(sc))
18814         return page_vals_e2;
18815     else if (CHIP_IS_E3(sc))
18816         return page_vals_e3;
18817     else
18818         return NULL;
18819 }
18820
18821 static uint32_t
18822 __bxe_get_page_reg_num(struct bxe_softc *sc)
18823 {
18824     if (CHIP_IS_E2(sc))
18825         return PAGE_MODE_VALUES_E2;
18826     else if (CHIP_IS_E3(sc))
18827         return PAGE_MODE_VALUES_E3;
18828     else
18829         return 0;
18830 }
18831
18832 static const uint32_t *
18833 __bxe_get_page_write_ar(struct bxe_softc *sc)
18834 {
18835     if (CHIP_IS_E2(sc))
18836         return page_write_regs_e2;
18837     else if (CHIP_IS_E3(sc))
18838         return page_write_regs_e3;
18839     else
18840         return NULL;
18841 }
18842
18843 static uint32_t
18844 __bxe_get_page_write_num(struct bxe_softc *sc)
18845 {
18846     if (CHIP_IS_E2(sc))
18847         return PAGE_WRITE_REGS_E2;
18848     else if (CHIP_IS_E3(sc))
18849         return PAGE_WRITE_REGS_E3;
18850     else
18851         return 0;
18852 }
18853
18854 static const struct reg_addr *
18855 __bxe_get_page_read_ar(struct bxe_softc *sc)
18856 {
18857     if (CHIP_IS_E2(sc))
18858         return page_read_regs_e2;
18859     else if (CHIP_IS_E3(sc))
18860         return page_read_regs_e3;
18861     else
18862         return NULL;
18863 }
18864
18865 static uint32_t
18866 __bxe_get_page_read_num(struct bxe_softc *sc)
18867 {
18868     if (CHIP_IS_E2(sc))
18869         return PAGE_READ_REGS_E2;
18870     else if (CHIP_IS_E3(sc))
18871         return PAGE_READ_REGS_E3;
18872     else
18873         return 0;
18874 }
18875
18876 static bool
18877 bxe_is_reg_in_chip(struct bxe_softc *sc, const struct reg_addr *reg_info)
18878 {
18879     if (CHIP_IS_E1(sc))
18880         return IS_E1_REG(reg_info->chips);
18881     else if (CHIP_IS_E1H(sc))
18882         return IS_E1H_REG(reg_info->chips);
18883     else if (CHIP_IS_E2(sc))
18884         return IS_E2_REG(reg_info->chips);
18885     else if (CHIP_IS_E3A0(sc))
18886         return IS_E3A0_REG(reg_info->chips);
18887     else if (CHIP_IS_E3B0(sc))
18888         return IS_E3B0_REG(reg_info->chips);
18889     else
18890         return 0;
18891 }
18892
18893 static bool
18894 bxe_is_wreg_in_chip(struct bxe_softc *sc, const struct wreg_addr *wreg_info)
18895 {
18896     if (CHIP_IS_E1(sc))
18897         return IS_E1_REG(wreg_info->chips);
18898     else if (CHIP_IS_E1H(sc))
18899         return IS_E1H_REG(wreg_info->chips);
18900     else if (CHIP_IS_E2(sc))
18901         return IS_E2_REG(wreg_info->chips);
18902     else if (CHIP_IS_E3A0(sc))
18903         return IS_E3A0_REG(wreg_info->chips);
18904     else if (CHIP_IS_E3B0(sc))
18905         return IS_E3B0_REG(wreg_info->chips);
18906     else
18907         return 0;
18908 }
18909
18910 /**
18911  * bxe_read_pages_regs - read "paged" registers
18912  *
18913  * @bp          device handle
18914  * @p           output buffer
18915  *
18916  * Reads "paged" memories: memories that may only be read by first writing to a
18917  * specific address ("write address") and then reading from a specific address
18918  * ("read address"). There may be more than one write address per "page" and
18919  * more than one read address per write address.
18920  */
18921 static void
18922 bxe_read_pages_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18923 {
18924     uint32_t i, j, k, n;
18925
18926     /* addresses of the paged registers */
18927     const uint32_t *page_addr = __bxe_get_page_addr_ar(sc);
18928     /* number of paged registers */
18929     int num_pages = __bxe_get_page_reg_num(sc);
18930     /* write addresses */
18931     const uint32_t *write_addr = __bxe_get_page_write_ar(sc);
18932     /* number of write addresses */
18933     int write_num = __bxe_get_page_write_num(sc);
18934     /* read addresses info */
18935     const struct reg_addr *read_addr = __bxe_get_page_read_ar(sc);
18936     /* number of read addresses */
18937     int read_num = __bxe_get_page_read_num(sc);
18938     uint32_t addr, size;
18939
18940     for (i = 0; i < num_pages; i++) {
18941         for (j = 0; j < write_num; j++) {
18942             REG_WR(sc, write_addr[j], page_addr[i]);
18943
18944             for (k = 0; k < read_num; k++) {
18945                 if (IS_REG_IN_PRESET(read_addr[k].presets, preset)) {
18946                     size = read_addr[k].size;
18947                     for (n = 0; n < size; n++) {
18948                         addr = read_addr[k].addr + n*4;
18949                         *p++ = REG_RD(sc, addr);
18950                     }
18951                 }
18952             }
18953         }
18954     }
18955     return;
18956 }
18957
18958
18959 static int
18960 bxe_get_preset_regs(struct bxe_softc *sc, uint32_t *p, uint32_t preset)
18961 {
18962     uint32_t i, j, addr;
18963     const struct wreg_addr *wreg_addr_p = NULL;
18964
18965     if (CHIP_IS_E1(sc))
18966         wreg_addr_p = &wreg_addr_e1;
18967     else if (CHIP_IS_E1H(sc))
18968         wreg_addr_p = &wreg_addr_e1h;
18969     else if (CHIP_IS_E2(sc))
18970         wreg_addr_p = &wreg_addr_e2;
18971     else if (CHIP_IS_E3A0(sc))
18972         wreg_addr_p = &wreg_addr_e3;
18973     else if (CHIP_IS_E3B0(sc))
18974         wreg_addr_p = &wreg_addr_e3b0;
18975     else
18976         return (-1);
18977
18978     /* Read the idle_chk registers */
18979     for (i = 0; i < IDLE_REGS_COUNT; i++) {
18980         if (bxe_is_reg_in_chip(sc, &idle_reg_addrs[i]) &&
18981             IS_REG_IN_PRESET(idle_reg_addrs[i].presets, preset)) {
18982             for (j = 0; j < idle_reg_addrs[i].size; j++)
18983                 *p++ = REG_RD(sc, idle_reg_addrs[i].addr + j*4);
18984         }
18985     }
18986
18987     /* Read the regular registers */
18988     for (i = 0; i < REGS_COUNT; i++) {
18989         if (bxe_is_reg_in_chip(sc, &reg_addrs[i]) &&
18990             IS_REG_IN_PRESET(reg_addrs[i].presets, preset)) {
18991             for (j = 0; j < reg_addrs[i].size; j++)
18992                 *p++ = REG_RD(sc, reg_addrs[i].addr + j*4);
18993         }
18994     }
18995
18996     /* Read the CAM registers */
18997     if (bxe_is_wreg_in_chip(sc, wreg_addr_p) &&
18998         IS_REG_IN_PRESET(wreg_addr_p->presets, preset)) {
18999         for (i = 0; i < wreg_addr_p->size; i++) {
19000             *p++ = REG_RD(sc, wreg_addr_p->addr + i*4);
19001
19002             /* In case of wreg_addr register, read additional
19003                registers from read_regs array
19004              */
19005             for (j = 0; j < wreg_addr_p->read_regs_count; j++) {
19006                 addr = *(wreg_addr_p->read_regs);
19007                 *p++ = REG_RD(sc, addr + j*4);
19008             }
19009         }
19010     }
19011
19012     /* Paged registers are supported in E2 & E3 only */
19013     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
19014         /* Read "paged" registers */
19015         bxe_read_pages_regs(sc, p, preset);
19016     }
19017
19018     return 0;
19019 }
19020
19021 int
19022 bxe_grc_dump(struct bxe_softc *sc)
19023 {
19024     int rval = 0;
19025     uint32_t preset_idx;
19026     uint8_t *buf;
19027     uint32_t size;
19028     struct  dump_header *d_hdr;
19029     uint32_t i;
19030     uint32_t reg_val;
19031     uint32_t reg_addr;
19032     uint32_t cmd_offset;
19033     struct ecore_ilt *ilt = SC_ILT(sc);
19034     struct bxe_fastpath *fp;
19035     struct ilt_client_info *ilt_cli;
19036     int grc_dump_size;
19037
19038
19039     if (sc->grcdump_done || sc->grcdump_started)
19040         return (rval);
19041     
19042     sc->grcdump_started = 1;
19043     BLOGI(sc, "Started collecting grcdump\n");
19044
19045     grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19046                 sizeof(struct  dump_header);
19047
19048     sc->grc_dump = malloc(grc_dump_size, M_DEVBUF, M_NOWAIT);
19049
19050     if (sc->grc_dump == NULL) {
19051         BLOGW(sc, "Unable to allocate memory for grcdump collection\n");
19052         return(ENOMEM);
19053     }
19054
19055
19056
19057     /* Disable parity attentions as long as following dump may
19058      * cause false alarms by reading never written registers. We
19059      * will re-enable parity attentions right after the dump.
19060      */
19061
19062     /* Disable parity on path 0 */
19063     bxe_pretend_func(sc, 0);
19064
19065     ecore_disable_blocks_parity(sc);
19066
19067     /* Disable parity on path 1 */
19068     bxe_pretend_func(sc, 1);
19069     ecore_disable_blocks_parity(sc);
19070
19071     /* Return to current function */
19072     bxe_pretend_func(sc, SC_ABS_FUNC(sc));
19073
19074     buf = sc->grc_dump;
19075     d_hdr = sc->grc_dump;
19076
19077     d_hdr->header_size = (sizeof(struct  dump_header) >> 2) - 1;
19078     d_hdr->version = BNX2X_DUMP_VERSION;
19079     d_hdr->preset = DUMP_ALL_PRESETS;
19080
19081     if (CHIP_IS_E1(sc)) {
19082         d_hdr->dump_meta_data = DUMP_CHIP_E1;
19083     } else if (CHIP_IS_E1H(sc)) {
19084         d_hdr->dump_meta_data = DUMP_CHIP_E1H;
19085     } else if (CHIP_IS_E2(sc)) {
19086         d_hdr->dump_meta_data = DUMP_CHIP_E2 |
19087                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
19088     } else if (CHIP_IS_E3A0(sc)) {
19089         d_hdr->dump_meta_data = DUMP_CHIP_E3A0 |
19090                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
19091     } else if (CHIP_IS_E3B0(sc)) {
19092         d_hdr->dump_meta_data = DUMP_CHIP_E3B0 |
19093                 (BXE_PATH(sc) ? DUMP_PATH_1 : DUMP_PATH_0);
19094     }
19095
19096     buf += sizeof(struct  dump_header);
19097
19098     for (preset_idx = 1; preset_idx <= DUMP_MAX_PRESETS; preset_idx++) {
19099
19100         /* Skip presets with IOR */
19101         if ((preset_idx == 2) || (preset_idx == 5) || (preset_idx == 8) ||
19102             (preset_idx == 11))
19103             continue;
19104
19105         rval = bxe_get_preset_regs(sc, (uint32_t *)buf, preset_idx);
19106
19107         if (rval)
19108             break;
19109
19110         size = bxe_get_preset_regs_len(sc, preset_idx) * (sizeof (uint32_t));
19111
19112         buf += size;
19113     }
19114
19115     bxe_pretend_func(sc, 0);
19116     ecore_clear_blocks_parity(sc);
19117     ecore_enable_blocks_parity(sc);
19118
19119     bxe_pretend_func(sc, 1);
19120     ecore_clear_blocks_parity(sc);
19121     ecore_enable_blocks_parity(sc);
19122
19123     /* Return to current function */
19124     bxe_pretend_func(sc, SC_ABS_FUNC(sc));
19125
19126
19127
19128     if(sc->state == BXE_STATE_OPEN) {
19129         if(sc->fw_stats_req  != NULL) {
19130                 BLOGI(sc, "fw stats start_paddr %#jx end_paddr %#jx vaddr %p size 0x%x\n",
19131                                 (uintmax_t)sc->fw_stats_req_mapping,
19132                                 (uintmax_t)sc->fw_stats_data_mapping,
19133                                 sc->fw_stats_req, (sc->fw_stats_req_size + sc->fw_stats_data_size));
19134                 }       
19135                 if(sc->def_sb != NULL) {
19136                         BLOGI(sc, "def_status_block paddr %p vaddr %p size 0x%zx\n",
19137                                 (void *)sc->def_sb_dma.paddr, sc->def_sb,
19138                                 sizeof(struct host_sp_status_block));
19139                 }
19140                 if(sc->eq_dma.vaddr != NULL) {
19141                 BLOGI(sc, "event_queue paddr %#jx vaddr %p size 0x%x\n",
19142                                 (uintmax_t)sc->eq_dma.paddr, sc->eq_dma.vaddr, BCM_PAGE_SIZE);
19143                 }
19144                 if(sc->sp_dma.vaddr != NULL) {
19145                 BLOGI(sc, "slow path paddr %#jx vaddr %p size 0x%zx\n",
19146                                 (uintmax_t)sc->sp_dma.paddr, sc->sp_dma.vaddr,
19147                                 sizeof(struct bxe_slowpath));
19148                 }
19149                 if(sc->spq_dma.vaddr != NULL) {
19150                 BLOGI(sc, "slow path queue paddr %#jx vaddr %p size 0x%x\n",
19151                                 (uintmax_t)sc->spq_dma.paddr, sc->spq_dma.vaddr, BCM_PAGE_SIZE);
19152                 }
19153                 if(sc->gz_buf_dma.vaddr != NULL) {
19154                 BLOGI(sc, "fw_buf paddr %#jx vaddr %p size 0x%x\n",
19155                                 (uintmax_t)sc->gz_buf_dma.paddr, sc->gz_buf_dma.vaddr,
19156                                 FW_BUF_SIZE);
19157                 }
19158         for (i = 0; i < sc->num_queues; i++) {
19159                 fp = &sc->fp[i];
19160                         if(fp->sb_dma.vaddr != NULL && fp->tx_dma.vaddr != NULL &&
19161                         fp->rx_dma.vaddr != NULL && fp->rcq_dma.vaddr != NULL &&
19162                         fp->rx_sge_dma.vaddr != NULL) {
19163
19164                                 BLOGI(sc, "FP status block fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
19165                                 (uintmax_t)fp->sb_dma.paddr, fp->sb_dma.vaddr,
19166                                 sizeof(union bxe_host_hc_status_block));
19167                                 BLOGI(sc, "TX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
19168                                 (uintmax_t)fp->tx_dma.paddr, fp->tx_dma.vaddr,
19169                                 (BCM_PAGE_SIZE * TX_BD_NUM_PAGES));
19170                         BLOGI(sc, "RX BD CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
19171                                 (uintmax_t)fp->rx_dma.paddr, fp->rx_dma.vaddr,
19172                                 (BCM_PAGE_SIZE * RX_BD_NUM_PAGES));
19173                         BLOGI(sc, "RX RCQ CHAIN fp %d paddr %#jx vaddr %p size 0x%zx\n", i,
19174                                 (uintmax_t)fp->rcq_dma.paddr, fp->rcq_dma.vaddr,
19175                                 (BCM_PAGE_SIZE * RCQ_NUM_PAGES));
19176                         BLOGI(sc, "RX SGE CHAIN fp %d paddr %#jx vaddr %p size 0x%x\n", i,
19177                                 (uintmax_t)fp->rx_sge_dma.paddr, fp->rx_sge_dma.vaddr,
19178                                 (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES));
19179                 }
19180                 }
19181                 if(ilt != NULL ) {
19182                 ilt_cli = &ilt->clients[1];
19183                         if(ilt->lines != NULL) {
19184                 for (i = ilt_cli->start; i <= ilt_cli->end; i++) {
19185                         BLOGI(sc, "ECORE_ILT paddr %#jx vaddr %p size 0x%x\n",
19186                                 (uintmax_t)(((struct bxe_dma *)((&ilt->lines[i])->page))->paddr),
19187                                 ((struct bxe_dma *)((&ilt->lines[i])->page))->vaddr, BCM_PAGE_SIZE);
19188                 }
19189                         }
19190                 }
19191
19192
19193         cmd_offset = DMAE_REG_CMD_MEM;
19194         for (i = 0; i < 224; i++) {
19195                 reg_addr = (cmd_offset +(i * 4));
19196                 reg_val = REG_RD(sc, reg_addr);
19197                 BLOGI(sc, "DMAE_REG_CMD_MEM i=%d reg_addr 0x%x reg_val 0x%08x\n",i,
19198                                 reg_addr, reg_val);
19199         }
19200         }
19201
19202     BLOGI(sc, "Collection of grcdump done\n");
19203     sc->grcdump_done = 1;
19204     return(rval);
19205 }
19206
19207 static int
19208 bxe_add_cdev(struct bxe_softc *sc)
19209 {
19210     sc->eeprom = malloc(BXE_EEPROM_MAX_DATA_LEN, M_DEVBUF, M_NOWAIT);
19211
19212     if (sc->eeprom == NULL) {
19213         BLOGW(sc, "Unable to alloc for eeprom size buffer\n");
19214         return (-1);
19215     }
19216
19217     sc->ioctl_dev = make_dev(&bxe_cdevsw,
19218                             sc->ifnet->if_dunit,
19219                             UID_ROOT,
19220                             GID_WHEEL,
19221                             0600,
19222                             "%s",
19223                             if_name(sc->ifnet));
19224
19225     if (sc->ioctl_dev == NULL) {
19226         free(sc->eeprom, M_DEVBUF);
19227         sc->eeprom = NULL;
19228         return (-1);
19229     }
19230
19231     sc->ioctl_dev->si_drv1 = sc;
19232
19233     return (0);
19234 }
19235
19236 static void
19237 bxe_del_cdev(struct bxe_softc *sc)
19238 {
19239     if (sc->ioctl_dev != NULL)
19240         destroy_dev(sc->ioctl_dev);
19241
19242     if (sc->eeprom != NULL) {
19243         free(sc->eeprom, M_DEVBUF);
19244         sc->eeprom = NULL;
19245     }
19246     sc->ioctl_dev = NULL;
19247
19248     return;
19249 }
19250
19251 static bool bxe_is_nvram_accessible(struct bxe_softc *sc)
19252 {
19253
19254     if ((sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) == 0)
19255         return FALSE;
19256
19257     return TRUE;
19258 }
19259
19260
19261 static int
19262 bxe_wr_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
19263 {
19264     int rval = 0;
19265
19266     if(!bxe_is_nvram_accessible(sc)) {
19267         BLOGW(sc, "Cannot access eeprom when interface is down\n");
19268         return (-EAGAIN);
19269     }
19270     rval = bxe_nvram_write(sc, offset, (uint8_t *)data, len);
19271
19272
19273    return (rval);
19274 }
19275
19276 static int
19277 bxe_rd_eeprom(struct bxe_softc *sc, void *data, uint32_t offset, uint32_t len)
19278 {
19279     int rval = 0;
19280
19281     if(!bxe_is_nvram_accessible(sc)) {
19282         BLOGW(sc, "Cannot access eeprom when interface is down\n");
19283         return (-EAGAIN);
19284     }
19285     rval = bxe_nvram_read(sc, offset, (uint8_t *)data, len);
19286
19287    return (rval);
19288 }
19289
19290 static int
19291 bxe_eeprom_rd_wr(struct bxe_softc *sc, bxe_eeprom_t *eeprom)
19292 {
19293     int rval = 0;
19294
19295     switch (eeprom->eeprom_cmd) {
19296
19297     case BXE_EEPROM_CMD_SET_EEPROM:
19298
19299         rval = copyin(eeprom->eeprom_data, sc->eeprom,
19300                        eeprom->eeprom_data_len);
19301
19302         if (rval)
19303             break;
19304
19305         rval = bxe_wr_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
19306                        eeprom->eeprom_data_len);
19307         break;
19308
19309     case BXE_EEPROM_CMD_GET_EEPROM:
19310
19311         rval = bxe_rd_eeprom(sc, sc->eeprom, eeprom->eeprom_offset,
19312                        eeprom->eeprom_data_len);
19313
19314         if (rval) {
19315             break;
19316         }
19317
19318         rval = copyout(sc->eeprom, eeprom->eeprom_data,
19319                        eeprom->eeprom_data_len);
19320         break;
19321
19322     default:
19323             rval = EINVAL;
19324             break;
19325     }
19326
19327     if (rval) {
19328         BLOGW(sc, "ioctl cmd %d  failed rval %d\n", eeprom->eeprom_cmd, rval);
19329     }
19330
19331     return (rval);
19332 }
19333
19334 static int
19335 bxe_get_settings(struct bxe_softc *sc, bxe_dev_setting_t *dev_p)
19336 {
19337     uint32_t ext_phy_config;
19338     int port = SC_PORT(sc);
19339     int cfg_idx = bxe_get_link_cfg_idx(sc);
19340
19341     dev_p->supported = sc->port.supported[cfg_idx] |
19342             (sc->port.supported[cfg_idx ^ 1] &
19343             (ELINK_SUPPORTED_TP | ELINK_SUPPORTED_FIBRE));
19344     dev_p->advertising = sc->port.advertising[cfg_idx];
19345     if(sc->link_params.phy[bxe_get_cur_phy_idx(sc)].media_type ==
19346         ELINK_ETH_PHY_SFP_1G_FIBER) {
19347         dev_p->supported = ~(ELINK_SUPPORTED_10000baseT_Full);
19348         dev_p->advertising &= ~(ADVERTISED_10000baseT_Full);
19349     }
19350     if ((sc->state == BXE_STATE_OPEN) && sc->link_vars.link_up &&
19351         !(sc->flags & BXE_MF_FUNC_DIS)) {
19352         dev_p->duplex = sc->link_vars.duplex;
19353         if (IS_MF(sc) && !BXE_NOMCP(sc))
19354             dev_p->speed = bxe_get_mf_speed(sc);
19355         else
19356             dev_p->speed = sc->link_vars.line_speed;
19357     } else {
19358         dev_p->duplex = DUPLEX_UNKNOWN;
19359         dev_p->speed = SPEED_UNKNOWN;
19360     }
19361
19362     dev_p->port = bxe_media_detect(sc);
19363
19364     ext_phy_config = SHMEM_RD(sc,
19365                          dev_info.port_hw_config[port].external_phy_config);
19366     if((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) ==
19367         PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
19368         dev_p->phy_address =  sc->port.phy_addr;
19369     else if(((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
19370             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
19371         ((ext_phy_config & PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK) !=
19372             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
19373         dev_p->phy_address = ELINK_XGXS_EXT_PHY_ADDR(ext_phy_config);
19374     else
19375         dev_p->phy_address = 0;
19376
19377     if(sc->link_params.req_line_speed[cfg_idx] == ELINK_SPEED_AUTO_NEG)
19378         dev_p->autoneg = AUTONEG_ENABLE;
19379     else
19380        dev_p->autoneg = AUTONEG_DISABLE;
19381
19382
19383     return 0;
19384 }
19385
19386 static int
19387 bxe_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
19388         struct thread *td)
19389 {
19390     struct bxe_softc    *sc;
19391     int                 rval = 0;
19392     device_t            pci_dev;
19393     bxe_grcdump_t       *dump = NULL;
19394     int grc_dump_size;
19395     bxe_drvinfo_t   *drv_infop = NULL;
19396     bxe_dev_setting_t  *dev_p;
19397     bxe_dev_setting_t  dev_set;
19398     bxe_get_regs_t  *reg_p;
19399     bxe_reg_rdw_t *reg_rdw_p;
19400     bxe_pcicfg_rdw_t *cfg_rdw_p;
19401     bxe_perm_mac_addr_t *mac_addr_p;
19402
19403
19404     if ((sc = (struct bxe_softc *)dev->si_drv1) == NULL)
19405         return ENXIO;
19406
19407     pci_dev= sc->dev;
19408
19409     dump = (bxe_grcdump_t *)data;
19410
19411     switch(cmd) {
19412
19413         case BXE_GRC_DUMP_SIZE:
19414             dump->pci_func = sc->pcie_func;
19415             dump->grcdump_size =
19416                 (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19417                      sizeof(struct  dump_header);
19418             break;
19419
19420         case BXE_GRC_DUMP:
19421             
19422             grc_dump_size = (bxe_get_total_regs_len32(sc) * sizeof(uint32_t)) +
19423                                 sizeof(struct  dump_header);
19424             if ((!sc->trigger_grcdump) || (dump->grcdump == NULL) ||
19425                 (dump->grcdump_size < grc_dump_size)) {
19426                 rval = EINVAL;
19427                 break;
19428             }
19429
19430             if((sc->trigger_grcdump) && (!sc->grcdump_done) &&
19431                 (!sc->grcdump_started)) {
19432                 rval =  bxe_grc_dump(sc);
19433             }
19434
19435             if((!rval) && (sc->grcdump_done) && (sc->grcdump_started) &&
19436                 (sc->grc_dump != NULL))  {
19437                 dump->grcdump_dwords = grc_dump_size >> 2;
19438                 rval = copyout(sc->grc_dump, dump->grcdump, grc_dump_size);
19439                 free(sc->grc_dump, M_DEVBUF);
19440                 sc->grc_dump = NULL;
19441                 sc->grcdump_started = 0;
19442                 sc->grcdump_done = 0;
19443             }
19444
19445             break;
19446
19447         case BXE_DRV_INFO:
19448             drv_infop = (bxe_drvinfo_t *)data;
19449             snprintf(drv_infop->drv_name, BXE_DRV_NAME_LENGTH, "%s", "bxe");
19450             snprintf(drv_infop->drv_version, BXE_DRV_VERSION_LENGTH, "v:%s",
19451                 BXE_DRIVER_VERSION);
19452             snprintf(drv_infop->mfw_version, BXE_MFW_VERSION_LENGTH, "%s",
19453                 sc->devinfo.bc_ver_str);
19454             snprintf(drv_infop->stormfw_version, BXE_STORMFW_VERSION_LENGTH,
19455                 "%s", sc->fw_ver_str);
19456             drv_infop->eeprom_dump_len = sc->devinfo.flash_size;
19457             drv_infop->reg_dump_len =
19458                 (bxe_get_total_regs_len32(sc) * sizeof(uint32_t))
19459                     + sizeof(struct  dump_header);
19460             snprintf(drv_infop->bus_info, BXE_BUS_INFO_LENGTH, "%d:%d:%d",
19461                 sc->pcie_bus, sc->pcie_device, sc->pcie_func);
19462             break;
19463
19464         case BXE_DEV_SETTING:
19465             dev_p = (bxe_dev_setting_t *)data;
19466             bxe_get_settings(sc, &dev_set);
19467             dev_p->supported = dev_set.supported;
19468             dev_p->advertising = dev_set.advertising;
19469             dev_p->speed = dev_set.speed;
19470             dev_p->duplex = dev_set.duplex;
19471             dev_p->port = dev_set.port;
19472             dev_p->phy_address = dev_set.phy_address;
19473             dev_p->autoneg = dev_set.autoneg;
19474
19475             break;
19476
19477         case BXE_GET_REGS:
19478
19479             reg_p = (bxe_get_regs_t *)data;
19480             grc_dump_size = reg_p->reg_buf_len;
19481
19482             if((!sc->grcdump_done) && (!sc->grcdump_started)) {
19483                 bxe_grc_dump(sc);
19484             }
19485             if((sc->grcdump_done) && (sc->grcdump_started) &&
19486                 (sc->grc_dump != NULL))  {
19487                 rval = copyout(sc->grc_dump, reg_p->reg_buf, grc_dump_size);
19488                 free(sc->grc_dump, M_DEVBUF);
19489                 sc->grc_dump = NULL;
19490                 sc->grcdump_started = 0;
19491                 sc->grcdump_done = 0;
19492             }
19493
19494             break;
19495
19496         case BXE_RDW_REG:
19497             reg_rdw_p = (bxe_reg_rdw_t *)data;
19498             if((reg_rdw_p->reg_cmd == BXE_READ_REG_CMD) &&
19499                 (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19500                 reg_rdw_p->reg_val = REG_RD(sc, reg_rdw_p->reg_id);
19501
19502             if((reg_rdw_p->reg_cmd == BXE_WRITE_REG_CMD) &&
19503                 (reg_rdw_p->reg_access_type == BXE_REG_ACCESS_DIRECT))
19504                 REG_WR(sc, reg_rdw_p->reg_id, reg_rdw_p->reg_val);
19505
19506             break;
19507
19508         case BXE_RDW_PCICFG:
19509             cfg_rdw_p = (bxe_pcicfg_rdw_t *)data;
19510             if(cfg_rdw_p->cfg_cmd == BXE_READ_PCICFG) {
19511
19512                 cfg_rdw_p->cfg_val = pci_read_config(sc->dev, cfg_rdw_p->cfg_id,
19513                                          cfg_rdw_p->cfg_width);
19514
19515             } else if(cfg_rdw_p->cfg_cmd == BXE_WRITE_PCICFG) {
19516                 pci_write_config(sc->dev, cfg_rdw_p->cfg_id, cfg_rdw_p->cfg_val,
19517                             cfg_rdw_p->cfg_width);
19518             } else {
19519                 BLOGW(sc, "BXE_RDW_PCICFG ioctl wrong cmd passed\n");
19520             }
19521             break;
19522
19523         case BXE_MAC_ADDR:
19524             mac_addr_p = (bxe_perm_mac_addr_t *)data;
19525             snprintf(mac_addr_p->mac_addr_str, sizeof(sc->mac_addr_str), "%s",
19526                 sc->mac_addr_str);
19527             break;
19528
19529         case BXE_EEPROM:
19530             rval = bxe_eeprom_rd_wr(sc, (bxe_eeprom_t *)data);
19531             break;
19532
19533
19534         default:
19535             break;
19536     }
19537
19538     return (rval);
19539 }