]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/bxe/bxe.c
MFC r284739
[FreeBSD/stable/8.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.79"
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 #if 0
128     {
129         BRCM_VENDORID,
130         CHIP_NUM_57712_VF,
131         PCI_ANY_ID, PCI_ANY_ID,
132         "QLogic NetXtreme II BCM57712 VF 10GbE"
133     },
134 #endif
135     {
136         BRCM_VENDORID,
137         CHIP_NUM_57800,
138         PCI_ANY_ID, PCI_ANY_ID,
139         "QLogic NetXtreme II BCM57800 10GbE"
140     },
141     {
142         BRCM_VENDORID,
143         CHIP_NUM_57800_MF,
144         PCI_ANY_ID, PCI_ANY_ID,
145         "QLogic NetXtreme II BCM57800 MF 10GbE"
146     },
147 #if 0
148     {
149         BRCM_VENDORID,
150         CHIP_NUM_57800_VF,
151         PCI_ANY_ID, PCI_ANY_ID,
152         "QLogic NetXtreme II BCM57800 VF 10GbE"
153     },
154 #endif
155     {
156         BRCM_VENDORID,
157         CHIP_NUM_57810,
158         PCI_ANY_ID, PCI_ANY_ID,
159         "QLogic NetXtreme II BCM57810 10GbE"
160     },
161     {
162         BRCM_VENDORID,
163         CHIP_NUM_57810_MF,
164         PCI_ANY_ID, PCI_ANY_ID,
165         "QLogic NetXtreme II BCM57810 MF 10GbE"
166     },
167 #if 0
168     {
169         BRCM_VENDORID,
170         CHIP_NUM_57810_VF,
171         PCI_ANY_ID, PCI_ANY_ID,
172         "QLogic NetXtreme II BCM57810 VF 10GbE"
173     },
174 #endif
175     {
176         BRCM_VENDORID,
177         CHIP_NUM_57811,
178         PCI_ANY_ID, PCI_ANY_ID,
179         "QLogic NetXtreme II BCM57811 10GbE"
180     },
181     {
182         BRCM_VENDORID,
183         CHIP_NUM_57811_MF,
184         PCI_ANY_ID, PCI_ANY_ID,
185         "QLogic NetXtreme II BCM57811 MF 10GbE"
186     },
187 #if 0
188     {
189         BRCM_VENDORID,
190         CHIP_NUM_57811_VF,
191         PCI_ANY_ID, PCI_ANY_ID,
192         "QLogic NetXtreme II BCM57811 VF 10GbE"
193     },
194 #endif
195     {
196         BRCM_VENDORID,
197         CHIP_NUM_57840_4_10,
198         PCI_ANY_ID, PCI_ANY_ID,
199         "QLogic NetXtreme II BCM57840 4x10GbE"
200     },
201 #if 0
202     {
203         BRCM_VENDORID,
204         CHIP_NUM_57840_2_20,
205         PCI_ANY_ID, PCI_ANY_ID,
206         "QLogic NetXtreme II BCM57840 2x20GbE"
207     },
208 #endif
209     {
210         BRCM_VENDORID,
211         CHIP_NUM_57840_MF,
212         PCI_ANY_ID, PCI_ANY_ID,
213         "QLogic NetXtreme II BCM57840 MF 10GbE"
214     },
215 #if 0
216     {
217         BRCM_VENDORID,
218         CHIP_NUM_57840_VF,
219         PCI_ANY_ID, PCI_ANY_ID,
220         "QLogic NetXtreme II BCM57840 VF 10GbE"
221     },
222 #endif
223     {
224         0, 0, 0, 0, NULL
225     }
226 };
227
228 MALLOC_DECLARE(M_BXE_ILT);
229 MALLOC_DEFINE(M_BXE_ILT, "bxe_ilt", "bxe ILT pointer");
230
231 /*
232  * FreeBSD device entry points.
233  */
234 static int bxe_probe(device_t);
235 static int bxe_attach(device_t);
236 static int bxe_detach(device_t);
237 static int bxe_shutdown(device_t);
238
239 /*
240  * FreeBSD KLD module/device interface event handler method.
241  */
242 static device_method_t bxe_methods[] = {
243     /* Device interface (device_if.h) */
244     DEVMETHOD(device_probe,     bxe_probe),
245     DEVMETHOD(device_attach,    bxe_attach),
246     DEVMETHOD(device_detach,    bxe_detach),
247     DEVMETHOD(device_shutdown,  bxe_shutdown),
248 #if 0
249     DEVMETHOD(device_suspend,   bxe_suspend),
250     DEVMETHOD(device_resume,    bxe_resume),
251 #endif
252     /* Bus interface (bus_if.h) */
253     DEVMETHOD(bus_print_child,  bus_generic_print_child),
254     DEVMETHOD(bus_driver_added, bus_generic_driver_added),
255     KOBJMETHOD_END
256 };
257
258 /*
259  * FreeBSD KLD Module data declaration
260  */
261 static driver_t bxe_driver = {
262     "bxe",                   /* module name */
263     bxe_methods,             /* event handler */
264     sizeof(struct bxe_softc) /* extra data */
265 };
266
267 /*
268  * FreeBSD dev class is needed to manage dev instances and
269  * to associate with a bus type
270  */
271 static devclass_t bxe_devclass;
272
273 MODULE_DEPEND(bxe, pci, 1, 1, 1);
274 MODULE_DEPEND(bxe, ether, 1, 1, 1);
275 DRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
276
277 /* resources needed for unloading a previously loaded device */
278
279 #define BXE_PREV_WAIT_NEEDED 1
280 struct mtx bxe_prev_mtx;
281 MTX_SYSINIT(bxe_prev_mtx, &bxe_prev_mtx, "bxe_prev_lock", MTX_DEF);
282 struct bxe_prev_list_node {
283     LIST_ENTRY(bxe_prev_list_node) node;
284     uint8_t bus;
285     uint8_t slot;
286     uint8_t path;
287     uint8_t aer; /* XXX automatic error recovery */
288     uint8_t undi;
289 };
290 static LIST_HEAD(, bxe_prev_list_node) bxe_prev_list = LIST_HEAD_INITIALIZER(bxe_prev_list);
291
292 static int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
293
294 /* Tunable device values... */
295
296 SYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
297
298 /* Debug */
299 unsigned long bxe_debug = 0;
300 TUNABLE_ULONG("hw.bxe.debug", &bxe_debug);
301 SYSCTL_ULONG(_hw_bxe, OID_AUTO, debug, (CTLFLAG_RDTUN),
302              &bxe_debug, 0, "Debug logging mode");
303
304 /* Interrupt Mode: 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
305 static int bxe_interrupt_mode = INTR_MODE_MSIX;
306 TUNABLE_INT("hw.bxe.interrupt_mode", &bxe_interrupt_mode);
307 SYSCTL_INT(_hw_bxe, OID_AUTO, interrupt_mode, CTLFLAG_RDTUN,
308            &bxe_interrupt_mode, 0, "Interrupt (MSI-X/MSI/INTx) mode");
309
310 /* Number of Queues: 0 (Auto) or 1 to 16 (fixed queue number) */
311 static int bxe_queue_count = 4;
312 TUNABLE_INT("hw.bxe.queue_count", &bxe_queue_count);
313 SYSCTL_INT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN,
314            &bxe_queue_count, 0, "Multi-Queue queue count");
315
316 /* max number of buffers per queue (default RX_BD_USABLE) */
317 static int bxe_max_rx_bufs = 0;
318 TUNABLE_INT("hw.bxe.max_rx_bufs", &bxe_max_rx_bufs);
319 SYSCTL_INT(_hw_bxe, OID_AUTO, max_rx_bufs, CTLFLAG_RDTUN,
320            &bxe_max_rx_bufs, 0, "Maximum Number of Rx Buffers Per Queue");
321
322 /* Host interrupt coalescing RX tick timer (usecs) */
323 static int bxe_hc_rx_ticks = 25;
324 TUNABLE_INT("hw.bxe.hc_rx_ticks", &bxe_hc_rx_ticks);
325 SYSCTL_INT(_hw_bxe, OID_AUTO, hc_rx_ticks, CTLFLAG_RDTUN,
326            &bxe_hc_rx_ticks, 0, "Host Coalescing Rx ticks");
327
328 /* Host interrupt coalescing TX tick timer (usecs) */
329 static int bxe_hc_tx_ticks = 50;
330 TUNABLE_INT("hw.bxe.hc_tx_ticks", &bxe_hc_tx_ticks);
331 SYSCTL_INT(_hw_bxe, OID_AUTO, hc_tx_ticks, CTLFLAG_RDTUN,
332            &bxe_hc_tx_ticks, 0, "Host Coalescing Tx ticks");
333
334 /* Maximum number of Rx packets to process at a time */
335 static int bxe_rx_budget = 0xffffffff;
336 TUNABLE_INT("hw.bxe.rx_budget", &bxe_rx_budget);
337 SYSCTL_INT(_hw_bxe, OID_AUTO, rx_budget, CTLFLAG_TUN,
338            &bxe_rx_budget, 0, "Rx processing budget");
339
340 /* Maximum LRO aggregation size */
341 static int bxe_max_aggregation_size = 0;
342 TUNABLE_INT("hw.bxe.max_aggregation_size", &bxe_max_aggregation_size);
343 SYSCTL_INT(_hw_bxe, OID_AUTO, max_aggregation_size, CTLFLAG_TUN,
344            &bxe_max_aggregation_size, 0, "max aggregation size");
345
346 /* PCI MRRS: -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB) */
347 static int bxe_mrrs = -1;
348 TUNABLE_INT("hw.bxe.mrrs", &bxe_mrrs);
349 SYSCTL_INT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN,
350            &bxe_mrrs, 0, "PCIe maximum read request size");
351
352 /* AutoGrEEEn: 0 (hardware default), 1 (force on), 2 (force off) */
353 static int bxe_autogreeen = 0;
354 TUNABLE_INT("hw.bxe.autogreeen", &bxe_autogreeen);
355 SYSCTL_INT(_hw_bxe, OID_AUTO, autogreeen, CTLFLAG_RDTUN,
356            &bxe_autogreeen, 0, "AutoGrEEEn support");
357
358 /* 4-tuple RSS support for UDP: 0 (disabled), 1 (enabled) */
359 static int bxe_udp_rss = 0;
360 TUNABLE_INT("hw.bxe.udp_rss", &bxe_udp_rss);
361 SYSCTL_INT(_hw_bxe, OID_AUTO, udp_rss, CTLFLAG_RDTUN,
362            &bxe_udp_rss, 0, "UDP RSS support");
363
364
365 #define STAT_NAME_LEN 32 /* no stat names below can be longer than this */
366
367 #define STATS_OFFSET32(stat_name)                   \
368     (offsetof(struct bxe_eth_stats, stat_name) / 4)
369
370 #define Q_STATS_OFFSET32(stat_name)                   \
371     (offsetof(struct bxe_eth_q_stats, stat_name) / 4)
372
373 static const struct {
374     uint32_t offset;
375     uint32_t size;
376     uint32_t flags;
377 #define STATS_FLAGS_PORT  1
378 #define STATS_FLAGS_FUNC  2 /* MF only cares about function stats */
379 #define STATS_FLAGS_BOTH  (STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
380     char string[STAT_NAME_LEN];
381 } bxe_eth_stats_arr[] = {
382     { STATS_OFFSET32(total_bytes_received_hi),
383                 8, STATS_FLAGS_BOTH, "rx_bytes" },
384     { STATS_OFFSET32(error_bytes_received_hi),
385                 8, STATS_FLAGS_BOTH, "rx_error_bytes" },
386     { STATS_OFFSET32(total_unicast_packets_received_hi),
387                 8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
388     { STATS_OFFSET32(total_multicast_packets_received_hi),
389                 8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
390     { STATS_OFFSET32(total_broadcast_packets_received_hi),
391                 8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
392     { STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
393                 8, STATS_FLAGS_PORT, "rx_crc_errors" },
394     { STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
395                 8, STATS_FLAGS_PORT, "rx_align_errors" },
396     { STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
397                 8, STATS_FLAGS_PORT, "rx_undersize_packets" },
398     { STATS_OFFSET32(etherstatsoverrsizepkts_hi),
399                 8, STATS_FLAGS_PORT, "rx_oversize_packets" },
400     { STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
401                 8, STATS_FLAGS_PORT, "rx_fragments" },
402     { STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
403                 8, STATS_FLAGS_PORT, "rx_jabbers" },
404     { STATS_OFFSET32(no_buff_discard_hi),
405                 8, STATS_FLAGS_BOTH, "rx_discards" },
406     { STATS_OFFSET32(mac_filter_discard),
407                 4, STATS_FLAGS_PORT, "rx_filtered_packets" },
408     { STATS_OFFSET32(mf_tag_discard),
409                 4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
410     { STATS_OFFSET32(pfc_frames_received_hi),
411                 8, STATS_FLAGS_PORT, "pfc_frames_received" },
412     { STATS_OFFSET32(pfc_frames_sent_hi),
413                 8, STATS_FLAGS_PORT, "pfc_frames_sent" },
414     { STATS_OFFSET32(brb_drop_hi),
415                 8, STATS_FLAGS_PORT, "rx_brb_discard" },
416     { STATS_OFFSET32(brb_truncate_hi),
417                 8, STATS_FLAGS_PORT, "rx_brb_truncate" },
418     { STATS_OFFSET32(pause_frames_received_hi),
419                 8, STATS_FLAGS_PORT, "rx_pause_frames" },
420     { STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
421                 8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
422     { STATS_OFFSET32(nig_timer_max),
423                 4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
424     { STATS_OFFSET32(total_bytes_transmitted_hi),
425                 8, STATS_FLAGS_BOTH, "tx_bytes" },
426     { STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
427                 8, STATS_FLAGS_PORT, "tx_error_bytes" },
428     { STATS_OFFSET32(total_unicast_packets_transmitted_hi),
429                 8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
430     { STATS_OFFSET32(total_multicast_packets_transmitted_hi),
431                 8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
432     { STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
433                 8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
434     { STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
435                 8, STATS_FLAGS_PORT, "tx_mac_errors" },
436     { STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
437                 8, STATS_FLAGS_PORT, "tx_carrier_errors" },
438     { STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
439                 8, STATS_FLAGS_PORT, "tx_single_collisions" },
440     { STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
441                 8, STATS_FLAGS_PORT, "tx_multi_collisions" },
442     { STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
443                 8, STATS_FLAGS_PORT, "tx_deferred" },
444     { STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
445                 8, STATS_FLAGS_PORT, "tx_excess_collisions" },
446     { STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
447                 8, STATS_FLAGS_PORT, "tx_late_collisions" },
448     { STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
449                 8, STATS_FLAGS_PORT, "tx_total_collisions" },
450     { STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
451                 8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
452     { STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
453                 8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
454     { STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
455                 8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
456     { STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
457                 8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
458     { STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
459                 8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
460     { STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
461                 8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
462     { STATS_OFFSET32(etherstatspktsover1522octets_hi),
463                 8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
464     { STATS_OFFSET32(pause_frames_sent_hi),
465                 8, STATS_FLAGS_PORT, "tx_pause_frames" },
466     { STATS_OFFSET32(total_tpa_aggregations_hi),
467                 8, STATS_FLAGS_FUNC, "tpa_aggregations" },
468     { STATS_OFFSET32(total_tpa_aggregated_frames_hi),
469                 8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
470     { STATS_OFFSET32(total_tpa_bytes_hi),
471                 8, STATS_FLAGS_FUNC, "tpa_bytes"},
472 #if 0
473     { STATS_OFFSET32(recoverable_error),
474                 4, STATS_FLAGS_FUNC, "recoverable_errors" },
475     { STATS_OFFSET32(unrecoverable_error),
476                 4, STATS_FLAGS_FUNC, "unrecoverable_errors" },
477 #endif
478     { STATS_OFFSET32(eee_tx_lpi),
479                 4, STATS_FLAGS_PORT, "eee_tx_lpi"},
480     { STATS_OFFSET32(rx_calls),
481                 4, STATS_FLAGS_FUNC, "rx_calls"},
482     { STATS_OFFSET32(rx_pkts),
483                 4, STATS_FLAGS_FUNC, "rx_pkts"},
484     { STATS_OFFSET32(rx_tpa_pkts),
485                 4, STATS_FLAGS_FUNC, "rx_tpa_pkts"},
486     { STATS_OFFSET32(rx_jumbo_sge_pkts),
487                 4, STATS_FLAGS_FUNC, "rx_jumbo_sge_pkts"},
488     { STATS_OFFSET32(rx_soft_errors),
489                 4, STATS_FLAGS_FUNC, "rx_soft_errors"},
490     { STATS_OFFSET32(rx_hw_csum_errors),
491                 4, STATS_FLAGS_FUNC, "rx_hw_csum_errors"},
492     { STATS_OFFSET32(rx_ofld_frames_csum_ip),
493                 4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_ip"},
494     { STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
495                 4, STATS_FLAGS_FUNC, "rx_ofld_frames_csum_tcp_udp"},
496     { STATS_OFFSET32(rx_budget_reached),
497                 4, STATS_FLAGS_FUNC, "rx_budget_reached"},
498     { STATS_OFFSET32(tx_pkts),
499                 4, STATS_FLAGS_FUNC, "tx_pkts"},
500     { STATS_OFFSET32(tx_soft_errors),
501                 4, STATS_FLAGS_FUNC, "tx_soft_errors"},
502     { STATS_OFFSET32(tx_ofld_frames_csum_ip),
503                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_ip"},
504     { STATS_OFFSET32(tx_ofld_frames_csum_tcp),
505                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_tcp"},
506     { STATS_OFFSET32(tx_ofld_frames_csum_udp),
507                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_csum_udp"},
508     { STATS_OFFSET32(tx_ofld_frames_lso),
509                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso"},
510     { STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
511                 4, STATS_FLAGS_FUNC, "tx_ofld_frames_lso_hdr_splits"},
512     { STATS_OFFSET32(tx_encap_failures),
513                 4, STATS_FLAGS_FUNC, "tx_encap_failures"},
514     { STATS_OFFSET32(tx_hw_queue_full),
515                 4, STATS_FLAGS_FUNC, "tx_hw_queue_full"},
516     { STATS_OFFSET32(tx_hw_max_queue_depth),
517                 4, STATS_FLAGS_FUNC, "tx_hw_max_queue_depth"},
518     { STATS_OFFSET32(tx_dma_mapping_failure),
519                 4, STATS_FLAGS_FUNC, "tx_dma_mapping_failure"},
520     { STATS_OFFSET32(tx_max_drbr_queue_depth),
521                 4, STATS_FLAGS_FUNC, "tx_max_drbr_queue_depth"},
522     { STATS_OFFSET32(tx_window_violation_std),
523                 4, STATS_FLAGS_FUNC, "tx_window_violation_std"},
524     { STATS_OFFSET32(tx_window_violation_tso),
525                 4, STATS_FLAGS_FUNC, "tx_window_violation_tso"},
526 #if 0
527     { STATS_OFFSET32(tx_unsupported_tso_request_ipv6),
528                 4, STATS_FLAGS_FUNC, "tx_unsupported_tso_request_ipv6"},
529     { STATS_OFFSET32(tx_unsupported_tso_request_not_tcp),
530                 4, STATS_FLAGS_FUNC, "tx_unsupported_tso_request_not_tcp"},
531 #endif
532     { STATS_OFFSET32(tx_chain_lost_mbuf),
533                 4, STATS_FLAGS_FUNC, "tx_chain_lost_mbuf"},
534     { STATS_OFFSET32(tx_frames_deferred),
535                 4, STATS_FLAGS_FUNC, "tx_frames_deferred"},
536     { STATS_OFFSET32(tx_queue_xoff),
537                 4, STATS_FLAGS_FUNC, "tx_queue_xoff"},
538     { STATS_OFFSET32(mbuf_defrag_attempts),
539                 4, STATS_FLAGS_FUNC, "mbuf_defrag_attempts"},
540     { STATS_OFFSET32(mbuf_defrag_failures),
541                 4, STATS_FLAGS_FUNC, "mbuf_defrag_failures"},
542     { STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
543                 4, STATS_FLAGS_FUNC, "mbuf_rx_bd_alloc_failed"},
544     { STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
545                 4, STATS_FLAGS_FUNC, "mbuf_rx_bd_mapping_failed"},
546     { STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
547                 4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_alloc_failed"},
548     { STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
549                 4, STATS_FLAGS_FUNC, "mbuf_rx_tpa_mapping_failed"},
550     { STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
551                 4, STATS_FLAGS_FUNC, "mbuf_rx_sge_alloc_failed"},
552     { STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
553                 4, STATS_FLAGS_FUNC, "mbuf_rx_sge_mapping_failed"},
554     { STATS_OFFSET32(mbuf_alloc_tx),
555                 4, STATS_FLAGS_FUNC, "mbuf_alloc_tx"},
556     { STATS_OFFSET32(mbuf_alloc_rx),
557                 4, STATS_FLAGS_FUNC, "mbuf_alloc_rx"},
558     { STATS_OFFSET32(mbuf_alloc_sge),
559                 4, STATS_FLAGS_FUNC, "mbuf_alloc_sge"},
560     { STATS_OFFSET32(mbuf_alloc_tpa),
561                 4, STATS_FLAGS_FUNC, "mbuf_alloc_tpa"}
562 };
563
564 static const struct {
565     uint32_t offset;
566     uint32_t size;
567     char string[STAT_NAME_LEN];
568 } bxe_eth_q_stats_arr[] = {
569     { Q_STATS_OFFSET32(total_bytes_received_hi),
570                 8, "rx_bytes" },
571     { Q_STATS_OFFSET32(total_unicast_packets_received_hi),
572                 8, "rx_ucast_packets" },
573     { Q_STATS_OFFSET32(total_multicast_packets_received_hi),
574                 8, "rx_mcast_packets" },
575     { Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
576                 8, "rx_bcast_packets" },
577     { Q_STATS_OFFSET32(no_buff_discard_hi),
578                 8, "rx_discards" },
579     { Q_STATS_OFFSET32(total_bytes_transmitted_hi),
580                 8, "tx_bytes" },
581     { Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
582                 8, "tx_ucast_packets" },
583     { Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
584                 8, "tx_mcast_packets" },
585     { Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
586                 8, "tx_bcast_packets" },
587     { Q_STATS_OFFSET32(total_tpa_aggregations_hi),
588                 8, "tpa_aggregations" },
589     { Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
590                 8, "tpa_aggregated_frames"},
591     { Q_STATS_OFFSET32(total_tpa_bytes_hi),
592                 8, "tpa_bytes"},
593     { Q_STATS_OFFSET32(rx_calls),
594                 4, "rx_calls"},
595     { Q_STATS_OFFSET32(rx_pkts),
596                 4, "rx_pkts"},
597     { Q_STATS_OFFSET32(rx_tpa_pkts),
598                 4, "rx_tpa_pkts"},
599     { Q_STATS_OFFSET32(rx_jumbo_sge_pkts),
600                 4, "rx_jumbo_sge_pkts"},
601     { Q_STATS_OFFSET32(rx_soft_errors),
602                 4, "rx_soft_errors"},
603     { Q_STATS_OFFSET32(rx_hw_csum_errors),
604                 4, "rx_hw_csum_errors"},
605     { Q_STATS_OFFSET32(rx_ofld_frames_csum_ip),
606                 4, "rx_ofld_frames_csum_ip"},
607     { Q_STATS_OFFSET32(rx_ofld_frames_csum_tcp_udp),
608                 4, "rx_ofld_frames_csum_tcp_udp"},
609     { Q_STATS_OFFSET32(rx_budget_reached),
610                 4, "rx_budget_reached"},
611     { Q_STATS_OFFSET32(tx_pkts),
612                 4, "tx_pkts"},
613     { Q_STATS_OFFSET32(tx_soft_errors),
614                 4, "tx_soft_errors"},
615     { Q_STATS_OFFSET32(tx_ofld_frames_csum_ip),
616                 4, "tx_ofld_frames_csum_ip"},
617     { Q_STATS_OFFSET32(tx_ofld_frames_csum_tcp),
618                 4, "tx_ofld_frames_csum_tcp"},
619     { Q_STATS_OFFSET32(tx_ofld_frames_csum_udp),
620                 4, "tx_ofld_frames_csum_udp"},
621     { Q_STATS_OFFSET32(tx_ofld_frames_lso),
622                 4, "tx_ofld_frames_lso"},
623     { Q_STATS_OFFSET32(tx_ofld_frames_lso_hdr_splits),
624                 4, "tx_ofld_frames_lso_hdr_splits"},
625     { Q_STATS_OFFSET32(tx_encap_failures),
626                 4, "tx_encap_failures"},
627     { Q_STATS_OFFSET32(tx_hw_queue_full),
628                 4, "tx_hw_queue_full"},
629     { Q_STATS_OFFSET32(tx_hw_max_queue_depth),
630                 4, "tx_hw_max_queue_depth"},
631     { Q_STATS_OFFSET32(tx_dma_mapping_failure),
632                 4, "tx_dma_mapping_failure"},
633     { Q_STATS_OFFSET32(tx_max_drbr_queue_depth),
634                 4, "tx_max_drbr_queue_depth"},
635     { Q_STATS_OFFSET32(tx_window_violation_std),
636                 4, "tx_window_violation_std"},
637     { Q_STATS_OFFSET32(tx_window_violation_tso),
638                 4, "tx_window_violation_tso"},
639 #if 0
640     { Q_STATS_OFFSET32(tx_unsupported_tso_request_ipv6),
641                 4, "tx_unsupported_tso_request_ipv6"},
642     { Q_STATS_OFFSET32(tx_unsupported_tso_request_not_tcp),
643                 4, "tx_unsupported_tso_request_not_tcp"},
644 #endif
645     { Q_STATS_OFFSET32(tx_chain_lost_mbuf),
646                 4, "tx_chain_lost_mbuf"},
647     { Q_STATS_OFFSET32(tx_frames_deferred),
648                 4, "tx_frames_deferred"},
649     { Q_STATS_OFFSET32(tx_queue_xoff),
650                 4, "tx_queue_xoff"},
651     { Q_STATS_OFFSET32(mbuf_defrag_attempts),
652                 4, "mbuf_defrag_attempts"},
653     { Q_STATS_OFFSET32(mbuf_defrag_failures),
654                 4, "mbuf_defrag_failures"},
655     { Q_STATS_OFFSET32(mbuf_rx_bd_alloc_failed),
656                 4, "mbuf_rx_bd_alloc_failed"},
657     { Q_STATS_OFFSET32(mbuf_rx_bd_mapping_failed),
658                 4, "mbuf_rx_bd_mapping_failed"},
659     { Q_STATS_OFFSET32(mbuf_rx_tpa_alloc_failed),
660                 4, "mbuf_rx_tpa_alloc_failed"},
661     { Q_STATS_OFFSET32(mbuf_rx_tpa_mapping_failed),
662                 4, "mbuf_rx_tpa_mapping_failed"},
663     { Q_STATS_OFFSET32(mbuf_rx_sge_alloc_failed),
664                 4, "mbuf_rx_sge_alloc_failed"},
665     { Q_STATS_OFFSET32(mbuf_rx_sge_mapping_failed),
666                 4, "mbuf_rx_sge_mapping_failed"},
667     { Q_STATS_OFFSET32(mbuf_alloc_tx),
668                 4, "mbuf_alloc_tx"},
669     { Q_STATS_OFFSET32(mbuf_alloc_rx),
670                 4, "mbuf_alloc_rx"},
671     { Q_STATS_OFFSET32(mbuf_alloc_sge),
672                 4, "mbuf_alloc_sge"},
673     { Q_STATS_OFFSET32(mbuf_alloc_tpa),
674                 4, "mbuf_alloc_tpa"}
675 };
676
677 #define BXE_NUM_ETH_STATS   ARRAY_SIZE(bxe_eth_stats_arr)
678 #define BXE_NUM_ETH_Q_STATS ARRAY_SIZE(bxe_eth_q_stats_arr)
679
680
681 static void    bxe_cmng_fns_init(struct bxe_softc *sc,
682                                  uint8_t          read_cfg,
683                                  uint8_t          cmng_type);
684 static int     bxe_get_cmng_fns_mode(struct bxe_softc *sc);
685 static void    storm_memset_cmng(struct bxe_softc *sc,
686                                  struct cmng_init *cmng,
687                                  uint8_t          port);
688 static void    bxe_set_reset_global(struct bxe_softc *sc);
689 static void    bxe_set_reset_in_progress(struct bxe_softc *sc);
690 static uint8_t bxe_reset_is_done(struct bxe_softc *sc,
691                                  int              engine);
692 static uint8_t bxe_clear_pf_load(struct bxe_softc *sc);
693 static uint8_t bxe_chk_parity_attn(struct bxe_softc *sc,
694                                    uint8_t          *global,
695                                    uint8_t          print);
696 static void    bxe_int_disable(struct bxe_softc *sc);
697 static int     bxe_release_leader_lock(struct bxe_softc *sc);
698 static void    bxe_pf_disable(struct bxe_softc *sc);
699 static void    bxe_free_fp_buffers(struct bxe_softc *sc);
700 static inline void bxe_update_rx_prod(struct bxe_softc    *sc,
701                                       struct bxe_fastpath *fp,
702                                       uint16_t            rx_bd_prod,
703                                       uint16_t            rx_cq_prod,
704                                       uint16_t            rx_sge_prod);
705 static void    bxe_link_report_locked(struct bxe_softc *sc);
706 static void    bxe_link_report(struct bxe_softc *sc);
707 static void    bxe_link_status_update(struct bxe_softc *sc);
708 static void    bxe_periodic_callout_func(void *xsc);
709 static void    bxe_periodic_start(struct bxe_softc *sc);
710 static void    bxe_periodic_stop(struct bxe_softc *sc);
711 static int     bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
712                                     uint16_t prev_index,
713                                     uint16_t index);
714 static int     bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
715                                      int                 queue);
716 static int     bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
717                                      uint16_t            index);
718 static uint8_t bxe_txeof(struct bxe_softc *sc,
719                          struct bxe_fastpath *fp);
720 static void    bxe_task_fp(struct bxe_fastpath *fp);
721 static __noinline void bxe_dump_mbuf(struct bxe_softc *sc,
722                                      struct mbuf      *m,
723                                      uint8_t          contents);
724 static int     bxe_alloc_mem(struct bxe_softc *sc);
725 static void    bxe_free_mem(struct bxe_softc *sc);
726 static int     bxe_alloc_fw_stats_mem(struct bxe_softc *sc);
727 static void    bxe_free_fw_stats_mem(struct bxe_softc *sc);
728 static int     bxe_interrupt_attach(struct bxe_softc *sc);
729 static void    bxe_interrupt_detach(struct bxe_softc *sc);
730 static void    bxe_set_rx_mode(struct bxe_softc *sc);
731 static int     bxe_init_locked(struct bxe_softc *sc);
732 static int     bxe_stop_locked(struct bxe_softc *sc);
733 static __noinline int bxe_nic_load(struct bxe_softc *sc,
734                                    int              load_mode);
735 static __noinline int bxe_nic_unload(struct bxe_softc *sc,
736                                      uint32_t         unload_mode,
737                                      uint8_t          keep_link);
738
739 static void bxe_handle_sp_tq(void *context, int pending);
740 static void bxe_handle_fp_tq(void *context, int pending);
741
742
743 /* calculate crc32 on a buffer (NOTE: crc32_length MUST be aligned to 8) */
744 uint32_t
745 calc_crc32(uint8_t  *crc32_packet,
746            uint32_t crc32_length,
747            uint32_t crc32_seed,
748            uint8_t  complement)
749 {
750    uint32_t byte         = 0;
751    uint32_t bit          = 0;
752    uint8_t  msb          = 0;
753    uint32_t temp         = 0;
754    uint32_t shft         = 0;
755    uint8_t  current_byte = 0;
756    uint32_t crc32_result = crc32_seed;
757    const uint32_t CRC32_POLY = 0x1edc6f41;
758
759    if ((crc32_packet == NULL) ||
760        (crc32_length == 0) ||
761        ((crc32_length % 8) != 0))
762     {
763         return (crc32_result);
764     }
765
766     for (byte = 0; byte < crc32_length; byte = byte + 1)
767     {
768         current_byte = crc32_packet[byte];
769         for (bit = 0; bit < 8; bit = bit + 1)
770         {
771             /* msb = crc32_result[31]; */
772             msb = (uint8_t)(crc32_result >> 31);
773
774             crc32_result = crc32_result << 1;
775
776             /* it (msb != current_byte[bit]) */
777             if (msb != (0x1 & (current_byte >> bit)))
778             {
779                 crc32_result = crc32_result ^ CRC32_POLY;
780                 /* crc32_result[0] = 1 */
781                 crc32_result |= 1;
782             }
783         }
784     }
785
786     /* Last step is to:
787      * 1. "mirror" every bit
788      * 2. swap the 4 bytes
789      * 3. complement each bit
790      */
791
792     /* Mirror */
793     temp = crc32_result;
794     shft = sizeof(crc32_result) * 8 - 1;
795
796     for (crc32_result >>= 1; crc32_result; crc32_result >>= 1)
797     {
798         temp <<= 1;
799         temp |= crc32_result & 1;
800         shft-- ;
801     }
802
803     /* temp[31-bit] = crc32_result[bit] */
804     temp <<= shft;
805
806     /* Swap */
807     /* crc32_result = {temp[7:0], temp[15:8], temp[23:16], temp[31:24]} */
808     {
809         uint32_t t0, t1, t2, t3;
810         t0 = (0x000000ff & (temp >> 24));
811         t1 = (0x0000ff00 & (temp >> 8));
812         t2 = (0x00ff0000 & (temp << 8));
813         t3 = (0xff000000 & (temp << 24));
814         crc32_result = t0 | t1 | t2 | t3;
815     }
816
817     /* Complement */
818     if (complement)
819     {
820         crc32_result = ~crc32_result;
821     }
822
823     return (crc32_result);
824 }
825
826 int
827 bxe_test_bit(int                    nr,
828              volatile unsigned long *addr)
829 {
830     return ((atomic_load_acq_long(addr) & (1 << nr)) != 0);
831 }
832
833 void
834 bxe_set_bit(unsigned int           nr,
835             volatile unsigned long *addr)
836 {
837     atomic_set_acq_long(addr, (1 << nr));
838 }
839
840 void
841 bxe_clear_bit(int                    nr,
842               volatile unsigned long *addr)
843 {
844     atomic_clear_acq_long(addr, (1 << nr));
845 }
846
847 int
848 bxe_test_and_set_bit(int                    nr,
849                        volatile unsigned long *addr)
850 {
851     unsigned long x;
852     nr = (1 << nr);
853     do {
854         x = *addr;
855     } while (atomic_cmpset_acq_long(addr, x, x | nr) == 0);
856     // if (x & nr) bit_was_set; else bit_was_not_set;
857     return (x & nr);
858 }
859
860 int
861 bxe_test_and_clear_bit(int                    nr,
862                        volatile unsigned long *addr)
863 {
864     unsigned long x;
865     nr = (1 << nr);
866     do {
867         x = *addr;
868     } while (atomic_cmpset_acq_long(addr, x, x & ~nr) == 0);
869     // if (x & nr) bit_was_set; else bit_was_not_set;
870     return (x & nr);
871 }
872
873 int
874 bxe_cmpxchg(volatile int *addr,
875             int          old,
876             int          new)
877 {
878     int x;
879     do {
880         x = *addr;
881     } while (atomic_cmpset_acq_int(addr, old, new) == 0);
882     return (x);
883 }
884
885 /*
886  * Get DMA memory from the OS.
887  *
888  * Validates that the OS has provided DMA buffers in response to a
889  * bus_dmamap_load call and saves the physical address of those buffers.
890  * When the callback is used the OS will return 0 for the mapping function
891  * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
892  * failures back to the caller.
893  *
894  * Returns:
895  *   Nothing.
896  */
897 static void
898 bxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
899 {
900     struct bxe_dma *dma = arg;
901
902     if (error) {
903         dma->paddr = 0;
904         dma->nseg  = 0;
905         BLOGE(dma->sc, "Failed DMA alloc '%s' (%d)!\n", dma->msg, error);
906     } else {
907         dma->paddr = segs->ds_addr;
908         dma->nseg  = nseg;
909 #if 0
910         BLOGD(dma->sc, DBG_LOAD,
911               "DMA alloc '%s': vaddr=%p paddr=%p nseg=%d size=%lu\n",
912               dma->msg, dma->vaddr, (void *)dma->paddr,
913               dma->nseg, dma->size);
914 #endif
915     }
916 }
917
918 /*
919  * Allocate a block of memory and map it for DMA. No partial completions
920  * allowed and release any resources acquired if we can't acquire all
921  * resources.
922  *
923  * Returns:
924  *   0 = Success, !0 = Failure
925  */
926 int
927 bxe_dma_alloc(struct bxe_softc *sc,
928               bus_size_t       size,
929               struct bxe_dma   *dma,
930               const char       *msg)
931 {
932     int rc;
933
934     if (dma->size > 0) {
935         BLOGE(sc, "dma block '%s' already has size %lu\n", msg,
936               (unsigned long)dma->size);
937         return (1);
938     }
939
940     memset(dma, 0, sizeof(*dma)); /* sanity */
941     dma->sc   = sc;
942     dma->size = size;
943     snprintf(dma->msg, sizeof(dma->msg), "%s", msg);
944
945     rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
946                             BCM_PAGE_SIZE,      /* alignment */
947                             0,                  /* boundary limit */
948                             BUS_SPACE_MAXADDR,  /* restricted low */
949                             BUS_SPACE_MAXADDR,  /* restricted hi */
950                             NULL,               /* addr filter() */
951                             NULL,               /* addr filter() arg */
952                             size,               /* max map size */
953                             1,                  /* num discontinuous */
954                             size,               /* max seg size */
955                             BUS_DMA_ALLOCNOW,   /* flags */
956                             NULL,               /* lock() */
957                             NULL,               /* lock() arg */
958                             &dma->tag);         /* returned dma tag */
959     if (rc != 0) {
960         BLOGE(sc, "Failed to create dma tag for '%s' (%d)\n", msg, rc);
961         memset(dma, 0, sizeof(*dma));
962         return (1);
963     }
964
965     rc = bus_dmamem_alloc(dma->tag,
966                           (void **)&dma->vaddr,
967                           (BUS_DMA_NOWAIT | BUS_DMA_ZERO),
968                           &dma->map);
969     if (rc != 0) {
970         BLOGE(sc, "Failed to alloc dma mem for '%s' (%d)\n", msg, rc);
971         bus_dma_tag_destroy(dma->tag);
972         memset(dma, 0, sizeof(*dma));
973         return (1);
974     }
975
976     rc = bus_dmamap_load(dma->tag,
977                          dma->map,
978                          dma->vaddr,
979                          size,
980                          bxe_dma_map_addr, /* BLOGD in here */
981                          dma,
982                          BUS_DMA_NOWAIT);
983     if (rc != 0) {
984         BLOGE(sc, "Failed to load dma map for '%s' (%d)\n", msg, rc);
985         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
986         bus_dma_tag_destroy(dma->tag);
987         memset(dma, 0, sizeof(*dma));
988         return (1);
989     }
990
991     return (0);
992 }
993
994 void
995 bxe_dma_free(struct bxe_softc *sc,
996              struct bxe_dma   *dma)
997 {
998     if (dma->size > 0) {
999 #if 0
1000         BLOGD(sc, DBG_LOAD,
1001               "DMA free '%s': vaddr=%p paddr=%p nseg=%d size=%lu\n",
1002               dma->msg, dma->vaddr, (void *)dma->paddr,
1003               dma->nseg, dma->size);
1004 #endif
1005
1006         DBASSERT(sc, (dma->tag != NULL), ("dma tag is NULL"));
1007
1008         bus_dmamap_sync(dma->tag, dma->map,
1009                         (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE));
1010         bus_dmamap_unload(dma->tag, dma->map);
1011         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
1012         bus_dma_tag_destroy(dma->tag);
1013     }
1014
1015     memset(dma, 0, sizeof(*dma));
1016 }
1017
1018 /*
1019  * These indirect read and write routines are only during init.
1020  * The locking is handled by the MCP.
1021  */
1022
1023 void
1024 bxe_reg_wr_ind(struct bxe_softc *sc,
1025                uint32_t         addr,
1026                uint32_t         val)
1027 {
1028     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
1029     pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
1030     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
1031 }
1032
1033 uint32_t
1034 bxe_reg_rd_ind(struct bxe_softc *sc,
1035                uint32_t         addr)
1036 {
1037     uint32_t val;
1038
1039     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, addr, 4);
1040     val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
1041     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
1042
1043     return (val);
1044 }
1045
1046 #if 0
1047 void bxe_dp_dmae(struct bxe_softc *sc, struct dmae_command *dmae, int msglvl)
1048 {
1049     uint32_t src_type = dmae->opcode & DMAE_COMMAND_SRC;
1050
1051     switch (dmae->opcode & DMAE_COMMAND_DST) {
1052     case DMAE_CMD_DST_PCI:
1053         if (src_type == DMAE_CMD_SRC_PCI)
1054             DP(msglvl, "DMAE: opcode 0x%08x\n"
1055                "src [%x:%08x], len [%d*4], dst [%x:%08x]\n"
1056                "comp_addr [%x:%08x], comp_val 0x%08x\n",
1057                dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
1058                dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
1059                dmae->comp_addr_hi, dmae->comp_addr_lo,
1060                dmae->comp_val);
1061         else
1062             DP(msglvl, "DMAE: opcode 0x%08x\n"
1063                "src [%08x], len [%d*4], dst [%x:%08x]\n"
1064                "comp_addr [%x:%08x], comp_val 0x%08x\n",
1065                dmae->opcode, dmae->src_addr_lo >> 2,
1066                dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
1067                dmae->comp_addr_hi, dmae->comp_addr_lo,
1068                dmae->comp_val);
1069         break;
1070     case DMAE_CMD_DST_GRC:
1071         if (src_type == DMAE_CMD_SRC_PCI)
1072             DP(msglvl, "DMAE: opcode 0x%08x\n"
1073                "src [%x:%08x], len [%d*4], dst_addr [%08x]\n"
1074                "comp_addr [%x:%08x], comp_val 0x%08x\n",
1075                dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
1076                dmae->len, dmae->dst_addr_lo >> 2,
1077                dmae->comp_addr_hi, dmae->comp_addr_lo,
1078                dmae->comp_val);
1079         else
1080             DP(msglvl, "DMAE: opcode 0x%08x\n"
1081                "src [%08x], len [%d*4], dst [%08x]\n"
1082                "comp_addr [%x:%08x], comp_val 0x%08x\n",
1083                dmae->opcode, dmae->src_addr_lo >> 2,
1084                dmae->len, dmae->dst_addr_lo >> 2,
1085                dmae->comp_addr_hi, dmae->comp_addr_lo,
1086                dmae->comp_val);
1087         break;
1088     default:
1089         if (src_type == DMAE_CMD_SRC_PCI)
1090             DP(msglvl, "DMAE: opcode 0x%08x\n"
1091                "src_addr [%x:%08x]  len [%d * 4]  dst_addr [none]\n"
1092                "comp_addr [%x:%08x]  comp_val 0x%08x\n",
1093                dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
1094                dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
1095                dmae->comp_val);
1096         else
1097             DP(msglvl, "DMAE: opcode 0x%08x\n"
1098                "src_addr [%08x]  len [%d * 4]  dst_addr [none]\n"
1099                "comp_addr [%x:%08x]  comp_val 0x%08x\n",
1100                dmae->opcode, dmae->src_addr_lo >> 2,
1101                dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
1102                dmae->comp_val);
1103         break;
1104     }
1105
1106 }
1107 #endif
1108
1109 static int
1110 bxe_acquire_hw_lock(struct bxe_softc *sc,
1111                     uint32_t         resource)
1112 {
1113     uint32_t lock_status;
1114     uint32_t resource_bit = (1 << resource);
1115     int func = SC_FUNC(sc);
1116     uint32_t hw_lock_control_reg;
1117     int cnt;
1118
1119     /* validate the resource is within range */
1120     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1121         BLOGE(sc, "resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE\n", resource);
1122         return (-1);
1123     }
1124
1125     if (func <= 5) {
1126         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1127     } else {
1128         hw_lock_control_reg =
1129                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1130     }
1131
1132     /* validate the resource is not already taken */
1133     lock_status = REG_RD(sc, hw_lock_control_reg);
1134     if (lock_status & resource_bit) {
1135         BLOGE(sc, "resource in use (status 0x%x bit 0x%x)\n",
1136               lock_status, resource_bit);
1137         return (-1);
1138     }
1139
1140     /* try every 5ms for 5 seconds */
1141     for (cnt = 0; cnt < 1000; cnt++) {
1142         REG_WR(sc, (hw_lock_control_reg + 4), resource_bit);
1143         lock_status = REG_RD(sc, hw_lock_control_reg);
1144         if (lock_status & resource_bit) {
1145             return (0);
1146         }
1147         DELAY(5000);
1148     }
1149
1150     BLOGE(sc, "Resource lock timeout!\n");
1151     return (-1);
1152 }
1153
1154 static int
1155 bxe_release_hw_lock(struct bxe_softc *sc,
1156                     uint32_t         resource)
1157 {
1158     uint32_t lock_status;
1159     uint32_t resource_bit = (1 << resource);
1160     int func = SC_FUNC(sc);
1161     uint32_t hw_lock_control_reg;
1162
1163     /* validate the resource is within range */
1164     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1165         BLOGE(sc, "resource 0x%x > HW_LOCK_MAX_RESOURCE_VALUE\n", resource);
1166         return (-1);
1167     }
1168
1169     if (func <= 5) {
1170         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + (func * 8));
1171     } else {
1172         hw_lock_control_reg =
1173                 (MISC_REG_DRIVER_CONTROL_7 + ((func - 6) * 8));
1174     }
1175
1176     /* validate the resource is currently taken */
1177     lock_status = REG_RD(sc, hw_lock_control_reg);
1178     if (!(lock_status & resource_bit)) {
1179         BLOGE(sc, "resource not in use (status 0x%x bit 0x%x)\n",
1180               lock_status, resource_bit);
1181         return (-1);
1182     }
1183
1184     REG_WR(sc, hw_lock_control_reg, resource_bit);
1185     return (0);
1186 }
1187 static void bxe_acquire_phy_lock(struct bxe_softc *sc)
1188 {
1189         BXE_PHY_LOCK(sc);
1190         bxe_acquire_hw_lock(sc,HW_LOCK_RESOURCE_MDIO); 
1191 }
1192
1193 static void bxe_release_phy_lock(struct bxe_softc *sc)
1194 {
1195         bxe_release_hw_lock(sc,HW_LOCK_RESOURCE_MDIO); 
1196         BXE_PHY_UNLOCK(sc);
1197 }
1198 /*
1199  * Per pf misc lock must be acquired before the per port mcp lock. Otherwise,
1200  * had we done things the other way around, if two pfs from the same port
1201  * would attempt to access nvram at the same time, we could run into a
1202  * scenario such as:
1203  * pf A takes the port lock.
1204  * pf B succeeds in taking the same lock since they are from the same port.
1205  * pf A takes the per pf misc lock. Performs eeprom access.
1206  * pf A finishes. Unlocks the per pf misc lock.
1207  * Pf B takes the lock and proceeds to perform it's own access.
1208  * pf A unlocks the per port lock, while pf B is still working (!).
1209  * mcp takes the per port lock and corrupts pf B's access (and/or has it's own
1210  * access corrupted by pf B).*
1211  */
1212 static int
1213 bxe_acquire_nvram_lock(struct bxe_softc *sc)
1214 {
1215     int port = SC_PORT(sc);
1216     int count, i;
1217     uint32_t val = 0;
1218
1219     /* acquire HW lock: protect against other PFs in PF Direct Assignment */
1220     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1221
1222     /* adjust timeout for emulation/FPGA */
1223     count = NVRAM_TIMEOUT_COUNT;
1224     if (CHIP_REV_IS_SLOW(sc)) {
1225         count *= 100;
1226     }
1227
1228     /* request access to nvram interface */
1229     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1230            (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
1231
1232     for (i = 0; i < count*10; i++) {
1233         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1234         if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1235             break;
1236         }
1237
1238         DELAY(5);
1239     }
1240
1241     if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1242         BLOGE(sc, "Cannot get access to nvram interface\n");
1243         return (-1);
1244     }
1245
1246     return (0);
1247 }
1248
1249 static int
1250 bxe_release_nvram_lock(struct bxe_softc *sc)
1251 {
1252     int port = SC_PORT(sc);
1253     int count, i;
1254     uint32_t val = 0;
1255
1256     /* adjust timeout for emulation/FPGA */
1257     count = NVRAM_TIMEOUT_COUNT;
1258     if (CHIP_REV_IS_SLOW(sc)) {
1259         count *= 100;
1260     }
1261
1262     /* relinquish nvram interface */
1263     REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
1264            (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
1265
1266     for (i = 0; i < count*10; i++) {
1267         val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
1268         if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
1269             break;
1270         }
1271
1272         DELAY(5);
1273     }
1274
1275     if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
1276         BLOGE(sc, "Cannot free access to nvram interface\n");
1277         return (-1);
1278     }
1279
1280     /* release HW lock: protect against other PFs in PF Direct Assignment */
1281     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_NVRAM);
1282
1283     return (0);
1284 }
1285
1286 static void
1287 bxe_enable_nvram_access(struct bxe_softc *sc)
1288 {
1289     uint32_t val;
1290
1291     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1292
1293     /* enable both bits, even on read */
1294     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1295            (val | MCPR_NVM_ACCESS_ENABLE_EN | MCPR_NVM_ACCESS_ENABLE_WR_EN));
1296 }
1297
1298 static void
1299 bxe_disable_nvram_access(struct bxe_softc *sc)
1300 {
1301     uint32_t val;
1302
1303     val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
1304
1305     /* disable both bits, even after read */
1306     REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
1307            (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
1308                     MCPR_NVM_ACCESS_ENABLE_WR_EN)));
1309 }
1310
1311 static int
1312 bxe_nvram_read_dword(struct bxe_softc *sc,
1313                      uint32_t         offset,
1314                      uint32_t         *ret_val,
1315                      uint32_t         cmd_flags)
1316 {
1317     int count, i, rc;
1318     uint32_t val;
1319
1320     /* build the command word */
1321     cmd_flags |= MCPR_NVM_COMMAND_DOIT;
1322
1323     /* need to clear DONE bit separately */
1324     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1325
1326     /* address of the NVRAM to read from */
1327     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1328            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1329
1330     /* issue a read command */
1331     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1332
1333     /* adjust timeout for emulation/FPGA */
1334     count = NVRAM_TIMEOUT_COUNT;
1335     if (CHIP_REV_IS_SLOW(sc)) {
1336         count *= 100;
1337     }
1338
1339     /* wait for completion */
1340     *ret_val = 0;
1341     rc = -1;
1342     for (i = 0; i < count; i++) {
1343         DELAY(5);
1344         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1345
1346         if (val & MCPR_NVM_COMMAND_DONE) {
1347             val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
1348             /* we read nvram data in cpu order
1349              * but ethtool sees it as an array of bytes
1350              * converting to big-endian will do the work
1351              */
1352             *ret_val = htobe32(val);
1353             rc = 0;
1354             break;
1355         }
1356     }
1357
1358     if (rc == -1) {
1359         BLOGE(sc, "nvram read timeout expired\n");
1360     }
1361
1362     return (rc);
1363 }
1364
1365 static int
1366 bxe_nvram_read(struct bxe_softc *sc,
1367                uint32_t         offset,
1368                uint8_t          *ret_buf,
1369                int              buf_size)
1370 {
1371     uint32_t cmd_flags;
1372     uint32_t val;
1373     int rc;
1374
1375     if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1376         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1377               offset, buf_size);
1378         return (-1);
1379     }
1380
1381     if ((offset + buf_size) > sc->devinfo.flash_size) {
1382         BLOGE(sc, "Invalid parameter, "
1383                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1384               offset, buf_size, sc->devinfo.flash_size);
1385         return (-1);
1386     }
1387
1388     /* request access to nvram interface */
1389     rc = bxe_acquire_nvram_lock(sc);
1390     if (rc) {
1391         return (rc);
1392     }
1393
1394     /* enable access to nvram interface */
1395     bxe_enable_nvram_access(sc);
1396
1397     /* read the first word(s) */
1398     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1399     while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
1400         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1401         memcpy(ret_buf, &val, 4);
1402
1403         /* advance to the next dword */
1404         offset += sizeof(uint32_t);
1405         ret_buf += sizeof(uint32_t);
1406         buf_size -= sizeof(uint32_t);
1407         cmd_flags = 0;
1408     }
1409
1410     if (rc == 0) {
1411         cmd_flags |= MCPR_NVM_COMMAND_LAST;
1412         rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
1413         memcpy(ret_buf, &val, 4);
1414     }
1415
1416     /* disable access to nvram interface */
1417     bxe_disable_nvram_access(sc);
1418     bxe_release_nvram_lock(sc);
1419
1420     return (rc);
1421 }
1422
1423 static int
1424 bxe_nvram_write_dword(struct bxe_softc *sc,
1425                       uint32_t         offset,
1426                       uint32_t         val,
1427                       uint32_t         cmd_flags)
1428 {
1429     int count, i, rc;
1430
1431     /* build the command word */
1432     cmd_flags |= (MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR);
1433
1434     /* need to clear DONE bit separately */
1435     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1436
1437     /* write the data */
1438     REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
1439
1440     /* address of the NVRAM to write to */
1441     REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
1442            (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1443
1444     /* issue the write command */
1445     REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1446
1447     /* adjust timeout for emulation/FPGA */
1448     count = NVRAM_TIMEOUT_COUNT;
1449     if (CHIP_REV_IS_SLOW(sc)) {
1450         count *= 100;
1451     }
1452
1453     /* wait for completion */
1454     rc = -1;
1455     for (i = 0; i < count; i++) {
1456         DELAY(5);
1457         val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
1458         if (val & MCPR_NVM_COMMAND_DONE) {
1459             rc = 0;
1460             break;
1461         }
1462     }
1463
1464     if (rc == -1) {
1465         BLOGE(sc, "nvram write timeout expired\n");
1466     }
1467
1468     return (rc);
1469 }
1470
1471 #define BYTE_OFFSET(offset) (8 * (offset & 0x03))
1472
1473 static int
1474 bxe_nvram_write1(struct bxe_softc *sc,
1475                  uint32_t         offset,
1476                  uint8_t          *data_buf,
1477                  int              buf_size)
1478 {
1479     uint32_t cmd_flags;
1480     uint32_t align_offset;
1481     uint32_t val;
1482     int rc;
1483
1484     if ((offset + buf_size) > sc->devinfo.flash_size) {
1485         BLOGE(sc, "Invalid parameter, "
1486                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1487               offset, buf_size, sc->devinfo.flash_size);
1488         return (-1);
1489     }
1490
1491     /* request access to nvram interface */
1492     rc = bxe_acquire_nvram_lock(sc);
1493     if (rc) {
1494         return (rc);
1495     }
1496
1497     /* enable access to nvram interface */
1498     bxe_enable_nvram_access(sc);
1499
1500     cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1501     align_offset = (offset & ~0x03);
1502     rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
1503
1504     if (rc == 0) {
1505         val &= ~(0xff << BYTE_OFFSET(offset));
1506         val |= (*data_buf << BYTE_OFFSET(offset));
1507
1508         /* nvram data is returned as an array of bytes
1509          * convert it back to cpu order
1510          */
1511         val = be32toh(val);
1512
1513         rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
1514     }
1515
1516     /* disable access to nvram interface */
1517     bxe_disable_nvram_access(sc);
1518     bxe_release_nvram_lock(sc);
1519
1520     return (rc);
1521 }
1522
1523 static int
1524 bxe_nvram_write(struct bxe_softc *sc,
1525                 uint32_t         offset,
1526                 uint8_t          *data_buf,
1527                 int              buf_size)
1528 {
1529     uint32_t cmd_flags;
1530     uint32_t val;
1531     uint32_t written_so_far;
1532     int rc;
1533
1534     if (buf_size == 1) {
1535         return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
1536     }
1537
1538     if ((offset & 0x03) || (buf_size & 0x03) /* || (buf_size == 0) */) {
1539         BLOGE(sc, "Invalid parameter, offset 0x%x buf_size 0x%x\n",
1540               offset, buf_size);
1541         return (-1);
1542     }
1543
1544     if (buf_size == 0) {
1545         return (0); /* nothing to do */
1546     }
1547
1548     if ((offset + buf_size) > sc->devinfo.flash_size) {
1549         BLOGE(sc, "Invalid parameter, "
1550                   "offset 0x%x + buf_size 0x%x > flash_size 0x%x\n",
1551               offset, buf_size, sc->devinfo.flash_size);
1552         return (-1);
1553     }
1554
1555     /* request access to nvram interface */
1556     rc = bxe_acquire_nvram_lock(sc);
1557     if (rc) {
1558         return (rc);
1559     }
1560
1561     /* enable access to nvram interface */
1562     bxe_enable_nvram_access(sc);
1563
1564     written_so_far = 0;
1565     cmd_flags = MCPR_NVM_COMMAND_FIRST;
1566     while ((written_so_far < buf_size) && (rc == 0)) {
1567         if (written_so_far == (buf_size - sizeof(uint32_t))) {
1568             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1569         } else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0) {
1570             cmd_flags |= MCPR_NVM_COMMAND_LAST;
1571         } else if ((offset % NVRAM_PAGE_SIZE) == 0) {
1572             cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1573         }
1574
1575         memcpy(&val, data_buf, 4);
1576
1577         rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
1578
1579         /* advance to the next dword */
1580         offset += sizeof(uint32_t);
1581         data_buf += sizeof(uint32_t);
1582         written_so_far += sizeof(uint32_t);
1583         cmd_flags = 0;
1584     }
1585
1586     /* disable access to nvram interface */
1587     bxe_disable_nvram_access(sc);
1588     bxe_release_nvram_lock(sc);
1589
1590     return (rc);
1591 }
1592
1593 /* copy command into DMAE command memory and set DMAE command Go */
1594 void
1595 bxe_post_dmae(struct bxe_softc    *sc,
1596               struct dmae_command *dmae,
1597               int                 idx)
1598 {
1599     uint32_t cmd_offset;
1600     int i;
1601
1602     cmd_offset = (DMAE_REG_CMD_MEM + (sizeof(struct dmae_command) * idx));
1603     for (i = 0; i < ((sizeof(struct dmae_command) / 4)); i++) {
1604         REG_WR(sc, (cmd_offset + (i * 4)), *(((uint32_t *)dmae) + i));
1605     }
1606
1607     REG_WR(sc, dmae_reg_go_c[idx], 1);
1608 }
1609
1610 uint32_t
1611 bxe_dmae_opcode_add_comp(uint32_t opcode,
1612                          uint8_t  comp_type)
1613 {
1614     return (opcode | ((comp_type << DMAE_COMMAND_C_DST_SHIFT) |
1615                       DMAE_COMMAND_C_TYPE_ENABLE));
1616 }
1617
1618 uint32_t
1619 bxe_dmae_opcode_clr_src_reset(uint32_t opcode)
1620 {
1621     return (opcode & ~DMAE_COMMAND_SRC_RESET);
1622 }
1623
1624 uint32_t
1625 bxe_dmae_opcode(struct bxe_softc *sc,
1626                 uint8_t          src_type,
1627                 uint8_t          dst_type,
1628                 uint8_t          with_comp,
1629                 uint8_t          comp_type)
1630 {
1631     uint32_t opcode = 0;
1632
1633     opcode |= ((src_type << DMAE_COMMAND_SRC_SHIFT) |
1634                (dst_type << DMAE_COMMAND_DST_SHIFT));
1635
1636     opcode |= (DMAE_COMMAND_SRC_RESET | DMAE_COMMAND_DST_RESET);
1637
1638     opcode |= (SC_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
1639
1640     opcode |= ((SC_VN(sc) << DMAE_COMMAND_E1HVN_SHIFT) |
1641                (SC_VN(sc) << DMAE_COMMAND_DST_VN_SHIFT));
1642
1643     opcode |= (DMAE_COM_SET_ERR << DMAE_COMMAND_ERR_POLICY_SHIFT);
1644
1645 #ifdef __BIG_ENDIAN
1646     opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
1647 #else
1648     opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
1649 #endif
1650
1651     if (with_comp) {
1652         opcode = bxe_dmae_opcode_add_comp(opcode, comp_type);
1653     }
1654
1655     return (opcode);
1656 }
1657
1658 static void
1659 bxe_prep_dmae_with_comp(struct bxe_softc    *sc,
1660                         struct dmae_command *dmae,
1661                         uint8_t             src_type,
1662                         uint8_t             dst_type)
1663 {
1664     memset(dmae, 0, sizeof(struct dmae_command));
1665
1666     /* set the opcode */
1667     dmae->opcode = bxe_dmae_opcode(sc, src_type, dst_type,
1668                                    TRUE, DMAE_COMP_PCI);
1669
1670     /* fill in the completion parameters */
1671     dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
1672     dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
1673     dmae->comp_val     = DMAE_COMP_VAL;
1674 }
1675
1676 /* issue a DMAE command over the init channel and wait for completion */
1677 static int
1678 bxe_issue_dmae_with_comp(struct bxe_softc    *sc,
1679                          struct dmae_command *dmae)
1680 {
1681     uint32_t *wb_comp = BXE_SP(sc, wb_comp);
1682     int timeout = CHIP_REV_IS_SLOW(sc) ? 400000 : 4000;
1683
1684     BXE_DMAE_LOCK(sc);
1685
1686     /* reset completion */
1687     *wb_comp = 0;
1688
1689     /* post the command on the channel used for initializations */
1690     bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
1691
1692     /* wait for completion */
1693     DELAY(5);
1694
1695     while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
1696         if (!timeout ||
1697             (sc->recovery_state != BXE_RECOVERY_DONE &&
1698              sc->recovery_state != BXE_RECOVERY_NIC_LOADING)) {
1699             BLOGE(sc, "DMAE timeout!\n");
1700             BXE_DMAE_UNLOCK(sc);
1701             return (DMAE_TIMEOUT);
1702         }
1703
1704         timeout--;
1705         DELAY(50);
1706     }
1707
1708     if (*wb_comp & DMAE_PCI_ERR_FLAG) {
1709         BLOGE(sc, "DMAE PCI error!\n");
1710         BXE_DMAE_UNLOCK(sc);
1711         return (DMAE_PCI_ERROR);
1712     }
1713
1714     BXE_DMAE_UNLOCK(sc);
1715     return (0);
1716 }
1717
1718 void
1719 bxe_read_dmae(struct bxe_softc *sc,
1720               uint32_t         src_addr,
1721               uint32_t         len32)
1722 {
1723     struct dmae_command dmae;
1724     uint32_t *data;
1725     int i, rc;
1726
1727     DBASSERT(sc, (len32 <= 4), ("DMAE read length is %d", len32));
1728
1729     if (!sc->dmae_ready) {
1730         data = BXE_SP(sc, wb_data[0]);
1731
1732         for (i = 0; i < len32; i++) {
1733             data[i] = (CHIP_IS_E1(sc)) ?
1734                           bxe_reg_rd_ind(sc, (src_addr + (i * 4))) :
1735                           REG_RD(sc, (src_addr + (i * 4)));
1736         }
1737
1738         return;
1739     }
1740
1741     /* set opcode and fixed command fields */
1742     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
1743
1744     /* fill in addresses and len */
1745     dmae.src_addr_lo = (src_addr >> 2); /* GRC addr has dword resolution */
1746     dmae.src_addr_hi = 0;
1747     dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
1748     dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
1749     dmae.len         = len32;
1750
1751     /* issue the command and wait for completion */
1752     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1753         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1754     };
1755 }
1756
1757 void
1758 bxe_write_dmae(struct bxe_softc *sc,
1759                bus_addr_t       dma_addr,
1760                uint32_t         dst_addr,
1761                uint32_t         len32)
1762 {
1763     struct dmae_command dmae;
1764     int rc;
1765
1766     if (!sc->dmae_ready) {
1767         DBASSERT(sc, (len32 <= 4), ("DMAE not ready and length is %d", len32));
1768
1769         if (CHIP_IS_E1(sc)) {
1770             ecore_init_ind_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1771         } else {
1772             ecore_init_str_wr(sc, dst_addr, BXE_SP(sc, wb_data[0]), len32);
1773         }
1774
1775         return;
1776     }
1777
1778     /* set opcode and fixed command fields */
1779     bxe_prep_dmae_with_comp(sc, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
1780
1781     /* fill in addresses and len */
1782     dmae.src_addr_lo = U64_LO(dma_addr);
1783     dmae.src_addr_hi = U64_HI(dma_addr);
1784     dmae.dst_addr_lo = (dst_addr >> 2); /* GRC addr has dword resolution */
1785     dmae.dst_addr_hi = 0;
1786     dmae.len         = len32;
1787
1788     /* issue the command and wait for completion */
1789     if ((rc = bxe_issue_dmae_with_comp(sc, &dmae)) != 0) {
1790         bxe_panic(sc, ("DMAE failed (%d)\n", rc));
1791     }
1792 }
1793
1794 void
1795 bxe_write_dmae_phys_len(struct bxe_softc *sc,
1796                         bus_addr_t       phys_addr,
1797                         uint32_t         addr,
1798                         uint32_t         len)
1799 {
1800     int dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
1801     int offset = 0;
1802
1803     while (len > dmae_wr_max) {
1804         bxe_write_dmae(sc,
1805                        (phys_addr + offset), /* src DMA address */
1806                        (addr + offset),      /* dst GRC address */
1807                        dmae_wr_max);
1808         offset += (dmae_wr_max * 4);
1809         len -= dmae_wr_max;
1810     }
1811
1812     bxe_write_dmae(sc,
1813                    (phys_addr + offset), /* src DMA address */
1814                    (addr + offset),      /* dst GRC address */
1815                    len);
1816 }
1817
1818 void
1819 bxe_set_ctx_validation(struct bxe_softc   *sc,
1820                        struct eth_context *cxt,
1821                        uint32_t           cid)
1822 {
1823     /* ustorm cxt validation */
1824     cxt->ustorm_ag_context.cdu_usage =
1825         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1826             CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
1827     /* xcontext validation */
1828     cxt->xstorm_ag_context.cdu_reserved =
1829         CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, cid),
1830             CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
1831 }
1832
1833 static void
1834 bxe_storm_memset_hc_timeout(struct bxe_softc *sc,
1835                             uint8_t          port,
1836                             uint8_t          fw_sb_id,
1837                             uint8_t          sb_index,
1838                             uint8_t          ticks)
1839 {
1840     uint32_t addr =
1841         (BAR_CSTRORM_INTMEM +
1842          CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index));
1843
1844     REG_WR8(sc, addr, ticks);
1845
1846     BLOGD(sc, DBG_LOAD,
1847           "port %d fw_sb_id %d sb_index %d ticks %d\n",
1848           port, fw_sb_id, sb_index, ticks);
1849 }
1850
1851 static void
1852 bxe_storm_memset_hc_disable(struct bxe_softc *sc,
1853                             uint8_t          port,
1854                             uint16_t         fw_sb_id,
1855                             uint8_t          sb_index,
1856                             uint8_t          disable)
1857 {
1858     uint32_t enable_flag =
1859         (disable) ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
1860     uint32_t addr =
1861         (BAR_CSTRORM_INTMEM +
1862          CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index));
1863     uint8_t flags;
1864
1865     /* clear and set */
1866     flags = REG_RD8(sc, addr);
1867     flags &= ~HC_INDEX_DATA_HC_ENABLED;
1868     flags |= enable_flag;
1869     REG_WR8(sc, addr, flags);
1870
1871     BLOGD(sc, DBG_LOAD,
1872           "port %d fw_sb_id %d sb_index %d disable %d\n",
1873           port, fw_sb_id, sb_index, disable);
1874 }
1875
1876 void
1877 bxe_update_coalesce_sb_index(struct bxe_softc *sc,
1878                              uint8_t          fw_sb_id,
1879                              uint8_t          sb_index,
1880                              uint8_t          disable,
1881                              uint16_t         usec)
1882 {
1883     int port = SC_PORT(sc);
1884     uint8_t ticks = (usec / 4); /* XXX ??? */
1885
1886     bxe_storm_memset_hc_timeout(sc, port, fw_sb_id, sb_index, ticks);
1887
1888     disable = (disable) ? 1 : ((usec) ? 0 : 1);
1889     bxe_storm_memset_hc_disable(sc, port, fw_sb_id, sb_index, disable);
1890 }
1891
1892 void
1893 elink_cb_udelay(struct bxe_softc *sc,
1894                 uint32_t         usecs)
1895 {
1896     DELAY(usecs);
1897 }
1898
1899 uint32_t
1900 elink_cb_reg_read(struct bxe_softc *sc,
1901                   uint32_t         reg_addr)
1902 {
1903     return (REG_RD(sc, reg_addr));
1904 }
1905
1906 void
1907 elink_cb_reg_write(struct bxe_softc *sc,
1908                    uint32_t         reg_addr,
1909                    uint32_t         val)
1910 {
1911     REG_WR(sc, reg_addr, val);
1912 }
1913
1914 void
1915 elink_cb_reg_wb_write(struct bxe_softc *sc,
1916                       uint32_t         offset,
1917                       uint32_t         *wb_write,
1918                       uint16_t         len)
1919 {
1920     REG_WR_DMAE(sc, offset, wb_write, len);
1921 }
1922
1923 void
1924 elink_cb_reg_wb_read(struct bxe_softc *sc,
1925                      uint32_t         offset,
1926                      uint32_t         *wb_write,
1927                      uint16_t         len)
1928 {
1929     REG_RD_DMAE(sc, offset, wb_write, len);
1930 }
1931
1932 uint8_t
1933 elink_cb_path_id(struct bxe_softc *sc)
1934 {
1935     return (SC_PATH(sc));
1936 }
1937
1938 void
1939 elink_cb_event_log(struct bxe_softc     *sc,
1940                    const elink_log_id_t elink_log_id,
1941                    ...)
1942 {
1943     /* XXX */
1944 #if 0
1945     //va_list ap;
1946     va_start(ap, elink_log_id);
1947     _XXX_(sc, lm_log_id, ap);
1948     va_end(ap);
1949 #endif
1950     BLOGI(sc, "ELINK EVENT LOG (%d)\n", elink_log_id);
1951 }
1952
1953 static int
1954 bxe_set_spio(struct bxe_softc *sc,
1955              int              spio,
1956              uint32_t         mode)
1957 {
1958     uint32_t spio_reg;
1959
1960     /* Only 2 SPIOs are configurable */
1961     if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
1962         BLOGE(sc, "Invalid SPIO 0x%x\n", spio);
1963         return (-1);
1964     }
1965
1966     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1967
1968     /* read SPIO and mask except the float bits */
1969     spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
1970
1971     switch (mode) {
1972     case MISC_SPIO_OUTPUT_LOW:
1973         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output low\n", spio);
1974         /* clear FLOAT and set CLR */
1975         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1976         spio_reg |=  (spio << MISC_SPIO_CLR_POS);
1977         break;
1978
1979     case MISC_SPIO_OUTPUT_HIGH:
1980         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> output high\n", spio);
1981         /* clear FLOAT and set SET */
1982         spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
1983         spio_reg |=  (spio << MISC_SPIO_SET_POS);
1984         break;
1985
1986     case MISC_SPIO_INPUT_HI_Z:
1987         BLOGD(sc, DBG_LOAD, "Set SPIO 0x%x -> input\n", spio);
1988         /* set FLOAT */
1989         spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
1990         break;
1991
1992     default:
1993         break;
1994     }
1995
1996     REG_WR(sc, MISC_REG_SPIO, spio_reg);
1997     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
1998
1999     return (0);
2000 }
2001
2002 static int
2003 bxe_gpio_read(struct bxe_softc *sc,
2004               int              gpio_num,
2005               uint8_t          port)
2006 {
2007     /* The GPIO should be swapped if swap register is set and active */
2008     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2009                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2010     int gpio_shift = (gpio_num +
2011                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2012     uint32_t gpio_mask = (1 << gpio_shift);
2013     uint32_t gpio_reg;
2014
2015     if (gpio_num > MISC_REGISTERS_GPIO_3) {
2016         BLOGE(sc, "Invalid GPIO %d\n", gpio_num);
2017         return (-1);
2018     }
2019
2020     /* read GPIO value */
2021     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
2022
2023     /* get the requested pin value */
2024     return ((gpio_reg & gpio_mask) == gpio_mask) ? 1 : 0;
2025 }
2026
2027 static int
2028 bxe_gpio_write(struct bxe_softc *sc,
2029                int              gpio_num,
2030                uint32_t         mode,
2031                uint8_t          port)
2032 {
2033     /* The GPIO should be swapped if swap register is set and active */
2034     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2035                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2036     int gpio_shift = (gpio_num +
2037                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2038     uint32_t gpio_mask = (1 << gpio_shift);
2039     uint32_t gpio_reg;
2040
2041     if (gpio_num > MISC_REGISTERS_GPIO_3) {
2042         BLOGE(sc, "Invalid GPIO %d\n", gpio_num);
2043         return (-1);
2044     }
2045
2046     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2047
2048     /* read GPIO and mask except the float bits */
2049     gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
2050
2051     switch (mode) {
2052     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2053         BLOGD(sc, DBG_PHY,
2054               "Set GPIO %d (shift %d) -> output low\n",
2055               gpio_num, gpio_shift);
2056         /* clear FLOAT and set CLR */
2057         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2058         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
2059         break;
2060
2061     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2062         BLOGD(sc, DBG_PHY,
2063               "Set GPIO %d (shift %d) -> output high\n",
2064               gpio_num, gpio_shift);
2065         /* clear FLOAT and set SET */
2066         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2067         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
2068         break;
2069
2070     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2071         BLOGD(sc, DBG_PHY,
2072               "Set GPIO %d (shift %d) -> input\n",
2073               gpio_num, gpio_shift);
2074         /* set FLOAT */
2075         gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2076         break;
2077
2078     default:
2079         break;
2080     }
2081
2082     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2083     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2084
2085     return (0);
2086 }
2087
2088 static int
2089 bxe_gpio_mult_write(struct bxe_softc *sc,
2090                     uint8_t          pins,
2091                     uint32_t         mode)
2092 {
2093     uint32_t gpio_reg;
2094
2095     /* any port swapping should be handled by caller */
2096
2097     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2098
2099     /* read GPIO and mask except the float bits */
2100     gpio_reg = REG_RD(sc, MISC_REG_GPIO);
2101     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2102     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
2103     gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
2104
2105     switch (mode) {
2106     case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2107         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output low\n", pins);
2108         /* set CLR */
2109         gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
2110         break;
2111
2112     case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2113         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> output high\n", pins);
2114         /* set SET */
2115         gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
2116         break;
2117
2118     case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2119         BLOGD(sc, DBG_PHY, "Set GPIO 0x%x -> input\n", pins);
2120         /* set FLOAT */
2121         gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2122         break;
2123
2124     default:
2125         BLOGE(sc, "Invalid GPIO mode assignment %d\n", mode);
2126         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2127         return (-1);
2128     }
2129
2130     REG_WR(sc, MISC_REG_GPIO, gpio_reg);
2131     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2132
2133     return (0);
2134 }
2135
2136 static int
2137 bxe_gpio_int_write(struct bxe_softc *sc,
2138                    int              gpio_num,
2139                    uint32_t         mode,
2140                    uint8_t          port)
2141 {
2142     /* The GPIO should be swapped if swap register is set and active */
2143     int gpio_port = ((REG_RD(sc, NIG_REG_PORT_SWAP) &&
2144                       REG_RD(sc, NIG_REG_STRAP_OVERRIDE)) ^ port);
2145     int gpio_shift = (gpio_num +
2146                       (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0));
2147     uint32_t gpio_mask = (1 << gpio_shift);
2148     uint32_t gpio_reg;
2149
2150     if (gpio_num > MISC_REGISTERS_GPIO_3) {
2151         BLOGE(sc, "Invalid GPIO %d\n", gpio_num);
2152         return (-1);
2153     }
2154
2155     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2156
2157     /* read GPIO int */
2158     gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
2159
2160     switch (mode) {
2161     case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2162         BLOGD(sc, DBG_PHY,
2163               "Clear GPIO INT %d (shift %d) -> output low\n",
2164               gpio_num, gpio_shift);
2165         /* clear SET and set CLR */
2166         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2167         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2168         break;
2169
2170     case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2171         BLOGD(sc, DBG_PHY,
2172               "Set GPIO INT %d (shift %d) -> output high\n",
2173               gpio_num, gpio_shift);
2174         /* clear CLR and set SET */
2175         gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2176         gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2177         break;
2178
2179     default:
2180         break;
2181     }
2182
2183     REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
2184     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
2185
2186     return (0);
2187 }
2188
2189 uint32_t
2190 elink_cb_gpio_read(struct bxe_softc *sc,
2191                    uint16_t         gpio_num,
2192                    uint8_t          port)
2193 {
2194     return (bxe_gpio_read(sc, gpio_num, port));
2195 }
2196
2197 uint8_t
2198 elink_cb_gpio_write(struct bxe_softc *sc,
2199                     uint16_t         gpio_num,
2200                     uint8_t          mode, /* 0=low 1=high */
2201                     uint8_t          port)
2202 {
2203     return (bxe_gpio_write(sc, gpio_num, mode, port));
2204 }
2205
2206 uint8_t
2207 elink_cb_gpio_mult_write(struct bxe_softc *sc,
2208                          uint8_t          pins,
2209                          uint8_t          mode) /* 0=low 1=high */
2210 {
2211     return (bxe_gpio_mult_write(sc, pins, mode));
2212 }
2213
2214 uint8_t
2215 elink_cb_gpio_int_write(struct bxe_softc *sc,
2216                         uint16_t         gpio_num,
2217                         uint8_t          mode, /* 0=low 1=high */
2218                         uint8_t          port)
2219 {
2220     return (bxe_gpio_int_write(sc, gpio_num, mode, port));
2221 }
2222
2223 void
2224 elink_cb_notify_link_changed(struct bxe_softc *sc)
2225 {
2226     REG_WR(sc, (MISC_REG_AEU_GENERAL_ATTN_12 +
2227                 (SC_FUNC(sc) * sizeof(uint32_t))), 1);
2228 }
2229
2230 /* send the MCP a request, block until there is a reply */
2231 uint32_t
2232 elink_cb_fw_command(struct bxe_softc *sc,
2233                     uint32_t         command,
2234                     uint32_t         param)
2235 {
2236     int mb_idx = SC_FW_MB_IDX(sc);
2237     uint32_t seq;
2238     uint32_t rc = 0;
2239     uint32_t cnt = 1;
2240     uint8_t delay = CHIP_REV_IS_SLOW(sc) ? 100 : 10;
2241
2242     BXE_FWMB_LOCK(sc);
2243
2244     seq = ++sc->fw_seq;
2245     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_param, param);
2246     SHMEM_WR(sc, func_mb[mb_idx].drv_mb_header, (command | seq));
2247
2248     BLOGD(sc, DBG_PHY,
2249           "wrote command 0x%08x to FW MB param 0x%08x\n",
2250           (command | seq), param);
2251
2252     /* Let the FW do it's magic. GIve it up to 5 seconds... */
2253     do {
2254         DELAY(delay * 1000);
2255         rc = SHMEM_RD(sc, func_mb[mb_idx].fw_mb_header);
2256     } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2257
2258     BLOGD(sc, DBG_PHY,
2259           "[after %d ms] read 0x%x seq 0x%x from FW MB\n",
2260           cnt*delay, rc, seq);
2261
2262     /* is this a reply to our command? */
2263     if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK)) {
2264         rc &= FW_MSG_CODE_MASK;
2265     } else {
2266         /* Ruh-roh! */
2267         BLOGE(sc, "FW failed to respond!\n");
2268         // XXX bxe_fw_dump(sc);
2269         rc = 0;
2270     }
2271
2272     BXE_FWMB_UNLOCK(sc);
2273     return (rc);
2274 }
2275
2276 static uint32_t
2277 bxe_fw_command(struct bxe_softc *sc,
2278                uint32_t         command,
2279                uint32_t         param)
2280 {
2281     return (elink_cb_fw_command(sc, command, param));
2282 }
2283
2284 static void
2285 __storm_memset_dma_mapping(struct bxe_softc *sc,
2286                            uint32_t         addr,
2287                            bus_addr_t       mapping)
2288 {
2289     REG_WR(sc, addr, U64_LO(mapping));
2290     REG_WR(sc, (addr + 4), U64_HI(mapping));
2291 }
2292
2293 static void
2294 storm_memset_spq_addr(struct bxe_softc *sc,
2295                       bus_addr_t       mapping,
2296                       uint16_t         abs_fid)
2297 {
2298     uint32_t addr = (XSEM_REG_FAST_MEMORY +
2299                      XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid));
2300     __storm_memset_dma_mapping(sc, addr, mapping);
2301 }
2302
2303 static void
2304 storm_memset_vf_to_pf(struct bxe_softc *sc,
2305                       uint16_t         abs_fid,
2306                       uint16_t         pf_id)
2307 {
2308     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2309     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2310     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2311     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid)), pf_id);
2312 }
2313
2314 static void
2315 storm_memset_func_en(struct bxe_softc *sc,
2316                      uint16_t         abs_fid,
2317                      uint8_t          enable)
2318 {
2319     REG_WR8(sc, (BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2320     REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2321     REG_WR8(sc, (BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2322     REG_WR8(sc, (BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid)), enable);
2323 }
2324
2325 static void
2326 storm_memset_eq_data(struct bxe_softc       *sc,
2327                      struct event_ring_data *eq_data,
2328                      uint16_t               pfid)
2329 {
2330     uint32_t addr;
2331     size_t size;
2332
2333     addr = (BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid));
2334     size = sizeof(struct event_ring_data);
2335     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)eq_data);
2336 }
2337
2338 static void
2339 storm_memset_eq_prod(struct bxe_softc *sc,
2340                      uint16_t         eq_prod,
2341                      uint16_t         pfid)
2342 {
2343     uint32_t addr = (BAR_CSTRORM_INTMEM +
2344                      CSTORM_EVENT_RING_PROD_OFFSET(pfid));
2345     REG_WR16(sc, addr, eq_prod);
2346 }
2347
2348 /*
2349  * Post a slowpath command.
2350  *
2351  * A slowpath command is used to propogate a configuration change through
2352  * the controller in a controlled manner, allowing each STORM processor and
2353  * other H/W blocks to phase in the change.  The commands sent on the
2354  * slowpath are referred to as ramrods.  Depending on the ramrod used the
2355  * completion of the ramrod will occur in different ways.  Here's a
2356  * breakdown of ramrods and how they complete:
2357  *
2358  * RAMROD_CMD_ID_ETH_PORT_SETUP
2359  *   Used to setup the leading connection on a port.  Completes on the
2360  *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
2361  *
2362  * RAMROD_CMD_ID_ETH_CLIENT_SETUP
2363  *   Used to setup an additional connection on a port.  Completes on the
2364  *   RCQ of the multi-queue/RSS connection being initialized.
2365  *
2366  * RAMROD_CMD_ID_ETH_STAT_QUERY
2367  *   Used to force the storm processors to update the statistics database
2368  *   in host memory.  This ramrod is send on the leading connection CID and
2369  *   completes as an index increment of the CSTORM on the default status
2370  *   block.
2371  *
2372  * RAMROD_CMD_ID_ETH_UPDATE
2373  *   Used to update the state of the leading connection, usually to udpate
2374  *   the RSS indirection table.  Completes on the RCQ of the leading
2375  *   connection. (Not currently used under FreeBSD until OS support becomes
2376  *   available.)
2377  *
2378  * RAMROD_CMD_ID_ETH_HALT
2379  *   Used when tearing down a connection prior to driver unload.  Completes
2380  *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
2381  *   use this on the leading connection.
2382  *
2383  * RAMROD_CMD_ID_ETH_SET_MAC
2384  *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
2385  *   the RCQ of the leading connection.
2386  *
2387  * RAMROD_CMD_ID_ETH_CFC_DEL
2388  *   Used when tearing down a conneciton prior to driver unload.  Completes
2389  *   on the RCQ of the leading connection (since the current connection
2390  *   has been completely removed from controller memory).
2391  *
2392  * RAMROD_CMD_ID_ETH_PORT_DEL
2393  *   Used to tear down the leading connection prior to driver unload,
2394  *   typically fp[0].  Completes as an index increment of the CSTORM on the
2395  *   default status block.
2396  *
2397  * RAMROD_CMD_ID_ETH_FORWARD_SETUP
2398  *   Used for connection offload.  Completes on the RCQ of the multi-queue
2399  *   RSS connection that is being offloaded.  (Not currently used under
2400  *   FreeBSD.)
2401  *
2402  * There can only be one command pending per function.
2403  *
2404  * Returns:
2405  *   0 = Success, !0 = Failure.
2406  */
2407
2408 /* must be called under the spq lock */
2409 static inline
2410 struct eth_spe *bxe_sp_get_next(struct bxe_softc *sc)
2411 {
2412     struct eth_spe *next_spe = sc->spq_prod_bd;
2413
2414     if (sc->spq_prod_bd == sc->spq_last_bd) {
2415         /* wrap back to the first eth_spq */
2416         sc->spq_prod_bd = sc->spq;
2417         sc->spq_prod_idx = 0;
2418     } else {
2419         sc->spq_prod_bd++;
2420         sc->spq_prod_idx++;
2421     }
2422
2423     return (next_spe);
2424 }
2425
2426 /* must be called under the spq lock */
2427 static inline
2428 void bxe_sp_prod_update(struct bxe_softc *sc)
2429 {
2430     int func = SC_FUNC(sc);
2431
2432     /*
2433      * Make sure that BD data is updated before writing the producer.
2434      * BD data is written to the memory, the producer is read from the
2435      * memory, thus we need a full memory barrier to ensure the ordering.
2436      */
2437     mb();
2438
2439     REG_WR16(sc, (BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func)),
2440              sc->spq_prod_idx);
2441
2442     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
2443                       BUS_SPACE_BARRIER_WRITE);
2444 }
2445
2446 /**
2447  * bxe_is_contextless_ramrod - check if the current command ends on EQ
2448  *
2449  * @cmd:      command to check
2450  * @cmd_type: command type
2451  */
2452 static inline
2453 int bxe_is_contextless_ramrod(int cmd,
2454                               int cmd_type)
2455 {
2456     if ((cmd_type == NONE_CONNECTION_TYPE) ||
2457         (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
2458         (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2459         (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2460         (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2461         (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2462         (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE)) {
2463         return (TRUE);
2464     } else {
2465         return (FALSE);
2466     }
2467 }
2468
2469 /**
2470  * bxe_sp_post - place a single command on an SP ring
2471  *
2472  * @sc:         driver handle
2473  * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2474  * @cid:        SW CID the command is related to
2475  * @data_hi:    command private data address (high 32 bits)
2476  * @data_lo:    command private data address (low 32 bits)
2477  * @cmd_type:   command type (e.g. NONE, ETH)
2478  *
2479  * SP data is handled as if it's always an address pair, thus data fields are
2480  * not swapped to little endian in upper functions. Instead this function swaps
2481  * data as if it's two uint32 fields.
2482  */
2483 int
2484 bxe_sp_post(struct bxe_softc *sc,
2485             int              command,
2486             int              cid,
2487             uint32_t         data_hi,
2488             uint32_t         data_lo,
2489             int              cmd_type)
2490 {
2491     struct eth_spe *spe;
2492     uint16_t type;
2493     int common;
2494
2495     common = bxe_is_contextless_ramrod(command, cmd_type);
2496
2497     BXE_SP_LOCK(sc);
2498
2499     if (common) {
2500         if (!atomic_load_acq_long(&sc->eq_spq_left)) {
2501             BLOGE(sc, "EQ ring is full!\n");
2502             BXE_SP_UNLOCK(sc);
2503             return (-1);
2504         }
2505     } else {
2506         if (!atomic_load_acq_long(&sc->cq_spq_left)) {
2507             BLOGE(sc, "SPQ ring is full!\n");
2508             BXE_SP_UNLOCK(sc);
2509             return (-1);
2510         }
2511     }
2512
2513     spe = bxe_sp_get_next(sc);
2514
2515     /* CID needs port number to be encoded int it */
2516     spe->hdr.conn_and_cmd_data =
2517         htole32((command << SPE_HDR_CMD_ID_SHIFT) | HW_CID(sc, cid));
2518
2519     type = (cmd_type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
2520
2521     /* TBD: Check if it works for VFs */
2522     type |= ((SC_FUNC(sc) << SPE_HDR_FUNCTION_ID_SHIFT) &
2523              SPE_HDR_FUNCTION_ID);
2524
2525     spe->hdr.type = htole16(type);
2526
2527     spe->data.update_data_addr.hi = htole32(data_hi);
2528     spe->data.update_data_addr.lo = htole32(data_lo);
2529
2530     /*
2531      * It's ok if the actual decrement is issued towards the memory
2532      * somewhere between the lock and unlock. Thus no more explict
2533      * memory barrier is needed.
2534      */
2535     if (common) {
2536         atomic_subtract_acq_long(&sc->eq_spq_left, 1);
2537     } else {
2538         atomic_subtract_acq_long(&sc->cq_spq_left, 1);
2539     }
2540
2541     BLOGD(sc, DBG_SP, "SPQE -> %#jx\n", (uintmax_t)sc->spq_dma.paddr);
2542     BLOGD(sc, DBG_SP, "FUNC_RDATA -> %p / %#jx\n",
2543           BXE_SP(sc, func_rdata), (uintmax_t)BXE_SP_MAPPING(sc, func_rdata));
2544     BLOGD(sc, DBG_SP,
2545           "SPQE[%x] (%x:%x) (cmd, common?) (%d,%d) hw_cid %x data (%x:%x) type(0x%x) left (CQ, EQ) (%lx,%lx)\n",
2546           sc->spq_prod_idx,
2547           (uint32_t)U64_HI(sc->spq_dma.paddr),
2548           (uint32_t)(U64_LO(sc->spq_dma.paddr) + (uint8_t *)sc->spq_prod_bd - (uint8_t *)sc->spq),
2549           command,
2550           common,
2551           HW_CID(sc, cid),
2552           data_hi,
2553           data_lo,
2554           type,
2555           atomic_load_acq_long(&sc->cq_spq_left),
2556           atomic_load_acq_long(&sc->eq_spq_left));
2557
2558     bxe_sp_prod_update(sc);
2559
2560     BXE_SP_UNLOCK(sc);
2561     return (0);
2562 }
2563
2564 /**
2565  * bxe_debug_print_ind_table - prints the indirection table configuration.
2566  *
2567  * @sc: driver hanlde
2568  * @p:  pointer to rss configuration
2569  */
2570 #if 0
2571 static void
2572 bxe_debug_print_ind_table(struct bxe_softc               *sc,
2573                           struct ecore_config_rss_params *p)
2574 {
2575     int i;
2576
2577     BLOGD(sc, DBG_LOAD, "Setting indirection table to:\n");
2578     BLOGD(sc, DBG_LOAD, "    0x0000: ");
2579     for (i = 0; i < T_ETH_INDIRECTION_TABLE_SIZE; i++) {
2580         BLOGD(sc, DBG_LOAD, "0x%02x ", p->ind_table[i]);
2581
2582         /* Print 4 bytes in a line */
2583         if ((i + 1 < T_ETH_INDIRECTION_TABLE_SIZE) &&
2584             (((i + 1) & 0x3) == 0)) {
2585             BLOGD(sc, DBG_LOAD, "\n");
2586             BLOGD(sc, DBG_LOAD, "0x%04x: ", i + 1);
2587         }
2588     }
2589
2590     BLOGD(sc, DBG_LOAD, "\n");
2591 }
2592 #endif
2593
2594 /*
2595  * FreeBSD Device probe function.
2596  *
2597  * Compares the device found to the driver's list of supported devices and
2598  * reports back to the bsd loader whether this is the right driver for the device.
2599  * This is the driver entry function called from the "kldload" command.
2600  *
2601  * Returns:
2602  *   BUS_PROBE_DEFAULT on success, positive value on failure.
2603  */
2604 static int
2605 bxe_probe(device_t dev)
2606 {
2607     struct bxe_softc *sc;
2608     struct bxe_device_type *t;
2609     char *descbuf;
2610     uint16_t did, sdid, svid, vid;
2611
2612     /* Find our device structure */
2613     sc = device_get_softc(dev);
2614     sc->dev = dev;
2615     t = bxe_devs;
2616
2617     /* Get the data for the device to be probed. */
2618     vid  = pci_get_vendor(dev);
2619     did  = pci_get_device(dev);
2620     svid = pci_get_subvendor(dev);
2621     sdid = pci_get_subdevice(dev);
2622
2623     BLOGD(sc, DBG_LOAD,
2624           "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
2625           "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
2626
2627     /* Look through the list of known devices for a match. */
2628     while (t->bxe_name != NULL) {
2629         if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
2630             ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
2631             ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
2632             descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
2633             if (descbuf == NULL)
2634                 return (ENOMEM);
2635
2636             /* Print out the device identity. */
2637             snprintf(descbuf, BXE_DEVDESC_MAX,
2638                      "%s (%c%d) BXE v:%s\n", t->bxe_name,
2639                      (((pci_read_config(dev, PCIR_REVID, 4) &
2640                         0xf0) >> 4) + 'A'),
2641                      (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
2642                      BXE_DRIVER_VERSION);
2643
2644             device_set_desc_copy(dev, descbuf);
2645             free(descbuf, M_TEMP);
2646             return (BUS_PROBE_DEFAULT);
2647         }
2648         t++;
2649     }
2650
2651     return (ENXIO);
2652 }
2653
2654 static void
2655 bxe_init_mutexes(struct bxe_softc *sc)
2656 {
2657 #ifdef BXE_CORE_LOCK_SX
2658     snprintf(sc->core_sx_name, sizeof(sc->core_sx_name),
2659              "bxe%d_core_lock", sc->unit);
2660     sx_init(&sc->core_sx, sc->core_sx_name);
2661 #else
2662     snprintf(sc->core_mtx_name, sizeof(sc->core_mtx_name),
2663              "bxe%d_core_lock", sc->unit);
2664     mtx_init(&sc->core_mtx, sc->core_mtx_name, NULL, MTX_DEF);
2665 #endif
2666
2667     snprintf(sc->sp_mtx_name, sizeof(sc->sp_mtx_name),
2668              "bxe%d_sp_lock", sc->unit);
2669     mtx_init(&sc->sp_mtx, sc->sp_mtx_name, NULL, MTX_DEF);
2670
2671     snprintf(sc->dmae_mtx_name, sizeof(sc->dmae_mtx_name),
2672              "bxe%d_dmae_lock", sc->unit);
2673     mtx_init(&sc->dmae_mtx, sc->dmae_mtx_name, NULL, MTX_DEF);
2674
2675     snprintf(sc->port.phy_mtx_name, sizeof(sc->port.phy_mtx_name),
2676              "bxe%d_phy_lock", sc->unit);
2677     mtx_init(&sc->port.phy_mtx, sc->port.phy_mtx_name, NULL, MTX_DEF);
2678
2679     snprintf(sc->fwmb_mtx_name, sizeof(sc->fwmb_mtx_name),
2680              "bxe%d_fwmb_lock", sc->unit);
2681     mtx_init(&sc->fwmb_mtx, sc->fwmb_mtx_name, NULL, MTX_DEF);
2682
2683     snprintf(sc->print_mtx_name, sizeof(sc->print_mtx_name),
2684              "bxe%d_print_lock", sc->unit);
2685     mtx_init(&(sc->print_mtx), sc->print_mtx_name, NULL, MTX_DEF);
2686
2687     snprintf(sc->stats_mtx_name, sizeof(sc->stats_mtx_name),
2688              "bxe%d_stats_lock", sc->unit);
2689     mtx_init(&(sc->stats_mtx), sc->stats_mtx_name, NULL, MTX_DEF);
2690
2691     snprintf(sc->mcast_mtx_name, sizeof(sc->mcast_mtx_name),
2692              "bxe%d_mcast_lock", sc->unit);
2693     mtx_init(&(sc->mcast_mtx), sc->mcast_mtx_name, NULL, MTX_DEF);
2694 }
2695
2696 static void
2697 bxe_release_mutexes(struct bxe_softc *sc)
2698 {
2699 #ifdef BXE_CORE_LOCK_SX
2700     sx_destroy(&sc->core_sx);
2701 #else
2702     if (mtx_initialized(&sc->core_mtx)) {
2703         mtx_destroy(&sc->core_mtx);
2704     }
2705 #endif
2706
2707     if (mtx_initialized(&sc->sp_mtx)) {
2708         mtx_destroy(&sc->sp_mtx);
2709     }
2710
2711     if (mtx_initialized(&sc->dmae_mtx)) {
2712         mtx_destroy(&sc->dmae_mtx);
2713     }
2714
2715     if (mtx_initialized(&sc->port.phy_mtx)) {
2716         mtx_destroy(&sc->port.phy_mtx);
2717     }
2718
2719     if (mtx_initialized(&sc->fwmb_mtx)) {
2720         mtx_destroy(&sc->fwmb_mtx);
2721     }
2722
2723     if (mtx_initialized(&sc->print_mtx)) {
2724         mtx_destroy(&sc->print_mtx);
2725     }
2726
2727     if (mtx_initialized(&sc->stats_mtx)) {
2728         mtx_destroy(&sc->stats_mtx);
2729     }
2730
2731     if (mtx_initialized(&sc->mcast_mtx)) {
2732         mtx_destroy(&sc->mcast_mtx);
2733     }
2734 }
2735
2736 static void
2737 bxe_tx_disable(struct bxe_softc* sc)
2738 {
2739     struct ifnet *ifp = sc->ifnet;
2740
2741     /* tell the stack the driver is stopped and TX queue is full */
2742     if (ifp != NULL) {
2743         ifp->if_drv_flags = 0;
2744     }
2745 }
2746
2747 static void
2748 bxe_drv_pulse(struct bxe_softc *sc)
2749 {
2750     SHMEM_WR(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb,
2751              sc->fw_drv_pulse_wr_seq);
2752 }
2753
2754 static inline uint16_t
2755 bxe_tx_avail(struct bxe_softc *sc,
2756              struct bxe_fastpath *fp)
2757 {
2758     int16_t  used;
2759     uint16_t prod;
2760     uint16_t cons;
2761
2762     prod = fp->tx_bd_prod;
2763     cons = fp->tx_bd_cons;
2764
2765     used = SUB_S16(prod, cons);
2766
2767 #if 0
2768     KASSERT((used < 0), ("used tx bds < 0"));
2769     KASSERT((used > sc->tx_ring_size), ("used tx bds > tx_ring_size"));
2770     KASSERT(((sc->tx_ring_size - used) > MAX_TX_AVAIL),
2771             ("invalid number of tx bds used"));
2772 #endif
2773
2774     return (int16_t)(sc->tx_ring_size) - used;
2775 }
2776
2777 static inline int
2778 bxe_tx_queue_has_work(struct bxe_fastpath *fp)
2779 {
2780     uint16_t hw_cons;
2781
2782     mb(); /* status block fields can change */
2783     hw_cons = le16toh(*fp->tx_cons_sb);
2784     return (hw_cons != fp->tx_pkt_cons);
2785 }
2786
2787 static inline uint8_t
2788 bxe_has_tx_work(struct bxe_fastpath *fp)
2789 {
2790     /* expand this for multi-cos if ever supported */
2791     return (bxe_tx_queue_has_work(fp)) ? TRUE : FALSE;
2792 }
2793
2794 static inline int
2795 bxe_has_rx_work(struct bxe_fastpath *fp)
2796 {
2797     uint16_t rx_cq_cons_sb;
2798
2799     mb(); /* status block fields can change */
2800     rx_cq_cons_sb = le16toh(*fp->rx_cq_cons_sb);
2801     if ((rx_cq_cons_sb & RCQ_MAX) == RCQ_MAX)
2802         rx_cq_cons_sb++;
2803     return (fp->rx_cq_cons != rx_cq_cons_sb);
2804 }
2805
2806 static void
2807 bxe_sp_event(struct bxe_softc    *sc,
2808              struct bxe_fastpath *fp,
2809              union eth_rx_cqe    *rr_cqe)
2810 {
2811     int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2812     int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
2813     enum ecore_queue_cmd drv_cmd = ECORE_Q_CMD_MAX;
2814     struct ecore_queue_sp_obj *q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
2815
2816     BLOGD(sc, DBG_SP, "fp=%d cid=%d got ramrod #%d state is %x type is %d\n",
2817           fp->index, cid, command, sc->state, rr_cqe->ramrod_cqe.ramrod_type);
2818
2819 #if 0
2820     /*
2821      * If cid is within VF range, replace the slowpath object with the
2822      * one corresponding to this VF
2823      */
2824     if ((cid >= BXE_FIRST_VF_CID) && (cid < BXE_FIRST_VF_CID + BXE_VF_CIDS)) {
2825         bxe_iov_set_queue_sp_obj(sc, cid, &q_obj);
2826     }
2827 #endif
2828
2829     switch (command) {
2830     case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
2831         BLOGD(sc, DBG_SP, "got UPDATE ramrod. CID %d\n", cid);
2832         drv_cmd = ECORE_Q_CMD_UPDATE;
2833         break;
2834
2835     case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
2836         BLOGD(sc, DBG_SP, "got MULTI[%d] setup ramrod\n", cid);
2837         drv_cmd = ECORE_Q_CMD_SETUP;
2838         break;
2839
2840     case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
2841         BLOGD(sc, DBG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
2842         drv_cmd = ECORE_Q_CMD_SETUP_TX_ONLY;
2843         break;
2844
2845     case (RAMROD_CMD_ID_ETH_HALT):
2846         BLOGD(sc, DBG_SP, "got MULTI[%d] halt ramrod\n", cid);
2847         drv_cmd = ECORE_Q_CMD_HALT;
2848         break;
2849
2850     case (RAMROD_CMD_ID_ETH_TERMINATE):
2851         BLOGD(sc, DBG_SP, "got MULTI[%d] teminate ramrod\n", cid);
2852         drv_cmd = ECORE_Q_CMD_TERMINATE;
2853         break;
2854
2855     case (RAMROD_CMD_ID_ETH_EMPTY):
2856         BLOGD(sc, DBG_SP, "got MULTI[%d] empty ramrod\n", cid);
2857         drv_cmd = ECORE_Q_CMD_EMPTY;
2858         break;
2859
2860     default:
2861         BLOGD(sc, DBG_SP, "ERROR: unexpected MC reply (%d) on fp[%d]\n",
2862               command, fp->index);
2863         return;
2864     }
2865
2866     if ((drv_cmd != ECORE_Q_CMD_MAX) &&
2867         q_obj->complete_cmd(sc, q_obj, drv_cmd)) {
2868         /*
2869          * q_obj->complete_cmd() failure means that this was
2870          * an unexpected completion.
2871          *
2872          * In this case we don't want to increase the sc->spq_left
2873          * because apparently we haven't sent this command the first
2874          * place.
2875          */
2876         // bxe_panic(sc, ("Unexpected SP completion\n"));
2877         return;
2878     }
2879
2880 #if 0
2881     /* SRIOV: reschedule any 'in_progress' operations */
2882     bxe_iov_sp_event(sc, cid, TRUE);
2883 #endif
2884
2885     atomic_add_acq_long(&sc->cq_spq_left, 1);
2886
2887     BLOGD(sc, DBG_SP, "sc->cq_spq_left 0x%lx\n",
2888           atomic_load_acq_long(&sc->cq_spq_left));
2889
2890 #if 0
2891     if ((drv_cmd == ECORE_Q_CMD_UPDATE) && (IS_FCOE_FP(fp)) &&
2892         (!!bxe_test_bit(ECORE_AFEX_FCOE_Q_UPDATE_PENDING, &sc->sp_state))) {
2893         /*
2894          * If Queue update ramrod is completed for last Queue in AFEX VIF set
2895          * flow, then ACK MCP at the end. Mark pending ACK to MCP bit to
2896          * prevent case that both bits are cleared. At the end of load/unload
2897          * driver checks that sp_state is cleared and this order prevents
2898          * races.
2899          */
2900         bxe_set_bit(ECORE_AFEX_PENDING_VIFSET_MCP_ACK, &sc->sp_state);
2901         wmb();
2902         bxe_clear_bit(ECORE_AFEX_FCOE_Q_UPDATE_PENDING, &sc->sp_state);
2903
2904         /* schedule the sp task as MCP ack is required */
2905         bxe_schedule_sp_task(sc);
2906     }
2907 #endif
2908 }
2909
2910 /*
2911  * The current mbuf is part of an aggregation. Move the mbuf into the TPA
2912  * aggregation queue, put an empty mbuf back onto the receive chain, and mark
2913  * the current aggregation queue as in-progress.
2914  */
2915 static void
2916 bxe_tpa_start(struct bxe_softc            *sc,
2917               struct bxe_fastpath         *fp,
2918               uint16_t                    queue,
2919               uint16_t                    cons,
2920               uint16_t                    prod,
2921               struct eth_fast_path_rx_cqe *cqe)
2922 {
2923     struct bxe_sw_rx_bd tmp_bd;
2924     struct bxe_sw_rx_bd *rx_buf;
2925     struct eth_rx_bd *rx_bd;
2926     int max_agg_queues;
2927     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
2928     uint16_t index;
2929
2930     BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA START "
2931                        "cons=%d prod=%d\n",
2932           fp->index, queue, cons, prod);
2933
2934     max_agg_queues = MAX_AGG_QS(sc);
2935
2936     KASSERT((queue < max_agg_queues),
2937             ("fp[%02d] invalid aggr queue (%d >= %d)!",
2938              fp->index, queue, max_agg_queues));
2939
2940     KASSERT((tpa_info->state == BXE_TPA_STATE_STOP),
2941             ("fp[%02d].tpa[%02d] starting aggr on queue not stopped!",
2942              fp->index, queue));
2943
2944     /* copy the existing mbuf and mapping from the TPA pool */
2945     tmp_bd = tpa_info->bd;
2946
2947     if (tmp_bd.m == NULL) {
2948         BLOGE(sc, "fp[%02d].tpa[%02d] mbuf not allocated!\n",
2949               fp->index, queue);
2950         /* XXX Error handling? */
2951         return;
2952     }
2953
2954     /* change the TPA queue to the start state */
2955     tpa_info->state            = BXE_TPA_STATE_START;
2956     tpa_info->placement_offset = cqe->placement_offset;
2957     tpa_info->parsing_flags    = le16toh(cqe->pars_flags.flags);
2958     tpa_info->vlan_tag         = le16toh(cqe->vlan_tag);
2959     tpa_info->len_on_bd        = le16toh(cqe->len_on_bd);
2960
2961     fp->rx_tpa_queue_used |= (1 << queue);
2962
2963     /*
2964      * If all the buffer descriptors are filled with mbufs then fill in
2965      * the current consumer index with a new BD. Else if a maximum Rx
2966      * buffer limit is imposed then fill in the next producer index.
2967      */
2968     index = (sc->max_rx_bufs != RX_BD_USABLE) ?
2969                 prod : cons;
2970
2971     /* move the received mbuf and mapping to TPA pool */
2972     tpa_info->bd = fp->rx_mbuf_chain[cons];
2973
2974     /* release any existing RX BD mbuf mappings */
2975     if (cons != index) {
2976         rx_buf = &fp->rx_mbuf_chain[cons];
2977
2978         if (rx_buf->m_map != NULL) {
2979             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
2980                             BUS_DMASYNC_POSTREAD);
2981             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
2982         }
2983
2984         /*
2985          * We get here when the maximum number of rx buffers is less than
2986          * RX_BD_USABLE. The mbuf is already saved above so it's OK to NULL
2987          * it out here without concern of a memory leak.
2988          */
2989         fp->rx_mbuf_chain[cons].m = NULL;
2990     }
2991
2992     /* update the Rx SW BD with the mbuf info from the TPA pool */
2993     fp->rx_mbuf_chain[index] = tmp_bd;
2994
2995     /* update the Rx BD with the empty mbuf phys address from the TPA pool */
2996     rx_bd = &fp->rx_chain[index];
2997     rx_bd->addr_hi = htole32(U64_HI(tpa_info->seg.ds_addr));
2998     rx_bd->addr_lo = htole32(U64_LO(tpa_info->seg.ds_addr));
2999 }
3000
3001 /*
3002  * When a TPA aggregation is completed, loop through the individual mbufs
3003  * of the aggregation, combining them into a single mbuf which will be sent
3004  * up the stack. Refill all freed SGEs with mbufs as we go along.
3005  */
3006 static int
3007 bxe_fill_frag_mbuf(struct bxe_softc          *sc,
3008                    struct bxe_fastpath       *fp,
3009                    struct bxe_sw_tpa_info    *tpa_info,
3010                    uint16_t                  queue,
3011                    uint16_t                  pages,
3012                    struct mbuf               *m,
3013                                struct eth_end_agg_rx_cqe *cqe,
3014                    uint16_t                  cqe_idx)
3015 {
3016     struct mbuf *m_frag;
3017     uint32_t frag_len, frag_size, i;
3018     uint16_t sge_idx;
3019     int rc = 0;
3020     int j;
3021
3022     frag_size = le16toh(cqe->pkt_len) - tpa_info->len_on_bd;
3023
3024     BLOGD(sc, DBG_LRO,
3025           "fp[%02d].tpa[%02d] TPA fill len_on_bd=%d frag_size=%d pages=%d\n",
3026           fp->index, queue, tpa_info->len_on_bd, frag_size, pages);
3027
3028     /* make sure the aggregated frame is not too big to handle */
3029     if (pages > 8 * PAGES_PER_SGE) {
3030         BLOGE(sc, "fp[%02d].sge[0x%04x] has too many pages (%d)! "
3031                   "pkt_len=%d len_on_bd=%d frag_size=%d\n",
3032               fp->index, cqe_idx, pages, le16toh(cqe->pkt_len),
3033               tpa_info->len_on_bd, frag_size);
3034         bxe_panic(sc, ("sge page count error\n"));
3035         return (EINVAL);
3036     }
3037
3038     /*
3039      * Scan through the scatter gather list pulling individual mbufs into a
3040      * single mbuf for the host stack.
3041      */
3042     for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
3043         sge_idx = RX_SGE(le16toh(cqe->sgl_or_raw_data.sgl[j]));
3044
3045         /*
3046          * Firmware gives the indices of the SGE as if the ring is an array
3047          * (meaning that the "next" element will consume 2 indices).
3048          */
3049         frag_len = min(frag_size, (uint32_t)(SGE_PAGES));
3050
3051         BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA fill i=%d j=%d "
3052                            "sge_idx=%d frag_size=%d frag_len=%d\n",
3053               fp->index, queue, i, j, sge_idx, frag_size, frag_len);
3054
3055         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3056
3057         /* allocate a new mbuf for the SGE */
3058         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3059         if (rc) {
3060             /* Leave all remaining SGEs in the ring! */
3061             return (rc);
3062         }
3063
3064         /* update the fragment length */
3065         m_frag->m_len = frag_len;
3066
3067         /* concatenate the fragment to the head mbuf */
3068         m_cat(m, m_frag);
3069         fp->eth_q_stats.mbuf_alloc_sge--;
3070
3071         /* update the TPA mbuf size and remaining fragment size */
3072         m->m_pkthdr.len += frag_len;
3073         frag_size -= frag_len;
3074     }
3075
3076     BLOGD(sc, DBG_LRO,
3077           "fp[%02d].tpa[%02d] TPA fill done frag_size=%d\n",
3078           fp->index, queue, frag_size);
3079
3080     return (rc);
3081 }
3082
3083 static inline void
3084 bxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
3085 {
3086     int i, j;
3087
3088     for (i = 1; i <= RX_SGE_NUM_PAGES; i++) {
3089         int idx = RX_SGE_TOTAL_PER_PAGE * i - 1;
3090
3091         for (j = 0; j < 2; j++) {
3092             BIT_VEC64_CLEAR_BIT(fp->sge_mask, idx);
3093             idx--;
3094         }
3095     }
3096 }
3097
3098 static inline void
3099 bxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
3100 {
3101     /* set the mask to all 1's, it's faster to compare to 0 than to 0xf's */
3102     memset(fp->sge_mask, 0xff, sizeof(fp->sge_mask));
3103
3104     /*
3105      * Clear the two last indices in the page to 1. These are the indices that
3106      * correspond to the "next" element, hence will never be indicated and
3107      * should be removed from the calculations.
3108      */
3109     bxe_clear_sge_mask_next_elems(fp);
3110 }
3111
3112 static inline void
3113 bxe_update_last_max_sge(struct bxe_fastpath *fp,
3114                         uint16_t            idx)
3115 {
3116     uint16_t last_max = fp->last_max_sge;
3117
3118     if (SUB_S16(idx, last_max) > 0) {
3119         fp->last_max_sge = idx;
3120     }
3121 }
3122
3123 static inline void
3124 bxe_update_sge_prod(struct bxe_softc          *sc,
3125                     struct bxe_fastpath       *fp,
3126                     uint16_t                  sge_len,
3127                     union eth_sgl_or_raw_data *cqe)
3128 {
3129     uint16_t last_max, last_elem, first_elem;
3130     uint16_t delta = 0;
3131     uint16_t i;
3132
3133     if (!sge_len) {
3134         return;
3135     }
3136
3137     /* first mark all used pages */
3138     for (i = 0; i < sge_len; i++) {
3139         BIT_VEC64_CLEAR_BIT(fp->sge_mask,
3140                             RX_SGE(le16toh(cqe->sgl[i])));
3141     }
3142
3143     BLOGD(sc, DBG_LRO,
3144           "fp[%02d] fp_cqe->sgl[%d] = %d\n",
3145           fp->index, sge_len - 1,
3146           le16toh(cqe->sgl[sge_len - 1]));
3147
3148     /* assume that the last SGE index is the biggest */
3149     bxe_update_last_max_sge(fp,
3150                             le16toh(cqe->sgl[sge_len - 1]));
3151
3152     last_max = RX_SGE(fp->last_max_sge);
3153     last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
3154     first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
3155
3156     /* if ring is not full */
3157     if (last_elem + 1 != first_elem) {
3158         last_elem++;
3159     }
3160
3161     /* now update the prod */
3162     for (i = first_elem; i != last_elem; i = RX_SGE_NEXT_MASK_ELEM(i)) {
3163         if (__predict_true(fp->sge_mask[i])) {
3164             break;
3165         }
3166
3167         fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
3168         delta += BIT_VEC64_ELEM_SZ;
3169     }
3170
3171     if (delta > 0) {
3172         fp->rx_sge_prod += delta;
3173         /* clear page-end entries */
3174         bxe_clear_sge_mask_next_elems(fp);
3175     }
3176
3177     BLOGD(sc, DBG_LRO,
3178           "fp[%02d] fp->last_max_sge=%d fp->rx_sge_prod=%d\n",
3179           fp->index, fp->last_max_sge, fp->rx_sge_prod);
3180 }
3181
3182 /*
3183  * The aggregation on the current TPA queue has completed. Pull the individual
3184  * mbuf fragments together into a single mbuf, perform all necessary checksum
3185  * calculations, and send the resuting mbuf to the stack.
3186  */
3187 static void
3188 bxe_tpa_stop(struct bxe_softc          *sc,
3189              struct bxe_fastpath       *fp,
3190              struct bxe_sw_tpa_info    *tpa_info,
3191              uint16_t                  queue,
3192              uint16_t                  pages,
3193                          struct eth_end_agg_rx_cqe *cqe,
3194              uint16_t                  cqe_idx)
3195 {
3196     struct ifnet *ifp = sc->ifnet;
3197     struct mbuf *m;
3198     int rc = 0;
3199
3200     BLOGD(sc, DBG_LRO,
3201           "fp[%02d].tpa[%02d] pad=%d pkt_len=%d pages=%d vlan=%d\n",
3202           fp->index, queue, tpa_info->placement_offset,
3203           le16toh(cqe->pkt_len), pages, tpa_info->vlan_tag);
3204
3205     m = tpa_info->bd.m;
3206
3207     /* allocate a replacement before modifying existing mbuf */
3208     rc = bxe_alloc_rx_tpa_mbuf(fp, queue);
3209     if (rc) {
3210         /* drop the frame and log an error */
3211         fp->eth_q_stats.rx_soft_errors++;
3212         goto bxe_tpa_stop_exit;
3213     }
3214
3215     /* we have a replacement, fixup the current mbuf */
3216     m_adj(m, tpa_info->placement_offset);
3217     m->m_pkthdr.len = m->m_len = tpa_info->len_on_bd;
3218
3219     /* mark the checksums valid (taken care of by the firmware) */
3220     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3221     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3222     m->m_pkthdr.csum_data = 0xffff;
3223     m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED |
3224                                CSUM_IP_VALID   |
3225                                CSUM_DATA_VALID |
3226                                CSUM_PSEUDO_HDR);
3227
3228     /* aggregate all of the SGEs into a single mbuf */
3229     rc = bxe_fill_frag_mbuf(sc, fp, tpa_info, queue, pages, m, cqe, cqe_idx);
3230     if (rc) {
3231         /* drop the packet and log an error */
3232         fp->eth_q_stats.rx_soft_errors++;
3233         m_freem(m);
3234     } else {
3235         if (tpa_info->parsing_flags & PARSING_FLAGS_VLAN) {
3236             m->m_pkthdr.ether_vtag = tpa_info->vlan_tag;
3237             m->m_flags |= M_VLANTAG;
3238         }
3239
3240         /* assign packet to this interface interface */
3241         m->m_pkthdr.rcvif = ifp;
3242
3243 #if __FreeBSD_version >= 800000
3244         /* specify what RSS queue was used for this flow */
3245         m->m_pkthdr.flowid = fp->index;
3246         m->m_flags |= M_FLOWID;
3247 #endif
3248
3249         ifp->if_ipackets++;
3250         fp->eth_q_stats.rx_tpa_pkts++;
3251
3252         /* pass the frame to the stack */
3253         (*ifp->if_input)(ifp, m);
3254     }
3255
3256     /* we passed an mbuf up the stack or dropped the frame */
3257     fp->eth_q_stats.mbuf_alloc_tpa--;
3258
3259 bxe_tpa_stop_exit:
3260
3261     fp->rx_tpa_info[queue].state = BXE_TPA_STATE_STOP;
3262     fp->rx_tpa_queue_used &= ~(1 << queue);
3263 }
3264
3265 static uint8_t
3266 bxe_service_rxsgl(
3267                  struct bxe_fastpath *fp,
3268                  uint16_t len,
3269                  uint16_t lenonbd,
3270                  struct mbuf *m,
3271                  struct eth_fast_path_rx_cqe *cqe_fp)
3272 {
3273     struct mbuf *m_frag;
3274     uint16_t frags, frag_len;
3275     uint16_t sge_idx = 0;
3276     uint16_t j;
3277     uint8_t i, rc = 0;
3278     uint32_t frag_size;
3279
3280     /* adjust the mbuf */
3281     m->m_len = lenonbd;
3282
3283     frag_size =  len - lenonbd;
3284     frags = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3285
3286     for (i = 0, j = 0; i < frags; i += PAGES_PER_SGE, j++) {
3287         sge_idx = RX_SGE(le16toh(cqe_fp->sgl_or_raw_data.sgl[j]));
3288
3289         m_frag = fp->rx_sge_mbuf_chain[sge_idx].m;
3290         frag_len = min(frag_size, (uint32_t)(SGE_PAGE_SIZE));
3291         m_frag->m_len = frag_len;
3292
3293        /* allocate a new mbuf for the SGE */
3294         rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
3295         if (rc) {
3296             /* Leave all remaining SGEs in the ring! */
3297             return (rc);
3298         }
3299         fp->eth_q_stats.mbuf_alloc_sge--;
3300
3301         /* concatenate the fragment to the head mbuf */
3302         m_cat(m, m_frag);
3303
3304         frag_size -= frag_len;
3305     }
3306
3307     bxe_update_sge_prod(fp->sc, fp, frags, &cqe_fp->sgl_or_raw_data);
3308
3309     return rc;
3310 }
3311
3312 static uint8_t
3313 bxe_rxeof(struct bxe_softc    *sc,
3314           struct bxe_fastpath *fp)
3315 {
3316     struct ifnet *ifp = sc->ifnet;
3317     uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
3318     uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
3319     int rx_pkts = 0;
3320     int rc = 0;
3321
3322     BXE_FP_RX_LOCK(fp);
3323
3324     /* CQ "next element" is of the size of the regular element */
3325     hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
3326     if ((hw_cq_cons & RCQ_USABLE_PER_PAGE) == RCQ_USABLE_PER_PAGE) {
3327         hw_cq_cons++;
3328     }
3329
3330     bd_cons = fp->rx_bd_cons;
3331     bd_prod = fp->rx_bd_prod;
3332     bd_prod_fw = bd_prod;
3333     sw_cq_cons = fp->rx_cq_cons;
3334     sw_cq_prod = fp->rx_cq_prod;
3335
3336     /*
3337      * Memory barrier necessary as speculative reads of the rx
3338      * buffer can be ahead of the index in the status block
3339      */
3340     rmb();
3341
3342     BLOGD(sc, DBG_RX,
3343           "fp[%02d] Rx START hw_cq_cons=%u sw_cq_cons=%u\n",
3344           fp->index, hw_cq_cons, sw_cq_cons);
3345
3346     while (sw_cq_cons != hw_cq_cons) {
3347         struct bxe_sw_rx_bd *rx_buf = NULL;
3348         union eth_rx_cqe *cqe;
3349         struct eth_fast_path_rx_cqe *cqe_fp;
3350         uint8_t cqe_fp_flags;
3351         enum eth_rx_cqe_type cqe_fp_type;
3352         uint16_t len, lenonbd,  pad;
3353         struct mbuf *m = NULL;
3354
3355         comp_ring_cons = RCQ(sw_cq_cons);
3356         bd_prod = RX_BD(bd_prod);
3357         bd_cons = RX_BD(bd_cons);
3358
3359         cqe          = &fp->rcq_chain[comp_ring_cons];
3360         cqe_fp       = &cqe->fast_path_cqe;
3361         cqe_fp_flags = cqe_fp->type_error_flags;
3362         cqe_fp_type  = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
3363
3364         BLOGD(sc, DBG_RX,
3365               "fp[%02d] Rx hw_cq_cons=%d hw_sw_cons=%d "
3366               "BD prod=%d cons=%d CQE type=0x%x err=0x%x "
3367               "status=0x%x rss_hash=0x%x vlan=0x%x len=%u lenonbd=%u\n",
3368               fp->index,
3369               hw_cq_cons,
3370               sw_cq_cons,
3371               bd_prod,
3372               bd_cons,
3373               CQE_TYPE(cqe_fp_flags),
3374               cqe_fp_flags,
3375               cqe_fp->status_flags,
3376               le32toh(cqe_fp->rss_hash_result),
3377               le16toh(cqe_fp->vlan_tag),
3378               le16toh(cqe_fp->pkt_len_or_gro_seg_len),
3379               le16toh(cqe_fp->len_on_bd));
3380
3381         /* is this a slowpath msg? */
3382         if (__predict_false(CQE_TYPE_SLOW(cqe_fp_type))) {
3383             bxe_sp_event(sc, fp, cqe);
3384             goto next_cqe;
3385         }
3386
3387         rx_buf = &fp->rx_mbuf_chain[bd_cons];
3388
3389         if (!CQE_TYPE_FAST(cqe_fp_type)) {
3390             struct bxe_sw_tpa_info *tpa_info;
3391             uint16_t frag_size, pages;
3392             uint8_t queue;
3393
3394 #if 0
3395             /* sanity check */
3396             if (!fp->tpa_enable &&
3397                 (CQE_TYPE_START(cqe_fp_type) || CQE_TYPE_STOP(cqe_fp_type))) {
3398                 BLOGE(sc, "START/STOP packet while !tpa_enable type (0x%x)\n",
3399                       CQE_TYPE(cqe_fp_type));
3400             }
3401 #endif
3402
3403             if (CQE_TYPE_START(cqe_fp_type)) {
3404                 bxe_tpa_start(sc, fp, cqe_fp->queue_index,
3405                               bd_cons, bd_prod, cqe_fp);
3406                 m = NULL; /* packet not ready yet */
3407                 goto next_rx;
3408             }
3409
3410             KASSERT(CQE_TYPE_STOP(cqe_fp_type),
3411                     ("CQE type is not STOP! (0x%x)\n", cqe_fp_type));
3412
3413             queue = cqe->end_agg_cqe.queue_index;
3414             tpa_info = &fp->rx_tpa_info[queue];
3415
3416             BLOGD(sc, DBG_LRO, "fp[%02d].tpa[%02d] TPA STOP\n",
3417                   fp->index, queue);
3418
3419             frag_size = (le16toh(cqe->end_agg_cqe.pkt_len) -
3420                          tpa_info->len_on_bd);
3421             pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
3422
3423             bxe_tpa_stop(sc, fp, tpa_info, queue, pages,
3424                          &cqe->end_agg_cqe, comp_ring_cons);
3425
3426             bxe_update_sge_prod(sc, fp, pages, &cqe->end_agg_cqe.sgl_or_raw_data);
3427
3428             goto next_cqe;
3429         }
3430
3431         /* non TPA */
3432
3433         /* is this an error packet? */
3434         if (__predict_false(cqe_fp_flags &
3435                             ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
3436             BLOGE(sc, "flags 0x%x rx packet %u\n", cqe_fp_flags, sw_cq_cons);
3437             fp->eth_q_stats.rx_soft_errors++;
3438             goto next_rx;
3439         }
3440
3441         len = le16toh(cqe_fp->pkt_len_or_gro_seg_len);
3442         lenonbd = le16toh(cqe_fp->len_on_bd);
3443         pad = cqe_fp->placement_offset;
3444
3445         m = rx_buf->m;
3446
3447         if (__predict_false(m == NULL)) {
3448             BLOGE(sc, "No mbuf in rx chain descriptor %d for fp[%02d]\n",
3449                   bd_cons, fp->index);
3450             goto next_rx;
3451         }
3452
3453         /* XXX double copy if packet length under a threshold */
3454
3455         /*
3456          * If all the buffer descriptors are filled with mbufs then fill in
3457          * the current consumer index with a new BD. Else if a maximum Rx
3458          * buffer limit is imposed then fill in the next producer index.
3459          */
3460         rc = bxe_alloc_rx_bd_mbuf(fp, bd_cons,
3461                                   (sc->max_rx_bufs != RX_BD_USABLE) ?
3462                                       bd_prod : bd_cons);
3463         if (rc != 0) {
3464
3465             /* we simply reuse the received mbuf and don't post it to the stack */
3466             m = NULL;
3467
3468             BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
3469                   fp->index, rc);
3470             fp->eth_q_stats.rx_soft_errors++;
3471
3472             if (sc->max_rx_bufs != RX_BD_USABLE) {
3473                 /* copy this consumer index to the producer index */
3474                 memcpy(&fp->rx_mbuf_chain[bd_prod], rx_buf,
3475                        sizeof(struct bxe_sw_rx_bd));
3476                 memset(rx_buf, 0, sizeof(struct bxe_sw_rx_bd));
3477             }
3478
3479             goto next_rx;
3480         }
3481
3482         /* current mbuf was detached from the bd */
3483         fp->eth_q_stats.mbuf_alloc_rx--;
3484
3485         /* we allocated a replacement mbuf, fixup the current one */
3486         m_adj(m, pad);
3487         m->m_pkthdr.len = m->m_len = len;
3488
3489         if (len != lenonbd){
3490             rc = bxe_service_rxsgl(fp, len, lenonbd, m, cqe_fp);
3491             if (rc)
3492                 break;
3493             fp->eth_q_stats.rx_jumbo_sge_pkts++;
3494         }
3495
3496         /* assign packet to this interface interface */
3497         m->m_pkthdr.rcvif = ifp;
3498
3499         /* assume no hardware checksum has complated */
3500         m->m_pkthdr.csum_flags = 0;
3501
3502         /* validate checksum if offload enabled */
3503         if (ifp->if_capenable & IFCAP_RXCSUM) {
3504             /* check for a valid IP frame */
3505             if (!(cqe->fast_path_cqe.status_flags &
3506                   ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
3507                 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3508                 if (__predict_false(cqe_fp_flags &
3509                                     ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
3510                     fp->eth_q_stats.rx_hw_csum_errors++;
3511                 } else {
3512                     fp->eth_q_stats.rx_ofld_frames_csum_ip++;
3513                     m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3514                 }
3515             }
3516
3517             /* check for a valid TCP/UDP frame */
3518             if (!(cqe->fast_path_cqe.status_flags &
3519                   ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
3520                 if (__predict_false(cqe_fp_flags &
3521                                     ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
3522                     fp->eth_q_stats.rx_hw_csum_errors++;
3523                 } else {
3524                     fp->eth_q_stats.rx_ofld_frames_csum_tcp_udp++;
3525                     m->m_pkthdr.csum_data = 0xFFFF;
3526                     m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
3527                                                CSUM_PSEUDO_HDR);
3528                 }
3529             }
3530         }
3531
3532         /* if there is a VLAN tag then flag that info */
3533         if (cqe->fast_path_cqe.pars_flags.flags & PARSING_FLAGS_VLAN) {
3534             m->m_pkthdr.ether_vtag = cqe->fast_path_cqe.vlan_tag;
3535             m->m_flags |= M_VLANTAG;
3536         }
3537
3538 #if __FreeBSD_version >= 800000
3539         /* specify what RSS queue was used for this flow */
3540         m->m_pkthdr.flowid = fp->index;
3541         m->m_flags |= M_FLOWID;
3542 #endif
3543
3544 next_rx:
3545
3546         bd_cons    = RX_BD_NEXT(bd_cons);
3547         bd_prod    = RX_BD_NEXT(bd_prod);
3548         bd_prod_fw = RX_BD_NEXT(bd_prod_fw);
3549
3550         /* pass the frame to the stack */
3551         if (__predict_true(m != NULL)) {
3552             ifp->if_ipackets++;
3553             rx_pkts++;
3554             (*ifp->if_input)(ifp, m);
3555         }
3556
3557 next_cqe:
3558
3559         sw_cq_prod = RCQ_NEXT(sw_cq_prod);
3560         sw_cq_cons = RCQ_NEXT(sw_cq_cons);
3561
3562         /* limit spinning on the queue */
3563         if (rc != 0)
3564             break;
3565
3566         if (rx_pkts == sc->rx_budget) {
3567             fp->eth_q_stats.rx_budget_reached++;
3568             break;
3569         }
3570     } /* while work to do */
3571
3572     fp->rx_bd_cons = bd_cons;
3573     fp->rx_bd_prod = bd_prod_fw;
3574     fp->rx_cq_cons = sw_cq_cons;
3575     fp->rx_cq_prod = sw_cq_prod;
3576
3577     /* Update producers */
3578     bxe_update_rx_prod(sc, fp, bd_prod_fw, sw_cq_prod, fp->rx_sge_prod);
3579
3580     fp->eth_q_stats.rx_pkts += rx_pkts;
3581     fp->eth_q_stats.rx_calls++;
3582
3583     BXE_FP_RX_UNLOCK(fp);
3584
3585     return (sw_cq_cons != hw_cq_cons);
3586 }
3587
3588 static uint16_t
3589 bxe_free_tx_pkt(struct bxe_softc    *sc,
3590                 struct bxe_fastpath *fp,
3591                 uint16_t            idx)
3592 {
3593     struct bxe_sw_tx_bd *tx_buf = &fp->tx_mbuf_chain[idx];
3594     struct eth_tx_start_bd *tx_start_bd;
3595     uint16_t bd_idx = TX_BD(tx_buf->first_bd);
3596     uint16_t new_cons;
3597     int nbd;
3598
3599     /* unmap the mbuf from non-paged memory */
3600     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
3601
3602     tx_start_bd = &fp->tx_chain[bd_idx].start_bd;
3603     nbd = le16toh(tx_start_bd->nbd) - 1;
3604
3605 #if 0
3606     if ((nbd - 1) > (MAX_MBUF_FRAGS + 2)) {
3607         bxe_panic(sc, ("BAD nbd!\n"));
3608     }
3609 #endif
3610
3611     new_cons = (tx_buf->first_bd + nbd);
3612
3613 #if 0
3614     struct eth_tx_bd *tx_data_bd;
3615
3616     /*
3617      * The following code doesn't do anything but is left here
3618      * for clarity on what the new value of new_cons skipped.
3619      */
3620
3621     /* get the next bd */
3622     bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3623
3624     /* skip the parse bd */
3625     --nbd;
3626     bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3627
3628     /* skip the TSO split header bd since they have no mapping */
3629     if (tx_buf->flags & BXE_TSO_SPLIT_BD) {
3630         --nbd;
3631         bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3632     }
3633
3634     /* now free frags */
3635     while (nbd > 0) {
3636         tx_data_bd = &fp->tx_chain[bd_idx].reg_bd;
3637         if (--nbd) {
3638             bd_idx = TX_BD(TX_BD_NEXT(bd_idx));
3639         }
3640     }
3641 #endif
3642
3643     /* free the mbuf */
3644     if (__predict_true(tx_buf->m != NULL)) {
3645         m_freem(tx_buf->m);
3646         fp->eth_q_stats.mbuf_alloc_tx--;
3647     } else {
3648         fp->eth_q_stats.tx_chain_lost_mbuf++;
3649     }
3650
3651     tx_buf->m = NULL;
3652     tx_buf->first_bd = 0;
3653
3654     return (new_cons);
3655 }
3656
3657 /* transmit timeout watchdog */
3658 static int
3659 bxe_watchdog(struct bxe_softc    *sc,
3660              struct bxe_fastpath *fp)
3661 {
3662     BXE_FP_TX_LOCK(fp);
3663
3664     if ((fp->watchdog_timer == 0) || (--fp->watchdog_timer)) {
3665         BXE_FP_TX_UNLOCK(fp);
3666         return (0);
3667     }
3668
3669     BLOGE(sc, "TX watchdog timeout on fp[%02d], resetting!\n", fp->index);
3670
3671     BXE_FP_TX_UNLOCK(fp);
3672
3673     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_REINIT);
3674     taskqueue_enqueue(sc->chip_tq, &sc->chip_tq_task);
3675
3676     return (-1);
3677 }
3678
3679 /* processes transmit completions */
3680 static uint8_t
3681 bxe_txeof(struct bxe_softc    *sc,
3682           struct bxe_fastpath *fp)
3683 {
3684     struct ifnet *ifp = sc->ifnet;
3685     uint16_t bd_cons, hw_cons, sw_cons, pkt_cons;
3686     uint16_t tx_bd_avail;
3687
3688     BXE_FP_TX_LOCK_ASSERT(fp);
3689
3690     bd_cons = fp->tx_bd_cons;
3691     hw_cons = le16toh(*fp->tx_cons_sb);
3692     sw_cons = fp->tx_pkt_cons;
3693
3694     while (sw_cons != hw_cons) {
3695         pkt_cons = TX_BD(sw_cons);
3696
3697         BLOGD(sc, DBG_TX,
3698               "TX: fp[%d]: hw_cons=%u sw_cons=%u pkt_cons=%u\n",
3699               fp->index, hw_cons, sw_cons, pkt_cons);
3700
3701         bd_cons = bxe_free_tx_pkt(sc, fp, pkt_cons);
3702
3703         sw_cons++;
3704     }
3705
3706     fp->tx_pkt_cons = sw_cons;
3707     fp->tx_bd_cons  = bd_cons;
3708
3709     BLOGD(sc, DBG_TX,
3710           "TX done: fp[%d]: hw_cons=%u sw_cons=%u sw_prod=%u\n",
3711           fp->index, hw_cons, fp->tx_pkt_cons, fp->tx_pkt_prod);
3712
3713     mb();
3714
3715     tx_bd_avail = bxe_tx_avail(sc, fp);
3716
3717     if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
3718         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3719     } else {
3720         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3721     }
3722
3723     if (fp->tx_pkt_prod != fp->tx_pkt_cons) {
3724         /* reset the watchdog timer if there are pending transmits */
3725         fp->watchdog_timer = BXE_TX_TIMEOUT;
3726         return (TRUE);
3727     } else {
3728         /* clear watchdog when there are no pending transmits */
3729         fp->watchdog_timer = 0;
3730         return (FALSE);
3731     }
3732 }
3733
3734 static void
3735 bxe_drain_tx_queues(struct bxe_softc *sc)
3736 {
3737     struct bxe_fastpath *fp;
3738     int i, count;
3739
3740     /* wait until all TX fastpath tasks have completed */
3741     for (i = 0; i < sc->num_queues; i++) {
3742         fp = &sc->fp[i];
3743
3744         count = 1000;
3745
3746         while (bxe_has_tx_work(fp)) {
3747
3748             BXE_FP_TX_LOCK(fp);
3749             bxe_txeof(sc, fp);
3750             BXE_FP_TX_UNLOCK(fp);
3751
3752             if (count == 0) {
3753                 BLOGE(sc, "Timeout waiting for fp[%d] "
3754                           "transmits to complete!\n", i);
3755                 bxe_panic(sc, ("tx drain failure\n"));
3756                 return;
3757             }
3758
3759             count--;
3760             DELAY(1000);
3761             rmb();
3762         }
3763     }
3764
3765     return;
3766 }
3767
3768 static int
3769 bxe_del_all_macs(struct bxe_softc          *sc,
3770                  struct ecore_vlan_mac_obj *mac_obj,
3771                  int                       mac_type,
3772                  uint8_t                   wait_for_comp)
3773 {
3774     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
3775     int rc;
3776
3777     /* wait for completion of requested */
3778     if (wait_for_comp) {
3779         bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
3780     }
3781
3782     /* Set the mac type of addresses we want to clear */
3783     bxe_set_bit(mac_type, &vlan_mac_flags);
3784
3785     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags, &ramrod_flags);
3786     if (rc < 0) {
3787         BLOGE(sc, "Failed to delete MACs (%d)\n", rc);
3788     }
3789
3790     return (rc);
3791 }
3792
3793 static int
3794 bxe_fill_accept_flags(struct bxe_softc *sc,
3795                       uint32_t         rx_mode,
3796                       unsigned long    *rx_accept_flags,
3797                       unsigned long    *tx_accept_flags)
3798 {
3799     /* Clear the flags first */
3800     *rx_accept_flags = 0;
3801     *tx_accept_flags = 0;
3802
3803     switch (rx_mode) {
3804     case BXE_RX_MODE_NONE:
3805         /*
3806          * 'drop all' supersedes any accept flags that may have been
3807          * passed to the function.
3808          */
3809         break;
3810
3811     case BXE_RX_MODE_NORMAL:
3812         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3813         bxe_set_bit(ECORE_ACCEPT_MULTICAST, rx_accept_flags);
3814         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3815
3816         /* internal switching mode */
3817         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3818         bxe_set_bit(ECORE_ACCEPT_MULTICAST, tx_accept_flags);
3819         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3820
3821         break;
3822
3823     case BXE_RX_MODE_ALLMULTI:
3824         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3825         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3826         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3827
3828         /* internal switching mode */
3829         bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3830         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3831         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3832
3833         break;
3834
3835     case BXE_RX_MODE_PROMISC:
3836         /*
3837          * According to deffinition of SI mode, iface in promisc mode
3838          * should receive matched and unmatched (in resolution of port)
3839          * unicast packets.
3840          */
3841         bxe_set_bit(ECORE_ACCEPT_UNMATCHED, rx_accept_flags);
3842         bxe_set_bit(ECORE_ACCEPT_UNICAST, rx_accept_flags);
3843         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, rx_accept_flags);
3844         bxe_set_bit(ECORE_ACCEPT_BROADCAST, rx_accept_flags);
3845
3846         /* internal switching mode */
3847         bxe_set_bit(ECORE_ACCEPT_ALL_MULTICAST, tx_accept_flags);
3848         bxe_set_bit(ECORE_ACCEPT_BROADCAST, tx_accept_flags);
3849
3850         if (IS_MF_SI(sc)) {
3851             bxe_set_bit(ECORE_ACCEPT_ALL_UNICAST, tx_accept_flags);
3852         } else {
3853             bxe_set_bit(ECORE_ACCEPT_UNICAST, tx_accept_flags);
3854         }
3855
3856         break;
3857
3858     default:
3859         BLOGE(sc, "Unknown rx_mode (%d)\n", rx_mode);
3860         return (-1);
3861     }
3862
3863     /* Set ACCEPT_ANY_VLAN as we do not enable filtering by VLAN */
3864     if (rx_mode != BXE_RX_MODE_NONE) {
3865         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, rx_accept_flags);
3866         bxe_set_bit(ECORE_ACCEPT_ANY_VLAN, tx_accept_flags);
3867     }
3868
3869     return (0);
3870 }
3871
3872 static int
3873 bxe_set_q_rx_mode(struct bxe_softc *sc,
3874                   uint8_t          cl_id,
3875                   unsigned long    rx_mode_flags,
3876                   unsigned long    rx_accept_flags,
3877                   unsigned long    tx_accept_flags,
3878                   unsigned long    ramrod_flags)
3879 {
3880     struct ecore_rx_mode_ramrod_params ramrod_param;
3881     int rc;
3882
3883     memset(&ramrod_param, 0, sizeof(ramrod_param));
3884
3885     /* Prepare ramrod parameters */
3886     ramrod_param.cid = 0;
3887     ramrod_param.cl_id = cl_id;
3888     ramrod_param.rx_mode_obj = &sc->rx_mode_obj;
3889     ramrod_param.func_id = SC_FUNC(sc);
3890
3891     ramrod_param.pstate = &sc->sp_state;
3892     ramrod_param.state = ECORE_FILTER_RX_MODE_PENDING;
3893
3894     ramrod_param.rdata = BXE_SP(sc, rx_mode_rdata);
3895     ramrod_param.rdata_mapping = BXE_SP_MAPPING(sc, rx_mode_rdata);
3896
3897     bxe_set_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
3898
3899     ramrod_param.ramrod_flags = ramrod_flags;
3900     ramrod_param.rx_mode_flags = rx_mode_flags;
3901
3902     ramrod_param.rx_accept_flags = rx_accept_flags;
3903     ramrod_param.tx_accept_flags = tx_accept_flags;
3904
3905     rc = ecore_config_rx_mode(sc, &ramrod_param);
3906     if (rc < 0) {
3907         BLOGE(sc, "Set rx_mode %d failed\n", sc->rx_mode);
3908         return (rc);
3909     }
3910
3911     return (0);
3912 }
3913
3914 static int
3915 bxe_set_storm_rx_mode(struct bxe_softc *sc)
3916 {
3917     unsigned long rx_mode_flags = 0, ramrod_flags = 0;
3918     unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
3919     int rc;
3920
3921     rc = bxe_fill_accept_flags(sc, sc->rx_mode, &rx_accept_flags,
3922                                &tx_accept_flags);
3923     if (rc) {
3924         return (rc);
3925     }
3926
3927     bxe_set_bit(RAMROD_RX, &ramrod_flags);
3928     bxe_set_bit(RAMROD_TX, &ramrod_flags);
3929
3930     /* XXX ensure all fastpath have same cl_id and/or move it to bxe_softc */
3931     return (bxe_set_q_rx_mode(sc, sc->fp[0].cl_id, rx_mode_flags,
3932                               rx_accept_flags, tx_accept_flags,
3933                               ramrod_flags));
3934 }
3935
3936 /* returns the "mcp load_code" according to global load_count array */
3937 static int
3938 bxe_nic_load_no_mcp(struct bxe_softc *sc)
3939 {
3940     int path = SC_PATH(sc);
3941     int port = SC_PORT(sc);
3942
3943     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3944           path, load_count[path][0], load_count[path][1],
3945           load_count[path][2]);
3946     load_count[path][0]++;
3947     load_count[path][1 + port]++;
3948     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3949           path, load_count[path][0], load_count[path][1],
3950           load_count[path][2]);
3951     if (load_count[path][0] == 1) {
3952         return (FW_MSG_CODE_DRV_LOAD_COMMON);
3953     } else if (load_count[path][1 + port] == 1) {
3954         return (FW_MSG_CODE_DRV_LOAD_PORT);
3955     } else {
3956         return (FW_MSG_CODE_DRV_LOAD_FUNCTION);
3957     }
3958 }
3959
3960 /* returns the "mcp load_code" according to global load_count array */
3961 static int
3962 bxe_nic_unload_no_mcp(struct bxe_softc *sc)
3963 {
3964     int port = SC_PORT(sc);
3965     int path = SC_PATH(sc);
3966
3967     BLOGI(sc, "NO MCP - load counts[%d]      %d, %d, %d\n",
3968           path, load_count[path][0], load_count[path][1],
3969           load_count[path][2]);
3970     load_count[path][0]--;
3971     load_count[path][1 + port]--;
3972     BLOGI(sc, "NO MCP - new load counts[%d]  %d, %d, %d\n",
3973           path, load_count[path][0], load_count[path][1],
3974           load_count[path][2]);
3975     if (load_count[path][0] == 0) {
3976         return (FW_MSG_CODE_DRV_UNLOAD_COMMON);
3977     } else if (load_count[path][1 + port] == 0) {
3978         return (FW_MSG_CODE_DRV_UNLOAD_PORT);
3979     } else {
3980         return (FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
3981     }
3982 }
3983
3984 /* request unload mode from the MCP: COMMON, PORT or FUNCTION */
3985 static uint32_t
3986 bxe_send_unload_req(struct bxe_softc *sc,
3987                     int              unload_mode)
3988 {
3989     uint32_t reset_code = 0;
3990 #if 0
3991     int port = SC_PORT(sc);
3992     int path = SC_PATH(sc);
3993 #endif
3994
3995     /* Select the UNLOAD request mode */
3996     if (unload_mode == UNLOAD_NORMAL) {
3997         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3998     }
3999 #if 0
4000     else if (sc->flags & BXE_NO_WOL_FLAG) {
4001         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP;
4002     } else if (sc->wol) {
4003         uint32_t emac_base = port ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
4004         uint8_t *mac_addr = sc->dev->dev_addr;
4005         uint32_t val;
4006         uint16_t pmc;
4007
4008         /*
4009          * The mac address is written to entries 1-4 to
4010          * preserve entry 0 which is used by the PMF
4011          */
4012         uint8_t entry = (SC_VN(sc) + 1)*8;
4013
4014         val = (mac_addr[0] << 8) | mac_addr[1];
4015         EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry, val);
4016
4017         val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
4018               (mac_addr[4] << 8) | mac_addr[5];
4019         EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry + 4, val);
4020
4021         /* Enable the PME and clear the status */
4022         pmc = pci_read_config(sc->dev,
4023                               (sc->devinfo.pcie_pm_cap_reg +
4024                                PCIR_POWER_STATUS),
4025                               2);
4026         pmc |= PCIM_PSTAT_PMEENABLE | PCIM_PSTAT_PME;
4027         pci_write_config(sc->dev,
4028                          (sc->devinfo.pcie_pm_cap_reg +
4029                           PCIR_POWER_STATUS),
4030                          pmc, 4);
4031
4032         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_EN;
4033     }
4034 #endif
4035     else {
4036         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
4037     }
4038
4039     /* Send the request to the MCP */
4040     if (!BXE_NOMCP(sc)) {
4041         reset_code = bxe_fw_command(sc, reset_code, 0);
4042     } else {
4043         reset_code = bxe_nic_unload_no_mcp(sc);
4044     }
4045
4046     return (reset_code);
4047 }
4048
4049 /* send UNLOAD_DONE command to the MCP */
4050 static void
4051 bxe_send_unload_done(struct bxe_softc *sc,
4052                      uint8_t          keep_link)
4053 {
4054     uint32_t reset_param =
4055         keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
4056
4057     /* Report UNLOAD_DONE to MCP */
4058     if (!BXE_NOMCP(sc)) {
4059         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
4060     }
4061 }
4062
4063 static int
4064 bxe_func_wait_started(struct bxe_softc *sc)
4065 {
4066     int tout = 50;
4067
4068     if (!sc->port.pmf) {
4069         return (0);
4070     }
4071
4072     /*
4073      * (assumption: No Attention from MCP at this stage)
4074      * PMF probably in the middle of TX disable/enable transaction
4075      * 1. Sync IRS for default SB
4076      * 2. Sync SP queue - this guarantees us that attention handling started
4077      * 3. Wait, that TX disable/enable transaction completes
4078      *
4079      * 1+2 guarantee that if DCBX attention was scheduled it already changed
4080      * pending bit of transaction from STARTED-->TX_STOPPED, if we already
4081      * received completion for the transaction the state is TX_STOPPED.
4082      * State will return to STARTED after completion of TX_STOPPED-->STARTED
4083      * transaction.
4084      */
4085
4086     /* XXX make sure default SB ISR is done */
4087     /* need a way to synchronize an irq (intr_mtx?) */
4088
4089     /* XXX flush any work queues */
4090
4091     while (ecore_func_get_state(sc, &sc->func_obj) !=
4092            ECORE_F_STATE_STARTED && tout--) {
4093         DELAY(20000);
4094     }
4095
4096     if (ecore_func_get_state(sc, &sc->func_obj) != ECORE_F_STATE_STARTED) {
4097         /*
4098          * Failed to complete the transaction in a "good way"
4099          * Force both transactions with CLR bit.
4100          */
4101         struct ecore_func_state_params func_params = { NULL };
4102
4103         BLOGE(sc, "Unexpected function state! "
4104                   "Forcing STARTED-->TX_STOPPED-->STARTED\n");
4105
4106         func_params.f_obj = &sc->func_obj;
4107         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
4108
4109         /* STARTED-->TX_STOPPED */
4110         func_params.cmd = ECORE_F_CMD_TX_STOP;
4111         ecore_func_state_change(sc, &func_params);
4112
4113         /* TX_STOPPED-->STARTED */
4114         func_params.cmd = ECORE_F_CMD_TX_START;
4115         return (ecore_func_state_change(sc, &func_params));
4116     }
4117
4118     return (0);
4119 }
4120
4121 static int
4122 bxe_stop_queue(struct bxe_softc *sc,
4123                int              index)
4124 {
4125     struct bxe_fastpath *fp = &sc->fp[index];
4126     struct ecore_queue_state_params q_params = { NULL };
4127     int rc;
4128
4129     BLOGD(sc, DBG_LOAD, "stopping queue %d cid %d\n", index, fp->index);
4130
4131     q_params.q_obj = &sc->sp_objs[fp->index].q_obj;
4132     /* We want to wait for completion in this context */
4133     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
4134
4135     /* Stop the primary connection: */
4136
4137     /* ...halt the connection */
4138     q_params.cmd = ECORE_Q_CMD_HALT;
4139     rc = ecore_queue_state_change(sc, &q_params);
4140     if (rc) {
4141         return (rc);
4142     }
4143
4144     /* ...terminate the connection */
4145     q_params.cmd = ECORE_Q_CMD_TERMINATE;
4146     memset(&q_params.params.terminate, 0, sizeof(q_params.params.terminate));
4147     q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
4148     rc = ecore_queue_state_change(sc, &q_params);
4149     if (rc) {
4150         return (rc);
4151     }
4152
4153     /* ...delete cfc entry */
4154     q_params.cmd = ECORE_Q_CMD_CFC_DEL;
4155     memset(&q_params.params.cfc_del, 0, sizeof(q_params.params.cfc_del));
4156     q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
4157     return (ecore_queue_state_change(sc, &q_params));
4158 }
4159
4160 /* wait for the outstanding SP commands */
4161 static inline uint8_t
4162 bxe_wait_sp_comp(struct bxe_softc *sc,
4163                  unsigned long    mask)
4164 {
4165     unsigned long tmp;
4166     int tout = 5000; /* wait for 5 secs tops */
4167
4168     while (tout--) {
4169         mb();
4170         if (!(atomic_load_acq_long(&sc->sp_state) & mask)) {
4171             return (TRUE);
4172         }
4173
4174         DELAY(1000);
4175     }
4176
4177     mb();
4178
4179     tmp = atomic_load_acq_long(&sc->sp_state);
4180     if (tmp & mask) {
4181         BLOGE(sc, "Filtering completion timed out: "
4182                   "sp_state 0x%lx, mask 0x%lx\n",
4183               tmp, mask);
4184         return (FALSE);
4185     }
4186
4187     return (FALSE);
4188 }
4189
4190 static int
4191 bxe_func_stop(struct bxe_softc *sc)
4192 {
4193     struct ecore_func_state_params func_params = { NULL };
4194     int rc;
4195
4196     /* prepare parameters for function state transitions */
4197     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
4198     func_params.f_obj = &sc->func_obj;
4199     func_params.cmd = ECORE_F_CMD_STOP;
4200
4201     /*
4202      * Try to stop the function the 'good way'. If it fails (in case
4203      * of a parity error during bxe_chip_cleanup()) and we are
4204      * not in a debug mode, perform a state transaction in order to
4205      * enable further HW_RESET transaction.
4206      */
4207     rc = ecore_func_state_change(sc, &func_params);
4208     if (rc) {
4209         BLOGE(sc, "FUNC_STOP ramrod failed. "
4210                   "Running a dry transaction\n");
4211         bxe_set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
4212         return (ecore_func_state_change(sc, &func_params));
4213     }
4214
4215     return (0);
4216 }
4217
4218 static int
4219 bxe_reset_hw(struct bxe_softc *sc,
4220              uint32_t         load_code)
4221 {
4222     struct ecore_func_state_params func_params = { NULL };
4223
4224     /* Prepare parameters for function state transitions */
4225     bxe_set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
4226
4227     func_params.f_obj = &sc->func_obj;
4228     func_params.cmd = ECORE_F_CMD_HW_RESET;
4229
4230     func_params.params.hw_init.load_phase = load_code;
4231
4232     return (ecore_func_state_change(sc, &func_params));
4233 }
4234
4235 static void
4236 bxe_int_disable_sync(struct bxe_softc *sc,
4237                      int              disable_hw)
4238 {
4239     if (disable_hw) {
4240         /* prevent the HW from sending interrupts */
4241         bxe_int_disable(sc);
4242     }
4243
4244     /* XXX need a way to synchronize ALL irqs (intr_mtx?) */
4245     /* make sure all ISRs are done */
4246
4247     /* XXX make sure sp_task is not running */
4248     /* cancel and flush work queues */
4249 }
4250
4251 static void
4252 bxe_chip_cleanup(struct bxe_softc *sc,
4253                  uint32_t         unload_mode,
4254                  uint8_t          keep_link)
4255 {
4256     int port = SC_PORT(sc);
4257     struct ecore_mcast_ramrod_params rparam = { NULL };
4258     uint32_t reset_code;
4259     int i, rc = 0;
4260
4261     bxe_drain_tx_queues(sc);
4262
4263     /* give HW time to discard old tx messages */
4264     DELAY(1000);
4265
4266     /* Clean all ETH MACs */
4267     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_ETH_MAC, FALSE);
4268     if (rc < 0) {
4269         BLOGE(sc, "Failed to delete all ETH MACs (%d)\n", rc);
4270     }
4271
4272     /* Clean up UC list  */
4273     rc = bxe_del_all_macs(sc, &sc->sp_objs[0].mac_obj, ECORE_UC_LIST_MAC, TRUE);
4274     if (rc < 0) {
4275         BLOGE(sc, "Failed to delete UC MACs list (%d)\n", rc);
4276     }
4277
4278     /* Disable LLH */
4279     if (!CHIP_IS_E1(sc)) {
4280         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
4281     }
4282
4283     /* Set "drop all" to stop Rx */
4284
4285     /*
4286      * We need to take the BXE_MCAST_LOCK() here in order to prevent
4287      * a race between the completion code and this code.
4288      */
4289     BXE_MCAST_LOCK(sc);
4290
4291     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
4292         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
4293     } else {
4294         bxe_set_storm_rx_mode(sc);
4295     }
4296
4297     /* Clean up multicast configuration */
4298     rparam.mcast_obj = &sc->mcast_obj;
4299     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4300     if (rc < 0) {
4301         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4302     }
4303
4304     BXE_MCAST_UNLOCK(sc);
4305
4306     // XXX bxe_iov_chip_cleanup(sc);
4307
4308     /*
4309      * Send the UNLOAD_REQUEST to the MCP. This will return if
4310      * this function should perform FUNCTION, PORT, or COMMON HW
4311      * reset.
4312      */
4313     reset_code = bxe_send_unload_req(sc, unload_mode);
4314
4315     /*
4316      * (assumption: No Attention from MCP at this stage)
4317      * PMF probably in the middle of TX disable/enable transaction
4318      */
4319     rc = bxe_func_wait_started(sc);
4320     if (rc) {
4321         BLOGE(sc, "bxe_func_wait_started failed\n");
4322     }
4323
4324     /*
4325      * Close multi and leading connections
4326      * Completions for ramrods are collected in a synchronous way
4327      */
4328     for (i = 0; i < sc->num_queues; i++) {
4329         if (bxe_stop_queue(sc, i)) {
4330             goto unload_error;
4331         }
4332     }
4333
4334     /*
4335      * If SP settings didn't get completed so far - something
4336      * very wrong has happen.
4337      */
4338     if (!bxe_wait_sp_comp(sc, ~0x0UL)) {
4339         BLOGE(sc, "Common slow path ramrods got stuck!\n");
4340     }
4341
4342 unload_error:
4343
4344     rc = bxe_func_stop(sc);
4345     if (rc) {
4346         BLOGE(sc, "Function stop failed!\n");
4347     }
4348
4349     /* disable HW interrupts */
4350     bxe_int_disable_sync(sc, TRUE);
4351
4352     /* detach interrupts */
4353     bxe_interrupt_detach(sc);
4354
4355     /* Reset the chip */
4356     rc = bxe_reset_hw(sc, reset_code);
4357     if (rc) {
4358         BLOGE(sc, "Hardware reset failed\n");
4359     }
4360
4361     /* Report UNLOAD_DONE to MCP */
4362     bxe_send_unload_done(sc, keep_link);
4363 }
4364
4365 static void
4366 bxe_disable_close_the_gate(struct bxe_softc *sc)
4367 {
4368     uint32_t val;
4369     int port = SC_PORT(sc);
4370
4371     BLOGD(sc, DBG_LOAD,
4372           "Disabling 'close the gates'\n");
4373
4374     if (CHIP_IS_E1(sc)) {
4375         uint32_t addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4376                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
4377         val = REG_RD(sc, addr);
4378         val &= ~(0x300);
4379         REG_WR(sc, addr, val);
4380     } else {
4381         val = REG_RD(sc, MISC_REG_AEU_GENERAL_MASK);
4382         val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
4383                  MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
4384         REG_WR(sc, MISC_REG_AEU_GENERAL_MASK, val);
4385     }
4386 }
4387
4388 /*
4389  * Cleans the object that have internal lists without sending
4390  * ramrods. Should be run when interrutps are disabled.
4391  */
4392 static void
4393 bxe_squeeze_objects(struct bxe_softc *sc)
4394 {
4395     unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
4396     struct ecore_mcast_ramrod_params rparam = { NULL };
4397     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
4398     int rc;
4399
4400     /* Cleanup MACs' object first... */
4401
4402     /* Wait for completion of requested */
4403     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
4404     /* Perform a dry cleanup */
4405     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
4406
4407     /* Clean ETH primary MAC */
4408     bxe_set_bit(ECORE_ETH_MAC, &vlan_mac_flags);
4409     rc = mac_obj->delete_all(sc, &sc->sp_objs->mac_obj, &vlan_mac_flags,
4410                              &ramrod_flags);
4411     if (rc != 0) {
4412         BLOGE(sc, "Failed to clean ETH MACs (%d)\n", rc);
4413     }
4414
4415     /* Cleanup UC list */
4416     vlan_mac_flags = 0;
4417     bxe_set_bit(ECORE_UC_LIST_MAC, &vlan_mac_flags);
4418     rc = mac_obj->delete_all(sc, mac_obj, &vlan_mac_flags,
4419                              &ramrod_flags);
4420     if (rc != 0) {
4421         BLOGE(sc, "Failed to clean UC list MACs (%d)\n", rc);
4422     }
4423
4424     /* Now clean mcast object... */
4425
4426     rparam.mcast_obj = &sc->mcast_obj;
4427     bxe_set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
4428
4429     /* Add a DEL command... */
4430     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
4431     if (rc < 0) {
4432         BLOGE(sc, "Failed to send DEL MCAST command (%d)\n", rc);
4433     }
4434
4435     /* now wait until all pending commands are cleared */
4436
4437     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4438     while (rc != 0) {
4439         if (rc < 0) {
4440             BLOGE(sc, "Failed to clean MCAST object (%d)\n", rc);
4441             return;
4442         }
4443
4444         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
4445     }
4446 }
4447
4448 /* stop the controller */
4449 static __noinline int
4450 bxe_nic_unload(struct bxe_softc *sc,
4451                uint32_t         unload_mode,
4452                uint8_t          keep_link)
4453 {
4454     uint8_t global = FALSE;
4455     uint32_t val;
4456
4457     BXE_CORE_LOCK_ASSERT(sc);
4458
4459     BLOGD(sc, DBG_LOAD, "Starting NIC unload...\n");
4460
4461     /* mark driver as unloaded in shmem2 */
4462     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
4463         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
4464         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
4465                   val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
4466     }
4467
4468     if (IS_PF(sc) && sc->recovery_state != BXE_RECOVERY_DONE &&
4469         (sc->state == BXE_STATE_CLOSED || sc->state == BXE_STATE_ERROR)) {
4470         /*
4471          * We can get here if the driver has been unloaded
4472          * during parity error recovery and is either waiting for a
4473          * leader to complete or for other functions to unload and
4474          * then ifconfig down has been issued. In this case we want to
4475          * unload and let other functions to complete a recovery
4476          * process.
4477          */
4478         sc->recovery_state = BXE_RECOVERY_DONE;
4479         sc->is_leader = 0;
4480         bxe_release_leader_lock(sc);
4481         mb();
4482
4483         BLOGD(sc, DBG_LOAD, "Releasing a leadership...\n");
4484         BLOGE(sc, "Can't unload in closed or error state\n");
4485         return (-1);
4486     }
4487
4488     /*
4489      * Nothing to do during unload if previous bxe_nic_load()
4490      * did not completed succesfully - all resourses are released.
4491      */
4492     if ((sc->state == BXE_STATE_CLOSED) ||
4493         (sc->state == BXE_STATE_ERROR)) {
4494         return (0);
4495     }
4496
4497     sc->state = BXE_STATE_CLOSING_WAITING_HALT;
4498     mb();
4499
4500     /* stop tx */
4501     bxe_tx_disable(sc);
4502
4503     sc->rx_mode = BXE_RX_MODE_NONE;
4504     /* XXX set rx mode ??? */
4505
4506     if (IS_PF(sc)) {
4507         /* set ALWAYS_ALIVE bit in shmem */
4508         sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
4509
4510         bxe_drv_pulse(sc);
4511
4512         bxe_stats_handle(sc, STATS_EVENT_STOP);
4513         bxe_save_statistics(sc);
4514     }
4515
4516     /* wait till consumers catch up with producers in all queues */
4517     bxe_drain_tx_queues(sc);
4518
4519     /* if VF indicate to PF this function is going down (PF will delete sp
4520      * elements and clear initializations
4521      */
4522     if (IS_VF(sc)) {
4523         ; /* bxe_vfpf_close_vf(sc); */
4524     } else if (unload_mode != UNLOAD_RECOVERY) {
4525         /* if this is a normal/close unload need to clean up chip */
4526         bxe_chip_cleanup(sc, unload_mode, keep_link);
4527     } else {
4528         /* Send the UNLOAD_REQUEST to the MCP */
4529         bxe_send_unload_req(sc, unload_mode);
4530
4531         /*
4532          * Prevent transactions to host from the functions on the
4533          * engine that doesn't reset global blocks in case of global
4534          * attention once gloabl blocks are reset and gates are opened
4535          * (the engine which leader will perform the recovery
4536          * last).
4537          */
4538         if (!CHIP_IS_E1x(sc)) {
4539             bxe_pf_disable(sc);
4540         }
4541
4542         /* disable HW interrupts */
4543         bxe_int_disable_sync(sc, TRUE);
4544
4545         /* detach interrupts */
4546         bxe_interrupt_detach(sc);
4547
4548         /* Report UNLOAD_DONE to MCP */
4549         bxe_send_unload_done(sc, FALSE);
4550     }
4551
4552     /*
4553      * At this stage no more interrupts will arrive so we may safely clean
4554      * the queue'able objects here in case they failed to get cleaned so far.
4555      */
4556     if (IS_PF(sc)) {
4557         bxe_squeeze_objects(sc);
4558     }
4559
4560     /* There should be no more pending SP commands at this stage */
4561     sc->sp_state = 0;
4562
4563     sc->port.pmf = 0;
4564
4565     bxe_free_fp_buffers(sc);
4566
4567     if (IS_PF(sc)) {
4568         bxe_free_mem(sc);
4569     }
4570
4571     bxe_free_fw_stats_mem(sc);
4572
4573     sc->state = BXE_STATE_CLOSED;
4574
4575     /*
4576      * Check if there are pending parity attentions. If there are - set
4577      * RECOVERY_IN_PROGRESS.
4578      */
4579     if (IS_PF(sc) && bxe_chk_parity_attn(sc, &global, FALSE)) {
4580         bxe_set_reset_in_progress(sc);
4581
4582         /* Set RESET_IS_GLOBAL if needed */
4583         if (global) {
4584             bxe_set_reset_global(sc);
4585         }
4586     }
4587
4588     /*
4589      * The last driver must disable a "close the gate" if there is no
4590      * parity attention or "process kill" pending.
4591      */
4592     if (IS_PF(sc) && !bxe_clear_pf_load(sc) &&
4593         bxe_reset_is_done(sc, SC_PATH(sc))) {
4594         bxe_disable_close_the_gate(sc);
4595     }
4596
4597     BLOGD(sc, DBG_LOAD, "Ended NIC unload\n");
4598
4599     return (0);
4600 }
4601
4602 /*
4603  * Called by the OS to set various media options (i.e. link, speed, etc.) when
4604  * the user runs "ifconfig bxe media ..." or "ifconfig bxe mediaopt ...".
4605  */
4606 static int
4607 bxe_ifmedia_update(struct ifnet *ifp)
4608 {
4609     struct bxe_softc *sc = (struct bxe_softc *)ifp->if_softc;
4610     struct ifmedia *ifm;
4611
4612     ifm = &sc->ifmedia;
4613
4614     /* We only support Ethernet media type. */
4615     if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
4616         return (EINVAL);
4617     }
4618
4619     switch (IFM_SUBTYPE(ifm->ifm_media)) {
4620     case IFM_AUTO:
4621          break;
4622     case IFM_10G_CX4:
4623     case IFM_10G_SR:
4624     case IFM_10G_T:
4625     case IFM_10G_TWINAX:
4626     default:
4627         /* We don't support changing the media type. */
4628         BLOGD(sc, DBG_LOAD, "Invalid media type (%d)\n",
4629               IFM_SUBTYPE(ifm->ifm_media));
4630         return (EINVAL);
4631     }
4632
4633     return (0);
4634 }
4635
4636 /*
4637  * Called by the OS to get the current media status (i.e. link, speed, etc.).
4638  */
4639 static void
4640 bxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4641 {
4642     struct bxe_softc *sc = ifp->if_softc;
4643
4644     /* Report link down if the driver isn't running. */
4645     if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
4646         ifmr->ifm_active |= IFM_NONE;
4647         return;
4648     }
4649
4650     /* Setup the default interface info. */
4651     ifmr->ifm_status = IFM_AVALID;
4652     ifmr->ifm_active = IFM_ETHER;
4653
4654     if (sc->link_vars.link_up) {
4655         ifmr->ifm_status |= IFM_ACTIVE;
4656     } else {
4657         ifmr->ifm_active |= IFM_NONE;
4658         return;
4659     }
4660
4661     ifmr->ifm_active |= sc->media;
4662
4663     if (sc->link_vars.duplex == DUPLEX_FULL) {
4664         ifmr->ifm_active |= IFM_FDX;
4665     } else {
4666         ifmr->ifm_active |= IFM_HDX;
4667     }
4668 }
4669
4670 static int
4671 bxe_ioctl_nvram(struct bxe_softc *sc,
4672                 uint32_t         priv_op,
4673                 struct ifreq     *ifr)
4674 {
4675     struct bxe_nvram_data nvdata_base;
4676     struct bxe_nvram_data *nvdata;
4677     int len;
4678     int error = 0;
4679
4680     copyin(ifr->ifr_data, &nvdata_base, sizeof(nvdata_base));
4681
4682     len = (sizeof(struct bxe_nvram_data) +
4683            nvdata_base.len -
4684            sizeof(uint32_t));
4685
4686     if (len > sizeof(struct bxe_nvram_data)) {
4687         if ((nvdata = (struct bxe_nvram_data *)
4688                  malloc(len, M_DEVBUF,
4689                         (M_NOWAIT | M_ZERO))) == NULL) {
4690             BLOGE(sc, "BXE_IOC_RD_NVRAM malloc failed\n");
4691             return (1);
4692         }
4693         memcpy(nvdata, &nvdata_base, sizeof(struct bxe_nvram_data));
4694     } else {
4695         nvdata = &nvdata_base;
4696     }
4697
4698     if (priv_op == BXE_IOC_RD_NVRAM) {
4699         BLOGD(sc, DBG_IOCTL, "IOC_RD_NVRAM 0x%x %d\n",
4700               nvdata->offset, nvdata->len);
4701         error = bxe_nvram_read(sc,
4702                                nvdata->offset,
4703                                (uint8_t *)nvdata->value,
4704                                nvdata->len);
4705         copyout(nvdata, ifr->ifr_data, len);
4706     } else { /* BXE_IOC_WR_NVRAM */
4707         BLOGD(sc, DBG_IOCTL, "IOC_WR_NVRAM 0x%x %d\n",
4708               nvdata->offset, nvdata->len);
4709         copyin(ifr->ifr_data, nvdata, len);
4710         error = bxe_nvram_write(sc,
4711                                 nvdata->offset,
4712                                 (uint8_t *)nvdata->value,
4713                                 nvdata->len);
4714     }
4715
4716     if (len > sizeof(struct bxe_nvram_data)) {
4717         free(nvdata, M_DEVBUF);
4718     }
4719
4720     return (error);
4721 }
4722
4723 static int
4724 bxe_ioctl_stats_show(struct bxe_softc *sc,
4725                      uint32_t         priv_op,
4726                      struct ifreq     *ifr)
4727 {
4728     const size_t str_size   = (BXE_NUM_ETH_STATS * STAT_NAME_LEN);
4729     const size_t stats_size = (BXE_NUM_ETH_STATS * sizeof(uint64_t));
4730     caddr_t p_tmp;
4731     uint32_t *offset;
4732     int i;
4733
4734     switch (priv_op)
4735     {
4736     case BXE_IOC_STATS_SHOW_NUM:
4737         memset(ifr->ifr_data, 0, sizeof(union bxe_stats_show_data));
4738         ((union bxe_stats_show_data *)ifr->ifr_data)->desc.num =
4739             BXE_NUM_ETH_STATS;
4740         ((union bxe_stats_show_data *)ifr->ifr_data)->desc.len =
4741             STAT_NAME_LEN;
4742         return (0);
4743
4744     case BXE_IOC_STATS_SHOW_STR:
4745         memset(ifr->ifr_data, 0, str_size);
4746         p_tmp = ifr->ifr_data;
4747         for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4748             strcpy(p_tmp, bxe_eth_stats_arr[i].string);
4749             p_tmp += STAT_NAME_LEN;
4750         }
4751         return (0);
4752
4753     case BXE_IOC_STATS_SHOW_CNT:
4754         memset(ifr->ifr_data, 0, stats_size);
4755         p_tmp = ifr->ifr_data;
4756         for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
4757             offset = ((uint32_t *)&sc->eth_stats +
4758                       bxe_eth_stats_arr[i].offset);
4759             switch (bxe_eth_stats_arr[i].size) {
4760             case 4:
4761                 *((uint64_t *)p_tmp) = (uint64_t)*offset;
4762                 break;
4763             case 8:
4764                 *((uint64_t *)p_tmp) = HILO_U64(*offset, *(offset + 1));
4765                 break;
4766             default:
4767                 *((uint64_t *)p_tmp) = 0;
4768             }
4769             p_tmp += sizeof(uint64_t);
4770         }
4771         return (0);
4772
4773     default:
4774         return (-1);
4775     }
4776 }
4777
4778 static void
4779 bxe_handle_chip_tq(void *context,
4780                    int  pending)
4781 {
4782     struct bxe_softc *sc = (struct bxe_softc *)context;
4783     long work = atomic_load_acq_long(&sc->chip_tq_flags);
4784
4785     switch (work)
4786     {
4787     case CHIP_TQ_REINIT:
4788         if (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) {
4789             /* restart the interface */
4790             BLOGD(sc, DBG_LOAD, "Restarting the interface...\n");
4791             bxe_periodic_stop(sc);
4792             BXE_CORE_LOCK(sc);
4793             bxe_stop_locked(sc);
4794             bxe_init_locked(sc);
4795             BXE_CORE_UNLOCK(sc);
4796         }
4797         break;
4798
4799     default:
4800         break;
4801     }
4802 }
4803
4804 /*
4805  * Handles any IOCTL calls from the operating system.
4806  *
4807  * Returns:
4808  *   0 = Success, >0 Failure
4809  */
4810 static int
4811 bxe_ioctl(struct ifnet *ifp,
4812           u_long       command,
4813           caddr_t      data)
4814 {
4815     struct bxe_softc *sc = ifp->if_softc;
4816     struct ifreq *ifr = (struct ifreq *)data;
4817     struct bxe_nvram_data *nvdata;
4818     uint32_t priv_op;
4819     int mask = 0;
4820     int reinit = 0;
4821     int error = 0;
4822
4823     int mtu_min = (ETH_MIN_PACKET_SIZE - ETH_HLEN);
4824     int mtu_max = (MJUM9BYTES - ETH_OVERHEAD - IP_HEADER_ALIGNMENT_PADDING);
4825
4826     switch (command)
4827     {
4828     case SIOCSIFMTU:
4829         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFMTU ioctl (mtu=%d)\n",
4830               ifr->ifr_mtu);
4831
4832         if (sc->mtu == ifr->ifr_mtu) {
4833             /* nothing to change */
4834             break;
4835         }
4836
4837         if ((ifr->ifr_mtu < mtu_min) || (ifr->ifr_mtu > mtu_max)) {
4838             BLOGE(sc, "Unsupported MTU size %d (range is %d-%d)\n",
4839                   ifr->ifr_mtu, mtu_min, mtu_max);
4840             error = EINVAL;
4841             break;
4842         }
4843
4844         atomic_store_rel_int((volatile unsigned int *)&sc->mtu,
4845                              (unsigned long)ifr->ifr_mtu);
4846         atomic_store_rel_long((volatile unsigned long *)&ifp->if_mtu,
4847                               (unsigned long)ifr->ifr_mtu);
4848
4849         reinit = 1;
4850         break;
4851
4852     case SIOCSIFFLAGS:
4853         /* toggle the interface state up or down */
4854         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFFLAGS ioctl\n");
4855
4856         BXE_CORE_LOCK(sc);
4857         /* check if the interface is up */
4858         if (ifp->if_flags & IFF_UP) {
4859             if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4860                 /* set the receive mode flags */
4861                 bxe_set_rx_mode(sc);
4862             } else {
4863                 bxe_init_locked(sc);
4864             }
4865         } else {
4866             if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4867                 bxe_periodic_stop(sc);
4868                 bxe_stop_locked(sc);
4869             }
4870         }
4871         BXE_CORE_UNLOCK(sc);
4872
4873         break;
4874
4875     case SIOCADDMULTI:
4876     case SIOCDELMULTI:
4877         /* add/delete multicast addresses */
4878         BLOGD(sc, DBG_IOCTL, "Received SIOCADDMULTI/SIOCDELMULTI ioctl\n");
4879
4880         /* check if the interface is up */
4881         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4882             /* set the receive mode flags */
4883             BXE_CORE_LOCK(sc);
4884             bxe_set_rx_mode(sc);
4885             BXE_CORE_UNLOCK(sc); 
4886         }
4887
4888         break;
4889
4890     case SIOCSIFCAP:
4891         /* find out which capabilities have changed */
4892         mask = (ifr->ifr_reqcap ^ ifp->if_capenable);
4893
4894         BLOGD(sc, DBG_IOCTL, "Received SIOCSIFCAP ioctl (mask=0x%08x)\n",
4895               mask);
4896
4897         /* toggle the LRO capabilites enable flag */
4898         if (mask & IFCAP_LRO) {
4899             ifp->if_capenable ^= IFCAP_LRO;
4900             BLOGD(sc, DBG_IOCTL, "Turning LRO %s\n",
4901                   (ifp->if_capenable & IFCAP_LRO) ? "ON" : "OFF");
4902             reinit = 1;
4903         }
4904
4905         /* toggle the TXCSUM checksum capabilites enable flag */
4906         if (mask & IFCAP_TXCSUM) {
4907             ifp->if_capenable ^= IFCAP_TXCSUM;
4908             BLOGD(sc, DBG_IOCTL, "Turning TXCSUM %s\n",
4909                   (ifp->if_capenable & IFCAP_TXCSUM) ? "ON" : "OFF");
4910             if (ifp->if_capenable & IFCAP_TXCSUM) {
4911                 ifp->if_hwassist = (CSUM_IP       |
4912                                     CSUM_TCP      |
4913                                     CSUM_UDP      |
4914                                     CSUM_TSO      |
4915                                     CSUM_TCP_IPV6 |
4916                                     CSUM_UDP_IPV6);
4917             } else {
4918                 ifp->if_hwassist = 0;
4919             }
4920         }
4921
4922         /* toggle the RXCSUM checksum capabilities enable flag */
4923         if (mask & IFCAP_RXCSUM) {
4924             ifp->if_capenable ^= IFCAP_RXCSUM;
4925             BLOGD(sc, DBG_IOCTL, "Turning RXCSUM %s\n",
4926                   (ifp->if_capenable & IFCAP_RXCSUM) ? "ON" : "OFF");
4927             if (ifp->if_capenable & IFCAP_RXCSUM) {
4928                 ifp->if_hwassist = (CSUM_IP       |
4929                                     CSUM_TCP      |
4930                                     CSUM_UDP      |
4931                                     CSUM_TSO      |
4932                                     CSUM_TCP_IPV6 |
4933                                     CSUM_UDP_IPV6);
4934             } else {
4935                 ifp->if_hwassist = 0;
4936             }
4937         }
4938
4939         /* toggle TSO4 capabilities enabled flag */
4940         if (mask & IFCAP_TSO4) {
4941             ifp->if_capenable ^= IFCAP_TSO4;
4942             BLOGD(sc, DBG_IOCTL, "Turning TSO4 %s\n",
4943                   (ifp->if_capenable & IFCAP_TSO4) ? "ON" : "OFF");
4944         }
4945
4946         /* toggle TSO6 capabilities enabled flag */
4947         if (mask & IFCAP_TSO6) {
4948             ifp->if_capenable ^= IFCAP_TSO6;
4949             BLOGD(sc, DBG_IOCTL, "Turning TSO6 %s\n",
4950                   (ifp->if_capenable & IFCAP_TSO6) ? "ON" : "OFF");
4951         }
4952
4953         /* toggle VLAN_HWTSO capabilities enabled flag */
4954         if (mask & IFCAP_VLAN_HWTSO) {
4955             ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
4956             BLOGD(sc, DBG_IOCTL, "Turning VLAN_HWTSO %s\n",
4957                   (ifp->if_capenable & IFCAP_VLAN_HWTSO) ? "ON" : "OFF");
4958         }
4959
4960         /* toggle VLAN_HWCSUM capabilities enabled flag */
4961         if (mask & IFCAP_VLAN_HWCSUM) {
4962             /* XXX investigate this... */
4963             BLOGE(sc, "Changing VLAN_HWCSUM is not supported!\n");
4964             error = EINVAL;
4965         }
4966
4967         /* toggle VLAN_MTU capabilities enable flag */
4968         if (mask & IFCAP_VLAN_MTU) {
4969             /* XXX investigate this... */
4970             BLOGE(sc, "Changing VLAN_MTU is not supported!\n");
4971             error = EINVAL;
4972         }
4973
4974         /* toggle VLAN_HWTAGGING capabilities enabled flag */
4975         if (mask & IFCAP_VLAN_HWTAGGING) {
4976             /* XXX investigate this... */
4977             BLOGE(sc, "Changing VLAN_HWTAGGING is not supported!\n");
4978             error = EINVAL;
4979         }
4980
4981         /* toggle VLAN_HWFILTER capabilities enabled flag */
4982         if (mask & IFCAP_VLAN_HWFILTER) {
4983             /* XXX investigate this... */
4984             BLOGE(sc, "Changing VLAN_HWFILTER is not supported!\n");
4985             error = EINVAL;
4986         }
4987
4988         /* XXX not yet...
4989          * IFCAP_WOL_MAGIC
4990          */
4991
4992         break;
4993
4994     case SIOCSIFMEDIA:
4995     case SIOCGIFMEDIA:
4996         /* set/get interface media */
4997         BLOGD(sc, DBG_IOCTL,
4998               "Received SIOCSIFMEDIA/SIOCGIFMEDIA ioctl (cmd=%lu)\n",
4999               (command & 0xff));
5000         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
5001         break;
5002
5003     case SIOCGPRIVATE_0:
5004         copyin(ifr->ifr_data, &priv_op, sizeof(priv_op));
5005
5006         switch (priv_op)
5007         {
5008         case BXE_IOC_RD_NVRAM:
5009         case BXE_IOC_WR_NVRAM:
5010             nvdata = (struct bxe_nvram_data *)ifr->ifr_data;
5011             BLOGD(sc, DBG_IOCTL,
5012                   "Received Private NVRAM ioctl addr=0x%x size=%u\n",
5013                   nvdata->offset, nvdata->len);
5014             error = bxe_ioctl_nvram(sc, priv_op, ifr);
5015             break;
5016
5017         case BXE_IOC_STATS_SHOW_NUM:
5018         case BXE_IOC_STATS_SHOW_STR:
5019         case BXE_IOC_STATS_SHOW_CNT:
5020             BLOGD(sc, DBG_IOCTL, "Received Private Stats ioctl (%d)\n",
5021                   priv_op);
5022             error = bxe_ioctl_stats_show(sc, priv_op, ifr);
5023             break;
5024
5025         default:
5026             BLOGW(sc, "Received Private Unknown ioctl (%d)\n", priv_op);
5027             error = EINVAL;
5028             break;
5029         }
5030
5031         break;
5032
5033     default:
5034         BLOGD(sc, DBG_IOCTL, "Received Unknown Ioctl (cmd=%lu)\n",
5035               (command & 0xff));
5036         error = ether_ioctl(ifp, command, data);
5037         break;
5038     }
5039
5040     if (reinit && (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING)) {
5041         BLOGD(sc, DBG_LOAD | DBG_IOCTL,
5042               "Re-initializing hardware from IOCTL change\n");
5043         bxe_periodic_stop(sc);
5044         BXE_CORE_LOCK(sc);
5045         bxe_stop_locked(sc);
5046         bxe_init_locked(sc);
5047         BXE_CORE_UNLOCK(sc);
5048     }
5049
5050     return (error);
5051 }
5052
5053 static __noinline void
5054 bxe_dump_mbuf(struct bxe_softc *sc,
5055               struct mbuf      *m,
5056               uint8_t          contents)
5057 {
5058     char * type;
5059     int i = 0;
5060
5061     if (!(sc->debug & DBG_MBUF)) {
5062         return;
5063     }
5064
5065     if (m == NULL) {
5066         BLOGD(sc, DBG_MBUF, "mbuf: null pointer\n");
5067         return;
5068     }
5069
5070     while (m) {
5071         BLOGD(sc, DBG_MBUF,
5072               "%02d: mbuf=%p m_len=%d m_flags=0x%b m_data=%p\n",
5073               i, m, m->m_len, m->m_flags,
5074               "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
5075
5076         if (m->m_flags & M_PKTHDR) {
5077              BLOGD(sc, DBG_MBUF,
5078                    "%02d: - m_pkthdr: tot_len=%d flags=0x%b csum_flags=%b\n",
5079                    i, m->m_pkthdr.len, m->m_flags,
5080                    "\20\12M_BCAST\13M_MCAST\14M_FRAG"
5081                    "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
5082                    "\22M_PROMISC\23M_NOFREE",
5083                    (int)m->m_pkthdr.csum_flags,
5084                    "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
5085                    "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
5086                    "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
5087                    "\14CSUM_PSEUDO_HDR");
5088         }
5089
5090         if (m->m_flags & M_EXT) {
5091             switch (m->m_ext.ext_type) {
5092             case EXT_CLUSTER:    type = "EXT_CLUSTER";    break;
5093             case EXT_SFBUF:      type = "EXT_SFBUF";      break;
5094             case EXT_JUMBOP:     type = "EXT_JUMBOP";     break;
5095             case EXT_JUMBO9:     type = "EXT_JUMBO9";     break;
5096             case EXT_JUMBO16:    type = "EXT_JUMBO16";    break;
5097             case EXT_PACKET:     type = "EXT_PACKET";     break;
5098             case EXT_MBUF:       type = "EXT_MBUF";       break;
5099             case EXT_NET_DRV:    type = "EXT_NET_DRV";    break;
5100             case EXT_MOD_TYPE:   type = "EXT_MOD_TYPE";   break;
5101             case EXT_DISPOSABLE: type = "EXT_DISPOSABLE"; break;
5102             case EXT_EXTREF:     type = "EXT_EXTREF";     break;
5103             default:             type = "UNKNOWN";        break;
5104             }
5105
5106             BLOGD(sc, DBG_MBUF,
5107                   "%02d: - m_ext: %p ext_size=%d type=%s\n",
5108                   i, m->m_ext.ext_buf, m->m_ext.ext_size, type);
5109         }
5110
5111         if (contents) {
5112             bxe_dump_mbuf_data(sc, "mbuf data", m, TRUE);
5113         }
5114
5115         m = m->m_next;
5116         i++;
5117     }
5118 }
5119
5120 /*
5121  * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
5122  * Check that (13 total bds - 3 bds) = 10 bd window >= MSS.
5123  * The window: 3 bds are = 1 for headers BD + 2 for parse BD and last BD
5124  * The headers comes in a seperate bd in FreeBSD so 13-3=10.
5125  * Returns: 0 if OK to send, 1 if packet needs further defragmentation
5126  */
5127 static int
5128 bxe_chktso_window(struct bxe_softc  *sc,
5129                   int               nsegs,
5130                   bus_dma_segment_t *segs,
5131                   struct mbuf       *m)
5132 {
5133     uint32_t num_wnds, wnd_size, wnd_sum;
5134     int32_t frag_idx, wnd_idx;
5135     unsigned short lso_mss;
5136     int defrag;
5137
5138     defrag = 0;
5139     wnd_sum = 0;
5140     wnd_size = 10;
5141     num_wnds = nsegs - wnd_size;
5142     lso_mss = htole16(m->m_pkthdr.tso_segsz);
5143
5144     /*
5145      * Total header lengths Eth+IP+TCP in first FreeBSD mbuf so calculate the
5146      * first window sum of data while skipping the first assuming it is the
5147      * header in FreeBSD.
5148      */
5149     for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++) {
5150         wnd_sum += htole16(segs[frag_idx].ds_len);
5151     }
5152
5153     /* check the first 10 bd window size */
5154     if (wnd_sum < lso_mss) {
5155         return (1);
5156     }
5157
5158     /* run through the windows */
5159     for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
5160         /* subtract the first mbuf->m_len of the last wndw(-header) */
5161         wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
5162         /* add the next mbuf len to the len of our new window */
5163         wnd_sum += htole16(segs[frag_idx].ds_len);
5164         if (wnd_sum < lso_mss) {
5165             return (1);
5166         }
5167     }
5168
5169     return (0);
5170 }
5171
5172 static uint8_t
5173 bxe_set_pbd_csum_e2(struct bxe_fastpath *fp,
5174                     struct mbuf         *m,
5175                     uint32_t            *parsing_data)
5176 {
5177     struct ether_vlan_header *eh = NULL;
5178     struct ip *ip4 = NULL;
5179     struct ip6_hdr *ip6 = NULL;
5180     caddr_t ip = NULL;
5181     struct tcphdr *th = NULL;
5182     int e_hlen, ip_hlen, l4_off;
5183     uint16_t proto;
5184
5185     if (m->m_pkthdr.csum_flags == CSUM_IP) {
5186         /* no L4 checksum offload needed */
5187         return (0);
5188     }
5189
5190     /* get the Ethernet header */
5191     eh = mtod(m, struct ether_vlan_header *);
5192
5193     /* handle VLAN encapsulation if present */
5194     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
5195         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5196         proto  = ntohs(eh->evl_proto);
5197     } else {
5198         e_hlen = ETHER_HDR_LEN;
5199         proto  = ntohs(eh->evl_encap_proto);
5200     }
5201
5202     switch (proto) {
5203     case ETHERTYPE_IP:
5204         /* get the IP header, if mbuf len < 20 then header in next mbuf */
5205         ip4 = (m->m_len < sizeof(struct ip)) ?
5206                   (struct ip *)m->m_next->m_data :
5207                   (struct ip *)(m->m_data + e_hlen);
5208         /* ip_hl is number of 32-bit words */
5209         ip_hlen = (ip4->ip_hl << 2);
5210         ip = (caddr_t)ip4;
5211         break;
5212     case ETHERTYPE_IPV6:
5213         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
5214         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
5215                   (struct ip6_hdr *)m->m_next->m_data :
5216                   (struct ip6_hdr *)(m->m_data + e_hlen);
5217         /* XXX cannot support offload with IPv6 extensions */
5218         ip_hlen = sizeof(struct ip6_hdr);
5219         ip = (caddr_t)ip6;
5220         break;
5221     default:
5222         /* We can't offload in this case... */
5223         /* XXX error stat ??? */
5224         return (0);
5225     }
5226
5227     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5228     l4_off = (e_hlen + ip_hlen);
5229
5230     *parsing_data |=
5231         (((l4_off >> 1) << ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
5232          ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W);
5233
5234     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5235                                   CSUM_TSO |
5236                                   CSUM_TCP_IPV6)) {
5237         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5238         th = (struct tcphdr *)(ip + ip_hlen);
5239         /* th_off is number of 32-bit words */
5240         *parsing_data |= ((th->th_off <<
5241                            ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
5242                           ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW);
5243         return (l4_off + (th->th_off << 2)); /* entire header length */
5244     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5245                                          CSUM_UDP_IPV6)) {
5246         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5247         return (l4_off + sizeof(struct udphdr)); /* entire header length */
5248     } else {
5249         /* XXX error stat ??? */
5250         return (0);
5251     }
5252 }
5253
5254 static uint8_t
5255 bxe_set_pbd_csum(struct bxe_fastpath        *fp,
5256                  struct mbuf                *m,
5257                  struct eth_tx_parse_bd_e1x *pbd)
5258 {
5259     struct ether_vlan_header *eh = NULL;
5260     struct ip *ip4 = NULL;
5261     struct ip6_hdr *ip6 = NULL;
5262     caddr_t ip = NULL;
5263     struct tcphdr *th = NULL;
5264     struct udphdr *uh = NULL;
5265     int e_hlen, ip_hlen;
5266     uint16_t proto;
5267     uint8_t hlen;
5268     uint16_t tmp_csum;
5269     uint32_t *tmp_uh;
5270
5271     /* get the Ethernet header */
5272     eh = mtod(m, struct ether_vlan_header *);
5273
5274     /* handle VLAN encapsulation if present */
5275     if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
5276         e_hlen = (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5277         proto  = ntohs(eh->evl_proto);
5278     } else {
5279         e_hlen = ETHER_HDR_LEN;
5280         proto  = ntohs(eh->evl_encap_proto);
5281     }
5282
5283     switch (proto) {
5284     case ETHERTYPE_IP:
5285         /* get the IP header, if mbuf len < 20 then header in next mbuf */
5286         ip4 = (m->m_len < sizeof(struct ip)) ?
5287                   (struct ip *)m->m_next->m_data :
5288                   (struct ip *)(m->m_data + e_hlen);
5289         /* ip_hl is number of 32-bit words */
5290         ip_hlen = (ip4->ip_hl << 1);
5291         ip = (caddr_t)ip4;
5292         break;
5293     case ETHERTYPE_IPV6:
5294         /* get the IPv6 header, if mbuf len < 40 then header in next mbuf */
5295         ip6 = (m->m_len < sizeof(struct ip6_hdr)) ?
5296                   (struct ip6_hdr *)m->m_next->m_data :
5297                   (struct ip6_hdr *)(m->m_data + e_hlen);
5298         /* XXX cannot support offload with IPv6 extensions */
5299         ip_hlen = (sizeof(struct ip6_hdr) >> 1);
5300         ip = (caddr_t)ip6;
5301         break;
5302     default:
5303         /* We can't offload in this case... */
5304         /* XXX error stat ??? */
5305         return (0);
5306     }
5307
5308     hlen = (e_hlen >> 1);
5309
5310     /* note that rest of global_data is indirectly zeroed here */
5311     if (m->m_flags & M_VLANTAG) {
5312         pbd->global_data =
5313             htole16(hlen | (1 << ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
5314     } else {
5315         pbd->global_data = htole16(hlen);
5316     }
5317
5318     pbd->ip_hlen_w = ip_hlen;
5319
5320     hlen += pbd->ip_hlen_w;
5321
5322     /* XXX assuming L4 header is contiguous to IPv4/IPv6 in the same mbuf */
5323
5324     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5325                                   CSUM_TSO |
5326                                   CSUM_TCP_IPV6)) {
5327         th = (struct tcphdr *)(ip + (ip_hlen << 1));
5328         /* th_off is number of 32-bit words */
5329         hlen += (uint16_t)(th->th_off << 1);
5330     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5331                                          CSUM_UDP_IPV6)) {
5332         uh = (struct udphdr *)(ip + (ip_hlen << 1));
5333         hlen += (sizeof(struct udphdr) / 2);
5334     } else {
5335         /* valid case as only CSUM_IP was set */
5336         return (0);
5337     }
5338
5339     pbd->total_hlen_w = htole16(hlen);
5340
5341     if (m->m_pkthdr.csum_flags & (CSUM_TCP |
5342                                   CSUM_TSO |
5343                                   CSUM_TCP_IPV6)) {
5344         fp->eth_q_stats.tx_ofld_frames_csum_tcp++;
5345         pbd->tcp_pseudo_csum = ntohs(th->th_sum);
5346     } else if (m->m_pkthdr.csum_flags & (CSUM_UDP |
5347                                          CSUM_UDP_IPV6)) {
5348         fp->eth_q_stats.tx_ofld_frames_csum_udp++;
5349
5350         /*
5351          * Everest1 (i.e. 57710, 57711, 57711E) does not natively support UDP
5352          * checksums and does not know anything about the UDP header and where
5353          * the checksum field is located. It only knows about TCP. Therefore
5354          * we "lie" to the hardware for outgoing UDP packets w/ checksum
5355          * offload. Since the checksum field offset for TCP is 16 bytes and
5356          * for UDP it is 6 bytes we pass a pointer to the hardware that is 10
5357          * bytes less than the start of the UDP header. This allows the
5358          * hardware to write the checksum in the correct spot. But the
5359          * hardware will compute a checksum which includes the last 10 bytes
5360          * of the IP header. To correct this we tweak the stack computed
5361          * pseudo checksum by folding in the calculation of the inverse
5362          * checksum for those final 10 bytes of the IP header. This allows
5363          * the correct checksum to be computed by the hardware.
5364          */
5365
5366         /* set pointer 10 bytes before UDP header */
5367         tmp_uh = (uint32_t *)((uint8_t *)uh - 10);
5368
5369         /* calculate a pseudo header checksum over the first 10 bytes */
5370         tmp_csum = in_pseudo(*tmp_uh,
5371                              *(tmp_uh + 1),
5372                              *(uint16_t *)(tmp_uh + 2));
5373
5374         pbd->tcp_pseudo_csum = ntohs(in_addword(uh->uh_sum, ~tmp_csum));
5375     }
5376
5377     return (hlen * 2); /* entire header length, number of bytes */
5378 }
5379
5380 static void
5381 bxe_set_pbd_lso_e2(struct mbuf *m,
5382                    uint32_t    *parsing_data)
5383 {
5384     *parsing_data |= ((m->m_pkthdr.tso_segsz <<
5385                        ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
5386                       ETH_TX_PARSE_BD_E2_LSO_MSS);
5387
5388     /* XXX test for IPv6 with extension header... */
5389 #if 0
5390     struct ip6_hdr *ip6;
5391     if (ip6 && ip6->ip6_nxt == 'some ipv6 extension header')
5392         *parsing_data |= ETH_TX_PARSE_BD_E2_IPV6_WITH_EXT_HDR;
5393 #endif
5394 }
5395
5396 static void
5397 bxe_set_pbd_lso(struct mbuf                *m,
5398                 struct eth_tx_parse_bd_e1x *pbd)
5399 {
5400     struct ether_vlan_header *eh = NULL;
5401     struct ip *ip = NULL;
5402     struct tcphdr *th = NULL;
5403     int e_hlen;
5404
5405     /* get the Ethernet header */
5406     eh = mtod(m, struct ether_vlan_header *);
5407
5408     /* handle VLAN encapsulation if present */
5409     e_hlen = (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) ?
5410                  (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) : ETHER_HDR_LEN;
5411
5412     /* get the IP and TCP header, with LSO entire header in first mbuf */
5413     /* XXX assuming IPv4 */
5414     ip = (struct ip *)(m->m_data + e_hlen);
5415     th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
5416
5417     pbd->lso_mss = htole16(m->m_pkthdr.tso_segsz);
5418     pbd->tcp_send_seq = ntohl(th->th_seq);
5419     pbd->tcp_flags = ((ntohl(((uint32_t *)th)[3]) >> 16) & 0xff);
5420
5421 #if 1
5422         /* XXX IPv4 */
5423         pbd->ip_id = ntohs(ip->ip_id);
5424         pbd->tcp_pseudo_csum =
5425             ntohs(in_pseudo(ip->ip_src.s_addr,
5426                             ip->ip_dst.s_addr,
5427                             htons(IPPROTO_TCP)));
5428 #else
5429         /* XXX IPv6 */
5430         pbd->tcp_pseudo_csum =
5431             ntohs(in_pseudo(&ip6->ip6_src,
5432                             &ip6->ip6_dst,
5433                             htons(IPPROTO_TCP)));
5434 #endif
5435
5436     pbd->global_data |=
5437         htole16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
5438 }
5439
5440 /*
5441  * Encapsulte an mbuf cluster into the tx bd chain and makes the memory
5442  * visible to the controller.
5443  *
5444  * If an mbuf is submitted to this routine and cannot be given to the
5445  * controller (e.g. it has too many fragments) then the function may free
5446  * the mbuf and return to the caller.
5447  *
5448  * Returns:
5449  *   0 = Success, !0 = Failure
5450  *   Note the side effect that an mbuf may be freed if it causes a problem.
5451  */
5452 static int
5453 bxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
5454 {
5455     bus_dma_segment_t segs[32];
5456     struct mbuf *m0;
5457     struct bxe_sw_tx_bd *tx_buf;
5458     struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
5459     struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
5460     /* struct eth_tx_parse_2nd_bd *pbd2 = NULL; */
5461     struct eth_tx_bd *tx_data_bd;
5462     struct eth_tx_bd *tx_total_pkt_size_bd;
5463     struct eth_tx_start_bd *tx_start_bd;
5464     uint16_t bd_prod, pkt_prod, total_pkt_size;
5465     uint8_t mac_type;
5466     int defragged, error, nsegs, rc, nbds, vlan_off, ovlan;
5467     struct bxe_softc *sc;
5468     uint16_t tx_bd_avail;
5469     struct ether_vlan_header *eh;
5470     uint32_t pbd_e2_parsing_data = 0;
5471     uint8_t hlen = 0;
5472     int tmp_bd;
5473     int i;
5474
5475     sc = fp->sc;
5476
5477     M_ASSERTPKTHDR(*m_head);
5478
5479     m0 = *m_head;
5480     rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
5481     tx_start_bd = NULL;
5482     tx_data_bd = NULL;
5483     tx_total_pkt_size_bd = NULL;
5484
5485     /* get the H/W pointer for packets and BDs */
5486     pkt_prod = fp->tx_pkt_prod;
5487     bd_prod = fp->tx_bd_prod;
5488
5489     mac_type = UNICAST_ADDRESS;
5490
5491     /* map the mbuf into the next open DMAable memory */
5492     tx_buf = &fp->tx_mbuf_chain[TX_BD(pkt_prod)];
5493     error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5494                                     tx_buf->m_map, m0,
5495                                     segs, &nsegs, BUS_DMA_NOWAIT);
5496
5497     /* mapping errors */
5498     if(__predict_false(error != 0)) {
5499         fp->eth_q_stats.tx_dma_mapping_failure++;
5500         if (error == ENOMEM) {
5501             /* resource issue, try again later */
5502             rc = ENOMEM;
5503         } else if (error == EFBIG) {
5504             /* possibly recoverable with defragmentation */
5505             fp->eth_q_stats.mbuf_defrag_attempts++;
5506             m0 = m_defrag(*m_head, M_DONTWAIT);
5507             if (m0 == NULL) {
5508                 fp->eth_q_stats.mbuf_defrag_failures++;
5509                 rc = ENOBUFS;
5510             } else {
5511                 /* defrag successful, try mapping again */
5512                 *m_head = m0;
5513                 error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5514                                                 tx_buf->m_map, m0,
5515                                                 segs, &nsegs, BUS_DMA_NOWAIT);
5516                 if (error) {
5517                     fp->eth_q_stats.tx_dma_mapping_failure++;
5518                     rc = error;
5519                 }
5520             }
5521         } else {
5522             /* unknown, unrecoverable mapping error */
5523             BLOGE(sc, "Unknown TX mapping error rc=%d\n", error);
5524             bxe_dump_mbuf(sc, m0, FALSE);
5525             rc = error;
5526         }
5527
5528         goto bxe_tx_encap_continue;
5529     }
5530
5531     tx_bd_avail = bxe_tx_avail(sc, fp);
5532
5533     /* make sure there is enough room in the send queue */
5534     if (__predict_false(tx_bd_avail < (nsegs + 2))) {
5535         /* Recoverable, try again later. */
5536         fp->eth_q_stats.tx_hw_queue_full++;
5537         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5538         rc = ENOMEM;
5539         goto bxe_tx_encap_continue;
5540     }
5541
5542     /* capture the current H/W TX chain high watermark */
5543     if (__predict_false(fp->eth_q_stats.tx_hw_max_queue_depth <
5544                         (TX_BD_USABLE - tx_bd_avail))) {
5545         fp->eth_q_stats.tx_hw_max_queue_depth = (TX_BD_USABLE - tx_bd_avail);
5546     }
5547
5548     /* make sure it fits in the packet window */
5549     if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5550         /*
5551          * The mbuf may be to big for the controller to handle. If the frame
5552          * is a TSO frame we'll need to do an additional check.
5553          */
5554         if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5555             if (bxe_chktso_window(sc, nsegs, segs, m0) == 0) {
5556                 goto bxe_tx_encap_continue; /* OK to send */
5557             } else {
5558                 fp->eth_q_stats.tx_window_violation_tso++;
5559             }
5560         } else {
5561             fp->eth_q_stats.tx_window_violation_std++;
5562         }
5563
5564         /* lets try to defragment this mbuf and remap it */
5565         fp->eth_q_stats.mbuf_defrag_attempts++;
5566         bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5567
5568         m0 = m_defrag(*m_head, M_DONTWAIT);
5569         if (m0 == NULL) {
5570             fp->eth_q_stats.mbuf_defrag_failures++;
5571             /* Ugh, just drop the frame... :( */
5572             rc = ENOBUFS;
5573         } else {
5574             /* defrag successful, try mapping again */
5575             *m_head = m0;
5576             error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag,
5577                                             tx_buf->m_map, m0,
5578                                             segs, &nsegs, BUS_DMA_NOWAIT);
5579             if (error) {
5580                 fp->eth_q_stats.tx_dma_mapping_failure++;
5581                 /* No sense in trying to defrag/copy chain, drop it. :( */
5582                 rc = error;
5583             }
5584             else {
5585                 /* if the chain is still too long then drop it */
5586                 if (__predict_false(nsegs > BXE_MAX_SEGMENTS)) {
5587                     bus_dmamap_unload(fp->tx_mbuf_tag, tx_buf->m_map);
5588                     rc = ENODEV;
5589                 }
5590             }
5591         }
5592     }
5593
5594 bxe_tx_encap_continue:
5595
5596     /* Check for errors */
5597     if (rc) {
5598         if (rc == ENOMEM) {
5599             /* recoverable try again later  */
5600         } else {
5601             fp->eth_q_stats.tx_soft_errors++;
5602             fp->eth_q_stats.mbuf_alloc_tx--;
5603             m_freem(*m_head);
5604             *m_head = NULL;
5605         }
5606
5607         return (rc);
5608     }
5609
5610     /* set flag according to packet type (UNICAST_ADDRESS is default) */
5611     if (m0->m_flags & M_BCAST) {
5612         mac_type = BROADCAST_ADDRESS;
5613     } else if (m0->m_flags & M_MCAST) {
5614         mac_type = MULTICAST_ADDRESS;
5615     }
5616
5617     /* store the mbuf into the mbuf ring */
5618     tx_buf->m        = m0;
5619     tx_buf->first_bd = fp->tx_bd_prod;
5620     tx_buf->flags    = 0;
5621
5622     /* prepare the first transmit (start) BD for the mbuf */
5623     tx_start_bd = &fp->tx_chain[TX_BD(bd_prod)].start_bd;
5624
5625     BLOGD(sc, DBG_TX,
5626           "sending pkt_prod=%u tx_buf=%p next_idx=%u bd=%u tx_start_bd=%p\n",
5627           pkt_prod, tx_buf, fp->tx_pkt_prod, bd_prod, tx_start_bd);
5628
5629     tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
5630     tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
5631     tx_start_bd->nbytes  = htole16(segs[0].ds_len);
5632     total_pkt_size += tx_start_bd->nbytes;
5633     tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
5634
5635     tx_start_bd->general_data = (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
5636
5637     /* all frames have at least Start BD + Parsing BD */
5638     nbds = nsegs + 1;
5639     tx_start_bd->nbd = htole16(nbds);
5640
5641     if (m0->m_flags & M_VLANTAG) {
5642         tx_start_bd->vlan_or_ethertype = htole16(m0->m_pkthdr.ether_vtag);
5643         tx_start_bd->bd_flags.as_bitfield |=
5644             (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
5645     } else {
5646         /* vf tx, start bd must hold the ethertype for fw to enforce it */
5647         if (IS_VF(sc)) {
5648             /* map ethernet header to find type and header length */
5649             eh = mtod(m0, struct ether_vlan_header *);
5650             tx_start_bd->vlan_or_ethertype = eh->evl_encap_proto;
5651         } else {
5652             /* used by FW for packet accounting */
5653             tx_start_bd->vlan_or_ethertype = htole16(fp->tx_pkt_prod);
5654 #if 0
5655             /*
5656              * If NPAR-SD is active then FW should do the tagging regardless
5657              * of value of priority. Otherwise, if priority indicates this is
5658              * a control packet we need to indicate to FW to avoid tagging.
5659              */
5660             if (!IS_MF_AFEX(sc) && (mbuf priority == PRIO_CONTROL)) {
5661                 SET_FLAG(tx_start_bd->general_data,
5662                          ETH_TX_START_BD_FORCE_VLAN_MODE, 1);
5663             }
5664 #endif
5665         }
5666     }
5667
5668     /*
5669      * add a parsing BD from the chain. The parsing BD is always added
5670      * though it is only used for TSO and chksum
5671      */
5672     bd_prod = TX_BD_NEXT(bd_prod);
5673
5674     if (m0->m_pkthdr.csum_flags) {
5675         if (m0->m_pkthdr.csum_flags & CSUM_IP) {
5676             fp->eth_q_stats.tx_ofld_frames_csum_ip++;
5677             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IP_CSUM;
5678         }
5679
5680         if (m0->m_pkthdr.csum_flags & CSUM_TCP_IPV6) {
5681             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6 |
5682                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5683         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP_IPV6) {
5684             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_IPV6   |
5685                                                   ETH_TX_BD_FLAGS_IS_UDP |
5686                                                   ETH_TX_BD_FLAGS_L4_CSUM);
5687         } else if ((m0->m_pkthdr.csum_flags & CSUM_TCP) ||
5688                    (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5689             tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
5690         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
5691             tx_start_bd->bd_flags.as_bitfield |= (ETH_TX_BD_FLAGS_L4_CSUM |
5692                                                   ETH_TX_BD_FLAGS_IS_UDP);
5693         }
5694     }
5695
5696     if (!CHIP_IS_E1x(sc)) {
5697         pbd_e2 = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e2;
5698         memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
5699
5700         if (m0->m_pkthdr.csum_flags) {
5701             hlen = bxe_set_pbd_csum_e2(fp, m0, &pbd_e2_parsing_data);
5702         }
5703
5704 #if 0
5705         /*
5706          * Add the MACs to the parsing BD if the module param was
5707          * explicitly set, if this is a vf, or in switch independent
5708          * mode.
5709          */
5710         if (sc->flags & BXE_TX_SWITCHING || IS_VF(sc) || IS_MF_SI(sc)) {
5711             eh = mtod(m0, struct ether_vlan_header *);
5712             bxe_set_fw_mac_addr(&pbd_e2->data.mac_addr.src_hi,
5713                                 &pbd_e2->data.mac_addr.src_mid,
5714                                 &pbd_e2->data.mac_addr.src_lo,
5715                                 eh->evl_shost);
5716             bxe_set_fw_mac_addr(&pbd_e2->data.mac_addr.dst_hi,
5717                                 &pbd_e2->data.mac_addr.dst_mid,
5718                                 &pbd_e2->data.mac_addr.dst_lo,
5719                                 eh->evl_dhost);
5720         }
5721 #endif
5722
5723         SET_FLAG(pbd_e2_parsing_data, ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE,
5724                  mac_type);
5725     } else {
5726         uint16_t global_data = 0;
5727
5728         pbd_e1x = &fp->tx_chain[TX_BD(bd_prod)].parse_bd_e1x;
5729         memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
5730
5731         if (m0->m_pkthdr.csum_flags) {
5732             hlen = bxe_set_pbd_csum(fp, m0, pbd_e1x);
5733         }
5734
5735         SET_FLAG(global_data,
5736                  ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
5737         pbd_e1x->global_data |= htole16(global_data);
5738     }
5739
5740     /* setup the parsing BD with TSO specific info */
5741     if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
5742         fp->eth_q_stats.tx_ofld_frames_lso++;
5743         tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
5744
5745         if (__predict_false(tx_start_bd->nbytes > hlen)) {
5746             fp->eth_q_stats.tx_ofld_frames_lso_hdr_splits++;
5747
5748             /* split the first BD into header/data making the fw job easy */
5749             nbds++;
5750             tx_start_bd->nbd = htole16(nbds);
5751             tx_start_bd->nbytes = htole16(hlen);
5752
5753             bd_prod = TX_BD_NEXT(bd_prod);
5754
5755             /* new transmit BD after the tx_parse_bd */
5756             tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5757             tx_data_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr + hlen));
5758             tx_data_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr + hlen));
5759             tx_data_bd->nbytes  = htole16(segs[0].ds_len - hlen);
5760             if (tx_total_pkt_size_bd == NULL) {
5761                 tx_total_pkt_size_bd = tx_data_bd;
5762             }
5763
5764             BLOGD(sc, DBG_TX,
5765                   "TSO split header size is %d (%x:%x) nbds %d\n",
5766                   le16toh(tx_start_bd->nbytes),
5767                   le32toh(tx_start_bd->addr_hi),
5768                   le32toh(tx_start_bd->addr_lo),
5769                   nbds);
5770         }
5771
5772         if (!CHIP_IS_E1x(sc)) {
5773             bxe_set_pbd_lso_e2(m0, &pbd_e2_parsing_data);
5774         } else {
5775             bxe_set_pbd_lso(m0, pbd_e1x);
5776         }
5777     }
5778
5779     if (pbd_e2_parsing_data) {
5780         pbd_e2->parsing_data = htole32(pbd_e2_parsing_data);
5781     }
5782
5783     /* prepare remaining BDs, start tx bd contains first seg/frag */
5784     for (i = 1; i < nsegs ; i++) {
5785         bd_prod = TX_BD_NEXT(bd_prod);
5786         tx_data_bd = &fp->tx_chain[TX_BD(bd_prod)].reg_bd;
5787         tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
5788         tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
5789         tx_data_bd->nbytes  = htole16(segs[i].ds_len);
5790         if (tx_total_pkt_size_bd == NULL) {
5791             tx_total_pkt_size_bd = tx_data_bd;
5792         }
5793         total_pkt_size += tx_data_bd->nbytes;
5794     }
5795
5796     BLOGD(sc, DBG_TX, "last bd %p\n", tx_data_bd);
5797
5798     if (tx_total_pkt_size_bd != NULL) {
5799         tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
5800     }
5801
5802     if (__predict_false(sc->debug & DBG_TX)) {
5803         tmp_bd = tx_buf->first_bd;
5804         for (i = 0; i < nbds; i++)
5805         {
5806             if (i == 0) {
5807                 BLOGD(sc, DBG_TX,
5808                       "TX Strt: %p bd=%d nbd=%d vlan=0x%x "
5809                       "bd_flags=0x%x hdr_nbds=%d\n",
5810                       tx_start_bd,
5811                       tmp_bd,
5812                       le16toh(tx_start_bd->nbd),
5813                       le16toh(tx_start_bd->vlan_or_ethertype),
5814                       tx_start_bd->bd_flags.as_bitfield,
5815                       (tx_start_bd->general_data & ETH_TX_START_BD_HDR_NBDS));
5816             } else if (i == 1) {
5817                 if (pbd_e1x) {
5818                     BLOGD(sc, DBG_TX,
5819                           "-> Prse: %p bd=%d global=0x%x ip_hlen_w=%u "
5820                           "ip_id=%u lso_mss=%u tcp_flags=0x%x csum=0x%x "
5821                           "tcp_seq=%u total_hlen_w=%u\n",
5822                           pbd_e1x,
5823                           tmp_bd,
5824                           pbd_e1x->global_data,
5825                           pbd_e1x->ip_hlen_w,
5826                           pbd_e1x->ip_id,
5827                           pbd_e1x->lso_mss,
5828                           pbd_e1x->tcp_flags,
5829                           pbd_e1x->tcp_pseudo_csum,
5830                           pbd_e1x->tcp_send_seq,
5831                           le16toh(pbd_e1x->total_hlen_w));
5832                 } else { /* if (pbd_e2) */
5833                     BLOGD(sc, DBG_TX,
5834                           "-> Parse: %p bd=%d dst=%02x:%02x:%02x "
5835                           "src=%02x:%02x:%02x parsing_data=0x%x\n",
5836                           pbd_e2,
5837                           tmp_bd,
5838                           pbd_e2->data.mac_addr.dst_hi,
5839                           pbd_e2->data.mac_addr.dst_mid,
5840                           pbd_e2->data.mac_addr.dst_lo,
5841                           pbd_e2->data.mac_addr.src_hi,
5842                           pbd_e2->data.mac_addr.src_mid,
5843                           pbd_e2->data.mac_addr.src_lo,
5844                           pbd_e2->parsing_data);
5845                 }
5846             }
5847
5848             if (i != 1) { /* skip parse db as it doesn't hold data */
5849                 tx_data_bd = &fp->tx_chain[TX_BD(tmp_bd)].reg_bd;
5850                 BLOGD(sc, DBG_TX,
5851                       "-> Frag: %p bd=%d nbytes=%d hi=0x%x lo: 0x%x\n",
5852                       tx_data_bd,
5853                       tmp_bd,
5854                       le16toh(tx_data_bd->nbytes),
5855                       le32toh(tx_data_bd->addr_hi),
5856                       le32toh(tx_data_bd->addr_lo));
5857             }
5858
5859             tmp_bd = TX_BD_NEXT(tmp_bd);
5860         }
5861     }
5862
5863     BLOGD(sc, DBG_TX, "doorbell: nbds=%d bd=%u\n", nbds, bd_prod);
5864
5865     /* update TX BD producer index value for next TX */
5866     bd_prod = TX_BD_NEXT(bd_prod);
5867
5868     /*
5869      * If the chain of tx_bd's describing this frame is adjacent to or spans
5870      * an eth_tx_next_bd element then we need to increment the nbds value.
5871      */
5872     if (TX_BD_IDX(bd_prod) < nbds) {
5873         nbds++;
5874     }
5875
5876     /* don't allow reordering of writes for nbd and packets */
5877     mb();
5878
5879     fp->tx_db.data.prod += nbds;
5880
5881     /* producer points to the next free tx_bd at this point */
5882     fp->tx_pkt_prod++;
5883     fp->tx_bd_prod = bd_prod;
5884
5885     DOORBELL(sc, fp->index, fp->tx_db.raw);
5886
5887     fp->eth_q_stats.tx_pkts++;
5888
5889     /* Prevent speculative reads from getting ahead of the status block. */
5890     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle,
5891                       0, 0, BUS_SPACE_BARRIER_READ);
5892
5893     /* Prevent speculative reads from getting ahead of the doorbell. */
5894     bus_space_barrier(sc->bar[BAR2].tag, sc->bar[BAR2].handle,
5895                       0, 0, BUS_SPACE_BARRIER_READ);
5896
5897     return (0);
5898 }
5899
5900 static void
5901 bxe_tx_start_locked(struct bxe_softc    *sc,
5902                     struct ifnet        *ifp,
5903                     struct bxe_fastpath *fp)
5904 {
5905     struct mbuf *m = NULL;
5906     int tx_count = 0;
5907     uint16_t tx_bd_avail;
5908
5909     BXE_FP_TX_LOCK_ASSERT(fp);
5910
5911     /* keep adding entries while there are frames to send */
5912     while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
5913
5914         /*
5915          * check for any frames to send
5916          * dequeue can still be NULL even if queue is not empty
5917          */
5918         IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
5919         if (__predict_false(m == NULL)) {
5920             break;
5921         }
5922
5923         /* the mbuf now belongs to us */
5924         fp->eth_q_stats.mbuf_alloc_tx++;
5925
5926         /*
5927          * Put the frame into the transmit ring. If we don't have room,
5928          * place the mbuf back at the head of the TX queue, set the
5929          * OACTIVE flag, and wait for the NIC to drain the chain.
5930          */
5931         if (__predict_false(bxe_tx_encap(fp, &m))) {
5932             fp->eth_q_stats.tx_encap_failures++;
5933             if (m != NULL) {
5934                 /* mark the TX queue as full and return the frame */
5935                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5936                 IFQ_DRV_PREPEND(&ifp->if_snd, m);
5937                 fp->eth_q_stats.mbuf_alloc_tx--;
5938                 fp->eth_q_stats.tx_queue_xoff++;
5939             }
5940
5941             /* stop looking for more work */
5942             break;
5943         }
5944
5945         /* the frame was enqueued successfully */
5946         tx_count++;
5947
5948         /* send a copy of the frame to any BPF listeners. */
5949         BPF_MTAP(ifp, m);
5950
5951         tx_bd_avail = bxe_tx_avail(sc, fp);
5952
5953         /* handle any completions if we're running low */
5954         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
5955             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
5956             bxe_txeof(sc, fp);
5957             if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5958                 break;
5959             }
5960         }
5961     }
5962
5963     /* all TX packets were dequeued and/or the tx ring is full */
5964     if (tx_count > 0) {
5965         /* reset the TX watchdog timeout timer */
5966         fp->watchdog_timer = BXE_TX_TIMEOUT;
5967     }
5968 }
5969
5970 /* Legacy (non-RSS) dispatch routine */
5971 static void
5972 bxe_tx_start(struct ifnet *ifp)
5973 {
5974     struct bxe_softc *sc;
5975     struct bxe_fastpath *fp;
5976
5977     sc = ifp->if_softc;
5978
5979     if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
5980         BLOGW(sc, "Interface not running, ignoring transmit request\n");
5981         return;
5982     }
5983
5984     if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5985         BLOGW(sc, "Interface TX queue is full, ignoring transmit request\n");
5986         return;
5987     }
5988
5989     if (!sc->link_vars.link_up) {
5990         BLOGW(sc, "Interface link is down, ignoring transmit request\n");
5991         return;
5992     }
5993
5994     fp = &sc->fp[0];
5995
5996     BXE_FP_TX_LOCK(fp);
5997     bxe_tx_start_locked(sc, ifp, fp);
5998     BXE_FP_TX_UNLOCK(fp);
5999 }
6000
6001 #if __FreeBSD_version >= 800000
6002
6003 static int
6004 bxe_tx_mq_start_locked(struct bxe_softc    *sc,
6005                        struct ifnet        *ifp,
6006                        struct bxe_fastpath *fp,
6007                        struct mbuf         *m)
6008 {
6009     struct buf_ring *tx_br = fp->tx_br;
6010     struct mbuf *next;
6011     int depth, rc, tx_count;
6012     uint16_t tx_bd_avail;
6013
6014     rc = tx_count = 0;
6015
6016     if (!tx_br) {
6017         BLOGE(sc, "Multiqueue TX and no buf_ring!\n");
6018         return (EINVAL);
6019     }
6020
6021     /* fetch the depth of the driver queue */
6022     depth = drbr_inuse(ifp, tx_br);
6023     if (depth > fp->eth_q_stats.tx_max_drbr_queue_depth) {
6024         fp->eth_q_stats.tx_max_drbr_queue_depth = depth;
6025     }
6026
6027     BXE_FP_TX_LOCK_ASSERT(fp);
6028
6029     if (m == NULL) {
6030         /* no new work, check for pending frames */
6031         next = drbr_dequeue(ifp, tx_br);
6032     } else if (drbr_needs_enqueue(ifp, tx_br)) {
6033         /* have both new and pending work, maintain packet order */
6034         rc = drbr_enqueue(ifp, tx_br, m);
6035         if (rc != 0) {
6036             fp->eth_q_stats.tx_soft_errors++;
6037             goto bxe_tx_mq_start_locked_exit;
6038         }
6039         next = drbr_dequeue(ifp, tx_br);
6040     } else {
6041         /* new work only and nothing pending */
6042         next = m;
6043     }
6044
6045     /* keep adding entries while there are frames to send */
6046     while (next != NULL) {
6047
6048         /* the mbuf now belongs to us */
6049         fp->eth_q_stats.mbuf_alloc_tx++;
6050
6051         /*
6052          * Put the frame into the transmit ring. If we don't have room,
6053          * place the mbuf back at the head of the TX queue, set the
6054          * OACTIVE flag, and wait for the NIC to drain the chain.
6055          */
6056         rc = bxe_tx_encap(fp, &next);
6057         if (__predict_false(rc != 0)) {
6058             fp->eth_q_stats.tx_encap_failures++;
6059             if (next != NULL) {
6060                 /* mark the TX queue as full and save the frame */
6061                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
6062                 /* XXX this may reorder the frame */
6063                 rc = drbr_enqueue(ifp, tx_br, next);
6064                 fp->eth_q_stats.mbuf_alloc_tx--;
6065                 fp->eth_q_stats.tx_frames_deferred++;
6066             }
6067
6068             /* stop looking for more work */
6069             break;
6070         }
6071
6072         /* the transmit frame was enqueued successfully */
6073         tx_count++;
6074
6075         /* send a copy of the frame to any BPF listeners */
6076         BPF_MTAP(ifp, next);
6077
6078         tx_bd_avail = bxe_tx_avail(sc, fp);
6079
6080         /* handle any completions if we're running low */
6081         if (tx_bd_avail < BXE_TX_CLEANUP_THRESHOLD) {
6082             /* bxe_txeof will set IFF_DRV_OACTIVE appropriately */
6083             bxe_txeof(sc, fp);
6084             if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
6085                 break;
6086             }
6087         }
6088
6089         next = drbr_dequeue(ifp, tx_br);
6090     }
6091
6092     /* all TX packets were dequeued and/or the tx ring is full */
6093     if (tx_count > 0) {
6094         /* reset the TX watchdog timeout timer */
6095         fp->watchdog_timer = BXE_TX_TIMEOUT;
6096     }
6097
6098 bxe_tx_mq_start_locked_exit:
6099
6100     return (rc);
6101 }
6102
6103 /* Multiqueue (TSS) dispatch routine. */
6104 static int
6105 bxe_tx_mq_start(struct ifnet *ifp,
6106                 struct mbuf  *m)
6107 {
6108     struct bxe_softc *sc = ifp->if_softc;
6109     struct bxe_fastpath *fp;
6110     int fp_index, rc;
6111
6112     fp_index = 0; /* default is the first queue */
6113
6114     /* change the queue if using flow ID */
6115     if ((m->m_flags & M_FLOWID) != 0) {
6116         fp_index = (m->m_pkthdr.flowid % sc->num_queues);
6117     }
6118
6119     fp = &sc->fp[fp_index];
6120
6121     if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
6122         BLOGW(sc, "Interface not running, ignoring transmit request\n");
6123         return (ENETDOWN);
6124     }
6125
6126     if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
6127         BLOGW(sc, "Interface TX queue is full, ignoring transmit request\n");
6128         return (EBUSY);
6129     }
6130
6131     if (!sc->link_vars.link_up) {
6132         BLOGW(sc, "Interface link is down, ignoring transmit request\n");
6133         return (ENETDOWN);
6134     }
6135
6136     /* XXX change to TRYLOCK here and if failed then schedule taskqueue */
6137
6138     BXE_FP_TX_LOCK(fp);
6139     rc = bxe_tx_mq_start_locked(sc, ifp, fp, m);
6140     BXE_FP_TX_UNLOCK(fp);
6141
6142     return (rc);
6143 }
6144
6145 static void
6146 bxe_mq_flush(struct ifnet *ifp)
6147 {
6148     struct bxe_softc *sc = ifp->if_softc;
6149     struct bxe_fastpath *fp;
6150     struct mbuf *m;
6151     int i;
6152
6153     for (i = 0; i < sc->num_queues; i++) {
6154         fp = &sc->fp[i];
6155
6156         if (fp->state != BXE_FP_STATE_OPEN) {
6157             BLOGD(sc, DBG_LOAD, "Not clearing fp[%02d] buf_ring (state=%d)\n",
6158                   fp->index, fp->state);
6159             continue;
6160         }
6161
6162         if (fp->tx_br != NULL) {
6163             BLOGD(sc, DBG_LOAD, "Clearing fp[%02d] buf_ring\n", fp->index);
6164             BXE_FP_TX_LOCK(fp);
6165             while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL) {
6166                 m_freem(m);
6167             }
6168             BXE_FP_TX_UNLOCK(fp);
6169         }
6170     }
6171
6172     if_qflush(ifp);
6173 }
6174
6175 #endif /* FreeBSD_version >= 800000 */
6176
6177 static uint16_t
6178 bxe_cid_ilt_lines(struct bxe_softc *sc)
6179 {
6180     if (IS_SRIOV(sc)) {
6181         return ((BXE_FIRST_VF_CID + BXE_VF_CIDS) / ILT_PAGE_CIDS);
6182     }
6183     return (L2_ILT_LINES(sc));
6184 }
6185
6186 static void
6187 bxe_ilt_set_info(struct bxe_softc *sc)
6188 {
6189     struct ilt_client_info *ilt_client;
6190     struct ecore_ilt *ilt = sc->ilt;
6191     uint16_t line = 0;
6192
6193     ilt->start_line = FUNC_ILT_BASE(SC_FUNC(sc));
6194     BLOGD(sc, DBG_LOAD, "ilt starts at line %d\n", ilt->start_line);
6195
6196     /* CDU */
6197     ilt_client = &ilt->clients[ILT_CLIENT_CDU];
6198     ilt_client->client_num = ILT_CLIENT_CDU;
6199     ilt_client->page_size = CDU_ILT_PAGE_SZ;
6200     ilt_client->flags = ILT_CLIENT_SKIP_MEM;
6201     ilt_client->start = line;
6202     line += bxe_cid_ilt_lines(sc);
6203
6204     if (CNIC_SUPPORT(sc)) {
6205         line += CNIC_ILT_LINES;
6206     }
6207
6208     ilt_client->end = (line - 1);
6209
6210     BLOGD(sc, DBG_LOAD,
6211           "ilt client[CDU]: start %d, end %d, "
6212           "psz 0x%x, flags 0x%x, hw psz %d\n",
6213           ilt_client->start, ilt_client->end,
6214           ilt_client->page_size,
6215           ilt_client->flags,
6216           ilog2(ilt_client->page_size >> 12));
6217
6218     /* QM */
6219     if (QM_INIT(sc->qm_cid_count)) {
6220         ilt_client = &ilt->clients[ILT_CLIENT_QM];
6221         ilt_client->client_num = ILT_CLIENT_QM;
6222         ilt_client->page_size = QM_ILT_PAGE_SZ;
6223         ilt_client->flags = 0;
6224         ilt_client->start = line;
6225
6226         /* 4 bytes for each cid */
6227         line += DIV_ROUND_UP(sc->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
6228                              QM_ILT_PAGE_SZ);
6229
6230         ilt_client->end = (line - 1);
6231
6232         BLOGD(sc, DBG_LOAD,
6233               "ilt client[QM]: start %d, end %d, "
6234               "psz 0x%x, flags 0x%x, hw psz %d\n",
6235               ilt_client->start, ilt_client->end,
6236               ilt_client->page_size, ilt_client->flags,
6237               ilog2(ilt_client->page_size >> 12));
6238     }
6239
6240     if (CNIC_SUPPORT(sc)) {
6241         /* SRC */
6242         ilt_client = &ilt->clients[ILT_CLIENT_SRC];
6243         ilt_client->client_num = ILT_CLIENT_SRC;
6244         ilt_client->page_size = SRC_ILT_PAGE_SZ;
6245         ilt_client->flags = 0;
6246         ilt_client->start = line;
6247         line += SRC_ILT_LINES;
6248         ilt_client->end = (line - 1);
6249
6250         BLOGD(sc, DBG_LOAD,
6251               "ilt client[SRC]: start %d, end %d, "
6252               "psz 0x%x, flags 0x%x, hw psz %d\n",
6253               ilt_client->start, ilt_client->end,
6254               ilt_client->page_size, ilt_client->flags,
6255               ilog2(ilt_client->page_size >> 12));
6256
6257         /* TM */
6258         ilt_client = &ilt->clients[ILT_CLIENT_TM];
6259         ilt_client->client_num = ILT_CLIENT_TM;
6260         ilt_client->page_size = TM_ILT_PAGE_SZ;
6261         ilt_client->flags = 0;
6262         ilt_client->start = line;
6263         line += TM_ILT_LINES;
6264         ilt_client->end = (line - 1);
6265
6266         BLOGD(sc, DBG_LOAD,
6267               "ilt client[TM]: start %d, end %d, "
6268               "psz 0x%x, flags 0x%x, hw psz %d\n",
6269               ilt_client->start, ilt_client->end,
6270               ilt_client->page_size, ilt_client->flags,
6271               ilog2(ilt_client->page_size >> 12));
6272     }
6273
6274     KASSERT((line <= ILT_MAX_LINES), ("Invalid number of ILT lines!"));
6275 }
6276
6277 static void
6278 bxe_set_fp_rx_buf_size(struct bxe_softc *sc)
6279 {
6280     int i;
6281     uint32_t rx_buf_size;
6282
6283     rx_buf_size = (IP_HEADER_ALIGNMENT_PADDING + ETH_OVERHEAD + sc->mtu);
6284
6285     for (i = 0; i < sc->num_queues; i++) {
6286         if(rx_buf_size <= MCLBYTES){
6287             sc->fp[i].rx_buf_size = rx_buf_size;
6288             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6289         }else if (rx_buf_size <= MJUMPAGESIZE){
6290             sc->fp[i].rx_buf_size = rx_buf_size;
6291             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6292         }else if (rx_buf_size <= (MJUMPAGESIZE + MCLBYTES)){
6293             sc->fp[i].rx_buf_size = MCLBYTES;
6294             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6295         }else if (rx_buf_size <= (2 * MJUMPAGESIZE)){
6296             sc->fp[i].rx_buf_size = MJUMPAGESIZE;
6297             sc->fp[i].mbuf_alloc_size = MJUMPAGESIZE;
6298         }else {
6299             sc->fp[i].rx_buf_size = MCLBYTES;
6300             sc->fp[i].mbuf_alloc_size = MCLBYTES;
6301         }
6302     }
6303 }
6304
6305 static int
6306 bxe_alloc_ilt_mem(struct bxe_softc *sc)
6307 {
6308     int rc = 0;
6309
6310     if ((sc->ilt =
6311          (struct ecore_ilt *)malloc(sizeof(struct ecore_ilt),
6312                                     M_BXE_ILT,
6313                                     (M_NOWAIT | M_ZERO))) == NULL) {
6314         rc = 1;
6315     }
6316
6317     return (rc);
6318 }
6319
6320 static int
6321 bxe_alloc_ilt_lines_mem(struct bxe_softc *sc)
6322 {
6323     int rc = 0;
6324
6325     if ((sc->ilt->lines =
6326          (struct ilt_line *)malloc((sizeof(struct ilt_line) * ILT_MAX_LINES),
6327                                     M_BXE_ILT,
6328                                     (M_NOWAIT | M_ZERO))) == NULL) {
6329         rc = 1;
6330     }
6331
6332     return (rc);
6333 }
6334
6335 static void
6336 bxe_free_ilt_mem(struct bxe_softc *sc)
6337 {
6338     if (sc->ilt != NULL) {
6339         free(sc->ilt, M_BXE_ILT);
6340         sc->ilt = NULL;
6341     }
6342 }
6343
6344 static void
6345 bxe_free_ilt_lines_mem(struct bxe_softc *sc)
6346 {
6347     if (sc->ilt->lines != NULL) {
6348         free(sc->ilt->lines, M_BXE_ILT);
6349         sc->ilt->lines = NULL;
6350     }
6351 }
6352
6353 static void
6354 bxe_free_mem(struct bxe_softc *sc)
6355 {
6356     int i;
6357
6358 #if 0
6359     if (!CONFIGURE_NIC_MODE(sc)) {
6360         /* free searcher T2 table */
6361         bxe_dma_free(sc, &sc->t2);
6362     }
6363 #endif
6364
6365     for (i = 0; i < L2_ILT_LINES(sc); i++) {
6366         bxe_dma_free(sc, &sc->context[i].vcxt_dma);
6367         sc->context[i].vcxt = NULL;
6368         sc->context[i].size = 0;
6369     }
6370
6371     ecore_ilt_mem_op(sc, ILT_MEMOP_FREE);
6372
6373     bxe_free_ilt_lines_mem(sc);
6374
6375 #if 0
6376     bxe_iov_free_mem(sc);
6377 #endif
6378 }
6379
6380 static int
6381 bxe_alloc_mem(struct bxe_softc *sc)
6382 {
6383     int context_size;
6384     int allocated;
6385     int i;
6386
6387 #if 0
6388     if (!CONFIGURE_NIC_MODE(sc)) {
6389         /* allocate searcher T2 table */
6390         if (bxe_dma_alloc(sc, SRC_T2_SZ,
6391                           &sc->t2, "searcher t2 table") != 0) {
6392             return (-1);
6393         }
6394     }
6395 #endif
6396
6397     /*
6398      * Allocate memory for CDU context:
6399      * This memory is allocated separately and not in the generic ILT
6400      * functions because CDU differs in few aspects:
6401      * 1. There can be multiple entities allocating memory for context -
6402      * regular L2, CNIC, and SRIOV drivers. Each separately controls
6403      * its own ILT lines.
6404      * 2. Since CDU page-size is not a single 4KB page (which is the case
6405      * for the other ILT clients), to be efficient we want to support
6406      * allocation of sub-page-size in the last entry.
6407      * 3. Context pointers are used by the driver to pass to FW / update
6408      * the context (for the other ILT clients the pointers are used just to
6409      * free the memory during unload).
6410      */
6411     context_size = (sizeof(union cdu_context) * BXE_L2_CID_COUNT(sc));
6412     for (i = 0, allocated = 0; allocated < context_size; i++) {
6413         sc->context[i].size = min(CDU_ILT_PAGE_SZ,
6414                                   (context_size - allocated));
6415
6416         if (bxe_dma_alloc(sc, sc->context[i].size,
6417                           &sc->context[i].vcxt_dma,
6418                           "cdu context") != 0) {
6419             bxe_free_mem(sc);
6420             return (-1);
6421         }
6422
6423         sc->context[i].vcxt =
6424             (union cdu_context *)sc->context[i].vcxt_dma.vaddr;
6425
6426         allocated += sc->context[i].size;
6427     }
6428
6429     bxe_alloc_ilt_lines_mem(sc);
6430
6431     BLOGD(sc, DBG_LOAD, "ilt=%p start_line=%u lines=%p\n",
6432           sc->ilt, sc->ilt->start_line, sc->ilt->lines);
6433     {
6434         for (i = 0; i < 4; i++) {
6435             BLOGD(sc, DBG_LOAD,
6436                   "c%d page_size=%u start=%u end=%u num=%u flags=0x%x\n",
6437                   i,
6438                   sc->ilt->clients[i].page_size,
6439                   sc->ilt->clients[i].start,
6440                   sc->ilt->clients[i].end,
6441                   sc->ilt->clients[i].client_num,
6442                   sc->ilt->clients[i].flags);
6443         }
6444     }
6445     if (ecore_ilt_mem_op(sc, ILT_MEMOP_ALLOC)) {
6446         BLOGE(sc, "ecore_ilt_mem_op ILT_MEMOP_ALLOC failed\n");
6447         bxe_free_mem(sc);
6448         return (-1);
6449     }
6450
6451 #if 0
6452     if (bxe_iov_alloc_mem(sc)) {
6453         BLOGE(sc, "Failed to allocate memory for SRIOV\n");
6454         bxe_free_mem(sc);
6455         return (-1);
6456     }
6457 #endif
6458
6459     return (0);
6460 }
6461
6462 static void
6463 bxe_free_rx_bd_chain(struct bxe_fastpath *fp)
6464 {
6465     struct bxe_softc *sc;
6466     int i;
6467
6468     sc = fp->sc;
6469
6470     if (fp->rx_mbuf_tag == NULL) {
6471         return;
6472     }
6473
6474     /* free all mbufs and unload all maps */
6475     for (i = 0; i < RX_BD_TOTAL; i++) {
6476         if (fp->rx_mbuf_chain[i].m_map != NULL) {
6477             bus_dmamap_sync(fp->rx_mbuf_tag,
6478                             fp->rx_mbuf_chain[i].m_map,
6479                             BUS_DMASYNC_POSTREAD);
6480             bus_dmamap_unload(fp->rx_mbuf_tag,
6481                               fp->rx_mbuf_chain[i].m_map);
6482         }
6483
6484         if (fp->rx_mbuf_chain[i].m != NULL) {
6485             m_freem(fp->rx_mbuf_chain[i].m);
6486             fp->rx_mbuf_chain[i].m = NULL;
6487             fp->eth_q_stats.mbuf_alloc_rx--;
6488         }
6489     }
6490 }
6491
6492 static void
6493 bxe_free_tpa_pool(struct bxe_fastpath *fp)
6494 {
6495     struct bxe_softc *sc;
6496     int i, max_agg_queues;
6497
6498     sc = fp->sc;
6499
6500     if (fp->rx_mbuf_tag == NULL) {
6501         return;
6502     }
6503
6504     max_agg_queues = MAX_AGG_QS(sc);
6505
6506     /* release all mbufs and unload all DMA maps in the TPA pool */
6507     for (i = 0; i < max_agg_queues; i++) {
6508         if (fp->rx_tpa_info[i].bd.m_map != NULL) {
6509             bus_dmamap_sync(fp->rx_mbuf_tag,
6510                             fp->rx_tpa_info[i].bd.m_map,
6511                             BUS_DMASYNC_POSTREAD);
6512             bus_dmamap_unload(fp->rx_mbuf_tag,
6513                               fp->rx_tpa_info[i].bd.m_map);
6514         }
6515
6516         if (fp->rx_tpa_info[i].bd.m != NULL) {
6517             m_freem(fp->rx_tpa_info[i].bd.m);
6518             fp->rx_tpa_info[i].bd.m = NULL;
6519             fp->eth_q_stats.mbuf_alloc_tpa--;
6520         }
6521     }
6522 }
6523
6524 static void
6525 bxe_free_sge_chain(struct bxe_fastpath *fp)
6526 {
6527     struct bxe_softc *sc;
6528     int i;
6529
6530     sc = fp->sc;
6531
6532     if (fp->rx_sge_mbuf_tag == NULL) {
6533         return;
6534     }
6535
6536     /* rree all mbufs and unload all maps */
6537     for (i = 0; i < RX_SGE_TOTAL; i++) {
6538         if (fp->rx_sge_mbuf_chain[i].m_map != NULL) {
6539             bus_dmamap_sync(fp->rx_sge_mbuf_tag,
6540                             fp->rx_sge_mbuf_chain[i].m_map,
6541                             BUS_DMASYNC_POSTREAD);
6542             bus_dmamap_unload(fp->rx_sge_mbuf_tag,
6543                               fp->rx_sge_mbuf_chain[i].m_map);
6544         }
6545
6546         if (fp->rx_sge_mbuf_chain[i].m != NULL) {
6547             m_freem(fp->rx_sge_mbuf_chain[i].m);
6548             fp->rx_sge_mbuf_chain[i].m = NULL;
6549             fp->eth_q_stats.mbuf_alloc_sge--;
6550         }
6551     }
6552 }
6553
6554 static void
6555 bxe_free_fp_buffers(struct bxe_softc *sc)
6556 {
6557     struct bxe_fastpath *fp;
6558     int i;
6559
6560     for (i = 0; i < sc->num_queues; i++) {
6561         fp = &sc->fp[i];
6562
6563 #if __FreeBSD_version >= 800000
6564         if (fp->tx_br != NULL) {
6565             /* just in case bxe_mq_flush() wasn't called */
6566             if (mtx_initialized(&fp->tx_mtx)) {
6567                 struct mbuf *m;
6568
6569                 BXE_FP_TX_LOCK(fp);
6570                 while ((m = buf_ring_dequeue_sc(fp->tx_br)) != NULL)
6571                     m_freem(m);
6572                 BXE_FP_TX_UNLOCK(fp);
6573             }
6574             buf_ring_free(fp->tx_br, M_DEVBUF);
6575             fp->tx_br = NULL;
6576         }
6577 #endif
6578
6579         /* free all RX buffers */
6580         bxe_free_rx_bd_chain(fp);
6581         bxe_free_tpa_pool(fp);
6582         bxe_free_sge_chain(fp);
6583
6584         if (fp->eth_q_stats.mbuf_alloc_rx != 0) {
6585             BLOGE(sc, "failed to claim all rx mbufs (%d left)\n",
6586                   fp->eth_q_stats.mbuf_alloc_rx);
6587         }
6588
6589         if (fp->eth_q_stats.mbuf_alloc_sge != 0) {
6590             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6591                   fp->eth_q_stats.mbuf_alloc_sge);
6592         }
6593
6594         if (fp->eth_q_stats.mbuf_alloc_tpa != 0) {
6595             BLOGE(sc, "failed to claim all sge mbufs (%d left)\n",
6596                   fp->eth_q_stats.mbuf_alloc_tpa);
6597         }
6598
6599         if (fp->eth_q_stats.mbuf_alloc_tx != 0) {
6600             BLOGE(sc, "failed to release tx mbufs (%d left)\n",
6601                   fp->eth_q_stats.mbuf_alloc_tx);
6602         }
6603
6604         /* XXX verify all mbufs were reclaimed */
6605
6606         if (mtx_initialized(&fp->tx_mtx)) {
6607             mtx_destroy(&fp->tx_mtx);
6608         }
6609
6610         if (mtx_initialized(&fp->rx_mtx)) {
6611             mtx_destroy(&fp->rx_mtx);
6612         }
6613     }
6614 }
6615
6616 static int
6617 bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp,
6618                      uint16_t            prev_index,
6619                      uint16_t            index)
6620 {
6621     struct bxe_sw_rx_bd *rx_buf;
6622     struct eth_rx_bd *rx_bd;
6623     bus_dma_segment_t segs[1];
6624     bus_dmamap_t map;
6625     struct mbuf *m;
6626     int nsegs, rc;
6627
6628     rc = 0;
6629
6630     /* allocate the new RX BD mbuf */
6631     m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6632     if (__predict_false(m == NULL)) {
6633         fp->eth_q_stats.mbuf_rx_bd_alloc_failed++;
6634         return (ENOBUFS);
6635     }
6636
6637     fp->eth_q_stats.mbuf_alloc_rx++;
6638
6639     /* initialize the mbuf buffer length */
6640     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6641
6642     /* map the mbuf into non-paged pool */
6643     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6644                                  fp->rx_mbuf_spare_map,
6645                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6646     if (__predict_false(rc != 0)) {
6647         fp->eth_q_stats.mbuf_rx_bd_mapping_failed++;
6648         m_freem(m);
6649         fp->eth_q_stats.mbuf_alloc_rx--;
6650         return (rc);
6651     }
6652
6653     /* all mbufs must map to a single segment */
6654     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6655
6656     /* release any existing RX BD mbuf mappings */
6657
6658     if (prev_index != index) {
6659         rx_buf = &fp->rx_mbuf_chain[prev_index];
6660
6661         if (rx_buf->m_map != NULL) {
6662             bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6663                             BUS_DMASYNC_POSTREAD);
6664             bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6665         }
6666
6667         /*
6668          * We only get here from bxe_rxeof() when the maximum number
6669          * of rx buffers is less than RX_BD_USABLE. bxe_rxeof() already
6670          * holds the mbuf in the prev_index so it's OK to NULL it out
6671          * here without concern of a memory leak.
6672          */
6673         fp->rx_mbuf_chain[prev_index].m = NULL;
6674     }
6675
6676     rx_buf = &fp->rx_mbuf_chain[index];
6677
6678     if (rx_buf->m_map != NULL) {
6679         bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6680                         BUS_DMASYNC_POSTREAD);
6681         bus_dmamap_unload(fp->rx_mbuf_tag, rx_buf->m_map);
6682     }
6683
6684     /* save the mbuf and mapping info for a future packet */
6685     map = (prev_index != index) ?
6686               fp->rx_mbuf_chain[prev_index].m_map : rx_buf->m_map;
6687     rx_buf->m_map = fp->rx_mbuf_spare_map;
6688     fp->rx_mbuf_spare_map = map;
6689     bus_dmamap_sync(fp->rx_mbuf_tag, rx_buf->m_map,
6690                     BUS_DMASYNC_PREREAD);
6691     rx_buf->m = m;
6692
6693     rx_bd = &fp->rx_chain[index];
6694     rx_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6695     rx_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6696
6697     return (rc);
6698 }
6699
6700 static int
6701 bxe_alloc_rx_tpa_mbuf(struct bxe_fastpath *fp,
6702                       int                 queue)
6703 {
6704     struct bxe_sw_tpa_info *tpa_info = &fp->rx_tpa_info[queue];
6705     bus_dma_segment_t segs[1];
6706     bus_dmamap_t map;
6707     struct mbuf *m;
6708     int nsegs;
6709     int rc = 0;
6710
6711     /* allocate the new TPA mbuf */
6712     m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, fp->mbuf_alloc_size);
6713     if (__predict_false(m == NULL)) {
6714         fp->eth_q_stats.mbuf_rx_tpa_alloc_failed++;
6715         return (ENOBUFS);
6716     }
6717
6718     fp->eth_q_stats.mbuf_alloc_tpa++;
6719
6720     /* initialize the mbuf buffer length */
6721     m->m_pkthdr.len = m->m_len = fp->rx_buf_size;
6722
6723     /* map the mbuf into non-paged pool */
6724     rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
6725                                  fp->rx_tpa_info_mbuf_spare_map,
6726                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6727     if (__predict_false(rc != 0)) {
6728         fp->eth_q_stats.mbuf_rx_tpa_mapping_failed++;
6729         m_free(m);
6730         fp->eth_q_stats.mbuf_alloc_tpa--;
6731         return (rc);
6732     }
6733
6734     /* all mbufs must map to a single segment */
6735     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6736
6737     /* release any existing TPA mbuf mapping */
6738     if (tpa_info->bd.m_map != NULL) {
6739         bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6740                         BUS_DMASYNC_POSTREAD);
6741         bus_dmamap_unload(fp->rx_mbuf_tag, tpa_info->bd.m_map);
6742     }
6743
6744     /* save the mbuf and mapping info for the TPA mbuf */
6745     map = tpa_info->bd.m_map;
6746     tpa_info->bd.m_map = fp->rx_tpa_info_mbuf_spare_map;
6747     fp->rx_tpa_info_mbuf_spare_map = map;
6748     bus_dmamap_sync(fp->rx_mbuf_tag, tpa_info->bd.m_map,
6749                     BUS_DMASYNC_PREREAD);
6750     tpa_info->bd.m = m;
6751     tpa_info->seg = segs[0];
6752
6753     return (rc);
6754 }
6755
6756 /*
6757  * Allocate an mbuf and assign it to the receive scatter gather chain. The
6758  * caller must take care to save a copy of the existing mbuf in the SG mbuf
6759  * chain.
6760  */
6761 static int
6762 bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp,
6763                       uint16_t            index)
6764 {
6765     struct bxe_sw_rx_bd *sge_buf;
6766     struct eth_rx_sge *sge;
6767     bus_dma_segment_t segs[1];
6768     bus_dmamap_t map;
6769     struct mbuf *m;
6770     int nsegs;
6771     int rc = 0;
6772
6773     /* allocate a new SGE mbuf */
6774     m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
6775     if (__predict_false(m == NULL)) {
6776         fp->eth_q_stats.mbuf_rx_sge_alloc_failed++;
6777         return (ENOMEM);
6778     }
6779
6780     fp->eth_q_stats.mbuf_alloc_sge++;
6781
6782     /* initialize the mbuf buffer length */
6783     m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
6784
6785     /* map the SGE mbuf into non-paged pool */
6786     rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_mbuf_tag,
6787                                  fp->rx_sge_mbuf_spare_map,
6788                                  m, segs, &nsegs, BUS_DMA_NOWAIT);
6789     if (__predict_false(rc != 0)) {
6790         fp->eth_q_stats.mbuf_rx_sge_mapping_failed++;
6791         m_freem(m);
6792         fp->eth_q_stats.mbuf_alloc_sge--;
6793         return (rc);
6794     }
6795
6796     /* all mbufs must map to a single segment */
6797     KASSERT((nsegs == 1), ("Too many segments, %d returned!", nsegs));
6798
6799     sge_buf = &fp->rx_sge_mbuf_chain[index];
6800
6801     /* release any existing SGE mbuf mapping */
6802     if (sge_buf->m_map != NULL) {
6803         bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6804                         BUS_DMASYNC_POSTREAD);
6805         bus_dmamap_unload(fp->rx_sge_mbuf_tag, sge_buf->m_map);
6806     }
6807
6808     /* save the mbuf and mapping info for a future packet */
6809     map = sge_buf->m_map;
6810     sge_buf->m_map = fp->rx_sge_mbuf_spare_map;
6811     fp->rx_sge_mbuf_spare_map = map;
6812     bus_dmamap_sync(fp->rx_sge_mbuf_tag, sge_buf->m_map,
6813                     BUS_DMASYNC_PREREAD);
6814     sge_buf->m = m;
6815
6816     sge = &fp->rx_sge_chain[index];
6817     sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
6818     sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
6819
6820     return (rc);
6821 }
6822
6823 static __noinline int
6824 bxe_alloc_fp_buffers(struct bxe_softc *sc)
6825 {
6826     struct bxe_fastpath *fp;
6827     int i, j, rc = 0;
6828     int ring_prod, cqe_ring_prod;
6829     int max_agg_queues;
6830
6831     for (i = 0; i < sc->num_queues; i++) {
6832         fp = &sc->fp[i];
6833
6834 #if __FreeBSD_version >= 800000
6835         fp->tx_br = buf_ring_alloc(BXE_BR_SIZE, M_DEVBUF,
6836                                    M_DONTWAIT, &fp->tx_mtx);
6837         if (fp->tx_br == NULL) {
6838             BLOGE(sc, "buf_ring alloc fail for fp[%02d]\n", i);
6839             goto bxe_alloc_fp_buffers_error;
6840         }
6841 #endif
6842
6843         ring_prod = cqe_ring_prod = 0;
6844         fp->rx_bd_cons = 0;
6845         fp->rx_cq_cons = 0;
6846
6847         /* allocate buffers for the RX BDs in RX BD chain */
6848         for (j = 0; j < sc->max_rx_bufs; j++) {
6849             rc = bxe_alloc_rx_bd_mbuf(fp, ring_prod, ring_prod);
6850             if (rc != 0) {
6851                 BLOGE(sc, "mbuf alloc fail for fp[%02d] rx chain (%d)\n",
6852                       i, rc);
6853                 goto bxe_alloc_fp_buffers_error;
6854             }
6855
6856             ring_prod     = RX_BD_NEXT(ring_prod);
6857             cqe_ring_prod = RCQ_NEXT(cqe_ring_prod);
6858         }
6859
6860         fp->rx_bd_prod = ring_prod;
6861         fp->rx_cq_prod = cqe_ring_prod;
6862         fp->eth_q_stats.rx_calls = fp->eth_q_stats.rx_pkts = 0;
6863
6864         max_agg_queues = MAX_AGG_QS(sc);
6865
6866         fp->tpa_enable = TRUE;
6867
6868         /* fill the TPA pool */
6869         for (j = 0; j < max_agg_queues; j++) {
6870             rc = bxe_alloc_rx_tpa_mbuf(fp, j);
6871             if (rc != 0) {
6872                 BLOGE(sc, "mbuf alloc fail for fp[%02d] TPA queue %d\n",
6873                           i, j);
6874                 fp->tpa_enable = FALSE;
6875                 goto bxe_alloc_fp_buffers_error;
6876             }
6877
6878             fp->rx_tpa_info[j].state = BXE_TPA_STATE_STOP;
6879         }
6880
6881         if (fp->tpa_enable) {
6882             /* fill the RX SGE chain */
6883             ring_prod = 0;
6884             for (j = 0; j < RX_SGE_USABLE; j++) {
6885                 rc = bxe_alloc_rx_sge_mbuf(fp, ring_prod);
6886                 if (rc != 0) {
6887                     BLOGE(sc, "mbuf alloc fail for fp[%02d] SGE %d\n",
6888                               i, ring_prod);
6889                     fp->tpa_enable = FALSE;
6890                     ring_prod = 0;
6891                     goto bxe_alloc_fp_buffers_error;
6892                 }
6893
6894                 ring_prod = RX_SGE_NEXT(ring_prod);
6895             }
6896
6897             fp->rx_sge_prod = ring_prod;
6898         }
6899     }
6900
6901     return (0);
6902
6903 bxe_alloc_fp_buffers_error:
6904
6905     /* unwind what was already allocated */
6906     bxe_free_rx_bd_chain(fp);
6907     bxe_free_tpa_pool(fp);
6908     bxe_free_sge_chain(fp);
6909
6910     return (ENOBUFS);
6911 }
6912
6913 static void
6914 bxe_free_fw_stats_mem(struct bxe_softc *sc)
6915 {
6916     bxe_dma_free(sc, &sc->fw_stats_dma);
6917
6918     sc->fw_stats_num = 0;
6919
6920     sc->fw_stats_req_size = 0;
6921     sc->fw_stats_req = NULL;
6922     sc->fw_stats_req_mapping = 0;
6923
6924     sc->fw_stats_data_size = 0;
6925     sc->fw_stats_data = NULL;
6926     sc->fw_stats_data_mapping = 0;
6927 }
6928
6929 static int
6930 bxe_alloc_fw_stats_mem(struct bxe_softc *sc)
6931 {
6932     uint8_t num_queue_stats;
6933     int num_groups;
6934
6935     /* number of queues for statistics is number of eth queues */
6936     num_queue_stats = BXE_NUM_ETH_QUEUES(sc);
6937
6938     /*
6939      * Total number of FW statistics requests =
6940      *   1 for port stats + 1 for PF stats + num of queues
6941      */
6942     sc->fw_stats_num = (2 + num_queue_stats);
6943
6944     /*
6945      * Request is built from stats_query_header and an array of
6946      * stats_query_cmd_group each of which contains STATS_QUERY_CMD_COUNT
6947      * rules. The real number or requests is configured in the
6948      * stats_query_header.
6949      */
6950     num_groups =
6951         ((sc->fw_stats_num / STATS_QUERY_CMD_COUNT) +
6952          ((sc->fw_stats_num % STATS_QUERY_CMD_COUNT) ? 1 : 0));
6953
6954     BLOGD(sc, DBG_LOAD, "stats fw_stats_num %d num_groups %d\n",
6955           sc->fw_stats_num, num_groups);
6956
6957     sc->fw_stats_req_size =
6958         (sizeof(struct stats_query_header) +
6959          (num_groups * sizeof(struct stats_query_cmd_group)));
6960
6961     /*
6962      * Data for statistics requests + stats_counter.
6963      * stats_counter holds per-STORM counters that are incremented when
6964      * STORM has finished with the current request. Memory for FCoE
6965      * offloaded statistics are counted anyway, even if they will not be sent.
6966      * VF stats are not accounted for here as the data of VF stats is stored
6967      * in memory allocated by the VF, not here.
6968      */
6969     sc->fw_stats_data_size =
6970         (sizeof(struct stats_counter) +
6971          sizeof(struct per_port_stats) +
6972          sizeof(struct per_pf_stats) +
6973          /* sizeof(struct fcoe_statistics_params) + */
6974          (sizeof(struct per_queue_stats) * num_queue_stats));
6975
6976     if (bxe_dma_alloc(sc, (sc->fw_stats_req_size + sc->fw_stats_data_size),
6977                       &sc->fw_stats_dma, "fw stats") != 0) {
6978         bxe_free_fw_stats_mem(sc);
6979         return (-1);
6980     }
6981
6982     /* set up the shortcuts */
6983
6984     sc->fw_stats_req =
6985         (struct bxe_fw_stats_req *)sc->fw_stats_dma.vaddr;
6986     sc->fw_stats_req_mapping = sc->fw_stats_dma.paddr;
6987
6988     sc->fw_stats_data =
6989         (struct bxe_fw_stats_data *)((uint8_t *)sc->fw_stats_dma.vaddr +
6990                                      sc->fw_stats_req_size);
6991     sc->fw_stats_data_mapping = (sc->fw_stats_dma.paddr +
6992                                  sc->fw_stats_req_size);
6993
6994     BLOGD(sc, DBG_LOAD, "statistics request base address set to %#jx\n",
6995           (uintmax_t)sc->fw_stats_req_mapping);
6996
6997     BLOGD(sc, DBG_LOAD, "statistics data base address set to %#jx\n",
6998           (uintmax_t)sc->fw_stats_data_mapping);
6999
7000     return (0);
7001 }
7002
7003 /*
7004  * Bits map:
7005  * 0-7  - Engine0 load counter.
7006  * 8-15 - Engine1 load counter.
7007  * 16   - Engine0 RESET_IN_PROGRESS bit.
7008  * 17   - Engine1 RESET_IN_PROGRESS bit.
7009  * 18   - Engine0 ONE_IS_LOADED. Set when there is at least one active
7010  *        function on the engine
7011  * 19   - Engine1 ONE_IS_LOADED.
7012  * 20   - Chip reset flow bit. When set none-leader must wait for both engines
7013  *        leader to complete (check for both RESET_IN_PROGRESS bits and not
7014  *        for just the one belonging to its engine).
7015  */
7016 #define BXE_RECOVERY_GLOB_REG     MISC_REG_GENERIC_POR_1
7017 #define BXE_PATH0_LOAD_CNT_MASK   0x000000ff
7018 #define BXE_PATH0_LOAD_CNT_SHIFT  0
7019 #define BXE_PATH1_LOAD_CNT_MASK   0x0000ff00
7020 #define BXE_PATH1_LOAD_CNT_SHIFT  8
7021 #define BXE_PATH0_RST_IN_PROG_BIT 0x00010000
7022 #define BXE_PATH1_RST_IN_PROG_BIT 0x00020000
7023 #define BXE_GLOBAL_RESET_BIT      0x00040000
7024
7025 /* set the GLOBAL_RESET bit, should be run under rtnl lock */
7026 static void
7027 bxe_set_reset_global(struct bxe_softc *sc)
7028 {
7029     uint32_t val;
7030     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7031     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7032     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val | BXE_GLOBAL_RESET_BIT);
7033     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7034 }
7035
7036 /* clear the GLOBAL_RESET bit, should be run under rtnl lock */
7037 static void
7038 bxe_clear_reset_global(struct bxe_softc *sc)
7039 {
7040     uint32_t val;
7041     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7042     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7043     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val & (~BXE_GLOBAL_RESET_BIT));
7044     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7045 }
7046
7047 /* checks the GLOBAL_RESET bit, should be run under rtnl lock */
7048 static uint8_t
7049 bxe_reset_is_global(struct bxe_softc *sc)
7050 {
7051     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7052     BLOGD(sc, DBG_LOAD, "GLOB_REG=0x%08x\n", val);
7053     return (val & BXE_GLOBAL_RESET_BIT) ? TRUE : FALSE;
7054 }
7055
7056 /* clear RESET_IN_PROGRESS bit for the engine, should be run under rtnl lock */
7057 static void
7058 bxe_set_reset_done(struct bxe_softc *sc)
7059 {
7060     uint32_t val;
7061     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
7062                                  BXE_PATH0_RST_IN_PROG_BIT;
7063
7064     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7065
7066     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7067     /* Clear the bit */
7068     val &= ~bit;
7069     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7070
7071     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7072 }
7073
7074 /* set RESET_IN_PROGRESS for the engine, should be run under rtnl lock */
7075 static void
7076 bxe_set_reset_in_progress(struct bxe_softc *sc)
7077 {
7078     uint32_t val;
7079     uint32_t bit = SC_PATH(sc) ? BXE_PATH1_RST_IN_PROG_BIT :
7080                                  BXE_PATH0_RST_IN_PROG_BIT;
7081
7082     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7083
7084     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7085     /* Set the bit */
7086     val |= bit;
7087     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7088
7089     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7090 }
7091
7092 /* check RESET_IN_PROGRESS bit for an engine, should be run under rtnl lock */
7093 static uint8_t
7094 bxe_reset_is_done(struct bxe_softc *sc,
7095                   int              engine)
7096 {
7097     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7098     uint32_t bit = engine ? BXE_PATH1_RST_IN_PROG_BIT :
7099                             BXE_PATH0_RST_IN_PROG_BIT;
7100
7101     /* return false if bit is set */
7102     return (val & bit) ? FALSE : TRUE;
7103 }
7104
7105 /* get the load status for an engine, should be run under rtnl lock */
7106 static uint8_t
7107 bxe_get_load_status(struct bxe_softc *sc,
7108                     int              engine)
7109 {
7110     uint32_t mask = engine ? BXE_PATH1_LOAD_CNT_MASK :
7111                              BXE_PATH0_LOAD_CNT_MASK;
7112     uint32_t shift = engine ? BXE_PATH1_LOAD_CNT_SHIFT :
7113                               BXE_PATH0_LOAD_CNT_SHIFT;
7114     uint32_t val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7115
7116     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
7117
7118     val = ((val & mask) >> shift);
7119
7120     BLOGD(sc, DBG_LOAD, "Load mask engine %d = 0x%08x\n", engine, val);
7121
7122     return (val != 0);
7123 }
7124
7125 /* set pf load mark */
7126 /* XXX needs to be under rtnl lock */
7127 static void
7128 bxe_set_pf_load(struct bxe_softc *sc)
7129 {
7130     uint32_t val;
7131     uint32_t val1;
7132     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
7133                                   BXE_PATH0_LOAD_CNT_MASK;
7134     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
7135                                    BXE_PATH0_LOAD_CNT_SHIFT;
7136
7137     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7138
7139     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7140     BLOGD(sc, DBG_LOAD, "Old value for GLOB_REG=0x%08x\n", val);
7141
7142     /* get the current counter value */
7143     val1 = ((val & mask) >> shift);
7144
7145     /* set bit of this PF */
7146     val1 |= (1 << SC_ABS_FUNC(sc));
7147
7148     /* clear the old value */
7149     val &= ~mask;
7150
7151     /* set the new one */
7152     val |= ((val1 << shift) & mask);
7153
7154     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7155
7156     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7157 }
7158
7159 /* clear pf load mark */
7160 /* XXX needs to be under rtnl lock */
7161 static uint8_t
7162 bxe_clear_pf_load(struct bxe_softc *sc)
7163 {
7164     uint32_t val1, val;
7165     uint32_t mask = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_MASK :
7166                                   BXE_PATH0_LOAD_CNT_MASK;
7167     uint32_t shift = SC_PATH(sc) ? BXE_PATH1_LOAD_CNT_SHIFT :
7168                                    BXE_PATH0_LOAD_CNT_SHIFT;
7169
7170     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7171     val = REG_RD(sc, BXE_RECOVERY_GLOB_REG);
7172     BLOGD(sc, DBG_LOAD, "Old GEN_REG_VAL=0x%08x\n", val);
7173
7174     /* get the current counter value */
7175     val1 = (val & mask) >> shift;
7176
7177     /* clear bit of that PF */
7178     val1 &= ~(1 << SC_ABS_FUNC(sc));
7179
7180     /* clear the old value */
7181     val &= ~mask;
7182
7183     /* set the new one */
7184     val |= ((val1 << shift) & mask);
7185
7186     REG_WR(sc, BXE_RECOVERY_GLOB_REG, val);
7187     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RECOVERY_REG);
7188     return (val1 != 0);
7189 }
7190
7191 /* send load requrest to mcp and analyze response */
7192 static int
7193 bxe_nic_load_request(struct bxe_softc *sc,
7194                      uint32_t         *load_code)
7195 {
7196     /* init fw_seq */
7197     sc->fw_seq =
7198         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
7199          DRV_MSG_SEQ_NUMBER_MASK);
7200
7201     BLOGD(sc, DBG_LOAD, "initial fw_seq 0x%04x\n", sc->fw_seq);
7202
7203     /* get the current FW pulse sequence */
7204     sc->fw_drv_pulse_wr_seq =
7205         (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_pulse_mb) &
7206          DRV_PULSE_SEQ_MASK);
7207
7208     BLOGD(sc, DBG_LOAD, "initial drv_pulse 0x%04x\n",
7209           sc->fw_drv_pulse_wr_seq);
7210
7211     /* load request */
7212     (*load_code) = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
7213                                   DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
7214
7215     /* if the MCP fails to respond we must abort */
7216     if (!(*load_code)) {
7217         BLOGE(sc, "MCP response failure!\n");
7218         return (-1);
7219     }
7220
7221     /* if MCP refused then must abort */
7222     if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
7223         BLOGE(sc, "MCP refused load request\n");
7224         return (-1);
7225     }
7226
7227     return (0);
7228 }
7229
7230 /*
7231  * Check whether another PF has already loaded FW to chip. In virtualized
7232  * environments a pf from anoth VM may have already initialized the device
7233  * including loading FW.
7234  */
7235 static int
7236 bxe_nic_load_analyze_req(struct bxe_softc *sc,
7237                          uint32_t         load_code)
7238 {
7239     uint32_t my_fw, loaded_fw;
7240
7241     /* is another pf loaded on this engine? */
7242     if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
7243         (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
7244         /* build my FW version dword */
7245         my_fw = (BCM_5710_FW_MAJOR_VERSION +
7246                  (BCM_5710_FW_MINOR_VERSION << 8 ) +
7247                  (BCM_5710_FW_REVISION_VERSION << 16) +
7248                  (BCM_5710_FW_ENGINEERING_VERSION << 24));
7249
7250         /* read loaded FW from chip */
7251         loaded_fw = REG_RD(sc, XSEM_REG_PRAM);
7252         BLOGD(sc, DBG_LOAD, "loaded FW 0x%08x / my FW 0x%08x\n",
7253               loaded_fw, my_fw);
7254
7255         /* abort nic load if version mismatch */
7256         if (my_fw != loaded_fw) {
7257             BLOGE(sc, "FW 0x%08x already loaded (mine is 0x%08x)",
7258                   loaded_fw, my_fw);
7259             return (-1);
7260         }
7261     }
7262
7263     return (0);
7264 }
7265
7266 /* mark PMF if applicable */
7267 static void
7268 bxe_nic_load_pmf(struct bxe_softc *sc,
7269                  uint32_t         load_code)
7270 {
7271     uint32_t ncsi_oem_data_addr;
7272
7273     if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
7274         (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
7275         (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
7276         /*
7277          * Barrier here for ordering between the writing to sc->port.pmf here
7278          * and reading it from the periodic task.
7279          */
7280         sc->port.pmf = 1;
7281         mb();
7282     } else {
7283         sc->port.pmf = 0;
7284     }
7285
7286     BLOGD(sc, DBG_LOAD, "pmf %d\n", sc->port.pmf);
7287
7288     /* XXX needed? */
7289     if (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) {
7290         if (SHMEM2_HAS(sc, ncsi_oem_data_addr)) {
7291             ncsi_oem_data_addr = SHMEM2_RD(sc, ncsi_oem_data_addr);
7292             if (ncsi_oem_data_addr) {
7293                 REG_WR(sc,
7294                        (ncsi_oem_data_addr +
7295                         offsetof(struct glob_ncsi_oem_data, driver_version)),
7296                        0);
7297             }
7298         }
7299     }
7300 }
7301
7302 static void
7303 bxe_read_mf_cfg(struct bxe_softc *sc)
7304 {
7305     int n = (CHIP_IS_MODE_4_PORT(sc) ? 2 : 1);
7306     int abs_func;
7307     int vn;
7308
7309     if (BXE_NOMCP(sc)) {
7310         return; /* what should be the default bvalue in this case */
7311     }
7312
7313     /*
7314      * The formula for computing the absolute function number is...
7315      * For 2 port configuration (4 functions per port):
7316      *   abs_func = 2 * vn + SC_PORT + SC_PATH
7317      * For 4 port configuration (2 functions per port):
7318      *   abs_func = 4 * vn + 2 * SC_PORT + SC_PATH
7319      */
7320     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
7321         abs_func = (n * (2 * vn + SC_PORT(sc)) + SC_PATH(sc));
7322         if (abs_func >= E1H_FUNC_MAX) {
7323             break;
7324         }
7325         sc->devinfo.mf_info.mf_config[vn] =
7326             MFCFG_RD(sc, func_mf_config[abs_func].config);
7327     }
7328
7329     if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] &
7330         FUNC_MF_CFG_FUNC_DISABLED) {
7331         BLOGD(sc, DBG_LOAD, "mf_cfg function disabled\n");
7332         sc->flags |= BXE_MF_FUNC_DIS;
7333     } else {
7334         BLOGD(sc, DBG_LOAD, "mf_cfg function enabled\n");
7335         sc->flags &= ~BXE_MF_FUNC_DIS;
7336     }
7337 }
7338
7339 /* acquire split MCP access lock register */
7340 static int bxe_acquire_alr(struct bxe_softc *sc)
7341 {
7342     uint32_t j, val;
7343
7344     for (j = 0; j < 1000; j++) {
7345         val = (1UL << 31);
7346         REG_WR(sc, GRCBASE_MCP + 0x9c, val);
7347         val = REG_RD(sc, GRCBASE_MCP + 0x9c);
7348         if (val & (1L << 31))
7349             break;
7350
7351         DELAY(5000);
7352     }
7353
7354     if (!(val & (1L << 31))) {
7355         BLOGE(sc, "Cannot acquire MCP access lock register\n");
7356         return (-1);
7357     }
7358
7359     return (0);
7360 }
7361
7362 /* release split MCP access lock register */
7363 static void bxe_release_alr(struct bxe_softc *sc)
7364 {
7365     REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
7366 }
7367
7368 static void
7369 bxe_fan_failure(struct bxe_softc *sc)
7370 {
7371     int port = SC_PORT(sc);
7372     uint32_t ext_phy_config;
7373
7374     /* mark the failure */
7375     ext_phy_config =
7376         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
7377
7378     ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
7379     ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
7380     SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
7381              ext_phy_config);
7382
7383     /* log the failure */
7384     BLOGW(sc, "Fan Failure has caused the driver to shutdown "
7385               "the card to prevent permanent damage. "
7386               "Please contact OEM Support for assistance\n");
7387
7388     /* XXX */
7389 #if 1
7390     bxe_panic(sc, ("Schedule task to handle fan failure\n"));
7391 #else
7392     /*
7393      * Schedule device reset (unload)
7394      * This is due to some boards consuming sufficient power when driver is
7395      * up to overheat if fan fails.
7396      */
7397     bxe_set_bit(BXE_SP_RTNL_FAN_FAILURE, &sc->sp_rtnl_state);
7398     schedule_delayed_work(&sc->sp_rtnl_task, 0);
7399 #endif
7400 }
7401
7402 /* this function is called upon a link interrupt */
7403 static void
7404 bxe_link_attn(struct bxe_softc *sc)
7405 {
7406     uint32_t pause_enabled = 0;
7407     struct host_port_stats *pstats;
7408     int cmng_fns;
7409
7410     /* Make sure that we are synced with the current statistics */
7411     bxe_stats_handle(sc, STATS_EVENT_STOP);
7412
7413     elink_link_update(&sc->link_params, &sc->link_vars);
7414
7415     if (sc->link_vars.link_up) {
7416
7417         /* dropless flow control */
7418         if (!CHIP_IS_E1(sc) && sc->dropless_fc) {
7419             pause_enabled = 0;
7420
7421             if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
7422                 pause_enabled = 1;
7423             }
7424
7425             REG_WR(sc,
7426                    (BAR_USTRORM_INTMEM +
7427                     USTORM_ETH_PAUSE_ENABLED_OFFSET(SC_PORT(sc))),
7428                    pause_enabled);
7429         }
7430
7431         if (sc->link_vars.mac_type != ELINK_MAC_TYPE_EMAC) {
7432             pstats = BXE_SP(sc, port_stats);
7433             /* reset old mac stats */
7434             memset(&(pstats->mac_stx[0]), 0, sizeof(struct mac_stx));
7435         }
7436
7437         if (sc->state == BXE_STATE_OPEN) {
7438             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
7439         }
7440     }
7441
7442     if (sc->link_vars.link_up && sc->link_vars.line_speed) {
7443         cmng_fns = bxe_get_cmng_fns_mode(sc);
7444
7445         if (cmng_fns != CMNG_FNS_NONE) {
7446             bxe_cmng_fns_init(sc, FALSE, cmng_fns);
7447             storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
7448         } else {
7449             /* rate shaping and fairness are disabled */
7450             BLOGD(sc, DBG_LOAD, "single function mode without fairness\n");
7451         }
7452     }
7453
7454     bxe_link_report_locked(sc);
7455
7456     if (IS_MF(sc)) {
7457         ; // XXX bxe_link_sync_notify(sc);
7458     }
7459 }
7460
7461 static void
7462 bxe_attn_int_asserted(struct bxe_softc *sc,
7463                       uint32_t         asserted)
7464 {
7465     int port = SC_PORT(sc);
7466     uint32_t aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7467                                MISC_REG_AEU_MASK_ATTN_FUNC_0;
7468     uint32_t nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
7469                                         NIG_REG_MASK_INTERRUPT_PORT0;
7470     uint32_t aeu_mask;
7471     uint32_t nig_mask = 0;
7472     uint32_t reg_addr;
7473     uint32_t igu_acked;
7474     uint32_t cnt;
7475
7476     if (sc->attn_state & asserted) {
7477         BLOGE(sc, "IGU ERROR attn=0x%08x\n", asserted);
7478     }
7479
7480     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7481
7482     aeu_mask = REG_RD(sc, aeu_addr);
7483
7484     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly asserted 0x%08x\n",
7485           aeu_mask, asserted);
7486
7487     aeu_mask &= ~(asserted & 0x3ff);
7488
7489     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
7490
7491     REG_WR(sc, aeu_addr, aeu_mask);
7492
7493     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7494
7495     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
7496     sc->attn_state |= asserted;
7497     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
7498
7499     if (asserted & ATTN_HARD_WIRED_MASK) {
7500         if (asserted & ATTN_NIG_FOR_FUNC) {
7501
7502             bxe_acquire_phy_lock(sc);
7503             /* save nig interrupt mask */
7504             nig_mask = REG_RD(sc, nig_int_mask_addr);
7505
7506             /* If nig_mask is not set, no need to call the update function */
7507             if (nig_mask) {
7508                 REG_WR(sc, nig_int_mask_addr, 0);
7509
7510                 bxe_link_attn(sc);
7511             }
7512
7513             /* handle unicore attn? */
7514         }
7515
7516         if (asserted & ATTN_SW_TIMER_4_FUNC) {
7517             BLOGD(sc, DBG_INTR, "ATTN_SW_TIMER_4_FUNC!\n");
7518         }
7519
7520         if (asserted & GPIO_2_FUNC) {
7521             BLOGD(sc, DBG_INTR, "GPIO_2_FUNC!\n");
7522         }
7523
7524         if (asserted & GPIO_3_FUNC) {
7525             BLOGD(sc, DBG_INTR, "GPIO_3_FUNC!\n");
7526         }
7527
7528         if (asserted & GPIO_4_FUNC) {
7529             BLOGD(sc, DBG_INTR, "GPIO_4_FUNC!\n");
7530         }
7531
7532         if (port == 0) {
7533             if (asserted & ATTN_GENERAL_ATTN_1) {
7534                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_1!\n");
7535                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
7536             }
7537             if (asserted & ATTN_GENERAL_ATTN_2) {
7538                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_2!\n");
7539                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
7540             }
7541             if (asserted & ATTN_GENERAL_ATTN_3) {
7542                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_3!\n");
7543                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
7544             }
7545         } else {
7546             if (asserted & ATTN_GENERAL_ATTN_4) {
7547                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_4!\n");
7548                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
7549             }
7550             if (asserted & ATTN_GENERAL_ATTN_5) {
7551                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_5!\n");
7552                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
7553             }
7554             if (asserted & ATTN_GENERAL_ATTN_6) {
7555                 BLOGD(sc, DBG_INTR, "ATTN_GENERAL_ATTN_6!\n");
7556                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
7557             }
7558         }
7559     } /* hardwired */
7560
7561     if (sc->devinfo.int_block == INT_BLOCK_HC) {
7562         reg_addr = (HC_REG_COMMAND_REG + port*32 + COMMAND_REG_ATTN_BITS_SET);
7563     } else {
7564         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
7565     }
7566
7567     BLOGD(sc, DBG_INTR, "about to mask 0x%08x at %s addr 0x%08x\n",
7568           asserted,
7569           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
7570     REG_WR(sc, reg_addr, asserted);
7571
7572     /* now set back the mask */
7573     if (asserted & ATTN_NIG_FOR_FUNC) {
7574         /*
7575          * Verify that IGU ack through BAR was written before restoring
7576          * NIG mask. This loop should exit after 2-3 iterations max.
7577          */
7578         if (sc->devinfo.int_block != INT_BLOCK_HC) {
7579             cnt = 0;
7580
7581             do {
7582                 igu_acked = REG_RD(sc, IGU_REG_ATTENTION_ACK_BITS);
7583             } while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
7584                      (++cnt < MAX_IGU_ATTN_ACK_TO));
7585
7586             if (!igu_acked) {
7587                 BLOGE(sc, "Failed to verify IGU ack on time\n");
7588             }
7589
7590             mb();
7591         }
7592
7593         REG_WR(sc, nig_int_mask_addr, nig_mask);
7594
7595         bxe_release_phy_lock(sc);
7596     }
7597 }
7598
7599 static void
7600 bxe_print_next_block(struct bxe_softc *sc,
7601                      int              idx,
7602                      const char       *blk)
7603 {
7604     BLOGI(sc, "%s%s", idx ? ", " : "", blk);
7605 }
7606
7607 static int
7608 bxe_check_blocks_with_parity0(struct bxe_softc *sc,
7609                               uint32_t         sig,
7610                               int              par_num,
7611                               uint8_t          print)
7612 {
7613     uint32_t cur_bit = 0;
7614     int i = 0;
7615
7616     for (i = 0; sig; i++) {
7617         cur_bit = ((uint32_t)0x1 << i);
7618         if (sig & cur_bit) {
7619             switch (cur_bit) {
7620             case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
7621                 if (print)
7622                     bxe_print_next_block(sc, par_num++, "BRB");
7623                 break;
7624             case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
7625                 if (print)
7626                     bxe_print_next_block(sc, par_num++, "PARSER");
7627                 break;
7628             case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
7629                 if (print)
7630                     bxe_print_next_block(sc, par_num++, "TSDM");
7631                 break;
7632             case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
7633                 if (print)
7634                     bxe_print_next_block(sc, par_num++, "SEARCHER");
7635                 break;
7636             case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
7637                 if (print)
7638                     bxe_print_next_block(sc, par_num++, "TCM");
7639                 break;
7640             case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
7641                 if (print)
7642                     bxe_print_next_block(sc, par_num++, "TSEMI");
7643                 break;
7644             case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
7645                 if (print)
7646                     bxe_print_next_block(sc, par_num++, "XPB");
7647                 break;
7648             }
7649
7650             /* Clear the bit */
7651             sig &= ~cur_bit;
7652         }
7653     }
7654
7655     return (par_num);
7656 }
7657
7658 static int
7659 bxe_check_blocks_with_parity1(struct bxe_softc *sc,
7660                               uint32_t         sig,
7661                               int              par_num,
7662                               uint8_t          *global,
7663                               uint8_t          print)
7664 {
7665     int i = 0;
7666     uint32_t cur_bit = 0;
7667     for (i = 0; sig; i++) {
7668         cur_bit = ((uint32_t)0x1 << i);
7669         if (sig & cur_bit) {
7670             switch (cur_bit) {
7671             case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
7672                 if (print)
7673                     bxe_print_next_block(sc, par_num++, "PBF");
7674                 break;
7675             case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
7676                 if (print)
7677                     bxe_print_next_block(sc, par_num++, "QM");
7678                 break;
7679             case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
7680                 if (print)
7681                     bxe_print_next_block(sc, par_num++, "TM");
7682                 break;
7683             case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
7684                 if (print)
7685                     bxe_print_next_block(sc, par_num++, "XSDM");
7686                 break;
7687             case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
7688                 if (print)
7689                     bxe_print_next_block(sc, par_num++, "XCM");
7690                 break;
7691             case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
7692                 if (print)
7693                     bxe_print_next_block(sc, par_num++, "XSEMI");
7694                 break;
7695             case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
7696                 if (print)
7697                     bxe_print_next_block(sc, par_num++, "DOORBELLQ");
7698                 break;
7699             case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
7700                 if (print)
7701                     bxe_print_next_block(sc, par_num++, "NIG");
7702                 break;
7703             case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
7704                 if (print)
7705                     bxe_print_next_block(sc, par_num++, "VAUX PCI CORE");
7706                 *global = TRUE;
7707                 break;
7708             case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
7709                 if (print)
7710                     bxe_print_next_block(sc, par_num++, "DEBUG");
7711                 break;
7712             case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
7713                 if (print)
7714                     bxe_print_next_block(sc, par_num++, "USDM");
7715                 break;
7716             case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
7717                 if (print)
7718                     bxe_print_next_block(sc, par_num++, "UCM");
7719                 break;
7720             case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
7721                 if (print)
7722                     bxe_print_next_block(sc, par_num++, "USEMI");
7723                 break;
7724             case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
7725                 if (print)
7726                     bxe_print_next_block(sc, par_num++, "UPB");
7727                 break;
7728             case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
7729                 if (print)
7730                     bxe_print_next_block(sc, par_num++, "CSDM");
7731                 break;
7732             case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
7733                 if (print)
7734                     bxe_print_next_block(sc, par_num++, "CCM");
7735                 break;
7736             }
7737
7738             /* Clear the bit */
7739             sig &= ~cur_bit;
7740         }
7741     }
7742
7743     return (par_num);
7744 }
7745
7746 static int
7747 bxe_check_blocks_with_parity2(struct bxe_softc *sc,
7748                               uint32_t         sig,
7749                               int              par_num,
7750                               uint8_t          print)
7751 {
7752     uint32_t cur_bit = 0;
7753     int i = 0;
7754
7755     for (i = 0; sig; i++) {
7756         cur_bit = ((uint32_t)0x1 << i);
7757         if (sig & cur_bit) {
7758             switch (cur_bit) {
7759             case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
7760                 if (print)
7761                     bxe_print_next_block(sc, par_num++, "CSEMI");
7762                 break;
7763             case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
7764                 if (print)
7765                     bxe_print_next_block(sc, par_num++, "PXP");
7766                 break;
7767             case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
7768                 if (print)
7769                     bxe_print_next_block(sc, par_num++, "PXPPCICLOCKCLIENT");
7770                 break;
7771             case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
7772                 if (print)
7773                     bxe_print_next_block(sc, par_num++, "CFC");
7774                 break;
7775             case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
7776                 if (print)
7777                     bxe_print_next_block(sc, par_num++, "CDU");
7778                 break;
7779             case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
7780                 if (print)
7781                     bxe_print_next_block(sc, par_num++, "DMAE");
7782                 break;
7783             case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
7784                 if (print)
7785                     bxe_print_next_block(sc, par_num++, "IGU");
7786                 break;
7787             case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
7788                 if (print)
7789                     bxe_print_next_block(sc, par_num++, "MISC");
7790                 break;
7791             }
7792
7793             /* Clear the bit */
7794             sig &= ~cur_bit;
7795         }
7796     }
7797
7798     return (par_num);
7799 }
7800
7801 static int
7802 bxe_check_blocks_with_parity3(struct bxe_softc *sc,
7803                               uint32_t         sig,
7804                               int              par_num,
7805                               uint8_t          *global,
7806                               uint8_t          print)
7807 {
7808     uint32_t cur_bit = 0;
7809     int i = 0;
7810
7811     for (i = 0; sig; i++) {
7812         cur_bit = ((uint32_t)0x1 << i);
7813         if (sig & cur_bit) {
7814             switch (cur_bit) {
7815             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
7816                 if (print)
7817                     bxe_print_next_block(sc, par_num++, "MCP ROM");
7818                 *global = TRUE;
7819                 break;
7820             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
7821                 if (print)
7822                     bxe_print_next_block(sc, par_num++,
7823                               "MCP UMP RX");
7824                 *global = TRUE;
7825                 break;
7826             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
7827                 if (print)
7828                     bxe_print_next_block(sc, par_num++,
7829                               "MCP UMP TX");
7830                 *global = TRUE;
7831                 break;
7832             case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
7833                 if (print)
7834                     bxe_print_next_block(sc, par_num++,
7835                               "MCP SCPAD");
7836                 *global = TRUE;
7837                 break;
7838             }
7839
7840             /* Clear the bit */
7841             sig &= ~cur_bit;
7842         }
7843     }
7844
7845     return (par_num);
7846 }
7847
7848 static int
7849 bxe_check_blocks_with_parity4(struct bxe_softc *sc,
7850                               uint32_t         sig,
7851                               int              par_num,
7852                               uint8_t          print)
7853 {
7854     uint32_t cur_bit = 0;
7855     int i = 0;
7856
7857     for (i = 0; sig; i++) {
7858         cur_bit = ((uint32_t)0x1 << i);
7859         if (sig & cur_bit) {
7860             switch (cur_bit) {
7861             case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
7862                 if (print)
7863                     bxe_print_next_block(sc, par_num++, "PGLUE_B");
7864                 break;
7865             case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
7866                 if (print)
7867                     bxe_print_next_block(sc, par_num++, "ATC");
7868                 break;
7869             }
7870
7871             /* Clear the bit */
7872             sig &= ~cur_bit;
7873         }
7874     }
7875
7876     return (par_num);
7877 }
7878
7879 static uint8_t
7880 bxe_parity_attn(struct bxe_softc *sc,
7881                 uint8_t          *global,
7882                 uint8_t          print,
7883                 uint32_t         *sig)
7884 {
7885     int par_num = 0;
7886
7887     if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
7888         (sig[1] & HW_PRTY_ASSERT_SET_1) ||
7889         (sig[2] & HW_PRTY_ASSERT_SET_2) ||
7890         (sig[3] & HW_PRTY_ASSERT_SET_3) ||
7891         (sig[4] & HW_PRTY_ASSERT_SET_4)) {
7892         BLOGE(sc, "Parity error: HW block parity attention:\n"
7893                   "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
7894               (uint32_t)(sig[0] & HW_PRTY_ASSERT_SET_0),
7895               (uint32_t)(sig[1] & HW_PRTY_ASSERT_SET_1),
7896               (uint32_t)(sig[2] & HW_PRTY_ASSERT_SET_2),
7897               (uint32_t)(sig[3] & HW_PRTY_ASSERT_SET_3),
7898               (uint32_t)(sig[4] & HW_PRTY_ASSERT_SET_4));
7899
7900         if (print)
7901             BLOGI(sc, "Parity errors detected in blocks: ");
7902
7903         par_num =
7904             bxe_check_blocks_with_parity0(sc, sig[0] &
7905                                           HW_PRTY_ASSERT_SET_0,
7906                                           par_num, print);
7907         par_num =
7908             bxe_check_blocks_with_parity1(sc, sig[1] &
7909                                           HW_PRTY_ASSERT_SET_1,
7910                                           par_num, global, print);
7911         par_num =
7912             bxe_check_blocks_with_parity2(sc, sig[2] &
7913                                           HW_PRTY_ASSERT_SET_2,
7914                                           par_num, print);
7915         par_num =
7916             bxe_check_blocks_with_parity3(sc, sig[3] &
7917                                           HW_PRTY_ASSERT_SET_3,
7918                                           par_num, global, print);
7919         par_num =
7920             bxe_check_blocks_with_parity4(sc, sig[4] &
7921                                           HW_PRTY_ASSERT_SET_4,
7922                                           par_num, print);
7923
7924         if (print)
7925             BLOGI(sc, "\n");
7926
7927         return (TRUE);
7928     }
7929
7930     return (FALSE);
7931 }
7932
7933 static uint8_t
7934 bxe_chk_parity_attn(struct bxe_softc *sc,
7935                     uint8_t          *global,
7936                     uint8_t          print)
7937 {
7938     struct attn_route attn = { {0} };
7939     int port = SC_PORT(sc);
7940
7941     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
7942     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
7943     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
7944     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
7945
7946     if (!CHIP_IS_E1x(sc))
7947         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
7948
7949     return (bxe_parity_attn(sc, global, print, attn.sig));
7950 }
7951
7952 static void
7953 bxe_attn_int_deasserted4(struct bxe_softc *sc,
7954                          uint32_t         attn)
7955 {
7956     uint32_t val;
7957
7958     if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
7959         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
7960         BLOGE(sc, "PGLUE hw attention 0x%08x\n", val);
7961         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
7962             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
7963         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
7964             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
7965         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
7966             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
7967         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
7968             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
7969         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
7970             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
7971         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
7972             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
7973         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
7974             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
7975         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
7976             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
7977         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
7978             BLOGE(sc, "PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
7979     }
7980
7981     if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
7982         val = REG_RD(sc, ATC_REG_ATC_INT_STS_CLR);
7983         BLOGE(sc, "ATC hw attention 0x%08x\n", val);
7984         if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
7985             BLOGE(sc, "ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
7986         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
7987             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
7988         if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
7989             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
7990         if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
7991             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
7992         if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
7993             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
7994         if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
7995             BLOGE(sc, "ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
7996     }
7997
7998     if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
7999                 AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
8000         BLOGE(sc, "FATAL parity attention set4 0x%08x\n",
8001               (uint32_t)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
8002                                  AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
8003     }
8004 }
8005
8006 static void
8007 bxe_e1h_disable(struct bxe_softc *sc)
8008 {
8009     int port = SC_PORT(sc);
8010
8011     bxe_tx_disable(sc);
8012
8013     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 0);
8014 }
8015
8016 static void
8017 bxe_e1h_enable(struct bxe_softc *sc)
8018 {
8019     int port = SC_PORT(sc);
8020
8021     REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
8022
8023     // XXX bxe_tx_enable(sc);
8024 }
8025
8026 /*
8027  * called due to MCP event (on pmf):
8028  *   reread new bandwidth configuration
8029  *   configure FW
8030  *   notify others function about the change
8031  */
8032 static void
8033 bxe_config_mf_bw(struct bxe_softc *sc)
8034 {
8035     if (sc->link_vars.link_up) {
8036         bxe_cmng_fns_init(sc, TRUE, CMNG_FNS_MINMAX);
8037         // XXX bxe_link_sync_notify(sc);
8038     }
8039
8040     storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
8041 }
8042
8043 static void
8044 bxe_set_mf_bw(struct bxe_softc *sc)
8045 {
8046     bxe_config_mf_bw(sc);
8047     bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
8048 }
8049
8050 static void
8051 bxe_handle_eee_event(struct bxe_softc *sc)
8052 {
8053     BLOGD(sc, DBG_INTR, "EEE - LLDP event\n");
8054     bxe_fw_command(sc, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
8055 }
8056
8057 #define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
8058
8059 static void
8060 bxe_drv_info_ether_stat(struct bxe_softc *sc)
8061 {
8062     struct eth_stats_info *ether_stat =
8063         &sc->sp->drv_info_to_mcp.ether_stat;
8064
8065     strlcpy(ether_stat->version, BXE_DRIVER_VERSION,
8066             ETH_STAT_INFO_VERSION_LEN);
8067
8068     /* XXX (+ MAC_PAD) taken from other driver... verify this is right */
8069     sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
8070                                           DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
8071                                           ether_stat->mac_local + MAC_PAD,
8072                                           MAC_PAD, ETH_ALEN);
8073
8074     ether_stat->mtu_size = sc->mtu;
8075
8076     ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
8077     if (sc->ifnet->if_capenable & (IFCAP_TSO4 | IFCAP_TSO6)) {
8078         ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
8079     }
8080
8081     // XXX ether_stat->feature_flags |= ???;
8082
8083     ether_stat->promiscuous_mode = 0; // (flags & PROMISC) ? 1 : 0;
8084
8085     ether_stat->txq_size = sc->tx_ring_size;
8086     ether_stat->rxq_size = sc->rx_ring_size;
8087 }
8088
8089 static void
8090 bxe_handle_drv_info_req(struct bxe_softc *sc)
8091 {
8092     enum drv_info_opcode op_code;
8093     uint32_t drv_info_ctl = SHMEM2_RD(sc, drv_info_control);
8094
8095     /* if drv_info version supported by MFW doesn't match - send NACK */
8096     if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
8097         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
8098         return;
8099     }
8100
8101     op_code = ((drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
8102                DRV_INFO_CONTROL_OP_CODE_SHIFT);
8103
8104     memset(&sc->sp->drv_info_to_mcp, 0, sizeof(union drv_info_to_mcp));
8105
8106     switch (op_code) {
8107     case ETH_STATS_OPCODE:
8108         bxe_drv_info_ether_stat(sc);
8109         break;
8110     case FCOE_STATS_OPCODE:
8111     case ISCSI_STATS_OPCODE:
8112     default:
8113         /* if op code isn't supported - send NACK */
8114         bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_NACK, 0);
8115         return;
8116     }
8117
8118     /*
8119      * If we got drv_info attn from MFW then these fields are defined in
8120      * shmem2 for sure
8121      */
8122     SHMEM2_WR(sc, drv_info_host_addr_lo,
8123               U64_LO(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
8124     SHMEM2_WR(sc, drv_info_host_addr_hi,
8125               U64_HI(BXE_SP_MAPPING(sc, drv_info_to_mcp)));
8126
8127     bxe_fw_command(sc, DRV_MSG_CODE_DRV_INFO_ACK, 0);
8128 }
8129
8130 static void
8131 bxe_dcc_event(struct bxe_softc *sc,
8132               uint32_t         dcc_event)
8133 {
8134     BLOGD(sc, DBG_INTR, "dcc_event 0x%08x\n", dcc_event);
8135
8136     if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
8137         /*
8138          * This is the only place besides the function initialization
8139          * where the sc->flags can change so it is done without any
8140          * locks
8141          */
8142         if (sc->devinfo.mf_info.mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
8143             BLOGD(sc, DBG_INTR, "mf_cfg function disabled\n");
8144             sc->flags |= BXE_MF_FUNC_DIS;
8145             bxe_e1h_disable(sc);
8146         } else {
8147             BLOGD(sc, DBG_INTR, "mf_cfg function enabled\n");
8148             sc->flags &= ~BXE_MF_FUNC_DIS;
8149             bxe_e1h_enable(sc);
8150         }
8151         dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
8152     }
8153
8154     if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
8155         bxe_config_mf_bw(sc);
8156         dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
8157     }
8158
8159     /* Report results to MCP */
8160     if (dcc_event)
8161         bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE, 0);
8162     else
8163         bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK, 0);
8164 }
8165
8166 static void
8167 bxe_pmf_update(struct bxe_softc *sc)
8168 {
8169     int port = SC_PORT(sc);
8170     uint32_t val;
8171
8172     sc->port.pmf = 1;
8173     BLOGD(sc, DBG_INTR, "pmf %d\n", sc->port.pmf);
8174
8175     /*
8176      * We need the mb() to ensure the ordering between the writing to
8177      * sc->port.pmf here and reading it from the bxe_periodic_task().
8178      */
8179     mb();
8180
8181     /* queue a periodic task */
8182     // XXX schedule task...
8183
8184     // XXX bxe_dcbx_pmf_update(sc);
8185
8186     /* enable nig attention */
8187     val = (0xff0f | (1 << (SC_VN(sc) + 4)));
8188     if (sc->devinfo.int_block == INT_BLOCK_HC) {
8189         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, val);
8190         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, val);
8191     } else if (!CHIP_IS_E1x(sc)) {
8192         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
8193         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
8194     }
8195
8196     bxe_stats_handle(sc, STATS_EVENT_PMF);
8197 }
8198
8199 static int
8200 bxe_mc_assert(struct bxe_softc *sc)
8201 {
8202     char last_idx;
8203     int i, rc = 0;
8204     uint32_t row0, row1, row2, row3;
8205
8206     /* XSTORM */
8207     last_idx = REG_RD8(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_INDEX_OFFSET);
8208     if (last_idx)
8209         BLOGE(sc, "XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8210
8211     /* print the asserts */
8212     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8213
8214         row0 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i));
8215         row1 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 4);
8216         row2 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 8);
8217         row3 = REG_RD(sc, BAR_XSTRORM_INTMEM + XSTORM_ASSERT_LIST_OFFSET(i) + 12);
8218
8219         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8220             BLOGE(sc, "XSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8221                   i, row3, row2, row1, row0);
8222             rc++;
8223         } else {
8224             break;
8225         }
8226     }
8227
8228     /* TSTORM */
8229     last_idx = REG_RD8(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_INDEX_OFFSET);
8230     if (last_idx) {
8231         BLOGE(sc, "TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8232     }
8233
8234     /* print the asserts */
8235     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8236
8237         row0 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i));
8238         row1 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 4);
8239         row2 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 8);
8240         row3 = REG_RD(sc, BAR_TSTRORM_INTMEM + TSTORM_ASSERT_LIST_OFFSET(i) + 12);
8241
8242         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8243             BLOGE(sc, "TSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8244                   i, row3, row2, row1, row0);
8245             rc++;
8246         } else {
8247             break;
8248         }
8249     }
8250
8251     /* CSTORM */
8252     last_idx = REG_RD8(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_INDEX_OFFSET);
8253     if (last_idx) {
8254         BLOGE(sc, "CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8255     }
8256
8257     /* print the asserts */
8258     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8259
8260         row0 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i));
8261         row1 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 4);
8262         row2 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 8);
8263         row3 = REG_RD(sc, BAR_CSTRORM_INTMEM + CSTORM_ASSERT_LIST_OFFSET(i) + 12);
8264
8265         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8266             BLOGE(sc, "CSTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8267                   i, row3, row2, row1, row0);
8268             rc++;
8269         } else {
8270             break;
8271         }
8272     }
8273
8274     /* USTORM */
8275     last_idx = REG_RD8(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_INDEX_OFFSET);
8276     if (last_idx) {
8277         BLOGE(sc, "USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
8278     }
8279
8280     /* print the asserts */
8281     for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
8282
8283         row0 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i));
8284         row1 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 4);
8285         row2 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 8);
8286         row3 = REG_RD(sc, BAR_USTRORM_INTMEM + USTORM_ASSERT_LIST_OFFSET(i) + 12);
8287
8288         if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
8289             BLOGE(sc, "USTORM_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
8290                   i, row3, row2, row1, row0);
8291             rc++;
8292         } else {
8293             break;
8294         }
8295     }
8296
8297     return (rc);
8298 }
8299
8300 static void
8301 bxe_attn_int_deasserted3(struct bxe_softc *sc,
8302                          uint32_t         attn)
8303 {
8304     int func = SC_FUNC(sc);
8305     uint32_t val;
8306
8307     if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
8308
8309         if (attn & BXE_PMF_LINK_ASSERT(sc)) {
8310
8311             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
8312             bxe_read_mf_cfg(sc);
8313             sc->devinfo.mf_info.mf_config[SC_VN(sc)] =
8314                 MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
8315             val = SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_status);
8316
8317             if (val & DRV_STATUS_DCC_EVENT_MASK)
8318                 bxe_dcc_event(sc, (val & DRV_STATUS_DCC_EVENT_MASK));
8319
8320             if (val & DRV_STATUS_SET_MF_BW)
8321                 bxe_set_mf_bw(sc);
8322
8323             if (val & DRV_STATUS_DRV_INFO_REQ)
8324                 bxe_handle_drv_info_req(sc);
8325
8326 #if 0
8327             if (val & DRV_STATUS_VF_DISABLED)
8328                 bxe_vf_handle_flr_event(sc);
8329 #endif
8330
8331             if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
8332                 bxe_pmf_update(sc);
8333
8334 #if 0
8335             if (sc->port.pmf &&
8336                 (val & DRV_STATUS_DCBX_NEGOTIATION_RESULTS) &&
8337                 (sc->dcbx_enabled > 0))
8338                 /* start dcbx state machine */
8339                 bxe_dcbx_set_params(sc, BXE_DCBX_STATE_NEG_RECEIVED);
8340 #endif
8341
8342 #if 0
8343             if (val & DRV_STATUS_AFEX_EVENT_MASK)
8344                 bxe_handle_afex_cmd(sc, val & DRV_STATUS_AFEX_EVENT_MASK);
8345 #endif
8346
8347             if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
8348                 bxe_handle_eee_event(sc);
8349
8350             if (sc->link_vars.periodic_flags &
8351                 ELINK_PERIODIC_FLAGS_LINK_EVENT) {
8352                 /* sync with link */
8353                 bxe_acquire_phy_lock(sc);
8354                 sc->link_vars.periodic_flags &=
8355                     ~ELINK_PERIODIC_FLAGS_LINK_EVENT;
8356                 bxe_release_phy_lock(sc);
8357                 if (IS_MF(sc))
8358                     ; // XXX bxe_link_sync_notify(sc);
8359                 bxe_link_report(sc);
8360             }
8361
8362             /*
8363              * Always call it here: bxe_link_report() will
8364              * prevent the link indication duplication.
8365              */
8366             bxe_link_status_update(sc);
8367
8368         } else if (attn & BXE_MC_ASSERT_BITS) {
8369
8370             BLOGE(sc, "MC assert!\n");
8371             bxe_mc_assert(sc);
8372             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
8373             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
8374             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
8375             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
8376             bxe_panic(sc, ("MC assert!\n"));
8377
8378         } else if (attn & BXE_MCP_ASSERT) {
8379
8380             BLOGE(sc, "MCP assert!\n");
8381             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
8382             // XXX bxe_fw_dump(sc);
8383
8384         } else {
8385             BLOGE(sc, "Unknown HW assert! (attn 0x%08x)\n", attn);
8386         }
8387     }
8388
8389     if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
8390         BLOGE(sc, "LATCHED attention 0x%08x (masked)\n", attn);
8391         if (attn & BXE_GRC_TIMEOUT) {
8392             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_TIMEOUT_ATTN);
8393             BLOGE(sc, "GRC time-out 0x%08x\n", val);
8394         }
8395         if (attn & BXE_GRC_RSV) {
8396             val = CHIP_IS_E1(sc) ? 0 : REG_RD(sc, MISC_REG_GRC_RSV_ATTN);
8397             BLOGE(sc, "GRC reserved 0x%08x\n", val);
8398         }
8399         REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
8400     }
8401 }
8402
8403 static void
8404 bxe_attn_int_deasserted2(struct bxe_softc *sc,
8405                          uint32_t         attn)
8406 {
8407     int port = SC_PORT(sc);
8408     int reg_offset;
8409     uint32_t val0, mask0, val1, mask1;
8410     uint32_t val;
8411
8412     if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
8413         val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
8414         BLOGE(sc, "CFC hw attention 0x%08x\n", val);
8415         /* CFC error attention */
8416         if (val & 0x2) {
8417             BLOGE(sc, "FATAL error from CFC\n");
8418         }
8419     }
8420
8421     if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
8422         val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
8423         BLOGE(sc, "PXP hw attention-0 0x%08x\n", val);
8424         /* RQ_USDMDP_FIFO_OVERFLOW */
8425         if (val & 0x18000) {
8426             BLOGE(sc, "FATAL error from PXP\n");
8427         }
8428
8429         if (!CHIP_IS_E1x(sc)) {
8430             val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_1);
8431             BLOGE(sc, "PXP hw attention-1 0x%08x\n", val);
8432         }
8433     }
8434
8435 #define PXP2_EOP_ERROR_BIT  PXP2_PXP2_INT_STS_CLR_0_REG_WR_PGLUE_EOP_ERROR
8436 #define AEU_PXP2_HW_INT_BIT AEU_INPUTS_ATTN_BITS_PXPPCICLOCKCLIENT_HW_INTERRUPT
8437
8438     if (attn & AEU_PXP2_HW_INT_BIT) {
8439         /*  CQ47854 workaround do not panic on
8440          *  PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8441          */
8442         if (!CHIP_IS_E1x(sc)) {
8443             mask0 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_0);
8444             val1 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_1);
8445             mask1 = REG_RD(sc, PXP2_REG_PXP2_INT_MASK_1);
8446             val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_0);
8447             /*
8448              * If the olny PXP2_EOP_ERROR_BIT is set in
8449              * STS0 and STS1 - clear it
8450              *
8451              * probably we lose additional attentions between
8452              * STS0 and STS_CLR0, in this case user will not
8453              * be notified about them
8454              */
8455             if (val0 & mask0 & PXP2_EOP_ERROR_BIT &&
8456                 !(val1 & mask1))
8457                 val0 = REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
8458
8459             /* print the register, since no one can restore it */
8460             BLOGE(sc, "PXP2_REG_PXP2_INT_STS_CLR_0 0x%08x\n", val0);
8461
8462             /*
8463              * if PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR
8464              * then notify
8465              */
8466             if (val0 & PXP2_EOP_ERROR_BIT) {
8467                 BLOGE(sc, "PXP2_WR_PGLUE_EOP_ERROR\n");
8468
8469                 /*
8470                  * if only PXP2_PXP2_INT_STS_0_REG_WR_PGLUE_EOP_ERROR is
8471                  * set then clear attention from PXP2 block without panic
8472                  */
8473                 if (((val0 & mask0) == PXP2_EOP_ERROR_BIT) &&
8474                     ((val1 & mask1) == 0))
8475                     attn &= ~AEU_PXP2_HW_INT_BIT;
8476             }
8477         }
8478     }
8479
8480     if (attn & HW_INTERRUT_ASSERT_SET_2) {
8481         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
8482                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
8483
8484         val = REG_RD(sc, reg_offset);
8485         val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
8486         REG_WR(sc, reg_offset, val);
8487
8488         BLOGE(sc, "FATAL HW block attention set2 0x%x\n",
8489               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_2));
8490         bxe_panic(sc, ("HW block attention set2\n"));
8491     }
8492 }
8493
8494 static void
8495 bxe_attn_int_deasserted1(struct bxe_softc *sc,
8496                          uint32_t         attn)
8497 {
8498     int port = SC_PORT(sc);
8499     int reg_offset;
8500     uint32_t val;
8501
8502     if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
8503         val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
8504         BLOGE(sc, "DB hw attention 0x%08x\n", val);
8505         /* DORQ discard attention */
8506         if (val & 0x2) {
8507             BLOGE(sc, "FATAL error from DORQ\n");
8508         }
8509     }
8510
8511     if (attn & HW_INTERRUT_ASSERT_SET_1) {
8512         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
8513                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
8514
8515         val = REG_RD(sc, reg_offset);
8516         val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
8517         REG_WR(sc, reg_offset, val);
8518
8519         BLOGE(sc, "FATAL HW block attention set1 0x%08x\n",
8520               (uint32_t)(attn & HW_INTERRUT_ASSERT_SET_1));
8521         bxe_panic(sc, ("HW block attention set1\n"));
8522     }
8523 }
8524
8525 static void
8526 bxe_attn_int_deasserted0(struct bxe_softc *sc,
8527                          uint32_t         attn)
8528 {
8529     int port = SC_PORT(sc);
8530     int reg_offset;
8531     uint32_t val;
8532
8533     reg_offset = (port) ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
8534                           MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
8535
8536     if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
8537         val = REG_RD(sc, reg_offset);
8538         val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
8539         REG_WR(sc, reg_offset, val);
8540
8541         BLOGW(sc, "SPIO5 hw attention\n");
8542
8543         /* Fan failure attention */
8544         elink_hw_reset_phy(&sc->link_params);
8545         bxe_fan_failure(sc);
8546     }
8547
8548     if ((attn & sc->link_vars.aeu_int_mask) && sc->port.pmf) {
8549         bxe_acquire_phy_lock(sc);
8550         elink_handle_module_detect_int(&sc->link_params);
8551         bxe_release_phy_lock(sc);
8552     }
8553
8554     if (attn & HW_INTERRUT_ASSERT_SET_0) {
8555         val = REG_RD(sc, reg_offset);
8556         val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
8557         REG_WR(sc, reg_offset, val);
8558
8559         bxe_panic(sc, ("FATAL HW block attention set0 0x%lx\n",
8560                        (attn & HW_INTERRUT_ASSERT_SET_0)));
8561     }
8562 }
8563
8564 static void
8565 bxe_attn_int_deasserted(struct bxe_softc *sc,
8566                         uint32_t         deasserted)
8567 {
8568     struct attn_route attn;
8569     struct attn_route *group_mask;
8570     int port = SC_PORT(sc);
8571     int index;
8572     uint32_t reg_addr;
8573     uint32_t val;
8574     uint32_t aeu_mask;
8575     uint8_t global = FALSE;
8576
8577     /*
8578      * Need to take HW lock because MCP or other port might also
8579      * try to handle this event.
8580      */
8581     bxe_acquire_alr(sc);
8582
8583     if (bxe_chk_parity_attn(sc, &global, TRUE)) {
8584         /* XXX
8585          * In case of parity errors don't handle attentions so that
8586          * other function would "see" parity errors.
8587          */
8588         sc->recovery_state = BXE_RECOVERY_INIT;
8589         // XXX schedule a recovery task...
8590         /* disable HW interrupts */
8591         bxe_int_disable(sc);
8592         bxe_release_alr(sc);
8593         return;
8594     }
8595
8596     attn.sig[0] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
8597     attn.sig[1] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
8598     attn.sig[2] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
8599     attn.sig[3] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
8600     if (!CHIP_IS_E1x(sc)) {
8601         attn.sig[4] = REG_RD(sc, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
8602     } else {
8603         attn.sig[4] = 0;
8604     }
8605
8606     BLOGD(sc, DBG_INTR, "attn: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
8607           attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
8608
8609     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
8610         if (deasserted & (1 << index)) {
8611             group_mask = &sc->attn_group[index];
8612
8613             BLOGD(sc, DBG_INTR,
8614                   "group[%d]: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", index,
8615                   group_mask->sig[0], group_mask->sig[1],
8616                   group_mask->sig[2], group_mask->sig[3],
8617                   group_mask->sig[4]);
8618
8619             bxe_attn_int_deasserted4(sc, attn.sig[4] & group_mask->sig[4]);
8620             bxe_attn_int_deasserted3(sc, attn.sig[3] & group_mask->sig[3]);
8621             bxe_attn_int_deasserted1(sc, attn.sig[1] & group_mask->sig[1]);
8622             bxe_attn_int_deasserted2(sc, attn.sig[2] & group_mask->sig[2]);
8623             bxe_attn_int_deasserted0(sc, attn.sig[0] & group_mask->sig[0]);
8624         }
8625     }
8626
8627     bxe_release_alr(sc);
8628
8629     if (sc->devinfo.int_block == INT_BLOCK_HC) {
8630         reg_addr = (HC_REG_COMMAND_REG + port*32 +
8631                     COMMAND_REG_ATTN_BITS_CLR);
8632     } else {
8633         reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
8634     }
8635
8636     val = ~deasserted;
8637     BLOGD(sc, DBG_INTR,
8638           "about to mask 0x%08x at %s addr 0x%08x\n", val,
8639           (sc->devinfo.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
8640     REG_WR(sc, reg_addr, val);
8641
8642     if (~sc->attn_state & deasserted) {
8643         BLOGE(sc, "IGU error\n");
8644     }
8645
8646     reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8647                       MISC_REG_AEU_MASK_ATTN_FUNC_0;
8648
8649     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8650
8651     aeu_mask = REG_RD(sc, reg_addr);
8652
8653     BLOGD(sc, DBG_INTR, "aeu_mask 0x%08x newly deasserted 0x%08x\n",
8654           aeu_mask, deasserted);
8655     aeu_mask |= (deasserted & 0x3ff);
8656     BLOGD(sc, DBG_INTR, "new mask 0x%08x\n", aeu_mask);
8657
8658     REG_WR(sc, reg_addr, aeu_mask);
8659     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
8660
8661     BLOGD(sc, DBG_INTR, "attn_state 0x%08x\n", sc->attn_state);
8662     sc->attn_state &= ~deasserted;
8663     BLOGD(sc, DBG_INTR, "new state 0x%08x\n", sc->attn_state);
8664 }
8665
8666 static void
8667 bxe_attn_int(struct bxe_softc *sc)
8668 {
8669     /* read local copy of bits */
8670     uint32_t attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
8671     uint32_t attn_ack = le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
8672     uint32_t attn_state = sc->attn_state;
8673
8674     /* look for changed bits */
8675     uint32_t asserted   =  attn_bits & ~attn_ack & ~attn_state;
8676     uint32_t deasserted = ~attn_bits &  attn_ack &  attn_state;
8677
8678     BLOGD(sc, DBG_INTR,
8679           "attn_bits 0x%08x attn_ack 0x%08x asserted 0x%08x deasserted 0x%08x\n",
8680           attn_bits, attn_ack, asserted, deasserted);
8681
8682     if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state)) {
8683         BLOGE(sc, "BAD attention state\n");
8684     }
8685
8686     /* handle bits that were raised */
8687     if (asserted) {
8688         bxe_attn_int_asserted(sc, asserted);
8689     }
8690
8691     if (deasserted) {
8692         bxe_attn_int_deasserted(sc, deasserted);
8693     }
8694 }
8695
8696 static uint16_t
8697 bxe_update_dsb_idx(struct bxe_softc *sc)
8698 {
8699     struct host_sp_status_block *def_sb = sc->def_sb;
8700     uint16_t rc = 0;
8701
8702     mb(); /* status block is written to by the chip */
8703
8704     if (sc->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
8705         sc->def_att_idx = def_sb->atten_status_block.attn_bits_index;
8706         rc |= BXE_DEF_SB_ATT_IDX;
8707     }
8708
8709     if (sc->def_idx != def_sb->sp_sb.running_index) {
8710         sc->def_idx = def_sb->sp_sb.running_index;
8711         rc |= BXE_DEF_SB_IDX;
8712     }
8713
8714     mb();
8715
8716     return (rc);
8717 }
8718
8719 static inline struct ecore_queue_sp_obj *
8720 bxe_cid_to_q_obj(struct bxe_softc *sc,
8721                  uint32_t         cid)
8722 {
8723     BLOGD(sc, DBG_SP, "retrieving fp from cid %d\n", cid);
8724     return (&sc->sp_objs[CID_TO_FP(cid, sc)].q_obj);
8725 }
8726
8727 static void
8728 bxe_handle_mcast_eqe(struct bxe_softc *sc)
8729 {
8730     struct ecore_mcast_ramrod_params rparam;
8731     int rc;
8732
8733     memset(&rparam, 0, sizeof(rparam));
8734
8735     rparam.mcast_obj = &sc->mcast_obj;
8736
8737     BXE_MCAST_LOCK(sc);
8738
8739     /* clear pending state for the last command */
8740     sc->mcast_obj.raw.clear_pending(&sc->mcast_obj.raw);
8741
8742     /* if there are pending mcast commands - send them */
8743     if (sc->mcast_obj.check_pending(&sc->mcast_obj)) {
8744         rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_CONT);
8745         if (rc < 0) {
8746             BLOGD(sc, DBG_SP,
8747                   "ERROR: Failed to send pending mcast commands (%d)\n",
8748                   rc);
8749         }
8750     }
8751
8752     BXE_MCAST_UNLOCK(sc);
8753 }
8754
8755 static void
8756 bxe_handle_classification_eqe(struct bxe_softc      *sc,
8757                               union event_ring_elem *elem)
8758 {
8759     unsigned long ramrod_flags = 0;
8760     int rc = 0;
8761     uint32_t cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8762     struct ecore_vlan_mac_obj *vlan_mac_obj;
8763
8764     /* always push next commands out, don't wait here */
8765     bit_set(&ramrod_flags, RAMROD_CONT);
8766
8767     switch (le32toh(elem->message.data.eth_event.echo) >> BXE_SWCID_SHIFT) {
8768     case ECORE_FILTER_MAC_PENDING:
8769         BLOGD(sc, DBG_SP, "Got SETUP_MAC completions\n");
8770         vlan_mac_obj = &sc->sp_objs[cid].mac_obj;
8771         break;
8772
8773     case ECORE_FILTER_MCAST_PENDING:
8774         BLOGD(sc, DBG_SP, "Got SETUP_MCAST completions\n");
8775         /*
8776          * This is only relevant for 57710 where multicast MACs are
8777          * configured as unicast MACs using the same ramrod.
8778          */
8779         bxe_handle_mcast_eqe(sc);
8780         return;
8781
8782     default:
8783         BLOGE(sc, "Unsupported classification command: %d\n",
8784               elem->message.data.eth_event.echo);
8785         return;
8786     }
8787
8788     rc = vlan_mac_obj->complete(sc, vlan_mac_obj, elem, &ramrod_flags);
8789
8790     if (rc < 0) {
8791         BLOGE(sc, "Failed to schedule new commands (%d)\n", rc);
8792     } else if (rc > 0) {
8793         BLOGD(sc, DBG_SP, "Scheduled next pending commands...\n");
8794     }
8795 }
8796
8797 static void
8798 bxe_handle_rx_mode_eqe(struct bxe_softc      *sc,
8799                        union event_ring_elem *elem)
8800 {
8801     bxe_clear_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state);
8802
8803     /* send rx_mode command again if was requested */
8804     if (bxe_test_and_clear_bit(ECORE_FILTER_RX_MODE_SCHED,
8805                                &sc->sp_state)) {
8806         bxe_set_storm_rx_mode(sc);
8807     }
8808 #if 0
8809     else if (bxe_test_and_clear_bit(ECORE_FILTER_ISCSI_ETH_START_SCHED,
8810                                     &sc->sp_state)) {
8811         bxe_set_iscsi_eth_rx_mode(sc, TRUE);
8812     }
8813     else if (bxe_test_and_clear_bit(ECORE_FILTER_ISCSI_ETH_STOP_SCHED,
8814                                     &sc->sp_state)) {
8815         bxe_set_iscsi_eth_rx_mode(sc, FALSE);
8816     }
8817 #endif
8818 }
8819
8820 static void
8821 bxe_update_eq_prod(struct bxe_softc *sc,
8822                    uint16_t         prod)
8823 {
8824     storm_memset_eq_prod(sc, prod, SC_FUNC(sc));
8825     wmb(); /* keep prod updates ordered */
8826 }
8827
8828 static void
8829 bxe_eq_int(struct bxe_softc *sc)
8830 {
8831     uint16_t hw_cons, sw_cons, sw_prod;
8832     union event_ring_elem *elem;
8833     uint8_t echo;
8834     uint32_t cid;
8835     uint8_t opcode;
8836     int spqe_cnt = 0;
8837     struct ecore_queue_sp_obj *q_obj;
8838     struct ecore_func_sp_obj *f_obj = &sc->func_obj;
8839     struct ecore_raw_obj *rss_raw = &sc->rss_conf_obj.raw;
8840
8841     hw_cons = le16toh(*sc->eq_cons_sb);
8842
8843     /*
8844      * The hw_cons range is 1-255, 257 - the sw_cons range is 0-254, 256.
8845      * when we get to the next-page we need to adjust so the loop
8846      * condition below will be met. The next element is the size of a
8847      * regular element and hence incrementing by 1
8848      */
8849     if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE) {
8850         hw_cons++;
8851     }
8852
8853     /*
8854      * This function may never run in parallel with itself for a
8855      * specific sc and no need for a read memory barrier here.
8856      */
8857     sw_cons = sc->eq_cons;
8858     sw_prod = sc->eq_prod;
8859
8860     BLOGD(sc, DBG_SP,"EQ: hw_cons=%u sw_cons=%u eq_spq_left=0x%lx\n",
8861           hw_cons, sw_cons, atomic_load_acq_long(&sc->eq_spq_left));
8862
8863     for (;
8864          sw_cons != hw_cons;
8865          sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
8866
8867         elem = &sc->eq[EQ_DESC(sw_cons)];
8868
8869 #if 0
8870         int rc;
8871         rc = bxe_iov_eq_sp_event(sc, elem);
8872         if (!rc) {
8873             BLOGE(sc, "bxe_iov_eq_sp_event returned %d\n", rc);
8874             goto next_spqe;
8875         }
8876 #endif
8877
8878         /* elem CID originates from FW, actually LE */
8879         cid = SW_CID(elem->message.data.cfc_del_event.cid);
8880         opcode = elem->message.opcode;
8881
8882         /* handle eq element */
8883         switch (opcode) {
8884 #if 0
8885         case EVENT_RING_OPCODE_VF_PF_CHANNEL:
8886             BLOGD(sc, DBG_SP, "vf/pf channel element on eq\n");
8887             bxe_vf_mbx(sc, &elem->message.data.vf_pf_event);
8888             continue;
8889 #endif
8890
8891         case EVENT_RING_OPCODE_STAT_QUERY:
8892             BLOGD(sc, DBG_SP, "got statistics completion event %d\n",
8893                   sc->stats_comp++);
8894             /* nothing to do with stats comp */
8895             goto next_spqe;
8896
8897         case EVENT_RING_OPCODE_CFC_DEL:
8898             /* handle according to cid range */
8899             /* we may want to verify here that the sc state is HALTING */
8900             BLOGD(sc, DBG_SP, "got delete ramrod for MULTI[%d]\n", cid);
8901             q_obj = bxe_cid_to_q_obj(sc, cid);
8902             if (q_obj->complete_cmd(sc, q_obj, ECORE_Q_CMD_CFC_DEL)) {
8903                 break;
8904             }
8905             goto next_spqe;
8906
8907         case EVENT_RING_OPCODE_STOP_TRAFFIC:
8908             BLOGD(sc, DBG_SP, "got STOP TRAFFIC\n");
8909             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_STOP)) {
8910                 break;
8911             }
8912             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_PAUSED);
8913             goto next_spqe;
8914
8915         case EVENT_RING_OPCODE_START_TRAFFIC:
8916             BLOGD(sc, DBG_SP, "got START TRAFFIC\n");
8917             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_TX_START)) {
8918                 break;
8919             }
8920             // XXX bxe_dcbx_set_params(sc, BXE_DCBX_STATE_TX_RELEASED);
8921             goto next_spqe;
8922
8923         case EVENT_RING_OPCODE_FUNCTION_UPDATE:
8924             echo = elem->message.data.function_update_event.echo;
8925             if (echo == SWITCH_UPDATE) {
8926                 BLOGD(sc, DBG_SP, "got FUNC_SWITCH_UPDATE ramrod\n");
8927                 if (f_obj->complete_cmd(sc, f_obj,
8928                                         ECORE_F_CMD_SWITCH_UPDATE)) {
8929                     break;
8930                 }
8931             }
8932             else {
8933                 BLOGD(sc, DBG_SP,
8934                       "AFEX: ramrod completed FUNCTION_UPDATE\n");
8935 #if 0
8936                 f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_AFEX_UPDATE);
8937                 /*
8938                  * We will perform the queues update from the sp_core_task as
8939                  * all queue SP operations should run with CORE_LOCK.
8940                  */
8941                 bxe_set_bit(BXE_SP_CORE_AFEX_F_UPDATE, &sc->sp_core_state);
8942                 taskqueue_enqueue(sc->sp_tq, &sc->sp_tq_task);
8943 #endif
8944             }
8945             goto next_spqe;
8946
8947 #if 0
8948         case EVENT_RING_OPCODE_AFEX_VIF_LISTS:
8949             f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_AFEX_VIFLISTS);
8950             bxe_after_afex_vif_lists(sc, elem);
8951             goto next_spqe;
8952 #endif
8953
8954         case EVENT_RING_OPCODE_FORWARD_SETUP:
8955             q_obj = &bxe_fwd_sp_obj(sc, q_obj);
8956             if (q_obj->complete_cmd(sc, q_obj,
8957                                     ECORE_Q_CMD_SETUP_TX_ONLY)) {
8958                 break;
8959             }
8960             goto next_spqe;
8961
8962         case EVENT_RING_OPCODE_FUNCTION_START:
8963             BLOGD(sc, DBG_SP, "got FUNC_START ramrod\n");
8964             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_START)) {
8965                 break;
8966             }
8967             goto next_spqe;
8968
8969         case EVENT_RING_OPCODE_FUNCTION_STOP:
8970             BLOGD(sc, DBG_SP, "got FUNC_STOP ramrod\n");
8971             if (f_obj->complete_cmd(sc, f_obj, ECORE_F_CMD_STOP)) {
8972                 break;
8973             }
8974             goto next_spqe;
8975         }
8976
8977         switch (opcode | sc->state) {
8978         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPEN):
8979         case (EVENT_RING_OPCODE_RSS_UPDATE_RULES | BXE_STATE_OPENING_WAITING_PORT):
8980             cid = elem->message.data.eth_event.echo & BXE_SWCID_MASK;
8981             BLOGD(sc, DBG_SP, "got RSS_UPDATE ramrod. CID %d\n", cid);
8982             rss_raw->clear_pending(rss_raw);
8983             break;
8984
8985         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_OPEN):
8986         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_DIAG):
8987         case (EVENT_RING_OPCODE_SET_MAC | BXE_STATE_CLOSING_WAITING_HALT):
8988         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_OPEN):
8989         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_DIAG):
8990         case (EVENT_RING_OPCODE_CLASSIFICATION_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8991             BLOGD(sc, DBG_SP, "got (un)set mac ramrod\n");
8992             bxe_handle_classification_eqe(sc, elem);
8993             break;
8994
8995         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_OPEN):
8996         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_DIAG):
8997         case (EVENT_RING_OPCODE_MULTICAST_RULES | BXE_STATE_CLOSING_WAITING_HALT):
8998             BLOGD(sc, DBG_SP, "got mcast ramrod\n");
8999             bxe_handle_mcast_eqe(sc);
9000             break;
9001
9002         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_OPEN):
9003         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_DIAG):
9004         case (EVENT_RING_OPCODE_FILTERS_RULES | BXE_STATE_CLOSING_WAITING_HALT):
9005             BLOGD(sc, DBG_SP, "got rx_mode ramrod\n");
9006             bxe_handle_rx_mode_eqe(sc, elem);
9007             break;
9008
9009         default:
9010             /* unknown event log error and continue */
9011             BLOGE(sc, "Unknown EQ event %d, sc->state 0x%x\n",
9012                   elem->message.opcode, sc->state);
9013         }
9014
9015 next_spqe:
9016         spqe_cnt++;
9017     } /* for */
9018
9019     mb();
9020     atomic_add_acq_long(&sc->eq_spq_left, spqe_cnt);
9021
9022     sc->eq_cons = sw_cons;
9023     sc->eq_prod = sw_prod;
9024
9025     /* make sure that above mem writes were issued towards the memory */
9026     wmb();
9027
9028     /* update producer */
9029     bxe_update_eq_prod(sc, sc->eq_prod);
9030 }
9031
9032 static void
9033 bxe_handle_sp_tq(void *context,
9034                  int  pending)
9035 {
9036     struct bxe_softc *sc = (struct bxe_softc *)context;
9037     uint16_t status;
9038
9039     BLOGD(sc, DBG_SP, "---> SP TASK <---\n");
9040
9041     /* what work needs to be performed? */
9042     status = bxe_update_dsb_idx(sc);
9043
9044     BLOGD(sc, DBG_SP, "dsb status 0x%04x\n", status);
9045
9046     /* HW attentions */
9047     if (status & BXE_DEF_SB_ATT_IDX) {
9048         BLOGD(sc, DBG_SP, "---> ATTN INTR <---\n");
9049         bxe_attn_int(sc);
9050         status &= ~BXE_DEF_SB_ATT_IDX;
9051     }
9052
9053     /* SP events: STAT_QUERY and others */
9054     if (status & BXE_DEF_SB_IDX) {
9055         /* handle EQ completions */
9056         BLOGD(sc, DBG_SP, "---> EQ INTR <---\n");
9057         bxe_eq_int(sc);
9058         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID,
9059                    le16toh(sc->def_idx), IGU_INT_NOP, 1);
9060         status &= ~BXE_DEF_SB_IDX;
9061     }
9062
9063     /* if status is non zero then something went wrong */
9064     if (__predict_false(status)) {
9065         BLOGE(sc, "Got an unknown SP interrupt! (0x%04x)\n", status);
9066     }
9067
9068     /* ack status block only if something was actually handled */
9069     bxe_ack_sb(sc, sc->igu_dsb_id, ATTENTION_ID,
9070                le16toh(sc->def_att_idx), IGU_INT_ENABLE, 1);
9071
9072     /*
9073      * Must be called after the EQ processing (since eq leads to sriov
9074      * ramrod completion flows).
9075      * This flow may have been scheduled by the arrival of a ramrod
9076      * completion, or by the sriov code rescheduling itself.
9077      */
9078     // XXX bxe_iov_sp_task(sc);
9079
9080 #if 0
9081     /* AFEX - poll to check if VIFSET_ACK should be sent to MFW */
9082     if (bxe_test_and_clear_bit(ECORE_AFEX_PENDING_VIFSET_MCP_ACK,
9083                                &sc->sp_state)) {
9084         bxe_link_report(sc);
9085         bxe_fw_command(sc, DRV_MSG_CODE_AFEX_VIFSET_ACK, 0);
9086     }
9087 #endif
9088 }
9089
9090 static void
9091 bxe_handle_fp_tq(void *context,
9092                  int  pending)
9093 {
9094     struct bxe_fastpath *fp = (struct bxe_fastpath *)context;
9095     struct bxe_softc *sc = fp->sc;
9096     uint8_t more_tx = FALSE;
9097     uint8_t more_rx = FALSE;
9098
9099     BLOGD(sc, DBG_INTR, "---> FP TASK QUEUE (%d) <---\n", fp->index);
9100
9101     /* XXX
9102      * IFF_DRV_RUNNING state can't be checked here since we process
9103      * slowpath events on a client queue during setup. Instead
9104      * we need to add a "process/continue" flag here that the driver
9105      * can use to tell the task here not to do anything.
9106      */
9107 #if 0
9108     if (!(sc->ifnet->if_drv_flags & IFF_DRV_RUNNING)) {
9109         return;
9110     }
9111 #endif
9112
9113     /* update the fastpath index */
9114     bxe_update_fp_sb_idx(fp);
9115
9116     /* XXX add loop here if ever support multiple tx CoS */
9117     /* fp->txdata[cos] */
9118     if (bxe_has_tx_work(fp)) {
9119         BXE_FP_TX_LOCK(fp);
9120         more_tx = bxe_txeof(sc, fp);
9121         BXE_FP_TX_UNLOCK(fp);
9122     }
9123
9124     if (bxe_has_rx_work(fp)) {
9125         more_rx = bxe_rxeof(sc, fp);
9126     }
9127
9128     if (more_rx /*|| more_tx*/) {
9129         /* still more work to do */
9130         taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
9131         return;
9132     }
9133
9134     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
9135                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
9136 }
9137
9138 static void
9139 bxe_task_fp(struct bxe_fastpath *fp)
9140 {
9141     struct bxe_softc *sc = fp->sc;
9142     uint8_t more_tx = FALSE;
9143     uint8_t more_rx = FALSE;
9144
9145     BLOGD(sc, DBG_INTR, "---> FP TASK ISR (%d) <---\n", fp->index);
9146
9147     /* update the fastpath index */
9148     bxe_update_fp_sb_idx(fp);
9149
9150     /* XXX add loop here if ever support multiple tx CoS */
9151     /* fp->txdata[cos] */
9152     if (bxe_has_tx_work(fp)) {
9153         BXE_FP_TX_LOCK(fp);
9154         more_tx = bxe_txeof(sc, fp);
9155         BXE_FP_TX_UNLOCK(fp);
9156     }
9157
9158     if (bxe_has_rx_work(fp)) {
9159         more_rx = bxe_rxeof(sc, fp);
9160     }
9161
9162     if (more_rx /*|| more_tx*/) {
9163         /* still more work to do, bail out if this ISR and process later */
9164         taskqueue_enqueue_fast(fp->tq, &fp->tq_task);
9165         return;
9166     }
9167
9168     /*
9169      * Here we write the fastpath index taken before doing any tx or rx work.
9170      * It is very well possible other hw events occurred up to this point and
9171      * they were actually processed accordingly above. Since we're going to
9172      * write an older fastpath index, an interrupt is coming which we might
9173      * not do any work in.
9174      */
9175     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID,
9176                le16toh(fp->fp_hc_idx), IGU_INT_ENABLE, 1);
9177 }
9178
9179 /*
9180  * Legacy interrupt entry point.
9181  *
9182  * Verifies that the controller generated the interrupt and
9183  * then calls a separate routine to handle the various
9184  * interrupt causes: link, RX, and TX.
9185  */
9186 static void
9187 bxe_intr_legacy(void *xsc)
9188 {
9189     struct bxe_softc *sc = (struct bxe_softc *)xsc;
9190     struct bxe_fastpath *fp;
9191     uint16_t status, mask;
9192     int i;
9193
9194     BLOGD(sc, DBG_INTR, "---> BXE INTx <---\n");
9195
9196 #if 0
9197     /* Don't handle any interrupts if we're not ready. */
9198     if (__predict_false(sc->intr_sem != 0)) {
9199         return;
9200     }
9201 #endif
9202
9203     /*
9204      * 0 for ustorm, 1 for cstorm
9205      * the bits returned from ack_int() are 0-15
9206      * bit 0 = attention status block
9207      * bit 1 = fast path status block
9208      * a mask of 0x2 or more = tx/rx event
9209      * a mask of 1 = slow path event
9210      */
9211
9212     status = bxe_ack_int(sc);
9213
9214     /* the interrupt is not for us */
9215     if (__predict_false(status == 0)) {
9216         BLOGD(sc, DBG_INTR, "Not our interrupt!\n");
9217         return;
9218     }
9219
9220     BLOGD(sc, DBG_INTR, "Interrupt status 0x%04x\n", status);
9221
9222     FOR_EACH_ETH_QUEUE(sc, i) {
9223         fp = &sc->fp[i];
9224         mask = (0x2 << (fp->index + CNIC_SUPPORT(sc)));
9225         if (status & mask) {
9226             /* acknowledge and disable further fastpath interrupts */
9227             bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9228             bxe_task_fp(fp);
9229             status &= ~mask;
9230         }
9231     }
9232
9233 #if 0
9234     if (CNIC_SUPPORT(sc)) {
9235         mask = 0x2;
9236         if (status & (mask | 0x1)) {
9237             ...
9238             status &= ~mask;
9239         }
9240     }
9241 #endif
9242
9243     if (__predict_false(status & 0x1)) {
9244         /* acknowledge and disable further slowpath interrupts */
9245         bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9246
9247         /* schedule slowpath handler */
9248         taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
9249
9250         status &= ~0x1;
9251     }
9252
9253     if (__predict_false(status)) {
9254         BLOGW(sc, "Unexpected fastpath status (0x%08x)!\n", status);
9255     }
9256 }
9257
9258 /* slowpath interrupt entry point */
9259 static void
9260 bxe_intr_sp(void *xsc)
9261 {
9262     struct bxe_softc *sc = (struct bxe_softc *)xsc;
9263
9264     BLOGD(sc, (DBG_INTR | DBG_SP), "---> SP INTR <---\n");
9265
9266     /* acknowledge and disable further slowpath interrupts */
9267     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9268
9269     /* schedule slowpath handler */
9270     taskqueue_enqueue_fast(sc->sp_tq, &sc->sp_tq_task);
9271 }
9272
9273 /* fastpath interrupt entry point */
9274 static void
9275 bxe_intr_fp(void *xfp)
9276 {
9277     struct bxe_fastpath *fp = (struct bxe_fastpath *)xfp;
9278     struct bxe_softc *sc = fp->sc;
9279
9280     BLOGD(sc, DBG_INTR, "---> FP INTR %d <---\n", fp->index);
9281
9282     BLOGD(sc, DBG_INTR,
9283           "(cpu=%d) MSI-X fp=%d fw_sb=%d igu_sb=%d\n",
9284           curcpu, fp->index, fp->fw_sb_id, fp->igu_sb_id);
9285
9286 #if 0
9287     /* Don't handle any interrupts if we're not ready. */
9288     if (__predict_false(sc->intr_sem != 0)) {
9289         return;
9290     }
9291 #endif
9292
9293     /* acknowledge and disable further fastpath interrupts */
9294     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9295
9296     bxe_task_fp(fp);
9297 }
9298
9299 /* Release all interrupts allocated by the driver. */
9300 static void
9301 bxe_interrupt_free(struct bxe_softc *sc)
9302 {
9303     int i;
9304
9305     switch (sc->interrupt_mode) {
9306     case INTR_MODE_INTX:
9307         BLOGD(sc, DBG_LOAD, "Releasing legacy INTx vector\n");
9308         if (sc->intr[0].resource != NULL) {
9309             bus_release_resource(sc->dev,
9310                                  SYS_RES_IRQ,
9311                                  sc->intr[0].rid,
9312                                  sc->intr[0].resource);
9313         }
9314         break;
9315     case INTR_MODE_MSI:
9316         for (i = 0; i < sc->intr_count; i++) {
9317             BLOGD(sc, DBG_LOAD, "Releasing MSI vector %d\n", i);
9318             if (sc->intr[i].resource && sc->intr[i].rid) {
9319                 bus_release_resource(sc->dev,
9320                                      SYS_RES_IRQ,
9321                                      sc->intr[i].rid,
9322                                      sc->intr[i].resource);
9323             }
9324         }
9325         pci_release_msi(sc->dev);
9326         break;
9327     case INTR_MODE_MSIX:
9328         for (i = 0; i < sc->intr_count; i++) {
9329             BLOGD(sc, DBG_LOAD, "Releasing MSI-X vector %d\n", i);
9330             if (sc->intr[i].resource && sc->intr[i].rid) {
9331                 bus_release_resource(sc->dev,
9332                                      SYS_RES_IRQ,
9333                                      sc->intr[i].rid,
9334                                      sc->intr[i].resource);
9335             }
9336         }
9337         pci_release_msi(sc->dev);
9338         break;
9339     default:
9340         /* nothing to do as initial allocation failed */
9341         break;
9342     }
9343 }
9344
9345 /*
9346  * This function determines and allocates the appropriate
9347  * interrupt based on system capabilites and user request.
9348  *
9349  * The user may force a particular interrupt mode, specify
9350  * the number of receive queues, specify the method for
9351  * distribuitng received frames to receive queues, or use
9352  * the default settings which will automatically select the
9353  * best supported combination.  In addition, the OS may or
9354  * may not support certain combinations of these settings.
9355  * This routine attempts to reconcile the settings requested
9356  * by the user with the capabilites available from the system
9357  * to select the optimal combination of features.
9358  *
9359  * Returns:
9360  *   0 = Success, !0 = Failure.
9361  */
9362 static int
9363 bxe_interrupt_alloc(struct bxe_softc *sc)
9364 {
9365     int msix_count = 0;
9366     int msi_count = 0;
9367     int num_requested = 0;
9368     int num_allocated = 0;
9369     int rid, i, j;
9370     int rc;
9371
9372     /* get the number of available MSI/MSI-X interrupts from the OS */
9373     if (sc->interrupt_mode > 0) {
9374         if (sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) {
9375             msix_count = pci_msix_count(sc->dev);
9376         }
9377
9378         if (sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) {
9379             msi_count = pci_msi_count(sc->dev);
9380         }
9381
9382         BLOGD(sc, DBG_LOAD, "%d MSI and %d MSI-X vectors available\n",
9383               msi_count, msix_count);
9384     }
9385
9386     do { /* try allocating MSI-X interrupt resources (at least 2) */
9387         if (sc->interrupt_mode != INTR_MODE_MSIX) {
9388             break;
9389         }
9390
9391         if (((sc->devinfo.pcie_cap_flags & BXE_MSIX_CAPABLE_FLAG) == 0) ||
9392             (msix_count < 2)) {
9393             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9394             break;
9395         }
9396
9397         /* ask for the necessary number of MSI-X vectors */
9398         num_requested = min((sc->num_queues + 1), msix_count);
9399
9400         BLOGD(sc, DBG_LOAD, "Requesting %d MSI-X vectors\n", num_requested);
9401
9402         num_allocated = num_requested;
9403         if ((rc = pci_alloc_msix(sc->dev, &num_allocated)) != 0) {
9404             BLOGE(sc, "MSI-X alloc failed! (%d)\n", rc);
9405             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9406             break;
9407         }
9408
9409         if (num_allocated < 2) { /* possible? */
9410             BLOGE(sc, "MSI-X allocation less than 2!\n");
9411             sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9412             pci_release_msi(sc->dev);
9413             break;
9414         }
9415
9416         BLOGI(sc, "MSI-X vectors Requested %d and Allocated %d\n",
9417               num_requested, num_allocated);
9418
9419         /* best effort so use the number of vectors allocated to us */
9420         sc->intr_count = num_allocated;
9421         sc->num_queues = num_allocated - 1;
9422
9423         rid = 1; /* initial resource identifier */
9424
9425         /* allocate the MSI-X vectors */
9426         for (i = 0; i < num_allocated; i++) {
9427             sc->intr[i].rid = (rid + i);
9428
9429             if ((sc->intr[i].resource =
9430                  bus_alloc_resource_any(sc->dev,
9431                                         SYS_RES_IRQ,
9432                                         &sc->intr[i].rid,
9433                                         RF_ACTIVE)) == NULL) {
9434                 BLOGE(sc, "Failed to map MSI-X[%d] (rid=%d)!\n",
9435                       i, (rid + i));
9436
9437                 for (j = (i - 1); j >= 0; j--) {
9438                     bus_release_resource(sc->dev,
9439                                          SYS_RES_IRQ,
9440                                          sc->intr[j].rid,
9441                                          sc->intr[j].resource);
9442                 }
9443
9444                 sc->intr_count = 0;
9445                 sc->num_queues = 0;
9446                 sc->interrupt_mode = INTR_MODE_MSI; /* try MSI next */
9447                 pci_release_msi(sc->dev);
9448                 break;
9449             }
9450
9451             BLOGD(sc, DBG_LOAD, "Mapped MSI-X[%d] (rid=%d)\n", i, (rid + i));
9452         }
9453     } while (0);
9454
9455     do { /* try allocating MSI vector resources (at least 2) */
9456         if (sc->interrupt_mode != INTR_MODE_MSI) {
9457             break;
9458         }
9459
9460         if (((sc->devinfo.pcie_cap_flags & BXE_MSI_CAPABLE_FLAG) == 0) ||
9461             (msi_count < 1)) {
9462             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9463             break;
9464         }
9465
9466         /* ask for a single MSI vector */
9467         num_requested = 1;
9468
9469         BLOGD(sc, DBG_LOAD, "Requesting %d MSI vectors\n", num_requested);
9470
9471         num_allocated = num_requested;
9472         if ((rc = pci_alloc_msi(sc->dev, &num_allocated)) != 0) {
9473             BLOGE(sc, "MSI alloc failed (%d)!\n", rc);
9474             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9475             break;
9476         }
9477
9478         if (num_allocated != 1) { /* possible? */
9479             BLOGE(sc, "MSI allocation is not 1!\n");
9480             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9481             pci_release_msi(sc->dev);
9482             break;
9483         }
9484
9485         BLOGI(sc, "MSI vectors Requested %d and Allocated %d\n",
9486               num_requested, num_allocated);
9487
9488         /* best effort so use the number of vectors allocated to us */
9489         sc->intr_count = num_allocated;
9490         sc->num_queues = num_allocated;
9491
9492         rid = 1; /* initial resource identifier */
9493
9494         sc->intr[0].rid = rid;
9495
9496         if ((sc->intr[0].resource =
9497              bus_alloc_resource_any(sc->dev,
9498                                     SYS_RES_IRQ,
9499                                     &sc->intr[0].rid,
9500                                     RF_ACTIVE)) == NULL) {
9501             BLOGE(sc, "Failed to map MSI[0] (rid=%d)!\n", rid);
9502             sc->intr_count = 0;
9503             sc->num_queues = 0;
9504             sc->interrupt_mode = INTR_MODE_INTX; /* try INTx next */
9505             pci_release_msi(sc->dev);
9506             break;
9507         }
9508
9509         BLOGD(sc, DBG_LOAD, "Mapped MSI[0] (rid=%d)\n", rid);
9510     } while (0);
9511
9512     do { /* try allocating INTx vector resources */
9513         if (sc->interrupt_mode != INTR_MODE_INTX) {
9514             break;
9515         }
9516
9517         BLOGD(sc, DBG_LOAD, "Requesting legacy INTx interrupt\n");
9518
9519         /* only one vector for INTx */
9520         sc->intr_count = 1;
9521         sc->num_queues = 1;
9522
9523         rid = 0; /* initial resource identifier */
9524
9525         sc->intr[0].rid = rid;
9526
9527         if ((sc->intr[0].resource =
9528              bus_alloc_resource_any(sc->dev,
9529                                     SYS_RES_IRQ,
9530                                     &sc->intr[0].rid,
9531                                     (RF_ACTIVE | RF_SHAREABLE))) == NULL) {
9532             BLOGE(sc, "Failed to map INTx (rid=%d)!\n", rid);
9533             sc->intr_count = 0;
9534             sc->num_queues = 0;
9535             sc->interrupt_mode = -1; /* Failed! */
9536             break;
9537         }
9538
9539         BLOGD(sc, DBG_LOAD, "Mapped INTx (rid=%d)\n", rid);
9540     } while (0);
9541
9542     if (sc->interrupt_mode == -1) {
9543         BLOGE(sc, "Interrupt Allocation: FAILED!!!\n");
9544         rc = 1;
9545     } else {
9546         BLOGD(sc, DBG_LOAD,
9547               "Interrupt Allocation: interrupt_mode=%d, num_queues=%d\n",
9548               sc->interrupt_mode, sc->num_queues);
9549         rc = 0;
9550     }
9551
9552     return (rc);
9553 }
9554
9555 static void
9556 bxe_interrupt_detach(struct bxe_softc *sc)
9557 {
9558     struct bxe_fastpath *fp;
9559     int i;
9560
9561     /* release interrupt resources */
9562     for (i = 0; i < sc->intr_count; i++) {
9563         if (sc->intr[i].resource && sc->intr[i].tag) {
9564             BLOGD(sc, DBG_LOAD, "Disabling interrupt vector %d\n", i);
9565             bus_teardown_intr(sc->dev, sc->intr[i].resource, sc->intr[i].tag);
9566         }
9567     }
9568
9569     for (i = 0; i < sc->num_queues; i++) {
9570         fp = &sc->fp[i];
9571         if (fp->tq) {
9572             taskqueue_drain(fp->tq, &fp->tq_task);
9573             taskqueue_free(fp->tq);
9574             fp->tq = NULL;
9575         }
9576     }
9577
9578
9579     if (sc->sp_tq) {
9580         taskqueue_drain(sc->sp_tq, &sc->sp_tq_task);
9581         taskqueue_free(sc->sp_tq);
9582         sc->sp_tq = NULL;
9583     }
9584 }
9585
9586 /*
9587  * Enables interrupts and attach to the ISR.
9588  *
9589  * When using multiple MSI/MSI-X vectors the first vector
9590  * is used for slowpath operations while all remaining
9591  * vectors are used for fastpath operations.  If only a
9592  * single MSI/MSI-X vector is used (SINGLE_ISR) then the
9593  * ISR must look for both slowpath and fastpath completions.
9594  */
9595 static int
9596 bxe_interrupt_attach(struct bxe_softc *sc)
9597 {
9598     struct bxe_fastpath *fp;
9599     int rc = 0;
9600     int i;
9601
9602     snprintf(sc->sp_tq_name, sizeof(sc->sp_tq_name),
9603              "bxe%d_sp_tq", sc->unit);
9604     TASK_INIT(&sc->sp_tq_task, 0, bxe_handle_sp_tq, sc);
9605     sc->sp_tq = taskqueue_create_fast(sc->sp_tq_name, M_NOWAIT,
9606                                       taskqueue_thread_enqueue,
9607                                       &sc->sp_tq);
9608     taskqueue_start_threads(&sc->sp_tq, 1, PWAIT, /* lower priority */
9609                             "%s", sc->sp_tq_name);
9610
9611
9612     for (i = 0; i < sc->num_queues; i++) {
9613         fp = &sc->fp[i];
9614         snprintf(fp->tq_name, sizeof(fp->tq_name),
9615                  "bxe%d_fp%d_tq", sc->unit, i);
9616         TASK_INIT(&fp->tq_task, 0, bxe_handle_fp_tq, fp);
9617         fp->tq = taskqueue_create_fast(fp->tq_name, M_NOWAIT,
9618                                        taskqueue_thread_enqueue,
9619                                        &fp->tq);
9620         taskqueue_start_threads(&fp->tq, 1, PI_NET, /* higher priority */
9621                                 "%s", fp->tq_name);
9622     }
9623
9624     /* setup interrupt handlers */
9625     if (sc->interrupt_mode == INTR_MODE_MSIX) {
9626         BLOGD(sc, DBG_LOAD, "Enabling slowpath MSI-X[0] vector\n");
9627
9628         /*
9629          * Setup the interrupt handler. Note that we pass the driver instance
9630          * to the interrupt handler for the slowpath.
9631          */
9632         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9633                                  (INTR_TYPE_NET | INTR_MPSAFE),
9634                                  NULL, bxe_intr_sp, sc,
9635                                  &sc->intr[0].tag)) != 0) {
9636             BLOGE(sc, "Failed to allocate MSI-X[0] vector (%d)\n", rc);
9637             goto bxe_interrupt_attach_exit;
9638         }
9639
9640         bus_describe_intr(sc->dev, sc->intr[0].resource,
9641                           sc->intr[0].tag, "sp");
9642
9643         /* bus_bind_intr(sc->dev, sc->intr[0].resource, 0); */
9644
9645         /* initialize the fastpath vectors (note the first was used for sp) */
9646         for (i = 0; i < sc->num_queues; i++) {
9647             fp = &sc->fp[i];
9648             BLOGD(sc, DBG_LOAD, "Enabling MSI-X[%d] vector\n", (i + 1));
9649
9650             /*
9651              * Setup the interrupt handler. Note that we pass the
9652              * fastpath context to the interrupt handler in this
9653              * case.
9654              */
9655             if ((rc = bus_setup_intr(sc->dev, sc->intr[i + 1].resource,
9656                                      (INTR_TYPE_NET | INTR_MPSAFE),
9657                                      NULL, bxe_intr_fp, fp,
9658                                      &sc->intr[i + 1].tag)) != 0) {
9659                 BLOGE(sc, "Failed to allocate MSI-X[%d] vector (%d)\n",
9660                       (i + 1), rc);
9661                 goto bxe_interrupt_attach_exit;
9662             }
9663
9664             bus_describe_intr(sc->dev, sc->intr[i + 1].resource,
9665                               sc->intr[i + 1].tag, "fp%02d", i);
9666
9667             /* bind the fastpath instance to a cpu */
9668             if (sc->num_queues > 1) {
9669                 bus_bind_intr(sc->dev, sc->intr[i + 1].resource, i);
9670             }
9671
9672             fp->state = BXE_FP_STATE_IRQ;
9673         }
9674     } else if (sc->interrupt_mode == INTR_MODE_MSI) {
9675         BLOGD(sc, DBG_LOAD, "Enabling MSI[0] vector\n");
9676
9677         /*
9678          * Setup the interrupt handler. Note that we pass the
9679          * driver instance to the interrupt handler which
9680          * will handle both the slowpath and fastpath.
9681          */
9682         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9683                                  (INTR_TYPE_NET | INTR_MPSAFE),
9684                                  NULL, bxe_intr_legacy, sc,
9685                                  &sc->intr[0].tag)) != 0) {
9686             BLOGE(sc, "Failed to allocate MSI[0] vector (%d)\n", rc);
9687             goto bxe_interrupt_attach_exit;
9688         }
9689
9690     } else { /* (sc->interrupt_mode == INTR_MODE_INTX) */
9691         BLOGD(sc, DBG_LOAD, "Enabling INTx interrupts\n");
9692
9693         /*
9694          * Setup the interrupt handler. Note that we pass the
9695          * driver instance to the interrupt handler which
9696          * will handle both the slowpath and fastpath.
9697          */
9698         if ((rc = bus_setup_intr(sc->dev, sc->intr[0].resource,
9699                                  (INTR_TYPE_NET | INTR_MPSAFE),
9700                                  NULL, bxe_intr_legacy, sc,
9701                                  &sc->intr[0].tag)) != 0) {
9702             BLOGE(sc, "Failed to allocate INTx interrupt (%d)\n", rc);
9703             goto bxe_interrupt_attach_exit;
9704         }
9705     }
9706
9707 bxe_interrupt_attach_exit:
9708
9709     return (rc);
9710 }
9711
9712 static int  bxe_init_hw_common_chip(struct bxe_softc *sc);
9713 static int  bxe_init_hw_common(struct bxe_softc *sc);
9714 static int  bxe_init_hw_port(struct bxe_softc *sc);
9715 static int  bxe_init_hw_func(struct bxe_softc *sc);
9716 static void bxe_reset_common(struct bxe_softc *sc);
9717 static void bxe_reset_port(struct bxe_softc *sc);
9718 static void bxe_reset_func(struct bxe_softc *sc);
9719 static int  bxe_gunzip_init(struct bxe_softc *sc);
9720 static void bxe_gunzip_end(struct bxe_softc *sc);
9721 static int  bxe_init_firmware(struct bxe_softc *sc);
9722 static void bxe_release_firmware(struct bxe_softc *sc);
9723
9724 static struct
9725 ecore_func_sp_drv_ops bxe_func_sp_drv = {
9726     .init_hw_cmn_chip = bxe_init_hw_common_chip,
9727     .init_hw_cmn      = bxe_init_hw_common,
9728     .init_hw_port     = bxe_init_hw_port,
9729     .init_hw_func     = bxe_init_hw_func,
9730
9731     .reset_hw_cmn     = bxe_reset_common,
9732     .reset_hw_port    = bxe_reset_port,
9733     .reset_hw_func    = bxe_reset_func,
9734
9735     .gunzip_init      = bxe_gunzip_init,
9736     .gunzip_end       = bxe_gunzip_end,
9737
9738     .init_fw          = bxe_init_firmware,
9739     .release_fw       = bxe_release_firmware,
9740 };
9741
9742 static void
9743 bxe_init_func_obj(struct bxe_softc *sc)
9744 {
9745     sc->dmae_ready = 0;
9746
9747     ecore_init_func_obj(sc,
9748                         &sc->func_obj,
9749                         BXE_SP(sc, func_rdata),
9750                         BXE_SP_MAPPING(sc, func_rdata),
9751                         BXE_SP(sc, func_afex_rdata),
9752                         BXE_SP_MAPPING(sc, func_afex_rdata),
9753                         &bxe_func_sp_drv);
9754 }
9755
9756 static int
9757 bxe_init_hw(struct bxe_softc *sc,
9758             uint32_t         load_code)
9759 {
9760     struct ecore_func_state_params func_params = { NULL };
9761     int rc;
9762
9763     /* prepare the parameters for function state transitions */
9764     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
9765
9766     func_params.f_obj = &sc->func_obj;
9767     func_params.cmd = ECORE_F_CMD_HW_INIT;
9768
9769     func_params.params.hw_init.load_phase = load_code;
9770
9771     /*
9772      * Via a plethora of function pointers, we will eventually reach
9773      * bxe_init_hw_common(), bxe_init_hw_port(), or bxe_init_hw_func().
9774      */
9775     rc = ecore_func_state_change(sc, &func_params);
9776
9777     return (rc);
9778 }
9779
9780 static void
9781 bxe_fill(struct bxe_softc *sc,
9782          uint32_t         addr,
9783          int              fill,
9784          uint32_t         len)
9785 {
9786     uint32_t i;
9787
9788     if (!(len % 4) && !(addr % 4)) {
9789         for (i = 0; i < len; i += 4) {
9790             REG_WR(sc, (addr + i), fill);
9791         }
9792     } else {
9793         for (i = 0; i < len; i++) {
9794             REG_WR8(sc, (addr + i), fill);
9795         }
9796     }
9797 }
9798
9799 /* writes FP SP data to FW - data_size in dwords */
9800 static void
9801 bxe_wr_fp_sb_data(struct bxe_softc *sc,
9802                   int              fw_sb_id,
9803                   uint32_t         *sb_data_p,
9804                   uint32_t         data_size)
9805 {
9806     int index;
9807
9808     for (index = 0; index < data_size; index++) {
9809         REG_WR(sc,
9810                (BAR_CSTRORM_INTMEM +
9811                 CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
9812                 (sizeof(uint32_t) * index)),
9813                *(sb_data_p + index));
9814     }
9815 }
9816
9817 static void
9818 bxe_zero_fp_sb(struct bxe_softc *sc,
9819                int              fw_sb_id)
9820 {
9821     struct hc_status_block_data_e2 sb_data_e2;
9822     struct hc_status_block_data_e1x sb_data_e1x;
9823     uint32_t *sb_data_p;
9824     uint32_t data_size = 0;
9825
9826     if (!CHIP_IS_E1x(sc)) {
9827         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9828         sb_data_e2.common.state = SB_DISABLED;
9829         sb_data_e2.common.p_func.vf_valid = FALSE;
9830         sb_data_p = (uint32_t *)&sb_data_e2;
9831         data_size = (sizeof(struct hc_status_block_data_e2) /
9832                      sizeof(uint32_t));
9833     } else {
9834         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9835         sb_data_e1x.common.state = SB_DISABLED;
9836         sb_data_e1x.common.p_func.vf_valid = FALSE;
9837         sb_data_p = (uint32_t *)&sb_data_e1x;
9838         data_size = (sizeof(struct hc_status_block_data_e1x) /
9839                      sizeof(uint32_t));
9840     }
9841
9842     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9843
9844     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id)),
9845              0, CSTORM_STATUS_BLOCK_SIZE);
9846     bxe_fill(sc, (BAR_CSTRORM_INTMEM + CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id)),
9847              0, CSTORM_SYNC_BLOCK_SIZE);
9848 }
9849
9850 static void
9851 bxe_wr_sp_sb_data(struct bxe_softc               *sc,
9852                   struct hc_sp_status_block_data *sp_sb_data)
9853 {
9854     int i;
9855
9856     for (i = 0;
9857          i < (sizeof(struct hc_sp_status_block_data) / sizeof(uint32_t));
9858          i++) {
9859         REG_WR(sc,
9860                (BAR_CSTRORM_INTMEM +
9861                 CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(SC_FUNC(sc)) +
9862                 (i * sizeof(uint32_t))),
9863                *((uint32_t *)sp_sb_data + i));
9864     }
9865 }
9866
9867 static void
9868 bxe_zero_sp_sb(struct bxe_softc *sc)
9869 {
9870     struct hc_sp_status_block_data sp_sb_data;
9871
9872     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
9873
9874     sp_sb_data.state           = SB_DISABLED;
9875     sp_sb_data.p_func.vf_valid = FALSE;
9876
9877     bxe_wr_sp_sb_data(sc, &sp_sb_data);
9878
9879     bxe_fill(sc,
9880              (BAR_CSTRORM_INTMEM +
9881               CSTORM_SP_STATUS_BLOCK_OFFSET(SC_FUNC(sc))),
9882               0, CSTORM_SP_STATUS_BLOCK_SIZE);
9883     bxe_fill(sc,
9884              (BAR_CSTRORM_INTMEM +
9885               CSTORM_SP_SYNC_BLOCK_OFFSET(SC_FUNC(sc))),
9886               0, CSTORM_SP_SYNC_BLOCK_SIZE);
9887 }
9888
9889 static void
9890 bxe_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
9891                              int                       igu_sb_id,
9892                              int                       igu_seg_id)
9893 {
9894     hc_sm->igu_sb_id      = igu_sb_id;
9895     hc_sm->igu_seg_id     = igu_seg_id;
9896     hc_sm->timer_value    = 0xFF;
9897     hc_sm->time_to_expire = 0xFFFFFFFF;
9898 }
9899
9900 static void
9901 bxe_map_sb_state_machines(struct hc_index_data *index_data)
9902 {
9903     /* zero out state machine indices */
9904
9905     /* rx indices */
9906     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
9907
9908     /* tx indices */
9909     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags      &= ~HC_INDEX_DATA_SM_ID;
9910     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
9911     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
9912     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
9913
9914     /* map indices */
9915
9916     /* rx indices */
9917     index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
9918         (SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9919
9920     /* tx indices */
9921     index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
9922         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9923     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
9924         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9925     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
9926         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9927     index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
9928         (SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT);
9929 }
9930
9931 static void
9932 bxe_init_sb(struct bxe_softc *sc,
9933             bus_addr_t       busaddr,
9934             int              vfid,
9935             uint8_t          vf_valid,
9936             int              fw_sb_id,
9937             int              igu_sb_id)
9938 {
9939     struct hc_status_block_data_e2  sb_data_e2;
9940     struct hc_status_block_data_e1x sb_data_e1x;
9941     struct hc_status_block_sm       *hc_sm_p;
9942     uint32_t *sb_data_p;
9943     int igu_seg_id;
9944     int data_size;
9945
9946     if (CHIP_INT_MODE_IS_BC(sc)) {
9947         igu_seg_id = HC_SEG_ACCESS_NORM;
9948     } else {
9949         igu_seg_id = IGU_SEG_ACCESS_NORM;
9950     }
9951
9952     bxe_zero_fp_sb(sc, fw_sb_id);
9953
9954     if (!CHIP_IS_E1x(sc)) {
9955         memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
9956         sb_data_e2.common.state = SB_ENABLED;
9957         sb_data_e2.common.p_func.pf_id = SC_FUNC(sc);
9958         sb_data_e2.common.p_func.vf_id = vfid;
9959         sb_data_e2.common.p_func.vf_valid = vf_valid;
9960         sb_data_e2.common.p_func.vnic_id = SC_VN(sc);
9961         sb_data_e2.common.same_igu_sb_1b = TRUE;
9962         sb_data_e2.common.host_sb_addr.hi = U64_HI(busaddr);
9963         sb_data_e2.common.host_sb_addr.lo = U64_LO(busaddr);
9964         hc_sm_p = sb_data_e2.common.state_machine;
9965         sb_data_p = (uint32_t *)&sb_data_e2;
9966         data_size = (sizeof(struct hc_status_block_data_e2) /
9967                      sizeof(uint32_t));
9968         bxe_map_sb_state_machines(sb_data_e2.index_data);
9969     } else {
9970         memset(&sb_data_e1x, 0, sizeof(struct hc_status_block_data_e1x));
9971         sb_data_e1x.common.state = SB_ENABLED;
9972         sb_data_e1x.common.p_func.pf_id = SC_FUNC(sc);
9973         sb_data_e1x.common.p_func.vf_id = 0xff;
9974         sb_data_e1x.common.p_func.vf_valid = FALSE;
9975         sb_data_e1x.common.p_func.vnic_id = SC_VN(sc);
9976         sb_data_e1x.common.same_igu_sb_1b = TRUE;
9977         sb_data_e1x.common.host_sb_addr.hi = U64_HI(busaddr);
9978         sb_data_e1x.common.host_sb_addr.lo = U64_LO(busaddr);
9979         hc_sm_p = sb_data_e1x.common.state_machine;
9980         sb_data_p = (uint32_t *)&sb_data_e1x;
9981         data_size = (sizeof(struct hc_status_block_data_e1x) /
9982                      sizeof(uint32_t));
9983         bxe_map_sb_state_machines(sb_data_e1x.index_data);
9984     }
9985
9986     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID], igu_sb_id, igu_seg_id);
9987     bxe_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID], igu_sb_id, igu_seg_id);
9988
9989     BLOGD(sc, DBG_LOAD, "Init FW SB %d\n", fw_sb_id);
9990
9991     /* write indices to HW - PCI guarantees endianity of regpairs */
9992     bxe_wr_fp_sb_data(sc, fw_sb_id, sb_data_p, data_size);
9993 }
9994
9995 static inline uint8_t
9996 bxe_fp_qzone_id(struct bxe_fastpath *fp)
9997 {
9998     if (CHIP_IS_E1x(fp->sc)) {
9999         return (fp->cl_id + SC_PORT(fp->sc) * ETH_MAX_RX_CLIENTS_E1H);
10000     } else {
10001         return (fp->cl_id);
10002     }
10003 }
10004
10005 static inline uint32_t
10006 bxe_rx_ustorm_prods_offset(struct bxe_softc    *sc,
10007                            struct bxe_fastpath *fp)
10008 {
10009     uint32_t offset = BAR_USTRORM_INTMEM;
10010
10011 #if 0
10012     if (IS_VF(sc)) {
10013         return (PXP_VF_ADDR_USDM_QUEUES_START +
10014                 (sc->acquire_resp.resc.hw_qid[fp->index] *
10015                  sizeof(struct ustorm_queue_zone_data)));
10016     } else
10017 #endif
10018     if (!CHIP_IS_E1x(sc)) {
10019         offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
10020     } else {
10021         offset += USTORM_RX_PRODS_E1X_OFFSET(SC_PORT(sc), fp->cl_id);
10022     }
10023
10024     return (offset);
10025 }
10026
10027 static void
10028 bxe_init_eth_fp(struct bxe_softc *sc,
10029                 int              idx)
10030 {
10031     struct bxe_fastpath *fp = &sc->fp[idx];
10032     uint32_t cids[ECORE_MULTI_TX_COS] = { 0 };
10033     unsigned long q_type = 0;
10034     int cos;
10035
10036     fp->sc    = sc;
10037     fp->index = idx;
10038
10039     snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
10040              "bxe%d_fp%d_tx_lock", sc->unit, idx);
10041     mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
10042
10043     snprintf(fp->rx_mtx_name, sizeof(fp->rx_mtx_name),
10044              "bxe%d_fp%d_rx_lock", sc->unit, idx);
10045     mtx_init(&fp->rx_mtx, fp->rx_mtx_name, NULL, MTX_DEF);
10046
10047     fp->igu_sb_id = (sc->igu_base_sb + idx + CNIC_SUPPORT(sc));
10048     fp->fw_sb_id = (sc->base_fw_ndsb + idx + CNIC_SUPPORT(sc));
10049
10050     fp->cl_id = (CHIP_IS_E1x(sc)) ?
10051                     (SC_L_ID(sc) + idx) :
10052                     /* want client ID same as IGU SB ID for non-E1 */
10053                     fp->igu_sb_id;
10054     fp->cl_qzone_id = bxe_fp_qzone_id(fp);
10055
10056     /* setup sb indices */
10057     if (!CHIP_IS_E1x(sc)) {
10058         fp->sb_index_values  = fp->status_block.e2_sb->sb.index_values;
10059         fp->sb_running_index = fp->status_block.e2_sb->sb.running_index;
10060     } else {
10061         fp->sb_index_values  = fp->status_block.e1x_sb->sb.index_values;
10062         fp->sb_running_index = fp->status_block.e1x_sb->sb.running_index;
10063     }
10064
10065     /* init shortcut */
10066     fp->ustorm_rx_prods_offset = bxe_rx_ustorm_prods_offset(sc, fp);
10067
10068     fp->rx_cq_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_RX_CQ_CONS];
10069
10070     /*
10071      * XXX If multiple CoS is ever supported then each fastpath structure
10072      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
10073      */
10074     for (cos = 0; cos < sc->max_cos; cos++) {
10075         cids[cos] = idx;
10076     }
10077     fp->tx_cons_sb = &fp->sb_index_values[HC_INDEX_ETH_TX_CQ_CONS_COS0];
10078
10079     /* nothing more for a VF to do */
10080     if (IS_VF(sc)) {
10081         return;
10082     }
10083
10084     bxe_init_sb(sc, fp->sb_dma.paddr, BXE_VF_ID_INVALID, FALSE,
10085                 fp->fw_sb_id, fp->igu_sb_id);
10086
10087     bxe_update_fp_sb_idx(fp);
10088
10089     /* Configure Queue State object */
10090     bit_set(&q_type, ECORE_Q_TYPE_HAS_RX);
10091     bit_set(&q_type, ECORE_Q_TYPE_HAS_TX);
10092
10093     ecore_init_queue_obj(sc,
10094                          &sc->sp_objs[idx].q_obj,
10095                          fp->cl_id,
10096                          cids,
10097                          sc->max_cos,
10098                          SC_FUNC(sc),
10099                          BXE_SP(sc, q_rdata),
10100                          BXE_SP_MAPPING(sc, q_rdata),
10101                          q_type);
10102
10103     /* configure classification DBs */
10104     ecore_init_mac_obj(sc,
10105                        &sc->sp_objs[idx].mac_obj,
10106                        fp->cl_id,
10107                        idx,
10108                        SC_FUNC(sc),
10109                        BXE_SP(sc, mac_rdata),
10110                        BXE_SP_MAPPING(sc, mac_rdata),
10111                        ECORE_FILTER_MAC_PENDING,
10112                        &sc->sp_state,
10113                        ECORE_OBJ_TYPE_RX_TX,
10114                        &sc->macs_pool);
10115
10116     BLOGD(sc, DBG_LOAD, "fp[%d]: sb=%p cl_id=%d fw_sb=%d igu_sb=%d\n",
10117           idx, fp->status_block.e2_sb, fp->cl_id, fp->fw_sb_id, fp->igu_sb_id);
10118 }
10119
10120 static inline void
10121 bxe_update_rx_prod(struct bxe_softc    *sc,
10122                    struct bxe_fastpath *fp,
10123                    uint16_t            rx_bd_prod,
10124                    uint16_t            rx_cq_prod,
10125                    uint16_t            rx_sge_prod)
10126 {
10127     struct ustorm_eth_rx_producers rx_prods = { 0 };
10128     uint32_t i;
10129
10130     /* update producers */
10131     rx_prods.bd_prod  = rx_bd_prod;
10132     rx_prods.cqe_prod = rx_cq_prod;
10133     rx_prods.sge_prod = rx_sge_prod;
10134
10135     /*
10136      * Make sure that the BD and SGE data is updated before updating the
10137      * producers since FW might read the BD/SGE right after the producer
10138      * is updated.
10139      * This is only applicable for weak-ordered memory model archs such
10140      * as IA-64. The following barrier is also mandatory since FW will
10141      * assumes BDs must have buffers.
10142      */
10143     wmb();
10144
10145     for (i = 0; i < (sizeof(rx_prods) / 4); i++) {
10146         REG_WR(sc,
10147                (fp->ustorm_rx_prods_offset + (i * 4)),
10148                ((uint32_t *)&rx_prods)[i]);
10149     }
10150
10151     wmb(); /* keep prod updates ordered */
10152
10153     BLOGD(sc, DBG_RX,
10154           "RX fp[%d]: wrote prods bd_prod=%u cqe_prod=%u sge_prod=%u\n",
10155           fp->index, rx_bd_prod, rx_cq_prod, rx_sge_prod);
10156 }
10157
10158 static void
10159 bxe_init_rx_rings(struct bxe_softc *sc)
10160 {
10161     struct bxe_fastpath *fp;
10162     int i;
10163
10164     for (i = 0; i < sc->num_queues; i++) {
10165         fp = &sc->fp[i];
10166
10167         fp->rx_bd_cons = 0;
10168
10169         /*
10170          * Activate the BD ring...
10171          * Warning, this will generate an interrupt (to the TSTORM)
10172          * so this can only be done after the chip is initialized
10173          */
10174         bxe_update_rx_prod(sc, fp,
10175                            fp->rx_bd_prod,
10176                            fp->rx_cq_prod,
10177                            fp->rx_sge_prod);
10178
10179         if (i != 0) {
10180             continue;
10181         }
10182
10183         if (CHIP_IS_E1(sc)) {
10184             REG_WR(sc,
10185                    (BAR_USTRORM_INTMEM +
10186                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc))),
10187                    U64_LO(fp->rcq_dma.paddr));
10188             REG_WR(sc,
10189                    (BAR_USTRORM_INTMEM +
10190                     USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(SC_FUNC(sc)) + 4),
10191                    U64_HI(fp->rcq_dma.paddr));
10192         }
10193     }
10194 }
10195
10196 static void
10197 bxe_init_tx_ring_one(struct bxe_fastpath *fp)
10198 {
10199     SET_FLAG(fp->tx_db.data.header.header, DOORBELL_HDR_DB_TYPE, 1);
10200     fp->tx_db.data.zero_fill1 = 0;
10201     fp->tx_db.data.prod = 0;
10202
10203     fp->tx_pkt_prod = 0;
10204     fp->tx_pkt_cons = 0;
10205     fp->tx_bd_prod = 0;
10206     fp->tx_bd_cons = 0;
10207     fp->eth_q_stats.tx_pkts = 0;
10208 }
10209
10210 static inline void
10211 bxe_init_tx_rings(struct bxe_softc *sc)
10212 {
10213     int i;
10214
10215     for (i = 0; i < sc->num_queues; i++) {
10216 #if 0
10217         uint8_t cos;
10218         for (cos = 0; cos < sc->max_cos; cos++) {
10219             bxe_init_tx_ring_one(&sc->fp[i].txdata[cos]);
10220         }
10221 #else
10222         bxe_init_tx_ring_one(&sc->fp[i]);
10223 #endif
10224     }
10225 }
10226
10227 static void
10228 bxe_init_def_sb(struct bxe_softc *sc)
10229 {
10230     struct host_sp_status_block *def_sb = sc->def_sb;
10231     bus_addr_t mapping = sc->def_sb_dma.paddr;
10232     int igu_sp_sb_index;
10233     int igu_seg_id;
10234     int port = SC_PORT(sc);
10235     int func = SC_FUNC(sc);
10236     int reg_offset, reg_offset_en5;
10237     uint64_t section;
10238     int index, sindex;
10239     struct hc_sp_status_block_data sp_sb_data;
10240
10241     memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
10242
10243     if (CHIP_INT_MODE_IS_BC(sc)) {
10244         igu_sp_sb_index = DEF_SB_IGU_ID;
10245         igu_seg_id = HC_SEG_ACCESS_DEF;
10246     } else {
10247         igu_sp_sb_index = sc->igu_dsb_id;
10248         igu_seg_id = IGU_SEG_ACCESS_DEF;
10249     }
10250
10251     /* attentions */
10252     section = ((uint64_t)mapping +
10253                offsetof(struct host_sp_status_block, atten_status_block));
10254     def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
10255     sc->attn_state = 0;
10256
10257     reg_offset = (port) ?
10258                      MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
10259                      MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
10260     reg_offset_en5 = (port) ?
10261                          MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
10262                          MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0;
10263
10264     for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
10265         /* take care of sig[0]..sig[4] */
10266         for (sindex = 0; sindex < 4; sindex++) {
10267             sc->attn_group[index].sig[sindex] =
10268                 REG_RD(sc, (reg_offset + (sindex * 0x4) + (0x10 * index)));
10269         }
10270
10271         if (!CHIP_IS_E1x(sc)) {
10272             /*
10273              * enable5 is separate from the rest of the registers,
10274              * and the address skip is 4 and not 16 between the
10275              * different groups
10276              */
10277             sc->attn_group[index].sig[4] =
10278                 REG_RD(sc, (reg_offset_en5 + (0x4 * index)));
10279         } else {
10280             sc->attn_group[index].sig[4] = 0;
10281         }
10282     }
10283
10284     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10285         reg_offset = (port) ?
10286                          HC_REG_ATTN_MSG1_ADDR_L :
10287                          HC_REG_ATTN_MSG0_ADDR_L;
10288         REG_WR(sc, reg_offset, U64_LO(section));
10289         REG_WR(sc, (reg_offset + 4), U64_HI(section));
10290     } else if (!CHIP_IS_E1x(sc)) {
10291         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
10292         REG_WR(sc, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
10293     }
10294
10295     section = ((uint64_t)mapping +
10296                offsetof(struct host_sp_status_block, sp_sb));
10297
10298     bxe_zero_sp_sb(sc);
10299
10300     /* PCI guarantees endianity of regpair */
10301     sp_sb_data.state           = SB_ENABLED;
10302     sp_sb_data.host_sb_addr.lo = U64_LO(section);
10303     sp_sb_data.host_sb_addr.hi = U64_HI(section);
10304     sp_sb_data.igu_sb_id       = igu_sp_sb_index;
10305     sp_sb_data.igu_seg_id      = igu_seg_id;
10306     sp_sb_data.p_func.pf_id    = func;
10307     sp_sb_data.p_func.vnic_id  = SC_VN(sc);
10308     sp_sb_data.p_func.vf_id    = 0xff;
10309
10310     bxe_wr_sp_sb_data(sc, &sp_sb_data);
10311
10312     bxe_ack_sb(sc, sc->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
10313 }
10314
10315 static void
10316 bxe_init_sp_ring(struct bxe_softc *sc)
10317 {
10318     atomic_store_rel_long(&sc->cq_spq_left, MAX_SPQ_PENDING);
10319     sc->spq_prod_idx = 0;
10320     sc->dsb_sp_prod = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_ETH_DEF_CONS];
10321     sc->spq_prod_bd = sc->spq;
10322     sc->spq_last_bd = (sc->spq_prod_bd + MAX_SP_DESC_CNT);
10323 }
10324
10325 static void
10326 bxe_init_eq_ring(struct bxe_softc *sc)
10327 {
10328     union event_ring_elem *elem;
10329     int i;
10330
10331     for (i = 1; i <= NUM_EQ_PAGES; i++) {
10332         elem = &sc->eq[EQ_DESC_CNT_PAGE * i - 1];
10333
10334         elem->next_page.addr.hi = htole32(U64_HI(sc->eq_dma.paddr +
10335                                                  BCM_PAGE_SIZE *
10336                                                  (i % NUM_EQ_PAGES)));
10337         elem->next_page.addr.lo = htole32(U64_LO(sc->eq_dma.paddr +
10338                                                  BCM_PAGE_SIZE *
10339                                                  (i % NUM_EQ_PAGES)));
10340     }
10341
10342     sc->eq_cons    = 0;
10343     sc->eq_prod    = NUM_EQ_DESC;
10344     sc->eq_cons_sb = &sc->def_sb->sp_sb.index_values[HC_SP_INDEX_EQ_CONS];
10345
10346     atomic_store_rel_long(&sc->eq_spq_left,
10347                           (min((MAX_SP_DESC_CNT - MAX_SPQ_PENDING),
10348                                NUM_EQ_DESC) - 1));
10349 }
10350
10351 static void
10352 bxe_init_internal_common(struct bxe_softc *sc)
10353 {
10354     int i;
10355
10356     if (IS_MF_SI(sc)) {
10357         /*
10358          * In switch independent mode, the TSTORM needs to accept
10359          * packets that failed classification, since approximate match
10360          * mac addresses aren't written to NIG LLH.
10361          */
10362         REG_WR8(sc,
10363                 (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
10364                 2);
10365     } else if (!CHIP_IS_E1(sc)) { /* 57710 doesn't support MF */
10366         REG_WR8(sc,
10367                 (BAR_TSTRORM_INTMEM + TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET),
10368                 0);
10369     }
10370
10371     /*
10372      * Zero this manually as its initialization is currently missing
10373      * in the initTool.
10374      */
10375     for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++) {
10376         REG_WR(sc,
10377                (BAR_USTRORM_INTMEM + USTORM_AGG_DATA_OFFSET + (i * 4)),
10378                0);
10379     }
10380
10381     if (!CHIP_IS_E1x(sc)) {
10382         REG_WR8(sc, (BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET),
10383                 CHIP_INT_MODE_IS_BC(sc) ? HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
10384     }
10385 }
10386
10387 static void
10388 bxe_init_internal(struct bxe_softc *sc,
10389                   uint32_t         load_code)
10390 {
10391     switch (load_code) {
10392     case FW_MSG_CODE_DRV_LOAD_COMMON:
10393     case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
10394         bxe_init_internal_common(sc);
10395         /* no break */
10396
10397     case FW_MSG_CODE_DRV_LOAD_PORT:
10398         /* nothing to do */
10399         /* no break */
10400
10401     case FW_MSG_CODE_DRV_LOAD_FUNCTION:
10402         /* internal memory per function is initialized inside bxe_pf_init */
10403         break;
10404
10405     default:
10406         BLOGE(sc, "Unknown load_code (0x%x) from MCP\n", load_code);
10407         break;
10408     }
10409 }
10410
10411 static void
10412 storm_memset_func_cfg(struct bxe_softc                         *sc,
10413                       struct tstorm_eth_function_common_config *tcfg,
10414                       uint16_t                                  abs_fid)
10415 {
10416     uint32_t addr;
10417     size_t size;
10418
10419     addr = (BAR_TSTRORM_INTMEM +
10420             TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid));
10421     size = sizeof(struct tstorm_eth_function_common_config);
10422     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)tcfg);
10423 }
10424
10425 static void
10426 bxe_func_init(struct bxe_softc            *sc,
10427               struct bxe_func_init_params *p)
10428 {
10429     struct tstorm_eth_function_common_config tcfg = { 0 };
10430
10431     if (CHIP_IS_E1x(sc)) {
10432         storm_memset_func_cfg(sc, &tcfg, p->func_id);
10433     }
10434
10435     /* Enable the function in the FW */
10436     storm_memset_vf_to_pf(sc, p->func_id, p->pf_id);
10437     storm_memset_func_en(sc, p->func_id, 1);
10438
10439     /* spq */
10440     if (p->func_flgs & FUNC_FLG_SPQ) {
10441         storm_memset_spq_addr(sc, p->spq_map, p->func_id);
10442         REG_WR(sc,
10443                (XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(p->func_id)),
10444                p->spq_prod);
10445     }
10446 }
10447
10448 /*
10449  * Calculates the sum of vn_min_rates.
10450  * It's needed for further normalizing of the min_rates.
10451  * Returns:
10452  *   sum of vn_min_rates.
10453  *     or
10454  *   0 - if all the min_rates are 0.
10455  * In the later case fainess algorithm should be deactivated.
10456  * If all min rates are not zero then those that are zeroes will be set to 1.
10457  */
10458 static void
10459 bxe_calc_vn_min(struct bxe_softc       *sc,
10460                 struct cmng_init_input *input)
10461 {
10462     uint32_t vn_cfg;
10463     uint32_t vn_min_rate;
10464     int all_zero = 1;
10465     int vn;
10466
10467     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10468         vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10469         vn_min_rate = (((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
10470                         FUNC_MF_CFG_MIN_BW_SHIFT) * 100);
10471
10472         if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10473             /* skip hidden VNs */
10474             vn_min_rate = 0;
10475         } else if (!vn_min_rate) {
10476             /* If min rate is zero - set it to 100 */
10477             vn_min_rate = DEF_MIN_RATE;
10478         } else {
10479             all_zero = 0;
10480         }
10481
10482         input->vnic_min_rate[vn] = vn_min_rate;
10483     }
10484
10485     /* if ETS or all min rates are zeros - disable fairness */
10486     if (BXE_IS_ETS_ENABLED(sc)) {
10487         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10488         BLOGD(sc, DBG_LOAD, "Fairness disabled (ETS)\n");
10489     } else if (all_zero) {
10490         input->flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10491         BLOGD(sc, DBG_LOAD,
10492               "Fariness disabled (all MIN values are zeroes)\n");
10493     } else {
10494         input->flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
10495     }
10496 }
10497
10498 static inline uint16_t
10499 bxe_extract_max_cfg(struct bxe_softc *sc,
10500                     uint32_t         mf_cfg)
10501 {
10502     uint16_t max_cfg = ((mf_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
10503                         FUNC_MF_CFG_MAX_BW_SHIFT);
10504
10505     if (!max_cfg) {
10506         BLOGD(sc, DBG_LOAD, "Max BW configured to 0 - using 100 instead\n");
10507         max_cfg = 100;
10508     }
10509
10510     return (max_cfg);
10511 }
10512
10513 static void
10514 bxe_calc_vn_max(struct bxe_softc       *sc,
10515                 int                    vn,
10516                 struct cmng_init_input *input)
10517 {
10518     uint16_t vn_max_rate;
10519     uint32_t vn_cfg = sc->devinfo.mf_info.mf_config[vn];
10520     uint32_t max_cfg;
10521
10522     if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
10523         vn_max_rate = 0;
10524     } else {
10525         max_cfg = bxe_extract_max_cfg(sc, vn_cfg);
10526
10527         if (IS_MF_SI(sc)) {
10528             /* max_cfg in percents of linkspeed */
10529             vn_max_rate = ((sc->link_vars.line_speed * max_cfg) / 100);
10530         } else { /* SD modes */
10531             /* max_cfg is absolute in 100Mb units */
10532             vn_max_rate = (max_cfg * 100);
10533         }
10534     }
10535
10536     BLOGD(sc, DBG_LOAD, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
10537
10538     input->vnic_max_rate[vn] = vn_max_rate;
10539 }
10540
10541 static void
10542 bxe_cmng_fns_init(struct bxe_softc *sc,
10543                   uint8_t          read_cfg,
10544                   uint8_t          cmng_type)
10545 {
10546     struct cmng_init_input input;
10547     int vn;
10548
10549     memset(&input, 0, sizeof(struct cmng_init_input));
10550
10551     input.port_rate = sc->link_vars.line_speed;
10552
10553     if (cmng_type == CMNG_FNS_MINMAX) {
10554         /* read mf conf from shmem */
10555         if (read_cfg) {
10556             bxe_read_mf_cfg(sc);
10557         }
10558
10559         /* get VN min rate and enable fairness if not 0 */
10560         bxe_calc_vn_min(sc, &input);
10561
10562         /* get VN max rate */
10563         if (sc->port.pmf) {
10564             for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10565                 bxe_calc_vn_max(sc, vn, &input);
10566             }
10567         }
10568
10569         /* always enable rate shaping and fairness */
10570         input.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
10571
10572         ecore_init_cmng(&input, &sc->cmng);
10573         return;
10574     }
10575
10576     /* rate shaping and fairness are disabled */
10577     BLOGD(sc, DBG_LOAD, "rate shaping and fairness have been disabled\n");
10578 }
10579
10580 static int
10581 bxe_get_cmng_fns_mode(struct bxe_softc *sc)
10582 {
10583     if (CHIP_REV_IS_SLOW(sc)) {
10584         return (CMNG_FNS_NONE);
10585     }
10586
10587     if (IS_MF(sc)) {
10588         return (CMNG_FNS_MINMAX);
10589     }
10590
10591     return (CMNG_FNS_NONE);
10592 }
10593
10594 static void
10595 storm_memset_cmng(struct bxe_softc *sc,
10596                   struct cmng_init *cmng,
10597                   uint8_t          port)
10598 {
10599     int vn;
10600     int func;
10601     uint32_t addr;
10602     size_t size;
10603
10604     addr = (BAR_XSTRORM_INTMEM +
10605             XSTORM_CMNG_PER_PORT_VARS_OFFSET(port));
10606     size = sizeof(struct cmng_struct_per_port);
10607     ecore_storm_memset_struct(sc, addr, size, (uint32_t *)&cmng->port);
10608
10609     for (vn = VN_0; vn < SC_MAX_VN_NUM(sc); vn++) {
10610         func = func_by_vn(sc, vn);
10611
10612         addr = (BAR_XSTRORM_INTMEM +
10613                 XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func));
10614         size = sizeof(struct rate_shaping_vars_per_vn);
10615         ecore_storm_memset_struct(sc, addr, size,
10616                                   (uint32_t *)&cmng->vnic.vnic_max_rate[vn]);
10617
10618         addr = (BAR_XSTRORM_INTMEM +
10619                 XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func));
10620         size = sizeof(struct fairness_vars_per_vn);
10621         ecore_storm_memset_struct(sc, addr, size,
10622                                   (uint32_t *)&cmng->vnic.vnic_min_rate[vn]);
10623     }
10624 }
10625
10626 static void
10627 bxe_pf_init(struct bxe_softc *sc)
10628 {
10629     struct bxe_func_init_params func_init = { 0 };
10630     struct event_ring_data eq_data = { { 0 } };
10631     uint16_t flags;
10632
10633     if (!CHIP_IS_E1x(sc)) {
10634         /* reset IGU PF statistics: MSIX + ATTN */
10635         /* PF */
10636         REG_WR(sc,
10637                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10638                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10639                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10640                0);
10641         /* ATTN */
10642         REG_WR(sc,
10643                (IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
10644                 (BXE_IGU_STAS_MSG_VF_CNT * 4) +
10645                 (BXE_IGU_STAS_MSG_PF_CNT * 4) +
10646                 ((CHIP_IS_MODE_4_PORT(sc) ? SC_FUNC(sc) : SC_VN(sc)) * 4)),
10647                0);
10648     }
10649
10650     /* function setup flags */
10651     flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
10652
10653     /*
10654      * This flag is relevant for E1x only.
10655      * E2 doesn't have a TPA configuration in a function level.
10656      */
10657     flags |= (sc->ifnet->if_capenable & IFCAP_LRO) ? FUNC_FLG_TPA : 0;
10658
10659     func_init.func_flgs = flags;
10660     func_init.pf_id     = SC_FUNC(sc);
10661     func_init.func_id   = SC_FUNC(sc);
10662     func_init.spq_map   = sc->spq_dma.paddr;
10663     func_init.spq_prod  = sc->spq_prod_idx;
10664
10665     bxe_func_init(sc, &func_init);
10666
10667     memset(&sc->cmng, 0, sizeof(struct cmng_struct_per_port));
10668
10669     /*
10670      * Congestion management values depend on the link rate.
10671      * There is no active link so initial link rate is set to 10Gbps.
10672      * When the link comes up the congestion management values are
10673      * re-calculated according to the actual link rate.
10674      */
10675     sc->link_vars.line_speed = SPEED_10000;
10676     bxe_cmng_fns_init(sc, TRUE, bxe_get_cmng_fns_mode(sc));
10677
10678     /* Only the PMF sets the HW */
10679     if (sc->port.pmf) {
10680         storm_memset_cmng(sc, &sc->cmng, SC_PORT(sc));
10681     }
10682
10683     /* init Event Queue - PCI bus guarantees correct endainity */
10684     eq_data.base_addr.hi = U64_HI(sc->eq_dma.paddr);
10685     eq_data.base_addr.lo = U64_LO(sc->eq_dma.paddr);
10686     eq_data.producer     = sc->eq_prod;
10687     eq_data.index_id     = HC_SP_INDEX_EQ_CONS;
10688     eq_data.sb_id        = DEF_SB_ID;
10689     storm_memset_eq_data(sc, &eq_data, SC_FUNC(sc));
10690 }
10691
10692 static void
10693 bxe_hc_int_enable(struct bxe_softc *sc)
10694 {
10695     int port = SC_PORT(sc);
10696     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10697     uint32_t val = REG_RD(sc, addr);
10698     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10699     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10700                            (sc->intr_count == 1)) ? TRUE : FALSE;
10701     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10702
10703     if (msix) {
10704         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10705                  HC_CONFIG_0_REG_INT_LINE_EN_0);
10706         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10707                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10708         if (single_msix) {
10709             val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
10710         }
10711     } else if (msi) {
10712         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
10713         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10714                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10715                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10716     } else {
10717         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10718                 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10719                 HC_CONFIG_0_REG_INT_LINE_EN_0 |
10720                 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10721
10722         if (!CHIP_IS_E1(sc)) {
10723             BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n",
10724                   val, port, addr);
10725
10726             REG_WR(sc, addr, val);
10727
10728             val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
10729         }
10730     }
10731
10732     if (CHIP_IS_E1(sc)) {
10733         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0x1FFFF);
10734     }
10735
10736     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x) mode %s\n",
10737           val, port, addr, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10738
10739     REG_WR(sc, addr, val);
10740
10741     /* ensure that HC_CONFIG is written before leading/trailing edge config */
10742     mb();
10743
10744     if (!CHIP_IS_E1(sc)) {
10745         /* init leading/trailing edge */
10746         if (IS_MF(sc)) {
10747             val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10748             if (sc->port.pmf) {
10749                 /* enable nig and gpio3 attention */
10750                 val |= 0x1100;
10751             }
10752         } else {
10753             val = 0xffff;
10754         }
10755
10756         REG_WR(sc, (HC_REG_TRAILING_EDGE_0 + port*8), val);
10757         REG_WR(sc, (HC_REG_LEADING_EDGE_0 + port*8), val);
10758     }
10759
10760     /* make sure that interrupts are indeed enabled from here on */
10761     mb();
10762 }
10763
10764 static void
10765 bxe_igu_int_enable(struct bxe_softc *sc)
10766 {
10767     uint32_t val;
10768     uint8_t msix = (sc->interrupt_mode == INTR_MODE_MSIX) ? TRUE : FALSE;
10769     uint8_t single_msix = ((sc->interrupt_mode == INTR_MODE_MSIX) &&
10770                            (sc->intr_count == 1)) ? TRUE : FALSE;
10771     uint8_t msi = (sc->interrupt_mode == INTR_MODE_MSI) ? TRUE : FALSE;
10772
10773     val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10774
10775     if (msix) {
10776         val &= ~(IGU_PF_CONF_INT_LINE_EN |
10777                  IGU_PF_CONF_SINGLE_ISR_EN);
10778         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10779                 IGU_PF_CONF_ATTN_BIT_EN);
10780         if (single_msix) {
10781             val |= IGU_PF_CONF_SINGLE_ISR_EN;
10782         }
10783     } else if (msi) {
10784         val &= ~IGU_PF_CONF_INT_LINE_EN;
10785         val |= (IGU_PF_CONF_MSI_MSIX_EN |
10786                 IGU_PF_CONF_ATTN_BIT_EN |
10787                 IGU_PF_CONF_SINGLE_ISR_EN);
10788     } else {
10789         val &= ~IGU_PF_CONF_MSI_MSIX_EN;
10790         val |= (IGU_PF_CONF_INT_LINE_EN |
10791                 IGU_PF_CONF_ATTN_BIT_EN |
10792                 IGU_PF_CONF_SINGLE_ISR_EN);
10793     }
10794
10795     /* clean previous status - need to configure igu prior to ack*/
10796     if ((!msix) || single_msix) {
10797         REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10798         bxe_ack_int(sc);
10799     }
10800
10801     val |= IGU_PF_CONF_FUNC_EN;
10802
10803     BLOGD(sc, DBG_INTR, "write 0x%x to IGU mode %s\n",
10804           val, ((msix) ? "MSI-X" : ((msi) ? "MSI" : "INTx")));
10805
10806     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10807
10808     mb();
10809
10810     /* init leading/trailing edge */
10811     if (IS_MF(sc)) {
10812         val = (0xee0f | (1 << (SC_VN(sc) + 4)));
10813         if (sc->port.pmf) {
10814             /* enable nig and gpio3 attention */
10815             val |= 0x1100;
10816         }
10817     } else {
10818         val = 0xffff;
10819     }
10820
10821     REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, val);
10822     REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, val);
10823
10824     /* make sure that interrupts are indeed enabled from here on */
10825     mb();
10826 }
10827
10828 static void
10829 bxe_int_enable(struct bxe_softc *sc)
10830 {
10831     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10832         bxe_hc_int_enable(sc);
10833     } else {
10834         bxe_igu_int_enable(sc);
10835     }
10836 }
10837
10838 static void
10839 bxe_hc_int_disable(struct bxe_softc *sc)
10840 {
10841     int port = SC_PORT(sc);
10842     uint32_t addr = (port) ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
10843     uint32_t val = REG_RD(sc, addr);
10844
10845     /*
10846      * In E1 we must use only PCI configuration space to disable MSI/MSIX
10847      * capablility. It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC
10848      * block
10849      */
10850     if (CHIP_IS_E1(sc)) {
10851         /*
10852          * Since IGU_PF_CONF_MSI_MSIX_EN still always on use mask register
10853          * to prevent from HC sending interrupts after we exit the function
10854          */
10855         REG_WR(sc, (HC_REG_INT_MASK + port*4), 0);
10856
10857         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10858                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10859                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10860     } else {
10861         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
10862                  HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
10863                  HC_CONFIG_0_REG_INT_LINE_EN_0 |
10864                  HC_CONFIG_0_REG_ATTN_BIT_EN_0);
10865     }
10866
10867     BLOGD(sc, DBG_INTR, "write %x to HC %d (addr 0x%x)\n", val, port, addr);
10868
10869     /* flush all outstanding writes */
10870     mb();
10871
10872     REG_WR(sc, addr, val);
10873     if (REG_RD(sc, addr) != val) {
10874         BLOGE(sc, "proper val not read from HC IGU!\n");
10875     }
10876 }
10877
10878 static void
10879 bxe_igu_int_disable(struct bxe_softc *sc)
10880 {
10881     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
10882
10883     val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
10884              IGU_PF_CONF_INT_LINE_EN |
10885              IGU_PF_CONF_ATTN_BIT_EN);
10886
10887     BLOGD(sc, DBG_INTR, "write %x to IGU\n", val);
10888
10889     /* flush all outstanding writes */
10890     mb();
10891
10892     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
10893     if (REG_RD(sc, IGU_REG_PF_CONFIGURATION) != val) {
10894         BLOGE(sc, "proper val not read from IGU!\n");
10895     }
10896 }
10897
10898 static void
10899 bxe_int_disable(struct bxe_softc *sc)
10900 {
10901     if (sc->devinfo.int_block == INT_BLOCK_HC) {
10902         bxe_hc_int_disable(sc);
10903     } else {
10904         bxe_igu_int_disable(sc);
10905     }
10906 }
10907
10908 static void
10909 bxe_nic_init(struct bxe_softc *sc,
10910              int              load_code)
10911 {
10912     int i;
10913
10914     for (i = 0; i < sc->num_queues; i++) {
10915         bxe_init_eth_fp(sc, i);
10916     }
10917
10918     rmb(); /* ensure status block indices were read */
10919
10920     bxe_init_rx_rings(sc);
10921     bxe_init_tx_rings(sc);
10922
10923     if (IS_VF(sc)) {
10924         return;
10925     }
10926
10927     /* initialize MOD_ABS interrupts */
10928     elink_init_mod_abs_int(sc, &sc->link_vars,
10929                            sc->devinfo.chip_id,
10930                            sc->devinfo.shmem_base,
10931                            sc->devinfo.shmem2_base,
10932                            SC_PORT(sc));
10933
10934     bxe_init_def_sb(sc);
10935     bxe_update_dsb_idx(sc);
10936     bxe_init_sp_ring(sc);
10937     bxe_init_eq_ring(sc);
10938     bxe_init_internal(sc, load_code);
10939     bxe_pf_init(sc);
10940     bxe_stats_init(sc);
10941
10942     /* flush all before enabling interrupts */
10943     mb();
10944
10945     bxe_int_enable(sc);
10946
10947     /* check for SPIO5 */
10948     bxe_attn_int_deasserted0(sc,
10949                              REG_RD(sc,
10950                                     (MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
10951                                      SC_PORT(sc)*4)) &
10952                              AEU_INPUTS_ATTN_BITS_SPIO5);
10953 }
10954
10955 static inline void
10956 bxe_init_objs(struct bxe_softc *sc)
10957 {
10958     /* mcast rules must be added to tx if tx switching is enabled */
10959     ecore_obj_type o_type =
10960         (sc->flags & BXE_TX_SWITCHING) ? ECORE_OBJ_TYPE_RX_TX :
10961                                          ECORE_OBJ_TYPE_RX;
10962
10963     /* RX_MODE controlling object */
10964     ecore_init_rx_mode_obj(sc, &sc->rx_mode_obj);
10965
10966     /* multicast configuration controlling object */
10967     ecore_init_mcast_obj(sc,
10968                          &sc->mcast_obj,
10969                          sc->fp[0].cl_id,
10970                          sc->fp[0].index,
10971                          SC_FUNC(sc),
10972                          SC_FUNC(sc),
10973                          BXE_SP(sc, mcast_rdata),
10974                          BXE_SP_MAPPING(sc, mcast_rdata),
10975                          ECORE_FILTER_MCAST_PENDING,
10976                          &sc->sp_state,
10977                          o_type);
10978
10979     /* Setup CAM credit pools */
10980     ecore_init_mac_credit_pool(sc,
10981                                &sc->macs_pool,
10982                                SC_FUNC(sc),
10983                                CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10984                                                  VNICS_PER_PATH(sc));
10985
10986     ecore_init_vlan_credit_pool(sc,
10987                                 &sc->vlans_pool,
10988                                 SC_ABS_FUNC(sc) >> 1,
10989                                 CHIP_IS_E1x(sc) ? VNICS_PER_PORT(sc) :
10990                                                   VNICS_PER_PATH(sc));
10991
10992     /* RSS configuration object */
10993     ecore_init_rss_config_obj(sc,
10994                               &sc->rss_conf_obj,
10995                               sc->fp[0].cl_id,
10996                               sc->fp[0].index,
10997                               SC_FUNC(sc),
10998                               SC_FUNC(sc),
10999                               BXE_SP(sc, rss_rdata),
11000                               BXE_SP_MAPPING(sc, rss_rdata),
11001                               ECORE_FILTER_RSS_CONF_PENDING,
11002                               &sc->sp_state, ECORE_OBJ_TYPE_RX);
11003 }
11004
11005 /*
11006  * Initialize the function. This must be called before sending CLIENT_SETUP
11007  * for the first client.
11008  */
11009 static inline int
11010 bxe_func_start(struct bxe_softc *sc)
11011 {
11012     struct ecore_func_state_params func_params = { NULL };
11013     struct ecore_func_start_params *start_params = &func_params.params.start;
11014
11015     /* Prepare parameters for function state transitions */
11016     bit_set(&func_params.ramrod_flags, RAMROD_COMP_WAIT);
11017
11018     func_params.f_obj = &sc->func_obj;
11019     func_params.cmd = ECORE_F_CMD_START;
11020
11021     /* Function parameters */
11022     start_params->mf_mode     = sc->devinfo.mf_info.mf_mode;
11023     start_params->sd_vlan_tag = OVLAN(sc);
11024
11025     if (CHIP_IS_E2(sc) || CHIP_IS_E3(sc)) {
11026         start_params->network_cos_mode = STATIC_COS;
11027     } else { /* CHIP_IS_E1X */
11028         start_params->network_cos_mode = FW_WRR;
11029     }
11030
11031     start_params->gre_tunnel_mode = 0;
11032     start_params->gre_tunnel_rss  = 0;
11033
11034     return (ecore_func_state_change(sc, &func_params));
11035 }
11036
11037 static int
11038 bxe_set_power_state(struct bxe_softc *sc,
11039                     uint8_t          state)
11040 {
11041     uint16_t pmcsr;
11042
11043     /* If there is no power capability, silently succeed */
11044     if (!(sc->devinfo.pcie_cap_flags & BXE_PM_CAPABLE_FLAG)) {
11045         BLOGW(sc, "No power capability\n");
11046         return (0);
11047     }
11048
11049     pmcsr = pci_read_config(sc->dev,
11050                             (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
11051                             2);
11052
11053     switch (state) {
11054     case PCI_PM_D0:
11055         pci_write_config(sc->dev,
11056                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
11057                          ((pmcsr & ~PCIM_PSTAT_DMASK) | PCIM_PSTAT_PME), 2);
11058
11059         if (pmcsr & PCIM_PSTAT_DMASK) {
11060             /* delay required during transition out of D3hot */
11061             DELAY(20000);
11062         }
11063
11064         break;
11065
11066     case PCI_PM_D3hot:
11067         /* XXX if there are other clients above don't shut down the power */
11068
11069         /* don't shut down the power for emulation and FPGA */
11070         if (CHIP_REV_IS_SLOW(sc)) {
11071             return (0);
11072         }
11073
11074         pmcsr &= ~PCIM_PSTAT_DMASK;
11075         pmcsr |= PCIM_PSTAT_D3;
11076
11077         if (sc->wol) {
11078             pmcsr |= PCIM_PSTAT_PMEENABLE;
11079         }
11080
11081         pci_write_config(sc->dev,
11082                          (sc->devinfo.pcie_pm_cap_reg + PCIR_POWER_STATUS),
11083                          pmcsr, 4);
11084
11085         /*
11086          * No more memory access after this point until device is brought back
11087          * to D0 state.
11088          */
11089         break;
11090
11091     default:
11092         BLOGE(sc, "Can't support PCI power state = %d\n", state);
11093         return (-1);
11094     }
11095
11096     return (0);
11097 }
11098
11099
11100 /* return true if succeeded to acquire the lock */
11101 static uint8_t
11102 bxe_trylock_hw_lock(struct bxe_softc *sc,
11103                     uint32_t         resource)
11104 {
11105     uint32_t lock_status;
11106     uint32_t resource_bit = (1 << resource);
11107     int func = SC_FUNC(sc);
11108     uint32_t hw_lock_control_reg;
11109
11110     BLOGD(sc, DBG_LOAD, "Trying to take a resource lock 0x%x\n", resource);
11111
11112     /* Validating that the resource is within range */
11113     if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
11114         BLOGD(sc, DBG_LOAD,
11115               "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
11116               resource, HW_LOCK_MAX_RESOURCE_VALUE);
11117         return (FALSE);
11118     }
11119
11120     if (func <= 5) {
11121         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
11122     } else {
11123         hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
11124     }
11125
11126     /* try to acquire the lock */
11127     REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
11128     lock_status = REG_RD(sc, hw_lock_control_reg);
11129     if (lock_status & resource_bit) {
11130         return (TRUE);
11131     }
11132
11133     BLOGE(sc, "Failed to get a resource lock 0x%x\n", resource);
11134
11135     return (FALSE);
11136 }
11137
11138 /*
11139  * Get the recovery leader resource id according to the engine this function
11140  * belongs to. Currently only only 2 engines is supported.
11141  */
11142 static int
11143 bxe_get_leader_lock_resource(struct bxe_softc *sc)
11144 {
11145     if (SC_PATH(sc)) {
11146         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_1);
11147     } else {
11148         return (HW_LOCK_RESOURCE_RECOVERY_LEADER_0);
11149     }
11150 }
11151
11152 /* try to acquire a leader lock for current engine */
11153 static uint8_t
11154 bxe_trylock_leader_lock(struct bxe_softc *sc)
11155 {
11156     return (bxe_trylock_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
11157 }
11158
11159 static int
11160 bxe_release_leader_lock(struct bxe_softc *sc)
11161 {
11162     return (bxe_release_hw_lock(sc, bxe_get_leader_lock_resource(sc)));
11163 }
11164
11165 /* close gates #2, #3 and #4 */
11166 static void
11167 bxe_set_234_gates(struct bxe_softc *sc,
11168                   uint8_t          close)
11169 {
11170     uint32_t val;
11171
11172     /* gates #2 and #4a are closed/opened for "not E1" only */
11173     if (!CHIP_IS_E1(sc)) {
11174         /* #4 */
11175         REG_WR(sc, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
11176         /* #2 */
11177         REG_WR(sc, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
11178     }
11179
11180     /* #3 */
11181     if (CHIP_IS_E1x(sc)) {
11182         /* prevent interrupts from HC on both ports */
11183         val = REG_RD(sc, HC_REG_CONFIG_1);
11184         REG_WR(sc, HC_REG_CONFIG_1,
11185                (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
11186                (val & ~(uint32_t)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
11187
11188         val = REG_RD(sc, HC_REG_CONFIG_0);
11189         REG_WR(sc, HC_REG_CONFIG_0,
11190                (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
11191                (val & ~(uint32_t)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
11192     } else {
11193         /* Prevent incomming interrupts in IGU */
11194         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
11195
11196         REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION,
11197                (!close) ?
11198                (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
11199                (val & ~(uint32_t)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
11200     }
11201
11202     BLOGD(sc, DBG_LOAD, "%s gates #2, #3 and #4\n",
11203           close ? "closing" : "opening");
11204
11205     wmb();
11206 }
11207
11208 /* poll for pending writes bit, it should get cleared in no more than 1s */
11209 static int
11210 bxe_er_poll_igu_vq(struct bxe_softc *sc)
11211 {
11212     uint32_t cnt = 1000;
11213     uint32_t pend_bits = 0;
11214
11215     do {
11216         pend_bits = REG_RD(sc, IGU_REG_PENDING_BITS_STATUS);
11217
11218         if (pend_bits == 0) {
11219             break;
11220         }
11221
11222         DELAY(1000);
11223     } while (--cnt > 0);
11224
11225     if (cnt == 0) {
11226         BLOGE(sc, "Still pending IGU requests bits=0x%08x!\n", pend_bits);
11227         return (-1);
11228     }
11229
11230     return (0);
11231 }
11232
11233 #define SHARED_MF_CLP_MAGIC  0x80000000 /* 'magic' bit */
11234
11235 static void
11236 bxe_clp_reset_prep(struct bxe_softc *sc,
11237                    uint32_t         *magic_val)
11238 {
11239     /* Do some magic... */
11240     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
11241     *magic_val = val & SHARED_MF_CLP_MAGIC;
11242     MFCFG_WR(sc, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
11243 }
11244
11245 /* restore the value of the 'magic' bit */
11246 static void
11247 bxe_clp_reset_done(struct bxe_softc *sc,
11248                    uint32_t         magic_val)
11249 {
11250     /* Restore the 'magic' bit value... */
11251     uint32_t val = MFCFG_RD(sc, shared_mf_config.clp_mb);
11252     MFCFG_WR(sc, shared_mf_config.clp_mb,
11253               (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
11254 }
11255
11256 /* prepare for MCP reset, takes care of CLP configurations */
11257 static void
11258 bxe_reset_mcp_prep(struct bxe_softc *sc,
11259                    uint32_t         *magic_val)
11260 {
11261     uint32_t shmem;
11262     uint32_t validity_offset;
11263
11264     /* set `magic' bit in order to save MF config */
11265     if (!CHIP_IS_E1(sc)) {
11266         bxe_clp_reset_prep(sc, magic_val);
11267     }
11268
11269     /* get shmem offset */
11270     shmem = REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
11271     validity_offset =
11272         offsetof(struct shmem_region, validity_map[SC_PORT(sc)]);
11273
11274     /* Clear validity map flags */
11275     if (shmem > 0) {
11276         REG_WR(sc, shmem + validity_offset, 0);
11277     }
11278 }
11279
11280 #define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
11281 #define MCP_ONE_TIMEOUT  100    /* 100 ms */
11282
11283 static void
11284 bxe_mcp_wait_one(struct bxe_softc *sc)
11285 {
11286     /* special handling for emulation and FPGA (10 times longer) */
11287     if (CHIP_REV_IS_SLOW(sc)) {
11288         DELAY((MCP_ONE_TIMEOUT*10) * 1000);
11289     } else {
11290         DELAY((MCP_ONE_TIMEOUT) * 1000);
11291     }
11292 }
11293
11294 /* initialize shmem_base and waits for validity signature to appear */
11295 static int
11296 bxe_init_shmem(struct bxe_softc *sc)
11297 {
11298     int cnt = 0;
11299     uint32_t val = 0;
11300
11301     do {
11302         sc->devinfo.shmem_base     =
11303         sc->link_params.shmem_base =
11304             REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
11305
11306         if (sc->devinfo.shmem_base) {
11307             val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
11308             if (val & SHR_MEM_VALIDITY_MB)
11309                 return (0);
11310         }
11311
11312         bxe_mcp_wait_one(sc);
11313
11314     } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
11315
11316     BLOGE(sc, "BAD MCP validity signature\n");
11317
11318     return (-1);
11319 }
11320
11321 static int
11322 bxe_reset_mcp_comp(struct bxe_softc *sc,
11323                    uint32_t         magic_val)
11324 {
11325     int rc = bxe_init_shmem(sc);
11326
11327     /* Restore the `magic' bit value */
11328     if (!CHIP_IS_E1(sc)) {
11329         bxe_clp_reset_done(sc, magic_val);
11330     }
11331
11332     return (rc);
11333 }
11334
11335 static void
11336 bxe_pxp_prep(struct bxe_softc *sc)
11337 {
11338     if (!CHIP_IS_E1(sc)) {
11339         REG_WR(sc, PXP2_REG_RD_START_INIT, 0);
11340         REG_WR(sc, PXP2_REG_RQ_RBC_DONE, 0);
11341         wmb();
11342     }
11343 }
11344
11345 /*
11346  * Reset the whole chip except for:
11347  *      - PCIE core
11348  *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by one reset bit)
11349  *      - IGU
11350  *      - MISC (including AEU)
11351  *      - GRC
11352  *      - RBCN, RBCP
11353  */
11354 static void
11355 bxe_process_kill_chip_reset(struct bxe_softc *sc,
11356                             uint8_t          global)
11357 {
11358     uint32_t not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
11359     uint32_t global_bits2, stay_reset2;
11360
11361     /*
11362      * Bits that have to be set in reset_mask2 if we want to reset 'global'
11363      * (per chip) blocks.
11364      */
11365     global_bits2 =
11366         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
11367         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
11368
11369     /*
11370      * Don't reset the following blocks.
11371      * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
11372      *            reset, as in 4 port device they might still be owned
11373      *            by the MCP (there is only one leader per path).
11374      */
11375     not_reset_mask1 =
11376         MISC_REGISTERS_RESET_REG_1_RST_HC |
11377         MISC_REGISTERS_RESET_REG_1_RST_PXPV |
11378         MISC_REGISTERS_RESET_REG_1_RST_PXP;
11379
11380     not_reset_mask2 =
11381         MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
11382         MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
11383         MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
11384         MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
11385         MISC_REGISTERS_RESET_REG_2_RST_RBCN |
11386         MISC_REGISTERS_RESET_REG_2_RST_GRC  |
11387         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
11388         MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
11389         MISC_REGISTERS_RESET_REG_2_RST_ATC |
11390         MISC_REGISTERS_RESET_REG_2_PGLC |
11391         MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
11392         MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
11393         MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
11394         MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
11395         MISC_REGISTERS_RESET_REG_2_UMAC0 |
11396         MISC_REGISTERS_RESET_REG_2_UMAC1;
11397
11398     /*
11399      * Keep the following blocks in reset:
11400      *  - all xxMACs are handled by the elink code.
11401      */
11402     stay_reset2 =
11403         MISC_REGISTERS_RESET_REG_2_XMAC |
11404         MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
11405
11406     /* Full reset masks according to the chip */
11407     reset_mask1 = 0xffffffff;
11408
11409     if (CHIP_IS_E1(sc))
11410         reset_mask2 = 0xffff;
11411     else if (CHIP_IS_E1H(sc))
11412         reset_mask2 = 0x1ffff;
11413     else if (CHIP_IS_E2(sc))
11414         reset_mask2 = 0xfffff;
11415     else /* CHIP_IS_E3 */
11416         reset_mask2 = 0x3ffffff;
11417
11418     /* Don't reset global blocks unless we need to */
11419     if (!global)
11420         reset_mask2 &= ~global_bits2;
11421
11422     /*
11423      * In case of attention in the QM, we need to reset PXP
11424      * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
11425      * because otherwise QM reset would release 'close the gates' shortly
11426      * before resetting the PXP, then the PSWRQ would send a write
11427      * request to PGLUE. Then when PXP is reset, PGLUE would try to
11428      * read the payload data from PSWWR, but PSWWR would not
11429      * respond. The write queue in PGLUE would stuck, dmae commands
11430      * would not return. Therefore it's important to reset the second
11431      * reset register (containing the
11432      * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
11433      * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
11434      * bit).
11435      */
11436     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
11437            reset_mask2 & (~not_reset_mask2));
11438
11439     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
11440            reset_mask1 & (~not_reset_mask1));
11441
11442     mb();
11443     wmb();
11444
11445     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
11446            reset_mask2 & (~stay_reset2));
11447
11448     mb();
11449     wmb();
11450
11451     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
11452     wmb();
11453 }
11454
11455 static int
11456 bxe_process_kill(struct bxe_softc *sc,
11457                  uint8_t          global)
11458 {
11459     int cnt = 1000;
11460     uint32_t val = 0;
11461     uint32_t sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
11462     uint32_t tags_63_32 = 0;
11463
11464     /* Empty the Tetris buffer, wait for 1s */
11465     do {
11466         sr_cnt  = REG_RD(sc, PXP2_REG_RD_SR_CNT);
11467         blk_cnt = REG_RD(sc, PXP2_REG_RD_BLK_CNT);
11468         port_is_idle_0 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_0);
11469         port_is_idle_1 = REG_RD(sc, PXP2_REG_RD_PORT_IS_IDLE_1);
11470         pgl_exp_rom2 = REG_RD(sc, PXP2_REG_PGL_EXP_ROM2);
11471         if (CHIP_IS_E3(sc)) {
11472             tags_63_32 = REG_RD(sc, PGLUE_B_REG_TAGS_63_32);
11473         }
11474
11475         if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
11476             ((port_is_idle_0 & 0x1) == 0x1) &&
11477             ((port_is_idle_1 & 0x1) == 0x1) &&
11478             (pgl_exp_rom2 == 0xffffffff) &&
11479             (!CHIP_IS_E3(sc) || (tags_63_32 == 0xffffffff)))
11480             break;
11481         DELAY(1000);
11482     } while (cnt-- > 0);
11483
11484     if (cnt <= 0) {
11485         BLOGE(sc, "ERROR: Tetris buffer didn't get empty or there "
11486                   "are still outstanding read requests after 1s! "
11487                   "sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, "
11488                   "port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
11489               sr_cnt, blk_cnt, port_is_idle_0,
11490               port_is_idle_1, pgl_exp_rom2);
11491         return (-1);
11492     }
11493
11494     mb();
11495
11496     /* Close gates #2, #3 and #4 */
11497     bxe_set_234_gates(sc, TRUE);
11498
11499     /* Poll for IGU VQs for 57712 and newer chips */
11500     if (!CHIP_IS_E1x(sc) && bxe_er_poll_igu_vq(sc)) {
11501         return (-1);
11502     }
11503
11504     /* XXX indicate that "process kill" is in progress to MCP */
11505
11506     /* clear "unprepared" bit */
11507     REG_WR(sc, MISC_REG_UNPREPARED, 0);
11508     mb();
11509
11510     /* Make sure all is written to the chip before the reset */
11511     wmb();
11512
11513     /*
11514      * Wait for 1ms to empty GLUE and PCI-E core queues,
11515      * PSWHST, GRC and PSWRD Tetris buffer.
11516      */
11517     DELAY(1000);
11518
11519     /* Prepare to chip reset: */
11520     /* MCP */
11521     if (global) {
11522         bxe_reset_mcp_prep(sc, &val);
11523     }
11524
11525     /* PXP */
11526     bxe_pxp_prep(sc);
11527     mb();
11528
11529     /* reset the chip */
11530     bxe_process_kill_chip_reset(sc, global);
11531     mb();
11532
11533     /* clear errors in PGB */
11534     if (!CHIP_IS_E1(sc))
11535         REG_WR(sc, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
11536
11537     /* Recover after reset: */
11538     /* MCP */
11539     if (global && bxe_reset_mcp_comp(sc, val)) {
11540         return (-1);
11541     }
11542
11543     /* XXX add resetting the NO_MCP mode DB here */
11544
11545     /* Open the gates #2, #3 and #4 */
11546     bxe_set_234_gates(sc, FALSE);
11547
11548     /* XXX
11549      * IGU/AEU preparation bring back the AEU/IGU to a reset state
11550      * re-enable attentions
11551      */
11552
11553     return (0);
11554 }
11555
11556 static int
11557 bxe_leader_reset(struct bxe_softc *sc)
11558 {
11559     int rc = 0;
11560     uint8_t global = bxe_reset_is_global(sc);
11561     uint32_t load_code;
11562
11563     /*
11564      * If not going to reset MCP, load "fake" driver to reset HW while
11565      * driver is owner of the HW.
11566      */
11567     if (!global && !BXE_NOMCP(sc)) {
11568         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ,
11569                                    DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
11570         if (!load_code) {
11571             BLOGE(sc, "MCP response failure, aborting\n");
11572             rc = -1;
11573             goto exit_leader_reset;
11574         }
11575
11576         if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
11577             (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
11578             BLOGE(sc, "MCP unexpected response, aborting\n");
11579             rc = -1;
11580             goto exit_leader_reset2;
11581         }
11582
11583         load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
11584         if (!load_code) {
11585             BLOGE(sc, "MCP response failure, aborting\n");
11586             rc = -1;
11587             goto exit_leader_reset2;
11588         }
11589     }
11590
11591     /* try to recover after the failure */
11592     if (bxe_process_kill(sc, global)) {
11593         BLOGE(sc, "Something bad occurred on engine %d!\n", SC_PATH(sc));
11594         rc = -1;
11595         goto exit_leader_reset2;
11596     }
11597
11598     /*
11599      * Clear the RESET_IN_PROGRESS and RESET_GLOBAL bits and update the driver
11600      * state.
11601      */
11602     bxe_set_reset_done(sc);
11603     if (global) {
11604         bxe_clear_reset_global(sc);
11605     }
11606
11607 exit_leader_reset2:
11608
11609     /* unload "fake driver" if it was loaded */
11610     if (!global && !BXE_NOMCP(sc)) {
11611         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
11612         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
11613     }
11614
11615 exit_leader_reset:
11616
11617     sc->is_leader = 0;
11618     bxe_release_leader_lock(sc);
11619
11620     mb();
11621     return (rc);
11622 }
11623
11624 /*
11625  * prepare INIT transition, parameters configured:
11626  *   - HC configuration
11627  *   - Queue's CDU context
11628  */
11629 static void
11630 bxe_pf_q_prep_init(struct bxe_softc               *sc,
11631                    struct bxe_fastpath            *fp,
11632                    struct ecore_queue_init_params *init_params)
11633 {
11634     uint8_t cos;
11635     int cxt_index, cxt_offset;
11636
11637     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->rx.flags);
11638     bxe_set_bit(ECORE_Q_FLG_HC, &init_params->tx.flags);
11639
11640     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->rx.flags);
11641     bxe_set_bit(ECORE_Q_FLG_HC_EN, &init_params->tx.flags);
11642
11643     /* HC rate */
11644     init_params->rx.hc_rate =
11645         sc->hc_rx_ticks ? (1000000 / sc->hc_rx_ticks) : 0;
11646     init_params->tx.hc_rate =
11647         sc->hc_tx_ticks ? (1000000 / sc->hc_tx_ticks) : 0;
11648
11649     /* FW SB ID */
11650     init_params->rx.fw_sb_id = init_params->tx.fw_sb_id = fp->fw_sb_id;
11651
11652     /* CQ index among the SB indices */
11653     init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11654     init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
11655
11656     /* set maximum number of COSs supported by this queue */
11657     init_params->max_cos = sc->max_cos;
11658
11659     BLOGD(sc, DBG_LOAD, "fp %d setting queue params max cos to %d\n",
11660           fp->index, init_params->max_cos);
11661
11662     /* set the context pointers queue object */
11663     for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
11664         /* XXX change index/cid here if ever support multiple tx CoS */
11665         /* fp->txdata[cos]->cid */
11666         cxt_index = fp->index / ILT_PAGE_CIDS;
11667         cxt_offset = fp->index - (cxt_index * ILT_PAGE_CIDS);
11668         init_params->cxts[cos] = &sc->context[cxt_index].vcxt[cxt_offset].eth;
11669     }
11670 }
11671
11672 /* set flags that are common for the Tx-only and not normal connections */
11673 static unsigned long
11674 bxe_get_common_flags(struct bxe_softc    *sc,
11675                      struct bxe_fastpath *fp,
11676                      uint8_t             zero_stats)
11677 {
11678     unsigned long flags = 0;
11679
11680     /* PF driver will always initialize the Queue to an ACTIVE state */
11681     bxe_set_bit(ECORE_Q_FLG_ACTIVE, &flags);
11682
11683     /*
11684      * tx only connections collect statistics (on the same index as the
11685      * parent connection). The statistics are zeroed when the parent
11686      * connection is initialized.
11687      */
11688
11689     bxe_set_bit(ECORE_Q_FLG_STATS, &flags);
11690     if (zero_stats) {
11691         bxe_set_bit(ECORE_Q_FLG_ZERO_STATS, &flags);
11692     }
11693
11694     /*
11695      * tx only connections can support tx-switching, though their
11696      * CoS-ness doesn't survive the loopback
11697      */
11698     if (sc->flags & BXE_TX_SWITCHING) {
11699         bxe_set_bit(ECORE_Q_FLG_TX_SWITCH, &flags);
11700     }
11701
11702     bxe_set_bit(ECORE_Q_FLG_PCSUM_ON_PKT, &flags);
11703
11704     return (flags);
11705 }
11706
11707 static unsigned long
11708 bxe_get_q_flags(struct bxe_softc    *sc,
11709                 struct bxe_fastpath *fp,
11710                 uint8_t             leading)
11711 {
11712     unsigned long flags = 0;
11713
11714     if (IS_MF_SD(sc)) {
11715         bxe_set_bit(ECORE_Q_FLG_OV, &flags);
11716     }
11717
11718     if (sc->ifnet->if_capenable & IFCAP_LRO) {
11719         bxe_set_bit(ECORE_Q_FLG_TPA, &flags);
11720         bxe_set_bit(ECORE_Q_FLG_TPA_IPV6, &flags);
11721 #if 0
11722         if (fp->mode == TPA_MODE_GRO)
11723             __set_bit(ECORE_Q_FLG_TPA_GRO, &flags);
11724 #endif
11725     }
11726
11727     if (leading) {
11728         bxe_set_bit(ECORE_Q_FLG_LEADING_RSS, &flags);
11729         bxe_set_bit(ECORE_Q_FLG_MCAST, &flags);
11730     }
11731
11732     bxe_set_bit(ECORE_Q_FLG_VLAN, &flags);
11733
11734 #if 0
11735     /* configure silent vlan removal */
11736     if (IS_MF_AFEX(sc)) {
11737         bxe_set_bit(ECORE_Q_FLG_SILENT_VLAN_REM, &flags);
11738     }
11739 #endif
11740
11741     /* merge with common flags */
11742     return (flags | bxe_get_common_flags(sc, fp, TRUE));
11743 }
11744
11745 static void
11746 bxe_pf_q_prep_general(struct bxe_softc                  *sc,
11747                       struct bxe_fastpath               *fp,
11748                       struct ecore_general_setup_params *gen_init,
11749                       uint8_t                           cos)
11750 {
11751     gen_init->stat_id = bxe_stats_id(fp);
11752     gen_init->spcl_id = fp->cl_id;
11753     gen_init->mtu = sc->mtu;
11754     gen_init->cos = cos;
11755 }
11756
11757 static void
11758 bxe_pf_rx_q_prep(struct bxe_softc              *sc,
11759                  struct bxe_fastpath           *fp,
11760                  struct rxq_pause_params       *pause,
11761                  struct ecore_rxq_setup_params *rxq_init)
11762 {
11763     uint8_t max_sge = 0;
11764     uint16_t sge_sz = 0;
11765     uint16_t tpa_agg_size = 0;
11766
11767     pause->sge_th_lo = SGE_TH_LO(sc);
11768     pause->sge_th_hi = SGE_TH_HI(sc);
11769
11770     /* validate SGE ring has enough to cross high threshold */
11771     if (sc->dropless_fc &&
11772             (pause->sge_th_hi + FW_PREFETCH_CNT) >
11773             (RX_SGE_USABLE_PER_PAGE * RX_SGE_NUM_PAGES)) {
11774         BLOGW(sc, "sge ring threshold limit\n");
11775     }
11776
11777     /* minimum max_aggregation_size is 2*MTU (two full buffers) */
11778     tpa_agg_size = (2 * sc->mtu);
11779     if (tpa_agg_size < sc->max_aggregation_size) {
11780         tpa_agg_size = sc->max_aggregation_size;
11781     }
11782
11783     max_sge = SGE_PAGE_ALIGN(sc->mtu) >> SGE_PAGE_SHIFT;
11784     max_sge = ((max_sge + PAGES_PER_SGE - 1) &
11785                    (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11786     sge_sz = (uint16_t)min(SGE_PAGES, 0xffff);
11787
11788     /* pause - not for e1 */
11789     if (!CHIP_IS_E1(sc)) {
11790         pause->bd_th_lo = BD_TH_LO(sc);
11791         pause->bd_th_hi = BD_TH_HI(sc);
11792
11793         pause->rcq_th_lo = RCQ_TH_LO(sc);
11794         pause->rcq_th_hi = RCQ_TH_HI(sc);
11795
11796         /* validate rings have enough entries to cross high thresholds */
11797         if (sc->dropless_fc &&
11798             pause->bd_th_hi + FW_PREFETCH_CNT >
11799             sc->rx_ring_size) {
11800             BLOGW(sc, "rx bd ring threshold limit\n");
11801         }
11802
11803         if (sc->dropless_fc &&
11804             pause->rcq_th_hi + FW_PREFETCH_CNT >
11805             RCQ_NUM_PAGES * RCQ_USABLE_PER_PAGE) {
11806             BLOGW(sc, "rcq ring threshold limit\n");
11807         }
11808
11809         pause->pri_map = 1;
11810     }
11811
11812     /* rxq setup */
11813     rxq_init->dscr_map   = fp->rx_dma.paddr;
11814     rxq_init->sge_map    = fp->rx_sge_dma.paddr;
11815     rxq_init->rcq_map    = fp->rcq_dma.paddr;
11816     rxq_init->rcq_np_map = (fp->rcq_dma.paddr + BCM_PAGE_SIZE);
11817
11818     /*
11819      * This should be a maximum number of data bytes that may be
11820      * placed on the BD (not including paddings).
11821      */
11822     rxq_init->buf_sz = (fp->rx_buf_size -
11823                         IP_HEADER_ALIGNMENT_PADDING);
11824
11825     rxq_init->cl_qzone_id     = fp->cl_qzone_id;
11826     rxq_init->tpa_agg_sz      = tpa_agg_size;
11827     rxq_init->sge_buf_sz      = sge_sz;
11828     rxq_init->max_sges_pkt    = max_sge;
11829     rxq_init->rss_engine_id   = SC_FUNC(sc);
11830     rxq_init->mcast_engine_id = SC_FUNC(sc);
11831
11832     /*
11833      * Maximum number or simultaneous TPA aggregation for this Queue.
11834      * For PF Clients it should be the maximum available number.
11835      * VF driver(s) may want to define it to a smaller value.
11836      */
11837     rxq_init->max_tpa_queues = MAX_AGG_QS(sc);
11838
11839     rxq_init->cache_line_log = BXE_RX_ALIGN_SHIFT;
11840     rxq_init->fw_sb_id = fp->fw_sb_id;
11841
11842     rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
11843
11844     /*
11845      * configure silent vlan removal
11846      * if multi function mode is afex, then mask default vlan
11847      */
11848     if (IS_MF_AFEX(sc)) {
11849         rxq_init->silent_removal_value =
11850             sc->devinfo.mf_info.afex_def_vlan_tag;
11851         rxq_init->silent_removal_mask = EVL_VLID_MASK;
11852     }
11853 }
11854
11855 static void
11856 bxe_pf_tx_q_prep(struct bxe_softc              *sc,
11857                  struct bxe_fastpath           *fp,
11858                  struct ecore_txq_setup_params *txq_init,
11859                  uint8_t                       cos)
11860 {
11861     /*
11862      * XXX If multiple CoS is ever supported then each fastpath structure
11863      * will need to maintain tx producer/consumer/dma/etc values *per* CoS.
11864      * fp->txdata[cos]->tx_dma.paddr;
11865      */
11866     txq_init->dscr_map     = fp->tx_dma.paddr;
11867     txq_init->sb_cq_index  = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
11868     txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
11869     txq_init->fw_sb_id     = fp->fw_sb_id;
11870
11871     /*
11872      * set the TSS leading client id for TX classfication to the
11873      * leading RSS client id
11874      */
11875     txq_init->tss_leading_cl_id = BXE_FP(sc, 0, cl_id);
11876 }
11877
11878 /*
11879  * This function performs 2 steps in a queue state machine:
11880  *   1) RESET->INIT
11881  *   2) INIT->SETUP
11882  */
11883 static int
11884 bxe_setup_queue(struct bxe_softc    *sc,
11885                 struct bxe_fastpath *fp,
11886                 uint8_t             leading)
11887 {
11888     struct ecore_queue_state_params q_params = { NULL };
11889     struct ecore_queue_setup_params *setup_params =
11890                         &q_params.params.setup;
11891 #if 0
11892     struct ecore_queue_setup_tx_only_params *tx_only_params =
11893                         &q_params.params.tx_only;
11894     uint8_t tx_index;
11895 #endif
11896     int rc;
11897
11898     BLOGD(sc, DBG_LOAD, "setting up queue %d\n", fp->index);
11899
11900     bxe_ack_sb(sc, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
11901
11902     q_params.q_obj = &BXE_SP_OBJ(sc, fp).q_obj;
11903
11904     /* we want to wait for completion in this context */
11905     bxe_set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
11906
11907     /* prepare the INIT parameters */
11908     bxe_pf_q_prep_init(sc, fp, &q_params.params.init);
11909
11910     /* Set the command */
11911     q_params.cmd = ECORE_Q_CMD_INIT;
11912
11913     /* Change the state to INIT */
11914     rc = ecore_queue_state_change(sc, &q_params);
11915     if (rc) {
11916         BLOGE(sc, "Queue(%d) INIT failed\n", fp->index);
11917         return (rc);
11918     }
11919
11920     BLOGD(sc, DBG_LOAD, "init complete\n");
11921
11922     /* now move the Queue to the SETUP state */
11923     memset(setup_params, 0, sizeof(*setup_params));
11924
11925     /* set Queue flags */
11926     setup_params->flags = bxe_get_q_flags(sc, fp, leading);
11927
11928     /* set general SETUP parameters */
11929     bxe_pf_q_prep_general(sc, fp, &setup_params->gen_params,
11930                           FIRST_TX_COS_INDEX);
11931
11932     bxe_pf_rx_q_prep(sc, fp,
11933                      &setup_params->pause_params,
11934                      &setup_params->rxq_params);
11935
11936     bxe_pf_tx_q_prep(sc, fp,
11937                      &setup_params->txq_params,
11938                      FIRST_TX_COS_INDEX);
11939
11940     /* Set the command */
11941     q_params.cmd = ECORE_Q_CMD_SETUP;
11942
11943     /* change the state to SETUP */
11944     rc = ecore_queue_state_change(sc, &q_params);
11945     if (rc) {
11946         BLOGE(sc, "Queue(%d) SETUP failed\n", fp->index);
11947         return (rc);
11948     }
11949
11950 #if 0
11951     /* loop through the relevant tx-only indices */
11952     for (tx_index = FIRST_TX_ONLY_COS_INDEX;
11953          tx_index < sc->max_cos;
11954          tx_index++) {
11955         /* prepare and send tx-only ramrod*/
11956         rc = bxe_setup_tx_only(sc, fp, &q_params,
11957                                tx_only_params, tx_index, leading);
11958         if (rc) {
11959             BLOGE(sc, "Queue(%d.%d) TX_ONLY_SETUP failed\n",
11960                   fp->index, tx_index);
11961             return (rc);
11962         }
11963     }
11964 #endif
11965
11966     return (rc);
11967 }
11968
11969 static int
11970 bxe_setup_leading(struct bxe_softc *sc)
11971 {
11972     return (bxe_setup_queue(sc, &sc->fp[0], TRUE));
11973 }
11974
11975 static int
11976 bxe_config_rss_pf(struct bxe_softc            *sc,
11977                   struct ecore_rss_config_obj *rss_obj,
11978                   uint8_t                     config_hash)
11979 {
11980     struct ecore_config_rss_params params = { NULL };
11981     int i;
11982
11983     /*
11984      * Although RSS is meaningless when there is a single HW queue we
11985      * still need it enabled in order to have HW Rx hash generated.
11986      */
11987
11988     params.rss_obj = rss_obj;
11989
11990     bxe_set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
11991
11992     bxe_set_bit(ECORE_RSS_MODE_REGULAR, &params.rss_flags);
11993
11994     /* RSS configuration */
11995     bxe_set_bit(ECORE_RSS_IPV4, &params.rss_flags);
11996     bxe_set_bit(ECORE_RSS_IPV4_TCP, &params.rss_flags);
11997     bxe_set_bit(ECORE_RSS_IPV6, &params.rss_flags);
11998     bxe_set_bit(ECORE_RSS_IPV6_TCP, &params.rss_flags);
11999     if (rss_obj->udp_rss_v4) {
12000         bxe_set_bit(ECORE_RSS_IPV4_UDP, &params.rss_flags);
12001     }
12002     if (rss_obj->udp_rss_v6) {
12003         bxe_set_bit(ECORE_RSS_IPV6_UDP, &params.rss_flags);
12004     }
12005
12006     /* Hash bits */
12007     params.rss_result_mask = MULTI_MASK;
12008
12009     memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
12010
12011     if (config_hash) {
12012         /* RSS keys */
12013         for (i = 0; i < sizeof(params.rss_key) / 4; i++) {
12014             params.rss_key[i] = arc4random();
12015         }
12016
12017         bxe_set_bit(ECORE_RSS_SET_SRCH, &params.rss_flags);
12018     }
12019
12020     return (ecore_config_rss(sc, &params));
12021 }
12022
12023 static int
12024 bxe_config_rss_eth(struct bxe_softc *sc,
12025                    uint8_t          config_hash)
12026 {
12027     return (bxe_config_rss_pf(sc, &sc->rss_conf_obj, config_hash));
12028 }
12029
12030 static int
12031 bxe_init_rss_pf(struct bxe_softc *sc)
12032 {
12033     uint8_t num_eth_queues = BXE_NUM_ETH_QUEUES(sc);
12034     int i;
12035
12036     /*
12037      * Prepare the initial contents of the indirection table if
12038      * RSS is enabled
12039      */
12040     for (i = 0; i < sizeof(sc->rss_conf_obj.ind_table); i++) {
12041         sc->rss_conf_obj.ind_table[i] =
12042             (sc->fp->cl_id + (i % num_eth_queues));
12043     }
12044
12045     if (sc->udp_rss) {
12046         sc->rss_conf_obj.udp_rss_v4 = sc->rss_conf_obj.udp_rss_v6 = 1;
12047     }
12048
12049     /*
12050      * For 57710 and 57711 SEARCHER configuration (rss_keys) is
12051      * per-port, so if explicit configuration is needed, do it only
12052      * for a PMF.
12053      *
12054      * For 57712 and newer it's a per-function configuration.
12055      */
12056     return (bxe_config_rss_eth(sc, sc->port.pmf || !CHIP_IS_E1x(sc)));
12057 }
12058
12059 static int
12060 bxe_set_mac_one(struct bxe_softc          *sc,
12061                 uint8_t                   *mac,
12062                 struct ecore_vlan_mac_obj *obj,
12063                 uint8_t                   set,
12064                 int                       mac_type,
12065                 unsigned long             *ramrod_flags)
12066 {
12067     struct ecore_vlan_mac_ramrod_params ramrod_param;
12068     int rc;
12069
12070     memset(&ramrod_param, 0, sizeof(ramrod_param));
12071
12072     /* fill in general parameters */
12073     ramrod_param.vlan_mac_obj = obj;
12074     ramrod_param.ramrod_flags = *ramrod_flags;
12075
12076     /* fill a user request section if needed */
12077     if (!bxe_test_bit(RAMROD_CONT, ramrod_flags)) {
12078         memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
12079
12080         bxe_set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
12081
12082         /* Set the command: ADD or DEL */
12083         ramrod_param.user_req.cmd = (set) ? ECORE_VLAN_MAC_ADD :
12084                                             ECORE_VLAN_MAC_DEL;
12085     }
12086
12087     rc = ecore_config_vlan_mac(sc, &ramrod_param);
12088
12089     if (rc == ECORE_EXISTS) {
12090         BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12091         /* do not treat adding same MAC as error */
12092         rc = 0;
12093     } else if (rc < 0) {
12094         BLOGE(sc, "%s MAC failed (%d)\n", (set ? "Set" : "Delete"), rc);
12095     }
12096
12097     return (rc);
12098 }
12099
12100 static int
12101 bxe_set_eth_mac(struct bxe_softc *sc,
12102                 uint8_t          set)
12103 {
12104     unsigned long ramrod_flags = 0;
12105
12106     BLOGD(sc, DBG_LOAD, "Adding Ethernet MAC\n");
12107
12108     bxe_set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
12109
12110     /* Eth MAC is set on RSS leading client (fp[0]) */
12111     return (bxe_set_mac_one(sc, sc->link_params.mac_addr,
12112                             &sc->sp_objs->mac_obj,
12113                             set, ECORE_ETH_MAC, &ramrod_flags));
12114 }
12115
12116 #if 0
12117 static void
12118 bxe_update_max_mf_config(struct bxe_softc *sc,
12119                          uint32_t         value)
12120 {
12121     /* load old values */
12122     uint32_t mf_cfg = sc->devinfo.mf_info.mf_config[SC_VN(sc)];
12123
12124     if (value != bxe_extract_max_cfg(sc, mf_cfg)) {
12125         /* leave all but MAX value */
12126         mf_cfg &= ~FUNC_MF_CFG_MAX_BW_MASK;
12127
12128         /* set new MAX value */
12129         mf_cfg |= ((value << FUNC_MF_CFG_MAX_BW_SHIFT) &
12130                    FUNC_MF_CFG_MAX_BW_MASK);
12131
12132         bxe_fw_command(sc, DRV_MSG_CODE_SET_MF_BW, mf_cfg);
12133     }
12134 }
12135 #endif
12136
12137 static int
12138 bxe_get_cur_phy_idx(struct bxe_softc *sc)
12139 {
12140     uint32_t sel_phy_idx = 0;
12141
12142     if (sc->link_params.num_phys <= 1) {
12143         return (ELINK_INT_PHY);
12144     }
12145
12146     if (sc->link_vars.link_up) {
12147         sel_phy_idx = ELINK_EXT_PHY1;
12148         /* In case link is SERDES, check if the ELINK_EXT_PHY2 is the one */
12149         if ((sc->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
12150             (sc->link_params.phy[ELINK_EXT_PHY2].supported &
12151              ELINK_SUPPORTED_FIBRE))
12152             sel_phy_idx = ELINK_EXT_PHY2;
12153     } else {
12154         switch (elink_phy_selection(&sc->link_params)) {
12155         case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
12156         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
12157         case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
12158                sel_phy_idx = ELINK_EXT_PHY1;
12159                break;
12160         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
12161         case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
12162                sel_phy_idx = ELINK_EXT_PHY2;
12163                break;
12164         }
12165     }
12166
12167     return (sel_phy_idx);
12168 }
12169
12170 static int
12171 bxe_get_link_cfg_idx(struct bxe_softc *sc)
12172 {
12173     uint32_t sel_phy_idx = bxe_get_cur_phy_idx(sc);
12174
12175     /*
12176      * The selected activated PHY is always after swapping (in case PHY
12177      * swapping is enabled). So when swapping is enabled, we need to reverse
12178      * the configuration
12179      */
12180
12181     if (sc->link_params.multi_phy_config & PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
12182         if (sel_phy_idx == ELINK_EXT_PHY1)
12183             sel_phy_idx = ELINK_EXT_PHY2;
12184         else if (sel_phy_idx == ELINK_EXT_PHY2)
12185             sel_phy_idx = ELINK_EXT_PHY1;
12186     }
12187
12188     return (ELINK_LINK_CONFIG_IDX(sel_phy_idx));
12189 }
12190
12191 static void
12192 bxe_set_requested_fc(struct bxe_softc *sc)
12193 {
12194     /*
12195      * Initialize link parameters structure variables
12196      * It is recommended to turn off RX FC for jumbo frames
12197      * for better performance
12198      */
12199     if (CHIP_IS_E1x(sc) && (sc->mtu > 5000)) {
12200         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_TX;
12201     } else {
12202         sc->link_params.req_fc_auto_adv = ELINK_FLOW_CTRL_BOTH;
12203     }
12204 }
12205
12206 static void
12207 bxe_calc_fc_adv(struct bxe_softc *sc)
12208 {
12209     uint8_t cfg_idx = bxe_get_link_cfg_idx(sc);
12210     switch (sc->link_vars.ieee_fc &
12211             MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
12212     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
12213     default:
12214         sc->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
12215                                            ADVERTISED_Pause);
12216         break;
12217
12218     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
12219         sc->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
12220                                           ADVERTISED_Pause);
12221         break;
12222
12223     case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
12224         sc->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
12225         break;
12226     }
12227 }
12228
12229 static uint16_t
12230 bxe_get_mf_speed(struct bxe_softc *sc)
12231 {
12232     uint16_t line_speed = sc->link_vars.line_speed;
12233     if (IS_MF(sc)) {
12234         uint16_t maxCfg =
12235             bxe_extract_max_cfg(sc, sc->devinfo.mf_info.mf_config[SC_VN(sc)]);
12236
12237         /* calculate the current MAX line speed limit for the MF devices */
12238         if (IS_MF_SI(sc)) {
12239             line_speed = (line_speed * maxCfg) / 100;
12240         } else { /* SD mode */
12241             uint16_t vn_max_rate = maxCfg * 100;
12242
12243             if (vn_max_rate < line_speed) {
12244                 line_speed = vn_max_rate;
12245             }
12246         }
12247     }
12248
12249     return (line_speed);
12250 }
12251
12252 static void
12253 bxe_fill_report_data(struct bxe_softc            *sc,
12254                      struct bxe_link_report_data *data)
12255 {
12256     uint16_t line_speed = bxe_get_mf_speed(sc);
12257
12258     memset(data, 0, sizeof(*data));
12259
12260     /* fill the report data with the effective line speed */
12261     data->line_speed = line_speed;
12262
12263     /* Link is down */
12264     if (!sc->link_vars.link_up || (sc->flags & BXE_MF_FUNC_DIS)) {
12265         bxe_set_bit(BXE_LINK_REPORT_LINK_DOWN, &data->link_report_flags);
12266     }
12267
12268     /* Full DUPLEX */
12269     if (sc->link_vars.duplex == DUPLEX_FULL) {
12270         bxe_set_bit(BXE_LINK_REPORT_FULL_DUPLEX, &data->link_report_flags);
12271     }
12272
12273     /* Rx Flow Control is ON */
12274     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_RX) {
12275         bxe_set_bit(BXE_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
12276     }
12277
12278     /* Tx Flow Control is ON */
12279     if (sc->link_vars.flow_ctrl & ELINK_FLOW_CTRL_TX) {
12280         bxe_set_bit(BXE_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
12281     }
12282 }
12283
12284 /* report link status to OS, should be called under phy_lock */
12285 static void
12286 bxe_link_report_locked(struct bxe_softc *sc)
12287 {
12288     struct bxe_link_report_data cur_data;
12289
12290     /* reread mf_cfg */
12291     if (IS_PF(sc) && !CHIP_IS_E1(sc)) {
12292         bxe_read_mf_cfg(sc);
12293     }
12294
12295     /* Read the current link report info */
12296     bxe_fill_report_data(sc, &cur_data);
12297
12298     /* Don't report link down or exactly the same link status twice */
12299     if (!memcmp(&cur_data, &sc->last_reported_link, sizeof(cur_data)) ||
12300         (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
12301                       &sc->last_reported_link.link_report_flags) &&
12302          bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
12303                       &cur_data.link_report_flags))) {
12304         return;
12305     }
12306
12307     sc->link_cnt++;
12308
12309     /* report new link params and remember the state for the next time */
12310     memcpy(&sc->last_reported_link, &cur_data, sizeof(cur_data));
12311
12312     if (bxe_test_bit(BXE_LINK_REPORT_LINK_DOWN,
12313                      &cur_data.link_report_flags)) {
12314         if_link_state_change(sc->ifnet, LINK_STATE_DOWN);
12315         BLOGI(sc, "NIC Link is Down\n");
12316     } else {
12317         const char *duplex;
12318         const char *flow;
12319
12320         if (bxe_test_and_clear_bit(BXE_LINK_REPORT_FULL_DUPLEX,
12321                                    &cur_data.link_report_flags)) {
12322             duplex = "full";
12323         } else {
12324             duplex = "half";
12325         }
12326
12327         /*
12328          * Handle the FC at the end so that only these flags would be
12329          * possibly set. This way we may easily check if there is no FC
12330          * enabled.
12331          */
12332         if (cur_data.link_report_flags) {
12333             if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
12334                              &cur_data.link_report_flags) &&
12335                 bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
12336                              &cur_data.link_report_flags)) {
12337                 flow = "ON - receive & transmit";
12338             } else if (bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
12339                                     &cur_data.link_report_flags) &&
12340                        !bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
12341                                      &cur_data.link_report_flags)) {
12342                 flow = "ON - receive";
12343             } else if (!bxe_test_bit(BXE_LINK_REPORT_RX_FC_ON,
12344                                      &cur_data.link_report_flags) &&
12345                        bxe_test_bit(BXE_LINK_REPORT_TX_FC_ON,
12346                                     &cur_data.link_report_flags)) {
12347                 flow = "ON - transmit";
12348             } else {
12349                 flow = "none"; /* possible? */
12350             }
12351         } else {
12352             flow = "none";
12353         }
12354
12355         if_link_state_change(sc->ifnet, LINK_STATE_UP);
12356         BLOGI(sc, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
12357               cur_data.line_speed, duplex, flow);
12358     }
12359 }
12360
12361 static void
12362 bxe_link_report(struct bxe_softc *sc)
12363 {
12364     bxe_acquire_phy_lock(sc);
12365     bxe_link_report_locked(sc);
12366     bxe_release_phy_lock(sc);
12367 }
12368
12369 static void
12370 bxe_link_status_update(struct bxe_softc *sc)
12371 {
12372     if (sc->state != BXE_STATE_OPEN) {
12373         return;
12374     }
12375
12376 #if 0
12377     /* read updated dcb configuration */
12378     if (IS_PF(sc))
12379         bxe_dcbx_pmf_update(sc);
12380 #endif
12381
12382     if (IS_PF(sc) && !CHIP_REV_IS_SLOW(sc)) {
12383         elink_link_status_update(&sc->link_params, &sc->link_vars);
12384     } else {
12385         sc->port.supported[0] |= (ELINK_SUPPORTED_10baseT_Half |
12386                                   ELINK_SUPPORTED_10baseT_Full |
12387                                   ELINK_SUPPORTED_100baseT_Half |
12388                                   ELINK_SUPPORTED_100baseT_Full |
12389                                   ELINK_SUPPORTED_1000baseT_Full |
12390                                   ELINK_SUPPORTED_2500baseX_Full |
12391                                   ELINK_SUPPORTED_10000baseT_Full |
12392                                   ELINK_SUPPORTED_TP |
12393                                   ELINK_SUPPORTED_FIBRE |
12394                                   ELINK_SUPPORTED_Autoneg |
12395                                   ELINK_SUPPORTED_Pause |
12396                                   ELINK_SUPPORTED_Asym_Pause);
12397         sc->port.advertising[0] = sc->port.supported[0];
12398
12399         sc->link_params.sc                = sc;
12400         sc->link_params.port              = SC_PORT(sc);
12401         sc->link_params.req_duplex[0]     = DUPLEX_FULL;
12402         sc->link_params.req_flow_ctrl[0]  = ELINK_FLOW_CTRL_NONE;
12403         sc->link_params.req_line_speed[0] = SPEED_10000;
12404         sc->link_params.speed_cap_mask[0] = 0x7f0000;
12405         sc->link_params.switch_cfg        = ELINK_SWITCH_CFG_10G;
12406
12407         if (CHIP_REV_IS_FPGA(sc)) {
12408             sc->link_vars.mac_type    = ELINK_MAC_TYPE_EMAC;
12409             sc->link_vars.line_speed  = ELINK_SPEED_1000;
12410             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
12411                                          LINK_STATUS_SPEED_AND_DUPLEX_1000TFD);
12412         } else {
12413             sc->link_vars.mac_type    = ELINK_MAC_TYPE_BMAC;
12414             sc->link_vars.line_speed  = ELINK_SPEED_10000;
12415             sc->link_vars.link_status = (LINK_STATUS_LINK_UP |
12416                                          LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
12417         }
12418
12419         sc->link_vars.link_up = 1;
12420
12421         sc->link_vars.duplex    = DUPLEX_FULL;
12422         sc->link_vars.flow_ctrl = ELINK_FLOW_CTRL_NONE;
12423
12424         if (IS_PF(sc)) {
12425             REG_WR(sc, NIG_REG_EGRESS_DRAIN0_MODE + sc->link_params.port*4, 0);
12426             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12427             bxe_link_report(sc);
12428         }
12429     }
12430
12431     if (IS_PF(sc)) {
12432         if (sc->link_vars.link_up) {
12433             bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12434         } else {
12435             bxe_stats_handle(sc, STATS_EVENT_STOP);
12436         }
12437         bxe_link_report(sc);
12438     } else {
12439         bxe_link_report(sc);
12440         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12441     }
12442 }
12443
12444 static int
12445 bxe_initial_phy_init(struct bxe_softc *sc,
12446                      int              load_mode)
12447 {
12448     int rc, cfg_idx = bxe_get_link_cfg_idx(sc);
12449     uint16_t req_line_speed = sc->link_params.req_line_speed[cfg_idx];
12450     struct elink_params *lp = &sc->link_params;
12451
12452     bxe_set_requested_fc(sc);
12453
12454     if (CHIP_REV_IS_SLOW(sc)) {
12455         uint32_t bond = CHIP_BOND_ID(sc);
12456         uint32_t feat = 0;
12457
12458         if (CHIP_IS_E2(sc) && CHIP_IS_MODE_4_PORT(sc)) {
12459             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12460         } else if (bond & 0x4) {
12461             if (CHIP_IS_E3(sc)) {
12462                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_XMAC;
12463             } else {
12464                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_BMAC;
12465             }
12466         } else if (bond & 0x8) {
12467             if (CHIP_IS_E3(sc)) {
12468                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_UMAC;
12469             } else {
12470                 feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12471             }
12472         }
12473
12474         /* disable EMAC for E3 and above */
12475         if (bond & 0x2) {
12476             feat |= ELINK_FEATURE_CONFIG_EMUL_DISABLE_EMAC;
12477         }
12478
12479         sc->link_params.feature_config_flags |= feat;
12480     }
12481
12482     bxe_acquire_phy_lock(sc);
12483
12484     if (load_mode == LOAD_DIAG) {
12485         lp->loopback_mode = ELINK_LOOPBACK_XGXS;
12486         /* Prefer doing PHY loopback at 10G speed, if possible */
12487         if (lp->req_line_speed[cfg_idx] < ELINK_SPEED_10000) {
12488             if (lp->speed_cap_mask[cfg_idx] &
12489                 PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
12490                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_10000;
12491             } else {
12492                 lp->req_line_speed[cfg_idx] = ELINK_SPEED_1000;
12493             }
12494         }
12495     }
12496
12497     if (load_mode == LOAD_LOOPBACK_EXT) {
12498         lp->loopback_mode = ELINK_LOOPBACK_EXT;
12499     }
12500
12501     rc = elink_phy_init(&sc->link_params, &sc->link_vars);
12502
12503     bxe_release_phy_lock(sc);
12504
12505     bxe_calc_fc_adv(sc);
12506
12507     if (sc->link_vars.link_up) {
12508         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
12509         bxe_link_report(sc);
12510     }
12511
12512     if (!CHIP_REV_IS_SLOW(sc)) {
12513         bxe_periodic_start(sc);
12514     }
12515
12516     sc->link_params.req_line_speed[cfg_idx] = req_line_speed;
12517     return (rc);
12518 }
12519
12520 /* must be called under IF_ADDR_LOCK */
12521 static int
12522 bxe_init_mcast_macs_list(struct bxe_softc                 *sc,
12523                          struct ecore_mcast_ramrod_params *p)
12524 {
12525     struct ifnet *ifp = sc->ifnet;
12526     int mc_count = 0;
12527     struct ifmultiaddr *ifma;
12528     struct ecore_mcast_list_elem *mc_mac;
12529
12530     TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
12531         if (ifma->ifma_addr->sa_family != AF_LINK) {
12532             continue;
12533         }
12534
12535         mc_count++;
12536     }
12537
12538     ECORE_LIST_INIT(&p->mcast_list);
12539     p->mcast_list_len = 0;
12540
12541     if (!mc_count) {
12542         return (0);
12543     }
12544
12545     mc_mac = malloc(sizeof(*mc_mac) * mc_count, M_DEVBUF,
12546                     (M_NOWAIT | M_ZERO));
12547     if (!mc_mac) {
12548         BLOGE(sc, "Failed to allocate temp mcast list\n");
12549         return (-1);
12550     }
12551     bzero(mc_mac, (sizeof(*mc_mac) * mc_count));
12552
12553     TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
12554         if (ifma->ifma_addr->sa_family != AF_LINK) {
12555             continue;
12556         }
12557
12558         mc_mac->mac = (uint8_t *)LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
12559         ECORE_LIST_PUSH_TAIL(&mc_mac->link, &p->mcast_list);
12560
12561         BLOGD(sc, DBG_LOAD,
12562               "Setting MCAST %02X:%02X:%02X:%02X:%02X:%02X\n",
12563               mc_mac->mac[0], mc_mac->mac[1], mc_mac->mac[2],
12564               mc_mac->mac[3], mc_mac->mac[4], mc_mac->mac[5]);
12565
12566         mc_mac++;
12567     }
12568
12569     p->mcast_list_len = mc_count;
12570
12571     return (0);
12572 }
12573
12574 static void
12575 bxe_free_mcast_macs_list(struct ecore_mcast_ramrod_params *p)
12576 {
12577     struct ecore_mcast_list_elem *mc_mac =
12578         ECORE_LIST_FIRST_ENTRY(&p->mcast_list,
12579                                struct ecore_mcast_list_elem,
12580                                link);
12581
12582     if (mc_mac) {
12583         /* only a single free as all mc_macs are in the same heap array */
12584         free(mc_mac, M_DEVBUF);
12585     }
12586 }
12587
12588 static int
12589 bxe_set_mc_list(struct bxe_softc *sc)
12590 {
12591     struct ecore_mcast_ramrod_params rparam = { NULL };
12592     int rc = 0;
12593
12594     rparam.mcast_obj = &sc->mcast_obj;
12595
12596     BXE_MCAST_LOCK(sc);
12597
12598     /* first, clear all configured multicast MACs */
12599     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_DEL);
12600     if (rc < 0) {
12601         BLOGE(sc, "Failed to clear multicast configuration: %d\n", rc);
12602         BXE_MCAST_UNLOCK(sc);
12603         return (rc);
12604     }
12605
12606     /* configure a new MACs list */
12607     rc = bxe_init_mcast_macs_list(sc, &rparam);
12608     if (rc) {
12609         BLOGE(sc, "Failed to create mcast MACs list (%d)\n", rc);
12610         BXE_MCAST_UNLOCK(sc);
12611         return (rc);
12612     }
12613
12614     /* Now add the new MACs */
12615     rc = ecore_config_mcast(sc, &rparam, ECORE_MCAST_CMD_ADD);
12616     if (rc < 0) {
12617         BLOGE(sc, "Failed to set new mcast config (%d)\n", rc);
12618     }
12619
12620     bxe_free_mcast_macs_list(&rparam);
12621
12622     BXE_MCAST_UNLOCK(sc);
12623
12624     return (rc);
12625 }
12626
12627 static int
12628 bxe_set_uc_list(struct bxe_softc *sc)
12629 {
12630     struct ifnet *ifp = sc->ifnet;
12631     struct ecore_vlan_mac_obj *mac_obj = &sc->sp_objs->mac_obj;
12632     struct ifaddr *ifa;
12633     unsigned long ramrod_flags = 0;
12634     int rc;
12635
12636 #if __FreeBSD_version < 800000
12637     IF_ADDR_LOCK(ifp);
12638 #else
12639     if_addr_rlock(ifp);
12640 #endif
12641
12642     /* first schedule a cleanup up of old configuration */
12643     rc = bxe_del_all_macs(sc, mac_obj, ECORE_UC_LIST_MAC, FALSE);
12644     if (rc < 0) {
12645         BLOGE(sc, "Failed to schedule delete of all ETH MACs (%d)\n", rc);
12646 #if __FreeBSD_version < 800000
12647         IF_ADDR_UNLOCK(ifp);
12648 #else
12649         if_addr_runlock(ifp);
12650 #endif
12651         return (rc);
12652     }
12653
12654     ifa = ifp->if_addr;
12655     while (ifa) {
12656         if (ifa->ifa_addr->sa_family != AF_LINK) {
12657             ifa = TAILQ_NEXT(ifa, ifa_link);
12658             continue;
12659         }
12660
12661         rc = bxe_set_mac_one(sc, (uint8_t *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
12662                              mac_obj, TRUE, ECORE_UC_LIST_MAC, &ramrod_flags);
12663         if (rc == -EEXIST) {
12664             BLOGD(sc, DBG_SP, "Failed to schedule ADD operations (EEXIST)\n");
12665             /* do not treat adding same MAC as an error */
12666             rc = 0;
12667         } else if (rc < 0) {
12668             BLOGE(sc, "Failed to schedule ADD operations (%d)\n", rc);
12669 #if __FreeBSD_version < 800000
12670             IF_ADDR_UNLOCK(ifp);
12671 #else
12672             if_addr_runlock(ifp);
12673 #endif
12674             return (rc);
12675         }
12676
12677         ifa = TAILQ_NEXT(ifa, ifa_link);
12678     }
12679
12680 #if __FreeBSD_version < 800000
12681     IF_ADDR_UNLOCK(ifp);
12682 #else
12683     if_addr_runlock(ifp);
12684 #endif
12685
12686     /* Execute the pending commands */
12687     bit_set(&ramrod_flags, RAMROD_CONT);
12688     return (bxe_set_mac_one(sc, NULL, mac_obj, FALSE /* don't care */,
12689                             ECORE_UC_LIST_MAC, &ramrod_flags));
12690 }
12691
12692 static void
12693 bxe_set_rx_mode(struct bxe_softc *sc)
12694 {
12695     struct ifnet *ifp = sc->ifnet;
12696     uint32_t rx_mode = BXE_RX_MODE_NORMAL;
12697
12698     if (sc->state != BXE_STATE_OPEN) {
12699         BLOGD(sc, DBG_SP, "state is %x, returning\n", sc->state);
12700         return;
12701     }
12702
12703     BLOGD(sc, DBG_SP, "ifp->if_flags=0x%x\n", ifp->if_flags);
12704
12705     if (ifp->if_flags & IFF_PROMISC) {
12706         rx_mode = BXE_RX_MODE_PROMISC;
12707     } else if ((ifp->if_flags & IFF_ALLMULTI) ||
12708                ((ifp->if_amcount > BXE_MAX_MULTICAST) &&
12709                 CHIP_IS_E1(sc))) {
12710         rx_mode = BXE_RX_MODE_ALLMULTI;
12711     } else {
12712         if (IS_PF(sc)) {
12713             /* some multicasts */
12714             if (bxe_set_mc_list(sc) < 0) {
12715                 rx_mode = BXE_RX_MODE_ALLMULTI;
12716             }
12717             if (bxe_set_uc_list(sc) < 0) {
12718                 rx_mode = BXE_RX_MODE_PROMISC;
12719             }
12720         }
12721 #if 0
12722         else {
12723             /*
12724              * Configuring mcast to a VF involves sleeping (when we
12725              * wait for the PF's response). Since this function is
12726              * called from a non sleepable context we must schedule
12727              * a work item for this purpose
12728              */
12729             bxe_set_bit(BXE_SP_RTNL_VFPF_MCAST, &sc->sp_rtnl_state);
12730             schedule_delayed_work(&sc->sp_rtnl_task, 0);
12731         }
12732 #endif
12733     }
12734
12735     sc->rx_mode = rx_mode;
12736
12737     /* schedule the rx_mode command */
12738     if (bxe_test_bit(ECORE_FILTER_RX_MODE_PENDING, &sc->sp_state)) {
12739         BLOGD(sc, DBG_LOAD, "Scheduled setting rx_mode with ECORE...\n");
12740         bxe_set_bit(ECORE_FILTER_RX_MODE_SCHED, &sc->sp_state);
12741         return;
12742     }
12743
12744     if (IS_PF(sc)) {
12745         bxe_set_storm_rx_mode(sc);
12746     }
12747 #if 0
12748     else {
12749         /*
12750          * Configuring mcast to a VF involves sleeping (when we
12751          * wait for the PF's response). Since this function is
12752          * called from a non sleepable context we must schedule
12753          * a work item for this purpose
12754          */
12755         bxe_set_bit(BXE_SP_RTNL_VFPF_STORM_RX_MODE, &sc->sp_rtnl_state);
12756         schedule_delayed_work(&sc->sp_rtnl_task, 0);
12757     }
12758 #endif
12759
12760 }
12761
12762
12763 /* update flags in shmem */
12764 static void
12765 bxe_update_drv_flags(struct bxe_softc *sc,
12766                      uint32_t         flags,
12767                      uint32_t         set)
12768 {
12769     uint32_t drv_flags;
12770
12771     if (SHMEM2_HAS(sc, drv_flags)) {
12772         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12773         drv_flags = SHMEM2_RD(sc, drv_flags);
12774
12775         if (set) {
12776             SET_FLAGS(drv_flags, flags);
12777         } else {
12778             RESET_FLAGS(drv_flags, flags);
12779         }
12780
12781         SHMEM2_WR(sc, drv_flags, drv_flags);
12782         BLOGD(sc, DBG_LOAD, "drv_flags 0x%08x\n", drv_flags);
12783
12784         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_DRV_FLAGS);
12785     }
12786 }
12787
12788 /* periodic timer callout routine, only runs when the interface is up */
12789
12790 static void
12791 bxe_periodic_callout_func(void *xsc)
12792 {
12793     struct bxe_softc *sc = (struct bxe_softc *)xsc;
12794     int i;
12795
12796     if (!BXE_CORE_TRYLOCK(sc)) {
12797         /* just bail and try again next time */
12798
12799         if ((sc->state == BXE_STATE_OPEN) &&
12800             (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12801             /* schedule the next periodic callout */
12802             callout_reset(&sc->periodic_callout, hz,
12803                           bxe_periodic_callout_func, sc);
12804         }
12805
12806         return;
12807     }
12808
12809     if ((sc->state != BXE_STATE_OPEN) ||
12810         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_STOP)) {
12811         BLOGW(sc, "periodic callout exit (state=0x%x)\n", sc->state);
12812         BXE_CORE_UNLOCK(sc);
12813         return;
12814     }
12815
12816     /* Check for TX timeouts on any fastpath. */
12817     FOR_EACH_QUEUE(sc, i) {
12818         if (bxe_watchdog(sc, &sc->fp[i]) != 0) {
12819             /* Ruh-Roh, chip was reset! */
12820             break;
12821         }
12822     }
12823
12824     if (!CHIP_REV_IS_SLOW(sc)) {
12825         /*
12826          * This barrier is needed to ensure the ordering between the writing
12827          * to the sc->port.pmf in the bxe_nic_load() or bxe_pmf_update() and
12828          * the reading here.
12829          */
12830         mb();
12831         if (sc->port.pmf) {
12832             bxe_acquire_phy_lock(sc);
12833             elink_period_func(&sc->link_params, &sc->link_vars);
12834             bxe_release_phy_lock(sc);
12835         }
12836     }
12837
12838     if (IS_PF(sc) && !(sc->flags & BXE_NO_PULSE)) {
12839         int mb_idx = SC_FW_MB_IDX(sc);
12840         uint32_t drv_pulse;
12841         uint32_t mcp_pulse;
12842
12843         ++sc->fw_drv_pulse_wr_seq;
12844         sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
12845
12846         drv_pulse = sc->fw_drv_pulse_wr_seq;
12847         bxe_drv_pulse(sc);
12848
12849         mcp_pulse = (SHMEM_RD(sc, func_mb[mb_idx].mcp_pulse_mb) &
12850                      MCP_PULSE_SEQ_MASK);
12851
12852         /*
12853          * The delta between driver pulse and mcp response should
12854          * be 1 (before mcp response) or 0 (after mcp response).
12855          */
12856         if ((drv_pulse != mcp_pulse) &&
12857             (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
12858             /* someone lost a heartbeat... */
12859             BLOGE(sc, "drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
12860                   drv_pulse, mcp_pulse);
12861         }
12862     }
12863
12864     /* state is BXE_STATE_OPEN */
12865     bxe_stats_handle(sc, STATS_EVENT_UPDATE);
12866
12867 #if 0
12868     /* sample VF bulletin board for new posts from PF */
12869     if (IS_VF(sc)) {
12870         bxe_sample_bulletin(sc);
12871     }
12872 #endif
12873
12874     BXE_CORE_UNLOCK(sc);
12875
12876     if ((sc->state == BXE_STATE_OPEN) &&
12877         (atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO)) {
12878         /* schedule the next periodic callout */
12879         callout_reset(&sc->periodic_callout, hz,
12880                       bxe_periodic_callout_func, sc);
12881     }
12882 }
12883
12884 static void
12885 bxe_periodic_start(struct bxe_softc *sc)
12886 {
12887     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
12888     callout_reset(&sc->periodic_callout, hz, bxe_periodic_callout_func, sc);
12889 }
12890
12891 static void
12892 bxe_periodic_stop(struct bxe_softc *sc)
12893 {
12894     atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
12895     callout_drain(&sc->periodic_callout);
12896 }
12897
12898 /* start the controller */
12899 static __noinline int
12900 bxe_nic_load(struct bxe_softc *sc,
12901              int              load_mode)
12902 {
12903     uint32_t val;
12904     int load_code = 0;
12905     int i, rc = 0;
12906
12907     BXE_CORE_LOCK_ASSERT(sc);
12908
12909     BLOGD(sc, DBG_LOAD, "Starting NIC load...\n");
12910
12911     sc->state = BXE_STATE_OPENING_WAITING_LOAD;
12912
12913     if (IS_PF(sc)) {
12914         /* must be called before memory allocation and HW init */
12915         bxe_ilt_set_info(sc);
12916     }
12917
12918     sc->last_reported_link_state = LINK_STATE_UNKNOWN;
12919
12920     bxe_set_fp_rx_buf_size(sc);
12921
12922     if (bxe_alloc_fp_buffers(sc) != 0) {
12923         BLOGE(sc, "Failed to allocate fastpath memory\n");
12924         sc->state = BXE_STATE_CLOSED;
12925         rc = ENOMEM;
12926         goto bxe_nic_load_error0;
12927     }
12928
12929     if (bxe_alloc_mem(sc) != 0) {
12930         sc->state = BXE_STATE_CLOSED;
12931         rc = ENOMEM;
12932         goto bxe_nic_load_error0;
12933     }
12934
12935     if (bxe_alloc_fw_stats_mem(sc) != 0) {
12936         sc->state = BXE_STATE_CLOSED;
12937         rc = ENOMEM;
12938         goto bxe_nic_load_error0;
12939     }
12940
12941     if (IS_PF(sc)) {
12942         /* set pf load just before approaching the MCP */
12943         bxe_set_pf_load(sc);
12944
12945         /* if MCP exists send load request and analyze response */
12946         if (!BXE_NOMCP(sc)) {
12947             /* attempt to load pf */
12948             if (bxe_nic_load_request(sc, &load_code) != 0) {
12949                 sc->state = BXE_STATE_CLOSED;
12950                 rc = ENXIO;
12951                 goto bxe_nic_load_error1;
12952             }
12953
12954             /* what did the MCP say? */
12955             if (bxe_nic_load_analyze_req(sc, load_code) != 0) {
12956                 bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12957                 sc->state = BXE_STATE_CLOSED;
12958                 rc = ENXIO;
12959                 goto bxe_nic_load_error2;
12960             }
12961         } else {
12962             BLOGI(sc, "Device has no MCP!\n");
12963             load_code = bxe_nic_load_no_mcp(sc);
12964         }
12965
12966         /* mark PMF if applicable */
12967         bxe_nic_load_pmf(sc, load_code);
12968
12969         /* Init Function state controlling object */
12970         bxe_init_func_obj(sc);
12971
12972         /* Initialize HW */
12973         if (bxe_init_hw(sc, load_code) != 0) {
12974             BLOGE(sc, "HW init failed\n");
12975             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
12976             sc->state = BXE_STATE_CLOSED;
12977             rc = ENXIO;
12978             goto bxe_nic_load_error2;
12979         }
12980     }
12981
12982     /* set ALWAYS_ALIVE bit in shmem */
12983     sc->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
12984     bxe_drv_pulse(sc);
12985     sc->flags |= BXE_NO_PULSE;
12986
12987     /* attach interrupts */
12988     if (bxe_interrupt_attach(sc) != 0) {
12989         sc->state = BXE_STATE_CLOSED;
12990         rc = ENXIO;
12991         goto bxe_nic_load_error2;
12992     }
12993
12994     bxe_nic_init(sc, load_code);
12995
12996     /* Init per-function objects */
12997     if (IS_PF(sc)) {
12998         bxe_init_objs(sc);
12999         // XXX bxe_iov_nic_init(sc);
13000
13001         /* set AFEX default VLAN tag to an invalid value */
13002         sc->devinfo.mf_info.afex_def_vlan_tag = -1;
13003         // XXX bxe_nic_load_afex_dcc(sc, load_code);
13004
13005         sc->state = BXE_STATE_OPENING_WAITING_PORT;
13006         rc = bxe_func_start(sc);
13007         if (rc) {
13008             BLOGE(sc, "Function start failed!\n");
13009             bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
13010             sc->state = BXE_STATE_ERROR;
13011             goto bxe_nic_load_error3;
13012         }
13013
13014         /* send LOAD_DONE command to MCP */
13015         if (!BXE_NOMCP(sc)) {
13016             load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE, 0);
13017             if (!load_code) {
13018                 BLOGE(sc, "MCP response failure, aborting\n");
13019                 sc->state = BXE_STATE_ERROR;
13020                 rc = ENXIO;
13021                 goto bxe_nic_load_error3;
13022             }
13023         }
13024
13025         rc = bxe_setup_leading(sc);
13026         if (rc) {
13027             BLOGE(sc, "Setup leading failed!\n");
13028             sc->state = BXE_STATE_ERROR;
13029             goto bxe_nic_load_error3;
13030         }
13031
13032         FOR_EACH_NONDEFAULT_ETH_QUEUE(sc, i) {
13033             rc = bxe_setup_queue(sc, &sc->fp[i], FALSE);
13034             if (rc) {
13035                 BLOGE(sc, "Queue(%d) setup failed\n", i);
13036                 sc->state = BXE_STATE_ERROR;
13037                 goto bxe_nic_load_error3;
13038             }
13039         }
13040
13041         rc = bxe_init_rss_pf(sc);
13042         if (rc) {
13043             BLOGE(sc, "PF RSS init failed\n");
13044             sc->state = BXE_STATE_ERROR;
13045             goto bxe_nic_load_error3;
13046         }
13047     }
13048     /* XXX VF */
13049 #if 0
13050     else { /* VF */
13051         FOR_EACH_ETH_QUEUE(sc, i) {
13052             rc = bxe_vfpf_setup_q(sc, i);
13053             if (rc) {
13054                 BLOGE(sc, "Queue(%d) setup failed\n", i);
13055                 sc->state = BXE_STATE_ERROR;
13056                 goto bxe_nic_load_error3;
13057             }
13058         }
13059     }
13060 #endif
13061
13062     /* now when Clients are configured we are ready to work */
13063     sc->state = BXE_STATE_OPEN;
13064
13065     /* Configure a ucast MAC */
13066     if (IS_PF(sc)) {
13067         rc = bxe_set_eth_mac(sc, TRUE);
13068     }
13069 #if 0
13070     else { /* IS_VF(sc) */
13071         rc = bxe_vfpf_set_mac(sc);
13072     }
13073 #endif
13074     if (rc) {
13075         BLOGE(sc, "Setting Ethernet MAC failed\n");
13076         sc->state = BXE_STATE_ERROR;
13077         goto bxe_nic_load_error3;
13078     }
13079
13080 #if 0
13081     if (IS_PF(sc) && sc->pending_max) {
13082         /* for AFEX */
13083         bxe_update_max_mf_config(sc, sc->pending_max);
13084         sc->pending_max = 0;
13085     }
13086 #endif
13087
13088     if (sc->port.pmf) {
13089         rc = bxe_initial_phy_init(sc, /* XXX load_mode */LOAD_OPEN);
13090         if (rc) {
13091             sc->state = BXE_STATE_ERROR;
13092             goto bxe_nic_load_error3;
13093         }
13094     }
13095
13096     sc->link_params.feature_config_flags &=
13097         ~ELINK_FEATURE_CONFIG_BOOT_FROM_SAN;
13098
13099     /* start fast path */
13100
13101     /* Initialize Rx filter */
13102     bxe_set_rx_mode(sc);
13103
13104     /* start the Tx */
13105     switch (/* XXX load_mode */LOAD_OPEN) {
13106     case LOAD_NORMAL:
13107     case LOAD_OPEN:
13108         break;
13109
13110     case LOAD_DIAG:
13111     case LOAD_LOOPBACK_EXT:
13112         sc->state = BXE_STATE_DIAG;
13113         break;
13114
13115     default:
13116         break;
13117     }
13118
13119     if (sc->port.pmf) {
13120         bxe_update_drv_flags(sc, 1 << DRV_FLAGS_PORT_MASK, 0);
13121     } else {
13122         bxe_link_status_update(sc);
13123     }
13124
13125     /* start the periodic timer callout */
13126     bxe_periodic_start(sc);
13127
13128     if (IS_PF(sc) && SHMEM2_HAS(sc, drv_capabilities_flag)) {
13129         /* mark driver is loaded in shmem2 */
13130         val = SHMEM2_RD(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)]);
13131         SHMEM2_WR(sc, drv_capabilities_flag[SC_FW_MB_IDX(sc)],
13132                   (val |
13133                    DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
13134                    DRV_FLAGS_CAPABILITIES_LOADED_L2));
13135     }
13136
13137     /* wait for all pending SP commands to complete */
13138     if (IS_PF(sc) && !bxe_wait_sp_comp(sc, ~0x0UL)) {
13139         BLOGE(sc, "Timeout waiting for all SPs to complete!\n");
13140         bxe_periodic_stop(sc);
13141         bxe_nic_unload(sc, UNLOAD_CLOSE, FALSE);
13142         return (ENXIO);
13143     }
13144
13145 #if 0
13146     /* If PMF - send ADMIN DCBX msg to MFW to initiate DCBX FSM */
13147     if (sc->port.pmf && (sc->state != BXE_STATE_DIAG)) {
13148         bxe_dcbx_init(sc, FALSE);
13149     }
13150 #endif
13151
13152     /* Tell the stack the driver is running! */
13153     sc->ifnet->if_drv_flags = IFF_DRV_RUNNING;
13154
13155     BLOGD(sc, DBG_LOAD, "NIC successfully loaded\n");
13156
13157     return (0);
13158
13159 bxe_nic_load_error3:
13160
13161     if (IS_PF(sc)) {
13162         bxe_int_disable_sync(sc, 1);
13163
13164         /* clean out queued objects */
13165         bxe_squeeze_objects(sc);
13166     }
13167
13168     bxe_interrupt_detach(sc);
13169
13170 bxe_nic_load_error2:
13171
13172     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
13173         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
13174         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE, 0);
13175     }
13176
13177     sc->port.pmf = 0;
13178
13179 bxe_nic_load_error1:
13180
13181     /* clear pf_load status, as it was already set */
13182     if (IS_PF(sc)) {
13183         bxe_clear_pf_load(sc);
13184     }
13185
13186 bxe_nic_load_error0:
13187
13188     bxe_free_fw_stats_mem(sc);
13189     bxe_free_fp_buffers(sc);
13190     bxe_free_mem(sc);
13191
13192     return (rc);
13193 }
13194
13195 static int
13196 bxe_init_locked(struct bxe_softc *sc)
13197 {
13198     int other_engine = SC_PATH(sc) ? 0 : 1;
13199     uint8_t other_load_status, load_status;
13200     uint8_t global = FALSE;
13201     int rc;
13202
13203     BXE_CORE_LOCK_ASSERT(sc);
13204
13205     /* check if the driver is already running */
13206     if (sc->ifnet->if_drv_flags & IFF_DRV_RUNNING) {
13207         BLOGD(sc, DBG_LOAD, "Init called while driver is running!\n");
13208         return (0);
13209     }
13210
13211     bxe_set_power_state(sc, PCI_PM_D0);
13212
13213     /*
13214      * If parity occurred during the unload, then attentions and/or
13215      * RECOVERY_IN_PROGRES may still be set. If so we want the first function
13216      * loaded on the current engine to complete the recovery. Parity recovery
13217      * is only relevant for PF driver.
13218      */
13219     if (IS_PF(sc)) {
13220         other_load_status = bxe_get_load_status(sc, other_engine);
13221         load_status = bxe_get_load_status(sc, SC_PATH(sc));
13222
13223         if (!bxe_reset_is_done(sc, SC_PATH(sc)) ||
13224             bxe_chk_parity_attn(sc, &global, TRUE)) {
13225             do {
13226                 /*
13227                  * If there are attentions and they are in global blocks, set
13228                  * the GLOBAL_RESET bit regardless whether it will be this
13229                  * function that will complete the recovery or not.
13230                  */
13231                 if (global) {
13232                     bxe_set_reset_global(sc);
13233                 }
13234
13235                 /*
13236                  * Only the first function on the current engine should try
13237                  * to recover in open. In case of attentions in global blocks
13238                  * only the first in the chip should try to recover.
13239                  */
13240                 if ((!load_status && (!global || !other_load_status)) &&
13241                     bxe_trylock_leader_lock(sc) && !bxe_leader_reset(sc)) {
13242                     BLOGI(sc, "Recovered during init\n");
13243                     break;
13244                 }
13245
13246                 /* recovery has failed... */
13247                 bxe_set_power_state(sc, PCI_PM_D3hot);
13248                 sc->recovery_state = BXE_RECOVERY_FAILED;
13249
13250                 BLOGE(sc, "Recovery flow hasn't properly "
13251                           "completed yet, try again later. "
13252                           "If you still see this message after a "
13253                           "few retries then power cycle is required.\n");
13254
13255                 rc = ENXIO;
13256                 goto bxe_init_locked_done;
13257             } while (0);
13258         }
13259     }
13260
13261     sc->recovery_state = BXE_RECOVERY_DONE;
13262
13263     rc = bxe_nic_load(sc, LOAD_OPEN);
13264
13265 bxe_init_locked_done:
13266
13267     if (rc) {
13268         /* Tell the stack the driver is NOT running! */
13269         BLOGE(sc, "Initialization failed, "
13270                   "stack notified driver is NOT running!\n");
13271         sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
13272     }
13273
13274     return (rc);
13275 }
13276
13277 static int
13278 bxe_stop_locked(struct bxe_softc *sc)
13279 {
13280     BXE_CORE_LOCK_ASSERT(sc);
13281     return (bxe_nic_unload(sc, UNLOAD_NORMAL, TRUE));
13282 }
13283
13284 /*
13285  * Handles controller initialization when called from an unlocked routine.
13286  * ifconfig calls this function.
13287  *
13288  * Returns:
13289  *   void
13290  */
13291 static void
13292 bxe_init(void *xsc)
13293 {
13294     struct bxe_softc *sc = (struct bxe_softc *)xsc;
13295
13296     BXE_CORE_LOCK(sc);
13297     bxe_init_locked(sc);
13298     BXE_CORE_UNLOCK(sc);
13299 }
13300
13301 static int
13302 bxe_init_ifnet(struct bxe_softc *sc)
13303 {
13304     struct ifnet *ifp;
13305
13306     /* ifconfig entrypoint for media type/status reporting */
13307     ifmedia_init(&sc->ifmedia, IFM_IMASK,
13308                  bxe_ifmedia_update,
13309                  bxe_ifmedia_status);
13310
13311     /* set the default interface values */
13312     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_FDX | sc->media), 0, NULL);
13313     ifmedia_add(&sc->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
13314     ifmedia_set(&sc->ifmedia, (IFM_ETHER | IFM_AUTO));
13315
13316     sc->ifmedia.ifm_media = sc->ifmedia.ifm_cur->ifm_media; /* XXX ? */
13317
13318     /* allocate the ifnet structure */
13319     if ((ifp = if_alloc(IFT_ETHER)) == NULL) {
13320         BLOGE(sc, "Interface allocation failed!\n");
13321         return (ENXIO);
13322     }
13323
13324     ifp->if_softc = sc;
13325     if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
13326     ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
13327     ifp->if_ioctl = bxe_ioctl;
13328     ifp->if_start = bxe_tx_start;
13329 #if __FreeBSD_version >= 800000
13330     ifp->if_transmit = bxe_tx_mq_start;
13331     ifp->if_qflush = bxe_mq_flush;
13332 #endif
13333 #ifdef FreeBSD8_0
13334     ifp->if_timer = 0;
13335 #endif
13336     ifp->if_init = bxe_init;
13337     ifp->if_mtu = sc->mtu;
13338     ifp->if_hwassist = (CSUM_IP       |
13339                         CSUM_TCP      |
13340                         CSUM_UDP      |
13341                         CSUM_TSO      |
13342                         CSUM_TCP_IPV6 |
13343                         CSUM_UDP_IPV6);
13344     ifp->if_capabilities =
13345 #if __FreeBSD_version < 700000
13346         (IFCAP_VLAN_MTU       |
13347          IFCAP_VLAN_HWTAGGING |
13348          IFCAP_HWCSUM         |
13349          IFCAP_JUMBO_MTU      |
13350          IFCAP_LRO);
13351 #else
13352         (IFCAP_VLAN_MTU       |
13353          IFCAP_VLAN_HWTAGGING |
13354          IFCAP_VLAN_HWTSO     |
13355          IFCAP_VLAN_HWFILTER  |
13356          IFCAP_VLAN_HWCSUM    |
13357          IFCAP_HWCSUM         |
13358          IFCAP_JUMBO_MTU      |
13359          IFCAP_LRO            |
13360          IFCAP_TSO4           |
13361          IFCAP_TSO6           |
13362          IFCAP_WOL_MAGIC);
13363 #endif
13364     ifp->if_capenable = ifp->if_capabilities;
13365     ifp->if_capenable &= ~IFCAP_WOL_MAGIC; /* XXX not yet... */
13366 #if __FreeBSD_version < 1000025
13367     ifp->if_baudrate = 1000000000;
13368 #else
13369     if_initbaudrate(ifp, IF_Gbps(10));
13370 #endif
13371     ifp->if_snd.ifq_drv_maxlen = sc->tx_ring_size;
13372
13373     IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
13374     IFQ_SET_READY(&ifp->if_snd);
13375
13376     sc->ifnet = ifp;
13377
13378     /* attach to the Ethernet interface list */
13379     ether_ifattach(ifp, sc->link_params.mac_addr);
13380
13381     return (0);
13382 }
13383
13384 static void
13385 bxe_deallocate_bars(struct bxe_softc *sc)
13386 {
13387     int i;
13388
13389     for (i = 0; i < MAX_BARS; i++) {
13390         if (sc->bar[i].resource != NULL) {
13391             bus_release_resource(sc->dev,
13392                                  SYS_RES_MEMORY,
13393                                  sc->bar[i].rid,
13394                                  sc->bar[i].resource);
13395             BLOGD(sc, DBG_LOAD, "Released PCI BAR%d [%02x] memory\n",
13396                   i, PCIR_BAR(i));
13397         }
13398     }
13399 }
13400
13401 static int
13402 bxe_allocate_bars(struct bxe_softc *sc)
13403 {
13404     u_int flags;
13405     int i;
13406
13407     memset(sc->bar, 0, sizeof(sc->bar));
13408
13409     for (i = 0; i < MAX_BARS; i++) {
13410
13411         /* memory resources reside at BARs 0, 2, 4 */
13412         /* Run `pciconf -lb` to see mappings */
13413         if ((i != 0) && (i != 2) && (i != 4)) {
13414             continue;
13415         }
13416
13417         sc->bar[i].rid = PCIR_BAR(i);
13418
13419         flags = RF_ACTIVE;
13420         if (i == 0) {
13421             flags |= RF_SHAREABLE;
13422         }
13423
13424         if ((sc->bar[i].resource =
13425              bus_alloc_resource_any(sc->dev,
13426                                     SYS_RES_MEMORY,
13427                                     &sc->bar[i].rid,
13428                                     flags)) == NULL) {
13429 #if 0
13430             /* BAR4 doesn't exist for E1 */
13431             BLOGE(sc, "PCI BAR%d [%02x] memory allocation failed\n",
13432                   i, PCIR_BAR(i));
13433 #endif
13434             return (0);
13435         }
13436
13437         sc->bar[i].tag    = rman_get_bustag(sc->bar[i].resource);
13438         sc->bar[i].handle = rman_get_bushandle(sc->bar[i].resource);
13439         sc->bar[i].kva    = (vm_offset_t)rman_get_virtual(sc->bar[i].resource);
13440
13441         BLOGI(sc, "PCI BAR%d [%02x] memory allocated: %p-%p (%ld) -> %p\n",
13442               i, PCIR_BAR(i),
13443               (void *)rman_get_start(sc->bar[i].resource),
13444               (void *)rman_get_end(sc->bar[i].resource),
13445               rman_get_size(sc->bar[i].resource),
13446               (void *)sc->bar[i].kva);
13447     }
13448
13449     return (0);
13450 }
13451
13452 static void
13453 bxe_get_function_num(struct bxe_softc *sc)
13454 {
13455     uint32_t val = 0;
13456
13457     /*
13458      * Read the ME register to get the function number. The ME register
13459      * holds the relative-function number and absolute-function number. The
13460      * absolute-function number appears only in E2 and above. Before that
13461      * these bits always contained zero, therefore we cannot blindly use them.
13462      */
13463
13464     val = REG_RD(sc, BAR_ME_REGISTER);
13465
13466     sc->pfunc_rel =
13467         (uint8_t)((val & ME_REG_PF_NUM) >> ME_REG_PF_NUM_SHIFT);
13468     sc->path_id =
13469         (uint8_t)((val & ME_REG_ABS_PF_NUM) >> ME_REG_ABS_PF_NUM_SHIFT) & 1;
13470
13471     if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13472         sc->pfunc_abs = ((sc->pfunc_rel << 1) | sc->path_id);
13473     } else {
13474         sc->pfunc_abs = (sc->pfunc_rel | sc->path_id);
13475     }
13476
13477     BLOGD(sc, DBG_LOAD,
13478           "Relative function %d, Absolute function %d, Path %d\n",
13479           sc->pfunc_rel, sc->pfunc_abs, sc->path_id);
13480 }
13481
13482 static uint32_t
13483 bxe_get_shmem_mf_cfg_base(struct bxe_softc *sc)
13484 {
13485     uint32_t shmem2_size;
13486     uint32_t offset;
13487     uint32_t mf_cfg_offset_value;
13488
13489     /* Non 57712 */
13490     offset = (SHMEM_RD(sc, func_mb) +
13491               (MAX_FUNC_NUM * sizeof(struct drv_func_mb)));
13492
13493     /* 57712 plus */
13494     if (sc->devinfo.shmem2_base != 0) {
13495         shmem2_size = SHMEM2_RD(sc, size);
13496         if (shmem2_size > offsetof(struct shmem2_region, mf_cfg_addr)) {
13497             mf_cfg_offset_value = SHMEM2_RD(sc, mf_cfg_addr);
13498             if (SHMEM_MF_CFG_ADDR_NONE != mf_cfg_offset_value) {
13499                 offset = mf_cfg_offset_value;
13500             }
13501         }
13502     }
13503
13504     return (offset);
13505 }
13506
13507 static uint32_t
13508 bxe_pcie_capability_read(struct bxe_softc *sc,
13509                          int    reg,
13510                          int    width)
13511 {
13512     int pcie_reg;
13513
13514     /* ensure PCIe capability is enabled */
13515     if (pci_find_cap(sc->dev, PCIY_EXPRESS, &pcie_reg) == 0) {
13516         if (pcie_reg != 0) {
13517             BLOGD(sc, DBG_LOAD, "PCIe capability at 0x%04x\n", pcie_reg);
13518             return (pci_read_config(sc->dev, (pcie_reg + reg), width));
13519         }
13520     }
13521
13522     BLOGE(sc, "PCIe capability NOT FOUND!!!\n");
13523
13524     return (0);
13525 }
13526
13527 static uint8_t
13528 bxe_is_pcie_pending(struct bxe_softc *sc)
13529 {
13530     return (bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_STA, 2) &
13531             PCIM_EXP_STA_TRANSACTION_PND);
13532 }
13533
13534 /*
13535  * Walk the PCI capabiites list for the device to find what features are
13536  * supported. These capabilites may be enabled/disabled by firmware so it's
13537  * best to walk the list rather than make assumptions.
13538  */
13539 static void
13540 bxe_probe_pci_caps(struct bxe_softc *sc)
13541 {
13542     uint16_t link_status;
13543     int reg;
13544
13545     /* check if PCI Power Management is enabled */
13546     if (pci_find_cap(sc->dev, PCIY_PMG, &reg) == 0) {
13547         if (reg != 0) {
13548             BLOGD(sc, DBG_LOAD, "Found PM capability at 0x%04x\n", reg);
13549
13550             sc->devinfo.pcie_cap_flags |= BXE_PM_CAPABLE_FLAG;
13551             sc->devinfo.pcie_pm_cap_reg = (uint16_t)reg;
13552         }
13553     }
13554
13555     link_status = bxe_pcie_capability_read(sc, PCIR_EXPRESS_LINK_STA, 2);
13556
13557     /* handle PCIe 2.0 workarounds for 57710 */
13558     if (CHIP_IS_E1(sc)) {
13559         /* workaround for 57710 errata E4_57710_27462 */
13560         sc->devinfo.pcie_link_speed =
13561             (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
13562
13563         /* workaround for 57710 errata E4_57710_27488 */
13564         sc->devinfo.pcie_link_width =
13565             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13566         if (sc->devinfo.pcie_link_speed > 1) {
13567             sc->devinfo.pcie_link_width =
13568                 ((link_status & PCIM_LINK_STA_WIDTH) >> 4) >> 1;
13569         }
13570     } else {
13571         sc->devinfo.pcie_link_speed =
13572             (link_status & PCIM_LINK_STA_SPEED);
13573         sc->devinfo.pcie_link_width =
13574             ((link_status & PCIM_LINK_STA_WIDTH) >> 4);
13575     }
13576
13577     BLOGD(sc, DBG_LOAD, "PCIe link speed=%d width=%d\n",
13578           sc->devinfo.pcie_link_speed, sc->devinfo.pcie_link_width);
13579
13580     sc->devinfo.pcie_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
13581     sc->devinfo.pcie_pcie_cap_reg = (uint16_t)reg;
13582
13583     /* check if MSI capability is enabled */
13584     if (pci_find_cap(sc->dev, PCIY_MSI, &reg) == 0) {
13585         if (reg != 0) {
13586             BLOGD(sc, DBG_LOAD, "Found MSI capability at 0x%04x\n", reg);
13587
13588             sc->devinfo.pcie_cap_flags |= BXE_MSI_CAPABLE_FLAG;
13589             sc->devinfo.pcie_msi_cap_reg = (uint16_t)reg;
13590         }
13591     }
13592
13593     /* check if MSI-X capability is enabled */
13594     if (pci_find_cap(sc->dev, PCIY_MSIX, &reg) == 0) {
13595         if (reg != 0) {
13596             BLOGD(sc, DBG_LOAD, "Found MSI-X capability at 0x%04x\n", reg);
13597
13598             sc->devinfo.pcie_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
13599             sc->devinfo.pcie_msix_cap_reg = (uint16_t)reg;
13600         }
13601     }
13602 }
13603
13604 static int
13605 bxe_get_shmem_mf_cfg_info_sd(struct bxe_softc *sc)
13606 {
13607     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13608     uint32_t val;
13609
13610     /* get the outer vlan if we're in switch-dependent mode */
13611
13612     val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13613     mf_info->ext_id = (uint16_t)val;
13614
13615     mf_info->multi_vnics_mode = 1;
13616
13617     if (!VALID_OVLAN(mf_info->ext_id)) {
13618         BLOGE(sc, "Invalid VLAN (%d)\n", mf_info->ext_id);
13619         return (1);
13620     }
13621
13622     /* get the capabilities */
13623     if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13624         FUNC_MF_CFG_PROTOCOL_ISCSI) {
13625         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ISCSI;
13626     } else if ((mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_PROTOCOL_MASK) ==
13627                FUNC_MF_CFG_PROTOCOL_FCOE) {
13628         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_FCOE;
13629     } else {
13630         mf_info->mf_protos_supported |= MF_PROTO_SUPPORT_ETHERNET;
13631     }
13632
13633     mf_info->vnics_per_port =
13634         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13635
13636     return (0);
13637 }
13638
13639 static uint32_t
13640 bxe_get_shmem_ext_proto_support_flags(struct bxe_softc *sc)
13641 {
13642     uint32_t retval = 0;
13643     uint32_t val;
13644
13645     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13646
13647     if (val & MACP_FUNC_CFG_FLAGS_ENABLED) {
13648         if (val & MACP_FUNC_CFG_FLAGS_ETHERNET) {
13649             retval |= MF_PROTO_SUPPORT_ETHERNET;
13650         }
13651         if (val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
13652             retval |= MF_PROTO_SUPPORT_ISCSI;
13653         }
13654         if (val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
13655             retval |= MF_PROTO_SUPPORT_FCOE;
13656         }
13657     }
13658
13659     return (retval);
13660 }
13661
13662 static int
13663 bxe_get_shmem_mf_cfg_info_si(struct bxe_softc *sc)
13664 {
13665     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13666     uint32_t val;
13667
13668     /*
13669      * There is no outer vlan if we're in switch-independent mode.
13670      * If the mac is valid then assume multi-function.
13671      */
13672
13673     val = MFCFG_RD(sc, func_ext_config[SC_ABS_FUNC(sc)].func_cfg);
13674
13675     mf_info->multi_vnics_mode = ((val & MACP_FUNC_CFG_FLAGS_MASK) != 0);
13676
13677     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13678
13679     mf_info->vnics_per_port =
13680         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13681
13682     return (0);
13683 }
13684
13685 static int
13686 bxe_get_shmem_mf_cfg_info_niv(struct bxe_softc *sc)
13687 {
13688     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13689     uint32_t e1hov_tag;
13690     uint32_t func_config;
13691     uint32_t niv_config;
13692
13693     mf_info->multi_vnics_mode = 1;
13694
13695     e1hov_tag   = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13696     func_config = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13697     niv_config  = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].afex_config);
13698
13699     mf_info->ext_id =
13700         (uint16_t)((e1hov_tag & FUNC_MF_CFG_E1HOV_TAG_MASK) >>
13701                    FUNC_MF_CFG_E1HOV_TAG_SHIFT);
13702
13703     mf_info->default_vlan =
13704         (uint16_t)((e1hov_tag & FUNC_MF_CFG_AFEX_VLAN_MASK) >>
13705                    FUNC_MF_CFG_AFEX_VLAN_SHIFT);
13706
13707     mf_info->niv_allowed_priorities =
13708         (uint8_t)((niv_config & FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
13709                   FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT);
13710
13711     mf_info->niv_default_cos =
13712         (uint8_t)((func_config & FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
13713                   FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT);
13714
13715     mf_info->afex_vlan_mode =
13716         ((niv_config & FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
13717          FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT);
13718
13719     mf_info->niv_mba_enabled =
13720         ((niv_config & FUNC_MF_CFG_AFEX_MBA_ENABLED_MASK) >>
13721          FUNC_MF_CFG_AFEX_MBA_ENABLED_SHIFT);
13722
13723     mf_info->mf_protos_supported = bxe_get_shmem_ext_proto_support_flags(sc);
13724
13725     mf_info->vnics_per_port =
13726         (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) ? 2 : 4;
13727
13728     return (0);
13729 }
13730
13731 static int
13732 bxe_check_valid_mf_cfg(struct bxe_softc *sc)
13733 {
13734     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13735     uint32_t mf_cfg1;
13736     uint32_t mf_cfg2;
13737     uint32_t ovlan1;
13738     uint32_t ovlan2;
13739     uint8_t i, j;
13740
13741     BLOGD(sc, DBG_LOAD, "MF config parameters for function %d\n",
13742           SC_PORT(sc));
13743     BLOGD(sc, DBG_LOAD, "\tmf_config=0x%x\n",
13744           mf_info->mf_config[SC_VN(sc)]);
13745     BLOGD(sc, DBG_LOAD, "\tmulti_vnics_mode=%d\n",
13746           mf_info->multi_vnics_mode);
13747     BLOGD(sc, DBG_LOAD, "\tvnics_per_port=%d\n",
13748           mf_info->vnics_per_port);
13749     BLOGD(sc, DBG_LOAD, "\tovlan/vifid=%d\n",
13750           mf_info->ext_id);
13751     BLOGD(sc, DBG_LOAD, "\tmin_bw=%d/%d/%d/%d\n",
13752           mf_info->min_bw[0], mf_info->min_bw[1],
13753           mf_info->min_bw[2], mf_info->min_bw[3]);
13754     BLOGD(sc, DBG_LOAD, "\tmax_bw=%d/%d/%d/%d\n",
13755           mf_info->max_bw[0], mf_info->max_bw[1],
13756           mf_info->max_bw[2], mf_info->max_bw[3]);
13757     BLOGD(sc, DBG_LOAD, "\tmac_addr: %s\n",
13758           sc->mac_addr_str);
13759
13760     /* various MF mode sanity checks... */
13761
13762     if (mf_info->mf_config[SC_VN(sc)] & FUNC_MF_CFG_FUNC_HIDE) {
13763         BLOGE(sc, "Enumerated function %d is marked as hidden\n",
13764               SC_PORT(sc));
13765         return (1);
13766     }
13767
13768     if ((mf_info->vnics_per_port > 1) && !mf_info->multi_vnics_mode) {
13769         BLOGE(sc, "vnics_per_port=%d multi_vnics_mode=%d\n",
13770               mf_info->vnics_per_port, mf_info->multi_vnics_mode);
13771         return (1);
13772     }
13773
13774     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13775         /* vnic id > 0 must have valid ovlan in switch-dependent mode */
13776         if ((SC_VN(sc) > 0) && !VALID_OVLAN(OVLAN(sc))) {
13777             BLOGE(sc, "mf_mode=SD vnic_id=%d ovlan=%d\n",
13778                   SC_VN(sc), OVLAN(sc));
13779             return (1);
13780         }
13781
13782         if (!VALID_OVLAN(OVLAN(sc)) && mf_info->multi_vnics_mode) {
13783             BLOGE(sc, "mf_mode=SD multi_vnics_mode=%d ovlan=%d\n",
13784                   mf_info->multi_vnics_mode, OVLAN(sc));
13785             return (1);
13786         }
13787
13788         /*
13789          * Verify all functions are either MF or SF mode. If MF, make sure
13790          * sure that all non-hidden functions have a valid ovlan. If SF,
13791          * make sure that all non-hidden functions have an invalid ovlan.
13792          */
13793         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13794             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13795             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13796             if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13797                 (((mf_info->multi_vnics_mode) && !VALID_OVLAN(ovlan1)) ||
13798                  ((!mf_info->multi_vnics_mode) && VALID_OVLAN(ovlan1)))) {
13799                 BLOGE(sc, "mf_mode=SD function %d MF config "
13800                           "mismatch, multi_vnics_mode=%d ovlan=%d\n",
13801                       i, mf_info->multi_vnics_mode, ovlan1);
13802                 return (1);
13803             }
13804         }
13805
13806         /* Verify all funcs on the same port each have a different ovlan. */
13807         FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13808             mf_cfg1 = MFCFG_RD(sc, func_mf_config[i].config);
13809             ovlan1  = MFCFG_RD(sc, func_mf_config[i].e1hov_tag);
13810             /* iterate from the next function on the port to the max func */
13811             for (j = i + 2; j < MAX_FUNC_NUM; j += 2) {
13812                 mf_cfg2 = MFCFG_RD(sc, func_mf_config[j].config);
13813                 ovlan2  = MFCFG_RD(sc, func_mf_config[j].e1hov_tag);
13814                 if (!(mf_cfg1 & FUNC_MF_CFG_FUNC_HIDE) &&
13815                     VALID_OVLAN(ovlan1) &&
13816                     !(mf_cfg2 & FUNC_MF_CFG_FUNC_HIDE) &&
13817                     VALID_OVLAN(ovlan2) &&
13818                     (ovlan1 == ovlan2)) {
13819                     BLOGE(sc, "mf_mode=SD functions %d and %d "
13820                               "have the same ovlan (%d)\n",
13821                           i, j, ovlan1);
13822                     return (1);
13823                 }
13824             }
13825         }
13826     } /* MULTI_FUNCTION_SD */
13827
13828     return (0);
13829 }
13830
13831 static int
13832 bxe_get_mf_cfg_info(struct bxe_softc *sc)
13833 {
13834     struct bxe_mf_info *mf_info = &sc->devinfo.mf_info;
13835     uint32_t val, mac_upper;
13836     uint8_t i, vnic;
13837
13838     /* initialize mf_info defaults */
13839     mf_info->vnics_per_port   = 1;
13840     mf_info->multi_vnics_mode = FALSE;
13841     mf_info->path_has_ovlan   = FALSE;
13842     mf_info->mf_mode          = SINGLE_FUNCTION;
13843
13844     if (!CHIP_IS_MF_CAP(sc)) {
13845         return (0);
13846     }
13847
13848     if (sc->devinfo.mf_cfg_base == SHMEM_MF_CFG_ADDR_NONE) {
13849         BLOGE(sc, "Invalid mf_cfg_base!\n");
13850         return (1);
13851     }
13852
13853     /* get the MF mode (switch dependent / independent / single-function) */
13854
13855     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
13856
13857     switch (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK)
13858     {
13859     case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
13860
13861         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13862
13863         /* check for legal upper mac bytes */
13864         if (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT) {
13865             mf_info->mf_mode = MULTI_FUNCTION_SI;
13866         } else {
13867             BLOGE(sc, "Invalid config for Switch Independent mode\n");
13868         }
13869
13870         break;
13871
13872     case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
13873     case SHARED_FEAT_CFG_FORCE_SF_MODE_SPIO4:
13874
13875         /* get outer vlan configuration */
13876         val = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].e1hov_tag);
13877
13878         if ((val & FUNC_MF_CFG_E1HOV_TAG_MASK) !=
13879             FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
13880             mf_info->mf_mode = MULTI_FUNCTION_SD;
13881         } else {
13882             BLOGE(sc, "Invalid config for Switch Dependent mode\n");
13883         }
13884
13885         break;
13886
13887     case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
13888
13889         /* not in MF mode, vnics_per_port=1 and multi_vnics_mode=FALSE */
13890         return (0);
13891
13892     case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
13893
13894         /*
13895          * Mark MF mode as NIV if MCP version includes NPAR-SD support
13896          * and the MAC address is valid.
13897          */
13898         mac_upper = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
13899
13900         if ((SHMEM2_HAS(sc, afex_driver_support)) &&
13901             (mac_upper != FUNC_MF_CFG_UPPERMAC_DEFAULT)) {
13902             mf_info->mf_mode = MULTI_FUNCTION_AFEX;
13903         } else {
13904             BLOGE(sc, "Invalid config for AFEX mode\n");
13905         }
13906
13907         break;
13908
13909     default:
13910
13911         BLOGE(sc, "Unknown MF mode (0x%08x)\n",
13912               (val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK));
13913
13914         return (1);
13915     }
13916
13917     /* set path mf_mode (which could be different than function mf_mode) */
13918     if (mf_info->mf_mode == MULTI_FUNCTION_SD) {
13919         mf_info->path_has_ovlan = TRUE;
13920     } else if (mf_info->mf_mode == SINGLE_FUNCTION) {
13921         /*
13922          * Decide on path multi vnics mode. If we're not in MF mode and in
13923          * 4-port mode, this is good enough to check vnic-0 of the other port
13924          * on the same path
13925          */
13926         if (CHIP_PORT_MODE(sc) == CHIP_4_PORT_MODE) {
13927             uint8_t other_port = !(PORT_ID(sc) & 1);
13928             uint8_t abs_func_other_port = (SC_PATH(sc) + (2 * other_port));
13929
13930             val = MFCFG_RD(sc, func_mf_config[abs_func_other_port].e1hov_tag);
13931
13932             mf_info->path_has_ovlan = VALID_OVLAN((uint16_t)val) ? 1 : 0;
13933         }
13934     }
13935
13936     if (mf_info->mf_mode == SINGLE_FUNCTION) {
13937         /* invalid MF config */
13938         if (SC_VN(sc) >= 1) {
13939             BLOGE(sc, "VNIC ID >= 1 in SF mode\n");
13940             return (1);
13941         }
13942
13943         return (0);
13944     }
13945
13946     /* get the MF configuration */
13947     mf_info->mf_config[SC_VN(sc)] =
13948         MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].config);
13949
13950     switch(mf_info->mf_mode)
13951     {
13952     case MULTI_FUNCTION_SD:
13953
13954         bxe_get_shmem_mf_cfg_info_sd(sc);
13955         break;
13956
13957     case MULTI_FUNCTION_SI:
13958
13959         bxe_get_shmem_mf_cfg_info_si(sc);
13960         break;
13961
13962     case MULTI_FUNCTION_AFEX:
13963
13964         bxe_get_shmem_mf_cfg_info_niv(sc);
13965         break;
13966
13967     default:
13968
13969         BLOGE(sc, "Get MF config failed (mf_mode=0x%08x)\n",
13970               mf_info->mf_mode);
13971         return (1);
13972     }
13973
13974     /* get the congestion management parameters */
13975
13976     vnic = 0;
13977     FOREACH_ABS_FUNC_IN_PORT(sc, i) {
13978         /* get min/max bw */
13979         val = MFCFG_RD(sc, func_mf_config[i].config);
13980         mf_info->min_bw[vnic] =
13981             ((val & FUNC_MF_CFG_MIN_BW_MASK) >> FUNC_MF_CFG_MIN_BW_SHIFT);
13982         mf_info->max_bw[vnic] =
13983             ((val & FUNC_MF_CFG_MAX_BW_MASK) >> FUNC_MF_CFG_MAX_BW_SHIFT);
13984         vnic++;
13985     }
13986
13987     return (bxe_check_valid_mf_cfg(sc));
13988 }
13989
13990 static int
13991 bxe_get_shmem_info(struct bxe_softc *sc)
13992 {
13993     int port;
13994     uint32_t mac_hi, mac_lo, val;
13995
13996     port = SC_PORT(sc);
13997     mac_hi = mac_lo = 0;
13998
13999     sc->link_params.sc   = sc;
14000     sc->link_params.port = port;
14001
14002     /* get the hardware config info */
14003     sc->devinfo.hw_config =
14004         SHMEM_RD(sc, dev_info.shared_hw_config.config);
14005     sc->devinfo.hw_config2 =
14006         SHMEM_RD(sc, dev_info.shared_hw_config.config2);
14007
14008     sc->link_params.hw_led_mode =
14009         ((sc->devinfo.hw_config & SHARED_HW_CFG_LED_MODE_MASK) >>
14010          SHARED_HW_CFG_LED_MODE_SHIFT);
14011
14012     /* get the port feature config */
14013     sc->port.config =
14014         SHMEM_RD(sc, dev_info.port_feature_config[port].config),
14015
14016     /* get the link params */
14017     sc->link_params.speed_cap_mask[0] =
14018         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
14019     sc->link_params.speed_cap_mask[1] =
14020         SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask2);
14021
14022     /* get the lane config */
14023     sc->link_params.lane_config =
14024         SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
14025
14026     /* get the link config */
14027     val = SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
14028     sc->port.link_config[ELINK_INT_PHY] = val;
14029     sc->link_params.switch_cfg = (val & PORT_FEATURE_CONNECTED_SWITCH_MASK);
14030     sc->port.link_config[ELINK_EXT_PHY1] =
14031         SHMEM_RD(sc, dev_info.port_feature_config[port].link_config2);
14032
14033     /* get the override preemphasis flag and enable it or turn it off */
14034     val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
14035     if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED) {
14036         sc->link_params.feature_config_flags |=
14037             ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
14038     } else {
14039         sc->link_params.feature_config_flags &=
14040             ~ELINK_FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
14041     }
14042
14043     /* get the initial value of the link params */
14044     sc->link_params.multi_phy_config =
14045         SHMEM_RD(sc, dev_info.port_hw_config[port].multi_phy_config);
14046
14047     /* get external phy info */
14048     sc->port.ext_phy_config =
14049         SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
14050
14051     /* get the multifunction configuration */
14052     bxe_get_mf_cfg_info(sc);
14053
14054     /* get the mac address */
14055     if (IS_MF(sc)) {
14056         mac_hi = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_upper);
14057         mac_lo = MFCFG_RD(sc, func_mf_config[SC_ABS_FUNC(sc)].mac_lower);
14058     } else {
14059         mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
14060         mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
14061     }
14062
14063     if ((mac_lo == 0) && (mac_hi == 0)) {
14064         *sc->mac_addr_str = 0;
14065         BLOGE(sc, "No Ethernet address programmed!\n");
14066     } else {
14067         sc->link_params.mac_addr[0] = (uint8_t)(mac_hi >> 8);
14068         sc->link_params.mac_addr[1] = (uint8_t)(mac_hi);
14069         sc->link_params.mac_addr[2] = (uint8_t)(mac_lo >> 24);
14070         sc->link_params.mac_addr[3] = (uint8_t)(mac_lo >> 16);
14071         sc->link_params.mac_addr[4] = (uint8_t)(mac_lo >> 8);
14072         sc->link_params.mac_addr[5] = (uint8_t)(mac_lo);
14073         snprintf(sc->mac_addr_str, sizeof(sc->mac_addr_str),
14074                  "%02x:%02x:%02x:%02x:%02x:%02x",
14075                  sc->link_params.mac_addr[0], sc->link_params.mac_addr[1],
14076                  sc->link_params.mac_addr[2], sc->link_params.mac_addr[3],
14077                  sc->link_params.mac_addr[4], sc->link_params.mac_addr[5]);
14078         BLOGD(sc, DBG_LOAD, "Ethernet address: %s\n", sc->mac_addr_str);
14079     }
14080
14081 #if 0
14082     if (!IS_MF(sc) &&
14083         ((sc->port.config & PORT_FEAT_CFG_STORAGE_PERSONALITY_MASK) ==
14084          PORT_FEAT_CFG_STORAGE_PERSONALITY_FCOE)) {
14085         sc->flags |= BXE_NO_ISCSI;
14086     }
14087     if (!IS_MF(sc) &&
14088         ((sc->port.config & PORT_FEAT_CFG_STORAGE_PERSONALITY_MASK) ==
14089          PORT_FEAT_CFG_STORAGE_PERSONALITY_ISCSI)) {
14090         sc->flags |= BXE_NO_FCOE_FLAG;
14091     }
14092 #endif
14093
14094     return (0);
14095 }
14096
14097 static void
14098 bxe_get_tunable_params(struct bxe_softc *sc)
14099 {
14100     /* sanity checks */
14101
14102     if ((bxe_interrupt_mode != INTR_MODE_INTX) &&
14103         (bxe_interrupt_mode != INTR_MODE_MSI)  &&
14104         (bxe_interrupt_mode != INTR_MODE_MSIX)) {
14105         BLOGW(sc, "invalid interrupt_mode value (%d)\n", bxe_interrupt_mode);
14106         bxe_interrupt_mode = INTR_MODE_MSIX;
14107     }
14108
14109     if ((bxe_queue_count < 0) || (bxe_queue_count > MAX_RSS_CHAINS)) {
14110         BLOGW(sc, "invalid queue_count value (%d)\n", bxe_queue_count);
14111         bxe_queue_count = 0;
14112     }
14113
14114     if ((bxe_max_rx_bufs < 1) || (bxe_max_rx_bufs > RX_BD_USABLE)) {
14115         if (bxe_max_rx_bufs == 0) {
14116             bxe_max_rx_bufs = RX_BD_USABLE;
14117         } else {
14118             BLOGW(sc, "invalid max_rx_bufs (%d)\n", bxe_max_rx_bufs);
14119             bxe_max_rx_bufs = 2048;
14120         }
14121     }
14122
14123     if ((bxe_hc_rx_ticks < 1) || (bxe_hc_rx_ticks > 100)) {
14124         BLOGW(sc, "invalid hc_rx_ticks (%d)\n", bxe_hc_rx_ticks);
14125         bxe_hc_rx_ticks = 25;
14126     }
14127
14128     if ((bxe_hc_tx_ticks < 1) || (bxe_hc_tx_ticks > 100)) {
14129         BLOGW(sc, "invalid hc_tx_ticks (%d)\n", bxe_hc_tx_ticks);
14130         bxe_hc_tx_ticks = 50;
14131     }
14132
14133     if (bxe_max_aggregation_size == 0) {
14134         bxe_max_aggregation_size = TPA_AGG_SIZE;
14135     }
14136
14137     if (bxe_max_aggregation_size > 0xffff) {
14138         BLOGW(sc, "invalid max_aggregation_size (%d)\n",
14139               bxe_max_aggregation_size);
14140         bxe_max_aggregation_size = TPA_AGG_SIZE;
14141     }
14142
14143     if ((bxe_mrrs < -1) || (bxe_mrrs > 3)) {
14144         BLOGW(sc, "invalid mrrs (%d)\n", bxe_mrrs);
14145         bxe_mrrs = -1;
14146     }
14147
14148     if ((bxe_autogreeen < 0) || (bxe_autogreeen > 2)) {
14149         BLOGW(sc, "invalid autogreeen (%d)\n", bxe_autogreeen);
14150         bxe_autogreeen = 0;
14151     }
14152
14153     if ((bxe_udp_rss < 0) || (bxe_udp_rss > 1)) {
14154         BLOGW(sc, "invalid udp_rss (%d)\n", bxe_udp_rss);
14155         bxe_udp_rss = 0;
14156     }
14157
14158     /* pull in user settings */
14159
14160     sc->interrupt_mode       = bxe_interrupt_mode;
14161     sc->max_rx_bufs          = bxe_max_rx_bufs;
14162     sc->hc_rx_ticks          = bxe_hc_rx_ticks;
14163     sc->hc_tx_ticks          = bxe_hc_tx_ticks;
14164     sc->max_aggregation_size = bxe_max_aggregation_size;
14165     sc->mrrs                 = bxe_mrrs;
14166     sc->autogreeen           = bxe_autogreeen;
14167     sc->udp_rss              = bxe_udp_rss;
14168
14169     if (bxe_interrupt_mode == INTR_MODE_INTX) {
14170         sc->num_queues = 1;
14171     } else { /* INTR_MODE_MSI or INTR_MODE_MSIX */
14172         sc->num_queues =
14173             min((bxe_queue_count ? bxe_queue_count : mp_ncpus),
14174                 MAX_RSS_CHAINS);
14175         if (sc->num_queues > mp_ncpus) {
14176             sc->num_queues = mp_ncpus;
14177         }
14178     }
14179
14180     BLOGD(sc, DBG_LOAD,
14181           "User Config: "
14182           "debug=0x%lx "
14183           "interrupt_mode=%d "
14184           "queue_count=%d "
14185           "hc_rx_ticks=%d "
14186           "hc_tx_ticks=%d "
14187           "rx_budget=%d "
14188           "max_aggregation_size=%d "
14189           "mrrs=%d "
14190           "autogreeen=%d "
14191           "udp_rss=%d\n",
14192           bxe_debug,
14193           sc->interrupt_mode,
14194           sc->num_queues,
14195           sc->hc_rx_ticks,
14196           sc->hc_tx_ticks,
14197           bxe_rx_budget,
14198           sc->max_aggregation_size,
14199           sc->mrrs,
14200           sc->autogreeen,
14201           sc->udp_rss);
14202 }
14203
14204 static void
14205 bxe_media_detect(struct bxe_softc *sc)
14206 {
14207     uint32_t phy_idx = bxe_get_cur_phy_idx(sc);
14208     switch (sc->link_params.phy[phy_idx].media_type) {
14209     case ELINK_ETH_PHY_SFPP_10G_FIBER:
14210     case ELINK_ETH_PHY_XFP_FIBER:
14211         BLOGI(sc, "Found 10Gb Fiber media.\n");
14212         sc->media = IFM_10G_SR;
14213         break;
14214     case ELINK_ETH_PHY_SFP_1G_FIBER:
14215         BLOGI(sc, "Found 1Gb Fiber media.\n");
14216         sc->media = IFM_1000_SX;
14217         break;
14218     case ELINK_ETH_PHY_KR:
14219     case ELINK_ETH_PHY_CX4:
14220         BLOGI(sc, "Found 10GBase-CX4 media.\n");
14221         sc->media = IFM_10G_CX4;
14222         break;
14223     case ELINK_ETH_PHY_DA_TWINAX:
14224         BLOGI(sc, "Found 10Gb Twinax media.\n");
14225         sc->media = IFM_10G_TWINAX;
14226         break;
14227     case ELINK_ETH_PHY_BASE_T:
14228         if (sc->link_params.speed_cap_mask[0] &
14229             PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) {
14230             BLOGI(sc, "Found 10GBase-T media.\n");
14231             sc->media = IFM_10G_T;
14232         } else {
14233             BLOGI(sc, "Found 1000Base-T media.\n");
14234             sc->media = IFM_1000_T;
14235         }
14236         break;
14237     case ELINK_ETH_PHY_NOT_PRESENT:
14238         BLOGI(sc, "Media not present.\n");
14239         sc->media = 0;
14240         break;
14241     case ELINK_ETH_PHY_UNSPECIFIED:
14242     default:
14243         BLOGI(sc, "Unknown media!\n");
14244         sc->media = 0;
14245         break;
14246     }
14247 }
14248
14249 #define GET_FIELD(value, fname)                     \
14250     (((value) & (fname##_MASK)) >> (fname##_SHIFT))
14251 #define IGU_FID(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
14252 #define IGU_VEC(val) GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
14253
14254 static int
14255 bxe_get_igu_cam_info(struct bxe_softc *sc)
14256 {
14257     int pfid = SC_FUNC(sc);
14258     int igu_sb_id;
14259     uint32_t val;
14260     uint8_t fid, igu_sb_cnt = 0;
14261
14262     sc->igu_base_sb = 0xff;
14263
14264     if (CHIP_INT_MODE_IS_BC(sc)) {
14265         int vn = SC_VN(sc);
14266         igu_sb_cnt = sc->igu_sb_cnt;
14267         sc->igu_base_sb = ((CHIP_IS_MODE_4_PORT(sc) ? pfid : vn) *
14268                            FP_SB_MAX_E1x);
14269         sc->igu_dsb_id = (E1HVN_MAX * FP_SB_MAX_E1x +
14270                           (CHIP_IS_MODE_4_PORT(sc) ? pfid : vn));
14271         return (0);
14272     }
14273
14274     /* IGU in normal mode - read CAM */
14275     for (igu_sb_id = 0;
14276          igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
14277          igu_sb_id++) {
14278         val = REG_RD(sc, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
14279         if (!(val & IGU_REG_MAPPING_MEMORY_VALID)) {
14280             continue;
14281         }
14282         fid = IGU_FID(val);
14283         if ((fid & IGU_FID_ENCODE_IS_PF)) {
14284             if ((fid & IGU_FID_PF_NUM_MASK) != pfid) {
14285                 continue;
14286             }
14287             if (IGU_VEC(val) == 0) {
14288                 /* default status block */
14289                 sc->igu_dsb_id = igu_sb_id;
14290             } else {
14291                 if (sc->igu_base_sb == 0xff) {
14292                     sc->igu_base_sb = igu_sb_id;
14293                 }
14294                 igu_sb_cnt++;
14295             }
14296         }
14297     }
14298
14299     /*
14300      * Due to new PF resource allocation by MFW T7.4 and above, it's optional
14301      * that number of CAM entries will not be equal to the value advertised in
14302      * PCI. Driver should use the minimal value of both as the actual status
14303      * block count
14304      */
14305     sc->igu_sb_cnt = min(sc->igu_sb_cnt, igu_sb_cnt);
14306
14307     if (igu_sb_cnt == 0) {
14308         BLOGE(sc, "CAM configuration error\n");
14309         return (-1);
14310     }
14311
14312     return (0);
14313 }
14314
14315 /*
14316  * Gather various information from the device config space, the device itself,
14317  * shmem, and the user input.
14318  */
14319 static int
14320 bxe_get_device_info(struct bxe_softc *sc)
14321 {
14322     uint32_t val;
14323     int rc;
14324
14325     /* Get the data for the device */
14326     sc->devinfo.vendor_id    = pci_get_vendor(sc->dev);
14327     sc->devinfo.device_id    = pci_get_device(sc->dev);
14328     sc->devinfo.subvendor_id = pci_get_subvendor(sc->dev);
14329     sc->devinfo.subdevice_id = pci_get_subdevice(sc->dev);
14330
14331     /* get the chip revision (chip metal comes from pci config space) */
14332     sc->devinfo.chip_id     =
14333     sc->link_params.chip_id =
14334         (((REG_RD(sc, MISC_REG_CHIP_NUM)                   & 0xffff) << 16) |
14335          ((REG_RD(sc, MISC_REG_CHIP_REV)                   & 0xf)    << 12) |
14336          (((REG_RD(sc, PCICFG_OFFSET + PCI_ID_VAL3) >> 24) & 0xf)    << 4)  |
14337          ((REG_RD(sc, MISC_REG_BOND_ID)                    & 0xf)    << 0));
14338
14339     /* force 57811 according to MISC register */
14340     if (REG_RD(sc, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
14341         if (CHIP_IS_57810(sc)) {
14342             sc->devinfo.chip_id = ((CHIP_NUM_57811 << 16) |
14343                                    (sc->devinfo.chip_id & 0x0000ffff));
14344         } else if (CHIP_IS_57810_MF(sc)) {
14345             sc->devinfo.chip_id = ((CHIP_NUM_57811_MF << 16) |
14346                                    (sc->devinfo.chip_id & 0x0000ffff));
14347         }
14348         sc->devinfo.chip_id |= 0x1;
14349     }
14350
14351     BLOGD(sc, DBG_LOAD,
14352           "chip_id=0x%08x (num=0x%04x rev=0x%01x metal=0x%02x bond=0x%01x)\n",
14353           sc->devinfo.chip_id,
14354           ((sc->devinfo.chip_id >> 16) & 0xffff),
14355           ((sc->devinfo.chip_id >> 12) & 0xf),
14356           ((sc->devinfo.chip_id >>  4) & 0xff),
14357           ((sc->devinfo.chip_id >>  0) & 0xf));
14358
14359     val = (REG_RD(sc, 0x2874) & 0x55);
14360     if ((sc->devinfo.chip_id & 0x1) ||
14361         (CHIP_IS_E1(sc) && val) ||
14362         (CHIP_IS_E1H(sc) && (val == 0x55))) {
14363         sc->flags |= BXE_ONE_PORT_FLAG;
14364         BLOGD(sc, DBG_LOAD, "single port device\n");
14365     }
14366
14367     /* set the doorbell size */
14368     sc->doorbell_size = (1 << BXE_DB_SHIFT);
14369
14370     /* determine whether the device is in 2 port or 4 port mode */
14371     sc->devinfo.chip_port_mode = CHIP_PORT_MODE_NONE; /* E1 & E1h*/
14372     if (CHIP_IS_E2E3(sc)) {
14373         /*
14374          * Read port4mode_en_ovwr[0]:
14375          *   If 1, four port mode is in port4mode_en_ovwr[1].
14376          *   If 0, four port mode is in port4mode_en[0].
14377          */
14378         val = REG_RD(sc, MISC_REG_PORT4MODE_EN_OVWR);
14379         if (val & 1) {
14380             val = ((val >> 1) & 1);
14381         } else {
14382             val = REG_RD(sc, MISC_REG_PORT4MODE_EN);
14383         }
14384
14385         sc->devinfo.chip_port_mode =
14386             (val) ? CHIP_4_PORT_MODE : CHIP_2_PORT_MODE;
14387
14388         BLOGD(sc, DBG_LOAD, "Port mode = %s\n", (val) ? "4" : "2");
14389     }
14390
14391     /* get the function and path info for the device */
14392     bxe_get_function_num(sc);
14393
14394     /* get the shared memory base address */
14395     sc->devinfo.shmem_base     =
14396     sc->link_params.shmem_base =
14397         REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
14398     sc->devinfo.shmem2_base =
14399         REG_RD(sc, (SC_PATH(sc) ? MISC_REG_GENERIC_CR_1 :
14400                                   MISC_REG_GENERIC_CR_0));
14401
14402     BLOGD(sc, DBG_LOAD, "shmem_base=0x%08x, shmem2_base=0x%08x\n",
14403           sc->devinfo.shmem_base, sc->devinfo.shmem2_base);
14404
14405     if (!sc->devinfo.shmem_base) {
14406         /* this should ONLY prevent upcoming shmem reads */
14407         BLOGI(sc, "MCP not active\n");
14408         sc->flags |= BXE_NO_MCP_FLAG;
14409         return (0);
14410     }
14411
14412     /* make sure the shared memory contents are valid */
14413     val = SHMEM_RD(sc, validity_map[SC_PORT(sc)]);
14414     if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
14415         (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
14416         BLOGE(sc, "Invalid SHMEM validity signature: 0x%08x\n", val);
14417         return (0);
14418     }
14419     BLOGD(sc, DBG_LOAD, "Valid SHMEM validity signature: 0x%08x\n", val);
14420
14421     /* get the bootcode version */
14422     sc->devinfo.bc_ver = SHMEM_RD(sc, dev_info.bc_rev);
14423     snprintf(sc->devinfo.bc_ver_str,
14424              sizeof(sc->devinfo.bc_ver_str),
14425              "%d.%d.%d",
14426              ((sc->devinfo.bc_ver >> 24) & 0xff),
14427              ((sc->devinfo.bc_ver >> 16) & 0xff),
14428              ((sc->devinfo.bc_ver >>  8) & 0xff));
14429     BLOGD(sc, DBG_LOAD, "Bootcode version: %s\n", sc->devinfo.bc_ver_str);
14430
14431     /* get the bootcode shmem address */
14432     sc->devinfo.mf_cfg_base = bxe_get_shmem_mf_cfg_base(sc);
14433     BLOGD(sc, DBG_LOAD, "mf_cfg_base=0x08%x \n", sc->devinfo.mf_cfg_base);
14434
14435     /* clean indirect addresses as they're not used */
14436     pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, 0, 4);
14437     if (IS_PF(sc)) {
14438         REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0, 0);
14439         REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0, 0);
14440         REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0, 0);
14441         REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0, 0);
14442         if (CHIP_IS_E1x(sc)) {
14443             REG_WR(sc, PXP2_REG_PGL_ADDR_88_F1, 0);
14444             REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F1, 0);
14445             REG_WR(sc, PXP2_REG_PGL_ADDR_90_F1, 0);
14446             REG_WR(sc, PXP2_REG_PGL_ADDR_94_F1, 0);
14447         }
14448
14449         /*
14450          * Enable internal target-read (in case we are probed after PF
14451          * FLR). Must be done prior to any BAR read access. Only for
14452          * 57712 and up
14453          */
14454         if (!CHIP_IS_E1x(sc)) {
14455             REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
14456         }
14457     }
14458
14459     /* get the nvram size */
14460     val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
14461     sc->devinfo.flash_size =
14462         (NVRAM_1MB_SIZE << (val & MCPR_NVM_CFG4_FLASH_SIZE));
14463     BLOGD(sc, DBG_LOAD, "nvram flash size: %d\n", sc->devinfo.flash_size);
14464
14465     /* get PCI capabilites */
14466     bxe_probe_pci_caps(sc);
14467
14468     bxe_set_power_state(sc, PCI_PM_D0);
14469
14470     /* get various configuration parameters from shmem */
14471     bxe_get_shmem_info(sc);
14472
14473     if (sc->devinfo.pcie_msix_cap_reg != 0) {
14474         val = pci_read_config(sc->dev,
14475                               (sc->devinfo.pcie_msix_cap_reg +
14476                                PCIR_MSIX_CTRL),
14477                               2);
14478         sc->igu_sb_cnt = (val & PCIM_MSIXCTRL_TABLE_SIZE);
14479     } else {
14480         sc->igu_sb_cnt = 1;
14481     }
14482
14483     sc->igu_base_addr = BAR_IGU_INTMEM;
14484
14485     /* initialize IGU parameters */
14486     if (CHIP_IS_E1x(sc)) {
14487         sc->devinfo.int_block = INT_BLOCK_HC;
14488         sc->igu_dsb_id = DEF_SB_IGU_ID;
14489         sc->igu_base_sb = 0;
14490     } else {
14491         sc->devinfo.int_block = INT_BLOCK_IGU;
14492
14493         /* do not allow device reset during IGU info preocessing */
14494         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14495
14496         val = REG_RD(sc, IGU_REG_BLOCK_CONFIGURATION);
14497
14498         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14499             int tout = 5000;
14500
14501             BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode\n");
14502
14503             val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
14504             REG_WR(sc, IGU_REG_BLOCK_CONFIGURATION, val);
14505             REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x7f);
14506
14507             while (tout && REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14508                 tout--;
14509                 DELAY(1000);
14510             }
14511
14512             if (REG_RD(sc, IGU_REG_RESET_MEMORIES)) {
14513                 BLOGD(sc, DBG_LOAD, "FORCING IGU Normal Mode failed!!!\n");
14514                 bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14515                 return (-1);
14516             }
14517         }
14518
14519         if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
14520             BLOGD(sc, DBG_LOAD, "IGU Backward Compatible Mode\n");
14521             sc->devinfo.int_block |= INT_BLOCK_MODE_BW_COMP;
14522         } else {
14523             BLOGD(sc, DBG_LOAD, "IGU Normal Mode\n");
14524         }
14525
14526         rc = bxe_get_igu_cam_info(sc);
14527
14528         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
14529
14530         if (rc) {
14531             return (rc);
14532         }
14533     }
14534
14535     /*
14536      * Get base FW non-default (fast path) status block ID. This value is
14537      * used to initialize the fw_sb_id saved on the fp/queue structure to
14538      * determine the id used by the FW.
14539      */
14540     if (CHIP_IS_E1x(sc)) {
14541         sc->base_fw_ndsb = ((SC_PORT(sc) * FP_SB_MAX_E1x) + SC_L_ID(sc));
14542     } else {
14543         /*
14544          * 57712+ - We currently use one FW SB per IGU SB (Rx and Tx of
14545          * the same queue are indicated on the same IGU SB). So we prefer
14546          * FW and IGU SBs to be the same value.
14547          */
14548         sc->base_fw_ndsb = sc->igu_base_sb;
14549     }
14550
14551     BLOGD(sc, DBG_LOAD,
14552           "igu_dsb_id=%d igu_base_sb=%d igu_sb_cnt=%d base_fw_ndsb=%d\n",
14553           sc->igu_dsb_id, sc->igu_base_sb,
14554           sc->igu_sb_cnt, sc->base_fw_ndsb);
14555
14556     elink_phy_probe(&sc->link_params);
14557
14558     return (0);
14559 }
14560
14561 static void
14562 bxe_link_settings_supported(struct bxe_softc *sc,
14563                             uint32_t         switch_cfg)
14564 {
14565     uint32_t cfg_size = 0;
14566     uint32_t idx;
14567     uint8_t port = SC_PORT(sc);
14568
14569     /* aggregation of supported attributes of all external phys */
14570     sc->port.supported[0] = 0;
14571     sc->port.supported[1] = 0;
14572
14573     switch (sc->link_params.num_phys) {
14574     case 1:
14575         sc->port.supported[0] = sc->link_params.phy[ELINK_INT_PHY].supported;
14576         cfg_size = 1;
14577         break;
14578     case 2:
14579         sc->port.supported[0] = sc->link_params.phy[ELINK_EXT_PHY1].supported;
14580         cfg_size = 1;
14581         break;
14582     case 3:
14583         if (sc->link_params.multi_phy_config &
14584             PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
14585             sc->port.supported[1] =
14586                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
14587             sc->port.supported[0] =
14588                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
14589         } else {
14590             sc->port.supported[0] =
14591                 sc->link_params.phy[ELINK_EXT_PHY1].supported;
14592             sc->port.supported[1] =
14593                 sc->link_params.phy[ELINK_EXT_PHY2].supported;
14594         }
14595         cfg_size = 2;
14596         break;
14597     }
14598
14599     if (!(sc->port.supported[0] || sc->port.supported[1])) {
14600         BLOGE(sc, "Invalid phy config in NVRAM (PHY1=0x%08x PHY2=0x%08x)\n",
14601               SHMEM_RD(sc,
14602                        dev_info.port_hw_config[port].external_phy_config),
14603               SHMEM_RD(sc,
14604                        dev_info.port_hw_config[port].external_phy_config2));
14605         return;
14606     }
14607
14608     if (CHIP_IS_E3(sc))
14609         sc->port.phy_addr = REG_RD(sc, MISC_REG_WC0_CTRL_PHY_ADDR);
14610     else {
14611         switch (switch_cfg) {
14612         case ELINK_SWITCH_CFG_1G:
14613             sc->port.phy_addr =
14614                 REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
14615             break;
14616         case ELINK_SWITCH_CFG_10G:
14617             sc->port.phy_addr =
14618                 REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
14619             break;
14620         default:
14621             BLOGE(sc, "Invalid switch config in link_config=0x%08x\n",
14622                   sc->port.link_config[0]);
14623             return;
14624         }
14625     }
14626
14627     BLOGD(sc, DBG_LOAD, "PHY addr 0x%08x\n", sc->port.phy_addr);
14628
14629     /* mask what we support according to speed_cap_mask per configuration */
14630     for (idx = 0; idx < cfg_size; idx++) {
14631         if (!(sc->link_params.speed_cap_mask[idx] &
14632               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF)) {
14633             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Half;
14634         }
14635
14636         if (!(sc->link_params.speed_cap_mask[idx] &
14637               PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL)) {
14638             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10baseT_Full;
14639         }
14640
14641         if (!(sc->link_params.speed_cap_mask[idx] &
14642               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF)) {
14643             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Half;
14644         }
14645
14646         if (!(sc->link_params.speed_cap_mask[idx] &
14647               PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL)) {
14648             sc->port.supported[idx] &= ~ELINK_SUPPORTED_100baseT_Full;
14649         }
14650
14651         if (!(sc->link_params.speed_cap_mask[idx] &
14652               PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) {
14653             sc->port.supported[idx] &= ~ELINK_SUPPORTED_1000baseT_Full;
14654         }
14655
14656         if (!(sc->link_params.speed_cap_mask[idx] &
14657               PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)) {
14658             sc->port.supported[idx] &= ~ELINK_SUPPORTED_2500baseX_Full;
14659         }
14660
14661         if (!(sc->link_params.speed_cap_mask[idx] &
14662               PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
14663             sc->port.supported[idx] &= ~ELINK_SUPPORTED_10000baseT_Full;
14664         }
14665
14666         if (!(sc->link_params.speed_cap_mask[idx] &
14667               PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) {
14668             sc->port.supported[idx] &= ~ELINK_SUPPORTED_20000baseKR2_Full;
14669         }
14670     }
14671
14672     BLOGD(sc, DBG_LOAD, "PHY supported 0=0x%08x 1=0x%08x\n",
14673           sc->port.supported[0], sc->port.supported[1]);
14674 }
14675
14676 static void
14677 bxe_link_settings_requested(struct bxe_softc *sc)
14678 {
14679     uint32_t link_config;
14680     uint32_t idx;
14681     uint32_t cfg_size = 0;
14682
14683     sc->port.advertising[0] = 0;
14684     sc->port.advertising[1] = 0;
14685
14686     switch (sc->link_params.num_phys) {
14687     case 1:
14688     case 2:
14689         cfg_size = 1;
14690         break;
14691     case 3:
14692         cfg_size = 2;
14693         break;
14694     }
14695
14696     for (idx = 0; idx < cfg_size; idx++) {
14697         sc->link_params.req_duplex[idx] = DUPLEX_FULL;
14698         link_config = sc->port.link_config[idx];
14699
14700         switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
14701         case PORT_FEATURE_LINK_SPEED_AUTO:
14702             if (sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg) {
14703                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14704                 sc->port.advertising[idx] |= sc->port.supported[idx];
14705                 if (sc->link_params.phy[ELINK_EXT_PHY1].type ==
14706                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
14707                     sc->port.advertising[idx] |=
14708                         (ELINK_SUPPORTED_100baseT_Half |
14709                          ELINK_SUPPORTED_100baseT_Full);
14710             } else {
14711                 /* force 10G, no AN */
14712                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14713                 sc->port.advertising[idx] |=
14714                     (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
14715                 continue;
14716             }
14717             break;
14718
14719         case PORT_FEATURE_LINK_SPEED_10M_FULL:
14720             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Full) {
14721                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14722                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Full |
14723                                               ADVERTISED_TP);
14724             } else {
14725                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14726                           "speed_cap_mask=0x%08x\n",
14727                       link_config, sc->link_params.speed_cap_mask[idx]);
14728                 return;
14729             }
14730             break;
14731
14732         case PORT_FEATURE_LINK_SPEED_10M_HALF:
14733             if (sc->port.supported[idx] & ELINK_SUPPORTED_10baseT_Half) {
14734                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10;
14735                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14736                 sc->port.advertising[idx] |= (ADVERTISED_10baseT_Half |
14737                                               ADVERTISED_TP);
14738             } else {
14739                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14740                           "speed_cap_mask=0x%08x\n",
14741                       link_config, sc->link_params.speed_cap_mask[idx]);
14742                 return;
14743             }
14744             break;
14745
14746         case PORT_FEATURE_LINK_SPEED_100M_FULL:
14747             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Full) {
14748                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14749                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Full |
14750                                               ADVERTISED_TP);
14751             } else {
14752                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14753                           "speed_cap_mask=0x%08x\n",
14754                       link_config, sc->link_params.speed_cap_mask[idx]);
14755                 return;
14756             }
14757             break;
14758
14759         case PORT_FEATURE_LINK_SPEED_100M_HALF:
14760             if (sc->port.supported[idx] & ELINK_SUPPORTED_100baseT_Half) {
14761                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_100;
14762                 sc->link_params.req_duplex[idx] = DUPLEX_HALF;
14763                 sc->port.advertising[idx] |= (ADVERTISED_100baseT_Half |
14764                                               ADVERTISED_TP);
14765             } else {
14766                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14767                           "speed_cap_mask=0x%08x\n",
14768                       link_config, sc->link_params.speed_cap_mask[idx]);
14769                 return;
14770             }
14771             break;
14772
14773         case PORT_FEATURE_LINK_SPEED_1G:
14774             if (sc->port.supported[idx] & ELINK_SUPPORTED_1000baseT_Full) {
14775                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_1000;
14776                 sc->port.advertising[idx] |= (ADVERTISED_1000baseT_Full |
14777                                               ADVERTISED_TP);
14778             } else {
14779                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14780                           "speed_cap_mask=0x%08x\n",
14781                       link_config, sc->link_params.speed_cap_mask[idx]);
14782                 return;
14783             }
14784             break;
14785
14786         case PORT_FEATURE_LINK_SPEED_2_5G:
14787             if (sc->port.supported[idx] & ELINK_SUPPORTED_2500baseX_Full) {
14788                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_2500;
14789                 sc->port.advertising[idx] |= (ADVERTISED_2500baseX_Full |
14790                                               ADVERTISED_TP);
14791             } else {
14792                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14793                           "speed_cap_mask=0x%08x\n",
14794                       link_config, sc->link_params.speed_cap_mask[idx]);
14795                 return;
14796             }
14797             break;
14798
14799         case PORT_FEATURE_LINK_SPEED_10G_CX4:
14800             if (sc->port.supported[idx] & ELINK_SUPPORTED_10000baseT_Full) {
14801                 sc->link_params.req_line_speed[idx] = ELINK_SPEED_10000;
14802                 sc->port.advertising[idx] |= (ADVERTISED_10000baseT_Full |
14803                                               ADVERTISED_FIBRE);
14804             } else {
14805                 BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14806                           "speed_cap_mask=0x%08x\n",
14807                       link_config, sc->link_params.speed_cap_mask[idx]);
14808                 return;
14809             }
14810             break;
14811
14812         case PORT_FEATURE_LINK_SPEED_20G:
14813             sc->link_params.req_line_speed[idx] = ELINK_SPEED_20000;
14814             break;
14815
14816         default:
14817             BLOGE(sc, "Invalid NVRAM config link_config=0x%08x "
14818                       "speed_cap_mask=0x%08x\n",
14819                   link_config, sc->link_params.speed_cap_mask[idx]);
14820             sc->link_params.req_line_speed[idx] = ELINK_SPEED_AUTO_NEG;
14821             sc->port.advertising[idx] = sc->port.supported[idx];
14822             break;
14823         }
14824
14825         sc->link_params.req_flow_ctrl[idx] =
14826             (link_config & PORT_FEATURE_FLOW_CONTROL_MASK);
14827
14828         if (sc->link_params.req_flow_ctrl[idx] == ELINK_FLOW_CTRL_AUTO) {
14829             if (!(sc->port.supported[idx] & ELINK_SUPPORTED_Autoneg)) {
14830                 sc->link_params.req_flow_ctrl[idx] = ELINK_FLOW_CTRL_NONE;
14831             } else {
14832                 bxe_set_requested_fc(sc);
14833             }
14834         }
14835
14836         BLOGD(sc, DBG_LOAD, "req_line_speed=%d req_duplex=%d "
14837                             "req_flow_ctrl=0x%x advertising=0x%x\n",
14838               sc->link_params.req_line_speed[idx],
14839               sc->link_params.req_duplex[idx],
14840               sc->link_params.req_flow_ctrl[idx],
14841               sc->port.advertising[idx]);
14842     }
14843 }
14844
14845 static void
14846 bxe_get_phy_info(struct bxe_softc *sc)
14847 {
14848     uint8_t port = SC_PORT(sc);
14849     uint32_t config = sc->port.config;
14850     uint32_t eee_mode;
14851
14852     /* shmem data already read in bxe_get_shmem_info() */
14853
14854     BLOGD(sc, DBG_LOAD, "lane_config=0x%08x speed_cap_mask0=0x%08x "
14855                         "link_config0=0x%08x\n",
14856                sc->link_params.lane_config,
14857                sc->link_params.speed_cap_mask[0],
14858                sc->port.link_config[0]);
14859
14860     bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
14861     bxe_link_settings_requested(sc);
14862
14863     if (sc->autogreeen == AUTO_GREEN_FORCE_ON) {
14864         sc->link_params.feature_config_flags |=
14865             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14866     } else if (sc->autogreeen == AUTO_GREEN_FORCE_OFF) {
14867         sc->link_params.feature_config_flags &=
14868             ~ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14869     } else if (config & PORT_FEAT_CFG_AUTOGREEEN_ENABLED) {
14870         sc->link_params.feature_config_flags |=
14871             ELINK_FEATURE_CONFIG_AUTOGREEEN_ENABLED;
14872     }
14873
14874     /* configure link feature according to nvram value */
14875     eee_mode =
14876         (((SHMEM_RD(sc, dev_info.port_feature_config[port].eee_power_mode)) &
14877           PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
14878          PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
14879     if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
14880         sc->link_params.eee_mode = (ELINK_EEE_MODE_ADV_LPI |
14881                                     ELINK_EEE_MODE_ENABLE_LPI |
14882                                     ELINK_EEE_MODE_OUTPUT_TIME);
14883     } else {
14884         sc->link_params.eee_mode = 0;
14885     }
14886
14887     /* get the media type */
14888     bxe_media_detect(sc);
14889 }
14890
14891 static void
14892 bxe_get_params(struct bxe_softc *sc)
14893 {
14894     /* get user tunable params */
14895     bxe_get_tunable_params(sc);
14896
14897     /* select the RX and TX ring sizes */
14898     sc->tx_ring_size = TX_BD_USABLE;
14899     sc->rx_ring_size = RX_BD_USABLE;
14900
14901     /* XXX disable WoL */
14902     sc->wol = 0;
14903 }
14904
14905 static void
14906 bxe_set_modes_bitmap(struct bxe_softc *sc)
14907 {
14908     uint32_t flags = 0;
14909
14910     if (CHIP_REV_IS_FPGA(sc)) {
14911         SET_FLAGS(flags, MODE_FPGA);
14912     } else if (CHIP_REV_IS_EMUL(sc)) {
14913         SET_FLAGS(flags, MODE_EMUL);
14914     } else {
14915         SET_FLAGS(flags, MODE_ASIC);
14916     }
14917
14918     if (CHIP_IS_MODE_4_PORT(sc)) {
14919         SET_FLAGS(flags, MODE_PORT4);
14920     } else {
14921         SET_FLAGS(flags, MODE_PORT2);
14922     }
14923
14924     if (CHIP_IS_E2(sc)) {
14925         SET_FLAGS(flags, MODE_E2);
14926     } else if (CHIP_IS_E3(sc)) {
14927         SET_FLAGS(flags, MODE_E3);
14928         if (CHIP_REV(sc) == CHIP_REV_Ax) {
14929             SET_FLAGS(flags, MODE_E3_A0);
14930         } else /*if (CHIP_REV(sc) == CHIP_REV_Bx)*/ {
14931             SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
14932         }
14933     }
14934
14935     if (IS_MF(sc)) {
14936         SET_FLAGS(flags, MODE_MF);
14937         switch (sc->devinfo.mf_info.mf_mode) {
14938         case MULTI_FUNCTION_SD:
14939             SET_FLAGS(flags, MODE_MF_SD);
14940             break;
14941         case MULTI_FUNCTION_SI:
14942             SET_FLAGS(flags, MODE_MF_SI);
14943             break;
14944         case MULTI_FUNCTION_AFEX:
14945             SET_FLAGS(flags, MODE_MF_AFEX);
14946             break;
14947         }
14948     } else {
14949         SET_FLAGS(flags, MODE_SF);
14950     }
14951
14952 #if defined(__LITTLE_ENDIAN)
14953     SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
14954 #else /* __BIG_ENDIAN */
14955     SET_FLAGS(flags, MODE_BIG_ENDIAN);
14956 #endif
14957
14958     INIT_MODE_FLAGS(sc) = flags;
14959 }
14960
14961 static int
14962 bxe_alloc_hsi_mem(struct bxe_softc *sc)
14963 {
14964     struct bxe_fastpath *fp;
14965     bus_addr_t busaddr;
14966     int max_agg_queues;
14967     int max_segments;
14968     bus_size_t max_size;
14969     bus_size_t max_seg_size;
14970     char buf[32];
14971     int rc;
14972     int i, j;
14973
14974     /* XXX zero out all vars here and call bxe_alloc_hsi_mem on error */
14975
14976     /* allocate the parent bus DMA tag */
14977     rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent tag */
14978                             1,                        /* alignment */
14979                             0,                        /* boundary limit */
14980                             BUS_SPACE_MAXADDR,        /* restricted low */
14981                             BUS_SPACE_MAXADDR,        /* restricted hi */
14982                             NULL,                     /* addr filter() */
14983                             NULL,                     /* addr filter() arg */
14984                             BUS_SPACE_MAXSIZE_32BIT,  /* max map size */
14985                             BUS_SPACE_UNRESTRICTED,   /* num discontinuous */
14986                             BUS_SPACE_MAXSIZE_32BIT,  /* max seg size */
14987                             0,                        /* flags */
14988                             NULL,                     /* lock() */
14989                             NULL,                     /* lock() arg */
14990                             &sc->parent_dma_tag);     /* returned dma tag */
14991     if (rc != 0) {
14992         BLOGE(sc, "Failed to alloc parent DMA tag (%d)!\n", rc);
14993         return (1);
14994     }
14995
14996     /************************/
14997     /* DEFAULT STATUS BLOCK */
14998     /************************/
14999
15000     if (bxe_dma_alloc(sc, sizeof(struct host_sp_status_block),
15001                       &sc->def_sb_dma, "default status block") != 0) {
15002         /* XXX */
15003         bus_dma_tag_destroy(sc->parent_dma_tag);
15004         return (1);
15005     }
15006
15007     sc->def_sb = (struct host_sp_status_block *)sc->def_sb_dma.vaddr;
15008
15009     /***************/
15010     /* EVENT QUEUE */
15011     /***************/
15012
15013     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
15014                       &sc->eq_dma, "event queue") != 0) {
15015         /* XXX */
15016         bxe_dma_free(sc, &sc->def_sb_dma);
15017         sc->def_sb = NULL;
15018         bus_dma_tag_destroy(sc->parent_dma_tag);
15019         return (1);
15020     }
15021
15022     sc->eq = (union event_ring_elem * )sc->eq_dma.vaddr;
15023
15024     /*************/
15025     /* SLOW PATH */
15026     /*************/
15027
15028     if (bxe_dma_alloc(sc, sizeof(struct bxe_slowpath),
15029                       &sc->sp_dma, "slow path") != 0) {
15030         /* XXX */
15031         bxe_dma_free(sc, &sc->eq_dma);
15032         sc->eq = NULL;
15033         bxe_dma_free(sc, &sc->def_sb_dma);
15034         sc->def_sb = NULL;
15035         bus_dma_tag_destroy(sc->parent_dma_tag);
15036         return (1);
15037     }
15038
15039     sc->sp = (struct bxe_slowpath *)sc->sp_dma.vaddr;
15040
15041     /*******************/
15042     /* SLOW PATH QUEUE */
15043     /*******************/
15044
15045     if (bxe_dma_alloc(sc, BCM_PAGE_SIZE,
15046                       &sc->spq_dma, "slow path queue") != 0) {
15047         /* XXX */
15048         bxe_dma_free(sc, &sc->sp_dma);
15049         sc->sp = NULL;
15050         bxe_dma_free(sc, &sc->eq_dma);
15051         sc->eq = NULL;
15052         bxe_dma_free(sc, &sc->def_sb_dma);
15053         sc->def_sb = NULL;
15054         bus_dma_tag_destroy(sc->parent_dma_tag);
15055         return (1);
15056     }
15057
15058     sc->spq = (struct eth_spe *)sc->spq_dma.vaddr;
15059
15060     /***************************/
15061     /* FW DECOMPRESSION BUFFER */
15062     /***************************/
15063
15064     if (bxe_dma_alloc(sc, FW_BUF_SIZE, &sc->gz_buf_dma,
15065                       "fw decompression buffer") != 0) {
15066         /* XXX */
15067         bxe_dma_free(sc, &sc->spq_dma);
15068         sc->spq = NULL;
15069         bxe_dma_free(sc, &sc->sp_dma);
15070         sc->sp = NULL;
15071         bxe_dma_free(sc, &sc->eq_dma);
15072         sc->eq = NULL;
15073         bxe_dma_free(sc, &sc->def_sb_dma);
15074         sc->def_sb = NULL;
15075         bus_dma_tag_destroy(sc->parent_dma_tag);
15076         return (1);
15077     }
15078
15079     sc->gz_buf = (void *)sc->gz_buf_dma.vaddr;
15080
15081     if ((sc->gz_strm =
15082          malloc(sizeof(*sc->gz_strm), M_DEVBUF, M_NOWAIT)) == NULL) {
15083         /* XXX */
15084         bxe_dma_free(sc, &sc->gz_buf_dma);
15085         sc->gz_buf = NULL;
15086         bxe_dma_free(sc, &sc->spq_dma);
15087         sc->spq = NULL;
15088         bxe_dma_free(sc, &sc->sp_dma);
15089         sc->sp = NULL;
15090         bxe_dma_free(sc, &sc->eq_dma);
15091         sc->eq = NULL;
15092         bxe_dma_free(sc, &sc->def_sb_dma);
15093         sc->def_sb = NULL;
15094         bus_dma_tag_destroy(sc->parent_dma_tag);
15095         return (1);
15096     }
15097
15098     /*************/
15099     /* FASTPATHS */
15100     /*************/
15101
15102     /* allocate DMA memory for each fastpath structure */
15103     for (i = 0; i < sc->num_queues; i++) {
15104         fp = &sc->fp[i];
15105         fp->sc    = sc;
15106         fp->index = i;
15107
15108         /*******************/
15109         /* FP STATUS BLOCK */
15110         /*******************/
15111
15112         snprintf(buf, sizeof(buf), "fp %d status block", i);
15113         if (bxe_dma_alloc(sc, sizeof(union bxe_host_hc_status_block),
15114                           &fp->sb_dma, buf) != 0) {
15115             /* XXX unwind and free previous fastpath allocations */
15116             BLOGE(sc, "Failed to alloc %s\n", buf);
15117             return (1);
15118         } else {
15119             if (CHIP_IS_E2E3(sc)) {
15120                 fp->status_block.e2_sb =
15121                     (struct host_hc_status_block_e2 *)fp->sb_dma.vaddr;
15122             } else {
15123                 fp->status_block.e1x_sb =
15124                     (struct host_hc_status_block_e1x *)fp->sb_dma.vaddr;
15125             }
15126         }
15127
15128         /******************/
15129         /* FP TX BD CHAIN */
15130         /******************/
15131
15132         snprintf(buf, sizeof(buf), "fp %d tx bd chain", i);
15133         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * TX_BD_NUM_PAGES),
15134                           &fp->tx_dma, buf) != 0) {
15135             /* XXX unwind and free previous fastpath allocations */
15136             BLOGE(sc, "Failed to alloc %s\n", buf);
15137             return (1);
15138         } else {
15139             fp->tx_chain = (union eth_tx_bd_types *)fp->tx_dma.vaddr;
15140         }
15141
15142         /* link together the tx bd chain pages */
15143         for (j = 1; j <= TX_BD_NUM_PAGES; j++) {
15144             /* index into the tx bd chain array to last entry per page */
15145             struct eth_tx_next_bd *tx_next_bd =
15146                 &fp->tx_chain[TX_BD_TOTAL_PER_PAGE * j - 1].next_bd;
15147             /* point to the next page and wrap from last page */
15148             busaddr = (fp->tx_dma.paddr +
15149                        (BCM_PAGE_SIZE * (j % TX_BD_NUM_PAGES)));
15150             tx_next_bd->addr_hi = htole32(U64_HI(busaddr));
15151             tx_next_bd->addr_lo = htole32(U64_LO(busaddr));
15152         }
15153
15154         /******************/
15155         /* FP RX BD CHAIN */
15156         /******************/
15157
15158         snprintf(buf, sizeof(buf), "fp %d rx bd chain", i);
15159         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_BD_NUM_PAGES),
15160                           &fp->rx_dma, buf) != 0) {
15161             /* XXX unwind and free previous fastpath allocations */
15162             BLOGE(sc, "Failed to alloc %s\n", buf);
15163             return (1);
15164         } else {
15165             fp->rx_chain = (struct eth_rx_bd *)fp->rx_dma.vaddr;
15166         }
15167
15168         /* link together the rx bd chain pages */
15169         for (j = 1; j <= RX_BD_NUM_PAGES; j++) {
15170             /* index into the rx bd chain array to last entry per page */
15171             struct eth_rx_bd *rx_bd =
15172                 &fp->rx_chain[RX_BD_TOTAL_PER_PAGE * j - 2];
15173             /* point to the next page and wrap from last page */
15174             busaddr = (fp->rx_dma.paddr +
15175                        (BCM_PAGE_SIZE * (j % RX_BD_NUM_PAGES)));
15176             rx_bd->addr_hi = htole32(U64_HI(busaddr));
15177             rx_bd->addr_lo = htole32(U64_LO(busaddr));
15178         }
15179
15180         /*******************/
15181         /* FP RX RCQ CHAIN */
15182         /*******************/
15183
15184         snprintf(buf, sizeof(buf), "fp %d rcq chain", i);
15185         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RCQ_NUM_PAGES),
15186                           &fp->rcq_dma, buf) != 0) {
15187             /* XXX unwind and free previous fastpath allocations */
15188             BLOGE(sc, "Failed to alloc %s\n", buf);
15189             return (1);
15190         } else {
15191             fp->rcq_chain = (union eth_rx_cqe *)fp->rcq_dma.vaddr;
15192         }
15193
15194         /* link together the rcq chain pages */
15195         for (j = 1; j <= RCQ_NUM_PAGES; j++) {
15196             /* index into the rcq chain array to last entry per page */
15197             struct eth_rx_cqe_next_page *rx_cqe_next =
15198                 (struct eth_rx_cqe_next_page *)
15199                 &fp->rcq_chain[RCQ_TOTAL_PER_PAGE * j - 1];
15200             /* point to the next page and wrap from last page */
15201             busaddr = (fp->rcq_dma.paddr +
15202                        (BCM_PAGE_SIZE * (j % RCQ_NUM_PAGES)));
15203             rx_cqe_next->addr_hi = htole32(U64_HI(busaddr));
15204             rx_cqe_next->addr_lo = htole32(U64_LO(busaddr));
15205         }
15206
15207         /*******************/
15208         /* FP RX SGE CHAIN */
15209         /*******************/
15210
15211         snprintf(buf, sizeof(buf), "fp %d sge chain", i);
15212         if (bxe_dma_alloc(sc, (BCM_PAGE_SIZE * RX_SGE_NUM_PAGES),
15213                           &fp->rx_sge_dma, buf) != 0) {
15214             /* XXX unwind and free previous fastpath allocations */
15215             BLOGE(sc, "Failed to alloc %s\n", buf);
15216             return (1);
15217         } else {
15218             fp->rx_sge_chain = (struct eth_rx_sge *)fp->rx_sge_dma.vaddr;
15219         }
15220
15221         /* link together the sge chain pages */
15222         for (j = 1; j <= RX_SGE_NUM_PAGES; j++) {
15223             /* index into the rcq chain array to last entry per page */
15224             struct eth_rx_sge *rx_sge =
15225                 &fp->rx_sge_chain[RX_SGE_TOTAL_PER_PAGE * j - 2];
15226             /* point to the next page and wrap from last page */
15227             busaddr = (fp->rx_sge_dma.paddr +
15228                        (BCM_PAGE_SIZE * (j % RX_SGE_NUM_PAGES)));
15229             rx_sge->addr_hi = htole32(U64_HI(busaddr));
15230             rx_sge->addr_lo = htole32(U64_LO(busaddr));
15231         }
15232
15233         /***********************/
15234         /* FP TX MBUF DMA MAPS */
15235         /***********************/
15236
15237         /* set required sizes before mapping to conserve resources */
15238         if (sc->ifnet->if_capenable & (IFCAP_TSO4 | IFCAP_TSO6)) {
15239             max_size     = BXE_TSO_MAX_SIZE;
15240             max_segments = BXE_TSO_MAX_SEGMENTS;
15241             max_seg_size = BXE_TSO_MAX_SEG_SIZE;
15242         } else {
15243             max_size     = (MCLBYTES * BXE_MAX_SEGMENTS);
15244             max_segments = BXE_MAX_SEGMENTS;
15245             max_seg_size = MCLBYTES;
15246         }
15247
15248         /* create a dma tag for the tx mbufs */
15249         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15250                                 1,                  /* alignment */
15251                                 0,                  /* boundary limit */
15252                                 BUS_SPACE_MAXADDR,  /* restricted low */
15253                                 BUS_SPACE_MAXADDR,  /* restricted hi */
15254                                 NULL,               /* addr filter() */
15255                                 NULL,               /* addr filter() arg */
15256                                 max_size,           /* max map size */
15257                                 max_segments,       /* num discontinuous */
15258                                 max_seg_size,       /* max seg size */
15259                                 0,                  /* flags */
15260                                 NULL,               /* lock() */
15261                                 NULL,               /* lock() arg */
15262                                 &fp->tx_mbuf_tag);  /* returned dma tag */
15263         if (rc != 0) {
15264             /* XXX unwind and free previous fastpath allocations */
15265             BLOGE(sc, "Failed to create dma tag for "
15266                       "'fp %d tx mbufs' (%d)\n",
15267                   i, rc);
15268             return (1);
15269         }
15270
15271         /* create dma maps for each of the tx mbuf clusters */
15272         for (j = 0; j < TX_BD_TOTAL; j++) {
15273             if (bus_dmamap_create(fp->tx_mbuf_tag,
15274                                   BUS_DMA_NOWAIT,
15275                                   &fp->tx_mbuf_chain[j].m_map)) {
15276                 /* XXX unwind and free previous fastpath allocations */
15277                 BLOGE(sc, "Failed to create dma map for "
15278                           "'fp %d tx mbuf %d' (%d)\n",
15279                       i, j, rc);
15280                 return (1);
15281             }
15282         }
15283
15284         /***********************/
15285         /* FP RX MBUF DMA MAPS */
15286         /***********************/
15287
15288         /* create a dma tag for the rx mbufs */
15289         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15290                                 1,                  /* alignment */
15291                                 0,                  /* boundary limit */
15292                                 BUS_SPACE_MAXADDR,  /* restricted low */
15293                                 BUS_SPACE_MAXADDR,  /* restricted hi */
15294                                 NULL,               /* addr filter() */
15295                                 NULL,               /* addr filter() arg */
15296                                 MJUM9BYTES,         /* max map size */
15297                                 1,                  /* num discontinuous */
15298                                 MJUM9BYTES,         /* max seg size */
15299                                 0,                  /* flags */
15300                                 NULL,               /* lock() */
15301                                 NULL,               /* lock() arg */
15302                                 &fp->rx_mbuf_tag);  /* returned dma tag */
15303         if (rc != 0) {
15304             /* XXX unwind and free previous fastpath allocations */
15305             BLOGE(sc, "Failed to create dma tag for "
15306                       "'fp %d rx mbufs' (%d)\n",
15307                   i, rc);
15308             return (1);
15309         }
15310
15311         /* create dma maps for each of the rx mbuf clusters */
15312         for (j = 0; j < RX_BD_TOTAL; j++) {
15313             if (bus_dmamap_create(fp->rx_mbuf_tag,
15314                                   BUS_DMA_NOWAIT,
15315                                   &fp->rx_mbuf_chain[j].m_map)) {
15316                 /* XXX unwind and free previous fastpath allocations */
15317                 BLOGE(sc, "Failed to create dma map for "
15318                           "'fp %d rx mbuf %d' (%d)\n",
15319                       i, j, rc);
15320                 return (1);
15321             }
15322         }
15323
15324         /* create dma map for the spare rx mbuf cluster */
15325         if (bus_dmamap_create(fp->rx_mbuf_tag,
15326                               BUS_DMA_NOWAIT,
15327                               &fp->rx_mbuf_spare_map)) {
15328             /* XXX unwind and free previous fastpath allocations */
15329             BLOGE(sc, "Failed to create dma map for "
15330                       "'fp %d spare rx mbuf' (%d)\n",
15331                   i, rc);
15332             return (1);
15333         }
15334
15335         /***************************/
15336         /* FP RX SGE MBUF DMA MAPS */
15337         /***************************/
15338
15339         /* create a dma tag for the rx sge mbufs */
15340         rc = bus_dma_tag_create(sc->parent_dma_tag, /* parent tag */
15341                                 1,                  /* alignment */
15342                                 0,                  /* boundary limit */
15343                                 BUS_SPACE_MAXADDR,  /* restricted low */
15344                                 BUS_SPACE_MAXADDR,  /* restricted hi */
15345                                 NULL,               /* addr filter() */
15346                                 NULL,               /* addr filter() arg */
15347                                 BCM_PAGE_SIZE,      /* max map size */
15348                                 1,                  /* num discontinuous */
15349                                 BCM_PAGE_SIZE,      /* max seg size */
15350                                 0,                  /* flags */
15351                                 NULL,               /* lock() */
15352                                 NULL,               /* lock() arg */
15353                                 &fp->rx_sge_mbuf_tag); /* returned dma tag */
15354         if (rc != 0) {
15355             /* XXX unwind and free previous fastpath allocations */
15356             BLOGE(sc, "Failed to create dma tag for "
15357                       "'fp %d rx sge mbufs' (%d)\n",
15358                   i, rc);
15359             return (1);
15360         }
15361
15362         /* create dma maps for the rx sge mbuf clusters */
15363         for (j = 0; j < RX_SGE_TOTAL; j++) {
15364             if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15365                                   BUS_DMA_NOWAIT,
15366                                   &fp->rx_sge_mbuf_chain[j].m_map)) {
15367                 /* XXX unwind and free previous fastpath allocations */
15368                 BLOGE(sc, "Failed to create dma map for "
15369                           "'fp %d rx sge mbuf %d' (%d)\n",
15370                       i, j, rc);
15371                 return (1);
15372             }
15373         }
15374
15375         /* create dma map for the spare rx sge mbuf cluster */
15376         if (bus_dmamap_create(fp->rx_sge_mbuf_tag,
15377                               BUS_DMA_NOWAIT,
15378                               &fp->rx_sge_mbuf_spare_map)) {
15379             /* XXX unwind and free previous fastpath allocations */
15380             BLOGE(sc, "Failed to create dma map for "
15381                       "'fp %d spare rx sge mbuf' (%d)\n",
15382                   i, rc);
15383             return (1);
15384         }
15385
15386         /***************************/
15387         /* FP RX TPA MBUF DMA MAPS */
15388         /***************************/
15389
15390         /* create dma maps for the rx tpa mbuf clusters */
15391         max_agg_queues = MAX_AGG_QS(sc);
15392
15393         for (j = 0; j < max_agg_queues; j++) {
15394             if (bus_dmamap_create(fp->rx_mbuf_tag,
15395                                   BUS_DMA_NOWAIT,
15396                                   &fp->rx_tpa_info[j].bd.m_map)) {
15397                 /* XXX unwind and free previous fastpath allocations */
15398                 BLOGE(sc, "Failed to create dma map for "
15399                           "'fp %d rx tpa mbuf %d' (%d)\n",
15400                       i, j, rc);
15401                 return (1);
15402             }
15403         }
15404
15405         /* create dma map for the spare rx tpa mbuf cluster */
15406         if (bus_dmamap_create(fp->rx_mbuf_tag,
15407                               BUS_DMA_NOWAIT,
15408                               &fp->rx_tpa_info_mbuf_spare_map)) {
15409             /* XXX unwind and free previous fastpath allocations */
15410             BLOGE(sc, "Failed to create dma map for "
15411                       "'fp %d spare rx tpa mbuf' (%d)\n",
15412                   i, rc);
15413             return (1);
15414         }
15415
15416         bxe_init_sge_ring_bit_mask(fp);
15417     }
15418
15419     return (0);
15420 }
15421
15422 static void
15423 bxe_free_hsi_mem(struct bxe_softc *sc)
15424 {
15425     struct bxe_fastpath *fp;
15426     int max_agg_queues;
15427     int i, j;
15428
15429     if (sc->parent_dma_tag == NULL) {
15430         return; /* assume nothing was allocated */
15431     }
15432
15433     for (i = 0; i < sc->num_queues; i++) {
15434         fp = &sc->fp[i];
15435
15436         /*******************/
15437         /* FP STATUS BLOCK */
15438         /*******************/
15439
15440         bxe_dma_free(sc, &fp->sb_dma);
15441         memset(&fp->status_block, 0, sizeof(fp->status_block));
15442
15443         /******************/
15444         /* FP TX BD CHAIN */
15445         /******************/
15446
15447         bxe_dma_free(sc, &fp->tx_dma);
15448         fp->tx_chain = NULL;
15449
15450         /******************/
15451         /* FP RX BD CHAIN */
15452         /******************/
15453
15454         bxe_dma_free(sc, &fp->rx_dma);
15455         fp->rx_chain = NULL;
15456
15457         /*******************/
15458         /* FP RX RCQ CHAIN */
15459         /*******************/
15460
15461         bxe_dma_free(sc, &fp->rcq_dma);
15462         fp->rcq_chain = NULL;
15463
15464         /*******************/
15465         /* FP RX SGE CHAIN */
15466         /*******************/
15467
15468         bxe_dma_free(sc, &fp->rx_sge_dma);
15469         fp->rx_sge_chain = NULL;
15470
15471         /***********************/
15472         /* FP TX MBUF DMA MAPS */
15473         /***********************/
15474
15475         if (fp->tx_mbuf_tag != NULL) {
15476             for (j = 0; j < TX_BD_TOTAL; j++) {
15477                 if (fp->tx_mbuf_chain[j].m_map != NULL) {
15478                     bus_dmamap_unload(fp->tx_mbuf_tag,
15479                                       fp->tx_mbuf_chain[j].m_map);
15480                     bus_dmamap_destroy(fp->tx_mbuf_tag,
15481                                        fp->tx_mbuf_chain[j].m_map);
15482                 }
15483             }
15484
15485             bus_dma_tag_destroy(fp->tx_mbuf_tag);
15486             fp->tx_mbuf_tag = NULL;
15487         }
15488
15489         /***********************/
15490         /* FP RX MBUF DMA MAPS */
15491         /***********************/
15492
15493         if (fp->rx_mbuf_tag != NULL) {
15494             for (j = 0; j < RX_BD_TOTAL; j++) {
15495                 if (fp->rx_mbuf_chain[j].m_map != NULL) {
15496                     bus_dmamap_unload(fp->rx_mbuf_tag,
15497                                       fp->rx_mbuf_chain[j].m_map);
15498                     bus_dmamap_destroy(fp->rx_mbuf_tag,
15499                                        fp->rx_mbuf_chain[j].m_map);
15500                 }
15501             }
15502
15503             if (fp->rx_mbuf_spare_map != NULL) {
15504                 bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15505                 bus_dmamap_destroy(fp->rx_mbuf_tag, fp->rx_mbuf_spare_map);
15506             }
15507
15508             /***************************/
15509             /* FP RX TPA MBUF DMA MAPS */
15510             /***************************/
15511
15512             max_agg_queues = MAX_AGG_QS(sc);
15513
15514             for (j = 0; j < max_agg_queues; j++) {
15515                 if (fp->rx_tpa_info[j].bd.m_map != NULL) {
15516                     bus_dmamap_unload(fp->rx_mbuf_tag,
15517                                       fp->rx_tpa_info[j].bd.m_map);
15518                     bus_dmamap_destroy(fp->rx_mbuf_tag,
15519                                        fp->rx_tpa_info[j].bd.m_map);
15520                 }
15521             }
15522
15523             if (fp->rx_tpa_info_mbuf_spare_map != NULL) {
15524                 bus_dmamap_unload(fp->rx_mbuf_tag,
15525                                   fp->rx_tpa_info_mbuf_spare_map);
15526                 bus_dmamap_destroy(fp->rx_mbuf_tag,
15527                                    fp->rx_tpa_info_mbuf_spare_map);
15528             }
15529
15530             bus_dma_tag_destroy(fp->rx_mbuf_tag);
15531             fp->rx_mbuf_tag = NULL;
15532         }
15533
15534         /***************************/
15535         /* FP RX SGE MBUF DMA MAPS */
15536         /***************************/
15537
15538         if (fp->rx_sge_mbuf_tag != NULL) {
15539             for (j = 0; j < RX_SGE_TOTAL; j++) {
15540                 if (fp->rx_sge_mbuf_chain[j].m_map != NULL) {
15541                     bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15542                                       fp->rx_sge_mbuf_chain[j].m_map);
15543                     bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15544                                        fp->rx_sge_mbuf_chain[j].m_map);
15545                 }
15546             }
15547
15548             if (fp->rx_sge_mbuf_spare_map != NULL) {
15549                 bus_dmamap_unload(fp->rx_sge_mbuf_tag,
15550                                   fp->rx_sge_mbuf_spare_map);
15551                 bus_dmamap_destroy(fp->rx_sge_mbuf_tag,
15552                                    fp->rx_sge_mbuf_spare_map);
15553             }
15554
15555             bus_dma_tag_destroy(fp->rx_sge_mbuf_tag);
15556             fp->rx_sge_mbuf_tag = NULL;
15557         }
15558     }
15559
15560     /***************************/
15561     /* FW DECOMPRESSION BUFFER */
15562     /***************************/
15563
15564     bxe_dma_free(sc, &sc->gz_buf_dma);
15565     sc->gz_buf = NULL;
15566     free(sc->gz_strm, M_DEVBUF);
15567     sc->gz_strm = NULL;
15568
15569     /*******************/
15570     /* SLOW PATH QUEUE */
15571     /*******************/
15572
15573     bxe_dma_free(sc, &sc->spq_dma);
15574     sc->spq = NULL;
15575
15576     /*************/
15577     /* SLOW PATH */
15578     /*************/
15579
15580     bxe_dma_free(sc, &sc->sp_dma);
15581     sc->sp = NULL;
15582
15583     /***************/
15584     /* EVENT QUEUE */
15585     /***************/
15586
15587     bxe_dma_free(sc, &sc->eq_dma);
15588     sc->eq = NULL;
15589
15590     /************************/
15591     /* DEFAULT STATUS BLOCK */
15592     /************************/
15593
15594     bxe_dma_free(sc, &sc->def_sb_dma);
15595     sc->def_sb = NULL;
15596
15597     bus_dma_tag_destroy(sc->parent_dma_tag);
15598     sc->parent_dma_tag = NULL;
15599 }
15600
15601 /*
15602  * Previous driver DMAE transaction may have occurred when pre-boot stage
15603  * ended and boot began. This would invalidate the addresses of the
15604  * transaction, resulting in was-error bit set in the PCI causing all
15605  * hw-to-host PCIe transactions to timeout. If this happened we want to clear
15606  * the interrupt which detected this from the pglueb and the was-done bit
15607  */
15608 static void
15609 bxe_prev_interrupted_dmae(struct bxe_softc *sc)
15610 {
15611     uint32_t val;
15612
15613     if (!CHIP_IS_E1x(sc)) {
15614         val = REG_RD(sc, PGLUE_B_REG_PGLUE_B_INT_STS);
15615         if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN) {
15616             BLOGD(sc, DBG_LOAD,
15617                   "Clearing 'was-error' bit that was set in pglueb");
15618             REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, 1 << SC_FUNC(sc));
15619         }
15620     }
15621 }
15622
15623 static int
15624 bxe_prev_mcp_done(struct bxe_softc *sc)
15625 {
15626     uint32_t rc = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE,
15627                                  DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
15628     if (!rc) {
15629         BLOGE(sc, "MCP response failure, aborting\n");
15630         return (-1);
15631     }
15632
15633     return (0);
15634 }
15635
15636 static struct bxe_prev_list_node *
15637 bxe_prev_path_get_entry(struct bxe_softc *sc)
15638 {
15639     struct bxe_prev_list_node *tmp;
15640
15641     LIST_FOREACH(tmp, &bxe_prev_list, node) {
15642         if ((sc->pcie_bus == tmp->bus) &&
15643             (sc->pcie_device == tmp->slot) &&
15644             (SC_PATH(sc) == tmp->path)) {
15645             return (tmp);
15646         }
15647     }
15648
15649     return (NULL);
15650 }
15651
15652 static uint8_t
15653 bxe_prev_is_path_marked(struct bxe_softc *sc)
15654 {
15655     struct bxe_prev_list_node *tmp;
15656     int rc = FALSE;
15657
15658     mtx_lock(&bxe_prev_mtx);
15659
15660     tmp = bxe_prev_path_get_entry(sc);
15661     if (tmp) {
15662         if (tmp->aer) {
15663             BLOGD(sc, DBG_LOAD,
15664                   "Path %d/%d/%d was marked by AER\n",
15665                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15666         } else {
15667             rc = TRUE;
15668             BLOGD(sc, DBG_LOAD,
15669                   "Path %d/%d/%d was already cleaned from previous drivers\n",
15670                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15671         }
15672     }
15673
15674     mtx_unlock(&bxe_prev_mtx);
15675
15676     return (rc);
15677 }
15678
15679 static int
15680 bxe_prev_mark_path(struct bxe_softc *sc,
15681                    uint8_t          after_undi)
15682 {
15683     struct bxe_prev_list_node *tmp;
15684
15685     mtx_lock(&bxe_prev_mtx);
15686
15687     /* Check whether the entry for this path already exists */
15688     tmp = bxe_prev_path_get_entry(sc);
15689     if (tmp) {
15690         if (!tmp->aer) {
15691             BLOGD(sc, DBG_LOAD,
15692                   "Re-marking AER in path %d/%d/%d\n",
15693                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15694         } else {
15695             BLOGD(sc, DBG_LOAD,
15696                   "Removing AER indication from path %d/%d/%d\n",
15697                   sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15698             tmp->aer = 0;
15699         }
15700
15701         mtx_unlock(&bxe_prev_mtx);
15702         return (0);
15703     }
15704
15705     mtx_unlock(&bxe_prev_mtx);
15706
15707     /* Create an entry for this path and add it */
15708     tmp = malloc(sizeof(struct bxe_prev_list_node), M_DEVBUF,
15709                  (M_NOWAIT | M_ZERO));
15710     if (!tmp) {
15711         BLOGE(sc, "Failed to allocate 'bxe_prev_list_node'\n");
15712         return (-1);
15713     }
15714
15715     tmp->bus  = sc->pcie_bus;
15716     tmp->slot = sc->pcie_device;
15717     tmp->path = SC_PATH(sc);
15718     tmp->aer  = 0;
15719     tmp->undi = after_undi ? (1 << SC_PORT(sc)) : 0;
15720
15721     mtx_lock(&bxe_prev_mtx);
15722
15723     BLOGD(sc, DBG_LOAD,
15724           "Marked path %d/%d/%d - finished previous unload\n",
15725           sc->pcie_bus, sc->pcie_device, SC_PATH(sc));
15726     LIST_INSERT_HEAD(&bxe_prev_list, tmp, node);
15727
15728     mtx_unlock(&bxe_prev_mtx);
15729
15730     return (0);
15731 }
15732
15733 static int
15734 bxe_do_flr(struct bxe_softc *sc)
15735 {
15736     int i;
15737
15738     /* only E2 and onwards support FLR */
15739     if (CHIP_IS_E1x(sc)) {
15740         BLOGD(sc, DBG_LOAD, "FLR not supported in E1/E1H\n");
15741         return (-1);
15742     }
15743
15744     /* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
15745     if (sc->devinfo.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
15746         BLOGD(sc, DBG_LOAD, "FLR not supported by BC_VER: 0x%08x\n",
15747               sc->devinfo.bc_ver);
15748         return (-1);
15749     }
15750
15751     /* Wait for Transaction Pending bit clean */
15752     for (i = 0; i < 4; i++) {
15753         if (i) {
15754             DELAY(((1 << (i - 1)) * 100) * 1000);
15755         }
15756
15757         if (!bxe_is_pcie_pending(sc)) {
15758             goto clear;
15759         }
15760     }
15761
15762     BLOGE(sc, "PCIE transaction is not cleared, "
15763               "proceeding with reset anyway\n");
15764
15765 clear:
15766
15767     BLOGD(sc, DBG_LOAD, "Initiating FLR\n");
15768     bxe_fw_command(sc, DRV_MSG_CODE_INITIATE_FLR, 0);
15769
15770     return (0);
15771 }
15772
15773 struct bxe_mac_vals {
15774     uint32_t xmac_addr;
15775     uint32_t xmac_val;
15776     uint32_t emac_addr;
15777     uint32_t emac_val;
15778     uint32_t umac_addr;
15779     uint32_t umac_val;
15780     uint32_t bmac_addr;
15781     uint32_t bmac_val[2];
15782 };
15783
15784 static void
15785 bxe_prev_unload_close_mac(struct bxe_softc *sc,
15786                           struct bxe_mac_vals *vals)
15787 {
15788     uint32_t val, base_addr, offset, mask, reset_reg;
15789     uint8_t mac_stopped = FALSE;
15790     uint8_t port = SC_PORT(sc);
15791     uint32_t wb_data[2];
15792
15793     /* reset addresses as they also mark which values were changed */
15794     vals->bmac_addr = 0;
15795     vals->umac_addr = 0;
15796     vals->xmac_addr = 0;
15797     vals->emac_addr = 0;
15798
15799     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_2);
15800
15801     if (!CHIP_IS_E3(sc)) {
15802         val = REG_RD(sc, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
15803         mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
15804         if ((mask & reset_reg) && val) {
15805             BLOGD(sc, DBG_LOAD, "Disable BMAC Rx\n");
15806             base_addr = SC_PORT(sc) ? NIG_REG_INGRESS_BMAC1_MEM
15807                                     : NIG_REG_INGRESS_BMAC0_MEM;
15808             offset = CHIP_IS_E2(sc) ? BIGMAC2_REGISTER_BMAC_CONTROL
15809                                     : BIGMAC_REGISTER_BMAC_CONTROL;
15810
15811             /*
15812              * use rd/wr since we cannot use dmae. This is safe
15813              * since MCP won't access the bus due to the request
15814              * to unload, and no function on the path can be
15815              * loaded at this time.
15816              */
15817             wb_data[0] = REG_RD(sc, base_addr + offset);
15818             wb_data[1] = REG_RD(sc, base_addr + offset + 0x4);
15819             vals->bmac_addr = base_addr + offset;
15820             vals->bmac_val[0] = wb_data[0];
15821             vals->bmac_val[1] = wb_data[1];
15822             wb_data[0] &= ~ELINK_BMAC_CONTROL_RX_ENABLE;
15823             REG_WR(sc, vals->bmac_addr, wb_data[0]);
15824             REG_WR(sc, vals->bmac_addr + 0x4, wb_data[1]);
15825         }
15826
15827         BLOGD(sc, DBG_LOAD, "Disable EMAC Rx\n");
15828         vals->emac_addr = NIG_REG_NIG_EMAC0_EN + SC_PORT(sc)*4;
15829         vals->emac_val = REG_RD(sc, vals->emac_addr);
15830         REG_WR(sc, vals->emac_addr, 0);
15831         mac_stopped = TRUE;
15832     } else {
15833         if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
15834             BLOGD(sc, DBG_LOAD, "Disable XMAC Rx\n");
15835             base_addr = SC_PORT(sc) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
15836             val = REG_RD(sc, base_addr + XMAC_REG_PFC_CTRL_HI);
15837             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val & ~(1 << 1));
15838             REG_WR(sc, base_addr + XMAC_REG_PFC_CTRL_HI, val | (1 << 1));
15839             vals->xmac_addr = base_addr + XMAC_REG_CTRL;
15840             vals->xmac_val = REG_RD(sc, vals->xmac_addr);
15841             REG_WR(sc, vals->xmac_addr, 0);
15842             mac_stopped = TRUE;
15843         }
15844
15845         mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
15846         if (mask & reset_reg) {
15847             BLOGD(sc, DBG_LOAD, "Disable UMAC Rx\n");
15848             base_addr = SC_PORT(sc) ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
15849             vals->umac_addr = base_addr + UMAC_REG_COMMAND_CONFIG;
15850             vals->umac_val = REG_RD(sc, vals->umac_addr);
15851             REG_WR(sc, vals->umac_addr, 0);
15852             mac_stopped = TRUE;
15853         }
15854     }
15855
15856     if (mac_stopped) {
15857         DELAY(20000);
15858     }
15859 }
15860
15861 #define BXE_PREV_UNDI_PROD_ADDR(p)  (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
15862 #define BXE_PREV_UNDI_RCQ(val)      ((val) & 0xffff)
15863 #define BXE_PREV_UNDI_BD(val)       ((val) >> 16 & 0xffff)
15864 #define BXE_PREV_UNDI_PROD(rcq, bd) ((bd) << 16 | (rcq))
15865
15866 static void
15867 bxe_prev_unload_undi_inc(struct bxe_softc *sc,
15868                          uint8_t          port,
15869                          uint8_t          inc)
15870 {
15871     uint16_t rcq, bd;
15872     uint32_t tmp_reg = REG_RD(sc, BXE_PREV_UNDI_PROD_ADDR(port));
15873
15874     rcq = BXE_PREV_UNDI_RCQ(tmp_reg) + inc;
15875     bd = BXE_PREV_UNDI_BD(tmp_reg) + inc;
15876
15877     tmp_reg = BXE_PREV_UNDI_PROD(rcq, bd);
15878     REG_WR(sc, BXE_PREV_UNDI_PROD_ADDR(port), tmp_reg);
15879
15880     BLOGD(sc, DBG_LOAD,
15881           "UNDI producer [%d] rings bd -> 0x%04x, rcq -> 0x%04x\n",
15882           port, bd, rcq);
15883 }
15884
15885 static int
15886 bxe_prev_unload_common(struct bxe_softc *sc)
15887 {
15888     uint32_t reset_reg, tmp_reg = 0, rc;
15889     uint8_t prev_undi = FALSE;
15890     struct bxe_mac_vals mac_vals;
15891     uint32_t timer_count = 1000;
15892     uint32_t prev_brb;
15893
15894     /*
15895      * It is possible a previous function received 'common' answer,
15896      * but hasn't loaded yet, therefore creating a scenario of
15897      * multiple functions receiving 'common' on the same path.
15898      */
15899     BLOGD(sc, DBG_LOAD, "Common unload Flow\n");
15900
15901     memset(&mac_vals, 0, sizeof(mac_vals));
15902
15903     if (bxe_prev_is_path_marked(sc)) {
15904         return (bxe_prev_mcp_done(sc));
15905     }
15906
15907     reset_reg = REG_RD(sc, MISC_REG_RESET_REG_1);
15908
15909     /* Reset should be performed after BRB is emptied */
15910     if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
15911         /* Close the MAC Rx to prevent BRB from filling up */
15912         bxe_prev_unload_close_mac(sc, &mac_vals);
15913
15914         /* close LLH filters towards the BRB */
15915         elink_set_rx_filter(&sc->link_params, 0);
15916
15917         /*
15918          * Check if the UNDI driver was previously loaded.
15919          * UNDI driver initializes CID offset for normal bell to 0x7
15920          */
15921         if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_DORQ) {
15922             tmp_reg = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
15923             if (tmp_reg == 0x7) {
15924                 BLOGD(sc, DBG_LOAD, "UNDI previously loaded\n");
15925                 prev_undi = TRUE;
15926                 /* clear the UNDI indication */
15927                 REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
15928                 /* clear possible idle check errors */
15929                 REG_RD(sc, NIG_REG_NIG_INT_STS_CLR_0);
15930             }
15931         }
15932
15933         /* wait until BRB is empty */
15934         tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15935         while (timer_count) {
15936             prev_brb = tmp_reg;
15937
15938             tmp_reg = REG_RD(sc, BRB1_REG_NUM_OF_FULL_BLOCKS);
15939             if (!tmp_reg) {
15940                 break;
15941             }
15942
15943             BLOGD(sc, DBG_LOAD, "BRB still has 0x%08x\n", tmp_reg);
15944
15945             /* reset timer as long as BRB actually gets emptied */
15946             if (prev_brb > tmp_reg) {
15947                 timer_count = 1000;
15948             } else {
15949                 timer_count--;
15950             }
15951
15952             /* If UNDI resides in memory, manually increment it */
15953             if (prev_undi) {
15954                 bxe_prev_unload_undi_inc(sc, SC_PORT(sc), 1);
15955             }
15956
15957             DELAY(10);
15958         }
15959
15960         if (!timer_count) {
15961             BLOGE(sc, "Failed to empty BRB\n");
15962         }
15963     }
15964
15965     /* No packets are in the pipeline, path is ready for reset */
15966     bxe_reset_common(sc);
15967
15968     if (mac_vals.xmac_addr) {
15969         REG_WR(sc, mac_vals.xmac_addr, mac_vals.xmac_val);
15970     }
15971     if (mac_vals.umac_addr) {
15972         REG_WR(sc, mac_vals.umac_addr, mac_vals.umac_val);
15973     }
15974     if (mac_vals.emac_addr) {
15975         REG_WR(sc, mac_vals.emac_addr, mac_vals.emac_val);
15976     }
15977     if (mac_vals.bmac_addr) {
15978         REG_WR(sc, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
15979         REG_WR(sc, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
15980     }
15981
15982     rc = bxe_prev_mark_path(sc, prev_undi);
15983     if (rc) {
15984         bxe_prev_mcp_done(sc);
15985         return (rc);
15986     }
15987
15988     return (bxe_prev_mcp_done(sc));
15989 }
15990
15991 static int
15992 bxe_prev_unload_uncommon(struct bxe_softc *sc)
15993 {
15994     int rc;
15995
15996     BLOGD(sc, DBG_LOAD, "Uncommon unload Flow\n");
15997
15998     /* Test if previous unload process was already finished for this path */
15999     if (bxe_prev_is_path_marked(sc)) {
16000         return (bxe_prev_mcp_done(sc));
16001     }
16002
16003     BLOGD(sc, DBG_LOAD, "Path is unmarked\n");
16004
16005     /*
16006      * If function has FLR capabilities, and existing FW version matches
16007      * the one required, then FLR will be sufficient to clean any residue
16008      * left by previous driver
16009      */
16010     rc = bxe_nic_load_analyze_req(sc, FW_MSG_CODE_DRV_LOAD_FUNCTION);
16011     if (!rc) {
16012         /* fw version is good */
16013         BLOGD(sc, DBG_LOAD, "FW version matches our own, attempting FLR\n");
16014         rc = bxe_do_flr(sc);
16015     }
16016
16017     if (!rc) {
16018         /* FLR was performed */
16019         BLOGD(sc, DBG_LOAD, "FLR successful\n");
16020         return (0);
16021     }
16022
16023     BLOGD(sc, DBG_LOAD, "Could not FLR\n");
16024
16025     /* Close the MCP request, return failure*/
16026     rc = bxe_prev_mcp_done(sc);
16027     if (!rc) {
16028         rc = BXE_PREV_WAIT_NEEDED;
16029     }
16030
16031     return (rc);
16032 }
16033
16034 static int
16035 bxe_prev_unload(struct bxe_softc *sc)
16036 {
16037     int time_counter = 10;
16038     uint32_t fw, hw_lock_reg, hw_lock_val;
16039     uint32_t rc = 0;
16040
16041     /*
16042      * Clear HW from errors which may have resulted from an interrupted
16043      * DMAE transaction.
16044      */
16045     bxe_prev_interrupted_dmae(sc);
16046
16047     /* Release previously held locks */
16048     hw_lock_reg =
16049         (SC_FUNC(sc) <= 5) ?
16050             (MISC_REG_DRIVER_CONTROL_1 + SC_FUNC(sc) * 8) :
16051             (MISC_REG_DRIVER_CONTROL_7 + (SC_FUNC(sc) - 6) * 8);
16052
16053     hw_lock_val = (REG_RD(sc, hw_lock_reg));
16054     if (hw_lock_val) {
16055         if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
16056             BLOGD(sc, DBG_LOAD, "Releasing previously held NVRAM lock\n");
16057             REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
16058                    (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << SC_PORT(sc)));
16059         }
16060         BLOGD(sc, DBG_LOAD, "Releasing previously held HW lock\n");
16061         REG_WR(sc, hw_lock_reg, 0xffffffff);
16062     } else {
16063         BLOGD(sc, DBG_LOAD, "No need to release HW/NVRAM locks\n");
16064     }
16065
16066     if (MCPR_ACCESS_LOCK_LOCK & REG_RD(sc, MCP_REG_MCPR_ACCESS_LOCK)) {
16067         BLOGD(sc, DBG_LOAD, "Releasing previously held ALR\n");
16068         REG_WR(sc, MCP_REG_MCPR_ACCESS_LOCK, 0);
16069     }
16070
16071     do {
16072         /* Lock MCP using an unload request */
16073         fw = bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
16074         if (!fw) {
16075             BLOGE(sc, "MCP response failure, aborting\n");
16076             rc = -1;
16077             break;
16078         }
16079
16080         if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON) {
16081             rc = bxe_prev_unload_common(sc);
16082             break;
16083         }
16084
16085         /* non-common reply from MCP night require looping */
16086         rc = bxe_prev_unload_uncommon(sc);
16087         if (rc != BXE_PREV_WAIT_NEEDED) {
16088             break;
16089         }
16090
16091         DELAY(20000);
16092     } while (--time_counter);
16093
16094     if (!time_counter || rc) {
16095         BLOGE(sc, "Failed to unload previous driver!\n");
16096         rc = -1;
16097     }
16098
16099     return (rc);
16100 }
16101
16102 void
16103 bxe_dcbx_set_state(struct bxe_softc *sc,
16104                    uint8_t          dcb_on,
16105                    uint32_t         dcbx_enabled)
16106 {
16107     if (!CHIP_IS_E1x(sc)) {
16108         sc->dcb_state = dcb_on;
16109         sc->dcbx_enabled = dcbx_enabled;
16110     } else {
16111         sc->dcb_state = FALSE;
16112         sc->dcbx_enabled = BXE_DCBX_ENABLED_INVALID;
16113     }
16114     BLOGD(sc, DBG_LOAD,
16115           "DCB state [%s:%s]\n",
16116           dcb_on ? "ON" : "OFF",
16117           (dcbx_enabled == BXE_DCBX_ENABLED_OFF) ? "user-mode" :
16118           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_OFF) ? "on-chip static" :
16119           (dcbx_enabled == BXE_DCBX_ENABLED_ON_NEG_ON) ?
16120           "on-chip with negotiation" : "invalid");
16121 }
16122
16123 /* must be called after sriov-enable */
16124 static int
16125 bxe_set_qm_cid_count(struct bxe_softc *sc)
16126 {
16127     int cid_count = BXE_L2_MAX_CID(sc);
16128
16129     if (IS_SRIOV(sc)) {
16130         cid_count += BXE_VF_CIDS;
16131     }
16132
16133     if (CNIC_SUPPORT(sc)) {
16134         cid_count += CNIC_CID_MAX;
16135     }
16136
16137     return (roundup(cid_count, QM_CID_ROUND));
16138 }
16139
16140 static void
16141 bxe_init_multi_cos(struct bxe_softc *sc)
16142 {
16143     int pri, cos;
16144
16145     uint32_t pri_map = 0; /* XXX change to user config */
16146
16147     for (pri = 0; pri < BXE_MAX_PRIORITY; pri++) {
16148         cos = ((pri_map & (0xf << (pri * 4))) >> (pri * 4));
16149         if (cos < sc->max_cos) {
16150             sc->prio_to_cos[pri] = cos;
16151         } else {
16152             BLOGW(sc, "Invalid COS %d for priority %d "
16153                       "(max COS is %d), setting to 0\n",
16154                   cos, pri, (sc->max_cos - 1));
16155             sc->prio_to_cos[pri] = 0;
16156         }
16157     }
16158 }
16159
16160 static int
16161 bxe_sysctl_state(SYSCTL_HANDLER_ARGS)
16162 {
16163     struct bxe_softc *sc;
16164     int error, result;
16165
16166     result = 0;
16167     error = sysctl_handle_int(oidp, &result, 0, req);
16168
16169     if (error || !req->newptr) {
16170         return (error);
16171     }
16172
16173     if (result == 1) {
16174         sc = (struct bxe_softc *)arg1;
16175         BLOGI(sc, "... dumping driver state ...\n");
16176         /* XXX */
16177     }
16178
16179     return (error);
16180 }
16181
16182 static int
16183 bxe_sysctl_eth_stat(SYSCTL_HANDLER_ARGS)
16184 {
16185     struct bxe_softc *sc = (struct bxe_softc *)arg1;
16186     uint32_t *eth_stats = (uint32_t *)&sc->eth_stats;
16187     uint32_t *offset;
16188     uint64_t value = 0;
16189     int index = (int)arg2;
16190
16191     if (index >= BXE_NUM_ETH_STATS) {
16192         BLOGE(sc, "bxe_eth_stats index out of range (%d)\n", index);
16193         return (-1);
16194     }
16195
16196     offset = (eth_stats + bxe_eth_stats_arr[index].offset);
16197
16198     switch (bxe_eth_stats_arr[index].size) {
16199     case 4:
16200         value = (uint64_t)*offset;
16201         break;
16202     case 8:
16203         value = HILO_U64(*offset, *(offset + 1));
16204         break;
16205     default:
16206         BLOGE(sc, "Invalid bxe_eth_stats size (index=%d size=%d)\n",
16207               index, bxe_eth_stats_arr[index].size);
16208         return (-1);
16209     }
16210
16211     return (sysctl_handle_64(oidp, &value, 0, req));
16212 }
16213
16214 static int
16215 bxe_sysctl_eth_q_stat(SYSCTL_HANDLER_ARGS)
16216 {
16217     struct bxe_softc *sc = (struct bxe_softc *)arg1;
16218     uint32_t *eth_stats;
16219     uint32_t *offset;
16220     uint64_t value = 0;
16221     uint32_t q_stat = (uint32_t)arg2;
16222     uint32_t fp_index = ((q_stat >> 16) & 0xffff);
16223     uint32_t index = (q_stat & 0xffff);
16224
16225     eth_stats = (uint32_t *)&sc->fp[fp_index].eth_q_stats;
16226
16227     if (index >= BXE_NUM_ETH_Q_STATS) {
16228         BLOGE(sc, "bxe_eth_q_stats index out of range (%d)\n", index);
16229         return (-1);
16230     }
16231
16232     offset = (eth_stats + bxe_eth_q_stats_arr[index].offset);
16233
16234     switch (bxe_eth_q_stats_arr[index].size) {
16235     case 4:
16236         value = (uint64_t)*offset;
16237         break;
16238     case 8:
16239         value = HILO_U64(*offset, *(offset + 1));
16240         break;
16241     default:
16242         BLOGE(sc, "Invalid bxe_eth_q_stats size (index=%d size=%d)\n",
16243               index, bxe_eth_q_stats_arr[index].size);
16244         return (-1);
16245     }
16246
16247     return (sysctl_handle_64(oidp, &value, 0, req));
16248 }
16249
16250 static void
16251 bxe_add_sysctls(struct bxe_softc *sc)
16252 {
16253     struct sysctl_ctx_list *ctx;
16254     struct sysctl_oid_list *children;
16255     struct sysctl_oid *queue_top, *queue;
16256     struct sysctl_oid_list *queue_top_children, *queue_children;
16257     char queue_num_buf[32];
16258     uint32_t q_stat;
16259     int i, j;
16260
16261     ctx = device_get_sysctl_ctx(sc->dev);
16262     children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
16263
16264     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "version",
16265                       CTLFLAG_RD, BXE_DRIVER_VERSION, 0,
16266                       "version");
16267
16268     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bc_version",
16269                       CTLFLAG_RD, &sc->devinfo.bc_ver_str, 0,
16270                       "bootcode version");
16271
16272     snprintf(sc->fw_ver_str, sizeof(sc->fw_ver_str), "%d.%d.%d.%d",
16273              BCM_5710_FW_MAJOR_VERSION,
16274              BCM_5710_FW_MINOR_VERSION,
16275              BCM_5710_FW_REVISION_VERSION,
16276              BCM_5710_FW_ENGINEERING_VERSION);
16277     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "fw_version",
16278                       CTLFLAG_RD, &sc->fw_ver_str, 0,
16279                       "firmware version");
16280
16281     snprintf(sc->mf_mode_str, sizeof(sc->mf_mode_str), "%s",
16282         ((sc->devinfo.mf_info.mf_mode == SINGLE_FUNCTION)     ? "Single"  :
16283          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SD)   ? "MF-SD"   :
16284          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_SI)   ? "MF-SI"   :
16285          (sc->devinfo.mf_info.mf_mode == MULTI_FUNCTION_AFEX) ? "MF-AFEX" :
16286                                                                 "Unknown"));
16287     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mf_mode",
16288                       CTLFLAG_RD, &sc->mf_mode_str, 0,
16289                       "multifunction mode");
16290
16291     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "mf_vnics",
16292                     CTLFLAG_RD, &sc->devinfo.mf_info.vnics_per_port, 0,
16293                     "multifunction vnics per port");
16294
16295     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "mac_addr",
16296                       CTLFLAG_RD, &sc->mac_addr_str, 0,
16297                       "mac address");
16298
16299     snprintf(sc->pci_link_str, sizeof(sc->pci_link_str), "%s x%d",
16300         ((sc->devinfo.pcie_link_speed == 1) ? "2.5GT/s" :
16301          (sc->devinfo.pcie_link_speed == 2) ? "5.0GT/s" :
16302          (sc->devinfo.pcie_link_speed == 4) ? "8.0GT/s" :
16303                                               "???GT/s"),
16304         sc->devinfo.pcie_link_width);
16305     SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pci_link",
16306                       CTLFLAG_RD, &sc->pci_link_str, 0,
16307                       "pci link status");
16308
16309     sc->debug = bxe_debug;
16310     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "debug",
16311                     CTLFLAG_RW, &sc->debug, 0,
16312                     "debug logging mode");
16313
16314     sc->rx_budget = bxe_rx_budget;
16315     SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_budget",
16316                     CTLFLAG_RW, &sc->rx_budget, 0,
16317                     "rx processing budget");
16318
16319     SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "state",
16320                     CTLTYPE_UINT | CTLFLAG_RW, sc, 0,
16321                     bxe_sysctl_state, "IU", "dump driver state");
16322
16323     for (i = 0; i < BXE_NUM_ETH_STATS; i++) {
16324         SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
16325                         bxe_eth_stats_arr[i].string,
16326                         CTLTYPE_U64 | CTLFLAG_RD, sc, i,
16327                         bxe_sysctl_eth_stat, "LU",
16328                         bxe_eth_stats_arr[i].string);
16329     }
16330
16331     /* add a new parent node for all queues "dev.bxe.#.queue" */
16332     queue_top = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "queue",
16333                                 CTLFLAG_RD, NULL, "queue");
16334     queue_top_children = SYSCTL_CHILDREN(queue_top);
16335
16336     for (i = 0; i < sc->num_queues; i++) {
16337         /* add a new parent node for a single queue "dev.bxe.#.queue.#" */
16338         snprintf(queue_num_buf, sizeof(queue_num_buf), "%d", i);
16339         queue = SYSCTL_ADD_NODE(ctx, queue_top_children, OID_AUTO,
16340                                 queue_num_buf, CTLFLAG_RD, NULL,
16341                                 "single queue");
16342         queue_children = SYSCTL_CHILDREN(queue);
16343
16344         for (j = 0; j < BXE_NUM_ETH_Q_STATS; j++) {
16345             q_stat = ((i << 16) | j);
16346             SYSCTL_ADD_PROC(ctx, queue_children, OID_AUTO,
16347                             bxe_eth_q_stats_arr[j].string,
16348                             CTLTYPE_U64 | CTLFLAG_RD, sc, q_stat,
16349                             bxe_sysctl_eth_q_stat, "LU",
16350                             bxe_eth_q_stats_arr[j].string);
16351         }
16352     }
16353 }
16354
16355 /*
16356  * Device attach function.
16357  *
16358  * Allocates device resources, performs secondary chip identification, and
16359  * initializes driver instance variables. This function is called from driver
16360  * load after a successful probe.
16361  *
16362  * Returns:
16363  *   0 = Success, >0 = Failure
16364  */
16365 static int
16366 bxe_attach(device_t dev)
16367 {
16368     struct bxe_softc *sc;
16369
16370     sc = device_get_softc(dev);
16371
16372     BLOGD(sc, DBG_LOAD, "Starting attach...\n");
16373
16374     sc->state = BXE_STATE_CLOSED;
16375
16376     sc->dev  = dev;
16377     sc->unit = device_get_unit(dev);
16378
16379     BLOGD(sc, DBG_LOAD, "softc = %p\n", sc);
16380
16381     sc->pcie_bus    = pci_get_bus(dev);
16382     sc->pcie_device = pci_get_slot(dev);
16383     sc->pcie_func   = pci_get_function(dev);
16384
16385     /* enable bus master capability */
16386     pci_enable_busmaster(dev);
16387
16388     /* get the BARs */
16389     if (bxe_allocate_bars(sc) != 0) {
16390         return (ENXIO);
16391     }
16392
16393     /* initialize the mutexes */
16394     bxe_init_mutexes(sc);
16395
16396     /* prepare the periodic callout */
16397     callout_init(&sc->periodic_callout, 0);
16398
16399     /* prepare the chip taskqueue */
16400     sc->chip_tq_flags = CHIP_TQ_NONE;
16401     snprintf(sc->chip_tq_name, sizeof(sc->chip_tq_name),
16402              "bxe%d_chip_tq", sc->unit);
16403     TASK_INIT(&sc->chip_tq_task, 0, bxe_handle_chip_tq, sc);
16404     sc->chip_tq = taskqueue_create(sc->chip_tq_name, M_NOWAIT,
16405                                    taskqueue_thread_enqueue,
16406                                    &sc->chip_tq);
16407     taskqueue_start_threads(&sc->chip_tq, 1, PWAIT, /* lower priority */
16408                             "%s", sc->chip_tq_name);
16409
16410     /* get device info and set params */
16411     if (bxe_get_device_info(sc) != 0) {
16412         BLOGE(sc, "getting device info\n");
16413         bxe_deallocate_bars(sc);
16414         pci_disable_busmaster(dev);
16415         return (ENXIO);
16416     }
16417
16418     /* get final misc params */
16419     bxe_get_params(sc);
16420
16421     /* set the default MTU (changed via ifconfig) */
16422     sc->mtu = ETHERMTU;
16423
16424     bxe_set_modes_bitmap(sc);
16425
16426     /* XXX
16427      * If in AFEX mode and the function is configured for FCoE
16428      * then bail... no L2 allowed.
16429      */
16430
16431     /* get phy settings from shmem and 'and' against admin settings */
16432     bxe_get_phy_info(sc);
16433
16434     /* initialize the FreeBSD ifnet interface */
16435     if (bxe_init_ifnet(sc) != 0) {
16436         bxe_release_mutexes(sc);
16437         bxe_deallocate_bars(sc);
16438         pci_disable_busmaster(dev);
16439         return (ENXIO);
16440     }
16441
16442     /* allocate device interrupts */
16443     if (bxe_interrupt_alloc(sc) != 0) {
16444         if (sc->ifnet != NULL) {
16445             ether_ifdetach(sc->ifnet);
16446         }
16447         ifmedia_removeall(&sc->ifmedia);
16448         bxe_release_mutexes(sc);
16449         bxe_deallocate_bars(sc);
16450         pci_disable_busmaster(dev);
16451         return (ENXIO);
16452     }
16453
16454     /* allocate ilt */
16455     if (bxe_alloc_ilt_mem(sc) != 0) {
16456         bxe_interrupt_free(sc);
16457         if (sc->ifnet != NULL) {
16458             ether_ifdetach(sc->ifnet);
16459         }
16460         ifmedia_removeall(&sc->ifmedia);
16461         bxe_release_mutexes(sc);
16462         bxe_deallocate_bars(sc);
16463         pci_disable_busmaster(dev);
16464         return (ENXIO);
16465     }
16466
16467     /* allocate the host hardware/software hsi structures */
16468     if (bxe_alloc_hsi_mem(sc) != 0) {
16469         bxe_free_ilt_mem(sc);
16470         bxe_interrupt_free(sc);
16471         if (sc->ifnet != NULL) {
16472             ether_ifdetach(sc->ifnet);
16473         }
16474         ifmedia_removeall(&sc->ifmedia);
16475         bxe_release_mutexes(sc);
16476         bxe_deallocate_bars(sc);
16477         pci_disable_busmaster(dev);
16478         return (ENXIO);
16479     }
16480
16481     /* need to reset chip if UNDI was active */
16482     if (IS_PF(sc) && !BXE_NOMCP(sc)) {
16483         /* init fw_seq */
16484         sc->fw_seq =
16485             (SHMEM_RD(sc, func_mb[SC_FW_MB_IDX(sc)].drv_mb_header) &
16486              DRV_MSG_SEQ_NUMBER_MASK);
16487         BLOGD(sc, DBG_LOAD, "prev unload fw_seq 0x%04x\n", sc->fw_seq);
16488         bxe_prev_unload(sc);
16489     }
16490
16491 #if 1
16492     /* XXX */
16493     bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16494 #else
16495     if (SHMEM2_HAS(sc, dcbx_lldp_params_offset) &&
16496         SHMEM2_HAS(sc, dcbx_lldp_dcbx_stat_offset) &&
16497         SHMEM2_RD(sc, dcbx_lldp_params_offset) &&
16498         SHMEM2_RD(sc, dcbx_lldp_dcbx_stat_offset)) {
16499         bxe_dcbx_set_state(sc, TRUE, BXE_DCBX_ENABLED_ON_NEG_ON);
16500         bxe_dcbx_init_params(sc);
16501     } else {
16502         bxe_dcbx_set_state(sc, FALSE, BXE_DCBX_ENABLED_OFF);
16503     }
16504 #endif
16505
16506     /* calculate qm_cid_count */
16507     sc->qm_cid_count = bxe_set_qm_cid_count(sc);
16508     BLOGD(sc, DBG_LOAD, "qm_cid_count=%d\n", sc->qm_cid_count);
16509
16510     sc->max_cos = 1;
16511     bxe_init_multi_cos(sc);
16512
16513     bxe_add_sysctls(sc);
16514
16515     return (0);
16516 }
16517
16518 /*
16519  * Device detach function.
16520  *
16521  * Stops the controller, resets the controller, and releases resources.
16522  *
16523  * Returns:
16524  *   0 = Success, >0 = Failure
16525  */
16526 static int
16527 bxe_detach(device_t dev)
16528 {
16529     struct bxe_softc *sc;
16530     struct ifnet *ifp;
16531
16532     sc = device_get_softc(dev);
16533
16534     BLOGD(sc, DBG_LOAD, "Starting detach...\n");
16535
16536     ifp = sc->ifnet;
16537     if (ifp != NULL && ifp->if_vlantrunk != NULL) {
16538         BLOGE(sc, "Cannot detach while VLANs are in use.\n");
16539         return(EBUSY);
16540     }
16541
16542     /* stop the periodic callout */
16543     bxe_periodic_stop(sc);
16544
16545     /* stop the chip taskqueue */
16546     atomic_store_rel_long(&sc->chip_tq_flags, CHIP_TQ_NONE);
16547     if (sc->chip_tq) {
16548         taskqueue_drain(sc->chip_tq, &sc->chip_tq_task);
16549         taskqueue_free(sc->chip_tq);
16550         sc->chip_tq = NULL;
16551     }
16552
16553     /* stop and reset the controller if it was open */
16554     if (sc->state != BXE_STATE_CLOSED) {
16555         BXE_CORE_LOCK(sc);
16556         bxe_nic_unload(sc, UNLOAD_CLOSE, TRUE);
16557         BXE_CORE_UNLOCK(sc);
16558     }
16559
16560     /* release the network interface */
16561     if (ifp != NULL) {
16562         ether_ifdetach(ifp);
16563     }
16564     ifmedia_removeall(&sc->ifmedia);
16565
16566     /* XXX do the following based on driver state... */
16567
16568     /* free the host hardware/software hsi structures */
16569     bxe_free_hsi_mem(sc);
16570
16571     /* free ilt */
16572     bxe_free_ilt_mem(sc);
16573
16574     /* release the interrupts */
16575     bxe_interrupt_free(sc);
16576
16577     /* Release the mutexes*/
16578     bxe_release_mutexes(sc);
16579
16580     /* Release the PCIe BAR mapped memory */
16581     bxe_deallocate_bars(sc);
16582
16583     /* Release the FreeBSD interface. */
16584     if (sc->ifnet != NULL) {
16585         if_free(sc->ifnet);
16586     }
16587
16588     pci_disable_busmaster(dev);
16589
16590     return (0);
16591 }
16592
16593 /*
16594  * Device shutdown function.
16595  *
16596  * Stops and resets the controller.
16597  *
16598  * Returns:
16599  *   Nothing
16600  */
16601 static int
16602 bxe_shutdown(device_t dev)
16603 {
16604     struct bxe_softc *sc;
16605
16606     sc = device_get_softc(dev);
16607
16608     BLOGD(sc, DBG_LOAD, "Starting shutdown...\n");
16609
16610     /* stop the periodic callout */
16611     bxe_periodic_stop(sc);
16612
16613     BXE_CORE_LOCK(sc);
16614     bxe_nic_unload(sc, UNLOAD_NORMAL, FALSE);
16615     BXE_CORE_UNLOCK(sc);
16616
16617     return (0);
16618 }
16619
16620 void
16621 bxe_igu_ack_sb(struct bxe_softc *sc,
16622                uint8_t          igu_sb_id,
16623                uint8_t          segment,
16624                uint16_t         index,
16625                uint8_t          op,
16626                uint8_t          update)
16627 {
16628     uint32_t igu_addr = sc->igu_base_addr;
16629     igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
16630     bxe_igu_ack_sb_gen(sc, igu_sb_id, segment, index, op, update, igu_addr);
16631 }
16632
16633 static void
16634 bxe_igu_clear_sb_gen(struct bxe_softc *sc,
16635                      uint8_t          func,
16636                      uint8_t          idu_sb_id,
16637                      uint8_t          is_pf)
16638 {
16639     uint32_t data, ctl, cnt = 100;
16640     uint32_t igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
16641     uint32_t igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
16642     uint32_t igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
16643     uint32_t sb_bit =  1 << (idu_sb_id%32);
16644     uint32_t func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
16645     uint32_t addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
16646
16647     /* Not supported in BC mode */
16648     if (CHIP_INT_MODE_IS_BC(sc)) {
16649         return;
16650     }
16651
16652     data = ((IGU_USE_REGISTER_cstorm_type_0_sb_cleanup <<
16653              IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
16654             IGU_REGULAR_CLEANUP_SET |
16655             IGU_REGULAR_BCLEANUP);
16656
16657     ctl = ((addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT) |
16658            (func_encode << IGU_CTRL_REG_FID_SHIFT) |
16659            (IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT));
16660
16661     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16662             data, igu_addr_data);
16663     REG_WR(sc, igu_addr_data, data);
16664
16665     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16666                       BUS_SPACE_BARRIER_WRITE);
16667     mb();
16668
16669     BLOGD(sc, DBG_LOAD, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
16670             ctl, igu_addr_ctl);
16671     REG_WR(sc, igu_addr_ctl, ctl);
16672
16673     bus_space_barrier(sc->bar[BAR0].tag, sc->bar[BAR0].handle, 0, 0,
16674                       BUS_SPACE_BARRIER_WRITE);
16675     mb();
16676
16677     /* wait for clean up to finish */
16678     while (!(REG_RD(sc, igu_addr_ack) & sb_bit) && --cnt) {
16679         DELAY(20000);
16680     }
16681
16682     if (!(REG_RD(sc, igu_addr_ack) & sb_bit)) {
16683         BLOGD(sc, DBG_LOAD,
16684               "Unable to finish IGU cleanup: "
16685               "idu_sb_id %d offset %d bit %d (cnt %d)\n",
16686               idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
16687     }
16688 }
16689
16690 static void
16691 bxe_igu_clear_sb(struct bxe_softc *sc,
16692                  uint8_t          idu_sb_id)
16693 {
16694     bxe_igu_clear_sb_gen(sc, SC_FUNC(sc), idu_sb_id, TRUE /*PF*/);
16695 }
16696
16697
16698
16699
16700
16701
16702
16703 /*******************/
16704 /* ECORE CALLBACKS */
16705 /*******************/
16706
16707 static void
16708 bxe_reset_common(struct bxe_softc *sc)
16709 {
16710     uint32_t val = 0x1400;
16711
16712     /* reset_common */
16713     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR), 0xd3ffff7f);
16714
16715     if (CHIP_IS_E3(sc)) {
16716         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
16717         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
16718     }
16719
16720     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR), val);
16721 }
16722
16723 static void
16724 bxe_common_init_phy(struct bxe_softc *sc)
16725 {
16726     uint32_t shmem_base[2];
16727     uint32_t shmem2_base[2];
16728
16729     /* Avoid common init in case MFW supports LFA */
16730     if (SHMEM2_RD(sc, size) >
16731         (uint32_t)offsetof(struct shmem2_region,
16732                            lfa_host_addr[SC_PORT(sc)])) {
16733         return;
16734     }
16735
16736     shmem_base[0]  = sc->devinfo.shmem_base;
16737     shmem2_base[0] = sc->devinfo.shmem2_base;
16738
16739     if (!CHIP_IS_E1x(sc)) {
16740         shmem_base[1]  = SHMEM2_RD(sc, other_shmem_base_addr);
16741         shmem2_base[1] = SHMEM2_RD(sc, other_shmem2_base_addr);
16742     }
16743
16744     bxe_acquire_phy_lock(sc);
16745     elink_common_init_phy(sc, shmem_base, shmem2_base,
16746                           sc->devinfo.chip_id, 0);
16747     bxe_release_phy_lock(sc);
16748 }
16749
16750 static void
16751 bxe_pf_disable(struct bxe_softc *sc)
16752 {
16753     uint32_t val = REG_RD(sc, IGU_REG_PF_CONFIGURATION);
16754
16755     val &= ~IGU_PF_CONF_FUNC_EN;
16756
16757     REG_WR(sc, IGU_REG_PF_CONFIGURATION, val);
16758     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
16759     REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 0);
16760 }
16761
16762 static void
16763 bxe_init_pxp(struct bxe_softc *sc)
16764 {
16765     uint16_t devctl;
16766     int r_order, w_order;
16767
16768     devctl = bxe_pcie_capability_read(sc, PCIR_EXPRESS_DEVICE_CTL, 2);
16769
16770     BLOGD(sc, DBG_LOAD, "read 0x%08x from devctl\n", devctl);
16771
16772     w_order = ((devctl & PCIM_EXP_CTL_MAX_PAYLOAD) >> 5);
16773
16774     if (sc->mrrs == -1) {
16775         r_order = ((devctl & PCIM_EXP_CTL_MAX_READ_REQUEST) >> 12);
16776     } else {
16777         BLOGD(sc, DBG_LOAD, "forcing read order to %d\n", sc->mrrs);
16778         r_order = sc->mrrs;
16779     }
16780
16781     ecore_init_pxp_arb(sc, r_order, w_order);
16782 }
16783
16784 static uint32_t
16785 bxe_get_pretend_reg(struct bxe_softc *sc)
16786 {
16787     uint32_t base = PXP2_REG_PGL_PRETEND_FUNC_F0;
16788     uint32_t stride = (PXP2_REG_PGL_PRETEND_FUNC_F1 - base);
16789     return (base + (SC_ABS_FUNC(sc)) * stride);
16790 }
16791
16792 /*
16793  * Called only on E1H or E2.
16794  * When pretending to be PF, the pretend value is the function number 0..7.
16795  * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
16796  * combination.
16797  */
16798 static int
16799 bxe_pretend_func(struct bxe_softc *sc,
16800                  uint16_t         pretend_func_val)
16801 {
16802     uint32_t pretend_reg;
16803
16804     if (CHIP_IS_E1H(sc) && (pretend_func_val > E1H_FUNC_MAX)) {
16805         return (-1);
16806     }
16807
16808     /* get my own pretend register */
16809     pretend_reg = bxe_get_pretend_reg(sc);
16810     REG_WR(sc, pretend_reg, pretend_func_val);
16811     REG_RD(sc, pretend_reg);
16812     return (0);
16813 }
16814
16815 static void
16816 bxe_iov_init_dmae(struct bxe_softc *sc)
16817 {
16818     return;
16819 #if 0
16820     BLOGD(sc, DBG_LOAD, "SRIOV is %s\n", IS_SRIOV(sc) ? "ON" : "OFF");
16821
16822     if (!IS_SRIOV(sc)) {
16823         return;
16824     }
16825
16826     REG_WR(sc, DMAE_REG_BACKWARD_COMP_EN, 0);
16827 #endif
16828 }
16829
16830 #if 0
16831 static int
16832 bxe_iov_init_ilt(struct bxe_softc *sc,
16833                  uint16_t         line)
16834 {
16835     return (line);
16836 #if 0
16837     int i;
16838     struct ecore_ilt* ilt = sc->ilt;
16839
16840     if (!IS_SRIOV(sc)) {
16841         return (line);
16842     }
16843
16844     /* set vfs ilt lines */
16845     for (i = 0; i < BXE_VF_CIDS/ILT_PAGE_CIDS ; i++) {
16846         struct hw_dma *hw_cxt = SC_VF_CXT_PAGE(sc,i);
16847         ilt->lines[line+i].page = hw_cxt->addr;
16848         ilt->lines[line+i].page_mapping = hw_cxt->mapping;
16849         ilt->lines[line+i].size = hw_cxt->size; /* doesn't matter */
16850     }
16851     return (line+i);
16852 #endif
16853 }
16854 #endif
16855
16856 static void
16857 bxe_iov_init_dq(struct bxe_softc *sc)
16858 {
16859     return;
16860 #if 0
16861     if (!IS_SRIOV(sc)) {
16862         return;
16863     }
16864
16865     /* Set the DQ such that the CID reflect the abs_vfid */
16866     REG_WR(sc, DORQ_REG_VF_NORM_VF_BASE, 0);
16867     REG_WR(sc, DORQ_REG_MAX_RVFID_SIZE, ilog2(BNX2X_MAX_NUM_OF_VFS));
16868
16869     /*
16870      * Set VFs starting CID. If its > 0 the preceding CIDs are belong to
16871      * the PF L2 queues
16872      */
16873     REG_WR(sc, DORQ_REG_VF_NORM_CID_BASE, BNX2X_FIRST_VF_CID);
16874
16875     /* The VF window size is the log2 of the max number of CIDs per VF */
16876     REG_WR(sc, DORQ_REG_VF_NORM_CID_WND_SIZE, BNX2X_VF_CID_WND);
16877
16878     /*
16879      * The VF doorbell size  0 - *B, 4 - 128B. We set it here to match
16880      * the Pf doorbell size although the 2 are independent.
16881      */
16882     REG_WR(sc, DORQ_REG_VF_NORM_CID_OFST,
16883            BNX2X_DB_SHIFT - BNX2X_DB_MIN_SHIFT);
16884
16885     /*
16886      * No security checks for now -
16887      * configure single rule (out of 16) mask = 0x1, value = 0x0,
16888      * CID range 0 - 0x1ffff
16889      */
16890     REG_WR(sc, DORQ_REG_VF_TYPE_MASK_0, 1);
16891     REG_WR(sc, DORQ_REG_VF_TYPE_VALUE_0, 0);
16892     REG_WR(sc, DORQ_REG_VF_TYPE_MIN_MCID_0, 0);
16893     REG_WR(sc, DORQ_REG_VF_TYPE_MAX_MCID_0, 0x1ffff);
16894
16895     /* set the number of VF alllowed doorbells to the full DQ range */
16896     REG_WR(sc, DORQ_REG_VF_NORM_MAX_CID_COUNT, 0x20000);
16897
16898     /* set the VF doorbell threshold */
16899     REG_WR(sc, DORQ_REG_VF_USAGE_CT_LIMIT, 4);
16900 #endif
16901 }
16902
16903 /* send a NIG loopback debug packet */
16904 static void
16905 bxe_lb_pckt(struct bxe_softc *sc)
16906 {
16907     uint32_t wb_write[3];
16908
16909     /* Ethernet source and destination addresses */
16910     wb_write[0] = 0x55555555;
16911     wb_write[1] = 0x55555555;
16912     wb_write[2] = 0x20;     /* SOP */
16913     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16914
16915     /* NON-IP protocol */
16916     wb_write[0] = 0x09000000;
16917     wb_write[1] = 0x55555555;
16918     wb_write[2] = 0x10;     /* EOP, eop_bvalid = 0 */
16919     REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
16920 }
16921
16922 /*
16923  * Some of the internal memories are not directly readable from the driver.
16924  * To test them we send debug packets.
16925  */
16926 static int
16927 bxe_int_mem_test(struct bxe_softc *sc)
16928 {
16929     int factor;
16930     int count, i;
16931     uint32_t val = 0;
16932
16933     if (CHIP_REV_IS_FPGA(sc)) {
16934         factor = 120;
16935     } else if (CHIP_REV_IS_EMUL(sc)) {
16936         factor = 200;
16937     } else {
16938         factor = 1;
16939     }
16940
16941     /* disable inputs of parser neighbor blocks */
16942     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16943     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
16944     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
16945     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
16946
16947     /*  write 0 to parser credits for CFC search request */
16948     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
16949
16950     /* send Ethernet packet */
16951     bxe_lb_pckt(sc);
16952
16953     /* TODO do i reset NIG statistic? */
16954     /* Wait until NIG register shows 1 packet of size 0x10 */
16955     count = 1000 * factor;
16956     while (count) {
16957         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
16958         val = *BXE_SP(sc, wb_data[0]);
16959         if (val == 0x10) {
16960             break;
16961         }
16962
16963         DELAY(10000);
16964         count--;
16965     }
16966
16967     if (val != 0x10) {
16968         BLOGE(sc, "NIG timeout val=0x%x\n", val);
16969         return (-1);
16970     }
16971
16972     /* wait until PRS register shows 1 packet */
16973     count = (1000 * factor);
16974     while (count) {
16975         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
16976         if (val == 1) {
16977             break;
16978         }
16979
16980         DELAY(10000);
16981         count--;
16982     }
16983
16984     if (val != 0x1) {
16985         BLOGE(sc, "PRS timeout val=0x%x\n", val);
16986         return (-2);
16987     }
16988
16989     /* Reset and init BRB, PRS */
16990     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
16991     DELAY(50000);
16992     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
16993     DELAY(50000);
16994     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
16995     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
16996
16997     /* Disable inputs of parser neighbor blocks */
16998     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
16999     REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
17000     REG_WR(sc, CFC_REG_DEBUG0, 0x1);
17001     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
17002
17003     /* Write 0 to parser credits for CFC search request */
17004     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
17005
17006     /* send 10 Ethernet packets */
17007     for (i = 0; i < 10; i++) {
17008         bxe_lb_pckt(sc);
17009     }
17010
17011     /* Wait until NIG register shows 10+1 packets of size 11*0x10 = 0xb0 */
17012     count = (1000 * factor);
17013     while (count) {
17014         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17015         val = *BXE_SP(sc, wb_data[0]);
17016         if (val == 0xb0) {
17017             break;
17018         }
17019
17020         DELAY(10000);
17021         count--;
17022     }
17023
17024     if (val != 0xb0) {
17025         BLOGE(sc, "NIG timeout val=0x%x\n", val);
17026         return (-3);
17027     }
17028
17029     /* Wait until PRS register shows 2 packets */
17030     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
17031     if (val != 2) {
17032         BLOGE(sc, "PRS timeout val=0x%x\n", val);
17033     }
17034
17035     /* Write 1 to parser credits for CFC search request */
17036     REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
17037
17038     /* Wait until PRS register shows 3 packets */
17039     DELAY(10000 * factor);
17040
17041     /* Wait until NIG register shows 1 packet of size 0x10 */
17042     val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
17043     if (val != 3) {
17044         BLOGE(sc, "PRS timeout val=0x%x\n", val);
17045     }
17046
17047     /* clear NIG EOP FIFO */
17048     for (i = 0; i < 11; i++) {
17049         REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
17050     }
17051
17052     val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
17053     if (val != 1) {
17054         BLOGE(sc, "clear of NIG failed\n");
17055         return (-4);
17056     }
17057
17058     /* Reset and init BRB, PRS, NIG */
17059     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
17060     DELAY(50000);
17061     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
17062     DELAY(50000);
17063     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17064     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17065     if (!CNIC_SUPPORT(sc)) {
17066         /* set NIC mode */
17067         REG_WR(sc, PRS_REG_NIC_MODE, 1);
17068     }
17069
17070     /* Enable inputs of parser neighbor blocks */
17071     REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
17072     REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
17073     REG_WR(sc, CFC_REG_DEBUG0, 0x0);
17074     REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
17075
17076     return (0);
17077 }
17078
17079 static void
17080 bxe_setup_fan_failure_detection(struct bxe_softc *sc)
17081 {
17082     int is_required;
17083     uint32_t val;
17084     int port;
17085
17086     is_required = 0;
17087     val = (SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
17088            SHARED_HW_CFG_FAN_FAILURE_MASK);
17089
17090     if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED) {
17091         is_required = 1;
17092     }
17093     /*
17094      * The fan failure mechanism is usually related to the PHY type since
17095      * the power consumption of the board is affected by the PHY. Currently,
17096      * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
17097      */
17098     else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE) {
17099         for (port = PORT_0; port < PORT_MAX; port++) {
17100             is_required |= elink_fan_failure_det_req(sc,
17101                                                      sc->devinfo.shmem_base,
17102                                                      sc->devinfo.shmem2_base,
17103                                                      port);
17104         }
17105     }
17106
17107     BLOGD(sc, DBG_LOAD, "fan detection setting: %d\n", is_required);
17108
17109     if (is_required == 0) {
17110         return;
17111     }
17112
17113     /* Fan failure is indicated by SPIO 5 */
17114     bxe_set_spio(sc, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
17115
17116     /* set to active low mode */
17117     val = REG_RD(sc, MISC_REG_SPIO_INT);
17118     val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
17119     REG_WR(sc, MISC_REG_SPIO_INT, val);
17120
17121     /* enable interrupt to signal the IGU */
17122     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17123     val |= MISC_SPIO_SPIO5;
17124     REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
17125 }
17126
17127 static void
17128 bxe_enable_blocks_attention(struct bxe_softc *sc)
17129 {
17130     uint32_t val;
17131
17132     REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17133     if (!CHIP_IS_E1x(sc)) {
17134         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0x40);
17135     } else {
17136         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
17137     }
17138     REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17139     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17140     /*
17141      * mask read length error interrupts in brb for parser
17142      * (parsing unit and 'checksum and crc' unit)
17143      * these errors are legal (PU reads fixed length and CAC can cause
17144      * read length error on truncated packets)
17145      */
17146     REG_WR(sc, BRB1_REG_BRB1_INT_MASK, 0xFC00);
17147     REG_WR(sc, QM_REG_QM_INT_MASK, 0);
17148     REG_WR(sc, TM_REG_TM_INT_MASK, 0);
17149     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
17150     REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
17151     REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
17152 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_0, 0); */
17153 /*      REG_WR(sc, XSEM_REG_XSEM_INT_MASK_1, 0); */
17154     REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
17155     REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
17156     REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
17157 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_0, 0); */
17158 /*      REG_WR(sc, USEM_REG_USEM_INT_MASK_1, 0); */
17159     REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
17160     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
17161     REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
17162     REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
17163 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_0, 0); */
17164 /*      REG_WR(sc, CSEM_REG_CSEM_INT_MASK_1, 0); */
17165
17166     val = (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT |
17167            PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
17168            PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN);
17169     if (!CHIP_IS_E1x(sc)) {
17170         val |= (PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
17171                 PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED);
17172     }
17173     REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, val);
17174
17175     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
17176     REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
17177     REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
17178 /*      REG_WR(sc, TSEM_REG_TSEM_INT_MASK_0, 0); */
17179
17180     if (!CHIP_IS_E1x(sc)) {
17181         /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
17182         REG_WR(sc, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
17183     }
17184
17185     REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
17186     REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
17187 /*      REG_WR(sc, MISC_REG_MISC_INT_MASK, 0); */
17188     REG_WR(sc, PBF_REG_PBF_INT_MASK, 0x18);     /* bit 3,4 masked */
17189 }
17190
17191 /**
17192  * bxe_init_hw_common - initialize the HW at the COMMON phase.
17193  *
17194  * @sc:     driver handle
17195  */
17196 static int
17197 bxe_init_hw_common(struct bxe_softc *sc)
17198 {
17199     uint8_t abs_func_id;
17200     uint32_t val;
17201
17202     BLOGD(sc, DBG_LOAD, "starting common init for func %d\n",
17203           SC_ABS_FUNC(sc));
17204
17205     /*
17206      * take the RESET lock to protect undi_unload flow from accessing
17207      * registers while we are resetting the chip
17208      */
17209     bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17210
17211     bxe_reset_common(sc);
17212
17213     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET), 0xffffffff);
17214
17215     val = 0xfffc;
17216     if (CHIP_IS_E3(sc)) {
17217         val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
17218         val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
17219     }
17220
17221     REG_WR(sc, (GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET), val);
17222
17223     bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_RESET);
17224
17225     ecore_init_block(sc, BLOCK_MISC, PHASE_COMMON);
17226     BLOGD(sc, DBG_LOAD, "after misc block init\n");
17227
17228     if (!CHIP_IS_E1x(sc)) {
17229         /*
17230          * 4-port mode or 2-port mode we need to turn off master-enable for
17231          * everyone. After that we turn it back on for self. So, we disregard
17232          * multi-function, and always disable all functions on the given path,
17233          * this means 0,2,4,6 for path 0 and 1,3,5,7 for path 1
17234          */
17235         for (abs_func_id = SC_PATH(sc);
17236              abs_func_id < (E2_FUNC_MAX * 2);
17237              abs_func_id += 2) {
17238             if (abs_func_id == SC_ABS_FUNC(sc)) {
17239                 REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17240                 continue;
17241             }
17242
17243             bxe_pretend_func(sc, abs_func_id);
17244
17245             /* clear pf enable */
17246             bxe_pf_disable(sc);
17247
17248             bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17249         }
17250     }
17251
17252     BLOGD(sc, DBG_LOAD, "after pf disable\n");
17253
17254     ecore_init_block(sc, BLOCK_PXP, PHASE_COMMON);
17255
17256     if (CHIP_IS_E1(sc)) {
17257         /*
17258          * enable HW interrupt from PXP on USDM overflow
17259          * bit 16 on INT_MASK_0
17260          */
17261         REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
17262     }
17263
17264     ecore_init_block(sc, BLOCK_PXP2, PHASE_COMMON);
17265     bxe_init_pxp(sc);
17266
17267 #ifdef __BIG_ENDIAN
17268     REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
17269     REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
17270     REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
17271     REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
17272     REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
17273     /* make sure this value is 0 */
17274     REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
17275
17276     //REG_WR(sc, PXP2_REG_RD_PBF_SWAP_MODE, 1);
17277     REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
17278     REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
17279     REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
17280     REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
17281 #endif
17282
17283     ecore_ilt_init_page_size(sc, INITOP_SET);
17284
17285     if (CHIP_REV_IS_FPGA(sc) && CHIP_IS_E1H(sc)) {
17286         REG_WR(sc, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
17287     }
17288
17289     /* let the HW do it's magic... */
17290     DELAY(100000);
17291
17292     /* finish PXP init */
17293     val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
17294     if (val != 1) {
17295         BLOGE(sc, "PXP2 CFG failed\n");
17296         return (-1);
17297     }
17298     val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
17299     if (val != 1) {
17300         BLOGE(sc, "PXP2 RD_INIT failed\n");
17301         return (-1);
17302     }
17303
17304     BLOGD(sc, DBG_LOAD, "after pxp init\n");
17305
17306     /*
17307      * Timer bug workaround for E2 only. We need to set the entire ILT to have
17308      * entries with value "0" and valid bit on. This needs to be done by the
17309      * first PF that is loaded in a path (i.e. common phase)
17310      */
17311     if (!CHIP_IS_E1x(sc)) {
17312 /*
17313  * In E2 there is a bug in the timers block that can cause function 6 / 7
17314  * (i.e. vnic3) to start even if it is marked as "scan-off".
17315  * This occurs when a different function (func2,3) is being marked
17316  * as "scan-off". Real-life scenario for example: if a driver is being
17317  * load-unloaded while func6,7 are down. This will cause the timer to access
17318  * the ilt, translate to a logical address and send a request to read/write.
17319  * Since the ilt for the function that is down is not valid, this will cause
17320  * a translation error which is unrecoverable.
17321  * The Workaround is intended to make sure that when this happens nothing
17322  * fatal will occur. The workaround:
17323  *  1.  First PF driver which loads on a path will:
17324  *      a.  After taking the chip out of reset, by using pretend,
17325  *          it will write "0" to the following registers of
17326  *          the other vnics.
17327  *          REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
17328  *          REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
17329  *          REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
17330  *          And for itself it will write '1' to
17331  *          PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
17332  *          dmae-operations (writing to pram for example.)
17333  *          note: can be done for only function 6,7 but cleaner this
17334  *            way.
17335  *      b.  Write zero+valid to the entire ILT.
17336  *      c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
17337  *          VNIC3 (of that port). The range allocated will be the
17338  *          entire ILT. This is needed to prevent  ILT range error.
17339  *  2.  Any PF driver load flow:
17340  *      a.  ILT update with the physical addresses of the allocated
17341  *          logical pages.
17342  *      b.  Wait 20msec. - note that this timeout is needed to make
17343  *          sure there are no requests in one of the PXP internal
17344  *          queues with "old" ILT addresses.
17345  *      c.  PF enable in the PGLC.
17346  *      d.  Clear the was_error of the PF in the PGLC. (could have
17347  *          occurred while driver was down)
17348  *      e.  PF enable in the CFC (WEAK + STRONG)
17349  *      f.  Timers scan enable
17350  *  3.  PF driver unload flow:
17351  *      a.  Clear the Timers scan_en.
17352  *      b.  Polling for scan_on=0 for that PF.
17353  *      c.  Clear the PF enable bit in the PXP.
17354  *      d.  Clear the PF enable in the CFC (WEAK + STRONG)
17355  *      e.  Write zero+valid to all ILT entries (The valid bit must
17356  *          stay set)
17357  *      f.  If this is VNIC 3 of a port then also init
17358  *          first_timers_ilt_entry to zero and last_timers_ilt_entry
17359  *          to the last enrty in the ILT.
17360  *
17361  *      Notes:
17362  *      Currently the PF error in the PGLC is non recoverable.
17363  *      In the future the there will be a recovery routine for this error.
17364  *      Currently attention is masked.
17365  *      Having an MCP lock on the load/unload process does not guarantee that
17366  *      there is no Timer disable during Func6/7 enable. This is because the
17367  *      Timers scan is currently being cleared by the MCP on FLR.
17368  *      Step 2.d can be done only for PF6/7 and the driver can also check if
17369  *      there is error before clearing it. But the flow above is simpler and
17370  *      more general.
17371  *      All ILT entries are written by zero+valid and not just PF6/7
17372  *      ILT entries since in the future the ILT entries allocation for
17373  *      PF-s might be dynamic.
17374  */
17375         struct ilt_client_info ilt_cli;
17376         struct ecore_ilt ilt;
17377
17378         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
17379         memset(&ilt, 0, sizeof(struct ecore_ilt));
17380
17381         /* initialize dummy TM client */
17382         ilt_cli.start      = 0;
17383         ilt_cli.end        = ILT_NUM_PAGE_ENTRIES - 1;
17384         ilt_cli.client_num = ILT_CLIENT_TM;
17385
17386         /*
17387          * Step 1: set zeroes to all ilt page entries with valid bit on
17388          * Step 2: set the timers first/last ilt entry to point
17389          * to the entire range to prevent ILT range error for 3rd/4th
17390          * vnic (this code assumes existence of the vnic)
17391          *
17392          * both steps performed by call to ecore_ilt_client_init_op()
17393          * with dummy TM client
17394          *
17395          * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
17396          * and his brother are split registers
17397          */
17398
17399         bxe_pretend_func(sc, (SC_PATH(sc) + 6));
17400         ecore_ilt_client_init_op_ilt(sc, &ilt, &ilt_cli, INITOP_CLEAR);
17401         bxe_pretend_func(sc, SC_ABS_FUNC(sc));
17402
17403         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN, BXE_PXP_DRAM_ALIGN);
17404         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_RD, BXE_PXP_DRAM_ALIGN);
17405         REG_WR(sc, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
17406     }
17407
17408     REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
17409     REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
17410
17411     if (!CHIP_IS_E1x(sc)) {
17412         int factor = CHIP_REV_IS_EMUL(sc) ? 1000 :
17413                      (CHIP_REV_IS_FPGA(sc) ? 400 : 0);
17414
17415         ecore_init_block(sc, BLOCK_PGLUE_B, PHASE_COMMON);
17416         ecore_init_block(sc, BLOCK_ATC, PHASE_COMMON);
17417
17418         /* let the HW do it's magic... */
17419         do {
17420             DELAY(200000);
17421             val = REG_RD(sc, ATC_REG_ATC_INIT_DONE);
17422         } while (factor-- && (val != 1));
17423
17424         if (val != 1) {
17425             BLOGE(sc, "ATC_INIT failed\n");
17426             return (-1);
17427         }
17428     }
17429
17430     BLOGD(sc, DBG_LOAD, "after pglue and atc init\n");
17431
17432     ecore_init_block(sc, BLOCK_DMAE, PHASE_COMMON);
17433
17434     bxe_iov_init_dmae(sc);
17435
17436     /* clean the DMAE memory */
17437     sc->dmae_ready = 1;
17438     ecore_init_fill(sc, TSEM_REG_PRAM, 0, 8, 1);
17439
17440     ecore_init_block(sc, BLOCK_TCM, PHASE_COMMON);
17441
17442     ecore_init_block(sc, BLOCK_UCM, PHASE_COMMON);
17443
17444     ecore_init_block(sc, BLOCK_CCM, PHASE_COMMON);
17445
17446     ecore_init_block(sc, BLOCK_XCM, PHASE_COMMON);
17447
17448     bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
17449     bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
17450     bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
17451     bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
17452
17453     ecore_init_block(sc, BLOCK_QM, PHASE_COMMON);
17454
17455     /* QM queues pointers table */
17456     ecore_qm_init_ptr_table(sc, sc->qm_cid_count, INITOP_SET);
17457
17458     /* soft reset pulse */
17459     REG_WR(sc, QM_REG_SOFT_RESET, 1);
17460     REG_WR(sc, QM_REG_SOFT_RESET, 0);
17461
17462     if (CNIC_SUPPORT(sc))
17463         ecore_init_block(sc, BLOCK_TM, PHASE_COMMON);
17464
17465     ecore_init_block(sc, BLOCK_DORQ, PHASE_COMMON);
17466     REG_WR(sc, DORQ_REG_DPM_CID_OFST, BXE_DB_SHIFT);
17467     if (!CHIP_REV_IS_SLOW(sc)) {
17468         /* enable hw interrupt from doorbell Q */
17469         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
17470     }
17471
17472     ecore_init_block(sc, BLOCK_BRB1, PHASE_COMMON);
17473
17474     ecore_init_block(sc, BLOCK_PRS, PHASE_COMMON);
17475     REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
17476
17477     if (!CHIP_IS_E1(sc)) {
17478         REG_WR(sc, PRS_REG_E1HOV_MODE, sc->devinfo.mf_info.path_has_ovlan);
17479     }
17480
17481     if (!CHIP_IS_E1x(sc) && !CHIP_IS_E3B0(sc)) {
17482         if (IS_MF_AFEX(sc)) {
17483             /*
17484              * configure that AFEX and VLAN headers must be
17485              * received in AFEX mode
17486              */
17487             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC, 0xE);
17488             REG_WR(sc, PRS_REG_MUST_HAVE_HDRS, 0xA);
17489             REG_WR(sc, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
17490             REG_WR(sc, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
17491             REG_WR(sc, PRS_REG_TAG_LEN_0, 0x4);
17492         } else {
17493             /*
17494              * Bit-map indicating which L2 hdrs may appear
17495              * after the basic Ethernet header
17496              */
17497             REG_WR(sc, PRS_REG_HDRS_AFTER_BASIC,
17498                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17499         }
17500     }
17501
17502     ecore_init_block(sc, BLOCK_TSDM, PHASE_COMMON);
17503     ecore_init_block(sc, BLOCK_CSDM, PHASE_COMMON);
17504     ecore_init_block(sc, BLOCK_USDM, PHASE_COMMON);
17505     ecore_init_block(sc, BLOCK_XSDM, PHASE_COMMON);
17506
17507     if (!CHIP_IS_E1x(sc)) {
17508         /* reset VFC memories */
17509         REG_WR(sc, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17510                VFC_MEMORIES_RST_REG_CAM_RST |
17511                VFC_MEMORIES_RST_REG_RAM_RST);
17512         REG_WR(sc, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
17513                VFC_MEMORIES_RST_REG_CAM_RST |
17514                VFC_MEMORIES_RST_REG_RAM_RST);
17515
17516         DELAY(20000);
17517     }
17518
17519     ecore_init_block(sc, BLOCK_TSEM, PHASE_COMMON);
17520     ecore_init_block(sc, BLOCK_USEM, PHASE_COMMON);
17521     ecore_init_block(sc, BLOCK_CSEM, PHASE_COMMON);
17522     ecore_init_block(sc, BLOCK_XSEM, PHASE_COMMON);
17523
17524     /* sync semi rtc */
17525     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
17526            0x80000000);
17527     REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
17528            0x80000000);
17529
17530     ecore_init_block(sc, BLOCK_UPB, PHASE_COMMON);
17531     ecore_init_block(sc, BLOCK_XPB, PHASE_COMMON);
17532     ecore_init_block(sc, BLOCK_PBF, PHASE_COMMON);
17533
17534     if (!CHIP_IS_E1x(sc)) {
17535         if (IS_MF_AFEX(sc)) {
17536             /*
17537              * configure that AFEX and VLAN headers must be
17538              * sent in AFEX mode
17539              */
17540             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC, 0xE);
17541             REG_WR(sc, PBF_REG_MUST_HAVE_HDRS, 0xA);
17542             REG_WR(sc, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
17543             REG_WR(sc, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
17544             REG_WR(sc, PBF_REG_TAG_LEN_0, 0x4);
17545         } else {
17546             REG_WR(sc, PBF_REG_HDRS_AFTER_BASIC,
17547                    sc->devinfo.mf_info.path_has_ovlan ? 7 : 6);
17548         }
17549     }
17550
17551     REG_WR(sc, SRC_REG_SOFT_RST, 1);
17552
17553     ecore_init_block(sc, BLOCK_SRC, PHASE_COMMON);
17554
17555     if (CNIC_SUPPORT(sc)) {
17556         REG_WR(sc, SRC_REG_KEYSEARCH_0, 0x63285672);
17557         REG_WR(sc, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
17558         REG_WR(sc, SRC_REG_KEYSEARCH_2, 0x223aef9b);
17559         REG_WR(sc, SRC_REG_KEYSEARCH_3, 0x26001e3a);
17560         REG_WR(sc, SRC_REG_KEYSEARCH_4, 0x7ae91116);
17561         REG_WR(sc, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
17562         REG_WR(sc, SRC_REG_KEYSEARCH_6, 0x298d8adf);
17563         REG_WR(sc, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
17564         REG_WR(sc, SRC_REG_KEYSEARCH_8, 0x1830f82f);
17565         REG_WR(sc, SRC_REG_KEYSEARCH_9, 0x01e46be7);
17566     }
17567     REG_WR(sc, SRC_REG_SOFT_RST, 0);
17568
17569     if (sizeof(union cdu_context) != 1024) {
17570         /* we currently assume that a context is 1024 bytes */
17571         BLOGE(sc, "please adjust the size of cdu_context(%ld)\n",
17572               (long)sizeof(union cdu_context));
17573     }
17574
17575     ecore_init_block(sc, BLOCK_CDU, PHASE_COMMON);
17576     val = (4 << 24) + (0 << 12) + 1024;
17577     REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
17578
17579     ecore_init_block(sc, BLOCK_CFC, PHASE_COMMON);
17580
17581     REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
17582     /* enable context validation interrupt from CFC */
17583     REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
17584
17585     /* set the thresholds to prevent CFC/CDU race */
17586     REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
17587     ecore_init_block(sc, BLOCK_HC, PHASE_COMMON);
17588
17589     if (!CHIP_IS_E1x(sc) && BXE_NOMCP(sc)) {
17590         REG_WR(sc, IGU_REG_RESET_MEMORIES, 0x36);
17591     }
17592
17593     ecore_init_block(sc, BLOCK_IGU, PHASE_COMMON);
17594     ecore_init_block(sc, BLOCK_MISC_AEU, PHASE_COMMON);
17595
17596     /* Reset PCIE errors for debug */
17597     REG_WR(sc, 0x2814, 0xffffffff);
17598     REG_WR(sc, 0x3820, 0xffffffff);
17599
17600     if (!CHIP_IS_E1x(sc)) {
17601         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
17602                (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
17603                 PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
17604         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
17605                (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
17606                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
17607                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
17608         REG_WR(sc, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
17609                (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
17610                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
17611                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
17612     }
17613
17614     ecore_init_block(sc, BLOCK_NIG, PHASE_COMMON);
17615
17616     if (!CHIP_IS_E1(sc)) {
17617         /* in E3 this done in per-port section */
17618         if (!CHIP_IS_E3(sc))
17619             REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_MF(sc));
17620     }
17621
17622     if (CHIP_IS_E1H(sc)) {
17623         /* not applicable for E2 (and above ...) */
17624         REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(sc));
17625     }
17626
17627     if (CHIP_REV_IS_SLOW(sc)) {
17628         DELAY(200000);
17629     }
17630
17631     /* finish CFC init */
17632     val = reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
17633     if (val != 1) {
17634         BLOGE(sc, "CFC LL_INIT failed\n");
17635         return (-1);
17636     }
17637     val = reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
17638     if (val != 1) {
17639         BLOGE(sc, "CFC AC_INIT failed\n");
17640         return (-1);
17641     }
17642     val = reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
17643     if (val != 1) {
17644         BLOGE(sc, "CFC CAM_INIT failed\n");
17645         return (-1);
17646     }
17647     REG_WR(sc, CFC_REG_DEBUG0, 0);
17648
17649     if (CHIP_IS_E1(sc)) {
17650         /* read NIG statistic to see if this is our first up since powerup */
17651         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
17652         val = *BXE_SP(sc, wb_data[0]);
17653
17654         /* do internal memory self test */
17655         if ((val == 0) && bxe_int_mem_test(sc)) {
17656             BLOGE(sc, "internal mem self test failed\n");
17657             return (-1);
17658         }
17659     }
17660
17661     bxe_setup_fan_failure_detection(sc);
17662
17663     /* clear PXP2 attentions */
17664     REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
17665
17666     bxe_enable_blocks_attention(sc);
17667
17668     if (!CHIP_REV_IS_SLOW(sc)) {
17669         ecore_enable_blocks_parity(sc);
17670     }
17671
17672     if (!BXE_NOMCP(sc)) {
17673         if (CHIP_IS_E1x(sc)) {
17674             bxe_common_init_phy(sc);
17675         }
17676     }
17677
17678     return (0);
17679 }
17680
17681 /**
17682  * bxe_init_hw_common_chip - init HW at the COMMON_CHIP phase.
17683  *
17684  * @sc:     driver handle
17685  */
17686 static int
17687 bxe_init_hw_common_chip(struct bxe_softc *sc)
17688 {
17689     int rc = bxe_init_hw_common(sc);
17690
17691     if (rc) {
17692         return (rc);
17693     }
17694
17695     /* In E2 2-PORT mode, same ext phy is used for the two paths */
17696     if (!BXE_NOMCP(sc)) {
17697         bxe_common_init_phy(sc);
17698     }
17699
17700     return (0);
17701 }
17702
17703 static int
17704 bxe_init_hw_port(struct bxe_softc *sc)
17705 {
17706     int port = SC_PORT(sc);
17707     int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
17708     uint32_t low, high;
17709     uint32_t val;
17710
17711     BLOGD(sc, DBG_LOAD, "starting port init for port %d\n", port);
17712
17713     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
17714
17715     ecore_init_block(sc, BLOCK_MISC, init_phase);
17716     ecore_init_block(sc, BLOCK_PXP, init_phase);
17717     ecore_init_block(sc, BLOCK_PXP2, init_phase);
17718
17719     /*
17720      * Timers bug workaround: disables the pf_master bit in pglue at
17721      * common phase, we need to enable it here before any dmae access are
17722      * attempted. Therefore we manually added the enable-master to the
17723      * port phase (it also happens in the function phase)
17724      */
17725     if (!CHIP_IS_E1x(sc)) {
17726         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
17727     }
17728
17729     ecore_init_block(sc, BLOCK_ATC, init_phase);
17730     ecore_init_block(sc, BLOCK_DMAE, init_phase);
17731     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
17732     ecore_init_block(sc, BLOCK_QM, init_phase);
17733
17734     ecore_init_block(sc, BLOCK_TCM, init_phase);
17735     ecore_init_block(sc, BLOCK_UCM, init_phase);
17736     ecore_init_block(sc, BLOCK_CCM, init_phase);
17737     ecore_init_block(sc, BLOCK_XCM, init_phase);
17738
17739     /* QM cid (connection) count */
17740     ecore_qm_init_cid_count(sc, sc->qm_cid_count, INITOP_SET);
17741
17742     if (CNIC_SUPPORT(sc)) {
17743         ecore_init_block(sc, BLOCK_TM, init_phase);
17744         REG_WR(sc, TM_REG_LIN0_SCAN_TIME + port*4, 20);
17745         REG_WR(sc, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
17746     }
17747
17748     ecore_init_block(sc, BLOCK_DORQ, init_phase);
17749
17750     ecore_init_block(sc, BLOCK_BRB1, init_phase);
17751
17752     if (CHIP_IS_E1(sc) || CHIP_IS_E1H(sc)) {
17753         if (IS_MF(sc)) {
17754             low = (BXE_ONE_PORT(sc) ? 160 : 246);
17755         } else if (sc->mtu > 4096) {
17756             if (BXE_ONE_PORT(sc)) {
17757                 low = 160;
17758             } else {
17759                 val = sc->mtu;
17760                 /* (24*1024 + val*4)/256 */
17761                 low = (96 + (val / 64) + ((val % 64) ? 1 : 0));
17762             }
17763         } else {
17764             low = (BXE_ONE_PORT(sc) ? 80 : 160);
17765         }
17766         high = (low + 56); /* 14*1024/256 */
17767         REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
17768         REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
17769     }
17770
17771     if (CHIP_IS_MODE_4_PORT(sc)) {
17772         REG_WR(sc, SC_PORT(sc) ?
17773                BRB1_REG_MAC_GUARANTIED_1 :
17774                BRB1_REG_MAC_GUARANTIED_0, 40);
17775     }
17776
17777     ecore_init_block(sc, BLOCK_PRS, init_phase);
17778     if (CHIP_IS_E3B0(sc)) {
17779         if (IS_MF_AFEX(sc)) {
17780             /* configure headers for AFEX mode */
17781             REG_WR(sc, SC_PORT(sc) ?
17782                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17783                    PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
17784             REG_WR(sc, SC_PORT(sc) ?
17785                    PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
17786                    PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
17787             REG_WR(sc, SC_PORT(sc) ?
17788                    PRS_REG_MUST_HAVE_HDRS_PORT_1 :
17789                    PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
17790         } else {
17791             /* Ovlan exists only if we are in multi-function +
17792              * switch-dependent mode, in switch-independent there
17793              * is no ovlan headers
17794              */
17795             REG_WR(sc, SC_PORT(sc) ?
17796                    PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
17797                    PRS_REG_HDRS_AFTER_BASIC_PORT_0,
17798                    (sc->devinfo.mf_info.path_has_ovlan ? 7 : 6));
17799         }
17800     }
17801
17802     ecore_init_block(sc, BLOCK_TSDM, init_phase);
17803     ecore_init_block(sc, BLOCK_CSDM, init_phase);
17804     ecore_init_block(sc, BLOCK_USDM, init_phase);
17805     ecore_init_block(sc, BLOCK_XSDM, init_phase);
17806
17807     ecore_init_block(sc, BLOCK_TSEM, init_phase);
17808     ecore_init_block(sc, BLOCK_USEM, init_phase);
17809     ecore_init_block(sc, BLOCK_CSEM, init_phase);
17810     ecore_init_block(sc, BLOCK_XSEM, init_phase);
17811
17812     ecore_init_block(sc, BLOCK_UPB, init_phase);
17813     ecore_init_block(sc, BLOCK_XPB, init_phase);
17814
17815     ecore_init_block(sc, BLOCK_PBF, init_phase);
17816
17817     if (CHIP_IS_E1x(sc)) {
17818         /* configure PBF to work without PAUSE mtu 9000 */
17819         REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
17820
17821         /* update threshold */
17822         REG_WR(sc, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
17823         /* update init credit */
17824         REG_WR(sc, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
17825
17826         /* probe changes */
17827         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 1);
17828         DELAY(50);
17829         REG_WR(sc, PBF_REG_INIT_P0 + port*4, 0);
17830     }
17831
17832     if (CNIC_SUPPORT(sc)) {
17833         ecore_init_block(sc, BLOCK_SRC, init_phase);
17834     }
17835
17836     ecore_init_block(sc, BLOCK_CDU, init_phase);
17837     ecore_init_block(sc, BLOCK_CFC, init_phase);
17838
17839     if (CHIP_IS_E1(sc)) {
17840         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
17841         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
17842     }
17843     ecore_init_block(sc, BLOCK_HC, init_phase);
17844
17845     ecore_init_block(sc, BLOCK_IGU, init_phase);
17846
17847     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
17848     /* init aeu_mask_attn_func_0/1:
17849      *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
17850      *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
17851      *             bits 4-7 are used for "per vn group attention" */
17852     val = IS_MF(sc) ? 0xF7 : 0x7;
17853     /* Enable DCBX attention for all but E1 */
17854     val |= CHIP_IS_E1(sc) ? 0 : 0x10;
17855     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
17856
17857     ecore_init_block(sc, BLOCK_NIG, init_phase);
17858
17859     if (!CHIP_IS_E1x(sc)) {
17860         /* Bit-map indicating which L2 hdrs may appear after the
17861          * basic Ethernet header
17862          */
17863         if (IS_MF_AFEX(sc)) {
17864             REG_WR(sc, SC_PORT(sc) ?
17865                    NIG_REG_P1_HDRS_AFTER_BASIC :
17866                    NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
17867         } else {
17868             REG_WR(sc, SC_PORT(sc) ?
17869                    NIG_REG_P1_HDRS_AFTER_BASIC :
17870                    NIG_REG_P0_HDRS_AFTER_BASIC,
17871                    IS_MF_SD(sc) ? 7 : 6);
17872         }
17873
17874         if (CHIP_IS_E3(sc)) {
17875             REG_WR(sc, SC_PORT(sc) ?
17876                    NIG_REG_LLH1_MF_MODE :
17877                    NIG_REG_LLH_MF_MODE, IS_MF(sc));
17878         }
17879     }
17880     if (!CHIP_IS_E3(sc)) {
17881         REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
17882     }
17883
17884     if (!CHIP_IS_E1(sc)) {
17885         /* 0x2 disable mf_ov, 0x1 enable */
17886         REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
17887                (IS_MF_SD(sc) ? 0x1 : 0x2));
17888
17889         if (!CHIP_IS_E1x(sc)) {
17890             val = 0;
17891             switch (sc->devinfo.mf_info.mf_mode) {
17892             case MULTI_FUNCTION_SD:
17893                 val = 1;
17894                 break;
17895             case MULTI_FUNCTION_SI:
17896             case MULTI_FUNCTION_AFEX:
17897                 val = 2;
17898                 break;
17899             }
17900
17901             REG_WR(sc, (SC_PORT(sc) ? NIG_REG_LLH1_CLS_TYPE :
17902                         NIG_REG_LLH0_CLS_TYPE), val);
17903         }
17904         REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
17905         REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
17906         REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
17907     }
17908
17909     /* If SPIO5 is set to generate interrupts, enable it for this port */
17910     val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
17911     if (val & MISC_SPIO_SPIO5) {
17912         uint32_t reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
17913                                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
17914         val = REG_RD(sc, reg_addr);
17915         val |= AEU_INPUTS_ATTN_BITS_SPIO5;
17916         REG_WR(sc, reg_addr, val);
17917     }
17918
17919     return (0);
17920 }
17921
17922 static uint32_t
17923 bxe_flr_clnup_reg_poll(struct bxe_softc *sc,
17924                        uint32_t         reg,
17925                        uint32_t         expected,
17926                        uint32_t         poll_count)
17927 {
17928     uint32_t cur_cnt = poll_count;
17929     uint32_t val;
17930
17931     while ((val = REG_RD(sc, reg)) != expected && cur_cnt--) {
17932         DELAY(FLR_WAIT_INTERVAL);
17933     }
17934
17935     return (val);
17936 }
17937
17938 static int
17939 bxe_flr_clnup_poll_hw_counter(struct bxe_softc *sc,
17940                               uint32_t         reg,
17941                               char             *msg,
17942                               uint32_t         poll_cnt)
17943 {
17944     uint32_t val = bxe_flr_clnup_reg_poll(sc, reg, 0, poll_cnt);
17945
17946     if (val != 0) {
17947         BLOGE(sc, "%s usage count=%d\n", msg, val);
17948         return (1);
17949     }
17950
17951     return (0);
17952 }
17953
17954 /* Common routines with VF FLR cleanup */
17955 static uint32_t
17956 bxe_flr_clnup_poll_count(struct bxe_softc *sc)
17957 {
17958     /* adjust polling timeout */
17959     if (CHIP_REV_IS_EMUL(sc)) {
17960         return (FLR_POLL_CNT * 2000);
17961     }
17962
17963     if (CHIP_REV_IS_FPGA(sc)) {
17964         return (FLR_POLL_CNT * 120);
17965     }
17966
17967     return (FLR_POLL_CNT);
17968 }
17969
17970 static int
17971 bxe_poll_hw_usage_counters(struct bxe_softc *sc,
17972                            uint32_t         poll_cnt)
17973 {
17974     /* wait for CFC PF usage-counter to zero (includes all the VFs) */
17975     if (bxe_flr_clnup_poll_hw_counter(sc,
17976                                       CFC_REG_NUM_LCIDS_INSIDE_PF,
17977                                       "CFC PF usage counter timed out",
17978                                       poll_cnt)) {
17979         return (1);
17980     }
17981
17982     /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
17983     if (bxe_flr_clnup_poll_hw_counter(sc,
17984                                       DORQ_REG_PF_USAGE_CNT,
17985                                       "DQ PF usage counter timed out",
17986                                       poll_cnt)) {
17987         return (1);
17988     }
17989
17990     /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
17991     if (bxe_flr_clnup_poll_hw_counter(sc,
17992                                       QM_REG_PF_USG_CNT_0 + 4*SC_FUNC(sc),
17993                                       "QM PF usage counter timed out",
17994                                       poll_cnt)) {
17995         return (1);
17996     }
17997
17998     /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
17999     if (bxe_flr_clnup_poll_hw_counter(sc,
18000                                       TM_REG_LIN0_VNIC_UC + 4*SC_PORT(sc),
18001                                       "Timers VNIC usage counter timed out",
18002                                       poll_cnt)) {
18003         return (1);
18004     }
18005
18006     if (bxe_flr_clnup_poll_hw_counter(sc,
18007                                       TM_REG_LIN0_NUM_SCANS + 4*SC_PORT(sc),
18008                                       "Timers NUM_SCANS usage counter timed out",
18009                                       poll_cnt)) {
18010         return (1);
18011     }
18012
18013     /* Wait DMAE PF usage counter to zero */
18014     if (bxe_flr_clnup_poll_hw_counter(sc,
18015                                       dmae_reg_go_c[INIT_DMAE_C(sc)],
18016                                       "DMAE dommand register timed out",
18017                                       poll_cnt)) {
18018         return (1);
18019     }
18020
18021     return (0);
18022 }
18023
18024 #define OP_GEN_PARAM(param)                                            \
18025     (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
18026 #define OP_GEN_TYPE(type)                                           \
18027     (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
18028 #define OP_GEN_AGG_VECT(index)                                             \
18029     (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
18030
18031 static int
18032 bxe_send_final_clnup(struct bxe_softc *sc,
18033                      uint8_t          clnup_func,
18034                      uint32_t         poll_cnt)
18035 {
18036     uint32_t op_gen_command = 0;
18037     uint32_t comp_addr = (BAR_CSTRORM_INTMEM +
18038                           CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func));
18039     int ret = 0;
18040
18041     if (REG_RD(sc, comp_addr)) {
18042         BLOGE(sc, "Cleanup complete was not 0 before sending\n");
18043         return (1);
18044     }
18045
18046     op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
18047     op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
18048     op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
18049     op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
18050
18051     BLOGD(sc, DBG_LOAD, "sending FW Final cleanup\n");
18052     REG_WR(sc, XSDM_REG_OPERATION_GEN, op_gen_command);
18053
18054     if (bxe_flr_clnup_reg_poll(sc, comp_addr, 1, poll_cnt) != 1) {
18055         BLOGE(sc, "FW final cleanup did not succeed\n");
18056         BLOGD(sc, DBG_LOAD, "At timeout completion address contained %x\n",
18057               (REG_RD(sc, comp_addr)));
18058         bxe_panic(sc, ("FLR cleanup failed\n"));
18059         return (1);
18060     }
18061
18062     /* Zero completion for nxt FLR */
18063     REG_WR(sc, comp_addr, 0);
18064
18065     return (ret);
18066 }
18067
18068 static void
18069 bxe_pbf_pN_buf_flushed(struct bxe_softc       *sc,
18070                        struct pbf_pN_buf_regs *regs,
18071                        uint32_t               poll_count)
18072 {
18073     uint32_t init_crd, crd, crd_start, crd_freed, crd_freed_start;
18074     uint32_t cur_cnt = poll_count;
18075
18076     crd_freed = crd_freed_start = REG_RD(sc, regs->crd_freed);
18077     crd = crd_start = REG_RD(sc, regs->crd);
18078     init_crd = REG_RD(sc, regs->init_crd);
18079
18080     BLOGD(sc, DBG_LOAD, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
18081     BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
18082     BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
18083
18084     while ((crd != init_crd) &&
18085            ((uint32_t)((int32_t)crd_freed - (int32_t)crd_freed_start) <
18086             (init_crd - crd_start))) {
18087         if (cur_cnt--) {
18088             DELAY(FLR_WAIT_INTERVAL);
18089             crd = REG_RD(sc, regs->crd);
18090             crd_freed = REG_RD(sc, regs->crd_freed);
18091         } else {
18092             BLOGD(sc, DBG_LOAD, "PBF tx buffer[%d] timed out\n", regs->pN);
18093             BLOGD(sc, DBG_LOAD, "CREDIT[%d]      : c:%x\n", regs->pN, crd);
18094             BLOGD(sc, DBG_LOAD, "CREDIT_FREED[%d]: c:%x\n", regs->pN, crd_freed);
18095             break;
18096         }
18097     }
18098
18099     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF tx buffer[%d]\n",
18100           poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18101 }
18102
18103 static void
18104 bxe_pbf_pN_cmd_flushed(struct bxe_softc       *sc,
18105                        struct pbf_pN_cmd_regs *regs,
18106                        uint32_t               poll_count)
18107 {
18108     uint32_t occup, to_free, freed, freed_start;
18109     uint32_t cur_cnt = poll_count;
18110
18111     occup = to_free = REG_RD(sc, regs->lines_occup);
18112     freed = freed_start = REG_RD(sc, regs->lines_freed);
18113
18114     BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18115     BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18116
18117     while (occup &&
18118            ((uint32_t)((int32_t)freed - (int32_t)freed_start) < to_free)) {
18119         if (cur_cnt--) {
18120             DELAY(FLR_WAIT_INTERVAL);
18121             occup = REG_RD(sc, regs->lines_occup);
18122             freed = REG_RD(sc, regs->lines_freed);
18123         } else {
18124             BLOGD(sc, DBG_LOAD, "PBF cmd queue[%d] timed out\n", regs->pN);
18125             BLOGD(sc, DBG_LOAD, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
18126             BLOGD(sc, DBG_LOAD, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
18127             break;
18128         }
18129     }
18130
18131     BLOGD(sc, DBG_LOAD, "Waited %d*%d usec for PBF cmd queue[%d]\n",
18132           poll_count - cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
18133 }
18134
18135 static void
18136 bxe_tx_hw_flushed(struct bxe_softc *sc, uint32_t poll_count)
18137 {
18138     struct pbf_pN_cmd_regs cmd_regs[] = {
18139         {0, (CHIP_IS_E3B0(sc)) ?
18140             PBF_REG_TQ_OCCUPANCY_Q0 :
18141             PBF_REG_P0_TQ_OCCUPANCY,
18142             (CHIP_IS_E3B0(sc)) ?
18143             PBF_REG_TQ_LINES_FREED_CNT_Q0 :
18144             PBF_REG_P0_TQ_LINES_FREED_CNT},
18145         {1, (CHIP_IS_E3B0(sc)) ?
18146             PBF_REG_TQ_OCCUPANCY_Q1 :
18147             PBF_REG_P1_TQ_OCCUPANCY,
18148             (CHIP_IS_E3B0(sc)) ?
18149             PBF_REG_TQ_LINES_FREED_CNT_Q1 :
18150             PBF_REG_P1_TQ_LINES_FREED_CNT},
18151         {4, (CHIP_IS_E3B0(sc)) ?
18152             PBF_REG_TQ_OCCUPANCY_LB_Q :
18153             PBF_REG_P4_TQ_OCCUPANCY,
18154             (CHIP_IS_E3B0(sc)) ?
18155             PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
18156             PBF_REG_P4_TQ_LINES_FREED_CNT}
18157     };
18158
18159     struct pbf_pN_buf_regs buf_regs[] = {
18160         {0, (CHIP_IS_E3B0(sc)) ?
18161             PBF_REG_INIT_CRD_Q0 :
18162             PBF_REG_P0_INIT_CRD ,
18163             (CHIP_IS_E3B0(sc)) ?
18164             PBF_REG_CREDIT_Q0 :
18165             PBF_REG_P0_CREDIT,
18166             (CHIP_IS_E3B0(sc)) ?
18167             PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
18168             PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
18169         {1, (CHIP_IS_E3B0(sc)) ?
18170             PBF_REG_INIT_CRD_Q1 :
18171             PBF_REG_P1_INIT_CRD,
18172             (CHIP_IS_E3B0(sc)) ?
18173             PBF_REG_CREDIT_Q1 :
18174             PBF_REG_P1_CREDIT,
18175             (CHIP_IS_E3B0(sc)) ?
18176             PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
18177             PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
18178         {4, (CHIP_IS_E3B0(sc)) ?
18179             PBF_REG_INIT_CRD_LB_Q :
18180             PBF_REG_P4_INIT_CRD,
18181             (CHIP_IS_E3B0(sc)) ?
18182             PBF_REG_CREDIT_LB_Q :
18183             PBF_REG_P4_CREDIT,
18184             (CHIP_IS_E3B0(sc)) ?
18185             PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
18186             PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
18187     };
18188
18189     int i;
18190
18191     /* Verify the command queues are flushed P0, P1, P4 */
18192     for (i = 0; i < ARRAY_SIZE(cmd_regs); i++) {
18193         bxe_pbf_pN_cmd_flushed(sc, &cmd_regs[i], poll_count);
18194     }
18195
18196     /* Verify the transmission buffers are flushed P0, P1, P4 */
18197     for (i = 0; i < ARRAY_SIZE(buf_regs); i++) {
18198         bxe_pbf_pN_buf_flushed(sc, &buf_regs[i], poll_count);
18199     }
18200 }
18201
18202 static void
18203 bxe_hw_enable_status(struct bxe_softc *sc)
18204 {
18205     uint32_t val;
18206
18207     val = REG_RD(sc, CFC_REG_WEAK_ENABLE_PF);
18208     BLOGD(sc, DBG_LOAD, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
18209
18210     val = REG_RD(sc, PBF_REG_DISABLE_PF);
18211     BLOGD(sc, DBG_LOAD, "PBF_REG_DISABLE_PF is 0x%x\n", val);
18212
18213     val = REG_RD(sc, IGU_REG_PCI_PF_MSI_EN);
18214     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
18215
18216     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_EN);
18217     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
18218
18219     val = REG_RD(sc, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
18220     BLOGD(sc, DBG_LOAD, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
18221
18222     val = REG_RD(sc, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
18223     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
18224
18225     val = REG_RD(sc, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
18226     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
18227
18228     val = REG_RD(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
18229     BLOGD(sc, DBG_LOAD, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n", val);
18230 }
18231
18232 static int
18233 bxe_pf_flr_clnup(struct bxe_softc *sc)
18234 {
18235     uint32_t poll_cnt = bxe_flr_clnup_poll_count(sc);
18236
18237     BLOGD(sc, DBG_LOAD, "Cleanup after FLR PF[%d]\n", SC_ABS_FUNC(sc));
18238
18239     /* Re-enable PF target read access */
18240     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
18241
18242     /* Poll HW usage counters */
18243     BLOGD(sc, DBG_LOAD, "Polling usage counters\n");
18244     if (bxe_poll_hw_usage_counters(sc, poll_cnt)) {
18245         return (-1);
18246     }
18247
18248     /* Zero the igu 'trailing edge' and 'leading edge' */
18249
18250     /* Send the FW cleanup command */
18251     if (bxe_send_final_clnup(sc, (uint8_t)SC_FUNC(sc), poll_cnt)) {
18252         return (-1);
18253     }
18254
18255     /* ATC cleanup */
18256
18257     /* Verify TX hw is flushed */
18258     bxe_tx_hw_flushed(sc, poll_cnt);
18259
18260     /* Wait 100ms (not adjusted according to platform) */
18261     DELAY(100000);
18262
18263     /* Verify no pending pci transactions */
18264     if (bxe_is_pcie_pending(sc)) {
18265         BLOGE(sc, "PCIE Transactions still pending\n");
18266     }
18267
18268     /* Debug */
18269     bxe_hw_enable_status(sc);
18270
18271     /*
18272      * Master enable - Due to WB DMAE writes performed before this
18273      * register is re-initialized as part of the regular function init
18274      */
18275     REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18276
18277     return (0);
18278 }
18279
18280 #if 0
18281 static void
18282 bxe_init_searcher(struct bxe_softc *sc)
18283 {
18284     int port = SC_PORT(sc);
18285     ecore_src_init_t2(sc, sc->t2, sc->t2_mapping, SRC_CONN_NUM);
18286     /* T1 hash bits value determines the T1 number of entries */
18287     REG_WR(sc, SRC_REG_NUMBER_HASH_BITS0 + port*4, SRC_HASH_BITS);
18288 }
18289 #endif
18290
18291 static int
18292 bxe_init_hw_func(struct bxe_softc *sc)
18293 {
18294     int port = SC_PORT(sc);
18295     int func = SC_FUNC(sc);
18296     int init_phase = PHASE_PF0 + func;
18297     struct ecore_ilt *ilt = sc->ilt;
18298     uint16_t cdu_ilt_start;
18299     uint32_t addr, val;
18300     uint32_t main_mem_base, main_mem_size, main_mem_prty_clr;
18301     int i, main_mem_width, rc;
18302
18303     BLOGD(sc, DBG_LOAD, "starting func init for func %d\n", func);
18304
18305     /* FLR cleanup */
18306     if (!CHIP_IS_E1x(sc)) {
18307         rc = bxe_pf_flr_clnup(sc);
18308         if (rc) {
18309             BLOGE(sc, "FLR cleanup failed!\n");
18310             // XXX bxe_fw_dump(sc);
18311             // XXX bxe_idle_chk(sc);
18312             return (rc);
18313         }
18314     }
18315
18316     /* set MSI reconfigure capability */
18317     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18318         addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
18319         val = REG_RD(sc, addr);
18320         val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
18321         REG_WR(sc, addr, val);
18322     }
18323
18324     ecore_init_block(sc, BLOCK_PXP, init_phase);
18325     ecore_init_block(sc, BLOCK_PXP2, init_phase);
18326
18327     ilt = sc->ilt;
18328     cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
18329
18330 #if 0
18331     if (IS_SRIOV(sc)) {
18332         cdu_ilt_start += BXE_FIRST_VF_CID/ILT_PAGE_CIDS;
18333     }
18334     cdu_ilt_start = bxe_iov_init_ilt(sc, cdu_ilt_start);
18335
18336 #if (BXE_FIRST_VF_CID > 0)
18337     /*
18338      * If BXE_FIRST_VF_CID > 0 then the PF L2 cids precedes
18339      * those of the VFs, so start line should be reset
18340      */
18341     cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
18342 #endif
18343 #endif
18344
18345     for (i = 0; i < L2_ILT_LINES(sc); i++) {
18346         ilt->lines[cdu_ilt_start + i].page = sc->context[i].vcxt;
18347         ilt->lines[cdu_ilt_start + i].page_mapping =
18348             sc->context[i].vcxt_dma.paddr;
18349         ilt->lines[cdu_ilt_start + i].size = sc->context[i].size;
18350     }
18351     ecore_ilt_init_op(sc, INITOP_SET);
18352
18353 #if 0
18354     if (!CONFIGURE_NIC_MODE(sc)) {
18355         bxe_init_searcher(sc);
18356         REG_WR(sc, PRS_REG_NIC_MODE, 0);
18357         BLOGD(sc, DBG_LOAD, "NIC MODE disabled\n");
18358     } else
18359 #endif
18360     {
18361         /* Set NIC mode */
18362         REG_WR(sc, PRS_REG_NIC_MODE, 1);
18363         BLOGD(sc, DBG_LOAD, "NIC MODE configured\n");
18364     }
18365
18366     if (!CHIP_IS_E1x(sc)) {
18367         uint32_t pf_conf = IGU_PF_CONF_FUNC_EN;
18368
18369         /* Turn on a single ISR mode in IGU if driver is going to use
18370          * INT#x or MSI
18371          */
18372         if (sc->interrupt_mode != INTR_MODE_MSIX) {
18373             pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
18374         }
18375
18376         /*
18377          * Timers workaround bug: function init part.
18378          * Need to wait 20msec after initializing ILT,
18379          * needed to make sure there are no requests in
18380          * one of the PXP internal queues with "old" ILT addresses
18381          */
18382         DELAY(20000);
18383
18384         /*
18385          * Master enable - Due to WB DMAE writes performed before this
18386          * register is re-initialized as part of the regular function
18387          * init
18388          */
18389         REG_WR(sc, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
18390         /* Enable the function in IGU */
18391         REG_WR(sc, IGU_REG_PF_CONFIGURATION, pf_conf);
18392     }
18393
18394     sc->dmae_ready = 1;
18395
18396     ecore_init_block(sc, BLOCK_PGLUE_B, init_phase);
18397
18398     if (!CHIP_IS_E1x(sc))
18399         REG_WR(sc, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
18400
18401     ecore_init_block(sc, BLOCK_ATC, init_phase);
18402     ecore_init_block(sc, BLOCK_DMAE, init_phase);
18403     ecore_init_block(sc, BLOCK_NIG, init_phase);
18404     ecore_init_block(sc, BLOCK_SRC, init_phase);
18405     ecore_init_block(sc, BLOCK_MISC, init_phase);
18406     ecore_init_block(sc, BLOCK_TCM, init_phase);
18407     ecore_init_block(sc, BLOCK_UCM, init_phase);
18408     ecore_init_block(sc, BLOCK_CCM, init_phase);
18409     ecore_init_block(sc, BLOCK_XCM, init_phase);
18410     ecore_init_block(sc, BLOCK_TSEM, init_phase);
18411     ecore_init_block(sc, BLOCK_USEM, init_phase);
18412     ecore_init_block(sc, BLOCK_CSEM, init_phase);
18413     ecore_init_block(sc, BLOCK_XSEM, init_phase);
18414
18415     if (!CHIP_IS_E1x(sc))
18416         REG_WR(sc, QM_REG_PF_EN, 1);
18417
18418     if (!CHIP_IS_E1x(sc)) {
18419         REG_WR(sc, TSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18420         REG_WR(sc, USEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18421         REG_WR(sc, CSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18422         REG_WR(sc, XSEM_REG_VFPF_ERR_NUM, BXE_MAX_NUM_OF_VFS + func);
18423     }
18424     ecore_init_block(sc, BLOCK_QM, init_phase);
18425
18426     ecore_init_block(sc, BLOCK_TM, init_phase);
18427     ecore_init_block(sc, BLOCK_DORQ, init_phase);
18428
18429     bxe_iov_init_dq(sc);
18430
18431     ecore_init_block(sc, BLOCK_BRB1, init_phase);
18432     ecore_init_block(sc, BLOCK_PRS, init_phase);
18433     ecore_init_block(sc, BLOCK_TSDM, init_phase);
18434     ecore_init_block(sc, BLOCK_CSDM, init_phase);
18435     ecore_init_block(sc, BLOCK_USDM, init_phase);
18436     ecore_init_block(sc, BLOCK_XSDM, init_phase);
18437     ecore_init_block(sc, BLOCK_UPB, init_phase);
18438     ecore_init_block(sc, BLOCK_XPB, init_phase);
18439     ecore_init_block(sc, BLOCK_PBF, init_phase);
18440     if (!CHIP_IS_E1x(sc))
18441         REG_WR(sc, PBF_REG_DISABLE_PF, 0);
18442
18443     ecore_init_block(sc, BLOCK_CDU, init_phase);
18444
18445     ecore_init_block(sc, BLOCK_CFC, init_phase);
18446
18447     if (!CHIP_IS_E1x(sc))
18448         REG_WR(sc, CFC_REG_WEAK_ENABLE_PF, 1);
18449
18450     if (IS_MF(sc)) {
18451         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port*8, 1);
18452         REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, OVLAN(sc));
18453     }
18454
18455     ecore_init_block(sc, BLOCK_MISC_AEU, init_phase);
18456
18457     /* HC init per function */
18458     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18459         if (CHIP_IS_E1H(sc)) {
18460             REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18461
18462             REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18463             REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18464         }
18465         ecore_init_block(sc, BLOCK_HC, init_phase);
18466
18467     } else {
18468         int num_segs, sb_idx, prod_offset;
18469
18470         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
18471
18472         if (!CHIP_IS_E1x(sc)) {
18473             REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18474             REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18475         }
18476
18477         ecore_init_block(sc, BLOCK_IGU, init_phase);
18478
18479         if (!CHIP_IS_E1x(sc)) {
18480             int dsb_idx = 0;
18481             /**
18482              * Producer memory:
18483              * E2 mode: address 0-135 match to the mapping memory;
18484              * 136 - PF0 default prod; 137 - PF1 default prod;
18485              * 138 - PF2 default prod; 139 - PF3 default prod;
18486              * 140 - PF0 attn prod;    141 - PF1 attn prod;
18487              * 142 - PF2 attn prod;    143 - PF3 attn prod;
18488              * 144-147 reserved.
18489              *
18490              * E1.5 mode - In backward compatible mode;
18491              * for non default SB; each even line in the memory
18492              * holds the U producer and each odd line hold
18493              * the C producer. The first 128 producers are for
18494              * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
18495              * producers are for the DSB for each PF.
18496              * Each PF has five segments: (the order inside each
18497              * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
18498              * 132-135 C prods; 136-139 X prods; 140-143 T prods;
18499              * 144-147 attn prods;
18500              */
18501             /* non-default-status-blocks */
18502             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18503                 IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
18504             for (sb_idx = 0; sb_idx < sc->igu_sb_cnt; sb_idx++) {
18505                 prod_offset = (sc->igu_base_sb + sb_idx) *
18506                     num_segs;
18507
18508                 for (i = 0; i < num_segs; i++) {
18509                     addr = IGU_REG_PROD_CONS_MEMORY +
18510                             (prod_offset + i) * 4;
18511                     REG_WR(sc, addr, 0);
18512                 }
18513                 /* send consumer update with value 0 */
18514                 bxe_ack_sb(sc, sc->igu_base_sb + sb_idx,
18515                            USTORM_ID, 0, IGU_INT_NOP, 1);
18516                 bxe_igu_clear_sb(sc, sc->igu_base_sb + sb_idx);
18517             }
18518
18519             /* default-status-blocks */
18520             num_segs = CHIP_INT_MODE_IS_BC(sc) ?
18521                 IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
18522
18523             if (CHIP_IS_MODE_4_PORT(sc))
18524                 dsb_idx = SC_FUNC(sc);
18525             else
18526                 dsb_idx = SC_VN(sc);
18527
18528             prod_offset = (CHIP_INT_MODE_IS_BC(sc) ?
18529                        IGU_BC_BASE_DSB_PROD + dsb_idx :
18530                        IGU_NORM_BASE_DSB_PROD + dsb_idx);
18531
18532             /*
18533              * igu prods come in chunks of E1HVN_MAX (4) -
18534              * does not matters what is the current chip mode
18535              */
18536             for (i = 0; i < (num_segs * E1HVN_MAX);
18537                  i += E1HVN_MAX) {
18538                 addr = IGU_REG_PROD_CONS_MEMORY +
18539                             (prod_offset + i)*4;
18540                 REG_WR(sc, addr, 0);
18541             }
18542             /* send consumer update with 0 */
18543             if (CHIP_INT_MODE_IS_BC(sc)) {
18544                 bxe_ack_sb(sc, sc->igu_dsb_id,
18545                            USTORM_ID, 0, IGU_INT_NOP, 1);
18546                 bxe_ack_sb(sc, sc->igu_dsb_id,
18547                            CSTORM_ID, 0, IGU_INT_NOP, 1);
18548                 bxe_ack_sb(sc, sc->igu_dsb_id,
18549                            XSTORM_ID, 0, IGU_INT_NOP, 1);
18550                 bxe_ack_sb(sc, sc->igu_dsb_id,
18551                            TSTORM_ID, 0, IGU_INT_NOP, 1);
18552                 bxe_ack_sb(sc, sc->igu_dsb_id,
18553                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
18554             } else {
18555                 bxe_ack_sb(sc, sc->igu_dsb_id,
18556                            USTORM_ID, 0, IGU_INT_NOP, 1);
18557                 bxe_ack_sb(sc, sc->igu_dsb_id,
18558                            ATTENTION_ID, 0, IGU_INT_NOP, 1);
18559             }
18560             bxe_igu_clear_sb(sc, sc->igu_dsb_id);
18561
18562             /* !!! these should become driver const once
18563                rf-tool supports split-68 const */
18564             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
18565             REG_WR(sc, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
18566             REG_WR(sc, IGU_REG_SB_MASK_LSB, 0);
18567             REG_WR(sc, IGU_REG_SB_MASK_MSB, 0);
18568             REG_WR(sc, IGU_REG_PBA_STATUS_LSB, 0);
18569             REG_WR(sc, IGU_REG_PBA_STATUS_MSB, 0);
18570         }
18571     }
18572
18573     /* Reset PCIE errors for debug */
18574     REG_WR(sc, 0x2114, 0xffffffff);
18575     REG_WR(sc, 0x2120, 0xffffffff);
18576
18577     if (CHIP_IS_E1x(sc)) {
18578         main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
18579         main_mem_base = HC_REG_MAIN_MEMORY +
18580                 SC_PORT(sc) * (main_mem_size * 4);
18581         main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
18582         main_mem_width = 8;
18583
18584         val = REG_RD(sc, main_mem_prty_clr);
18585         if (val) {
18586             BLOGD(sc, DBG_LOAD,
18587                   "Parity errors in HC block during function init (0x%x)!\n",
18588                   val);
18589         }
18590
18591         /* Clear "false" parity errors in MSI-X table */
18592         for (i = main_mem_base;
18593              i < main_mem_base + main_mem_size * 4;
18594              i += main_mem_width) {
18595             bxe_read_dmae(sc, i, main_mem_width / 4);
18596             bxe_write_dmae(sc, BXE_SP_MAPPING(sc, wb_data),
18597                            i, main_mem_width / 4);
18598         }
18599         /* Clear HC parity attention */
18600         REG_RD(sc, main_mem_prty_clr);
18601     }
18602
18603 #if 1
18604     /* Enable STORMs SP logging */
18605     REG_WR8(sc, BAR_USTRORM_INTMEM +
18606            USTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18607     REG_WR8(sc, BAR_TSTRORM_INTMEM +
18608            TSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18609     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18610            CSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18611     REG_WR8(sc, BAR_XSTRORM_INTMEM +
18612            XSTORM_RECORD_SLOW_PATH_OFFSET(SC_FUNC(sc)), 1);
18613 #endif
18614
18615     elink_phy_probe(&sc->link_params);
18616
18617     return (0);
18618 }
18619
18620 static void
18621 bxe_link_reset(struct bxe_softc *sc)
18622 {
18623     if (!BXE_NOMCP(sc)) {
18624         bxe_acquire_phy_lock(sc);
18625         elink_lfa_reset(&sc->link_params, &sc->link_vars);
18626         bxe_release_phy_lock(sc);
18627     } else {
18628         if (!CHIP_REV_IS_SLOW(sc)) {
18629             BLOGW(sc, "Bootcode is missing - cannot reset link\n");
18630         }
18631     }
18632 }
18633
18634 static void
18635 bxe_reset_port(struct bxe_softc *sc)
18636 {
18637     int port = SC_PORT(sc);
18638     uint32_t val;
18639
18640     /* reset physical Link */
18641     bxe_link_reset(sc);
18642
18643     REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
18644
18645     /* Do not rcv packets to BRB */
18646     REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
18647     /* Do not direct rcv packets that are not for MCP to the BRB */
18648     REG_WR(sc, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
18649                NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
18650
18651     /* Configure AEU */
18652     REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
18653
18654     DELAY(100000);
18655
18656     /* Check for BRB port occupancy */
18657     val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
18658     if (val) {
18659         BLOGD(sc, DBG_LOAD,
18660               "BRB1 is not empty, %d blocks are occupied\n", val);
18661     }
18662
18663     /* TODO: Close Doorbell port? */
18664 }
18665
18666 static void
18667 bxe_ilt_wr(struct bxe_softc *sc,
18668            uint32_t         index,
18669            bus_addr_t       addr)
18670 {
18671     int reg;
18672     uint32_t wb_write[2];
18673
18674     if (CHIP_IS_E1(sc)) {
18675         reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
18676     } else {
18677         reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
18678     }
18679
18680     wb_write[0] = ONCHIP_ADDR1(addr);
18681     wb_write[1] = ONCHIP_ADDR2(addr);
18682     REG_WR_DMAE(sc, reg, wb_write, 2);
18683 }
18684
18685 static void
18686 bxe_clear_func_ilt(struct bxe_softc *sc,
18687                    uint32_t         func)
18688 {
18689     uint32_t i, base = FUNC_ILT_BASE(func);
18690     for (i = base; i < base + ILT_PER_FUNC; i++) {
18691         bxe_ilt_wr(sc, i, 0);
18692     }
18693 }
18694
18695 static void
18696 bxe_reset_func(struct bxe_softc *sc)
18697 {
18698     struct bxe_fastpath *fp;
18699     int port = SC_PORT(sc);
18700     int func = SC_FUNC(sc);
18701     int i;
18702
18703     /* Disable the function in the FW */
18704     REG_WR8(sc, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
18705     REG_WR8(sc, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
18706     REG_WR8(sc, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
18707     REG_WR8(sc, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
18708
18709     /* FP SBs */
18710     FOR_EACH_ETH_QUEUE(sc, i) {
18711         fp = &sc->fp[i];
18712         REG_WR8(sc, BAR_CSTRORM_INTMEM +
18713                 CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
18714                 SB_DISABLED);
18715     }
18716
18717 #if 0
18718     if (CNIC_LOADED(sc)) {
18719         /* CNIC SB */
18720         REG_WR8(sc, BAR_CSTRORM_INTMEM +
18721                 CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET
18722                 (bxe_cnic_fw_sb_id(sc)), SB_DISABLED);
18723     }
18724 #endif
18725
18726     /* SP SB */
18727     REG_WR8(sc, BAR_CSTRORM_INTMEM +
18728             CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
18729             SB_DISABLED);
18730
18731     for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++) {
18732         REG_WR(sc, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func), 0);
18733     }
18734
18735     /* Configure IGU */
18736     if (sc->devinfo.int_block == INT_BLOCK_HC) {
18737         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port*8, 0);
18738         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port*8, 0);
18739     } else {
18740         REG_WR(sc, IGU_REG_LEADING_EDGE_LATCH, 0);
18741         REG_WR(sc, IGU_REG_TRAILING_EDGE_LATCH, 0);
18742     }
18743
18744     if (CNIC_LOADED(sc)) {
18745         /* Disable Timer scan */
18746         REG_WR(sc, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
18747         /*
18748          * Wait for at least 10ms and up to 2 second for the timers
18749          * scan to complete
18750          */
18751         for (i = 0; i < 200; i++) {
18752             DELAY(10000);
18753             if (!REG_RD(sc, TM_REG_LIN0_SCAN_ON + port*4))
18754                 break;
18755         }
18756     }
18757
18758     /* Clear ILT */
18759     bxe_clear_func_ilt(sc, func);
18760
18761     /*
18762      * Timers workaround bug for E2: if this is vnic-3,
18763      * we need to set the entire ilt range for this timers.
18764      */
18765     if (!CHIP_IS_E1x(sc) && SC_VN(sc) == 3) {
18766         struct ilt_client_info ilt_cli;
18767         /* use dummy TM client */
18768         memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
18769         ilt_cli.start = 0;
18770         ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
18771         ilt_cli.client_num = ILT_CLIENT_TM;
18772
18773         ecore_ilt_boundry_init_op(sc, &ilt_cli, 0, INITOP_CLEAR);
18774     }
18775
18776     /* this assumes that reset_port() called before reset_func()*/
18777     if (!CHIP_IS_E1x(sc)) {
18778         bxe_pf_disable(sc);
18779     }
18780
18781     sc->dmae_ready = 0;
18782 }
18783
18784 static int
18785 bxe_gunzip_init(struct bxe_softc *sc)
18786 {
18787     return (0);
18788 }
18789
18790 static void
18791 bxe_gunzip_end(struct bxe_softc *sc)
18792 {
18793     return;
18794 }
18795
18796 static int
18797 bxe_init_firmware(struct bxe_softc *sc)
18798 {
18799     if (CHIP_IS_E1(sc)) {
18800         ecore_init_e1_firmware(sc);
18801         sc->iro_array = e1_iro_arr;
18802     } else if (CHIP_IS_E1H(sc)) {
18803         ecore_init_e1h_firmware(sc);
18804         sc->iro_array = e1h_iro_arr;
18805     } else if (!CHIP_IS_E1x(sc)) {
18806         ecore_init_e2_firmware(sc);
18807         sc->iro_array = e2_iro_arr;
18808     } else {
18809         BLOGE(sc, "Unsupported chip revision\n");
18810         return (-1);
18811     }
18812
18813     return (0);
18814 }
18815
18816 static void
18817 bxe_release_firmware(struct bxe_softc *sc)
18818 {
18819     /* Do nothing */
18820     return;
18821 }
18822
18823 static int
18824 ecore_gunzip(struct bxe_softc *sc,
18825              const uint8_t    *zbuf,
18826              int              len)
18827 {
18828     /* XXX : Implement... */
18829     BLOGD(sc, DBG_LOAD, "ECORE_GUNZIP NOT IMPLEMENTED\n");
18830     return (FALSE);
18831 }
18832
18833 static void
18834 ecore_reg_wr_ind(struct bxe_softc *sc,
18835                  uint32_t         addr,
18836                  uint32_t         val)
18837 {
18838     bxe_reg_wr_ind(sc, addr, val);
18839 }
18840
18841 static void
18842 ecore_write_dmae_phys_len(struct bxe_softc *sc,
18843                           bus_addr_t       phys_addr,
18844                           uint32_t         addr,
18845                           uint32_t         len)
18846 {
18847     bxe_write_dmae_phys_len(sc, phys_addr, addr, len);
18848 }
18849
18850 void
18851 ecore_storm_memset_struct(struct bxe_softc *sc,
18852                           uint32_t         addr,
18853                           size_t           size,
18854                           uint32_t         *data)
18855 {
18856     uint8_t i;
18857     for (i = 0; i < size/4; i++) {
18858         REG_WR(sc, addr + (i * 4), data[i]);
18859     }
18860 }
18861