if_iavf.c revision 1.5 1 1.5 yamaguch /* $NetBSD: if_iavf.c,v 1.5 2020/09/10 03:20:08 yamaguchi Exp $ */
2 1.1 yamaguch
3 1.1 yamaguch /*
4 1.1 yamaguch * Copyright (c) 2013-2015, Intel Corporation
5 1.1 yamaguch * All rights reserved.
6 1.1 yamaguch
7 1.1 yamaguch * Redistribution and use in source and binary forms, with or without
8 1.1 yamaguch * modification, are permitted provided that the following conditions are met:
9 1.1 yamaguch *
10 1.1 yamaguch * 1. Redistributions of source code must retain the above copyright notice,
11 1.1 yamaguch * this list of conditions and the following disclaimer.
12 1.1 yamaguch *
13 1.1 yamaguch * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 yamaguch * notice, this list of conditions and the following disclaimer in the
15 1.1 yamaguch * documentation and/or other materials provided with the distribution.
16 1.1 yamaguch *
17 1.1 yamaguch * 3. Neither the name of the Intel Corporation nor the names of its
18 1.1 yamaguch * contributors may be used to endorse or promote products derived from
19 1.1 yamaguch * this software without specific prior written permission.
20 1.1 yamaguch *
21 1.1 yamaguch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 1.1 yamaguch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 yamaguch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 yamaguch * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 1.1 yamaguch * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 yamaguch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 yamaguch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 yamaguch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 yamaguch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 yamaguch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 yamaguch * POSSIBILITY OF SUCH DAMAGE.
32 1.1 yamaguch */
33 1.1 yamaguch
34 1.1 yamaguch /*
35 1.1 yamaguch * Copyright (c) 2016,2017 David Gwynne <dlg (at) openbsd.org>
36 1.1 yamaguch * Copyright (c) 2019 Jonathan Matthew <jmatthew (at) openbsd.org>
37 1.1 yamaguch *
38 1.1 yamaguch * Permission to use, copy, modify, and distribute this software for any
39 1.1 yamaguch * purpose with or without fee is hereby granted, provided that the above
40 1.1 yamaguch * copyright notice and this permission notice appear in all copies.
41 1.1 yamaguch *
42 1.1 yamaguch * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
43 1.1 yamaguch * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44 1.1 yamaguch * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
45 1.1 yamaguch * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46 1.1 yamaguch * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47 1.1 yamaguch * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
48 1.1 yamaguch * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 1.1 yamaguch */
50 1.1 yamaguch
51 1.1 yamaguch /*
52 1.1 yamaguch * Copyright (c) 2020 Internet Initiative Japan, Inc.
53 1.1 yamaguch * All rights reserved.
54 1.1 yamaguch *
55 1.1 yamaguch * Redistribution and use in source and binary forms, with or without
56 1.1 yamaguch * modification, are permitted provided that the following conditions
57 1.1 yamaguch * are met:
58 1.1 yamaguch * 1. Redistributions of source code must retain the above copyright
59 1.1 yamaguch * notice, this list of conditions and the following disclaimer.
60 1.1 yamaguch * 2. Redistributions in binary form must reproduce the above copyright
61 1.1 yamaguch * notice, this list of conditions and the following disclaimer in the
62 1.1 yamaguch * documentation and/or other materials provided with the distribution.
63 1.1 yamaguch *
64 1.1 yamaguch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
65 1.1 yamaguch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
66 1.1 yamaguch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 1.1 yamaguch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
68 1.1 yamaguch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69 1.1 yamaguch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70 1.1 yamaguch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71 1.1 yamaguch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
72 1.1 yamaguch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73 1.1 yamaguch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
74 1.1 yamaguch * POSSIBILITY OF SUCH DAMAGE.
75 1.1 yamaguch */
76 1.1 yamaguch
77 1.1 yamaguch #include <sys/cdefs.h>
78 1.5 yamaguch __KERNEL_RCSID(0, "$NetBSD: if_iavf.c,v 1.5 2020/09/10 03:20:08 yamaguchi Exp $");
79 1.1 yamaguch
80 1.1 yamaguch #include <sys/param.h>
81 1.1 yamaguch #include <sys/types.h>
82 1.1 yamaguch
83 1.1 yamaguch #include <sys/bitops.h>
84 1.1 yamaguch #include <sys/bus.h>
85 1.1 yamaguch #include <sys/cprng.h>
86 1.1 yamaguch #include <sys/cpu.h>
87 1.1 yamaguch #include <sys/device.h>
88 1.1 yamaguch #include <sys/evcnt.h>
89 1.1 yamaguch #include <sys/interrupt.h>
90 1.1 yamaguch #include <sys/kmem.h>
91 1.1 yamaguch #include <sys/module.h>
92 1.1 yamaguch #include <sys/mutex.h>
93 1.1 yamaguch #include <sys/pcq.h>
94 1.1 yamaguch #include <sys/queue.h>
95 1.1 yamaguch #include <sys/syslog.h>
96 1.1 yamaguch #include <sys/workqueue.h>
97 1.1 yamaguch
98 1.1 yamaguch #include <net/bpf.h>
99 1.1 yamaguch #include <net/if.h>
100 1.1 yamaguch #include <net/if_dl.h>
101 1.1 yamaguch #include <net/if_media.h>
102 1.1 yamaguch #include <net/if_ether.h>
103 1.1 yamaguch #include <net/rss_config.h>
104 1.1 yamaguch
105 1.1 yamaguch #include <netinet/tcp.h> /* for struct tcphdr */
106 1.1 yamaguch #include <netinet/udp.h> /* for struct udphdr */
107 1.1 yamaguch
108 1.1 yamaguch #include <dev/pci/pcivar.h>
109 1.1 yamaguch #include <dev/pci/pcidevs.h>
110 1.1 yamaguch
111 1.1 yamaguch #include <dev/pci/if_ixlreg.h>
112 1.1 yamaguch #include <dev/pci/if_ixlvar.h>
113 1.1 yamaguch #include <dev/pci/if_iavfvar.h>
114 1.1 yamaguch
115 1.1 yamaguch #include <prop/proplib.h>
116 1.1 yamaguch
117 1.1 yamaguch #define IAVF_PCIREG PCI_MAPREG_START
118 1.1 yamaguch #define IAVF_AQ_NUM 256
119 1.1 yamaguch #define IAVF_AQ_MASK (IAVF_AQ_NUM-1)
120 1.1 yamaguch #define IAVF_AQ_ALIGN 64
121 1.1 yamaguch #define IAVF_AQ_BUFLEN 4096
122 1.1 yamaguch #define I40E_AQ_LARGE_BUF 512
123 1.1 yamaguch #define IAVF_VF_MAJOR 1
124 1.1 yamaguch #define IAVF_VF_MINOR 1
125 1.1 yamaguch
126 1.1 yamaguch #define IAVF_VFR_INPROGRESS 0
127 1.1 yamaguch #define IAVF_VFR_COMPLETED 1
128 1.1 yamaguch #define IAVF_VFR_VFACTIVE 2
129 1.1 yamaguch
130 1.1 yamaguch #define IAVF_REG_VFR 0xdeadbeef
131 1.1 yamaguch
132 1.1 yamaguch #define IAVF_ITR_RX 0x0
133 1.1 yamaguch #define IAVF_ITR_TX 0x1
134 1.1 yamaguch #define IAVF_ITR_MISC 0x2
135 1.1 yamaguch #define IAVF_NOITR 0x3
136 1.1 yamaguch
137 1.1 yamaguch #define IAVF_MTU_ETHERLEN (ETHER_HDR_LEN \
138 1.1 yamaguch + ETHER_CRC_LEN)
139 1.1 yamaguch #define IAVF_MAX_MTU (9600 - IAVF_MTU_ETHERLEN)
140 1.1 yamaguch #define IAVF_MIN_MTU (ETHER_MIN_LEN - ETHER_CRC_LEN)
141 1.1 yamaguch
142 1.1 yamaguch #define IAVF_WORKQUEUE_PRI PRI_SOFTNET
143 1.1 yamaguch
144 1.1 yamaguch #define IAVF_TX_PKT_DESCS 8
145 1.1 yamaguch #define IAVF_TX_QUEUE_ALIGN 128
146 1.1 yamaguch #define IAVF_RX_QUEUE_ALIGN 128
147 1.1 yamaguch #define IAVF_TX_PKT_MAXSIZE (MCLBYTES * IAVF_TX_PKT_DESCS)
148 1.1 yamaguch #define IAVF_MCLBYTES (MCLBYTES - ETHER_ALIGN)
149 1.1 yamaguch
150 1.1 yamaguch #define IAVF_TICK_INTERVAL (5 * hz)
151 1.1 yamaguch #define IAVF_WATCHDOG_TICKS 3
152 1.1 yamaguch #define IAVF_WATCHDOG_STOP 0
153 1.1 yamaguch
154 1.1 yamaguch #define IAVF_TXRX_PROCESS_UNLIMIT UINT_MAX
155 1.1 yamaguch #define IAVF_TX_PROCESS_LIMIT 256
156 1.1 yamaguch #define IAVF_RX_PROCESS_LIMIT 256
157 1.1 yamaguch #define IAVF_TX_INTR_PROCESS_LIMIT 256
158 1.1 yamaguch #define IAVF_RX_INTR_PROCESS_LIMIT 0U
159 1.1 yamaguch
160 1.1 yamaguch #define IAVF_EXEC_TIMEOUT 3000
161 1.1 yamaguch
162 1.1 yamaguch #define IAVF_IFCAP_RXCSUM (IFCAP_CSUM_IPv4_Rx | \
163 1.1 yamaguch IFCAP_CSUM_TCPv4_Rx | \
164 1.1 yamaguch IFCAP_CSUM_UDPv4_Rx | \
165 1.1 yamaguch IFCAP_CSUM_TCPv6_Rx | \
166 1.1 yamaguch IFCAP_CSUM_UDPv6_Rx)
167 1.1 yamaguch #define IAVF_IFCAP_TXCSUM (IFCAP_CSUM_IPv4_Tx | \
168 1.1 yamaguch IFCAP_CSUM_TCPv4_Tx | \
169 1.1 yamaguch IFCAP_CSUM_UDPv4_Tx | \
170 1.1 yamaguch IFCAP_CSUM_TCPv6_Tx | \
171 1.1 yamaguch IFCAP_CSUM_UDPv6_Tx)
172 1.1 yamaguch #define IAVF_CSUM_ALL_OFFLOAD (M_CSUM_IPv4 | \
173 1.1 yamaguch M_CSUM_TCPv4 | M_CSUM_TCPv6 | \
174 1.1 yamaguch M_CSUM_UDPv4 | M_CSUM_UDPv6)
175 1.1 yamaguch
176 1.1 yamaguch struct iavf_softc; /* defined */
177 1.1 yamaguch
178 1.1 yamaguch struct iavf_module_params {
179 1.1 yamaguch int debug;
180 1.1 yamaguch uint32_t rx_itr;
181 1.1 yamaguch uint32_t tx_itr;
182 1.1 yamaguch unsigned int rx_ndescs;
183 1.1 yamaguch unsigned int tx_ndescs;
184 1.1 yamaguch int max_qps;
185 1.1 yamaguch };
186 1.1 yamaguch
187 1.1 yamaguch struct iavf_product {
188 1.1 yamaguch unsigned int vendor_id;
189 1.1 yamaguch unsigned int product_id;
190 1.1 yamaguch };
191 1.1 yamaguch
192 1.1 yamaguch struct iavf_link_speed {
193 1.1 yamaguch uint64_t baudrate;
194 1.1 yamaguch uint64_t media;
195 1.1 yamaguch };
196 1.1 yamaguch
197 1.1 yamaguch struct iavf_aq_regs {
198 1.1 yamaguch bus_size_t atq_tail;
199 1.1 yamaguch bus_size_t atq_head;
200 1.1 yamaguch bus_size_t atq_len;
201 1.1 yamaguch bus_size_t atq_bal;
202 1.1 yamaguch bus_size_t atq_bah;
203 1.1 yamaguch
204 1.1 yamaguch bus_size_t arq_tail;
205 1.1 yamaguch bus_size_t arq_head;
206 1.1 yamaguch bus_size_t arq_len;
207 1.1 yamaguch bus_size_t arq_bal;
208 1.1 yamaguch bus_size_t arq_bah;
209 1.1 yamaguch
210 1.1 yamaguch uint32_t atq_len_enable;
211 1.1 yamaguch uint32_t atq_tail_mask;
212 1.1 yamaguch uint32_t atq_head_mask;
213 1.1 yamaguch
214 1.1 yamaguch uint32_t arq_len_enable;
215 1.1 yamaguch uint32_t arq_tail_mask;
216 1.1 yamaguch uint32_t arq_head_mask;
217 1.1 yamaguch };
218 1.1 yamaguch
219 1.1 yamaguch struct iavf_work {
220 1.1 yamaguch struct work ixw_cookie;
221 1.1 yamaguch void (*ixw_func)(void *);
222 1.1 yamaguch void *ixw_arg;
223 1.1 yamaguch unsigned int ixw_added;
224 1.1 yamaguch };
225 1.1 yamaguch
226 1.1 yamaguch struct iavf_tx_map {
227 1.1 yamaguch struct mbuf *txm_m;
228 1.1 yamaguch bus_dmamap_t txm_map;
229 1.1 yamaguch unsigned int txm_eop;
230 1.1 yamaguch };
231 1.1 yamaguch
232 1.1 yamaguch struct iavf_tx_ring {
233 1.1 yamaguch unsigned int txr_qid;
234 1.1 yamaguch char txr_name[16];
235 1.1 yamaguch
236 1.1 yamaguch struct iavf_softc *txr_sc;
237 1.1 yamaguch kmutex_t txr_lock;
238 1.1 yamaguch pcq_t *txr_intrq;
239 1.1 yamaguch void *txr_si;
240 1.1 yamaguch unsigned int txr_prod;
241 1.1 yamaguch unsigned int txr_cons;
242 1.1 yamaguch
243 1.1 yamaguch struct iavf_tx_map *txr_maps;
244 1.1 yamaguch struct ixl_dmamem txr_mem;
245 1.1 yamaguch bus_size_t txr_tail;
246 1.1 yamaguch
247 1.1 yamaguch int txr_watchdog;
248 1.1 yamaguch
249 1.1 yamaguch struct evcnt txr_defragged;
250 1.1 yamaguch struct evcnt txr_defrag_failed;
251 1.1 yamaguch struct evcnt txr_pcqdrop;
252 1.1 yamaguch struct evcnt txr_transmitdef;
253 1.1 yamaguch struct evcnt txr_defer;
254 1.1 yamaguch struct evcnt txr_watchdogto;
255 1.1 yamaguch struct evcnt txr_intr;
256 1.1 yamaguch };
257 1.1 yamaguch
258 1.1 yamaguch struct iavf_rx_map {
259 1.1 yamaguch struct mbuf *rxm_m;
260 1.1 yamaguch bus_dmamap_t rxm_map;
261 1.1 yamaguch };
262 1.1 yamaguch
263 1.1 yamaguch struct iavf_rx_ring {
264 1.1 yamaguch unsigned int rxr_qid;
265 1.1 yamaguch char rxr_name[16];
266 1.1 yamaguch
267 1.1 yamaguch struct iavf_softc *rxr_sc;
268 1.1 yamaguch kmutex_t rxr_lock;
269 1.1 yamaguch
270 1.1 yamaguch unsigned int rxr_prod;
271 1.1 yamaguch unsigned int rxr_cons;
272 1.1 yamaguch
273 1.1 yamaguch struct iavf_rx_map *rxr_maps;
274 1.1 yamaguch struct ixl_dmamem rxr_mem;
275 1.1 yamaguch bus_size_t rxr_tail;
276 1.1 yamaguch
277 1.1 yamaguch struct mbuf *rxr_m_head;
278 1.1 yamaguch struct mbuf **rxr_m_tail;
279 1.1 yamaguch
280 1.1 yamaguch struct evcnt rxr_mgethdr_failed;
281 1.1 yamaguch struct evcnt rxr_mgetcl_failed;
282 1.1 yamaguch struct evcnt rxr_mbuf_load_failed;
283 1.1 yamaguch struct evcnt rxr_defer;
284 1.1 yamaguch struct evcnt rxr_intr;
285 1.1 yamaguch };
286 1.1 yamaguch
287 1.1 yamaguch struct iavf_queue_pair {
288 1.1 yamaguch struct iavf_tx_ring *qp_txr;
289 1.1 yamaguch struct iavf_rx_ring *qp_rxr;
290 1.1 yamaguch struct work qp_work;
291 1.1 yamaguch void *qp_si;
292 1.1 yamaguch bool qp_workqueue;
293 1.1 yamaguch };
294 1.1 yamaguch
295 1.1 yamaguch struct iavf_stat_counters {
296 1.1 yamaguch struct evcnt isc_rx_bytes;
297 1.1 yamaguch struct evcnt isc_rx_unicast;
298 1.1 yamaguch struct evcnt isc_rx_multicast;
299 1.1 yamaguch struct evcnt isc_rx_broadcast;
300 1.1 yamaguch struct evcnt isc_rx_discards;
301 1.1 yamaguch struct evcnt isc_rx_unknown_protocol;
302 1.1 yamaguch struct evcnt isc_tx_bytes;
303 1.1 yamaguch struct evcnt isc_tx_unicast;
304 1.1 yamaguch struct evcnt isc_tx_multicast;
305 1.1 yamaguch struct evcnt isc_tx_broadcast;
306 1.1 yamaguch struct evcnt isc_tx_discards;
307 1.1 yamaguch struct evcnt isc_tx_errors;
308 1.1 yamaguch };
309 1.1 yamaguch
310 1.1 yamaguch /*
311 1.1 yamaguch * Locking notes:
312 1.1 yamaguch * + A field in iavf_tx_ring is protected by txr_lock (a spin mutex), and
313 1.1 yamaguch * A field in iavf_rx_ring is protected by rxr_lock (a spin mutex).
314 1.1 yamaguch * - more than one lock must not be held at once.
315 1.1 yamaguch * + fields named sc_atq_*, sc_arq_*, and sc_adminq_* are protected by
316 1.1 yamaguch * sc_adminq_lock(a spin mutex).
317 1.1 yamaguch * - The lock is held while accessing sc_aq_regs
318 1.1 yamaguch * and is not held with txr_lock and rxr_lock together.
319 1.1 yamaguch * + Other fields in iavf_softc is protected by sc_cfg_lock
320 1.1 yamaguch * (an adaptive mutex).
321 1.1 yamaguch * - The lock must be held before acquiring another lock.
322 1.4 yamaguch *
323 1.4 yamaguch * Locking order:
324 1.4 yamaguch * - IFNET_LOCK => sc_cfg_lock => sc_adminq_lock
325 1.4 yamaguch * - sc_cfg_lock => ETHER_LOCK => sc_adminq_lock
326 1.4 yamaguch * - sc_cfg_lock => txr_lock
327 1.4 yamaguch * - sc_cfg_lock => rxr_lock
328 1.1 yamaguch */
329 1.1 yamaguch
330 1.1 yamaguch struct iavf_softc {
331 1.1 yamaguch device_t sc_dev;
332 1.1 yamaguch enum i40e_mac_type sc_mac_type;
333 1.1 yamaguch int sc_debuglevel;
334 1.1 yamaguch bool sc_attached;
335 1.1 yamaguch bool sc_dead;
336 1.1 yamaguch kmutex_t sc_cfg_lock;
337 1.1 yamaguch callout_t sc_tick;
338 1.1 yamaguch struct ifmedia sc_media;
339 1.1 yamaguch uint64_t sc_media_status;
340 1.1 yamaguch uint64_t sc_media_active;
341 1.1 yamaguch int sc_link_state;
342 1.1 yamaguch
343 1.1 yamaguch const struct iavf_aq_regs *
344 1.1 yamaguch sc_aq_regs;
345 1.1 yamaguch
346 1.1 yamaguch struct ethercom sc_ec;
347 1.1 yamaguch uint8_t sc_enaddr[ETHER_ADDR_LEN];
348 1.1 yamaguch uint8_t sc_enaddr_fake[ETHER_ADDR_LEN];
349 1.1 yamaguch uint8_t sc_enaddr_added[ETHER_ADDR_LEN];
350 1.1 yamaguch uint8_t sc_enaddr_reset[ETHER_ADDR_LEN];
351 1.1 yamaguch struct if_percpuq *sc_ipq;
352 1.1 yamaguch
353 1.1 yamaguch struct pci_attach_args sc_pa;
354 1.1 yamaguch bus_dma_tag_t sc_dmat;
355 1.1 yamaguch bus_space_tag_t sc_memt;
356 1.1 yamaguch bus_space_handle_t sc_memh;
357 1.1 yamaguch bus_size_t sc_mems;
358 1.1 yamaguch pci_intr_handle_t *sc_ihp;
359 1.1 yamaguch void **sc_ihs;
360 1.1 yamaguch unsigned int sc_nintrs;
361 1.1 yamaguch
362 1.1 yamaguch uint32_t sc_major_ver;
363 1.1 yamaguch uint32_t sc_minor_ver;
364 1.1 yamaguch uint32_t sc_vf_id;
365 1.1 yamaguch uint32_t sc_vf_cap;
366 1.1 yamaguch uint16_t sc_vsi_id;
367 1.1 yamaguch uint16_t sc_qset_handle;
368 1.1 yamaguch uint16_t sc_max_mtu;
369 1.1 yamaguch bool sc_got_vf_resources;
370 1.1 yamaguch bool sc_got_irq_map;
371 1.1 yamaguch unsigned int sc_max_vectors;
372 1.1 yamaguch
373 1.1 yamaguch kmutex_t sc_adminq_lock;
374 1.1 yamaguch kcondvar_t sc_adminq_cv;
375 1.1 yamaguch struct ixl_dmamem sc_atq;
376 1.1 yamaguch unsigned int sc_atq_prod;
377 1.1 yamaguch unsigned int sc_atq_cons;
378 1.1 yamaguch struct ixl_aq_bufs sc_atq_idle;
379 1.1 yamaguch struct ixl_aq_bufs sc_atq_live;
380 1.1 yamaguch struct ixl_dmamem sc_arq;
381 1.1 yamaguch struct ixl_aq_bufs sc_arq_idle;
382 1.1 yamaguch struct ixl_aq_bufs sc_arq_live;
383 1.1 yamaguch unsigned int sc_arq_prod;
384 1.1 yamaguch unsigned int sc_arq_cons;
385 1.1 yamaguch struct iavf_work sc_arq_refill;
386 1.1 yamaguch uint32_t sc_arq_opcode;
387 1.1 yamaguch uint32_t sc_arq_retval;
388 1.1 yamaguch
389 1.1 yamaguch uint32_t sc_tx_itr;
390 1.1 yamaguch uint32_t sc_rx_itr;
391 1.1 yamaguch unsigned int sc_tx_ring_ndescs;
392 1.1 yamaguch unsigned int sc_rx_ring_ndescs;
393 1.1 yamaguch unsigned int sc_nqueue_pairs;
394 1.1 yamaguch unsigned int sc_nqps_alloc;
395 1.1 yamaguch unsigned int sc_nqps_vsi;
396 1.1 yamaguch unsigned int sc_nqps_req;
397 1.1 yamaguch struct iavf_queue_pair *sc_qps;
398 1.1 yamaguch bool sc_txrx_workqueue;
399 1.1 yamaguch u_int sc_tx_intr_process_limit;
400 1.1 yamaguch u_int sc_tx_process_limit;
401 1.1 yamaguch u_int sc_rx_intr_process_limit;
402 1.1 yamaguch u_int sc_rx_process_limit;
403 1.1 yamaguch
404 1.1 yamaguch struct workqueue *sc_workq;
405 1.1 yamaguch struct workqueue *sc_workq_txrx;
406 1.1 yamaguch struct iavf_work sc_reset_task;
407 1.1 yamaguch struct iavf_work sc_wdto_task;
408 1.1 yamaguch struct iavf_work sc_req_queues_task;
409 1.1 yamaguch bool sc_req_queues_retried;
410 1.1 yamaguch bool sc_resetting;
411 1.1 yamaguch bool sc_reset_up;
412 1.1 yamaguch
413 1.1 yamaguch struct sysctllog *sc_sysctllog;
414 1.1 yamaguch struct iavf_stat_counters
415 1.1 yamaguch sc_stat_counters;
416 1.1 yamaguch };
417 1.1 yamaguch
418 1.1 yamaguch #define IAVF_LOG(_sc, _lvl, _fmt, _args...) \
419 1.1 yamaguch do { \
420 1.1 yamaguch if (!(_sc)->sc_attached) { \
421 1.1 yamaguch switch (_lvl) { \
422 1.1 yamaguch case LOG_ERR: \
423 1.1 yamaguch case LOG_WARNING: \
424 1.1 yamaguch aprint_error_dev((_sc)->sc_dev, _fmt, ##_args); \
425 1.1 yamaguch break; \
426 1.1 yamaguch case LOG_INFO: \
427 1.1 yamaguch aprint_normal_dev((_sc)->sc_dev,_fmt, ##_args); \
428 1.1 yamaguch break; \
429 1.1 yamaguch case LOG_DEBUG: \
430 1.1 yamaguch default: \
431 1.1 yamaguch aprint_debug_dev((_sc)->sc_dev, _fmt, ##_args); \
432 1.1 yamaguch } \
433 1.1 yamaguch } else { \
434 1.1 yamaguch struct ifnet *_ifp = &(_sc)->sc_ec.ec_if; \
435 1.1 yamaguch log((_lvl), "%s: " _fmt, _ifp->if_xname, ##_args); \
436 1.1 yamaguch } \
437 1.1 yamaguch } while (0)
438 1.1 yamaguch
439 1.1 yamaguch static int iavf_dmamem_alloc(bus_dma_tag_t, struct ixl_dmamem *,
440 1.1 yamaguch bus_size_t, bus_size_t);
441 1.1 yamaguch static void iavf_dmamem_free(bus_dma_tag_t, struct ixl_dmamem *);
442 1.1 yamaguch static struct ixl_aq_buf *
443 1.1 yamaguch iavf_aqb_get(struct iavf_softc *, struct ixl_aq_bufs *);
444 1.1 yamaguch static struct ixl_aq_buf *
445 1.1 yamaguch iavf_aqb_get_locked(struct ixl_aq_bufs *);
446 1.1 yamaguch static void iavf_aqb_put_locked(struct ixl_aq_bufs *, struct ixl_aq_buf *);
447 1.1 yamaguch static void iavf_aqb_clean(struct ixl_aq_bufs *, bus_dma_tag_t);
448 1.1 yamaguch
449 1.1 yamaguch static const struct iavf_product *
450 1.1 yamaguch iavf_lookup(const struct pci_attach_args *);
451 1.1 yamaguch static enum i40e_mac_type
452 1.1 yamaguch iavf_mactype(pci_product_id_t);
453 1.1 yamaguch static void iavf_pci_csr_setup(pci_chipset_tag_t, pcitag_t);
454 1.1 yamaguch static int iavf_wait_active(struct iavf_softc *);
455 1.1 yamaguch static bool iavf_is_etheranyaddr(const uint8_t *);
456 1.1 yamaguch static void iavf_prepare_fakeaddr(struct iavf_softc *);
457 1.1 yamaguch static int iavf_replace_lla(struct ifnet *,
458 1.1 yamaguch const uint8_t *, const uint8_t *);
459 1.1 yamaguch static void iavf_evcnt_attach(struct evcnt *,
460 1.1 yamaguch const char *, const char *);
461 1.1 yamaguch static int iavf_setup_interrupts(struct iavf_softc *);
462 1.1 yamaguch static void iavf_teardown_interrupts(struct iavf_softc *);
463 1.1 yamaguch static int iavf_setup_sysctls(struct iavf_softc *);
464 1.1 yamaguch static void iavf_teardown_sysctls(struct iavf_softc *);
465 1.1 yamaguch static int iavf_setup_stats(struct iavf_softc *);
466 1.1 yamaguch static void iavf_teardown_stats(struct iavf_softc *);
467 1.1 yamaguch static struct workqueue *
468 1.1 yamaguch iavf_workq_create(const char *, pri_t, int, int);
469 1.1 yamaguch static void iavf_workq_destroy(struct workqueue *);
470 1.1 yamaguch static int iavf_work_set(struct iavf_work *, void (*)(void *), void *);
471 1.1 yamaguch static void iavf_work_add(struct workqueue *, struct iavf_work *);
472 1.1 yamaguch static void iavf_work_wait(struct workqueue *, struct iavf_work *);
473 1.1 yamaguch static unsigned int
474 1.1 yamaguch iavf_calc_msix_count(struct iavf_softc *);
475 1.1 yamaguch static unsigned int
476 1.1 yamaguch iavf_calc_queue_pair_size(struct iavf_softc *);
477 1.1 yamaguch static int iavf_queue_pairs_alloc(struct iavf_softc *);
478 1.1 yamaguch static void iavf_queue_pairs_free(struct iavf_softc *);
479 1.1 yamaguch static int iavf_arq_fill(struct iavf_softc *);
480 1.1 yamaguch static void iavf_arq_refill(void *);
481 1.1 yamaguch static int iavf_arq_poll(struct iavf_softc *, uint32_t, int);
482 1.1 yamaguch static void iavf_atq_done(struct iavf_softc *);
483 1.1 yamaguch static int iavf_init_admin_queue(struct iavf_softc *);
484 1.1 yamaguch static void iavf_cleanup_admin_queue(struct iavf_softc *);
485 1.1 yamaguch static int iavf_arq(struct iavf_softc *);
486 1.1 yamaguch static int iavf_adminq_exec(struct iavf_softc *,
487 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *);
488 1.1 yamaguch static int iavf_adminq_poll(struct iavf_softc *,
489 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *, int);
490 1.1 yamaguch static int iavf_adminq_poll_locked(struct iavf_softc *,
491 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *, int);
492 1.1 yamaguch static int iavf_add_multi(struct iavf_softc *, uint8_t *, uint8_t *);
493 1.1 yamaguch static int iavf_del_multi(struct iavf_softc *, uint8_t *, uint8_t *);
494 1.1 yamaguch static void iavf_del_all_multi(struct iavf_softc *);
495 1.1 yamaguch
496 1.1 yamaguch static int iavf_get_version(struct iavf_softc *, struct ixl_aq_buf *);
497 1.1 yamaguch static int iavf_get_vf_resources(struct iavf_softc *, struct ixl_aq_buf *);
498 1.1 yamaguch static int iavf_get_stats(struct iavf_softc *);
499 1.1 yamaguch static int iavf_config_irq_map(struct iavf_softc *, struct ixl_aq_buf *);
500 1.1 yamaguch static int iavf_config_vsi_queues(struct iavf_softc *);
501 1.1 yamaguch static int iavf_config_hena(struct iavf_softc *);
502 1.1 yamaguch static int iavf_config_rss_key(struct iavf_softc *);
503 1.1 yamaguch static int iavf_config_rss_lut(struct iavf_softc *);
504 1.1 yamaguch static int iavf_config_promisc_mode(struct iavf_softc *, int, int);
505 1.1 yamaguch static int iavf_config_vlan_stripping(struct iavf_softc *, int);
506 1.1 yamaguch static int iavf_config_vlan_id(struct iavf_softc *, uint16_t, uint32_t);
507 1.1 yamaguch static int iavf_queue_select(struct iavf_softc *, int);
508 1.1 yamaguch static int iavf_request_queues(struct iavf_softc *, unsigned int);
509 1.1 yamaguch static int iavf_reset_vf(struct iavf_softc *);
510 1.1 yamaguch static int iavf_eth_addr(struct iavf_softc *, const uint8_t *, uint32_t);
511 1.1 yamaguch static void iavf_process_version(struct iavf_softc *,
512 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *);
513 1.1 yamaguch static void iavf_process_vf_resources(struct iavf_softc *,
514 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *);
515 1.1 yamaguch static void iavf_process_irq_map(struct iavf_softc *,
516 1.1 yamaguch struct ixl_aq_desc *);
517 1.1 yamaguch static void iavf_process_vc_event(struct iavf_softc *,
518 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *);
519 1.1 yamaguch static void iavf_process_stats(struct iavf_softc *,
520 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *);
521 1.1 yamaguch static void iavf_process_req_queues(struct iavf_softc *,
522 1.1 yamaguch struct ixl_aq_desc *, struct ixl_aq_buf *);
523 1.1 yamaguch
524 1.1 yamaguch static int iavf_intr(void *);
525 1.1 yamaguch static int iavf_queue_intr(void *);
526 1.1 yamaguch static void iavf_tick(void *);
527 1.1 yamaguch static void iavf_tick_halt(void *);
528 1.1 yamaguch static void iavf_reset_request(void *);
529 1.1 yamaguch static void iavf_reset_start(void *);
530 1.1 yamaguch static void iavf_reset(void *);
531 1.1 yamaguch static void iavf_reset_finish(struct iavf_softc *);
532 1.1 yamaguch static int iavf_init(struct ifnet *);
533 1.1 yamaguch static int iavf_init_locked(struct iavf_softc *);
534 1.1 yamaguch static void iavf_stop(struct ifnet *, int);
535 1.1 yamaguch static void iavf_stop_locked(struct iavf_softc *);
536 1.1 yamaguch static int iavf_ioctl(struct ifnet *, u_long, void *);
537 1.1 yamaguch static void iavf_start(struct ifnet *);
538 1.1 yamaguch static int iavf_transmit(struct ifnet *, struct mbuf*);
539 1.1 yamaguch static int iavf_watchdog(struct iavf_tx_ring *);
540 1.1 yamaguch static void iavf_watchdog_timeout(void *);
541 1.1 yamaguch static int iavf_media_change(struct ifnet *);
542 1.1 yamaguch static void iavf_media_status(struct ifnet *, struct ifmediareq *);
543 1.1 yamaguch static int iavf_ifflags_cb(struct ethercom *);
544 1.1 yamaguch static int iavf_vlan_cb(struct ethercom *, uint16_t, bool);
545 1.1 yamaguch static void iavf_deferred_transmit(void *);
546 1.1 yamaguch static void iavf_handle_queue(void *);
547 1.1 yamaguch static void iavf_handle_queue_wk(struct work *, void *);
548 1.1 yamaguch static int iavf_reinit(struct iavf_softc *);
549 1.1 yamaguch static int iavf_rxfill(struct iavf_softc *, struct iavf_rx_ring *);
550 1.1 yamaguch static void iavf_txr_clean(struct iavf_softc *, struct iavf_tx_ring *);
551 1.1 yamaguch static void iavf_rxr_clean(struct iavf_softc *, struct iavf_rx_ring *);
552 1.1 yamaguch static int iavf_txeof(struct iavf_softc *, struct iavf_tx_ring *,
553 1.1 yamaguch u_int, struct evcnt *);
554 1.1 yamaguch static int iavf_rxeof(struct iavf_softc *, struct iavf_rx_ring *,
555 1.1 yamaguch u_int, struct evcnt *);
556 1.1 yamaguch static int iavf_iff(struct iavf_softc *);
557 1.1 yamaguch static int iavf_iff_locked(struct iavf_softc *);
558 1.1 yamaguch static void iavf_post_request_queues(void *);
559 1.1 yamaguch static int iavf_sysctl_itr_handler(SYSCTLFN_PROTO);
560 1.1 yamaguch
561 1.1 yamaguch static int iavf_match(device_t, cfdata_t, void *);
562 1.1 yamaguch static void iavf_attach(device_t, device_t, void*);
563 1.1 yamaguch static int iavf_detach(device_t, int);
564 1.1 yamaguch static int iavf_finalize_teardown(device_t);
565 1.1 yamaguch
566 1.1 yamaguch CFATTACH_DECL3_NEW(iavf, sizeof(struct iavf_softc),
567 1.1 yamaguch iavf_match, iavf_attach, iavf_detach, NULL, NULL, NULL,
568 1.1 yamaguch DVF_DETACH_SHUTDOWN);
569 1.1 yamaguch
570 1.1 yamaguch static const struct iavf_product iavf_products[] = {
571 1.1 yamaguch { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_XL710_VF },
572 1.1 yamaguch { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_XL710_VF_HV },
573 1.1 yamaguch { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_X722_VF },
574 1.1 yamaguch /* required last entry */
575 1.1 yamaguch {0, 0}
576 1.1 yamaguch };
577 1.1 yamaguch
578 1.1 yamaguch static const struct iavf_link_speed iavf_link_speeds[] = {
579 1.1 yamaguch { 0, 0 },
580 1.1 yamaguch { IF_Mbps(100), IFM_100_TX },
581 1.1 yamaguch { IF_Mbps(1000), IFM_1000_T },
582 1.1 yamaguch { IF_Gbps(10), IFM_10G_T },
583 1.1 yamaguch { IF_Gbps(40), IFM_40G_CR4 },
584 1.1 yamaguch { IF_Gbps(20), IFM_20G_KR2 },
585 1.1 yamaguch { IF_Gbps(25), IFM_25G_CR }
586 1.1 yamaguch };
587 1.1 yamaguch
588 1.1 yamaguch static const struct iavf_aq_regs iavf_aq_regs = {
589 1.1 yamaguch .atq_tail = I40E_VF_ATQT1,
590 1.1 yamaguch .atq_tail_mask = I40E_VF_ATQT1_ATQT_MASK,
591 1.1 yamaguch .atq_head = I40E_VF_ATQH1,
592 1.1 yamaguch .atq_head_mask = I40E_VF_ARQH1_ARQH_MASK,
593 1.1 yamaguch .atq_len = I40E_VF_ATQLEN1,
594 1.1 yamaguch .atq_bal = I40E_VF_ATQBAL1,
595 1.1 yamaguch .atq_bah = I40E_VF_ATQBAH1,
596 1.1 yamaguch .atq_len_enable = I40E_VF_ATQLEN1_ATQENABLE_MASK,
597 1.1 yamaguch
598 1.1 yamaguch .arq_tail = I40E_VF_ARQT1,
599 1.1 yamaguch .arq_tail_mask = I40E_VF_ARQT1_ARQT_MASK,
600 1.1 yamaguch .arq_head = I40E_VF_ARQH1,
601 1.1 yamaguch .arq_head_mask = I40E_VF_ARQH1_ARQH_MASK,
602 1.1 yamaguch .arq_len = I40E_VF_ARQLEN1,
603 1.1 yamaguch .arq_bal = I40E_VF_ARQBAL1,
604 1.1 yamaguch .arq_bah = I40E_VF_ARQBAH1,
605 1.1 yamaguch .arq_len_enable = I40E_VF_ARQLEN1_ARQENABLE_MASK,
606 1.1 yamaguch };
607 1.1 yamaguch
608 1.1 yamaguch static struct iavf_module_params iavf_params = {
609 1.1 yamaguch .debug = 0,
610 1.1 yamaguch .rx_itr = 0x07a, /* 4K intrs/sec */
611 1.1 yamaguch .tx_itr = 0x07a, /* 4K intrs/sec */
612 1.1 yamaguch .tx_ndescs = 512,
613 1.1 yamaguch .rx_ndescs = 256,
614 1.1 yamaguch .max_qps = INT_MAX,
615 1.1 yamaguch };
616 1.1 yamaguch
617 1.1 yamaguch #define delaymsec(_x) DELAY(1000 * (_x))
618 1.1 yamaguch #define iavf_rd(_s, _r) \
619 1.1 yamaguch bus_space_read_4((_s)->sc_memt, (_s)->sc_memh, (_r))
620 1.1 yamaguch #define iavf_wr(_s, _r, _v) \
621 1.1 yamaguch bus_space_write_4((_s)->sc_memt, (_s)->sc_memh, (_r), (_v))
622 1.1 yamaguch #define iavf_barrier(_s, _r, _l, _o) \
623 1.1 yamaguch bus_space_barrier((_s)->sc_memt, (_s)->sc_memh, (_r), (_l), (_o))
624 1.1 yamaguch #define iavf_flush(_s) (void)iavf_rd((_s), I40E_VFGEN_RSTAT)
625 1.1 yamaguch #define iavf_nqueues(_sc) (1 << ((_sc)->sc_nqueue_pairs - 1))
626 1.1 yamaguch #define iavf_allqueues(_sc) ((1 << ((_sc)->sc_nqueue_pairs)) - 1)
627 1.1 yamaguch
628 1.1 yamaguch static inline void
629 1.1 yamaguch iavf_intr_enable(struct iavf_softc *sc)
630 1.1 yamaguch {
631 1.1 yamaguch
632 1.1 yamaguch iavf_wr(sc, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL0_INTENA_MASK |
633 1.1 yamaguch I40E_VFINT_DYN_CTL0_CLEARPBA_MASK |
634 1.1 yamaguch (IAVF_NOITR << I40E_VFINT_DYN_CTL0_ITR_INDX_SHIFT));
635 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
636 1.1 yamaguch iavf_flush(sc);
637 1.1 yamaguch }
638 1.1 yamaguch
639 1.1 yamaguch static inline void
640 1.1 yamaguch iavf_intr_disable(struct iavf_softc *sc)
641 1.1 yamaguch {
642 1.1 yamaguch
643 1.1 yamaguch iavf_wr(sc, I40E_VFINT_DYN_CTL01,
644 1.1 yamaguch (IAVF_NOITR << I40E_VFINT_DYN_CTL0_ITR_INDX_SHIFT));
645 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ICR0_ENA1, 0);
646 1.1 yamaguch iavf_flush(sc);
647 1.1 yamaguch }
648 1.1 yamaguch
649 1.1 yamaguch static inline void
650 1.1 yamaguch iavf_queue_intr_enable(struct iavf_softc *sc, unsigned int qid)
651 1.1 yamaguch {
652 1.1 yamaguch
653 1.1 yamaguch iavf_wr(sc, I40E_VFINT_DYN_CTLN1(qid),
654 1.1 yamaguch I40E_VFINT_DYN_CTLN1_INTENA_MASK |
655 1.1 yamaguch I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK |
656 1.1 yamaguch (IAVF_NOITR << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT));
657 1.1 yamaguch iavf_flush(sc);
658 1.1 yamaguch }
659 1.1 yamaguch
660 1.1 yamaguch static inline void
661 1.1 yamaguch iavf_queue_intr_disable(struct iavf_softc *sc, unsigned int qid)
662 1.1 yamaguch {
663 1.1 yamaguch
664 1.1 yamaguch iavf_wr(sc, I40E_VFINT_DYN_CTLN1(qid),
665 1.1 yamaguch (IAVF_NOITR << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT));
666 1.1 yamaguch iavf_flush(sc);
667 1.1 yamaguch }
668 1.1 yamaguch
669 1.1 yamaguch static inline void
670 1.1 yamaguch iavf_aq_vc_set_opcode(struct ixl_aq_desc *iaq, uint32_t opcode)
671 1.1 yamaguch {
672 1.1 yamaguch struct iavf_aq_vc *vc;
673 1.1 yamaguch
674 1.1 yamaguch vc = (struct iavf_aq_vc *)&iaq->iaq_cookie;
675 1.1 yamaguch vc->iaq_vc_opcode = htole32(opcode);
676 1.1 yamaguch }
677 1.1 yamaguch
678 1.1 yamaguch static inline uint32_t
679 1.1 yamaguch iavf_aq_vc_get_opcode(const struct ixl_aq_desc *iaq)
680 1.1 yamaguch {
681 1.1 yamaguch const struct iavf_aq_vc *vc;
682 1.1 yamaguch
683 1.1 yamaguch vc = (const struct iavf_aq_vc *)&iaq->iaq_cookie;
684 1.1 yamaguch return le32toh(vc->iaq_vc_opcode);
685 1.1 yamaguch }
686 1.1 yamaguch
687 1.1 yamaguch static inline uint32_t
688 1.1 yamaguch iavf_aq_vc_get_retval(const struct ixl_aq_desc *iaq)
689 1.1 yamaguch {
690 1.1 yamaguch const struct iavf_aq_vc *vc;
691 1.1 yamaguch
692 1.1 yamaguch vc = (const struct iavf_aq_vc *)&iaq->iaq_cookie;
693 1.1 yamaguch return le32toh(vc->iaq_vc_retval);
694 1.1 yamaguch }
695 1.1 yamaguch
696 1.1 yamaguch static int
697 1.1 yamaguch iavf_match(device_t parent, cfdata_t match, void *aux)
698 1.1 yamaguch {
699 1.1 yamaguch const struct pci_attach_args *pa = aux;
700 1.1 yamaguch
701 1.1 yamaguch return (iavf_lookup(pa) != NULL) ? 1 : 0;
702 1.1 yamaguch }
703 1.1 yamaguch
704 1.1 yamaguch static void
705 1.1 yamaguch iavf_attach(device_t parent, device_t self, void *aux)
706 1.1 yamaguch {
707 1.1 yamaguch struct iavf_softc *sc;
708 1.1 yamaguch struct pci_attach_args *pa = aux;
709 1.1 yamaguch struct ifnet *ifp;
710 1.1 yamaguch struct ixl_aq_buf *aqb;
711 1.1 yamaguch pcireg_t memtype;
712 1.1 yamaguch char xnamebuf[MAXCOMLEN];
713 1.1 yamaguch int error, i;
714 1.1 yamaguch
715 1.1 yamaguch sc = device_private(self);
716 1.1 yamaguch sc->sc_dev = self;
717 1.1 yamaguch ifp = &sc->sc_ec.ec_if;
718 1.1 yamaguch
719 1.1 yamaguch sc->sc_pa = *pa;
720 1.1 yamaguch sc->sc_dmat = (pci_dma64_available(pa)) ? pa->pa_dmat64 : pa->pa_dmat;
721 1.1 yamaguch sc->sc_aq_regs = &iavf_aq_regs;
722 1.1 yamaguch sc->sc_debuglevel = iavf_params.debug;
723 1.1 yamaguch sc->sc_tx_ring_ndescs = iavf_params.tx_ndescs;
724 1.1 yamaguch sc->sc_rx_ring_ndescs = iavf_params.rx_ndescs;
725 1.1 yamaguch sc->sc_tx_itr = iavf_params.tx_itr;
726 1.1 yamaguch sc->sc_rx_itr = iavf_params.rx_itr;
727 1.1 yamaguch sc->sc_nqps_req = MIN(ncpu, iavf_params.max_qps);
728 1.1 yamaguch iavf_prepare_fakeaddr(sc);
729 1.1 yamaguch
730 1.1 yamaguch sc->sc_mac_type = iavf_mactype(PCI_PRODUCT(pa->pa_id));
731 1.1 yamaguch iavf_pci_csr_setup(pa->pa_pc, pa->pa_tag);
732 1.1 yamaguch
733 1.1 yamaguch memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, IAVF_PCIREG);
734 1.1 yamaguch if (pci_mapreg_map(pa, IAVF_PCIREG, memtype, 0,
735 1.1 yamaguch &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems)) {
736 1.1 yamaguch aprint_error(": unable to map registers\n");
737 1.1 yamaguch return;
738 1.1 yamaguch }
739 1.1 yamaguch
740 1.1 yamaguch if (iavf_wait_active(sc) != 0) {
741 1.1 yamaguch aprint_error(": VF reset timed out\n");
742 1.1 yamaguch goto unmap;
743 1.1 yamaguch }
744 1.1 yamaguch
745 1.1 yamaguch mutex_init(&sc->sc_cfg_lock, MUTEX_DEFAULT, IPL_SOFTNET);
746 1.1 yamaguch mutex_init(&sc->sc_adminq_lock, MUTEX_DEFAULT, IPL_NET);
747 1.1 yamaguch SIMPLEQ_INIT(&sc->sc_atq_idle);
748 1.1 yamaguch SIMPLEQ_INIT(&sc->sc_atq_live);
749 1.1 yamaguch SIMPLEQ_INIT(&sc->sc_arq_idle);
750 1.1 yamaguch SIMPLEQ_INIT(&sc->sc_arq_live);
751 1.1 yamaguch sc->sc_arq_cons = 0;
752 1.1 yamaguch sc->sc_arq_prod = 0;
753 1.1 yamaguch aqb = NULL;
754 1.1 yamaguch
755 1.1 yamaguch if (iavf_dmamem_alloc(sc->sc_dmat, &sc->sc_atq,
756 1.1 yamaguch sizeof(struct ixl_aq_desc) * IAVF_AQ_NUM, IAVF_AQ_ALIGN) != 0) {
757 1.1 yamaguch aprint_error(": unable to allocate atq\n");
758 1.1 yamaguch goto free_mutex;
759 1.1 yamaguch }
760 1.1 yamaguch
761 1.1 yamaguch if (iavf_dmamem_alloc(sc->sc_dmat, &sc->sc_arq,
762 1.1 yamaguch sizeof(struct ixl_aq_desc) * IAVF_AQ_NUM, IAVF_AQ_ALIGN) != 0) {
763 1.1 yamaguch aprint_error(": unable to allocate arq\n");
764 1.1 yamaguch goto free_atq;
765 1.1 yamaguch }
766 1.1 yamaguch
767 1.1 yamaguch for (i = 0; i < IAVF_AQ_NUM; i++) {
768 1.1 yamaguch aqb = iavf_aqb_get(sc, NULL);
769 1.1 yamaguch if (aqb != NULL) {
770 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_arq_idle, aqb);
771 1.1 yamaguch }
772 1.1 yamaguch }
773 1.1 yamaguch aqb = NULL;
774 1.1 yamaguch
775 1.1 yamaguch if (!iavf_arq_fill(sc)) {
776 1.1 yamaguch aprint_error(": unable to fill arq descriptors\n");
777 1.1 yamaguch goto free_arq;
778 1.1 yamaguch }
779 1.1 yamaguch
780 1.1 yamaguch if (iavf_init_admin_queue(sc) != 0) {
781 1.1 yamaguch aprint_error(": unable to initialize admin queue\n");
782 1.1 yamaguch goto shutdown;
783 1.1 yamaguch }
784 1.1 yamaguch
785 1.1 yamaguch aqb = iavf_aqb_get(sc, NULL);
786 1.1 yamaguch if (aqb == NULL) {
787 1.1 yamaguch aprint_error(": unable to allocate buffer for ATQ\n");
788 1.1 yamaguch goto shutdown;
789 1.1 yamaguch }
790 1.1 yamaguch
791 1.1 yamaguch error = iavf_get_version(sc, aqb);
792 1.1 yamaguch switch (error) {
793 1.1 yamaguch case 0:
794 1.1 yamaguch break;
795 1.1 yamaguch case ETIMEDOUT:
796 1.1 yamaguch aprint_error(": timeout waiting for VF version\n");
797 1.1 yamaguch goto shutdown;
798 1.1 yamaguch case ENOTSUP:
799 1.1 yamaguch aprint_error(": unsupported VF version %d\n", sc->sc_major_ver);
800 1.1 yamaguch goto shutdown;
801 1.1 yamaguch default:
802 1.1 yamaguch aprint_error(":unable to get VF interface version\n");
803 1.1 yamaguch goto shutdown;
804 1.1 yamaguch }
805 1.1 yamaguch
806 1.1 yamaguch if (iavf_get_vf_resources(sc, aqb) != 0) {
807 1.1 yamaguch aprint_error(": timeout waiting for VF resources\n");
808 1.1 yamaguch goto shutdown;
809 1.1 yamaguch }
810 1.1 yamaguch
811 1.1 yamaguch aprint_normal(", VF version %d.%d%s",
812 1.1 yamaguch sc->sc_major_ver, sc->sc_minor_ver,
813 1.1 yamaguch (sc->sc_minor_ver > IAVF_VF_MINOR) ? "(minor mismatch)" : "");
814 1.1 yamaguch aprint_normal(", VF %d, VSI %d", sc->sc_vf_id, sc->sc_vsi_id);
815 1.1 yamaguch aprint_normal("\n");
816 1.1 yamaguch aprint_naive("\n");
817 1.1 yamaguch
818 1.1 yamaguch aprint_normal_dev(self, "Ethernet address %s\n",
819 1.1 yamaguch ether_sprintf(sc->sc_enaddr));
820 1.1 yamaguch
821 1.1 yamaguch if (iavf_queue_pairs_alloc(sc) != 0) {
822 1.1 yamaguch goto shutdown;
823 1.1 yamaguch }
824 1.1 yamaguch
825 1.1 yamaguch if (iavf_setup_interrupts(sc) != 0) {
826 1.1 yamaguch goto free_queue_pairs;
827 1.1 yamaguch }
828 1.1 yamaguch
829 1.1 yamaguch if (iavf_config_irq_map(sc, aqb) != 0) {
830 1.1 yamaguch aprint_error(", timed out waiting for IRQ map response\n");
831 1.1 yamaguch goto teardown_intrs;
832 1.1 yamaguch }
833 1.1 yamaguch
834 1.1 yamaguch if (iavf_setup_sysctls(sc) != 0) {
835 1.1 yamaguch goto teardown_intrs;
836 1.1 yamaguch }
837 1.1 yamaguch
838 1.1 yamaguch if (iavf_setup_stats(sc) != 0) {
839 1.1 yamaguch goto teardown_sysctls;
840 1.1 yamaguch }
841 1.1 yamaguch
842 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
843 1.1 yamaguch aqb = NULL;
844 1.1 yamaguch
845 1.1 yamaguch snprintf(xnamebuf, sizeof(xnamebuf),
846 1.1 yamaguch "%s_adminq_cv", device_xname(self));
847 1.1 yamaguch cv_init(&sc->sc_adminq_cv, xnamebuf);
848 1.1 yamaguch
849 1.1 yamaguch callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
850 1.1 yamaguch callout_setfunc(&sc->sc_tick, iavf_tick, sc);
851 1.1 yamaguch
852 1.1 yamaguch iavf_work_set(&sc->sc_reset_task, iavf_reset_start, sc);
853 1.1 yamaguch iavf_work_set(&sc->sc_arq_refill, iavf_arq_refill, sc);
854 1.1 yamaguch iavf_work_set(&sc->sc_wdto_task, iavf_watchdog_timeout, sc);
855 1.1 yamaguch iavf_work_set(&sc->sc_req_queues_task, iavf_post_request_queues, sc);
856 1.1 yamaguch snprintf(xnamebuf, sizeof(xnamebuf), "%s_wq_cfg", device_xname(self));
857 1.1 yamaguch sc->sc_workq = iavf_workq_create(xnamebuf, IAVF_WORKQUEUE_PRI,
858 1.1 yamaguch IPL_NET, WQ_MPSAFE);
859 1.1 yamaguch if (sc->sc_workq == NULL)
860 1.1 yamaguch goto destroy_cv;
861 1.1 yamaguch
862 1.1 yamaguch snprintf(xnamebuf, sizeof(xnamebuf), "%s_wq_txrx", device_xname(self));
863 1.1 yamaguch error = workqueue_create(&sc->sc_workq_txrx, xnamebuf,
864 1.1 yamaguch iavf_handle_queue_wk, sc, IAVF_WORKQUEUE_PRI, IPL_NET,
865 1.1 yamaguch WQ_PERCPU|WQ_MPSAFE);
866 1.1 yamaguch if (error != 0) {
867 1.1 yamaguch sc->sc_workq_txrx = NULL;
868 1.1 yamaguch goto teardown_wqs;
869 1.1 yamaguch }
870 1.1 yamaguch
871 1.1 yamaguch error = if_initialize(ifp);
872 1.1 yamaguch if (error != 0) {
873 1.1 yamaguch aprint_error_dev(self, "if_initialize failed=%d\n", error);
874 1.1 yamaguch goto teardown_wqs;
875 1.1 yamaguch }
876 1.1 yamaguch
877 1.1 yamaguch strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
878 1.1 yamaguch
879 1.1 yamaguch ifp->if_softc = sc;
880 1.1 yamaguch ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
881 1.1 yamaguch ifp->if_extflags = IFEF_MPSAFE;
882 1.1 yamaguch ifp->if_ioctl = iavf_ioctl;
883 1.1 yamaguch ifp->if_start = iavf_start;
884 1.1 yamaguch ifp->if_transmit = iavf_transmit;
885 1.1 yamaguch ifp->if_watchdog = NULL;
886 1.1 yamaguch ifp->if_init = iavf_init;
887 1.1 yamaguch ifp->if_stop = iavf_stop;
888 1.1 yamaguch
889 1.1 yamaguch IFQ_SET_MAXLEN(&ifp->if_snd, sc->sc_tx_ring_ndescs);
890 1.1 yamaguch IFQ_SET_READY(&ifp->if_snd);
891 1.1 yamaguch sc->sc_ipq = if_percpuq_create(ifp);
892 1.1 yamaguch
893 1.1 yamaguch ifp->if_capabilities |= IAVF_IFCAP_RXCSUM;
894 1.1 yamaguch ifp->if_capabilities |= IAVF_IFCAP_TXCSUM;
895 1.1 yamaguch
896 1.1 yamaguch ether_set_vlan_cb(&sc->sc_ec, iavf_vlan_cb);
897 1.1 yamaguch sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
898 1.1 yamaguch sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
899 1.1 yamaguch sc->sc_ec.ec_capenable = sc->sc_ec.ec_capabilities;
900 1.1 yamaguch
901 1.1 yamaguch ether_set_ifflags_cb(&sc->sc_ec, iavf_ifflags_cb);
902 1.1 yamaguch
903 1.1 yamaguch sc->sc_ec.ec_ifmedia = &sc->sc_media;
904 1.1 yamaguch ifmedia_init_with_lock(&sc->sc_media, IFM_IMASK, iavf_media_change,
905 1.1 yamaguch iavf_media_status, &sc->sc_cfg_lock);
906 1.1 yamaguch
907 1.1 yamaguch ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
908 1.1 yamaguch ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
909 1.1 yamaguch
910 1.1 yamaguch if_deferred_start_init(ifp, NULL);
911 1.1 yamaguch ether_ifattach(ifp, sc->sc_enaddr);
912 1.1 yamaguch
913 1.1 yamaguch sc->sc_txrx_workqueue = true;
914 1.1 yamaguch sc->sc_tx_process_limit = IAVF_TX_PROCESS_LIMIT;
915 1.1 yamaguch sc->sc_rx_process_limit = IAVF_RX_PROCESS_LIMIT;
916 1.1 yamaguch sc->sc_tx_intr_process_limit = IAVF_TX_INTR_PROCESS_LIMIT;
917 1.1 yamaguch sc->sc_rx_intr_process_limit = IAVF_RX_INTR_PROCESS_LIMIT;
918 1.1 yamaguch
919 1.1 yamaguch if_register(ifp);
920 1.1 yamaguch if_link_state_change(ifp, sc->sc_link_state);
921 1.1 yamaguch iavf_intr_enable(sc);
922 1.1 yamaguch if (sc->sc_nqps_vsi < sc->sc_nqps_req)
923 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_req_queues_task);
924 1.1 yamaguch sc->sc_attached = true;
925 1.1 yamaguch return;
926 1.1 yamaguch
927 1.1 yamaguch teardown_wqs:
928 1.1 yamaguch config_finalize_register(self, iavf_finalize_teardown);
929 1.1 yamaguch destroy_cv:
930 1.1 yamaguch cv_destroy(&sc->sc_adminq_cv);
931 1.1 yamaguch callout_destroy(&sc->sc_tick);
932 1.1 yamaguch iavf_teardown_stats(sc);
933 1.1 yamaguch teardown_sysctls:
934 1.1 yamaguch iavf_teardown_sysctls(sc);
935 1.1 yamaguch teardown_intrs:
936 1.1 yamaguch iavf_teardown_interrupts(sc);
937 1.1 yamaguch free_queue_pairs:
938 1.1 yamaguch iavf_queue_pairs_free(sc);
939 1.1 yamaguch shutdown:
940 1.1 yamaguch if (aqb != NULL)
941 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
942 1.1 yamaguch iavf_cleanup_admin_queue(sc);
943 1.1 yamaguch iavf_aqb_clean(&sc->sc_atq_idle, sc->sc_dmat);
944 1.1 yamaguch iavf_aqb_clean(&sc->sc_arq_idle, sc->sc_dmat);
945 1.1 yamaguch free_arq:
946 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &sc->sc_arq);
947 1.1 yamaguch free_atq:
948 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &sc->sc_atq);
949 1.1 yamaguch free_mutex:
950 1.1 yamaguch mutex_destroy(&sc->sc_cfg_lock);
951 1.1 yamaguch mutex_destroy(&sc->sc_adminq_lock);
952 1.1 yamaguch unmap:
953 1.1 yamaguch bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
954 1.1 yamaguch sc->sc_mems = 0;
955 1.1 yamaguch sc->sc_attached = false;
956 1.1 yamaguch }
957 1.1 yamaguch
958 1.1 yamaguch static int
959 1.1 yamaguch iavf_detach(device_t self, int flags)
960 1.1 yamaguch {
961 1.1 yamaguch struct iavf_softc *sc = device_private(self);
962 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
963 1.1 yamaguch
964 1.1 yamaguch if (!sc->sc_attached)
965 1.1 yamaguch return 0;
966 1.1 yamaguch
967 1.1 yamaguch iavf_stop(ifp, 1);
968 1.1 yamaguch ether_ifdetach(ifp);
969 1.1 yamaguch if_detach(ifp);
970 1.1 yamaguch ifmedia_fini(&sc->sc_media);
971 1.1 yamaguch if_percpuq_destroy(sc->sc_ipq);
972 1.1 yamaguch
973 1.1 yamaguch iavf_intr_disable(sc);
974 1.1 yamaguch
975 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
976 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
977 1.1 yamaguch
978 1.1 yamaguch /*
979 1.1 yamaguch * set a dummy function to halt callout safely
980 1.1 yamaguch * even if a workqueue entry calls callout_schedule()
981 1.1 yamaguch */
982 1.1 yamaguch callout_setfunc(&sc->sc_tick, iavf_tick_halt, sc);
983 1.1 yamaguch
984 1.1 yamaguch iavf_work_wait(sc->sc_workq, &sc->sc_reset_task);
985 1.1 yamaguch iavf_work_wait(sc->sc_workq, &sc->sc_arq_refill);
986 1.1 yamaguch iavf_work_wait(sc->sc_workq, &sc->sc_wdto_task);
987 1.1 yamaguch iavf_workq_destroy(sc->sc_workq);
988 1.1 yamaguch sc->sc_workq = NULL;
989 1.1 yamaguch
990 1.1 yamaguch callout_halt(&sc->sc_tick, NULL);
991 1.1 yamaguch callout_destroy(&sc->sc_tick);
992 1.1 yamaguch
993 1.1 yamaguch iavf_cleanup_admin_queue(sc);
994 1.1 yamaguch iavf_aqb_clean(&sc->sc_atq_idle, sc->sc_dmat);
995 1.1 yamaguch iavf_aqb_clean(&sc->sc_arq_idle, sc->sc_dmat);
996 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &sc->sc_arq);
997 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &sc->sc_atq);
998 1.1 yamaguch cv_destroy(&sc->sc_adminq_cv);
999 1.1 yamaguch
1000 1.1 yamaguch iavf_queue_pairs_free(sc);
1001 1.1 yamaguch iavf_teardown_interrupts(sc);
1002 1.1 yamaguch iavf_teardown_sysctls(sc);
1003 1.1 yamaguch iavf_teardown_stats(sc);
1004 1.1 yamaguch bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
1005 1.1 yamaguch
1006 1.1 yamaguch mutex_destroy(&sc->sc_adminq_lock);
1007 1.1 yamaguch mutex_destroy(&sc->sc_cfg_lock);
1008 1.1 yamaguch
1009 1.1 yamaguch return 0;
1010 1.1 yamaguch }
1011 1.1 yamaguch
1012 1.1 yamaguch static int
1013 1.1 yamaguch iavf_finalize_teardown(device_t self)
1014 1.1 yamaguch {
1015 1.1 yamaguch struct iavf_softc *sc = device_private(self);
1016 1.1 yamaguch
1017 1.1 yamaguch if (sc->sc_workq != NULL) {
1018 1.1 yamaguch iavf_workq_destroy(sc->sc_workq);
1019 1.1 yamaguch sc->sc_workq = NULL;
1020 1.1 yamaguch }
1021 1.1 yamaguch
1022 1.1 yamaguch if (sc->sc_workq_txrx != NULL) {
1023 1.1 yamaguch workqueue_destroy(sc->sc_workq_txrx);
1024 1.1 yamaguch sc->sc_workq_txrx = NULL;
1025 1.1 yamaguch }
1026 1.1 yamaguch
1027 1.1 yamaguch return 0;
1028 1.1 yamaguch }
1029 1.1 yamaguch
1030 1.1 yamaguch static int
1031 1.1 yamaguch iavf_init(struct ifnet *ifp)
1032 1.1 yamaguch {
1033 1.1 yamaguch struct iavf_softc *sc;
1034 1.1 yamaguch int rv;
1035 1.1 yamaguch
1036 1.1 yamaguch sc = ifp->if_softc;
1037 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
1038 1.1 yamaguch rv = iavf_init_locked(sc);
1039 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
1040 1.1 yamaguch
1041 1.1 yamaguch return rv;
1042 1.1 yamaguch }
1043 1.1 yamaguch
1044 1.1 yamaguch static int
1045 1.1 yamaguch iavf_init_locked(struct iavf_softc *sc)
1046 1.1 yamaguch {
1047 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
1048 1.1 yamaguch unsigned int i;
1049 1.1 yamaguch int error;
1050 1.1 yamaguch
1051 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_cfg_lock));
1052 1.1 yamaguch
1053 1.1 yamaguch if (ISSET(ifp->if_flags, IFF_RUNNING))
1054 1.1 yamaguch iavf_stop_locked(sc);
1055 1.1 yamaguch
1056 1.1 yamaguch if (sc->sc_resetting)
1057 1.1 yamaguch return ENXIO;
1058 1.1 yamaguch
1059 1.1 yamaguch error = iavf_reinit(sc);
1060 1.1 yamaguch if (error) {
1061 1.1 yamaguch iavf_stop_locked(sc);
1062 1.1 yamaguch return error;
1063 1.1 yamaguch }
1064 1.1 yamaguch
1065 1.1 yamaguch SET(ifp->if_flags, IFF_RUNNING);
1066 1.1 yamaguch CLR(ifp->if_flags, IFF_OACTIVE);
1067 1.1 yamaguch
1068 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
1069 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ITRN1(IAVF_ITR_RX, i), sc->sc_rx_itr);
1070 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ITRN1(IAVF_ITR_TX, i), sc->sc_tx_itr);
1071 1.1 yamaguch }
1072 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ITR01(IAVF_ITR_RX), sc->sc_rx_itr);
1073 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ITR01(IAVF_ITR_TX), sc->sc_tx_itr);
1074 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ITR01(IAVF_ITR_MISC), 0);
1075 1.1 yamaguch
1076 1.1 yamaguch error = iavf_iff_locked(sc);
1077 1.1 yamaguch if (error) {
1078 1.1 yamaguch iavf_stop_locked(sc);
1079 1.1 yamaguch return error;
1080 1.1 yamaguch };
1081 1.1 yamaguch
1082 1.1 yamaguch /* ETHERCAP_VLAN_HWFILTER can not be disabled */
1083 1.1 yamaguch SET(sc->sc_ec.ec_capenable, ETHERCAP_VLAN_HWFILTER);
1084 1.1 yamaguch
1085 1.1 yamaguch callout_schedule(&sc->sc_tick, IAVF_TICK_INTERVAL);
1086 1.1 yamaguch return 0;
1087 1.1 yamaguch }
1088 1.1 yamaguch
1089 1.1 yamaguch static int
1090 1.1 yamaguch iavf_reinit(struct iavf_softc *sc)
1091 1.1 yamaguch {
1092 1.1 yamaguch struct iavf_rx_ring *rxr;
1093 1.1 yamaguch struct iavf_tx_ring *txr;
1094 1.1 yamaguch unsigned int i;
1095 1.1 yamaguch uint32_t reg;
1096 1.1 yamaguch
1097 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_cfg_lock));
1098 1.1 yamaguch
1099 1.1 yamaguch sc->sc_reset_up = true;
1100 1.1 yamaguch sc->sc_nqueue_pairs = MIN(sc->sc_nqps_alloc, sc->sc_nintrs - 1);
1101 1.1 yamaguch
1102 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
1103 1.1 yamaguch rxr = sc->sc_qps[i].qp_rxr;
1104 1.1 yamaguch txr = sc->sc_qps[i].qp_txr;
1105 1.1 yamaguch
1106 1.1 yamaguch iavf_rxfill(sc, rxr);
1107 1.1 yamaguch txr->txr_watchdog = IAVF_WATCHDOG_STOP;
1108 1.1 yamaguch }
1109 1.1 yamaguch
1110 1.1 yamaguch if (iavf_config_vsi_queues(sc) != 0)
1111 1.1 yamaguch return EIO;
1112 1.1 yamaguch
1113 1.1 yamaguch if (iavf_config_hena(sc) != 0)
1114 1.1 yamaguch return EIO;
1115 1.1 yamaguch
1116 1.1 yamaguch iavf_config_rss_key(sc);
1117 1.1 yamaguch iavf_config_rss_lut(sc);
1118 1.1 yamaguch
1119 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
1120 1.1 yamaguch iavf_queue_intr_enable(sc, i);
1121 1.1 yamaguch }
1122 1.1 yamaguch /* unmask */
1123 1.1 yamaguch reg = iavf_rd(sc, I40E_VFINT_DYN_CTL01);
1124 1.1 yamaguch reg |= (IAVF_NOITR << I40E_VFINT_DYN_CTL0_ITR_INDX_SHIFT);
1125 1.1 yamaguch iavf_wr(sc, I40E_VFINT_DYN_CTL01, reg);
1126 1.1 yamaguch
1127 1.1 yamaguch if (iavf_queue_select(sc, IAVF_VC_OP_ENABLE_QUEUES) != 0)
1128 1.1 yamaguch return EIO;
1129 1.1 yamaguch
1130 1.1 yamaguch return 0;
1131 1.1 yamaguch }
1132 1.1 yamaguch
1133 1.1 yamaguch static void
1134 1.1 yamaguch iavf_stop(struct ifnet *ifp, int disable)
1135 1.1 yamaguch {
1136 1.1 yamaguch struct iavf_softc *sc;
1137 1.1 yamaguch
1138 1.1 yamaguch sc = ifp->if_softc;
1139 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
1140 1.1 yamaguch iavf_stop_locked(sc);
1141 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
1142 1.1 yamaguch }
1143 1.1 yamaguch
1144 1.1 yamaguch static void
1145 1.1 yamaguch iavf_stop_locked(struct iavf_softc *sc)
1146 1.1 yamaguch {
1147 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
1148 1.1 yamaguch struct iavf_rx_ring *rxr;
1149 1.1 yamaguch struct iavf_tx_ring *txr;
1150 1.1 yamaguch uint32_t reg;
1151 1.1 yamaguch unsigned int i;
1152 1.1 yamaguch
1153 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_cfg_lock));
1154 1.1 yamaguch
1155 1.1 yamaguch CLR(ifp->if_flags, IFF_RUNNING);
1156 1.1 yamaguch sc->sc_reset_up = false;
1157 1.1 yamaguch callout_stop(&sc->sc_tick);
1158 1.1 yamaguch
1159 1.1 yamaguch if (!sc->sc_resetting) {
1160 1.1 yamaguch /* disable queues*/
1161 1.1 yamaguch if (iavf_queue_select(sc, IAVF_VC_OP_DISABLE_QUEUES) != 0) {
1162 1.1 yamaguch goto die;
1163 1.1 yamaguch }
1164 1.1 yamaguch }
1165 1.1 yamaguch
1166 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
1167 1.1 yamaguch iavf_queue_intr_disable(sc, i);
1168 1.1 yamaguch }
1169 1.1 yamaguch
1170 1.1 yamaguch /* mask interrupts */
1171 1.1 yamaguch reg = iavf_rd(sc, I40E_VFINT_DYN_CTL01);
1172 1.1 yamaguch reg |= I40E_VFINT_DYN_CTL0_INTENA_MSK_MASK |
1173 1.1 yamaguch (IAVF_NOITR << I40E_VFINT_DYN_CTL0_ITR_INDX_SHIFT);
1174 1.1 yamaguch iavf_wr(sc, I40E_VFINT_DYN_CTL01, reg);
1175 1.1 yamaguch
1176 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
1177 1.1 yamaguch rxr = sc->sc_qps[i].qp_rxr;
1178 1.1 yamaguch txr = sc->sc_qps[i].qp_txr;
1179 1.1 yamaguch
1180 1.1 yamaguch mutex_enter(&rxr->rxr_lock);
1181 1.1 yamaguch iavf_rxr_clean(sc, rxr);
1182 1.1 yamaguch mutex_exit(&rxr->rxr_lock);
1183 1.1 yamaguch
1184 1.1 yamaguch mutex_enter(&txr->txr_lock);
1185 1.1 yamaguch iavf_txr_clean(sc, txr);
1186 1.1 yamaguch mutex_exit(&txr->txr_lock);
1187 1.1 yamaguch
1188 1.1 yamaguch workqueue_wait(sc->sc_workq_txrx,
1189 1.1 yamaguch &sc->sc_qps[i].qp_work);
1190 1.1 yamaguch }
1191 1.1 yamaguch
1192 1.1 yamaguch return;
1193 1.1 yamaguch die:
1194 1.1 yamaguch if (!sc->sc_dead) {
1195 1.1 yamaguch sc->sc_dead = true;
1196 1.1 yamaguch log(LOG_INFO, "%s: Request VF reset\n", ifp->if_xname);
1197 1.1 yamaguch
1198 1.1 yamaguch iavf_work_set(&sc->sc_reset_task, iavf_reset_request, sc);
1199 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_reset_task);
1200 1.1 yamaguch }
1201 1.1 yamaguch log(LOG_CRIT, "%s: failed to shut down rings\n", ifp->if_xname);
1202 1.1 yamaguch }
1203 1.1 yamaguch
1204 1.1 yamaguch static int
1205 1.1 yamaguch iavf_watchdog(struct iavf_tx_ring *txr)
1206 1.1 yamaguch {
1207 1.1 yamaguch struct iavf_softc *sc;
1208 1.1 yamaguch
1209 1.1 yamaguch sc = txr->txr_sc;
1210 1.1 yamaguch
1211 1.1 yamaguch mutex_enter(&txr->txr_lock);
1212 1.1 yamaguch
1213 1.1 yamaguch if (txr->txr_watchdog == IAVF_WATCHDOG_STOP
1214 1.1 yamaguch || --txr->txr_watchdog > 0) {
1215 1.1 yamaguch mutex_exit(&txr->txr_lock);
1216 1.1 yamaguch return 0;
1217 1.1 yamaguch }
1218 1.1 yamaguch
1219 1.1 yamaguch txr->txr_watchdog = IAVF_WATCHDOG_STOP;
1220 1.1 yamaguch txr->txr_watchdogto.ev_count++;
1221 1.1 yamaguch mutex_exit(&txr->txr_lock);
1222 1.1 yamaguch
1223 1.1 yamaguch device_printf(sc->sc_dev, "watchdog timeout on queue %d\n",
1224 1.1 yamaguch txr->txr_qid);
1225 1.1 yamaguch return 1;
1226 1.1 yamaguch }
1227 1.1 yamaguch
1228 1.1 yamaguch static void
1229 1.1 yamaguch iavf_watchdog_timeout(void *xsc)
1230 1.1 yamaguch {
1231 1.1 yamaguch struct iavf_softc *sc;
1232 1.1 yamaguch struct ifnet *ifp;
1233 1.1 yamaguch
1234 1.1 yamaguch sc = xsc;
1235 1.1 yamaguch ifp = &sc->sc_ec.ec_if;
1236 1.1 yamaguch
1237 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
1238 1.1 yamaguch if (ISSET(ifp->if_flags, IFF_RUNNING))
1239 1.1 yamaguch iavf_init_locked(sc);
1240 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
1241 1.1 yamaguch }
1242 1.1 yamaguch
1243 1.1 yamaguch static int
1244 1.1 yamaguch iavf_media_change(struct ifnet *ifp)
1245 1.1 yamaguch {
1246 1.1 yamaguch struct iavf_softc *sc;
1247 1.1 yamaguch struct ifmedia *ifm;
1248 1.1 yamaguch
1249 1.1 yamaguch sc = ifp->if_softc;
1250 1.1 yamaguch ifm = &sc->sc_media;
1251 1.1 yamaguch
1252 1.1 yamaguch if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1253 1.1 yamaguch return EINVAL;
1254 1.1 yamaguch
1255 1.1 yamaguch switch (IFM_SUBTYPE(ifm->ifm_media)) {
1256 1.1 yamaguch case IFM_AUTO:
1257 1.1 yamaguch break;
1258 1.1 yamaguch default:
1259 1.1 yamaguch return EINVAL;
1260 1.1 yamaguch }
1261 1.1 yamaguch
1262 1.1 yamaguch return 0;
1263 1.1 yamaguch }
1264 1.1 yamaguch
1265 1.1 yamaguch static void
1266 1.1 yamaguch iavf_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1267 1.1 yamaguch {
1268 1.1 yamaguch struct iavf_softc *sc = ifp->if_softc;
1269 1.1 yamaguch
1270 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_cfg_lock));
1271 1.1 yamaguch
1272 1.1 yamaguch ifmr->ifm_status = sc->sc_media_status;
1273 1.1 yamaguch ifmr->ifm_active = sc->sc_media_active;
1274 1.1 yamaguch }
1275 1.1 yamaguch
1276 1.1 yamaguch static int
1277 1.1 yamaguch iavf_ifflags_cb(struct ethercom *ec)
1278 1.1 yamaguch {
1279 1.1 yamaguch struct ifnet *ifp = &ec->ec_if;
1280 1.1 yamaguch struct iavf_softc *sc = ifp->if_softc;
1281 1.1 yamaguch
1282 1.1 yamaguch /* vlan hwfilter can not be disabled */
1283 1.1 yamaguch SET(ec->ec_capenable, ETHERCAP_VLAN_HWFILTER);
1284 1.1 yamaguch
1285 1.1 yamaguch return iavf_iff(sc);
1286 1.1 yamaguch }
1287 1.1 yamaguch
1288 1.1 yamaguch static int
1289 1.1 yamaguch iavf_vlan_cb(struct ethercom *ec, uint16_t vid, bool set)
1290 1.1 yamaguch {
1291 1.1 yamaguch struct ifnet *ifp = &ec->ec_if;
1292 1.1 yamaguch struct iavf_softc *sc = ifp->if_softc;
1293 1.1 yamaguch int rv;
1294 1.1 yamaguch
1295 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
1296 1.1 yamaguch
1297 1.1 yamaguch if (sc->sc_resetting) {
1298 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
1299 1.1 yamaguch
1300 1.1 yamaguch /* all vlan id was already removed */
1301 1.1 yamaguch if (!set)
1302 1.1 yamaguch return 0;
1303 1.1 yamaguch
1304 1.1 yamaguch return ENXIO;
1305 1.1 yamaguch }
1306 1.1 yamaguch
1307 1.1 yamaguch /* ETHERCAP_VLAN_HWFILTER can not be disabled */
1308 1.1 yamaguch SET(sc->sc_ec.ec_capenable, ETHERCAP_VLAN_HWFILTER);
1309 1.1 yamaguch
1310 1.1 yamaguch if (set) {
1311 1.1 yamaguch rv = iavf_config_vlan_id(sc, vid, IAVF_VC_OP_ADD_VLAN);
1312 1.1 yamaguch if (!ISSET(sc->sc_ec.ec_capenable, ETHERCAP_VLAN_HWTAGGING)) {
1313 1.1 yamaguch iavf_config_vlan_stripping(sc,
1314 1.1 yamaguch sc->sc_ec.ec_capenable);
1315 1.1 yamaguch }
1316 1.1 yamaguch } else {
1317 1.1 yamaguch rv = iavf_config_vlan_id(sc, vid, IAVF_VC_OP_DEL_VLAN);
1318 1.1 yamaguch }
1319 1.1 yamaguch
1320 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
1321 1.1 yamaguch
1322 1.1 yamaguch if (rv != 0)
1323 1.1 yamaguch return EIO;
1324 1.1 yamaguch
1325 1.1 yamaguch return 0;
1326 1.1 yamaguch }
1327 1.1 yamaguch
1328 1.1 yamaguch static int
1329 1.1 yamaguch iavf_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1330 1.1 yamaguch {
1331 1.1 yamaguch struct ifreq *ifr = (struct ifreq *)data;
1332 1.1 yamaguch struct iavf_softc *sc = (struct iavf_softc *)ifp->if_softc;
1333 1.1 yamaguch const struct sockaddr *sa;
1334 1.1 yamaguch uint8_t addrhi[ETHER_ADDR_LEN], addrlo[ETHER_ADDR_LEN];
1335 1.1 yamaguch int s, error = 0;
1336 1.1 yamaguch unsigned int nmtu;
1337 1.1 yamaguch
1338 1.1 yamaguch switch (cmd) {
1339 1.1 yamaguch case SIOCSIFMTU:
1340 1.1 yamaguch nmtu = ifr->ifr_mtu;
1341 1.1 yamaguch
1342 1.1 yamaguch if (nmtu < IAVF_MIN_MTU || nmtu > IAVF_MAX_MTU) {
1343 1.1 yamaguch error = EINVAL;
1344 1.1 yamaguch break;
1345 1.1 yamaguch }
1346 1.1 yamaguch if (ifp->if_mtu != nmtu) {
1347 1.1 yamaguch s = splnet();
1348 1.1 yamaguch error = ether_ioctl(ifp, cmd, data);
1349 1.1 yamaguch splx(s);
1350 1.1 yamaguch if (error == ENETRESET)
1351 1.1 yamaguch error = iavf_init(ifp);
1352 1.1 yamaguch }
1353 1.1 yamaguch break;
1354 1.1 yamaguch case SIOCADDMULTI:
1355 1.1 yamaguch sa = ifreq_getaddr(SIOCADDMULTI, ifr);
1356 1.1 yamaguch if (ether_addmulti(sa, &sc->sc_ec) == ENETRESET) {
1357 1.1 yamaguch error = ether_multiaddr(sa, addrlo, addrhi);
1358 1.1 yamaguch if (error != 0)
1359 1.1 yamaguch return error;
1360 1.1 yamaguch
1361 1.1 yamaguch error = iavf_add_multi(sc, addrlo, addrhi);
1362 1.1 yamaguch if (error != 0 && error != ENETRESET) {
1363 1.1 yamaguch ether_delmulti(sa, &sc->sc_ec);
1364 1.1 yamaguch error = EIO;
1365 1.1 yamaguch }
1366 1.1 yamaguch }
1367 1.1 yamaguch break;
1368 1.1 yamaguch
1369 1.1 yamaguch case SIOCDELMULTI:
1370 1.1 yamaguch sa = ifreq_getaddr(SIOCDELMULTI, ifr);
1371 1.1 yamaguch if (ether_delmulti(sa, &sc->sc_ec) == ENETRESET) {
1372 1.1 yamaguch error = ether_multiaddr(sa, addrlo, addrhi);
1373 1.1 yamaguch if (error != 0)
1374 1.1 yamaguch return error;
1375 1.1 yamaguch
1376 1.1 yamaguch error = iavf_del_multi(sc, addrlo, addrhi);
1377 1.1 yamaguch }
1378 1.1 yamaguch break;
1379 1.1 yamaguch
1380 1.1 yamaguch default:
1381 1.1 yamaguch s = splnet();
1382 1.1 yamaguch error = ether_ioctl(ifp, cmd, data);
1383 1.1 yamaguch splx(s);
1384 1.1 yamaguch }
1385 1.1 yamaguch
1386 1.1 yamaguch if (error == ENETRESET)
1387 1.1 yamaguch error = iavf_iff(sc);
1388 1.1 yamaguch
1389 1.1 yamaguch return error;
1390 1.1 yamaguch }
1391 1.1 yamaguch
1392 1.1 yamaguch static int
1393 1.1 yamaguch iavf_iff(struct iavf_softc *sc)
1394 1.1 yamaguch {
1395 1.1 yamaguch int error;
1396 1.1 yamaguch
1397 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
1398 1.1 yamaguch error = iavf_iff_locked(sc);
1399 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
1400 1.1 yamaguch
1401 1.1 yamaguch return error;
1402 1.1 yamaguch }
1403 1.1 yamaguch
1404 1.1 yamaguch static int
1405 1.1 yamaguch iavf_iff_locked(struct iavf_softc *sc)
1406 1.1 yamaguch {
1407 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
1408 1.1 yamaguch int unicast, multicast;
1409 1.1 yamaguch const uint8_t *enaddr;
1410 1.1 yamaguch
1411 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_cfg_lock));
1412 1.1 yamaguch
1413 1.1 yamaguch if (!ISSET(ifp->if_flags, IFF_RUNNING))
1414 1.1 yamaguch return 0;
1415 1.1 yamaguch
1416 1.1 yamaguch unicast = 0;
1417 1.1 yamaguch multicast = 0;
1418 1.1 yamaguch if (ISSET(ifp->if_flags, IFF_PROMISC)) {
1419 1.1 yamaguch unicast = 1;
1420 1.1 yamaguch multicast = 1;
1421 1.1 yamaguch } else if (ISSET(ifp->if_flags, IFF_ALLMULTI)) {
1422 1.1 yamaguch multicast = 1;
1423 1.1 yamaguch }
1424 1.1 yamaguch
1425 1.1 yamaguch iavf_config_promisc_mode(sc, unicast, multicast);
1426 1.1 yamaguch
1427 1.1 yamaguch iavf_config_vlan_stripping(sc, sc->sc_ec.ec_capenable);
1428 1.1 yamaguch
1429 1.1 yamaguch enaddr = CLLADDR(ifp->if_sadl);
1430 1.1 yamaguch if (memcmp(enaddr, sc->sc_enaddr_added, ETHER_ADDR_LEN) != 0) {
1431 1.1 yamaguch if (!iavf_is_etheranyaddr(sc->sc_enaddr_added)) {
1432 1.1 yamaguch iavf_eth_addr(sc, sc->sc_enaddr_added,
1433 1.1 yamaguch IAVF_VC_OP_DEL_ETH_ADDR);
1434 1.1 yamaguch }
1435 1.1 yamaguch memcpy(sc->sc_enaddr_added, enaddr, ETHER_ADDR_LEN);
1436 1.1 yamaguch iavf_eth_addr(sc, enaddr, IAVF_VC_OP_ADD_ETH_ADDR);
1437 1.1 yamaguch }
1438 1.1 yamaguch
1439 1.1 yamaguch return 0;
1440 1.1 yamaguch }
1441 1.1 yamaguch
1442 1.1 yamaguch static const struct iavf_product *
1443 1.1 yamaguch iavf_lookup(const struct pci_attach_args *pa)
1444 1.1 yamaguch {
1445 1.1 yamaguch const struct iavf_product *iavfp;
1446 1.1 yamaguch
1447 1.1 yamaguch for (iavfp = iavf_products; iavfp->vendor_id != 0; iavfp++) {
1448 1.1 yamaguch if (PCI_VENDOR(pa->pa_id) == iavfp->vendor_id &&
1449 1.1 yamaguch PCI_PRODUCT(pa->pa_id) == iavfp->product_id)
1450 1.1 yamaguch return iavfp;
1451 1.1 yamaguch }
1452 1.1 yamaguch
1453 1.1 yamaguch return NULL;
1454 1.1 yamaguch }
1455 1.1 yamaguch
1456 1.1 yamaguch static enum i40e_mac_type
1457 1.1 yamaguch iavf_mactype(pci_product_id_t id)
1458 1.1 yamaguch {
1459 1.1 yamaguch
1460 1.1 yamaguch switch (id) {
1461 1.1 yamaguch case PCI_PRODUCT_INTEL_XL710_VF:
1462 1.1 yamaguch case PCI_PRODUCT_INTEL_XL710_VF_HV:
1463 1.1 yamaguch return I40E_MAC_VF;
1464 1.1 yamaguch case PCI_PRODUCT_INTEL_X722_VF:
1465 1.1 yamaguch return I40E_MAC_X722_VF;
1466 1.1 yamaguch }
1467 1.1 yamaguch
1468 1.1 yamaguch return I40E_MAC_GENERIC;
1469 1.1 yamaguch }
1470 1.1 yamaguch
1471 1.1 yamaguch static const struct iavf_link_speed *
1472 1.1 yamaguch iavf_find_link_speed(struct iavf_softc *sc, uint32_t link_speed)
1473 1.1 yamaguch {
1474 1.1 yamaguch size_t i;
1475 1.1 yamaguch
1476 1.1 yamaguch for (i = 0; i < __arraycount(iavf_link_speeds); i++) {
1477 1.1 yamaguch if (link_speed & (1 << i))
1478 1.1 yamaguch return (&iavf_link_speeds[i]);
1479 1.1 yamaguch }
1480 1.1 yamaguch
1481 1.1 yamaguch return NULL;
1482 1.1 yamaguch }
1483 1.1 yamaguch
1484 1.1 yamaguch static void
1485 1.1 yamaguch iavf_pci_csr_setup(pci_chipset_tag_t pc, pcitag_t tag)
1486 1.1 yamaguch {
1487 1.1 yamaguch pcireg_t csr;
1488 1.1 yamaguch
1489 1.1 yamaguch csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
1490 1.1 yamaguch csr |= (PCI_COMMAND_MASTER_ENABLE |
1491 1.1 yamaguch PCI_COMMAND_MEM_ENABLE);
1492 1.1 yamaguch pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
1493 1.1 yamaguch }
1494 1.1 yamaguch
1495 1.1 yamaguch static int
1496 1.1 yamaguch iavf_wait_active(struct iavf_softc *sc)
1497 1.1 yamaguch {
1498 1.1 yamaguch int tries;
1499 1.1 yamaguch uint32_t reg;
1500 1.1 yamaguch
1501 1.1 yamaguch for (tries = 0; tries < 100; tries++) {
1502 1.1 yamaguch reg = iavf_rd(sc, I40E_VFGEN_RSTAT) &
1503 1.1 yamaguch I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1504 1.1 yamaguch if (reg == IAVF_VFR_VFACTIVE ||
1505 1.1 yamaguch reg == IAVF_VFR_COMPLETED)
1506 1.1 yamaguch return 0;
1507 1.1 yamaguch
1508 1.1 yamaguch delaymsec(10);
1509 1.1 yamaguch }
1510 1.1 yamaguch
1511 1.1 yamaguch return -1;
1512 1.1 yamaguch }
1513 1.1 yamaguch
1514 1.1 yamaguch static bool
1515 1.1 yamaguch iavf_is_etheranyaddr(const uint8_t *enaddr)
1516 1.1 yamaguch {
1517 1.1 yamaguch static const uint8_t etheranyaddr[ETHER_ADDR_LEN] = {
1518 1.1 yamaguch 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1519 1.1 yamaguch };
1520 1.1 yamaguch
1521 1.1 yamaguch if (memcmp(enaddr, etheranyaddr, ETHER_ADDR_LEN) != 0)
1522 1.1 yamaguch return false;
1523 1.1 yamaguch
1524 1.1 yamaguch return true;
1525 1.1 yamaguch }
1526 1.1 yamaguch
1527 1.1 yamaguch static void
1528 1.1 yamaguch iavf_prepare_fakeaddr(struct iavf_softc *sc)
1529 1.1 yamaguch {
1530 1.1 yamaguch uint64_t rndval;
1531 1.1 yamaguch
1532 1.1 yamaguch if (!iavf_is_etheranyaddr(sc->sc_enaddr_fake))
1533 1.1 yamaguch return;
1534 1.1 yamaguch
1535 1.1 yamaguch rndval = cprng_strong64();
1536 1.1 yamaguch
1537 1.1 yamaguch memcpy(sc->sc_enaddr_fake, &rndval, sizeof(sc->sc_enaddr_fake));
1538 1.1 yamaguch sc->sc_enaddr_fake[0] &= 0xFE;
1539 1.1 yamaguch sc->sc_enaddr_fake[0] |= 0x02;
1540 1.1 yamaguch }
1541 1.1 yamaguch
1542 1.1 yamaguch static int
1543 1.1 yamaguch iavf_replace_lla(struct ifnet *ifp, const uint8_t *prev, const uint8_t *next)
1544 1.1 yamaguch {
1545 1.1 yamaguch union {
1546 1.1 yamaguch struct sockaddr sa;
1547 1.1 yamaguch struct sockaddr_dl sdl;
1548 1.1 yamaguch struct sockaddr_storage ss;
1549 1.1 yamaguch } u;
1550 1.1 yamaguch struct psref psref_prev, psref_next;
1551 1.1 yamaguch struct ifaddr *ifa_prev, *ifa_next;
1552 1.1 yamaguch const struct sockaddr_dl *nsdl;
1553 1.1 yamaguch int s, error;
1554 1.1 yamaguch
1555 1.1 yamaguch KASSERT(IFNET_LOCKED(ifp));
1556 1.1 yamaguch
1557 1.1 yamaguch error = 0;
1558 1.1 yamaguch ifa_prev = ifa_next = NULL;
1559 1.1 yamaguch
1560 1.1 yamaguch if (memcmp(prev, next, ETHER_ADDR_LEN) == 0) {
1561 1.1 yamaguch goto done;
1562 1.1 yamaguch }
1563 1.1 yamaguch
1564 1.1 yamaguch if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index,
1565 1.1 yamaguch ifp->if_type, ifp->if_xname, strlen(ifp->if_xname),
1566 1.1 yamaguch prev, ETHER_ADDR_LEN) == NULL) {
1567 1.1 yamaguch error = EINVAL;
1568 1.1 yamaguch goto done;
1569 1.1 yamaguch }
1570 1.1 yamaguch
1571 1.1 yamaguch s = pserialize_read_enter();
1572 1.1 yamaguch IFADDR_READER_FOREACH(ifa_prev, ifp) {
1573 1.1 yamaguch if (sockaddr_cmp(&u.sa, ifa_prev->ifa_addr) == 0) {
1574 1.1 yamaguch ifa_acquire(ifa_prev, &psref_prev);
1575 1.1 yamaguch break;
1576 1.1 yamaguch }
1577 1.1 yamaguch }
1578 1.1 yamaguch pserialize_read_exit(s);
1579 1.1 yamaguch
1580 1.1 yamaguch if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index,
1581 1.1 yamaguch ifp->if_type, ifp->if_xname, strlen(ifp->if_xname),
1582 1.1 yamaguch next, ETHER_ADDR_LEN) == NULL) {
1583 1.1 yamaguch error = EINVAL;
1584 1.1 yamaguch goto done;
1585 1.1 yamaguch }
1586 1.1 yamaguch
1587 1.1 yamaguch s = pserialize_read_enter();
1588 1.1 yamaguch IFADDR_READER_FOREACH(ifa_next, ifp) {
1589 1.1 yamaguch if (sockaddr_cmp(&u.sa, ifa_next->ifa_addr) == 0) {
1590 1.1 yamaguch ifa_acquire(ifa_next, &psref_next);
1591 1.1 yamaguch break;
1592 1.1 yamaguch }
1593 1.1 yamaguch }
1594 1.1 yamaguch pserialize_read_exit(s);
1595 1.1 yamaguch
1596 1.1 yamaguch if (ifa_next == NULL) {
1597 1.1 yamaguch nsdl = &u.sdl;
1598 1.1 yamaguch ifa_next = if_dl_create(ifp, &nsdl);
1599 1.1 yamaguch if (ifa_next == NULL) {
1600 1.1 yamaguch error = ENOMEM;
1601 1.1 yamaguch goto done;
1602 1.1 yamaguch }
1603 1.1 yamaguch
1604 1.1 yamaguch s = pserialize_read_enter();
1605 1.1 yamaguch ifa_acquire(ifa_next, &psref_next);
1606 1.1 yamaguch pserialize_read_exit(s);
1607 1.1 yamaguch
1608 1.1 yamaguch sockaddr_copy(ifa_next->ifa_addr,
1609 1.1 yamaguch ifa_next->ifa_addr->sa_len, &u.sa);
1610 1.1 yamaguch ifa_insert(ifp, ifa_next);
1611 1.1 yamaguch } else {
1612 1.1 yamaguch nsdl = NULL;
1613 1.1 yamaguch }
1614 1.1 yamaguch
1615 1.1 yamaguch if (ifa_prev != NULL && ifa_prev == ifp->if_dl) {
1616 1.1 yamaguch if_activate_sadl(ifp, ifa_next, nsdl);
1617 1.1 yamaguch }
1618 1.1 yamaguch
1619 1.1 yamaguch ifa_release(ifa_next, &psref_next);
1620 1.1 yamaguch ifa_next = NULL;
1621 1.1 yamaguch
1622 1.1 yamaguch if (ifa_prev != NULL && ifa_prev != ifp->if_hwdl) {
1623 1.1 yamaguch ifaref(ifa_prev);
1624 1.1 yamaguch ifa_release(ifa_prev, &psref_prev);
1625 1.1 yamaguch ifa_remove(ifp, ifa_prev);
1626 1.1 yamaguch KASSERTMSG(ifa_prev->ifa_refcnt == 1, "ifa_refcnt=%d",
1627 1.1 yamaguch ifa_prev->ifa_refcnt);
1628 1.1 yamaguch ifafree(ifa_prev);
1629 1.1 yamaguch ifa_prev = NULL;
1630 1.1 yamaguch }
1631 1.1 yamaguch
1632 1.1 yamaguch if (ISSET(ifp->if_flags, IFF_RUNNING))
1633 1.1 yamaguch error = ENETRESET;
1634 1.1 yamaguch
1635 1.1 yamaguch done:
1636 1.1 yamaguch if (ifa_prev != NULL)
1637 1.1 yamaguch ifa_release(ifa_prev, &psref_prev);
1638 1.1 yamaguch if (ifa_next != NULL)
1639 1.1 yamaguch ifa_release(ifa_next, &psref_next);
1640 1.1 yamaguch
1641 1.1 yamaguch return error;
1642 1.1 yamaguch }
1643 1.1 yamaguch static int
1644 1.1 yamaguch iavf_add_multi(struct iavf_softc *sc, uint8_t *addrlo, uint8_t *addrhi)
1645 1.1 yamaguch {
1646 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
1647 1.1 yamaguch int rv;
1648 1.1 yamaguch
1649 1.1 yamaguch if (ISSET(ifp->if_flags, IFF_ALLMULTI))
1650 1.1 yamaguch return 0;
1651 1.1 yamaguch
1652 1.1 yamaguch if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) {
1653 1.1 yamaguch iavf_del_all_multi(sc);
1654 1.1 yamaguch SET(ifp->if_flags, IFF_ALLMULTI);
1655 1.1 yamaguch return ENETRESET;
1656 1.1 yamaguch }
1657 1.1 yamaguch
1658 1.1 yamaguch rv = iavf_eth_addr(sc, addrlo, IAVF_VC_OP_ADD_ETH_ADDR);
1659 1.1 yamaguch
1660 1.1 yamaguch if (rv == ENOSPC) {
1661 1.1 yamaguch iavf_del_all_multi(sc);
1662 1.1 yamaguch SET(ifp->if_flags, IFF_ALLMULTI);
1663 1.1 yamaguch return ENETRESET;
1664 1.1 yamaguch }
1665 1.1 yamaguch
1666 1.1 yamaguch return rv;
1667 1.1 yamaguch }
1668 1.1 yamaguch
1669 1.1 yamaguch static int
1670 1.1 yamaguch iavf_del_multi(struct iavf_softc *sc, uint8_t *addrlo, uint8_t *addrhi)
1671 1.1 yamaguch {
1672 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
1673 1.1 yamaguch struct ethercom *ec = &sc->sc_ec;
1674 1.1 yamaguch struct ether_multi *enm, *enm_last;
1675 1.1 yamaguch struct ether_multistep step;
1676 1.1 yamaguch int error, rv = 0;
1677 1.1 yamaguch
1678 1.1 yamaguch if (!ISSET(ifp->if_flags, IFF_ALLMULTI)) {
1679 1.1 yamaguch if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0)
1680 1.1 yamaguch return 0;
1681 1.1 yamaguch
1682 1.1 yamaguch iavf_eth_addr(sc, addrlo, IAVF_VC_OP_DEL_ETH_ADDR);
1683 1.1 yamaguch return 0;
1684 1.1 yamaguch }
1685 1.1 yamaguch
1686 1.1 yamaguch ETHER_LOCK(ec);
1687 1.1 yamaguch for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
1688 1.1 yamaguch ETHER_NEXT_MULTI(step, enm)) {
1689 1.1 yamaguch if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1690 1.1 yamaguch ETHER_ADDR_LEN) != 0) {
1691 1.1 yamaguch goto out;
1692 1.1 yamaguch }
1693 1.1 yamaguch }
1694 1.1 yamaguch
1695 1.1 yamaguch for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
1696 1.1 yamaguch ETHER_NEXT_MULTI(step, enm)) {
1697 1.1 yamaguch error = iavf_eth_addr(sc, enm->enm_addrlo,
1698 1.1 yamaguch IAVF_VC_OP_ADD_ETH_ADDR);
1699 1.1 yamaguch if (error != 0)
1700 1.1 yamaguch break;
1701 1.1 yamaguch }
1702 1.1 yamaguch
1703 1.1 yamaguch if (enm != NULL) {
1704 1.1 yamaguch enm_last = enm;
1705 1.1 yamaguch for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
1706 1.1 yamaguch ETHER_NEXT_MULTI(step, enm)) {
1707 1.1 yamaguch if (enm == enm_last)
1708 1.1 yamaguch break;
1709 1.1 yamaguch
1710 1.1 yamaguch iavf_eth_addr(sc, enm->enm_addrlo,
1711 1.1 yamaguch IAVF_VC_OP_DEL_ETH_ADDR);
1712 1.1 yamaguch }
1713 1.1 yamaguch } else {
1714 1.1 yamaguch CLR(ifp->if_flags, IFF_ALLMULTI);
1715 1.1 yamaguch rv = ENETRESET;
1716 1.1 yamaguch }
1717 1.1 yamaguch
1718 1.1 yamaguch out:
1719 1.1 yamaguch ETHER_UNLOCK(ec);
1720 1.1 yamaguch return rv;
1721 1.1 yamaguch }
1722 1.1 yamaguch
1723 1.1 yamaguch static void
1724 1.1 yamaguch iavf_del_all_multi(struct iavf_softc *sc)
1725 1.1 yamaguch {
1726 1.1 yamaguch struct ethercom *ec = &sc->sc_ec;
1727 1.1 yamaguch struct ether_multi *enm;
1728 1.1 yamaguch struct ether_multistep step;
1729 1.1 yamaguch
1730 1.1 yamaguch ETHER_LOCK(ec);
1731 1.1 yamaguch for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
1732 1.1 yamaguch ETHER_NEXT_MULTI(step, enm)) {
1733 1.1 yamaguch iavf_eth_addr(sc, enm->enm_addrlo,
1734 1.1 yamaguch IAVF_VC_OP_DEL_ETH_ADDR);
1735 1.1 yamaguch }
1736 1.1 yamaguch ETHER_UNLOCK(ec);
1737 1.1 yamaguch }
1738 1.1 yamaguch
1739 1.1 yamaguch static int
1740 1.1 yamaguch iavf_setup_interrupts(struct iavf_softc *sc)
1741 1.1 yamaguch {
1742 1.1 yamaguch struct pci_attach_args *pa;
1743 1.1 yamaguch kcpuset_t *affinity = NULL;
1744 1.1 yamaguch char intrbuf[PCI_INTRSTR_LEN], xnamebuf[32];
1745 1.1 yamaguch char const *intrstr;
1746 1.1 yamaguch int counts[PCI_INTR_TYPE_SIZE];
1747 1.1 yamaguch int error, affinity_to;
1748 1.1 yamaguch unsigned int vector, qid, num;
1749 1.1 yamaguch
1750 1.1 yamaguch /* queue pairs + misc interrupt */
1751 1.1 yamaguch num = sc->sc_nqps_alloc + 1;
1752 1.1 yamaguch
1753 1.1 yamaguch num = MIN(num, iavf_calc_msix_count(sc));
1754 1.1 yamaguch if (num <= 0) {
1755 1.1 yamaguch return -1;
1756 1.1 yamaguch }
1757 1.1 yamaguch
1758 1.1 yamaguch KASSERT(sc->sc_nqps_alloc > 0);
1759 1.1 yamaguch num = MIN(num, sc->sc_nqps_alloc + 1);
1760 1.1 yamaguch
1761 1.1 yamaguch pa = &sc->sc_pa;
1762 1.1 yamaguch memset(counts, 0, sizeof(counts));
1763 1.1 yamaguch counts[PCI_INTR_TYPE_MSIX] = num;
1764 1.1 yamaguch
1765 1.1 yamaguch error = pci_intr_alloc(pa, &sc->sc_ihp, counts, PCI_INTR_TYPE_MSIX);
1766 1.1 yamaguch if (error != 0) {
1767 1.1 yamaguch IAVF_LOG(sc, LOG_WARNING, "couldn't allocate interrupts\n");
1768 1.1 yamaguch return -1;
1769 1.1 yamaguch }
1770 1.1 yamaguch
1771 1.1 yamaguch KASSERT(pci_intr_type(pa->pa_pc, sc->sc_ihp[0]) == PCI_INTR_TYPE_MSIX);
1772 1.1 yamaguch
1773 1.1 yamaguch if (counts[PCI_INTR_TYPE_MSIX] < 1) {
1774 1.1 yamaguch IAVF_LOG(sc, LOG_ERR, "couldn't allocate interrupts\n");
1775 1.1 yamaguch } else if (counts[PCI_INTR_TYPE_MSIX] != (int)num) {
1776 1.1 yamaguch IAVF_LOG(sc, LOG_DEBUG,
1777 1.1 yamaguch "request %u intruppts, but allocate %d interrupts\n",
1778 1.1 yamaguch num, counts[PCI_INTR_TYPE_MSIX]);
1779 1.1 yamaguch num = counts[PCI_INTR_TYPE_MSIX];
1780 1.1 yamaguch }
1781 1.1 yamaguch
1782 1.1 yamaguch sc->sc_ihs = kmem_alloc(sizeof(sc->sc_ihs[0]) * num, KM_NOSLEEP);
1783 1.1 yamaguch if (sc->sc_ihs == NULL) {
1784 1.1 yamaguch IAVF_LOG(sc, LOG_ERR,
1785 1.1 yamaguch "couldn't allocate memory for interrupts\n");
1786 1.1 yamaguch goto fail;
1787 1.1 yamaguch }
1788 1.1 yamaguch
1789 1.1 yamaguch /* vector #0 is Misc interrupt */
1790 1.1 yamaguch vector = 0;
1791 1.1 yamaguch pci_intr_setattr(pa->pa_pc, &sc->sc_ihp[vector], PCI_INTR_MPSAFE, true);
1792 1.1 yamaguch intrstr = pci_intr_string(pa->pa_pc, sc->sc_ihp[vector],
1793 1.1 yamaguch intrbuf, sizeof(intrbuf));
1794 1.1 yamaguch snprintf(xnamebuf, sizeof(xnamebuf), "%s-Misc",
1795 1.1 yamaguch device_xname(sc->sc_dev));
1796 1.1 yamaguch
1797 1.1 yamaguch sc->sc_ihs[vector] = pci_intr_establish_xname(pa->pa_pc,
1798 1.1 yamaguch sc->sc_ihp[vector], IPL_NET, iavf_intr, sc, xnamebuf);
1799 1.1 yamaguch if (sc->sc_ihs[vector] == NULL) {
1800 1.1 yamaguch IAVF_LOG(sc, LOG_WARNING,
1801 1.1 yamaguch "unable to establish interrupt at %s", intrstr);
1802 1.1 yamaguch goto fail;
1803 1.1 yamaguch }
1804 1.1 yamaguch
1805 1.1 yamaguch kcpuset_create(&affinity, false);
1806 1.1 yamaguch affinity_to = ((int)num <= ncpu) ? 1 : 0;
1807 1.1 yamaguch qid = 0;
1808 1.1 yamaguch for (vector = 1; vector < num; vector++) {
1809 1.1 yamaguch pci_intr_setattr(pa->pa_pc, &sc->sc_ihp[vector],
1810 1.1 yamaguch PCI_INTR_MPSAFE, true);
1811 1.1 yamaguch intrstr = pci_intr_string(pa->pa_pc, sc->sc_ihp[vector],
1812 1.1 yamaguch intrbuf, sizeof(intrbuf));
1813 1.1 yamaguch snprintf(xnamebuf, sizeof(xnamebuf), "%s-TXRX%u",
1814 1.1 yamaguch device_xname(sc->sc_dev), qid);
1815 1.1 yamaguch
1816 1.1 yamaguch sc->sc_ihs[vector] = pci_intr_establish_xname(pa->pa_pc,
1817 1.1 yamaguch sc->sc_ihp[vector], IPL_NET, iavf_queue_intr,
1818 1.1 yamaguch (void *)&sc->sc_qps[qid], xnamebuf);
1819 1.1 yamaguch if (sc->sc_ihs[vector] == NULL) {
1820 1.1 yamaguch IAVF_LOG(sc, LOG_WARNING,
1821 1.1 yamaguch "unable to establish interrupt at %s\n", intrstr);
1822 1.1 yamaguch goto fail;
1823 1.1 yamaguch }
1824 1.1 yamaguch
1825 1.1 yamaguch kcpuset_zero(affinity);
1826 1.1 yamaguch kcpuset_set(affinity, affinity_to);
1827 1.1 yamaguch error = interrupt_distribute(sc->sc_ihs[vector],
1828 1.1 yamaguch affinity, NULL);
1829 1.1 yamaguch
1830 1.1 yamaguch if (error == 0) {
1831 1.1 yamaguch IAVF_LOG(sc, LOG_INFO,
1832 1.1 yamaguch "for TXRX%d interrupt at %s, affinity to %d\n",
1833 1.1 yamaguch qid, intrstr, affinity_to);
1834 1.1 yamaguch } else {
1835 1.1 yamaguch IAVF_LOG(sc, LOG_INFO,
1836 1.1 yamaguch "for TXRX%d interrupt at %s\n",
1837 1.1 yamaguch qid, intrstr);
1838 1.1 yamaguch }
1839 1.1 yamaguch
1840 1.1 yamaguch qid++;
1841 1.1 yamaguch affinity_to = (affinity_to + 1) % ncpu;
1842 1.1 yamaguch }
1843 1.1 yamaguch
1844 1.1 yamaguch kcpuset_destroy(affinity);
1845 1.1 yamaguch
1846 1.1 yamaguch sc->sc_nintrs = num;
1847 1.1 yamaguch return 0;
1848 1.1 yamaguch
1849 1.1 yamaguch fail:
1850 1.1 yamaguch if (affinity != NULL)
1851 1.1 yamaguch kcpuset_destroy(affinity);
1852 1.1 yamaguch for (vector = 0; vector < num; vector++) {
1853 1.1 yamaguch if (sc->sc_ihs[vector] == NULL)
1854 1.1 yamaguch continue;
1855 1.1 yamaguch pci_intr_disestablish(pa->pa_pc, sc->sc_ihs[vector]);
1856 1.1 yamaguch }
1857 1.1 yamaguch kmem_free(sc->sc_ihs, sizeof(sc->sc_ihs[0]) * num);
1858 1.1 yamaguch pci_intr_release(pa->pa_pc, sc->sc_ihp, num);
1859 1.1 yamaguch
1860 1.1 yamaguch return -1;
1861 1.1 yamaguch }
1862 1.1 yamaguch
1863 1.1 yamaguch static void
1864 1.1 yamaguch iavf_teardown_interrupts(struct iavf_softc *sc)
1865 1.1 yamaguch {
1866 1.1 yamaguch struct pci_attach_args *pa;
1867 1.1 yamaguch unsigned int i;
1868 1.1 yamaguch
1869 1.1 yamaguch if (sc->sc_ihs == NULL)
1870 1.1 yamaguch return;
1871 1.1 yamaguch
1872 1.1 yamaguch pa = &sc->sc_pa;
1873 1.1 yamaguch
1874 1.1 yamaguch for (i = 0; i < sc->sc_nintrs; i++) {
1875 1.1 yamaguch pci_intr_disestablish(pa->pa_pc, sc->sc_ihs[i]);
1876 1.1 yamaguch }
1877 1.1 yamaguch
1878 1.1 yamaguch kmem_free(sc->sc_ihs, sizeof(sc->sc_ihs[0]) * sc->sc_nintrs);
1879 1.1 yamaguch sc->sc_ihs = NULL;
1880 1.1 yamaguch
1881 1.1 yamaguch pci_intr_release(pa->pa_pc, sc->sc_ihp, sc->sc_nintrs);
1882 1.1 yamaguch sc->sc_nintrs = 0;
1883 1.1 yamaguch }
1884 1.1 yamaguch
1885 1.1 yamaguch static int
1886 1.1 yamaguch iavf_setup_sysctls(struct iavf_softc *sc)
1887 1.1 yamaguch {
1888 1.1 yamaguch const char *devname;
1889 1.1 yamaguch struct sysctllog **log;
1890 1.1 yamaguch const struct sysctlnode *rnode, *rxnode, *txnode;
1891 1.1 yamaguch int error;
1892 1.1 yamaguch
1893 1.1 yamaguch log = &sc->sc_sysctllog;
1894 1.1 yamaguch devname = device_xname(sc->sc_dev);
1895 1.1 yamaguch
1896 1.1 yamaguch error = sysctl_createv(log, 0, NULL, &rnode,
1897 1.1 yamaguch 0, CTLTYPE_NODE, devname,
1898 1.1 yamaguch SYSCTL_DESCR("iavf information and settings"),
1899 1.1 yamaguch NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
1900 1.1 yamaguch if (error)
1901 1.1 yamaguch goto out;
1902 1.1 yamaguch
1903 1.1 yamaguch error = sysctl_createv(log, 0, &rnode, NULL,
1904 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_BOOL, "txrx_workqueue",
1905 1.1 yamaguch SYSCTL_DESCR("Use workqueue for packet processing"),
1906 1.1 yamaguch NULL, 0, &sc->sc_txrx_workqueue, 0, CTL_CREATE, CTL_EOL);
1907 1.1 yamaguch if (error)
1908 1.1 yamaguch goto out;
1909 1.1 yamaguch
1910 1.1 yamaguch error = sysctl_createv(log, 0, &rnode, NULL,
1911 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_INT, "debug_level",
1912 1.1 yamaguch SYSCTL_DESCR("Debug level"),
1913 1.1 yamaguch NULL, 0, &sc->sc_debuglevel, 0, CTL_CREATE, CTL_EOL);
1914 1.1 yamaguch if (error)
1915 1.1 yamaguch goto out;
1916 1.1 yamaguch
1917 1.1 yamaguch error = sysctl_createv(log, 0, &rnode, &rxnode,
1918 1.1 yamaguch 0, CTLTYPE_NODE, "rx",
1919 1.1 yamaguch SYSCTL_DESCR("iavf information and settings for Rx"),
1920 1.1 yamaguch NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
1921 1.1 yamaguch if (error)
1922 1.1 yamaguch goto out;
1923 1.1 yamaguch
1924 1.1 yamaguch error = sysctl_createv(log, 0, &rxnode, NULL,
1925 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_INT, "itr",
1926 1.1 yamaguch SYSCTL_DESCR("Interrupt Throttling"),
1927 1.1 yamaguch iavf_sysctl_itr_handler, 0,
1928 1.1 yamaguch (void *)sc, 0, CTL_CREATE, CTL_EOL);
1929 1.1 yamaguch if (error)
1930 1.1 yamaguch goto out;
1931 1.1 yamaguch
1932 1.1 yamaguch error = sysctl_createv(log, 0, &rxnode, NULL,
1933 1.1 yamaguch CTLFLAG_READONLY, CTLTYPE_INT, "descriptor_num",
1934 1.1 yamaguch SYSCTL_DESCR("descriptor size"),
1935 1.1 yamaguch NULL, 0, &sc->sc_rx_ring_ndescs, 0, CTL_CREATE, CTL_EOL);
1936 1.1 yamaguch if (error)
1937 1.1 yamaguch goto out;
1938 1.1 yamaguch
1939 1.1 yamaguch error = sysctl_createv(log, 0, &rxnode, NULL,
1940 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
1941 1.1 yamaguch SYSCTL_DESCR("max number of Rx packets"
1942 1.1 yamaguch " to process for interrupt processing"),
1943 1.1 yamaguch NULL, 0, &sc->sc_rx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
1944 1.1 yamaguch if (error)
1945 1.1 yamaguch goto out;
1946 1.1 yamaguch
1947 1.1 yamaguch error = sysctl_createv(log, 0, &rxnode, NULL,
1948 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
1949 1.1 yamaguch SYSCTL_DESCR("max number of Rx packets"
1950 1.1 yamaguch " to process for deferred processing"),
1951 1.1 yamaguch NULL, 0, &sc->sc_rx_process_limit, 0, CTL_CREATE, CTL_EOL);
1952 1.1 yamaguch if (error)
1953 1.1 yamaguch goto out;
1954 1.1 yamaguch
1955 1.1 yamaguch error = sysctl_createv(log, 0, &rnode, &txnode,
1956 1.1 yamaguch 0, CTLTYPE_NODE, "tx",
1957 1.1 yamaguch SYSCTL_DESCR("iavf information and settings for Tx"),
1958 1.1 yamaguch NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
1959 1.1 yamaguch if (error)
1960 1.1 yamaguch goto out;
1961 1.1 yamaguch
1962 1.1 yamaguch error = sysctl_createv(log, 0, &txnode, NULL,
1963 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_INT, "itr",
1964 1.1 yamaguch SYSCTL_DESCR("Interrupt Throttling"),
1965 1.1 yamaguch iavf_sysctl_itr_handler, 0,
1966 1.1 yamaguch (void *)sc, 0, CTL_CREATE, CTL_EOL);
1967 1.1 yamaguch if (error)
1968 1.1 yamaguch goto out;
1969 1.1 yamaguch
1970 1.1 yamaguch error = sysctl_createv(log, 0, &txnode, NULL,
1971 1.1 yamaguch CTLFLAG_READONLY, CTLTYPE_INT, "descriptor_num",
1972 1.1 yamaguch SYSCTL_DESCR("the number of Tx descriptors"),
1973 1.1 yamaguch NULL, 0, &sc->sc_tx_ring_ndescs, 0, CTL_CREATE, CTL_EOL);
1974 1.1 yamaguch if (error)
1975 1.1 yamaguch goto out;
1976 1.1 yamaguch
1977 1.1 yamaguch error = sysctl_createv(log, 0, &txnode, NULL,
1978 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
1979 1.1 yamaguch SYSCTL_DESCR("max number of Tx packets"
1980 1.1 yamaguch " to process for interrupt processing"),
1981 1.1 yamaguch NULL, 0, &sc->sc_tx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
1982 1.1 yamaguch if (error)
1983 1.1 yamaguch goto out;
1984 1.1 yamaguch
1985 1.1 yamaguch error = sysctl_createv(log, 0, &txnode, NULL,
1986 1.1 yamaguch CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
1987 1.1 yamaguch SYSCTL_DESCR("max number of Tx packets"
1988 1.1 yamaguch " to process for deferred processing"),
1989 1.1 yamaguch NULL, 0, &sc->sc_tx_process_limit, 0, CTL_CREATE, CTL_EOL);
1990 1.1 yamaguch if (error)
1991 1.1 yamaguch goto out;
1992 1.1 yamaguch out:
1993 1.1 yamaguch return error;
1994 1.1 yamaguch }
1995 1.1 yamaguch
1996 1.1 yamaguch static void
1997 1.1 yamaguch iavf_teardown_sysctls(struct iavf_softc *sc)
1998 1.1 yamaguch {
1999 1.1 yamaguch
2000 1.1 yamaguch sysctl_teardown(&sc->sc_sysctllog);
2001 1.1 yamaguch }
2002 1.1 yamaguch
2003 1.1 yamaguch static int
2004 1.1 yamaguch iavf_setup_stats(struct iavf_softc *sc)
2005 1.1 yamaguch {
2006 1.1 yamaguch struct iavf_stat_counters *isc;
2007 1.1 yamaguch const char *dn;
2008 1.1 yamaguch
2009 1.1 yamaguch dn = device_xname(sc->sc_dev);
2010 1.1 yamaguch isc = &sc->sc_stat_counters;
2011 1.1 yamaguch
2012 1.1 yamaguch iavf_evcnt_attach(&isc->isc_rx_bytes, dn, "Rx bytes");
2013 1.1 yamaguch iavf_evcnt_attach(&isc->isc_rx_unicast, dn, "Rx unicast");
2014 1.1 yamaguch iavf_evcnt_attach(&isc->isc_rx_multicast, dn, "Rx multicast");
2015 1.1 yamaguch iavf_evcnt_attach(&isc->isc_rx_broadcast, dn, "Rx broadcast");
2016 1.1 yamaguch iavf_evcnt_attach(&isc->isc_rx_discards, dn, "Rx discards");
2017 1.1 yamaguch iavf_evcnt_attach(&isc->isc_rx_unknown_protocol,
2018 1.1 yamaguch dn, "Rx unknown protocol");
2019 1.1 yamaguch
2020 1.1 yamaguch iavf_evcnt_attach(&isc->isc_tx_bytes, dn, "Tx bytes");
2021 1.1 yamaguch iavf_evcnt_attach(&isc->isc_tx_unicast, dn, "Tx unicast");
2022 1.1 yamaguch iavf_evcnt_attach(&isc->isc_tx_multicast, dn, "Tx multicast");
2023 1.1 yamaguch iavf_evcnt_attach(&isc->isc_tx_broadcast, dn, "Tx broadcast");
2024 1.1 yamaguch iavf_evcnt_attach(&isc->isc_tx_discards, dn, "Tx discards");
2025 1.1 yamaguch iavf_evcnt_attach(&isc->isc_tx_errors, dn, "Tx errors");
2026 1.1 yamaguch
2027 1.1 yamaguch return 0;
2028 1.1 yamaguch }
2029 1.1 yamaguch
2030 1.1 yamaguch static void
2031 1.1 yamaguch iavf_teardown_stats(struct iavf_softc *sc)
2032 1.1 yamaguch {
2033 1.1 yamaguch struct iavf_stat_counters *isc;
2034 1.1 yamaguch
2035 1.1 yamaguch isc = &sc->sc_stat_counters;
2036 1.1 yamaguch
2037 1.1 yamaguch evcnt_detach(&isc->isc_rx_bytes);
2038 1.1 yamaguch evcnt_detach(&isc->isc_rx_unicast);
2039 1.1 yamaguch evcnt_detach(&isc->isc_rx_multicast);
2040 1.1 yamaguch evcnt_detach(&isc->isc_rx_broadcast);
2041 1.1 yamaguch evcnt_detach(&isc->isc_rx_discards);
2042 1.1 yamaguch evcnt_detach(&isc->isc_rx_unknown_protocol);
2043 1.1 yamaguch
2044 1.1 yamaguch evcnt_detach(&isc->isc_tx_bytes);
2045 1.1 yamaguch evcnt_detach(&isc->isc_tx_unicast);
2046 1.1 yamaguch evcnt_detach(&isc->isc_tx_multicast);
2047 1.1 yamaguch evcnt_detach(&isc->isc_tx_broadcast);
2048 1.1 yamaguch evcnt_detach(&isc->isc_tx_discards);
2049 1.1 yamaguch evcnt_detach(&isc->isc_tx_errors);
2050 1.1 yamaguch
2051 1.1 yamaguch }
2052 1.1 yamaguch
2053 1.1 yamaguch static int
2054 1.1 yamaguch iavf_init_admin_queue(struct iavf_softc *sc)
2055 1.1 yamaguch {
2056 1.1 yamaguch uint32_t reg;
2057 1.1 yamaguch
2058 1.1 yamaguch sc->sc_atq_cons = 0;
2059 1.1 yamaguch sc->sc_atq_prod = 0;
2060 1.1 yamaguch
2061 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
2062 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq),
2063 1.1 yamaguch BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2064 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
2065 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_arq),
2066 1.1 yamaguch BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2067 1.1 yamaguch
2068 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_head, 0);
2069 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_head, 0);
2070 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_tail, 0);
2071 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_tail, 0);
2072 1.1 yamaguch
2073 1.1 yamaguch iavf_barrier(sc, 0, sc->sc_mems, BUS_SPACE_BARRIER_WRITE);
2074 1.1 yamaguch
2075 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_bal,
2076 1.1 yamaguch ixl_dmamem_lo(&sc->sc_atq));
2077 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_bah,
2078 1.1 yamaguch ixl_dmamem_hi(&sc->sc_atq));
2079 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_len,
2080 1.1 yamaguch sc->sc_aq_regs->atq_len_enable | IAVF_AQ_NUM);
2081 1.1 yamaguch
2082 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_bal,
2083 1.1 yamaguch ixl_dmamem_lo(&sc->sc_arq));
2084 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_bah,
2085 1.1 yamaguch ixl_dmamem_hi(&sc->sc_arq));
2086 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_len,
2087 1.1 yamaguch sc->sc_aq_regs->arq_len_enable | IAVF_AQ_NUM);
2088 1.1 yamaguch
2089 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_tail, sc->sc_arq_prod);
2090 1.1 yamaguch
2091 1.1 yamaguch reg = iavf_rd(sc, sc->sc_aq_regs->atq_bal);
2092 1.1 yamaguch if (reg != ixl_dmamem_lo(&sc->sc_atq))
2093 1.1 yamaguch goto fail;
2094 1.1 yamaguch
2095 1.1 yamaguch reg = iavf_rd(sc, sc->sc_aq_regs->arq_bal);
2096 1.1 yamaguch if (reg != ixl_dmamem_lo(&sc->sc_arq))
2097 1.1 yamaguch goto fail;
2098 1.1 yamaguch
2099 1.1 yamaguch sc->sc_dead = false;
2100 1.1 yamaguch return 0;
2101 1.1 yamaguch
2102 1.1 yamaguch fail:
2103 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_len, 0);
2104 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_len, 0);
2105 1.1 yamaguch return -1;
2106 1.1 yamaguch }
2107 1.1 yamaguch
2108 1.1 yamaguch static void
2109 1.1 yamaguch iavf_cleanup_admin_queue(struct iavf_softc *sc)
2110 1.1 yamaguch {
2111 1.1 yamaguch struct ixl_aq_buf *aqb;
2112 1.1 yamaguch
2113 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_head, 0);
2114 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_head, 0);
2115 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_tail, 0);
2116 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_tail, 0);
2117 1.1 yamaguch
2118 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_bal, 0);
2119 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_bah, 0);
2120 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_len, 0);
2121 1.1 yamaguch
2122 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_bal, 0);
2123 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_bah, 0);
2124 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_len, 0);
2125 1.1 yamaguch iavf_flush(sc);
2126 1.1 yamaguch
2127 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
2128 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_arq),
2129 1.1 yamaguch BUS_DMASYNC_POSTREAD);
2130 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
2131 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq),
2132 1.1 yamaguch BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2133 1.1 yamaguch
2134 1.1 yamaguch sc->sc_atq_cons = 0;
2135 1.1 yamaguch sc->sc_atq_prod = 0;
2136 1.1 yamaguch sc->sc_arq_cons = 0;
2137 1.1 yamaguch sc->sc_arq_prod = 0;
2138 1.1 yamaguch
2139 1.1 yamaguch memset(IXL_DMA_KVA(&sc->sc_arq), 0, IXL_DMA_LEN(&sc->sc_arq));
2140 1.1 yamaguch memset(IXL_DMA_KVA(&sc->sc_atq), 0, IXL_DMA_LEN(&sc->sc_atq));
2141 1.1 yamaguch
2142 1.1 yamaguch while ((aqb = iavf_aqb_get_locked(&sc->sc_arq_live)) != NULL) {
2143 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0, aqb->aqb_size,
2144 1.1 yamaguch BUS_DMASYNC_POSTREAD);
2145 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_arq_idle, aqb);
2146 1.1 yamaguch }
2147 1.1 yamaguch
2148 1.1 yamaguch while ((aqb = iavf_aqb_get_locked(&sc->sc_atq_live)) != NULL) {
2149 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0, aqb->aqb_size,
2150 1.1 yamaguch BUS_DMASYNC_POSTREAD);
2151 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
2152 1.1 yamaguch }
2153 1.1 yamaguch }
2154 1.1 yamaguch
2155 1.1 yamaguch static unsigned int
2156 1.1 yamaguch iavf_calc_msix_count(struct iavf_softc *sc)
2157 1.1 yamaguch {
2158 1.1 yamaguch struct pci_attach_args *pa;
2159 1.1 yamaguch int count;
2160 1.1 yamaguch
2161 1.1 yamaguch pa = &sc->sc_pa;
2162 1.1 yamaguch count = pci_msix_count(pa->pa_pc, pa->pa_tag);
2163 1.1 yamaguch if (count < 0) {
2164 1.1 yamaguch IAVF_LOG(sc, LOG_DEBUG,"MSIX config error\n");
2165 1.1 yamaguch count = 0;
2166 1.1 yamaguch }
2167 1.1 yamaguch
2168 1.1 yamaguch return MIN(sc->sc_max_vectors, (unsigned int)count);
2169 1.1 yamaguch }
2170 1.1 yamaguch
2171 1.1 yamaguch static unsigned int
2172 1.1 yamaguch iavf_calc_queue_pair_size(struct iavf_softc *sc)
2173 1.1 yamaguch {
2174 1.1 yamaguch unsigned int nqp, nvec;
2175 1.1 yamaguch
2176 1.1 yamaguch nvec = iavf_calc_msix_count(sc);
2177 1.1 yamaguch if (sc->sc_max_vectors > 1) {
2178 1.1 yamaguch /* decrease the number of misc interrupt */
2179 1.1 yamaguch nvec -= 1;
2180 1.1 yamaguch }
2181 1.1 yamaguch
2182 1.1 yamaguch nqp = ncpu;
2183 1.1 yamaguch nqp = MIN(nqp, sc->sc_nqps_vsi);
2184 1.1 yamaguch nqp = MIN(nqp, nvec);
2185 1.1 yamaguch nqp = MIN(nqp, (unsigned int)iavf_params.max_qps);
2186 1.1 yamaguch
2187 1.1 yamaguch return nqp;
2188 1.1 yamaguch }
2189 1.1 yamaguch
2190 1.1 yamaguch static struct iavf_tx_ring *
2191 1.1 yamaguch iavf_txr_alloc(struct iavf_softc *sc, unsigned int qid)
2192 1.1 yamaguch {
2193 1.1 yamaguch struct iavf_tx_ring *txr;
2194 1.1 yamaguch struct iavf_tx_map *maps;
2195 1.1 yamaguch unsigned int i;
2196 1.1 yamaguch int error;
2197 1.1 yamaguch
2198 1.1 yamaguch txr = kmem_zalloc(sizeof(*txr), KM_NOSLEEP);
2199 1.1 yamaguch if (txr == NULL)
2200 1.1 yamaguch return NULL;
2201 1.1 yamaguch
2202 1.1 yamaguch maps = kmem_zalloc(sizeof(maps[0]) * sc->sc_tx_ring_ndescs,
2203 1.1 yamaguch KM_NOSLEEP);
2204 1.1 yamaguch if (maps == NULL)
2205 1.1 yamaguch goto free_txr;
2206 1.1 yamaguch
2207 1.1 yamaguch if (iavf_dmamem_alloc(sc->sc_dmat, &txr->txr_mem,
2208 1.1 yamaguch sizeof(struct ixl_tx_desc) * sc->sc_tx_ring_ndescs,
2209 1.1 yamaguch IAVF_TX_QUEUE_ALIGN) != 0) {
2210 1.1 yamaguch goto free_maps;
2211 1.1 yamaguch }
2212 1.1 yamaguch
2213 1.1 yamaguch for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
2214 1.1 yamaguch error = bus_dmamap_create(sc->sc_dmat, IAVF_TX_PKT_MAXSIZE,
2215 1.1 yamaguch IAVF_TX_PKT_DESCS, IAVF_TX_PKT_MAXSIZE, 0,
2216 1.1 yamaguch BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &maps[i].txm_map);
2217 1.1 yamaguch if (error)
2218 1.1 yamaguch goto destroy_maps;
2219 1.1 yamaguch }
2220 1.1 yamaguch
2221 1.1 yamaguch txr->txr_intrq = pcq_create(sc->sc_tx_ring_ndescs, KM_NOSLEEP);
2222 1.1 yamaguch if (txr->txr_intrq == NULL)
2223 1.1 yamaguch goto destroy_maps;
2224 1.1 yamaguch
2225 1.1 yamaguch txr->txr_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
2226 1.1 yamaguch iavf_deferred_transmit, txr);
2227 1.1 yamaguch if (txr->txr_si == NULL)
2228 1.1 yamaguch goto destroy_pcq;
2229 1.1 yamaguch
2230 1.1 yamaguch snprintf(txr->txr_name, sizeof(txr->txr_name), "%s-tx%d",
2231 1.1 yamaguch device_xname(sc->sc_dev), qid);
2232 1.1 yamaguch
2233 1.1 yamaguch iavf_evcnt_attach(&txr->txr_defragged,
2234 1.1 yamaguch txr->txr_name, "m_defrag successed");
2235 1.1 yamaguch iavf_evcnt_attach(&txr->txr_defrag_failed,
2236 1.1 yamaguch txr->txr_name, "m_defrag failed");
2237 1.1 yamaguch iavf_evcnt_attach(&txr->txr_pcqdrop,
2238 1.1 yamaguch txr->txr_name, "Dropped in pcq");
2239 1.1 yamaguch iavf_evcnt_attach(&txr->txr_transmitdef,
2240 1.1 yamaguch txr->txr_name, "Deferred transmit");
2241 1.1 yamaguch iavf_evcnt_attach(&txr->txr_watchdogto,
2242 1.1 yamaguch txr->txr_name, "Watchdog timedout on queue");
2243 1.1 yamaguch iavf_evcnt_attach(&txr->txr_defer,
2244 1.1 yamaguch txr->txr_name, "Handled queue in softint/workqueue");
2245 1.1 yamaguch
2246 1.1 yamaguch evcnt_attach_dynamic(&txr->txr_intr, EVCNT_TYPE_INTR, NULL,
2247 1.1 yamaguch txr->txr_name, "Interrupt on queue");
2248 1.1 yamaguch
2249 1.1 yamaguch txr->txr_qid = qid;
2250 1.1 yamaguch txr->txr_sc = sc;
2251 1.1 yamaguch txr->txr_maps = maps;
2252 1.1 yamaguch txr->txr_prod = txr->txr_cons = 0;
2253 1.1 yamaguch txr->txr_tail = I40E_QTX_TAIL1(qid);
2254 1.1 yamaguch mutex_init(&txr->txr_lock, MUTEX_DEFAULT, IPL_NET);
2255 1.1 yamaguch
2256 1.1 yamaguch return txr;
2257 1.1 yamaguch destroy_pcq:
2258 1.1 yamaguch pcq_destroy(txr->txr_intrq);
2259 1.1 yamaguch destroy_maps:
2260 1.1 yamaguch for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
2261 1.1 yamaguch if (maps[i].txm_map == NULL)
2262 1.1 yamaguch continue;
2263 1.1 yamaguch bus_dmamap_destroy(sc->sc_dmat, maps[i].txm_map);
2264 1.1 yamaguch }
2265 1.1 yamaguch
2266 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &txr->txr_mem);
2267 1.1 yamaguch free_maps:
2268 1.1 yamaguch kmem_free(maps, sizeof(maps[0]) * sc->sc_tx_ring_ndescs);
2269 1.1 yamaguch free_txr:
2270 1.1 yamaguch kmem_free(txr, sizeof(*txr));
2271 1.1 yamaguch return NULL;
2272 1.1 yamaguch }
2273 1.1 yamaguch
2274 1.1 yamaguch static void
2275 1.1 yamaguch iavf_txr_free(struct iavf_softc *sc, struct iavf_tx_ring *txr)
2276 1.1 yamaguch {
2277 1.1 yamaguch struct iavf_tx_map *maps;
2278 1.1 yamaguch unsigned int i;
2279 1.1 yamaguch
2280 1.1 yamaguch maps = txr->txr_maps;
2281 1.1 yamaguch if (maps != NULL) {
2282 1.1 yamaguch for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
2283 1.1 yamaguch if (maps[i].txm_map == NULL)
2284 1.1 yamaguch continue;
2285 1.1 yamaguch bus_dmamap_destroy(sc->sc_dmat, maps[i].txm_map);
2286 1.1 yamaguch }
2287 1.1 yamaguch kmem_free(txr->txr_maps,
2288 1.1 yamaguch sizeof(maps[0]) * sc->sc_tx_ring_ndescs);
2289 1.1 yamaguch txr->txr_maps = NULL;
2290 1.1 yamaguch }
2291 1.1 yamaguch
2292 1.1 yamaguch evcnt_detach(&txr->txr_defragged);
2293 1.1 yamaguch evcnt_detach(&txr->txr_defrag_failed);
2294 1.1 yamaguch evcnt_detach(&txr->txr_pcqdrop);
2295 1.1 yamaguch evcnt_detach(&txr->txr_transmitdef);
2296 1.1 yamaguch evcnt_detach(&txr->txr_watchdogto);
2297 1.1 yamaguch evcnt_detach(&txr->txr_defer);
2298 1.1 yamaguch evcnt_detach(&txr->txr_intr);
2299 1.1 yamaguch
2300 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &txr->txr_mem);
2301 1.1 yamaguch softint_disestablish(txr->txr_si);
2302 1.1 yamaguch pcq_destroy(txr->txr_intrq);
2303 1.1 yamaguch mutex_destroy(&txr->txr_lock);
2304 1.1 yamaguch kmem_free(txr, sizeof(*txr));
2305 1.1 yamaguch }
2306 1.1 yamaguch
2307 1.1 yamaguch static struct iavf_rx_ring *
2308 1.1 yamaguch iavf_rxr_alloc(struct iavf_softc *sc, unsigned int qid)
2309 1.1 yamaguch {
2310 1.1 yamaguch struct iavf_rx_ring *rxr;
2311 1.1 yamaguch struct iavf_rx_map *maps;
2312 1.1 yamaguch unsigned int i;
2313 1.1 yamaguch int error;
2314 1.1 yamaguch
2315 1.1 yamaguch rxr = kmem_zalloc(sizeof(*rxr), KM_NOSLEEP);
2316 1.1 yamaguch if (rxr == NULL)
2317 1.1 yamaguch return NULL;
2318 1.1 yamaguch
2319 1.1 yamaguch maps = kmem_zalloc(sizeof(maps[0]) * sc->sc_rx_ring_ndescs,
2320 1.1 yamaguch KM_NOSLEEP);
2321 1.1 yamaguch if (maps == NULL)
2322 1.1 yamaguch goto free_rxr;
2323 1.1 yamaguch
2324 1.1 yamaguch if (iavf_dmamem_alloc(sc->sc_dmat, &rxr->rxr_mem,
2325 1.1 yamaguch sizeof(struct ixl_rx_rd_desc_32) * sc->sc_rx_ring_ndescs,
2326 1.1 yamaguch IAVF_RX_QUEUE_ALIGN) != 0)
2327 1.1 yamaguch goto free_maps;
2328 1.1 yamaguch
2329 1.1 yamaguch for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
2330 1.1 yamaguch error = bus_dmamap_create(sc->sc_dmat, IAVF_MCLBYTES,
2331 1.1 yamaguch 1, IAVF_MCLBYTES, 0,
2332 1.1 yamaguch BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &maps[i].rxm_map);
2333 1.1 yamaguch if (error)
2334 1.1 yamaguch goto destroy_maps;
2335 1.1 yamaguch }
2336 1.1 yamaguch
2337 1.1 yamaguch snprintf(rxr->rxr_name, sizeof(rxr->rxr_name), "%s-rx%d",
2338 1.1 yamaguch device_xname(sc->sc_dev), qid);
2339 1.1 yamaguch
2340 1.1 yamaguch iavf_evcnt_attach(&rxr->rxr_mgethdr_failed,
2341 1.1 yamaguch rxr->rxr_name, "MGETHDR failed");
2342 1.1 yamaguch iavf_evcnt_attach(&rxr->rxr_mgetcl_failed,
2343 1.1 yamaguch rxr->rxr_name, "MCLGET failed");
2344 1.1 yamaguch iavf_evcnt_attach(&rxr->rxr_mbuf_load_failed,
2345 1.1 yamaguch rxr->rxr_name, "bus_dmamap_load_mbuf failed");
2346 1.1 yamaguch iavf_evcnt_attach(&rxr->rxr_defer,
2347 1.1 yamaguch rxr->rxr_name, "Handled queue in softint/workqueue");
2348 1.1 yamaguch
2349 1.1 yamaguch evcnt_attach_dynamic(&rxr->rxr_intr, EVCNT_TYPE_INTR, NULL,
2350 1.1 yamaguch rxr->rxr_name, "Interrupt on queue");
2351 1.1 yamaguch
2352 1.1 yamaguch rxr->rxr_qid = qid;
2353 1.1 yamaguch rxr->rxr_sc = sc;
2354 1.1 yamaguch rxr->rxr_cons = rxr->rxr_prod = 0;
2355 1.1 yamaguch rxr->rxr_m_head = NULL;
2356 1.1 yamaguch rxr->rxr_m_tail = &rxr->rxr_m_head;
2357 1.1 yamaguch rxr->rxr_maps = maps;
2358 1.1 yamaguch rxr->rxr_tail = I40E_QRX_TAIL1(qid);
2359 1.1 yamaguch mutex_init(&rxr->rxr_lock, MUTEX_DEFAULT, IPL_NET);
2360 1.1 yamaguch
2361 1.1 yamaguch return rxr;
2362 1.1 yamaguch
2363 1.1 yamaguch destroy_maps:
2364 1.1 yamaguch for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
2365 1.1 yamaguch if (maps[i].rxm_map == NULL)
2366 1.1 yamaguch continue;
2367 1.1 yamaguch bus_dmamap_destroy(sc->sc_dmat, maps[i].rxm_map);
2368 1.1 yamaguch }
2369 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &rxr->rxr_mem);
2370 1.1 yamaguch free_maps:
2371 1.1 yamaguch kmem_free(maps, sizeof(maps[0]) * sc->sc_rx_ring_ndescs);
2372 1.1 yamaguch free_rxr:
2373 1.1 yamaguch kmem_free(rxr, sizeof(*rxr));
2374 1.1 yamaguch
2375 1.1 yamaguch return NULL;
2376 1.1 yamaguch }
2377 1.1 yamaguch
2378 1.1 yamaguch static void
2379 1.1 yamaguch iavf_rxr_free(struct iavf_softc *sc, struct iavf_rx_ring *rxr)
2380 1.1 yamaguch {
2381 1.1 yamaguch struct iavf_rx_map *maps;
2382 1.1 yamaguch unsigned int i;
2383 1.1 yamaguch
2384 1.1 yamaguch maps = rxr->rxr_maps;
2385 1.1 yamaguch if (maps != NULL) {
2386 1.1 yamaguch for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
2387 1.1 yamaguch if (maps[i].rxm_map == NULL)
2388 1.1 yamaguch continue;
2389 1.1 yamaguch bus_dmamap_destroy(sc->sc_dmat, maps[i].rxm_map);
2390 1.1 yamaguch }
2391 1.1 yamaguch kmem_free(maps, sizeof(maps[0]) * sc->sc_rx_ring_ndescs);
2392 1.1 yamaguch rxr->rxr_maps = NULL;
2393 1.1 yamaguch }
2394 1.1 yamaguch
2395 1.1 yamaguch evcnt_detach(&rxr->rxr_mgethdr_failed);
2396 1.1 yamaguch evcnt_detach(&rxr->rxr_mgetcl_failed);
2397 1.1 yamaguch evcnt_detach(&rxr->rxr_mbuf_load_failed);
2398 1.1 yamaguch evcnt_detach(&rxr->rxr_defer);
2399 1.1 yamaguch evcnt_detach(&rxr->rxr_intr);
2400 1.1 yamaguch
2401 1.1 yamaguch iavf_dmamem_free(sc->sc_dmat, &rxr->rxr_mem);
2402 1.1 yamaguch mutex_destroy(&rxr->rxr_lock);
2403 1.1 yamaguch kmem_free(rxr, sizeof(*rxr));
2404 1.1 yamaguch }
2405 1.1 yamaguch
2406 1.1 yamaguch static int
2407 1.1 yamaguch iavf_queue_pairs_alloc(struct iavf_softc *sc)
2408 1.1 yamaguch {
2409 1.1 yamaguch struct iavf_queue_pair *qp;
2410 1.1 yamaguch unsigned int i, num;
2411 1.1 yamaguch
2412 1.1 yamaguch num = iavf_calc_queue_pair_size(sc);
2413 1.1 yamaguch if (num <= 0) {
2414 1.1 yamaguch return -1;
2415 1.1 yamaguch }
2416 1.1 yamaguch
2417 1.1 yamaguch sc->sc_qps = kmem_zalloc(sizeof(sc->sc_qps[0]) * num, KM_NOSLEEP);
2418 1.1 yamaguch if (sc->sc_qps == NULL) {
2419 1.1 yamaguch return -1;
2420 1.1 yamaguch }
2421 1.1 yamaguch
2422 1.1 yamaguch for (i = 0; i < num; i++) {
2423 1.1 yamaguch qp = &sc->sc_qps[i];
2424 1.1 yamaguch
2425 1.1 yamaguch qp->qp_rxr = iavf_rxr_alloc(sc, i);
2426 1.1 yamaguch qp->qp_txr = iavf_txr_alloc(sc, i);
2427 1.1 yamaguch
2428 1.1 yamaguch if (qp->qp_rxr == NULL || qp->qp_txr == NULL)
2429 1.1 yamaguch goto free;
2430 1.1 yamaguch
2431 1.1 yamaguch qp->qp_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
2432 1.1 yamaguch iavf_handle_queue, qp);
2433 1.1 yamaguch if (qp->qp_si == NULL)
2434 1.1 yamaguch goto free;
2435 1.1 yamaguch }
2436 1.1 yamaguch
2437 1.1 yamaguch sc->sc_nqps_alloc = num;
2438 1.1 yamaguch return 0;
2439 1.1 yamaguch free:
2440 1.1 yamaguch for (i = 0; i < num; i++) {
2441 1.1 yamaguch qp = &sc->sc_qps[i];
2442 1.1 yamaguch
2443 1.1 yamaguch if (qp->qp_si != NULL)
2444 1.1 yamaguch softint_disestablish(qp->qp_si);
2445 1.1 yamaguch if (qp->qp_rxr != NULL)
2446 1.1 yamaguch iavf_rxr_free(sc, qp->qp_rxr);
2447 1.1 yamaguch if (qp->qp_txr != NULL)
2448 1.1 yamaguch iavf_txr_free(sc, qp->qp_txr);
2449 1.1 yamaguch }
2450 1.1 yamaguch
2451 1.1 yamaguch kmem_free(sc->sc_qps, sizeof(sc->sc_qps[0]) * num);
2452 1.1 yamaguch sc->sc_qps = NULL;
2453 1.1 yamaguch
2454 1.1 yamaguch return -1;
2455 1.1 yamaguch }
2456 1.1 yamaguch
2457 1.1 yamaguch static void
2458 1.1 yamaguch iavf_queue_pairs_free(struct iavf_softc *sc)
2459 1.1 yamaguch {
2460 1.1 yamaguch struct iavf_queue_pair *qp;
2461 1.1 yamaguch unsigned int i;
2462 1.1 yamaguch size_t sz;
2463 1.1 yamaguch
2464 1.1 yamaguch if (sc->sc_qps == NULL)
2465 1.1 yamaguch return;
2466 1.1 yamaguch
2467 1.1 yamaguch for (i = 0; i < sc->sc_nqps_alloc; i++) {
2468 1.1 yamaguch qp = &sc->sc_qps[i];
2469 1.1 yamaguch
2470 1.1 yamaguch if (qp->qp_si != NULL)
2471 1.1 yamaguch softint_disestablish(qp->qp_si);
2472 1.1 yamaguch if (qp->qp_rxr != NULL)
2473 1.1 yamaguch iavf_rxr_free(sc, qp->qp_rxr);
2474 1.1 yamaguch if (qp->qp_txr != NULL)
2475 1.1 yamaguch iavf_txr_free(sc, qp->qp_txr);
2476 1.1 yamaguch }
2477 1.1 yamaguch
2478 1.1 yamaguch sz = sizeof(sc->sc_qps[0]) * sc->sc_nqps_alloc;
2479 1.1 yamaguch kmem_free(sc->sc_qps, sz);
2480 1.1 yamaguch sc->sc_qps = NULL;
2481 1.1 yamaguch sc->sc_nqps_alloc = 0;
2482 1.1 yamaguch }
2483 1.1 yamaguch
2484 1.1 yamaguch static int
2485 1.1 yamaguch iavf_rxfill(struct iavf_softc *sc, struct iavf_rx_ring *rxr)
2486 1.1 yamaguch {
2487 1.1 yamaguch struct ixl_rx_rd_desc_32 *ring, *rxd;
2488 1.1 yamaguch struct iavf_rx_map *rxm;
2489 1.1 yamaguch bus_dmamap_t map;
2490 1.1 yamaguch struct mbuf *m;
2491 1.1 yamaguch unsigned int slots, prod, mask;
2492 1.1 yamaguch int error, post;
2493 1.1 yamaguch
2494 1.1 yamaguch slots = ixl_rxr_unrefreshed(rxr->rxr_prod, rxr->rxr_cons,
2495 1.1 yamaguch sc->sc_rx_ring_ndescs);
2496 1.1 yamaguch
2497 1.1 yamaguch if (slots == 0)
2498 1.1 yamaguch return 0;
2499 1.1 yamaguch
2500 1.1 yamaguch error = 0;
2501 1.1 yamaguch prod = rxr->rxr_prod;
2502 1.1 yamaguch
2503 1.1 yamaguch ring = IXL_DMA_KVA(&rxr->rxr_mem);
2504 1.1 yamaguch mask = sc->sc_rx_ring_ndescs - 1;
2505 1.1 yamaguch
2506 1.1 yamaguch do {
2507 1.1 yamaguch rxm = &rxr->rxr_maps[prod];
2508 1.1 yamaguch
2509 1.1 yamaguch MGETHDR(m, M_DONTWAIT, MT_DATA);
2510 1.1 yamaguch if (m == NULL) {
2511 1.1 yamaguch rxr->rxr_mgethdr_failed.ev_count++;
2512 1.1 yamaguch error = -1;
2513 1.1 yamaguch break;
2514 1.1 yamaguch }
2515 1.1 yamaguch
2516 1.1 yamaguch MCLGET(m, M_DONTWAIT);
2517 1.1 yamaguch if (!ISSET(m->m_flags, M_EXT)) {
2518 1.1 yamaguch rxr->rxr_mgetcl_failed.ev_count++;
2519 1.1 yamaguch error = -1;
2520 1.1 yamaguch m_freem(m);
2521 1.1 yamaguch break;
2522 1.1 yamaguch }
2523 1.1 yamaguch
2524 1.1 yamaguch m->m_len = m->m_pkthdr.len = MCLBYTES;
2525 1.1 yamaguch m_adj(m, ETHER_ALIGN);
2526 1.1 yamaguch
2527 1.1 yamaguch map = rxm->rxm_map;
2528 1.1 yamaguch
2529 1.1 yamaguch if (bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
2530 1.1 yamaguch BUS_DMA_READ|BUS_DMA_NOWAIT) != 0) {
2531 1.1 yamaguch rxr->rxr_mbuf_load_failed.ev_count++;
2532 1.1 yamaguch error = -1;
2533 1.1 yamaguch m_freem(m);
2534 1.1 yamaguch break;
2535 1.1 yamaguch }
2536 1.1 yamaguch
2537 1.1 yamaguch rxm->rxm_m = m;
2538 1.1 yamaguch
2539 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
2540 1.1 yamaguch BUS_DMASYNC_PREREAD);
2541 1.1 yamaguch
2542 1.1 yamaguch rxd = &ring[prod];
2543 1.1 yamaguch rxd->paddr = htole64(map->dm_segs[0].ds_addr);
2544 1.1 yamaguch rxd->haddr = htole64(0);
2545 1.1 yamaguch
2546 1.1 yamaguch prod++;
2547 1.1 yamaguch prod &= mask;
2548 1.1 yamaguch post = 1;
2549 1.1 yamaguch } while (--slots);
2550 1.1 yamaguch
2551 1.1 yamaguch if (post) {
2552 1.1 yamaguch rxr->rxr_prod = prod;
2553 1.1 yamaguch iavf_wr(sc, rxr->rxr_tail, prod);
2554 1.1 yamaguch }
2555 1.1 yamaguch
2556 1.1 yamaguch return error;
2557 1.1 yamaguch }
2558 1.1 yamaguch
2559 1.1 yamaguch static inline void
2560 1.1 yamaguch iavf_rx_csum(struct mbuf *m, uint64_t qword)
2561 1.1 yamaguch {
2562 1.1 yamaguch int flags_mask;
2563 1.1 yamaguch
2564 1.1 yamaguch if (!ISSET(qword, IXL_RX_DESC_L3L4P)) {
2565 1.1 yamaguch /* No L3 or L4 checksum was calculated */
2566 1.1 yamaguch return;
2567 1.1 yamaguch }
2568 1.1 yamaguch
2569 1.1 yamaguch switch (__SHIFTOUT(qword, IXL_RX_DESC_PTYPE_MASK)) {
2570 1.1 yamaguch case IXL_RX_DESC_PTYPE_IPV4FRAG:
2571 1.1 yamaguch case IXL_RX_DESC_PTYPE_IPV4:
2572 1.1 yamaguch case IXL_RX_DESC_PTYPE_SCTPV4:
2573 1.1 yamaguch case IXL_RX_DESC_PTYPE_ICMPV4:
2574 1.1 yamaguch flags_mask = M_CSUM_IPv4 | M_CSUM_IPv4_BAD;
2575 1.1 yamaguch break;
2576 1.1 yamaguch case IXL_RX_DESC_PTYPE_TCPV4:
2577 1.1 yamaguch flags_mask = M_CSUM_IPv4 | M_CSUM_IPv4_BAD;
2578 1.1 yamaguch flags_mask |= M_CSUM_TCPv4 | M_CSUM_TCP_UDP_BAD;
2579 1.1 yamaguch break;
2580 1.1 yamaguch case IXL_RX_DESC_PTYPE_UDPV4:
2581 1.1 yamaguch flags_mask = M_CSUM_IPv4 | M_CSUM_IPv4_BAD;
2582 1.1 yamaguch flags_mask |= M_CSUM_UDPv4 | M_CSUM_TCP_UDP_BAD;
2583 1.1 yamaguch break;
2584 1.1 yamaguch case IXL_RX_DESC_PTYPE_TCPV6:
2585 1.1 yamaguch flags_mask = M_CSUM_TCPv6 | M_CSUM_TCP_UDP_BAD;
2586 1.1 yamaguch break;
2587 1.1 yamaguch case IXL_RX_DESC_PTYPE_UDPV6:
2588 1.1 yamaguch flags_mask = M_CSUM_UDPv6 | M_CSUM_TCP_UDP_BAD;
2589 1.1 yamaguch break;
2590 1.1 yamaguch default:
2591 1.1 yamaguch flags_mask = 0;
2592 1.1 yamaguch }
2593 1.1 yamaguch
2594 1.1 yamaguch m->m_pkthdr.csum_flags |= (flags_mask & (M_CSUM_IPv4 |
2595 1.1 yamaguch M_CSUM_TCPv4 | M_CSUM_TCPv6 | M_CSUM_UDPv4 | M_CSUM_UDPv6));
2596 1.1 yamaguch
2597 1.1 yamaguch if (ISSET(qword, IXL_RX_DESC_IPE)) {
2598 1.1 yamaguch m->m_pkthdr.csum_flags |= (flags_mask & M_CSUM_IPv4_BAD);
2599 1.1 yamaguch }
2600 1.1 yamaguch
2601 1.1 yamaguch if (ISSET(qword, IXL_RX_DESC_L4E)) {
2602 1.1 yamaguch m->m_pkthdr.csum_flags |= (flags_mask & M_CSUM_TCP_UDP_BAD);
2603 1.1 yamaguch }
2604 1.1 yamaguch }
2605 1.1 yamaguch
2606 1.1 yamaguch static int
2607 1.1 yamaguch iavf_rxeof(struct iavf_softc *sc, struct iavf_rx_ring *rxr, u_int rxlimit,
2608 1.1 yamaguch struct evcnt *ecnt)
2609 1.1 yamaguch {
2610 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
2611 1.1 yamaguch struct ixl_rx_wb_desc_32 *ring, *rxd;
2612 1.1 yamaguch struct iavf_rx_map *rxm;
2613 1.1 yamaguch bus_dmamap_t map;
2614 1.1 yamaguch unsigned int cons, prod;
2615 1.1 yamaguch struct mbuf *m;
2616 1.1 yamaguch uint64_t word, word0;
2617 1.1 yamaguch unsigned int len;
2618 1.1 yamaguch unsigned int mask;
2619 1.1 yamaguch int done = 0, more = 0;
2620 1.1 yamaguch
2621 1.1 yamaguch KASSERT(mutex_owned(&rxr->rxr_lock));
2622 1.1 yamaguch
2623 1.1 yamaguch if (!ISSET(ifp->if_flags, IFF_RUNNING))
2624 1.1 yamaguch return 0;
2625 1.1 yamaguch
2626 1.1 yamaguch prod = rxr->rxr_prod;
2627 1.1 yamaguch cons = rxr->rxr_cons;
2628 1.1 yamaguch
2629 1.1 yamaguch if (cons == prod)
2630 1.1 yamaguch return 0;
2631 1.1 yamaguch
2632 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&rxr->rxr_mem),
2633 1.1 yamaguch 0, IXL_DMA_LEN(&rxr->rxr_mem),
2634 1.1 yamaguch BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2635 1.1 yamaguch
2636 1.1 yamaguch ring = IXL_DMA_KVA(&rxr->rxr_mem);
2637 1.1 yamaguch mask = sc->sc_rx_ring_ndescs - 1;
2638 1.1 yamaguch
2639 1.1 yamaguch net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
2640 1.1 yamaguch
2641 1.1 yamaguch do {
2642 1.1 yamaguch if (rxlimit-- <= 0) {
2643 1.1 yamaguch more = 1;
2644 1.1 yamaguch break;
2645 1.1 yamaguch }
2646 1.1 yamaguch
2647 1.1 yamaguch rxd = &ring[cons];
2648 1.1 yamaguch
2649 1.1 yamaguch word = le64toh(rxd->qword1);
2650 1.1 yamaguch
2651 1.1 yamaguch if (!ISSET(word, IXL_RX_DESC_DD))
2652 1.1 yamaguch break;
2653 1.1 yamaguch
2654 1.1 yamaguch rxm = &rxr->rxr_maps[cons];
2655 1.1 yamaguch
2656 1.1 yamaguch map = rxm->rxm_map;
2657 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
2658 1.1 yamaguch BUS_DMASYNC_POSTREAD);
2659 1.1 yamaguch bus_dmamap_unload(sc->sc_dmat, map);
2660 1.1 yamaguch
2661 1.1 yamaguch m = rxm->rxm_m;
2662 1.1 yamaguch rxm->rxm_m = NULL;
2663 1.1 yamaguch
2664 1.1 yamaguch KASSERT(m != NULL);
2665 1.1 yamaguch
2666 1.1 yamaguch len = (word & IXL_RX_DESC_PLEN_MASK) >> IXL_RX_DESC_PLEN_SHIFT;
2667 1.1 yamaguch m->m_len = len;
2668 1.1 yamaguch m->m_pkthdr.len = 0;
2669 1.1 yamaguch
2670 1.1 yamaguch m->m_next = NULL;
2671 1.1 yamaguch *rxr->rxr_m_tail = m;
2672 1.1 yamaguch rxr->rxr_m_tail = &m->m_next;
2673 1.1 yamaguch
2674 1.1 yamaguch m = rxr->rxr_m_head;
2675 1.1 yamaguch m->m_pkthdr.len += len;
2676 1.1 yamaguch
2677 1.1 yamaguch if (ISSET(word, IXL_RX_DESC_EOP)) {
2678 1.1 yamaguch word0 = le64toh(rxd->qword0);
2679 1.1 yamaguch
2680 1.1 yamaguch if (ISSET(word, IXL_RX_DESC_L2TAG1P)) {
2681 1.1 yamaguch vlan_set_tag(m,
2682 1.1 yamaguch __SHIFTOUT(word0, IXL_RX_DESC_L2TAG1_MASK));
2683 1.1 yamaguch }
2684 1.1 yamaguch
2685 1.1 yamaguch if ((ifp->if_capenable & IAVF_IFCAP_RXCSUM) != 0)
2686 1.1 yamaguch iavf_rx_csum(m, word);
2687 1.1 yamaguch
2688 1.1 yamaguch if (!ISSET(word,
2689 1.1 yamaguch IXL_RX_DESC_RXE | IXL_RX_DESC_OVERSIZE)) {
2690 1.1 yamaguch m_set_rcvif(m, ifp);
2691 1.1 yamaguch if_statinc_ref(nsr, if_ipackets);
2692 1.1 yamaguch if_statadd_ref(nsr, if_ibytes,
2693 1.1 yamaguch m->m_pkthdr.len);
2694 1.1 yamaguch if_percpuq_enqueue(sc->sc_ipq, m);
2695 1.1 yamaguch } else {
2696 1.1 yamaguch if_statinc_ref(nsr, if_ierrors);
2697 1.1 yamaguch m_freem(m);
2698 1.1 yamaguch }
2699 1.1 yamaguch
2700 1.1 yamaguch rxr->rxr_m_head = NULL;
2701 1.1 yamaguch rxr->rxr_m_tail = &rxr->rxr_m_head;
2702 1.1 yamaguch }
2703 1.1 yamaguch
2704 1.1 yamaguch cons++;
2705 1.1 yamaguch cons &= mask;
2706 1.1 yamaguch
2707 1.1 yamaguch done = 1;
2708 1.1 yamaguch } while (cons != prod);
2709 1.1 yamaguch
2710 1.1 yamaguch if (done) {
2711 1.1 yamaguch ecnt->ev_count++;
2712 1.1 yamaguch rxr->rxr_cons = cons;
2713 1.1 yamaguch if (iavf_rxfill(sc, rxr) == -1)
2714 1.1 yamaguch if_statinc_ref(nsr, if_iqdrops);
2715 1.1 yamaguch }
2716 1.1 yamaguch
2717 1.1 yamaguch IF_STAT_PUTREF(ifp);
2718 1.1 yamaguch
2719 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&rxr->rxr_mem),
2720 1.1 yamaguch 0, IXL_DMA_LEN(&rxr->rxr_mem),
2721 1.1 yamaguch BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2722 1.1 yamaguch
2723 1.1 yamaguch return more;
2724 1.1 yamaguch }
2725 1.1 yamaguch
2726 1.1 yamaguch static void
2727 1.1 yamaguch iavf_rxr_clean(struct iavf_softc *sc, struct iavf_rx_ring *rxr)
2728 1.1 yamaguch {
2729 1.1 yamaguch struct iavf_rx_map *maps, *rxm;
2730 1.1 yamaguch bus_dmamap_t map;
2731 1.1 yamaguch unsigned int i;
2732 1.1 yamaguch
2733 1.1 yamaguch KASSERT(mutex_owned(&rxr->rxr_lock));
2734 1.1 yamaguch
2735 1.1 yamaguch maps = rxr->rxr_maps;
2736 1.1 yamaguch for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
2737 1.1 yamaguch rxm = &maps[i];
2738 1.1 yamaguch
2739 1.1 yamaguch if (rxm->rxm_m == NULL)
2740 1.1 yamaguch continue;
2741 1.1 yamaguch
2742 1.1 yamaguch map = rxm->rxm_map;
2743 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
2744 1.1 yamaguch BUS_DMASYNC_POSTWRITE);
2745 1.1 yamaguch bus_dmamap_unload(sc->sc_dmat, map);
2746 1.1 yamaguch
2747 1.1 yamaguch m_freem(rxm->rxm_m);
2748 1.1 yamaguch rxm->rxm_m = NULL;
2749 1.1 yamaguch }
2750 1.1 yamaguch
2751 1.1 yamaguch m_freem(rxr->rxr_m_head);
2752 1.1 yamaguch rxr->rxr_m_head = NULL;
2753 1.1 yamaguch rxr->rxr_m_tail = &rxr->rxr_m_head;
2754 1.1 yamaguch
2755 1.1 yamaguch memset(IXL_DMA_KVA(&rxr->rxr_mem), 0, IXL_DMA_LEN(&rxr->rxr_mem));
2756 1.1 yamaguch rxr->rxr_prod = rxr->rxr_cons = 0;
2757 1.1 yamaguch }
2758 1.1 yamaguch
2759 1.1 yamaguch static int
2760 1.1 yamaguch iavf_txeof(struct iavf_softc *sc, struct iavf_tx_ring *txr, u_int txlimit,
2761 1.1 yamaguch struct evcnt *ecnt)
2762 1.1 yamaguch {
2763 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
2764 1.1 yamaguch struct ixl_tx_desc *ring, *txd;
2765 1.1 yamaguch struct iavf_tx_map *txm;
2766 1.1 yamaguch struct mbuf *m;
2767 1.1 yamaguch bus_dmamap_t map;
2768 1.1 yamaguch unsigned int cons, prod, last;
2769 1.1 yamaguch unsigned int mask;
2770 1.1 yamaguch uint64_t dtype;
2771 1.1 yamaguch int done = 0, more = 0;
2772 1.1 yamaguch
2773 1.1 yamaguch KASSERT(mutex_owned(&txr->txr_lock));
2774 1.1 yamaguch
2775 1.1 yamaguch prod = txr->txr_prod;
2776 1.1 yamaguch cons = txr->txr_cons;
2777 1.1 yamaguch
2778 1.1 yamaguch if (cons == prod)
2779 1.1 yamaguch return 0;
2780 1.1 yamaguch
2781 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
2782 1.1 yamaguch 0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_POSTREAD);
2783 1.1 yamaguch
2784 1.1 yamaguch ring = IXL_DMA_KVA(&txr->txr_mem);
2785 1.1 yamaguch mask = sc->sc_tx_ring_ndescs - 1;
2786 1.1 yamaguch
2787 1.1 yamaguch net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
2788 1.1 yamaguch
2789 1.1 yamaguch do {
2790 1.1 yamaguch if (txlimit-- <= 0) {
2791 1.1 yamaguch more = 1;
2792 1.1 yamaguch break;
2793 1.1 yamaguch }
2794 1.1 yamaguch
2795 1.1 yamaguch txm = &txr->txr_maps[cons];
2796 1.1 yamaguch last = txm->txm_eop;
2797 1.1 yamaguch txd = &ring[last];
2798 1.1 yamaguch
2799 1.1 yamaguch dtype = txd->cmd & htole64(IXL_TX_DESC_DTYPE_MASK);
2800 1.1 yamaguch if (dtype != htole64(IXL_TX_DESC_DTYPE_DONE))
2801 1.1 yamaguch break;
2802 1.1 yamaguch
2803 1.1 yamaguch map = txm->txm_map;
2804 1.1 yamaguch
2805 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
2806 1.1 yamaguch BUS_DMASYNC_POSTWRITE);
2807 1.1 yamaguch bus_dmamap_unload(sc->sc_dmat, map);
2808 1.1 yamaguch
2809 1.1 yamaguch m = txm->txm_m;
2810 1.1 yamaguch if (m != NULL) {
2811 1.1 yamaguch if_statinc_ref(nsr, if_opackets);
2812 1.1 yamaguch if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
2813 1.1 yamaguch if (ISSET(m->m_flags, M_MCAST))
2814 1.1 yamaguch if_statinc_ref(nsr, if_omcasts);
2815 1.1 yamaguch m_freem(m);
2816 1.1 yamaguch }
2817 1.1 yamaguch
2818 1.1 yamaguch txm->txm_m = NULL;
2819 1.1 yamaguch txm->txm_eop = -1;
2820 1.1 yamaguch
2821 1.1 yamaguch cons = last + 1;
2822 1.1 yamaguch cons &= mask;
2823 1.1 yamaguch done = 1;
2824 1.1 yamaguch } while (cons != prod);
2825 1.1 yamaguch
2826 1.1 yamaguch IF_STAT_PUTREF(ifp);
2827 1.1 yamaguch
2828 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
2829 1.1 yamaguch 0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_PREREAD);
2830 1.1 yamaguch
2831 1.1 yamaguch txr->txr_cons = cons;
2832 1.1 yamaguch
2833 1.1 yamaguch if (done) {
2834 1.1 yamaguch ecnt->ev_count++;
2835 1.1 yamaguch softint_schedule(txr->txr_si);
2836 1.1 yamaguch if (txr->txr_qid == 0) {
2837 1.1 yamaguch CLR(ifp->if_flags, IFF_OACTIVE);
2838 1.1 yamaguch if_schedule_deferred_start(ifp);
2839 1.1 yamaguch }
2840 1.1 yamaguch }
2841 1.1 yamaguch
2842 1.1 yamaguch if (txr->txr_cons == txr->txr_prod) {
2843 1.1 yamaguch txr->txr_watchdog = IAVF_WATCHDOG_STOP;
2844 1.1 yamaguch }
2845 1.1 yamaguch
2846 1.1 yamaguch return more;
2847 1.1 yamaguch }
2848 1.1 yamaguch
2849 1.1 yamaguch static inline int
2850 1.1 yamaguch iavf_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf **m0,
2851 1.1 yamaguch struct iavf_tx_ring *txr)
2852 1.1 yamaguch {
2853 1.1 yamaguch struct mbuf *m;
2854 1.1 yamaguch int error;
2855 1.1 yamaguch
2856 1.1 yamaguch KASSERT(mutex_owned(&txr->txr_lock));
2857 1.1 yamaguch
2858 1.1 yamaguch m = *m0;
2859 1.1 yamaguch
2860 1.1 yamaguch error = bus_dmamap_load_mbuf(dmat, map, m,
2861 1.1 yamaguch BUS_DMA_STREAMING|BUS_DMA_WRITE|BUS_DMA_NOWAIT);
2862 1.1 yamaguch if (error != EFBIG)
2863 1.1 yamaguch return error;
2864 1.1 yamaguch
2865 1.1 yamaguch m = m_defrag(m, M_DONTWAIT);
2866 1.1 yamaguch if (m != NULL) {
2867 1.1 yamaguch *m0 = m;
2868 1.1 yamaguch txr->txr_defragged.ev_count++;
2869 1.1 yamaguch error = bus_dmamap_load_mbuf(dmat, map, m,
2870 1.1 yamaguch BUS_DMA_STREAMING|BUS_DMA_WRITE|BUS_DMA_NOWAIT);
2871 1.1 yamaguch } else {
2872 1.1 yamaguch txr->txr_defrag_failed.ev_count++;
2873 1.1 yamaguch error = ENOBUFS;
2874 1.1 yamaguch }
2875 1.1 yamaguch
2876 1.1 yamaguch return error;
2877 1.1 yamaguch }
2878 1.1 yamaguch
2879 1.1 yamaguch static inline int
2880 1.1 yamaguch iavf_tx_setup_offloads(struct mbuf *m, uint64_t *cmd_txd)
2881 1.1 yamaguch {
2882 1.1 yamaguch struct ether_header *eh;
2883 1.1 yamaguch size_t len;
2884 1.1 yamaguch uint64_t cmd;
2885 1.1 yamaguch
2886 1.1 yamaguch cmd = 0;
2887 1.1 yamaguch
2888 1.1 yamaguch eh = mtod(m, struct ether_header *);
2889 1.1 yamaguch switch (htons(eh->ether_type)) {
2890 1.1 yamaguch case ETHERTYPE_IP:
2891 1.1 yamaguch case ETHERTYPE_IPV6:
2892 1.1 yamaguch len = ETHER_HDR_LEN;
2893 1.1 yamaguch break;
2894 1.1 yamaguch case ETHERTYPE_VLAN:
2895 1.1 yamaguch len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
2896 1.1 yamaguch break;
2897 1.1 yamaguch default:
2898 1.1 yamaguch len = 0;
2899 1.1 yamaguch }
2900 1.1 yamaguch cmd |= ((len >> 1) << IXL_TX_DESC_MACLEN_SHIFT);
2901 1.1 yamaguch
2902 1.1 yamaguch if (m->m_pkthdr.csum_flags &
2903 1.1 yamaguch (M_CSUM_TSOv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
2904 1.1 yamaguch cmd |= IXL_TX_DESC_CMD_IIPT_IPV4;
2905 1.1 yamaguch }
2906 1.1 yamaguch if (m->m_pkthdr.csum_flags & M_CSUM_IPv4) {
2907 1.1 yamaguch cmd |= IXL_TX_DESC_CMD_IIPT_IPV4_CSUM;
2908 1.1 yamaguch }
2909 1.1 yamaguch
2910 1.1 yamaguch if (m->m_pkthdr.csum_flags &
2911 1.1 yamaguch (M_CSUM_TSOv6 | M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
2912 1.1 yamaguch cmd |= IXL_TX_DESC_CMD_IIPT_IPV6;
2913 1.1 yamaguch }
2914 1.1 yamaguch
2915 1.1 yamaguch switch (cmd & IXL_TX_DESC_CMD_IIPT_MASK) {
2916 1.1 yamaguch case IXL_TX_DESC_CMD_IIPT_IPV4:
2917 1.1 yamaguch case IXL_TX_DESC_CMD_IIPT_IPV4_CSUM:
2918 1.1 yamaguch len = M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
2919 1.1 yamaguch break;
2920 1.1 yamaguch case IXL_TX_DESC_CMD_IIPT_IPV6:
2921 1.1 yamaguch len = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
2922 1.1 yamaguch break;
2923 1.1 yamaguch default:
2924 1.1 yamaguch len = 0;
2925 1.1 yamaguch }
2926 1.1 yamaguch cmd |= ((len >> 2) << IXL_TX_DESC_IPLEN_SHIFT);
2927 1.1 yamaguch
2928 1.1 yamaguch if (m->m_pkthdr.csum_flags &
2929 1.1 yamaguch (M_CSUM_TSOv4 | M_CSUM_TSOv6 | M_CSUM_TCPv4 | M_CSUM_TCPv6)) {
2930 1.1 yamaguch len = sizeof(struct tcphdr);
2931 1.1 yamaguch cmd |= IXL_TX_DESC_CMD_L4T_EOFT_TCP;
2932 1.1 yamaguch } else if (m->m_pkthdr.csum_flags & (M_CSUM_UDPv4 | M_CSUM_UDPv6)) {
2933 1.1 yamaguch len = sizeof(struct udphdr);
2934 1.1 yamaguch cmd |= IXL_TX_DESC_CMD_L4T_EOFT_UDP;
2935 1.1 yamaguch } else {
2936 1.1 yamaguch len = 0;
2937 1.1 yamaguch }
2938 1.1 yamaguch cmd |= ((len >> 2) << IXL_TX_DESC_L4LEN_SHIFT);
2939 1.1 yamaguch
2940 1.1 yamaguch *cmd_txd |= cmd;
2941 1.1 yamaguch return 0;
2942 1.1 yamaguch }
2943 1.1 yamaguch
2944 1.1 yamaguch static void
2945 1.1 yamaguch iavf_tx_common_locked(struct ifnet *ifp, struct iavf_tx_ring *txr,
2946 1.1 yamaguch bool is_transmit)
2947 1.1 yamaguch {
2948 1.1 yamaguch struct iavf_softc *sc;
2949 1.1 yamaguch struct ixl_tx_desc *ring, *txd;
2950 1.1 yamaguch struct iavf_tx_map *txm;
2951 1.1 yamaguch bus_dmamap_t map;
2952 1.1 yamaguch struct mbuf *m;
2953 1.1 yamaguch unsigned int prod, free, last, i;
2954 1.1 yamaguch unsigned int mask;
2955 1.1 yamaguch uint64_t cmd, cmd_txd;
2956 1.1 yamaguch int post = 0;
2957 1.1 yamaguch
2958 1.1 yamaguch KASSERT(mutex_owned(&txr->txr_lock));
2959 1.1 yamaguch
2960 1.1 yamaguch sc = ifp->if_softc;
2961 1.1 yamaguch
2962 1.1 yamaguch if (!ISSET(ifp->if_flags, IFF_RUNNING)
2963 1.1 yamaguch || (!is_transmit && ISSET(ifp->if_flags, IFF_OACTIVE))) {
2964 1.1 yamaguch if (!is_transmit)
2965 1.1 yamaguch IFQ_PURGE(&ifp->if_snd);
2966 1.1 yamaguch return;
2967 1.1 yamaguch }
2968 1.1 yamaguch
2969 1.1 yamaguch prod = txr->txr_prod;
2970 1.1 yamaguch free = txr->txr_cons;
2971 1.1 yamaguch
2972 1.1 yamaguch if (free <= prod)
2973 1.1 yamaguch free += sc->sc_tx_ring_ndescs;
2974 1.1 yamaguch free -= prod;
2975 1.1 yamaguch
2976 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
2977 1.1 yamaguch 0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_POSTWRITE);
2978 1.1 yamaguch
2979 1.1 yamaguch ring = IXL_DMA_KVA(&txr->txr_mem);
2980 1.1 yamaguch mask = sc->sc_tx_ring_ndescs - 1;
2981 1.1 yamaguch last = prod;
2982 1.1 yamaguch cmd = 0;
2983 1.1 yamaguch txd = NULL;
2984 1.1 yamaguch
2985 1.1 yamaguch for (;;) {
2986 1.1 yamaguch if (free < IAVF_TX_PKT_DESCS) {
2987 1.1 yamaguch if (!is_transmit)
2988 1.1 yamaguch SET(ifp->if_flags, IFF_OACTIVE);
2989 1.1 yamaguch break;
2990 1.1 yamaguch }
2991 1.1 yamaguch
2992 1.1 yamaguch if (is_transmit)
2993 1.1 yamaguch m = pcq_get(txr->txr_intrq);
2994 1.1 yamaguch else
2995 1.1 yamaguch IFQ_DEQUEUE(&ifp->if_snd, m);
2996 1.1 yamaguch
2997 1.1 yamaguch if (m == NULL)
2998 1.1 yamaguch break;
2999 1.1 yamaguch
3000 1.1 yamaguch txm = &txr->txr_maps[prod];
3001 1.1 yamaguch map = txm->txm_map;
3002 1.1 yamaguch
3003 1.1 yamaguch if (iavf_load_mbuf(sc->sc_dmat, map, &m, txr) != 0) {
3004 1.1 yamaguch if_statinc(ifp, if_oerrors);
3005 1.1 yamaguch m_freem(m);
3006 1.1 yamaguch continue;
3007 1.1 yamaguch }
3008 1.1 yamaguch
3009 1.1 yamaguch cmd_txd = 0;
3010 1.1 yamaguch if (m->m_pkthdr.csum_flags & IAVF_CSUM_ALL_OFFLOAD) {
3011 1.1 yamaguch iavf_tx_setup_offloads(m, &cmd_txd);
3012 1.1 yamaguch }
3013 1.1 yamaguch if (vlan_has_tag(m)) {
3014 1.1 yamaguch cmd_txd |= IXL_TX_DESC_CMD_IL2TAG1 |
3015 1.1 yamaguch ((uint64_t)vlan_get_tag(m)
3016 1.1 yamaguch << IXL_TX_DESC_L2TAG1_SHIFT);
3017 1.1 yamaguch }
3018 1.1 yamaguch
3019 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, map, 0,
3020 1.1 yamaguch map->dm_mapsize, BUS_DMASYNC_PREWRITE);
3021 1.1 yamaguch
3022 1.1 yamaguch for (i = 0; i < (unsigned int)map->dm_nsegs; i++) {
3023 1.1 yamaguch txd = &ring[prod];
3024 1.1 yamaguch
3025 1.1 yamaguch cmd = (uint64_t)map->dm_segs[i].ds_len <<
3026 1.1 yamaguch IXL_TX_DESC_BSIZE_SHIFT;
3027 1.1 yamaguch cmd |= IXL_TX_DESC_DTYPE_DATA|IXL_TX_DESC_CMD_ICRC|
3028 1.1 yamaguch cmd_txd;
3029 1.1 yamaguch
3030 1.1 yamaguch txd->addr = htole64(map->dm_segs[i].ds_addr);
3031 1.1 yamaguch txd->cmd = htole64(cmd);
3032 1.1 yamaguch
3033 1.1 yamaguch last = prod;
3034 1.1 yamaguch prod++;
3035 1.1 yamaguch prod &= mask;
3036 1.1 yamaguch }
3037 1.1 yamaguch
3038 1.1 yamaguch cmd |= IXL_TX_DESC_CMD_EOP|IXL_TX_DESC_CMD_RS;
3039 1.1 yamaguch txd->cmd = htole64(cmd);
3040 1.1 yamaguch txm->txm_m = m;
3041 1.1 yamaguch txm->txm_eop = last;
3042 1.1 yamaguch
3043 1.1 yamaguch bpf_mtap(ifp, m, BPF_D_OUT);
3044 1.1 yamaguch free -= i;
3045 1.1 yamaguch post = 1;
3046 1.1 yamaguch }
3047 1.1 yamaguch
3048 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
3049 1.1 yamaguch 0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_PREWRITE);
3050 1.1 yamaguch
3051 1.1 yamaguch if (post) {
3052 1.1 yamaguch txr->txr_prod = prod;
3053 1.1 yamaguch iavf_wr(sc, txr->txr_tail, prod);
3054 1.1 yamaguch txr->txr_watchdog = IAVF_WATCHDOG_TICKS;
3055 1.1 yamaguch }
3056 1.1 yamaguch }
3057 1.1 yamaguch
3058 1.1 yamaguch static inline int
3059 1.1 yamaguch iavf_handle_queue_common(struct iavf_softc *sc, struct iavf_queue_pair *qp,
3060 1.1 yamaguch u_int txlimit, struct evcnt *txevcnt,
3061 1.1 yamaguch u_int rxlimit, struct evcnt *rxevcnt)
3062 1.1 yamaguch {
3063 1.1 yamaguch struct iavf_tx_ring *txr;
3064 1.1 yamaguch struct iavf_rx_ring *rxr;
3065 1.1 yamaguch int txmore, rxmore;
3066 1.1 yamaguch int rv;
3067 1.1 yamaguch
3068 1.1 yamaguch txr = qp->qp_txr;
3069 1.1 yamaguch rxr = qp->qp_rxr;
3070 1.1 yamaguch
3071 1.1 yamaguch mutex_enter(&txr->txr_lock);
3072 1.1 yamaguch txmore = iavf_txeof(sc, txr, txlimit, txevcnt);
3073 1.1 yamaguch mutex_exit(&txr->txr_lock);
3074 1.1 yamaguch
3075 1.1 yamaguch mutex_enter(&rxr->rxr_lock);
3076 1.1 yamaguch rxmore = iavf_rxeof(sc, rxr, rxlimit, rxevcnt);
3077 1.1 yamaguch mutex_exit(&rxr->rxr_lock);
3078 1.1 yamaguch
3079 1.1 yamaguch rv = txmore | (rxmore << 1);
3080 1.1 yamaguch
3081 1.1 yamaguch return rv;
3082 1.1 yamaguch }
3083 1.1 yamaguch
3084 1.1 yamaguch static void
3085 1.1 yamaguch iavf_sched_handle_queue(struct iavf_softc *sc, struct iavf_queue_pair *qp)
3086 1.1 yamaguch {
3087 1.1 yamaguch
3088 1.1 yamaguch if (qp->qp_workqueue)
3089 1.1 yamaguch workqueue_enqueue(sc->sc_workq_txrx, &qp->qp_work, NULL);
3090 1.1 yamaguch else
3091 1.1 yamaguch softint_schedule(qp->qp_si);
3092 1.1 yamaguch }
3093 1.1 yamaguch
3094 1.1 yamaguch static void
3095 1.1 yamaguch iavf_start(struct ifnet *ifp)
3096 1.1 yamaguch {
3097 1.1 yamaguch struct iavf_softc *sc;
3098 1.1 yamaguch struct iavf_tx_ring *txr;
3099 1.1 yamaguch
3100 1.1 yamaguch sc = ifp->if_softc;
3101 1.1 yamaguch txr = sc->sc_qps[0].qp_txr;
3102 1.1 yamaguch
3103 1.1 yamaguch mutex_enter(&txr->txr_lock);
3104 1.1 yamaguch iavf_tx_common_locked(ifp, txr, false);
3105 1.1 yamaguch mutex_exit(&txr->txr_lock);
3106 1.1 yamaguch
3107 1.1 yamaguch }
3108 1.1 yamaguch
3109 1.1 yamaguch static inline unsigned int
3110 1.1 yamaguch iavf_select_txqueue(struct iavf_softc *sc, struct mbuf *m)
3111 1.1 yamaguch {
3112 1.1 yamaguch u_int cpuid;
3113 1.1 yamaguch
3114 1.1 yamaguch cpuid = cpu_index(curcpu());
3115 1.1 yamaguch
3116 1.1 yamaguch return (unsigned int)(cpuid % sc->sc_nqueue_pairs);
3117 1.1 yamaguch }
3118 1.1 yamaguch
3119 1.1 yamaguch static int
3120 1.1 yamaguch iavf_transmit(struct ifnet *ifp, struct mbuf *m)
3121 1.1 yamaguch {
3122 1.1 yamaguch struct iavf_softc *sc;
3123 1.1 yamaguch struct iavf_tx_ring *txr;
3124 1.1 yamaguch unsigned int qid;
3125 1.1 yamaguch
3126 1.1 yamaguch sc = ifp->if_softc;
3127 1.1 yamaguch qid = iavf_select_txqueue(sc, m);
3128 1.1 yamaguch
3129 1.1 yamaguch txr = sc->sc_qps[qid].qp_txr;
3130 1.1 yamaguch
3131 1.1 yamaguch if (__predict_false(!pcq_put(txr->txr_intrq, m))) {
3132 1.1 yamaguch mutex_enter(&txr->txr_lock);
3133 1.1 yamaguch txr->txr_pcqdrop.ev_count++;
3134 1.1 yamaguch mutex_exit(&txr->txr_lock);
3135 1.1 yamaguch
3136 1.1 yamaguch m_freem(m);
3137 1.1 yamaguch return ENOBUFS;
3138 1.1 yamaguch }
3139 1.1 yamaguch
3140 1.1 yamaguch if (mutex_tryenter(&txr->txr_lock)) {
3141 1.1 yamaguch iavf_tx_common_locked(ifp, txr, true);
3142 1.1 yamaguch mutex_exit(&txr->txr_lock);
3143 1.1 yamaguch } else {
3144 1.1 yamaguch kpreempt_disable();
3145 1.1 yamaguch softint_schedule(txr->txr_si);
3146 1.1 yamaguch kpreempt_enable();
3147 1.1 yamaguch }
3148 1.1 yamaguch return 0;
3149 1.1 yamaguch }
3150 1.1 yamaguch
3151 1.1 yamaguch static void
3152 1.1 yamaguch iavf_deferred_transmit(void *xtxr)
3153 1.1 yamaguch {
3154 1.1 yamaguch struct iavf_tx_ring *txr;
3155 1.1 yamaguch struct iavf_softc *sc;
3156 1.1 yamaguch struct ifnet *ifp;
3157 1.1 yamaguch
3158 1.1 yamaguch txr = xtxr;
3159 1.1 yamaguch sc = txr->txr_sc;
3160 1.1 yamaguch ifp = &sc->sc_ec.ec_if;
3161 1.1 yamaguch
3162 1.1 yamaguch mutex_enter(&txr->txr_lock);
3163 1.1 yamaguch txr->txr_transmitdef.ev_count++;
3164 1.1 yamaguch if (pcq_peek(txr->txr_intrq) != NULL)
3165 1.1 yamaguch iavf_tx_common_locked(ifp, txr, true);
3166 1.1 yamaguch mutex_exit(&txr->txr_lock);
3167 1.1 yamaguch }
3168 1.1 yamaguch
3169 1.1 yamaguch static void
3170 1.1 yamaguch iavf_txr_clean(struct iavf_softc *sc, struct iavf_tx_ring *txr)
3171 1.1 yamaguch {
3172 1.1 yamaguch struct iavf_tx_map *maps, *txm;
3173 1.1 yamaguch bus_dmamap_t map;
3174 1.1 yamaguch unsigned int i;
3175 1.1 yamaguch
3176 1.1 yamaguch KASSERT(mutex_owned(&txr->txr_lock));
3177 1.1 yamaguch
3178 1.1 yamaguch maps = txr->txr_maps;
3179 1.1 yamaguch for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
3180 1.1 yamaguch txm = &maps[i];
3181 1.1 yamaguch
3182 1.1 yamaguch if (txm->txm_m == NULL)
3183 1.1 yamaguch continue;
3184 1.1 yamaguch
3185 1.1 yamaguch map = txm->txm_map;
3186 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
3187 1.1 yamaguch BUS_DMASYNC_POSTWRITE);
3188 1.1 yamaguch bus_dmamap_unload(sc->sc_dmat, map);
3189 1.1 yamaguch
3190 1.1 yamaguch m_freem(txm->txm_m);
3191 1.1 yamaguch txm->txm_m = NULL;
3192 1.1 yamaguch }
3193 1.1 yamaguch
3194 1.1 yamaguch memset(IXL_DMA_KVA(&txr->txr_mem), 0, IXL_DMA_LEN(&txr->txr_mem));
3195 1.1 yamaguch txr->txr_prod = txr->txr_cons = 0;
3196 1.1 yamaguch }
3197 1.1 yamaguch
3198 1.1 yamaguch static int
3199 1.1 yamaguch iavf_intr(void *xsc)
3200 1.1 yamaguch {
3201 1.1 yamaguch struct iavf_softc *sc = xsc;
3202 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
3203 1.1 yamaguch struct iavf_rx_ring *rxr;
3204 1.1 yamaguch struct iavf_tx_ring *txr;
3205 1.1 yamaguch uint32_t icr;
3206 1.1 yamaguch unsigned int i;
3207 1.1 yamaguch
3208 1.1 yamaguch /* read I40E_VFINT_ICR_ENA1 to clear status */
3209 1.1 yamaguch (void)iavf_rd(sc, I40E_VFINT_ICR0_ENA1);
3210 1.1 yamaguch
3211 1.1 yamaguch iavf_intr_enable(sc);
3212 1.1 yamaguch icr = iavf_rd(sc, I40E_VFINT_ICR01);
3213 1.1 yamaguch
3214 1.1 yamaguch if (icr == IAVF_REG_VFR) {
3215 1.1 yamaguch log(LOG_INFO, "%s: VF reset in progress\n",
3216 1.1 yamaguch ifp->if_xname);
3217 1.1 yamaguch iavf_work_set(&sc->sc_reset_task, iavf_reset_start, sc);
3218 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_reset_task);
3219 1.1 yamaguch return 1;
3220 1.1 yamaguch }
3221 1.1 yamaguch
3222 1.1 yamaguch if (ISSET(icr, I40E_VFINT_ICR01_ADMINQ_MASK)) {
3223 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
3224 1.1 yamaguch iavf_atq_done(sc);
3225 1.1 yamaguch iavf_arq(sc);
3226 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
3227 1.1 yamaguch }
3228 1.1 yamaguch
3229 1.1 yamaguch if (ISSET(icr, I40E_VFINT_ICR01_QUEUE_0_MASK)) {
3230 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
3231 1.1 yamaguch rxr = sc->sc_qps[i].qp_rxr;
3232 1.1 yamaguch txr = sc->sc_qps[i].qp_txr;
3233 1.1 yamaguch
3234 1.1 yamaguch mutex_enter(&rxr->rxr_lock);
3235 1.1 yamaguch while (iavf_rxeof(sc, rxr, UINT_MAX,
3236 1.1 yamaguch &rxr->rxr_intr) != 0) {
3237 1.1 yamaguch /* do nothing */
3238 1.1 yamaguch }
3239 1.1 yamaguch mutex_exit(&rxr->rxr_lock);
3240 1.1 yamaguch
3241 1.1 yamaguch mutex_enter(&txr->txr_lock);
3242 1.1 yamaguch while (iavf_txeof(sc, txr, UINT_MAX,
3243 1.1 yamaguch &txr->txr_intr) != 0) {
3244 1.1 yamaguch /* do nothing */
3245 1.1 yamaguch }
3246 1.1 yamaguch mutex_exit(&txr->txr_lock);
3247 1.1 yamaguch }
3248 1.1 yamaguch }
3249 1.1 yamaguch
3250 1.1 yamaguch return 0;
3251 1.1 yamaguch }
3252 1.1 yamaguch
3253 1.1 yamaguch static int
3254 1.1 yamaguch iavf_queue_intr(void *xqp)
3255 1.1 yamaguch {
3256 1.1 yamaguch struct iavf_queue_pair *qp = xqp;
3257 1.1 yamaguch struct iavf_tx_ring *txr;
3258 1.1 yamaguch struct iavf_rx_ring *rxr;
3259 1.1 yamaguch struct iavf_softc *sc;
3260 1.1 yamaguch unsigned int qid;
3261 1.1 yamaguch u_int txlimit, rxlimit;
3262 1.1 yamaguch int more;
3263 1.1 yamaguch
3264 1.1 yamaguch txr = qp->qp_txr;
3265 1.1 yamaguch rxr = qp->qp_rxr;
3266 1.1 yamaguch sc = txr->txr_sc;
3267 1.1 yamaguch qid = txr->txr_qid;
3268 1.1 yamaguch
3269 1.1 yamaguch txlimit = sc->sc_tx_intr_process_limit;
3270 1.1 yamaguch rxlimit = sc->sc_rx_intr_process_limit;
3271 1.1 yamaguch qp->qp_workqueue = sc->sc_txrx_workqueue;
3272 1.1 yamaguch
3273 1.1 yamaguch more = iavf_handle_queue_common(sc, qp,
3274 1.1 yamaguch txlimit, &txr->txr_intr, rxlimit, &rxr->rxr_intr);
3275 1.1 yamaguch
3276 1.1 yamaguch if (more != 0) {
3277 1.1 yamaguch iavf_sched_handle_queue(sc, qp);
3278 1.1 yamaguch } else {
3279 1.1 yamaguch /* for ALTQ */
3280 1.1 yamaguch if (txr->txr_qid == 0)
3281 1.1 yamaguch if_schedule_deferred_start(&sc->sc_ec.ec_if);
3282 1.1 yamaguch softint_schedule(txr->txr_si);
3283 1.1 yamaguch
3284 1.1 yamaguch iavf_queue_intr_enable(sc, qid);
3285 1.1 yamaguch }
3286 1.1 yamaguch
3287 1.1 yamaguch return 0;
3288 1.1 yamaguch }
3289 1.1 yamaguch
3290 1.1 yamaguch static void
3291 1.1 yamaguch iavf_handle_queue_wk(struct work *wk, void *xsc __unused)
3292 1.1 yamaguch {
3293 1.1 yamaguch struct iavf_queue_pair *qp;
3294 1.1 yamaguch
3295 1.1 yamaguch qp = container_of(wk, struct iavf_queue_pair, qp_work);
3296 1.1 yamaguch iavf_handle_queue(qp);
3297 1.1 yamaguch }
3298 1.1 yamaguch
3299 1.1 yamaguch static void
3300 1.1 yamaguch iavf_handle_queue(void *xqp)
3301 1.1 yamaguch {
3302 1.1 yamaguch struct iavf_queue_pair *qp = xqp;
3303 1.1 yamaguch struct iavf_tx_ring *txr;
3304 1.1 yamaguch struct iavf_rx_ring *rxr;
3305 1.1 yamaguch struct iavf_softc *sc;
3306 1.1 yamaguch unsigned int qid;
3307 1.1 yamaguch u_int txlimit, rxlimit;
3308 1.1 yamaguch int more;
3309 1.1 yamaguch
3310 1.1 yamaguch txr = qp->qp_txr;
3311 1.1 yamaguch rxr = qp->qp_rxr;
3312 1.1 yamaguch sc = txr->txr_sc;
3313 1.1 yamaguch qid = txr->txr_qid;
3314 1.1 yamaguch
3315 1.1 yamaguch txlimit = sc->sc_tx_process_limit;
3316 1.1 yamaguch rxlimit = sc->sc_rx_process_limit;
3317 1.1 yamaguch
3318 1.1 yamaguch more = iavf_handle_queue_common(sc, qp,
3319 1.1 yamaguch txlimit, &txr->txr_defer, rxlimit, &rxr->rxr_defer);
3320 1.1 yamaguch
3321 1.1 yamaguch if (more != 0)
3322 1.1 yamaguch iavf_sched_handle_queue(sc, qp);
3323 1.1 yamaguch else
3324 1.1 yamaguch iavf_queue_intr_enable(sc, qid);
3325 1.1 yamaguch }
3326 1.1 yamaguch
3327 1.1 yamaguch static void
3328 1.1 yamaguch iavf_tick(void *xsc)
3329 1.1 yamaguch {
3330 1.1 yamaguch struct iavf_softc *sc;
3331 1.1 yamaguch unsigned int i;
3332 1.1 yamaguch int timedout;
3333 1.1 yamaguch
3334 1.1 yamaguch sc = xsc;
3335 1.1 yamaguch timedout = 0;
3336 1.1 yamaguch
3337 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
3338 1.1 yamaguch
3339 1.1 yamaguch if (sc->sc_resetting) {
3340 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_reset_task);
3341 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
3342 1.1 yamaguch return;
3343 1.1 yamaguch }
3344 1.1 yamaguch
3345 1.1 yamaguch iavf_get_stats(sc);
3346 1.1 yamaguch
3347 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
3348 1.1 yamaguch timedout |= iavf_watchdog(sc->sc_qps[i].qp_txr);
3349 1.1 yamaguch }
3350 1.1 yamaguch
3351 1.1 yamaguch if (timedout != 0) {
3352 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_wdto_task);
3353 1.1 yamaguch } else {
3354 1.1 yamaguch callout_schedule(&sc->sc_tick, IAVF_TICK_INTERVAL);
3355 1.1 yamaguch }
3356 1.1 yamaguch
3357 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
3358 1.1 yamaguch }
3359 1.1 yamaguch
3360 1.1 yamaguch static void
3361 1.1 yamaguch iavf_tick_halt(void *unused __unused)
3362 1.1 yamaguch {
3363 1.1 yamaguch
3364 1.1 yamaguch /* do nothing */
3365 1.1 yamaguch }
3366 1.1 yamaguch
3367 1.1 yamaguch static void
3368 1.1 yamaguch iavf_reset_request(void *xsc)
3369 1.1 yamaguch {
3370 1.1 yamaguch struct iavf_softc *sc = xsc;
3371 1.1 yamaguch
3372 1.1 yamaguch iavf_reset_vf(sc);
3373 1.1 yamaguch iavf_reset_start(sc);
3374 1.1 yamaguch }
3375 1.1 yamaguch
3376 1.1 yamaguch static void
3377 1.1 yamaguch iavf_reset_start(void *xsc)
3378 1.1 yamaguch {
3379 1.1 yamaguch struct iavf_softc *sc = xsc;
3380 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
3381 1.1 yamaguch
3382 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
3383 1.1 yamaguch
3384 1.1 yamaguch if (sc->sc_resetting)
3385 1.1 yamaguch goto do_reset;
3386 1.1 yamaguch
3387 1.1 yamaguch sc->sc_resetting = true;
3388 1.1 yamaguch if_link_state_change(ifp, LINK_STATE_DOWN);
3389 1.1 yamaguch
3390 1.1 yamaguch if (ISSET(ifp->if_flags, IFF_RUNNING)) {
3391 1.1 yamaguch iavf_stop_locked(sc);
3392 1.1 yamaguch sc->sc_reset_up = true;
3393 1.1 yamaguch }
3394 1.1 yamaguch
3395 1.1 yamaguch memcpy(sc->sc_enaddr_reset, sc->sc_enaddr, ETHER_ADDR_LEN);
3396 1.1 yamaguch
3397 1.1 yamaguch do_reset:
3398 1.1 yamaguch iavf_work_set(&sc->sc_reset_task, iavf_reset, sc);
3399 1.1 yamaguch
3400 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
3401 1.1 yamaguch
3402 1.1 yamaguch iavf_reset((void *)sc);
3403 1.1 yamaguch }
3404 1.1 yamaguch
3405 1.1 yamaguch static void
3406 1.1 yamaguch iavf_reset(void *xsc)
3407 1.1 yamaguch {
3408 1.1 yamaguch struct iavf_softc *sc = xsc;
3409 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
3410 1.1 yamaguch struct ixl_aq_buf *aqb;
3411 1.1 yamaguch bool realloc_qps, realloc_intrs;
3412 1.1 yamaguch
3413 1.1 yamaguch mutex_enter(&sc->sc_cfg_lock);
3414 1.1 yamaguch
3415 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
3416 1.1 yamaguch iavf_cleanup_admin_queue(sc);
3417 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
3418 1.1 yamaguch
3419 1.1 yamaguch sc->sc_major_ver = UINT_MAX;
3420 1.1 yamaguch sc->sc_minor_ver = UINT_MAX;
3421 1.1 yamaguch sc->sc_got_vf_resources = 0;
3422 1.1 yamaguch sc->sc_got_irq_map = 0;
3423 1.1 yamaguch
3424 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
3425 1.1 yamaguch if (aqb == NULL)
3426 1.1 yamaguch goto failed;
3427 1.1 yamaguch
3428 1.1 yamaguch if (iavf_wait_active(sc) != 0) {
3429 1.1 yamaguch log(LOG_WARNING, "%s: VF reset timed out\n",
3430 1.1 yamaguch ifp->if_xname);
3431 1.1 yamaguch goto failed;
3432 1.1 yamaguch }
3433 1.1 yamaguch
3434 1.1 yamaguch if (!iavf_arq_fill(sc)) {
3435 1.1 yamaguch log(LOG_ERR, "%s: unable to fill arq descriptors\n",
3436 1.1 yamaguch ifp->if_xname);
3437 1.1 yamaguch goto failed;
3438 1.1 yamaguch }
3439 1.1 yamaguch
3440 1.1 yamaguch if (iavf_init_admin_queue(sc) != 0) {
3441 1.1 yamaguch log(LOG_ERR, "%s: unable to initialize admin queue\n",
3442 1.1 yamaguch ifp->if_xname);
3443 1.1 yamaguch goto failed;
3444 1.1 yamaguch }
3445 1.1 yamaguch
3446 1.1 yamaguch if (iavf_get_version(sc, aqb) != 0) {
3447 1.1 yamaguch log(LOG_ERR, "%s: unable to get VF interface version\n",
3448 1.1 yamaguch ifp->if_xname);
3449 1.1 yamaguch goto failed;
3450 1.1 yamaguch }
3451 1.1 yamaguch
3452 1.1 yamaguch if (iavf_get_vf_resources(sc, aqb) != 0) {
3453 1.1 yamaguch log(LOG_ERR, "%s: timed out waiting for VF resources\n",
3454 1.1 yamaguch ifp->if_xname);
3455 1.1 yamaguch goto failed;
3456 1.1 yamaguch }
3457 1.1 yamaguch
3458 1.1 yamaguch if (sc->sc_nqps_alloc < iavf_calc_queue_pair_size(sc)) {
3459 1.1 yamaguch realloc_qps = true;
3460 1.1 yamaguch } else {
3461 1.1 yamaguch realloc_qps = false;
3462 1.1 yamaguch }
3463 1.1 yamaguch
3464 1.1 yamaguch if (sc->sc_nintrs < iavf_calc_msix_count(sc)) {
3465 1.1 yamaguch realloc_intrs = true;
3466 1.1 yamaguch } else {
3467 1.1 yamaguch realloc_intrs = false;
3468 1.1 yamaguch }
3469 1.1 yamaguch
3470 1.1 yamaguch if (realloc_qps || realloc_intrs)
3471 1.1 yamaguch iavf_teardown_interrupts(sc);
3472 1.1 yamaguch
3473 1.1 yamaguch if (realloc_qps) {
3474 1.1 yamaguch iavf_queue_pairs_free(sc);
3475 1.1 yamaguch if (iavf_queue_pairs_alloc(sc) != 0) {
3476 1.1 yamaguch log(LOG_ERR, "%s: failed to allocate queue pairs\n",
3477 1.1 yamaguch ifp->if_xname);
3478 1.1 yamaguch goto failed;
3479 1.1 yamaguch }
3480 1.1 yamaguch }
3481 1.1 yamaguch
3482 1.1 yamaguch if (realloc_qps || realloc_intrs) {
3483 1.1 yamaguch if (iavf_setup_interrupts(sc) != 0) {
3484 1.1 yamaguch sc->sc_nintrs = 0;
3485 1.1 yamaguch log(LOG_ERR, "%s: failed to allocate interrupts\n",
3486 1.1 yamaguch ifp->if_xname);
3487 1.1 yamaguch goto failed;
3488 1.1 yamaguch }
3489 1.1 yamaguch log(LOG_INFO, "%s: reallocated queues\n", ifp->if_xname);
3490 1.1 yamaguch }
3491 1.1 yamaguch
3492 1.1 yamaguch if (iavf_config_irq_map(sc, aqb) != 0) {
3493 1.1 yamaguch log(LOG_ERR, "%s: timed out configuring IRQ map\n",
3494 1.1 yamaguch ifp->if_xname);
3495 1.1 yamaguch goto failed;
3496 1.1 yamaguch }
3497 1.1 yamaguch
3498 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
3499 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
3500 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
3501 1.1 yamaguch
3502 1.1 yamaguch iavf_reset_finish(sc);
3503 1.1 yamaguch
3504 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
3505 1.1 yamaguch return;
3506 1.1 yamaguch
3507 1.1 yamaguch failed:
3508 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
3509 1.1 yamaguch iavf_cleanup_admin_queue(sc);
3510 1.1 yamaguch if (aqb != NULL) {
3511 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
3512 1.1 yamaguch }
3513 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
3514 1.1 yamaguch callout_schedule(&sc->sc_tick, IAVF_TICK_INTERVAL);
3515 1.1 yamaguch mutex_exit(&sc->sc_cfg_lock);
3516 1.1 yamaguch }
3517 1.1 yamaguch
3518 1.1 yamaguch static void
3519 1.1 yamaguch iavf_reset_finish(struct iavf_softc *sc)
3520 1.1 yamaguch {
3521 1.1 yamaguch struct ethercom *ec = &sc->sc_ec;
3522 1.1 yamaguch struct ether_multi *enm;
3523 1.1 yamaguch struct ether_multistep step;
3524 1.1 yamaguch struct ifnet *ifp = &ec->ec_if;
3525 1.1 yamaguch struct vlanid_list *vlanidp;
3526 1.5 yamaguch uint8_t enaddr_prev[ETHER_ADDR_LEN], enaddr_next[ETHER_ADDR_LEN];
3527 1.1 yamaguch
3528 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_cfg_lock));
3529 1.1 yamaguch
3530 1.1 yamaguch callout_stop(&sc->sc_tick);
3531 1.1 yamaguch
3532 1.1 yamaguch iavf_intr_enable(sc);
3533 1.1 yamaguch
3534 1.1 yamaguch if (!iavf_is_etheranyaddr(sc->sc_enaddr_added)) {
3535 1.1 yamaguch iavf_eth_addr(sc, sc->sc_enaddr_added, IAVF_VC_OP_ADD_ETH_ADDR);
3536 1.1 yamaguch }
3537 1.1 yamaguch
3538 1.1 yamaguch ETHER_LOCK(ec);
3539 1.1 yamaguch if (!ISSET(ifp->if_flags, IFF_ALLMULTI)) {
3540 1.1 yamaguch for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
3541 1.1 yamaguch ETHER_NEXT_MULTI(step, enm)) {
3542 1.1 yamaguch iavf_add_multi(sc, enm->enm_addrlo, enm->enm_addrhi);
3543 1.1 yamaguch }
3544 1.1 yamaguch }
3545 1.1 yamaguch
3546 1.1 yamaguch SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
3547 1.1 yamaguch ETHER_UNLOCK(ec);
3548 1.1 yamaguch iavf_config_vlan_id(sc, vlanidp->vid, IAVF_VC_OP_ADD_VLAN);
3549 1.1 yamaguch ETHER_LOCK(ec);
3550 1.1 yamaguch }
3551 1.1 yamaguch ETHER_UNLOCK(ec);
3552 1.1 yamaguch
3553 1.1 yamaguch if (memcmp(sc->sc_enaddr, sc->sc_enaddr_reset, ETHER_ADDR_LEN) != 0) {
3554 1.5 yamaguch memcpy(enaddr_prev, sc->sc_enaddr_reset, sizeof(enaddr_prev));
3555 1.5 yamaguch memcpy(enaddr_next, sc->sc_enaddr, sizeof(enaddr_next));
3556 1.1 yamaguch log(LOG_INFO, "%s: Ethernet address changed to %s\n",
3557 1.5 yamaguch ifp->if_xname, ether_sprintf(enaddr_next));
3558 1.5 yamaguch
3559 1.5 yamaguch mutex_exit(&sc->sc_cfg_lock);
3560 1.1 yamaguch IFNET_LOCK(ifp);
3561 1.1 yamaguch kpreempt_disable();
3562 1.1 yamaguch /*XXX we need an API to change ethernet address. */
3563 1.5 yamaguch iavf_replace_lla(ifp, enaddr_prev, enaddr_next);
3564 1.1 yamaguch kpreempt_enable();
3565 1.1 yamaguch IFNET_UNLOCK(ifp);
3566 1.5 yamaguch mutex_enter(&sc->sc_cfg_lock);
3567 1.1 yamaguch }
3568 1.1 yamaguch
3569 1.1 yamaguch sc->sc_resetting = false;
3570 1.1 yamaguch
3571 1.1 yamaguch if (sc->sc_reset_up) {
3572 1.1 yamaguch iavf_init_locked(sc);
3573 1.1 yamaguch }
3574 1.1 yamaguch
3575 1.1 yamaguch if (sc->sc_link_state != LINK_STATE_DOWN) {
3576 1.1 yamaguch if_link_state_change(ifp, sc->sc_link_state);
3577 1.1 yamaguch }
3578 1.1 yamaguch
3579 1.1 yamaguch }
3580 1.1 yamaguch
3581 1.1 yamaguch static int
3582 1.1 yamaguch iavf_dmamem_alloc(bus_dma_tag_t dmat, struct ixl_dmamem *ixm,
3583 1.1 yamaguch bus_size_t size, bus_size_t align)
3584 1.1 yamaguch {
3585 1.1 yamaguch ixm->ixm_size = size;
3586 1.1 yamaguch
3587 1.1 yamaguch if (bus_dmamap_create(dmat, ixm->ixm_size, 1,
3588 1.1 yamaguch ixm->ixm_size, 0,
3589 1.1 yamaguch BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
3590 1.1 yamaguch &ixm->ixm_map) != 0)
3591 1.1 yamaguch return 1;
3592 1.1 yamaguch if (bus_dmamem_alloc(dmat, ixm->ixm_size,
3593 1.1 yamaguch align, 0, &ixm->ixm_seg, 1, &ixm->ixm_nsegs,
3594 1.1 yamaguch BUS_DMA_WAITOK) != 0)
3595 1.1 yamaguch goto destroy;
3596 1.1 yamaguch if (bus_dmamem_map(dmat, &ixm->ixm_seg, ixm->ixm_nsegs,
3597 1.1 yamaguch ixm->ixm_size, &ixm->ixm_kva, BUS_DMA_WAITOK) != 0)
3598 1.1 yamaguch goto free;
3599 1.1 yamaguch if (bus_dmamap_load(dmat, ixm->ixm_map, ixm->ixm_kva,
3600 1.1 yamaguch ixm->ixm_size, NULL, BUS_DMA_WAITOK) != 0)
3601 1.1 yamaguch goto unmap;
3602 1.1 yamaguch
3603 1.1 yamaguch memset(ixm->ixm_kva, 0, ixm->ixm_size);
3604 1.1 yamaguch
3605 1.1 yamaguch return 0;
3606 1.1 yamaguch unmap:
3607 1.1 yamaguch bus_dmamem_unmap(dmat, ixm->ixm_kva, ixm->ixm_size);
3608 1.1 yamaguch free:
3609 1.1 yamaguch bus_dmamem_free(dmat, &ixm->ixm_seg, 1);
3610 1.1 yamaguch destroy:
3611 1.1 yamaguch bus_dmamap_destroy(dmat, ixm->ixm_map);
3612 1.1 yamaguch return 1;
3613 1.1 yamaguch }
3614 1.1 yamaguch
3615 1.1 yamaguch static void
3616 1.1 yamaguch iavf_dmamem_free(bus_dma_tag_t dmat, struct ixl_dmamem *ixm)
3617 1.1 yamaguch {
3618 1.1 yamaguch
3619 1.1 yamaguch bus_dmamap_unload(dmat, ixm->ixm_map);
3620 1.1 yamaguch bus_dmamem_unmap(dmat, ixm->ixm_kva, ixm->ixm_size);
3621 1.1 yamaguch bus_dmamem_free(dmat, &ixm->ixm_seg, 1);
3622 1.1 yamaguch bus_dmamap_destroy(dmat, ixm->ixm_map);
3623 1.1 yamaguch }
3624 1.1 yamaguch
3625 1.1 yamaguch static struct ixl_aq_buf *
3626 1.1 yamaguch iavf_aqb_alloc(bus_dma_tag_t dmat, size_t buflen)
3627 1.1 yamaguch {
3628 1.1 yamaguch struct ixl_aq_buf *aqb;
3629 1.1 yamaguch
3630 1.1 yamaguch aqb = kmem_alloc(sizeof(*aqb), KM_NOSLEEP);
3631 1.1 yamaguch if (aqb == NULL)
3632 1.1 yamaguch return NULL;
3633 1.1 yamaguch
3634 1.1 yamaguch aqb->aqb_size = buflen;
3635 1.1 yamaguch
3636 1.1 yamaguch if (bus_dmamap_create(dmat, aqb->aqb_size, 1,
3637 1.1 yamaguch aqb->aqb_size, 0,
3638 1.1 yamaguch BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &aqb->aqb_map) != 0)
3639 1.1 yamaguch goto free;
3640 1.1 yamaguch if (bus_dmamem_alloc(dmat, aqb->aqb_size,
3641 1.1 yamaguch IAVF_AQ_ALIGN, 0, &aqb->aqb_seg, 1, &aqb->aqb_nsegs,
3642 1.1 yamaguch BUS_DMA_WAITOK) != 0)
3643 1.1 yamaguch goto destroy;
3644 1.1 yamaguch if (bus_dmamem_map(dmat, &aqb->aqb_seg, aqb->aqb_nsegs,
3645 1.1 yamaguch aqb->aqb_size, &aqb->aqb_data, BUS_DMA_WAITOK) != 0)
3646 1.1 yamaguch goto dma_free;
3647 1.1 yamaguch if (bus_dmamap_load(dmat, aqb->aqb_map, aqb->aqb_data,
3648 1.1 yamaguch aqb->aqb_size, NULL, BUS_DMA_WAITOK) != 0)
3649 1.1 yamaguch goto unmap;
3650 1.1 yamaguch
3651 1.1 yamaguch return aqb;
3652 1.1 yamaguch unmap:
3653 1.1 yamaguch bus_dmamem_unmap(dmat, aqb->aqb_data, aqb->aqb_size);
3654 1.1 yamaguch dma_free:
3655 1.1 yamaguch bus_dmamem_free(dmat, &aqb->aqb_seg, 1);
3656 1.1 yamaguch destroy:
3657 1.1 yamaguch bus_dmamap_destroy(dmat, aqb->aqb_map);
3658 1.1 yamaguch free:
3659 1.1 yamaguch kmem_free(aqb, sizeof(*aqb));
3660 1.1 yamaguch
3661 1.1 yamaguch return NULL;
3662 1.1 yamaguch }
3663 1.1 yamaguch
3664 1.1 yamaguch static void
3665 1.1 yamaguch iavf_aqb_free(bus_dma_tag_t dmat, struct ixl_aq_buf *aqb)
3666 1.1 yamaguch {
3667 1.1 yamaguch
3668 1.1 yamaguch bus_dmamap_unload(dmat, aqb->aqb_map);
3669 1.1 yamaguch bus_dmamem_unmap(dmat, aqb->aqb_data, aqb->aqb_size);
3670 1.1 yamaguch bus_dmamem_free(dmat, &aqb->aqb_seg, 1);
3671 1.1 yamaguch bus_dmamap_destroy(dmat, aqb->aqb_map);
3672 1.1 yamaguch kmem_free(aqb, sizeof(*aqb));
3673 1.1 yamaguch }
3674 1.1 yamaguch
3675 1.1 yamaguch static struct ixl_aq_buf *
3676 1.1 yamaguch iavf_aqb_get_locked(struct ixl_aq_bufs *q)
3677 1.1 yamaguch {
3678 1.1 yamaguch struct ixl_aq_buf *aqb;
3679 1.1 yamaguch
3680 1.1 yamaguch aqb = SIMPLEQ_FIRST(q);
3681 1.1 yamaguch if (aqb != NULL) {
3682 1.1 yamaguch SIMPLEQ_REMOVE(q, aqb, ixl_aq_buf, aqb_entry);
3683 1.1 yamaguch }
3684 1.1 yamaguch
3685 1.1 yamaguch return aqb;
3686 1.1 yamaguch }
3687 1.1 yamaguch
3688 1.1 yamaguch static struct ixl_aq_buf *
3689 1.1 yamaguch iavf_aqb_get(struct iavf_softc *sc, struct ixl_aq_bufs *q)
3690 1.1 yamaguch {
3691 1.1 yamaguch struct ixl_aq_buf *aqb;
3692 1.1 yamaguch
3693 1.1 yamaguch if (q != NULL) {
3694 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
3695 1.1 yamaguch aqb = iavf_aqb_get_locked(q);
3696 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
3697 1.1 yamaguch } else {
3698 1.1 yamaguch aqb = NULL;
3699 1.1 yamaguch }
3700 1.1 yamaguch
3701 1.1 yamaguch if (aqb == NULL) {
3702 1.1 yamaguch aqb = iavf_aqb_alloc(sc->sc_dmat, IAVF_AQ_BUFLEN);
3703 1.1 yamaguch }
3704 1.1 yamaguch
3705 1.1 yamaguch return aqb;
3706 1.1 yamaguch }
3707 1.1 yamaguch
3708 1.1 yamaguch static void
3709 1.1 yamaguch iavf_aqb_put_locked(struct ixl_aq_bufs *q, struct ixl_aq_buf *aqb)
3710 1.1 yamaguch {
3711 1.1 yamaguch
3712 1.1 yamaguch SIMPLEQ_INSERT_TAIL(q, aqb, aqb_entry);
3713 1.1 yamaguch }
3714 1.1 yamaguch
3715 1.1 yamaguch static void
3716 1.1 yamaguch iavf_aqb_clean(struct ixl_aq_bufs *q, bus_dma_tag_t dmat)
3717 1.1 yamaguch {
3718 1.1 yamaguch struct ixl_aq_buf *aqb;
3719 1.1 yamaguch
3720 1.1 yamaguch while ((aqb = SIMPLEQ_FIRST(q)) != NULL) {
3721 1.1 yamaguch SIMPLEQ_REMOVE(q, aqb, ixl_aq_buf, aqb_entry);
3722 1.1 yamaguch iavf_aqb_free(dmat, aqb);
3723 1.1 yamaguch }
3724 1.1 yamaguch }
3725 1.1 yamaguch
3726 1.1 yamaguch static const char *
3727 1.1 yamaguch iavf_aq_vc_opcode_str(const struct ixl_aq_desc *iaq)
3728 1.1 yamaguch {
3729 1.1 yamaguch
3730 1.1 yamaguch switch (iavf_aq_vc_get_opcode(iaq)) {
3731 1.1 yamaguch case IAVF_VC_OP_VERSION:
3732 1.1 yamaguch return "GET_VERSION";
3733 1.1 yamaguch case IAVF_VC_OP_RESET_VF:
3734 1.1 yamaguch return "RESET_VF";
3735 1.1 yamaguch case IAVF_VC_OP_GET_VF_RESOURCES:
3736 1.1 yamaguch return "GET_VF_RESOURCES";
3737 1.1 yamaguch case IAVF_VC_OP_CONFIG_TX_QUEUE:
3738 1.1 yamaguch return "CONFIG_TX_QUEUE";
3739 1.1 yamaguch case IAVF_VC_OP_CONFIG_RX_QUEUE:
3740 1.1 yamaguch return "CONFIG_RX_QUEUE";
3741 1.1 yamaguch case IAVF_VC_OP_CONFIG_VSI_QUEUES:
3742 1.1 yamaguch return "CONFIG_VSI_QUEUES";
3743 1.1 yamaguch case IAVF_VC_OP_CONFIG_IRQ_MAP:
3744 1.1 yamaguch return "CONFIG_IRQ_MAP";
3745 1.1 yamaguch case IAVF_VC_OP_ENABLE_QUEUES:
3746 1.1 yamaguch return "ENABLE_QUEUES";
3747 1.1 yamaguch case IAVF_VC_OP_DISABLE_QUEUES:
3748 1.1 yamaguch return "DISABLE_QUEUES";
3749 1.1 yamaguch case IAVF_VC_OP_ADD_ETH_ADDR:
3750 1.1 yamaguch return "ADD_ETH_ADDR";
3751 1.1 yamaguch case IAVF_VC_OP_DEL_ETH_ADDR:
3752 1.1 yamaguch return "DEL_ETH_ADDR";
3753 1.1 yamaguch case IAVF_VC_OP_CONFIG_PROMISC:
3754 1.1 yamaguch return "CONFIG_PROMISC";
3755 1.1 yamaguch case IAVF_VC_OP_GET_STATS:
3756 1.1 yamaguch return "GET_STATS";
3757 1.1 yamaguch case IAVF_VC_OP_EVENT:
3758 1.1 yamaguch return "EVENT";
3759 1.1 yamaguch case IAVF_VC_OP_CONFIG_RSS_KEY:
3760 1.1 yamaguch return "CONFIG_RSS_KEY";
3761 1.1 yamaguch case IAVF_VC_OP_GET_RSS_HENA_CAPS:
3762 1.1 yamaguch return "GET_RS_HENA_CAPS";
3763 1.1 yamaguch case IAVF_VC_OP_SET_RSS_HENA:
3764 1.1 yamaguch return "SET_RSS_HENA";
3765 1.1 yamaguch case IAVF_VC_OP_ENABLE_VLAN_STRIP:
3766 1.1 yamaguch return "ENABLE_VLAN_STRIPPING";
3767 1.1 yamaguch case IAVF_VC_OP_DISABLE_VLAN_STRIP:
3768 1.1 yamaguch return "DISABLE_VLAN_STRIPPING";
3769 1.1 yamaguch case IAVF_VC_OP_REQUEST_QUEUES:
3770 1.1 yamaguch return "REQUEST_QUEUES";
3771 1.1 yamaguch }
3772 1.1 yamaguch
3773 1.1 yamaguch return "unknown";
3774 1.1 yamaguch }
3775 1.1 yamaguch
3776 1.1 yamaguch static void
3777 1.1 yamaguch iavf_aq_dump(const struct iavf_softc *sc, const struct ixl_aq_desc *iaq,
3778 1.1 yamaguch const char *msg)
3779 1.1 yamaguch {
3780 1.1 yamaguch char buf[512];
3781 1.1 yamaguch size_t len;
3782 1.1 yamaguch
3783 1.1 yamaguch len = sizeof(buf);
3784 1.1 yamaguch buf[--len] = '\0';
3785 1.1 yamaguch
3786 1.1 yamaguch device_printf(sc->sc_dev, "%s\n", msg);
3787 1.1 yamaguch snprintb(buf, len, IXL_AQ_FLAGS_FMT, le16toh(iaq->iaq_flags));
3788 1.1 yamaguch device_printf(sc->sc_dev, "flags %s opcode %04x\n",
3789 1.1 yamaguch buf, le16toh(iaq->iaq_opcode));
3790 1.1 yamaguch device_printf(sc->sc_dev, "datalen %u retval %u\n",
3791 1.1 yamaguch le16toh(iaq->iaq_datalen), le16toh(iaq->iaq_retval));
3792 1.1 yamaguch device_printf(sc->sc_dev, "vc-opcode %u (%s)\n",
3793 1.1 yamaguch iavf_aq_vc_get_opcode(iaq),
3794 1.1 yamaguch iavf_aq_vc_opcode_str(iaq));
3795 1.1 yamaguch device_printf(sc->sc_dev, "vc-retval %u\n",
3796 1.1 yamaguch iavf_aq_vc_get_retval(iaq));
3797 1.1 yamaguch device_printf(sc->sc_dev, "cookie %016" PRIx64 "\n", iaq->iaq_cookie);
3798 1.1 yamaguch device_printf(sc->sc_dev, "%08x %08x %08x %08x\n",
3799 1.1 yamaguch le32toh(iaq->iaq_param[0]), le32toh(iaq->iaq_param[1]),
3800 1.1 yamaguch le32toh(iaq->iaq_param[2]), le32toh(iaq->iaq_param[3]));
3801 1.1 yamaguch }
3802 1.1 yamaguch
3803 1.1 yamaguch static int
3804 1.1 yamaguch iavf_arq_fill(struct iavf_softc *sc)
3805 1.1 yamaguch {
3806 1.1 yamaguch struct ixl_aq_buf *aqb;
3807 1.1 yamaguch struct ixl_aq_desc *arq, *iaq;
3808 1.1 yamaguch unsigned int prod = sc->sc_arq_prod;
3809 1.1 yamaguch unsigned int n;
3810 1.1 yamaguch int filled;
3811 1.1 yamaguch
3812 1.1 yamaguch n = ixl_rxr_unrefreshed(sc->sc_arq_prod, sc->sc_arq_cons,
3813 1.1 yamaguch IAVF_AQ_NUM);
3814 1.1 yamaguch
3815 1.1 yamaguch if (__predict_false(n <= 0))
3816 1.1 yamaguch return 0;
3817 1.1 yamaguch
3818 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
3819 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_arq),
3820 1.1 yamaguch BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3821 1.1 yamaguch
3822 1.1 yamaguch arq = IXL_DMA_KVA(&sc->sc_arq);
3823 1.1 yamaguch
3824 1.1 yamaguch do {
3825 1.1 yamaguch iaq = &arq[prod];
3826 1.1 yamaguch
3827 1.1 yamaguch if (ixl_aq_has_dva(iaq)) {
3828 1.1 yamaguch /* already filled */
3829 1.1 yamaguch break;
3830 1.1 yamaguch }
3831 1.1 yamaguch
3832 1.1 yamaguch aqb = iavf_aqb_get_locked(&sc->sc_arq_idle);
3833 1.1 yamaguch if (aqb == NULL)
3834 1.1 yamaguch break;
3835 1.1 yamaguch
3836 1.1 yamaguch memset(aqb->aqb_data, 0, aqb->aqb_size);
3837 1.1 yamaguch
3838 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0,
3839 1.1 yamaguch aqb->aqb_size, BUS_DMASYNC_PREREAD);
3840 1.1 yamaguch
3841 1.1 yamaguch iaq->iaq_flags = htole16(IXL_AQ_BUF |
3842 1.1 yamaguch (aqb->aqb_size > I40E_AQ_LARGE_BUF ?
3843 1.1 yamaguch IXL_AQ_LB : 0));
3844 1.1 yamaguch iaq->iaq_opcode = 0;
3845 1.1 yamaguch iaq->iaq_datalen = htole16(aqb->aqb_size);
3846 1.1 yamaguch iaq->iaq_retval = 0;
3847 1.1 yamaguch iaq->iaq_cookie = 0;
3848 1.1 yamaguch iaq->iaq_param[0] = 0;
3849 1.1 yamaguch iaq->iaq_param[1] = 0;
3850 1.1 yamaguch ixl_aq_dva(iaq, IXL_AQB_DVA(aqb));
3851 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_arq_live, aqb);
3852 1.1 yamaguch
3853 1.1 yamaguch prod++;
3854 1.1 yamaguch prod &= IAVF_AQ_MASK;
3855 1.1 yamaguch filled = 1;
3856 1.1 yamaguch } while (--n);
3857 1.1 yamaguch
3858 1.1 yamaguch sc->sc_arq_prod = prod;
3859 1.1 yamaguch
3860 1.1 yamaguch if (filled) {
3861 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
3862 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_arq),
3863 1.1 yamaguch BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3864 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->arq_tail, sc->sc_arq_prod);
3865 1.1 yamaguch }
3866 1.1 yamaguch
3867 1.1 yamaguch return filled;
3868 1.1 yamaguch }
3869 1.1 yamaguch
3870 1.1 yamaguch static int
3871 1.1 yamaguch iavf_arq_wait(struct iavf_softc *sc, uint32_t opcode)
3872 1.1 yamaguch {
3873 1.1 yamaguch int error;
3874 1.1 yamaguch
3875 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_adminq_lock));
3876 1.1 yamaguch
3877 1.1 yamaguch while ((error = cv_timedwait(&sc->sc_adminq_cv,
3878 1.1 yamaguch &sc->sc_adminq_lock, mstohz(IAVF_EXEC_TIMEOUT))) == 0) {
3879 1.1 yamaguch if (opcode == sc->sc_arq_opcode)
3880 1.1 yamaguch break;
3881 1.1 yamaguch }
3882 1.1 yamaguch
3883 1.1 yamaguch if (error != 0 &&
3884 1.1 yamaguch atomic_load_relaxed(&sc->sc_debuglevel) >= 2)
3885 1.1 yamaguch device_printf(sc->sc_dev, "cv_timedwait error=%d\n", error);
3886 1.1 yamaguch
3887 1.1 yamaguch return error;
3888 1.1 yamaguch }
3889 1.1 yamaguch
3890 1.1 yamaguch static void
3891 1.1 yamaguch iavf_arq_refill(void *xsc)
3892 1.1 yamaguch {
3893 1.1 yamaguch struct iavf_softc *sc = xsc;
3894 1.1 yamaguch struct ixl_aq_bufs aqbs;
3895 1.1 yamaguch struct ixl_aq_buf *aqb;
3896 1.1 yamaguch unsigned int n, i;
3897 1.1 yamaguch
3898 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
3899 1.1 yamaguch iavf_arq_fill(sc);
3900 1.1 yamaguch n = ixl_rxr_unrefreshed(sc->sc_arq_prod, sc->sc_arq_cons,
3901 1.1 yamaguch IAVF_AQ_NUM);
3902 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
3903 1.1 yamaguch
3904 1.1 yamaguch if (n == 0)
3905 1.1 yamaguch return;
3906 1.1 yamaguch
3907 1.1 yamaguch if (atomic_load_relaxed(&sc->sc_debuglevel) >= 1)
3908 1.1 yamaguch device_printf(sc->sc_dev, "Allocate %d bufs for arq\n", n);
3909 1.1 yamaguch
3910 1.1 yamaguch SIMPLEQ_INIT(&aqbs);
3911 1.1 yamaguch for (i = 0; i < n; i++) {
3912 1.1 yamaguch aqb = iavf_aqb_get(sc, NULL);
3913 1.1 yamaguch if (aqb == NULL)
3914 1.1 yamaguch continue;
3915 1.1 yamaguch SIMPLEQ_INSERT_TAIL(&aqbs, aqb, aqb_entry);
3916 1.1 yamaguch }
3917 1.1 yamaguch
3918 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
3919 1.1 yamaguch while ((aqb = SIMPLEQ_FIRST(&aqbs)) != NULL) {
3920 1.1 yamaguch SIMPLEQ_REMOVE(&aqbs, aqb, ixl_aq_buf, aqb_entry);
3921 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_arq_idle, aqb);
3922 1.1 yamaguch }
3923 1.1 yamaguch iavf_arq_fill(sc);
3924 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
3925 1.1 yamaguch }
3926 1.1 yamaguch
3927 1.1 yamaguch static uint32_t
3928 1.1 yamaguch iavf_process_arq(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
3929 1.1 yamaguch struct ixl_aq_buf *aqb)
3930 1.1 yamaguch {
3931 1.1 yamaguch uint32_t vc_retval, vc_opcode;
3932 1.1 yamaguch int dbg;
3933 1.1 yamaguch
3934 1.1 yamaguch dbg = atomic_load_relaxed(&sc->sc_debuglevel);
3935 1.1 yamaguch if (dbg >= 3)
3936 1.1 yamaguch iavf_aq_dump(sc, iaq, "arq proc");
3937 1.1 yamaguch
3938 1.1 yamaguch if (dbg >= 2) {
3939 1.1 yamaguch vc_retval = iavf_aq_vc_get_retval(iaq);
3940 1.1 yamaguch if (vc_retval != IAVF_VC_RC_SUCCESS) {
3941 1.1 yamaguch device_printf(sc->sc_dev, "%s failed=%d(arq)\n",
3942 1.1 yamaguch iavf_aq_vc_opcode_str(iaq), vc_retval);
3943 1.1 yamaguch }
3944 1.1 yamaguch }
3945 1.1 yamaguch
3946 1.1 yamaguch vc_opcode = iavf_aq_vc_get_opcode(iaq);
3947 1.1 yamaguch switch (vc_opcode) {
3948 1.1 yamaguch case IAVF_VC_OP_VERSION:
3949 1.1 yamaguch iavf_process_version(sc, iaq, aqb);
3950 1.1 yamaguch break;
3951 1.1 yamaguch case IAVF_VC_OP_GET_VF_RESOURCES:
3952 1.1 yamaguch iavf_process_vf_resources(sc, iaq, aqb);
3953 1.1 yamaguch break;
3954 1.1 yamaguch case IAVF_VC_OP_CONFIG_IRQ_MAP:
3955 1.1 yamaguch iavf_process_irq_map(sc, iaq);
3956 1.1 yamaguch break;
3957 1.1 yamaguch case IAVF_VC_OP_EVENT:
3958 1.1 yamaguch iavf_process_vc_event(sc, iaq, aqb);
3959 1.1 yamaguch break;
3960 1.1 yamaguch case IAVF_VC_OP_GET_STATS:
3961 1.1 yamaguch iavf_process_stats(sc, iaq, aqb);
3962 1.1 yamaguch break;
3963 1.1 yamaguch case IAVF_VC_OP_REQUEST_QUEUES:
3964 1.1 yamaguch iavf_process_req_queues(sc, iaq, aqb);
3965 1.1 yamaguch break;
3966 1.1 yamaguch }
3967 1.1 yamaguch
3968 1.1 yamaguch return vc_opcode;
3969 1.1 yamaguch }
3970 1.1 yamaguch
3971 1.1 yamaguch static int
3972 1.1 yamaguch iavf_arq_poll(struct iavf_softc *sc, uint32_t wait_opcode, int retry)
3973 1.1 yamaguch {
3974 1.1 yamaguch struct ixl_aq_desc *arq, *iaq;
3975 1.1 yamaguch struct ixl_aq_buf *aqb;
3976 1.1 yamaguch unsigned int cons = sc->sc_arq_cons;
3977 1.1 yamaguch unsigned int prod;
3978 1.1 yamaguch uint32_t vc_opcode;
3979 1.1 yamaguch bool received;
3980 1.1 yamaguch int i;
3981 1.1 yamaguch
3982 1.1 yamaguch for (i = 0, received = false; i < retry && !received; i++) {
3983 1.1 yamaguch prod = iavf_rd(sc, sc->sc_aq_regs->arq_head);
3984 1.1 yamaguch prod &= sc->sc_aq_regs->arq_head_mask;
3985 1.1 yamaguch
3986 1.1 yamaguch if (prod == cons) {
3987 1.1 yamaguch delaymsec(1);
3988 1.1 yamaguch continue;
3989 1.1 yamaguch }
3990 1.1 yamaguch
3991 1.1 yamaguch if (prod >= IAVF_AQ_NUM) {
3992 1.1 yamaguch return EIO;
3993 1.1 yamaguch }
3994 1.1 yamaguch
3995 1.1 yamaguch arq = IXL_DMA_KVA(&sc->sc_arq);
3996 1.1 yamaguch
3997 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
3998 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_arq),
3999 1.1 yamaguch BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
4000 1.1 yamaguch
4001 1.1 yamaguch do {
4002 1.1 yamaguch iaq = &arq[cons];
4003 1.1 yamaguch aqb = iavf_aqb_get_locked(&sc->sc_arq_live);
4004 1.1 yamaguch KASSERT(aqb != NULL);
4005 1.1 yamaguch
4006 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0,
4007 1.1 yamaguch IAVF_AQ_BUFLEN, BUS_DMASYNC_POSTREAD);
4008 1.1 yamaguch
4009 1.1 yamaguch vc_opcode = iavf_process_arq(sc, iaq, aqb);
4010 1.1 yamaguch
4011 1.1 yamaguch if (vc_opcode == wait_opcode)
4012 1.1 yamaguch received = true;
4013 1.1 yamaguch
4014 1.1 yamaguch memset(iaq, 0, sizeof(*iaq));
4015 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_arq_idle, aqb);
4016 1.1 yamaguch
4017 1.1 yamaguch cons++;
4018 1.1 yamaguch cons &= IAVF_AQ_MASK;
4019 1.1 yamaguch
4020 1.1 yamaguch } while (cons != prod);
4021 1.1 yamaguch
4022 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
4023 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_arq),
4024 1.1 yamaguch BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
4025 1.1 yamaguch
4026 1.1 yamaguch sc->sc_arq_cons = cons;
4027 1.1 yamaguch iavf_arq_fill(sc);
4028 1.1 yamaguch
4029 1.1 yamaguch }
4030 1.1 yamaguch
4031 1.1 yamaguch if (!received)
4032 1.1 yamaguch return ETIMEDOUT;
4033 1.1 yamaguch
4034 1.1 yamaguch return 0;
4035 1.1 yamaguch }
4036 1.1 yamaguch
4037 1.1 yamaguch static int
4038 1.1 yamaguch iavf_arq(struct iavf_softc *sc)
4039 1.1 yamaguch {
4040 1.1 yamaguch struct ixl_aq_desc *arq, *iaq;
4041 1.1 yamaguch struct ixl_aq_buf *aqb;
4042 1.1 yamaguch unsigned int cons = sc->sc_arq_cons;
4043 1.1 yamaguch unsigned int prod;
4044 1.1 yamaguch uint32_t vc_opcode;
4045 1.1 yamaguch
4046 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_adminq_lock));
4047 1.1 yamaguch
4048 1.1 yamaguch prod = iavf_rd(sc, sc->sc_aq_regs->arq_head);
4049 1.1 yamaguch prod &= sc->sc_aq_regs->arq_head_mask;
4050 1.1 yamaguch
4051 1.1 yamaguch /* broken value at resetting */
4052 1.1 yamaguch if (prod >= IAVF_AQ_NUM) {
4053 1.1 yamaguch iavf_work_set(&sc->sc_reset_task, iavf_reset_start, sc);
4054 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_reset_task);
4055 1.1 yamaguch return 0;
4056 1.1 yamaguch }
4057 1.1 yamaguch
4058 1.1 yamaguch if (cons == prod)
4059 1.1 yamaguch return 0;
4060 1.1 yamaguch
4061 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
4062 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_arq),
4063 1.1 yamaguch BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
4064 1.1 yamaguch
4065 1.1 yamaguch arq = IXL_DMA_KVA(&sc->sc_arq);
4066 1.1 yamaguch
4067 1.1 yamaguch do {
4068 1.1 yamaguch iaq = &arq[cons];
4069 1.1 yamaguch aqb = iavf_aqb_get_locked(&sc->sc_arq_live);
4070 1.1 yamaguch
4071 1.1 yamaguch KASSERT(aqb != NULL);
4072 1.1 yamaguch
4073 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0, IAVF_AQ_BUFLEN,
4074 1.1 yamaguch BUS_DMASYNC_POSTREAD);
4075 1.1 yamaguch
4076 1.1 yamaguch vc_opcode = iavf_process_arq(sc, iaq, aqb);
4077 1.1 yamaguch
4078 1.1 yamaguch switch (vc_opcode) {
4079 1.1 yamaguch case IAVF_VC_OP_CONFIG_TX_QUEUE:
4080 1.1 yamaguch case IAVF_VC_OP_CONFIG_RX_QUEUE:
4081 1.1 yamaguch case IAVF_VC_OP_CONFIG_VSI_QUEUES:
4082 1.1 yamaguch case IAVF_VC_OP_ENABLE_QUEUES:
4083 1.1 yamaguch case IAVF_VC_OP_DISABLE_QUEUES:
4084 1.1 yamaguch case IAVF_VC_OP_GET_RSS_HENA_CAPS:
4085 1.1 yamaguch case IAVF_VC_OP_SET_RSS_HENA:
4086 1.1 yamaguch case IAVF_VC_OP_ADD_ETH_ADDR:
4087 1.1 yamaguch case IAVF_VC_OP_DEL_ETH_ADDR:
4088 1.1 yamaguch case IAVF_VC_OP_CONFIG_PROMISC:
4089 1.1 yamaguch case IAVF_VC_OP_ADD_VLAN:
4090 1.1 yamaguch case IAVF_VC_OP_DEL_VLAN:
4091 1.1 yamaguch case IAVF_VC_OP_ENABLE_VLAN_STRIP:
4092 1.1 yamaguch case IAVF_VC_OP_DISABLE_VLAN_STRIP:
4093 1.1 yamaguch case IAVF_VC_OP_CONFIG_RSS_KEY:
4094 1.1 yamaguch case IAVF_VC_OP_CONFIG_RSS_LUT:
4095 1.1 yamaguch sc->sc_arq_retval = iavf_aq_vc_get_retval(iaq);
4096 1.1 yamaguch sc->sc_arq_opcode = vc_opcode;
4097 1.1 yamaguch cv_signal(&sc->sc_adminq_cv);
4098 1.1 yamaguch break;
4099 1.1 yamaguch }
4100 1.1 yamaguch
4101 1.1 yamaguch memset(iaq, 0, sizeof(*iaq));
4102 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_arq_idle, aqb);
4103 1.1 yamaguch
4104 1.1 yamaguch cons++;
4105 1.1 yamaguch cons &= IAVF_AQ_MASK;
4106 1.1 yamaguch } while (cons != prod);
4107 1.1 yamaguch
4108 1.1 yamaguch sc->sc_arq_cons = cons;
4109 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_arq_refill);
4110 1.1 yamaguch
4111 1.1 yamaguch return 1;
4112 1.1 yamaguch }
4113 1.1 yamaguch
4114 1.1 yamaguch static int
4115 1.1 yamaguch iavf_atq_post(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4116 1.1 yamaguch struct ixl_aq_buf *aqb)
4117 1.1 yamaguch {
4118 1.1 yamaguch struct ixl_aq_desc *atq, *slot;
4119 1.1 yamaguch unsigned int prod;
4120 1.1 yamaguch
4121 1.1 yamaguch atq = IXL_DMA_KVA(&sc->sc_atq);
4122 1.1 yamaguch prod = sc->sc_atq_prod;
4123 1.1 yamaguch slot = &atq[prod];
4124 1.1 yamaguch
4125 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
4126 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTWRITE);
4127 1.1 yamaguch
4128 1.1 yamaguch *slot = *iaq;
4129 1.1 yamaguch slot->iaq_flags |= htole16(IXL_AQ_SI);
4130 1.1 yamaguch if (aqb != NULL) {
4131 1.1 yamaguch ixl_aq_dva(slot, IXL_AQB_DVA(aqb));
4132 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_AQB_MAP(aqb),
4133 1.1 yamaguch 0, IXL_AQB_LEN(aqb), BUS_DMASYNC_PREWRITE);
4134 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_live, aqb);
4135 1.1 yamaguch } else {
4136 1.1 yamaguch ixl_aq_dva(slot, (bus_addr_t)0);
4137 1.1 yamaguch }
4138 1.1 yamaguch
4139 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
4140 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREWRITE);
4141 1.1 yamaguch
4142 1.1 yamaguch if (atomic_load_relaxed(&sc->sc_debuglevel) >= 3)
4143 1.1 yamaguch iavf_aq_dump(sc, slot, "post");
4144 1.1 yamaguch
4145 1.1 yamaguch prod++;
4146 1.1 yamaguch prod &= IAVF_AQ_MASK;
4147 1.1 yamaguch sc->sc_atq_prod = prod;
4148 1.1 yamaguch iavf_wr(sc, sc->sc_aq_regs->atq_tail, prod);
4149 1.1 yamaguch return prod;
4150 1.1 yamaguch }
4151 1.1 yamaguch
4152 1.1 yamaguch static int
4153 1.1 yamaguch iavf_atq_poll(struct iavf_softc *sc, unsigned int tm)
4154 1.1 yamaguch {
4155 1.1 yamaguch struct ixl_aq_desc *atq, *slot;
4156 1.1 yamaguch struct ixl_aq_desc iaq;
4157 1.1 yamaguch struct ixl_aq_buf *aqb;
4158 1.1 yamaguch unsigned int prod;
4159 1.1 yamaguch unsigned int t;
4160 1.1 yamaguch int dbg;
4161 1.1 yamaguch
4162 1.1 yamaguch dbg = atomic_load_relaxed(&sc->sc_debuglevel);
4163 1.1 yamaguch atq = IXL_DMA_KVA(&sc->sc_atq);
4164 1.1 yamaguch prod = sc->sc_atq_prod;
4165 1.1 yamaguch slot = &atq[prod];
4166 1.1 yamaguch t = 0;
4167 1.1 yamaguch
4168 1.1 yamaguch while (iavf_rd(sc, sc->sc_aq_regs->atq_head) != prod) {
4169 1.1 yamaguch delaymsec(1);
4170 1.1 yamaguch
4171 1.1 yamaguch if (t++ > tm) {
4172 1.1 yamaguch if (dbg >= 2) {
4173 1.1 yamaguch device_printf(sc->sc_dev,
4174 1.1 yamaguch "atq timedout\n");
4175 1.1 yamaguch }
4176 1.1 yamaguch return ETIMEDOUT;
4177 1.1 yamaguch }
4178 1.1 yamaguch }
4179 1.1 yamaguch
4180 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
4181 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTREAD);
4182 1.1 yamaguch iaq = *slot;
4183 1.1 yamaguch memset(slot, 0, sizeof(*slot));
4184 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
4185 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREREAD);
4186 1.1 yamaguch
4187 1.1 yamaguch aqb = iavf_aqb_get_locked(&sc->sc_atq_live);
4188 1.1 yamaguch if (aqb != NULL) {
4189 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_AQB_MAP(aqb),
4190 1.1 yamaguch 0, IXL_AQB_LEN(aqb), BUS_DMASYNC_POSTWRITE);
4191 1.1 yamaguch /* no need to do iavf_aqb_put(&sc->sc_atq_idle, aqb) */
4192 1.1 yamaguch }
4193 1.1 yamaguch
4194 1.1 yamaguch if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
4195 1.1 yamaguch if (dbg >= 2) {
4196 1.1 yamaguch device_printf(sc->sc_dev,
4197 1.1 yamaguch "atq retcode=0x%04x\n", le16toh(iaq.iaq_retval));
4198 1.1 yamaguch }
4199 1.1 yamaguch return EIO;
4200 1.1 yamaguch }
4201 1.1 yamaguch
4202 1.1 yamaguch return 0;
4203 1.1 yamaguch }
4204 1.1 yamaguch
4205 1.1 yamaguch static void
4206 1.1 yamaguch iavf_atq_done(struct iavf_softc *sc)
4207 1.1 yamaguch {
4208 1.1 yamaguch struct ixl_aq_desc *atq, *slot;
4209 1.1 yamaguch struct ixl_aq_buf *aqb;
4210 1.1 yamaguch unsigned int cons;
4211 1.1 yamaguch unsigned int prod;
4212 1.1 yamaguch
4213 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_adminq_lock));
4214 1.1 yamaguch
4215 1.1 yamaguch prod = sc->sc_atq_prod;
4216 1.1 yamaguch cons = sc->sc_atq_cons;
4217 1.1 yamaguch
4218 1.1 yamaguch if (prod == cons)
4219 1.1 yamaguch return;
4220 1.1 yamaguch
4221 1.1 yamaguch atq = IXL_DMA_KVA(&sc->sc_atq);
4222 1.1 yamaguch
4223 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
4224 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq),
4225 1.1 yamaguch BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
4226 1.1 yamaguch
4227 1.1 yamaguch do {
4228 1.1 yamaguch slot = &atq[cons];
4229 1.1 yamaguch if (!ISSET(slot->iaq_flags, htole16(IXL_AQ_DD)))
4230 1.1 yamaguch break;
4231 1.1 yamaguch
4232 1.1 yamaguch if (ixl_aq_has_dva(slot) &&
4233 1.1 yamaguch (aqb = iavf_aqb_get_locked(&sc->sc_atq_live)) != NULL) {
4234 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_AQB_MAP(aqb),
4235 1.1 yamaguch 0, IXL_AQB_LEN(aqb), BUS_DMASYNC_POSTWRITE);
4236 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
4237 1.1 yamaguch }
4238 1.1 yamaguch
4239 1.1 yamaguch memset(slot, 0, sizeof(*slot));
4240 1.1 yamaguch
4241 1.1 yamaguch cons++;
4242 1.1 yamaguch cons &= IAVF_AQ_MASK;
4243 1.1 yamaguch } while (cons != prod);
4244 1.1 yamaguch
4245 1.1 yamaguch bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
4246 1.1 yamaguch 0, IXL_DMA_LEN(&sc->sc_atq),
4247 1.1 yamaguch BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
4248 1.1 yamaguch
4249 1.1 yamaguch sc->sc_atq_cons = cons;
4250 1.1 yamaguch }
4251 1.1 yamaguch
4252 1.1 yamaguch static int
4253 1.1 yamaguch iavf_adminq_poll(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4254 1.1 yamaguch struct ixl_aq_buf *aqb, int retry)
4255 1.1 yamaguch {
4256 1.1 yamaguch int error;
4257 1.1 yamaguch
4258 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4259 1.1 yamaguch error = iavf_adminq_poll_locked(sc, iaq, aqb, retry);
4260 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4261 1.1 yamaguch
4262 1.1 yamaguch return error;
4263 1.1 yamaguch }
4264 1.1 yamaguch
4265 1.1 yamaguch static int
4266 1.1 yamaguch iavf_adminq_poll_locked(struct iavf_softc *sc,
4267 1.1 yamaguch struct ixl_aq_desc *iaq, struct ixl_aq_buf *aqb, int retry)
4268 1.1 yamaguch {
4269 1.1 yamaguch uint32_t opcode;
4270 1.1 yamaguch int error;
4271 1.1 yamaguch
4272 1.1 yamaguch KASSERT(!sc->sc_attached || mutex_owned(&sc->sc_adminq_lock));
4273 1.1 yamaguch
4274 1.1 yamaguch opcode = iavf_aq_vc_get_opcode(iaq);
4275 1.1 yamaguch
4276 1.1 yamaguch iavf_atq_post(sc, iaq, aqb);
4277 1.1 yamaguch
4278 1.1 yamaguch error = iavf_atq_poll(sc, retry);
4279 1.1 yamaguch if (error)
4280 1.1 yamaguch return error;
4281 1.1 yamaguch
4282 1.1 yamaguch error = iavf_arq_poll(sc, opcode, retry);
4283 1.1 yamaguch
4284 1.1 yamaguch if (error != 0 &&
4285 1.1 yamaguch atomic_load_relaxed(&sc->sc_debuglevel) >= 1) {
4286 1.1 yamaguch device_printf(sc->sc_dev, "%s failed=%d(polling)\n",
4287 1.1 yamaguch iavf_aq_vc_opcode_str(iaq), error);
4288 1.1 yamaguch }
4289 1.1 yamaguch
4290 1.1 yamaguch return error;
4291 1.1 yamaguch }
4292 1.1 yamaguch
4293 1.1 yamaguch static int
4294 1.1 yamaguch iavf_adminq_exec(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4295 1.1 yamaguch struct ixl_aq_buf *aqb)
4296 1.1 yamaguch {
4297 1.1 yamaguch int error;
4298 1.1 yamaguch uint32_t opcode;
4299 1.1 yamaguch
4300 1.1 yamaguch opcode = iavf_aq_vc_get_opcode(iaq);
4301 1.1 yamaguch
4302 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4303 1.1 yamaguch iavf_atq_post(sc, iaq, aqb);
4304 1.1 yamaguch
4305 1.1 yamaguch error = iavf_arq_wait(sc, opcode);
4306 1.1 yamaguch if (error == 0) {
4307 1.1 yamaguch error = sc->sc_arq_retval;
4308 1.1 yamaguch if (error != IAVF_VC_RC_SUCCESS &&
4309 1.1 yamaguch atomic_load_relaxed(&sc->sc_debuglevel) >= 1) {
4310 1.1 yamaguch device_printf(sc->sc_dev, "%s failed=%d\n",
4311 1.1 yamaguch iavf_aq_vc_opcode_str(iaq), error);
4312 1.1 yamaguch }
4313 1.1 yamaguch }
4314 1.1 yamaguch
4315 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4316 1.1 yamaguch return error;
4317 1.1 yamaguch }
4318 1.1 yamaguch
4319 1.1 yamaguch static void
4320 1.1 yamaguch iavf_process_version(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4321 1.1 yamaguch struct ixl_aq_buf *aqb)
4322 1.1 yamaguch {
4323 1.1 yamaguch struct iavf_vc_version_info *ver;
4324 1.1 yamaguch
4325 1.1 yamaguch ver = (struct iavf_vc_version_info *)aqb->aqb_data;
4326 1.1 yamaguch sc->sc_major_ver = le32toh(ver->major);
4327 1.1 yamaguch sc->sc_minor_ver = le32toh(ver->minor);
4328 1.1 yamaguch }
4329 1.1 yamaguch
4330 1.1 yamaguch static void
4331 1.1 yamaguch iavf_process_vf_resources(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4332 1.1 yamaguch struct ixl_aq_buf *aqb)
4333 1.1 yamaguch {
4334 1.1 yamaguch struct iavf_vc_vf_resource *vf_res;
4335 1.1 yamaguch struct iavf_vc_vsi_resource *vsi_res;
4336 1.1 yamaguch uint8_t *enaddr;
4337 1.1 yamaguch int mtu, dbg;
4338 1.1 yamaguch char buf[512];
4339 1.1 yamaguch
4340 1.1 yamaguch dbg = atomic_load_relaxed(&sc->sc_debuglevel);
4341 1.1 yamaguch sc->sc_got_vf_resources = 1;
4342 1.1 yamaguch
4343 1.1 yamaguch vf_res = aqb->aqb_data;
4344 1.1 yamaguch sc->sc_max_vectors = le16toh(vf_res->max_vectors);
4345 1.1 yamaguch if (le16toh(vf_res->num_vsis) == 0) {
4346 1.1 yamaguch if (dbg >= 1) {
4347 1.1 yamaguch device_printf(sc->sc_dev, "no vsi available\n");
4348 1.1 yamaguch }
4349 1.1 yamaguch return;
4350 1.1 yamaguch }
4351 1.1 yamaguch sc->sc_vf_cap = le32toh(vf_res->offload_flags);
4352 1.1 yamaguch if (dbg >= 2) {
4353 1.1 yamaguch snprintb(buf, sizeof(buf),
4354 1.1 yamaguch IAVF_VC_OFFLOAD_FMT, sc->sc_vf_cap);
4355 1.1 yamaguch device_printf(sc->sc_dev, "VF cap=%s\n", buf);
4356 1.1 yamaguch }
4357 1.1 yamaguch
4358 1.1 yamaguch mtu = le16toh(vf_res->max_mtu);
4359 1.1 yamaguch if (IAVF_MIN_MTU < mtu && mtu < IAVF_MAX_MTU) {
4360 1.1 yamaguch sc->sc_max_mtu = MIN(IAVF_MAX_MTU, mtu);
4361 1.1 yamaguch }
4362 1.1 yamaguch
4363 1.1 yamaguch vsi_res = &vf_res->vsi_res[0];
4364 1.1 yamaguch sc->sc_vsi_id = le16toh(vsi_res->vsi_id);
4365 1.1 yamaguch sc->sc_vf_id = le32toh(iaq->iaq_param[0]);
4366 1.1 yamaguch sc->sc_qset_handle = le16toh(vsi_res->qset_handle);
4367 1.1 yamaguch sc->sc_nqps_vsi = le16toh(vsi_res->num_queue_pairs);
4368 1.1 yamaguch if (!iavf_is_etheranyaddr(vsi_res->default_mac)) {
4369 1.1 yamaguch enaddr = vsi_res->default_mac;
4370 1.1 yamaguch } else {
4371 1.1 yamaguch enaddr = sc->sc_enaddr_fake;
4372 1.1 yamaguch }
4373 1.1 yamaguch memcpy(sc->sc_enaddr, enaddr, ETHER_ADDR_LEN);
4374 1.1 yamaguch }
4375 1.1 yamaguch
4376 1.1 yamaguch static void
4377 1.1 yamaguch iavf_process_irq_map(struct iavf_softc *sc, struct ixl_aq_desc *iaq)
4378 1.1 yamaguch {
4379 1.1 yamaguch uint32_t retval;
4380 1.1 yamaguch
4381 1.1 yamaguch retval = iavf_aq_vc_get_retval(iaq);
4382 1.1 yamaguch if (retval != IAVF_VC_RC_SUCCESS) {
4383 1.1 yamaguch return;
4384 1.1 yamaguch }
4385 1.1 yamaguch
4386 1.1 yamaguch sc->sc_got_irq_map = 1;
4387 1.1 yamaguch }
4388 1.1 yamaguch
4389 1.1 yamaguch static void
4390 1.1 yamaguch iavf_process_vc_event(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4391 1.1 yamaguch struct ixl_aq_buf *aqb)
4392 1.1 yamaguch {
4393 1.1 yamaguch struct iavf_vc_pf_event *event;
4394 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
4395 1.1 yamaguch const struct iavf_link_speed *speed;
4396 1.1 yamaguch int link;
4397 1.1 yamaguch
4398 1.1 yamaguch event = aqb->aqb_data;
4399 1.1 yamaguch switch (event->event) {
4400 1.1 yamaguch case IAVF_VC_EVENT_LINK_CHANGE:
4401 1.1 yamaguch sc->sc_media_status = IFM_AVALID;
4402 1.1 yamaguch sc->sc_media_active = IFM_ETHER;
4403 1.1 yamaguch link = LINK_STATE_DOWN;
4404 1.1 yamaguch if (event->link_status) {
4405 1.1 yamaguch link = LINK_STATE_UP;
4406 1.1 yamaguch sc->sc_media_status |= IFM_ACTIVE;
4407 1.1 yamaguch
4408 1.1 yamaguch ifp->if_baudrate = 0;
4409 1.1 yamaguch speed = iavf_find_link_speed(sc, event->link_speed);
4410 1.1 yamaguch if (speed != NULL) {
4411 1.1 yamaguch sc->sc_media_active |= speed->media;
4412 1.1 yamaguch ifp->if_baudrate = speed->baudrate;
4413 1.1 yamaguch }
4414 1.1 yamaguch }
4415 1.1 yamaguch
4416 1.1 yamaguch if (sc->sc_link_state != link) {
4417 1.1 yamaguch sc->sc_link_state = link;
4418 1.1 yamaguch if (sc->sc_attached) {
4419 1.1 yamaguch if_link_state_change(ifp, link);
4420 1.1 yamaguch }
4421 1.1 yamaguch }
4422 1.1 yamaguch break;
4423 1.1 yamaguch case IAVF_VC_EVENT_RESET_IMPENDING:
4424 1.1 yamaguch log(LOG_INFO, "%s: Reset warning received from the PF\n",
4425 1.1 yamaguch ifp->if_xname);
4426 1.1 yamaguch iavf_work_set(&sc->sc_reset_task, iavf_reset_request, sc);
4427 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_reset_task);
4428 1.1 yamaguch break;
4429 1.1 yamaguch }
4430 1.1 yamaguch }
4431 1.1 yamaguch
4432 1.1 yamaguch static void
4433 1.1 yamaguch iavf_process_stats(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4434 1.1 yamaguch struct ixl_aq_buf *aqb)
4435 1.1 yamaguch {
4436 1.1 yamaguch struct iavf_stat_counters *isc;
4437 1.1 yamaguch struct i40e_eth_stats *st;
4438 1.1 yamaguch
4439 1.1 yamaguch KASSERT(mutex_owned(&sc->sc_adminq_lock));
4440 1.1 yamaguch
4441 1.1 yamaguch st = aqb->aqb_data;
4442 1.1 yamaguch isc = &sc->sc_stat_counters;
4443 1.1 yamaguch
4444 1.1 yamaguch isc->isc_rx_bytes.ev_count = st->rx_bytes;
4445 1.1 yamaguch isc->isc_rx_unicast.ev_count = st->rx_unicast;
4446 1.1 yamaguch isc->isc_rx_multicast.ev_count = st->rx_multicast;
4447 1.1 yamaguch isc->isc_rx_broadcast.ev_count = st->rx_broadcast;
4448 1.1 yamaguch isc->isc_rx_discards.ev_count = st->rx_discards;
4449 1.1 yamaguch isc->isc_rx_unknown_protocol.ev_count = st->rx_unknown_protocol;
4450 1.1 yamaguch
4451 1.1 yamaguch isc->isc_tx_bytes.ev_count = st->tx_bytes;
4452 1.1 yamaguch isc->isc_tx_unicast.ev_count = st->tx_unicast;
4453 1.1 yamaguch isc->isc_tx_multicast.ev_count = st->tx_multicast;
4454 1.1 yamaguch isc->isc_tx_broadcast.ev_count = st->tx_broadcast;
4455 1.1 yamaguch isc->isc_tx_discards.ev_count = st->tx_discards;
4456 1.1 yamaguch isc->isc_tx_errors.ev_count = st->tx_errors;
4457 1.1 yamaguch }
4458 1.1 yamaguch
4459 1.1 yamaguch static void
4460 1.1 yamaguch iavf_process_req_queues(struct iavf_softc *sc, struct ixl_aq_desc *iaq,
4461 1.1 yamaguch struct ixl_aq_buf *aqb)
4462 1.1 yamaguch {
4463 1.1 yamaguch struct iavf_vc_res_request *req;
4464 1.1 yamaguch struct ifnet *ifp;
4465 1.1 yamaguch uint32_t vc_retval;
4466 1.1 yamaguch
4467 1.1 yamaguch ifp = &sc->sc_ec.ec_if;
4468 1.1 yamaguch req = aqb->aqb_data;
4469 1.1 yamaguch
4470 1.1 yamaguch vc_retval = iavf_aq_vc_get_retval(iaq);
4471 1.1 yamaguch if (vc_retval != IAVF_VC_RC_SUCCESS) {
4472 1.1 yamaguch return;
4473 1.1 yamaguch }
4474 1.1 yamaguch
4475 1.1 yamaguch if (sc->sc_nqps_req < req->num_queue_pairs) {
4476 1.1 yamaguch log(LOG_INFO,
4477 1.1 yamaguch "%s: requested %d queues, but only %d left.\n",
4478 1.1 yamaguch ifp->if_xname,
4479 1.1 yamaguch sc->sc_nqps_req, req->num_queue_pairs);
4480 1.1 yamaguch }
4481 1.1 yamaguch
4482 1.1 yamaguch if (sc->sc_nqps_vsi < req->num_queue_pairs) {
4483 1.1 yamaguch if (!sc->sc_req_queues_retried) {
4484 1.1 yamaguch /* req->num_queue_pairs indicates max qps */
4485 1.1 yamaguch sc->sc_nqps_req = req->num_queue_pairs;
4486 1.1 yamaguch
4487 1.1 yamaguch sc->sc_req_queues_retried = true;
4488 1.1 yamaguch iavf_work_add(sc->sc_workq, &sc->sc_req_queues_task);
4489 1.1 yamaguch }
4490 1.1 yamaguch }
4491 1.1 yamaguch }
4492 1.1 yamaguch
4493 1.1 yamaguch static int
4494 1.1 yamaguch iavf_get_version(struct iavf_softc *sc, struct ixl_aq_buf *aqb)
4495 1.1 yamaguch {
4496 1.1 yamaguch struct ixl_aq_desc iaq;
4497 1.1 yamaguch struct iavf_vc_version_info *ver;
4498 1.1 yamaguch int error;
4499 1.1 yamaguch
4500 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4501 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4502 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4503 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_VERSION);
4504 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(struct iavf_vc_version_info));
4505 1.1 yamaguch
4506 1.1 yamaguch ver = IXL_AQB_KVA(aqb);
4507 1.1 yamaguch ver->major = htole32(IAVF_VF_MAJOR);
4508 1.1 yamaguch ver->minor = htole32(IAVF_VF_MINOR);
4509 1.1 yamaguch
4510 1.1 yamaguch sc->sc_major_ver = UINT_MAX;
4511 1.1 yamaguch sc->sc_minor_ver = UINT_MAX;
4512 1.1 yamaguch
4513 1.1 yamaguch if (sc->sc_attached) {
4514 1.1 yamaguch error = iavf_adminq_poll(sc, &iaq, aqb, 250);
4515 1.1 yamaguch } else {
4516 1.1 yamaguch error = iavf_adminq_poll_locked(sc, &iaq, aqb, 250);
4517 1.1 yamaguch }
4518 1.1 yamaguch
4519 1.1 yamaguch if (error)
4520 1.1 yamaguch return -1;
4521 1.1 yamaguch
4522 1.1 yamaguch return 0;
4523 1.1 yamaguch }
4524 1.1 yamaguch
4525 1.1 yamaguch static int
4526 1.1 yamaguch iavf_get_vf_resources(struct iavf_softc *sc, struct ixl_aq_buf *aqb)
4527 1.1 yamaguch {
4528 1.1 yamaguch struct ixl_aq_desc iaq;
4529 1.1 yamaguch uint32_t *cap, cap0;
4530 1.1 yamaguch int error;
4531 1.1 yamaguch
4532 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4533 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4534 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4535 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_GET_VF_RESOURCES);
4536 1.1 yamaguch
4537 1.1 yamaguch if (sc->sc_major_ver > 0) {
4538 1.1 yamaguch cap0 = IAVF_VC_OFFLOAD_L2 |
4539 1.1 yamaguch IAVF_VC_OFFLOAD_VLAN |
4540 1.1 yamaguch IAVF_VC_OFFLOAD_RSS_PF |
4541 1.1 yamaguch IAVF_VC_OFFLOAD_REQ_QUEUES;
4542 1.1 yamaguch
4543 1.1 yamaguch cap = IXL_AQB_KVA(aqb);
4544 1.1 yamaguch *cap = htole32(cap0);
4545 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*cap));
4546 1.1 yamaguch }
4547 1.1 yamaguch
4548 1.1 yamaguch sc->sc_got_vf_resources = 0;
4549 1.1 yamaguch if (sc->sc_attached) {
4550 1.1 yamaguch error = iavf_adminq_poll(sc, &iaq, aqb, 250);
4551 1.1 yamaguch } else {
4552 1.1 yamaguch error = iavf_adminq_poll_locked(sc, &iaq, aqb, 250);
4553 1.1 yamaguch }
4554 1.1 yamaguch
4555 1.1 yamaguch if (error)
4556 1.1 yamaguch return -1;
4557 1.1 yamaguch return 0;
4558 1.1 yamaguch }
4559 1.1 yamaguch
4560 1.1 yamaguch static int
4561 1.1 yamaguch iavf_get_stats(struct iavf_softc *sc)
4562 1.1 yamaguch {
4563 1.1 yamaguch struct ixl_aq_desc iaq;
4564 1.1 yamaguch struct ixl_aq_buf *aqb;
4565 1.1 yamaguch struct iavf_vc_queue_select *qsel;
4566 1.1 yamaguch int error;
4567 1.1 yamaguch
4568 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4569 1.1 yamaguch aqb = iavf_aqb_get_locked(&sc->sc_atq_idle);
4570 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4571 1.1 yamaguch
4572 1.1 yamaguch if (aqb == NULL)
4573 1.1 yamaguch return ENOMEM;
4574 1.1 yamaguch
4575 1.1 yamaguch qsel = IXL_AQB_KVA(aqb);
4576 1.1 yamaguch memset(qsel, 0, sizeof(*qsel));
4577 1.1 yamaguch qsel->vsi_id = htole16(sc->sc_vsi_id);
4578 1.1 yamaguch
4579 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4580 1.1 yamaguch
4581 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4582 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4583 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_GET_STATS);
4584 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*qsel));
4585 1.1 yamaguch
4586 1.1 yamaguch if (atomic_load_relaxed(&sc->sc_debuglevel) >= 3) {
4587 1.1 yamaguch device_printf(sc->sc_dev, "post GET_STATS command\n");
4588 1.1 yamaguch }
4589 1.1 yamaguch
4590 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4591 1.1 yamaguch error = iavf_atq_post(sc, &iaq, aqb);
4592 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4593 1.1 yamaguch
4594 1.1 yamaguch return error;
4595 1.1 yamaguch }
4596 1.1 yamaguch
4597 1.1 yamaguch static int
4598 1.1 yamaguch iavf_config_irq_map(struct iavf_softc *sc, struct ixl_aq_buf *aqb)
4599 1.1 yamaguch {
4600 1.1 yamaguch struct ixl_aq_desc iaq;
4601 1.1 yamaguch struct iavf_vc_vector_map *vec;
4602 1.1 yamaguch struct iavf_vc_irq_map_info *map;
4603 1.1 yamaguch struct iavf_rx_ring *rxr;
4604 1.1 yamaguch struct iavf_tx_ring *txr;
4605 1.1 yamaguch unsigned int num_vec;
4606 1.1 yamaguch int error;
4607 1.1 yamaguch
4608 1.1 yamaguch map = IXL_AQB_KVA(aqb);
4609 1.1 yamaguch vec = map->vecmap;
4610 1.1 yamaguch num_vec = 0;
4611 1.1 yamaguch
4612 1.1 yamaguch if (sc->sc_nintrs == 1) {
4613 1.1 yamaguch vec[0].vsi_id = htole16(sc->sc_vsi_id);
4614 1.1 yamaguch vec[0].vector_id = htole16(0);
4615 1.1 yamaguch vec[0].rxq_map = htole16(iavf_allqueues(sc));
4616 1.1 yamaguch vec[0].txq_map = htole16(iavf_allqueues(sc));
4617 1.1 yamaguch vec[0].rxitr_idx = htole16(IAVF_NOITR);
4618 1.1 yamaguch vec[0].rxitr_idx = htole16(IAVF_NOITR);
4619 1.1 yamaguch num_vec = 1;
4620 1.1 yamaguch } else if (sc->sc_nintrs > 1) {
4621 1.1 yamaguch KASSERT(sc->sc_nqps_alloc >= (sc->sc_nintrs - 1));
4622 1.1 yamaguch for (; num_vec < (sc->sc_nintrs - 1); num_vec++) {
4623 1.1 yamaguch rxr = sc->sc_qps[num_vec].qp_rxr;
4624 1.1 yamaguch txr = sc->sc_qps[num_vec].qp_txr;
4625 1.1 yamaguch
4626 1.1 yamaguch vec[num_vec].vsi_id = htole16(sc->sc_vsi_id);
4627 1.1 yamaguch vec[num_vec].vector_id = htole16(num_vec + 1);
4628 1.1 yamaguch vec[num_vec].rxq_map = htole16(__BIT(rxr->rxr_qid));
4629 1.1 yamaguch vec[num_vec].txq_map = htole16(__BIT(txr->txr_qid));
4630 1.1 yamaguch vec[num_vec].rxitr_idx = htole16(IAVF_ITR_RX);
4631 1.1 yamaguch vec[num_vec].txitr_idx = htole16(IAVF_ITR_TX);
4632 1.1 yamaguch }
4633 1.1 yamaguch
4634 1.1 yamaguch vec[num_vec].vsi_id = htole16(sc->sc_vsi_id);
4635 1.1 yamaguch vec[num_vec].vector_id = htole16(0);
4636 1.1 yamaguch vec[num_vec].rxq_map = htole16(0);
4637 1.1 yamaguch vec[num_vec].txq_map = htole16(0);
4638 1.1 yamaguch num_vec++;
4639 1.1 yamaguch }
4640 1.1 yamaguch
4641 1.1 yamaguch map->num_vectors = htole16(num_vec);
4642 1.1 yamaguch
4643 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4644 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4645 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4646 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_CONFIG_IRQ_MAP);
4647 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*map) + sizeof(*vec) * num_vec);
4648 1.1 yamaguch
4649 1.1 yamaguch if (sc->sc_attached) {
4650 1.1 yamaguch error = iavf_adminq_poll(sc, &iaq, aqb, 250);
4651 1.1 yamaguch } else {
4652 1.1 yamaguch error = iavf_adminq_poll_locked(sc, &iaq, aqb, 250);
4653 1.1 yamaguch }
4654 1.1 yamaguch
4655 1.1 yamaguch if (error)
4656 1.1 yamaguch return -1;
4657 1.1 yamaguch
4658 1.1 yamaguch return 0;
4659 1.1 yamaguch }
4660 1.1 yamaguch
4661 1.1 yamaguch static int
4662 1.1 yamaguch iavf_config_vsi_queues(struct iavf_softc *sc)
4663 1.1 yamaguch {
4664 1.1 yamaguch struct ifnet *ifp = &sc->sc_ec.ec_if;
4665 1.1 yamaguch struct ixl_aq_desc iaq;
4666 1.1 yamaguch struct ixl_aq_buf *aqb;
4667 1.1 yamaguch struct iavf_vc_queue_config_info *config;
4668 1.1 yamaguch struct iavf_vc_txq_info *txq;
4669 1.1 yamaguch struct iavf_vc_rxq_info *rxq;
4670 1.1 yamaguch struct iavf_rx_ring *rxr;
4671 1.1 yamaguch struct iavf_tx_ring *txr;
4672 1.1 yamaguch uint32_t rxmtu_max;
4673 1.1 yamaguch unsigned int i;
4674 1.1 yamaguch int error;
4675 1.1 yamaguch
4676 1.1 yamaguch rxmtu_max = ifp->if_mtu + IAVF_MTU_ETHERLEN;
4677 1.1 yamaguch
4678 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4679 1.1 yamaguch
4680 1.1 yamaguch if (aqb == NULL)
4681 1.1 yamaguch return -1;
4682 1.1 yamaguch
4683 1.1 yamaguch config = IXL_AQB_KVA(aqb);
4684 1.1 yamaguch memset(config, 0, sizeof(*config));
4685 1.1 yamaguch config->vsi_id = htole16(sc->sc_vsi_id);
4686 1.1 yamaguch config->num_queue_pairs = htole16(sc->sc_nqueue_pairs);
4687 1.1 yamaguch
4688 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
4689 1.1 yamaguch rxr = sc->sc_qps[i].qp_rxr;
4690 1.1 yamaguch txr = sc->sc_qps[i].qp_txr;
4691 1.1 yamaguch
4692 1.1 yamaguch txq = &config->qpair[i].txq;
4693 1.1 yamaguch txq->vsi_id = htole16(sc->sc_vsi_id);
4694 1.1 yamaguch txq->queue_id = htole16(txr->txr_qid);
4695 1.1 yamaguch txq->ring_len = htole16(sc->sc_tx_ring_ndescs);
4696 1.1 yamaguch txq->headwb_ena = 0;
4697 1.1 yamaguch txq->dma_ring_addr = htole64(IXL_DMA_DVA(&txr->txr_mem));
4698 1.1 yamaguch txq->dma_headwb_addr = 0;
4699 1.1 yamaguch
4700 1.1 yamaguch rxq = &config->qpair[i].rxq;
4701 1.1 yamaguch rxq->vsi_id = htole16(sc->sc_vsi_id);
4702 1.1 yamaguch rxq->queue_id = htole16(rxr->rxr_qid);
4703 1.1 yamaguch rxq->ring_len = htole16(sc->sc_rx_ring_ndescs);
4704 1.1 yamaguch rxq->splithdr_ena = 0;
4705 1.1 yamaguch rxq->databuf_size = htole32(IAVF_MCLBYTES);
4706 1.1 yamaguch rxq->max_pkt_size = htole32(rxmtu_max);
4707 1.1 yamaguch rxq->dma_ring_addr = htole64(IXL_DMA_DVA(&rxr->rxr_mem));
4708 1.1 yamaguch rxq->rx_split_pos = 0;
4709 1.1 yamaguch }
4710 1.1 yamaguch
4711 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4712 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4713 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4714 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_CONFIG_VSI_QUEUES);
4715 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*config) +
4716 1.1 yamaguch sizeof(config->qpair[0]) * sc->sc_nqueue_pairs);
4717 1.1 yamaguch
4718 1.1 yamaguch error = iavf_adminq_exec(sc, &iaq, aqb);
4719 1.1 yamaguch if (error != IAVF_VC_RC_SUCCESS) {
4720 1.1 yamaguch return -1;
4721 1.1 yamaguch }
4722 1.1 yamaguch
4723 1.1 yamaguch return 0;
4724 1.1 yamaguch }
4725 1.1 yamaguch
4726 1.1 yamaguch static int
4727 1.1 yamaguch iavf_config_hena(struct iavf_softc *sc)
4728 1.1 yamaguch {
4729 1.1 yamaguch struct ixl_aq_desc iaq;
4730 1.1 yamaguch struct ixl_aq_buf *aqb;
4731 1.1 yamaguch uint64_t *caps;
4732 1.1 yamaguch int error;
4733 1.1 yamaguch
4734 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4735 1.1 yamaguch
4736 1.1 yamaguch if (aqb == NULL)
4737 1.1 yamaguch return -1;
4738 1.1 yamaguch
4739 1.1 yamaguch caps = IXL_AQB_KVA(aqb);
4740 1.1 yamaguch if (sc->sc_mac_type == I40E_MAC_X722_VF)
4741 1.1 yamaguch *caps = IXL_RSS_HENA_DEFAULT_XL710;
4742 1.1 yamaguch else
4743 1.1 yamaguch *caps = IXL_RSS_HENA_DEFAULT_X722;
4744 1.1 yamaguch
4745 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4746 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4747 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4748 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_SET_RSS_HENA);
4749 1.2 jakllsch iaq.iaq_datalen = htole16(sizeof(*caps));
4750 1.1 yamaguch
4751 1.1 yamaguch error = iavf_adminq_exec(sc, &iaq, aqb);
4752 1.1 yamaguch if (error != IAVF_VC_RC_SUCCESS) {
4753 1.1 yamaguch return -1;
4754 1.1 yamaguch }
4755 1.1 yamaguch
4756 1.1 yamaguch return 0;
4757 1.1 yamaguch }
4758 1.1 yamaguch
4759 1.1 yamaguch static inline void
4760 1.1 yamaguch iavf_get_default_rss_key(uint8_t *buf, size_t len)
4761 1.1 yamaguch {
4762 1.1 yamaguch uint8_t rss_seed[RSS_KEYSIZE];
4763 1.1 yamaguch size_t cplen;
4764 1.1 yamaguch
4765 1.1 yamaguch cplen = MIN(len, sizeof(rss_seed));
4766 1.1 yamaguch rss_getkey(rss_seed);
4767 1.1 yamaguch
4768 1.1 yamaguch memcpy(buf, rss_seed, cplen);
4769 1.1 yamaguch if (cplen < len)
4770 1.1 yamaguch memset(buf + cplen, 0, len - cplen);
4771 1.1 yamaguch }
4772 1.1 yamaguch
4773 1.1 yamaguch static int
4774 1.1 yamaguch iavf_config_rss_key(struct iavf_softc *sc)
4775 1.1 yamaguch {
4776 1.1 yamaguch struct ixl_aq_desc iaq;
4777 1.1 yamaguch struct ixl_aq_buf *aqb;
4778 1.1 yamaguch struct iavf_vc_rss_key *rss_key;
4779 1.1 yamaguch size_t key_len;
4780 1.1 yamaguch int rv;
4781 1.1 yamaguch
4782 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4783 1.1 yamaguch if (aqb == NULL)
4784 1.1 yamaguch return -1;
4785 1.1 yamaguch
4786 1.1 yamaguch rss_key = IXL_AQB_KVA(aqb);
4787 1.1 yamaguch rss_key->vsi_id = htole16(sc->sc_vsi_id);
4788 1.1 yamaguch key_len = IXL_RSS_KEY_SIZE;
4789 1.1 yamaguch iavf_get_default_rss_key(rss_key->key, key_len);
4790 1.1 yamaguch rss_key->key_len = key_len;
4791 1.1 yamaguch
4792 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4793 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4794 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4795 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_CONFIG_RSS_KEY);
4796 1.2 jakllsch iaq.iaq_datalen = htole16(sizeof(*rss_key) - sizeof(rss_key->pad)
4797 1.1 yamaguch + (sizeof(rss_key->key[0]) * key_len));
4798 1.1 yamaguch
4799 1.1 yamaguch rv = iavf_adminq_exec(sc, &iaq, aqb);
4800 1.1 yamaguch if (rv != IAVF_VC_RC_SUCCESS) {
4801 1.1 yamaguch return -1;
4802 1.1 yamaguch }
4803 1.1 yamaguch
4804 1.1 yamaguch return 0;
4805 1.1 yamaguch }
4806 1.1 yamaguch
4807 1.1 yamaguch static int
4808 1.1 yamaguch iavf_config_rss_lut(struct iavf_softc *sc)
4809 1.1 yamaguch {
4810 1.1 yamaguch struct ixl_aq_desc iaq;
4811 1.1 yamaguch struct ixl_aq_buf *aqb;
4812 1.1 yamaguch struct iavf_vc_rss_lut *rss_lut;
4813 1.1 yamaguch uint8_t *lut, v;
4814 1.1 yamaguch int rv, i;
4815 1.1 yamaguch
4816 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4817 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4818 1.1 yamaguch
4819 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4820 1.1 yamaguch if (aqb == NULL)
4821 1.1 yamaguch return -1;
4822 1.1 yamaguch
4823 1.1 yamaguch rss_lut = IXL_AQB_KVA(aqb);
4824 1.1 yamaguch rss_lut->vsi_id = htole16(sc->sc_vsi_id);
4825 1.1 yamaguch rss_lut->lut_entries = htole16(IXL_RSS_VSI_LUT_SIZE);
4826 1.1 yamaguch
4827 1.1 yamaguch lut = rss_lut->lut;
4828 1.1 yamaguch for (i = 0; i < IXL_RSS_VSI_LUT_SIZE; i++) {
4829 1.1 yamaguch v = i % sc->sc_nqueue_pairs;
4830 1.1 yamaguch v &= IAVF_RSS_VSI_LUT_ENTRY_MASK;
4831 1.1 yamaguch lut[i] = v;
4832 1.1 yamaguch }
4833 1.1 yamaguch
4834 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4835 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4836 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4837 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_CONFIG_RSS_LUT);
4838 1.2 jakllsch iaq.iaq_datalen = htole16(sizeof(*rss_lut) - sizeof(rss_lut->pad)
4839 1.1 yamaguch + (sizeof(rss_lut->lut[0]) * IXL_RSS_VSI_LUT_SIZE));
4840 1.1 yamaguch
4841 1.1 yamaguch rv = iavf_adminq_exec(sc, &iaq, aqb);
4842 1.1 yamaguch if (rv != IAVF_VC_RC_SUCCESS) {
4843 1.1 yamaguch return -1;
4844 1.1 yamaguch }
4845 1.1 yamaguch
4846 1.1 yamaguch return 0;
4847 1.1 yamaguch }
4848 1.1 yamaguch
4849 1.1 yamaguch static int
4850 1.1 yamaguch iavf_queue_select(struct iavf_softc *sc, int opcode)
4851 1.1 yamaguch {
4852 1.1 yamaguch struct ixl_aq_desc iaq;
4853 1.1 yamaguch struct ixl_aq_buf *aqb;
4854 1.1 yamaguch struct iavf_vc_queue_select *qsel;
4855 1.1 yamaguch int error;
4856 1.1 yamaguch
4857 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4858 1.1 yamaguch if (aqb == NULL)
4859 1.1 yamaguch return -1;
4860 1.1 yamaguch
4861 1.1 yamaguch qsel = IXL_AQB_KVA(aqb);
4862 1.1 yamaguch qsel->vsi_id = htole16(sc->sc_vsi_id);
4863 1.1 yamaguch qsel->rx_queues = htole32(iavf_allqueues(sc));
4864 1.1 yamaguch qsel->tx_queues = htole32(iavf_allqueues(sc));
4865 1.1 yamaguch
4866 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4867 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4868 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4869 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, opcode);
4870 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*qsel));
4871 1.1 yamaguch
4872 1.1 yamaguch error = iavf_adminq_exec(sc, &iaq, aqb);
4873 1.1 yamaguch if (error != IAVF_VC_RC_SUCCESS) {
4874 1.1 yamaguch return -1;
4875 1.1 yamaguch }
4876 1.1 yamaguch
4877 1.1 yamaguch return 0;
4878 1.1 yamaguch }
4879 1.1 yamaguch
4880 1.1 yamaguch static int
4881 1.1 yamaguch iavf_request_queues(struct iavf_softc *sc, unsigned int req_num)
4882 1.1 yamaguch {
4883 1.1 yamaguch struct ixl_aq_desc iaq;
4884 1.1 yamaguch struct ixl_aq_buf *aqb;
4885 1.1 yamaguch struct iavf_vc_res_request *req;
4886 1.1 yamaguch int rv;
4887 1.1 yamaguch
4888 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4889 1.1 yamaguch if (aqb == NULL)
4890 1.1 yamaguch return ENOMEM;
4891 1.1 yamaguch
4892 1.1 yamaguch req = IXL_AQB_KVA(aqb);
4893 1.1 yamaguch req->num_queue_pairs = req_num;
4894 1.1 yamaguch
4895 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4896 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4897 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4898 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_REQUEST_QUEUES);
4899 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*req));
4900 1.1 yamaguch
4901 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4902 1.1 yamaguch rv = iavf_atq_post(sc, &iaq, aqb);
4903 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4904 1.1 yamaguch
4905 1.1 yamaguch return rv;
4906 1.1 yamaguch }
4907 1.1 yamaguch
4908 1.1 yamaguch static int
4909 1.1 yamaguch iavf_reset_vf(struct iavf_softc *sc)
4910 1.1 yamaguch {
4911 1.1 yamaguch struct ixl_aq_desc iaq;
4912 1.1 yamaguch int error;
4913 1.1 yamaguch
4914 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4915 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_RD);
4916 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4917 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_RESET_VF);
4918 1.1 yamaguch iaq.iaq_datalen = htole16(0);
4919 1.1 yamaguch
4920 1.1 yamaguch iavf_wr(sc, I40E_VFGEN_RSTAT, IAVF_VFR_INPROGRESS);
4921 1.1 yamaguch
4922 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4923 1.1 yamaguch error = iavf_atq_post(sc, &iaq, NULL);
4924 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4925 1.1 yamaguch
4926 1.1 yamaguch return error;
4927 1.1 yamaguch }
4928 1.1 yamaguch
4929 1.1 yamaguch static int
4930 1.1 yamaguch iavf_eth_addr(struct iavf_softc *sc, const uint8_t *addr, uint32_t opcode)
4931 1.1 yamaguch {
4932 1.1 yamaguch struct ixl_aq_desc iaq;
4933 1.1 yamaguch struct ixl_aq_buf *aqb;
4934 1.1 yamaguch struct iavf_vc_eth_addr_list *addrs;
4935 1.1 yamaguch struct iavf_vc_eth_addr *vcaddr;
4936 1.1 yamaguch int rv;
4937 1.1 yamaguch
4938 1.1 yamaguch KASSERT(sc->sc_attached);
4939 1.1 yamaguch KASSERT(opcode == IAVF_VC_OP_ADD_ETH_ADDR ||
4940 1.1 yamaguch opcode == IAVF_VC_OP_DEL_ETH_ADDR);
4941 1.1 yamaguch
4942 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4943 1.1 yamaguch if (aqb == NULL)
4944 1.1 yamaguch return -1;
4945 1.1 yamaguch
4946 1.1 yamaguch addrs = IXL_AQB_KVA(aqb);
4947 1.1 yamaguch addrs->vsi_id = htole16(sc->sc_vsi_id);
4948 1.1 yamaguch addrs->num_elements = htole16(1);
4949 1.1 yamaguch vcaddr = addrs->list;
4950 1.1 yamaguch memcpy(vcaddr->addr, addr, ETHER_ADDR_LEN);
4951 1.1 yamaguch
4952 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4953 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
4954 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
4955 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, opcode);
4956 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*addrs) + sizeof(*vcaddr));
4957 1.1 yamaguch
4958 1.1 yamaguch if (sc->sc_resetting) {
4959 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
4960 1.1 yamaguch rv = iavf_adminq_poll_locked(sc, &iaq, aqb, 250);
4961 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
4962 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
4963 1.1 yamaguch } else {
4964 1.1 yamaguch rv = iavf_adminq_exec(sc, &iaq, aqb);
4965 1.1 yamaguch }
4966 1.1 yamaguch
4967 1.1 yamaguch if (rv != IAVF_VC_RC_SUCCESS) {
4968 1.1 yamaguch return -1;
4969 1.1 yamaguch }
4970 1.1 yamaguch
4971 1.1 yamaguch return 0;
4972 1.1 yamaguch }
4973 1.1 yamaguch
4974 1.1 yamaguch static int
4975 1.1 yamaguch iavf_config_promisc_mode(struct iavf_softc *sc, int unicast, int multicast)
4976 1.1 yamaguch {
4977 1.1 yamaguch struct ixl_aq_desc iaq;
4978 1.1 yamaguch struct ixl_aq_buf *aqb;
4979 1.1 yamaguch struct iavf_vc_promisc_info *promisc;
4980 1.1 yamaguch int flags;
4981 1.1 yamaguch
4982 1.1 yamaguch KASSERT(sc->sc_attached);
4983 1.1 yamaguch
4984 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
4985 1.1 yamaguch if (aqb == NULL)
4986 1.1 yamaguch return -1;
4987 1.1 yamaguch
4988 1.1 yamaguch flags = 0;
4989 1.1 yamaguch if (unicast)
4990 1.1 yamaguch flags |= IAVF_FLAG_VF_UNICAST_PROMISC;
4991 1.1 yamaguch if (multicast)
4992 1.1 yamaguch flags |= IAVF_FLAG_VF_MULTICAST_PROMISC;
4993 1.1 yamaguch
4994 1.1 yamaguch promisc = IXL_AQB_KVA(aqb);
4995 1.1 yamaguch promisc->vsi_id = htole16(sc->sc_vsi_id);
4996 1.1 yamaguch promisc->flags = htole16(flags);
4997 1.1 yamaguch
4998 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
4999 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
5000 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
5001 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, IAVF_VC_OP_CONFIG_PROMISC);
5002 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*promisc));
5003 1.1 yamaguch
5004 1.1 yamaguch if (iavf_adminq_exec(sc, &iaq, aqb) != IAVF_VC_RC_SUCCESS) {
5005 1.1 yamaguch return -1;
5006 1.1 yamaguch }
5007 1.1 yamaguch
5008 1.1 yamaguch return 0;
5009 1.1 yamaguch }
5010 1.1 yamaguch
5011 1.1 yamaguch static int
5012 1.1 yamaguch iavf_config_vlan_stripping(struct iavf_softc *sc, int eccap)
5013 1.1 yamaguch {
5014 1.1 yamaguch struct ixl_aq_desc iaq;
5015 1.1 yamaguch uint32_t opcode;
5016 1.1 yamaguch
5017 1.1 yamaguch opcode = ISSET(eccap, ETHERCAP_VLAN_HWTAGGING) ?
5018 1.1 yamaguch IAVF_VC_OP_ENABLE_VLAN_STRIP : IAVF_VC_OP_DISABLE_VLAN_STRIP;
5019 1.1 yamaguch
5020 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
5021 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_RD);
5022 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
5023 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, opcode);
5024 1.1 yamaguch iaq.iaq_datalen = htole16(0);
5025 1.1 yamaguch
5026 1.1 yamaguch if (iavf_adminq_exec(sc, &iaq, NULL) != IAVF_VC_RC_SUCCESS) {
5027 1.1 yamaguch return -1;
5028 1.1 yamaguch }
5029 1.1 yamaguch
5030 1.1 yamaguch return 0;
5031 1.1 yamaguch }
5032 1.1 yamaguch
5033 1.1 yamaguch static int
5034 1.1 yamaguch iavf_config_vlan_id(struct iavf_softc *sc, uint16_t vid, uint32_t opcode)
5035 1.1 yamaguch {
5036 1.1 yamaguch struct ixl_aq_desc iaq;
5037 1.1 yamaguch struct ixl_aq_buf *aqb;
5038 1.1 yamaguch struct iavf_vc_vlan_filter *vfilter;
5039 1.1 yamaguch int rv;
5040 1.1 yamaguch
5041 1.1 yamaguch KASSERT(opcode == IAVF_VC_OP_ADD_VLAN || opcode == IAVF_VC_OP_DEL_VLAN);
5042 1.1 yamaguch
5043 1.1 yamaguch aqb = iavf_aqb_get(sc, &sc->sc_atq_idle);
5044 1.1 yamaguch
5045 1.1 yamaguch if (aqb == NULL)
5046 1.1 yamaguch return -1;
5047 1.1 yamaguch
5048 1.1 yamaguch vfilter = IXL_AQB_KVA(aqb);
5049 1.1 yamaguch vfilter->vsi_id = htole16(sc->sc_vsi_id);
5050 1.1 yamaguch vfilter->num_vlan_id = htole16(1);
5051 1.1 yamaguch vfilter->vlan_id[0] = vid;
5052 1.1 yamaguch
5053 1.1 yamaguch memset(&iaq, 0, sizeof(iaq));
5054 1.1 yamaguch iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
5055 1.1 yamaguch iaq.iaq_opcode = htole16(IAVF_AQ_OP_SEND_TO_PF);
5056 1.1 yamaguch iavf_aq_vc_set_opcode(&iaq, opcode);
5057 1.1 yamaguch iaq.iaq_datalen = htole16(sizeof(*vfilter) + sizeof(vid));
5058 1.1 yamaguch
5059 1.1 yamaguch if (sc->sc_resetting) {
5060 1.1 yamaguch mutex_enter(&sc->sc_adminq_lock);
5061 1.1 yamaguch rv = iavf_adminq_poll_locked(sc, &iaq, aqb, 250);
5062 1.1 yamaguch iavf_aqb_put_locked(&sc->sc_atq_idle, aqb);
5063 1.1 yamaguch mutex_exit(&sc->sc_adminq_lock);
5064 1.1 yamaguch } else {
5065 1.1 yamaguch rv = iavf_adminq_exec(sc, &iaq, aqb);
5066 1.1 yamaguch }
5067 1.1 yamaguch
5068 1.1 yamaguch if (rv != IAVF_VC_RC_SUCCESS) {
5069 1.1 yamaguch return -1;
5070 1.1 yamaguch }
5071 1.1 yamaguch
5072 1.1 yamaguch return 0;
5073 1.1 yamaguch }
5074 1.1 yamaguch
5075 1.1 yamaguch static void
5076 1.1 yamaguch iavf_post_request_queues(void *xsc)
5077 1.1 yamaguch {
5078 1.1 yamaguch struct iavf_softc *sc;
5079 1.1 yamaguch struct ifnet *ifp;
5080 1.1 yamaguch
5081 1.1 yamaguch sc = xsc;
5082 1.1 yamaguch ifp = &sc->sc_ec.ec_if;
5083 1.1 yamaguch
5084 1.1 yamaguch if (!ISSET(sc->sc_vf_cap, IAVF_VC_OFFLOAD_REQ_QUEUES)) {
5085 1.1 yamaguch log(LOG_DEBUG, "%s: the VF has no REQ_QUEUES capability\n",
5086 1.1 yamaguch ifp->if_xname);
5087 1.1 yamaguch return;
5088 1.1 yamaguch }
5089 1.1 yamaguch
5090 1.1 yamaguch log(LOG_INFO, "%s: try to change the number of queue pairs"
5091 1.1 yamaguch " (vsi %u, %u allocated, request %u)\n",
5092 1.1 yamaguch ifp->if_xname,
5093 1.1 yamaguch sc->sc_nqps_vsi, sc->sc_nqps_alloc, sc->sc_nqps_req);
5094 1.1 yamaguch iavf_request_queues(sc, sc->sc_nqps_req);
5095 1.1 yamaguch }
5096 1.1 yamaguch
5097 1.1 yamaguch static bool
5098 1.1 yamaguch iavf_sysctlnode_is_rx(struct sysctlnode *node)
5099 1.1 yamaguch {
5100 1.1 yamaguch
5101 1.1 yamaguch if (strstr(node->sysctl_parent->sysctl_name, "rx") != NULL)
5102 1.1 yamaguch return true;
5103 1.1 yamaguch
5104 1.1 yamaguch return false;
5105 1.1 yamaguch }
5106 1.1 yamaguch
5107 1.1 yamaguch static int
5108 1.1 yamaguch iavf_sysctl_itr_handler(SYSCTLFN_ARGS)
5109 1.1 yamaguch {
5110 1.1 yamaguch struct sysctlnode node = *rnode;
5111 1.1 yamaguch struct iavf_softc *sc = (struct iavf_softc *)node.sysctl_data;
5112 1.1 yamaguch uint32_t newitr, *itrptr;
5113 1.1 yamaguch unsigned int i;
5114 1.1 yamaguch int itr, error;
5115 1.1 yamaguch
5116 1.1 yamaguch if (iavf_sysctlnode_is_rx(&node)) {
5117 1.1 yamaguch itrptr = &sc->sc_rx_itr;
5118 1.1 yamaguch itr = IAVF_ITR_RX;
5119 1.1 yamaguch } else {
5120 1.1 yamaguch itrptr = &sc->sc_tx_itr;
5121 1.1 yamaguch itr = IAVF_ITR_TX;
5122 1.1 yamaguch }
5123 1.1 yamaguch
5124 1.1 yamaguch newitr = *itrptr;
5125 1.1 yamaguch node.sysctl_data = &newitr;
5126 1.1 yamaguch node.sysctl_size = sizeof(newitr);
5127 1.1 yamaguch
5128 1.1 yamaguch error = sysctl_lookup(SYSCTLFN_CALL(&node));
5129 1.1 yamaguch if (error || newp == NULL)
5130 1.1 yamaguch return error;
5131 1.1 yamaguch
5132 1.1 yamaguch if (newitr > 0x07FF)
5133 1.1 yamaguch return EINVAL;
5134 1.1 yamaguch
5135 1.1 yamaguch *itrptr = newitr;
5136 1.1 yamaguch
5137 1.1 yamaguch for (i = 0; i < sc->sc_nqueue_pairs; i++) {
5138 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ITRN1(itr, i), *itrptr);
5139 1.1 yamaguch }
5140 1.1 yamaguch iavf_wr(sc, I40E_VFINT_ITR01(itr), *itrptr);
5141 1.1 yamaguch
5142 1.1 yamaguch return 0;
5143 1.1 yamaguch }
5144 1.1 yamaguch
5145 1.1 yamaguch static void
5146 1.1 yamaguch iavf_workq_work(struct work *wk, void *context)
5147 1.1 yamaguch {
5148 1.1 yamaguch struct iavf_work *work;
5149 1.1 yamaguch
5150 1.1 yamaguch work = container_of(wk, struct iavf_work, ixw_cookie);
5151 1.1 yamaguch
5152 1.1 yamaguch atomic_swap_uint(&work->ixw_added, 0);
5153 1.1 yamaguch work->ixw_func(work->ixw_arg);
5154 1.1 yamaguch }
5155 1.1 yamaguch
5156 1.1 yamaguch static struct workqueue *
5157 1.1 yamaguch iavf_workq_create(const char *name, pri_t prio, int ipl, int flags)
5158 1.1 yamaguch {
5159 1.1 yamaguch struct workqueue *wq;
5160 1.1 yamaguch int error;
5161 1.1 yamaguch
5162 1.1 yamaguch error = workqueue_create(&wq, name, iavf_workq_work, NULL,
5163 1.1 yamaguch prio, ipl, flags);
5164 1.1 yamaguch
5165 1.1 yamaguch if (error)
5166 1.1 yamaguch return NULL;
5167 1.1 yamaguch
5168 1.1 yamaguch return wq;
5169 1.1 yamaguch }
5170 1.1 yamaguch
5171 1.1 yamaguch static void
5172 1.1 yamaguch iavf_workq_destroy(struct workqueue *wq)
5173 1.1 yamaguch {
5174 1.1 yamaguch
5175 1.1 yamaguch workqueue_destroy(wq);
5176 1.1 yamaguch }
5177 1.1 yamaguch
5178 1.1 yamaguch static int
5179 1.1 yamaguch iavf_work_set(struct iavf_work *work, void (*func)(void *), void *arg)
5180 1.1 yamaguch {
5181 1.1 yamaguch
5182 1.1 yamaguch if (work->ixw_added != 0)
5183 1.1 yamaguch return -1;
5184 1.1 yamaguch
5185 1.1 yamaguch memset(work, 0, sizeof(*work));
5186 1.1 yamaguch work->ixw_func = func;
5187 1.1 yamaguch work->ixw_arg = arg;
5188 1.1 yamaguch
5189 1.1 yamaguch return 0;
5190 1.1 yamaguch }
5191 1.1 yamaguch
5192 1.1 yamaguch static void
5193 1.1 yamaguch iavf_work_add(struct workqueue *wq, struct iavf_work *work)
5194 1.1 yamaguch {
5195 1.1 yamaguch if (atomic_cas_uint(&work->ixw_added, 0, 1) != 0)
5196 1.1 yamaguch return;
5197 1.1 yamaguch
5198 1.1 yamaguch kpreempt_disable();
5199 1.1 yamaguch workqueue_enqueue(wq, &work->ixw_cookie, NULL);
5200 1.1 yamaguch kpreempt_enable();
5201 1.1 yamaguch }
5202 1.1 yamaguch
5203 1.1 yamaguch static void
5204 1.1 yamaguch iavf_work_wait(struct workqueue *wq, struct iavf_work *work)
5205 1.1 yamaguch {
5206 1.1 yamaguch
5207 1.1 yamaguch workqueue_wait(wq, &work->ixw_cookie);
5208 1.1 yamaguch }
5209 1.1 yamaguch
5210 1.1 yamaguch static void
5211 1.1 yamaguch iavf_evcnt_attach(struct evcnt *ec,
5212 1.1 yamaguch const char *n0, const char *n1)
5213 1.1 yamaguch {
5214 1.1 yamaguch
5215 1.1 yamaguch evcnt_attach_dynamic(ec, EVCNT_TYPE_MISC,
5216 1.1 yamaguch NULL, n0, n1);
5217 1.1 yamaguch }
5218 1.1 yamaguch
5219 1.1 yamaguch MODULE(MODULE_CLASS_DRIVER, if_iavf, "pci");
5220 1.1 yamaguch
5221 1.1 yamaguch #ifdef _MODULE
5222 1.1 yamaguch #include "ioconf.c"
5223 1.1 yamaguch #endif
5224 1.1 yamaguch
5225 1.1 yamaguch #ifdef _MODULE
5226 1.1 yamaguch static void
5227 1.1 yamaguch iavf_parse_modprop(prop_dictionary_t dict)
5228 1.1 yamaguch {
5229 1.1 yamaguch prop_object_t obj;
5230 1.1 yamaguch int64_t val;
5231 1.1 yamaguch uint32_t n;
5232 1.1 yamaguch
5233 1.1 yamaguch if (dict == NULL)
5234 1.1 yamaguch return;
5235 1.1 yamaguch
5236 1.1 yamaguch obj = prop_dictionary_get(dict, "debug_level");
5237 1.1 yamaguch if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
5238 1.1 yamaguch val = prop_number_signed_value((prop_number_t)obj);
5239 1.1 yamaguch
5240 1.1 yamaguch if (val > 0) {
5241 1.1 yamaguch iavf_params.debug = val;
5242 1.1 yamaguch printf("iavf: debug level=%d\n", iavf_params.debug);
5243 1.1 yamaguch }
5244 1.1 yamaguch }
5245 1.1 yamaguch
5246 1.1 yamaguch obj = prop_dictionary_get(dict, "max_qps");
5247 1.1 yamaguch if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
5248 1.1 yamaguch val = prop_number_signed_value((prop_number_t)obj);
5249 1.1 yamaguch
5250 1.3 jakllsch if (val < 1 || val > I40E_MAX_VF_QUEUES) {
5251 1.1 yamaguch printf("iavf: invalid queue size(1 <= n <= %d)",
5252 1.1 yamaguch I40E_MAX_VF_QUEUES);
5253 1.1 yamaguch } else {
5254 1.1 yamaguch iavf_params.max_qps = val;
5255 1.1 yamaguch printf("iavf: request queue pair = %u\n",
5256 1.1 yamaguch iavf_params.max_qps);
5257 1.1 yamaguch }
5258 1.1 yamaguch }
5259 1.1 yamaguch
5260 1.1 yamaguch obj = prop_dictionary_get(dict, "tx_itr");
5261 1.1 yamaguch if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
5262 1.1 yamaguch val = prop_number_signed_value((prop_number_t)obj);
5263 1.1 yamaguch if (val > 0x07FF) {
5264 1.1 yamaguch printf("iavf: TX ITR too big (%" PRId64 " <= %d)",
5265 1.1 yamaguch val, 0x7FF);
5266 1.1 yamaguch } else {
5267 1.1 yamaguch iavf_params.tx_itr = val;
5268 1.1 yamaguch printf("iavf: TX ITR = 0x%" PRIx32,
5269 1.1 yamaguch iavf_params.tx_itr);
5270 1.1 yamaguch }
5271 1.1 yamaguch }
5272 1.1 yamaguch
5273 1.1 yamaguch obj = prop_dictionary_get(dict, "rx_itr");
5274 1.1 yamaguch if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
5275 1.1 yamaguch val = prop_number_signed_value((prop_number_t)obj);
5276 1.1 yamaguch if (val > 0x07FF) {
5277 1.1 yamaguch printf("iavf: RX ITR too big (%" PRId64 " <= %d)",
5278 1.1 yamaguch val, 0x7FF);
5279 1.1 yamaguch } else {
5280 1.1 yamaguch iavf_params.rx_itr = val;
5281 1.1 yamaguch printf("iavf: RX ITR = 0x%" PRIx32,
5282 1.1 yamaguch iavf_params.rx_itr);
5283 1.1 yamaguch }
5284 1.1 yamaguch }
5285 1.1 yamaguch
5286 1.1 yamaguch obj = prop_dictionary_get(dict, "tx_ndescs");
5287 1.1 yamaguch if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
5288 1.1 yamaguch val = prop_number_signed_value((prop_number_t)obj);
5289 1.1 yamaguch n = 1U << (fls32(val) - 1);
5290 1.1 yamaguch if (val != (int64_t) n) {
5291 1.1 yamaguch printf("iavf: TX desc invlaid size"
5292 1.1 yamaguch "(%" PRId64 " != %" PRIu32 ")\n", val, n);
5293 1.1 yamaguch } else if (val > (8192 - 32)) {
5294 1.1 yamaguch printf("iavf: Tx desc too big (%" PRId64 " > %d)",
5295 1.1 yamaguch val, (8192 - 32));
5296 1.1 yamaguch } else {
5297 1.1 yamaguch iavf_params.tx_ndescs = val;
5298 1.1 yamaguch printf("iavf: TX descriptors = 0x%04x",
5299 1.1 yamaguch iavf_params.tx_ndescs);
5300 1.1 yamaguch }
5301 1.1 yamaguch }
5302 1.1 yamaguch
5303 1.1 yamaguch obj = prop_dictionary_get(dict, "rx_ndescs");
5304 1.1 yamaguch if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
5305 1.1 yamaguch val = prop_number_signed_value((prop_number_t)obj);
5306 1.1 yamaguch n = 1U << (fls32(val) - 1);
5307 1.1 yamaguch if (val != (int64_t) n) {
5308 1.1 yamaguch printf("iavf: RX desc invlaid size"
5309 1.1 yamaguch "(%" PRId64 " != %" PRIu32 ")\n", val, n);
5310 1.1 yamaguch } else if (val > (8192 - 32)) {
5311 1.1 yamaguch printf("iavf: Rx desc too big (%" PRId64 " > %d)",
5312 1.1 yamaguch val, (8192 - 32));
5313 1.1 yamaguch } else {
5314 1.1 yamaguch iavf_params.rx_ndescs = val;
5315 1.1 yamaguch printf("iavf: RX descriptors = 0x%04x",
5316 1.1 yamaguch iavf_params.rx_ndescs);
5317 1.1 yamaguch }
5318 1.1 yamaguch }
5319 1.1 yamaguch }
5320 1.1 yamaguch #endif
5321 1.1 yamaguch
5322 1.1 yamaguch static int
5323 1.1 yamaguch if_iavf_modcmd(modcmd_t cmd, void *opaque)
5324 1.1 yamaguch {
5325 1.1 yamaguch int error = 0;
5326 1.1 yamaguch
5327 1.1 yamaguch #ifdef _MODULE
5328 1.1 yamaguch switch (cmd) {
5329 1.1 yamaguch case MODULE_CMD_INIT:
5330 1.1 yamaguch iavf_parse_modprop((prop_dictionary_t)opaque);
5331 1.1 yamaguch error = config_init_component(cfdriver_ioconf_if_iavf,
5332 1.1 yamaguch cfattach_ioconf_if_iavf, cfdata_ioconf_if_iavf);
5333 1.1 yamaguch break;
5334 1.1 yamaguch case MODULE_CMD_FINI:
5335 1.1 yamaguch error = config_fini_component(cfdriver_ioconf_if_iavf,
5336 1.1 yamaguch cfattach_ioconf_if_iavf, cfdata_ioconf_if_iavf);
5337 1.1 yamaguch break;
5338 1.1 yamaguch default:
5339 1.1 yamaguch error = ENOTTY;
5340 1.1 yamaguch break;
5341 1.1 yamaguch }
5342 1.1 yamaguch #endif
5343 1.1 yamaguch
5344 1.1 yamaguch return error;
5345 1.1 yamaguch }
5346