xref: /freebsd/sys/dev/ice/ice_lib.c (revision 6577e32ea2c7868c275eae6d1c68f1c37d418c71)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2024, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @file ice_lib.c
34  * @brief Generic device setup and sysctl functions
35  *
36  * Library of generic device functions not specific to the networking stack.
37  *
38  * This includes hardware initialization functions, as well as handlers for
39  * many of the device sysctls used to probe driver status or tune specific
40  * behaviors.
41  */
42 
43 #include "ice_lib.h"
44 #include "ice_iflib.h"
45 #ifdef PCI_IOV
46 #include "ice_iov.h"
47 #endif
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <machine/resource.h>
51 #include <net/if_dl.h>
52 #include <sys/firmware.h>
53 #include <sys/priv.h>
54 #include <sys/limits.h>
55 
56 /**
57  * @var M_ICE
58  * @brief main ice driver allocation type
59  *
60  * malloc(9) allocation type used by the majority of memory allocations in the
61  * ice driver.
62  */
63 MALLOC_DEFINE(M_ICE, "ice", "Intel(R) 100Gb Network Driver lib allocations");
64 
65 /*
66  * Helper function prototypes
67  */
68 static int ice_get_next_vsi(struct ice_vsi **all_vsi, int size);
69 static void ice_set_default_vsi_ctx(struct ice_vsi_ctx *ctx);
70 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctx, enum ice_vsi_type type);
71 static int ice_setup_vsi_qmap(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx);
72 static int ice_setup_tx_ctx(struct ice_tx_queue *txq,
73 			    struct ice_tlan_ctx *tlan_ctx, u16 pf_q);
74 static int ice_setup_rx_ctx(struct ice_rx_queue *rxq);
75 static int ice_is_rxq_ready(struct ice_hw *hw, int pf_q, u32 *reg);
76 static void ice_free_fltr_list(struct ice_list_head *list);
77 static int ice_add_mac_to_list(struct ice_vsi *vsi, struct ice_list_head *list,
78 			       const u8 *addr, enum ice_sw_fwd_act_type action);
79 static void ice_check_ctrlq_errors(struct ice_softc *sc, const char *qname,
80 				   struct ice_ctl_q_info *cq);
81 static void ice_process_link_event(struct ice_softc *sc, struct ice_rq_event_info *e);
82 static void ice_process_ctrlq_event(struct ice_softc *sc, const char *qname,
83 				    struct ice_rq_event_info *event);
84 static void ice_nvm_version_str(struct ice_hw *hw, struct sbuf *buf);
85 static void ice_update_port_oversize(struct ice_softc *sc, u64 rx_errors);
86 static void ice_active_pkg_version_str(struct ice_hw *hw, struct sbuf *buf);
87 static void ice_os_pkg_version_str(struct ice_hw *hw, struct sbuf *buf);
88 static bool ice_filter_is_mcast(struct ice_vsi *vsi, struct ice_fltr_info *info);
89 static u_int ice_sync_one_mcast_filter(void *p, struct sockaddr_dl *sdl, u_int errors);
90 static void ice_add_debug_tunables(struct ice_softc *sc);
91 static void ice_add_debug_sysctls(struct ice_softc *sc);
92 static void ice_vsi_set_rss_params(struct ice_vsi *vsi);
93 static void ice_get_default_rss_key(u8 *seed);
94 static int  ice_set_rss_key(struct ice_vsi *vsi);
95 static int  ice_set_rss_lut(struct ice_vsi *vsi);
96 static void ice_set_rss_flow_flds(struct ice_vsi *vsi);
97 static void ice_clean_vsi_rss_cfg(struct ice_vsi *vsi);
98 static const char *ice_aq_speed_to_str(struct ice_port_info *pi);
99 static const char *ice_requested_fec_mode(struct ice_port_info *pi);
100 static const char *ice_negotiated_fec_mode(struct ice_port_info *pi);
101 static const char *ice_autoneg_mode(struct ice_port_info *pi);
102 static const char *ice_flowcontrol_mode(struct ice_port_info *pi);
103 static void ice_print_bus_link_data(device_t dev, struct ice_hw *hw);
104 static void ice_set_pci_link_status_data(struct ice_hw *hw, u16 link_status);
105 static uint8_t ice_pcie_bandwidth_check(struct ice_softc *sc);
106 static uint64_t ice_pcie_bus_speed_to_rate(enum ice_pcie_bus_speed speed);
107 static int ice_pcie_lnk_width_to_int(enum ice_pcie_link_width width);
108 static uint64_t ice_phy_types_to_max_rate(struct ice_port_info *pi);
109 static void ice_add_sysctls_sw_stats(struct ice_vsi *vsi,
110 				     struct sysctl_ctx_list *ctx,
111 				     struct sysctl_oid *parent);
112 static void
113 ice_add_sysctls_mac_pfc_one_stat(struct sysctl_ctx_list *ctx,
114 				 struct sysctl_oid_list *parent_list,
115 				 u64* pfc_stat_location,
116 				 const char *node_name,
117 				 const char *descr);
118 static void ice_add_sysctls_mac_pfc_stats(struct sysctl_ctx_list *ctx,
119 					  struct sysctl_oid *parent,
120 					  struct ice_hw_port_stats *stats);
121 static void ice_setup_vsi_common(struct ice_softc *sc, struct ice_vsi *vsi,
122 				 enum ice_vsi_type type, int idx,
123 				 bool dynamic);
124 static void ice_handle_mib_change_event(struct ice_softc *sc,
125 				 struct ice_rq_event_info *event);
126 static void
127 ice_handle_lan_overflow_event(struct ice_softc *sc,
128 			      struct ice_rq_event_info *event);
129 static int ice_add_ethertype_to_list(struct ice_vsi *vsi,
130 				     struct ice_list_head *list,
131 				     u16 ethertype, u16 direction,
132 				     enum ice_sw_fwd_act_type action);
133 static void ice_del_rx_lldp_filter(struct ice_softc *sc);
134 static u16 ice_aq_phy_types_to_link_speeds(u64 phy_type_low,
135 					   u64 phy_type_high);
136 struct ice_phy_data;
137 static int
138 ice_intersect_phy_types_and_speeds(struct ice_softc *sc,
139 				   struct ice_phy_data *phy_data);
140 static int
141 ice_apply_saved_phy_req_to_cfg(struct ice_softc *sc,
142 			       struct ice_aqc_set_phy_cfg_data *cfg);
143 static int
144 ice_apply_saved_fec_req_to_cfg(struct ice_softc *sc,
145 			       struct ice_aqc_set_phy_cfg_data *cfg);
146 static void
147 ice_apply_saved_fc_req_to_cfg(struct ice_port_info *pi,
148 			      struct ice_aqc_set_phy_cfg_data *cfg);
149 static void
150 ice_print_ldo_tlv(struct ice_softc *sc,
151 		  struct ice_link_default_override_tlv *tlv);
152 static void
153 ice_sysctl_speeds_to_aq_phy_types(u16 sysctl_speeds, u64 *phy_type_low,
154 				  u64 *phy_type_high);
155 static u16 ice_apply_supported_speed_filter(u16 report_speeds, u8 mod_type);
156 static void
157 ice_handle_health_status_event(struct ice_softc *sc,
158 			       struct ice_rq_event_info *event);
159 static void
160 ice_print_health_status_string(device_t dev,
161 			       struct ice_aqc_health_status_elem *elem);
162 static void
163 ice_debug_print_mib_change_event(struct ice_softc *sc,
164 				 struct ice_rq_event_info *event);
165 static bool ice_check_ets_bw(u8 *table);
166 static u8 ice_dcb_get_num_tc(struct ice_dcbx_cfg *dcbcfg);
167 static bool
168 ice_dcb_needs_reconfig(struct ice_softc *sc, struct ice_dcbx_cfg *old_cfg,
169 		       struct ice_dcbx_cfg *new_cfg);
170 static void ice_dcb_recfg(struct ice_softc *sc);
171 static u8 ice_dcb_tc_contig(u8 tc_map);
172 static int ice_ets_str_to_tbl(const char *str, u8 *table, u8 limit);
173 static int ice_pf_vsi_cfg_tc(struct ice_softc *sc, u8 tc_map);
174 static void ice_sbuf_print_ets_cfg(struct sbuf *sbuf, const char *name,
175 				   struct ice_dcb_ets_cfg *ets);
176 static void ice_stop_pf_vsi(struct ice_softc *sc);
177 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt);
178 static int ice_config_pfc(struct ice_softc *sc, u8 new_mode);
179 void
180 ice_add_dscp2tc_map_sysctls(struct ice_softc *sc,
181 			    struct sysctl_ctx_list *ctx,
182 			    struct sysctl_oid_list *ctx_list);
183 static void ice_set_default_local_mib_settings(struct ice_softc *sc);
184 static bool ice_dscp_is_mapped(struct ice_dcbx_cfg *dcbcfg);
185 static void ice_start_dcbx_agent(struct ice_softc *sc);
186 static u16 ice_fw_debug_dump_print_cluster(struct ice_softc *sc,
187 					   struct sbuf *sbuf, u16 cluster_id);
188 static void ice_fw_debug_dump_print_clusters(struct ice_softc *sc,
189 					     struct sbuf *sbuf);
190 static void ice_remove_vsi_mirroring(struct ice_vsi *vsi);
191 static int ice_get_tx_rx_equalizations(struct ice_hw *hw, u8 serdes_num,
192 				       struct ice_serdes_equalization *ptr);
193 static int ice_fec_counter_read(struct ice_hw *hw, u32 receiver_id,
194 				u32 reg_offset, u16 *output);
195 static int ice_get_port_fec_stats(struct ice_hw *hw, u16 pcs_quad, u16 pcs_port,
196 				  struct ice_fec_stats_to_sysctl *fec_stats);
197 static bool ice_is_serdes_muxed(struct ice_hw *hw);
198 static int ice_get_maxspeed(struct ice_hw *hw, u8 lport, u8 *max_speed);
199 static int ice_update_port_topology(u8 lport,
200 				    struct ice_port_topology *port_topology,
201 				    bool is_muxed);
202 static int ice_get_port_topology(struct ice_hw *hw, u8 lport,
203 				 struct ice_port_topology *port_topology);
204 
205 static int ice_module_init(void);
206 static int ice_module_exit(void);
207 
208 /*
209  * package version comparison functions
210  */
211 static bool pkg_ver_empty(struct ice_pkg_ver *pkg_ver, u8 *pkg_name);
212 static int pkg_ver_compatible(struct ice_pkg_ver *pkg_ver);
213 
214 /*
215  * dynamic sysctl handlers
216  */
217 static int ice_sysctl_show_fw(SYSCTL_HANDLER_ARGS);
218 static int ice_sysctl_pkg_version(SYSCTL_HANDLER_ARGS);
219 static int ice_sysctl_os_pkg_version(SYSCTL_HANDLER_ARGS);
220 static int ice_sysctl_dump_mac_filters(SYSCTL_HANDLER_ARGS);
221 static int ice_sysctl_dump_vlan_filters(SYSCTL_HANDLER_ARGS);
222 static int ice_sysctl_dump_ethertype_filters(SYSCTL_HANDLER_ARGS);
223 static int ice_sysctl_dump_ethertype_mac_filters(SYSCTL_HANDLER_ARGS);
224 static int ice_sysctl_current_speed(SYSCTL_HANDLER_ARGS);
225 static int ice_sysctl_request_reset(SYSCTL_HANDLER_ARGS);
226 static int ice_sysctl_dump_state_flags(SYSCTL_HANDLER_ARGS);
227 static int ice_sysctl_fec_config(SYSCTL_HANDLER_ARGS);
228 static int ice_sysctl_fc_config(SYSCTL_HANDLER_ARGS);
229 static int ice_sysctl_negotiated_fc(SYSCTL_HANDLER_ARGS);
230 static int ice_sysctl_negotiated_fec(SYSCTL_HANDLER_ARGS);
231 static int ice_sysctl_phy_type_low(SYSCTL_HANDLER_ARGS);
232 static int ice_sysctl_phy_type_high(SYSCTL_HANDLER_ARGS);
233 static int __ice_sysctl_phy_type_handler(SYSCTL_HANDLER_ARGS,
234 					 bool is_phy_type_high);
235 static int ice_sysctl_advertise_speed(SYSCTL_HANDLER_ARGS);
236 static int ice_sysctl_rx_itr(SYSCTL_HANDLER_ARGS);
237 static int ice_sysctl_tx_itr(SYSCTL_HANDLER_ARGS);
238 static int ice_sysctl_fw_lldp_agent(SYSCTL_HANDLER_ARGS);
239 static int ice_sysctl_fw_cur_lldp_persist_status(SYSCTL_HANDLER_ARGS);
240 static int ice_sysctl_fw_dflt_lldp_persist_status(SYSCTL_HANDLER_ARGS);
241 static int ice_sysctl_phy_caps(SYSCTL_HANDLER_ARGS, u8 report_mode);
242 static int ice_sysctl_phy_sw_caps(SYSCTL_HANDLER_ARGS);
243 static int ice_sysctl_phy_nvm_caps(SYSCTL_HANDLER_ARGS);
244 static int ice_sysctl_phy_topo_caps(SYSCTL_HANDLER_ARGS);
245 static int ice_sysctl_phy_link_status(SYSCTL_HANDLER_ARGS);
246 static int ice_sysctl_read_i2c_diag_data(SYSCTL_HANDLER_ARGS);
247 static int ice_sysctl_tx_cso_stat(SYSCTL_HANDLER_ARGS);
248 static int ice_sysctl_rx_cso_stat(SYSCTL_HANDLER_ARGS);
249 static int ice_sysctl_pba_number(SYSCTL_HANDLER_ARGS);
250 static int ice_sysctl_rx_errors_stat(SYSCTL_HANDLER_ARGS);
251 static int ice_sysctl_dump_dcbx_cfg(SYSCTL_HANDLER_ARGS);
252 static int ice_sysctl_dump_vsi_cfg(SYSCTL_HANDLER_ARGS);
253 static int ice_sysctl_dump_phy_stats(SYSCTL_HANDLER_ARGS);
254 static int ice_sysctl_ets_min_rate(SYSCTL_HANDLER_ARGS);
255 static int ice_sysctl_up2tc_map(SYSCTL_HANDLER_ARGS);
256 static int ice_sysctl_pfc_config(SYSCTL_HANDLER_ARGS);
257 static int ice_sysctl_query_port_ets(SYSCTL_HANDLER_ARGS);
258 static int ice_sysctl_dscp2tc_map(SYSCTL_HANDLER_ARGS);
259 static int ice_sysctl_pfc_mode(SYSCTL_HANDLER_ARGS);
260 static int ice_sysctl_fw_debug_dump_cluster_setting(SYSCTL_HANDLER_ARGS);
261 static int ice_sysctl_fw_debug_dump_do_dump(SYSCTL_HANDLER_ARGS);
262 static int ice_sysctl_allow_no_fec_mod_in_auto(SYSCTL_HANDLER_ARGS);
263 static int ice_sysctl_set_link_active(SYSCTL_HANDLER_ARGS);
264 static int ice_sysctl_debug_set_link(SYSCTL_HANDLER_ARGS);
265 static int ice_sysctl_temperature(SYSCTL_HANDLER_ARGS);
266 static int ice_sysctl_create_mirror_interface(SYSCTL_HANDLER_ARGS);
267 static int ice_sysctl_destroy_mirror_interface(SYSCTL_HANDLER_ARGS);
268 
269 /**
270  * ice_map_bar - Map PCIe BAR memory
271  * @dev: the PCIe device
272  * @bar: the BAR info structure
273  * @bar_num: PCIe BAR number
274  *
275  * Maps the specified PCIe BAR. Stores the mapping data in struct
276  * ice_bar_info.
277  */
278 int
ice_map_bar(device_t dev,struct ice_bar_info * bar,int bar_num)279 ice_map_bar(device_t dev, struct ice_bar_info *bar, int bar_num)
280 {
281 	if (bar->res != NULL) {
282 		device_printf(dev, "PCI BAR%d already mapped\n", bar_num);
283 		return (EDOOFUS);
284 	}
285 
286 	bar->rid = PCIR_BAR(bar_num);
287 	bar->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &bar->rid,
288 					  RF_ACTIVE);
289 	if (!bar->res) {
290 		device_printf(dev, "PCI BAR%d mapping failed\n", bar_num);
291 		return (ENXIO);
292 	}
293 
294 	bar->tag = rman_get_bustag(bar->res);
295 	bar->handle = rman_get_bushandle(bar->res);
296 	bar->size = rman_get_size(bar->res);
297 
298 	return (0);
299 }
300 
301 /**
302  * ice_free_bar - Free PCIe BAR memory
303  * @dev: the PCIe device
304  * @bar: the BAR info structure
305  *
306  * Frees the specified PCIe BAR, releasing its resources.
307  */
308 void
ice_free_bar(device_t dev,struct ice_bar_info * bar)309 ice_free_bar(device_t dev, struct ice_bar_info *bar)
310 {
311 	if (bar->res != NULL)
312 		bus_release_resource(dev, SYS_RES_MEMORY, bar->rid, bar->res);
313 	bar->res = NULL;
314 }
315 
316 /**
317  * ice_set_ctrlq_len - Configure ctrlq lengths for a device
318  * @hw: the device hardware structure
319  *
320  * Configures the control queues for the given device, setting up the
321  * specified lengths, prior to initializing hardware.
322  */
323 void
ice_set_ctrlq_len(struct ice_hw * hw)324 ice_set_ctrlq_len(struct ice_hw *hw)
325 {
326 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
327 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
328 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
329 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
330 
331 	hw->mailboxq.num_rq_entries = ICE_MBXQ_LEN;
332 	hw->mailboxq.num_sq_entries = ICE_MBXQ_LEN;
333 	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
334 	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
335 
336 	hw->sbq.num_rq_entries = ICE_SBQ_LEN;
337 	hw->sbq.num_sq_entries = ICE_SBQ_LEN;
338 	hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
339 	hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
340 }
341 
342 /**
343  * ice_get_next_vsi - Get the next available VSI slot
344  * @all_vsi: the VSI list
345  * @size: the size of the VSI list
346  *
347  * Returns the index to the first available VSI slot. Will return size (one
348  * past the last index) if there are no slots available.
349  */
350 static int
ice_get_next_vsi(struct ice_vsi ** all_vsi,int size)351 ice_get_next_vsi(struct ice_vsi **all_vsi, int size)
352 {
353 	int i;
354 
355 	for (i = 0; i < size; i++) {
356 		if (all_vsi[i] == NULL)
357 			return i;
358 	}
359 
360 	return size;
361 }
362 
363 /**
364  * ice_setup_vsi_common - Common VSI setup for both dynamic and static VSIs
365  * @sc: the device private softc structure
366  * @vsi: the VSI to setup
367  * @type: the VSI type of the new VSI
368  * @idx: the index in the all_vsi array to use
369  * @dynamic: whether this VSI memory was dynamically allocated
370  *
371  * Perform setup for a VSI that is common to both dynamically allocated VSIs
372  * and the static PF VSI which is embedded in the softc structure.
373  */
374 static void
ice_setup_vsi_common(struct ice_softc * sc,struct ice_vsi * vsi,enum ice_vsi_type type,int idx,bool dynamic)375 ice_setup_vsi_common(struct ice_softc *sc, struct ice_vsi *vsi,
376 		     enum ice_vsi_type type, int idx, bool dynamic)
377 {
378 	/* Store important values in VSI struct */
379 	vsi->type = type;
380 	vsi->sc = sc;
381 	vsi->idx = idx;
382 	sc->all_vsi[idx] = vsi;
383 	vsi->dynamic = dynamic;
384 
385 	/* Set default mirroring rule information */
386 	vsi->rule_mir_ingress = ICE_INVAL_MIRROR_RULE_ID;
387 	vsi->rule_mir_egress = ICE_INVAL_MIRROR_RULE_ID;
388 
389 	/* Setup the VSI tunables now */
390 	ice_add_vsi_tunables(vsi, sc->vsi_sysctls);
391 }
392 
393 /**
394  * ice_alloc_vsi - Allocate a dynamic VSI
395  * @sc: device softc structure
396  * @type: VSI type
397  *
398  * Allocates a new dynamic VSI structure and inserts it into the VSI list.
399  */
400 struct ice_vsi *
ice_alloc_vsi(struct ice_softc * sc,enum ice_vsi_type type)401 ice_alloc_vsi(struct ice_softc *sc, enum ice_vsi_type type)
402 {
403 	struct ice_vsi *vsi;
404 	int idx;
405 
406 	/* Find an open index for a new VSI to be allocated. If the returned
407 	 * index is >= the num_available_vsi then it means no slot is
408 	 * available.
409 	 */
410 	idx = ice_get_next_vsi(sc->all_vsi, sc->num_available_vsi);
411 	if (idx >= sc->num_available_vsi) {
412 		device_printf(sc->dev, "No available VSI slots\n");
413 		return NULL;
414 	}
415 
416 	vsi = (struct ice_vsi *)malloc(sizeof(*vsi), M_ICE, M_NOWAIT | M_ZERO);
417 	if (!vsi) {
418 		device_printf(sc->dev, "Unable to allocate VSI memory\n");
419 		return NULL;
420 	}
421 
422 	ice_setup_vsi_common(sc, vsi, type, idx, true);
423 
424 	return vsi;
425 }
426 
427 /**
428  * ice_setup_pf_vsi - Setup the PF VSI
429  * @sc: the device private softc
430  *
431  * Setup the PF VSI structure which is embedded as sc->pf_vsi in the device
432  * private softc. Unlike other VSIs, the PF VSI memory is allocated as part of
433  * the softc memory, instead of being dynamically allocated at creation.
434  */
435 void
ice_setup_pf_vsi(struct ice_softc * sc)436 ice_setup_pf_vsi(struct ice_softc *sc)
437 {
438 	ice_setup_vsi_common(sc, &sc->pf_vsi, ICE_VSI_PF, 0, false);
439 }
440 
441 /**
442  * ice_alloc_vsi_qmap
443  * @vsi: VSI structure
444  * @max_tx_queues: Number of transmit queues to identify
445  * @max_rx_queues: Number of receive queues to identify
446  *
447  * Allocates a max_[t|r]x_queues array of words for the VSI where each
448  * word contains the index of the queue it represents.  In here, all
449  * words are initialized to an index of ICE_INVALID_RES_IDX, indicating
450  * all queues for this VSI are not yet assigned an index and thus,
451  * not ready for use.
452  *
453  */
454 void
ice_alloc_vsi_qmap(struct ice_vsi * vsi,const int max_tx_queues,const int max_rx_queues)455 ice_alloc_vsi_qmap(struct ice_vsi *vsi, const int max_tx_queues,
456 		   const int max_rx_queues)
457 {
458 	int i;
459 
460 	MPASS(max_tx_queues > 0);
461 	MPASS(max_rx_queues > 0);
462 
463 	/* Allocate Tx queue mapping memory */
464 	vsi->tx_qmap = malloc(sizeof(u16) * max_tx_queues, M_ICE, M_WAITOK);
465 
466 	/* Allocate Rx queue mapping memory */
467 	vsi->rx_qmap = malloc(sizeof(u16) * max_rx_queues, M_ICE, M_WAITOK);
468 
469 	/* Mark every queue map as invalid to start with */
470 	for (i = 0; i < max_tx_queues; i++) {
471 		vsi->tx_qmap[i] = ICE_INVALID_RES_IDX;
472 	}
473 	for (i = 0; i < max_rx_queues; i++) {
474 		vsi->rx_qmap[i] = ICE_INVALID_RES_IDX;
475 	}
476 }
477 
478 /**
479  * ice_free_vsi_qmaps - Free the PF qmaps associated with a VSI
480  * @vsi: the VSI private structure
481  *
482  * Frees the PF qmaps associated with the given VSI. Generally this will be
483  * called by ice_release_vsi, but may need to be called during attach cleanup,
484  * depending on when the qmaps were allocated.
485  */
486 void
ice_free_vsi_qmaps(struct ice_vsi * vsi)487 ice_free_vsi_qmaps(struct ice_vsi *vsi)
488 {
489 	struct ice_softc *sc = vsi->sc;
490 
491 	if (vsi->tx_qmap) {
492 		ice_resmgr_release_map(&sc->tx_qmgr, vsi->tx_qmap,
493 					   vsi->num_tx_queues);
494 		free(vsi->tx_qmap, M_ICE);
495 		vsi->tx_qmap = NULL;
496 	}
497 
498 	if (vsi->rx_qmap) {
499 		ice_resmgr_release_map(&sc->rx_qmgr, vsi->rx_qmap,
500 					   vsi->num_rx_queues);
501 		free(vsi->rx_qmap, M_ICE);
502 		vsi->rx_qmap = NULL;
503 	}
504 }
505 
506 /**
507  * ice_set_default_vsi_ctx - Setup default VSI context parameters
508  * @ctx: the VSI context to initialize
509  *
510  * Initialize and prepare a default VSI context for configuring a new VSI.
511  */
512 static void
ice_set_default_vsi_ctx(struct ice_vsi_ctx * ctx)513 ice_set_default_vsi_ctx(struct ice_vsi_ctx *ctx)
514 {
515 	u32 table = 0;
516 
517 	memset(&ctx->info, 0, sizeof(ctx->info));
518 	/* VSI will be allocated from shared pool */
519 	ctx->alloc_from_pool = true;
520 	/* Enable source pruning by default */
521 	ctx->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
522 	/* Traffic from VSI can be sent to LAN */
523 	ctx->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
524 	/* Allow all packets untagged/tagged */
525 	ctx->info.inner_vlan_flags = ((ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL &
526 				       ICE_AQ_VSI_INNER_VLAN_TX_MODE_M) >>
527 				       ICE_AQ_VSI_INNER_VLAN_TX_MODE_S);
528 	/* Show VLAN/UP from packets in Rx descriptors */
529 	ctx->info.inner_vlan_flags |= ((ICE_AQ_VSI_INNER_VLAN_EMODE_STR_BOTH &
530 					ICE_AQ_VSI_INNER_VLAN_EMODE_M) >>
531 					ICE_AQ_VSI_INNER_VLAN_EMODE_S);
532 	/* Have 1:1 UP mapping for both ingress/egress tables */
533 	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
534 	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
535 	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
536 	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
537 	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
538 	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
539 	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
540 	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
541 	ctx->info.ingress_table = CPU_TO_LE32(table);
542 	ctx->info.egress_table = CPU_TO_LE32(table);
543 	/* Have 1:1 UP mapping for outer to inner UP table */
544 	ctx->info.outer_up_table = CPU_TO_LE32(table);
545 	/* No Outer tag support, so outer_vlan_flags remains zero */
546 }
547 
548 /**
549  * ice_set_rss_vsi_ctx - Setup VSI context parameters for RSS
550  * @ctx: the VSI context to configure
551  * @type: the VSI type
552  *
553  * Configures the VSI context for RSS, based on the VSI type.
554  */
555 static void
ice_set_rss_vsi_ctx(struct ice_vsi_ctx * ctx,enum ice_vsi_type type)556 ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctx, enum ice_vsi_type type)
557 {
558 	u8 lut_type, hash_type;
559 
560 	switch (type) {
561 	case ICE_VSI_PF:
562 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
563 		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
564 		break;
565 	case ICE_VSI_VF:
566 	case ICE_VSI_VMDQ2:
567 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
568 		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
569 		break;
570 	default:
571 		/* Other VSI types do not support RSS */
572 		return;
573 	}
574 
575 	ctx->info.q_opt_rss = (((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
576 				 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
577 				((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
578 				 ICE_AQ_VSI_Q_OPT_RSS_HASH_M));
579 }
580 
581 /**
582  * ice_setup_vsi_qmap - Setup the queue mapping for a VSI
583  * @vsi: the VSI to configure
584  * @ctx: the VSI context to configure
585  *
586  * Configures the context for the given VSI, setting up how the firmware
587  * should map the queues for this VSI.
588  *
589  * @pre vsi->qmap_type is set to a valid type
590  */
591 static int
ice_setup_vsi_qmap(struct ice_vsi * vsi,struct ice_vsi_ctx * ctx)592 ice_setup_vsi_qmap(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
593 {
594 	int pow = 0;
595 	u16 qmap;
596 
597 	MPASS(vsi->rx_qmap != NULL);
598 
599 	switch (vsi->qmap_type) {
600 	case ICE_RESMGR_ALLOC_CONTIGUOUS:
601 		ctx->info.mapping_flags |= CPU_TO_LE16(ICE_AQ_VSI_Q_MAP_CONTIG);
602 
603 		ctx->info.q_mapping[0] = CPU_TO_LE16(vsi->rx_qmap[0]);
604 		ctx->info.q_mapping[1] = CPU_TO_LE16(vsi->num_rx_queues);
605 
606 		break;
607 	case ICE_RESMGR_ALLOC_SCATTERED:
608 		ctx->info.mapping_flags |= CPU_TO_LE16(ICE_AQ_VSI_Q_MAP_NONCONTIG);
609 
610 		for (int i = 0; i < vsi->num_rx_queues; i++)
611 			ctx->info.q_mapping[i] = CPU_TO_LE16(vsi->rx_qmap[i]);
612 		break;
613 	default:
614 		return (EOPNOTSUPP);
615 	}
616 
617 	/* Calculate the next power-of-2 of number of queues */
618 	if (vsi->num_rx_queues)
619 		pow = flsl(vsi->num_rx_queues - 1);
620 
621 	/* Assign all the queues to traffic class zero */
622 	qmap = (pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M;
623 	ctx->info.tc_mapping[0] = CPU_TO_LE16(qmap);
624 
625 	/* Fill out default driver TC queue info for VSI */
626 	vsi->tc_info[0].qoffset = 0;
627 	vsi->tc_info[0].qcount_rx = vsi->num_rx_queues;
628 	vsi->tc_info[0].qcount_tx = vsi->num_tx_queues;
629 	for (int i = 1; i < ICE_MAX_TRAFFIC_CLASS; i++) {
630 		vsi->tc_info[i].qoffset = 0;
631 		vsi->tc_info[i].qcount_rx = 1;
632 		vsi->tc_info[i].qcount_tx = 1;
633 	}
634 	vsi->tc_map = 0x1;
635 
636 	return 0;
637 }
638 
639 /**
640  * ice_setup_vsi_mirroring -- Setup a VSI for mirroring PF VSI traffic
641  * @vsi: VSI to setup
642  *
643  * @pre vsi->mirror_src_vsi is set to the SW VSI num that traffic is to be
644  * mirrored from
645  *
646  * Returns 0 on success, EINVAL on failure.
647  */
648 int
ice_setup_vsi_mirroring(struct ice_vsi * vsi)649 ice_setup_vsi_mirroring(struct ice_vsi *vsi)
650 {
651 	struct ice_mir_rule_buf rule = { };
652 	struct ice_softc *sc = vsi->sc;
653 	struct ice_hw *hw = &sc->hw;
654 	device_t dev = sc->dev;
655 	int status;
656 	u16 rule_id, dest_vsi;
657 	u16 count = 1;
658 
659 	rule.vsi_idx = ice_get_hw_vsi_num(hw, vsi->mirror_src_vsi);
660 	rule.add = true;
661 
662 	dest_vsi = ice_get_hw_vsi_num(hw, vsi->idx);
663 	rule_id = ICE_INVAL_MIRROR_RULE_ID;
664 	status = ice_aq_add_update_mir_rule(hw, ICE_AQC_RULE_TYPE_VPORT_INGRESS,
665 					    dest_vsi, count, &rule, NULL,
666 					    &rule_id);
667 	if (status) {
668 		device_printf(dev,
669 		    "Could not add INGRESS rule for mirror vsi %d to vsi %d, err %s aq_err %s\n",
670 		    rule.vsi_idx, dest_vsi, ice_status_str(status),
671 		    ice_aq_str(hw->adminq.sq_last_status));
672 		return (EINVAL);
673 	}
674 
675 	vsi->rule_mir_ingress = rule_id;
676 
677 	rule_id = ICE_INVAL_MIRROR_RULE_ID;
678 	status = ice_aq_add_update_mir_rule(hw, ICE_AQC_RULE_TYPE_VPORT_EGRESS,
679 					    dest_vsi, count, &rule, NULL, &rule_id);
680 	if (status) {
681 		device_printf(dev,
682 		    "Could not add EGRESS rule for mirror vsi %d to vsi %d, err %s aq_err %s\n",
683 		    rule.vsi_idx, dest_vsi, ice_status_str(status),
684 		    ice_aq_str(hw->adminq.sq_last_status));
685 		return (EINVAL);
686 	}
687 
688 	vsi->rule_mir_egress = rule_id;
689 
690 	return (0);
691 }
692 
693 /**
694  * ice_remove_vsi_mirroring -- Teardown any VSI mirroring rules
695  * @vsi: VSI to remove mirror rules from
696  */
697 static void
ice_remove_vsi_mirroring(struct ice_vsi * vsi)698 ice_remove_vsi_mirroring(struct ice_vsi *vsi)
699 {
700 	struct ice_hw *hw = &vsi->sc->hw;
701 	int status = 0;
702 	bool keep_alloc = false;
703 
704 	if (vsi->rule_mir_ingress != ICE_INVAL_MIRROR_RULE_ID)
705 		status = ice_aq_delete_mir_rule(hw, vsi->rule_mir_ingress, keep_alloc, NULL);
706 
707 	if (status)
708 		device_printf(vsi->sc->dev, "Could not remove mirror VSI ingress rule, err %s aq_err %s\n",
709 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
710 
711 	status = 0;
712 
713 	if (vsi->rule_mir_egress != ICE_INVAL_MIRROR_RULE_ID)
714 		status = ice_aq_delete_mir_rule(hw, vsi->rule_mir_egress, keep_alloc, NULL);
715 
716 	if (status)
717 		device_printf(vsi->sc->dev, "Could not remove mirror VSI egress rule, err %s aq_err %s\n",
718 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
719 }
720 
721 /**
722  * ice_initialize_vsi - Initialize a VSI for use
723  * @vsi: the vsi to initialize
724  *
725  * Initialize a VSI over the adminq and prepare it for operation.
726  *
727  * @pre vsi->num_tx_queues is set
728  * @pre vsi->num_rx_queues is set
729  */
730 int
ice_initialize_vsi(struct ice_vsi * vsi)731 ice_initialize_vsi(struct ice_vsi *vsi)
732 {
733 	struct ice_vsi_ctx ctx = { 0 };
734 	struct ice_hw *hw = &vsi->sc->hw;
735 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
736 	int status;
737 	int err;
738 
739 	/* For now, we only have code supporting PF VSIs */
740 	switch (vsi->type) {
741 	case ICE_VSI_PF:
742 		ctx.flags = ICE_AQ_VSI_TYPE_PF;
743 		break;
744 	case ICE_VSI_VMDQ2:
745 		ctx.flags = ICE_AQ_VSI_TYPE_VMDQ2;
746 		break;
747 #ifdef PCI_IOV
748 	case ICE_VSI_VF:
749 		ctx.flags = ICE_AQ_VSI_TYPE_VF;
750 		ctx.vf_num = vsi->vf_num;
751 		break;
752 #endif
753 	default:
754 		return (ENODEV);
755 	}
756 
757 	ice_set_default_vsi_ctx(&ctx);
758 	ice_set_rss_vsi_ctx(&ctx, vsi->type);
759 
760 	/* XXX: VSIs of other types may need different port info? */
761 	ctx.info.sw_id = hw->port_info->sw_id;
762 
763 	/* Set some RSS parameters based on the VSI type */
764 	ice_vsi_set_rss_params(vsi);
765 
766 	/* Initialize the Rx queue mapping for this VSI */
767 	err = ice_setup_vsi_qmap(vsi, &ctx);
768 	if (err) {
769 		return err;
770 	}
771 
772 	/* (Re-)add VSI to HW VSI handle list */
773 	status = ice_add_vsi(hw, vsi->idx, &ctx, NULL);
774 	if (status != 0) {
775 		device_printf(vsi->sc->dev,
776 		    "Add VSI AQ call failed, err %s aq_err %s\n",
777 		    ice_status_str(status),
778 		    ice_aq_str(hw->adminq.sq_last_status));
779 		return (EIO);
780 	}
781 	vsi->info = ctx.info;
782 
783 	/* Initialize VSI with just 1 TC to start */
784 	max_txqs[0] = vsi->num_tx_queues;
785 
786 	status = ice_cfg_vsi_lan(hw->port_info, vsi->idx,
787 			      ICE_DFLT_TRAFFIC_CLASS, max_txqs);
788 	if (status) {
789 		device_printf(vsi->sc->dev,
790 		    "Failed VSI lan queue config, err %s aq_err %s\n",
791 		    ice_status_str(status),
792 		    ice_aq_str(hw->adminq.sq_last_status));
793 		ice_deinit_vsi(vsi);
794 		return (ENODEV);
795 	}
796 
797 	/* Reset VSI stats */
798 	ice_reset_vsi_stats(vsi);
799 
800 	return 0;
801 }
802 
803 /**
804  * ice_deinit_vsi - Tell firmware to release resources for a VSI
805  * @vsi: the VSI to release
806  *
807  * Helper function which requests the firmware to release the hardware
808  * resources associated with a given VSI.
809  */
810 void
ice_deinit_vsi(struct ice_vsi * vsi)811 ice_deinit_vsi(struct ice_vsi *vsi)
812 {
813 	struct ice_vsi_ctx ctx = { 0 };
814 	struct ice_softc *sc = vsi->sc;
815 	struct ice_hw *hw = &sc->hw;
816 	int status;
817 
818 	/* Assert that the VSI pointer matches in the list */
819 	MPASS(vsi == sc->all_vsi[vsi->idx]);
820 
821 	ctx.info = vsi->info;
822 
823 	status = ice_rm_vsi_lan_cfg(hw->port_info, vsi->idx);
824 	if (status) {
825 		/*
826 		 * This should only fail if the VSI handle is invalid, or if
827 		 * any of the nodes have leaf nodes which are still in use.
828 		 */
829 		device_printf(sc->dev,
830 			      "Unable to remove scheduler nodes for VSI %d, err %s\n",
831 			      vsi->idx, ice_status_str(status));
832 	}
833 
834 	/* Tell firmware to release the VSI resources */
835 	status = ice_free_vsi(hw, vsi->idx, &ctx, false, NULL);
836 	if (status != 0) {
837 		device_printf(sc->dev,
838 		    "Free VSI %u AQ call failed, err %s aq_err %s\n",
839 		    vsi->idx, ice_status_str(status),
840 		    ice_aq_str(hw->adminq.sq_last_status));
841 	}
842 }
843 
844 /**
845  * ice_release_vsi - Release resources associated with a VSI
846  * @vsi: the VSI to release
847  *
848  * Release software and firmware resources associated with a VSI. Release the
849  * queue managers associated with this VSI. Also free the VSI structure memory
850  * if the VSI was allocated dynamically using ice_alloc_vsi().
851  */
852 void
ice_release_vsi(struct ice_vsi * vsi)853 ice_release_vsi(struct ice_vsi *vsi)
854 {
855 	struct ice_softc *sc = vsi->sc;
856 	int idx = vsi->idx;
857 
858 	/* Assert that the VSI pointer matches in the list */
859 	MPASS(vsi == sc->all_vsi[idx]);
860 
861 	/* Cleanup RSS configuration */
862 	if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_RSS))
863 		ice_clean_vsi_rss_cfg(vsi);
864 
865 	ice_del_vsi_sysctl_ctx(vsi);
866 
867 	/* Remove the configured mirror rule, if it exists */
868 	ice_remove_vsi_mirroring(vsi);
869 
870 	/*
871 	 * If we unload the driver after a reset fails, we do not need to do
872 	 * this step.
873 	 */
874 	if (!ice_test_state(&sc->state, ICE_STATE_RESET_FAILED))
875 		ice_deinit_vsi(vsi);
876 
877 	ice_free_vsi_qmaps(vsi);
878 
879 	if (vsi->dynamic) {
880 		free(sc->all_vsi[idx], M_ICE);
881 	}
882 
883 	sc->all_vsi[idx] = NULL;
884 }
885 
886 /**
887  * ice_aq_speed_to_rate - Convert AdminQ speed enum to baudrate
888  * @pi: port info data
889  *
890  * Returns the baudrate value for the current link speed of a given port.
891  */
892 uint64_t
ice_aq_speed_to_rate(struct ice_port_info * pi)893 ice_aq_speed_to_rate(struct ice_port_info *pi)
894 {
895 	switch (pi->phy.link_info.link_speed) {
896 	case ICE_AQ_LINK_SPEED_200GB:
897 		return IF_Gbps(200);
898 	case ICE_AQ_LINK_SPEED_100GB:
899 		return IF_Gbps(100);
900 	case ICE_AQ_LINK_SPEED_50GB:
901 		return IF_Gbps(50);
902 	case ICE_AQ_LINK_SPEED_40GB:
903 		return IF_Gbps(40);
904 	case ICE_AQ_LINK_SPEED_25GB:
905 		return IF_Gbps(25);
906 	case ICE_AQ_LINK_SPEED_10GB:
907 		return IF_Gbps(10);
908 	case ICE_AQ_LINK_SPEED_5GB:
909 		return IF_Gbps(5);
910 	case ICE_AQ_LINK_SPEED_2500MB:
911 		return IF_Mbps(2500);
912 	case ICE_AQ_LINK_SPEED_1000MB:
913 		return IF_Mbps(1000);
914 	case ICE_AQ_LINK_SPEED_100MB:
915 		return IF_Mbps(100);
916 	case ICE_AQ_LINK_SPEED_10MB:
917 		return IF_Mbps(10);
918 	case ICE_AQ_LINK_SPEED_UNKNOWN:
919 	default:
920 		/* return 0 if we don't know the link speed */
921 		return 0;
922 	}
923 }
924 
925 /**
926  * ice_aq_speed_to_str - Convert AdminQ speed enum to string representation
927  * @pi: port info data
928  *
929  * Returns the string representation of the current link speed for a given
930  * port.
931  */
932 static const char *
ice_aq_speed_to_str(struct ice_port_info * pi)933 ice_aq_speed_to_str(struct ice_port_info *pi)
934 {
935 	switch (pi->phy.link_info.link_speed) {
936 	case ICE_AQ_LINK_SPEED_200GB:
937 		return "200 Gbps";
938 	case ICE_AQ_LINK_SPEED_100GB:
939 		return "100 Gbps";
940 	case ICE_AQ_LINK_SPEED_50GB:
941 		return "50 Gbps";
942 	case ICE_AQ_LINK_SPEED_40GB:
943 		return "40 Gbps";
944 	case ICE_AQ_LINK_SPEED_25GB:
945 		return "25 Gbps";
946 	case ICE_AQ_LINK_SPEED_20GB:
947 		return "20 Gbps";
948 	case ICE_AQ_LINK_SPEED_10GB:
949 		return "10 Gbps";
950 	case ICE_AQ_LINK_SPEED_5GB:
951 		return "5 Gbps";
952 	case ICE_AQ_LINK_SPEED_2500MB:
953 		return "2.5 Gbps";
954 	case ICE_AQ_LINK_SPEED_1000MB:
955 		return "1 Gbps";
956 	case ICE_AQ_LINK_SPEED_100MB:
957 		return "100 Mbps";
958 	case ICE_AQ_LINK_SPEED_10MB:
959 		return "10 Mbps";
960 	case ICE_AQ_LINK_SPEED_UNKNOWN:
961 	default:
962 		return "Unknown speed";
963 	}
964 }
965 
966 /**
967  * ice_get_phy_type_low - Get media associated with phy_type_low
968  * @phy_type_low: the low 64bits of phy_type from the AdminQ
969  *
970  * Given the lower 64bits of the phy_type from the hardware, return the
971  * ifm_active bit associated. Return IFM_UNKNOWN when phy_type_low is unknown.
972  * Note that only one of ice_get_phy_type_low or ice_get_phy_type_high should
973  * be called. If phy_type_low is zero, call ice_phy_type_high.
974  */
975 int
ice_get_phy_type_low(uint64_t phy_type_low)976 ice_get_phy_type_low(uint64_t phy_type_low)
977 {
978 	switch (phy_type_low) {
979 	case ICE_PHY_TYPE_LOW_100BASE_TX:
980 		return IFM_100_TX;
981 	case ICE_PHY_TYPE_LOW_100M_SGMII:
982 		return IFM_100_SGMII;
983 	case ICE_PHY_TYPE_LOW_1000BASE_T:
984 		return IFM_1000_T;
985 	case ICE_PHY_TYPE_LOW_1000BASE_SX:
986 		return IFM_1000_SX;
987 	case ICE_PHY_TYPE_LOW_1000BASE_LX:
988 		return IFM_1000_LX;
989 	case ICE_PHY_TYPE_LOW_1000BASE_KX:
990 		return IFM_1000_KX;
991 	case ICE_PHY_TYPE_LOW_1G_SGMII:
992 		return IFM_1000_SGMII;
993 	case ICE_PHY_TYPE_LOW_2500BASE_T:
994 		return IFM_2500_T;
995 	case ICE_PHY_TYPE_LOW_2500BASE_X:
996 		return IFM_2500_X;
997 	case ICE_PHY_TYPE_LOW_2500BASE_KX:
998 		return IFM_2500_KX;
999 	case ICE_PHY_TYPE_LOW_5GBASE_T:
1000 		return IFM_5000_T;
1001 	case ICE_PHY_TYPE_LOW_5GBASE_KR:
1002 		return IFM_5000_KR;
1003 	case ICE_PHY_TYPE_LOW_10GBASE_T:
1004 		return IFM_10G_T;
1005 	case ICE_PHY_TYPE_LOW_10G_SFI_DA:
1006 		return IFM_10G_TWINAX;
1007 	case ICE_PHY_TYPE_LOW_10GBASE_SR:
1008 		return IFM_10G_SR;
1009 	case ICE_PHY_TYPE_LOW_10GBASE_LR:
1010 		return IFM_10G_LR;
1011 	case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
1012 		return IFM_10G_KR;
1013 	case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
1014 		return IFM_10G_AOC;
1015 	case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
1016 		return IFM_10G_SFI;
1017 	case ICE_PHY_TYPE_LOW_25GBASE_T:
1018 		return IFM_25G_T;
1019 	case ICE_PHY_TYPE_LOW_25GBASE_CR:
1020 		return IFM_25G_CR;
1021 	case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
1022 		return IFM_25G_CR_S;
1023 	case ICE_PHY_TYPE_LOW_25GBASE_CR1:
1024 		return IFM_25G_CR1;
1025 	case ICE_PHY_TYPE_LOW_25GBASE_SR:
1026 		return IFM_25G_SR;
1027 	case ICE_PHY_TYPE_LOW_25GBASE_LR:
1028 		return IFM_25G_LR;
1029 	case ICE_PHY_TYPE_LOW_25GBASE_KR:
1030 		return IFM_25G_KR;
1031 	case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
1032 		return IFM_25G_KR_S;
1033 	case ICE_PHY_TYPE_LOW_25GBASE_KR1:
1034 		return IFM_25G_KR1;
1035 	case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
1036 		return IFM_25G_AOC;
1037 	case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
1038 		return IFM_25G_AUI;
1039 	case ICE_PHY_TYPE_LOW_40GBASE_CR4:
1040 		return IFM_40G_CR4;
1041 	case ICE_PHY_TYPE_LOW_40GBASE_SR4:
1042 		return IFM_40G_SR4;
1043 	case ICE_PHY_TYPE_LOW_40GBASE_LR4:
1044 		return IFM_40G_LR4;
1045 	case ICE_PHY_TYPE_LOW_40GBASE_KR4:
1046 		return IFM_40G_KR4;
1047 	case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
1048 		return IFM_40G_XLAUI_AC;
1049 	case ICE_PHY_TYPE_LOW_40G_XLAUI:
1050 		return IFM_40G_XLAUI;
1051 	case ICE_PHY_TYPE_LOW_50GBASE_CR2:
1052 		return IFM_50G_CR2;
1053 	case ICE_PHY_TYPE_LOW_50GBASE_SR2:
1054 		return IFM_50G_SR2;
1055 	case ICE_PHY_TYPE_LOW_50GBASE_LR2:
1056 		return IFM_50G_LR2;
1057 	case ICE_PHY_TYPE_LOW_50GBASE_KR2:
1058 		return IFM_50G_KR2;
1059 	case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
1060 		return IFM_50G_LAUI2_AC;
1061 	case ICE_PHY_TYPE_LOW_50G_LAUI2:
1062 		return IFM_50G_LAUI2;
1063 	case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
1064 		return IFM_50G_AUI2_AC;
1065 	case ICE_PHY_TYPE_LOW_50G_AUI2:
1066 		return IFM_50G_AUI2;
1067 	case ICE_PHY_TYPE_LOW_50GBASE_CP:
1068 		return IFM_50G_CP;
1069 	case ICE_PHY_TYPE_LOW_50GBASE_SR:
1070 		return IFM_50G_SR;
1071 	case ICE_PHY_TYPE_LOW_50GBASE_FR:
1072 		return IFM_50G_FR;
1073 	case ICE_PHY_TYPE_LOW_50GBASE_LR:
1074 		return IFM_50G_LR;
1075 	case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
1076 		return IFM_50G_KR_PAM4;
1077 	case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
1078 		return IFM_50G_AUI1_AC;
1079 	case ICE_PHY_TYPE_LOW_50G_AUI1:
1080 		return IFM_50G_AUI1;
1081 	case ICE_PHY_TYPE_LOW_100GBASE_CR4:
1082 		return IFM_100G_CR4;
1083 	case ICE_PHY_TYPE_LOW_100GBASE_SR4:
1084 		return IFM_100G_SR4;
1085 	case ICE_PHY_TYPE_LOW_100GBASE_LR4:
1086 		return IFM_100G_LR4;
1087 	case ICE_PHY_TYPE_LOW_100GBASE_KR4:
1088 		return IFM_100G_KR4;
1089 	case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
1090 		return IFM_100G_CAUI4_AC;
1091 	case ICE_PHY_TYPE_LOW_100G_CAUI4:
1092 		return IFM_100G_CAUI4;
1093 	case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
1094 		return IFM_100G_AUI4_AC;
1095 	case ICE_PHY_TYPE_LOW_100G_AUI4:
1096 		return IFM_100G_AUI4;
1097 	case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
1098 		return IFM_100G_CR_PAM4;
1099 	case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
1100 		return IFM_100G_KR_PAM4;
1101 	case ICE_PHY_TYPE_LOW_100GBASE_CP2:
1102 		return IFM_100G_CP2;
1103 	case ICE_PHY_TYPE_LOW_100GBASE_SR2:
1104 		return IFM_100G_SR2;
1105 	case ICE_PHY_TYPE_LOW_100GBASE_DR:
1106 		return IFM_100G_DR;
1107 	default:
1108 		return IFM_UNKNOWN;
1109 	}
1110 }
1111 
1112 /**
1113  * ice_get_phy_type_high - Get media associated with phy_type_high
1114  * @phy_type_high: the upper 64bits of phy_type from the AdminQ
1115  *
1116  * Given the upper 64bits of the phy_type from the hardware, return the
1117  * ifm_active bit associated. Return IFM_UNKNOWN on an unknown value. Note
1118  * that only one of ice_get_phy_type_low or ice_get_phy_type_high should be
1119  * called. If phy_type_high is zero, call ice_get_phy_type_low.
1120  */
1121 int
ice_get_phy_type_high(uint64_t phy_type_high)1122 ice_get_phy_type_high(uint64_t phy_type_high)
1123 {
1124 	switch (phy_type_high) {
1125 	case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
1126 		return IFM_100G_KR2_PAM4;
1127 	case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
1128 		return IFM_100G_CAUI2_AC;
1129 	case ICE_PHY_TYPE_HIGH_100G_CAUI2:
1130 		return IFM_100G_CAUI2;
1131 	case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
1132 		return IFM_100G_AUI2_AC;
1133 	case ICE_PHY_TYPE_HIGH_100G_AUI2:
1134 		return IFM_100G_AUI2;
1135 	case ICE_PHY_TYPE_HIGH_200G_CR4_PAM4:
1136 		return IFM_200G_CR4_PAM4;
1137 	case ICE_PHY_TYPE_HIGH_200G_SR4:
1138 		return IFM_200G_SR4;
1139 	case ICE_PHY_TYPE_HIGH_200G_FR4:
1140 		return IFM_200G_FR4;
1141 	case ICE_PHY_TYPE_HIGH_200G_LR4:
1142 		return IFM_200G_LR4;
1143 	case ICE_PHY_TYPE_HIGH_200G_DR4:
1144 		return IFM_200G_DR4;
1145 	case ICE_PHY_TYPE_HIGH_200G_KR4_PAM4:
1146 		return IFM_200G_KR4_PAM4;
1147 	case ICE_PHY_TYPE_HIGH_200G_AUI4_AOC_ACC:
1148 		return IFM_200G_AUI4_AC;
1149 	case ICE_PHY_TYPE_HIGH_200G_AUI4:
1150 		return IFM_200G_AUI4;
1151 	case ICE_PHY_TYPE_HIGH_200G_AUI8_AOC_ACC:
1152 		return IFM_200G_AUI8_AC;
1153 	case ICE_PHY_TYPE_HIGH_200G_AUI8:
1154 		return IFM_200G_AUI8;
1155 	default:
1156 		return IFM_UNKNOWN;
1157 	}
1158 }
1159 
1160 /**
1161  * ice_phy_types_to_max_rate - Returns port's max supported baudrate
1162  * @pi: port info struct
1163  *
1164  * ice_aq_get_phy_caps() w/ ICE_AQC_REPORT_TOPO_CAP_MEDIA parameter needs
1165  * to have been called before this function for it to work.
1166  */
1167 static uint64_t
ice_phy_types_to_max_rate(struct ice_port_info * pi)1168 ice_phy_types_to_max_rate(struct ice_port_info *pi)
1169 {
1170 	uint64_t phy_low = pi->phy.phy_type_low;
1171 	uint64_t phy_high = pi->phy.phy_type_high;
1172 	uint64_t max_rate = 0;
1173 	int bit;
1174 
1175 	/*
1176 	 * These are based on the indices used in the BIT() macros for
1177 	 * ICE_PHY_TYPE_LOW_*
1178 	 */
1179 	static const uint64_t phy_rates[] = {
1180 	    IF_Mbps(100),
1181 	    IF_Mbps(100),
1182 	    IF_Gbps(1ULL),
1183 	    IF_Gbps(1ULL),
1184 	    IF_Gbps(1ULL),
1185 	    IF_Gbps(1ULL),
1186 	    IF_Gbps(1ULL),
1187 	    IF_Mbps(2500ULL),
1188 	    IF_Mbps(2500ULL),
1189 	    IF_Mbps(2500ULL),
1190 	    IF_Gbps(5ULL),
1191 	    IF_Gbps(5ULL),
1192 	    IF_Gbps(10ULL),
1193 	    IF_Gbps(10ULL),
1194 	    IF_Gbps(10ULL),
1195 	    IF_Gbps(10ULL),
1196 	    IF_Gbps(10ULL),
1197 	    IF_Gbps(10ULL),
1198 	    IF_Gbps(10ULL),
1199 	    IF_Gbps(25ULL),
1200 	    IF_Gbps(25ULL),
1201 	    IF_Gbps(25ULL),
1202 	    IF_Gbps(25ULL),
1203 	    IF_Gbps(25ULL),
1204 	    IF_Gbps(25ULL),
1205 	    IF_Gbps(25ULL),
1206 	    IF_Gbps(25ULL),
1207 	    IF_Gbps(25ULL),
1208 	    IF_Gbps(25ULL),
1209 	    IF_Gbps(25ULL),
1210 	    IF_Gbps(40ULL),
1211 	    IF_Gbps(40ULL),
1212 	    IF_Gbps(40ULL),
1213 	    IF_Gbps(40ULL),
1214 	    IF_Gbps(40ULL),
1215 	    IF_Gbps(40ULL),
1216 	    IF_Gbps(50ULL),
1217 	    IF_Gbps(50ULL),
1218 	    IF_Gbps(50ULL),
1219 	    IF_Gbps(50ULL),
1220 	    IF_Gbps(50ULL),
1221 	    IF_Gbps(50ULL),
1222 	    IF_Gbps(50ULL),
1223 	    IF_Gbps(50ULL),
1224 	    IF_Gbps(50ULL),
1225 	    IF_Gbps(50ULL),
1226 	    IF_Gbps(50ULL),
1227 	    IF_Gbps(50ULL),
1228 	    IF_Gbps(50ULL),
1229 	    IF_Gbps(50ULL),
1230 	    IF_Gbps(50ULL),
1231 	    IF_Gbps(100ULL),
1232 	    IF_Gbps(100ULL),
1233 	    IF_Gbps(100ULL),
1234 	    IF_Gbps(100ULL),
1235 	    IF_Gbps(100ULL),
1236 	    IF_Gbps(100ULL),
1237 	    IF_Gbps(100ULL),
1238 	    IF_Gbps(100ULL),
1239 	    IF_Gbps(100ULL),
1240 	    IF_Gbps(100ULL),
1241 	    IF_Gbps(100ULL),
1242 	    IF_Gbps(100ULL),
1243 	    IF_Gbps(100ULL),
1244 	    /* These rates are for ICE_PHY_TYPE_HIGH_* */
1245 	    IF_Gbps(100ULL),
1246 	    IF_Gbps(100ULL),
1247 	    IF_Gbps(100ULL),
1248 	    IF_Gbps(100ULL),
1249 	    IF_Gbps(100ULL),
1250 	    IF_Gbps(200ULL),
1251 	    IF_Gbps(200ULL),
1252 	    IF_Gbps(200ULL),
1253 	    IF_Gbps(200ULL),
1254 	    IF_Gbps(200ULL),
1255 	    IF_Gbps(200ULL),
1256 	    IF_Gbps(200ULL),
1257 	    IF_Gbps(200ULL),
1258 	    IF_Gbps(200ULL),
1259 	    IF_Gbps(200ULL),
1260 	};
1261 
1262 	/* coverity[address_of] */
1263 	for_each_set_bit(bit, &phy_high, 64)
1264 		if ((bit + 64) < (int)ARRAY_SIZE(phy_rates))
1265 			max_rate = uqmax(max_rate, phy_rates[(bit + 64)]);
1266 
1267 	/* coverity[address_of] */
1268 	for_each_set_bit(bit, &phy_low, 64)
1269 		max_rate = uqmax(max_rate, phy_rates[bit]);
1270 
1271 	return (max_rate);
1272 }
1273 
1274 /* The if_media type is split over the original 5 bit media variant field,
1275  * along with extended types using up extra bits in the options section.
1276  * We want to convert this split number into a bitmap index, so we reverse the
1277  * calculation of IFM_X here.
1278  */
1279 #define IFM_IDX(x) (((x) & IFM_TMASK) | \
1280 		    (((x) & IFM_ETH_XTYPE) >> IFM_ETH_XSHIFT))
1281 
1282 /**
1283  * ice_add_media_types - Add supported media types to the media structure
1284  * @sc: ice private softc structure
1285  * @media: ifmedia structure to setup
1286  *
1287  * Looks up the supported phy types, and initializes the various media types
1288  * available.
1289  *
1290  * @pre this function must be protected from being called while another thread
1291  * is accessing the ifmedia types.
1292  */
1293 int
ice_add_media_types(struct ice_softc * sc,struct ifmedia * media)1294 ice_add_media_types(struct ice_softc *sc, struct ifmedia *media)
1295 {
1296 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
1297 	struct ice_port_info *pi = sc->hw.port_info;
1298 	int status;
1299 	uint64_t phy_low, phy_high;
1300 	int bit;
1301 
1302 	ASSERT_CFG_LOCKED(sc);
1303 
1304 	/* the maximum possible media type index is 511. We probably don't
1305 	 * need most of this space, but this ensures future compatibility when
1306 	 * additional media types are used.
1307 	 */
1308 	ice_declare_bitmap(already_added, 511);
1309 
1310 	/* Remove all previous media types */
1311 	ifmedia_removeall(media);
1312 
1313 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG,
1314 				     &pcaps, NULL);
1315 	if (status) {
1316 		device_printf(sc->dev,
1317 		    "%s: ice_aq_get_phy_caps (ACTIVE) failed; status %s, aq_err %s\n",
1318 		    __func__, ice_status_str(status),
1319 		    ice_aq_str(sc->hw.adminq.sq_last_status));
1320 		return (status);
1321 	}
1322 	phy_low = le64toh(pcaps.phy_type_low);
1323 	phy_high = le64toh(pcaps.phy_type_high);
1324 
1325 	/* make sure the added bitmap is zero'd */
1326 	memset(already_added, 0, sizeof(already_added));
1327 
1328 	/* coverity[address_of] */
1329 	for_each_set_bit(bit, &phy_low, 64) {
1330 		uint64_t type = BIT_ULL(bit);
1331 		int ostype;
1332 
1333 		/* get the OS media type */
1334 		ostype = ice_get_phy_type_low(type);
1335 
1336 		/* don't bother adding the unknown type */
1337 		if (ostype == IFM_UNKNOWN)
1338 			continue;
1339 
1340 		/* only add each media type to the list once */
1341 		if (ice_is_bit_set(already_added, IFM_IDX(ostype)))
1342 			continue;
1343 
1344 		ifmedia_add(media, IFM_ETHER | ostype, 0, NULL);
1345 		ice_set_bit(IFM_IDX(ostype), already_added);
1346 	}
1347 
1348 	/* coverity[address_of] */
1349 	for_each_set_bit(bit, &phy_high, 64) {
1350 		uint64_t type = BIT_ULL(bit);
1351 		int ostype;
1352 
1353 		/* get the OS media type */
1354 		ostype = ice_get_phy_type_high(type);
1355 
1356 		/* don't bother adding the unknown type */
1357 		if (ostype == IFM_UNKNOWN)
1358 			continue;
1359 
1360 		/* only add each media type to the list once */
1361 		if (ice_is_bit_set(already_added, IFM_IDX(ostype)))
1362 			continue;
1363 
1364 		ifmedia_add(media, IFM_ETHER | ostype, 0, NULL);
1365 		ice_set_bit(IFM_IDX(ostype), already_added);
1366 	}
1367 
1368 	/* Use autoselect media by default */
1369 	ifmedia_add(media, IFM_ETHER | IFM_AUTO, 0, NULL);
1370 	ifmedia_set(media, IFM_ETHER | IFM_AUTO);
1371 
1372 	return (0);
1373 }
1374 
1375 /**
1376  * ice_configure_rxq_interrupt - Configure HW Rx queue for an MSI-X interrupt
1377  * @hw: ice hw structure
1378  * @rxqid: Rx queue index in PF space
1379  * @vector: MSI-X vector index in PF/VF space
1380  * @itr_idx: ITR index to use for interrupt
1381  *
1382  * @remark ice_flush() may need to be called after this
1383  */
1384 void
ice_configure_rxq_interrupt(struct ice_hw * hw,u16 rxqid,u16 vector,u8 itr_idx)1385 ice_configure_rxq_interrupt(struct ice_hw *hw, u16 rxqid, u16 vector, u8 itr_idx)
1386 {
1387 	u32 val;
1388 
1389 	MPASS(itr_idx <= ICE_ITR_NONE);
1390 
1391 	val = (QINT_RQCTL_CAUSE_ENA_M |
1392 	       (itr_idx << QINT_RQCTL_ITR_INDX_S) |
1393 	       (vector << QINT_RQCTL_MSIX_INDX_S));
1394 	wr32(hw, QINT_RQCTL(rxqid), val);
1395 }
1396 
1397 /**
1398  * ice_configure_all_rxq_interrupts - Configure HW Rx queues for MSI-X interrupts
1399  * @vsi: the VSI to configure
1400  *
1401  * Called when setting up MSI-X interrupts to configure the Rx hardware queues.
1402  */
1403 void
ice_configure_all_rxq_interrupts(struct ice_vsi * vsi)1404 ice_configure_all_rxq_interrupts(struct ice_vsi *vsi)
1405 {
1406 	struct ice_hw *hw = &vsi->sc->hw;
1407 	int i;
1408 
1409 	for (i = 0; i < vsi->num_rx_queues; i++) {
1410 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
1411 
1412 		ice_configure_rxq_interrupt(hw, vsi->rx_qmap[rxq->me],
1413 					    rxq->irqv->me, ICE_RX_ITR);
1414 
1415 		ice_debug(hw, ICE_DBG_INIT,
1416 		    "RXQ(%d) intr enable: me %d rxqid %d vector %d\n",
1417 		    i, rxq->me, vsi->rx_qmap[rxq->me], rxq->irqv->me);
1418 	}
1419 
1420 	ice_flush(hw);
1421 }
1422 
1423 /**
1424  * ice_configure_txq_interrupt - Configure HW Tx queue for an MSI-X interrupt
1425  * @hw: ice hw structure
1426  * @txqid: Tx queue index in PF space
1427  * @vector: MSI-X vector index in PF/VF space
1428  * @itr_idx: ITR index to use for interrupt
1429  *
1430  * @remark ice_flush() may need to be called after this
1431  */
1432 void
ice_configure_txq_interrupt(struct ice_hw * hw,u16 txqid,u16 vector,u8 itr_idx)1433 ice_configure_txq_interrupt(struct ice_hw *hw, u16 txqid, u16 vector, u8 itr_idx)
1434 {
1435 	u32 val;
1436 
1437 	MPASS(itr_idx <= ICE_ITR_NONE);
1438 
1439 	val = (QINT_TQCTL_CAUSE_ENA_M |
1440 	       (itr_idx << QINT_TQCTL_ITR_INDX_S) |
1441 	       (vector << QINT_TQCTL_MSIX_INDX_S));
1442 	wr32(hw, QINT_TQCTL(txqid), val);
1443 }
1444 
1445 /**
1446  * ice_configure_all_txq_interrupts - Configure HW Tx queues for MSI-X interrupts
1447  * @vsi: the VSI to configure
1448  *
1449  * Called when setting up MSI-X interrupts to configure the Tx hardware queues.
1450  */
1451 void
ice_configure_all_txq_interrupts(struct ice_vsi * vsi)1452 ice_configure_all_txq_interrupts(struct ice_vsi *vsi)
1453 {
1454 	struct ice_hw *hw = &vsi->sc->hw;
1455 	int i;
1456 
1457 	for (i = 0; i < vsi->num_tx_queues; i++) {
1458 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
1459 
1460 		ice_configure_txq_interrupt(hw, vsi->tx_qmap[txq->me],
1461 					    txq->irqv->me, ICE_TX_ITR);
1462 	}
1463 
1464 	ice_flush(hw);
1465 }
1466 
1467 /**
1468  * ice_flush_rxq_interrupts - Unconfigure Hw Rx queues MSI-X interrupt cause
1469  * @vsi: the VSI to configure
1470  *
1471  * Unset the CAUSE_ENA flag of the TQCTL register for each queue, then trigger
1472  * a software interrupt on that cause. This is required as part of the Rx
1473  * queue disable logic to dissociate the Rx queue from the interrupt.
1474  *
1475  * Note: this function must be called prior to disabling Rx queues with
1476  * ice_control_all_rx_queues, otherwise the Rx queue may not be disabled properly.
1477  */
1478 void
ice_flush_rxq_interrupts(struct ice_vsi * vsi)1479 ice_flush_rxq_interrupts(struct ice_vsi *vsi)
1480 {
1481 	struct ice_hw *hw = &vsi->sc->hw;
1482 	int i;
1483 
1484 	for (i = 0; i < vsi->num_rx_queues; i++) {
1485 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
1486 		u32 reg, val;
1487 
1488 		/* Clear the CAUSE_ENA flag */
1489 		reg = vsi->rx_qmap[rxq->me];
1490 		val = rd32(hw, QINT_RQCTL(reg));
1491 		val &= ~QINT_RQCTL_CAUSE_ENA_M;
1492 		wr32(hw, QINT_RQCTL(reg), val);
1493 
1494 		ice_flush(hw);
1495 
1496 		/* Trigger a software interrupt to complete interrupt
1497 		 * dissociation.
1498 		 */
1499 		wr32(hw, GLINT_DYN_CTL(rxq->irqv->me),
1500 		     GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
1501 	}
1502 }
1503 
1504 /**
1505  * ice_flush_txq_interrupts - Unconfigure Hw Tx queues MSI-X interrupt cause
1506  * @vsi: the VSI to configure
1507  *
1508  * Unset the CAUSE_ENA flag of the TQCTL register for each queue, then trigger
1509  * a software interrupt on that cause. This is required as part of the Tx
1510  * queue disable logic to dissociate the Tx queue from the interrupt.
1511  *
1512  * Note: this function must be called prior to ice_vsi_disable_tx, otherwise
1513  * the Tx queue disable may not complete properly.
1514  */
1515 void
ice_flush_txq_interrupts(struct ice_vsi * vsi)1516 ice_flush_txq_interrupts(struct ice_vsi *vsi)
1517 {
1518 	struct ice_hw *hw = &vsi->sc->hw;
1519 	int i;
1520 
1521 	for (i = 0; i < vsi->num_tx_queues; i++) {
1522 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
1523 		u32 reg, val;
1524 
1525 		/* Clear the CAUSE_ENA flag */
1526 		reg = vsi->tx_qmap[txq->me];
1527 		val = rd32(hw, QINT_TQCTL(reg));
1528 		val &= ~QINT_TQCTL_CAUSE_ENA_M;
1529 		wr32(hw, QINT_TQCTL(reg), val);
1530 
1531 		ice_flush(hw);
1532 
1533 		/* Trigger a software interrupt to complete interrupt
1534 		 * dissociation.
1535 		 */
1536 		wr32(hw, GLINT_DYN_CTL(txq->irqv->me),
1537 		     GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
1538 	}
1539 }
1540 
1541 /**
1542  * ice_configure_rx_itr - Configure the Rx ITR settings for this VSI
1543  * @vsi: the VSI to configure
1544  *
1545  * Program the hardware ITR registers with the settings for this VSI.
1546  */
1547 void
ice_configure_rx_itr(struct ice_vsi * vsi)1548 ice_configure_rx_itr(struct ice_vsi *vsi)
1549 {
1550 	struct ice_hw *hw = &vsi->sc->hw;
1551 	int i;
1552 
1553 	/* TODO: Handle per-queue/per-vector ITR? */
1554 
1555 	for (i = 0; i < vsi->num_rx_queues; i++) {
1556 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
1557 
1558 		wr32(hw, GLINT_ITR(ICE_RX_ITR, rxq->irqv->me),
1559 		     ice_itr_to_reg(hw, vsi->rx_itr));
1560 	}
1561 
1562 	ice_flush(hw);
1563 }
1564 
1565 /**
1566  * ice_configure_tx_itr - Configure the Tx ITR settings for this VSI
1567  * @vsi: the VSI to configure
1568  *
1569  * Program the hardware ITR registers with the settings for this VSI.
1570  */
1571 void
ice_configure_tx_itr(struct ice_vsi * vsi)1572 ice_configure_tx_itr(struct ice_vsi *vsi)
1573 {
1574 	struct ice_hw *hw = &vsi->sc->hw;
1575 	int i;
1576 
1577 	/* TODO: Handle per-queue/per-vector ITR? */
1578 
1579 	for (i = 0; i < vsi->num_tx_queues; i++) {
1580 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
1581 
1582 		wr32(hw, GLINT_ITR(ICE_TX_ITR, txq->irqv->me),
1583 		     ice_itr_to_reg(hw, vsi->tx_itr));
1584 	}
1585 
1586 	ice_flush(hw);
1587 }
1588 
1589 /**
1590  * ice_setup_tx_ctx - Setup an ice_tlan_ctx structure for a queue
1591  * @txq: the Tx queue to configure
1592  * @tlan_ctx: the Tx LAN queue context structure to initialize
1593  * @pf_q: real queue number
1594  */
1595 static int
ice_setup_tx_ctx(struct ice_tx_queue * txq,struct ice_tlan_ctx * tlan_ctx,u16 pf_q)1596 ice_setup_tx_ctx(struct ice_tx_queue *txq, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
1597 {
1598 	struct ice_vsi *vsi = txq->vsi;
1599 	struct ice_softc *sc = vsi->sc;
1600 	struct ice_hw *hw = &sc->hw;
1601 
1602 	tlan_ctx->port_num = hw->port_info->lport;
1603 
1604 	/* number of descriptors in the queue */
1605 	tlan_ctx->qlen = txq->desc_count;
1606 
1607 	/* set the transmit queue base address, defined in 128 byte units */
1608 	tlan_ctx->base = txq->tx_paddr >> 7;
1609 
1610 	tlan_ctx->pf_num = hw->pf_id;
1611 
1612 	switch (vsi->type) {
1613 	case ICE_VSI_PF:
1614 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
1615 		break;
1616 	case ICE_VSI_VMDQ2:
1617 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_VMQ;
1618 		break;
1619 #ifdef PCI_IOV
1620 	case ICE_VSI_VF:
1621 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_VF;
1622 		tlan_ctx->vmvf_num = hw->func_caps.vf_base_id + vsi->vf_num;
1623 		break;
1624 #endif
1625 	default:
1626 		return (ENODEV);
1627 	}
1628 
1629 	tlan_ctx->src_vsi = ice_get_hw_vsi_num(hw, vsi->idx);
1630 
1631 	/* Enable TSO */
1632 	tlan_ctx->tso_ena = 1;
1633 	tlan_ctx->internal_usage_flag = 1;
1634 
1635 	tlan_ctx->tso_qnum = pf_q;
1636 
1637 	/*
1638 	 * Stick with the older legacy Tx queue interface, instead of the new
1639 	 * advanced queue interface.
1640 	 */
1641 	tlan_ctx->legacy_int = 1;
1642 
1643 	/* Descriptor WB mode */
1644 	tlan_ctx->wb_mode = 0;
1645 
1646 	return (0);
1647 }
1648 
1649 /**
1650  * ice_cfg_vsi_for_tx - Configure the hardware for Tx
1651  * @vsi: the VSI to configure
1652  *
1653  * Configure the device Tx queues through firmware AdminQ commands. After
1654  * this, Tx queues will be ready for transmit.
1655  */
1656 int
ice_cfg_vsi_for_tx(struct ice_vsi * vsi)1657 ice_cfg_vsi_for_tx(struct ice_vsi *vsi)
1658 {
1659 	struct ice_aqc_add_tx_qgrp *qg;
1660 	struct ice_hw *hw = &vsi->sc->hw;
1661 	device_t dev = vsi->sc->dev;
1662 	int status;
1663 	int i;
1664 	int err = 0;
1665 	u16 qg_size, pf_q;
1666 
1667 	qg_size = ice_struct_size(qg, txqs, 1);
1668 	qg = (struct ice_aqc_add_tx_qgrp *)malloc(qg_size, M_ICE, M_NOWAIT|M_ZERO);
1669 	if (!qg)
1670 		return (ENOMEM);
1671 
1672 	qg->num_txqs = 1;
1673 
1674 	for (i = 0; i < vsi->num_tx_queues; i++) {
1675 		struct ice_tlan_ctx tlan_ctx = { 0 };
1676 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
1677 
1678 		/* Last configured queue */
1679 		if (txq->desc_count == 0)
1680 			break;
1681 
1682 		pf_q = vsi->tx_qmap[txq->me];
1683 		qg->txqs[0].txq_id = htole16(pf_q);
1684 
1685 		err = ice_setup_tx_ctx(txq, &tlan_ctx, pf_q);
1686 		if (err)
1687 			goto free_txqg;
1688 
1689 		ice_set_ctx(hw, (u8 *)&tlan_ctx, qg->txqs[0].txq_ctx,
1690 			    ice_tlan_ctx_info);
1691 
1692 		status = ice_ena_vsi_txq(hw->port_info, vsi->idx, txq->tc,
1693 					 txq->q_handle, 1, qg, qg_size, NULL);
1694 		if (status) {
1695 			device_printf(dev,
1696 				      "Failed to set LAN Tx queue %d (TC %d, handle %d) context, err %s aq_err %s\n",
1697 				      i, txq->tc, txq->q_handle,
1698 				      ice_status_str(status),
1699 				      ice_aq_str(hw->adminq.sq_last_status));
1700 			err = ENODEV;
1701 			goto free_txqg;
1702 		}
1703 
1704 		/* Keep track of the Tx queue TEID */
1705 		if (pf_q == le16toh(qg->txqs[0].txq_id))
1706 			txq->q_teid = le32toh(qg->txqs[0].q_teid);
1707 	}
1708 
1709 free_txqg:
1710 	free(qg, M_ICE);
1711 
1712 	return (err);
1713 }
1714 
1715 /**
1716  * ice_setup_rx_ctx - Setup an Rx context structure for a receive queue
1717  * @rxq: the receive queue to program
1718  *
1719  * Setup an Rx queue context structure and program it into the hardware
1720  * registers. This is a necessary step for enabling the Rx queue.
1721  *
1722  * @pre the VSI associated with this queue must have initialized mbuf_sz
1723  */
1724 static int
ice_setup_rx_ctx(struct ice_rx_queue * rxq)1725 ice_setup_rx_ctx(struct ice_rx_queue *rxq)
1726 {
1727 	struct ice_rlan_ctx rlan_ctx = {0};
1728 	struct ice_vsi *vsi = rxq->vsi;
1729 	struct ice_softc *sc = vsi->sc;
1730 	struct ice_hw *hw = &sc->hw;
1731 	int status;
1732 	u32 rxdid = ICE_RXDID_FLEX_NIC;
1733 	u32 regval;
1734 	u16 pf_q;
1735 
1736 	pf_q = vsi->rx_qmap[rxq->me];
1737 
1738 	/* set the receive queue base address, defined in 128 byte units */
1739 	rlan_ctx.base = rxq->rx_paddr >> 7;
1740 
1741 	rlan_ctx.qlen = rxq->desc_count;
1742 
1743 	rlan_ctx.dbuf = vsi->mbuf_sz >> ICE_RLAN_CTX_DBUF_S;
1744 
1745 	/* use 32 byte descriptors */
1746 	rlan_ctx.dsize = 1;
1747 
1748 	/* Strip the Ethernet CRC bytes before the packet is posted to the
1749 	 * host memory.
1750 	 */
1751 	rlan_ctx.crcstrip = 1;
1752 
1753 	rlan_ctx.l2tsel = 1;
1754 
1755 	/* don't do header splitting */
1756 	rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
1757 	rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
1758 	rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
1759 
1760 	/* strip VLAN from inner headers */
1761 	rlan_ctx.showiv = 1;
1762 
1763 	rlan_ctx.rxmax = min(vsi->max_frame_size,
1764 			     ICE_MAX_RX_SEGS * vsi->mbuf_sz);
1765 
1766 	rlan_ctx.lrxqthresh = 1;
1767 
1768 	if (vsi->type != ICE_VSI_VF) {
1769 		regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1770 		regval &= ~QRXFLXP_CNTXT_RXDID_IDX_M;
1771 		regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1772 			QRXFLXP_CNTXT_RXDID_IDX_M;
1773 
1774 		regval &= ~QRXFLXP_CNTXT_RXDID_PRIO_M;
1775 		regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1776 			QRXFLXP_CNTXT_RXDID_PRIO_M;
1777 
1778 		wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1779 	}
1780 
1781 	status = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
1782 	if (status) {
1783 		device_printf(sc->dev,
1784 			      "Failed to set LAN Rx queue context, err %s aq_err %s\n",
1785 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
1786 		return (EIO);
1787 	}
1788 
1789 	wr32(hw, rxq->tail, 0);
1790 
1791 	return 0;
1792 }
1793 
1794 /**
1795  * ice_cfg_vsi_for_rx - Configure the hardware for Rx
1796  * @vsi: the VSI to configure
1797  *
1798  * Prepare an Rx context descriptor and configure the device to receive
1799  * traffic.
1800  *
1801  * @pre the VSI must have initialized mbuf_sz
1802  */
1803 int
ice_cfg_vsi_for_rx(struct ice_vsi * vsi)1804 ice_cfg_vsi_for_rx(struct ice_vsi *vsi)
1805 {
1806 	int i, err;
1807 
1808 	for (i = 0; i < vsi->num_rx_queues; i++) {
1809 		MPASS(vsi->mbuf_sz > 0);
1810 		/* Last configured queue */
1811 		if (vsi->rx_queues[i].desc_count == 0)
1812 			break;
1813 
1814 		err = ice_setup_rx_ctx(&vsi->rx_queues[i]);
1815 		if (err)
1816 			return err;
1817 	}
1818 
1819 	return (0);
1820 }
1821 
1822 /**
1823  * ice_is_rxq_ready - Check if an Rx queue is ready
1824  * @hw: ice hw structure
1825  * @pf_q: absolute PF queue index to check
1826  * @reg: on successful return, contains qrx_ctrl contents
1827  *
1828  * Reads the QRX_CTRL register and verifies if the queue is in a consistent
1829  * state. That is, QENA_REQ matches QENA_STAT. Used to check before making
1830  * a request to change the queue, as well as to verify the request has
1831  * finished. The queue should change status within a few microseconds, so we
1832  * use a small delay while polling the register.
1833  *
1834  * Returns an error code if the queue does not update after a few retries.
1835  */
1836 static int
ice_is_rxq_ready(struct ice_hw * hw,int pf_q,u32 * reg)1837 ice_is_rxq_ready(struct ice_hw *hw, int pf_q, u32 *reg)
1838 {
1839 	u32 qrx_ctrl, qena_req, qena_stat;
1840 	int i;
1841 
1842 	for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
1843 		qrx_ctrl = rd32(hw, QRX_CTRL(pf_q));
1844 		qena_req = (qrx_ctrl >> QRX_CTRL_QENA_REQ_S) & 1;
1845 		qena_stat = (qrx_ctrl >> QRX_CTRL_QENA_STAT_S) & 1;
1846 
1847 		/* if the request and status bits equal, then the queue is
1848 		 * fully disabled or enabled.
1849 		 */
1850 		if (qena_req == qena_stat) {
1851 			*reg = qrx_ctrl;
1852 			return (0);
1853 		}
1854 
1855 		/* wait a few microseconds before we check again */
1856 		DELAY(10);
1857 	}
1858 
1859 	return (ETIMEDOUT);
1860 }
1861 
1862 /**
1863  * ice_control_rx_queue - Configure hardware to start or stop an Rx queue
1864  * @vsi: VSI containing queue to enable/disable
1865  * @qidx: Queue index in VSI space
1866  * @enable: true to enable queue, false to disable
1867  *
1868  * Control the Rx queue through the QRX_CTRL register, enabling or disabling
1869  * it. Wait for the appropriate time to ensure that the queue has actually
1870  * reached the expected state.
1871  */
1872 int
ice_control_rx_queue(struct ice_vsi * vsi,u16 qidx,bool enable)1873 ice_control_rx_queue(struct ice_vsi *vsi, u16 qidx, bool enable)
1874 {
1875 	struct ice_hw *hw = &vsi->sc->hw;
1876 	device_t dev = vsi->sc->dev;
1877 	u32 qrx_ctrl = 0;
1878 	int err;
1879 
1880 	struct ice_rx_queue *rxq = &vsi->rx_queues[qidx];
1881 	int pf_q = vsi->rx_qmap[rxq->me];
1882 
1883 	err = ice_is_rxq_ready(hw, pf_q, &qrx_ctrl);
1884 	if (err) {
1885 		device_printf(dev,
1886 			      "Rx queue %d is not ready\n",
1887 			      pf_q);
1888 		return err;
1889 	}
1890 
1891 	/* Skip if the queue is already in correct state */
1892 	if (enable == !!(qrx_ctrl & QRX_CTRL_QENA_STAT_M))
1893 		return (0);
1894 
1895 	if (enable)
1896 		qrx_ctrl |= QRX_CTRL_QENA_REQ_M;
1897 	else
1898 		qrx_ctrl &= ~QRX_CTRL_QENA_REQ_M;
1899 	wr32(hw, QRX_CTRL(pf_q), qrx_ctrl);
1900 
1901 	/* wait for the queue to finalize the request */
1902 	err = ice_is_rxq_ready(hw, pf_q, &qrx_ctrl);
1903 	if (err) {
1904 		device_printf(dev,
1905 			      "Rx queue %d %sable timeout\n",
1906 			      pf_q, (enable ? "en" : "dis"));
1907 		return err;
1908 	}
1909 
1910 	/* this should never happen */
1911 	if (enable != !!(qrx_ctrl & QRX_CTRL_QENA_STAT_M)) {
1912 		device_printf(dev,
1913 			      "Rx queue %d invalid state\n",
1914 			      pf_q);
1915 		return (EDOOFUS);
1916 	}
1917 
1918 	return (0);
1919 }
1920 
1921 /**
1922  * ice_control_all_rx_queues - Configure hardware to start or stop the Rx queues
1923  * @vsi: VSI to enable/disable queues
1924  * @enable: true to enable queues, false to disable
1925  *
1926  * Control the Rx queues through the QRX_CTRL register, enabling or disabling
1927  * them. Wait for the appropriate time to ensure that the queues have actually
1928  * reached the expected state.
1929  */
1930 int
ice_control_all_rx_queues(struct ice_vsi * vsi,bool enable)1931 ice_control_all_rx_queues(struct ice_vsi *vsi, bool enable)
1932 {
1933 	int i, err;
1934 
1935 	/* TODO: amortize waits by changing all queues up front and then
1936 	 * checking their status afterwards. This will become more necessary
1937 	 * when we have a large number of queues.
1938 	 */
1939 	for (i = 0; i < vsi->num_rx_queues; i++) {
1940 		err = ice_control_rx_queue(vsi, i, enable);
1941 		if (err)
1942 			break;
1943 	}
1944 
1945 	return (0);
1946 }
1947 
1948 /**
1949  * ice_add_mac_to_list - Add MAC filter to a MAC filter list
1950  * @vsi: the VSI to forward to
1951  * @list: list which contains MAC filter entries
1952  * @addr: the MAC address to be added
1953  * @action: filter action to perform on match
1954  *
1955  * Adds a MAC address filter to the list which will be forwarded to firmware
1956  * to add a series of MAC address filters.
1957  *
1958  * Returns 0 on success, and an error code on failure.
1959  *
1960  */
1961 static int
ice_add_mac_to_list(struct ice_vsi * vsi,struct ice_list_head * list,const u8 * addr,enum ice_sw_fwd_act_type action)1962 ice_add_mac_to_list(struct ice_vsi *vsi, struct ice_list_head *list,
1963 		    const u8 *addr, enum ice_sw_fwd_act_type action)
1964 {
1965 	struct ice_fltr_list_entry *entry;
1966 
1967 	entry = (__typeof(entry))malloc(sizeof(*entry), M_ICE, M_NOWAIT|M_ZERO);
1968 	if (!entry)
1969 		return (ENOMEM);
1970 
1971 	entry->fltr_info.flag = ICE_FLTR_TX;
1972 	entry->fltr_info.src_id = ICE_SRC_ID_VSI;
1973 	entry->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1974 	entry->fltr_info.fltr_act = action;
1975 	entry->fltr_info.vsi_handle = vsi->idx;
1976 	bcopy(addr, entry->fltr_info.l_data.mac.mac_addr, ETHER_ADDR_LEN);
1977 
1978 	LIST_ADD(&entry->list_entry, list);
1979 
1980 	return 0;
1981 }
1982 
1983 /**
1984  * ice_free_fltr_list - Free memory associated with a MAC address list
1985  * @list: the list to free
1986  *
1987  * Free the memory of each entry associated with the list.
1988  */
1989 static void
ice_free_fltr_list(struct ice_list_head * list)1990 ice_free_fltr_list(struct ice_list_head *list)
1991 {
1992 	struct ice_fltr_list_entry *e, *tmp;
1993 
1994 	LIST_FOR_EACH_ENTRY_SAFE(e, tmp, list, ice_fltr_list_entry, list_entry) {
1995 		LIST_DEL(&e->list_entry);
1996 		free(e, M_ICE);
1997 	}
1998 }
1999 
2000 /**
2001  * ice_add_vsi_mac_filter - Add a MAC address filter for a VSI
2002  * @vsi: the VSI to add the filter for
2003  * @addr: MAC address to add a filter for
2004  *
2005  * Add a MAC address filter for a given VSI. This is a wrapper around
2006  * ice_add_mac to simplify the interface. First, it only accepts a single
2007  * address, so we don't have to mess around with the list setup in other
2008  * functions. Second, it ignores the ICE_ERR_ALREADY_EXISTS error, so that
2009  * callers don't need to worry about attempting to add the same filter twice.
2010  */
2011 int
ice_add_vsi_mac_filter(struct ice_vsi * vsi,const u8 * addr)2012 ice_add_vsi_mac_filter(struct ice_vsi *vsi, const u8 *addr)
2013 {
2014 	struct ice_list_head mac_addr_list;
2015 	struct ice_hw *hw = &vsi->sc->hw;
2016 	device_t dev = vsi->sc->dev;
2017 	int status;
2018 	int err = 0;
2019 
2020 	INIT_LIST_HEAD(&mac_addr_list);
2021 
2022 	err = ice_add_mac_to_list(vsi, &mac_addr_list, addr, ICE_FWD_TO_VSI);
2023 	if (err)
2024 		goto free_mac_list;
2025 
2026 	status = ice_add_mac(hw, &mac_addr_list);
2027 	if (status == ICE_ERR_ALREADY_EXISTS) {
2028 		; /* Don't complain if we try to add a filter that already exists */
2029 	} else if (status) {
2030 		device_printf(dev,
2031 			      "Failed to add a filter for MAC %6D, err %s aq_err %s\n",
2032 			      addr, ":",
2033 			      ice_status_str(status),
2034 			      ice_aq_str(hw->adminq.sq_last_status));
2035 		err = (EIO);
2036 	}
2037 
2038 free_mac_list:
2039 	ice_free_fltr_list(&mac_addr_list);
2040 	return err;
2041 }
2042 
2043 /**
2044  * ice_cfg_pf_default_mac_filters - Setup default unicast and broadcast addrs
2045  * @sc: device softc structure
2046  *
2047  * Program the default unicast and broadcast filters for the PF VSI.
2048  */
2049 int
ice_cfg_pf_default_mac_filters(struct ice_softc * sc)2050 ice_cfg_pf_default_mac_filters(struct ice_softc *sc)
2051 {
2052 	struct ice_vsi *vsi = &sc->pf_vsi;
2053 	struct ice_hw *hw = &sc->hw;
2054 	int err;
2055 
2056 	/* Add the LAN MAC address */
2057 	err = ice_add_vsi_mac_filter(vsi, hw->port_info->mac.lan_addr);
2058 	if (err)
2059 		return err;
2060 
2061 	/* Add the broadcast address */
2062 	err = ice_add_vsi_mac_filter(vsi, broadcastaddr);
2063 	if (err)
2064 		return err;
2065 
2066 	return (0);
2067 }
2068 
2069 /**
2070  * ice_remove_vsi_mac_filter - Remove a MAC address filter for a VSI
2071  * @vsi: the VSI to add the filter for
2072  * @addr: MAC address to remove a filter for
2073  *
2074  * Remove a MAC address filter from a given VSI. This is a wrapper around
2075  * ice_remove_mac to simplify the interface. First, it only accepts a single
2076  * address, so we don't have to mess around with the list setup in other
2077  * functions. Second, it ignores the ICE_ERR_DOES_NOT_EXIST error, so that
2078  * callers don't need to worry about attempting to remove filters which
2079  * haven't yet been added.
2080  */
2081 int
ice_remove_vsi_mac_filter(struct ice_vsi * vsi,const u8 * addr)2082 ice_remove_vsi_mac_filter(struct ice_vsi *vsi, const u8 *addr)
2083 {
2084 	struct ice_list_head mac_addr_list;
2085 	struct ice_hw *hw = &vsi->sc->hw;
2086 	device_t dev = vsi->sc->dev;
2087 	int status;
2088 	int err = 0;
2089 
2090 	INIT_LIST_HEAD(&mac_addr_list);
2091 
2092 	err = ice_add_mac_to_list(vsi, &mac_addr_list, addr, ICE_FWD_TO_VSI);
2093 	if (err)
2094 		goto free_mac_list;
2095 
2096 	status = ice_remove_mac(hw, &mac_addr_list);
2097 	if (status == ICE_ERR_DOES_NOT_EXIST) {
2098 		; /* Don't complain if we try to remove a filter that doesn't exist */
2099 	} else if (status) {
2100 		device_printf(dev,
2101 			      "Failed to remove a filter for MAC %6D, err %s aq_err %s\n",
2102 			      addr, ":",
2103 			      ice_status_str(status),
2104 			      ice_aq_str(hw->adminq.sq_last_status));
2105 		err = (EIO);
2106 	}
2107 
2108 free_mac_list:
2109 	ice_free_fltr_list(&mac_addr_list);
2110 	return err;
2111 }
2112 
2113 /**
2114  * ice_rm_pf_default_mac_filters - Remove default unicast and broadcast addrs
2115  * @sc: device softc structure
2116  *
2117  * Remove the default unicast and broadcast filters from the PF VSI.
2118  */
2119 int
ice_rm_pf_default_mac_filters(struct ice_softc * sc)2120 ice_rm_pf_default_mac_filters(struct ice_softc *sc)
2121 {
2122 	struct ice_vsi *vsi = &sc->pf_vsi;
2123 	struct ice_hw *hw = &sc->hw;
2124 	int err;
2125 
2126 	/* Remove the LAN MAC address */
2127 	err = ice_remove_vsi_mac_filter(vsi, hw->port_info->mac.lan_addr);
2128 	if (err)
2129 		return err;
2130 
2131 	/* Remove the broadcast address */
2132 	err = ice_remove_vsi_mac_filter(vsi, broadcastaddr);
2133 	if (err)
2134 		return (EIO);
2135 
2136 	return (0);
2137 }
2138 
2139 /**
2140  * ice_check_ctrlq_errors - Check for and report controlq errors
2141  * @sc: device private structure
2142  * @qname: name of the controlq
2143  * @cq: the controlq to check
2144  *
2145  * Check and report controlq errors. Currently all we do is report them to the
2146  * kernel message log, but we might want to improve this in the future, such
2147  * as to keep track of statistics.
2148  */
2149 static void
ice_check_ctrlq_errors(struct ice_softc * sc,const char * qname,struct ice_ctl_q_info * cq)2150 ice_check_ctrlq_errors(struct ice_softc *sc, const char *qname,
2151 		       struct ice_ctl_q_info *cq)
2152 {
2153 	struct ice_hw *hw = &sc->hw;
2154 	u32 val;
2155 
2156 	/* Check for error indications. Note that all the controlqs use the
2157 	 * same register layout, so we use the PF_FW_AxQLEN defines only.
2158 	 */
2159 	val = rd32(hw, cq->rq.len);
2160 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
2161 		   PF_FW_ARQLEN_ARQCRIT_M)) {
2162 		if (val & PF_FW_ARQLEN_ARQVFE_M)
2163 			device_printf(sc->dev,
2164 				"%s Receive Queue VF Error detected\n", qname);
2165 		if (val & PF_FW_ARQLEN_ARQOVFL_M)
2166 			device_printf(sc->dev,
2167 				"%s Receive Queue Overflow Error detected\n",
2168 				qname);
2169 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
2170 			device_printf(sc->dev,
2171 				"%s Receive Queue Critical Error detected\n",
2172 				qname);
2173 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
2174 			 PF_FW_ARQLEN_ARQCRIT_M);
2175 		wr32(hw, cq->rq.len, val);
2176 	}
2177 
2178 	val = rd32(hw, cq->sq.len);
2179 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
2180 		   PF_FW_ATQLEN_ATQCRIT_M)) {
2181 		if (val & PF_FW_ATQLEN_ATQVFE_M)
2182 			device_printf(sc->dev,
2183 				"%s Send Queue VF Error detected\n", qname);
2184 		if (val & PF_FW_ATQLEN_ATQOVFL_M)
2185 			device_printf(sc->dev,
2186 				"%s Send Queue Overflow Error detected\n",
2187 				qname);
2188 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
2189 			device_printf(sc->dev,
2190 				"%s Send Queue Critical Error detected\n",
2191 				qname);
2192 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
2193 			 PF_FW_ATQLEN_ATQCRIT_M);
2194 		wr32(hw, cq->sq.len, val);
2195 	}
2196 }
2197 
2198 /**
2199  * ice_process_link_event - Process a link event indication from firmware
2200  * @sc: device softc structure
2201  * @e: the received event data
2202  *
2203  * Gets the current link status from hardware, and may print a message if an
2204  * unqualified is detected.
2205  */
2206 static void
ice_process_link_event(struct ice_softc * sc,struct ice_rq_event_info __invariant_only * e)2207 ice_process_link_event(struct ice_softc *sc,
2208 		       struct ice_rq_event_info __invariant_only *e)
2209 {
2210 	struct ice_port_info *pi = sc->hw.port_info;
2211 	struct ice_hw *hw = &sc->hw;
2212 	device_t dev = sc->dev;
2213 	int status;
2214 
2215 	/* Sanity check that the data length isn't too small */
2216 	MPASS(le16toh(e->desc.datalen) >= ICE_GET_LINK_STATUS_DATALEN_V1);
2217 
2218 	/*
2219 	 * Even though the adapter gets link status information inside the
2220 	 * event, it needs to send a Get Link Status AQ command in order
2221 	 * to re-enable link events.
2222 	 */
2223 	pi->phy.get_link_info = true;
2224 	ice_get_link_status(pi, &sc->link_up);
2225 
2226 	if (pi->phy.link_info.topo_media_conflict &
2227 	   (ICE_AQ_LINK_TOPO_CONFLICT | ICE_AQ_LINK_MEDIA_CONFLICT |
2228 	    ICE_AQ_LINK_TOPO_CORRUPT))
2229 		device_printf(dev,
2230 		    "Possible mis-configuration of the Ethernet port detected; please use the Intel (R) Ethernet Port Configuration Tool utility to address the issue.\n");
2231 
2232 	if ((pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) &&
2233 	    !(pi->phy.link_info.link_info & ICE_AQ_LINK_UP)) {
2234 		if (!(pi->phy.link_info.an_info & ICE_AQ_QUALIFIED_MODULE))
2235 			device_printf(dev,
2236 			    "Link is disabled on this device because an unsupported module type was detected! Refer to the Intel (R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
2237 		if (pi->phy.link_info.link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED)
2238 			device_printf(dev,
2239 			    "The module's power requirements exceed the device's power supply. Cannot start link.\n");
2240 		if (pi->phy.link_info.link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT)
2241 			device_printf(dev,
2242 			    "The installed module is incompatible with the device's NVM image. Cannot start link.\n");
2243 	}
2244 
2245 	if (!(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
2246 		if (!ice_testandset_state(&sc->state, ICE_STATE_NO_MEDIA)) {
2247 			status = ice_aq_set_link_restart_an(pi, false, NULL);
2248 			if (status && hw->adminq.sq_last_status != ICE_AQ_RC_EMODE)
2249 				device_printf(dev,
2250 				    "%s: ice_aq_set_link_restart_an: status %s, aq_err %s\n",
2251 				    __func__, ice_status_str(status),
2252 				    ice_aq_str(hw->adminq.sq_last_status));
2253 		}
2254 	}
2255 	/* ICE_STATE_NO_MEDIA is cleared when polling task detects media */
2256 
2257 	/* Indicate that link status must be reported again */
2258 	ice_clear_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED);
2259 
2260 	/* OS link info is updated elsewhere */
2261 }
2262 
2263 /**
2264  * ice_process_ctrlq_event - Respond to a controlq event
2265  * @sc: device private structure
2266  * @qname: the name for this controlq
2267  * @event: the event to process
2268  *
2269  * Perform actions in response to various controlq event notifications.
2270  */
2271 static void
ice_process_ctrlq_event(struct ice_softc * sc,const char * qname,struct ice_rq_event_info * event)2272 ice_process_ctrlq_event(struct ice_softc *sc, const char *qname,
2273 			struct ice_rq_event_info *event)
2274 {
2275 	u16 opcode;
2276 
2277 	opcode = le16toh(event->desc.opcode);
2278 
2279 	switch (opcode) {
2280 	case ice_aqc_opc_get_link_status:
2281 		ice_process_link_event(sc, event);
2282 		break;
2283 #ifdef PCI_IOV
2284 	case ice_mbx_opc_send_msg_to_pf:
2285 		ice_vc_handle_vf_msg(sc, event);
2286 		break;
2287 #endif
2288 	case ice_aqc_opc_fw_logs_event:
2289 		ice_handle_fw_log_event(sc, &event->desc, event->msg_buf);
2290 		break;
2291 	case ice_aqc_opc_lldp_set_mib_change:
2292 		ice_handle_mib_change_event(sc, event);
2293 		break;
2294 	case ice_aqc_opc_event_lan_overflow:
2295 		ice_handle_lan_overflow_event(sc, event);
2296 		break;
2297 	case ice_aqc_opc_get_health_status:
2298 		ice_handle_health_status_event(sc, event);
2299 		break;
2300 	default:
2301 		device_printf(sc->dev,
2302 			      "%s Receive Queue unhandled event 0x%04x ignored\n",
2303 			      qname, opcode);
2304 	}
2305 }
2306 
2307 /**
2308  * ice_process_ctrlq - helper function to process controlq rings
2309  * @sc: device private structure
2310  * @q_type: specific control queue type
2311  * @pending: return parameter to track remaining events
2312  *
2313  * Process controlq events for a given control queue type. Returns zero on
2314  * success, and an error code on failure. If successful, pending is the number
2315  * of remaining events left in the queue.
2316  */
2317 int
ice_process_ctrlq(struct ice_softc * sc,enum ice_ctl_q q_type,u16 * pending)2318 ice_process_ctrlq(struct ice_softc *sc, enum ice_ctl_q q_type, u16 *pending)
2319 {
2320 	struct ice_rq_event_info event = { { 0 } };
2321 	struct ice_hw *hw = &sc->hw;
2322 	struct ice_ctl_q_info *cq;
2323 	int status;
2324 	const char *qname;
2325 	int loop = 0;
2326 
2327 	switch (q_type) {
2328 	case ICE_CTL_Q_ADMIN:
2329 		cq = &hw->adminq;
2330 		qname = "Admin";
2331 		break;
2332 	case ICE_CTL_Q_SB:
2333 		cq = &hw->sbq;
2334 		qname = "Sideband";
2335 		break;
2336 	case ICE_CTL_Q_MAILBOX:
2337 		cq = &hw->mailboxq;
2338 		qname = "Mailbox";
2339 		break;
2340 	default:
2341 		device_printf(sc->dev,
2342 			      "Unknown control queue type 0x%x\n",
2343 			      q_type);
2344 		return 0;
2345 	}
2346 
2347 	ice_check_ctrlq_errors(sc, qname, cq);
2348 
2349 	/*
2350 	 * Control queue processing happens during the admin task which may be
2351 	 * holding a non-sleepable lock, so we *must* use M_NOWAIT here.
2352 	 */
2353 	event.buf_len = cq->rq_buf_size;
2354 	event.msg_buf = (u8 *)malloc(event.buf_len, M_ICE, M_ZERO | M_NOWAIT);
2355 	if (!event.msg_buf) {
2356 		device_printf(sc->dev,
2357 			      "Unable to allocate memory for %s Receive Queue event\n",
2358 			      qname);
2359 		return (ENOMEM);
2360 	}
2361 
2362 	do {
2363 		status = ice_clean_rq_elem(hw, cq, &event, pending);
2364 		if (status == ICE_ERR_AQ_NO_WORK)
2365 			break;
2366 		if (status) {
2367 			device_printf(sc->dev,
2368 				      "%s Receive Queue event error %s\n",
2369 				      qname, ice_status_str(status));
2370 			free(event.msg_buf, M_ICE);
2371 			return (EIO);
2372 		}
2373 		/* XXX should we separate this handler by controlq type? */
2374 		ice_process_ctrlq_event(sc, qname, &event);
2375 	} while (*pending && (++loop < ICE_CTRLQ_WORK_LIMIT));
2376 
2377 	free(event.msg_buf, M_ICE);
2378 
2379 	return 0;
2380 }
2381 
2382 /**
2383  * pkg_ver_empty - Check if a package version is empty
2384  * @pkg_ver: the package version to check
2385  * @pkg_name: the package name to check
2386  *
2387  * Checks if the package version structure is empty. We consider a package
2388  * version as empty if none of the versions are non-zero and the name string
2389  * is null as well.
2390  *
2391  * This is used to check if the package version was initialized by the driver,
2392  * as we do not expect an actual DDP package file to have a zero'd version and
2393  * name.
2394  *
2395  * @returns true if the package version is valid, or false otherwise.
2396  */
2397 static bool
pkg_ver_empty(struct ice_pkg_ver * pkg_ver,u8 * pkg_name)2398 pkg_ver_empty(struct ice_pkg_ver *pkg_ver, u8 *pkg_name)
2399 {
2400 	return (pkg_name[0] == '\0' &&
2401 		pkg_ver->major == 0 &&
2402 		pkg_ver->minor == 0 &&
2403 		pkg_ver->update == 0 &&
2404 		pkg_ver->draft == 0);
2405 }
2406 
2407 /**
2408  * pkg_ver_compatible - Check if the package version is compatible
2409  * @pkg_ver: the package version to check
2410  *
2411  * Compares the package version number to the driver's expected major/minor
2412  * version. Returns an integer indicating whether the version is older, newer,
2413  * or compatible with the driver.
2414  *
2415  * @returns 0 if the package version is compatible, -1 if the package version
2416  * is older, and 1 if the package version is newer than the driver version.
2417  */
2418 static int
pkg_ver_compatible(struct ice_pkg_ver * pkg_ver)2419 pkg_ver_compatible(struct ice_pkg_ver *pkg_ver)
2420 {
2421 	if (pkg_ver->major > ICE_PKG_SUPP_VER_MAJ)
2422 		return (1); /* newer */
2423 	else if ((pkg_ver->major == ICE_PKG_SUPP_VER_MAJ) &&
2424 		 (pkg_ver->minor > ICE_PKG_SUPP_VER_MNR))
2425 		return (1); /* newer */
2426 	else if ((pkg_ver->major == ICE_PKG_SUPP_VER_MAJ) &&
2427 		 (pkg_ver->minor == ICE_PKG_SUPP_VER_MNR))
2428 		return (0); /* compatible */
2429 	else
2430 		return (-1); /* older */
2431 }
2432 
2433 /**
2434  * ice_os_pkg_version_str - Format OS package version info into a sbuf
2435  * @hw: device hw structure
2436  * @buf: string buffer to store name/version string
2437  *
2438  * Formats the name and version of the OS DDP package as found in the ice_ddp
2439  * module into a string.
2440  *
2441  * @remark This will almost always be the same as the active package, but
2442  * could be different in some cases. Use ice_active_pkg_version_str to get the
2443  * version of the active DDP package.
2444  */
2445 static void
ice_os_pkg_version_str(struct ice_hw * hw,struct sbuf * buf)2446 ice_os_pkg_version_str(struct ice_hw *hw, struct sbuf *buf)
2447 {
2448 	char name_buf[ICE_PKG_NAME_SIZE];
2449 
2450 	/* If the OS DDP package info is empty, use "None" */
2451 	if (pkg_ver_empty(&hw->pkg_ver, hw->pkg_name)) {
2452 		sbuf_printf(buf, "None");
2453 		return;
2454 	}
2455 
2456 	/*
2457 	 * This should already be null-terminated, but since this is a raw
2458 	 * value from an external source, strlcpy() into a new buffer to
2459 	 * make sure.
2460 	 */
2461 	bzero(name_buf, sizeof(name_buf));
2462 	strlcpy(name_buf, (char *)hw->pkg_name, ICE_PKG_NAME_SIZE);
2463 
2464 	sbuf_printf(buf, "%s version %u.%u.%u.%u",
2465 	    name_buf,
2466 	    hw->pkg_ver.major,
2467 	    hw->pkg_ver.minor,
2468 	    hw->pkg_ver.update,
2469 	    hw->pkg_ver.draft);
2470 }
2471 
2472 /**
2473  * ice_active_pkg_version_str - Format active package version info into a sbuf
2474  * @hw: device hw structure
2475  * @buf: string buffer to store name/version string
2476  *
2477  * Formats the name and version of the active DDP package info into a string
2478  * buffer for use.
2479  */
2480 static void
ice_active_pkg_version_str(struct ice_hw * hw,struct sbuf * buf)2481 ice_active_pkg_version_str(struct ice_hw *hw, struct sbuf *buf)
2482 {
2483 	char name_buf[ICE_PKG_NAME_SIZE];
2484 
2485 	/* If the active DDP package info is empty, use "None" */
2486 	if (pkg_ver_empty(&hw->active_pkg_ver, hw->active_pkg_name)) {
2487 		sbuf_printf(buf, "None");
2488 		return;
2489 	}
2490 
2491 	/*
2492 	 * This should already be null-terminated, but since this is a raw
2493 	 * value from an external source, strlcpy() into a new buffer to
2494 	 * make sure.
2495 	 */
2496 	bzero(name_buf, sizeof(name_buf));
2497 	strlcpy(name_buf, (char *)hw->active_pkg_name, ICE_PKG_NAME_SIZE);
2498 
2499 	sbuf_printf(buf, "%s version %u.%u.%u.%u",
2500 	    name_buf,
2501 	    hw->active_pkg_ver.major,
2502 	    hw->active_pkg_ver.minor,
2503 	    hw->active_pkg_ver.update,
2504 	    hw->active_pkg_ver.draft);
2505 
2506 	if (hw->active_track_id != 0)
2507 		sbuf_printf(buf, ", track id 0x%08x", hw->active_track_id);
2508 }
2509 
2510 /**
2511  * ice_nvm_version_str - Format the NVM version information into a sbuf
2512  * @hw: device hw structure
2513  * @buf: string buffer to store version string
2514  *
2515  * Formats the NVM information including firmware version, API version, NVM
2516  * version, the EETRACK id, and OEM specific version information into a string
2517  * buffer.
2518  */
2519 static void
ice_nvm_version_str(struct ice_hw * hw,struct sbuf * buf)2520 ice_nvm_version_str(struct ice_hw *hw, struct sbuf *buf)
2521 {
2522 	struct ice_nvm_info *nvm = &hw->flash.nvm;
2523 	struct ice_orom_info *orom = &hw->flash.orom;
2524 	struct ice_netlist_info *netlist = &hw->flash.netlist;
2525 
2526 	/* Note that the netlist versions are stored in packed Binary Coded
2527 	 * Decimal format. The use of '%x' will correctly display these as
2528 	 * decimal numbers. This works because every 4 bits will be displayed
2529 	 * as a hexadecimal digit, and the BCD format will only use the values
2530 	 * 0-9.
2531 	 */
2532 	sbuf_printf(buf,
2533 		    "fw %u.%u.%u api %u.%u nvm %x.%02x etid %08x netlist %x.%x.%x-%x.%x.%x.%04x oem %u.%u.%u",
2534 		    hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
2535 		    hw->api_maj_ver, hw->api_min_ver,
2536 		    nvm->major, nvm->minor, nvm->eetrack,
2537 		    netlist->major, netlist->minor,
2538 		    netlist->type >> 16, netlist->type & 0xFFFF,
2539 		    netlist->rev, netlist->cust_ver, netlist->hash,
2540 		    orom->major, orom->build, orom->patch);
2541 }
2542 
2543 /**
2544  * ice_print_nvm_version - Print the NVM info to the kernel message log
2545  * @sc: the device softc structure
2546  *
2547  * Format and print an NVM version string using ice_nvm_version_str().
2548  */
2549 void
ice_print_nvm_version(struct ice_softc * sc)2550 ice_print_nvm_version(struct ice_softc *sc)
2551 {
2552 	struct ice_hw *hw = &sc->hw;
2553 	device_t dev = sc->dev;
2554 	struct sbuf *sbuf;
2555 
2556 	sbuf = sbuf_new_auto();
2557 	ice_nvm_version_str(hw, sbuf);
2558 	sbuf_finish(sbuf);
2559 	device_printf(dev, "%s\n", sbuf_data(sbuf));
2560 	sbuf_delete(sbuf);
2561 }
2562 
2563 /**
2564  * ice_update_port_oversize - Update port oversize stats
2565  * @sc: device private structure
2566  * @rx_errors: VSI error drops
2567  *
2568  * Add ERROR_CNT from GLV_REPC VSI register and rx_oversize stats counter
2569  */
2570 static void
ice_update_port_oversize(struct ice_softc * sc,u64 rx_errors)2571 ice_update_port_oversize(struct ice_softc *sc, u64 rx_errors)
2572 {
2573 	struct ice_hw_port_stats *cur_ps;
2574 	cur_ps = &sc->stats.cur;
2575 
2576 	sc->soft_stats.rx_roc_error = rx_errors + cur_ps->rx_oversize;
2577 }
2578 
2579 /**
2580  * ice_update_vsi_hw_stats - Update VSI-specific ethernet statistics counters
2581  * @vsi: the VSI to be updated
2582  *
2583  * Reads hardware stats and updates the ice_vsi_hw_stats tracking structure with
2584  * the updated values.
2585  */
2586 void
ice_update_vsi_hw_stats(struct ice_vsi * vsi)2587 ice_update_vsi_hw_stats(struct ice_vsi *vsi)
2588 {
2589 	struct ice_eth_stats *prev_es, *cur_es;
2590 	struct ice_hw *hw = &vsi->sc->hw;
2591 	u16 vsi_num;
2592 
2593 	if (!ice_is_vsi_valid(hw, vsi->idx))
2594 		return;
2595 
2596 	vsi_num = ice_get_hw_vsi_num(hw, vsi->idx); /* HW absolute index of a VSI */
2597 	prev_es = &vsi->hw_stats.prev;
2598 	cur_es = &vsi->hw_stats.cur;
2599 
2600 #define ICE_VSI_STAT40(name, location) \
2601 	ice_stat_update40(hw, name ## L(vsi_num), \
2602 			  vsi->hw_stats.offsets_loaded, \
2603 			  &prev_es->location, &cur_es->location)
2604 
2605 #define ICE_VSI_STAT32(name, location) \
2606 	ice_stat_update32(hw, name(vsi_num), \
2607 			  vsi->hw_stats.offsets_loaded, \
2608 			  &prev_es->location, &cur_es->location)
2609 
2610 	ICE_VSI_STAT40(GLV_GORC, rx_bytes);
2611 	ICE_VSI_STAT40(GLV_UPRC, rx_unicast);
2612 	ICE_VSI_STAT40(GLV_MPRC, rx_multicast);
2613 	ICE_VSI_STAT40(GLV_BPRC, rx_broadcast);
2614 	ICE_VSI_STAT32(GLV_RDPC, rx_discards);
2615 	ICE_VSI_STAT40(GLV_GOTC, tx_bytes);
2616 	ICE_VSI_STAT40(GLV_UPTC, tx_unicast);
2617 	ICE_VSI_STAT40(GLV_MPTC, tx_multicast);
2618 	ICE_VSI_STAT40(GLV_BPTC, tx_broadcast);
2619 	ICE_VSI_STAT32(GLV_TEPC, tx_errors);
2620 
2621 	ice_stat_update_repc(hw, vsi->idx, vsi->hw_stats.offsets_loaded,
2622 			     cur_es);
2623 	ice_update_port_oversize(vsi->sc, cur_es->rx_errors);
2624 #undef ICE_VSI_STAT40
2625 #undef ICE_VSI_STAT32
2626 
2627 	vsi->hw_stats.offsets_loaded = true;
2628 }
2629 
2630 /**
2631  * ice_reset_vsi_stats - Reset VSI statistics counters
2632  * @vsi: VSI structure
2633  *
2634  * Resets the software tracking counters for the VSI statistics, and indicate
2635  * that the offsets haven't been loaded. This is intended to be called
2636  * post-reset so that VSI statistics count from zero again.
2637  */
2638 void
ice_reset_vsi_stats(struct ice_vsi * vsi)2639 ice_reset_vsi_stats(struct ice_vsi *vsi)
2640 {
2641 	/* Reset HW stats */
2642 	memset(&vsi->hw_stats.prev, 0, sizeof(vsi->hw_stats.prev));
2643 	memset(&vsi->hw_stats.cur, 0, sizeof(vsi->hw_stats.cur));
2644 	vsi->hw_stats.offsets_loaded = false;
2645 }
2646 
2647 /**
2648  * ice_update_pf_stats - Update port stats counters
2649  * @sc: device private softc structure
2650  *
2651  * Reads hardware statistics registers and updates the software tracking
2652  * structure with new values.
2653  */
2654 void
ice_update_pf_stats(struct ice_softc * sc)2655 ice_update_pf_stats(struct ice_softc *sc)
2656 {
2657 	struct ice_hw_port_stats *prev_ps, *cur_ps;
2658 	struct ice_hw *hw = &sc->hw;
2659 	u8 lport;
2660 
2661 	MPASS(hw->port_info);
2662 
2663 	prev_ps = &sc->stats.prev;
2664 	cur_ps = &sc->stats.cur;
2665 	lport = hw->port_info->lport;
2666 
2667 #define ICE_PF_STAT_PFC(name, location, index) \
2668 	ice_stat_update40(hw, name(lport, index), \
2669 			  sc->stats.offsets_loaded, \
2670 			  &prev_ps->location[index], &cur_ps->location[index])
2671 
2672 #define ICE_PF_STAT40(name, location) \
2673 	ice_stat_update40(hw, name ## L(lport), \
2674 			  sc->stats.offsets_loaded, \
2675 			  &prev_ps->location, &cur_ps->location)
2676 
2677 #define ICE_PF_STAT32(name, location) \
2678 	ice_stat_update32(hw, name(lport), \
2679 			  sc->stats.offsets_loaded, \
2680 			  &prev_ps->location, &cur_ps->location)
2681 
2682 	ICE_PF_STAT40(GLPRT_GORC, eth.rx_bytes);
2683 	ICE_PF_STAT40(GLPRT_UPRC, eth.rx_unicast);
2684 	ICE_PF_STAT40(GLPRT_MPRC, eth.rx_multicast);
2685 	ICE_PF_STAT40(GLPRT_BPRC, eth.rx_broadcast);
2686 	ICE_PF_STAT40(GLPRT_GOTC, eth.tx_bytes);
2687 	ICE_PF_STAT40(GLPRT_UPTC, eth.tx_unicast);
2688 	ICE_PF_STAT40(GLPRT_MPTC, eth.tx_multicast);
2689 	ICE_PF_STAT40(GLPRT_BPTC, eth.tx_broadcast);
2690 	/* This stat register doesn't have an lport */
2691 	ice_stat_update32(hw, PRTRPB_RDPC,
2692 			  sc->stats.offsets_loaded,
2693 			  &prev_ps->eth.rx_discards, &cur_ps->eth.rx_discards);
2694 
2695 	ICE_PF_STAT32(GLPRT_TDOLD, tx_dropped_link_down);
2696 	ICE_PF_STAT40(GLPRT_PRC64, rx_size_64);
2697 	ICE_PF_STAT40(GLPRT_PRC127, rx_size_127);
2698 	ICE_PF_STAT40(GLPRT_PRC255, rx_size_255);
2699 	ICE_PF_STAT40(GLPRT_PRC511, rx_size_511);
2700 	ICE_PF_STAT40(GLPRT_PRC1023, rx_size_1023);
2701 	ICE_PF_STAT40(GLPRT_PRC1522, rx_size_1522);
2702 	ICE_PF_STAT40(GLPRT_PRC9522, rx_size_big);
2703 	ICE_PF_STAT40(GLPRT_PTC64, tx_size_64);
2704 	ICE_PF_STAT40(GLPRT_PTC127, tx_size_127);
2705 	ICE_PF_STAT40(GLPRT_PTC255, tx_size_255);
2706 	ICE_PF_STAT40(GLPRT_PTC511, tx_size_511);
2707 	ICE_PF_STAT40(GLPRT_PTC1023, tx_size_1023);
2708 	ICE_PF_STAT40(GLPRT_PTC1522, tx_size_1522);
2709 	ICE_PF_STAT40(GLPRT_PTC9522, tx_size_big);
2710 
2711 	/* Update Priority Flow Control Stats */
2712 	for (int i = 0; i <= GLPRT_PXOFFRXC_MAX_INDEX; i++) {
2713 		ICE_PF_STAT_PFC(GLPRT_PXONRXC, priority_xon_rx, i);
2714 		ICE_PF_STAT_PFC(GLPRT_PXOFFRXC, priority_xoff_rx, i);
2715 		ICE_PF_STAT_PFC(GLPRT_PXONTXC, priority_xon_tx, i);
2716 		ICE_PF_STAT_PFC(GLPRT_PXOFFTXC, priority_xoff_tx, i);
2717 		ICE_PF_STAT_PFC(GLPRT_RXON2OFFCNT, priority_xon_2_xoff, i);
2718 	}
2719 
2720 	ICE_PF_STAT32(GLPRT_LXONRXC, link_xon_rx);
2721 	ICE_PF_STAT32(GLPRT_LXOFFRXC, link_xoff_rx);
2722 	ICE_PF_STAT32(GLPRT_LXONTXC, link_xon_tx);
2723 	ICE_PF_STAT32(GLPRT_LXOFFTXC, link_xoff_tx);
2724 	ICE_PF_STAT32(GLPRT_CRCERRS, crc_errors);
2725 	ICE_PF_STAT32(GLPRT_ILLERRC, illegal_bytes);
2726 	ICE_PF_STAT32(GLPRT_MLFC, mac_local_faults);
2727 	ICE_PF_STAT32(GLPRT_MRFC, mac_remote_faults);
2728 	ICE_PF_STAT32(GLPRT_RLEC, rx_len_errors);
2729 	ICE_PF_STAT32(GLPRT_RUC, rx_undersize);
2730 	ICE_PF_STAT32(GLPRT_RFC, rx_fragments);
2731 	ICE_PF_STAT32(GLPRT_ROC, rx_oversize);
2732 	ICE_PF_STAT32(GLPRT_RJC, rx_jabber);
2733 
2734 #undef ICE_PF_STAT40
2735 #undef ICE_PF_STAT32
2736 #undef ICE_PF_STAT_PFC
2737 
2738 	sc->stats.offsets_loaded = true;
2739 }
2740 
2741 /**
2742  * ice_reset_pf_stats - Reset port stats counters
2743  * @sc: Device private softc structure
2744  *
2745  * Reset software tracking values for statistics to zero, and indicate that
2746  * offsets haven't been loaded. Intended to be called after a device reset so
2747  * that statistics count from zero again.
2748  */
2749 void
ice_reset_pf_stats(struct ice_softc * sc)2750 ice_reset_pf_stats(struct ice_softc *sc)
2751 {
2752 	memset(&sc->stats.prev, 0, sizeof(sc->stats.prev));
2753 	memset(&sc->stats.cur, 0, sizeof(sc->stats.cur));
2754 	sc->stats.offsets_loaded = false;
2755 }
2756 
2757 /**
2758  * ice_sysctl_show_fw - sysctl callback to show firmware information
2759  * @oidp: sysctl oid structure
2760  * @arg1: pointer to private data structure
2761  * @arg2: unused
2762  * @req: sysctl request pointer
2763  *
2764  * Callback for the fw_version sysctl, to display the current firmware
2765  * information found at hardware init time.
2766  */
2767 static int
ice_sysctl_show_fw(SYSCTL_HANDLER_ARGS)2768 ice_sysctl_show_fw(SYSCTL_HANDLER_ARGS)
2769 {
2770 	struct ice_softc *sc = (struct ice_softc *)arg1;
2771 	struct ice_hw *hw = &sc->hw;
2772 	struct sbuf *sbuf;
2773 
2774 	UNREFERENCED_PARAMETER(oidp);
2775 	UNREFERENCED_PARAMETER(arg2);
2776 
2777 	if (ice_driver_is_detaching(sc))
2778 		return (ESHUTDOWN);
2779 
2780 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
2781 	ice_nvm_version_str(hw, sbuf);
2782 	sbuf_finish(sbuf);
2783 	sbuf_delete(sbuf);
2784 
2785 	return (0);
2786 }
2787 
2788 /**
2789  * ice_sysctl_pba_number - sysctl callback to show PBA number
2790  * @oidp: sysctl oid structure
2791  * @arg1: pointer to private data structure
2792  * @arg2: unused
2793  * @req: sysctl request pointer
2794  *
2795  * Callback for the pba_number sysctl, used to read the Product Board Assembly
2796  * number for this device.
2797  */
2798 static int
ice_sysctl_pba_number(SYSCTL_HANDLER_ARGS)2799 ice_sysctl_pba_number(SYSCTL_HANDLER_ARGS)
2800 {
2801 	struct ice_softc *sc = (struct ice_softc *)arg1;
2802 	struct ice_hw *hw = &sc->hw;
2803 	device_t dev = sc->dev;
2804 	u8 pba_string[32] = "";
2805 	int status;
2806 
2807 	UNREFERENCED_PARAMETER(arg2);
2808 
2809 	if (ice_driver_is_detaching(sc))
2810 		return (ESHUTDOWN);
2811 
2812 	status = ice_read_pba_string(hw, pba_string, sizeof(pba_string));
2813 	if (status) {
2814 		device_printf(dev,
2815 		    "%s: failed to read PBA string from NVM; status %s, aq_err %s\n",
2816 		    __func__, ice_status_str(status),
2817 		    ice_aq_str(hw->adminq.sq_last_status));
2818 		return (EIO);
2819 	}
2820 
2821 	return sysctl_handle_string(oidp, pba_string, sizeof(pba_string), req);
2822 }
2823 
2824 /**
2825  * ice_sysctl_pkg_version - sysctl to show the active package version info
2826  * @oidp: sysctl oid structure
2827  * @arg1: pointer to private data structure
2828  * @arg2: unused
2829  * @req: sysctl request pointer
2830  *
2831  * Callback for the pkg_version sysctl, to display the active DDP package name
2832  * and version information.
2833  */
2834 static int
ice_sysctl_pkg_version(SYSCTL_HANDLER_ARGS)2835 ice_sysctl_pkg_version(SYSCTL_HANDLER_ARGS)
2836 {
2837 	struct ice_softc *sc = (struct ice_softc *)arg1;
2838 	struct ice_hw *hw = &sc->hw;
2839 	struct sbuf *sbuf;
2840 
2841 	UNREFERENCED_PARAMETER(oidp);
2842 	UNREFERENCED_PARAMETER(arg2);
2843 
2844 	if (ice_driver_is_detaching(sc))
2845 		return (ESHUTDOWN);
2846 
2847 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
2848 	ice_active_pkg_version_str(hw, sbuf);
2849 	sbuf_finish(sbuf);
2850 	sbuf_delete(sbuf);
2851 
2852 	return (0);
2853 }
2854 
2855 /**
2856  * ice_sysctl_os_pkg_version - sysctl to show the OS package version info
2857  * @oidp: sysctl oid structure
2858  * @arg1: pointer to private data structure
2859  * @arg2: unused
2860  * @req: sysctl request pointer
2861  *
2862  * Callback for the pkg_version sysctl, to display the OS DDP package name and
2863  * version info found in the ice_ddp module.
2864  */
2865 static int
ice_sysctl_os_pkg_version(SYSCTL_HANDLER_ARGS)2866 ice_sysctl_os_pkg_version(SYSCTL_HANDLER_ARGS)
2867 {
2868 	struct ice_softc *sc = (struct ice_softc *)arg1;
2869 	struct ice_hw *hw = &sc->hw;
2870 	struct sbuf *sbuf;
2871 
2872 	UNREFERENCED_PARAMETER(oidp);
2873 	UNREFERENCED_PARAMETER(arg2);
2874 
2875 	if (ice_driver_is_detaching(sc))
2876 		return (ESHUTDOWN);
2877 
2878 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
2879 	ice_os_pkg_version_str(hw, sbuf);
2880 	sbuf_finish(sbuf);
2881 	sbuf_delete(sbuf);
2882 
2883 	return (0);
2884 }
2885 
2886 /**
2887  * ice_sysctl_current_speed - sysctl callback to show current link speed
2888  * @oidp: sysctl oid structure
2889  * @arg1: pointer to private data structure
2890  * @arg2: unused
2891  * @req: sysctl request pointer
2892  *
2893  * Callback for the current_speed sysctl, to display the string representing
2894  * the current link speed.
2895  */
2896 static int
ice_sysctl_current_speed(SYSCTL_HANDLER_ARGS)2897 ice_sysctl_current_speed(SYSCTL_HANDLER_ARGS)
2898 {
2899 	struct ice_softc *sc = (struct ice_softc *)arg1;
2900 	struct ice_hw *hw = &sc->hw;
2901 	struct sbuf *sbuf;
2902 
2903 	UNREFERENCED_PARAMETER(oidp);
2904 	UNREFERENCED_PARAMETER(arg2);
2905 
2906 	if (ice_driver_is_detaching(sc))
2907 		return (ESHUTDOWN);
2908 
2909 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 10, req);
2910 	sbuf_printf(sbuf, "%s", ice_aq_speed_to_str(hw->port_info));
2911 	sbuf_finish(sbuf);
2912 	sbuf_delete(sbuf);
2913 
2914 	return (0);
2915 }
2916 
2917 /**
2918  * @var phy_link_speeds
2919  * @brief PHY link speed conversion array
2920  *
2921  * Array of link speeds to convert ICE_PHY_TYPE_LOW and ICE_PHY_TYPE_HIGH into
2922  * link speeds used by the link speed sysctls.
2923  *
2924  * @remark these are based on the indices used in the BIT() macros for the
2925  * ICE_PHY_TYPE_LOW_* and ICE_PHY_TYPE_HIGH_* definitions.
2926  */
2927 static const uint16_t phy_link_speeds[] = {
2928     ICE_AQ_LINK_SPEED_100MB,
2929     ICE_AQ_LINK_SPEED_100MB,
2930     ICE_AQ_LINK_SPEED_1000MB,
2931     ICE_AQ_LINK_SPEED_1000MB,
2932     ICE_AQ_LINK_SPEED_1000MB,
2933     ICE_AQ_LINK_SPEED_1000MB,
2934     ICE_AQ_LINK_SPEED_1000MB,
2935     ICE_AQ_LINK_SPEED_2500MB,
2936     ICE_AQ_LINK_SPEED_2500MB,
2937     ICE_AQ_LINK_SPEED_2500MB,
2938     ICE_AQ_LINK_SPEED_5GB,
2939     ICE_AQ_LINK_SPEED_5GB,
2940     ICE_AQ_LINK_SPEED_10GB,
2941     ICE_AQ_LINK_SPEED_10GB,
2942     ICE_AQ_LINK_SPEED_10GB,
2943     ICE_AQ_LINK_SPEED_10GB,
2944     ICE_AQ_LINK_SPEED_10GB,
2945     ICE_AQ_LINK_SPEED_10GB,
2946     ICE_AQ_LINK_SPEED_10GB,
2947     ICE_AQ_LINK_SPEED_25GB,
2948     ICE_AQ_LINK_SPEED_25GB,
2949     ICE_AQ_LINK_SPEED_25GB,
2950     ICE_AQ_LINK_SPEED_25GB,
2951     ICE_AQ_LINK_SPEED_25GB,
2952     ICE_AQ_LINK_SPEED_25GB,
2953     ICE_AQ_LINK_SPEED_25GB,
2954     ICE_AQ_LINK_SPEED_25GB,
2955     ICE_AQ_LINK_SPEED_25GB,
2956     ICE_AQ_LINK_SPEED_25GB,
2957     ICE_AQ_LINK_SPEED_25GB,
2958     ICE_AQ_LINK_SPEED_40GB,
2959     ICE_AQ_LINK_SPEED_40GB,
2960     ICE_AQ_LINK_SPEED_40GB,
2961     ICE_AQ_LINK_SPEED_40GB,
2962     ICE_AQ_LINK_SPEED_40GB,
2963     ICE_AQ_LINK_SPEED_40GB,
2964     ICE_AQ_LINK_SPEED_50GB,
2965     ICE_AQ_LINK_SPEED_50GB,
2966     ICE_AQ_LINK_SPEED_50GB,
2967     ICE_AQ_LINK_SPEED_50GB,
2968     ICE_AQ_LINK_SPEED_50GB,
2969     ICE_AQ_LINK_SPEED_50GB,
2970     ICE_AQ_LINK_SPEED_50GB,
2971     ICE_AQ_LINK_SPEED_50GB,
2972     ICE_AQ_LINK_SPEED_50GB,
2973     ICE_AQ_LINK_SPEED_50GB,
2974     ICE_AQ_LINK_SPEED_50GB,
2975     ICE_AQ_LINK_SPEED_50GB,
2976     ICE_AQ_LINK_SPEED_50GB,
2977     ICE_AQ_LINK_SPEED_50GB,
2978     ICE_AQ_LINK_SPEED_50GB,
2979     ICE_AQ_LINK_SPEED_100GB,
2980     ICE_AQ_LINK_SPEED_100GB,
2981     ICE_AQ_LINK_SPEED_100GB,
2982     ICE_AQ_LINK_SPEED_100GB,
2983     ICE_AQ_LINK_SPEED_100GB,
2984     ICE_AQ_LINK_SPEED_100GB,
2985     ICE_AQ_LINK_SPEED_100GB,
2986     ICE_AQ_LINK_SPEED_100GB,
2987     ICE_AQ_LINK_SPEED_100GB,
2988     ICE_AQ_LINK_SPEED_100GB,
2989     ICE_AQ_LINK_SPEED_100GB,
2990     ICE_AQ_LINK_SPEED_100GB,
2991     ICE_AQ_LINK_SPEED_100GB,
2992     /* These rates are for ICE_PHY_TYPE_HIGH_* */
2993     ICE_AQ_LINK_SPEED_100GB,
2994     ICE_AQ_LINK_SPEED_100GB,
2995     ICE_AQ_LINK_SPEED_100GB,
2996     ICE_AQ_LINK_SPEED_100GB,
2997     ICE_AQ_LINK_SPEED_100GB,
2998     ICE_AQ_LINK_SPEED_200GB,
2999     ICE_AQ_LINK_SPEED_200GB,
3000     ICE_AQ_LINK_SPEED_200GB,
3001     ICE_AQ_LINK_SPEED_200GB,
3002     ICE_AQ_LINK_SPEED_200GB,
3003     ICE_AQ_LINK_SPEED_200GB,
3004     ICE_AQ_LINK_SPEED_200GB,
3005     ICE_AQ_LINK_SPEED_200GB,
3006     ICE_AQ_LINK_SPEED_200GB,
3007     ICE_AQ_LINK_SPEED_200GB,
3008 };
3009 
3010 #define ICE_SYSCTL_HELP_ADVERTISE_SPEED		\
3011 "\nControl advertised link speed."		\
3012 "\nFlags:"					\
3013 "\n\t   0x0 - Auto"				\
3014 "\n\t   0x1 - 10 Mb"				\
3015 "\n\t   0x2 - 100 Mb"				\
3016 "\n\t   0x4 - 1G"				\
3017 "\n\t   0x8 - 2.5G"				\
3018 "\n\t  0x10 - 5G"				\
3019 "\n\t  0x20 - 10G"				\
3020 "\n\t  0x40 - 20G"				\
3021 "\n\t  0x80 - 25G"				\
3022 "\n\t 0x100 - 40G"				\
3023 "\n\t 0x200 - 50G"				\
3024 "\n\t 0x400 - 100G"				\
3025 "\n\t 0x800 - 200G"				\
3026 "\n\t0x8000 - Unknown"				\
3027 "\n\t"						\
3028 "\nUse \"sysctl -x\" to view flags properly."
3029 
3030 #define ICE_PHYS_100MB			\
3031     (ICE_PHY_TYPE_LOW_100BASE_TX |	\
3032      ICE_PHY_TYPE_LOW_100M_SGMII)
3033 #define ICE_PHYS_1000MB			\
3034     (ICE_PHY_TYPE_LOW_1000BASE_T |	\
3035      ICE_PHY_TYPE_LOW_1000BASE_SX |	\
3036      ICE_PHY_TYPE_LOW_1000BASE_LX |	\
3037      ICE_PHY_TYPE_LOW_1000BASE_KX |	\
3038      ICE_PHY_TYPE_LOW_1G_SGMII)
3039 #define ICE_PHYS_2500MB			\
3040     (ICE_PHY_TYPE_LOW_2500BASE_T |	\
3041      ICE_PHY_TYPE_LOW_2500BASE_X |	\
3042      ICE_PHY_TYPE_LOW_2500BASE_KX)
3043 #define ICE_PHYS_5GB			\
3044     (ICE_PHY_TYPE_LOW_5GBASE_T |	\
3045      ICE_PHY_TYPE_LOW_5GBASE_KR)
3046 #define ICE_PHYS_10GB			\
3047     (ICE_PHY_TYPE_LOW_10GBASE_T |	\
3048      ICE_PHY_TYPE_LOW_10G_SFI_DA |	\
3049      ICE_PHY_TYPE_LOW_10GBASE_SR |	\
3050      ICE_PHY_TYPE_LOW_10GBASE_LR |	\
3051      ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 |	\
3052      ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC |	\
3053      ICE_PHY_TYPE_LOW_10G_SFI_C2C)
3054 #define ICE_PHYS_25GB			\
3055     (ICE_PHY_TYPE_LOW_25GBASE_T |	\
3056      ICE_PHY_TYPE_LOW_25GBASE_CR |	\
3057      ICE_PHY_TYPE_LOW_25GBASE_CR_S |	\
3058      ICE_PHY_TYPE_LOW_25GBASE_CR1 |	\
3059      ICE_PHY_TYPE_LOW_25GBASE_SR |	\
3060      ICE_PHY_TYPE_LOW_25GBASE_LR |	\
3061      ICE_PHY_TYPE_LOW_25GBASE_KR |	\
3062      ICE_PHY_TYPE_LOW_25GBASE_KR_S |	\
3063      ICE_PHY_TYPE_LOW_25GBASE_KR1 |	\
3064      ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC |	\
3065      ICE_PHY_TYPE_LOW_25G_AUI_C2C)
3066 #define ICE_PHYS_40GB			\
3067     (ICE_PHY_TYPE_LOW_40GBASE_CR4 |	\
3068      ICE_PHY_TYPE_LOW_40GBASE_SR4 |	\
3069      ICE_PHY_TYPE_LOW_40GBASE_LR4 |	\
3070      ICE_PHY_TYPE_LOW_40GBASE_KR4 |	\
3071      ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC | \
3072      ICE_PHY_TYPE_LOW_40G_XLAUI)
3073 #define ICE_PHYS_50GB			\
3074     (ICE_PHY_TYPE_LOW_50GBASE_CR2 |	\
3075      ICE_PHY_TYPE_LOW_50GBASE_SR2 |	\
3076      ICE_PHY_TYPE_LOW_50GBASE_LR2 |	\
3077      ICE_PHY_TYPE_LOW_50GBASE_KR2 |	\
3078      ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC | \
3079      ICE_PHY_TYPE_LOW_50G_LAUI2 |	\
3080      ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC | \
3081      ICE_PHY_TYPE_LOW_50G_AUI2 |	\
3082      ICE_PHY_TYPE_LOW_50GBASE_CP |	\
3083      ICE_PHY_TYPE_LOW_50GBASE_SR |	\
3084      ICE_PHY_TYPE_LOW_50GBASE_FR |	\
3085      ICE_PHY_TYPE_LOW_50GBASE_LR |	\
3086      ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4 |	\
3087      ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC | \
3088      ICE_PHY_TYPE_LOW_50G_AUI1)
3089 #define ICE_PHYS_100GB_LOW		\
3090     (ICE_PHY_TYPE_LOW_100GBASE_CR4 |	\
3091      ICE_PHY_TYPE_LOW_100GBASE_SR4 |	\
3092      ICE_PHY_TYPE_LOW_100GBASE_LR4 |	\
3093      ICE_PHY_TYPE_LOW_100GBASE_KR4 |	\
3094      ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC | \
3095      ICE_PHY_TYPE_LOW_100G_CAUI4 |	\
3096      ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC | \
3097      ICE_PHY_TYPE_LOW_100G_AUI4 |	\
3098      ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 | \
3099      ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 | \
3100      ICE_PHY_TYPE_LOW_100GBASE_CP2 |	\
3101      ICE_PHY_TYPE_LOW_100GBASE_SR2 |	\
3102      ICE_PHY_TYPE_LOW_100GBASE_DR)
3103 #define ICE_PHYS_100GB_HIGH		\
3104     (ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4 | \
3105      ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC | \
3106      ICE_PHY_TYPE_HIGH_100G_CAUI2 |	\
3107      ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC | \
3108      ICE_PHY_TYPE_HIGH_100G_AUI2)
3109 #define ICE_PHYS_200GB			\
3110     (ICE_PHY_TYPE_HIGH_200G_CR4_PAM4 |	\
3111      ICE_PHY_TYPE_HIGH_200G_SR4 |	\
3112      ICE_PHY_TYPE_HIGH_200G_FR4 |	\
3113      ICE_PHY_TYPE_HIGH_200G_LR4 |	\
3114      ICE_PHY_TYPE_HIGH_200G_DR4 |	\
3115      ICE_PHY_TYPE_HIGH_200G_KR4_PAM4 |	\
3116      ICE_PHY_TYPE_HIGH_200G_AUI4_AOC_ACC | \
3117      ICE_PHY_TYPE_HIGH_200G_AUI4 |	\
3118      ICE_PHY_TYPE_HIGH_200G_AUI8_AOC_ACC | \
3119      ICE_PHY_TYPE_HIGH_200G_AUI8)
3120 
3121 /**
3122  * ice_aq_phy_types_to_link_speeds - Convert the PHY Types to speeds
3123  * @phy_type_low: lower 64-bit PHY Type bitmask
3124  * @phy_type_high: upper 64-bit PHY Type bitmask
3125  *
3126  * Convert the PHY Type fields from Get PHY Abilities and Set PHY Config into
3127  * link speed flags. If phy_type_high has an unknown PHY type, then the return
3128  * value will include the "ICE_AQ_LINK_SPEED_UNKNOWN" flag as well.
3129  */
3130 static u16
ice_aq_phy_types_to_link_speeds(u64 phy_type_low,u64 phy_type_high)3131 ice_aq_phy_types_to_link_speeds(u64 phy_type_low, u64 phy_type_high)
3132 {
3133 	u16 sysctl_speeds = 0;
3134 	int bit;
3135 
3136 	/* coverity[address_of] */
3137 	for_each_set_bit(bit, &phy_type_low, 64)
3138 		sysctl_speeds |= phy_link_speeds[bit];
3139 
3140 	/* coverity[address_of] */
3141 	for_each_set_bit(bit, &phy_type_high, 64) {
3142 		if ((bit + 64) < (int)ARRAY_SIZE(phy_link_speeds))
3143 			sysctl_speeds |= phy_link_speeds[bit + 64];
3144 		else
3145 			sysctl_speeds |= ICE_AQ_LINK_SPEED_UNKNOWN;
3146 	}
3147 
3148 	return (sysctl_speeds);
3149 }
3150 
3151 /**
3152  * ice_sysctl_speeds_to_aq_phy_types - Convert sysctl speed flags to AQ PHY flags
3153  * @sysctl_speeds: 16-bit sysctl speeds or AQ_LINK_SPEED flags
3154  * @phy_type_low: output parameter for lower AQ PHY flags
3155  * @phy_type_high: output parameter for higher AQ PHY flags
3156  *
3157  * Converts the given link speed flags into AQ PHY type flag sets appropriate
3158  * for use in a Set PHY Config command.
3159  */
3160 static void
ice_sysctl_speeds_to_aq_phy_types(u16 sysctl_speeds,u64 * phy_type_low,u64 * phy_type_high)3161 ice_sysctl_speeds_to_aq_phy_types(u16 sysctl_speeds, u64 *phy_type_low,
3162 				  u64 *phy_type_high)
3163 {
3164 	*phy_type_low = 0, *phy_type_high = 0;
3165 
3166 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_100MB)
3167 		*phy_type_low |= ICE_PHYS_100MB;
3168 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_1000MB)
3169 		*phy_type_low |= ICE_PHYS_1000MB;
3170 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_2500MB)
3171 		*phy_type_low |= ICE_PHYS_2500MB;
3172 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_5GB)
3173 		*phy_type_low |= ICE_PHYS_5GB;
3174 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_10GB)
3175 		*phy_type_low |= ICE_PHYS_10GB;
3176 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_25GB)
3177 		*phy_type_low |= ICE_PHYS_25GB;
3178 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_40GB)
3179 		*phy_type_low |= ICE_PHYS_40GB;
3180 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_50GB)
3181 		*phy_type_low |= ICE_PHYS_50GB;
3182 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_100GB) {
3183 		*phy_type_low |= ICE_PHYS_100GB_LOW;
3184 		*phy_type_high |= ICE_PHYS_100GB_HIGH;
3185 	}
3186 	if (sysctl_speeds & ICE_AQ_LINK_SPEED_200GB)
3187 		*phy_type_high |= ICE_PHYS_200GB;
3188 }
3189 
3190 /**
3191  * @struct ice_phy_data
3192  * @brief PHY caps and link speeds
3193  *
3194  * Buffer providing report mode and user speeds;
3195  * returning intersection of PHY types and speeds.
3196  */
3197 struct ice_phy_data {
3198 	u64 phy_low_orig;     /* PHY low quad from report */
3199 	u64 phy_high_orig;    /* PHY high quad from report */
3200 	u64 phy_low_intr;     /* PHY low quad intersection with user speeds */
3201 	u64 phy_high_intr;    /* PHY high quad intersection with user speeds */
3202 	u16 user_speeds_orig; /* Input from caller - See ICE_AQ_LINK_SPEED_* */
3203 	u16 user_speeds_intr; /* Intersect with report speeds */
3204 	u8 report_mode;       /* See ICE_AQC_REPORT_* */
3205 };
3206 
3207 /**
3208  * ice_intersect_phy_types_and_speeds - Return intersection of link speeds
3209  * @sc: device private structure
3210  * @phy_data: device PHY data
3211  *
3212  * On read: Displays the currently supported speeds
3213  * On write: Sets the device's supported speeds
3214  * Valid input flags: see ICE_SYSCTL_HELP_ADVERTISE_SPEED
3215  */
3216 static int
ice_intersect_phy_types_and_speeds(struct ice_softc * sc,struct ice_phy_data * phy_data)3217 ice_intersect_phy_types_and_speeds(struct ice_softc *sc,
3218 				   struct ice_phy_data *phy_data)
3219 {
3220 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
3221 	const char *report_types[5] = { "w/o MEDIA",
3222 					"w/MEDIA",
3223 					"ACTIVE",
3224 					"EDOOFUS", /* Not used */
3225 					"DFLT" };
3226 	struct ice_hw *hw = &sc->hw;
3227 	struct ice_port_info *pi = hw->port_info;
3228 	int status;
3229 	u16 report_speeds, temp_speeds;
3230 	u8 report_type;
3231 	bool apply_speed_filter = false;
3232 
3233 	switch (phy_data->report_mode) {
3234 	case ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA:
3235 	case ICE_AQC_REPORT_TOPO_CAP_MEDIA:
3236 	case ICE_AQC_REPORT_ACTIVE_CFG:
3237 	case ICE_AQC_REPORT_DFLT_CFG:
3238 		report_type = phy_data->report_mode >> 1;
3239 		break;
3240 	default:
3241 		device_printf(sc->dev,
3242 		    "%s: phy_data.report_mode \"%u\" doesn't exist\n",
3243 		    __func__, phy_data->report_mode);
3244 		return (EINVAL);
3245 	}
3246 
3247 	/* 0 is treated as "Auto"; the driver will handle selecting the
3248 	 * correct speeds. Including, in some cases, applying an override
3249 	 * if provided.
3250 	 */
3251 	if (phy_data->user_speeds_orig == 0)
3252 		phy_data->user_speeds_orig = USHRT_MAX;
3253 	else if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_LENIENT_LINK_MODE))
3254 		apply_speed_filter = true;
3255 
3256 	status = ice_aq_get_phy_caps(pi, false, phy_data->report_mode, &pcaps, NULL);
3257 	if (status) {
3258 		device_printf(sc->dev,
3259 		    "%s: ice_aq_get_phy_caps (%s) failed; status %s, aq_err %s\n",
3260 		    __func__, report_types[report_type],
3261 		    ice_status_str(status),
3262 		    ice_aq_str(sc->hw.adminq.sq_last_status));
3263 		return (EIO);
3264 	}
3265 
3266 	phy_data->phy_low_orig = le64toh(pcaps.phy_type_low);
3267 	phy_data->phy_high_orig = le64toh(pcaps.phy_type_high);
3268 	report_speeds = ice_aq_phy_types_to_link_speeds(phy_data->phy_low_orig,
3269 	    phy_data->phy_high_orig);
3270 	if (apply_speed_filter) {
3271 		temp_speeds = ice_apply_supported_speed_filter(report_speeds,
3272 		    pcaps.module_type[0]);
3273 		if ((phy_data->user_speeds_orig & temp_speeds) == 0) {
3274 			device_printf(sc->dev,
3275 			    "User-specified speeds (\"0x%04X\") not supported\n",
3276 			    phy_data->user_speeds_orig);
3277 			return (EINVAL);
3278 		}
3279 		report_speeds = temp_speeds;
3280 	}
3281 	ice_sysctl_speeds_to_aq_phy_types(phy_data->user_speeds_orig,
3282 	    &phy_data->phy_low_intr, &phy_data->phy_high_intr);
3283 	phy_data->user_speeds_intr = phy_data->user_speeds_orig & report_speeds;
3284 	phy_data->phy_low_intr &= phy_data->phy_low_orig;
3285 	phy_data->phy_high_intr &= phy_data->phy_high_orig;
3286 
3287 	return (0);
3288  }
3289 
3290 /**
3291  * ice_sysctl_advertise_speed - Display/change link speeds supported by port
3292  * @oidp: sysctl oid structure
3293  * @arg1: pointer to private data structure
3294  * @arg2: unused
3295  * @req: sysctl request pointer
3296  *
3297  * On read: Displays the currently supported speeds
3298  * On write: Sets the device's supported speeds
3299  * Valid input flags: see ICE_SYSCTL_HELP_ADVERTISE_SPEED
3300  */
3301 static int
ice_sysctl_advertise_speed(SYSCTL_HANDLER_ARGS)3302 ice_sysctl_advertise_speed(SYSCTL_HANDLER_ARGS)
3303 {
3304 	struct ice_softc *sc = (struct ice_softc *)arg1;
3305 	struct ice_port_info *pi = sc->hw.port_info;
3306 	struct ice_phy_data phy_data = { 0 };
3307 	device_t dev = sc->dev;
3308 	u16 sysctl_speeds;
3309 	int ret;
3310 
3311 	UNREFERENCED_PARAMETER(arg2);
3312 
3313 	if (ice_driver_is_detaching(sc))
3314 		return (ESHUTDOWN);
3315 
3316 	/* Get the current speeds from the adapter's "active" configuration. */
3317 	phy_data.report_mode = ICE_AQC_REPORT_ACTIVE_CFG;
3318 	ret = ice_intersect_phy_types_and_speeds(sc, &phy_data);
3319 	if (ret) {
3320 		/* Error message already printed within function */
3321 		return (ret);
3322 	}
3323 
3324 	sysctl_speeds = phy_data.user_speeds_intr;
3325 
3326 	ret = sysctl_handle_16(oidp, &sysctl_speeds, 0, req);
3327 	if ((ret) || (req->newptr == NULL))
3328 		return (ret);
3329 
3330 	if (sysctl_speeds > ICE_SYSCTL_SPEEDS_VALID_RANGE) {
3331 		device_printf(dev,
3332 			      "%s: \"%u\" is outside of the range of acceptable values.\n",
3333 			      __func__, sysctl_speeds);
3334 		return (EINVAL);
3335 	}
3336 
3337 	pi->phy.curr_user_speed_req = sysctl_speeds;
3338 
3339 	if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) &&
3340 	    !sc->link_up && !(if_getflags(sc->ifp) & IFF_UP))
3341 		return 0;
3342 
3343 	/* Apply settings requested by user */
3344 	return ice_apply_saved_phy_cfg(sc, ICE_APPLY_LS);
3345 }
3346 
3347 #define ICE_SYSCTL_HELP_FEC_CONFIG			\
3348 "\nDisplay or set the port's requested FEC mode."	\
3349 "\n\tauto - " ICE_FEC_STRING_AUTO			\
3350 "\n\tfc - " ICE_FEC_STRING_BASER			\
3351 "\n\trs - " ICE_FEC_STRING_RS				\
3352 "\n\tnone - " ICE_FEC_STRING_NONE			\
3353 "\nEither of the left or right strings above can be used to set the requested mode."
3354 
3355 /**
3356  * ice_sysctl_fec_config - Display/change the configured FEC mode
3357  * @oidp: sysctl oid structure
3358  * @arg1: pointer to private data structure
3359  * @arg2: unused
3360  * @req: sysctl request pointer
3361  *
3362  * On read: Displays the configured FEC mode
3363  * On write: Sets the device's FEC mode to the input string, if it's valid.
3364  * Valid input strings: see ICE_SYSCTL_HELP_FEC_CONFIG
3365  */
3366 static int
ice_sysctl_fec_config(SYSCTL_HANDLER_ARGS)3367 ice_sysctl_fec_config(SYSCTL_HANDLER_ARGS)
3368 {
3369 	struct ice_softc *sc = (struct ice_softc *)arg1;
3370 	struct ice_port_info *pi = sc->hw.port_info;
3371 	enum ice_fec_mode new_mode;
3372 	device_t dev = sc->dev;
3373 	char req_fec[32];
3374 	int ret;
3375 
3376 	UNREFERENCED_PARAMETER(arg2);
3377 
3378 	if (ice_driver_is_detaching(sc))
3379 		return (ESHUTDOWN);
3380 
3381 	bzero(req_fec, sizeof(req_fec));
3382 	strlcpy(req_fec, ice_requested_fec_mode(pi), sizeof(req_fec));
3383 
3384 	ret = sysctl_handle_string(oidp, req_fec, sizeof(req_fec), req);
3385 	if ((ret) || (req->newptr == NULL))
3386 		return (ret);
3387 
3388 	if (strcmp(req_fec, "auto") == 0 ||
3389 	    strcmp(req_fec, ice_fec_str(ICE_FEC_AUTO)) == 0) {
3390 		if (sc->allow_no_fec_mod_in_auto)
3391 			new_mode = ICE_FEC_DIS_AUTO;
3392 		else
3393 			new_mode = ICE_FEC_AUTO;
3394 	} else if (strcmp(req_fec, "fc") == 0 ||
3395 	    strcmp(req_fec, ice_fec_str(ICE_FEC_BASER)) == 0) {
3396 		new_mode = ICE_FEC_BASER;
3397 	} else if (strcmp(req_fec, "rs") == 0 ||
3398 	    strcmp(req_fec, ice_fec_str(ICE_FEC_RS)) == 0) {
3399 		new_mode = ICE_FEC_RS;
3400 	} else if (strcmp(req_fec, "none") == 0 ||
3401 	    strcmp(req_fec, ice_fec_str(ICE_FEC_NONE)) == 0) {
3402 		new_mode = ICE_FEC_NONE;
3403 	} else {
3404 		device_printf(dev,
3405 		    "%s: \"%s\" is not a valid FEC mode\n",
3406 		    __func__, req_fec);
3407 		return (EINVAL);
3408 	}
3409 
3410 	/* Cache user FEC mode for later link ups */
3411 	pi->phy.curr_user_fec_req = new_mode;
3412 
3413 	if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) && !sc->link_up)
3414 		return 0;
3415 
3416 	/* Apply settings requested by user */
3417 	return ice_apply_saved_phy_cfg(sc, ICE_APPLY_FEC);
3418 }
3419 
3420 /**
3421  * ice_sysctl_negotiated_fec - Display the negotiated FEC mode on the link
3422  * @oidp: sysctl oid structure
3423  * @arg1: pointer to private data structure
3424  * @arg2: unused
3425  * @req: sysctl request pointer
3426  *
3427  * On read: Displays the negotiated FEC mode, in a string
3428  */
3429 static int
ice_sysctl_negotiated_fec(SYSCTL_HANDLER_ARGS)3430 ice_sysctl_negotiated_fec(SYSCTL_HANDLER_ARGS)
3431 {
3432 	struct ice_softc *sc = (struct ice_softc *)arg1;
3433 	struct ice_hw *hw = &sc->hw;
3434 	char neg_fec[32];
3435 	int ret;
3436 
3437 	UNREFERENCED_PARAMETER(arg2);
3438 
3439 	if (ice_driver_is_detaching(sc))
3440 		return (ESHUTDOWN);
3441 
3442 	/* Copy const string into a buffer to drop const qualifier */
3443 	bzero(neg_fec, sizeof(neg_fec));
3444 	strlcpy(neg_fec, ice_negotiated_fec_mode(hw->port_info), sizeof(neg_fec));
3445 
3446 	ret = sysctl_handle_string(oidp, neg_fec, 0, req);
3447 	if (req->newptr != NULL)
3448 		return (EPERM);
3449 
3450 	return (ret);
3451 }
3452 
3453 #define ICE_SYSCTL_HELP_FC_CONFIG				\
3454 "\nDisplay or set the port's advertised flow control mode.\n"	\
3455 "\t0 - " ICE_FC_STRING_NONE					\
3456 "\n\t1 - " ICE_FC_STRING_RX					\
3457 "\n\t2 - " ICE_FC_STRING_TX					\
3458 "\n\t3 - " ICE_FC_STRING_FULL					\
3459 "\nEither the numbers or the strings above can be used to set the advertised mode."
3460 
3461 /**
3462  * ice_sysctl_fc_config - Display/change the advertised flow control mode
3463  * @oidp: sysctl oid structure
3464  * @arg1: pointer to private data structure
3465  * @arg2: unused
3466  * @req: sysctl request pointer
3467  *
3468  * On read: Displays the configured flow control mode
3469  * On write: Sets the device's flow control mode to the input, if it's valid.
3470  * Valid input strings: see ICE_SYSCTL_HELP_FC_CONFIG
3471  */
3472 static int
ice_sysctl_fc_config(SYSCTL_HANDLER_ARGS)3473 ice_sysctl_fc_config(SYSCTL_HANDLER_ARGS)
3474 {
3475 	struct ice_softc *sc = (struct ice_softc *)arg1;
3476 	struct ice_port_info *pi = sc->hw.port_info;
3477 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
3478 	enum ice_fc_mode old_mode, new_mode;
3479 	struct ice_hw *hw = &sc->hw;
3480 	device_t dev = sc->dev;
3481 	int status;
3482 	int ret, fc_num;
3483 	bool mode_set = false;
3484 	struct sbuf buf;
3485 	char *fc_str_end;
3486 	char fc_str[32];
3487 
3488 	UNREFERENCED_PARAMETER(arg2);
3489 
3490 	if (ice_driver_is_detaching(sc))
3491 		return (ESHUTDOWN);
3492 
3493 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG,
3494 				     &pcaps, NULL);
3495 	if (status) {
3496 		device_printf(dev,
3497 		    "%s: ice_aq_get_phy_caps failed; status %s, aq_err %s\n",
3498 		    __func__, ice_status_str(status),
3499 		    ice_aq_str(hw->adminq.sq_last_status));
3500 		return (EIO);
3501 	}
3502 
3503 	/* Convert HW response format to SW enum value */
3504 	if ((pcaps.caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
3505 	    (pcaps.caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE))
3506 		old_mode = ICE_FC_FULL;
3507 	else if (pcaps.caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
3508 		old_mode = ICE_FC_TX_PAUSE;
3509 	else if (pcaps.caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
3510 		old_mode = ICE_FC_RX_PAUSE;
3511 	else
3512 		old_mode = ICE_FC_NONE;
3513 
3514 	/* Create "old" string for output */
3515 	bzero(fc_str, sizeof(fc_str));
3516 	sbuf_new_for_sysctl(&buf, fc_str, sizeof(fc_str), req);
3517 	sbuf_printf(&buf, "%d<%s>", old_mode, ice_fc_str(old_mode));
3518 	sbuf_finish(&buf);
3519 	sbuf_delete(&buf);
3520 
3521 	ret = sysctl_handle_string(oidp, fc_str, sizeof(fc_str), req);
3522 	if ((ret) || (req->newptr == NULL))
3523 		return (ret);
3524 
3525 	/* Try to parse input as a string, first */
3526 	if (strcasecmp(ice_fc_str(ICE_FC_FULL), fc_str) == 0) {
3527 		new_mode = ICE_FC_FULL;
3528 		mode_set = true;
3529 	}
3530 	else if (strcasecmp(ice_fc_str(ICE_FC_TX_PAUSE), fc_str) == 0) {
3531 		new_mode = ICE_FC_TX_PAUSE;
3532 		mode_set = true;
3533 	}
3534 	else if (strcasecmp(ice_fc_str(ICE_FC_RX_PAUSE), fc_str) == 0) {
3535 		new_mode = ICE_FC_RX_PAUSE;
3536 		mode_set = true;
3537 	}
3538 	else if (strcasecmp(ice_fc_str(ICE_FC_NONE), fc_str) == 0) {
3539 		new_mode = ICE_FC_NONE;
3540 		mode_set = true;
3541 	}
3542 
3543 	/*
3544 	 * Then check if it's an integer, for compatibility with the method
3545 	 * used in older drivers.
3546 	 */
3547 	if (!mode_set) {
3548 		fc_num = strtol(fc_str, &fc_str_end, 0);
3549 		if (fc_str_end == fc_str)
3550 			fc_num = -1;
3551 		switch (fc_num) {
3552 		case 3:
3553 			new_mode = ICE_FC_FULL;
3554 			break;
3555 		case 2:
3556 			new_mode = ICE_FC_TX_PAUSE;
3557 			break;
3558 		case 1:
3559 			new_mode = ICE_FC_RX_PAUSE;
3560 			break;
3561 		case 0:
3562 			new_mode = ICE_FC_NONE;
3563 			break;
3564 		default:
3565 			device_printf(dev,
3566 			    "%s: \"%s\" is not a valid flow control mode\n",
3567 			    __func__, fc_str);
3568 			return (EINVAL);
3569 		}
3570 	}
3571 
3572 	/* Save flow control mode from user */
3573 	pi->phy.curr_user_fc_req = new_mode;
3574 
3575 	/* Turn off Priority Flow Control when Link Flow Control is enabled */
3576 	if ((hw->port_info->qos_cfg.is_sw_lldp) &&
3577 	    (hw->port_info->qos_cfg.local_dcbx_cfg.pfc.pfcena != 0) &&
3578 	    (new_mode != ICE_FC_NONE)) {
3579 		ret = ice_config_pfc(sc, 0x0);
3580 		if (ret)
3581 			return (ret);
3582 	}
3583 
3584 	if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) && !sc->link_up)
3585 		return 0;
3586 
3587 	/* Apply settings requested by user */
3588 	return ice_apply_saved_phy_cfg(sc, ICE_APPLY_FC);
3589 }
3590 
3591 /**
3592  * ice_sysctl_negotiated_fc - Display currently negotiated FC mode
3593  * @oidp: sysctl oid structure
3594  * @arg1: pointer to private data structure
3595  * @arg2: unused
3596  * @req: sysctl request pointer
3597  *
3598  * On read: Displays the currently negotiated flow control settings.
3599  *
3600  * If link is not established, this will report ICE_FC_NONE, as no flow
3601  * control is negotiated while link is down.
3602  */
3603 static int
ice_sysctl_negotiated_fc(SYSCTL_HANDLER_ARGS)3604 ice_sysctl_negotiated_fc(SYSCTL_HANDLER_ARGS)
3605 {
3606 	struct ice_softc *sc = (struct ice_softc *)arg1;
3607 	struct ice_port_info *pi = sc->hw.port_info;
3608 	const char *negotiated_fc;
3609 
3610 	UNREFERENCED_PARAMETER(arg2);
3611 
3612 	if (ice_driver_is_detaching(sc))
3613 		return (ESHUTDOWN);
3614 
3615 	negotiated_fc = ice_flowcontrol_mode(pi);
3616 
3617 	return sysctl_handle_string(oidp, __DECONST(char *, negotiated_fc), 0, req);
3618 }
3619 
3620 /**
3621  * __ice_sysctl_phy_type_handler - Display/change supported PHY types/speeds
3622  * @oidp: sysctl oid structure
3623  * @arg1: pointer to private data structure
3624  * @arg2: unused
3625  * @req: sysctl request pointer
3626  * @is_phy_type_high: if true, handle the high PHY type instead of the low PHY type
3627  *
3628  * Private handler for phy_type_high and phy_type_low sysctls.
3629  */
3630 static int
__ice_sysctl_phy_type_handler(SYSCTL_HANDLER_ARGS,bool is_phy_type_high)3631 __ice_sysctl_phy_type_handler(SYSCTL_HANDLER_ARGS, bool is_phy_type_high)
3632 {
3633 	struct ice_softc *sc = (struct ice_softc *)arg1;
3634 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
3635 	struct ice_aqc_set_phy_cfg_data cfg = { 0 };
3636 	struct ice_hw *hw = &sc->hw;
3637 	device_t dev = sc->dev;
3638 	int status;
3639 	uint64_t types;
3640 	int ret;
3641 
3642 	UNREFERENCED_PARAMETER(arg2);
3643 
3644 	if (ice_driver_is_detaching(sc))
3645 		return (ESHUTDOWN);
3646 
3647 	status = ice_aq_get_phy_caps(hw->port_info, false, ICE_AQC_REPORT_ACTIVE_CFG,
3648 				     &pcaps, NULL);
3649 	if (status) {
3650 		device_printf(dev,
3651 		    "%s: ice_aq_get_phy_caps failed; status %s, aq_err %s\n",
3652 		    __func__, ice_status_str(status),
3653 		    ice_aq_str(hw->adminq.sq_last_status));
3654 		return (EIO);
3655 	}
3656 
3657 	if (is_phy_type_high)
3658 		types = pcaps.phy_type_high;
3659 	else
3660 		types = pcaps.phy_type_low;
3661 
3662 	ret = sysctl_handle_64(oidp, &types, sizeof(types), req);
3663 	if ((ret) || (req->newptr == NULL))
3664 		return (ret);
3665 
3666 	ice_copy_phy_caps_to_cfg(hw->port_info, &pcaps, &cfg);
3667 
3668 	if (is_phy_type_high)
3669 		cfg.phy_type_high = types & hw->port_info->phy.phy_type_high;
3670 	else
3671 		cfg.phy_type_low = types & hw->port_info->phy.phy_type_low;
3672 	cfg.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
3673 
3674 	status = ice_aq_set_phy_cfg(hw, hw->port_info, &cfg, NULL);
3675 	if (status) {
3676 		device_printf(dev,
3677 		    "%s: ice_aq_set_phy_cfg failed; status %s, aq_err %s\n",
3678 		    __func__, ice_status_str(status),
3679 		    ice_aq_str(hw->adminq.sq_last_status));
3680 		return (EIO);
3681 	}
3682 
3683 	return (0);
3684 
3685 }
3686 
3687 /**
3688  * ice_sysctl_phy_type_low - Display/change supported lower PHY types/speeds
3689  * @oidp: sysctl oid structure
3690  * @arg1: pointer to private data structure
3691  * @arg2: unused
3692  * @req: sysctl request pointer
3693  *
3694  * On read: Displays the currently supported lower PHY types
3695  * On write: Sets the device's supported low PHY types
3696  */
3697 static int
ice_sysctl_phy_type_low(SYSCTL_HANDLER_ARGS)3698 ice_sysctl_phy_type_low(SYSCTL_HANDLER_ARGS)
3699 {
3700 	return __ice_sysctl_phy_type_handler(oidp, arg1, arg2, req, false);
3701 }
3702 
3703 /**
3704  * ice_sysctl_phy_type_high - Display/change supported higher PHY types/speeds
3705  * @oidp: sysctl oid structure
3706  * @arg1: pointer to private data structure
3707  * @arg2: unused
3708  * @req: sysctl request pointer
3709  *
3710  * On read: Displays the currently supported higher PHY types
3711  * On write: Sets the device's supported high PHY types
3712  */
3713 static int
ice_sysctl_phy_type_high(SYSCTL_HANDLER_ARGS)3714 ice_sysctl_phy_type_high(SYSCTL_HANDLER_ARGS)
3715 {
3716 	return __ice_sysctl_phy_type_handler(oidp, arg1, arg2, req, true);
3717 }
3718 
3719 /**
3720  * ice_sysctl_phy_caps - Display response from Get PHY abililties
3721  * @oidp: sysctl oid structure
3722  * @arg1: pointer to private data structure
3723  * @arg2: unused
3724  * @req: sysctl request pointer
3725  * @report_mode: the mode to report
3726  *
3727  * On read: Display the response from Get PHY abillities with the given report
3728  * mode.
3729  */
3730 static int
ice_sysctl_phy_caps(SYSCTL_HANDLER_ARGS,u8 report_mode)3731 ice_sysctl_phy_caps(SYSCTL_HANDLER_ARGS, u8 report_mode)
3732 {
3733 	struct ice_softc *sc = (struct ice_softc *)arg1;
3734 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
3735 	struct ice_hw *hw = &sc->hw;
3736 	struct ice_port_info *pi = hw->port_info;
3737 	device_t dev = sc->dev;
3738 	int status;
3739 	int ret;
3740 
3741 	UNREFERENCED_PARAMETER(arg2);
3742 
3743 	ret = priv_check(curthread, PRIV_DRIVER);
3744 	if (ret)
3745 		return (ret);
3746 
3747 	if (ice_driver_is_detaching(sc))
3748 		return (ESHUTDOWN);
3749 
3750 	status = ice_aq_get_phy_caps(pi, true, report_mode, &pcaps, NULL);
3751 	if (status) {
3752 		device_printf(dev,
3753 		    "%s: ice_aq_get_phy_caps failed; status %s, aq_err %s\n",
3754 		    __func__, ice_status_str(status),
3755 		    ice_aq_str(hw->adminq.sq_last_status));
3756 		return (EIO);
3757 	}
3758 
3759 	ret = sysctl_handle_opaque(oidp, &pcaps, sizeof(pcaps), req);
3760 	if (req->newptr != NULL)
3761 		return (EPERM);
3762 
3763 	return (ret);
3764 }
3765 
3766 /**
3767  * ice_sysctl_phy_sw_caps - Display response from Get PHY abililties
3768  * @oidp: sysctl oid structure
3769  * @arg1: pointer to private data structure
3770  * @arg2: unused
3771  * @req: sysctl request pointer
3772  *
3773  * On read: Display the response from Get PHY abillities reporting the last
3774  * software configuration.
3775  */
3776 static int
ice_sysctl_phy_sw_caps(SYSCTL_HANDLER_ARGS)3777 ice_sysctl_phy_sw_caps(SYSCTL_HANDLER_ARGS)
3778 {
3779 	return ice_sysctl_phy_caps(oidp, arg1, arg2, req,
3780 				   ICE_AQC_REPORT_ACTIVE_CFG);
3781 }
3782 
3783 /**
3784  * ice_sysctl_phy_nvm_caps - Display response from Get PHY abililties
3785  * @oidp: sysctl oid structure
3786  * @arg1: pointer to private data structure
3787  * @arg2: unused
3788  * @req: sysctl request pointer
3789  *
3790  * On read: Display the response from Get PHY abillities reporting the NVM
3791  * configuration.
3792  */
3793 static int
ice_sysctl_phy_nvm_caps(SYSCTL_HANDLER_ARGS)3794 ice_sysctl_phy_nvm_caps(SYSCTL_HANDLER_ARGS)
3795 {
3796 	return ice_sysctl_phy_caps(oidp, arg1, arg2, req,
3797 				   ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA);
3798 }
3799 
3800 /**
3801  * ice_sysctl_phy_topo_caps - Display response from Get PHY abililties
3802  * @oidp: sysctl oid structure
3803  * @arg1: pointer to private data structure
3804  * @arg2: unused
3805  * @req: sysctl request pointer
3806  *
3807  * On read: Display the response from Get PHY abillities reporting the
3808  * topology configuration.
3809  */
3810 static int
ice_sysctl_phy_topo_caps(SYSCTL_HANDLER_ARGS)3811 ice_sysctl_phy_topo_caps(SYSCTL_HANDLER_ARGS)
3812 {
3813 	return ice_sysctl_phy_caps(oidp, arg1, arg2, req,
3814 				   ICE_AQC_REPORT_TOPO_CAP_MEDIA);
3815 }
3816 
3817 /**
3818  * ice_sysctl_phy_link_status - Display response from Get Link Status
3819  * @oidp: sysctl oid structure
3820  * @arg1: pointer to private data structure
3821  * @arg2: unused
3822  * @req: sysctl request pointer
3823  *
3824  * On read: Display the response from firmware for the Get Link Status
3825  * request.
3826  */
3827 static int
ice_sysctl_phy_link_status(SYSCTL_HANDLER_ARGS)3828 ice_sysctl_phy_link_status(SYSCTL_HANDLER_ARGS)
3829 {
3830 	struct ice_aqc_get_link_status_data link_data = { 0 };
3831 	struct ice_softc *sc = (struct ice_softc *)arg1;
3832 	struct ice_hw *hw = &sc->hw;
3833 	struct ice_port_info *pi = hw->port_info;
3834 	struct ice_aqc_get_link_status *resp;
3835 	struct ice_aq_desc desc;
3836 	device_t dev = sc->dev;
3837 	int status;
3838 	int ret;
3839 
3840 	UNREFERENCED_PARAMETER(arg2);
3841 
3842 	/*
3843 	 * Ensure that only contexts with driver privilege are allowed to
3844 	 * access this information
3845 	 */
3846 	ret = priv_check(curthread, PRIV_DRIVER);
3847 	if (ret)
3848 		return (ret);
3849 
3850 	if (ice_driver_is_detaching(sc))
3851 		return (ESHUTDOWN);
3852 
3853 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_status);
3854 	resp = &desc.params.get_link_status;
3855 	resp->lport_num = pi->lport;
3856 
3857 	status = ice_aq_send_cmd(hw, &desc, &link_data, sizeof(link_data), NULL);
3858 	if (status) {
3859 		device_printf(dev,
3860 		    "%s: ice_aq_send_cmd failed; status %s, aq_err %s\n",
3861 		    __func__, ice_status_str(status),
3862 		    ice_aq_str(hw->adminq.sq_last_status));
3863 		return (EIO);
3864 	}
3865 
3866 	ret = sysctl_handle_opaque(oidp, &link_data, sizeof(link_data), req);
3867 	if (req->newptr != NULL)
3868 		return (EPERM);
3869 
3870 	return (ret);
3871 }
3872 
3873 /**
3874  * ice_sysctl_fw_cur_lldp_persist_status - Display current FW LLDP status
3875  * @oidp: sysctl oid structure
3876  * @arg1: pointer to private softc structure
3877  * @arg2: unused
3878  * @req: sysctl request pointer
3879  *
3880  * On read: Displays current persistent LLDP status.
3881  */
3882 static int
ice_sysctl_fw_cur_lldp_persist_status(SYSCTL_HANDLER_ARGS)3883 ice_sysctl_fw_cur_lldp_persist_status(SYSCTL_HANDLER_ARGS)
3884 {
3885 	struct ice_softc *sc = (struct ice_softc *)arg1;
3886 	struct ice_hw *hw = &sc->hw;
3887 	device_t dev = sc->dev;
3888 	int status;
3889 	struct sbuf *sbuf;
3890 	u32 lldp_state;
3891 
3892 	UNREFERENCED_PARAMETER(arg2);
3893 	UNREFERENCED_PARAMETER(oidp);
3894 
3895 	if (ice_driver_is_detaching(sc))
3896 		return (ESHUTDOWN);
3897 
3898 	status = ice_get_cur_lldp_persist_status(hw, &lldp_state);
3899 	if (status) {
3900 		device_printf(dev,
3901 		    "Could not acquire current LLDP persistence status, err %s aq_err %s\n",
3902 		    ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
3903 		return (EIO);
3904 	}
3905 
3906 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
3907 	sbuf_printf(sbuf, "%s", ice_fw_lldp_status(lldp_state));
3908 	sbuf_finish(sbuf);
3909 	sbuf_delete(sbuf);
3910 
3911 	return (0);
3912 }
3913 
3914 /**
3915  * ice_sysctl_fw_dflt_lldp_persist_status - Display default FW LLDP status
3916  * @oidp: sysctl oid structure
3917  * @arg1: pointer to private softc structure
3918  * @arg2: unused
3919  * @req: sysctl request pointer
3920  *
3921  * On read: Displays default persistent LLDP status.
3922  */
3923 static int
ice_sysctl_fw_dflt_lldp_persist_status(SYSCTL_HANDLER_ARGS)3924 ice_sysctl_fw_dflt_lldp_persist_status(SYSCTL_HANDLER_ARGS)
3925 {
3926 	struct ice_softc *sc = (struct ice_softc *)arg1;
3927 	struct ice_hw *hw = &sc->hw;
3928 	device_t dev = sc->dev;
3929 	int status;
3930 	struct sbuf *sbuf;
3931 	u32 lldp_state;
3932 
3933 	UNREFERENCED_PARAMETER(arg2);
3934 	UNREFERENCED_PARAMETER(oidp);
3935 
3936 	if (ice_driver_is_detaching(sc))
3937 		return (ESHUTDOWN);
3938 
3939 	status = ice_get_dflt_lldp_persist_status(hw, &lldp_state);
3940 	if (status) {
3941 		device_printf(dev,
3942 		    "Could not acquire default LLDP persistence status, err %s aq_err %s\n",
3943 		    ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
3944 		return (EIO);
3945 	}
3946 
3947 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
3948 	sbuf_printf(sbuf, "%s", ice_fw_lldp_status(lldp_state));
3949 	sbuf_finish(sbuf);
3950 	sbuf_delete(sbuf);
3951 
3952 	return (0);
3953 }
3954 
3955 /**
3956  * ice_dscp_is_mapped - Check for non-zero DSCP to TC mappings
3957  * @dcbcfg: Configuration struct to check for mappings in
3958  *
3959  * @return true if there exists a non-zero DSCP to TC mapping
3960  * inside the input DCB configuration struct.
3961  */
3962 static bool
ice_dscp_is_mapped(struct ice_dcbx_cfg * dcbcfg)3963 ice_dscp_is_mapped(struct ice_dcbx_cfg *dcbcfg)
3964 {
3965 	for (int i = 0; i < ICE_DSCP_NUM_VAL; i++)
3966 		if (dcbcfg->dscp_map[i] != 0)
3967 			return (true);
3968 
3969 	return (false);
3970 }
3971 
3972 #define ICE_SYSCTL_HELP_FW_LLDP_AGENT	\
3973 "\nDisplay or change FW LLDP agent state:" \
3974 "\n\t0 - disabled"			\
3975 "\n\t1 - enabled"
3976 
3977 /**
3978  * ice_sysctl_fw_lldp_agent - Display or change the FW LLDP agent status
3979  * @oidp: sysctl oid structure
3980  * @arg1: pointer to private softc structure
3981  * @arg2: unused
3982  * @req: sysctl request pointer
3983  *
3984  * On read: Displays whether the FW LLDP agent is running
3985  * On write: Persistently enables or disables the FW LLDP agent
3986  */
3987 static int
ice_sysctl_fw_lldp_agent(SYSCTL_HANDLER_ARGS)3988 ice_sysctl_fw_lldp_agent(SYSCTL_HANDLER_ARGS)
3989 {
3990 	struct ice_softc *sc = (struct ice_softc *)arg1;
3991 	struct ice_dcbx_cfg *local_dcbx_cfg;
3992 	struct ice_hw *hw = &sc->hw;
3993 	device_t dev = sc->dev;
3994 	int status;
3995 	int ret;
3996 	u32 old_state;
3997 	u8 fw_lldp_enabled;
3998 	bool retried_start_lldp = false;
3999 
4000 	UNREFERENCED_PARAMETER(arg2);
4001 
4002 	if (ice_driver_is_detaching(sc))
4003 		return (ESHUTDOWN);
4004 
4005 	status = ice_get_cur_lldp_persist_status(hw, &old_state);
4006 	if (status) {
4007 		device_printf(dev,
4008 		    "Could not acquire current LLDP persistence status, err %s aq_err %s\n",
4009 		    ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
4010 		return (EIO);
4011 	}
4012 
4013 	if (old_state > ICE_LLDP_ADMINSTATUS_ENA_RXTX) {
4014 		status = ice_get_dflt_lldp_persist_status(hw, &old_state);
4015 		if (status) {
4016 			device_printf(dev,
4017 			    "Could not acquire default LLDP persistence status, err %s aq_err %s\n",
4018 			    ice_status_str(status),
4019 			    ice_aq_str(hw->adminq.sq_last_status));
4020 			return (EIO);
4021 		}
4022 	}
4023 	if (old_state == 0)
4024 		fw_lldp_enabled = false;
4025 	else
4026 		fw_lldp_enabled = true;
4027 
4028 	ret = sysctl_handle_bool(oidp, &fw_lldp_enabled, 0, req);
4029 	if ((ret) || (req->newptr == NULL))
4030 		return (ret);
4031 
4032 	if (old_state == 0 && fw_lldp_enabled == false)
4033 		return (0);
4034 
4035 	if (old_state != 0 && fw_lldp_enabled == true)
4036 		return (0);
4037 
4038 	/* Block transition to FW LLDP if DSCP mode is enabled */
4039 	local_dcbx_cfg = &hw->port_info->qos_cfg.local_dcbx_cfg;
4040 	if ((local_dcbx_cfg->pfc_mode == ICE_QOS_MODE_DSCP) ||
4041 	    ice_dscp_is_mapped(local_dcbx_cfg)) {
4042 		device_printf(dev,
4043 			      "Cannot enable FW-LLDP agent while DSCP QoS is active.\n");
4044 		return (EOPNOTSUPP);
4045 	}
4046 
4047 	if (fw_lldp_enabled == false) {
4048 		status = ice_aq_stop_lldp(hw, true, true, NULL);
4049 		/* EPERM is returned if the LLDP agent is already shutdown */
4050 		if (status && hw->adminq.sq_last_status != ICE_AQ_RC_EPERM) {
4051 			device_printf(dev,
4052 			    "%s: ice_aq_stop_lldp failed; status %s, aq_err %s\n",
4053 			    __func__, ice_status_str(status),
4054 			    ice_aq_str(hw->adminq.sq_last_status));
4055 			return (EIO);
4056 		}
4057 		ice_aq_set_dcb_parameters(hw, true, NULL);
4058 		hw->port_info->qos_cfg.is_sw_lldp = true;
4059 		ice_add_rx_lldp_filter(sc);
4060 	} else {
4061 		ice_del_rx_lldp_filter(sc);
4062 retry_start_lldp:
4063 		status = ice_aq_start_lldp(hw, true, NULL);
4064 		if (status) {
4065 			switch (hw->adminq.sq_last_status) {
4066 			/* EEXIST is returned if the LLDP agent is already started */
4067 			case ICE_AQ_RC_EEXIST:
4068 				break;
4069 			case ICE_AQ_RC_EAGAIN:
4070 				/* Retry command after a 2 second wait */
4071 				if (retried_start_lldp == false) {
4072 					retried_start_lldp = true;
4073 					pause("slldp", ICE_START_LLDP_RETRY_WAIT);
4074 					goto retry_start_lldp;
4075 				}
4076 				/* Fallthrough */
4077 			default:
4078 				device_printf(dev,
4079 				    "%s: ice_aq_start_lldp failed; status %s, aq_err %s\n",
4080 				    __func__, ice_status_str(status),
4081 				    ice_aq_str(hw->adminq.sq_last_status));
4082 				return (EIO);
4083 			}
4084 		}
4085 		ice_start_dcbx_agent(sc);
4086 
4087 		/* Init DCB needs to be done during enabling LLDP to properly
4088 		 * propagate the configuration.
4089 		 */
4090 		status = ice_init_dcb(hw, true);
4091 		if (status) {
4092 			device_printf(dev,
4093 			    "%s: ice_init_dcb failed; status %s, aq_err %s\n",
4094 			    __func__, ice_status_str(status),
4095 			    ice_aq_str(hw->adminq.sq_last_status));
4096 			hw->port_info->qos_cfg.dcbx_status = ICE_DCBX_STATUS_NOT_STARTED;
4097 		}
4098 	}
4099 
4100 	return (ret);
4101 }
4102 
4103 #define ICE_SYSCTL_HELP_ETS_MIN_RATE \
4104 "\nIn FW DCB mode (fw_lldp_agent=1), displays the current ETS bandwidth table." \
4105 "\nIn SW DCB mode, displays and allows setting the table." \
4106 "\nInput must be in the format e.g. 30,10,10,10,10,10,10,10" \
4107 "\nWhere the bandwidth total must add up to 100"
4108 
4109 /**
4110  * ice_sysctl_ets_min_rate - Report/configure ETS bandwidth
4111  * @oidp: sysctl oid structure
4112  * @arg1: pointer to private data structure
4113  * @arg2: unused
4114  * @req: sysctl request pointer
4115  *
4116  * Returns the current ETS TC bandwidth table
4117  * cached by the driver.
4118  *
4119  * In SW DCB mode this sysctl also accepts a value that will
4120  * be sent to the firmware for configuration.
4121  */
4122 static int
ice_sysctl_ets_min_rate(SYSCTL_HANDLER_ARGS)4123 ice_sysctl_ets_min_rate(SYSCTL_HANDLER_ARGS)
4124 {
4125 	struct ice_softc *sc = (struct ice_softc *)arg1;
4126 	struct ice_dcbx_cfg *local_dcbx_cfg;
4127 	struct ice_port_info *pi;
4128 	struct ice_hw *hw = &sc->hw;
4129 	device_t dev = sc->dev;
4130 	int status;
4131 	struct sbuf *sbuf;
4132 	int ret;
4133 
4134 	/* Store input rates from user */
4135 	char ets_user_buf[128] = "";
4136 	u8 new_ets_table[ICE_MAX_TRAFFIC_CLASS] = {};
4137 
4138 	UNREFERENCED_PARAMETER(arg2);
4139 
4140 	if (ice_driver_is_detaching(sc))
4141 		return (ESHUTDOWN);
4142 
4143 	if (req->oldptr == NULL && req->newptr == NULL) {
4144 		ret = SYSCTL_OUT(req, 0, 128);
4145 		return (ret);
4146 	}
4147 
4148 	pi = hw->port_info;
4149 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
4150 
4151 	sbuf = sbuf_new(NULL, ets_user_buf, 128, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
4152 
4153 	/* Format ETS BW data for output */
4154 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
4155 		sbuf_printf(sbuf, "%d", local_dcbx_cfg->etscfg.tcbwtable[i]);
4156 		if (i != ICE_MAX_TRAFFIC_CLASS - 1)
4157 			sbuf_printf(sbuf, ",");
4158 	}
4159 
4160 	sbuf_finish(sbuf);
4161 	sbuf_delete(sbuf);
4162 
4163 	/* Read in the new ETS values */
4164 	ret = sysctl_handle_string(oidp, ets_user_buf, sizeof(ets_user_buf), req);
4165 	if ((ret) || (req->newptr == NULL))
4166 		return (ret);
4167 
4168 	/* Don't allow setting changes in FW DCB mode */
4169 	if (!hw->port_info->qos_cfg.is_sw_lldp)
4170 		return (EPERM);
4171 
4172 	ret = ice_ets_str_to_tbl(ets_user_buf, new_ets_table, 100);
4173 	if (ret) {
4174 		device_printf(dev, "%s: Could not parse input BW table: %s\n",
4175 		    __func__, ets_user_buf);
4176 		return (ret);
4177 	}
4178 
4179 	if (!ice_check_ets_bw(new_ets_table)) {
4180 		device_printf(dev, "%s: Bandwidth sum does not equal 100: %s\n",
4181 		    __func__, ets_user_buf);
4182 		return (EINVAL);
4183 	}
4184 
4185 	memcpy(local_dcbx_cfg->etscfg.tcbwtable, new_ets_table,
4186 	    sizeof(new_ets_table));
4187 
4188 	/* If BW > 0, then set TSA entry to 2 */
4189 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
4190 		if (new_ets_table[i] > 0)
4191 			local_dcbx_cfg->etscfg.tsatable[i] = 2;
4192 		else
4193 			local_dcbx_cfg->etscfg.tsatable[i] = 0;
4194 	}
4195 	local_dcbx_cfg->etscfg.willing = 0;
4196 	local_dcbx_cfg->etsrec = local_dcbx_cfg->etscfg;
4197 	local_dcbx_cfg->app_mode = ICE_DCBX_APPS_NON_WILLING;
4198 
4199 	status = ice_set_dcb_cfg(pi);
4200 	if (status) {
4201 		device_printf(dev,
4202 		    "%s: Failed to set DCB config; status %s, aq_err %s\n",
4203 		    __func__, ice_status_str(status),
4204 		    ice_aq_str(hw->adminq.sq_last_status));
4205 		return (EIO);
4206 	}
4207 
4208 	ice_do_dcb_reconfig(sc, false);
4209 
4210 	return (0);
4211 }
4212 
4213 #define ICE_SYSCTL_HELP_UP2TC_MAP \
4214 "\nIn FW DCB mode (fw_lldp_agent=1), displays the current ETS priority assignment table." \
4215 "\nIn SW DCB mode, displays and allows setting the table." \
4216 "\nInput must be in this format: 0,1,2,3,4,5,6,7" \
4217 "\nWhere the 1st number is the TC for UP0, 2nd number is the TC for UP1, etc"
4218 
4219 /**
4220  * ice_sysctl_up2tc_map - Report or configure UP2TC mapping
4221  * @oidp: sysctl oid structure
4222  * @arg1: pointer to private data structure
4223  * @arg2: unused
4224  * @req: sysctl request pointer
4225  *
4226  * In FW DCB mode, returns the current ETS prio table /
4227  * UP2TC mapping from the local MIB.
4228  *
4229  * In SW DCB mode this sysctl also accepts a value that will
4230  * be sent to the firmware for configuration.
4231  */
4232 static int
ice_sysctl_up2tc_map(SYSCTL_HANDLER_ARGS)4233 ice_sysctl_up2tc_map(SYSCTL_HANDLER_ARGS)
4234 {
4235 	struct ice_softc *sc = (struct ice_softc *)arg1;
4236 	struct ice_dcbx_cfg *local_dcbx_cfg;
4237 	struct ice_port_info *pi;
4238 	struct ice_hw *hw = &sc->hw;
4239 	device_t dev = sc->dev;
4240 	int status;
4241 	struct sbuf *sbuf;
4242 	int ret;
4243 
4244 	/* Store input rates from user */
4245 	char up2tc_user_buf[128] = "";
4246 	/* This array is indexed by UP, not TC */
4247 	u8 new_up2tc[ICE_MAX_TRAFFIC_CLASS] = {};
4248 
4249 	UNREFERENCED_PARAMETER(arg2);
4250 
4251 	if (ice_driver_is_detaching(sc))
4252 		return (ESHUTDOWN);
4253 
4254 	if (req->oldptr == NULL && req->newptr == NULL) {
4255 		ret = SYSCTL_OUT(req, 0, 128);
4256 		return (ret);
4257 	}
4258 
4259 	pi = hw->port_info;
4260 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
4261 
4262 	sbuf = sbuf_new(NULL, up2tc_user_buf, 128, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
4263 
4264 	/* Format ETS Priority Mapping Table for output */
4265 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
4266 		sbuf_printf(sbuf, "%d", local_dcbx_cfg->etscfg.prio_table[i]);
4267 		if (i != ICE_MAX_TRAFFIC_CLASS - 1)
4268 			sbuf_printf(sbuf, ",");
4269 	}
4270 
4271 	sbuf_finish(sbuf);
4272 	sbuf_delete(sbuf);
4273 
4274 	/* Read in the new ETS priority mapping */
4275 	ret = sysctl_handle_string(oidp, up2tc_user_buf, sizeof(up2tc_user_buf), req);
4276 	if ((ret) || (req->newptr == NULL))
4277 		return (ret);
4278 
4279 	/* Don't allow setting changes in FW DCB mode */
4280 	if (!hw->port_info->qos_cfg.is_sw_lldp)
4281 		return (EPERM);
4282 
4283 	ret = ice_ets_str_to_tbl(up2tc_user_buf, new_up2tc,
4284 	    ICE_MAX_TRAFFIC_CLASS - 1);
4285 	if (ret) {
4286 		device_printf(dev, "%s: Could not parse input priority assignment table: %s\n",
4287 		    __func__, up2tc_user_buf);
4288 		return (ret);
4289 	}
4290 
4291 	/* Prepare updated ETS CFG/REC TLVs */
4292 	memcpy(local_dcbx_cfg->etscfg.prio_table, new_up2tc,
4293 	    sizeof(new_up2tc));
4294 	memcpy(local_dcbx_cfg->etsrec.prio_table, new_up2tc,
4295 	    sizeof(new_up2tc));
4296 
4297 	status = ice_set_dcb_cfg(pi);
4298 	if (status) {
4299 		device_printf(dev,
4300 		    "%s: Failed to set DCB config; status %s, aq_err %s\n",
4301 		    __func__, ice_status_str(status),
4302 		    ice_aq_str(hw->adminq.sq_last_status));
4303 		return (EIO);
4304 	}
4305 
4306 	ice_do_dcb_reconfig(sc, false);
4307 
4308 	return (0);
4309 }
4310 
4311 /**
4312  * ice_config_pfc - helper function to set PFC config in FW
4313  * @sc: device private structure
4314  * @new_mode: bit flags indicating PFC status for TCs
4315  *
4316  * @pre must be in SW DCB mode
4317  *
4318  * Configures the driver's local PFC TLV and sends it to the
4319  * FW for configuration, then reconfigures the driver/VSI
4320  * for DCB if needed.
4321  */
4322 static int
ice_config_pfc(struct ice_softc * sc,u8 new_mode)4323 ice_config_pfc(struct ice_softc *sc, u8 new_mode)
4324 {
4325 	struct ice_dcbx_cfg *local_dcbx_cfg;
4326 	struct ice_hw *hw = &sc->hw;
4327 	struct ice_port_info *pi;
4328 	device_t dev = sc->dev;
4329 	int status;
4330 
4331 	pi = hw->port_info;
4332 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
4333 
4334 	/* Prepare updated PFC TLV */
4335 	local_dcbx_cfg->pfc.pfcena = new_mode;
4336 	local_dcbx_cfg->pfc.pfccap = ICE_MAX_TRAFFIC_CLASS;
4337 	local_dcbx_cfg->pfc.willing = 0;
4338 	local_dcbx_cfg->pfc.mbc = 0;
4339 
4340 	/* Warn if PFC is being disabled with RoCE v2 in use */
4341 	if (new_mode == 0 && sc->rdma_entry.attached)
4342 		device_printf(dev,
4343 		    "WARNING: Recommended that Priority Flow Control is enabled when RoCEv2 is in use\n");
4344 
4345 	status = ice_set_dcb_cfg(pi);
4346 	if (status) {
4347 		device_printf(dev,
4348 		    "%s: Failed to set DCB config; status %s, aq_err %s\n",
4349 		    __func__, ice_status_str(status),
4350 		    ice_aq_str(hw->adminq.sq_last_status));
4351 		return (EIO);
4352 	}
4353 
4354 	ice_do_dcb_reconfig(sc, false);
4355 
4356 	return (0);
4357 }
4358 
4359 #define ICE_SYSCTL_HELP_PFC_CONFIG \
4360 "\nIn FW DCB mode (fw_lldp_agent=1), displays the current Priority Flow Control configuration" \
4361 "\nIn SW DCB mode, displays and allows setting the configuration" \
4362 "\nInput/Output is in this format: 0xff" \
4363 "\nWhere bit position # enables/disables PFC for that Traffic Class #"
4364 
4365 /**
4366  * ice_sysctl_pfc_config - Report or configure enabled PFC TCs
4367  * @oidp: sysctl oid structure
4368  * @arg1: pointer to private data structure
4369  * @arg2: unused
4370  * @req: sysctl request pointer
4371  *
4372  * In FW DCB mode, returns a bitmap containing the current TCs
4373  * that have PFC enabled on them.
4374  *
4375  * In SW DCB mode this sysctl also accepts a value that will
4376  * be sent to the firmware for configuration.
4377  */
4378 static int
ice_sysctl_pfc_config(SYSCTL_HANDLER_ARGS)4379 ice_sysctl_pfc_config(SYSCTL_HANDLER_ARGS)
4380 {
4381 	struct ice_softc *sc = (struct ice_softc *)arg1;
4382 	struct ice_dcbx_cfg *local_dcbx_cfg;
4383 	struct ice_port_info *pi;
4384 	struct ice_hw *hw = &sc->hw;
4385 	int ret;
4386 
4387 	/* Store input flags from user */
4388 	u8 user_pfc;
4389 
4390 	UNREFERENCED_PARAMETER(arg2);
4391 
4392 	if (ice_driver_is_detaching(sc))
4393 		return (ESHUTDOWN);
4394 
4395 	if (req->oldptr == NULL && req->newptr == NULL) {
4396 		ret = SYSCTL_OUT(req, 0, sizeof(u8));
4397 		return (ret);
4398 	}
4399 
4400 	pi = hw->port_info;
4401 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
4402 
4403 	/* Format current PFC enable setting for output */
4404 	user_pfc = local_dcbx_cfg->pfc.pfcena;
4405 
4406 	/* Read in the new PFC config */
4407 	ret = sysctl_handle_8(oidp, &user_pfc, 0, req);
4408 	if ((ret) || (req->newptr == NULL))
4409 		return (ret);
4410 
4411 	/* Don't allow setting changes in FW DCB mode */
4412 	if (!hw->port_info->qos_cfg.is_sw_lldp)
4413 		return (EPERM);
4414 
4415 	/* If LFC is active and PFC is going to be turned on, turn LFC off */
4416 	if (user_pfc != 0 && pi->phy.curr_user_fc_req != ICE_FC_NONE) {
4417 		pi->phy.curr_user_fc_req = ICE_FC_NONE;
4418 		if (ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) ||
4419 			 sc->link_up) {
4420 			ret = ice_apply_saved_phy_cfg(sc, ICE_APPLY_FC);
4421 			if (ret)
4422 				return (ret);
4423 		}
4424 	}
4425 
4426 	return ice_config_pfc(sc, user_pfc);
4427 }
4428 
4429 #define ICE_SYSCTL_HELP_PFC_MODE \
4430 "\nDisplay and set the current QoS mode for the firmware" \
4431 "\n\t0: VLAN UP mode" \
4432 "\n\t1: DSCP mode"
4433 
4434 /**
4435  * ice_sysctl_pfc_mode
4436  * @oidp: sysctl oid structure
4437  * @arg1: pointer to private data structure
4438  * @arg2: unused
4439  * @req: sysctl request pointer
4440  *
4441  * Gets and sets whether the port is in DSCP or VLAN PCP-based
4442  * PFC mode. This is also used to set whether DSCP or VLAN PCP
4443  * -based settings are configured for DCB.
4444  */
4445 static int
ice_sysctl_pfc_mode(SYSCTL_HANDLER_ARGS)4446 ice_sysctl_pfc_mode(SYSCTL_HANDLER_ARGS)
4447 {
4448 	struct ice_softc *sc = (struct ice_softc *)arg1;
4449 	struct ice_dcbx_cfg *local_dcbx_cfg;
4450 	struct ice_port_info *pi;
4451 	struct ice_hw *hw = &sc->hw;
4452 	device_t dev = sc->dev;
4453 	int status;
4454 	u8 user_pfc_mode, aq_pfc_mode;
4455 	int ret;
4456 
4457 	UNREFERENCED_PARAMETER(arg2);
4458 
4459 	if (ice_driver_is_detaching(sc))
4460 		return (ESHUTDOWN);
4461 
4462 	if (req->oldptr == NULL && req->newptr == NULL) {
4463 		ret = SYSCTL_OUT(req, 0, sizeof(u8));
4464 		return (ret);
4465 	}
4466 
4467 	pi = hw->port_info;
4468 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
4469 
4470 	user_pfc_mode = local_dcbx_cfg->pfc_mode;
4471 
4472 	/* Read in the new mode */
4473 	ret = sysctl_handle_8(oidp, &user_pfc_mode, 0, req);
4474 	if ((ret) || (req->newptr == NULL))
4475 		return (ret);
4476 
4477 	/* Don't allow setting changes in FW DCB mode */
4478 	if (!hw->port_info->qos_cfg.is_sw_lldp)
4479 		return (EPERM);
4480 
4481 	/* Currently, there are only two modes */
4482 	switch (user_pfc_mode) {
4483 	case 0:
4484 		aq_pfc_mode = ICE_AQC_PFC_VLAN_BASED_PFC;
4485 		break;
4486 	case 1:
4487 		aq_pfc_mode = ICE_AQC_PFC_DSCP_BASED_PFC;
4488 		break;
4489 	default:
4490 		device_printf(dev,
4491 		    "%s: Valid input range is 0-1 (input %d)\n",
4492 		    __func__, user_pfc_mode);
4493 		return (EINVAL);
4494 	}
4495 
4496 	status = ice_aq_set_pfc_mode(hw, aq_pfc_mode, NULL);
4497 	if (status == ICE_ERR_NOT_SUPPORTED) {
4498 		device_printf(dev,
4499 		    "%s: Failed to set PFC mode; DCB not supported\n",
4500 		    __func__);
4501 		return (ENODEV);
4502 	}
4503 	if (status) {
4504 		device_printf(dev,
4505 		    "%s: Failed to set PFC mode; status %s, aq_err %s\n",
4506 		    __func__, ice_status_str(status),
4507 		    ice_aq_str(hw->adminq.sq_last_status));
4508 		return (EIO);
4509 	}
4510 
4511 	/* Reset settings to default when mode is changed */
4512 	ice_set_default_local_mib_settings(sc);
4513 	/* Cache current settings and reconfigure */
4514 	local_dcbx_cfg->pfc_mode = user_pfc_mode;
4515 	ice_do_dcb_reconfig(sc, false);
4516 
4517 	return (0);
4518 }
4519 
4520 #define ICE_SYSCTL_HELP_SET_LINK_ACTIVE \
4521 "\nKeep link active after setting interface down:" \
4522 "\n\t0 - disable" \
4523 "\n\t1 - enable"
4524 
4525 /**
4526  * ice_sysctl_set_link_active
4527  * @oidp: sysctl oid structure
4528  * @arg1: pointer to private data structure
4529  * @arg2: unused
4530  * @req: sysctl request pointer
4531  *
4532  * Set the link_active_on_if_down sysctl flag.
4533  */
4534 static int
ice_sysctl_set_link_active(SYSCTL_HANDLER_ARGS)4535 ice_sysctl_set_link_active(SYSCTL_HANDLER_ARGS)
4536 {
4537 	struct ice_softc *sc = (struct ice_softc *)arg1;
4538 	bool mode;
4539 	int ret;
4540 
4541 	UNREFERENCED_PARAMETER(arg2);
4542 
4543 	mode = ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN);
4544 
4545 	ret = sysctl_handle_bool(oidp, &mode, 0, req);
4546 	if ((ret) || (req->newptr == NULL))
4547 		return (ret);
4548 
4549 	if (mode)
4550 		ice_set_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN);
4551 	else
4552 		ice_clear_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN);
4553 
4554 	return (0);
4555 }
4556 
4557 /**
4558  * ice_sysctl_debug_set_link
4559  * @oidp: sysctl oid structure
4560  * @arg1: pointer to private data structure
4561  * @arg2: unused
4562  * @req: sysctl request pointer
4563  *
4564  * Set link up/down in debug session.
4565  */
4566 static int
ice_sysctl_debug_set_link(SYSCTL_HANDLER_ARGS)4567 ice_sysctl_debug_set_link(SYSCTL_HANDLER_ARGS)
4568 {
4569 	struct ice_softc *sc = (struct ice_softc *)arg1;
4570 	bool mode;
4571 	int ret;
4572 
4573 	UNREFERENCED_PARAMETER(arg2);
4574 
4575 	ret = sysctl_handle_bool(oidp, &mode, 0, req);
4576 	if ((ret) || (req->newptr == NULL))
4577 		return (ret);
4578 
4579 	ice_set_link(sc, mode != 0);
4580 
4581 	return (0);
4582 }
4583 
4584 /**
4585  * ice_add_device_sysctls - add device specific dynamic sysctls
4586  * @sc: device private structure
4587  *
4588  * Add per-device dynamic sysctls which show device configuration or enable
4589  * configuring device functionality. For tunable values which can be set prior
4590  * to load, see ice_add_device_tunables.
4591  *
4592  * This function depends on the sysctl layout setup by ice_add_device_tunables,
4593  * and likely should be called near the end of the attach process.
4594  */
4595 void
ice_add_device_sysctls(struct ice_softc * sc)4596 ice_add_device_sysctls(struct ice_softc *sc)
4597 {
4598 	struct sysctl_oid *hw_node;
4599 	device_t dev = sc->dev;
4600 
4601 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
4602 	struct sysctl_oid_list *ctx_list =
4603 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
4604 
4605 	SYSCTL_ADD_PROC(ctx, ctx_list,
4606 	    OID_AUTO, "fw_version", CTLTYPE_STRING | CTLFLAG_RD,
4607 	    sc, 0, ice_sysctl_show_fw, "A", "Firmware version");
4608 
4609 	if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_HAS_PBA)) {
4610 		SYSCTL_ADD_PROC(ctx, ctx_list,
4611 		    OID_AUTO, "pba_number", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
4612 		    ice_sysctl_pba_number, "A", "Product Board Assembly Number");
4613 	}
4614 	if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_TEMP_SENSOR)) {
4615 		SYSCTL_ADD_PROC(ctx, ctx_list,
4616 		    OID_AUTO, "temp", CTLTYPE_S8 | CTLFLAG_RD,
4617 		    sc, 0, ice_sysctl_temperature, "CU",
4618 		    "Device temperature in degrees Celcius (C)");
4619 	}
4620 
4621 	SYSCTL_ADD_PROC(ctx, ctx_list,
4622 	    OID_AUTO, "ddp_version", CTLTYPE_STRING | CTLFLAG_RD,
4623 	    sc, 0, ice_sysctl_pkg_version, "A", "Active DDP package name and version");
4624 
4625 	SYSCTL_ADD_PROC(ctx, ctx_list,
4626 	    OID_AUTO, "current_speed", CTLTYPE_STRING | CTLFLAG_RD,
4627 	    sc, 0, ice_sysctl_current_speed, "A", "Current Port Link Speed");
4628 
4629 	SYSCTL_ADD_PROC(ctx, ctx_list,
4630 	    OID_AUTO, "requested_fec", CTLTYPE_STRING | CTLFLAG_RW,
4631 	    sc, 0, ice_sysctl_fec_config, "A", ICE_SYSCTL_HELP_FEC_CONFIG);
4632 
4633 	SYSCTL_ADD_PROC(ctx, ctx_list,
4634 	    OID_AUTO, "negotiated_fec", CTLTYPE_STRING | CTLFLAG_RD,
4635 	    sc, 0, ice_sysctl_negotiated_fec, "A", "Current Negotiated FEC mode");
4636 
4637 	SYSCTL_ADD_PROC(ctx, ctx_list,
4638 	    OID_AUTO, "fc", CTLTYPE_STRING | CTLFLAG_RW,
4639 	    sc, 0, ice_sysctl_fc_config, "A", ICE_SYSCTL_HELP_FC_CONFIG);
4640 
4641 	SYSCTL_ADD_PROC(ctx, ctx_list,
4642 	    OID_AUTO, "advertise_speed", CTLTYPE_U16 | CTLFLAG_RW,
4643 	    sc, 0, ice_sysctl_advertise_speed, "SU", ICE_SYSCTL_HELP_ADVERTISE_SPEED);
4644 
4645 	SYSCTL_ADD_PROC(ctx, ctx_list,
4646 	    OID_AUTO, "fw_lldp_agent", CTLTYPE_U8 | CTLFLAG_RWTUN,
4647 	    sc, 0, ice_sysctl_fw_lldp_agent, "CU", ICE_SYSCTL_HELP_FW_LLDP_AGENT);
4648 
4649 	SYSCTL_ADD_PROC(ctx, ctx_list,
4650 	    OID_AUTO, "ets_min_rate", CTLTYPE_STRING | CTLFLAG_RW,
4651 	    sc, 0, ice_sysctl_ets_min_rate, "A", ICE_SYSCTL_HELP_ETS_MIN_RATE);
4652 
4653 	SYSCTL_ADD_PROC(ctx, ctx_list,
4654 	    OID_AUTO, "up2tc_map", CTLTYPE_STRING | CTLFLAG_RW,
4655 	    sc, 0, ice_sysctl_up2tc_map, "A", ICE_SYSCTL_HELP_UP2TC_MAP);
4656 
4657 	SYSCTL_ADD_PROC(ctx, ctx_list,
4658 	    OID_AUTO, "pfc", CTLTYPE_U8 | CTLFLAG_RW,
4659 	    sc, 0, ice_sysctl_pfc_config, "CU", ICE_SYSCTL_HELP_PFC_CONFIG);
4660 
4661 	SYSCTL_ADD_PROC(ctx, ctx_list,
4662 	    OID_AUTO, "pfc_mode", CTLTYPE_U8 | CTLFLAG_RWTUN,
4663 	    sc, 0, ice_sysctl_pfc_mode, "CU", ICE_SYSCTL_HELP_PFC_MODE);
4664 
4665 	SYSCTL_ADD_PROC(ctx, ctx_list,
4666 	    OID_AUTO, "allow_no_fec_modules_in_auto",
4667 	    CTLTYPE_U8 | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
4668 	    sc, 0, ice_sysctl_allow_no_fec_mod_in_auto, "CU",
4669 	    "Allow \"No FEC\" mode in FEC auto-negotiation");
4670 
4671 	SYSCTL_ADD_PROC(ctx, ctx_list,
4672 	    OID_AUTO, "link_active_on_if_down", CTLTYPE_U8 | CTLFLAG_RWTUN,
4673 	    sc, 0, ice_sysctl_set_link_active, "CU", ICE_SYSCTL_HELP_SET_LINK_ACTIVE);
4674 
4675 	SYSCTL_ADD_PROC(ctx, ctx_list,
4676 	    OID_AUTO, "create_mirror_interface", CTLTYPE_STRING | CTLFLAG_RW,
4677 	    sc, 0, ice_sysctl_create_mirror_interface, "A", "");
4678 
4679 	SYSCTL_ADD_PROC(ctx, ctx_list,
4680 	    OID_AUTO, "destroy_mirror_interface", CTLTYPE_STRING | CTLFLAG_RW,
4681 	    sc, 0, ice_sysctl_destroy_mirror_interface, "A", "");
4682 
4683 	ice_add_dscp2tc_map_sysctls(sc, ctx, ctx_list);
4684 
4685 	/* Differentiate software and hardware statistics, by keeping hw stats
4686 	 * in their own node. This isn't in ice_add_device_tunables, because
4687 	 * we won't have any CTLFLAG_TUN sysctls under this node.
4688 	 */
4689 	hw_node = SYSCTL_ADD_NODE(ctx, ctx_list, OID_AUTO, "hw", CTLFLAG_RD,
4690 				  NULL, "Port Hardware Statistics");
4691 
4692 	ice_add_sysctls_mac_stats(ctx, hw_node, sc);
4693 
4694 	/* Add the main PF VSI stats now. Other VSIs will add their own stats
4695 	 * during creation
4696 	 */
4697 	ice_add_vsi_sysctls(&sc->pf_vsi);
4698 
4699 	/* Add sysctls related to debugging the device driver. This includes
4700 	 * sysctls which display additional internal driver state for use in
4701 	 * understanding what is happening within the driver.
4702 	 */
4703 	ice_add_debug_sysctls(sc);
4704 }
4705 
4706 /**
4707  * @enum hmc_error_type
4708  * @brief enumeration of HMC errors
4709  *
4710  * Enumeration defining the possible HMC errors that might occur.
4711  */
4712 enum hmc_error_type {
4713 	HMC_ERR_PMF_INVALID = 0,
4714 	HMC_ERR_VF_IDX_INVALID = 1,
4715 	HMC_ERR_VF_PARENT_PF_INVALID = 2,
4716 	/* 3 is reserved */
4717 	HMC_ERR_INDEX_TOO_BIG = 4,
4718 	HMC_ERR_ADDRESS_TOO_LARGE = 5,
4719 	HMC_ERR_SEGMENT_DESC_INVALID = 6,
4720 	HMC_ERR_SEGMENT_DESC_TOO_SMALL = 7,
4721 	HMC_ERR_PAGE_DESC_INVALID = 8,
4722 	HMC_ERR_UNSUPPORTED_REQUEST_COMPLETION = 9,
4723 	/* 10 is reserved */
4724 	HMC_ERR_INVALID_OBJECT_TYPE = 11,
4725 	/* 12 is reserved */
4726 };
4727 
4728 /**
4729  * ice_log_hmc_error - Log an HMC error message
4730  * @hw: device hw structure
4731  * @dev: the device to pass to device_printf()
4732  *
4733  * Log a message when an HMC error interrupt is triggered.
4734  */
4735 void
ice_log_hmc_error(struct ice_hw * hw,device_t dev)4736 ice_log_hmc_error(struct ice_hw *hw, device_t dev)
4737 {
4738 	u32 info, data;
4739 	u8 index, errtype, objtype;
4740 	bool isvf;
4741 
4742 	info = rd32(hw, PFHMC_ERRORINFO);
4743 	data = rd32(hw, PFHMC_ERRORDATA);
4744 
4745 	index = (u8)(info & PFHMC_ERRORINFO_PMF_INDEX_M);
4746 	errtype = (u8)((info & PFHMC_ERRORINFO_HMC_ERROR_TYPE_M) >>
4747 		       PFHMC_ERRORINFO_HMC_ERROR_TYPE_S);
4748 	objtype = (u8)((info & PFHMC_ERRORINFO_HMC_OBJECT_TYPE_M) >>
4749 		       PFHMC_ERRORINFO_HMC_OBJECT_TYPE_S);
4750 
4751 	isvf = info & PFHMC_ERRORINFO_PMF_ISVF_M;
4752 
4753 	device_printf(dev, "%s HMC Error detected on PMF index %d:\n",
4754 		      isvf ? "VF" : "PF", index);
4755 
4756 	device_printf(dev, "error type %d, object type %d, data 0x%08x\n",
4757 		      errtype, objtype, data);
4758 
4759 	switch (errtype) {
4760 	case HMC_ERR_PMF_INVALID:
4761 		device_printf(dev, "Private Memory Function is not valid\n");
4762 		break;
4763 	case HMC_ERR_VF_IDX_INVALID:
4764 		device_printf(dev, "Invalid Private Memory Function index for PE enabled VF\n");
4765 		break;
4766 	case HMC_ERR_VF_PARENT_PF_INVALID:
4767 		device_printf(dev, "Invalid parent PF for PE enabled VF\n");
4768 		break;
4769 	case HMC_ERR_INDEX_TOO_BIG:
4770 		device_printf(dev, "Object index too big\n");
4771 		break;
4772 	case HMC_ERR_ADDRESS_TOO_LARGE:
4773 		device_printf(dev, "Address extends beyond segment descriptor limit\n");
4774 		break;
4775 	case HMC_ERR_SEGMENT_DESC_INVALID:
4776 		device_printf(dev, "Segment descriptor is invalid\n");
4777 		break;
4778 	case HMC_ERR_SEGMENT_DESC_TOO_SMALL:
4779 		device_printf(dev, "Segment descriptor is too small\n");
4780 		break;
4781 	case HMC_ERR_PAGE_DESC_INVALID:
4782 		device_printf(dev, "Page descriptor is invalid\n");
4783 		break;
4784 	case HMC_ERR_UNSUPPORTED_REQUEST_COMPLETION:
4785 		device_printf(dev, "Unsupported Request completion received from PCIe\n");
4786 		break;
4787 	case HMC_ERR_INVALID_OBJECT_TYPE:
4788 		device_printf(dev, "Invalid object type\n");
4789 		break;
4790 	default:
4791 		device_printf(dev, "Unknown HMC error\n");
4792 	}
4793 
4794 	/* Clear the error indication */
4795 	wr32(hw, PFHMC_ERRORINFO, 0);
4796 }
4797 
4798 /**
4799  * @struct ice_sysctl_info
4800  * @brief sysctl information
4801  *
4802  * Structure used to simplify the process of defining the many similar
4803  * statistics sysctls.
4804  */
4805 struct ice_sysctl_info {
4806 	u64		*stat;
4807 	const char	*name;
4808 	const char	*description;
4809 };
4810 
4811 /**
4812  * ice_add_sysctls_eth_stats - Add sysctls for ethernet statistics
4813  * @ctx: sysctl ctx to use
4814  * @parent: the parent node to add sysctls under
4815  * @stats: the ethernet stats structure to source values from
4816  *
4817  * Adds statistics sysctls for the ethernet statistics of the MAC or a VSI.
4818  * Will add them under the parent node specified.
4819  *
4820  * Note that tx_errors is only meaningful for VSIs and not the global MAC/PF
4821  * statistics, so it is not included here. Similarly, rx_discards has different
4822  * descriptions for VSIs and MAC/PF stats, so it is also not included here.
4823  */
4824 void
ice_add_sysctls_eth_stats(struct sysctl_ctx_list * ctx,struct sysctl_oid * parent,struct ice_eth_stats * stats)4825 ice_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx,
4826 			  struct sysctl_oid *parent,
4827 			  struct ice_eth_stats *stats)
4828 {
4829 	const struct ice_sysctl_info ctls[] = {
4830 		/* Rx Stats */
4831 		{ &stats->rx_bytes, "good_octets_rcvd", "Good Octets Received" },
4832 		{ &stats->rx_unicast, "ucast_pkts_rcvd", "Unicast Packets Received" },
4833 		{ &stats->rx_multicast, "mcast_pkts_rcvd", "Multicast Packets Received" },
4834 		{ &stats->rx_broadcast, "bcast_pkts_rcvd", "Broadcast Packets Received" },
4835 		/* Tx Stats */
4836 		{ &stats->tx_bytes, "good_octets_txd", "Good Octets Transmitted" },
4837 		{ &stats->tx_unicast, "ucast_pkts_txd", "Unicast Packets Transmitted" },
4838 		{ &stats->tx_multicast, "mcast_pkts_txd", "Multicast Packets Transmitted" },
4839 		{ &stats->tx_broadcast, "bcast_pkts_txd", "Broadcast Packets Transmitted" },
4840 		/* End */
4841 		{ 0, 0, 0 }
4842 	};
4843 
4844 	struct sysctl_oid_list *parent_list = SYSCTL_CHILDREN(parent);
4845 
4846 	const struct ice_sysctl_info *entry = ctls;
4847 	while (entry->stat != 0) {
4848 		SYSCTL_ADD_U64(ctx, parent_list, OID_AUTO, entry->name,
4849 			       CTLFLAG_RD | CTLFLAG_STATS, entry->stat, 0,
4850 			       entry->description);
4851 		entry++;
4852 	}
4853 }
4854 
4855 /**
4856  * ice_sysctl_tx_cso_stat - Display Tx checksum offload statistic
4857  * @oidp: sysctl oid structure
4858  * @arg1: pointer to private data structure
4859  * @arg2: Tx CSO stat to read
4860  * @req: sysctl request pointer
4861  *
4862  * On read: Sums the per-queue Tx CSO stat and displays it.
4863  */
4864 static int
ice_sysctl_tx_cso_stat(SYSCTL_HANDLER_ARGS)4865 ice_sysctl_tx_cso_stat(SYSCTL_HANDLER_ARGS)
4866 {
4867 	struct ice_vsi *vsi = (struct ice_vsi *)arg1;
4868 	enum ice_tx_cso_stat type = (enum ice_tx_cso_stat)arg2;
4869 	u64 stat = 0;
4870 	int i;
4871 
4872 	if (ice_driver_is_detaching(vsi->sc))
4873 		return (ESHUTDOWN);
4874 
4875 	/* Check that the type is valid */
4876 	if (type >= ICE_CSO_STAT_TX_COUNT)
4877 		return (EDOOFUS);
4878 
4879 	/* Sum the stat for each of the Tx queues */
4880 	for (i = 0; i < vsi->num_tx_queues; i++)
4881 		stat += vsi->tx_queues[i].stats.cso[type];
4882 
4883 	return sysctl_handle_64(oidp, NULL, stat, req);
4884 }
4885 
4886 /**
4887  * ice_sysctl_rx_cso_stat - Display Rx checksum offload statistic
4888  * @oidp: sysctl oid structure
4889  * @arg1: pointer to private data structure
4890  * @arg2: Rx CSO stat to read
4891  * @req: sysctl request pointer
4892  *
4893  * On read: Sums the per-queue Rx CSO stat and displays it.
4894  */
4895 static int
ice_sysctl_rx_cso_stat(SYSCTL_HANDLER_ARGS)4896 ice_sysctl_rx_cso_stat(SYSCTL_HANDLER_ARGS)
4897 {
4898 	struct ice_vsi *vsi = (struct ice_vsi *)arg1;
4899 	enum ice_rx_cso_stat type = (enum ice_rx_cso_stat)arg2;
4900 	u64 stat = 0;
4901 	int i;
4902 
4903 	if (ice_driver_is_detaching(vsi->sc))
4904 		return (ESHUTDOWN);
4905 
4906 	/* Check that the type is valid */
4907 	if (type >= ICE_CSO_STAT_RX_COUNT)
4908 		return (EDOOFUS);
4909 
4910 	/* Sum the stat for each of the Rx queues */
4911 	for (i = 0; i < vsi->num_rx_queues; i++)
4912 		stat += vsi->rx_queues[i].stats.cso[type];
4913 
4914 	return sysctl_handle_64(oidp, NULL, stat, req);
4915 }
4916 
4917 /**
4918  * ice_sysctl_rx_errors_stat - Display aggregate of Rx errors
4919  * @oidp: sysctl oid structure
4920  * @arg1: pointer to private data structure
4921  * @arg2: unused
4922  * @req: sysctl request pointer
4923  *
4924  * On read: Sums current values of Rx error statistics and
4925  * displays it.
4926  */
4927 static int
ice_sysctl_rx_errors_stat(SYSCTL_HANDLER_ARGS)4928 ice_sysctl_rx_errors_stat(SYSCTL_HANDLER_ARGS)
4929 {
4930 	struct ice_vsi *vsi = (struct ice_vsi *)arg1;
4931 	struct ice_hw_port_stats *hs = &vsi->sc->stats.cur;
4932 	u64 stat = 0;
4933 	int i, type;
4934 
4935 	UNREFERENCED_PARAMETER(arg2);
4936 
4937 	if (ice_driver_is_detaching(vsi->sc))
4938 		return (ESHUTDOWN);
4939 
4940 	stat += hs->rx_undersize;
4941 	stat += hs->rx_fragments;
4942 	stat += hs->rx_oversize;
4943 	stat += hs->rx_jabber;
4944 	stat += hs->crc_errors;
4945 	stat += hs->illegal_bytes;
4946 
4947 	/* Checksum error stats */
4948 	for (i = 0; i < vsi->num_rx_queues; i++)
4949 		for (type = ICE_CSO_STAT_RX_IP4_ERR;
4950 		     type < ICE_CSO_STAT_RX_COUNT;
4951 		     type++)
4952 			stat += vsi->rx_queues[i].stats.cso[type];
4953 
4954 	return sysctl_handle_64(oidp, NULL, stat, req);
4955 }
4956 
4957 /**
4958  * @struct ice_rx_cso_stat_info
4959  * @brief sysctl information for an Rx checksum offload statistic
4960  *
4961  * Structure used to simplify the process of defining the checksum offload
4962  * statistics.
4963  */
4964 struct ice_rx_cso_stat_info {
4965 	enum ice_rx_cso_stat	type;
4966 	const char		*name;
4967 	const char		*description;
4968 };
4969 
4970 /**
4971  * @struct ice_tx_cso_stat_info
4972  * @brief sysctl information for a Tx checksum offload statistic
4973  *
4974  * Structure used to simplify the process of defining the checksum offload
4975  * statistics.
4976  */
4977 struct ice_tx_cso_stat_info {
4978 	enum ice_tx_cso_stat	type;
4979 	const char		*name;
4980 	const char		*description;
4981 };
4982 
4983 /**
4984  * ice_add_sysctls_sw_stats - Add sysctls for software statistics
4985  * @vsi: pointer to the VSI to add sysctls for
4986  * @ctx: sysctl ctx to use
4987  * @parent: the parent node to add sysctls under
4988  *
4989  * Add statistics sysctls for software tracked statistics of a VSI.
4990  *
4991  * Currently this only adds checksum offload statistics, but more counters may
4992  * be added in the future.
4993  */
4994 static void
ice_add_sysctls_sw_stats(struct ice_vsi * vsi,struct sysctl_ctx_list * ctx,struct sysctl_oid * parent)4995 ice_add_sysctls_sw_stats(struct ice_vsi *vsi,
4996 			 struct sysctl_ctx_list *ctx,
4997 			 struct sysctl_oid *parent)
4998 {
4999 	struct sysctl_oid *cso_node;
5000 	struct sysctl_oid_list *cso_list;
5001 
5002 	/* Tx CSO Stats */
5003 	const struct ice_tx_cso_stat_info tx_ctls[] = {
5004 		{ ICE_CSO_STAT_TX_TCP, "tx_tcp", "Transmit TCP Packets marked for HW checksum" },
5005 		{ ICE_CSO_STAT_TX_UDP, "tx_udp", "Transmit UDP Packets marked for HW checksum" },
5006 		{ ICE_CSO_STAT_TX_SCTP, "tx_sctp", "Transmit SCTP Packets marked for HW checksum" },
5007 		{ ICE_CSO_STAT_TX_IP4, "tx_ip4", "Transmit IPv4 Packets marked for HW checksum" },
5008 		{ ICE_CSO_STAT_TX_IP6, "tx_ip6", "Transmit IPv6 Packets marked for HW checksum" },
5009 		{ ICE_CSO_STAT_TX_L3_ERR, "tx_l3_err", "Transmit packets that driver failed to set L3 HW CSO bits for" },
5010 		{ ICE_CSO_STAT_TX_L4_ERR, "tx_l4_err", "Transmit packets that driver failed to set L4 HW CSO bits for" },
5011 		/* End */
5012 		{ ICE_CSO_STAT_TX_COUNT, 0, 0 }
5013 	};
5014 
5015 	/* Rx CSO Stats */
5016 	const struct ice_rx_cso_stat_info rx_ctls[] = {
5017 		{ ICE_CSO_STAT_RX_IP4_ERR, "rx_ip4_err", "Received packets with invalid IPv4 checksum indicated by HW" },
5018 		{ ICE_CSO_STAT_RX_IP6_ERR, "rx_ip6_err", "Received IPv6 packets with extension headers" },
5019 		{ ICE_CSO_STAT_RX_L3_ERR, "rx_l3_err", "Received packets with an unexpected invalid L3 checksum indicated by HW" },
5020 		{ ICE_CSO_STAT_RX_TCP_ERR, "rx_tcp_err", "Received packets with invalid TCP checksum indicated by HW" },
5021 		{ ICE_CSO_STAT_RX_UDP_ERR, "rx_udp_err", "Received packets with invalid UDP checksum indicated by HW" },
5022 		{ ICE_CSO_STAT_RX_SCTP_ERR, "rx_sctp_err", "Received packets with invalid SCTP checksum indicated by HW" },
5023 		{ ICE_CSO_STAT_RX_L4_ERR, "rx_l4_err", "Received packets with an unexpected invalid L4 checksum indicated by HW" },
5024 		/* End */
5025 		{ ICE_CSO_STAT_RX_COUNT, 0, 0 }
5026 	};
5027 
5028 	struct sysctl_oid_list *parent_list = SYSCTL_CHILDREN(parent);
5029 
5030 	/* Add a node for statistics tracked by software. */
5031 	cso_node = SYSCTL_ADD_NODE(ctx, parent_list, OID_AUTO, "cso", CTLFLAG_RD,
5032 				  NULL, "Checksum offload Statistics");
5033 	cso_list = SYSCTL_CHILDREN(cso_node);
5034 
5035 	const struct ice_tx_cso_stat_info *tx_entry = tx_ctls;
5036 	while (tx_entry->name && tx_entry->description) {
5037 		SYSCTL_ADD_PROC(ctx, cso_list, OID_AUTO, tx_entry->name,
5038 				CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
5039 				vsi, tx_entry->type, ice_sysctl_tx_cso_stat, "QU",
5040 				tx_entry->description);
5041 		tx_entry++;
5042 	}
5043 
5044 	const struct ice_rx_cso_stat_info *rx_entry = rx_ctls;
5045 	while (rx_entry->name && rx_entry->description) {
5046 		SYSCTL_ADD_PROC(ctx, cso_list, OID_AUTO, rx_entry->name,
5047 				CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
5048 				vsi, rx_entry->type, ice_sysctl_rx_cso_stat, "QU",
5049 				rx_entry->description);
5050 		rx_entry++;
5051 	}
5052 }
5053 
5054 /**
5055  * ice_add_vsi_sysctls - Add sysctls for a VSI
5056  * @vsi: pointer to VSI structure
5057  *
5058  * Add various sysctls for a given VSI.
5059  */
5060 void
ice_add_vsi_sysctls(struct ice_vsi * vsi)5061 ice_add_vsi_sysctls(struct ice_vsi *vsi)
5062 {
5063 	struct sysctl_ctx_list *ctx = &vsi->ctx;
5064 	struct sysctl_oid *hw_node, *sw_node;
5065 	struct sysctl_oid_list *vsi_list, *hw_list;
5066 
5067 	vsi_list = SYSCTL_CHILDREN(vsi->vsi_node);
5068 
5069 	/* Keep hw stats in their own node. */
5070 	hw_node = SYSCTL_ADD_NODE(ctx, vsi_list, OID_AUTO, "hw", CTLFLAG_RD,
5071 				  NULL, "VSI Hardware Statistics");
5072 	hw_list = SYSCTL_CHILDREN(hw_node);
5073 
5074 	/* Add the ethernet statistics for this VSI */
5075 	ice_add_sysctls_eth_stats(ctx, hw_node, &vsi->hw_stats.cur);
5076 
5077 	SYSCTL_ADD_U64(ctx, hw_list, OID_AUTO, "rx_discards",
5078 			CTLFLAG_RD | CTLFLAG_STATS, &vsi->hw_stats.cur.rx_discards,
5079 			0, "Discarded Rx Packets (see rx_errors or rx_no_desc)");
5080 
5081 	SYSCTL_ADD_PROC(ctx, hw_list, OID_AUTO, "rx_errors",
5082 			CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
5083 			vsi, 0, ice_sysctl_rx_errors_stat, "QU",
5084 			"Aggregate of all Rx errors");
5085 
5086 	SYSCTL_ADD_U64(ctx, hw_list, OID_AUTO, "rx_no_desc",
5087 		       CTLFLAG_RD | CTLFLAG_STATS, &vsi->hw_stats.cur.rx_no_desc,
5088 		       0, "Rx Packets Discarded Due To Lack Of Descriptors");
5089 
5090 	SYSCTL_ADD_U64(ctx, hw_list, OID_AUTO, "tx_errors",
5091 			CTLFLAG_RD | CTLFLAG_STATS, &vsi->hw_stats.cur.tx_errors,
5092 			0, "Tx Packets Discarded Due To Error");
5093 
5094 	/* Add a node for statistics tracked by software. */
5095 	sw_node = SYSCTL_ADD_NODE(ctx, vsi_list, OID_AUTO, "sw", CTLFLAG_RD,
5096 				  NULL, "VSI Software Statistics");
5097 
5098 	ice_add_sysctls_sw_stats(vsi, ctx, sw_node);
5099 }
5100 
5101 /**
5102  * ice_add_sysctls_mac_pfc_one_stat - Add sysctl node for a PFC statistic
5103  * @ctx: sysctl ctx to use
5104  * @parent_list: parent sysctl list to add sysctls under
5105  * @pfc_stat_location: address of statistic for sysctl to display
5106  * @node_name: Name for statistic node
5107  * @descr: Description used for nodes added in this function
5108  *
5109  * A helper function for ice_add_sysctls_mac_pfc_stats that adds a node
5110  * for a stat and leaves for each traffic class for that stat.
5111  */
5112 static void
ice_add_sysctls_mac_pfc_one_stat(struct sysctl_ctx_list * ctx,struct sysctl_oid_list * parent_list,u64 * pfc_stat_location,const char * node_name,const char * descr)5113 ice_add_sysctls_mac_pfc_one_stat(struct sysctl_ctx_list *ctx,
5114 				 struct sysctl_oid_list *parent_list,
5115 				 u64* pfc_stat_location,
5116 				 const char *node_name,
5117 				 const char *descr)
5118 {
5119 	struct sysctl_oid_list *node_list;
5120 	struct sysctl_oid *node;
5121 	struct sbuf *namebuf, *descbuf;
5122 
5123 	node = SYSCTL_ADD_NODE(ctx, parent_list, OID_AUTO, node_name, CTLFLAG_RD,
5124 				   NULL, descr);
5125 	node_list = SYSCTL_CHILDREN(node);
5126 
5127 	namebuf = sbuf_new_auto();
5128 	descbuf = sbuf_new_auto();
5129 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
5130 		sbuf_clear(namebuf);
5131 		sbuf_clear(descbuf);
5132 
5133 		sbuf_printf(namebuf, "%d", i);
5134 		sbuf_printf(descbuf, "%s for TC %d", descr, i);
5135 
5136 		sbuf_finish(namebuf);
5137 		sbuf_finish(descbuf);
5138 
5139 		SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, sbuf_data(namebuf),
5140 			CTLFLAG_RD | CTLFLAG_STATS, &pfc_stat_location[i], 0,
5141 			sbuf_data(descbuf));
5142 	}
5143 
5144 	sbuf_delete(namebuf);
5145 	sbuf_delete(descbuf);
5146 }
5147 
5148 /**
5149  * ice_add_sysctls_mac_pfc_stats - Add sysctls for MAC PFC statistics
5150  * @ctx: the sysctl ctx to use
5151  * @parent: parent node to add the sysctls under
5152  * @stats: the hw ports stat structure to pull values from
5153  *
5154  * Add global Priority Flow Control MAC statistics sysctls. These are
5155  * structured as a node with the PFC statistic, where there are eight
5156  * nodes for each traffic class.
5157  */
5158 static void
ice_add_sysctls_mac_pfc_stats(struct sysctl_ctx_list * ctx,struct sysctl_oid * parent,struct ice_hw_port_stats * stats)5159 ice_add_sysctls_mac_pfc_stats(struct sysctl_ctx_list *ctx,
5160 			      struct sysctl_oid *parent,
5161 			      struct ice_hw_port_stats *stats)
5162 {
5163 	struct sysctl_oid_list *parent_list;
5164 
5165 	parent_list = SYSCTL_CHILDREN(parent);
5166 
5167 	ice_add_sysctls_mac_pfc_one_stat(ctx, parent_list, stats->priority_xon_rx,
5168 	    "p_xon_recvd", "PFC XON received");
5169 	ice_add_sysctls_mac_pfc_one_stat(ctx, parent_list, stats->priority_xoff_rx,
5170 	    "p_xoff_recvd", "PFC XOFF received");
5171 	ice_add_sysctls_mac_pfc_one_stat(ctx, parent_list, stats->priority_xon_tx,
5172 	    "p_xon_txd", "PFC XON transmitted");
5173 	ice_add_sysctls_mac_pfc_one_stat(ctx, parent_list, stats->priority_xoff_tx,
5174 	    "p_xoff_txd", "PFC XOFF transmitted");
5175 	ice_add_sysctls_mac_pfc_one_stat(ctx, parent_list, stats->priority_xon_2_xoff,
5176 	    "p_xon2xoff", "PFC XON to XOFF transitions");
5177 }
5178 
5179 /**
5180  * ice_add_sysctls_mac_stats - Add sysctls for global MAC statistics
5181  * @ctx: the sysctl ctx to use
5182  * @parent: parent node to add the sysctls under
5183  * @sc: device private structure
5184  *
5185  * Add global MAC statistics sysctls.
5186  */
5187 void
ice_add_sysctls_mac_stats(struct sysctl_ctx_list * ctx,struct sysctl_oid * parent,struct ice_softc * sc)5188 ice_add_sysctls_mac_stats(struct sysctl_ctx_list *ctx,
5189 			  struct sysctl_oid *parent,
5190 			  struct ice_softc *sc)
5191 {
5192 	struct sysctl_oid *mac_node;
5193 	struct sysctl_oid_list *parent_list, *mac_list;
5194 	struct ice_hw_port_stats *stats = &sc->stats.cur;
5195 
5196 	parent_list = SYSCTL_CHILDREN(parent);
5197 
5198 	mac_node = SYSCTL_ADD_NODE(ctx, parent_list, OID_AUTO, "mac", CTLFLAG_RD,
5199 				   NULL, "Mac Hardware Statistics");
5200 	mac_list = SYSCTL_CHILDREN(mac_node);
5201 
5202 	/* Add the ethernet statistics common to VSI and MAC */
5203 	ice_add_sysctls_eth_stats(ctx, mac_node, &stats->eth);
5204 
5205 	/* Add PFC stats that add per-TC counters */
5206 	ice_add_sysctls_mac_pfc_stats(ctx, mac_node, stats);
5207 
5208 	const struct ice_sysctl_info ctls[] = {
5209 		/* Packet Reception Stats */
5210 		{&stats->rx_size_64, "rx_frames_64", "64 byte frames received"},
5211 		{&stats->rx_size_127, "rx_frames_65_127", "65-127 byte frames received"},
5212 		{&stats->rx_size_255, "rx_frames_128_255", "128-255 byte frames received"},
5213 		{&stats->rx_size_511, "rx_frames_256_511", "256-511 byte frames received"},
5214 		{&stats->rx_size_1023, "rx_frames_512_1023", "512-1023 byte frames received"},
5215 		{&stats->rx_size_1522, "rx_frames_1024_1522", "1024-1522 byte frames received"},
5216 		{&stats->rx_size_big, "rx_frames_big", "1523-9522 byte frames received"},
5217 		{&stats->rx_undersize, "rx_undersize", "Undersized packets received"},
5218 		{&stats->rx_fragments, "rx_fragmented", "Fragmented packets received"},
5219 		{&stats->rx_jabber, "rx_jabber", "Received Jabber"},
5220 		{&stats->eth.rx_discards, "rx_discards",
5221 		    "Discarded Rx Packets by Port (shortage of storage space)"},
5222 		/* Packet Transmission Stats */
5223 		{&stats->tx_size_64, "tx_frames_64", "64 byte frames transmitted"},
5224 		{&stats->tx_size_127, "tx_frames_65_127", "65-127 byte frames transmitted"},
5225 		{&stats->tx_size_255, "tx_frames_128_255", "128-255 byte frames transmitted"},
5226 		{&stats->tx_size_511, "tx_frames_256_511", "256-511 byte frames transmitted"},
5227 		{&stats->tx_size_1023, "tx_frames_512_1023", "512-1023 byte frames transmitted"},
5228 		{&stats->tx_size_1522, "tx_frames_1024_1522", "1024-1522 byte frames transmitted"},
5229 		{&stats->tx_size_big, "tx_frames_big", "1523-9522 byte frames transmitted"},
5230 		{&stats->tx_dropped_link_down, "tx_dropped", "Tx Dropped Due To Link Down"},
5231 		/* Flow control */
5232 		{&stats->link_xon_tx, "xon_txd", "Link XON transmitted"},
5233 		{&stats->link_xon_rx, "xon_recvd", "Link XON received"},
5234 		{&stats->link_xoff_tx, "xoff_txd", "Link XOFF transmitted"},
5235 		{&stats->link_xoff_rx, "xoff_recvd", "Link XOFF received"},
5236 		/* Other */
5237 		{&stats->crc_errors, "crc_errors", "CRC Errors"},
5238 		{&stats->illegal_bytes, "illegal_bytes", "Illegal Byte Errors"},
5239 		{&stats->mac_local_faults, "local_faults", "MAC Local Faults"},
5240 		{&stats->mac_remote_faults, "remote_faults", "MAC Remote Faults"},
5241 		/* End */
5242 		{ 0, 0, 0 }
5243 	};
5244 
5245 	const struct ice_sysctl_info *entry = ctls;
5246 	while (entry->stat != 0) {
5247 		SYSCTL_ADD_U64(ctx, mac_list, OID_AUTO, entry->name,
5248 			CTLFLAG_RD | CTLFLAG_STATS, entry->stat, 0,
5249 			entry->description);
5250 		entry++;
5251 	}
5252 	/* Port oversize packet stats */
5253 	SYSCTL_ADD_U64(ctx, mac_list, OID_AUTO, "rx_oversized",
5254 		       CTLFLAG_RD | CTLFLAG_STATS, &sc->soft_stats.rx_roc_error,
5255 		       0, "Oversized packets received");
5256 
5257 }
5258 
5259 /**
5260  * ice_configure_misc_interrupts - enable 'other' interrupt causes
5261  * @sc: pointer to device private softc
5262  *
5263  * Enable various "other" interrupt causes, and associate them to interrupt 0,
5264  * which is our administrative interrupt.
5265  */
5266 void
ice_configure_misc_interrupts(struct ice_softc * sc)5267 ice_configure_misc_interrupts(struct ice_softc *sc)
5268 {
5269 	struct ice_hw *hw = &sc->hw;
5270 	u32 val;
5271 
5272 	/* Read the OICR register to clear it */
5273 	rd32(hw, PFINT_OICR);
5274 
5275 	/* Enable useful "other" interrupt causes */
5276 	val = (PFINT_OICR_ECC_ERR_M |
5277 	       PFINT_OICR_MAL_DETECT_M |
5278 	       PFINT_OICR_GRST_M |
5279 	       PFINT_OICR_PCI_EXCEPTION_M |
5280 	       PFINT_OICR_VFLR_M |
5281 	       PFINT_OICR_HMC_ERR_M |
5282 	       PFINT_OICR_PE_CRITERR_M);
5283 
5284 	wr32(hw, PFINT_OICR_ENA, val);
5285 
5286 	/* Note that since we're using MSI-X index 0, and ITR index 0, we do
5287 	 * not explicitly program them when writing to the PFINT_*_CTL
5288 	 * registers. Nevertheless, these writes are associating the
5289 	 * interrupts with the ITR 0 vector
5290 	 */
5291 
5292 	/* Associate the OICR interrupt with ITR 0, and enable it */
5293 	wr32(hw, PFINT_OICR_CTL, PFINT_OICR_CTL_CAUSE_ENA_M);
5294 
5295 	/* Associate the Mailbox interrupt with ITR 0, and enable it */
5296 	wr32(hw, PFINT_MBX_CTL, PFINT_MBX_CTL_CAUSE_ENA_M);
5297 
5298 	/* Associate the SB Queue interrupt with ITR 0, and enable it */
5299 	wr32(hw, PFINT_SB_CTL, PFINT_SB_CTL_CAUSE_ENA_M);
5300 
5301 	/* Associate the AdminQ interrupt with ITR 0, and enable it */
5302 	wr32(hw, PFINT_FW_CTL, PFINT_FW_CTL_CAUSE_ENA_M);
5303 }
5304 
5305 /**
5306  * ice_filter_is_mcast - Check if info is a multicast filter
5307  * @vsi: vsi structure addresses are targeted towards
5308  * @info: filter info
5309  *
5310  * @returns true if the provided info is a multicast filter, and false
5311  * otherwise.
5312  */
5313 static bool
ice_filter_is_mcast(struct ice_vsi * vsi,struct ice_fltr_info * info)5314 ice_filter_is_mcast(struct ice_vsi *vsi, struct ice_fltr_info *info)
5315 {
5316 	const u8 *addr = info->l_data.mac.mac_addr;
5317 
5318 	/*
5319 	 * Check if this info matches a multicast filter added by
5320 	 * ice_add_mac_to_list
5321 	 */
5322 	if ((info->flag == ICE_FLTR_TX) &&
5323 	    (info->src_id == ICE_SRC_ID_VSI) &&
5324 	    (info->lkup_type == ICE_SW_LKUP_MAC) &&
5325 	    (info->vsi_handle == vsi->idx) &&
5326 	    ETHER_IS_MULTICAST(addr) && !ETHER_IS_BROADCAST(addr))
5327 		return true;
5328 
5329 	return false;
5330 }
5331 
5332 /**
5333  * @struct ice_mcast_sync_data
5334  * @brief data used by ice_sync_one_mcast_filter function
5335  *
5336  * Structure used to store data needed for processing by the
5337  * ice_sync_one_mcast_filter. This structure contains a linked list of filters
5338  * to be added, an error indication, and a pointer to the device softc.
5339  */
5340 struct ice_mcast_sync_data {
5341 	struct ice_list_head add_list;
5342 	struct ice_softc *sc;
5343 	int err;
5344 };
5345 
5346 /**
5347  * ice_sync_one_mcast_filter - Check if we need to program the filter
5348  * @p: void pointer to algorithm data
5349  * @sdl: link level socket address
5350  * @count: unused count value
5351  *
5352  * Called by if_foreach_llmaddr to operate on each filter in the ifp filter
5353  * list. For the given address, search our internal list to see if we have
5354  * found the filter. If not, add it to our list of filters that need to be
5355  * programmed.
5356  *
5357  * @returns (1) if we've actually setup the filter to be added
5358  */
5359 static u_int
ice_sync_one_mcast_filter(void * p,struct sockaddr_dl * sdl,u_int __unused count)5360 ice_sync_one_mcast_filter(void *p, struct sockaddr_dl *sdl,
5361 			  u_int __unused count)
5362 {
5363 	struct ice_mcast_sync_data *data = (struct ice_mcast_sync_data *)p;
5364 	struct ice_softc *sc = data->sc;
5365 	struct ice_hw *hw = &sc->hw;
5366 	struct ice_switch_info *sw = hw->switch_info;
5367 	const u8 *sdl_addr = (const u8 *)LLADDR(sdl);
5368 	struct ice_fltr_mgmt_list_entry *itr;
5369 	struct ice_list_head *rules;
5370 	int err;
5371 
5372 	rules = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
5373 
5374 	/*
5375 	 * If a previous filter already indicated an error, there is no need
5376 	 * for us to finish processing the rest of the filters.
5377 	 */
5378 	if (data->err)
5379 		return (0);
5380 
5381 	/* See if this filter has already been programmed */
5382 	LIST_FOR_EACH_ENTRY(itr, rules, ice_fltr_mgmt_list_entry, list_entry) {
5383 		struct ice_fltr_info *info = &itr->fltr_info;
5384 		const u8 *addr = info->l_data.mac.mac_addr;
5385 
5386 		/* Only check multicast filters */
5387 		if (!ice_filter_is_mcast(&sc->pf_vsi, info))
5388 			continue;
5389 
5390 		/*
5391 		 * If this filter matches, mark the internal filter as
5392 		 * "found", and exit.
5393 		 */
5394 		if (bcmp(addr, sdl_addr, ETHER_ADDR_LEN) == 0) {
5395 			itr->marker = ICE_FLTR_FOUND;
5396 			return (1);
5397 		}
5398 	}
5399 
5400 	/*
5401 	 * If we failed to locate the filter in our internal list, we need to
5402 	 * place it into our add list.
5403 	 */
5404 	err = ice_add_mac_to_list(&sc->pf_vsi, &data->add_list, sdl_addr,
5405 				  ICE_FWD_TO_VSI);
5406 	if (err) {
5407 		device_printf(sc->dev,
5408 			      "Failed to place MAC %6D onto add list, err %s\n",
5409 			      sdl_addr, ":", ice_err_str(err));
5410 		data->err = err;
5411 
5412 		return (0);
5413 	}
5414 
5415 	return (1);
5416 }
5417 
5418 /**
5419  * ice_sync_multicast_filters - Synchronize OS and internal filter list
5420  * @sc: device private structure
5421  *
5422  * Called in response to SIOCDELMULTI to synchronize the operating system
5423  * multicast address list with the internal list of filters programmed to
5424  * firmware.
5425  *
5426  * Works in one phase to find added and deleted filters using a marker bit on
5427  * the internal list.
5428  *
5429  * First, a loop over the internal list clears the marker bit. Second, for
5430  * each filter in the ifp list is checked. If we find it in the internal list,
5431  * the marker bit is set. Otherwise, the filter is added to the add list.
5432  * Third, a loop over the internal list determines if any filters have not
5433  * been found. Each of these is added to the delete list. Finally, the add and
5434  * delete lists are programmed to firmware to update the filters.
5435  *
5436  * @returns zero on success or an integer error code on failure.
5437  */
5438 int
ice_sync_multicast_filters(struct ice_softc * sc)5439 ice_sync_multicast_filters(struct ice_softc *sc)
5440 {
5441 	struct ice_hw *hw = &sc->hw;
5442 	struct ice_switch_info *sw = hw->switch_info;
5443 	struct ice_fltr_mgmt_list_entry *itr;
5444 	struct ice_mcast_sync_data data = {};
5445 	struct ice_list_head *rules, remove_list;
5446 	int status;
5447 	int err = 0;
5448 
5449 	INIT_LIST_HEAD(&data.add_list);
5450 	INIT_LIST_HEAD(&remove_list);
5451 	data.sc = sc;
5452 	data.err = 0;
5453 
5454 	rules = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
5455 
5456 	/* Acquire the lock for the entire duration */
5457 	ice_acquire_lock(&sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock);
5458 
5459 	/* (1) Reset the marker state for all filters */
5460 	LIST_FOR_EACH_ENTRY(itr, rules, ice_fltr_mgmt_list_entry, list_entry)
5461 		itr->marker = ICE_FLTR_NOT_FOUND;
5462 
5463 	/* (2) determine which filters need to be added and removed */
5464 	if_foreach_llmaddr(sc->ifp, ice_sync_one_mcast_filter, (void *)&data);
5465 	if (data.err) {
5466 		/* ice_sync_one_mcast_filter already prints an error */
5467 		err = data.err;
5468 		ice_release_lock(&sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock);
5469 		goto free_filter_lists;
5470 	}
5471 
5472 	LIST_FOR_EACH_ENTRY(itr, rules, ice_fltr_mgmt_list_entry, list_entry) {
5473 		struct ice_fltr_info *info = &itr->fltr_info;
5474 		const u8 *addr = info->l_data.mac.mac_addr;
5475 
5476 		/* Only check multicast filters */
5477 		if (!ice_filter_is_mcast(&sc->pf_vsi, info))
5478 			continue;
5479 
5480 		/*
5481 		 * If the filter is not marked as found, then it must no
5482 		 * longer be in the ifp address list, so we need to remove it.
5483 		 */
5484 		if (itr->marker == ICE_FLTR_NOT_FOUND) {
5485 			err = ice_add_mac_to_list(&sc->pf_vsi, &remove_list,
5486 						  addr, ICE_FWD_TO_VSI);
5487 			if (err) {
5488 				device_printf(sc->dev,
5489 					      "Failed to place MAC %6D onto remove list, err %s\n",
5490 					      addr, ":", ice_err_str(err));
5491 				ice_release_lock(&sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock);
5492 				goto free_filter_lists;
5493 			}
5494 		}
5495 	}
5496 
5497 	ice_release_lock(&sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock);
5498 
5499 	status = ice_add_mac(hw, &data.add_list);
5500 	if (status) {
5501 		device_printf(sc->dev,
5502 			      "Could not add new MAC filters, err %s aq_err %s\n",
5503 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
5504 		err = (EIO);
5505 		goto free_filter_lists;
5506 	}
5507 
5508 	status = ice_remove_mac(hw, &remove_list);
5509 	if (status) {
5510 		device_printf(sc->dev,
5511 			      "Could not remove old MAC filters, err %s aq_err %s\n",
5512 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
5513 		err = (EIO);
5514 		goto free_filter_lists;
5515 	}
5516 
5517 free_filter_lists:
5518 	ice_free_fltr_list(&data.add_list);
5519 	ice_free_fltr_list(&remove_list);
5520 
5521 	return (err);
5522 }
5523 
5524 /**
5525  * ice_add_vlan_hw_filters - Add multiple VLAN filters for a given VSI
5526  * @vsi: The VSI to add the filter for
5527  * @vid: array of VLAN ids to add
5528  * @length: length of vid array
5529  *
5530  * Programs HW filters so that the given VSI will receive the specified VLANs.
5531  */
5532 int
ice_add_vlan_hw_filters(struct ice_vsi * vsi,u16 * vid,u16 length)5533 ice_add_vlan_hw_filters(struct ice_vsi *vsi, u16 *vid, u16 length)
5534 {
5535 	struct ice_hw *hw = &vsi->sc->hw;
5536 	struct ice_list_head vlan_list;
5537 	struct ice_fltr_list_entry *vlan_entries;
5538 	int status;
5539 
5540 	MPASS(length > 0);
5541 
5542 	INIT_LIST_HEAD(&vlan_list);
5543 
5544 	vlan_entries = (struct ice_fltr_list_entry *)
5545 	    malloc(sizeof(*vlan_entries) * length, M_ICE, M_NOWAIT | M_ZERO);
5546 	if (!vlan_entries)
5547 		return (ICE_ERR_NO_MEMORY);
5548 
5549 	for (u16 i = 0; i < length; i++) {
5550 		vlan_entries[i].fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
5551 		vlan_entries[i].fltr_info.fltr_act = ICE_FWD_TO_VSI;
5552 		vlan_entries[i].fltr_info.flag = ICE_FLTR_TX;
5553 		vlan_entries[i].fltr_info.src_id = ICE_SRC_ID_VSI;
5554 		vlan_entries[i].fltr_info.vsi_handle = vsi->idx;
5555 		vlan_entries[i].fltr_info.l_data.vlan.vlan_id = vid[i];
5556 
5557 		LIST_ADD(&vlan_entries[i].list_entry, &vlan_list);
5558 	}
5559 
5560 	status = ice_add_vlan(hw, &vlan_list);
5561 	if (!status)
5562 		goto done;
5563 
5564 	device_printf(vsi->sc->dev, "Failed to add VLAN filters:\n");
5565 	for (u16 i = 0; i < length; i++) {
5566 		device_printf(vsi->sc->dev,
5567 		    "- vlan %d, status %d\n",
5568 		    vlan_entries[i].fltr_info.l_data.vlan.vlan_id,
5569 		    vlan_entries[i].status);
5570 	}
5571 done:
5572 	free(vlan_entries, M_ICE);
5573 	return (status);
5574 }
5575 
5576 /**
5577  * ice_add_vlan_hw_filter - Add a VLAN filter for a given VSI
5578  * @vsi: The VSI to add the filter for
5579  * @vid: VLAN to add
5580  *
5581  * Programs a HW filter so that the given VSI will receive the specified VLAN.
5582  */
5583 int
ice_add_vlan_hw_filter(struct ice_vsi * vsi,u16 vid)5584 ice_add_vlan_hw_filter(struct ice_vsi *vsi, u16 vid)
5585 {
5586 	return ice_add_vlan_hw_filters(vsi, &vid, 1);
5587 }
5588 
5589 /**
5590  * ice_remove_vlan_hw_filters - Remove multiple VLAN filters for a given VSI
5591  * @vsi: The VSI to remove the filters from
5592  * @vid: array of VLAN ids to remove
5593  * @length: length of vid array
5594  *
5595  * Removes previously programmed HW filters for the specified VSI.
5596  */
5597 int
ice_remove_vlan_hw_filters(struct ice_vsi * vsi,u16 * vid,u16 length)5598 ice_remove_vlan_hw_filters(struct ice_vsi *vsi, u16 *vid, u16 length)
5599 {
5600 	struct ice_hw *hw = &vsi->sc->hw;
5601 	struct ice_list_head vlan_list;
5602 	struct ice_fltr_list_entry *vlan_entries;
5603 	int status;
5604 
5605 	MPASS(length > 0);
5606 
5607 	INIT_LIST_HEAD(&vlan_list);
5608 
5609 	vlan_entries = (struct ice_fltr_list_entry *)
5610 	    malloc(sizeof(*vlan_entries) * length, M_ICE, M_NOWAIT | M_ZERO);
5611 	if (!vlan_entries)
5612 		return (ICE_ERR_NO_MEMORY);
5613 
5614 	for (u16 i = 0; i < length; i++) {
5615 		vlan_entries[i].fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
5616 		vlan_entries[i].fltr_info.fltr_act = ICE_FWD_TO_VSI;
5617 		vlan_entries[i].fltr_info.flag = ICE_FLTR_TX;
5618 		vlan_entries[i].fltr_info.src_id = ICE_SRC_ID_VSI;
5619 		vlan_entries[i].fltr_info.vsi_handle = vsi->idx;
5620 		vlan_entries[i].fltr_info.l_data.vlan.vlan_id = vid[i];
5621 
5622 		LIST_ADD(&vlan_entries[i].list_entry, &vlan_list);
5623 	}
5624 
5625 	status = ice_remove_vlan(hw, &vlan_list);
5626 	if (!status)
5627 		goto done;
5628 
5629 	device_printf(vsi->sc->dev, "Failed to remove VLAN filters:\n");
5630 	for (u16 i = 0; i < length; i++) {
5631 		device_printf(vsi->sc->dev,
5632 		    "- vlan %d, status %d\n",
5633 		    vlan_entries[i].fltr_info.l_data.vlan.vlan_id,
5634 		    vlan_entries[i].status);
5635 	}
5636 done:
5637 	free(vlan_entries, M_ICE);
5638 	return (status);
5639 }
5640 
5641 /**
5642  * ice_remove_vlan_hw_filter - Remove a VLAN filter for a given VSI
5643  * @vsi: The VSI to remove the filter from
5644  * @vid: VLAN to remove
5645  *
5646  * Removes a previously programmed HW filter for the specified VSI.
5647  */
5648 int
ice_remove_vlan_hw_filter(struct ice_vsi * vsi,u16 vid)5649 ice_remove_vlan_hw_filter(struct ice_vsi *vsi, u16 vid)
5650 {
5651 	return ice_remove_vlan_hw_filters(vsi, &vid, 1);
5652 }
5653 
5654 #define ICE_SYSCTL_HELP_RX_ITR			\
5655 "\nControl Rx interrupt throttle rate."		\
5656 "\n\t0-8160 - sets interrupt rate in usecs"	\
5657 "\n\t    -1 - reset the Rx itr to default"
5658 
5659 /**
5660  * ice_sysctl_rx_itr - Display or change the Rx ITR for a VSI
5661  * @oidp: sysctl oid structure
5662  * @arg1: pointer to private data structure
5663  * @arg2: unused
5664  * @req: sysctl request pointer
5665  *
5666  * On read: Displays the current Rx ITR value
5667  * on write: Sets the Rx ITR value, reconfiguring device if it is up
5668  */
5669 static int
ice_sysctl_rx_itr(SYSCTL_HANDLER_ARGS)5670 ice_sysctl_rx_itr(SYSCTL_HANDLER_ARGS)
5671 {
5672 	struct ice_vsi *vsi = (struct ice_vsi *)arg1;
5673 	struct ice_softc *sc = vsi->sc;
5674 	int increment, ret;
5675 
5676 	UNREFERENCED_PARAMETER(arg2);
5677 
5678 	if (ice_driver_is_detaching(sc))
5679 		return (ESHUTDOWN);
5680 
5681 	ret = sysctl_handle_16(oidp, &vsi->rx_itr, 0, req);
5682 	if ((ret) || (req->newptr == NULL))
5683 		return (ret);
5684 
5685 	if (vsi->rx_itr < 0)
5686 		vsi->rx_itr = ICE_DFLT_RX_ITR;
5687 	if (vsi->rx_itr > ICE_ITR_MAX)
5688 		vsi->rx_itr = ICE_ITR_MAX;
5689 
5690 	/* Assume 2usec increment if it hasn't been loaded yet */
5691 	increment = sc->hw.itr_gran ? : 2;
5692 
5693 	/* We need to round the value to the hardware's ITR granularity */
5694 	vsi->rx_itr = (vsi->rx_itr / increment ) * increment;
5695 
5696 	/* If the driver has finished initializing, then we need to reprogram
5697 	 * the ITR registers now. Otherwise, they will be programmed during
5698 	 * driver initialization.
5699 	 */
5700 	if (ice_test_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED))
5701 		ice_configure_rx_itr(vsi);
5702 
5703 	return (0);
5704 }
5705 
5706 #define ICE_SYSCTL_HELP_TX_ITR			\
5707 "\nControl Tx interrupt throttle rate."		\
5708 "\n\t0-8160 - sets interrupt rate in usecs"	\
5709 "\n\t    -1 - reset the Tx itr to default"
5710 
5711 /**
5712  * ice_sysctl_tx_itr - Display or change the Tx ITR for a VSI
5713  * @oidp: sysctl oid structure
5714  * @arg1: pointer to private data structure
5715  * @arg2: unused
5716  * @req: sysctl request pointer
5717  *
5718  * On read: Displays the current Tx ITR value
5719  * on write: Sets the Tx ITR value, reconfiguring device if it is up
5720  */
5721 static int
ice_sysctl_tx_itr(SYSCTL_HANDLER_ARGS)5722 ice_sysctl_tx_itr(SYSCTL_HANDLER_ARGS)
5723 {
5724 	struct ice_vsi *vsi = (struct ice_vsi *)arg1;
5725 	struct ice_softc *sc = vsi->sc;
5726 	int increment, ret;
5727 
5728 	UNREFERENCED_PARAMETER(arg2);
5729 
5730 	if (ice_driver_is_detaching(sc))
5731 		return (ESHUTDOWN);
5732 
5733 	ret = sysctl_handle_16(oidp, &vsi->tx_itr, 0, req);
5734 	if ((ret) || (req->newptr == NULL))
5735 		return (ret);
5736 
5737 	/* Allow configuring a negative value to reset to the default */
5738 	if (vsi->tx_itr < 0)
5739 		vsi->tx_itr = ICE_DFLT_TX_ITR;
5740 	if (vsi->tx_itr > ICE_ITR_MAX)
5741 		vsi->tx_itr = ICE_ITR_MAX;
5742 
5743 	/* Assume 2usec increment if it hasn't been loaded yet */
5744 	increment = sc->hw.itr_gran ? : 2;
5745 
5746 	/* We need to round the value to the hardware's ITR granularity */
5747 	vsi->tx_itr = (vsi->tx_itr / increment ) * increment;
5748 
5749 	/* If the driver has finished initializing, then we need to reprogram
5750 	 * the ITR registers now. Otherwise, they will be programmed during
5751 	 * driver initialization.
5752 	 */
5753 	if (ice_test_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED))
5754 		ice_configure_tx_itr(vsi);
5755 
5756 	return (0);
5757 }
5758 
5759 /**
5760  * ice_add_vsi_tunables - Add tunables and nodes for a VSI
5761  * @vsi: pointer to VSI structure
5762  * @parent: parent node to add the tunables under
5763  *
5764  * Create a sysctl context for the VSI, so that sysctls for the VSI can be
5765  * dynamically removed upon VSI removal.
5766  *
5767  * Add various tunables and set up the basic node structure for the VSI. Must
5768  * be called *prior* to ice_add_vsi_sysctls. It should be called as soon as
5769  * possible after the VSI memory is initialized.
5770  *
5771  * VSI specific sysctls with CTLFLAG_TUN should be initialized here so that
5772  * their values can be read from loader.conf prior to their first use in the
5773  * driver.
5774  */
5775 void
ice_add_vsi_tunables(struct ice_vsi * vsi,struct sysctl_oid * parent)5776 ice_add_vsi_tunables(struct ice_vsi *vsi, struct sysctl_oid *parent)
5777 {
5778 	struct sysctl_oid_list *vsi_list;
5779 	char vsi_name[32], vsi_desc[32];
5780 
5781 	struct sysctl_oid_list *parent_list = SYSCTL_CHILDREN(parent);
5782 
5783 	/* Initialize the sysctl context for this VSI */
5784 	sysctl_ctx_init(&vsi->ctx);
5785 
5786 	/* Add a node to collect this VSI's statistics together */
5787 	snprintf(vsi_name, sizeof(vsi_name), "%u", vsi->idx);
5788 	snprintf(vsi_desc, sizeof(vsi_desc), "VSI %u", vsi->idx);
5789 	vsi->vsi_node = SYSCTL_ADD_NODE(&vsi->ctx, parent_list, OID_AUTO, vsi_name,
5790 					CTLFLAG_RD, NULL, vsi_desc);
5791 	vsi_list = SYSCTL_CHILDREN(vsi->vsi_node);
5792 
5793 	vsi->rx_itr = ICE_DFLT_TX_ITR;
5794 	SYSCTL_ADD_PROC(&vsi->ctx, vsi_list, OID_AUTO, "rx_itr",
5795 			CTLTYPE_S16 | CTLFLAG_RWTUN,
5796 			vsi, 0, ice_sysctl_rx_itr, "S",
5797 			ICE_SYSCTL_HELP_RX_ITR);
5798 
5799 	vsi->tx_itr = ICE_DFLT_TX_ITR;
5800 	SYSCTL_ADD_PROC(&vsi->ctx, vsi_list, OID_AUTO, "tx_itr",
5801 			CTLTYPE_S16 | CTLFLAG_RWTUN,
5802 			vsi, 0, ice_sysctl_tx_itr, "S",
5803 			ICE_SYSCTL_HELP_TX_ITR);
5804 }
5805 
5806 /**
5807  * ice_del_vsi_sysctl_ctx - Delete the sysctl context(s) of a VSI
5808  * @vsi: the VSI to remove contexts for
5809  *
5810  * Free the context for the VSI sysctls. This includes the main context, as
5811  * well as the per-queue sysctls.
5812  */
5813 void
ice_del_vsi_sysctl_ctx(struct ice_vsi * vsi)5814 ice_del_vsi_sysctl_ctx(struct ice_vsi *vsi)
5815 {
5816 	device_t dev = vsi->sc->dev;
5817 	int err;
5818 
5819 	if (vsi->vsi_node) {
5820 		err = sysctl_ctx_free(&vsi->ctx);
5821 		if (err)
5822 			device_printf(dev, "failed to free VSI %d sysctl context, err %s\n",
5823 				      vsi->idx, ice_err_str(err));
5824 		vsi->vsi_node = NULL;
5825 	}
5826 }
5827 
5828 /**
5829  * ice_add_dscp2tc_map_sysctls - Add sysctl tree for DSCP to TC mapping
5830  * @sc: pointer to device private softc
5831  * @ctx: the sysctl ctx to use
5832  * @ctx_list: list of sysctl children for device (to add sysctl tree to)
5833  *
5834  * Add a sysctl tree for individual dscp2tc_map sysctls. Each child of this
5835  * node can map 8 DSCPs to TC values; there are 8 of these in turn for a total
5836  * of 64 DSCP to TC map values that the user can configure.
5837  */
5838 void
ice_add_dscp2tc_map_sysctls(struct ice_softc * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * ctx_list)5839 ice_add_dscp2tc_map_sysctls(struct ice_softc *sc,
5840 			    struct sysctl_ctx_list *ctx,
5841 			    struct sysctl_oid_list *ctx_list)
5842 {
5843 	struct sysctl_oid_list *node_list;
5844 	struct sysctl_oid *node;
5845 	struct sbuf *namebuf, *descbuf;
5846 	int first_dscp_val, last_dscp_val;
5847 
5848 	node = SYSCTL_ADD_NODE(ctx, ctx_list, OID_AUTO, "dscp2tc_map", CTLFLAG_RD,
5849 			       NULL, "Map of DSCP values to DCB TCs");
5850 	node_list = SYSCTL_CHILDREN(node);
5851 
5852 	namebuf = sbuf_new_auto();
5853 	descbuf = sbuf_new_auto();
5854 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
5855 		sbuf_clear(namebuf);
5856 		sbuf_clear(descbuf);
5857 
5858 		first_dscp_val = i * 8;
5859 		last_dscp_val = first_dscp_val + 7;
5860 
5861 		sbuf_printf(namebuf, "%d-%d", first_dscp_val, last_dscp_val);
5862 		sbuf_printf(descbuf, "Map DSCP values %d to %d to TCs",
5863 			    first_dscp_val, last_dscp_val);
5864 
5865 		sbuf_finish(namebuf);
5866 		sbuf_finish(descbuf);
5867 
5868 		SYSCTL_ADD_PROC(ctx, node_list,
5869 		    OID_AUTO, sbuf_data(namebuf), CTLTYPE_STRING | CTLFLAG_RW,
5870 		    sc, i, ice_sysctl_dscp2tc_map, "A", sbuf_data(descbuf));
5871 	}
5872 
5873 	sbuf_delete(namebuf);
5874 	sbuf_delete(descbuf);
5875 }
5876 
5877 /**
5878  * ice_add_device_tunables - Add early tunable sysctls and sysctl nodes
5879  * @sc: device private structure
5880  *
5881  * Add per-device dynamic tunable sysctls, and setup the general sysctl trees
5882  * for re-use by ice_add_device_sysctls.
5883  *
5884  * In order for the sysctl fields to be initialized before use, this function
5885  * should be called as early as possible during attach activities.
5886  *
5887  * Any non-global sysctl marked as CTLFLAG_TUN should likely be initialized
5888  * here in this function, rather than later in ice_add_device_sysctls.
5889  *
5890  * To make things easier, this function is also expected to setup the various
5891  * sysctl nodes in addition to tunables so that other sysctls which can't be
5892  * initialized early can hook into the same nodes.
5893  */
5894 void
ice_add_device_tunables(struct ice_softc * sc)5895 ice_add_device_tunables(struct ice_softc *sc)
5896 {
5897 	device_t dev = sc->dev;
5898 
5899 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
5900 	struct sysctl_oid_list *ctx_list =
5901 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
5902 
5903 	sc->enable_health_events = ice_enable_health_events;
5904 
5905 	SYSCTL_ADD_BOOL(ctx, ctx_list, OID_AUTO, "enable_health_events",
5906 			CTLFLAG_RDTUN, &sc->enable_health_events, 0,
5907 			"Enable FW health event reporting for this PF");
5908 
5909 	/* Add a node to track VSI sysctls. Keep track of the node in the
5910 	 * softc so that we can hook other sysctls into it later. This
5911 	 * includes both the VSI statistics, as well as potentially dynamic
5912 	 * VSIs in the future.
5913 	 */
5914 
5915 	sc->vsi_sysctls = SYSCTL_ADD_NODE(ctx, ctx_list, OID_AUTO, "vsi",
5916 					  CTLFLAG_RD, NULL, "VSI Configuration and Statistics");
5917 
5918 	/* Add debug tunables */
5919 	ice_add_debug_tunables(sc);
5920 }
5921 
5922 /**
5923  * ice_sysctl_dump_mac_filters - Dump a list of all HW MAC Filters
5924  * @oidp: sysctl oid structure
5925  * @arg1: pointer to private data structure
5926  * @arg2: unused
5927  * @req: sysctl request pointer
5928  *
5929  * Callback for "mac_filters" sysctl to dump the programmed MAC filters.
5930  */
5931 static int
ice_sysctl_dump_mac_filters(SYSCTL_HANDLER_ARGS)5932 ice_sysctl_dump_mac_filters(SYSCTL_HANDLER_ARGS)
5933 {
5934 	struct ice_softc *sc = (struct ice_softc *)arg1;
5935 	struct ice_hw *hw = &sc->hw;
5936 	struct ice_switch_info *sw = hw->switch_info;
5937 	struct ice_fltr_mgmt_list_entry *fm_entry;
5938 	struct ice_list_head *rule_head;
5939 	struct ice_lock *rule_lock;
5940 	struct ice_fltr_info *fi;
5941 	struct sbuf *sbuf;
5942 	int ret;
5943 
5944 	UNREFERENCED_PARAMETER(oidp);
5945 	UNREFERENCED_PARAMETER(arg2);
5946 
5947 	if (ice_driver_is_detaching(sc))
5948 		return (ESHUTDOWN);
5949 
5950 	/* Wire the old buffer so we can take a non-sleepable lock */
5951 	ret = sysctl_wire_old_buffer(req, 0);
5952 	if (ret)
5953 		return (ret);
5954 
5955 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
5956 
5957 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
5958 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
5959 
5960 	sbuf_printf(sbuf, "MAC Filter List");
5961 
5962 	ice_acquire_lock(rule_lock);
5963 
5964 	LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry, list_entry) {
5965 		fi = &fm_entry->fltr_info;
5966 
5967 		sbuf_printf(sbuf,
5968 			    "\nmac = %6D, vsi_handle = %3d, fw_act_flag = %5s, lb_en = %1d, lan_en = %1d, fltr_act = %15s, fltr_rule_id = %d",
5969 			    fi->l_data.mac.mac_addr, ":", fi->vsi_handle,
5970 			    ice_fltr_flag_str(fi->flag), fi->lb_en, fi->lan_en,
5971 			    ice_fwd_act_str(fi->fltr_act), fi->fltr_rule_id);
5972 
5973 		/* if we have a vsi_list_info, print some information about that */
5974 		if (fm_entry->vsi_list_info) {
5975 			sbuf_printf(sbuf,
5976 				    ", vsi_count = %3d, vsi_list_id = %3d, ref_cnt = %3d",
5977 				    fm_entry->vsi_count,
5978 				    fm_entry->vsi_list_info->vsi_list_id,
5979 				    fm_entry->vsi_list_info->ref_cnt);
5980 		}
5981 	}
5982 
5983 	ice_release_lock(rule_lock);
5984 
5985 	sbuf_finish(sbuf);
5986 	sbuf_delete(sbuf);
5987 
5988 	return (0);
5989 }
5990 
5991 /**
5992  * ice_sysctl_dump_vlan_filters - Dump a list of all HW VLAN Filters
5993  * @oidp: sysctl oid structure
5994  * @arg1: pointer to private data structure
5995  * @arg2: unused
5996  * @req: sysctl request pointer
5997  *
5998  * Callback for "vlan_filters" sysctl to dump the programmed VLAN filters.
5999  */
6000 static int
ice_sysctl_dump_vlan_filters(SYSCTL_HANDLER_ARGS)6001 ice_sysctl_dump_vlan_filters(SYSCTL_HANDLER_ARGS)
6002 {
6003 	struct ice_softc *sc = (struct ice_softc *)arg1;
6004 	struct ice_hw *hw = &sc->hw;
6005 	struct ice_switch_info *sw = hw->switch_info;
6006 	struct ice_fltr_mgmt_list_entry *fm_entry;
6007 	struct ice_list_head *rule_head;
6008 	struct ice_lock *rule_lock;
6009 	struct ice_fltr_info *fi;
6010 	struct sbuf *sbuf;
6011 	int ret;
6012 
6013 	UNREFERENCED_PARAMETER(oidp);
6014 	UNREFERENCED_PARAMETER(arg2);
6015 
6016 	if (ice_driver_is_detaching(sc))
6017 		return (ESHUTDOWN);
6018 
6019 	/* Wire the old buffer so we can take a non-sleepable lock */
6020 	ret = sysctl_wire_old_buffer(req, 0);
6021 	if (ret)
6022 		return (ret);
6023 
6024 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6025 
6026 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
6027 	rule_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
6028 
6029 	sbuf_printf(sbuf, "VLAN Filter List");
6030 
6031 	ice_acquire_lock(rule_lock);
6032 
6033 	LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry, list_entry) {
6034 		fi = &fm_entry->fltr_info;
6035 
6036 		sbuf_printf(sbuf,
6037 			    "\nvlan_id = %4d, vsi_handle = %3d, fw_act_flag = %5s, lb_en = %1d, lan_en = %1d, fltr_act = %15s, fltr_rule_id = %4d",
6038 			    fi->l_data.vlan.vlan_id, fi->vsi_handle,
6039 			    ice_fltr_flag_str(fi->flag), fi->lb_en, fi->lan_en,
6040 			    ice_fwd_act_str(fi->fltr_act), fi->fltr_rule_id);
6041 
6042 		/* if we have a vsi_list_info, print some information about that */
6043 		if (fm_entry->vsi_list_info) {
6044 			sbuf_printf(sbuf,
6045 				    ", vsi_count = %3d, vsi_list_id = %3d, ref_cnt = %3d",
6046 				    fm_entry->vsi_count,
6047 				    fm_entry->vsi_list_info->vsi_list_id,
6048 				    fm_entry->vsi_list_info->ref_cnt);
6049 		}
6050 	}
6051 
6052 	ice_release_lock(rule_lock);
6053 
6054 	sbuf_finish(sbuf);
6055 	sbuf_delete(sbuf);
6056 
6057 	return (0);
6058 }
6059 
6060 /**
6061  * ice_sysctl_dump_ethertype_filters - Dump a list of all HW Ethertype filters
6062  * @oidp: sysctl oid structure
6063  * @arg1: pointer to private data structure
6064  * @arg2: unused
6065  * @req: sysctl request pointer
6066  *
6067  * Callback for "ethertype_filters" sysctl to dump the programmed Ethertype
6068  * filters.
6069  */
6070 static int
ice_sysctl_dump_ethertype_filters(SYSCTL_HANDLER_ARGS)6071 ice_sysctl_dump_ethertype_filters(SYSCTL_HANDLER_ARGS)
6072 {
6073 	struct ice_softc *sc = (struct ice_softc *)arg1;
6074 	struct ice_hw *hw = &sc->hw;
6075 	struct ice_switch_info *sw = hw->switch_info;
6076 	struct ice_fltr_mgmt_list_entry *fm_entry;
6077 	struct ice_list_head *rule_head;
6078 	struct ice_lock *rule_lock;
6079 	struct ice_fltr_info *fi;
6080 	struct sbuf *sbuf;
6081 	int ret;
6082 
6083 	UNREFERENCED_PARAMETER(oidp);
6084 	UNREFERENCED_PARAMETER(arg2);
6085 
6086 	if (ice_driver_is_detaching(sc))
6087 		return (ESHUTDOWN);
6088 
6089 	/* Wire the old buffer so we can take a non-sleepable lock */
6090 	ret = sysctl_wire_old_buffer(req, 0);
6091 	if (ret)
6092 		return (ret);
6093 
6094 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6095 
6096 	rule_lock = &sw->recp_list[ICE_SW_LKUP_ETHERTYPE].filt_rule_lock;
6097 	rule_head = &sw->recp_list[ICE_SW_LKUP_ETHERTYPE].filt_rules;
6098 
6099 	sbuf_printf(sbuf, "Ethertype Filter List");
6100 
6101 	ice_acquire_lock(rule_lock);
6102 
6103 	LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry, list_entry) {
6104 		fi = &fm_entry->fltr_info;
6105 
6106 		sbuf_printf(sbuf,
6107 			    "\nethertype = 0x%04x, vsi_handle = %3d, fw_act_flag = %5s, lb_en = %1d, lan_en = %1d, fltr_act = %15s, fltr_rule_id = %4d",
6108 			fi->l_data.ethertype_mac.ethertype,
6109 			fi->vsi_handle, ice_fltr_flag_str(fi->flag),
6110 			fi->lb_en, fi->lan_en, ice_fwd_act_str(fi->fltr_act),
6111 			fi->fltr_rule_id);
6112 
6113 		/* if we have a vsi_list_info, print some information about that */
6114 		if (fm_entry->vsi_list_info) {
6115 			sbuf_printf(sbuf,
6116 				    ", vsi_count = %3d, vsi_list_id = %3d, ref_cnt = %3d",
6117 				    fm_entry->vsi_count,
6118 				    fm_entry->vsi_list_info->vsi_list_id,
6119 				    fm_entry->vsi_list_info->ref_cnt);
6120 		}
6121 	}
6122 
6123 	ice_release_lock(rule_lock);
6124 
6125 	sbuf_finish(sbuf);
6126 	sbuf_delete(sbuf);
6127 
6128 	return (0);
6129 }
6130 
6131 /**
6132  * ice_sysctl_dump_ethertype_mac_filters - Dump a list of all HW Ethertype/MAC filters
6133  * @oidp: sysctl oid structure
6134  * @arg1: pointer to private data structure
6135  * @arg2: unused
6136  * @req: sysctl request pointer
6137  *
6138  * Callback for "ethertype_mac_filters" sysctl to dump the programmed
6139  * Ethertype/MAC filters.
6140  */
6141 static int
ice_sysctl_dump_ethertype_mac_filters(SYSCTL_HANDLER_ARGS)6142 ice_sysctl_dump_ethertype_mac_filters(SYSCTL_HANDLER_ARGS)
6143 {
6144 	struct ice_softc *sc = (struct ice_softc *)arg1;
6145 	struct ice_hw *hw = &sc->hw;
6146 	struct ice_switch_info *sw = hw->switch_info;
6147 	struct ice_fltr_mgmt_list_entry *fm_entry;
6148 	struct ice_list_head *rule_head;
6149 	struct ice_lock *rule_lock;
6150 	struct ice_fltr_info *fi;
6151 	struct sbuf *sbuf;
6152 	int ret;
6153 
6154 	UNREFERENCED_PARAMETER(oidp);
6155 	UNREFERENCED_PARAMETER(arg2);
6156 
6157 	if (ice_driver_is_detaching(sc))
6158 		return (ESHUTDOWN);
6159 
6160 	/* Wire the old buffer so we can take a non-sleepable lock */
6161 	ret = sysctl_wire_old_buffer(req, 0);
6162 	if (ret)
6163 		return (ret);
6164 
6165 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6166 
6167 	rule_lock = &sw->recp_list[ICE_SW_LKUP_ETHERTYPE_MAC].filt_rule_lock;
6168 	rule_head = &sw->recp_list[ICE_SW_LKUP_ETHERTYPE_MAC].filt_rules;
6169 
6170 	sbuf_printf(sbuf, "Ethertype/MAC Filter List");
6171 
6172 	ice_acquire_lock(rule_lock);
6173 
6174 	LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry, list_entry) {
6175 		fi = &fm_entry->fltr_info;
6176 
6177 		sbuf_printf(sbuf,
6178 			    "\nethertype = 0x%04x, mac = %6D, vsi_handle = %3d, fw_act_flag = %5s, lb_en = %1d, lan_en = %1d, fltr_act = %15s, fltr_rule_id = %4d",
6179 			    fi->l_data.ethertype_mac.ethertype,
6180 			    fi->l_data.ethertype_mac.mac_addr, ":",
6181 			    fi->vsi_handle, ice_fltr_flag_str(fi->flag),
6182 			    fi->lb_en, fi->lan_en, ice_fwd_act_str(fi->fltr_act),
6183 			    fi->fltr_rule_id);
6184 
6185 		/* if we have a vsi_list_info, print some information about that */
6186 		if (fm_entry->vsi_list_info) {
6187 			sbuf_printf(sbuf,
6188 				    ", vsi_count = %3d, vsi_list_id = %3d, ref_cnt = %3d",
6189 				    fm_entry->vsi_count,
6190 				    fm_entry->vsi_list_info->vsi_list_id,
6191 				    fm_entry->vsi_list_info->ref_cnt);
6192 		}
6193 	}
6194 
6195 	ice_release_lock(rule_lock);
6196 
6197 	sbuf_finish(sbuf);
6198 	sbuf_delete(sbuf);
6199 
6200 	return (0);
6201 }
6202 
6203 /**
6204  * ice_sysctl_dump_state_flags - Dump device driver state flags
6205  * @oidp: sysctl oid structure
6206  * @arg1: pointer to private data structure
6207  * @arg2: unused
6208  * @req: sysctl request pointer
6209  *
6210  * Callback for "state" sysctl to display currently set driver state flags.
6211  */
6212 static int
ice_sysctl_dump_state_flags(SYSCTL_HANDLER_ARGS)6213 ice_sysctl_dump_state_flags(SYSCTL_HANDLER_ARGS)
6214 {
6215 	struct ice_softc *sc = (struct ice_softc *)arg1;
6216 	struct sbuf *sbuf;
6217 	u32 copied_state;
6218 	unsigned int i;
6219 	bool at_least_one = false;
6220 
6221 	UNREFERENCED_PARAMETER(oidp);
6222 	UNREFERENCED_PARAMETER(arg2);
6223 
6224 	if (ice_driver_is_detaching(sc))
6225 		return (ESHUTDOWN);
6226 
6227 	/* Make a copy of the state to ensure we display coherent values */
6228 	copied_state = atomic_load_acq_32(&sc->state);
6229 
6230 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6231 
6232 	/* Add the string for each set state to the sbuf */
6233 	for (i = 0; i < 32; i++) {
6234 		if (copied_state & BIT(i)) {
6235 			const char *str = ice_state_to_str((enum ice_state)i);
6236 
6237 			at_least_one = true;
6238 
6239 			if (str)
6240 				sbuf_printf(sbuf, "\n%s", str);
6241 			else
6242 				sbuf_printf(sbuf, "\nBIT(%u)", i);
6243 		}
6244 	}
6245 
6246 	if (!at_least_one)
6247 		sbuf_printf(sbuf, "Nothing set");
6248 
6249 	sbuf_finish(sbuf);
6250 	sbuf_delete(sbuf);
6251 
6252 	return (0);
6253 }
6254 
6255 #define ICE_SYSCTL_DEBUG_MASK_HELP \
6256 "\nSelect debug statements to print to kernel message log"	\
6257 "\nFlags:"							\
6258 "\n\t         0x1 - Function Tracing"				\
6259 "\n\t         0x2 - Driver Initialization"			\
6260 "\n\t         0x4 - Release"					\
6261 "\n\t         0x8 - FW Logging"					\
6262 "\n\t        0x10 - Link"					\
6263 "\n\t        0x20 - PHY"					\
6264 "\n\t        0x40 - Queue Context"				\
6265 "\n\t        0x80 - NVM"					\
6266 "\n\t       0x100 - LAN"					\
6267 "\n\t       0x200 - Flow"					\
6268 "\n\t       0x400 - DCB"					\
6269 "\n\t       0x800 - Diagnostics"				\
6270 "\n\t      0x1000 - Flow Director"				\
6271 "\n\t      0x2000 - Switch"					\
6272 "\n\t      0x4000 - Scheduler"					\
6273 "\n\t      0x8000 - RDMA"					\
6274 "\n\t     0x10000 - DDP Package"				\
6275 "\n\t     0x20000 - Resources"					\
6276 "\n\t     0x40000 - ACL"					\
6277 "\n\t     0x80000 - PTP"					\
6278 "\n\t   ..."							\
6279 "\n\t   0x1000000 - Admin Queue messages"			\
6280 "\n\t   0x2000000 - Admin Queue descriptors"			\
6281 "\n\t   0x4000000 - Admin Queue descriptor buffers"		\
6282 "\n\t   0x8000000 - Admin Queue commands"			\
6283 "\n\t  0x10000000 - Parser"					\
6284 "\n\t   ..."							\
6285 "\n\t  0x80000000 - (Reserved for user)"			\
6286 "\n\t"								\
6287 "\nUse \"sysctl -x\" to view flags properly."
6288 
6289 /**
6290  * ice_add_debug_tunables - Add tunables helpful for debugging the device driver
6291  * @sc: device private structure
6292  *
6293  * Add sysctl tunable values related to debugging the device driver. For now,
6294  * this means a tunable to set the debug mask early during driver load.
6295  *
6296  * The debug node will be marked CTLFLAG_SKIP unless INVARIANTS is defined, so
6297  * that in normal kernel builds, these will all be hidden, but on a debug
6298  * kernel they will be more easily visible.
6299  */
6300 static void
ice_add_debug_tunables(struct ice_softc * sc)6301 ice_add_debug_tunables(struct ice_softc *sc)
6302 {
6303 	struct sysctl_oid_list *debug_list;
6304 	device_t dev = sc->dev;
6305 
6306 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
6307 	struct sysctl_oid_list *ctx_list =
6308 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
6309 
6310 	sc->debug_sysctls = SYSCTL_ADD_NODE(ctx, ctx_list, OID_AUTO, "debug",
6311 					    ICE_CTLFLAG_DEBUG | CTLFLAG_RD,
6312 					    NULL, "Debug Sysctls");
6313 	debug_list = SYSCTL_CHILDREN(sc->debug_sysctls);
6314 
6315 	SYSCTL_ADD_U64(ctx, debug_list, OID_AUTO, "debug_mask",
6316 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RWTUN,
6317 		       &sc->hw.debug_mask, 0,
6318 		       ICE_SYSCTL_DEBUG_MASK_HELP);
6319 
6320 	/* Load the default value from the global sysctl first */
6321 	sc->enable_tx_fc_filter = ice_enable_tx_fc_filter;
6322 
6323 	SYSCTL_ADD_BOOL(ctx, debug_list, OID_AUTO, "enable_tx_fc_filter",
6324 			ICE_CTLFLAG_DEBUG | CTLFLAG_RDTUN,
6325 			&sc->enable_tx_fc_filter, 0,
6326 			"Drop Ethertype 0x8808 control frames originating from software on this PF");
6327 
6328 	sc->tx_balance_en = ice_tx_balance_en;
6329 	SYSCTL_ADD_BOOL(ctx, debug_list, OID_AUTO, "tx_balance",
6330 			ICE_CTLFLAG_DEBUG | CTLFLAG_RWTUN,
6331 			&sc->tx_balance_en, 0,
6332 			"Enable 5-layer scheduler topology");
6333 
6334 	/* Load the default value from the global sysctl first */
6335 	sc->enable_tx_lldp_filter = ice_enable_tx_lldp_filter;
6336 
6337 	SYSCTL_ADD_BOOL(ctx, debug_list, OID_AUTO, "enable_tx_lldp_filter",
6338 			ICE_CTLFLAG_DEBUG | CTLFLAG_RDTUN,
6339 			&sc->enable_tx_lldp_filter, 0,
6340 			"Drop Ethertype 0x88cc LLDP frames originating from software on this PF");
6341 
6342 	ice_add_fw_logging_tunables(sc, sc->debug_sysctls);
6343 }
6344 
6345 #define ICE_SYSCTL_HELP_REQUEST_RESET		\
6346 "\nRequest the driver to initiate a reset."	\
6347 "\n\tpfr - Initiate a PF reset"			\
6348 "\n\tcorer - Initiate a CORE reset"		\
6349 "\n\tglobr - Initiate a GLOBAL reset"
6350 
6351 /**
6352  * @var rl_sysctl_ticks
6353  * @brief timestamp for latest reset request sysctl call
6354  *
6355  * Helps rate-limit the call to the sysctl which resets the device
6356  */
6357 int rl_sysctl_ticks = 0;
6358 
6359 /**
6360  * ice_sysctl_request_reset - Request that the driver initiate a reset
6361  * @oidp: sysctl oid structure
6362  * @arg1: pointer to private data structure
6363  * @arg2: unused
6364  * @req: sysctl request pointer
6365  *
6366  * Callback for "request_reset" sysctl to request that the driver initiate
6367  * a reset. Expects to be passed one of the following strings
6368  *
6369  * "pfr" - Initiate a PF reset
6370  * "corer" - Initiate a CORE reset
6371  * "globr" - Initiate a Global reset
6372  */
6373 static int
ice_sysctl_request_reset(SYSCTL_HANDLER_ARGS)6374 ice_sysctl_request_reset(SYSCTL_HANDLER_ARGS)
6375 {
6376 	struct ice_softc *sc = (struct ice_softc *)arg1;
6377 	struct ice_hw *hw = &sc->hw;
6378 	int status;
6379 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
6380 	const char *reset_message;
6381 	int ret;
6382 
6383 	/* Buffer to store the requested reset string. Must contain enough
6384 	 * space to store the largest expected reset string, which currently
6385 	 * means 6 bytes of space.
6386 	 */
6387 	char reset[6] = "";
6388 
6389 	UNREFERENCED_PARAMETER(arg2);
6390 
6391 	ret = priv_check(curthread, PRIV_DRIVER);
6392 	if (ret)
6393 		return (ret);
6394 
6395 	if (ice_driver_is_detaching(sc))
6396 		return (ESHUTDOWN);
6397 
6398 	/* Read in the requested reset type. */
6399 	ret = sysctl_handle_string(oidp, reset, sizeof(reset), req);
6400 	if ((ret) || (req->newptr == NULL))
6401 		return (ret);
6402 
6403 	if (strcmp(reset, "pfr") == 0) {
6404 		reset_message = "Requesting a PF reset";
6405 		reset_type = ICE_RESET_PFR;
6406 	} else if (strcmp(reset, "corer") == 0) {
6407 		reset_message = "Initiating a CORE reset";
6408 		reset_type = ICE_RESET_CORER;
6409 	} else if (strcmp(reset, "globr") == 0) {
6410 		reset_message = "Initiating a GLOBAL reset";
6411 		reset_type = ICE_RESET_GLOBR;
6412 	} else if (strcmp(reset, "empr") == 0) {
6413 		device_printf(sc->dev, "Triggering an EMP reset via software is not currently supported\n");
6414 		return (EOPNOTSUPP);
6415 	}
6416 
6417 	if (reset_type == ICE_RESET_INVAL) {
6418 		device_printf(sc->dev, "%s is not a valid reset request\n", reset);
6419 		return (EINVAL);
6420 	}
6421 
6422 	/*
6423 	 * Rate-limit the frequency at which this function is called.
6424 	 * Assuming this is called successfully once, typically,
6425 	 * everything should be handled within the allotted time frame.
6426 	 * However, in the odd setup situations, we've also put in
6427 	 * guards for when the reset has finished, but we're in the
6428 	 * process of rebuilding. And instead of queueing an intent,
6429 	 * simply error out and let the caller retry, if so desired.
6430 	 */
6431 	if (TICKS_2_MSEC(ticks - rl_sysctl_ticks) < 500) {
6432 		device_printf(sc->dev,
6433 		    "Call frequency too high. Operation aborted.\n");
6434 		return (EBUSY);
6435 	}
6436 	rl_sysctl_ticks = ticks;
6437 
6438 	if (TICKS_2_MSEC(ticks - sc->rebuild_ticks) < 100) {
6439 		device_printf(sc->dev, "Device rebuilding. Operation aborted.\n");
6440 		return (EBUSY);
6441 	}
6442 
6443 	if (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_DEVSTATE_M) {
6444 		device_printf(sc->dev, "Device in reset. Operation aborted.\n");
6445 		return (EBUSY);
6446 	}
6447 
6448 	device_printf(sc->dev, "%s\n", reset_message);
6449 
6450 	/* Initiate the PF reset during the admin status task */
6451 	if (reset_type == ICE_RESET_PFR) {
6452 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
6453 		return (0);
6454 	}
6455 
6456 	/*
6457 	 * Other types of resets including CORE and GLOBAL resets trigger an
6458 	 * interrupt on all PFs. Initiate the reset now. Preparation and
6459 	 * rebuild logic will be handled by the admin status task.
6460 	 */
6461 	status = ice_reset(hw, reset_type);
6462 
6463 	/*
6464 	 * Resets can take a long time and we still don't want another call
6465 	 * to this function before we settle down.
6466 	 */
6467 	rl_sysctl_ticks = ticks;
6468 
6469 	if (status) {
6470 		device_printf(sc->dev, "failed to initiate device reset, err %s\n",
6471 			      ice_status_str(status));
6472 		ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
6473 		return (EFAULT);
6474 	}
6475 
6476 	return (0);
6477 }
6478 
6479 #define ICE_AQC_DBG_DUMP_CLUSTER_ID_INVALID	(0xFFFFFF)
6480 #define ICE_SYSCTL_HELP_FW_DEBUG_DUMP_CLUSTER_SETTING		\
6481 "\nSelect clusters to dump with \"dump\" sysctl"		\
6482 "\nFlags:"							\
6483 "\n\t        0 - All clusters (default)"			\
6484 "\n\t      0x1 - Switch"					\
6485 "\n\t      0x2 - ACL"						\
6486 "\n\t      0x4 - Tx Scheduler"					\
6487 "\n\t      0x8 - Profile Configuration"				\
6488 "\n\t     0x20 - Link"						\
6489 "\n\t     0x80 - DCB"						\
6490 "\n\t    0x100 - L2P"						\
6491 "\n\t 0x400000 - Manageability Transactions (excluding E830)"	\
6492 "\n"								\
6493 "\nUse \"sysctl -x\" to view flags properly."
6494 
6495 /**
6496  * ice_sysctl_fw_debug_dump_cluster_setting - Set which clusters to dump
6497  *     from FW when FW debug dump occurs
6498  * @oidp: sysctl oid structure
6499  * @arg1: pointer to private data structure
6500  * @arg2: unused
6501  * @req: sysctl request pointer
6502  */
6503 static int
ice_sysctl_fw_debug_dump_cluster_setting(SYSCTL_HANDLER_ARGS)6504 ice_sysctl_fw_debug_dump_cluster_setting(SYSCTL_HANDLER_ARGS)
6505 {
6506 	struct ice_softc *sc = (struct ice_softc *)arg1;
6507 	device_t dev = sc->dev;
6508 	u32 clusters;
6509 	int ret;
6510 
6511 	UNREFERENCED_PARAMETER(arg2);
6512 
6513 	ret = priv_check(curthread, PRIV_DRIVER);
6514 	if (ret)
6515 		return (ret);
6516 
6517 	if (ice_driver_is_detaching(sc))
6518 		return (ESHUTDOWN);
6519 
6520 	clusters = sc->fw_debug_dump_cluster_mask;
6521 
6522 	ret = sysctl_handle_32(oidp, &clusters, 0, req);
6523 	if ((ret) || (req->newptr == NULL))
6524 		return (ret);
6525 
6526 	u32 valid_cluster_mask;
6527 	if (ice_is_e830(&sc->hw))
6528 		valid_cluster_mask = ICE_FW_DEBUG_DUMP_VALID_CLUSTER_MASK_E830;
6529 	else
6530 		valid_cluster_mask = ICE_FW_DEBUG_DUMP_VALID_CLUSTER_MASK_E810;
6531 
6532 	if (clusters & ~(valid_cluster_mask)) {
6533 		device_printf(dev,
6534 		    "%s: ERROR: Incorrect settings requested\n",
6535 		    __func__);
6536 		sc->fw_debug_dump_cluster_mask = ICE_AQC_DBG_DUMP_CLUSTER_ID_INVALID;
6537 		return (EINVAL);
6538 	}
6539 
6540 	sc->fw_debug_dump_cluster_mask = clusters;
6541 
6542 	return (0);
6543 }
6544 
6545 #define ICE_FW_DUMP_AQ_COUNT_LIMIT	(10000)
6546 
6547 /**
6548  * ice_fw_debug_dump_print_cluster - Print formatted cluster data from FW
6549  * @sc: the device softc
6550  * @sbuf: initialized sbuf to print data to
6551  * @cluster_id: FW cluster ID to print data from
6552  *
6553  * Reads debug data from the specified cluster id in the FW and prints it to
6554  * the input sbuf. This function issues multiple AQ commands to the FW in
6555  * order to get all of the data in the cluster.
6556  *
6557  * @remark Only intended to be used by the sysctl handler
6558  * ice_sysctl_fw_debug_dump_do_dump
6559  */
6560 static u16
ice_fw_debug_dump_print_cluster(struct ice_softc * sc,struct sbuf * sbuf,u16 cluster_id)6561 ice_fw_debug_dump_print_cluster(struct ice_softc *sc, struct sbuf *sbuf, u16 cluster_id)
6562 {
6563 	struct ice_hw *hw = &sc->hw;
6564 	device_t dev = sc->dev;
6565 	u16 data_buf_size = ICE_AQ_MAX_BUF_LEN;
6566 	const u8 reserved_buf[8] = {};
6567 	int status;
6568 	int counter = 0;
6569 	u8 *data_buf;
6570 
6571 	/* Input parameters / loop variables */
6572 	u16 table_id = 0;
6573 	u32 offset = 0;
6574 
6575 	/* Output from the Get Internal Data AQ command */
6576 	u16 ret_buf_size = 0;
6577 	u16 ret_next_cluster = 0;
6578 	u16 ret_next_table = 0;
6579 	u32 ret_next_index = 0;
6580 
6581 	/* Other setup */
6582 	data_buf = (u8 *)malloc(data_buf_size, M_ICE, M_NOWAIT | M_ZERO);
6583 	if (!data_buf)
6584 		return ret_next_cluster;
6585 
6586 	ice_debug(hw, ICE_DBG_DIAG, "%s: dumping cluster id %d\n", __func__,
6587 	    cluster_id);
6588 
6589 	for (;;) {
6590 		/* Do not trust the FW behavior to be completely correct */
6591 		if (counter++ >= ICE_FW_DUMP_AQ_COUNT_LIMIT) {
6592 			device_printf(dev,
6593 			    "%s: Exceeded counter limit for cluster %d\n",
6594 			    __func__, cluster_id);
6595 			break;
6596 		}
6597 
6598 		ice_debug(hw, ICE_DBG_DIAG, "---\n");
6599 		ice_debug(hw, ICE_DBG_DIAG,
6600 		    "table_id 0x%04x offset 0x%08x buf_size %d\n",
6601 		    table_id, offset, data_buf_size);
6602 
6603 		status = ice_aq_get_internal_data(hw, cluster_id, table_id,
6604 		    offset, data_buf, data_buf_size, &ret_buf_size,
6605 		    &ret_next_cluster, &ret_next_table, &ret_next_index, NULL);
6606 		if (status) {
6607 			device_printf(dev,
6608 			    "%s: ice_aq_get_internal_data in cluster %d: err %s aq_err %s\n",
6609 			    __func__, cluster_id, ice_status_str(status),
6610 			    ice_aq_str(hw->adminq.sq_last_status));
6611 			break;
6612 		}
6613 
6614 		ice_debug(hw, ICE_DBG_DIAG,
6615 		    "ret_table_id 0x%04x ret_offset 0x%08x ret_buf_size %d\n",
6616 		    ret_next_table, ret_next_index, ret_buf_size);
6617 
6618 		/* Print cluster id */
6619 		u32 print_cluster_id = (u32)cluster_id;
6620 		sbuf_bcat(sbuf, &print_cluster_id, sizeof(print_cluster_id));
6621 		/* Print table id */
6622 		u32 print_table_id = (u32)table_id;
6623 		sbuf_bcat(sbuf, &print_table_id, sizeof(print_table_id));
6624 		/* Print table length */
6625 		u32 print_table_length = (u32)ret_buf_size;
6626 		sbuf_bcat(sbuf, &print_table_length, sizeof(print_table_length));
6627 		/* Print current offset */
6628 		u32 print_curr_offset = offset;
6629 		sbuf_bcat(sbuf, &print_curr_offset, sizeof(print_curr_offset));
6630 		/* Print reserved bytes */
6631 		sbuf_bcat(sbuf, reserved_buf, sizeof(reserved_buf));
6632 		/* Print data */
6633 		sbuf_bcat(sbuf, data_buf, ret_buf_size);
6634 
6635 		/* Adjust loop variables */
6636 		memset(data_buf, 0, data_buf_size);
6637 		bool same_table_next = (table_id == ret_next_table);
6638 		bool last_table_next;
6639 		if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_NEXT_CLUSTER_ID))
6640 			last_table_next =
6641 			    (ret_next_table == 0xffff);
6642 		else
6643 			last_table_next =
6644 			    (ret_next_table == 0xff || ret_next_table == 0xffff);
6645 		bool last_offset_next = (ret_next_index == 0xffffffff || ret_next_index == 0);
6646 
6647 		if ((!same_table_next && !last_offset_next) ||
6648 		    (same_table_next && last_table_next)) {
6649 			device_printf(dev,
6650 			    "%s: Unexpected conditions for same_table_next(%d) last_table_next(%d) last_offset_next(%d), ending cluster (%d)\n",
6651 			    __func__, same_table_next, last_table_next, last_offset_next, cluster_id);
6652 			break;
6653 		}
6654 
6655 		if (!same_table_next && !last_table_next && last_offset_next) {
6656 			/* We've hit the end of the table */
6657 			table_id = ret_next_table;
6658 			offset = 0;
6659 		}
6660 		else if (!same_table_next && last_table_next && last_offset_next) {
6661 			/* We've hit the end of the cluster */
6662 			break;
6663 		}
6664 		else if (same_table_next && !last_table_next && last_offset_next) {
6665 			if (cluster_id == 0x1 && table_id < 39)
6666 				table_id += 1;
6667 			else
6668 				break;
6669 		}
6670 		else { /* if (same_table_next && !last_table_next && !last_offset_next) */
6671 			/* More data left in the table */
6672 			offset = ret_next_index;
6673 		}
6674 	}
6675 
6676 	free(data_buf, M_ICE);
6677 	return ret_next_cluster;
6678 }
6679 
6680 /**
6681  * ice_fw_debug_dump_print_clusters - Print data from FW clusters to sbuf
6682  * @sc: the device softc
6683  * @sbuf: initialized sbuf to print data to
6684  *
6685  * Handles dumping all of the clusters to dump to the indicated sbuf. The
6686  * clusters do dump are determined by the value in the
6687  * fw_debug_dump_cluster_mask field in the sc argument.
6688  *
6689  * @remark Only intended to be used by the sysctl handler
6690  * ice_sysctl_fw_debug_dump_do_dump
6691  */
6692 static void
ice_fw_debug_dump_print_clusters(struct ice_softc * sc,struct sbuf * sbuf)6693 ice_fw_debug_dump_print_clusters(struct ice_softc *sc, struct sbuf *sbuf)
6694 {
6695 	u16 next_cluster_id, max_cluster_id, start_cluster_id;
6696 	u32 cluster_mask = sc->fw_debug_dump_cluster_mask;
6697 	struct ice_hw *hw = &sc->hw;
6698 	int bit;
6699 
6700 	ice_debug(hw, ICE_DBG_DIAG, "%s: Debug Dump running...\n", __func__);
6701 
6702 	if (ice_is_e830(hw)) {
6703 		max_cluster_id = ICE_AQC_DBG_DUMP_CLUSTER_ID_QUEUE_MNG_E830;
6704 		start_cluster_id = ICE_AQC_DBG_DUMP_CLUSTER_ID_SW_E830;
6705 	} else {
6706 		max_cluster_id = ICE_AQC_DBG_DUMP_CLUSTER_ID_QUEUE_MNG_E810;
6707 		start_cluster_id = ICE_AQC_DBG_DUMP_CLUSTER_ID_SW_E810;
6708 	}
6709 
6710 	if (cluster_mask != 0) {
6711 		for_each_set_bit(bit, &cluster_mask,
6712 		    sizeof(cluster_mask) * BITS_PER_BYTE) {
6713 			ice_fw_debug_dump_print_cluster(sc, sbuf,
6714 			    bit + start_cluster_id);
6715 		}
6716 	} else {
6717 		next_cluster_id = start_cluster_id;
6718 
6719 		/* We don't support QUEUE_MNG and FULL_CSR_SPACE */
6720 		do {
6721 			next_cluster_id =
6722 			    ice_fw_debug_dump_print_cluster(sc, sbuf, next_cluster_id);
6723 		} while ((next_cluster_id != 0) &&
6724 			 (next_cluster_id < max_cluster_id));
6725 	}
6726 
6727 }
6728 
6729 #define ICE_SYSCTL_HELP_FW_DEBUG_DUMP_DO_DUMP \
6730 "\nWrite 1 to output a FW debug dump containing the clusters specified by the" \
6731 "\n\"clusters\" sysctl."						\
6732 "\n"									\
6733 "\nThe \"-b\" flag must be used in order to dump this data as binary data because" \
6734 "\nthis data is opaque and not a string."
6735 
6736 #define ICE_FW_DUMP_BASE_TEXT_SIZE	(1024 * 1024)
6737 #define ICE_FW_DUMP_ALL_TEXT_SIZE	(10 * 1024 * 1024)
6738 #define ICE_FW_DUMP_CLUST0_TEXT_SIZE	(2 * 1024 * 1024)
6739 #define ICE_FW_DUMP_CLUST1_TEXT_SIZE	(128 * 1024)
6740 #define ICE_FW_DUMP_CLUST2_TEXT_SIZE	(2 * 1024 * 1024)
6741 
6742 /**
6743  * ice_sysctl_fw_debug_dump_do_dump - Dump data from FW to sysctl output
6744  * @oidp: sysctl oid structure
6745  * @arg1: pointer to private data structure
6746  * @arg2: unused
6747  * @req: sysctl request pointer
6748  *
6749  * Sysctl handler for the debug.dump.dump sysctl. Prints out a specially-
6750  * formatted dump of some debug FW data intended to be processed by a special
6751  * Intel tool. Prints out the cluster data specified by the "clusters"
6752  * sysctl.
6753  *
6754  * @remark The actual AQ calls and printing are handled by a helper
6755  * function above.
6756  */
6757 static int
ice_sysctl_fw_debug_dump_do_dump(SYSCTL_HANDLER_ARGS)6758 ice_sysctl_fw_debug_dump_do_dump(SYSCTL_HANDLER_ARGS)
6759 {
6760 	struct ice_softc *sc = (struct ice_softc *)arg1;
6761 	device_t dev = sc->dev;
6762 	struct sbuf *sbuf;
6763 	int ret;
6764 
6765 	UNREFERENCED_PARAMETER(arg2);
6766 
6767 	ret = priv_check(curthread, PRIV_DRIVER);
6768 	if (ret)
6769 		return (ret);
6770 
6771 	if (ice_driver_is_detaching(sc))
6772 		return (ESHUTDOWN);
6773 
6774 	/* If the user hasn't written "1" to this sysctl yet: */
6775 	if (!ice_test_state(&sc->state, ICE_STATE_DO_FW_DEBUG_DUMP)) {
6776 		/* Avoid output on the first set of reads to this sysctl in
6777 		 * order to prevent a null byte from being written to the
6778 		 * end result when called via sysctl(8).
6779 		 */
6780 		if (req->oldptr == NULL && req->newptr == NULL) {
6781 			ret = SYSCTL_OUT(req, 0, 0);
6782 			return (ret);
6783 		}
6784 
6785 		char input_buf[2] = "";
6786 		ret = sysctl_handle_string(oidp, input_buf, sizeof(input_buf), req);
6787 		if ((ret) || (req->newptr == NULL))
6788 			return (ret);
6789 
6790 		/* If we get '1', then indicate we'll do a dump in the next
6791 		 * sysctl read call.
6792 		 */
6793 		if (input_buf[0] == '1') {
6794 			if (sc->fw_debug_dump_cluster_mask == ICE_AQC_DBG_DUMP_CLUSTER_ID_INVALID) {
6795 				device_printf(dev,
6796 				    "%s: Debug Dump failed because an invalid cluster was specified.\n",
6797 				    __func__);
6798 				return (EINVAL);
6799 			}
6800 
6801 			ice_set_state(&sc->state, ICE_STATE_DO_FW_DEBUG_DUMP);
6802 			return (0);
6803 		}
6804 
6805 		return (EINVAL);
6806 	}
6807 
6808 	/* --- FW debug dump state is set --- */
6809 
6810 
6811 	/* Caller just wants the upper bound for size */
6812 	if (req->oldptr == NULL && req->newptr == NULL) {
6813 		size_t est_output_len = ICE_FW_DUMP_BASE_TEXT_SIZE;
6814 		if (sc->fw_debug_dump_cluster_mask == 0)
6815 			est_output_len += ICE_FW_DUMP_ALL_TEXT_SIZE;
6816 		else {
6817 			if (sc->fw_debug_dump_cluster_mask & 0x1)
6818 				est_output_len += ICE_FW_DUMP_CLUST0_TEXT_SIZE;
6819 			if (sc->fw_debug_dump_cluster_mask & 0x2)
6820 				est_output_len += ICE_FW_DUMP_CLUST1_TEXT_SIZE;
6821 			if (sc->fw_debug_dump_cluster_mask & 0x4)
6822 				est_output_len += ICE_FW_DUMP_CLUST2_TEXT_SIZE;
6823 		}
6824 
6825 		ret = SYSCTL_OUT(req, 0, est_output_len);
6826 		return (ret);
6827 	}
6828 
6829 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6830 	sbuf_clear_flags(sbuf, SBUF_INCLUDENUL);
6831 
6832 	ice_fw_debug_dump_print_clusters(sc, sbuf);
6833 
6834 	sbuf_finish(sbuf);
6835 	sbuf_delete(sbuf);
6836 
6837 	ice_clear_state(&sc->state, ICE_STATE_DO_FW_DEBUG_DUMP);
6838 	return (ret);
6839 }
6840 
6841 /**
6842  * ice_add_debug_sysctls - Add sysctls helpful for debugging the device driver
6843  * @sc: device private structure
6844  *
6845  * Add sysctls related to debugging the device driver. Generally these should
6846  * simply be sysctls which dump internal driver state, to aid in understanding
6847  * what the driver is doing.
6848  */
6849 static void
ice_add_debug_sysctls(struct ice_softc * sc)6850 ice_add_debug_sysctls(struct ice_softc *sc)
6851 {
6852 	struct sysctl_oid *sw_node, *dump_node;
6853 	struct sysctl_oid_list *debug_list, *sw_list, *dump_list;
6854 	device_t dev = sc->dev;
6855 
6856 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
6857 
6858 	debug_list = SYSCTL_CHILDREN(sc->debug_sysctls);
6859 
6860 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "request_reset",
6861 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_WR, sc, 0,
6862 			ice_sysctl_request_reset, "A",
6863 			ICE_SYSCTL_HELP_REQUEST_RESET);
6864 
6865 	SYSCTL_ADD_U32(ctx, debug_list, OID_AUTO, "pfr_count",
6866 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RD,
6867 		       &sc->soft_stats.pfr_count, 0,
6868 		       "# of PF resets handled");
6869 
6870 	SYSCTL_ADD_U32(ctx, debug_list, OID_AUTO, "corer_count",
6871 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RD,
6872 		       &sc->soft_stats.corer_count, 0,
6873 		       "# of CORE resets handled");
6874 
6875 	SYSCTL_ADD_U32(ctx, debug_list, OID_AUTO, "globr_count",
6876 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RD,
6877 		       &sc->soft_stats.globr_count, 0,
6878 		       "# of Global resets handled");
6879 
6880 	SYSCTL_ADD_U32(ctx, debug_list, OID_AUTO, "empr_count",
6881 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RD,
6882 		       &sc->soft_stats.empr_count, 0,
6883 		       "# of EMP resets handled");
6884 
6885 	SYSCTL_ADD_U32(ctx, debug_list, OID_AUTO, "tx_mdd_count",
6886 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RD,
6887 		       &sc->soft_stats.tx_mdd_count, 0,
6888 		       "# of Tx MDD events detected");
6889 
6890 	SYSCTL_ADD_U32(ctx, debug_list, OID_AUTO, "rx_mdd_count",
6891 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RD,
6892 		       &sc->soft_stats.rx_mdd_count, 0,
6893 		       "# of Rx MDD events detected");
6894 
6895 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "state",
6896 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6897 			ice_sysctl_dump_state_flags, "A",
6898 			"Driver State Flags");
6899 
6900 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "set_link",
6901 			ICE_CTLFLAG_DEBUG | CTLTYPE_U8 | CTLFLAG_RW, sc, 0,
6902 			ice_sysctl_debug_set_link, "CU", "Set link");
6903 
6904 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "phy_type_low",
6905 			ICE_CTLFLAG_DEBUG | CTLTYPE_U64 | CTLFLAG_RW, sc, 0,
6906 			ice_sysctl_phy_type_low, "QU",
6907 			"PHY type Low from Get PHY Caps/Set PHY Cfg");
6908 
6909 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "phy_type_high",
6910 			ICE_CTLFLAG_DEBUG | CTLTYPE_U64 | CTLFLAG_RW, sc, 0,
6911 			ice_sysctl_phy_type_high, "QU",
6912 			"PHY type High from Get PHY Caps/Set PHY Cfg");
6913 
6914 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "phy_sw_caps",
6915 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRUCT | CTLFLAG_RD, sc, 0,
6916 			ice_sysctl_phy_sw_caps, "",
6917 			"Get PHY Capabilities (Software configuration)");
6918 
6919 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "phy_nvm_caps",
6920 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRUCT | CTLFLAG_RD, sc, 0,
6921 			ice_sysctl_phy_nvm_caps, "",
6922 			"Get PHY Capabilities (NVM configuration)");
6923 
6924 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "phy_topo_caps",
6925 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRUCT | CTLFLAG_RD, sc, 0,
6926 			ice_sysctl_phy_topo_caps, "",
6927 			"Get PHY Capabilities (Topology configuration)");
6928 
6929 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "phy_link_status",
6930 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRUCT | CTLFLAG_RD, sc, 0,
6931 			ice_sysctl_phy_link_status, "",
6932 			"Get PHY Link Status");
6933 
6934 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "read_i2c_diag_data",
6935 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6936 			ice_sysctl_read_i2c_diag_data, "A",
6937 			"Dump selected diagnostic data from FW");
6938 
6939 	SYSCTL_ADD_U32(ctx, debug_list, OID_AUTO, "fw_build",
6940 		       ICE_CTLFLAG_DEBUG | CTLFLAG_RD, &sc->hw.fw_build, 0,
6941 		       "FW Build ID");
6942 
6943 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "os_ddp_version",
6944 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6945 			ice_sysctl_os_pkg_version, "A",
6946 			"DDP package name and version found in ice_ddp");
6947 
6948 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "cur_lldp_persist_status",
6949 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6950 			ice_sysctl_fw_cur_lldp_persist_status, "A",
6951 			"Current LLDP persistent status");
6952 
6953 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "dflt_lldp_persist_status",
6954 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6955 			ice_sysctl_fw_dflt_lldp_persist_status, "A",
6956 			"Default LLDP persistent status");
6957 
6958 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "negotiated_fc",
6959 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6960 			ice_sysctl_negotiated_fc, "A",
6961 			"Current Negotiated Flow Control mode");
6962 
6963 	if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_PHY_STATISTICS)) {
6964 		SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "phy_statistics",
6965 				CTLTYPE_STRING | CTLFLAG_RD,
6966 				sc, 0, ice_sysctl_dump_phy_stats, "A",
6967 				"Dumps PHY statistics from firmware");
6968 	}
6969 
6970 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "local_dcbx_cfg",
6971 			CTLTYPE_STRING | CTLFLAG_RD, sc, ICE_AQ_LLDP_MIB_LOCAL,
6972 			ice_sysctl_dump_dcbx_cfg, "A",
6973 			"Dumps Local MIB information from firmware");
6974 
6975 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "remote_dcbx_cfg",
6976 			CTLTYPE_STRING | CTLFLAG_RD, sc, ICE_AQ_LLDP_MIB_REMOTE,
6977 			ice_sysctl_dump_dcbx_cfg, "A",
6978 			"Dumps Remote MIB information from firmware");
6979 
6980 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "pf_vsi_cfg", CTLTYPE_STRING | CTLFLAG_RD,
6981 			sc, 0, ice_sysctl_dump_vsi_cfg, "A",
6982 			"Dumps Selected PF VSI parameters from firmware");
6983 
6984 	SYSCTL_ADD_PROC(ctx, debug_list, OID_AUTO, "query_port_ets", CTLTYPE_STRING | CTLFLAG_RD,
6985 			sc, 0, ice_sysctl_query_port_ets, "A",
6986 			"Prints selected output from Query Port ETS AQ command");
6987 
6988 	SYSCTL_ADD_U64(ctx, debug_list, OID_AUTO, "rx_length_errors",
6989 		       CTLFLAG_RD | CTLFLAG_STATS, &sc->stats.cur.rx_len_errors, 0,
6990 		       "Receive Length Errors (SNAP packets)");
6991 
6992 	sw_node = SYSCTL_ADD_NODE(ctx, debug_list, OID_AUTO, "switch",
6993 				  ICE_CTLFLAG_DEBUG | CTLFLAG_RD, NULL,
6994 				  "Switch Configuration");
6995 	sw_list = SYSCTL_CHILDREN(sw_node);
6996 
6997 	SYSCTL_ADD_PROC(ctx, sw_list, OID_AUTO, "mac_filters",
6998 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6999 			ice_sysctl_dump_mac_filters, "A",
7000 			"MAC Filters");
7001 
7002 	SYSCTL_ADD_PROC(ctx, sw_list, OID_AUTO, "vlan_filters",
7003 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
7004 			ice_sysctl_dump_vlan_filters, "A",
7005 			"VLAN Filters");
7006 
7007 	SYSCTL_ADD_PROC(ctx, sw_list, OID_AUTO, "ethertype_filters",
7008 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
7009 			ice_sysctl_dump_ethertype_filters, "A",
7010 			"Ethertype Filters");
7011 
7012 	SYSCTL_ADD_PROC(ctx, sw_list, OID_AUTO, "ethertype_mac_filters",
7013 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
7014 			ice_sysctl_dump_ethertype_mac_filters, "A",
7015 			"Ethertype/MAC Filters");
7016 
7017 	dump_node = SYSCTL_ADD_NODE(ctx, debug_list, OID_AUTO, "dump",
7018 				  ICE_CTLFLAG_DEBUG | CTLFLAG_RD, NULL,
7019 				  "Internal FW Dump");
7020 	dump_list = SYSCTL_CHILDREN(dump_node);
7021 
7022 	SYSCTL_ADD_PROC(ctx, dump_list, OID_AUTO, "clusters",
7023 			ICE_CTLFLAG_DEBUG | CTLTYPE_U32 | CTLFLAG_RW, sc, 0,
7024 			ice_sysctl_fw_debug_dump_cluster_setting, "SU",
7025 			ICE_SYSCTL_HELP_FW_DEBUG_DUMP_CLUSTER_SETTING);
7026 
7027 	SYSCTL_ADD_PROC(ctx, dump_list, OID_AUTO, "dump",
7028 			ICE_CTLFLAG_DEBUG | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7029 			ice_sysctl_fw_debug_dump_do_dump, "",
7030 			ICE_SYSCTL_HELP_FW_DEBUG_DUMP_DO_DUMP);
7031 }
7032 
7033 /**
7034  * ice_vsi_disable_tx - Disable (unconfigure) Tx queues for a VSI
7035  * @vsi: the VSI to disable
7036  *
7037  * Disables the Tx queues associated with this VSI. Essentially the opposite
7038  * of ice_cfg_vsi_for_tx.
7039  */
7040 int
ice_vsi_disable_tx(struct ice_vsi * vsi)7041 ice_vsi_disable_tx(struct ice_vsi *vsi)
7042 {
7043 	struct ice_softc *sc = vsi->sc;
7044 	struct ice_hw *hw = &sc->hw;
7045 	int status;
7046 	u32 *q_teids;
7047 	u16 *q_ids, *q_handles;
7048 	size_t q_teids_size, q_ids_size, q_handles_size;
7049 	int tc, j, buf_idx, err = 0;
7050 
7051 	if (vsi->num_tx_queues > 255)
7052 		return (ENOSYS);
7053 
7054 	q_teids_size = sizeof(*q_teids) * vsi->num_tx_queues;
7055 	q_teids = (u32 *)malloc(q_teids_size, M_ICE, M_NOWAIT|M_ZERO);
7056 	if (!q_teids)
7057 		return (ENOMEM);
7058 
7059 	q_ids_size = sizeof(*q_ids) * vsi->num_tx_queues;
7060 	q_ids = (u16 *)malloc(q_ids_size, M_ICE, M_NOWAIT|M_ZERO);
7061 	if (!q_ids) {
7062 		err = (ENOMEM);
7063 		goto free_q_teids;
7064 	}
7065 
7066 	q_handles_size = sizeof(*q_handles) * vsi->num_tx_queues;
7067 	q_handles = (u16 *)malloc(q_handles_size, M_ICE, M_NOWAIT|M_ZERO);
7068 	if (!q_handles) {
7069 		err = (ENOMEM);
7070 		goto free_q_ids;
7071 	}
7072 
7073 	ice_for_each_traffic_class(tc) {
7074 		struct ice_tc_info *tc_info = &vsi->tc_info[tc];
7075 		u16 start_idx, end_idx;
7076 
7077 		/* Skip rest of disabled TCs once the first
7078 		 * disabled TC is found */
7079 		if (!(vsi->tc_map & BIT(tc)))
7080 			break;
7081 
7082 		/* Fill out TX queue information for this TC */
7083 		start_idx = tc_info->qoffset;
7084 		end_idx = start_idx + tc_info->qcount_tx;
7085 		buf_idx = 0;
7086 		for (j = start_idx; j < end_idx; j++) {
7087 			struct ice_tx_queue *txq = &vsi->tx_queues[j];
7088 
7089 			q_ids[buf_idx] = vsi->tx_qmap[j];
7090 			q_handles[buf_idx] = txq->q_handle;
7091 			q_teids[buf_idx] = txq->q_teid;
7092 			buf_idx++;
7093 		}
7094 
7095 		status = ice_dis_vsi_txq(hw->port_info, vsi->idx, tc, buf_idx,
7096 					 q_handles, q_ids, q_teids, ICE_NO_RESET, 0, NULL);
7097 		if (status == ICE_ERR_DOES_NOT_EXIST) {
7098 			; /* Queues have already been disabled, no need to report this as an error */
7099 		} else if (status == ICE_ERR_RESET_ONGOING) {
7100 			device_printf(sc->dev,
7101 				      "Reset in progress. LAN Tx queues already disabled\n");
7102 			break;
7103 		} else if (status) {
7104 			device_printf(sc->dev,
7105 				      "Failed to disable LAN Tx queues: err %s aq_err %s\n",
7106 				      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7107 			err = (ENODEV);
7108 			break;
7109 		}
7110 
7111 		/* Clear buffers */
7112 		memset(q_teids, 0, q_teids_size);
7113 		memset(q_ids, 0, q_ids_size);
7114 		memset(q_handles, 0, q_handles_size);
7115 	}
7116 
7117 /* free_q_handles: */
7118 	free(q_handles, M_ICE);
7119 free_q_ids:
7120 	free(q_ids, M_ICE);
7121 free_q_teids:
7122 	free(q_teids, M_ICE);
7123 
7124 	return err;
7125 }
7126 
7127 /**
7128  * ice_vsi_set_rss_params - Set the RSS parameters for the VSI
7129  * @vsi: the VSI to configure
7130  *
7131  * Sets the RSS table size and lookup table type for the VSI based on its
7132  * VSI type.
7133  */
7134 static void
ice_vsi_set_rss_params(struct ice_vsi * vsi)7135 ice_vsi_set_rss_params(struct ice_vsi *vsi)
7136 {
7137 	struct ice_softc *sc = vsi->sc;
7138 	struct ice_hw_common_caps *cap;
7139 
7140 	cap = &sc->hw.func_caps.common_cap;
7141 
7142 	switch (vsi->type) {
7143 	case ICE_VSI_PF:
7144 		/* The PF VSI inherits RSS instance of the PF */
7145 		vsi->rss_table_size = cap->rss_table_size;
7146 		vsi->rss_lut_type = ICE_LUT_PF;
7147 		break;
7148 	case ICE_VSI_VF:
7149 	case ICE_VSI_VMDQ2:
7150 		vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
7151 		vsi->rss_lut_type = ICE_LUT_VSI;
7152 		break;
7153 	default:
7154 		device_printf(sc->dev,
7155 			      "VSI %d: RSS not supported for VSI type %d\n",
7156 			      vsi->idx, vsi->type);
7157 		break;
7158 	}
7159 }
7160 
7161 /**
7162  * ice_vsi_add_txqs_ctx - Create a sysctl context and node to store txq sysctls
7163  * @vsi: The VSI to add the context for
7164  *
7165  * Creates a sysctl context for storing txq sysctls. Additionally creates
7166  * a node rooted at the given VSI's main sysctl node. This context will be
7167  * used to store per-txq sysctls which may need to be released during the
7168  * driver's lifetime.
7169  */
7170 void
ice_vsi_add_txqs_ctx(struct ice_vsi * vsi)7171 ice_vsi_add_txqs_ctx(struct ice_vsi *vsi)
7172 {
7173 	struct sysctl_oid_list *vsi_list;
7174 
7175 	sysctl_ctx_init(&vsi->txqs_ctx);
7176 
7177 	vsi_list = SYSCTL_CHILDREN(vsi->vsi_node);
7178 
7179 	vsi->txqs_node = SYSCTL_ADD_NODE(&vsi->txqs_ctx, vsi_list, OID_AUTO, "txqs",
7180 					 CTLFLAG_RD, NULL, "Tx Queues");
7181 }
7182 
7183 /**
7184  * ice_vsi_add_rxqs_ctx - Create a sysctl context and node to store rxq sysctls
7185  * @vsi: The VSI to add the context for
7186  *
7187  * Creates a sysctl context for storing rxq sysctls. Additionally creates
7188  * a node rooted at the given VSI's main sysctl node. This context will be
7189  * used to store per-rxq sysctls which may need to be released during the
7190  * driver's lifetime.
7191  */
7192 void
ice_vsi_add_rxqs_ctx(struct ice_vsi * vsi)7193 ice_vsi_add_rxqs_ctx(struct ice_vsi *vsi)
7194 {
7195 	struct sysctl_oid_list *vsi_list;
7196 
7197 	sysctl_ctx_init(&vsi->rxqs_ctx);
7198 
7199 	vsi_list = SYSCTL_CHILDREN(vsi->vsi_node);
7200 
7201 	vsi->rxqs_node = SYSCTL_ADD_NODE(&vsi->rxqs_ctx, vsi_list, OID_AUTO, "rxqs",
7202 					 CTLFLAG_RD, NULL, "Rx Queues");
7203 }
7204 
7205 /**
7206  * ice_vsi_del_txqs_ctx - Delete the Tx queue sysctl context for this VSI
7207  * @vsi: The VSI to delete from
7208  *
7209  * Frees the txq sysctl context created for storing the per-queue Tx sysctls.
7210  * Must be called prior to freeing the Tx queue memory, in order to avoid
7211  * having sysctls point at stale memory.
7212  */
7213 void
ice_vsi_del_txqs_ctx(struct ice_vsi * vsi)7214 ice_vsi_del_txqs_ctx(struct ice_vsi *vsi)
7215 {
7216 	device_t dev = vsi->sc->dev;
7217 	int err;
7218 
7219 	if (vsi->txqs_node) {
7220 		err = sysctl_ctx_free(&vsi->txqs_ctx);
7221 		if (err)
7222 			device_printf(dev, "failed to free VSI %d txqs_ctx, err %s\n",
7223 				      vsi->idx, ice_err_str(err));
7224 		vsi->txqs_node = NULL;
7225 	}
7226 }
7227 
7228 /**
7229  * ice_vsi_del_rxqs_ctx - Delete the Rx queue sysctl context for this VSI
7230  * @vsi: The VSI to delete from
7231  *
7232  * Frees the rxq sysctl context created for storing the per-queue Rx sysctls.
7233  * Must be called prior to freeing the Rx queue memory, in order to avoid
7234  * having sysctls point at stale memory.
7235  */
7236 void
ice_vsi_del_rxqs_ctx(struct ice_vsi * vsi)7237 ice_vsi_del_rxqs_ctx(struct ice_vsi *vsi)
7238 {
7239 	device_t dev = vsi->sc->dev;
7240 	int err;
7241 
7242 	if (vsi->rxqs_node) {
7243 		err = sysctl_ctx_free(&vsi->rxqs_ctx);
7244 		if (err)
7245 			device_printf(dev, "failed to free VSI %d rxqs_ctx, err %s\n",
7246 				      vsi->idx, ice_err_str(err));
7247 		vsi->rxqs_node = NULL;
7248 	}
7249 }
7250 
7251 /**
7252  * ice_add_txq_sysctls - Add per-queue sysctls for a Tx queue
7253  * @txq: pointer to the Tx queue
7254  *
7255 * Add per-queue sysctls for a given Tx queue. Can't be called during
7256 * ice_add_vsi_sysctls, since the queue memory has not yet been setup.
7257  */
7258 void
ice_add_txq_sysctls(struct ice_tx_queue * txq)7259 ice_add_txq_sysctls(struct ice_tx_queue *txq)
7260 {
7261 	struct ice_vsi *vsi = txq->vsi;
7262 	struct sysctl_ctx_list *ctx = &vsi->txqs_ctx;
7263 	struct sysctl_oid_list *txqs_list, *this_txq_list;
7264 	struct sysctl_oid *txq_node;
7265 	char txq_name[32], txq_desc[32];
7266 
7267 	const struct ice_sysctl_info ctls[] = {
7268 		{ &txq->stats.tx_packets, "tx_packets", "Queue Packets Transmitted" },
7269 		{ &txq->stats.tx_bytes, "tx_bytes", "Queue Bytes Transmitted" },
7270 		{ &txq->stats.mss_too_small, "mss_too_small", "TSO sends with an MSS less than 64" },
7271 		{ &txq->stats.tso, "tso", "TSO packets" },
7272 		{ 0, 0, 0 }
7273 	};
7274 
7275 	const struct ice_sysctl_info *entry = ctls;
7276 
7277 	txqs_list = SYSCTL_CHILDREN(vsi->txqs_node);
7278 
7279 	snprintf(txq_name, sizeof(txq_name), "%u", txq->me);
7280 	snprintf(txq_desc, sizeof(txq_desc), "Tx Queue %u", txq->me);
7281 	txq_node = SYSCTL_ADD_NODE(ctx, txqs_list, OID_AUTO, txq_name,
7282 				   CTLFLAG_RD, NULL, txq_desc);
7283 	this_txq_list = SYSCTL_CHILDREN(txq_node);
7284 
7285 	/* Add the Tx queue statistics */
7286 	while (entry->stat != 0) {
7287 		SYSCTL_ADD_U64(ctx, this_txq_list, OID_AUTO, entry->name,
7288 			       CTLFLAG_RD | CTLFLAG_STATS, entry->stat, 0,
7289 			       entry->description);
7290 		entry++;
7291 	}
7292 
7293 	SYSCTL_ADD_U8(ctx, this_txq_list, OID_AUTO, "tc",
7294 		       CTLFLAG_RD, &txq->tc, 0,
7295 		       "Traffic Class that Queue belongs to");
7296 }
7297 
7298 /**
7299  * ice_add_rxq_sysctls - Add per-queue sysctls for an Rx queue
7300  * @rxq: pointer to the Rx queue
7301  *
7302  * Add per-queue sysctls for a given Rx queue. Can't be called during
7303  * ice_add_vsi_sysctls, since the queue memory has not yet been setup.
7304  */
7305 void
ice_add_rxq_sysctls(struct ice_rx_queue * rxq)7306 ice_add_rxq_sysctls(struct ice_rx_queue *rxq)
7307 {
7308 	struct ice_vsi *vsi = rxq->vsi;
7309 	struct sysctl_ctx_list *ctx = &vsi->rxqs_ctx;
7310 	struct sysctl_oid_list *rxqs_list, *this_rxq_list;
7311 	struct sysctl_oid *rxq_node;
7312 	char rxq_name[32], rxq_desc[32];
7313 
7314 	const struct ice_sysctl_info ctls[] = {
7315 		{ &rxq->stats.rx_packets, "rx_packets", "Queue Packets Received" },
7316 		{ &rxq->stats.rx_bytes, "rx_bytes", "Queue Bytes Received" },
7317 		{ &rxq->stats.desc_errs, "rx_desc_errs", "Queue Rx Descriptor Errors" },
7318 		{ 0, 0, 0 }
7319 	};
7320 
7321 	const struct ice_sysctl_info *entry = ctls;
7322 
7323 	rxqs_list = SYSCTL_CHILDREN(vsi->rxqs_node);
7324 
7325 	snprintf(rxq_name, sizeof(rxq_name), "%u", rxq->me);
7326 	snprintf(rxq_desc, sizeof(rxq_desc), "Rx Queue %u", rxq->me);
7327 	rxq_node = SYSCTL_ADD_NODE(ctx, rxqs_list, OID_AUTO, rxq_name,
7328 				   CTLFLAG_RD, NULL, rxq_desc);
7329 	this_rxq_list = SYSCTL_CHILDREN(rxq_node);
7330 
7331 	/* Add the Rx queue statistics */
7332 	while (entry->stat != 0) {
7333 		SYSCTL_ADD_U64(ctx, this_rxq_list, OID_AUTO, entry->name,
7334 			       CTLFLAG_RD | CTLFLAG_STATS, entry->stat, 0,
7335 			       entry->description);
7336 		entry++;
7337 	}
7338 
7339 	SYSCTL_ADD_U8(ctx, this_rxq_list, OID_AUTO, "tc",
7340 		       CTLFLAG_RD, &rxq->tc, 0,
7341 		       "Traffic Class that Queue belongs to");
7342 }
7343 
7344 /**
7345  * ice_get_default_rss_key - Obtain a default RSS key
7346  * @seed: storage for the RSS key data
7347  *
7348  * Copies a pre-generated RSS key into the seed memory. The seed pointer must
7349  * point to a block of memory that is at least 40 bytes in size.
7350  *
7351  * The key isn't randomly generated each time this function is called because
7352  * that makes the RSS key change every time we reconfigure RSS. This does mean
7353  * that we're hard coding a possibly 'well known' key. We might want to
7354  * investigate randomly generating this key once during the first call.
7355  */
7356 static void
ice_get_default_rss_key(u8 * seed)7357 ice_get_default_rss_key(u8 *seed)
7358 {
7359 	const u8 default_seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE] = {
7360 		0x39, 0xed, 0xff, 0x4d, 0x43, 0x58, 0x42, 0xc3, 0x5f, 0xb8,
7361 		0xa5, 0x32, 0x95, 0x65, 0x81, 0xcd, 0x36, 0x79, 0x71, 0x97,
7362 		0xde, 0xa4, 0x41, 0x40, 0x6f, 0x27, 0xe9, 0x81, 0x13, 0xa0,
7363 		0x95, 0x93, 0x5b, 0x1e, 0x9d, 0x27, 0x9d, 0x24, 0x84, 0xb5,
7364 	};
7365 
7366 	bcopy(default_seed, seed, ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
7367 }
7368 
7369 /**
7370  * ice_set_rss_key - Configure a given VSI with the default RSS key
7371  * @vsi: the VSI to configure
7372  *
7373  * Program the hardware RSS key. We use rss_getkey to grab the kernel RSS key.
7374  * If the kernel RSS interface is not available, this will fall back to our
7375  * pre-generated hash seed from ice_get_default_rss_key().
7376  */
7377 static int
ice_set_rss_key(struct ice_vsi * vsi)7378 ice_set_rss_key(struct ice_vsi *vsi)
7379 {
7380 	struct ice_aqc_get_set_rss_keys keydata = { .standard_rss_key = {0} };
7381 	struct ice_softc *sc = vsi->sc;
7382 	struct ice_hw *hw = &sc->hw;
7383 	int status;
7384 
7385 	/*
7386 	 * If the RSS kernel interface is disabled, this will return the
7387 	 * default RSS key above.
7388 	 */
7389 	rss_getkey(keydata.standard_rss_key);
7390 
7391 	status = ice_aq_set_rss_key(hw, vsi->idx, &keydata);
7392 	if (status) {
7393 		device_printf(sc->dev,
7394 			      "ice_aq_set_rss_key status %s, error %s\n",
7395 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7396 		return (EIO);
7397 	}
7398 
7399 	return (0);
7400 }
7401 
7402 /**
7403  * ice_set_rss_flow_flds - Program the RSS hash flows after package init
7404  * @vsi: the VSI to configure
7405  *
7406  * If the package file is initialized, the default RSS flows are reset. We
7407  * need to reprogram the expected hash configuration. We'll use
7408  * rss_gethashconfig() to determine which flows to enable. If RSS kernel
7409  * support is not enabled, this macro will fall back to suitable defaults.
7410  */
7411 static void
ice_set_rss_flow_flds(struct ice_vsi * vsi)7412 ice_set_rss_flow_flds(struct ice_vsi *vsi)
7413 {
7414 	struct ice_softc *sc = vsi->sc;
7415 	struct ice_hw *hw = &sc->hw;
7416 	struct ice_rss_hash_cfg rss_cfg = { 0, 0, ICE_RSS_ANY_HEADERS, false };
7417 	device_t dev = sc->dev;
7418 	int status;
7419 	u_int rss_hash_config;
7420 
7421 	rss_hash_config = rss_gethashconfig();
7422 
7423 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4) {
7424 		rss_cfg.addl_hdrs = ICE_FLOW_SEG_HDR_IPV4;
7425 		rss_cfg.hash_flds = ICE_FLOW_HASH_IPV4;
7426 		status = ice_add_rss_cfg(hw, vsi->idx, &rss_cfg);
7427 		if (status)
7428 			device_printf(dev,
7429 				      "ice_add_rss_cfg on VSI %d failed for ipv4 flow, err %s aq_err %s\n",
7430 				      vsi->idx, ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7431 	}
7432 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4) {
7433 		rss_cfg.addl_hdrs = ICE_FLOW_SEG_HDR_IPV4 | ICE_FLOW_SEG_HDR_TCP;
7434 		rss_cfg.hash_flds = ICE_HASH_TCP_IPV4;
7435 		status = ice_add_rss_cfg(hw, vsi->idx, &rss_cfg);
7436 		if (status)
7437 			device_printf(dev,
7438 				      "ice_add_rss_cfg on VSI %d failed for tcp4 flow, err %s aq_err %s\n",
7439 				      vsi->idx, ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7440 	}
7441 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4) {
7442 		rss_cfg.addl_hdrs = ICE_FLOW_SEG_HDR_IPV4 | ICE_FLOW_SEG_HDR_UDP;
7443 		rss_cfg.hash_flds = ICE_HASH_UDP_IPV4;
7444 		status = ice_add_rss_cfg(hw, vsi->idx, &rss_cfg);
7445 		if (status)
7446 			device_printf(dev,
7447 				      "ice_add_rss_cfg on VSI %d failed for udp4 flow, err %s aq_err %s\n",
7448 				      vsi->idx, ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7449 	}
7450 	if (rss_hash_config & (RSS_HASHTYPE_RSS_IPV6 | RSS_HASHTYPE_RSS_IPV6_EX)) {
7451 		rss_cfg.addl_hdrs = ICE_FLOW_SEG_HDR_IPV6;
7452 		rss_cfg.hash_flds = ICE_FLOW_HASH_IPV6;
7453 		status = ice_add_rss_cfg(hw, vsi->idx, &rss_cfg);
7454 		if (status)
7455 			device_printf(dev,
7456 				      "ice_add_rss_cfg on VSI %d failed for ipv6 flow, err %s aq_err %s\n",
7457 				      vsi->idx, ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7458 	}
7459 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6) {
7460 		rss_cfg.addl_hdrs = ICE_FLOW_SEG_HDR_IPV6 | ICE_FLOW_SEG_HDR_TCP;
7461 		rss_cfg.hash_flds = ICE_HASH_TCP_IPV6;
7462 		status = ice_add_rss_cfg(hw, vsi->idx, &rss_cfg);
7463 		if (status)
7464 			device_printf(dev,
7465 				      "ice_add_rss_cfg on VSI %d failed for tcp6 flow, err %s aq_err %s\n",
7466 				      vsi->idx, ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7467 	}
7468 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6) {
7469 		rss_cfg.addl_hdrs = ICE_FLOW_SEG_HDR_IPV6 | ICE_FLOW_SEG_HDR_UDP;
7470 		rss_cfg.hash_flds = ICE_HASH_UDP_IPV6;
7471 		status = ice_add_rss_cfg(hw, vsi->idx, &rss_cfg);
7472 		if (status)
7473 			device_printf(dev,
7474 				      "ice_add_rss_cfg on VSI %d failed for udp6 flow, err %s aq_err %s\n",
7475 				      vsi->idx, ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7476 	}
7477 
7478 	/* Warn about RSS hash types which are not supported */
7479 	/* coverity[dead_error_condition] */
7480 	if (rss_hash_config & ~ICE_DEFAULT_RSS_HASH_CONFIG) {
7481 		device_printf(dev,
7482 			      "ice_add_rss_cfg on VSI %d could not configure every requested hash type\n",
7483 			      vsi->idx);
7484 	}
7485 }
7486 
7487 /**
7488  * ice_set_rss_lut - Program the RSS lookup table for a VSI
7489  * @vsi: the VSI to configure
7490  *
7491  * Programs the RSS lookup table for a given VSI. We use
7492  * rss_get_indirection_to_bucket which will use the indirection table provided
7493  * by the kernel RSS interface when available. If the kernel RSS interface is
7494  * not available, we will fall back to a simple round-robin fashion queue
7495  * assignment.
7496  */
7497 static int
ice_set_rss_lut(struct ice_vsi * vsi)7498 ice_set_rss_lut(struct ice_vsi *vsi)
7499 {
7500 	struct ice_softc *sc = vsi->sc;
7501 	struct ice_hw *hw = &sc->hw;
7502 	device_t dev = sc->dev;
7503 	struct ice_aq_get_set_rss_lut_params lut_params;
7504 	int status;
7505 	int i, err = 0;
7506 	u8 *lut;
7507 
7508 	lut = (u8 *)malloc(vsi->rss_table_size, M_ICE, M_NOWAIT|M_ZERO);
7509 	if (!lut) {
7510 		device_printf(dev, "Failed to allocate RSS lut memory\n");
7511 		return (ENOMEM);
7512 	}
7513 
7514 	/* Populate the LUT with max no. of queues. If the RSS kernel
7515 	 * interface is disabled, this will assign the lookup table in
7516 	 * a simple round robin fashion
7517 	 */
7518 	for (i = 0; i < vsi->rss_table_size; i++) {
7519 		/* XXX: this needs to be changed if num_rx_queues ever counts
7520 		 * more than just the RSS queues */
7521 		lut[i] = rss_get_indirection_to_bucket(i) % vsi->num_rx_queues;
7522 	}
7523 
7524 	lut_params.vsi_handle = vsi->idx;
7525 	lut_params.lut_size = vsi->rss_table_size;
7526 	lut_params.lut_type = vsi->rss_lut_type;
7527 	lut_params.lut = lut;
7528 	lut_params.global_lut_id = 0;
7529 	status = ice_aq_set_rss_lut(hw, &lut_params);
7530 	if (status) {
7531 		device_printf(dev,
7532 			      "Cannot set RSS lut, err %s aq_err %s\n",
7533 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
7534 		err = (EIO);
7535 	}
7536 
7537 	free(lut, M_ICE);
7538 	return err;
7539 }
7540 
7541 /**
7542  * ice_config_rss - Configure RSS for a VSI
7543  * @vsi: the VSI to configure
7544  *
7545  * If FEATURE_RSS is enabled, configures the RSS lookup table and hash key for
7546  * a given VSI.
7547  */
7548 int
ice_config_rss(struct ice_vsi * vsi)7549 ice_config_rss(struct ice_vsi *vsi)
7550 {
7551 	int err;
7552 
7553 	/* Nothing to do, if RSS is not enabled */
7554 	if (!ice_is_bit_set(vsi->sc->feat_en, ICE_FEATURE_RSS))
7555 		return 0;
7556 
7557 	err = ice_set_rss_key(vsi);
7558 	if (err)
7559 		return err;
7560 
7561 	ice_set_rss_flow_flds(vsi);
7562 
7563 	return ice_set_rss_lut(vsi);
7564 }
7565 
7566 /**
7567  * ice_log_pkg_init - Log a message about status of DDP initialization
7568  * @sc: the device softc pointer
7569  * @pkg_status: the status result of ice_copy_and_init_pkg
7570  *
7571  * Called by ice_load_pkg after an attempt to download the DDP package
7572  * contents to the device to log an appropriate message for the system
7573  * administrator about download status.
7574  *
7575  * @post ice_is_init_pkg_successful function is used to determine
7576  * whether the download was successful and DDP package is compatible
7577  * with this driver. Otherwise driver will transition to Safe Mode.
7578  */
7579 void
ice_log_pkg_init(struct ice_softc * sc,enum ice_ddp_state pkg_status)7580 ice_log_pkg_init(struct ice_softc *sc, enum ice_ddp_state pkg_status)
7581 {
7582 	struct ice_hw *hw = &sc->hw;
7583 	device_t dev = sc->dev;
7584 	struct sbuf *active_pkg, *os_pkg;
7585 
7586 	active_pkg = sbuf_new_auto();
7587 	ice_active_pkg_version_str(hw, active_pkg);
7588 	sbuf_finish(active_pkg);
7589 
7590 	os_pkg = sbuf_new_auto();
7591 	ice_os_pkg_version_str(hw, os_pkg);
7592 	sbuf_finish(os_pkg);
7593 
7594 	switch (pkg_status) {
7595 	case ICE_DDP_PKG_SUCCESS:
7596 		device_printf(dev,
7597 			      "The DDP package was successfully loaded: %s.\n",
7598 			      sbuf_data(active_pkg));
7599 		break;
7600 	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
7601 	case ICE_DDP_PKG_ALREADY_LOADED:
7602 		device_printf(dev,
7603 			      "DDP package already present on device: %s.\n",
7604 			      sbuf_data(active_pkg));
7605 		break;
7606 	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
7607 		device_printf(dev,
7608 			      "The driver could not load the DDP package file because a compatible DDP package is already present on the device.  The device has package %s.  The ice_ddp module has package: %s.\n",
7609 			      sbuf_data(active_pkg),
7610 			      sbuf_data(os_pkg));
7611 		break;
7612 	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
7613 		device_printf(dev,
7614 			      "The device has a DDP package that is higher than the driver supports.  The device has package %s.  The driver requires version %d.%d.x.x.  Entering Safe Mode.\n",
7615 			      sbuf_data(active_pkg),
7616 			      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7617 		break;
7618 	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
7619 		device_printf(dev,
7620 			      "The device has a DDP package that is lower than the driver supports.  The device has package %s.  The driver requires version %d.%d.x.x.  Entering Safe Mode.\n",
7621 			      sbuf_data(active_pkg),
7622 			      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7623 		break;
7624 	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
7625 		/*
7626 		 * This assumes that the active_pkg_ver will not be
7627 		 * initialized if the ice_ddp package version is not
7628 		 * supported.
7629 		 */
7630 		if (pkg_ver_empty(&hw->active_pkg_ver, hw->active_pkg_name)) {
7631 			/* The ice_ddp version is not supported */
7632 			if (pkg_ver_compatible(&hw->pkg_ver) > 0) {
7633 				device_printf(dev,
7634 					      "The DDP package in the ice_ddp module is higher than the driver supports.  The ice_ddp module has package %s.  The driver requires version %d.%d.x.x.  Please use an updated driver.  Entering Safe Mode.\n",
7635 					      sbuf_data(os_pkg),
7636 					      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7637 			} else if (pkg_ver_compatible(&hw->pkg_ver) < 0) {
7638 				device_printf(dev,
7639 					      "The DDP package in the ice_ddp module is lower than the driver supports.  The ice_ddp module has package %s.  The driver requires version %d.%d.x.x.  Please use an updated ice_ddp module.  Entering Safe Mode.\n",
7640 					      sbuf_data(os_pkg),
7641 					      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7642 			} else {
7643 				device_printf(dev,
7644 					      "An unknown error occurred when loading the DDP package.  The ice_ddp module has package %s.  The device has package %s.  The driver requires version %d.%d.x.x.  Entering Safe Mode.\n",
7645 					      sbuf_data(os_pkg),
7646 					      sbuf_data(active_pkg),
7647 					      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7648 			}
7649 		} else {
7650 			if (pkg_ver_compatible(&hw->active_pkg_ver) > 0) {
7651 				device_printf(dev,
7652 					      "The device has a DDP package that is higher than the driver supports.  The device has package %s.  The driver requires version %d.%d.x.x.  Entering Safe Mode.\n",
7653 					      sbuf_data(active_pkg),
7654 					      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7655 			} else if (pkg_ver_compatible(&hw->active_pkg_ver) < 0) {
7656 				device_printf(dev,
7657 					      "The device has a DDP package that is lower than the driver supports.  The device has package %s.  The driver requires version %d.%d.x.x.  Entering Safe Mode.\n",
7658 					      sbuf_data(active_pkg),
7659 					      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7660 			} else {
7661 				device_printf(dev,
7662 					      "An unknown error occurred when loading the DDP package.  The ice_ddp module has package %s.  The device has package %s.  The driver requires version %d.%d.x.x.  Entering Safe Mode.\n",
7663 					      sbuf_data(os_pkg),
7664 					      sbuf_data(active_pkg),
7665 					      ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
7666 			}
7667 		}
7668 		break;
7669 	case ICE_DDP_PKG_INVALID_FILE:
7670 		device_printf(dev,
7671 			      "The DDP package in the ice_ddp module is invalid.  Entering Safe Mode\n");
7672 		break;
7673 	case ICE_DDP_PKG_FW_MISMATCH:
7674 		device_printf(dev,
7675 			      "The firmware loaded on the device is not compatible with the DDP package.  Please update the device's NVM.  Entering safe mode.\n");
7676 		break;
7677 	case ICE_DDP_PKG_NO_SEC_MANIFEST:
7678 	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
7679 		device_printf(dev,
7680 			      "The DDP package in the ice_ddp module cannot be loaded because its signature is not valid.  Please use a valid ice_ddp module.  Entering Safe Mode.\n");
7681 		break;
7682 	case ICE_DDP_PKG_SECURE_VERSION_NBR_TOO_LOW:
7683 		device_printf(dev,
7684 			      "The DDP package in the ice_ddp module could not be loaded because its security revision is too low.  Please use an updated ice_ddp module.  Entering Safe Mode.\n");
7685 		break;
7686 	case ICE_DDP_PKG_MANIFEST_INVALID:
7687 	case ICE_DDP_PKG_BUFFER_INVALID:
7688 		device_printf(dev,
7689 			      "An error occurred on the device while loading the DDP package.  Entering Safe Mode.\n");
7690 		break;
7691 	default:
7692 		device_printf(dev,
7693 			 "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
7694 		break;
7695 	}
7696 
7697 	sbuf_delete(active_pkg);
7698 	sbuf_delete(os_pkg);
7699 }
7700 
7701 /**
7702  * ice_load_pkg_file - Load the DDP package file using firmware_get
7703  * @sc: device private softc
7704  *
7705  * Use firmware_get to load the DDP package memory and then request that
7706  * firmware download the package contents and program the relevant hardware
7707  * bits.
7708  *
7709  * This function makes a copy of the DDP package memory which is tracked in
7710  * the ice_hw structure. The copy will be managed and released by
7711  * ice_deinit_hw(). This allows the firmware reference to be immediately
7712  * released using firmware_put.
7713  */
7714 int
ice_load_pkg_file(struct ice_softc * sc)7715 ice_load_pkg_file(struct ice_softc *sc)
7716 {
7717 	struct ice_hw *hw = &sc->hw;
7718 	device_t dev = sc->dev;
7719 	enum ice_ddp_state state;
7720 	const struct firmware *pkg;
7721 	int status = 0;
7722 	u8 cached_layer_count;
7723 	u8 *buf_copy;
7724 
7725 	pkg = firmware_get("ice_ddp");
7726 	if (!pkg) {
7727 		device_printf(dev,
7728 		    "The DDP package module (ice_ddp) failed to load or could not be found. Entering Safe Mode.\n");
7729 		if (cold)
7730 			device_printf(dev,
7731 			    "The DDP package module cannot be automatically loaded while booting. You may want to specify ice_ddp_load=\"YES\" in your loader.conf\n");
7732 		status = ICE_ERR_CFG;
7733 		goto err_load_pkg;
7734 	}
7735 
7736 	/* Check for topology change */
7737 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_TX_BALANCE)) {
7738 		cached_layer_count = hw->num_tx_sched_layers;
7739 		buf_copy = (u8 *)malloc(pkg->datasize, M_ICE, M_NOWAIT);
7740 		if (buf_copy == NULL)
7741 			return ICE_ERR_NO_MEMORY;
7742 		memcpy(buf_copy, pkg->data, pkg->datasize);
7743 		status = ice_cfg_tx_topo(&sc->hw, buf_copy, pkg->datasize);
7744 		free(buf_copy, M_ICE);
7745 		/* Success indicates a change was made */
7746 		if (!status) {
7747 			/* 9 -> 5 */
7748 			if (cached_layer_count == 9)
7749 				device_printf(dev,
7750 				    "Transmit balancing feature enabled\n");
7751 			else
7752 				device_printf(dev,
7753 				    "Transmit balancing feature disabled\n");
7754 			ice_set_bit(ICE_FEATURE_TX_BALANCE, sc->feat_en);
7755 			return (status);
7756 		} else if (status == ICE_ERR_CFG) {
7757 			/* Status is ICE_ERR_CFG when DDP does not support transmit balancing */
7758 			device_printf(dev,
7759 			    "DDP package does not support transmit balancing feature - please update to the latest DDP package and try again\n");
7760 		} else if (status == ICE_ERR_ALREADY_EXISTS) {
7761 			/* Requested config already loaded */
7762 		} else if (status == ICE_ERR_AQ_ERROR) {
7763 			device_printf(dev,
7764 			    "Error configuring transmit balancing: %s\n",
7765 			    ice_status_str(status));
7766 		}
7767 	}
7768 
7769 	/* Copy and download the pkg contents */
7770 	state = ice_copy_and_init_pkg(hw, (const u8 *)pkg->data, pkg->datasize);
7771 
7772 	/* Release the firmware reference */
7773 	firmware_put(pkg, FIRMWARE_UNLOAD);
7774 
7775 	/* Check the active DDP package version and log a message */
7776 	ice_log_pkg_init(sc, state);
7777 
7778 	/* Place the driver into safe mode */
7779 	if (ice_is_init_pkg_successful(state))
7780 		return (ICE_ERR_ALREADY_EXISTS);
7781 
7782 err_load_pkg:
7783 	ice_zero_bitmap(sc->feat_cap, ICE_FEATURE_COUNT);
7784 	ice_zero_bitmap(sc->feat_en, ICE_FEATURE_COUNT);
7785 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_cap);
7786 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_en);
7787 
7788 	return (status);
7789 }
7790 
7791 /**
7792  * ice_get_ifnet_counter - Retrieve counter value for a given ifnet counter
7793  * @vsi: the vsi to retrieve the value for
7794  * @counter: the counter type to retrieve
7795  *
7796  * Returns the value for a given ifnet counter. To do so, we calculate the
7797  * value based on the matching hardware statistics.
7798  */
7799 uint64_t
ice_get_ifnet_counter(struct ice_vsi * vsi,ift_counter counter)7800 ice_get_ifnet_counter(struct ice_vsi *vsi, ift_counter counter)
7801 {
7802 	struct ice_hw_port_stats *hs = &vsi->sc->stats.cur;
7803 	struct ice_eth_stats *es = &vsi->hw_stats.cur;
7804 
7805 	/* For some statistics, especially those related to error flows, we do
7806 	 * not have per-VSI counters. In this case, we just report the global
7807 	 * counters.
7808 	 */
7809 
7810 	switch (counter) {
7811 	case IFCOUNTER_IPACKETS:
7812 		return (es->rx_unicast + es->rx_multicast + es->rx_broadcast);
7813 	case IFCOUNTER_IERRORS:
7814 		return (hs->crc_errors + hs->illegal_bytes +
7815 			hs->mac_local_faults + hs->mac_remote_faults +
7816 			hs->rx_undersize + hs->rx_oversize + hs->rx_fragments +
7817 			hs->rx_jabber);
7818 	case IFCOUNTER_OPACKETS:
7819 		return (es->tx_unicast + es->tx_multicast + es->tx_broadcast);
7820 	case IFCOUNTER_OERRORS:
7821 		return (if_get_counter_default(vsi->sc->ifp, counter) +
7822 		    es->tx_errors);
7823 	case IFCOUNTER_COLLISIONS:
7824 		return (0);
7825 	case IFCOUNTER_IBYTES:
7826 		return (es->rx_bytes);
7827 	case IFCOUNTER_OBYTES:
7828 		return (es->tx_bytes);
7829 	case IFCOUNTER_IMCASTS:
7830 		return (es->rx_multicast);
7831 	case IFCOUNTER_OMCASTS:
7832 		return (es->tx_multicast);
7833 	case IFCOUNTER_IQDROPS:
7834 		return (es->rx_discards);
7835 	case IFCOUNTER_OQDROPS:
7836 		return (if_get_counter_default(vsi->sc->ifp, counter) +
7837 		    hs->tx_dropped_link_down);
7838 	case IFCOUNTER_NOPROTO:
7839 		return (es->rx_unknown_protocol);
7840 	default:
7841 		return if_get_counter_default(vsi->sc->ifp, counter);
7842 	}
7843 }
7844 
7845 /**
7846  * ice_save_pci_info - Save PCI configuration fields in HW struct
7847  * @hw: the ice_hw struct to save the PCI information in
7848  * @dev: the device to get the PCI information from
7849  *
7850  * This should only be called once, early in the device attach
7851  * process.
7852  */
7853 void
ice_save_pci_info(struct ice_hw * hw,device_t dev)7854 ice_save_pci_info(struct ice_hw *hw, device_t dev)
7855 {
7856 	hw->vendor_id = pci_get_vendor(dev);
7857 	hw->device_id = pci_get_device(dev);
7858 	hw->subsystem_vendor_id = pci_get_subvendor(dev);
7859 	hw->subsystem_device_id = pci_get_subdevice(dev);
7860 	hw->revision_id = pci_get_revid(dev);
7861 	hw->bus.device = pci_get_slot(dev);
7862 	hw->bus.func = pci_get_function(dev);
7863 }
7864 
7865 /**
7866  * ice_replay_all_vsi_cfg - Replace configuration for all VSIs after reset
7867  * @sc: the device softc
7868  *
7869  * Replace the configuration for each VSI, and then cleanup replay
7870  * information. Called after a hardware reset in order to reconfigure the
7871  * active VSIs.
7872  */
7873 int
ice_replay_all_vsi_cfg(struct ice_softc * sc)7874 ice_replay_all_vsi_cfg(struct ice_softc *sc)
7875 {
7876 	struct ice_hw *hw = &sc->hw;
7877 	int status;
7878 	int i;
7879 
7880 	for (i = 0 ; i < sc->num_available_vsi; i++) {
7881 		struct ice_vsi *vsi = sc->all_vsi[i];
7882 
7883 		if (!vsi)
7884 			continue;
7885 
7886 		status = ice_replay_vsi(hw, vsi->idx);
7887 		if (status) {
7888 			device_printf(sc->dev, "Failed to replay VSI %d, err %s aq_err %s\n",
7889 				      vsi->idx, ice_status_str(status),
7890 				      ice_aq_str(hw->adminq.sq_last_status));
7891 			return (EIO);
7892 		}
7893 	}
7894 
7895 	/* Cleanup replay filters after successful reconfiguration */
7896 	ice_replay_post(hw);
7897 	return (0);
7898 }
7899 
7900 /**
7901  * ice_clean_vsi_rss_cfg - Cleanup RSS configuration for a given VSI
7902  * @vsi: pointer to the VSI structure
7903  *
7904  * Cleanup the advanced RSS configuration for a given VSI. This is necessary
7905  * during driver removal to ensure that all RSS resources are properly
7906  * released.
7907  *
7908  * @remark this function doesn't report an error as it is expected to be
7909  * called during driver reset and unload, and there isn't much the driver can
7910  * do if freeing RSS resources fails.
7911  */
7912 static void
ice_clean_vsi_rss_cfg(struct ice_vsi * vsi)7913 ice_clean_vsi_rss_cfg(struct ice_vsi *vsi)
7914 {
7915 	struct ice_softc *sc = vsi->sc;
7916 	struct ice_hw *hw = &sc->hw;
7917 	device_t dev = sc->dev;
7918 	int status;
7919 
7920 	status = ice_rem_vsi_rss_cfg(hw, vsi->idx);
7921 	if (status)
7922 		device_printf(dev,
7923 			      "Failed to remove RSS configuration for VSI %d, err %s\n",
7924 			      vsi->idx, ice_status_str(status));
7925 
7926 	/* Remove this VSI from the RSS list */
7927 	ice_rem_vsi_rss_list(hw, vsi->idx);
7928 }
7929 
7930 /**
7931  * ice_clean_all_vsi_rss_cfg - Cleanup RSS configuration for all VSIs
7932  * @sc: the device softc pointer
7933  *
7934  * Cleanup the advanced RSS configuration for all VSIs on a given PF
7935  * interface.
7936  *
7937  * @remark This should be called while preparing for a reset, to cleanup stale
7938  * RSS configuration for all VSIs.
7939  */
7940 void
ice_clean_all_vsi_rss_cfg(struct ice_softc * sc)7941 ice_clean_all_vsi_rss_cfg(struct ice_softc *sc)
7942 {
7943 	int i;
7944 
7945 	/* No need to cleanup if RSS is not enabled */
7946 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_RSS))
7947 		return;
7948 
7949 	for (i = 0; i < sc->num_available_vsi; i++) {
7950 		struct ice_vsi *vsi = sc->all_vsi[i];
7951 
7952 		if (vsi)
7953 			ice_clean_vsi_rss_cfg(vsi);
7954 	}
7955 }
7956 
7957 /**
7958  * ice_requested_fec_mode - Return the requested FEC mode as a string
7959  * @pi: The port info structure
7960  *
7961  * Return a string representing the requested FEC mode.
7962  */
7963 static const char *
ice_requested_fec_mode(struct ice_port_info * pi)7964 ice_requested_fec_mode(struct ice_port_info *pi)
7965 {
7966 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
7967 	int status;
7968 
7969 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG,
7970 				     &pcaps, NULL);
7971 	if (status)
7972 		/* Just report unknown if we can't get capabilities */
7973 		return "Unknown";
7974 
7975 	/* Check if RS-FEC has been requested first */
7976 	if (pcaps.link_fec_options & (ICE_AQC_PHY_FEC_25G_RS_528_REQ |
7977 				      ICE_AQC_PHY_FEC_25G_RS_544_REQ))
7978 		return ice_fec_str(ICE_FEC_RS);
7979 
7980 	/* If RS FEC has not been requested, then check BASE-R */
7981 	if (pcaps.link_fec_options & (ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ |
7982 				      ICE_AQC_PHY_FEC_25G_KR_REQ))
7983 		return ice_fec_str(ICE_FEC_BASER);
7984 
7985 	return ice_fec_str(ICE_FEC_NONE);
7986 }
7987 
7988 /**
7989  * ice_negotiated_fec_mode - Return the negotiated FEC mode as a string
7990  * @pi: The port info structure
7991  *
7992  * Return a string representing the current FEC mode.
7993  */
7994 static const char *
ice_negotiated_fec_mode(struct ice_port_info * pi)7995 ice_negotiated_fec_mode(struct ice_port_info *pi)
7996 {
7997 	/* First, check if RS has been requested first */
7998 	if (pi->phy.link_info.fec_info & (ICE_AQ_LINK_25G_RS_528_FEC_EN |
7999 					  ICE_AQ_LINK_25G_RS_544_FEC_EN))
8000 		return ice_fec_str(ICE_FEC_RS);
8001 
8002 	/* If RS FEC has not been requested, then check BASE-R */
8003 	if (pi->phy.link_info.fec_info & ICE_AQ_LINK_25G_KR_FEC_EN)
8004 		return ice_fec_str(ICE_FEC_BASER);
8005 
8006 	return ice_fec_str(ICE_FEC_NONE);
8007 }
8008 
8009 /**
8010  * ice_autoneg_mode - Return string indicating of autoneg completed
8011  * @pi: The port info structure
8012  *
8013  * Return "True" if autonegotiation is completed, "False" otherwise.
8014  */
8015 static const char *
ice_autoneg_mode(struct ice_port_info * pi)8016 ice_autoneg_mode(struct ice_port_info *pi)
8017 {
8018 	if (pi->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
8019 		return "True";
8020 	else
8021 		return "False";
8022 }
8023 
8024 /**
8025  * ice_flowcontrol_mode - Return string indicating the Flow Control mode
8026  * @pi: The port info structure
8027  *
8028  * Returns the current Flow Control mode as a string.
8029  */
8030 static const char *
ice_flowcontrol_mode(struct ice_port_info * pi)8031 ice_flowcontrol_mode(struct ice_port_info *pi)
8032 {
8033 	return ice_fc_str(pi->fc.current_mode);
8034 }
8035 
8036 /**
8037  * ice_link_up_msg - Log a link up message with associated info
8038  * @sc: the device private softc
8039  *
8040  * Log a link up message with LOG_NOTICE message level. Include information
8041  * about the duplex, FEC mode, autonegotiation and flow control.
8042  */
8043 void
ice_link_up_msg(struct ice_softc * sc)8044 ice_link_up_msg(struct ice_softc *sc)
8045 {
8046 	struct ice_hw *hw = &sc->hw;
8047 	struct ifnet *ifp = sc->ifp;
8048 	const char *speed, *req_fec, *neg_fec, *autoneg, *flowcontrol;
8049 
8050 	speed = ice_aq_speed_to_str(hw->port_info);
8051 	req_fec = ice_requested_fec_mode(hw->port_info);
8052 	neg_fec = ice_negotiated_fec_mode(hw->port_info);
8053 	autoneg = ice_autoneg_mode(hw->port_info);
8054 	flowcontrol = ice_flowcontrol_mode(hw->port_info);
8055 
8056 	log(LOG_NOTICE, "%s: Link is up, %s Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
8057 	    if_name(ifp), speed, req_fec, neg_fec, autoneg, flowcontrol);
8058 }
8059 
8060 /**
8061  * ice_update_laa_mac - Update MAC address if Locally Administered
8062  * @sc: the device softc
8063  *
8064  * Update the device MAC address when a Locally Administered Address is
8065  * assigned.
8066  *
8067  * This function does *not* update the MAC filter list itself. Instead, it
8068  * should be called after ice_rm_pf_default_mac_filters, so that the previous
8069  * address filter will be removed, and before ice_cfg_pf_default_mac_filters,
8070  * so that the new address filter will be assigned.
8071  */
8072 int
ice_update_laa_mac(struct ice_softc * sc)8073 ice_update_laa_mac(struct ice_softc *sc)
8074 {
8075 	const u8 *lladdr = (const u8 *)if_getlladdr(sc->ifp);
8076 	struct ice_hw *hw = &sc->hw;
8077 	int status;
8078 
8079 	/* If the address is the same, then there is nothing to update */
8080 	if (!memcmp(lladdr, hw->port_info->mac.lan_addr, ETHER_ADDR_LEN))
8081 		return (0);
8082 
8083 	/* Reject Multicast addresses */
8084 	if (ETHER_IS_MULTICAST(lladdr))
8085 		return (EINVAL);
8086 
8087 	status = ice_aq_manage_mac_write(hw, lladdr, ICE_AQC_MAN_MAC_UPDATE_LAA_WOL, NULL);
8088 	if (status) {
8089 		device_printf(sc->dev, "Failed to write mac %6D to firmware, err %s aq_err %s\n",
8090 			      lladdr, ":", ice_status_str(status),
8091 			      ice_aq_str(hw->adminq.sq_last_status));
8092 		return (EFAULT);
8093 	}
8094 
8095 	/* Copy the address into place of the LAN address. */
8096 	bcopy(lladdr, hw->port_info->mac.lan_addr, ETHER_ADDR_LEN);
8097 
8098 	return (0);
8099 }
8100 
8101 /**
8102  * ice_get_and_print_bus_info - Save (PCI) bus info and print messages
8103  * @sc: device softc
8104  *
8105  * This will potentially print out a warning message if bus bandwidth
8106  * is insufficient for full-speed operation. This will not print out anything
8107  * for E82x devices since those are in SoCs, do not report valid PCIe info,
8108  * and cannot be moved to a different slot.
8109  *
8110  * This should only be called once, during the attach process, after
8111  * hw->port_info has been filled out with port link topology information
8112  * (from the Get PHY Capabilities Admin Queue command).
8113  */
8114 void
ice_get_and_print_bus_info(struct ice_softc * sc)8115 ice_get_and_print_bus_info(struct ice_softc *sc)
8116 {
8117 	struct ice_hw *hw = &sc->hw;
8118 	device_t dev = sc->dev;
8119 	u16 pci_link_status;
8120 	int offset;
8121 
8122 	if (!ice_is_e810(hw) && !ice_is_e830(hw))
8123 		return;
8124 
8125 	pci_find_cap(dev, PCIY_EXPRESS, &offset);
8126 	pci_link_status = pci_read_config(dev, offset + PCIER_LINK_STA, 2);
8127 
8128 	/* Fill out hw struct with PCIE link status info */
8129 	ice_set_pci_link_status_data(hw, pci_link_status);
8130 
8131 	/* Use info to print out bandwidth messages */
8132 	ice_print_bus_link_data(dev, hw);
8133 
8134 	if (ice_pcie_bandwidth_check(sc)) {
8135 		device_printf(dev,
8136 		    "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8137 		device_printf(dev,
8138 		    "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8139 	}
8140 }
8141 
8142 /**
8143  * ice_pcie_bus_speed_to_rate - Convert driver bus speed enum value to
8144  * a 64-bit baudrate.
8145  * @speed: enum value to convert
8146  *
8147  * This only goes up to PCIE Gen 5.
8148  */
8149 static uint64_t
ice_pcie_bus_speed_to_rate(enum ice_pcie_bus_speed speed)8150 ice_pcie_bus_speed_to_rate(enum ice_pcie_bus_speed speed)
8151 {
8152 	/* If the PCI-E speed is Gen1 or Gen2, then report
8153 	 * only 80% of bus speed to account for encoding overhead.
8154 	 */
8155 	switch (speed) {
8156 	case ice_pcie_speed_2_5GT:
8157 		return IF_Gbps(2);
8158 	case ice_pcie_speed_5_0GT:
8159 		return IF_Gbps(4);
8160 	case ice_pcie_speed_8_0GT:
8161 		return IF_Gbps(8);
8162 	case ice_pcie_speed_16_0GT:
8163 		return IF_Gbps(16);
8164 	case ice_pcie_speed_32_0GT:
8165 		return IF_Gbps(32);
8166 	case ice_pcie_speed_unknown:
8167 	default:
8168 		return 0;
8169 	}
8170 }
8171 
8172 /**
8173  * ice_pcie_lnk_width_to_int - Convert driver pci-e width enum value to
8174  * a 32-bit number.
8175  * @width: enum value to convert
8176  */
8177 static int
ice_pcie_lnk_width_to_int(enum ice_pcie_link_width width)8178 ice_pcie_lnk_width_to_int(enum ice_pcie_link_width width)
8179 {
8180 	switch (width) {
8181 	case ice_pcie_lnk_x1:
8182 		return (1);
8183 	case ice_pcie_lnk_x2:
8184 		return (2);
8185 	case ice_pcie_lnk_x4:
8186 		return (4);
8187 	case ice_pcie_lnk_x8:
8188 		return (8);
8189 	case ice_pcie_lnk_x12:
8190 		return (12);
8191 	case ice_pcie_lnk_x16:
8192 		return (16);
8193 	case ice_pcie_lnk_x32:
8194 		return (32);
8195 	case ice_pcie_lnk_width_resrv:
8196 	case ice_pcie_lnk_width_unknown:
8197 	default:
8198 		return (0);
8199 	}
8200 }
8201 
8202 /**
8203  * ice_pcie_bandwidth_check - Check if PCI-E bandwidth is sufficient for
8204  * full-speed device operation.
8205  * @sc: adapter softc
8206  *
8207  * Returns 0 if sufficient; 1 if not.
8208  */
8209 static uint8_t
ice_pcie_bandwidth_check(struct ice_softc * sc)8210 ice_pcie_bandwidth_check(struct ice_softc *sc)
8211 {
8212 	struct ice_hw *hw = &sc->hw;
8213 	int num_ports, pcie_width;
8214 	u64 pcie_speed, port_speed;
8215 
8216 	MPASS(hw->port_info);
8217 
8218 	num_ports = bitcount32(hw->func_caps.common_cap.valid_functions);
8219 	port_speed = ice_phy_types_to_max_rate(hw->port_info);
8220 	pcie_speed = ice_pcie_bus_speed_to_rate(hw->bus.speed);
8221 	pcie_width = ice_pcie_lnk_width_to_int(hw->bus.width);
8222 
8223 	/*
8224 	 * If 2x100 on E810 or 2x200 on E830, clamp ports to 1 -- 2nd port is
8225 	 * intended for failover.
8226 	 */
8227 	if ((port_speed >= IF_Gbps(100)) &&
8228 	    ((port_speed == IF_Gbps(100) && ice_is_e810(hw)) ||
8229 	     (port_speed == IF_Gbps(200) && ice_is_e830(hw))))
8230 		num_ports = 1;
8231 
8232 	return !!((num_ports * port_speed) > pcie_speed * pcie_width);
8233 }
8234 
8235 /**
8236  * ice_print_bus_link_data - Print PCI-E bandwidth information
8237  * @dev: device to print string for
8238  * @hw: hw struct with PCI-e link information
8239  */
8240 static void
ice_print_bus_link_data(device_t dev,struct ice_hw * hw)8241 ice_print_bus_link_data(device_t dev, struct ice_hw *hw)
8242 {
8243         device_printf(dev, "PCI Express Bus: Speed %s Width %s\n",
8244             ((hw->bus.speed == ice_pcie_speed_32_0GT) ? "32.0GT/s" :
8245             (hw->bus.speed == ice_pcie_speed_16_0GT) ? "16.0GT/s" :
8246             (hw->bus.speed == ice_pcie_speed_8_0GT) ? "8.0GT/s" :
8247             (hw->bus.speed == ice_pcie_speed_5_0GT) ? "5.0GT/s" :
8248             (hw->bus.speed == ice_pcie_speed_2_5GT) ? "2.5GT/s" : "Unknown"),
8249             (hw->bus.width == ice_pcie_lnk_x32) ? "x32" :
8250             (hw->bus.width == ice_pcie_lnk_x16) ? "x16" :
8251             (hw->bus.width == ice_pcie_lnk_x12) ? "x12" :
8252             (hw->bus.width == ice_pcie_lnk_x8) ? "x8" :
8253             (hw->bus.width == ice_pcie_lnk_x4) ? "x4" :
8254             (hw->bus.width == ice_pcie_lnk_x2) ? "x2" :
8255             (hw->bus.width == ice_pcie_lnk_x1) ? "x1" : "Unknown");
8256 }
8257 
8258 /**
8259  * ice_set_pci_link_status_data - store PCI bus info
8260  * @hw: pointer to hardware structure
8261  * @link_status: the link status word from PCI config space
8262  *
8263  * Stores the PCI bus info (speed, width, type) within the ice_hw structure
8264  **/
8265 static void
ice_set_pci_link_status_data(struct ice_hw * hw,u16 link_status)8266 ice_set_pci_link_status_data(struct ice_hw *hw, u16 link_status)
8267 {
8268 	u16 reg;
8269 
8270 	hw->bus.type = ice_bus_pci_express;
8271 
8272 	reg = (link_status & PCIEM_LINK_STA_WIDTH) >> 4;
8273 
8274 	switch (reg) {
8275 	case ice_pcie_lnk_x1:
8276 	case ice_pcie_lnk_x2:
8277 	case ice_pcie_lnk_x4:
8278 	case ice_pcie_lnk_x8:
8279 	case ice_pcie_lnk_x12:
8280 	case ice_pcie_lnk_x16:
8281 	case ice_pcie_lnk_x32:
8282 		hw->bus.width = (enum ice_pcie_link_width)reg;
8283 		break;
8284 	default:
8285 		hw->bus.width = ice_pcie_lnk_width_unknown;
8286 		break;
8287 	}
8288 
8289 	reg = (link_status & PCIEM_LINK_STA_SPEED) + 0x13;
8290 
8291 	switch (reg) {
8292 	case ice_pcie_speed_2_5GT:
8293 	case ice_pcie_speed_5_0GT:
8294 	case ice_pcie_speed_8_0GT:
8295 	case ice_pcie_speed_16_0GT:
8296 	case ice_pcie_speed_32_0GT:
8297 		hw->bus.speed = (enum ice_pcie_bus_speed)reg;
8298 		break;
8299 	default:
8300 		hw->bus.speed = ice_pcie_speed_unknown;
8301 		break;
8302 	}
8303 }
8304 
8305 /**
8306  * ice_init_link_events - Initialize Link Status Events mask
8307  * @sc: the device softc
8308  *
8309  * Initialize the Link Status Events mask to disable notification of link
8310  * events we don't care about in software. Also request that link status
8311  * events be enabled.
8312  */
8313 int
ice_init_link_events(struct ice_softc * sc)8314 ice_init_link_events(struct ice_softc *sc)
8315 {
8316 	struct ice_hw *hw = &sc->hw;
8317 	int status;
8318 	u16 wanted_events;
8319 
8320 	/* Set the bits for the events that we want to be notified by */
8321 	wanted_events = (ICE_AQ_LINK_EVENT_UPDOWN |
8322 			 ICE_AQ_LINK_EVENT_MEDIA_NA |
8323 			 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL);
8324 
8325 	/* request that every event except the wanted events be masked */
8326 	status = ice_aq_set_event_mask(hw, hw->port_info->lport, ~wanted_events, NULL);
8327 	if (status) {
8328 		device_printf(sc->dev,
8329 			      "Failed to set link status event mask, err %s aq_err %s\n",
8330 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
8331 		return (EIO);
8332 	}
8333 
8334 	/* Request link info with the LSE bit set to enable link status events */
8335 	status = ice_aq_get_link_info(hw->port_info, true, NULL, NULL);
8336 	if (status) {
8337 		device_printf(sc->dev,
8338 			      "Failed to enable link status events, err %s aq_err %s\n",
8339 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
8340 		return (EIO);
8341 	}
8342 
8343 	return (0);
8344 }
8345 
8346 #ifndef GL_MDET_TX_TCLAN
8347 /* Temporarily use this redefinition until the definition is fixed */
8348 #define GL_MDET_TX_TCLAN	E800_GL_MDET_TX_TCLAN
8349 #define PF_MDET_TX_TCLAN	E800_PF_MDET_TX_TCLAN
8350 #endif /* !defined(GL_MDET_TX_TCLAN) */
8351 /**
8352  * ice_handle_mdd_event - Handle possibly malicious events
8353  * @sc: the device softc
8354  *
8355  * Called by the admin task if an MDD detection interrupt is triggered.
8356  * Identifies possibly malicious events coming from VFs. Also triggers for
8357  * similar incorrect behavior from the PF as well.
8358  */
8359 void
ice_handle_mdd_event(struct ice_softc * sc)8360 ice_handle_mdd_event(struct ice_softc *sc)
8361 {
8362 	struct ice_hw *hw = &sc->hw;
8363 	bool mdd_detected = false, request_reinit = false;
8364 	device_t dev = sc->dev;
8365 	u32 reg;
8366 
8367 	if (!ice_testandclear_state(&sc->state, ICE_STATE_MDD_PENDING))
8368 		return;
8369 
8370 	reg = rd32(hw, GL_MDET_TX_TCLAN);
8371 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
8372 		u8 pf_num  = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >> GL_MDET_TX_TCLAN_PF_NUM_S;
8373 		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >> GL_MDET_TX_TCLAN_VF_NUM_S;
8374 		u8 event   = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >> GL_MDET_TX_TCLAN_MAL_TYPE_S;
8375 		u16 queue  = (reg & GL_MDET_TX_TCLAN_QNUM_M) >> GL_MDET_TX_TCLAN_QNUM_S;
8376 
8377 		device_printf(dev, "Malicious Driver Detection Tx Descriptor check event '%s' on Tx queue %u PF# %u VF# %u\n",
8378 			      ice_mdd_tx_tclan_str(event), queue, pf_num, vf_num);
8379 
8380 		/* Only clear this event if it matches this PF, that way other
8381 		 * PFs can read the event and determine VF and queue number.
8382 		 */
8383 		if (pf_num == hw->pf_id)
8384 			wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
8385 
8386 		mdd_detected = true;
8387 	}
8388 
8389 	/* Determine what triggered the MDD event */
8390 	reg = rd32(hw, GL_MDET_TX_PQM);
8391 	if (reg & GL_MDET_TX_PQM_VALID_M) {
8392 		u8 pf_num  = (reg & GL_MDET_TX_PQM_PF_NUM_M) >> GL_MDET_TX_PQM_PF_NUM_S;
8393 		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >> GL_MDET_TX_PQM_VF_NUM_S;
8394 		u8 event   = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >> GL_MDET_TX_PQM_MAL_TYPE_S;
8395 		u16 queue  = (reg & GL_MDET_TX_PQM_QNUM_M) >> GL_MDET_TX_PQM_QNUM_S;
8396 
8397 		device_printf(dev, "Malicious Driver Detection Tx Quanta check event '%s' on Tx queue %u PF# %u VF# %u\n",
8398 			      ice_mdd_tx_pqm_str(event), queue, pf_num, vf_num);
8399 
8400 		/* Only clear this event if it matches this PF, that way other
8401 		 * PFs can read the event and determine VF and queue number.
8402 		 */
8403 		if (pf_num == hw->pf_id)
8404 			wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
8405 
8406 		mdd_detected = true;
8407 	}
8408 
8409 	reg = rd32(hw, GL_MDET_RX);
8410 	if (reg & GL_MDET_RX_VALID_M) {
8411 		u8 pf_num  = (reg & GL_MDET_RX_PF_NUM_M) >> GL_MDET_RX_PF_NUM_S;
8412 		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >> GL_MDET_RX_VF_NUM_S;
8413 		u8 event   = (reg & GL_MDET_RX_MAL_TYPE_M) >> GL_MDET_RX_MAL_TYPE_S;
8414 		u16 queue  = (reg & GL_MDET_RX_QNUM_M) >> GL_MDET_RX_QNUM_S;
8415 
8416 		device_printf(dev, "Malicious Driver Detection Rx event '%s' on Rx queue %u PF# %u VF# %u\n",
8417 			      ice_mdd_rx_str(event), queue, pf_num, vf_num);
8418 
8419 		/* Only clear this event if it matches this PF, that way other
8420 		 * PFs can read the event and determine VF and queue number.
8421 		 */
8422 		if (pf_num == hw->pf_id)
8423 			wr32(hw, GL_MDET_RX, 0xffffffff);
8424 
8425 		mdd_detected = true;
8426 	}
8427 
8428 	/* Now, confirm that this event actually affects this PF, by checking
8429 	 * the PF registers.
8430 	 */
8431 	if (mdd_detected) {
8432 		reg = rd32(hw, PF_MDET_TX_TCLAN);
8433 		if (reg & PF_MDET_TX_TCLAN_VALID_M) {
8434 			wr32(hw, PF_MDET_TX_TCLAN, 0xffff);
8435 			sc->soft_stats.tx_mdd_count++;
8436 			request_reinit = true;
8437 		}
8438 
8439 		reg = rd32(hw, PF_MDET_TX_PQM);
8440 		if (reg & PF_MDET_TX_PQM_VALID_M) {
8441 			wr32(hw, PF_MDET_TX_PQM, 0xffff);
8442 			sc->soft_stats.tx_mdd_count++;
8443 			request_reinit = true;
8444 		}
8445 
8446 		reg = rd32(hw, PF_MDET_RX);
8447 		if (reg & PF_MDET_RX_VALID_M) {
8448 			wr32(hw, PF_MDET_RX, 0xffff);
8449 			sc->soft_stats.rx_mdd_count++;
8450 			request_reinit = true;
8451 		}
8452 	}
8453 
8454 	/* TODO: Implement logic to detect and handle events caused by VFs. */
8455 
8456 	/* request that the upper stack re-initialize the Tx/Rx queues */
8457 	if (request_reinit)
8458 		ice_request_stack_reinit(sc);
8459 
8460 	ice_flush(hw);
8461 }
8462 
8463 /**
8464  * ice_start_dcbx_agent - Start DCBX agent in FW via AQ command
8465  * @sc: the device softc
8466  *
8467  * @pre device is DCB capable and the FW LLDP agent has started
8468  *
8469  * Checks DCBX status and starts the DCBX agent if it is not in
8470  * a valid state via an AQ command.
8471  */
8472 static void
ice_start_dcbx_agent(struct ice_softc * sc)8473 ice_start_dcbx_agent(struct ice_softc *sc)
8474 {
8475 	struct ice_hw *hw = &sc->hw;
8476 	device_t dev = sc->dev;
8477 	bool dcbx_agent_status;
8478 	int status;
8479 
8480 	hw->port_info->qos_cfg.dcbx_status = ice_get_dcbx_status(hw);
8481 
8482 	if (hw->port_info->qos_cfg.dcbx_status != ICE_DCBX_STATUS_DONE &&
8483 	    hw->port_info->qos_cfg.dcbx_status != ICE_DCBX_STATUS_IN_PROGRESS) {
8484 		/*
8485 		 * Start DCBX agent, but not LLDP. The return value isn't
8486 		 * checked here because a more detailed dcbx agent status is
8487 		 * retrieved and checked in ice_init_dcb() and elsewhere.
8488 		 */
8489 		status = ice_aq_start_stop_dcbx(hw, true, &dcbx_agent_status, NULL);
8490 		if (status && hw->adminq.sq_last_status != ICE_AQ_RC_EPERM)
8491 			device_printf(dev,
8492 			    "start_stop_dcbx failed, err %s aq_err %s\n",
8493 			    ice_status_str(status),
8494 			    ice_aq_str(hw->adminq.sq_last_status));
8495 	}
8496 }
8497 
8498 /**
8499  * ice_init_dcb_setup - Initialize DCB settings for HW
8500  * @sc: the device softc
8501  *
8502  * This needs to be called after the fw_lldp_agent sysctl is added, since that
8503  * can update the device's LLDP agent status if a tunable value is set.
8504  *
8505  * Get and store the initial state of DCB settings on driver load. Print out
8506  * informational messages as well.
8507  */
8508 void
ice_init_dcb_setup(struct ice_softc * sc)8509 ice_init_dcb_setup(struct ice_softc *sc)
8510 {
8511 	struct ice_dcbx_cfg *local_dcbx_cfg;
8512 	struct ice_hw *hw = &sc->hw;
8513 	device_t dev = sc->dev;
8514 	int status;
8515 	u8 pfcmode_ret;
8516 
8517 	/* Don't do anything if DCB isn't supported */
8518 	if (!ice_is_bit_set(sc->feat_cap, ICE_FEATURE_DCB)) {
8519 		device_printf(dev, "%s: No DCB support\n", __func__);
8520 		return;
8521 	}
8522 
8523 	/* Starts DCBX agent if it needs starting */
8524 	ice_start_dcbx_agent(sc);
8525 
8526 	/* This sets hw->port_info->qos_cfg.is_sw_lldp */
8527 	status = ice_init_dcb(hw, true);
8528 
8529 	/* If there is an error, then FW LLDP is not in a usable state */
8530 	if (status != 0 && status != ICE_ERR_NOT_READY) {
8531 		/* Don't print an error message if the return code from the AQ
8532 		 * cmd performed in ice_init_dcb() is EPERM; that means the
8533 		 * FW LLDP engine is disabled, and that is a valid state.
8534 		 */
8535 		if (!(status == ICE_ERR_AQ_ERROR &&
8536 		      hw->adminq.sq_last_status == ICE_AQ_RC_EPERM)) {
8537 			device_printf(dev, "DCB init failed, err %s aq_err %s\n",
8538 				      ice_status_str(status),
8539 				      ice_aq_str(hw->adminq.sq_last_status));
8540 		}
8541 		hw->port_info->qos_cfg.dcbx_status = ICE_DCBX_STATUS_NOT_STARTED;
8542 	}
8543 
8544 	switch (hw->port_info->qos_cfg.dcbx_status) {
8545 	case ICE_DCBX_STATUS_DIS:
8546 		ice_debug(hw, ICE_DBG_DCB, "DCBX disabled\n");
8547 		break;
8548 	case ICE_DCBX_STATUS_NOT_STARTED:
8549 		ice_debug(hw, ICE_DBG_DCB, "DCBX not started\n");
8550 		break;
8551 	case ICE_DCBX_STATUS_MULTIPLE_PEERS:
8552 		ice_debug(hw, ICE_DBG_DCB, "DCBX detected multiple peers\n");
8553 		break;
8554 	default:
8555 		break;
8556 	}
8557 
8558 	/* LLDP disabled in FW */
8559 	if (hw->port_info->qos_cfg.is_sw_lldp) {
8560 		ice_add_rx_lldp_filter(sc);
8561 		device_printf(dev, "Firmware LLDP agent disabled\n");
8562 	}
8563 
8564 	/* Query and cache PFC mode */
8565 	status = ice_aq_query_pfc_mode(hw, &pfcmode_ret, NULL);
8566 	if (status) {
8567 		device_printf(dev, "PFC mode query failed, err %s aq_err %s\n",
8568 			      ice_status_str(status),
8569 			      ice_aq_str(hw->adminq.sq_last_status));
8570 	}
8571 	local_dcbx_cfg = &hw->port_info->qos_cfg.local_dcbx_cfg;
8572 	switch (pfcmode_ret) {
8573 	case ICE_AQC_PFC_VLAN_BASED_PFC:
8574 		local_dcbx_cfg->pfc_mode = ICE_QOS_MODE_VLAN;
8575 		break;
8576 	case ICE_AQC_PFC_DSCP_BASED_PFC:
8577 		local_dcbx_cfg->pfc_mode = ICE_QOS_MODE_DSCP;
8578 		break;
8579 	default:
8580 		/* DCB is disabled, but we shouldn't get here */
8581 		break;
8582 	}
8583 
8584 	/* Set default SW MIB for init */
8585 	ice_set_default_local_mib_settings(sc);
8586 
8587 	ice_set_bit(ICE_FEATURE_DCB, sc->feat_en);
8588 }
8589 
8590 /**
8591  * ice_dcb_get_tc_map - Scans config to get bitmap of enabled TCs
8592  * @dcbcfg: DCB configuration to examine
8593  *
8594  * Scans a TC mapping table inside dcbcfg to find traffic classes
8595  * enabled and @returns a bitmask of enabled TCs
8596  */
8597 u8
ice_dcb_get_tc_map(const struct ice_dcbx_cfg * dcbcfg)8598 ice_dcb_get_tc_map(const struct ice_dcbx_cfg *dcbcfg)
8599 {
8600 	u8 tc_map = 0;
8601 	int i = 0;
8602 
8603 	switch (dcbcfg->pfc_mode) {
8604 	case ICE_QOS_MODE_VLAN:
8605 		/* XXX: "i" is actually "User Priority" here, not
8606 		 * Traffic Class, but the max for both is 8, so it works
8607 		 * out here.
8608 		 */
8609 		for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++)
8610 			tc_map |= BIT(dcbcfg->etscfg.prio_table[i]);
8611 		break;
8612 	case ICE_QOS_MODE_DSCP:
8613 		for (i = 0; i < ICE_DSCP_NUM_VAL; i++)
8614 			tc_map |= BIT(dcbcfg->dscp_map[i]);
8615 		break;
8616 	default:
8617 		/* Invalid Mode */
8618 		tc_map = ICE_DFLT_TRAFFIC_CLASS;
8619 		break;
8620 	}
8621 
8622 	return (tc_map);
8623 }
8624 
8625 /**
8626  * ice_dcb_get_num_tc - Get the number of TCs from DCBX config
8627  * @dcbcfg: config to retrieve number of TCs from
8628  *
8629  * @return number of contiguous TCs found in dcbcfg's ETS Configuration
8630  * Priority Assignment Table, a value from 1 to 8. If there are
8631  * non-contiguous TCs used (e.g. assigning 1 and 3 without using 2),
8632  * then returns 0.
8633  */
8634 static u8
ice_dcb_get_num_tc(struct ice_dcbx_cfg * dcbcfg)8635 ice_dcb_get_num_tc(struct ice_dcbx_cfg *dcbcfg)
8636 {
8637 	u8 tc_map;
8638 
8639 	tc_map = ice_dcb_get_tc_map(dcbcfg);
8640 
8641 	return (ice_dcb_tc_contig(tc_map));
8642 }
8643 
8644 /**
8645  * ice_debug_print_mib_change_event - helper function to log LLDP MIB change events
8646  * @sc: the device private softc
8647  * @event: event received on a control queue
8648  *
8649  * Prints out the type and contents of an LLDP MIB change event in a DCB debug message.
8650  */
8651 static void
ice_debug_print_mib_change_event(struct ice_softc * sc,struct ice_rq_event_info * event)8652 ice_debug_print_mib_change_event(struct ice_softc *sc, struct ice_rq_event_info *event)
8653 {
8654 	struct ice_aqc_lldp_get_mib *params =
8655 	    (struct ice_aqc_lldp_get_mib *)&event->desc.params.lldp_get_mib;
8656 	u8 mib_type, bridge_type, tx_status;
8657 
8658 	static const char* mib_type_strings[] = {
8659 	    "Local MIB",
8660 	    "Remote MIB",
8661 	    "Reserved",
8662 	    "Reserved"
8663 	};
8664 	static const char* bridge_type_strings[] = {
8665 	    "Nearest Bridge",
8666 	    "Non-TPMR Bridge",
8667 	    "Reserved",
8668 	    "Reserved"
8669 	};
8670 	static const char* tx_status_strings[] = {
8671 	    "Port's TX active",
8672 	    "Port's TX suspended and drained",
8673 	    "Reserved",
8674 	    "Port's TX suspended and drained; blocked TC pipe flushed"
8675 	};
8676 
8677 	mib_type = (params->type & ICE_AQ_LLDP_MIB_TYPE_M) >>
8678 	    ICE_AQ_LLDP_MIB_TYPE_S;
8679 	bridge_type = (params->type & ICE_AQ_LLDP_BRID_TYPE_M) >>
8680 	    ICE_AQ_LLDP_BRID_TYPE_S;
8681 	tx_status = (params->type & ICE_AQ_LLDP_TX_M) >>
8682 	    ICE_AQ_LLDP_TX_S;
8683 
8684 	ice_debug(&sc->hw, ICE_DBG_DCB, "LLDP MIB Change Event (%s, %s, %s)\n",
8685 	    mib_type_strings[mib_type], bridge_type_strings[bridge_type],
8686 	    tx_status_strings[tx_status]);
8687 
8688 	/* Nothing else to report */
8689 	if (!event->msg_buf)
8690 		return;
8691 
8692 	ice_debug(&sc->hw, ICE_DBG_DCB, "- %s contents:\n", mib_type_strings[mib_type]);
8693 	ice_debug_array(&sc->hw, ICE_DBG_DCB, 16, 1, event->msg_buf,
8694 			event->msg_len);
8695 }
8696 
8697 /**
8698  * ice_dcb_needs_reconfig - Returns true if driver needs to reconfigure
8699  * @sc: the device private softc
8700  * @old_cfg: Old DCBX configuration to compare against
8701  * @new_cfg: New DCBX configuration to check
8702  *
8703  * @return true if something changed in new_cfg that requires the driver
8704  * to do some reconfiguration.
8705  */
8706 static bool
ice_dcb_needs_reconfig(struct ice_softc * sc,struct ice_dcbx_cfg * old_cfg,struct ice_dcbx_cfg * new_cfg)8707 ice_dcb_needs_reconfig(struct ice_softc *sc, struct ice_dcbx_cfg *old_cfg,
8708     struct ice_dcbx_cfg *new_cfg)
8709 {
8710 	struct ice_hw *hw = &sc->hw;
8711 	bool needs_reconfig = false;
8712 
8713 	/* No change detected in DCBX config */
8714 	if (!memcmp(old_cfg, new_cfg, sizeof(*old_cfg))) {
8715 		ice_debug(hw, ICE_DBG_DCB,
8716 		    "No change detected in local DCBX configuration\n");
8717 		return (false);
8718 	}
8719 
8720 	/* Check if ETS config has changed */
8721 	if (memcmp(&new_cfg->etscfg, &old_cfg->etscfg,
8722 		   sizeof(new_cfg->etscfg))) {
8723 		/* If Priority Table has changed, then driver reconfig is needed */
8724 		if (memcmp(&new_cfg->etscfg.prio_table,
8725 			   &old_cfg->etscfg.prio_table,
8726 			   sizeof(new_cfg->etscfg.prio_table))) {
8727 			ice_debug(hw, ICE_DBG_DCB, "ETS UP2TC changed\n");
8728 			needs_reconfig = true;
8729 		}
8730 
8731 		/* These are just informational */
8732 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8733 			   &old_cfg->etscfg.tcbwtable,
8734 			   sizeof(new_cfg->etscfg.tcbwtable))) {
8735 			ice_debug(hw, ICE_DBG_DCB, "ETS TCBW table changed\n");
8736 			needs_reconfig = true;
8737 		}
8738 
8739 		if (memcmp(&new_cfg->etscfg.tsatable,
8740 			   &old_cfg->etscfg.tsatable,
8741 			   sizeof(new_cfg->etscfg.tsatable))) {
8742 			ice_debug(hw, ICE_DBG_DCB, "ETS TSA table changed\n");
8743 			needs_reconfig = true;
8744 		}
8745 	}
8746 
8747 	/* Check if PFC config has changed */
8748 	if (memcmp(&new_cfg->pfc, &old_cfg->pfc, sizeof(new_cfg->pfc))) {
8749 		ice_debug(hw, ICE_DBG_DCB, "PFC config changed\n");
8750 		needs_reconfig = true;
8751 	}
8752 
8753 	/* Check if APP table has changed */
8754 	if (memcmp(&new_cfg->app, &old_cfg->app, sizeof(new_cfg->app)))
8755 		ice_debug(hw, ICE_DBG_DCB, "APP Table changed\n");
8756 
8757 	ice_debug(hw, ICE_DBG_DCB, "%s result: %d\n", __func__, needs_reconfig);
8758 
8759 	return (needs_reconfig);
8760 }
8761 
8762 /**
8763  * ice_stop_pf_vsi - Stop queues for PF LAN VSI
8764  * @sc: the device private softc
8765  *
8766  * Flushes interrupts and stops the queues associated with the PF LAN VSI.
8767  */
8768 static void
ice_stop_pf_vsi(struct ice_softc * sc)8769 ice_stop_pf_vsi(struct ice_softc *sc)
8770 {
8771 	/* Dissociate the Tx and Rx queues from the interrupts */
8772 	ice_flush_txq_interrupts(&sc->pf_vsi);
8773 	ice_flush_rxq_interrupts(&sc->pf_vsi);
8774 
8775 	if (!ice_testandclear_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED))
8776 		return;
8777 
8778 	/* Disable the Tx and Rx queues */
8779 	ice_vsi_disable_tx(&sc->pf_vsi);
8780 	ice_control_all_rx_queues(&sc->pf_vsi, false);
8781 }
8782 
8783 /**
8784  * ice_vsi_setup_q_map - Setup a VSI queue map
8785  * @vsi: the VSI being configured
8786  * @ctxt: VSI context structure
8787  */
8788 static void
ice_vsi_setup_q_map(struct ice_vsi * vsi,struct ice_vsi_ctx * ctxt)8789 ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
8790 {
8791 	u16 qcounts[ICE_MAX_TRAFFIC_CLASS] = {};
8792 	u16 offset = 0, qmap = 0, pow = 0;
8793 	u16 num_q_per_tc, qcount_rx, rem_queues;
8794 	int i, j, k;
8795 
8796 	if (vsi->num_tcs == 0) {
8797 		/* at least TC0 should be enabled by default */
8798 		vsi->num_tcs = 1;
8799 		vsi->tc_map = 0x1;
8800 	}
8801 
8802 	qcount_rx = vsi->num_rx_queues;
8803 	num_q_per_tc = min(qcount_rx / vsi->num_tcs, ICE_MAX_RXQS_PER_TC);
8804 
8805 	if (!num_q_per_tc)
8806 		num_q_per_tc = 1;
8807 
8808 	/* Set initial values for # of queues to use for each active TC */
8809 	ice_for_each_traffic_class(i)
8810 		if (i < vsi->num_tcs)
8811 			qcounts[i] = num_q_per_tc;
8812 
8813 	/* If any queues are unassigned, add them to TC 0 */
8814 	rem_queues = qcount_rx % vsi->num_tcs;
8815 	if (rem_queues > 0)
8816 		qcounts[0] += rem_queues;
8817 
8818 	/* TC mapping is a function of the number of Rx queues assigned to the
8819 	 * VSI for each traffic class and the offset of these queues.
8820 	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
8821 	 * queues allocated to TC0. No:of queues is a power-of-2.
8822 	 *
8823 	 * If TC is not enabled, the queue offset is set to 0, and allocate one
8824 	 * queue, this way, traffic for the given TC will be sent to the default
8825 	 * queue.
8826 	 *
8827 	 * Setup number and offset of Rx queues for all TCs for the VSI
8828 	 */
8829 	ice_for_each_traffic_class(i) {
8830 		if (!(vsi->tc_map & BIT(i))) {
8831 			/* TC is not enabled */
8832 			vsi->tc_info[i].qoffset = 0;
8833 			vsi->tc_info[i].qcount_rx = 1;
8834 			vsi->tc_info[i].qcount_tx = 1;
8835 
8836 			ctxt->info.tc_mapping[i] = 0;
8837 			continue;
8838 		}
8839 
8840 		/* TC is enabled */
8841 		vsi->tc_info[i].qoffset = offset;
8842 		vsi->tc_info[i].qcount_rx = qcounts[i];
8843 		vsi->tc_info[i].qcount_tx = qcounts[i];
8844 
8845 		/* find the (rounded up) log-2 of queue count for current TC */
8846 		pow = fls(qcounts[i] - 1);
8847 
8848 		qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
8849 			ICE_AQ_VSI_TC_Q_OFFSET_M) |
8850 			((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
8851 			 ICE_AQ_VSI_TC_Q_NUM_M);
8852 		ctxt->info.tc_mapping[i] = CPU_TO_LE16(qmap);
8853 
8854 		/* Store traffic class and handle data in queue structures */
8855 		for (j = offset, k = 0; j < offset + qcounts[i]; j++, k++) {
8856 			vsi->tx_queues[j].q_handle = k;
8857 			vsi->tx_queues[j].tc = i;
8858 
8859 			vsi->rx_queues[j].tc = i;
8860 		}
8861 
8862 		offset += qcounts[i];
8863 	}
8864 
8865 	/* Rx queue mapping */
8866 	ctxt->info.mapping_flags |= CPU_TO_LE16(ICE_AQ_VSI_Q_MAP_CONTIG);
8867 	ctxt->info.q_mapping[0] = CPU_TO_LE16(vsi->rx_qmap[0]);
8868 	ctxt->info.q_mapping[1] = CPU_TO_LE16(vsi->num_rx_queues);
8869 }
8870 
8871 /**
8872  * ice_pf_vsi_cfg_tc - Configure PF VSI for a given TC map
8873  * @sc: the device private softc
8874  * @tc_map: traffic class bitmap
8875  *
8876  * @pre VSI queues are stopped
8877  *
8878  * @return 0 if configuration is successful
8879  * @return EIO if Update VSI AQ cmd fails
8880  * @return ENODEV if updating Tx Scheduler fails
8881  */
8882 static int
ice_pf_vsi_cfg_tc(struct ice_softc * sc,u8 tc_map)8883 ice_pf_vsi_cfg_tc(struct ice_softc *sc, u8 tc_map)
8884 {
8885 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
8886 	struct ice_vsi *vsi = &sc->pf_vsi;
8887 	struct ice_hw *hw = &sc->hw;
8888 	struct ice_vsi_ctx ctx = { 0 };
8889 	device_t dev = sc->dev;
8890 	int status;
8891 	u8 num_tcs = 0;
8892 	int i = 0;
8893 
8894 	/* Count the number of enabled Traffic Classes */
8895 	ice_for_each_traffic_class(i)
8896 		if (tc_map & BIT(i))
8897 			num_tcs++;
8898 
8899 	vsi->tc_map = tc_map;
8900 	vsi->num_tcs = num_tcs;
8901 
8902 	/* Set default parameters for context */
8903 	ctx.vf_num = 0;
8904 	ctx.info = vsi->info;
8905 
8906 	/* Setup queue map */
8907 	ice_vsi_setup_q_map(vsi, &ctx);
8908 
8909 	/* Update VSI configuration in firmware (RX queues) */
8910 	ctx.info.valid_sections = CPU_TO_LE16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
8911 	status = ice_update_vsi(hw, vsi->idx, &ctx, NULL);
8912 	if (status) {
8913 		device_printf(dev,
8914 		    "%s: Update VSI AQ call failed, err %s aq_err %s\n",
8915 		    __func__, ice_status_str(status),
8916 		    ice_aq_str(hw->adminq.sq_last_status));
8917 		return (EIO);
8918 	}
8919 	vsi->info = ctx.info;
8920 
8921 	/* Use values derived in ice_vsi_setup_q_map() */
8922 	for (i = 0; i < num_tcs; i++)
8923 		max_txqs[i] = vsi->tc_info[i].qcount_tx;
8924 
8925 	if (hw->debug_mask & ICE_DBG_DCB) {
8926 		device_printf(dev, "%s: max_txqs:", __func__);
8927 		ice_for_each_traffic_class(i)
8928 			printf(" %d", max_txqs[i]);
8929 		printf("\n");
8930 	}
8931 
8932 	/* Update LAN Tx queue info in firmware */
8933 	status = ice_cfg_vsi_lan(hw->port_info, vsi->idx, vsi->tc_map,
8934 				 max_txqs);
8935 	if (status) {
8936 		device_printf(dev,
8937 		    "%s: Failed VSI lan queue config, err %s aq_err %s\n",
8938 		    __func__, ice_status_str(status),
8939 		    ice_aq_str(hw->adminq.sq_last_status));
8940 		return (ENODEV);
8941 	}
8942 
8943 	vsi->info.valid_sections = 0;
8944 
8945 	return (0);
8946 }
8947 
8948 /**
8949  * ice_dcb_tc_contig - Count TCs if they're contiguous
8950  * @tc_map: pointer to priority table
8951  *
8952  * @return The number of traffic classes in
8953  * an 8-bit TC bitmap, or if there is a gap, then returns 0.
8954  */
8955 static u8
ice_dcb_tc_contig(u8 tc_map)8956 ice_dcb_tc_contig(u8 tc_map)
8957 {
8958 	bool tc_unused = false;
8959 	u8 ret = 0;
8960 
8961 	/* Scan bitmask for contiguous TCs starting with TC0 */
8962 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
8963 		if (tc_map & BIT(i)) {
8964 			if (!tc_unused) {
8965 				ret++;
8966 			} else {
8967 				/* Non-contiguous TCs detected */
8968 				return (0);
8969 			}
8970 		} else
8971 			tc_unused = true;
8972 	}
8973 
8974 	return (ret);
8975 }
8976 
8977 /**
8978  * ice_dcb_recfg - Reconfigure VSI with new DCB settings
8979  * @sc: the device private softc
8980  *
8981  * @pre All VSIs have been disabled/stopped
8982  *
8983  * Reconfigures VSI settings based on local_dcbx_cfg.
8984  */
8985 static void
ice_dcb_recfg(struct ice_softc * sc)8986 ice_dcb_recfg(struct ice_softc *sc)
8987 {
8988 	struct ice_dcbx_cfg *dcbcfg =
8989 	    &sc->hw.port_info->qos_cfg.local_dcbx_cfg;
8990 	device_t dev = sc->dev;
8991 	u8 tc_map = 0;
8992 	int ret;
8993 
8994 	tc_map = ice_dcb_get_tc_map(dcbcfg);
8995 
8996 	/* If non-contiguous TCs are used, then configure
8997 	 * the default TC instead. There's no support for
8998 	 * non-contiguous TCs being used.
8999 	 */
9000 	if (ice_dcb_tc_contig(tc_map) == 0) {
9001 		tc_map = ICE_DFLT_TRAFFIC_CLASS;
9002 		ice_set_default_local_lldp_mib(sc);
9003 	}
9004 
9005 	/* Reconfigure VSI queues to add/remove traffic classes */
9006 	ret = ice_pf_vsi_cfg_tc(sc, tc_map);
9007 	if (ret)
9008 		device_printf(dev,
9009 		    "Failed to configure TCs for PF VSI, err %s\n",
9010 		    ice_err_str(ret));
9011 
9012 }
9013 
9014 /**
9015  * ice_set_default_local_mib_settings - Set Local LLDP MIB to default settings
9016  * @sc: device softc structure
9017  *
9018  * Overwrites the driver's SW local LLDP MIB with default settings. This
9019  * ensures the driver has a valid MIB when it next uses the Set Local LLDP MIB
9020  * admin queue command.
9021  */
9022 static void
ice_set_default_local_mib_settings(struct ice_softc * sc)9023 ice_set_default_local_mib_settings(struct ice_softc *sc)
9024 {
9025 	struct ice_dcbx_cfg *dcbcfg;
9026 	struct ice_hw *hw = &sc->hw;
9027 	struct ice_port_info *pi;
9028 	u8 maxtcs, maxtcs_ets, old_pfc_mode;
9029 
9030 	pi = hw->port_info;
9031 
9032 	dcbcfg = &pi->qos_cfg.local_dcbx_cfg;
9033 
9034 	maxtcs = hw->func_caps.common_cap.maxtc;
9035 	/* This value is only 3 bits; 8 TCs maps to 0 */
9036 	maxtcs_ets = maxtcs & ICE_IEEE_ETS_MAXTC_M;
9037 
9038 	/* VLAN vs DSCP mode needs to be preserved */
9039 	old_pfc_mode = dcbcfg->pfc_mode;
9040 
9041 	/**
9042 	 * Setup the default settings used by the driver for the Set Local
9043 	 * LLDP MIB Admin Queue command (0x0A08). (1TC w/ 100% BW, ETS, no
9044 	 * PFC, TSA=2).
9045 	 */
9046 	memset(dcbcfg, 0, sizeof(*dcbcfg));
9047 
9048 	dcbcfg->etscfg.willing = 1;
9049 	dcbcfg->etscfg.tcbwtable[0] = 100;
9050 	dcbcfg->etscfg.maxtcs = maxtcs_ets;
9051 	dcbcfg->etscfg.tsatable[0] = 2;
9052 
9053 	dcbcfg->etsrec = dcbcfg->etscfg;
9054 	dcbcfg->etsrec.willing = 0;
9055 
9056 	dcbcfg->pfc.willing = 1;
9057 	dcbcfg->pfc.pfccap = maxtcs;
9058 
9059 	dcbcfg->pfc_mode = old_pfc_mode;
9060 }
9061 
9062 /**
9063  * ice_do_dcb_reconfig - notify RDMA and reconfigure PF LAN VSI
9064  * @sc: the device private softc
9065  * @pending_mib: FW has a pending MIB change to execute
9066  *
9067  * @pre Determined that the DCB configuration requires a change
9068  *
9069  * Reconfigures the PF LAN VSI based on updated DCB configuration
9070  * found in the hw struct's/port_info's/ local dcbx configuration.
9071  */
9072 void
ice_do_dcb_reconfig(struct ice_softc * sc,bool pending_mib)9073 ice_do_dcb_reconfig(struct ice_softc *sc, bool pending_mib)
9074 {
9075 	struct ice_aqc_port_ets_elem port_ets = { 0 };
9076 	struct ice_dcbx_cfg *local_dcbx_cfg;
9077 	struct ice_hw *hw = &sc->hw;
9078 	struct ice_port_info *pi;
9079 	device_t dev = sc->dev;
9080 	int status;
9081 
9082 	pi = sc->hw.port_info;
9083 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
9084 
9085 	ice_rdma_notify_dcb_qos_change(sc);
9086 	/* If there's a pending MIB, tell the FW to execute the MIB change
9087 	 * now.
9088 	 */
9089 	if (pending_mib) {
9090 		status = ice_lldp_execute_pending_mib(hw);
9091 		if ((status == ICE_ERR_AQ_ERROR) &&
9092 		    (hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)) {
9093 			device_printf(dev,
9094 			    "Execute Pending LLDP MIB AQ call failed, no pending MIB\n");
9095 		} else if (status) {
9096 			device_printf(dev,
9097 			    "Execute Pending LLDP MIB AQ call failed, err %s aq_err %s\n",
9098 			    ice_status_str(status),
9099 			    ice_aq_str(hw->adminq.sq_last_status));
9100 			/* This won't break traffic, but QoS will not work as expected */
9101 		}
9102 	}
9103 
9104 	/* Set state when there's more than one TC */
9105 	if (ice_dcb_get_num_tc(local_dcbx_cfg) > 1) {
9106 		device_printf(dev, "Multiple traffic classes enabled\n");
9107 		ice_set_state(&sc->state, ICE_STATE_MULTIPLE_TCS);
9108 	} else {
9109 		device_printf(dev, "Multiple traffic classes disabled\n");
9110 		ice_clear_state(&sc->state, ICE_STATE_MULTIPLE_TCS);
9111 	}
9112 
9113 	/* Disable PF VSI since it's going to be reconfigured */
9114 	ice_stop_pf_vsi(sc);
9115 
9116 	/* Query ETS configuration and update SW Tx scheduler info */
9117 	status = ice_query_port_ets(pi, &port_ets, sizeof(port_ets), NULL);
9118 	if (status) {
9119 		device_printf(dev,
9120 		    "Query Port ETS AQ call failed, err %s aq_err %s\n",
9121 		    ice_status_str(status),
9122 		    ice_aq_str(hw->adminq.sq_last_status));
9123 		/* This won't break traffic, but QoS will not work as expected */
9124 	}
9125 
9126 	/* Change PF VSI configuration */
9127 	ice_dcb_recfg(sc);
9128 
9129 	/* Send new configuration to RDMA client driver */
9130 	ice_rdma_dcb_qos_update(sc, pi);
9131 
9132 	ice_request_stack_reinit(sc);
9133 }
9134 
9135 /**
9136  * ice_handle_mib_change_event - helper function to handle LLDP MIB change events
9137  * @sc: the device private softc
9138  * @event: event received on a control queue
9139  *
9140  * Checks the updated MIB it receives and possibly reconfigures the PF LAN
9141  * VSI depending on what has changed. This will also print out some debug
9142  * information about the MIB event if ICE_DBG_DCB is enabled in the debug_mask.
9143  */
9144 static void
ice_handle_mib_change_event(struct ice_softc * sc,struct ice_rq_event_info * event)9145 ice_handle_mib_change_event(struct ice_softc *sc, struct ice_rq_event_info *event)
9146 {
9147 	struct ice_aqc_lldp_get_mib *params =
9148 	    (struct ice_aqc_lldp_get_mib *)&event->desc.params.lldp_get_mib;
9149 	struct ice_dcbx_cfg tmp_dcbx_cfg, *local_dcbx_cfg;
9150 	struct ice_port_info *pi;
9151 	device_t dev = sc->dev;
9152 	struct ice_hw *hw = &sc->hw;
9153 	bool needs_reconfig, mib_is_pending;
9154 	int status;
9155 	u8 mib_type, bridge_type;
9156 
9157 	ASSERT_CFG_LOCKED(sc);
9158 
9159 	ice_debug_print_mib_change_event(sc, event);
9160 
9161 	pi = sc->hw.port_info;
9162 
9163 	mib_type = (params->type & ICE_AQ_LLDP_MIB_TYPE_M) >>
9164 	    ICE_AQ_LLDP_MIB_TYPE_S;
9165 	bridge_type = (params->type & ICE_AQ_LLDP_BRID_TYPE_M) >>
9166 	    ICE_AQ_LLDP_BRID_TYPE_S;
9167 	mib_is_pending = (params->state & ICE_AQ_LLDP_MIB_CHANGE_STATE_M) >>
9168 	    ICE_AQ_LLDP_MIB_CHANGE_STATE_S;
9169 
9170 	/* Ignore if event is not for Nearest Bridge */
9171 	if (bridge_type != ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID)
9172 		return;
9173 
9174 	/* Check MIB Type and return if event for Remote MIB update */
9175 	if (mib_type == ICE_AQ_LLDP_MIB_REMOTE) {
9176 		/* Update the cached remote MIB and return */
9177 		status = ice_aq_get_dcb_cfg(pi->hw, ICE_AQ_LLDP_MIB_REMOTE,
9178 					 ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID,
9179 					 &pi->qos_cfg.remote_dcbx_cfg);
9180 		if (status)
9181 			device_printf(dev,
9182 			    "%s: Failed to get Remote DCB config; status %s, aq_err %s\n",
9183 			    __func__, ice_status_str(status),
9184 			    ice_aq_str(hw->adminq.sq_last_status));
9185 		/* Not fatal if this fails */
9186 		return;
9187 	}
9188 
9189 	/* Save line length by aliasing the local dcbx cfg */
9190 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
9191 	/* Save off the old configuration and clear current config */
9192 	tmp_dcbx_cfg = *local_dcbx_cfg;
9193 	memset(local_dcbx_cfg, 0, sizeof(*local_dcbx_cfg));
9194 
9195 	/* Update the current local_dcbx_cfg with new data */
9196 	if (mib_is_pending) {
9197 		ice_get_dcb_cfg_from_mib_change(pi, event);
9198 	} else {
9199 		/* Get updated DCBX data from firmware */
9200 		status = ice_get_dcb_cfg(pi);
9201 		if (status) {
9202 			device_printf(dev,
9203 			    "%s: Failed to get Local DCB config; status %s, aq_err %s\n",
9204 			    __func__, ice_status_str(status),
9205 			    ice_aq_str(hw->adminq.sq_last_status));
9206 			return;
9207 		}
9208 	}
9209 
9210 	/* Check to see if DCB needs reconfiguring */
9211 	needs_reconfig = ice_dcb_needs_reconfig(sc, &tmp_dcbx_cfg,
9212 	    local_dcbx_cfg);
9213 
9214 	if (!needs_reconfig && !mib_is_pending)
9215 		return;
9216 
9217 	/* Reconfigure -- this will also notify FW that configuration is done,
9218 	 * if the FW MIB change is only pending instead of executed.
9219 	 */
9220 	ice_do_dcb_reconfig(sc, mib_is_pending);
9221 }
9222 
9223 /**
9224  * ice_send_version - Send driver version to firmware
9225  * @sc: the device private softc
9226  *
9227  * Send the driver version to the firmware. This must be called as early as
9228  * possible after ice_init_hw().
9229  */
9230 int
ice_send_version(struct ice_softc * sc)9231 ice_send_version(struct ice_softc *sc)
9232 {
9233 	struct ice_driver_ver driver_version = {0};
9234 	struct ice_hw *hw = &sc->hw;
9235 	device_t dev = sc->dev;
9236 	int status;
9237 
9238 	driver_version.major_ver = ice_major_version;
9239 	driver_version.minor_ver = ice_minor_version;
9240 	driver_version.build_ver = ice_patch_version;
9241 	driver_version.subbuild_ver = ice_rc_version;
9242 
9243 	strlcpy((char *)driver_version.driver_string, ice_driver_version,
9244 		sizeof(driver_version.driver_string));
9245 
9246 	status = ice_aq_send_driver_ver(hw, &driver_version, NULL);
9247 	if (status) {
9248 		device_printf(dev, "Unable to send driver version to firmware, err %s aq_err %s\n",
9249 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
9250 		return (EIO);
9251 	}
9252 
9253 	return (0);
9254 }
9255 
9256 /**
9257  * ice_handle_lan_overflow_event - helper function to log LAN overflow events
9258  * @sc: device softc
9259  * @event: event received on a control queue
9260  *
9261  * Prints out a message when a LAN overflow event is detected on a receive
9262  * queue.
9263  */
9264 static void
ice_handle_lan_overflow_event(struct ice_softc * sc,struct ice_rq_event_info * event)9265 ice_handle_lan_overflow_event(struct ice_softc *sc, struct ice_rq_event_info *event)
9266 {
9267 	struct ice_aqc_event_lan_overflow *params =
9268 	    (struct ice_aqc_event_lan_overflow *)&event->desc.params.lan_overflow;
9269 	struct ice_hw *hw = &sc->hw;
9270 
9271 	ice_debug(hw, ICE_DBG_DCB, "LAN overflow event detected, prtdcb_ruptq=0x%08x, qtx_ctl=0x%08x\n",
9272 		  LE32_TO_CPU(params->prtdcb_ruptq),
9273 		  LE32_TO_CPU(params->qtx_ctl));
9274 }
9275 
9276 /**
9277  * ice_add_ethertype_to_list - Add an Ethertype filter to a filter list
9278  * @vsi: the VSI to target packets to
9279  * @list: the list to add the filter to
9280  * @ethertype: the Ethertype to filter on
9281  * @direction: The direction of the filter (Tx or Rx)
9282  * @action: the action to take
9283  *
9284  * Add an Ethertype filter to a filter list. Used to forward a series of
9285  * filters to the firmware for configuring the switch.
9286  *
9287  * Returns 0 on success, and an error code on failure.
9288  */
9289 static int
ice_add_ethertype_to_list(struct ice_vsi * vsi,struct ice_list_head * list,u16 ethertype,u16 direction,enum ice_sw_fwd_act_type action)9290 ice_add_ethertype_to_list(struct ice_vsi *vsi, struct ice_list_head *list,
9291 			  u16 ethertype, u16 direction,
9292 			  enum ice_sw_fwd_act_type action)
9293 {
9294 	struct ice_fltr_list_entry *entry;
9295 
9296 	MPASS((direction == ICE_FLTR_TX) || (direction == ICE_FLTR_RX));
9297 
9298 	entry = (__typeof(entry))malloc(sizeof(*entry), M_ICE, M_NOWAIT|M_ZERO);
9299 	if (!entry)
9300 		return (ENOMEM);
9301 
9302 	entry->fltr_info.flag = direction;
9303 	entry->fltr_info.src_id = ICE_SRC_ID_VSI;
9304 	entry->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
9305 	entry->fltr_info.fltr_act = action;
9306 	entry->fltr_info.vsi_handle = vsi->idx;
9307 	entry->fltr_info.l_data.ethertype_mac.ethertype = ethertype;
9308 
9309 	LIST_ADD(&entry->list_entry, list);
9310 
9311 	return 0;
9312 }
9313 
9314 #define ETHERTYPE_PAUSE_FRAMES 0x8808
9315 #define ETHERTYPE_LLDP_FRAMES 0x88cc
9316 
9317 /**
9318  * ice_cfg_pf_ethertype_filters - Configure switch to drop ethertypes
9319  * @sc: the device private softc
9320  *
9321  * Configure the switch to drop PAUSE frames and LLDP frames transmitted from
9322  * the host. This prevents malicious VFs from sending these frames and being
9323  * able to control or configure the network.
9324  */
9325 int
ice_cfg_pf_ethertype_filters(struct ice_softc * sc)9326 ice_cfg_pf_ethertype_filters(struct ice_softc *sc)
9327 {
9328 	struct ice_list_head ethertype_list;
9329 	struct ice_vsi *vsi = &sc->pf_vsi;
9330 	struct ice_hw *hw = &sc->hw;
9331 	device_t dev = sc->dev;
9332 	int status;
9333 	int err = 0;
9334 
9335 	INIT_LIST_HEAD(&ethertype_list);
9336 
9337 	/*
9338 	 * Note that the switch filters will ignore the VSI index for the drop
9339 	 * action, so we only need to program drop filters once for the main
9340 	 * VSI.
9341 	 */
9342 
9343 	/* Configure switch to drop all Tx pause frames coming from any VSI. */
9344 	if (sc->enable_tx_fc_filter) {
9345 		err = ice_add_ethertype_to_list(vsi, &ethertype_list,
9346 						ETHERTYPE_PAUSE_FRAMES,
9347 						ICE_FLTR_TX, ICE_DROP_PACKET);
9348 		if (err)
9349 			goto free_ethertype_list;
9350 	}
9351 
9352 	/* Configure switch to drop LLDP frames coming from any VSI */
9353 	if (sc->enable_tx_lldp_filter) {
9354 		err = ice_add_ethertype_to_list(vsi, &ethertype_list,
9355 						ETHERTYPE_LLDP_FRAMES,
9356 						ICE_FLTR_TX, ICE_DROP_PACKET);
9357 		if (err)
9358 			goto free_ethertype_list;
9359 	}
9360 
9361 	status = ice_add_eth_mac(hw, &ethertype_list);
9362 	if (status) {
9363 		device_printf(dev,
9364 			      "Failed to add Tx Ethertype filters, err %s aq_err %s\n",
9365 			      ice_status_str(status),
9366 			      ice_aq_str(hw->adminq.sq_last_status));
9367 		err = (EIO);
9368 	}
9369 
9370 free_ethertype_list:
9371 	ice_free_fltr_list(&ethertype_list);
9372 	return err;
9373 }
9374 
9375 /**
9376  * ice_add_rx_lldp_filter - add ethertype filter for Rx LLDP frames
9377  * @sc: the device private structure
9378  *
9379  * Add a switch ethertype filter which forwards the LLDP frames to the main PF
9380  * VSI. Called when the fw_lldp_agent is disabled, to allow the LLDP frames to
9381  * be forwarded to the stack.
9382  */
9383 void
ice_add_rx_lldp_filter(struct ice_softc * sc)9384 ice_add_rx_lldp_filter(struct ice_softc *sc)
9385 {
9386 	struct ice_list_head ethertype_list;
9387 	struct ice_vsi *vsi = &sc->pf_vsi;
9388 	struct ice_hw *hw = &sc->hw;
9389 	device_t dev = sc->dev;
9390 	int status;
9391 	int err;
9392 	u16 vsi_num;
9393 
9394 	/*
9395 	 * If FW is new enough, use a direct AQ command to perform the filter
9396 	 * addition.
9397 	 */
9398 	if (ice_fw_supports_lldp_fltr_ctrl(hw)) {
9399 		vsi_num = ice_get_hw_vsi_num(hw, vsi->idx);
9400 		status = ice_lldp_fltr_add_remove(hw, vsi_num, true);
9401 		if (status) {
9402 			device_printf(dev,
9403 			    "Failed to add Rx LLDP filter, err %s aq_err %s\n",
9404 			    ice_status_str(status),
9405 			    ice_aq_str(hw->adminq.sq_last_status));
9406 		} else
9407 			ice_set_state(&sc->state,
9408 			    ICE_STATE_LLDP_RX_FLTR_FROM_DRIVER);
9409 		return;
9410 	}
9411 
9412 	INIT_LIST_HEAD(&ethertype_list);
9413 
9414 	/* Forward Rx LLDP frames to the stack */
9415 	err = ice_add_ethertype_to_list(vsi, &ethertype_list,
9416 					ETHERTYPE_LLDP_FRAMES,
9417 					ICE_FLTR_RX, ICE_FWD_TO_VSI);
9418 	if (err) {
9419 		device_printf(dev,
9420 			      "Failed to add Rx LLDP filter, err %s\n",
9421 			      ice_err_str(err));
9422 		goto free_ethertype_list;
9423 	}
9424 
9425 	status = ice_add_eth_mac(hw, &ethertype_list);
9426 	if (status && status != ICE_ERR_ALREADY_EXISTS) {
9427 		device_printf(dev,
9428 			      "Failed to add Rx LLDP filter, err %s aq_err %s\n",
9429 			      ice_status_str(status),
9430 			      ice_aq_str(hw->adminq.sq_last_status));
9431 	} else {
9432 		/*
9433 		 * If status == ICE_ERR_ALREADY_EXISTS, we won't treat an
9434 		 * already existing filter as an error case.
9435 		 */
9436 		ice_set_state(&sc->state, ICE_STATE_LLDP_RX_FLTR_FROM_DRIVER);
9437 	}
9438 
9439 free_ethertype_list:
9440 	ice_free_fltr_list(&ethertype_list);
9441 }
9442 
9443 /**
9444  * ice_del_rx_lldp_filter - Remove ethertype filter for Rx LLDP frames
9445  * @sc: the device private structure
9446  *
9447  * Remove the switch filter forwarding LLDP frames to the main PF VSI, called
9448  * when the firmware LLDP agent is enabled, to stop routing LLDP frames to the
9449  * stack.
9450  */
9451 static void
ice_del_rx_lldp_filter(struct ice_softc * sc)9452 ice_del_rx_lldp_filter(struct ice_softc *sc)
9453 {
9454 	struct ice_list_head ethertype_list;
9455 	struct ice_vsi *vsi = &sc->pf_vsi;
9456 	struct ice_hw *hw = &sc->hw;
9457 	device_t dev = sc->dev;
9458 	int status;
9459 	int err;
9460 	u16 vsi_num;
9461 
9462 	/*
9463 	 * Only in the scenario where the driver added the filter during
9464 	 * this session (while the driver was loaded) would we be able to
9465 	 * delete this filter.
9466 	 */
9467 	if (!ice_test_state(&sc->state, ICE_STATE_LLDP_RX_FLTR_FROM_DRIVER))
9468 		return;
9469 
9470 	/*
9471 	 * If FW is new enough, use a direct AQ command to perform the filter
9472 	 * removal.
9473 	 */
9474 	if (ice_fw_supports_lldp_fltr_ctrl(hw)) {
9475 		vsi_num = ice_get_hw_vsi_num(hw, vsi->idx);
9476 		status = ice_lldp_fltr_add_remove(hw, vsi_num, false);
9477 		if (status) {
9478 			device_printf(dev,
9479 			    "Failed to remove Rx LLDP filter, err %s aq_err %s\n",
9480 			    ice_status_str(status),
9481 			    ice_aq_str(hw->adminq.sq_last_status));
9482 		}
9483 		return;
9484 	}
9485 
9486 	INIT_LIST_HEAD(&ethertype_list);
9487 
9488 	/* Remove filter forwarding Rx LLDP frames to the stack */
9489 	err = ice_add_ethertype_to_list(vsi, &ethertype_list,
9490 					ETHERTYPE_LLDP_FRAMES,
9491 					ICE_FLTR_RX, ICE_FWD_TO_VSI);
9492 	if (err) {
9493 		device_printf(dev,
9494 			      "Failed to remove Rx LLDP filter, err %s\n",
9495 			      ice_err_str(err));
9496 		goto free_ethertype_list;
9497 	}
9498 
9499 	status = ice_remove_eth_mac(hw, &ethertype_list);
9500 	if (status == ICE_ERR_DOES_NOT_EXIST) {
9501 		; /* Don't complain if we try to remove a filter that doesn't exist */
9502 	} else if (status) {
9503 		device_printf(dev,
9504 			      "Failed to remove Rx LLDP filter, err %s aq_err %s\n",
9505 			      ice_status_str(status),
9506 			      ice_aq_str(hw->adminq.sq_last_status));
9507 	}
9508 
9509 free_ethertype_list:
9510 	ice_free_fltr_list(&ethertype_list);
9511 }
9512 
9513 /**
9514  * ice_init_link_configuration -- Setup link in different ways depending
9515  * on whether media is available or not.
9516  * @sc: device private structure
9517  *
9518  * Called at the end of the attach process to either set default link
9519  * parameters if there is media available, or force HW link down and
9520  * set a state bit if there is no media.
9521  */
9522 void
ice_init_link_configuration(struct ice_softc * sc)9523 ice_init_link_configuration(struct ice_softc *sc)
9524 {
9525 	struct ice_port_info *pi = sc->hw.port_info;
9526 	struct ice_hw *hw = &sc->hw;
9527 	device_t dev = sc->dev;
9528 	int status, retry_count = 0;
9529 
9530 retry:
9531 	pi->phy.get_link_info = true;
9532 	status = ice_get_link_status(pi, &sc->link_up);
9533 
9534 	if (status) {
9535 		if (hw->adminq.sq_last_status == ICE_AQ_RC_EAGAIN) {
9536 			retry_count++;
9537 			ice_debug(hw, ICE_DBG_LINK,
9538 			    "%s: ice_get_link_status failed with EAGAIN, attempt %d\n",
9539 			    __func__, retry_count);
9540 			if (retry_count < ICE_LINK_AQ_MAX_RETRIES) {
9541 				ice_msec_pause(ICE_LINK_RETRY_DELAY);
9542 				goto retry;
9543 			}
9544 		} else {
9545 			device_printf(dev,
9546 			    "%s: ice_get_link_status failed; status %s, aq_err %s\n",
9547 			    __func__, ice_status_str(status),
9548 			    ice_aq_str(hw->adminq.sq_last_status));
9549 		}
9550 		return;
9551 	}
9552 
9553 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9554 		ice_clear_state(&sc->state, ICE_STATE_NO_MEDIA);
9555 		/* Apply default link settings */
9556 		if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN)) {
9557 			ice_set_link(sc, false);
9558 			ice_set_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED);
9559 		} else
9560 			ice_apply_saved_phy_cfg(sc, ICE_APPLY_LS_FEC_FC);
9561 	} else {
9562 		 /* Set link down, and poll for media available in timer. This prevents the
9563 		  * driver from receiving spurious link-related events.
9564 		  */
9565 		ice_set_state(&sc->state, ICE_STATE_NO_MEDIA);
9566 		status = ice_aq_set_link_restart_an(pi, false, NULL);
9567 		if (status && hw->adminq.sq_last_status != ICE_AQ_RC_EMODE)
9568 			device_printf(dev,
9569 			    "%s: ice_aq_set_link_restart_an: status %s, aq_err %s\n",
9570 			    __func__, ice_status_str(status),
9571 			    ice_aq_str(hw->adminq.sq_last_status));
9572 	}
9573 }
9574 
9575 /**
9576  * ice_apply_saved_phy_req_to_cfg -- Write saved user PHY settings to cfg data
9577  * @sc: device private structure
9578  * @cfg: new PHY config data to be modified
9579  *
9580  * Applies user settings for advertised speeds to the PHY type fields in the
9581  * supplied PHY config struct. It uses the data from pcaps to check if the
9582  * saved settings are invalid and uses the pcaps data instead if they are
9583  * invalid.
9584  */
9585 static int
ice_apply_saved_phy_req_to_cfg(struct ice_softc * sc,struct ice_aqc_set_phy_cfg_data * cfg)9586 ice_apply_saved_phy_req_to_cfg(struct ice_softc *sc,
9587 			       struct ice_aqc_set_phy_cfg_data *cfg)
9588 {
9589 	struct ice_phy_data phy_data = { 0 };
9590 	struct ice_port_info *pi = sc->hw.port_info;
9591 	u64 phy_low = 0, phy_high = 0;
9592 	u16 link_speeds;
9593 	int ret;
9594 
9595 	link_speeds = pi->phy.curr_user_speed_req;
9596 
9597 	if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_LINK_MGMT_VER_2)) {
9598 		memset(&phy_data, 0, sizeof(phy_data));
9599 		phy_data.report_mode = ICE_AQC_REPORT_DFLT_CFG;
9600 		phy_data.user_speeds_orig = link_speeds;
9601 		ret = ice_intersect_phy_types_and_speeds(sc, &phy_data);
9602 		if (ret != 0) {
9603 			/* Error message already printed within function */
9604 			return (ret);
9605 		}
9606 		phy_low = phy_data.phy_low_intr;
9607 		phy_high = phy_data.phy_high_intr;
9608 
9609 		if (link_speeds == 0 || phy_data.user_speeds_intr)
9610 			goto finalize_link_speed;
9611 		if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_LENIENT_LINK_MODE)) {
9612 			memset(&phy_data, 0, sizeof(phy_data));
9613 			phy_data.report_mode = ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA;
9614 			phy_data.user_speeds_orig = link_speeds;
9615 			ret = ice_intersect_phy_types_and_speeds(sc, &phy_data);
9616 			if (ret != 0) {
9617 				/* Error message already printed within function */
9618 				return (ret);
9619 			}
9620 			phy_low = phy_data.phy_low_intr;
9621 			phy_high = phy_data.phy_high_intr;
9622 
9623 			if (!phy_data.user_speeds_intr) {
9624 				phy_low = phy_data.phy_low_orig;
9625 				phy_high = phy_data.phy_high_orig;
9626 			}
9627 			goto finalize_link_speed;
9628 		}
9629 		/* If we're here, then it means the benefits of Version 2
9630 		 * link management aren't utilized.  We fall through to
9631 		 * handling Strict Link Mode the same as Version 1 link
9632 		 * management.
9633 		 */
9634 	}
9635 
9636 	memset(&phy_data, 0, sizeof(phy_data));
9637 	if ((link_speeds == 0) &&
9638 	    (sc->ldo_tlv.phy_type_low || sc->ldo_tlv.phy_type_high))
9639 		phy_data.report_mode = ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA;
9640 	else
9641 		phy_data.report_mode = ICE_AQC_REPORT_TOPO_CAP_MEDIA;
9642 	phy_data.user_speeds_orig = link_speeds;
9643 	ret = ice_intersect_phy_types_and_speeds(sc, &phy_data);
9644 	if (ret != 0) {
9645 		/* Error message already printed within function */
9646 		return (ret);
9647 	}
9648 	phy_low = phy_data.phy_low_intr;
9649 	phy_high = phy_data.phy_high_intr;
9650 
9651 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_LENIENT_LINK_MODE)) {
9652 		if (phy_low == 0 && phy_high == 0) {
9653 			device_printf(sc->dev,
9654 			    "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
9655 			return (EINVAL);
9656 		}
9657 	} else {
9658 		if (link_speeds == 0) {
9659 			if (sc->ldo_tlv.phy_type_low & phy_low ||
9660 			    sc->ldo_tlv.phy_type_high & phy_high) {
9661 				phy_low &= sc->ldo_tlv.phy_type_low;
9662 				phy_high &= sc->ldo_tlv.phy_type_high;
9663 			}
9664 		} else if (phy_low == 0 && phy_high == 0) {
9665 			memset(&phy_data, 0, sizeof(phy_data));
9666 			phy_data.report_mode = ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA;
9667 			phy_data.user_speeds_orig = link_speeds;
9668 			ret = ice_intersect_phy_types_and_speeds(sc, &phy_data);
9669 			if (ret != 0) {
9670 				/* Error message already printed within function */
9671 				return (ret);
9672 			}
9673 			phy_low = phy_data.phy_low_intr;
9674 			phy_high = phy_data.phy_high_intr;
9675 
9676 			if (!phy_data.user_speeds_intr) {
9677 				phy_low = phy_data.phy_low_orig;
9678 				phy_high = phy_data.phy_high_orig;
9679 			}
9680 		}
9681 	}
9682 
9683 finalize_link_speed:
9684 
9685 	/* Cache new user settings for speeds */
9686 	pi->phy.curr_user_speed_req = phy_data.user_speeds_intr;
9687 	cfg->phy_type_low = htole64(phy_low);
9688 	cfg->phy_type_high = htole64(phy_high);
9689 
9690 	return (ret);
9691 }
9692 
9693 /**
9694  * ice_apply_saved_fec_req_to_cfg -- Write saved user FEC mode to cfg data
9695  * @sc: device private structure
9696  * @cfg: new PHY config data to be modified
9697  *
9698  * Applies user setting for FEC mode to PHY config struct. It uses the data
9699  * from pcaps to check if the saved settings are invalid and uses the pcaps
9700  * data instead if they are invalid.
9701  */
9702 static int
ice_apply_saved_fec_req_to_cfg(struct ice_softc * sc,struct ice_aqc_set_phy_cfg_data * cfg)9703 ice_apply_saved_fec_req_to_cfg(struct ice_softc *sc,
9704 			       struct ice_aqc_set_phy_cfg_data *cfg)
9705 {
9706 	struct ice_port_info *pi = sc->hw.port_info;
9707 	int status;
9708 
9709 	cfg->caps &= ~ICE_AQC_PHY_EN_AUTO_FEC;
9710 	status = ice_cfg_phy_fec(pi, cfg, pi->phy.curr_user_fec_req);
9711 	if (status)
9712 		return (EIO);
9713 
9714 	return (0);
9715 }
9716 
9717 /**
9718  * ice_apply_saved_fc_req_to_cfg -- Write saved user flow control mode to cfg data
9719  * @pi: port info struct
9720  * @cfg: new PHY config data to be modified
9721  *
9722  * Applies user setting for flow control mode to PHY config struct. There are
9723  * no invalid flow control mode settings; if there are, then this function
9724  * treats them like "ICE_FC_NONE".
9725  */
9726 static void
ice_apply_saved_fc_req_to_cfg(struct ice_port_info * pi,struct ice_aqc_set_phy_cfg_data * cfg)9727 ice_apply_saved_fc_req_to_cfg(struct ice_port_info *pi,
9728 			      struct ice_aqc_set_phy_cfg_data *cfg)
9729 {
9730 	cfg->caps &= ~(ICE_AQ_PHY_ENA_TX_PAUSE_ABILITY |
9731 		       ICE_AQ_PHY_ENA_RX_PAUSE_ABILITY);
9732 
9733 	switch (pi->phy.curr_user_fc_req) {
9734 	case ICE_FC_FULL:
9735 		cfg->caps |= ICE_AQ_PHY_ENA_TX_PAUSE_ABILITY |
9736 			     ICE_AQ_PHY_ENA_RX_PAUSE_ABILITY;
9737 		break;
9738 	case ICE_FC_RX_PAUSE:
9739 		cfg->caps |= ICE_AQ_PHY_ENA_RX_PAUSE_ABILITY;
9740 		break;
9741 	case ICE_FC_TX_PAUSE:
9742 		cfg->caps |= ICE_AQ_PHY_ENA_TX_PAUSE_ABILITY;
9743 		break;
9744 	default:
9745 		/* ICE_FC_NONE */
9746 		break;
9747 	}
9748 }
9749 
9750 /**
9751  * ice_apply_saved_phy_cfg -- Re-apply user PHY config settings
9752  * @sc: device private structure
9753  * @settings: which settings to apply
9754  *
9755  * Applies user settings for advertised speeds, FEC mode, and flow
9756  * control mode to a PHY config struct; it uses the data from pcaps
9757  * to check if the saved settings are invalid and uses the pcaps
9758  * data instead if they are invalid.
9759  *
9760  * For things like sysctls where only one setting needs to be
9761  * updated, the bitmap allows the caller to specify which setting
9762  * to update.
9763  */
9764 int
ice_apply_saved_phy_cfg(struct ice_softc * sc,u8 settings)9765 ice_apply_saved_phy_cfg(struct ice_softc *sc, u8 settings)
9766 {
9767 	struct ice_aqc_set_phy_cfg_data cfg = { 0 };
9768 	struct ice_port_info *pi = sc->hw.port_info;
9769 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
9770 	struct ice_hw *hw = &sc->hw;
9771 	device_t dev = sc->dev;
9772 	u64 phy_low, phy_high;
9773 	int status;
9774 	enum ice_fec_mode dflt_fec_mode;
9775 	u16 dflt_user_speed;
9776 
9777 	if (!settings || settings > ICE_APPLY_LS_FEC_FC) {
9778 		ice_debug(hw, ICE_DBG_LINK, "Settings out-of-bounds: %u\n",
9779 		    settings);
9780 	}
9781 
9782 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG,
9783 				     &pcaps, NULL);
9784 	if (status) {
9785 		device_printf(dev,
9786 		    "%s: ice_aq_get_phy_caps (ACTIVE) failed; status %s, aq_err %s\n",
9787 		    __func__, ice_status_str(status),
9788 		    ice_aq_str(hw->adminq.sq_last_status));
9789 		return (EIO);
9790 	}
9791 
9792 	phy_low = le64toh(pcaps.phy_type_low);
9793 	phy_high = le64toh(pcaps.phy_type_high);
9794 
9795 	/* Save off initial config parameters */
9796 	dflt_user_speed = ice_aq_phy_types_to_link_speeds(phy_low, phy_high);
9797 	dflt_fec_mode = ice_caps_to_fec_mode(pcaps.caps, pcaps.link_fec_options);
9798 
9799 	/* Setup new PHY config */
9800 	ice_copy_phy_caps_to_cfg(pi, &pcaps, &cfg);
9801 
9802 	/* On error, restore active configuration values */
9803 	if ((settings & ICE_APPLY_LS) &&
9804 	    ice_apply_saved_phy_req_to_cfg(sc, &cfg)) {
9805 		pi->phy.curr_user_speed_req = dflt_user_speed;
9806 		cfg.phy_type_low = pcaps.phy_type_low;
9807 		cfg.phy_type_high = pcaps.phy_type_high;
9808 	}
9809 	if ((settings & ICE_APPLY_FEC) &&
9810 	    ice_apply_saved_fec_req_to_cfg(sc, &cfg)) {
9811 		pi->phy.curr_user_fec_req = dflt_fec_mode;
9812 	}
9813 	if (settings & ICE_APPLY_FC) {
9814 		/* No real error indicators for this process,
9815 		 * so we'll just have to assume it works. */
9816 		ice_apply_saved_fc_req_to_cfg(pi, &cfg);
9817 	}
9818 
9819 	/* Enable link and re-negotiate it */
9820 	cfg.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
9821 
9822 	status = ice_aq_set_phy_cfg(hw, pi, &cfg, NULL);
9823 	if (status) {
9824 		/* Don't indicate failure if there's no media in the port.
9825 		 * The settings have been saved and will apply when media
9826 		 * is inserted.
9827 		 */
9828 		if ((status == ICE_ERR_AQ_ERROR) &&
9829 		    (hw->adminq.sq_last_status == ICE_AQ_RC_EBUSY)) {
9830 			device_printf(dev,
9831 			    "%s: Setting will be applied when media is inserted\n",
9832 			    __func__);
9833 			return (0);
9834 		} else {
9835 			device_printf(dev,
9836 			    "%s: ice_aq_set_phy_cfg failed; status %s, aq_err %s\n",
9837 			    __func__, ice_status_str(status),
9838 			    ice_aq_str(hw->adminq.sq_last_status));
9839 			return (EIO);
9840 		}
9841 	}
9842 
9843 	return (0);
9844 }
9845 
9846 /**
9847  * ice_print_ldo_tlv - Print out LDO TLV information
9848  * @sc: device private structure
9849  * @tlv: LDO TLV information from the adapter NVM
9850  *
9851  * Dump out the information in tlv to the kernel message buffer; intended for
9852  * debugging purposes.
9853  */
9854 static void
ice_print_ldo_tlv(struct ice_softc * sc,struct ice_link_default_override_tlv * tlv)9855 ice_print_ldo_tlv(struct ice_softc *sc, struct ice_link_default_override_tlv *tlv)
9856 {
9857 	device_t dev = sc->dev;
9858 
9859 	device_printf(dev, "TLV: -options     0x%02x\n", tlv->options);
9860 	device_printf(dev, "     -phy_config  0x%02x\n", tlv->phy_config);
9861 	device_printf(dev, "     -fec_options 0x%02x\n", tlv->fec_options);
9862 	device_printf(dev, "     -phy_high    0x%016llx\n",
9863 	    (unsigned long long)tlv->phy_type_high);
9864 	device_printf(dev, "     -phy_low     0x%016llx\n",
9865 	    (unsigned long long)tlv->phy_type_low);
9866 }
9867 
9868 /**
9869  * ice_set_link_management_mode -- Strict or lenient link management
9870  * @sc: device private structure
9871  *
9872  * Some NVMs give the adapter the option to advertise a superset of link
9873  * configurations.  This checks to see if that option is enabled.
9874  * Further, the NVM could also provide a specific set of configurations
9875  * to try; these are cached in the driver's private structure if they
9876  * are available.
9877  */
9878 void
ice_set_link_management_mode(struct ice_softc * sc)9879 ice_set_link_management_mode(struct ice_softc *sc)
9880 {
9881 	struct ice_port_info *pi = sc->hw.port_info;
9882 	device_t dev = sc->dev;
9883 	struct ice_link_default_override_tlv tlv = { 0 };
9884 	int status;
9885 
9886 	/* Port must be in strict mode if FW version is below a certain
9887 	 * version. (i.e. Don't set lenient mode features)
9888 	 */
9889 	if (!(ice_fw_supports_link_override(&sc->hw)))
9890 		return;
9891 
9892 	status = ice_get_link_default_override(&tlv, pi);
9893 	if (status) {
9894 		device_printf(dev,
9895 		    "%s: ice_get_link_default_override failed; status %s, aq_err %s\n",
9896 		    __func__, ice_status_str(status),
9897 		    ice_aq_str(sc->hw.adminq.sq_last_status));
9898 		return;
9899 	}
9900 
9901 	if (sc->hw.debug_mask & ICE_DBG_LINK)
9902 		ice_print_ldo_tlv(sc, &tlv);
9903 
9904 	/* Set lenient link mode */
9905 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_LENIENT_LINK_MODE) &&
9906 	    (!(tlv.options & ICE_LINK_OVERRIDE_STRICT_MODE)))
9907 		ice_set_bit(ICE_FEATURE_LENIENT_LINK_MODE, sc->feat_en);
9908 
9909 	/* FW supports reporting a default configuration */
9910 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_LINK_MGMT_VER_2) &&
9911 	    ice_fw_supports_report_dflt_cfg(&sc->hw)) {
9912 		ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_2, sc->feat_en);
9913 		/* Knowing we're at a high enough firmware revision to
9914 		 * support this link management configuration, we don't
9915 		 * need to check/support earlier versions.
9916 		 */
9917 		return;
9918 	}
9919 
9920 	/* Default overrides only work if in lenient link mode */
9921 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_LINK_MGMT_VER_1) &&
9922 	    ice_is_bit_set(sc->feat_en, ICE_FEATURE_LENIENT_LINK_MODE) &&
9923 	    (tlv.options & ICE_LINK_OVERRIDE_EN))
9924 		ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_1, sc->feat_en);
9925 
9926 	/* Cache the LDO TLV structure in the driver, since it
9927 	 * won't change during the driver's lifetime.
9928 	 */
9929 	sc->ldo_tlv = tlv;
9930 }
9931 
9932 /**
9933  * ice_set_link -- Set up/down link on phy
9934  * @sc: device private structure
9935  * @enabled: link status to set up
9936  *
9937  * This should be called when change of link status is needed.
9938  */
9939 void
ice_set_link(struct ice_softc * sc,bool enabled)9940 ice_set_link(struct ice_softc *sc, bool enabled)
9941 {
9942 	struct ice_hw *hw = &sc->hw;
9943 	device_t dev = sc->dev;
9944 	int status;
9945 
9946 	if (ice_driver_is_detaching(sc))
9947 		return;
9948 
9949 	if (ice_test_state(&sc->state, ICE_STATE_NO_MEDIA))
9950 		return;
9951 
9952 	if (enabled)
9953 		ice_apply_saved_phy_cfg(sc, ICE_APPLY_LS_FEC_FC);
9954 	else {
9955 		status = ice_aq_set_link_restart_an(hw->port_info, false, NULL);
9956 		if (status) {
9957 			if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
9958 				device_printf(dev,
9959 				    "%s: Link control not enabled in current device mode\n",
9960 				    __func__);
9961 			else
9962 				device_printf(dev,
9963 				    "%s: ice_aq_set_link_restart_an: status %s, aq_err %s\n",
9964 				    __func__, ice_status_str(status),
9965 				    ice_aq_str(hw->adminq.sq_last_status));
9966 		} else
9967 			sc->link_up = false;
9968 	}
9969 }
9970 
9971 /**
9972  * ice_init_saved_phy_cfg -- Set cached user PHY cfg settings with NVM defaults
9973  * @sc: device private structure
9974  *
9975  * This should be called before the tunables for these link settings
9976  * (e.g. advertise_speed) are added -- so that these defaults don't overwrite
9977  * the cached values that the sysctl handlers will write.
9978  *
9979  * This also needs to be called before ice_init_link_configuration, to ensure
9980  * that there are sane values that can be written if there is media available
9981  * in the port.
9982  */
9983 void
ice_init_saved_phy_cfg(struct ice_softc * sc)9984 ice_init_saved_phy_cfg(struct ice_softc *sc)
9985 {
9986 	struct ice_port_info *pi = sc->hw.port_info;
9987 	struct ice_aqc_get_phy_caps_data pcaps = { 0 };
9988 	struct ice_hw *hw = &sc->hw;
9989 	device_t dev = sc->dev;
9990 	int status;
9991 	u64 phy_low, phy_high;
9992 	u8 report_mode = ICE_AQC_REPORT_TOPO_CAP_MEDIA;
9993 
9994 	if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_LINK_MGMT_VER_2))
9995 		report_mode = ICE_AQC_REPORT_DFLT_CFG;
9996 	status = ice_aq_get_phy_caps(pi, false, report_mode, &pcaps, NULL);
9997 	if (status) {
9998 		device_printf(dev,
9999 		    "%s: ice_aq_get_phy_caps (%s) failed; status %s, aq_err %s\n",
10000 		    __func__,
10001 		    report_mode == ICE_AQC_REPORT_DFLT_CFG ? "DFLT" : "w/MEDIA",
10002 		    ice_status_str(status),
10003 		    ice_aq_str(hw->adminq.sq_last_status));
10004 		return;
10005 	}
10006 
10007 	phy_low = le64toh(pcaps.phy_type_low);
10008 	phy_high = le64toh(pcaps.phy_type_high);
10009 
10010 	/* Save off initial config parameters */
10011 	pi->phy.curr_user_speed_req =
10012 	   ice_aq_phy_types_to_link_speeds(phy_low, phy_high);
10013 	pi->phy.curr_user_fec_req = ice_caps_to_fec_mode(pcaps.caps,
10014 	    pcaps.link_fec_options);
10015 	pi->phy.curr_user_fc_req = ice_caps_to_fc_mode(pcaps.caps);
10016 }
10017 
10018 /**
10019  * ice_module_init - Driver callback to handle module load
10020  *
10021  * Callback for handling module load events. This function should initialize
10022  * any data structures that are used for the life of the device driver.
10023  */
10024 static int
ice_module_init(void)10025 ice_module_init(void)
10026 {
10027 	ice_rdma_init();
10028 	return (0);
10029 }
10030 
10031 /**
10032  * ice_module_exit - Driver callback to handle module exit
10033  *
10034  * Callback for handling module unload events. This function should release
10035  * any resources initialized during ice_module_init.
10036  *
10037  * If this function returns non-zero, the module will not be unloaded. It
10038  * should only return such a value if the module cannot be unloaded at all,
10039  * such as due to outstanding memory references that cannot be revoked.
10040  */
10041 static int
ice_module_exit(void)10042 ice_module_exit(void)
10043 {
10044 	ice_rdma_exit();
10045 	return (0);
10046 }
10047 
10048 /**
10049  * ice_module_event_handler - Callback for module events
10050  * @mod: unused module_t parameter
10051  * @what: the event requested
10052  * @arg: unused event argument
10053  *
10054  * Callback used to handle module events from the stack. Used to allow the
10055  * driver to define custom behavior that should happen at module load and
10056  * unload.
10057  */
10058 int
ice_module_event_handler(module_t __unused mod,int what,void __unused * arg)10059 ice_module_event_handler(module_t __unused mod, int what, void __unused *arg)
10060 {
10061 	switch (what) {
10062 	case MOD_LOAD:
10063 		return ice_module_init();
10064 	case MOD_UNLOAD:
10065 		return ice_module_exit();
10066 	default:
10067 		/* TODO: do we need to handle MOD_QUIESCE and MOD_SHUTDOWN? */
10068 		return (EOPNOTSUPP);
10069 	}
10070 }
10071 
10072 /**
10073  * ice_handle_nvm_access_ioctl - Handle an NVM access ioctl request
10074  * @sc: the device private softc
10075  * @ifd: ifdrv ioctl request pointer
10076  */
10077 int
ice_handle_nvm_access_ioctl(struct ice_softc * sc,struct ifdrv * ifd)10078 ice_handle_nvm_access_ioctl(struct ice_softc *sc, struct ifdrv *ifd)
10079 {
10080 	union ice_nvm_access_data *data;
10081 	struct ice_nvm_access_cmd *cmd;
10082 	size_t ifd_len = ifd->ifd_len, malloc_len;
10083 	struct ice_hw *hw = &sc->hw;
10084 	device_t dev = sc->dev;
10085 	int status;
10086 	u8 *nvm_buffer;
10087 	int err;
10088 
10089 	/*
10090 	 * ifioctl forwards SIOCxDRVSPEC to iflib without performing
10091 	 * a privilege check. In turn, iflib forwards the ioctl to the driver
10092 	 * without performing a privilege check. Perform one here to ensure
10093 	 * that non-privileged threads cannot access this interface.
10094 	 */
10095 	err = priv_check(curthread, PRIV_DRIVER);
10096 	if (err)
10097 		return (err);
10098 
10099 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
10100 		device_printf(dev, "%s: Driver must rebuild data structures after a reset. Operation aborted.\n",
10101 			      __func__);
10102 		return (EBUSY);
10103 	}
10104 
10105 	if (ifd_len < sizeof(struct ice_nvm_access_cmd)) {
10106 		device_printf(dev, "%s: ifdrv length is too small. Got %zu, but expected %zu\n",
10107 			      __func__, ifd_len, sizeof(struct ice_nvm_access_cmd));
10108 		return (EINVAL);
10109 	}
10110 
10111 	if (ifd->ifd_data == NULL) {
10112 		device_printf(dev, "%s: ifd data buffer not present.\n",
10113 			      __func__);
10114 		return (EINVAL);
10115 	}
10116 
10117 	/*
10118 	 * If everything works correctly, ice_handle_nvm_access should not
10119 	 * modify data past the size of the ioctl length. However, it could
10120 	 * lead to memory corruption if it did. Make sure to allocate at least
10121 	 * enough space for the command and data regardless. This
10122 	 * ensures that any access to the data union will not access invalid
10123 	 * memory.
10124 	 */
10125 	malloc_len = max(ifd_len, sizeof(*data) + sizeof(*cmd));
10126 
10127 	nvm_buffer = (u8 *)malloc(malloc_len, M_ICE, M_ZERO | M_WAITOK);
10128 	if (!nvm_buffer)
10129 		return (ENOMEM);
10130 
10131 	/* Copy the NVM access command and data in from user space */
10132 	/* coverity[tainted_data_argument] */
10133 	err = copyin(ifd->ifd_data, nvm_buffer, ifd_len);
10134 	if (err) {
10135 		device_printf(dev, "%s: Copying request from user space failed, err %s\n",
10136 			      __func__, ice_err_str(err));
10137 		goto cleanup_free_nvm_buffer;
10138 	}
10139 
10140 	/*
10141 	 * The NVM command structure is immediately followed by data which
10142 	 * varies in size based on the command.
10143 	 */
10144 	cmd = (struct ice_nvm_access_cmd *)nvm_buffer;
10145 	data = (union ice_nvm_access_data *)(nvm_buffer + sizeof(struct ice_nvm_access_cmd));
10146 
10147 	/* Handle the NVM access request */
10148 	status = ice_handle_nvm_access(hw, cmd, data);
10149 	if (status)
10150 		ice_debug(hw, ICE_DBG_NVM,
10151 			  "NVM access request failed, err %s\n",
10152 			  ice_status_str(status));
10153 
10154 	/* Copy the possibly modified contents of the handled request out */
10155 	err = copyout(nvm_buffer, ifd->ifd_data, ifd_len);
10156 	if (err) {
10157 		device_printf(dev, "%s: Copying response back to user space failed, err %s\n",
10158 			      __func__, ice_err_str(err));
10159 		goto cleanup_free_nvm_buffer;
10160 	}
10161 
10162 	/* Convert private status to an error code for proper ioctl response */
10163 	switch (status) {
10164 	case 0:
10165 		err = (0);
10166 		break;
10167 	case ICE_ERR_NO_MEMORY:
10168 		err = (ENOMEM);
10169 		break;
10170 	case ICE_ERR_OUT_OF_RANGE:
10171 		err = (ENOTTY);
10172 		break;
10173 	case ICE_ERR_PARAM:
10174 	default:
10175 		err = (EINVAL);
10176 		break;
10177 	}
10178 
10179 cleanup_free_nvm_buffer:
10180 	free(nvm_buffer, M_ICE);
10181 	return err;
10182 }
10183 
10184 /**
10185  * ice_read_sff_eeprom - Read data from SFF eeprom
10186  * @sc: device softc
10187  * @dev_addr: I2C device address (typically 0xA0 or 0xA2)
10188  * @offset: offset into the eeprom
10189  * @data: pointer to data buffer to store read data in
10190  * @length: length to read; max length is 16
10191  *
10192  * Read from the SFF eeprom in the module for this PF's port. For more details
10193  * on the contents of an SFF eeprom, refer to SFF-8724 (SFP), SFF-8636 (QSFP),
10194  * and SFF-8024 (both).
10195  */
10196 int
ice_read_sff_eeprom(struct ice_softc * sc,u16 dev_addr,u16 offset,u8 * data,u16 length)10197 ice_read_sff_eeprom(struct ice_softc *sc, u16 dev_addr, u16 offset, u8* data, u16 length)
10198 {
10199 	struct ice_hw *hw = &sc->hw;
10200 	int ret = 0, retries = 0;
10201 	int status;
10202 
10203 	if (length > 16)
10204 		return (EINVAL);
10205 
10206 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
10207 		return (ENOSYS);
10208 
10209 	if (ice_test_state(&sc->state, ICE_STATE_NO_MEDIA))
10210 		return (ENXIO);
10211 
10212 	do {
10213 		status = ice_aq_sff_eeprom(hw, 0, dev_addr,
10214 					   offset, 0, 0, data, length,
10215 					   false, NULL);
10216 		if (!status) {
10217 			ret = 0;
10218 			break;
10219 		}
10220 		if (status == ICE_ERR_AQ_ERROR &&
10221 		    hw->adminq.sq_last_status == ICE_AQ_RC_EBUSY) {
10222 			ret = EBUSY;
10223 			continue;
10224 		}
10225 		if (status == ICE_ERR_AQ_ERROR &&
10226 		    hw->adminq.sq_last_status == ICE_AQ_RC_EACCES) {
10227 			/* FW says I2C access isn't supported */
10228 			ret = EACCES;
10229 			break;
10230 		}
10231 		if (status == ICE_ERR_AQ_ERROR &&
10232 		    hw->adminq.sq_last_status == ICE_AQ_RC_EPERM) {
10233 			device_printf(sc->dev,
10234 				  "%s: Module pointer location specified in command does not permit the required operation.\n",
10235 				  __func__);
10236 			ret = EPERM;
10237 			break;
10238 		} else {
10239 			device_printf(sc->dev,
10240 				  "%s: Error reading I2C data: err %s aq_err %s\n",
10241 				  __func__, ice_status_str(status),
10242 				  ice_aq_str(hw->adminq.sq_last_status));
10243 			ret = EIO;
10244 			break;
10245 		}
10246 	} while (retries++ < ICE_I2C_MAX_RETRIES);
10247 
10248 	if (ret == EBUSY)
10249 		device_printf(sc->dev,
10250 			  "%s: Error reading I2C data after %d retries\n",
10251 			  __func__, ICE_I2C_MAX_RETRIES);
10252 
10253 	return (ret);
10254 }
10255 
10256 /**
10257  * ice_handle_i2c_req - Driver independent I2C request handler
10258  * @sc: device softc
10259  * @req: The I2C parameters to use
10260  *
10261  * Read from the port's I2C eeprom using the parameters from the ioctl.
10262  */
10263 int
ice_handle_i2c_req(struct ice_softc * sc,struct ifi2creq * req)10264 ice_handle_i2c_req(struct ice_softc *sc, struct ifi2creq *req)
10265 {
10266 	return ice_read_sff_eeprom(sc, req->dev_addr, req->offset, req->data, req->len);
10267 }
10268 
10269 /**
10270  * ice_sysctl_read_i2c_diag_data - Read some module diagnostic data via i2c
10271  * @oidp: sysctl oid structure
10272  * @arg1: pointer to private data structure
10273  * @arg2: unused
10274  * @req: sysctl request pointer
10275  *
10276  * Read 8 bytes of diagnostic data from the SFF eeprom in the (Q)SFP module
10277  * inserted into the port.
10278  *
10279  *             | SFP A2  | QSFP Lower Page
10280  * ------------|---------|----------------
10281  * Temperature | 96-97	 | 22-23
10282  * Vcc         | 98-99   | 26-27
10283  * TX power    | 102-103 | 34-35..40-41
10284  * RX power    | 104-105 | 50-51..56-57
10285  */
10286 static int
ice_sysctl_read_i2c_diag_data(SYSCTL_HANDLER_ARGS)10287 ice_sysctl_read_i2c_diag_data(SYSCTL_HANDLER_ARGS)
10288 {
10289 	struct ice_softc *sc = (struct ice_softc *)arg1;
10290 	device_t dev = sc->dev;
10291 	struct sbuf *sbuf;
10292 	int ret;
10293 	u8 data[16];
10294 
10295 	UNREFERENCED_PARAMETER(arg2);
10296 	UNREFERENCED_PARAMETER(oidp);
10297 
10298 	if (ice_driver_is_detaching(sc))
10299 		return (ESHUTDOWN);
10300 
10301 	if (req->oldptr == NULL) {
10302 		ret = SYSCTL_OUT(req, 0, 128);
10303 		return (ret);
10304 	}
10305 
10306 	ret = ice_read_sff_eeprom(sc, 0xA0, 0, data, 1);
10307 	if (ret)
10308 		return (ret);
10309 
10310 	/* 0x3 for SFP; 0xD/0x11 for QSFP+/QSFP28 */
10311 	if (data[0] == 0x3) {
10312 		/*
10313 		 * Check for:
10314 		 * - Internally calibrated data
10315 		 * - Diagnostic monitoring is implemented
10316 		 */
10317 		ice_read_sff_eeprom(sc, 0xA0, 92, data, 1);
10318 		if (!(data[0] & 0x60)) {
10319 			device_printf(dev, "Module doesn't support diagnostics: 0xA0[92] = %02X\n", data[0]);
10320 			return (ENODEV);
10321 		}
10322 
10323 		sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
10324 
10325 		ice_read_sff_eeprom(sc, 0xA2, 96, data, 4);
10326 		for (int i = 0; i < 4; i++)
10327 			sbuf_printf(sbuf, "%02X ", data[i]);
10328 
10329 		ice_read_sff_eeprom(sc, 0xA2, 102, data, 4);
10330 		for (int i = 0; i < 4; i++)
10331 			sbuf_printf(sbuf, "%02X ", data[i]);
10332 	} else if (data[0] == 0xD || data[0] == 0x11) {
10333 		/*
10334 		 * QSFP+ modules are always internally calibrated, and must indicate
10335 		 * what types of diagnostic monitoring are implemented
10336 		 */
10337 		sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
10338 
10339 		ice_read_sff_eeprom(sc, 0xA0, 22, data, 2);
10340 		for (int i = 0; i < 2; i++)
10341 			sbuf_printf(sbuf, "%02X ", data[i]);
10342 
10343 		ice_read_sff_eeprom(sc, 0xA0, 26, data, 2);
10344 		for (int i = 0; i < 2; i++)
10345 			sbuf_printf(sbuf, "%02X ", data[i]);
10346 
10347 		ice_read_sff_eeprom(sc, 0xA0, 34, data, 2);
10348 		for (int i = 0; i < 2; i++)
10349 			sbuf_printf(sbuf, "%02X ", data[i]);
10350 
10351 		ice_read_sff_eeprom(sc, 0xA0, 50, data, 2);
10352 		for (int i = 0; i < 2; i++)
10353 			sbuf_printf(sbuf, "%02X ", data[i]);
10354 	} else {
10355 		device_printf(dev, "Module is not SFP/SFP+/SFP28/QSFP+ (%02X)\n", data[0]);
10356 		return (ENODEV);
10357 	}
10358 
10359 	sbuf_finish(sbuf);
10360 	sbuf_delete(sbuf);
10361 
10362 	return (0);
10363 }
10364 
10365 /**
10366  * ice_alloc_intr_tracking - Setup interrupt tracking structures
10367  * @sc: device softc structure
10368  *
10369  * Sets up the resource manager for keeping track of interrupt allocations,
10370  * and initializes the tracking maps for the PF's interrupt allocations.
10371  *
10372  * Unlike the scheme for queues, this is done in one step since both the
10373  * manager and the maps both have the same lifetime.
10374  *
10375  * @returns 0 on success, or an error code on failure.
10376  */
10377 int
ice_alloc_intr_tracking(struct ice_softc * sc)10378 ice_alloc_intr_tracking(struct ice_softc *sc)
10379 {
10380 	struct ice_hw *hw = &sc->hw;
10381 	device_t dev = sc->dev;
10382 	int err;
10383 
10384 	if (hw->func_caps.common_cap.num_msix_vectors > ICE_MAX_MSIX_VECTORS) {
10385 		device_printf(dev, "%s: Invalid num_msix_vectors value (%u) received from FW.\n",
10386 			__func__,
10387 			hw->func_caps.common_cap.num_msix_vectors);
10388 		return (EINVAL);
10389 	}
10390 
10391 	/* Initialize the interrupt allocation manager */
10392 	err = ice_resmgr_init_contig_only(&sc->dev_imgr,
10393 	    hw->func_caps.common_cap.num_msix_vectors);
10394 	if (err) {
10395 		device_printf(dev, "Unable to initialize PF interrupt manager: %s\n",
10396 			      ice_err_str(err));
10397 		return (err);
10398 	}
10399 
10400 	/* Allocate PF interrupt mapping storage */
10401 	if (!(sc->pf_imap =
10402 	      (u16 *)malloc(sizeof(u16) * hw->func_caps.common_cap.num_msix_vectors,
10403 	      M_ICE, M_NOWAIT))) {
10404 		device_printf(dev, "Unable to allocate PF imap memory\n");
10405 		err = ENOMEM;
10406 		goto free_imgr;
10407 	}
10408 	if (!(sc->rdma_imap =
10409 	      (u16 *)malloc(sizeof(u16) * hw->func_caps.common_cap.num_msix_vectors,
10410 	      M_ICE, M_NOWAIT))) {
10411 		device_printf(dev, "Unable to allocate RDMA imap memory\n");
10412 		err = ENOMEM;
10413 		free(sc->pf_imap, M_ICE);
10414 		goto free_imgr;
10415 	}
10416 	for (u32 i = 0; i < hw->func_caps.common_cap.num_msix_vectors; i++) {
10417 		sc->pf_imap[i] = ICE_INVALID_RES_IDX;
10418 		sc->rdma_imap[i] = ICE_INVALID_RES_IDX;
10419 	}
10420 
10421 	return (0);
10422 
10423 free_imgr:
10424 	ice_resmgr_destroy(&sc->dev_imgr);
10425 	return (err);
10426 }
10427 
10428 /**
10429  * ice_free_intr_tracking - Free PF interrupt tracking structures
10430  * @sc: device softc structure
10431  *
10432  * Frees the interrupt resource allocation manager and the PF's owned maps.
10433  *
10434  * VF maps are released when the owning VF's are destroyed, which should always
10435  * happen before this function is called.
10436  */
10437 void
ice_free_intr_tracking(struct ice_softc * sc)10438 ice_free_intr_tracking(struct ice_softc *sc)
10439 {
10440 	if (sc->pf_imap) {
10441 		ice_resmgr_release_map(&sc->dev_imgr, sc->pf_imap,
10442 				       sc->lan_vectors);
10443 		free(sc->pf_imap, M_ICE);
10444 		sc->pf_imap = NULL;
10445 	}
10446 	if (sc->rdma_imap) {
10447 		ice_resmgr_release_map(&sc->dev_imgr, sc->rdma_imap,
10448 				       sc->lan_vectors);
10449 		free(sc->rdma_imap, M_ICE);
10450 		sc->rdma_imap = NULL;
10451 	}
10452 
10453 	ice_resmgr_destroy(&sc->dev_imgr);
10454 
10455 	ice_resmgr_destroy(&sc->os_imgr);
10456 }
10457 
10458 /**
10459  * ice_apply_supported_speed_filter - Mask off unsupported speeds
10460  * @report_speeds: bit-field for the desired link speeds
10461  * @mod_type: type of module/sgmii connection we have
10462  *
10463  * Given a bitmap of the desired lenient mode link speeds,
10464  * this function will mask off the speeds that are not currently
10465  * supported by the device.
10466  */
10467 static u16
ice_apply_supported_speed_filter(u16 report_speeds,u8 mod_type)10468 ice_apply_supported_speed_filter(u16 report_speeds, u8 mod_type)
10469 {
10470 	u16 speed_mask;
10471 	enum { IS_SGMII, IS_SFP, IS_QSFP } module;
10472 
10473 	/*
10474 	 * The SFF specification says 0 is unknown, so we'll
10475 	 * treat it like we're connected through SGMII for now.
10476 	 * This may need revisiting if a new type is supported
10477 	 * in the future.
10478 	 */
10479 	switch (mod_type) {
10480 	case 0:
10481 		module = IS_SGMII;
10482 		break;
10483 	case 3:
10484 		module = IS_SFP;
10485 		break;
10486 	default:
10487 		module = IS_QSFP;
10488 		break;
10489 	}
10490 
10491 	/* We won't offer anything lower than 100M for any part,
10492 	 * but we'll need to mask off other speeds based on the
10493 	 * device and module type.
10494 	 */
10495 	speed_mask = ~((u16)ICE_AQ_LINK_SPEED_100MB - 1);
10496 	if ((report_speeds & ICE_AQ_LINK_SPEED_10GB) && (module == IS_SFP))
10497 		speed_mask = ~((u16)ICE_AQ_LINK_SPEED_1000MB - 1);
10498 	if (report_speeds & ICE_AQ_LINK_SPEED_25GB)
10499 		speed_mask = ~((u16)ICE_AQ_LINK_SPEED_1000MB - 1);
10500 	if (report_speeds & ICE_AQ_LINK_SPEED_50GB) {
10501 		speed_mask = ~((u16)ICE_AQ_LINK_SPEED_1000MB - 1);
10502 		if (module == IS_QSFP)
10503 			speed_mask = ~((u16)ICE_AQ_LINK_SPEED_10GB - 1);
10504 	}
10505 	if ((report_speeds & ICE_AQ_LINK_SPEED_100GB) ||
10506 	    (report_speeds & ICE_AQ_LINK_SPEED_200GB))
10507 		speed_mask = ~((u16)ICE_AQ_LINK_SPEED_25GB - 1);
10508 	return (report_speeds & speed_mask);
10509 }
10510 
10511 /**
10512  * ice_init_health_events - Enable FW health event reporting
10513  * @sc: device softc
10514  *
10515  * Will try to enable firmware health event reporting, but shouldn't
10516  * cause any grief (to the caller) if this fails.
10517  */
10518 void
ice_init_health_events(struct ice_softc * sc)10519 ice_init_health_events(struct ice_softc *sc)
10520 {
10521 	int status;
10522 	u8 health_mask;
10523 
10524 	if ((!ice_is_bit_set(sc->feat_cap, ICE_FEATURE_HEALTH_STATUS)) ||
10525 	    (!sc->enable_health_events))
10526 		return;
10527 
10528 	health_mask = ICE_AQC_HEALTH_STATUS_SET_PF_SPECIFIC_MASK |
10529 		      ICE_AQC_HEALTH_STATUS_SET_GLOBAL_MASK;
10530 
10531 	status = ice_aq_set_health_status_config(&sc->hw, health_mask, NULL);
10532 	if (status)
10533 		device_printf(sc->dev,
10534 		    "Failed to enable firmware health events, err %s aq_err %s\n",
10535 		    ice_status_str(status),
10536 		    ice_aq_str(sc->hw.adminq.sq_last_status));
10537 	else
10538 		ice_set_bit(ICE_FEATURE_HEALTH_STATUS, sc->feat_en);
10539 }
10540 
10541 /**
10542  * ice_print_health_status_string - Print message for given FW health event
10543  * @dev: the PCIe device
10544  * @elem: health status element containing status code
10545  *
10546  * A rather large list of possible health status codes and their associated
10547  * messages.
10548  */
10549 static void
ice_print_health_status_string(device_t dev,struct ice_aqc_health_status_elem * elem)10550 ice_print_health_status_string(device_t dev,
10551 			       struct ice_aqc_health_status_elem *elem)
10552 {
10553 	u16 status_code = le16toh(elem->health_status_code);
10554 
10555 	switch (status_code) {
10556 	case ICE_AQC_HEALTH_STATUS_INFO_RECOVERY:
10557 		device_printf(dev, "The device is in firmware recovery mode.\n");
10558 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10559 		break;
10560 	case ICE_AQC_HEALTH_STATUS_ERR_FLASH_ACCESS:
10561 		device_printf(dev, "The flash chip cannot be accessed.\n");
10562 		device_printf(dev, "Possible Solution: If issue persists, call customer support.\n");
10563 		break;
10564 	case ICE_AQC_HEALTH_STATUS_ERR_NVM_AUTH:
10565 		device_printf(dev, "NVM authentication failed.\n");
10566 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10567 		break;
10568 	case ICE_AQC_HEALTH_STATUS_ERR_OROM_AUTH:
10569 		device_printf(dev, "Option ROM authentication failed.\n");
10570 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10571 		break;
10572 	case ICE_AQC_HEALTH_STATUS_ERR_DDP_AUTH:
10573 		device_printf(dev, "DDP package failed.\n");
10574 		device_printf(dev, "Possible Solution: Update to latest base driver and DDP package.\n");
10575 		break;
10576 	case ICE_AQC_HEALTH_STATUS_ERR_NVM_COMPAT:
10577 		device_printf(dev, "NVM image is incompatible.\n");
10578 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10579 		break;
10580 	case ICE_AQC_HEALTH_STATUS_ERR_OROM_COMPAT:
10581 		device_printf(dev, "Option ROM is incompatible.\n");
10582 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10583 		break;
10584 	case ICE_AQC_HEALTH_STATUS_ERR_DCB_MIB:
10585 		device_printf(dev, "Supplied MIB file is invalid. DCB reverted to default configuration.\n");
10586 		device_printf(dev, "Possible Solution: Disable FW-LLDP and check DCBx system configuration.\n");
10587 		break;
10588 	case ICE_AQC_HEALTH_STATUS_ERR_UNKNOWN_MOD_STRICT:
10589 		device_printf(dev, "An unsupported module was detected.\n");
10590 		device_printf(dev, "Possible Solution 1: Check your cable connection.\n");
10591 		device_printf(dev, "Possible Solution 2: Change or replace the module or cable.\n");
10592 		break;
10593 	case ICE_AQC_HEALTH_STATUS_ERR_MOD_TYPE:
10594 		device_printf(dev, "Module type is not supported.\n");
10595 		device_printf(dev, "Possible Solution: Change or replace the module or cable.\n");
10596 		break;
10597 	case ICE_AQC_HEALTH_STATUS_ERR_MOD_QUAL:
10598 		device_printf(dev, "Module is not qualified.\n");
10599 		device_printf(dev, "Possible Solution 1: Check your cable connection.\n");
10600 		device_printf(dev, "Possible Solution 2: Change or replace the module or cable.\n");
10601 		device_printf(dev, "Possible Solution 3: Manually set speed and duplex.\n");
10602 		break;
10603 	case ICE_AQC_HEALTH_STATUS_ERR_MOD_COMM:
10604 		device_printf(dev, "Device cannot communicate with the module.\n");
10605 		device_printf(dev, "Possible Solution 1: Check your cable connection.\n");
10606 		device_printf(dev, "Possible Solution 2: Change or replace the module or cable.\n");
10607 		device_printf(dev, "Possible Solution 3: Manually set speed and duplex.\n");
10608 		break;
10609 	case ICE_AQC_HEALTH_STATUS_ERR_MOD_CONFLICT:
10610 		device_printf(dev, "Unresolved module conflict.\n");
10611 		device_printf(dev, "Possible Solution 1: Manually set speed/duplex or use Intel(R) Ethernet Port Configuration Tool to change the port option.\n");
10612 		device_printf(dev, "Possible Solution 2: If the problem persists, use a cable/module that is found in the supported modules and cables list for this device.\n");
10613 		break;
10614 	case ICE_AQC_HEALTH_STATUS_ERR_MOD_NOT_PRESENT:
10615 		device_printf(dev, "Module is not present.\n");
10616 		device_printf(dev, "Possible Solution 1: Check that the module is inserted correctly.\n");
10617 		device_printf(dev, "Possible Solution 2: If the problem persists, use a cable/module that is found in the supported modules and cables list for this device.\n");
10618 		break;
10619 	case ICE_AQC_HEALTH_STATUS_INFO_MOD_UNDERUTILIZED:
10620 		device_printf(dev, "Underutilized module.\n");
10621 		device_printf(dev, "Possible Solution 1: Change or replace the module or cable.\n");
10622 		device_printf(dev, "Possible Solution 2: Use Intel(R) Ethernet Port Configuration Tool to change the port option.\n");
10623 		break;
10624 	case ICE_AQC_HEALTH_STATUS_ERR_UNKNOWN_MOD_LENIENT:
10625 		device_printf(dev, "An unsupported module was detected.\n");
10626 		device_printf(dev, "Possible Solution 1: Check your cable connection.\n");
10627 		device_printf(dev, "Possible Solution 2: Change or replace the module or cable.\n");
10628 		device_printf(dev, "Possible Solution 3: Manually set speed and duplex.\n");
10629 		break;
10630 	case ICE_AQC_HEALTH_STATUS_ERR_INVALID_LINK_CFG:
10631 		device_printf(dev, "Invalid link configuration.\n");
10632 		break;
10633 	case ICE_AQC_HEALTH_STATUS_ERR_PORT_ACCESS:
10634 		device_printf(dev, "Port hardware access error.\n");
10635 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10636 		break;
10637 	case ICE_AQC_HEALTH_STATUS_ERR_PORT_UNREACHABLE:
10638 		device_printf(dev, "A port is unreachable.\n");
10639 		device_printf(dev, "Possible Solution 1: Use Intel(R) Ethernet Port Configuration Tool to change the port option.\n");
10640 		device_printf(dev, "Possible Solution 2: Update to the latest NVM image.\n");
10641 		break;
10642 	case ICE_AQC_HEALTH_STATUS_INFO_PORT_SPEED_MOD_LIMITED:
10643 		device_printf(dev, "Port speed is limited due to module.\n");
10644 		device_printf(dev, "Possible Solution: Change the module or use Intel(R) Ethernet Port Configuration Tool to configure the port option to match the current module speed.\n");
10645 		break;
10646 	case ICE_AQC_HEALTH_STATUS_ERR_PARALLEL_FAULT:
10647 		device_printf(dev, "All configured link modes were attempted but failed to establish link.\n");
10648 		device_printf(dev, "The device will restart the process to establish link.\n");
10649 		device_printf(dev, "Possible Solution: Check link partner connection and configuration.\n");
10650 		break;
10651 	case ICE_AQC_HEALTH_STATUS_INFO_PORT_SPEED_PHY_LIMITED:
10652 		device_printf(dev, "Port speed is limited by PHY capabilities.\n");
10653 		device_printf(dev, "Possible Solution 1: Change the module to align to port option.\n");
10654 		device_printf(dev, "Possible Solution 2: Use Intel(R) Ethernet Port Configuration Tool to change the port option.\n");
10655 		break;
10656 	case ICE_AQC_HEALTH_STATUS_ERR_NETLIST_TOPO:
10657 		device_printf(dev, "LOM topology netlist is corrupted.\n");
10658 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10659 		break;
10660 	case ICE_AQC_HEALTH_STATUS_ERR_NETLIST:
10661 		device_printf(dev, "Unrecoverable netlist error.\n");
10662 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10663 		break;
10664 	case ICE_AQC_HEALTH_STATUS_ERR_TOPO_CONFLICT:
10665 		device_printf(dev, "Port topology conflict.\n");
10666 		device_printf(dev, "Possible Solution 1: Use Intel(R) Ethernet Port Configuration Tool to change the port option.\n");
10667 		device_printf(dev, "Possible Solution 2: Update to the latest NVM image.\n");
10668 		break;
10669 	case ICE_AQC_HEALTH_STATUS_ERR_LINK_HW_ACCESS:
10670 		device_printf(dev, "Unrecoverable hardware access error.\n");
10671 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10672 		break;
10673 	case ICE_AQC_HEALTH_STATUS_ERR_LINK_RUNTIME:
10674 		device_printf(dev, "Unrecoverable runtime error.\n");
10675 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10676 		break;
10677 	case ICE_AQC_HEALTH_STATUS_ERR_DNL_INIT:
10678 		device_printf(dev, "Link management engine failed to initialize.\n");
10679 		device_printf(dev, "Possible Solution: Update to the latest NVM image.\n");
10680 		break;
10681 	default:
10682 		break;
10683 	}
10684 }
10685 
10686 /**
10687  * ice_handle_health_status_event - helper function to output health status
10688  * @sc: device softc structure
10689  * @event: event received on a control queue
10690  *
10691  * Prints out the appropriate string based on the given Health Status Event
10692  * code.
10693  */
10694 static void
ice_handle_health_status_event(struct ice_softc * sc,struct ice_rq_event_info * event)10695 ice_handle_health_status_event(struct ice_softc *sc,
10696 			       struct ice_rq_event_info *event)
10697 {
10698 	struct ice_aqc_health_status_elem *health_info;
10699 	u16 status_count;
10700 	int i;
10701 
10702 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_HEALTH_STATUS))
10703 		return;
10704 
10705 	health_info = (struct ice_aqc_health_status_elem *)event->msg_buf;
10706 	status_count = le16toh(event->desc.params.get_health_status.health_status_count);
10707 
10708 	if (status_count > (event->buf_len / sizeof(*health_info))) {
10709 		device_printf(sc->dev, "Received a health status event with invalid event count\n");
10710 		return;
10711 	}
10712 
10713 	for (i = 0; i < status_count; i++) {
10714 		ice_print_health_status_string(sc->dev, health_info);
10715 		health_info++;
10716 	}
10717 }
10718 
10719 /**
10720  * ice_set_default_local_lldp_mib - Possibly apply local LLDP MIB to FW
10721  * @sc: device softc structure
10722  *
10723  * This function needs to be called after link up; it makes sure the FW has
10724  * certain PFC/DCB settings. In certain configurations this will re-apply a
10725  * default local LLDP MIB configuration; this is intended to workaround a FW
10726  * behavior where these settings seem to be cleared on link up.
10727  */
10728 void
ice_set_default_local_lldp_mib(struct ice_softc * sc)10729 ice_set_default_local_lldp_mib(struct ice_softc *sc)
10730 {
10731 	struct ice_hw *hw = &sc->hw;
10732 	struct ice_port_info *pi;
10733 	device_t dev = sc->dev;
10734 	int status;
10735 
10736 	/* Set Local MIB can disrupt flow control settings for
10737 	 * non-DCB-supported devices.
10738 	 */
10739 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_DCB))
10740 		return;
10741 
10742 	pi = hw->port_info;
10743 
10744 	/* Don't overwrite a custom SW configuration */
10745 	if (!pi->qos_cfg.is_sw_lldp &&
10746 	    !ice_test_state(&sc->state, ICE_STATE_MULTIPLE_TCS))
10747 		ice_set_default_local_mib_settings(sc);
10748 
10749 	status = ice_set_dcb_cfg(pi);
10750 
10751 	if (status)
10752 		device_printf(dev,
10753 		    "Error setting Local LLDP MIB: %s aq_err %s\n",
10754 		    ice_status_str(status),
10755 		    ice_aq_str(hw->adminq.sq_last_status));
10756 }
10757 
10758 /**
10759  * ice_sbuf_print_ets_cfg - Helper function to print ETS cfg
10760  * @sbuf: string buffer to print to
10761  * @name: prefix string to use
10762  * @ets: structure to pull values from
10763  *
10764  * A helper function for ice_sysctl_dump_dcbx_cfg(), this
10765  * formats the ETS rec and cfg TLVs into text.
10766  */
10767 static void
ice_sbuf_print_ets_cfg(struct sbuf * sbuf,const char * name,struct ice_dcb_ets_cfg * ets)10768 ice_sbuf_print_ets_cfg(struct sbuf *sbuf, const char *name, struct ice_dcb_ets_cfg *ets)
10769 {
10770 	sbuf_printf(sbuf, "%s.willing: %u\n", name, ets->willing);
10771 	sbuf_printf(sbuf, "%s.cbs: %u\n", name, ets->cbs);
10772 	sbuf_printf(sbuf, "%s.maxtcs: %u\n", name, ets->maxtcs);
10773 
10774 	sbuf_printf(sbuf, "%s.prio_table:", name);
10775 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++)
10776 		sbuf_printf(sbuf, " %d", ets->prio_table[i]);
10777 	sbuf_printf(sbuf, "\n");
10778 
10779 	sbuf_printf(sbuf, "%s.tcbwtable:", name);
10780 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++)
10781 		sbuf_printf(sbuf, " %d", ets->tcbwtable[i]);
10782 	sbuf_printf(sbuf, "\n");
10783 
10784 	sbuf_printf(sbuf, "%s.tsatable:", name);
10785 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++)
10786 		sbuf_printf(sbuf, " %d", ets->tsatable[i]);
10787 	sbuf_printf(sbuf, "\n");
10788 }
10789 
10790 /**
10791  * ice_sysctl_dump_dcbx_cfg - Print out DCBX/DCB config info
10792  * @oidp: sysctl oid structure
10793  * @arg1: pointer to private data structure
10794  * @arg2: AQ define for either Local or Remote MIB
10795  * @req: sysctl request pointer
10796  *
10797  * Prints out DCB/DCBX configuration, including the contents
10798  * of either the local or remote MIB, depending on the value
10799  * used in arg2.
10800  */
10801 static int
ice_sysctl_dump_dcbx_cfg(SYSCTL_HANDLER_ARGS)10802 ice_sysctl_dump_dcbx_cfg(SYSCTL_HANDLER_ARGS)
10803 {
10804 	struct ice_softc *sc = (struct ice_softc *)arg1;
10805 	struct ice_aqc_get_cee_dcb_cfg_resp cee_cfg = {};
10806 	struct ice_dcbx_cfg dcb_buf = {};
10807 	struct ice_dcbx_cfg *dcbcfg;
10808 	struct ice_hw *hw = &sc->hw;
10809 	device_t dev = sc->dev;
10810 	struct sbuf *sbuf;
10811 	int status;
10812 	u8 maxtcs, dcbx_status, is_sw_lldp;
10813 
10814 	UNREFERENCED_PARAMETER(oidp);
10815 
10816 	if (ice_driver_is_detaching(sc))
10817 		return (ESHUTDOWN);
10818 
10819 	is_sw_lldp = hw->port_info->qos_cfg.is_sw_lldp;
10820 
10821 	/* The driver doesn't receive a Remote MIB via SW */
10822 	if (is_sw_lldp && arg2 == ICE_AQ_LLDP_MIB_REMOTE)
10823 		return (ENOENT);
10824 
10825 	dcbcfg = &hw->port_info->qos_cfg.local_dcbx_cfg;
10826 	if (!is_sw_lldp) {
10827 		/* Collect information from the FW in FW LLDP mode */
10828 		dcbcfg = &dcb_buf;
10829 		status = ice_aq_get_dcb_cfg(hw, (u8)arg2,
10830 		    ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID, dcbcfg);
10831 		if (status && arg2 == ICE_AQ_LLDP_MIB_REMOTE &&
10832 		    hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT) {
10833 			device_printf(dev,
10834 			    "Unable to query Remote MIB; port has not received one yet\n");
10835 			return (ENOENT);
10836 		}
10837 		if (status) {
10838 			device_printf(dev, "Unable to query LLDP MIB, err %s aq_err %s\n",
10839 			    ice_status_str(status),
10840 			    ice_aq_str(hw->adminq.sq_last_status));
10841 			return (EIO);
10842 		}
10843 	}
10844 
10845 	status = ice_aq_get_cee_dcb_cfg(hw, &cee_cfg, NULL);
10846 	if (!status)
10847 		dcbcfg->dcbx_mode = ICE_DCBX_MODE_CEE;
10848 	else if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
10849 		dcbcfg->dcbx_mode = ICE_DCBX_MODE_IEEE;
10850 	else
10851 		device_printf(dev, "Get CEE DCB Cfg AQ cmd err %s aq_err %s\n",
10852 		    ice_status_str(status),
10853 		    ice_aq_str(hw->adminq.sq_last_status));
10854 
10855 	maxtcs = hw->func_caps.common_cap.maxtc;
10856 	dcbx_status = ice_get_dcbx_status(hw);
10857 
10858 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
10859 
10860 	/* Do the actual printing */
10861 	sbuf_printf(sbuf, "\n");
10862 	sbuf_printf(sbuf, "SW LLDP mode: %d\n", is_sw_lldp);
10863 	sbuf_printf(sbuf, "Function caps maxtcs: %d\n", maxtcs);
10864 	sbuf_printf(sbuf, "dcbx_status: %d\n", dcbx_status);
10865 
10866 	sbuf_printf(sbuf, "numapps: %u\n", dcbcfg->numapps);
10867 	sbuf_printf(sbuf, "CEE TLV status: %u\n", dcbcfg->tlv_status);
10868 	sbuf_printf(sbuf, "pfc_mode: %s\n", (dcbcfg->pfc_mode == ICE_QOS_MODE_DSCP) ?
10869 	    "DSCP" : "VLAN");
10870 	sbuf_printf(sbuf, "dcbx_mode: %s\n",
10871 	    (dcbcfg->dcbx_mode == ICE_DCBX_MODE_IEEE) ? "IEEE" :
10872 	    (dcbcfg->dcbx_mode == ICE_DCBX_MODE_CEE) ? "CEE" :
10873 	    "Unknown");
10874 
10875 	ice_sbuf_print_ets_cfg(sbuf, "etscfg", &dcbcfg->etscfg);
10876 	ice_sbuf_print_ets_cfg(sbuf, "etsrec", &dcbcfg->etsrec);
10877 
10878 	sbuf_printf(sbuf, "pfc.willing: %u\n", dcbcfg->pfc.willing);
10879 	sbuf_printf(sbuf, "pfc.mbc: %u\n", dcbcfg->pfc.mbc);
10880 	sbuf_printf(sbuf, "pfc.pfccap: 0x%0x\n", dcbcfg->pfc.pfccap);
10881 	sbuf_printf(sbuf, "pfc.pfcena: 0x%0x\n", dcbcfg->pfc.pfcena);
10882 
10883 	if (arg2 == ICE_AQ_LLDP_MIB_LOCAL) {
10884 		sbuf_printf(sbuf, "dscp_map:\n");
10885 		for (int i = 0; i < 8; i++) {
10886 			for (int j = 0; j < 8; j++)
10887 				sbuf_printf(sbuf, " %d",
10888 					    dcbcfg->dscp_map[i * 8 + j]);
10889 			sbuf_printf(sbuf, "\n");
10890 		}
10891 
10892 		sbuf_printf(sbuf, "\nLocal registers:\n");
10893 		sbuf_printf(sbuf, "PRTDCB_GENC.NUMTC: %d\n",
10894 		    (rd32(hw, PRTDCB_GENC) & PRTDCB_GENC_NUMTC_M)
10895 		        >> PRTDCB_GENC_NUMTC_S);
10896 		sbuf_printf(sbuf, "PRTDCB_TUP2TC: 0x%0x\n",
10897 		    (rd32(hw, PRTDCB_TUP2TC)));
10898 		sbuf_printf(sbuf, "PRTDCB_RUP2TC: 0x%0x\n",
10899 		    (rd32(hw, PRTDCB_RUP2TC)));
10900 		sbuf_printf(sbuf, "GLDCB_TC2PFC: 0x%0x\n",
10901 		    (rd32(hw, GLDCB_TC2PFC)));
10902 	}
10903 
10904 	/* Finish */
10905 	sbuf_finish(sbuf);
10906 	sbuf_delete(sbuf);
10907 
10908 	return (0);
10909 }
10910 
10911 /**
10912  * ice_sysctl_dump_vsi_cfg - print PF LAN VSI configuration
10913  * @oidp: sysctl oid structure
10914  * @arg1: pointer to private data structure
10915  * @arg2: unused
10916  * @req: sysctl request pointer
10917  *
10918  * XXX: This could be extended to apply to arbitrary PF-owned VSIs,
10919  * but for simplicity, this only works on the PF's LAN VSI.
10920  */
10921 static int
ice_sysctl_dump_vsi_cfg(SYSCTL_HANDLER_ARGS)10922 ice_sysctl_dump_vsi_cfg(SYSCTL_HANDLER_ARGS)
10923 {
10924 	struct ice_softc *sc = (struct ice_softc *)arg1;
10925 	struct ice_vsi_ctx ctx = { 0 };
10926 	struct ice_hw *hw = &sc->hw;
10927 	device_t dev = sc->dev;
10928 	struct sbuf *sbuf;
10929 	int status;
10930 
10931 	UNREFERENCED_PARAMETER(oidp);
10932 	UNREFERENCED_PARAMETER(arg2);
10933 
10934 	if (ice_driver_is_detaching(sc))
10935 		return (ESHUTDOWN);
10936 
10937 	/* Get HW absolute index of a VSI */
10938 	ctx.vsi_num = ice_get_hw_vsi_num(hw, sc->pf_vsi.idx);
10939 
10940 	status = ice_aq_get_vsi_params(hw, &ctx, NULL);
10941 	if (status) {
10942 		device_printf(dev,
10943 		    "Get VSI AQ call failed, err %s aq_err %s\n",
10944 		    ice_status_str(status),
10945 		    ice_aq_str(hw->adminq.sq_last_status));
10946 		return (EIO);
10947 	}
10948 
10949 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
10950 
10951 	/* Do the actual printing */
10952 	sbuf_printf(sbuf, "\n");
10953 
10954 	sbuf_printf(sbuf, "VSI NUM: %d\n", ctx.vsi_num);
10955 	sbuf_printf(sbuf, "VF  NUM: %d\n", ctx.vf_num);
10956 	sbuf_printf(sbuf, "VSIs allocated: %d\n", ctx.vsis_allocd);
10957 	sbuf_printf(sbuf, "VSIs unallocated: %d\n", ctx.vsis_unallocated);
10958 
10959 	sbuf_printf(sbuf, "Rx Queue Map method: %d\n",
10960 	    LE16_TO_CPU(ctx.info.mapping_flags));
10961 	/* The PF VSI is always contiguous, so there's no if-statement here */
10962 	sbuf_printf(sbuf, "Rx Queue base: %d\n",
10963 	    LE16_TO_CPU(ctx.info.q_mapping[0]));
10964 	sbuf_printf(sbuf, "Rx Queue count: %d\n",
10965 	    LE16_TO_CPU(ctx.info.q_mapping[1]));
10966 
10967 	sbuf_printf(sbuf, "TC qbases  :");
10968 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
10969 		sbuf_printf(sbuf, " %4d",
10970 		    ctx.info.tc_mapping[i] & ICE_AQ_VSI_TC_Q_OFFSET_M);
10971 	}
10972 	sbuf_printf(sbuf, "\n");
10973 
10974 	sbuf_printf(sbuf, "TC qcounts :");
10975 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
10976 		sbuf_printf(sbuf, " %4d",
10977 		    1 << (ctx.info.tc_mapping[i] >> ICE_AQ_VSI_TC_Q_NUM_S));
10978 	}
10979 
10980 	/* Finish */
10981 	sbuf_finish(sbuf);
10982 	sbuf_delete(sbuf);
10983 
10984 	return (0);
10985 }
10986 
10987 /**
10988  * ice_get_tx_rx_equalizations -- read serdes tx rx equalization params
10989  * @hw: pointer to the HW struct
10990  * @serdes_num: represents the serdes number
10991  * @ptr: structure to read all serdes parameter for given serdes
10992  *
10993  * returns all serdes equalization parameter supported per serdes number
10994  */
10995 static int
ice_get_tx_rx_equalizations(struct ice_hw * hw,u8 serdes_num,struct ice_serdes_equalization * ptr)10996 ice_get_tx_rx_equalizations(struct ice_hw *hw, u8 serdes_num,
10997 			    struct ice_serdes_equalization *ptr)
10998 {
10999 	int err = 0;
11000 
11001 	if (!ptr)
11002 		return (EOPNOTSUPP);
11003 
11004 #define ICE_GET_PHY_EQUALIZATION(equ, dir, value) \
11005 	ice_aq_get_phy_equalization(hw, equ, dir, serdes_num, &(ptr->value))
11006 
11007 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_RX_EQU_PRE1,
11008 			ICE_AQC_OP_CODE_RX_EQU, rx_equalization_pre1);
11009 	if (err)
11010 		return err;
11011 
11012 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_RX_EQU_PRE2,
11013 			ICE_AQC_OP_CODE_RX_EQU, rx_equalization_pre2);
11014 	if (err)
11015 		return err;
11016 
11017 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_RX_EQU_POST1,
11018 			ICE_AQC_OP_CODE_RX_EQU, rx_equalization_post1);
11019 	if (err)
11020 		return err;
11021 
11022 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_RX_EQU_BFLF,
11023 			ICE_AQC_OP_CODE_RX_EQU, rx_equalization_bflf);
11024 	if (err)
11025 		return err;
11026 
11027 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_RX_EQU_BFHF,
11028 			ICE_AQC_OP_CODE_RX_EQU, rx_equalization_bfhf);
11029 	if (err)
11030 		return err;
11031 
11032 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_RX_EQU_DRATE,
11033 			ICE_AQC_OP_CODE_RX_EQU, rx_equalization_drate);
11034 	if (err)
11035 		return err;
11036 
11037 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_TX_EQU_PRE1,
11038 			ICE_AQC_OP_CODE_TX_EQU, tx_equalization_pre1);
11039 	if (err)
11040 		return err;
11041 
11042 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_TX_EQU_PRE2,
11043 			ICE_AQC_OP_CODE_TX_EQU, tx_equalization_pre2);
11044 	if (err)
11045 		return err;
11046 
11047 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_TX_EQU_PRE3,
11048 			ICE_AQC_OP_CODE_TX_EQU, tx_equalization_pre3);
11049 	if (err)
11050 		return err;
11051 
11052 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_TX_EQU_ATTEN,
11053 			ICE_AQC_OP_CODE_TX_EQU, tx_equalization_atten);
11054 	if (err)
11055 		return err;
11056 
11057 	err = ICE_GET_PHY_EQUALIZATION(ICE_AQC_TX_EQU_POST1,
11058 			ICE_AQC_OP_CODE_TX_EQU, tx_equalization_post1);
11059 	if (err)
11060 		return err;
11061 
11062 	return (0);
11063 }
11064 
11065 /**
11066  * ice_fec_counter_read - reads FEC stats from PHY
11067  * @hw: pointer to the HW struct
11068  * @receiver_id: pcsquad at registerlevel
11069  * @reg_offset: register for the current request
11070  * @output: pointer to the caller-supplied buffer to return requested fec stats
11071  *
11072  * Returns fec stats from phy
11073  */
11074 static int
ice_fec_counter_read(struct ice_hw * hw,u32 receiver_id,u32 reg_offset,u16 * output)11075 ice_fec_counter_read(struct ice_hw *hw, u32 receiver_id, u32 reg_offset,
11076 			    u16 *output)
11077 {
11078 	u16 flag = (ICE_AQ_FLAG_RD | ICE_AQ_FLAG_BUF | ICE_AQ_FLAG_SI);
11079 	struct ice_sbq_msg_input msg = {};
11080 	int err = 0;
11081 
11082 	memset(&msg, 0, sizeof(msg));
11083 	msg.msg_addr_low = ICE_LO_WORD(reg_offset);
11084 	msg.msg_addr_high = ICE_LO_DWORD(receiver_id);
11085 	msg.opcode = ice_sbq_msg_rd;
11086 	msg.dest_dev = rmn_0;
11087 
11088 	err = ice_sbq_rw_reg(hw, &msg, flag);
11089 	if (err) {
11090 		return err;
11091 	}
11092 	*output = ICE_LO_WORD(msg.data);
11093 	return (0);
11094 }
11095 
11096 /**
11097  * ice_get_port_fec_stats - returns fec correctable, uncorrectable stats per pcsquad, pcsport
11098  * @hw: pointer to the HW struct
11099  * @pcs_quad: pcsquad for input port
11100  * @pcs_port: pcsport for input port
11101  * @fec_stats: buffer to hold fec statistics for given port
11102  *
11103  * Returns fec stats
11104  */
11105 static int
ice_get_port_fec_stats(struct ice_hw * hw,u16 pcs_quad,u16 pcs_port,struct ice_fec_stats_to_sysctl * fec_stats)11106 ice_get_port_fec_stats(struct ice_hw *hw, u16 pcs_quad, u16 pcs_port,
11107 		       struct ice_fec_stats_to_sysctl *fec_stats)
11108 {
11109 	u32 uncorr_low_reg = 0, uncorr_high_reg = 0;
11110 	u16 uncorr_low_val = 0, uncorr_high_val = 0;
11111 	u32 corr_low_reg = 0, corr_high_reg = 0;
11112 	u16 corr_low_val = 0, corr_high_val = 0;
11113 	u32 receiver_id = 0;
11114 	int err;
11115 
11116 	switch (pcs_port) {
11117 	case 0:
11118 		corr_low_reg = ICE_RS_FEC_CORR_LOW_REG_PORT0;
11119 		corr_high_reg = ICE_RS_FEC_CORR_HIGH_REG_PORT0;
11120 		uncorr_low_reg = ICE_RS_FEC_UNCORR_LOW_REG_PORT0;
11121 		uncorr_high_reg = ICE_RS_FEC_UNCORR_HIGH_REG_PORT0;
11122 		break;
11123 	case 1:
11124 		corr_low_reg = ICE_RS_FEC_CORR_LOW_REG_PORT1;
11125 		corr_high_reg = ICE_RS_FEC_CORR_HIGH_REG_PORT1;
11126 		uncorr_low_reg = ICE_RS_FEC_UNCORR_LOW_REG_PORT1;
11127 		uncorr_high_reg = ICE_RS_FEC_UNCORR_HIGH_REG_PORT1;
11128 		break;
11129 	case 2:
11130 		corr_low_reg = ICE_RS_FEC_CORR_LOW_REG_PORT2;
11131 		corr_high_reg = ICE_RS_FEC_CORR_HIGH_REG_PORT2;
11132 		uncorr_low_reg = ICE_RS_FEC_UNCORR_LOW_REG_PORT2;
11133 		uncorr_high_reg = ICE_RS_FEC_UNCORR_HIGH_REG_PORT2;
11134 		break;
11135 	case 3:
11136 		corr_low_reg = ICE_RS_FEC_CORR_LOW_REG_PORT3;
11137 		corr_high_reg = ICE_RS_FEC_CORR_HIGH_REG_PORT3;
11138 		uncorr_low_reg = ICE_RS_FEC_UNCORR_LOW_REG_PORT3;
11139 		uncorr_high_reg = ICE_RS_FEC_UNCORR_HIGH_REG_PORT3;
11140 		break;
11141 	default:
11142 		return (EINVAL);
11143 	}
11144 	if (pcs_quad == 0)
11145 		receiver_id = ICE_RS_FEC_RECEIVER_ID_PCS0; /* MTIP PCS Quad 0 -FEC */
11146 	else if (pcs_quad == 1)
11147 		receiver_id = ICE_RS_FEC_RECEIVER_ID_PCS1; /* MTIP PCS Quad 1 -FEC */
11148 	else
11149 		return (EINVAL);
11150 
11151 	err = ice_fec_counter_read(hw, receiver_id, corr_low_reg,
11152 			&corr_low_val);
11153 	if (err)
11154 		return err;
11155 
11156 	err = ice_fec_counter_read(hw, receiver_id, corr_high_reg,
11157 			&corr_high_val);
11158 	if (err)
11159 		return err;
11160 
11161 	err = ice_fec_counter_read(hw, receiver_id, uncorr_low_reg,
11162 			&uncorr_low_val);
11163 	if (err)
11164 		return err;
11165 
11166 	err = ice_fec_counter_read(hw, receiver_id, uncorr_high_reg,
11167 			&uncorr_high_val);
11168 	if (err)
11169 		return err;
11170 
11171 	fec_stats->fec_corr_cnt_low =  corr_low_val;
11172 	fec_stats->fec_corr_cnt_high =  corr_high_val;
11173 	fec_stats->fec_uncorr_cnt_low =  uncorr_low_val;
11174 	fec_stats->fec_uncorr_cnt_high =  uncorr_high_val;
11175 
11176 	return (0);
11177 }
11178 
11179 /**
11180  * ice_is_serdes_muxed - returns whether serdes is muxed in hardware
11181  * @hw: pointer to the HW struct
11182  *
11183  * Returns True : when serdes is muxed
11184  *         False: when serdes is not muxed
11185  */
11186 static bool
ice_is_serdes_muxed(struct ice_hw * hw)11187 ice_is_serdes_muxed(struct ice_hw *hw)
11188 {
11189 	return (rd32(hw, 0xB81E0) & 0x4);
11190 }
11191 
11192 /**
11193  * ice_get_maxspeed - Get the max speed for given lport
11194  * @hw: pointer to the HW struct
11195  * @lport: logical port for which max speed is requested
11196  * @max_speed: return max speed for input lport
11197  */
11198 static int
ice_get_maxspeed(struct ice_hw * hw,u8 lport,u8 * max_speed)11199 ice_get_maxspeed(struct ice_hw *hw, u8 lport, u8 *max_speed)
11200 {
11201 	struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX] = {};
11202 	u8 option_count = ICE_AQC_PORT_OPT_MAX;
11203 	bool active_valid, pending_valid;
11204 	u8 active_idx, pending_idx;
11205 	int status;
11206 
11207 	status = ice_aq_get_port_options(hw, options, &option_count,
11208 			lport, true, &active_idx, &active_valid,
11209 			&pending_idx, &pending_valid);
11210 
11211 	if (status || active_idx >= ICE_AQC_PORT_OPT_MAX) {
11212 		ice_debug(hw, ICE_DBG_PHY, "Port split read err: %d\n", status);
11213 		return (EIO);
11214 	}
11215 
11216 	if (active_valid) {
11217 		ice_debug(hw, ICE_DBG_PHY, "Active idx: %d\n", active_idx);
11218 	} else {
11219 		ice_debug(hw, ICE_DBG_PHY, "No valid Active option\n");
11220 		return (EINVAL);
11221 	}
11222 	*max_speed = options[active_idx].max_lane_speed;
11223 
11224 	return (0);
11225 }
11226 
11227 /**
11228  * ice_update_port_topology - update port topology
11229  * @lport: logical port for which physical info requested
11230  * @port_topology: buffer to hold port topology
11231  * @is_muxed: serdes is muxed in hardware
11232  */
11233 static int
ice_update_port_topology(u8 lport,struct ice_port_topology * port_topology,bool is_muxed)11234 ice_update_port_topology(u8 lport, struct ice_port_topology *port_topology,
11235 		bool is_muxed)
11236 {
11237 	switch (lport) {
11238 	case 0:
11239 		port_topology->pcs_quad_select = 0;
11240 		port_topology->pcs_port = 0;
11241 		port_topology->primary_serdes_lane = 0;
11242 		break;
11243 	case 1:
11244 		port_topology->pcs_quad_select = 1;
11245 		port_topology->pcs_port = 0;
11246 		if (is_muxed == true)
11247 			port_topology->primary_serdes_lane = 2;
11248 		else
11249 			port_topology->primary_serdes_lane = 4;
11250 		break;
11251 	case 2:
11252 		port_topology->pcs_quad_select = 0;
11253 		port_topology->pcs_port = 1;
11254 		port_topology->primary_serdes_lane = 1;
11255 		break;
11256 	case 3:
11257 		port_topology->pcs_quad_select = 1;
11258 		port_topology->pcs_port = 1;
11259 		if (is_muxed == true)
11260 			port_topology->primary_serdes_lane = 3;
11261 		else
11262 			port_topology->primary_serdes_lane = 5;
11263 		break;
11264 	case 4:
11265 		port_topology->pcs_quad_select = 0;
11266 		port_topology->pcs_port = 2;
11267 		port_topology->primary_serdes_lane = 2;
11268 		break;
11269 	case 5:
11270 		port_topology->pcs_quad_select = 1;
11271 		port_topology->pcs_port = 2;
11272 		port_topology->primary_serdes_lane = 6;
11273 		break;
11274 	case 6:
11275 		port_topology->pcs_quad_select = 0;
11276 		port_topology->pcs_port = 3;
11277 		port_topology->primary_serdes_lane = 3;
11278 		break;
11279 	case 7:
11280 		port_topology->pcs_quad_select = 1;
11281 		port_topology->pcs_port = 3;
11282 		port_topology->primary_serdes_lane = 7;
11283 		break;
11284 	default:
11285 		return (EINVAL);
11286 	}
11287 	return 0;
11288 }
11289 
11290 /**
11291  * ice_get_port_topology - returns physical topology
11292  * @hw: pointer to the HW struct
11293  * @lport: logical port for which physical info requested
11294  * @port_topology: buffer to hold port topology
11295  *
11296  * Returns the physical component associated with the Port like pcsquad, pcsport, serdesnumber
11297  */
11298 static int
ice_get_port_topology(struct ice_hw * hw,u8 lport,struct ice_port_topology * port_topology)11299 ice_get_port_topology(struct ice_hw *hw, u8 lport,
11300 		      struct ice_port_topology *port_topology)
11301 {
11302 	struct ice_aqc_get_link_topo cmd;
11303 	bool is_muxed = false;
11304 	u8 cage_type = 0;
11305 	u16 node_handle;
11306 	u8 ctx = 0;
11307 	int err;
11308 
11309 	if (!hw || !port_topology)
11310 		return (EINVAL);
11311 
11312 	if (hw->device_id >= ICE_DEV_ID_E810_XXV_BACKPLANE) {
11313 		port_topology->serdes_lane_count = 1;
11314 		if (lport == 0) {
11315 			port_topology->pcs_quad_select = 0;
11316 			port_topology->pcs_port = 0;
11317 			port_topology->primary_serdes_lane = 0;
11318 		} else if (lport == 1) {
11319 			port_topology->pcs_quad_select = 1;
11320 			port_topology->pcs_port = 0;
11321 			port_topology->primary_serdes_lane = 1;
11322 		} else {
11323 			return (EINVAL);
11324 		}
11325 		return (0);
11326 	}
11327 
11328 	memset(&cmd, 0, sizeof(cmd));
11329 	ctx = ICE_AQC_LINK_TOPO_NODE_TYPE_CAGE << ICE_AQC_LINK_TOPO_NODE_TYPE_S;
11330 	ctx |= ICE_AQC_LINK_TOPO_NODE_CTX_PORT << ICE_AQC_LINK_TOPO_NODE_CTX_S;
11331 	cmd.addr.topo_params.node_type_ctx = ctx;
11332 	cmd.addr.topo_params.index = 0;
11333 	cmd.addr.topo_params.lport_num = 0;
11334 	cmd.addr.topo_params.lport_num_valid = 0;
11335 
11336 	err = ice_aq_get_netlist_node(hw, &cmd, &cage_type, &node_handle);
11337 	if (err)
11338 		return (EINVAL);
11339 
11340 	is_muxed = ice_is_serdes_muxed(hw);
11341 
11342 	err = ice_update_port_topology(lport, port_topology, is_muxed);
11343 	if (err)
11344 		return err;
11345 
11346 	if (cage_type == 0x11 ||  /* SFP */
11347 	   cage_type == 0x12) {   /* SFP28 */
11348 		port_topology->serdes_lane_count = 1;
11349 	} else if (cage_type == 0x13 ||  /* QSFP */
11350 		  cage_type == 0x14) {   /* QSFP28 */
11351 		u8 max_speed = 0;
11352 
11353 		err = ice_get_maxspeed(hw, port_topology->primary_serdes_lane,
11354 		    &max_speed);
11355 		if (err)
11356 			return err;
11357 
11358 		if (max_speed == ICE_AQC_PORT_OPT_MAX_LANE_M)
11359 			device_printf(ice_hw_to_dev(hw),
11360 			    "%s: WARNING: reported max_lane_speed is N/A\n",
11361 			    __func__);
11362 
11363 		if (max_speed == ICE_AQC_PORT_OPT_MAX_LANE_100G)
11364 			port_topology->serdes_lane_count = 4;
11365 		else if (max_speed == ICE_AQC_PORT_OPT_MAX_LANE_50G)
11366 			port_topology->serdes_lane_count = 2;
11367 		else
11368 			port_topology->serdes_lane_count = 1;
11369 	} else
11370 		return (EINVAL);
11371 
11372 	ice_debug(hw, ICE_DBG_PHY, "%s: Port Topology (lport %d):\n",
11373 	    __func__, lport);
11374 	ice_debug(hw, ICE_DBG_PHY, "serdes lane count %d\n",
11375 	    port_topology->serdes_lane_count);
11376 	ice_debug(hw, ICE_DBG_PHY, "pcs quad select %d\n",
11377 	    port_topology->pcs_quad_select);
11378 	ice_debug(hw, ICE_DBG_PHY, "pcs port %d\n",
11379 	    port_topology->pcs_port);
11380 	ice_debug(hw, ICE_DBG_PHY, "primary serdes lane %d\n",
11381 	    port_topology->primary_serdes_lane);
11382 
11383 	return (0);
11384 }
11385 
11386 /**
11387  * ice_sysctl_dump_phy_stats - print PHY stats
11388  * @oidp: sysctl oid structure
11389  * @arg1: pointer to private data structure
11390  * @arg2: unused
11391  * @req: sysctl request pointer
11392  */
11393 static int
ice_sysctl_dump_phy_stats(SYSCTL_HANDLER_ARGS)11394 ice_sysctl_dump_phy_stats(SYSCTL_HANDLER_ARGS)
11395 {
11396 	struct ice_regdump_to_sysctl ice_prv_regs_buf = {};
11397 	struct ice_softc *sc = (struct ice_softc *)arg1;
11398 	struct ice_port_topology port_topology;
11399 	struct ice_hw *hw = &sc->hw;
11400 	struct ice_port_info *pi;
11401 	device_t dev = sc->dev;
11402 	u8 serdes_num = 0;
11403 	unsigned int i;
11404 	int err = 0;
11405 	struct sbuf *sbuf;
11406 
11407 	pi = hw->port_info;
11408 
11409 	if (!pi) {
11410 		device_printf(dev, "Port info structure is null\n");
11411 		return (EINVAL);
11412 	}
11413 
11414 	UNREFERENCED_PARAMETER(oidp);
11415 	UNREFERENCED_PARAMETER(arg2);
11416 	UNREFERENCED_PARAMETER(req);
11417 
11418 	if (ice_driver_is_detaching(sc))
11419 		return (ESHUTDOWN);
11420 
11421 	if (ice_get_port_topology(hw, pi->lport, &port_topology) != 0) {
11422 		device_printf(dev,
11423 			      "Extended register dump failed for Lport %d\n",
11424 			      pi->lport);
11425 		return (EIO);
11426 	}
11427 
11428 	if (port_topology.serdes_lane_count > ICE_MAX_SERDES_LANE_COUNT) {
11429 		device_printf(dev,
11430 			"Extended register dump failed: Lport %d Serdes count %d\n",
11431 			pi->lport,
11432 			port_topology.serdes_lane_count);
11433 		return (EINVAL);
11434 	}
11435 
11436 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
11437 	/* Get serdes equalization parameter for available serdes */
11438 	for (i = 0; i < port_topology.serdes_lane_count; i++) {
11439 		serdes_num = port_topology.primary_serdes_lane + i;
11440 		err = ice_get_tx_rx_equalizations(hw, serdes_num,
11441 				&(ice_prv_regs_buf.equalization[i]));
11442 		if (err) {
11443 			device_printf(dev,
11444 			    "Serdes equalization get failed Lport %d Serdes %d Err %d\n",
11445 			    pi->lport,serdes_num, err);
11446 			sbuf_finish(sbuf);
11447 			sbuf_delete(sbuf);
11448 			return (EIO);
11449 		}
11450 		sbuf_printf(sbuf, "\nSerdes lane: %d\n", i);
11451 		sbuf_printf(sbuf, "RX PRE1 = %d\n",
11452 			ice_prv_regs_buf.equalization[i].rx_equalization_pre1);
11453 		sbuf_printf(sbuf, "RX PRE2 = %d\n",
11454 			(s16)ice_prv_regs_buf.equalization[i].rx_equalization_pre2);
11455 		sbuf_printf(sbuf, "RX POST1 = %d\n",
11456 			ice_prv_regs_buf.equalization[i].rx_equalization_post1);
11457 		sbuf_printf(sbuf, "RX BFLF = %d\n",
11458 			ice_prv_regs_buf.equalization[i].rx_equalization_bflf);
11459 		sbuf_printf(sbuf, "RX BFHF = %d\n",
11460 			ice_prv_regs_buf.equalization[i].rx_equalization_bfhf);
11461 		sbuf_printf(sbuf, "RX DRATE = %d\n",
11462 			(s16)ice_prv_regs_buf.equalization[i].rx_equalization_drate);
11463 		sbuf_printf(sbuf, "TX PRE1 = %d\n",
11464 			ice_prv_regs_buf.equalization[i].tx_equalization_pre1);
11465 		sbuf_printf(sbuf, "TX PRE2 = %d\n",
11466 			ice_prv_regs_buf.equalization[i].tx_equalization_pre2);
11467 		sbuf_printf(sbuf, "TX PRE3 = %d\n",
11468 			ice_prv_regs_buf.equalization[i].tx_equalization_pre3);
11469 		sbuf_printf(sbuf, "TX POST1 = %d\n",
11470 			ice_prv_regs_buf.equalization[i].tx_equalization_post1);
11471 		sbuf_printf(sbuf, "TX ATTEN = %d\n",
11472 			ice_prv_regs_buf.equalization[i].tx_equalization_atten);
11473 	}
11474 
11475 	/* Get fec  correctable , uncorrectable counter */
11476 	err = ice_get_port_fec_stats(hw, port_topology.pcs_quad_select,
11477 			             port_topology.pcs_port,
11478 				     &(ice_prv_regs_buf.stats));
11479 	if (err) {
11480 		device_printf(dev, "failed to get FEC stats Lport %d Err %d\n",
11481 				pi->lport, err);
11482 		sbuf_finish(sbuf);
11483 		sbuf_delete(sbuf);
11484 		return (EIO);
11485 	}
11486 
11487 	sbuf_printf(sbuf, "\nRS FEC Corrected codeword count = %d\n",
11488 			((u32)ice_prv_regs_buf.stats.fec_corr_cnt_high << 16) |
11489 			    ice_prv_regs_buf.stats.fec_corr_cnt_low);
11490 	sbuf_printf(sbuf, "RS FEC Uncorrected codeword count = %d\n",
11491 			((u32)ice_prv_regs_buf.stats.fec_uncorr_cnt_high << 16) |
11492 			    ice_prv_regs_buf.stats.fec_uncorr_cnt_low);
11493 
11494 	/* Finish */
11495 	sbuf_finish(sbuf);
11496 	sbuf_delete(sbuf);
11497 
11498 	return (0);
11499 }
11500 
11501 /**
11502  * ice_ets_str_to_tbl - Parse string into ETS table
11503  * @str: input string to parse
11504  * @table: output eight values used for ETS values
11505  * @limit: max valid value to accept for ETS values
11506  *
11507  * Parses a string and converts the eight values within
11508  * into a table that can be used in setting ETS settings
11509  * in a MIB.
11510  *
11511  * @return 0 on success, EINVAL if a parsed value is
11512  * not between 0 and limit.
11513  */
11514 static int
ice_ets_str_to_tbl(const char * str,u8 * table,u8 limit)11515 ice_ets_str_to_tbl(const char *str, u8 *table, u8 limit)
11516 {
11517 	const char *str_start = str;
11518 	char *str_end;
11519 	long token;
11520 
11521 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
11522 		token = strtol(str_start, &str_end, 0);
11523 		if (token < 0 || token > limit)
11524 			return (EINVAL);
11525 
11526 		table[i] = (u8)token;
11527 		str_start = (str_end + 1);
11528 	}
11529 
11530 	return (0);
11531 }
11532 
11533 /**
11534  * ice_check_ets_bw - Check if ETS bw vals are valid
11535  * @table: eight values used for ETS bandwidth
11536  *
11537  * @return true if the sum of all 8 values in table
11538  * equals 100.
11539  */
11540 static bool
ice_check_ets_bw(u8 * table)11541 ice_check_ets_bw(u8 *table)
11542 {
11543 	int sum = 0;
11544 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++)
11545 		sum += (int)table[i];
11546 
11547 	return (sum == 100);
11548 }
11549 
11550 /**
11551  * ice_cfg_pba_num - Determine if PBA Number is retrievable
11552  * @sc: the device private softc structure
11553  *
11554  * Sets the feature flag for the existence of a PBA number
11555  * based on the success of the read command.  This does not
11556  * cache the result.
11557  */
11558 void
ice_cfg_pba_num(struct ice_softc * sc)11559 ice_cfg_pba_num(struct ice_softc *sc)
11560 {
11561 	u8 pba_string[32] = "";
11562 
11563 	if ((ice_is_bit_set(sc->feat_cap, ICE_FEATURE_HAS_PBA)) &&
11564 	    (ice_read_pba_string(&sc->hw, pba_string, sizeof(pba_string)) == 0))
11565 		ice_set_bit(ICE_FEATURE_HAS_PBA, sc->feat_en);
11566 }
11567 
11568 /**
11569  * ice_sysctl_query_port_ets - print Port ETS Config from AQ
11570  * @oidp: sysctl oid structure
11571  * @arg1: pointer to private data structure
11572  * @arg2: unused
11573  * @req: sysctl request pointer
11574  */
11575 static int
ice_sysctl_query_port_ets(SYSCTL_HANDLER_ARGS)11576 ice_sysctl_query_port_ets(SYSCTL_HANDLER_ARGS)
11577 {
11578 	struct ice_softc *sc = (struct ice_softc *)arg1;
11579 	struct ice_aqc_port_ets_elem port_ets = { 0 };
11580 	struct ice_hw *hw = &sc->hw;
11581 	struct ice_port_info *pi;
11582 	device_t dev = sc->dev;
11583 	struct sbuf *sbuf;
11584 	int status;
11585 	int i = 0;
11586 
11587 	UNREFERENCED_PARAMETER(oidp);
11588 	UNREFERENCED_PARAMETER(arg2);
11589 
11590 	if (ice_driver_is_detaching(sc))
11591 		return (ESHUTDOWN);
11592 
11593 	pi = hw->port_info;
11594 
11595 	status = ice_aq_query_port_ets(pi, &port_ets, sizeof(port_ets), NULL);
11596 	if (status) {
11597 		device_printf(dev,
11598 		    "Query Port ETS AQ call failed, err %s aq_err %s\n",
11599 		    ice_status_str(status),
11600 		    ice_aq_str(hw->adminq.sq_last_status));
11601 		return (EIO);
11602 	}
11603 
11604 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
11605 
11606 	/* Do the actual printing */
11607 	sbuf_printf(sbuf, "\n");
11608 
11609 	sbuf_printf(sbuf, "Valid TC map: 0x%x\n", port_ets.tc_valid_bits);
11610 
11611 	sbuf_printf(sbuf, "TC BW %%:");
11612 	ice_for_each_traffic_class(i) {
11613 		sbuf_printf(sbuf, " %3d", port_ets.tc_bw_share[i]);
11614 	}
11615 	sbuf_printf(sbuf, "\n");
11616 
11617 	sbuf_printf(sbuf, "EIR profile ID: %d\n", port_ets.port_eir_prof_id);
11618 	sbuf_printf(sbuf, "CIR profile ID: %d\n", port_ets.port_cir_prof_id);
11619 	sbuf_printf(sbuf, "TC Node prio: 0x%x\n", port_ets.tc_node_prio);
11620 
11621 	sbuf_printf(sbuf, "TC Node TEIDs:\n");
11622 	ice_for_each_traffic_class(i) {
11623 		sbuf_printf(sbuf, "%d: %d\n", i, port_ets.tc_node_teid[i]);
11624 	}
11625 
11626 	/* Finish */
11627 	sbuf_finish(sbuf);
11628 	sbuf_delete(sbuf);
11629 
11630 	return (0);
11631 }
11632 
11633 /**
11634  * ice_sysctl_dscp2tc_map - Map DSCP to hardware TCs
11635  * @oidp: sysctl oid structure
11636  * @arg1: pointer to private data structure
11637  * @arg2: which eight DSCP to UP mappings to configure (0 - 7)
11638  * @req: sysctl request pointer
11639  *
11640  * Gets or sets the current DSCP to UP table cached by the driver. Since there
11641  * are 64 possible DSCP values to configure, this sysctl only configures
11642  * chunks of 8 in that space at a time.
11643  *
11644  * This sysctl is only relevant in DSCP mode, and will only function in SW DCB
11645  * mode.
11646  */
11647 static int
ice_sysctl_dscp2tc_map(SYSCTL_HANDLER_ARGS)11648 ice_sysctl_dscp2tc_map(SYSCTL_HANDLER_ARGS)
11649 {
11650 	struct ice_softc *sc = (struct ice_softc *)arg1;
11651 	struct ice_dcbx_cfg *local_dcbx_cfg;
11652 	struct ice_port_info *pi;
11653 	struct ice_hw *hw = &sc->hw;
11654 	device_t dev = sc->dev;
11655 	int status;
11656 	struct sbuf *sbuf;
11657 	int ret;
11658 
11659 	/* Store input rates from user */
11660 	char dscp_user_buf[128] = "";
11661 	u8 new_dscp_table_seg[ICE_MAX_TRAFFIC_CLASS] = {};
11662 
11663 	if (ice_driver_is_detaching(sc))
11664 		return (ESHUTDOWN);
11665 
11666 	if (req->oldptr == NULL && req->newptr == NULL) {
11667 		ret = SYSCTL_OUT(req, 0, 128);
11668 		return (ret);
11669 	}
11670 
11671 	pi = hw->port_info;
11672 	local_dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
11673 
11674 	sbuf = sbuf_new(NULL, dscp_user_buf, 128, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
11675 
11676 	/* Format DSCP-to-UP data for output */
11677 	for (int i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
11678 		sbuf_printf(sbuf, "%d", local_dcbx_cfg->dscp_map[arg2 * 8 + i]);
11679 		if (i != ICE_MAX_TRAFFIC_CLASS - 1)
11680 			sbuf_printf(sbuf, ",");
11681 	}
11682 
11683 	sbuf_finish(sbuf);
11684 	sbuf_delete(sbuf);
11685 
11686 	/* Read in the new DSCP mapping values */
11687 	ret = sysctl_handle_string(oidp, dscp_user_buf, sizeof(dscp_user_buf), req);
11688 	if ((ret) || (req->newptr == NULL))
11689 		return (ret);
11690 
11691 	/* Don't allow setting changes in FW DCB mode */
11692 	if (!hw->port_info->qos_cfg.is_sw_lldp) {
11693 		device_printf(dev, "%s: DSCP mapping is not allowed in FW DCBX mode\n",
11694 		    __func__);
11695 		return (EINVAL);
11696 	}
11697 
11698 	/* Convert 8 values in a string to a table; this is similar to what
11699 	 * needs to be done for ETS settings, so this function can be re-used
11700 	 * for that purpose.
11701 	 */
11702 	ret = ice_ets_str_to_tbl(dscp_user_buf, new_dscp_table_seg,
11703 	    ICE_MAX_TRAFFIC_CLASS - 1);
11704 	if (ret) {
11705 		device_printf(dev, "%s: Could not parse input DSCP2TC table: %s\n",
11706 		    __func__, dscp_user_buf);
11707 		return (ret);
11708 	}
11709 
11710 	memcpy(&local_dcbx_cfg->dscp_map[arg2 * 8], new_dscp_table_seg,
11711 	    sizeof(new_dscp_table_seg));
11712 
11713 	local_dcbx_cfg->app_mode = ICE_DCBX_APPS_NON_WILLING;
11714 
11715 	status = ice_set_dcb_cfg(pi);
11716 	if (status) {
11717 		device_printf(dev,
11718 		    "%s: Failed to set DCB config; status %s, aq_err %s\n",
11719 		    __func__, ice_status_str(status),
11720 		    ice_aq_str(hw->adminq.sq_last_status));
11721 		return (EIO);
11722 	}
11723 
11724 	ice_do_dcb_reconfig(sc, false);
11725 
11726 	return (0);
11727 }
11728 
11729 /**
11730  * ice_handle_debug_dump_ioctl - Handle a debug dump ioctl request
11731  * @sc: the device private softc
11732  * @ifd: ifdrv ioctl request pointer
11733  */
11734 int
ice_handle_debug_dump_ioctl(struct ice_softc * sc,struct ifdrv * ifd)11735 ice_handle_debug_dump_ioctl(struct ice_softc *sc, struct ifdrv *ifd)
11736 {
11737 	size_t ifd_len = ifd->ifd_len;
11738 	struct ice_hw *hw = &sc->hw;
11739 	device_t dev = sc->dev;
11740 	struct ice_debug_dump_cmd *ddc;
11741 	int status;
11742 	int err = 0;
11743 
11744 	/* Returned arguments from the Admin Queue */
11745 	u16 ret_buf_size = 0;
11746 	u16 ret_next_cluster = 0;
11747 	u16 ret_next_table = 0;
11748 	u32 ret_next_index = 0;
11749 
11750 	/*
11751 	 * ifioctl forwards SIOCxDRVSPEC to iflib without performing
11752 	 * a privilege check. In turn, iflib forwards the ioctl to the driver
11753 	 * without performing a privilege check. Perform one here to ensure
11754 	 * that non-privileged threads cannot access this interface.
11755 	 */
11756 	err = priv_check(curthread, PRIV_DRIVER);
11757 	if (err)
11758 		return (err);
11759 
11760 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
11761 		device_printf(dev,
11762 		    "%s: Driver must rebuild data structures after a reset. Operation aborted.\n",
11763 		    __func__);
11764 		return (EBUSY);
11765 	}
11766 
11767 	if (ifd_len < sizeof(*ddc)) {
11768 		device_printf(dev,
11769 		    "%s: ifdrv length is too small. Got %zu, but expected %zu\n",
11770 		    __func__, ifd_len, sizeof(*ddc));
11771 		return (EINVAL);
11772 	}
11773 
11774 	if (ifd->ifd_data == NULL) {
11775 		device_printf(dev, "%s: ifd data buffer not present.\n",
11776 		     __func__);
11777 		return (EINVAL);
11778 	}
11779 
11780 	ddc = (struct ice_debug_dump_cmd *)malloc(ifd_len, M_ICE, M_ZERO | M_NOWAIT);
11781 	if (!ddc)
11782 		return (ENOMEM);
11783 
11784 	/* Copy the NVM access command and data in from user space */
11785 	/* coverity[tainted_data_argument] */
11786 	err = copyin(ifd->ifd_data, ddc, ifd_len);
11787 	if (err) {
11788 		device_printf(dev, "%s: Copying request from user space failed, err %s\n",
11789 			      __func__, ice_err_str(err));
11790 		goto out;
11791 	}
11792 
11793 	/* The data_size arg must be at least 1 for the AQ cmd to work */
11794 	if (ddc->data_size == 0) {
11795 		device_printf(dev,
11796 		    "%s: data_size must be greater than 0\n", __func__);
11797 		err = EINVAL;
11798 		goto out;
11799 	}
11800 	/* ...and it can't be too long */
11801 	if (ddc->data_size > (ifd_len - sizeof(*ddc))) {
11802 		device_printf(dev,
11803 		    "%s: data_size (%d) is larger than ifd_len space (%zu)?\n", __func__,
11804 		    ddc->data_size, ifd_len - sizeof(*ddc));
11805 		err = EINVAL;
11806 		goto out;
11807 	}
11808 
11809 	/* Make sure any possible data buffer space is zeroed */
11810 	memset(ddc->data, 0, ifd_len - sizeof(*ddc));
11811 
11812 	status = ice_aq_get_internal_data(hw, ddc->cluster_id, ddc->table_id, ddc->offset,
11813 	    (u8 *)ddc->data, ddc->data_size, &ret_buf_size,
11814 	    &ret_next_cluster, &ret_next_table, &ret_next_index, NULL);
11815 	ice_debug(hw, ICE_DBG_DIAG, "%s: ret_buf_size %d, ret_next_table %d, ret_next_index %d\n",
11816 	    __func__, ret_buf_size, ret_next_table, ret_next_index);
11817 	if (status) {
11818 		device_printf(dev,
11819 		    "%s: Get Internal Data AQ command failed, err %s aq_err %s\n",
11820 		    __func__,
11821 		    ice_status_str(status),
11822 		    ice_aq_str(hw->adminq.sq_last_status));
11823 		goto aq_error;
11824 	}
11825 
11826 	ddc->table_id = ret_next_table;
11827 	ddc->offset = ret_next_index;
11828 	ddc->data_size = ret_buf_size;
11829 	ddc->cluster_id = ret_next_cluster;
11830 
11831 	/* Copy the possibly modified contents of the handled request out */
11832 	err = copyout(ddc, ifd->ifd_data, ifd->ifd_len);
11833 	if (err) {
11834 		device_printf(dev, "%s: Copying response back to user space failed, err %s\n",
11835 			      __func__, ice_err_str(err));
11836 		goto out;
11837 	}
11838 
11839 aq_error:
11840 	/* Convert private status to an error code for proper ioctl response */
11841 	switch (status) {
11842 	case 0:
11843 		err = (0);
11844 		break;
11845 	case ICE_ERR_NO_MEMORY:
11846 		err = (ENOMEM);
11847 		break;
11848 	case ICE_ERR_OUT_OF_RANGE:
11849 		err = (ENOTTY);
11850 		break;
11851 	case ICE_ERR_AQ_ERROR:
11852 		err = (EIO);
11853 		break;
11854 	case ICE_ERR_PARAM:
11855 	default:
11856 		err = (EINVAL);
11857 		break;
11858 	}
11859 
11860 out:
11861 	free(ddc, M_ICE);
11862 	return (err);
11863 }
11864 
11865 /**
11866  * ice_sysctl_allow_no_fec_mod_in_auto - Change Auto FEC behavior
11867  * @oidp: sysctl oid structure
11868  * @arg1: pointer to private data structure
11869  * @arg2: unused
11870  * @req: sysctl request pointer
11871  *
11872  * Allows user to let "No FEC" mode to be used in "Auto"
11873  * FEC mode during FEC negotiation. This is only supported
11874  * on newer firmware versions.
11875  */
11876 static int
ice_sysctl_allow_no_fec_mod_in_auto(SYSCTL_HANDLER_ARGS)11877 ice_sysctl_allow_no_fec_mod_in_auto(SYSCTL_HANDLER_ARGS)
11878 {
11879 	struct ice_softc *sc = (struct ice_softc *)arg1;
11880 	struct ice_hw *hw = &sc->hw;
11881 	device_t dev = sc->dev;
11882 	u8 user_flag;
11883 	int ret;
11884 
11885 	UNREFERENCED_PARAMETER(arg2);
11886 
11887 	ret = priv_check(curthread, PRIV_DRIVER);
11888 	if (ret)
11889 		return (ret);
11890 
11891 	if (ice_driver_is_detaching(sc))
11892 		return (ESHUTDOWN);
11893 
11894 	user_flag = (u8)sc->allow_no_fec_mod_in_auto;
11895 
11896 	ret = sysctl_handle_bool(oidp, &user_flag, 0, req);
11897 	if ((ret) || (req->newptr == NULL))
11898 		return (ret);
11899 
11900 	if (!ice_fw_supports_fec_dis_auto(hw)) {
11901 		log(LOG_INFO,
11902 		    "%s: Enabling or disabling of auto configuration of modules that don't support FEC is unsupported by the current firmware\n",
11903 		    device_get_nameunit(dev));
11904 		return (ENODEV);
11905 	}
11906 
11907 	if (user_flag == (bool)sc->allow_no_fec_mod_in_auto)
11908 		return (0);
11909 
11910 	sc->allow_no_fec_mod_in_auto = (u8)user_flag;
11911 
11912 	if (sc->allow_no_fec_mod_in_auto)
11913 		log(LOG_INFO, "%s: Enabled auto configuration of No FEC modules\n",
11914 		    device_get_nameunit(dev));
11915 	else
11916 		log(LOG_INFO,
11917 		    "%s: Auto configuration of No FEC modules reset to NVM defaults\n",
11918 		    device_get_nameunit(dev));
11919 
11920 	return (0);
11921 }
11922 
11923 /**
11924  * ice_sysctl_temperature - Retrieve NIC temp via AQ command
11925  * @oidp: sysctl oid structure
11926  * @arg1: pointer to private data structure
11927  * @arg2: unused
11928  * @req: sysctl request pointer
11929  *
11930  * If ICE_DBG_DIAG is set in the debug.debug_mask sysctl, then this will print
11931  * temperature threshold information in the kernel message log, too.
11932  */
11933 static int
ice_sysctl_temperature(SYSCTL_HANDLER_ARGS)11934 ice_sysctl_temperature(SYSCTL_HANDLER_ARGS)
11935 {
11936 	struct ice_aqc_get_sensor_reading_resp resp;
11937 	struct ice_softc *sc = (struct ice_softc *)arg1;
11938 	struct ice_hw *hw = &sc->hw;
11939 	device_t dev = sc->dev;
11940 	int status;
11941 
11942 	UNREFERENCED_PARAMETER(oidp);
11943 	UNREFERENCED_PARAMETER(arg2);
11944 
11945 	if (ice_driver_is_detaching(sc))
11946 		return (ESHUTDOWN);
11947 
11948 	status = ice_aq_get_sensor_reading(hw, ICE_AQC_INT_TEMP_SENSOR,
11949 	    ICE_AQC_INT_TEMP_FORMAT, &resp, NULL);
11950 	if (status) {
11951 		device_printf(dev,
11952 		    "Get Sensor Reading AQ call failed, err %s aq_err %s\n",
11953 		    ice_status_str(status),
11954 		    ice_aq_str(hw->adminq.sq_last_status));
11955 		return (EIO);
11956 	}
11957 
11958 	ice_debug(hw, ICE_DBG_DIAG, "%s: Warning Temp Threshold: %d\n", __func__,
11959 	    resp.data.s0f0.temp_warning_threshold);
11960 	ice_debug(hw, ICE_DBG_DIAG, "%s: Critical Temp Threshold: %d\n", __func__,
11961 	    resp.data.s0f0.temp_critical_threshold);
11962 	ice_debug(hw, ICE_DBG_DIAG, "%s: Fatal Temp Threshold: %d\n", __func__,
11963 	    resp.data.s0f0.temp_fatal_threshold);
11964 
11965 	return sysctl_handle_8(oidp, &resp.data.s0f0.temp, 0, req);
11966 }
11967 
11968 /**
11969  * ice_sysctl_create_mirror_interface - Create a new ifnet that monitors
11970  *     traffic from the main PF VSI
11971  */
11972 static int
ice_sysctl_create_mirror_interface(SYSCTL_HANDLER_ARGS)11973 ice_sysctl_create_mirror_interface(SYSCTL_HANDLER_ARGS)
11974 {
11975 	struct ice_softc *sc = (struct ice_softc *)arg1;
11976 	device_t dev = sc->dev;
11977 	int ret;
11978 
11979 	UNREFERENCED_PARAMETER(arg2);
11980 
11981 	ret = priv_check(curthread, PRIV_DRIVER);
11982 	if (ret)
11983 		return (ret);
11984 
11985 	if (ice_driver_is_detaching(sc))
11986 		return (ESHUTDOWN);
11987 
11988 	/* If the user hasn't written "1" to this sysctl yet: */
11989 	if (!ice_test_state(&sc->state, ICE_STATE_DO_CREATE_MIRR_INTFC)) {
11990 		/* Avoid output on the first set of reads to this sysctl in
11991 		 * order to prevent a null byte from being written to the
11992 		 * end result when called via sysctl(8).
11993 		 */
11994 		if (req->oldptr == NULL && req->newptr == NULL) {
11995 			ret = SYSCTL_OUT(req, 0, 0);
11996 			return (ret);
11997 		}
11998 
11999 		char input_buf[2] = "";
12000 		ret = sysctl_handle_string(oidp, input_buf, sizeof(input_buf), req);
12001 		if ((ret) || (req->newptr == NULL))
12002 			return (ret);
12003 
12004 		/* If we get '1', then indicate we'll create the interface in
12005 		 * the next sysctl read call.
12006 		 */
12007 		if (input_buf[0] == '1') {
12008 			if (sc->mirr_if) {
12009 				device_printf(dev,
12010 				    "Mirror interface %s already exists!\n",
12011 				    if_name(sc->mirr_if->ifp));
12012 				return (EEXIST);
12013 			}
12014 			ice_set_state(&sc->state, ICE_STATE_DO_CREATE_MIRR_INTFC);
12015 			return (0);
12016 		}
12017 
12018 		return (EINVAL);
12019 	}
12020 
12021 	/* --- "Do Create Mirror Interface" is set --- */
12022 
12023 	/* Caller just wants the upper bound for size */
12024 	if (req->oldptr == NULL && req->newptr == NULL) {
12025 		ret = SYSCTL_OUT(req, 0, 128);
12026 		return (ret);
12027 	}
12028 
12029 	device_printf(dev, "Creating new mirroring interface...\n");
12030 
12031 	ret = ice_create_mirror_interface(sc);
12032 	if (ret)
12033 		return (ret);
12034 
12035 	ice_clear_state(&sc->state, ICE_STATE_DO_CREATE_MIRR_INTFC);
12036 
12037 	ret = sysctl_handle_string(oidp, __DECONST(char *, "Interface attached"), 0, req);
12038 	return (ret);
12039 }
12040 
12041 /**
12042  * ice_sysctl_destroy_mirror_interface - Destroy network interface that monitors
12043  *     traffic from the main PF VSI
12044  */
12045 static int
ice_sysctl_destroy_mirror_interface(SYSCTL_HANDLER_ARGS)12046 ice_sysctl_destroy_mirror_interface(SYSCTL_HANDLER_ARGS)
12047 {
12048 	struct ice_softc *sc = (struct ice_softc *)arg1;
12049 	device_t dev = sc->dev;
12050 	int ret;
12051 
12052 	UNREFERENCED_PARAMETER(arg2);
12053 
12054 	ret = priv_check(curthread, PRIV_DRIVER);
12055 	if (ret)
12056 		return (ret);
12057 
12058 	if (ice_driver_is_detaching(sc))
12059 		return (ESHUTDOWN);
12060 
12061 	/* If the user hasn't written "1" to this sysctl yet: */
12062 	if (!ice_test_state(&sc->state, ICE_STATE_DO_DESTROY_MIRR_INTFC)) {
12063 		/* Avoid output on the first set of reads to this sysctl in
12064 		 * order to prevent a null byte from being written to the
12065 		 * end result when called via sysctl(8).
12066 		 */
12067 		if (req->oldptr == NULL && req->newptr == NULL) {
12068 			ret = SYSCTL_OUT(req, 0, 0);
12069 			return (ret);
12070 		}
12071 
12072 		char input_buf[2] = "";
12073 		ret = sysctl_handle_string(oidp, input_buf, sizeof(input_buf), req);
12074 		if ((ret) || (req->newptr == NULL))
12075 			return (ret);
12076 
12077 		/* If we get '1', then indicate we'll create the interface in
12078 		 * the next sysctl read call.
12079 		 */
12080 		if (input_buf[0] == '1') {
12081 			if (!sc->mirr_if) {
12082 				device_printf(dev,
12083 				    "No mirror interface exists!\n");
12084 				return (EINVAL);
12085 			}
12086 			ice_set_state(&sc->state, ICE_STATE_DO_DESTROY_MIRR_INTFC);
12087 			return (0);
12088 		}
12089 
12090 		return (EINVAL);
12091 	}
12092 
12093 	/* --- "Do Destroy Mirror Interface" is set --- */
12094 
12095 	/* Caller just wants the upper bound for size */
12096 	if (req->oldptr == NULL && req->newptr == NULL) {
12097 		ret = SYSCTL_OUT(req, 0, 128);
12098 		return (ret);
12099 	}
12100 
12101 	device_printf(dev, "Destroying mirroring interface...\n");
12102 
12103 	ice_destroy_mirror_interface(sc);
12104 
12105 	ice_clear_state(&sc->state, ICE_STATE_DO_DESTROY_MIRR_INTFC);
12106 
12107 	ret = sysctl_handle_string(oidp, __DECONST(char *, "Interface destroyed"), 0, req);
12108 	return (ret);
12109 }
12110