2 * Copyright (c) 2010-2012 Citrix Inc.
3 * Copyright (c) 2009-2012,2016 Microsoft Corp.
4 * Copyright (c) 2012 NetApp Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 2004-2006 Kip Macy
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
58 #include "opt_inet6.h"
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/sockio.h>
65 #include <sys/malloc.h>
66 #include <sys/module.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
70 #include <sys/queue.h>
73 #include <sys/sysctl.h>
74 #include <sys/buf_ring.h>
77 #include <net/if_arp.h>
78 #include <net/ethernet.h>
79 #include <net/if_dl.h>
80 #include <net/if_media.h>
84 #include <net/if_types.h>
85 #include <net/if_vlan_var.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/in.h>
90 #include <netinet/ip.h>
91 #include <netinet/if_ether.h>
92 #include <netinet/tcp.h>
93 #include <netinet/udp.h>
94 #include <netinet/ip6.h>
97 #include <vm/vm_param.h>
98 #include <vm/vm_kern.h>
101 #include <machine/bus.h>
102 #include <machine/resource.h>
103 #include <machine/frame.h>
104 #include <machine/vmparam.h>
107 #include <sys/rman.h>
108 #include <sys/mutex.h>
109 #include <sys/errno.h>
110 #include <sys/types.h>
111 #include <machine/atomic.h>
113 #include <machine/intr_machdep.h>
115 #include <machine/in_cksum.h>
117 #include <dev/hyperv/include/hyperv.h>
118 #include <dev/hyperv/include/hyperv_busdma.h>
120 #include "hv_net_vsc.h"
121 #include "hv_rndis.h"
122 #include "hv_rndis_filter.h"
123 #include "vmbus_if.h"
125 #define hv_chan_rxr hv_chan_priv1
126 #define hv_chan_txr hv_chan_priv2
128 /* Short for Hyper-V network interface */
129 #define NETVSC_DEVNAME "hn"
132 * It looks like offset 0 of buf is reserved to hold the softc pointer.
133 * The sc pointer evidently not needed, and is not presently populated.
134 * The packet offset is where the netvsc_packet starts in the buffer.
136 #define HV_NV_SC_PTR_OFFSET_IN_BUF 0
137 #define HV_NV_PACKET_OFFSET_IN_BUF 16
139 /* YYY should get it from the underlying channel */
140 #define HN_TX_DESC_CNT 512
142 #define HN_LROENT_CNT_DEF 128
144 #define HN_RING_CNT_DEF_MAX 8
146 #define HN_RNDIS_MSG_LEN \
147 (sizeof(rndis_msg) + \
148 RNDIS_HASHVAL_PPI_SIZE + \
149 RNDIS_VLAN_PPI_SIZE + \
150 RNDIS_TSO_PPI_SIZE + \
152 #define HN_RNDIS_MSG_BOUNDARY PAGE_SIZE
153 #define HN_RNDIS_MSG_ALIGN CACHE_LINE_SIZE
155 #define HN_TX_DATA_BOUNDARY PAGE_SIZE
156 #define HN_TX_DATA_MAXSIZE IP_MAXPACKET
157 #define HN_TX_DATA_SEGSIZE PAGE_SIZE
158 #define HN_TX_DATA_SEGCNT_MAX \
159 (NETVSC_PACKET_MAXPAGE - HV_RF_NUM_TX_RESERVED_PAGE_BUFS)
161 #define HN_DIRECT_TX_SIZE_DEF 128
163 #define HN_EARLY_TXEOF_THRESH 8
166 #ifndef HN_USE_TXDESC_BUFRING
167 SLIST_ENTRY(hn_txdesc) link;
170 struct hn_tx_ring *txr;
172 uint32_t flags; /* HN_TXD_FLAG_ */
173 netvsc_packet netvsc_pkt; /* XXX to be removed */
175 bus_dmamap_t data_dmap;
177 bus_addr_t rndis_msg_paddr;
178 rndis_msg *rndis_msg;
179 bus_dmamap_t rndis_msg_dmap;
182 #define HN_TXD_FLAG_ONLIST 0x1
183 #define HN_TXD_FLAG_DMAMAP 0x2
186 * Only enable UDP checksum offloading when it is on 2012R2 or
187 * later. UDP checksum offloading doesn't work on earlier
190 #define HN_CSUM_ASSIST_WIN8 (CSUM_IP | CSUM_TCP)
191 #define HN_CSUM_ASSIST (CSUM_IP | CSUM_UDP | CSUM_TCP)
193 #define HN_LRO_LENLIM_MULTIRX_DEF (12 * ETHERMTU)
194 #define HN_LRO_LENLIM_DEF (25 * ETHERMTU)
195 /* YYY 2*MTU is a bit rough, but should be good enough. */
196 #define HN_LRO_LENLIM_MIN(ifp) (2 * (ifp)->if_mtu)
198 #define HN_LRO_ACKCNT_DEF 1
201 * Be aware that this sleepable mutex will exhibit WITNESS errors when
202 * certain TCP and ARP code paths are taken. This appears to be a
203 * well-known condition, as all other drivers checked use a sleeping
204 * mutex to protect their transmit paths.
205 * Also Be aware that mutexes do not play well with semaphores, and there
206 * is a conflicting semaphore in a certain channel code path.
208 #define NV_LOCK_INIT(_sc, _name) \
209 mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
210 #define NV_LOCK(_sc) mtx_lock(&(_sc)->hn_lock)
211 #define NV_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->hn_lock, MA_OWNED)
212 #define NV_UNLOCK(_sc) mtx_unlock(&(_sc)->hn_lock)
213 #define NV_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->hn_lock)
220 int hv_promisc_mode = 0; /* normal mode by default */
222 SYSCTL_NODE(_hw, OID_AUTO, hn, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
223 "Hyper-V network interface");
225 /* Trust tcp segements verification on host side. */
226 static int hn_trust_hosttcp = 1;
227 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hosttcp, CTLFLAG_RDTUN,
228 &hn_trust_hosttcp, 0,
229 "Trust tcp segement verification on host side, "
230 "when csum info is missing (global setting)");
232 /* Trust udp datagrams verification on host side. */
233 static int hn_trust_hostudp = 1;
234 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostudp, CTLFLAG_RDTUN,
235 &hn_trust_hostudp, 0,
236 "Trust udp datagram verification on host side, "
237 "when csum info is missing (global setting)");
239 /* Trust ip packets verification on host side. */
240 static int hn_trust_hostip = 1;
241 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostip, CTLFLAG_RDTUN,
243 "Trust ip packet verification on host side, "
244 "when csum info is missing (global setting)");
246 /* Limit TSO burst size */
247 static int hn_tso_maxlen = 0;
248 SYSCTL_INT(_hw_hn, OID_AUTO, tso_maxlen, CTLFLAG_RDTUN,
249 &hn_tso_maxlen, 0, "TSO burst limit");
251 /* Limit chimney send size */
252 static int hn_tx_chimney_size = 0;
253 SYSCTL_INT(_hw_hn, OID_AUTO, tx_chimney_size, CTLFLAG_RDTUN,
254 &hn_tx_chimney_size, 0, "Chimney send packet size limit");
256 /* Limit the size of packet for direct transmission */
257 static int hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
258 SYSCTL_INT(_hw_hn, OID_AUTO, direct_tx_size, CTLFLAG_RDTUN,
259 &hn_direct_tx_size, 0, "Size of the packet for direct transmission");
261 #if defined(INET) || defined(INET6)
262 #if __FreeBSD_version >= 1100095
263 static int hn_lro_entry_count = HN_LROENT_CNT_DEF;
264 SYSCTL_INT(_hw_hn, OID_AUTO, lro_entry_count, CTLFLAG_RDTUN,
265 &hn_lro_entry_count, 0, "LRO entry count");
269 static int hn_share_tx_taskq = 0;
270 SYSCTL_INT(_hw_hn, OID_AUTO, share_tx_taskq, CTLFLAG_RDTUN,
271 &hn_share_tx_taskq, 0, "Enable shared TX taskqueue");
273 static struct taskqueue *hn_tx_taskq;
275 #ifndef HN_USE_TXDESC_BUFRING
276 static int hn_use_txdesc_bufring = 0;
278 static int hn_use_txdesc_bufring = 1;
280 SYSCTL_INT(_hw_hn, OID_AUTO, use_txdesc_bufring, CTLFLAG_RD,
281 &hn_use_txdesc_bufring, 0, "Use buf_ring for TX descriptors");
283 static int hn_bind_tx_taskq = -1;
284 SYSCTL_INT(_hw_hn, OID_AUTO, bind_tx_taskq, CTLFLAG_RDTUN,
285 &hn_bind_tx_taskq, 0, "Bind TX taskqueue to the specified cpu");
287 static int hn_use_if_start = 0;
288 SYSCTL_INT(_hw_hn, OID_AUTO, use_if_start, CTLFLAG_RDTUN,
289 &hn_use_if_start, 0, "Use if_start TX method");
291 static int hn_chan_cnt = 0;
292 SYSCTL_INT(_hw_hn, OID_AUTO, chan_cnt, CTLFLAG_RDTUN,
294 "# of channels to use; each channel has one RX ring and one TX ring");
296 static int hn_tx_ring_cnt = 0;
297 SYSCTL_INT(_hw_hn, OID_AUTO, tx_ring_cnt, CTLFLAG_RDTUN,
298 &hn_tx_ring_cnt, 0, "# of TX rings to use");
300 static int hn_tx_swq_depth = 0;
301 SYSCTL_INT(_hw_hn, OID_AUTO, tx_swq_depth, CTLFLAG_RDTUN,
302 &hn_tx_swq_depth, 0, "Depth of IFQ or BUFRING");
304 #if __FreeBSD_version >= 1100095
305 static u_int hn_lro_mbufq_depth = 0;
306 SYSCTL_UINT(_hw_hn, OID_AUTO, lro_mbufq_depth, CTLFLAG_RDTUN,
307 &hn_lro_mbufq_depth, 0, "Depth of LRO mbuf queue");
310 static u_int hn_cpu_index;
313 * Forward declarations
315 static void hn_stop(hn_softc_t *sc);
316 static void hn_ifinit_locked(hn_softc_t *sc);
317 static void hn_ifinit(void *xsc);
318 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
319 static int hn_start_locked(struct hn_tx_ring *txr, int len);
320 static void hn_start(struct ifnet *ifp);
321 static void hn_start_txeof(struct hn_tx_ring *);
322 static int hn_ifmedia_upd(struct ifnet *ifp);
323 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
324 #if __FreeBSD_version >= 1100099
325 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
326 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
328 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
329 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
330 #if __FreeBSD_version < 1100095
331 static int hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS);
333 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
335 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
336 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
337 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
338 static int hn_check_iplen(const struct mbuf *, int);
339 static int hn_create_tx_ring(struct hn_softc *, int);
340 static void hn_destroy_tx_ring(struct hn_tx_ring *);
341 static int hn_create_tx_data(struct hn_softc *, int);
342 static void hn_destroy_tx_data(struct hn_softc *);
343 static void hn_start_taskfunc(void *, int);
344 static void hn_start_txeof_taskfunc(void *, int);
345 static void hn_stop_tx_tasks(struct hn_softc *);
346 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
347 static void hn_create_rx_data(struct hn_softc *sc, int);
348 static void hn_destroy_rx_data(struct hn_softc *sc);
349 static void hn_set_tx_chimney_size(struct hn_softc *, int);
350 static void hn_channel_attach(struct hn_softc *, struct hv_vmbus_channel *);
351 static void hn_subchan_attach(struct hn_softc *, struct hv_vmbus_channel *);
352 static void hn_subchan_setup(struct hn_softc *);
354 static int hn_transmit(struct ifnet *, struct mbuf *);
355 static void hn_xmit_qflush(struct ifnet *);
356 static int hn_xmit(struct hn_tx_ring *, int);
357 static void hn_xmit_txeof(struct hn_tx_ring *);
358 static void hn_xmit_taskfunc(void *, int);
359 static void hn_xmit_txeof_taskfunc(void *, int);
361 #if __FreeBSD_version >= 1100099
363 hn_set_lro_lenlim(struct hn_softc *sc, int lenlim)
367 for (i = 0; i < sc->hn_rx_ring_inuse; ++i)
368 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
373 hn_get_txswq_depth(const struct hn_tx_ring *txr)
376 KASSERT(txr->hn_txdesc_cnt > 0, ("tx ring is not setup yet"));
377 if (hn_tx_swq_depth < txr->hn_txdesc_cnt)
378 return txr->hn_txdesc_cnt;
379 return hn_tx_swq_depth;
383 hn_ifmedia_upd(struct ifnet *ifp __unused)
390 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
392 struct hn_softc *sc = ifp->if_softc;
394 ifmr->ifm_status = IFM_AVALID;
395 ifmr->ifm_active = IFM_ETHER;
397 if (!sc->hn_carrier) {
398 ifmr->ifm_active |= IFM_NONE;
401 ifmr->ifm_status |= IFM_ACTIVE;
402 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
405 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
406 static const struct hyperv_guid g_net_vsc_device_type = {
407 .hv_guid = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
408 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
412 * Standard probe entry point.
416 netvsc_probe(device_t dev)
418 if (VMBUS_PROBE_GUID(device_get_parent(dev), dev,
419 &g_net_vsc_device_type) == 0) {
420 device_set_desc(dev, "Hyper-V Network Interface");
421 return BUS_PROBE_DEFAULT;
427 hn_cpuset_setthread_task(void *xmask, int pending __unused)
429 cpuset_t *mask = xmask;
432 error = cpuset_setthread(curthread->td_tid, mask);
434 panic("curthread=%ju: can't pin; error=%d",
435 (uintmax_t)curthread->td_tid, error);
440 * Standard attach entry point.
442 * Called when the driver is loaded. It allocates needed resources,
443 * and initializes the "hardware" and software.
446 netvsc_attach(device_t dev)
448 netvsc_device_info device_info;
450 int unit = device_get_unit(dev);
451 struct ifnet *ifp = NULL;
452 int error, ring_cnt, tx_ring_cnt;
455 sc = device_get_softc(dev);
459 sc->hn_prichan = vmbus_get_channel(dev);
461 if (hn_tx_taskq == NULL) {
462 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
463 taskqueue_thread_enqueue, &sc->hn_tx_taskq);
464 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
465 device_get_nameunit(dev));
466 if (hn_bind_tx_taskq >= 0) {
467 int cpu = hn_bind_tx_taskq;
468 struct task cpuset_task;
471 if (cpu > mp_ncpus - 1)
473 CPU_SETOF(cpu, &cpu_set);
474 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task,
476 taskqueue_enqueue(sc->hn_tx_taskq, &cpuset_task);
477 taskqueue_drain(sc->hn_tx_taskq, &cpuset_task);
480 sc->hn_tx_taskq = hn_tx_taskq;
482 NV_LOCK_INIT(sc, "NetVSCLock");
484 ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
486 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
489 * Figure out the # of RX rings (ring_cnt) and the # of TX rings
490 * to use (tx_ring_cnt).
493 * The # of RX rings to use is same as the # of channels to use.
495 ring_cnt = hn_chan_cnt;
499 if (ring_cnt > HN_RING_CNT_DEF_MAX)
500 ring_cnt = HN_RING_CNT_DEF_MAX;
501 } else if (ring_cnt > mp_ncpus) {
505 tx_ring_cnt = hn_tx_ring_cnt;
506 if (tx_ring_cnt <= 0 || tx_ring_cnt > ring_cnt)
507 tx_ring_cnt = ring_cnt;
508 if (hn_use_if_start) {
509 /* ifnet.if_start only needs one TX ring. */
514 * Set the leader CPU for channels.
516 sc->hn_cpu = atomic_fetchadd_int(&hn_cpu_index, ring_cnt) % mp_ncpus;
518 error = hn_create_tx_data(sc, tx_ring_cnt);
521 hn_create_rx_data(sc, ring_cnt);
524 * Associate the first TX/RX ring w/ the primary channel.
526 hn_channel_attach(sc, sc->hn_prichan);
528 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
529 ifp->if_ioctl = hn_ioctl;
530 ifp->if_init = hn_ifinit;
531 /* needed by hv_rf_on_device_add() code */
532 ifp->if_mtu = ETHERMTU;
533 if (hn_use_if_start) {
534 int qdepth = hn_get_txswq_depth(&sc->hn_tx_ring[0]);
536 ifp->if_start = hn_start;
537 IFQ_SET_MAXLEN(&ifp->if_snd, qdepth);
538 ifp->if_snd.ifq_drv_maxlen = qdepth - 1;
539 IFQ_SET_READY(&ifp->if_snd);
541 ifp->if_transmit = hn_transmit;
542 ifp->if_qflush = hn_xmit_qflush;
545 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
546 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
547 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
548 /* XXX ifmedia_set really should do this for us */
549 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
552 * Tell upper layers that we support full VLAN capability.
554 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
555 ifp->if_capabilities |=
556 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
559 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
561 ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
563 error = hv_rf_on_device_add(sc, &device_info, ring_cnt);
566 KASSERT(sc->net_dev->num_channel > 0 &&
567 sc->net_dev->num_channel <= sc->hn_rx_ring_inuse,
568 ("invalid channel count %u, should be less than %d",
569 sc->net_dev->num_channel, sc->hn_rx_ring_inuse));
572 * Set the # of TX/RX rings that could be used according to
573 * the # of channels that host offered.
575 if (sc->hn_tx_ring_inuse > sc->net_dev->num_channel)
576 sc->hn_tx_ring_inuse = sc->net_dev->num_channel;
577 sc->hn_rx_ring_inuse = sc->net_dev->num_channel;
578 device_printf(dev, "%d TX ring, %d RX ring\n",
579 sc->hn_tx_ring_inuse, sc->hn_rx_ring_inuse);
581 if (sc->net_dev->num_channel > 1)
582 hn_subchan_setup(sc);
584 #if __FreeBSD_version >= 1100099
585 if (sc->hn_rx_ring_inuse > 1) {
587 * Reduce TCP segment aggregation limit for multiple
588 * RX rings to increase ACK timeliness.
590 hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MULTIRX_DEF);
594 if (device_info.link_state == 0) {
598 tso_maxlen = hn_tso_maxlen;
599 if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
600 tso_maxlen = IP_MAXPACKET;
602 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
603 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
604 ifp->if_hw_tsomax = tso_maxlen -
605 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
607 ether_ifattach(ifp, device_info.mac_addr);
609 if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
610 ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
612 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
613 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
614 if (hn_tx_chimney_size > 0 &&
615 hn_tx_chimney_size < sc->hn_tx_chimney_max)
616 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
620 hn_destroy_tx_data(sc);
627 * Standard detach entry point
630 netvsc_detach(device_t dev)
632 struct hn_softc *sc = device_get_softc(dev);
635 printf("netvsc_detach\n");
638 * XXXKYS: Need to clean up all our
639 * driver state; this is the driver
644 * XXXKYS: Need to stop outgoing traffic and unregister
648 hv_rf_on_device_remove(sc, HV_RF_NV_DESTROY_CHANNEL);
650 hn_stop_tx_tasks(sc);
652 ifmedia_removeall(&sc->hn_media);
653 hn_destroy_rx_data(sc);
654 hn_destroy_tx_data(sc);
656 if (sc->hn_tx_taskq != hn_tx_taskq)
657 taskqueue_free(sc->hn_tx_taskq);
663 * Standard shutdown entry point
666 netvsc_shutdown(device_t dev)
672 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
673 struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
675 struct mbuf *m = *m_head;
678 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
679 m, segs, nsegs, BUS_DMA_NOWAIT);
680 if (error == EFBIG) {
683 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
688 txr->hn_tx_collapsed++;
690 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
691 txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
694 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
695 BUS_DMASYNC_PREWRITE);
696 txd->flags |= HN_TXD_FLAG_DMAMAP;
702 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
705 if (txd->flags & HN_TXD_FLAG_DMAMAP) {
706 bus_dmamap_sync(txr->hn_tx_data_dtag,
707 txd->data_dmap, BUS_DMASYNC_POSTWRITE);
708 bus_dmamap_unload(txr->hn_tx_data_dtag,
710 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
715 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
718 KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
719 ("put an onlist txd %#x", txd->flags));
721 KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
722 if (atomic_fetchadd_int(&txd->refs, -1) != 1)
725 hn_txdesc_dmamap_unload(txr, txd);
726 if (txd->m != NULL) {
731 txd->flags |= HN_TXD_FLAG_ONLIST;
733 #ifndef HN_USE_TXDESC_BUFRING
734 mtx_lock_spin(&txr->hn_txlist_spin);
735 KASSERT(txr->hn_txdesc_avail >= 0 &&
736 txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
737 ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
738 txr->hn_txdesc_avail++;
739 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
740 mtx_unlock_spin(&txr->hn_txlist_spin);
742 atomic_add_int(&txr->hn_txdesc_avail, 1);
743 buf_ring_enqueue(txr->hn_txdesc_br, txd);
749 static __inline struct hn_txdesc *
750 hn_txdesc_get(struct hn_tx_ring *txr)
752 struct hn_txdesc *txd;
754 #ifndef HN_USE_TXDESC_BUFRING
755 mtx_lock_spin(&txr->hn_txlist_spin);
756 txd = SLIST_FIRST(&txr->hn_txlist);
758 KASSERT(txr->hn_txdesc_avail > 0,
759 ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
760 txr->hn_txdesc_avail--;
761 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
763 mtx_unlock_spin(&txr->hn_txlist_spin);
765 txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
769 #ifdef HN_USE_TXDESC_BUFRING
770 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
772 KASSERT(txd->m == NULL && txd->refs == 0 &&
773 (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
774 txd->flags &= ~HN_TXD_FLAG_ONLIST;
781 hn_txdesc_hold(struct hn_txdesc *txd)
784 /* 0->1 transition will never work */
785 KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
786 atomic_add_int(&txd->refs, 1);
790 hn_txeof(struct hn_tx_ring *txr)
792 txr->hn_has_txeof = 0;
797 hn_tx_done(struct hv_vmbus_channel *chan, void *xpkt)
799 netvsc_packet *packet = xpkt;
800 struct hn_txdesc *txd;
801 struct hn_tx_ring *txr;
803 txd = (struct hn_txdesc *)(uintptr_t)
804 packet->compl.send.send_completion_tid;
807 KASSERT(txr->hn_chan == chan,
808 ("channel mismatch, on channel%u, should be channel%u",
810 txr->hn_chan->ch_subidx));
812 txr->hn_has_txeof = 1;
813 hn_txdesc_put(txr, txd);
815 ++txr->hn_txdone_cnt;
816 if (txr->hn_txdone_cnt >= HN_EARLY_TXEOF_THRESH) {
817 txr->hn_txdone_cnt = 0;
824 netvsc_channel_rollup(struct hv_vmbus_channel *chan)
826 struct hn_tx_ring *txr = chan->hv_chan_txr;
827 #if defined(INET) || defined(INET6)
828 struct hn_rx_ring *rxr = chan->hv_chan_rxr;
829 struct lro_ctrl *lro = &rxr->hn_lro;
830 struct lro_entry *queued;
832 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
833 SLIST_REMOVE_HEAD(&lro->lro_active, next);
834 tcp_lro_flush(lro, queued);
840 * 'txr' could be NULL, if multiple channels and
841 * ifnet.if_start method are enabled.
843 if (txr == NULL || !txr->hn_has_txeof)
846 txr->hn_txdone_cnt = 0;
852 * If this function fails, then both txd and m_head0 will be freed.
855 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
857 bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
859 struct mbuf *m_head = *m_head0;
860 netvsc_packet *packet;
861 rndis_msg *rndis_mesg;
862 rndis_packet *rndis_pkt;
863 rndis_per_packet_info *rppi;
864 struct rndis_hash_value *hash_value;
865 uint32_t rndis_msg_size;
867 packet = &txd->netvsc_pkt;
868 packet->is_data_pkt = TRUE;
869 packet->tot_data_buf_len = m_head->m_pkthdr.len;
872 * extension points to the area reserved for the
873 * rndis_filter_packet, which is placed just after
874 * the netvsc_packet (and rppi struct, if present;
875 * length is updated later).
877 rndis_mesg = txd->rndis_msg;
878 /* XXX not necessary */
879 memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
880 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
882 rndis_pkt = &rndis_mesg->msg.packet;
883 rndis_pkt->data_offset = sizeof(rndis_packet);
884 rndis_pkt->data_length = packet->tot_data_buf_len;
885 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
887 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
890 * Set the hash value for this packet, so that the host could
891 * dispatch the TX done event for this packet back to this TX
894 rndis_msg_size += RNDIS_HASHVAL_PPI_SIZE;
895 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_HASHVAL_PPI_SIZE,
897 hash_value = (struct rndis_hash_value *)((uint8_t *)rppi +
898 rppi->per_packet_info_offset);
899 hash_value->hash_value = txr->hn_tx_idx;
901 if (m_head->m_flags & M_VLANTAG) {
902 ndis_8021q_info *rppi_vlan_info;
904 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
905 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
908 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
909 rppi->per_packet_info_offset);
910 rppi_vlan_info->u1.s1.vlan_id =
911 m_head->m_pkthdr.ether_vtag & 0xfff;
914 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
915 rndis_tcp_tso_info *tso_info;
916 struct ether_vlan_header *eh;
920 * XXX need m_pullup and use mtodo
922 eh = mtod(m_head, struct ether_vlan_header*);
923 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
924 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
926 ether_len = ETHER_HDR_LEN;
928 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
929 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
930 tcp_large_send_info);
932 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
933 rppi->per_packet_info_offset);
934 tso_info->lso_v2_xmit.type =
935 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
938 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
940 (struct ip *)(m_head->m_data + ether_len);
941 unsigned long iph_len = ip->ip_hl << 2;
943 (struct tcphdr *)((caddr_t)ip + iph_len);
945 tso_info->lso_v2_xmit.ip_version =
946 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
950 th->th_sum = in_pseudo(ip->ip_src.s_addr,
951 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
954 #if defined(INET6) && defined(INET)
959 struct ip6_hdr *ip6 = (struct ip6_hdr *)
960 (m_head->m_data + ether_len);
961 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
963 tso_info->lso_v2_xmit.ip_version =
964 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
966 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
969 tso_info->lso_v2_xmit.tcp_header_offset = 0;
970 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
971 } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
972 rndis_tcp_ip_csum_info *csum_info;
974 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
975 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
977 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
978 rppi->per_packet_info_offset);
980 csum_info->xmit.is_ipv4 = 1;
981 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
982 csum_info->xmit.ip_header_csum = 1;
984 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
985 csum_info->xmit.tcp_csum = 1;
986 csum_info->xmit.tcp_header_offset = 0;
987 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
988 csum_info->xmit.udp_csum = 1;
992 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
993 packet->tot_data_buf_len = rndis_mesg->msg_len;
996 * Chimney send, if the packet could fit into one chimney buffer.
998 if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
999 netvsc_dev *net_dev = txr->hn_sc->net_dev;
1000 uint32_t send_buf_section_idx;
1002 txr->hn_tx_chimney_tried++;
1003 send_buf_section_idx =
1004 hv_nv_get_next_send_section(net_dev);
1005 if (send_buf_section_idx !=
1006 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
1007 uint8_t *dest = ((uint8_t *)net_dev->send_buf +
1008 (send_buf_section_idx *
1009 net_dev->send_section_size));
1011 memcpy(dest, rndis_mesg, rndis_msg_size);
1012 dest += rndis_msg_size;
1013 m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
1015 packet->send_buf_section_idx = send_buf_section_idx;
1016 packet->send_buf_section_size =
1017 packet->tot_data_buf_len;
1018 packet->page_buf_count = 0;
1019 txr->hn_tx_chimney++;
1024 error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
1029 * This mbuf is not linked w/ the txd yet, so free it now.
1034 freed = hn_txdesc_put(txr, txd);
1036 ("fail to free txd upon txdma error"));
1038 txr->hn_txdma_failed++;
1039 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
1044 packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
1046 /* send packet with page buffer */
1047 packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
1048 packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
1049 packet->page_buffers[0].length = rndis_msg_size;
1052 * Fill the page buffers with mbuf info starting at index
1053 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
1055 for (i = 0; i < nsegs; ++i) {
1056 hv_vmbus_page_buffer *pb = &packet->page_buffers[
1057 i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
1059 pb->pfn = atop(segs[i].ds_addr);
1060 pb->offset = segs[i].ds_addr & PAGE_MASK;
1061 pb->length = segs[i].ds_len;
1064 packet->send_buf_section_idx =
1065 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
1066 packet->send_buf_section_size = 0;
1070 /* Set the completion routine */
1071 packet->compl.send.on_send_completion = hn_tx_done;
1072 packet->compl.send.send_completion_context = packet;
1073 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
1080 * If this function fails, then txd will be freed, but the mbuf
1081 * associated w/ the txd will _not_ be freed.
1084 hn_send_pkt(struct ifnet *ifp, struct hn_tx_ring *txr, struct hn_txdesc *txd)
1086 int error, send_failed = 0;
1090 * Make sure that txd is not freed before ETHER_BPF_MTAP.
1092 hn_txdesc_hold(txd);
1093 error = hv_nv_on_send(txr->hn_chan, &txd->netvsc_pkt);
1095 ETHER_BPF_MTAP(ifp, txd->m);
1096 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1097 if (!hn_use_if_start) {
1098 if_inc_counter(ifp, IFCOUNTER_OBYTES,
1099 txd->m->m_pkthdr.len);
1100 if (txd->m->m_flags & M_MCAST)
1101 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
1105 hn_txdesc_put(txr, txd);
1107 if (__predict_false(error)) {
1111 * This should "really rarely" happen.
1113 * XXX Too many RX to be acked or too many sideband
1114 * commands to run? Ask netvsc_channel_rollup()
1115 * to kick start later.
1117 txr->hn_has_txeof = 1;
1119 txr->hn_send_failed++;
1122 * Try sending again after set hn_has_txeof;
1123 * in case that we missed the last
1124 * netvsc_channel_rollup().
1128 if_printf(ifp, "send failed\n");
1131 * Caller will perform further processing on the
1132 * associated mbuf, so don't free it in hn_txdesc_put();
1133 * only unload it from the DMA map in hn_txdesc_put(),
1137 freed = hn_txdesc_put(txr, txd);
1139 ("fail to free txd upon send error"));
1141 txr->hn_send_failed++;
1147 * Start a transmit of one or more packets
1150 hn_start_locked(struct hn_tx_ring *txr, int len)
1152 struct hn_softc *sc = txr->hn_sc;
1153 struct ifnet *ifp = sc->hn_ifp;
1155 KASSERT(hn_use_if_start,
1156 ("hn_start_locked is called, when if_start is disabled"));
1157 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1158 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
1160 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1164 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1165 struct hn_txdesc *txd;
1166 struct mbuf *m_head;
1169 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1173 if (len > 0 && m_head->m_pkthdr.len > len) {
1175 * This sending could be time consuming; let callers
1176 * dispatch this packet sending (and sending of any
1177 * following up packets) to tx taskqueue.
1179 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1183 txd = hn_txdesc_get(txr);
1185 txr->hn_no_txdescs++;
1186 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1187 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1191 error = hn_encap(txr, txd, &m_head);
1193 /* Both txd and m_head are freed */
1197 error = hn_send_pkt(ifp, txr, txd);
1198 if (__predict_false(error)) {
1199 /* txd is freed, but m_head is not */
1200 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1201 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1209 * Link up/down notification
1212 netvsc_linkstatus_callback(struct hn_softc *sc, uint32_t status)
1222 * Append the specified data to the indicated mbuf chain,
1223 * Extend the mbuf chain if the new data does not fit in
1226 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1227 * There should be an equivalent in the kernel mbuf code,
1228 * but there does not appear to be one yet.
1230 * Differs from m_append() in that additional mbufs are
1231 * allocated with cluster size MJUMPAGESIZE, and filled
1234 * Return 1 if able to complete the job; otherwise 0.
1237 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1240 int remainder, space;
1242 for (m = m0; m->m_next != NULL; m = m->m_next)
1245 space = M_TRAILINGSPACE(m);
1248 * Copy into available space.
1250 if (space > remainder)
1252 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1257 while (remainder > 0) {
1259 * Allocate a new mbuf; could check space
1260 * and allocate a cluster instead.
1262 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1265 n->m_len = min(MJUMPAGESIZE, remainder);
1266 bcopy(cp, mtod(n, caddr_t), n->m_len);
1268 remainder -= n->m_len;
1272 if (m0->m_flags & M_PKTHDR)
1273 m0->m_pkthdr.len += len - remainder;
1275 return (remainder == 0);
1278 #if defined(INET) || defined(INET6)
1280 hn_lro_rx(struct lro_ctrl *lc, struct mbuf *m)
1282 #if __FreeBSD_version >= 1100095
1283 if (hn_lro_mbufq_depth) {
1284 tcp_lro_queue_mbuf(lc, m);
1288 return tcp_lro_rx(lc, m, 0);
1293 * Called when we receive a data packet from the "wire" on the
1296 * Note: This is no longer used as a callback
1299 netvsc_recv(struct hv_vmbus_channel *chan, netvsc_packet *packet,
1300 const rndis_tcp_ip_csum_info *csum_info,
1301 const struct rndis_hash_info *hash_info,
1302 const struct rndis_hash_value *hash_value)
1304 struct hn_rx_ring *rxr = chan->hv_chan_rxr;
1305 struct ifnet *ifp = rxr->hn_ifp;
1307 int size, do_lro = 0, do_csum = 1;
1309 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1313 * Bail out if packet contains more data than configured MTU.
1315 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1317 } else if (packet->tot_data_buf_len <= MHLEN) {
1318 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1319 if (m_new == NULL) {
1320 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1323 memcpy(mtod(m_new, void *), packet->data,
1324 packet->tot_data_buf_len);
1325 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1326 rxr->hn_small_pkts++;
1329 * Get an mbuf with a cluster. For packets 2K or less,
1330 * get a standard 2K cluster. For anything larger, get a
1331 * 4K cluster. Any buffers larger than 4K can cause problems
1332 * if looped around to the Hyper-V TX channel, so avoid them.
1335 if (packet->tot_data_buf_len > MCLBYTES) {
1337 size = MJUMPAGESIZE;
1340 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1341 if (m_new == NULL) {
1342 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1346 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1348 m_new->m_pkthdr.rcvif = ifp;
1350 if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1353 /* receive side checksum offload */
1354 if (csum_info != NULL) {
1355 /* IP csum offload */
1356 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1357 m_new->m_pkthdr.csum_flags |=
1358 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1362 /* TCP/UDP csum offload */
1363 if ((csum_info->receive.tcp_csum_succeeded ||
1364 csum_info->receive.udp_csum_succeeded) && do_csum) {
1365 m_new->m_pkthdr.csum_flags |=
1366 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1367 m_new->m_pkthdr.csum_data = 0xffff;
1368 if (csum_info->receive.tcp_csum_succeeded)
1374 if (csum_info->receive.ip_csum_succeeded &&
1375 csum_info->receive.tcp_csum_succeeded)
1378 const struct ether_header *eh;
1383 if (m_new->m_len < hoff)
1385 eh = mtod(m_new, struct ether_header *);
1386 etype = ntohs(eh->ether_type);
1387 if (etype == ETHERTYPE_VLAN) {
1388 const struct ether_vlan_header *evl;
1390 hoff = sizeof(*evl);
1391 if (m_new->m_len < hoff)
1393 evl = mtod(m_new, struct ether_vlan_header *);
1394 etype = ntohs(evl->evl_proto);
1397 if (etype == ETHERTYPE_IP) {
1400 pr = hn_check_iplen(m_new, hoff);
1401 if (pr == IPPROTO_TCP) {
1403 (rxr->hn_trust_hcsum &
1404 HN_TRUST_HCSUM_TCP)) {
1405 rxr->hn_csum_trusted++;
1406 m_new->m_pkthdr.csum_flags |=
1407 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1408 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1409 m_new->m_pkthdr.csum_data = 0xffff;
1412 } else if (pr == IPPROTO_UDP) {
1414 (rxr->hn_trust_hcsum &
1415 HN_TRUST_HCSUM_UDP)) {
1416 rxr->hn_csum_trusted++;
1417 m_new->m_pkthdr.csum_flags |=
1418 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1419 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1420 m_new->m_pkthdr.csum_data = 0xffff;
1422 } else if (pr != IPPROTO_DONE && do_csum &&
1423 (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1424 rxr->hn_csum_trusted++;
1425 m_new->m_pkthdr.csum_flags |=
1426 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1431 if ((packet->vlan_tci != 0) &&
1432 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1433 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1434 m_new->m_flags |= M_VLANTAG;
1437 if (hash_info != NULL && hash_value != NULL) {
1438 int hash_type = M_HASHTYPE_OPAQUE;
1441 m_new->m_pkthdr.flowid = hash_value->hash_value;
1442 if ((hash_info->hash_info & NDIS_HASH_FUNCTION_MASK) ==
1443 NDIS_HASH_FUNCTION_TOEPLITZ) {
1445 (hash_info->hash_info & NDIS_HASH_TYPE_MASK);
1448 case NDIS_HASH_IPV4:
1449 hash_type = M_HASHTYPE_RSS_IPV4;
1452 case NDIS_HASH_TCP_IPV4:
1453 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
1456 case NDIS_HASH_IPV6:
1457 hash_type = M_HASHTYPE_RSS_IPV6;
1460 case NDIS_HASH_IPV6_EX:
1461 hash_type = M_HASHTYPE_RSS_IPV6_EX;
1464 case NDIS_HASH_TCP_IPV6:
1465 hash_type = M_HASHTYPE_RSS_TCP_IPV6;
1468 case NDIS_HASH_TCP_IPV6_EX:
1469 hash_type = M_HASHTYPE_RSS_TCP_IPV6_EX;
1473 M_HASHTYPE_SET(m_new, hash_type);
1475 if (hash_value != NULL)
1476 m_new->m_pkthdr.flowid = hash_value->hash_value;
1478 m_new->m_pkthdr.flowid = rxr->hn_rx_idx;
1479 M_HASHTYPE_SET(m_new, M_HASHTYPE_OPAQUE);
1483 * Note: Moved RX completion back to hv_nv_on_receive() so all
1484 * messages (not just data messages) will trigger a response.
1490 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1491 #if defined(INET) || defined(INET6)
1492 struct lro_ctrl *lro = &rxr->hn_lro;
1495 rxr->hn_lro_tried++;
1496 if (hn_lro_rx(lro, m_new) == 0) {
1504 /* We're not holding the lock here, so don't release it */
1505 (*ifp->if_input)(ifp, m_new);
1511 * Rules for using sc->temp_unusable:
1512 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
1513 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
1514 * sc->temp_unusable set, must release NV_LOCK() and exit
1515 * 3. to retain exclusive control of the interface,
1516 * sc->temp_unusable must be set by code before releasing NV_LOCK()
1517 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
1518 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
1522 * Standard ioctl entry point. Called when the user wants to configure
1526 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1528 hn_softc_t *sc = ifp->if_softc;
1529 struct ifreq *ifr = (struct ifreq *)data;
1531 struct ifaddr *ifa = (struct ifaddr *)data;
1533 netvsc_device_info device_info;
1534 int mask, error = 0;
1535 int retry_cnt = 500;
1541 if (ifa->ifa_addr->sa_family == AF_INET) {
1542 ifp->if_flags |= IFF_UP;
1543 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1545 arp_ifinit(ifp, ifa);
1548 error = ether_ioctl(ifp, cmd, data);
1551 /* Check MTU value change */
1552 if (ifp->if_mtu == ifr->ifr_mtu)
1555 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1560 /* Obtain and record requested MTU */
1561 ifp->if_mtu = ifr->ifr_mtu;
1563 #if __FreeBSD_version >= 1100099
1565 * Make sure that LRO aggregation length limit is still
1566 * valid, after the MTU change.
1569 if (sc->hn_rx_ring[0].hn_lro.lro_length_lim <
1570 HN_LRO_LENLIM_MIN(ifp))
1571 hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MIN(ifp));
1577 if (!sc->temp_unusable) {
1578 sc->temp_unusable = TRUE;
1582 if (retry_cnt > 0) {
1586 } while (retry_cnt > 0);
1588 if (retry_cnt == 0) {
1593 /* We must remove and add back the device to cause the new
1594 * MTU to take effect. This includes tearing down, but not
1595 * deleting the channel, then bringing it back up.
1597 error = hv_rf_on_device_remove(sc, HV_RF_NV_RETAIN_CHANNEL);
1600 sc->temp_unusable = FALSE;
1605 /* Wait for subchannels to be destroyed */
1606 vmbus_drain_subchan(sc->hn_prichan);
1608 error = hv_rf_on_device_add(sc, &device_info,
1609 sc->hn_rx_ring_inuse);
1612 sc->temp_unusable = FALSE;
1616 KASSERT(sc->hn_rx_ring_cnt == sc->net_dev->num_channel,
1617 ("RX ring count %d and channel count %u mismatch",
1618 sc->hn_rx_ring_cnt, sc->net_dev->num_channel));
1619 if (sc->net_dev->num_channel > 1) {
1623 * Skip the rings on primary channel; they are
1624 * handled by the hv_rf_on_device_add() above.
1626 for (r = 1; r < sc->hn_rx_ring_cnt; ++r) {
1627 sc->hn_rx_ring[r].hn_rx_flags &=
1628 ~HN_RX_FLAG_ATTACHED;
1630 for (r = 1; r < sc->hn_tx_ring_cnt; ++r) {
1631 sc->hn_tx_ring[r].hn_tx_flags &=
1632 ~HN_TX_FLAG_ATTACHED;
1634 hn_subchan_setup(sc);
1637 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1638 if (sc->hn_tx_ring[0].hn_tx_chimney_size >
1639 sc->hn_tx_chimney_max)
1640 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
1642 hn_ifinit_locked(sc);
1645 sc->temp_unusable = FALSE;
1651 if (!sc->temp_unusable) {
1652 sc->temp_unusable = TRUE;
1656 if (retry_cnt > 0) {
1660 } while (retry_cnt > 0);
1662 if (retry_cnt == 0) {
1667 if (ifp->if_flags & IFF_UP) {
1669 * If only the state of the PROMISC flag changed,
1670 * then just use the 'set promisc mode' command
1671 * instead of reinitializing the entire NIC. Doing
1672 * a full re-init means reloading the firmware and
1673 * waiting for it to start up, which may take a
1677 /* Fixme: Promiscuous mode? */
1678 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1679 ifp->if_flags & IFF_PROMISC &&
1680 !(sc->hn_if_flags & IFF_PROMISC)) {
1681 /* do something here for Hyper-V */
1682 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1683 !(ifp->if_flags & IFF_PROMISC) &&
1684 sc->hn_if_flags & IFF_PROMISC) {
1685 /* do something here for Hyper-V */
1688 hn_ifinit_locked(sc);
1690 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1695 sc->temp_unusable = FALSE;
1697 sc->hn_if_flags = ifp->if_flags;
1703 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1704 if (mask & IFCAP_TXCSUM) {
1705 ifp->if_capenable ^= IFCAP_TXCSUM;
1706 if (ifp->if_capenable & IFCAP_TXCSUM) {
1708 sc->hn_tx_ring[0].hn_csum_assist;
1711 ~sc->hn_tx_ring[0].hn_csum_assist;
1715 if (mask & IFCAP_RXCSUM)
1716 ifp->if_capenable ^= IFCAP_RXCSUM;
1718 if (mask & IFCAP_LRO)
1719 ifp->if_capenable ^= IFCAP_LRO;
1721 if (mask & IFCAP_TSO4) {
1722 ifp->if_capenable ^= IFCAP_TSO4;
1723 if (ifp->if_capenable & IFCAP_TSO4)
1724 ifp->if_hwassist |= CSUM_IP_TSO;
1726 ifp->if_hwassist &= ~CSUM_IP_TSO;
1729 if (mask & IFCAP_TSO6) {
1730 ifp->if_capenable ^= IFCAP_TSO6;
1731 if (ifp->if_capenable & IFCAP_TSO6)
1732 ifp->if_hwassist |= CSUM_IP6_TSO;
1734 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1743 /* Fixme: Multicast mode? */
1744 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1746 netvsc_setmulti(sc);
1755 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1758 error = ether_ioctl(ifp, cmd, data);
1769 hn_stop(hn_softc_t *sc)
1777 printf(" Closing Device ...\n");
1779 atomic_clear_int(&ifp->if_drv_flags,
1780 (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1781 for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
1782 sc->hn_tx_ring[i].hn_oactive = 0;
1784 if_link_state_change(ifp, LINK_STATE_DOWN);
1785 sc->hn_initdone = 0;
1787 ret = hv_rf_on_close(sc);
1791 * FreeBSD transmit entry point
1794 hn_start(struct ifnet *ifp)
1796 struct hn_softc *sc = ifp->if_softc;
1797 struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
1799 if (txr->hn_sched_tx)
1802 if (mtx_trylock(&txr->hn_tx_lock)) {
1805 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1806 mtx_unlock(&txr->hn_tx_lock);
1811 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
1815 hn_start_txeof(struct hn_tx_ring *txr)
1817 struct hn_softc *sc = txr->hn_sc;
1818 struct ifnet *ifp = sc->hn_ifp;
1820 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1822 if (txr->hn_sched_tx)
1825 if (mtx_trylock(&txr->hn_tx_lock)) {
1828 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1829 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1830 mtx_unlock(&txr->hn_tx_lock);
1832 taskqueue_enqueue(txr->hn_tx_taskq,
1838 * Release the OACTIVE earlier, with the hope, that
1839 * others could catch up. The task will clear the
1840 * flag again with the hn_tx_lock to avoid possible
1843 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1844 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
1852 hn_ifinit_locked(hn_softc_t *sc)
1859 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1863 hv_promisc_mode = 1;
1865 ret = hv_rf_on_open(sc);
1869 sc->hn_initdone = 1;
1872 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1873 for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
1874 sc->hn_tx_ring[i].hn_oactive = 0;
1876 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1877 if_link_state_change(ifp, LINK_STATE_UP);
1884 hn_ifinit(void *xsc)
1886 hn_softc_t *sc = xsc;
1889 if (sc->temp_unusable) {
1893 sc->temp_unusable = TRUE;
1896 hn_ifinit_locked(sc);
1899 sc->temp_unusable = FALSE;
1908 hn_watchdog(struct ifnet *ifp)
1913 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1914 hn_ifinit(sc); /*???*/
1919 #if __FreeBSD_version >= 1100099
1922 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1924 struct hn_softc *sc = arg1;
1925 unsigned int lenlim;
1928 lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
1929 error = sysctl_handle_int(oidp, &lenlim, 0, req);
1930 if (error || req->newptr == NULL)
1933 if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1934 lenlim > TCP_LRO_LENGTH_MAX)
1938 hn_set_lro_lenlim(sc, lenlim);
1944 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1946 struct hn_softc *sc = arg1;
1947 int ackcnt, error, i;
1950 * lro_ackcnt_lim is append count limit,
1951 * +1 to turn it into aggregation limit.
1953 ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
1954 error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1955 if (error || req->newptr == NULL)
1958 if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1962 * Convert aggregation limit back to append
1967 for (i = 0; i < sc->hn_rx_ring_inuse; ++i)
1968 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
1976 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1978 struct hn_softc *sc = arg1;
1983 if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
1986 error = sysctl_handle_int(oidp, &on, 0, req);
1987 if (error || req->newptr == NULL)
1991 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
1992 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1995 rxr->hn_trust_hcsum |= hcsum;
1997 rxr->hn_trust_hcsum &= ~hcsum;
2004 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
2006 struct hn_softc *sc = arg1;
2007 int chimney_size, error;
2009 chimney_size = sc->hn_tx_ring[0].hn_tx_chimney_size;
2010 error = sysctl_handle_int(oidp, &chimney_size, 0, req);
2011 if (error || req->newptr == NULL)
2014 if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
2017 hn_set_tx_chimney_size(sc, chimney_size);
2021 #if __FreeBSD_version < 1100095
2023 hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS)
2025 struct hn_softc *sc = arg1;
2026 int ofs = arg2, i, error;
2027 struct hn_rx_ring *rxr;
2031 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2032 rxr = &sc->hn_rx_ring[i];
2033 stat += *((int *)((uint8_t *)rxr + ofs));
2036 error = sysctl_handle_64(oidp, &stat, 0, req);
2037 if (error || req->newptr == NULL)
2040 /* Zero out this stat. */
2041 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2042 rxr = &sc->hn_rx_ring[i];
2043 *((int *)((uint8_t *)rxr + ofs)) = 0;
2049 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
2051 struct hn_softc *sc = arg1;
2052 int ofs = arg2, i, error;
2053 struct hn_rx_ring *rxr;
2057 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2058 rxr = &sc->hn_rx_ring[i];
2059 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
2062 error = sysctl_handle_64(oidp, &stat, 0, req);
2063 if (error || req->newptr == NULL)
2066 /* Zero out this stat. */
2067 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2068 rxr = &sc->hn_rx_ring[i];
2069 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
2077 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
2079 struct hn_softc *sc = arg1;
2080 int ofs = arg2, i, error;
2081 struct hn_rx_ring *rxr;
2085 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2086 rxr = &sc->hn_rx_ring[i];
2087 stat += *((u_long *)((uint8_t *)rxr + ofs));
2090 error = sysctl_handle_long(oidp, &stat, 0, req);
2091 if (error || req->newptr == NULL)
2094 /* Zero out this stat. */
2095 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2096 rxr = &sc->hn_rx_ring[i];
2097 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
2103 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
2105 struct hn_softc *sc = arg1;
2106 int ofs = arg2, i, error;
2107 struct hn_tx_ring *txr;
2111 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2112 txr = &sc->hn_tx_ring[i];
2113 stat += *((u_long *)((uint8_t *)txr + ofs));
2116 error = sysctl_handle_long(oidp, &stat, 0, req);
2117 if (error || req->newptr == NULL)
2120 /* Zero out this stat. */
2121 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2122 txr = &sc->hn_tx_ring[i];
2123 *((u_long *)((uint8_t *)txr + ofs)) = 0;
2129 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
2131 struct hn_softc *sc = arg1;
2132 int ofs = arg2, i, error, conf;
2133 struct hn_tx_ring *txr;
2135 txr = &sc->hn_tx_ring[0];
2136 conf = *((int *)((uint8_t *)txr + ofs));
2138 error = sysctl_handle_int(oidp, &conf, 0, req);
2139 if (error || req->newptr == NULL)
2143 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2144 txr = &sc->hn_tx_ring[i];
2145 *((int *)((uint8_t *)txr + ofs)) = conf;
2153 hn_check_iplen(const struct mbuf *m, int hoff)
2155 const struct ip *ip;
2156 int len, iphlen, iplen;
2157 const struct tcphdr *th;
2158 int thoff; /* TCP data offset */
2160 len = hoff + sizeof(struct ip);
2162 /* The packet must be at least the size of an IP header. */
2163 if (m->m_pkthdr.len < len)
2164 return IPPROTO_DONE;
2166 /* The fixed IP header must reside completely in the first mbuf. */
2168 return IPPROTO_DONE;
2170 ip = mtodo(m, hoff);
2172 /* Bound check the packet's stated IP header length. */
2173 iphlen = ip->ip_hl << 2;
2174 if (iphlen < sizeof(struct ip)) /* minimum header length */
2175 return IPPROTO_DONE;
2177 /* The full IP header must reside completely in the one mbuf. */
2178 if (m->m_len < hoff + iphlen)
2179 return IPPROTO_DONE;
2181 iplen = ntohs(ip->ip_len);
2184 * Check that the amount of data in the buffers is as
2185 * at least much as the IP header would have us expect.
2187 if (m->m_pkthdr.len < hoff + iplen)
2188 return IPPROTO_DONE;
2191 * Ignore IP fragments.
2193 if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
2194 return IPPROTO_DONE;
2197 * The TCP/IP or UDP/IP header must be entirely contained within
2198 * the first fragment of a packet.
2202 if (iplen < iphlen + sizeof(struct tcphdr))
2203 return IPPROTO_DONE;
2204 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
2205 return IPPROTO_DONE;
2206 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
2207 thoff = th->th_off << 2;
2208 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
2209 return IPPROTO_DONE;
2210 if (m->m_len < hoff + iphlen + thoff)
2211 return IPPROTO_DONE;
2214 if (iplen < iphlen + sizeof(struct udphdr))
2215 return IPPROTO_DONE;
2216 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
2217 return IPPROTO_DONE;
2221 return IPPROTO_DONE;
2228 hn_create_rx_data(struct hn_softc *sc, int ring_cnt)
2230 struct sysctl_oid_list *child;
2231 struct sysctl_ctx_list *ctx;
2232 device_t dev = sc->hn_dev;
2233 #if defined(INET) || defined(INET6)
2234 #if __FreeBSD_version >= 1100095
2240 sc->hn_rx_ring_cnt = ring_cnt;
2241 sc->hn_rx_ring_inuse = sc->hn_rx_ring_cnt;
2243 sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
2244 M_NETVSC, M_WAITOK | M_ZERO);
2246 #if defined(INET) || defined(INET6)
2247 #if __FreeBSD_version >= 1100095
2248 lroent_cnt = hn_lro_entry_count;
2249 if (lroent_cnt < TCP_LRO_ENTRIES)
2250 lroent_cnt = TCP_LRO_ENTRIES;
2251 device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
2253 #endif /* INET || INET6 */
2255 ctx = device_get_sysctl_ctx(dev);
2256 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2258 /* Create dev.hn.UNIT.rx sysctl tree */
2259 sc->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "rx",
2260 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2262 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2263 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
2265 if (hn_trust_hosttcp)
2266 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
2267 if (hn_trust_hostudp)
2268 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
2269 if (hn_trust_hostip)
2270 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
2271 rxr->hn_ifp = sc->hn_ifp;
2277 #if defined(INET) || defined(INET6)
2278 #if __FreeBSD_version >= 1100095
2279 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt,
2280 hn_lro_mbufq_depth);
2282 tcp_lro_init(&rxr->hn_lro);
2283 rxr->hn_lro.ifp = sc->hn_ifp;
2285 #if __FreeBSD_version >= 1100099
2286 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
2287 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
2289 #endif /* INET || INET6 */
2291 if (sc->hn_rx_sysctl_tree != NULL) {
2295 * Create per RX ring sysctl tree:
2296 * dev.hn.UNIT.rx.RINGID
2298 snprintf(name, sizeof(name), "%d", i);
2299 rxr->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx,
2300 SYSCTL_CHILDREN(sc->hn_rx_sysctl_tree),
2301 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2303 if (rxr->hn_rx_sysctl_tree != NULL) {
2304 SYSCTL_ADD_ULONG(ctx,
2305 SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
2306 OID_AUTO, "packets", CTLFLAG_RW,
2307 &rxr->hn_pkts, "# of packets received");
2308 SYSCTL_ADD_ULONG(ctx,
2309 SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
2310 OID_AUTO, "rss_pkts", CTLFLAG_RW,
2312 "# of packets w/ RSS info received");
2317 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
2318 CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2319 __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
2320 #if __FreeBSD_version < 1100095
2321 hn_rx_stat_int_sysctl,
2323 hn_rx_stat_u64_sysctl,
2325 "LU", "LRO queued");
2326 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
2327 CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2328 __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
2329 #if __FreeBSD_version < 1100095
2330 hn_rx_stat_int_sysctl,
2332 hn_rx_stat_u64_sysctl,
2334 "LU", "LRO flushed");
2335 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
2336 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2337 __offsetof(struct hn_rx_ring, hn_lro_tried),
2338 hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
2339 #if __FreeBSD_version >= 1100099
2340 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
2341 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2342 hn_lro_lenlim_sysctl, "IU",
2343 "Max # of data bytes to be aggregated by LRO");
2344 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
2345 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2346 hn_lro_ackcnt_sysctl, "I",
2347 "Max # of ACKs to be aggregated by LRO");
2349 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
2350 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_TCP,
2351 hn_trust_hcsum_sysctl, "I",
2352 "Trust tcp segement verification on host side, "
2353 "when csum info is missing");
2354 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2355 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_UDP,
2356 hn_trust_hcsum_sysctl, "I",
2357 "Trust udp datagram verification on host side, "
2358 "when csum info is missing");
2359 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2360 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_IP,
2361 hn_trust_hcsum_sysctl, "I",
2362 "Trust ip packet verification on host side, "
2363 "when csum info is missing");
2364 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2365 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2366 __offsetof(struct hn_rx_ring, hn_csum_ip),
2367 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2368 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2369 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2370 __offsetof(struct hn_rx_ring, hn_csum_tcp),
2371 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2372 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2373 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2374 __offsetof(struct hn_rx_ring, hn_csum_udp),
2375 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2376 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2377 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2378 __offsetof(struct hn_rx_ring, hn_csum_trusted),
2379 hn_rx_stat_ulong_sysctl, "LU",
2380 "# of packets that we trust host's csum verification");
2381 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2382 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2383 __offsetof(struct hn_rx_ring, hn_small_pkts),
2384 hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2385 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_cnt",
2386 CTLFLAG_RD, &sc->hn_rx_ring_cnt, 0, "# created RX rings");
2387 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_inuse",
2388 CTLFLAG_RD, &sc->hn_rx_ring_inuse, 0, "# used RX rings");
2392 hn_destroy_rx_data(struct hn_softc *sc)
2394 #if defined(INET) || defined(INET6)
2398 if (sc->hn_rx_ring_cnt == 0)
2401 #if defined(INET) || defined(INET6)
2402 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
2403 tcp_lro_free(&sc->hn_rx_ring[i].hn_lro);
2405 free(sc->hn_rx_ring, M_NETVSC);
2406 sc->hn_rx_ring = NULL;
2408 sc->hn_rx_ring_cnt = 0;
2409 sc->hn_rx_ring_inuse = 0;
2413 hn_create_tx_ring(struct hn_softc *sc, int id)
2415 struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2416 device_t dev = sc->hn_dev;
2417 bus_dma_tag_t parent_dtag;
2422 txr->hn_tx_idx = id;
2424 #ifndef HN_USE_TXDESC_BUFRING
2425 mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2427 mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2429 txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2430 txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2431 M_NETVSC, M_WAITOK | M_ZERO);
2432 #ifndef HN_USE_TXDESC_BUFRING
2433 SLIST_INIT(&txr->hn_txlist);
2435 txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2436 M_WAITOK, &txr->hn_tx_lock);
2439 txr->hn_tx_taskq = sc->hn_tx_taskq;
2441 if (hn_use_if_start) {
2442 txr->hn_txeof = hn_start_txeof;
2443 TASK_INIT(&txr->hn_tx_task, 0, hn_start_taskfunc, txr);
2444 TASK_INIT(&txr->hn_txeof_task, 0, hn_start_txeof_taskfunc, txr);
2448 txr->hn_txeof = hn_xmit_txeof;
2449 TASK_INIT(&txr->hn_tx_task, 0, hn_xmit_taskfunc, txr);
2450 TASK_INIT(&txr->hn_txeof_task, 0, hn_xmit_txeof_taskfunc, txr);
2452 br_depth = hn_get_txswq_depth(txr);
2453 txr->hn_mbuf_br = buf_ring_alloc(br_depth, M_NETVSC,
2454 M_WAITOK, &txr->hn_tx_lock);
2457 txr->hn_direct_tx_size = hn_direct_tx_size;
2458 version = VMBUS_GET_VERSION(device_get_parent(dev), dev);
2459 if (version >= VMBUS_VERSION_WIN8_1) {
2460 txr->hn_csum_assist = HN_CSUM_ASSIST;
2462 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2464 device_printf(dev, "bus version %u.%u, "
2465 "no UDP checksum offloading\n",
2466 VMBUS_VERSION_MAJOR(version),
2467 VMBUS_VERSION_MINOR(version));
2472 * Always schedule transmission instead of trying to do direct
2473 * transmission. This one gives the best performance so far.
2475 txr->hn_sched_tx = 1;
2477 parent_dtag = bus_get_dma_tag(dev);
2479 /* DMA tag for RNDIS messages. */
2480 error = bus_dma_tag_create(parent_dtag, /* parent */
2481 HN_RNDIS_MSG_ALIGN, /* alignment */
2482 HN_RNDIS_MSG_BOUNDARY, /* boundary */
2483 BUS_SPACE_MAXADDR, /* lowaddr */
2484 BUS_SPACE_MAXADDR, /* highaddr */
2485 NULL, NULL, /* filter, filterarg */
2486 HN_RNDIS_MSG_LEN, /* maxsize */
2488 HN_RNDIS_MSG_LEN, /* maxsegsize */
2490 NULL, /* lockfunc */
2491 NULL, /* lockfuncarg */
2492 &txr->hn_tx_rndis_dtag);
2494 device_printf(dev, "failed to create rndis dmatag\n");
2498 /* DMA tag for data. */
2499 error = bus_dma_tag_create(parent_dtag, /* parent */
2501 HN_TX_DATA_BOUNDARY, /* boundary */
2502 BUS_SPACE_MAXADDR, /* lowaddr */
2503 BUS_SPACE_MAXADDR, /* highaddr */
2504 NULL, NULL, /* filter, filterarg */
2505 HN_TX_DATA_MAXSIZE, /* maxsize */
2506 HN_TX_DATA_SEGCNT_MAX, /* nsegments */
2507 HN_TX_DATA_SEGSIZE, /* maxsegsize */
2509 NULL, /* lockfunc */
2510 NULL, /* lockfuncarg */
2511 &txr->hn_tx_data_dtag);
2513 device_printf(dev, "failed to create data dmatag\n");
2517 for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2518 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2523 * Allocate and load RNDIS messages.
2525 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2526 (void **)&txd->rndis_msg,
2527 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2528 &txd->rndis_msg_dmap);
2531 "failed to allocate rndis_msg, %d\n", i);
2535 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2536 txd->rndis_msg_dmap,
2537 txd->rndis_msg, HN_RNDIS_MSG_LEN,
2538 hyperv_dma_map_paddr, &txd->rndis_msg_paddr,
2542 "failed to load rndis_msg, %d\n", i);
2543 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2544 txd->rndis_msg, txd->rndis_msg_dmap);
2548 /* DMA map for TX data. */
2549 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2553 "failed to allocate tx data dmamap\n");
2554 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2555 txd->rndis_msg_dmap);
2556 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2557 txd->rndis_msg, txd->rndis_msg_dmap);
2561 /* All set, put it to list */
2562 txd->flags |= HN_TXD_FLAG_ONLIST;
2563 #ifndef HN_USE_TXDESC_BUFRING
2564 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2566 buf_ring_enqueue(txr->hn_txdesc_br, txd);
2569 txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2571 if (sc->hn_tx_sysctl_tree != NULL) {
2572 struct sysctl_oid_list *child;
2573 struct sysctl_ctx_list *ctx;
2577 * Create per TX ring sysctl tree:
2578 * dev.hn.UNIT.tx.RINGID
2580 ctx = device_get_sysctl_ctx(dev);
2581 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2583 snprintf(name, sizeof(name), "%d", id);
2584 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2585 name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2587 if (txr->hn_tx_sysctl_tree != NULL) {
2588 child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2590 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2591 CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2592 "# of available TX descs");
2593 if (!hn_use_if_start) {
2594 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "oactive",
2595 CTLFLAG_RD, &txr->hn_oactive, 0,
2598 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "packets",
2599 CTLFLAG_RW, &txr->hn_pkts,
2600 "# of packets transmitted");
2608 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
2610 struct hn_tx_ring *txr = txd->txr;
2612 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2613 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
2615 bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_msg_dmap);
2616 bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_msg,
2617 txd->rndis_msg_dmap);
2618 bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2622 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2624 struct hn_txdesc *txd;
2626 if (txr->hn_txdesc == NULL)
2629 #ifndef HN_USE_TXDESC_BUFRING
2630 while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2631 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2632 hn_txdesc_dmamap_destroy(txd);
2635 mtx_lock(&txr->hn_tx_lock);
2636 while ((txd = buf_ring_dequeue_sc(txr->hn_txdesc_br)) != NULL)
2637 hn_txdesc_dmamap_destroy(txd);
2638 mtx_unlock(&txr->hn_tx_lock);
2641 if (txr->hn_tx_data_dtag != NULL)
2642 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2643 if (txr->hn_tx_rndis_dtag != NULL)
2644 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2646 #ifdef HN_USE_TXDESC_BUFRING
2647 buf_ring_free(txr->hn_txdesc_br, M_NETVSC);
2650 free(txr->hn_txdesc, M_NETVSC);
2651 txr->hn_txdesc = NULL;
2653 if (txr->hn_mbuf_br != NULL)
2654 buf_ring_free(txr->hn_mbuf_br, M_NETVSC);
2656 #ifndef HN_USE_TXDESC_BUFRING
2657 mtx_destroy(&txr->hn_txlist_spin);
2659 mtx_destroy(&txr->hn_tx_lock);
2663 hn_create_tx_data(struct hn_softc *sc, int ring_cnt)
2665 struct sysctl_oid_list *child;
2666 struct sysctl_ctx_list *ctx;
2669 sc->hn_tx_ring_cnt = ring_cnt;
2670 sc->hn_tx_ring_inuse = sc->hn_tx_ring_cnt;
2672 sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2673 M_NETVSC, M_WAITOK | M_ZERO);
2675 ctx = device_get_sysctl_ctx(sc->hn_dev);
2676 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2678 /* Create dev.hn.UNIT.tx sysctl tree */
2679 sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2680 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2682 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2685 error = hn_create_tx_ring(sc, i);
2690 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2691 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2692 __offsetof(struct hn_tx_ring, hn_no_txdescs),
2693 hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2694 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2695 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2696 __offsetof(struct hn_tx_ring, hn_send_failed),
2697 hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2698 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2699 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2700 __offsetof(struct hn_tx_ring, hn_txdma_failed),
2701 hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2702 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2703 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2704 __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2705 hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2706 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2707 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2708 __offsetof(struct hn_tx_ring, hn_tx_chimney),
2709 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2710 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_tried",
2711 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2712 __offsetof(struct hn_tx_ring, hn_tx_chimney_tried),
2713 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send tries");
2714 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2715 CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2716 "# of total TX descs");
2717 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2718 CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2719 "Chimney send packet size upper boundary");
2720 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2721 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2722 hn_tx_chimney_size_sysctl,
2723 "I", "Chimney send packet size limit");
2724 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2725 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2726 __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2727 hn_tx_conf_int_sysctl, "I",
2728 "Size of the packet for direct transmission");
2729 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2730 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2731 __offsetof(struct hn_tx_ring, hn_sched_tx),
2732 hn_tx_conf_int_sysctl, "I",
2733 "Always schedule transmission "
2734 "instead of doing direct transmission");
2735 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_cnt",
2736 CTLFLAG_RD, &sc->hn_tx_ring_cnt, 0, "# created TX rings");
2737 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_inuse",
2738 CTLFLAG_RD, &sc->hn_tx_ring_inuse, 0, "# used TX rings");
2744 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2749 for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
2750 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2755 hn_destroy_tx_data(struct hn_softc *sc)
2759 if (sc->hn_tx_ring_cnt == 0)
2762 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2763 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2765 free(sc->hn_tx_ring, M_NETVSC);
2766 sc->hn_tx_ring = NULL;
2768 sc->hn_tx_ring_cnt = 0;
2769 sc->hn_tx_ring_inuse = 0;
2773 hn_start_taskfunc(void *xtxr, int pending __unused)
2775 struct hn_tx_ring *txr = xtxr;
2777 mtx_lock(&txr->hn_tx_lock);
2778 hn_start_locked(txr, 0);
2779 mtx_unlock(&txr->hn_tx_lock);
2783 hn_start_txeof_taskfunc(void *xtxr, int pending __unused)
2785 struct hn_tx_ring *txr = xtxr;
2787 mtx_lock(&txr->hn_tx_lock);
2788 atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2789 hn_start_locked(txr, 0);
2790 mtx_unlock(&txr->hn_tx_lock);
2794 hn_stop_tx_tasks(struct hn_softc *sc)
2798 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2799 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2801 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_tx_task);
2802 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2807 hn_xmit(struct hn_tx_ring *txr, int len)
2809 struct hn_softc *sc = txr->hn_sc;
2810 struct ifnet *ifp = sc->hn_ifp;
2811 struct mbuf *m_head;
2813 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
2814 KASSERT(hn_use_if_start == 0,
2815 ("hn_xmit is called, when if_start is enabled"));
2817 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || txr->hn_oactive)
2820 while ((m_head = drbr_peek(ifp, txr->hn_mbuf_br)) != NULL) {
2821 struct hn_txdesc *txd;
2824 if (len > 0 && m_head->m_pkthdr.len > len) {
2826 * This sending could be time consuming; let callers
2827 * dispatch this packet sending (and sending of any
2828 * following up packets) to tx taskqueue.
2830 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2834 txd = hn_txdesc_get(txr);
2836 txr->hn_no_txdescs++;
2837 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2838 txr->hn_oactive = 1;
2842 error = hn_encap(txr, txd, &m_head);
2844 /* Both txd and m_head are freed; discard */
2845 drbr_advance(ifp, txr->hn_mbuf_br);
2849 error = hn_send_pkt(ifp, txr, txd);
2850 if (__predict_false(error)) {
2851 /* txd is freed, but m_head is not */
2852 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2853 txr->hn_oactive = 1;
2858 drbr_advance(ifp, txr->hn_mbuf_br);
2864 hn_transmit(struct ifnet *ifp, struct mbuf *m)
2866 struct hn_softc *sc = ifp->if_softc;
2867 struct hn_tx_ring *txr;
2871 * Select the TX ring based on flowid
2873 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2874 idx = m->m_pkthdr.flowid % sc->hn_tx_ring_inuse;
2875 txr = &sc->hn_tx_ring[idx];
2877 error = drbr_enqueue(ifp, txr->hn_mbuf_br, m);
2879 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
2883 if (txr->hn_oactive)
2886 if (txr->hn_sched_tx)
2889 if (mtx_trylock(&txr->hn_tx_lock)) {
2892 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2893 mtx_unlock(&txr->hn_tx_lock);
2898 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
2903 hn_xmit_qflush(struct ifnet *ifp)
2905 struct hn_softc *sc = ifp->if_softc;
2908 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2909 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2912 mtx_lock(&txr->hn_tx_lock);
2913 while ((m = buf_ring_dequeue_sc(txr->hn_mbuf_br)) != NULL)
2915 mtx_unlock(&txr->hn_tx_lock);
2921 hn_xmit_txeof(struct hn_tx_ring *txr)
2924 if (txr->hn_sched_tx)
2927 if (mtx_trylock(&txr->hn_tx_lock)) {
2930 txr->hn_oactive = 0;
2931 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2932 mtx_unlock(&txr->hn_tx_lock);
2934 taskqueue_enqueue(txr->hn_tx_taskq,
2940 * Release the oactive earlier, with the hope, that
2941 * others could catch up. The task will clear the
2942 * oactive again with the hn_tx_lock to avoid possible
2945 txr->hn_oactive = 0;
2946 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
2951 hn_xmit_taskfunc(void *xtxr, int pending __unused)
2953 struct hn_tx_ring *txr = xtxr;
2955 mtx_lock(&txr->hn_tx_lock);
2957 mtx_unlock(&txr->hn_tx_lock);
2961 hn_xmit_txeof_taskfunc(void *xtxr, int pending __unused)
2963 struct hn_tx_ring *txr = xtxr;
2965 mtx_lock(&txr->hn_tx_lock);
2966 txr->hn_oactive = 0;
2968 mtx_unlock(&txr->hn_tx_lock);
2972 hn_channel_attach(struct hn_softc *sc, struct hv_vmbus_channel *chan)
2974 struct hn_rx_ring *rxr;
2977 idx = chan->ch_subidx;
2979 KASSERT(idx >= 0 && idx < sc->hn_rx_ring_inuse,
2980 ("invalid channel index %d, should > 0 && < %d",
2981 idx, sc->hn_rx_ring_inuse));
2982 rxr = &sc->hn_rx_ring[idx];
2983 KASSERT((rxr->hn_rx_flags & HN_RX_FLAG_ATTACHED) == 0,
2984 ("RX ring %d already attached", idx));
2985 rxr->hn_rx_flags |= HN_RX_FLAG_ATTACHED;
2987 chan->hv_chan_rxr = rxr;
2989 if_printf(sc->hn_ifp, "link RX ring %d to channel%u\n",
2993 if (idx < sc->hn_tx_ring_inuse) {
2994 struct hn_tx_ring *txr = &sc->hn_tx_ring[idx];
2996 KASSERT((txr->hn_tx_flags & HN_TX_FLAG_ATTACHED) == 0,
2997 ("TX ring %d already attached", idx));
2998 txr->hn_tx_flags |= HN_TX_FLAG_ATTACHED;
3000 chan->hv_chan_txr = txr;
3001 txr->hn_chan = chan;
3003 if_printf(sc->hn_ifp, "link TX ring %d to channel%u\n",
3008 /* Bind channel to a proper CPU */
3009 vmbus_channel_cpu_set(chan, (sc->hn_cpu + idx) % mp_ncpus);
3013 hn_subchan_attach(struct hn_softc *sc, struct hv_vmbus_channel *chan)
3016 KASSERT(!VMBUS_CHAN_ISPRIMARY(chan),
3017 ("subchannel callback on primary channel"));
3018 KASSERT(chan->ch_subidx > 0,
3019 ("invalid channel subidx %u",
3021 hn_channel_attach(sc, chan);
3025 hn_subchan_setup(struct hn_softc *sc)
3027 struct hv_vmbus_channel **subchan;
3028 int subchan_cnt = sc->net_dev->num_channel - 1;
3031 /* Wait for sub-channels setup to complete. */
3032 subchan = vmbus_get_subchan(sc->hn_prichan, subchan_cnt);
3034 /* Attach the sub-channels. */
3035 for (i = 0; i < subchan_cnt; ++i) {
3036 /* NOTE: Calling order is critical. */
3037 hn_subchan_attach(sc, subchan[i]);
3038 hv_nv_subchan_attach(subchan[i]);
3041 /* Release the sub-channels */
3042 vmbus_rel_subchan(subchan, subchan_cnt);
3043 if_printf(sc->hn_ifp, "%d sub-channels setup done\n", subchan_cnt);
3047 hn_tx_taskq_create(void *arg __unused)
3049 if (!hn_share_tx_taskq)
3052 hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
3053 taskqueue_thread_enqueue, &hn_tx_taskq);
3054 taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
3055 if (hn_bind_tx_taskq >= 0) {
3056 int cpu = hn_bind_tx_taskq;
3057 struct task cpuset_task;
3060 if (cpu > mp_ncpus - 1)
3062 CPU_SETOF(cpu, &cpu_set);
3063 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task, &cpu_set);
3064 taskqueue_enqueue(hn_tx_taskq, &cpuset_task);
3065 taskqueue_drain(hn_tx_taskq, &cpuset_task);
3068 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
3069 hn_tx_taskq_create, NULL);
3072 hn_tx_taskq_destroy(void *arg __unused)
3074 if (hn_tx_taskq != NULL)
3075 taskqueue_free(hn_tx_taskq);
3077 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
3078 hn_tx_taskq_destroy, NULL);
3080 static device_method_t netvsc_methods[] = {
3081 /* Device interface */
3082 DEVMETHOD(device_probe, netvsc_probe),
3083 DEVMETHOD(device_attach, netvsc_attach),
3084 DEVMETHOD(device_detach, netvsc_detach),
3085 DEVMETHOD(device_shutdown, netvsc_shutdown),
3090 static driver_t netvsc_driver = {
3096 static devclass_t netvsc_devclass;
3098 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
3099 MODULE_VERSION(hn, 1);
3100 MODULE_DEPEND(hn, vmbus, 1, 1, 1);