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