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 /* Short for Hyper-V network interface */
126 #define NETVSC_DEVNAME "hn"
129 * It looks like offset 0 of buf is reserved to hold the softc pointer.
130 * The sc pointer evidently not needed, and is not presently populated.
131 * The packet offset is where the netvsc_packet starts in the buffer.
133 #define HV_NV_SC_PTR_OFFSET_IN_BUF 0
134 #define HV_NV_PACKET_OFFSET_IN_BUF 16
136 /* YYY should get it from the underlying channel */
137 #define HN_TX_DESC_CNT 512
139 #define HN_LROENT_CNT_DEF 128
141 #define HN_RING_CNT_DEF_MAX 8
143 #define HN_RNDIS_MSG_LEN \
144 (sizeof(rndis_msg) + \
145 RNDIS_HASHVAL_PPI_SIZE + \
146 RNDIS_VLAN_PPI_SIZE + \
147 RNDIS_TSO_PPI_SIZE + \
149 #define HN_RNDIS_MSG_BOUNDARY PAGE_SIZE
150 #define HN_RNDIS_MSG_ALIGN CACHE_LINE_SIZE
152 #define HN_TX_DATA_BOUNDARY PAGE_SIZE
153 #define HN_TX_DATA_MAXSIZE IP_MAXPACKET
154 #define HN_TX_DATA_SEGSIZE PAGE_SIZE
155 #define HN_TX_DATA_SEGCNT_MAX \
156 (VMBUS_CHAN_SGLIST_MAX - HV_RF_NUM_TX_RESERVED_PAGE_BUFS)
158 #define HN_DIRECT_TX_SIZE_DEF 128
160 #define HN_EARLY_TXEOF_THRESH 8
163 #ifndef HN_USE_TXDESC_BUFRING
164 SLIST_ENTRY(hn_txdesc) link;
167 struct hn_tx_ring *txr;
169 uint32_t flags; /* HN_TXD_FLAG_ */
170 netvsc_packet netvsc_pkt; /* XXX to be removed */
172 bus_dmamap_t data_dmap;
174 bus_addr_t rndis_msg_paddr;
175 rndis_msg *rndis_msg;
176 bus_dmamap_t rndis_msg_dmap;
179 #define HN_TXD_FLAG_ONLIST 0x1
180 #define HN_TXD_FLAG_DMAMAP 0x2
183 * Only enable UDP checksum offloading when it is on 2012R2 or
184 * later. UDP checksum offloading doesn't work on earlier
187 #define HN_CSUM_ASSIST_WIN8 (CSUM_IP | CSUM_TCP)
188 #define HN_CSUM_ASSIST (CSUM_IP | CSUM_UDP | CSUM_TCP)
190 #define HN_LRO_LENLIM_MULTIRX_DEF (12 * ETHERMTU)
191 #define HN_LRO_LENLIM_DEF (25 * ETHERMTU)
192 /* YYY 2*MTU is a bit rough, but should be good enough. */
193 #define HN_LRO_LENLIM_MIN(ifp) (2 * (ifp)->if_mtu)
195 #define HN_LRO_ACKCNT_DEF 1
198 * Be aware that this sleepable mutex will exhibit WITNESS errors when
199 * certain TCP and ARP code paths are taken. This appears to be a
200 * well-known condition, as all other drivers checked use a sleeping
201 * mutex to protect their transmit paths.
202 * Also Be aware that mutexes do not play well with semaphores, and there
203 * is a conflicting semaphore in a certain channel code path.
205 #define NV_LOCK_INIT(_sc, _name) \
206 mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
207 #define NV_LOCK(_sc) mtx_lock(&(_sc)->hn_lock)
208 #define NV_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->hn_lock, MA_OWNED)
209 #define NV_UNLOCK(_sc) mtx_unlock(&(_sc)->hn_lock)
210 #define NV_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->hn_lock)
217 int hv_promisc_mode = 0; /* normal mode by default */
219 SYSCTL_NODE(_hw, OID_AUTO, hn, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
220 "Hyper-V network interface");
222 /* Trust tcp segements verification on host side. */
223 static int hn_trust_hosttcp = 1;
224 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hosttcp, CTLFLAG_RDTUN,
225 &hn_trust_hosttcp, 0,
226 "Trust tcp segement verification on host side, "
227 "when csum info is missing (global setting)");
229 /* Trust udp datagrams verification on host side. */
230 static int hn_trust_hostudp = 1;
231 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostudp, CTLFLAG_RDTUN,
232 &hn_trust_hostudp, 0,
233 "Trust udp datagram verification on host side, "
234 "when csum info is missing (global setting)");
236 /* Trust ip packets verification on host side. */
237 static int hn_trust_hostip = 1;
238 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostip, CTLFLAG_RDTUN,
240 "Trust ip packet verification on host side, "
241 "when csum info is missing (global setting)");
243 /* Limit TSO burst size */
244 static int hn_tso_maxlen = 0;
245 SYSCTL_INT(_hw_hn, OID_AUTO, tso_maxlen, CTLFLAG_RDTUN,
246 &hn_tso_maxlen, 0, "TSO burst limit");
248 /* Limit chimney send size */
249 static int hn_tx_chimney_size = 0;
250 SYSCTL_INT(_hw_hn, OID_AUTO, tx_chimney_size, CTLFLAG_RDTUN,
251 &hn_tx_chimney_size, 0, "Chimney send packet size limit");
253 /* Limit the size of packet for direct transmission */
254 static int hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
255 SYSCTL_INT(_hw_hn, OID_AUTO, direct_tx_size, CTLFLAG_RDTUN,
256 &hn_direct_tx_size, 0, "Size of the packet for direct transmission");
258 #if defined(INET) || defined(INET6)
259 #if __FreeBSD_version >= 1100095
260 static int hn_lro_entry_count = HN_LROENT_CNT_DEF;
261 SYSCTL_INT(_hw_hn, OID_AUTO, lro_entry_count, CTLFLAG_RDTUN,
262 &hn_lro_entry_count, 0, "LRO entry count");
266 static int hn_share_tx_taskq = 0;
267 SYSCTL_INT(_hw_hn, OID_AUTO, share_tx_taskq, CTLFLAG_RDTUN,
268 &hn_share_tx_taskq, 0, "Enable shared TX taskqueue");
270 static struct taskqueue *hn_tx_taskq;
272 #ifndef HN_USE_TXDESC_BUFRING
273 static int hn_use_txdesc_bufring = 0;
275 static int hn_use_txdesc_bufring = 1;
277 SYSCTL_INT(_hw_hn, OID_AUTO, use_txdesc_bufring, CTLFLAG_RD,
278 &hn_use_txdesc_bufring, 0, "Use buf_ring for TX descriptors");
280 static int hn_bind_tx_taskq = -1;
281 SYSCTL_INT(_hw_hn, OID_AUTO, bind_tx_taskq, CTLFLAG_RDTUN,
282 &hn_bind_tx_taskq, 0, "Bind TX taskqueue to the specified cpu");
284 static int hn_use_if_start = 0;
285 SYSCTL_INT(_hw_hn, OID_AUTO, use_if_start, CTLFLAG_RDTUN,
286 &hn_use_if_start, 0, "Use if_start TX method");
288 static int hn_chan_cnt = 0;
289 SYSCTL_INT(_hw_hn, OID_AUTO, chan_cnt, CTLFLAG_RDTUN,
291 "# of channels to use; each channel has one RX ring and one TX ring");
293 static int hn_tx_ring_cnt = 0;
294 SYSCTL_INT(_hw_hn, OID_AUTO, tx_ring_cnt, CTLFLAG_RDTUN,
295 &hn_tx_ring_cnt, 0, "# of TX rings to use");
297 static int hn_tx_swq_depth = 0;
298 SYSCTL_INT(_hw_hn, OID_AUTO, tx_swq_depth, CTLFLAG_RDTUN,
299 &hn_tx_swq_depth, 0, "Depth of IFQ or BUFRING");
301 #if __FreeBSD_version >= 1100095
302 static u_int hn_lro_mbufq_depth = 0;
303 SYSCTL_UINT(_hw_hn, OID_AUTO, lro_mbufq_depth, CTLFLAG_RDTUN,
304 &hn_lro_mbufq_depth, 0, "Depth of LRO mbuf queue");
307 static u_int hn_cpu_index;
310 * Forward declarations
312 static void hn_stop(hn_softc_t *sc);
313 static void hn_ifinit_locked(hn_softc_t *sc);
314 static void hn_ifinit(void *xsc);
315 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
316 static int hn_start_locked(struct hn_tx_ring *txr, int len);
317 static void hn_start(struct ifnet *ifp);
318 static void hn_start_txeof(struct hn_tx_ring *);
319 static int hn_ifmedia_upd(struct ifnet *ifp);
320 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
321 #if __FreeBSD_version >= 1100099
322 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
323 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
325 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
326 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
327 #if __FreeBSD_version < 1100095
328 static int hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS);
330 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
332 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
333 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
334 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
335 static int hn_check_iplen(const struct mbuf *, int);
336 static int hn_create_tx_ring(struct hn_softc *, int);
337 static void hn_destroy_tx_ring(struct hn_tx_ring *);
338 static int hn_create_tx_data(struct hn_softc *, int);
339 static void hn_destroy_tx_data(struct hn_softc *);
340 static void hn_start_taskfunc(void *, int);
341 static void hn_start_txeof_taskfunc(void *, int);
342 static void hn_stop_tx_tasks(struct hn_softc *);
343 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
344 static void hn_create_rx_data(struct hn_softc *sc, int);
345 static void hn_destroy_rx_data(struct hn_softc *sc);
346 static void hn_set_tx_chimney_size(struct hn_softc *, int);
347 static void hn_channel_attach(struct hn_softc *, struct vmbus_channel *);
348 static void hn_subchan_attach(struct hn_softc *, struct vmbus_channel *);
349 static void hn_subchan_setup(struct hn_softc *);
351 static int hn_transmit(struct ifnet *, struct mbuf *);
352 static void hn_xmit_qflush(struct ifnet *);
353 static int hn_xmit(struct hn_tx_ring *, int);
354 static void hn_xmit_txeof(struct hn_tx_ring *);
355 static void hn_xmit_taskfunc(void *, int);
356 static void hn_xmit_txeof_taskfunc(void *, int);
358 #if __FreeBSD_version >= 1100099
360 hn_set_lro_lenlim(struct hn_softc *sc, int lenlim)
364 for (i = 0; i < sc->hn_rx_ring_inuse; ++i)
365 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
370 hn_get_txswq_depth(const struct hn_tx_ring *txr)
373 KASSERT(txr->hn_txdesc_cnt > 0, ("tx ring is not setup yet"));
374 if (hn_tx_swq_depth < txr->hn_txdesc_cnt)
375 return txr->hn_txdesc_cnt;
376 return hn_tx_swq_depth;
380 hn_ifmedia_upd(struct ifnet *ifp __unused)
387 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
389 struct hn_softc *sc = ifp->if_softc;
391 ifmr->ifm_status = IFM_AVALID;
392 ifmr->ifm_active = IFM_ETHER;
394 if (!sc->hn_carrier) {
395 ifmr->ifm_active |= IFM_NONE;
398 ifmr->ifm_status |= IFM_ACTIVE;
399 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
402 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
403 static const struct hyperv_guid g_net_vsc_device_type = {
404 .hv_guid = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
405 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
409 * Standard probe entry point.
413 netvsc_probe(device_t dev)
415 if (VMBUS_PROBE_GUID(device_get_parent(dev), dev,
416 &g_net_vsc_device_type) == 0) {
417 device_set_desc(dev, "Hyper-V Network Interface");
418 return BUS_PROBE_DEFAULT;
424 hn_cpuset_setthread_task(void *xmask, int pending __unused)
426 cpuset_t *mask = xmask;
429 error = cpuset_setthread(curthread->td_tid, mask);
431 panic("curthread=%ju: can't pin; error=%d",
432 (uintmax_t)curthread->td_tid, error);
437 * Standard attach entry point.
439 * Called when the driver is loaded. It allocates needed resources,
440 * and initializes the "hardware" and software.
443 netvsc_attach(device_t dev)
445 netvsc_device_info device_info;
447 int unit = device_get_unit(dev);
448 struct ifnet *ifp = NULL;
449 int error, ring_cnt, tx_ring_cnt;
452 sc = device_get_softc(dev);
456 sc->hn_prichan = vmbus_get_channel(dev);
458 if (hn_tx_taskq == NULL) {
459 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
460 taskqueue_thread_enqueue, &sc->hn_tx_taskq);
461 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
462 device_get_nameunit(dev));
463 if (hn_bind_tx_taskq >= 0) {
464 int cpu = hn_bind_tx_taskq;
465 struct task cpuset_task;
468 if (cpu > mp_ncpus - 1)
470 CPU_SETOF(cpu, &cpu_set);
471 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task,
473 taskqueue_enqueue(sc->hn_tx_taskq, &cpuset_task);
474 taskqueue_drain(sc->hn_tx_taskq, &cpuset_task);
477 sc->hn_tx_taskq = hn_tx_taskq;
479 NV_LOCK_INIT(sc, "NetVSCLock");
481 ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
483 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
486 * Figure out the # of RX rings (ring_cnt) and the # of TX rings
487 * to use (tx_ring_cnt).
490 * The # of RX rings to use is same as the # of channels to use.
492 ring_cnt = hn_chan_cnt;
496 if (ring_cnt > HN_RING_CNT_DEF_MAX)
497 ring_cnt = HN_RING_CNT_DEF_MAX;
498 } else if (ring_cnt > mp_ncpus) {
502 tx_ring_cnt = hn_tx_ring_cnt;
503 if (tx_ring_cnt <= 0 || tx_ring_cnt > ring_cnt)
504 tx_ring_cnt = ring_cnt;
505 if (hn_use_if_start) {
506 /* ifnet.if_start only needs one TX ring. */
511 * Set the leader CPU for channels.
513 sc->hn_cpu = atomic_fetchadd_int(&hn_cpu_index, ring_cnt) % mp_ncpus;
515 error = hn_create_tx_data(sc, tx_ring_cnt);
518 hn_create_rx_data(sc, ring_cnt);
521 * Associate the first TX/RX ring w/ the primary channel.
523 hn_channel_attach(sc, sc->hn_prichan);
525 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
526 ifp->if_ioctl = hn_ioctl;
527 ifp->if_init = hn_ifinit;
528 /* needed by hv_rf_on_device_add() code */
529 ifp->if_mtu = ETHERMTU;
530 if (hn_use_if_start) {
531 int qdepth = hn_get_txswq_depth(&sc->hn_tx_ring[0]);
533 ifp->if_start = hn_start;
534 IFQ_SET_MAXLEN(&ifp->if_snd, qdepth);
535 ifp->if_snd.ifq_drv_maxlen = qdepth - 1;
536 IFQ_SET_READY(&ifp->if_snd);
538 ifp->if_transmit = hn_transmit;
539 ifp->if_qflush = hn_xmit_qflush;
542 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
543 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
544 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
545 /* XXX ifmedia_set really should do this for us */
546 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
549 * Tell upper layers that we support full VLAN capability.
551 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
552 ifp->if_capabilities |=
553 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
556 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
558 ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
560 error = hv_rf_on_device_add(sc, &device_info, ring_cnt,
564 KASSERT(sc->net_dev->num_channel > 0 &&
565 sc->net_dev->num_channel <= sc->hn_rx_ring_inuse,
566 ("invalid channel count %u, should be less than %d",
567 sc->net_dev->num_channel, sc->hn_rx_ring_inuse));
570 * Set the # of TX/RX rings that could be used according to
571 * the # of channels that host offered.
573 if (sc->hn_tx_ring_inuse > sc->net_dev->num_channel)
574 sc->hn_tx_ring_inuse = sc->net_dev->num_channel;
575 sc->hn_rx_ring_inuse = sc->net_dev->num_channel;
576 device_printf(dev, "%d TX ring, %d RX ring\n",
577 sc->hn_tx_ring_inuse, sc->hn_rx_ring_inuse);
579 if (sc->net_dev->num_channel > 1)
580 hn_subchan_setup(sc);
582 #if __FreeBSD_version >= 1100099
583 if (sc->hn_rx_ring_inuse > 1) {
585 * Reduce TCP segment aggregation limit for multiple
586 * RX rings to increase ACK timeliness.
588 hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MULTIRX_DEF);
592 if (device_info.link_state == 0) {
596 tso_maxlen = hn_tso_maxlen;
597 if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
598 tso_maxlen = IP_MAXPACKET;
600 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
601 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
602 ifp->if_hw_tsomax = tso_maxlen -
603 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
605 ether_ifattach(ifp, device_info.mac_addr);
607 if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
608 ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
610 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
611 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
612 if (hn_tx_chimney_size > 0 &&
613 hn_tx_chimney_size < sc->hn_tx_chimney_max)
614 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
618 hn_destroy_tx_data(sc);
625 * Standard detach entry point
628 netvsc_detach(device_t dev)
630 struct hn_softc *sc = device_get_softc(dev);
633 printf("netvsc_detach\n");
636 * XXXKYS: Need to clean up all our
637 * driver state; this is the driver
642 * XXXKYS: Need to stop outgoing traffic and unregister
646 hv_rf_on_device_remove(sc, HV_RF_NV_DESTROY_CHANNEL);
648 hn_stop_tx_tasks(sc);
650 ifmedia_removeall(&sc->hn_media);
651 hn_destroy_rx_data(sc);
652 hn_destroy_tx_data(sc);
654 if (sc->hn_tx_taskq != hn_tx_taskq)
655 taskqueue_free(sc->hn_tx_taskq);
661 * Standard shutdown entry point
664 netvsc_shutdown(device_t dev)
670 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
671 struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
673 struct mbuf *m = *m_head;
676 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
677 m, segs, nsegs, BUS_DMA_NOWAIT);
678 if (error == EFBIG) {
681 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
686 txr->hn_tx_collapsed++;
688 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
689 txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
692 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
693 BUS_DMASYNC_PREWRITE);
694 txd->flags |= HN_TXD_FLAG_DMAMAP;
700 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
703 if (txd->flags & HN_TXD_FLAG_DMAMAP) {
704 bus_dmamap_sync(txr->hn_tx_data_dtag,
705 txd->data_dmap, BUS_DMASYNC_POSTWRITE);
706 bus_dmamap_unload(txr->hn_tx_data_dtag,
708 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
713 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
716 KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
717 ("put an onlist txd %#x", txd->flags));
719 KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
720 if (atomic_fetchadd_int(&txd->refs, -1) != 1)
723 hn_txdesc_dmamap_unload(txr, txd);
724 if (txd->m != NULL) {
729 txd->flags |= HN_TXD_FLAG_ONLIST;
731 #ifndef HN_USE_TXDESC_BUFRING
732 mtx_lock_spin(&txr->hn_txlist_spin);
733 KASSERT(txr->hn_txdesc_avail >= 0 &&
734 txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
735 ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
736 txr->hn_txdesc_avail++;
737 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
738 mtx_unlock_spin(&txr->hn_txlist_spin);
740 atomic_add_int(&txr->hn_txdesc_avail, 1);
741 buf_ring_enqueue(txr->hn_txdesc_br, txd);
747 static __inline struct hn_txdesc *
748 hn_txdesc_get(struct hn_tx_ring *txr)
750 struct hn_txdesc *txd;
752 #ifndef HN_USE_TXDESC_BUFRING
753 mtx_lock_spin(&txr->hn_txlist_spin);
754 txd = SLIST_FIRST(&txr->hn_txlist);
756 KASSERT(txr->hn_txdesc_avail > 0,
757 ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
758 txr->hn_txdesc_avail--;
759 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
761 mtx_unlock_spin(&txr->hn_txlist_spin);
763 txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
767 #ifdef HN_USE_TXDESC_BUFRING
768 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
770 KASSERT(txd->m == NULL && txd->refs == 0 &&
771 (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
772 txd->flags &= ~HN_TXD_FLAG_ONLIST;
779 hn_txdesc_hold(struct hn_txdesc *txd)
782 /* 0->1 transition will never work */
783 KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
784 atomic_add_int(&txd->refs, 1);
788 hn_txeof(struct hn_tx_ring *txr)
790 txr->hn_has_txeof = 0;
795 hn_tx_done(struct vmbus_channel *chan, void *xpkt)
797 netvsc_packet *packet = xpkt;
798 struct hn_txdesc *txd;
799 struct hn_tx_ring *txr;
801 txd = (struct hn_txdesc *)(uintptr_t)
802 packet->compl.send.send_completion_tid;
805 KASSERT(txr->hn_chan == chan,
806 ("channel mismatch, on chan%u, should be chan%u",
807 vmbus_chan_subidx(chan), vmbus_chan_subidx(txr->hn_chan)));
809 txr->hn_has_txeof = 1;
810 hn_txdesc_put(txr, txd);
812 ++txr->hn_txdone_cnt;
813 if (txr->hn_txdone_cnt >= HN_EARLY_TXEOF_THRESH) {
814 txr->hn_txdone_cnt = 0;
821 netvsc_channel_rollup(struct hn_rx_ring *rxr, struct hn_tx_ring *txr)
823 #if defined(INET) || defined(INET6)
824 struct lro_ctrl *lro = &rxr->hn_lro;
825 struct lro_entry *queued;
827 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
828 SLIST_REMOVE_HEAD(&lro->lro_active, next);
829 tcp_lro_flush(lro, queued);
835 * 'txr' could be NULL, if multiple channels and
836 * ifnet.if_start method are enabled.
838 if (txr == NULL || !txr->hn_has_txeof)
841 txr->hn_txdone_cnt = 0;
847 * If this function fails, then both txd and m_head0 will be freed.
850 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
852 bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
854 struct mbuf *m_head = *m_head0;
855 netvsc_packet *packet;
856 rndis_msg *rndis_mesg;
857 rndis_packet *rndis_pkt;
858 rndis_per_packet_info *rppi;
859 struct rndis_hash_value *hash_value;
860 uint32_t rndis_msg_size;
862 packet = &txd->netvsc_pkt;
863 packet->is_data_pkt = TRUE;
864 packet->tot_data_buf_len = m_head->m_pkthdr.len;
867 * extension points to the area reserved for the
868 * rndis_filter_packet, which is placed just after
869 * the netvsc_packet (and rppi struct, if present;
870 * length is updated later).
872 rndis_mesg = txd->rndis_msg;
873 /* XXX not necessary */
874 memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
875 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
877 rndis_pkt = &rndis_mesg->msg.packet;
878 rndis_pkt->data_offset = sizeof(rndis_packet);
879 rndis_pkt->data_length = packet->tot_data_buf_len;
880 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
882 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
885 * Set the hash value for this packet, so that the host could
886 * dispatch the TX done event for this packet back to this TX
889 rndis_msg_size += RNDIS_HASHVAL_PPI_SIZE;
890 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_HASHVAL_PPI_SIZE,
892 hash_value = (struct rndis_hash_value *)((uint8_t *)rppi +
893 rppi->per_packet_info_offset);
894 hash_value->hash_value = txr->hn_tx_idx;
896 if (m_head->m_flags & M_VLANTAG) {
897 ndis_8021q_info *rppi_vlan_info;
899 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
900 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
903 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
904 rppi->per_packet_info_offset);
905 rppi_vlan_info->u1.s1.vlan_id =
906 m_head->m_pkthdr.ether_vtag & 0xfff;
909 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
910 rndis_tcp_tso_info *tso_info;
911 struct ether_vlan_header *eh;
915 * XXX need m_pullup and use mtodo
917 eh = mtod(m_head, struct ether_vlan_header*);
918 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
919 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
921 ether_len = ETHER_HDR_LEN;
923 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
924 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
925 tcp_large_send_info);
927 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
928 rppi->per_packet_info_offset);
929 tso_info->lso_v2_xmit.type =
930 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
933 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
935 (struct ip *)(m_head->m_data + ether_len);
936 unsigned long iph_len = ip->ip_hl << 2;
938 (struct tcphdr *)((caddr_t)ip + iph_len);
940 tso_info->lso_v2_xmit.ip_version =
941 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
945 th->th_sum = in_pseudo(ip->ip_src.s_addr,
946 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
949 #if defined(INET6) && defined(INET)
954 struct ip6_hdr *ip6 = (struct ip6_hdr *)
955 (m_head->m_data + ether_len);
956 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
958 tso_info->lso_v2_xmit.ip_version =
959 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
961 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
964 tso_info->lso_v2_xmit.tcp_header_offset = 0;
965 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
966 } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
967 rndis_tcp_ip_csum_info *csum_info;
969 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
970 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
972 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
973 rppi->per_packet_info_offset);
975 csum_info->xmit.is_ipv4 = 1;
976 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
977 csum_info->xmit.ip_header_csum = 1;
979 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
980 csum_info->xmit.tcp_csum = 1;
981 csum_info->xmit.tcp_header_offset = 0;
982 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
983 csum_info->xmit.udp_csum = 1;
987 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
988 packet->tot_data_buf_len = rndis_mesg->msg_len;
991 * Chimney send, if the packet could fit into one chimney buffer.
993 if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
994 netvsc_dev *net_dev = txr->hn_sc->net_dev;
995 uint32_t send_buf_section_idx;
997 txr->hn_tx_chimney_tried++;
998 send_buf_section_idx =
999 hv_nv_get_next_send_section(net_dev);
1000 if (send_buf_section_idx !=
1001 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
1002 uint8_t *dest = ((uint8_t *)net_dev->send_buf +
1003 (send_buf_section_idx *
1004 net_dev->send_section_size));
1006 memcpy(dest, rndis_mesg, rndis_msg_size);
1007 dest += rndis_msg_size;
1008 m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
1010 packet->send_buf_section_idx = send_buf_section_idx;
1011 packet->send_buf_section_size =
1012 packet->tot_data_buf_len;
1013 packet->gpa_cnt = 0;
1014 txr->hn_tx_chimney++;
1019 error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
1024 * This mbuf is not linked w/ the txd yet, so free it now.
1029 freed = hn_txdesc_put(txr, txd);
1031 ("fail to free txd upon txdma error"));
1033 txr->hn_txdma_failed++;
1034 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
1039 packet->gpa_cnt = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
1041 /* send packet with page buffer */
1042 packet->gpa[0].gpa_page = atop(txd->rndis_msg_paddr);
1043 packet->gpa[0].gpa_ofs = txd->rndis_msg_paddr & PAGE_MASK;
1044 packet->gpa[0].gpa_len = rndis_msg_size;
1047 * Fill the page buffers with mbuf info starting at index
1048 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
1050 for (i = 0; i < nsegs; ++i) {
1051 struct vmbus_gpa *gpa = &packet->gpa[
1052 i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
1054 gpa->gpa_page = atop(segs[i].ds_addr);
1055 gpa->gpa_ofs = segs[i].ds_addr & PAGE_MASK;
1056 gpa->gpa_len = segs[i].ds_len;
1059 packet->send_buf_section_idx =
1060 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
1061 packet->send_buf_section_size = 0;
1065 /* Set the completion routine */
1066 packet->compl.send.on_send_completion = hn_tx_done;
1067 packet->compl.send.send_completion_context = packet;
1068 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
1075 * If this function fails, then txd will be freed, but the mbuf
1076 * associated w/ the txd will _not_ be freed.
1079 hn_send_pkt(struct ifnet *ifp, struct hn_tx_ring *txr, struct hn_txdesc *txd)
1081 int error, send_failed = 0;
1085 * Make sure that txd is not freed before ETHER_BPF_MTAP.
1087 hn_txdesc_hold(txd);
1088 error = hv_nv_on_send(txr->hn_chan, &txd->netvsc_pkt);
1090 ETHER_BPF_MTAP(ifp, txd->m);
1091 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1092 if (!hn_use_if_start) {
1093 if_inc_counter(ifp, IFCOUNTER_OBYTES,
1094 txd->m->m_pkthdr.len);
1095 if (txd->m->m_flags & M_MCAST)
1096 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
1100 hn_txdesc_put(txr, txd);
1102 if (__predict_false(error)) {
1106 * This should "really rarely" happen.
1108 * XXX Too many RX to be acked or too many sideband
1109 * commands to run? Ask netvsc_channel_rollup()
1110 * to kick start later.
1112 txr->hn_has_txeof = 1;
1114 txr->hn_send_failed++;
1117 * Try sending again after set hn_has_txeof;
1118 * in case that we missed the last
1119 * netvsc_channel_rollup().
1123 if_printf(ifp, "send failed\n");
1126 * Caller will perform further processing on the
1127 * associated mbuf, so don't free it in hn_txdesc_put();
1128 * only unload it from the DMA map in hn_txdesc_put(),
1132 freed = hn_txdesc_put(txr, txd);
1134 ("fail to free txd upon send error"));
1136 txr->hn_send_failed++;
1142 * Start a transmit of one or more packets
1145 hn_start_locked(struct hn_tx_ring *txr, int len)
1147 struct hn_softc *sc = txr->hn_sc;
1148 struct ifnet *ifp = sc->hn_ifp;
1150 KASSERT(hn_use_if_start,
1151 ("hn_start_locked is called, when if_start is disabled"));
1152 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1153 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
1155 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1159 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1160 struct hn_txdesc *txd;
1161 struct mbuf *m_head;
1164 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1168 if (len > 0 && m_head->m_pkthdr.len > len) {
1170 * This sending could be time consuming; let callers
1171 * dispatch this packet sending (and sending of any
1172 * following up packets) to tx taskqueue.
1174 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1178 txd = hn_txdesc_get(txr);
1180 txr->hn_no_txdescs++;
1181 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1182 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1186 error = hn_encap(txr, txd, &m_head);
1188 /* Both txd and m_head are freed */
1192 error = hn_send_pkt(ifp, txr, txd);
1193 if (__predict_false(error)) {
1194 /* txd is freed, but m_head is not */
1195 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1196 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1204 * Link up/down notification
1207 netvsc_linkstatus_callback(struct hn_softc *sc, uint32_t status)
1217 * Append the specified data to the indicated mbuf chain,
1218 * Extend the mbuf chain if the new data does not fit in
1221 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1222 * There should be an equivalent in the kernel mbuf code,
1223 * but there does not appear to be one yet.
1225 * Differs from m_append() in that additional mbufs are
1226 * allocated with cluster size MJUMPAGESIZE, and filled
1229 * Return 1 if able to complete the job; otherwise 0.
1232 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1235 int remainder, space;
1237 for (m = m0; m->m_next != NULL; m = m->m_next)
1240 space = M_TRAILINGSPACE(m);
1243 * Copy into available space.
1245 if (space > remainder)
1247 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1252 while (remainder > 0) {
1254 * Allocate a new mbuf; could check space
1255 * and allocate a cluster instead.
1257 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1260 n->m_len = min(MJUMPAGESIZE, remainder);
1261 bcopy(cp, mtod(n, caddr_t), n->m_len);
1263 remainder -= n->m_len;
1267 if (m0->m_flags & M_PKTHDR)
1268 m0->m_pkthdr.len += len - remainder;
1270 return (remainder == 0);
1273 #if defined(INET) || defined(INET6)
1275 hn_lro_rx(struct lro_ctrl *lc, struct mbuf *m)
1277 #if __FreeBSD_version >= 1100095
1278 if (hn_lro_mbufq_depth) {
1279 tcp_lro_queue_mbuf(lc, m);
1283 return tcp_lro_rx(lc, m, 0);
1288 * Called when we receive a data packet from the "wire" on the
1291 * Note: This is no longer used as a callback
1294 netvsc_recv(struct hn_rx_ring *rxr, netvsc_packet *packet,
1295 const rndis_tcp_ip_csum_info *csum_info,
1296 const struct rndis_hash_info *hash_info,
1297 const struct rndis_hash_value *hash_value)
1299 struct ifnet *ifp = rxr->hn_ifp;
1301 int size, do_lro = 0, do_csum = 1;
1303 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1307 * Bail out if packet contains more data than configured MTU.
1309 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1311 } else if (packet->tot_data_buf_len <= MHLEN) {
1312 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1313 if (m_new == NULL) {
1314 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1317 memcpy(mtod(m_new, void *), packet->data,
1318 packet->tot_data_buf_len);
1319 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1320 rxr->hn_small_pkts++;
1323 * Get an mbuf with a cluster. For packets 2K or less,
1324 * get a standard 2K cluster. For anything larger, get a
1325 * 4K cluster. Any buffers larger than 4K can cause problems
1326 * if looped around to the Hyper-V TX channel, so avoid them.
1329 if (packet->tot_data_buf_len > MCLBYTES) {
1331 size = MJUMPAGESIZE;
1334 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1335 if (m_new == NULL) {
1336 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1340 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1342 m_new->m_pkthdr.rcvif = ifp;
1344 if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1347 /* receive side checksum offload */
1348 if (csum_info != NULL) {
1349 /* IP csum offload */
1350 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1351 m_new->m_pkthdr.csum_flags |=
1352 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1356 /* TCP/UDP csum offload */
1357 if ((csum_info->receive.tcp_csum_succeeded ||
1358 csum_info->receive.udp_csum_succeeded) && do_csum) {
1359 m_new->m_pkthdr.csum_flags |=
1360 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1361 m_new->m_pkthdr.csum_data = 0xffff;
1362 if (csum_info->receive.tcp_csum_succeeded)
1368 if (csum_info->receive.ip_csum_succeeded &&
1369 csum_info->receive.tcp_csum_succeeded)
1372 const struct ether_header *eh;
1377 if (m_new->m_len < hoff)
1379 eh = mtod(m_new, struct ether_header *);
1380 etype = ntohs(eh->ether_type);
1381 if (etype == ETHERTYPE_VLAN) {
1382 const struct ether_vlan_header *evl;
1384 hoff = sizeof(*evl);
1385 if (m_new->m_len < hoff)
1387 evl = mtod(m_new, struct ether_vlan_header *);
1388 etype = ntohs(evl->evl_proto);
1391 if (etype == ETHERTYPE_IP) {
1394 pr = hn_check_iplen(m_new, hoff);
1395 if (pr == IPPROTO_TCP) {
1397 (rxr->hn_trust_hcsum &
1398 HN_TRUST_HCSUM_TCP)) {
1399 rxr->hn_csum_trusted++;
1400 m_new->m_pkthdr.csum_flags |=
1401 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1402 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1403 m_new->m_pkthdr.csum_data = 0xffff;
1406 } else if (pr == IPPROTO_UDP) {
1408 (rxr->hn_trust_hcsum &
1409 HN_TRUST_HCSUM_UDP)) {
1410 rxr->hn_csum_trusted++;
1411 m_new->m_pkthdr.csum_flags |=
1412 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1413 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1414 m_new->m_pkthdr.csum_data = 0xffff;
1416 } else if (pr != IPPROTO_DONE && do_csum &&
1417 (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1418 rxr->hn_csum_trusted++;
1419 m_new->m_pkthdr.csum_flags |=
1420 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1425 if ((packet->vlan_tci != 0) &&
1426 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1427 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1428 m_new->m_flags |= M_VLANTAG;
1431 if (hash_info != NULL && hash_value != NULL) {
1432 int hash_type = M_HASHTYPE_OPAQUE;
1435 m_new->m_pkthdr.flowid = hash_value->hash_value;
1436 if ((hash_info->hash_info & NDIS_HASH_FUNCTION_MASK) ==
1437 NDIS_HASH_FUNCTION_TOEPLITZ) {
1439 (hash_info->hash_info & NDIS_HASH_TYPE_MASK);
1442 case NDIS_HASH_IPV4:
1443 hash_type = M_HASHTYPE_RSS_IPV4;
1446 case NDIS_HASH_TCP_IPV4:
1447 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
1450 case NDIS_HASH_IPV6:
1451 hash_type = M_HASHTYPE_RSS_IPV6;
1454 case NDIS_HASH_IPV6_EX:
1455 hash_type = M_HASHTYPE_RSS_IPV6_EX;
1458 case NDIS_HASH_TCP_IPV6:
1459 hash_type = M_HASHTYPE_RSS_TCP_IPV6;
1462 case NDIS_HASH_TCP_IPV6_EX:
1463 hash_type = M_HASHTYPE_RSS_TCP_IPV6_EX;
1467 M_HASHTYPE_SET(m_new, hash_type);
1469 if (hash_value != NULL)
1470 m_new->m_pkthdr.flowid = hash_value->hash_value;
1472 m_new->m_pkthdr.flowid = rxr->hn_rx_idx;
1473 M_HASHTYPE_SET(m_new, M_HASHTYPE_OPAQUE);
1477 * Note: Moved RX completion back to hv_nv_on_receive() so all
1478 * messages (not just data messages) will trigger a response.
1484 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1485 #if defined(INET) || defined(INET6)
1486 struct lro_ctrl *lro = &rxr->hn_lro;
1489 rxr->hn_lro_tried++;
1490 if (hn_lro_rx(lro, m_new) == 0) {
1498 /* We're not holding the lock here, so don't release it */
1499 (*ifp->if_input)(ifp, m_new);
1505 * Rules for using sc->temp_unusable:
1506 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
1507 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
1508 * sc->temp_unusable set, must release NV_LOCK() and exit
1509 * 3. to retain exclusive control of the interface,
1510 * sc->temp_unusable must be set by code before releasing NV_LOCK()
1511 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
1512 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
1516 * Standard ioctl entry point. Called when the user wants to configure
1520 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1522 hn_softc_t *sc = ifp->if_softc;
1523 struct ifreq *ifr = (struct ifreq *)data;
1525 struct ifaddr *ifa = (struct ifaddr *)data;
1527 netvsc_device_info device_info;
1528 int mask, error = 0;
1529 int retry_cnt = 500;
1535 if (ifa->ifa_addr->sa_family == AF_INET) {
1536 ifp->if_flags |= IFF_UP;
1537 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1539 arp_ifinit(ifp, ifa);
1542 error = ether_ioctl(ifp, cmd, data);
1545 /* Check MTU value change */
1546 if (ifp->if_mtu == ifr->ifr_mtu)
1549 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1554 /* Obtain and record requested MTU */
1555 ifp->if_mtu = ifr->ifr_mtu;
1557 #if __FreeBSD_version >= 1100099
1559 * Make sure that LRO aggregation length limit is still
1560 * valid, after the MTU change.
1563 if (sc->hn_rx_ring[0].hn_lro.lro_length_lim <
1564 HN_LRO_LENLIM_MIN(ifp))
1565 hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MIN(ifp));
1571 if (!sc->temp_unusable) {
1572 sc->temp_unusable = TRUE;
1576 if (retry_cnt > 0) {
1580 } while (retry_cnt > 0);
1582 if (retry_cnt == 0) {
1587 /* We must remove and add back the device to cause the new
1588 * MTU to take effect. This includes tearing down, but not
1589 * deleting the channel, then bringing it back up.
1591 error = hv_rf_on_device_remove(sc, HV_RF_NV_RETAIN_CHANNEL);
1594 sc->temp_unusable = FALSE;
1599 /* Wait for subchannels to be destroyed */
1600 vmbus_subchan_drain(sc->hn_prichan);
1602 error = hv_rf_on_device_add(sc, &device_info,
1603 sc->hn_rx_ring_inuse, &sc->hn_rx_ring[0]);
1606 sc->temp_unusable = FALSE;
1610 KASSERT(sc->hn_rx_ring_cnt == sc->net_dev->num_channel,
1611 ("RX ring count %d and channel count %u mismatch",
1612 sc->hn_rx_ring_cnt, sc->net_dev->num_channel));
1613 if (sc->net_dev->num_channel > 1) {
1617 * Skip the rings on primary channel; they are
1618 * handled by the hv_rf_on_device_add() above.
1620 for (r = 1; r < sc->hn_rx_ring_cnt; ++r) {
1621 sc->hn_rx_ring[r].hn_rx_flags &=
1622 ~HN_RX_FLAG_ATTACHED;
1624 for (r = 1; r < sc->hn_tx_ring_cnt; ++r) {
1625 sc->hn_tx_ring[r].hn_tx_flags &=
1626 ~HN_TX_FLAG_ATTACHED;
1628 hn_subchan_setup(sc);
1631 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1632 if (sc->hn_tx_ring[0].hn_tx_chimney_size >
1633 sc->hn_tx_chimney_max)
1634 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
1636 hn_ifinit_locked(sc);
1639 sc->temp_unusable = FALSE;
1645 if (!sc->temp_unusable) {
1646 sc->temp_unusable = TRUE;
1650 if (retry_cnt > 0) {
1654 } while (retry_cnt > 0);
1656 if (retry_cnt == 0) {
1661 if (ifp->if_flags & IFF_UP) {
1663 * If only the state of the PROMISC flag changed,
1664 * then just use the 'set promisc mode' command
1665 * instead of reinitializing the entire NIC. Doing
1666 * a full re-init means reloading the firmware and
1667 * waiting for it to start up, which may take a
1671 /* Fixme: Promiscuous mode? */
1672 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1673 ifp->if_flags & IFF_PROMISC &&
1674 !(sc->hn_if_flags & IFF_PROMISC)) {
1675 /* do something here for Hyper-V */
1676 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1677 !(ifp->if_flags & IFF_PROMISC) &&
1678 sc->hn_if_flags & IFF_PROMISC) {
1679 /* do something here for Hyper-V */
1682 hn_ifinit_locked(sc);
1684 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1689 sc->temp_unusable = FALSE;
1691 sc->hn_if_flags = ifp->if_flags;
1697 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1698 if (mask & IFCAP_TXCSUM) {
1699 ifp->if_capenable ^= IFCAP_TXCSUM;
1700 if (ifp->if_capenable & IFCAP_TXCSUM) {
1702 sc->hn_tx_ring[0].hn_csum_assist;
1705 ~sc->hn_tx_ring[0].hn_csum_assist;
1709 if (mask & IFCAP_RXCSUM)
1710 ifp->if_capenable ^= IFCAP_RXCSUM;
1712 if (mask & IFCAP_LRO)
1713 ifp->if_capenable ^= IFCAP_LRO;
1715 if (mask & IFCAP_TSO4) {
1716 ifp->if_capenable ^= IFCAP_TSO4;
1717 if (ifp->if_capenable & IFCAP_TSO4)
1718 ifp->if_hwassist |= CSUM_IP_TSO;
1720 ifp->if_hwassist &= ~CSUM_IP_TSO;
1723 if (mask & IFCAP_TSO6) {
1724 ifp->if_capenable ^= IFCAP_TSO6;
1725 if (ifp->if_capenable & IFCAP_TSO6)
1726 ifp->if_hwassist |= CSUM_IP6_TSO;
1728 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1737 /* Fixme: Multicast mode? */
1738 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1740 netvsc_setmulti(sc);
1749 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1752 error = ether_ioctl(ifp, cmd, data);
1763 hn_stop(hn_softc_t *sc)
1771 printf(" Closing Device ...\n");
1773 atomic_clear_int(&ifp->if_drv_flags,
1774 (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1775 for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
1776 sc->hn_tx_ring[i].hn_oactive = 0;
1778 if_link_state_change(ifp, LINK_STATE_DOWN);
1779 sc->hn_initdone = 0;
1781 ret = hv_rf_on_close(sc);
1785 * FreeBSD transmit entry point
1788 hn_start(struct ifnet *ifp)
1790 struct hn_softc *sc = ifp->if_softc;
1791 struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
1793 if (txr->hn_sched_tx)
1796 if (mtx_trylock(&txr->hn_tx_lock)) {
1799 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1800 mtx_unlock(&txr->hn_tx_lock);
1805 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
1809 hn_start_txeof(struct hn_tx_ring *txr)
1811 struct hn_softc *sc = txr->hn_sc;
1812 struct ifnet *ifp = sc->hn_ifp;
1814 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1816 if (txr->hn_sched_tx)
1819 if (mtx_trylock(&txr->hn_tx_lock)) {
1822 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1823 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1824 mtx_unlock(&txr->hn_tx_lock);
1826 taskqueue_enqueue(txr->hn_tx_taskq,
1832 * Release the OACTIVE earlier, with the hope, that
1833 * others could catch up. The task will clear the
1834 * flag again with the hn_tx_lock to avoid possible
1837 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1838 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
1846 hn_ifinit_locked(hn_softc_t *sc)
1853 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1857 hv_promisc_mode = 1;
1859 ret = hv_rf_on_open(sc);
1863 sc->hn_initdone = 1;
1866 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1867 for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
1868 sc->hn_tx_ring[i].hn_oactive = 0;
1870 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1871 if_link_state_change(ifp, LINK_STATE_UP);
1878 hn_ifinit(void *xsc)
1880 hn_softc_t *sc = xsc;
1883 if (sc->temp_unusable) {
1887 sc->temp_unusable = TRUE;
1890 hn_ifinit_locked(sc);
1893 sc->temp_unusable = FALSE;
1902 hn_watchdog(struct ifnet *ifp)
1907 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1908 hn_ifinit(sc); /*???*/
1913 #if __FreeBSD_version >= 1100099
1916 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1918 struct hn_softc *sc = arg1;
1919 unsigned int lenlim;
1922 lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
1923 error = sysctl_handle_int(oidp, &lenlim, 0, req);
1924 if (error || req->newptr == NULL)
1927 if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1928 lenlim > TCP_LRO_LENGTH_MAX)
1932 hn_set_lro_lenlim(sc, lenlim);
1938 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1940 struct hn_softc *sc = arg1;
1941 int ackcnt, error, i;
1944 * lro_ackcnt_lim is append count limit,
1945 * +1 to turn it into aggregation limit.
1947 ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
1948 error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1949 if (error || req->newptr == NULL)
1952 if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1956 * Convert aggregation limit back to append
1961 for (i = 0; i < sc->hn_rx_ring_inuse; ++i)
1962 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
1970 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1972 struct hn_softc *sc = arg1;
1977 if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
1980 error = sysctl_handle_int(oidp, &on, 0, req);
1981 if (error || req->newptr == NULL)
1985 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
1986 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1989 rxr->hn_trust_hcsum |= hcsum;
1991 rxr->hn_trust_hcsum &= ~hcsum;
1998 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
2000 struct hn_softc *sc = arg1;
2001 int chimney_size, error;
2003 chimney_size = sc->hn_tx_ring[0].hn_tx_chimney_size;
2004 error = sysctl_handle_int(oidp, &chimney_size, 0, req);
2005 if (error || req->newptr == NULL)
2008 if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
2011 hn_set_tx_chimney_size(sc, chimney_size);
2015 #if __FreeBSD_version < 1100095
2017 hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS)
2019 struct hn_softc *sc = arg1;
2020 int ofs = arg2, i, error;
2021 struct hn_rx_ring *rxr;
2025 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2026 rxr = &sc->hn_rx_ring[i];
2027 stat += *((int *)((uint8_t *)rxr + ofs));
2030 error = sysctl_handle_64(oidp, &stat, 0, req);
2031 if (error || req->newptr == NULL)
2034 /* Zero out this stat. */
2035 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2036 rxr = &sc->hn_rx_ring[i];
2037 *((int *)((uint8_t *)rxr + ofs)) = 0;
2043 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
2045 struct hn_softc *sc = arg1;
2046 int ofs = arg2, i, error;
2047 struct hn_rx_ring *rxr;
2051 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2052 rxr = &sc->hn_rx_ring[i];
2053 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
2056 error = sysctl_handle_64(oidp, &stat, 0, req);
2057 if (error || req->newptr == NULL)
2060 /* Zero out this stat. */
2061 for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
2062 rxr = &sc->hn_rx_ring[i];
2063 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
2071 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
2073 struct hn_softc *sc = arg1;
2074 int ofs = arg2, i, error;
2075 struct hn_rx_ring *rxr;
2079 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2080 rxr = &sc->hn_rx_ring[i];
2081 stat += *((u_long *)((uint8_t *)rxr + ofs));
2084 error = sysctl_handle_long(oidp, &stat, 0, req);
2085 if (error || req->newptr == NULL)
2088 /* Zero out this stat. */
2089 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2090 rxr = &sc->hn_rx_ring[i];
2091 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
2097 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
2099 struct hn_softc *sc = arg1;
2100 int ofs = arg2, i, error;
2101 struct hn_tx_ring *txr;
2105 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2106 txr = &sc->hn_tx_ring[i];
2107 stat += *((u_long *)((uint8_t *)txr + ofs));
2110 error = sysctl_handle_long(oidp, &stat, 0, req);
2111 if (error || req->newptr == NULL)
2114 /* Zero out this stat. */
2115 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2116 txr = &sc->hn_tx_ring[i];
2117 *((u_long *)((uint8_t *)txr + ofs)) = 0;
2123 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
2125 struct hn_softc *sc = arg1;
2126 int ofs = arg2, i, error, conf;
2127 struct hn_tx_ring *txr;
2129 txr = &sc->hn_tx_ring[0];
2130 conf = *((int *)((uint8_t *)txr + ofs));
2132 error = sysctl_handle_int(oidp, &conf, 0, req);
2133 if (error || req->newptr == NULL)
2137 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2138 txr = &sc->hn_tx_ring[i];
2139 *((int *)((uint8_t *)txr + ofs)) = conf;
2147 hn_check_iplen(const struct mbuf *m, int hoff)
2149 const struct ip *ip;
2150 int len, iphlen, iplen;
2151 const struct tcphdr *th;
2152 int thoff; /* TCP data offset */
2154 len = hoff + sizeof(struct ip);
2156 /* The packet must be at least the size of an IP header. */
2157 if (m->m_pkthdr.len < len)
2158 return IPPROTO_DONE;
2160 /* The fixed IP header must reside completely in the first mbuf. */
2162 return IPPROTO_DONE;
2164 ip = mtodo(m, hoff);
2166 /* Bound check the packet's stated IP header length. */
2167 iphlen = ip->ip_hl << 2;
2168 if (iphlen < sizeof(struct ip)) /* minimum header length */
2169 return IPPROTO_DONE;
2171 /* The full IP header must reside completely in the one mbuf. */
2172 if (m->m_len < hoff + iphlen)
2173 return IPPROTO_DONE;
2175 iplen = ntohs(ip->ip_len);
2178 * Check that the amount of data in the buffers is as
2179 * at least much as the IP header would have us expect.
2181 if (m->m_pkthdr.len < hoff + iplen)
2182 return IPPROTO_DONE;
2185 * Ignore IP fragments.
2187 if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
2188 return IPPROTO_DONE;
2191 * The TCP/IP or UDP/IP header must be entirely contained within
2192 * the first fragment of a packet.
2196 if (iplen < iphlen + sizeof(struct tcphdr))
2197 return IPPROTO_DONE;
2198 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
2199 return IPPROTO_DONE;
2200 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
2201 thoff = th->th_off << 2;
2202 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
2203 return IPPROTO_DONE;
2204 if (m->m_len < hoff + iphlen + thoff)
2205 return IPPROTO_DONE;
2208 if (iplen < iphlen + sizeof(struct udphdr))
2209 return IPPROTO_DONE;
2210 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
2211 return IPPROTO_DONE;
2215 return IPPROTO_DONE;
2222 hn_create_rx_data(struct hn_softc *sc, int ring_cnt)
2224 struct sysctl_oid_list *child;
2225 struct sysctl_ctx_list *ctx;
2226 device_t dev = sc->hn_dev;
2227 #if defined(INET) || defined(INET6)
2228 #if __FreeBSD_version >= 1100095
2234 sc->hn_rx_ring_cnt = ring_cnt;
2235 sc->hn_rx_ring_inuse = sc->hn_rx_ring_cnt;
2237 sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
2238 M_NETVSC, M_WAITOK | M_ZERO);
2240 #if defined(INET) || defined(INET6)
2241 #if __FreeBSD_version >= 1100095
2242 lroent_cnt = hn_lro_entry_count;
2243 if (lroent_cnt < TCP_LRO_ENTRIES)
2244 lroent_cnt = TCP_LRO_ENTRIES;
2245 device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
2247 #endif /* INET || INET6 */
2249 ctx = device_get_sysctl_ctx(dev);
2250 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2252 /* Create dev.hn.UNIT.rx sysctl tree */
2253 sc->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "rx",
2254 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2256 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2257 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
2259 if (hn_trust_hosttcp)
2260 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
2261 if (hn_trust_hostudp)
2262 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
2263 if (hn_trust_hostip)
2264 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
2265 rxr->hn_ifp = sc->hn_ifp;
2266 if (i < sc->hn_tx_ring_cnt)
2267 rxr->hn_txr = &sc->hn_tx_ring[i];
2268 rxr->hn_rdbuf = malloc(NETVSC_PACKET_SIZE, M_NETVSC, M_WAITOK);
2274 #if defined(INET) || defined(INET6)
2275 #if __FreeBSD_version >= 1100095
2276 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt,
2277 hn_lro_mbufq_depth);
2279 tcp_lro_init(&rxr->hn_lro);
2280 rxr->hn_lro.ifp = sc->hn_ifp;
2282 #if __FreeBSD_version >= 1100099
2283 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
2284 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
2286 #endif /* INET || INET6 */
2288 if (sc->hn_rx_sysctl_tree != NULL) {
2292 * Create per RX ring sysctl tree:
2293 * dev.hn.UNIT.rx.RINGID
2295 snprintf(name, sizeof(name), "%d", i);
2296 rxr->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx,
2297 SYSCTL_CHILDREN(sc->hn_rx_sysctl_tree),
2298 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2300 if (rxr->hn_rx_sysctl_tree != NULL) {
2301 SYSCTL_ADD_ULONG(ctx,
2302 SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
2303 OID_AUTO, "packets", CTLFLAG_RW,
2304 &rxr->hn_pkts, "# of packets received");
2305 SYSCTL_ADD_ULONG(ctx,
2306 SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
2307 OID_AUTO, "rss_pkts", CTLFLAG_RW,
2309 "# of packets w/ RSS info received");
2314 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
2315 CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2316 __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
2317 #if __FreeBSD_version < 1100095
2318 hn_rx_stat_int_sysctl,
2320 hn_rx_stat_u64_sysctl,
2322 "LU", "LRO queued");
2323 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
2324 CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2325 __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
2326 #if __FreeBSD_version < 1100095
2327 hn_rx_stat_int_sysctl,
2329 hn_rx_stat_u64_sysctl,
2331 "LU", "LRO flushed");
2332 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
2333 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2334 __offsetof(struct hn_rx_ring, hn_lro_tried),
2335 hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
2336 #if __FreeBSD_version >= 1100099
2337 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
2338 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2339 hn_lro_lenlim_sysctl, "IU",
2340 "Max # of data bytes to be aggregated by LRO");
2341 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
2342 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2343 hn_lro_ackcnt_sysctl, "I",
2344 "Max # of ACKs to be aggregated by LRO");
2346 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
2347 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_TCP,
2348 hn_trust_hcsum_sysctl, "I",
2349 "Trust tcp segement verification on host side, "
2350 "when csum info is missing");
2351 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2352 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_UDP,
2353 hn_trust_hcsum_sysctl, "I",
2354 "Trust udp datagram verification on host side, "
2355 "when csum info is missing");
2356 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2357 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_IP,
2358 hn_trust_hcsum_sysctl, "I",
2359 "Trust ip packet verification on host side, "
2360 "when csum info is missing");
2361 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2362 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2363 __offsetof(struct hn_rx_ring, hn_csum_ip),
2364 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2365 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2366 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2367 __offsetof(struct hn_rx_ring, hn_csum_tcp),
2368 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2369 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2370 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2371 __offsetof(struct hn_rx_ring, hn_csum_udp),
2372 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2373 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2374 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2375 __offsetof(struct hn_rx_ring, hn_csum_trusted),
2376 hn_rx_stat_ulong_sysctl, "LU",
2377 "# of packets that we trust host's csum verification");
2378 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2379 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2380 __offsetof(struct hn_rx_ring, hn_small_pkts),
2381 hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2382 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_cnt",
2383 CTLFLAG_RD, &sc->hn_rx_ring_cnt, 0, "# created RX rings");
2384 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_inuse",
2385 CTLFLAG_RD, &sc->hn_rx_ring_inuse, 0, "# used RX rings");
2389 hn_destroy_rx_data(struct hn_softc *sc)
2393 if (sc->hn_rx_ring_cnt == 0)
2396 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2397 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
2399 #if defined(INET) || defined(INET6)
2400 tcp_lro_free(&rxr->hn_lro);
2402 free(rxr->hn_rdbuf, M_NETVSC);
2404 free(sc->hn_rx_ring, M_NETVSC);
2405 sc->hn_rx_ring = NULL;
2407 sc->hn_rx_ring_cnt = 0;
2408 sc->hn_rx_ring_inuse = 0;
2412 hn_create_tx_ring(struct hn_softc *sc, int id)
2414 struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2415 device_t dev = sc->hn_dev;
2416 bus_dma_tag_t parent_dtag;
2421 txr->hn_tx_idx = id;
2423 #ifndef HN_USE_TXDESC_BUFRING
2424 mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2426 mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2428 txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2429 txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2430 M_NETVSC, M_WAITOK | M_ZERO);
2431 #ifndef HN_USE_TXDESC_BUFRING
2432 SLIST_INIT(&txr->hn_txlist);
2434 txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2435 M_WAITOK, &txr->hn_tx_lock);
2438 txr->hn_tx_taskq = sc->hn_tx_taskq;
2440 if (hn_use_if_start) {
2441 txr->hn_txeof = hn_start_txeof;
2442 TASK_INIT(&txr->hn_tx_task, 0, hn_start_taskfunc, txr);
2443 TASK_INIT(&txr->hn_txeof_task, 0, hn_start_txeof_taskfunc, txr);
2447 txr->hn_txeof = hn_xmit_txeof;
2448 TASK_INIT(&txr->hn_tx_task, 0, hn_xmit_taskfunc, txr);
2449 TASK_INIT(&txr->hn_txeof_task, 0, hn_xmit_txeof_taskfunc, txr);
2451 br_depth = hn_get_txswq_depth(txr);
2452 txr->hn_mbuf_br = buf_ring_alloc(br_depth, M_NETVSC,
2453 M_WAITOK, &txr->hn_tx_lock);
2456 txr->hn_direct_tx_size = hn_direct_tx_size;
2457 version = VMBUS_GET_VERSION(device_get_parent(dev), dev);
2458 if (version >= VMBUS_VERSION_WIN8_1) {
2459 txr->hn_csum_assist = HN_CSUM_ASSIST;
2461 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2463 device_printf(dev, "bus version %u.%u, "
2464 "no UDP checksum offloading\n",
2465 VMBUS_VERSION_MAJOR(version),
2466 VMBUS_VERSION_MINOR(version));
2471 * Always schedule transmission instead of trying to do direct
2472 * transmission. This one gives the best performance so far.
2474 txr->hn_sched_tx = 1;
2476 parent_dtag = bus_get_dma_tag(dev);
2478 /* DMA tag for RNDIS messages. */
2479 error = bus_dma_tag_create(parent_dtag, /* parent */
2480 HN_RNDIS_MSG_ALIGN, /* alignment */
2481 HN_RNDIS_MSG_BOUNDARY, /* boundary */
2482 BUS_SPACE_MAXADDR, /* lowaddr */
2483 BUS_SPACE_MAXADDR, /* highaddr */
2484 NULL, NULL, /* filter, filterarg */
2485 HN_RNDIS_MSG_LEN, /* maxsize */
2487 HN_RNDIS_MSG_LEN, /* maxsegsize */
2489 NULL, /* lockfunc */
2490 NULL, /* lockfuncarg */
2491 &txr->hn_tx_rndis_dtag);
2493 device_printf(dev, "failed to create rndis dmatag\n");
2497 /* DMA tag for data. */
2498 error = bus_dma_tag_create(parent_dtag, /* parent */
2500 HN_TX_DATA_BOUNDARY, /* boundary */
2501 BUS_SPACE_MAXADDR, /* lowaddr */
2502 BUS_SPACE_MAXADDR, /* highaddr */
2503 NULL, NULL, /* filter, filterarg */
2504 HN_TX_DATA_MAXSIZE, /* maxsize */
2505 HN_TX_DATA_SEGCNT_MAX, /* nsegments */
2506 HN_TX_DATA_SEGSIZE, /* maxsegsize */
2508 NULL, /* lockfunc */
2509 NULL, /* lockfuncarg */
2510 &txr->hn_tx_data_dtag);
2512 device_printf(dev, "failed to create data dmatag\n");
2516 for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2517 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2522 * Allocate and load RNDIS messages.
2524 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2525 (void **)&txd->rndis_msg,
2526 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2527 &txd->rndis_msg_dmap);
2530 "failed to allocate rndis_msg, %d\n", i);
2534 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2535 txd->rndis_msg_dmap,
2536 txd->rndis_msg, HN_RNDIS_MSG_LEN,
2537 hyperv_dma_map_paddr, &txd->rndis_msg_paddr,
2541 "failed to load rndis_msg, %d\n", i);
2542 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2543 txd->rndis_msg, txd->rndis_msg_dmap);
2547 /* DMA map for TX data. */
2548 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2552 "failed to allocate tx data dmamap\n");
2553 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2554 txd->rndis_msg_dmap);
2555 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2556 txd->rndis_msg, txd->rndis_msg_dmap);
2560 /* All set, put it to list */
2561 txd->flags |= HN_TXD_FLAG_ONLIST;
2562 #ifndef HN_USE_TXDESC_BUFRING
2563 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2565 buf_ring_enqueue(txr->hn_txdesc_br, txd);
2568 txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2570 if (sc->hn_tx_sysctl_tree != NULL) {
2571 struct sysctl_oid_list *child;
2572 struct sysctl_ctx_list *ctx;
2576 * Create per TX ring sysctl tree:
2577 * dev.hn.UNIT.tx.RINGID
2579 ctx = device_get_sysctl_ctx(dev);
2580 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2582 snprintf(name, sizeof(name), "%d", id);
2583 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2584 name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2586 if (txr->hn_tx_sysctl_tree != NULL) {
2587 child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2589 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2590 CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2591 "# of available TX descs");
2592 if (!hn_use_if_start) {
2593 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "oactive",
2594 CTLFLAG_RD, &txr->hn_oactive, 0,
2597 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "packets",
2598 CTLFLAG_RW, &txr->hn_pkts,
2599 "# of packets transmitted");
2607 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
2609 struct hn_tx_ring *txr = txd->txr;
2611 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2612 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
2614 bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_msg_dmap);
2615 bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_msg,
2616 txd->rndis_msg_dmap);
2617 bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2621 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2623 struct hn_txdesc *txd;
2625 if (txr->hn_txdesc == NULL)
2628 #ifndef HN_USE_TXDESC_BUFRING
2629 while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2630 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2631 hn_txdesc_dmamap_destroy(txd);
2634 mtx_lock(&txr->hn_tx_lock);
2635 while ((txd = buf_ring_dequeue_sc(txr->hn_txdesc_br)) != NULL)
2636 hn_txdesc_dmamap_destroy(txd);
2637 mtx_unlock(&txr->hn_tx_lock);
2640 if (txr->hn_tx_data_dtag != NULL)
2641 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2642 if (txr->hn_tx_rndis_dtag != NULL)
2643 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2645 #ifdef HN_USE_TXDESC_BUFRING
2646 buf_ring_free(txr->hn_txdesc_br, M_NETVSC);
2649 free(txr->hn_txdesc, M_NETVSC);
2650 txr->hn_txdesc = NULL;
2652 if (txr->hn_mbuf_br != NULL)
2653 buf_ring_free(txr->hn_mbuf_br, M_NETVSC);
2655 #ifndef HN_USE_TXDESC_BUFRING
2656 mtx_destroy(&txr->hn_txlist_spin);
2658 mtx_destroy(&txr->hn_tx_lock);
2662 hn_create_tx_data(struct hn_softc *sc, int ring_cnt)
2664 struct sysctl_oid_list *child;
2665 struct sysctl_ctx_list *ctx;
2668 sc->hn_tx_ring_cnt = ring_cnt;
2669 sc->hn_tx_ring_inuse = sc->hn_tx_ring_cnt;
2671 sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2672 M_NETVSC, M_WAITOK | M_ZERO);
2674 ctx = device_get_sysctl_ctx(sc->hn_dev);
2675 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2677 /* Create dev.hn.UNIT.tx sysctl tree */
2678 sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2679 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2681 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2684 error = hn_create_tx_ring(sc, i);
2689 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2690 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2691 __offsetof(struct hn_tx_ring, hn_no_txdescs),
2692 hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2693 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2694 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2695 __offsetof(struct hn_tx_ring, hn_send_failed),
2696 hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2697 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2698 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2699 __offsetof(struct hn_tx_ring, hn_txdma_failed),
2700 hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2701 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2702 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2703 __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2704 hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2705 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2706 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2707 __offsetof(struct hn_tx_ring, hn_tx_chimney),
2708 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2709 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_tried",
2710 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2711 __offsetof(struct hn_tx_ring, hn_tx_chimney_tried),
2712 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send tries");
2713 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2714 CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2715 "# of total TX descs");
2716 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2717 CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2718 "Chimney send packet size upper boundary");
2719 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2720 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2721 hn_tx_chimney_size_sysctl,
2722 "I", "Chimney send packet size limit");
2723 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2724 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2725 __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2726 hn_tx_conf_int_sysctl, "I",
2727 "Size of the packet for direct transmission");
2728 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2729 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2730 __offsetof(struct hn_tx_ring, hn_sched_tx),
2731 hn_tx_conf_int_sysctl, "I",
2732 "Always schedule transmission "
2733 "instead of doing direct transmission");
2734 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_cnt",
2735 CTLFLAG_RD, &sc->hn_tx_ring_cnt, 0, "# created TX rings");
2736 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_inuse",
2737 CTLFLAG_RD, &sc->hn_tx_ring_inuse, 0, "# used TX rings");
2743 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2748 for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
2749 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2754 hn_destroy_tx_data(struct hn_softc *sc)
2758 if (sc->hn_tx_ring_cnt == 0)
2761 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2762 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2764 free(sc->hn_tx_ring, M_NETVSC);
2765 sc->hn_tx_ring = NULL;
2767 sc->hn_tx_ring_cnt = 0;
2768 sc->hn_tx_ring_inuse = 0;
2772 hn_start_taskfunc(void *xtxr, int pending __unused)
2774 struct hn_tx_ring *txr = xtxr;
2776 mtx_lock(&txr->hn_tx_lock);
2777 hn_start_locked(txr, 0);
2778 mtx_unlock(&txr->hn_tx_lock);
2782 hn_start_txeof_taskfunc(void *xtxr, int pending __unused)
2784 struct hn_tx_ring *txr = xtxr;
2786 mtx_lock(&txr->hn_tx_lock);
2787 atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2788 hn_start_locked(txr, 0);
2789 mtx_unlock(&txr->hn_tx_lock);
2793 hn_stop_tx_tasks(struct hn_softc *sc)
2797 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2798 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2800 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_tx_task);
2801 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2806 hn_xmit(struct hn_tx_ring *txr, int len)
2808 struct hn_softc *sc = txr->hn_sc;
2809 struct ifnet *ifp = sc->hn_ifp;
2810 struct mbuf *m_head;
2812 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
2813 KASSERT(hn_use_if_start == 0,
2814 ("hn_xmit is called, when if_start is enabled"));
2816 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || txr->hn_oactive)
2819 while ((m_head = drbr_peek(ifp, txr->hn_mbuf_br)) != NULL) {
2820 struct hn_txdesc *txd;
2823 if (len > 0 && m_head->m_pkthdr.len > len) {
2825 * This sending could be time consuming; let callers
2826 * dispatch this packet sending (and sending of any
2827 * following up packets) to tx taskqueue.
2829 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2833 txd = hn_txdesc_get(txr);
2835 txr->hn_no_txdescs++;
2836 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2837 txr->hn_oactive = 1;
2841 error = hn_encap(txr, txd, &m_head);
2843 /* Both txd and m_head are freed; discard */
2844 drbr_advance(ifp, txr->hn_mbuf_br);
2848 error = hn_send_pkt(ifp, txr, txd);
2849 if (__predict_false(error)) {
2850 /* txd is freed, but m_head is not */
2851 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2852 txr->hn_oactive = 1;
2857 drbr_advance(ifp, txr->hn_mbuf_br);
2863 hn_transmit(struct ifnet *ifp, struct mbuf *m)
2865 struct hn_softc *sc = ifp->if_softc;
2866 struct hn_tx_ring *txr;
2870 * Select the TX ring based on flowid
2872 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2873 idx = m->m_pkthdr.flowid % sc->hn_tx_ring_inuse;
2874 txr = &sc->hn_tx_ring[idx];
2876 error = drbr_enqueue(ifp, txr->hn_mbuf_br, m);
2878 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
2882 if (txr->hn_oactive)
2885 if (txr->hn_sched_tx)
2888 if (mtx_trylock(&txr->hn_tx_lock)) {
2891 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2892 mtx_unlock(&txr->hn_tx_lock);
2897 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
2902 hn_xmit_qflush(struct ifnet *ifp)
2904 struct hn_softc *sc = ifp->if_softc;
2907 for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2908 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2911 mtx_lock(&txr->hn_tx_lock);
2912 while ((m = buf_ring_dequeue_sc(txr->hn_mbuf_br)) != NULL)
2914 mtx_unlock(&txr->hn_tx_lock);
2920 hn_xmit_txeof(struct hn_tx_ring *txr)
2923 if (txr->hn_sched_tx)
2926 if (mtx_trylock(&txr->hn_tx_lock)) {
2929 txr->hn_oactive = 0;
2930 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2931 mtx_unlock(&txr->hn_tx_lock);
2933 taskqueue_enqueue(txr->hn_tx_taskq,
2939 * Release the oactive earlier, with the hope, that
2940 * others could catch up. The task will clear the
2941 * oactive again with the hn_tx_lock to avoid possible
2944 txr->hn_oactive = 0;
2945 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
2950 hn_xmit_taskfunc(void *xtxr, int pending __unused)
2952 struct hn_tx_ring *txr = xtxr;
2954 mtx_lock(&txr->hn_tx_lock);
2956 mtx_unlock(&txr->hn_tx_lock);
2960 hn_xmit_txeof_taskfunc(void *xtxr, int pending __unused)
2962 struct hn_tx_ring *txr = xtxr;
2964 mtx_lock(&txr->hn_tx_lock);
2965 txr->hn_oactive = 0;
2967 mtx_unlock(&txr->hn_tx_lock);
2971 hn_channel_attach(struct hn_softc *sc, struct vmbus_channel *chan)
2973 struct hn_rx_ring *rxr;
2976 idx = vmbus_chan_subidx(chan);
2978 KASSERT(idx >= 0 && idx < sc->hn_rx_ring_inuse,
2979 ("invalid channel index %d, should > 0 && < %d",
2980 idx, sc->hn_rx_ring_inuse));
2981 rxr = &sc->hn_rx_ring[idx];
2982 KASSERT((rxr->hn_rx_flags & HN_RX_FLAG_ATTACHED) == 0,
2983 ("RX ring %d already attached", idx));
2984 rxr->hn_rx_flags |= HN_RX_FLAG_ATTACHED;
2987 if_printf(sc->hn_ifp, "link RX ring %d to channel%u\n",
2988 idx, vmbus_chan_id(chan));
2991 if (idx < sc->hn_tx_ring_inuse) {
2992 struct hn_tx_ring *txr = &sc->hn_tx_ring[idx];
2994 KASSERT((txr->hn_tx_flags & HN_TX_FLAG_ATTACHED) == 0,
2995 ("TX ring %d already attached", idx));
2996 txr->hn_tx_flags |= HN_TX_FLAG_ATTACHED;
2998 txr->hn_chan = chan;
3000 if_printf(sc->hn_ifp, "link TX ring %d to channel%u\n",
3001 idx, vmbus_chan_id(chan));
3005 /* Bind channel to a proper CPU */
3006 vmbus_chan_cpu_set(chan, (sc->hn_cpu + idx) % mp_ncpus);
3010 hn_subchan_attach(struct hn_softc *sc, struct vmbus_channel *chan)
3013 KASSERT(!vmbus_chan_is_primary(chan),
3014 ("subchannel callback on primary channel"));
3015 hn_channel_attach(sc, chan);
3019 hn_subchan_setup(struct hn_softc *sc)
3021 struct vmbus_channel **subchans;
3022 int subchan_cnt = sc->net_dev->num_channel - 1;
3025 /* Wait for sub-channels setup to complete. */
3026 subchans = vmbus_subchan_get(sc->hn_prichan, subchan_cnt);
3028 /* Attach the sub-channels. */
3029 for (i = 0; i < subchan_cnt; ++i) {
3030 struct vmbus_channel *subchan = subchans[i];
3032 /* NOTE: Calling order is critical. */
3033 hn_subchan_attach(sc, subchan);
3034 hv_nv_subchan_attach(subchan,
3035 &sc->hn_rx_ring[vmbus_chan_subidx(subchan)]);
3038 /* Release the sub-channels */
3039 vmbus_subchan_rel(subchans, subchan_cnt);
3040 if_printf(sc->hn_ifp, "%d sub-channels setup done\n", subchan_cnt);
3044 hn_tx_taskq_create(void *arg __unused)
3046 if (!hn_share_tx_taskq)
3049 hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
3050 taskqueue_thread_enqueue, &hn_tx_taskq);
3051 taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
3052 if (hn_bind_tx_taskq >= 0) {
3053 int cpu = hn_bind_tx_taskq;
3054 struct task cpuset_task;
3057 if (cpu > mp_ncpus - 1)
3059 CPU_SETOF(cpu, &cpu_set);
3060 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task, &cpu_set);
3061 taskqueue_enqueue(hn_tx_taskq, &cpuset_task);
3062 taskqueue_drain(hn_tx_taskq, &cpuset_task);
3065 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
3066 hn_tx_taskq_create, NULL);
3069 hn_tx_taskq_destroy(void *arg __unused)
3071 if (hn_tx_taskq != NULL)
3072 taskqueue_free(hn_tx_taskq);
3074 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
3075 hn_tx_taskq_destroy, NULL);
3077 static device_method_t netvsc_methods[] = {
3078 /* Device interface */
3079 DEVMETHOD(device_probe, netvsc_probe),
3080 DEVMETHOD(device_attach, netvsc_attach),
3081 DEVMETHOD(device_detach, netvsc_detach),
3082 DEVMETHOD(device_shutdown, netvsc_shutdown),
3087 static driver_t netvsc_driver = {
3093 static devclass_t netvsc_devclass;
3095 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
3096 MODULE_VERSION(hn, 1);
3097 MODULE_DEPEND(hn, vmbus, 1, 1, 1);